Basic support for LISP-GPE encapsulated NSH packets
[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_NSH:
102         return (DPO_PROTO_NSH);
103     case VNET_LINK_ARP:
104         break;
105     }
106     ASSERT(0);
107     return (0);
108 }
109
110 u8 *
111 format_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
120 u8 *
121 format_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
148 u8 *
149 format_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
156 void
157 dpo_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
193 void
194 dpo_reset (dpo_id_t *dpo)
195 {
196     dpo_id_t tmp = DPO_INVALID;
197
198     /*
199      * use the atomic copy operation.
200      */
201     dpo_copy(dpo, &tmp);
202 }
203
204 /**
205  * \brief
206  * Compare two Data-path objects
207  *
208  * like memcmp, return 0 is matching, !0 otherwise.
209  */
210 int
211 dpo_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
223 void
224 dpo_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
239 int
240 dpo_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
248 void
249 dpo_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
260 dpo_type_t
261 dpo_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
271 void
272 dpo_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
280 void
281 dpo_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
290 static u32
291 dpo_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);
303     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);
307
308     /*
309      * if the edge index has not yet been created for this node to node transistion
310      */
311     if (~0 == dpo_edges[child_type][child_proto][parent_type][parent_proto])
312     {
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
324         cc = 0;
325
326         /*
327          * create a graph arc from each of the parent's registered node types,
328          * to each of the childs.
329          */
330         while (NULL != dpo_nodes[child_type][child_proto][cc])
331         {
332             child_node =
333                 vlib_get_node_by_name(vm,
334                                       (u8*) dpo_nodes[child_type][child_proto][cc]);
335
336             pp = 0;
337
338             while (NULL != dpo_nodes[parent_type][parent_proto][pp])
339             {
340                 parent_node =
341                     vlib_get_node_by_name(vm,
342                                           (u8*) dpo_nodes[parent_type][parent_proto][pp]);
343
344                 edge = vlib_node_add_next(vm,
345                                           child_node->index,
346                                           parent_node->index);
347
348                 if (~0 == dpo_edges[child_type][child_proto][parent_type][parent_proto])
349                 {
350                     dpo_edges[child_type][child_proto][parent_type][parent_proto] = edge;
351                 }
352                 else
353                 {
354                     ASSERT(dpo_edges[child_type][child_proto][parent_type][parent_proto] == edge);
355                 }
356                 pp++;
357             }
358             cc++;
359         }
360     }
361
362     return (dpo_edges[child_type][child_proto][parent_type][parent_proto]);
363 }
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  */
370 static void
371 dpo_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      */
379     dpo_id_t tmp = DPO_INVALID;
380     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  */
400 void
401 dpo_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  */
415 void
416 dpo_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
444 static clib_error_t *
445 dpo_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();
454     ip_null_dpo_module_init();
455     replicate_module_init();
456
457     return (NULL);
458 }
459
460 VLIB_INIT_FUNCTION(dpo_module_init);
461
462 static clib_error_t *
463 dpo_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 ?*/
499 VLIB_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* */