blob: c93f586c349a198b5f7866e588e9f643ce9fd6db [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 Rannsc8972fe2019-12-02 23:10:08 +000017
Damjan Marion22311502016-10-28 20:30:15 +020018vnet_feature_main_t feature_main;
19
Neale Rannsc8972fe2019-12-02 23:10:08 +000020typedef struct vnet_feature_upd_registration_t_
21{
22 vnet_feature_update_cb_t cb;
23 void *data;
24} vnet_feature_upd_registration_t;
25
26static vnet_feature_upd_registration_t *regs;
27
28void
29vnet_feature_register (vnet_feature_update_cb_t cb, void *data)
30{
31 vnet_feature_upd_registration_t *reg;
32
33 vec_add2 (regs, reg, 1);
34
35 reg->cb = cb;
36 reg->data = data;
37}
38
39static void
Benoît Ganne6178bda2020-11-04 10:02:03 +010040vnet_feature_reg_invoke (u32 sw_if_index, u8 arc_index, u8 is_enable)
Neale Rannsc8972fe2019-12-02 23:10:08 +000041{
42 vnet_feature_upd_registration_t *reg;
43
44 vec_foreach (reg, regs)
45 reg->cb (sw_if_index, arc_index, is_enable, reg->data);
46}
47
48
Damjan Marion22311502016-10-28 20:30:15 +020049static clib_error_t *
50vnet_feature_init (vlib_main_t * vm)
51{
52 vnet_feature_main_t *fm = &feature_main;
53 vnet_feature_registration_t *freg;
54 vnet_feature_arc_registration_t *areg;
Dave Barach2dd192b2018-11-19 09:31:48 -050055 vnet_feature_constraint_registration_t *creg;
Damjan Marion22311502016-10-28 20:30:15 +020056 u32 arc_index = 0;
57
58 fm->arc_index_by_name = hash_create_string (0, sizeof (uword));
59 areg = fm->next_arc;
60
61 /* process feature arc registrations */
62 while (areg)
63 {
64 char *s;
65 int i = 0;
66 areg->feature_arc_index = arc_index;
Damjan Marion8b3191e2016-11-09 19:54:20 +010067 if (areg->arc_index_ptr)
68 *areg->arc_index_ptr = arc_index;
Damjan Marion22311502016-10-28 20:30:15 +020069 hash_set_mem (fm->arc_index_by_name, areg->arc_name,
70 pointer_to_uword (areg));
71
72 /* process start nodes */
73 while ((s = areg->start_nodes[i]))
74 {
Damjan Marion22311502016-10-28 20:30:15 +020075 i++;
76 }
77 areg->n_start_nodes = i;
78
79 /* next */
80 areg = areg->next;
81 arc_index++;
82 }
83
84 vec_validate (fm->next_feature_by_arc, arc_index - 1);
85 vec_validate (fm->feature_nodes, arc_index - 1);
86 vec_validate (fm->feature_config_mains, arc_index - 1);
87 vec_validate (fm->next_feature_by_name, arc_index - 1);
88 vec_validate (fm->sw_if_index_has_features, arc_index - 1);
89 vec_validate (fm->feature_count_by_sw_if_index, arc_index - 1);
Dave Barach2dd192b2018-11-19 09:31:48 -050090 vec_validate (fm->next_constraint_by_arc, arc_index - 1);
Damjan Marion22311502016-10-28 20:30:15 +020091
92 freg = fm->next_feature;
93 while (freg)
94 {
Damjan Marion96b41f72016-11-10 18:01:42 +010095 vnet_feature_registration_t *next;
Damjan Marion22311502016-10-28 20:30:15 +020096 uword *p = hash_get_mem (fm->arc_index_by_name, freg->arc_name);
97 if (p == 0)
Florin Coras3d2a9142017-08-16 21:23:44 -070098 {
99 /* Don't start vpp with broken features arcs */
100 clib_warning ("Unknown feature arc '%s'", freg->arc_name);
101 os_exit (1);
102 }
Damjan Marion22311502016-10-28 20:30:15 +0200103
104 areg = uword_to_pointer (p[0], vnet_feature_arc_registration_t *);
105 arc_index = areg->feature_arc_index;
106
Damjan Marion96b41f72016-11-10 18:01:42 +0100107 next = freg->next;
Damjan Marion13adc3d2018-04-09 20:59:53 +0200108 freg->next_in_arc = fm->next_feature_by_arc[arc_index];
Damjan Marion96b41f72016-11-10 18:01:42 +0100109 fm->next_feature_by_arc[arc_index] = freg;
Damjan Marion22311502016-10-28 20:30:15 +0200110
111 /* next */
Damjan Marion96b41f72016-11-10 18:01:42 +0100112 freg = next;
Damjan Marion22311502016-10-28 20:30:15 +0200113 }
114
Dave Barach2dd192b2018-11-19 09:31:48 -0500115 /* Move bulk constraints to the constraint by arc lists */
116 creg = fm->next_constraint;
117 while (creg)
118 {
119 vnet_feature_constraint_registration_t *next;
120 uword *p = hash_get_mem (fm->arc_index_by_name, creg->arc_name);
121 if (p == 0)
122 {
123 /* Don't start vpp with broken features arcs */
124 clib_warning ("Unknown feature arc '%s'", creg->arc_name);
125 os_exit (1);
126 }
127
128 areg = uword_to_pointer (p[0], vnet_feature_arc_registration_t *);
129 arc_index = areg->feature_arc_index;
130
131 next = creg->next;
132 creg->next_in_arc = fm->next_constraint_by_arc[arc_index];
133 fm->next_constraint_by_arc[arc_index] = creg;
134
135 /* next */
136 creg = next;
137 }
138
139
Damjan Marion96b41f72016-11-10 18:01:42 +0100140 areg = fm->next_arc;
Damjan Marion22311502016-10-28 20:30:15 +0200141 while (areg)
142 {
143 clib_error_t *error;
144 vnet_feature_config_main_t *cm;
145 vnet_config_main_t *vcm;
Dave Baracha25def72018-11-26 11:04:45 -0500146 char **features_in_order, *last_feature;
Damjan Marion22311502016-10-28 20:30:15 +0200147
148 arc_index = areg->feature_arc_index;
149 cm = &fm->feature_config_mains[arc_index];
150 vcm = &cm->config_main;
Dave Barach2dd192b2018-11-19 09:31:48 -0500151 if ((error = vnet_feature_arc_init
152 (vm, vcm, areg->start_nodes, areg->n_start_nodes,
Dave Barach5f9f3c82019-11-22 17:42:58 -0500153 areg->last_in_arc,
Dave Barach2dd192b2018-11-19 09:31:48 -0500154 fm->next_feature_by_arc[arc_index],
155 fm->next_constraint_by_arc[arc_index],
156 &fm->feature_nodes[arc_index])))
Damjan Marion22311502016-10-28 20:30:15 +0200157 {
Florin Coras3d2a9142017-08-16 21:23:44 -0700158 clib_error_report (error);
159 os_exit (1);
Damjan Marion22311502016-10-28 20:30:15 +0200160 }
161
Dave Baracha25def72018-11-26 11:04:45 -0500162 features_in_order = fm->feature_nodes[arc_index];
163
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700164 /* If specified, verify that the last node in the arc is actually last */
Dave Baracha25def72018-11-26 11:04:45 -0500165 if (areg->last_in_arc && vec_len (features_in_order) > 0)
166 {
167 last_feature = features_in_order[vec_len (features_in_order) - 1];
168 if (strncmp (areg->last_in_arc, last_feature,
169 strlen (areg->last_in_arc)))
170 clib_warning
171 ("WARNING: %s arc: last node is %s, but expected %s!",
172 areg->arc_name, last_feature, areg->last_in_arc);
173 }
174
Damjan Marion22311502016-10-28 20:30:15 +0200175 fm->next_feature_by_name[arc_index] =
176 hash_create_string (0, sizeof (uword));
177 freg = fm->next_feature_by_arc[arc_index];
178
179 while (freg)
180 {
181 hash_set_mem (fm->next_feature_by_name[arc_index],
182 freg->node_name, pointer_to_uword (freg));
Damjan Marion13adc3d2018-04-09 20:59:53 +0200183 freg = freg->next_in_arc;
Damjan Marion22311502016-10-28 20:30:15 +0200184 }
185
186 /* next */
187 areg = areg->next;
188 arc_index++;
189 }
190
191 return 0;
192}
193
194VLIB_INIT_FUNCTION (vnet_feature_init);
195
Damjan Marion87cd1192016-11-04 11:00:27 +0100196u8
197vnet_get_feature_arc_index (const char *s)
Damjan Marion22311502016-10-28 20:30:15 +0200198{
199 vnet_feature_main_t *fm = &feature_main;
200 vnet_feature_arc_registration_t *reg;
201 uword *p;
202
203 p = hash_get_mem (fm->arc_index_by_name, s);
204 if (p == 0)
205 return ~0;
206
207 reg = uword_to_pointer (p[0], vnet_feature_arc_registration_t *);
208 return reg->feature_arc_index;
209}
210
Pavel Kotucek7490a752016-11-15 09:19:11 +0100211vnet_feature_registration_t *
212vnet_get_feature_reg (const char *arc_name, const char *node_name)
213{
214 u8 arc_index;
215
216 arc_index = vnet_get_feature_arc_index (arc_name);
217 if (arc_index == (u8) ~ 0)
218 return 0;
219
220 vnet_feature_main_t *fm = &feature_main;
221 vnet_feature_registration_t *reg;
222 uword *p;
223
224 p = hash_get_mem (fm->next_feature_by_name[arc_index], node_name);
225 if (p == 0)
226 return 0;
227
228 reg = uword_to_pointer (p[0], vnet_feature_registration_t *);
229 return reg;
230}
231
Damjan Marion22311502016-10-28 20:30:15 +0200232u32
Damjan Marion87cd1192016-11-04 11:00:27 +0100233vnet_get_feature_index (u8 arc, const char *s)
Damjan Marion22311502016-10-28 20:30:15 +0200234{
235 vnet_feature_main_t *fm = &feature_main;
236 vnet_feature_registration_t *reg;
237 uword *p;
238
Damjan Marion21da6ce2016-11-28 18:21:59 +0100239 if (s == 0)
240 return ~0;
241
Damjan Marion22311502016-10-28 20:30:15 +0200242 p = hash_get_mem (fm->next_feature_by_name[arc], s);
243 if (p == 0)
244 return ~0;
245
246 reg = uword_to_pointer (p[0], vnet_feature_registration_t *);
Damjan Marion8b3191e2016-11-09 19:54:20 +0100247 return reg->feature_index;
Damjan Marion22311502016-10-28 20:30:15 +0200248}
249
Damjan Marion96b41f72016-11-10 18:01:42 +0100250int
Damjan Marion8b3191e2016-11-09 19:54:20 +0100251vnet_feature_enable_disable_with_index (u8 arc_index, u32 feature_index,
252 u32 sw_if_index, int enable_disable,
253 void *feature_config,
254 u32 n_feature_config_bytes)
Damjan Marion22311502016-10-28 20:30:15 +0200255{
256 vnet_feature_main_t *fm = &feature_main;
257 vnet_feature_config_main_t *cm;
Damjan Marion21da6ce2016-11-28 18:21:59 +0100258 i16 feature_count;
Matthew Smithc3267ed2018-05-15 15:51:30 -0500259 u32 ci;
Damjan Marion22311502016-10-28 20:30:15 +0200260
Damjan Marion05bb1dd2016-11-08 21:28:22 +0100261 if (arc_index == (u8) ~ 0)
Damjan Marion96b41f72016-11-10 18:01:42 +0100262 return VNET_API_ERROR_INVALID_VALUE;
Damjan Marion22311502016-10-28 20:30:15 +0200263
Damjan Marion22311502016-10-28 20:30:15 +0200264 if (feature_index == ~0)
Damjan Marion96b41f72016-11-10 18:01:42 +0100265 return VNET_API_ERROR_INVALID_VALUE_2;
Damjan Marion8b3191e2016-11-09 19:54:20 +0100266
267 cm = &fm->feature_config_mains[arc_index];
268 vec_validate_init_empty (cm->config_index_by_sw_if_index, sw_if_index, ~0);
Matthew Smithc3267ed2018-05-15 15:51:30 -0500269 ci = cm->config_index_by_sw_if_index[sw_if_index];
Damjan Marion22311502016-10-28 20:30:15 +0200270
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100271 vec_validate (fm->feature_count_by_sw_if_index[arc_index], sw_if_index);
Damjan Marion21da6ce2016-11-28 18:21:59 +0100272 feature_count = fm->feature_count_by_sw_if_index[arc_index][sw_if_index];
273
274 if (!enable_disable && feature_count < 1)
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100275 return 0;
276
Damjan Marion22311502016-10-28 20:30:15 +0200277 ci = (enable_disable
278 ? vnet_config_add_feature
279 : vnet_config_del_feature)
280 (vlib_get_main (), &cm->config_main, ci, feature_index, feature_config,
281 n_feature_config_bytes);
Matthew Smithc3267ed2018-05-15 15:51:30 -0500282 if (ci == ~0)
Klement Sekera3ecc2212018-03-27 10:34:43 +0200283 {
284 return 0;
285 }
Damjan Marion22311502016-10-28 20:30:15 +0200286 cm->config_index_by_sw_if_index[sw_if_index] = ci;
287
Damjan Marion21da6ce2016-11-28 18:21:59 +0100288 /* update feature count */
289 enable_disable = (enable_disable > 0);
290 feature_count += enable_disable ? 1 : -1;
Damjan Marion21da6ce2016-11-28 18:21:59 +0100291 ASSERT (feature_count >= 0);
Damjan Marion22311502016-10-28 20:30:15 +0200292
Damjan Marion21da6ce2016-11-28 18:21:59 +0100293 fm->sw_if_index_has_features[arc_index] =
294 clib_bitmap_set (fm->sw_if_index_has_features[arc_index], sw_if_index,
295 (feature_count > 0));
Benoît Ganne6178bda2020-11-04 10:02:03 +0100296 vnet_feature_reg_invoke (sw_if_index, arc_index, (feature_count > 0));
Damjan Marion21da6ce2016-11-28 18:21:59 +0100297
298 fm->feature_count_by_sw_if_index[arc_index][sw_if_index] = feature_count;
Damjan Marion96b41f72016-11-10 18:01:42 +0100299 return 0;
Damjan Marion22311502016-10-28 20:30:15 +0200300}
301
Damjan Marion8b3191e2016-11-09 19:54:20 +0100302int
303vnet_feature_enable_disable (const char *arc_name, const char *node_name,
304 u32 sw_if_index, int enable_disable,
305 void *feature_config, u32 n_feature_config_bytes)
306{
307 u32 feature_index;
308 u8 arc_index;
309
310 arc_index = vnet_get_feature_arc_index (arc_name);
311
312 if (arc_index == (u8) ~ 0)
313 return VNET_API_ERROR_INVALID_VALUE;
314
315 feature_index = vnet_get_feature_index (arc_index, node_name);
316
317 return vnet_feature_enable_disable_with_index (arc_index, feature_index,
318 sw_if_index, enable_disable,
319 feature_config,
320 n_feature_config_bytes);
321}
322
Dave Barachcaebbcf2020-07-29 18:07:02 -0400323int
324vnet_feature_is_enabled (const char *arc_name, const char *feature_node_name,
325 u32 sw_if_index)
326{
327 vnet_feature_main_t *fm = &feature_main;
328 vnet_feature_config_main_t *cm;
329 vnet_config_main_t *ccm;
330 vnet_config_t *current_config;
331 vnet_config_feature_t *f;
332 u32 feature_index;
333 u32 ci;
334 u8 arc_index;
335 u32 *p;
336
337 arc_index = vnet_get_feature_arc_index (arc_name);
338
339 /* No such arc? */
340 if (arc_index == (u8) ~ 0)
341 return VNET_API_ERROR_INVALID_VALUE;
342
343 feature_index = vnet_get_feature_index (arc_index, feature_node_name);
344
345 /* No such feature? */
346 if (feature_index == (u32) ~ 0)
347 return VNET_API_ERROR_INVALID_VALUE_2;
348
349 cm = &fm->feature_config_mains[arc_index];
350
351 if (sw_if_index < vec_len (cm->config_index_by_sw_if_index))
352 ci = vec_elt (cm->config_index_by_sw_if_index, sw_if_index);
353 else
354 /* sw_if_index out of range, certainly not enabled */
355 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
356
357 /* No features were ever configured? */
358 if (ci == ~0)
359 return 0;
360
361 ccm = &cm->config_main;
362
363 p = heap_elt_at_index (ccm->config_string_heap, ci);
364
365 current_config = pool_elt_at_index (ccm->config_pool, p[-1]);
366
367 /* Find feature with the required index */
368 vec_foreach (f, current_config->features)
369 {
370 if (f->feature_index == feature_index)
371 /* Feature was enabled */
372 return 1;
373 }
374 /* feature wasn't enabled */
375 return 0;
376}
377
378
Neale Ranns5d0136f2020-05-12 08:51:02 +0000379u32
Neale Ranns4ec36c52020-03-31 09:21:29 -0400380vnet_feature_modify_end_node (u8 arc_index,
381 u32 sw_if_index, u32 end_node_index)
382{
383 vnet_feature_main_t *fm = &feature_main;
384 vnet_feature_config_main_t *cm;
385 u32 ci;
386
387 if (arc_index == (u8) ~ 0)
388 return VNET_API_ERROR_INVALID_VALUE;
389
390 if (end_node_index == ~0)
391 return VNET_API_ERROR_INVALID_VALUE_2;
392
393 cm = &fm->feature_config_mains[arc_index];
394 vec_validate_init_empty (cm->config_index_by_sw_if_index, sw_if_index, ~0);
395 ci = cm->config_index_by_sw_if_index[sw_if_index];
396
397 ci = vnet_config_modify_end_node (vlib_get_main (), &cm->config_main,
398 ci, end_node_index);
399
Neale Ranns5d0136f2020-05-12 08:51:02 +0000400 if (ci != ~0)
401 cm->config_index_by_sw_if_index[sw_if_index] = ci;
Neale Ranns4ec36c52020-03-31 09:21:29 -0400402
Neale Ranns5d0136f2020-05-12 08:51:02 +0000403 return ci;
Neale Ranns4ec36c52020-03-31 09:21:29 -0400404}
405
Dave Barach525c9d02018-05-26 10:48:55 -0400406static int
407feature_cmp (void *a1, void *a2)
408{
409 vnet_feature_registration_t *reg1 = a1;
410 vnet_feature_registration_t *reg2 = a2;
411
412 return (int) reg1->feature_index - reg2->feature_index;
413}
Damjan Marion22311502016-10-28 20:30:15 +0200414
415/** Display the set of available driver features.
416 Useful for verifying that expected features are present
417*/
418
419static clib_error_t *
420show_features_command_fn (vlib_main_t * vm,
421 unformat_input_t * input, vlib_cli_command_t * cmd)
422{
423 vnet_feature_main_t *fm = &feature_main;
424 vnet_feature_arc_registration_t *areg;
425 vnet_feature_registration_t *freg;
Dave Barach525c9d02018-05-26 10:48:55 -0400426 vnet_feature_registration_t *feature_regs = 0;
427 int verbose = 0;
428
429 if (unformat (input, "verbose"))
430 verbose = 1;
Damjan Marion22311502016-10-28 20:30:15 +0200431
432 vlib_cli_output (vm, "Available feature paths");
433
434 areg = fm->next_arc;
435 while (areg)
436 {
Dave Barach525c9d02018-05-26 10:48:55 -0400437 if (verbose)
438 vlib_cli_output (vm, "[%2d] %s:", areg->feature_arc_index,
439 areg->arc_name);
440 else
441 vlib_cli_output (vm, "%s:", areg->arc_name);
442
Damjan Marion96b41f72016-11-10 18:01:42 +0100443 freg = fm->next_feature_by_arc[areg->feature_arc_index];
444 while (freg)
445 {
Dave Barach525c9d02018-05-26 10:48:55 -0400446 vec_add1 (feature_regs, freg[0]);
Damjan Marion13adc3d2018-04-09 20:59:53 +0200447 freg = freg->next_in_arc;
Damjan Marion96b41f72016-11-10 18:01:42 +0100448 }
Damjan Marion22311502016-10-28 20:30:15 +0200449
Dave Barach525c9d02018-05-26 10:48:55 -0400450 vec_sort_with_function (feature_regs, feature_cmp);
Damjan Marion22311502016-10-28 20:30:15 +0200451
Dave Barach525c9d02018-05-26 10:48:55 -0400452 vec_foreach (freg, feature_regs)
453 {
454 if (verbose)
455 vlib_cli_output (vm, " [%2d]: %s\n", freg->feature_index,
456 freg->node_name);
457 else
458 vlib_cli_output (vm, " %s\n", freg->node_name);
459 }
460 vec_reset_length (feature_regs);
Damjan Marion22311502016-10-28 20:30:15 +0200461 /* next */
462 areg = areg->next;
463 }
Dave Barach525c9d02018-05-26 10:48:55 -0400464 vec_free (feature_regs);
Damjan Marion22311502016-10-28 20:30:15 +0200465
466 return 0;
467}
468
469/*?
470 * Display the set of available driver features
471 *
472 * @cliexpar
473 * Example:
Dave Barach525c9d02018-05-26 10:48:55 -0400474 * @cliexcmd{show features [verbose]}
Damjan Marion22311502016-10-28 20:30:15 +0200475 * @cliexend
476 * @endparblock
477?*/
478/* *INDENT-OFF* */
479VLIB_CLI_COMMAND (show_features_command, static) = {
480 .path = "show features",
Paul Vinciguerrabaa17102019-11-06 15:41:45 -0500481 .short_help = "show features [verbose]",
Damjan Marion22311502016-10-28 20:30:15 +0200482 .function = show_features_command_fn,
483};
484/* *INDENT-ON* */
485
486/** Display the set of driver features configured on a specific interface
487 * Called by "show interface" handler
488 */
489
490void
Dave Barach525c9d02018-05-26 10:48:55 -0400491vnet_interface_features_show (vlib_main_t * vm, u32 sw_if_index, int verbose)
Damjan Marion22311502016-10-28 20:30:15 +0200492{
493 vnet_feature_main_t *fm = &feature_main;
494 u32 node_index, current_config_index;
495 u16 feature_arc;
496 vnet_feature_config_main_t *cm = fm->feature_config_mains;
497 vnet_feature_arc_registration_t *areg;
498 vnet_config_main_t *vcm;
499 vnet_config_t *cfg;
500 u32 cfg_index;
501 vnet_config_feature_t *feat;
502 vlib_node_t *n;
503 int i;
504
Dave Barach525c9d02018-05-26 10:48:55 -0400505 vlib_cli_output (vm, "Feature paths configured on %U...",
Damjan Marion22311502016-10-28 20:30:15 +0200506 format_vnet_sw_if_index_name,
507 vnet_get_main (), sw_if_index);
508
509 areg = fm->next_arc;
510 while (areg)
511 {
512 feature_arc = areg->feature_arc_index;
513 vcm = &(cm[feature_arc].config_main);
514
515 vlib_cli_output (vm, "\n%s:", areg->arc_name);
516 areg = areg->next;
517
Benoît Ganne6178bda2020-11-04 10:02:03 +0100518 if (!vnet_have_features (feature_arc, sw_if_index))
Damjan Marion22311502016-10-28 20:30:15 +0200519 {
520 vlib_cli_output (vm, " none configured");
521 continue;
522 }
523
524 current_config_index =
525 vec_elt (cm[feature_arc].config_index_by_sw_if_index, sw_if_index);
Benoît Ganne6178bda2020-11-04 10:02:03 +0100526 cfg_index =
527 vec_elt (vcm->config_pool_index_by_user_index, current_config_index);
Damjan Marion22311502016-10-28 20:30:15 +0200528 cfg = pool_elt_at_index (vcm->config_pool, cfg_index);
529
530 for (i = 0; i < vec_len (cfg->features); i++)
531 {
532 feat = cfg->features + i;
533 node_index = feat->node_index;
534 n = vlib_get_node (vm, node_index);
Dave Barach525c9d02018-05-26 10:48:55 -0400535 if (verbose)
536 vlib_cli_output (vm, " [%2d] %v", feat->feature_index, n->name);
537 else
538 vlib_cli_output (vm, " %v", n->name);
Damjan Marion22311502016-10-28 20:30:15 +0200539 }
Neale Ranns5d0136f2020-05-12 08:51:02 +0000540 if (verbose)
541 {
542 n =
543 vlib_get_node (vm,
544 vcm->end_node_indices_by_user_index
545 [current_config_index]);
546 vlib_cli_output (vm, " [end] %v", n->name);
547 }
Damjan Marion22311502016-10-28 20:30:15 +0200548 }
549}
550
Pavel Kotucek7490a752016-11-15 09:19:11 +0100551static clib_error_t *
552set_interface_features_command_fn (vlib_main_t * vm,
553 unformat_input_t * input,
554 vlib_cli_command_t * cmd)
555{
556 vnet_main_t *vnm = vnet_get_main ();
557 unformat_input_t _line_input, *line_input = &_line_input;
558 clib_error_t *error = 0;
559
560 u8 *arc_name = 0;
561 u8 *feature_name = 0;
562 u32 sw_if_index = ~0;
563 u8 enable = 1;
564
565 /* Get a line of input. */
566 if (!unformat_user (input, unformat_line_input, line_input))
Paul Vinciguerra3b4a6a12018-10-02 19:02:16 -0700567 return 0;
Pavel Kotucek7490a752016-11-15 09:19:11 +0100568
569 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
570 {
571 if (unformat
Paul Vinciguerraa4e2e7c2019-11-06 13:25:17 -0500572 (line_input, "%U %s arc %s", unformat_vnet_sw_interface, vnm,
573 &sw_if_index, &feature_name, &arc_name))
Pavel Kotucek7490a752016-11-15 09:19:11 +0100574 ;
575 else if (unformat (line_input, "disable"))
576 enable = 0;
577 else
578 {
579 error = unformat_parse_error (line_input);
580 goto done;
581 }
582 }
Paul Vinciguerraa4e2e7c2019-11-06 13:25:17 -0500583 if (!feature_name || !arc_name)
584 {
585 error = clib_error_return (0, "Both feature name and arc required...");
586 goto done;
587 }
Pavel Kotucek7490a752016-11-15 09:19:11 +0100588
589 if (sw_if_index == ~0)
590 {
591 error = clib_error_return (0, "Interface not specified...");
592 goto done;
593 }
594
595 vec_add1 (arc_name, 0);
596 vec_add1 (feature_name, 0);
597
Paul Vinciguerraa4e2e7c2019-11-06 13:25:17 -0500598 u8 arc_index;
599
600 arc_index = vnet_get_feature_arc_index ((const char *) arc_name);
601
602 if (arc_index == (u8) ~ 0)
603 {
604 error =
605 clib_error_return (0, "Unknown arc name (%s)... ",
606 (const char *) arc_name);
607 goto done;
608 }
609
Pavel Kotucek7490a752016-11-15 09:19:11 +0100610 vnet_feature_registration_t *reg;
611 reg =
612 vnet_get_feature_reg ((const char *) arc_name,
613 (const char *) feature_name);
614 if (reg == 0)
615 {
Paul Vinciguerraa4e2e7c2019-11-06 13:25:17 -0500616 error =
617 clib_error_return (0,
618 "Feature (%s) not registered to arc (%s)... See 'show features verbose' for valid feature/arc combinations. ",
619 feature_name, arc_name);
Pavel Kotucek7490a752016-11-15 09:19:11 +0100620 goto done;
621 }
622 if (reg->enable_disable_cb)
623 error = reg->enable_disable_cb (sw_if_index, enable);
624 if (!error)
625 vnet_feature_enable_disable ((const char *) arc_name,
626 (const char *) feature_name, sw_if_index,
627 enable, 0, 0);
628
629done:
630 vec_free (feature_name);
631 vec_free (arc_name);
Billy McFall614c1312017-03-01 17:01:06 -0500632 unformat_free (line_input);
Pavel Kotucek7490a752016-11-15 09:19:11 +0100633 return error;
634}
635
636/*?
637 * Set feature for given interface
638 *
639 * @cliexpar
640 * Example:
641 * @cliexcmd{set interface feature GigabitEthernet2/0/0 ip4_flow_classify arc ip4_unicast}
642 * @cliexend
643 * @endparblock
644?*/
645/* *INDENT-OFF* */
646VLIB_CLI_COMMAND (set_interface_feature_command, static) = {
647 .path = "set interface feature",
Pierre Pfister1bfd3722017-09-18 11:40:32 +0200648 .short_help = "set interface feature <intfc> <feature_name> arc <arc_name> "
649 "[disable]",
Pavel Kotucek7490a752016-11-15 09:19:11 +0100650 .function = set_interface_features_command_fn,
651};
652/* *INDENT-ON* */
653
Benoît Ganne6178bda2020-11-04 10:02:03 +0100654static clib_error_t *
655vnet_feature_add_del_sw_interface (vnet_main_t * vnm, u32 sw_if_index,
656 u32 is_add)
657{
658 vnet_feature_main_t *fm = &feature_main;
659 const vnet_feature_arc_registration_t *far;
660
661 if (is_add)
662 return 0;
663
664 /*
665 * remove all enabled features from an interface on deletion
666 */
667 for (far = fm->next_arc; far != 0; far = far->next)
668 {
669 const u8 arc_index = far->feature_arc_index;
670 vnet_feature_config_main_t *cm =
671 vec_elt_at_index (fm->feature_config_mains, arc_index);
672 const u32 ci =
673 vec_len (cm->config_index_by_sw_if_index) <=
674 sw_if_index ? ~0 : vec_elt (cm->config_index_by_sw_if_index,
675 sw_if_index);
676
677 if (~0 == ci)
678 continue;
679
680 fm->sw_if_index_has_features[arc_index] =
681 clib_bitmap_set (fm->sw_if_index_has_features[arc_index], sw_if_index,
682 0);
683
684 vnet_feature_reg_invoke (sw_if_index, arc_index, 0);
685
686 if (vec_len (fm->feature_count_by_sw_if_index[arc_index]) > sw_if_index)
687 vec_elt (fm->feature_count_by_sw_if_index[arc_index], sw_if_index) =
688 0;
689
690 vec_elt (cm->config_index_by_sw_if_index, sw_if_index) = ~0;
691 vnet_config_del (&cm->config_main, ci);
692 }
693
694 return 0;
695}
696
697VNET_SW_INTERFACE_ADD_DEL_FUNCTION_PRIO (vnet_feature_add_del_sw_interface,
698 VNET_ITF_FUNC_PRIORITY_HIGH);
699
Damjan Marion22311502016-10-28 20:30:15 +0200700/*
701 * fd.io coding-style-patch-verification: ON
702 *
703 * Local Variables:
704 * eval: (c-set-style "gnu")
705 * End:
706 */