FIB Memory Usage Diagnostics
[vpp.git] / vnet / vnet / dpo / dpo.h
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's data-path.
19  * 
20  * The DPO can be considered to be like is a base class that is specialised
21  * by other objects to provide concreate actions
22  *
23  * The VLIB graph nodes are graph of DPO types, the DPO graph is a graph of
24  * instances.
25  */
26
27 #ifndef __DPO_H__
28 #define __DPO_H__
29
30 #include <vnet/vnet.h>
31
32 /**
33  * @brief An index for adjacencies.
34  * Alas 'C' is not typesafe enough to b0rk when a u32 is used instead of
35  * an index_t. However, for us humans, we can glean much more intent
36  * from the declaration
37  *  foo barindex_t t);
38  * than we can from
39  *  foo bar(u32 t);
40  */
41 typedef u32 index_t;
42
43 /**
44  * @brief Invalid index - used when no index is known
45  * blazoned capitals INVALID speak volumes where ~0 does not.
46  */
47 #define INDEX_INVALID ((index_t)(~0))
48
49 /**
50  * @brief Data path protocol.
51  * Actions performed on packets in the data-plane can be described and represented
52  * by protocol independent objects, i.e. ADJACENCY, but the spceifics actions
53  * required during ADJACENCY processing can be protocol dependent. For example,
54  * the adjacency rewrite node performs a ip4 checksum calculation,  ip6 and MPLS
55  * do not, all 3 perform a TTL decrement. The VLIB graph nodes are thus protocol
56  * dependent, and thus each graph edge/arc is too.
57  * When programming a DPO's next node arc from child to parent it is thus required
58  * to know the parent's data-path protocol so the correct arc index can be used.
59  */
60 typedef enum dpo_proto_t_
61 {
62 #if CLIB_DEBUG > 0
63     DPO_PROTO_IP4 = 1,
64 #else
65     DPO_PROTO_IP4 = 0,
66 #endif
67     DPO_PROTO_IP6,
68     DPO_PROTO_ETHERNET,
69     DPO_PROTO_MPLS,
70 } __attribute__((packed)) dpo_proto_t;
71
72 #define DPO_PROTO_NUM (DPO_PROTO_MPLS+1)
73 #define DPO_PROTO_NONE (DPO_PROTO_NUM+1)
74
75 #define DPO_PROTOS {            \
76     [DPO_PROTO_IP4]  = "ip4",   \
77     [DPO_PROTO_IP6]  = "ip6",   \
78     [DPO_PROTO_ETHERNET]  = "ethernet", \
79     [DPO_PROTO_MPLS] = "mpls",  \
80 }
81
82 #define FOR_EACH_DPO_PROTO(_proto)    \
83     for (_proto = DPO_PROTO_IP4;      \
84          _proto <= DPO_PROTO_MPLS;    \
85          _proto++)
86
87 /**
88  * @brief Common types of data-path objects
89  * New types can be dynamically added using dpo_register_new_type()
90  */
91 typedef enum dpo_type_t_ {
92     /**
93      * A non-zero value first so we can spot unitialisation errors
94      */
95     DPO_FIRST,
96     DPO_DROP,
97     DPO_PUNT,
98     /**
99      * @brief load-balancing over a choice of [un]equal cost paths
100      */
101     DPO_LOAD_BALANCE,
102     DPO_ADJACENCY,
103     DPO_ADJACENCY_INCOMPLETE,
104     DPO_ADJACENCY_MIDCHAIN,
105     DPO_ADJACENCY_GLEAN,
106     DPO_RECEIVE,
107     DPO_LOOKUP,
108     DPO_LISP_CP,
109     DPO_CLASSIFY,
110     DPO_MPLS_LABEL,
111     DPO_LAST,
112 } __attribute__((packed)) dpo_type_t;
113
114 #define DPO_TYPE_NUM DPO_LAST
115
116 #define DPO_TYPES {                     \
117     [DPO_FIRST] = "dpo-invalid",        \
118     [DPO_DROP] = "dpo-drop",    \
119     [DPO_PUNT] = "dpo-punt",    \
120     [DPO_ADJACENCY] = "dpo-adjacency",  \
121     [DPO_ADJACENCY_INCOMPLETE] = "dpo-adjacency-incomplete",    \
122     [DPO_ADJACENCY_MIDCHAIN] = "dpo-adjacency-midcahin",        \
123     [DPO_ADJACENCY_GLEAN] = "dpo-glean",        \
124     [DPO_RECEIVE] = "dpo-receive",      \
125     [DPO_LOOKUP] = "dpo-lookup",        \
126     [DPO_LOAD_BALANCE] = "dpo-load-balance",    \
127     [DPO_LISP_CP] = "dpo-lisp-cp",      \
128     [DPO_CLASSIFY] = "dpo-classify",    \
129     [DPO_MPLS_LABEL] = "dpo-mpls-label",        \
130 }
131
132 /**
133  * @brief The identity of a DPO is a combination of its type and its
134  * instance number/index of objects of that type
135  */
136 typedef struct dpo_id_t_ {
137     /**
138      * the type
139      */
140     dpo_type_t dpoi_type;
141     /**
142      * the data-path protocol of the type.
143      */
144     dpo_proto_t dpoi_proto;
145     /**
146      * The next VLIB node to follow.
147      */
148     u16 dpoi_next_node;
149     /**
150      * the index of objects of that type
151      */
152     index_t dpoi_index;
153 } __attribute__ ((aligned(sizeof(u64)))) dpo_id_t;
154
155 _Static_assert(sizeof(dpo_id_t) <= sizeof(u64),
156                "DPO ID is greater than sizeof u64 "
157                "atomic updates need to be revisited");
158
159 /**
160  * @brief An initialiser for DPos declared on the stack.
161  */
162 #define DPO_NULL {0}
163
164 /**
165  * @brief Return true if the DPO object is valid, i.e. has been initialised.
166  */
167 static inline int
168 dpo_id_is_valid (const dpo_id_t *dpoi)
169 {
170     return (dpoi->dpoi_type != DPO_FIRST &&
171             dpoi->dpoi_index != INDEX_INVALID);
172 }
173
174 /**
175  * @brief
176  *  Take a reference counting lock on the DPO
177  */
178 extern void dpo_lock(dpo_id_t *dpo);
179
180 /**
181  * @brief
182  *  Release a reference counting lock on the DPO
183  */
184 extern void dpo_unlock(dpo_id_t *dpo);
185
186 /**
187  * @brief Set/create a DPO ID
188  * The DPO will be locked.
189  *
190  * @param dpo
191  *  The DPO object to configure
192  *
193  * @param type
194  *  The dpo_type_t of the DPO
195  *
196  * @param proto
197  *  The dpo_proto_t of the DPO
198  *
199  * @param index
200  *  The type specific index of the DPO
201  */
202 extern void dpo_set(dpo_id_t *dpo,
203                     dpo_type_t type,
204                     dpo_proto_t proto,
205                     index_t index);
206
207 /**
208  * @brief reset a DPO ID
209  * The DPO will be unlocked.
210  *
211  * @param dpo
212  *  The DPO object to reset
213  */
214 extern void dpo_reset(dpo_id_t *dpo);
215
216 /**
217  * @brief compare two DPOs for equality
218  */
219 extern int dpo_cmp(const dpo_id_t *dpo1,
220                    const dpo_id_t *dpo2);
221
222 /**
223  * @brief
224  *  atomic copy a data-plane object.
225  * This is safe to use when the dst DPO is currently switching packets
226  */
227 extern void dpo_copy(dpo_id_t *dst,
228                      const dpo_id_t *src);
229
230 /**
231  * @brief Return TRUE is the DPO is any type of adjacency
232  */
233 extern int dpo_is_adj(const dpo_id_t *dpo);
234
235 /**
236  * @biref Format a DPO_id_t oject
237  */
238 extern u8 *format_dpo_id(u8 * s, va_list * args);
239
240 /**
241  * @biref format a DPO type
242  */
243 extern u8 *format_dpo_type(u8 * s, va_list * args);
244
245 /**
246  * @brief format a DPO protocol
247  */
248 extern u8 *format_dpo_proto(u8 * s, va_list * args);
249
250 /**
251  * @brief
252  *  Set and stack a DPO.
253  *  The DPO passed is set to the parent DPO and the necessary
254  *  VLIB graph arcs are created. The child_type and child_proto
255  * are used to get the VLID nodes from which the arcs are added.
256  *
257  * @param child_type
258  *  Child DPO type.
259  *
260  * @param child_proto
261  *  Child DPO proto
262  *
263  * @parem dpo
264  *  This is the DPO to stack and set.
265  *
266  * @paren parent_dpo
267  *  The parent DPO to stack onto.
268  */
269 extern void dpo_stack(dpo_type_t child_type,
270                       dpo_proto_t child_proto,
271                       dpo_id_t *dpo,
272                       const dpo_id_t *parent_dpo);
273
274 /**
275  * @brief 
276  *  Set and stack a DPO.
277  *  The DPO passed is set to the parent DPO and the necessary
278  *  VLIB graph arcs are created, from the child_node passed.
279  *
280  * @param child_node
281  *  The VLIB grpah node index to create an arc from to the parent
282  *
283  * @parem dpo
284  *  This is the DPO to stack and set.
285  *
286  * @paren parent_dpo
287  *  The parent DPO to stack onto.
288  */ 
289 extern void dpo_stack_from_node(u32 child_node,
290                                 dpo_id_t *dpo,
291                                 const dpo_id_t *parent);
292
293 /**
294  * @brief  A lock function registered for a DPO type
295  */
296 typedef void (*dpo_lock_fn_t)(dpo_id_t *dpo);
297
298 /**
299  * @brief An unlock function registered for a DPO type
300  */
301 typedef void (*dpo_unlock_fn_t)(dpo_id_t *dpo);
302
303 /**
304  * @brief An memory usage show command
305  */
306 typedef void (*dpo_mem_show_t)(void);
307
308 /**
309  * @brief A virtual function table regisitered for a DPO type
310  */
311 typedef struct dpo_vft_t_
312 {
313     /**
314      * A reference counting lock function
315      */
316     dpo_lock_fn_t dv_lock;
317     /**
318      * A reference counting unlock function
319      */
320     dpo_lock_fn_t dv_unlock;
321     /**
322      * A format function
323      */
324     format_function_t *dv_format;
325     /**
326      * A show memory usage function
327      */
328     dpo_mem_show_t dv_mem_show;
329 } dpo_vft_t;
330
331
332 /**
333  * @brief For a given DPO type Register:
334  *   - a virtual function table
335  *   - a NULL terminated array of graph nodes from which that object type
336  *     will originate packets, i.e. the nodes in which the object type will be
337  *     the parent DPO in the DP graph. The ndoes are per-data-path protocol
338  *     (see above).
339  *
340  * @param type
341  *  The type being registered. 
342  *
343  * @param vft
344  *  The virtual function table to register for the type.
345  *
346  * @param nodes
347  *  The string description of the per-protocol VLIB graph nodes.
348  */
349 extern void dpo_register(dpo_type_t type,
350                          const dpo_vft_t *vft,
351                          const char * const * const * nodes);
352
353 /**
354  * @brief Create and register a new DPO type.
355  *
356  * This can be used by plugins to create new DPO types that are not listed
357  * in dpo_type_t enum
358  *
359  * @param vft
360  *  The virtual function table to register for the type.
361  *
362  * @param nodes
363  *  The string description of the per-protocol VLIB graph nodes.
364  *
365  * @return The new dpo_type_t
366  */
367 extern dpo_type_t dpo_register_new_type(const dpo_vft_t *vft,
368                                         const char * const * const * nodes);
369
370 #endif