f09a894ca03ab5c3f489bf6f4f87d5ce0d3ee3e7
[vpp.git] / vnet / vnet / ip / ip_feature_registration.c
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 #include <vnet/vnet.h>
17 #include <vnet/ip/ip.h>
18 #include <vnet/mpls/mpls.h>
19
20 /** \file
21
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.
25
26     Use the topological sort result to set up vnet_config_main_t's for
27     use at runtime.
28
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.
34
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
37     definitions.
38
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,
42     <CODE><PRE>
43     \#include <vnet/ip/ip_feature_registration.h>
44     </PRE></CODE>
45
46     and add the new feature as shown:
47
48     <CODE><PRE>
49     VNET_IP4_UNICAST_FEATURE_INIT (ip4_lookup, static) =
50     {
51       .node_name = "my-ip4-unicast-feature",
52       .runs_before = ORDER_CONSTRAINTS {"ip4-lookup", 0}
53       .feature_index = &my_feature_index,
54     };
55     </PRE></CODE>
56
57     Here's the standard coding pattern to enable / disable
58     @c my-ip4-unicast-feature on an interface:
59
60     <CODE><PRE>
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];
65
66     sw_if_index = <interface-handle>
67     ci = rx_cm->config_index_by_sw_if_index[sw_if_index];
68     ci = (is_add
69           ? vnet_config_add_feature
70           : vnet_config_del_feature)
71       (vm, &rx_cm->config_main,
72        ci,
73        my_feature_index,
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;
77     </PRE></CODE>
78
79     For tx features, add this line after setting
80     <CODE><PRE>
81     tx_cm->config_index_by_sw_if_index = ci.
82     </PRE></CODE>
83
84     This maintains a
85     per-interface "at least one TX feature enabled" bitmap:
86
87     <CODE><PRE>
88     vnet_config_update_tx_feature_count (lm, tx_cm, sw_if_index, is_add);
89     </PRE></CODE>
90
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:
93
94     <CODE><PRE>
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];
97
98     Call @c vnet_get_config_data to set next0, and to advance
99     @c b0->current_config_index:
100
101     config_data0 = vnet_get_config_data (&cm->config_main,
102                                          &b0->current_config_index,
103                                          &next0,
104                                          0 / * sizeof config data * /);
105     </PRE></CODE>
106
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.
110 */
111
112 static const char *vnet_cast_names[] = VNET_CAST_NAMES;
113
114 static int
115 comma_split (u8 * s, u8 ** a, u8 ** b)
116 {
117   *a = s;
118
119   while (*s && *s != ',')
120     s++;
121
122   if (*s == ',')
123     *s = 0;
124   else
125     return 1;
126
127   *b = (u8 *) (s + 1);
128   return 0;
129 }
130
131 clib_error_t *
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)
139 {
140   uword *index_by_name;
141   uword *reg_by_index;
142   u8 **node_names = 0;
143   u8 *node_name;
144   char **these_constraints;
145   char *this_constraint_c;
146   u8 **constraints = 0;
147   u8 *constraint_tuple;
148   u8 *this_constraint;
149   u8 **orig, **closure;
150   uword *p;
151   int i, j, k;
152   u8 *a_name, *b_name;
153   int a_index, b_index;
154   int n_features;
155   u32 *result = 0;
156   vnet_ip_feature_registration_t *this_reg = 0;
157   char **feature_nodes = 0;
158   hash_pair_t *hp;
159   u8 **keys_to_delete = 0;
160
161   index_by_name = hash_create_string (0, sizeof (uword));
162   reg_by_index = hash_create (0, sizeof (uword));
163
164   this_reg = first_reg;
165
166   /* pass 1, collect feature node names, construct a before b pairs */
167   while (this_reg)
168     {
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);
171
172       hash_set_mem (index_by_name, node_name, vec_len (node_names));
173
174       vec_add1 (node_names, node_name);
175
176       these_constraints = this_reg->runs_before;
177       while (these_constraints && these_constraints[0])
178         {
179           this_constraint_c = these_constraints[0];
180
181           constraint_tuple = format (0, "%s,%s%c", node_name,
182                                      this_constraint_c, 0);
183           vec_add1 (constraints, constraint_tuple);
184           these_constraints++;
185         }
186
187       these_constraints = this_reg->runs_after;
188       while (these_constraints && these_constraints[0])
189         {
190           this_constraint_c = these_constraints[0];
191
192           constraint_tuple = format (0, "%s,%s%c",
193                                      this_constraint_c, node_name, 0);
194           vec_add1 (constraints, constraint_tuple);
195           these_constraints++;
196         }
197
198       this_reg = this_reg->next;
199     }
200
201   n_features = vec_len (node_names);
202   orig = clib_ptclosure_alloc (n_features);
203
204   for (i = 0; i < vec_len (constraints); i++)
205     {
206       this_constraint = constraints[i];
207
208       if (comma_split (this_constraint, &a_name, &b_name))
209         return clib_error_return (0, "comma_split failed!");
210
211       p = hash_get_mem (index_by_name, a_name);
212       /*
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.
217        */
218       if (p == 0)
219         return clib_error_return (0, "feature node '%s' not found", a_name);
220       a_index = p[0];
221
222       p = hash_get_mem (index_by_name, b_name);
223       if (p == 0)
224         return clib_error_return (0, "feature node '%s' not found", b_name);
225       b_index = p[0];
226
227       /* add a before b to the original set of constraints */
228       orig[a_index][b_index] = 1;
229       vec_free (this_constraint);
230     }
231
232   /* Compute the positive transitive closure of the original constraints */
233   closure = clib_ptclosure (orig);
234
235   /* Compute a partial order across feature nodes, if one exists. */
236 again:
237   for (i = 0; i < n_features; i++)
238     {
239       for (j = 0; j < n_features; j++)
240         {
241           if (closure[i][j])
242             goto item_constrained;
243         }
244       /* Item i can be output */
245       vec_add1 (result, i);
246       {
247         for (k = 0; k < n_features; k++)
248           closure[k][i] = 0;
249         /*
250          * Add a "Magic" a before a constraint.
251          * This means we'll never output it again
252          */
253         closure[i][i] = 1;
254         goto again;
255       }
256     item_constrained:
257       ;
258     }
259
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!");
263
264   /*
265    * We win.
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.
270    */
271
272   for (i = n_features - 1; i >= 0; i--)
273     {
274       p = hash_get (reg_by_index, result[i]);
275       ASSERT (p != 0);
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);
279     }
280
281   /* Set up the config infrastructure */
282   vnet_config_init (vm, vcm,
283                     feature_start_nodes,
284                     num_feature_start_nodes,
285                     feature_nodes, vec_len (feature_nodes));
286
287   /* Save a copy for show command */
288   *in_feature_nodes = feature_nodes;
289
290   /* Finally, clean up all the shit we allocated */
291   /* *INDENT-OFF* */
292   hash_foreach_pair (hp, index_by_name,
293   ({
294     vec_add1 (keys_to_delete, (u8 *)hp->key);
295   }));
296   /* *INDENT-ON* */
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);
302   vec_free (result);
303   clib_ptclosure_free (orig);
304   clib_ptclosure_free (closure);
305   return 0;
306 }
307
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")
315
316 /** Display the set of available ip features.
317     Useful for verifying that expected features are present
318 */
319
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)
324 {
325   ip4_main_t *im4 = &ip4_main;
326   ip6_main_t *im6 = &ip6_main;
327   int i;
328   char **features;
329
330   vlib_cli_output (vm, "Available IP feature nodes");
331
332 #define _(a,c,s)                                        \
333   do {                                                  \
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]);      \
338   } while(0);
339   foreach_af_cast;
340 #undef _
341
342   return 0;
343 }
344
345 /* *INDENT-OFF* */
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,
350 };
351 /* *INDENT-ON* */
352
353 /** Display the set of IP features configured on a specific interface
354  */
355
356 void
357 ip_interface_features_show (vlib_main_t * vm,
358                             const char *pname,
359                             ip_config_main_t * cm, u32 sw_if_index)
360 {
361   u32 node_index, current_config_index;
362   vnet_cast_t cast;
363   vnet_config_main_t *vcm;
364   vnet_config_t *cfg;
365   u32 cfg_index;
366   vnet_config_feature_t *feat;
367   vlib_node_t *n;
368   int i;
369
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);
373
374   for (cast = VNET_IP_RX_UNICAST_FEAT; cast < VNET_N_IP_FEAT; cast++)
375     {
376       vcm = &(cm[cast].config_main);
377
378       vlib_cli_output (vm, "\n%s %s:", pname, vnet_cast_names[cast]);
379
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)
382         {
383           vlib_cli_output (vm, "none configured");
384           continue;
385         }
386
387       current_config_index = vec_elt (cm[cast].config_index_by_sw_if_index,
388                                       sw_if_index);
389
390       ASSERT (current_config_index
391               < vec_len (vcm->config_pool_index_by_user_index));
392
393       cfg_index = vcm->config_pool_index_by_user_index[current_config_index];
394       cfg = pool_elt_at_index (vcm->config_pool, cfg_index);
395
396       for (i = 0; i < vec_len (cfg->features); i++)
397         {
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);
402         }
403     }
404 }
405
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)
410 {
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;
416
417   ip_lookup_main_t *lm;
418   u32 sw_if_index, af;
419
420   if (!unformat (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
421     return clib_error_return (0, "Interface not specified...");
422
423   vlib_cli_output (vm, "IP feature paths configured on %U...",
424                    format_vnet_sw_if_index_name, vnm, sw_if_index);
425
426   for (af = 0; af < 2; af++)
427     {
428       if (af == 0)
429         lm = lm4;
430       else
431         lm = lm6;
432
433       ip_interface_features_show (vm, (af == 0) ? "ip4" : "ip6",
434                                   lm->feature_config_mains, sw_if_index);
435     }
436
437   return 0;
438 }
439
440 /* *INDENT-OFF* */
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,
445 };
446 /* *INDENT-ON* */
447
448 /*
449  * fd.io coding-style-patch-verification: ON
450  *
451  * Local Variables:
452  * eval: (c-set-style "gnu")
453  * End:
454  */