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>
20 vnet_ip6_fib_init (u32 fib_index)
23 .fp_proto = FIB_PROTOCOL_IP6,
33 * Add the default route.
35 fib_table_entry_special_add(fib_index,
37 FIB_SOURCE_DEFAULT_ROUTE,
41 * all link local for us
43 pfx.fp_addr.ip6.as_u64[0] = clib_host_to_net_u64 (0xFE80000000000000ULL);
44 pfx.fp_addr.ip6.as_u64[1] = 0;
46 fib_table_entry_special_add(fib_index,
49 FIB_ENTRY_FLAG_LOCAL);
53 create_fib_with_table_id (u32 table_id,
56 fib_table_t *fib_table;
59 pool_get_aligned(ip6_main.fibs, fib_table, CLIB_CACHE_LINE_BYTES);
60 pool_get_aligned(ip6_main.v6_fibs, v6_fib, CLIB_CACHE_LINE_BYTES);
62 memset(fib_table, 0, sizeof(*fib_table));
63 memset(v6_fib, 0, sizeof(*v6_fib));
65 ASSERT((fib_table - ip6_main.fibs) ==
66 (v6_fib - ip6_main.v6_fibs));
68 fib_table->ft_proto = FIB_PROTOCOL_IP6;
71 (fib_table - ip6_main.fibs);
73 hash_set(ip6_main.fib_index_by_table_id, table_id, fib_table->ft_index);
75 fib_table->ft_table_id =
78 fib_table->ft_flow_hash_config = IP_FLOW_HASH_DEFAULT;
80 vnet_ip6_fib_init(fib_table->ft_index);
81 fib_table_lock(fib_table->ft_index, FIB_PROTOCOL_IP6, src);
83 return (fib_table->ft_index);
87 ip6_fib_table_find_or_create_and_lock (u32 table_id,
92 p = hash_get (ip6_main.fib_index_by_table_id, table_id);
94 return create_fib_with_table_id(table_id, src);
96 fib_table_lock(p[0], FIB_PROTOCOL_IP6, src);
102 ip6_fib_table_create_and_lock (fib_source_t src)
104 return (create_fib_with_table_id(~0, src));
108 ip6_fib_table_destroy (u32 fib_index)
111 .fp_proto = FIB_PROTOCOL_IP6,
123 fib_table_entry_special_remove(fib_index,
125 FIB_SOURCE_DEFAULT_ROUTE);
131 ip6_set_solicited_node_multicast_address(&pfx.fp_addr.ip6, 0);
133 fib_table_entry_special_remove(fib_index,
138 * all-routers multicast address
140 ip6_set_reserved_multicast_address (&pfx.fp_addr.ip6,
141 IP6_MULTICAST_SCOPE_link_local,
142 IP6_MULTICAST_GROUP_ID_all_routers);
144 fib_table_entry_special_remove(fib_index,
149 * all-nodes multicast address
151 ip6_set_reserved_multicast_address (&pfx.fp_addr.ip6,
152 IP6_MULTICAST_SCOPE_link_local,
153 IP6_MULTICAST_GROUP_ID_all_hosts);
155 fib_table_entry_special_remove(fib_index,
160 * all-mldv2 multicast address
162 ip6_set_reserved_multicast_address (&pfx.fp_addr.ip6,
163 IP6_MULTICAST_SCOPE_link_local,
164 IP6_MULTICAST_GROUP_ID_mldv2_routers);
166 fib_table_entry_special_remove(fib_index,
173 pfx.fp_addr.ip6.as_u64[0] = clib_host_to_net_u64 (0xFE80000000000000ULL);
174 pfx.fp_addr.ip6.as_u64[1] = 0;
176 fib_table_entry_special_remove(fib_index,
180 fib_table_t *fib_table = fib_table_get(fib_index, FIB_PROTOCOL_IP6);
184 * validate no more routes.
186 ASSERT(0 == fib_table->ft_total_route_counts);
187 FOR_EACH_FIB_SOURCE(source)
189 ASSERT(0 == fib_table->ft_src_route_counts[source]);
192 if (~0 != fib_table->ft_table_id)
194 hash_unset (ip6_main.fib_index_by_table_id, fib_table->ft_table_id);
196 pool_put_index(ip6_main.v6_fibs, fib_table->ft_index);
197 pool_put(ip6_main.fibs, fib_table);
201 ip6_fib_table_lookup (u32 fib_index,
202 const ip6_address_t *addr,
205 ip6_fib_table_instance_t *table;
206 BVT(clib_bihash_kv) kv, value;
210 table = &ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING];
211 n_p = vec_len (table->prefix_lengths_in_search_order);
213 kv.key[0] = addr->as_u64[0];
214 kv.key[1] = addr->as_u64[1];
215 fib = ((u64)((fib_index))<<32);
218 * start search from a mask length same length or shorter.
219 * we don't want matches longer than the mask passed
222 while (i < n_p && table->prefix_lengths_in_search_order[i] > len)
229 int dst_address_length = table->prefix_lengths_in_search_order[i];
230 ip6_address_t * mask = &ip6_main.fib_masks[dst_address_length];
232 ASSERT(dst_address_length >= 0 && dst_address_length <= 128);
233 //As lengths are decreasing, masks are increasingly specific.
234 kv.key[0] &= mask->as_u64[0];
235 kv.key[1] &= mask->as_u64[1];
236 kv.key[2] = fib | dst_address_length;
238 rv = BV(clib_bihash_search_inline_2)(&table->ip6_hash, &kv, &value);
243 return (FIB_NODE_INDEX_INVALID);
247 ip6_fib_table_lookup_exact_match (u32 fib_index,
248 const ip6_address_t *addr,
251 ip6_fib_table_instance_t *table;
252 BVT(clib_bihash_kv) kv, value;
257 table = &ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING];
258 mask = &ip6_main.fib_masks[len];
259 fib = ((u64)((fib_index))<<32);
261 kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
262 kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
263 kv.key[2] = fib | len;
265 rv = BV(clib_bihash_search_inline_2)(&table->ip6_hash, &kv, &value);
269 return (FIB_NODE_INDEX_INVALID);
273 compute_prefix_lengths_in_search_order (ip6_fib_table_instance_t *table)
276 vec_reset_length (table->prefix_lengths_in_search_order);
277 /* Note: bitmap reversed so this is in fact a longest prefix match */
278 clib_bitmap_foreach (i, table->non_empty_dst_address_length_bitmap,
280 int dst_address_length = 128 - i;
281 vec_add1(table->prefix_lengths_in_search_order, dst_address_length);
286 ip6_fib_table_entry_remove (u32 fib_index,
287 const ip6_address_t *addr,
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;
303 BV(clib_bihash_add_del)(&table->ip6_hash, &kv, 0);
305 /* refcount accounting */
306 ASSERT (table->dst_address_length_refcounts[len] > 0);
307 if (--table->dst_address_length_refcounts[len] == 0)
309 table->non_empty_dst_address_length_bitmap =
310 clib_bitmap_set (table->non_empty_dst_address_length_bitmap,
312 compute_prefix_lengths_in_search_order (table);
317 ip6_fib_table_entry_insert (u32 fib_index,
318 const ip6_address_t *addr,
320 fib_node_index_t fib_entry_index)
322 ip6_fib_table_instance_t *table;
323 BVT(clib_bihash_kv) kv;
327 table = &ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING];
328 mask = &ip6_main.fib_masks[len];
329 fib = ((u64)((fib_index))<<32);
331 kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
332 kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
333 kv.key[2] = fib | len;
334 kv.value = fib_entry_index;
336 BV(clib_bihash_add_del)(&table->ip6_hash, &kv, 1);
338 table->dst_address_length_refcounts[len]++;
340 table->non_empty_dst_address_length_bitmap =
341 clib_bitmap_set (table->non_empty_dst_address_length_bitmap,
343 compute_prefix_lengths_in_search_order (table);
346 u32 ip6_fib_table_fwding_lookup_with_if_index (ip6_main_t * im,
348 const ip6_address_t * dst)
350 u32 fib_index = vec_elt (im->fib_index_by_sw_if_index, sw_if_index);
351 return ip6_fib_table_fwding_lookup(im, fib_index, dst);
355 ip6_fib_table_get_index_for_sw_if_index (u32 sw_if_index)
357 if (sw_if_index >= vec_len(ip6_main.fib_index_by_sw_if_index))
360 * This is the case for interfaces that are not yet mapped to
365 return (ip6_main.fib_index_by_sw_if_index[sw_if_index]);
369 ip6_fib_table_fwding_dpo_update (u32 fib_index,
370 const ip6_address_t *addr,
374 ip6_fib_table_instance_t *table;
375 BVT(clib_bihash_kv) kv;
379 table = &ip6_main.ip6_table[IP6_FIB_TABLE_FWDING];
380 mask = &ip6_main.fib_masks[len];
381 fib = ((u64)((fib_index))<<32);
383 kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
384 kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
385 kv.key[2] = fib | len;
386 kv.value = dpo->dpoi_index;
388 BV(clib_bihash_add_del)(&table->ip6_hash, &kv, 1);
390 table->dst_address_length_refcounts[len]++;
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);
399 ip6_fib_table_fwding_dpo_remove (u32 fib_index,
400 const ip6_address_t *addr,
404 ip6_fib_table_instance_t *table;
405 BVT(clib_bihash_kv) kv;
409 table = &ip6_main.ip6_table[IP6_FIB_TABLE_FWDING];
410 mask = &ip6_main.fib_masks[len];
411 fib = ((u64)((fib_index))<<32);
413 kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
414 kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
415 kv.key[2] = fib | len;
416 kv.value = dpo->dpoi_index;
418 BV(clib_bihash_add_del)(&table->ip6_hash, &kv, 0);
420 /* refcount accounting */
421 ASSERT (table->dst_address_length_refcounts[len] > 0);
422 if (--table->dst_address_length_refcounts[len] == 0)
424 table->non_empty_dst_address_length_bitmap =
425 clib_bitmap_set (table->non_empty_dst_address_length_bitmap,
427 compute_prefix_lengths_in_search_order (table);
432 * @brief Context when walking the IPv6 table. Since all VRFs are in the
433 * same hash table, we need to filter only those we need as we walk
435 typedef struct ip6_fib_walk_ctx_t_
438 fib_table_walk_fn_t i6w_fn;
440 fib_prefix_t i6w_root;
441 fib_prefix_t *i6w_sub_trees;
442 } ip6_fib_walk_ctx_t;
445 ip6_fib_walk_cb (clib_bihash_kv_24_8_t * kvp,
448 ip6_fib_walk_ctx_t *ctx = arg;
451 if ((kvp->key[2] >> 32) == ctx->i6w_fib_index)
453 key.as_u64[0] = kvp->key[0];
454 key.as_u64[1] = kvp->key[1];
456 if (ip6_destination_matches_route(&ip6_main,
458 &ctx->i6w_root.fp_addr.ip6,
459 ctx->i6w_root.fp_len))
461 const fib_prefix_t *sub_tree;
465 * exclude sub-trees the walk does not want to explore
467 vec_foreach(sub_tree, ctx->i6w_sub_trees)
469 if (ip6_destination_matches_route(&ip6_main,
471 &sub_tree->fp_addr.ip6,
481 switch (ctx->i6w_fn(kvp->value, ctx->i6w_ctx))
483 case FIB_TABLE_WALK_CONTINUE:
485 case FIB_TABLE_WALK_SUB_TREE_STOP: {
487 .fp_proto = FIB_PROTOCOL_IP6,
488 .fp_len = kvp->key[2] & 0xffffffff,
491 vec_add1(ctx->i6w_sub_trees, pfx);
494 case FIB_TABLE_WALK_STOP:
506 ip6_fib_table_walk (u32 fib_index,
507 fib_table_walk_fn_t fn,
510 ip6_fib_walk_ctx_t ctx = {
511 .i6w_fib_index = fib_index,
515 .fp_proto = FIB_PROTOCOL_IP6,
517 .i6w_sub_trees = NULL,
520 BV(clib_bihash_foreach_key_value_pair)(
521 &ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash,
525 vec_free(ctx.i6w_sub_trees);
529 ip6_fib_table_sub_tree_walk (u32 fib_index,
530 const fib_prefix_t *root,
531 fib_table_walk_fn_t fn,
534 ip6_fib_walk_ctx_t ctx = {
535 .i6w_fib_index = fib_index,
541 BV(clib_bihash_foreach_key_value_pair)(
542 &ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash,
547 typedef struct ip6_fib_show_ctx_t_ {
548 fib_node_index_t *entries;
549 } ip6_fib_show_ctx_t;
551 static fib_table_walk_rc_t
552 ip6_fib_table_show_walk (fib_node_index_t fib_entry_index,
555 ip6_fib_show_ctx_t *ctx = arg;
557 vec_add1(ctx->entries, fib_entry_index);
559 return (FIB_TABLE_WALK_CONTINUE);
563 ip6_fib_table_show_all (ip6_fib_t *fib,
566 fib_node_index_t *fib_entry_index;
567 ip6_fib_show_ctx_t ctx = {
571 ip6_fib_table_walk(fib->index, ip6_fib_table_show_walk, &ctx);
572 vec_sort_with_function(ctx.entries, fib_entry_cmp_for_sort);
574 vec_foreach(fib_entry_index, ctx.entries)
576 vlib_cli_output(vm, "%U",
579 FIB_ENTRY_FORMAT_BRIEF);
582 vec_free(ctx.entries);
586 ip6_fib_table_show_one (ip6_fib_t *fib,
588 ip6_address_t *address,
592 vlib_cli_output(vm, "%U",
594 ip6_fib_table_lookup(fib->index, address, mask_len),
596 FIB_ENTRY_FORMAT_DETAIL2:
597 FIB_ENTRY_FORMAT_DETAIL));
601 format_ip6_fib_table_memory (u8 * s, va_list * args)
603 s = format(s, "%=30s %=6d %=8ld\n",
605 pool_elts(ip6_main.fibs),
606 mheap_bytes(ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash.mheap) +
607 mheap_bytes(ip6_main.ip6_table[IP6_FIB_TABLE_FWDING].ip6_hash.mheap));
614 u64 count_by_prefix_length[129];
615 } count_routes_in_fib_at_prefix_length_arg_t;
618 count_routes_in_fib_at_prefix_length (BVT(clib_bihash_kv) * kvp,
621 count_routes_in_fib_at_prefix_length_arg_t * ap = arg;
624 if ((kvp->key[2]>>32) != ap->fib_index)
627 mask_width = kvp->key[2] & 0xFF;
629 ap->count_by_prefix_length[mask_width]++;
632 static clib_error_t *
633 ip6_show_fib (vlib_main_t * vm,
634 unformat_input_t * input,
635 vlib_cli_command_t * cmd)
637 count_routes_in_fib_at_prefix_length_arg_t _ca, *ca = &_ca;
638 ip6_main_t * im6 = &ip6_main;
639 fib_table_t *fib_table;
641 int verbose, matching;
642 ip6_address_t matching_address;
644 int table_id = -1, fib_index = ~0;
650 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
652 if (unformat (input, "brief") ||
653 unformat (input, "summary") ||
654 unformat (input, "sum"))
657 else if (unformat (input, "detail") ||
658 unformat (input, "det"))
661 else if (unformat (input, "%U/%d",
662 unformat_ip6_address, &matching_address, &mask_len))
665 else if (unformat (input, "%U", unformat_ip6_address, &matching_address))
668 else if (unformat (input, "table %d", &table_id))
670 else if (unformat (input, "index %d", &fib_index))
676 pool_foreach (fib_table, im6->fibs,
681 fib = pool_elt_at_index(im6->v6_fibs, fib_table->ft_index);
682 if (table_id >= 0 && table_id != (int)fib->table_id)
684 if (fib_index != ~0 && fib_index != (int)fib->index)
687 s = format(s, "%U, fib_index:%d, flow hash:[%U] locks:[",
688 format_fib_table_name, fib->index,
691 format_ip_flow_hash_config,
692 fib_table->ft_flow_hash_config);
693 FOR_EACH_FIB_SOURCE(source)
695 if (0 != fib_table->ft_locks[source])
697 s = format(s, "%U:%d, ",
698 format_fib_source, source,
699 fib_table->ft_locks[source]);
703 vlib_cli_output (vm, "%v", s);
709 BVT(clib_bihash) * h = &im6->ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash;
712 vlib_cli_output (vm, "%=20s%=16s", "Prefix length", "Count");
714 memset (ca, 0, sizeof(*ca));
715 ca->fib_index = fib->index;
717 BV(clib_bihash_foreach_key_value_pair)
718 (h, count_routes_in_fib_at_prefix_length, ca);
720 for (len = 128; len >= 0; len--)
722 if (ca->count_by_prefix_length[len])
723 vlib_cli_output (vm, "%=20d%=16lld",
724 len, ca->count_by_prefix_length[len]);
731 ip6_fib_table_show_all(fib, vm);
735 ip6_fib_table_show_one(fib, vm, &matching_address, mask_len, detail);
743 * This command displays the IPv6 FIB Tables (VRF Tables) and the route
744 * entries for each table.
746 * @note This command will run for a long time when the FIB tables are
747 * comprised of millions of entries. For those senarios, consider displaying
752 * Example of how to display all the IPv6 FIB tables:
753 * @cliexstart{show ip6 fib}
754 * ipv6-VRF:0, fib_index 0, flow hash: src dst sport dport proto
757 * [@0]: dpo-load-balance: [index:5 buckets:1 uRPF:5 to:[0:0]]
758 * [0] [@0]: dpo-drop ip6
761 * [@0]: dpo-load-balance: [index:10 buckets:1 uRPF:10 to:[0:0]]
762 * [0] [@2]: dpo-receive
765 * [@0]: dpo-load-balance: [index:8 buckets:1 uRPF:8 to:[0:0]]
766 * [0] [@2]: dpo-receive
769 * [@0]: dpo-load-balance: [index:7 buckets:1 uRPF:7 to:[0:0]]
770 * [0] [@2]: dpo-receive
773 * [@0]: dpo-load-balance: [index:9 buckets:1 uRPF:9 to:[0:0]]
774 * [0] [@2]: dpo-receive
777 * [@0]: dpo-load-balance: [index:6 buckets:1 uRPF:6 to:[0:0]]
778 * [0] [@2]: dpo-receive
779 * ipv6-VRF:8, fib_index 1, flow hash: src dst sport dport proto
782 * [@0]: dpo-load-balance: [index:21 buckets:1 uRPF:20 to:[0:0]]
783 * [0] [@0]: dpo-drop ip6
786 * [@0]: dpo-load-balance: [index:27 buckets:1 uRPF:26 to:[0:0]]
787 * [0] [@4]: ipv6-glean: af_packet0
790 * [@0]: dpo-load-balance: [index:28 buckets:1 uRPF:27 to:[0:0]]
791 * [0] [@2]: dpo-receive: @::a:1:1:0:7 on af_packet0
794 * [@0]: dpo-load-balance: [index:26 buckets:1 uRPF:25 to:[0:0]]
795 * [0] [@2]: dpo-receive
796 * fe80::fe:3eff:fe3e:9222/128
798 * [@0]: dpo-load-balance: [index:29 buckets:1 uRPF:28 to:[0:0]]
799 * [0] [@2]: dpo-receive: fe80::fe:3eff:fe3e:9222 on af_packet0
802 * [@0]: dpo-load-balance: [index:24 buckets:1 uRPF:23 to:[0:0]]
803 * [0] [@2]: dpo-receive
806 * [@0]: dpo-load-balance: [index:23 buckets:1 uRPF:22 to:[0:0]]
807 * [0] [@2]: dpo-receive
810 * [@0]: dpo-load-balance: [index:25 buckets:1 uRPF:24 to:[0:0]]
811 * [0] [@2]: dpo-receive
814 * [@0]: dpo-load-balance: [index:22 buckets:1 uRPF:21 to:[0:0]]
815 * [0] [@2]: dpo-receive
818 * Example of how to display a summary of all IPv6 FIB tables:
819 * @cliexstart{show ip6 fib summary}
820 * ipv6-VRF:0, fib_index 0, flow hash: src dst sport dport proto
821 * Prefix length Count
826 * ipv6-VRF:8, fib_index 1, flow hash: src dst sport dport proto
827 * Prefix length Count
837 VLIB_CLI_COMMAND (ip6_show_fib_command, static) = {
838 .path = "show ip6 fib",
839 .short_help = "show ip6 fib [summary] [table <table-id>] [index <fib-id>] [<ip6-addr>[/<width>]] [detail]",
840 .function = ip6_show_fib,