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 * Keep a lock per-source and a total
28 #define FIB_TABLE_N_LOCKS (FIB_SOURCE_MAX+1)
29 #define FIB_TABLE_TOTAL_LOCKS FIB_SOURCE_MAX
32 * Flags for the source data
34 typedef enum fib_table_attribute_t_ {
36 * Marker. Add new values after this one.
38 FIB_TABLE_ATTRIBUTE_FIRST,
40 * the table is for IP6 link local addresses
42 FIB_TABLE_ATTRIBUTE_IP6_LL = FIB_TABLE_ATTRIBUTE_FIRST,
44 * Marker. add new entries before this one.
46 FIB_TABLE_ATTRIBUTE_LAST = FIB_TABLE_ATTRIBUTE_IP6_LL,
47 } fib_table_attribute_t;
49 #define FIB_TABLE_ATTRIBUTE_MAX (FIB_TABLE_ATTRIBUTE_LAST+1)
51 #define FIB_TABLE_ATTRIBUTES { \
52 [FIB_TABLE_ATTRIBUTE_IP6_LL] = "ip6-ll", \
55 #define FOR_EACH_FIB_TABLE_ATTRIBUTE(_item) \
56 for (_item = FIB_TABLE_ATTRIBUTE_FIRST; \
57 _item < FIB_TABLE_ATTRIBUTE_MAX; \
60 typedef enum fib_table_flags_t_ {
61 FIB_TABLE_FLAG_NONE = 0,
62 FIB_TABLE_FLAG_IP6_LL = (1 << FIB_TABLE_ATTRIBUTE_IP6_LL),
63 } __attribute__ ((packed)) fib_table_flags_t;
67 * A protocol Independent FIB table
69 typedef struct fib_table_t_
72 * Which protocol this table serves. Used to switch on the union above.
74 fib_protocol_t ft_proto;
79 fib_table_flags_t ft_flags;
82 * per-source number of locks on the table
84 u16 ft_locks[FIB_TABLE_N_LOCKS];
87 * Table ID (hash key) for this FIB.
92 * Index into FIB vector.
94 fib_node_index_t ft_index;
97 * flow hash configuration
99 u32 ft_flow_hash_config;
102 * Per-source route counters
104 u32 ft_src_route_counts[FIB_SOURCE_MAX];
107 * Total route counters
109 u32 ft_total_route_counts;
119 * Format the description/name of the table
121 extern u8* format_fib_table_name(u8* s, va_list *ap);
125 * Perfom a longest prefix match in the non-forwarding table
128 * The index of the FIB
131 * The prefix to lookup
134 * The index of the fib_entry_t for the best match, which may be the default route
136 extern fib_node_index_t fib_table_lookup(u32 fib_index,
137 const fib_prefix_t *prefix);
141 * Perfom an exact match in the non-forwarding table
144 * The index of the FIB
147 * The prefix to lookup
150 * The index of the fib_entry_t for the exact match, or INVALID
151 * is there is no match.
153 extern fib_node_index_t fib_table_lookup_exact_match(u32 fib_index,
154 const fib_prefix_t *prefix);
158 * Get the less specific (covering) prefix
161 * The index of the FIB
164 * The prefix to lookup
167 * The index of the less specific fib_entry_t.
169 extern fib_node_index_t fib_table_get_less_specific(u32 fib_index,
170 const fib_prefix_t *prefix);
174 * Add a 'special' entry to the FIB.
175 * A special entry is an entry that the FIB is not expect to resolve
176 * via the usual mechanisms (i.e. recurisve or neighbour adj DB lookup).
177 * Instead the will link to a DPO valid for the source and/or the flags.
178 * This add is reference counting per-source. So n 'removes' are required
179 * for n 'adds', if the entry is no longer required.
180 * If the source needs to provide non-default forwarding use:
181 * fib_table_entry_special_dpo_add()
184 * The index of the FIB
190 * The ID of the client/source adding the entry.
193 * Flags for the entry.
196 * the index of the fib_entry_t that is created (or exists already).
198 extern fib_node_index_t fib_table_entry_special_add(u32 fib_index,
199 const fib_prefix_t *prefix,
201 fib_entry_flag_t flags);
205 * Add a 'special' entry to the FIB that links to the DPO passed
206 * A special entry is an entry that the FIB is not expect to resolve
207 * via the usual mechanisms (i.e. recurisve or neighbour adj DB lookup).
208 * Instead the client/source provides the DPO to link to.
209 * This add is reference counting per-source. So n 'removes' are required
210 * for n 'adds', if the entry is no longer required.
213 * The index of the FIB
219 * The ID of the client/source adding the entry.
222 * Flags for the entry.
225 * The DPO to link to.
228 * the index of the fib_entry_t that is created (or existed already).
230 extern fib_node_index_t fib_table_entry_special_dpo_add(u32 fib_index,
231 const fib_prefix_t *prefix,
233 fib_entry_flag_t stype,
234 const dpo_id_t *dpo);
238 * Update a 'special' entry to the FIB that links to the DPO passed
239 * A special entry is an entry that the FIB is not expect to resolve
240 * via the usual mechanisms (i.e. recurisve or neighbour adj DB lookup).
241 * Instead the client/source provides the DPO to link to.
242 * Special entries are add/remove reference counted per-source. So n
243 * 'removes' are required for n 'adds', if the entry is no longer required.
244 * An 'update' is an 'add' if no 'add' has already been called, otherwise an 'add'
245 * is therefore assumed to act on the reference instance of that add.
247 * @param fib_entry_index
248 * The index of the FIB entry to update
251 * The ID of the client/source adding the entry.
254 * Flags for the entry.
257 * The DPO to link to.
260 * the index of the fib_entry_t that is created (or existed already).
262 extern fib_node_index_t fib_table_entry_special_dpo_update (u32 fib_index,
263 const fib_prefix_t *prefix,
265 fib_entry_flag_t stype,
266 const dpo_id_t *dpo);
270 * Remove a 'special' entry from the FIB.
271 * This add is reference counting per-source. So n 'removes' are required
272 * for n 'adds', if the entry is no longer required.
275 * The index of the FIB
278 * The prefix to remove
281 * The ID of the client/source adding the entry.
284 extern void fib_table_entry_special_remove(u32 fib_index,
285 const fib_prefix_t *prefix,
286 fib_source_t source);
290 * Add one path to an entry (aka route) in the FIB. If the entry does not
291 * exist, it will be created.
292 * See the documentation for fib_route_path_t for more descirptions of
293 * the path parameters.
296 * The index of the FIB
299 * The prefix for the entry to add
302 * The ID of the client/source adding the entry.
305 * Flags for the entry.
307 * @paran next_hop_proto
308 * The protocol of the next hop. This cannot be derived in the event that
309 * the next hop is all zeros.
312 * The address of the next-hop.
315 * The index of the interface.
317 * @param next_hop_fib_index,
318 * The fib index of the next-hop for recursive resolution
320 * @param next_hop_weight
321 * [un]equal cost path weight
323 * @param next_hop_label_stack
324 * The path's out-going label stack. NULL is there is none.
330 * the index of the fib_entry_t that is created (or existed already).
332 extern fib_node_index_t fib_table_entry_path_add(u32 fib_index,
333 const fib_prefix_t *prefix,
335 fib_entry_flag_t flags,
336 dpo_proto_t next_hop_proto,
337 const ip46_address_t *next_hop,
338 u32 next_hop_sw_if_index,
339 u32 next_hop_fib_index,
341 fib_mpls_label_t *next_hop_label_stack,
342 fib_route_path_flags_t pf);
345 * Add n paths to an entry (aka route) in the FIB. If the entry does not
346 * exist, it will be created.
347 * See the documentation for fib_route_path_t for more descirptions of
348 * the path parameters.
351 * The index of the FIB
354 * The prefix for the entry to add
357 * The ID of the client/source adding the entry.
360 * Flags for the entry.
363 * A vector of paths. Not const since they may be modified.
366 * the index of the fib_entry_t that is created (or existed already).
368 extern fib_node_index_t fib_table_entry_path_add2(u32 fib_index,
369 const fib_prefix_t *prefix,
371 fib_entry_flag_t flags,
372 fib_route_path_t *rpath);
376 * remove one path to an entry (aka route) in the FIB. If this is the entry's
377 * last path, then the entry will be removed, unless it has other sources.
378 * See the documentation for fib_route_path_t for more descirptions of
379 * the path parameters.
382 * The index of the FIB
385 * The prefix for the entry to add
388 * The ID of the client/source adding the entry.
390 * @paran next_hop_proto
391 * The protocol of the next hop. This cannot be derived in the event that
392 * the next hop is all zeros.
395 * The address of the next-hop.
398 * The index of the interface.
400 * @param next_hop_fib_index,
401 * The fib index of the next-hop for recursive resolution
403 * @param next_hop_weight
404 * [un]equal cost path weight
409 extern void fib_table_entry_path_remove(u32 fib_index,
410 const fib_prefix_t *prefix,
412 dpo_proto_t next_hop_proto,
413 const ip46_address_t *next_hop,
414 u32 next_hop_sw_if_index,
415 u32 next_hop_fib_index,
417 fib_route_path_flags_t pf);
421 * Remove n paths to an entry (aka route) in the FIB. If this is the entry's
422 * last path, then the entry will be removed, unless it has other sources.
423 * See the documentation for fib_route_path_t for more descirptions of
424 * the path parameters.
427 * The index of the FIB
430 * The prefix for the entry to add
433 * The ID of the client/source adding the entry.
438 extern void fib_table_entry_path_remove2(u32 fib_index,
439 const fib_prefix_t *prefix,
441 fib_route_path_t *paths);
445 * Update an entry to have a new set of paths. If the entry does not
446 * exist, it will be created.
447 * The difference between an 'path-add' and an update, is that path-add is
448 * an incremental addition of paths, whereas an update is a wholesale swap.
451 * The index of the FIB
454 * The prefix for the entry to add
457 * The ID of the client/source adding the entry.
460 * A vector of paths. Not const since they may be modified.
463 * the index of the fib_entry_t that is created (or existed already).
465 extern fib_node_index_t fib_table_entry_update(u32 fib_index,
466 const fib_prefix_t *prefix,
468 fib_entry_flag_t flags,
469 fib_route_path_t *paths);
473 * Update the entry to have just one path. If the entry does not
474 * exist, it will be created.
475 * See the documentation for fib_route_path_t for more descirptions of
476 * the path parameters.
479 * The index of the FIB
482 * The prefix for the entry to add
485 * The ID of the client/source adding the entry.
488 * Flags for the entry.
490 * @paran next_hop_proto
491 * The protocol of the next hop. This cannot be derived in the event that
492 * the next hop is all zeros.
495 * The address of the next-hop.
498 * The index of the interface.
500 * @param next_hop_fib_index,
501 * The fib index of the next-hop for recursive resolution
503 * @param next_hop_weight
504 * [un]equal cost path weight
506 * @param next_hop_label_stack
507 * The path's out-going label stack. NULL is there is none.
513 * the index of the fib_entry_t that is created (or existed already).
515 extern fib_node_index_t fib_table_entry_update_one_path(u32 fib_index,
516 const fib_prefix_t *prefix,
518 fib_entry_flag_t flags,
519 dpo_proto_t next_hop_proto,
520 const ip46_address_t *next_hop,
521 u32 next_hop_sw_if_index,
522 u32 next_hop_fib_index,
524 fib_mpls_label_t *next_hop_label_stack,
525 fib_route_path_flags_t pf);
529 * Add a MPLS local label for the prefix/route. If the entry does not
530 * exist, it will be created. In theory more than one local label can be
531 * added, but this is not yet supported.
534 * The index of the FIB
537 * The prefix for the entry to which to add the label
540 * The MPLS label to add
543 * the index of the fib_entry_t that is created (or existed already).
545 extern fib_node_index_t fib_table_entry_local_label_add(u32 fib_index,
546 const fib_prefix_t *prefix,
550 * remove a MPLS local label for the prefix/route.
553 * The index of the FIB
556 * The prefix for the entry to which to add the label
559 * The MPLS label to add
561 extern void fib_table_entry_local_label_remove(u32 fib_index,
562 const fib_prefix_t *prefix,
567 * Delete a FIB entry. If the entry has no more sources, then it is
568 * removed from the table.
571 * The index of the FIB
574 * The prefix for the entry to remove
577 * The ID of the client/source adding the entry.
579 extern void fib_table_entry_delete(u32 fib_index,
580 const fib_prefix_t *prefix,
581 fib_source_t source);
585 * Delete a FIB entry. If the entry has no more sources, then it is
586 * removed from the table.
589 * The index of the FIB entry
592 * The ID of the client/source adding the entry.
594 extern void fib_table_entry_delete_index(fib_node_index_t entry_index,
595 fib_source_t source);
599 * Flush all entries from a table for the source
602 * The index of the FIB
605 * The protocol of the entries in the table
608 * the source to flush
610 extern void fib_table_flush(u32 fib_index,
611 fib_protocol_t proto,
612 fib_source_t source);
616 * Get the index of the FIB bound to the interface
619 * The protocol of the FIB (and thus the entries therein)
622 * The interface index
625 * The index of the FIB
627 extern u32 fib_table_get_index_for_sw_if_index(fib_protocol_t proto,
632 * Get the Table-ID of the FIB bound to the interface
635 * The protocol of the FIB (and thus the entries therein)
638 * The interface index
641 * The tableID of the FIB
643 extern u32 fib_table_get_table_id_for_sw_if_index(fib_protocol_t proto,
648 * Get the index of the FIB for a Table-ID. This DOES NOT create the
649 * FIB if it does not exist.
652 * The protocol of the FIB (and thus the entries therein)
658 * The index of the FIB, which may be INVALID.
660 extern u32 fib_table_find(fib_protocol_t proto, u32 table_id);
665 * Get the index of the FIB for a Table-ID. This DOES create the
666 * FIB if it does not exist.
669 * The protocol of the FIB (and thus the entries therein)
675 * The index of the FIB
678 * The ID of the client/source.
680 extern u32 fib_table_find_or_create_and_lock(fib_protocol_t proto,
682 fib_source_t source);
686 * Get the index of the FIB for a Table-ID. This DOES create the
687 * FIB if it does not exist.
690 * The protocol of the FIB (and thus the entries therein)
696 * The index of the FIB
699 * The ID of the client/source.
702 * The client is choosing the name they want the table to have
704 extern u32 fib_table_find_or_create_and_lock_w_name(fib_protocol_t proto,
711 * Create a new table with no table ID. This means it does not get
712 * added to the hash-table and so can only be found by using the index returned.
715 * The protocol of the FIB (and thus the entries therein)
718 * A string to describe the table
721 * The ID of the client/source.
724 * The index of the FIB
726 extern u32 fib_table_create_and_lock(fib_protocol_t proto,
728 const char *const fmt,
733 * Get the flow hash configured used by the table
736 * The index of the FIB
739 * The protocol the packets the flow hash will be calculated for.
741 * @return The flow hash config
743 extern flow_hash_config_t fib_table_get_flow_hash_config(u32 fib_index,
744 fib_protocol_t proto);
748 * Get the flow hash configured used by the protocol
751 * The protocol of the FIB (and thus the entries therein)
753 * @return The flow hash config
755 extern flow_hash_config_t fib_table_get_default_flow_hash_config(fib_protocol_t proto);
759 * Set the flow hash configured used by the table
762 * The index of the FIB
765 * The protocol of the FIB (and thus the entries therein)
768 * The flow-hash config to set
772 extern void fib_table_set_flow_hash_config(u32 fib_index,
773 fib_protocol_t proto,
774 flow_hash_config_t hash_config);
778 * Take a reference counting lock on the table
781 * The index of the FIB
784 * The protocol of the FIB (and thus the entries therein)
787 * The ID of the client/source.
789 extern void fib_table_unlock(u32 fib_index,
790 fib_protocol_t proto,
791 fib_source_t source);
795 * Release a reference counting lock on the table. When the last lock
796 * has gone. the FIB is deleted.
799 * The index of the FIB
802 * The protocol of the FIB (and thus the entries therein)
805 * The ID of the client/source.
807 extern void fib_table_lock(u32 fib_index,
808 fib_protocol_t proto,
809 fib_source_t source);
813 * Return the number of entries in the FIB added by a given source.
816 * The index of the FIB
819 * The protocol of the FIB (and thus the entries therein)
821 * @return number of sourced entries.
823 extern u32 fib_table_get_num_entries(u32 fib_index,
824 fib_protocol_t proto,
825 fib_source_t source);
829 * Get a pointer to a FIB table
831 extern fib_table_t *fib_table_get(fib_node_index_t index,
832 fib_protocol_t proto);
835 * @brief return code controlling how a table walk proceeds
837 typedef enum fib_table_walk_rc_t_
840 * Continue on to the next entry
842 FIB_TABLE_WALK_CONTINUE,
844 * Do no traverse down this sub-tree
846 FIB_TABLE_WALK_SUB_TREE_STOP,
848 * Stop the walk completely
851 } fib_table_walk_rc_t;
854 * @brief Call back function when walking entries in a FIB table
856 typedef fib_table_walk_rc_t (*fib_table_walk_fn_t)(fib_node_index_t fei,
860 * @brief Walk all entries in a FIB table
861 * N.B: This is NOT safe to deletes. If you need to delete walk the whole
862 * table and store elements in a vector, then delete the elements
864 extern void fib_table_walk(u32 fib_index,
865 fib_protocol_t proto,
866 fib_table_walk_fn_t fn,
870 * @brief Walk all entries in a sub-tree FIB table. The 'root' paraneter
871 * is the prefix at the root of the sub-tree.
872 * N.B: This is NOT safe to deletes. If you need to delete walk the whole
873 * table and store elements in a vector, then delete the elements
875 extern void fib_table_sub_tree_walk(u32 fib_index,
876 fib_protocol_t proto,
877 const fib_prefix_t *root,
878 fib_table_walk_fn_t fn,
882 * @brief format (display) the memory used by the FIB tables
884 extern u8 *format_fib_table_memory(u8 *s, va_list *args);