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);
68 return clib_error_return (0, "Unknown feature arc '%s'",
71 areg = uword_to_pointer (p[0], vnet_feature_arc_registration_t *);
72 arc_index = areg->feature_arc_index;
75 freg->next = fm->next_feature_by_arc[arc_index];
76 fm->next_feature_by_arc[arc_index] = freg;
86 vnet_feature_config_main_t *cm;
87 vnet_config_main_t *vcm;
89 arc_index = areg->feature_arc_index;
90 cm = &fm->feature_config_mains[arc_index];
91 vcm = &cm->config_main;
92 if ((error = vnet_feature_arc_init (vm, vcm,
95 fm->next_feature_by_arc[arc_index],
96 &fm->feature_nodes[arc_index])))
101 fm->next_feature_by_name[arc_index] =
102 hash_create_string (0, sizeof (uword));
103 freg = fm->next_feature_by_arc[arc_index];
107 hash_set_mem (fm->next_feature_by_name[arc_index],
108 freg->node_name, pointer_to_uword (freg));
120 VLIB_INIT_FUNCTION (vnet_feature_init);
123 vnet_get_feature_arc_index (const char *s)
125 vnet_feature_main_t *fm = &feature_main;
126 vnet_feature_arc_registration_t *reg;
129 p = hash_get_mem (fm->arc_index_by_name, s);
133 reg = uword_to_pointer (p[0], vnet_feature_arc_registration_t *);
134 return reg->feature_arc_index;
137 vnet_feature_registration_t *
138 vnet_get_feature_reg (const char *arc_name, const char *node_name)
142 arc_index = vnet_get_feature_arc_index (arc_name);
143 if (arc_index == (u8) ~ 0)
146 vnet_feature_main_t *fm = &feature_main;
147 vnet_feature_registration_t *reg;
150 p = hash_get_mem (fm->next_feature_by_name[arc_index], node_name);
154 reg = uword_to_pointer (p[0], vnet_feature_registration_t *);
159 vnet_get_feature_index (u8 arc, const char *s)
161 vnet_feature_main_t *fm = &feature_main;
162 vnet_feature_registration_t *reg;
168 p = hash_get_mem (fm->next_feature_by_name[arc], s);
172 reg = uword_to_pointer (p[0], vnet_feature_registration_t *);
173 return reg->feature_index;
177 vnet_feature_enable_disable_with_index (u8 arc_index, u32 feature_index,
178 u32 sw_if_index, int enable_disable,
179 void *feature_config,
180 u32 n_feature_config_bytes)
182 vnet_feature_main_t *fm = &feature_main;
183 vnet_feature_config_main_t *cm;
187 if (arc_index == (u8) ~ 0)
188 return VNET_API_ERROR_INVALID_VALUE;
190 if (feature_index == ~0)
191 return VNET_API_ERROR_INVALID_VALUE_2;
193 cm = &fm->feature_config_mains[arc_index];
194 vec_validate_init_empty (cm->config_index_by_sw_if_index, sw_if_index, ~0);
195 ci = cm->config_index_by_sw_if_index[sw_if_index];
197 vec_validate (fm->feature_count_by_sw_if_index[arc_index], sw_if_index);
198 feature_count = fm->feature_count_by_sw_if_index[arc_index][sw_if_index];
200 if (!enable_disable && feature_count < 1)
204 ? vnet_config_add_feature
205 : vnet_config_del_feature)
206 (vlib_get_main (), &cm->config_main, ci, feature_index, feature_config,
207 n_feature_config_bytes);
208 cm->config_index_by_sw_if_index[sw_if_index] = ci;
210 /* update feature count */
211 enable_disable = (enable_disable > 0);
212 feature_count += enable_disable ? 1 : -1;
213 ASSERT (feature_count >= 0);
215 fm->sw_if_index_has_features[arc_index] =
216 clib_bitmap_set (fm->sw_if_index_has_features[arc_index], sw_if_index,
217 (feature_count > 0));
218 adj_feature_update (sw_if_index, arc_index, (feature_count > 0));
220 fm->feature_count_by_sw_if_index[arc_index][sw_if_index] = feature_count;
225 vnet_feature_enable_disable (const char *arc_name, const char *node_name,
226 u32 sw_if_index, int enable_disable,
227 void *feature_config, u32 n_feature_config_bytes)
232 arc_index = vnet_get_feature_arc_index (arc_name);
234 if (arc_index == (u8) ~ 0)
235 return VNET_API_ERROR_INVALID_VALUE;
237 feature_index = vnet_get_feature_index (arc_index, node_name);
239 return vnet_feature_enable_disable_with_index (arc_index, feature_index,
240 sw_if_index, enable_disable,
242 n_feature_config_bytes);
246 /** Display the set of available driver features.
247 Useful for verifying that expected features are present
250 static clib_error_t *
251 show_features_command_fn (vlib_main_t * vm,
252 unformat_input_t * input, vlib_cli_command_t * cmd)
254 vnet_feature_main_t *fm = &feature_main;
255 vnet_feature_arc_registration_t *areg;
256 vnet_feature_registration_t *freg;
258 vlib_cli_output (vm, "Available feature paths");
263 vlib_cli_output (vm, "%s:", areg->arc_name);
264 freg = fm->next_feature_by_arc[areg->feature_arc_index];
267 vlib_cli_output (vm, " %s\n", freg->node_name);
280 * Display the set of available driver features
284 * @cliexcmd{show ip features}
289 VLIB_CLI_COMMAND (show_features_command, static) = {
290 .path = "show features",
291 .short_help = "show features",
292 .function = show_features_command_fn,
296 /** Display the set of driver features configured on a specific interface
297 * Called by "show interface" handler
301 vnet_interface_features_show (vlib_main_t * vm, u32 sw_if_index)
303 vnet_feature_main_t *fm = &feature_main;
304 u32 node_index, current_config_index;
306 vnet_feature_config_main_t *cm = fm->feature_config_mains;
307 vnet_feature_arc_registration_t *areg;
308 vnet_config_main_t *vcm;
311 vnet_config_feature_t *feat;
315 vlib_cli_output (vm, "Driver feature paths configured on %U...",
316 format_vnet_sw_if_index_name,
317 vnet_get_main (), sw_if_index);
322 feature_arc = areg->feature_arc_index;
323 vcm = &(cm[feature_arc].config_main);
325 vlib_cli_output (vm, "\n%s:", areg->arc_name);
328 if (NULL == cm[feature_arc].config_index_by_sw_if_index ||
329 vec_len (cm[feature_arc].config_index_by_sw_if_index) <=
332 vlib_cli_output (vm, " none configured");
336 current_config_index =
337 vec_elt (cm[feature_arc].config_index_by_sw_if_index, sw_if_index);
339 if (current_config_index == ~0)
341 vlib_cli_output (vm, " none configured");
345 ASSERT (current_config_index
346 < vec_len (vcm->config_pool_index_by_user_index));
348 cfg_index = vcm->config_pool_index_by_user_index[current_config_index];
349 cfg = pool_elt_at_index (vcm->config_pool, cfg_index);
351 for (i = 0; i < vec_len (cfg->features); i++)
353 feat = cfg->features + i;
354 node_index = feat->node_index;
355 n = vlib_get_node (vm, node_index);
356 vlib_cli_output (vm, " %v", n->name);
361 static clib_error_t *
362 set_interface_features_command_fn (vlib_main_t * vm,
363 unformat_input_t * input,
364 vlib_cli_command_t * cmd)
366 vnet_main_t *vnm = vnet_get_main ();
367 unformat_input_t _line_input, *line_input = &_line_input;
368 clib_error_t *error = 0;
371 u8 *feature_name = 0;
372 u32 sw_if_index = ~0;
375 /* Get a line of input. */
376 if (!unformat_user (input, unformat_line_input, line_input))
379 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
382 (line_input, "%U %v", unformat_vnet_sw_interface, vnm, &sw_if_index,
385 else if (unformat (line_input, "arc %v", &arc_name))
387 else if (unformat (line_input, "disable"))
391 error = unformat_parse_error (line_input);
396 if (sw_if_index == ~0)
398 error = clib_error_return (0, "Interface not specified...");
402 vec_add1 (arc_name, 0);
403 vec_add1 (feature_name, 0);
405 vnet_feature_registration_t *reg;
407 vnet_get_feature_reg ((const char *) arc_name,
408 (const char *) feature_name);
411 error = clib_error_return (0, "Unknown feature...");
414 if (reg->enable_disable_cb)
415 error = reg->enable_disable_cb (sw_if_index, enable);
417 vnet_feature_enable_disable ((const char *) arc_name,
418 (const char *) feature_name, sw_if_index,
422 vec_free (feature_name);
424 unformat_free (line_input);
429 * Set feature for given interface
433 * @cliexcmd{set interface feature GigabitEthernet2/0/0 ip4_flow_classify arc ip4_unicast}
438 VLIB_CLI_COMMAND (set_interface_feature_command, static) = {
439 .path = "set interface feature",
440 .short_help = "set interface feature <intfc> <feature_name> arc <arc_name>",
441 .function = set_interface_features_command_fn,
446 * fd.io coding-style-patch-verification: ON
449 * eval: (c-set-style "gnu")