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