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>
23 * Key and mask for radix
25 typedef clib_bihash_kv_40_8_t ip6_mfib_key_t;
27 static const mfib_prefix_t all_zeros = {
36 .fp_proto = FIB_PROTOCOL_IP6,
39 typedef enum ip6_mfib_special_type_t_ {
40 IP6_MFIB_SPECIAL_TYPE_NONE,
41 IP6_MFIB_SPECIAL_TYPE_SOLICITED,
42 } ip6_mfib_special_type_t;
44 typedef struct ip6_mfib_special_t_ {
46 * @brief solicited or not
48 ip6_mfib_special_type_t ims_type;
51 * @brief the Prefix length
56 * @brief The last byte of the mcast address
60 * @brief The scope of the address
65 static const ip6_mfib_special_t ip6_mfib_specials[] =
69 * Add ff02::1:ff00:0/104 via local route for all tables.
70 * This is required for neighbor discovery to work.
72 .ims_type = IP6_MFIB_SPECIAL_TYPE_SOLICITED,
77 * all-routers multicast address
79 .ims_type = IP6_MFIB_SPECIAL_TYPE_NONE,
80 .ims_scope = IP6_MULTICAST_SCOPE_link_local,
81 .ims_byte = IP6_MULTICAST_GROUP_ID_all_routers,
86 * all-nodes multicast address
88 .ims_type = IP6_MFIB_SPECIAL_TYPE_NONE,
89 .ims_scope = IP6_MULTICAST_SCOPE_link_local,
90 .ims_byte = IP6_MULTICAST_GROUP_ID_all_hosts,
95 * Add all-mldv2 multicast address via local route for all tables
97 .ims_type = IP6_MFIB_SPECIAL_TYPE_NONE,
99 .ims_scope = IP6_MULTICAST_SCOPE_link_local,
100 .ims_byte = IP6_MULTICAST_GROUP_ID_mldv2_routers,
104 #define FOR_EACH_IP6_SPECIAL(_pfx, _body) \
106 const ip6_mfib_special_t *_spec; \
109 _ii < ARRAY_LEN(ip6_mfib_specials); \
112 _spec = &ip6_mfib_specials[_ii]; \
113 if (IP6_MFIB_SPECIAL_TYPE_SOLICITED == _spec->ims_type) \
115 ip6_set_solicited_node_multicast_address( \
116 &(_pfx)->fp_grp_addr.ip6, 0); \
120 ip6_set_reserved_multicast_address ( \
121 &(_pfx)->fp_grp_addr.ip6, \
125 (_pfx)->fp_len = _spec->ims_len; \
126 do { _body; } while (0); \
132 ip6_create_mfib_with_table_id (u32 table_id,
135 mfib_table_t *mfib_table;
136 mfib_prefix_t pfx = {
137 .fp_proto = FIB_PROTOCOL_IP6,
139 const fib_route_path_t path_for_us = {
140 .frp_proto = DPO_PROTO_IP6,
141 .frp_addr = zero_addr,
142 .frp_sw_if_index = 0xffffffff,
145 .frp_flags = FIB_ROUTE_PATH_LOCAL,
148 pool_get_aligned(ip6_main.mfibs, mfib_table, CLIB_CACHE_LINE_BYTES);
149 clib_memset(mfib_table, 0, sizeof(*mfib_table));
151 mfib_table->mft_proto = FIB_PROTOCOL_IP6;
152 mfib_table->mft_index =
153 mfib_table->v6.index =
154 (mfib_table - ip6_main.mfibs);
156 hash_set (ip6_main.mfib_index_by_table_id,
158 mfib_table->mft_index);
160 mfib_table->mft_table_id =
161 mfib_table->v6.table_id =
164 mfib_table_lock(mfib_table->mft_index, FIB_PROTOCOL_IP6, src);
167 * add the special entries into the new FIB
169 mfib_table_entry_update(mfib_table->mft_index,
171 MFIB_SOURCE_DEFAULT_ROUTE,
173 MFIB_ENTRY_FLAG_DROP);
176 * Add each of the specials
178 FOR_EACH_IP6_SPECIAL(&pfx,
180 mfib_table_entry_path_update(mfib_table->mft_index,
184 MFIB_ITF_FLAG_FORWARD);
187 return (mfib_table->mft_index);
191 ip6_mfib_table_destroy (ip6_mfib_t *mfib)
193 mfib_table_t *mfib_table = (mfib_table_t*)mfib;
194 fib_node_index_t mfei;
195 mfib_prefix_t pfx = {
196 .fp_proto = FIB_PROTOCOL_IP6,
198 const fib_route_path_t path_for_us = {
199 .frp_proto = DPO_PROTO_IP6,
200 .frp_addr = zero_addr,
201 .frp_sw_if_index = 0xffffffff,
204 .frp_flags = FIB_ROUTE_PATH_LOCAL,
208 * remove all the specials we added when the table was created.
210 FOR_EACH_IP6_SPECIAL(&pfx,
212 mfib_table_entry_path_remove(mfib_table->mft_index,
218 mfei = mfib_table_lookup_exact_match(mfib_table->mft_index, &all_zeros);
219 mfib_table_entry_delete_index(mfei, MFIB_SOURCE_DEFAULT_ROUTE);
222 * validate no more routes.
224 ASSERT(0 == mfib_table->mft_total_route_counts);
225 ASSERT(~0 != mfib_table->mft_table_id);
227 hash_unset (ip6_main.mfib_index_by_table_id, mfib_table->mft_table_id);
228 pool_put(ip6_main.mfibs, mfib_table);
232 ip6_mfib_interface_enable_disable (u32 sw_if_index, int is_enable)
234 const fib_route_path_t path = {
235 .frp_proto = DPO_PROTO_IP6,
236 .frp_addr = zero_addr,
237 .frp_sw_if_index = sw_if_index,
241 mfib_prefix_t pfx = {
242 .fp_proto = FIB_PROTOCOL_IP6,
246 vec_validate (ip6_main.mfib_index_by_sw_if_index, sw_if_index);
247 mfib_index = ip6_mfib_table_get_index_for_sw_if_index(sw_if_index);
251 FOR_EACH_IP6_SPECIAL(&pfx,
253 mfib_table_entry_path_update(mfib_index,
257 MFIB_ITF_FLAG_ACCEPT);
262 FOR_EACH_IP6_SPECIAL(&pfx,
264 mfib_table_entry_path_remove(mfib_index,
273 ip6_mfib_table_find_or_create_and_lock (u32 table_id,
278 index = ip6_mfib_index_from_table_id(table_id);
280 return ip6_create_mfib_with_table_id(table_id, src);
281 mfib_table_lock(index, FIB_PROTOCOL_IP6, src);
287 ip6_mfib_table_get_index_for_sw_if_index (u32 sw_if_index)
289 if (sw_if_index >= vec_len(ip6_main.mfib_index_by_sw_if_index))
292 * This is the case for interfaces that are not yet mapped to
297 return (ip6_main.mfib_index_by_sw_if_index[sw_if_index]);
300 #define IPV6_MFIB_GRP_LEN(_len) \
301 (_len > 128 ? 128 : _len)
303 #define IP6_MFIB_MK_KEY(_mfib, _grp, _src, _len, _key) \
305 _key.key[0] = (_grp->as_u64[0] & \
306 ip6_main.fib_masks[IPV6_MFIB_GRP_LEN(_len)].as_u64[0]); \
307 _key.key[1] = (_grp->as_u64[1] & \
308 ip6_main.fib_masks[IPV6_MFIB_GRP_LEN(_len)].as_u64[1]); \
310 _key.key[2] = _src->as_u64[0]; \
311 _key.key[3] = _src->as_u64[1]; \
316 _key.key[4] = _mfib->index; \
317 _key.key[4] = (_key.key[4] << 32) | len; \
321 * ip6_fib_table_lookup_exact_match
323 * Exact match prefix lookup
326 ip6_mfib_table_lookup_exact_match (const ip6_mfib_t *mfib,
327 const ip6_address_t *grp,
328 const ip6_address_t *src,
331 ip6_mfib_key_t key, value;
334 IP6_MFIB_MK_KEY(mfib, grp, src, len, key);
336 rv = clib_bihash_search_inline_2_40_8(&ip6_main.ip6_mtable.ip6_mhash,
341 return (FIB_NODE_INDEX_INVALID);
345 * ip6_fib_table_lookup
347 * Longest prefix match for the forwarding plane (no mask given)
350 ip6_mfib_table_fwd_lookup (const ip6_mfib_t *mfib,
351 const ip6_address_t *src,
352 const ip6_address_t *grp)
354 ip6_mfib_table_instance_t *table;
355 ip6_mfib_key_t key, value;
359 table = &ip6_main.ip6_mtable;
360 n = vec_len (table->prefix_lengths_in_search_order);
362 for (i = 0; i < n; i++)
364 len = table->prefix_lengths_in_search_order[i];
366 ASSERT(len >= 0 && len <= 256);
367 IP6_MFIB_MK_KEY(mfib, grp, src, len, key);
368 rv = clib_bihash_search_inline_2_40_8(&table->ip6_mhash, &key, &value);
373 return (FIB_NODE_INDEX_INVALID);
378 ip6_mfib_table_get_less_specific (const ip6_mfib_t *mfib,
379 const ip6_address_t *src,
380 const ip6_address_t *grp,
386 * in the absence of a tree structure for the table that allows for an O(1)
387 * parent get, a cheeky way to find the cover is to LPM for the prefix with
389 * there should always be a cover, though it may be the default route. the
390 * default route's cover is the default route.
394 /* go from (S,G) to (*,G*) */
406 return (ip6_mfib_table_lookup(mfib, src, grp, mask_len));
410 * ip6_fib_table_lookup
412 * Longest prefix match
415 ip6_mfib_table_lookup (const ip6_mfib_t *mfib,
416 const ip6_address_t *src,
417 const ip6_address_t *grp,
420 ip6_mfib_table_instance_t *table;
421 ip6_mfib_key_t key, value;
424 table = &ip6_main.ip6_mtable;
425 n = vec_len (table->prefix_lengths_in_search_order);
428 * start search from a mask length same length or shorter.
429 * we don't want matches longer than the mask passed
432 while (i < n && table->prefix_lengths_in_search_order[i] > len)
439 len = table->prefix_lengths_in_search_order[i];
442 IP6_MFIB_MK_KEY(mfib, grp, src, len, key);
444 rv = clib_bihash_search_inline_2_40_8(&table->ip6_mhash, &key, &value);
449 return (FIB_NODE_INDEX_INVALID);
453 compute_prefix_lengths_in_search_order (ip6_mfib_table_instance_t *table)
456 vec_reset_length (table->prefix_lengths_in_search_order);
457 /* Note: bitmap reversed so this is in fact a longest prefix match */
458 clib_bitmap_foreach (i, table->non_empty_dst_address_length_bitmap,
460 vec_add1(table->prefix_lengths_in_search_order, (256 - i));
465 ip6_mfib_table_entry_insert (ip6_mfib_t *mfib,
466 const ip6_address_t *grp,
467 const ip6_address_t *src,
469 fib_node_index_t mfib_entry_index)
471 ip6_mfib_table_instance_t *table;
474 table = &ip6_main.ip6_mtable;
475 IP6_MFIB_MK_KEY(mfib, grp, src, len, key);
476 key.value = mfib_entry_index;
478 clib_bihash_add_del_40_8(&table->ip6_mhash, &key, 1);
480 if (0 == 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);
490 ip6_mfib_table_entry_remove (ip6_mfib_t *mfib,
491 const ip6_address_t *grp,
492 const ip6_address_t *src,
495 ip6_mfib_table_instance_t *table;
498 IP6_MFIB_MK_KEY(mfib, grp, src, len, key);
500 table = &ip6_main.ip6_mtable;
501 clib_bihash_add_del_40_8(&table->ip6_mhash, &key, 0);
503 ASSERT (table->dst_address_length_refcounts[len] > 0);
504 if (--table->dst_address_length_refcounts[len] == 0)
506 table->non_empty_dst_address_length_bitmap =
507 clib_bitmap_set (table->non_empty_dst_address_length_bitmap,
509 compute_prefix_lengths_in_search_order (table);
513 static clib_error_t *
514 ip6_mfib_module_init (vlib_main_t * vm)
519 VLIB_INIT_FUNCTION(ip6_mfib_module_init);
522 format_ip6_mfib_table_memory (u8 * s, va_list * args)
524 s = format(s, "%=30s %=6d %=8s\n",
526 pool_elts(ip6_main.mfibs), "???");
532 ip6_mfib_table_show_one (ip6_mfib_t *mfib,
541 vlib_cli_output(vm, "%U",
543 ip6_mfib_table_get_less_specific(mfib, src, grp, mask_len),
544 MFIB_ENTRY_FORMAT_DETAIL);
548 vlib_cli_output(vm, "%U",
550 ip6_mfib_table_lookup(mfib, src, grp, mask_len),
551 MFIB_ENTRY_FORMAT_DETAIL);
555 typedef struct ip6_mfib_show_ctx_t_ {
556 fib_node_index_t *entries;
557 } ip6_mfib_show_ctx_t;
561 ip6_mfib_table_collect_entries (fib_node_index_t mfei, void *arg)
563 ip6_mfib_show_ctx_t *ctx = arg;
565 vec_add1(ctx->entries, mfei);
571 ip6_mfib_table_show_all (ip6_mfib_t *mfib,
574 fib_node_index_t *mfib_entry_index;
575 ip6_mfib_show_ctx_t ctx = {
579 ip6_mfib_table_walk(mfib,
580 ip6_mfib_table_collect_entries,
583 vec_sort_with_function(ctx.entries, mfib_entry_cmp_for_sort);
585 vec_foreach(mfib_entry_index, ctx.entries)
587 vlib_cli_output(vm, "%U",
590 MFIB_ENTRY_FORMAT_BRIEF);
593 vec_free(ctx.entries);
597 * @brief Context when walking the IPv6 table. Since all VRFs are in the
598 * same hash table, we need to filter only those we need as we walk
600 typedef struct ip6_mfib_walk_ctx_t_
603 mfib_table_walk_fn_t i6w_fn;
605 } ip6_mfib_walk_ctx_t;
608 ip6_mfib_walk_cb (clib_bihash_kv_40_8_t * kvp,
611 ip6_mfib_walk_ctx_t *ctx = arg;
613 if ((kvp->key[4] >> 32) == ctx->i6w_mfib_index)
615 return (ctx->i6w_fn(kvp->value, ctx->i6w_ctx));
617 return (FIB_TABLE_WALK_CONTINUE);
621 ip6_mfib_table_walk (ip6_mfib_t *mfib,
622 mfib_table_walk_fn_t fn,
625 ip6_mfib_walk_ctx_t ctx = {
626 .i6w_mfib_index = mfib->index,
631 clib_bihash_foreach_key_value_pair_40_8(
632 &ip6_main.ip6_mtable.ip6_mhash,
637 static clib_error_t *
638 ip6_show_mfib (vlib_main_t * vm,
639 unformat_input_t * input,
640 vlib_cli_command_t * cmd)
642 ip6_main_t * im6 = &ip6_main;
643 mfib_table_t *mfib_table;
644 int verbose, matching;
645 ip6_address_t grp, src = {{0}};
646 u32 mask = 128, cover;
647 int table_id = -1, fib_index = ~0;
653 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
655 if (unformat (input, "brief") || unformat (input, "summary")
656 || unformat (input, "sum"))
659 else if (unformat (input, "%U %U",
660 unformat_ip6_address, &src,
661 unformat_ip6_address, &grp))
666 else if (unformat (input, "%U/%d", unformat_ip6_address, &grp, &mask))
668 clib_memset(&src, 0, sizeof(src));
671 else if (unformat (input, "%U", unformat_ip6_address, &grp))
673 clib_memset(&src, 0, sizeof(src));
677 else if (unformat (input, "table %d", &table_id))
679 else if (unformat (input, "index %d", &fib_index))
681 else if (unformat (input, "cover"))
687 pool_foreach (mfib_table, im6->mfibs,
689 ip6_mfib_t *mfib = &mfib_table->v6;
691 if (table_id >= 0 && table_id != (int)mfib->table_id)
693 if (fib_index != ~0 && fib_index != (int)mfib->index)
696 vlib_cli_output (vm, "%U, fib_index %d",
697 format_mfib_table_name, mfib->index, FIB_PROTOCOL_IP6,
703 /* vlib_cli_output (vm, "%=20s%=16s", "Prefix length", "Count"); */
704 /* for (i = 0; i < ARRAY_LEN (mfib->fib_entry_by_dst_address); i++) */
706 /* uword * hash = mfib->fib_entry_by_dst_address[i]; */
707 /* uword n_elts = hash_elts (hash); */
708 /* if (n_elts > 0) */
709 /* vlib_cli_output (vm, "%20d%16d", i, n_elts); */
716 ip6_mfib_table_show_all(mfib, vm);
720 ip6_mfib_table_show_one(mfib, vm, &src, &grp, mask, cover);
728 * This command displays the IPv6 MulticasrFIB Tables (VRF Tables) and
729 * the route entries for each table.
731 * @note This command will run for a long time when the FIB tables are
732 * comprised of millions of entries. For those senarios, consider displaying
733 * a single table or summary mode.
736 * Example of how to display all the IPv6 Multicast FIB tables:
737 * @cliexstart{show ip fib}
738 * ipv6-VRF:0, fib_index 0
739 * (*, 0.0.0.0/0): flags:D,
741 * multicast-ip6-chain
745 * test-eth1: Forward,
746 * test-eth2: Forward,
748 * multicast-ip6-chain
749 * [@2]: dpo-replicate: [index:1 buckets:2 to:[0:0]]
750 * [0] [@1]: ipv6-mcast: test-eth1: IP6: d0:d1:d2:d3:d4:01 -> 01:00:05:00:00:00
751 * [1] [@1]: ipv6-mcast: test-eth2: IP6: d0:d1:d2:d3:d4:02 -> 01:00:05:00:00:00
754 * Example of how to display a summary of all IPv6 FIB tables:
755 * @cliexstart{show ip fib summary}
756 * ipv6-VRF:0, fib_index 0, flow hash: src dst sport dport proto
757 * Prefix length Count
761 * ipv6-VRF:7, fib_index 1, flow hash: src dst sport dport proto
762 * Prefix length Count
770 VLIB_CLI_COMMAND (ip6_show_fib_command, static) = {
771 .path = "show ip6 mfib",
772 .short_help = "show ip mfib [summary] [table <table-id>] [index <fib-id>] [<grp-addr>[/<mask>]] [<grp-addr>] [<src-addr> <grp-addr>]",
773 .function = ip6_show_mfib,