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>
20 #include <vppinfra/bihash_24_8.h>
21 #include <vppinfra/bihash_template.c>
24 vnet_ip6_fib_init (u32 fib_index)
27 .fp_proto = FIB_PROTOCOL_IP6,
37 * Add the default route.
39 fib_table_entry_special_add(fib_index,
41 FIB_SOURCE_DEFAULT_ROUTE,
45 * all link local via the link local lookup DPO
47 pfx.fp_addr.ip6.as_u64[0] = clib_host_to_net_u64 (0xFE80000000000000ULL);
48 pfx.fp_addr.ip6.as_u64[1] = 0;
50 fib_table_entry_special_dpo_add(fib_index,
58 create_fib_with_table_id (u32 table_id,
60 fib_table_flags_t flags,
63 fib_table_t *fib_table;
66 pool_get(ip6_main.fibs, fib_table);
67 pool_get_aligned(ip6_main.v6_fibs, v6_fib, CLIB_CACHE_LINE_BYTES);
69 clib_memset(fib_table, 0, sizeof(*fib_table));
70 clib_memset(v6_fib, 0, sizeof(*v6_fib));
72 ASSERT((fib_table - ip6_main.fibs) ==
73 (v6_fib - ip6_main.v6_fibs));
75 fib_table->ft_proto = FIB_PROTOCOL_IP6;
78 (fib_table - ip6_main.fibs);
80 hash_set(ip6_main.fib_index_by_table_id, table_id, fib_table->ft_index);
82 fib_table->ft_table_id =
85 fib_table->ft_flow_hash_config = IP_FLOW_HASH_DEFAULT;
86 fib_table->ft_flags = flags;
87 fib_table->ft_desc = desc;
89 vnet_ip6_fib_init(fib_table->ft_index);
90 fib_table_lock(fib_table->ft_index, FIB_PROTOCOL_IP6, src);
92 return (fib_table->ft_index);
96 ip6_fib_table_find_or_create_and_lock (u32 table_id,
101 p = hash_get (ip6_main.fib_index_by_table_id, table_id);
103 return create_fib_with_table_id(table_id, src,
107 fib_table_lock(p[0], FIB_PROTOCOL_IP6, src);
113 ip6_fib_table_create_and_lock (fib_source_t src,
114 fib_table_flags_t flags,
117 return (create_fib_with_table_id(~0, src, flags, desc));
121 ip6_fib_table_destroy (u32 fib_index)
124 * all link local first ...
127 .fp_proto = FIB_PROTOCOL_IP6,
138 fib_table_entry_delete(fib_index,
143 * ... then the default route.
145 pfx.fp_addr.ip6.as_u64[0] = 0;
147 fib_table_entry_special_remove(fib_index,
149 FIB_SOURCE_DEFAULT_ROUTE);
151 fib_table_t *fib_table = fib_table_get(fib_index, FIB_PROTOCOL_IP6);
155 * validate no more routes.
158 if (0 != fib_table->ft_total_route_counts)
159 fib_table_assert_empty(fib_table);
162 vec_foreach_index(source, fib_table->ft_src_route_counts)
164 ASSERT(0 == fib_table->ft_src_route_counts[source]);
167 if (~0 != fib_table->ft_table_id)
169 hash_unset (ip6_main.fib_index_by_table_id, fib_table->ft_table_id);
171 vec_free(fib_table->ft_src_route_counts);
172 pool_put_index(ip6_main.v6_fibs, fib_table->ft_index);
173 pool_put(ip6_main.fibs, fib_table);
177 ip6_fib_table_lookup (u32 fib_index,
178 const ip6_address_t *addr,
181 ip6_fib_table_instance_t *table;
182 clib_bihash_kv_24_8_t kv, value;
186 table = &ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING];
187 n_p = vec_len (table->prefix_lengths_in_search_order);
189 kv.key[0] = addr->as_u64[0];
190 kv.key[1] = addr->as_u64[1];
191 fib = ((u64)((fib_index))<<32);
194 * start search from a mask length same length or shorter.
195 * we don't want matches longer than the mask passed
198 while (i < n_p && table->prefix_lengths_in_search_order[i] > len)
205 int dst_address_length = table->prefix_lengths_in_search_order[i];
206 ip6_address_t * mask = &ip6_main.fib_masks[dst_address_length];
208 ASSERT(dst_address_length >= 0 && dst_address_length <= 128);
209 //As lengths are decreasing, masks are increasingly specific.
210 kv.key[0] &= mask->as_u64[0];
211 kv.key[1] &= mask->as_u64[1];
212 kv.key[2] = fib | dst_address_length;
214 rv = clib_bihash_search_inline_2_24_8(&table->ip6_hash, &kv, &value);
219 return (FIB_NODE_INDEX_INVALID);
223 ip6_fib_table_lookup_exact_match (u32 fib_index,
224 const ip6_address_t *addr,
227 ip6_fib_table_instance_t *table;
228 clib_bihash_kv_24_8_t kv, value;
233 table = &ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING];
234 mask = &ip6_main.fib_masks[len];
235 fib = ((u64)((fib_index))<<32);
237 kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
238 kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
239 kv.key[2] = fib | len;
241 rv = clib_bihash_search_inline_2_24_8(&table->ip6_hash, &kv, &value);
245 return (FIB_NODE_INDEX_INVALID);
249 compute_prefix_lengths_in_search_order (ip6_fib_table_instance_t *table)
251 u8 *old, *prefix_lengths_in_search_order = NULL;
255 * build the list in a scratch space then cutover so the workers
256 * can continue uninterrupted.
258 old = table->prefix_lengths_in_search_order;
260 /* Note: bitmap reversed so this is in fact a longest prefix match */
261 clib_bitmap_foreach (i, table->non_empty_dst_address_length_bitmap,
263 int dst_address_length = 128 - i;
264 vec_add1(prefix_lengths_in_search_order, dst_address_length);
267 table->prefix_lengths_in_search_order = prefix_lengths_in_search_order;
270 * let the workers go once round the track before we free the old set
272 vlib_worker_wait_one_loop();
277 ip6_fib_table_entry_remove (u32 fib_index,
278 const ip6_address_t *addr,
281 ip6_fib_table_instance_t *table;
282 clib_bihash_kv_24_8_t kv;
286 table = &ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING];
287 mask = &ip6_main.fib_masks[len];
288 fib = ((u64)((fib_index))<<32);
290 kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
291 kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
292 kv.key[2] = fib | len;
294 clib_bihash_add_del_24_8(&table->ip6_hash, &kv, 0);
296 /* refcount accounting */
297 ASSERT (table->dst_address_length_refcounts[len] > 0);
298 if (--table->dst_address_length_refcounts[len] == 0)
300 table->non_empty_dst_address_length_bitmap =
301 clib_bitmap_set (table->non_empty_dst_address_length_bitmap,
303 compute_prefix_lengths_in_search_order (table);
308 ip6_fib_table_entry_insert (u32 fib_index,
309 const ip6_address_t *addr,
311 fib_node_index_t fib_entry_index)
313 ip6_fib_table_instance_t *table;
314 clib_bihash_kv_24_8_t kv;
318 table = &ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING];
319 mask = &ip6_main.fib_masks[len];
320 fib = ((u64)((fib_index))<<32);
322 kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
323 kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
324 kv.key[2] = fib | len;
325 kv.value = fib_entry_index;
327 clib_bihash_add_del_24_8(&table->ip6_hash, &kv, 1);
329 if (0 == table->dst_address_length_refcounts[len]++)
331 table->non_empty_dst_address_length_bitmap =
332 clib_bitmap_set (table->non_empty_dst_address_length_bitmap,
334 compute_prefix_lengths_in_search_order (table);
338 u32 ip6_fib_table_fwding_lookup_with_if_index (ip6_main_t * im,
340 const ip6_address_t * dst)
342 u32 fib_index = vec_elt (im->fib_index_by_sw_if_index, sw_if_index);
343 return ip6_fib_table_fwding_lookup(fib_index, dst);
347 ip6_fib_table_get_index_for_sw_if_index (u32 sw_if_index)
349 if (sw_if_index >= vec_len(ip6_main.fib_index_by_sw_if_index))
352 * This is the case for interfaces that are not yet mapped to
357 return (ip6_main.fib_index_by_sw_if_index[sw_if_index]);
361 ip6_fib_table_fwding_dpo_update (u32 fib_index,
362 const ip6_address_t *addr,
366 ip6_fib_table_instance_t *table;
367 clib_bihash_kv_24_8_t kv;
371 table = &ip6_main.ip6_table[IP6_FIB_TABLE_FWDING];
372 mask = &ip6_main.fib_masks[len];
373 fib = ((u64)((fib_index))<<32);
375 kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
376 kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
377 kv.key[2] = fib | len;
378 kv.value = dpo->dpoi_index;
380 clib_bihash_add_del_24_8(&table->ip6_hash, &kv, 1);
382 if (0 == table->dst_address_length_refcounts[len]++)
384 table->non_empty_dst_address_length_bitmap =
385 clib_bitmap_set (table->non_empty_dst_address_length_bitmap,
387 compute_prefix_lengths_in_search_order (table);
392 ip6_fib_table_fwding_dpo_remove (u32 fib_index,
393 const ip6_address_t *addr,
397 ip6_fib_table_instance_t *table;
398 clib_bihash_kv_24_8_t kv;
402 table = &ip6_main.ip6_table[IP6_FIB_TABLE_FWDING];
403 mask = &ip6_main.fib_masks[len];
404 fib = ((u64)((fib_index))<<32);
406 kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
407 kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
408 kv.key[2] = fib | len;
409 kv.value = dpo->dpoi_index;
411 clib_bihash_add_del_24_8(&table->ip6_hash, &kv, 0);
413 /* refcount accounting */
414 ASSERT (table->dst_address_length_refcounts[len] > 0);
415 if (--table->dst_address_length_refcounts[len] == 0)
417 table->non_empty_dst_address_length_bitmap =
418 clib_bitmap_set (table->non_empty_dst_address_length_bitmap,
420 compute_prefix_lengths_in_search_order (table);
425 * @brief Context when walking the IPv6 table. Since all VRFs are in the
426 * same hash table, we need to filter only those we need as we walk
428 typedef struct ip6_fib_walk_ctx_t_
431 fib_table_walk_fn_t i6w_fn;
433 fib_prefix_t i6w_root;
434 fib_prefix_t *i6w_sub_trees;
435 } ip6_fib_walk_ctx_t;
438 ip6_fib_walk_cb (clib_bihash_kv_24_8_t * kvp,
441 ip6_fib_walk_ctx_t *ctx = arg;
444 if ((kvp->key[2] >> 32) == ctx->i6w_fib_index)
446 key.as_u64[0] = kvp->key[0];
447 key.as_u64[1] = kvp->key[1];
449 if (ip6_destination_matches_route(&ip6_main,
451 &ctx->i6w_root.fp_addr.ip6,
452 ctx->i6w_root.fp_len))
454 const fib_prefix_t *sub_tree;
458 * exclude sub-trees the walk does not want to explore
460 vec_foreach(sub_tree, ctx->i6w_sub_trees)
462 if (ip6_destination_matches_route(&ip6_main,
464 &sub_tree->fp_addr.ip6,
474 switch (ctx->i6w_fn(kvp->value, ctx->i6w_ctx))
476 case FIB_TABLE_WALK_CONTINUE:
478 case FIB_TABLE_WALK_SUB_TREE_STOP: {
480 .fp_proto = FIB_PROTOCOL_IP6,
481 .fp_len = kvp->key[2] & 0xffffffff,
484 vec_add1(ctx->i6w_sub_trees, pfx);
487 case FIB_TABLE_WALK_STOP:
499 ip6_fib_table_walk (u32 fib_index,
500 fib_table_walk_fn_t fn,
503 ip6_fib_walk_ctx_t ctx = {
504 .i6w_fib_index = fib_index,
508 .fp_proto = FIB_PROTOCOL_IP6,
510 .i6w_sub_trees = NULL,
513 clib_bihash_foreach_key_value_pair_24_8(
514 &ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash,
518 vec_free(ctx.i6w_sub_trees);
522 ip6_fib_table_sub_tree_walk (u32 fib_index,
523 const fib_prefix_t *root,
524 fib_table_walk_fn_t fn,
527 ip6_fib_walk_ctx_t ctx = {
528 .i6w_fib_index = fib_index,
534 clib_bihash_foreach_key_value_pair_24_8(
535 &ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash,
540 typedef struct ip6_fib_show_ctx_t_ {
541 fib_node_index_t *entries;
542 } ip6_fib_show_ctx_t;
544 static fib_table_walk_rc_t
545 ip6_fib_table_show_walk (fib_node_index_t fib_entry_index,
548 ip6_fib_show_ctx_t *ctx = arg;
550 vec_add1(ctx->entries, fib_entry_index);
552 return (FIB_TABLE_WALK_CONTINUE);
556 ip6_fib_table_show_all (ip6_fib_t *fib,
559 fib_node_index_t *fib_entry_index;
560 ip6_fib_show_ctx_t ctx = {
564 ip6_fib_table_walk(fib->index, ip6_fib_table_show_walk, &ctx);
565 vec_sort_with_function(ctx.entries, fib_entry_cmp_for_sort);
567 vec_foreach(fib_entry_index, ctx.entries)
569 vlib_cli_output(vm, "%U",
572 FIB_ENTRY_FORMAT_BRIEF);
575 vec_free(ctx.entries);
579 ip6_fib_table_show_one (ip6_fib_t *fib,
581 ip6_address_t *address,
585 vlib_cli_output(vm, "%U",
587 ip6_fib_table_lookup(fib->index, address, mask_len),
589 FIB_ENTRY_FORMAT_DETAIL2:
590 FIB_ENTRY_FORMAT_DETAIL));
594 format_ip6_fib_table_memory (u8 * s, va_list * args)
598 bytes_inuse = (alloc_arena_next(&(ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash)) +
599 alloc_arena_next(&(ip6_main.ip6_table[IP6_FIB_TABLE_FWDING].ip6_hash)));
601 s = format(s, "%=30s %=6d %=12ld\n",
603 pool_elts(ip6_main.fibs),
610 u64 count_by_prefix_length[129];
611 } count_routes_in_fib_at_prefix_length_arg_t;
614 count_routes_in_fib_at_prefix_length (clib_bihash_kv_24_8_t * kvp,
617 count_routes_in_fib_at_prefix_length_arg_t * ap = arg;
620 if ((kvp->key[2]>>32) != ap->fib_index)
621 return (BIHASH_WALK_CONTINUE);
623 mask_width = kvp->key[2] & 0xFF;
625 ap->count_by_prefix_length[mask_width]++;
627 return (BIHASH_WALK_CONTINUE);
630 static clib_error_t *
631 ip6_show_fib (vlib_main_t * vm,
632 unformat_input_t * input,
633 vlib_cli_command_t * cmd)
635 count_routes_in_fib_at_prefix_length_arg_t _ca, *ca = &_ca;
636 ip6_main_t * im6 = &ip6_main;
637 fib_table_t *fib_table;
639 int verbose, matching;
640 ip6_address_t matching_address;
642 int table_id = -1, fib_index = ~0;
649 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
651 if (unformat (input, "brief") ||
652 unformat (input, "summary") ||
653 unformat (input, "sum"))
656 else if (unformat (input, "detail") ||
657 unformat (input, "det"))
660 else if (unformat (input, "hash") ||
661 unformat (input, "mem") ||
662 unformat (input, "memory"))
665 else if (unformat (input, "%U/%d",
666 unformat_ip6_address, &matching_address, &mask_len))
669 else if (unformat (input, "%U", unformat_ip6_address, &matching_address))
672 else if (unformat (input, "table %d", &table_id))
674 else if (unformat (input, "index %d", &fib_index))
682 vlib_cli_output (vm, "IPv6 Non-Forwarding Hash Table:\n%U\n",
684 &im6->ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash,
686 vlib_cli_output (vm, "IPv6 Forwarding Hash Table:\n%U\n",
688 &im6->ip6_table[IP6_FIB_TABLE_FWDING].ip6_hash,
693 pool_foreach (fib_table, im6->fibs,
698 fib = pool_elt_at_index(im6->v6_fibs, fib_table->ft_index);
699 if (table_id >= 0 && table_id != (int)fib->table_id)
701 if (fib_index != ~0 && fib_index != (int)fib->index)
703 if (fib_table->ft_flags & FIB_TABLE_FLAG_IP6_LL)
706 s = format(s, "%U, fib_index:%d, flow hash:[%U] epoch:%d flags:%U locks:[",
707 format_fib_table_name, fib->index,
710 format_ip_flow_hash_config,
711 fib_table->ft_flow_hash_config,
713 format_fib_table_flags, fib_table->ft_flags);
715 vec_foreach_index(source, fib_table->ft_locks)
717 if (0 != fib_table->ft_locks[source])
719 s = format(s, "%U:%d, ",
720 format_fib_source, source,
721 fib_table->ft_locks[source]);
725 vlib_cli_output (vm, "%v", s);
731 clib_bihash_24_8_t * h = &im6->ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash;
734 vlib_cli_output (vm, "%=20s%=16s", "Prefix length", "Count");
736 clib_memset (ca, 0, sizeof(*ca));
737 ca->fib_index = fib->index;
739 clib_bihash_foreach_key_value_pair_24_8
740 (h, count_routes_in_fib_at_prefix_length, ca);
742 for (len = 128; len >= 0; len--)
744 if (ca->count_by_prefix_length[len])
745 vlib_cli_output (vm, "%=20d%=16lld",
746 len, ca->count_by_prefix_length[len]);
753 ip6_fib_table_show_all(fib, vm);
757 ip6_fib_table_show_one(fib, vm, &matching_address, mask_len, detail);
765 * This command displays the IPv6 FIB Tables (VRF Tables) and the route
766 * entries for each table.
768 * @note This command will run for a long time when the FIB tables are
769 * comprised of millions of entries. For those senarios, consider displaying
774 * Example of how to display all the IPv6 FIB tables:
775 * @cliexstart{show ip6 fib}
776 * ipv6-VRF:0, fib_index 0, flow hash: src dst sport dport proto
779 * [@0]: dpo-load-balance: [index:5 buckets:1 uRPF:5 to:[0:0]]
780 * [0] [@0]: dpo-drop ip6
783 * [@0]: dpo-load-balance: [index:10 buckets:1 uRPF:10 to:[0:0]]
784 * [0] [@2]: dpo-receive
787 * [@0]: dpo-load-balance: [index:8 buckets:1 uRPF:8 to:[0:0]]
788 * [0] [@2]: dpo-receive
791 * [@0]: dpo-load-balance: [index:7 buckets:1 uRPF:7 to:[0:0]]
792 * [0] [@2]: dpo-receive
795 * [@0]: dpo-load-balance: [index:9 buckets:1 uRPF:9 to:[0:0]]
796 * [0] [@2]: dpo-receive
799 * [@0]: dpo-load-balance: [index:6 buckets:1 uRPF:6 to:[0:0]]
800 * [0] [@2]: dpo-receive
801 * ipv6-VRF:8, fib_index 1, flow hash: src dst sport dport proto
804 * [@0]: dpo-load-balance: [index:21 buckets:1 uRPF:20 to:[0:0]]
805 * [0] [@0]: dpo-drop ip6
808 * [@0]: dpo-load-balance: [index:27 buckets:1 uRPF:26 to:[0:0]]
809 * [0] [@4]: ipv6-glean: af_packet0
812 * [@0]: dpo-load-balance: [index:28 buckets:1 uRPF:27 to:[0:0]]
813 * [0] [@2]: dpo-receive: @::a:1:1:0:7 on af_packet0
816 * [@0]: dpo-load-balance: [index:26 buckets:1 uRPF:25 to:[0:0]]
817 * [0] [@2]: dpo-receive
818 * fe80::fe:3eff:fe3e:9222/128
820 * [@0]: dpo-load-balance: [index:29 buckets:1 uRPF:28 to:[0:0]]
821 * [0] [@2]: dpo-receive: fe80::fe:3eff:fe3e:9222 on af_packet0
824 * [@0]: dpo-load-balance: [index:24 buckets:1 uRPF:23 to:[0:0]]
825 * [0] [@2]: dpo-receive
828 * [@0]: dpo-load-balance: [index:23 buckets:1 uRPF:22 to:[0:0]]
829 * [0] [@2]: dpo-receive
832 * [@0]: dpo-load-balance: [index:25 buckets:1 uRPF:24 to:[0:0]]
833 * [0] [@2]: dpo-receive
836 * [@0]: dpo-load-balance: [index:22 buckets:1 uRPF:21 to:[0:0]]
837 * [0] [@2]: dpo-receive
840 * Example of how to display a summary of all IPv6 FIB tables:
841 * @cliexstart{show ip6 fib summary}
842 * ipv6-VRF:0, fib_index 0, flow hash: src dst sport dport proto
843 * Prefix length Count
848 * ipv6-VRF:8, fib_index 1, flow hash: src dst sport dport proto
849 * Prefix length Count
859 VLIB_CLI_COMMAND (ip6_show_fib_command, static) = {
860 .path = "show ip6 fib",
861 .short_help = "show ip6 fib [summary] [table <table-id>] [index <fib-id>] [<ip6-addr>[/<width>]] [detail]",
862 .function = ip6_show_fib,