blob: 28aa0c23351bd9369c9b8b128657c212781d154e [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 Ranns0f26c5a2017-03-01 15:12:11 -080040#include <vnet/dpo/interface_dpo.h>
41#include <vnet/dpo/mpls_disposition.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010042
43/**
44 * Array of char* names for the DPO types and protos
45 */
46static const char* dpo_type_names[] = DPO_TYPES;
47static const char* dpo_proto_names[] = DPO_PROTOS;
48
49/**
50 * @brief Vector of virtual function tables for the DPO types
51 *
52 * This is a vector so we can dynamically register new DPO types in plugins.
53 */
54static dpo_vft_t *dpo_vfts;
55
56/**
57 * @brief vector of graph node names associated with each DPO type and protocol.
58 *
59 * dpo_nodes[child_type][child_proto][node_X] = node_name;
60 * i.e.
61 * dpo_node[DPO_LOAD_BALANCE][DPO_PROTO_IP4][0] = "ip4-lookup"
62 * dpo_node[DPO_LOAD_BALANCE][DPO_PROTO_IP4][1] = "ip4-load-balance"
63 *
64 * This is a vector so we can dynamically register new DPO types in plugins.
65 */
66static const char* const * const ** dpo_nodes;
67
68/**
69 * @brief Vector of edge indicies from parent DPO nodes to child
70 *
Neale Ranns8fe8cc22016-11-01 10:05:08 +000071 * dpo_edges[child_type][child_proto][parent_type][parent_proto] = edge_index
Neale Ranns0bfe5d82016-08-25 15:29:12 +010072 *
73 * This array is derived at init time from the dpo_nodes above. Note that
74 * the third dimension in dpo_nodes is lost, hence, the edge index from each
75 * node MUST be the same.
Neale Ranns8fe8cc22016-11-01 10:05:08 +000076 * Including both the child and parent protocol is required to support the
77 * case where it changes as the grapth is traversed, most notablly when an
78 * MPLS label is popped.
Neale Ranns0bfe5d82016-08-25 15:29:12 +010079 *
80 * Note that this array is child type specific, not child instance specific.
81 */
Neale Ranns8fe8cc22016-11-01 10:05:08 +000082static u32 ****dpo_edges;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010083
84/**
85 * @brief The DPO type value that can be assigend to the next dynamic
86 * type registration.
87 */
88static dpo_type_t dpo_dynamic = DPO_LAST;
89
Neale Rannsad95b5d2016-11-10 20:35:14 +000090dpo_proto_t
91vnet_link_to_dpo_proto (vnet_link_t linkt)
92{
93 switch (linkt)
94 {
95 case VNET_LINK_IP6:
96 return (DPO_PROTO_IP6);
97 case VNET_LINK_IP4:
98 return (DPO_PROTO_IP4);
99 case VNET_LINK_MPLS:
100 return (DPO_PROTO_MPLS);
101 case VNET_LINK_ETHERNET:
102 return (DPO_PROTO_ETHERNET);
Florin Corasce1b4c72017-01-26 14:25:34 -0800103 case VNET_LINK_NSH:
104 return (DPO_PROTO_NSH);
Neale Rannsad95b5d2016-11-10 20:35:14 +0000105 case VNET_LINK_ARP:
106 break;
107 }
108 ASSERT(0);
109 return (0);
110}
111
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100112u8 *
113format_dpo_type (u8 * s, va_list * args)
114{
115 dpo_type_t type = va_arg (*args, int);
116
117 s = format(s, "%s", dpo_type_names[type]);
118
119 return (s);
120}
121
122u8 *
123format_dpo_id (u8 * s, va_list * args)
124{
125 dpo_id_t *dpo = va_arg (*args, dpo_id_t*);
126 u32 indent = va_arg (*args, u32);
127
128 s = format(s, "[@%d]: ", dpo->dpoi_next_node);
129
130 if (NULL != dpo_vfts[dpo->dpoi_type].dv_format)
131 {
132 return (format(s, "%U",
133 dpo_vfts[dpo->dpoi_type].dv_format,
134 dpo->dpoi_index,
135 indent));
136 }
137
138 switch (dpo->dpoi_type)
139 {
140 case DPO_FIRST:
141 s = format(s, "unset");
142 break;
143 default:
144 s = format(s, "unknown");
145 break;
146 }
147 return (s);
148}
149
150u8 *
151format_dpo_proto (u8 * s, va_list * args)
152{
153 dpo_proto_t proto = va_arg (*args, int);
154
155 return (format(s, "%s", dpo_proto_names[proto]));
156}
157
158void
159dpo_set (dpo_id_t *dpo,
160 dpo_type_t type,
161 dpo_proto_t proto,
162 index_t index)
163{
164 dpo_id_t tmp = *dpo;
165
166 dpo->dpoi_type = type;
167 dpo->dpoi_proto = proto,
168 dpo->dpoi_index = index;
169
170 if (DPO_ADJACENCY == type)
171 {
172 /*
173 * set the adj subtype
174 */
175 ip_adjacency_t *adj;
176
177 adj = adj_get(index);
178
179 switch (adj->lookup_next_index)
180 {
181 case IP_LOOKUP_NEXT_ARP:
182 dpo->dpoi_type = DPO_ADJACENCY_INCOMPLETE;
183 break;
184 case IP_LOOKUP_NEXT_MIDCHAIN:
185 dpo->dpoi_type = DPO_ADJACENCY_MIDCHAIN;
186 break;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800187 case IP_LOOKUP_NEXT_MCAST_MIDCHAIN:
188 dpo->dpoi_type = DPO_ADJACENCY_MCAST_MIDCHAIN;
189 break;
190 case IP_LOOKUP_NEXT_MCAST:
191 dpo->dpoi_type = DPO_ADJACENCY_MCAST;
Neale Ranns8c4611b2017-05-23 03:43:47 -0700192 break;
193 case IP_LOOKUP_NEXT_GLEAN:
194 dpo->dpoi_type = DPO_ADJACENCY_GLEAN;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800195 break;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100196 default:
197 break;
198 }
199 }
200 dpo_lock(dpo);
201 dpo_unlock(&tmp);
202}
203
204void
205dpo_reset (dpo_id_t *dpo)
206{
Neale Rannsad95b5d2016-11-10 20:35:14 +0000207 dpo_id_t tmp = DPO_INVALID;
208
209 /*
210 * use the atomic copy operation.
211 */
212 dpo_copy(dpo, &tmp);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100213}
214
215/**
216 * \brief
217 * Compare two Data-path objects
218 *
219 * like memcmp, return 0 is matching, !0 otherwise.
220 */
221int
222dpo_cmp (const dpo_id_t *dpo1,
223 const dpo_id_t *dpo2)
224{
225 int res;
226
227 res = dpo1->dpoi_type - dpo2->dpoi_type;
228
229 if (0 != res) return (res);
230
231 return (dpo1->dpoi_index - dpo2->dpoi_index);
232}
233
234void
235dpo_copy (dpo_id_t *dst,
236 const dpo_id_t *src)
237{
238 dpo_id_t tmp = *dst;
239
240 /*
241 * the destination is written in a single u64 write - hence atomically w.r.t
242 * any packets inflight.
243 */
244 *((u64*)dst) = *(u64*)src;
245
246 dpo_lock(dst);
247 dpo_unlock(&tmp);
248}
249
250int
251dpo_is_adj (const dpo_id_t *dpo)
252{
253 return ((dpo->dpoi_type == DPO_ADJACENCY) ||
254 (dpo->dpoi_type == DPO_ADJACENCY_INCOMPLETE) ||
255 (dpo->dpoi_type == DPO_ADJACENCY_MIDCHAIN) ||
256 (dpo->dpoi_type == DPO_ADJACENCY_GLEAN));
257}
258
259void
260dpo_register (dpo_type_t type,
261 const dpo_vft_t *vft,
262 const char * const * const * nodes)
263{
264 vec_validate(dpo_vfts, type);
265 dpo_vfts[type] = *vft;
266
267 vec_validate(dpo_nodes, type);
268 dpo_nodes[type] = nodes;
269}
270
271dpo_type_t
272dpo_register_new_type (const dpo_vft_t *vft,
273 const char * const * const * nodes)
274{
275 dpo_type_t type = dpo_dynamic++;
276
277 dpo_register(type, vft, nodes);
278
279 return (type);
280}
281
282void
283dpo_lock (dpo_id_t *dpo)
284{
285 if (!dpo_id_is_valid(dpo))
286 return;
287
288 dpo_vfts[dpo->dpoi_type].dv_lock(dpo);
289}
290
291void
292dpo_unlock (dpo_id_t *dpo)
293{
294 if (!dpo_id_is_valid(dpo))
295 return;
296
297 dpo_vfts[dpo->dpoi_type].dv_unlock(dpo);
298}
299
300
301static u32
302dpo_get_next_node (dpo_type_t child_type,
303 dpo_proto_t child_proto,
304 const dpo_id_t *parent_dpo)
305{
306 dpo_proto_t parent_proto;
307 dpo_type_t parent_type;
308
309 parent_type = parent_dpo->dpoi_type;
310 parent_proto = parent_dpo->dpoi_proto;
311
312 vec_validate(dpo_edges, child_type);
313 vec_validate(dpo_edges[child_type], child_proto);
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000314 vec_validate(dpo_edges[child_type][child_proto], parent_type);
315 vec_validate_init_empty(
316 dpo_edges[child_type][child_proto][parent_type],
317 parent_proto, ~0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100318
319 /*
320 * if the edge index has not yet been created for this node to node transistion
321 */
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000322 if (~0 == dpo_edges[child_type][child_proto][parent_type][parent_proto])
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100323 {
324 vlib_node_t *parent_node, *child_node;
325 vlib_main_t *vm;
326 u32 edge ,pp, cc;
327
328 vm = vlib_get_main();
329
330 ASSERT(NULL != dpo_nodes[child_type]);
331 ASSERT(NULL != dpo_nodes[child_type][child_proto]);
332 ASSERT(NULL != dpo_nodes[parent_type]);
333 ASSERT(NULL != dpo_nodes[parent_type][parent_proto]);
334
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000335 cc = 0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100336
337 /*
338 * create a graph arc from each of the parent's registered node types,
339 * to each of the childs.
340 */
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000341 while (NULL != dpo_nodes[child_type][child_proto][cc])
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100342 {
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000343 child_node =
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100344 vlib_get_node_by_name(vm,
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000345 (u8*) dpo_nodes[child_type][child_proto][cc]);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100346
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000347 pp = 0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100348
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000349 while (NULL != dpo_nodes[parent_type][parent_proto][pp])
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100350 {
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000351 parent_node =
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100352 vlib_get_node_by_name(vm,
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000353 (u8*) dpo_nodes[parent_type][parent_proto][pp]);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100354
355 edge = vlib_node_add_next(vm,
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000356 child_node->index,
357 parent_node->index);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100358
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000359 if (~0 == dpo_edges[child_type][child_proto][parent_type][parent_proto])
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100360 {
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000361 dpo_edges[child_type][child_proto][parent_type][parent_proto] = edge;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100362 }
363 else
364 {
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000365 ASSERT(dpo_edges[child_type][child_proto][parent_type][parent_proto] == edge);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100366 }
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000367 pp++;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100368 }
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000369 cc++;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100370 }
371 }
372
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000373 return (dpo_edges[child_type][child_proto][parent_type][parent_proto]);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100374}
375
376/**
377 * @brief Stack one DPO object on another, and thus establish a child parent
378 * relationship. The VLIB graph arc used is taken from the parent and child types
379 * passed.
380 */
381static void
382dpo_stack_i (u32 edge,
383 dpo_id_t *dpo,
384 const dpo_id_t *parent)
385{
386 /*
387 * in order to get an atomic update of the parent we create a temporary,
388 * from a copy of the child, and add the next_node. then we copy to the parent
389 */
Neale Ranns948e00f2016-10-20 13:39:34 +0100390 dpo_id_t tmp = DPO_INVALID;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100391 dpo_copy(&tmp, parent);
392
393 /*
394 * get the edge index for the parent to child VLIB graph transisition
395 */
396 tmp.dpoi_next_node = edge;
397
398 /*
399 * this update is atomic.
400 */
401 dpo_copy(dpo, &tmp);
402
403 dpo_reset(&tmp);
404}
405
406/**
407 * @brief Stack one DPO object on another, and thus establish a child-parent
408 * relationship. The VLIB graph arc used is taken from the parent and child types
409 * passed.
410 */
411void
412dpo_stack (dpo_type_t child_type,
413 dpo_proto_t child_proto,
414 dpo_id_t *dpo,
415 const dpo_id_t *parent)
416{
417 dpo_stack_i(dpo_get_next_node(child_type, child_proto, parent), dpo, parent);
418}
419
420/**
421 * @brief Stack one DPO object on another, and thus establish a child parent
422 * relationship. A new VLIB graph arc is created from the child node passed
423 * to the nodes registered by the parent. The VLIB infra will ensure this arc
424 * is added only once.
425 */
426void
427dpo_stack_from_node (u32 child_node_index,
428 dpo_id_t *dpo,
429 const dpo_id_t *parent)
430{
431 dpo_proto_t parent_proto;
432 vlib_node_t *parent_node;
433 dpo_type_t parent_type;
434 vlib_main_t *vm;
435 u32 edge;
436
437 parent_type = parent->dpoi_type;
438 parent_proto = parent->dpoi_proto;
439
440 vm = vlib_get_main();
441
442 ASSERT(NULL != dpo_nodes[parent_type]);
443 ASSERT(NULL != dpo_nodes[parent_type][parent_proto]);
444
445 parent_node =
446 vlib_get_node_by_name(vm, (u8*) dpo_nodes[parent_type][parent_proto][0]);
447
448 edge = vlib_node_add_next(vm,
449 child_node_index,
450 parent_node->index);
451
452 dpo_stack_i(edge, dpo, parent);
453}
454
455static clib_error_t *
456dpo_module_init (vlib_main_t * vm)
457{
458 drop_dpo_module_init();
459 punt_dpo_module_init();
460 receive_dpo_module_init();
461 load_balance_module_init();
462 mpls_label_dpo_module_init();
463 classify_dpo_module_init();
464 lookup_dpo_module_init();
Neale Ranns948e00f2016-10-20 13:39:34 +0100465 ip_null_dpo_module_init();
Neale Ranns32e1c012016-11-22 17:07:28 +0000466 replicate_module_init();
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800467 interface_dpo_module_init();
468 mpls_disp_dpo_module_init();
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100469
470 return (NULL);
471}
472
473VLIB_INIT_FUNCTION(dpo_module_init);
Neale Ranns6c3ebcc2016-10-02 21:20:15 +0100474
475static clib_error_t *
476dpo_memory_show (vlib_main_t * vm,
477 unformat_input_t * input,
478 vlib_cli_command_t * cmd)
479{
480 dpo_vft_t *vft;
481
482 vlib_cli_output (vm, "DPO memory");
483 vlib_cli_output (vm, "%=30s %=5s %=8s/%=9s totals",
484 "Name","Size", "in-use", "allocated");
485
486 vec_foreach(vft, dpo_vfts)
487 {
488 if (NULL != vft->dv_mem_show)
489 vft->dv_mem_show();
490 }
491
492 return (NULL);
493}
494
495/* *INDENT-OFF* */
496/*?
497 * The '<em>sh dpo memory </em>' command displays the memory usage for each
498 * data-plane object type.
499 *
500 * @cliexpar
501 * @cliexstart{show dpo memory}
502 * DPO memory
503 * Name Size in-use /allocated totals
504 * load-balance 64 12 / 12 768/768
505 * Adjacency 256 1 / 1 256/256
506 * Receive 24 5 / 5 120/120
507 * Lookup 12 0 / 0 0/0
508 * Classify 12 0 / 0 0/0
509 * MPLS label 24 0 / 0 0/0
510 * @cliexend
511?*/
512VLIB_CLI_COMMAND (show_fib_memory, static) = {
513 .path = "show dpo memory",
514 .function = dpo_memory_show,
515 .short_help = "show dpo memory",
516};
517/* *INDENT-ON* */