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,
42 * Add ff02::1:ff00:0/104 via local route for all tables.
43 * This is required for neighbor discovery to work.
45 ip6_set_solicited_node_multicast_address(&pfx.fp_addr.ip6, 0);
47 fib_table_entry_special_add(fib_index,
54 * Add all-routers multicast address via local route for all tables
56 ip6_set_reserved_multicast_address (&pfx.fp_addr.ip6,
57 IP6_MULTICAST_SCOPE_link_local,
58 IP6_MULTICAST_GROUP_ID_all_routers);
60 fib_table_entry_special_add(fib_index,
67 * Add all-nodes multicast address via local route for all tables
69 ip6_set_reserved_multicast_address (&pfx.fp_addr.ip6,
70 IP6_MULTICAST_SCOPE_link_local,
71 IP6_MULTICAST_GROUP_ID_all_hosts);
73 fib_table_entry_special_add(fib_index,
80 * Add all-mldv2 multicast address via local route for all tables
82 ip6_set_reserved_multicast_address (&pfx.fp_addr.ip6,
83 IP6_MULTICAST_SCOPE_link_local,
84 IP6_MULTICAST_GROUP_ID_mldv2_routers);
86 fib_table_entry_special_add(fib_index,
93 * all link local for us
95 pfx.fp_addr.ip6.as_u64[0] = clib_host_to_net_u64 (0xFE80000000000000ULL);
96 pfx.fp_addr.ip6.as_u64[1] = 0;
98 fib_table_entry_special_add(fib_index,
101 FIB_ENTRY_FLAG_LOCAL,
106 create_fib_with_table_id (u32 table_id)
108 fib_table_t *fib_table;
110 pool_get_aligned(ip6_main.fibs, fib_table, CLIB_CACHE_LINE_BYTES);
111 memset(fib_table, 0, sizeof(*fib_table));
113 fib_table->ft_proto = FIB_PROTOCOL_IP6;
114 fib_table->ft_index =
115 fib_table->v6.index =
116 (fib_table - ip6_main.fibs);
118 hash_set(ip6_main.fib_index_by_table_id, table_id, fib_table->ft_index);
120 fib_table->ft_table_id =
121 fib_table->v6.table_id =
123 fib_table->ft_flow_hash_config =
124 fib_table->v6.flow_hash_config =
125 IP_FLOW_HASH_DEFAULT;
127 vnet_ip6_fib_init(fib_table->ft_index);
128 fib_table_lock(fib_table->ft_index, FIB_PROTOCOL_IP6);
130 return (fib_table->ft_index);
134 ip6_fib_table_find_or_create_and_lock (u32 table_id)
138 p = hash_get (ip6_main.fib_index_by_table_id, table_id);
140 return create_fib_with_table_id(table_id);
142 fib_table_lock(p[0], FIB_PROTOCOL_IP6);
148 ip6_fib_table_create_and_lock (void)
150 return (create_fib_with_table_id(~0));
154 ip6_fib_table_destroy (u32 fib_index)
157 .fp_proto = FIB_PROTOCOL_IP6,
169 fib_table_entry_special_remove(fib_index,
171 FIB_SOURCE_DEFAULT_ROUTE);
177 ip6_set_solicited_node_multicast_address(&pfx.fp_addr.ip6, 0);
179 fib_table_entry_special_remove(fib_index,
184 * all-routers multicast address
186 ip6_set_reserved_multicast_address (&pfx.fp_addr.ip6,
187 IP6_MULTICAST_SCOPE_link_local,
188 IP6_MULTICAST_GROUP_ID_all_routers);
190 fib_table_entry_special_remove(fib_index,
195 * all-nodes multicast address
197 ip6_set_reserved_multicast_address (&pfx.fp_addr.ip6,
198 IP6_MULTICAST_SCOPE_link_local,
199 IP6_MULTICAST_GROUP_ID_all_hosts);
201 fib_table_entry_special_remove(fib_index,
206 * all-mldv2 multicast address
208 ip6_set_reserved_multicast_address (&pfx.fp_addr.ip6,
209 IP6_MULTICAST_SCOPE_link_local,
210 IP6_MULTICAST_GROUP_ID_mldv2_routers);
212 fib_table_entry_special_remove(fib_index,
219 pfx.fp_addr.ip6.as_u64[0] = clib_host_to_net_u64 (0xFE80000000000000ULL);
220 pfx.fp_addr.ip6.as_u64[1] = 0;
222 fib_table_entry_special_remove(fib_index,
226 fib_table_t *fib_table = fib_table_get(fib_index, FIB_PROTOCOL_IP6);
230 * validate no more routes.
232 ASSERT(0 == fib_table->ft_total_route_counts);
233 FOR_EACH_FIB_SOURCE(source)
235 ASSERT(0 == fib_table->ft_src_route_counts[source]);
238 if (~0 != fib_table->ft_table_id)
240 hash_unset (ip6_main.fib_index_by_table_id, fib_table->ft_table_id);
242 pool_put(ip6_main.fibs, fib_table);
246 ip6_fib_table_lookup (u32 fib_index,
247 const ip6_address_t *addr,
250 const ip6_fib_table_instance_t *table;
251 BVT(clib_bihash_kv) kv, value;
255 table = &ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING];
256 n_p = vec_len (table->prefix_lengths_in_search_order);
258 kv.key[0] = addr->as_u64[0];
259 kv.key[1] = addr->as_u64[1];
260 fib = ((u64)((fib_index))<<32);
263 * start search from a mask length same length or shorter.
264 * we don't want matches longer than the mask passed
267 while (i < n_p && table->prefix_lengths_in_search_order[i] > len)
274 int dst_address_length = table->prefix_lengths_in_search_order[i];
275 ip6_address_t * mask = &ip6_main.fib_masks[dst_address_length];
277 ASSERT(dst_address_length >= 0 && dst_address_length <= 128);
278 //As lengths are decreasing, masks are increasingly specific.
279 kv.key[0] &= mask->as_u64[0];
280 kv.key[1] &= mask->as_u64[1];
281 kv.key[2] = fib | dst_address_length;
283 rv = BV(clib_bihash_search_inline_2)(&table->ip6_hash, &kv, &value);
288 return (FIB_NODE_INDEX_INVALID);
292 ip6_fib_table_lookup_exact_match (u32 fib_index,
293 const ip6_address_t *addr,
296 const ip6_fib_table_instance_t *table;
297 BVT(clib_bihash_kv) kv, value;
302 table = &ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING];
303 mask = &ip6_main.fib_masks[len];
304 fib = ((u64)((fib_index))<<32);
306 kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
307 kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
308 kv.key[2] = fib | len;
310 rv = BV(clib_bihash_search_inline_2)(&table->ip6_hash, &kv, &value);
314 return (FIB_NODE_INDEX_INVALID);
318 compute_prefix_lengths_in_search_order (ip6_fib_table_instance_t *table)
321 vec_reset_length (table->prefix_lengths_in_search_order);
322 /* Note: bitmap reversed so this is in fact a longest prefix match */
323 clib_bitmap_foreach (i, table->non_empty_dst_address_length_bitmap,
325 int dst_address_length = 128 - i;
326 vec_add1(table->prefix_lengths_in_search_order, dst_address_length);
331 ip6_fib_table_entry_remove (u32 fib_index,
332 const ip6_address_t *addr,
335 ip6_fib_table_instance_t *table;
336 BVT(clib_bihash_kv) kv;
340 table = &ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING];
341 mask = &ip6_main.fib_masks[len];
342 fib = ((u64)((fib_index))<<32);
344 kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
345 kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
346 kv.key[2] = fib | len;
348 BV(clib_bihash_add_del)(&table->ip6_hash, &kv, 0);
350 /* refcount accounting */
351 ASSERT (table->dst_address_length_refcounts[len] > 0);
352 if (--table->dst_address_length_refcounts[len] == 0)
354 table->non_empty_dst_address_length_bitmap =
355 clib_bitmap_set (table->non_empty_dst_address_length_bitmap,
357 compute_prefix_lengths_in_search_order (table);
362 ip6_fib_table_entry_insert (u32 fib_index,
363 const ip6_address_t *addr,
365 fib_node_index_t fib_entry_index)
367 ip6_fib_table_instance_t *table;
368 BVT(clib_bihash_kv) kv;
372 table = &ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING];
373 mask = &ip6_main.fib_masks[len];
374 fib = ((u64)((fib_index))<<32);
376 kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
377 kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
378 kv.key[2] = fib | len;
379 kv.value = fib_entry_index;
381 BV(clib_bihash_add_del)(&table->ip6_hash, &kv, 1);
383 table->dst_address_length_refcounts[len]++;
385 table->non_empty_dst_address_length_bitmap =
386 clib_bitmap_set (table->non_empty_dst_address_length_bitmap,
388 compute_prefix_lengths_in_search_order (table);
392 ip6_fib_table_fwding_lookup (ip6_main_t * im,
394 const ip6_address_t * dst)
396 const ip6_fib_table_instance_t *table;
399 BVT(clib_bihash_kv) kv, value;
402 table = &ip6_main.ip6_table[IP6_FIB_TABLE_FWDING];
403 len = vec_len (table->prefix_lengths_in_search_order);
405 kv.key[0] = dst->as_u64[0];
406 kv.key[1] = dst->as_u64[1];
407 fib = ((u64)((fib_index))<<32);
409 for (i = 0; i < len; i++)
411 int dst_address_length = table->prefix_lengths_in_search_order[i];
412 ip6_address_t * mask = &ip6_main.fib_masks[dst_address_length];
414 ASSERT(dst_address_length >= 0 && dst_address_length <= 128);
415 //As lengths are decreasing, masks are increasingly specific.
416 kv.key[0] &= mask->as_u64[0];
417 kv.key[1] &= mask->as_u64[1];
418 kv.key[2] = fib | dst_address_length;
420 rv = BV(clib_bihash_search_inline_2)(&table->ip6_hash, &kv, &value);
425 /* default route is always present */
430 u32 ip6_fib_table_fwding_lookup_with_if_index (ip6_main_t * im,
432 const ip6_address_t * dst)
434 u32 fib_index = vec_elt (im->fib_index_by_sw_if_index, sw_if_index);
435 return ip6_fib_table_fwding_lookup(im, fib_index, dst);
439 ip6_fib_table_get_flow_hash_config (u32 fib_index)
441 return (ip6_fib_get(fib_index)->flow_hash_config);
445 ip6_fib_table_get_index_for_sw_if_index (u32 sw_if_index)
447 if (sw_if_index >= vec_len(ip6_main.fib_index_by_sw_if_index))
450 * This is the case for interfaces that are not yet mapped to
455 return (ip6_main.fib_index_by_sw_if_index[sw_if_index]);
459 ip6_fib_table_fwding_dpo_update (u32 fib_index,
460 const ip6_address_t *addr,
464 ip6_fib_table_instance_t *table;
465 BVT(clib_bihash_kv) kv;
469 table = &ip6_main.ip6_table[IP6_FIB_TABLE_FWDING];
470 mask = &ip6_main.fib_masks[len];
471 fib = ((u64)((fib_index))<<32);
473 kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
474 kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
475 kv.key[2] = fib | len;
476 kv.value = dpo->dpoi_index;
478 BV(clib_bihash_add_del)(&table->ip6_hash, &kv, 1);
480 table->dst_address_length_refcounts[len]++;
482 table->non_empty_dst_address_length_bitmap =
483 clib_bitmap_set (table->non_empty_dst_address_length_bitmap,
485 compute_prefix_lengths_in_search_order (table);
489 ip6_fib_table_fwding_dpo_remove (u32 fib_index,
490 const ip6_address_t *addr,
494 ip6_fib_table_instance_t *table;
495 BVT(clib_bihash_kv) kv;
499 table = &ip6_main.ip6_table[IP6_FIB_TABLE_FWDING];
500 mask = &ip6_main.fib_masks[len];
501 fib = ((u64)((fib_index))<<32);
503 kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
504 kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
505 kv.key[2] = fib | len;
506 kv.value = dpo->dpoi_index;
508 BV(clib_bihash_add_del)(&table->ip6_hash, &kv, 0);
510 /* refcount accounting */
511 ASSERT (table->dst_address_length_refcounts[len] > 0);
512 if (--table->dst_address_length_refcounts[len] == 0)
514 table->non_empty_dst_address_length_bitmap =
515 clib_bitmap_set (table->non_empty_dst_address_length_bitmap,
517 compute_prefix_lengths_in_search_order (table);
521 typedef struct ip6_fib_show_ctx_t_ {
523 fib_node_index_t *entries;
524 } ip6_fib_show_ctx_t;
527 ip6_fib_table_collect_entries (clib_bihash_kv_24_8_t * kvp,
530 ip6_fib_show_ctx_t *ctx = arg;
532 if ((kvp->key[2] >> 32) == ctx->fib_index)
534 vec_add1(ctx->entries, kvp->value);
539 ip6_fib_table_show_all (ip6_fib_t *fib,
542 fib_node_index_t *fib_entry_index;
543 ip6_fib_show_ctx_t ctx = {
544 .fib_index = fib->index,
547 ip6_main_t *im = &ip6_main;
549 BV(clib_bihash_foreach_key_value_pair)(&im->ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash,
550 ip6_fib_table_collect_entries,
553 vec_sort_with_function(ctx.entries, fib_entry_cmp_for_sort);
555 vec_foreach(fib_entry_index, ctx.entries)
557 vlib_cli_output(vm, "%U",
560 FIB_ENTRY_FORMAT_BRIEF);
563 vec_free(ctx.entries);
567 ip6_fib_table_show_one (ip6_fib_t *fib,
569 ip6_address_t *address,
572 vlib_cli_output(vm, "%U",
574 ip6_fib_table_lookup(fib->index, address, mask_len),
575 FIB_ENTRY_FORMAT_DETAIL);
580 u64 count_by_prefix_length[129];
581 } count_routes_in_fib_at_prefix_length_arg_t;
583 static void count_routes_in_fib_at_prefix_length
584 (BVT(clib_bihash_kv) * kvp, void *arg)
586 count_routes_in_fib_at_prefix_length_arg_t * ap = arg;
589 if ((kvp->key[2]>>32) != ap->fib_index)
592 mask_width = kvp->key[2] & 0xFF;
594 ap->count_by_prefix_length[mask_width]++;
597 static clib_error_t *
598 ip6_show_fib (vlib_main_t * vm,
599 unformat_input_t * input,
600 vlib_cli_command_t * cmd)
602 count_routes_in_fib_at_prefix_length_arg_t _ca, *ca = &_ca;
603 ip6_main_t * im6 = &ip6_main;
604 fib_table_t *fib_table;
606 int verbose, matching;
607 ip6_address_t matching_address;
609 int table_id = -1, fib_index = ~0;
614 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
616 if (unformat (input, "brief") ||
617 unformat (input, "summary") ||
618 unformat (input, "sum"))
621 else if (unformat (input, "%U/%d",
622 unformat_ip6_address, &matching_address, &mask_len))
625 else if (unformat (input, "%U", unformat_ip6_address, &matching_address))
628 else if (unformat (input, "table %d", &table_id))
630 else if (unformat (input, "index %d", &fib_index))
636 pool_foreach (fib_table, im6->fibs,
638 fib = &(fib_table->v6);
639 if (table_id >= 0 && table_id != (int)fib->table_id)
641 if (fib_index != ~0 && fib_index != (int)fib->index)
644 vlib_cli_output (vm, "%s, fib_index %d, flow hash: %U",
645 fib_table->ft_desc, fib->index,
646 format_ip_flow_hash_config, fib->flow_hash_config);
651 BVT(clib_bihash) * h = &im6->ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash;
654 vlib_cli_output (vm, "%=20s%=16s", "Prefix length", "Count");
656 memset (ca, 0, sizeof(*ca));
657 ca->fib_index = fib->index;
659 BV(clib_bihash_foreach_key_value_pair)
660 (h, count_routes_in_fib_at_prefix_length, ca);
662 for (len = 128; len >= 0; len--)
664 if (ca->count_by_prefix_length[len])
665 vlib_cli_output (vm, "%=20d%=16lld",
666 len, ca->count_by_prefix_length[len]);
673 ip6_fib_table_show_all(fib, vm);
677 ip6_fib_table_show_one(fib, vm, &matching_address, mask_len);
685 * This command displays the IPv6 FIB Tables (VRF Tables) and the route
686 * entries for each table.
688 * @note This command will run for a long time when the FIB tables are
689 * comprised of millions of entries. For those senarios, consider displaying
694 * Example of how to display all the IPv6 FIB tables:
695 * @cliexstart{show ip6 fib}
696 * ipv6-VRF:0, fib_index 0, flow hash: src dst sport dport proto
699 * [@0]: dpo-load-balance: [index:5 buckets:1 uRPF:5 to:[0:0]]
700 * [0] [@0]: dpo-drop ip6
703 * [@0]: dpo-load-balance: [index:10 buckets:1 uRPF:10 to:[0:0]]
704 * [0] [@2]: dpo-receive
707 * [@0]: dpo-load-balance: [index:8 buckets:1 uRPF:8 to:[0:0]]
708 * [0] [@2]: dpo-receive
711 * [@0]: dpo-load-balance: [index:7 buckets:1 uRPF:7 to:[0:0]]
712 * [0] [@2]: dpo-receive
715 * [@0]: dpo-load-balance: [index:9 buckets:1 uRPF:9 to:[0:0]]
716 * [0] [@2]: dpo-receive
719 * [@0]: dpo-load-balance: [index:6 buckets:1 uRPF:6 to:[0:0]]
720 * [0] [@2]: dpo-receive
721 * ipv6-VRF:8, fib_index 1, flow hash: src dst sport dport proto
724 * [@0]: dpo-load-balance: [index:21 buckets:1 uRPF:20 to:[0:0]]
725 * [0] [@0]: dpo-drop ip6
728 * [@0]: dpo-load-balance: [index:27 buckets:1 uRPF:26 to:[0:0]]
729 * [0] [@4]: ipv6-glean: af_packet0
732 * [@0]: dpo-load-balance: [index:28 buckets:1 uRPF:27 to:[0:0]]
733 * [0] [@2]: dpo-receive: @::a:1:1:0:7 on af_packet0
736 * [@0]: dpo-load-balance: [index:26 buckets:1 uRPF:25 to:[0:0]]
737 * [0] [@2]: dpo-receive
738 * fe80::fe:3eff:fe3e:9222/128
740 * [@0]: dpo-load-balance: [index:29 buckets:1 uRPF:28 to:[0:0]]
741 * [0] [@2]: dpo-receive: fe80::fe:3eff:fe3e:9222 on af_packet0
744 * [@0]: dpo-load-balance: [index:24 buckets:1 uRPF:23 to:[0:0]]
745 * [0] [@2]: dpo-receive
748 * [@0]: dpo-load-balance: [index:23 buckets:1 uRPF:22 to:[0:0]]
749 * [0] [@2]: dpo-receive
752 * [@0]: dpo-load-balance: [index:25 buckets:1 uRPF:24 to:[0:0]]
753 * [0] [@2]: dpo-receive
756 * [@0]: dpo-load-balance: [index:22 buckets:1 uRPF:21 to:[0:0]]
757 * [0] [@2]: dpo-receive
760 * Example of how to display a summary of all IPv6 FIB tables:
761 * @cliexstart{show ip6 fib summary}
762 * ipv6-VRF:0, fib_index 0, flow hash: src dst sport dport proto
763 * Prefix length Count
768 * ipv6-VRF:8, fib_index 1, flow hash: src dst sport dport proto
769 * Prefix length Count
779 VLIB_CLI_COMMAND (ip6_show_fib_command, static) = {
780 .path = "show ip6 fib",
781 .short_help = "show ip6 fib [summary] [table <table-id>] [index <fib-id>] [<ip6-addr>[/<width>]]",
782 .function = ip6_show_fib,