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.
28 #include <vnet/dpo/dpo.h>
29 #include <vnet/ip/lookup.h>
30 #include <vnet/ip/format.h>
31 #include <vnet/adj/adj.h>
33 #include <vnet/dpo/load_balance.h>
34 #include <vnet/dpo/mpls_label_dpo.h>
35 #include <vnet/dpo/lookup_dpo.h>
36 #include <vnet/dpo/drop_dpo.h>
37 #include <vnet/dpo/receive_dpo.h>
38 #include <vnet/dpo/punt_dpo.h>
39 #include <vnet/dpo/classify_dpo.h>
40 #include <vnet/dpo/ip_null_dpo.h>
41 #include <vnet/dpo/replicate_dpo.h>
42 #include <vnet/dpo/interface_rx_dpo.h>
43 #include <vnet/dpo/interface_tx_dpo.h>
44 #include <vnet/dpo/mpls_disposition.h>
45 #include <vnet/dpo/dvr_dpo.h>
46 #include <vnet/dpo/l3_proxy_dpo.h>
47 #include <vnet/dpo/ip6_ll_dpo.h>
48 #include <vnet/dpo/pw_cw.h>
51 * Array of char* names for the DPO types and protos
53 static const char* dpo_type_names[] = DPO_TYPES;
54 static const char* dpo_proto_names[] = DPO_PROTOS;
57 * @brief Vector of virtual function tables for the DPO types
59 * This is a vector so we can dynamically register new DPO types in plugins.
61 static dpo_vft_t *dpo_vfts;
64 * @brief vector of graph node names associated with each DPO type and protocol.
66 * dpo_nodes[child_type][child_proto][node_X] = node_name;
68 * dpo_node[DPO_LOAD_BALANCE][DPO_PROTO_IP4][0] = "ip4-lookup"
69 * dpo_node[DPO_LOAD_BALANCE][DPO_PROTO_IP4][1] = "ip4-load-balance"
71 * This is a vector so we can dynamically register new DPO types in plugins.
73 static const char* const * const ** dpo_nodes;
76 * @brief Vector of edge indicies from parent DPO nodes to child
78 * dpo_edges[child_type][child_proto][parent_type][parent_proto] = edge_index
80 * This array is derived at init time from the dpo_nodes above. Note that
81 * the third dimension in dpo_nodes is lost, hence, the edge index from each
82 * node MUST be the same.
83 * Including both the child and parent protocol is required to support the
84 * case where it changes as the graph is traversed, most notably when an
85 * MPLS label is popped.
87 * Note that this array is child type specific, not child instance specific.
89 static u32 ****dpo_edges;
92 * @brief The DPO type value that can be assigned to the next dynamic
95 static dpo_type_t dpo_dynamic = DPO_LAST;
98 vnet_link_to_dpo_proto (vnet_link_t linkt)
103 return (DPO_PROTO_IP6);
105 return (DPO_PROTO_IP4);
107 return (DPO_PROTO_MPLS);
108 case VNET_LINK_ETHERNET:
109 return (DPO_PROTO_ETHERNET);
111 return (DPO_PROTO_NSH);
120 dpo_proto_to_link (dpo_proto_t dp)
125 return (VNET_LINK_IP6);
127 return (VNET_LINK_IP4);
130 return (VNET_LINK_MPLS);
131 case DPO_PROTO_ETHERNET:
132 return (VNET_LINK_ETHERNET);
134 return (VNET_LINK_NSH);
140 format_dpo_type (u8 * s, va_list * args)
142 dpo_type_t type = va_arg (*args, int);
144 s = format(s, "%s", dpo_type_names[type]);
150 format_dpo_id (u8 * s, va_list * args)
152 dpo_id_t *dpo = va_arg (*args, dpo_id_t*);
153 u32 indent = va_arg (*args, u32);
155 s = format(s, "[@%d]: ", dpo->dpoi_next_node);
157 if (NULL != dpo_vfts[dpo->dpoi_type].dv_format)
160 dpo_vfts[dpo->dpoi_type].dv_format,
166 switch (dpo->dpoi_type)
169 s = format(s, "unset");
172 s = format(s, "unknown");
180 format_dpo_proto (u8 * s, va_list * args)
182 dpo_proto_t proto = va_arg (*args, int);
184 return (format(s, "%s", dpo_proto_names[proto]));
188 dpo_set (dpo_id_t *dpo,
195 dpo->dpoi_type = type;
196 dpo->dpoi_proto = proto,
197 dpo->dpoi_index = index;
199 if (DPO_ADJACENCY == type)
202 * set the adj subtype
206 adj = adj_get(index);
208 switch (adj->lookup_next_index)
210 case IP_LOOKUP_NEXT_ARP:
211 dpo->dpoi_type = DPO_ADJACENCY_INCOMPLETE;
213 case IP_LOOKUP_NEXT_MIDCHAIN:
214 dpo->dpoi_type = DPO_ADJACENCY_MIDCHAIN;
216 case IP_LOOKUP_NEXT_MCAST_MIDCHAIN:
217 dpo->dpoi_type = DPO_ADJACENCY_MCAST_MIDCHAIN;
219 case IP_LOOKUP_NEXT_MCAST:
220 dpo->dpoi_type = DPO_ADJACENCY_MCAST;
222 case IP_LOOKUP_NEXT_GLEAN:
223 dpo->dpoi_type = DPO_ADJACENCY_GLEAN;
234 dpo_reset (dpo_id_t *dpo)
236 dpo_id_t tmp = DPO_INVALID;
239 * use the atomic copy operation.
246 * Compare two Data-path objects
248 * like memcmp, return 0 is matching, !0 otherwise.
251 dpo_cmp (const dpo_id_t *dpo1,
252 const dpo_id_t *dpo2)
256 res = dpo1->dpoi_type - dpo2->dpoi_type;
258 if (0 != res) return (res);
260 return (dpo1->dpoi_index - dpo2->dpoi_index);
264 dpo_copy (dpo_id_t *dst,
268 .as_u64 = dst->as_u64
272 * the destination is written in a single u64 write - hence atomically w.r.t
273 * any packets inflight.
275 dst->as_u64 = src->as_u64;
282 dpo_is_adj (const dpo_id_t *dpo)
284 return ((dpo->dpoi_type == DPO_ADJACENCY) ||
285 (dpo->dpoi_type == DPO_ADJACENCY_INCOMPLETE) ||
286 (dpo->dpoi_type == DPO_ADJACENCY_GLEAN) ||
287 (dpo->dpoi_type == DPO_ADJACENCY_MCAST) ||
288 (dpo->dpoi_type == DPO_ADJACENCY_MCAST_MIDCHAIN) ||
289 (dpo->dpoi_type == DPO_ADJACENCY_MIDCHAIN) ||
290 (dpo->dpoi_type == DPO_ADJACENCY_GLEAN));
294 dpo_default_get_next_node (const dpo_id_t *dpo)
296 u32 *node_indices = NULL;
297 const char *node_name;
300 node_name = dpo_nodes[dpo->dpoi_type][dpo->dpoi_proto][ii];
301 while (NULL != node_name)
305 node = vlib_get_node_by_name(vlib_get_main(), (u8*) node_name);
306 ASSERT(NULL != node);
307 vec_add1(node_indices, node->index);
310 node_name = dpo_nodes[dpo->dpoi_type][dpo->dpoi_proto][ii];
313 return (node_indices);
317 * A default variant of the make interpose function that just returns
321 dpo_default_mk_interpose (const dpo_id_t *original,
322 const dpo_id_t *parent,
325 dpo_copy(clone, original);
329 dpo_register (dpo_type_t type,
330 const dpo_vft_t *vft,
331 const char * const * const * nodes)
333 vec_validate(dpo_vfts, type);
334 dpo_vfts[type] = *vft;
335 if (NULL == dpo_vfts[type].dv_get_next_node)
337 dpo_vfts[type].dv_get_next_node = dpo_default_get_next_node;
339 if (NULL == dpo_vfts[type].dv_mk_interpose)
341 dpo_vfts[type].dv_mk_interpose = dpo_default_mk_interpose;
344 vec_validate(dpo_nodes, type);
345 dpo_nodes[type] = nodes;
349 dpo_register_new_type (const dpo_vft_t *vft,
350 const char * const * const * nodes)
352 dpo_type_t type = dpo_dynamic++;
354 dpo_register(type, vft, nodes);
360 dpo_mk_interpose (const dpo_id_t *original,
361 const dpo_id_t *parent,
364 if (!dpo_id_is_valid(original))
367 dpo_vfts[original->dpoi_type].dv_mk_interpose(original, parent, clone);
371 dpo_lock (dpo_id_t *dpo)
373 if (!dpo_id_is_valid(dpo))
376 dpo_vfts[dpo->dpoi_type].dv_lock(dpo);
380 dpo_unlock (dpo_id_t *dpo)
382 if (!dpo_id_is_valid(dpo))
385 dpo_vfts[dpo->dpoi_type].dv_unlock(dpo);
389 dpo_get_urpf(const dpo_id_t *dpo)
391 if (dpo_id_is_valid(dpo) &&
392 (NULL != dpo_vfts[dpo->dpoi_type].dv_get_urpf))
394 return (dpo_vfts[dpo->dpoi_type].dv_get_urpf(dpo));
401 dpo_get_mtu(const dpo_id_t *dpo)
403 if (dpo_id_is_valid(dpo) &&
404 (NULL != dpo_vfts[dpo->dpoi_type].dv_get_mtu))
406 return (dpo_vfts[dpo->dpoi_type].dv_get_mtu(dpo));
413 dpo_get_next_node (dpo_type_t child_type,
414 dpo_proto_t child_proto,
415 const dpo_id_t *parent_dpo)
417 dpo_proto_t parent_proto;
418 dpo_type_t parent_type;
420 parent_type = parent_dpo->dpoi_type;
421 parent_proto = parent_dpo->dpoi_proto;
423 vec_validate(dpo_edges, child_type);
424 vec_validate(dpo_edges[child_type], child_proto);
425 vec_validate(dpo_edges[child_type][child_proto], parent_type);
426 vec_validate_init_empty(
427 dpo_edges[child_type][child_proto][parent_type],
431 * if the edge index has not yet been created for this node to node transition
433 if (~0 == dpo_edges[child_type][child_proto][parent_type][parent_proto])
435 vlib_node_t *child_node;
440 vm = vlib_get_main();
442 ASSERT(NULL != dpo_vfts[parent_type].dv_get_next_node);
443 ASSERT(NULL != dpo_nodes[child_type]);
444 ASSERT(NULL != dpo_nodes[child_type][child_proto]);
447 parent_indices = dpo_vfts[parent_type].dv_get_next_node(parent_dpo);
449 vlib_worker_thread_barrier_sync(vm);
452 * create a graph arc from each of the child's registered node types,
453 * to each of the parent's.
455 while (NULL != dpo_nodes[child_type][child_proto][cc])
458 vlib_get_node_by_name(vm,
459 (u8*) dpo_nodes[child_type][child_proto][cc]);
461 vec_foreach(pi, parent_indices)
463 edge = vlib_node_add_next(vm, child_node->index, *pi);
465 if (~0 == dpo_edges[child_type][child_proto][parent_type][parent_proto])
467 dpo_edges[child_type][child_proto][parent_type][parent_proto] = edge;
471 ASSERT(dpo_edges[child_type][child_proto][parent_type][parent_proto] == edge);
477 vlib_worker_thread_barrier_release(vm);
478 vec_free(parent_indices);
481 return (dpo_edges[child_type][child_proto][parent_type][parent_proto]);
485 * @brief return already stacked up next node index for a given
486 * child_type/child_proto and parent_type/patent_proto.
487 * The VLIB graph arc used is taken from the parent and child types
491 dpo_get_next_node_by_type_and_proto (dpo_type_t child_type,
492 dpo_proto_t child_proto,
493 dpo_type_t parent_type,
494 dpo_proto_t parent_proto)
496 return (dpo_edges[child_type][child_proto][parent_type][parent_proto]);
500 * @brief Stack one DPO object on another, and thus establish a child parent
501 * relationship. The VLIB graph arc used is taken from the parent and child types
505 dpo_stack_i (u32 edge,
507 const dpo_id_t *parent)
510 * in order to get an atomic update of the parent we create a temporary,
511 * from a copy of the child, and add the next_node. then we copy to the parent
513 dpo_id_t tmp = DPO_INVALID;
514 dpo_copy(&tmp, parent);
517 * get the edge index for the parent to child VLIB graph transition
519 tmp.dpoi_next_node = edge;
522 * this update is atomic.
530 * @brief Stack one DPO object on another, and thus establish a child-parent
531 * relationship. The VLIB graph arc used is taken from the parent and child types
535 dpo_stack (dpo_type_t child_type,
536 dpo_proto_t child_proto,
538 const dpo_id_t *parent)
540 dpo_stack_i(dpo_get_next_node(child_type, child_proto, parent), dpo, parent);
544 * @brief Stack one DPO object on another, and thus establish a child parent
545 * relationship. A new VLIB graph arc is created from the child node passed
546 * to the nodes registered by the parent. The VLIB infra will ensure this arc
547 * is added only once.
550 dpo_stack_from_node (u32 child_node_index,
552 const dpo_id_t *parent)
554 dpo_type_t parent_type;
560 parent_type = parent->dpoi_type;
561 vm = vlib_get_main();
563 ASSERT(NULL != dpo_vfts[parent_type].dv_get_next_node);
564 parent_indices = dpo_vfts[parent_type].dv_get_next_node(parent);
565 ASSERT(parent_indices);
568 * This loop is purposefully written with the worker thread lock in the
569 * inner loop because;
570 * 1) the likelihood that the edge does not exist is smaller
571 * 2) the likelihood there is more than one node is even smaller
572 * so we are optimising for not need to take the lock
574 vec_foreach(pi, parent_indices)
576 edge = vlib_node_get_next(vm, child_node_index, *pi);
580 vlib_worker_thread_barrier_sync(vm);
582 edge = vlib_node_add_next(vm, child_node_index, *pi);
584 vlib_worker_thread_barrier_release(vm);
587 dpo_stack_i(edge, dpo, parent);
589 /* should free this local vector to avoid memory leak */
590 vec_free(parent_indices);
593 static clib_error_t *
594 dpo_module_init (vlib_main_t * vm)
596 drop_dpo_module_init();
597 punt_dpo_module_init();
598 receive_dpo_module_init();
599 load_balance_module_init();
600 mpls_label_dpo_module_init();
601 classify_dpo_module_init();
602 lookup_dpo_module_init();
603 ip_null_dpo_module_init();
604 ip6_ll_dpo_module_init();
605 replicate_module_init();
606 interface_rx_dpo_module_init();
607 interface_tx_dpo_module_init();
608 mpls_disp_dpo_module_init();
609 dvr_dpo_module_init();
610 l3_proxy_dpo_module_init();
611 pw_cw_dpo_module_init();
617 VLIB_INIT_FUNCTION(dpo_module_init) =
619 .runs_before = VLIB_INITS ("ip_main_init"),
623 static clib_error_t *
624 dpo_memory_show (vlib_main_t * vm,
625 unformat_input_t * input,
626 vlib_cli_command_t * cmd)
630 vlib_cli_output (vm, "DPO memory");
631 vlib_cli_output (vm, "%=30s %=5s %=8s/%=9s totals",
632 "Name","Size", "in-use", "allocated");
634 vec_foreach(vft, dpo_vfts)
636 if (NULL != vft->dv_mem_show)
645 * The '<em>sh dpo memory </em>' command displays the memory usage for each
646 * data-plane object type.
649 * @cliexstart{show dpo memory}
651 * Name Size in-use /allocated totals
652 * load-balance 64 12 / 12 768/768
653 * Adjacency 256 1 / 1 256/256
654 * Receive 24 5 / 5 120/120
655 * Lookup 12 0 / 0 0/0
656 * Classify 12 0 / 0 0/0
657 * MPLS label 24 0 / 0 0/0
660 VLIB_CLI_COMMAND (show_fib_memory, static) = {
661 .path = "show dpo memory",
662 .function = dpo_memory_show,
663 .short_help = "show dpo memory",