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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #ifndef __FIB_TABLE_H__
17 #define __FIB_TABLE_H__
19 #include <vnet/ip/ip.h>
20 #include <vnet/adj/adj.h>
21 #include <vnet/fib/fib_entry.h>
22 #include <vnet/mpls/mpls.h>
23 #include <vnet/mpls/packet.h>
26 * Flags for the source data
28 typedef enum fib_table_attribute_t_ {
30 * Marker. Add new values after this one.
32 FIB_TABLE_ATTRIBUTE_FIRST,
34 * the table is for IP6 link local addresses
36 FIB_TABLE_ATTRIBUTE_IP6_LL = FIB_TABLE_ATTRIBUTE_FIRST,
38 * the table is currently resync-ing
40 FIB_TABLE_ATTRIBUTE_RESYNC,
42 * Marker. add new entries before this one.
44 FIB_TABLE_ATTRIBUTE_LAST = FIB_TABLE_ATTRIBUTE_RESYNC,
45 } fib_table_attribute_t;
47 #define FIB_TABLE_ATTRIBUTE_MAX (FIB_TABLE_ATTRIBUTE_LAST+1)
49 #define FIB_TABLE_ATTRIBUTES { \
50 [FIB_TABLE_ATTRIBUTE_IP6_LL] = "ip6-ll", \
51 [FIB_TABLE_ATTRIBUTE_RESYNC] = "resync", \
54 #define FOR_EACH_FIB_TABLE_ATTRIBUTE(_item) \
55 for (_item = FIB_TABLE_ATTRIBUTE_FIRST; \
56 _item < FIB_TABLE_ATTRIBUTE_MAX; \
59 typedef enum fib_table_flags_t_ {
60 FIB_TABLE_FLAG_NONE = 0,
61 FIB_TABLE_FLAG_IP6_LL = (1 << FIB_TABLE_ATTRIBUTE_IP6_LL),
62 FIB_TABLE_FLAG_RESYNC = (1 << FIB_TABLE_ATTRIBUTE_RESYNC),
63 } __attribute__ ((packed)) fib_table_flags_t;
65 extern u8* format_fib_table_flags(u8 *s, va_list *args);
69 * A protocol Independent FIB table
71 typedef struct fib_table_t_
74 * Which protocol this table serves. Used to switch on the union above.
76 fib_protocol_t ft_proto;
81 fib_table_flags_t ft_flags;
84 * per-source number of locks on the table
90 * Table ID (hash key) for this FIB.
95 * Index into FIB vector.
97 fib_node_index_t ft_index;
100 * flow hash configuration
102 u32 ft_flow_hash_config;
105 * Per-source route counters
107 u32 *ft_src_route_counts;
110 * Total route counters
112 u32 ft_total_route_counts;
115 * Epoch - number of resyncs performed
128 * Default names for IP4, IP6, and MPLS FIB table index 0.
129 * Nominally like "ipv4-VRF:0", but this will override that name if set
130 * in a config section of the startup.conf file.
132 extern char *fib_table_default_names[FIB_PROTOCOL_MAX];
136 * Format the description/name of the table
138 extern u8* format_fib_table_name(u8* s, va_list *ap);
142 * Perfom a longest prefix match in the non-forwarding table
145 * The index of the FIB
148 * The prefix to lookup
151 * The index of the fib_entry_t for the best match, which may be the default route
153 extern fib_node_index_t fib_table_lookup(u32 fib_index,
154 const fib_prefix_t *prefix);
158 * Perfom an exact match in the non-forwarding table
161 * The index of the FIB
164 * The prefix to lookup
167 * The index of the fib_entry_t for the exact match, or INVALID
168 * is there is no match.
170 extern fib_node_index_t fib_table_lookup_exact_match(u32 fib_index,
171 const fib_prefix_t *prefix);
175 * Get the less specific (covering) prefix
178 * The index of the FIB
181 * The prefix to lookup
184 * The index of the less specific fib_entry_t.
186 extern fib_node_index_t fib_table_get_less_specific(u32 fib_index,
187 const fib_prefix_t *prefix);
191 * Add a 'special' entry to the FIB.
192 * A special entry is an entry that the FIB is not expect to resolve
193 * via the usual mechanisms (i.e. recurisve or neighbour adj DB lookup).
194 * Instead the will link to a DPO valid for the source and/or the flags.
195 * This add is reference counting per-source. So n 'removes' are required
196 * for n 'adds', if the entry is no longer required.
197 * If the source needs to provide non-default forwarding use:
198 * fib_table_entry_special_dpo_add()
201 * The index of the FIB
207 * The ID of the client/source adding the entry.
210 * Flags for the entry.
213 * the index of the fib_entry_t that is created (or exists already).
215 extern fib_node_index_t fib_table_entry_special_add(u32 fib_index,
216 const fib_prefix_t *prefix,
218 fib_entry_flag_t flags);
222 * Add a 'special' entry to the FIB that links to the DPO passed
223 * A special entry is an entry that the FIB is not expect to resolve
224 * via the usual mechanisms (i.e. recurisve or neighbour adj DB lookup).
225 * Instead the client/source provides the DPO to link to.
226 * This add is reference counting per-source. So n 'removes' are required
227 * for n 'adds', if the entry is no longer required.
230 * The index of the FIB
236 * The ID of the client/source adding the entry.
239 * Flags for the entry.
242 * The DPO to link to.
245 * the index of the fib_entry_t that is created (or existed already).
247 extern fib_node_index_t fib_table_entry_special_dpo_add(u32 fib_index,
248 const fib_prefix_t *prefix,
250 fib_entry_flag_t stype,
251 const dpo_id_t *dpo);
255 * Update a 'special' entry to the FIB that links to the DPO passed
256 * A special entry is an entry that the FIB is not expect to resolve
257 * via the usual mechanisms (i.e. recurisve or neighbour adj DB lookup).
258 * Instead the client/source provides the DPO to link to.
259 * Special entries are add/remove reference counted per-source. So n
260 * 'removes' are required for n 'adds', if the entry is no longer required.
261 * An 'update' is an 'add' if no 'add' has already been called, otherwise an 'add'
262 * is therefore assumed to act on the reference instance of that add.
264 * @param fib_entry_index
265 * The index of the FIB entry to update
268 * The ID of the client/source adding the entry.
271 * Flags for the entry.
274 * The DPO to link to.
277 * the index of the fib_entry_t that is created (or existed already).
279 extern fib_node_index_t fib_table_entry_special_dpo_update (u32 fib_index,
280 const fib_prefix_t *prefix,
282 fib_entry_flag_t stype,
283 const dpo_id_t *dpo);
287 * Remove a 'special' entry from the FIB.
288 * This add is reference counting per-source. So n 'removes' are required
289 * for n 'adds', if the entry is no longer required.
292 * The index of the FIB
295 * The prefix to remove
298 * The ID of the client/source adding the entry.
301 extern void fib_table_entry_special_remove(u32 fib_index,
302 const fib_prefix_t *prefix,
303 fib_source_t source);
307 * Add one path to an entry (aka route) in the FIB. If the entry does not
308 * exist, it will be created.
309 * See the documentation for fib_route_path_t for more descirptions of
310 * the path parameters.
313 * The index of the FIB
316 * The prefix for the entry to add
319 * The ID of the client/source adding the entry.
322 * Flags for the entry.
324 * @paran next_hop_proto
325 * The protocol of the next hop. This cannot be derived in the event that
326 * the next hop is all zeros.
329 * The address of the next-hop.
332 * The index of the interface.
334 * @param next_hop_fib_index,
335 * The fib index of the next-hop for recursive resolution
337 * @param next_hop_weight
338 * [un]equal cost path weight
340 * @param next_hop_label_stack
341 * The path's out-going label stack. NULL is there is none.
347 * the index of the fib_entry_t that is created (or existed already).
349 extern fib_node_index_t fib_table_entry_path_add(u32 fib_index,
350 const fib_prefix_t *prefix,
352 fib_entry_flag_t flags,
353 dpo_proto_t next_hop_proto,
354 const ip46_address_t *next_hop,
355 u32 next_hop_sw_if_index,
356 u32 next_hop_fib_index,
358 fib_mpls_label_t *next_hop_label_stack,
359 fib_route_path_flags_t pf);
362 * Add n paths to an entry (aka route) in the FIB. If the entry does not
363 * exist, it will be created.
364 * See the documentation for fib_route_path_t for more descirptions of
365 * the path parameters.
368 * The index of the FIB
371 * The prefix for the entry to add
374 * The ID of the client/source adding the entry.
377 * Flags for the entry.
380 * A vector of paths. Not const since they may be modified.
383 * the index of the fib_entry_t that is created (or existed already).
385 extern fib_node_index_t fib_table_entry_path_add2(u32 fib_index,
386 const fib_prefix_t *prefix,
388 fib_entry_flag_t flags,
389 fib_route_path_t *rpath);
393 * remove one path to an entry (aka route) in the FIB. If this is the entry's
394 * last path, then the entry will be removed, unless it has other sources.
395 * See the documentation for fib_route_path_t for more descirptions of
396 * the path parameters.
399 * The index of the FIB
402 * The prefix for the entry to add
405 * The ID of the client/source adding the entry.
407 * @paran next_hop_proto
408 * The protocol of the next hop. This cannot be derived in the event that
409 * the next hop is all zeros.
412 * The address of the next-hop.
415 * The index of the interface.
417 * @param next_hop_fib_index,
418 * The fib index of the next-hop for recursive resolution
420 * @param next_hop_weight
421 * [un]equal cost path weight
426 extern void fib_table_entry_path_remove(u32 fib_index,
427 const fib_prefix_t *prefix,
429 dpo_proto_t next_hop_proto,
430 const ip46_address_t *next_hop,
431 u32 next_hop_sw_if_index,
432 u32 next_hop_fib_index,
434 fib_route_path_flags_t pf);
438 * Remove n paths to an entry (aka route) in the FIB. If this is the entry's
439 * last path, then the entry will be removed, unless it has other sources.
440 * See the documentation for fib_route_path_t for more descirptions of
441 * the path parameters.
444 * The index of the FIB
447 * The prefix for the entry to add
450 * The ID of the client/source adding the entry.
455 extern void fib_table_entry_path_remove2(u32 fib_index,
456 const fib_prefix_t *prefix,
458 fib_route_path_t *paths);
462 * Update an entry to have a new set of paths. If the entry does not
463 * exist, it will be created.
464 * The difference between an 'path-add' and an update, is that path-add is
465 * an incremental addition of paths, whereas an update is a wholesale swap.
468 * The index of the FIB
471 * The prefix for the entry to add
474 * The ID of the client/source adding the entry.
477 * A vector of paths. Not const since they may be modified.
480 * the index of the fib_entry_t that is created (or existed already).
482 extern fib_node_index_t fib_table_entry_update(u32 fib_index,
483 const fib_prefix_t *prefix,
485 fib_entry_flag_t flags,
486 fib_route_path_t *paths);
490 * Update the entry to have just one path. If the entry does not
491 * exist, it will be created.
492 * See the documentation for fib_route_path_t for more descirptions of
493 * the path parameters.
496 * The index of the FIB
499 * The prefix for the entry to add
502 * The ID of the client/source adding the entry.
505 * Flags for the entry.
507 * @paran next_hop_proto
508 * The protocol of the next hop. This cannot be derived in the event that
509 * the next hop is all zeros.
512 * The address of the next-hop.
515 * The index of the interface.
517 * @param next_hop_fib_index,
518 * The fib index of the next-hop for recursive resolution
520 * @param next_hop_weight
521 * [un]equal cost path weight
523 * @param next_hop_label_stack
524 * The path's out-going label stack. NULL is there is none.
530 * the index of the fib_entry_t that is created (or existed already).
532 extern fib_node_index_t fib_table_entry_update_one_path(u32 fib_index,
533 const fib_prefix_t *prefix,
535 fib_entry_flag_t flags,
536 dpo_proto_t next_hop_proto,
537 const ip46_address_t *next_hop,
538 u32 next_hop_sw_if_index,
539 u32 next_hop_fib_index,
541 fib_mpls_label_t *next_hop_label_stack,
542 fib_route_path_flags_t pf);
546 * Add a MPLS local label for the prefix/route. If the entry does not
547 * exist, it will be created. In theory more than one local label can be
548 * added, but this is not yet supported.
551 * The index of the FIB
554 * The prefix for the entry to which to add the label
557 * The MPLS label to add
560 * the index of the fib_entry_t that is created (or existed already).
562 extern fib_node_index_t fib_table_entry_local_label_add(u32 fib_index,
563 const fib_prefix_t *prefix,
567 * remove a MPLS local label for the prefix/route.
570 * The index of the FIB
573 * The prefix for the entry to which to add the label
576 * The MPLS label to add
578 extern void fib_table_entry_local_label_remove(u32 fib_index,
579 const fib_prefix_t *prefix,
584 * Delete a FIB entry. If the entry has no more sources, then it is
585 * removed from the table.
588 * The index of the FIB
591 * The prefix for the entry to remove
594 * The ID of the client/source adding the entry.
596 extern void fib_table_entry_delete(u32 fib_index,
597 const fib_prefix_t *prefix,
598 fib_source_t source);
602 * Delete a FIB entry. If the entry has no more sources, then it is
603 * removed from the table.
606 * The index of the FIB entry
609 * The ID of the client/source adding the entry.
611 extern void fib_table_entry_delete_index(fib_node_index_t entry_index,
612 fib_source_t source);
616 * Return the stats index for a FIB entry
618 * The table's FIB index
620 * The entry's prefix's
622 extern u32 fib_table_entry_get_stats_index(u32 fib_index,
623 const fib_prefix_t *prefix);
627 * Flush all entries from a table for the source
630 * The index of the FIB
633 * The protocol of the entries in the table
636 * the source to flush
638 extern void fib_table_flush(u32 fib_index,
639 fib_protocol_t proto,
640 fib_source_t source);
644 * Resync all entries from a table for the source
645 * this is the mark part of the mark and sweep algorithm.
646 * All entries in this FIB that are sourced by 'source' are marked
650 * The index of the FIB
653 * The protocol of the entries in the table
656 * the source to flush
658 extern void fib_table_mark(u32 fib_index,
659 fib_protocol_t proto,
660 fib_source_t source);
664 * Signal that the table has converged, i.e. all updates are complete.
665 * this is the sweep part of the mark and sweep algorithm.
666 * All entries in this FIB that are sourced by 'source' and marked
667 * as stale are flushed.
670 * The index of the FIB
673 * The protocol of the entries in the table
676 * the source to flush
678 extern void fib_table_sweep(u32 fib_index,
679 fib_protocol_t proto,
680 fib_source_t source);
684 * Get the index of the FIB bound to the interface
687 * The protocol of the FIB (and thus the entries therein)
690 * The interface index
693 * The index of the FIB
695 extern u32 fib_table_get_index_for_sw_if_index(fib_protocol_t proto,
700 * Get the Table-ID of the FIB bound to the interface
703 * The protocol of the FIB (and thus the entries therein)
706 * The interface index
709 * The tableID of the FIB
711 extern u32 fib_table_get_table_id_for_sw_if_index(fib_protocol_t proto,
716 * Get the Table-ID of the FIB from protocol and index
722 * The protocol of the FIB (and thus the entries therein)
725 * The tableID of the FIB
727 extern u32 fib_table_get_table_id(u32 fib_index, fib_protocol_t proto);
731 * Get the index of the FIB for a Table-ID. This DOES NOT create the
732 * FIB if it does not exist.
735 * The protocol of the FIB (and thus the entries therein)
741 * The index of the FIB, which may be INVALID.
743 extern u32 fib_table_find(fib_protocol_t proto, u32 table_id);
748 * Get the index of the FIB for a Table-ID. This DOES create the
749 * FIB if it does not exist.
752 * The protocol of the FIB (and thus the entries therein)
758 * The index of the FIB
761 * The ID of the client/source.
763 extern u32 fib_table_find_or_create_and_lock(fib_protocol_t proto,
765 fib_source_t source);
769 * Get the index of the FIB for a Table-ID. This DOES create the
770 * FIB if it does not exist.
773 * The protocol of the FIB (and thus the entries therein)
779 * The index of the FIB
782 * The ID of the client/source.
785 * The client is choosing the name they want the table to have
787 extern u32 fib_table_find_or_create_and_lock_w_name(fib_protocol_t proto,
794 * Create a new table with no table ID. This means it does not get
795 * added to the hash-table and so can only be found by using the index returned.
798 * The protocol of the FIB (and thus the entries therein)
801 * A string to describe the table
804 * The ID of the client/source.
807 * The index of the FIB
809 extern u32 fib_table_create_and_lock(fib_protocol_t proto,
811 const char *const fmt,
816 * Get the flow hash configured used by the table
819 * The index of the FIB
822 * The protocol the packets the flow hash will be calculated for.
824 * @return The flow hash config
826 extern flow_hash_config_t fib_table_get_flow_hash_config(u32 fib_index,
827 fib_protocol_t proto);
831 * Get the flow hash configured used by the protocol
834 * The protocol of the FIB (and thus the entries therein)
836 * @return The flow hash config
838 extern flow_hash_config_t fib_table_get_default_flow_hash_config(fib_protocol_t proto);
842 * Set the flow hash configured used by the table
845 * The index of the FIB
848 * The protocol of the FIB (and thus the entries therein)
851 * The flow-hash config to set
855 extern void fib_table_set_flow_hash_config(u32 fib_index,
856 fib_protocol_t proto,
857 flow_hash_config_t hash_config);
861 * Take a reference counting lock on the table
864 * The index of the FIB
867 * The protocol of the FIB (and thus the entries therein)
870 * The ID of the client/source.
872 extern void fib_table_unlock(u32 fib_index,
873 fib_protocol_t proto,
874 fib_source_t source);
878 * Release a reference counting lock on the table. When the last lock
879 * has gone. the FIB is deleted.
882 * The index of the FIB
885 * The protocol of the FIB (and thus the entries therein)
888 * The ID of the client/source.
890 extern void fib_table_lock(u32 fib_index,
891 fib_protocol_t proto,
892 fib_source_t source);
896 * Return the number of entries in the FIB added by a given source.
899 * The index of the FIB
902 * The protocol of the FIB (and thus the entries therein)
904 * @return number of sourced entries.
906 extern u32 fib_table_get_num_entries(u32 fib_index,
907 fib_protocol_t proto,
908 fib_source_t source);
912 * Get a pointer to a FIB table
914 extern fib_table_t *fib_table_get(fib_node_index_t index,
915 fib_protocol_t proto);
918 * @brief return code controlling how a table walk proceeds
920 typedef enum fib_table_walk_rc_t_
923 * Continue on to the next entry
925 FIB_TABLE_WALK_CONTINUE,
927 * Do no traverse down this sub-tree
929 FIB_TABLE_WALK_SUB_TREE_STOP,
931 * Stop the walk completely
934 } fib_table_walk_rc_t;
937 * @brief Call back function when walking entries in a FIB table
939 typedef fib_table_walk_rc_t (*fib_table_walk_fn_t)(fib_node_index_t fei,
943 * @brief Walk all entries in a FIB table
944 * N.B: This is NOT safe to deletes. If you need to delete walk the whole
945 * table and store elements in a vector, then delete the elements
947 extern void fib_table_walk(u32 fib_index,
948 fib_protocol_t proto,
949 fib_table_walk_fn_t fn,
953 * @brief Walk all entries in a FIB table
954 * N.B: This is NOT safe to deletes. If you need to delete walk the whole
955 * table and store elements in a vector, then delete the elements
957 extern void fib_table_walk_w_src(u32 fib_index,
958 fib_protocol_t proto,
960 fib_table_walk_fn_t fn,
964 * @brief Walk all entries in a sub-tree FIB table. The 'root' paraneter
965 * is the prefix at the root of the sub-tree.
966 * N.B: This is NOT safe to deletes. If you need to delete walk the whole
967 * table and store elements in a vector, then delete the elements
969 extern void fib_table_sub_tree_walk(u32 fib_index,
970 fib_protocol_t proto,
971 const fib_prefix_t *root,
972 fib_table_walk_fn_t fn,
976 * @brief format (display) the memory used by the FIB tables
978 extern u8 *format_fib_table_memory(u8 *s, va_list *args);
984 extern void fib_table_assert_empty(const fib_table_t *fib_table);