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>
18 vnet_feature_main_t feature_main;
21 vnet_feature_init (vlib_main_t * vm)
23 vnet_feature_main_t *fm = &feature_main;
24 vnet_feature_registration_t *freg;
25 vnet_feature_arc_registration_t *areg;
28 fm->arc_index_by_name = hash_create_string (0, sizeof (uword));
31 /* process feature arc registrations */
36 areg->feature_arc_index = arc_index;
37 if (areg->arc_index_ptr)
38 *areg->arc_index_ptr = arc_index;
39 hash_set_mem (fm->arc_index_by_name, areg->arc_name,
40 pointer_to_uword (areg));
42 /* process start nodes */
43 while ((s = areg->start_nodes[i]))
47 areg->n_start_nodes = i;
54 vec_validate (fm->next_feature_by_arc, arc_index - 1);
55 vec_validate (fm->feature_nodes, arc_index - 1);
56 vec_validate (fm->feature_config_mains, arc_index - 1);
57 vec_validate (fm->next_feature_by_name, arc_index - 1);
58 vec_validate (fm->sw_if_index_has_features, arc_index - 1);
59 vec_validate (fm->feature_count_by_sw_if_index, arc_index - 1);
61 freg = fm->next_feature;
64 vnet_feature_registration_t *next;
65 uword *p = hash_get_mem (fm->arc_index_by_name, freg->arc_name);
67 return clib_error_return (0, "Unknown feature arc '%s'",
70 areg = uword_to_pointer (p[0], vnet_feature_arc_registration_t *);
71 arc_index = areg->feature_arc_index;
74 freg->next = fm->next_feature_by_arc[arc_index];
75 fm->next_feature_by_arc[arc_index] = freg;
85 vnet_feature_config_main_t *cm;
86 vnet_config_main_t *vcm;
88 arc_index = areg->feature_arc_index;
89 cm = &fm->feature_config_mains[arc_index];
90 vcm = &cm->config_main;
91 if ((error = vnet_feature_arc_init (vm, vcm,
94 fm->next_feature_by_arc[arc_index],
95 &fm->feature_nodes[arc_index])))
100 fm->next_feature_by_name[arc_index] =
101 hash_create_string (0, sizeof (uword));
102 freg = fm->next_feature_by_arc[arc_index];
106 hash_set_mem (fm->next_feature_by_name[arc_index],
107 freg->node_name, pointer_to_uword (freg));
111 cm->end_feature_index =
112 vnet_get_feature_index (arc_index, areg->end_node);
122 VLIB_INIT_FUNCTION (vnet_feature_init);
125 vnet_get_feature_arc_index (const char *s)
127 vnet_feature_main_t *fm = &feature_main;
128 vnet_feature_arc_registration_t *reg;
131 p = hash_get_mem (fm->arc_index_by_name, s);
135 reg = uword_to_pointer (p[0], vnet_feature_arc_registration_t *);
136 return reg->feature_arc_index;
139 vnet_feature_registration_t *
140 vnet_get_feature_reg (const char *arc_name, const char *node_name)
144 arc_index = vnet_get_feature_arc_index (arc_name);
145 if (arc_index == (u8) ~ 0)
148 vnet_feature_main_t *fm = &feature_main;
149 vnet_feature_registration_t *reg;
152 p = hash_get_mem (fm->next_feature_by_name[arc_index], node_name);
156 reg = uword_to_pointer (p[0], vnet_feature_registration_t *);
161 vnet_get_feature_index (u8 arc, const char *s)
163 vnet_feature_main_t *fm = &feature_main;
164 vnet_feature_registration_t *reg;
170 p = hash_get_mem (fm->next_feature_by_name[arc], s);
174 reg = uword_to_pointer (p[0], vnet_feature_registration_t *);
175 return reg->feature_index;
179 vnet_feature_enable_disable_with_index (u8 arc_index, u32 feature_index,
180 u32 sw_if_index, int enable_disable,
181 void *feature_config,
182 u32 n_feature_config_bytes)
184 vnet_feature_main_t *fm = &feature_main;
185 vnet_feature_config_main_t *cm;
187 int is_first_or_last;
190 if (arc_index == (u8) ~ 0)
191 return VNET_API_ERROR_INVALID_VALUE;
193 if (feature_index == ~0)
194 return VNET_API_ERROR_INVALID_VALUE_2;
196 cm = &fm->feature_config_mains[arc_index];
197 vec_validate_init_empty (cm->config_index_by_sw_if_index, sw_if_index, ~0);
198 ci = cm->config_index_by_sw_if_index[sw_if_index];
200 vec_validate (fm->feature_count_by_sw_if_index[arc_index], sw_if_index);
201 feature_count = fm->feature_count_by_sw_if_index[arc_index][sw_if_index];
203 if (!enable_disable && feature_count < 1)
207 ? vnet_config_add_feature
208 : vnet_config_del_feature)
209 (vlib_get_main (), &cm->config_main, ci, feature_index, feature_config,
210 n_feature_config_bytes);
211 cm->config_index_by_sw_if_index[sw_if_index] = ci;
213 /* update feature count */
214 enable_disable = (enable_disable > 0);
215 feature_count += enable_disable ? 1 : -1;
216 is_first_or_last = (feature_count == enable_disable);
217 ASSERT (feature_count >= 0);
219 if (is_first_or_last && cm->end_feature_index != ~0)
221 /*register end node */
223 ? vnet_config_add_feature
224 : vnet_config_del_feature)
225 (vlib_get_main (), &cm->config_main, ci, cm->end_feature_index, 0, 0);
226 cm->config_index_by_sw_if_index[sw_if_index] = ci;
229 fm->sw_if_index_has_features[arc_index] =
230 clib_bitmap_set (fm->sw_if_index_has_features[arc_index], sw_if_index,
231 (feature_count > 0));
233 fm->feature_count_by_sw_if_index[arc_index][sw_if_index] = feature_count;
238 vnet_feature_enable_disable (const char *arc_name, const char *node_name,
239 u32 sw_if_index, int enable_disable,
240 void *feature_config, u32 n_feature_config_bytes)
245 arc_index = vnet_get_feature_arc_index (arc_name);
247 if (arc_index == (u8) ~ 0)
248 return VNET_API_ERROR_INVALID_VALUE;
250 feature_index = vnet_get_feature_index (arc_index, node_name);
252 return vnet_feature_enable_disable_with_index (arc_index, feature_index,
253 sw_if_index, enable_disable,
255 n_feature_config_bytes);
259 /** Display the set of available driver features.
260 Useful for verifying that expected features are present
263 static clib_error_t *
264 show_features_command_fn (vlib_main_t * vm,
265 unformat_input_t * input, vlib_cli_command_t * cmd)
267 vnet_feature_main_t *fm = &feature_main;
268 vnet_feature_arc_registration_t *areg;
269 vnet_feature_registration_t *freg;
271 vlib_cli_output (vm, "Available feature paths");
276 vlib_cli_output (vm, "%s:", areg->arc_name);
277 freg = fm->next_feature_by_arc[areg->feature_arc_index];
280 vlib_cli_output (vm, " %s\n", freg->node_name);
293 * Display the set of available driver features
297 * @cliexcmd{show ip features}
302 VLIB_CLI_COMMAND (show_features_command, static) = {
303 .path = "show features",
304 .short_help = "show features",
305 .function = show_features_command_fn,
309 /** Display the set of driver features configured on a specific interface
310 * Called by "show interface" handler
314 vnet_interface_features_show (vlib_main_t * vm, u32 sw_if_index)
316 vnet_feature_main_t *fm = &feature_main;
317 u32 node_index, current_config_index;
319 vnet_feature_config_main_t *cm = fm->feature_config_mains;
320 vnet_feature_arc_registration_t *areg;
321 vnet_config_main_t *vcm;
324 vnet_config_feature_t *feat;
328 vlib_cli_output (vm, "Driver feature paths configured on %U...",
329 format_vnet_sw_if_index_name,
330 vnet_get_main (), sw_if_index);
335 feature_arc = areg->feature_arc_index;
336 vcm = &(cm[feature_arc].config_main);
338 vlib_cli_output (vm, "\n%s:", areg->arc_name);
341 if (NULL == cm[feature_arc].config_index_by_sw_if_index ||
342 vec_len (cm[feature_arc].config_index_by_sw_if_index) <=
345 vlib_cli_output (vm, " none configured");
349 current_config_index =
350 vec_elt (cm[feature_arc].config_index_by_sw_if_index, sw_if_index);
352 if (current_config_index == ~0)
354 vlib_cli_output (vm, " none configured");
358 ASSERT (current_config_index
359 < vec_len (vcm->config_pool_index_by_user_index));
361 cfg_index = vcm->config_pool_index_by_user_index[current_config_index];
362 cfg = pool_elt_at_index (vcm->config_pool, cfg_index);
364 for (i = 0; i < vec_len (cfg->features); i++)
366 feat = cfg->features + i;
367 node_index = feat->node_index;
368 n = vlib_get_node (vm, node_index);
369 vlib_cli_output (vm, " %v", n->name);
374 static clib_error_t *
375 set_interface_features_command_fn (vlib_main_t * vm,
376 unformat_input_t * input,
377 vlib_cli_command_t * cmd)
379 vnet_main_t *vnm = vnet_get_main ();
380 unformat_input_t _line_input, *line_input = &_line_input;
381 clib_error_t *error = 0;
384 u8 *feature_name = 0;
385 u32 sw_if_index = ~0;
388 /* Get a line of input. */
389 if (!unformat_user (input, unformat_line_input, line_input))
392 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
395 (line_input, "%U %v", unformat_vnet_sw_interface, vnm, &sw_if_index,
398 else if (unformat (line_input, "arc %v", &arc_name))
400 else if (unformat (line_input, "disable"))
404 error = unformat_parse_error (line_input);
409 if (sw_if_index == ~0)
411 error = clib_error_return (0, "Interface not specified...");
415 vec_add1 (arc_name, 0);
416 vec_add1 (feature_name, 0);
418 vnet_feature_registration_t *reg;
420 vnet_get_feature_reg ((const char *) arc_name,
421 (const char *) feature_name);
424 error = clib_error_return (0, "Unknown feature...");
427 if (reg->enable_disable_cb)
428 error = reg->enable_disable_cb (sw_if_index, enable);
430 vnet_feature_enable_disable ((const char *) arc_name,
431 (const char *) feature_name, sw_if_index,
435 vec_free (feature_name);
441 * Set feature for given interface
445 * @cliexcmd{set interface feature GigabitEthernet2/0/0 ip4_flow_classify arc ip4_unicast}
450 VLIB_CLI_COMMAND (set_interface_feature_command, static) = {
451 .path = "set interface feature",
452 .short_help = "set interface feature <intfc> <feature_name> arc <arc_name>",
453 .function = set_interface_features_command_fn,
458 * fd.io coding-style-patch-verification: ON
461 * eval: (c-set-style "gnu")