blob: f1afa3746f0dcc74ede1ad68fab09f5bdc8d2357 [file] [log] [blame]
Damjan Marion22311502016-10-28 20:30:15 +02001/*
2 * Copyright (c) 2016 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 <vnet/feature/feature.h>
Neale Rannsb069a692017-03-15 12:34:25 -040017#include <vnet/adj/adj.h>
Damjan Marion22311502016-10-28 20:30:15 +020018
19vnet_feature_main_t feature_main;
20
21static clib_error_t *
22vnet_feature_init (vlib_main_t * vm)
23{
24 vnet_feature_main_t *fm = &feature_main;
25 vnet_feature_registration_t *freg;
26 vnet_feature_arc_registration_t *areg;
Dave Barach2dd192b2018-11-19 09:31:48 -050027 vnet_feature_constraint_registration_t *creg;
Damjan Marion22311502016-10-28 20:30:15 +020028 u32 arc_index = 0;
29
30 fm->arc_index_by_name = hash_create_string (0, sizeof (uword));
31 areg = fm->next_arc;
32
33 /* process feature arc registrations */
34 while (areg)
35 {
36 char *s;
37 int i = 0;
38 areg->feature_arc_index = arc_index;
Damjan Marion8b3191e2016-11-09 19:54:20 +010039 if (areg->arc_index_ptr)
40 *areg->arc_index_ptr = arc_index;
Damjan Marion22311502016-10-28 20:30:15 +020041 hash_set_mem (fm->arc_index_by_name, areg->arc_name,
42 pointer_to_uword (areg));
43
44 /* process start nodes */
45 while ((s = areg->start_nodes[i]))
46 {
Damjan Marion22311502016-10-28 20:30:15 +020047 i++;
48 }
49 areg->n_start_nodes = i;
50
51 /* next */
52 areg = areg->next;
53 arc_index++;
54 }
55
56 vec_validate (fm->next_feature_by_arc, arc_index - 1);
57 vec_validate (fm->feature_nodes, arc_index - 1);
58 vec_validate (fm->feature_config_mains, arc_index - 1);
59 vec_validate (fm->next_feature_by_name, arc_index - 1);
60 vec_validate (fm->sw_if_index_has_features, arc_index - 1);
61 vec_validate (fm->feature_count_by_sw_if_index, arc_index - 1);
Dave Barach2dd192b2018-11-19 09:31:48 -050062 vec_validate (fm->next_constraint_by_arc, arc_index - 1);
Damjan Marion22311502016-10-28 20:30:15 +020063
64 freg = fm->next_feature;
65 while (freg)
66 {
Damjan Marion96b41f72016-11-10 18:01:42 +010067 vnet_feature_registration_t *next;
Damjan Marion22311502016-10-28 20:30:15 +020068 uword *p = hash_get_mem (fm->arc_index_by_name, freg->arc_name);
69 if (p == 0)
Florin Coras3d2a9142017-08-16 21:23:44 -070070 {
71 /* Don't start vpp with broken features arcs */
72 clib_warning ("Unknown feature arc '%s'", freg->arc_name);
73 os_exit (1);
74 }
Damjan Marion22311502016-10-28 20:30:15 +020075
76 areg = uword_to_pointer (p[0], vnet_feature_arc_registration_t *);
77 arc_index = areg->feature_arc_index;
78
Damjan Marion96b41f72016-11-10 18:01:42 +010079 next = freg->next;
Damjan Marion13adc3d2018-04-09 20:59:53 +020080 freg->next_in_arc = fm->next_feature_by_arc[arc_index];
Damjan Marion96b41f72016-11-10 18:01:42 +010081 fm->next_feature_by_arc[arc_index] = freg;
Damjan Marion22311502016-10-28 20:30:15 +020082
83 /* next */
Damjan Marion96b41f72016-11-10 18:01:42 +010084 freg = next;
Damjan Marion22311502016-10-28 20:30:15 +020085 }
86
Dave Barach2dd192b2018-11-19 09:31:48 -050087 /* Move bulk constraints to the constraint by arc lists */
88 creg = fm->next_constraint;
89 while (creg)
90 {
91 vnet_feature_constraint_registration_t *next;
92 uword *p = hash_get_mem (fm->arc_index_by_name, creg->arc_name);
93 if (p == 0)
94 {
95 /* Don't start vpp with broken features arcs */
96 clib_warning ("Unknown feature arc '%s'", creg->arc_name);
97 os_exit (1);
98 }
99
100 areg = uword_to_pointer (p[0], vnet_feature_arc_registration_t *);
101 arc_index = areg->feature_arc_index;
102
103 next = creg->next;
104 creg->next_in_arc = fm->next_constraint_by_arc[arc_index];
105 fm->next_constraint_by_arc[arc_index] = creg;
106
107 /* next */
108 creg = next;
109 }
110
111
Damjan Marion96b41f72016-11-10 18:01:42 +0100112 areg = fm->next_arc;
Damjan Marion22311502016-10-28 20:30:15 +0200113 while (areg)
114 {
115 clib_error_t *error;
116 vnet_feature_config_main_t *cm;
117 vnet_config_main_t *vcm;
Dave Baracha25def72018-11-26 11:04:45 -0500118 char **features_in_order, *last_feature;
Damjan Marion22311502016-10-28 20:30:15 +0200119
120 arc_index = areg->feature_arc_index;
121 cm = &fm->feature_config_mains[arc_index];
122 vcm = &cm->config_main;
Dave Barach2dd192b2018-11-19 09:31:48 -0500123 if ((error = vnet_feature_arc_init
124 (vm, vcm, areg->start_nodes, areg->n_start_nodes,
125 fm->next_feature_by_arc[arc_index],
126 fm->next_constraint_by_arc[arc_index],
127 &fm->feature_nodes[arc_index])))
Damjan Marion22311502016-10-28 20:30:15 +0200128 {
Florin Coras3d2a9142017-08-16 21:23:44 -0700129 clib_error_report (error);
130 os_exit (1);
Damjan Marion22311502016-10-28 20:30:15 +0200131 }
132
Dave Baracha25def72018-11-26 11:04:45 -0500133 features_in_order = fm->feature_nodes[arc_index];
134
135 /* If specificed, verify that the last node in the arc is actually last */
136 if (areg->last_in_arc && vec_len (features_in_order) > 0)
137 {
138 last_feature = features_in_order[vec_len (features_in_order) - 1];
139 if (strncmp (areg->last_in_arc, last_feature,
140 strlen (areg->last_in_arc)))
141 clib_warning
142 ("WARNING: %s arc: last node is %s, but expected %s!",
143 areg->arc_name, last_feature, areg->last_in_arc);
144 }
145
Damjan Marion22311502016-10-28 20:30:15 +0200146 fm->next_feature_by_name[arc_index] =
147 hash_create_string (0, sizeof (uword));
148 freg = fm->next_feature_by_arc[arc_index];
149
150 while (freg)
151 {
152 hash_set_mem (fm->next_feature_by_name[arc_index],
153 freg->node_name, pointer_to_uword (freg));
Damjan Marion13adc3d2018-04-09 20:59:53 +0200154 freg = freg->next_in_arc;
Damjan Marion22311502016-10-28 20:30:15 +0200155 }
156
157 /* next */
158 areg = areg->next;
159 arc_index++;
160 }
161
162 return 0;
163}
164
165VLIB_INIT_FUNCTION (vnet_feature_init);
166
Damjan Marion87cd1192016-11-04 11:00:27 +0100167u8
168vnet_get_feature_arc_index (const char *s)
Damjan Marion22311502016-10-28 20:30:15 +0200169{
170 vnet_feature_main_t *fm = &feature_main;
171 vnet_feature_arc_registration_t *reg;
172 uword *p;
173
174 p = hash_get_mem (fm->arc_index_by_name, s);
175 if (p == 0)
176 return ~0;
177
178 reg = uword_to_pointer (p[0], vnet_feature_arc_registration_t *);
179 return reg->feature_arc_index;
180}
181
Pavel Kotucek7490a752016-11-15 09:19:11 +0100182vnet_feature_registration_t *
183vnet_get_feature_reg (const char *arc_name, const char *node_name)
184{
185 u8 arc_index;
186
187 arc_index = vnet_get_feature_arc_index (arc_name);
188 if (arc_index == (u8) ~ 0)
189 return 0;
190
191 vnet_feature_main_t *fm = &feature_main;
192 vnet_feature_registration_t *reg;
193 uword *p;
194
195 p = hash_get_mem (fm->next_feature_by_name[arc_index], node_name);
196 if (p == 0)
197 return 0;
198
199 reg = uword_to_pointer (p[0], vnet_feature_registration_t *);
200 return reg;
201}
202
Damjan Marion22311502016-10-28 20:30:15 +0200203u32
Damjan Marion87cd1192016-11-04 11:00:27 +0100204vnet_get_feature_index (u8 arc, const char *s)
Damjan Marion22311502016-10-28 20:30:15 +0200205{
206 vnet_feature_main_t *fm = &feature_main;
207 vnet_feature_registration_t *reg;
208 uword *p;
209
Damjan Marion21da6ce2016-11-28 18:21:59 +0100210 if (s == 0)
211 return ~0;
212
Damjan Marion22311502016-10-28 20:30:15 +0200213 p = hash_get_mem (fm->next_feature_by_name[arc], s);
214 if (p == 0)
215 return ~0;
216
217 reg = uword_to_pointer (p[0], vnet_feature_registration_t *);
Damjan Marion8b3191e2016-11-09 19:54:20 +0100218 return reg->feature_index;
Damjan Marion22311502016-10-28 20:30:15 +0200219}
220
Damjan Marion96b41f72016-11-10 18:01:42 +0100221int
Damjan Marion8b3191e2016-11-09 19:54:20 +0100222vnet_feature_enable_disable_with_index (u8 arc_index, u32 feature_index,
223 u32 sw_if_index, int enable_disable,
224 void *feature_config,
225 u32 n_feature_config_bytes)
Damjan Marion22311502016-10-28 20:30:15 +0200226{
227 vnet_feature_main_t *fm = &feature_main;
228 vnet_feature_config_main_t *cm;
Damjan Marion21da6ce2016-11-28 18:21:59 +0100229 i16 feature_count;
Matthew Smithc3267ed2018-05-15 15:51:30 -0500230 u32 ci;
Damjan Marion22311502016-10-28 20:30:15 +0200231
Damjan Marion05bb1dd2016-11-08 21:28:22 +0100232 if (arc_index == (u8) ~ 0)
Damjan Marion96b41f72016-11-10 18:01:42 +0100233 return VNET_API_ERROR_INVALID_VALUE;
Damjan Marion22311502016-10-28 20:30:15 +0200234
Damjan Marion22311502016-10-28 20:30:15 +0200235 if (feature_index == ~0)
Damjan Marion96b41f72016-11-10 18:01:42 +0100236 return VNET_API_ERROR_INVALID_VALUE_2;
Damjan Marion8b3191e2016-11-09 19:54:20 +0100237
238 cm = &fm->feature_config_mains[arc_index];
239 vec_validate_init_empty (cm->config_index_by_sw_if_index, sw_if_index, ~0);
Matthew Smithc3267ed2018-05-15 15:51:30 -0500240 ci = cm->config_index_by_sw_if_index[sw_if_index];
Damjan Marion22311502016-10-28 20:30:15 +0200241
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100242 vec_validate (fm->feature_count_by_sw_if_index[arc_index], sw_if_index);
Damjan Marion21da6ce2016-11-28 18:21:59 +0100243 feature_count = fm->feature_count_by_sw_if_index[arc_index][sw_if_index];
244
245 if (!enable_disable && feature_count < 1)
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100246 return 0;
247
Damjan Marion22311502016-10-28 20:30:15 +0200248 ci = (enable_disable
249 ? vnet_config_add_feature
250 : vnet_config_del_feature)
251 (vlib_get_main (), &cm->config_main, ci, feature_index, feature_config,
252 n_feature_config_bytes);
Matthew Smithc3267ed2018-05-15 15:51:30 -0500253 if (ci == ~0)
Klement Sekera3ecc2212018-03-27 10:34:43 +0200254 {
255 return 0;
256 }
Damjan Marion22311502016-10-28 20:30:15 +0200257 cm->config_index_by_sw_if_index[sw_if_index] = ci;
258
Damjan Marion21da6ce2016-11-28 18:21:59 +0100259 /* update feature count */
260 enable_disable = (enable_disable > 0);
261 feature_count += enable_disable ? 1 : -1;
Damjan Marion21da6ce2016-11-28 18:21:59 +0100262 ASSERT (feature_count >= 0);
Damjan Marion22311502016-10-28 20:30:15 +0200263
Damjan Marion21da6ce2016-11-28 18:21:59 +0100264 fm->sw_if_index_has_features[arc_index] =
265 clib_bitmap_set (fm->sw_if_index_has_features[arc_index], sw_if_index,
266 (feature_count > 0));
Neale Rannsb069a692017-03-15 12:34:25 -0400267 adj_feature_update (sw_if_index, arc_index, (feature_count > 0));
Damjan Marion21da6ce2016-11-28 18:21:59 +0100268
269 fm->feature_count_by_sw_if_index[arc_index][sw_if_index] = feature_count;
Damjan Marion96b41f72016-11-10 18:01:42 +0100270 return 0;
Damjan Marion22311502016-10-28 20:30:15 +0200271}
272
Damjan Marion8b3191e2016-11-09 19:54:20 +0100273int
274vnet_feature_enable_disable (const char *arc_name, const char *node_name,
275 u32 sw_if_index, int enable_disable,
276 void *feature_config, u32 n_feature_config_bytes)
277{
278 u32 feature_index;
279 u8 arc_index;
280
281 arc_index = vnet_get_feature_arc_index (arc_name);
282
283 if (arc_index == (u8) ~ 0)
284 return VNET_API_ERROR_INVALID_VALUE;
285
286 feature_index = vnet_get_feature_index (arc_index, node_name);
287
288 return vnet_feature_enable_disable_with_index (arc_index, feature_index,
289 sw_if_index, enable_disable,
290 feature_config,
291 n_feature_config_bytes);
292}
293
Dave Barach525c9d02018-05-26 10:48:55 -0400294static int
295feature_cmp (void *a1, void *a2)
296{
297 vnet_feature_registration_t *reg1 = a1;
298 vnet_feature_registration_t *reg2 = a2;
299
300 return (int) reg1->feature_index - reg2->feature_index;
301}
Damjan Marion22311502016-10-28 20:30:15 +0200302
303/** Display the set of available driver features.
304 Useful for verifying that expected features are present
305*/
306
307static clib_error_t *
308show_features_command_fn (vlib_main_t * vm,
309 unformat_input_t * input, vlib_cli_command_t * cmd)
310{
311 vnet_feature_main_t *fm = &feature_main;
312 vnet_feature_arc_registration_t *areg;
313 vnet_feature_registration_t *freg;
Dave Barach525c9d02018-05-26 10:48:55 -0400314 vnet_feature_registration_t *feature_regs = 0;
315 int verbose = 0;
316
317 if (unformat (input, "verbose"))
318 verbose = 1;
Damjan Marion22311502016-10-28 20:30:15 +0200319
320 vlib_cli_output (vm, "Available feature paths");
321
322 areg = fm->next_arc;
323 while (areg)
324 {
Dave Barach525c9d02018-05-26 10:48:55 -0400325 if (verbose)
326 vlib_cli_output (vm, "[%2d] %s:", areg->feature_arc_index,
327 areg->arc_name);
328 else
329 vlib_cli_output (vm, "%s:", areg->arc_name);
330
Damjan Marion96b41f72016-11-10 18:01:42 +0100331 freg = fm->next_feature_by_arc[areg->feature_arc_index];
332 while (freg)
333 {
Dave Barach525c9d02018-05-26 10:48:55 -0400334 vec_add1 (feature_regs, freg[0]);
Damjan Marion13adc3d2018-04-09 20:59:53 +0200335 freg = freg->next_in_arc;
Damjan Marion96b41f72016-11-10 18:01:42 +0100336 }
Damjan Marion22311502016-10-28 20:30:15 +0200337
Dave Barach525c9d02018-05-26 10:48:55 -0400338 vec_sort_with_function (feature_regs, feature_cmp);
Damjan Marion22311502016-10-28 20:30:15 +0200339
Dave Barach525c9d02018-05-26 10:48:55 -0400340 vec_foreach (freg, feature_regs)
341 {
342 if (verbose)
343 vlib_cli_output (vm, " [%2d]: %s\n", freg->feature_index,
344 freg->node_name);
345 else
346 vlib_cli_output (vm, " %s\n", freg->node_name);
347 }
348 vec_reset_length (feature_regs);
Damjan Marion22311502016-10-28 20:30:15 +0200349 /* next */
350 areg = areg->next;
351 }
Dave Barach525c9d02018-05-26 10:48:55 -0400352 vec_free (feature_regs);
Damjan Marion22311502016-10-28 20:30:15 +0200353
354 return 0;
355}
356
357/*?
358 * Display the set of available driver features
359 *
360 * @cliexpar
361 * Example:
Dave Barach525c9d02018-05-26 10:48:55 -0400362 * @cliexcmd{show features [verbose]}
Damjan Marion22311502016-10-28 20:30:15 +0200363 * @cliexend
364 * @endparblock
365?*/
366/* *INDENT-OFF* */
367VLIB_CLI_COMMAND (show_features_command, static) = {
368 .path = "show features",
369 .short_help = "show features",
370 .function = show_features_command_fn,
371};
372/* *INDENT-ON* */
373
374/** Display the set of driver features configured on a specific interface
375 * Called by "show interface" handler
376 */
377
378void
Dave Barach525c9d02018-05-26 10:48:55 -0400379vnet_interface_features_show (vlib_main_t * vm, u32 sw_if_index, int verbose)
Damjan Marion22311502016-10-28 20:30:15 +0200380{
381 vnet_feature_main_t *fm = &feature_main;
382 u32 node_index, current_config_index;
383 u16 feature_arc;
384 vnet_feature_config_main_t *cm = fm->feature_config_mains;
385 vnet_feature_arc_registration_t *areg;
386 vnet_config_main_t *vcm;
387 vnet_config_t *cfg;
388 u32 cfg_index;
389 vnet_config_feature_t *feat;
390 vlib_node_t *n;
391 int i;
392
Dave Barach525c9d02018-05-26 10:48:55 -0400393 vlib_cli_output (vm, "Feature paths configured on %U...",
Damjan Marion22311502016-10-28 20:30:15 +0200394 format_vnet_sw_if_index_name,
395 vnet_get_main (), sw_if_index);
396
397 areg = fm->next_arc;
398 while (areg)
399 {
400 feature_arc = areg->feature_arc_index;
401 vcm = &(cm[feature_arc].config_main);
402
403 vlib_cli_output (vm, "\n%s:", areg->arc_name);
404 areg = areg->next;
405
406 if (NULL == cm[feature_arc].config_index_by_sw_if_index ||
Damjan Marion87cd1192016-11-04 11:00:27 +0100407 vec_len (cm[feature_arc].config_index_by_sw_if_index) <=
408 sw_if_index)
Damjan Marion22311502016-10-28 20:30:15 +0200409 {
410 vlib_cli_output (vm, " none configured");
411 continue;
412 }
413
414 current_config_index =
415 vec_elt (cm[feature_arc].config_index_by_sw_if_index, sw_if_index);
416
417 if (current_config_index == ~0)
418 {
419 vlib_cli_output (vm, " none configured");
420 continue;
421 }
422
423 ASSERT (current_config_index
424 < vec_len (vcm->config_pool_index_by_user_index));
425
426 cfg_index = vcm->config_pool_index_by_user_index[current_config_index];
427 cfg = pool_elt_at_index (vcm->config_pool, cfg_index);
428
429 for (i = 0; i < vec_len (cfg->features); i++)
430 {
431 feat = cfg->features + i;
432 node_index = feat->node_index;
433 n = vlib_get_node (vm, node_index);
Dave Barach525c9d02018-05-26 10:48:55 -0400434 if (verbose)
435 vlib_cli_output (vm, " [%2d] %v", feat->feature_index, n->name);
436 else
437 vlib_cli_output (vm, " %v", n->name);
Damjan Marion22311502016-10-28 20:30:15 +0200438 }
439 }
440}
441
Pavel Kotucek7490a752016-11-15 09:19:11 +0100442static clib_error_t *
443set_interface_features_command_fn (vlib_main_t * vm,
444 unformat_input_t * input,
445 vlib_cli_command_t * cmd)
446{
447 vnet_main_t *vnm = vnet_get_main ();
448 unformat_input_t _line_input, *line_input = &_line_input;
449 clib_error_t *error = 0;
450
451 u8 *arc_name = 0;
452 u8 *feature_name = 0;
453 u32 sw_if_index = ~0;
454 u8 enable = 1;
455
456 /* Get a line of input. */
457 if (!unformat_user (input, unformat_line_input, line_input))
Paul Vinciguerra3b4a6a12018-10-02 19:02:16 -0700458 return 0;
Pavel Kotucek7490a752016-11-15 09:19:11 +0100459
460 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
461 {
462 if (unformat
463 (line_input, "%U %v", unformat_vnet_sw_interface, vnm, &sw_if_index,
464 &feature_name))
465 ;
466 else if (unformat (line_input, "arc %v", &arc_name))
467 ;
468 else if (unformat (line_input, "disable"))
469 enable = 0;
470 else
471 {
Dave Barach525c9d02018-05-26 10:48:55 -0400472 if (feature_name && arc_name)
473 break;
Pavel Kotucek7490a752016-11-15 09:19:11 +0100474 error = unformat_parse_error (line_input);
475 goto done;
476 }
477 }
478
479 if (sw_if_index == ~0)
480 {
481 error = clib_error_return (0, "Interface not specified...");
482 goto done;
483 }
484
485 vec_add1 (arc_name, 0);
486 vec_add1 (feature_name, 0);
487
488 vnet_feature_registration_t *reg;
489 reg =
490 vnet_get_feature_reg ((const char *) arc_name,
491 (const char *) feature_name);
492 if (reg == 0)
493 {
494 error = clib_error_return (0, "Unknown feature...");
495 goto done;
496 }
497 if (reg->enable_disable_cb)
498 error = reg->enable_disable_cb (sw_if_index, enable);
499 if (!error)
500 vnet_feature_enable_disable ((const char *) arc_name,
501 (const char *) feature_name, sw_if_index,
502 enable, 0, 0);
503
504done:
505 vec_free (feature_name);
506 vec_free (arc_name);
Billy McFall614c1312017-03-01 17:01:06 -0500507 unformat_free (line_input);
Pavel Kotucek7490a752016-11-15 09:19:11 +0100508 return error;
509}
510
511/*?
512 * Set feature for given interface
513 *
514 * @cliexpar
515 * Example:
516 * @cliexcmd{set interface feature GigabitEthernet2/0/0 ip4_flow_classify arc ip4_unicast}
517 * @cliexend
518 * @endparblock
519?*/
520/* *INDENT-OFF* */
521VLIB_CLI_COMMAND (set_interface_feature_command, static) = {
522 .path = "set interface feature",
Pierre Pfister1bfd3722017-09-18 11:40:32 +0200523 .short_help = "set interface feature <intfc> <feature_name> arc <arc_name> "
524 "[disable]",
Pavel Kotucek7490a752016-11-15 09:19:11 +0100525 .function = set_interface_features_command_fn,
526};
527/* *INDENT-ON* */
528
Damjan Marion22311502016-10-28 20:30:15 +0200529/*
530 * fd.io coding-style-patch-verification: ON
531 *
532 * Local Variables:
533 * eval: (c-set-style "gnu")
534 * End:
535 */