FD.io VPP  v17.04.2-2-ga8f93f8
Vector Packet Processing
dpo.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 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  * @brief
17  * A Data-Path Object is an object that represents actions that are
18  * applied to packets are they are switched through VPP.
19  *
20  * The DPO is a base class that is specialised by other objects to provide
21  * concreate actions
22  *
23  * The VLIB graph nodes are graph of types, the DPO graph is a graph of instances.
24  */
25 
26 #include <vnet/dpo/dpo.h>
27 #include <vnet/ip/lookup.h>
28 #include <vnet/ip/format.h>
29 #include <vnet/adj/adj.h>
30 
31 #include <vnet/dpo/load_balance.h>
33 #include <vnet/dpo/lookup_dpo.h>
34 #include <vnet/dpo/drop_dpo.h>
35 #include <vnet/dpo/receive_dpo.h>
36 #include <vnet/dpo/punt_dpo.h>
37 #include <vnet/dpo/classify_dpo.h>
38 #include <vnet/dpo/ip_null_dpo.h>
39 #include <vnet/dpo/replicate_dpo.h>
40 
41 /**
42  * Array of char* names for the DPO types and protos
43  */
44 static const char* dpo_type_names[] = DPO_TYPES;
45 static const char* dpo_proto_names[] = DPO_PROTOS;
46 
47 /**
48  * @brief Vector of virtual function tables for the DPO types
49  *
50  * This is a vector so we can dynamically register new DPO types in plugins.
51  */
53 
54 /**
55  * @brief vector of graph node names associated with each DPO type and protocol.
56  *
57  * dpo_nodes[child_type][child_proto][node_X] = node_name;
58  * i.e.
59  * dpo_node[DPO_LOAD_BALANCE][DPO_PROTO_IP4][0] = "ip4-lookup"
60  * dpo_node[DPO_LOAD_BALANCE][DPO_PROTO_IP4][1] = "ip4-load-balance"
61  *
62  * This is a vector so we can dynamically register new DPO types in plugins.
63  */
64 static const char* const * const ** dpo_nodes;
65 
66 /**
67  * @brief Vector of edge indicies from parent DPO nodes to child
68  *
69  * dpo_edges[child_type][child_proto][parent_type][parent_proto] = edge_index
70  *
71  * This array is derived at init time from the dpo_nodes above. Note that
72  * the third dimension in dpo_nodes is lost, hence, the edge index from each
73  * node MUST be the same.
74  * Including both the child and parent protocol is required to support the
75  * case where it changes as the grapth is traversed, most notablly when an
76  * MPLS label is popped.
77  *
78  * Note that this array is child type specific, not child instance specific.
79  */
80 static u32 ****dpo_edges;
81 
82 /**
83  * @brief The DPO type value that can be assigend to the next dynamic
84  * type registration.
85  */
87 
90 {
91  switch (linkt)
92  {
93  case VNET_LINK_IP6:
94  return (DPO_PROTO_IP6);
95  case VNET_LINK_IP4:
96  return (DPO_PROTO_IP4);
97  case VNET_LINK_MPLS:
98  return (DPO_PROTO_MPLS);
99  case VNET_LINK_ETHERNET:
100  return (DPO_PROTO_ETHERNET);
101  case VNET_LINK_NSH:
102  return (DPO_PROTO_NSH);
103  case VNET_LINK_ARP:
104  break;
105  }
106  ASSERT(0);
107  return (0);
108 }
109 
110 u8 *
111 format_dpo_type (u8 * s, va_list * args)
112 {
113  dpo_type_t type = va_arg (*args, int);
114 
115  s = format(s, "%s", dpo_type_names[type]);
116 
117  return (s);
118 }
119 
120 u8 *
121 format_dpo_id (u8 * s, va_list * args)
122 {
123  dpo_id_t *dpo = va_arg (*args, dpo_id_t*);
124  u32 indent = va_arg (*args, u32);
125 
126  s = format(s, "[@%d]: ", dpo->dpoi_next_node);
127 
128  if (NULL != dpo_vfts[dpo->dpoi_type].dv_format)
129  {
130  return (format(s, "%U",
131  dpo_vfts[dpo->dpoi_type].dv_format,
132  dpo->dpoi_index,
133  indent));
134  }
135 
136  switch (dpo->dpoi_type)
137  {
138  case DPO_FIRST:
139  s = format(s, "unset");
140  break;
141  default:
142  s = format(s, "unknown");
143  break;
144  }
145  return (s);
146 }
147 
148 u8 *
149 format_dpo_proto (u8 * s, va_list * args)
150 {
151  dpo_proto_t proto = va_arg (*args, int);
152 
153  return (format(s, "%s", dpo_proto_names[proto]));
154 }
155 
156 void
158  dpo_type_t type,
159  dpo_proto_t proto,
160  index_t index)
161 {
162  dpo_id_t tmp = *dpo;
163 
164  dpo->dpoi_type = type;
165  dpo->dpoi_proto = proto,
166  dpo->dpoi_index = index;
167 
168  if (DPO_ADJACENCY == type)
169  {
170  /*
171  * set the adj subtype
172  */
173  ip_adjacency_t *adj;
174 
175  adj = adj_get(index);
176 
177  switch (adj->lookup_next_index)
178  {
179  case IP_LOOKUP_NEXT_ARP:
181  break;
184  break;
185  default:
186  break;
187  }
188  }
189  dpo_lock(dpo);
190  dpo_unlock(&tmp);
191 }
192 
193 void
195 {
196  dpo_id_t tmp = DPO_INVALID;
197 
198  /*
199  * use the atomic copy operation.
200  */
201  dpo_copy(dpo, &tmp);
202 }
203 
204 /**
205  * \brief
206  * Compare two Data-path objects
207  *
208  * like memcmp, return 0 is matching, !0 otherwise.
209  */
210 int
211 dpo_cmp (const dpo_id_t *dpo1,
212  const dpo_id_t *dpo2)
213 {
214  int res;
215 
216  res = dpo1->dpoi_type - dpo2->dpoi_type;
217 
218  if (0 != res) return (res);
219 
220  return (dpo1->dpoi_index - dpo2->dpoi_index);
221 }
222 
223 void
225  const dpo_id_t *src)
226 {
227  dpo_id_t tmp = *dst;
228 
229  /*
230  * the destination is written in a single u64 write - hence atomically w.r.t
231  * any packets inflight.
232  */
233  *((u64*)dst) = *(u64*)src;
234 
235  dpo_lock(dst);
236  dpo_unlock(&tmp);
237 }
238 
239 int
240 dpo_is_adj (const dpo_id_t *dpo)
241 {
242  return ((dpo->dpoi_type == DPO_ADJACENCY) ||
244  (dpo->dpoi_type == DPO_ADJACENCY_MIDCHAIN) ||
245  (dpo->dpoi_type == DPO_ADJACENCY_GLEAN));
246 }
247 
248 void
250  const dpo_vft_t *vft,
251  const char * const * const * nodes)
252 {
253  vec_validate(dpo_vfts, type);
254  dpo_vfts[type] = *vft;
255 
256  vec_validate(dpo_nodes, type);
257  dpo_nodes[type] = nodes;
258 }
259 
262  const char * const * const * nodes)
263 {
264  dpo_type_t type = dpo_dynamic++;
265 
266  dpo_register(type, vft, nodes);
267 
268  return (type);
269 }
270 
271 void
273 {
274  if (!dpo_id_is_valid(dpo))
275  return;
276 
277  dpo_vfts[dpo->dpoi_type].dv_lock(dpo);
278 }
279 
280 void
282 {
283  if (!dpo_id_is_valid(dpo))
284  return;
285 
286  dpo_vfts[dpo->dpoi_type].dv_unlock(dpo);
287 }
288 
289 
290 static u32
292  dpo_proto_t child_proto,
293  const dpo_id_t *parent_dpo)
294 {
295  dpo_proto_t parent_proto;
296  dpo_type_t parent_type;
297 
298  parent_type = parent_dpo->dpoi_type;
299  parent_proto = parent_dpo->dpoi_proto;
300 
301  vec_validate(dpo_edges, child_type);
302  vec_validate(dpo_edges[child_type], child_proto);
303  vec_validate(dpo_edges[child_type][child_proto], parent_type);
305  dpo_edges[child_type][child_proto][parent_type],
306  parent_proto, ~0);
307 
308  /*
309  * if the edge index has not yet been created for this node to node transistion
310  */
311  if (~0 == dpo_edges[child_type][child_proto][parent_type][parent_proto])
312  {
313  vlib_node_t *parent_node, *child_node;
314  vlib_main_t *vm;
315  u32 edge ,pp, cc;
316 
317  vm = vlib_get_main();
318 
320 
321  ASSERT(NULL != dpo_nodes[child_type]);
322  ASSERT(NULL != dpo_nodes[child_type][child_proto]);
323  ASSERT(NULL != dpo_nodes[parent_type]);
324  ASSERT(NULL != dpo_nodes[parent_type][parent_proto]);
325 
326  cc = 0;
327 
328  /*
329  * create a graph arc from each of the parent's registered node types,
330  * to each of the childs.
331  */
332  while (NULL != dpo_nodes[child_type][child_proto][cc])
333  {
334  child_node =
336  (u8*) dpo_nodes[child_type][child_proto][cc]);
337 
338  pp = 0;
339 
340  while (NULL != dpo_nodes[parent_type][parent_proto][pp])
341  {
342  parent_node =
344  (u8*) dpo_nodes[parent_type][parent_proto][pp]);
345 
346  edge = vlib_node_add_next(vm,
347  child_node->index,
348  parent_node->index);
349 
350  if (~0 == dpo_edges[child_type][child_proto][parent_type][parent_proto])
351  {
352  dpo_edges[child_type][child_proto][parent_type][parent_proto] = edge;
353  }
354  else
355  {
356  ASSERT(dpo_edges[child_type][child_proto][parent_type][parent_proto] == edge);
357  }
358  pp++;
359  }
360  cc++;
361  }
362 
364  }
365 
366  return (dpo_edges[child_type][child_proto][parent_type][parent_proto]);
367 }
368 
369 /**
370  * @brief Stack one DPO object on another, and thus establish a child parent
371  * relationship. The VLIB graph arc used is taken from the parent and child types
372  * passed.
373  */
374 static void
376  dpo_id_t *dpo,
377  const dpo_id_t *parent)
378 {
379  /*
380  * in order to get an atomic update of the parent we create a temporary,
381  * from a copy of the child, and add the next_node. then we copy to the parent
382  */
383  dpo_id_t tmp = DPO_INVALID;
384  dpo_copy(&tmp, parent);
385 
386  /*
387  * get the edge index for the parent to child VLIB graph transisition
388  */
389  tmp.dpoi_next_node = edge;
390 
391  /*
392  * this update is atomic.
393  */
394  dpo_copy(dpo, &tmp);
395 
396  dpo_reset(&tmp);
397 }
398 
399 /**
400  * @brief Stack one DPO object on another, and thus establish a child-parent
401  * relationship. The VLIB graph arc used is taken from the parent and child types
402  * passed.
403  */
404 void
405 dpo_stack (dpo_type_t child_type,
406  dpo_proto_t child_proto,
407  dpo_id_t *dpo,
408  const dpo_id_t *parent)
409 {
410  dpo_stack_i(dpo_get_next_node(child_type, child_proto, parent), dpo, parent);
411 }
412 
413 /**
414  * @brief Stack one DPO object on another, and thus establish a child parent
415  * relationship. A new VLIB graph arc is created from the child node passed
416  * to the nodes registered by the parent. The VLIB infra will ensure this arc
417  * is added only once.
418  */
419 void
420 dpo_stack_from_node (u32 child_node_index,
421  dpo_id_t *dpo,
422  const dpo_id_t *parent)
423 {
424  dpo_proto_t parent_proto;
425  vlib_node_t *parent_node;
426  dpo_type_t parent_type;
427  vlib_main_t *vm;
428  u32 edge;
429 
430  parent_type = parent->dpoi_type;
431  parent_proto = parent->dpoi_proto;
432 
433  vm = vlib_get_main();
434 
435  ASSERT(NULL != dpo_nodes[parent_type]);
436  ASSERT(NULL != dpo_nodes[parent_type][parent_proto]);
437 
438  parent_node =
439  vlib_get_node_by_name(vm, (u8*) dpo_nodes[parent_type][parent_proto][0]);
440 
441  edge = vlib_node_get_next(vm,
442  child_node_index,
443  parent_node->index);
444 
445  if (~0 == edge)
446  {
448 
449  edge = vlib_node_add_next(vm,
450  child_node_index,
451  parent_node->index);
452 
454  }
455 
456  dpo_stack_i(edge, dpo, parent);
457 }
458 
459 static clib_error_t *
461 {
471 
472  return (NULL);
473 }
474 
476 
477 static clib_error_t *
479  unformat_input_t * input,
480  vlib_cli_command_t * cmd)
481 {
482  dpo_vft_t *vft;
483 
484  vlib_cli_output (vm, "DPO memory");
485  vlib_cli_output (vm, "%=30s %=5s %=8s/%=9s totals",
486  "Name","Size", "in-use", "allocated");
487 
488  vec_foreach(vft, dpo_vfts)
489  {
490  if (NULL != vft->dv_mem_show)
491  vft->dv_mem_show();
492  }
493 
494  return (NULL);
495 }
496 
497 /* *INDENT-OFF* */
498 /*?
499  * The '<em>sh dpo memory </em>' command displays the memory usage for each
500  * data-plane object type.
501  *
502  * @cliexpar
503  * @cliexstart{show dpo memory}
504  * DPO memory
505  * Name Size in-use /allocated totals
506  * load-balance 64 12 / 12 768/768
507  * Adjacency 256 1 / 1 256/256
508  * Receive 24 5 / 5 120/120
509  * Lookup 12 0 / 0 0/0
510  * Classify 12 0 / 0 0/0
511  * MPLS label 24 0 / 0 0/0
512  * @cliexend
513 ?*/
514 VLIB_CLI_COMMAND (show_fib_memory, static) = {
515  .path = "show dpo memory",
516  .function = dpo_memory_show,
517  .short_help = "show dpo memory",
518 };
519 /* *INDENT-ON* */
void dpo_unlock(dpo_id_t *dpo)
Release a reference counting lock on the DPO.
Definition: dpo.c:281
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:436
void dpo_stack_from_node(u32 child_node_index, dpo_id_t *dpo, const dpo_id_t *parent)
Stack one DPO object on another, and thus establish a child parent relationship.
Definition: dpo.c:420
dpo_lock_fn_t dv_lock
A reference counting lock function.
Definition: dpo.h:335
static const char * dpo_type_names[]
A Data-Path Object is an object that represents actions that are applied to packets are they are swit...
Definition: dpo.c:44
A virtual function table regisitered for a DPO type.
Definition: dpo.h:330
u8 * format_dpo_type(u8 *s, va_list *args)
format a DPO type
Definition: dpo.c:111
int dpo_is_adj(const dpo_id_t *dpo)
Return TRUE is the DPO is any type of adjacency.
Definition: dpo.c:240
static dpo_type_t dpo_dynamic
The DPO type value that can be assigend to the next dynamic type registration.
Definition: dpo.c:86
static int dpo_id_is_valid(const dpo_id_t *dpoi)
Return true if the DPO object is valid, i.e.
Definition: dpo.h:185
Definitions for all things IP (v4|v6) unicast and multicast lookup related.
#define NULL
Definition: clib.h:55
dpo_proto_t dpoi_proto
the data-path protocol of the type.
Definition: dpo.h:154
u32 index
Definition: node.h:237
IP unicast adjacency.
Definition: lookup.h:193
void dpo_copy(dpo_id_t *dst, const dpo_id_t *src)
atomic copy a data-plane object.
Definition: dpo.c:224
u32 index_t
A Data-Path Object is an object that represents actions that are applied to packets are they are swit...
Definition: dpo.h:41
#define DPO_PROTOS
Definition: dpo.h:76
static const char * dpo_proto_names[]
Definition: dpo.c:45
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:418
static uword vlib_node_add_next(vlib_main_t *vm, uword node, uword next_node)
Definition: node_funcs.h:1065
static ip_adjacency_t * adj_get(adj_index_t adj_index)
Get a pointer to an adjacency object from its index.
Definition: adj.h:128
void dpo_register(dpo_type_t type, const dpo_vft_t *vft, const char *const *const *nodes)
For a given DPO type Register:
Definition: dpo.c:249
enum dpo_type_t_ dpo_type_t
Common types of data-path objects New types can be dynamically added using dpo_register_new_type() ...
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:111
dpo_proto_t vnet_link_to_dpo_proto(vnet_link_t linkt)
Definition: dpo.c:89
static u32 dpo_get_next_node(dpo_type_t child_type, dpo_proto_t child_proto, const dpo_id_t *parent_dpo)
Definition: dpo.c:291
void load_balance_module_init(void)
Definition: load_balance.c:828
#define DPO_TYPES
Definition: dpo.h:122
void receive_dpo_module_init(void)
Definition: receive_dpo.c:167
unsigned long u64
Definition: types.h:89
enum dpo_proto_t_ dpo_proto_t
Data path protocol.
dpo_type_t dpo_register_new_type(const dpo_vft_t *vft, const char *const *const *nodes)
Create and register a new DPO type.
Definition: dpo.c:261
static u32 **** dpo_edges
Vector of edge indicies from parent DPO nodes to child.
Definition: dpo.c:80
The identity of a DPO is a combination of its type and its instance number/index of objects of that t...
Definition: dpo.h:146
void ip_null_dpo_module_init(void)
Definition: ip_null_dpo.c:405
Definition: dpo.h:117
dpo_type_t dpoi_type
the type
Definition: dpo.h:150
static const char *const *const ** dpo_nodes
vector of graph node names associated with each DPO type and protocol.
Definition: dpo.c:64
struct _unformat_input_t unformat_input_t
void classify_dpo_module_init(void)
Definition: classify_dpo.c:128
void dpo_lock(dpo_id_t *dpo)
Take a reference counting lock on the DPO.
Definition: dpo.c:272
void lookup_dpo_module_init(void)
Definition: lookup_dpo.c:1185
vlib_main_t * vm
Definition: buffer.c:276
void vlib_worker_thread_barrier_sync(vlib_main_t *vm)
Definition: threads.c:1199
vlib_node_t * vlib_get_node_by_name(vlib_main_t *vm, u8 *name)
Definition: node.c:45
This packet matches an "incomplete adjacency" and packets need to be passed to ARP to find rewrite st...
Definition: lookup.h:73
void dpo_set(dpo_id_t *dpo, dpo_type_t type, dpo_proto_t proto, index_t index)
Set/create a DPO ID The DPO will be locked.
Definition: dpo.c:157
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:154
void mpls_label_dpo_module_init(void)
static clib_error_t * dpo_module_init(vlib_main_t *vm)
Definition: dpo.c:460
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
static dpo_vft_t * dpo_vfts
Vector of virtual function tables for the DPO types.
Definition: dpo.c:52
void punt_dpo_module_init(void)
Definition: punt_dpo.c:97
enum vnet_link_t_ vnet_link_t
Link Type: A description of the protocol of packets on the link.
A non-zero value first so we can spot unitialisation errors.
Definition: dpo.h:97
This packets follow a mid-chain adjacency.
Definition: lookup.h:89
int dpo_cmp(const dpo_id_t *dpo1, const dpo_id_t *dpo2)
Compare two Data-path objects.
Definition: dpo.c:211
u8 * format_dpo_id(u8 *s, va_list *args)
Format a DPO_id_t oject
Definition: dpo.c:121
dpo_mem_show_t dv_mem_show
A show memory usage function.
Definition: dpo.h:347
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
uword vlib_node_get_next(vlib_main_t *vm, uword node_index, uword next_node_index)
Definition: node.c:155
static clib_error_t * dpo_memory_show(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: dpo.c:478
index_t dpoi_index
the index of objects of that type
Definition: dpo.h:162
format_function_t * dv_format
A format function.
Definition: dpo.h:343
unsigned char u8
Definition: types.h:56
ip_lookup_next_t lookup_next_index
Definition: lookup.h:204
dpo_lock_fn_t dv_unlock
A reference counting unlock function.
Definition: dpo.h:339
#define DPO_INVALID
An initialiser for DPOs declared on the stack.
Definition: dpo.h:173
u8 * format_dpo_proto(u8 *s, va_list *args)
format a DPO protocol
Definition: dpo.c:149
void vlib_worker_thread_barrier_release(vlib_main_t *vm)
Definition: threads.c:1231
void dpo_reset(dpo_id_t *dpo)
reset a DPO ID The DPO will be unlocked.
Definition: dpo.c:194
#define vec_foreach(var, vec)
Vector iterator.
u16 dpoi_next_node
The next VLIB node to follow.
Definition: dpo.h:158
#define vec_validate_init_empty(V, I, INIT)
Make sure vector is long enough for given index and initialize empty space (no header, unspecified alignment)
Definition: vec.h:485
void drop_dpo_module_init(void)
Definition: drop_dpo.c:109
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:577
static void dpo_stack_i(u32 edge, dpo_id_t *dpo, const dpo_id_t *parent)
Stack one DPO object on another, and thus establish a child parent relationship.
Definition: dpo.c:375
void replicate_module_init(void)
void dpo_stack(dpo_type_t child_type, dpo_proto_t child_proto, dpo_id_t *dpo, const dpo_id_t *parent)
Stack one DPO object on another, and thus establish a child-parent relationship.
Definition: dpo.c:405