blob: 792cc4bf2432a779bc312b7931cc20ad4f57bff5 [file] [log] [blame]
Damjan Marion91f17dc2019-03-18 18:59:25 +01001/*
2 * Copyright (c) 2019 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <stdbool.h>
17#include <vlib/vlib.h>
18#include <vnet/crypto/crypto.h>
19
20static clib_error_t *
21show_crypto_engines_command_fn (vlib_main_t * vm,
22 unformat_input_t * input,
23 vlib_cli_command_t * cmd)
24{
25 unformat_input_t _line_input, *line_input = &_line_input;
26 vnet_crypto_main_t *cm = &crypto_main;
27 vnet_crypto_engine_t *p;
28
29 if (unformat_user (input, unformat_line_input, line_input))
30 unformat_free (line_input);
31
32 if (vec_len (cm->engines) == 0)
33 {
34 vlib_cli_output (vm, "No crypto engines registered");
35 return 0;
36 }
37
38 vlib_cli_output (vm, "%-20s%-8s%s", "Name", "Prio", "Description");
39 /* *INDENT-OFF* */
40 vec_foreach (p, cm->engines)
41 {
42 vlib_cli_output (vm, "%-20s%-8u%s", p->name, p->priority, p->desc);
43 }
44 /* *INDENT-ON* */
45 return 0;
46}
47
48/* *INDENT-OFF* */
49VLIB_CLI_COMMAND (show_crypto_engines_command, static) =
50{
51 .path = "show crypto engines",
52 .short_help = "show crypto engines",
53 .function = show_crypto_engines_command_fn,
54};
55
56static clib_error_t *
57show_crypto_handlers_command_fn (vlib_main_t * vm,
58 unformat_input_t * input, vlib_cli_command_t * cmd)
59{
60 vnet_crypto_main_t *cm = &crypto_main;
61 unformat_input_t _line_input, *line_input = &_line_input;
62 u8 *s = 0;
63
64 if (unformat_user (input, unformat_line_input, line_input))
65 unformat_free (line_input);
66
67 vlib_cli_output (vm, "%-40s%-20s%s", "Name", "Active", "Candidates");
68 for (int i = 1; i < VNET_CRYPTO_N_OP_TYPES; i++)
69 {
70 vnet_crypto_op_type_data_t *otd = cm->opt_data + i;
71 vnet_crypto_engine_t *e;
72
73 vec_reset_length (s);
74 vec_foreach (e, cm->engines)
75 {
76 if (e->ops_handlers[i] != 0)
77 s = format (s, "%U ", format_vnet_crypto_engine, e - cm->engines);
78 }
79 vlib_cli_output (vm, "%-40U%-20U%v", format_vnet_crypto_op, i,
80 format_vnet_crypto_engine, otd->active_engine_index,s);
81 }
82 vec_free (s);
83 return 0;
84}
85
86/* *INDENT-OFF* */
87VLIB_CLI_COMMAND (show_crypto_handlers_command, static) =
88{
89 .path = "show crypto handlers",
90 .short_help = "show crypto handlers",
91 .function = show_crypto_handlers_command_fn,
92};
93/* *INDENT-ON* */
94
Filip Tehlar1469d542019-03-25 09:04:41 -070095static clib_error_t *
96set_crypto_handler_command_fn (vlib_main_t * vm,
97 unformat_input_t * input,
98 vlib_cli_command_t * cmd)
99{
100 unformat_input_t _line_input, *line_input = &_line_input;
101 vnet_crypto_main_t *cm = &crypto_main;
102 int rc = 0;
103 char **args = 0, *s, **arg, *engine = 0;
104 int all = 0;
105 clib_error_t *error = 0;
106
107 if (!unformat_user (input, unformat_line_input, line_input))
108 return 0;
109
110 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
111 {
112 if (unformat (line_input, "all"))
113 all = 1;
114 else if (unformat (line_input, "%s", &s))
115 vec_add1 (args, s);
116 else
117 {
118 error = clib_error_return (0, "invalid params");
119 goto done;
120 }
121 }
122
123 if ((vec_len (args) < 2 && !all) || (vec_len (args) == 0 && all))
124 {
125 error = clib_error_return (0, "missing cipher or engine!");
126 goto done;
127 }
128
129 engine = vec_elt_at_index (args, vec_len (args) - 1)[0];
130 vec_del1 (args, vec_len (args) - 1);
131
132 if (all)
133 {
134 char *key;
135 u8 *value;
136
137 /* *INDENT-OFF* */
138 hash_foreach_mem (key, value, cm->ops_handler_index_by_name,
139 ({
140 (void) value;
141 rc += vnet_crypto_set_handler (key, engine);
142 }));
143 /* *INDENT-ON* */
144
145 if (rc)
146 vlib_cli_output (vm, "failed to set crypto engine!");
147 }
148 else
149 {
150 vec_foreach (arg, args)
151 {
152 rc = vnet_crypto_set_handler (arg[0], engine);
153 if (rc)
154 {
155 vlib_cli_output (vm, "failed to set engine %s for %s!",
156 engine, arg[0]);
157 }
158 }
159 }
160
161done:
162 vec_free (engine);
163 vec_foreach (arg, args) vec_free (arg[0]);
164 vec_free (args);
165 unformat_free (line_input);
166 return error;
167}
168
169/* *INDENT-OFF* */
170VLIB_CLI_COMMAND (set_crypto_handler_command, static) =
171{
172 .path = "set crypto handler",
173 .short_help = "set crypto handler cipher [cipher2 cipher3 ...] engine",
174 .function = set_crypto_handler_command_fn,
175};
176/* *INDENT-ON* */
177
Damjan Marion91f17dc2019-03-18 18:59:25 +0100178/*
179 * fd.io coding-style-patch-verification: ON
180 *
181 * Local Variables:
182 * eval: (c-set-style "gnu")
183 * End:
184 */