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/mfib/ip6_mfib.h>
18 #include <vnet/mfib/mfib_table.h>
19 #include <vnet/mfib/mfib_entry.h>
20 #include <vnet/fib/ip6_fib.h>
22 ip6_mfib_table_instance_t ip6_mfib_table;
25 * Key and mask for radix
27 typedef clib_bihash_kv_40_8_t ip6_mfib_key_t;
29 static const mfib_prefix_t all_zeros = {
38 .fp_proto = FIB_PROTOCOL_IP6,
41 typedef enum ip6_mfib_special_type_t_ {
42 IP6_MFIB_SPECIAL_TYPE_NONE,
43 IP6_MFIB_SPECIAL_TYPE_SOLICITED,
44 } ip6_mfib_special_type_t;
46 typedef struct ip6_mfib_special_t_ {
48 * @brief solicited or not
50 ip6_mfib_special_type_t ims_type;
53 * @brief the Prefix length
58 * @brief The last byte of the mcast address
62 * @brief The scope of the address
67 static const ip6_mfib_special_t ip6_mfib_specials[] =
71 * Add ff02::1:ff00:0/104 via local route for all tables.
72 * This is required for neighbor discovery to work.
74 .ims_type = IP6_MFIB_SPECIAL_TYPE_SOLICITED,
79 * all-routers multicast address
81 .ims_type = IP6_MFIB_SPECIAL_TYPE_NONE,
82 .ims_scope = IP6_MULTICAST_SCOPE_link_local,
83 .ims_byte = IP6_MULTICAST_GROUP_ID_all_routers,
88 * all-nodes multicast address
90 .ims_type = IP6_MFIB_SPECIAL_TYPE_NONE,
91 .ims_scope = IP6_MULTICAST_SCOPE_link_local,
92 .ims_byte = IP6_MULTICAST_GROUP_ID_all_hosts,
97 * Add all-mldv2 multicast address via local route for all tables
99 .ims_type = IP6_MFIB_SPECIAL_TYPE_NONE,
101 .ims_scope = IP6_MULTICAST_SCOPE_link_local,
102 .ims_byte = IP6_MULTICAST_GROUP_ID_mldv2_routers,
106 #define FOR_EACH_IP6_SPECIAL(_pfx, _body) \
108 const ip6_mfib_special_t *_spec; \
111 _ii < ARRAY_LEN(ip6_mfib_specials); \
114 _spec = &ip6_mfib_specials[_ii]; \
115 if (IP6_MFIB_SPECIAL_TYPE_SOLICITED == _spec->ims_type) \
117 ip6_set_solicited_node_multicast_address( \
118 &(_pfx)->fp_grp_addr.ip6, 0); \
122 ip6_set_reserved_multicast_address ( \
123 &(_pfx)->fp_grp_addr.ip6, \
127 (_pfx)->fp_len = _spec->ims_len; \
128 do { _body; } while (0); \
134 ip6_create_mfib_with_table_id (u32 table_id,
137 mfib_table_t *mfib_table;
138 mfib_prefix_t pfx = {
139 .fp_proto = FIB_PROTOCOL_IP6,
141 const fib_route_path_t path_for_us = {
142 .frp_proto = DPO_PROTO_IP6,
143 .frp_addr = zero_addr,
144 .frp_sw_if_index = 0xffffffff,
147 .frp_flags = FIB_ROUTE_PATH_LOCAL,
148 .frp_mitf_flags = MFIB_ITF_FLAG_FORWARD,
151 pool_get_aligned(ip6_main.mfibs, mfib_table, CLIB_CACHE_LINE_BYTES);
152 clib_memset(mfib_table, 0, sizeof(*mfib_table));
154 mfib_table->mft_proto = FIB_PROTOCOL_IP6;
155 mfib_table->mft_index =
156 mfib_table->v6.index =
157 (mfib_table - ip6_main.mfibs);
159 hash_set (ip6_main.mfib_index_by_table_id,
161 mfib_table->mft_index);
163 mfib_table->mft_table_id =
164 mfib_table->v6.table_id =
167 mfib_table_lock(mfib_table->mft_index, FIB_PROTOCOL_IP6, src);
170 * add the special entries into the new FIB
172 mfib_table_entry_update(mfib_table->mft_index,
174 MFIB_SOURCE_DEFAULT_ROUTE,
176 MFIB_ENTRY_FLAG_DROP);
179 * Add each of the specials
181 FOR_EACH_IP6_SPECIAL(&pfx,
183 mfib_table_entry_path_update(mfib_table->mft_index,
189 return (mfib_table->mft_index);
193 ip6_mfib_table_destroy (ip6_mfib_t *mfib)
195 mfib_table_t *mfib_table = (mfib_table_t*)mfib;
196 fib_node_index_t mfei;
197 mfib_prefix_t pfx = {
198 .fp_proto = FIB_PROTOCOL_IP6,
200 const fib_route_path_t path_for_us = {
201 .frp_proto = DPO_PROTO_IP6,
202 .frp_addr = zero_addr,
203 .frp_sw_if_index = 0xffffffff,
206 .frp_flags = FIB_ROUTE_PATH_LOCAL,
210 * remove all the specials we added when the table was created.
212 FOR_EACH_IP6_SPECIAL(&pfx,
214 mfib_table_entry_path_remove(mfib_table->mft_index,
220 mfei = mfib_table_lookup_exact_match(mfib_table->mft_index, &all_zeros);
221 mfib_table_entry_delete_index(mfei, MFIB_SOURCE_DEFAULT_ROUTE);
224 * validate no more routes.
226 ASSERT(0 == mfib_table->mft_total_route_counts);
227 ASSERT(~0 != mfib_table->mft_table_id);
229 hash_unset (ip6_main.mfib_index_by_table_id, mfib_table->mft_table_id);
230 pool_put(ip6_main.mfibs, mfib_table);
234 ip6_mfib_interface_enable_disable (u32 sw_if_index, int is_enable)
236 const fib_route_path_t path = {
237 .frp_proto = DPO_PROTO_IP6,
238 .frp_addr = zero_addr,
239 .frp_sw_if_index = sw_if_index,
242 .frp_mitf_flags = MFIB_ITF_FLAG_ACCEPT,
244 mfib_prefix_t pfx = {
245 .fp_proto = FIB_PROTOCOL_IP6,
249 vec_validate (ip6_main.mfib_index_by_sw_if_index, sw_if_index);
250 mfib_index = ip6_mfib_table_get_index_for_sw_if_index(sw_if_index);
254 FOR_EACH_IP6_SPECIAL(&pfx,
256 mfib_table_entry_path_update(mfib_index,
264 FOR_EACH_IP6_SPECIAL(&pfx,
266 mfib_table_entry_path_remove(mfib_index,
275 ip6_mfib_table_find_or_create_and_lock (u32 table_id,
280 index = ip6_mfib_index_from_table_id(table_id);
282 return ip6_create_mfib_with_table_id(table_id, src);
283 mfib_table_lock(index, FIB_PROTOCOL_IP6, src);
289 ip6_mfib_table_get_index_for_sw_if_index (u32 sw_if_index)
291 if (sw_if_index >= vec_len(ip6_main.mfib_index_by_sw_if_index))
294 * This is the case for interfaces that are not yet mapped to
299 return (ip6_main.mfib_index_by_sw_if_index[sw_if_index]);
302 #define IPV6_MFIB_GRP_LEN(_len) \
303 (_len > 128 ? 128 : _len)
305 #define IP6_MFIB_MK_KEY(_mfib, _grp, _src, _len, _key) \
307 _key.key[0] = (_grp->as_u64[0] & \
308 ip6_main.fib_masks[IPV6_MFIB_GRP_LEN(_len)].as_u64[0]); \
309 _key.key[1] = (_grp->as_u64[1] & \
310 ip6_main.fib_masks[IPV6_MFIB_GRP_LEN(_len)].as_u64[1]); \
312 _key.key[2] = _src->as_u64[0]; \
313 _key.key[3] = _src->as_u64[1]; \
318 _key.key[4] = _mfib->index; \
319 _key.key[4] = (_key.key[4] << 32) | len; \
323 * ip6_fib_table_lookup_exact_match
325 * Exact match prefix lookup
328 ip6_mfib_table_lookup_exact_match (const ip6_mfib_t *mfib,
329 const ip6_address_t *grp,
330 const ip6_address_t *src,
333 ip6_mfib_key_t key, value;
336 IP6_MFIB_MK_KEY(mfib, grp, src, len, key);
338 rv = clib_bihash_search_inline_2_40_8(&ip6_mfib_table.ip6_mhash,
343 return (FIB_NODE_INDEX_INVALID);
347 * ip6_fib_table_lookup
349 * Longest prefix match for the forwarding plane (no mask given)
352 ip6_mfib_table_fwd_lookup (const ip6_mfib_t *mfib,
353 const ip6_address_t *src,
354 const ip6_address_t *grp)
356 ip6_mfib_table_instance_t *table;
357 ip6_mfib_key_t key, value;
361 table = &ip6_mfib_table;
362 n = vec_len (table->prefix_lengths_in_search_order);
364 for (i = 0; i < n; i++)
366 len = table->prefix_lengths_in_search_order[i];
368 ASSERT(len >= 0 && len <= 256);
369 IP6_MFIB_MK_KEY(mfib, grp, src, len, key);
370 rv = clib_bihash_search_inline_2_40_8(&table->ip6_mhash, &key, &value);
375 return (FIB_NODE_INDEX_INVALID);
380 ip6_mfib_table_get_less_specific (const ip6_mfib_t *mfib,
381 const ip6_address_t *src,
382 const ip6_address_t *grp,
388 * in the absence of a tree structure for the table that allows for an O(1)
389 * parent get, a cheeky way to find the cover is to LPM for the prefix with
391 * there should always be a cover, though it may be the default route. the
392 * default route's cover is the default route.
396 /* go from (S,G) to (*,G*) */
408 return (ip6_mfib_table_lookup(mfib, src, grp, mask_len));
412 * ip6_fib_table_lookup
414 * Longest prefix match
417 ip6_mfib_table_lookup (const ip6_mfib_t *mfib,
418 const ip6_address_t *src,
419 const ip6_address_t *grp,
422 ip6_mfib_table_instance_t *table;
423 ip6_mfib_key_t key, value;
426 table = &ip6_mfib_table;
427 n = vec_len (table->prefix_lengths_in_search_order);
430 * start search from a mask length same length or shorter.
431 * we don't want matches longer than the mask passed
434 while (i < n && table->prefix_lengths_in_search_order[i] > len)
441 len = table->prefix_lengths_in_search_order[i];
444 IP6_MFIB_MK_KEY(mfib, grp, src, len, key);
446 rv = clib_bihash_search_inline_2_40_8(&table->ip6_mhash, &key, &value);
451 return (FIB_NODE_INDEX_INVALID);
455 compute_prefix_lengths_in_search_order (ip6_mfib_table_instance_t *table)
458 vec_reset_length (table->prefix_lengths_in_search_order);
459 /* Note: bitmap reversed so this is in fact a longest prefix match */
460 clib_bitmap_foreach (i, table->non_empty_dst_address_length_bitmap,
462 vec_add1(table->prefix_lengths_in_search_order, (256 - i));
467 ip6_mfib_table_entry_insert (ip6_mfib_t *mfib,
468 const ip6_address_t *grp,
469 const ip6_address_t *src,
471 fib_node_index_t mfib_entry_index)
473 ip6_mfib_table_instance_t *table;
476 table = &ip6_mfib_table;
477 IP6_MFIB_MK_KEY(mfib, grp, src, len, key);
478 key.value = mfib_entry_index;
480 clib_bihash_add_del_40_8(&table->ip6_mhash, &key, 1);
482 if (0 == table->dst_address_length_refcounts[len]++)
484 table->non_empty_dst_address_length_bitmap =
485 clib_bitmap_set (table->non_empty_dst_address_length_bitmap,
487 compute_prefix_lengths_in_search_order (table);
492 ip6_mfib_table_entry_remove (ip6_mfib_t *mfib,
493 const ip6_address_t *grp,
494 const ip6_address_t *src,
497 ip6_mfib_table_instance_t *table;
500 IP6_MFIB_MK_KEY(mfib, grp, src, len, key);
502 table = &ip6_mfib_table;
503 clib_bihash_add_del_40_8(&table->ip6_mhash, &key, 0);
505 ASSERT (table->dst_address_length_refcounts[len] > 0);
506 if (--table->dst_address_length_refcounts[len] == 0)
508 table->non_empty_dst_address_length_bitmap =
509 clib_bitmap_set (table->non_empty_dst_address_length_bitmap,
511 compute_prefix_lengths_in_search_order (table);
515 static clib_error_t *
516 ip6_mfib_module_init (vlib_main_t * vm)
521 VLIB_INIT_FUNCTION(ip6_mfib_module_init);
524 format_ip6_mfib_table_memory (u8 * s, va_list * args)
528 bytes_inuse = alloc_arena_next(&(ip6_mfib_table.ip6_mhash));
530 s = format(s, "%=30s %=6d %=12ld\n",
532 pool_elts(ip6_main.mfibs),
539 ip6_mfib_table_show_one (ip6_mfib_t *mfib,
548 vlib_cli_output(vm, "%U",
550 ip6_mfib_table_get_less_specific(mfib, src, grp, mask_len),
551 MFIB_ENTRY_FORMAT_DETAIL);
555 vlib_cli_output(vm, "%U",
557 ip6_mfib_table_lookup(mfib, src, grp, mask_len),
558 MFIB_ENTRY_FORMAT_DETAIL);
562 typedef struct ip6_mfib_show_ctx_t_ {
563 fib_node_index_t *entries;
564 } ip6_mfib_show_ctx_t;
568 ip6_mfib_table_collect_entries (fib_node_index_t mfei, void *arg)
570 ip6_mfib_show_ctx_t *ctx = arg;
572 vec_add1(ctx->entries, mfei);
574 return (WALK_CONTINUE);
578 ip6_mfib_table_show_all (ip6_mfib_t *mfib,
581 fib_node_index_t *mfib_entry_index;
582 ip6_mfib_show_ctx_t ctx = {
586 ip6_mfib_table_walk(mfib,
587 ip6_mfib_table_collect_entries,
590 vec_sort_with_function(ctx.entries, mfib_entry_cmp_for_sort);
592 vec_foreach(mfib_entry_index, ctx.entries)
594 vlib_cli_output(vm, "%U",
597 MFIB_ENTRY_FORMAT_BRIEF);
600 vec_free(ctx.entries);
604 * @brief Context when walking the IPv6 table. Since all VRFs are in the
605 * same hash table, we need to filter only those we need as we walk
607 typedef struct ip6_mfib_walk_ctx_t_
610 mfib_table_walk_fn_t i6w_fn;
612 } ip6_mfib_walk_ctx_t;
615 ip6_mfib_walk_cb (clib_bihash_kv_40_8_t * kvp,
618 ip6_mfib_walk_ctx_t *ctx = arg;
620 if ((kvp->key[4] >> 32) == ctx->i6w_mfib_index)
622 ctx->i6w_fn(kvp->value, ctx->i6w_ctx);
624 return (BIHASH_WALK_CONTINUE);
628 ip6_mfib_table_walk (ip6_mfib_t *mfib,
629 mfib_table_walk_fn_t fn,
632 ip6_mfib_walk_ctx_t ctx = {
633 .i6w_mfib_index = mfib->index,
638 clib_bihash_foreach_key_value_pair_40_8(
639 &ip6_mfib_table.ip6_mhash,
644 static clib_error_t *
645 ip6_show_mfib (vlib_main_t * vm,
646 unformat_input_t * input,
647 vlib_cli_command_t * cmd)
649 ip6_main_t * im6 = &ip6_main;
650 mfib_table_t *mfib_table;
651 int verbose, matching;
652 ip6_address_t grp, src = {{0}};
653 u32 mask = 128, cover;
654 int table_id = -1, fib_index = ~0;
660 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
662 if (unformat (input, "brief") || unformat (input, "summary")
663 || unformat (input, "sum"))
666 else if (unformat (input, "%U %U",
667 unformat_ip6_address, &src,
668 unformat_ip6_address, &grp))
673 else if (unformat (input, "%U/%d", unformat_ip6_address, &grp, &mask))
675 clib_memset(&src, 0, sizeof(src));
678 else if (unformat (input, "%U", unformat_ip6_address, &grp))
680 clib_memset(&src, 0, sizeof(src));
684 else if (unformat (input, "table %d", &table_id))
686 else if (unformat (input, "index %d", &fib_index))
688 else if (unformat (input, "cover"))
694 pool_foreach (mfib_table, im6->mfibs)
696 ip6_mfib_t *mfib = &mfib_table->v6;
698 if (table_id >= 0 && table_id != (int)mfib->table_id)
700 if (fib_index != ~0 && fib_index != (int)mfib->index)
703 vlib_cli_output (vm, "%U, fib_index %d",
704 format_mfib_table_name, mfib->index, FIB_PROTOCOL_IP6,
710 /* vlib_cli_output (vm, "%=20s%=16s", "Prefix length", "Count"); */
711 /* for (i = 0; i < ARRAY_LEN (mfib->fib_entry_by_dst_address); i++) */
713 /* uword * hash = mfib->fib_entry_by_dst_address[i]; */
714 /* uword n_elts = hash_elts (hash); */
715 /* if (n_elts > 0) */
716 /* vlib_cli_output (vm, "%20d%16d", i, n_elts); */
723 ip6_mfib_table_show_all(mfib, vm);
727 ip6_mfib_table_show_one(mfib, vm, &src, &grp, mask, cover);
735 * This command displays the IPv6 MulticasrFIB Tables (VRF Tables) and
736 * the route entries for each table.
738 * @note This command will run for a long time when the FIB tables are
739 * comprised of millions of entries. For those senarios, consider displaying
740 * a single table or summary mode.
743 * Example of how to display all the IPv6 Multicast FIB tables:
744 * @cliexstart{show ip fib}
745 * ipv6-VRF:0, fib_index 0
746 * (*, 0.0.0.0/0): flags:D,
748 * multicast-ip6-chain
752 * test-eth1: Forward,
753 * test-eth2: Forward,
755 * multicast-ip6-chain
756 * [@2]: dpo-replicate: [index:1 buckets:2 to:[0:0]]
757 * [0] [@1]: ipv6-mcast: test-eth1: IP6: d0:d1:d2:d3:d4:01 -> 01:00:05:00:00:00
758 * [1] [@1]: ipv6-mcast: test-eth2: IP6: d0:d1:d2:d3:d4:02 -> 01:00:05:00:00:00
761 * Example of how to display a summary of all IPv6 FIB tables:
762 * @cliexstart{show ip fib summary}
763 * ipv6-VRF:0, fib_index 0, flow hash: src dst sport dport proto
764 * Prefix length Count
768 * ipv6-VRF:7, fib_index 1, flow hash: src dst sport dport proto
769 * Prefix length Count
777 VLIB_CLI_COMMAND (ip6_show_fib_command, static) = {
778 .path = "show ip6 mfib",
779 .short_help = "show ip mfib [summary] [table <table-id>] [index <fib-id>] [<grp-addr>[/<mask>]] [<grp-addr>] [<src-addr> <grp-addr>]",
780 .function = ip6_show_mfib,
784 static clib_error_t *
785 ip6_mfib_init (vlib_main_t * vm)
787 clib_bihash_init_40_8 (&ip6_mfib_table.ip6_mhash,
789 IP6_MFIB_DEFAULT_HASH_NUM_BUCKETS,
790 IP6_MFIB_DEFAULT_HASH_MEMORY_SIZE);
795 VLIB_INIT_FUNCTION (ip6_mfib_init) =
797 .runs_before = VLIB_INITS("ip6_lookup_init"),