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>
45 * Array of char* names for the DPO types and protos
47 static const char* dpo_type_names[] = DPO_TYPES;
48 static const char* dpo_proto_names[] = DPO_PROTOS;
51 * @brief Vector of virtual function tables for the DPO types
53 * This is a vector so we can dynamically register new DPO types in plugins.
55 static dpo_vft_t *dpo_vfts;
58 * @brief vector of graph node names associated with each DPO type and protocol.
60 * dpo_nodes[child_type][child_proto][node_X] = node_name;
62 * dpo_node[DPO_LOAD_BALANCE][DPO_PROTO_IP4][0] = "ip4-lookup"
63 * dpo_node[DPO_LOAD_BALANCE][DPO_PROTO_IP4][1] = "ip4-load-balance"
65 * This is a vector so we can dynamically register new DPO types in plugins.
67 static const char* const * const ** dpo_nodes;
70 * @brief Vector of edge indicies from parent DPO nodes to child
72 * dpo_edges[child_type][child_proto][parent_type][parent_proto] = edge_index
74 * This array is derived at init time from the dpo_nodes above. Note that
75 * the third dimension in dpo_nodes is lost, hence, the edge index from each
76 * node MUST be the same.
77 * Including both the child and parent protocol is required to support the
78 * case where it changes as the grapth is traversed, most notablly when an
79 * MPLS label is popped.
81 * Note that this array is child type specific, not child instance specific.
83 static u32 ****dpo_edges;
86 * @brief The DPO type value that can be assigend to the next dynamic
89 static dpo_type_t dpo_dynamic = DPO_LAST;
92 vnet_link_to_dpo_proto (vnet_link_t linkt)
97 return (DPO_PROTO_IP6);
99 return (DPO_PROTO_IP4);
101 return (DPO_PROTO_MPLS);
102 case VNET_LINK_ETHERNET:
103 return (DPO_PROTO_ETHERNET);
105 return (DPO_PROTO_NSH);
114 dpo_proto_to_link (dpo_proto_t dp)
119 return (VNET_LINK_IP6);
121 return (VNET_LINK_IP4);
123 return (VNET_LINK_MPLS);
124 case DPO_PROTO_ETHERNET:
125 return (VNET_LINK_ETHERNET);
127 return (VNET_LINK_NSH);
133 format_dpo_type (u8 * s, va_list * args)
135 dpo_type_t type = va_arg (*args, int);
137 s = format(s, "%s", dpo_type_names[type]);
143 format_dpo_id (u8 * s, va_list * args)
145 dpo_id_t *dpo = va_arg (*args, dpo_id_t*);
146 u32 indent = va_arg (*args, u32);
148 s = format(s, "[@%d]: ", dpo->dpoi_next_node);
150 if (NULL != dpo_vfts[dpo->dpoi_type].dv_format)
152 return (format(s, "%U",
153 dpo_vfts[dpo->dpoi_type].dv_format,
158 switch (dpo->dpoi_type)
161 s = format(s, "unset");
164 s = format(s, "unknown");
171 format_dpo_proto (u8 * s, va_list * args)
173 dpo_proto_t proto = va_arg (*args, int);
175 return (format(s, "%s", dpo_proto_names[proto]));
179 dpo_set (dpo_id_t *dpo,
186 dpo->dpoi_type = type;
187 dpo->dpoi_proto = proto,
188 dpo->dpoi_index = index;
190 if (DPO_ADJACENCY == type)
193 * set the adj subtype
197 adj = adj_get(index);
199 switch (adj->lookup_next_index)
201 case IP_LOOKUP_NEXT_ARP:
202 dpo->dpoi_type = DPO_ADJACENCY_INCOMPLETE;
204 case IP_LOOKUP_NEXT_MIDCHAIN:
205 dpo->dpoi_type = DPO_ADJACENCY_MIDCHAIN;
207 case IP_LOOKUP_NEXT_MCAST_MIDCHAIN:
208 dpo->dpoi_type = DPO_ADJACENCY_MCAST_MIDCHAIN;
210 case IP_LOOKUP_NEXT_MCAST:
211 dpo->dpoi_type = DPO_ADJACENCY_MCAST;
213 case IP_LOOKUP_NEXT_GLEAN:
214 dpo->dpoi_type = DPO_ADJACENCY_GLEAN;
225 dpo_reset (dpo_id_t *dpo)
227 dpo_id_t tmp = DPO_INVALID;
230 * use the atomic copy operation.
237 * Compare two Data-path objects
239 * like memcmp, return 0 is matching, !0 otherwise.
242 dpo_cmp (const dpo_id_t *dpo1,
243 const dpo_id_t *dpo2)
247 res = dpo1->dpoi_type - dpo2->dpoi_type;
249 if (0 != res) return (res);
251 return (dpo1->dpoi_index - dpo2->dpoi_index);
255 dpo_copy (dpo_id_t *dst,
261 * the destination is written in a single u64 write - hence atomically w.r.t
262 * any packets inflight.
264 *((u64*)dst) = *(u64*)src;
271 dpo_is_adj (const dpo_id_t *dpo)
273 return ((dpo->dpoi_type == DPO_ADJACENCY) ||
274 (dpo->dpoi_type == DPO_ADJACENCY_INCOMPLETE) ||
275 (dpo->dpoi_type == DPO_ADJACENCY_MIDCHAIN) ||
276 (dpo->dpoi_type == DPO_ADJACENCY_GLEAN));
280 dpo_default_get_next_node (const dpo_id_t *dpo)
282 u32 *node_indices = NULL;
283 const char *node_name;
286 node_name = dpo_nodes[dpo->dpoi_type][dpo->dpoi_proto][ii];
287 while (NULL != node_name)
291 node = vlib_get_node_by_name(vlib_get_main(), (u8*) node_name);
292 ASSERT(NULL != node);
293 vec_add1(node_indices, node->index);
296 node_name = dpo_nodes[dpo->dpoi_type][dpo->dpoi_proto][ii];
299 return (node_indices);
303 dpo_register (dpo_type_t type,
304 const dpo_vft_t *vft,
305 const char * const * const * nodes)
307 vec_validate(dpo_vfts, type);
308 dpo_vfts[type] = *vft;
309 if (NULL == dpo_vfts[type].dv_get_next_node)
311 dpo_vfts[type].dv_get_next_node = dpo_default_get_next_node;
314 vec_validate(dpo_nodes, type);
315 dpo_nodes[type] = nodes;
319 dpo_register_new_type (const dpo_vft_t *vft,
320 const char * const * const * nodes)
322 dpo_type_t type = dpo_dynamic++;
324 dpo_register(type, vft, nodes);
330 dpo_lock (dpo_id_t *dpo)
332 if (!dpo_id_is_valid(dpo))
335 dpo_vfts[dpo->dpoi_type].dv_lock(dpo);
339 dpo_unlock (dpo_id_t *dpo)
341 if (!dpo_id_is_valid(dpo))
344 dpo_vfts[dpo->dpoi_type].dv_unlock(dpo);
349 dpo_get_next_node (dpo_type_t child_type,
350 dpo_proto_t child_proto,
351 const dpo_id_t *parent_dpo)
353 dpo_proto_t parent_proto;
354 dpo_type_t parent_type;
356 parent_type = parent_dpo->dpoi_type;
357 parent_proto = parent_dpo->dpoi_proto;
359 vec_validate(dpo_edges, child_type);
360 vec_validate(dpo_edges[child_type], child_proto);
361 vec_validate(dpo_edges[child_type][child_proto], parent_type);
362 vec_validate_init_empty(
363 dpo_edges[child_type][child_proto][parent_type],
367 * if the edge index has not yet been created for this node to node transistion
369 if (~0 == dpo_edges[child_type][child_proto][parent_type][parent_proto])
371 vlib_node_t *child_node;
376 vm = vlib_get_main();
378 ASSERT(NULL != dpo_vfts[parent_type].dv_get_next_node);
379 ASSERT(NULL != dpo_nodes[child_type]);
380 ASSERT(NULL != dpo_nodes[child_type][child_proto]);
383 parent_indices = dpo_vfts[parent_type].dv_get_next_node(parent_dpo);
385 vlib_worker_thread_barrier_sync(vm);
388 * create a graph arc from each of the child's registered node types,
389 * to each of the parent's.
391 while (NULL != dpo_nodes[child_type][child_proto][cc])
394 vlib_get_node_by_name(vm,
395 (u8*) dpo_nodes[child_type][child_proto][cc]);
397 vec_foreach(pi, parent_indices)
399 edge = vlib_node_add_next(vm, child_node->index, *pi);
401 if (~0 == dpo_edges[child_type][child_proto][parent_type][parent_proto])
403 dpo_edges[child_type][child_proto][parent_type][parent_proto] = edge;
407 ASSERT(dpo_edges[child_type][child_proto][parent_type][parent_proto] == edge);
413 vlib_worker_thread_barrier_release(vm);
414 vec_free(parent_indices);
417 return (dpo_edges[child_type][child_proto][parent_type][parent_proto]);
421 * @brief Stack one DPO object on another, and thus establish a child parent
422 * relationship. The VLIB graph arc used is taken from the parent and child types
426 dpo_stack_i (u32 edge,
428 const dpo_id_t *parent)
431 * in order to get an atomic update of the parent we create a temporary,
432 * from a copy of the child, and add the next_node. then we copy to the parent
434 dpo_id_t tmp = DPO_INVALID;
435 dpo_copy(&tmp, parent);
438 * get the edge index for the parent to child VLIB graph transisition
440 tmp.dpoi_next_node = edge;
443 * this update is atomic.
451 * @brief Stack one DPO object on another, and thus establish a child-parent
452 * relationship. The VLIB graph arc used is taken from the parent and child types
456 dpo_stack (dpo_type_t child_type,
457 dpo_proto_t child_proto,
459 const dpo_id_t *parent)
461 dpo_stack_i(dpo_get_next_node(child_type, child_proto, parent), dpo, parent);
465 * @brief Stack one DPO object on another, and thus establish a child parent
466 * relationship. A new VLIB graph arc is created from the child node passed
467 * to the nodes registered by the parent. The VLIB infra will ensure this arc
468 * is added only once.
471 dpo_stack_from_node (u32 child_node_index,
473 const dpo_id_t *parent)
475 dpo_type_t parent_type;
481 parent_type = parent->dpoi_type;
482 vm = vlib_get_main();
484 ASSERT(NULL != dpo_vfts[parent_type].dv_get_next_node);
485 parent_indices = dpo_vfts[parent_type].dv_get_next_node(parent);
486 ASSERT(parent_indices);
489 * This loop is purposefully written with the worker thread lock in the
490 * inner loop because;
491 * 1) the likelihood that the edge does not exist is smaller
492 * 2) the likelihood there is more than one node is even smaller
493 * so we are optimising for not need to take the lock
495 vec_foreach(pi, parent_indices)
497 edge = vlib_node_get_next(vm, child_node_index, *pi);
501 vlib_worker_thread_barrier_sync(vm);
503 edge = vlib_node_add_next(vm, child_node_index, *pi);
505 vlib_worker_thread_barrier_release(vm);
508 dpo_stack_i(edge, dpo, parent);
511 static clib_error_t *
512 dpo_module_init (vlib_main_t * vm)
514 drop_dpo_module_init();
515 punt_dpo_module_init();
516 receive_dpo_module_init();
517 load_balance_module_init();
518 mpls_label_dpo_module_init();
519 classify_dpo_module_init();
520 lookup_dpo_module_init();
521 ip_null_dpo_module_init();
522 replicate_module_init();
523 interface_rx_dpo_module_init();
524 interface_tx_dpo_module_init();
525 mpls_disp_dpo_module_init();
530 VLIB_INIT_FUNCTION(dpo_module_init);
532 static clib_error_t *
533 dpo_memory_show (vlib_main_t * vm,
534 unformat_input_t * input,
535 vlib_cli_command_t * cmd)
539 vlib_cli_output (vm, "DPO memory");
540 vlib_cli_output (vm, "%=30s %=5s %=8s/%=9s totals",
541 "Name","Size", "in-use", "allocated");
543 vec_foreach(vft, dpo_vfts)
545 if (NULL != vft->dv_mem_show)
554 * The '<em>sh dpo memory </em>' command displays the memory usage for each
555 * data-plane object type.
558 * @cliexstart{show dpo memory}
560 * Name Size in-use /allocated totals
561 * load-balance 64 12 / 12 768/768
562 * Adjacency 256 1 / 1 256/256
563 * Receive 24 5 / 5 120/120
564 * Lookup 12 0 / 0 0/0
565 * Classify 12 0 / 0 0/0
566 * MPLS label 24 0 / 0 0/0
569 VLIB_CLI_COMMAND (show_fib_memory, static) = {
570 .path = "show dpo memory",
571 .function = dpo_memory_show,
572 .short_help = "show dpo memory",