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 * all link local for us
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_add(fib_index,
55 create_fib_with_table_id (u32 table_id)
57 fib_table_t *fib_table;
60 pool_get_aligned(ip6_main.fibs, fib_table, CLIB_CACHE_LINE_BYTES);
61 pool_get_aligned(ip6_main.v6_fibs, v6_fib, CLIB_CACHE_LINE_BYTES);
63 memset(fib_table, 0, sizeof(*fib_table));
64 memset(v6_fib, 0, sizeof(*v6_fib));
66 ASSERT((fib_table - ip6_main.fibs) ==
67 (v6_fib - ip6_main.v6_fibs));
69 fib_table->ft_proto = FIB_PROTOCOL_IP6;
72 (fib_table - ip6_main.fibs);
74 hash_set(ip6_main.fib_index_by_table_id, table_id, fib_table->ft_index);
76 fib_table->ft_table_id =
79 fib_table->ft_flow_hash_config =
80 v6_fib->flow_hash_config =
83 vnet_ip6_fib_init(fib_table->ft_index);
84 fib_table_lock(fib_table->ft_index, FIB_PROTOCOL_IP6);
86 return (fib_table->ft_index);
90 ip6_fib_table_find_or_create_and_lock (u32 table_id)
94 p = hash_get (ip6_main.fib_index_by_table_id, table_id);
96 return create_fib_with_table_id(table_id);
98 fib_table_lock(p[0], FIB_PROTOCOL_IP6);
104 ip6_fib_table_create_and_lock (void)
106 return (create_fib_with_table_id(~0));
110 ip6_fib_table_destroy (u32 fib_index)
113 .fp_proto = FIB_PROTOCOL_IP6,
125 fib_table_entry_special_remove(fib_index,
127 FIB_SOURCE_DEFAULT_ROUTE);
133 ip6_set_solicited_node_multicast_address(&pfx.fp_addr.ip6, 0);
135 fib_table_entry_special_remove(fib_index,
140 * all-routers multicast address
142 ip6_set_reserved_multicast_address (&pfx.fp_addr.ip6,
143 IP6_MULTICAST_SCOPE_link_local,
144 IP6_MULTICAST_GROUP_ID_all_routers);
146 fib_table_entry_special_remove(fib_index,
151 * all-nodes multicast address
153 ip6_set_reserved_multicast_address (&pfx.fp_addr.ip6,
154 IP6_MULTICAST_SCOPE_link_local,
155 IP6_MULTICAST_GROUP_ID_all_hosts);
157 fib_table_entry_special_remove(fib_index,
162 * all-mldv2 multicast address
164 ip6_set_reserved_multicast_address (&pfx.fp_addr.ip6,
165 IP6_MULTICAST_SCOPE_link_local,
166 IP6_MULTICAST_GROUP_ID_mldv2_routers);
168 fib_table_entry_special_remove(fib_index,
175 pfx.fp_addr.ip6.as_u64[0] = clib_host_to_net_u64 (0xFE80000000000000ULL);
176 pfx.fp_addr.ip6.as_u64[1] = 0;
178 fib_table_entry_special_remove(fib_index,
182 fib_table_t *fib_table = fib_table_get(fib_index, FIB_PROTOCOL_IP6);
186 * validate no more routes.
188 ASSERT(0 == fib_table->ft_total_route_counts);
189 FOR_EACH_FIB_SOURCE(source)
191 ASSERT(0 == fib_table->ft_src_route_counts[source]);
194 if (~0 != fib_table->ft_table_id)
196 hash_unset (ip6_main.fib_index_by_table_id, fib_table->ft_table_id);
198 pool_put_index(ip6_main.v6_fibs, fib_table->ft_index);
199 pool_put(ip6_main.fibs, fib_table);
203 ip6_fib_table_lookup (u32 fib_index,
204 const ip6_address_t *addr,
207 const ip6_fib_table_instance_t *table;
208 BVT(clib_bihash_kv) kv, value;
212 table = &ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING];
213 n_p = vec_len (table->prefix_lengths_in_search_order);
215 kv.key[0] = addr->as_u64[0];
216 kv.key[1] = addr->as_u64[1];
217 fib = ((u64)((fib_index))<<32);
220 * start search from a mask length same length or shorter.
221 * we don't want matches longer than the mask passed
224 while (i < n_p && table->prefix_lengths_in_search_order[i] > len)
231 int dst_address_length = table->prefix_lengths_in_search_order[i];
232 ip6_address_t * mask = &ip6_main.fib_masks[dst_address_length];
234 ASSERT(dst_address_length >= 0 && dst_address_length <= 128);
235 //As lengths are decreasing, masks are increasingly specific.
236 kv.key[0] &= mask->as_u64[0];
237 kv.key[1] &= mask->as_u64[1];
238 kv.key[2] = fib | dst_address_length;
240 rv = BV(clib_bihash_search_inline_2)(&table->ip6_hash, &kv, &value);
245 return (FIB_NODE_INDEX_INVALID);
249 ip6_fib_table_lookup_exact_match (u32 fib_index,
250 const ip6_address_t *addr,
253 const ip6_fib_table_instance_t *table;
254 BVT(clib_bihash_kv) kv, value;
259 table = &ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING];
260 mask = &ip6_main.fib_masks[len];
261 fib = ((u64)((fib_index))<<32);
263 kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
264 kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
265 kv.key[2] = fib | len;
267 rv = BV(clib_bihash_search_inline_2)(&table->ip6_hash, &kv, &value);
271 return (FIB_NODE_INDEX_INVALID);
275 compute_prefix_lengths_in_search_order (ip6_fib_table_instance_t *table)
278 vec_reset_length (table->prefix_lengths_in_search_order);
279 /* Note: bitmap reversed so this is in fact a longest prefix match */
280 clib_bitmap_foreach (i, table->non_empty_dst_address_length_bitmap,
282 int dst_address_length = 128 - i;
283 vec_add1(table->prefix_lengths_in_search_order, dst_address_length);
288 ip6_fib_table_entry_remove (u32 fib_index,
289 const ip6_address_t *addr,
292 ip6_fib_table_instance_t *table;
293 BVT(clib_bihash_kv) kv;
297 table = &ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING];
298 mask = &ip6_main.fib_masks[len];
299 fib = ((u64)((fib_index))<<32);
301 kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
302 kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
303 kv.key[2] = fib | len;
305 BV(clib_bihash_add_del)(&table->ip6_hash, &kv, 0);
307 /* refcount accounting */
308 ASSERT (table->dst_address_length_refcounts[len] > 0);
309 if (--table->dst_address_length_refcounts[len] == 0)
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);
319 ip6_fib_table_entry_insert (u32 fib_index,
320 const ip6_address_t *addr,
322 fib_node_index_t fib_entry_index)
324 ip6_fib_table_instance_t *table;
325 BVT(clib_bihash_kv) kv;
329 table = &ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING];
330 mask = &ip6_main.fib_masks[len];
331 fib = ((u64)((fib_index))<<32);
333 kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
334 kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
335 kv.key[2] = fib | len;
336 kv.value = fib_entry_index;
338 BV(clib_bihash_add_del)(&table->ip6_hash, &kv, 1);
340 table->dst_address_length_refcounts[len]++;
342 table->non_empty_dst_address_length_bitmap =
343 clib_bitmap_set (table->non_empty_dst_address_length_bitmap,
345 compute_prefix_lengths_in_search_order (table);
349 ip6_fib_table_fwding_lookup (ip6_main_t * im,
351 const ip6_address_t * dst)
353 const ip6_fib_table_instance_t *table;
356 BVT(clib_bihash_kv) kv, value;
359 table = &ip6_main.ip6_table[IP6_FIB_TABLE_FWDING];
360 len = vec_len (table->prefix_lengths_in_search_order);
362 kv.key[0] = dst->as_u64[0];
363 kv.key[1] = dst->as_u64[1];
364 fib = ((u64)((fib_index))<<32);
366 for (i = 0; i < len; i++)
368 int dst_address_length = table->prefix_lengths_in_search_order[i];
369 ip6_address_t * mask = &ip6_main.fib_masks[dst_address_length];
371 ASSERT(dst_address_length >= 0 && dst_address_length <= 128);
372 //As lengths are decreasing, masks are increasingly specific.
373 kv.key[0] &= mask->as_u64[0];
374 kv.key[1] &= mask->as_u64[1];
375 kv.key[2] = fib | dst_address_length;
377 rv = BV(clib_bihash_search_inline_2)(&table->ip6_hash, &kv, &value);
382 /* default route is always present */
387 u32 ip6_fib_table_fwding_lookup_with_if_index (ip6_main_t * im,
389 const ip6_address_t * dst)
391 u32 fib_index = vec_elt (im->fib_index_by_sw_if_index, sw_if_index);
392 return ip6_fib_table_fwding_lookup(im, fib_index, dst);
396 ip6_fib_table_get_flow_hash_config (u32 fib_index)
398 return (ip6_fib_get(fib_index)->flow_hash_config);
402 ip6_fib_table_get_index_for_sw_if_index (u32 sw_if_index)
404 if (sw_if_index >= vec_len(ip6_main.fib_index_by_sw_if_index))
407 * This is the case for interfaces that are not yet mapped to
412 return (ip6_main.fib_index_by_sw_if_index[sw_if_index]);
416 ip6_fib_table_fwding_dpo_update (u32 fib_index,
417 const ip6_address_t *addr,
421 ip6_fib_table_instance_t *table;
422 BVT(clib_bihash_kv) kv;
426 table = &ip6_main.ip6_table[IP6_FIB_TABLE_FWDING];
427 mask = &ip6_main.fib_masks[len];
428 fib = ((u64)((fib_index))<<32);
430 kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
431 kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
432 kv.key[2] = fib | len;
433 kv.value = dpo->dpoi_index;
435 BV(clib_bihash_add_del)(&table->ip6_hash, &kv, 1);
437 table->dst_address_length_refcounts[len]++;
439 table->non_empty_dst_address_length_bitmap =
440 clib_bitmap_set (table->non_empty_dst_address_length_bitmap,
442 compute_prefix_lengths_in_search_order (table);
446 ip6_fib_table_fwding_dpo_remove (u32 fib_index,
447 const ip6_address_t *addr,
451 ip6_fib_table_instance_t *table;
452 BVT(clib_bihash_kv) kv;
456 table = &ip6_main.ip6_table[IP6_FIB_TABLE_FWDING];
457 mask = &ip6_main.fib_masks[len];
458 fib = ((u64)((fib_index))<<32);
460 kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
461 kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
462 kv.key[2] = fib | len;
463 kv.value = dpo->dpoi_index;
465 BV(clib_bihash_add_del)(&table->ip6_hash, &kv, 0);
467 /* refcount accounting */
468 ASSERT (table->dst_address_length_refcounts[len] > 0);
469 if (--table->dst_address_length_refcounts[len] == 0)
471 table->non_empty_dst_address_length_bitmap =
472 clib_bitmap_set (table->non_empty_dst_address_length_bitmap,
474 compute_prefix_lengths_in_search_order (table);
479 * @brief Context when walking the IPv6 table. Since all VRFs are in the
480 * same hash table, we need to filter only those we need as we walk
482 typedef struct ip6_fib_walk_ctx_t_
485 fib_table_walk_fn_t i6w_fn;
487 } ip6_fib_walk_ctx_t;
490 ip6_fib_walk_cb (clib_bihash_kv_24_8_t * kvp,
493 ip6_fib_walk_ctx_t *ctx = arg;
495 if ((kvp->key[2] >> 32) == ctx->i6w_fib_index)
497 ctx->i6w_fn(kvp->value, ctx->i6w_ctx);
504 ip6_fib_table_walk (u32 fib_index,
505 fib_table_walk_fn_t fn,
508 ip6_fib_walk_ctx_t ctx = {
509 .i6w_fib_index = fib_index,
513 ip6_main_t *im = &ip6_main;
515 BV(clib_bihash_foreach_key_value_pair)(&im->ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash,
521 typedef struct ip6_fib_show_ctx_t_ {
522 fib_node_index_t *entries;
523 } ip6_fib_show_ctx_t;
526 ip6_fib_table_show_walk (fib_node_index_t fib_entry_index,
529 ip6_fib_show_ctx_t *ctx = arg;
531 vec_add1(ctx->entries, fib_entry_index);
537 ip6_fib_table_show_all (ip6_fib_t *fib,
540 fib_node_index_t *fib_entry_index;
541 ip6_fib_show_ctx_t ctx = {
545 ip6_fib_table_walk(fib->index, ip6_fib_table_show_walk, &ctx);
546 vec_sort_with_function(ctx.entries, fib_entry_cmp_for_sort);
548 vec_foreach(fib_entry_index, ctx.entries)
550 vlib_cli_output(vm, "%U",
553 FIB_ENTRY_FORMAT_BRIEF);
556 vec_free(ctx.entries);
560 ip6_fib_table_show_one (ip6_fib_t *fib,
562 ip6_address_t *address,
565 vlib_cli_output(vm, "%U",
567 ip6_fib_table_lookup(fib->index, address, mask_len),
568 FIB_ENTRY_FORMAT_DETAIL);
573 u64 count_by_prefix_length[129];
574 } count_routes_in_fib_at_prefix_length_arg_t;
576 static void count_routes_in_fib_at_prefix_length
577 (BVT(clib_bihash_kv) * kvp, void *arg)
579 count_routes_in_fib_at_prefix_length_arg_t * ap = arg;
582 if ((kvp->key[2]>>32) != ap->fib_index)
585 mask_width = kvp->key[2] & 0xFF;
587 ap->count_by_prefix_length[mask_width]++;
590 static clib_error_t *
591 ip6_show_fib (vlib_main_t * vm,
592 unformat_input_t * input,
593 vlib_cli_command_t * cmd)
595 count_routes_in_fib_at_prefix_length_arg_t _ca, *ca = &_ca;
596 ip6_main_t * im6 = &ip6_main;
597 fib_table_t *fib_table;
599 int verbose, matching;
600 ip6_address_t matching_address;
602 int table_id = -1, fib_index = ~0;
607 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
609 if (unformat (input, "brief") ||
610 unformat (input, "summary") ||
611 unformat (input, "sum"))
614 else if (unformat (input, "%U/%d",
615 unformat_ip6_address, &matching_address, &mask_len))
618 else if (unformat (input, "%U", unformat_ip6_address, &matching_address))
621 else if (unformat (input, "table %d", &table_id))
623 else if (unformat (input, "index %d", &fib_index))
629 pool_foreach (fib_table, im6->fibs,
631 fib = pool_elt_at_index(im6->v6_fibs, fib_table->ft_index);
632 if (table_id >= 0 && table_id != (int)fib->table_id)
634 if (fib_index != ~0 && fib_index != (int)fib->index)
637 vlib_cli_output (vm, "%s, fib_index %d, flow hash: %U",
638 fib_table->ft_desc, fib->index,
639 format_ip_flow_hash_config, fib->flow_hash_config);
644 BVT(clib_bihash) * h = &im6->ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash;
647 vlib_cli_output (vm, "%=20s%=16s", "Prefix length", "Count");
649 memset (ca, 0, sizeof(*ca));
650 ca->fib_index = fib->index;
652 BV(clib_bihash_foreach_key_value_pair)
653 (h, count_routes_in_fib_at_prefix_length, ca);
655 for (len = 128; len >= 0; len--)
657 if (ca->count_by_prefix_length[len])
658 vlib_cli_output (vm, "%=20d%=16lld",
659 len, ca->count_by_prefix_length[len]);
666 ip6_fib_table_show_all(fib, vm);
670 ip6_fib_table_show_one(fib, vm, &matching_address, mask_len);
678 * This command displays the IPv6 FIB Tables (VRF Tables) and the route
679 * entries for each table.
681 * @note This command will run for a long time when the FIB tables are
682 * comprised of millions of entries. For those senarios, consider displaying
687 * Example of how to display all the IPv6 FIB tables:
688 * @cliexstart{show ip6 fib}
689 * ipv6-VRF:0, fib_index 0, flow hash: src dst sport dport proto
692 * [@0]: dpo-load-balance: [index:5 buckets:1 uRPF:5 to:[0:0]]
693 * [0] [@0]: dpo-drop ip6
696 * [@0]: dpo-load-balance: [index:10 buckets:1 uRPF:10 to:[0:0]]
697 * [0] [@2]: dpo-receive
700 * [@0]: dpo-load-balance: [index:8 buckets:1 uRPF:8 to:[0:0]]
701 * [0] [@2]: dpo-receive
704 * [@0]: dpo-load-balance: [index:7 buckets:1 uRPF:7 to:[0:0]]
705 * [0] [@2]: dpo-receive
708 * [@0]: dpo-load-balance: [index:9 buckets:1 uRPF:9 to:[0:0]]
709 * [0] [@2]: dpo-receive
712 * [@0]: dpo-load-balance: [index:6 buckets:1 uRPF:6 to:[0:0]]
713 * [0] [@2]: dpo-receive
714 * ipv6-VRF:8, fib_index 1, flow hash: src dst sport dport proto
717 * [@0]: dpo-load-balance: [index:21 buckets:1 uRPF:20 to:[0:0]]
718 * [0] [@0]: dpo-drop ip6
721 * [@0]: dpo-load-balance: [index:27 buckets:1 uRPF:26 to:[0:0]]
722 * [0] [@4]: ipv6-glean: af_packet0
725 * [@0]: dpo-load-balance: [index:28 buckets:1 uRPF:27 to:[0:0]]
726 * [0] [@2]: dpo-receive: @::a:1:1:0:7 on af_packet0
729 * [@0]: dpo-load-balance: [index:26 buckets:1 uRPF:25 to:[0:0]]
730 * [0] [@2]: dpo-receive
731 * fe80::fe:3eff:fe3e:9222/128
733 * [@0]: dpo-load-balance: [index:29 buckets:1 uRPF:28 to:[0:0]]
734 * [0] [@2]: dpo-receive: fe80::fe:3eff:fe3e:9222 on af_packet0
737 * [@0]: dpo-load-balance: [index:24 buckets:1 uRPF:23 to:[0:0]]
738 * [0] [@2]: dpo-receive
741 * [@0]: dpo-load-balance: [index:23 buckets:1 uRPF:22 to:[0:0]]
742 * [0] [@2]: dpo-receive
745 * [@0]: dpo-load-balance: [index:25 buckets:1 uRPF:24 to:[0:0]]
746 * [0] [@2]: dpo-receive
749 * [@0]: dpo-load-balance: [index:22 buckets:1 uRPF:21 to:[0:0]]
750 * [0] [@2]: dpo-receive
753 * Example of how to display a summary of all IPv6 FIB tables:
754 * @cliexstart{show ip6 fib summary}
755 * ipv6-VRF:0, fib_index 0, flow hash: src dst sport dport proto
756 * Prefix length Count
761 * ipv6-VRF:8, fib_index 1, flow hash: src dst sport dport proto
762 * Prefix length Count
772 VLIB_CLI_COMMAND (ip6_show_fib_command, static) = {
773 .path = "show ip6 fib",
774 .short_help = "show ip6 fib [summary] [table <table-id>] [index <fib-id>] [<ip6-addr>[/<width>]]",
775 .function = ip6_show_fib,