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
33 * A protocol Independent FIB table
35 typedef struct fib_table_t_
38 * Which protocol this table serves. Used to switch on the union above.
40 fib_protocol_t ft_proto;
43 * per-source number of locks on the table
45 u16 ft_locks[FIB_TABLE_N_LOCKS];
48 * Table ID (hash key) for this FIB.
53 * Index into FIB vector.
55 fib_node_index_t ft_index;
58 * flow hash configuration
60 u32 ft_flow_hash_config;
63 * Per-source route counters
65 u32 ft_src_route_counts[FIB_SOURCE_MAX];
68 * Total route counters
70 u32 ft_total_route_counts;
80 * Format the description/name of the table
82 extern u8* format_fib_table_name(u8* s, va_list *ap);
86 * Perfom a longest prefix match in the non-forwarding table
89 * The index of the FIB
92 * The prefix to lookup
95 * The index of the fib_entry_t for the best match, which may be the default route
97 extern fib_node_index_t fib_table_lookup(u32 fib_index,
98 const fib_prefix_t *prefix);
102 * Perfom an exact match in the non-forwarding table
105 * The index of the FIB
108 * The prefix to lookup
111 * The index of the fib_entry_t for the exact match, or INVALID
112 * is there is no match.
114 extern fib_node_index_t fib_table_lookup_exact_match(u32 fib_index,
115 const fib_prefix_t *prefix);
119 * Get the less specific (covering) prefix
122 * The index of the FIB
125 * The prefix to lookup
128 * The index of the less specific fib_entry_t.
130 extern fib_node_index_t fib_table_get_less_specific(u32 fib_index,
131 const fib_prefix_t *prefix);
135 * Add a 'special' entry to the FIB.
136 * A special entry is an entry that the FIB is not expect to resolve
137 * via the usual mechanisms (i.e. recurisve or neighbour adj DB lookup).
138 * Instead the will link to a DPO valid for the source and/or the flags.
139 * This add is reference counting per-source. So n 'removes' are required
140 * for n 'adds', if the entry is no longer required.
141 * If the source needs to provide non-default forwarding use:
142 * fib_table_entry_special_dpo_add()
145 * The index of the FIB
151 * The ID of the client/source adding the entry.
154 * Flags for the entry.
157 * the index of the fib_entry_t that is created (or exists already).
159 extern fib_node_index_t fib_table_entry_special_add(u32 fib_index,
160 const fib_prefix_t *prefix,
162 fib_entry_flag_t flags);
166 * Add a 'special' entry to the FIB that links to the DPO passed
167 * A special entry is an entry that the FIB is not expect to resolve
168 * via the usual mechanisms (i.e. recurisve or neighbour adj DB lookup).
169 * Instead the client/source provides the DPO to link to.
170 * This add is reference counting per-source. So n 'removes' are required
171 * for n 'adds', if the entry is no longer required.
174 * The index of the FIB
180 * The ID of the client/source adding the entry.
183 * Flags for the entry.
186 * The DPO to link to.
189 * the index of the fib_entry_t that is created (or existed already).
191 extern fib_node_index_t fib_table_entry_special_dpo_add(u32 fib_index,
192 const fib_prefix_t *prefix,
194 fib_entry_flag_t stype,
195 const dpo_id_t *dpo);
199 * Update a 'special' entry to the FIB that links to the DPO passed
200 * A special entry is an entry that the FIB is not expect to resolve
201 * via the usual mechanisms (i.e. recurisve or neighbour adj DB lookup).
202 * Instead the client/source provides the DPO to link to.
203 * Special entries are add/remove reference counted per-source. So n
204 * 'removes' are required for n 'adds', if the entry is no longer required.
205 * An 'update' is an 'add' if no 'add' has already been called, otherwise an 'add'
206 * is therefore assumed to act on the reference instance of that add.
208 * @param fib_entry_index
209 * The index of the FIB entry to update
212 * The ID of the client/source adding the entry.
215 * Flags for the entry.
218 * The DPO to link to.
221 * the index of the fib_entry_t that is created (or existed already).
223 extern fib_node_index_t fib_table_entry_special_dpo_update (u32 fib_index,
224 const fib_prefix_t *prefix,
226 fib_entry_flag_t stype,
227 const dpo_id_t *dpo);
231 * Remove a 'special' entry from the FIB.
232 * This add is reference counting per-source. So n 'removes' are required
233 * for n 'adds', if the entry is no longer required.
236 * The index of the FIB
239 * The prefix to remove
242 * The ID of the client/source adding the entry.
245 extern void fib_table_entry_special_remove(u32 fib_index,
246 const fib_prefix_t *prefix,
247 fib_source_t source);
251 * Add one path to an entry (aka route) in the FIB. If the entry does not
252 * exist, it will be created.
253 * See the documentation for fib_route_path_t for more descirptions of
254 * the path parameters.
257 * The index of the FIB
260 * The prefix for the entry to add
263 * The ID of the client/source adding the entry.
266 * Flags for the entry.
268 * @paran next_hop_proto
269 * The protocol of the next hop. This cannot be derived in the event that
270 * the next hop is all zeros.
273 * The address of the next-hop.
276 * The index of the interface.
278 * @param next_hop_fib_index,
279 * The fib index of the next-hop for recursive resolution
281 * @param next_hop_weight
282 * [un]equal cost path weight
284 * @param next_hop_label_stack
285 * The path's out-going label stack. NULL is there is none.
291 * the index of the fib_entry_t that is created (or existed already).
293 extern fib_node_index_t fib_table_entry_path_add(u32 fib_index,
294 const fib_prefix_t *prefix,
296 fib_entry_flag_t flags,
297 dpo_proto_t next_hop_proto,
298 const ip46_address_t *next_hop,
299 u32 next_hop_sw_if_index,
300 u32 next_hop_fib_index,
302 mpls_label_t *next_hop_label_stack,
303 fib_route_path_flags_t pf);
306 * Add n paths to an entry (aka route) in the FIB. If the entry does not
307 * exist, it will be created.
308 * See the documentation for fib_route_path_t for more descirptions of
309 * the path parameters.
312 * The index of the FIB
315 * The prefix for the entry to add
318 * The ID of the client/source adding the entry.
321 * Flags for the entry.
324 * A vector of paths. Not const since they may be modified.
327 * the index of the fib_entry_t that is created (or existed already).
329 extern fib_node_index_t fib_table_entry_path_add2(u32 fib_index,
330 const fib_prefix_t *prefix,
332 fib_entry_flag_t flags,
333 fib_route_path_t *rpath);
337 * remove one path to an entry (aka route) in the FIB. If this is the entry's
338 * last path, then the entry will be removed, unless it has other sources.
339 * See the documentation for fib_route_path_t for more descirptions of
340 * the path parameters.
343 * The index of the FIB
346 * The prefix for the entry to add
349 * The ID of the client/source adding the entry.
351 * @paran next_hop_proto
352 * The protocol of the next hop. This cannot be derived in the event that
353 * the next hop is all zeros.
356 * The address of the next-hop.
359 * The index of the interface.
361 * @param next_hop_fib_index,
362 * The fib index of the next-hop for recursive resolution
364 * @param next_hop_weight
365 * [un]equal cost path weight
370 extern void fib_table_entry_path_remove(u32 fib_index,
371 const fib_prefix_t *prefix,
373 dpo_proto_t next_hop_proto,
374 const ip46_address_t *next_hop,
375 u32 next_hop_sw_if_index,
376 u32 next_hop_fib_index,
378 fib_route_path_flags_t pf);
382 * Remove n paths to an entry (aka route) in the FIB. If this is the entry's
383 * last path, then the entry will be removed, unless it has other sources.
384 * See the documentation for fib_route_path_t for more descirptions of
385 * the path parameters.
388 * The index of the FIB
391 * The prefix for the entry to add
394 * The ID of the client/source adding the entry.
399 extern void fib_table_entry_path_remove2(u32 fib_index,
400 const fib_prefix_t *prefix,
402 fib_route_path_t *paths);
406 * Update an entry to have a new set of paths. If the entry does not
407 * exist, it will be created.
408 * The difference between an 'path-add' and an update, is that path-add is
409 * an incremental addition of paths, whereas an update is a wholesale swap.
412 * The index of the FIB
415 * The prefix for the entry to add
418 * The ID of the client/source adding the entry.
421 * A vector of paths. Not const since they may be modified.
424 * the index of the fib_entry_t that is created (or existed already).
426 extern fib_node_index_t fib_table_entry_update(u32 fib_index,
427 const fib_prefix_t *prefix,
429 fib_entry_flag_t flags,
430 fib_route_path_t *paths);
434 * Update the entry to have just one path. If the entry does not
435 * exist, it will be created.
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.
449 * Flags for the entry.
451 * @paran next_hop_proto
452 * The protocol of the next hop. This cannot be derived in the event that
453 * the next hop is all zeros.
456 * The address of the next-hop.
459 * The index of the interface.
461 * @param next_hop_fib_index,
462 * The fib index of the next-hop for recursive resolution
464 * @param next_hop_weight
465 * [un]equal cost path weight
467 * @param next_hop_label_stack
468 * The path's out-going label stack. NULL is there is none.
474 * the index of the fib_entry_t that is created (or existed already).
476 extern fib_node_index_t fib_table_entry_update_one_path(u32 fib_index,
477 const fib_prefix_t *prefix,
479 fib_entry_flag_t flags,
480 dpo_proto_t next_hop_proto,
481 const ip46_address_t *next_hop,
482 u32 next_hop_sw_if_index,
483 u32 next_hop_fib_index,
485 mpls_label_t *next_hop_label_stack,
486 fib_route_path_flags_t pf);
490 * Add a MPLS local label for the prefix/route. If the entry does not
491 * exist, it will be created. In theory more than one local label can be
492 * added, but this is not yet supported.
495 * The index of the FIB
498 * The prefix for the entry to which to add the label
501 * The MPLS label to add
504 * the index of the fib_entry_t that is created (or existed already).
506 extern fib_node_index_t fib_table_entry_local_label_add(u32 fib_index,
507 const fib_prefix_t *prefix,
511 * remove a MPLS local label for the prefix/route.
514 * The index of the FIB
517 * The prefix for the entry to which to add the label
520 * The MPLS label to add
522 extern void fib_table_entry_local_label_remove(u32 fib_index,
523 const fib_prefix_t *prefix,
528 * Delete a FIB entry. If the entry has no more sources, then it is
529 * removed from the table.
532 * The index of the FIB
535 * The prefix for the entry to remove
538 * The ID of the client/source adding the entry.
540 extern void fib_table_entry_delete(u32 fib_index,
541 const fib_prefix_t *prefix,
542 fib_source_t source);
546 * Delete a FIB entry. If the entry has no more sources, then it is
547 * removed from the table.
550 * The index of the FIB entry
553 * The ID of the client/source adding the entry.
555 extern void fib_table_entry_delete_index(fib_node_index_t entry_index,
556 fib_source_t source);
560 * Flush all entries from a table for the source
563 * The index of the FIB
566 * The protocol of the entries in the table
569 * the source to flush
571 extern void fib_table_flush(u32 fib_index,
572 fib_protocol_t proto,
573 fib_source_t source);
577 * Get the index of the FIB bound to the interface
580 * The protocol of the FIB (and thus the entries therein)
583 * The interface index
586 * The index of the FIB
588 extern u32 fib_table_get_index_for_sw_if_index(fib_protocol_t proto,
593 * Get the Table-ID of the FIB bound to the interface
596 * The protocol of the FIB (and thus the entries therein)
599 * The interface index
602 * The tableID of the FIB
604 extern u32 fib_table_get_table_id_for_sw_if_index(fib_protocol_t proto,
609 * Get the index of the FIB for a Table-ID. This DOES NOT create the
610 * FIB if it does not exist.
613 * The protocol of the FIB (and thus the entries therein)
619 * The index of the FIB, which may be INVALID.
621 extern u32 fib_table_find(fib_protocol_t proto, u32 table_id);
626 * Get the index of the FIB for a Table-ID. This DOES create the
627 * FIB if it does not exist.
630 * The protocol of the FIB (and thus the entries therein)
636 * The index of the FIB
639 * The ID of the client/source.
641 extern u32 fib_table_find_or_create_and_lock(fib_protocol_t proto,
643 fib_source_t source);
647 * Get the index of the FIB for a Table-ID. This DOES create the
648 * FIB if it does not exist.
651 * The protocol of the FIB (and thus the entries therein)
657 * The index of the FIB
660 * The ID of the client/source.
663 * The client is choosing the name they want the table to have
665 extern u32 fib_table_find_or_create_and_lock_w_name(fib_protocol_t proto,
672 * Create a new table with no table ID. This means it does not get
673 * added to the hash-table and so can only be found by using the index returned.
676 * The protocol of the FIB (and thus the entries therein)
679 * A string to describe the table
682 * The ID of the client/source.
685 * The index of the FIB
687 extern u32 fib_table_create_and_lock(fib_protocol_t proto,
689 const char *const fmt,
694 * Get the flow hash configured used by the table
697 * The index of the FIB
700 * The protocol the packets the flow hash will be calculated for.
702 * @return The flow hash config
704 extern flow_hash_config_t fib_table_get_flow_hash_config(u32 fib_index,
705 fib_protocol_t proto);
709 * Get the flow hash configured used by the protocol
712 * The protocol of the FIB (and thus the entries therein)
714 * @return The flow hash config
716 extern flow_hash_config_t fib_table_get_default_flow_hash_config(fib_protocol_t proto);
720 * Set the flow hash configured used by the table
723 * The index of the FIB
726 * The protocol of the FIB (and thus the entries therein)
729 * The flow-hash config to set
733 extern void fib_table_set_flow_hash_config(u32 fib_index,
734 fib_protocol_t proto,
735 flow_hash_config_t hash_config);
739 * Take a reference counting lock on the table
742 * The index of the FIB
745 * The protocol of the FIB (and thus the entries therein)
748 * The ID of the client/source.
750 extern void fib_table_unlock(u32 fib_index,
751 fib_protocol_t proto,
752 fib_source_t source);
756 * Release a reference counting lock on the table. When the last lock
757 * has gone. the FIB is deleted.
760 * The index of the FIB
763 * The protocol of the FIB (and thus the entries therein)
766 * The ID of the client/source.
768 extern void fib_table_lock(u32 fib_index,
769 fib_protocol_t proto,
770 fib_source_t source);
774 * Return the number of entries in the FIB added by a given source.
777 * The index of the FIB
780 * The protocol of the FIB (and thus the entries therein)
782 * @return number of sourced entries.
784 extern u32 fib_table_get_num_entries(u32 fib_index,
785 fib_protocol_t proto,
786 fib_source_t source);
790 * Get a pointer to a FIB table
792 extern fib_table_t *fib_table_get(fib_node_index_t index,
793 fib_protocol_t proto);
796 * @brief Call back function when walking entries in a FIB table
798 typedef int (*fib_table_walk_fn_t)(fib_node_index_t fei,
802 * @brief Walk all entries in a FIB table
803 * N.B: This is NOT safe to deletes. If you need to delete walk the whole
804 * table and store elements in a vector, then delete the elements
806 extern void fib_table_walk(u32 fib_index,
807 fib_protocol_t proto,
808 fib_table_walk_fn_t fn,