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 pefixes 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,
104 ip4_create_fib_with_table_id (u32 table_id)
106 fib_table_t *fib_table;
109 pool_get_aligned(ip4_main.fibs, fib_table, CLIB_CACHE_LINE_BYTES);
110 memset(fib_table, 0, sizeof(*fib_table));
112 pool_get_aligned(ip4_main.v4_fibs, v4_fib, CLIB_CACHE_LINE_BYTES);
114 ASSERT((fib_table - ip4_main.fibs) ==
115 (v4_fib - ip4_main.v4_fibs));
117 fib_table->ft_proto = FIB_PROTOCOL_IP4;
118 fib_table->ft_index =
120 (fib_table - ip4_main.fibs);
122 hash_set (ip4_main.fib_index_by_table_id, table_id, fib_table->ft_index);
124 fib_table->ft_table_id =
127 fib_table->ft_flow_hash_config =
128 v4_fib->flow_hash_config =
129 IP_FLOW_HASH_DEFAULT;
130 v4_fib->fwd_classify_table_index = ~0;
131 v4_fib->rev_classify_table_index = ~0;
133 fib_table_lock(fib_table->ft_index, FIB_PROTOCOL_IP4);
135 ip4_mtrie_init(&v4_fib->mtrie);
138 * add the special entries into the new FIB
142 for (ii = 0; ii < ARRAY_LEN(ip4_specials); ii++)
144 fib_prefix_t prefix = ip4_specials[ii].ift_prefix;
146 prefix.fp_addr.ip4.data_u32 =
147 clib_host_to_net_u32(prefix.fp_addr.ip4.data_u32);
149 fib_table_entry_special_add(fib_table->ft_index,
151 ip4_specials[ii].ift_source,
152 ip4_specials[ii].ift_flag,
156 return (fib_table->ft_index);
160 ip4_fib_table_destroy (u32 fib_index)
162 fib_table_t *fib_table = pool_elt_at_index(ip4_main.fibs, fib_index);
163 ip4_fib_t *v4_fib = pool_elt_at_index(ip4_main.v4_fibs, fib_index);
167 * remove all the specials we added when the table was created.
168 * In reverse order so the default route is last.
170 for (ii = ARRAY_LEN(ip4_specials) - 1; ii >= 0; ii--)
172 fib_prefix_t prefix = ip4_specials[ii].ift_prefix;
174 prefix.fp_addr.ip4.data_u32 =
175 clib_host_to_net_u32(prefix.fp_addr.ip4.data_u32);
177 fib_table_entry_special_remove(fib_table->ft_index,
179 ip4_specials[ii].ift_source);
183 * validate no more routes.
185 ASSERT(0 == fib_table->ft_total_route_counts);
186 FOR_EACH_FIB_SOURCE(ii)
188 ASSERT(0 == fib_table->ft_src_route_counts[ii]);
191 if (~0 != fib_table->ft_table_id)
193 hash_unset (ip4_main.fib_index_by_table_id, fib_table->ft_table_id);
196 ip4_mtrie_free(&v4_fib->mtrie);
198 pool_put(ip4_main.v4_fibs, v4_fib);
199 pool_put(ip4_main.fibs, fib_table);
204 ip4_fib_table_find_or_create_and_lock (u32 table_id)
208 index = ip4_fib_index_from_table_id(table_id);
210 return ip4_create_fib_with_table_id(table_id);
212 fib_table_lock(index, FIB_PROTOCOL_IP4);
218 ip4_fib_table_create_and_lock (void)
220 return (ip4_create_fib_with_table_id(~0));
224 ip4_fib_table_get_index_for_sw_if_index (u32 sw_if_index)
226 if (sw_if_index >= vec_len(ip4_main.fib_index_by_sw_if_index))
229 * This is the case for interfaces that are not yet mapped to
234 return (ip4_main.fib_index_by_sw_if_index[sw_if_index]);
238 ip4_fib_table_get_flow_hash_config (u32 fib_index)
240 return (ip4_fib_get(fib_index)->flow_hash_config);
244 * ip4_fib_table_lookup_exact_match
246 * Exact match prefix lookup
249 ip4_fib_table_lookup_exact_match (const ip4_fib_t *fib,
250 const ip4_address_t *addr,
253 uword * hash, * result;
256 hash = fib->fib_entry_by_dst_address[len];
257 key = (addr->data_u32 & ip4_main.fib_masks[len]);
259 result = hash_get(hash, key);
261 if (NULL != result) {
264 return (FIB_NODE_INDEX_INVALID);
268 * ip4_fib_table_lookup_adj
270 * Longest prefix match
273 ip4_fib_table_lookup_lb (ip4_fib_t *fib,
274 const ip4_address_t *addr)
276 fib_node_index_t fei;
278 fei = ip4_fib_table_lookup(fib, addr, 32);
280 if (FIB_NODE_INDEX_INVALID != fei)
284 dpo = fib_entry_contribute_ip_forwarding(fei);
286 return (dpo->dpoi_index);
288 return (INDEX_INVALID);
292 * ip4_fib_table_lookup
294 * Longest prefix match
297 ip4_fib_table_lookup (const ip4_fib_t *fib,
298 const ip4_address_t *addr,
301 uword * hash, * result;
305 for (mask_len = len; mask_len >= 0; mask_len--)
307 hash = fib->fib_entry_by_dst_address[mask_len];
308 key = (addr->data_u32 & ip4_main.fib_masks[mask_len]);
310 result = hash_get (hash, key);
312 if (NULL != result) {
316 return (FIB_NODE_INDEX_INVALID);
320 ip4_fib_table_entry_insert (ip4_fib_t *fib,
321 const ip4_address_t *addr,
323 fib_node_index_t fib_entry_index)
325 uword * hash, * result;
328 key = (addr->data_u32 & ip4_main.fib_masks[len]);
329 hash = fib->fib_entry_by_dst_address[len];
330 result = hash_get (hash, key);
332 if (NULL == result) {
337 hash = hash_create (32 /* elts */, sizeof (uword));
338 hash_set_flags (hash, HASH_FLAG_NO_AUTO_SHRINK);
340 hash = hash_set(hash, key, fib_entry_index);
341 fib->fib_entry_by_dst_address[len] = hash;
350 ip4_fib_table_entry_remove (ip4_fib_t *fib,
351 const ip4_address_t *addr,
354 uword * hash, * result;
357 key = (addr->data_u32 & ip4_main.fib_masks[len]);
358 hash = fib->fib_entry_by_dst_address[len];
359 result = hash_get (hash, key);
364 * removing a non-existant entry. i'll allow it.
369 hash_unset(hash, key);
372 fib->fib_entry_by_dst_address[len] = hash;
376 ip4_fib_table_fwding_dpo_update (ip4_fib_t *fib,
377 const ip4_address_t *addr,
381 ip4_fib_mtrie_route_add(&fib->mtrie, addr, len, dpo->dpoi_index);
385 ip4_fib_table_fwding_dpo_remove (ip4_fib_t *fib,
386 const ip4_address_t *addr,
391 fib_prefix_t cover_prefix = {
394 const dpo_id_t *cover_dpo;
397 * We need to pass the MTRIE the LB index and address length of the
398 * covering prefix, so it can fill the plys with the correct replacement
399 * for the entry being removed
401 fib_entry_get_prefix(cover_index, &cover_prefix);
402 cover_dpo = fib_entry_contribute_ip_forwarding(cover_index);
404 ip4_fib_mtrie_route_del(&fib->mtrie,
405 addr, len, dpo->dpoi_index,
407 cover_dpo->dpoi_index);
411 ip4_fib_table_walk (ip4_fib_t *fib,
412 fib_table_walk_fn_t fn,
417 for (i = 0; i < ARRAY_LEN (fib->fib_entry_by_dst_address); i++)
419 uword * hash = fib->fib_entry_by_dst_address[i];
425 hash_foreach_pair (p, hash,
427 fn(p->value[0], ctx);
436 typedef struct ip4_fib_show_walk_ctx_t_
438 fib_node_index_t *ifsw_indicies;
439 } ip4_fib_show_walk_ctx_t;
442 ip4_fib_show_walk_cb (fib_node_index_t fib_entry_index,
445 ip4_fib_show_walk_ctx_t *ctx = arg;
447 vec_add1(ctx->ifsw_indicies, fib_entry_index);
453 ip4_fib_table_show_all (ip4_fib_t *fib,
456 ip4_fib_show_walk_ctx_t ctx = {
457 .ifsw_indicies = NULL,
459 fib_node_index_t *fib_entry_index;
461 ip4_fib_table_walk(fib, ip4_fib_show_walk_cb, &ctx);
462 vec_sort_with_function(ctx.ifsw_indicies,
463 fib_entry_cmp_for_sort);
465 vec_foreach(fib_entry_index, ctx.ifsw_indicies)
467 vlib_cli_output(vm, "%U",
470 FIB_ENTRY_FORMAT_BRIEF);
473 vec_free(ctx.ifsw_indicies);
477 ip4_fib_table_show_one (ip4_fib_t *fib,
479 ip4_address_t *address,
483 vlib_cli_output(vm, "%U",
485 ip4_fib_table_lookup(fib, address, mask_len),
487 FIB_ENTRY_FORMAT_DETAIL2 :
488 FIB_ENTRY_FORMAT_DETAIL));
491 static clib_error_t *
492 ip4_show_fib (vlib_main_t * vm,
493 unformat_input_t * input,
494 vlib_cli_command_t * cmd)
496 ip4_main_t * im4 = &ip4_main;
497 fib_table_t * fib_table;
498 int verbose, matching, mtrie;
499 ip4_address_t matching_address;
500 u32 matching_mask = 32;
501 int i, table_id = -1, fib_index = ~0;
507 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
509 if (unformat (input, "brief") || unformat (input, "summary")
510 || unformat (input, "sum"))
513 else if (unformat (input, "detail") || unformat (input, "det"))
516 else if (unformat (input, "mtrie"))
519 else if (unformat (input, "%U/%d",
520 unformat_ip4_address, &matching_address, &matching_mask))
523 else if (unformat (input, "%U", unformat_ip4_address, &matching_address))
526 else if (unformat (input, "table %d", &table_id))
528 else if (unformat (input, "index %d", &fib_index))
534 pool_foreach (fib_table, im4->fibs,
536 ip4_fib_t *fib = pool_elt_at_index(im4->v4_fibs, fib_table->ft_index);
538 if (table_id >= 0 && table_id != (int)fib->table_id)
540 if (fib_index != ~0 && fib_index != (int)fib->index)
543 vlib_cli_output (vm, "%U, fib_index %d, flow hash: %U",
544 format_fib_table_name, fib->index, FIB_PROTOCOL_IP4,
546 format_ip_flow_hash_config, fib->flow_hash_config);
551 vlib_cli_output (vm, "%=20s%=16s", "Prefix length", "Count");
552 for (i = 0; i < ARRAY_LEN (fib->fib_entry_by_dst_address); i++)
554 uword * hash = fib->fib_entry_by_dst_address[i];
555 uword n_elts = hash_elts (hash);
557 vlib_cli_output (vm, "%20d%16d", i, n_elts);
563 vlib_cli_output (vm, "%U", format_ip4_fib_mtrie, &fib->mtrie);
569 ip4_fib_table_show_all(fib, vm);
573 ip4_fib_table_show_one(fib, vm, &matching_address,
574 matching_mask, detail);
582 * This command displays the IPv4 FIB Tables (VRF Tables) and the route
583 * entries for each table.
585 * @note This command will run for a long time when the FIB tables are
586 * comprised of millions of entries. For those senarios, consider displaying
587 * a single table or summary mode.
590 * Example of how to display all the IPv4 FIB tables:
591 * @cliexstart{show ip fib}
592 * ipv4-VRF:0, fib_index 0, flow hash: src dst sport dport proto
595 * [@0]: dpo-load-balance: [index:0 buckets:1 uRPF:0 to:[0:0]]
596 * [0] [@0]: dpo-drop ip6
599 * [@0]: dpo-load-balance: [index:1 buckets:1 uRPF:1 to:[0:0]]
600 * [0] [@0]: dpo-drop ip6
603 * [@0]: dpo-load-balance: [index:30 buckets:1 uRPF:29 to:[0:0]]
604 * [0] [@3]: arp-ipv4: via 6.0.0.1 af_packet0
607 * [@0]: dpo-load-balance: [index:31 buckets:4 uRPF:30 to:[0:0]]
608 * [0] [@3]: arp-ipv4: via 6.0.0.2 af_packet0
609 * [1] [@3]: arp-ipv4: via 6.0.0.2 af_packet0
610 * [2] [@3]: arp-ipv4: via 6.0.0.2 af_packet0
611 * [3] [@3]: arp-ipv4: via 6.0.0.1 af_packet0
614 * [@0]: dpo-load-balance: [index:3 buckets:1 uRPF:3 to:[0:0]]
615 * [0] [@0]: dpo-drop ip6
618 * [@0]: dpo-load-balance: [index:2 buckets:1 uRPF:2 to:[0:0]]
619 * [0] [@0]: dpo-drop ip6
622 * [@0]: dpo-load-balance: [index:4 buckets:1 uRPF:4 to:[0:0]]
623 * [0] [@0]: dpo-drop ip6
624 * ipv4-VRF:7, fib_index 1, flow hash: src dst sport dport proto
627 * [@0]: dpo-load-balance: [index:12 buckets:1 uRPF:11 to:[0:0]]
628 * [0] [@0]: dpo-drop ip6
631 * [@0]: dpo-load-balance: [index:13 buckets:1 uRPF:12 to:[0:0]]
632 * [0] [@0]: dpo-drop ip6
635 * [@0]: dpo-load-balance: [index:17 buckets:1 uRPF:16 to:[0:0]]
636 * [0] [@4]: ipv4-glean: af_packet0
639 * [@0]: dpo-load-balance: [index:18 buckets:1 uRPF:17 to:[1:84]]
640 * [0] [@2]: dpo-receive: 172.16.1.1 on af_packet0
643 * [@0]: dpo-load-balance: [index:21 buckets:1 uRPF:20 to:[0:0]]
644 * [0] [@5]: ipv4 via 172.16.1.2 af_packet0: IP4: 02:fe:9e:70:7a:2b -> 26:a5:f6:9c:3a:36
647 * [@0]: dpo-load-balance: [index:19 buckets:1 uRPF:18 to:[0:0]]
648 * [0] [@4]: ipv4-glean: af_packet1
651 * [@0]: dpo-load-balance: [index:20 buckets:1 uRPF:19 to:[0:0]]
652 * [0] [@2]: dpo-receive: 172.16.2.1 on af_packet1
655 * [@0]: dpo-load-balance: [index:15 buckets:1 uRPF:14 to:[0:0]]
656 * [0] [@0]: dpo-drop ip6
659 * [@0]: dpo-load-balance: [index:14 buckets:1 uRPF:13 to:[0:0]]
660 * [0] [@0]: dpo-drop ip6
663 * [@0]: dpo-load-balance: [index:16 buckets:1 uRPF:15 to:[0:0]]
664 * [0] [@0]: dpo-drop ip6
666 * Example of how to display a single IPv4 FIB table:
667 * @cliexstart{show ip fib table 7}
668 * ipv4-VRF:7, fib_index 1, flow hash: src dst sport dport proto
671 * [@0]: dpo-load-balance: [index:12 buckets:1 uRPF:11 to:[0:0]]
672 * [0] [@0]: dpo-drop ip6
675 * [@0]: dpo-load-balance: [index:13 buckets:1 uRPF:12 to:[0:0]]
676 * [0] [@0]: dpo-drop ip6
679 * [@0]: dpo-load-balance: [index:17 buckets:1 uRPF:16 to:[0:0]]
680 * [0] [@4]: ipv4-glean: af_packet0
683 * [@0]: dpo-load-balance: [index:18 buckets:1 uRPF:17 to:[1:84]]
684 * [0] [@2]: dpo-receive: 172.16.1.1 on af_packet0
687 * [@0]: dpo-load-balance: [index:21 buckets:1 uRPF:20 to:[0:0]]
688 * [0] [@5]: ipv4 via 172.16.1.2 af_packet0: IP4: 02:fe:9e:70:7a:2b -> 26:a5:f6:9c:3a:36
691 * [@0]: dpo-load-balance: [index:19 buckets:1 uRPF:18 to:[0:0]]
692 * [0] [@4]: ipv4-glean: af_packet1
695 * [@0]: dpo-load-balance: [index:20 buckets:1 uRPF:19 to:[0:0]]
696 * [0] [@2]: dpo-receive: 172.16.2.1 on af_packet1
699 * [@0]: dpo-load-balance: [index:15 buckets:1 uRPF:14 to:[0:0]]
700 * [0] [@0]: dpo-drop ip6
703 * [@0]: dpo-load-balance: [index:14 buckets:1 uRPF:13 to:[0:0]]
704 * [0] [@0]: dpo-drop ip6
707 * [@0]: dpo-load-balance: [index:16 buckets:1 uRPF:15 to:[0:0]]
708 * [0] [@0]: dpo-drop ip6
710 * Example of how to display a summary of all IPv4 FIB tables:
711 * @cliexstart{show ip fib summary}
712 * ipv4-VRF:0, fib_index 0, flow hash: src dst sport dport proto
713 * Prefix length Count
717 * ipv4-VRF:7, fib_index 1, flow hash: src dst sport dport proto
718 * Prefix length Count
726 VLIB_CLI_COMMAND (ip4_show_fib_command, static) = {
727 .path = "show ip fib",
728 .short_help = "show ip fib [summary] [table <table-id>] [index <fib-id>] [<ip4-addr>[/<mask>]] [mtrie] [detail]",
729 .function = ip4_show_fib,