blob: ef6371ad3dc9c0f65f0571a1700e18cce48f52c9 [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
Damjan Marion060bfb92019-03-29 13:47:54 +010056static u8 *
57format_vnet_crypto_handlers (u8 * s, va_list * args)
58{
59 vnet_crypto_alg_t alg = va_arg (*args, vnet_crypto_alg_t);
60 vnet_crypto_main_t *cm = &crypto_main;
61 vnet_crypto_alg_data_t *d = vec_elt_at_index (cm->algs, alg);
62 u32 indent = format_get_indent (s);
63 int i, first = 1;
64
65 for (i = 0; i < VNET_CRYPTO_OP_N_TYPES; i++)
66 {
67 vnet_crypto_op_data_t *od;
68 vnet_crypto_engine_t *e;
69 vnet_crypto_op_id_t id = d->op_by_type[i];
70
71 if (id == 0)
72 continue;
73
Lijian Zhangb15d7962019-09-27 16:25:35 +080074 od = cm->opt_data + id;
Damjan Marion060bfb92019-03-29 13:47:54 +010075 if (first == 0)
76 s = format (s, "\n%U", format_white_space, indent);
Filip Tehlarefcad1a2020-02-04 09:36:04 +000077 s = format (s, "%-22U%-20U", format_vnet_crypto_op_type, od->type, 0,
78 format_vnet_crypto_engine, od->active_engine_index_simple,s);
Damjan Marion060bfb92019-03-29 13:47:54 +010079
80 vec_foreach (e, cm->engines)
81 {
82 if (e->ops_handlers[id] != 0)
83 s = format (s, "%U ", format_vnet_crypto_engine, e - cm->engines);
84 }
Filip Tehlarefcad1a2020-02-04 09:36:04 +000085
86 s = format (s, "\n%U", format_white_space, indent);
87 s = format (s, "%-22U%-20U", format_vnet_crypto_op_type, od->type, 1,
88 format_vnet_crypto_engine,
89 od->active_engine_index_chained);
90 vec_foreach (e, cm->engines)
91 {
92 if (e->chained_ops_handlers[id] != 0)
93 s = format (s, "%U ", format_vnet_crypto_engine, e - cm->engines);
94 }
Damjan Marion060bfb92019-03-29 13:47:54 +010095 first = 0;
96 }
97 return s;
98}
99
100
Damjan Marion91f17dc2019-03-18 18:59:25 +0100101static clib_error_t *
102show_crypto_handlers_command_fn (vlib_main_t * vm,
103 unformat_input_t * input, vlib_cli_command_t * cmd)
104{
Damjan Marion91f17dc2019-03-18 18:59:25 +0100105 unformat_input_t _line_input, *line_input = &_line_input;
Damjan Marion060bfb92019-03-29 13:47:54 +0100106 int i;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100107
108 if (unformat_user (input, unformat_line_input, line_input))
109 unformat_free (line_input);
110
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000111 vlib_cli_output (vm, "%-20s%-22s%-20s%s", "Algo", "Type", "Active",
Damjan Marion060bfb92019-03-29 13:47:54 +0100112 "Candidates");
Damjan Marion91f17dc2019-03-18 18:59:25 +0100113
Damjan Marion060bfb92019-03-29 13:47:54 +0100114 for (i = 0; i < VNET_CRYPTO_N_ALGS; i++)
115 vlib_cli_output (vm, "%-20U%U", format_vnet_crypto_alg, i,
116 format_vnet_crypto_handlers, i);
117
Damjan Marion91f17dc2019-03-18 18:59:25 +0100118 return 0;
119}
120
121/* *INDENT-OFF* */
122VLIB_CLI_COMMAND (show_crypto_handlers_command, static) =
123{
124 .path = "show crypto handlers",
125 .short_help = "show crypto handlers",
126 .function = show_crypto_handlers_command_fn,
127};
128/* *INDENT-ON* */
129
Filip Tehlar1469d542019-03-25 09:04:41 -0700130static clib_error_t *
131set_crypto_handler_command_fn (vlib_main_t * vm,
132 unformat_input_t * input,
133 vlib_cli_command_t * cmd)
134{
135 unformat_input_t _line_input, *line_input = &_line_input;
136 vnet_crypto_main_t *cm = &crypto_main;
137 int rc = 0;
138 char **args = 0, *s, **arg, *engine = 0;
139 int all = 0;
140 clib_error_t *error = 0;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000141 crypto_op_class_type_t oct = CRYPTO_OP_BOTH;
Filip Tehlar1469d542019-03-25 09:04:41 -0700142
143 if (!unformat_user (input, unformat_line_input, line_input))
144 return 0;
145
146 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
147 {
148 if (unformat (line_input, "all"))
149 all = 1;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000150 else if (unformat (line_input, "simple"))
151 oct = CRYPTO_OP_SIMPLE;
152 else if (unformat (line_input, "chained"))
153 oct = CRYPTO_OP_CHAINED;
154 else if (unformat (line_input, "both"))
155 oct = CRYPTO_OP_BOTH;
Filip Tehlar1469d542019-03-25 09:04:41 -0700156 else if (unformat (line_input, "%s", &s))
157 vec_add1 (args, s);
158 else
159 {
160 error = clib_error_return (0, "invalid params");
161 goto done;
162 }
163 }
164
165 if ((vec_len (args) < 2 && !all) || (vec_len (args) == 0 && all))
166 {
167 error = clib_error_return (0, "missing cipher or engine!");
168 goto done;
169 }
170
171 engine = vec_elt_at_index (args, vec_len (args) - 1)[0];
172 vec_del1 (args, vec_len (args) - 1);
173
174 if (all)
175 {
176 char *key;
177 u8 *value;
178
179 /* *INDENT-OFF* */
Damjan Marion060bfb92019-03-29 13:47:54 +0100180 hash_foreach_mem (key, value, cm->alg_index_by_name,
Filip Tehlar1469d542019-03-25 09:04:41 -0700181 ({
182 (void) value;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000183 rc += vnet_crypto_set_handler2 (key, engine, oct);
Filip Tehlar1469d542019-03-25 09:04:41 -0700184 }));
185 /* *INDENT-ON* */
186
187 if (rc)
188 vlib_cli_output (vm, "failed to set crypto engine!");
189 }
190 else
191 {
192 vec_foreach (arg, args)
193 {
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000194 rc = vnet_crypto_set_handler2 (arg[0], engine, oct);
Filip Tehlar1469d542019-03-25 09:04:41 -0700195 if (rc)
196 {
197 vlib_cli_output (vm, "failed to set engine %s for %s!",
198 engine, arg[0]);
199 }
200 }
201 }
202
203done:
204 vec_free (engine);
205 vec_foreach (arg, args) vec_free (arg[0]);
206 vec_free (args);
207 unformat_free (line_input);
208 return error;
209}
210
211/* *INDENT-OFF* */
212VLIB_CLI_COMMAND (set_crypto_handler_command, static) =
213{
214 .path = "set crypto handler",
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000215 .short_help = "set crypto handler cipher [cipher2 cipher3 ...] engine"
216 " [simple|chained]",
Filip Tehlar1469d542019-03-25 09:04:41 -0700217 .function = set_crypto_handler_command_fn,
218};
219/* *INDENT-ON* */
220
Damjan Marion91f17dc2019-03-18 18:59:25 +0100221/*
222 * fd.io coding-style-patch-verification: ON
223 *
224 * Local Variables:
225 * eval: (c-set-style "gnu")
226 * End:
227 */