blob: de6cbf3d37c98e901bbcf0c34ee05aea60650590 [file] [log] [blame]
Neale Ranns32e1c012016-11-22 17:07:28 +00001/*
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/mfib/ip6_mfib.h>
17
18#include <vnet/mfib/mfib_table.h>
19#include <vnet/mfib/mfib_entry.h>
20#include <vnet/fib/ip6_fib.h>
21
Neale Ranns5a59b2b2020-10-19 14:47:20 +000022ip6_mfib_table_instance_t ip6_mfib_table;
23
Neale Ranns32e1c012016-11-22 17:07:28 +000024/**
Neale Ranns32e1c012016-11-22 17:07:28 +000025 * Key and mask for radix
26 */
Neale Rannsae809832018-11-23 09:00:27 -080027typedef clib_bihash_kv_40_8_t ip6_mfib_key_t;
Neale Ranns32e1c012016-11-22 17:07:28 +000028
29static const mfib_prefix_t all_zeros = {
30 /* (*,*) */
31 .fp_src_addr = {
32 .ip6.as_u64 = {0, 0},
33 },
34 .fp_grp_addr = {
35 .ip6.as_u64 = {0, 0},
36 },
37 .fp_len = 0,
38 .fp_proto = FIB_PROTOCOL_IP6,
39};
40
41typedef enum ip6_mfib_special_type_t_ {
42 IP6_MFIB_SPECIAL_TYPE_NONE,
43 IP6_MFIB_SPECIAL_TYPE_SOLICITED,
44} ip6_mfib_special_type_t;
45
46typedef struct ip6_mfib_special_t_ {
47 /**
48 * @brief solicited or not
49 */
50 ip6_mfib_special_type_t ims_type;
51
52 /**
53 * @brief the Prefix length
54 */
55 u8 ims_len;
56
57 /**
58 * @brief The last byte of the mcast address
59 */
60 u8 ims_byte;
61 /**
62 * @brief The scope of the address
63 */
64 u8 ims_scope;
65} ip6_mfib_special_t;
66
67static const ip6_mfib_special_t ip6_mfib_specials[] =
68{
69 {
70 /*
71 * Add ff02::1:ff00:0/104 via local route for all tables.
72 * This is required for neighbor discovery to work.
73 */
74 .ims_type = IP6_MFIB_SPECIAL_TYPE_SOLICITED,
75 .ims_len = 104,
76 },
77 {
78 /*
79 * all-routers multicast address
80 */
81 .ims_type = IP6_MFIB_SPECIAL_TYPE_NONE,
82 .ims_scope = IP6_MULTICAST_SCOPE_link_local,
83 .ims_byte = IP6_MULTICAST_GROUP_ID_all_routers,
84 .ims_len = 128,
85 },
86 {
87 /*
88 * all-nodes multicast address
89 */
90 .ims_type = IP6_MFIB_SPECIAL_TYPE_NONE,
91 .ims_scope = IP6_MULTICAST_SCOPE_link_local,
92 .ims_byte = IP6_MULTICAST_GROUP_ID_all_hosts,
93 .ims_len = 128,
94 },
95 {
96 /*
97 * Add all-mldv2 multicast address via local route for all tables
98 */
99 .ims_type = IP6_MFIB_SPECIAL_TYPE_NONE,
100 .ims_len = 128,
101 .ims_scope = IP6_MULTICAST_SCOPE_link_local,
102 .ims_byte = IP6_MULTICAST_GROUP_ID_mldv2_routers,
103 }
104};
105
106#define FOR_EACH_IP6_SPECIAL(_pfx, _body) \
107{ \
108 const ip6_mfib_special_t *_spec; \
109 u8 _ii; \
110 for (_ii = 0; \
111 _ii < ARRAY_LEN(ip6_mfib_specials); \
112 _ii++) \
113 { \
114 _spec = &ip6_mfib_specials[_ii]; \
115 if (IP6_MFIB_SPECIAL_TYPE_SOLICITED == _spec->ims_type) \
116 { \
117 ip6_set_solicited_node_multicast_address( \
118 &(_pfx)->fp_grp_addr.ip6, 0); \
119 } \
120 else \
121 { \
122 ip6_set_reserved_multicast_address ( \
123 &(_pfx)->fp_grp_addr.ip6, \
124 _spec->ims_scope, \
125 _spec->ims_byte); \
126 } \
127 (_pfx)->fp_len = _spec->ims_len; \
128 do { _body; } while (0); \
129 } \
130}
131
132
133static u32
Neale Ranns15002542017-09-10 04:39:11 -0700134ip6_create_mfib_with_table_id (u32 table_id,
135 mfib_source_t src)
Neale Ranns32e1c012016-11-22 17:07:28 +0000136{
137 mfib_table_t *mfib_table;
138 mfib_prefix_t pfx = {
139 .fp_proto = FIB_PROTOCOL_IP6,
140 };
141 const fib_route_path_t path_for_us = {
Neale Rannsda78f952017-05-24 09:15:43 -0700142 .frp_proto = DPO_PROTO_IP6,
Neale Ranns32e1c012016-11-22 17:07:28 +0000143 .frp_addr = zero_addr,
144 .frp_sw_if_index = 0xffffffff,
145 .frp_fib_index = ~0,
Neale Ranns097fa662018-05-01 05:17:55 -0700146 .frp_weight = 1,
Neale Ranns32e1c012016-11-22 17:07:28 +0000147 .frp_flags = FIB_ROUTE_PATH_LOCAL,
Neale Ranns097fa662018-05-01 05:17:55 -0700148 .frp_mitf_flags = MFIB_ITF_FLAG_FORWARD,
Neale Ranns32e1c012016-11-22 17:07:28 +0000149 };
150
151 pool_get_aligned(ip6_main.mfibs, mfib_table, CLIB_CACHE_LINE_BYTES);
Dave Barachb7b92992018-10-17 10:38:51 -0400152 clib_memset(mfib_table, 0, sizeof(*mfib_table));
Neale Ranns32e1c012016-11-22 17:07:28 +0000153
154 mfib_table->mft_proto = FIB_PROTOCOL_IP6;
155 mfib_table->mft_index =
156 mfib_table->v6.index =
157 (mfib_table - ip6_main.mfibs);
158
159 hash_set (ip6_main.mfib_index_by_table_id,
160 table_id,
161 mfib_table->mft_index);
162
163 mfib_table->mft_table_id =
164 mfib_table->v6.table_id =
165 table_id;
166
Neale Ranns15002542017-09-10 04:39:11 -0700167 mfib_table_lock(mfib_table->mft_index, FIB_PROTOCOL_IP6, src);
Neale Ranns32e1c012016-11-22 17:07:28 +0000168
Neale Ranns32e1c012016-11-22 17:07:28 +0000169 /*
170 * add the special entries into the new FIB
171 */
172 mfib_table_entry_update(mfib_table->mft_index,
173 &all_zeros,
174 MFIB_SOURCE_DEFAULT_ROUTE,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800175 MFIB_RPF_ID_NONE,
Neale Ranns32e1c012016-11-22 17:07:28 +0000176 MFIB_ENTRY_FLAG_DROP);
177
178 /*
179 * Add each of the specials
180 */
181 FOR_EACH_IP6_SPECIAL(&pfx,
182 ({
183 mfib_table_entry_path_update(mfib_table->mft_index,
184 &pfx,
185 MFIB_SOURCE_SPECIAL,
Neale Ranns097fa662018-05-01 05:17:55 -0700186 &path_for_us);
Neale Ranns32e1c012016-11-22 17:07:28 +0000187 }));
188
189 return (mfib_table->mft_index);
190}
191
192void
193ip6_mfib_table_destroy (ip6_mfib_t *mfib)
194{
195 mfib_table_t *mfib_table = (mfib_table_t*)mfib;
196 fib_node_index_t mfei;
197 mfib_prefix_t pfx = {
198 .fp_proto = FIB_PROTOCOL_IP6,
199 };
200 const fib_route_path_t path_for_us = {
Neale Rannsda78f952017-05-24 09:15:43 -0700201 .frp_proto = DPO_PROTO_IP6,
Neale Ranns32e1c012016-11-22 17:07:28 +0000202 .frp_addr = zero_addr,
203 .frp_sw_if_index = 0xffffffff,
204 .frp_fib_index = ~0,
Neale Ranns097fa662018-05-01 05:17:55 -0700205 .frp_weight = 1,
Neale Ranns32e1c012016-11-22 17:07:28 +0000206 .frp_flags = FIB_ROUTE_PATH_LOCAL,
207 };
208
209 /*
210 * remove all the specials we added when the table was created.
211 */
212 FOR_EACH_IP6_SPECIAL(&pfx,
213 {
214 mfib_table_entry_path_remove(mfib_table->mft_index,
215 &pfx,
216 MFIB_SOURCE_SPECIAL,
217 &path_for_us);
218 });
219
220 mfei = mfib_table_lookup_exact_match(mfib_table->mft_index, &all_zeros);
221 mfib_table_entry_delete_index(mfei, MFIB_SOURCE_DEFAULT_ROUTE);
222
223 /*
224 * validate no more routes.
225 */
226 ASSERT(0 == mfib_table->mft_total_route_counts);
227 ASSERT(~0 != mfib_table->mft_table_id);
228
229 hash_unset (ip6_main.mfib_index_by_table_id, mfib_table->mft_table_id);
Neale Ranns32e1c012016-11-22 17:07:28 +0000230 pool_put(ip6_main.mfibs, mfib_table);
231}
232
233void
234ip6_mfib_interface_enable_disable (u32 sw_if_index, int is_enable)
235{
236 const fib_route_path_t path = {
Neale Rannsda78f952017-05-24 09:15:43 -0700237 .frp_proto = DPO_PROTO_IP6,
Neale Ranns32e1c012016-11-22 17:07:28 +0000238 .frp_addr = zero_addr,
239 .frp_sw_if_index = sw_if_index,
240 .frp_fib_index = ~0,
Neale Ranns097fa662018-05-01 05:17:55 -0700241 .frp_weight = 1,
242 .frp_mitf_flags = MFIB_ITF_FLAG_ACCEPT,
Neale Ranns32e1c012016-11-22 17:07:28 +0000243 };
244 mfib_prefix_t pfx = {
245 .fp_proto = FIB_PROTOCOL_IP6,
246 };
247 u32 mfib_index;
248
Neale Ranns32e1c012016-11-22 17:07:28 +0000249 mfib_index = ip6_mfib_table_get_index_for_sw_if_index(sw_if_index);
250
251 if (is_enable)
252 {
253 FOR_EACH_IP6_SPECIAL(&pfx,
254 {
255 mfib_table_entry_path_update(mfib_index,
256 &pfx,
257 MFIB_SOURCE_SPECIAL,
Neale Ranns097fa662018-05-01 05:17:55 -0700258 &path);
Neale Ranns32e1c012016-11-22 17:07:28 +0000259 });
260 }
261 else
262 {
263 FOR_EACH_IP6_SPECIAL(&pfx,
264 {
265 mfib_table_entry_path_remove(mfib_index,
266 &pfx,
267 MFIB_SOURCE_SPECIAL,
268 &path);
269 });
270 }
271}
272
273u32
Neale Ranns15002542017-09-10 04:39:11 -0700274ip6_mfib_table_find_or_create_and_lock (u32 table_id,
275 mfib_source_t src)
Neale Ranns32e1c012016-11-22 17:07:28 +0000276{
277 u32 index;
278
279 index = ip6_mfib_index_from_table_id(table_id);
280 if (~0 == index)
Neale Ranns15002542017-09-10 04:39:11 -0700281 return ip6_create_mfib_with_table_id(table_id, src);
282 mfib_table_lock(index, FIB_PROTOCOL_IP6, src);
Neale Ranns32e1c012016-11-22 17:07:28 +0000283
284 return (index);
285}
286
287u32
288ip6_mfib_table_get_index_for_sw_if_index (u32 sw_if_index)
289{
290 if (sw_if_index >= vec_len(ip6_main.mfib_index_by_sw_if_index))
291 {
292 /*
293 * This is the case for interfaces that are not yet mapped to
294 * a IP table
295 */
296 return (~0);
297 }
298 return (ip6_main.mfib_index_by_sw_if_index[sw_if_index]);
299}
300
Neale Rannsae809832018-11-23 09:00:27 -0800301#define IPV6_MFIB_GRP_LEN(_len) \
302 (_len > 128 ? 128 : _len)
Neale Ranns32e1c012016-11-22 17:07:28 +0000303
Neale Rannsae809832018-11-23 09:00:27 -0800304#define IP6_MFIB_MK_KEY(_mfib, _grp, _src, _len, _key) \
305{ \
306 _key.key[0] = (_grp->as_u64[0] & \
307 ip6_main.fib_masks[IPV6_MFIB_GRP_LEN(_len)].as_u64[0]); \
308 _key.key[1] = (_grp->as_u64[1] & \
309 ip6_main.fib_masks[IPV6_MFIB_GRP_LEN(_len)].as_u64[1]); \
310 if (_len == 256) { \
311 _key.key[2] = _src->as_u64[0]; \
312 _key.key[3] = _src->as_u64[1]; \
313 } else { \
314 _key.key[2] = 0; \
315 _key.key[3] = 0; \
316 } \
317 _key.key[4] = _mfib->index; \
318 _key.key[4] = (_key.key[4] << 32) | len; \
Neale Ranns32e1c012016-11-22 17:07:28 +0000319}
320
321/*
322 * ip6_fib_table_lookup_exact_match
323 *
324 * Exact match prefix lookup
325 */
326fib_node_index_t
327ip6_mfib_table_lookup_exact_match (const ip6_mfib_t *mfib,
328 const ip6_address_t *grp,
329 const ip6_address_t *src,
330 u32 len)
331{
Neale Rannsae809832018-11-23 09:00:27 -0800332 ip6_mfib_key_t key, value;
333 int rv;
Neale Ranns32e1c012016-11-22 17:07:28 +0000334
Neale Rannsae809832018-11-23 09:00:27 -0800335 IP6_MFIB_MK_KEY(mfib, grp, src, len, key);
Neale Ranns32e1c012016-11-22 17:07:28 +0000336
Neale Ranns5a59b2b2020-10-19 14:47:20 +0000337 rv = clib_bihash_search_inline_2_40_8(&ip6_mfib_table.ip6_mhash,
Neale Rannsae809832018-11-23 09:00:27 -0800338 &key, &value);
339 if (rv == 0)
340 return value.value;
Neale Ranns32e1c012016-11-22 17:07:28 +0000341
Neale Rannsae809832018-11-23 09:00:27 -0800342 return (FIB_NODE_INDEX_INVALID);
343}
344
345/*
346 * ip6_fib_table_lookup
347 *
348 * Longest prefix match for the forwarding plane (no mask given)
349 */
350fib_node_index_t
351ip6_mfib_table_fwd_lookup (const ip6_mfib_t *mfib,
352 const ip6_address_t *src,
353 const ip6_address_t *grp)
354{
355 ip6_mfib_table_instance_t *table;
356 ip6_mfib_key_t key, value;
357 int i, n, len;
358 int rv;
359
Neale Ranns5a59b2b2020-10-19 14:47:20 +0000360 table = &ip6_mfib_table;
Neale Rannsae809832018-11-23 09:00:27 -0800361 n = vec_len (table->prefix_lengths_in_search_order);
362
363 for (i = 0; i < n; i++)
Neale Ranns32e1c012016-11-22 17:07:28 +0000364 {
Neale Rannsae809832018-11-23 09:00:27 -0800365 len = table->prefix_lengths_in_search_order[i];
366
367 ASSERT(len >= 0 && len <= 256);
368 IP6_MFIB_MK_KEY(mfib, grp, src, len, key);
Neale Rannsae809832018-11-23 09:00:27 -0800369 rv = clib_bihash_search_inline_2_40_8(&table->ip6_mhash, &key, &value);
370 if (rv == 0)
371 return value.value;
Neale Ranns32e1c012016-11-22 17:07:28 +0000372 }
373
Neale Rannsae809832018-11-23 09:00:27 -0800374 return (FIB_NODE_INDEX_INVALID);
Neale Ranns32e1c012016-11-22 17:07:28 +0000375}
376
Neale Ranns9e829a82018-12-17 05:50:32 -0800377
378fib_node_index_t
379ip6_mfib_table_get_less_specific (const ip6_mfib_t *mfib,
380 const ip6_address_t *src,
381 const ip6_address_t *grp,
382 u32 len)
383{
384 u32 mask_len;
385
386 /*
387 * in the absence of a tree structure for the table that allows for an O(1)
388 * parent get, a cheeky way to find the cover is to LPM for the prefix with
389 * mask-1.
390 * there should always be a cover, though it may be the default route. the
391 * default route's cover is the default route.
392 */
393 if (len == 256)
394 {
395 /* go from (S,G) to (*,G*) */
396 mask_len = 128;
397 }
398 else if (len != 0)
399 {
400 mask_len = len - 1;
401 }
402 else
403 {
404 mask_len = len;
405 }
406
407 return (ip6_mfib_table_lookup(mfib, src, grp, mask_len));
408}
409
Neale Ranns32e1c012016-11-22 17:07:28 +0000410/*
411 * ip6_fib_table_lookup
412 *
413 * Longest prefix match
414 */
415fib_node_index_t
416ip6_mfib_table_lookup (const ip6_mfib_t *mfib,
417 const ip6_address_t *src,
418 const ip6_address_t *grp,
419 u32 len)
420{
Neale Rannsae809832018-11-23 09:00:27 -0800421 ip6_mfib_table_instance_t *table;
422 ip6_mfib_key_t key, value;
423 int i, n, rv;
Neale Ranns32e1c012016-11-22 17:07:28 +0000424
Neale Ranns5a59b2b2020-10-19 14:47:20 +0000425 table = &ip6_mfib_table;
Neale Rannsae809832018-11-23 09:00:27 -0800426 n = vec_len (table->prefix_lengths_in_search_order);
Neale Ranns32e1c012016-11-22 17:07:28 +0000427
Neale Rannsae809832018-11-23 09:00:27 -0800428 /*
429 * start search from a mask length same length or shorter.
430 * we don't want matches longer than the mask passed
431 */
432 i = 0;
433 while (i < n && table->prefix_lengths_in_search_order[i] > len)
434 {
435 i++;
436 }
Neale Ranns32e1c012016-11-22 17:07:28 +0000437
Neale Rannsae809832018-11-23 09:00:27 -0800438 for (; i < n; i++)
439 {
440 len = table->prefix_lengths_in_search_order[i];
Neale Ranns32e1c012016-11-22 17:07:28 +0000441
Neale Rannsb7778b62018-12-21 07:00:56 -0800442 ASSERT(len <= 256);
Neale Rannsae809832018-11-23 09:00:27 -0800443 IP6_MFIB_MK_KEY(mfib, grp, src, len, key);
444
445 rv = clib_bihash_search_inline_2_40_8(&table->ip6_mhash, &key, &value);
446 if (rv == 0)
447 return value.value;
448 }
449
450 return (FIB_NODE_INDEX_INVALID);
Neale Ranns32e1c012016-11-22 17:07:28 +0000451}
452
Neale Rannsae809832018-11-23 09:00:27 -0800453static void
454compute_prefix_lengths_in_search_order (ip6_mfib_table_instance_t *table)
Neale Ranns32e1c012016-11-22 17:07:28 +0000455{
Neale Rannsae809832018-11-23 09:00:27 -0800456 int i;
457 vec_reset_length (table->prefix_lengths_in_search_order);
458 /* Note: bitmap reversed so this is in fact a longest prefix match */
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100459 clib_bitmap_foreach (i, table->non_empty_dst_address_length_bitmap)
460 {
Neale Rannsae809832018-11-23 09:00:27 -0800461 vec_add1(table->prefix_lengths_in_search_order, (256 - i));
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100462 }
Neale Ranns32e1c012016-11-22 17:07:28 +0000463}
464
465void
466ip6_mfib_table_entry_insert (ip6_mfib_t *mfib,
467 const ip6_address_t *grp,
468 const ip6_address_t *src,
469 u32 len,
470 fib_node_index_t mfib_entry_index)
471{
Neale Rannsae809832018-11-23 09:00:27 -0800472 ip6_mfib_table_instance_t *table;
473 ip6_mfib_key_t key;
Neale Ranns32e1c012016-11-22 17:07:28 +0000474
Neale Ranns5a59b2b2020-10-19 14:47:20 +0000475 table = &ip6_mfib_table;
Neale Rannsae809832018-11-23 09:00:27 -0800476 IP6_MFIB_MK_KEY(mfib, grp, src, len, key);
477 key.value = mfib_entry_index;
Neale Ranns32e1c012016-11-22 17:07:28 +0000478
Neale Rannsae809832018-11-23 09:00:27 -0800479 clib_bihash_add_del_40_8(&table->ip6_mhash, &key, 1);
Neale Ranns32e1c012016-11-22 17:07:28 +0000480
Neale Rannsae809832018-11-23 09:00:27 -0800481 if (0 == table->dst_address_length_refcounts[len]++)
Neale Ranns32e1c012016-11-22 17:07:28 +0000482 {
Neale Rannsae809832018-11-23 09:00:27 -0800483 table->non_empty_dst_address_length_bitmap =
484 clib_bitmap_set (table->non_empty_dst_address_length_bitmap,
485 256 - len, 1);
486 compute_prefix_lengths_in_search_order (table);
Neale Ranns32e1c012016-11-22 17:07:28 +0000487 }
488}
489
490void
491ip6_mfib_table_entry_remove (ip6_mfib_t *mfib,
492 const ip6_address_t *grp,
493 const ip6_address_t *src,
494 u32 len)
495{
Neale Rannsae809832018-11-23 09:00:27 -0800496 ip6_mfib_table_instance_t *table;
Neale Ranns32e1c012016-11-22 17:07:28 +0000497 ip6_mfib_key_t key;
498
Neale Rannsae809832018-11-23 09:00:27 -0800499 IP6_MFIB_MK_KEY(mfib, grp, src, len, key);
Neale Ranns32e1c012016-11-22 17:07:28 +0000500
Neale Ranns5a59b2b2020-10-19 14:47:20 +0000501 table = &ip6_mfib_table;
Neale Rannsae809832018-11-23 09:00:27 -0800502 clib_bihash_add_del_40_8(&table->ip6_mhash, &key, 0);
Neale Ranns32e1c012016-11-22 17:07:28 +0000503
Neale Rannsae809832018-11-23 09:00:27 -0800504 ASSERT (table->dst_address_length_refcounts[len] > 0);
505 if (--table->dst_address_length_refcounts[len] == 0)
506 {
507 table->non_empty_dst_address_length_bitmap =
508 clib_bitmap_set (table->non_empty_dst_address_length_bitmap,
509 256 - len, 0);
510 compute_prefix_lengths_in_search_order (table);
511 }
Neale Ranns32e1c012016-11-22 17:07:28 +0000512}
513
514static clib_error_t *
515ip6_mfib_module_init (vlib_main_t * vm)
516{
517 return (NULL);
518}
519
520VLIB_INIT_FUNCTION(ip6_mfib_module_init);
521
Neale Rannsc87aafa2017-11-29 00:59:31 -0800522u8 *
523format_ip6_mfib_table_memory (u8 * s, va_list * args)
524{
Neale Ranns05cac302019-05-28 11:09:40 +0000525 u64 bytes_inuse;
526
Neale Ranns5a59b2b2020-10-19 14:47:20 +0000527 bytes_inuse = alloc_arena_next(&(ip6_mfib_table.ip6_mhash));
Neale Ranns05cac302019-05-28 11:09:40 +0000528
529 s = format(s, "%=30s %=6d %=12ld\n",
Neale Rannsc87aafa2017-11-29 00:59:31 -0800530 "IPv6 multicast",
Neale Ranns05cac302019-05-28 11:09:40 +0000531 pool_elts(ip6_main.mfibs),
532 bytes_inuse);
Neale Rannsc87aafa2017-11-29 00:59:31 -0800533
534 return (s);
535}
536
Neale Ranns32e1c012016-11-22 17:07:28 +0000537static void
538ip6_mfib_table_show_one (ip6_mfib_t *mfib,
539 vlib_main_t * vm,
540 ip6_address_t *src,
541 ip6_address_t *grp,
Neale Ranns9e829a82018-12-17 05:50:32 -0800542 u32 mask_len,
543 u32 cover)
Neale Ranns32e1c012016-11-22 17:07:28 +0000544{
Neale Ranns9e829a82018-12-17 05:50:32 -0800545 if (cover)
546 {
547 vlib_cli_output(vm, "%U",
548 format_mfib_entry,
549 ip6_mfib_table_get_less_specific(mfib, src, grp, mask_len),
550 MFIB_ENTRY_FORMAT_DETAIL);
551 }
552 else
553 {
554 vlib_cli_output(vm, "%U",
555 format_mfib_entry,
556 ip6_mfib_table_lookup(mfib, src, grp, mask_len),
557 MFIB_ENTRY_FORMAT_DETAIL);
558 }
Neale Ranns32e1c012016-11-22 17:07:28 +0000559}
560
561typedef struct ip6_mfib_show_ctx_t_ {
Neale Ranns32e1c012016-11-22 17:07:28 +0000562 fib_node_index_t *entries;
563} ip6_mfib_show_ctx_t;
564
565
Neale Ranns9db6ada2019-11-08 12:42:31 +0000566static walk_rc_t
Neale Ranns5a8123b2017-01-26 01:18:23 -0800567ip6_mfib_table_collect_entries (fib_node_index_t mfei, void *arg)
Neale Ranns32e1c012016-11-22 17:07:28 +0000568{
569 ip6_mfib_show_ctx_t *ctx = arg;
Neale Ranns32e1c012016-11-22 17:07:28 +0000570
Neale Ranns5a8123b2017-01-26 01:18:23 -0800571 vec_add1(ctx->entries, mfei);
Neale Ranns32e1c012016-11-22 17:07:28 +0000572
Neale Ranns9db6ada2019-11-08 12:42:31 +0000573 return (WALK_CONTINUE);
Neale Ranns32e1c012016-11-22 17:07:28 +0000574}
575
576static void
577ip6_mfib_table_show_all (ip6_mfib_t *mfib,
578 vlib_main_t * vm)
579{
580 fib_node_index_t *mfib_entry_index;
581 ip6_mfib_show_ctx_t ctx = {
Neale Ranns32e1c012016-11-22 17:07:28 +0000582 .entries = NULL,
583 };
584
Neale Ranns5a8123b2017-01-26 01:18:23 -0800585 ip6_mfib_table_walk(mfib,
586 ip6_mfib_table_collect_entries,
587 &ctx);
Neale Ranns32e1c012016-11-22 17:07:28 +0000588
589 vec_sort_with_function(ctx.entries, mfib_entry_cmp_for_sort);
590
591 vec_foreach(mfib_entry_index, ctx.entries)
592 {
593 vlib_cli_output(vm, "%U",
594 format_mfib_entry,
595 *mfib_entry_index,
596 MFIB_ENTRY_FORMAT_BRIEF);
597 }
598
599 vec_free(ctx.entries);
600}
601
Neale Rannsae809832018-11-23 09:00:27 -0800602/**
603 * @brief Context when walking the IPv6 table. Since all VRFs are in the
604 * same hash table, we need to filter only those we need as we walk
605 */
606typedef struct ip6_mfib_walk_ctx_t_
Neale Ranns5a8123b2017-01-26 01:18:23 -0800607{
Neale Rannsae809832018-11-23 09:00:27 -0800608 u32 i6w_mfib_index;
609 mfib_table_walk_fn_t i6w_fn;
610 void *i6w_ctx;
611} ip6_mfib_walk_ctx_t;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800612
Neale Rannsf50bac12019-12-06 05:53:17 +0000613static int
Neale Rannsae809832018-11-23 09:00:27 -0800614ip6_mfib_walk_cb (clib_bihash_kv_40_8_t * kvp,
615 void *arg)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800616{
Neale Rannsae809832018-11-23 09:00:27 -0800617 ip6_mfib_walk_ctx_t *ctx = arg;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800618
Neale Rannsae809832018-11-23 09:00:27 -0800619 if ((kvp->key[4] >> 32) == ctx->i6w_mfib_index)
620 {
Neale Ranns9db6ada2019-11-08 12:42:31 +0000621 ctx->i6w_fn(kvp->value, ctx->i6w_ctx);
Neale Rannsae809832018-11-23 09:00:27 -0800622 }
Neale Rannsf50bac12019-12-06 05:53:17 +0000623 return (BIHASH_WALK_CONTINUE);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800624}
625
626void
627ip6_mfib_table_walk (ip6_mfib_t *mfib,
628 mfib_table_walk_fn_t fn,
Neale Rannsae809832018-11-23 09:00:27 -0800629 void *arg)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800630{
Neale Rannsae809832018-11-23 09:00:27 -0800631 ip6_mfib_walk_ctx_t ctx = {
632 .i6w_mfib_index = mfib->index,
633 .i6w_fn = fn,
634 .i6w_ctx = arg,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800635 };
636
Neale Rannsae809832018-11-23 09:00:27 -0800637 clib_bihash_foreach_key_value_pair_40_8(
Neale Ranns5a59b2b2020-10-19 14:47:20 +0000638 &ip6_mfib_table.ip6_mhash,
Neale Rannsae809832018-11-23 09:00:27 -0800639 ip6_mfib_walk_cb,
640 &ctx);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800641}
642
Neale Ranns32e1c012016-11-22 17:07:28 +0000643static clib_error_t *
644ip6_show_mfib (vlib_main_t * vm,
645 unformat_input_t * input,
646 vlib_cli_command_t * cmd)
647{
Neale Rannsc87aafa2017-11-29 00:59:31 -0800648 ip6_main_t * im6 = &ip6_main;
Neale Ranns32e1c012016-11-22 17:07:28 +0000649 mfib_table_t *mfib_table;
650 int verbose, matching;
651 ip6_address_t grp, src = {{0}};
Neale Ranns9e829a82018-12-17 05:50:32 -0800652 u32 mask = 128, cover;
Neale Ranns32e1c012016-11-22 17:07:28 +0000653 int table_id = -1, fib_index = ~0;
654
655 verbose = 1;
656 matching = 0;
Neale Ranns9e829a82018-12-17 05:50:32 -0800657 cover = 0;
Neale Ranns32e1c012016-11-22 17:07:28 +0000658
659 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
660 {
661 if (unformat (input, "brief") || unformat (input, "summary")
662 || unformat (input, "sum"))
663 verbose = 0;
664
665 else if (unformat (input, "%U %U",
666 unformat_ip6_address, &src,
667 unformat_ip6_address, &grp))
668 {
669 matching = 1;
Neale Rannsc7409dc2017-05-19 02:54:32 -0700670 mask = 256;
671 }
672 else if (unformat (input, "%U/%d", unformat_ip6_address, &grp, &mask))
673 {
Dave Barachb7b92992018-10-17 10:38:51 -0400674 clib_memset(&src, 0, sizeof(src));
Neale Rannsc7409dc2017-05-19 02:54:32 -0700675 matching = 1;
Neale Ranns32e1c012016-11-22 17:07:28 +0000676 }
677 else if (unformat (input, "%U", unformat_ip6_address, &grp))
678 {
Dave Barachb7b92992018-10-17 10:38:51 -0400679 clib_memset(&src, 0, sizeof(src));
Neale Ranns32e1c012016-11-22 17:07:28 +0000680 matching = 1;
Neale Rannsc7409dc2017-05-19 02:54:32 -0700681 mask = 128;
Neale Ranns32e1c012016-11-22 17:07:28 +0000682 }
Neale Ranns32e1c012016-11-22 17:07:28 +0000683 else if (unformat (input, "table %d", &table_id))
684 ;
685 else if (unformat (input, "index %d", &fib_index))
686 ;
Neale Ranns9e829a82018-12-17 05:50:32 -0800687 else if (unformat (input, "cover"))
688 cover = 1;
Neale Ranns32e1c012016-11-22 17:07:28 +0000689 else
690 break;
691 }
692
Damjan Marionb2c31b62020-12-13 21:47:40 +0100693 pool_foreach (mfib_table, im6->mfibs)
694 {
Neale Ranns32e1c012016-11-22 17:07:28 +0000695 ip6_mfib_t *mfib = &mfib_table->v6;
696
697 if (table_id >= 0 && table_id != (int)mfib->table_id)
698 continue;
699 if (fib_index != ~0 && fib_index != (int)mfib->index)
700 continue;
701
702 vlib_cli_output (vm, "%U, fib_index %d",
703 format_mfib_table_name, mfib->index, FIB_PROTOCOL_IP6,
704 mfib->index);
705
706 /* Show summary? */
707 if (! verbose)
708 {
709 /* vlib_cli_output (vm, "%=20s%=16s", "Prefix length", "Count"); */
710 /* for (i = 0; i < ARRAY_LEN (mfib->fib_entry_by_dst_address); i++) */
711 /* { */
712 /* uword * hash = mfib->fib_entry_by_dst_address[i]; */
713 /* uword n_elts = hash_elts (hash); */
714 /* if (n_elts > 0) */
715 /* vlib_cli_output (vm, "%20d%16d", i, n_elts); */
716 /* } */
717 continue;
718 }
719
720 if (!matching)
721 {
722 ip6_mfib_table_show_all(mfib, vm);
723 }
724 else
725 {
Neale Ranns9e829a82018-12-17 05:50:32 -0800726 ip6_mfib_table_show_one(mfib, vm, &src, &grp, mask, cover);
Neale Ranns32e1c012016-11-22 17:07:28 +0000727 }
Damjan Marionb2c31b62020-12-13 21:47:40 +0100728 }
Neale Ranns32e1c012016-11-22 17:07:28 +0000729
730 return 0;
731}
732
Nathan Skrzypczakda331052021-09-29 15:28:26 +0200733/* clang-format off */
734/*?
Neale Rannsc87aafa2017-11-29 00:59:31 -0800735 * This command displays the IPv6 MulticasrFIB Tables (VRF Tables) and
Neale Ranns32e1c012016-11-22 17:07:28 +0000736 * the route entries for each table.
737 *
738 * @note This command will run for a long time when the FIB tables are
Nathan Skrzypczakda331052021-09-29 15:28:26 +0200739 * comprised of millions of entries. For those scenarios, consider displaying
Neale Ranns32e1c012016-11-22 17:07:28 +0000740 * a single table or summary mode.
741 *
742 * @cliexpar
Neale Rannsc87aafa2017-11-29 00:59:31 -0800743 * Example of how to display all the IPv6 Multicast FIB tables:
Neale Ranns32e1c012016-11-22 17:07:28 +0000744 * @cliexstart{show ip fib}
Neale Rannsc87aafa2017-11-29 00:59:31 -0800745 * ipv6-VRF:0, fib_index 0
Neale Ranns32e1c012016-11-22 17:07:28 +0000746 * (*, 0.0.0.0/0): flags:D,
747 * Interfaces:
748 * multicast-ip6-chain
749 * [@1]: dpo-drop ip6
750 * (*, 232.1.1.1/32):
751 * Interfaces:
752 * test-eth1: Forward,
753 * test-eth2: Forward,
754 * test-eth0: Accept,
755 * multicast-ip6-chain
756 * [@2]: dpo-replicate: [index:1 buckets:2 to:[0:0]]
Neale Rannsc87aafa2017-11-29 00:59:31 -0800757 * [0] [@1]: ipv6-mcast: test-eth1: IP6: d0:d1:d2:d3:d4:01 -> 01:00:05:00:00:00
758 * [1] [@1]: ipv6-mcast: test-eth2: IP6: d0:d1:d2:d3:d4:02 -> 01:00:05:00:00:00
Neale Ranns32e1c012016-11-22 17:07:28 +0000759 *
760 * @cliexend
Neale Rannsc87aafa2017-11-29 00:59:31 -0800761 * Example of how to display a summary of all IPv6 FIB tables:
Neale Ranns32e1c012016-11-22 17:07:28 +0000762 * @cliexstart{show ip fib summary}
Neale Rannsc87aafa2017-11-29 00:59:31 -0800763 * ipv6-VRF:0, fib_index 0, flow hash: src dst sport dport proto
Neale Ranns32e1c012016-11-22 17:07:28 +0000764 * Prefix length Count
765 * 0 1
766 * 8 2
767 * 32 4
Neale Rannsc87aafa2017-11-29 00:59:31 -0800768 * ipv6-VRF:7, fib_index 1, flow hash: src dst sport dport proto
Neale Ranns32e1c012016-11-22 17:07:28 +0000769 * Prefix length Count
770 * 0 1
771 * 8 2
772 * 24 2
773 * 32 4
774 * @cliexend
Nathan Skrzypczakda331052021-09-29 15:28:26 +0200775 ?*/
776/* clang-format on */
Neale Ranns32e1c012016-11-22 17:07:28 +0000777VLIB_CLI_COMMAND (ip6_show_fib_command, static) = {
778 .path = "show ip6 mfib",
779 .short_help = "show ip mfib [summary] [table <table-id>] [index <fib-id>] [<grp-addr>[/<mask>]] [<grp-addr>] [<src-addr> <grp-addr>]",
780 .function = ip6_show_mfib,
781};
Neale Ranns5a59b2b2020-10-19 14:47:20 +0000782
783static clib_error_t *
784ip6_mfib_init (vlib_main_t * vm)
785{
786 clib_bihash_init_40_8 (&ip6_mfib_table.ip6_mhash,
787 "ip6 mFIB table",
788 IP6_MFIB_DEFAULT_HASH_NUM_BUCKETS,
789 IP6_MFIB_DEFAULT_HASH_MEMORY_SIZE);
790
791 return (NULL);
792}
793
794VLIB_INIT_FUNCTION (ip6_mfib_init) =
795{
796 .runs_before = VLIB_INITS("ip6_lookup_init"),
797};