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;
29 fm->arc_index_by_name = hash_create_string (0, sizeof (uword));
32 /* process feature arc registrations */
37 areg->feature_arc_index = arc_index;
38 if (areg->arc_index_ptr)
39 *areg->arc_index_ptr = arc_index;
40 hash_set_mem (fm->arc_index_by_name, areg->arc_name,
41 pointer_to_uword (areg));
43 /* process start nodes */
44 while ((s = areg->start_nodes[i]))
48 areg->n_start_nodes = i;
55 vec_validate (fm->next_feature_by_arc, arc_index - 1);
56 vec_validate (fm->feature_nodes, arc_index - 1);
57 vec_validate (fm->feature_config_mains, arc_index - 1);
58 vec_validate (fm->next_feature_by_name, arc_index - 1);
59 vec_validate (fm->sw_if_index_has_features, arc_index - 1);
60 vec_validate (fm->feature_count_by_sw_if_index, arc_index - 1);
62 freg = fm->next_feature;
65 vnet_feature_registration_t *next;
66 uword *p = hash_get_mem (fm->arc_index_by_name, freg->arc_name);
69 /* Don't start vpp with broken features arcs */
70 clib_warning ("Unknown feature arc '%s'", freg->arc_name);
74 areg = uword_to_pointer (p[0], vnet_feature_arc_registration_t *);
75 arc_index = areg->feature_arc_index;
78 freg->next = fm->next_feature_by_arc[arc_index];
79 fm->next_feature_by_arc[arc_index] = freg;
89 vnet_feature_config_main_t *cm;
90 vnet_config_main_t *vcm;
92 arc_index = areg->feature_arc_index;
93 cm = &fm->feature_config_mains[arc_index];
94 vcm = &cm->config_main;
95 if ((error = vnet_feature_arc_init (vm, vcm,
98 fm->next_feature_by_arc[arc_index],
99 &fm->feature_nodes[arc_index])))
101 clib_error_report (error);
105 fm->next_feature_by_name[arc_index] =
106 hash_create_string (0, sizeof (uword));
107 freg = fm->next_feature_by_arc[arc_index];
111 hash_set_mem (fm->next_feature_by_name[arc_index],
112 freg->node_name, pointer_to_uword (freg));
124 VLIB_INIT_FUNCTION (vnet_feature_init);
127 vnet_get_feature_arc_index (const char *s)
129 vnet_feature_main_t *fm = &feature_main;
130 vnet_feature_arc_registration_t *reg;
133 p = hash_get_mem (fm->arc_index_by_name, s);
137 reg = uword_to_pointer (p[0], vnet_feature_arc_registration_t *);
138 return reg->feature_arc_index;
141 vnet_feature_registration_t *
142 vnet_get_feature_reg (const char *arc_name, const char *node_name)
146 arc_index = vnet_get_feature_arc_index (arc_name);
147 if (arc_index == (u8) ~ 0)
150 vnet_feature_main_t *fm = &feature_main;
151 vnet_feature_registration_t *reg;
154 p = hash_get_mem (fm->next_feature_by_name[arc_index], node_name);
158 reg = uword_to_pointer (p[0], vnet_feature_registration_t *);
163 vnet_get_feature_index (u8 arc, const char *s)
165 vnet_feature_main_t *fm = &feature_main;
166 vnet_feature_registration_t *reg;
172 p = hash_get_mem (fm->next_feature_by_name[arc], s);
176 reg = uword_to_pointer (p[0], vnet_feature_registration_t *);
177 return reg->feature_index;
181 vnet_feature_enable_disable_with_index (u8 arc_index, u32 feature_index,
182 u32 sw_if_index, int enable_disable,
183 void *feature_config,
184 u32 n_feature_config_bytes)
186 vnet_feature_main_t *fm = &feature_main;
187 vnet_feature_config_main_t *cm;
191 if (arc_index == (u8) ~ 0)
192 return VNET_API_ERROR_INVALID_VALUE;
194 if (feature_index == ~0)
195 return VNET_API_ERROR_INVALID_VALUE_2;
197 cm = &fm->feature_config_mains[arc_index];
198 vec_validate_init_empty (cm->config_index_by_sw_if_index, sw_if_index, ~0);
199 ci = cm->config_index_by_sw_if_index[sw_if_index];
201 vec_validate (fm->feature_count_by_sw_if_index[arc_index], sw_if_index);
202 feature_count = fm->feature_count_by_sw_if_index[arc_index][sw_if_index];
204 if (!enable_disable && feature_count < 1)
208 ? vnet_config_add_feature
209 : vnet_config_del_feature)
210 (vlib_get_main (), &cm->config_main, ci, feature_index, feature_config,
211 n_feature_config_bytes);
212 cm->config_index_by_sw_if_index[sw_if_index] = ci;
214 /* update feature count */
215 enable_disable = (enable_disable > 0);
216 feature_count += enable_disable ? 1 : -1;
217 ASSERT (feature_count >= 0);
219 fm->sw_if_index_has_features[arc_index] =
220 clib_bitmap_set (fm->sw_if_index_has_features[arc_index], sw_if_index,
221 (feature_count > 0));
222 adj_feature_update (sw_if_index, arc_index, (feature_count > 0));
224 fm->feature_count_by_sw_if_index[arc_index][sw_if_index] = feature_count;
229 vnet_feature_enable_disable (const char *arc_name, const char *node_name,
230 u32 sw_if_index, int enable_disable,
231 void *feature_config, u32 n_feature_config_bytes)
236 arc_index = vnet_get_feature_arc_index (arc_name);
238 if (arc_index == (u8) ~ 0)
239 return VNET_API_ERROR_INVALID_VALUE;
241 feature_index = vnet_get_feature_index (arc_index, node_name);
243 return vnet_feature_enable_disable_with_index (arc_index, feature_index,
244 sw_if_index, enable_disable,
246 n_feature_config_bytes);
250 /** Display the set of available driver features.
251 Useful for verifying that expected features are present
254 static clib_error_t *
255 show_features_command_fn (vlib_main_t * vm,
256 unformat_input_t * input, vlib_cli_command_t * cmd)
258 vnet_feature_main_t *fm = &feature_main;
259 vnet_feature_arc_registration_t *areg;
260 vnet_feature_registration_t *freg;
262 vlib_cli_output (vm, "Available feature paths");
267 vlib_cli_output (vm, "%s:", areg->arc_name);
268 freg = fm->next_feature_by_arc[areg->feature_arc_index];
271 vlib_cli_output (vm, " %s\n", freg->node_name);
284 * Display the set of available driver features
288 * @cliexcmd{show ip features}
293 VLIB_CLI_COMMAND (show_features_command, static) = {
294 .path = "show features",
295 .short_help = "show features",
296 .function = show_features_command_fn,
300 /** Display the set of driver features configured on a specific interface
301 * Called by "show interface" handler
305 vnet_interface_features_show (vlib_main_t * vm, u32 sw_if_index)
307 vnet_feature_main_t *fm = &feature_main;
308 u32 node_index, current_config_index;
310 vnet_feature_config_main_t *cm = fm->feature_config_mains;
311 vnet_feature_arc_registration_t *areg;
312 vnet_config_main_t *vcm;
315 vnet_config_feature_t *feat;
319 vlib_cli_output (vm, "Driver feature paths configured on %U...",
320 format_vnet_sw_if_index_name,
321 vnet_get_main (), sw_if_index);
326 feature_arc = areg->feature_arc_index;
327 vcm = &(cm[feature_arc].config_main);
329 vlib_cli_output (vm, "\n%s:", areg->arc_name);
332 if (NULL == cm[feature_arc].config_index_by_sw_if_index ||
333 vec_len (cm[feature_arc].config_index_by_sw_if_index) <=
336 vlib_cli_output (vm, " none configured");
340 current_config_index =
341 vec_elt (cm[feature_arc].config_index_by_sw_if_index, sw_if_index);
343 if (current_config_index == ~0)
345 vlib_cli_output (vm, " none configured");
349 ASSERT (current_config_index
350 < vec_len (vcm->config_pool_index_by_user_index));
352 cfg_index = vcm->config_pool_index_by_user_index[current_config_index];
353 cfg = pool_elt_at_index (vcm->config_pool, cfg_index);
355 for (i = 0; i < vec_len (cfg->features); i++)
357 feat = cfg->features + i;
358 node_index = feat->node_index;
359 n = vlib_get_node (vm, node_index);
360 vlib_cli_output (vm, " %v", n->name);
365 static clib_error_t *
366 set_interface_features_command_fn (vlib_main_t * vm,
367 unformat_input_t * input,
368 vlib_cli_command_t * cmd)
370 vnet_main_t *vnm = vnet_get_main ();
371 unformat_input_t _line_input, *line_input = &_line_input;
372 clib_error_t *error = 0;
375 u8 *feature_name = 0;
376 u32 sw_if_index = ~0;
379 /* Get a line of input. */
380 if (!unformat_user (input, unformat_line_input, line_input))
383 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
386 (line_input, "%U %v", unformat_vnet_sw_interface, vnm, &sw_if_index,
389 else if (unformat (line_input, "arc %v", &arc_name))
391 else if (unformat (line_input, "disable"))
395 error = unformat_parse_error (line_input);
400 if (sw_if_index == ~0)
402 error = clib_error_return (0, "Interface not specified...");
406 vec_add1 (arc_name, 0);
407 vec_add1 (feature_name, 0);
409 vnet_feature_registration_t *reg;
411 vnet_get_feature_reg ((const char *) arc_name,
412 (const char *) feature_name);
415 error = clib_error_return (0, "Unknown feature...");
418 if (reg->enable_disable_cb)
419 error = reg->enable_disable_cb (sw_if_index, enable);
421 vnet_feature_enable_disable ((const char *) arc_name,
422 (const char *) feature_name, sw_if_index,
426 vec_free (feature_name);
428 unformat_free (line_input);
433 * Set feature for given interface
437 * @cliexcmd{set interface feature GigabitEthernet2/0/0 ip4_flow_classify arc ip4_unicast}
442 VLIB_CLI_COMMAND (set_interface_feature_command, static) = {
443 .path = "set interface feature",
444 .short_help = "set interface feature <intfc> <feature_name> arc <arc_name> "
446 .function = set_interface_features_command_fn,
451 * fd.io coding-style-patch-verification: ON
454 * eval: (c-set-style "gnu")