FD.io VPP  v16.12-rc0-308-g931be3a
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 
40 /**
41  * Array of char* names for the DPO types and protos
42  */
43 static const char* dpo_type_names[] = DPO_TYPES;
44 static const char* dpo_proto_names[] = DPO_PROTOS;
45 
46 /**
47  * @brief Vector of virtual function tables for the DPO types
48  *
49  * This is a vector so we can dynamically register new DPO types in plugins.
50  */
52 
53 /**
54  * @brief vector of graph node names associated with each DPO type and protocol.
55  *
56  * dpo_nodes[child_type][child_proto][node_X] = node_name;
57  * i.e.
58  * dpo_node[DPO_LOAD_BALANCE][DPO_PROTO_IP4][0] = "ip4-lookup"
59  * dpo_node[DPO_LOAD_BALANCE][DPO_PROTO_IP4][1] = "ip4-load-balance"
60  *
61  * This is a vector so we can dynamically register new DPO types in plugins.
62  */
63 static const char* const * const ** dpo_nodes;
64 
65 /**
66  * @brief Vector of edge indicies from parent DPO nodes to child
67  *
68  * dpo_edges[child_type][child_proto][parent_type][parent_proto] = edge_index
69  *
70  * This array is derived at init time from the dpo_nodes above. Note that
71  * the third dimension in dpo_nodes is lost, hence, the edge index from each
72  * node MUST be the same.
73  * Including both the child and parent protocol is required to support the
74  * case where it changes as the grapth is traversed, most notablly when an
75  * MPLS label is popped.
76  *
77  * Note that this array is child type specific, not child instance specific.
78  */
79 static u32 ****dpo_edges;
80 
81 /**
82  * @brief The DPO type value that can be assigend to the next dynamic
83  * type registration.
84  */
86 
87 u8 *
88 format_dpo_type (u8 * s, va_list * args)
89 {
90  dpo_type_t type = va_arg (*args, int);
91 
92  s = format(s, "%s", dpo_type_names[type]);
93 
94  return (s);
95 }
96 
97 u8 *
98 format_dpo_id (u8 * s, va_list * args)
99 {
100  dpo_id_t *dpo = va_arg (*args, dpo_id_t*);
101  u32 indent = va_arg (*args, u32);
102 
103  s = format(s, "[@%d]: ", dpo->dpoi_next_node);
104 
105  if (NULL != dpo_vfts[dpo->dpoi_type].dv_format)
106  {
107  return (format(s, "%U",
108  dpo_vfts[dpo->dpoi_type].dv_format,
109  dpo->dpoi_index,
110  indent));
111  }
112 
113  switch (dpo->dpoi_type)
114  {
115  case DPO_FIRST:
116  s = format(s, "unset");
117  break;
118  default:
119  s = format(s, "unknown");
120  break;
121  }
122  return (s);
123 }
124 
125 u8 *
126 format_dpo_proto (u8 * s, va_list * args)
127 {
128  dpo_proto_t proto = va_arg (*args, int);
129 
130  return (format(s, "%s", dpo_proto_names[proto]));
131 }
132 
133 void
136  dpo_proto_t proto,
137  index_t index)
138 {
139  dpo_id_t tmp = *dpo;
140 
141  dpo->dpoi_type = type;
142  dpo->dpoi_proto = proto,
143  dpo->dpoi_index = index;
144 
145  if (DPO_ADJACENCY == type)
146  {
147  /*
148  * set the adj subtype
149  */
150  ip_adjacency_t *adj;
151 
152  adj = adj_get(index);
153 
154  switch (adj->lookup_next_index)
155  {
156  case IP_LOOKUP_NEXT_ARP:
158  break;
161  break;
162  default:
163  break;
164  }
165  }
166  dpo_lock(dpo);
167  dpo_unlock(&tmp);
168 }
169 
170 void
172 {
174 }
175 
176 /**
177  * \brief
178  * Compare two Data-path objects
179  *
180  * like memcmp, return 0 is matching, !0 otherwise.
181  */
182 int
183 dpo_cmp (const dpo_id_t *dpo1,
184  const dpo_id_t *dpo2)
185 {
186  int res;
187 
188  res = dpo1->dpoi_type - dpo2->dpoi_type;
189 
190  if (0 != res) return (res);
191 
192  return (dpo1->dpoi_index - dpo2->dpoi_index);
193 }
194 
195 void
197  const dpo_id_t *src)
198 {
199  dpo_id_t tmp = *dst;
200 
201  /*
202  * the destination is written in a single u64 write - hence atomically w.r.t
203  * any packets inflight.
204  */
205  *((u64*)dst) = *(u64*)src;
206 
207  dpo_lock(dst);
208  dpo_unlock(&tmp);
209 }
210 
211 int
212 dpo_is_adj (const dpo_id_t *dpo)
213 {
214  return ((dpo->dpoi_type == DPO_ADJACENCY) ||
216  (dpo->dpoi_type == DPO_ADJACENCY_MIDCHAIN) ||
217  (dpo->dpoi_type == DPO_ADJACENCY_GLEAN));
218 }
219 
220 void
222  const dpo_vft_t *vft,
223  const char * const * const * nodes)
224 {
225  vec_validate(dpo_vfts, type);
226  dpo_vfts[type] = *vft;
227 
228  vec_validate(dpo_nodes, type);
229  dpo_nodes[type] = nodes;
230 }
231 
234  const char * const * const * nodes)
235 {
237 
238  dpo_register(type, vft, nodes);
239 
240  return (type);
241 }
242 
243 void
245 {
246  if (!dpo_id_is_valid(dpo))
247  return;
248 
249  dpo_vfts[dpo->dpoi_type].dv_lock(dpo);
250 }
251 
252 void
254 {
255  if (!dpo_id_is_valid(dpo))
256  return;
257 
258  dpo_vfts[dpo->dpoi_type].dv_unlock(dpo);
259 }
260 
261 
262 static u32
264  dpo_proto_t child_proto,
265  const dpo_id_t *parent_dpo)
266 {
267  dpo_proto_t parent_proto;
268  dpo_type_t parent_type;
269 
270  parent_type = parent_dpo->dpoi_type;
271  parent_proto = parent_dpo->dpoi_proto;
272 
273  vec_validate(dpo_edges, child_type);
274  vec_validate(dpo_edges[child_type], child_proto);
275  vec_validate(dpo_edges[child_type][child_proto], parent_type);
277  dpo_edges[child_type][child_proto][parent_type],
278  parent_proto, ~0);
279 
280  /*
281  * if the edge index has not yet been created for this node to node transistion
282  */
283  if (~0 == dpo_edges[child_type][child_proto][parent_type][parent_proto])
284  {
285  vlib_node_t *parent_node, *child_node;
286  vlib_main_t *vm;
287  u32 edge ,pp, cc;
288 
289  vm = vlib_get_main();
290 
291  ASSERT(NULL != dpo_nodes[child_type]);
292  ASSERT(NULL != dpo_nodes[child_type][child_proto]);
293  ASSERT(NULL != dpo_nodes[parent_type]);
294  ASSERT(NULL != dpo_nodes[parent_type][parent_proto]);
295 
296  cc = 0;
297 
298  /*
299  * create a graph arc from each of the parent's registered node types,
300  * to each of the childs.
301  */
302  while (NULL != dpo_nodes[child_type][child_proto][cc])
303  {
304  child_node =
306  (u8*) dpo_nodes[child_type][child_proto][cc]);
307 
308  pp = 0;
309 
310  while (NULL != dpo_nodes[parent_type][parent_proto][pp])
311  {
312  parent_node =
314  (u8*) dpo_nodes[parent_type][parent_proto][pp]);
315 
316  edge = vlib_node_add_next(vm,
317  child_node->index,
318  parent_node->index);
319 
320  if (~0 == dpo_edges[child_type][child_proto][parent_type][parent_proto])
321  {
322  dpo_edges[child_type][child_proto][parent_type][parent_proto] = edge;
323  }
324  else
325  {
326  ASSERT(dpo_edges[child_type][child_proto][parent_type][parent_proto] == edge);
327  }
328  pp++;
329  }
330  cc++;
331  }
332  }
333 
334  return (dpo_edges[child_type][child_proto][parent_type][parent_proto]);
335 }
336 
337 /**
338  * @brief Stack one DPO object on another, and thus establish a child parent
339  * relationship. The VLIB graph arc used is taken from the parent and child types
340  * passed.
341  */
342 static void
344  dpo_id_t *dpo,
345  const dpo_id_t *parent)
346 {
347  /*
348  * in order to get an atomic update of the parent we create a temporary,
349  * from a copy of the child, and add the next_node. then we copy to the parent
350  */
351  dpo_id_t tmp = DPO_INVALID;
352  dpo_copy(&tmp, parent);
353 
354  /*
355  * get the edge index for the parent to child VLIB graph transisition
356  */
357  tmp.dpoi_next_node = edge;
358 
359  /*
360  * this update is atomic.
361  */
362  dpo_copy(dpo, &tmp);
363 
364  dpo_reset(&tmp);
365 }
366 
367 /**
368  * @brief Stack one DPO object on another, and thus establish a child-parent
369  * relationship. The VLIB graph arc used is taken from the parent and child types
370  * passed.
371  */
372 void
373 dpo_stack (dpo_type_t child_type,
374  dpo_proto_t child_proto,
375  dpo_id_t *dpo,
376  const dpo_id_t *parent)
377 {
378  dpo_stack_i(dpo_get_next_node(child_type, child_proto, parent), dpo, parent);
379 }
380 
381 /**
382  * @brief Stack one DPO object on another, and thus establish a child parent
383  * relationship. A new VLIB graph arc is created from the child node passed
384  * to the nodes registered by the parent. The VLIB infra will ensure this arc
385  * is added only once.
386  */
387 void
388 dpo_stack_from_node (u32 child_node_index,
389  dpo_id_t *dpo,
390  const dpo_id_t *parent)
391 {
392  dpo_proto_t parent_proto;
393  vlib_node_t *parent_node;
394  dpo_type_t parent_type;
395  vlib_main_t *vm;
396  u32 edge;
397 
398  parent_type = parent->dpoi_type;
399  parent_proto = parent->dpoi_proto;
400 
401  vm = vlib_get_main();
402 
403  ASSERT(NULL != dpo_nodes[parent_type]);
404  ASSERT(NULL != dpo_nodes[parent_type][parent_proto]);
405 
406  parent_node =
407  vlib_get_node_by_name(vm, (u8*) dpo_nodes[parent_type][parent_proto][0]);
408 
409  edge = vlib_node_add_next(vm,
410  child_node_index,
411  parent_node->index);
412 
413  dpo_stack_i(edge, dpo, parent);
414 }
415 
416 static clib_error_t *
418 {
427 
428  return (NULL);
429 }
430 
432 
433 static clib_error_t *
435  unformat_input_t * input,
436  vlib_cli_command_t * cmd)
437 {
438  dpo_vft_t *vft;
439 
440  vlib_cli_output (vm, "DPO memory");
441  vlib_cli_output (vm, "%=30s %=5s %=8s/%=9s totals",
442  "Name","Size", "in-use", "allocated");
443 
444  vec_foreach(vft, dpo_vfts)
445  {
446  if (NULL != vft->dv_mem_show)
447  vft->dv_mem_show();
448  }
449 
450  return (NULL);
451 }
452 
453 /* *INDENT-OFF* */
454 /*?
455  * The '<em>sh dpo memory </em>' command displays the memory usage for each
456  * data-plane object type.
457  *
458  * @cliexpar
459  * @cliexstart{show dpo memory}
460  * DPO memory
461  * Name Size in-use /allocated totals
462  * load-balance 64 12 / 12 768/768
463  * Adjacency 256 1 / 1 256/256
464  * Receive 24 5 / 5 120/120
465  * Lookup 12 0 / 0 0/0
466  * Classify 12 0 / 0 0/0
467  * MPLS label 24 0 / 0 0/0
468  * @cliexend
469 ?*/
470 VLIB_CLI_COMMAND (show_fib_memory, static) = {
471  .path = "show dpo memory",
472  .function = dpo_memory_show,
473  .short_help = "show dpo memory",
474 };
475 /* *INDENT-ON* */
void dpo_unlock(dpo_id_t *dpo)
Release a reference counting lock on the DPO.
Definition: dpo.c:253
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:396
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:388
dpo_lock_fn_t dv_lock
A reference counting lock function.
Definition: dpo.h:318
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:43
A virtual function table regisitered for a DPO type.
Definition: dpo.h:313
u8 * format_dpo_type(u8 *s, va_list *args)
format a DPO type
Definition: dpo.c:88
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
int dpo_is_adj(const dpo_id_t *dpo)
Return TRUE is the DPO is any type of adjacency.
Definition: dpo.c:212
bad routing header type(not 4)") sr_error (NO_MORE_SEGMENTS
static dpo_type_t dpo_dynamic
The DPO type value that can be assigend to the next dynamic type registration.
Definition: dpo.c:85
static int dpo_id_is_valid(const dpo_id_t *dpoi)
Return true if the DPO object is valid, i.e.
Definition: dpo.h:170
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:146
u32 index
Definition: node.h:237
IP unicast adjacency.
Definition: lookup.h:174
void dpo_copy(dpo_id_t *dst, const dpo_id_t *src)
atomic copy a data-plane object.
Definition: dpo.c:196
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:75
static const char * dpo_proto_names[]
Definition: dpo.c:44
static uword vlib_node_add_next(vlib_main_t *vm, uword node, uword next_node)
Definition: node_funcs.h:1063
static ip_adjacency_t * adj_get(adj_index_t adj_index)
Get a pointer to an adjacency object from its index.
Definition: adj.h:117
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:221
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
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:263
void load_balance_module_init(void)
Definition: load_balance.c:765
#define DPO_PROTO_NONE
Definition: dpo.h:73
#define DPO_TYPES
Definition: dpo.h:117
void receive_dpo_module_init(void)
Definition: receive_dpo.c:162
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:233
static u32 **** dpo_edges
Vector of edge indicies from parent DPO nodes to child.
Definition: dpo.c:79
The identity of a DPO is a combination of its type and its instance number/index of objects of that t...
Definition: dpo.h:138
void ip_null_dpo_module_init(void)
Definition: ip_null_dpo.c:405
Definition: dpo.h:112
dpo_type_t dpoi_type
the type
Definition: dpo.h:142
static const char *const *const ** dpo_nodes
vector of graph node names associated with each DPO type and protocol.
Definition: dpo.c:63
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:244
void lookup_dpo_module_init(void)
Definition: lookup_dpo.c:1157
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:575
This packet matches an "incomplete adjacency" and packets need to be passed to ARP to find rewrite st...
Definition: lookup.h:72
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:134
#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:417
#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:51
void punt_dpo_module_init(void)
Definition: punt_dpo.c:97
A non-zero value first so we can spot unitialisation errors.
Definition: dpo.h:95
This packets follow a mid-chain adjacency.
Definition: lookup.h:88
int dpo_cmp(const dpo_id_t *dpo1, const dpo_id_t *dpo2)
Compare two Data-path objects.
Definition: dpo.c:183
u8 * format_dpo_id(u8 *s, va_list *args)
Format a DPO_id_t oject
Definition: dpo.c:98
dpo_mem_show_t dv_mem_show
A show memory usage function.
Definition: dpo.h:330
vlib_node_t * vlib_get_node_by_name(vlib_main_t *vm, u8 *name)
Definition: node.c:45
static clib_error_t * dpo_memory_show(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: dpo.c:434
index_t dpoi_index
the index of objects of that type
Definition: dpo.h:154
format_function_t * dv_format
A format function.
Definition: dpo.h:326
unsigned char u8
Definition: types.h:56
ip_lookup_next_t lookup_next_index
Definition: lookup.h:183
#define INDEX_INVALID
Invalid index - used when no index is known blazoned capitals INVALID speak volumes where ~0 does not...
Definition: dpo.h:47
dpo_lock_fn_t dv_unlock
A reference counting unlock function.
Definition: dpo.h:322
#define DPO_INVALID
An initialiser for DPos declared on the stack.
Definition: dpo.h:164
u8 * format_dpo_proto(u8 *s, va_list *args)
format a DPO protocol
Definition: dpo.c:126
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:418
void dpo_reset(dpo_id_t *dpo)
reset a DPO ID The DPO will be unlocked.
Definition: dpo.c:171
#define vec_foreach(var, vec)
Vector iterator.
u16 dpoi_next_node
The next VLIB node to follow.
Definition: dpo.h:150
struct _unformat_input_t unformat_input_t
#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:445
void drop_dpo_module_init(void)
Definition: drop_dpo.c:103
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:343
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:373