blob: 22b47579d3ea3093b0b11ee337c21badc63994f2 [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/ip6_fib.h>
17#include <vnet/fib/fib_table.h>
Neale Ranns53da2212018-02-24 02:11:19 -080018#include <vnet/dpo/ip6_ll_dpo.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010019
20static void
21vnet_ip6_fib_init (u32 fib_index)
22{
23 fib_prefix_t pfx = {
24 .fp_proto = FIB_PROTOCOL_IP6,
25 .fp_len = 0,
26 .fp_addr = {
27 .ip6 = {
28 { 0, 0, },
29 },
30 }
31 };
32
33 /*
34 * Add the default route.
35 */
36 fib_table_entry_special_add(fib_index,
37 &pfx,
38 FIB_SOURCE_DEFAULT_ROUTE,
Neale Rannsa0558302017-04-13 00:44:52 -070039 FIB_ENTRY_FLAG_DROP);
Neale Ranns0bfe5d82016-08-25 15:29:12 +010040
41 /*
Neale Ranns53da2212018-02-24 02:11:19 -080042 * all link local via the link local lookup DPO
Neale Ranns0bfe5d82016-08-25 15:29:12 +010043 */
44 pfx.fp_addr.ip6.as_u64[0] = clib_host_to_net_u64 (0xFE80000000000000ULL);
45 pfx.fp_addr.ip6.as_u64[1] = 0;
46 pfx.fp_len = 10;
Neale Ranns53da2212018-02-24 02:11:19 -080047 fib_table_entry_special_dpo_add(fib_index,
48 &pfx,
49 FIB_SOURCE_SPECIAL,
50 FIB_ENTRY_FLAG_NONE,
51 ip6_ll_dpo_get());
Neale Ranns0bfe5d82016-08-25 15:29:12 +010052}
53
54static u32
Neale Ranns15002542017-09-10 04:39:11 -070055create_fib_with_table_id (u32 table_id,
Neale Ranns53da2212018-02-24 02:11:19 -080056 fib_source_t src,
57 fib_table_flags_t flags,
58 u8 *desc)
Neale Ranns0bfe5d82016-08-25 15:29:12 +010059{
60 fib_table_t *fib_table;
Neale Rannsa3af3372017-03-28 03:49:52 -070061 ip6_fib_t *v6_fib;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010062
Dave Baracheb987d32018-05-03 08:26:39 -040063 pool_get(ip6_main.fibs, fib_table);
Neale Rannsa3af3372017-03-28 03:49:52 -070064 pool_get_aligned(ip6_main.v6_fibs, v6_fib, CLIB_CACHE_LINE_BYTES);
Neale Ranns0bfe5d82016-08-25 15:29:12 +010065
Neale Rannsa3af3372017-03-28 03:49:52 -070066 memset(fib_table, 0, sizeof(*fib_table));
67 memset(v6_fib, 0, sizeof(*v6_fib));
68
69 ASSERT((fib_table - ip6_main.fibs) ==
70 (v6_fib - ip6_main.v6_fibs));
71
Neale Ranns0bfe5d82016-08-25 15:29:12 +010072 fib_table->ft_proto = FIB_PROTOCOL_IP6;
73 fib_table->ft_index =
Neale Rannsa3af3372017-03-28 03:49:52 -070074 v6_fib->index =
75 (fib_table - ip6_main.fibs);
Neale Ranns0bfe5d82016-08-25 15:29:12 +010076
77 hash_set(ip6_main.fib_index_by_table_id, table_id, fib_table->ft_index);
78
79 fib_table->ft_table_id =
Neale Rannsa3af3372017-03-28 03:49:52 -070080 v6_fib->table_id =
Neale Ranns0bfe5d82016-08-25 15:29:12 +010081 table_id;
Neale Ranns227038a2017-04-21 01:07:59 -070082 fib_table->ft_flow_hash_config = IP_FLOW_HASH_DEFAULT;
Neale Ranns53da2212018-02-24 02:11:19 -080083 fib_table->ft_flags = flags;
84 fib_table->ft_desc = desc;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010085
86 vnet_ip6_fib_init(fib_table->ft_index);
Neale Ranns15002542017-09-10 04:39:11 -070087 fib_table_lock(fib_table->ft_index, FIB_PROTOCOL_IP6, src);
Neale Ranns0bfe5d82016-08-25 15:29:12 +010088
89 return (fib_table->ft_index);
90}
91
92u32
Neale Ranns15002542017-09-10 04:39:11 -070093ip6_fib_table_find_or_create_and_lock (u32 table_id,
94 fib_source_t src)
Neale Ranns0bfe5d82016-08-25 15:29:12 +010095{
96 uword * p;
97
98 p = hash_get (ip6_main.fib_index_by_table_id, table_id);
99 if (NULL == p)
Neale Ranns53da2212018-02-24 02:11:19 -0800100 return create_fib_with_table_id(table_id, src,
101 FIB_TABLE_FLAG_NONE,
102 NULL);
103
Neale Ranns15002542017-09-10 04:39:11 -0700104 fib_table_lock(p[0], FIB_PROTOCOL_IP6, src);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100105
106 return (p[0]);
107}
108
109u32
Neale Ranns53da2212018-02-24 02:11:19 -0800110ip6_fib_table_create_and_lock (fib_source_t src,
111 fib_table_flags_t flags,
112 u8 *desc)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100113{
Neale Ranns53da2212018-02-24 02:11:19 -0800114 return (create_fib_with_table_id(~0, src, flags, desc));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100115}
116
117void
118ip6_fib_table_destroy (u32 fib_index)
119{
Neale Ranns53da2212018-02-24 02:11:19 -0800120 /*
121 * all link local first ...
122 */
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100123 fib_prefix_t pfx = {
124 .fp_proto = FIB_PROTOCOL_IP6,
Neale Ranns53da2212018-02-24 02:11:19 -0800125 .fp_len = 10,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100126 .fp_addr = {
127 .ip6 = {
Neale Ranns53da2212018-02-24 02:11:19 -0800128 .as_u8 = {
129 [0] = 0xFE,
130 [1] = 0x80,
131 },
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100132 },
133 }
134 };
Neale Ranns53da2212018-02-24 02:11:19 -0800135 fib_table_entry_delete(fib_index,
136 &pfx,
137 FIB_SOURCE_SPECIAL);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100138
139 /*
Neale Ranns53da2212018-02-24 02:11:19 -0800140 * ... then the default route.
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100141 */
Neale Ranns53da2212018-02-24 02:11:19 -0800142 pfx.fp_addr.ip6.as_u64[0] = 0;
143 pfx.fp_len = 00;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100144 fib_table_entry_special_remove(fib_index,
145 &pfx,
146 FIB_SOURCE_DEFAULT_ROUTE);
147
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100148 fib_table_t *fib_table = fib_table_get(fib_index, FIB_PROTOCOL_IP6);
149 fib_source_t source;
Neale Ranns53da2212018-02-24 02:11:19 -0800150
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100151 /*
152 * validate no more routes.
153 */
154 ASSERT(0 == fib_table->ft_total_route_counts);
155 FOR_EACH_FIB_SOURCE(source)
156 {
157 ASSERT(0 == fib_table->ft_src_route_counts[source]);
158 }
159
160 if (~0 != fib_table->ft_table_id)
161 {
162 hash_unset (ip6_main.fib_index_by_table_id, fib_table->ft_table_id);
163 }
Neale Rannsa3af3372017-03-28 03:49:52 -0700164 pool_put_index(ip6_main.v6_fibs, fib_table->ft_index);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100165 pool_put(ip6_main.fibs, fib_table);
166}
167
168fib_node_index_t
169ip6_fib_table_lookup (u32 fib_index,
170 const ip6_address_t *addr,
171 u32 len)
172{
Dave Barach908a5ea2017-07-14 12:42:21 -0400173 ip6_fib_table_instance_t *table;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100174 BVT(clib_bihash_kv) kv, value;
175 int i, n_p, rv;
176 u64 fib;
177
178 table = &ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING];
179 n_p = vec_len (table->prefix_lengths_in_search_order);
180
181 kv.key[0] = addr->as_u64[0];
182 kv.key[1] = addr->as_u64[1];
183 fib = ((u64)((fib_index))<<32);
184
185 /*
186 * start search from a mask length same length or shorter.
187 * we don't want matches longer than the mask passed
188 */
189 i = 0;
190 while (i < n_p && table->prefix_lengths_in_search_order[i] > len)
191 {
192 i++;
193 }
194
195 for (; i < n_p; i++)
196 {
197 int dst_address_length = table->prefix_lengths_in_search_order[i];
198 ip6_address_t * mask = &ip6_main.fib_masks[dst_address_length];
199
200 ASSERT(dst_address_length >= 0 && dst_address_length <= 128);
201 //As lengths are decreasing, masks are increasingly specific.
202 kv.key[0] &= mask->as_u64[0];
203 kv.key[1] &= mask->as_u64[1];
204 kv.key[2] = fib | dst_address_length;
205
206 rv = BV(clib_bihash_search_inline_2)(&table->ip6_hash, &kv, &value);
207 if (rv == 0)
208 return value.value;
209 }
210
211 return (FIB_NODE_INDEX_INVALID);
212}
213
214fib_node_index_t
215ip6_fib_table_lookup_exact_match (u32 fib_index,
216 const ip6_address_t *addr,
217 u32 len)
218{
Dave Barach908a5ea2017-07-14 12:42:21 -0400219 ip6_fib_table_instance_t *table;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100220 BVT(clib_bihash_kv) kv, value;
221 ip6_address_t *mask;
222 u64 fib;
223 int rv;
224
225 table = &ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING];
226 mask = &ip6_main.fib_masks[len];
227 fib = ((u64)((fib_index))<<32);
228
229 kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
230 kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
231 kv.key[2] = fib | len;
232
233 rv = BV(clib_bihash_search_inline_2)(&table->ip6_hash, &kv, &value);
234 if (rv == 0)
235 return value.value;
236
237 return (FIB_NODE_INDEX_INVALID);
238}
239
240static void
241compute_prefix_lengths_in_search_order (ip6_fib_table_instance_t *table)
242{
243 int i;
244 vec_reset_length (table->prefix_lengths_in_search_order);
245 /* Note: bitmap reversed so this is in fact a longest prefix match */
246 clib_bitmap_foreach (i, table->non_empty_dst_address_length_bitmap,
247 ({
248 int dst_address_length = 128 - i;
249 vec_add1(table->prefix_lengths_in_search_order, dst_address_length);
250 }));
251}
252
253void
254ip6_fib_table_entry_remove (u32 fib_index,
255 const ip6_address_t *addr,
256 u32 len)
257{
258 ip6_fib_table_instance_t *table;
259 BVT(clib_bihash_kv) kv;
260 ip6_address_t *mask;
261 u64 fib;
262
263 table = &ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING];
264 mask = &ip6_main.fib_masks[len];
265 fib = ((u64)((fib_index))<<32);
266
267 kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
268 kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
269 kv.key[2] = fib | len;
270
271 BV(clib_bihash_add_del)(&table->ip6_hash, &kv, 0);
272
273 /* refcount accounting */
274 ASSERT (table->dst_address_length_refcounts[len] > 0);
275 if (--table->dst_address_length_refcounts[len] == 0)
276 {
277 table->non_empty_dst_address_length_bitmap =
278 clib_bitmap_set (table->non_empty_dst_address_length_bitmap,
279 128 - len, 0);
280 compute_prefix_lengths_in_search_order (table);
281 }
282}
283
284void
285ip6_fib_table_entry_insert (u32 fib_index,
286 const ip6_address_t *addr,
287 u32 len,
288 fib_node_index_t fib_entry_index)
289{
290 ip6_fib_table_instance_t *table;
291 BVT(clib_bihash_kv) kv;
292 ip6_address_t *mask;
293 u64 fib;
294
295 table = &ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING];
296 mask = &ip6_main.fib_masks[len];
297 fib = ((u64)((fib_index))<<32);
298
299 kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
300 kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
301 kv.key[2] = fib | len;
302 kv.value = fib_entry_index;
303
304 BV(clib_bihash_add_del)(&table->ip6_hash, &kv, 1);
305
306 table->dst_address_length_refcounts[len]++;
307
308 table->non_empty_dst_address_length_bitmap =
309 clib_bitmap_set (table->non_empty_dst_address_length_bitmap,
310 128 - len, 1);
311 compute_prefix_lengths_in_search_order (table);
312}
313
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100314u32 ip6_fib_table_fwding_lookup_with_if_index (ip6_main_t * im,
315 u32 sw_if_index,
316 const ip6_address_t * dst)
317{
318 u32 fib_index = vec_elt (im->fib_index_by_sw_if_index, sw_if_index);
319 return ip6_fib_table_fwding_lookup(im, fib_index, dst);
320}
321
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100322u32
323ip6_fib_table_get_index_for_sw_if_index (u32 sw_if_index)
324{
325 if (sw_if_index >= vec_len(ip6_main.fib_index_by_sw_if_index))
326 {
327 /*
328 * This is the case for interfaces that are not yet mapped to
329 * a IP table
330 */
331 return (~0);
332 }
333 return (ip6_main.fib_index_by_sw_if_index[sw_if_index]);
334}
335
336void
337ip6_fib_table_fwding_dpo_update (u32 fib_index,
338 const ip6_address_t *addr,
339 u32 len,
340 const dpo_id_t *dpo)
341{
342 ip6_fib_table_instance_t *table;
343 BVT(clib_bihash_kv) kv;
344 ip6_address_t *mask;
345 u64 fib;
346
347 table = &ip6_main.ip6_table[IP6_FIB_TABLE_FWDING];
348 mask = &ip6_main.fib_masks[len];
349 fib = ((u64)((fib_index))<<32);
350
351 kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
352 kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
353 kv.key[2] = fib | len;
354 kv.value = dpo->dpoi_index;
355
356 BV(clib_bihash_add_del)(&table->ip6_hash, &kv, 1);
357
358 table->dst_address_length_refcounts[len]++;
359
360 table->non_empty_dst_address_length_bitmap =
361 clib_bitmap_set (table->non_empty_dst_address_length_bitmap,
362 128 - len, 1);
363 compute_prefix_lengths_in_search_order (table);
364}
365
366void
367ip6_fib_table_fwding_dpo_remove (u32 fib_index,
368 const ip6_address_t *addr,
369 u32 len,
370 const dpo_id_t *dpo)
371{
372 ip6_fib_table_instance_t *table;
373 BVT(clib_bihash_kv) kv;
374 ip6_address_t *mask;
375 u64 fib;
376
377 table = &ip6_main.ip6_table[IP6_FIB_TABLE_FWDING];
378 mask = &ip6_main.fib_masks[len];
379 fib = ((u64)((fib_index))<<32);
380
381 kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
382 kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
383 kv.key[2] = fib | len;
384 kv.value = dpo->dpoi_index;
385
386 BV(clib_bihash_add_del)(&table->ip6_hash, &kv, 0);
387
388 /* refcount accounting */
389 ASSERT (table->dst_address_length_refcounts[len] > 0);
390 if (--table->dst_address_length_refcounts[len] == 0)
391 {
392 table->non_empty_dst_address_length_bitmap =
Neale Ranns32e1c012016-11-22 17:07:28 +0000393 clib_bitmap_set (table->non_empty_dst_address_length_bitmap,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100394 128 - len, 0);
395 compute_prefix_lengths_in_search_order (table);
396 }
397}
398
Neale Ranns32e1c012016-11-22 17:07:28 +0000399/**
400 * @brief Context when walking the IPv6 table. Since all VRFs are in the
401 * same hash table, we need to filter only those we need as we walk
402 */
403typedef struct ip6_fib_walk_ctx_t_
404{
405 u32 i6w_fib_index;
406 fib_table_walk_fn_t i6w_fn;
407 void *i6w_ctx;
Neale Ranns89541992017-04-06 04:41:02 -0700408 fib_prefix_t i6w_root;
409 fib_prefix_t *i6w_sub_trees;
Neale Ranns32e1c012016-11-22 17:07:28 +0000410} ip6_fib_walk_ctx_t;
411
412static int
413ip6_fib_walk_cb (clib_bihash_kv_24_8_t * kvp,
414 void *arg)
415{
416 ip6_fib_walk_ctx_t *ctx = arg;
Neale Ranns89541992017-04-06 04:41:02 -0700417 ip6_address_t key;
Neale Ranns32e1c012016-11-22 17:07:28 +0000418
419 if ((kvp->key[2] >> 32) == ctx->i6w_fib_index)
420 {
Neale Ranns89541992017-04-06 04:41:02 -0700421 key.as_u64[0] = kvp->key[0];
422 key.as_u64[1] = kvp->key[1];
423
424 if (ip6_destination_matches_route(&ip6_main,
425 &key,
426 &ctx->i6w_root.fp_addr.ip6,
427 ctx->i6w_root.fp_len))
428 {
429 const fib_prefix_t *sub_tree;
430 int skip = 0;
431
432 /*
433 * exclude sub-trees the walk does not want to explore
434 */
435 vec_foreach(sub_tree, ctx->i6w_sub_trees)
436 {
437 if (ip6_destination_matches_route(&ip6_main,
438 &key,
439 &sub_tree->fp_addr.ip6,
440 sub_tree->fp_len))
441 {
442 skip = 1;
443 break;
444 }
445 }
446
447 if (!skip)
448 {
449 switch (ctx->i6w_fn(kvp->value, ctx->i6w_ctx))
450 {
451 case FIB_TABLE_WALK_CONTINUE:
452 break;
453 case FIB_TABLE_WALK_SUB_TREE_STOP: {
454 fib_prefix_t pfx = {
455 .fp_proto = FIB_PROTOCOL_IP6,
456 .fp_len = kvp->key[2] & 0xffffffff,
457 .fp_addr.ip6 = key,
458 };
459 vec_add1(ctx->i6w_sub_trees, pfx);
460 break;
461 }
462 case FIB_TABLE_WALK_STOP:
463 goto done;
464 }
465 }
466 }
Neale Ranns32e1c012016-11-22 17:07:28 +0000467 }
Neale Ranns89541992017-04-06 04:41:02 -0700468done:
Neale Ranns32e1c012016-11-22 17:07:28 +0000469
470 return (1);
471}
472
473void
474ip6_fib_table_walk (u32 fib_index,
475 fib_table_walk_fn_t fn,
476 void *arg)
477{
478 ip6_fib_walk_ctx_t ctx = {
479 .i6w_fib_index = fib_index,
480 .i6w_fn = fn,
481 .i6w_ctx = arg,
Neale Ranns89541992017-04-06 04:41:02 -0700482 .i6w_root = {
483 .fp_proto = FIB_PROTOCOL_IP6,
484 },
485 .i6w_sub_trees = NULL,
Neale Ranns32e1c012016-11-22 17:07:28 +0000486 };
Neale Ranns32e1c012016-11-22 17:07:28 +0000487
Neale Ranns89541992017-04-06 04:41:02 -0700488 BV(clib_bihash_foreach_key_value_pair)(
489 &ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash,
490 ip6_fib_walk_cb,
491 &ctx);
Neale Ranns32e1c012016-11-22 17:07:28 +0000492
Neale Ranns89541992017-04-06 04:41:02 -0700493 vec_free(ctx.i6w_sub_trees);
494}
495
496void
497ip6_fib_table_sub_tree_walk (u32 fib_index,
498 const fib_prefix_t *root,
499 fib_table_walk_fn_t fn,
500 void *arg)
501{
502 ip6_fib_walk_ctx_t ctx = {
503 .i6w_fib_index = fib_index,
504 .i6w_fn = fn,
505 .i6w_ctx = arg,
506 .i6w_root = *root,
507 };
508
509 BV(clib_bihash_foreach_key_value_pair)(
510 &ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash,
511 ip6_fib_walk_cb,
512 &ctx);
Neale Ranns32e1c012016-11-22 17:07:28 +0000513}
514
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100515typedef struct ip6_fib_show_ctx_t_ {
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100516 fib_node_index_t *entries;
517} ip6_fib_show_ctx_t;
518
Neale Ranns89541992017-04-06 04:41:02 -0700519static fib_table_walk_rc_t
Neale Ranns32e1c012016-11-22 17:07:28 +0000520ip6_fib_table_show_walk (fib_node_index_t fib_entry_index,
521 void *arg)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100522{
523 ip6_fib_show_ctx_t *ctx = arg;
524
Neale Ranns32e1c012016-11-22 17:07:28 +0000525 vec_add1(ctx->entries, fib_entry_index);
526
Neale Ranns89541992017-04-06 04:41:02 -0700527 return (FIB_TABLE_WALK_CONTINUE);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100528}
529
530static void
531ip6_fib_table_show_all (ip6_fib_t *fib,
532 vlib_main_t * vm)
533{
534 fib_node_index_t *fib_entry_index;
535 ip6_fib_show_ctx_t ctx = {
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100536 .entries = NULL,
537 };
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100538
Neale Ranns32e1c012016-11-22 17:07:28 +0000539 ip6_fib_table_walk(fib->index, ip6_fib_table_show_walk, &ctx);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100540 vec_sort_with_function(ctx.entries, fib_entry_cmp_for_sort);
541
542 vec_foreach(fib_entry_index, ctx.entries)
543 {
544 vlib_cli_output(vm, "%U",
545 format_fib_entry,
546 *fib_entry_index,
547 FIB_ENTRY_FORMAT_BRIEF);
548 }
549
550 vec_free(ctx.entries);
551}
552
553static void
554ip6_fib_table_show_one (ip6_fib_t *fib,
555 vlib_main_t * vm,
556 ip6_address_t *address,
Neale Ranns88fc83e2017-04-05 08:11:14 -0700557 u32 mask_len,
558 int detail)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100559{
560 vlib_cli_output(vm, "%U",
561 format_fib_entry,
562 ip6_fib_table_lookup(fib->index, address, mask_len),
Neale Ranns88fc83e2017-04-05 08:11:14 -0700563 (detail ?
564 FIB_ENTRY_FORMAT_DETAIL2:
565 FIB_ENTRY_FORMAT_DETAIL));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100566}
567
Neale Rannsc87aafa2017-11-29 00:59:31 -0800568u8 *
569format_ip6_fib_table_memory (u8 * s, va_list * args)
570{
Dave Barach97f5af02018-02-22 09:48:45 -0500571 uword bytes_inuse;
572
573 bytes_inuse =
Dave Barach9466c452018-08-24 17:21:14 -0400574 alloc_arena_next
575 (&(ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash))
576 - alloc_arena (&(ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash));
Dave Barach97f5af02018-02-22 09:48:45 -0500577
578 bytes_inuse +=
Dave Barach9466c452018-08-24 17:21:14 -0400579 alloc_arena_next(&(ip6_main.ip6_table[IP6_FIB_TABLE_FWDING].ip6_hash))
580 - alloc_arena(&(ip6_main.ip6_table[IP6_FIB_TABLE_FWDING].ip6_hash));
Dave Barach97f5af02018-02-22 09:48:45 -0500581
Neale Rannsc87aafa2017-11-29 00:59:31 -0800582 s = format(s, "%=30s %=6d %=8ld\n",
583 "IPv6 unicast",
584 pool_elts(ip6_main.fibs),
Dave Barach97f5af02018-02-22 09:48:45 -0500585 bytes_inuse);
Neale Rannsc87aafa2017-11-29 00:59:31 -0800586 return (s);
587}
588
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100589typedef struct {
590 u32 fib_index;
591 u64 count_by_prefix_length[129];
592} count_routes_in_fib_at_prefix_length_arg_t;
593
Neale Ranns88fc83e2017-04-05 08:11:14 -0700594static void
595count_routes_in_fib_at_prefix_length (BVT(clib_bihash_kv) * kvp,
596 void *arg)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100597{
598 count_routes_in_fib_at_prefix_length_arg_t * ap = arg;
599 int mask_width;
600
601 if ((kvp->key[2]>>32) != ap->fib_index)
602 return;
603
604 mask_width = kvp->key[2] & 0xFF;
605
606 ap->count_by_prefix_length[mask_width]++;
607}
608
609static clib_error_t *
610ip6_show_fib (vlib_main_t * vm,
611 unformat_input_t * input,
612 vlib_cli_command_t * cmd)
613{
614 count_routes_in_fib_at_prefix_length_arg_t _ca, *ca = &_ca;
615 ip6_main_t * im6 = &ip6_main;
616 fib_table_t *fib_table;
617 ip6_fib_t * fib;
618 int verbose, matching;
619 ip6_address_t matching_address;
620 u32 mask_len = 128;
621 int table_id = -1, fib_index = ~0;
Neale Ranns88fc83e2017-04-05 08:11:14 -0700622 int detail = 0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100623
624 verbose = 1;
625 matching = 0;
626
627 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
628 {
629 if (unformat (input, "brief") ||
630 unformat (input, "summary") ||
631 unformat (input, "sum"))
632 verbose = 0;
Neale Ranns88fc83e2017-04-05 08:11:14 -0700633
634 else if (unformat (input, "detail") ||
635 unformat (input, "det"))
636 detail = 1;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100637
638 else if (unformat (input, "%U/%d",
639 unformat_ip6_address, &matching_address, &mask_len))
640 matching = 1;
641
642 else if (unformat (input, "%U", unformat_ip6_address, &matching_address))
643 matching = 1;
644
645 else if (unformat (input, "table %d", &table_id))
646 ;
647 else if (unformat (input, "index %d", &fib_index))
648 ;
649 else
650 break;
651 }
652
653 pool_foreach (fib_table, im6->fibs,
654 ({
Neale Ranns15002542017-09-10 04:39:11 -0700655 fib_source_t source;
656 u8 *s = NULL;
657
Neale Rannsa3af3372017-03-28 03:49:52 -0700658 fib = pool_elt_at_index(im6->v6_fibs, fib_table->ft_index);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100659 if (table_id >= 0 && table_id != (int)fib->table_id)
660 continue;
661 if (fib_index != ~0 && fib_index != (int)fib->index)
662 continue;
Neale Ranns53da2212018-02-24 02:11:19 -0800663 if (fib_table->ft_flags & FIB_TABLE_FLAG_IP6_LL)
664 continue;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100665
Neale Ranns15002542017-09-10 04:39:11 -0700666 s = format(s, "%U, fib_index:%d, flow hash:[%U] locks:[",
667 format_fib_table_name, fib->index,
668 FIB_PROTOCOL_IP6,
669 fib->index,
670 format_ip_flow_hash_config,
671 fib_table->ft_flow_hash_config);
672 FOR_EACH_FIB_SOURCE(source)
673 {
674 if (0 != fib_table->ft_locks[source])
675 {
676 s = format(s, "%U:%d, ",
677 format_fib_source, source,
678 fib_table->ft_locks[source]);
679 }
680 }
681 s = format (s, "]");
Neale Ranns2297af02017-09-12 09:45:04 -0700682 vlib_cli_output (vm, "%v", s);
Neale Ranns15002542017-09-10 04:39:11 -0700683 vec_free(s);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100684
685 /* Show summary? */
686 if (! verbose)
687 {
688 BVT(clib_bihash) * h = &im6->ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash;
689 int len;
690
691 vlib_cli_output (vm, "%=20s%=16s", "Prefix length", "Count");
692
693 memset (ca, 0, sizeof(*ca));
694 ca->fib_index = fib->index;
695
696 BV(clib_bihash_foreach_key_value_pair)
697 (h, count_routes_in_fib_at_prefix_length, ca);
698
699 for (len = 128; len >= 0; len--)
700 {
701 if (ca->count_by_prefix_length[len])
702 vlib_cli_output (vm, "%=20d%=16lld",
703 len, ca->count_by_prefix_length[len]);
704 }
705 continue;
706 }
707
708 if (!matching)
709 {
710 ip6_fib_table_show_all(fib, vm);
711 }
712 else
713 {
Neale Ranns88fc83e2017-04-05 08:11:14 -0700714 ip6_fib_table_show_one(fib, vm, &matching_address, mask_len, detail);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100715 }
716 }));
717
718 return 0;
719}
720
721/*?
Billy McFall0683c9c2016-10-13 08:27:31 -0400722 * This command displays the IPv6 FIB Tables (VRF Tables) and the route
723 * entries for each table.
724 *
725 * @note This command will run for a long time when the FIB tables are
726 * comprised of millions of entries. For those senarios, consider displaying
727 * in summary mode.
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100728 *
729 * @cliexpar
Billy McFall0683c9c2016-10-13 08:27:31 -0400730 * @parblock
731 * Example of how to display all the IPv6 FIB tables:
732 * @cliexstart{show ip6 fib}
Billy McFallebb9a6a2016-10-17 11:35:32 -0400733 * ipv6-VRF:0, fib_index 0, flow hash: src dst sport dport proto
734 * @::/0
735 * unicast-ip6-chain
736 * [@0]: dpo-load-balance: [index:5 buckets:1 uRPF:5 to:[0:0]]
737 * [0] [@0]: dpo-drop ip6
738 * fe80::/10
739 * unicast-ip6-chain
740 * [@0]: dpo-load-balance: [index:10 buckets:1 uRPF:10 to:[0:0]]
741 * [0] [@2]: dpo-receive
742 * ff02::1/128
743 * unicast-ip6-chain
744 * [@0]: dpo-load-balance: [index:8 buckets:1 uRPF:8 to:[0:0]]
745 * [0] [@2]: dpo-receive
746 * ff02::2/128
747 * unicast-ip6-chain
748 * [@0]: dpo-load-balance: [index:7 buckets:1 uRPF:7 to:[0:0]]
749 * [0] [@2]: dpo-receive
750 * ff02::16/128
751 * unicast-ip6-chain
752 * [@0]: dpo-load-balance: [index:9 buckets:1 uRPF:9 to:[0:0]]
753 * [0] [@2]: dpo-receive
754 * ff02::1:ff00:0/104
755 * unicast-ip6-chain
756 * [@0]: dpo-load-balance: [index:6 buckets:1 uRPF:6 to:[0:0]]
757 * [0] [@2]: dpo-receive
758 * ipv6-VRF:8, fib_index 1, flow hash: src dst sport dport proto
759 * @::/0
760 * unicast-ip6-chain
761 * [@0]: dpo-load-balance: [index:21 buckets:1 uRPF:20 to:[0:0]]
762 * [0] [@0]: dpo-drop ip6
763 * @::a:1:1:0:4/126
764 * unicast-ip6-chain
765 * [@0]: dpo-load-balance: [index:27 buckets:1 uRPF:26 to:[0:0]]
766 * [0] [@4]: ipv6-glean: af_packet0
767 * @::a:1:1:0:7/128
768 * unicast-ip6-chain
769 * [@0]: dpo-load-balance: [index:28 buckets:1 uRPF:27 to:[0:0]]
770 * [0] [@2]: dpo-receive: @::a:1:1:0:7 on af_packet0
771 * fe80::/10
772 * unicast-ip6-chain
773 * [@0]: dpo-load-balance: [index:26 buckets:1 uRPF:25 to:[0:0]]
774 * [0] [@2]: dpo-receive
775 * fe80::fe:3eff:fe3e:9222/128
776 * unicast-ip6-chain
777 * [@0]: dpo-load-balance: [index:29 buckets:1 uRPF:28 to:[0:0]]
778 * [0] [@2]: dpo-receive: fe80::fe:3eff:fe3e:9222 on af_packet0
779 * ff02::1/128
780 * unicast-ip6-chain
781 * [@0]: dpo-load-balance: [index:24 buckets:1 uRPF:23 to:[0:0]]
782 * [0] [@2]: dpo-receive
783 * ff02::2/128
784 * unicast-ip6-chain
785 * [@0]: dpo-load-balance: [index:23 buckets:1 uRPF:22 to:[0:0]]
786 * [0] [@2]: dpo-receive
787 * ff02::16/128
788 * unicast-ip6-chain
789 * [@0]: dpo-load-balance: [index:25 buckets:1 uRPF:24 to:[0:0]]
790 * [0] [@2]: dpo-receive
791 * ff02::1:ff00:0/104
792 * unicast-ip6-chain
793 * [@0]: dpo-load-balance: [index:22 buckets:1 uRPF:21 to:[0:0]]
794 * [0] [@2]: dpo-receive
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100795 * @cliexend
Billy McFallebb9a6a2016-10-17 11:35:32 -0400796 *
Billy McFall0683c9c2016-10-13 08:27:31 -0400797 * Example of how to display a summary of all IPv6 FIB tables:
798 * @cliexstart{show ip6 fib summary}
Billy McFallebb9a6a2016-10-17 11:35:32 -0400799 * ipv6-VRF:0, fib_index 0, flow hash: src dst sport dport proto
Billy McFall0683c9c2016-10-13 08:27:31 -0400800 * Prefix length Count
801 * 128 3
802 * 104 1
Billy McFallebb9a6a2016-10-17 11:35:32 -0400803 * 10 1
804 * 0 1
805 * ipv6-VRF:8, fib_index 1, flow hash: src dst sport dport proto
Billy McFall0683c9c2016-10-13 08:27:31 -0400806 * Prefix length Count
807 * 128 5
808 * 126 1
809 * 104 1
Billy McFallebb9a6a2016-10-17 11:35:32 -0400810 * 10 1
811 * 0 1
Billy McFall0683c9c2016-10-13 08:27:31 -0400812 * @cliexend
813 * @endparblock
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100814 ?*/
Billy McFall0683c9c2016-10-13 08:27:31 -0400815/* *INDENT-OFF* */
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100816VLIB_CLI_COMMAND (ip6_show_fib_command, static) = {
817 .path = "show ip6 fib",
Neale Ranns88fc83e2017-04-05 08:11:14 -0700818 .short_help = "show ip6 fib [summary] [table <table-id>] [index <fib-id>] [<ip6-addr>[/<width>]] [detail]",
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100819 .function = ip6_show_fib,
820};
Billy McFall0683c9c2016-10-13 08:27:31 -0400821/* *INDENT-ON* */