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