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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * A Data-Path Object is an object that represents actions that are
18 * applied to packets are they are switched through VPP.
20 * The DPO is a base class that is specialised by other objects to provide
23 * The VLIB graph nodes are graph of types, the DPO graph is a graph of instances.
26 #include <vnet/dpo/dpo.h>
27 #include <vnet/ip/lookup.h>
28 #include <vnet/ip/format.h>
29 #include <vnet/adj/adj.h>
31 #include <vnet/dpo/load_balance.h>
32 #include <vnet/dpo/mpls_label_dpo.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 #include <vnet/dpo/interface_dpo.h>
41 #include <vnet/dpo/mpls_disposition.h>
44 * Array of char* names for the DPO types and protos
46 static const char* dpo_type_names[] = DPO_TYPES;
47 static const char* dpo_proto_names[] = DPO_PROTOS;
50 * @brief Vector of virtual function tables for the DPO types
52 * This is a vector so we can dynamically register new DPO types in plugins.
54 static dpo_vft_t *dpo_vfts;
57 * @brief vector of graph node names associated with each DPO type and protocol.
59 * dpo_nodes[child_type][child_proto][node_X] = node_name;
61 * dpo_node[DPO_LOAD_BALANCE][DPO_PROTO_IP4][0] = "ip4-lookup"
62 * dpo_node[DPO_LOAD_BALANCE][DPO_PROTO_IP4][1] = "ip4-load-balance"
64 * This is a vector so we can dynamically register new DPO types in plugins.
66 static const char* const * const ** dpo_nodes;
69 * @brief Vector of edge indicies from parent DPO nodes to child
71 * dpo_edges[child_type][child_proto][parent_type][parent_proto] = edge_index
73 * This array is derived at init time from the dpo_nodes above. Note that
74 * the third dimension in dpo_nodes is lost, hence, the edge index from each
75 * node MUST be the same.
76 * Including both the child and parent protocol is required to support the
77 * case where it changes as the grapth is traversed, most notablly when an
78 * MPLS label is popped.
80 * Note that this array is child type specific, not child instance specific.
82 static u32 ****dpo_edges;
85 * @brief The DPO type value that can be assigend to the next dynamic
88 static dpo_type_t dpo_dynamic = DPO_LAST;
91 vnet_link_to_dpo_proto (vnet_link_t linkt)
96 return (DPO_PROTO_IP6);
98 return (DPO_PROTO_IP4);
100 return (DPO_PROTO_MPLS);
101 case VNET_LINK_ETHERNET:
102 return (DPO_PROTO_ETHERNET);
104 return (DPO_PROTO_NSH);
113 format_dpo_type (u8 * s, va_list * args)
115 dpo_type_t type = va_arg (*args, int);
117 s = format(s, "%s", dpo_type_names[type]);
123 format_dpo_id (u8 * s, va_list * args)
125 dpo_id_t *dpo = va_arg (*args, dpo_id_t*);
126 u32 indent = va_arg (*args, u32);
128 s = format(s, "[@%d]: ", dpo->dpoi_next_node);
130 if (NULL != dpo_vfts[dpo->dpoi_type].dv_format)
132 return (format(s, "%U",
133 dpo_vfts[dpo->dpoi_type].dv_format,
138 switch (dpo->dpoi_type)
141 s = format(s, "unset");
144 s = format(s, "unknown");
151 format_dpo_proto (u8 * s, va_list * args)
153 dpo_proto_t proto = va_arg (*args, int);
155 return (format(s, "%s", dpo_proto_names[proto]));
159 dpo_set (dpo_id_t *dpo,
166 dpo->dpoi_type = type;
167 dpo->dpoi_proto = proto,
168 dpo->dpoi_index = index;
170 if (DPO_ADJACENCY == type)
173 * set the adj subtype
177 adj = adj_get(index);
179 switch (adj->lookup_next_index)
181 case IP_LOOKUP_NEXT_ARP:
182 dpo->dpoi_type = DPO_ADJACENCY_INCOMPLETE;
184 case IP_LOOKUP_NEXT_MIDCHAIN:
185 dpo->dpoi_type = DPO_ADJACENCY_MIDCHAIN;
187 case IP_LOOKUP_NEXT_MCAST_MIDCHAIN:
188 dpo->dpoi_type = DPO_ADJACENCY_MCAST_MIDCHAIN;
190 case IP_LOOKUP_NEXT_MCAST:
191 dpo->dpoi_type = DPO_ADJACENCY_MCAST;
193 case IP_LOOKUP_NEXT_GLEAN:
194 dpo->dpoi_type = DPO_ADJACENCY_GLEAN;
205 dpo_reset (dpo_id_t *dpo)
207 dpo_id_t tmp = DPO_INVALID;
210 * use the atomic copy operation.
217 * Compare two Data-path objects
219 * like memcmp, return 0 is matching, !0 otherwise.
222 dpo_cmp (const dpo_id_t *dpo1,
223 const dpo_id_t *dpo2)
227 res = dpo1->dpoi_type - dpo2->dpoi_type;
229 if (0 != res) return (res);
231 return (dpo1->dpoi_index - dpo2->dpoi_index);
235 dpo_copy (dpo_id_t *dst,
241 * the destination is written in a single u64 write - hence atomically w.r.t
242 * any packets inflight.
244 *((u64*)dst) = *(u64*)src;
251 dpo_is_adj (const dpo_id_t *dpo)
253 return ((dpo->dpoi_type == DPO_ADJACENCY) ||
254 (dpo->dpoi_type == DPO_ADJACENCY_INCOMPLETE) ||
255 (dpo->dpoi_type == DPO_ADJACENCY_MIDCHAIN) ||
256 (dpo->dpoi_type == DPO_ADJACENCY_GLEAN));
260 dpo_register (dpo_type_t type,
261 const dpo_vft_t *vft,
262 const char * const * const * nodes)
264 vec_validate(dpo_vfts, type);
265 dpo_vfts[type] = *vft;
267 vec_validate(dpo_nodes, type);
268 dpo_nodes[type] = nodes;
272 dpo_register_new_type (const dpo_vft_t *vft,
273 const char * const * const * nodes)
275 dpo_type_t type = dpo_dynamic++;
277 dpo_register(type, vft, nodes);
283 dpo_lock (dpo_id_t *dpo)
285 if (!dpo_id_is_valid(dpo))
288 dpo_vfts[dpo->dpoi_type].dv_lock(dpo);
292 dpo_unlock (dpo_id_t *dpo)
294 if (!dpo_id_is_valid(dpo))
297 dpo_vfts[dpo->dpoi_type].dv_unlock(dpo);
302 dpo_get_next_node (dpo_type_t child_type,
303 dpo_proto_t child_proto,
304 const dpo_id_t *parent_dpo)
306 dpo_proto_t parent_proto;
307 dpo_type_t parent_type;
309 parent_type = parent_dpo->dpoi_type;
310 parent_proto = parent_dpo->dpoi_proto;
312 vec_validate(dpo_edges, child_type);
313 vec_validate(dpo_edges[child_type], child_proto);
314 vec_validate(dpo_edges[child_type][child_proto], parent_type);
315 vec_validate_init_empty(
316 dpo_edges[child_type][child_proto][parent_type],
320 * if the edge index has not yet been created for this node to node transistion
322 if (~0 == dpo_edges[child_type][child_proto][parent_type][parent_proto])
324 vlib_node_t *parent_node, *child_node;
328 vm = vlib_get_main();
330 vlib_worker_thread_barrier_sync(vm);
332 ASSERT(NULL != dpo_nodes[child_type]);
333 ASSERT(NULL != dpo_nodes[child_type][child_proto]);
334 ASSERT(NULL != dpo_nodes[parent_type]);
335 ASSERT(NULL != dpo_nodes[parent_type][parent_proto]);
340 * create a graph arc from each of the parent's registered node types,
341 * to each of the childs.
343 while (NULL != dpo_nodes[child_type][child_proto][cc])
346 vlib_get_node_by_name(vm,
347 (u8*) dpo_nodes[child_type][child_proto][cc]);
351 while (NULL != dpo_nodes[parent_type][parent_proto][pp])
354 vlib_get_node_by_name(vm,
355 (u8*) dpo_nodes[parent_type][parent_proto][pp]);
357 edge = vlib_node_add_next(vm,
361 if (~0 == dpo_edges[child_type][child_proto][parent_type][parent_proto])
363 dpo_edges[child_type][child_proto][parent_type][parent_proto] = edge;
367 ASSERT(dpo_edges[child_type][child_proto][parent_type][parent_proto] == edge);
374 vlib_worker_thread_barrier_release(vm);
377 return (dpo_edges[child_type][child_proto][parent_type][parent_proto]);
381 * @brief Stack one DPO object on another, and thus establish a child parent
382 * relationship. The VLIB graph arc used is taken from the parent and child types
386 dpo_stack_i (u32 edge,
388 const dpo_id_t *parent)
391 * in order to get an atomic update of the parent we create a temporary,
392 * from a copy of the child, and add the next_node. then we copy to the parent
394 dpo_id_t tmp = DPO_INVALID;
395 dpo_copy(&tmp, parent);
398 * get the edge index for the parent to child VLIB graph transisition
400 tmp.dpoi_next_node = edge;
403 * this update is atomic.
411 * @brief Stack one DPO object on another, and thus establish a child-parent
412 * relationship. The VLIB graph arc used is taken from the parent and child types
416 dpo_stack (dpo_type_t child_type,
417 dpo_proto_t child_proto,
419 const dpo_id_t *parent)
421 dpo_stack_i(dpo_get_next_node(child_type, child_proto, parent), dpo, parent);
425 * @brief Stack one DPO object on another, and thus establish a child parent
426 * relationship. A new VLIB graph arc is created from the child node passed
427 * to the nodes registered by the parent. The VLIB infra will ensure this arc
428 * is added only once.
431 dpo_stack_from_node (u32 child_node_index,
433 const dpo_id_t *parent)
435 dpo_proto_t parent_proto;
436 vlib_node_t *parent_node;
437 dpo_type_t parent_type;
441 parent_type = parent->dpoi_type;
442 parent_proto = parent->dpoi_proto;
444 vm = vlib_get_main();
446 ASSERT(NULL != dpo_nodes[parent_type]);
447 ASSERT(NULL != dpo_nodes[parent_type][parent_proto]);
450 vlib_get_node_by_name(vm, (u8*) dpo_nodes[parent_type][parent_proto][0]);
452 edge = vlib_node_get_next(vm,
458 vlib_worker_thread_barrier_sync(vm);
460 edge = vlib_node_add_next(vm,
464 vlib_worker_thread_barrier_release(vm);
467 dpo_stack_i(edge, dpo, parent);
470 static clib_error_t *
471 dpo_module_init (vlib_main_t * vm)
473 drop_dpo_module_init();
474 punt_dpo_module_init();
475 receive_dpo_module_init();
476 load_balance_module_init();
477 mpls_label_dpo_module_init();
478 classify_dpo_module_init();
479 lookup_dpo_module_init();
480 ip_null_dpo_module_init();
481 replicate_module_init();
482 interface_dpo_module_init();
483 mpls_disp_dpo_module_init();
488 VLIB_INIT_FUNCTION(dpo_module_init);
490 static clib_error_t *
491 dpo_memory_show (vlib_main_t * vm,
492 unformat_input_t * input,
493 vlib_cli_command_t * cmd)
497 vlib_cli_output (vm, "DPO memory");
498 vlib_cli_output (vm, "%=30s %=5s %=8s/%=9s totals",
499 "Name","Size", "in-use", "allocated");
501 vec_foreach(vft, dpo_vfts)
503 if (NULL != vft->dv_mem_show)
512 * The '<em>sh dpo memory </em>' command displays the memory usage for each
513 * data-plane object type.
516 * @cliexstart{show dpo memory}
518 * Name Size in-use /allocated totals
519 * load-balance 64 12 / 12 768/768
520 * Adjacency 256 1 / 1 256/256
521 * Receive 24 5 / 5 120/120
522 * Lookup 12 0 / 0 0/0
523 * Classify 12 0 / 0 0/0
524 * MPLS label 24 0 / 0 0/0
527 VLIB_CLI_COMMAND (show_fib_memory, static) = {
528 .path = "show dpo memory",
529 .function = dpo_memory_show,
530 .short_help = "show dpo memory",