MPLS Mcast
[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_dpo.h>
41 #include <vnet/dpo/mpls_disposition.h>
42
43 /**
44  * Array of char* names for the DPO types and protos
45  */
46 static const char* dpo_type_names[] = DPO_TYPES;
47 static 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  */
54 static 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  */
66 static const char* const * const ** dpo_nodes;
67
68 /**
69  * @brief Vector of edge indicies from parent DPO nodes to child
70  *
71  * dpo_edges[child_type][child_proto][parent_type][parent_proto] = edge_index
72  *
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.
76  * 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.
79  *
80  * Note that this array is child type specific, not child instance specific.
81  */
82 static u32 ****dpo_edges;
83
84 /**
85  * @brief The DPO type value that can be assigend to the next dynamic
86  *        type registration.
87  */
88 static dpo_type_t dpo_dynamic = DPO_LAST;
89
90 dpo_proto_t
91 vnet_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);
103     case VNET_LINK_NSH:
104         return (DPO_PROTO_NSH);
105     case VNET_LINK_ARP:
106         break;
107     }
108     ASSERT(0);
109     return (0);
110 }
111
112 u8 *
113 format_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
122 u8 *
123 format_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
150 u8 *
151 format_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
158 void
159 dpo_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;
187         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;
192             break;
193         default:
194             break;
195         }
196     }
197     dpo_lock(dpo);
198     dpo_unlock(&tmp);
199 }
200
201 void
202 dpo_reset (dpo_id_t *dpo)
203 {
204     dpo_id_t tmp = DPO_INVALID;
205
206     /*
207      * use the atomic copy operation.
208      */
209     dpo_copy(dpo, &tmp);
210 }
211
212 /**
213  * \brief
214  * Compare two Data-path objects
215  *
216  * like memcmp, return 0 is matching, !0 otherwise.
217  */
218 int
219 dpo_cmp (const dpo_id_t *dpo1,
220          const dpo_id_t *dpo2)
221 {
222     int res;
223
224     res = dpo1->dpoi_type - dpo2->dpoi_type;
225
226     if (0 != res) return (res);
227
228     return (dpo1->dpoi_index - dpo2->dpoi_index);
229 }
230
231 void
232 dpo_copy (dpo_id_t *dst,
233           const dpo_id_t *src)
234 {
235     dpo_id_t tmp = *dst;
236
237     /*
238      * the destination is written in a single u64 write - hence atomically w.r.t
239      * any packets inflight.
240      */
241     *((u64*)dst) = *(u64*)src; 
242
243     dpo_lock(dst);
244     dpo_unlock(&tmp);    
245 }
246
247 int
248 dpo_is_adj (const dpo_id_t *dpo)
249 {
250     return ((dpo->dpoi_type == DPO_ADJACENCY) ||
251             (dpo->dpoi_type == DPO_ADJACENCY_INCOMPLETE) ||
252             (dpo->dpoi_type == DPO_ADJACENCY_MIDCHAIN) ||
253             (dpo->dpoi_type == DPO_ADJACENCY_GLEAN));
254 }
255
256 void
257 dpo_register (dpo_type_t type,
258               const dpo_vft_t *vft,
259               const char * const * const * nodes)
260 {
261     vec_validate(dpo_vfts, type);
262     dpo_vfts[type] = *vft;
263
264     vec_validate(dpo_nodes, type);
265     dpo_nodes[type] = nodes;
266 }
267
268 dpo_type_t
269 dpo_register_new_type (const dpo_vft_t *vft,
270                        const char * const * const * nodes)
271 {
272     dpo_type_t type = dpo_dynamic++;
273
274     dpo_register(type, vft, nodes);
275
276     return (type);
277 }
278
279 void
280 dpo_lock (dpo_id_t *dpo)
281 {
282     if (!dpo_id_is_valid(dpo))
283         return;
284
285     dpo_vfts[dpo->dpoi_type].dv_lock(dpo);
286 }
287
288 void
289 dpo_unlock (dpo_id_t *dpo)
290 {
291     if (!dpo_id_is_valid(dpo))
292         return;
293
294     dpo_vfts[dpo->dpoi_type].dv_unlock(dpo);
295 }
296
297
298 static u32
299 dpo_get_next_node (dpo_type_t child_type,
300                    dpo_proto_t child_proto,
301                    const dpo_id_t *parent_dpo)
302 {
303     dpo_proto_t parent_proto;
304     dpo_type_t parent_type;
305
306     parent_type = parent_dpo->dpoi_type;
307     parent_proto = parent_dpo->dpoi_proto;
308
309     vec_validate(dpo_edges, child_type);
310     vec_validate(dpo_edges[child_type], child_proto);
311     vec_validate(dpo_edges[child_type][child_proto], parent_type);
312     vec_validate_init_empty(
313         dpo_edges[child_type][child_proto][parent_type],
314         parent_proto, ~0);
315
316     /*
317      * if the edge index has not yet been created for this node to node transistion
318      */
319     if (~0 == dpo_edges[child_type][child_proto][parent_type][parent_proto])
320     {
321         vlib_node_t *parent_node, *child_node;
322         vlib_main_t *vm;
323         u32 edge ,pp, cc;
324
325         vm = vlib_get_main();
326
327         ASSERT(NULL != dpo_nodes[child_type]);
328         ASSERT(NULL != dpo_nodes[child_type][child_proto]);
329         ASSERT(NULL != dpo_nodes[parent_type]);
330         ASSERT(NULL != dpo_nodes[parent_type][parent_proto]);
331
332         cc = 0;
333
334         /*
335          * create a graph arc from each of the parent's registered node types,
336          * to each of the childs.
337          */
338         while (NULL != dpo_nodes[child_type][child_proto][cc])
339         {
340             child_node =
341                 vlib_get_node_by_name(vm,
342                                       (u8*) dpo_nodes[child_type][child_proto][cc]);
343
344             pp = 0;
345
346             while (NULL != dpo_nodes[parent_type][parent_proto][pp])
347             {
348                 parent_node =
349                     vlib_get_node_by_name(vm,
350                                           (u8*) dpo_nodes[parent_type][parent_proto][pp]);
351
352                 edge = vlib_node_add_next(vm,
353                                           child_node->index,
354                                           parent_node->index);
355
356                 if (~0 == dpo_edges[child_type][child_proto][parent_type][parent_proto])
357                 {
358                     dpo_edges[child_type][child_proto][parent_type][parent_proto] = edge;
359                 }
360                 else
361                 {
362                     ASSERT(dpo_edges[child_type][child_proto][parent_type][parent_proto] == edge);
363                 }
364                 pp++;
365             }
366             cc++;
367         }
368     }
369
370     return (dpo_edges[child_type][child_proto][parent_type][parent_proto]);
371 }
372
373 /**
374  * @brief Stack one DPO object on another, and thus establish a child parent
375  * relationship. The VLIB graph arc used is taken from the parent and child types
376  * passed.
377  */
378 static void
379 dpo_stack_i (u32 edge,
380              dpo_id_t *dpo,
381              const dpo_id_t *parent)
382 {
383     /*
384      * in order to get an atomic update of the parent we create a temporary,
385      * from a copy of the child, and add the next_node. then we copy to the parent
386      */
387     dpo_id_t tmp = DPO_INVALID;
388     dpo_copy(&tmp, parent);
389
390     /*
391      * get the edge index for the parent to child VLIB graph transisition
392      */
393     tmp.dpoi_next_node = edge;
394
395     /*
396      * this update is atomic.
397      */
398     dpo_copy(dpo, &tmp);
399
400     dpo_reset(&tmp);
401 }
402
403 /**
404  * @brief Stack one DPO object on another, and thus establish a child-parent
405  * relationship. The VLIB graph arc used is taken from the parent and child types
406  * passed.
407  */
408 void
409 dpo_stack (dpo_type_t child_type,
410            dpo_proto_t child_proto,
411            dpo_id_t *dpo,
412            const dpo_id_t *parent)
413 {
414     dpo_stack_i(dpo_get_next_node(child_type, child_proto, parent), dpo, parent);
415 }
416
417 /**
418  * @brief Stack one DPO object on another, and thus establish a child parent
419  * relationship. A new VLIB graph arc is created from the child node passed
420  * to the nodes registered by the parent. The VLIB infra will ensure this arc
421  * is added only once.
422  */
423 void
424 dpo_stack_from_node (u32 child_node_index,
425                      dpo_id_t *dpo,
426                      const dpo_id_t *parent)
427 {
428     dpo_proto_t parent_proto;
429     vlib_node_t *parent_node;
430     dpo_type_t parent_type;
431     vlib_main_t *vm;
432     u32 edge;
433
434     parent_type = parent->dpoi_type;
435     parent_proto = parent->dpoi_proto;
436
437     vm = vlib_get_main();
438
439     ASSERT(NULL != dpo_nodes[parent_type]);
440     ASSERT(NULL != dpo_nodes[parent_type][parent_proto]);
441
442     parent_node =
443         vlib_get_node_by_name(vm, (u8*) dpo_nodes[parent_type][parent_proto][0]);
444
445     edge = vlib_node_add_next(vm,
446                               child_node_index,
447                               parent_node->index);
448
449     dpo_stack_i(edge, dpo, parent);
450 }
451
452 static clib_error_t *
453 dpo_module_init (vlib_main_t * vm)
454 {
455     drop_dpo_module_init();
456     punt_dpo_module_init();
457     receive_dpo_module_init();
458     load_balance_module_init();
459     mpls_label_dpo_module_init();
460     classify_dpo_module_init();
461     lookup_dpo_module_init();
462     ip_null_dpo_module_init();
463     replicate_module_init();
464     interface_dpo_module_init();
465     mpls_disp_dpo_module_init();
466
467     return (NULL);
468 }
469
470 VLIB_INIT_FUNCTION(dpo_module_init);
471
472 static clib_error_t *
473 dpo_memory_show (vlib_main_t * vm,
474                  unformat_input_t * input,
475                  vlib_cli_command_t * cmd)
476 {
477     dpo_vft_t *vft;
478
479     vlib_cli_output (vm, "DPO memory");
480     vlib_cli_output (vm, "%=30s %=5s %=8s/%=9s   totals",
481                      "Name","Size", "in-use", "allocated");
482
483     vec_foreach(vft, dpo_vfts)
484     {
485         if (NULL != vft->dv_mem_show)
486             vft->dv_mem_show();
487     }
488
489     return (NULL);
490 }
491
492 /* *INDENT-OFF* */
493 /*?
494  * The '<em>sh dpo memory </em>' command displays the memory usage for each
495  * data-plane object type.
496  *
497  * @cliexpar
498  * @cliexstart{show dpo memory}
499  * DPO memory
500  *             Name               Size  in-use /allocated   totals
501  *         load-balance            64     12   /    12      768/768
502  *           Adjacency            256      1   /    1       256/256
503  *            Receive              24      5   /    5       120/120
504  *            Lookup               12      0   /    0       0/0
505  *           Classify              12      0   /    0       0/0
506  *          MPLS label             24      0   /    0       0/0
507  * @cliexend
508 ?*/
509 VLIB_CLI_COMMAND (show_fib_memory, static) = {
510     .path = "show dpo memory",
511     .function = dpo_memory_show,
512     .short_help = "show dpo memory",
513 };
514 /* *INDENT-ON* */