FD.io VPP  v19.04.4-rc0-5-ge88582fac
Vector Packet Processing
cli.c
Go to the documentation of this file.
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 *
22  unformat_input_t * input,
23  vlib_cli_command_t * cmd)
24 {
25  unformat_input_t _line_input, *line_input = &_line_input;
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",
54 };
55 
56 static u8 *
57 format_vnet_crypto_handlers (u8 * s, va_list * args)
58 {
59  vnet_crypto_alg_t alg = va_arg (*args, vnet_crypto_alg_t);
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  {
70 
71  if (id == 0)
72  continue;
73 
74  od = vec_elt_at_index (cm->opt_data, id);
75  if (first == 0)
76  s = format (s, "\n%U", format_white_space, indent);
77  s = format (s, "%-20U%-20U", format_vnet_crypto_op_type, od->type,
79 
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  }
85  first = 0;
86  }
87  return s;
88 }
89 
90 
91 static clib_error_t *
93  unformat_input_t * input, vlib_cli_command_t * cmd)
94 {
95  unformat_input_t _line_input, *line_input = &_line_input;
96  int i;
97 
98  if (unformat_user (input, unformat_line_input, line_input))
99  unformat_free (line_input);
100 
101  vlib_cli_output (vm, "%-20s%-20s%-20s%s", "Algo", "Type", "Active",
102  "Candidates");
103 
104  for (i = 0; i < VNET_CRYPTO_N_ALGS; i++)
105  vlib_cli_output (vm, "%-20U%U", format_vnet_crypto_alg, i,
107 
108  return 0;
109 }
110 
111 /* *INDENT-OFF* */
112 VLIB_CLI_COMMAND (show_crypto_handlers_command, static) =
113 {
114  .path = "show crypto handlers",
115  .short_help = "show crypto handlers",
117 };
118 /* *INDENT-ON* */
119 
120 static clib_error_t *
122  unformat_input_t * input,
123  vlib_cli_command_t * cmd)
124 {
125  unformat_input_t _line_input, *line_input = &_line_input;
127  int rc = 0;
128  char **args = 0, *s, **arg, *engine = 0;
129  int all = 0;
130  clib_error_t *error = 0;
131 
132  if (!unformat_user (input, unformat_line_input, line_input))
133  return 0;
134 
135  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
136  {
137  if (unformat (line_input, "all"))
138  all = 1;
139  else if (unformat (line_input, "%s", &s))
140  vec_add1 (args, s);
141  else
142  {
143  error = clib_error_return (0, "invalid params");
144  goto done;
145  }
146  }
147 
148  if ((vec_len (args) < 2 && !all) || (vec_len (args) == 0 && all))
149  {
150  error = clib_error_return (0, "missing cipher or engine!");
151  goto done;
152  }
153 
154  engine = vec_elt_at_index (args, vec_len (args) - 1)[0];
155  vec_del1 (args, vec_len (args) - 1);
156 
157  if (all)
158  {
159  char *key;
160  u8 *value;
161 
162  /* *INDENT-OFF* */
163  hash_foreach_mem (key, value, cm->alg_index_by_name,
164  ({
165  (void) value;
166  rc += vnet_crypto_set_handler (key, engine);
167  }));
168  /* *INDENT-ON* */
169 
170  if (rc)
171  vlib_cli_output (vm, "failed to set crypto engine!");
172  }
173  else
174  {
175  vec_foreach (arg, args)
176  {
177  rc = vnet_crypto_set_handler (arg[0], engine);
178  if (rc)
179  {
180  vlib_cli_output (vm, "failed to set engine %s for %s!",
181  engine, arg[0]);
182  }
183  }
184  }
185 
186 done:
187  vec_free (engine);
188  vec_foreach (arg, args) vec_free (arg[0]);
189  vec_free (args);
190  unformat_free (line_input);
191  return error;
192 }
193 
194 /* *INDENT-OFF* */
195 VLIB_CLI_COMMAND (set_crypto_handler_command, static) =
196 {
197  .path = "set crypto handler",
198  .short_help = "set crypto handler cipher [cipher2 cipher3 ...] engine",
199  .function = set_crypto_handler_command_fn,
200 };
201 /* *INDENT-ON* */
202 
203 /*
204  * fd.io coding-style-patch-verification: ON
205  *
206  * Local Variables:
207  * eval: (c-set-style "gnu")
208  * End:
209  */
format_function_t format_vnet_crypto_op_type
Definition: crypto.h:186
vnet_crypto_engine_t * engines
Definition: crypto.h:167
uword * alg_index_by_name
Definition: crypto.h:169
vnet_crypto_op_data_t opt_data[VNET_CRYPTO_N_OP_IDS]
Definition: crypto.h:166
vnet_crypto_alg_data_t * algs
Definition: crypto.h:163
static clib_error_t * set_crypto_handler_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: cli.c:121
vnet_crypto_op_id_t op_by_type[VNET_CRYPTO_OP_N_TYPES]
Definition: crypto.h:104
format_function_t format_vnet_crypto_alg
Definition: crypto.h:183
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:522
int i
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:983
static u32 format_get_indent(u8 *s)
Definition: format.h:72
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
unsigned char u8
Definition: types.h:56
u8 * format_white_space(u8 *s, va_list *va)
Definition: std-formats.c:113
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
#define clib_error_return(e, args...)
Definition: error.h:99
unsigned int u32
Definition: types.h:88
vnet_crypto_alg_t
Definition: crypto.h:75
unformat_function_t unformat_line_input
Definition: format.h:282
static heap_elt_t * first(heap_header_t *h)
Definition: heap.c:59
int vnet_crypto_set_handler(char *alg_name, char *engine)
Definition: crypto.c:95
struct _unformat_input_t unformat_input_t
#define vec_del1(v, i)
Delete the element at index I.
Definition: vec.h:804
#define hash_foreach_mem(key_var, value_var, h, body)
Definition: hash.h:461
#define UNFORMAT_END_OF_INPUT
Definition: format.h:144
vlib_main_t * vm
Definition: buffer.c:312
static clib_error_t * show_crypto_handlers_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: cli.c:92
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:341
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:155
static clib_error_t * show_crypto_engines_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: cli.c:21
static u8 * format_vnet_crypto_handlers(u8 *s, va_list *args)
Definition: cli.c:57
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
format_function_t format_vnet_crypto_engine
Definition: crypto.h:184
static void unformat_free(unformat_input_t *i)
Definition: format.h:162
typedef key
Definition: ipsec.api:244
vnet_crypto_op_type_t type
Definition: crypto.h:130
vnet_crypto_ops_handler_t * ops_handlers[VNET_CRYPTO_N_OP_IDS]
Definition: crypto.h:158
vnet_crypto_op_id_t
Definition: crypto.h:87
#define vec_foreach(var, vec)
Vector iterator.
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:762
vnet_crypto_main_t crypto_main
Definition: crypto.c:20
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:972
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:170