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 <vnet/fib/fib_table.h>
17 #include <vnet/fib/fib_entry.h>
18 #include <vnet/fib/ip4_fib.h>
21 * A table of prefixes to be added to tables and the sources for them
23 typedef struct ip4_fib_table_special_prefix_t_ {
24 fib_prefix_t ift_prefix;
25 fib_source_t ift_source;
26 fib_entry_flag_t ift_flag;
27 } ip4_fib_table_special_prefix_t;
29 static const ip4_fib_table_special_prefix_t ip4_specials[] = {
37 .fp_proto = FIB_PROTOCOL_IP4,
39 .ift_source = FIB_SOURCE_DEFAULT_ROUTE,
40 .ift_flag = FIB_ENTRY_FLAG_DROP,
49 .fp_proto = FIB_PROTOCOL_IP4,
51 .ift_source = FIB_SOURCE_DEFAULT_ROUTE,
52 .ift_flag = FIB_ENTRY_FLAG_DROP,
61 .ip4.data_u32 = 0xf0000000,
64 .fp_proto = FIB_PROTOCOL_IP4,
66 .ift_source = FIB_SOURCE_SPECIAL,
67 .ift_flag = FIB_ENTRY_FLAG_DROP,
77 .ip4.data_u32 = 0xe0000000,
80 .fp_proto = FIB_PROTOCOL_IP4,
82 .ift_source = FIB_SOURCE_SPECIAL,
83 .ift_flag = FIB_ENTRY_FLAG_DROP,
88 * drop, but we'll allow it to be usurped by the likes of DHCP
92 .ip4.data_u32 = 0xffffffff,
95 .fp_proto = FIB_PROTOCOL_IP4,
97 .ift_source = FIB_SOURCE_DEFAULT_ROUTE,
98 .ift_flag = FIB_ENTRY_FLAG_DROP,
104 ip4_create_fib_with_table_id (u32 table_id,
107 fib_table_t *fib_table;
111 pool_get(ip4_main.fibs, fib_table);
112 clib_memset(fib_table, 0, sizeof(*fib_table));
114 old_heap = clib_mem_set_heap (ip4_main.mtrie_mheap);
115 pool_get_aligned(ip4_main.v4_fibs, v4_fib, CLIB_CACHE_LINE_BYTES);
116 clib_mem_set_heap (old_heap);
118 ASSERT((fib_table - ip4_main.fibs) ==
119 (v4_fib - ip4_main.v4_fibs));
121 fib_table->ft_proto = FIB_PROTOCOL_IP4;
122 fib_table->ft_index =
124 (fib_table - ip4_main.fibs);
126 hash_set (ip4_main.fib_index_by_table_id, table_id, fib_table->ft_index);
128 fib_table->ft_table_id =
131 fib_table->ft_flow_hash_config = IP_FLOW_HASH_DEFAULT;
133 fib_table_lock(fib_table->ft_index, FIB_PROTOCOL_IP4, src);
135 ip4_mtrie_init(&v4_fib->mtrie);
138 * add the special entries into the new FIB
142 for (ii = 0; ii < ARRAY_LEN(ip4_specials); ii++)
144 fib_prefix_t prefix = ip4_specials[ii].ift_prefix;
146 prefix.fp_addr.ip4.data_u32 =
147 clib_host_to_net_u32(prefix.fp_addr.ip4.data_u32);
149 fib_table_entry_special_add(fib_table->ft_index,
151 ip4_specials[ii].ift_source,
152 ip4_specials[ii].ift_flag);
155 return (fib_table->ft_index);
159 ip4_fib_table_destroy (u32 fib_index)
161 fib_table_t *fib_table = pool_elt_at_index(ip4_main.fibs, fib_index);
162 ip4_fib_t *v4_fib = pool_elt_at_index(ip4_main.v4_fibs, fib_index);
166 * remove all the specials we added when the table was created.
167 * In reverse order so the default route is last.
169 for (ii = ARRAY_LEN(ip4_specials) - 1; ii >= 0; ii--)
171 fib_prefix_t prefix = ip4_specials[ii].ift_prefix;
173 prefix.fp_addr.ip4.data_u32 =
174 clib_host_to_net_u32(prefix.fp_addr.ip4.data_u32);
176 fib_table_entry_special_remove(fib_table->ft_index,
178 ip4_specials[ii].ift_source);
182 * validate no more routes.
184 ASSERT(0 == fib_table->ft_total_route_counts);
185 FOR_EACH_FIB_SOURCE(ii)
187 ASSERT(0 == fib_table->ft_src_route_counts[ii]);
190 if (~0 != fib_table->ft_table_id)
192 hash_unset (ip4_main.fib_index_by_table_id, fib_table->ft_table_id);
195 ip4_mtrie_free(&v4_fib->mtrie);
197 pool_put(ip4_main.v4_fibs, v4_fib);
198 pool_put(ip4_main.fibs, fib_table);
203 ip4_fib_table_find_or_create_and_lock (u32 table_id,
208 index = ip4_fib_index_from_table_id(table_id);
210 return ip4_create_fib_with_table_id(table_id, src);
212 fib_table_lock(index, FIB_PROTOCOL_IP4, src);
218 ip4_fib_table_create_and_lock (fib_source_t src)
220 return (ip4_create_fib_with_table_id(~0, src));
224 ip4_fib_table_get_index_for_sw_if_index (u32 sw_if_index)
226 if (sw_if_index >= vec_len(ip4_main.fib_index_by_sw_if_index))
229 * This is the case for interfaces that are not yet mapped to
234 return (ip4_main.fib_index_by_sw_if_index[sw_if_index]);
238 * ip4_fib_table_lookup_exact_match
240 * Exact match prefix lookup
243 ip4_fib_table_lookup_exact_match (const ip4_fib_t *fib,
244 const ip4_address_t *addr,
247 uword * hash, * result;
250 hash = fib->fib_entry_by_dst_address[len];
251 key = (addr->data_u32 & ip4_main.fib_masks[len]);
253 result = hash_get(hash, key);
255 if (NULL != result) {
258 return (FIB_NODE_INDEX_INVALID);
262 * ip4_fib_table_lookup_adj
264 * Longest prefix match
267 ip4_fib_table_lookup_lb (ip4_fib_t *fib,
268 const ip4_address_t *addr)
270 fib_node_index_t fei;
272 fei = ip4_fib_table_lookup(fib, addr, 32);
274 if (FIB_NODE_INDEX_INVALID != fei)
278 dpo = fib_entry_contribute_ip_forwarding(fei);
280 return (dpo->dpoi_index);
282 return (INDEX_INVALID);
286 * ip4_fib_table_lookup
288 * Longest prefix match
291 ip4_fib_table_lookup (const ip4_fib_t *fib,
292 const ip4_address_t *addr,
295 uword * hash, * result;
299 for (mask_len = len; mask_len >= 0; mask_len--)
301 hash = fib->fib_entry_by_dst_address[mask_len];
302 key = (addr->data_u32 & ip4_main.fib_masks[mask_len]);
304 result = hash_get (hash, key);
306 if (NULL != result) {
310 return (FIB_NODE_INDEX_INVALID);
314 ip4_fib_table_entry_insert (ip4_fib_t *fib,
315 const ip4_address_t *addr,
317 fib_node_index_t fib_entry_index)
319 uword * hash, * result;
322 key = (addr->data_u32 & ip4_main.fib_masks[len]);
323 hash = fib->fib_entry_by_dst_address[len];
324 result = hash_get (hash, key);
326 if (NULL == result) {
331 old_heap = clib_mem_set_heap (ip4_main.mtrie_mheap);
334 hash = hash_create (32 /* elts */, sizeof (uword));
335 hash_set_flags (hash, HASH_FLAG_NO_AUTO_SHRINK);
338 hash = hash_set(hash, key, fib_entry_index);
339 fib->fib_entry_by_dst_address[len] = hash;
340 clib_mem_set_heap (old_heap);
349 ip4_fib_table_entry_remove (ip4_fib_t *fib,
350 const ip4_address_t *addr,
353 uword * hash, * result;
356 key = (addr->data_u32 & ip4_main.fib_masks[len]);
357 hash = fib->fib_entry_by_dst_address[len];
358 result = hash_get (hash, key);
363 * removing a non-existent entry. i'll allow it.
370 old_heap = clib_mem_set_heap (ip4_main.mtrie_mheap);
371 hash_unset(hash, key);
372 clib_mem_set_heap (old_heap);
375 fib->fib_entry_by_dst_address[len] = hash;
379 ip4_fib_table_fwding_dpo_update (ip4_fib_t *fib,
380 const ip4_address_t *addr,
384 ip4_fib_mtrie_route_add(&fib->mtrie, addr, len, dpo->dpoi_index);
388 ip4_fib_table_fwding_dpo_remove (ip4_fib_t *fib,
389 const ip4_address_t *addr,
394 const fib_prefix_t *cover_prefix;
395 const dpo_id_t *cover_dpo;
398 * We need to pass the MTRIE the LB index and address length of the
399 * covering prefix, so it can fill the plys with the correct replacement
400 * for the entry being removed
402 cover_prefix = fib_entry_get_prefix(cover_index);
403 cover_dpo = fib_entry_contribute_ip_forwarding(cover_index);
405 ip4_fib_mtrie_route_del(&fib->mtrie,
406 addr, len, dpo->dpoi_index,
407 cover_prefix->fp_len,
408 cover_dpo->dpoi_index);
412 ip4_fib_table_walk (ip4_fib_t *fib,
413 fib_table_walk_fn_t fn,
416 fib_prefix_t root = {
417 .fp_proto = FIB_PROTOCOL_IP4,
418 // address and length default to all 0
422 * A full tree walk is the dengenerate case of a sub-tree from
425 return (ip4_fib_table_sub_tree_walk(fib, &root, fn, ctx));
429 ip4_fib_table_sub_tree_walk (ip4_fib_t *fib,
430 const fib_prefix_t *root,
431 fib_table_walk_fn_t fn,
434 fib_prefix_t *sub_trees = NULL;
438 * There is no efficient way to walk this array of hash tables.
439 * so we walk each table with a mask length greater than and equal to
440 * the required root and check it is covered by the root.
442 for (i = root->fp_len;
443 i < ARRAY_LEN (fib->fib_entry_by_dst_address);
446 uword * hash = fib->fib_entry_by_dst_address[i];
453 hash_foreach_pair (p, hash,
456 if (ip4_destination_matches_route(&ip4_main,
461 const fib_prefix_t *sub_tree;
465 * exclude sub-trees the walk does not want to explore
467 vec_foreach(sub_tree, sub_trees)
469 if (ip4_destination_matches_route(&ip4_main,
471 &sub_tree->fp_addr.ip4,
481 switch (fn(p->value[0], ctx))
483 case FIB_TABLE_WALK_CONTINUE:
485 case FIB_TABLE_WALK_SUB_TREE_STOP: {
487 .fp_proto = FIB_PROTOCOL_IP4,
491 vec_add1(sub_trees, pfx);
494 case FIB_TABLE_WALK_STOP:
510 typedef struct ip4_fib_show_walk_ctx_t_
512 fib_node_index_t *ifsw_indicies;
513 } ip4_fib_show_walk_ctx_t;
515 static fib_table_walk_rc_t
516 ip4_fib_show_walk_cb (fib_node_index_t fib_entry_index,
519 ip4_fib_show_walk_ctx_t *ctx = arg;
521 vec_add1(ctx->ifsw_indicies, fib_entry_index);
523 return (FIB_TABLE_WALK_CONTINUE);
527 ip4_fib_table_show_all (ip4_fib_t *fib,
530 ip4_fib_show_walk_ctx_t ctx = {
531 .ifsw_indicies = NULL,
533 fib_node_index_t *fib_entry_index;
535 ip4_fib_table_walk(fib, ip4_fib_show_walk_cb, &ctx);
536 vec_sort_with_function(ctx.ifsw_indicies,
537 fib_entry_cmp_for_sort);
539 vec_foreach(fib_entry_index, ctx.ifsw_indicies)
541 vlib_cli_output(vm, "%U",
544 FIB_ENTRY_FORMAT_BRIEF);
547 vec_free(ctx.ifsw_indicies);
551 ip4_fib_table_show_one (ip4_fib_t *fib,
553 ip4_address_t *address,
557 vlib_cli_output(vm, "%U",
559 ip4_fib_table_lookup(fib, address, mask_len),
561 FIB_ENTRY_FORMAT_DETAIL2 :
562 FIB_ENTRY_FORMAT_DETAIL));
566 format_ip4_fib_table_memory (u8 * s, va_list * args)
568 #if USE_DLMALLOC == 0
569 s = format(s, "%=30s %=6d %=12ld\n",
571 pool_elts(ip4_main.fibs),
572 mheap_bytes(ip4_main.mtrie_mheap));
574 s = format(s, "%=30s %=6d %=12ld\n",
576 pool_elts(ip4_main.fibs),
577 mspace_footprint(ip4_main.mtrie_mheap));
584 static clib_error_t *
585 ip4_show_fib (vlib_main_t * vm,
586 unformat_input_t * input,
587 vlib_cli_command_t * cmd)
589 ip4_main_t * im4 = &ip4_main;
590 fib_table_t * fib_table;
591 u64 total_mtrie_memory, total_hash_memory;
592 int verbose, matching, mtrie, memory;
593 ip4_address_t matching_address;
594 u32 matching_mask = 32;
595 int i, table_id = -1, fib_index = ~0;
599 matching = mtrie = memory = 0;
600 total_hash_memory = total_mtrie_memory = 0;
602 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
604 if (unformat (input, "brief") || unformat (input, "summary")
605 || unformat (input, "sum"))
608 else if (unformat (input, "detail") || unformat (input, "det"))
611 else if (unformat (input, "mtrie"))
614 else if (unformat (input, "mem") ||
615 unformat (input, "memory"))
618 else if (unformat (input, "%U/%d",
619 unformat_ip4_address, &matching_address, &matching_mask))
622 else if (unformat (input, "%U", unformat_ip4_address, &matching_address))
625 else if (unformat (input, "table %d", &table_id))
627 else if (unformat (input, "index %d", &fib_index))
633 pool_foreach (fib_table, im4->fibs,
635 ip4_fib_t *fib = pool_elt_at_index(im4->v4_fibs, fib_table->ft_index);
639 if (table_id >= 0 && table_id != (int)fib->table_id)
641 if (fib_index != ~0 && fib_index != (int)fib->index)
646 uword mtrie_size, hash_size, *old_heap;
649 mtrie_size = ip4_fib_mtrie_memory_usage(&fib->mtrie);
652 old_heap = clib_mem_set_heap (ip4_main.mtrie_mheap);
653 for (i = 0; i < ARRAY_LEN (fib->fib_entry_by_dst_address); i++)
655 uword * hash = fib->fib_entry_by_dst_address[i];
658 hash_size += hash_bytes(hash);
661 clib_mem_set_heap (old_heap);
664 vlib_cli_output (vm, "%U mtrie:%d hash:%d",
665 format_fib_table_name, fib->index,
669 total_mtrie_memory += mtrie_size;
670 total_hash_memory += hash_size;
674 s = format(s, "%U, fib_index:%d, flow hash:[%U] locks:[",
675 format_fib_table_name, fib->index,
678 format_ip_flow_hash_config,
679 fib_table->ft_flow_hash_config);
680 FOR_EACH_FIB_SOURCE(source)
682 if (0 != fib_table->ft_locks[source])
684 s = format(s, "%U:%d, ",
685 format_fib_source, source,
686 fib_table->ft_locks[source]);
690 vlib_cli_output (vm, "%v", s);
696 vlib_cli_output (vm, "%U", format_ip4_fib_mtrie, &fib->mtrie, verbose);
701 vlib_cli_output (vm, "%=20s%=16s", "Prefix length", "Count");
702 for (i = 0; i < ARRAY_LEN (fib->fib_entry_by_dst_address); i++)
704 uword * hash = fib->fib_entry_by_dst_address[i];
705 uword n_elts = hash_elts (hash);
707 vlib_cli_output (vm, "%20d%16d", i, n_elts);
714 ip4_fib_table_show_all(fib, vm);
718 ip4_fib_table_show_one(fib, vm, &matching_address,
719 matching_mask, detail);
725 vlib_cli_output (vm, "totals: mtrie:%ld hash:%ld all:%ld",
728 total_mtrie_memory + total_hash_memory);
729 vlib_cli_output (vm, "\nMtrie Mheap Usage: %U\n",
730 format_mheap, ip4_main.mtrie_mheap, 1);
736 * This command displays the IPv4 FIB Tables (VRF Tables) and the route
737 * entries for each table.
739 * @note This command will run for a long time when the FIB tables are
740 * comprised of millions of entries. For those senarios, consider displaying
741 * a single table or summary mode.
744 * Example of how to display all the IPv4 FIB tables:
745 * @cliexstart{show ip fib}
746 * ipv4-VRF:0, fib_index 0, flow hash: src dst sport dport proto
749 * [@0]: dpo-load-balance: [index:0 buckets:1 uRPF:0 to:[0:0]]
750 * [0] [@0]: dpo-drop ip6
753 * [@0]: dpo-load-balance: [index:1 buckets:1 uRPF:1 to:[0:0]]
754 * [0] [@0]: dpo-drop ip6
757 * [@0]: dpo-load-balance: [index:30 buckets:1 uRPF:29 to:[0:0]]
758 * [0] [@3]: arp-ipv4: via 6.0.0.1 af_packet0
761 * [@0]: dpo-load-balance: [index:31 buckets:4 uRPF:30 to:[0:0]]
762 * [0] [@3]: arp-ipv4: via 6.0.0.2 af_packet0
763 * [1] [@3]: arp-ipv4: via 6.0.0.2 af_packet0
764 * [2] [@3]: arp-ipv4: via 6.0.0.2 af_packet0
765 * [3] [@3]: arp-ipv4: via 6.0.0.1 af_packet0
768 * [@0]: dpo-load-balance: [index:3 buckets:1 uRPF:3 to:[0:0]]
769 * [0] [@0]: dpo-drop ip6
772 * [@0]: dpo-load-balance: [index:2 buckets:1 uRPF:2 to:[0:0]]
773 * [0] [@0]: dpo-drop ip6
776 * [@0]: dpo-load-balance: [index:4 buckets:1 uRPF:4 to:[0:0]]
777 * [0] [@0]: dpo-drop ip6
778 * ipv4-VRF:7, fib_index 1, flow hash: src dst sport dport proto
781 * [@0]: dpo-load-balance: [index:12 buckets:1 uRPF:11 to:[0:0]]
782 * [0] [@0]: dpo-drop ip6
785 * [@0]: dpo-load-balance: [index:13 buckets:1 uRPF:12 to:[0:0]]
786 * [0] [@0]: dpo-drop ip6
789 * [@0]: dpo-load-balance: [index:17 buckets:1 uRPF:16 to:[0:0]]
790 * [0] [@4]: ipv4-glean: af_packet0
793 * [@0]: dpo-load-balance: [index:18 buckets:1 uRPF:17 to:[1:84]]
794 * [0] [@2]: dpo-receive: 172.16.1.1 on af_packet0
797 * [@0]: dpo-load-balance: [index:21 buckets:1 uRPF:20 to:[0:0]]
798 * [0] [@5]: ipv4 via 172.16.1.2 af_packet0: IP4: 02:fe:9e:70:7a:2b -> 26:a5:f6:9c:3a:36
801 * [@0]: dpo-load-balance: [index:19 buckets:1 uRPF:18 to:[0:0]]
802 * [0] [@4]: ipv4-glean: af_packet1
805 * [@0]: dpo-load-balance: [index:20 buckets:1 uRPF:19 to:[0:0]]
806 * [0] [@2]: dpo-receive: 172.16.2.1 on af_packet1
809 * [@0]: dpo-load-balance: [index:15 buckets:1 uRPF:14 to:[0:0]]
810 * [0] [@0]: dpo-drop ip6
813 * [@0]: dpo-load-balance: [index:14 buckets:1 uRPF:13 to:[0:0]]
814 * [0] [@0]: dpo-drop ip6
817 * [@0]: dpo-load-balance: [index:16 buckets:1 uRPF:15 to:[0:0]]
818 * [0] [@0]: dpo-drop ip6
820 * Example of how to display a single IPv4 FIB table:
821 * @cliexstart{show ip fib table 7}
822 * ipv4-VRF:7, fib_index 1, flow hash: src dst sport dport proto
825 * [@0]: dpo-load-balance: [index:12 buckets:1 uRPF:11 to:[0:0]]
826 * [0] [@0]: dpo-drop ip6
829 * [@0]: dpo-load-balance: [index:13 buckets:1 uRPF:12 to:[0:0]]
830 * [0] [@0]: dpo-drop ip6
833 * [@0]: dpo-load-balance: [index:17 buckets:1 uRPF:16 to:[0:0]]
834 * [0] [@4]: ipv4-glean: af_packet0
837 * [@0]: dpo-load-balance: [index:18 buckets:1 uRPF:17 to:[1:84]]
838 * [0] [@2]: dpo-receive: 172.16.1.1 on af_packet0
841 * [@0]: dpo-load-balance: [index:21 buckets:1 uRPF:20 to:[0:0]]
842 * [0] [@5]: ipv4 via 172.16.1.2 af_packet0: IP4: 02:fe:9e:70:7a:2b -> 26:a5:f6:9c:3a:36
845 * [@0]: dpo-load-balance: [index:19 buckets:1 uRPF:18 to:[0:0]]
846 * [0] [@4]: ipv4-glean: af_packet1
849 * [@0]: dpo-load-balance: [index:20 buckets:1 uRPF:19 to:[0:0]]
850 * [0] [@2]: dpo-receive: 172.16.2.1 on af_packet1
853 * [@0]: dpo-load-balance: [index:15 buckets:1 uRPF:14 to:[0:0]]
854 * [0] [@0]: dpo-drop ip6
857 * [@0]: dpo-load-balance: [index:14 buckets:1 uRPF:13 to:[0:0]]
858 * [0] [@0]: dpo-drop ip6
861 * [@0]: dpo-load-balance: [index:16 buckets:1 uRPF:15 to:[0:0]]
862 * [0] [@0]: dpo-drop ip6
864 * Example of how to display a summary of all IPv4 FIB tables:
865 * @cliexstart{show ip fib summary}
866 * ipv4-VRF:0, fib_index 0, flow hash: src dst sport dport proto
867 * Prefix length Count
871 * ipv4-VRF:7, fib_index 1, flow hash: src dst sport dport proto
872 * Prefix length Count
880 VLIB_CLI_COMMAND (ip4_show_fib_command, static) = {
881 .path = "show ip fib",
882 .short_help = "show ip fib [summary] [table <table-id>] [index <fib-id>] [<ip4-addr>[/<mask>]] [mtrie] [detail]",
883 .function = ip4_show_fib,