blob: bd18b66bfd55f10817c1cea81165547cdd20688b [file] [log] [blame]
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001/*
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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15/**
16 * @brief
17 * A Data-Path Object is an object that represents actions that are
18 * applied to packets are they are switched through VPP.
19 *
20 * The DPO is a base class that is specialised by other objects to provide
21 * concreate actions
22 *
23 * The VLIB graph nodes are graph of types, the DPO graph is a graph of instances.
24 */
25
26#include <vnet/dpo/dpo.h>
27#include <vnet/ip/lookup.h>
28#include <vnet/ip/format.h>
29#include <vnet/adj/adj.h>
30
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>
Neale Ranns948e00f2016-10-20 13:39:34 +010038#include <vnet/dpo/ip_null_dpo.h>
Neale Ranns32e1c012016-11-22 17:07:28 +000039#include <vnet/dpo/replicate_dpo.h>
Neale Ranns43161a82017-08-12 02:12:00 -070040#include <vnet/dpo/interface_rx_dpo.h>
41#include <vnet/dpo/interface_tx_dpo.h>
Neale Ranns0f26c5a2017-03-01 15:12:11 -080042#include <vnet/dpo/mpls_disposition.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010043
44/**
45 * Array of char* names for the DPO types and protos
46 */
47static const char* dpo_type_names[] = DPO_TYPES;
48static const char* dpo_proto_names[] = DPO_PROTOS;
49
50/**
51 * @brief Vector of virtual function tables for the DPO types
52 *
53 * This is a vector so we can dynamically register new DPO types in plugins.
54 */
55static dpo_vft_t *dpo_vfts;
56
57/**
58 * @brief vector of graph node names associated with each DPO type and protocol.
59 *
60 * dpo_nodes[child_type][child_proto][node_X] = node_name;
61 * i.e.
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"
64 *
65 * This is a vector so we can dynamically register new DPO types in plugins.
66 */
67static const char* const * const ** dpo_nodes;
68
69/**
70 * @brief Vector of edge indicies from parent DPO nodes to child
71 *
Neale Ranns8fe8cc22016-11-01 10:05:08 +000072 * dpo_edges[child_type][child_proto][parent_type][parent_proto] = edge_index
Neale Ranns0bfe5d82016-08-25 15:29:12 +010073 *
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.
Neale Ranns8fe8cc22016-11-01 10:05:08 +000077 * 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.
Neale Ranns0bfe5d82016-08-25 15:29:12 +010080 *
81 * Note that this array is child type specific, not child instance specific.
82 */
Neale Ranns8fe8cc22016-11-01 10:05:08 +000083static u32 ****dpo_edges;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010084
85/**
86 * @brief The DPO type value that can be assigend to the next dynamic
87 * type registration.
88 */
89static dpo_type_t dpo_dynamic = DPO_LAST;
90
Neale Rannsad95b5d2016-11-10 20:35:14 +000091dpo_proto_t
92vnet_link_to_dpo_proto (vnet_link_t linkt)
93{
94 switch (linkt)
95 {
96 case VNET_LINK_IP6:
97 return (DPO_PROTO_IP6);
98 case VNET_LINK_IP4:
99 return (DPO_PROTO_IP4);
100 case VNET_LINK_MPLS:
101 return (DPO_PROTO_MPLS);
102 case VNET_LINK_ETHERNET:
103 return (DPO_PROTO_ETHERNET);
Florin Corasce1b4c72017-01-26 14:25:34 -0800104 case VNET_LINK_NSH:
105 return (DPO_PROTO_NSH);
Neale Rannsad95b5d2016-11-10 20:35:14 +0000106 case VNET_LINK_ARP:
107 break;
108 }
109 ASSERT(0);
110 return (0);
111}
112
Neale Rannsda78f952017-05-24 09:15:43 -0700113vnet_link_t
114dpo_proto_to_link (dpo_proto_t dp)
115{
116 switch (dp)
117 {
118 case DPO_PROTO_IP6:
119 return (VNET_LINK_IP6);
120 case DPO_PROTO_IP4:
121 return (VNET_LINK_IP4);
122 case DPO_PROTO_MPLS:
123 return (VNET_LINK_MPLS);
124 case DPO_PROTO_ETHERNET:
125 return (VNET_LINK_ETHERNET);
126 case DPO_PROTO_NSH:
127 return (VNET_LINK_NSH);
128 }
129 return (~0);
130}
131
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100132u8 *
133format_dpo_type (u8 * s, va_list * args)
134{
135 dpo_type_t type = va_arg (*args, int);
136
137 s = format(s, "%s", dpo_type_names[type]);
138
139 return (s);
140}
141
142u8 *
143format_dpo_id (u8 * s, va_list * args)
144{
145 dpo_id_t *dpo = va_arg (*args, dpo_id_t*);
146 u32 indent = va_arg (*args, u32);
147
148 s = format(s, "[@%d]: ", dpo->dpoi_next_node);
149
150 if (NULL != dpo_vfts[dpo->dpoi_type].dv_format)
151 {
152 return (format(s, "%U",
153 dpo_vfts[dpo->dpoi_type].dv_format,
154 dpo->dpoi_index,
155 indent));
156 }
157
158 switch (dpo->dpoi_type)
159 {
160 case DPO_FIRST:
161 s = format(s, "unset");
162 break;
163 default:
164 s = format(s, "unknown");
165 break;
166 }
167 return (s);
168}
169
170u8 *
171format_dpo_proto (u8 * s, va_list * args)
172{
173 dpo_proto_t proto = va_arg (*args, int);
174
175 return (format(s, "%s", dpo_proto_names[proto]));
176}
177
178void
179dpo_set (dpo_id_t *dpo,
180 dpo_type_t type,
181 dpo_proto_t proto,
182 index_t index)
183{
184 dpo_id_t tmp = *dpo;
185
186 dpo->dpoi_type = type;
187 dpo->dpoi_proto = proto,
188 dpo->dpoi_index = index;
189
190 if (DPO_ADJACENCY == type)
191 {
192 /*
193 * set the adj subtype
194 */
195 ip_adjacency_t *adj;
196
197 adj = adj_get(index);
198
199 switch (adj->lookup_next_index)
200 {
201 case IP_LOOKUP_NEXT_ARP:
202 dpo->dpoi_type = DPO_ADJACENCY_INCOMPLETE;
203 break;
204 case IP_LOOKUP_NEXT_MIDCHAIN:
205 dpo->dpoi_type = DPO_ADJACENCY_MIDCHAIN;
206 break;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800207 case IP_LOOKUP_NEXT_MCAST_MIDCHAIN:
208 dpo->dpoi_type = DPO_ADJACENCY_MCAST_MIDCHAIN;
209 break;
210 case IP_LOOKUP_NEXT_MCAST:
211 dpo->dpoi_type = DPO_ADJACENCY_MCAST;
Neale Ranns8c4611b2017-05-23 03:43:47 -0700212 break;
213 case IP_LOOKUP_NEXT_GLEAN:
214 dpo->dpoi_type = DPO_ADJACENCY_GLEAN;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800215 break;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100216 default:
217 break;
218 }
219 }
220 dpo_lock(dpo);
221 dpo_unlock(&tmp);
222}
223
224void
225dpo_reset (dpo_id_t *dpo)
226{
Neale Rannsad95b5d2016-11-10 20:35:14 +0000227 dpo_id_t tmp = DPO_INVALID;
228
229 /*
230 * use the atomic copy operation.
231 */
232 dpo_copy(dpo, &tmp);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100233}
234
235/**
236 * \brief
237 * Compare two Data-path objects
238 *
239 * like memcmp, return 0 is matching, !0 otherwise.
240 */
241int
242dpo_cmp (const dpo_id_t *dpo1,
243 const dpo_id_t *dpo2)
244{
245 int res;
246
247 res = dpo1->dpoi_type - dpo2->dpoi_type;
248
249 if (0 != res) return (res);
250
251 return (dpo1->dpoi_index - dpo2->dpoi_index);
252}
253
254void
255dpo_copy (dpo_id_t *dst,
256 const dpo_id_t *src)
257{
258 dpo_id_t tmp = *dst;
259
260 /*
261 * the destination is written in a single u64 write - hence atomically w.r.t
262 * any packets inflight.
263 */
264 *((u64*)dst) = *(u64*)src;
265
266 dpo_lock(dst);
267 dpo_unlock(&tmp);
268}
269
270int
271dpo_is_adj (const dpo_id_t *dpo)
272{
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));
277}
278
Neale Ranns43161a82017-08-12 02:12:00 -0700279static u32 *
280dpo_default_get_next_node (const dpo_id_t *dpo)
281{
282 u32 *node_indices = NULL;
283 const char *node_name;
284 u32 ii = 0;
285
286 node_name = dpo_nodes[dpo->dpoi_type][dpo->dpoi_proto][ii];
287 while (NULL != node_name)
288 {
289 vlib_node_t *node;
290
291 node = vlib_get_node_by_name(vlib_get_main(), (u8*) node_name);
292 ASSERT(NULL != node);
293 vec_add1(node_indices, node->index);
294
295 ++ii;
296 node_name = dpo_nodes[dpo->dpoi_type][dpo->dpoi_proto][ii];
297 }
298
299 return (node_indices);
300}
301
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100302void
303dpo_register (dpo_type_t type,
304 const dpo_vft_t *vft,
305 const char * const * const * nodes)
306{
307 vec_validate(dpo_vfts, type);
308 dpo_vfts[type] = *vft;
Neale Ranns43161a82017-08-12 02:12:00 -0700309 if (NULL == dpo_vfts[type].dv_get_next_node)
310 {
311 dpo_vfts[type].dv_get_next_node = dpo_default_get_next_node;
312 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100313
314 vec_validate(dpo_nodes, type);
315 dpo_nodes[type] = nodes;
316}
317
318dpo_type_t
319dpo_register_new_type (const dpo_vft_t *vft,
320 const char * const * const * nodes)
321{
322 dpo_type_t type = dpo_dynamic++;
323
324 dpo_register(type, vft, nodes);
325
326 return (type);
327}
328
329void
330dpo_lock (dpo_id_t *dpo)
331{
332 if (!dpo_id_is_valid(dpo))
333 return;
334
335 dpo_vfts[dpo->dpoi_type].dv_lock(dpo);
336}
337
338void
339dpo_unlock (dpo_id_t *dpo)
340{
341 if (!dpo_id_is_valid(dpo))
342 return;
343
344 dpo_vfts[dpo->dpoi_type].dv_unlock(dpo);
345}
346
347
348static u32
349dpo_get_next_node (dpo_type_t child_type,
350 dpo_proto_t child_proto,
351 const dpo_id_t *parent_dpo)
352{
353 dpo_proto_t parent_proto;
354 dpo_type_t parent_type;
355
356 parent_type = parent_dpo->dpoi_type;
357 parent_proto = parent_dpo->dpoi_proto;
358
359 vec_validate(dpo_edges, child_type);
360 vec_validate(dpo_edges[child_type], child_proto);
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000361 vec_validate(dpo_edges[child_type][child_proto], parent_type);
362 vec_validate_init_empty(
363 dpo_edges[child_type][child_proto][parent_type],
364 parent_proto, ~0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100365
366 /*
367 * if the edge index has not yet been created for this node to node transistion
368 */
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000369 if (~0 == dpo_edges[child_type][child_proto][parent_type][parent_proto])
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100370 {
Neale Ranns43161a82017-08-12 02:12:00 -0700371 vlib_node_t *child_node;
372 u32 *parent_indices;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100373 vlib_main_t *vm;
Neale Ranns43161a82017-08-12 02:12:00 -0700374 u32 edge, *pi, cc;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100375
376 vm = vlib_get_main();
377
Neale Ranns43161a82017-08-12 02:12:00 -0700378 ASSERT(NULL != dpo_vfts[parent_type].dv_get_next_node);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100379 ASSERT(NULL != dpo_nodes[child_type]);
380 ASSERT(NULL != dpo_nodes[child_type][child_proto]);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100381
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000382 cc = 0;
Neale Ranns43161a82017-08-12 02:12:00 -0700383 parent_indices = dpo_vfts[parent_type].dv_get_next_node(parent_dpo);
384
385 vlib_worker_thread_barrier_sync(vm);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100386
387 /*
Neale Ranns43161a82017-08-12 02:12:00 -0700388 * create a graph arc from each of the child's registered node types,
389 * to each of the parent's.
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100390 */
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000391 while (NULL != dpo_nodes[child_type][child_proto][cc])
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100392 {
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000393 child_node =
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100394 vlib_get_node_by_name(vm,
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000395 (u8*) dpo_nodes[child_type][child_proto][cc]);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100396
Neale Ranns43161a82017-08-12 02:12:00 -0700397 vec_foreach(pi, parent_indices)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100398 {
Neale Ranns43161a82017-08-12 02:12:00 -0700399 edge = vlib_node_add_next(vm, child_node->index, *pi);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100400
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000401 if (~0 == dpo_edges[child_type][child_proto][parent_type][parent_proto])
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100402 {
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000403 dpo_edges[child_type][child_proto][parent_type][parent_proto] = edge;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100404 }
405 else
406 {
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000407 ASSERT(dpo_edges[child_type][child_proto][parent_type][parent_proto] == edge);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100408 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100409 }
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000410 cc++;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100411 }
Neale Rannsbb620d72017-06-29 00:19:08 -0700412
413 vlib_worker_thread_barrier_release(vm);
Neale Ranns43161a82017-08-12 02:12:00 -0700414 vec_free(parent_indices);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100415 }
416
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000417 return (dpo_edges[child_type][child_proto][parent_type][parent_proto]);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100418}
419
420/**
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
423 * passed.
424 */
425static void
426dpo_stack_i (u32 edge,
427 dpo_id_t *dpo,
428 const dpo_id_t *parent)
429{
430 /*
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
433 */
Neale Ranns948e00f2016-10-20 13:39:34 +0100434 dpo_id_t tmp = DPO_INVALID;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100435 dpo_copy(&tmp, parent);
436
437 /*
438 * get the edge index for the parent to child VLIB graph transisition
439 */
440 tmp.dpoi_next_node = edge;
441
442 /*
443 * this update is atomic.
444 */
445 dpo_copy(dpo, &tmp);
446
447 dpo_reset(&tmp);
448}
449
450/**
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
453 * passed.
454 */
455void
456dpo_stack (dpo_type_t child_type,
457 dpo_proto_t child_proto,
458 dpo_id_t *dpo,
459 const dpo_id_t *parent)
460{
461 dpo_stack_i(dpo_get_next_node(child_type, child_proto, parent), dpo, parent);
462}
463
464/**
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.
469 */
470void
471dpo_stack_from_node (u32 child_node_index,
472 dpo_id_t *dpo,
473 const dpo_id_t *parent)
474{
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100475 dpo_type_t parent_type;
Neale Ranns43161a82017-08-12 02:12:00 -0700476 u32 *parent_indices;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100477 vlib_main_t *vm;
Neale Ranns43161a82017-08-12 02:12:00 -0700478 u32 edge, *pi;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100479
Neale Ranns43161a82017-08-12 02:12:00 -0700480 edge = 0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100481 parent_type = parent->dpoi_type;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100482 vm = vlib_get_main();
483
Neale Ranns43161a82017-08-12 02:12:00 -0700484 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);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100487
Neale Ranns43161a82017-08-12 02:12:00 -0700488 /*
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
494 */
495 vec_foreach(pi, parent_indices)
Neale Rannsbb620d72017-06-29 00:19:08 -0700496 {
Neale Ranns43161a82017-08-12 02:12:00 -0700497 edge = vlib_node_get_next(vm, child_node_index, *pi);
Neale Rannsbb620d72017-06-29 00:19:08 -0700498
Neale Ranns43161a82017-08-12 02:12:00 -0700499 if (~0 == edge)
500 {
501 vlib_worker_thread_barrier_sync(vm);
Neale Rannsbb620d72017-06-29 00:19:08 -0700502
Neale Ranns43161a82017-08-12 02:12:00 -0700503 edge = vlib_node_add_next(vm, child_node_index, *pi);
504
505 vlib_worker_thread_barrier_release(vm);
506 }
Neale Rannsbb620d72017-06-29 00:19:08 -0700507 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100508 dpo_stack_i(edge, dpo, parent);
509}
510
511static clib_error_t *
512dpo_module_init (vlib_main_t * vm)
513{
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();
Neale Ranns948e00f2016-10-20 13:39:34 +0100521 ip_null_dpo_module_init();
Neale Ranns32e1c012016-11-22 17:07:28 +0000522 replicate_module_init();
Neale Ranns43161a82017-08-12 02:12:00 -0700523 interface_rx_dpo_module_init();
524 interface_tx_dpo_module_init();
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800525 mpls_disp_dpo_module_init();
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100526
527 return (NULL);
528}
529
530VLIB_INIT_FUNCTION(dpo_module_init);
Neale Ranns6c3ebcc2016-10-02 21:20:15 +0100531
532static clib_error_t *
533dpo_memory_show (vlib_main_t * vm,
534 unformat_input_t * input,
535 vlib_cli_command_t * cmd)
536{
537 dpo_vft_t *vft;
538
539 vlib_cli_output (vm, "DPO memory");
540 vlib_cli_output (vm, "%=30s %=5s %=8s/%=9s totals",
541 "Name","Size", "in-use", "allocated");
542
543 vec_foreach(vft, dpo_vfts)
544 {
545 if (NULL != vft->dv_mem_show)
546 vft->dv_mem_show();
547 }
548
549 return (NULL);
550}
551
552/* *INDENT-OFF* */
553/*?
554 * The '<em>sh dpo memory </em>' command displays the memory usage for each
555 * data-plane object type.
556 *
557 * @cliexpar
558 * @cliexstart{show dpo memory}
559 * 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
567 * @cliexend
568?*/
569VLIB_CLI_COMMAND (show_fib_memory, static) = {
570 .path = "show dpo memory",
571 .function = dpo_memory_show,
572 .short_help = "show dpo memory",
573};
574/* *INDENT-ON* */