FIB: optimise for src memory allocations
[vpp.git] / src / vnet / fib / fib_entry.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_ENTRY_H__
17 #define __FIB_ENTRY_H__
18
19 #include <vnet/fib/fib_node.h>
20 #include <vnet/fib/fib_entry_delegate.h>
21 #include <vnet/adj/adj.h>
22 #include <vnet/ip/ip.h>
23 #include <vnet/dpo/dpo.h>
24
25 /**
26  * The different sources that can create a route.
27  * The sources are defined here the thier relative priority order.
28  * The lower the value the higher the priority
29  */
30 typedef enum fib_source_t_ {
31     /**
32      * An invalid source of value 0
33      */
34     FIB_SOURCE_INVALID,
35     /**
36      * Marker. Add new values after this one.
37      */
38     FIB_SOURCE_FIRST,
39     /**
40      * Special sources. These are for entries that are added to all
41      * FIBs by default, and should never be over-ridden (hence they
42      * are the highest priority)
43      */
44     FIB_SOURCE_SPECIAL = FIB_SOURCE_FIRST,
45     /**
46      * Classify. A route that links directly to a classify adj
47      */
48     FIB_SOURCE_CLASSIFY,
49     /**
50      * A route the is being 'proxied' on behalf of another device
51      */
52     FIB_SOURCE_PROXY,
53     /**
54      * Route added as a result of interface configuration.
55      * this will also come from the API/CLI, but the distinction is
56      * that is from confiiguration on an interface, not a 'ip route' command
57      */
58     FIB_SOURCE_INTERFACE,
59     /**
60      * SRv6 and SR-MPLS
61      */
62     FIB_SOURCE_SR,
63     /**
64      * A high priority source a plugin can use
65      */
66     FIB_SOURCE_PLUGIN_HI,
67     /**
68      * From the BIER subsystem
69      */
70     FIB_SOURCE_BIER,
71     /**
72      * From the control plane API
73      */
74     FIB_SOURCE_API,
75     /**
76      * From the CLI.
77      */
78     FIB_SOURCE_CLI,
79     /**
80      * LISP
81      */
82     FIB_SOURCE_LISP,
83     /**
84      * IPv[46] Mapping
85      */
86     FIB_SOURCE_MAP,
87     /**
88      * SIXRD
89      */
90     FIB_SOURCE_SIXRD,
91     /**
92      * DHCP
93      */
94     FIB_SOURCE_DHCP,
95     /**
96      * IPv6 Proxy ND
97      */
98     FIB_SOURCE_IP6_ND_PROXY,
99     /**
100      * Adjacency source.
101      * routes created as a result of ARP/ND entries. This is lower priority
102      * then the API/CLI. This is on purpose. trust me.
103      */
104     FIB_SOURCE_ADJ,
105     /**
106      * MPLS label. The prefix has been assigned a local label. This source
107      * never provides forwarding information, instead it acts as a place-holder
108      * so the association of label to prefix can be maintained
109      */
110     FIB_SOURCE_MPLS,
111     /**
112      * Attached Export source.
113      * routes created as a result of attahced export. routes thus sourced
114      * will be present in the export tables
115      */
116     FIB_SOURCE_AE,
117     /**
118      * Recursive resolution source.
119      * Used to install an entry that is the resolution traget of another.
120      */
121     FIB_SOURCE_RR,
122     /**
123      * uRPF bypass/exemption.
124      * Used to install an entry that is exempt from the loose uRPF check
125      */
126     FIB_SOURCE_URPF_EXEMPT,
127     /**
128      * The default route source.
129      * The default route is always added to the FIB table (like the
130      * special sources) but we need to be able to over-ride it with
131      * 'ip route' sources when provided
132      */
133     FIB_SOURCE_DEFAULT_ROUTE,
134     /**
135      * Marker. add new entries before this one.
136      */
137     FIB_SOURCE_LAST = FIB_SOURCE_DEFAULT_ROUTE,
138 } __attribute__ ((packed)) fib_source_t;
139
140 STATIC_ASSERT (sizeof(fib_source_t) == 1,
141                "FIB too many sources");
142
143 /**
144  * The maximum number of sources
145  */
146 #define FIB_SOURCE_MAX (FIB_SOURCE_LAST+1)
147
148 #define FIB_SOURCES {                                   \
149     [FIB_SOURCE_INVALID] = "invalid",                   \
150     [FIB_SOURCE_SPECIAL] = "special",                   \
151     [FIB_SOURCE_INTERFACE] = "interface",               \
152     [FIB_SOURCE_PROXY] = "proxy",                       \
153     [FIB_SOURCE_BIER] = "BIER",                         \
154     [FIB_SOURCE_API] = "API",                           \
155     [FIB_SOURCE_CLI] = "CLI",                           \
156     [FIB_SOURCE_ADJ] = "adjacency",                     \
157     [FIB_SOURCE_MAP] = "MAP",                           \
158     [FIB_SOURCE_SR] = "SR",                             \
159     [FIB_SOURCE_SIXRD] = "SixRD",                       \
160     [FIB_SOURCE_LISP] = "LISP",                         \
161     [FIB_SOURCE_CLASSIFY] = "classify",                 \
162     [FIB_SOURCE_DHCP] = "DHCP",                         \
163     [FIB_SOURCE_IP6_ND_PROXY] = "IPv6-proxy-nd",        \
164     [FIB_SOURCE_RR] = "recursive-resolution",           \
165     [FIB_SOURCE_AE] = "attached_export",                \
166     [FIB_SOURCE_MPLS] = "mpls",                         \
167     [FIB_SOURCE_URPF_EXEMPT] = "urpf-exempt",           \
168     [FIB_SOURCE_DEFAULT_ROUTE] = "default-route",       \
169 }
170
171 #define FOR_EACH_FIB_SOURCE(_item) \
172     for (_item = FIB_SOURCE_FIRST; _item < FIB_SOURCE_MAX; _item++)
173
174 /**
175  * The different sources that can create a route.
176  * The sources are defined here the thier relative priority order.
177  * The lower the value the higher the priority
178  */
179 typedef enum fib_entry_attribute_t_ {
180     /**
181      * Marker. Add new values after this one.
182      */
183     FIB_ENTRY_ATTRIBUTE_FIRST,
184     /**
185      * Connected. The prefix is configured on an interface.
186      */
187     FIB_ENTRY_ATTRIBUTE_CONNECTED = FIB_ENTRY_ATTRIBUTE_FIRST,
188     /**
189      * Attached. The prefix is attached to an interface.
190      */
191     FIB_ENTRY_ATTRIBUTE_ATTACHED,
192     /**
193      * The route is an explicit drop.
194      */
195     FIB_ENTRY_ATTRIBUTE_DROP,
196     /**
197      * The route is exclusive. The client creating the route is
198      * providing an exclusive adjacency.
199      */
200     FIB_ENTRY_ATTRIBUTE_EXCLUSIVE,
201     /**
202      * The route is attached cross tables and thus imports covered
203      * prefixes from the other table.
204      */
205     FIB_ENTRY_ATTRIBUTE_IMPORT,
206     /**
207      * The prefix/address is local to this device
208      */
209     FIB_ENTRY_ATTRIBUTE_LOCAL,
210     /**
211      * The prefix/address is a multicast prefix.
212      *  this aplies only to MPLS. IP multicast is handled by mfib
213      */
214     FIB_ENTRY_ATTRIBUTE_MULTICAST,
215     /**
216      * The prefix/address exempted from loose uRPF check
217      * To be used with caution
218      */
219     FIB_ENTRY_ATTRIBUTE_URPF_EXEMPT,
220     /**
221      * Marker. add new entries before this one.
222      */
223     FIB_ENTRY_ATTRIBUTE_LAST = FIB_ENTRY_ATTRIBUTE_URPF_EXEMPT,
224 } fib_entry_attribute_t;
225
226 #define FIB_ENTRY_ATTRIBUTES {                          \
227     [FIB_ENTRY_ATTRIBUTE_CONNECTED] = "connected",      \
228     [FIB_ENTRY_ATTRIBUTE_ATTACHED]  = "attached",       \
229     [FIB_ENTRY_ATTRIBUTE_IMPORT]    = "import",         \
230     [FIB_ENTRY_ATTRIBUTE_DROP]      = "drop",           \
231     [FIB_ENTRY_ATTRIBUTE_EXCLUSIVE] = "exclusive",      \
232     [FIB_ENTRY_ATTRIBUTE_LOCAL]     = "local",          \
233     [FIB_ENTRY_ATTRIBUTE_URPF_EXEMPT] = "uRPF-exempt",  \
234     [FIB_ENTRY_ATTRIBUTE_MULTICAST] = "multicast",      \
235 }
236
237 #define FOR_EACH_FIB_ATTRIBUTE(_item)                   \
238     for (_item = FIB_ENTRY_ATTRIBUTE_FIRST;             \
239          _item <= FIB_ENTRY_ATTRIBUTE_LAST;             \
240          _item++)
241
242 typedef enum fib_entry_flag_t_ {
243     FIB_ENTRY_FLAG_NONE      = 0,
244     FIB_ENTRY_FLAG_CONNECTED = (1 << FIB_ENTRY_ATTRIBUTE_CONNECTED),
245     FIB_ENTRY_FLAG_ATTACHED  = (1 << FIB_ENTRY_ATTRIBUTE_ATTACHED),
246     FIB_ENTRY_FLAG_DROP      = (1 << FIB_ENTRY_ATTRIBUTE_DROP),
247     FIB_ENTRY_FLAG_EXCLUSIVE = (1 << FIB_ENTRY_ATTRIBUTE_EXCLUSIVE),
248     FIB_ENTRY_FLAG_LOCAL     = (1 << FIB_ENTRY_ATTRIBUTE_LOCAL),
249     FIB_ENTRY_FLAG_IMPORT    = (1 << FIB_ENTRY_ATTRIBUTE_IMPORT),
250     FIB_ENTRY_FLAG_LOOSE_URPF_EXEMPT = (1 << FIB_ENTRY_ATTRIBUTE_URPF_EXEMPT),
251     FIB_ENTRY_FLAG_MULTICAST = (1 << FIB_ENTRY_ATTRIBUTE_MULTICAST),
252 } __attribute__((packed)) fib_entry_flag_t;
253
254 /**
255  * Flags for the source data
256  */
257 typedef enum fib_entry_src_attribute_t_ {
258     /**
259      * Marker. Add new values after this one.
260      */
261     FIB_ENTRY_SRC_ATTRIBUTE_FIRST,
262     /**
263      * the source has been added to the entry
264      */
265     FIB_ENTRY_SRC_ATTRIBUTE_ADDED = FIB_ENTRY_SRC_ATTRIBUTE_FIRST,
266     /**
267      * the source is active/best
268      */
269     FIB_ENTRY_SRC_ATTRIBUTE_ACTIVE,
270     /**
271      * Marker. add new entries before this one.
272      */
273     FIB_ENTRY_SRC_ATTRIBUTE_LAST = FIB_ENTRY_SRC_ATTRIBUTE_ACTIVE,
274 } fib_entry_src_attribute_t;
275
276 #define FIB_ENTRY_SRC_ATTRIBUTE_MAX (FIB_ENTRY_SRC_ATTRIBUTE_LAST+1)
277
278 #define FIB_ENTRY_SRC_ATTRIBUTES {               \
279     [FIB_ENTRY_SRC_ATTRIBUTE_ADDED]  = "added",  \
280     [FIB_ENTRY_SRC_ATTRIBUTE_ACTIVE] = "active", \
281 }
282
283 typedef enum fib_entry_src_flag_t_ {
284     FIB_ENTRY_SRC_FLAG_NONE   = 0,
285     FIB_ENTRY_SRC_FLAG_ADDED  = (1 << FIB_ENTRY_SRC_ATTRIBUTE_ADDED),
286     FIB_ENTRY_SRC_FLAG_ACTIVE = (1 << FIB_ENTRY_SRC_ATTRIBUTE_ACTIVE),
287 } __attribute__ ((packed)) fib_entry_src_flag_t;
288
289 /*
290  * Keep the size of the flags field to 2 bytes, so it
291  * can be placed next to the 2 bytes reference count
292  */
293 STATIC_ASSERT (sizeof(fib_entry_src_flag_t) <= 2,
294                "FIB entry flags field size too big");
295
296 /**
297  * Information related to the source of a FIB entry
298  */
299 typedef struct fib_entry_src_t_ {
300     /**
301      * A vector of path extensions
302      */
303     fib_path_ext_list_t fes_path_exts;
304
305     /**
306      * The path-list created by the source
307      */
308     fib_node_index_t fes_pl;
309     /**
310      * Which source this info block is for
311      */
312     fib_source_t fes_src;
313     /**
314      * Flags on the source
315      */
316     fib_entry_src_flag_t fes_flags;
317
318     /**
319      * 1 bytes ref count. This is not the number of users of the Entry
320      * (which is itself not large, due to path-list sharing), but the number
321      * of times a given source has been added. Which is even fewer
322      */
323     u8 fes_ref_count;
324
325     /**
326      * Flags the source contributes to the entry
327      */
328     fib_entry_flag_t fes_entry_flags;
329     
330     /**
331      * Source specific info
332      */
333     union {
334         struct {
335             /**
336              * the index of the FIB entry that is the covering entry
337              */
338             fib_node_index_t fesr_cover;
339             /**
340              * This source's index in the cover's list
341              */
342             u32 fesr_sibling;
343         } rr;
344         struct {
345             /**
346              * the index of the FIB entry that is the covering entry
347              */
348             fib_node_index_t fesa_cover;
349             /**
350              * This source's index in the cover's list
351              */
352             u32 fesa_sibling;
353         } adj;
354         struct {
355             /**
356              * the index of the FIB entry that is the covering entry
357              */
358             fib_node_index_t fesi_cover;
359             /**
360              * This source's index in the cover's list
361              */
362             u32 fesi_sibling;
363         } interface;
364         struct {
365             /**
366              * This MPLS local label associated with the prefix.
367              */
368             mpls_label_t fesm_label;
369
370             /**
371              * the indicies of the LFIB entries created
372              */
373             fib_node_index_t fesm_lfes[2];
374         } mpls;
375         struct {
376             /**
377              * The source FIB index.
378              */
379             fib_node_index_t fesl_fib_index;
380         } lisp;
381     };
382 } fib_entry_src_t;
383
384 /**
385  * FIB entry flags.
386  *  these are stored in the pad space within the fib_node_t
387  */
388 typedef enum fib_entry_node_attribute_t_
389 {
390     /**
391      * FIB entry has multiple sources, so the fe_srcs union
392      * uses the vector
393      */
394     FIB_ENTRY_NODE_ATTR_MULTIPLE_SRCS,
395 } fib_entry_node_attribute_t;
396
397 #define FIB_ENTRY_NODE_FLAG_NAMES {                            \
398     [FIB_ENTRY_NODE_ATTR_MULTIPLE_SRCS] = "multiple-srcs",     \
399 }
400
401 typedef enum fib_entry_node_flags_t_
402 {
403     FIB_ENTRY_NODE_FLAG_MULTIPLE_SRCS = (1 << FIB_ENTRY_NODE_ATTR_MULTIPLE_SRCS),
404 } fib_entry_node_flags_t;
405
406
407 /**
408  * An entry in a FIB table.
409  *
410  * This entry represents a route added to the FIB that is stored
411  * in one of the FIB tables.
412  */
413 typedef struct fib_entry_t_ {
414     /**
415      * Base class. The entry's node representation in the graph.
416      */
417     fib_node_t fe_node;
418     /**
419      * The prefix of the route. this is const just to be sure.
420      * It is the entry's key/identity and so should never change.
421      */
422     const fib_prefix_t fe_prefix;
423     /**
424      * The index of the FIB table this entry is in
425      */
426     u32 fe_fib_index;
427     /**
428      * The load-balance used for forwarding.
429      *
430      * We don't share the EOS and non-EOS even in case when they could be
431      * because:
432      *   - complexity & reliability v. memory
433      *       determining the conditions where sharing is possible is non-trivial.
434      *   - separate LBs means we can get the EOS bit right in the MPLS label DPO
435      *     and so save a few clock cycles in the DP imposition node since we can
436      *     paint the header straight on without the need to check the packet
437      *     type to derive the EOS bit value.
438      */
439     dpo_id_t fe_lb;
440
441     /**
442      * Source info.
443      * in the majority of cases a FIB entry will have only one source.
444      * so to save the extra memory allocation of the source's vector, we
445      * store space for one source inline. When two sources are present, 
446      * we burn extra memory.
447      * The union is switched based on the FIB_ENTRY_NODE_FLAG_MULTIPLE_SRCS
448      */
449     union {
450         fib_entry_src_t *fe_srcs;
451         fib_entry_src_t fe_src;
452     } fe_u_src;
453
454     /**
455      * the path-list for which this entry is a child. This is also the path-list
456      * that is contributing forwarding for this entry.
457      */
458     fib_node_index_t fe_parent;
459     /**
460      * index of this entry in the parent's child list.
461      * This is set when this entry is added as a child, but can also
462      * be changed by the parent as it manages its list.
463      */
464     u32 fe_sibling;
465
466     /**
467      * A vector of delegates.
468      */
469     fib_entry_delegate_t *fe_delegates;
470 } fib_entry_t;
471
472 #define FOR_EACH_FIB_ENTRY_FLAG(_item) \
473     for (_item = FIB_ENTRY_FLAG_FIRST; _item < FIB_ENTRY_FLAG_MAX; _item++)
474
475 #define FIB_ENTRY_FORMAT_BRIEF   (0x0)
476 #define FIB_ENTRY_FORMAT_DETAIL  (0x1)
477 #define FIB_ENTRY_FORMAT_DETAIL2 (0x2)
478
479 extern u8 *format_fib_entry (u8 * s, va_list * args);
480 extern u8 *format_fib_source (u8 * s, va_list * args);
481
482 extern fib_node_index_t fib_entry_create_special(u32 fib_index,
483                                                  const fib_prefix_t *prefix,
484                                                  fib_source_t source,
485                                                  fib_entry_flag_t flags,
486                                                  const dpo_id_t *dpo);
487
488 extern fib_node_index_t fib_entry_create (u32 fib_index,
489                                           const fib_prefix_t *prefix,
490                                           fib_source_t source,
491                                           fib_entry_flag_t flags,
492                                           const fib_route_path_t *paths);
493 extern void fib_entry_update (fib_node_index_t fib_entry_index,
494                               fib_source_t source,
495                               fib_entry_flag_t flags,
496                               const fib_route_path_t *paths);
497
498 extern void fib_entry_path_add(fib_node_index_t fib_entry_index,
499                                fib_source_t source,
500                                fib_entry_flag_t flags,
501                                const fib_route_path_t *rpath);
502 extern void fib_entry_special_add(fib_node_index_t fib_entry_index,
503                                   fib_source_t source,
504                                   fib_entry_flag_t flags,
505                                   const dpo_id_t *dpo);
506 extern void fib_entry_special_update(fib_node_index_t fib_entry_index,
507                                      fib_source_t source,
508                                      fib_entry_flag_t flags,
509                                      const dpo_id_t *dpo);
510 extern fib_entry_src_flag_t fib_entry_special_remove(fib_node_index_t fib_entry_index,
511                                                      fib_source_t source);
512
513 extern fib_entry_src_flag_t fib_entry_path_remove(fib_node_index_t fib_entry_index,
514                                                   fib_source_t source,
515                                                   const fib_route_path_t *rpath);
516 extern fib_entry_src_flag_t fib_entry_delete(fib_node_index_t fib_entry_index,
517                                              fib_source_t source);
518
519 extern void fib_entry_contribute_urpf(fib_node_index_t path_index,
520                                       index_t urpf);
521 extern void fib_entry_contribute_forwarding(
522     fib_node_index_t fib_entry_index,
523     fib_forward_chain_type_t type,
524     dpo_id_t *dpo);
525 extern const dpo_id_t * fib_entry_contribute_ip_forwarding(
526     fib_node_index_t fib_entry_index);
527 extern adj_index_t fib_entry_get_adj_for_source(
528     fib_node_index_t fib_entry_index,
529     fib_source_t source);
530 extern const int fib_entry_get_dpo_for_source (
531     fib_node_index_t fib_entry_index,
532     fib_source_t source,
533     dpo_id_t *dpo);
534
535 extern adj_index_t fib_entry_get_adj(fib_node_index_t fib_entry_index);
536
537 extern int fib_entry_cmp_for_sort(void *i1, void *i2);
538
539 extern void fib_entry_cover_changed(fib_node_index_t fib_entry);
540 extern void fib_entry_cover_updated(fib_node_index_t fib_entry);
541 extern int fib_entry_recursive_loop_detect(fib_node_index_t entry_index,
542                                            fib_node_index_t **entry_indicies);
543
544 extern void fib_entry_lock(fib_node_index_t fib_entry_index);
545 extern void fib_entry_unlock(fib_node_index_t fib_entry_index);
546
547 extern u32 fib_entry_child_add(fib_node_index_t fib_entry_index,
548                                fib_node_type_t type,
549                                fib_node_index_t child_index);
550 extern void fib_entry_child_remove(fib_node_index_t fib_entry_index,
551                                    u32 sibling_index);
552 extern u32 fib_entry_get_resolving_interface(fib_node_index_t fib_entry_index);
553 extern u32 fib_entry_get_resolving_interface_for_source(
554     fib_node_index_t fib_entry_index,
555     fib_source_t source);
556
557 extern void fib_entry_encode(fib_node_index_t fib_entry_index,
558                              fib_route_path_encode_t **api_rpaths);
559 extern void fib_entry_get_prefix(fib_node_index_t fib_entry_index,
560                                  fib_prefix_t *pfx);
561 extern u32 fib_entry_get_fib_index(fib_node_index_t fib_entry_index);
562 extern void fib_entry_set_source_data(fib_node_index_t fib_entry_index,
563                                       fib_source_t source,
564                                       const void *data);
565 extern const void* fib_entry_get_source_data(fib_node_index_t fib_entry_index,
566                                              fib_source_t source);
567
568 extern fib_entry_flag_t fib_entry_get_flags(fib_node_index_t fib_entry_index);
569 extern fib_entry_flag_t fib_entry_get_flags_for_source(
570     fib_node_index_t fib_entry_index,
571     fib_source_t source);
572 extern fib_source_t fib_entry_get_best_source(fib_node_index_t fib_entry_index);
573 extern int fib_entry_is_sourced(fib_node_index_t fib_entry_index,
574                                 fib_source_t source);
575
576 extern fib_node_index_t fib_entry_get_path_list(fib_node_index_t fib_entry_index);
577 extern int fib_entry_is_resolved(fib_node_index_t fib_entry_index);
578 extern void fib_entry_set_flow_hash_config(fib_node_index_t fib_entry_index,
579                                            flow_hash_config_t hash_config);
580
581 extern void fib_entry_module_init(void);
582
583 /*
584  * unsafe... beware the raw pointer.
585  */
586 extern fib_node_index_t fib_entry_get_index(const fib_entry_t * fib_entry);
587 extern fib_entry_t * fib_entry_get(fib_node_index_t fib_entry_index);
588
589 /*
590  * for testing purposes.
591  */
592 extern u32 fib_entry_pool_size(void);
593
594 #endif