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)
55 fib_table_t *fib_table;
58 pool_get_aligned(ip6_main.fibs, fib_table, CLIB_CACHE_LINE_BYTES);
59 pool_get_aligned(ip6_main.v6_fibs, v6_fib, CLIB_CACHE_LINE_BYTES);
61 memset(fib_table, 0, sizeof(*fib_table));
62 memset(v6_fib, 0, sizeof(*v6_fib));
64 ASSERT((fib_table - ip6_main.fibs) ==
65 (v6_fib - ip6_main.v6_fibs));
67 fib_table->ft_proto = FIB_PROTOCOL_IP6;
70 (fib_table - ip6_main.fibs);
72 hash_set(ip6_main.fib_index_by_table_id, table_id, fib_table->ft_index);
74 fib_table->ft_table_id =
77 fib_table->ft_flow_hash_config = IP_FLOW_HASH_DEFAULT;
79 vnet_ip6_fib_init(fib_table->ft_index);
80 fib_table_lock(fib_table->ft_index, FIB_PROTOCOL_IP6);
82 return (fib_table->ft_index);
86 ip6_fib_table_find_or_create_and_lock (u32 table_id)
90 p = hash_get (ip6_main.fib_index_by_table_id, table_id);
92 return create_fib_with_table_id(table_id);
94 fib_table_lock(p[0], FIB_PROTOCOL_IP6);
100 ip6_fib_table_create_and_lock (void)
102 return (create_fib_with_table_id(~0));
106 ip6_fib_table_destroy (u32 fib_index)
109 .fp_proto = FIB_PROTOCOL_IP6,
121 fib_table_entry_special_remove(fib_index,
123 FIB_SOURCE_DEFAULT_ROUTE);
129 ip6_set_solicited_node_multicast_address(&pfx.fp_addr.ip6, 0);
131 fib_table_entry_special_remove(fib_index,
136 * all-routers multicast address
138 ip6_set_reserved_multicast_address (&pfx.fp_addr.ip6,
139 IP6_MULTICAST_SCOPE_link_local,
140 IP6_MULTICAST_GROUP_ID_all_routers);
142 fib_table_entry_special_remove(fib_index,
147 * all-nodes multicast address
149 ip6_set_reserved_multicast_address (&pfx.fp_addr.ip6,
150 IP6_MULTICAST_SCOPE_link_local,
151 IP6_MULTICAST_GROUP_ID_all_hosts);
153 fib_table_entry_special_remove(fib_index,
158 * all-mldv2 multicast address
160 ip6_set_reserved_multicast_address (&pfx.fp_addr.ip6,
161 IP6_MULTICAST_SCOPE_link_local,
162 IP6_MULTICAST_GROUP_ID_mldv2_routers);
164 fib_table_entry_special_remove(fib_index,
171 pfx.fp_addr.ip6.as_u64[0] = clib_host_to_net_u64 (0xFE80000000000000ULL);
172 pfx.fp_addr.ip6.as_u64[1] = 0;
174 fib_table_entry_special_remove(fib_index,
178 fib_table_t *fib_table = fib_table_get(fib_index, FIB_PROTOCOL_IP6);
182 * validate no more routes.
184 ASSERT(0 == fib_table->ft_total_route_counts);
185 FOR_EACH_FIB_SOURCE(source)
187 ASSERT(0 == fib_table->ft_src_route_counts[source]);
190 if (~0 != fib_table->ft_table_id)
192 hash_unset (ip6_main.fib_index_by_table_id, fib_table->ft_table_id);
194 pool_put_index(ip6_main.v6_fibs, fib_table->ft_index);
195 pool_put(ip6_main.fibs, fib_table);
199 ip6_fib_table_lookup (u32 fib_index,
200 const ip6_address_t *addr,
203 const ip6_fib_table_instance_t *table;
204 BVT(clib_bihash_kv) kv, value;
208 table = &ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING];
209 n_p = vec_len (table->prefix_lengths_in_search_order);
211 kv.key[0] = addr->as_u64[0];
212 kv.key[1] = addr->as_u64[1];
213 fib = ((u64)((fib_index))<<32);
216 * start search from a mask length same length or shorter.
217 * we don't want matches longer than the mask passed
220 while (i < n_p && table->prefix_lengths_in_search_order[i] > len)
227 int dst_address_length = table->prefix_lengths_in_search_order[i];
228 ip6_address_t * mask = &ip6_main.fib_masks[dst_address_length];
230 ASSERT(dst_address_length >= 0 && dst_address_length <= 128);
231 //As lengths are decreasing, masks are increasingly specific.
232 kv.key[0] &= mask->as_u64[0];
233 kv.key[1] &= mask->as_u64[1];
234 kv.key[2] = fib | dst_address_length;
236 rv = BV(clib_bihash_search_inline_2)(&table->ip6_hash, &kv, &value);
241 return (FIB_NODE_INDEX_INVALID);
245 ip6_fib_table_lookup_exact_match (u32 fib_index,
246 const ip6_address_t *addr,
249 const ip6_fib_table_instance_t *table;
250 BVT(clib_bihash_kv) kv, value;
255 table = &ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING];
256 mask = &ip6_main.fib_masks[len];
257 fib = ((u64)((fib_index))<<32);
259 kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
260 kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
261 kv.key[2] = fib | len;
263 rv = BV(clib_bihash_search_inline_2)(&table->ip6_hash, &kv, &value);
267 return (FIB_NODE_INDEX_INVALID);
271 compute_prefix_lengths_in_search_order (ip6_fib_table_instance_t *table)
274 vec_reset_length (table->prefix_lengths_in_search_order);
275 /* Note: bitmap reversed so this is in fact a longest prefix match */
276 clib_bitmap_foreach (i, table->non_empty_dst_address_length_bitmap,
278 int dst_address_length = 128 - i;
279 vec_add1(table->prefix_lengths_in_search_order, dst_address_length);
284 ip6_fib_table_entry_remove (u32 fib_index,
285 const ip6_address_t *addr,
288 ip6_fib_table_instance_t *table;
289 BVT(clib_bihash_kv) kv;
293 table = &ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING];
294 mask = &ip6_main.fib_masks[len];
295 fib = ((u64)((fib_index))<<32);
297 kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
298 kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
299 kv.key[2] = fib | len;
301 BV(clib_bihash_add_del)(&table->ip6_hash, &kv, 0);
303 /* refcount accounting */
304 ASSERT (table->dst_address_length_refcounts[len] > 0);
305 if (--table->dst_address_length_refcounts[len] == 0)
307 table->non_empty_dst_address_length_bitmap =
308 clib_bitmap_set (table->non_empty_dst_address_length_bitmap,
310 compute_prefix_lengths_in_search_order (table);
315 ip6_fib_table_entry_insert (u32 fib_index,
316 const ip6_address_t *addr,
318 fib_node_index_t fib_entry_index)
320 ip6_fib_table_instance_t *table;
321 BVT(clib_bihash_kv) kv;
325 table = &ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING];
326 mask = &ip6_main.fib_masks[len];
327 fib = ((u64)((fib_index))<<32);
329 kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
330 kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
331 kv.key[2] = fib | len;
332 kv.value = fib_entry_index;
334 BV(clib_bihash_add_del)(&table->ip6_hash, &kv, 1);
336 table->dst_address_length_refcounts[len]++;
338 table->non_empty_dst_address_length_bitmap =
339 clib_bitmap_set (table->non_empty_dst_address_length_bitmap,
341 compute_prefix_lengths_in_search_order (table);
345 ip6_fib_table_fwding_lookup (ip6_main_t * im,
347 const ip6_address_t * dst)
349 const ip6_fib_table_instance_t *table;
352 BVT(clib_bihash_kv) kv, value;
355 table = &ip6_main.ip6_table[IP6_FIB_TABLE_FWDING];
356 len = vec_len (table->prefix_lengths_in_search_order);
358 kv.key[0] = dst->as_u64[0];
359 kv.key[1] = dst->as_u64[1];
360 fib = ((u64)((fib_index))<<32);
362 for (i = 0; i < len; i++)
364 int dst_address_length = table->prefix_lengths_in_search_order[i];
365 ip6_address_t * mask = &ip6_main.fib_masks[dst_address_length];
367 ASSERT(dst_address_length >= 0 && dst_address_length <= 128);
368 //As lengths are decreasing, masks are increasingly specific.
369 kv.key[0] &= mask->as_u64[0];
370 kv.key[1] &= mask->as_u64[1];
371 kv.key[2] = fib | dst_address_length;
373 rv = BV(clib_bihash_search_inline_2)(&table->ip6_hash, &kv, &value);
378 /* default route is always present */
383 u32 ip6_fib_table_fwding_lookup_with_if_index (ip6_main_t * im,
385 const ip6_address_t * dst)
387 u32 fib_index = vec_elt (im->fib_index_by_sw_if_index, sw_if_index);
388 return ip6_fib_table_fwding_lookup(im, fib_index, dst);
392 ip6_fib_table_get_index_for_sw_if_index (u32 sw_if_index)
394 if (sw_if_index >= vec_len(ip6_main.fib_index_by_sw_if_index))
397 * This is the case for interfaces that are not yet mapped to
402 return (ip6_main.fib_index_by_sw_if_index[sw_if_index]);
406 ip6_fib_table_fwding_dpo_update (u32 fib_index,
407 const ip6_address_t *addr,
411 ip6_fib_table_instance_t *table;
412 BVT(clib_bihash_kv) kv;
416 table = &ip6_main.ip6_table[IP6_FIB_TABLE_FWDING];
417 mask = &ip6_main.fib_masks[len];
418 fib = ((u64)((fib_index))<<32);
420 kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
421 kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
422 kv.key[2] = fib | len;
423 kv.value = dpo->dpoi_index;
425 BV(clib_bihash_add_del)(&table->ip6_hash, &kv, 1);
427 table->dst_address_length_refcounts[len]++;
429 table->non_empty_dst_address_length_bitmap =
430 clib_bitmap_set (table->non_empty_dst_address_length_bitmap,
432 compute_prefix_lengths_in_search_order (table);
436 ip6_fib_table_fwding_dpo_remove (u32 fib_index,
437 const ip6_address_t *addr,
441 ip6_fib_table_instance_t *table;
442 BVT(clib_bihash_kv) kv;
446 table = &ip6_main.ip6_table[IP6_FIB_TABLE_FWDING];
447 mask = &ip6_main.fib_masks[len];
448 fib = ((u64)((fib_index))<<32);
450 kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
451 kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
452 kv.key[2] = fib | len;
453 kv.value = dpo->dpoi_index;
455 BV(clib_bihash_add_del)(&table->ip6_hash, &kv, 0);
457 /* refcount accounting */
458 ASSERT (table->dst_address_length_refcounts[len] > 0);
459 if (--table->dst_address_length_refcounts[len] == 0)
461 table->non_empty_dst_address_length_bitmap =
462 clib_bitmap_set (table->non_empty_dst_address_length_bitmap,
464 compute_prefix_lengths_in_search_order (table);
469 * @brief Context when walking the IPv6 table. Since all VRFs are in the
470 * same hash table, we need to filter only those we need as we walk
472 typedef struct ip6_fib_walk_ctx_t_
475 fib_table_walk_fn_t i6w_fn;
477 } ip6_fib_walk_ctx_t;
480 ip6_fib_walk_cb (clib_bihash_kv_24_8_t * kvp,
483 ip6_fib_walk_ctx_t *ctx = arg;
485 if ((kvp->key[2] >> 32) == ctx->i6w_fib_index)
487 ctx->i6w_fn(kvp->value, ctx->i6w_ctx);
494 ip6_fib_table_walk (u32 fib_index,
495 fib_table_walk_fn_t fn,
498 ip6_fib_walk_ctx_t ctx = {
499 .i6w_fib_index = fib_index,
503 ip6_main_t *im = &ip6_main;
505 BV(clib_bihash_foreach_key_value_pair)(&im->ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash,
511 typedef struct ip6_fib_show_ctx_t_ {
512 fib_node_index_t *entries;
513 } ip6_fib_show_ctx_t;
516 ip6_fib_table_show_walk (fib_node_index_t fib_entry_index,
519 ip6_fib_show_ctx_t *ctx = arg;
521 vec_add1(ctx->entries, fib_entry_index);
527 ip6_fib_table_show_all (ip6_fib_t *fib,
530 fib_node_index_t *fib_entry_index;
531 ip6_fib_show_ctx_t ctx = {
535 ip6_fib_table_walk(fib->index, ip6_fib_table_show_walk, &ctx);
536 vec_sort_with_function(ctx.entries, fib_entry_cmp_for_sort);
538 vec_foreach(fib_entry_index, ctx.entries)
540 vlib_cli_output(vm, "%U",
543 FIB_ENTRY_FORMAT_BRIEF);
546 vec_free(ctx.entries);
550 ip6_fib_table_show_one (ip6_fib_t *fib,
552 ip6_address_t *address,
556 vlib_cli_output(vm, "%U",
558 ip6_fib_table_lookup(fib->index, address, mask_len),
560 FIB_ENTRY_FORMAT_DETAIL2:
561 FIB_ENTRY_FORMAT_DETAIL));
566 u64 count_by_prefix_length[129];
567 } count_routes_in_fib_at_prefix_length_arg_t;
570 count_routes_in_fib_at_prefix_length (BVT(clib_bihash_kv) * kvp,
573 count_routes_in_fib_at_prefix_length_arg_t * ap = arg;
576 if ((kvp->key[2]>>32) != ap->fib_index)
579 mask_width = kvp->key[2] & 0xFF;
581 ap->count_by_prefix_length[mask_width]++;
584 static clib_error_t *
585 ip6_show_fib (vlib_main_t * vm,
586 unformat_input_t * input,
587 vlib_cli_command_t * cmd)
589 count_routes_in_fib_at_prefix_length_arg_t _ca, *ca = &_ca;
590 ip6_main_t * im6 = &ip6_main;
591 fib_table_t *fib_table;
593 int verbose, matching;
594 ip6_address_t matching_address;
596 int table_id = -1, fib_index = ~0;
602 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
604 if (unformat (input, "brief") ||
605 unformat (input, "summary") ||
606 unformat (input, "sum"))
609 else if (unformat (input, "detail") ||
610 unformat (input, "det"))
613 else if (unformat (input, "%U/%d",
614 unformat_ip6_address, &matching_address, &mask_len))
617 else if (unformat (input, "%U", unformat_ip6_address, &matching_address))
620 else if (unformat (input, "table %d", &table_id))
622 else if (unformat (input, "index %d", &fib_index))
628 pool_foreach (fib_table, im6->fibs,
630 fib = pool_elt_at_index(im6->v6_fibs, fib_table->ft_index);
631 if (table_id >= 0 && table_id != (int)fib->table_id)
633 if (fib_index != ~0 && fib_index != (int)fib->index)
636 vlib_cli_output (vm, "%s, fib_index %d, flow hash: %U",
637 fib_table->ft_desc, fib->index,
638 format_ip_flow_hash_config, fib_table->ft_flow_hash_config);
643 BVT(clib_bihash) * h = &im6->ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash;
646 vlib_cli_output (vm, "%=20s%=16s", "Prefix length", "Count");
648 memset (ca, 0, sizeof(*ca));
649 ca->fib_index = fib->index;
651 BV(clib_bihash_foreach_key_value_pair)
652 (h, count_routes_in_fib_at_prefix_length, ca);
654 for (len = 128; len >= 0; len--)
656 if (ca->count_by_prefix_length[len])
657 vlib_cli_output (vm, "%=20d%=16lld",
658 len, ca->count_by_prefix_length[len]);
665 ip6_fib_table_show_all(fib, vm);
669 ip6_fib_table_show_one(fib, vm, &matching_address, mask_len, detail);
677 * This command displays the IPv6 FIB Tables (VRF Tables) and the route
678 * entries for each table.
680 * @note This command will run for a long time when the FIB tables are
681 * comprised of millions of entries. For those senarios, consider displaying
686 * Example of how to display all the IPv6 FIB tables:
687 * @cliexstart{show ip6 fib}
688 * ipv6-VRF:0, fib_index 0, flow hash: src dst sport dport proto
691 * [@0]: dpo-load-balance: [index:5 buckets:1 uRPF:5 to:[0:0]]
692 * [0] [@0]: dpo-drop ip6
695 * [@0]: dpo-load-balance: [index:10 buckets:1 uRPF:10 to:[0:0]]
696 * [0] [@2]: dpo-receive
699 * [@0]: dpo-load-balance: [index:8 buckets:1 uRPF:8 to:[0:0]]
700 * [0] [@2]: dpo-receive
703 * [@0]: dpo-load-balance: [index:7 buckets:1 uRPF:7 to:[0:0]]
704 * [0] [@2]: dpo-receive
707 * [@0]: dpo-load-balance: [index:9 buckets:1 uRPF:9 to:[0:0]]
708 * [0] [@2]: dpo-receive
711 * [@0]: dpo-load-balance: [index:6 buckets:1 uRPF:6 to:[0:0]]
712 * [0] [@2]: dpo-receive
713 * ipv6-VRF:8, fib_index 1, flow hash: src dst sport dport proto
716 * [@0]: dpo-load-balance: [index:21 buckets:1 uRPF:20 to:[0:0]]
717 * [0] [@0]: dpo-drop ip6
720 * [@0]: dpo-load-balance: [index:27 buckets:1 uRPF:26 to:[0:0]]
721 * [0] [@4]: ipv6-glean: af_packet0
724 * [@0]: dpo-load-balance: [index:28 buckets:1 uRPF:27 to:[0:0]]
725 * [0] [@2]: dpo-receive: @::a:1:1:0:7 on af_packet0
728 * [@0]: dpo-load-balance: [index:26 buckets:1 uRPF:25 to:[0:0]]
729 * [0] [@2]: dpo-receive
730 * fe80::fe:3eff:fe3e:9222/128
732 * [@0]: dpo-load-balance: [index:29 buckets:1 uRPF:28 to:[0:0]]
733 * [0] [@2]: dpo-receive: fe80::fe:3eff:fe3e:9222 on af_packet0
736 * [@0]: dpo-load-balance: [index:24 buckets:1 uRPF:23 to:[0:0]]
737 * [0] [@2]: dpo-receive
740 * [@0]: dpo-load-balance: [index:23 buckets:1 uRPF:22 to:[0:0]]
741 * [0] [@2]: dpo-receive
744 * [@0]: dpo-load-balance: [index:25 buckets:1 uRPF:24 to:[0:0]]
745 * [0] [@2]: dpo-receive
748 * [@0]: dpo-load-balance: [index:22 buckets:1 uRPF:21 to:[0:0]]
749 * [0] [@2]: dpo-receive
752 * Example of how to display a summary of all IPv6 FIB tables:
753 * @cliexstart{show ip6 fib summary}
754 * ipv6-VRF:0, fib_index 0, flow hash: src dst sport dport proto
755 * Prefix length Count
760 * ipv6-VRF:8, fib_index 1, flow hash: src dst sport dport proto
761 * Prefix length Count
771 VLIB_CLI_COMMAND (ip6_show_fib_command, static) = {
772 .path = "show ip6 fib",
773 .short_help = "show ip6 fib [summary] [table <table-id>] [index <fib-id>] [<ip6-addr>[/<width>]] [detail]",
774 .function = ip6_show_fib,