A Protocol Independent Hierarchical FIB (VPP-352)
[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 int
113 comma_split (u8 * s, u8 ** a, u8 ** b)
114 {
115   *a = s;
116
117   while (*s && *s != ',')
118     s++;
119
120   if (*s == ',')
121     *s = 0;
122   else
123     return 1;
124
125   *b = (u8 *) (s + 1);
126   return 0;
127 }
128
129 clib_error_t *
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)
136 {
137   uword *index_by_name;
138   uword *reg_by_index;
139   u8 **node_names = 0;
140   u8 *node_name;
141   char **these_constraints;
142   char *this_constraint_c;
143   u8 **constraints = 0;
144   u8 *constraint_tuple;
145   u8 *this_constraint;
146   u8 **orig, **closure;
147   uword *p;
148   int i, j, k;
149   u8 *a_name, *b_name;
150   int a_index, b_index;
151   int n_features;
152   u32 *result = 0;
153   vnet_ip_feature_registration_t *this_reg, *first_reg = 0;
154   char **feature_nodes = 0;
155   hash_pair_t *hp;
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;
160
161   index_by_name = hash_create_string (0, sizeof (uword));
162   reg_by_index = hash_create (0, sizeof (uword));
163
164   if (cast == VNET_IP_RX_UNICAST_FEAT)
165     {
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;
172       else
173         return clib_error_return (0,
174                                   "protocol %d cast %d unsupport for features",
175                                   proto, cast);
176     }
177   else if (cast == VNET_IP_RX_MULTICAST_FEAT)
178     {
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;
183       else
184         return clib_error_return (0,
185                                   "protocol %d cast %d unsupport for features",
186                                   proto, cast);
187     }
188   else if (cast == VNET_IP_TX_FEAT)
189     {
190       if (proto == VNET_L3_PACKET_TYPE_IP4)
191         first_reg = im4->next_tx_feature;
192       else
193         first_reg = im6->next_tx_feature;
194     }
195
196   this_reg = first_reg;
197
198   /* pass 1, collect feature node names, construct a before b pairs */
199   while (this_reg)
200     {
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);
203
204       hash_set_mem (index_by_name, node_name, vec_len (node_names));
205
206       vec_add1 (node_names, node_name);
207
208       these_constraints = this_reg->runs_before;
209       while (these_constraints && these_constraints[0])
210         {
211           this_constraint_c = these_constraints[0];
212
213           constraint_tuple = format (0, "%s,%s%c", node_name,
214                                      this_constraint_c, 0);
215           vec_add1 (constraints, constraint_tuple);
216           these_constraints++;
217         }
218
219       these_constraints = this_reg->runs_after;
220       while (these_constraints && these_constraints[0])
221         {
222           this_constraint_c = these_constraints[0];
223
224           constraint_tuple = format (0, "%s,%s%c",
225                                      this_constraint_c, node_name, 0);
226           vec_add1 (constraints, constraint_tuple);
227           these_constraints++;
228         }
229
230       this_reg = this_reg->next;
231     }
232
233   n_features = vec_len (node_names);
234   orig = clib_ptclosure_alloc (n_features);
235
236   for (i = 0; i < vec_len (constraints); i++)
237     {
238       this_constraint = constraints[i];
239
240       if (comma_split (this_constraint, &a_name, &b_name))
241         return clib_error_return (0, "comma_split failed!");
242
243       p = hash_get_mem (index_by_name, a_name);
244       /*
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.
249        */
250       if (p == 0)
251         return clib_error_return (0, "feature node '%s' not found", a_name);
252       a_index = p[0];
253
254       p = hash_get_mem (index_by_name, b_name);
255       if (p == 0)
256         return clib_error_return (0, "feature node '%s' not found", b_name);
257       b_index = p[0];
258
259       /* add a before b to the original set of constraints */
260       orig[a_index][b_index] = 1;
261       vec_free (this_constraint);
262     }
263
264   /* Compute the positive transitive closure of the original constraints */
265   closure = clib_ptclosure (orig);
266
267   /* Compute a partial order across feature nodes, if one exists. */
268 again:
269   for (i = 0; i < n_features; i++)
270     {
271       for (j = 0; j < n_features; j++)
272         {
273           if (closure[i][j])
274             goto item_constrained;
275         }
276       /* Item i can be output */
277       vec_add1 (result, i);
278       {
279         for (k = 0; k < n_features; k++)
280           closure[k][i] = 0;
281         /*
282          * Add a "Magic" a before a constraint.
283          * This means we'll never output it again
284          */
285         closure[i][i] = 1;
286         goto again;
287       }
288     item_constrained:
289       ;
290     }
291
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);
296
297   /*
298    * We win.
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.
303    */
304
305   for (i = n_features - 1; i >= 0; i--)
306     {
307       p = hash_get (reg_by_index, result[i]);
308       ASSERT (p != 0);
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);
312     }
313
314   /* Set up the config infrastructure */
315   vnet_config_init (vm, vcm,
316                     feature_start_nodes,
317                     num_feature_start_nodes,
318                     feature_nodes, vec_len (feature_nodes));
319
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;
327
328   /* Finally, clean up all the shit we allocated */
329   /* *INDENT-OFF* */
330   hash_foreach_pair (hp, index_by_name,
331   ({
332     vec_add1 (keys_to_delete, (u8 *)hp->key);
333   }));
334   /* *INDENT-ON* */
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);
340   vec_free (result);
341   clib_ptclosure_free (orig);
342   clib_ptclosure_free (closure);
343   return 0;
344 }
345
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")
353
354 /** Display the set of available ip features.
355     Useful for verifying that expected features are present
356 */
357
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)
362 {
363   ip4_main_t *im4 = &ip4_main;
364   ip6_main_t *im6 = &ip6_main;
365   int i;
366   char **features;
367
368   vlib_cli_output (vm, "Available IP feature nodes");
369
370 #define _(a,c,s)                                        \
371   do {                                                  \
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]);      \
376   } while(0);
377   foreach_af_cast;
378 #undef _
379
380   return 0;
381 }
382
383 /* *INDENT-OFF* */
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,
388 };
389 /* *INDENT-ON* */
390
391 /** Display the set of IP features configured on a specific interface
392  */
393
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)
398 {
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;
404
405   ip_lookup_main_t *lm;
406   ip_config_main_t *cm;
407   vnet_config_main_t *vcm;
408   vnet_config_t *cfg;
409   u32 cfg_index;
410   vnet_config_feature_t *feat;
411   vlib_node_t *n;
412   u32 sw_if_index;
413   u32 node_index;
414   u32 current_config_index;
415   int i, af;
416   u32 cast;
417
418   if (!unformat (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
419     return clib_error_return (0, "Interface not specified...");
420
421   vlib_cli_output (vm, "IP feature paths configured on %U...",
422                    format_vnet_sw_if_index_name, vnm, sw_if_index);
423
424
425   for (af = 0; af < 2; af++)
426     {
427       if (af == 0)
428         lm = lm4;
429       else
430         lm = lm6;
431
432       for (cast = VNET_IP_RX_UNICAST_FEAT; cast < VNET_N_IP_FEAT; cast++)
433         {
434           cm = lm->feature_config_mains + cast;
435           vcm = &cm->config_main;
436
437           vlib_cli_output (vm, "\nipv%s %scast:",
438                            (af == 0) ? "4" : "6",
439                            cast == VNET_IP_RX_UNICAST_FEAT ? "uni" : "multi");
440
441           current_config_index = vec_elt (cm->config_index_by_sw_if_index,
442                                           sw_if_index);
443
444           ASSERT (current_config_index
445                   < vec_len (vcm->config_pool_index_by_user_index));
446
447           cfg_index =
448             vcm->config_pool_index_by_user_index[current_config_index];
449           cfg = pool_elt_at_index (vcm->config_pool, cfg_index);
450
451           for (i = 0; i < vec_len (cfg->features); i++)
452             {
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);
457             }
458         }
459     }
460
461   return 0;
462 }
463
464 /* *INDENT-OFF* */
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,
469 };
470 /* *INDENT-ON* */
471
472 /*
473  * fd.io coding-style-patch-verification: ON
474  *
475  * Local Variables:
476  * eval: (c-set-style "gnu")
477  * End:
478  */