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/fib_table.h>
17 #include <vnet/fib/fib_entry.h>
18 #include <vnet/fib/ip4_fib.h>
21 * A table of prefixes to be added to tables and the sources for them
23 typedef struct ip4_fib_table_special_prefix_t_ {
24 fib_prefix_t ift_prefix;
25 fib_source_t ift_source;
26 fib_entry_flag_t ift_flag;
27 } ip4_fib_table_special_prefix_t;
29 static const ip4_fib_table_special_prefix_t ip4_specials[] = {
37 .fp_proto = FIB_PROTOCOL_IP4,
39 .ift_source = FIB_SOURCE_DEFAULT_ROUTE,
40 .ift_flag = FIB_ENTRY_FLAG_DROP,
49 .fp_proto = FIB_PROTOCOL_IP4,
51 .ift_source = FIB_SOURCE_DEFAULT_ROUTE,
52 .ift_flag = FIB_ENTRY_FLAG_DROP,
61 .ip4.data_u32 = 0xf0000000,
64 .fp_proto = FIB_PROTOCOL_IP4,
66 .ift_source = FIB_SOURCE_SPECIAL,
67 .ift_flag = FIB_ENTRY_FLAG_DROP,
77 .ip4.data_u32 = 0xe0000000,
80 .fp_proto = FIB_PROTOCOL_IP4,
82 .ift_source = FIB_SOURCE_SPECIAL,
83 .ift_flag = FIB_ENTRY_FLAG_DROP,
88 * drop, but we'll allow it to be usurped by the likes of DHCP
92 .ip4.data_u32 = 0xffffffff,
95 .fp_proto = FIB_PROTOCOL_IP4,
97 .ift_source = FIB_SOURCE_DEFAULT_ROUTE,
98 .ift_flag = FIB_ENTRY_FLAG_DROP,
103 ip4_fib_hash_load_specials (u32 fib_index)
106 * add the special entries into the new FIB
110 for (ii = 0; ii < ARRAY_LEN(ip4_specials); ii++)
112 fib_prefix_t prefix = ip4_specials[ii].ift_prefix;
114 prefix.fp_addr.ip4.data_u32 =
115 clib_host_to_net_u32(prefix.fp_addr.ip4.data_u32);
117 fib_table_entry_special_add(fib_index,
119 ip4_specials[ii].ift_source,
120 ip4_specials[ii].ift_flag);
125 ip4_fib_hash_flush_specials (u32 fib_index)
130 * remove all the specials we added when the table was created.
131 * In reverse order so the default route is last.
133 for (ii = ARRAY_LEN(ip4_specials) - 1; ii >= 0; ii--)
135 fib_prefix_t prefix = ip4_specials[ii].ift_prefix;
137 prefix.fp_addr.ip4.data_u32 =
138 clib_host_to_net_u32(prefix.fp_addr.ip4.data_u32);
140 fib_table_entry_special_remove(fib_index,
142 ip4_specials[ii].ift_source);
147 ip4_create_fib_with_table_id (u32 table_id,
150 fib_table_t *fib_table;
153 pool_get(ip4_main.fibs, fib_table);
154 clib_memset(fib_table, 0, sizeof(*fib_table));
156 pool_get_aligned(ip4_fibs, v4_fib, CLIB_CACHE_LINE_BYTES);
158 fib_table->ft_proto = FIB_PROTOCOL_IP4;
159 fib_table->ft_index = (v4_fib - ip4_fibs);
162 * It is required that the index of the fib_table_t in its pool
163 * is the same as the index of the ip4_fib_t in its pool, since the
164 * rest of the code usues the 'fib_index' to mean either of these
165 * objects, depending on the context.
167 ASSERT(fib_table->ft_index == fib_table - ip4_main.fibs);
169 hash_set (ip4_main.fib_index_by_table_id, table_id, fib_table->ft_index);
171 fib_table->ft_table_id =
172 v4_fib->hash.table_id =
174 fib_table->ft_flow_hash_config = IP_FLOW_HASH_DEFAULT;
176 fib_table_lock(fib_table->ft_index, FIB_PROTOCOL_IP4, src);
178 ip4_fib_table_init(v4_fib);
181 * add the special entries into the new FIB
183 ip4_fib_hash_load_specials(fib_table - ip4_main.fibs);
185 return (fib_table->ft_index);
189 ip4_fib_table_destroy (u32 fib_index)
191 fib_table_t *fib_table = pool_elt_at_index(ip4_main.fibs, fib_index);
192 ip4_fib_t *v4_fib = pool_elt_at_index(ip4_fibs, fib_table->ft_index);
196 * remove all the specials we added when the table was created.
197 * In reverse order so the default route is last.
199 ip4_fib_hash_flush_specials(fib_table - ip4_main.fibs);
202 * validate no more routes.
205 if (0 != fib_table->ft_total_route_counts)
206 fib_table_assert_empty(fib_table);
209 vec_foreach(n_locks, fib_table->ft_src_route_counts)
211 ASSERT(0 == *n_locks);
214 if (~0 != fib_table->ft_table_id)
216 hash_unset (ip4_main.fib_index_by_table_id, fib_table->ft_table_id);
219 vec_free (fib_table->ft_locks);
220 vec_free(fib_table->ft_src_route_counts);
221 ip4_fib_table_free(v4_fib);
223 pool_put(ip4_fibs, v4_fib);
224 pool_put(ip4_main.fibs, fib_table);
229 ip4_fib_table_find_or_create_and_lock (u32 table_id,
234 index = ip4_fib_index_from_table_id(table_id);
236 return ip4_create_fib_with_table_id(table_id, src);
238 fib_table_lock(index, FIB_PROTOCOL_IP4, src);
244 ip4_fib_table_create_and_lock (fib_source_t src)
246 return (ip4_create_fib_with_table_id(~0, src));
250 ip4_fib_table_get_index_for_sw_if_index (u32 sw_if_index)
252 if (sw_if_index >= vec_len(ip4_main.fib_index_by_sw_if_index))
255 * This is the case for interfaces that are not yet mapped to
260 return (ip4_main.fib_index_by_sw_if_index[sw_if_index]);
267 typedef struct ip4_fib_show_walk_ctx_t_
269 fib_node_index_t *ifsw_indicies;
270 } ip4_fib_show_walk_ctx_t;
272 static fib_table_walk_rc_t
273 ip4_fib_show_walk_cb (fib_node_index_t fib_entry_index,
276 ip4_fib_show_walk_ctx_t *ctx = arg;
278 vec_add1(ctx->ifsw_indicies, fib_entry_index);
280 return (FIB_TABLE_WALK_CONTINUE);
284 ip4_fib_table_show_all (ip4_fib_t *fib,
287 ip4_fib_show_walk_ctx_t ctx = {
288 .ifsw_indicies = NULL,
290 fib_node_index_t *fib_entry_index;
292 ip4_fib_table_walk(fib, ip4_fib_show_walk_cb, &ctx);
293 vec_sort_with_function(ctx.ifsw_indicies,
294 fib_entry_cmp_for_sort);
296 vec_foreach(fib_entry_index, ctx.ifsw_indicies)
298 vlib_cli_output(vm, "%U",
301 FIB_ENTRY_FORMAT_BRIEF);
304 vec_free(ctx.ifsw_indicies);
308 ip4_fib_table_show_one (ip4_fib_t *fib,
310 ip4_address_t *address,
314 vlib_cli_output(vm, "%U",
316 ip4_fib_table_lookup(fib, address, mask_len),
318 FIB_ENTRY_FORMAT_DETAIL2 :
319 FIB_ENTRY_FORMAT_DETAIL));
323 format_ip4_fib_table_memory (u8 * s, va_list * args)
325 s = format(s, "%=30s %=6d\n",
327 pool_elts(ip4_main.fibs));
331 static clib_error_t *
332 ip4_show_fib (vlib_main_t * vm,
333 unformat_input_t * input,
334 vlib_cli_command_t * cmd)
336 ip4_main_t * im4 = &ip4_main;
337 u64 total_mtrie_memory, total_hash_memory;
338 int verbose, matching, mtrie, memory;
339 ip4_address_t matching_address;
340 u32 fib_index, matching_mask = 32;
341 int i, table_id = -1, user_fib_index = ~0;
345 matching = mtrie = memory = 0;
346 total_hash_memory = total_mtrie_memory = 0;
348 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
350 if (unformat (input, "brief") || unformat (input, "summary")
351 || unformat (input, "sum"))
354 else if (unformat (input, "detail") || unformat (input, "det"))
357 else if (unformat (input, "mtrie"))
360 else if (unformat (input, "mem") ||
361 unformat (input, "memory"))
364 else if (unformat (input, "%U/%d",
365 unformat_ip4_address, &matching_address, &matching_mask))
368 else if (unformat (input, "%U", unformat_ip4_address, &matching_address))
371 else if (unformat (input, "table %d", &table_id))
373 else if (unformat (input, "index %d", &user_fib_index))
379 pool_foreach_index (fib_index, im4->fibs)
381 fib_table_t *fib_table = pool_elt_at_index(im4->fibs, fib_index);
382 ip4_fib_t *fib = pool_elt_at_index(ip4_fibs, fib_table->ft_index);
386 if (table_id >= 0 && table_id != (int)fib->hash.table_id)
388 if (user_fib_index != ~0 && user_fib_index != fib_index)
393 uword mtrie_size, hash_size;
396 mtrie_size = ip4_mtrie_memory_usage(&fib->mtrie);
399 for (i = 0; i < ARRAY_LEN (fib->hash.fib_entry_by_dst_address); i++)
401 uword * hash = fib->hash.fib_entry_by_dst_address[i];
404 hash_size += hash_bytes(hash);
409 vlib_cli_output (vm, "%U mtrie:%d hash:%d",
410 format_fib_table_name, fib_index,
414 total_mtrie_memory += mtrie_size;
415 total_hash_memory += hash_size;
419 s = format(s, "%U, fib_index:%d, flow hash:[%U] epoch:%d flags:%U locks:[",
420 format_fib_table_name, fib_index,
423 format_ip_flow_hash_config,
424 fib_table->ft_flow_hash_config,
426 format_fib_table_flags, fib_table->ft_flags);
427 vec_foreach_index(source, fib_table->ft_locks)
429 if (0 != fib_table->ft_locks[source])
431 s = format(s, "%U:%d, ",
432 format_fib_source, source,
433 fib_table->ft_locks[source]);
437 vlib_cli_output (vm, "%v", s);
443 vlib_cli_output (vm, "%U", format_ip4_mtrie, &fib->mtrie, verbose);
448 vlib_cli_output (vm, "%=20s%=16s", "Prefix length", "Count");
449 for (i = 0; i < ARRAY_LEN (fib->hash.fib_entry_by_dst_address); i++)
451 uword * hash = fib->hash.fib_entry_by_dst_address[i];
452 uword n_elts = hash_elts (hash);
454 vlib_cli_output (vm, "%20d%16d", i, n_elts);
461 ip4_fib_table_show_all(fib, vm);
465 ip4_fib_table_show_one(fib, vm, &matching_address,
466 matching_mask, detail);
472 vlib_cli_output (vm, "totals: mtrie:%ld hash:%ld all:%ld",
475 total_mtrie_memory + total_hash_memory);
481 * This command displays the IPv4 FIB Tables (VRF Tables) and the route
482 * entries for each table.
484 * @note This command will run for a long time when the FIB tables are
485 * comprised of millions of entries. For those scenarios, consider displaying
486 * a single table or summary mode.
489 * Example of how to display all the IPv4 FIB tables:
490 * @cliexstart{show ip fib}
491 * ipv4-VRF:0, fib_index 0, flow hash: src dst sport dport proto
494 * [@0]: dpo-load-balance: [index:0 buckets:1 uRPF:0 to:[0:0]]
495 * [0] [@0]: dpo-drop ip6
498 * [@0]: dpo-load-balance: [index:1 buckets:1 uRPF:1 to:[0:0]]
499 * [0] [@0]: dpo-drop ip6
502 * [@0]: dpo-load-balance: [index:30 buckets:1 uRPF:29 to:[0:0]]
503 * [0] [@3]: arp-ipv4: via 6.0.0.1 af_packet0
506 * [@0]: dpo-load-balance: [index:31 buckets:4 uRPF:30 to:[0:0]]
507 * [0] [@3]: arp-ipv4: via 6.0.0.2 af_packet0
508 * [1] [@3]: arp-ipv4: via 6.0.0.2 af_packet0
509 * [2] [@3]: arp-ipv4: via 6.0.0.2 af_packet0
510 * [3] [@3]: arp-ipv4: via 6.0.0.1 af_packet0
513 * [@0]: dpo-load-balance: [index:3 buckets:1 uRPF:3 to:[0:0]]
514 * [0] [@0]: dpo-drop ip6
517 * [@0]: dpo-load-balance: [index:2 buckets:1 uRPF:2 to:[0:0]]
518 * [0] [@0]: dpo-drop ip6
521 * [@0]: dpo-load-balance: [index:4 buckets:1 uRPF:4 to:[0:0]]
522 * [0] [@0]: dpo-drop ip6
523 * ipv4-VRF:7, fib_index 1, flow hash: src dst sport dport proto
526 * [@0]: dpo-load-balance: [index:12 buckets:1 uRPF:11 to:[0:0]]
527 * [0] [@0]: dpo-drop ip6
530 * [@0]: dpo-load-balance: [index:13 buckets:1 uRPF:12 to:[0:0]]
531 * [0] [@0]: dpo-drop ip6
534 * [@0]: dpo-load-balance: [index:17 buckets:1 uRPF:16 to:[0:0]]
535 * [0] [@4]: ipv4-glean: af_packet0
538 * [@0]: dpo-load-balance: [index:18 buckets:1 uRPF:17 to:[1:84]]
539 * [0] [@2]: dpo-receive: 172.16.1.1 on af_packet0
542 * [@0]: dpo-load-balance: [index:21 buckets:1 uRPF:20 to:[0:0]]
543 * [0] [@5]: ipv4 via 172.16.1.2 af_packet0: IP4: 02:fe:9e:70:7a:2b -> 26:a5:f6:9c:3a:36
546 * [@0]: dpo-load-balance: [index:19 buckets:1 uRPF:18 to:[0:0]]
547 * [0] [@4]: ipv4-glean: af_packet1
550 * [@0]: dpo-load-balance: [index:20 buckets:1 uRPF:19 to:[0:0]]
551 * [0] [@2]: dpo-receive: 172.16.2.1 on af_packet1
554 * [@0]: dpo-load-balance: [index:15 buckets:1 uRPF:14 to:[0:0]]
555 * [0] [@0]: dpo-drop ip6
558 * [@0]: dpo-load-balance: [index:14 buckets:1 uRPF:13 to:[0:0]]
559 * [0] [@0]: dpo-drop ip6
562 * [@0]: dpo-load-balance: [index:16 buckets:1 uRPF:15 to:[0:0]]
563 * [0] [@0]: dpo-drop ip6
565 * Example of how to display a single IPv4 FIB table:
566 * @cliexstart{show ip fib table 7}
567 * ipv4-VRF:7, fib_index 1, flow hash: src dst sport dport proto
570 * [@0]: dpo-load-balance: [index:12 buckets:1 uRPF:11 to:[0:0]]
571 * [0] [@0]: dpo-drop ip6
574 * [@0]: dpo-load-balance: [index:13 buckets:1 uRPF:12 to:[0:0]]
575 * [0] [@0]: dpo-drop ip6
578 * [@0]: dpo-load-balance: [index:17 buckets:1 uRPF:16 to:[0:0]]
579 * [0] [@4]: ipv4-glean: af_packet0
582 * [@0]: dpo-load-balance: [index:18 buckets:1 uRPF:17 to:[1:84]]
583 * [0] [@2]: dpo-receive: 172.16.1.1 on af_packet0
586 * [@0]: dpo-load-balance: [index:21 buckets:1 uRPF:20 to:[0:0]]
587 * [0] [@5]: ipv4 via 172.16.1.2 af_packet0: IP4: 02:fe:9e:70:7a:2b -> 26:a5:f6:9c:3a:36
590 * [@0]: dpo-load-balance: [index:19 buckets:1 uRPF:18 to:[0:0]]
591 * [0] [@4]: ipv4-glean: af_packet1
594 * [@0]: dpo-load-balance: [index:20 buckets:1 uRPF:19 to:[0:0]]
595 * [0] [@2]: dpo-receive: 172.16.2.1 on af_packet1
598 * [@0]: dpo-load-balance: [index:15 buckets:1 uRPF:14 to:[0:0]]
599 * [0] [@0]: dpo-drop ip6
602 * [@0]: dpo-load-balance: [index:14 buckets:1 uRPF:13 to:[0:0]]
603 * [0] [@0]: dpo-drop ip6
606 * [@0]: dpo-load-balance: [index:16 buckets:1 uRPF:15 to:[0:0]]
607 * [0] [@0]: dpo-drop ip6
609 * Example of how to display a summary of all IPv4 FIB tables:
610 * @cliexstart{show ip fib summary}
611 * ipv4-VRF:0, fib_index 0, flow hash: src dst sport dport proto
612 * Prefix length Count
616 * ipv4-VRF:7, fib_index 1, flow hash: src dst sport dport proto
617 * Prefix length Count
625 VLIB_CLI_COMMAND (ip4_show_fib_command, static) = {
626 .path = "show ip fib",
627 .short_help = "show ip fib [summary] [table <table-id>] [index <fib-id>] [<ip4-addr>[/<mask>]] [mtrie] [detail]",
628 .function = ip4_show_fib,