2 * Copyright (c) 2015 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/vnet.h>
17 #include <vnet/ip/ip.h>
18 #include <vnet/mpls/mpls.h>
22 Dynamically compute IP feature subgraph ordering by performing a
23 topological sort across a set of "feature A before feature B" and
24 "feature C after feature B" constraints.
26 Use the topological sort result to set up vnet_config_main_t's for
29 Feature subgraph arcs are simple enough. They start at specific
30 fixed nodes, and end at specific fixed nodes. In between, a
31 per-interface current feature configuration dictates which
32 additional nodes each packet visits. Each so-called feature node
33 can [of course] drop any specific packet.
35 See ip4_forward.c, ip6_forward.c in this directory to see the
36 current rx-unicast, rx-multicast, and tx feature subgraph arc
39 Let's say that we wish to add a new feature to the ip4 unicast
40 feature subgraph arc, which needs to run before @c ip4-lookup. In
41 either base code or a plugin,
43 \#include <vnet/ip/ip_feature_registration.h>
46 and add the new feature as shown:
49 VNET_IP4_UNICAST_FEATURE_INIT (ip4_lookup, static) =
51 .node_name = "my-ip4-unicast-feature",
52 .runs_before = ORDER_CONSTRAINTS {"ip4-lookup", 0}
53 .feature_index = &my_feature_index,
57 Here's the standard coding pattern to enable / disable
58 @c my-ip4-unicast-feature on an interface:
61 ip4_main_t *im = \&ip4_main;
62 ip_lookup_main_t *lm = &im->lookup_main;
63 ip_config_main_t *rx_cm =
64 &lm->feature_config_mains[VNET_IP_RX_UNICAST_FEAT];
66 sw_if_index = <interface-handle>
67 ci = rx_cm->config_index_by_sw_if_index[sw_if_index];
69 ? vnet_config_add_feature
70 : vnet_config_del_feature)
71 (vm, &rx_cm->config_main,
74 0 / * &config struct if feature uses private config data * /,
75 0 / * sizeof config struct if feature uses private config data * /);
76 rx_cm->config_index_by_sw_if_index[sw_if_index] = ci;
79 For tx features, add this line after setting
81 tx_cm->config_index_by_sw_if_index = ci.
85 per-interface "at least one TX feature enabled" bitmap:
88 vnet_config_update_tx_feature_count (lm, tx_cm, sw_if_index, is_add);
91 Here's how to obtain the correct next node index in packet
92 processing code, aka in the implementation of @c my-ip4-unicast-feature:
95 ip_lookup_main_t * lm = sm->ip4_lookup_main;
96 ip_config_main_t * cm = &lm->feature_config_mains[VNET_IP_RX_UNICAST_FEAT];
98 Call @c vnet_get_config_data to set next0, and to advance
99 @c b0->current_config_index:
101 config_data0 = vnet_get_config_data (&cm->config_main,
102 &b0->current_config_index,
104 0 / * sizeof config data * /);
107 Nodes are free to drop or otherwise redirect packets. Packets
108 which "pass" should be enqueued via the next0 arc computed by
109 vnet_get_config_data.
112 static const char *vnet_cast_names[] = VNET_CAST_NAMES;
115 comma_split (u8 * s, u8 ** a, u8 ** b)
119 while (*s && *s != ',')
132 * @brief Initialize a feature graph arc
133 * @param vm vlib main structure pointer
134 * @param vcm vnet config main structure pointer
135 * @param feature_start_nodes names of start-nodes which use this
137 * @param num_feature_start_nodes number of start-nodes
138 * @param first_reg first element in
139 * [an __attribute__((constructor)) function built, or
140 * otherwise created] singly-linked list of feature registrations
141 * @param [out] in_feature_nodes returned vector of
142 * topologically-sorted feature node names, for use in
144 * @returns 0 on success, otherwise an error message. Errors
145 * are fatal since they invariably involve mistyped node-names, or
146 * genuinely missing node-names
149 vnet_feature_arc_init (vlib_main_t * vm,
150 vnet_config_main_t * vcm,
151 char **feature_start_nodes,
152 int num_feature_start_nodes,
153 vnet_ip_feature_registration_t * first_reg,
154 char ***in_feature_nodes)
156 uword *index_by_name;
160 char **these_constraints;
161 char *this_constraint_c;
162 u8 **constraints = 0;
163 u8 *constraint_tuple;
165 u8 **orig, **closure;
169 int a_index, b_index;
172 vnet_ip_feature_registration_t *this_reg = 0;
173 char **feature_nodes = 0;
175 u8 **keys_to_delete = 0;
177 index_by_name = hash_create_string (0, sizeof (uword));
178 reg_by_index = hash_create (0, sizeof (uword));
180 this_reg = first_reg;
182 /* pass 1, collect feature node names, construct a before b pairs */
185 node_name = format (0, "%s%c", this_reg->node_name, 0);
186 hash_set (reg_by_index, vec_len (node_names), (uword) this_reg);
188 hash_set_mem (index_by_name, node_name, vec_len (node_names));
190 vec_add1 (node_names, node_name);
192 these_constraints = this_reg->runs_before;
193 while (these_constraints && these_constraints[0])
195 this_constraint_c = these_constraints[0];
197 constraint_tuple = format (0, "%s,%s%c", node_name,
198 this_constraint_c, 0);
199 vec_add1 (constraints, constraint_tuple);
203 these_constraints = this_reg->runs_after;
204 while (these_constraints && these_constraints[0])
206 this_constraint_c = these_constraints[0];
208 constraint_tuple = format (0, "%s,%s%c",
209 this_constraint_c, node_name, 0);
210 vec_add1 (constraints, constraint_tuple);
214 this_reg = this_reg->next;
217 n_features = vec_len (node_names);
218 orig = clib_ptclosure_alloc (n_features);
220 for (i = 0; i < vec_len (constraints); i++)
222 this_constraint = constraints[i];
224 if (comma_split (this_constraint, &a_name, &b_name))
225 return clib_error_return (0, "comma_split failed!");
227 p = hash_get_mem (index_by_name, a_name);
229 * Note: the next two errors mean that the xxx_FEATURE_INIT macros are
230 * b0rked. As in: if you code "A depends on B," and you forget
231 * to define a FEATURE_INIT macro for B, you lose.
232 * Nonexistent graph nodes are tolerated.
235 return clib_error_return (0, "feature node '%s' not found", a_name);
238 p = hash_get_mem (index_by_name, b_name);
240 return clib_error_return (0, "feature node '%s' not found", b_name);
243 /* add a before b to the original set of constraints */
244 orig[a_index][b_index] = 1;
245 vec_free (this_constraint);
248 /* Compute the positive transitive closure of the original constraints */
249 closure = clib_ptclosure (orig);
251 /* Compute a partial order across feature nodes, if one exists. */
253 for (i = 0; i < n_features; i++)
255 for (j = 0; j < n_features; j++)
258 goto item_constrained;
260 /* Item i can be output */
261 vec_add1 (result, i);
263 for (k = 0; k < n_features; k++)
266 * Add a "Magic" a before a constraint.
267 * This means we'll never output it again
276 /* see if we got a partial order... */
277 if (vec_len (result) != n_features)
278 return clib_error_return (0, "%d feature_init_cast no partial order!");
282 * Bind the index variables, and output the feature node name vector
283 * using the partial order we just computed. Result is in stack
284 * order, because the entry with the fewest constraints (e.g. none)
285 * is output first, etc.
288 for (i = n_features - 1; i >= 0; i--)
290 p = hash_get (reg_by_index, result[i]);
292 this_reg = (vnet_ip_feature_registration_t *) p[0];
293 *this_reg->feature_index = n_features - (i + 1);
294 vec_add1 (feature_nodes, this_reg->node_name);
297 /* Set up the config infrastructure */
298 vnet_config_init (vm, vcm,
300 num_feature_start_nodes,
301 feature_nodes, vec_len (feature_nodes));
303 /* Save a copy for show command */
304 *in_feature_nodes = feature_nodes;
306 /* Finally, clean up all the shit we allocated */
308 hash_foreach_pair (hp, index_by_name,
310 vec_add1 (keys_to_delete, (u8 *)hp->key);
313 hash_free (index_by_name);
314 for (i = 0; i < vec_len (keys_to_delete); i++)
315 vec_free (keys_to_delete[i]);
316 vec_free (keys_to_delete);
317 hash_free (reg_by_index);
319 clib_ptclosure_free (orig);
320 clib_ptclosure_free (closure);
324 #define foreach_af_cast \
325 _(4, VNET_IP_RX_UNICAST_FEAT, "ip4 unicast") \
326 _(4, VNET_IP_RX_MULTICAST_FEAT, "ip4 multicast") \
327 _(4, VNET_IP_TX_FEAT, "ip4 output") \
328 _(6, VNET_IP_RX_UNICAST_FEAT, "ip6 unicast") \
329 _(6, VNET_IP_RX_MULTICAST_FEAT, "ip6 multicast") \
330 _(6, VNET_IP_TX_FEAT, "ip6 output")
332 /** Display the set of available ip features.
333 Useful for verifying that expected features are present
336 static clib_error_t *
337 show_ip_features_command_fn (vlib_main_t * vm,
338 unformat_input_t * input,
339 vlib_cli_command_t * cmd)
341 ip4_main_t *im4 = &ip4_main;
342 ip6_main_t *im6 = &ip6_main;
346 vlib_cli_output (vm, "Available IP feature nodes");
350 features = im##a->feature_nodes[c]; \
351 vlib_cli_output (vm, "%s:", s); \
352 for (i = 0; i < vec_len(features); i++) \
353 vlib_cli_output (vm, " %s\n", features[i]); \
362 * Display the set of available ip features
366 * @cliexcmd{show ip features}
371 VLIB_CLI_COMMAND (show_ip_features_command, static) = {
372 .path = "show ip features",
373 .short_help = "show ip features",
374 .function = show_ip_features_command_fn,
378 /** Display the set of IP features configured on a specific interface
382 ip_interface_features_show (vlib_main_t * vm,
384 ip_config_main_t * cm, u32 sw_if_index)
386 u32 node_index, current_config_index;
388 vnet_config_main_t *vcm;
391 vnet_config_feature_t *feat;
395 vlib_cli_output (vm, "%s feature paths configured on %U...",
396 pname, format_vnet_sw_if_index_name,
397 vnet_get_main (), sw_if_index);
399 for (cast = VNET_IP_RX_UNICAST_FEAT; cast < VNET_N_IP_FEAT; cast++)
401 vcm = &(cm[cast].config_main);
403 vlib_cli_output (vm, "\n%s %s:", pname, vnet_cast_names[cast]);
405 if (NULL == cm[cast].config_index_by_sw_if_index ||
406 vec_len (cm[cast].config_index_by_sw_if_index) < sw_if_index)
408 vlib_cli_output (vm, "none configured");
412 current_config_index = vec_elt (cm[cast].config_index_by_sw_if_index,
415 ASSERT (current_config_index
416 < vec_len (vcm->config_pool_index_by_user_index));
418 cfg_index = vcm->config_pool_index_by_user_index[current_config_index];
419 cfg = pool_elt_at_index (vcm->config_pool, cfg_index);
421 for (i = 0; i < vec_len (cfg->features); i++)
423 feat = cfg->features + i;
424 node_index = feat->node_index;
425 n = vlib_get_node (vm, node_index);
426 vlib_cli_output (vm, " %v", n->name);
431 static clib_error_t *
432 show_ip_interface_features_command_fn (vlib_main_t * vm,
433 unformat_input_t * input,
434 vlib_cli_command_t * cmd)
436 vnet_main_t *vnm = vnet_get_main ();
437 ip4_main_t *im4 = &ip4_main;
438 ip_lookup_main_t *lm4 = &im4->lookup_main;
439 ip6_main_t *im6 = &ip6_main;
440 ip_lookup_main_t *lm6 = &im6->lookup_main;
442 ip_lookup_main_t *lm;
445 if (!unformat (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
446 return clib_error_return (0, "Interface not specified...");
448 vlib_cli_output (vm, "IP feature paths configured on %U...",
449 format_vnet_sw_if_index_name, vnm, sw_if_index);
451 for (af = 0; af < 2; af++)
458 ip_interface_features_show (vm, (af == 0) ? "ip4" : "ip6",
459 lm->feature_config_mains, sw_if_index);
466 * Display the ip features configured on a specific interface
470 * @cliexcmd{show ip interface features GigabitEthernet2/0/0}
475 VLIB_CLI_COMMAND (show_ip_interface_features_command, static) = {
476 .path = "show ip interface features",
477 .short_help = "show ip interface features <intfc>",
478 .function = show_ip_interface_features_command_fn,
483 * fd.io coding-style-patch-verification: ON
486 * eval: (c-set-style "gnu")