FD.io VPP  v16.12-rc0-217-g241e522
Vector Packet Processing
ip_feature_registration.c
Go to the documentation of this file.
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 /**
21  * @file
22  * @brief IP Feature Subgraph Ordering.
23 
24  Dynamically compute IP feature subgraph ordering by performing a
25  topological sort across a set of "feature A before feature B" and
26  "feature C after feature B" constraints.
27 
28  Use the topological sort result to set up vnet_config_main_t's for
29  use at runtime.
30 
31  Feature subgraph arcs are simple enough. They start at specific
32  fixed nodes, and end at specific fixed nodes. In between, a
33  per-interface current feature configuration dictates which
34  additional nodes each packet visits. Each so-called feature node
35  can [of course] drop any specific packet.
36 
37  See ip4_forward.c, ip6_forward.c in this directory to see the
38  current rx-unicast, rx-multicast, and tx feature subgraph arc
39  definitions.
40 
41  Let's say that we wish to add a new feature to the ip4 unicast
42  feature subgraph arc, which needs to run before @c ip4-lookup. In
43  either base code or a plugin,
44  <CODE><PRE>
45  \#include <vnet/ip/ip_feature_registration.h>
46  </PRE></CODE>
47 
48  and add the new feature as shown:
49 
50  <CODE><PRE>
51  VNET_IP4_UNICAST_FEATURE_INIT (ip4_lookup, static) =
52  {
53  .node_name = "my-ip4-unicast-feature",
54  .runs_before = ORDER_CONSTRAINTS {"ip4-lookup", 0}
55  .feature_index = &my_feature_index,
56  };
57  </PRE></CODE>
58 
59  Here's the standard coding pattern to enable / disable
60  @c my-ip4-unicast-feature on an interface:
61 
62  <CODE><PRE>
63  ip4_main_t *im = \&ip4_main;
64  ip_lookup_main_t *lm = &im->lookup_main;
65  ip_config_main_t *rx_cm =
66  &lm->feature_config_mains[VNET_IP_RX_UNICAST_FEAT];
67 
68  sw_if_index = <interface-handle>
69  ci = rx_cm->config_index_by_sw_if_index[sw_if_index];
70  ci = (is_add
71  ? vnet_config_add_feature
72  : vnet_config_del_feature)
73  (vm, &rx_cm->config_main,
74  ci,
75  my_feature_index,
76  0 / * &config struct if feature uses private config data * /,
77  0 / * sizeof config struct if feature uses private config data * /);
78  rx_cm->config_index_by_sw_if_index[sw_if_index] = ci;
79  </PRE></CODE>
80 
81  For tx features, add this line after setting
82  <CODE><PRE>
83  tx_cm->config_index_by_sw_if_index = ci.
84  </PRE></CODE>
85 
86  This maintains a
87  per-interface "at least one TX feature enabled" bitmap:
88 
89  <CODE><PRE>
90  vnet_config_update_tx_feature_count (lm, tx_cm, sw_if_index, is_add);
91  </PRE></CODE>
92 
93  Here's how to obtain the correct next node index in packet
94  processing code, aka in the implementation of @c my-ip4-unicast-feature:
95 
96  <CODE><PRE>
97  ip_lookup_main_t * lm = sm->ip4_lookup_main;
98  ip_config_main_t * cm = &lm->feature_config_mains[VNET_IP_RX_UNICAST_FEAT];
99 
100  Call @c vnet_get_config_data to set next0, and to advance
101  @c b0->current_config_index:
102 
103  config_data0 = vnet_get_config_data (&cm->config_main,
104  &b0->current_config_index,
105  &next0,
106  0 / * sizeof config data * /);
107  </PRE></CODE>
108 
109  Nodes are free to drop or otherwise redirect packets. Packets
110  which "pass" should be enqueued via the next0 arc computed by
111  vnet_get_config_data.
112 */
113 
114 static const char *vnet_cast_names[] = VNET_CAST_NAMES;
115 
116 static int
117 comma_split (u8 * s, u8 ** a, u8 ** b)
118 {
119  *a = s;
120 
121  while (*s && *s != ',')
122  s++;
123 
124  if (*s == ',')
125  *s = 0;
126  else
127  return 1;
128 
129  *b = (u8 *) (s + 1);
130  return 0;
131 }
132 
133 /**
134  * @brief Initialize a feature graph arc
135  * @param vm vlib main structure pointer
136  * @param vcm vnet config main structure pointer
137  * @param feature_start_nodes names of start-nodes which use this
138  * feature graph arc
139  * @param num_feature_start_nodes number of start-nodes
140  * @param first_reg first element in
141  * [an __attribute__((constructor)) function built, or
142  * otherwise created] singly-linked list of feature registrations
143  * @param [out] in_feature_nodes returned vector of
144  * topologically-sorted feature node names, for use in
145  * show commands
146  * @returns 0 on success, otherwise an error message. Errors
147  * are fatal since they invariably involve mistyped node-names, or
148  * genuinely missing node-names
149  */
150 clib_error_t *
152  vnet_config_main_t * vcm,
153  char **feature_start_nodes,
154  int num_feature_start_nodes,
155  vnet_ip_feature_registration_t * first_reg,
156  char ***in_feature_nodes)
157 {
158  uword *index_by_name;
159  uword *reg_by_index;
160  u8 **node_names = 0;
161  u8 *node_name;
162  char **these_constraints;
163  char *this_constraint_c;
164  u8 **constraints = 0;
165  u8 *constraint_tuple;
166  u8 *this_constraint;
167  u8 **orig, **closure;
168  uword *p;
169  int i, j, k;
170  u8 *a_name, *b_name;
171  int a_index, b_index;
172  int n_features;
173  u32 *result = 0;
174  vnet_ip_feature_registration_t *this_reg = 0;
175  char **feature_nodes = 0;
176  hash_pair_t *hp;
177  u8 **keys_to_delete = 0;
178 
179  index_by_name = hash_create_string (0, sizeof (uword));
180  reg_by_index = hash_create (0, sizeof (uword));
181 
182  this_reg = first_reg;
183 
184  /* pass 1, collect feature node names, construct a before b pairs */
185  while (this_reg)
186  {
187  node_name = format (0, "%s%c", this_reg->node_name, 0);
188  hash_set (reg_by_index, vec_len (node_names), (uword) this_reg);
189 
190  hash_set_mem (index_by_name, node_name, vec_len (node_names));
191 
192  vec_add1 (node_names, node_name);
193 
194  these_constraints = this_reg->runs_before;
195  while (these_constraints && these_constraints[0])
196  {
197  this_constraint_c = these_constraints[0];
198 
199  constraint_tuple = format (0, "%s,%s%c", node_name,
200  this_constraint_c, 0);
201  vec_add1 (constraints, constraint_tuple);
202  these_constraints++;
203  }
204 
205  these_constraints = this_reg->runs_after;
206  while (these_constraints && these_constraints[0])
207  {
208  this_constraint_c = these_constraints[0];
209 
210  constraint_tuple = format (0, "%s,%s%c",
211  this_constraint_c, node_name, 0);
212  vec_add1 (constraints, constraint_tuple);
213  these_constraints++;
214  }
215 
216  this_reg = this_reg->next;
217  }
218 
219  n_features = vec_len (node_names);
220  orig = clib_ptclosure_alloc (n_features);
221 
222  for (i = 0; i < vec_len (constraints); i++)
223  {
224  this_constraint = constraints[i];
225 
226  if (comma_split (this_constraint, &a_name, &b_name))
227  return clib_error_return (0, "comma_split failed!");
228 
229  p = hash_get_mem (index_by_name, a_name);
230  /*
231  * Note: the next two errors mean that the xxx_FEATURE_INIT macros are
232  * b0rked. As in: if you code "A depends on B," and you forget
233  * to define a FEATURE_INIT macro for B, you lose.
234  * Nonexistent graph nodes are tolerated.
235  */
236  if (p == 0)
237  return clib_error_return (0, "feature node '%s' not found", a_name);
238  a_index = p[0];
239 
240  p = hash_get_mem (index_by_name, b_name);
241  if (p == 0)
242  return clib_error_return (0, "feature node '%s' not found", b_name);
243  b_index = p[0];
244 
245  /* add a before b to the original set of constraints */
246  orig[a_index][b_index] = 1;
247  vec_free (this_constraint);
248  }
249 
250  /* Compute the positive transitive closure of the original constraints */
251  closure = clib_ptclosure (orig);
252 
253  /* Compute a partial order across feature nodes, if one exists. */
254 again:
255  for (i = 0; i < n_features; i++)
256  {
257  for (j = 0; j < n_features; j++)
258  {
259  if (closure[i][j])
260  goto item_constrained;
261  }
262  /* Item i can be output */
263  vec_add1 (result, i);
264  {
265  for (k = 0; k < n_features; k++)
266  closure[k][i] = 0;
267  /*
268  * Add a "Magic" a before a constraint.
269  * This means we'll never output it again
270  */
271  closure[i][i] = 1;
272  goto again;
273  }
274  item_constrained:
275  ;
276  }
277 
278  /* see if we got a partial order... */
279  if (vec_len (result) != n_features)
280  return clib_error_return (0, "%d feature_init_cast no partial order!");
281 
282  /*
283  * We win.
284  * Bind the index variables, and output the feature node name vector
285  * using the partial order we just computed. Result is in stack
286  * order, because the entry with the fewest constraints (e.g. none)
287  * is output first, etc.
288  */
289 
290  for (i = n_features - 1; i >= 0; i--)
291  {
292  p = hash_get (reg_by_index, result[i]);
293  ASSERT (p != 0);
294  this_reg = (vnet_ip_feature_registration_t *) p[0];
295  *this_reg->feature_index = n_features - (i + 1);
296  vec_add1 (feature_nodes, this_reg->node_name);
297  }
298 
299  /* Set up the config infrastructure */
300  vnet_config_init (vm, vcm,
301  feature_start_nodes,
302  num_feature_start_nodes,
303  feature_nodes, vec_len (feature_nodes));
304 
305  /* Save a copy for show command */
306  *in_feature_nodes = feature_nodes;
307 
308  /* Finally, clean up all the shit we allocated */
309  /* *INDENT-OFF* */
310  hash_foreach_pair (hp, index_by_name,
311  ({
312  vec_add1 (keys_to_delete, (u8 *)hp->key);
313  }));
314  /* *INDENT-ON* */
315  hash_free (index_by_name);
316  for (i = 0; i < vec_len (keys_to_delete); i++)
317  vec_free (keys_to_delete[i]);
318  vec_free (keys_to_delete);
319  hash_free (reg_by_index);
320  vec_free (result);
321  clib_ptclosure_free (orig);
322  clib_ptclosure_free (closure);
323  return 0;
324 }
325 
326 #define foreach_af_cast \
327 _(4, VNET_IP_RX_UNICAST_FEAT, "ip4 unicast") \
328 _(4, VNET_IP_RX_MULTICAST_FEAT, "ip4 multicast") \
329 _(4, VNET_IP_TX_FEAT, "ip4 output") \
330 _(6, VNET_IP_RX_UNICAST_FEAT, "ip6 unicast") \
331 _(6, VNET_IP_RX_MULTICAST_FEAT, "ip6 multicast") \
332 _(6, VNET_IP_TX_FEAT, "ip6 output")
333 
334 /** Display the set of available ip features.
335  Useful for verifying that expected features are present
336 */
337 
338 static clib_error_t *
340  unformat_input_t * input,
341  vlib_cli_command_t * cmd)
342 {
343  ip4_main_t *im4 = &ip4_main;
344  ip6_main_t *im6 = &ip6_main;
345  int i;
346  char **features;
347 
348  vlib_cli_output (vm, "Available IP feature nodes");
349 
350 #define _(a,c,s) \
351  do { \
352  features = im##a->feature_nodes[c]; \
353  vlib_cli_output (vm, "%s:", s); \
354  for (i = 0; i < vec_len(features); i++) \
355  vlib_cli_output (vm, " %s\n", features[i]); \
356  } while(0);
358 #undef _
359 
360  return 0;
361 }
362 
363 /*?
364  * This command is used to display the set of available IP features.
365  * This can be useful for verifying that expected features are present.
366  *
367  * @cliexpar
368  * Example of how to display the set of available IP features:
369  * @cliexstart{show ip features}
370  * Available IP feature nodes
371  * ip4 unicast:
372  * ip4-inacl
373  * ip4-source-check-via-rx
374  * ip4-source-check-via-any
375  * ip4-source-and-port-range-check-rx
376  * ip4-policer-classify
377  * ipsec-input-ip4
378  * vpath-input-ip4
379  * snat-in2out
380  * snat-out2in
381  * ip4-lookup
382  * ip4 multicast:
383  * vpath-input-ip4
384  * ip4-lookup-multicast
385  * ip4 output:
386  * ip4-source-and-port-range-check-tx
387  * interface-output
388  * ip6 unicast:
389  * ip6-inacl
390  * ip6-policer-classify
391  * ipsec-input-ip6
392  * l2tp-decap
393  * vpath-input-ip6
394  * sir-to-ila
395  * ip6-lookup
396  * ip6 multicast:
397  * vpath-input-ip6
398  * ip6-lookup
399  * ip6 output:
400  * interface-output
401  * @cliexend
402 ?*/
403 /* *INDENT-OFF* */
404 VLIB_CLI_COMMAND (show_ip_features_command, static) = {
405  .path = "show ip features",
406  .short_help = "show ip features",
407  .function = show_ip_features_command_fn,
408 };
409 /* *INDENT-ON* */
410 
411 /** Display the set of IP features configured on a specific interface
412  */
413 
414 void
416  const char *pname,
417  ip_config_main_t * cm, u32 sw_if_index)
418 {
419  u32 node_index, current_config_index;
420  vnet_cast_t cast;
421  vnet_config_main_t *vcm;
422  vnet_config_t *cfg;
423  u32 cfg_index;
424  vnet_config_feature_t *feat;
425  vlib_node_t *n;
426  int i;
427 
428  vlib_cli_output (vm, "%s feature paths configured on %U...",
430  vnet_get_main (), sw_if_index);
431 
432  for (cast = VNET_IP_RX_UNICAST_FEAT; cast < VNET_N_IP_FEAT; cast++)
433  {
434  vcm = &(cm[cast].config_main);
435 
436  vlib_cli_output (vm, "\n%s %s:", pname, vnet_cast_names[cast]);
437 
438  if (NULL == cm[cast].config_index_by_sw_if_index ||
439  vec_len (cm[cast].config_index_by_sw_if_index) < sw_if_index)
440  {
441  vlib_cli_output (vm, "none configured");
442  continue;
443  }
444 
445  current_config_index = vec_elt (cm[cast].config_index_by_sw_if_index,
446  sw_if_index);
447 
448  ASSERT (current_config_index
450 
451  cfg_index = vcm->config_pool_index_by_user_index[current_config_index];
452  cfg = pool_elt_at_index (vcm->config_pool, cfg_index);
453 
454  for (i = 0; i < vec_len (cfg->features); i++)
455  {
456  feat = cfg->features + i;
457  node_index = feat->node_index;
458  n = vlib_get_node (vm, node_index);
459  vlib_cli_output (vm, " %v", n->name);
460  }
461  }
462 }
463 
464 static clib_error_t *
466  unformat_input_t * input,
467  vlib_cli_command_t * cmd)
468 {
469  vnet_main_t *vnm = vnet_get_main ();
470  ip4_main_t *im4 = &ip4_main;
471  ip_lookup_main_t *lm4 = &im4->lookup_main;
472  ip6_main_t *im6 = &ip6_main;
473  ip_lookup_main_t *lm6 = &im6->lookup_main;
474 
475  ip_lookup_main_t *lm;
476  u32 sw_if_index, af;
477 
478  if (!unformat (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
479  return clib_error_return (0, "Interface not specified...");
480 
481  vlib_cli_output (vm, "IP feature paths configured on %U...",
482  format_vnet_sw_if_index_name, vnm, sw_if_index);
483 
484  for (af = 0; af < 2; af++)
485  {
486  if (af == 0)
487  lm = lm4;
488  else
489  lm = lm6;
490 
491  ip_interface_features_show (vm, (af == 0) ? "ip4" : "ip6",
492  lm->feature_config_mains, sw_if_index);
493  }
494 
495  return 0;
496 }
497 
498 /*?
499  * This command is used to display the set of IP features configured
500  * on a specific interface
501  *
502  * @cliexpar
503  * Example of how to display the set of available IP features on an interface:
504  * @cliexstart{show ip interface features GigabitEthernet2/0/0}
505  * IP feature paths configured on GigabitEthernet2/0/0...
506  * ipv4 unicast:
507  * ip4-lookup
508  * ipv4 multicast:
509  * ip4-lookup-multicast
510  * ipv4 multicast:
511  * interface-output
512  * ipv6 unicast:
513  * ip6-lookup
514  * ipv6 multicast:
515  * ip6-lookup
516  * ipv6 multicast:
517  * interface-output
518  * @cliexend
519 ?*/
520 /* *INDENT-OFF* */
521 VLIB_CLI_COMMAND (show_ip_interface_features_command, static) = {
522  .path = "show ip interface features",
523  .short_help = "show ip interface features <interface>",
525 };
526 /* *INDENT-ON* */
527 
528 /*
529  * fd.io coding-style-patch-verification: ON
530  *
531  * Local Variables:
532  * eval: (c-set-style "gnu")
533  * End:
534  */
static const char * vnet_cast_names[]
#define hash_set(h, key, value)
Definition: hash.h:254
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:343
vnet_config_feature_t * features
Definition: config.h:71
static char * feature_start_nodes[]
Definition: init.c:64
uword unformat(unformat_input_t *i, char *fmt,...)
Definition: unformat.c:966
void vnet_config_init(vlib_main_t *vm, vnet_config_main_t *cm, char *start_node_names[], int n_start_node_names, char *feature_node_names[], int n_feature_node_names)
Definition: config.c:161
a
Definition: bitmap.h:516
#define foreach_af_cast
#define NULL
Definition: clib.h:55
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:482
#define hash_set_mem(h, key, value)
Definition: hash.h:274
ip_lookup_main_t lookup_main
Definition: ip4.h:96
unformat_function_t unformat_vnet_sw_interface
format_function_t format_vnet_sw_if_index_name
static clib_error_t * show_ip_interface_features_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
clib_error_t * vnet_feature_arc_init(vlib_main_t *vm, vnet_config_main_t *vcm, char **feature_start_nodes, int num_feature_start_nodes, vnet_ip_feature_registration_t *first_reg, char ***in_feature_nodes)
Initialize a feature graph arc.
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
#define hash_create_string(elts, value_bytes)
Definition: hash.h:652
#define hash_get(h, key)
Definition: hash.h:248
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:369
#define hash_free(h)
Definition: hash.h:286
u8 ** clib_ptclosure_alloc(int n)
Definition: ptclosure.c:19
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:575
static int comma_split(u8 *s, u8 **a, u8 **b)
u8 * name
Definition: node.h:221
void clib_ptclosure_free(u8 **ptc)
Definition: ptclosure.c:39
u32 * config_pool_index_by_user_index
Definition: config.h:104
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:300
ip_config_main_t feature_config_mains[VNET_N_IP_FEAT]
rx unicast, multicast, tx interface/feature configuration.
Definition: lookup.h:360
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:154
#define hash_create(elts, value_bytes)
Definition: hash.h:658
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
ip6_main_t ip6_main
Definition: ip6_forward.c:2640
ip_lookup_main_t lookup_main
Definition: ip6.h:132
vnet_cast_t
Definition: vnet.h:45
IPv4 main type.
Definition: ip4.h:95
static clib_error_t * show_ip_features_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Display the set of available ip features.
u64 uword
Definition: types.h:112
#define vec_elt(v, i)
Get vector value at index i.
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
unsigned char u8
Definition: types.h:56
#define hash_foreach_pair(p, v, body)
Iterate over hash pairs.
Definition: hash.h:349
void ip_interface_features_show(vlib_main_t *vm, const char *pname, ip_config_main_t *cm, u32 sw_if_index)
Display the set of IP features configured on a specific interface.
u8 ** clib_ptclosure(u8 **orig)
Definition: ptclosure.c:90
#define VNET_CAST_NAMES
Definition: vnet.h:53
#define hash_get_mem(h, key)
Definition: hash.h:268
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:418
ip4_main_t ip4_main
Global ip4 main structure.
Definition: ip4_forward.c:1010
vnet_config_t * config_pool
Definition: config.h:89
static vlib_node_t * vlib_get_node(vlib_main_t *vm, u32 i)
Get vlib node by index.
Definition: node_funcs.h:58
#define clib_error_return(e, args...)
Definition: error.h:111
struct _unformat_input_t unformat_input_t
uword key
Definition: hash.h:161
struct _vnet_ip_feature_registration vnet_ip_feature_registration_t
feature registration object
vnet_config_main_t config_main