Rename ip_feature_init_cast(...) to vnet_feature_arc_init(...), VPP-481
[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 /**
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
136  *        feature graph arc
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
143  *        show commands
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
147  */
148 clib_error_t *
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)
155 {
156   uword *index_by_name;
157   uword *reg_by_index;
158   u8 **node_names = 0;
159   u8 *node_name;
160   char **these_constraints;
161   char *this_constraint_c;
162   u8 **constraints = 0;
163   u8 *constraint_tuple;
164   u8 *this_constraint;
165   u8 **orig, **closure;
166   uword *p;
167   int i, j, k;
168   u8 *a_name, *b_name;
169   int a_index, b_index;
170   int n_features;
171   u32 *result = 0;
172   vnet_ip_feature_registration_t *this_reg = 0;
173   char **feature_nodes = 0;
174   hash_pair_t *hp;
175   u8 **keys_to_delete = 0;
176
177   index_by_name = hash_create_string (0, sizeof (uword));
178   reg_by_index = hash_create (0, sizeof (uword));
179
180   this_reg = first_reg;
181
182   /* pass 1, collect feature node names, construct a before b pairs */
183   while (this_reg)
184     {
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);
187
188       hash_set_mem (index_by_name, node_name, vec_len (node_names));
189
190       vec_add1 (node_names, node_name);
191
192       these_constraints = this_reg->runs_before;
193       while (these_constraints && these_constraints[0])
194         {
195           this_constraint_c = these_constraints[0];
196
197           constraint_tuple = format (0, "%s,%s%c", node_name,
198                                      this_constraint_c, 0);
199           vec_add1 (constraints, constraint_tuple);
200           these_constraints++;
201         }
202
203       these_constraints = this_reg->runs_after;
204       while (these_constraints && these_constraints[0])
205         {
206           this_constraint_c = these_constraints[0];
207
208           constraint_tuple = format (0, "%s,%s%c",
209                                      this_constraint_c, node_name, 0);
210           vec_add1 (constraints, constraint_tuple);
211           these_constraints++;
212         }
213
214       this_reg = this_reg->next;
215     }
216
217   n_features = vec_len (node_names);
218   orig = clib_ptclosure_alloc (n_features);
219
220   for (i = 0; i < vec_len (constraints); i++)
221     {
222       this_constraint = constraints[i];
223
224       if (comma_split (this_constraint, &a_name, &b_name))
225         return clib_error_return (0, "comma_split failed!");
226
227       p = hash_get_mem (index_by_name, a_name);
228       /*
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.
233        */
234       if (p == 0)
235         return clib_error_return (0, "feature node '%s' not found", a_name);
236       a_index = p[0];
237
238       p = hash_get_mem (index_by_name, b_name);
239       if (p == 0)
240         return clib_error_return (0, "feature node '%s' not found", b_name);
241       b_index = p[0];
242
243       /* add a before b to the original set of constraints */
244       orig[a_index][b_index] = 1;
245       vec_free (this_constraint);
246     }
247
248   /* Compute the positive transitive closure of the original constraints */
249   closure = clib_ptclosure (orig);
250
251   /* Compute a partial order across feature nodes, if one exists. */
252 again:
253   for (i = 0; i < n_features; i++)
254     {
255       for (j = 0; j < n_features; j++)
256         {
257           if (closure[i][j])
258             goto item_constrained;
259         }
260       /* Item i can be output */
261       vec_add1 (result, i);
262       {
263         for (k = 0; k < n_features; k++)
264           closure[k][i] = 0;
265         /*
266          * Add a "Magic" a before a constraint.
267          * This means we'll never output it again
268          */
269         closure[i][i] = 1;
270         goto again;
271       }
272     item_constrained:
273       ;
274     }
275
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!");
279
280   /*
281    * We win.
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.
286    */
287
288   for (i = n_features - 1; i >= 0; i--)
289     {
290       p = hash_get (reg_by_index, result[i]);
291       ASSERT (p != 0);
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);
295     }
296
297   /* Set up the config infrastructure */
298   vnet_config_init (vm, vcm,
299                     feature_start_nodes,
300                     num_feature_start_nodes,
301                     feature_nodes, vec_len (feature_nodes));
302
303   /* Save a copy for show command */
304   *in_feature_nodes = feature_nodes;
305
306   /* Finally, clean up all the shit we allocated */
307   /* *INDENT-OFF* */
308   hash_foreach_pair (hp, index_by_name,
309   ({
310     vec_add1 (keys_to_delete, (u8 *)hp->key);
311   }));
312   /* *INDENT-ON* */
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);
318   vec_free (result);
319   clib_ptclosure_free (orig);
320   clib_ptclosure_free (closure);
321   return 0;
322 }
323
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")
331
332 /** Display the set of available ip features.
333     Useful for verifying that expected features are present
334 */
335
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)
340 {
341   ip4_main_t *im4 = &ip4_main;
342   ip6_main_t *im6 = &ip6_main;
343   int i;
344   char **features;
345
346   vlib_cli_output (vm, "Available IP feature nodes");
347
348 #define _(a,c,s)                                        \
349   do {                                                  \
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]);      \
354   } while(0);
355   foreach_af_cast;
356 #undef _
357
358   return 0;
359 }
360
361 /*?
362  * Display the set of available ip features
363  *
364  * @cliexpar
365  * Example:
366  * @cliexcmd{show ip features}
367  * @cliexend
368  * @endparblock
369 ?*/
370 /* *INDENT-OFF* */
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,
375 };
376 /* *INDENT-ON* */
377
378 /** Display the set of IP features configured on a specific interface
379  */
380
381 void
382 ip_interface_features_show (vlib_main_t * vm,
383                             const char *pname,
384                             ip_config_main_t * cm, u32 sw_if_index)
385 {
386   u32 node_index, current_config_index;
387   vnet_cast_t cast;
388   vnet_config_main_t *vcm;
389   vnet_config_t *cfg;
390   u32 cfg_index;
391   vnet_config_feature_t *feat;
392   vlib_node_t *n;
393   int i;
394
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);
398
399   for (cast = VNET_IP_RX_UNICAST_FEAT; cast < VNET_N_IP_FEAT; cast++)
400     {
401       vcm = &(cm[cast].config_main);
402
403       vlib_cli_output (vm, "\n%s %s:", pname, vnet_cast_names[cast]);
404
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)
407         {
408           vlib_cli_output (vm, "none configured");
409           continue;
410         }
411
412       current_config_index = vec_elt (cm[cast].config_index_by_sw_if_index,
413                                       sw_if_index);
414
415       ASSERT (current_config_index
416               < vec_len (vcm->config_pool_index_by_user_index));
417
418       cfg_index = vcm->config_pool_index_by_user_index[current_config_index];
419       cfg = pool_elt_at_index (vcm->config_pool, cfg_index);
420
421       for (i = 0; i < vec_len (cfg->features); i++)
422         {
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);
427         }
428     }
429 }
430
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)
435 {
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;
441
442   ip_lookup_main_t *lm;
443   u32 sw_if_index, af;
444
445   if (!unformat (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
446     return clib_error_return (0, "Interface not specified...");
447
448   vlib_cli_output (vm, "IP feature paths configured on %U...",
449                    format_vnet_sw_if_index_name, vnm, sw_if_index);
450
451   for (af = 0; af < 2; af++)
452     {
453       if (af == 0)
454         lm = lm4;
455       else
456         lm = lm6;
457
458       ip_interface_features_show (vm, (af == 0) ? "ip4" : "ip6",
459                                   lm->feature_config_mains, sw_if_index);
460     }
461
462   return 0;
463 }
464
465 /*?
466  * Display the ip features configured on a specific interface
467  *
468  * @cliexpar
469  * Example:
470  * @cliexcmd{show ip interface features GigabitEthernet2/0/0}
471  * @cliexend
472  * @endparblock
473 ?*/
474 /* *INDENT-OFF* */
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,
479 };
480 /* *INDENT-ON* */
481
482 /*
483  * fd.io coding-style-patch-verification: ON
484  *
485  * Local Variables:
486  * eval: (c-set-style "gnu")
487  * End:
488  */