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.
113 comma_split (u8 * s, u8 ** a, u8 ** b)
117 while (*s && *s != ',')
130 ip_feature_init_cast (vlib_main_t * vm,
131 ip_config_main_t * cm,
132 vnet_config_main_t * vcm,
133 char **feature_start_nodes,
134 int num_feature_start_nodes,
135 vnet_cast_t cast, vnet_l3_packet_type_t proto)
137 uword *index_by_name;
141 char **these_constraints;
142 char *this_constraint_c;
143 u8 **constraints = 0;
144 u8 *constraint_tuple;
146 u8 **orig, **closure;
150 int a_index, b_index;
153 vnet_ip_feature_registration_t *this_reg, *first_reg = 0;
154 char **feature_nodes = 0;
156 u8 **keys_to_delete = 0;
157 ip4_main_t *im4 = &ip4_main;
158 ip6_main_t *im6 = &ip6_main;
159 mpls_main_t *mm = &mpls_main;
161 index_by_name = hash_create_string (0, sizeof (uword));
162 reg_by_index = hash_create (0, sizeof (uword));
164 if (cast == VNET_IP_RX_UNICAST_FEAT)
166 if (proto == VNET_L3_PACKET_TYPE_IP4)
167 first_reg = im4->next_uc_feature;
168 else if (proto == VNET_L3_PACKET_TYPE_IP6)
169 first_reg = im6->next_uc_feature;
170 else if (proto == VNET_L3_PACKET_TYPE_MPLS_UNICAST)
171 first_reg = mm->next_feature;
173 return clib_error_return (0,
174 "protocol %d cast %d unsupport for features",
177 else if (cast == VNET_IP_RX_MULTICAST_FEAT)
179 if (proto == VNET_L3_PACKET_TYPE_IP4)
180 first_reg = im4->next_mc_feature;
181 else if (proto == VNET_L3_PACKET_TYPE_IP6)
182 first_reg = im6->next_mc_feature;
184 return clib_error_return (0,
185 "protocol %d cast %d unsupport for features",
188 else if (cast == VNET_IP_TX_FEAT)
190 if (proto == VNET_L3_PACKET_TYPE_IP4)
191 first_reg = im4->next_tx_feature;
193 first_reg = im6->next_tx_feature;
196 this_reg = first_reg;
198 /* pass 1, collect feature node names, construct a before b pairs */
201 node_name = format (0, "%s%c", this_reg->node_name, 0);
202 hash_set (reg_by_index, vec_len (node_names), (uword) this_reg);
204 hash_set_mem (index_by_name, node_name, vec_len (node_names));
206 vec_add1 (node_names, node_name);
208 these_constraints = this_reg->runs_before;
209 while (these_constraints && these_constraints[0])
211 this_constraint_c = these_constraints[0];
213 constraint_tuple = format (0, "%s,%s%c", node_name,
214 this_constraint_c, 0);
215 vec_add1 (constraints, constraint_tuple);
219 these_constraints = this_reg->runs_after;
220 while (these_constraints && these_constraints[0])
222 this_constraint_c = these_constraints[0];
224 constraint_tuple = format (0, "%s,%s%c",
225 this_constraint_c, node_name, 0);
226 vec_add1 (constraints, constraint_tuple);
230 this_reg = this_reg->next;
233 n_features = vec_len (node_names);
234 orig = clib_ptclosure_alloc (n_features);
236 for (i = 0; i < vec_len (constraints); i++)
238 this_constraint = constraints[i];
240 if (comma_split (this_constraint, &a_name, &b_name))
241 return clib_error_return (0, "comma_split failed!");
243 p = hash_get_mem (index_by_name, a_name);
245 * Note: the next two errors mean that the xxx_FEATURE_INIT macros are
246 * b0rked. As in: if you code "A depends on B," and you forget
247 * to define a FEATURE_INIT macro for B, you lose.
248 * Nonexistent graph nodes are tolerated.
251 return clib_error_return (0, "feature node '%s' not found", a_name);
254 p = hash_get_mem (index_by_name, b_name);
256 return clib_error_return (0, "feature node '%s' not found", b_name);
259 /* add a before b to the original set of constraints */
260 orig[a_index][b_index] = 1;
261 vec_free (this_constraint);
264 /* Compute the positive transitive closure of the original constraints */
265 closure = clib_ptclosure (orig);
267 /* Compute a partial order across feature nodes, if one exists. */
269 for (i = 0; i < n_features; i++)
271 for (j = 0; j < n_features; j++)
274 goto item_constrained;
276 /* Item i can be output */
277 vec_add1 (result, i);
279 for (k = 0; k < n_features; k++)
282 * Add a "Magic" a before a constraint.
283 * This means we'll never output it again
292 /* see if we got a partial order... */
293 if (vec_len (result) != n_features)
294 return clib_error_return
295 (0, "%d feature_init_cast (cast=%d), no partial order!", proto, cast);
299 * Bind the index variables, and output the feature node name vector
300 * using the partial order we just computed. Result is in stack
301 * order, because the entry with the fewest constraints (e.g. none)
302 * is output first, etc.
305 for (i = n_features - 1; i >= 0; i--)
307 p = hash_get (reg_by_index, result[i]);
309 this_reg = (vnet_ip_feature_registration_t *) p[0];
310 *this_reg->feature_index = n_features - (i + 1);
311 vec_add1 (feature_nodes, this_reg->node_name);
314 /* Set up the config infrastructure */
315 vnet_config_init (vm, vcm,
317 num_feature_start_nodes,
318 feature_nodes, vec_len (feature_nodes));
320 /* Save a copy for show command */
321 if (proto == VNET_L3_PACKET_TYPE_IP4)
322 im4->feature_nodes[cast] = feature_nodes;
323 else if (proto == VNET_L3_PACKET_TYPE_IP6)
324 im6->feature_nodes[cast] = feature_nodes;
325 else if (proto == VNET_L3_PACKET_TYPE_MPLS_UNICAST)
326 mm->feature_nodes = feature_nodes;
328 /* Finally, clean up all the shit we allocated */
330 hash_foreach_pair (hp, index_by_name,
332 vec_add1 (keys_to_delete, (u8 *)hp->key);
335 hash_free (index_by_name);
336 for (i = 0; i < vec_len (keys_to_delete); i++)
337 vec_free (keys_to_delete[i]);
338 vec_free (keys_to_delete);
339 hash_free (reg_by_index);
341 clib_ptclosure_free (orig);
342 clib_ptclosure_free (closure);
346 #define foreach_af_cast \
347 _(4, VNET_IP_RX_UNICAST_FEAT, "ip4 unicast") \
348 _(4, VNET_IP_RX_MULTICAST_FEAT, "ip4 multicast") \
349 _(4, VNET_IP_TX_FEAT, "ip4 output") \
350 _(6, VNET_IP_RX_UNICAST_FEAT, "ip6 unicast") \
351 _(6, VNET_IP_RX_MULTICAST_FEAT, "ip6 multicast") \
352 _(6, VNET_IP_TX_FEAT, "ip6 output")
354 /** Display the set of available ip features.
355 Useful for verifying that expected features are present
358 static clib_error_t *
359 show_ip_features_command_fn (vlib_main_t * vm,
360 unformat_input_t * input,
361 vlib_cli_command_t * cmd)
363 ip4_main_t *im4 = &ip4_main;
364 ip6_main_t *im6 = &ip6_main;
368 vlib_cli_output (vm, "Available IP feature nodes");
372 features = im##a->feature_nodes[c]; \
373 vlib_cli_output (vm, "%s:", s); \
374 for (i = 0; i < vec_len(features); i++) \
375 vlib_cli_output (vm, " %s\n", features[i]); \
384 VLIB_CLI_COMMAND (show_ip_features_command, static) = {
385 .path = "show ip features",
386 .short_help = "show ip features",
387 .function = show_ip_features_command_fn,
391 /** Display the set of IP features configured on a specific interface
394 static clib_error_t *
395 show_ip_interface_features_command_fn (vlib_main_t * vm,
396 unformat_input_t * input,
397 vlib_cli_command_t * cmd)
399 vnet_main_t *vnm = vnet_get_main ();
400 ip4_main_t *im4 = &ip4_main;
401 ip_lookup_main_t *lm4 = &im4->lookup_main;
402 ip6_main_t *im6 = &ip6_main;
403 ip_lookup_main_t *lm6 = &im6->lookup_main;
405 ip_lookup_main_t *lm;
406 ip_config_main_t *cm;
407 vnet_config_main_t *vcm;
410 vnet_config_feature_t *feat;
414 u32 current_config_index;
418 if (!unformat (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
419 return clib_error_return (0, "Interface not specified...");
421 vlib_cli_output (vm, "IP feature paths configured on %U...",
422 format_vnet_sw_if_index_name, vnm, sw_if_index);
425 for (af = 0; af < 2; af++)
432 for (cast = VNET_IP_RX_UNICAST_FEAT; cast < VNET_N_IP_FEAT; cast++)
434 cm = lm->feature_config_mains + cast;
435 vcm = &cm->config_main;
437 vlib_cli_output (vm, "\nipv%s %scast:",
438 (af == 0) ? "4" : "6",
439 cast == VNET_IP_RX_UNICAST_FEAT ? "uni" : "multi");
441 current_config_index = vec_elt (cm->config_index_by_sw_if_index,
444 ASSERT (current_config_index
445 < vec_len (vcm->config_pool_index_by_user_index));
448 vcm->config_pool_index_by_user_index[current_config_index];
449 cfg = pool_elt_at_index (vcm->config_pool, cfg_index);
451 for (i = 0; i < vec_len (cfg->features); i++)
453 feat = cfg->features + i;
454 node_index = feat->node_index;
455 n = vlib_get_node (vm, node_index);
456 vlib_cli_output (vm, " %v", n->name);
465 VLIB_CLI_COMMAND (show_ip_interface_features_command, static) = {
466 .path = "show ip interface features",
467 .short_help = "show ip interface features <intfc>",
468 .function = show_ip_interface_features_command_fn,
473 * fd.io coding-style-patch-verification: ON
476 * eval: (c-set-style "gnu")