9505a09e20da2d4bb9fa23d3bafa38ca30efc63b
[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
19 /** \file
20
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.
24
25     Use the topological sort result to set up vnet_config_main_t's for
26     use at runtime.
27
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.
33
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
36     definitions.
37
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,
41     <CODE><PRE>
42     \#include <vnet/ip/ip_feature_registration.h>
43     </PRE></CODE>
44
45     and add the new feature as shown:
46
47     <CODE><PRE>
48     VNET_IP4_UNICAST_FEATURE_INIT (ip4_lookup, static) =
49     {
50       .node_name = "my-ip4-unicast-feature",
51       .runs_before = ORDER_CONSTRAINTS {"ip4-lookup", 0}
52       .feature_index = &my_feature_index,
53     };
54     </PRE></CODE>
55
56     Here's the standard coding pattern to enable / disable
57     @c my-ip4-unicast-feature on an interface:
58
59     <CODE><PRE>
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];
64
65     sw_if_index = <interface-handle>
66     ci = rx_cm->config_index_by_sw_if_index[sw_if_index];
67     ci = (is_add
68           ? vnet_config_add_feature
69           : vnet_config_del_feature)
70       (vm, &rx_cm->config_main,
71        ci,
72        my_feature_index,
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;
76     </PRE></CODE>
77
78     For tx features, add this line after setting
79     <CODE><PRE>
80     tx_cm->config_index_by_sw_if_index = ci.
81     </PRE></CODE>
82
83     This maintains a
84     per-interface "at least one TX feature enabled" bitmap:
85
86     <CODE><PRE>
87     vnet_config_update_tx_feature_count (lm, tx_cm, sw_if_index, is_add);
88     </PRE></CODE>
89
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:
92
93     <CODE><PRE>
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];
96
97     Call @c vnet_get_config_data to set next0, and to advance
98     @c b0->current_config_index:
99
100     config_data0 = vnet_get_config_data (&cm->config_main,
101                                          &b0->current_config_index,
102                                          &next0,
103                                          0 / * sizeof config data * /);
104     </PRE></CODE>
105
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.
109 */
110
111 static int
112 comma_split (u8 * s, u8 ** a, u8 ** b)
113 {
114   *a = s;
115
116   while (*s && *s != ',')
117     s++;
118
119   if (*s == ',')
120     *s = 0;
121   else
122     return 1;
123
124   *b = (u8 *) (s + 1);
125   return 0;
126 }
127
128 clib_error_t *
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)
135 {
136   uword *index_by_name;
137   uword *reg_by_index;
138   u8 **node_names = 0;
139   u8 *node_name;
140   char **these_constraints;
141   char *this_constraint_c;
142   u8 **constraints = 0;
143   u8 *constraint_tuple;
144   u8 *this_constraint;
145   u8 **orig, **closure;
146   uword *p;
147   int i, j, k;
148   u8 *a_name, *b_name;
149   int a_index, b_index;
150   int n_features;
151   u32 *result = 0;
152   vnet_ip_feature_registration_t *this_reg, *first_reg = 0;
153   char **feature_nodes = 0;
154   hash_pair_t *hp;
155   u8 **keys_to_delete = 0;
156   ip4_main_t *im4 = &ip4_main;
157   ip6_main_t *im6 = &ip6_main;
158
159   index_by_name = hash_create_string (0, sizeof (uword));
160   reg_by_index = hash_create (0, sizeof (uword));
161
162   if (cast == VNET_IP_RX_UNICAST_FEAT)
163     {
164       if (is_ip4)
165         first_reg = im4->next_uc_feature;
166       else
167         first_reg = im6->next_uc_feature;
168     }
169   else if (cast == VNET_IP_RX_MULTICAST_FEAT)
170     {
171       if (is_ip4)
172         first_reg = im4->next_mc_feature;
173       else
174         first_reg = im6->next_mc_feature;
175     }
176   else if (cast == VNET_IP_TX_FEAT)
177     {
178       if (is_ip4)
179         first_reg = im4->next_tx_feature;
180       else
181         first_reg = im6->next_tx_feature;
182     }
183
184
185   this_reg = first_reg;
186
187   /* pass 1, collect feature node names, construct a before b pairs */
188   while (this_reg)
189     {
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);
192
193       hash_set_mem (index_by_name, node_name, vec_len (node_names));
194
195       vec_add1 (node_names, node_name);
196
197       these_constraints = this_reg->runs_before;
198       while (these_constraints && these_constraints[0])
199         {
200           this_constraint_c = these_constraints[0];
201
202           constraint_tuple = format (0, "%s,%s%c", node_name,
203                                      this_constraint_c, 0);
204           vec_add1 (constraints, constraint_tuple);
205           these_constraints++;
206         }
207
208       these_constraints = this_reg->runs_after;
209       while (these_constraints && these_constraints[0])
210         {
211           this_constraint_c = these_constraints[0];
212
213           constraint_tuple = format (0, "%s,%s%c",
214                                      this_constraint_c, node_name, 0);
215           vec_add1 (constraints, constraint_tuple);
216           these_constraints++;
217         }
218
219       this_reg = this_reg->next;
220     }
221
222   n_features = vec_len (node_names);
223   orig = clib_ptclosure_alloc (n_features);
224
225   for (i = 0; i < vec_len (constraints); i++)
226     {
227       this_constraint = constraints[i];
228
229       if (comma_split (this_constraint, &a_name, &b_name))
230         return clib_error_return (0, "comma_split failed!");
231
232       p = hash_get_mem (index_by_name, a_name);
233       /*
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.
238        */
239       if (p == 0)
240         return clib_error_return (0, "feature node '%s' not found", a_name);
241       a_index = p[0];
242
243       p = hash_get_mem (index_by_name, b_name);
244       if (p == 0)
245         return clib_error_return (0, "feature node '%s' not found", b_name);
246       b_index = p[0];
247
248       /* add a before b to the original set of constraints */
249       orig[a_index][b_index] = 1;
250       vec_free (this_constraint);
251     }
252
253   /* Compute the positive transitive closure of the original constraints */
254   closure = clib_ptclosure (orig);
255
256   /* Compute a partial order across feature nodes, if one exists. */
257 again:
258   for (i = 0; i < n_features; i++)
259     {
260       for (j = 0; j < n_features; j++)
261         {
262           if (closure[i][j])
263             goto item_constrained;
264         }
265       /* Item i can be output */
266       vec_add1 (result, i);
267       {
268         for (k = 0; k < n_features; k++)
269           closure[k][i] = 0;
270         /*
271          * Add a "Magic" a before a constraint.
272          * This means we'll never output it again
273          */
274         closure[i][i] = 1;
275         goto again;
276       }
277     item_constrained:
278       ;
279     }
280
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);
286
287   /*
288    * We win.
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.
293    */
294
295   for (i = n_features - 1; i >= 0; i--)
296     {
297       p = hash_get (reg_by_index, result[i]);
298       ASSERT (p != 0);
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);
302     }
303
304   /* Set up the config infrastructure */
305   vnet_config_init (vm, vcm,
306                     feature_start_nodes,
307                     num_feature_start_nodes,
308                     feature_nodes, vec_len (feature_nodes));
309
310   /* Save a copy for show command */
311   if (is_ip4)
312     im4->feature_nodes[cast] = feature_nodes;
313   else
314     im6->feature_nodes[cast] = feature_nodes;
315
316   /* Finally, clean up all the shit we allocated */
317   /* *INDENT-OFF* */
318   hash_foreach_pair (hp, index_by_name,
319   ({
320     vec_add1 (keys_to_delete, (u8 *)hp->key);
321   }));
322   /* *INDENT-ON* */
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);
328   vec_free (result);
329   clib_ptclosure_free (orig);
330   clib_ptclosure_free (closure);
331   return 0;
332 }
333
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")
341
342 /** Display the set of available ip features.
343     Useful for verifying that expected features are present
344 */
345
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)
350 {
351   ip4_main_t *im4 = &ip4_main;
352   ip6_main_t *im6 = &ip6_main;
353   int i;
354   char **features;
355
356   vlib_cli_output (vm, "Available IP feature nodes");
357
358 #define _(a,c,s)                                        \
359   do {                                                  \
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]);      \
364   } while(0);
365   foreach_af_cast;
366 #undef _
367
368   return 0;
369 }
370
371 /* *INDENT-OFF* */
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,
376 };
377 /* *INDENT-ON* */
378
379 /** Display the set of IP features configured on a specific interface
380  */
381
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)
386 {
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;
392
393   ip_lookup_main_t *lm;
394   ip_config_main_t *cm;
395   vnet_config_main_t *vcm;
396   vnet_config_t *cfg;
397   u32 cfg_index;
398   vnet_config_feature_t *feat;
399   vlib_node_t *n;
400   u32 sw_if_index;
401   u32 node_index;
402   u32 current_config_index;
403   int i, af;
404   u32 cast;
405
406   if (!unformat (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
407     return clib_error_return (0, "Interface not specified...");
408
409   vlib_cli_output (vm, "IP feature paths configured on %U...",
410                    format_vnet_sw_if_index_name, vnm, sw_if_index);
411
412
413   for (af = 0; af < 2; af++)
414     {
415       if (af == 0)
416         lm = lm4;
417       else
418         lm = lm6;
419
420       for (cast = VNET_IP_RX_UNICAST_FEAT; cast < VNET_N_IP_FEAT; cast++)
421         {
422           cm = lm->feature_config_mains + cast;
423           vcm = &cm->config_main;
424
425           vlib_cli_output (vm, "\nipv%s %scast:",
426                            (af == 0) ? "4" : "6",
427                            cast == VNET_IP_RX_UNICAST_FEAT ? "uni" : "multi");
428
429           current_config_index = vec_elt (cm->config_index_by_sw_if_index,
430                                           sw_if_index);
431
432           ASSERT (current_config_index
433                   < vec_len (vcm->config_pool_index_by_user_index));
434
435           cfg_index =
436             vcm->config_pool_index_by_user_index[current_config_index];
437           cfg = pool_elt_at_index (vcm->config_pool, cfg_index);
438
439           for (i = 0; i < vec_len (cfg->features); i++)
440             {
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);
445             }
446         }
447     }
448
449   return 0;
450 }
451
452 /* *INDENT-OFF* */
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,
457 };
458 /* *INDENT-ON* */
459
460 /*
461  * fd.io coding-style-patch-verification: ON
462  *
463  * Local Variables:
464  * eval: (c-set-style "gnu")
465  * End:
466  */