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/ip4_mfib.h>
18 #include <vnet/mfib/mfib_table.h>
19 #include <vnet/mfib/mfib_entry.h>
21 static const mfib_prefix_t all_zeros =
23 .fp_proto = FIB_PROTOCOL_IP4,
25 static const mfib_prefix_t ip4_specials[] =
27 /* ALL prefixes are in network order */
29 /* (*,224.0.0.1)/32 - all hosts */
31 .ip4.data_u32 = 0x010000e0,
34 .fp_proto = FIB_PROTOCOL_IP4,
37 /* (*,224.0.0.2)/32 - all routers */
39 .ip4.data_u32 = 0x020000e0,
42 .fp_proto = FIB_PROTOCOL_IP4,
47 ip4_create_mfib_with_table_id (u32 table_id,
50 mfib_table_t *mfib_table;
52 pool_get_aligned(ip4_main.mfibs, mfib_table, CLIB_CACHE_LINE_BYTES);
53 clib_memset(mfib_table, 0, sizeof(*mfib_table));
55 mfib_table->mft_proto = FIB_PROTOCOL_IP4;
56 mfib_table->mft_index =
57 mfib_table->v4.index =
58 (mfib_table - ip4_main.mfibs);
60 hash_set (ip4_main.mfib_index_by_table_id,
62 mfib_table->mft_index);
64 mfib_table->mft_table_id =
65 mfib_table->v4.table_id =
68 mfib_table_lock(mfib_table->mft_index, FIB_PROTOCOL_IP4, src);
71 * add the default route into the new FIB
73 mfib_table_entry_update(mfib_table->mft_index,
75 MFIB_SOURCE_DEFAULT_ROUTE,
77 MFIB_ENTRY_FLAG_DROP);
79 const fib_route_path_t path = {
80 .frp_proto = DPO_PROTO_IP4,
81 .frp_addr = zero_addr,
82 .frp_sw_if_index = ~0,
85 .frp_flags = FIB_ROUTE_PATH_LOCAL,
86 .frp_mitf_flags = MFIB_ITF_FLAG_FORWARD,
90 for (ii = 0; ii < ARRAY_LEN(ip4_specials); ii++)
92 mfib_table_entry_path_update(mfib_table->mft_index,
98 return (mfib_table->mft_index);
102 ip4_mfib_table_destroy (ip4_mfib_t *mfib)
104 mfib_table_t *mfib_table = (mfib_table_t*)mfib;
108 * remove all the specials we added when the table was created.
110 mfib_table_entry_delete(mfib_table->mft_index,
112 MFIB_SOURCE_DEFAULT_ROUTE);
114 for (ii = 0; ii < ARRAY_LEN(ip4_specials); ii++)
116 mfib_table_entry_delete(mfib_table->mft_index,
118 MFIB_SOURCE_SPECIAL);
122 * validate no more routes.
124 ASSERT(0 == mfib_table->mft_total_route_counts);
125 ASSERT(~0 != mfib_table->mft_table_id);
127 hash_unset (ip4_main.mfib_index_by_table_id, mfib_table->mft_table_id);
128 pool_put(ip4_main.mfibs, mfib_table);
132 ip4_mfib_interface_enable_disable (u32 sw_if_index, int is_enable)
134 const fib_route_path_t path = {
135 .frp_proto = DPO_PROTO_IP4,
136 .frp_addr = zero_addr,
137 .frp_sw_if_index = sw_if_index,
140 .frp_mitf_flags = MFIB_ITF_FLAG_ACCEPT,
145 mfib_index = ip4_mfib_table_get_index_for_sw_if_index(sw_if_index);
147 for (ii = 0; ii < ARRAY_LEN(ip4_specials); ii++)
151 mfib_table_entry_path_update(mfib_index,
158 mfib_table_entry_path_remove(mfib_index,
167 ip4_mfib_table_find_or_create_and_lock (u32 table_id,
172 index = ip4_mfib_index_from_table_id(table_id);
174 return ip4_create_mfib_with_table_id(table_id, src);
175 mfib_table_lock(index, FIB_PROTOCOL_IP4, src);
181 ip4_mfib_table_get_index_for_sw_if_index (u32 sw_if_index)
183 if (sw_if_index >= vec_len(ip4_main.mfib_index_by_sw_if_index))
186 * This is the case for interfaces that are not yet mapped to
191 return (ip4_main.mfib_index_by_sw_if_index[sw_if_index]);
194 #define IPV4_MFIB_GRP_LEN(_len)\
195 (_len > 32 ? 32 : _len)
197 #define IP4_MFIB_MK_KEY(_grp, _src, _len, _key) \
199 _key = ((u64)(_grp->data_u32 & \
200 ip4_main.fib_masks[IPV4_MFIB_GRP_LEN(_len)])) << 32; \
201 _key |= _src->data_u32; \
203 #define IP4_MFIB_MK_GRP_KEY(_grp, _len, _key) \
205 _key = ((u64)(_grp->data_u32 & \
206 ip4_main.fib_masks[IPV4_MFIB_GRP_LEN(_len)])) << 32; \
210 * ip4_fib_table_lookup_exact_match
212 * Exact match prefix lookup
215 ip4_mfib_table_lookup_exact_match (const ip4_mfib_t *mfib,
216 const ip4_address_t *grp,
217 const ip4_address_t *src,
220 uword * hash, * result;
223 hash = mfib->fib_entry_by_dst_address[len];
224 IP4_MFIB_MK_KEY(grp, src, len, key);
226 result = hash_get(hash, key);
228 if (NULL != result) {
231 return (FIB_NODE_INDEX_INVALID);
235 * ip4_fib_table_lookup
237 * Longest prefix match
240 ip4_mfib_table_lookup (const ip4_mfib_t *mfib,
241 const ip4_address_t *src,
242 const ip4_address_t *grp,
245 uword * hash, * result;
251 if (PREDICT_TRUE(64 == mask_len))
253 hash = mfib->fib_entry_by_dst_address[mask_len];
254 IP4_MFIB_MK_KEY(grp, src, mask_len, key);
256 result = hash_get (hash, key);
258 if (NULL != result) {
263 for (mask_len = (len == 64 ? 32 : len); mask_len >= 0; mask_len--)
265 hash = mfib->fib_entry_by_dst_address[mask_len];
266 IP4_MFIB_MK_GRP_KEY(grp, mask_len, key);
268 result = hash_get (hash, key);
270 if (NULL != result) {
274 return (FIB_NODE_INDEX_INVALID);
278 ip4_mfib_table_get_less_specific (const ip4_mfib_t *mfib,
279 const ip4_address_t *src,
280 const ip4_address_t *grp,
286 * in the absence of a tree structure for the table that allows for an O(1)
287 * parent get, a cheeky way to find the cover is to LPM for the prefix with
289 * there should always be a cover, though it may be the default route. the
290 * default route's cover is the default route.
294 /* go from (S,G) to (*,G*) */
306 return (ip4_mfib_table_lookup(mfib, src, grp, mask_len));
310 ip4_mfib_table_entry_insert (ip4_mfib_t *mfib,
311 const ip4_address_t *grp,
312 const ip4_address_t *src,
314 fib_node_index_t fib_entry_index)
316 uword * hash, * result;
319 IP4_MFIB_MK_KEY(grp, src, len, key);
320 hash = mfib->fib_entry_by_dst_address[len];
321 result = hash_get (hash, key);
323 if (NULL == result) {
328 hash = hash_create (32 /* elts */, sizeof (uword));
329 hash_set_flags (hash, HASH_FLAG_NO_AUTO_SHRINK);
331 hash = hash_set(hash, key, fib_entry_index);
332 mfib->fib_entry_by_dst_address[len] = hash;
341 ip4_mfib_table_entry_remove (ip4_mfib_t *mfib,
342 const ip4_address_t *grp,
343 const ip4_address_t *src,
346 uword * hash, * result;
349 IP4_MFIB_MK_KEY(grp, src, len, key);
350 hash = mfib->fib_entry_by_dst_address[len];
351 result = hash_get (hash, key);
356 * removing a non-existent entry. i'll allow it.
361 hash_unset(hash, key);
364 mfib->fib_entry_by_dst_address[len] = hash;
368 ip4_mfib_table_walk (ip4_mfib_t *mfib,
369 mfib_table_walk_fn_t fn,
374 for (i = 0; i < ARRAY_LEN (mfib->fib_entry_by_dst_address); i++)
376 uword * hash = mfib->fib_entry_by_dst_address[i];
382 hash_foreach_pair (p, hash,
384 fn(p->value[0], ctx);
391 format_ip4_mfib_table_memory (u8 * s, va_list * args)
393 mfib_table_t *mfib_table;
398 pool_foreach (mfib_table, ip4_main.mfibs)
400 ip4_mfib_t *mfib = &mfib_table->v4;
406 for (i = 0; i < ARRAY_LEN (mfib->fib_entry_by_dst_address); i++)
408 uword * hash = mfib->fib_entry_by_dst_address[i];
412 mfib_size += hash_bytes(hash);
416 total_memory += mfib_size;
419 s = format(s, "%=30s %=6d %=12ld\n",
421 pool_elts(ip4_main.mfibs), total_memory);
427 ip4_mfib_table_show_all (ip4_mfib_t *mfib,
430 fib_node_index_t *mfib_entry_indicies;
431 fib_node_index_t *mfib_entry_index;
434 mfib_entry_indicies = NULL;
436 for (i = 0; i < ARRAY_LEN (mfib->fib_entry_by_dst_address); i++)
438 uword * hash = mfib->fib_entry_by_dst_address[i];
444 hash_foreach_pair (p, hash,
446 vec_add1(mfib_entry_indicies, p->value[0]);
451 vec_sort_with_function(mfib_entry_indicies, mfib_entry_cmp_for_sort);
453 vec_foreach(mfib_entry_index, mfib_entry_indicies)
455 vlib_cli_output(vm, "%U",
458 MFIB_ENTRY_FORMAT_BRIEF);
461 vec_free(mfib_entry_indicies);
465 ip4_mfib_table_show_one (ip4_mfib_t *mfib,
471 vlib_cli_output(vm, "%U",
473 ip4_mfib_table_lookup(mfib, src, grp, mask_len),
474 MFIB_ENTRY_FORMAT_DETAIL);
477 static clib_error_t *
478 ip4_show_mfib (vlib_main_t * vm,
479 unformat_input_t * input,
480 vlib_cli_command_t * cmd)
482 ip4_main_t * im4 = &ip4_main;
483 mfib_table_t *mfib_table;
484 int verbose, matching, memory;
485 ip4_address_t grp, src = {{0}};
487 u64 total_hash_memory;
488 int i, table_id = -1, fib_index = ~0;
491 memory = matching = 0;
492 total_hash_memory = 0;
494 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
496 if (unformat (input, "brief") || unformat (input, "summary")
497 || unformat (input, "sum"))
499 else if (unformat (input, "mem") || unformat (input, "memory"))
501 else if (unformat (input, "%U %U",
502 unformat_ip4_address, &src,
503 unformat_ip4_address, &grp))
508 else if (unformat (input, "%U/%d", unformat_ip4_address, &grp, &mask))
510 clib_memset(&src, 0, sizeof(src));
513 else if (unformat (input, "%U", unformat_ip4_address, &grp))
515 clib_memset(&src, 0, sizeof(src));
519 else if (unformat (input, "table %d", &table_id))
521 else if (unformat (input, "index %d", &fib_index))
527 pool_foreach (mfib_table, im4->mfibs)
529 ip4_mfib_t *mfib = &mfib_table->v4;
531 if (table_id >= 0 && table_id != (int)mfib->table_id)
533 if (fib_index != ~0 && fib_index != (int)mfib->index)
542 for (i = 0; i < ARRAY_LEN (mfib->fib_entry_by_dst_address); i++)
544 uword * hash = mfib->fib_entry_by_dst_address[i];
547 hash_size += hash_bytes(hash);
551 vlib_cli_output (vm, "%U hash:%d",
552 format_mfib_table_name, mfib->index,
555 total_hash_memory += hash_size;
559 vlib_cli_output (vm, "%U, fib_index:%d flags:%U",
560 format_mfib_table_name, mfib->index, FIB_PROTOCOL_IP4,
562 format_mfib_table_flags, mfib_table->mft_flags);
567 vlib_cli_output (vm, "%=20s%=16s", "Prefix length", "Count");
568 for (i = 0; i < ARRAY_LEN (mfib->fib_entry_by_dst_address); i++)
570 uword * hash = mfib->fib_entry_by_dst_address[i];
571 uword n_elts = hash_elts (hash);
573 vlib_cli_output (vm, "%20d%16d", i, n_elts);
580 ip4_mfib_table_show_all(mfib, vm);
584 ip4_mfib_table_show_one(mfib, vm, &src, &grp, mask);
588 vlib_cli_output (vm, "totals: hash:%ld", total_hash_memory);
594 * This command displays the IPv4 MulticasrFIB Tables (VRF Tables) and
595 * the route entries for each table.
597 * @note This command will run for a long time when the FIB tables are
598 * comprised of millions of entries. For those senarios, consider displaying
599 * a single table or summary mode.
602 * Example of how to display all the IPv4 Multicast FIB tables:
603 * @cliexstart{show ip fib}
604 * ipv4-VRF:0, fib_index 0
605 * (*, 0.0.0.0/0): flags:D,
607 * multicast-ip4-chain
611 * test-eth1: Forward,
612 * test-eth2: Forward,
614 * multicast-ip4-chain
615 * [@2]: dpo-replicate: [index:1 buckets:2 to:[0:0]]
616 * [0] [@1]: ipv4-mcast: test-eth1: IP4: d0:d1:d2:d3:d4:01 -> 01:00:05:00:00:00
617 * [1] [@1]: ipv4-mcast: test-eth2: IP4: d0:d1:d2:d3:d4:02 -> 01:00:05:00:00:00
620 * Example of how to display a summary of all IPv4 FIB tables:
621 * @cliexstart{show ip fib summary}
622 * ipv4-VRF:0, fib_index 0, flow hash: src dst sport dport proto
623 * Prefix length Count
627 * ipv4-VRF:7, fib_index 1, flow hash: src dst sport dport proto
628 * Prefix length Count
636 VLIB_CLI_COMMAND (ip4_show_mfib_command, static) = {
637 .path = "show ip mfib",
638 .short_help = "show ip mfib [summary] [table <table-id>] [index <fib-id>] [<grp-addr>[/<mask>]] [<grp-addr>] [<src-addr> <grp-addr>]",
639 .function = ip4_show_mfib,