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");
40 vec_foreach (p, cm->engines)
42 vlib_cli_output (vm, "%-20s%-8u%s", p->name, p->priority, p->desc);
49 VLIB_CLI_COMMAND (show_crypto_engines_command, static) =
51 .path = "show crypto engines",
52 .short_help = "show crypto engines",
53 .function = show_crypto_engines_command_fn,
57 format_vnet_crypto_engine_candidates (u8 * s, va_list * args)
59 vnet_crypto_engine_t *e;
60 vnet_crypto_main_t *cm = &crypto_main;
61 u32 id = va_arg (*args, u32);
62 u32 ei = va_arg (*args, u32);
63 int is_chained = va_arg (*args, int);
64 int is_async = va_arg (*args, int);
68 vec_foreach (e, cm->engines)
70 if (e->enqueue_handlers[id] && e->dequeue_handler)
72 s = format (s, "%U", format_vnet_crypto_engine, e - cm->engines);
73 if (ei == e - cm->engines)
74 s = format (s, "%c ", '*');
84 vec_foreach (e, cm->engines)
86 void * h = is_chained ? (void *) e->chained_ops_handlers[id]
87 : (void *) e->ops_handlers[id];
91 s = format (s, "%U", format_vnet_crypto_engine, e - cm->engines);
92 if (ei == e - cm->engines)
93 s = format (s, "%c ", '*');
103 format_vnet_crypto_handlers (u8 * s, va_list * args)
105 vnet_crypto_alg_t alg = va_arg (*args, vnet_crypto_alg_t);
106 vnet_crypto_main_t *cm = &crypto_main;
107 vnet_crypto_alg_data_t *d = vec_elt_at_index (cm->algs, alg);
108 u32 indent = format_get_indent (s);
111 for (i = 0; i < VNET_CRYPTO_OP_N_TYPES; i++)
113 vnet_crypto_op_data_t *od;
114 vnet_crypto_op_id_t id = d->op_by_type[i];
119 od = cm->opt_data + id;
121 s = format (s, "\n%U", format_white_space, indent);
122 s = format (s, "%-16U", format_vnet_crypto_op_type, od->type);
124 s = format (s, "%-28U", format_vnet_crypto_engine_candidates, id,
125 od->active_engine_index_simple, 0, 0);
126 s = format (s, "%U", format_vnet_crypto_engine_candidates, id,
127 od->active_engine_index_chained, 1, 0);
134 static clib_error_t *
135 show_crypto_handlers_command_fn (vlib_main_t * vm,
136 unformat_input_t * input, vlib_cli_command_t * cmd)
138 unformat_input_t _line_input, *line_input = &_line_input;
141 if (unformat_user (input, unformat_line_input, line_input))
142 unformat_free (line_input);
144 vlib_cli_output (vm, "%-16s%-16s%-28s%s", "Algo", "Type", "Simple",
147 for (i = 0; i < VNET_CRYPTO_N_ALGS; i++)
148 vlib_cli_output (vm, "%-16U%U", format_vnet_crypto_alg, i,
149 format_vnet_crypto_handlers, i);
155 VLIB_CLI_COMMAND (show_crypto_handlers_command, static) =
157 .path = "show crypto handlers",
158 .short_help = "show crypto handlers",
159 .function = show_crypto_handlers_command_fn,
163 static clib_error_t *
164 set_crypto_handler_command_fn (vlib_main_t * vm,
165 unformat_input_t * input,
166 vlib_cli_command_t * cmd)
168 unformat_input_t _line_input, *line_input = &_line_input;
169 vnet_crypto_main_t *cm = &crypto_main;
171 char **args = 0, *s, **arg, *engine = 0;
173 clib_error_t *error = 0;
174 crypto_op_class_type_t oct = CRYPTO_OP_BOTH;
176 if (!unformat_user (input, unformat_line_input, line_input))
179 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
181 if (unformat (line_input, "all"))
183 else if (unformat (line_input, "simple"))
184 oct = CRYPTO_OP_SIMPLE;
185 else if (unformat (line_input, "chained"))
186 oct = CRYPTO_OP_CHAINED;
187 else if (unformat (line_input, "both"))
188 oct = CRYPTO_OP_BOTH;
189 else if (unformat (line_input, "%s", &s))
193 error = clib_error_return (0, "invalid params");
198 if ((vec_len (args) < 2 && !all) || (vec_len (args) == 0 && all))
200 error = clib_error_return (0, "missing cipher or engine!");
204 engine = vec_elt_at_index (args, vec_len (args) - 1)[0];
205 vec_del1 (args, vec_len (args) - 1);
213 hash_foreach_mem (key, value, cm->alg_index_by_name,
216 rc += vnet_crypto_set_handler2 (key, engine, oct);
221 vlib_cli_output (vm, "failed to set crypto engine!");
225 vec_foreach (arg, args)
227 rc = vnet_crypto_set_handler2 (arg[0], engine, oct);
230 vlib_cli_output (vm, "failed to set engine %s for %s!",
238 vec_foreach (arg, args) vec_free (arg[0]);
240 unformat_free (line_input);
245 VLIB_CLI_COMMAND (set_crypto_handler_command, static) =
247 .path = "set crypto handler",
248 .short_help = "set crypto handler cipher [cipher2 cipher3 ...] engine"
250 .function = set_crypto_handler_command_fn,
255 format_vnet_crypto_async_handlers (u8 * s, va_list * args)
257 vnet_crypto_async_alg_t alg = va_arg (*args, vnet_crypto_async_alg_t);
258 vnet_crypto_main_t *cm = &crypto_main;
259 vnet_crypto_async_alg_data_t *d = vec_elt_at_index (cm->async_algs, alg);
260 u32 indent = format_get_indent (s);
263 for (i = 0; i < VNET_CRYPTO_ASYNC_OP_N_TYPES; i++)
265 vnet_crypto_async_op_data_t *od;
266 vnet_crypto_async_op_id_t id = d->op_by_type[i];
271 od = cm->async_opt_data + id;
273 s = format (s, "\n%U", format_white_space, indent);
274 s = format (s, "%-16U", format_vnet_crypto_async_op_type, od->type);
276 s = format (s, "%U", format_vnet_crypto_engine_candidates, id,
277 od->active_engine_index_async, 0, 1);
283 static clib_error_t *
284 show_crypto_async_handlers_command_fn (vlib_main_t * vm,
285 unformat_input_t * input,
286 vlib_cli_command_t * cmd)
288 unformat_input_t _line_input, *line_input = &_line_input;
291 if (unformat_user (input, unformat_line_input, line_input))
292 unformat_free (line_input);
294 vlib_cli_output (vm, "%-28s%-16s%s", "Algo", "Type", "Handler");
296 for (i = 0; i < VNET_CRYPTO_N_ASYNC_ALGS; i++)
297 vlib_cli_output (vm, "%-28U%U", format_vnet_crypto_async_alg, i,
298 format_vnet_crypto_async_handlers, i);
304 VLIB_CLI_COMMAND (show_crypto_async_handlers_command, static) =
306 .path = "show crypto async handlers",
307 .short_help = "show crypto async handlers",
308 .function = show_crypto_async_handlers_command_fn,
313 static clib_error_t *
314 show_crypto_async_status_command_fn (vlib_main_t * vm,
315 unformat_input_t * input,
316 vlib_cli_command_t * cmd)
318 vnet_crypto_main_t *cm = &crypto_main;
319 u32 skip_master = vlib_num_workers () > 0;
320 vlib_thread_main_t *tm = vlib_get_thread_main ();
321 unformat_input_t _line_input, *line_input = &_line_input;
324 if (unformat_user (input, unformat_line_input, line_input))
325 unformat_free (line_input);
327 vlib_cli_output (vm, "Crypto async dispatch mode: %s",
329 VNET_CRYPTO_ASYNC_DISPATCH_POLLING ? "POLLING" :
332 for (i = skip_master; i < tm->n_vlib_mains; i++)
334 vlib_node_state_t state = vlib_node_get_state (
335 vlib_get_main_by_index (i), cm->crypto_node_index);
336 if (state == VLIB_NODE_STATE_POLLING)
337 vlib_cli_output (vm, "threadId: %-6d POLLING", i);
338 if (state == VLIB_NODE_STATE_INTERRUPT)
339 vlib_cli_output (vm, "threadId: %-6d INTERRUPT", i);
340 if (state == VLIB_NODE_STATE_DISABLED)
341 vlib_cli_output (vm, "threadId: %-6d DISABLED", i);
347 VLIB_CLI_COMMAND (show_crypto_async_status_command, static) =
349 .path = "show crypto async status",
350 .short_help = "show crypto async status",
351 .function = show_crypto_async_status_command_fn,
355 static clib_error_t *
356 set_crypto_async_handler_command_fn (vlib_main_t * vm,
357 unformat_input_t * input,
358 vlib_cli_command_t * cmd)
360 unformat_input_t _line_input, *line_input = &_line_input;
361 vnet_crypto_main_t *cm = &crypto_main;
363 char **args = 0, *s, **arg, *engine = 0;
365 clib_error_t *error = 0;
367 if (!unformat_user (input, unformat_line_input, line_input))
370 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
372 if (unformat (line_input, "all"))
374 else if (unformat (line_input, "%s", &s))
378 error = clib_error_return (0, "invalid params");
383 if ((vec_len (args) < 2 && !all) || (vec_len (args) == 0 && all))
385 error = clib_error_return (0, "missing cipher or engine!");
389 engine = vec_elt_at_index (args, vec_len (args) - 1)[0];
390 vec_del1 (args, vec_len (args) - 1);
398 hash_foreach_mem (key, value, cm->async_alg_index_by_name,
401 rc += vnet_crypto_set_async_handler2 (key, engine);
406 vlib_cli_output (vm, "failed to set crypto engine!");
410 vec_foreach (arg, args)
412 rc = vnet_crypto_set_async_handler2 (arg[0], engine);
415 vlib_cli_output (vm, "failed to set engine %s for %s!",
423 vec_foreach (arg, args) vec_free (arg[0]);
425 unformat_free (line_input);
430 VLIB_CLI_COMMAND (set_crypto_async_handler_command, static) =
432 .path = "set crypto async handler",
433 .short_help = "set crypto async handler type [type2 type3 ...] engine",
434 .function = set_crypto_async_handler_command_fn,
439 print_crypto_async_dispatch_warning ()
441 clib_warning ("Switching dispatch mode might not work is some situations.");
443 ("Use 'show crypto async status' to verify that the nodes' states were set");
444 clib_warning ("and if not, set 'crypto async dispatch' mode again.");
447 static clib_error_t *
448 set_crypto_async_dispatch_polling_command_fn (vlib_main_t * vm,
449 unformat_input_t * input,
450 vlib_cli_command_t * cmd)
452 print_crypto_async_dispatch_warning ();
453 vnet_crypto_set_async_dispatch_mode (VNET_CRYPTO_ASYNC_DISPATCH_POLLING);
457 static clib_error_t *
458 set_crypto_async_dispatch_interrupt_command_fn (vlib_main_t * vm,
459 unformat_input_t * input,
460 vlib_cli_command_t * cmd)
462 print_crypto_async_dispatch_warning ();
463 vnet_crypto_set_async_dispatch_mode (VNET_CRYPTO_ASYNC_DISPATCH_INTERRUPT);
468 VLIB_CLI_COMMAND (set_crypto_async_dispatch_polling_command, static) =
470 .path = "set crypto async dispatch polling",
471 .short_help = "set crypto async dispatch polling|interrupt",
472 .function = set_crypto_async_dispatch_polling_command_fn,
474 VLIB_CLI_COMMAND (set_crypto_async_dispatch_interrupt_command, static) =
476 .path = "set crypto async dispatch interrupt",
477 .short_help = "set crypto async dispatch polling|interrupt",
478 .function = set_crypto_async_dispatch_interrupt_command_fn,
481 * fd.io coding-style-patch-verification: ON
484 * eval: (c-set-style "gnu")