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/ip6_fib.h>
17 #include <vnet/fib/fib_table.h>
18 #include <vnet/dpo/ip6_ll_dpo.h>
21 vnet_ip6_fib_init (u32 fib_index)
24 .fp_proto = FIB_PROTOCOL_IP6,
34 * Add the default route.
36 fib_table_entry_special_add(fib_index,
38 FIB_SOURCE_DEFAULT_ROUTE,
42 * all link local via the link local lookup DPO
44 pfx.fp_addr.ip6.as_u64[0] = clib_host_to_net_u64 (0xFE80000000000000ULL);
45 pfx.fp_addr.ip6.as_u64[1] = 0;
47 fib_table_entry_special_dpo_add(fib_index,
55 create_fib_with_table_id (u32 table_id,
57 fib_table_flags_t flags,
60 fib_table_t *fib_table;
63 pool_get(ip6_main.fibs, fib_table);
64 pool_get_aligned(ip6_main.v6_fibs, v6_fib, CLIB_CACHE_LINE_BYTES);
66 memset(fib_table, 0, sizeof(*fib_table));
67 memset(v6_fib, 0, sizeof(*v6_fib));
69 ASSERT((fib_table - ip6_main.fibs) ==
70 (v6_fib - ip6_main.v6_fibs));
72 fib_table->ft_proto = FIB_PROTOCOL_IP6;
75 (fib_table - ip6_main.fibs);
77 hash_set(ip6_main.fib_index_by_table_id, table_id, fib_table->ft_index);
79 fib_table->ft_table_id =
82 fib_table->ft_flow_hash_config = IP_FLOW_HASH_DEFAULT;
83 fib_table->ft_flags = flags;
84 fib_table->ft_desc = desc;
86 vnet_ip6_fib_init(fib_table->ft_index);
87 fib_table_lock(fib_table->ft_index, FIB_PROTOCOL_IP6, src);
89 return (fib_table->ft_index);
93 ip6_fib_table_find_or_create_and_lock (u32 table_id,
98 p = hash_get (ip6_main.fib_index_by_table_id, table_id);
100 return create_fib_with_table_id(table_id, src,
104 fib_table_lock(p[0], FIB_PROTOCOL_IP6, src);
110 ip6_fib_table_create_and_lock (fib_source_t src,
111 fib_table_flags_t flags,
114 return (create_fib_with_table_id(~0, src, flags, desc));
118 ip6_fib_table_destroy (u32 fib_index)
121 * all link local first ...
124 .fp_proto = FIB_PROTOCOL_IP6,
135 fib_table_entry_delete(fib_index,
140 * ... then the default route.
142 pfx.fp_addr.ip6.as_u64[0] = 0;
144 fib_table_entry_special_remove(fib_index,
146 FIB_SOURCE_DEFAULT_ROUTE);
148 fib_table_t *fib_table = fib_table_get(fib_index, FIB_PROTOCOL_IP6);
152 * validate no more routes.
154 ASSERT(0 == fib_table->ft_total_route_counts);
155 FOR_EACH_FIB_SOURCE(source)
157 ASSERT(0 == fib_table->ft_src_route_counts[source]);
160 if (~0 != fib_table->ft_table_id)
162 hash_unset (ip6_main.fib_index_by_table_id, fib_table->ft_table_id);
164 pool_put_index(ip6_main.v6_fibs, fib_table->ft_index);
165 pool_put(ip6_main.fibs, fib_table);
169 ip6_fib_table_lookup (u32 fib_index,
170 const ip6_address_t *addr,
173 ip6_fib_table_instance_t *table;
174 BVT(clib_bihash_kv) kv, value;
178 table = &ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING];
179 n_p = vec_len (table->prefix_lengths_in_search_order);
181 kv.key[0] = addr->as_u64[0];
182 kv.key[1] = addr->as_u64[1];
183 fib = ((u64)((fib_index))<<32);
186 * start search from a mask length same length or shorter.
187 * we don't want matches longer than the mask passed
190 while (i < n_p && table->prefix_lengths_in_search_order[i] > len)
197 int dst_address_length = table->prefix_lengths_in_search_order[i];
198 ip6_address_t * mask = &ip6_main.fib_masks[dst_address_length];
200 ASSERT(dst_address_length >= 0 && dst_address_length <= 128);
201 //As lengths are decreasing, masks are increasingly specific.
202 kv.key[0] &= mask->as_u64[0];
203 kv.key[1] &= mask->as_u64[1];
204 kv.key[2] = fib | dst_address_length;
206 rv = BV(clib_bihash_search_inline_2)(&table->ip6_hash, &kv, &value);
211 return (FIB_NODE_INDEX_INVALID);
215 ip6_fib_table_lookup_exact_match (u32 fib_index,
216 const ip6_address_t *addr,
219 ip6_fib_table_instance_t *table;
220 BVT(clib_bihash_kv) kv, value;
225 table = &ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING];
226 mask = &ip6_main.fib_masks[len];
227 fib = ((u64)((fib_index))<<32);
229 kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
230 kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
231 kv.key[2] = fib | len;
233 rv = BV(clib_bihash_search_inline_2)(&table->ip6_hash, &kv, &value);
237 return (FIB_NODE_INDEX_INVALID);
241 compute_prefix_lengths_in_search_order (ip6_fib_table_instance_t *table)
244 vec_reset_length (table->prefix_lengths_in_search_order);
245 /* Note: bitmap reversed so this is in fact a longest prefix match */
246 clib_bitmap_foreach (i, table->non_empty_dst_address_length_bitmap,
248 int dst_address_length = 128 - i;
249 vec_add1(table->prefix_lengths_in_search_order, dst_address_length);
254 ip6_fib_table_entry_remove (u32 fib_index,
255 const ip6_address_t *addr,
258 ip6_fib_table_instance_t *table;
259 BVT(clib_bihash_kv) kv;
263 table = &ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING];
264 mask = &ip6_main.fib_masks[len];
265 fib = ((u64)((fib_index))<<32);
267 kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
268 kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
269 kv.key[2] = fib | len;
271 BV(clib_bihash_add_del)(&table->ip6_hash, &kv, 0);
273 /* refcount accounting */
274 ASSERT (table->dst_address_length_refcounts[len] > 0);
275 if (--table->dst_address_length_refcounts[len] == 0)
277 table->non_empty_dst_address_length_bitmap =
278 clib_bitmap_set (table->non_empty_dst_address_length_bitmap,
280 compute_prefix_lengths_in_search_order (table);
285 ip6_fib_table_entry_insert (u32 fib_index,
286 const ip6_address_t *addr,
288 fib_node_index_t fib_entry_index)
290 ip6_fib_table_instance_t *table;
291 BVT(clib_bihash_kv) kv;
295 table = &ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING];
296 mask = &ip6_main.fib_masks[len];
297 fib = ((u64)((fib_index))<<32);
299 kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
300 kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
301 kv.key[2] = fib | len;
302 kv.value = fib_entry_index;
304 BV(clib_bihash_add_del)(&table->ip6_hash, &kv, 1);
306 table->dst_address_length_refcounts[len]++;
308 table->non_empty_dst_address_length_bitmap =
309 clib_bitmap_set (table->non_empty_dst_address_length_bitmap,
311 compute_prefix_lengths_in_search_order (table);
314 u32 ip6_fib_table_fwding_lookup_with_if_index (ip6_main_t * im,
316 const ip6_address_t * dst)
318 u32 fib_index = vec_elt (im->fib_index_by_sw_if_index, sw_if_index);
319 return ip6_fib_table_fwding_lookup(im, fib_index, dst);
323 ip6_fib_table_get_index_for_sw_if_index (u32 sw_if_index)
325 if (sw_if_index >= vec_len(ip6_main.fib_index_by_sw_if_index))
328 * This is the case for interfaces that are not yet mapped to
333 return (ip6_main.fib_index_by_sw_if_index[sw_if_index]);
337 ip6_fib_table_fwding_dpo_update (u32 fib_index,
338 const ip6_address_t *addr,
342 ip6_fib_table_instance_t *table;
343 BVT(clib_bihash_kv) kv;
347 table = &ip6_main.ip6_table[IP6_FIB_TABLE_FWDING];
348 mask = &ip6_main.fib_masks[len];
349 fib = ((u64)((fib_index))<<32);
351 kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
352 kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
353 kv.key[2] = fib | len;
354 kv.value = dpo->dpoi_index;
356 BV(clib_bihash_add_del)(&table->ip6_hash, &kv, 1);
358 table->dst_address_length_refcounts[len]++;
360 table->non_empty_dst_address_length_bitmap =
361 clib_bitmap_set (table->non_empty_dst_address_length_bitmap,
363 compute_prefix_lengths_in_search_order (table);
367 ip6_fib_table_fwding_dpo_remove (u32 fib_index,
368 const ip6_address_t *addr,
372 ip6_fib_table_instance_t *table;
373 BVT(clib_bihash_kv) kv;
377 table = &ip6_main.ip6_table[IP6_FIB_TABLE_FWDING];
378 mask = &ip6_main.fib_masks[len];
379 fib = ((u64)((fib_index))<<32);
381 kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
382 kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
383 kv.key[2] = fib | len;
384 kv.value = dpo->dpoi_index;
386 BV(clib_bihash_add_del)(&table->ip6_hash, &kv, 0);
388 /* refcount accounting */
389 ASSERT (table->dst_address_length_refcounts[len] > 0);
390 if (--table->dst_address_length_refcounts[len] == 0)
392 table->non_empty_dst_address_length_bitmap =
393 clib_bitmap_set (table->non_empty_dst_address_length_bitmap,
395 compute_prefix_lengths_in_search_order (table);
400 * @brief Context when walking the IPv6 table. Since all VRFs are in the
401 * same hash table, we need to filter only those we need as we walk
403 typedef struct ip6_fib_walk_ctx_t_
406 fib_table_walk_fn_t i6w_fn;
408 fib_prefix_t i6w_root;
409 fib_prefix_t *i6w_sub_trees;
410 } ip6_fib_walk_ctx_t;
413 ip6_fib_walk_cb (clib_bihash_kv_24_8_t * kvp,
416 ip6_fib_walk_ctx_t *ctx = arg;
419 if ((kvp->key[2] >> 32) == ctx->i6w_fib_index)
421 key.as_u64[0] = kvp->key[0];
422 key.as_u64[1] = kvp->key[1];
424 if (ip6_destination_matches_route(&ip6_main,
426 &ctx->i6w_root.fp_addr.ip6,
427 ctx->i6w_root.fp_len))
429 const fib_prefix_t *sub_tree;
433 * exclude sub-trees the walk does not want to explore
435 vec_foreach(sub_tree, ctx->i6w_sub_trees)
437 if (ip6_destination_matches_route(&ip6_main,
439 &sub_tree->fp_addr.ip6,
449 switch (ctx->i6w_fn(kvp->value, ctx->i6w_ctx))
451 case FIB_TABLE_WALK_CONTINUE:
453 case FIB_TABLE_WALK_SUB_TREE_STOP: {
455 .fp_proto = FIB_PROTOCOL_IP6,
456 .fp_len = kvp->key[2] & 0xffffffff,
459 vec_add1(ctx->i6w_sub_trees, pfx);
462 case FIB_TABLE_WALK_STOP:
474 ip6_fib_table_walk (u32 fib_index,
475 fib_table_walk_fn_t fn,
478 ip6_fib_walk_ctx_t ctx = {
479 .i6w_fib_index = fib_index,
483 .fp_proto = FIB_PROTOCOL_IP6,
485 .i6w_sub_trees = NULL,
488 BV(clib_bihash_foreach_key_value_pair)(
489 &ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash,
493 vec_free(ctx.i6w_sub_trees);
497 ip6_fib_table_sub_tree_walk (u32 fib_index,
498 const fib_prefix_t *root,
499 fib_table_walk_fn_t fn,
502 ip6_fib_walk_ctx_t ctx = {
503 .i6w_fib_index = fib_index,
509 BV(clib_bihash_foreach_key_value_pair)(
510 &ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash,
515 typedef struct ip6_fib_show_ctx_t_ {
516 fib_node_index_t *entries;
517 } ip6_fib_show_ctx_t;
519 static fib_table_walk_rc_t
520 ip6_fib_table_show_walk (fib_node_index_t fib_entry_index,
523 ip6_fib_show_ctx_t *ctx = arg;
525 vec_add1(ctx->entries, fib_entry_index);
527 return (FIB_TABLE_WALK_CONTINUE);
531 ip6_fib_table_show_all (ip6_fib_t *fib,
534 fib_node_index_t *fib_entry_index;
535 ip6_fib_show_ctx_t ctx = {
539 ip6_fib_table_walk(fib->index, ip6_fib_table_show_walk, &ctx);
540 vec_sort_with_function(ctx.entries, fib_entry_cmp_for_sort);
542 vec_foreach(fib_entry_index, ctx.entries)
544 vlib_cli_output(vm, "%U",
547 FIB_ENTRY_FORMAT_BRIEF);
550 vec_free(ctx.entries);
554 ip6_fib_table_show_one (ip6_fib_t *fib,
556 ip6_address_t *address,
560 vlib_cli_output(vm, "%U",
562 ip6_fib_table_lookup(fib->index, address, mask_len),
564 FIB_ENTRY_FORMAT_DETAIL2:
565 FIB_ENTRY_FORMAT_DETAIL));
569 format_ip6_fib_table_memory (u8 * s, va_list * args)
574 ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash.alloc_arena_next
575 - ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash.alloc_arena;
578 ip6_main.ip6_table[IP6_FIB_TABLE_FWDING].ip6_hash.alloc_arena_next
579 - ip6_main.ip6_table[IP6_FIB_TABLE_FWDING].ip6_hash.alloc_arena;
581 s = format(s, "%=30s %=6d %=8ld\n",
583 pool_elts(ip6_main.fibs),
590 u64 count_by_prefix_length[129];
591 } count_routes_in_fib_at_prefix_length_arg_t;
594 count_routes_in_fib_at_prefix_length (BVT(clib_bihash_kv) * kvp,
597 count_routes_in_fib_at_prefix_length_arg_t * ap = arg;
600 if ((kvp->key[2]>>32) != ap->fib_index)
603 mask_width = kvp->key[2] & 0xFF;
605 ap->count_by_prefix_length[mask_width]++;
608 static clib_error_t *
609 ip6_show_fib (vlib_main_t * vm,
610 unformat_input_t * input,
611 vlib_cli_command_t * cmd)
613 count_routes_in_fib_at_prefix_length_arg_t _ca, *ca = &_ca;
614 ip6_main_t * im6 = &ip6_main;
615 fib_table_t *fib_table;
617 int verbose, matching;
618 ip6_address_t matching_address;
620 int table_id = -1, fib_index = ~0;
626 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
628 if (unformat (input, "brief") ||
629 unformat (input, "summary") ||
630 unformat (input, "sum"))
633 else if (unformat (input, "detail") ||
634 unformat (input, "det"))
637 else if (unformat (input, "%U/%d",
638 unformat_ip6_address, &matching_address, &mask_len))
641 else if (unformat (input, "%U", unformat_ip6_address, &matching_address))
644 else if (unformat (input, "table %d", &table_id))
646 else if (unformat (input, "index %d", &fib_index))
652 pool_foreach (fib_table, im6->fibs,
657 fib = pool_elt_at_index(im6->v6_fibs, fib_table->ft_index);
658 if (table_id >= 0 && table_id != (int)fib->table_id)
660 if (fib_index != ~0 && fib_index != (int)fib->index)
662 if (fib_table->ft_flags & FIB_TABLE_FLAG_IP6_LL)
665 s = format(s, "%U, fib_index:%d, flow hash:[%U] locks:[",
666 format_fib_table_name, fib->index,
669 format_ip_flow_hash_config,
670 fib_table->ft_flow_hash_config);
671 FOR_EACH_FIB_SOURCE(source)
673 if (0 != fib_table->ft_locks[source])
675 s = format(s, "%U:%d, ",
676 format_fib_source, source,
677 fib_table->ft_locks[source]);
681 vlib_cli_output (vm, "%v", s);
687 BVT(clib_bihash) * h = &im6->ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash;
690 vlib_cli_output (vm, "%=20s%=16s", "Prefix length", "Count");
692 memset (ca, 0, sizeof(*ca));
693 ca->fib_index = fib->index;
695 BV(clib_bihash_foreach_key_value_pair)
696 (h, count_routes_in_fib_at_prefix_length, ca);
698 for (len = 128; len >= 0; len--)
700 if (ca->count_by_prefix_length[len])
701 vlib_cli_output (vm, "%=20d%=16lld",
702 len, ca->count_by_prefix_length[len]);
709 ip6_fib_table_show_all(fib, vm);
713 ip6_fib_table_show_one(fib, vm, &matching_address, mask_len, detail);
721 * This command displays the IPv6 FIB Tables (VRF Tables) and the route
722 * entries for each table.
724 * @note This command will run for a long time when the FIB tables are
725 * comprised of millions of entries. For those senarios, consider displaying
730 * Example of how to display all the IPv6 FIB tables:
731 * @cliexstart{show ip6 fib}
732 * ipv6-VRF:0, fib_index 0, flow hash: src dst sport dport proto
735 * [@0]: dpo-load-balance: [index:5 buckets:1 uRPF:5 to:[0:0]]
736 * [0] [@0]: dpo-drop ip6
739 * [@0]: dpo-load-balance: [index:10 buckets:1 uRPF:10 to:[0:0]]
740 * [0] [@2]: dpo-receive
743 * [@0]: dpo-load-balance: [index:8 buckets:1 uRPF:8 to:[0:0]]
744 * [0] [@2]: dpo-receive
747 * [@0]: dpo-load-balance: [index:7 buckets:1 uRPF:7 to:[0:0]]
748 * [0] [@2]: dpo-receive
751 * [@0]: dpo-load-balance: [index:9 buckets:1 uRPF:9 to:[0:0]]
752 * [0] [@2]: dpo-receive
755 * [@0]: dpo-load-balance: [index:6 buckets:1 uRPF:6 to:[0:0]]
756 * [0] [@2]: dpo-receive
757 * ipv6-VRF:8, fib_index 1, flow hash: src dst sport dport proto
760 * [@0]: dpo-load-balance: [index:21 buckets:1 uRPF:20 to:[0:0]]
761 * [0] [@0]: dpo-drop ip6
764 * [@0]: dpo-load-balance: [index:27 buckets:1 uRPF:26 to:[0:0]]
765 * [0] [@4]: ipv6-glean: af_packet0
768 * [@0]: dpo-load-balance: [index:28 buckets:1 uRPF:27 to:[0:0]]
769 * [0] [@2]: dpo-receive: @::a:1:1:0:7 on af_packet0
772 * [@0]: dpo-load-balance: [index:26 buckets:1 uRPF:25 to:[0:0]]
773 * [0] [@2]: dpo-receive
774 * fe80::fe:3eff:fe3e:9222/128
776 * [@0]: dpo-load-balance: [index:29 buckets:1 uRPF:28 to:[0:0]]
777 * [0] [@2]: dpo-receive: fe80::fe:3eff:fe3e:9222 on af_packet0
780 * [@0]: dpo-load-balance: [index:24 buckets:1 uRPF:23 to:[0:0]]
781 * [0] [@2]: dpo-receive
784 * [@0]: dpo-load-balance: [index:23 buckets:1 uRPF:22 to:[0:0]]
785 * [0] [@2]: dpo-receive
788 * [@0]: dpo-load-balance: [index:25 buckets:1 uRPF:24 to:[0:0]]
789 * [0] [@2]: dpo-receive
792 * [@0]: dpo-load-balance: [index:22 buckets:1 uRPF:21 to:[0:0]]
793 * [0] [@2]: dpo-receive
796 * Example of how to display a summary of all IPv6 FIB tables:
797 * @cliexstart{show ip6 fib summary}
798 * ipv6-VRF:0, fib_index 0, flow hash: src dst sport dport proto
799 * Prefix length Count
804 * ipv6-VRF:8, fib_index 1, flow hash: src dst sport dport proto
805 * Prefix length Count
815 VLIB_CLI_COMMAND (ip6_show_fib_command, static) = {
816 .path = "show ip6 fib",
817 .short_help = "show ip6 fib [summary] [table <table-id>] [index <fib-id>] [<ip6-addr>[/<width>]] [detail]",
818 .function = ip6_show_fib,