blob: ccf8e22f5a7eba197164a623bbe780142d7d20d8 [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 */
Benoît Gannecaaa6332023-09-13 17:21:04 +0200163 fib_table_assert_empty(fib_table);
Neale Rannscbe25aa2019-09-30 10:53:31 +0000164
Neale Ranns3bab8f92019-12-04 06:11:00 +0000165 vec_foreach_index(source, fib_table->ft_src_route_counts)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100166 {
167 ASSERT(0 == fib_table->ft_src_route_counts[source]);
168 }
169
170 if (~0 != fib_table->ft_table_id)
171 {
172 hash_unset (ip6_main.fib_index_by_table_id, fib_table->ft_table_id);
173 }
Steven Luong97866a32022-02-08 07:59:11 -0800174 vec_free (fib_table->ft_locks);
Neale Ranns3bab8f92019-12-04 06:11:00 +0000175 vec_free(fib_table->ft_src_route_counts);
Neale Rannsa3af3372017-03-28 03:49:52 -0700176 pool_put_index(ip6_main.v6_fibs, fib_table->ft_index);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100177 pool_put(ip6_main.fibs, fib_table);
178}
179
180fib_node_index_t
181ip6_fib_table_lookup (u32 fib_index,
182 const ip6_address_t *addr,
183 u32 len)
184{
Dave Barach908a5ea2017-07-14 12:42:21 -0400185 ip6_fib_table_instance_t *table;
Neale Rannsae809832018-11-23 09:00:27 -0800186 clib_bihash_kv_24_8_t kv, value;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100187 int i, n_p, rv;
188 u64 fib;
189
Neale Ranns5a59b2b2020-10-19 14:47:20 +0000190 table = &ip6_fib_table[IP6_FIB_TABLE_NON_FWDING];
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100191 n_p = vec_len (table->prefix_lengths_in_search_order);
192
193 kv.key[0] = addr->as_u64[0];
194 kv.key[1] = addr->as_u64[1];
195 fib = ((u64)((fib_index))<<32);
196
197 /*
198 * start search from a mask length same length or shorter.
199 * we don't want matches longer than the mask passed
200 */
201 i = 0;
202 while (i < n_p && table->prefix_lengths_in_search_order[i] > len)
203 {
204 i++;
205 }
206
207 for (; i < n_p; i++)
208 {
209 int dst_address_length = table->prefix_lengths_in_search_order[i];
210 ip6_address_t * mask = &ip6_main.fib_masks[dst_address_length];
211
212 ASSERT(dst_address_length >= 0 && dst_address_length <= 128);
213 //As lengths are decreasing, masks are increasingly specific.
214 kv.key[0] &= mask->as_u64[0];
215 kv.key[1] &= mask->as_u64[1];
216 kv.key[2] = fib | dst_address_length;
217
Neale Rannsae809832018-11-23 09:00:27 -0800218 rv = clib_bihash_search_inline_2_24_8(&table->ip6_hash, &kv, &value);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100219 if (rv == 0)
220 return value.value;
221 }
222
223 return (FIB_NODE_INDEX_INVALID);
224}
225
226fib_node_index_t
227ip6_fib_table_lookup_exact_match (u32 fib_index,
228 const ip6_address_t *addr,
229 u32 len)
230{
Dave Barach908a5ea2017-07-14 12:42:21 -0400231 ip6_fib_table_instance_t *table;
Neale Rannsae809832018-11-23 09:00:27 -0800232 clib_bihash_kv_24_8_t kv, value;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100233 ip6_address_t *mask;
234 u64 fib;
235 int rv;
236
Neale Ranns5a59b2b2020-10-19 14:47:20 +0000237 table = &ip6_fib_table[IP6_FIB_TABLE_NON_FWDING];
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100238 mask = &ip6_main.fib_masks[len];
239 fib = ((u64)((fib_index))<<32);
240
241 kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
242 kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
243 kv.key[2] = fib | len;
244
Neale Rannsae809832018-11-23 09:00:27 -0800245 rv = clib_bihash_search_inline_2_24_8(&table->ip6_hash, &kv, &value);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100246 if (rv == 0)
247 return value.value;
248
249 return (FIB_NODE_INDEX_INVALID);
250}
251
252static void
253compute_prefix_lengths_in_search_order (ip6_fib_table_instance_t *table)
254{
Neale Ranns42845dd2020-05-26 13:12:17 +0000255 u8 *old, *prefix_lengths_in_search_order = NULL;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100256 int i;
Neale Ranns42845dd2020-05-26 13:12:17 +0000257
258 /*
259 * build the list in a scratch space then cutover so the workers
260 * can continue uninterrupted.
261 */
262 old = table->prefix_lengths_in_search_order;
263
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100264 /* Note: bitmap reversed so this is in fact a longest prefix match */
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100265 clib_bitmap_foreach (i, table->non_empty_dst_address_length_bitmap)
266 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100267 int dst_address_length = 128 - i;
Neale Ranns42845dd2020-05-26 13:12:17 +0000268 vec_add1(prefix_lengths_in_search_order, dst_address_length);
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100269 }
Neale Ranns42845dd2020-05-26 13:12:17 +0000270
271 table->prefix_lengths_in_search_order = prefix_lengths_in_search_order;
272
273 /*
274 * let the workers go once round the track before we free the old set
275 */
276 vlib_worker_wait_one_loop();
277 vec_free(old);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100278}
279
280void
281ip6_fib_table_entry_remove (u32 fib_index,
282 const ip6_address_t *addr,
283 u32 len)
284{
285 ip6_fib_table_instance_t *table;
Neale Rannsae809832018-11-23 09:00:27 -0800286 clib_bihash_kv_24_8_t kv;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100287 ip6_address_t *mask;
288 u64 fib;
289
Neale Ranns5a59b2b2020-10-19 14:47:20 +0000290 table = &ip6_fib_table[IP6_FIB_TABLE_NON_FWDING];
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100291 mask = &ip6_main.fib_masks[len];
292 fib = ((u64)((fib_index))<<32);
293
294 kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
295 kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
296 kv.key[2] = fib | len;
297
Neale Rannsae809832018-11-23 09:00:27 -0800298 clib_bihash_add_del_24_8(&table->ip6_hash, &kv, 0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100299
300 /* refcount accounting */
301 ASSERT (table->dst_address_length_refcounts[len] > 0);
302 if (--table->dst_address_length_refcounts[len] == 0)
303 {
304 table->non_empty_dst_address_length_bitmap =
305 clib_bitmap_set (table->non_empty_dst_address_length_bitmap,
306 128 - len, 0);
307 compute_prefix_lengths_in_search_order (table);
308 }
309}
310
311void
312ip6_fib_table_entry_insert (u32 fib_index,
313 const ip6_address_t *addr,
314 u32 len,
315 fib_node_index_t fib_entry_index)
316{
317 ip6_fib_table_instance_t *table;
Neale Rannsae809832018-11-23 09:00:27 -0800318 clib_bihash_kv_24_8_t kv;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100319 ip6_address_t *mask;
320 u64 fib;
321
Neale Ranns5a59b2b2020-10-19 14:47:20 +0000322 table = &ip6_fib_table[IP6_FIB_TABLE_NON_FWDING];
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100323 mask = &ip6_main.fib_masks[len];
324 fib = ((u64)((fib_index))<<32);
325
326 kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
327 kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
328 kv.key[2] = fib | len;
329 kv.value = fib_entry_index;
330
Neale Rannsae809832018-11-23 09:00:27 -0800331 clib_bihash_add_del_24_8(&table->ip6_hash, &kv, 1);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100332
Neale Ranns42845dd2020-05-26 13:12:17 +0000333 if (0 == table->dst_address_length_refcounts[len]++)
334 {
335 table->non_empty_dst_address_length_bitmap =
336 clib_bitmap_set (table->non_empty_dst_address_length_bitmap,
337 128 - len, 1);
338 compute_prefix_lengths_in_search_order (table);
339 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100340}
341
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100342u32 ip6_fib_table_fwding_lookup_with_if_index (ip6_main_t * im,
343 u32 sw_if_index,
344 const ip6_address_t * dst)
345{
346 u32 fib_index = vec_elt (im->fib_index_by_sw_if_index, sw_if_index);
Simon Zhange7eba482019-08-25 15:30:45 +0800347 return ip6_fib_table_fwding_lookup(fib_index, dst);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100348}
349
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100350u32
351ip6_fib_table_get_index_for_sw_if_index (u32 sw_if_index)
352{
353 if (sw_if_index >= vec_len(ip6_main.fib_index_by_sw_if_index))
354 {
355 /*
356 * This is the case for interfaces that are not yet mapped to
357 * a IP table
358 */
359 return (~0);
360 }
361 return (ip6_main.fib_index_by_sw_if_index[sw_if_index]);
362}
363
364void
365ip6_fib_table_fwding_dpo_update (u32 fib_index,
366 const ip6_address_t *addr,
367 u32 len,
368 const dpo_id_t *dpo)
369{
370 ip6_fib_table_instance_t *table;
Neale Rannsae809832018-11-23 09:00:27 -0800371 clib_bihash_kv_24_8_t kv;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100372 ip6_address_t *mask;
373 u64 fib;
374
Neale Ranns5a59b2b2020-10-19 14:47:20 +0000375 table = &ip6_fib_table[IP6_FIB_TABLE_FWDING];
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100376 mask = &ip6_main.fib_masks[len];
377 fib = ((u64)((fib_index))<<32);
378
379 kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
380 kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
381 kv.key[2] = fib | len;
382 kv.value = dpo->dpoi_index;
383
Neale Rannsae809832018-11-23 09:00:27 -0800384 clib_bihash_add_del_24_8(&table->ip6_hash, &kv, 1);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100385
Neale Ranns42845dd2020-05-26 13:12:17 +0000386 if (0 == table->dst_address_length_refcounts[len]++)
387 {
388 table->non_empty_dst_address_length_bitmap =
389 clib_bitmap_set (table->non_empty_dst_address_length_bitmap,
390 128 - len, 1);
391 compute_prefix_lengths_in_search_order (table);
392 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100393}
394
395void
396ip6_fib_table_fwding_dpo_remove (u32 fib_index,
397 const ip6_address_t *addr,
398 u32 len,
399 const dpo_id_t *dpo)
400{
401 ip6_fib_table_instance_t *table;
Neale Rannsae809832018-11-23 09:00:27 -0800402 clib_bihash_kv_24_8_t kv;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100403 ip6_address_t *mask;
404 u64 fib;
405
Neale Ranns5a59b2b2020-10-19 14:47:20 +0000406 table = &ip6_fib_table[IP6_FIB_TABLE_FWDING];
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100407 mask = &ip6_main.fib_masks[len];
408 fib = ((u64)((fib_index))<<32);
409
410 kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
411 kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
412 kv.key[2] = fib | len;
413 kv.value = dpo->dpoi_index;
414
Neale Rannsae809832018-11-23 09:00:27 -0800415 clib_bihash_add_del_24_8(&table->ip6_hash, &kv, 0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100416
417 /* refcount accounting */
418 ASSERT (table->dst_address_length_refcounts[len] > 0);
419 if (--table->dst_address_length_refcounts[len] == 0)
420 {
421 table->non_empty_dst_address_length_bitmap =
Neale Ranns32e1c012016-11-22 17:07:28 +0000422 clib_bitmap_set (table->non_empty_dst_address_length_bitmap,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100423 128 - len, 0);
424 compute_prefix_lengths_in_search_order (table);
425 }
426}
427
Neale Ranns32e1c012016-11-22 17:07:28 +0000428/**
429 * @brief Context when walking the IPv6 table. Since all VRFs are in the
430 * same hash table, we need to filter only those we need as we walk
431 */
432typedef struct ip6_fib_walk_ctx_t_
433{
434 u32 i6w_fib_index;
435 fib_table_walk_fn_t i6w_fn;
436 void *i6w_ctx;
Neale Ranns89541992017-04-06 04:41:02 -0700437 fib_prefix_t i6w_root;
438 fib_prefix_t *i6w_sub_trees;
Neale Ranns32e1c012016-11-22 17:07:28 +0000439} ip6_fib_walk_ctx_t;
440
441static int
442ip6_fib_walk_cb (clib_bihash_kv_24_8_t * kvp,
443 void *arg)
444{
445 ip6_fib_walk_ctx_t *ctx = arg;
Neale Ranns89541992017-04-06 04:41:02 -0700446 ip6_address_t key;
Neale Ranns32e1c012016-11-22 17:07:28 +0000447
448 if ((kvp->key[2] >> 32) == ctx->i6w_fib_index)
449 {
Neale Ranns89541992017-04-06 04:41:02 -0700450 key.as_u64[0] = kvp->key[0];
451 key.as_u64[1] = kvp->key[1];
452
453 if (ip6_destination_matches_route(&ip6_main,
454 &key,
455 &ctx->i6w_root.fp_addr.ip6,
456 ctx->i6w_root.fp_len))
457 {
458 const fib_prefix_t *sub_tree;
459 int skip = 0;
460
461 /*
462 * exclude sub-trees the walk does not want to explore
463 */
464 vec_foreach(sub_tree, ctx->i6w_sub_trees)
465 {
466 if (ip6_destination_matches_route(&ip6_main,
467 &key,
468 &sub_tree->fp_addr.ip6,
469 sub_tree->fp_len))
470 {
471 skip = 1;
472 break;
473 }
474 }
475
476 if (!skip)
477 {
478 switch (ctx->i6w_fn(kvp->value, ctx->i6w_ctx))
479 {
480 case FIB_TABLE_WALK_CONTINUE:
481 break;
482 case FIB_TABLE_WALK_SUB_TREE_STOP: {
483 fib_prefix_t pfx = {
484 .fp_proto = FIB_PROTOCOL_IP6,
485 .fp_len = kvp->key[2] & 0xffffffff,
486 .fp_addr.ip6 = key,
487 };
488 vec_add1(ctx->i6w_sub_trees, pfx);
489 break;
490 }
491 case FIB_TABLE_WALK_STOP:
492 goto done;
493 }
494 }
495 }
Neale Ranns32e1c012016-11-22 17:07:28 +0000496 }
Neale Ranns89541992017-04-06 04:41:02 -0700497done:
Neale Ranns32e1c012016-11-22 17:07:28 +0000498
499 return (1);
500}
501
502void
503ip6_fib_table_walk (u32 fib_index,
504 fib_table_walk_fn_t fn,
505 void *arg)
506{
507 ip6_fib_walk_ctx_t ctx = {
508 .i6w_fib_index = fib_index,
509 .i6w_fn = fn,
510 .i6w_ctx = arg,
Neale Ranns89541992017-04-06 04:41:02 -0700511 .i6w_root = {
512 .fp_proto = FIB_PROTOCOL_IP6,
513 },
514 .i6w_sub_trees = NULL,
Neale Ranns32e1c012016-11-22 17:07:28 +0000515 };
Neale Ranns32e1c012016-11-22 17:07:28 +0000516
Neale Rannsae809832018-11-23 09:00:27 -0800517 clib_bihash_foreach_key_value_pair_24_8(
Neale Ranns5a59b2b2020-10-19 14:47:20 +0000518 &ip6_fib_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash,
Neale Ranns89541992017-04-06 04:41:02 -0700519 ip6_fib_walk_cb,
520 &ctx);
Neale Ranns32e1c012016-11-22 17:07:28 +0000521
Neale Ranns89541992017-04-06 04:41:02 -0700522 vec_free(ctx.i6w_sub_trees);
523}
524
525void
526ip6_fib_table_sub_tree_walk (u32 fib_index,
527 const fib_prefix_t *root,
528 fib_table_walk_fn_t fn,
529 void *arg)
530{
531 ip6_fib_walk_ctx_t ctx = {
532 .i6w_fib_index = fib_index,
533 .i6w_fn = fn,
534 .i6w_ctx = arg,
535 .i6w_root = *root,
536 };
537
Neale Rannsae809832018-11-23 09:00:27 -0800538 clib_bihash_foreach_key_value_pair_24_8(
Neale Ranns5a59b2b2020-10-19 14:47:20 +0000539 &ip6_fib_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash,
Neale Ranns89541992017-04-06 04:41:02 -0700540 ip6_fib_walk_cb,
541 &ctx);
Neale Ranns32e1c012016-11-22 17:07:28 +0000542}
543
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100544typedef struct ip6_fib_show_ctx_t_ {
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100545 fib_node_index_t *entries;
546} ip6_fib_show_ctx_t;
547
Neale Ranns89541992017-04-06 04:41:02 -0700548static fib_table_walk_rc_t
Neale Ranns32e1c012016-11-22 17:07:28 +0000549ip6_fib_table_show_walk (fib_node_index_t fib_entry_index,
550 void *arg)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100551{
552 ip6_fib_show_ctx_t *ctx = arg;
553
Neale Ranns32e1c012016-11-22 17:07:28 +0000554 vec_add1(ctx->entries, fib_entry_index);
555
Neale Ranns89541992017-04-06 04:41:02 -0700556 return (FIB_TABLE_WALK_CONTINUE);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100557}
558
559static void
560ip6_fib_table_show_all (ip6_fib_t *fib,
561 vlib_main_t * vm)
562{
563 fib_node_index_t *fib_entry_index;
564 ip6_fib_show_ctx_t ctx = {
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100565 .entries = NULL,
566 };
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100567
Neale Ranns32e1c012016-11-22 17:07:28 +0000568 ip6_fib_table_walk(fib->index, ip6_fib_table_show_walk, &ctx);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100569 vec_sort_with_function(ctx.entries, fib_entry_cmp_for_sort);
570
571 vec_foreach(fib_entry_index, ctx.entries)
572 {
573 vlib_cli_output(vm, "%U",
574 format_fib_entry,
575 *fib_entry_index,
576 FIB_ENTRY_FORMAT_BRIEF);
577 }
578
579 vec_free(ctx.entries);
580}
581
582static void
583ip6_fib_table_show_one (ip6_fib_t *fib,
584 vlib_main_t * vm,
585 ip6_address_t *address,
Neale Ranns88fc83e2017-04-05 08:11:14 -0700586 u32 mask_len,
587 int detail)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100588{
589 vlib_cli_output(vm, "%U",
590 format_fib_entry,
591 ip6_fib_table_lookup(fib->index, address, mask_len),
Neale Ranns88fc83e2017-04-05 08:11:14 -0700592 (detail ?
593 FIB_ENTRY_FORMAT_DETAIL2:
594 FIB_ENTRY_FORMAT_DETAIL));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100595}
596
Neale Rannsc87aafa2017-11-29 00:59:31 -0800597u8 *
598format_ip6_fib_table_memory (u8 * s, va_list * args)
599{
Dave Barach97f5af02018-02-22 09:48:45 -0500600 uword bytes_inuse;
601
Neale Ranns5a59b2b2020-10-19 14:47:20 +0000602 bytes_inuse = (alloc_arena_next(&(ip6_fib_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash)) +
603 alloc_arena_next(&(ip6_fib_table[IP6_FIB_TABLE_FWDING].ip6_hash)));
Dave Barach97f5af02018-02-22 09:48:45 -0500604
Neale Ranns05cac302019-05-28 11:09:40 +0000605 s = format(s, "%=30s %=6d %=12ld\n",
Neale Rannsc87aafa2017-11-29 00:59:31 -0800606 "IPv6 unicast",
607 pool_elts(ip6_main.fibs),
Dave Barach97f5af02018-02-22 09:48:45 -0500608 bytes_inuse);
Neale Rannsc87aafa2017-11-29 00:59:31 -0800609 return (s);
610}
611
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100612typedef struct {
613 u32 fib_index;
614 u64 count_by_prefix_length[129];
615} count_routes_in_fib_at_prefix_length_arg_t;
616
Neale Rannsf50bac12019-12-06 05:53:17 +0000617static int
Neale Rannsae809832018-11-23 09:00:27 -0800618count_routes_in_fib_at_prefix_length (clib_bihash_kv_24_8_t * kvp,
Neale Ranns88fc83e2017-04-05 08:11:14 -0700619 void *arg)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100620{
621 count_routes_in_fib_at_prefix_length_arg_t * ap = arg;
622 int mask_width;
623
624 if ((kvp->key[2]>>32) != ap->fib_index)
Neale Rannsf50bac12019-12-06 05:53:17 +0000625 return (BIHASH_WALK_CONTINUE);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100626
627 mask_width = kvp->key[2] & 0xFF;
628
629 ap->count_by_prefix_length[mask_width]++;
Neale Rannsf50bac12019-12-06 05:53:17 +0000630
631 return (BIHASH_WALK_CONTINUE);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100632}
633
634static clib_error_t *
635ip6_show_fib (vlib_main_t * vm,
636 unformat_input_t * input,
637 vlib_cli_command_t * cmd)
638{
639 count_routes_in_fib_at_prefix_length_arg_t _ca, *ca = &_ca;
640 ip6_main_t * im6 = &ip6_main;
641 fib_table_t *fib_table;
642 ip6_fib_t * fib;
643 int verbose, matching;
644 ip6_address_t matching_address;
645 u32 mask_len = 128;
646 int table_id = -1, fib_index = ~0;
Neale Ranns88fc83e2017-04-05 08:11:14 -0700647 int detail = 0;
Neale Ranns05cac302019-05-28 11:09:40 +0000648 int hash = 0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100649
650 verbose = 1;
651 matching = 0;
652
653 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
654 {
655 if (unformat (input, "brief") ||
656 unformat (input, "summary") ||
657 unformat (input, "sum"))
658 verbose = 0;
Neale Ranns88fc83e2017-04-05 08:11:14 -0700659
660 else if (unformat (input, "detail") ||
661 unformat (input, "det"))
662 detail = 1;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100663
Neale Ranns05cac302019-05-28 11:09:40 +0000664 else if (unformat (input, "hash") ||
665 unformat (input, "mem") ||
666 unformat (input, "memory"))
667 hash = 1;
668
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100669 else if (unformat (input, "%U/%d",
670 unformat_ip6_address, &matching_address, &mask_len))
671 matching = 1;
672
673 else if (unformat (input, "%U", unformat_ip6_address, &matching_address))
674 matching = 1;
675
676 else if (unformat (input, "table %d", &table_id))
677 ;
678 else if (unformat (input, "index %d", &fib_index))
679 ;
680 else
681 break;
682 }
683
Neale Ranns05cac302019-05-28 11:09:40 +0000684 if (hash)
685 {
686 vlib_cli_output (vm, "IPv6 Non-Forwarding Hash Table:\n%U\n",
687 BV (format_bihash),
Neale Ranns5a59b2b2020-10-19 14:47:20 +0000688 &ip6_fib_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash,
Neale Ranns05cac302019-05-28 11:09:40 +0000689 detail);
690 vlib_cli_output (vm, "IPv6 Forwarding Hash Table:\n%U\n",
691 BV (format_bihash),
Neale Ranns5a59b2b2020-10-19 14:47:20 +0000692 &ip6_fib_table[IP6_FIB_TABLE_FWDING].ip6_hash,
Neale Ranns05cac302019-05-28 11:09:40 +0000693 detail);
694 return (NULL);
695 }
696
Damjan Marionb2c31b62020-12-13 21:47:40 +0100697 pool_foreach (fib_table, im6->fibs)
698 {
Neale Ranns15002542017-09-10 04:39:11 -0700699 fib_source_t source;
700 u8 *s = NULL;
701
Neale Rannsa3af3372017-03-28 03:49:52 -0700702 fib = pool_elt_at_index(im6->v6_fibs, fib_table->ft_index);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100703 if (table_id >= 0 && table_id != (int)fib->table_id)
704 continue;
705 if (fib_index != ~0 && fib_index != (int)fib->index)
706 continue;
Neale Ranns53da2212018-02-24 02:11:19 -0800707 if (fib_table->ft_flags & FIB_TABLE_FLAG_IP6_LL)
708 continue;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100709
Neale Ranns9db6ada2019-11-08 12:42:31 +0000710 s = format(s, "%U, fib_index:%d, flow hash:[%U] epoch:%d flags:%U locks:[",
Neale Ranns15002542017-09-10 04:39:11 -0700711 format_fib_table_name, fib->index,
712 FIB_PROTOCOL_IP6,
713 fib->index,
714 format_ip_flow_hash_config,
Neale Ranns9db6ada2019-11-08 12:42:31 +0000715 fib_table->ft_flow_hash_config,
716 fib_table->ft_epoch,
717 format_fib_table_flags, fib_table->ft_flags);
718
Neale Ranns3bab8f92019-12-04 06:11:00 +0000719 vec_foreach_index(source, fib_table->ft_locks)
Neale Ranns15002542017-09-10 04:39:11 -0700720 {
721 if (0 != fib_table->ft_locks[source])
722 {
723 s = format(s, "%U:%d, ",
724 format_fib_source, source,
725 fib_table->ft_locks[source]);
726 }
727 }
728 s = format (s, "]");
Neale Ranns2297af02017-09-12 09:45:04 -0700729 vlib_cli_output (vm, "%v", s);
Neale Ranns15002542017-09-10 04:39:11 -0700730 vec_free(s);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100731
732 /* Show summary? */
733 if (! verbose)
734 {
Neale Ranns5a59b2b2020-10-19 14:47:20 +0000735 clib_bihash_24_8_t * h = &ip6_fib_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100736 int len;
737
738 vlib_cli_output (vm, "%=20s%=16s", "Prefix length", "Count");
739
Dave Barachb7b92992018-10-17 10:38:51 -0400740 clib_memset (ca, 0, sizeof(*ca));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100741 ca->fib_index = fib->index;
742
Neale Rannsae809832018-11-23 09:00:27 -0800743 clib_bihash_foreach_key_value_pair_24_8
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100744 (h, count_routes_in_fib_at_prefix_length, ca);
745
746 for (len = 128; len >= 0; len--)
747 {
748 if (ca->count_by_prefix_length[len])
749 vlib_cli_output (vm, "%=20d%=16lld",
750 len, ca->count_by_prefix_length[len]);
751 }
752 continue;
753 }
754
755 if (!matching)
756 {
757 ip6_fib_table_show_all(fib, vm);
758 }
759 else
760 {
Neale Ranns88fc83e2017-04-05 08:11:14 -0700761 ip6_fib_table_show_one(fib, vm, &matching_address, mask_len, detail);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100762 }
Damjan Marionb2c31b62020-12-13 21:47:40 +0100763 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100764
765 return 0;
766}
767
768/*?
Billy McFall0683c9c2016-10-13 08:27:31 -0400769 * This command displays the IPv6 FIB Tables (VRF Tables) and the route
770 * entries for each table.
771 *
772 * @note This command will run for a long time when the FIB tables are
Nathan Skrzypczakda331052021-09-29 15:28:26 +0200773 * comprised of millions of entries. For those scenarios, consider displaying
Billy McFall0683c9c2016-10-13 08:27:31 -0400774 * in summary mode.
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100775 *
776 * @cliexpar
Billy McFall0683c9c2016-10-13 08:27:31 -0400777 * @parblock
778 * Example of how to display all the IPv6 FIB tables:
779 * @cliexstart{show ip6 fib}
Billy McFallebb9a6a2016-10-17 11:35:32 -0400780 * ipv6-VRF:0, fib_index 0, flow hash: src dst sport dport proto
781 * @::/0
782 * unicast-ip6-chain
783 * [@0]: dpo-load-balance: [index:5 buckets:1 uRPF:5 to:[0:0]]
784 * [0] [@0]: dpo-drop ip6
785 * fe80::/10
786 * unicast-ip6-chain
787 * [@0]: dpo-load-balance: [index:10 buckets:1 uRPF:10 to:[0:0]]
788 * [0] [@2]: dpo-receive
789 * ff02::1/128
790 * unicast-ip6-chain
791 * [@0]: dpo-load-balance: [index:8 buckets:1 uRPF:8 to:[0:0]]
792 * [0] [@2]: dpo-receive
793 * ff02::2/128
794 * unicast-ip6-chain
795 * [@0]: dpo-load-balance: [index:7 buckets:1 uRPF:7 to:[0:0]]
796 * [0] [@2]: dpo-receive
797 * ff02::16/128
798 * unicast-ip6-chain
799 * [@0]: dpo-load-balance: [index:9 buckets:1 uRPF:9 to:[0:0]]
800 * [0] [@2]: dpo-receive
801 * ff02::1:ff00:0/104
802 * unicast-ip6-chain
803 * [@0]: dpo-load-balance: [index:6 buckets:1 uRPF:6 to:[0:0]]
804 * [0] [@2]: dpo-receive
805 * ipv6-VRF:8, fib_index 1, flow hash: src dst sport dport proto
806 * @::/0
807 * unicast-ip6-chain
808 * [@0]: dpo-load-balance: [index:21 buckets:1 uRPF:20 to:[0:0]]
809 * [0] [@0]: dpo-drop ip6
810 * @::a:1:1:0:4/126
811 * unicast-ip6-chain
812 * [@0]: dpo-load-balance: [index:27 buckets:1 uRPF:26 to:[0:0]]
813 * [0] [@4]: ipv6-glean: af_packet0
814 * @::a:1:1:0:7/128
815 * unicast-ip6-chain
816 * [@0]: dpo-load-balance: [index:28 buckets:1 uRPF:27 to:[0:0]]
817 * [0] [@2]: dpo-receive: @::a:1:1:0:7 on af_packet0
818 * fe80::/10
819 * unicast-ip6-chain
820 * [@0]: dpo-load-balance: [index:26 buckets:1 uRPF:25 to:[0:0]]
821 * [0] [@2]: dpo-receive
822 * fe80::fe:3eff:fe3e:9222/128
823 * unicast-ip6-chain
824 * [@0]: dpo-load-balance: [index:29 buckets:1 uRPF:28 to:[0:0]]
825 * [0] [@2]: dpo-receive: fe80::fe:3eff:fe3e:9222 on af_packet0
826 * ff02::1/128
827 * unicast-ip6-chain
828 * [@0]: dpo-load-balance: [index:24 buckets:1 uRPF:23 to:[0:0]]
829 * [0] [@2]: dpo-receive
830 * ff02::2/128
831 * unicast-ip6-chain
832 * [@0]: dpo-load-balance: [index:23 buckets:1 uRPF:22 to:[0:0]]
833 * [0] [@2]: dpo-receive
834 * ff02::16/128
835 * unicast-ip6-chain
836 * [@0]: dpo-load-balance: [index:25 buckets:1 uRPF:24 to:[0:0]]
837 * [0] [@2]: dpo-receive
838 * ff02::1:ff00:0/104
839 * unicast-ip6-chain
840 * [@0]: dpo-load-balance: [index:22 buckets:1 uRPF:21 to:[0:0]]
841 * [0] [@2]: dpo-receive
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100842 * @cliexend
Billy McFallebb9a6a2016-10-17 11:35:32 -0400843 *
Billy McFall0683c9c2016-10-13 08:27:31 -0400844 * Example of how to display a summary of all IPv6 FIB tables:
845 * @cliexstart{show ip6 fib summary}
Billy McFallebb9a6a2016-10-17 11:35:32 -0400846 * ipv6-VRF:0, fib_index 0, flow hash: src dst sport dport proto
Billy McFall0683c9c2016-10-13 08:27:31 -0400847 * Prefix length Count
848 * 128 3
849 * 104 1
Billy McFallebb9a6a2016-10-17 11:35:32 -0400850 * 10 1
851 * 0 1
852 * ipv6-VRF:8, fib_index 1, flow hash: src dst sport dport proto
Billy McFall0683c9c2016-10-13 08:27:31 -0400853 * Prefix length Count
854 * 128 5
855 * 126 1
856 * 104 1
Billy McFallebb9a6a2016-10-17 11:35:32 -0400857 * 10 1
858 * 0 1
Billy McFall0683c9c2016-10-13 08:27:31 -0400859 * @cliexend
860 * @endparblock
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100861 ?*/
862VLIB_CLI_COMMAND (ip6_show_fib_command, static) = {
863 .path = "show ip6 fib",
Neale Ranns88fc83e2017-04-05 08:11:14 -0700864 .short_help = "show ip6 fib [summary] [table <table-id>] [index <fib-id>] [<ip6-addr>[/<width>]] [detail]",
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100865 .function = ip6_show_fib,
866};
Neale Ranns5a59b2b2020-10-19 14:47:20 +0000867
868static clib_error_t *
869ip6_config (vlib_main_t * vm, unformat_input_t * input)
870{
871 uword heapsize = 0;
872 u32 nbuckets = 0;
Jon Loeligerd465fd02024-03-22 12:22:36 -0500873 char *default_name = 0;
Neale Ranns5a59b2b2020-10-19 14:47:20 +0000874
875 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
876 {
877 if (unformat (input, "hash-buckets %d", &nbuckets))
878 ;
879 else if (unformat (input, "heap-size %U",
880 unformat_memory_size, &heapsize))
881 ;
Jon Loeligerd465fd02024-03-22 12:22:36 -0500882 else if (unformat (input, "default-table-name %s", &default_name))
883 ;
Neale Ranns5a59b2b2020-10-19 14:47:20 +0000884 else
885 return clib_error_return (0, "unknown input '%U'",
886 format_unformat_error, input);
887 }
888
889 ip6_fib_table_nbuckets = nbuckets;
890 ip6_fib_table_size = heapsize;
Jon Loeligerd465fd02024-03-22 12:22:36 -0500891 fib_table_default_names[FIB_PROTOCOL_IP6] = default_name;
Neale Ranns5a59b2b2020-10-19 14:47:20 +0000892
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};