blob: 331f3f34dcc72f24e161e93ece3a4ab6a97a2977 [file] [log] [blame]
Neale Ranns53da2212018-02-24 02:11:19 -08001/*
2 * Copyright (c) 2018 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 <vlib/vlib.h>
17#include <vnet/dpo/drop_dpo.h>
18#include <vnet/fib/ip6_fib.h>
19
20#include <vnet/ip/ip6_ll_table.h>
21
22/**
23 * There's only one IP6 link local table
24 */
25static ip6_ll_table_t ip6_ll_table;
26
27u32
28ip6_ll_fib_get (u32 sw_if_index)
29{
30 ASSERT (vec_len (ip6_ll_table.ilt_fibs) > sw_if_index);
31
32 return (ip6_ll_table.ilt_fibs[sw_if_index]);
33}
34
35fib_node_index_t
36ip6_ll_table_lookup (const ip6_ll_prefix_t * prefix)
37{
38 return (ip6_fib_table_lookup (ip6_ll_fib_get (prefix->ilp_sw_if_index),
39 &prefix->ilp_addr, 128));
40}
41
42fib_node_index_t
43ip6_ll_table_lookup_exact_match (const ip6_ll_prefix_t * prefix)
44{
45 return (ip6_fib_table_lookup_exact_match
46 (ip6_ll_fib_get (prefix->ilp_sw_if_index), &prefix->ilp_addr, 128));
47}
48
49static void
50ip6_ll_fib_create (u32 sw_if_index)
51{
52 vnet_main_t *vnm = vnet_get_main ();
53 u8 *desc;
54
Vladislav Grishenkofb9d1ac2022-08-04 20:36:45 +050055 desc = format (NULL, "IP6-link-local:%U", format_vnet_sw_if_index_name, vnm,
56 sw_if_index);
Neale Ranns53da2212018-02-24 02:11:19 -080057
58 ip6_ll_table.ilt_fibs[sw_if_index] =
59 ip6_fib_table_create_and_lock (FIB_SOURCE_IP6_ND,
60 FIB_TABLE_FLAG_IP6_LL, desc);
61
62 /*
63 * leave the default route as a drop, but fix fe::/10 to be a glean
64 * via the interface.
65 */
66 /* *INDENT-OFF* */
67 fib_prefix_t pfx = {
68 .fp_proto = FIB_PROTOCOL_IP6,
69 .fp_len = 10,
70 .fp_addr = {
71 .ip6 = {
72 .as_u8 = {
73 [0] = 0xFE,
74 [1] = 0x80,
75 }
76 },
77 }
78 };
79 fib_table_entry_update_one_path(
80 ip6_ll_table.ilt_fibs[sw_if_index],
81 &pfx,
82 FIB_SOURCE_SPECIAL,
83 (FIB_ENTRY_FLAG_ATTACHED |
84 FIB_ENTRY_FLAG_NO_ATTACHED_EXPORT),
85 DPO_PROTO_IP6,
86 NULL,
87 sw_if_index,
88 ~0,
89 1,
90 NULL,
91 FIB_ROUTE_PATH_FLAG_NONE);
92 /* *INDENT-ON* */
93}
94
95static void
96ip6_ll_prefix_to_fib (const ip6_ll_prefix_t * ilp, fib_prefix_t * fp)
97{
98 fp->fp_proto = FIB_PROTOCOL_IP6;
99 fp->fp_len = 128;
100 fp->fp_addr.ip6 = ilp->ilp_addr;
Neale Ranns39a233a2020-02-20 13:13:45 +0000101 fp->___fp___pad = 0;
Neale Ranns53da2212018-02-24 02:11:19 -0800102}
103
104fib_node_index_t
105ip6_ll_table_entry_update (const ip6_ll_prefix_t * ilp,
106 fib_route_path_flags_t flags)
107{
108 fib_node_index_t ip6_ll_entry_index;
109 fib_route_path_t *rpaths, rpath = {
110 .frp_flags = flags,
111 .frp_sw_if_index = ilp->ilp_sw_if_index,
112 .frp_proto = DPO_PROTO_IP6,
Vladislav Grishenko1fb62c02022-05-16 01:44:43 +0500113 .frp_fib_index = ~0,
114 .frp_weight = 1,
Neale Ranns53da2212018-02-24 02:11:19 -0800115 };
Vladislav Grishenko1fb62c02022-05-16 01:44:43 +0500116 fib_prefix_t fp = { 0 };
117
118 if (flags & FIB_ROUTE_PATH_LOCAL)
119 rpath.frp_addr.ip6 = ilp->ilp_addr;
Neale Ranns53da2212018-02-24 02:11:19 -0800120
Vladislav Grishenkob9feb612021-11-19 22:53:41 +0500121 vec_validate_init_empty (ip6_ll_table.ilt_fibs, ilp->ilp_sw_if_index, ~0);
Neale Ranns53da2212018-02-24 02:11:19 -0800122
Vladislav Grishenkob9feb612021-11-19 22:53:41 +0500123 if (~0 == ip6_ll_fib_get (ilp->ilp_sw_if_index))
Neale Ranns53da2212018-02-24 02:11:19 -0800124 {
125 ip6_ll_fib_create (ilp->ilp_sw_if_index);
126 }
127
128 rpaths = NULL;
129 vec_add1 (rpaths, rpath);
130
131 ip6_ll_prefix_to_fib (ilp, &fp);
132 ip6_ll_entry_index =
133 fib_table_entry_update (ip6_ll_fib_get (ilp->ilp_sw_if_index), &fp,
134 FIB_SOURCE_IP6_ND,
135 (flags & FIB_ROUTE_PATH_LOCAL ?
136 FIB_ENTRY_FLAG_LOCAL : FIB_ENTRY_FLAG_NONE),
137 rpaths);
138 vec_free (rpaths);
139
140 return (ip6_ll_entry_index);
141}
142
143void
144ip6_ll_table_entry_delete (const ip6_ll_prefix_t * ilp)
145{
146 fib_node_index_t ip6_ll_entry_index;
147 u32 fib_index;
148
149 ip6_ll_entry_index = ip6_ll_table_lookup_exact_match (ilp);
150
151 if (FIB_NODE_INDEX_INVALID != ip6_ll_entry_index)
152 fib_table_entry_delete_index (ip6_ll_entry_index, FIB_SOURCE_IP6_ND);
153
154 /*
155 * if there are no ND sourced prefixes left, then we can clean up this FIB
156 */
157 fib_index = ip6_ll_fib_get (ilp->ilp_sw_if_index);
Vladislav Grishenkob9feb612021-11-19 22:53:41 +0500158 if (~0 != fib_index &&
159 0 == fib_table_get_num_entries (fib_index, FIB_PROTOCOL_IP6,
160 FIB_SOURCE_IP6_ND))
Neale Ranns53da2212018-02-24 02:11:19 -0800161 {
162 fib_table_unlock (fib_index, FIB_PROTOCOL_IP6, FIB_SOURCE_IP6_ND);
Vladislav Grishenkob9feb612021-11-19 22:53:41 +0500163 ip6_ll_table.ilt_fibs[ilp->ilp_sw_if_index] = ~0;
Neale Ranns53da2212018-02-24 02:11:19 -0800164 }
165}
166
167static void
168ip6_ll_table_show_one (vlib_main_t * vm, ip6_ll_prefix_t * ilp, int detail)
169{
170 vlib_cli_output (vm, "%U",
171 format_fib_entry,
172 ip6_ll_table_lookup (ilp),
173 (detail ?
174 FIB_ENTRY_FORMAT_DETAIL2 : FIB_ENTRY_FORMAT_DETAIL));
175}
176
177typedef struct ip6_ll_show_ctx_t_
178{
179 fib_node_index_t *entries;
180} ip6_ll_show_ctx_t;
181
182static fib_table_walk_rc_t
183ip6_ll_table_show_walk (fib_node_index_t fib_entry_index, void *arg)
184{
185 ip6_ll_show_ctx_t *ctx = arg;
186
187 vec_add1 (ctx->entries, fib_entry_index);
188
189 return (FIB_TABLE_WALK_CONTINUE);
190}
191
192static void
193ip6_ll_table_show_all (vlib_main_t * vm, u32 fib_index)
194{
195 fib_node_index_t *fib_entry_index;
196 ip6_ll_show_ctx_t ctx = {
197 .entries = NULL,
198 };
199
200 fib_table_walk (fib_index, FIB_PROTOCOL_IP6, ip6_ll_table_show_walk, &ctx);
201 vec_sort_with_function (ctx.entries, fib_entry_cmp_for_sort);
202
203 vec_foreach (fib_entry_index, ctx.entries)
204 {
205 vlib_cli_output (vm, "%U",
206 format_fib_entry,
207 *fib_entry_index, FIB_ENTRY_FORMAT_BRIEF);
208 }
209
210 vec_free (ctx.entries);
211}
212
213typedef struct
214{
215 u32 fib_index;
216 u64 count_by_prefix_length[129];
217} count_routes_in_fib_at_prefix_length_arg_t;
218
Neale Rannsf50bac12019-12-06 05:53:17 +0000219static int
220count_routes_in_fib_at_prefix_length (clib_bihash_kv_24_8_t * kvp, void *arg)
Neale Ranns53da2212018-02-24 02:11:19 -0800221{
222 count_routes_in_fib_at_prefix_length_arg_t *ap = arg;
223 int mask_width;
224
225 if ((kvp->key[2] >> 32) != ap->fib_index)
Neale Rannsf50bac12019-12-06 05:53:17 +0000226 return (BIHASH_WALK_CONTINUE);
Neale Ranns53da2212018-02-24 02:11:19 -0800227
228 mask_width = kvp->key[2] & 0xFF;
229
230 ap->count_by_prefix_length[mask_width]++;
Neale Rannsf50bac12019-12-06 05:53:17 +0000231
232 return (BIHASH_WALK_CONTINUE);
Neale Ranns53da2212018-02-24 02:11:19 -0800233}
234
235static clib_error_t *
236ip6_ll_show_fib (vlib_main_t * vm,
237 unformat_input_t * input, vlib_cli_command_t * cmd)
238{
239 count_routes_in_fib_at_prefix_length_arg_t _ca, *ca = &_ca;
Neale Ranns53da2212018-02-24 02:11:19 -0800240 fib_table_t *fib_table;
241 int verbose, matching;
242 ip6_address_t matching_address;
243 u32 mask_len = 128;
244 u32 sw_if_index = ~0;
245 int detail = 0;
246 vnet_main_t *vnm = vnet_get_main ();
247 u32 fib_index;
248
249 verbose = 1;
250 matching = 0;
251
252 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
253 {
254 if (unformat (input, "brief") ||
255 unformat (input, "summary") || unformat (input, "sum"))
256 verbose = 0;
257
258 else if (unformat (input, "detail") || unformat (input, "det"))
259 detail = 1;
260
261 else if (unformat (input, "%U/%d",
262 unformat_ip6_address, &matching_address, &mask_len))
263 matching = 1;
264
265 else
266 if (unformat (input, "%U", unformat_ip6_address, &matching_address))
267 matching = 1;
268 else if (unformat (input, "%U",
269 unformat_vnet_sw_interface, vnm, &sw_if_index))
270 ;
271 else
272 break;
273 }
274
275 vec_foreach_index (sw_if_index, ip6_ll_table.ilt_fibs)
276 {
277 fib_source_t source;
278 u8 *s = NULL;
279
280 fib_index = ip6_ll_table.ilt_fibs[sw_if_index];
Vladislav Grishenkob9feb612021-11-19 22:53:41 +0500281 if (~0 == fib_index)
Neale Ranns53da2212018-02-24 02:11:19 -0800282 continue;
283
284 fib_table = fib_table_get (fib_index, FIB_PROTOCOL_IP6);
285
286 if (!(fib_table->ft_flags & FIB_TABLE_FLAG_IP6_LL))
287 continue;
288
289 s = format (s, "%U, fib_index:%d, locks:[",
290 format_fib_table_name, fib_index,
291 FIB_PROTOCOL_IP6, fib_index);
Neale Ranns3bab8f92019-12-04 06:11:00 +0000292 vec_foreach_index (source, fib_table->ft_locks)
Neale Ranns53da2212018-02-24 02:11:19 -0800293 {
294 if (0 != fib_table->ft_locks[source])
295 {
296 s = format (s, "%U:%d, ",
297 format_fib_source, source, fib_table->ft_locks[source]);
298 }
299 }
300 s = format (s, "]");
301 vlib_cli_output (vm, "%v", s);
302 vec_free (s);
303
304 /* Show summary? */
305 if (!verbose)
306 {
Neale Rannsae809832018-11-23 09:00:27 -0800307 clib_bihash_24_8_t *h =
Neale Ranns5a59b2b2020-10-19 14:47:20 +0000308 &ip6_fib_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash;
Neale Ranns53da2212018-02-24 02:11:19 -0800309 int len;
310
311 vlib_cli_output (vm, "%=20s%=16s", "Prefix length", "Count");
312
Dave Barachb7b92992018-10-17 10:38:51 -0400313 clib_memset (ca, 0, sizeof (*ca));
Neale Ranns53da2212018-02-24 02:11:19 -0800314 ca->fib_index = fib_index;
315
Neale Rannsae809832018-11-23 09:00:27 -0800316 clib_bihash_foreach_key_value_pair_24_8
Neale Ranns53da2212018-02-24 02:11:19 -0800317 (h, count_routes_in_fib_at_prefix_length, ca);
318
319 for (len = 128; len >= 0; len--)
320 {
321 if (ca->count_by_prefix_length[len])
322 vlib_cli_output (vm, "%=20d%=16lld",
323 len, ca->count_by_prefix_length[len]);
324 }
325 continue;
326 }
327
328 if (!matching)
329 {
330 ip6_ll_table_show_all (vm, fib_index);
331 }
332 else
333 {
334 if (~0 == sw_if_index)
335 {
336 vlib_cli_output (vm, "specify the interface");
337 }
338 else
339 {
340 ip6_ll_prefix_t ilp = {
341 .ilp_addr = matching_address,
342 .ilp_sw_if_index = sw_if_index,
343 };
344 ip6_ll_table_show_one (vm, &ilp, detail);
345 }
346 }
347 };
348
349 return 0;
350}
351
352/* *INDENT-OFF* */
353VLIB_CLI_COMMAND (ip6_show_fib_command, static) = {
354 .path = "show ip6-ll",
355 .short_help = "show ip6-ll [summary] [interface] [<ip6-addr>[/<width>]] [detail]",
356 .function = ip6_ll_show_fib,
357};
358/* *INDENT-ON* */
359
360static clib_error_t *
Vladislav Grishenkob9feb612021-11-19 22:53:41 +0500361ip6_ll_sw_interface_add_del (vnet_main_t *vnm, u32 sw_if_index, u32 is_add)
362{
363 vec_validate_init_empty (ip6_ll_table.ilt_fibs, sw_if_index, ~0);
364
365 return (NULL);
366}
367
368VNET_SW_INTERFACE_ADD_DEL_FUNCTION (ip6_ll_sw_interface_add_del);
369
370static clib_error_t *
Neale Ranns53da2212018-02-24 02:11:19 -0800371ip6_ll_module_init (vlib_main_t * vm)
372{
373 clib_error_t *error;
374
375 error = vlib_call_init_function (vm, ip6_lookup_init);
376
377 return (error);
378}
379
380VLIB_INIT_FUNCTION (ip6_ll_module_init);
381
382/*
383 * fd.io coding-style-patch-verification: ON
384 *
385 * Local Variables:
386 * eval: (c-set-style "gnu")
387 * End:
388 */