blob: 861edccd4a99f8b529b4cf37be4b45001f166415 [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
Neale Rannsae809832018-11-23 09:00:27 -080020#include <vppinfra/bihash_24_8.h>
21#include <vppinfra/bihash_template.c>
22
Neale Ranns5a59b2b2020-10-19 14:47:20 +000023ip6_fib_table_instance_t ip6_fib_table[IP6_FIB_NUM_TABLES];
24
25/* ip6 lookup table config parameters */
26u32 ip6_fib_table_nbuckets;
27uword ip6_fib_table_size;
28
Neale Ranns0bfe5d82016-08-25 15:29:12 +010029static void
30vnet_ip6_fib_init (u32 fib_index)
31{
32 fib_prefix_t pfx = {
33 .fp_proto = FIB_PROTOCOL_IP6,
34 .fp_len = 0,
35 .fp_addr = {
36 .ip6 = {
37 { 0, 0, },
38 },
39 }
40 };
41
42 /*
43 * Add the default route.
44 */
45 fib_table_entry_special_add(fib_index,
46 &pfx,
47 FIB_SOURCE_DEFAULT_ROUTE,
Neale Rannsa0558302017-04-13 00:44:52 -070048 FIB_ENTRY_FLAG_DROP);
Neale Ranns0bfe5d82016-08-25 15:29:12 +010049
50 /*
Neale Ranns53da2212018-02-24 02:11:19 -080051 * all link local via the link local lookup DPO
Neale Ranns0bfe5d82016-08-25 15:29:12 +010052 */
53 pfx.fp_addr.ip6.as_u64[0] = clib_host_to_net_u64 (0xFE80000000000000ULL);
54 pfx.fp_addr.ip6.as_u64[1] = 0;
55 pfx.fp_len = 10;
Neale Ranns53da2212018-02-24 02:11:19 -080056 fib_table_entry_special_dpo_add(fib_index,
57 &pfx,
58 FIB_SOURCE_SPECIAL,
59 FIB_ENTRY_FLAG_NONE,
60 ip6_ll_dpo_get());
Neale Ranns0bfe5d82016-08-25 15:29:12 +010061}
62
63static u32
Neale Ranns15002542017-09-10 04:39:11 -070064create_fib_with_table_id (u32 table_id,
Neale Ranns53da2212018-02-24 02:11:19 -080065 fib_source_t src,
66 fib_table_flags_t flags,
67 u8 *desc)
Neale Ranns0bfe5d82016-08-25 15:29:12 +010068{
69 fib_table_t *fib_table;
Neale Rannsa3af3372017-03-28 03:49:52 -070070 ip6_fib_t *v6_fib;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010071
Dave Baracheb987d32018-05-03 08:26:39 -040072 pool_get(ip6_main.fibs, fib_table);
Neale Rannsa3af3372017-03-28 03:49:52 -070073 pool_get_aligned(ip6_main.v6_fibs, v6_fib, CLIB_CACHE_LINE_BYTES);
Neale Ranns0bfe5d82016-08-25 15:29:12 +010074
Dave Barachb7b92992018-10-17 10:38:51 -040075 clib_memset(fib_table, 0, sizeof(*fib_table));
76 clib_memset(v6_fib, 0, sizeof(*v6_fib));
Neale Rannsa3af3372017-03-28 03:49:52 -070077
78 ASSERT((fib_table - ip6_main.fibs) ==
79 (v6_fib - ip6_main.v6_fibs));
80
Neale Ranns0bfe5d82016-08-25 15:29:12 +010081 fib_table->ft_proto = FIB_PROTOCOL_IP6;
82 fib_table->ft_index =
Neale Rannsa3af3372017-03-28 03:49:52 -070083 v6_fib->index =
84 (fib_table - ip6_main.fibs);
Neale Ranns0bfe5d82016-08-25 15:29:12 +010085
86 hash_set(ip6_main.fib_index_by_table_id, table_id, fib_table->ft_index);
87
88 fib_table->ft_table_id =
Neale Rannsa3af3372017-03-28 03:49:52 -070089 v6_fib->table_id =
Neale Ranns0bfe5d82016-08-25 15:29:12 +010090 table_id;
Neale Ranns227038a2017-04-21 01:07:59 -070091 fib_table->ft_flow_hash_config = IP_FLOW_HASH_DEFAULT;
Neale Ranns53da2212018-02-24 02:11:19 -080092 fib_table->ft_flags = flags;
93 fib_table->ft_desc = desc;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010094
95 vnet_ip6_fib_init(fib_table->ft_index);
Neale Ranns15002542017-09-10 04:39:11 -070096 fib_table_lock(fib_table->ft_index, FIB_PROTOCOL_IP6, src);
Neale Ranns0bfe5d82016-08-25 15:29:12 +010097
98 return (fib_table->ft_index);
99}
100
101u32
Neale Ranns15002542017-09-10 04:39:11 -0700102ip6_fib_table_find_or_create_and_lock (u32 table_id,
103 fib_source_t src)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100104{
105 uword * p;
106
107 p = hash_get (ip6_main.fib_index_by_table_id, table_id);
108 if (NULL == p)
Neale Ranns53da2212018-02-24 02:11:19 -0800109 return create_fib_with_table_id(table_id, src,
110 FIB_TABLE_FLAG_NONE,
111 NULL);
112
Neale Ranns15002542017-09-10 04:39:11 -0700113 fib_table_lock(p[0], FIB_PROTOCOL_IP6, src);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100114
115 return (p[0]);
116}
117
118u32
Neale Ranns53da2212018-02-24 02:11:19 -0800119ip6_fib_table_create_and_lock (fib_source_t src,
120 fib_table_flags_t flags,
121 u8 *desc)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100122{
Neale Ranns53da2212018-02-24 02:11:19 -0800123 return (create_fib_with_table_id(~0, src, flags, desc));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100124}
125
126void
127ip6_fib_table_destroy (u32 fib_index)
128{
Neale Ranns53da2212018-02-24 02:11:19 -0800129 /*
130 * all link local first ...
131 */
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100132 fib_prefix_t pfx = {
133 .fp_proto = FIB_PROTOCOL_IP6,
Neale Ranns53da2212018-02-24 02:11:19 -0800134 .fp_len = 10,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100135 .fp_addr = {
136 .ip6 = {
Neale Ranns53da2212018-02-24 02:11:19 -0800137 .as_u8 = {
138 [0] = 0xFE,
139 [1] = 0x80,
140 },
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100141 },
142 }
143 };
Neale Ranns53da2212018-02-24 02:11:19 -0800144 fib_table_entry_delete(fib_index,
145 &pfx,
146 FIB_SOURCE_SPECIAL);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100147
148 /*
Neale Ranns53da2212018-02-24 02:11:19 -0800149 * ... then the default route.
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100150 */
Neale Ranns53da2212018-02-24 02:11:19 -0800151 pfx.fp_addr.ip6.as_u64[0] = 0;
152 pfx.fp_len = 00;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100153 fib_table_entry_special_remove(fib_index,
154 &pfx,
155 FIB_SOURCE_DEFAULT_ROUTE);
156
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100157 fib_table_t *fib_table = fib_table_get(fib_index, FIB_PROTOCOL_IP6);
158 fib_source_t source;
Neale Ranns53da2212018-02-24 02:11:19 -0800159
Neale Ranns3bab8f92019-12-04 06:11:00 +0000160 /*
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100161 * validate no more routes.
162 */
Matthew Smithee167e52020-07-02 17:24:17 -0500163#if CLIB_DEBUG > 0
Neale Rannscbe25aa2019-09-30 10:53:31 +0000164 if (0 != fib_table->ft_total_route_counts)
165 fib_table_assert_empty(fib_table);
166#endif
167
Neale Ranns3bab8f92019-12-04 06:11:00 +0000168 vec_foreach_index(source, fib_table->ft_src_route_counts)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100169 {
170 ASSERT(0 == fib_table->ft_src_route_counts[source]);
171 }
172
173 if (~0 != fib_table->ft_table_id)
174 {
175 hash_unset (ip6_main.fib_index_by_table_id, fib_table->ft_table_id);
176 }
Neale Ranns3bab8f92019-12-04 06:11:00 +0000177 vec_free(fib_table->ft_src_route_counts);
Neale Rannsa3af3372017-03-28 03:49:52 -0700178 pool_put_index(ip6_main.v6_fibs, fib_table->ft_index);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100179 pool_put(ip6_main.fibs, fib_table);
180}
181
182fib_node_index_t
183ip6_fib_table_lookup (u32 fib_index,
184 const ip6_address_t *addr,
185 u32 len)
186{
Dave Barach908a5ea2017-07-14 12:42:21 -0400187 ip6_fib_table_instance_t *table;
Neale Rannsae809832018-11-23 09:00:27 -0800188 clib_bihash_kv_24_8_t kv, value;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100189 int i, n_p, rv;
190 u64 fib;
191
Neale Ranns5a59b2b2020-10-19 14:47:20 +0000192 table = &ip6_fib_table[IP6_FIB_TABLE_NON_FWDING];
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100193 n_p = vec_len (table->prefix_lengths_in_search_order);
194
195 kv.key[0] = addr->as_u64[0];
196 kv.key[1] = addr->as_u64[1];
197 fib = ((u64)((fib_index))<<32);
198
199 /*
200 * start search from a mask length same length or shorter.
201 * we don't want matches longer than the mask passed
202 */
203 i = 0;
204 while (i < n_p && table->prefix_lengths_in_search_order[i] > len)
205 {
206 i++;
207 }
208
209 for (; i < n_p; i++)
210 {
211 int dst_address_length = table->prefix_lengths_in_search_order[i];
212 ip6_address_t * mask = &ip6_main.fib_masks[dst_address_length];
213
214 ASSERT(dst_address_length >= 0 && dst_address_length <= 128);
215 //As lengths are decreasing, masks are increasingly specific.
216 kv.key[0] &= mask->as_u64[0];
217 kv.key[1] &= mask->as_u64[1];
218 kv.key[2] = fib | dst_address_length;
219
Neale Rannsae809832018-11-23 09:00:27 -0800220 rv = clib_bihash_search_inline_2_24_8(&table->ip6_hash, &kv, &value);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100221 if (rv == 0)
222 return value.value;
223 }
224
225 return (FIB_NODE_INDEX_INVALID);
226}
227
228fib_node_index_t
229ip6_fib_table_lookup_exact_match (u32 fib_index,
230 const ip6_address_t *addr,
231 u32 len)
232{
Dave Barach908a5ea2017-07-14 12:42:21 -0400233 ip6_fib_table_instance_t *table;
Neale Rannsae809832018-11-23 09:00:27 -0800234 clib_bihash_kv_24_8_t kv, value;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100235 ip6_address_t *mask;
236 u64 fib;
237 int rv;
238
Neale Ranns5a59b2b2020-10-19 14:47:20 +0000239 table = &ip6_fib_table[IP6_FIB_TABLE_NON_FWDING];
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100240 mask = &ip6_main.fib_masks[len];
241 fib = ((u64)((fib_index))<<32);
242
243 kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
244 kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
245 kv.key[2] = fib | len;
246
Neale Rannsae809832018-11-23 09:00:27 -0800247 rv = clib_bihash_search_inline_2_24_8(&table->ip6_hash, &kv, &value);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100248 if (rv == 0)
249 return value.value;
250
251 return (FIB_NODE_INDEX_INVALID);
252}
253
254static void
255compute_prefix_lengths_in_search_order (ip6_fib_table_instance_t *table)
256{
Neale Ranns42845dd2020-05-26 13:12:17 +0000257 u8 *old, *prefix_lengths_in_search_order = NULL;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100258 int i;
Neale Ranns42845dd2020-05-26 13:12:17 +0000259
260 /*
261 * build the list in a scratch space then cutover so the workers
262 * can continue uninterrupted.
263 */
264 old = table->prefix_lengths_in_search_order;
265
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100266 /* Note: bitmap reversed so this is in fact a longest prefix match */
267 clib_bitmap_foreach (i, table->non_empty_dst_address_length_bitmap,
268 ({
269 int dst_address_length = 128 - i;
Neale Ranns42845dd2020-05-26 13:12:17 +0000270 vec_add1(prefix_lengths_in_search_order, dst_address_length);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100271 }));
Neale Ranns42845dd2020-05-26 13:12:17 +0000272
273 table->prefix_lengths_in_search_order = prefix_lengths_in_search_order;
274
275 /*
276 * let the workers go once round the track before we free the old set
277 */
278 vlib_worker_wait_one_loop();
279 vec_free(old);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100280}
281
282void
283ip6_fib_table_entry_remove (u32 fib_index,
284 const ip6_address_t *addr,
285 u32 len)
286{
287 ip6_fib_table_instance_t *table;
Neale Rannsae809832018-11-23 09:00:27 -0800288 clib_bihash_kv_24_8_t kv;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100289 ip6_address_t *mask;
290 u64 fib;
291
Neale Ranns5a59b2b2020-10-19 14:47:20 +0000292 table = &ip6_fib_table[IP6_FIB_TABLE_NON_FWDING];
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100293 mask = &ip6_main.fib_masks[len];
294 fib = ((u64)((fib_index))<<32);
295
296 kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
297 kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
298 kv.key[2] = fib | len;
299
Neale Rannsae809832018-11-23 09:00:27 -0800300 clib_bihash_add_del_24_8(&table->ip6_hash, &kv, 0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100301
302 /* refcount accounting */
303 ASSERT (table->dst_address_length_refcounts[len] > 0);
304 if (--table->dst_address_length_refcounts[len] == 0)
305 {
306 table->non_empty_dst_address_length_bitmap =
307 clib_bitmap_set (table->non_empty_dst_address_length_bitmap,
308 128 - len, 0);
309 compute_prefix_lengths_in_search_order (table);
310 }
311}
312
313void
314ip6_fib_table_entry_insert (u32 fib_index,
315 const ip6_address_t *addr,
316 u32 len,
317 fib_node_index_t fib_entry_index)
318{
319 ip6_fib_table_instance_t *table;
Neale Rannsae809832018-11-23 09:00:27 -0800320 clib_bihash_kv_24_8_t kv;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100321 ip6_address_t *mask;
322 u64 fib;
323
Neale Ranns5a59b2b2020-10-19 14:47:20 +0000324 table = &ip6_fib_table[IP6_FIB_TABLE_NON_FWDING];
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100325 mask = &ip6_main.fib_masks[len];
326 fib = ((u64)((fib_index))<<32);
327
328 kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
329 kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
330 kv.key[2] = fib | len;
331 kv.value = fib_entry_index;
332
Neale Rannsae809832018-11-23 09:00:27 -0800333 clib_bihash_add_del_24_8(&table->ip6_hash, &kv, 1);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100334
Neale Ranns42845dd2020-05-26 13:12:17 +0000335 if (0 == table->dst_address_length_refcounts[len]++)
336 {
337 table->non_empty_dst_address_length_bitmap =
338 clib_bitmap_set (table->non_empty_dst_address_length_bitmap,
339 128 - len, 1);
340 compute_prefix_lengths_in_search_order (table);
341 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100342}
343
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100344u32 ip6_fib_table_fwding_lookup_with_if_index (ip6_main_t * im,
345 u32 sw_if_index,
346 const ip6_address_t * dst)
347{
348 u32 fib_index = vec_elt (im->fib_index_by_sw_if_index, sw_if_index);
Simon Zhange7eba482019-08-25 15:30:45 +0800349 return ip6_fib_table_fwding_lookup(fib_index, dst);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100350}
351
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100352u32
353ip6_fib_table_get_index_for_sw_if_index (u32 sw_if_index)
354{
355 if (sw_if_index >= vec_len(ip6_main.fib_index_by_sw_if_index))
356 {
357 /*
358 * This is the case for interfaces that are not yet mapped to
359 * a IP table
360 */
361 return (~0);
362 }
363 return (ip6_main.fib_index_by_sw_if_index[sw_if_index]);
364}
365
366void
367ip6_fib_table_fwding_dpo_update (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;
Neale Rannsae809832018-11-23 09:00:27 -0800373 clib_bihash_kv_24_8_t kv;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100374 ip6_address_t *mask;
375 u64 fib;
376
Neale Ranns5a59b2b2020-10-19 14:47:20 +0000377 table = &ip6_fib_table[IP6_FIB_TABLE_FWDING];
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100378 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
Neale Rannsae809832018-11-23 09:00:27 -0800386 clib_bihash_add_del_24_8(&table->ip6_hash, &kv, 1);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100387
Neale Ranns42845dd2020-05-26 13:12:17 +0000388 if (0 == table->dst_address_length_refcounts[len]++)
389 {
390 table->non_empty_dst_address_length_bitmap =
391 clib_bitmap_set (table->non_empty_dst_address_length_bitmap,
392 128 - len, 1);
393 compute_prefix_lengths_in_search_order (table);
394 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100395}
396
397void
398ip6_fib_table_fwding_dpo_remove (u32 fib_index,
399 const ip6_address_t *addr,
400 u32 len,
401 const dpo_id_t *dpo)
402{
403 ip6_fib_table_instance_t *table;
Neale Rannsae809832018-11-23 09:00:27 -0800404 clib_bihash_kv_24_8_t kv;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100405 ip6_address_t *mask;
406 u64 fib;
407
Neale Ranns5a59b2b2020-10-19 14:47:20 +0000408 table = &ip6_fib_table[IP6_FIB_TABLE_FWDING];
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100409 mask = &ip6_main.fib_masks[len];
410 fib = ((u64)((fib_index))<<32);
411
412 kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
413 kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
414 kv.key[2] = fib | len;
415 kv.value = dpo->dpoi_index;
416
Neale Rannsae809832018-11-23 09:00:27 -0800417 clib_bihash_add_del_24_8(&table->ip6_hash, &kv, 0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100418
419 /* refcount accounting */
420 ASSERT (table->dst_address_length_refcounts[len] > 0);
421 if (--table->dst_address_length_refcounts[len] == 0)
422 {
423 table->non_empty_dst_address_length_bitmap =
Neale Ranns32e1c012016-11-22 17:07:28 +0000424 clib_bitmap_set (table->non_empty_dst_address_length_bitmap,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100425 128 - len, 0);
426 compute_prefix_lengths_in_search_order (table);
427 }
428}
429
Neale Ranns32e1c012016-11-22 17:07:28 +0000430/**
431 * @brief Context when walking the IPv6 table. Since all VRFs are in the
432 * same hash table, we need to filter only those we need as we walk
433 */
434typedef struct ip6_fib_walk_ctx_t_
435{
436 u32 i6w_fib_index;
437 fib_table_walk_fn_t i6w_fn;
438 void *i6w_ctx;
Neale Ranns89541992017-04-06 04:41:02 -0700439 fib_prefix_t i6w_root;
440 fib_prefix_t *i6w_sub_trees;
Neale Ranns32e1c012016-11-22 17:07:28 +0000441} ip6_fib_walk_ctx_t;
442
443static int
444ip6_fib_walk_cb (clib_bihash_kv_24_8_t * kvp,
445 void *arg)
446{
447 ip6_fib_walk_ctx_t *ctx = arg;
Neale Ranns89541992017-04-06 04:41:02 -0700448 ip6_address_t key;
Neale Ranns32e1c012016-11-22 17:07:28 +0000449
450 if ((kvp->key[2] >> 32) == ctx->i6w_fib_index)
451 {
Neale Ranns89541992017-04-06 04:41:02 -0700452 key.as_u64[0] = kvp->key[0];
453 key.as_u64[1] = kvp->key[1];
454
455 if (ip6_destination_matches_route(&ip6_main,
456 &key,
457 &ctx->i6w_root.fp_addr.ip6,
458 ctx->i6w_root.fp_len))
459 {
460 const fib_prefix_t *sub_tree;
461 int skip = 0;
462
463 /*
464 * exclude sub-trees the walk does not want to explore
465 */
466 vec_foreach(sub_tree, ctx->i6w_sub_trees)
467 {
468 if (ip6_destination_matches_route(&ip6_main,
469 &key,
470 &sub_tree->fp_addr.ip6,
471 sub_tree->fp_len))
472 {
473 skip = 1;
474 break;
475 }
476 }
477
478 if (!skip)
479 {
480 switch (ctx->i6w_fn(kvp->value, ctx->i6w_ctx))
481 {
482 case FIB_TABLE_WALK_CONTINUE:
483 break;
484 case FIB_TABLE_WALK_SUB_TREE_STOP: {
485 fib_prefix_t pfx = {
486 .fp_proto = FIB_PROTOCOL_IP6,
487 .fp_len = kvp->key[2] & 0xffffffff,
488 .fp_addr.ip6 = key,
489 };
490 vec_add1(ctx->i6w_sub_trees, pfx);
491 break;
492 }
493 case FIB_TABLE_WALK_STOP:
494 goto done;
495 }
496 }
497 }
Neale Ranns32e1c012016-11-22 17:07:28 +0000498 }
Neale Ranns89541992017-04-06 04:41:02 -0700499done:
Neale Ranns32e1c012016-11-22 17:07:28 +0000500
501 return (1);
502}
503
504void
505ip6_fib_table_walk (u32 fib_index,
506 fib_table_walk_fn_t fn,
507 void *arg)
508{
509 ip6_fib_walk_ctx_t ctx = {
510 .i6w_fib_index = fib_index,
511 .i6w_fn = fn,
512 .i6w_ctx = arg,
Neale Ranns89541992017-04-06 04:41:02 -0700513 .i6w_root = {
514 .fp_proto = FIB_PROTOCOL_IP6,
515 },
516 .i6w_sub_trees = NULL,
Neale Ranns32e1c012016-11-22 17:07:28 +0000517 };
Neale Ranns32e1c012016-11-22 17:07:28 +0000518
Neale Rannsae809832018-11-23 09:00:27 -0800519 clib_bihash_foreach_key_value_pair_24_8(
Neale Ranns5a59b2b2020-10-19 14:47:20 +0000520 &ip6_fib_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash,
Neale Ranns89541992017-04-06 04:41:02 -0700521 ip6_fib_walk_cb,
522 &ctx);
Neale Ranns32e1c012016-11-22 17:07:28 +0000523
Neale Ranns89541992017-04-06 04:41:02 -0700524 vec_free(ctx.i6w_sub_trees);
525}
526
527void
528ip6_fib_table_sub_tree_walk (u32 fib_index,
529 const fib_prefix_t *root,
530 fib_table_walk_fn_t fn,
531 void *arg)
532{
533 ip6_fib_walk_ctx_t ctx = {
534 .i6w_fib_index = fib_index,
535 .i6w_fn = fn,
536 .i6w_ctx = arg,
537 .i6w_root = *root,
538 };
539
Neale Rannsae809832018-11-23 09:00:27 -0800540 clib_bihash_foreach_key_value_pair_24_8(
Neale Ranns5a59b2b2020-10-19 14:47:20 +0000541 &ip6_fib_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash,
Neale Ranns89541992017-04-06 04:41:02 -0700542 ip6_fib_walk_cb,
543 &ctx);
Neale Ranns32e1c012016-11-22 17:07:28 +0000544}
545
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100546typedef struct ip6_fib_show_ctx_t_ {
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100547 fib_node_index_t *entries;
548} ip6_fib_show_ctx_t;
549
Neale Ranns89541992017-04-06 04:41:02 -0700550static fib_table_walk_rc_t
Neale Ranns32e1c012016-11-22 17:07:28 +0000551ip6_fib_table_show_walk (fib_node_index_t fib_entry_index,
552 void *arg)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100553{
554 ip6_fib_show_ctx_t *ctx = arg;
555
Neale Ranns32e1c012016-11-22 17:07:28 +0000556 vec_add1(ctx->entries, fib_entry_index);
557
Neale Ranns89541992017-04-06 04:41:02 -0700558 return (FIB_TABLE_WALK_CONTINUE);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100559}
560
561static void
562ip6_fib_table_show_all (ip6_fib_t *fib,
563 vlib_main_t * vm)
564{
565 fib_node_index_t *fib_entry_index;
566 ip6_fib_show_ctx_t ctx = {
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100567 .entries = NULL,
568 };
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100569
Neale Ranns32e1c012016-11-22 17:07:28 +0000570 ip6_fib_table_walk(fib->index, ip6_fib_table_show_walk, &ctx);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100571 vec_sort_with_function(ctx.entries, fib_entry_cmp_for_sort);
572
573 vec_foreach(fib_entry_index, ctx.entries)
574 {
575 vlib_cli_output(vm, "%U",
576 format_fib_entry,
577 *fib_entry_index,
578 FIB_ENTRY_FORMAT_BRIEF);
579 }
580
581 vec_free(ctx.entries);
582}
583
584static void
585ip6_fib_table_show_one (ip6_fib_t *fib,
586 vlib_main_t * vm,
587 ip6_address_t *address,
Neale Ranns88fc83e2017-04-05 08:11:14 -0700588 u32 mask_len,
589 int detail)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100590{
591 vlib_cli_output(vm, "%U",
592 format_fib_entry,
593 ip6_fib_table_lookup(fib->index, address, mask_len),
Neale Ranns88fc83e2017-04-05 08:11:14 -0700594 (detail ?
595 FIB_ENTRY_FORMAT_DETAIL2:
596 FIB_ENTRY_FORMAT_DETAIL));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100597}
598
Neale Rannsc87aafa2017-11-29 00:59:31 -0800599u8 *
600format_ip6_fib_table_memory (u8 * s, va_list * args)
601{
Dave Barach97f5af02018-02-22 09:48:45 -0500602 uword bytes_inuse;
603
Neale Ranns5a59b2b2020-10-19 14:47:20 +0000604 bytes_inuse = (alloc_arena_next(&(ip6_fib_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash)) +
605 alloc_arena_next(&(ip6_fib_table[IP6_FIB_TABLE_FWDING].ip6_hash)));
Dave Barach97f5af02018-02-22 09:48:45 -0500606
Neale Ranns05cac302019-05-28 11:09:40 +0000607 s = format(s, "%=30s %=6d %=12ld\n",
Neale Rannsc87aafa2017-11-29 00:59:31 -0800608 "IPv6 unicast",
609 pool_elts(ip6_main.fibs),
Dave Barach97f5af02018-02-22 09:48:45 -0500610 bytes_inuse);
Neale Rannsc87aafa2017-11-29 00:59:31 -0800611 return (s);
612}
613
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100614typedef struct {
615 u32 fib_index;
616 u64 count_by_prefix_length[129];
617} count_routes_in_fib_at_prefix_length_arg_t;
618
Neale Rannsf50bac12019-12-06 05:53:17 +0000619static int
Neale Rannsae809832018-11-23 09:00:27 -0800620count_routes_in_fib_at_prefix_length (clib_bihash_kv_24_8_t * kvp,
Neale Ranns88fc83e2017-04-05 08:11:14 -0700621 void *arg)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100622{
623 count_routes_in_fib_at_prefix_length_arg_t * ap = arg;
624 int mask_width;
625
626 if ((kvp->key[2]>>32) != ap->fib_index)
Neale Rannsf50bac12019-12-06 05:53:17 +0000627 return (BIHASH_WALK_CONTINUE);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100628
629 mask_width = kvp->key[2] & 0xFF;
630
631 ap->count_by_prefix_length[mask_width]++;
Neale Rannsf50bac12019-12-06 05:53:17 +0000632
633 return (BIHASH_WALK_CONTINUE);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100634}
635
636static clib_error_t *
637ip6_show_fib (vlib_main_t * vm,
638 unformat_input_t * input,
639 vlib_cli_command_t * cmd)
640{
641 count_routes_in_fib_at_prefix_length_arg_t _ca, *ca = &_ca;
642 ip6_main_t * im6 = &ip6_main;
643 fib_table_t *fib_table;
644 ip6_fib_t * fib;
645 int verbose, matching;
646 ip6_address_t matching_address;
647 u32 mask_len = 128;
648 int table_id = -1, fib_index = ~0;
Neale Ranns88fc83e2017-04-05 08:11:14 -0700649 int detail = 0;
Neale Ranns05cac302019-05-28 11:09:40 +0000650 int hash = 0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100651
652 verbose = 1;
653 matching = 0;
654
655 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
656 {
657 if (unformat (input, "brief") ||
658 unformat (input, "summary") ||
659 unformat (input, "sum"))
660 verbose = 0;
Neale Ranns88fc83e2017-04-05 08:11:14 -0700661
662 else if (unformat (input, "detail") ||
663 unformat (input, "det"))
664 detail = 1;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100665
Neale Ranns05cac302019-05-28 11:09:40 +0000666 else if (unformat (input, "hash") ||
667 unformat (input, "mem") ||
668 unformat (input, "memory"))
669 hash = 1;
670
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100671 else if (unformat (input, "%U/%d",
672 unformat_ip6_address, &matching_address, &mask_len))
673 matching = 1;
674
675 else if (unformat (input, "%U", unformat_ip6_address, &matching_address))
676 matching = 1;
677
678 else if (unformat (input, "table %d", &table_id))
679 ;
680 else if (unformat (input, "index %d", &fib_index))
681 ;
682 else
683 break;
684 }
685
Neale Ranns05cac302019-05-28 11:09:40 +0000686 if (hash)
687 {
688 vlib_cli_output (vm, "IPv6 Non-Forwarding Hash Table:\n%U\n",
689 BV (format_bihash),
Neale Ranns5a59b2b2020-10-19 14:47:20 +0000690 &ip6_fib_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash,
Neale Ranns05cac302019-05-28 11:09:40 +0000691 detail);
692 vlib_cli_output (vm, "IPv6 Forwarding Hash Table:\n%U\n",
693 BV (format_bihash),
Neale Ranns5a59b2b2020-10-19 14:47:20 +0000694 &ip6_fib_table[IP6_FIB_TABLE_FWDING].ip6_hash,
Neale Ranns05cac302019-05-28 11:09:40 +0000695 detail);
696 return (NULL);
697 }
698
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100699 pool_foreach (fib_table, im6->fibs,
700 ({
Neale Ranns15002542017-09-10 04:39:11 -0700701 fib_source_t source;
702 u8 *s = NULL;
703
Neale Rannsa3af3372017-03-28 03:49:52 -0700704 fib = pool_elt_at_index(im6->v6_fibs, fib_table->ft_index);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100705 if (table_id >= 0 && table_id != (int)fib->table_id)
706 continue;
707 if (fib_index != ~0 && fib_index != (int)fib->index)
708 continue;
Neale Ranns53da2212018-02-24 02:11:19 -0800709 if (fib_table->ft_flags & FIB_TABLE_FLAG_IP6_LL)
710 continue;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100711
Neale Ranns9db6ada2019-11-08 12:42:31 +0000712 s = format(s, "%U, fib_index:%d, flow hash:[%U] epoch:%d flags:%U locks:[",
Neale Ranns15002542017-09-10 04:39:11 -0700713 format_fib_table_name, fib->index,
714 FIB_PROTOCOL_IP6,
715 fib->index,
716 format_ip_flow_hash_config,
Neale Ranns9db6ada2019-11-08 12:42:31 +0000717 fib_table->ft_flow_hash_config,
718 fib_table->ft_epoch,
719 format_fib_table_flags, fib_table->ft_flags);
720
Neale Ranns3bab8f92019-12-04 06:11:00 +0000721 vec_foreach_index(source, fib_table->ft_locks)
Neale Ranns15002542017-09-10 04:39:11 -0700722 {
723 if (0 != fib_table->ft_locks[source])
724 {
725 s = format(s, "%U:%d, ",
726 format_fib_source, source,
727 fib_table->ft_locks[source]);
728 }
729 }
730 s = format (s, "]");
Neale Ranns2297af02017-09-12 09:45:04 -0700731 vlib_cli_output (vm, "%v", s);
Neale Ranns15002542017-09-10 04:39:11 -0700732 vec_free(s);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100733
734 /* Show summary? */
735 if (! verbose)
736 {
Neale Ranns5a59b2b2020-10-19 14:47:20 +0000737 clib_bihash_24_8_t * h = &ip6_fib_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100738 int len;
739
740 vlib_cli_output (vm, "%=20s%=16s", "Prefix length", "Count");
741
Dave Barachb7b92992018-10-17 10:38:51 -0400742 clib_memset (ca, 0, sizeof(*ca));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100743 ca->fib_index = fib->index;
744
Neale Rannsae809832018-11-23 09:00:27 -0800745 clib_bihash_foreach_key_value_pair_24_8
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100746 (h, count_routes_in_fib_at_prefix_length, ca);
747
748 for (len = 128; len >= 0; len--)
749 {
750 if (ca->count_by_prefix_length[len])
751 vlib_cli_output (vm, "%=20d%=16lld",
752 len, ca->count_by_prefix_length[len]);
753 }
754 continue;
755 }
756
757 if (!matching)
758 {
759 ip6_fib_table_show_all(fib, vm);
760 }
761 else
762 {
Neale Ranns88fc83e2017-04-05 08:11:14 -0700763 ip6_fib_table_show_one(fib, vm, &matching_address, mask_len, detail);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100764 }
765 }));
766
767 return 0;
768}
769
770/*?
Billy McFall0683c9c2016-10-13 08:27:31 -0400771 * This command displays the IPv6 FIB Tables (VRF Tables) and the route
772 * entries for each table.
773 *
774 * @note This command will run for a long time when the FIB tables are
775 * comprised of millions of entries. For those senarios, consider displaying
776 * in summary mode.
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100777 *
778 * @cliexpar
Billy McFall0683c9c2016-10-13 08:27:31 -0400779 * @parblock
780 * Example of how to display all the IPv6 FIB tables:
781 * @cliexstart{show ip6 fib}
Billy McFallebb9a6a2016-10-17 11:35:32 -0400782 * ipv6-VRF:0, fib_index 0, flow hash: src dst sport dport proto
783 * @::/0
784 * unicast-ip6-chain
785 * [@0]: dpo-load-balance: [index:5 buckets:1 uRPF:5 to:[0:0]]
786 * [0] [@0]: dpo-drop ip6
787 * fe80::/10
788 * unicast-ip6-chain
789 * [@0]: dpo-load-balance: [index:10 buckets:1 uRPF:10 to:[0:0]]
790 * [0] [@2]: dpo-receive
791 * ff02::1/128
792 * unicast-ip6-chain
793 * [@0]: dpo-load-balance: [index:8 buckets:1 uRPF:8 to:[0:0]]
794 * [0] [@2]: dpo-receive
795 * ff02::2/128
796 * unicast-ip6-chain
797 * [@0]: dpo-load-balance: [index:7 buckets:1 uRPF:7 to:[0:0]]
798 * [0] [@2]: dpo-receive
799 * ff02::16/128
800 * unicast-ip6-chain
801 * [@0]: dpo-load-balance: [index:9 buckets:1 uRPF:9 to:[0:0]]
802 * [0] [@2]: dpo-receive
803 * ff02::1:ff00:0/104
804 * unicast-ip6-chain
805 * [@0]: dpo-load-balance: [index:6 buckets:1 uRPF:6 to:[0:0]]
806 * [0] [@2]: dpo-receive
807 * ipv6-VRF:8, fib_index 1, flow hash: src dst sport dport proto
808 * @::/0
809 * unicast-ip6-chain
810 * [@0]: dpo-load-balance: [index:21 buckets:1 uRPF:20 to:[0:0]]
811 * [0] [@0]: dpo-drop ip6
812 * @::a:1:1:0:4/126
813 * unicast-ip6-chain
814 * [@0]: dpo-load-balance: [index:27 buckets:1 uRPF:26 to:[0:0]]
815 * [0] [@4]: ipv6-glean: af_packet0
816 * @::a:1:1:0:7/128
817 * unicast-ip6-chain
818 * [@0]: dpo-load-balance: [index:28 buckets:1 uRPF:27 to:[0:0]]
819 * [0] [@2]: dpo-receive: @::a:1:1:0:7 on af_packet0
820 * fe80::/10
821 * unicast-ip6-chain
822 * [@0]: dpo-load-balance: [index:26 buckets:1 uRPF:25 to:[0:0]]
823 * [0] [@2]: dpo-receive
824 * fe80::fe:3eff:fe3e:9222/128
825 * unicast-ip6-chain
826 * [@0]: dpo-load-balance: [index:29 buckets:1 uRPF:28 to:[0:0]]
827 * [0] [@2]: dpo-receive: fe80::fe:3eff:fe3e:9222 on af_packet0
828 * ff02::1/128
829 * unicast-ip6-chain
830 * [@0]: dpo-load-balance: [index:24 buckets:1 uRPF:23 to:[0:0]]
831 * [0] [@2]: dpo-receive
832 * ff02::2/128
833 * unicast-ip6-chain
834 * [@0]: dpo-load-balance: [index:23 buckets:1 uRPF:22 to:[0:0]]
835 * [0] [@2]: dpo-receive
836 * ff02::16/128
837 * unicast-ip6-chain
838 * [@0]: dpo-load-balance: [index:25 buckets:1 uRPF:24 to:[0:0]]
839 * [0] [@2]: dpo-receive
840 * ff02::1:ff00:0/104
841 * unicast-ip6-chain
842 * [@0]: dpo-load-balance: [index:22 buckets:1 uRPF:21 to:[0:0]]
843 * [0] [@2]: dpo-receive
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100844 * @cliexend
Billy McFallebb9a6a2016-10-17 11:35:32 -0400845 *
Billy McFall0683c9c2016-10-13 08:27:31 -0400846 * Example of how to display a summary of all IPv6 FIB tables:
847 * @cliexstart{show ip6 fib summary}
Billy McFallebb9a6a2016-10-17 11:35:32 -0400848 * ipv6-VRF:0, fib_index 0, flow hash: src dst sport dport proto
Billy McFall0683c9c2016-10-13 08:27:31 -0400849 * Prefix length Count
850 * 128 3
851 * 104 1
Billy McFallebb9a6a2016-10-17 11:35:32 -0400852 * 10 1
853 * 0 1
854 * ipv6-VRF:8, fib_index 1, flow hash: src dst sport dport proto
Billy McFall0683c9c2016-10-13 08:27:31 -0400855 * Prefix length Count
856 * 128 5
857 * 126 1
858 * 104 1
Billy McFallebb9a6a2016-10-17 11:35:32 -0400859 * 10 1
860 * 0 1
Billy McFall0683c9c2016-10-13 08:27:31 -0400861 * @cliexend
862 * @endparblock
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100863 ?*/
Billy McFall0683c9c2016-10-13 08:27:31 -0400864/* *INDENT-OFF* */
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100865VLIB_CLI_COMMAND (ip6_show_fib_command, static) = {
866 .path = "show ip6 fib",
Neale Ranns88fc83e2017-04-05 08:11:14 -0700867 .short_help = "show ip6 fib [summary] [table <table-id>] [index <fib-id>] [<ip6-addr>[/<width>]] [detail]",
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100868 .function = ip6_show_fib,
869};
Billy McFall0683c9c2016-10-13 08:27:31 -0400870/* *INDENT-ON* */
Neale Ranns5a59b2b2020-10-19 14:47:20 +0000871
872static clib_error_t *
873ip6_config (vlib_main_t * vm, unformat_input_t * input)
874{
875 uword heapsize = 0;
876 u32 nbuckets = 0;
877
878 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
879 {
880 if (unformat (input, "hash-buckets %d", &nbuckets))
881 ;
882 else if (unformat (input, "heap-size %U",
883 unformat_memory_size, &heapsize))
884 ;
885 else
886 return clib_error_return (0, "unknown input '%U'",
887 format_unformat_error, input);
888 }
889
890 ip6_fib_table_nbuckets = nbuckets;
891 ip6_fib_table_size = heapsize;
892
893 return 0;
894}
895
896VLIB_EARLY_CONFIG_FUNCTION (ip6_config, "ip6");
897
898static clib_error_t *
899ip6_fib_init (vlib_main_t * vm)
900{
901 if (ip6_fib_table_nbuckets == 0)
902 ip6_fib_table_nbuckets = IP6_FIB_DEFAULT_HASH_NUM_BUCKETS;
903
904 ip6_fib_table_nbuckets = 1 << max_log2 (ip6_fib_table_nbuckets);
905
906 if (ip6_fib_table_size == 0)
907 ip6_fib_table_size = IP6_FIB_DEFAULT_HASH_MEMORY_SIZE;
908
909 clib_bihash_init_24_8 (&(ip6_fib_table[IP6_FIB_TABLE_FWDING].ip6_hash),
910 "ip6 FIB fwding table",
911 ip6_fib_table_nbuckets, ip6_fib_table_size);
912 clib_bihash_init_24_8 (&ip6_fib_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash,
913 "ip6 FIB non-fwding table",
914 ip6_fib_table_nbuckets, ip6_fib_table_size);
915
916 return (NULL);
917}
918
919VLIB_INIT_FUNCTION (ip6_fib_init) =
920{
921 .runs_before = VLIB_INITS("ip6_lookup_init"),
922};