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 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 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);
344 u32 ip6_fib_table_fwding_lookup_with_if_index (ip6_main_t * im,
346 const ip6_address_t * dst)
348 u32 fib_index = vec_elt (im->fib_index_by_sw_if_index, sw_if_index);
349 return ip6_fib_table_fwding_lookup(im, fib_index, dst);
353 ip6_fib_table_get_index_for_sw_if_index (u32 sw_if_index)
355 if (sw_if_index >= vec_len(ip6_main.fib_index_by_sw_if_index))
358 * This is the case for interfaces that are not yet mapped to
363 return (ip6_main.fib_index_by_sw_if_index[sw_if_index]);
367 ip6_fib_table_fwding_dpo_update (u32 fib_index,
368 const ip6_address_t *addr,
372 ip6_fib_table_instance_t *table;
373 BVT(clib_bihash_kv) kv;
377 table = &ip6_main.ip6_table[IP6_FIB_TABLE_FWDING];
378 mask = &ip6_main.fib_masks[len];
379 fib = ((u64)((fib_index))<<32);
381 kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
382 kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
383 kv.key[2] = fib | len;
384 kv.value = dpo->dpoi_index;
386 BV(clib_bihash_add_del)(&table->ip6_hash, &kv, 1);
388 table->dst_address_length_refcounts[len]++;
390 table->non_empty_dst_address_length_bitmap =
391 clib_bitmap_set (table->non_empty_dst_address_length_bitmap,
393 compute_prefix_lengths_in_search_order (table);
397 ip6_fib_table_fwding_dpo_remove (u32 fib_index,
398 const ip6_address_t *addr,
402 ip6_fib_table_instance_t *table;
403 BVT(clib_bihash_kv) kv;
407 table = &ip6_main.ip6_table[IP6_FIB_TABLE_FWDING];
408 mask = &ip6_main.fib_masks[len];
409 fib = ((u64)((fib_index))<<32);
411 kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
412 kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
413 kv.key[2] = fib | len;
414 kv.value = dpo->dpoi_index;
416 BV(clib_bihash_add_del)(&table->ip6_hash, &kv, 0);
418 /* refcount accounting */
419 ASSERT (table->dst_address_length_refcounts[len] > 0);
420 if (--table->dst_address_length_refcounts[len] == 0)
422 table->non_empty_dst_address_length_bitmap =
423 clib_bitmap_set (table->non_empty_dst_address_length_bitmap,
425 compute_prefix_lengths_in_search_order (table);
430 * @brief Context when walking the IPv6 table. Since all VRFs are in the
431 * same hash table, we need to filter only those we need as we walk
433 typedef struct ip6_fib_walk_ctx_t_
436 fib_table_walk_fn_t i6w_fn;
438 } ip6_fib_walk_ctx_t;
441 ip6_fib_walk_cb (clib_bihash_kv_24_8_t * kvp,
444 ip6_fib_walk_ctx_t *ctx = arg;
446 if ((kvp->key[2] >> 32) == ctx->i6w_fib_index)
448 ctx->i6w_fn(kvp->value, ctx->i6w_ctx);
455 ip6_fib_table_walk (u32 fib_index,
456 fib_table_walk_fn_t fn,
459 ip6_fib_walk_ctx_t ctx = {
460 .i6w_fib_index = fib_index,
464 ip6_main_t *im = &ip6_main;
466 BV(clib_bihash_foreach_key_value_pair)(&im->ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash,
472 typedef struct ip6_fib_show_ctx_t_ {
473 fib_node_index_t *entries;
474 } ip6_fib_show_ctx_t;
477 ip6_fib_table_show_walk (fib_node_index_t fib_entry_index,
480 ip6_fib_show_ctx_t *ctx = arg;
482 vec_add1(ctx->entries, fib_entry_index);
488 ip6_fib_table_show_all (ip6_fib_t *fib,
491 fib_node_index_t *fib_entry_index;
492 ip6_fib_show_ctx_t ctx = {
496 ip6_fib_table_walk(fib->index, ip6_fib_table_show_walk, &ctx);
497 vec_sort_with_function(ctx.entries, fib_entry_cmp_for_sort);
499 vec_foreach(fib_entry_index, ctx.entries)
501 vlib_cli_output(vm, "%U",
504 FIB_ENTRY_FORMAT_BRIEF);
507 vec_free(ctx.entries);
511 ip6_fib_table_show_one (ip6_fib_t *fib,
513 ip6_address_t *address,
517 vlib_cli_output(vm, "%U",
519 ip6_fib_table_lookup(fib->index, address, mask_len),
521 FIB_ENTRY_FORMAT_DETAIL2:
522 FIB_ENTRY_FORMAT_DETAIL));
527 u64 count_by_prefix_length[129];
528 } count_routes_in_fib_at_prefix_length_arg_t;
531 count_routes_in_fib_at_prefix_length (BVT(clib_bihash_kv) * kvp,
534 count_routes_in_fib_at_prefix_length_arg_t * ap = arg;
537 if ((kvp->key[2]>>32) != ap->fib_index)
540 mask_width = kvp->key[2] & 0xFF;
542 ap->count_by_prefix_length[mask_width]++;
545 static clib_error_t *
546 ip6_show_fib (vlib_main_t * vm,
547 unformat_input_t * input,
548 vlib_cli_command_t * cmd)
550 count_routes_in_fib_at_prefix_length_arg_t _ca, *ca = &_ca;
551 ip6_main_t * im6 = &ip6_main;
552 fib_table_t *fib_table;
554 int verbose, matching;
555 ip6_address_t matching_address;
557 int table_id = -1, fib_index = ~0;
563 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
565 if (unformat (input, "brief") ||
566 unformat (input, "summary") ||
567 unformat (input, "sum"))
570 else if (unformat (input, "detail") ||
571 unformat (input, "det"))
574 else if (unformat (input, "%U/%d",
575 unformat_ip6_address, &matching_address, &mask_len))
578 else if (unformat (input, "%U", unformat_ip6_address, &matching_address))
581 else if (unformat (input, "table %d", &table_id))
583 else if (unformat (input, "index %d", &fib_index))
589 pool_foreach (fib_table, im6->fibs,
591 fib = pool_elt_at_index(im6->v6_fibs, fib_table->ft_index);
592 if (table_id >= 0 && table_id != (int)fib->table_id)
594 if (fib_index != ~0 && fib_index != (int)fib->index)
597 vlib_cli_output (vm, "%s, fib_index:%d, flow hash:[%U] locks:%d",
598 fib_table->ft_desc, fib->index,
599 format_ip_flow_hash_config, fib_table->ft_flow_hash_config,
600 fib_table->ft_locks);
605 BVT(clib_bihash) * h = &im6->ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash;
608 vlib_cli_output (vm, "%=20s%=16s", "Prefix length", "Count");
610 memset (ca, 0, sizeof(*ca));
611 ca->fib_index = fib->index;
613 BV(clib_bihash_foreach_key_value_pair)
614 (h, count_routes_in_fib_at_prefix_length, ca);
616 for (len = 128; len >= 0; len--)
618 if (ca->count_by_prefix_length[len])
619 vlib_cli_output (vm, "%=20d%=16lld",
620 len, ca->count_by_prefix_length[len]);
627 ip6_fib_table_show_all(fib, vm);
631 ip6_fib_table_show_one(fib, vm, &matching_address, mask_len, detail);
639 * This command displays the IPv6 FIB Tables (VRF Tables) and the route
640 * entries for each table.
642 * @note This command will run for a long time when the FIB tables are
643 * comprised of millions of entries. For those senarios, consider displaying
648 * Example of how to display all the IPv6 FIB tables:
649 * @cliexstart{show ip6 fib}
650 * ipv6-VRF:0, fib_index 0, flow hash: src dst sport dport proto
653 * [@0]: dpo-load-balance: [index:5 buckets:1 uRPF:5 to:[0:0]]
654 * [0] [@0]: dpo-drop ip6
657 * [@0]: dpo-load-balance: [index:10 buckets:1 uRPF:10 to:[0:0]]
658 * [0] [@2]: dpo-receive
661 * [@0]: dpo-load-balance: [index:8 buckets:1 uRPF:8 to:[0:0]]
662 * [0] [@2]: dpo-receive
665 * [@0]: dpo-load-balance: [index:7 buckets:1 uRPF:7 to:[0:0]]
666 * [0] [@2]: dpo-receive
669 * [@0]: dpo-load-balance: [index:9 buckets:1 uRPF:9 to:[0:0]]
670 * [0] [@2]: dpo-receive
673 * [@0]: dpo-load-balance: [index:6 buckets:1 uRPF:6 to:[0:0]]
674 * [0] [@2]: dpo-receive
675 * ipv6-VRF:8, fib_index 1, flow hash: src dst sport dport proto
678 * [@0]: dpo-load-balance: [index:21 buckets:1 uRPF:20 to:[0:0]]
679 * [0] [@0]: dpo-drop ip6
682 * [@0]: dpo-load-balance: [index:27 buckets:1 uRPF:26 to:[0:0]]
683 * [0] [@4]: ipv6-glean: af_packet0
686 * [@0]: dpo-load-balance: [index:28 buckets:1 uRPF:27 to:[0:0]]
687 * [0] [@2]: dpo-receive: @::a:1:1:0:7 on af_packet0
690 * [@0]: dpo-load-balance: [index:26 buckets:1 uRPF:25 to:[0:0]]
691 * [0] [@2]: dpo-receive
692 * fe80::fe:3eff:fe3e:9222/128
694 * [@0]: dpo-load-balance: [index:29 buckets:1 uRPF:28 to:[0:0]]
695 * [0] [@2]: dpo-receive: fe80::fe:3eff:fe3e:9222 on af_packet0
698 * [@0]: dpo-load-balance: [index:24 buckets:1 uRPF:23 to:[0:0]]
699 * [0] [@2]: dpo-receive
702 * [@0]: dpo-load-balance: [index:23 buckets:1 uRPF:22 to:[0:0]]
703 * [0] [@2]: dpo-receive
706 * [@0]: dpo-load-balance: [index:25 buckets:1 uRPF:24 to:[0:0]]
707 * [0] [@2]: dpo-receive
710 * [@0]: dpo-load-balance: [index:22 buckets:1 uRPF:21 to:[0:0]]
711 * [0] [@2]: dpo-receive
714 * Example of how to display a summary of all IPv6 FIB tables:
715 * @cliexstart{show ip6 fib summary}
716 * ipv6-VRF:0, fib_index 0, flow hash: src dst sport dport proto
717 * Prefix length Count
722 * ipv6-VRF:8, fib_index 1, flow hash: src dst sport dport proto
723 * Prefix length Count
733 VLIB_CLI_COMMAND (ip6_show_fib_command, static) = {
734 .path = "show ip6 fib",
735 .short_help = "show ip6 fib [summary] [table <table-id>] [index <fib-id>] [<ip6-addr>[/<width>]] [detail]",
736 .function = ip6_show_fib,