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_rx_dpo.h>
41 #include <vnet/dpo/interface_tx_dpo.h>
42 #include <vnet/dpo/mpls_disposition.h>
43 #include <vnet/dpo/dvr_dpo.h>
44 #include <vnet/dpo/l3_proxy_dpo.h>
45 #include <vnet/dpo/ip6_ll_dpo.h>
46 #include <vnet/dpo/pw_cw.h>
49 * Array of char* names for the DPO types and protos
51 static const char* dpo_type_names[] = DPO_TYPES;
52 static const char* dpo_proto_names[] = DPO_PROTOS;
55 * @brief Vector of virtual function tables for the DPO types
57 * This is a vector so we can dynamically register new DPO types in plugins.
59 static dpo_vft_t *dpo_vfts;
62 * @brief vector of graph node names associated with each DPO type and protocol.
64 * dpo_nodes[child_type][child_proto][node_X] = node_name;
66 * dpo_node[DPO_LOAD_BALANCE][DPO_PROTO_IP4][0] = "ip4-lookup"
67 * dpo_node[DPO_LOAD_BALANCE][DPO_PROTO_IP4][1] = "ip4-load-balance"
69 * This is a vector so we can dynamically register new DPO types in plugins.
71 static const char* const * const ** dpo_nodes;
74 * @brief Vector of edge indicies from parent DPO nodes to child
76 * dpo_edges[child_type][child_proto][parent_type][parent_proto] = edge_index
78 * This array is derived at init time from the dpo_nodes above. Note that
79 * the third dimension in dpo_nodes is lost, hence, the edge index from each
80 * node MUST be the same.
81 * Including both the child and parent protocol is required to support the
82 * case where it changes as the graph is traversed, most notably when an
83 * MPLS label is popped.
85 * Note that this array is child type specific, not child instance specific.
87 static u32 ****dpo_edges;
90 * @brief The DPO type value that can be assigned to the next dynamic
93 static dpo_type_t dpo_dynamic = DPO_LAST;
96 vnet_link_to_dpo_proto (vnet_link_t linkt)
101 return (DPO_PROTO_IP6);
103 return (DPO_PROTO_IP4);
105 return (DPO_PROTO_MPLS);
106 case VNET_LINK_ETHERNET:
107 return (DPO_PROTO_ETHERNET);
109 return (DPO_PROTO_NSH);
118 dpo_proto_to_link (dpo_proto_t dp)
123 return (VNET_LINK_IP6);
125 return (VNET_LINK_IP4);
128 return (VNET_LINK_MPLS);
129 case DPO_PROTO_ETHERNET:
130 return (VNET_LINK_ETHERNET);
132 return (VNET_LINK_NSH);
138 format_dpo_type (u8 * s, va_list * args)
140 dpo_type_t type = va_arg (*args, int);
142 s = format(s, "%s", dpo_type_names[type]);
148 format_dpo_id (u8 * s, va_list * args)
150 dpo_id_t *dpo = va_arg (*args, dpo_id_t*);
151 u32 indent = va_arg (*args, u32);
153 s = format(s, "[@%d]: ", dpo->dpoi_next_node);
155 if (NULL != dpo_vfts[dpo->dpoi_type].dv_format)
158 dpo_vfts[dpo->dpoi_type].dv_format,
164 switch (dpo->dpoi_type)
167 s = format(s, "unset");
170 s = format(s, "unknown");
178 format_dpo_proto (u8 * s, va_list * args)
180 dpo_proto_t proto = va_arg (*args, int);
182 return (format(s, "%s", dpo_proto_names[proto]));
186 dpo_set (dpo_id_t *dpo,
193 dpo->dpoi_type = type;
194 dpo->dpoi_proto = proto,
195 dpo->dpoi_index = index;
197 if (DPO_ADJACENCY == type)
200 * set the adj subtype
204 adj = adj_get(index);
206 switch (adj->lookup_next_index)
208 case IP_LOOKUP_NEXT_ARP:
209 dpo->dpoi_type = DPO_ADJACENCY_INCOMPLETE;
211 case IP_LOOKUP_NEXT_MIDCHAIN:
212 dpo->dpoi_type = DPO_ADJACENCY_MIDCHAIN;
214 case IP_LOOKUP_NEXT_MCAST_MIDCHAIN:
215 dpo->dpoi_type = DPO_ADJACENCY_MCAST_MIDCHAIN;
217 case IP_LOOKUP_NEXT_MCAST:
218 dpo->dpoi_type = DPO_ADJACENCY_MCAST;
220 case IP_LOOKUP_NEXT_GLEAN:
221 dpo->dpoi_type = DPO_ADJACENCY_GLEAN;
232 dpo_reset (dpo_id_t *dpo)
234 dpo_id_t tmp = DPO_INVALID;
237 * use the atomic copy operation.
244 * Compare two Data-path objects
246 * like memcmp, return 0 is matching, !0 otherwise.
249 dpo_cmp (const dpo_id_t *dpo1,
250 const dpo_id_t *dpo2)
254 res = dpo1->dpoi_type - dpo2->dpoi_type;
256 if (0 != res) return (res);
258 return (dpo1->dpoi_index - dpo2->dpoi_index);
262 dpo_copy (dpo_id_t *dst,
266 .as_u64 = dst->as_u64
270 * the destination is written in a single u64 write - hence atomically w.r.t
271 * any packets inflight.
273 dst->as_u64 = src->as_u64;
280 dpo_is_adj (const dpo_id_t *dpo)
282 return ((dpo->dpoi_type == DPO_ADJACENCY) ||
283 (dpo->dpoi_type == DPO_ADJACENCY_INCOMPLETE) ||
284 (dpo->dpoi_type == DPO_ADJACENCY_GLEAN) ||
285 (dpo->dpoi_type == DPO_ADJACENCY_MCAST) ||
286 (dpo->dpoi_type == DPO_ADJACENCY_MCAST_MIDCHAIN) ||
287 (dpo->dpoi_type == DPO_ADJACENCY_MIDCHAIN) ||
288 (dpo->dpoi_type == DPO_ADJACENCY_GLEAN));
292 dpo_default_get_next_node (const dpo_id_t *dpo)
294 u32 *node_indices = NULL;
295 const char *node_name;
298 node_name = dpo_nodes[dpo->dpoi_type][dpo->dpoi_proto][ii];
299 while (NULL != node_name)
303 node = vlib_get_node_by_name(vlib_get_main(), (u8*) node_name);
304 ASSERT(NULL != node);
305 vec_add1(node_indices, node->index);
308 node_name = dpo_nodes[dpo->dpoi_type][dpo->dpoi_proto][ii];
311 return (node_indices);
315 * A default variant of the make interpose function that just returns
319 dpo_default_mk_interpose (const dpo_id_t *original,
320 const dpo_id_t *parent,
323 dpo_copy(clone, original);
327 dpo_register (dpo_type_t type,
328 const dpo_vft_t *vft,
329 const char * const * const * nodes)
331 vec_validate(dpo_vfts, type);
332 dpo_vfts[type] = *vft;
333 if (NULL == dpo_vfts[type].dv_get_next_node)
335 dpo_vfts[type].dv_get_next_node = dpo_default_get_next_node;
337 if (NULL == dpo_vfts[type].dv_mk_interpose)
339 dpo_vfts[type].dv_mk_interpose = dpo_default_mk_interpose;
342 vec_validate(dpo_nodes, type);
343 dpo_nodes[type] = nodes;
347 dpo_register_new_type (const dpo_vft_t *vft,
348 const char * const * const * nodes)
350 dpo_type_t type = dpo_dynamic++;
352 dpo_register(type, vft, nodes);
358 dpo_mk_interpose (const dpo_id_t *original,
359 const dpo_id_t *parent,
362 if (!dpo_id_is_valid(original))
365 dpo_vfts[original->dpoi_type].dv_mk_interpose(original, parent, clone);
369 dpo_lock (dpo_id_t *dpo)
371 if (!dpo_id_is_valid(dpo))
374 dpo_vfts[dpo->dpoi_type].dv_lock(dpo);
378 dpo_unlock (dpo_id_t *dpo)
380 if (!dpo_id_is_valid(dpo))
383 dpo_vfts[dpo->dpoi_type].dv_unlock(dpo);
387 dpo_get_urpf(const dpo_id_t *dpo)
389 if (dpo_id_is_valid(dpo) &&
390 (NULL != dpo_vfts[dpo->dpoi_type].dv_get_urpf))
392 return (dpo_vfts[dpo->dpoi_type].dv_get_urpf(dpo));
399 dpo_get_next_node (dpo_type_t child_type,
400 dpo_proto_t child_proto,
401 const dpo_id_t *parent_dpo)
403 dpo_proto_t parent_proto;
404 dpo_type_t parent_type;
406 parent_type = parent_dpo->dpoi_type;
407 parent_proto = parent_dpo->dpoi_proto;
409 vec_validate(dpo_edges, child_type);
410 vec_validate(dpo_edges[child_type], child_proto);
411 vec_validate(dpo_edges[child_type][child_proto], parent_type);
412 vec_validate_init_empty(
413 dpo_edges[child_type][child_proto][parent_type],
417 * if the edge index has not yet been created for this node to node transition
419 if (~0 == dpo_edges[child_type][child_proto][parent_type][parent_proto])
421 vlib_node_t *child_node;
426 vm = vlib_get_main();
428 ASSERT(NULL != dpo_vfts[parent_type].dv_get_next_node);
429 ASSERT(NULL != dpo_nodes[child_type]);
430 ASSERT(NULL != dpo_nodes[child_type][child_proto]);
433 parent_indices = dpo_vfts[parent_type].dv_get_next_node(parent_dpo);
435 vlib_worker_thread_barrier_sync(vm);
438 * create a graph arc from each of the child's registered node types,
439 * to each of the parent's.
441 while (NULL != dpo_nodes[child_type][child_proto][cc])
444 vlib_get_node_by_name(vm,
445 (u8*) dpo_nodes[child_type][child_proto][cc]);
447 vec_foreach(pi, parent_indices)
449 edge = vlib_node_add_next(vm, child_node->index, *pi);
451 if (~0 == dpo_edges[child_type][child_proto][parent_type][parent_proto])
453 dpo_edges[child_type][child_proto][parent_type][parent_proto] = edge;
457 ASSERT(dpo_edges[child_type][child_proto][parent_type][parent_proto] == edge);
463 vlib_worker_thread_barrier_release(vm);
464 vec_free(parent_indices);
467 return (dpo_edges[child_type][child_proto][parent_type][parent_proto]);
471 * @brief return already stacked up next node index for a given
472 * child_type/child_proto and parent_type/patent_proto.
473 * The VLIB graph arc used is taken from the parent and child types
477 dpo_get_next_node_by_type_and_proto (dpo_type_t child_type,
478 dpo_proto_t child_proto,
479 dpo_type_t parent_type,
480 dpo_proto_t parent_proto)
482 return (dpo_edges[child_type][child_proto][parent_type][parent_proto]);
486 * @brief Stack one DPO object on another, and thus establish a child parent
487 * relationship. The VLIB graph arc used is taken from the parent and child types
491 dpo_stack_i (u32 edge,
493 const dpo_id_t *parent)
496 * in order to get an atomic update of the parent we create a temporary,
497 * from a copy of the child, and add the next_node. then we copy to the parent
499 dpo_id_t tmp = DPO_INVALID;
500 dpo_copy(&tmp, parent);
503 * get the edge index for the parent to child VLIB graph transition
505 tmp.dpoi_next_node = edge;
508 * this update is atomic.
516 * @brief Stack one DPO object on another, and thus establish a child-parent
517 * relationship. The VLIB graph arc used is taken from the parent and child types
521 dpo_stack (dpo_type_t child_type,
522 dpo_proto_t child_proto,
524 const dpo_id_t *parent)
526 dpo_stack_i(dpo_get_next_node(child_type, child_proto, parent), dpo, parent);
530 * @brief Stack one DPO object on another, and thus establish a child parent
531 * relationship. A new VLIB graph arc is created from the child node passed
532 * to the nodes registered by the parent. The VLIB infra will ensure this arc
533 * is added only once.
536 dpo_stack_from_node (u32 child_node_index,
538 const dpo_id_t *parent)
540 dpo_type_t parent_type;
546 parent_type = parent->dpoi_type;
547 vm = vlib_get_main();
549 ASSERT(NULL != dpo_vfts[parent_type].dv_get_next_node);
550 parent_indices = dpo_vfts[parent_type].dv_get_next_node(parent);
551 ASSERT(parent_indices);
554 * This loop is purposefully written with the worker thread lock in the
555 * inner loop because;
556 * 1) the likelihood that the edge does not exist is smaller
557 * 2) the likelihood there is more than one node is even smaller
558 * so we are optimising for not need to take the lock
560 vec_foreach(pi, parent_indices)
562 edge = vlib_node_get_next(vm, child_node_index, *pi);
566 vlib_worker_thread_barrier_sync(vm);
568 edge = vlib_node_add_next(vm, child_node_index, *pi);
570 vlib_worker_thread_barrier_release(vm);
573 dpo_stack_i(edge, dpo, parent);
575 /* should free this local vector to avoid memory leak */
576 vec_free(parent_indices);
579 static clib_error_t *
580 dpo_module_init (vlib_main_t * vm)
582 drop_dpo_module_init();
583 punt_dpo_module_init();
584 receive_dpo_module_init();
585 load_balance_module_init();
586 mpls_label_dpo_module_init();
587 classify_dpo_module_init();
588 lookup_dpo_module_init();
589 ip_null_dpo_module_init();
590 ip6_ll_dpo_module_init();
591 replicate_module_init();
592 interface_rx_dpo_module_init();
593 interface_tx_dpo_module_init();
594 mpls_disp_dpo_module_init();
595 dvr_dpo_module_init();
596 l3_proxy_dpo_module_init();
597 pw_cw_dpo_module_init();
603 VLIB_INIT_FUNCTION(dpo_module_init) =
605 .runs_before = VLIB_INITS ("ip_main_init"),
609 static clib_error_t *
610 dpo_memory_show (vlib_main_t * vm,
611 unformat_input_t * input,
612 vlib_cli_command_t * cmd)
616 vlib_cli_output (vm, "DPO memory");
617 vlib_cli_output (vm, "%=30s %=5s %=8s/%=9s totals",
618 "Name","Size", "in-use", "allocated");
620 vec_foreach(vft, dpo_vfts)
622 if (NULL != vft->dv_mem_show)
631 * The '<em>sh dpo memory </em>' command displays the memory usage for each
632 * data-plane object type.
635 * @cliexstart{show dpo memory}
637 * Name Size in-use /allocated totals
638 * load-balance 64 12 / 12 768/768
639 * Adjacency 256 1 / 1 256/256
640 * Receive 24 5 / 5 120/120
641 * Lookup 12 0 / 0 0/0
642 * Classify 12 0 / 0 0/0
643 * MPLS label 24 0 / 0 0/0
646 VLIB_CLI_COMMAND (show_fib_memory, static) = {
647 .path = "show dpo memory",
648 .function = dpo_memory_show,
649 .short_help = "show dpo memory",