blob: d8e075a7f2f7265e4fe70249e0ff729d08eec2ec [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 Ranns0bfe5d82016-08-25 15:29:12 +010040
41/**
42 * Array of char* names for the DPO types and protos
43 */
44static const char* dpo_type_names[] = DPO_TYPES;
45static const char* dpo_proto_names[] = DPO_PROTOS;
46
47/**
48 * @brief Vector of virtual function tables for the DPO types
49 *
50 * This is a vector so we can dynamically register new DPO types in plugins.
51 */
52static dpo_vft_t *dpo_vfts;
53
54/**
55 * @brief vector of graph node names associated with each DPO type and protocol.
56 *
57 * dpo_nodes[child_type][child_proto][node_X] = node_name;
58 * i.e.
59 * dpo_node[DPO_LOAD_BALANCE][DPO_PROTO_IP4][0] = "ip4-lookup"
60 * dpo_node[DPO_LOAD_BALANCE][DPO_PROTO_IP4][1] = "ip4-load-balance"
61 *
62 * This is a vector so we can dynamically register new DPO types in plugins.
63 */
64static const char* const * const ** dpo_nodes;
65
66/**
67 * @brief Vector of edge indicies from parent DPO nodes to child
68 *
Neale Ranns8fe8cc22016-11-01 10:05:08 +000069 * dpo_edges[child_type][child_proto][parent_type][parent_proto] = edge_index
Neale Ranns0bfe5d82016-08-25 15:29:12 +010070 *
71 * This array is derived at init time from the dpo_nodes above. Note that
72 * the third dimension in dpo_nodes is lost, hence, the edge index from each
73 * node MUST be the same.
Neale Ranns8fe8cc22016-11-01 10:05:08 +000074 * Including both the child and parent protocol is required to support the
75 * case where it changes as the grapth is traversed, most notablly when an
76 * MPLS label is popped.
Neale Ranns0bfe5d82016-08-25 15:29:12 +010077 *
78 * Note that this array is child type specific, not child instance specific.
79 */
Neale Ranns8fe8cc22016-11-01 10:05:08 +000080static u32 ****dpo_edges;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010081
82/**
83 * @brief The DPO type value that can be assigend to the next dynamic
84 * type registration.
85 */
86static dpo_type_t dpo_dynamic = DPO_LAST;
87
Neale Rannsad95b5d2016-11-10 20:35:14 +000088dpo_proto_t
89vnet_link_to_dpo_proto (vnet_link_t linkt)
90{
91 switch (linkt)
92 {
93 case VNET_LINK_IP6:
94 return (DPO_PROTO_IP6);
95 case VNET_LINK_IP4:
96 return (DPO_PROTO_IP4);
97 case VNET_LINK_MPLS:
98 return (DPO_PROTO_MPLS);
99 case VNET_LINK_ETHERNET:
100 return (DPO_PROTO_ETHERNET);
Florin Corasce1b4c72017-01-26 14:25:34 -0800101 case VNET_LINK_NSH:
102 return (DPO_PROTO_NSH);
Neale Rannsad95b5d2016-11-10 20:35:14 +0000103 case VNET_LINK_ARP:
104 break;
105 }
106 ASSERT(0);
107 return (0);
108}
109
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100110u8 *
111format_dpo_type (u8 * s, va_list * args)
112{
113 dpo_type_t type = va_arg (*args, int);
114
115 s = format(s, "%s", dpo_type_names[type]);
116
117 return (s);
118}
119
120u8 *
121format_dpo_id (u8 * s, va_list * args)
122{
123 dpo_id_t *dpo = va_arg (*args, dpo_id_t*);
124 u32 indent = va_arg (*args, u32);
125
126 s = format(s, "[@%d]: ", dpo->dpoi_next_node);
127
128 if (NULL != dpo_vfts[dpo->dpoi_type].dv_format)
129 {
130 return (format(s, "%U",
131 dpo_vfts[dpo->dpoi_type].dv_format,
132 dpo->dpoi_index,
133 indent));
134 }
135
136 switch (dpo->dpoi_type)
137 {
138 case DPO_FIRST:
139 s = format(s, "unset");
140 break;
141 default:
142 s = format(s, "unknown");
143 break;
144 }
145 return (s);
146}
147
148u8 *
149format_dpo_proto (u8 * s, va_list * args)
150{
151 dpo_proto_t proto = va_arg (*args, int);
152
153 return (format(s, "%s", dpo_proto_names[proto]));
154}
155
156void
157dpo_set (dpo_id_t *dpo,
158 dpo_type_t type,
159 dpo_proto_t proto,
160 index_t index)
161{
162 dpo_id_t tmp = *dpo;
163
164 dpo->dpoi_type = type;
165 dpo->dpoi_proto = proto,
166 dpo->dpoi_index = index;
167
168 if (DPO_ADJACENCY == type)
169 {
170 /*
171 * set the adj subtype
172 */
173 ip_adjacency_t *adj;
174
175 adj = adj_get(index);
176
177 switch (adj->lookup_next_index)
178 {
179 case IP_LOOKUP_NEXT_ARP:
180 dpo->dpoi_type = DPO_ADJACENCY_INCOMPLETE;
181 break;
182 case IP_LOOKUP_NEXT_MIDCHAIN:
183 dpo->dpoi_type = DPO_ADJACENCY_MIDCHAIN;
184 break;
185 default:
186 break;
187 }
188 }
189 dpo_lock(dpo);
190 dpo_unlock(&tmp);
191}
192
193void
194dpo_reset (dpo_id_t *dpo)
195{
Neale Rannsad95b5d2016-11-10 20:35:14 +0000196 dpo_id_t tmp = DPO_INVALID;
197
198 /*
199 * use the atomic copy operation.
200 */
201 dpo_copy(dpo, &tmp);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100202}
203
204/**
205 * \brief
206 * Compare two Data-path objects
207 *
208 * like memcmp, return 0 is matching, !0 otherwise.
209 */
210int
211dpo_cmp (const dpo_id_t *dpo1,
212 const dpo_id_t *dpo2)
213{
214 int res;
215
216 res = dpo1->dpoi_type - dpo2->dpoi_type;
217
218 if (0 != res) return (res);
219
220 return (dpo1->dpoi_index - dpo2->dpoi_index);
221}
222
223void
224dpo_copy (dpo_id_t *dst,
225 const dpo_id_t *src)
226{
227 dpo_id_t tmp = *dst;
228
229 /*
230 * the destination is written in a single u64 write - hence atomically w.r.t
231 * any packets inflight.
232 */
233 *((u64*)dst) = *(u64*)src;
234
235 dpo_lock(dst);
236 dpo_unlock(&tmp);
237}
238
239int
240dpo_is_adj (const dpo_id_t *dpo)
241{
242 return ((dpo->dpoi_type == DPO_ADJACENCY) ||
243 (dpo->dpoi_type == DPO_ADJACENCY_INCOMPLETE) ||
244 (dpo->dpoi_type == DPO_ADJACENCY_MIDCHAIN) ||
245 (dpo->dpoi_type == DPO_ADJACENCY_GLEAN));
246}
247
248void
249dpo_register (dpo_type_t type,
250 const dpo_vft_t *vft,
251 const char * const * const * nodes)
252{
253 vec_validate(dpo_vfts, type);
254 dpo_vfts[type] = *vft;
255
256 vec_validate(dpo_nodes, type);
257 dpo_nodes[type] = nodes;
258}
259
260dpo_type_t
261dpo_register_new_type (const dpo_vft_t *vft,
262 const char * const * const * nodes)
263{
264 dpo_type_t type = dpo_dynamic++;
265
266 dpo_register(type, vft, nodes);
267
268 return (type);
269}
270
271void
272dpo_lock (dpo_id_t *dpo)
273{
274 if (!dpo_id_is_valid(dpo))
275 return;
276
277 dpo_vfts[dpo->dpoi_type].dv_lock(dpo);
278}
279
280void
281dpo_unlock (dpo_id_t *dpo)
282{
283 if (!dpo_id_is_valid(dpo))
284 return;
285
286 dpo_vfts[dpo->dpoi_type].dv_unlock(dpo);
287}
288
289
290static u32
291dpo_get_next_node (dpo_type_t child_type,
292 dpo_proto_t child_proto,
293 const dpo_id_t *parent_dpo)
294{
295 dpo_proto_t parent_proto;
296 dpo_type_t parent_type;
297
298 parent_type = parent_dpo->dpoi_type;
299 parent_proto = parent_dpo->dpoi_proto;
300
301 vec_validate(dpo_edges, child_type);
302 vec_validate(dpo_edges[child_type], child_proto);
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000303 vec_validate(dpo_edges[child_type][child_proto], parent_type);
304 vec_validate_init_empty(
305 dpo_edges[child_type][child_proto][parent_type],
306 parent_proto, ~0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100307
308 /*
309 * if the edge index has not yet been created for this node to node transistion
310 */
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000311 if (~0 == dpo_edges[child_type][child_proto][parent_type][parent_proto])
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100312 {
313 vlib_node_t *parent_node, *child_node;
314 vlib_main_t *vm;
315 u32 edge ,pp, cc;
316
317 vm = vlib_get_main();
318
319 ASSERT(NULL != dpo_nodes[child_type]);
320 ASSERT(NULL != dpo_nodes[child_type][child_proto]);
321 ASSERT(NULL != dpo_nodes[parent_type]);
322 ASSERT(NULL != dpo_nodes[parent_type][parent_proto]);
323
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000324 cc = 0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100325
326 /*
327 * create a graph arc from each of the parent's registered node types,
328 * to each of the childs.
329 */
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000330 while (NULL != dpo_nodes[child_type][child_proto][cc])
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100331 {
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000332 child_node =
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100333 vlib_get_node_by_name(vm,
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000334 (u8*) dpo_nodes[child_type][child_proto][cc]);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100335
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000336 pp = 0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100337
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000338 while (NULL != dpo_nodes[parent_type][parent_proto][pp])
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100339 {
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000340 parent_node =
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100341 vlib_get_node_by_name(vm,
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000342 (u8*) dpo_nodes[parent_type][parent_proto][pp]);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100343
344 edge = vlib_node_add_next(vm,
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000345 child_node->index,
346 parent_node->index);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100347
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000348 if (~0 == dpo_edges[child_type][child_proto][parent_type][parent_proto])
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100349 {
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000350 dpo_edges[child_type][child_proto][parent_type][parent_proto] = edge;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100351 }
352 else
353 {
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000354 ASSERT(dpo_edges[child_type][child_proto][parent_type][parent_proto] == edge);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100355 }
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000356 pp++;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100357 }
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000358 cc++;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100359 }
360 }
361
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000362 return (dpo_edges[child_type][child_proto][parent_type][parent_proto]);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100363}
364
365/**
366 * @brief Stack one DPO object on another, and thus establish a child parent
367 * relationship. The VLIB graph arc used is taken from the parent and child types
368 * passed.
369 */
370static void
371dpo_stack_i (u32 edge,
372 dpo_id_t *dpo,
373 const dpo_id_t *parent)
374{
375 /*
376 * in order to get an atomic update of the parent we create a temporary,
377 * from a copy of the child, and add the next_node. then we copy to the parent
378 */
Neale Ranns948e00f2016-10-20 13:39:34 +0100379 dpo_id_t tmp = DPO_INVALID;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100380 dpo_copy(&tmp, parent);
381
382 /*
383 * get the edge index for the parent to child VLIB graph transisition
384 */
385 tmp.dpoi_next_node = edge;
386
387 /*
388 * this update is atomic.
389 */
390 dpo_copy(dpo, &tmp);
391
392 dpo_reset(&tmp);
393}
394
395/**
396 * @brief Stack one DPO object on another, and thus establish a child-parent
397 * relationship. The VLIB graph arc used is taken from the parent and child types
398 * passed.
399 */
400void
401dpo_stack (dpo_type_t child_type,
402 dpo_proto_t child_proto,
403 dpo_id_t *dpo,
404 const dpo_id_t *parent)
405{
406 dpo_stack_i(dpo_get_next_node(child_type, child_proto, parent), dpo, parent);
407}
408
409/**
410 * @brief Stack one DPO object on another, and thus establish a child parent
411 * relationship. A new VLIB graph arc is created from the child node passed
412 * to the nodes registered by the parent. The VLIB infra will ensure this arc
413 * is added only once.
414 */
415void
416dpo_stack_from_node (u32 child_node_index,
417 dpo_id_t *dpo,
418 const dpo_id_t *parent)
419{
420 dpo_proto_t parent_proto;
421 vlib_node_t *parent_node;
422 dpo_type_t parent_type;
423 vlib_main_t *vm;
424 u32 edge;
425
426 parent_type = parent->dpoi_type;
427 parent_proto = parent->dpoi_proto;
428
429 vm = vlib_get_main();
430
431 ASSERT(NULL != dpo_nodes[parent_type]);
432 ASSERT(NULL != dpo_nodes[parent_type][parent_proto]);
433
434 parent_node =
435 vlib_get_node_by_name(vm, (u8*) dpo_nodes[parent_type][parent_proto][0]);
436
437 edge = vlib_node_add_next(vm,
438 child_node_index,
439 parent_node->index);
440
441 dpo_stack_i(edge, dpo, parent);
442}
443
444static clib_error_t *
445dpo_module_init (vlib_main_t * vm)
446{
447 drop_dpo_module_init();
448 punt_dpo_module_init();
449 receive_dpo_module_init();
450 load_balance_module_init();
451 mpls_label_dpo_module_init();
452 classify_dpo_module_init();
453 lookup_dpo_module_init();
Neale Ranns948e00f2016-10-20 13:39:34 +0100454 ip_null_dpo_module_init();
Neale Ranns32e1c012016-11-22 17:07:28 +0000455 replicate_module_init();
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100456
457 return (NULL);
458}
459
460VLIB_INIT_FUNCTION(dpo_module_init);
Neale Ranns6c3ebcc2016-10-02 21:20:15 +0100461
462static clib_error_t *
463dpo_memory_show (vlib_main_t * vm,
464 unformat_input_t * input,
465 vlib_cli_command_t * cmd)
466{
467 dpo_vft_t *vft;
468
469 vlib_cli_output (vm, "DPO memory");
470 vlib_cli_output (vm, "%=30s %=5s %=8s/%=9s totals",
471 "Name","Size", "in-use", "allocated");
472
473 vec_foreach(vft, dpo_vfts)
474 {
475 if (NULL != vft->dv_mem_show)
476 vft->dv_mem_show();
477 }
478
479 return (NULL);
480}
481
482/* *INDENT-OFF* */
483/*?
484 * The '<em>sh dpo memory </em>' command displays the memory usage for each
485 * data-plane object type.
486 *
487 * @cliexpar
488 * @cliexstart{show dpo memory}
489 * DPO memory
490 * Name Size in-use /allocated totals
491 * load-balance 64 12 / 12 768/768
492 * Adjacency 256 1 / 1 256/256
493 * Receive 24 5 / 5 120/120
494 * Lookup 12 0 / 0 0/0
495 * Classify 12 0 / 0 0/0
496 * MPLS label 24 0 / 0 0/0
497 * @cliexend
498?*/
499VLIB_CLI_COMMAND (show_fib_memory, static) = {
500 .path = "show dpo memory",
501 .function = dpo_memory_show,
502 .short_help = "show dpo memory",
503};
504/* *INDENT-ON* */