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