Distributed Virtual Router Support
[vpp.git] / src / vnet / dpo / dpo.c
1 /*
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>
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>
43 #include <vnet/dpo/l2_bridge_dpo.h>
44
45 /**
46  * Array of char* names for the DPO types and protos
47  */
48 static const char* dpo_type_names[] = DPO_TYPES;
49 static const char* dpo_proto_names[] = DPO_PROTOS;
50
51 /**
52  * @brief Vector of virtual function tables for the DPO types
53  *
54  * This is a vector so we can dynamically register new DPO types in plugins.
55  */
56 static dpo_vft_t *dpo_vfts;
57
58 /**
59  * @brief vector of graph node names associated with each DPO type and protocol.
60  *
61  *   dpo_nodes[child_type][child_proto][node_X] = node_name;
62  * i.e.
63  *   dpo_node[DPO_LOAD_BALANCE][DPO_PROTO_IP4][0] = "ip4-lookup"
64  *   dpo_node[DPO_LOAD_BALANCE][DPO_PROTO_IP4][1] = "ip4-load-balance"
65  *
66  * This is a vector so we can dynamically register new DPO types in plugins.
67  */
68 static const char* const * const ** dpo_nodes;
69
70 /**
71  * @brief Vector of edge indicies from parent DPO nodes to child
72  *
73  * dpo_edges[child_type][child_proto][parent_type][parent_proto] = edge_index
74  *
75  * This array is derived at init time from the dpo_nodes above. Note that
76  * the third dimension in dpo_nodes is lost, hence, the edge index from each
77  * node MUST be the same.
78  * Including both the child and parent protocol is required to support the
79  * case where it changes as the grapth is traversed, most notablly when an
80  * MPLS label is popped.
81  *
82  * Note that this array is child type specific, not child instance specific.
83  */
84 static u32 ****dpo_edges;
85
86 /**
87  * @brief The DPO type value that can be assigend to the next dynamic
88  *        type registration.
89  */
90 static dpo_type_t dpo_dynamic = DPO_LAST;
91
92 dpo_proto_t
93 vnet_link_to_dpo_proto (vnet_link_t linkt)
94 {
95     switch (linkt)
96     {
97     case VNET_LINK_IP6:
98         return (DPO_PROTO_IP6);
99     case VNET_LINK_IP4:
100         return (DPO_PROTO_IP4);
101     case VNET_LINK_MPLS:
102         return (DPO_PROTO_MPLS);
103     case VNET_LINK_ETHERNET:
104         return (DPO_PROTO_ETHERNET);
105     case VNET_LINK_NSH:
106         return (DPO_PROTO_NSH);
107     case VNET_LINK_ARP:
108         break;
109     }
110     ASSERT(0);
111     return (0);
112 }
113
114 vnet_link_t
115 dpo_proto_to_link (dpo_proto_t dp)
116 {
117     switch (dp)
118     {
119     case DPO_PROTO_IP6:
120         return (VNET_LINK_IP6);
121     case DPO_PROTO_IP4:
122         return (VNET_LINK_IP4);
123     case DPO_PROTO_MPLS:
124         return (VNET_LINK_MPLS);
125     case DPO_PROTO_ETHERNET:
126         return (VNET_LINK_ETHERNET);
127     case DPO_PROTO_NSH:
128         return (VNET_LINK_NSH);
129     }
130     return (~0);
131 }
132
133 u8 *
134 format_dpo_type (u8 * s, va_list * args)
135 {
136     dpo_type_t type = va_arg (*args, int);
137
138     s = format(s, "%s", dpo_type_names[type]);
139
140     return (s);
141 }
142
143 u8 *
144 format_dpo_id (u8 * s, va_list * args)
145 {
146     dpo_id_t *dpo = va_arg (*args, dpo_id_t*);
147     u32 indent = va_arg (*args, u32);
148
149     s = format(s, "[@%d]: ", dpo->dpoi_next_node);
150
151     if (NULL != dpo_vfts[dpo->dpoi_type].dv_format)
152     {
153         return (format(s, "%U",
154                        dpo_vfts[dpo->dpoi_type].dv_format,
155                        dpo->dpoi_index,
156                        indent));
157     }
158
159     switch (dpo->dpoi_type)
160     {
161     case DPO_FIRST:
162         s = format(s, "unset");
163         break;
164     default:
165         s = format(s, "unknown");
166         break;
167     }
168     return (s);
169 }
170
171 u8 *
172 format_dpo_proto (u8 * s, va_list * args)
173 {
174     dpo_proto_t proto = va_arg (*args, int);
175
176     return (format(s, "%s", dpo_proto_names[proto]));
177 }
178
179 void
180 dpo_set (dpo_id_t *dpo,
181          dpo_type_t type,
182          dpo_proto_t proto,
183          index_t index)
184 {
185     dpo_id_t tmp = *dpo;
186
187     dpo->dpoi_type = type;
188     dpo->dpoi_proto = proto,
189     dpo->dpoi_index = index;
190
191     if (DPO_ADJACENCY == type)
192     {
193         /*
194          * set the adj subtype
195          */
196         ip_adjacency_t *adj;
197
198         adj = adj_get(index);
199
200         switch (adj->lookup_next_index)
201         {
202         case IP_LOOKUP_NEXT_ARP:
203             dpo->dpoi_type = DPO_ADJACENCY_INCOMPLETE;
204             break;
205         case IP_LOOKUP_NEXT_MIDCHAIN:
206             dpo->dpoi_type = DPO_ADJACENCY_MIDCHAIN;
207             break;
208         case IP_LOOKUP_NEXT_MCAST_MIDCHAIN:
209             dpo->dpoi_type = DPO_ADJACENCY_MCAST_MIDCHAIN;
210             break;
211         case IP_LOOKUP_NEXT_MCAST:
212             dpo->dpoi_type = DPO_ADJACENCY_MCAST;
213             break;
214         case IP_LOOKUP_NEXT_GLEAN:
215             dpo->dpoi_type = DPO_ADJACENCY_GLEAN;
216             break;
217         default:
218             break;
219         }
220     }
221     dpo_lock(dpo);
222     dpo_unlock(&tmp);
223 }
224
225 void
226 dpo_reset (dpo_id_t *dpo)
227 {
228     dpo_id_t tmp = DPO_INVALID;
229
230     /*
231      * use the atomic copy operation.
232      */
233     dpo_copy(dpo, &tmp);
234 }
235
236 /**
237  * \brief
238  * Compare two Data-path objects
239  *
240  * like memcmp, return 0 is matching, !0 otherwise.
241  */
242 int
243 dpo_cmp (const dpo_id_t *dpo1,
244          const dpo_id_t *dpo2)
245 {
246     int res;
247
248     res = dpo1->dpoi_type - dpo2->dpoi_type;
249
250     if (0 != res) return (res);
251
252     return (dpo1->dpoi_index - dpo2->dpoi_index);
253 }
254
255 void
256 dpo_copy (dpo_id_t *dst,
257           const dpo_id_t *src)
258 {
259     dpo_id_t tmp = *dst;
260
261     /*
262      * the destination is written in a single u64 write - hence atomically w.r.t
263      * any packets inflight.
264      */
265     *((u64*)dst) = *(u64*)src; 
266
267     dpo_lock(dst);
268     dpo_unlock(&tmp);    
269 }
270
271 int
272 dpo_is_adj (const dpo_id_t *dpo)
273 {
274     return ((dpo->dpoi_type == DPO_ADJACENCY) ||
275             (dpo->dpoi_type == DPO_ADJACENCY_INCOMPLETE) ||
276             (dpo->dpoi_type == DPO_ADJACENCY_MIDCHAIN) ||
277             (dpo->dpoi_type == DPO_ADJACENCY_GLEAN));
278 }
279
280 static u32 *
281 dpo_default_get_next_node (const dpo_id_t *dpo)
282 {
283     u32 *node_indices = NULL;
284     const char *node_name;
285     u32 ii = 0;
286
287     node_name = dpo_nodes[dpo->dpoi_type][dpo->dpoi_proto][ii];
288     while (NULL != node_name)
289     {
290         vlib_node_t *node;
291
292         node = vlib_get_node_by_name(vlib_get_main(), (u8*) node_name);
293         ASSERT(NULL != node);
294         vec_add1(node_indices, node->index);
295
296         ++ii;
297         node_name = dpo_nodes[dpo->dpoi_type][dpo->dpoi_proto][ii];
298     }
299
300     return (node_indices);
301 }
302
303 void
304 dpo_register (dpo_type_t type,
305               const dpo_vft_t *vft,
306               const char * const * const * nodes)
307 {
308     vec_validate(dpo_vfts, type);
309     dpo_vfts[type] = *vft;
310     if (NULL == dpo_vfts[type].dv_get_next_node)
311     {
312         dpo_vfts[type].dv_get_next_node = dpo_default_get_next_node;
313     }
314
315     vec_validate(dpo_nodes, type);
316     dpo_nodes[type] = nodes;
317 }
318
319 dpo_type_t
320 dpo_register_new_type (const dpo_vft_t *vft,
321                        const char * const * const * nodes)
322 {
323     dpo_type_t type = dpo_dynamic++;
324
325     dpo_register(type, vft, nodes);
326
327     return (type);
328 }
329
330 void
331 dpo_lock (dpo_id_t *dpo)
332 {
333     if (!dpo_id_is_valid(dpo))
334         return;
335
336     dpo_vfts[dpo->dpoi_type].dv_lock(dpo);
337 }
338
339 void
340 dpo_unlock (dpo_id_t *dpo)
341 {
342     if (!dpo_id_is_valid(dpo))
343         return;
344
345     dpo_vfts[dpo->dpoi_type].dv_unlock(dpo);
346 }
347
348
349 static u32
350 dpo_get_next_node (dpo_type_t child_type,
351                    dpo_proto_t child_proto,
352                    const dpo_id_t *parent_dpo)
353 {
354     dpo_proto_t parent_proto;
355     dpo_type_t parent_type;
356
357     parent_type = parent_dpo->dpoi_type;
358     parent_proto = parent_dpo->dpoi_proto;
359
360     vec_validate(dpo_edges, child_type);
361     vec_validate(dpo_edges[child_type], child_proto);
362     vec_validate(dpo_edges[child_type][child_proto], parent_type);
363     vec_validate_init_empty(
364         dpo_edges[child_type][child_proto][parent_type],
365         parent_proto, ~0);
366
367     /*
368      * if the edge index has not yet been created for this node to node transistion
369      */
370     if (~0 == dpo_edges[child_type][child_proto][parent_type][parent_proto])
371     {
372         vlib_node_t *child_node;
373         u32 *parent_indices;
374         vlib_main_t *vm;
375         u32 edge, *pi, cc;
376
377         vm = vlib_get_main();
378
379         ASSERT(NULL != dpo_vfts[parent_type].dv_get_next_node);
380         ASSERT(NULL != dpo_nodes[child_type]);
381         ASSERT(NULL != dpo_nodes[child_type][child_proto]);
382
383         cc = 0;
384         parent_indices = dpo_vfts[parent_type].dv_get_next_node(parent_dpo);
385
386         vlib_worker_thread_barrier_sync(vm);
387
388         /*
389          * create a graph arc from each of the child's registered node types,
390          * to each of the parent's.
391          */
392         while (NULL != dpo_nodes[child_type][child_proto][cc])
393         {
394             child_node =
395                 vlib_get_node_by_name(vm,
396                                       (u8*) dpo_nodes[child_type][child_proto][cc]);
397
398             vec_foreach(pi, parent_indices)
399             {
400                 edge = vlib_node_add_next(vm, child_node->index, *pi);
401
402                 if (~0 == dpo_edges[child_type][child_proto][parent_type][parent_proto])
403                 {
404                     dpo_edges[child_type][child_proto][parent_type][parent_proto] = edge;
405                 }
406                 else
407                 {
408                     ASSERT(dpo_edges[child_type][child_proto][parent_type][parent_proto] == edge);
409                 }
410             }
411             cc++;
412         }
413
414         vlib_worker_thread_barrier_release(vm);
415         vec_free(parent_indices);
416     }
417
418     return (dpo_edges[child_type][child_proto][parent_type][parent_proto]);
419 }
420
421 /**
422  * @brief Stack one DPO object on another, and thus establish a child parent
423  * relationship. The VLIB graph arc used is taken from the parent and child types
424  * passed.
425  */
426 static void
427 dpo_stack_i (u32 edge,
428              dpo_id_t *dpo,
429              const dpo_id_t *parent)
430 {
431     /*
432      * in order to get an atomic update of the parent we create a temporary,
433      * from a copy of the child, and add the next_node. then we copy to the parent
434      */
435     dpo_id_t tmp = DPO_INVALID;
436     dpo_copy(&tmp, parent);
437
438     /*
439      * get the edge index for the parent to child VLIB graph transisition
440      */
441     tmp.dpoi_next_node = edge;
442
443     /*
444      * this update is atomic.
445      */
446     dpo_copy(dpo, &tmp);
447
448     dpo_reset(&tmp);
449 }
450
451 /**
452  * @brief Stack one DPO object on another, and thus establish a child-parent
453  * relationship. The VLIB graph arc used is taken from the parent and child types
454  * passed.
455  */
456 void
457 dpo_stack (dpo_type_t child_type,
458            dpo_proto_t child_proto,
459            dpo_id_t *dpo,
460            const dpo_id_t *parent)
461 {
462     dpo_stack_i(dpo_get_next_node(child_type, child_proto, parent), dpo, parent);
463 }
464
465 /**
466  * @brief Stack one DPO object on another, and thus establish a child parent
467  * relationship. A new VLIB graph arc is created from the child node passed
468  * to the nodes registered by the parent. The VLIB infra will ensure this arc
469  * is added only once.
470  */
471 void
472 dpo_stack_from_node (u32 child_node_index,
473                      dpo_id_t *dpo,
474                      const dpo_id_t *parent)
475 {
476     dpo_type_t parent_type;
477     u32 *parent_indices;
478     vlib_main_t *vm;
479     u32 edge, *pi;
480
481     edge = 0;
482     parent_type = parent->dpoi_type;
483     vm = vlib_get_main();
484
485     ASSERT(NULL != dpo_vfts[parent_type].dv_get_next_node);
486     parent_indices = dpo_vfts[parent_type].dv_get_next_node(parent);
487     ASSERT(parent_indices);
488
489     /*
490      * This loop is purposefully written with the worker thread lock in the
491      * inner loop because;
492      *  1) the likelihood that the edge does not exist is smaller
493      *  2) the likelihood there is more than one node is even smaller
494      * so we are optimising for not need to take the lock
495      */
496     vec_foreach(pi, parent_indices)
497     {
498         edge = vlib_node_get_next(vm, child_node_index, *pi);
499
500         if (~0 == edge)
501         {
502             vlib_worker_thread_barrier_sync(vm);
503
504             edge = vlib_node_add_next(vm, child_node_index, *pi);
505
506             vlib_worker_thread_barrier_release(vm);
507         }
508     }
509     dpo_stack_i(edge, dpo, parent);
510 }
511
512 static clib_error_t *
513 dpo_module_init (vlib_main_t * vm)
514 {
515     drop_dpo_module_init();
516     punt_dpo_module_init();
517     receive_dpo_module_init();
518     load_balance_module_init();
519     mpls_label_dpo_module_init();
520     classify_dpo_module_init();
521     lookup_dpo_module_init();
522     ip_null_dpo_module_init();
523     replicate_module_init();
524     interface_rx_dpo_module_init();
525     interface_tx_dpo_module_init();
526     mpls_disp_dpo_module_init();
527     l2_bridge_dpo_module_init();
528
529     return (NULL);
530 }
531
532 VLIB_INIT_FUNCTION(dpo_module_init);
533
534 static clib_error_t *
535 dpo_memory_show (vlib_main_t * vm,
536                  unformat_input_t * input,
537                  vlib_cli_command_t * cmd)
538 {
539     dpo_vft_t *vft;
540
541     vlib_cli_output (vm, "DPO memory");
542     vlib_cli_output (vm, "%=30s %=5s %=8s/%=9s   totals",
543                      "Name","Size", "in-use", "allocated");
544
545     vec_foreach(vft, dpo_vfts)
546     {
547         if (NULL != vft->dv_mem_show)
548             vft->dv_mem_show();
549     }
550
551     return (NULL);
552 }
553
554 /* *INDENT-OFF* */
555 /*?
556  * The '<em>sh dpo memory </em>' command displays the memory usage for each
557  * data-plane object type.
558  *
559  * @cliexpar
560  * @cliexstart{show dpo memory}
561  * DPO memory
562  *             Name               Size  in-use /allocated   totals
563  *         load-balance            64     12   /    12      768/768
564  *           Adjacency            256      1   /    1       256/256
565  *            Receive              24      5   /    5       120/120
566  *            Lookup               12      0   /    0       0/0
567  *           Classify              12      0   /    0       0/0
568  *          MPLS label             24      0   /    0       0/0
569  * @cliexend
570 ?*/
571 VLIB_CLI_COMMAND (show_fib_memory, static) = {
572     .path = "show dpo memory",
573     .function = dpo_memory_show,
574     .short_help = "show dpo memory",
575 };
576 /* *INDENT-ON* */