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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <vnet/feature/feature.h>
17 #include <vnet/adj/adj.h>
19 vnet_feature_main_t feature_main;
22 vnet_feature_init (vlib_main_t * vm)
24 vnet_feature_main_t *fm = &feature_main;
25 vnet_feature_registration_t *freg;
26 vnet_feature_arc_registration_t *areg;
27 vnet_feature_constraint_registration_t *creg;
30 fm->arc_index_by_name = hash_create_string (0, sizeof (uword));
33 /* process feature arc registrations */
38 areg->feature_arc_index = arc_index;
39 if (areg->arc_index_ptr)
40 *areg->arc_index_ptr = arc_index;
41 hash_set_mem (fm->arc_index_by_name, areg->arc_name,
42 pointer_to_uword (areg));
44 /* process start nodes */
45 while ((s = areg->start_nodes[i]))
49 areg->n_start_nodes = i;
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);
62 vec_validate (fm->next_constraint_by_arc, arc_index - 1);
64 freg = fm->next_feature;
67 vnet_feature_registration_t *next;
68 uword *p = hash_get_mem (fm->arc_index_by_name, freg->arc_name);
71 /* Don't start vpp with broken features arcs */
72 clib_warning ("Unknown feature arc '%s'", freg->arc_name);
76 areg = uword_to_pointer (p[0], vnet_feature_arc_registration_t *);
77 arc_index = areg->feature_arc_index;
80 freg->next_in_arc = fm->next_feature_by_arc[arc_index];
81 fm->next_feature_by_arc[arc_index] = freg;
87 /* Move bulk constraints to the constraint by arc lists */
88 creg = fm->next_constraint;
91 vnet_feature_constraint_registration_t *next;
92 uword *p = hash_get_mem (fm->arc_index_by_name, creg->arc_name);
95 /* Don't start vpp with broken features arcs */
96 clib_warning ("Unknown feature arc '%s'", creg->arc_name);
100 areg = uword_to_pointer (p[0], vnet_feature_arc_registration_t *);
101 arc_index = areg->feature_arc_index;
104 creg->next_in_arc = fm->next_constraint_by_arc[arc_index];
105 fm->next_constraint_by_arc[arc_index] = creg;
116 vnet_feature_config_main_t *cm;
117 vnet_config_main_t *vcm;
118 char **features_in_order, *last_feature;
120 arc_index = areg->feature_arc_index;
121 cm = &fm->feature_config_mains[arc_index];
122 vcm = &cm->config_main;
123 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])))
129 clib_error_report (error);
133 features_in_order = fm->feature_nodes[arc_index];
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)
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)))
142 ("WARNING: %s arc: last node is %s, but expected %s!",
143 areg->arc_name, last_feature, areg->last_in_arc);
146 fm->next_feature_by_name[arc_index] =
147 hash_create_string (0, sizeof (uword));
148 freg = fm->next_feature_by_arc[arc_index];
152 hash_set_mem (fm->next_feature_by_name[arc_index],
153 freg->node_name, pointer_to_uword (freg));
154 freg = freg->next_in_arc;
165 VLIB_INIT_FUNCTION (vnet_feature_init);
168 vnet_get_feature_arc_index (const char *s)
170 vnet_feature_main_t *fm = &feature_main;
171 vnet_feature_arc_registration_t *reg;
174 p = hash_get_mem (fm->arc_index_by_name, s);
178 reg = uword_to_pointer (p[0], vnet_feature_arc_registration_t *);
179 return reg->feature_arc_index;
182 vnet_feature_registration_t *
183 vnet_get_feature_reg (const char *arc_name, const char *node_name)
187 arc_index = vnet_get_feature_arc_index (arc_name);
188 if (arc_index == (u8) ~ 0)
191 vnet_feature_main_t *fm = &feature_main;
192 vnet_feature_registration_t *reg;
195 p = hash_get_mem (fm->next_feature_by_name[arc_index], node_name);
199 reg = uword_to_pointer (p[0], vnet_feature_registration_t *);
204 vnet_get_feature_index (u8 arc, const char *s)
206 vnet_feature_main_t *fm = &feature_main;
207 vnet_feature_registration_t *reg;
213 p = hash_get_mem (fm->next_feature_by_name[arc], s);
217 reg = uword_to_pointer (p[0], vnet_feature_registration_t *);
218 return reg->feature_index;
222 vnet_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)
227 vnet_feature_main_t *fm = &feature_main;
228 vnet_feature_config_main_t *cm;
232 if (arc_index == (u8) ~ 0)
233 return VNET_API_ERROR_INVALID_VALUE;
235 if (feature_index == ~0)
236 return VNET_API_ERROR_INVALID_VALUE_2;
238 cm = &fm->feature_config_mains[arc_index];
239 vec_validate_init_empty (cm->config_index_by_sw_if_index, sw_if_index, ~0);
240 ci = cm->config_index_by_sw_if_index[sw_if_index];
242 vec_validate (fm->feature_count_by_sw_if_index[arc_index], sw_if_index);
243 feature_count = fm->feature_count_by_sw_if_index[arc_index][sw_if_index];
245 if (!enable_disable && feature_count < 1)
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);
257 cm->config_index_by_sw_if_index[sw_if_index] = ci;
259 /* update feature count */
260 enable_disable = (enable_disable > 0);
261 feature_count += enable_disable ? 1 : -1;
262 ASSERT (feature_count >= 0);
264 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));
267 adj_feature_update (sw_if_index, arc_index, (feature_count > 0));
269 fm->feature_count_by_sw_if_index[arc_index][sw_if_index] = feature_count;
274 vnet_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)
281 arc_index = vnet_get_feature_arc_index (arc_name);
283 if (arc_index == (u8) ~ 0)
284 return VNET_API_ERROR_INVALID_VALUE;
286 feature_index = vnet_get_feature_index (arc_index, node_name);
288 return vnet_feature_enable_disable_with_index (arc_index, feature_index,
289 sw_if_index, enable_disable,
291 n_feature_config_bytes);
295 feature_cmp (void *a1, void *a2)
297 vnet_feature_registration_t *reg1 = a1;
298 vnet_feature_registration_t *reg2 = a2;
300 return (int) reg1->feature_index - reg2->feature_index;
303 /** Display the set of available driver features.
304 Useful for verifying that expected features are present
307 static clib_error_t *
308 show_features_command_fn (vlib_main_t * vm,
309 unformat_input_t * input, vlib_cli_command_t * cmd)
311 vnet_feature_main_t *fm = &feature_main;
312 vnet_feature_arc_registration_t *areg;
313 vnet_feature_registration_t *freg;
314 vnet_feature_registration_t *feature_regs = 0;
317 if (unformat (input, "verbose"))
320 vlib_cli_output (vm, "Available feature paths");
326 vlib_cli_output (vm, "[%2d] %s:", areg->feature_arc_index,
329 vlib_cli_output (vm, "%s:", areg->arc_name);
331 freg = fm->next_feature_by_arc[areg->feature_arc_index];
334 vec_add1 (feature_regs, freg[0]);
335 freg = freg->next_in_arc;
338 vec_sort_with_function (feature_regs, feature_cmp);
340 vec_foreach (freg, feature_regs)
343 vlib_cli_output (vm, " [%2d]: %s\n", freg->feature_index,
346 vlib_cli_output (vm, " %s\n", freg->node_name);
348 vec_reset_length (feature_regs);
352 vec_free (feature_regs);
358 * Display the set of available driver features
362 * @cliexcmd{show features [verbose]}
367 VLIB_CLI_COMMAND (show_features_command, static) = {
368 .path = "show features",
369 .short_help = "show features",
370 .function = show_features_command_fn,
374 /** Display the set of driver features configured on a specific interface
375 * Called by "show interface" handler
379 vnet_interface_features_show (vlib_main_t * vm, u32 sw_if_index, int verbose)
381 vnet_feature_main_t *fm = &feature_main;
382 u32 node_index, current_config_index;
384 vnet_feature_config_main_t *cm = fm->feature_config_mains;
385 vnet_feature_arc_registration_t *areg;
386 vnet_config_main_t *vcm;
389 vnet_config_feature_t *feat;
393 vlib_cli_output (vm, "Feature paths configured on %U...",
394 format_vnet_sw_if_index_name,
395 vnet_get_main (), sw_if_index);
400 feature_arc = areg->feature_arc_index;
401 vcm = &(cm[feature_arc].config_main);
403 vlib_cli_output (vm, "\n%s:", areg->arc_name);
406 if (NULL == cm[feature_arc].config_index_by_sw_if_index ||
407 vec_len (cm[feature_arc].config_index_by_sw_if_index) <=
410 vlib_cli_output (vm, " none configured");
414 current_config_index =
415 vec_elt (cm[feature_arc].config_index_by_sw_if_index, sw_if_index);
417 if (current_config_index == ~0)
419 vlib_cli_output (vm, " none configured");
423 ASSERT (current_config_index
424 < vec_len (vcm->config_pool_index_by_user_index));
426 cfg_index = vcm->config_pool_index_by_user_index[current_config_index];
427 cfg = pool_elt_at_index (vcm->config_pool, cfg_index);
429 for (i = 0; i < vec_len (cfg->features); i++)
431 feat = cfg->features + i;
432 node_index = feat->node_index;
433 n = vlib_get_node (vm, node_index);
435 vlib_cli_output (vm, " [%2d] %v", feat->feature_index, n->name);
437 vlib_cli_output (vm, " %v", n->name);
442 static clib_error_t *
443 set_interface_features_command_fn (vlib_main_t * vm,
444 unformat_input_t * input,
445 vlib_cli_command_t * cmd)
447 vnet_main_t *vnm = vnet_get_main ();
448 unformat_input_t _line_input, *line_input = &_line_input;
449 clib_error_t *error = 0;
452 u8 *feature_name = 0;
453 u32 sw_if_index = ~0;
456 /* Get a line of input. */
457 if (!unformat_user (input, unformat_line_input, line_input))
460 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
463 (line_input, "%U %v", unformat_vnet_sw_interface, vnm, &sw_if_index,
466 else if (unformat (line_input, "arc %v", &arc_name))
468 else if (unformat (line_input, "disable"))
472 if (feature_name && arc_name)
474 error = unformat_parse_error (line_input);
479 if (sw_if_index == ~0)
481 error = clib_error_return (0, "Interface not specified...");
485 vec_add1 (arc_name, 0);
486 vec_add1 (feature_name, 0);
488 vnet_feature_registration_t *reg;
490 vnet_get_feature_reg ((const char *) arc_name,
491 (const char *) feature_name);
494 error = clib_error_return (0, "Unknown feature...");
497 if (reg->enable_disable_cb)
498 error = reg->enable_disable_cb (sw_if_index, enable);
500 vnet_feature_enable_disable ((const char *) arc_name,
501 (const char *) feature_name, sw_if_index,
505 vec_free (feature_name);
507 unformat_free (line_input);
512 * Set feature for given interface
516 * @cliexcmd{set interface feature GigabitEthernet2/0/0 ip4_flow_classify arc ip4_unicast}
521 VLIB_CLI_COMMAND (set_interface_feature_command, static) = {
522 .path = "set interface feature",
523 .short_help = "set interface feature <intfc> <feature_name> arc <arc_name> "
525 .function = set_interface_features_command_fn,
530 * fd.io coding-style-patch-verification: ON
533 * eval: (c-set-style "gnu")