blob: 29d121c96a78f88d3c59dd7afce1542ebba707e0 [file] [log] [blame]
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001/*
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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15
16#include <vnet/fib/fib_table.h>
17#include <vnet/fib/fib_entry.h>
18#include <vnet/fib/ip4_fib.h>
19
20/*
Lijian.Zhang33af8c12019-09-16 16:22:36 +080021 * A table of prefixes to be added to tables and the sources for them
Neale Ranns0bfe5d82016-08-25 15:29:12 +010022 */
23typedef 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;
28
29static const ip4_fib_table_special_prefix_t ip4_specials[] = {
30 {
31 /* 0.0.0.0/0*/
32 .ift_prefix = {
33 .fp_addr = {
34 .ip4.data_u32 = 0,
35 },
36 .fp_len = 0,
37 .fp_proto = FIB_PROTOCOL_IP4,
38 },
39 .ift_source = FIB_SOURCE_DEFAULT_ROUTE,
40 .ift_flag = FIB_ENTRY_FLAG_DROP,
41 },
42 {
43 /* 0.0.0.0/32*/
44 .ift_prefix = {
45 .fp_addr = {
46 .ip4.data_u32 = 0,
47 },
48 .fp_len = 32,
49 .fp_proto = FIB_PROTOCOL_IP4,
50 },
51 .ift_source = FIB_SOURCE_DEFAULT_ROUTE,
52 .ift_flag = FIB_ENTRY_FLAG_DROP,
53 },
54 {
55 /*
Neale Ranns86fb04d2016-12-01 17:03:25 +000056 * 240.0.0.0/4
Neale Ranns0bfe5d82016-08-25 15:29:12 +010057 * drop class E
58 */
59 .ift_prefix = {
60 .fp_addr = {
61 .ip4.data_u32 = 0xf0000000,
62 },
Neale Ranns86fb04d2016-12-01 17:03:25 +000063 .fp_len = 4,
Neale Ranns0bfe5d82016-08-25 15:29:12 +010064 .fp_proto = FIB_PROTOCOL_IP4,
65 },
66 .ift_source = FIB_SOURCE_SPECIAL,
67 .ift_flag = FIB_ENTRY_FLAG_DROP,
68
69 },
70 {
71 /*
Neale Ranns86fb04d2016-12-01 17:03:25 +000072 * 224.0.0.0/4
Neale Ranns0bfe5d82016-08-25 15:29:12 +010073 * drop all mcast
74 */
75 .ift_prefix = {
76 .fp_addr = {
77 .ip4.data_u32 = 0xe0000000,
78 },
Neale Ranns86fb04d2016-12-01 17:03:25 +000079 .fp_len = 4,
Neale Ranns0bfe5d82016-08-25 15:29:12 +010080 .fp_proto = FIB_PROTOCOL_IP4,
81 },
82 .ift_source = FIB_SOURCE_SPECIAL,
83 .ift_flag = FIB_ENTRY_FLAG_DROP,
84 },
85 {
86 /*
87 * 255.255.255.255/32
88 * drop, but we'll allow it to be usurped by the likes of DHCP
89 */
90 .ift_prefix = {
91 .fp_addr = {
92 .ip4.data_u32 = 0xffffffff,
93 },
94 .fp_len = 32,
95 .fp_proto = FIB_PROTOCOL_IP4,
96 },
97 .ift_source = FIB_SOURCE_DEFAULT_ROUTE,
98 .ift_flag = FIB_ENTRY_FLAG_DROP,
99 }
100};
101
102
103static u32
Neale Ranns15002542017-09-10 04:39:11 -0700104ip4_create_fib_with_table_id (u32 table_id,
105 fib_source_t src)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100106{
107 fib_table_t *fib_table;
Neale Rannsa3af3372017-03-28 03:49:52 -0700108 ip4_fib_t *v4_fib;
Neale Ranns1ec36522017-11-29 05:20:37 -0800109 void *old_heap;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100110
Dave Baracheb987d32018-05-03 08:26:39 -0400111 pool_get(ip4_main.fibs, fib_table);
Dave Barachb7b92992018-10-17 10:38:51 -0400112 clib_memset(fib_table, 0, sizeof(*fib_table));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100113
Neale Ranns1ec36522017-11-29 05:20:37 -0800114 old_heap = clib_mem_set_heap (ip4_main.mtrie_mheap);
Neale Rannsa3af3372017-03-28 03:49:52 -0700115 pool_get_aligned(ip4_main.v4_fibs, v4_fib, CLIB_CACHE_LINE_BYTES);
Neale Ranns1ec36522017-11-29 05:20:37 -0800116 clib_mem_set_heap (old_heap);
Neale Rannsa3af3372017-03-28 03:49:52 -0700117
118 ASSERT((fib_table - ip4_main.fibs) ==
119 (v4_fib - ip4_main.v4_fibs));
120
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100121 fib_table->ft_proto = FIB_PROTOCOL_IP4;
122 fib_table->ft_index =
Neale Rannsa3af3372017-03-28 03:49:52 -0700123 v4_fib->index =
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100124 (fib_table - ip4_main.fibs);
125
126 hash_set (ip4_main.fib_index_by_table_id, table_id, fib_table->ft_index);
127
128 fib_table->ft_table_id =
Neale Rannsa3af3372017-03-28 03:49:52 -0700129 v4_fib->table_id =
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100130 table_id;
Neale Ranns227038a2017-04-21 01:07:59 -0700131 fib_table->ft_flow_hash_config = IP_FLOW_HASH_DEFAULT;
Dave Barach2c8e0022020-02-11 15:06:34 -0500132
Neale Ranns15002542017-09-10 04:39:11 -0700133 fib_table_lock(fib_table->ft_index, FIB_PROTOCOL_IP4, src);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100134
Neale Rannsa3af3372017-03-28 03:49:52 -0700135 ip4_mtrie_init(&v4_fib->mtrie);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100136
137 /*
138 * add the special entries into the new FIB
139 */
140 int ii;
141
142 for (ii = 0; ii < ARRAY_LEN(ip4_specials); ii++)
143 {
144 fib_prefix_t prefix = ip4_specials[ii].ift_prefix;
145
146 prefix.fp_addr.ip4.data_u32 =
147 clib_host_to_net_u32(prefix.fp_addr.ip4.data_u32);
148
149 fib_table_entry_special_add(fib_table->ft_index,
150 &prefix,
151 ip4_specials[ii].ift_source,
Neale Rannsa0558302017-04-13 00:44:52 -0700152 ip4_specials[ii].ift_flag);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100153 }
154
155 return (fib_table->ft_index);
156}
157
158void
Neale Rannsa3af3372017-03-28 03:49:52 -0700159ip4_fib_table_destroy (u32 fib_index)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100160{
Neale Rannsa3af3372017-03-28 03:49:52 -0700161 fib_table_t *fib_table = pool_elt_at_index(ip4_main.fibs, fib_index);
162 ip4_fib_t *v4_fib = pool_elt_at_index(ip4_main.v4_fibs, fib_index);
Neale Ranns3bab8f92019-12-04 06:11:00 +0000163 u32 *n_locks;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100164 int ii;
165
166 /*
167 * remove all the specials we added when the table was created.
Neale Ranns04a75e32017-03-23 06:46:01 -0700168 * In reverse order so the default route is last.
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100169 */
Neale Ranns04a75e32017-03-23 06:46:01 -0700170 for (ii = ARRAY_LEN(ip4_specials) - 1; ii >= 0; ii--)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100171 {
172 fib_prefix_t prefix = ip4_specials[ii].ift_prefix;
173
174 prefix.fp_addr.ip4.data_u32 =
175 clib_host_to_net_u32(prefix.fp_addr.ip4.data_u32);
176
177 fib_table_entry_special_remove(fib_table->ft_index,
178 &prefix,
179 ip4_specials[ii].ift_source);
180 }
181
182 /*
183 * validate no more routes.
184 */
Neale Rannscbe25aa2019-09-30 10:53:31 +0000185#ifdef CLIB_DEBUG
186 if (0 != fib_table->ft_total_route_counts)
187 fib_table_assert_empty(fib_table);
188#endif
Neale Ranns3bab8f92019-12-04 06:11:00 +0000189
190 vec_foreach(n_locks, fib_table->ft_src_route_counts)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100191 {
Neale Ranns3bab8f92019-12-04 06:11:00 +0000192 ASSERT(0 == *n_locks);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100193 }
194
195 if (~0 != fib_table->ft_table_id)
196 {
197 hash_unset (ip4_main.fib_index_by_table_id, fib_table->ft_table_id);
198 }
Neale Rannsa3af3372017-03-28 03:49:52 -0700199
Neale Ranns3bab8f92019-12-04 06:11:00 +0000200 vec_free(fib_table->ft_src_route_counts);
Neale Rannsa3af3372017-03-28 03:49:52 -0700201 ip4_mtrie_free(&v4_fib->mtrie);
202
203 pool_put(ip4_main.v4_fibs, v4_fib);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100204 pool_put(ip4_main.fibs, fib_table);
205}
206
207
208u32
Neale Ranns15002542017-09-10 04:39:11 -0700209ip4_fib_table_find_or_create_and_lock (u32 table_id,
210 fib_source_t src)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100211{
212 u32 index;
213
214 index = ip4_fib_index_from_table_id(table_id);
215 if (~0 == index)
Neale Ranns15002542017-09-10 04:39:11 -0700216 return ip4_create_fib_with_table_id(table_id, src);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100217
Neale Ranns15002542017-09-10 04:39:11 -0700218 fib_table_lock(index, FIB_PROTOCOL_IP4, src);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100219
220 return (index);
221}
222
223u32
Neale Ranns15002542017-09-10 04:39:11 -0700224ip4_fib_table_create_and_lock (fib_source_t src)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100225{
Neale Ranns15002542017-09-10 04:39:11 -0700226 return (ip4_create_fib_with_table_id(~0, src));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100227}
228
229u32
230ip4_fib_table_get_index_for_sw_if_index (u32 sw_if_index)
231{
232 if (sw_if_index >= vec_len(ip4_main.fib_index_by_sw_if_index))
233 {
234 /*
235 * This is the case for interfaces that are not yet mapped to
236 * a IP table
237 */
238 return (~0);
239 }
240 return (ip4_main.fib_index_by_sw_if_index[sw_if_index]);
241}
242
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100243/*
244 * ip4_fib_table_lookup_exact_match
245 *
246 * Exact match prefix lookup
247 */
248fib_node_index_t
249ip4_fib_table_lookup_exact_match (const ip4_fib_t *fib,
250 const ip4_address_t *addr,
251 u32 len)
252{
253 uword * hash, * result;
254 u32 key;
255
256 hash = fib->fib_entry_by_dst_address[len];
257 key = (addr->data_u32 & ip4_main.fib_masks[len]);
258
259 result = hash_get(hash, key);
260
261 if (NULL != result) {
262 return (result[0]);
263 }
264 return (FIB_NODE_INDEX_INVALID);
265}
266
267/*
268 * ip4_fib_table_lookup_adj
269 *
270 * Longest prefix match
271 */
272index_t
273ip4_fib_table_lookup_lb (ip4_fib_t *fib,
274 const ip4_address_t *addr)
275{
276 fib_node_index_t fei;
277
278 fei = ip4_fib_table_lookup(fib, addr, 32);
279
280 if (FIB_NODE_INDEX_INVALID != fei)
281 {
282 const dpo_id_t *dpo;
283
284 dpo = fib_entry_contribute_ip_forwarding(fei);
285
286 return (dpo->dpoi_index);
287 }
288 return (INDEX_INVALID);
289}
290
291/*
292 * ip4_fib_table_lookup
293 *
294 * Longest prefix match
295 */
296fib_node_index_t
297ip4_fib_table_lookup (const ip4_fib_t *fib,
298 const ip4_address_t *addr,
299 u32 len)
300{
301 uword * hash, * result;
302 i32 mask_len;
303 u32 key;
304
305 for (mask_len = len; mask_len >= 0; mask_len--)
306 {
307 hash = fib->fib_entry_by_dst_address[mask_len];
308 key = (addr->data_u32 & ip4_main.fib_masks[mask_len]);
309
310 result = hash_get (hash, key);
311
312 if (NULL != result) {
313 return (result[0]);
314 }
315 }
316 return (FIB_NODE_INDEX_INVALID);
317}
318
319void
320ip4_fib_table_entry_insert (ip4_fib_t *fib,
321 const ip4_address_t *addr,
322 u32 len,
323 fib_node_index_t fib_entry_index)
324{
325 uword * hash, * result;
326 u32 key;
327
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);
331
332 if (NULL == result) {
333 /*
334 * adding a new entry
335 */
Neale Ranns1ec36522017-11-29 05:20:37 -0800336 uword *old_heap;
337 old_heap = clib_mem_set_heap (ip4_main.mtrie_mheap);
338
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100339 if (NULL == hash) {
340 hash = hash_create (32 /* elts */, sizeof (uword));
341 hash_set_flags (hash, HASH_FLAG_NO_AUTO_SHRINK);
Neale Ranns1ec36522017-11-29 05:20:37 -0800342
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100343 }
344 hash = hash_set(hash, key, fib_entry_index);
345 fib->fib_entry_by_dst_address[len] = hash;
Neale Ranns1ec36522017-11-29 05:20:37 -0800346 clib_mem_set_heap (old_heap);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100347 }
348 else
349 {
350 ASSERT(0);
351 }
352}
353
354void
355ip4_fib_table_entry_remove (ip4_fib_t *fib,
356 const ip4_address_t *addr,
357 u32 len)
358{
359 uword * hash, * result;
360 u32 key;
361
362 key = (addr->data_u32 & ip4_main.fib_masks[len]);
363 hash = fib->fib_entry_by_dst_address[len];
364 result = hash_get (hash, key);
365
366 if (NULL == result)
367 {
368 /*
Jim Thompsonf324dec2019-04-08 03:22:21 -0500369 * removing a non-existent entry. i'll allow it.
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100370 */
371 }
Dave Barach2c8e0022020-02-11 15:06:34 -0500372 else
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100373 {
Neale Ranns1ec36522017-11-29 05:20:37 -0800374 uword *old_heap;
375
376 old_heap = clib_mem_set_heap (ip4_main.mtrie_mheap);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100377 hash_unset(hash, key);
Neale Ranns1ec36522017-11-29 05:20:37 -0800378 clib_mem_set_heap (old_heap);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100379 }
380
381 fib->fib_entry_by_dst_address[len] = hash;
382}
383
384void
385ip4_fib_table_fwding_dpo_update (ip4_fib_t *fib,
386 const ip4_address_t *addr,
387 u32 len,
388 const dpo_id_t *dpo)
389{
Neale Rannsa3af3372017-03-28 03:49:52 -0700390 ip4_fib_mtrie_route_add(&fib->mtrie, addr, len, dpo->dpoi_index);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100391}
392
393void
394ip4_fib_table_fwding_dpo_remove (ip4_fib_t *fib,
395 const ip4_address_t *addr,
396 u32 len,
Neale Rannsa3af3372017-03-28 03:49:52 -0700397 const dpo_id_t *dpo,
398 u32 cover_index)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100399{
Neale Rannsc5d43172018-07-30 08:04:40 -0700400 const fib_prefix_t *cover_prefix;
Neale Rannsa3af3372017-03-28 03:49:52 -0700401 const dpo_id_t *cover_dpo;
402
403 /*
404 * We need to pass the MTRIE the LB index and address length of the
405 * covering prefix, so it can fill the plys with the correct replacement
406 * for the entry being removed
407 */
Neale Rannsc5d43172018-07-30 08:04:40 -0700408 cover_prefix = fib_entry_get_prefix(cover_index);
Neale Rannsa3af3372017-03-28 03:49:52 -0700409 cover_dpo = fib_entry_contribute_ip_forwarding(cover_index);
410
411 ip4_fib_mtrie_route_del(&fib->mtrie,
412 addr, len, dpo->dpoi_index,
Neale Rannsc5d43172018-07-30 08:04:40 -0700413 cover_prefix->fp_len,
Neale Rannsa3af3372017-03-28 03:49:52 -0700414 cover_dpo->dpoi_index);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100415}
416
Neale Ranns32e1c012016-11-22 17:07:28 +0000417void
418ip4_fib_table_walk (ip4_fib_t *fib,
419 fib_table_walk_fn_t fn,
420 void *ctx)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100421{
Neale Ranns89541992017-04-06 04:41:02 -0700422 fib_prefix_t root = {
423 .fp_proto = FIB_PROTOCOL_IP4,
424 // address and length default to all 0
425 };
426
427 /*
428 * A full tree walk is the dengenerate case of a sub-tree from
429 * the very root
430 */
431 return (ip4_fib_table_sub_tree_walk(fib, &root, fn, ctx));
432}
433
434void
435ip4_fib_table_sub_tree_walk (ip4_fib_t *fib,
436 const fib_prefix_t *root,
437 fib_table_walk_fn_t fn,
438 void *ctx)
439{
440 fib_prefix_t *sub_trees = NULL;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100441 int i;
442
Neale Ranns89541992017-04-06 04:41:02 -0700443 /*
Lijian.Zhang33af8c12019-09-16 16:22:36 +0800444 * There is no efficient way to walk this array of hash tables.
Neale Ranns89541992017-04-06 04:41:02 -0700445 * so we walk each table with a mask length greater than and equal to
446 * the required root and check it is covered by the root.
447 */
448 for (i = root->fp_len;
449 i < ARRAY_LEN (fib->fib_entry_by_dst_address);
450 i++)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100451 {
452 uword * hash = fib->fib_entry_by_dst_address[i];
453
454 if (NULL != hash)
455 {
Neale Ranns89541992017-04-06 04:41:02 -0700456 ip4_address_t key;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100457 hash_pair_t * p;
458
459 hash_foreach_pair (p, hash,
460 ({
Neale Ranns89541992017-04-06 04:41:02 -0700461 key.as_u32 = p->key;
462 if (ip4_destination_matches_route(&ip4_main,
463 &key,
464 &root->fp_addr.ip4,
465 root->fp_len))
466 {
467 const fib_prefix_t *sub_tree;
468 int skip = 0;
469
470 /*
471 * exclude sub-trees the walk does not want to explore
472 */
473 vec_foreach(sub_tree, sub_trees)
474 {
475 if (ip4_destination_matches_route(&ip4_main,
476 &key,
477 &sub_tree->fp_addr.ip4,
478 sub_tree->fp_len))
479 {
480 skip = 1;
481 break;
482 }
483 }
484
485 if (!skip)
486 {
487 switch (fn(p->value[0], ctx))
488 {
489 case FIB_TABLE_WALK_CONTINUE:
490 break;
491 case FIB_TABLE_WALK_SUB_TREE_STOP: {
492 fib_prefix_t pfx = {
493 .fp_proto = FIB_PROTOCOL_IP4,
494 .fp_len = i,
495 .fp_addr.ip4 = key,
496 };
497 vec_add1(sub_trees, pfx);
498 break;
499 }
500 case FIB_TABLE_WALK_STOP:
501 goto done;
502 }
503 }
504 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100505 }));
506 }
507 }
Neale Ranns89541992017-04-06 04:41:02 -0700508done:
509 vec_free(sub_trees);
510 return;
Neale Ranns32e1c012016-11-22 17:07:28 +0000511}
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100512
Neale Ranns32e1c012016-11-22 17:07:28 +0000513/**
514 * Walk show context
515 */
516typedef struct ip4_fib_show_walk_ctx_t_
517{
518 fib_node_index_t *ifsw_indicies;
519} ip4_fib_show_walk_ctx_t;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100520
Neale Ranns89541992017-04-06 04:41:02 -0700521static fib_table_walk_rc_t
Neale Ranns32e1c012016-11-22 17:07:28 +0000522ip4_fib_show_walk_cb (fib_node_index_t fib_entry_index,
523 void *arg)
524{
525 ip4_fib_show_walk_ctx_t *ctx = arg;
526
527 vec_add1(ctx->ifsw_indicies, fib_entry_index);
528
Neale Ranns89541992017-04-06 04:41:02 -0700529 return (FIB_TABLE_WALK_CONTINUE);
Neale Ranns32e1c012016-11-22 17:07:28 +0000530}
531
532static void
533ip4_fib_table_show_all (ip4_fib_t *fib,
534 vlib_main_t * vm)
535{
536 ip4_fib_show_walk_ctx_t ctx = {
537 .ifsw_indicies = NULL,
538 };
539 fib_node_index_t *fib_entry_index;
540
541 ip4_fib_table_walk(fib, ip4_fib_show_walk_cb, &ctx);
542 vec_sort_with_function(ctx.ifsw_indicies,
543 fib_entry_cmp_for_sort);
544
545 vec_foreach(fib_entry_index, ctx.ifsw_indicies)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100546 {
547 vlib_cli_output(vm, "%U",
548 format_fib_entry,
549 *fib_entry_index,
550 FIB_ENTRY_FORMAT_BRIEF);
551 }
552
Neale Ranns32e1c012016-11-22 17:07:28 +0000553 vec_free(ctx.ifsw_indicies);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100554}
555
556static void
557ip4_fib_table_show_one (ip4_fib_t *fib,
558 vlib_main_t * vm,
559 ip4_address_t *address,
Neale Ranns88fc83e2017-04-05 08:11:14 -0700560 u32 mask_len,
561 int detail)
Dave Barach2c8e0022020-02-11 15:06:34 -0500562{
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100563 vlib_cli_output(vm, "%U",
564 format_fib_entry,
565 ip4_fib_table_lookup(fib, address, mask_len),
Neale Ranns88fc83e2017-04-05 08:11:14 -0700566 (detail ?
567 FIB_ENTRY_FORMAT_DETAIL2 :
568 FIB_ENTRY_FORMAT_DETAIL));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100569}
570
Neale Rannsc87aafa2017-11-29 00:59:31 -0800571u8 *
572format_ip4_fib_table_memory (u8 * s, va_list * args)
573{
Neale Ranns05cac302019-05-28 11:09:40 +0000574 s = format(s, "%=30s %=6d %=12ld\n",
Dave Barach6a5adc32018-07-04 10:56:23 -0400575 "IPv4 unicast",
576 pool_elts(ip4_main.fibs),
577 mspace_footprint(ip4_main.mtrie_mheap));
Neale Rannsc87aafa2017-11-29 00:59:31 -0800578 return (s);
579}
580
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100581static clib_error_t *
582ip4_show_fib (vlib_main_t * vm,
583 unformat_input_t * input,
584 vlib_cli_command_t * cmd)
585{
586 ip4_main_t * im4 = &ip4_main;
587 fib_table_t * fib_table;
Neale Rannsc87aafa2017-11-29 00:59:31 -0800588 u64 total_mtrie_memory, total_hash_memory;
589 int verbose, matching, mtrie, memory;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100590 ip4_address_t matching_address;
591 u32 matching_mask = 32;
592 int i, table_id = -1, fib_index = ~0;
Neale Ranns88fc83e2017-04-05 08:11:14 -0700593 int detail = 0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100594
595 verbose = 1;
Neale Rannsc87aafa2017-11-29 00:59:31 -0800596 matching = mtrie = memory = 0;
597 total_hash_memory = total_mtrie_memory = 0;
598
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100599 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
600 {
601 if (unformat (input, "brief") || unformat (input, "summary")
602 || unformat (input, "sum"))
603 verbose = 0;
604
Neale Ranns88fc83e2017-04-05 08:11:14 -0700605 else if (unformat (input, "detail") || unformat (input, "det"))
606 detail = 1;
607
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100608 else if (unformat (input, "mtrie"))
609 mtrie = 1;
610
Neale Rannsc87aafa2017-11-29 00:59:31 -0800611 else if (unformat (input, "mem") ||
612 unformat (input, "memory"))
613 memory = 1;
614
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100615 else if (unformat (input, "%U/%d",
616 unformat_ip4_address, &matching_address, &matching_mask))
617 matching = 1;
618
619 else if (unformat (input, "%U", unformat_ip4_address, &matching_address))
620 matching = 1;
621
622 else if (unformat (input, "table %d", &table_id))
623 ;
624 else if (unformat (input, "index %d", &fib_index))
625 ;
626 else
627 break;
628 }
629
630 pool_foreach (fib_table, im4->fibs,
631 ({
Neale Rannsa3af3372017-03-28 03:49:52 -0700632 ip4_fib_t *fib = pool_elt_at_index(im4->v4_fibs, fib_table->ft_index);
Neale Ranns15002542017-09-10 04:39:11 -0700633 fib_source_t source;
634 u8 *s = NULL;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100635
636 if (table_id >= 0 && table_id != (int)fib->table_id)
637 continue;
638 if (fib_index != ~0 && fib_index != (int)fib->index)
639 continue;
640
Neale Rannsc87aafa2017-11-29 00:59:31 -0800641 if (memory)
642 {
Lollita Liue18b45c2019-01-11 05:23:12 -0500643 uword mtrie_size, hash_size, *old_heap;
644
Neale Rannsc87aafa2017-11-29 00:59:31 -0800645
646 mtrie_size = ip4_fib_mtrie_memory_usage(&fib->mtrie);
647 hash_size = 0;
648
Lollita Liue18b45c2019-01-11 05:23:12 -0500649 old_heap = clib_mem_set_heap (ip4_main.mtrie_mheap);
Neale Rannsc87aafa2017-11-29 00:59:31 -0800650 for (i = 0; i < ARRAY_LEN (fib->fib_entry_by_dst_address); i++)
651 {
652 uword * hash = fib->fib_entry_by_dst_address[i];
653 if (NULL != hash)
654 {
655 hash_size += hash_bytes(hash);
656 }
657 }
Lollita Liue18b45c2019-01-11 05:23:12 -0500658 clib_mem_set_heap (old_heap);
659
Neale Rannsc87aafa2017-11-29 00:59:31 -0800660 if (verbose)
661 vlib_cli_output (vm, "%U mtrie:%d hash:%d",
662 format_fib_table_name, fib->index,
663 FIB_PROTOCOL_IP4,
664 mtrie_size,
665 hash_size);
666 total_mtrie_memory += mtrie_size;
667 total_hash_memory += hash_size;
668 continue;
669 }
670
Neale Ranns9db6ada2019-11-08 12:42:31 +0000671 s = format(s, "%U, fib_index:%d, flow hash:[%U] epoch:%d flags:%U locks:[",
Neale Ranns15002542017-09-10 04:39:11 -0700672 format_fib_table_name, fib->index,
673 FIB_PROTOCOL_IP4,
674 fib->index,
675 format_ip_flow_hash_config,
Neale Ranns9db6ada2019-11-08 12:42:31 +0000676 fib_table->ft_flow_hash_config,
677 fib_table->ft_epoch,
678 format_fib_table_flags, fib_table->ft_flags);
Neale Ranns3bab8f92019-12-04 06:11:00 +0000679 vec_foreach_index(source, fib_table->ft_locks)
Neale Ranns15002542017-09-10 04:39:11 -0700680 {
681 if (0 != fib_table->ft_locks[source])
682 {
683 s = format(s, "%U:%d, ",
684 format_fib_source, source,
685 fib_table->ft_locks[source]);
686 }
687 }
688 s = format (s, "]");
Neale Ranns2297af02017-09-12 09:45:04 -0700689 vlib_cli_output (vm, "%v", s);
Neale Ranns15002542017-09-10 04:39:11 -0700690 vec_free(s);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100691
692 /* Show summary? */
Neale Ranns39194252017-11-27 01:03:25 -0800693 if (mtrie)
694 {
695 vlib_cli_output (vm, "%U", format_ip4_fib_mtrie, &fib->mtrie, verbose);
696 continue;
697 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100698 if (! verbose)
699 {
700 vlib_cli_output (vm, "%=20s%=16s", "Prefix length", "Count");
701 for (i = 0; i < ARRAY_LEN (fib->fib_entry_by_dst_address); i++)
702 {
703 uword * hash = fib->fib_entry_by_dst_address[i];
704 uword n_elts = hash_elts (hash);
705 if (n_elts > 0)
706 vlib_cli_output (vm, "%20d%16d", i, n_elts);
707 }
708 continue;
709 }
710
711 if (!matching)
712 {
713 ip4_fib_table_show_all(fib, vm);
714 }
715 else
716 {
Neale Ranns88fc83e2017-04-05 08:11:14 -0700717 ip4_fib_table_show_one(fib, vm, &matching_address,
718 matching_mask, detail);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100719 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100720 }));
721
Neale Rannsc87aafa2017-11-29 00:59:31 -0800722 if (memory)
Lollita Liue18b45c2019-01-11 05:23:12 -0500723 {
Neale Rannsc87aafa2017-11-29 00:59:31 -0800724 vlib_cli_output (vm, "totals: mtrie:%ld hash:%ld all:%ld",
725 total_mtrie_memory,
726 total_hash_memory,
727 total_mtrie_memory + total_hash_memory);
Lollita Liue18b45c2019-01-11 05:23:12 -0500728 vlib_cli_output (vm, "\nMtrie Mheap Usage: %U\n",
729 format_mheap, ip4_main.mtrie_mheap, 1);
730 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100731 return 0;
732}
733
734/*?
Billy McFall0683c9c2016-10-13 08:27:31 -0400735 * This command displays the IPv4 FIB Tables (VRF Tables) and the route
736 * entries for each table.
737 *
738 * @note This command will run for a long time when the FIB tables are
739 * comprised of millions of entries. For those senarios, consider displaying
740 * a single table or summary mode.
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100741 *
742 * @cliexpar
Billy McFall0683c9c2016-10-13 08:27:31 -0400743 * Example of how to display all the IPv4 FIB tables:
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100744 * @cliexstart{show ip fib}
Billy McFallebb9a6a2016-10-17 11:35:32 -0400745 * ipv4-VRF:0, fib_index 0, flow hash: src dst sport dport proto
746 * 0.0.0.0/0
747 * unicast-ip4-chain
748 * [@0]: dpo-load-balance: [index:0 buckets:1 uRPF:0 to:[0:0]]
749 * [0] [@0]: dpo-drop ip6
750 * 0.0.0.0/32
751 * unicast-ip4-chain
752 * [@0]: dpo-load-balance: [index:1 buckets:1 uRPF:1 to:[0:0]]
753 * [0] [@0]: dpo-drop ip6
754 * 6.0.1.2/32
755 * unicast-ip4-chain
756 * [@0]: dpo-load-balance: [index:30 buckets:1 uRPF:29 to:[0:0]]
757 * [0] [@3]: arp-ipv4: via 6.0.0.1 af_packet0
758 * 7.0.0.1/32
759 * unicast-ip4-chain
760 * [@0]: dpo-load-balance: [index:31 buckets:4 uRPF:30 to:[0:0]]
761 * [0] [@3]: arp-ipv4: via 6.0.0.2 af_packet0
762 * [1] [@3]: arp-ipv4: via 6.0.0.2 af_packet0
763 * [2] [@3]: arp-ipv4: via 6.0.0.2 af_packet0
764 * [3] [@3]: arp-ipv4: via 6.0.0.1 af_packet0
765 * 224.0.0.0/8
766 * unicast-ip4-chain
767 * [@0]: dpo-load-balance: [index:3 buckets:1 uRPF:3 to:[0:0]]
768 * [0] [@0]: dpo-drop ip6
769 * 240.0.0.0/8
770 * unicast-ip4-chain
771 * [@0]: dpo-load-balance: [index:2 buckets:1 uRPF:2 to:[0:0]]
772 * [0] [@0]: dpo-drop ip6
773 * 255.255.255.255/32
774 * unicast-ip4-chain
775 * [@0]: dpo-load-balance: [index:4 buckets:1 uRPF:4 to:[0:0]]
776 * [0] [@0]: dpo-drop ip6
777 * ipv4-VRF:7, fib_index 1, flow hash: src dst sport dport proto
778 * 0.0.0.0/0
779 * unicast-ip4-chain
780 * [@0]: dpo-load-balance: [index:12 buckets:1 uRPF:11 to:[0:0]]
781 * [0] [@0]: dpo-drop ip6
782 * 0.0.0.0/32
783 * unicast-ip4-chain
784 * [@0]: dpo-load-balance: [index:13 buckets:1 uRPF:12 to:[0:0]]
785 * [0] [@0]: dpo-drop ip6
786 * 172.16.1.0/24
787 * unicast-ip4-chain
788 * [@0]: dpo-load-balance: [index:17 buckets:1 uRPF:16 to:[0:0]]
789 * [0] [@4]: ipv4-glean: af_packet0
790 * 172.16.1.1/32
791 * unicast-ip4-chain
792 * [@0]: dpo-load-balance: [index:18 buckets:1 uRPF:17 to:[1:84]]
793 * [0] [@2]: dpo-receive: 172.16.1.1 on af_packet0
794 * 172.16.1.2/32
795 * unicast-ip4-chain
796 * [@0]: dpo-load-balance: [index:21 buckets:1 uRPF:20 to:[0:0]]
797 * [0] [@5]: ipv4 via 172.16.1.2 af_packet0: IP4: 02:fe:9e:70:7a:2b -> 26:a5:f6:9c:3a:36
798 * 172.16.2.0/24
799 * unicast-ip4-chain
800 * [@0]: dpo-load-balance: [index:19 buckets:1 uRPF:18 to:[0:0]]
801 * [0] [@4]: ipv4-glean: af_packet1
802 * 172.16.2.1/32
803 * unicast-ip4-chain
804 * [@0]: dpo-load-balance: [index:20 buckets:1 uRPF:19 to:[0:0]]
805 * [0] [@2]: dpo-receive: 172.16.2.1 on af_packet1
806 * 224.0.0.0/8
807 * unicast-ip4-chain
808 * [@0]: dpo-load-balance: [index:15 buckets:1 uRPF:14 to:[0:0]]
809 * [0] [@0]: dpo-drop ip6
810 * 240.0.0.0/8
811 * unicast-ip4-chain
812 * [@0]: dpo-load-balance: [index:14 buckets:1 uRPF:13 to:[0:0]]
813 * [0] [@0]: dpo-drop ip6
814 * 255.255.255.255/32
815 * unicast-ip4-chain
816 * [@0]: dpo-load-balance: [index:16 buckets:1 uRPF:15 to:[0:0]]
817 * [0] [@0]: dpo-drop ip6
Billy McFall0683c9c2016-10-13 08:27:31 -0400818 * @cliexend
819 * Example of how to display a single IPv4 FIB table:
820 * @cliexstart{show ip fib table 7}
Billy McFallebb9a6a2016-10-17 11:35:32 -0400821 * ipv4-VRF:7, fib_index 1, flow hash: src dst sport dport proto
822 * 0.0.0.0/0
823 * unicast-ip4-chain
824 * [@0]: dpo-load-balance: [index:12 buckets:1 uRPF:11 to:[0:0]]
825 * [0] [@0]: dpo-drop ip6
826 * 0.0.0.0/32
827 * unicast-ip4-chain
828 * [@0]: dpo-load-balance: [index:13 buckets:1 uRPF:12 to:[0:0]]
829 * [0] [@0]: dpo-drop ip6
830 * 172.16.1.0/24
831 * unicast-ip4-chain
832 * [@0]: dpo-load-balance: [index:17 buckets:1 uRPF:16 to:[0:0]]
833 * [0] [@4]: ipv4-glean: af_packet0
834 * 172.16.1.1/32
835 * unicast-ip4-chain
836 * [@0]: dpo-load-balance: [index:18 buckets:1 uRPF:17 to:[1:84]]
837 * [0] [@2]: dpo-receive: 172.16.1.1 on af_packet0
838 * 172.16.1.2/32
839 * unicast-ip4-chain
840 * [@0]: dpo-load-balance: [index:21 buckets:1 uRPF:20 to:[0:0]]
841 * [0] [@5]: ipv4 via 172.16.1.2 af_packet0: IP4: 02:fe:9e:70:7a:2b -> 26:a5:f6:9c:3a:36
842 * 172.16.2.0/24
843 * unicast-ip4-chain
844 * [@0]: dpo-load-balance: [index:19 buckets:1 uRPF:18 to:[0:0]]
845 * [0] [@4]: ipv4-glean: af_packet1
846 * 172.16.2.1/32
847 * unicast-ip4-chain
848 * [@0]: dpo-load-balance: [index:20 buckets:1 uRPF:19 to:[0:0]]
849 * [0] [@2]: dpo-receive: 172.16.2.1 on af_packet1
850 * 224.0.0.0/8
851 * unicast-ip4-chain
852 * [@0]: dpo-load-balance: [index:15 buckets:1 uRPF:14 to:[0:0]]
853 * [0] [@0]: dpo-drop ip6
854 * 240.0.0.0/8
855 * unicast-ip4-chain
856 * [@0]: dpo-load-balance: [index:14 buckets:1 uRPF:13 to:[0:0]]
857 * [0] [@0]: dpo-drop ip6
858 * 255.255.255.255/32
859 * unicast-ip4-chain
860 * [@0]: dpo-load-balance: [index:16 buckets:1 uRPF:15 to:[0:0]]
861 * [0] [@0]: dpo-drop ip6
Billy McFall0683c9c2016-10-13 08:27:31 -0400862 * @cliexend
863 * Example of how to display a summary of all IPv4 FIB tables:
864 * @cliexstart{show ip fib summary}
Billy McFallebb9a6a2016-10-17 11:35:32 -0400865 * ipv4-VRF:0, fib_index 0, flow hash: src dst sport dport proto
Billy McFall0683c9c2016-10-13 08:27:31 -0400866 * Prefix length Count
Billy McFallebb9a6a2016-10-17 11:35:32 -0400867 * 0 1
868 * 8 2
869 * 32 4
870 * ipv4-VRF:7, fib_index 1, flow hash: src dst sport dport proto
Billy McFall0683c9c2016-10-13 08:27:31 -0400871 * Prefix length Count
Billy McFallebb9a6a2016-10-17 11:35:32 -0400872 * 0 1
873 * 8 2
874 * 24 2
875 * 32 4
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100876 * @cliexend
877 ?*/
Billy McFall0683c9c2016-10-13 08:27:31 -0400878/* *INDENT-OFF* */
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100879VLIB_CLI_COMMAND (ip4_show_fib_command, static) = {
880 .path = "show ip fib",
Neale Ranns88fc83e2017-04-05 08:11:14 -0700881 .short_help = "show ip fib [summary] [table <table-id>] [index <fib-id>] [<ip4-addr>[/<mask>]] [mtrie] [detail]",
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100882 .function = ip4_show_fib,
883};
Billy McFall0683c9c2016-10-13 08:27:31 -0400884/* *INDENT-ON* */