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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <vlib/vlib.h>
18 #include <vnet/crypto/crypto.h>
21 show_crypto_engines_command_fn (vlib_main_t * vm,
22 unformat_input_t * input,
23 vlib_cli_command_t * cmd)
25 unformat_input_t _line_input, *line_input = &_line_input;
26 vnet_crypto_main_t *cm = &crypto_main;
27 vnet_crypto_engine_t *p;
29 if (unformat_user (input, unformat_line_input, line_input))
30 unformat_free (line_input);
32 if (vec_len (cm->engines) == 0)
34 vlib_cli_output (vm, "No crypto engines registered");
38 vlib_cli_output (vm, "%-20s%-8s%s", "Name", "Prio", "Description");
39 vec_foreach (p, cm->engines)
41 vlib_cli_output (vm, "%-20s%-8u%s", p->name, p->priority, p->desc);
46 VLIB_CLI_COMMAND (show_crypto_engines_command, static) =
48 .path = "show crypto engines",
49 .short_help = "show crypto engines",
50 .function = show_crypto_engines_command_fn,
54 format_vnet_crypto_engine_candidates (u8 * s, va_list * args)
56 vnet_crypto_engine_t *e;
57 vnet_crypto_main_t *cm = &crypto_main;
58 u32 id = va_arg (*args, u32);
59 u32 ei = va_arg (*args, u32);
60 int is_chained = va_arg (*args, int);
61 int is_async = va_arg (*args, int);
65 vec_foreach (e, cm->engines)
67 if (e->enqueue_handlers[id] && e->dequeue_handler)
69 s = format (s, "%U", format_vnet_crypto_engine, e - cm->engines);
70 if (ei == e - cm->engines)
71 s = format (s, "%c ", '*');
81 vec_foreach (e, cm->engines)
83 void * h = is_chained ? (void *) e->chained_ops_handlers[id]
84 : (void *) e->ops_handlers[id];
88 s = format (s, "%U", format_vnet_crypto_engine, e - cm->engines);
89 if (ei == e - cm->engines)
90 s = format (s, "%c ", '*');
100 format_vnet_crypto_handlers (u8 * s, va_list * args)
102 vnet_crypto_alg_t alg = va_arg (*args, vnet_crypto_alg_t);
103 vnet_crypto_main_t *cm = &crypto_main;
104 vnet_crypto_alg_data_t *d = vec_elt_at_index (cm->algs, alg);
105 u32 indent = format_get_indent (s);
108 for (i = 0; i < VNET_CRYPTO_OP_N_TYPES; i++)
110 vnet_crypto_op_data_t *od;
111 vnet_crypto_op_id_t id = d->op_by_type[i];
116 od = cm->opt_data + id;
118 s = format (s, "\n%U", format_white_space, indent);
119 s = format (s, "%-16U", format_vnet_crypto_op_type, od->type);
121 s = format (s, "%-28U", format_vnet_crypto_engine_candidates, id,
122 od->active_engine_index_simple, 0, 0);
123 s = format (s, "%U", format_vnet_crypto_engine_candidates, id,
124 od->active_engine_index_chained, 1, 0);
131 static clib_error_t *
132 show_crypto_handlers_command_fn (vlib_main_t * vm,
133 unformat_input_t * input, vlib_cli_command_t * cmd)
135 unformat_input_t _line_input, *line_input = &_line_input;
138 if (unformat_user (input, unformat_line_input, line_input))
139 unformat_free (line_input);
141 vlib_cli_output (vm, "%-16s%-16s%-28s%s", "Algo", "Type", "Simple",
144 for (i = 0; i < VNET_CRYPTO_N_ALGS; i++)
145 vlib_cli_output (vm, "%-20U%U", format_vnet_crypto_alg, i,
146 format_vnet_crypto_handlers, i);
151 VLIB_CLI_COMMAND (show_crypto_handlers_command, static) =
153 .path = "show crypto handlers",
154 .short_help = "show crypto handlers",
155 .function = show_crypto_handlers_command_fn,
158 static clib_error_t *
159 set_crypto_handler_command_fn (vlib_main_t * vm,
160 unformat_input_t * input,
161 vlib_cli_command_t * cmd)
163 unformat_input_t _line_input, *line_input = &_line_input;
164 vnet_crypto_main_t *cm = &crypto_main;
166 char **args = 0, *s, **arg, *engine = 0;
168 clib_error_t *error = 0;
169 crypto_op_class_type_t oct = CRYPTO_OP_BOTH;
171 if (!unformat_user (input, unformat_line_input, line_input))
174 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
176 if (unformat (line_input, "all"))
178 else if (unformat (line_input, "simple"))
179 oct = CRYPTO_OP_SIMPLE;
180 else if (unformat (line_input, "chained"))
181 oct = CRYPTO_OP_CHAINED;
182 else if (unformat (line_input, "both"))
183 oct = CRYPTO_OP_BOTH;
184 else if (unformat (line_input, "%s", &s))
188 error = clib_error_return (0, "invalid params");
193 if ((vec_len (args) < 2 && !all) || (vec_len (args) == 0 && all))
195 error = clib_error_return (0, "missing cipher or engine!");
199 engine = vec_elt_at_index (args, vec_len (args) - 1)[0];
200 vec_del1 (args, vec_len (args) - 1);
207 hash_foreach_mem (key, value, cm->alg_index_by_name,
210 rc += vnet_crypto_set_handler2 (key, engine, oct);
214 vlib_cli_output (vm, "failed to set crypto engine!");
218 vec_foreach (arg, args)
220 rc = vnet_crypto_set_handler2 (arg[0], engine, oct);
223 vlib_cli_output (vm, "failed to set engine %s for %s!",
231 vec_foreach (arg, args) vec_free (arg[0]);
233 unformat_free (line_input);
237 VLIB_CLI_COMMAND (set_crypto_handler_command, static) =
239 .path = "set crypto handler",
240 .short_help = "set crypto handler cipher [cipher2 cipher3 ...] engine"
242 .function = set_crypto_handler_command_fn,
246 format_vnet_crypto_async_handlers (u8 * s, va_list * args)
248 vnet_crypto_async_alg_t alg = va_arg (*args, vnet_crypto_async_alg_t);
249 vnet_crypto_main_t *cm = &crypto_main;
250 vnet_crypto_async_alg_data_t *d = vec_elt_at_index (cm->async_algs, alg);
251 u32 indent = format_get_indent (s);
254 for (i = 0; i < VNET_CRYPTO_ASYNC_OP_N_TYPES; i++)
256 vnet_crypto_async_op_data_t *od;
257 vnet_crypto_async_op_id_t id = d->op_by_type[i];
262 od = cm->async_opt_data + id;
264 s = format (s, "\n%U", format_white_space, indent);
265 s = format (s, "%-16U", format_vnet_crypto_async_op_type, od->type);
267 s = format (s, "%U", format_vnet_crypto_engine_candidates, id,
268 od->active_engine_index_async, 0, 1);
274 static clib_error_t *
275 show_crypto_async_handlers_command_fn (vlib_main_t * vm,
276 unformat_input_t * input,
277 vlib_cli_command_t * cmd)
279 unformat_input_t _line_input, *line_input = &_line_input;
282 if (unformat_user (input, unformat_line_input, line_input))
283 unformat_free (line_input);
285 vlib_cli_output (vm, "%-28s%-16s%s", "Algo", "Type", "Handler");
287 for (i = 0; i < VNET_CRYPTO_N_ASYNC_ALGS; i++)
288 vlib_cli_output (vm, "%-28U%U", format_vnet_crypto_async_alg, i,
289 format_vnet_crypto_async_handlers, i);
294 VLIB_CLI_COMMAND (show_crypto_async_handlers_command, static) =
296 .path = "show crypto async handlers",
297 .short_help = "show crypto async handlers",
298 .function = show_crypto_async_handlers_command_fn,
302 static clib_error_t *
303 show_crypto_async_status_command_fn (vlib_main_t * vm,
304 unformat_input_t * input,
305 vlib_cli_command_t * cmd)
307 vnet_crypto_main_t *cm = &crypto_main;
308 vlib_thread_main_t *tm = vlib_get_thread_main ();
309 unformat_input_t _line_input, *line_input = &_line_input;
312 if (unformat_user (input, unformat_line_input, line_input))
313 unformat_free (line_input);
315 for (i = 0; i < tm->n_vlib_mains; i++)
317 vlib_node_state_t state = vlib_node_get_state (
318 vlib_get_main_by_index (i), cm->crypto_node_index);
319 if (state == VLIB_NODE_STATE_POLLING)
320 vlib_cli_output (vm, "threadId: %-6d POLLING", i);
321 if (state == VLIB_NODE_STATE_INTERRUPT)
322 vlib_cli_output (vm, "threadId: %-6d INTERRUPT", i);
323 if (state == VLIB_NODE_STATE_DISABLED)
324 vlib_cli_output (vm, "threadId: %-6d DISABLED", i);
329 VLIB_CLI_COMMAND (show_crypto_async_status_command, static) =
331 .path = "show crypto async status",
332 .short_help = "show crypto async status",
333 .function = show_crypto_async_status_command_fn,
336 static clib_error_t *
337 set_crypto_async_handler_command_fn (vlib_main_t * vm,
338 unformat_input_t * input,
339 vlib_cli_command_t * cmd)
341 unformat_input_t _line_input, *line_input = &_line_input;
342 vnet_crypto_main_t *cm = &crypto_main;
344 char **args = 0, *s, **arg, *engine = 0;
346 clib_error_t *error = 0;
348 if (!unformat_user (input, unformat_line_input, line_input))
351 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
353 if (unformat (line_input, "all"))
355 else if (unformat (line_input, "%s", &s))
359 error = clib_error_return (0, "invalid params");
364 if ((vec_len (args) < 2 && !all) || (vec_len (args) == 0 && all))
366 error = clib_error_return (0, "missing cipher or engine!");
370 engine = vec_elt_at_index (args, vec_len (args) - 1)[0];
371 vec_del1 (args, vec_len (args) - 1);
378 hash_foreach_mem (key, value, cm->async_alg_index_by_name,
381 rc += vnet_crypto_set_async_handler2 (key, engine);
385 vlib_cli_output (vm, "failed to set crypto engine!");
389 vec_foreach (arg, args)
391 rc = vnet_crypto_set_async_handler2 (arg[0], engine);
394 vlib_cli_output (vm, "failed to set engine %s for %s!",
402 vec_foreach (arg, args) vec_free (arg[0]);
404 unformat_free (line_input);
408 VLIB_CLI_COMMAND (set_crypto_async_handler_command, static) =
410 .path = "set crypto async handler",
411 .short_help = "set crypto async handler type [type2 type3 ...] engine",
412 .function = set_crypto_async_handler_command_fn,
415 static clib_error_t *
416 set_crypto_async_dispatch_command_fn (vlib_main_t *vm, unformat_input_t *input,
417 vlib_cli_command_t *cmd)
419 unformat_input_t _line_input, *line_input = &_line_input;
420 clib_error_t *error = 0;
422 u8 mode = VLIB_NODE_STATE_INTERRUPT;
424 if (!unformat_user (input, unformat_line_input, line_input))
427 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
429 if (unformat (line_input, "polling"))
430 mode = VLIB_NODE_STATE_POLLING;
431 else if (unformat (line_input, "interrupt"))
432 mode = VLIB_NODE_STATE_INTERRUPT;
433 else if (unformat (line_input, "adaptive"))
437 error = clib_error_return (0, "invalid params");
442 vnet_crypto_set_async_dispatch (mode, adaptive);
444 unformat_free (line_input);
448 VLIB_CLI_COMMAND (set_crypto_async_dispatch_mode_command, static) = {
449 .path = "set crypto async dispatch mode",
450 .short_help = "set crypto async dispatch mode <polling|interrupt|adaptive>",
451 .function = set_crypto_async_dispatch_command_fn,
455 * fd.io coding-style-patch-verification: ON
458 * eval: (c-set-style "gnu")