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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <vlib/vlib.h>
17 #include <vnet/dpo/drop_dpo.h>
18 #include <vnet/fib/ip6_fib.h>
20 #include <vnet/ip/ip6_ll_table.h>
23 * There's only one IP6 link local table
25 static ip6_ll_table_t ip6_ll_table;
28 ip6_ll_fib_get (u32 sw_if_index)
30 ASSERT (vec_len (ip6_ll_table.ilt_fibs) > sw_if_index);
32 return (ip6_ll_table.ilt_fibs[sw_if_index]);
36 ip6_ll_table_lookup (const ip6_ll_prefix_t * prefix)
38 return (ip6_fib_table_lookup (ip6_ll_fib_get (prefix->ilp_sw_if_index),
39 &prefix->ilp_addr, 128));
43 ip6_ll_table_lookup_exact_match (const ip6_ll_prefix_t * prefix)
45 return (ip6_fib_table_lookup_exact_match
46 (ip6_ll_fib_get (prefix->ilp_sw_if_index), &prefix->ilp_addr, 128));
50 ip6_ll_fib_create (u32 sw_if_index)
52 vnet_main_t *vnm = vnet_get_main ();
55 desc = format (NULL, "IP6-link-local:%U", format_vnet_sw_if_index_name, vnm,
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);
63 * leave the default route as a drop, but fix fe::/10 to be a glean
67 .fp_proto = FIB_PROTOCOL_IP6,
78 fib_table_entry_update_one_path(
79 ip6_ll_table.ilt_fibs[sw_if_index],
82 (FIB_ENTRY_FLAG_ATTACHED |
83 FIB_ENTRY_FLAG_NO_ATTACHED_EXPORT),
90 FIB_ROUTE_PATH_FLAG_NONE);
94 ip6_ll_prefix_to_fib (const ip6_ll_prefix_t * ilp, fib_prefix_t * fp)
96 fp->fp_proto = FIB_PROTOCOL_IP6;
98 fp->fp_addr.ip6 = ilp->ilp_addr;
103 ip6_ll_table_entry_update (const ip6_ll_prefix_t * ilp,
104 fib_route_path_flags_t flags)
106 fib_node_index_t ip6_ll_entry_index;
107 fib_route_path_t *rpaths, rpath = {
109 .frp_sw_if_index = ilp->ilp_sw_if_index,
110 .frp_proto = DPO_PROTO_IP6,
114 fib_prefix_t fp = { 0 };
116 if (flags & FIB_ROUTE_PATH_LOCAL)
117 rpath.frp_addr.ip6 = ilp->ilp_addr;
119 vec_validate_init_empty (ip6_ll_table.ilt_fibs, ilp->ilp_sw_if_index, ~0);
121 if (~0 == ip6_ll_fib_get (ilp->ilp_sw_if_index))
123 ip6_ll_fib_create (ilp->ilp_sw_if_index);
127 vec_add1 (rpaths, rpath);
129 ip6_ll_prefix_to_fib (ilp, &fp);
131 fib_table_entry_update (ip6_ll_fib_get (ilp->ilp_sw_if_index), &fp,
133 (flags & FIB_ROUTE_PATH_LOCAL ?
134 FIB_ENTRY_FLAG_LOCAL : FIB_ENTRY_FLAG_NONE),
138 return (ip6_ll_entry_index);
142 ip6_ll_table_entry_delete (const ip6_ll_prefix_t * ilp)
144 fib_node_index_t ip6_ll_entry_index;
147 ip6_ll_entry_index = ip6_ll_table_lookup_exact_match (ilp);
149 if (FIB_NODE_INDEX_INVALID != ip6_ll_entry_index)
150 fib_table_entry_delete_index (ip6_ll_entry_index, FIB_SOURCE_IP6_ND);
153 * if there are no ND sourced prefixes left, then we can clean up this FIB
155 fib_index = ip6_ll_fib_get (ilp->ilp_sw_if_index);
156 if (~0 != fib_index &&
157 0 == fib_table_get_num_entries (fib_index, FIB_PROTOCOL_IP6,
160 fib_table_unlock (fib_index, FIB_PROTOCOL_IP6, FIB_SOURCE_IP6_ND);
161 ip6_ll_table.ilt_fibs[ilp->ilp_sw_if_index] = ~0;
166 ip6_ll_table_show_one (vlib_main_t * vm, ip6_ll_prefix_t * ilp, int detail)
168 vlib_cli_output (vm, "%U",
170 ip6_ll_table_lookup (ilp),
172 FIB_ENTRY_FORMAT_DETAIL2 : FIB_ENTRY_FORMAT_DETAIL));
175 typedef struct ip6_ll_show_ctx_t_
177 fib_node_index_t *entries;
180 static fib_table_walk_rc_t
181 ip6_ll_table_show_walk (fib_node_index_t fib_entry_index, void *arg)
183 ip6_ll_show_ctx_t *ctx = arg;
185 vec_add1 (ctx->entries, fib_entry_index);
187 return (FIB_TABLE_WALK_CONTINUE);
191 ip6_ll_table_show_all (vlib_main_t * vm, u32 fib_index)
193 fib_node_index_t *fib_entry_index;
194 ip6_ll_show_ctx_t ctx = {
198 fib_table_walk (fib_index, FIB_PROTOCOL_IP6, ip6_ll_table_show_walk, &ctx);
199 vec_sort_with_function (ctx.entries, fib_entry_cmp_for_sort);
201 vec_foreach (fib_entry_index, ctx.entries)
203 vlib_cli_output (vm, "%U",
205 *fib_entry_index, FIB_ENTRY_FORMAT_BRIEF);
208 vec_free (ctx.entries);
214 u64 count_by_prefix_length[129];
215 } count_routes_in_fib_at_prefix_length_arg_t;
218 count_routes_in_fib_at_prefix_length (clib_bihash_kv_24_8_t * kvp, void *arg)
220 count_routes_in_fib_at_prefix_length_arg_t *ap = arg;
223 if ((kvp->key[2] >> 32) != ap->fib_index)
224 return (BIHASH_WALK_CONTINUE);
226 mask_width = kvp->key[2] & 0xFF;
228 ap->count_by_prefix_length[mask_width]++;
230 return (BIHASH_WALK_CONTINUE);
233 static clib_error_t *
234 ip6_ll_show_fib (vlib_main_t * vm,
235 unformat_input_t * input, vlib_cli_command_t * cmd)
237 count_routes_in_fib_at_prefix_length_arg_t _ca, *ca = &_ca;
238 fib_table_t *fib_table;
239 int verbose, matching;
240 ip6_address_t matching_address;
242 u32 sw_if_index = ~0;
244 vnet_main_t *vnm = vnet_get_main ();
250 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
252 if (unformat (input, "brief") ||
253 unformat (input, "summary") || unformat (input, "sum"))
256 else if (unformat (input, "detail") || unformat (input, "det"))
259 else if (unformat (input, "%U/%d",
260 unformat_ip6_address, &matching_address, &mask_len))
264 if (unformat (input, "%U", unformat_ip6_address, &matching_address))
266 else if (unformat (input, "%U",
267 unformat_vnet_sw_interface, vnm, &sw_if_index))
273 vec_foreach_index (sw_if_index, ip6_ll_table.ilt_fibs)
278 fib_index = ip6_ll_table.ilt_fibs[sw_if_index];
282 fib_table = fib_table_get (fib_index, FIB_PROTOCOL_IP6);
284 if (!(fib_table->ft_flags & FIB_TABLE_FLAG_IP6_LL))
287 s = format (s, "%U, fib_index:%d, locks:[",
288 format_fib_table_name, fib_index,
289 FIB_PROTOCOL_IP6, fib_index);
290 vec_foreach_index (source, fib_table->ft_locks)
292 if (0 != fib_table->ft_locks[source])
294 s = format (s, "%U:%d, ",
295 format_fib_source, source, fib_table->ft_locks[source]);
299 vlib_cli_output (vm, "%v", s);
305 clib_bihash_24_8_t *h =
306 &ip6_fib_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash;
309 vlib_cli_output (vm, "%=20s%=16s", "Prefix length", "Count");
311 clib_memset (ca, 0, sizeof (*ca));
312 ca->fib_index = fib_index;
314 clib_bihash_foreach_key_value_pair_24_8
315 (h, count_routes_in_fib_at_prefix_length, ca);
317 for (len = 128; len >= 0; len--)
319 if (ca->count_by_prefix_length[len])
320 vlib_cli_output (vm, "%=20d%=16lld",
321 len, ca->count_by_prefix_length[len]);
328 ip6_ll_table_show_all (vm, fib_index);
332 if (~0 == sw_if_index)
334 vlib_cli_output (vm, "specify the interface");
338 ip6_ll_prefix_t ilp = {
339 .ilp_addr = matching_address,
340 .ilp_sw_if_index = sw_if_index,
342 ip6_ll_table_show_one (vm, &ilp, detail);
350 VLIB_CLI_COMMAND (ip6_show_fib_command, static) = {
351 .path = "show ip6-ll",
352 .short_help = "show ip6-ll [summary] [interface] [<ip6-addr>[/<width>]] [detail]",
353 .function = ip6_ll_show_fib,
356 static clib_error_t *
357 ip6_ll_sw_interface_add_del (vnet_main_t *vnm, u32 sw_if_index, u32 is_add)
359 vec_validate_init_empty (ip6_ll_table.ilt_fibs, sw_if_index, ~0);
364 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (ip6_ll_sw_interface_add_del);
366 static clib_error_t *
367 ip6_ll_module_init (vlib_main_t * vm)
371 error = vlib_call_init_function (vm, ip6_lookup_init);
376 VLIB_INIT_FUNCTION (ip6_ll_module_init);
379 * fd.io coding-style-patch-verification: ON
382 * eval: (c-set-style "gnu")