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>
21 Dynamically compute IP feature subgraph ordering by performing a
22 topological sort across a set of "feature A before feature B" and
23 "feature C after feature B" constraints.
25 Use the topological sort result to set up vnet_config_main_t's for
28 Feature subgraph arcs are simple enough. They start at specific
29 fixed nodes, and end at specific fixed nodes. In between, a
30 per-interface current feature configuration dictates which
31 additional nodes each packet visits. Each so-called feature node
32 can [of course] drop any specific packet.
34 See ip4_forward.c, ip6_forward.c in this directory to see the
35 current rx-unicast, rx-multicast, and tx feature subgraph arc
38 Let's say that we wish to add a new feature to the ip4 unicast
39 feature subgraph arc, which needs to run before @c ip4-lookup. In
40 either base code or a plugin,
42 \#include <vnet/ip/ip_feature_registration.h>
45 and add the new feature as shown:
48 VNET_IP4_UNICAST_FEATURE_INIT (ip4_lookup, static) =
50 .node_name = "my-ip4-unicast-feature",
51 .runs_before = ORDER_CONSTRAINTS {"ip4-lookup", 0}
52 .feature_index = &my_feature_index,
56 Here's the standard coding pattern to enable / disable
57 @c my-ip4-unicast-feature on an interface:
60 ip4_main_t *im = \&ip4_main;
61 ip_lookup_main_t *lm = &im->lookup_main;
62 ip_config_main_t *rx_cm =
63 &lm->feature_config_mains[VNET_IP_RX_UNICAST_FEAT];
65 sw_if_index = <interface-handle>
66 ci = rx_cm->config_index_by_sw_if_index[sw_if_index];
68 ? vnet_config_add_feature
69 : vnet_config_del_feature)
70 (vm, &rx_cm->config_main,
73 0 / * &config struct if feature uses private config data * /,
74 0 / * sizeof config struct if feature uses private config data * /);
75 rx_cm->config_index_by_sw_if_index[sw_if_index] = ci;
78 For tx features, add this line after setting
80 tx_cm->config_index_by_sw_if_index = ci.
84 per-interface "at least one TX feature enabled" bitmap:
87 vnet_config_update_tx_feature_count (lm, tx_cm, sw_if_index, is_add);
90 Here's how to obtain the correct next node index in packet
91 processing code, aka in the implementation of @c my-ip4-unicast-feature:
94 ip_lookup_main_t * lm = sm->ip4_lookup_main;
95 ip_config_main_t * cm = &lm->feature_config_mains[VNET_IP_RX_UNICAST_FEAT];
97 Call @c vnet_get_config_data to set next0, and to advance
98 @c b0->current_config_index:
100 config_data0 = vnet_get_config_data (&cm->config_main,
101 &b0->current_config_index,
103 0 / * sizeof config data * /);
106 Nodes are free to drop or otherwise redirect packets. Packets
107 which "pass" should be enqueued via the next0 arc computed by
108 vnet_get_config_data.
112 comma_split (u8 * s, u8 ** a, u8 ** b)
116 while (*s && *s != ',')
129 ip_feature_init_cast (vlib_main_t * vm,
130 ip_config_main_t * cm,
131 vnet_config_main_t * vcm,
132 char **feature_start_nodes,
133 int num_feature_start_nodes,
134 vnet_cast_t cast, int is_ip4)
136 uword *index_by_name;
140 char **these_constraints;
141 char *this_constraint_c;
142 u8 **constraints = 0;
143 u8 *constraint_tuple;
145 u8 **orig, **closure;
149 int a_index, b_index;
152 vnet_ip_feature_registration_t *this_reg, *first_reg = 0;
153 char **feature_nodes = 0;
155 u8 **keys_to_delete = 0;
156 ip4_main_t *im4 = &ip4_main;
157 ip6_main_t *im6 = &ip6_main;
159 index_by_name = hash_create_string (0, sizeof (uword));
160 reg_by_index = hash_create (0, sizeof (uword));
162 if (cast == VNET_IP_RX_UNICAST_FEAT)
165 first_reg = im4->next_uc_feature;
167 first_reg = im6->next_uc_feature;
169 else if (cast == VNET_IP_RX_MULTICAST_FEAT)
172 first_reg = im4->next_mc_feature;
174 first_reg = im6->next_mc_feature;
176 else if (cast == VNET_IP_TX_FEAT)
179 first_reg = im4->next_tx_feature;
181 first_reg = im6->next_tx_feature;
185 this_reg = first_reg;
187 /* pass 1, collect feature node names, construct a before b pairs */
190 node_name = format (0, "%s%c", this_reg->node_name, 0);
191 hash_set (reg_by_index, vec_len (node_names), (uword) this_reg);
193 hash_set_mem (index_by_name, node_name, vec_len (node_names));
195 vec_add1 (node_names, node_name);
197 these_constraints = this_reg->runs_before;
198 while (these_constraints && these_constraints[0])
200 this_constraint_c = these_constraints[0];
202 constraint_tuple = format (0, "%s,%s%c", node_name,
203 this_constraint_c, 0);
204 vec_add1 (constraints, constraint_tuple);
208 these_constraints = this_reg->runs_after;
209 while (these_constraints && these_constraints[0])
211 this_constraint_c = these_constraints[0];
213 constraint_tuple = format (0, "%s,%s%c",
214 this_constraint_c, node_name, 0);
215 vec_add1 (constraints, constraint_tuple);
219 this_reg = this_reg->next;
222 n_features = vec_len (node_names);
223 orig = clib_ptclosure_alloc (n_features);
225 for (i = 0; i < vec_len (constraints); i++)
227 this_constraint = constraints[i];
229 if (comma_split (this_constraint, &a_name, &b_name))
230 return clib_error_return (0, "comma_split failed!");
232 p = hash_get_mem (index_by_name, a_name);
234 * Note: the next two errors mean that the xxx_FEATURE_INIT macros are
235 * b0rked. As in: if you code "A depends on B," and you forget
236 * to define a FEATURE_INIT macro for B, you lose.
237 * Nonexistent graph nodes are tolerated.
240 return clib_error_return (0, "feature node '%s' not found", a_name);
243 p = hash_get_mem (index_by_name, b_name);
245 return clib_error_return (0, "feature node '%s' not found", b_name);
248 /* add a before b to the original set of constraints */
249 orig[a_index][b_index] = 1;
250 vec_free (this_constraint);
253 /* Compute the positive transitive closure of the original constraints */
254 closure = clib_ptclosure (orig);
256 /* Compute a partial order across feature nodes, if one exists. */
258 for (i = 0; i < n_features; i++)
260 for (j = 0; j < n_features; j++)
263 goto item_constrained;
265 /* Item i can be output */
266 vec_add1 (result, i);
268 for (k = 0; k < n_features; k++)
271 * Add a "Magic" a before a constraint.
272 * This means we'll never output it again
281 /* see if we got a partial order... */
282 if (vec_len (result) != n_features)
283 return clib_error_return
284 (0, "ip%s_feature_init_cast (cast=%d), no partial order!",
285 is_ip4 ? "4" : "6", cast);
289 * Bind the index variables, and output the feature node name vector
290 * using the partial order we just computed. Result is in stack
291 * order, because the entry with the fewest constraints (e.g. none)
292 * is output first, etc.
295 for (i = n_features - 1; i >= 0; i--)
297 p = hash_get (reg_by_index, result[i]);
299 this_reg = (vnet_ip_feature_registration_t *) p[0];
300 *this_reg->feature_index = n_features - (i + 1);
301 vec_add1 (feature_nodes, this_reg->node_name);
304 /* Set up the config infrastructure */
305 vnet_config_init (vm, vcm,
307 num_feature_start_nodes,
308 feature_nodes, vec_len (feature_nodes));
310 /* Save a copy for show command */
312 im4->feature_nodes[cast] = feature_nodes;
314 im6->feature_nodes[cast] = feature_nodes;
316 /* Finally, clean up all the shit we allocated */
318 hash_foreach_pair (hp, index_by_name,
320 vec_add1 (keys_to_delete, (u8 *)hp->key);
323 hash_free (index_by_name);
324 for (i = 0; i < vec_len (keys_to_delete); i++)
325 vec_free (keys_to_delete[i]);
326 vec_free (keys_to_delete);
327 hash_free (reg_by_index);
329 clib_ptclosure_free (orig);
330 clib_ptclosure_free (closure);
334 #define foreach_af_cast \
335 _(4, VNET_IP_RX_UNICAST_FEAT, "ip4 unicast") \
336 _(4, VNET_IP_RX_MULTICAST_FEAT, "ip4 multicast") \
337 _(4, VNET_IP_TX_FEAT, "ip4 output") \
338 _(6, VNET_IP_RX_UNICAST_FEAT, "ip6 unicast") \
339 _(6, VNET_IP_RX_MULTICAST_FEAT, "ip6 multicast") \
340 _(6, VNET_IP_TX_FEAT, "ip6 output")
342 /** Display the set of available ip features.
343 Useful for verifying that expected features are present
346 static clib_error_t *
347 show_ip_features_command_fn (vlib_main_t * vm,
348 unformat_input_t * input,
349 vlib_cli_command_t * cmd)
351 ip4_main_t *im4 = &ip4_main;
352 ip6_main_t *im6 = &ip6_main;
356 vlib_cli_output (vm, "Available IP feature nodes");
360 features = im##a->feature_nodes[c]; \
361 vlib_cli_output (vm, "%s:", s); \
362 for (i = 0; i < vec_len(features); i++) \
363 vlib_cli_output (vm, " %s\n", features[i]); \
372 VLIB_CLI_COMMAND (show_ip_features_command, static) = {
373 .path = "show ip features",
374 .short_help = "show ip features",
375 .function = show_ip_features_command_fn,
379 /** Display the set of IP features configured on a specific interface
382 static clib_error_t *
383 show_ip_interface_features_command_fn (vlib_main_t * vm,
384 unformat_input_t * input,
385 vlib_cli_command_t * cmd)
387 vnet_main_t *vnm = vnet_get_main ();
388 ip4_main_t *im4 = &ip4_main;
389 ip_lookup_main_t *lm4 = &im4->lookup_main;
390 ip6_main_t *im6 = &ip6_main;
391 ip_lookup_main_t *lm6 = &im6->lookup_main;
393 ip_lookup_main_t *lm;
394 ip_config_main_t *cm;
395 vnet_config_main_t *vcm;
398 vnet_config_feature_t *feat;
402 u32 current_config_index;
406 if (!unformat (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
407 return clib_error_return (0, "Interface not specified...");
409 vlib_cli_output (vm, "IP feature paths configured on %U...",
410 format_vnet_sw_if_index_name, vnm, sw_if_index);
413 for (af = 0; af < 2; af++)
420 for (cast = VNET_IP_RX_UNICAST_FEAT; cast < VNET_N_IP_FEAT; cast++)
422 cm = lm->feature_config_mains + cast;
423 vcm = &cm->config_main;
425 vlib_cli_output (vm, "\nipv%s %scast:",
426 (af == 0) ? "4" : "6",
427 cast == VNET_IP_RX_UNICAST_FEAT ? "uni" : "multi");
429 current_config_index = vec_elt (cm->config_index_by_sw_if_index,
432 ASSERT (current_config_index
433 < vec_len (vcm->config_pool_index_by_user_index));
436 vcm->config_pool_index_by_user_index[current_config_index];
437 cfg = pool_elt_at_index (vcm->config_pool, cfg_index);
439 for (i = 0; i < vec_len (cfg->features); i++)
441 feat = cfg->features + i;
442 node_index = feat->node_index;
443 n = vlib_get_node (vm, node_index);
444 vlib_cli_output (vm, " %v", n->name);
453 VLIB_CLI_COMMAND (show_ip_interface_features_command, static) = {
454 .path = "show ip interface features",
455 .short_help = "show ip interface features <intfc>",
456 .function = show_ip_interface_features_command_fn,
461 * fd.io coding-style-patch-verification: ON
464 * eval: (c-set-style "gnu")