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,
107 fib_table_t *fib_table;
110 pool_get_aligned(ip4_main.fibs, fib_table, CLIB_CACHE_LINE_BYTES);
111 memset(fib_table, 0, sizeof(*fib_table));
113 pool_get_aligned(ip4_main.v4_fibs, v4_fib, CLIB_CACHE_LINE_BYTES);
115 ASSERT((fib_table - ip4_main.fibs) ==
116 (v4_fib - ip4_main.v4_fibs));
118 fib_table->ft_proto = FIB_PROTOCOL_IP4;
119 fib_table->ft_index =
121 (fib_table - ip4_main.fibs);
123 hash_set (ip4_main.fib_index_by_table_id, table_id, fib_table->ft_index);
125 fib_table->ft_table_id =
128 fib_table->ft_flow_hash_config = IP_FLOW_HASH_DEFAULT;
129 v4_fib->fwd_classify_table_index = ~0;
130 v4_fib->rev_classify_table_index = ~0;
132 fib_table_lock(fib_table->ft_index, FIB_PROTOCOL_IP4, src);
134 ip4_mtrie_init(&v4_fib->mtrie);
137 * add the special entries into the new FIB
141 for (ii = 0; ii < ARRAY_LEN(ip4_specials); ii++)
143 fib_prefix_t prefix = ip4_specials[ii].ift_prefix;
145 prefix.fp_addr.ip4.data_u32 =
146 clib_host_to_net_u32(prefix.fp_addr.ip4.data_u32);
148 fib_table_entry_special_add(fib_table->ft_index,
150 ip4_specials[ii].ift_source,
151 ip4_specials[ii].ift_flag);
154 return (fib_table->ft_index);
158 ip4_fib_table_destroy (u32 fib_index)
160 fib_table_t *fib_table = pool_elt_at_index(ip4_main.fibs, fib_index);
161 ip4_fib_t *v4_fib = pool_elt_at_index(ip4_main.v4_fibs, fib_index);
165 * remove all the specials we added when the table was created.
166 * In reverse order so the default route is last.
168 for (ii = ARRAY_LEN(ip4_specials) - 1; ii >= 0; ii--)
170 fib_prefix_t prefix = ip4_specials[ii].ift_prefix;
172 prefix.fp_addr.ip4.data_u32 =
173 clib_host_to_net_u32(prefix.fp_addr.ip4.data_u32);
175 fib_table_entry_special_remove(fib_table->ft_index,
177 ip4_specials[ii].ift_source);
181 * validate no more routes.
183 ASSERT(0 == fib_table->ft_total_route_counts);
184 FOR_EACH_FIB_SOURCE(ii)
186 ASSERT(0 == fib_table->ft_src_route_counts[ii]);
189 if (~0 != fib_table->ft_table_id)
191 hash_unset (ip4_main.fib_index_by_table_id, fib_table->ft_table_id);
194 ip4_mtrie_free(&v4_fib->mtrie);
196 pool_put(ip4_main.v4_fibs, v4_fib);
197 pool_put(ip4_main.fibs, fib_table);
202 ip4_fib_table_find_or_create_and_lock (u32 table_id,
207 index = ip4_fib_index_from_table_id(table_id);
209 return ip4_create_fib_with_table_id(table_id, src);
211 fib_table_lock(index, FIB_PROTOCOL_IP4, src);
217 ip4_fib_table_create_and_lock (fib_source_t src)
219 return (ip4_create_fib_with_table_id(~0, src));
223 ip4_fib_table_get_index_for_sw_if_index (u32 sw_if_index)
225 if (sw_if_index >= vec_len(ip4_main.fib_index_by_sw_if_index))
228 * This is the case for interfaces that are not yet mapped to
233 return (ip4_main.fib_index_by_sw_if_index[sw_if_index]);
237 * ip4_fib_table_lookup_exact_match
239 * Exact match prefix lookup
242 ip4_fib_table_lookup_exact_match (const ip4_fib_t *fib,
243 const ip4_address_t *addr,
246 uword * hash, * result;
249 hash = fib->fib_entry_by_dst_address[len];
250 key = (addr->data_u32 & ip4_main.fib_masks[len]);
252 result = hash_get(hash, key);
254 if (NULL != result) {
257 return (FIB_NODE_INDEX_INVALID);
261 * ip4_fib_table_lookup_adj
263 * Longest prefix match
266 ip4_fib_table_lookup_lb (ip4_fib_t *fib,
267 const ip4_address_t *addr)
269 fib_node_index_t fei;
271 fei = ip4_fib_table_lookup(fib, addr, 32);
273 if (FIB_NODE_INDEX_INVALID != fei)
277 dpo = fib_entry_contribute_ip_forwarding(fei);
279 return (dpo->dpoi_index);
281 return (INDEX_INVALID);
285 * ip4_fib_table_lookup
287 * Longest prefix match
290 ip4_fib_table_lookup (const ip4_fib_t *fib,
291 const ip4_address_t *addr,
294 uword * hash, * result;
298 for (mask_len = len; mask_len >= 0; mask_len--)
300 hash = fib->fib_entry_by_dst_address[mask_len];
301 key = (addr->data_u32 & ip4_main.fib_masks[mask_len]);
303 result = hash_get (hash, key);
305 if (NULL != result) {
309 return (FIB_NODE_INDEX_INVALID);
313 ip4_fib_table_entry_insert (ip4_fib_t *fib,
314 const ip4_address_t *addr,
316 fib_node_index_t fib_entry_index)
318 uword * hash, * result;
321 key = (addr->data_u32 & ip4_main.fib_masks[len]);
322 hash = fib->fib_entry_by_dst_address[len];
323 result = hash_get (hash, key);
325 if (NULL == result) {
330 hash = hash_create (32 /* elts */, sizeof (uword));
331 hash_set_flags (hash, HASH_FLAG_NO_AUTO_SHRINK);
333 hash = hash_set(hash, key, fib_entry_index);
334 fib->fib_entry_by_dst_address[len] = hash;
343 ip4_fib_table_entry_remove (ip4_fib_t *fib,
344 const ip4_address_t *addr,
347 uword * hash, * result;
350 key = (addr->data_u32 & ip4_main.fib_masks[len]);
351 hash = fib->fib_entry_by_dst_address[len];
352 result = hash_get (hash, key);
357 * removing a non-existant entry. i'll allow it.
362 hash_unset(hash, key);
365 fib->fib_entry_by_dst_address[len] = hash;
369 ip4_fib_table_fwding_dpo_update (ip4_fib_t *fib,
370 const ip4_address_t *addr,
374 ip4_fib_mtrie_route_add(&fib->mtrie, addr, len, dpo->dpoi_index);
378 ip4_fib_table_fwding_dpo_remove (ip4_fib_t *fib,
379 const ip4_address_t *addr,
384 fib_prefix_t cover_prefix = {
387 const dpo_id_t *cover_dpo;
390 * We need to pass the MTRIE the LB index and address length of the
391 * covering prefix, so it can fill the plys with the correct replacement
392 * for the entry being removed
394 fib_entry_get_prefix(cover_index, &cover_prefix);
395 cover_dpo = fib_entry_contribute_ip_forwarding(cover_index);
397 ip4_fib_mtrie_route_del(&fib->mtrie,
398 addr, len, dpo->dpoi_index,
400 cover_dpo->dpoi_index);
404 ip4_fib_table_walk (ip4_fib_t *fib,
405 fib_table_walk_fn_t fn,
410 for (i = 0; i < ARRAY_LEN (fib->fib_entry_by_dst_address); i++)
412 uword * hash = fib->fib_entry_by_dst_address[i];
418 hash_foreach_pair (p, hash,
420 fn(p->value[0], ctx);
429 typedef struct ip4_fib_show_walk_ctx_t_
431 fib_node_index_t *ifsw_indicies;
432 } ip4_fib_show_walk_ctx_t;
435 ip4_fib_show_walk_cb (fib_node_index_t fib_entry_index,
438 ip4_fib_show_walk_ctx_t *ctx = arg;
440 vec_add1(ctx->ifsw_indicies, fib_entry_index);
446 ip4_fib_table_show_all (ip4_fib_t *fib,
449 ip4_fib_show_walk_ctx_t ctx = {
450 .ifsw_indicies = NULL,
452 fib_node_index_t *fib_entry_index;
454 ip4_fib_table_walk(fib, ip4_fib_show_walk_cb, &ctx);
455 vec_sort_with_function(ctx.ifsw_indicies,
456 fib_entry_cmp_for_sort);
458 vec_foreach(fib_entry_index, ctx.ifsw_indicies)
460 vlib_cli_output(vm, "%U",
463 FIB_ENTRY_FORMAT_BRIEF);
466 vec_free(ctx.ifsw_indicies);
470 ip4_fib_table_show_one (ip4_fib_t *fib,
472 ip4_address_t *address,
476 vlib_cli_output(vm, "%U",
478 ip4_fib_table_lookup(fib, address, mask_len),
480 FIB_ENTRY_FORMAT_DETAIL2 :
481 FIB_ENTRY_FORMAT_DETAIL));
484 static clib_error_t *
485 ip4_show_fib (vlib_main_t * vm,
486 unformat_input_t * input,
487 vlib_cli_command_t * cmd)
489 ip4_main_t * im4 = &ip4_main;
490 fib_table_t * fib_table;
491 int verbose, matching, mtrie;
492 ip4_address_t matching_address;
493 u32 matching_mask = 32;
494 int i, table_id = -1, fib_index = ~0;
500 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
502 if (unformat (input, "brief") || unformat (input, "summary")
503 || unformat (input, "sum"))
506 else if (unformat (input, "detail") || unformat (input, "det"))
509 else if (unformat (input, "mtrie"))
512 else if (unformat (input, "%U/%d",
513 unformat_ip4_address, &matching_address, &matching_mask))
516 else if (unformat (input, "%U", unformat_ip4_address, &matching_address))
519 else if (unformat (input, "table %d", &table_id))
521 else if (unformat (input, "index %d", &fib_index))
527 pool_foreach (fib_table, im4->fibs,
529 ip4_fib_t *fib = pool_elt_at_index(im4->v4_fibs, fib_table->ft_index);
533 if (table_id >= 0 && table_id != (int)fib->table_id)
535 if (fib_index != ~0 && fib_index != (int)fib->index)
538 s = format(s, "%U, fib_index:%d, flow hash:[%U] locks:[",
539 format_fib_table_name, fib->index,
542 format_ip_flow_hash_config,
543 fib_table->ft_flow_hash_config);
544 FOR_EACH_FIB_SOURCE(source)
546 if (0 != fib_table->ft_locks[source])
548 s = format(s, "%U:%d, ",
549 format_fib_source, source,
550 fib_table->ft_locks[source]);
554 vlib_cli_output (vm, "%v", s);
560 vlib_cli_output (vm, "%=20s%=16s", "Prefix length", "Count");
561 for (i = 0; i < ARRAY_LEN (fib->fib_entry_by_dst_address); i++)
563 uword * hash = fib->fib_entry_by_dst_address[i];
564 uword n_elts = hash_elts (hash);
566 vlib_cli_output (vm, "%20d%16d", i, n_elts);
572 vlib_cli_output (vm, "%U", format_ip4_fib_mtrie, &fib->mtrie);
578 ip4_fib_table_show_all(fib, vm);
582 ip4_fib_table_show_one(fib, vm, &matching_address,
583 matching_mask, detail);
591 * This command displays the IPv4 FIB Tables (VRF Tables) and the route
592 * entries for each table.
594 * @note This command will run for a long time when the FIB tables are
595 * comprised of millions of entries. For those senarios, consider displaying
596 * a single table or summary mode.
599 * Example of how to display all the IPv4 FIB tables:
600 * @cliexstart{show ip fib}
601 * ipv4-VRF:0, fib_index 0, flow hash: src dst sport dport proto
604 * [@0]: dpo-load-balance: [index:0 buckets:1 uRPF:0 to:[0:0]]
605 * [0] [@0]: dpo-drop ip6
608 * [@0]: dpo-load-balance: [index:1 buckets:1 uRPF:1 to:[0:0]]
609 * [0] [@0]: dpo-drop ip6
612 * [@0]: dpo-load-balance: [index:30 buckets:1 uRPF:29 to:[0:0]]
613 * [0] [@3]: arp-ipv4: via 6.0.0.1 af_packet0
616 * [@0]: dpo-load-balance: [index:31 buckets:4 uRPF:30 to:[0:0]]
617 * [0] [@3]: arp-ipv4: via 6.0.0.2 af_packet0
618 * [1] [@3]: arp-ipv4: via 6.0.0.2 af_packet0
619 * [2] [@3]: arp-ipv4: via 6.0.0.2 af_packet0
620 * [3] [@3]: arp-ipv4: via 6.0.0.1 af_packet0
623 * [@0]: dpo-load-balance: [index:3 buckets:1 uRPF:3 to:[0:0]]
624 * [0] [@0]: dpo-drop ip6
627 * [@0]: dpo-load-balance: [index:2 buckets:1 uRPF:2 to:[0:0]]
628 * [0] [@0]: dpo-drop ip6
631 * [@0]: dpo-load-balance: [index:4 buckets:1 uRPF:4 to:[0:0]]
632 * [0] [@0]: dpo-drop ip6
633 * ipv4-VRF:7, fib_index 1, flow hash: src dst sport dport proto
636 * [@0]: dpo-load-balance: [index:12 buckets:1 uRPF:11 to:[0:0]]
637 * [0] [@0]: dpo-drop ip6
640 * [@0]: dpo-load-balance: [index:13 buckets:1 uRPF:12 to:[0:0]]
641 * [0] [@0]: dpo-drop ip6
644 * [@0]: dpo-load-balance: [index:17 buckets:1 uRPF:16 to:[0:0]]
645 * [0] [@4]: ipv4-glean: af_packet0
648 * [@0]: dpo-load-balance: [index:18 buckets:1 uRPF:17 to:[1:84]]
649 * [0] [@2]: dpo-receive: 172.16.1.1 on af_packet0
652 * [@0]: dpo-load-balance: [index:21 buckets:1 uRPF:20 to:[0:0]]
653 * [0] [@5]: ipv4 via 172.16.1.2 af_packet0: IP4: 02:fe:9e:70:7a:2b -> 26:a5:f6:9c:3a:36
656 * [@0]: dpo-load-balance: [index:19 buckets:1 uRPF:18 to:[0:0]]
657 * [0] [@4]: ipv4-glean: af_packet1
660 * [@0]: dpo-load-balance: [index:20 buckets:1 uRPF:19 to:[0:0]]
661 * [0] [@2]: dpo-receive: 172.16.2.1 on af_packet1
664 * [@0]: dpo-load-balance: [index:15 buckets:1 uRPF:14 to:[0:0]]
665 * [0] [@0]: dpo-drop ip6
668 * [@0]: dpo-load-balance: [index:14 buckets:1 uRPF:13 to:[0:0]]
669 * [0] [@0]: dpo-drop ip6
672 * [@0]: dpo-load-balance: [index:16 buckets:1 uRPF:15 to:[0:0]]
673 * [0] [@0]: dpo-drop ip6
675 * Example of how to display a single IPv4 FIB table:
676 * @cliexstart{show ip fib table 7}
677 * ipv4-VRF:7, fib_index 1, flow hash: src dst sport dport proto
680 * [@0]: dpo-load-balance: [index:12 buckets:1 uRPF:11 to:[0:0]]
681 * [0] [@0]: dpo-drop ip6
684 * [@0]: dpo-load-balance: [index:13 buckets:1 uRPF:12 to:[0:0]]
685 * [0] [@0]: dpo-drop ip6
688 * [@0]: dpo-load-balance: [index:17 buckets:1 uRPF:16 to:[0:0]]
689 * [0] [@4]: ipv4-glean: af_packet0
692 * [@0]: dpo-load-balance: [index:18 buckets:1 uRPF:17 to:[1:84]]
693 * [0] [@2]: dpo-receive: 172.16.1.1 on af_packet0
696 * [@0]: dpo-load-balance: [index:21 buckets:1 uRPF:20 to:[0:0]]
697 * [0] [@5]: ipv4 via 172.16.1.2 af_packet0: IP4: 02:fe:9e:70:7a:2b -> 26:a5:f6:9c:3a:36
700 * [@0]: dpo-load-balance: [index:19 buckets:1 uRPF:18 to:[0:0]]
701 * [0] [@4]: ipv4-glean: af_packet1
704 * [@0]: dpo-load-balance: [index:20 buckets:1 uRPF:19 to:[0:0]]
705 * [0] [@2]: dpo-receive: 172.16.2.1 on af_packet1
708 * [@0]: dpo-load-balance: [index:15 buckets:1 uRPF:14 to:[0:0]]
709 * [0] [@0]: dpo-drop ip6
712 * [@0]: dpo-load-balance: [index:14 buckets:1 uRPF:13 to:[0:0]]
713 * [0] [@0]: dpo-drop ip6
716 * [@0]: dpo-load-balance: [index:16 buckets:1 uRPF:15 to:[0:0]]
717 * [0] [@0]: dpo-drop ip6
719 * Example of how to display a summary of all IPv4 FIB tables:
720 * @cliexstart{show ip fib summary}
721 * ipv4-VRF:0, fib_index 0, flow hash: src dst sport dport proto
722 * Prefix length Count
726 * ipv4-VRF:7, fib_index 1, flow hash: src dst sport dport proto
727 * Prefix length Count
735 VLIB_CLI_COMMAND (ip4_show_fib_command, static) = {
736 .path = "show ip fib",
737 .short_help = "show ip fib [summary] [table <table-id>] [index <fib-id>] [<ip4-addr>[/<mask>]] [mtrie] [detail]",
738 .function = ip4_show_fib,