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 #include <vlib/vlib.h>
17 #include <vnet/dpo/drop_dpo.h>
19 #include <vnet/fib/fib_table.h>
20 #include <vnet/fib/fib_entry_cover.h>
21 #include <vnet/fib/fib_internal.h>
22 #include <vnet/fib/ip4_fib.h>
23 #include <vnet/fib/ip6_fib.h>
24 #include <vnet/fib/mpls_fib.h>
26 const static char * fib_table_flags_strings[] = FIB_TABLE_ATTRIBUTES;
29 fib_table_get (fib_node_index_t index,
34 case FIB_PROTOCOL_IP4:
35 return (pool_elt_at_index(ip4_main.fibs, index));
36 case FIB_PROTOCOL_IP6:
37 return (pool_elt_at_index(ip6_main.fibs, index));
38 case FIB_PROTOCOL_MPLS:
39 return (pool_elt_at_index(mpls_main.fibs, index));
45 static inline fib_node_index_t
46 fib_table_lookup_i (fib_table_t *fib_table,
47 const fib_prefix_t *prefix)
49 switch (prefix->fp_proto)
51 case FIB_PROTOCOL_IP4:
52 return (ip4_fib_table_lookup(ip4_fib_get(fib_table->ft_index),
55 case FIB_PROTOCOL_IP6:
56 return (ip6_fib_table_lookup(fib_table->ft_index,
59 case FIB_PROTOCOL_MPLS:
60 return (mpls_fib_table_lookup(mpls_fib_get(fib_table->ft_index),
64 return (FIB_NODE_INDEX_INVALID);
68 fib_table_lookup (u32 fib_index,
69 const fib_prefix_t *prefix)
71 return (fib_table_lookup_i(fib_table_get(fib_index, prefix->fp_proto), prefix));
74 static inline fib_node_index_t
75 fib_table_lookup_exact_match_i (const fib_table_t *fib_table,
76 const fib_prefix_t *prefix)
78 switch (prefix->fp_proto)
80 case FIB_PROTOCOL_IP4:
81 return (ip4_fib_table_lookup_exact_match(ip4_fib_get(fib_table->ft_index),
84 case FIB_PROTOCOL_IP6:
85 return (ip6_fib_table_lookup_exact_match(fib_table->ft_index,
88 case FIB_PROTOCOL_MPLS:
89 return (mpls_fib_table_lookup(mpls_fib_get(fib_table->ft_index),
93 return (FIB_NODE_INDEX_INVALID);
97 fib_table_lookup_exact_match (u32 fib_index,
98 const fib_prefix_t *prefix)
100 return (fib_table_lookup_exact_match_i(fib_table_get(fib_index,
105 static fib_node_index_t
106 fib_table_get_less_specific_i (fib_table_t *fib_table,
107 const fib_prefix_t *prefix)
113 if (FIB_PROTOCOL_MPLS == pfx.fp_proto)
115 return (FIB_NODE_INDEX_INVALID);
119 * in the absence of a tree structure for the table that allows for an O(1)
120 * parent get, a cheeky way to find the cover is to LPM for the prefix with
122 * there should always be a cover, though it may be the default route. the
123 * default route's cover is the default route.
125 if (pfx.fp_len != 0) {
129 return (fib_table_lookup_i(fib_table, &pfx));
133 fib_table_get_less_specific (u32 fib_index,
134 const fib_prefix_t *prefix)
136 return (fib_table_get_less_specific_i(fib_table_get(fib_index,
142 fib_table_entry_remove (fib_table_t *fib_table,
143 const fib_prefix_t *prefix,
144 fib_node_index_t fib_entry_index)
146 vlib_smp_unsafe_warning();
148 fib_table->ft_total_route_counts--;
150 switch (prefix->fp_proto)
152 case FIB_PROTOCOL_IP4:
153 ip4_fib_table_entry_remove(ip4_fib_get(fib_table->ft_index),
154 &prefix->fp_addr.ip4,
157 case FIB_PROTOCOL_IP6:
158 ip6_fib_table_entry_remove(fib_table->ft_index,
159 &prefix->fp_addr.ip6,
162 case FIB_PROTOCOL_MPLS:
163 mpls_fib_table_entry_remove(mpls_fib_get(fib_table->ft_index),
169 fib_entry_unlock(fib_entry_index);
173 fib_table_post_insert_actions (fib_table_t *fib_table,
174 const fib_prefix_t *prefix,
175 fib_node_index_t fib_entry_index)
177 fib_node_index_t fib_entry_cover_index;
180 * no cover relationships in the MPLS FIB
182 if (FIB_PROTOCOL_MPLS == prefix->fp_proto)
186 * find the covering entry
188 fib_entry_cover_index = fib_table_get_less_specific_i(fib_table, prefix);
190 * the indicies are the same when the default route is first added
192 if (fib_entry_cover_index != fib_entry_index)
195 * push any inherting sources from the cover onto the covered
197 fib_entry_inherit(fib_entry_cover_index,
201 * inform the covering entry that a new more specific
202 * has been inserted beneath it.
203 * If the prefix that has been inserted is a host route
204 * then it is not possible that it will be the cover for any
205 * other entry, so we can elide the walk. This is particularly
206 * beneficial since there are often many host entries sharing the
207 * same cover (i.e. ADJ or RR sourced entries).
209 if (!fib_entry_is_host(fib_entry_index))
211 fib_entry_cover_change_notify(fib_entry_cover_index,
218 fib_table_entry_insert (fib_table_t *fib_table,
219 const fib_prefix_t *prefix,
220 fib_node_index_t fib_entry_index)
222 vlib_smp_unsafe_warning();
224 fib_entry_lock(fib_entry_index);
225 fib_table->ft_total_route_counts++;
227 switch (prefix->fp_proto)
229 case FIB_PROTOCOL_IP4:
230 ip4_fib_table_entry_insert(ip4_fib_get(fib_table->ft_index),
231 &prefix->fp_addr.ip4,
235 case FIB_PROTOCOL_IP6:
236 ip6_fib_table_entry_insert(fib_table->ft_index,
237 &prefix->fp_addr.ip6,
241 case FIB_PROTOCOL_MPLS:
242 mpls_fib_table_entry_insert(mpls_fib_get(fib_table->ft_index),
249 fib_table_post_insert_actions(fib_table, prefix, fib_entry_index);
253 fib_table_fwding_dpo_update (u32 fib_index,
254 const fib_prefix_t *prefix,
257 vlib_smp_unsafe_warning();
259 switch (prefix->fp_proto)
261 case FIB_PROTOCOL_IP4:
262 return (ip4_fib_table_fwding_dpo_update(ip4_fib_get(fib_index),
263 &prefix->fp_addr.ip4,
266 case FIB_PROTOCOL_IP6:
267 return (ip6_fib_table_fwding_dpo_update(fib_index,
268 &prefix->fp_addr.ip6,
271 case FIB_PROTOCOL_MPLS:
272 return (mpls_fib_forwarding_table_update(mpls_fib_get(fib_index),
280 fib_table_fwding_dpo_remove (u32 fib_index,
281 const fib_prefix_t *prefix,
284 vlib_smp_unsafe_warning();
286 switch (prefix->fp_proto)
288 case FIB_PROTOCOL_IP4:
289 return (ip4_fib_table_fwding_dpo_remove(ip4_fib_get(fib_index),
290 &prefix->fp_addr.ip4,
293 fib_table_get_less_specific(fib_index,
295 case FIB_PROTOCOL_IP6:
296 return (ip6_fib_table_fwding_dpo_remove(fib_index,
297 &prefix->fp_addr.ip6,
300 case FIB_PROTOCOL_MPLS:
301 return (mpls_fib_forwarding_table_reset(mpls_fib_get(fib_index),
308 fib_table_source_count_inc (fib_table_t *fib_table,
311 vec_validate (fib_table->ft_src_route_counts, source);
312 fib_table->ft_src_route_counts[source]++;
316 fib_table_source_count_dec (fib_table_t *fib_table,
319 vec_validate (fib_table->ft_src_route_counts, source);
320 fib_table->ft_src_route_counts[source]--;
324 fib_table_entry_special_dpo_add (u32 fib_index,
325 const fib_prefix_t *prefix,
327 fib_entry_flag_t flags,
330 fib_node_index_t fib_entry_index;
331 fib_table_t *fib_table;
333 fib_table = fib_table_get(fib_index, prefix->fp_proto);
334 fib_entry_index = fib_table_lookup_exact_match_i(fib_table, prefix);
336 if (FIB_NODE_INDEX_INVALID == fib_entry_index)
338 fib_entry_index = fib_entry_create_special(fib_index, prefix,
342 fib_table_entry_insert(fib_table, prefix, fib_entry_index);
343 fib_table_source_count_inc(fib_table, source);
349 was_sourced = fib_entry_is_sourced(fib_entry_index, source);
350 fib_entry_special_add(fib_entry_index, source, flags, dpo);
352 if (was_sourced != fib_entry_is_sourced(fib_entry_index, source))
354 fib_table_source_count_inc(fib_table, source);
359 return (fib_entry_index);
363 fib_table_entry_special_dpo_update (u32 fib_index,
364 const fib_prefix_t *prefix,
366 fib_entry_flag_t flags,
369 fib_node_index_t fib_entry_index;
370 fib_table_t *fib_table;
372 fib_table = fib_table_get(fib_index, prefix->fp_proto);
373 fib_entry_index = fib_table_lookup_exact_match_i(fib_table, prefix);
375 if (FIB_NODE_INDEX_INVALID == fib_entry_index)
377 fib_entry_index = fib_entry_create_special(fib_index, prefix,
381 fib_table_entry_insert(fib_table, prefix, fib_entry_index);
382 fib_table_source_count_inc(fib_table, source);
388 was_sourced = fib_entry_is_sourced(fib_entry_index, source);
391 fib_entry_special_update(fib_entry_index, source, flags, dpo);
393 fib_entry_special_add(fib_entry_index, source, flags, dpo);
395 if (was_sourced != fib_entry_is_sourced(fib_entry_index, source))
397 fib_table_source_count_inc(fib_table, source);
401 return (fib_entry_index);
405 fib_table_entry_special_add (u32 fib_index,
406 const fib_prefix_t *prefix,
408 fib_entry_flag_t flags)
410 fib_node_index_t fib_entry_index;
411 dpo_id_t tmp_dpo = DPO_INVALID;
413 dpo_copy(&tmp_dpo, drop_dpo_get(fib_proto_to_dpo(prefix->fp_proto)));
415 fib_entry_index = fib_table_entry_special_dpo_add(fib_index, prefix, source,
418 dpo_unlock(&tmp_dpo);
420 return (fib_entry_index);
424 fib_table_entry_special_remove (u32 fib_index,
425 const fib_prefix_t *prefix,
430 * yes => remove source
431 * 2 - is it still sourced?
434 fib_node_index_t fib_entry_index;
435 fib_table_t *fib_table;
437 fib_table = fib_table_get(fib_index, prefix->fp_proto);
438 fib_entry_index = fib_table_lookup_exact_match_i(fib_table, prefix);
440 if (FIB_NODE_INDEX_INVALID == fib_entry_index)
443 * removing an etry that does not exist. i'll allow it.
448 fib_entry_src_flag_t src_flag;
452 * don't nobody go nowhere
454 fib_entry_lock(fib_entry_index);
455 was_sourced = fib_entry_is_sourced(fib_entry_index, source);
457 src_flag = fib_entry_special_remove(fib_entry_index, source);
459 if (!(FIB_ENTRY_SRC_FLAG_ADDED & src_flag))
462 * last source gone. remove from the table
464 fib_table_entry_remove(fib_table, prefix, fib_entry_index);
467 * now the entry is no longer in the table, we can
468 * inform the entries that it covers to re-calculate their cover
470 fib_entry_cover_change_notify(fib_entry_index,
471 FIB_NODE_INDEX_INVALID);
475 * still has sources, leave it be.
477 if (was_sourced != fib_entry_is_sourced(fib_entry_index, source))
479 fib_table_source_count_dec(fib_table, source);
482 fib_entry_unlock(fib_entry_index);
487 * fib_table_route_path_fixup
489 * Convert attached hosts to attached next-hops.
491 * This special case is required because an attached path will link to a
492 * glean, and the FIB entry will have the interface or API/CLI source. When
493 * the ARP/ND process is completes then that source (which will provide a
494 * complete adjacency) will be lower priority and so the FIB entry will
495 * remain linked to a glean and traffic will never reach the hosts. For
496 * an ATTAHCED_HOST path we can link the path directly to the [incomplete]
500 fib_table_route_path_fixup (const fib_prefix_t *prefix,
501 fib_entry_flag_t *eflags,
502 fib_route_path_t *path)
505 * not all zeros next hop &&
506 * is recursive path &&
507 * nexthop is same as the route's address
509 if ((!ip46_address_is_zero(&path->frp_addr)) &&
510 (~0 == path->frp_sw_if_index) &&
511 (0 == ip46_address_cmp(&path->frp_addr, &prefix->fp_addr)))
513 /* Prefix recurses via itse;f */
514 path->frp_flags |= FIB_ROUTE_PATH_DROP;
516 if (!(path->frp_flags & FIB_ROUTE_PATH_LOCAL) &&
517 fib_prefix_is_host(prefix) &&
518 ip46_address_is_zero(&path->frp_addr) &&
519 path->frp_sw_if_index != ~0 &&
520 path->frp_proto != DPO_PROTO_ETHERNET)
522 path->frp_addr = prefix->fp_addr;
523 path->frp_flags |= FIB_ROUTE_PATH_ATTACHED;
525 if (*eflags & FIB_ENTRY_FLAG_DROP)
527 path->frp_flags |= FIB_ROUTE_PATH_DROP;
529 if (*eflags & FIB_ENTRY_FLAG_LOCAL)
531 path->frp_flags |= FIB_ROUTE_PATH_LOCAL;
533 if (*eflags & FIB_ENTRY_FLAG_EXCLUSIVE)
535 path->frp_flags |= FIB_ROUTE_PATH_EXCLUSIVE;
537 if (path->frp_flags & FIB_ROUTE_PATH_LOCAL)
539 *eflags |= FIB_ENTRY_FLAG_LOCAL;
541 if (path->frp_sw_if_index != ~0)
543 *eflags |= FIB_ENTRY_FLAG_CONNECTED;
549 fib_table_entry_path_add (u32 fib_index,
550 const fib_prefix_t *prefix,
552 fib_entry_flag_t flags,
553 dpo_proto_t next_hop_proto,
554 const ip46_address_t *next_hop,
555 u32 next_hop_sw_if_index,
556 u32 next_hop_fib_index,
558 fib_mpls_label_t *next_hop_labels,
559 fib_route_path_flags_t path_flags)
561 fib_route_path_t path = {
562 .frp_proto = next_hop_proto,
563 .frp_addr = (NULL == next_hop? zero_addr : *next_hop),
564 .frp_sw_if_index = next_hop_sw_if_index,
565 .frp_fib_index = next_hop_fib_index,
566 .frp_weight = next_hop_weight,
567 .frp_flags = path_flags,
568 .frp_rpf_id = INDEX_INVALID,
569 .frp_label_stack = next_hop_labels,
571 fib_node_index_t fib_entry_index;
572 fib_route_path_t *paths = NULL;
574 vec_add1(paths, path);
576 fib_entry_index = fib_table_entry_path_add2(fib_index, prefix,
577 source, flags, paths);
580 return (fib_entry_index);
584 fib_route_path_cmp_for_sort (void * v1,
587 return (fib_route_path_cmp(v1, v2));
591 fib_table_entry_path_add2 (u32 fib_index,
592 const fib_prefix_t *prefix,
594 fib_entry_flag_t flags,
595 fib_route_path_t *rpaths)
597 fib_node_index_t fib_entry_index;
598 fib_table_t *fib_table;
601 fib_table = fib_table_get(fib_index, prefix->fp_proto);
602 fib_entry_index = fib_table_lookup_exact_match_i(fib_table, prefix);
604 for (ii = 0; ii < vec_len(rpaths); ii++)
606 fib_table_route_path_fixup(prefix, &flags, &rpaths[ii]);
609 * sort the paths provided by the control plane. this means
610 * the paths and the extension on the entry will be sorted.
612 vec_sort_with_function(rpaths, fib_route_path_cmp_for_sort);
614 if (FIB_NODE_INDEX_INVALID == fib_entry_index)
616 fib_entry_index = fib_entry_create(fib_index, prefix,
620 fib_table_entry_insert(fib_table, prefix, fib_entry_index);
621 fib_table_source_count_inc(fib_table, source);
627 was_sourced = fib_entry_is_sourced(fib_entry_index, source);
628 fib_entry_path_add(fib_entry_index, source, flags, rpaths);;
630 if (was_sourced != fib_entry_is_sourced(fib_entry_index, source))
632 fib_table_source_count_inc(fib_table, source);
636 return (fib_entry_index);
640 fib_table_entry_path_remove2 (u32 fib_index,
641 const fib_prefix_t *prefix,
643 fib_route_path_t *rpaths)
647 * yes => remove source
648 * 2 - is it still sourced?
651 fib_node_index_t fib_entry_index;
652 fib_route_path_t *rpath;
653 fib_table_t *fib_table;
655 fib_table = fib_table_get(fib_index, prefix->fp_proto);
656 fib_entry_index = fib_table_lookup_exact_match_i(fib_table, prefix);
658 if (FIB_NODE_INDEX_INVALID == fib_entry_index)
661 * removing an etry that does not exist. i'll allow it.
666 fib_entry_src_flag_t src_flag;
670 * if it's not sourced, then there's nowt to remove
672 was_sourced = fib_entry_is_sourced(fib_entry_index, source);
679 * don't nobody go nowhere
681 fib_entry_lock(fib_entry_index);
683 vec_foreach(rpath, rpaths)
685 fib_entry_flag_t eflags;
687 eflags = fib_entry_get_flags_for_source(fib_entry_index,
689 fib_table_route_path_fixup(prefix, &eflags, rpath);
692 src_flag = fib_entry_path_remove(fib_entry_index, source, rpaths);
694 if (!(FIB_ENTRY_SRC_FLAG_ADDED & src_flag))
697 * last source gone. remove from the table
699 fib_table_entry_remove(fib_table, prefix, fib_entry_index);
702 * now the entry is no longer in the table, we can
703 * inform the entries that it covers to re-calculate their cover
705 fib_entry_cover_change_notify(fib_entry_index,
706 FIB_NODE_INDEX_INVALID);
710 * still has sources, leave it be.
712 if (was_sourced != fib_entry_is_sourced(fib_entry_index, source))
714 fib_table_source_count_dec(fib_table, source);
717 fib_entry_unlock(fib_entry_index);
722 fib_table_entry_path_remove (u32 fib_index,
723 const fib_prefix_t *prefix,
725 dpo_proto_t next_hop_proto,
726 const ip46_address_t *next_hop,
727 u32 next_hop_sw_if_index,
728 u32 next_hop_fib_index,
730 fib_route_path_flags_t path_flags)
734 * yes => remove source
735 * 2 - is it still sourced?
738 fib_route_path_t path = {
739 .frp_proto = next_hop_proto,
740 .frp_addr = (NULL == next_hop? zero_addr : *next_hop),
741 .frp_sw_if_index = next_hop_sw_if_index,
742 .frp_fib_index = next_hop_fib_index,
743 .frp_weight = next_hop_weight,
744 .frp_flags = path_flags,
746 fib_route_path_t *paths = NULL;
748 vec_add1(paths, path);
750 fib_table_entry_path_remove2(fib_index, prefix, source, paths);
756 fib_table_entry_update (u32 fib_index,
757 const fib_prefix_t *prefix,
759 fib_entry_flag_t flags,
760 fib_route_path_t *paths)
762 fib_node_index_t fib_entry_index;
763 fib_table_t *fib_table;
766 fib_table = fib_table_get(fib_index, prefix->fp_proto);
767 fib_entry_index = fib_table_lookup_exact_match_i(fib_table, prefix);
769 for (ii = 0; ii < vec_len(paths); ii++)
771 fib_table_route_path_fixup(prefix, &flags, &paths[ii]);
774 * sort the paths provided by the control plane. this means
775 * the paths and the extension on the entry will be sorted.
777 vec_sort_with_function(paths, fib_route_path_cmp_for_sort);
779 if (FIB_NODE_INDEX_INVALID == fib_entry_index)
781 fib_entry_index = fib_entry_create(fib_index, prefix,
785 fib_table_entry_insert(fib_table, prefix, fib_entry_index);
786 fib_table_source_count_inc(fib_table, source);
792 was_sourced = fib_entry_is_sourced(fib_entry_index, source);
793 fib_entry_update(fib_entry_index, source, flags, paths);
795 if (was_sourced != fib_entry_is_sourced(fib_entry_index, source))
797 fib_table_source_count_inc(fib_table, source);
801 return (fib_entry_index);
805 fib_table_entry_update_one_path (u32 fib_index,
806 const fib_prefix_t *prefix,
808 fib_entry_flag_t flags,
809 dpo_proto_t next_hop_proto,
810 const ip46_address_t *next_hop,
811 u32 next_hop_sw_if_index,
812 u32 next_hop_fib_index,
814 fib_mpls_label_t *next_hop_labels,
815 fib_route_path_flags_t path_flags)
817 fib_node_index_t fib_entry_index;
818 fib_route_path_t path = {
819 .frp_proto = next_hop_proto,
820 .frp_addr = (NULL == next_hop? zero_addr : *next_hop),
821 .frp_sw_if_index = next_hop_sw_if_index,
822 .frp_fib_index = next_hop_fib_index,
823 .frp_weight = next_hop_weight,
824 .frp_flags = path_flags,
825 .frp_label_stack = next_hop_labels,
827 fib_route_path_t *paths = NULL;
829 vec_add1(paths, path);
832 fib_table_entry_update(fib_index, prefix, source, flags, paths);
836 return (fib_entry_index);
840 fib_table_entry_delete_i (u32 fib_index,
841 fib_node_index_t fib_entry_index,
842 const fib_prefix_t *prefix,
845 fib_entry_src_flag_t src_flag;
846 fib_table_t *fib_table;
849 fib_table = fib_table_get(fib_index, prefix->fp_proto);
850 was_sourced = fib_entry_is_sourced(fib_entry_index, source);
853 * don't nobody go nowhere
855 fib_entry_lock(fib_entry_index);
857 src_flag = fib_entry_delete(fib_entry_index, source);
859 if (!(FIB_ENTRY_SRC_FLAG_ADDED & src_flag))
862 * last source gone. remove from the table
864 fib_table_entry_remove(fib_table, prefix, fib_entry_index);
867 * now the entry is no longer in the table, we can
868 * inform the entries that it covers to re-calculate their cover
870 fib_entry_cover_change_notify(fib_entry_index,
871 FIB_NODE_INDEX_INVALID);
875 * still has sources, leave it be.
877 if (was_sourced != fib_entry_is_sourced(fib_entry_index, source))
879 fib_table_source_count_dec(fib_table, source);
882 fib_entry_unlock(fib_entry_index);
886 fib_table_entry_delete (u32 fib_index,
887 const fib_prefix_t *prefix,
890 fib_node_index_t fib_entry_index;
892 fib_entry_index = fib_table_lookup_exact_match(fib_index, prefix);
894 if (FIB_NODE_INDEX_INVALID == fib_entry_index)
897 * removing an etry that does not exist.
898 * i'll allow it, but i won't like it.
901 clib_warning("%U not in FIB", format_fib_prefix, prefix);
905 fib_table_entry_delete_i(fib_index, fib_entry_index, prefix, source);
910 fib_table_entry_delete_index (fib_node_index_t fib_entry_index,
913 const fib_prefix_t *prefix;
915 prefix = fib_entry_get_prefix(fib_entry_index);
917 fib_table_entry_delete_i(fib_entry_get_fib_index(fib_entry_index),
918 fib_entry_index, prefix, source);
922 fib_table_entry_get_stats_index (u32 fib_index,
923 const fib_prefix_t *prefix)
925 return (fib_entry_get_stats_index(
926 fib_table_lookup_exact_match(fib_index, prefix)));
930 fib_table_entry_local_label_add (u32 fib_index,
931 const fib_prefix_t *prefix,
934 fib_node_index_t fib_entry_index;
936 fib_entry_index = fib_table_lookup_exact_match(fib_index, prefix);
938 if (FIB_NODE_INDEX_INVALID == fib_entry_index ||
939 !fib_entry_is_sourced(fib_entry_index, FIB_SOURCE_MPLS))
942 * only source the prefix once. this allows the label change
945 fib_entry_index = fib_table_entry_special_dpo_add(fib_index, prefix,
951 fib_entry_set_source_data(fib_entry_index, FIB_SOURCE_MPLS, &label);
953 return (fib_entry_index);
957 fib_table_entry_local_label_remove (u32 fib_index,
958 const fib_prefix_t *prefix,
961 fib_node_index_t fib_entry_index;
965 fib_entry_index = fib_table_lookup_exact_match(fib_index, prefix);
967 if (FIB_NODE_INDEX_INVALID == fib_entry_index)
970 data = fib_entry_get_source_data(fib_entry_index, FIB_SOURCE_MPLS);
975 pl = *(mpls_label_t*)data;
980 pl = MPLS_LABEL_INVALID;
982 fib_entry_set_source_data(fib_entry_index, FIB_SOURCE_MPLS, &pl);
983 fib_table_entry_special_remove(fib_index,
989 fib_table_get_index_for_sw_if_index (fib_protocol_t proto,
994 case FIB_PROTOCOL_IP4:
995 return (ip4_fib_table_get_index_for_sw_if_index(sw_if_index));
996 case FIB_PROTOCOL_IP6:
997 return (ip6_fib_table_get_index_for_sw_if_index(sw_if_index));
998 case FIB_PROTOCOL_MPLS:
999 return (mpls_fib_table_get_index_for_sw_if_index(sw_if_index));
1005 fib_table_get_flow_hash_config (u32 fib_index,
1006 fib_protocol_t proto)
1010 fib = fib_table_get(fib_index, proto);
1012 return (fib->ft_flow_hash_config);
1016 fib_table_get_default_flow_hash_config (fib_protocol_t proto)
1020 case FIB_PROTOCOL_IP4:
1021 case FIB_PROTOCOL_IP6:
1022 return (IP_FLOW_HASH_DEFAULT);
1024 case FIB_PROTOCOL_MPLS:
1025 return (MPLS_FLOW_HASH_DEFAULT);
1029 return (IP_FLOW_HASH_DEFAULT);
1033 * @brief Table set flow hash config context.
1035 typedef struct fib_table_set_flow_hash_config_ctx_t_
1038 * the flow hash config to set
1040 flow_hash_config_t hash_config;
1041 } fib_table_set_flow_hash_config_ctx_t;
1043 static fib_table_walk_rc_t
1044 fib_table_set_flow_hash_config_cb (fib_node_index_t fib_entry_index,
1047 fib_table_set_flow_hash_config_ctx_t *ctx = arg;
1049 fib_entry_set_flow_hash_config(fib_entry_index, ctx->hash_config);
1051 return (FIB_TABLE_WALK_CONTINUE);
1055 fib_table_set_flow_hash_config (u32 fib_index,
1056 fib_protocol_t proto,
1057 flow_hash_config_t hash_config)
1059 fib_table_set_flow_hash_config_ctx_t ctx = {
1060 .hash_config = hash_config,
1064 fib = fib_table_get(fib_index, proto);
1065 fib->ft_flow_hash_config = hash_config;
1067 fib_table_walk(fib_index, proto,
1068 fib_table_set_flow_hash_config_cb,
1073 fib_table_get_table_id_for_sw_if_index (fib_protocol_t proto,
1076 fib_table_t *fib_table;
1078 fib_table = fib_table_get(fib_table_get_index_for_sw_if_index(
1079 proto, sw_if_index),
1082 return ((NULL != fib_table ? fib_table->ft_table_id : ~0));
1086 fib_table_get_table_id (u32 fib_index,
1087 fib_protocol_t proto)
1089 fib_table_t *fib_table;
1091 fib_table = fib_table_get(fib_index, proto);
1093 return ((NULL != fib_table ? fib_table->ft_table_id : ~0));
1097 fib_table_find (fib_protocol_t proto,
1102 case FIB_PROTOCOL_IP4:
1103 return (ip4_fib_index_from_table_id(table_id));
1104 case FIB_PROTOCOL_IP6:
1105 return (ip6_fib_index_from_table_id(table_id));
1106 case FIB_PROTOCOL_MPLS:
1107 return (mpls_fib_index_from_table_id(table_id));
1113 fib_table_find_or_create_and_lock_i (fib_protocol_t proto,
1118 fib_table_t *fib_table;
1119 fib_node_index_t fi;
1123 case FIB_PROTOCOL_IP4:
1124 fi = ip4_fib_table_find_or_create_and_lock(table_id, src);
1126 case FIB_PROTOCOL_IP6:
1127 fi = ip6_fib_table_find_or_create_and_lock(table_id, src);
1129 case FIB_PROTOCOL_MPLS:
1130 fi = mpls_fib_table_find_or_create_and_lock(table_id, src);
1136 fib_table = fib_table_get(fi, proto);
1138 if (NULL == fib_table->ft_desc)
1140 if (name && name[0])
1142 fib_table->ft_desc = format(NULL, "%s", name);
1146 fib_table->ft_desc = format(NULL, "%U-VRF:%d",
1147 format_fib_protocol, proto,
1156 fib_table_find_or_create_and_lock (fib_protocol_t proto,
1160 return (fib_table_find_or_create_and_lock_i(proto, table_id,
1165 fib_table_find_or_create_and_lock_w_name (fib_protocol_t proto,
1170 return (fib_table_find_or_create_and_lock_i(proto, table_id,
1175 fib_table_create_and_lock (fib_protocol_t proto,
1177 const char *const fmt,
1180 fib_table_t *fib_table;
1181 fib_node_index_t fi;
1187 case FIB_PROTOCOL_IP4:
1188 fi = ip4_fib_table_create_and_lock(src);
1190 case FIB_PROTOCOL_IP6:
1191 fi = ip6_fib_table_create_and_lock(src, FIB_TABLE_FLAG_NONE, NULL);
1193 case FIB_PROTOCOL_MPLS:
1194 fi = mpls_fib_table_create_and_lock(src);
1200 fib_table = fib_table_get(fi, proto);
1204 fib_table->ft_desc = va_format(fib_table->ft_desc, fmt, &ap);
1211 fib_table_destroy (fib_table_t *fib_table)
1213 vec_free(fib_table->ft_desc);
1215 switch (fib_table->ft_proto)
1217 case FIB_PROTOCOL_IP4:
1218 ip4_fib_table_destroy(fib_table->ft_index);
1220 case FIB_PROTOCOL_IP6:
1221 ip6_fib_table_destroy(fib_table->ft_index);
1223 case FIB_PROTOCOL_MPLS:
1224 mpls_fib_table_destroy(fib_table->ft_index);
1230 fib_table_walk (u32 fib_index,
1231 fib_protocol_t proto,
1232 fib_table_walk_fn_t fn,
1237 case FIB_PROTOCOL_IP4:
1238 ip4_fib_table_walk(ip4_fib_get(fib_index), fn, ctx);
1240 case FIB_PROTOCOL_IP6:
1241 ip6_fib_table_walk(fib_index, fn, ctx);
1243 case FIB_PROTOCOL_MPLS:
1244 mpls_fib_table_walk(mpls_fib_get(fib_index), fn, ctx);
1250 fib_table_sub_tree_walk (u32 fib_index,
1251 fib_protocol_t proto,
1252 const fib_prefix_t *root,
1253 fib_table_walk_fn_t fn,
1258 case FIB_PROTOCOL_IP4:
1259 ip4_fib_table_sub_tree_walk(ip4_fib_get(fib_index), root, fn, ctx);
1261 case FIB_PROTOCOL_IP6:
1262 ip6_fib_table_sub_tree_walk(fib_index, root, fn, ctx);
1264 case FIB_PROTOCOL_MPLS:
1270 fib_table_lock_dec (fib_table_t *fib_table,
1271 fib_source_t source)
1273 vec_validate(fib_table->ft_locks, source);
1275 fib_table->ft_locks[source]--;
1276 fib_table->ft_total_locks--;
1280 fib_table_lock_inc (fib_table_t *fib_table,
1281 fib_source_t source)
1283 vec_validate(fib_table->ft_locks, source);
1285 ASSERT(fib_table->ft_total_locks < (0xffffffff - 1));
1286 fib_table->ft_locks[source]++;
1287 fib_table->ft_total_locks++;
1291 fib_table_unlock (u32 fib_index,
1292 fib_protocol_t proto,
1293 fib_source_t source)
1295 fib_table_t *fib_table;
1297 fib_table = fib_table_get(fib_index, proto);
1298 fib_table_lock_dec(fib_table, source);
1300 if (0 == fib_table->ft_total_locks)
1303 * no more locak from any source - kill it
1305 fib_table_destroy(fib_table);
1310 fib_table_lock (u32 fib_index,
1311 fib_protocol_t proto,
1312 fib_source_t source)
1314 fib_table_t *fib_table;
1316 fib_table = fib_table_get(fib_index, proto);
1318 fib_table_lock_inc(fib_table, source);
1322 fib_table_get_num_entries (u32 fib_index,
1323 fib_protocol_t proto,
1324 fib_source_t source)
1326 fib_table_t *fib_table;
1328 fib_table = fib_table_get(fib_index, proto);
1330 return (fib_table->ft_src_route_counts[source]);
1334 format_fib_table_name (u8* s, va_list* ap)
1336 fib_node_index_t fib_index = va_arg(*ap, fib_node_index_t);
1337 fib_protocol_t proto = va_arg(*ap, int); // int promotion
1338 fib_table_t *fib_table;
1340 fib_table = fib_table_get(fib_index, proto);
1342 s = format(s, "%v", fib_table->ft_desc);
1348 format_fib_table_flags (u8 *s, va_list *args)
1350 fib_table_flags_t flags = va_arg(*args, int);
1351 fib_table_attribute_t attr;
1355 return format(s, "none");
1358 FOR_EACH_FIB_TABLE_ATTRIBUTE(attr) {
1359 if (1 << attr & flags) {
1360 s = format(s, "%s", fib_table_flags_strings[attr]);
1368 * @brief Table flush context. Store the indicies of matching FIB entries
1369 * that need to be removed.
1371 typedef struct fib_table_flush_ctx_t_
1374 * The list of entries to flush
1376 fib_node_index_t *ftf_entries;
1379 * The source we are flushing
1381 fib_source_t ftf_source;
1382 } fib_table_flush_ctx_t;
1384 static fib_table_walk_rc_t
1385 fib_table_flush_cb (fib_node_index_t fib_entry_index,
1388 fib_table_flush_ctx_t *ctx = arg;
1390 if (fib_entry_is_sourced(fib_entry_index, ctx->ftf_source))
1392 vec_add1(ctx->ftf_entries, fib_entry_index);
1394 return (FIB_TABLE_WALK_CONTINUE);
1398 fib_table_flush (u32 fib_index,
1399 fib_protocol_t proto,
1400 fib_source_t source)
1402 fib_node_index_t *fib_entry_index;
1403 fib_table_flush_ctx_t ctx = {
1404 .ftf_entries = NULL,
1405 .ftf_source = source,
1408 fib_table_walk(fib_index, proto,
1412 vec_foreach(fib_entry_index, ctx.ftf_entries)
1414 fib_table_entry_delete_index(*fib_entry_index, source);
1417 vec_free(ctx.ftf_entries);
1420 static fib_table_walk_rc_t
1421 fib_table_mark_cb (fib_node_index_t fib_entry_index,
1424 fib_table_flush_ctx_t *ctx = arg;
1426 if (fib_entry_is_sourced(fib_entry_index, ctx->ftf_source))
1428 fib_entry_mark(fib_entry_index, ctx->ftf_source);
1430 return (FIB_TABLE_WALK_CONTINUE);
1434 fib_table_mark (u32 fib_index,
1435 fib_protocol_t proto,
1436 fib_source_t source)
1438 fib_table_flush_ctx_t ctx = {
1439 .ftf_source = source,
1441 fib_table_t *fib_table;
1443 fib_table = fib_table_get(fib_index, proto);
1445 fib_table->ft_epoch++;
1446 fib_table->ft_flags |= FIB_TABLE_FLAG_RESYNC;
1448 fib_table_walk(fib_index, proto,
1453 static fib_table_walk_rc_t
1454 fib_table_sweep_cb (fib_node_index_t fib_entry_index,
1457 fib_table_flush_ctx_t *ctx = arg;
1459 if (fib_entry_is_marked(fib_entry_index, ctx->ftf_source))
1461 vec_add1(ctx->ftf_entries, fib_entry_index);
1463 return (FIB_TABLE_WALK_CONTINUE);
1467 fib_table_sweep (u32 fib_index,
1468 fib_protocol_t proto,
1469 fib_source_t source)
1471 fib_table_flush_ctx_t ctx = {
1472 .ftf_source = source,
1474 fib_node_index_t *fib_entry_index;
1475 fib_table_t *fib_table;
1477 fib_table = fib_table_get(fib_index, proto);
1479 fib_table->ft_flags &= ~FIB_TABLE_FLAG_RESYNC;
1481 fib_table_walk(fib_index, proto,
1485 vec_foreach(fib_entry_index, ctx.ftf_entries)
1487 fib_table_entry_delete_index(*fib_entry_index, source);
1490 vec_free(ctx.ftf_entries);
1494 format_fib_table_memory (u8 *s, va_list *args)
1496 s = format(s, "%U", format_ip4_fib_table_memory);
1497 s = format(s, "%U", format_ip6_fib_table_memory);
1498 s = format(s, "%U", format_mpls_fib_table_memory);