6d26bdd36718a9365616d26861a5826c1c1c471b
[vpp.git] / src / vnet / fib / fib_node.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 #ifndef __FIB_NODE_H__
17 #define __FIB_NODE_H__
18
19 #include <vnet/fib/fib_types.h>
20
21 /**
22  * The types of nodes in a FIB graph
23  */
24 typedef enum fib_node_type_t_ {
25     /**
26      * Marker. New types after this one.
27      */
28     FIB_NODE_TYPE_FIRST = 0,
29     /**
30      * See the respective fib_*.h files for descriptions of these objects.
31      */
32     FIB_NODE_TYPE_WALK,
33     FIB_NODE_TYPE_ENTRY,
34     FIB_NODE_TYPE_MFIB_ENTRY,
35     FIB_NODE_TYPE_PATH_LIST,
36     FIB_NODE_TYPE_PATH,
37     FIB_NODE_TYPE_ADJ,
38     FIB_NODE_TYPE_MPLS_ENTRY,
39     FIB_NODE_TYPE_MPLS_TUNNEL,
40     FIB_NODE_TYPE_LISP_GPE_FWD_ENTRY,
41     FIB_NODE_TYPE_LISP_ADJ,
42     FIB_NODE_TYPE_GRE_TUNNEL,
43     FIB_NODE_TYPE_VXLAN_TUNNEL,
44     FIB_NODE_TYPE_MAP_E,
45     FIB_NODE_TYPE_VXLAN_GPE_TUNNEL,
46     FIB_NODE_TYPE_GENEVE_TUNNEL,
47     FIB_NODE_TYPE_UDP_ENCAP,
48     /**
49      * Marker. New types before this one. leave the test last.
50      */
51     FIB_NODE_TYPE_TEST,
52     FIB_NODE_TYPE_LAST = FIB_NODE_TYPE_TEST,
53 } fib_node_type_t;
54
55 #define FIB_NODE_TYPE_MAX (FIB_NODE_TYPE_LAST + 1)
56
57 #define FIB_NODE_TYPES {                          \
58     [FIB_NODE_TYPE_ENTRY]     = "entry",          \
59     [FIB_NODE_TYPE_MFIB_ENTRY] = "mfib-entry",    \
60     [FIB_NODE_TYPE_WALK]      = "walk",           \
61     [FIB_NODE_TYPE_PATH_LIST] = "path-list",      \
62     [FIB_NODE_TYPE_PATH]      = "path",           \
63     [FIB_NODE_TYPE_MPLS_ENTRY] = "mpls-entry",    \
64     [FIB_NODE_TYPE_MPLS_TUNNEL] = "mpls-tunnel",  \
65     [FIB_NODE_TYPE_ADJ] = "adj",                  \
66     [FIB_NODE_TYPE_LISP_GPE_FWD_ENTRY] = "lisp-gpe-fwd-entry", \
67     [FIB_NODE_TYPE_LISP_ADJ] = "lisp-adj", \
68     [FIB_NODE_TYPE_GRE_TUNNEL] = "gre-tunnel", \
69     [FIB_NODE_TYPE_VXLAN_TUNNEL] = "vxlan-tunnel", \
70     [FIB_NODE_TYPE_MAP_E] = "map-e", \
71     [FIB_NODE_TYPE_VXLAN_GPE_TUNNEL] = "vxlan-gpe-tunnel", \
72     [FIB_NODE_TYPE_UDP_ENCAP] = "udp-encap", \
73 }
74
75 /**
76  * Reasons for backwalking the FIB object graph
77  */
78 typedef enum fib_node_back_walk_reason_t_ {
79     /**
80      * Marker. Add new ones after.
81      */
82     FIB_NODE_BW_REASON_FIRST = 0,
83     /**
84      * Walk to re-resolve the child.
85      * Used when the parent is no longer a valid resolution target
86      */
87     FIB_NODE_BW_REASON_RESOLVE = FIB_NODE_BW_REASON_FIRST,
88     /**
89      * Walk to re-evaluate the forwarding contributed by the parent.
90      * Used when a parent's forwarding changes and the child needs to
91      * incorporate this change in its forwarding.
92      */
93     FIB_NODE_BW_REASON_EVALUATE,
94     /**
95      * A resolving interface has come up
96      */
97     FIB_NODE_BW_REASON_INTERFACE_UP,
98     /**
99      * A resolving interface has gone down
100      */
101     FIB_NODE_BW_REASON_INTERFACE_DOWN,
102     /**
103      * A resolving interface has been deleted.
104      */
105     FIB_NODE_BW_REASON_INTERFACE_DELETE,
106     /**
107      * Walk to re-collapse the multipath adjs when the rewrite of
108      * a unipath adjacency changes
109      */
110     FIB_NODE_BW_REASON_ADJ_UPDATE,
111     /**
112      * Walk to update children to inform them the adjacency is now down.
113      */
114     FIB_NODE_BW_REASON_ADJ_DOWN,
115     /**
116      * Marker. Add new before and update
117      */
118     FIB_NODE_BW_REASON_LAST = FIB_NODE_BW_REASON_ADJ_DOWN,
119 } fib_node_back_walk_reason_t;
120
121 #define FIB_NODE_BW_REASONS {                               \
122     [FIB_NODE_BW_REASON_RESOLVE] = "resolve",               \
123     [FIB_NODE_BW_REASON_EVALUATE] = "evaluate",             \
124     [FIB_NODE_BW_REASON_INTERFACE_UP] = "if-up",            \
125     [FIB_NODE_BW_REASON_INTERFACE_DOWN] = "if-down",        \
126     [FIB_NODE_BW_REASON_INTERFACE_DELETE] = "if-delete",    \
127     [FIB_NODE_BW_REASON_ADJ_UPDATE] = "adj-update",         \
128     [FIB_NODE_BW_REASON_ADJ_DOWN] = "adj-down",             \
129 }
130
131 #define FOR_EACH_FIB_NODE_BW_REASON(_item) \
132     for (_item = FIB_NODE_BW_REASON_FIRST; \
133          _item <= FIB_NODE_BW_REASON_LAST; \
134          _item++)
135
136 /**
137  * Flags enum constructed from the reaons
138  */
139 typedef enum fib_node_bw_reason_flag_t_ {
140     FIB_NODE_BW_REASON_FLAG_NONE = 0,
141     FIB_NODE_BW_REASON_FLAG_RESOLVE = (1 << FIB_NODE_BW_REASON_RESOLVE),
142     FIB_NODE_BW_REASON_FLAG_EVALUATE = (1 << FIB_NODE_BW_REASON_EVALUATE),
143     FIB_NODE_BW_REASON_FLAG_INTERFACE_UP = (1 << FIB_NODE_BW_REASON_INTERFACE_UP),
144     FIB_NODE_BW_REASON_FLAG_INTERFACE_DOWN = (1 << FIB_NODE_BW_REASON_INTERFACE_DOWN),
145     FIB_NODE_BW_REASON_FLAG_INTERFACE_DELETE = (1 << FIB_NODE_BW_REASON_INTERFACE_DELETE),
146     FIB_NODE_BW_REASON_FLAG_ADJ_UPDATE = (1 << FIB_NODE_BW_REASON_ADJ_UPDATE),
147     FIB_NODE_BW_REASON_FLAG_ADJ_DOWN = (1 << FIB_NODE_BW_REASON_ADJ_DOWN),
148 } __attribute__ ((packed)) fib_node_bw_reason_flag_t;
149
150 STATIC_ASSERT(sizeof(fib_node_bw_reason_flag_t) < 2,
151               "BW Reason enum < 2 byte. Consequences for cover_upd_res_t");
152
153 /**
154  * Flags on the walk
155  */
156 typedef enum fib_node_bw_flags_t_
157 {
158     /**
159      * Force the walk to be synchronous
160      */
161     FIB_NODE_BW_FLAG_FORCE_SYNC = (1 << 0),
162 } fib_node_bw_flags_t;
163
164 /**
165  * Forward eclarations
166  */
167 struct fib_node_t_;
168
169 /**
170  * A representation of one pointer to another node.
171  * To fully qualify a node, one must know its type and its index so it
172  * can be retrieved from the appropriate pool. Direct pointers to nodes
173  * are forbidden, since all nodes are allocated from pools, which are vectors,
174  * and thus subject to realloc at any time.
175  */
176 typedef struct fib_node_ptr_t_ {
177     /**
178      * node type
179      */
180     fib_node_type_t fnp_type;
181     /**
182      * node's index
183      */
184     fib_node_index_t fnp_index;
185 } fib_node_ptr_t;
186
187 /**
188  * @brief A list of FIB nodes.
189  */
190 typedef u32 fib_node_list_t;
191
192 /**
193  * Context passed between object during a back walk.
194  */
195 typedef struct fib_node_back_walk_ctx_t_ {
196     /**
197      * The reason/trigger for the backwalk
198      */
199     fib_node_bw_reason_flag_t fnbw_reason;
200
201     /**
202      * additional flags for the walk
203      */
204     fib_node_bw_flags_t fnbw_flags;
205
206     /**
207      * the number of levels the walk has already traversed.
208      * this value is maintained by the walk infra, tp limit the depth of
209      * a walk so it does not run indefinately the presence of a loop/cycle
210      * in the graph.
211      */
212     u32 fnbw_depth;
213 } fib_node_back_walk_ctx_t;
214
215 /**
216  * We consider a depth of 32 to be sufficient to cover all sane
217  * network topologies. Anything more is then an indication that
218  * there is a loop/cycle in the FIB graph.
219  * Note that all object types contribute to 1 to the depth.
220  */
221 #define FIB_NODE_GRAPH_MAX_DEPTH ((u32)32)
222
223 /**
224  * A callback function for walking a node dependency list
225  */
226 typedef int (*fib_node_ptr_walk_t)(fib_node_ptr_t *depend,
227                                    void *ctx);
228
229 /**
230  * A list of dependent nodes.
231  * This is currently implemented as a hash_table of fib_node_ptr_t
232  */
233 typedef fib_node_ptr_t fib_node_ptr_list_t;
234
235 /**
236  * Return code from a back walk function
237  */
238 typedef enum fib_node_back_walk_rc_t_ {
239     FIB_NODE_BACK_WALK_MERGE,
240     FIB_NODE_BACK_WALK_CONTINUE,
241 } fib_node_back_walk_rc_t;
242
243 /**
244  * Function definition to backwalk a FIB node
245  */
246 typedef fib_node_back_walk_rc_t (*fib_node_back_walk_t)(
247     struct fib_node_t_ *node,
248     fib_node_back_walk_ctx_t *ctx);
249
250 /**
251  * Function definition to get a FIB node from its index
252  */
253 typedef struct fib_node_t_* (*fib_node_get_t)(fib_node_index_t index);
254
255 /**
256  * Function definition to inform the FIB node that its last lock has gone.
257  */
258 typedef void (*fib_node_last_lock_gone_t)(struct fib_node_t_ *node);
259
260 /**
261  * Function definition to display the amount of memory used by a type.
262  * Implementations should call fib_show_memory_usage()
263  */
264 typedef void (*fib_node_memory_show_t)(void);
265
266 /**
267  * A FIB graph nodes virtual function table
268  */
269 typedef struct fib_node_vft_t_ {
270     fib_node_get_t fnv_get;
271     fib_node_last_lock_gone_t fnv_last_lock;
272     fib_node_back_walk_t fnv_back_walk;
273     format_function_t *fnv_format;
274     fib_node_memory_show_t fnv_mem_show;
275 } fib_node_vft_t;
276
277 /**
278  * An node in the FIB graph
279  *
280  * Objects in the FIB form a graph.
281  */
282 typedef struct fib_node_t_ {
283 #if CLIB_DEBUG > 0
284     /**
285      * The node's type. make sure we are dynamic/down casting correctly
286      */
287     fib_node_type_t fn_type;
288 #endif
289     /**
290      * The node's VFT.
291      * we could store the type here instead, and lookup the VFT using that. But
292      * I like this better,
293      */
294     const fib_node_vft_t *fn_vft;
295
296     /**
297      * Vector of nodes that depend upon/use/share this node
298      */
299     fib_node_list_t fn_children;
300
301     /**
302      * Number of dependents on this node. This number includes the number
303      * of children
304      */
305     u32 fn_locks;
306 } fib_node_t;
307
308 /**
309  * @brief
310  *  Register the function table for a given type
311  *
312  * @param ft
313  *  FIB node type
314  *
315  * @param vft
316  * virtual function table
317  */
318 extern void fib_node_register_type (fib_node_type_t ft,
319                                     const fib_node_vft_t *vft);
320
321 /**
322  * @brief
323  *  Create a new FIB node type and Register the function table for it.
324  *
325  * @param vft
326  * virtual function table
327  *
328  * @return new FIB node type
329  */
330 extern fib_node_type_t fib_node_register_new_type (const fib_node_vft_t *vft);
331
332 /**
333  * @brief Show the memory usage for a type
334  *
335  * This should be invoked by the type in response to the infra calling
336  * its registered memory show function
337  *
338  * @param name the name of the type
339  * @param in_use_elts The number of elements in use
340  * @param allocd_elts The number of allocated pool elemenets
341  * @param size_elt The size of one element
342  */
343 extern void fib_show_memory_usage(const char *name,
344                                   u32 in_use_elts,
345                                   u32 allocd_elts,
346                                   size_t size_elt);
347
348 extern void fib_node_init(fib_node_t *node,
349                           fib_node_type_t ft);
350 extern void fib_node_deinit(fib_node_t *node);
351
352 extern void fib_node_lock(fib_node_t *node);
353 extern void fib_node_unlock(fib_node_t *node);
354
355 extern u32 fib_node_get_n_children(fib_node_type_t parent_type,
356                                    fib_node_index_t parent_index);
357 extern u32 fib_node_child_add(fib_node_type_t parent_type,
358                               fib_node_index_t parent_index,
359                               fib_node_type_t child_type,
360                               fib_node_index_t child_index);
361 extern void fib_node_child_remove(fib_node_type_t parent_type,
362                                   fib_node_index_t parent_index,
363                                   fib_node_index_t sibling_index);
364
365 extern fib_node_back_walk_rc_t fib_node_back_walk_one(fib_node_ptr_t *ptr,
366                                                       fib_node_back_walk_ctx_t *ctx);
367
368 extern u8* fib_node_children_format(fib_node_list_t list,
369                                     u8 *s);
370
371 extern const char* fib_node_type_get_name(fib_node_type_t type);
372
373 static inline int
374 fib_node_index_is_valid (fib_node_index_t ni)
375 {
376     return (FIB_NODE_INDEX_INVALID != ni);
377 }
378
379 #endif
380