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>
40 * Array of char* names for the DPO types and protos
42 static const char* dpo_type_names[] = DPO_TYPES;
43 static const char* dpo_proto_names[] = DPO_PROTOS;
46 * @brief Vector of virtual function tables for the DPO types
48 * This is a vector so we can dynamically register new DPO types in plugins.
50 static dpo_vft_t *dpo_vfts;
53 * @brief vector of graph node names associated with each DPO type and protocol.
55 * dpo_nodes[child_type][child_proto][node_X] = node_name;
57 * dpo_node[DPO_LOAD_BALANCE][DPO_PROTO_IP4][0] = "ip4-lookup"
58 * dpo_node[DPO_LOAD_BALANCE][DPO_PROTO_IP4][1] = "ip4-load-balance"
60 * This is a vector so we can dynamically register new DPO types in plugins.
62 static const char* const * const ** dpo_nodes;
65 * @brief Vector of edge indicies from parent DPO nodes to child
67 * dpo_edges[child_type][child_proto][parent_type] = edge_index
69 * This array is derived at init time from the dpo_nodes above. Note that
70 * the third dimension in dpo_nodes is lost, hence, the edge index from each
71 * node MUST be the same.
73 * Note that this array is child type specific, not child instance specific.
75 static u32 ***dpo_edges;
78 * @brief The DPO type value that can be assigend to the next dynamic
81 static dpo_type_t dpo_dynamic = DPO_LAST;
84 format_dpo_type (u8 * s, va_list * args)
86 dpo_type_t type = va_arg (*args, int);
88 s = format(s, "%s", dpo_type_names[type]);
94 format_dpo_id (u8 * s, va_list * args)
96 dpo_id_t *dpo = va_arg (*args, dpo_id_t*);
97 u32 indent = va_arg (*args, u32);
99 s = format(s, "[@%d]: ", dpo->dpoi_next_node);
101 if (NULL != dpo_vfts[dpo->dpoi_type].dv_format)
103 return (format(s, "%U",
104 dpo_vfts[dpo->dpoi_type].dv_format,
109 switch (dpo->dpoi_type)
112 s = format(s, "unset");
115 s = format(s, "unknown");
122 format_dpo_proto (u8 * s, va_list * args)
124 dpo_proto_t proto = va_arg (*args, int);
126 return (format(s, "%s", dpo_proto_names[proto]));
130 dpo_set (dpo_id_t *dpo,
137 dpo->dpoi_type = type;
138 dpo->dpoi_proto = proto,
139 dpo->dpoi_index = index;
141 if (DPO_ADJACENCY == type)
144 * set the adj subtype
148 adj = adj_get(index);
150 switch (adj->lookup_next_index)
152 case IP_LOOKUP_NEXT_ARP:
153 dpo->dpoi_type = DPO_ADJACENCY_INCOMPLETE;
155 case IP_LOOKUP_NEXT_MIDCHAIN:
156 dpo->dpoi_type = DPO_ADJACENCY_MIDCHAIN;
167 dpo_reset (dpo_id_t *dpo)
169 dpo_set(dpo, DPO_FIRST, DPO_PROTO_NONE, INDEX_INVALID);
174 * Compare two Data-path objects
176 * like memcmp, return 0 is matching, !0 otherwise.
179 dpo_cmp (const dpo_id_t *dpo1,
180 const dpo_id_t *dpo2)
184 res = dpo1->dpoi_type - dpo2->dpoi_type;
186 if (0 != res) return (res);
188 return (dpo1->dpoi_index - dpo2->dpoi_index);
192 dpo_copy (dpo_id_t *dst,
198 * the destination is written in a single u64 write - hence atomically w.r.t
199 * any packets inflight.
201 *((u64*)dst) = *(u64*)src;
208 dpo_is_adj (const dpo_id_t *dpo)
210 return ((dpo->dpoi_type == DPO_ADJACENCY) ||
211 (dpo->dpoi_type == DPO_ADJACENCY_INCOMPLETE) ||
212 (dpo->dpoi_type == DPO_ADJACENCY_MIDCHAIN) ||
213 (dpo->dpoi_type == DPO_ADJACENCY_GLEAN));
217 dpo_register (dpo_type_t type,
218 const dpo_vft_t *vft,
219 const char * const * const * nodes)
221 vec_validate(dpo_vfts, type);
222 dpo_vfts[type] = *vft;
224 vec_validate(dpo_nodes, type);
225 dpo_nodes[type] = nodes;
229 dpo_register_new_type (const dpo_vft_t *vft,
230 const char * const * const * nodes)
232 dpo_type_t type = dpo_dynamic++;
234 dpo_register(type, vft, nodes);
240 dpo_lock (dpo_id_t *dpo)
242 if (!dpo_id_is_valid(dpo))
245 dpo_vfts[dpo->dpoi_type].dv_lock(dpo);
249 dpo_unlock (dpo_id_t *dpo)
251 if (!dpo_id_is_valid(dpo))
254 dpo_vfts[dpo->dpoi_type].dv_unlock(dpo);
259 dpo_get_next_node (dpo_type_t child_type,
260 dpo_proto_t child_proto,
261 const dpo_id_t *parent_dpo)
263 dpo_proto_t parent_proto;
264 dpo_type_t parent_type;
266 parent_type = parent_dpo->dpoi_type;
267 parent_proto = parent_dpo->dpoi_proto;
269 vec_validate(dpo_edges, child_type);
270 vec_validate(dpo_edges[child_type], child_proto);
271 vec_validate_init_empty(dpo_edges[child_type][child_proto],
272 parent_dpo->dpoi_type, ~0);
275 * if the edge index has not yet been created for this node to node transistion
277 if (~0 == dpo_edges[child_type][child_proto][parent_type])
279 vlib_node_t *parent_node, *child_node;
283 vm = vlib_get_main();
285 ASSERT(NULL != dpo_nodes[child_type]);
286 ASSERT(NULL != dpo_nodes[child_type][child_proto]);
287 ASSERT(NULL != dpo_nodes[parent_type]);
288 ASSERT(NULL != dpo_nodes[parent_type][parent_proto]);
293 * create a graph arc from each of the parent's registered node types,
294 * to each of the childs.
296 while (NULL != dpo_nodes[child_type][child_proto][pp])
299 vlib_get_node_by_name(vm,
300 (u8*) dpo_nodes[child_type][child_proto][pp]);
304 while (NULL != dpo_nodes[parent_type][child_proto][cc])
307 vlib_get_node_by_name(vm,
308 (u8*) dpo_nodes[parent_type][parent_proto][cc]);
310 edge = vlib_node_add_next(vm,
314 if (~0 == dpo_edges[child_type][child_proto][parent_type])
316 dpo_edges[child_type][child_proto][parent_type] = edge;
320 ASSERT(dpo_edges[child_type][child_proto][parent_type] == edge);
328 return (dpo_edges[child_type][child_proto][parent_type]);
332 * @brief Stack one DPO object on another, and thus establish a child parent
333 * relationship. The VLIB graph arc used is taken from the parent and child types
337 dpo_stack_i (u32 edge,
339 const dpo_id_t *parent)
342 * in order to get an atomic update of the parent we create a temporary,
343 * from a copy of the child, and add the next_node. then we copy to the parent
345 dpo_id_t tmp = DPO_NULL;
346 dpo_copy(&tmp, parent);
349 * get the edge index for the parent to child VLIB graph transisition
351 tmp.dpoi_next_node = edge;
354 * this update is atomic.
362 * @brief Stack one DPO object on another, and thus establish a child-parent
363 * relationship. The VLIB graph arc used is taken from the parent and child types
367 dpo_stack (dpo_type_t child_type,
368 dpo_proto_t child_proto,
370 const dpo_id_t *parent)
372 dpo_stack_i(dpo_get_next_node(child_type, child_proto, parent), dpo, parent);
376 * @brief Stack one DPO object on another, and thus establish a child parent
377 * relationship. A new VLIB graph arc is created from the child node passed
378 * to the nodes registered by the parent. The VLIB infra will ensure this arc
379 * is added only once.
382 dpo_stack_from_node (u32 child_node_index,
384 const dpo_id_t *parent)
386 dpo_proto_t parent_proto;
387 vlib_node_t *parent_node;
388 dpo_type_t parent_type;
392 parent_type = parent->dpoi_type;
393 parent_proto = parent->dpoi_proto;
395 vm = vlib_get_main();
397 ASSERT(NULL != dpo_nodes[parent_type]);
398 ASSERT(NULL != dpo_nodes[parent_type][parent_proto]);
401 vlib_get_node_by_name(vm, (u8*) dpo_nodes[parent_type][parent_proto][0]);
403 edge = vlib_node_add_next(vm,
407 dpo_stack_i(edge, dpo, parent);
410 static clib_error_t *
411 dpo_module_init (vlib_main_t * vm)
413 drop_dpo_module_init();
414 punt_dpo_module_init();
415 receive_dpo_module_init();
416 load_balance_module_init();
417 mpls_label_dpo_module_init();
418 classify_dpo_module_init();
419 lookup_dpo_module_init();
424 VLIB_INIT_FUNCTION(dpo_module_init);