fib: Table Replace
[vpp.git] / src / vnet / fib / ip6_fib.c
1 /*
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>
18 #include <vnet/dpo/ip6_ll_dpo.h>
19
20 #include <vppinfra/bihash_24_8.h>
21 #include <vppinfra/bihash_template.c>
22
23 static void
24 vnet_ip6_fib_init (u32 fib_index)
25 {
26     fib_prefix_t pfx = {
27         .fp_proto = FIB_PROTOCOL_IP6,
28         .fp_len = 0,
29         .fp_addr = {
30             .ip6 = {
31                 { 0, 0, },
32             },
33         }
34     };
35
36     /*
37      * Add the default route.
38      */
39     fib_table_entry_special_add(fib_index,
40                                 &pfx,
41                                 FIB_SOURCE_DEFAULT_ROUTE,
42                                 FIB_ENTRY_FLAG_DROP);
43
44     /*
45      * all link local via the link local lookup DPO
46      */
47     pfx.fp_addr.ip6.as_u64[0] = clib_host_to_net_u64 (0xFE80000000000000ULL);
48     pfx.fp_addr.ip6.as_u64[1] = 0;
49     pfx.fp_len = 10;
50     fib_table_entry_special_dpo_add(fib_index,
51                                     &pfx,
52                                     FIB_SOURCE_SPECIAL,
53                                     FIB_ENTRY_FLAG_NONE,
54                                     ip6_ll_dpo_get());
55 }
56
57 static u32
58 create_fib_with_table_id (u32 table_id,
59                           fib_source_t src,
60                           fib_table_flags_t flags,
61                           u8 *desc)
62 {
63     fib_table_t *fib_table;
64     ip6_fib_t *v6_fib;
65
66     pool_get(ip6_main.fibs, fib_table);
67     pool_get_aligned(ip6_main.v6_fibs, v6_fib, CLIB_CACHE_LINE_BYTES);
68
69     clib_memset(fib_table, 0, sizeof(*fib_table));
70     clib_memset(v6_fib, 0, sizeof(*v6_fib));
71
72     ASSERT((fib_table - ip6_main.fibs) ==
73            (v6_fib - ip6_main.v6_fibs));
74     
75     fib_table->ft_proto = FIB_PROTOCOL_IP6;
76     fib_table->ft_index =
77             v6_fib->index =
78                 (fib_table - ip6_main.fibs);
79
80     hash_set(ip6_main.fib_index_by_table_id, table_id, fib_table->ft_index);
81
82     fib_table->ft_table_id =
83         v6_fib->table_id =
84             table_id;
85     fib_table->ft_flow_hash_config = IP_FLOW_HASH_DEFAULT;
86     fib_table->ft_flags = flags;
87     fib_table->ft_desc = desc;
88
89     vnet_ip6_fib_init(fib_table->ft_index);
90     fib_table_lock(fib_table->ft_index, FIB_PROTOCOL_IP6, src);
91
92     return (fib_table->ft_index);
93 }
94
95 u32
96 ip6_fib_table_find_or_create_and_lock (u32 table_id,
97                                        fib_source_t src)
98 {
99     uword * p;
100
101     p = hash_get (ip6_main.fib_index_by_table_id, table_id);
102     if (NULL == p)
103         return create_fib_with_table_id(table_id, src,
104                                         FIB_TABLE_FLAG_NONE,
105                                         NULL);
106
107     fib_table_lock(p[0], FIB_PROTOCOL_IP6, src);
108
109     return (p[0]);
110 }
111
112 u32
113 ip6_fib_table_create_and_lock (fib_source_t src,
114                                fib_table_flags_t flags,
115                                u8 *desc)
116 {
117     return (create_fib_with_table_id(~0, src, flags, desc));
118 }
119
120 void
121 ip6_fib_table_destroy (u32 fib_index)
122 {
123     /*
124      * all link local first ...
125      */
126     fib_prefix_t pfx = {
127         .fp_proto = FIB_PROTOCOL_IP6,
128         .fp_len = 10,
129         .fp_addr = {
130             .ip6 = {
131                 .as_u8 = {
132                     [0] = 0xFE,
133                     [1] = 0x80,
134                 },
135             },
136         }
137     };
138     fib_table_entry_delete(fib_index,
139                            &pfx,
140                            FIB_SOURCE_SPECIAL);
141
142     /*
143      * ... then the default route.
144      */
145     pfx.fp_addr.ip6.as_u64[0] = 0;
146     pfx.fp_len = 00;
147     fib_table_entry_special_remove(fib_index,
148                                    &pfx,
149                                    FIB_SOURCE_DEFAULT_ROUTE);
150
151     fib_table_t *fib_table = fib_table_get(fib_index, FIB_PROTOCOL_IP6);
152     fib_source_t source;
153
154      /*
155      * validate no more routes.
156      */
157     ASSERT(0 == fib_table->ft_total_route_counts);
158     FOR_EACH_FIB_SOURCE(source)
159     {
160         ASSERT(0 == fib_table->ft_src_route_counts[source]);
161     }
162
163     if (~0 != fib_table->ft_table_id)
164     {
165         hash_unset (ip6_main.fib_index_by_table_id, fib_table->ft_table_id);
166     }
167     pool_put_index(ip6_main.v6_fibs, fib_table->ft_index);
168     pool_put(ip6_main.fibs, fib_table);
169 }
170
171 fib_node_index_t
172 ip6_fib_table_lookup (u32 fib_index,
173                       const ip6_address_t *addr,
174                       u32 len)
175 {
176     ip6_fib_table_instance_t *table;
177     clib_bihash_kv_24_8_t kv, value;
178     int i, n_p, rv;
179     u64 fib;
180
181     table = &ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING];
182     n_p = vec_len (table->prefix_lengths_in_search_order);
183
184     kv.key[0] = addr->as_u64[0];
185     kv.key[1] = addr->as_u64[1];
186     fib = ((u64)((fib_index))<<32);
187
188     /*
189      * start search from a mask length same length or shorter.
190      * we don't want matches longer than the mask passed
191      */
192     i = 0;
193     while (i < n_p && table->prefix_lengths_in_search_order[i] > len)
194     {
195         i++;
196     }
197
198     for (; i < n_p; i++)
199     {
200         int dst_address_length = table->prefix_lengths_in_search_order[i];
201         ip6_address_t * mask = &ip6_main.fib_masks[dst_address_length];
202       
203         ASSERT(dst_address_length >= 0 && dst_address_length <= 128);
204         //As lengths are decreasing, masks are increasingly specific.
205         kv.key[0] &= mask->as_u64[0];
206         kv.key[1] &= mask->as_u64[1];
207         kv.key[2] = fib | dst_address_length;
208       
209         rv = clib_bihash_search_inline_2_24_8(&table->ip6_hash, &kv, &value);
210         if (rv == 0)
211             return value.value;
212     }
213
214     return (FIB_NODE_INDEX_INVALID);
215 }
216
217 fib_node_index_t
218 ip6_fib_table_lookup_exact_match (u32 fib_index,
219                                   const ip6_address_t *addr,
220                                   u32 len)
221 {
222     ip6_fib_table_instance_t *table;
223     clib_bihash_kv_24_8_t kv, value;
224     ip6_address_t *mask;
225     u64 fib;
226     int rv;
227
228     table = &ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING];
229     mask = &ip6_main.fib_masks[len];
230     fib = ((u64)((fib_index))<<32);
231
232     kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
233     kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
234     kv.key[2] = fib | len;
235       
236     rv = clib_bihash_search_inline_2_24_8(&table->ip6_hash, &kv, &value);
237     if (rv == 0)
238         return value.value;
239
240     return (FIB_NODE_INDEX_INVALID);
241 }
242
243 static void
244 compute_prefix_lengths_in_search_order (ip6_fib_table_instance_t *table)
245 {
246     int i;
247     vec_reset_length (table->prefix_lengths_in_search_order);
248     /* Note: bitmap reversed so this is in fact a longest prefix match */
249     clib_bitmap_foreach (i, table->non_empty_dst_address_length_bitmap,
250     ({
251         int dst_address_length = 128 - i;
252         vec_add1(table->prefix_lengths_in_search_order, dst_address_length);
253     }));
254 }
255
256 void
257 ip6_fib_table_entry_remove (u32 fib_index,
258                             const ip6_address_t *addr,
259                             u32 len)
260 {
261     ip6_fib_table_instance_t *table;
262     clib_bihash_kv_24_8_t kv;
263     ip6_address_t *mask;
264     u64 fib;
265
266     table = &ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING];
267     mask = &ip6_main.fib_masks[len];
268     fib = ((u64)((fib_index))<<32);
269
270     kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
271     kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
272     kv.key[2] = fib | len;
273
274     clib_bihash_add_del_24_8(&table->ip6_hash, &kv, 0);
275
276     /* refcount accounting */
277     ASSERT (table->dst_address_length_refcounts[len] > 0);
278     if (--table->dst_address_length_refcounts[len] == 0)
279     {
280         table->non_empty_dst_address_length_bitmap =
281             clib_bitmap_set (table->non_empty_dst_address_length_bitmap, 
282                              128 - len, 0);
283         compute_prefix_lengths_in_search_order (table);
284     }
285 }
286
287 void
288 ip6_fib_table_entry_insert (u32 fib_index,
289                             const ip6_address_t *addr,
290                             u32 len,
291                             fib_node_index_t fib_entry_index)
292 {
293     ip6_fib_table_instance_t *table;
294     clib_bihash_kv_24_8_t kv;
295     ip6_address_t *mask;
296     u64 fib;
297
298     table = &ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING];
299     mask = &ip6_main.fib_masks[len];
300     fib = ((u64)((fib_index))<<32);
301
302     kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
303     kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
304     kv.key[2] = fib | len;
305     kv.value = fib_entry_index;
306
307     clib_bihash_add_del_24_8(&table->ip6_hash, &kv, 1);
308
309     table->dst_address_length_refcounts[len]++;
310
311     table->non_empty_dst_address_length_bitmap =
312         clib_bitmap_set (table->non_empty_dst_address_length_bitmap, 
313                          128 - len, 1);
314     compute_prefix_lengths_in_search_order (table);
315 }
316
317 u32 ip6_fib_table_fwding_lookup_with_if_index (ip6_main_t * im,
318                                                u32 sw_if_index,
319                                                const ip6_address_t * dst)
320 {
321     u32 fib_index = vec_elt (im->fib_index_by_sw_if_index, sw_if_index);
322     return ip6_fib_table_fwding_lookup(fib_index, dst);
323 }
324
325 u32
326 ip6_fib_table_get_index_for_sw_if_index (u32 sw_if_index)
327 {
328     if (sw_if_index >= vec_len(ip6_main.fib_index_by_sw_if_index))
329     {
330         /*
331          * This is the case for interfaces that are not yet mapped to
332          * a IP table
333          */
334         return (~0);
335     }
336     return (ip6_main.fib_index_by_sw_if_index[sw_if_index]);
337 }
338
339 void
340 ip6_fib_table_fwding_dpo_update (u32 fib_index,
341                                  const ip6_address_t *addr,
342                                  u32 len,
343                                  const dpo_id_t *dpo)
344 {
345     ip6_fib_table_instance_t *table;
346     clib_bihash_kv_24_8_t kv;
347     ip6_address_t *mask;
348     u64 fib;
349
350     table = &ip6_main.ip6_table[IP6_FIB_TABLE_FWDING];
351     mask = &ip6_main.fib_masks[len];
352     fib = ((u64)((fib_index))<<32);
353
354     kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
355     kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
356     kv.key[2] = fib | len;
357     kv.value = dpo->dpoi_index;
358
359     clib_bihash_add_del_24_8(&table->ip6_hash, &kv, 1);
360
361     table->dst_address_length_refcounts[len]++;
362
363     table->non_empty_dst_address_length_bitmap =
364         clib_bitmap_set (table->non_empty_dst_address_length_bitmap, 
365                          128 - len, 1);
366     compute_prefix_lengths_in_search_order (table);
367 }
368
369 void
370 ip6_fib_table_fwding_dpo_remove (u32 fib_index,
371                                  const ip6_address_t *addr,
372                                  u32 len,
373                                  const dpo_id_t *dpo)
374 {
375     ip6_fib_table_instance_t *table;
376     clib_bihash_kv_24_8_t kv;
377     ip6_address_t *mask;
378     u64 fib;
379
380     table = &ip6_main.ip6_table[IP6_FIB_TABLE_FWDING];
381     mask = &ip6_main.fib_masks[len];
382     fib = ((u64)((fib_index))<<32);
383
384     kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
385     kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
386     kv.key[2] = fib | len;
387     kv.value = dpo->dpoi_index;
388
389     clib_bihash_add_del_24_8(&table->ip6_hash, &kv, 0);
390
391     /* refcount accounting */
392     ASSERT (table->dst_address_length_refcounts[len] > 0);
393     if (--table->dst_address_length_refcounts[len] == 0)
394     {
395         table->non_empty_dst_address_length_bitmap =
396             clib_bitmap_set (table->non_empty_dst_address_length_bitmap,
397                              128 - len, 0);
398         compute_prefix_lengths_in_search_order (table);
399     }
400 }
401
402 /**
403  * @brief Context when walking the IPv6 table. Since all VRFs are in the
404  * same hash table, we need to filter only those we need as we walk
405  */
406 typedef struct ip6_fib_walk_ctx_t_
407 {
408     u32 i6w_fib_index;
409     fib_table_walk_fn_t i6w_fn;
410     void *i6w_ctx;
411     fib_prefix_t i6w_root;
412     fib_prefix_t *i6w_sub_trees;
413 } ip6_fib_walk_ctx_t;
414
415 static int
416 ip6_fib_walk_cb (clib_bihash_kv_24_8_t * kvp,
417                  void *arg)
418 {
419     ip6_fib_walk_ctx_t *ctx = arg;
420     ip6_address_t key;
421
422     if ((kvp->key[2] >> 32) == ctx->i6w_fib_index)
423     {
424         key.as_u64[0] = kvp->key[0];
425         key.as_u64[1] = kvp->key[1];
426
427         if (ip6_destination_matches_route(&ip6_main,
428                                           &key,
429                                           &ctx->i6w_root.fp_addr.ip6,
430                                           ctx->i6w_root.fp_len))
431         {
432             const fib_prefix_t *sub_tree;
433             int skip = 0;
434
435             /*
436              * exclude sub-trees the walk does not want to explore
437              */
438             vec_foreach(sub_tree, ctx->i6w_sub_trees)
439             {
440                 if (ip6_destination_matches_route(&ip6_main,
441                                                   &key,
442                                                   &sub_tree->fp_addr.ip6,
443                                                   sub_tree->fp_len))
444                 {
445                     skip = 1;
446                     break;
447                 }
448             }
449
450             if (!skip)
451             {
452                 switch (ctx->i6w_fn(kvp->value, ctx->i6w_ctx))
453                 {
454                 case FIB_TABLE_WALK_CONTINUE:
455                     break;
456                 case FIB_TABLE_WALK_SUB_TREE_STOP: {
457                     fib_prefix_t pfx = {
458                         .fp_proto = FIB_PROTOCOL_IP6,
459                         .fp_len = kvp->key[2] & 0xffffffff,
460                         .fp_addr.ip6 = key,
461                     };
462                     vec_add1(ctx->i6w_sub_trees, pfx);
463                     break;
464                 }
465                 case FIB_TABLE_WALK_STOP:
466                     goto done;
467                 }
468             }
469         }
470     }
471 done:
472
473     return (1);
474 }
475
476 void
477 ip6_fib_table_walk (u32 fib_index,
478                     fib_table_walk_fn_t fn,
479                     void *arg)
480 {
481     ip6_fib_walk_ctx_t ctx = {
482         .i6w_fib_index = fib_index,
483         .i6w_fn = fn,
484         .i6w_ctx = arg,
485         .i6w_root = {
486             .fp_proto = FIB_PROTOCOL_IP6,
487         },
488         .i6w_sub_trees = NULL,
489     };
490
491     clib_bihash_foreach_key_value_pair_24_8(
492         &ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash,
493         ip6_fib_walk_cb,
494         &ctx);
495
496     vec_free(ctx.i6w_sub_trees);
497 }
498
499 void
500 ip6_fib_table_sub_tree_walk (u32 fib_index,
501                              const fib_prefix_t *root,
502                              fib_table_walk_fn_t fn,
503                              void *arg)
504 {
505     ip6_fib_walk_ctx_t ctx = {
506         .i6w_fib_index = fib_index,
507         .i6w_fn = fn,
508         .i6w_ctx = arg,
509         .i6w_root = *root,
510     };
511
512     clib_bihash_foreach_key_value_pair_24_8(
513         &ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash,
514         ip6_fib_walk_cb,
515         &ctx);
516 }
517
518 typedef struct ip6_fib_show_ctx_t_ {
519     fib_node_index_t *entries;
520 } ip6_fib_show_ctx_t;
521
522 static fib_table_walk_rc_t
523 ip6_fib_table_show_walk (fib_node_index_t fib_entry_index,
524                          void *arg)
525 {
526     ip6_fib_show_ctx_t *ctx = arg;
527
528     vec_add1(ctx->entries, fib_entry_index);
529
530     return (FIB_TABLE_WALK_CONTINUE);
531 }
532
533 static void
534 ip6_fib_table_show_all (ip6_fib_t *fib,
535                         vlib_main_t * vm)
536 {
537     fib_node_index_t *fib_entry_index;
538     ip6_fib_show_ctx_t ctx = {
539         .entries = NULL,
540     };
541
542     ip6_fib_table_walk(fib->index, ip6_fib_table_show_walk, &ctx);
543     vec_sort_with_function(ctx.entries, fib_entry_cmp_for_sort);
544
545     vec_foreach(fib_entry_index, ctx.entries)
546     {
547         vlib_cli_output(vm, "%U",
548                         format_fib_entry,
549                         *fib_entry_index,
550                         FIB_ENTRY_FORMAT_BRIEF);
551     }
552
553     vec_free(ctx.entries);
554 }
555
556 static void
557 ip6_fib_table_show_one (ip6_fib_t *fib,
558                         vlib_main_t * vm,
559                         ip6_address_t *address,
560                         u32 mask_len,
561                         int detail)
562 {
563     vlib_cli_output(vm, "%U",
564                     format_fib_entry,
565                     ip6_fib_table_lookup(fib->index, address, mask_len),
566                     (detail ?
567                      FIB_ENTRY_FORMAT_DETAIL2:
568                      FIB_ENTRY_FORMAT_DETAIL));
569 }
570
571 u8 *
572 format_ip6_fib_table_memory (u8 * s, va_list * args)
573 {
574     uword bytes_inuse;
575
576     bytes_inuse = (alloc_arena_next(&(ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash)) +
577                    alloc_arena_next(&(ip6_main.ip6_table[IP6_FIB_TABLE_FWDING].ip6_hash)));
578
579     s = format(s, "%=30s %=6d %=12ld\n",
580                "IPv6 unicast",
581                pool_elts(ip6_main.fibs),
582                bytes_inuse);
583     return (s);
584 }
585
586 typedef struct {
587   u32 fib_index;
588   u64 count_by_prefix_length[129];
589 } count_routes_in_fib_at_prefix_length_arg_t;
590
591 static void
592 count_routes_in_fib_at_prefix_length (clib_bihash_kv_24_8_t * kvp,
593                                       void *arg)
594 {
595   count_routes_in_fib_at_prefix_length_arg_t * ap = arg;
596   int mask_width;
597
598   if ((kvp->key[2]>>32) != ap->fib_index)
599     return;
600
601   mask_width = kvp->key[2] & 0xFF;
602
603   ap->count_by_prefix_length[mask_width]++;
604 }
605
606 static clib_error_t *
607 ip6_show_fib (vlib_main_t * vm,
608               unformat_input_t * input,
609               vlib_cli_command_t * cmd)
610 {
611     count_routes_in_fib_at_prefix_length_arg_t _ca, *ca = &_ca;
612     ip6_main_t * im6 = &ip6_main;
613     fib_table_t *fib_table;
614     ip6_fib_t * fib;
615     int verbose, matching;
616     ip6_address_t matching_address;
617     u32 mask_len  = 128;
618     int table_id = -1, fib_index = ~0;
619     int detail = 0;
620     int hash = 0;
621
622     verbose = 1;
623     matching = 0;
624
625     while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
626     {
627         if (unformat (input, "brief")   ||
628             unformat (input, "summary") ||
629             unformat (input, "sum"))
630             verbose = 0;
631  
632         else if (unformat (input, "detail")   ||
633                  unformat (input, "det"))
634             detail = 1;
635
636         else if (unformat (input, "hash") ||
637                  unformat (input, "mem") ||
638                  unformat (input, "memory"))
639             hash = 1;
640
641         else if (unformat (input, "%U/%d",
642                            unformat_ip6_address, &matching_address, &mask_len))
643             matching = 1;
644
645         else if (unformat (input, "%U", unformat_ip6_address, &matching_address))
646             matching = 1;
647
648         else if (unformat (input, "table %d", &table_id))
649             ;
650         else if (unformat (input, "index %d", &fib_index))
651             ;
652         else
653             break;
654     }
655
656     if (hash)
657     {
658         vlib_cli_output (vm, "IPv6 Non-Forwarding Hash Table:\n%U\n",
659                          BV (format_bihash),
660                          &im6->ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash,
661                          detail);
662         vlib_cli_output (vm, "IPv6 Forwarding Hash Table:\n%U\n",
663                          BV (format_bihash),
664                          &im6->ip6_table[IP6_FIB_TABLE_FWDING].ip6_hash,
665                          detail);
666         return (NULL);
667     }
668
669     pool_foreach (fib_table, im6->fibs,
670     ({
671         fib_source_t source;
672         u8 *s = NULL;
673
674         fib = pool_elt_at_index(im6->v6_fibs, fib_table->ft_index);
675         if (table_id >= 0 && table_id != (int)fib->table_id)
676             continue;
677         if (fib_index != ~0 && fib_index != (int)fib->index)
678             continue;
679         if (fib_table->ft_flags & FIB_TABLE_FLAG_IP6_LL)
680             continue;
681
682         s = format(s, "%U, fib_index:%d, flow hash:[%U] epoch:%d flags:%U locks:[",
683                    format_fib_table_name, fib->index,
684                    FIB_PROTOCOL_IP6,
685                    fib->index,
686                    format_ip_flow_hash_config,
687                    fib_table->ft_flow_hash_config,
688                    fib_table->ft_epoch,
689                    format_fib_table_flags, fib_table->ft_flags);
690
691         FOR_EACH_FIB_SOURCE(source)
692         {
693             if (0 != fib_table->ft_locks[source])
694             {
695                 s = format(s, "%U:%d, ",
696                            format_fib_source, source,
697                            fib_table->ft_locks[source]);
698             }
699         }
700         s = format (s, "]");
701         vlib_cli_output (vm, "%v", s);
702         vec_free(s);
703
704         /* Show summary? */
705         if (! verbose)
706         {
707             clib_bihash_24_8_t * h = &im6->ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash;
708             int len;
709
710             vlib_cli_output (vm, "%=20s%=16s", "Prefix length", "Count");
711
712             clib_memset (ca, 0, sizeof(*ca));
713             ca->fib_index = fib->index;
714
715             clib_bihash_foreach_key_value_pair_24_8
716                 (h, count_routes_in_fib_at_prefix_length, ca);
717
718             for (len = 128; len >= 0; len--)
719             {
720                 if (ca->count_by_prefix_length[len])
721                     vlib_cli_output (vm, "%=20d%=16lld", 
722                                      len, ca->count_by_prefix_length[len]);
723             }
724             continue;
725         }
726
727         if (!matching)
728         {
729             ip6_fib_table_show_all(fib, vm);
730         }
731         else
732         {
733             ip6_fib_table_show_one(fib, vm, &matching_address, mask_len, detail);
734         }
735     }));
736
737     return 0;
738 }
739
740 /*?
741  * This command displays the IPv6 FIB Tables (VRF Tables) and the route
742  * entries for each table.
743  *
744  * @note This command will run for a long time when the FIB tables are
745  * comprised of millions of entries. For those senarios, consider displaying
746  * in summary mode.
747  *
748  * @cliexpar
749  * @parblock
750  * Example of how to display all the IPv6 FIB tables:
751  * @cliexstart{show ip6 fib}
752  * ipv6-VRF:0, fib_index 0, flow hash: src dst sport dport proto
753  * @::/0
754  *   unicast-ip6-chain
755  *   [@0]: dpo-load-balance: [index:5 buckets:1 uRPF:5 to:[0:0]]
756  *     [0] [@0]: dpo-drop ip6
757  * fe80::/10
758  *   unicast-ip6-chain
759  *   [@0]: dpo-load-balance: [index:10 buckets:1 uRPF:10 to:[0:0]]
760  *     [0] [@2]: dpo-receive
761  * ff02::1/128
762  *   unicast-ip6-chain
763  *   [@0]: dpo-load-balance: [index:8 buckets:1 uRPF:8 to:[0:0]]
764  *     [0] [@2]: dpo-receive
765  * ff02::2/128
766  *   unicast-ip6-chain
767  *   [@0]: dpo-load-balance: [index:7 buckets:1 uRPF:7 to:[0:0]]
768  *     [0] [@2]: dpo-receive
769  * ff02::16/128
770  *   unicast-ip6-chain
771  *   [@0]: dpo-load-balance: [index:9 buckets:1 uRPF:9 to:[0:0]]
772  *     [0] [@2]: dpo-receive
773  * ff02::1:ff00:0/104
774  *   unicast-ip6-chain
775  *   [@0]: dpo-load-balance: [index:6 buckets:1 uRPF:6 to:[0:0]]
776  *     [0] [@2]: dpo-receive
777  * ipv6-VRF:8, fib_index 1, flow hash: src dst sport dport proto
778  * @::/0
779  *   unicast-ip6-chain
780  *   [@0]: dpo-load-balance: [index:21 buckets:1 uRPF:20 to:[0:0]]
781  *     [0] [@0]: dpo-drop ip6
782  * @::a:1:1:0:4/126
783  *   unicast-ip6-chain
784  *   [@0]: dpo-load-balance: [index:27 buckets:1 uRPF:26 to:[0:0]]
785  *     [0] [@4]: ipv6-glean: af_packet0
786  * @::a:1:1:0:7/128
787  *   unicast-ip6-chain
788  *   [@0]: dpo-load-balance: [index:28 buckets:1 uRPF:27 to:[0:0]]
789  *     [0] [@2]: dpo-receive: @::a:1:1:0:7 on af_packet0
790  * fe80::/10
791  *   unicast-ip6-chain
792  *   [@0]: dpo-load-balance: [index:26 buckets:1 uRPF:25 to:[0:0]]
793  *     [0] [@2]: dpo-receive
794  * fe80::fe:3eff:fe3e:9222/128
795  *   unicast-ip6-chain
796  *   [@0]: dpo-load-balance: [index:29 buckets:1 uRPF:28 to:[0:0]]
797  *     [0] [@2]: dpo-receive: fe80::fe:3eff:fe3e:9222 on af_packet0
798  * ff02::1/128
799  *   unicast-ip6-chain
800  *   [@0]: dpo-load-balance: [index:24 buckets:1 uRPF:23 to:[0:0]]
801  *     [0] [@2]: dpo-receive
802  * ff02::2/128
803  *   unicast-ip6-chain
804  *   [@0]: dpo-load-balance: [index:23 buckets:1 uRPF:22 to:[0:0]]
805  *     [0] [@2]: dpo-receive
806  * ff02::16/128
807  *   unicast-ip6-chain
808  *   [@0]: dpo-load-balance: [index:25 buckets:1 uRPF:24 to:[0:0]]
809  *     [0] [@2]: dpo-receive
810  * ff02::1:ff00:0/104
811  *   unicast-ip6-chain
812  *   [@0]: dpo-load-balance: [index:22 buckets:1 uRPF:21 to:[0:0]]
813  *     [0] [@2]: dpo-receive
814  * @cliexend
815  *
816  * Example of how to display a summary of all IPv6 FIB tables:
817  * @cliexstart{show ip6 fib summary}
818  * ipv6-VRF:0, fib_index 0, flow hash: src dst sport dport proto
819  *     Prefix length         Count
820  *          128                3
821  *          104                1
822  *          10                 1
823  *           0                 1
824  * ipv6-VRF:8, fib_index 1, flow hash: src dst sport dport proto
825  *     Prefix length         Count
826  *          128                5
827  *          126                1
828  *          104                1
829  *          10                 1
830  *           0                 1
831  * @cliexend
832  * @endparblock
833  ?*/
834 /* *INDENT-OFF* */
835 VLIB_CLI_COMMAND (ip6_show_fib_command, static) = {
836     .path = "show ip6 fib",
837     .short_help = "show ip6 fib [summary] [table <table-id>] [index <fib-id>] [<ip6-addr>[/<width>]] [detail]",
838     .function = ip6_show_fib,
839 };
840 /* *INDENT-ON* */