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.
157 ASSERT(0 == fib_table->ft_total_route_counts);
158 FOR_EACH_FIB_SOURCE(source)
160 ASSERT(0 == fib_table->ft_src_route_counts[source]);
163 if (~0 != fib_table->ft_table_id)
165 hash_unset (ip6_main.fib_index_by_table_id, fib_table->ft_table_id);
167 pool_put_index(ip6_main.v6_fibs, fib_table->ft_index);
168 pool_put(ip6_main.fibs, fib_table);
172 ip6_fib_table_lookup (u32 fib_index,
173 const ip6_address_t *addr,
176 ip6_fib_table_instance_t *table;
177 clib_bihash_kv_24_8_t kv, value;
181 table = &ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING];
182 n_p = vec_len (table->prefix_lengths_in_search_order);
184 kv.key[0] = addr->as_u64[0];
185 kv.key[1] = addr->as_u64[1];
186 fib = ((u64)((fib_index))<<32);
189 * start search from a mask length same length or shorter.
190 * we don't want matches longer than the mask passed
193 while (i < n_p && table->prefix_lengths_in_search_order[i] > len)
200 int dst_address_length = table->prefix_lengths_in_search_order[i];
201 ip6_address_t * mask = &ip6_main.fib_masks[dst_address_length];
203 ASSERT(dst_address_length >= 0 && dst_address_length <= 128);
204 //As lengths are decreasing, masks are increasingly specific.
205 kv.key[0] &= mask->as_u64[0];
206 kv.key[1] &= mask->as_u64[1];
207 kv.key[2] = fib | dst_address_length;
209 rv = clib_bihash_search_inline_2_24_8(&table->ip6_hash, &kv, &value);
214 return (FIB_NODE_INDEX_INVALID);
218 ip6_fib_table_lookup_exact_match (u32 fib_index,
219 const ip6_address_t *addr,
222 ip6_fib_table_instance_t *table;
223 clib_bihash_kv_24_8_t kv, value;
228 table = &ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING];
229 mask = &ip6_main.fib_masks[len];
230 fib = ((u64)((fib_index))<<32);
232 kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
233 kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
234 kv.key[2] = fib | len;
236 rv = clib_bihash_search_inline_2_24_8(&table->ip6_hash, &kv, &value);
240 return (FIB_NODE_INDEX_INVALID);
244 compute_prefix_lengths_in_search_order (ip6_fib_table_instance_t *table)
247 vec_reset_length (table->prefix_lengths_in_search_order);
248 /* Note: bitmap reversed so this is in fact a longest prefix match */
249 clib_bitmap_foreach (i, table->non_empty_dst_address_length_bitmap,
251 int dst_address_length = 128 - i;
252 vec_add1(table->prefix_lengths_in_search_order, dst_address_length);
257 ip6_fib_table_entry_remove (u32 fib_index,
258 const ip6_address_t *addr,
261 ip6_fib_table_instance_t *table;
262 clib_bihash_kv_24_8_t kv;
266 table = &ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING];
267 mask = &ip6_main.fib_masks[len];
268 fib = ((u64)((fib_index))<<32);
270 kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
271 kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
272 kv.key[2] = fib | len;
274 clib_bihash_add_del_24_8(&table->ip6_hash, &kv, 0);
276 /* refcount accounting */
277 ASSERT (table->dst_address_length_refcounts[len] > 0);
278 if (--table->dst_address_length_refcounts[len] == 0)
280 table->non_empty_dst_address_length_bitmap =
281 clib_bitmap_set (table->non_empty_dst_address_length_bitmap,
283 compute_prefix_lengths_in_search_order (table);
288 ip6_fib_table_entry_insert (u32 fib_index,
289 const ip6_address_t *addr,
291 fib_node_index_t fib_entry_index)
293 ip6_fib_table_instance_t *table;
294 clib_bihash_kv_24_8_t kv;
298 table = &ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING];
299 mask = &ip6_main.fib_masks[len];
300 fib = ((u64)((fib_index))<<32);
302 kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
303 kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
304 kv.key[2] = fib | len;
305 kv.value = fib_entry_index;
307 clib_bihash_add_del_24_8(&table->ip6_hash, &kv, 1);
309 table->dst_address_length_refcounts[len]++;
311 table->non_empty_dst_address_length_bitmap =
312 clib_bitmap_set (table->non_empty_dst_address_length_bitmap,
314 compute_prefix_lengths_in_search_order (table);
317 u32 ip6_fib_table_fwding_lookup_with_if_index (ip6_main_t * im,
319 const ip6_address_t * dst)
321 u32 fib_index = vec_elt (im->fib_index_by_sw_if_index, sw_if_index);
322 return ip6_fib_table_fwding_lookup(im, fib_index, dst);
326 ip6_fib_table_get_index_for_sw_if_index (u32 sw_if_index)
328 if (sw_if_index >= vec_len(ip6_main.fib_index_by_sw_if_index))
331 * This is the case for interfaces that are not yet mapped to
336 return (ip6_main.fib_index_by_sw_if_index[sw_if_index]);
340 ip6_fib_table_fwding_dpo_update (u32 fib_index,
341 const ip6_address_t *addr,
345 ip6_fib_table_instance_t *table;
346 clib_bihash_kv_24_8_t kv;
350 table = &ip6_main.ip6_table[IP6_FIB_TABLE_FWDING];
351 mask = &ip6_main.fib_masks[len];
352 fib = ((u64)((fib_index))<<32);
354 kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
355 kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
356 kv.key[2] = fib | len;
357 kv.value = dpo->dpoi_index;
359 clib_bihash_add_del_24_8(&table->ip6_hash, &kv, 1);
361 table->dst_address_length_refcounts[len]++;
363 table->non_empty_dst_address_length_bitmap =
364 clib_bitmap_set (table->non_empty_dst_address_length_bitmap,
366 compute_prefix_lengths_in_search_order (table);
370 ip6_fib_table_fwding_dpo_remove (u32 fib_index,
371 const ip6_address_t *addr,
375 ip6_fib_table_instance_t *table;
376 clib_bihash_kv_24_8_t kv;
380 table = &ip6_main.ip6_table[IP6_FIB_TABLE_FWDING];
381 mask = &ip6_main.fib_masks[len];
382 fib = ((u64)((fib_index))<<32);
384 kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
385 kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
386 kv.key[2] = fib | len;
387 kv.value = dpo->dpoi_index;
389 clib_bihash_add_del_24_8(&table->ip6_hash, &kv, 0);
391 /* refcount accounting */
392 ASSERT (table->dst_address_length_refcounts[len] > 0);
393 if (--table->dst_address_length_refcounts[len] == 0)
395 table->non_empty_dst_address_length_bitmap =
396 clib_bitmap_set (table->non_empty_dst_address_length_bitmap,
398 compute_prefix_lengths_in_search_order (table);
403 * @brief Context when walking the IPv6 table. Since all VRFs are in the
404 * same hash table, we need to filter only those we need as we walk
406 typedef struct ip6_fib_walk_ctx_t_
409 fib_table_walk_fn_t i6w_fn;
411 fib_prefix_t i6w_root;
412 fib_prefix_t *i6w_sub_trees;
413 } ip6_fib_walk_ctx_t;
416 ip6_fib_walk_cb (clib_bihash_kv_24_8_t * kvp,
419 ip6_fib_walk_ctx_t *ctx = arg;
422 if ((kvp->key[2] >> 32) == ctx->i6w_fib_index)
424 key.as_u64[0] = kvp->key[0];
425 key.as_u64[1] = kvp->key[1];
427 if (ip6_destination_matches_route(&ip6_main,
429 &ctx->i6w_root.fp_addr.ip6,
430 ctx->i6w_root.fp_len))
432 const fib_prefix_t *sub_tree;
436 * exclude sub-trees the walk does not want to explore
438 vec_foreach(sub_tree, ctx->i6w_sub_trees)
440 if (ip6_destination_matches_route(&ip6_main,
442 &sub_tree->fp_addr.ip6,
452 switch (ctx->i6w_fn(kvp->value, ctx->i6w_ctx))
454 case FIB_TABLE_WALK_CONTINUE:
456 case FIB_TABLE_WALK_SUB_TREE_STOP: {
458 .fp_proto = FIB_PROTOCOL_IP6,
459 .fp_len = kvp->key[2] & 0xffffffff,
462 vec_add1(ctx->i6w_sub_trees, pfx);
465 case FIB_TABLE_WALK_STOP:
477 ip6_fib_table_walk (u32 fib_index,
478 fib_table_walk_fn_t fn,
481 ip6_fib_walk_ctx_t ctx = {
482 .i6w_fib_index = fib_index,
486 .fp_proto = FIB_PROTOCOL_IP6,
488 .i6w_sub_trees = NULL,
491 clib_bihash_foreach_key_value_pair_24_8(
492 &ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash,
496 vec_free(ctx.i6w_sub_trees);
500 ip6_fib_table_sub_tree_walk (u32 fib_index,
501 const fib_prefix_t *root,
502 fib_table_walk_fn_t fn,
505 ip6_fib_walk_ctx_t ctx = {
506 .i6w_fib_index = fib_index,
512 clib_bihash_foreach_key_value_pair_24_8(
513 &ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash,
518 typedef struct ip6_fib_show_ctx_t_ {
519 fib_node_index_t *entries;
520 } ip6_fib_show_ctx_t;
522 static fib_table_walk_rc_t
523 ip6_fib_table_show_walk (fib_node_index_t fib_entry_index,
526 ip6_fib_show_ctx_t *ctx = arg;
528 vec_add1(ctx->entries, fib_entry_index);
530 return (FIB_TABLE_WALK_CONTINUE);
534 ip6_fib_table_show_all (ip6_fib_t *fib,
537 fib_node_index_t *fib_entry_index;
538 ip6_fib_show_ctx_t ctx = {
542 ip6_fib_table_walk(fib->index, ip6_fib_table_show_walk, &ctx);
543 vec_sort_with_function(ctx.entries, fib_entry_cmp_for_sort);
545 vec_foreach(fib_entry_index, ctx.entries)
547 vlib_cli_output(vm, "%U",
550 FIB_ENTRY_FORMAT_BRIEF);
553 vec_free(ctx.entries);
557 ip6_fib_table_show_one (ip6_fib_t *fib,
559 ip6_address_t *address,
563 vlib_cli_output(vm, "%U",
565 ip6_fib_table_lookup(fib->index, address, mask_len),
567 FIB_ENTRY_FORMAT_DETAIL2:
568 FIB_ENTRY_FORMAT_DETAIL));
572 format_ip6_fib_table_memory (u8 * s, va_list * args)
578 (&(ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash))
579 - alloc_arena (&(ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash));
582 alloc_arena_next(&(ip6_main.ip6_table[IP6_FIB_TABLE_FWDING].ip6_hash))
583 - alloc_arena(&(ip6_main.ip6_table[IP6_FIB_TABLE_FWDING].ip6_hash));
585 s = format(s, "%=30s %=6d %=8ld\n",
587 pool_elts(ip6_main.fibs),
594 u64 count_by_prefix_length[129];
595 } count_routes_in_fib_at_prefix_length_arg_t;
598 count_routes_in_fib_at_prefix_length (clib_bihash_kv_24_8_t * kvp,
601 count_routes_in_fib_at_prefix_length_arg_t * ap = arg;
604 if ((kvp->key[2]>>32) != ap->fib_index)
607 mask_width = kvp->key[2] & 0xFF;
609 ap->count_by_prefix_length[mask_width]++;
612 static clib_error_t *
613 ip6_show_fib (vlib_main_t * vm,
614 unformat_input_t * input,
615 vlib_cli_command_t * cmd)
617 count_routes_in_fib_at_prefix_length_arg_t _ca, *ca = &_ca;
618 ip6_main_t * im6 = &ip6_main;
619 fib_table_t *fib_table;
621 int verbose, matching;
622 ip6_address_t matching_address;
624 int table_id = -1, fib_index = ~0;
630 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
632 if (unformat (input, "brief") ||
633 unformat (input, "summary") ||
634 unformat (input, "sum"))
637 else if (unformat (input, "detail") ||
638 unformat (input, "det"))
641 else if (unformat (input, "%U/%d",
642 unformat_ip6_address, &matching_address, &mask_len))
645 else if (unformat (input, "%U", unformat_ip6_address, &matching_address))
648 else if (unformat (input, "table %d", &table_id))
650 else if (unformat (input, "index %d", &fib_index))
656 pool_foreach (fib_table, im6->fibs,
661 fib = pool_elt_at_index(im6->v6_fibs, fib_table->ft_index);
662 if (table_id >= 0 && table_id != (int)fib->table_id)
664 if (fib_index != ~0 && fib_index != (int)fib->index)
666 if (fib_table->ft_flags & FIB_TABLE_FLAG_IP6_LL)
669 s = format(s, "%U, fib_index:%d, flow hash:[%U] locks:[",
670 format_fib_table_name, fib->index,
673 format_ip_flow_hash_config,
674 fib_table->ft_flow_hash_config);
675 FOR_EACH_FIB_SOURCE(source)
677 if (0 != fib_table->ft_locks[source])
679 s = format(s, "%U:%d, ",
680 format_fib_source, source,
681 fib_table->ft_locks[source]);
685 vlib_cli_output (vm, "%v", s);
691 clib_bihash_24_8_t * h = &im6->ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash;
694 vlib_cli_output (vm, "%=20s%=16s", "Prefix length", "Count");
696 clib_memset (ca, 0, sizeof(*ca));
697 ca->fib_index = fib->index;
699 clib_bihash_foreach_key_value_pair_24_8
700 (h, count_routes_in_fib_at_prefix_length, ca);
702 for (len = 128; len >= 0; len--)
704 if (ca->count_by_prefix_length[len])
705 vlib_cli_output (vm, "%=20d%=16lld",
706 len, ca->count_by_prefix_length[len]);
713 ip6_fib_table_show_all(fib, vm);
717 ip6_fib_table_show_one(fib, vm, &matching_address, mask_len, detail);
725 * This command displays the IPv6 FIB Tables (VRF Tables) and the route
726 * entries for each table.
728 * @note This command will run for a long time when the FIB tables are
729 * comprised of millions of entries. For those senarios, consider displaying
734 * Example of how to display all the IPv6 FIB tables:
735 * @cliexstart{show ip6 fib}
736 * ipv6-VRF:0, fib_index 0, flow hash: src dst sport dport proto
739 * [@0]: dpo-load-balance: [index:5 buckets:1 uRPF:5 to:[0:0]]
740 * [0] [@0]: dpo-drop ip6
743 * [@0]: dpo-load-balance: [index:10 buckets:1 uRPF:10 to:[0:0]]
744 * [0] [@2]: dpo-receive
747 * [@0]: dpo-load-balance: [index:8 buckets:1 uRPF:8 to:[0:0]]
748 * [0] [@2]: dpo-receive
751 * [@0]: dpo-load-balance: [index:7 buckets:1 uRPF:7 to:[0:0]]
752 * [0] [@2]: dpo-receive
755 * [@0]: dpo-load-balance: [index:9 buckets:1 uRPF:9 to:[0:0]]
756 * [0] [@2]: dpo-receive
759 * [@0]: dpo-load-balance: [index:6 buckets:1 uRPF:6 to:[0:0]]
760 * [0] [@2]: dpo-receive
761 * ipv6-VRF:8, fib_index 1, flow hash: src dst sport dport proto
764 * [@0]: dpo-load-balance: [index:21 buckets:1 uRPF:20 to:[0:0]]
765 * [0] [@0]: dpo-drop ip6
768 * [@0]: dpo-load-balance: [index:27 buckets:1 uRPF:26 to:[0:0]]
769 * [0] [@4]: ipv6-glean: af_packet0
772 * [@0]: dpo-load-balance: [index:28 buckets:1 uRPF:27 to:[0:0]]
773 * [0] [@2]: dpo-receive: @::a:1:1:0:7 on af_packet0
776 * [@0]: dpo-load-balance: [index:26 buckets:1 uRPF:25 to:[0:0]]
777 * [0] [@2]: dpo-receive
778 * fe80::fe:3eff:fe3e:9222/128
780 * [@0]: dpo-load-balance: [index:29 buckets:1 uRPF:28 to:[0:0]]
781 * [0] [@2]: dpo-receive: fe80::fe:3eff:fe3e:9222 on af_packet0
784 * [@0]: dpo-load-balance: [index:24 buckets:1 uRPF:23 to:[0:0]]
785 * [0] [@2]: dpo-receive
788 * [@0]: dpo-load-balance: [index:23 buckets:1 uRPF:22 to:[0:0]]
789 * [0] [@2]: dpo-receive
792 * [@0]: dpo-load-balance: [index:25 buckets:1 uRPF:24 to:[0:0]]
793 * [0] [@2]: dpo-receive
796 * [@0]: dpo-load-balance: [index:22 buckets:1 uRPF:21 to:[0:0]]
797 * [0] [@2]: dpo-receive
800 * Example of how to display a summary of all IPv6 FIB tables:
801 * @cliexstart{show ip6 fib summary}
802 * ipv6-VRF:0, fib_index 0, flow hash: src dst sport dport proto
803 * Prefix length Count
808 * ipv6-VRF:8, fib_index 1, flow hash: src dst sport dport proto
809 * Prefix length Count
819 VLIB_CLI_COMMAND (ip6_show_fib_command, static) = {
820 .path = "show ip6 fib",
821 .short_help = "show ip6 fib [summary] [table <table-id>] [index <fib-id>] [<ip6-addr>[/<width>]] [detail]",
822 .function = ip6_show_fib,