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 * the table is currently resync-ing
46 FIB_TABLE_ATTRIBUTE_RESYNC,
48 * Marker. add new entries before this one.
50 FIB_TABLE_ATTRIBUTE_LAST = FIB_TABLE_ATTRIBUTE_RESYNC,
51 } fib_table_attribute_t;
53 #define FIB_TABLE_ATTRIBUTE_MAX (FIB_TABLE_ATTRIBUTE_LAST+1)
55 #define FIB_TABLE_ATTRIBUTES { \
56 [FIB_TABLE_ATTRIBUTE_IP6_LL] = "ip6-ll", \
57 [FIB_TABLE_ATTRIBUTE_RESYNC] = "resync", \
60 #define FOR_EACH_FIB_TABLE_ATTRIBUTE(_item) \
61 for (_item = FIB_TABLE_ATTRIBUTE_FIRST; \
62 _item < FIB_TABLE_ATTRIBUTE_MAX; \
65 typedef enum fib_table_flags_t_ {
66 FIB_TABLE_FLAG_NONE = 0,
67 FIB_TABLE_FLAG_IP6_LL = (1 << FIB_TABLE_ATTRIBUTE_IP6_LL),
68 FIB_TABLE_FLAG_RESYNC = (1 << FIB_TABLE_ATTRIBUTE_RESYNC),
69 } __attribute__ ((packed)) fib_table_flags_t;
71 extern u8* format_fib_table_flags(u8 *s, va_list *args);
75 * A protocol Independent FIB table
77 typedef struct fib_table_t_
80 * Which protocol this table serves. Used to switch on the union above.
82 fib_protocol_t ft_proto;
87 fib_table_flags_t ft_flags;
90 * per-source number of locks on the table
92 u16 ft_locks[FIB_TABLE_N_LOCKS];
95 * Table ID (hash key) for this FIB.
100 * Index into FIB vector.
102 fib_node_index_t ft_index;
105 * flow hash configuration
107 u32 ft_flow_hash_config;
110 * Per-source route counters
112 u32 ft_src_route_counts[FIB_SOURCE_MAX];
115 * Total route counters
117 u32 ft_total_route_counts;
120 * Epoch - number of resyncs performed
132 * Format the description/name of the table
134 extern u8* format_fib_table_name(u8* s, va_list *ap);
138 * Perfom a longest prefix match in the non-forwarding table
141 * The index of the FIB
144 * The prefix to lookup
147 * The index of the fib_entry_t for the best match, which may be the default route
149 extern fib_node_index_t fib_table_lookup(u32 fib_index,
150 const fib_prefix_t *prefix);
154 * Perfom an exact match in the non-forwarding table
157 * The index of the FIB
160 * The prefix to lookup
163 * The index of the fib_entry_t for the exact match, or INVALID
164 * is there is no match.
166 extern fib_node_index_t fib_table_lookup_exact_match(u32 fib_index,
167 const fib_prefix_t *prefix);
171 * Get the less specific (covering) prefix
174 * The index of the FIB
177 * The prefix to lookup
180 * The index of the less specific fib_entry_t.
182 extern fib_node_index_t fib_table_get_less_specific(u32 fib_index,
183 const fib_prefix_t *prefix);
187 * Add a 'special' entry to the FIB.
188 * A special entry is an entry that the FIB is not expect to resolve
189 * via the usual mechanisms (i.e. recurisve or neighbour adj DB lookup).
190 * Instead the will link to a DPO valid for the source and/or the flags.
191 * This add is reference counting per-source. So n 'removes' are required
192 * for n 'adds', if the entry is no longer required.
193 * If the source needs to provide non-default forwarding use:
194 * fib_table_entry_special_dpo_add()
197 * The index of the FIB
203 * The ID of the client/source adding the entry.
206 * Flags for the entry.
209 * the index of the fib_entry_t that is created (or exists already).
211 extern fib_node_index_t fib_table_entry_special_add(u32 fib_index,
212 const fib_prefix_t *prefix,
214 fib_entry_flag_t flags);
218 * Add a 'special' entry to the FIB that links to the DPO passed
219 * A special entry is an entry that the FIB is not expect to resolve
220 * via the usual mechanisms (i.e. recurisve or neighbour adj DB lookup).
221 * Instead the client/source provides the DPO to link to.
222 * This add is reference counting per-source. So n 'removes' are required
223 * for n 'adds', if the entry is no longer required.
226 * The index of the FIB
232 * The ID of the client/source adding the entry.
235 * Flags for the entry.
238 * The DPO to link to.
241 * the index of the fib_entry_t that is created (or existed already).
243 extern fib_node_index_t fib_table_entry_special_dpo_add(u32 fib_index,
244 const fib_prefix_t *prefix,
246 fib_entry_flag_t stype,
247 const dpo_id_t *dpo);
251 * Update a 'special' entry to the FIB that links to the DPO passed
252 * A special entry is an entry that the FIB is not expect to resolve
253 * via the usual mechanisms (i.e. recurisve or neighbour adj DB lookup).
254 * Instead the client/source provides the DPO to link to.
255 * Special entries are add/remove reference counted per-source. So n
256 * 'removes' are required for n 'adds', if the entry is no longer required.
257 * An 'update' is an 'add' if no 'add' has already been called, otherwise an 'add'
258 * is therefore assumed to act on the reference instance of that add.
260 * @param fib_entry_index
261 * The index of the FIB entry to update
264 * The ID of the client/source adding the entry.
267 * Flags for the entry.
270 * The DPO to link to.
273 * the index of the fib_entry_t that is created (or existed already).
275 extern fib_node_index_t fib_table_entry_special_dpo_update (u32 fib_index,
276 const fib_prefix_t *prefix,
278 fib_entry_flag_t stype,
279 const dpo_id_t *dpo);
283 * Remove a 'special' entry from the FIB.
284 * This add is reference counting per-source. So n 'removes' are required
285 * for n 'adds', if the entry is no longer required.
288 * The index of the FIB
291 * The prefix to remove
294 * The ID of the client/source adding the entry.
297 extern void fib_table_entry_special_remove(u32 fib_index,
298 const fib_prefix_t *prefix,
299 fib_source_t source);
303 * Add one path to an entry (aka route) in the FIB. If the entry does not
304 * exist, it will be created.
305 * See the documentation for fib_route_path_t for more descirptions of
306 * the path parameters.
309 * The index of the FIB
312 * The prefix for the entry to add
315 * The ID of the client/source adding the entry.
318 * Flags for the entry.
320 * @paran next_hop_proto
321 * The protocol of the next hop. This cannot be derived in the event that
322 * the next hop is all zeros.
325 * The address of the next-hop.
328 * The index of the interface.
330 * @param next_hop_fib_index,
331 * The fib index of the next-hop for recursive resolution
333 * @param next_hop_weight
334 * [un]equal cost path weight
336 * @param next_hop_label_stack
337 * The path's out-going label stack. NULL is there is none.
343 * the index of the fib_entry_t that is created (or existed already).
345 extern fib_node_index_t fib_table_entry_path_add(u32 fib_index,
346 const fib_prefix_t *prefix,
348 fib_entry_flag_t flags,
349 dpo_proto_t next_hop_proto,
350 const ip46_address_t *next_hop,
351 u32 next_hop_sw_if_index,
352 u32 next_hop_fib_index,
354 fib_mpls_label_t *next_hop_label_stack,
355 fib_route_path_flags_t pf);
358 * Add n paths to an entry (aka route) in the FIB. If the entry does not
359 * exist, it will be created.
360 * See the documentation for fib_route_path_t for more descirptions of
361 * the path parameters.
364 * The index of the FIB
367 * The prefix for the entry to add
370 * The ID of the client/source adding the entry.
373 * Flags for the entry.
376 * A vector of paths. Not const since they may be modified.
379 * the index of the fib_entry_t that is created (or existed already).
381 extern fib_node_index_t fib_table_entry_path_add2(u32 fib_index,
382 const fib_prefix_t *prefix,
384 fib_entry_flag_t flags,
385 fib_route_path_t *rpath);
389 * remove one path to an entry (aka route) in the FIB. If this is the entry's
390 * last path, then the entry will be removed, unless it has other sources.
391 * See the documentation for fib_route_path_t for more descirptions of
392 * the path parameters.
395 * The index of the FIB
398 * The prefix for the entry to add
401 * The ID of the client/source adding the entry.
403 * @paran next_hop_proto
404 * The protocol of the next hop. This cannot be derived in the event that
405 * the next hop is all zeros.
408 * The address of the next-hop.
411 * The index of the interface.
413 * @param next_hop_fib_index,
414 * The fib index of the next-hop for recursive resolution
416 * @param next_hop_weight
417 * [un]equal cost path weight
422 extern void fib_table_entry_path_remove(u32 fib_index,
423 const fib_prefix_t *prefix,
425 dpo_proto_t next_hop_proto,
426 const ip46_address_t *next_hop,
427 u32 next_hop_sw_if_index,
428 u32 next_hop_fib_index,
430 fib_route_path_flags_t pf);
434 * Remove n paths to an entry (aka route) in the FIB. If this is the entry's
435 * last path, then the entry will be removed, unless it has other sources.
436 * See the documentation for fib_route_path_t for more descirptions of
437 * the path parameters.
440 * The index of the FIB
443 * The prefix for the entry to add
446 * The ID of the client/source adding the entry.
451 extern void fib_table_entry_path_remove2(u32 fib_index,
452 const fib_prefix_t *prefix,
454 fib_route_path_t *paths);
458 * Update an entry to have a new set of paths. If the entry does not
459 * exist, it will be created.
460 * The difference between an 'path-add' and an update, is that path-add is
461 * an incremental addition of paths, whereas an update is a wholesale swap.
464 * The index of the FIB
467 * The prefix for the entry to add
470 * The ID of the client/source adding the entry.
473 * A vector of paths. Not const since they may be modified.
476 * the index of the fib_entry_t that is created (or existed already).
478 extern fib_node_index_t fib_table_entry_update(u32 fib_index,
479 const fib_prefix_t *prefix,
481 fib_entry_flag_t flags,
482 fib_route_path_t *paths);
486 * Update the entry to have just one path. If the entry does not
487 * exist, it will be created.
488 * See the documentation for fib_route_path_t for more descirptions of
489 * the path parameters.
492 * The index of the FIB
495 * The prefix for the entry to add
498 * The ID of the client/source adding the entry.
501 * Flags for the entry.
503 * @paran next_hop_proto
504 * The protocol of the next hop. This cannot be derived in the event that
505 * the next hop is all zeros.
508 * The address of the next-hop.
511 * The index of the interface.
513 * @param next_hop_fib_index,
514 * The fib index of the next-hop for recursive resolution
516 * @param next_hop_weight
517 * [un]equal cost path weight
519 * @param next_hop_label_stack
520 * The path's out-going label stack. NULL is there is none.
526 * the index of the fib_entry_t that is created (or existed already).
528 extern fib_node_index_t fib_table_entry_update_one_path(u32 fib_index,
529 const fib_prefix_t *prefix,
531 fib_entry_flag_t flags,
532 dpo_proto_t next_hop_proto,
533 const ip46_address_t *next_hop,
534 u32 next_hop_sw_if_index,
535 u32 next_hop_fib_index,
537 fib_mpls_label_t *next_hop_label_stack,
538 fib_route_path_flags_t pf);
542 * Add a MPLS local label for the prefix/route. If the entry does not
543 * exist, it will be created. In theory more than one local label can be
544 * added, but this is not yet supported.
547 * The index of the FIB
550 * The prefix for the entry to which to add the label
553 * The MPLS label to add
556 * the index of the fib_entry_t that is created (or existed already).
558 extern fib_node_index_t fib_table_entry_local_label_add(u32 fib_index,
559 const fib_prefix_t *prefix,
563 * remove a MPLS local label for the prefix/route.
566 * The index of the FIB
569 * The prefix for the entry to which to add the label
572 * The MPLS label to add
574 extern void fib_table_entry_local_label_remove(u32 fib_index,
575 const fib_prefix_t *prefix,
580 * Delete a FIB entry. If the entry has no more sources, then it is
581 * removed from the table.
584 * The index of the FIB
587 * The prefix for the entry to remove
590 * The ID of the client/source adding the entry.
592 extern void fib_table_entry_delete(u32 fib_index,
593 const fib_prefix_t *prefix,
594 fib_source_t source);
598 * Delete a FIB entry. If the entry has no more sources, then it is
599 * removed from the table.
602 * The index of the FIB entry
605 * The ID of the client/source adding the entry.
607 extern void fib_table_entry_delete_index(fib_node_index_t entry_index,
608 fib_source_t source);
612 * Return the stats index for a FIB entry
614 * The table's FIB index
616 * The entry's prefix's
618 extern u32 fib_table_entry_get_stats_index(u32 fib_index,
619 const fib_prefix_t *prefix);
623 * Flush all entries from a table for the source
626 * The index of the FIB
629 * The protocol of the entries in the table
632 * the source to flush
634 extern void fib_table_flush(u32 fib_index,
635 fib_protocol_t proto,
636 fib_source_t source);
640 * Resync all entries from a table for the source
641 * this is the mark part of the mark and sweep algorithm.
642 * All entries in this FIB that are sourced by 'source' are marked
646 * The index of the FIB
649 * The protocol of the entries in the table
652 * the source to flush
654 extern void fib_table_mark(u32 fib_index,
655 fib_protocol_t proto,
656 fib_source_t source);
660 * Signal that the table has converged, i.e. all updates are complete.
661 * this is the sweep part of the mark and sweep algorithm.
662 * All entries in this FIB that are sourced by 'source' and marked
663 * as stale are flushed.
666 * The index of the FIB
669 * The protocol of the entries in the table
672 * the source to flush
674 extern void fib_table_sweep(u32 fib_index,
675 fib_protocol_t proto,
676 fib_source_t source);
680 * Get the index of the FIB bound to the interface
683 * The protocol of the FIB (and thus the entries therein)
686 * The interface index
689 * The index of the FIB
691 extern u32 fib_table_get_index_for_sw_if_index(fib_protocol_t proto,
696 * Get the Table-ID of the FIB bound to the interface
699 * The protocol of the FIB (and thus the entries therein)
702 * The interface index
705 * The tableID of the FIB
707 extern u32 fib_table_get_table_id_for_sw_if_index(fib_protocol_t proto,
712 * Get the Table-ID of the FIB from protocol and index
718 * The protocol of the FIB (and thus the entries therein)
721 * The tableID of the FIB
723 extern u32 fib_table_get_table_id(u32 fib_index, fib_protocol_t proto);
727 * Get the index of the FIB for a Table-ID. This DOES NOT create the
728 * FIB if it does not exist.
731 * The protocol of the FIB (and thus the entries therein)
737 * The index of the FIB, which may be INVALID.
739 extern u32 fib_table_find(fib_protocol_t proto, u32 table_id);
744 * Get the index of the FIB for a Table-ID. This DOES create the
745 * FIB if it does not exist.
748 * The protocol of the FIB (and thus the entries therein)
754 * The index of the FIB
757 * The ID of the client/source.
759 extern u32 fib_table_find_or_create_and_lock(fib_protocol_t proto,
761 fib_source_t source);
765 * Get the index of the FIB for a Table-ID. This DOES create the
766 * FIB if it does not exist.
769 * The protocol of the FIB (and thus the entries therein)
775 * The index of the FIB
778 * The ID of the client/source.
781 * The client is choosing the name they want the table to have
783 extern u32 fib_table_find_or_create_and_lock_w_name(fib_protocol_t proto,
790 * Create a new table with no table ID. This means it does not get
791 * added to the hash-table and so can only be found by using the index returned.
794 * The protocol of the FIB (and thus the entries therein)
797 * A string to describe the table
800 * The ID of the client/source.
803 * The index of the FIB
805 extern u32 fib_table_create_and_lock(fib_protocol_t proto,
807 const char *const fmt,
812 * Get the flow hash configured used by the table
815 * The index of the FIB
818 * The protocol the packets the flow hash will be calculated for.
820 * @return The flow hash config
822 extern flow_hash_config_t fib_table_get_flow_hash_config(u32 fib_index,
823 fib_protocol_t proto);
827 * Get the flow hash configured used by the protocol
830 * The protocol of the FIB (and thus the entries therein)
832 * @return The flow hash config
834 extern flow_hash_config_t fib_table_get_default_flow_hash_config(fib_protocol_t proto);
838 * Set the flow hash configured used by the table
841 * The index of the FIB
844 * The protocol of the FIB (and thus the entries therein)
847 * The flow-hash config to set
851 extern void fib_table_set_flow_hash_config(u32 fib_index,
852 fib_protocol_t proto,
853 flow_hash_config_t hash_config);
857 * Take a reference counting lock on the table
860 * The index of the FIB
863 * The protocol of the FIB (and thus the entries therein)
866 * The ID of the client/source.
868 extern void fib_table_unlock(u32 fib_index,
869 fib_protocol_t proto,
870 fib_source_t source);
874 * Release a reference counting lock on the table. When the last lock
875 * has gone. the FIB is deleted.
878 * The index of the FIB
881 * The protocol of the FIB (and thus the entries therein)
884 * The ID of the client/source.
886 extern void fib_table_lock(u32 fib_index,
887 fib_protocol_t proto,
888 fib_source_t source);
892 * Return the number of entries in the FIB added by a given source.
895 * The index of the FIB
898 * The protocol of the FIB (and thus the entries therein)
900 * @return number of sourced entries.
902 extern u32 fib_table_get_num_entries(u32 fib_index,
903 fib_protocol_t proto,
904 fib_source_t source);
908 * Get a pointer to a FIB table
910 extern fib_table_t *fib_table_get(fib_node_index_t index,
911 fib_protocol_t proto);
914 * @brief return code controlling how a table walk proceeds
916 typedef enum fib_table_walk_rc_t_
919 * Continue on to the next entry
921 FIB_TABLE_WALK_CONTINUE,
923 * Do no traverse down this sub-tree
925 FIB_TABLE_WALK_SUB_TREE_STOP,
927 * Stop the walk completely
930 } fib_table_walk_rc_t;
933 * @brief Call back function when walking entries in a FIB table
935 typedef fib_table_walk_rc_t (*fib_table_walk_fn_t)(fib_node_index_t fei,
939 * @brief Walk all entries in a FIB table
940 * N.B: This is NOT safe to deletes. If you need to delete walk the whole
941 * table and store elements in a vector, then delete the elements
943 extern void fib_table_walk(u32 fib_index,
944 fib_protocol_t proto,
945 fib_table_walk_fn_t fn,
949 * @brief Walk all entries in a sub-tree FIB table. The 'root' paraneter
950 * is the prefix at the root of the sub-tree.
951 * N.B: This is NOT safe to deletes. If you need to delete walk the whole
952 * table and store elements in a vector, then delete the elements
954 extern void fib_table_sub_tree_walk(u32 fib_index,
955 fib_protocol_t proto,
956 const fib_prefix_t *root,
957 fib_table_walk_fn_t fn,
961 * @brief format (display) the memory used by the FIB tables
963 extern u8 *format_fib_table_memory(u8 *s, va_list *args);