crypto: show crypto handlers re-design
[vpp.git] / src / vnet / crypto / cli.c
1 /*
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
20 static clib_error_t *
21 show_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* */
49 VLIB_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
56 static u8 *
57 format_vnet_crypto_engine_candidates (u8 * s, va_list * args)
58 {
59   vnet_crypto_engine_t *e;
60   vnet_crypto_main_t *cm = &crypto_main;
61
62   vnet_crypto_op_id_t id = va_arg (*args, vnet_crypto_op_id_t);
63   u32 ei = va_arg (*args, u32);
64   int is_chained = va_arg (*args, int);
65
66   vec_foreach (e, cm->engines)
67     {
68       void * h = is_chained ? (void *) e->chained_ops_handlers[id]
69         : (void *) e->ops_handlers[id];
70
71       if (h)
72         {
73           s = format (s, "%U", format_vnet_crypto_engine, e - cm->engines);
74           if (ei == e - cm->engines)
75             s = format (s, "%c ", '*');
76           else
77             s = format (s, " ");
78         }
79     }
80   return s;
81 }
82
83 static u8 *
84 format_vnet_crypto_handlers (u8 * s, va_list * args)
85 {
86   vnet_crypto_alg_t alg = va_arg (*args, vnet_crypto_alg_t);
87   vnet_crypto_main_t *cm = &crypto_main;
88   vnet_crypto_alg_data_t *d = vec_elt_at_index (cm->algs, alg);
89   u32 indent = format_get_indent (s);
90   int i, first = 1;
91
92   for (i = 0; i < VNET_CRYPTO_OP_N_TYPES; i++)
93     {
94       vnet_crypto_op_data_t *od;
95       vnet_crypto_op_id_t id = d->op_by_type[i];
96
97       if (id == 0)
98         continue;
99
100       od = cm->opt_data + id;
101       if (first == 0)
102         s = format (s, "\n%U", format_white_space, indent);
103       s = format (s, "%-16U", format_vnet_crypto_op_type, od->type);
104
105       s = format (s, "%-28U", format_vnet_crypto_engine_candidates, id,
106           od->active_engine_index_simple, 0);
107       s = format (s, "%U", format_vnet_crypto_engine_candidates, id,
108           od->active_engine_index_chained, 1);
109       first = 0;
110     }
111   return s;
112 }
113
114
115 static clib_error_t *
116 show_crypto_handlers_command_fn (vlib_main_t * vm,
117                         unformat_input_t * input, vlib_cli_command_t * cmd)
118 {
119   unformat_input_t _line_input, *line_input = &_line_input;
120   int i;
121
122   if (unformat_user (input, unformat_line_input, line_input))
123     unformat_free (line_input);
124
125   vlib_cli_output (vm, "%-16s%-16s%-28s%s", "Algo", "Type", "Simple",
126       "Chained");
127
128   for (i = 0; i < VNET_CRYPTO_N_ALGS; i++)
129     vlib_cli_output (vm, "%-16U%U", format_vnet_crypto_alg, i,
130                      format_vnet_crypto_handlers, i);
131
132   return 0;
133 }
134
135 /* *INDENT-OFF* */
136 VLIB_CLI_COMMAND (show_crypto_handlers_command, static) =
137 {
138   .path = "show crypto handlers",
139   .short_help = "show crypto handlers",
140   .function = show_crypto_handlers_command_fn,
141 };
142 /* *INDENT-ON* */
143
144 static clib_error_t *
145 set_crypto_handler_command_fn (vlib_main_t * vm,
146                                unformat_input_t * input,
147                                vlib_cli_command_t * cmd)
148 {
149   unformat_input_t _line_input, *line_input = &_line_input;
150   vnet_crypto_main_t *cm = &crypto_main;
151   int rc = 0;
152   char **args = 0, *s, **arg, *engine = 0;
153   int all = 0;
154   clib_error_t *error = 0;
155   crypto_op_class_type_t oct = CRYPTO_OP_BOTH;
156
157   if (!unformat_user (input, unformat_line_input, line_input))
158     return 0;
159
160   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
161     {
162       if (unformat (line_input, "all"))
163         all = 1;
164       else if (unformat (line_input, "simple"))
165         oct = CRYPTO_OP_SIMPLE;
166       else if (unformat (line_input, "chained"))
167         oct = CRYPTO_OP_CHAINED;
168       else if (unformat (line_input, "both"))
169         oct = CRYPTO_OP_BOTH;
170       else if (unformat (line_input, "%s", &s))
171         vec_add1 (args, s);
172       else
173         {
174           error = clib_error_return (0, "invalid params");
175           goto done;
176         }
177     }
178
179   if ((vec_len (args) < 2 && !all) || (vec_len (args) == 0 && all))
180     {
181       error = clib_error_return (0, "missing cipher or engine!");
182       goto done;
183     }
184
185   engine = vec_elt_at_index (args, vec_len (args) - 1)[0];
186   vec_del1 (args, vec_len (args) - 1);
187
188   if (all)
189     {
190       char *key;
191       u8 *value;
192
193       /* *INDENT-OFF* */
194       hash_foreach_mem (key, value, cm->alg_index_by_name,
195       ({
196         (void) value;
197         rc += vnet_crypto_set_handler2 (key, engine, oct);
198       }));
199       /* *INDENT-ON* */
200
201       if (rc)
202         vlib_cli_output (vm, "failed to set crypto engine!");
203     }
204   else
205     {
206       vec_foreach (arg, args)
207       {
208         rc = vnet_crypto_set_handler2 (arg[0], engine, oct);
209         if (rc)
210           {
211             vlib_cli_output (vm, "failed to set engine %s for %s!",
212                              engine, arg[0]);
213           }
214       }
215     }
216
217 done:
218   vec_free (engine);
219   vec_foreach (arg, args) vec_free (arg[0]);
220   vec_free (args);
221   unformat_free (line_input);
222   return error;
223 }
224
225 /* *INDENT-OFF* */
226 VLIB_CLI_COMMAND (set_crypto_handler_command, static) =
227 {
228   .path = "set crypto handler",
229   .short_help = "set crypto handler cipher [cipher2 cipher3 ...] engine"
230     " [simple|chained]",
231   .function = set_crypto_handler_command_fn,
232 };
233 /* *INDENT-ON* */
234
235 /*
236  * fd.io coding-style-patch-verification: ON
237  *
238  * Local Variables:
239  * eval: (c-set-style "gnu")
240  * End:
241  */