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 * The number of bytes in an address/ask key in the radix tree
24 * First byte is the length in bytes.
26 #define IP6_MFIB_KEY_LEN 33
29 * Key and mask for radix
31 typedef struct ip6_mfib_key_t_
33 u8 key[IP6_MFIB_KEY_LEN];
34 u8 mask[IP6_MFIB_KEY_LEN];
38 * An object that is inserted into the radix tree.
39 * Since it's in the tree and has pointers, it cannot realloc and so cannot
40 * come from a vlib pool.
42 typedef struct ip6_mfib_node_t_
44 struct radix_node i6mn_nodes[2];
45 ip6_mfib_key_t i6mn_key;
49 static const mfib_prefix_t all_zeros = {
58 .fp_proto = FIB_PROTOCOL_IP6,
61 typedef enum ip6_mfib_special_type_t_ {
62 IP6_MFIB_SPECIAL_TYPE_NONE,
63 IP6_MFIB_SPECIAL_TYPE_SOLICITED,
64 } ip6_mfib_special_type_t;
66 typedef struct ip6_mfib_special_t_ {
68 * @brief solicited or not
70 ip6_mfib_special_type_t ims_type;
73 * @brief the Prefix length
78 * @brief The last byte of the mcast address
82 * @brief The scope of the address
87 static const ip6_mfib_special_t ip6_mfib_specials[] =
91 * Add ff02::1:ff00:0/104 via local route for all tables.
92 * This is required for neighbor discovery to work.
94 .ims_type = IP6_MFIB_SPECIAL_TYPE_SOLICITED,
99 * all-routers multicast address
101 .ims_type = IP6_MFIB_SPECIAL_TYPE_NONE,
102 .ims_scope = IP6_MULTICAST_SCOPE_link_local,
103 .ims_byte = IP6_MULTICAST_GROUP_ID_all_routers,
108 * all-nodes multicast address
110 .ims_type = IP6_MFIB_SPECIAL_TYPE_NONE,
111 .ims_scope = IP6_MULTICAST_SCOPE_link_local,
112 .ims_byte = IP6_MULTICAST_GROUP_ID_all_hosts,
117 * Add all-mldv2 multicast address via local route for all tables
119 .ims_type = IP6_MFIB_SPECIAL_TYPE_NONE,
121 .ims_scope = IP6_MULTICAST_SCOPE_link_local,
122 .ims_byte = IP6_MULTICAST_GROUP_ID_mldv2_routers,
126 #define FOR_EACH_IP6_SPECIAL(_pfx, _body) \
128 const ip6_mfib_special_t *_spec; \
131 _ii < ARRAY_LEN(ip6_mfib_specials); \
134 _spec = &ip6_mfib_specials[_ii]; \
135 if (IP6_MFIB_SPECIAL_TYPE_SOLICITED == _spec->ims_type) \
137 ip6_set_solicited_node_multicast_address( \
138 &(_pfx)->fp_grp_addr.ip6, 0); \
142 ip6_set_reserved_multicast_address ( \
143 &(_pfx)->fp_grp_addr.ip6, \
147 (_pfx)->fp_len = _spec->ims_len; \
148 do { _body; } while (0); \
154 ip6_create_mfib_with_table_id (u32 table_id)
156 mfib_table_t *mfib_table;
157 mfib_prefix_t pfx = {
158 .fp_proto = FIB_PROTOCOL_IP6,
160 const fib_route_path_t path_for_us = {
161 .frp_proto = FIB_PROTOCOL_IP6,
162 .frp_addr = zero_addr,
163 .frp_sw_if_index = 0xffffffff,
166 .frp_flags = FIB_ROUTE_PATH_LOCAL,
169 pool_get_aligned(ip6_main.mfibs, mfib_table, CLIB_CACHE_LINE_BYTES);
170 memset(mfib_table, 0, sizeof(*mfib_table));
172 mfib_table->mft_proto = FIB_PROTOCOL_IP6;
173 mfib_table->mft_index =
174 mfib_table->v6.index =
175 (mfib_table - ip6_main.mfibs);
177 hash_set (ip6_main.mfib_index_by_table_id,
179 mfib_table->mft_index);
181 mfib_table->mft_table_id =
182 mfib_table->v6.table_id =
185 mfib_table_lock(mfib_table->mft_index, FIB_PROTOCOL_IP6);
187 mfib_table->v6.rhead =
188 clib_mem_alloc_aligned (sizeof(*mfib_table->v6.rhead),
189 CLIB_CACHE_LINE_BYTES);
190 rn_inithead0(mfib_table->v6.rhead, 8);
193 * add the special entries into the new FIB
195 mfib_table_entry_update(mfib_table->mft_index,
197 MFIB_SOURCE_DEFAULT_ROUTE,
198 MFIB_ENTRY_FLAG_DROP);
201 * Add each of the specials
203 FOR_EACH_IP6_SPECIAL(&pfx,
205 mfib_table_entry_path_update(mfib_table->mft_index,
209 MFIB_ITF_FLAG_FORWARD);
212 return (mfib_table->mft_index);
216 ip6_mfib_table_destroy (ip6_mfib_t *mfib)
218 mfib_table_t *mfib_table = (mfib_table_t*)mfib;
219 fib_node_index_t mfei;
220 mfib_prefix_t pfx = {
221 .fp_proto = FIB_PROTOCOL_IP6,
223 const fib_route_path_t path_for_us = {
224 .frp_proto = FIB_PROTOCOL_IP6,
225 .frp_addr = zero_addr,
226 .frp_sw_if_index = 0xffffffff,
229 .frp_flags = FIB_ROUTE_PATH_LOCAL,
233 * remove all the specials we added when the table was created.
235 FOR_EACH_IP6_SPECIAL(&pfx,
237 mfib_table_entry_path_remove(mfib_table->mft_index,
243 mfei = mfib_table_lookup_exact_match(mfib_table->mft_index, &all_zeros);
244 mfib_table_entry_delete_index(mfei, MFIB_SOURCE_DEFAULT_ROUTE);
247 * validate no more routes.
249 ASSERT(0 == mfib_table->mft_total_route_counts);
250 ASSERT(~0 != mfib_table->mft_table_id);
252 hash_unset (ip6_main.mfib_index_by_table_id, mfib_table->mft_table_id);
253 clib_mem_free(mfib_table->v6.rhead);
254 pool_put(ip6_main.mfibs, mfib_table);
258 ip6_mfib_interface_enable_disable (u32 sw_if_index, int is_enable)
260 const fib_route_path_t path = {
261 .frp_proto = FIB_PROTOCOL_IP6,
262 .frp_addr = zero_addr,
263 .frp_sw_if_index = sw_if_index,
267 mfib_prefix_t pfx = {
268 .fp_proto = FIB_PROTOCOL_IP6,
272 vec_validate (ip6_main.mfib_index_by_sw_if_index, sw_if_index);
273 mfib_index = ip6_mfib_table_get_index_for_sw_if_index(sw_if_index);
277 FOR_EACH_IP6_SPECIAL(&pfx,
279 mfib_table_entry_path_update(mfib_index,
283 MFIB_ITF_FLAG_ACCEPT);
288 FOR_EACH_IP6_SPECIAL(&pfx,
290 mfib_table_entry_path_remove(mfib_index,
299 ip6_mfib_table_find_or_create_and_lock (u32 table_id)
303 index = ip6_mfib_index_from_table_id(table_id);
305 return ip6_create_mfib_with_table_id(table_id);
306 mfib_table_lock(index, FIB_PROTOCOL_IP6);
312 ip6_mfib_table_get_index_for_sw_if_index (u32 sw_if_index)
314 if (sw_if_index >= vec_len(ip6_main.mfib_index_by_sw_if_index))
317 * This is the case for interfaces that are not yet mapped to
322 return (ip6_main.mfib_index_by_sw_if_index[sw_if_index]);
325 #define IP6_MFIB_MK_KEY(_grp, _src, _key) \
327 (_key)->key[0] = 33; \
328 memcpy((_key)->key+1, _grp, 16); \
329 memcpy((_key)->key+17, _src, 16); \
332 #define IP6_MFIB_MK_KEY_MASK(_grp, _src, _len, _key) \
334 IP6_MFIB_MK_KEY(_grp, _src, _key); \
336 (_key)->mask[0] = 33; \
339 memcpy((_key)->mask+1, &ip6_main.fib_masks[_len], 16); \
340 memset((_key)->mask+17, 0, 16); \
344 ASSERT(_len == 256); \
345 memcpy((_key)->mask+1, &ip6_main.fib_masks[128], 16); \
346 memcpy((_key)->mask+17, &ip6_main.fib_masks[128], 16); \
351 * ip6_fib_table_lookup_exact_match
353 * Exact match prefix lookup
356 ip6_mfib_table_lookup_exact_match (const ip6_mfib_t *mfib,
357 const ip6_address_t *grp,
358 const ip6_address_t *src,
361 ip6_mfib_node_t *i6mn;
364 IP6_MFIB_MK_KEY_MASK(grp, src, len, &key);
366 i6mn = (ip6_mfib_node_t*) rn_lookup(key.key, key.mask,
367 (struct radix_node_head *)mfib->rhead);
371 return (INDEX_INVALID);
374 return (i6mn->i6mn_entry);
378 * ip6_fib_table_lookup
380 * Longest prefix match
383 ip6_mfib_table_lookup (const ip6_mfib_t *mfib,
384 const ip6_address_t *src,
385 const ip6_address_t *grp,
388 ip6_mfib_node_t *i6mn;
391 IP6_MFIB_MK_KEY_MASK(grp, src, len, &key);
393 i6mn = (ip6_mfib_node_t*) rn_search_m(key.key,
394 mfib->rhead->rnh_treetop,
397 ASSERT(NULL != i6mn);
399 return (i6mn->i6mn_entry);
403 * ip6_fib_table_lookup
405 * Longest prefix match no mask
408 ip6_mfib_table_lookup2 (const ip6_mfib_t *mfib,
409 const ip6_address_t *src,
410 const ip6_address_t *grp)
412 ip6_mfib_node_t *i6mn;
415 IP6_MFIB_MK_KEY(grp, src, &key);
417 i6mn = (ip6_mfib_node_t*) rn_match(key.key,
418 (struct radix_node_head *)mfib->rhead); // const cast
420 ASSERT(NULL != i6mn);
422 return (i6mn->i6mn_entry);
426 ip6_mfib_table_entry_insert (ip6_mfib_t *mfib,
427 const ip6_address_t *grp,
428 const ip6_address_t *src,
430 fib_node_index_t mfib_entry_index)
432 ip6_mfib_node_t *i6mn = clib_mem_alloc(sizeof(*i6mn));
434 memset(i6mn, 0, sizeof(*i6mn));
436 IP6_MFIB_MK_KEY_MASK(grp, src, len, &i6mn->i6mn_key);
437 i6mn->i6mn_entry = mfib_entry_index;
439 if (NULL == rn_addroute(i6mn->i6mn_key.key,
449 ip6_mfib_table_entry_remove (ip6_mfib_t *mfib,
450 const ip6_address_t *grp,
451 const ip6_address_t *src,
454 ip6_mfib_node_t *i6mn;
457 IP6_MFIB_MK_KEY_MASK(grp, src, len, &key);
459 i6mn = (ip6_mfib_node_t*) rn_delete(key.key, key.mask, mfib->rhead);
464 static clib_error_t *
465 ip6_mfib_module_init (vlib_main_t * vm)
470 VLIB_INIT_FUNCTION(ip6_mfib_module_init);
473 ip6_mfib_table_show_one (ip6_mfib_t *mfib,
479 vlib_cli_output(vm, "%U",
481 ip6_mfib_table_lookup(mfib, src, grp, mask_len),
482 MFIB_ENTRY_FORMAT_DETAIL);
485 typedef struct ip6_mfib_show_ctx_t_ {
487 fib_node_index_t *entries;
488 } ip6_mfib_show_ctx_t;
492 ip6_mfib_table_collect_entries (struct radix_node *rn, void *arg)
494 ip6_mfib_show_ctx_t *ctx = arg;
495 ip6_mfib_node_t *i6mn;
497 i6mn = (ip6_mfib_node_t*) rn;
499 vec_add1(ctx->entries, i6mn->i6mn_entry);
505 ip6_mfib_table_show_all (ip6_mfib_t *mfib,
508 fib_node_index_t *mfib_entry_index;
509 ip6_mfib_show_ctx_t ctx = {
510 .fib_index = mfib->index,
514 rn_walktree(mfib->rhead,
515 ip6_mfib_table_collect_entries,
518 vec_sort_with_function(ctx.entries, mfib_entry_cmp_for_sort);
520 vec_foreach(mfib_entry_index, ctx.entries)
522 vlib_cli_output(vm, "%U",
525 MFIB_ENTRY_FORMAT_BRIEF);
528 vec_free(ctx.entries);
531 static clib_error_t *
532 ip6_show_mfib (vlib_main_t * vm,
533 unformat_input_t * input,
534 vlib_cli_command_t * cmd)
536 ip6_main_t * im4 = &ip6_main;
537 mfib_table_t *mfib_table;
538 int verbose, matching;
539 ip6_address_t grp, src = {{0}};
541 int table_id = -1, fib_index = ~0;
546 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
548 if (unformat (input, "brief") || unformat (input, "summary")
549 || unformat (input, "sum"))
552 else if (unformat (input, "%U %U",
553 unformat_ip6_address, &src,
554 unformat_ip6_address, &grp))
559 else if (unformat (input, "%U", unformat_ip6_address, &grp))
564 else if (unformat (input, "%U/%d",
565 unformat_ip6_address, &grp, &mask))
567 else if (unformat (input, "table %d", &table_id))
569 else if (unformat (input, "index %d", &fib_index))
575 pool_foreach (mfib_table, im4->mfibs,
577 ip6_mfib_t *mfib = &mfib_table->v6;
579 if (table_id >= 0 && table_id != (int)mfib->table_id)
581 if (fib_index != ~0 && fib_index != (int)mfib->index)
584 vlib_cli_output (vm, "%U, fib_index %d",
585 format_mfib_table_name, mfib->index, FIB_PROTOCOL_IP6,
591 /* vlib_cli_output (vm, "%=20s%=16s", "Prefix length", "Count"); */
592 /* for (i = 0; i < ARRAY_LEN (mfib->fib_entry_by_dst_address); i++) */
594 /* uword * hash = mfib->fib_entry_by_dst_address[i]; */
595 /* uword n_elts = hash_elts (hash); */
596 /* if (n_elts > 0) */
597 /* vlib_cli_output (vm, "%20d%16d", i, n_elts); */
604 ip6_mfib_table_show_all(mfib, vm);
608 ip6_mfib_table_show_one(mfib, vm, &src, &grp, mask);
616 * This command displays the IPv4 MulticasrFIB Tables (VRF Tables) and
617 * the route entries for each table.
619 * @note This command will run for a long time when the FIB tables are
620 * comprised of millions of entries. For those senarios, consider displaying
621 * a single table or summary mode.
624 * Example of how to display all the IPv4 Multicast FIB tables:
625 * @cliexstart{show ip fib}
626 * ipv4-VRF:0, fib_index 0
627 * (*, 0.0.0.0/0): flags:D,
629 * multicast-ip6-chain
633 * test-eth1: Forward,
634 * test-eth2: Forward,
636 * multicast-ip6-chain
637 * [@2]: dpo-replicate: [index:1 buckets:2 to:[0:0]]
638 * [0] [@1]: ipv4-mcast: test-eth1: IP6: d0:d1:d2:d3:d4:01 -> 01:00:05:00:00:00
639 * [1] [@1]: ipv4-mcast: test-eth2: IP6: d0:d1:d2:d3:d4:02 -> 01:00:05:00:00:00
642 * Example of how to display a summary of all IPv4 FIB tables:
643 * @cliexstart{show ip fib summary}
644 * ipv4-VRF:0, fib_index 0, flow hash: src dst sport dport proto
645 * Prefix length Count
649 * ipv4-VRF:7, fib_index 1, flow hash: src dst sport dport proto
650 * Prefix length Count
658 VLIB_CLI_COMMAND (ip6_show_fib_command, static) = {
659 .path = "show ip6 mfib",
660 .short_help = "show ip mfib [summary] [table <table-id>] [index <fib-id>] [<grp-addr>[/<mask>]] [<grp-addr>] [<src-addr> <grp-addr>]",
661 .function = ip6_show_mfib,