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;
108 pool_get_aligned(ip4_main.fibs, fib_table, CLIB_CACHE_LINE_BYTES);
109 memset(fib_table, 0, sizeof(*fib_table));
111 fib_table->ft_proto = FIB_PROTOCOL_IP4;
112 fib_table->ft_index =
113 fib_table->v4.index =
114 (fib_table - ip4_main.fibs);
116 hash_set (ip4_main.fib_index_by_table_id, table_id, fib_table->ft_index);
118 fib_table->ft_table_id =
119 fib_table->v4.table_id =
121 fib_table->ft_flow_hash_config =
122 fib_table->v4.flow_hash_config =
123 IP_FLOW_HASH_DEFAULT;
124 fib_table->v4.fwd_classify_table_index = ~0;
125 fib_table->v4.rev_classify_table_index = ~0;
127 fib_table_lock(fib_table->ft_index, FIB_PROTOCOL_IP4);
129 ip4_mtrie_init(&fib_table->v4.mtrie);
132 * add the special entries into the new FIB
136 for (ii = 0; ii < ARRAY_LEN(ip4_specials); ii++)
138 fib_prefix_t prefix = ip4_specials[ii].ift_prefix;
140 prefix.fp_addr.ip4.data_u32 =
141 clib_host_to_net_u32(prefix.fp_addr.ip4.data_u32);
143 fib_table_entry_special_add(fib_table->ft_index,
145 ip4_specials[ii].ift_source,
146 ip4_specials[ii].ift_flag,
150 return (fib_table->ft_index);
154 ip4_fib_table_destroy (ip4_fib_t *fib)
156 fib_table_t *fib_table = (fib_table_t*)fib;
160 * remove all the specials we added when the table was created.
162 for (ii = 0; ii < ARRAY_LEN(ip4_specials); ii++)
164 fib_prefix_t prefix = ip4_specials[ii].ift_prefix;
166 prefix.fp_addr.ip4.data_u32 =
167 clib_host_to_net_u32(prefix.fp_addr.ip4.data_u32);
169 fib_table_entry_special_remove(fib_table->ft_index,
171 ip4_specials[ii].ift_source);
175 * validate no more routes.
177 ASSERT(0 == fib_table->ft_total_route_counts);
178 FOR_EACH_FIB_SOURCE(ii)
180 ASSERT(0 == fib_table->ft_src_route_counts[ii]);
183 if (~0 != fib_table->ft_table_id)
185 hash_unset (ip4_main.fib_index_by_table_id, fib_table->ft_table_id);
187 pool_put(ip4_main.fibs, fib_table);
192 ip4_fib_table_find_or_create_and_lock (u32 table_id)
196 index = ip4_fib_index_from_table_id(table_id);
198 return ip4_create_fib_with_table_id(table_id);
200 fib_table_lock(index, FIB_PROTOCOL_IP4);
206 ip4_fib_table_create_and_lock (void)
208 return (ip4_create_fib_with_table_id(~0));
212 ip4_fib_table_get_index_for_sw_if_index (u32 sw_if_index)
214 if (sw_if_index >= vec_len(ip4_main.fib_index_by_sw_if_index))
217 * This is the case for interfaces that are not yet mapped to
222 return (ip4_main.fib_index_by_sw_if_index[sw_if_index]);
226 ip4_fib_table_get_flow_hash_config (u32 fib_index)
228 return (ip4_fib_get(fib_index)->flow_hash_config);
232 * ip4_fib_table_lookup_exact_match
234 * Exact match prefix lookup
237 ip4_fib_table_lookup_exact_match (const ip4_fib_t *fib,
238 const ip4_address_t *addr,
241 uword * hash, * result;
244 hash = fib->fib_entry_by_dst_address[len];
245 key = (addr->data_u32 & ip4_main.fib_masks[len]);
247 result = hash_get(hash, key);
249 if (NULL != result) {
252 return (FIB_NODE_INDEX_INVALID);
256 * ip4_fib_table_lookup_adj
258 * Longest prefix match
261 ip4_fib_table_lookup_lb (ip4_fib_t *fib,
262 const ip4_address_t *addr)
264 fib_node_index_t fei;
266 fei = ip4_fib_table_lookup(fib, addr, 32);
268 if (FIB_NODE_INDEX_INVALID != fei)
272 dpo = fib_entry_contribute_ip_forwarding(fei);
274 return (dpo->dpoi_index);
276 return (INDEX_INVALID);
280 * ip4_fib_table_lookup
282 * Longest prefix match
285 ip4_fib_table_lookup (const ip4_fib_t *fib,
286 const ip4_address_t *addr,
289 uword * hash, * result;
293 for (mask_len = len; mask_len >= 0; mask_len--)
295 hash = fib->fib_entry_by_dst_address[mask_len];
296 key = (addr->data_u32 & ip4_main.fib_masks[mask_len]);
298 result = hash_get (hash, key);
300 if (NULL != result) {
304 return (FIB_NODE_INDEX_INVALID);
308 ip4_fib_table_entry_insert (ip4_fib_t *fib,
309 const ip4_address_t *addr,
311 fib_node_index_t fib_entry_index)
313 uword * hash, * result;
316 key = (addr->data_u32 & ip4_main.fib_masks[len]);
317 hash = fib->fib_entry_by_dst_address[len];
318 result = hash_get (hash, key);
320 if (NULL == result) {
325 hash = hash_create (32 /* elts */, sizeof (uword));
326 hash_set_flags (hash, HASH_FLAG_NO_AUTO_SHRINK);
328 hash = hash_set(hash, key, fib_entry_index);
329 fib->fib_entry_by_dst_address[len] = hash;
338 ip4_fib_table_entry_remove (ip4_fib_t *fib,
339 const ip4_address_t *addr,
342 uword * hash, * result;
345 key = (addr->data_u32 & ip4_main.fib_masks[len]);
346 hash = fib->fib_entry_by_dst_address[len];
347 result = hash_get (hash, key);
352 * removing a non-existant entry. i'll allow it.
357 hash_unset(hash, key);
360 fib->fib_entry_by_dst_address[len] = hash;
364 ip4_fib_table_fwding_dpo_update (ip4_fib_t *fib,
365 const ip4_address_t *addr,
369 ip4_fib_mtrie_add_del_route(fib, *addr, len, dpo->dpoi_index, 0); // ADD
373 ip4_fib_table_fwding_dpo_remove (ip4_fib_t *fib,
374 const ip4_address_t *addr,
378 ip4_fib_mtrie_add_del_route(fib, *addr, len, dpo->dpoi_index, 1); // DELETE
382 ip4_fib_table_show_all (ip4_fib_t *fib,
385 fib_node_index_t *fib_entry_indicies;
386 fib_node_index_t *fib_entry_index;
389 fib_entry_indicies = NULL;
391 for (i = 0; i < ARRAY_LEN (fib->fib_entry_by_dst_address); i++)
393 uword * hash = fib->fib_entry_by_dst_address[i];
399 hash_foreach_pair (p, hash,
401 vec_add1(fib_entry_indicies, p->value[0]);
406 vec_sort_with_function(fib_entry_indicies, fib_entry_cmp_for_sort);
408 vec_foreach(fib_entry_index, fib_entry_indicies)
410 vlib_cli_output(vm, "%U",
413 FIB_ENTRY_FORMAT_BRIEF);
416 vec_free(fib_entry_indicies);
420 ip4_fib_table_show_one (ip4_fib_t *fib,
422 ip4_address_t *address,
425 vlib_cli_output(vm, "%U",
427 ip4_fib_table_lookup(fib, address, mask_len),
428 FIB_ENTRY_FORMAT_DETAIL);
431 static clib_error_t *
432 ip4_show_fib (vlib_main_t * vm,
433 unformat_input_t * input,
434 vlib_cli_command_t * cmd)
436 ip4_main_t * im4 = &ip4_main;
437 fib_table_t * fib_table;
438 int verbose, matching, mtrie;
439 ip4_address_t matching_address;
440 u32 matching_mask = 32;
441 int i, table_id = -1, fib_index = ~0;
446 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
448 if (unformat (input, "brief") || unformat (input, "summary")
449 || unformat (input, "sum"))
452 else if (unformat (input, "mtrie"))
455 else if (unformat (input, "%U/%d",
456 unformat_ip4_address, &matching_address, &matching_mask))
459 else if (unformat (input, "%U", unformat_ip4_address, &matching_address))
462 else if (unformat (input, "table %d", &table_id))
464 else if (unformat (input, "index %d", &fib_index))
470 pool_foreach (fib_table, im4->fibs,
472 ip4_fib_t *fib = &fib_table->v4;
474 if (table_id >= 0 && table_id != (int)fib->table_id)
476 if (fib_index != ~0 && fib_index != (int)fib->index)
479 vlib_cli_output (vm, "%U, fib_index %d, flow hash: %U",
480 format_fib_table_name, fib->index, FIB_PROTOCOL_IP4,
482 format_ip_flow_hash_config, fib->flow_hash_config);
487 vlib_cli_output (vm, "%=20s%=16s", "Prefix length", "Count");
488 for (i = 0; i < ARRAY_LEN (fib->fib_entry_by_dst_address); i++)
490 uword * hash = fib->fib_entry_by_dst_address[i];
491 uword n_elts = hash_elts (hash);
493 vlib_cli_output (vm, "%20d%16d", i, n_elts);
500 ip4_fib_table_show_all(fib, vm);
504 ip4_fib_table_show_one(fib, vm, &matching_address, matching_mask);
508 vlib_cli_output (vm, "%U", format_ip4_fib_mtrie, &fib->mtrie);
515 * This command displays the IPv4 FIB Tables (VRF Tables) and the route
516 * entries for each table.
518 * @note This command will run for a long time when the FIB tables are
519 * comprised of millions of entries. For those senarios, consider displaying
520 * a single table or summary mode.
523 * Example of how to display all the IPv4 FIB tables:
524 * @cliexstart{show ip fib}
525 * Table 0, fib_index 0, flow hash: src dst sport dport proto
526 * Destination Packets Bytes Adjacency
527 * 172.16.2.0/24 0 0 weight 1, index 5
529 * 172.16.2.1/32 0 0 weight 1, index 6
531 * Table 7, fib_index 1, flow hash: dst sport dport proto
532 * Destination Packets Bytes Adjacency
533 * 172.16.1.0/24 0 0 weight 1, index 3
535 * 172.16.1.1/32 1 98 weight 1, index 4
537 * 172.16.1.2/32 0 0 weight 1, index 7
538 * GigabitEthernet2/0/0
539 * IP4: 02:fe:6a:07:39:6f -> 16:d9:e0:91:79:86
541 * Example of how to display a single IPv4 FIB table:
542 * @cliexstart{show ip fib table 7}
543 * Table 7, fib_index 1, flow hash: dst sport dport proto
544 * Destination Packets Bytes Adjacency
545 * 172.16.1.0/24 0 0 weight 1, index 3
547 * 172.16.1.1/32 1 98 weight 1, index 4
549 * 172.16.1.2/32 0 0 weight 1, index 7
550 * GigabitEthernet2/0/0
551 * IP4: 02:fe:6a:07:39:6f -> 16:d9:e0:91:79:86
553 * Example of how to display a summary of all IPv4 FIB tables:
554 * @cliexstart{show ip fib summary}
555 * Table 0, fib_index 0, flow hash: src dst sport dport proto
556 * Prefix length Count
559 * Table 7, fib_index 1, flow hash: dst sport dport proto
560 * Prefix length Count
566 VLIB_CLI_COMMAND (ip4_show_fib_command, static) = {
567 .path = "show ip fib",
568 .short_help = "show ip fib [mtrie] [summary] [table <n>] [<ip4-addr>] [clear] [include-empty]",
569 .function = ip4_show_fib,