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 ip_feature_init_cast (vlib_main_t * vm,
133 ip_config_main_t * cm,
134 vnet_config_main_t * vcm,
135 char **feature_start_nodes,
136 int num_feature_start_nodes,
137 vnet_ip_feature_registration_t * first_reg,
138 char ***in_feature_nodes)
140 uword *index_by_name;
144 char **these_constraints;
145 char *this_constraint_c;
146 u8 **constraints = 0;
147 u8 *constraint_tuple;
149 u8 **orig, **closure;
153 int a_index, b_index;
156 vnet_ip_feature_registration_t *this_reg = 0;
157 char **feature_nodes = 0;
159 u8 **keys_to_delete = 0;
161 index_by_name = hash_create_string (0, sizeof (uword));
162 reg_by_index = hash_create (0, sizeof (uword));
164 this_reg = first_reg;
166 /* pass 1, collect feature node names, construct a before b pairs */
169 node_name = format (0, "%s%c", this_reg->node_name, 0);
170 hash_set (reg_by_index, vec_len (node_names), (uword) this_reg);
172 hash_set_mem (index_by_name, node_name, vec_len (node_names));
174 vec_add1 (node_names, node_name);
176 these_constraints = this_reg->runs_before;
177 while (these_constraints && these_constraints[0])
179 this_constraint_c = these_constraints[0];
181 constraint_tuple = format (0, "%s,%s%c", node_name,
182 this_constraint_c, 0);
183 vec_add1 (constraints, constraint_tuple);
187 these_constraints = this_reg->runs_after;
188 while (these_constraints && these_constraints[0])
190 this_constraint_c = these_constraints[0];
192 constraint_tuple = format (0, "%s,%s%c",
193 this_constraint_c, node_name, 0);
194 vec_add1 (constraints, constraint_tuple);
198 this_reg = this_reg->next;
201 n_features = vec_len (node_names);
202 orig = clib_ptclosure_alloc (n_features);
204 for (i = 0; i < vec_len (constraints); i++)
206 this_constraint = constraints[i];
208 if (comma_split (this_constraint, &a_name, &b_name))
209 return clib_error_return (0, "comma_split failed!");
211 p = hash_get_mem (index_by_name, a_name);
213 * Note: the next two errors mean that the xxx_FEATURE_INIT macros are
214 * b0rked. As in: if you code "A depends on B," and you forget
215 * to define a FEATURE_INIT macro for B, you lose.
216 * Nonexistent graph nodes are tolerated.
219 return clib_error_return (0, "feature node '%s' not found", a_name);
222 p = hash_get_mem (index_by_name, b_name);
224 return clib_error_return (0, "feature node '%s' not found", b_name);
227 /* add a before b to the original set of constraints */
228 orig[a_index][b_index] = 1;
229 vec_free (this_constraint);
232 /* Compute the positive transitive closure of the original constraints */
233 closure = clib_ptclosure (orig);
235 /* Compute a partial order across feature nodes, if one exists. */
237 for (i = 0; i < n_features; i++)
239 for (j = 0; j < n_features; j++)
242 goto item_constrained;
244 /* Item i can be output */
245 vec_add1 (result, i);
247 for (k = 0; k < n_features; k++)
250 * Add a "Magic" a before a constraint.
251 * This means we'll never output it again
260 /* see if we got a partial order... */
261 if (vec_len (result) != n_features)
262 return clib_error_return (0, "%d feature_init_cast no partial order!");
266 * Bind the index variables, and output the feature node name vector
267 * using the partial order we just computed. Result is in stack
268 * order, because the entry with the fewest constraints (e.g. none)
269 * is output first, etc.
272 for (i = n_features - 1; i >= 0; i--)
274 p = hash_get (reg_by_index, result[i]);
276 this_reg = (vnet_ip_feature_registration_t *) p[0];
277 *this_reg->feature_index = n_features - (i + 1);
278 vec_add1 (feature_nodes, this_reg->node_name);
281 /* Set up the config infrastructure */
282 vnet_config_init (vm, vcm,
284 num_feature_start_nodes,
285 feature_nodes, vec_len (feature_nodes));
287 /* Save a copy for show command */
288 *in_feature_nodes = feature_nodes;
290 /* Finally, clean up all the shit we allocated */
292 hash_foreach_pair (hp, index_by_name,
294 vec_add1 (keys_to_delete, (u8 *)hp->key);
297 hash_free (index_by_name);
298 for (i = 0; i < vec_len (keys_to_delete); i++)
299 vec_free (keys_to_delete[i]);
300 vec_free (keys_to_delete);
301 hash_free (reg_by_index);
303 clib_ptclosure_free (orig);
304 clib_ptclosure_free (closure);
308 #define foreach_af_cast \
309 _(4, VNET_IP_RX_UNICAST_FEAT, "ip4 unicast") \
310 _(4, VNET_IP_RX_MULTICAST_FEAT, "ip4 multicast") \
311 _(4, VNET_IP_TX_FEAT, "ip4 output") \
312 _(6, VNET_IP_RX_UNICAST_FEAT, "ip6 unicast") \
313 _(6, VNET_IP_RX_MULTICAST_FEAT, "ip6 multicast") \
314 _(6, VNET_IP_TX_FEAT, "ip6 output")
316 /** Display the set of available ip features.
317 Useful for verifying that expected features are present
320 static clib_error_t *
321 show_ip_features_command_fn (vlib_main_t * vm,
322 unformat_input_t * input,
323 vlib_cli_command_t * cmd)
325 ip4_main_t *im4 = &ip4_main;
326 ip6_main_t *im6 = &ip6_main;
330 vlib_cli_output (vm, "Available IP feature nodes");
334 features = im##a->feature_nodes[c]; \
335 vlib_cli_output (vm, "%s:", s); \
336 for (i = 0; i < vec_len(features); i++) \
337 vlib_cli_output (vm, " %s\n", features[i]); \
346 VLIB_CLI_COMMAND (show_ip_features_command, static) = {
347 .path = "show ip features",
348 .short_help = "show ip features",
349 .function = show_ip_features_command_fn,
353 /** Display the set of IP features configured on a specific interface
357 ip_interface_features_show (vlib_main_t * vm,
359 ip_config_main_t * cm, u32 sw_if_index)
361 u32 node_index, current_config_index;
363 vnet_config_main_t *vcm;
366 vnet_config_feature_t *feat;
370 vlib_cli_output (vm, "%s feature paths configured on %U...",
371 pname, format_vnet_sw_if_index_name,
372 vnet_get_main (), sw_if_index);
374 for (cast = VNET_IP_RX_UNICAST_FEAT; cast < VNET_N_IP_FEAT; cast++)
376 vcm = &(cm[cast].config_main);
378 vlib_cli_output (vm, "\n%s %s:", pname, vnet_cast_names[cast]);
380 if (NULL == cm[cast].config_index_by_sw_if_index ||
381 vec_len (cm[cast].config_index_by_sw_if_index) < sw_if_index)
383 vlib_cli_output (vm, "none configured");
387 current_config_index = vec_elt (cm[cast].config_index_by_sw_if_index,
390 ASSERT (current_config_index
391 < vec_len (vcm->config_pool_index_by_user_index));
393 cfg_index = vcm->config_pool_index_by_user_index[current_config_index];
394 cfg = pool_elt_at_index (vcm->config_pool, cfg_index);
396 for (i = 0; i < vec_len (cfg->features); i++)
398 feat = cfg->features + i;
399 node_index = feat->node_index;
400 n = vlib_get_node (vm, node_index);
401 vlib_cli_output (vm, " %v", n->name);
406 static clib_error_t *
407 show_ip_interface_features_command_fn (vlib_main_t * vm,
408 unformat_input_t * input,
409 vlib_cli_command_t * cmd)
411 vnet_main_t *vnm = vnet_get_main ();
412 ip4_main_t *im4 = &ip4_main;
413 ip_lookup_main_t *lm4 = &im4->lookup_main;
414 ip6_main_t *im6 = &ip6_main;
415 ip_lookup_main_t *lm6 = &im6->lookup_main;
417 ip_lookup_main_t *lm;
420 if (!unformat (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
421 return clib_error_return (0, "Interface not specified...");
423 vlib_cli_output (vm, "IP feature paths configured on %U...",
424 format_vnet_sw_if_index_name, vnm, sw_if_index);
426 for (af = 0; af < 2; af++)
433 ip_interface_features_show (vm, (af == 0) ? "ip4" : "ip6",
434 lm->feature_config_mains, sw_if_index);
441 VLIB_CLI_COMMAND (show_ip_interface_features_command, static) = {
442 .path = "show ip interface features",
443 .short_help = "show ip interface features <intfc>",
444 .function = show_ip_interface_features_command_fn,
449 * fd.io coding-style-patch-verification: ON
452 * eval: (c-set-style "gnu")