Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 1 | /* |
| 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/fib_table.h> |
| 17 | #include <vnet/fib/fib_entry.h> |
| 18 | #include <vnet/fib/ip4_fib.h> |
| 19 | |
| 20 | /* |
Lijian.Zhang | 33af8c1 | 2019-09-16 16:22:36 +0800 | [diff] [blame] | 21 | * A table of prefixes to be added to tables and the sources for them |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 22 | */ |
| 23 | typedef struct ip4_fib_table_special_prefix_t_ { |
| 24 | fib_prefix_t ift_prefix; |
| 25 | fib_source_t ift_source; |
| 26 | fib_entry_flag_t ift_flag; |
| 27 | } ip4_fib_table_special_prefix_t; |
| 28 | |
| 29 | static const ip4_fib_table_special_prefix_t ip4_specials[] = { |
| 30 | { |
| 31 | /* 0.0.0.0/0*/ |
| 32 | .ift_prefix = { |
| 33 | .fp_addr = { |
| 34 | .ip4.data_u32 = 0, |
| 35 | }, |
| 36 | .fp_len = 0, |
| 37 | .fp_proto = FIB_PROTOCOL_IP4, |
| 38 | }, |
| 39 | .ift_source = FIB_SOURCE_DEFAULT_ROUTE, |
| 40 | .ift_flag = FIB_ENTRY_FLAG_DROP, |
| 41 | }, |
| 42 | { |
| 43 | /* 0.0.0.0/32*/ |
| 44 | .ift_prefix = { |
| 45 | .fp_addr = { |
| 46 | .ip4.data_u32 = 0, |
| 47 | }, |
| 48 | .fp_len = 32, |
| 49 | .fp_proto = FIB_PROTOCOL_IP4, |
| 50 | }, |
| 51 | .ift_source = FIB_SOURCE_DEFAULT_ROUTE, |
| 52 | .ift_flag = FIB_ENTRY_FLAG_DROP, |
| 53 | }, |
| 54 | { |
| 55 | /* |
Neale Ranns | 86fb04d | 2016-12-01 17:03:25 +0000 | [diff] [blame] | 56 | * 240.0.0.0/4 |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 57 | * drop class E |
| 58 | */ |
| 59 | .ift_prefix = { |
| 60 | .fp_addr = { |
| 61 | .ip4.data_u32 = 0xf0000000, |
| 62 | }, |
Neale Ranns | 86fb04d | 2016-12-01 17:03:25 +0000 | [diff] [blame] | 63 | .fp_len = 4, |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 64 | .fp_proto = FIB_PROTOCOL_IP4, |
| 65 | }, |
| 66 | .ift_source = FIB_SOURCE_SPECIAL, |
| 67 | .ift_flag = FIB_ENTRY_FLAG_DROP, |
| 68 | |
| 69 | }, |
| 70 | { |
| 71 | /* |
Neale Ranns | 86fb04d | 2016-12-01 17:03:25 +0000 | [diff] [blame] | 72 | * 224.0.0.0/4 |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 73 | * drop all mcast |
| 74 | */ |
| 75 | .ift_prefix = { |
| 76 | .fp_addr = { |
| 77 | .ip4.data_u32 = 0xe0000000, |
| 78 | }, |
Neale Ranns | 86fb04d | 2016-12-01 17:03:25 +0000 | [diff] [blame] | 79 | .fp_len = 4, |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 80 | .fp_proto = FIB_PROTOCOL_IP4, |
| 81 | }, |
| 82 | .ift_source = FIB_SOURCE_SPECIAL, |
| 83 | .ift_flag = FIB_ENTRY_FLAG_DROP, |
| 84 | }, |
| 85 | { |
| 86 | /* |
| 87 | * 255.255.255.255/32 |
| 88 | * drop, but we'll allow it to be usurped by the likes of DHCP |
| 89 | */ |
| 90 | .ift_prefix = { |
| 91 | .fp_addr = { |
| 92 | .ip4.data_u32 = 0xffffffff, |
| 93 | }, |
| 94 | .fp_len = 32, |
| 95 | .fp_proto = FIB_PROTOCOL_IP4, |
| 96 | }, |
| 97 | .ift_source = FIB_SOURCE_DEFAULT_ROUTE, |
| 98 | .ift_flag = FIB_ENTRY_FLAG_DROP, |
| 99 | } |
| 100 | }; |
| 101 | |
Neale Ranns | d695333 | 2021-08-10 07:39:18 +0000 | [diff] [blame] | 102 | void |
| 103 | ip4_fib_hash_load_specials (u32 fib_index) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 104 | { |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 105 | /* |
| 106 | * add the special entries into the new FIB |
| 107 | */ |
| 108 | int ii; |
| 109 | |
| 110 | for (ii = 0; ii < ARRAY_LEN(ip4_specials); ii++) |
| 111 | { |
| 112 | fib_prefix_t prefix = ip4_specials[ii].ift_prefix; |
| 113 | |
| 114 | prefix.fp_addr.ip4.data_u32 = |
| 115 | clib_host_to_net_u32(prefix.fp_addr.ip4.data_u32); |
| 116 | |
Neale Ranns | d695333 | 2021-08-10 07:39:18 +0000 | [diff] [blame] | 117 | fib_table_entry_special_add(fib_index, |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 118 | &prefix, |
| 119 | ip4_specials[ii].ift_source, |
Neale Ranns | a055830 | 2017-04-13 00:44:52 -0700 | [diff] [blame] | 120 | ip4_specials[ii].ift_flag); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 121 | } |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | void |
Neale Ranns | d695333 | 2021-08-10 07:39:18 +0000 | [diff] [blame] | 125 | ip4_fib_hash_flush_specials (u32 fib_index) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 126 | { |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 127 | int ii; |
| 128 | |
| 129 | /* |
| 130 | * remove all the specials we added when the table was created. |
Neale Ranns | 04a75e3 | 2017-03-23 06:46:01 -0700 | [diff] [blame] | 131 | * In reverse order so the default route is last. |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 132 | */ |
Neale Ranns | 04a75e3 | 2017-03-23 06:46:01 -0700 | [diff] [blame] | 133 | for (ii = ARRAY_LEN(ip4_specials) - 1; ii >= 0; ii--) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 134 | { |
| 135 | fib_prefix_t prefix = ip4_specials[ii].ift_prefix; |
| 136 | |
| 137 | prefix.fp_addr.ip4.data_u32 = |
| 138 | clib_host_to_net_u32(prefix.fp_addr.ip4.data_u32); |
| 139 | |
Neale Ranns | d695333 | 2021-08-10 07:39:18 +0000 | [diff] [blame] | 140 | fib_table_entry_special_remove(fib_index, |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 141 | &prefix, |
| 142 | ip4_specials[ii].ift_source); |
| 143 | } |
Neale Ranns | d695333 | 2021-08-10 07:39:18 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | static u32 |
| 147 | ip4_create_fib_with_table_id (u32 table_id, |
| 148 | fib_source_t src) |
| 149 | { |
| 150 | fib_table_t *fib_table; |
| 151 | ip4_fib_t *v4_fib; |
| 152 | |
| 153 | pool_get(ip4_main.fibs, fib_table); |
| 154 | clib_memset(fib_table, 0, sizeof(*fib_table)); |
| 155 | |
| 156 | pool_get_aligned(ip4_fibs, v4_fib, CLIB_CACHE_LINE_BYTES); |
| 157 | |
| 158 | fib_table->ft_proto = FIB_PROTOCOL_IP4; |
| 159 | fib_table->ft_index = (v4_fib - ip4_fibs); |
| 160 | |
| 161 | /* |
| 162 | * It is required that the index of the fib_table_t in its pool |
| 163 | * is the same as the index of the ip4_fib_t in its pool, since the |
| 164 | * rest of the code usues the 'fib_index' to mean either of these |
| 165 | * objects, depending on the context. |
| 166 | */ |
| 167 | ASSERT(fib_table->ft_index == fib_table - ip4_main.fibs); |
| 168 | |
| 169 | hash_set (ip4_main.fib_index_by_table_id, table_id, fib_table->ft_index); |
| 170 | |
| 171 | fib_table->ft_table_id = |
| 172 | v4_fib->hash.table_id = |
| 173 | table_id; |
| 174 | fib_table->ft_flow_hash_config = IP_FLOW_HASH_DEFAULT; |
| 175 | |
| 176 | fib_table_lock(fib_table->ft_index, FIB_PROTOCOL_IP4, src); |
| 177 | |
| 178 | ip4_fib_table_init(v4_fib); |
| 179 | |
| 180 | /* |
| 181 | * add the special entries into the new FIB |
| 182 | */ |
| 183 | ip4_fib_hash_load_specials(fib_table - ip4_main.fibs); |
| 184 | |
| 185 | return (fib_table->ft_index); |
| 186 | } |
| 187 | |
| 188 | void |
| 189 | ip4_fib_table_destroy (u32 fib_index) |
| 190 | { |
| 191 | fib_table_t *fib_table = pool_elt_at_index(ip4_main.fibs, fib_index); |
| 192 | ip4_fib_t *v4_fib = pool_elt_at_index(ip4_fibs, fib_table->ft_index); |
| 193 | u32 *n_locks; |
| 194 | |
| 195 | /* |
| 196 | * remove all the specials we added when the table was created. |
| 197 | * In reverse order so the default route is last. |
| 198 | */ |
| 199 | ip4_fib_hash_flush_specials(fib_table - ip4_main.fibs); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 200 | |
| 201 | /* |
| 202 | * validate no more routes. |
| 203 | */ |
Matthew Smith | ee167e5 | 2020-07-02 17:24:17 -0500 | [diff] [blame] | 204 | #if CLIB_DEBUG > 0 |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 205 | if (0 != fib_table->ft_total_route_counts) |
| 206 | fib_table_assert_empty(fib_table); |
| 207 | #endif |
Neale Ranns | 3bab8f9 | 2019-12-04 06:11:00 +0000 | [diff] [blame] | 208 | |
| 209 | vec_foreach(n_locks, fib_table->ft_src_route_counts) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 210 | { |
Neale Ranns | 3bab8f9 | 2019-12-04 06:11:00 +0000 | [diff] [blame] | 211 | ASSERT(0 == *n_locks); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | if (~0 != fib_table->ft_table_id) |
| 215 | { |
| 216 | hash_unset (ip4_main.fib_index_by_table_id, fib_table->ft_table_id); |
| 217 | } |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 218 | |
Steven Luong | 221be7c | 2021-12-15 13:27:53 -0800 | [diff] [blame] | 219 | vec_free (fib_table->ft_locks); |
Neale Ranns | 3bab8f9 | 2019-12-04 06:11:00 +0000 | [diff] [blame] | 220 | vec_free(fib_table->ft_src_route_counts); |
Neale Ranns | d695333 | 2021-08-10 07:39:18 +0000 | [diff] [blame] | 221 | ip4_fib_table_free(v4_fib); |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 222 | |
Neale Ranns | d695333 | 2021-08-10 07:39:18 +0000 | [diff] [blame] | 223 | pool_put(ip4_fibs, v4_fib); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 224 | pool_put(ip4_main.fibs, fib_table); |
| 225 | } |
| 226 | |
| 227 | |
| 228 | u32 |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 229 | ip4_fib_table_find_or_create_and_lock (u32 table_id, |
| 230 | fib_source_t src) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 231 | { |
| 232 | u32 index; |
| 233 | |
| 234 | index = ip4_fib_index_from_table_id(table_id); |
| 235 | if (~0 == index) |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 236 | return ip4_create_fib_with_table_id(table_id, src); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 237 | |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 238 | fib_table_lock(index, FIB_PROTOCOL_IP4, src); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 239 | |
| 240 | return (index); |
| 241 | } |
| 242 | |
| 243 | u32 |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 244 | ip4_fib_table_create_and_lock (fib_source_t src) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 245 | { |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 246 | return (ip4_create_fib_with_table_id(~0, src)); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | u32 |
| 250 | ip4_fib_table_get_index_for_sw_if_index (u32 sw_if_index) |
| 251 | { |
| 252 | if (sw_if_index >= vec_len(ip4_main.fib_index_by_sw_if_index)) |
| 253 | { |
| 254 | /* |
| 255 | * This is the case for interfaces that are not yet mapped to |
| 256 | * a IP table |
| 257 | */ |
| 258 | return (~0); |
| 259 | } |
| 260 | return (ip4_main.fib_index_by_sw_if_index[sw_if_index]); |
| 261 | } |
| 262 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 263 | |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 264 | /** |
| 265 | * Walk show context |
| 266 | */ |
| 267 | typedef struct ip4_fib_show_walk_ctx_t_ |
| 268 | { |
| 269 | fib_node_index_t *ifsw_indicies; |
| 270 | } ip4_fib_show_walk_ctx_t; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 271 | |
Neale Ranns | 8954199 | 2017-04-06 04:41:02 -0700 | [diff] [blame] | 272 | static fib_table_walk_rc_t |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 273 | ip4_fib_show_walk_cb (fib_node_index_t fib_entry_index, |
| 274 | void *arg) |
| 275 | { |
| 276 | ip4_fib_show_walk_ctx_t *ctx = arg; |
| 277 | |
| 278 | vec_add1(ctx->ifsw_indicies, fib_entry_index); |
| 279 | |
Neale Ranns | 8954199 | 2017-04-06 04:41:02 -0700 | [diff] [blame] | 280 | return (FIB_TABLE_WALK_CONTINUE); |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | static void |
| 284 | ip4_fib_table_show_all (ip4_fib_t *fib, |
| 285 | vlib_main_t * vm) |
| 286 | { |
| 287 | ip4_fib_show_walk_ctx_t ctx = { |
| 288 | .ifsw_indicies = NULL, |
| 289 | }; |
| 290 | fib_node_index_t *fib_entry_index; |
| 291 | |
| 292 | ip4_fib_table_walk(fib, ip4_fib_show_walk_cb, &ctx); |
| 293 | vec_sort_with_function(ctx.ifsw_indicies, |
| 294 | fib_entry_cmp_for_sort); |
| 295 | |
| 296 | vec_foreach(fib_entry_index, ctx.ifsw_indicies) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 297 | { |
| 298 | vlib_cli_output(vm, "%U", |
| 299 | format_fib_entry, |
| 300 | *fib_entry_index, |
| 301 | FIB_ENTRY_FORMAT_BRIEF); |
| 302 | } |
| 303 | |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 304 | vec_free(ctx.ifsw_indicies); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | static void |
| 308 | ip4_fib_table_show_one (ip4_fib_t *fib, |
| 309 | vlib_main_t * vm, |
| 310 | ip4_address_t *address, |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 311 | u32 mask_len, |
| 312 | int detail) |
Dave Barach | 2c8e002 | 2020-02-11 15:06:34 -0500 | [diff] [blame] | 313 | { |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 314 | vlib_cli_output(vm, "%U", |
| 315 | format_fib_entry, |
| 316 | ip4_fib_table_lookup(fib, address, mask_len), |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 317 | (detail ? |
| 318 | FIB_ENTRY_FORMAT_DETAIL2 : |
| 319 | FIB_ENTRY_FORMAT_DETAIL)); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 320 | } |
| 321 | |
Neale Ranns | c87aafa | 2017-11-29 00:59:31 -0800 | [diff] [blame] | 322 | u8 * |
| 323 | format_ip4_fib_table_memory (u8 * s, va_list * args) |
| 324 | { |
Damjan Marion | 8157a16 | 2020-09-16 17:06:45 +0200 | [diff] [blame] | 325 | s = format(s, "%=30s %=6d\n", |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 326 | "IPv4 unicast", |
Damjan Marion | 8157a16 | 2020-09-16 17:06:45 +0200 | [diff] [blame] | 327 | pool_elts(ip4_main.fibs)); |
Neale Ranns | c87aafa | 2017-11-29 00:59:31 -0800 | [diff] [blame] | 328 | return (s); |
| 329 | } |
| 330 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 331 | static clib_error_t * |
| 332 | ip4_show_fib (vlib_main_t * vm, |
| 333 | unformat_input_t * input, |
| 334 | vlib_cli_command_t * cmd) |
| 335 | { |
| 336 | ip4_main_t * im4 = &ip4_main; |
Neale Ranns | c87aafa | 2017-11-29 00:59:31 -0800 | [diff] [blame] | 337 | u64 total_mtrie_memory, total_hash_memory; |
| 338 | int verbose, matching, mtrie, memory; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 339 | ip4_address_t matching_address; |
Neale Ranns | d695333 | 2021-08-10 07:39:18 +0000 | [diff] [blame] | 340 | u32 fib_index, matching_mask = 32; |
| 341 | int i, table_id = -1, user_fib_index = ~0; |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 342 | int detail = 0; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 343 | |
| 344 | verbose = 1; |
Neale Ranns | c87aafa | 2017-11-29 00:59:31 -0800 | [diff] [blame] | 345 | matching = mtrie = memory = 0; |
| 346 | total_hash_memory = total_mtrie_memory = 0; |
| 347 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 348 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 349 | { |
| 350 | if (unformat (input, "brief") || unformat (input, "summary") |
| 351 | || unformat (input, "sum")) |
| 352 | verbose = 0; |
| 353 | |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 354 | else if (unformat (input, "detail") || unformat (input, "det")) |
| 355 | detail = 1; |
| 356 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 357 | else if (unformat (input, "mtrie")) |
| 358 | mtrie = 1; |
| 359 | |
Neale Ranns | c87aafa | 2017-11-29 00:59:31 -0800 | [diff] [blame] | 360 | else if (unformat (input, "mem") || |
| 361 | unformat (input, "memory")) |
| 362 | memory = 1; |
| 363 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 364 | else if (unformat (input, "%U/%d", |
| 365 | unformat_ip4_address, &matching_address, &matching_mask)) |
| 366 | matching = 1; |
| 367 | |
| 368 | else if (unformat (input, "%U", unformat_ip4_address, &matching_address)) |
| 369 | matching = 1; |
| 370 | |
| 371 | else if (unformat (input, "table %d", &table_id)) |
| 372 | ; |
Neale Ranns | d695333 | 2021-08-10 07:39:18 +0000 | [diff] [blame] | 373 | else if (unformat (input, "index %d", &user_fib_index)) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 374 | ; |
| 375 | else |
| 376 | break; |
| 377 | } |
| 378 | |
Neale Ranns | d695333 | 2021-08-10 07:39:18 +0000 | [diff] [blame] | 379 | pool_foreach_index (fib_index, im4->fibs) |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 380 | { |
Neale Ranns | d695333 | 2021-08-10 07:39:18 +0000 | [diff] [blame] | 381 | fib_table_t *fib_table = pool_elt_at_index(im4->fibs, fib_index); |
| 382 | ip4_fib_t *fib = pool_elt_at_index(ip4_fibs, fib_table->ft_index); |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 383 | fib_source_t source; |
| 384 | u8 *s = NULL; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 385 | |
Neale Ranns | d695333 | 2021-08-10 07:39:18 +0000 | [diff] [blame] | 386 | if (table_id >= 0 && table_id != (int)fib->hash.table_id) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 387 | continue; |
Neale Ranns | d695333 | 2021-08-10 07:39:18 +0000 | [diff] [blame] | 388 | if (user_fib_index != ~0 && user_fib_index != fib_index) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 389 | continue; |
| 390 | |
Neale Ranns | c87aafa | 2017-11-29 00:59:31 -0800 | [diff] [blame] | 391 | if (memory) |
| 392 | { |
Damjan Marion | 8157a16 | 2020-09-16 17:06:45 +0200 | [diff] [blame] | 393 | uword mtrie_size, hash_size; |
Lollita Liu | e18b45c | 2019-01-11 05:23:12 -0500 | [diff] [blame] | 394 | |
Neale Ranns | c87aafa | 2017-11-29 00:59:31 -0800 | [diff] [blame] | 395 | |
Neale Ranns | d695333 | 2021-08-10 07:39:18 +0000 | [diff] [blame] | 396 | mtrie_size = ip4_mtrie_memory_usage(&fib->mtrie); |
Neale Ranns | c87aafa | 2017-11-29 00:59:31 -0800 | [diff] [blame] | 397 | hash_size = 0; |
| 398 | |
Neale Ranns | d695333 | 2021-08-10 07:39:18 +0000 | [diff] [blame] | 399 | for (i = 0; i < ARRAY_LEN (fib->hash.fib_entry_by_dst_address); i++) |
Neale Ranns | c87aafa | 2017-11-29 00:59:31 -0800 | [diff] [blame] | 400 | { |
Neale Ranns | d695333 | 2021-08-10 07:39:18 +0000 | [diff] [blame] | 401 | uword * hash = fib->hash.fib_entry_by_dst_address[i]; |
Neale Ranns | c87aafa | 2017-11-29 00:59:31 -0800 | [diff] [blame] | 402 | if (NULL != hash) |
| 403 | { |
| 404 | hash_size += hash_bytes(hash); |
| 405 | } |
| 406 | } |
Lollita Liu | e18b45c | 2019-01-11 05:23:12 -0500 | [diff] [blame] | 407 | |
Neale Ranns | c87aafa | 2017-11-29 00:59:31 -0800 | [diff] [blame] | 408 | if (verbose) |
| 409 | vlib_cli_output (vm, "%U mtrie:%d hash:%d", |
Neale Ranns | d695333 | 2021-08-10 07:39:18 +0000 | [diff] [blame] | 410 | format_fib_table_name, fib_index, |
Neale Ranns | c87aafa | 2017-11-29 00:59:31 -0800 | [diff] [blame] | 411 | FIB_PROTOCOL_IP4, |
| 412 | mtrie_size, |
| 413 | hash_size); |
| 414 | total_mtrie_memory += mtrie_size; |
| 415 | total_hash_memory += hash_size; |
| 416 | continue; |
| 417 | } |
| 418 | |
Neale Ranns | 9db6ada | 2019-11-08 12:42:31 +0000 | [diff] [blame] | 419 | s = format(s, "%U, fib_index:%d, flow hash:[%U] epoch:%d flags:%U locks:[", |
Neale Ranns | d695333 | 2021-08-10 07:39:18 +0000 | [diff] [blame] | 420 | format_fib_table_name, fib_index, |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 421 | FIB_PROTOCOL_IP4, |
Neale Ranns | d695333 | 2021-08-10 07:39:18 +0000 | [diff] [blame] | 422 | fib_index, |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 423 | format_ip_flow_hash_config, |
Neale Ranns | 9db6ada | 2019-11-08 12:42:31 +0000 | [diff] [blame] | 424 | fib_table->ft_flow_hash_config, |
| 425 | fib_table->ft_epoch, |
| 426 | format_fib_table_flags, fib_table->ft_flags); |
Neale Ranns | 3bab8f9 | 2019-12-04 06:11:00 +0000 | [diff] [blame] | 427 | vec_foreach_index(source, fib_table->ft_locks) |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 428 | { |
| 429 | if (0 != fib_table->ft_locks[source]) |
| 430 | { |
| 431 | s = format(s, "%U:%d, ", |
| 432 | format_fib_source, source, |
| 433 | fib_table->ft_locks[source]); |
| 434 | } |
| 435 | } |
| 436 | s = format (s, "]"); |
Neale Ranns | 2297af0 | 2017-09-12 09:45:04 -0700 | [diff] [blame] | 437 | vlib_cli_output (vm, "%v", s); |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 438 | vec_free(s); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 439 | |
| 440 | /* Show summary? */ |
Neale Ranns | 3919425 | 2017-11-27 01:03:25 -0800 | [diff] [blame] | 441 | if (mtrie) |
| 442 | { |
Neale Ranns | d695333 | 2021-08-10 07:39:18 +0000 | [diff] [blame] | 443 | vlib_cli_output (vm, "%U", format_ip4_mtrie, &fib->mtrie, verbose); |
Neale Ranns | 3919425 | 2017-11-27 01:03:25 -0800 | [diff] [blame] | 444 | continue; |
| 445 | } |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 446 | if (! verbose) |
| 447 | { |
| 448 | vlib_cli_output (vm, "%=20s%=16s", "Prefix length", "Count"); |
Neale Ranns | d695333 | 2021-08-10 07:39:18 +0000 | [diff] [blame] | 449 | for (i = 0; i < ARRAY_LEN (fib->hash.fib_entry_by_dst_address); i++) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 450 | { |
Neale Ranns | d695333 | 2021-08-10 07:39:18 +0000 | [diff] [blame] | 451 | uword * hash = fib->hash.fib_entry_by_dst_address[i]; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 452 | uword n_elts = hash_elts (hash); |
| 453 | if (n_elts > 0) |
| 454 | vlib_cli_output (vm, "%20d%16d", i, n_elts); |
| 455 | } |
| 456 | continue; |
| 457 | } |
| 458 | |
| 459 | if (!matching) |
| 460 | { |
| 461 | ip4_fib_table_show_all(fib, vm); |
| 462 | } |
| 463 | else |
| 464 | { |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 465 | ip4_fib_table_show_one(fib, vm, &matching_address, |
| 466 | matching_mask, detail); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 467 | } |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 468 | } |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 469 | |
Neale Ranns | c87aafa | 2017-11-29 00:59:31 -0800 | [diff] [blame] | 470 | if (memory) |
Lollita Liu | e18b45c | 2019-01-11 05:23:12 -0500 | [diff] [blame] | 471 | { |
Neale Ranns | c87aafa | 2017-11-29 00:59:31 -0800 | [diff] [blame] | 472 | vlib_cli_output (vm, "totals: mtrie:%ld hash:%ld all:%ld", |
| 473 | total_mtrie_memory, |
| 474 | total_hash_memory, |
| 475 | total_mtrie_memory + total_hash_memory); |
Lollita Liu | e18b45c | 2019-01-11 05:23:12 -0500 | [diff] [blame] | 476 | } |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 477 | return 0; |
| 478 | } |
| 479 | |
| 480 | /*? |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 481 | * This command displays the IPv4 FIB Tables (VRF Tables) and the route |
| 482 | * entries for each table. |
| 483 | * |
| 484 | * @note This command will run for a long time when the FIB tables are |
Nathan Skrzypczak | da33105 | 2021-09-29 15:28:26 +0200 | [diff] [blame] | 485 | * comprised of millions of entries. For those scenarios, consider displaying |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 486 | * a single table or summary mode. |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 487 | * |
| 488 | * @cliexpar |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 489 | * Example of how to display all the IPv4 FIB tables: |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 490 | * @cliexstart{show ip fib} |
Billy McFall | ebb9a6a | 2016-10-17 11:35:32 -0400 | [diff] [blame] | 491 | * ipv4-VRF:0, fib_index 0, flow hash: src dst sport dport proto |
| 492 | * 0.0.0.0/0 |
| 493 | * unicast-ip4-chain |
| 494 | * [@0]: dpo-load-balance: [index:0 buckets:1 uRPF:0 to:[0:0]] |
| 495 | * [0] [@0]: dpo-drop ip6 |
| 496 | * 0.0.0.0/32 |
| 497 | * unicast-ip4-chain |
| 498 | * [@0]: dpo-load-balance: [index:1 buckets:1 uRPF:1 to:[0:0]] |
| 499 | * [0] [@0]: dpo-drop ip6 |
| 500 | * 6.0.1.2/32 |
| 501 | * unicast-ip4-chain |
| 502 | * [@0]: dpo-load-balance: [index:30 buckets:1 uRPF:29 to:[0:0]] |
| 503 | * [0] [@3]: arp-ipv4: via 6.0.0.1 af_packet0 |
| 504 | * 7.0.0.1/32 |
| 505 | * unicast-ip4-chain |
| 506 | * [@0]: dpo-load-balance: [index:31 buckets:4 uRPF:30 to:[0:0]] |
| 507 | * [0] [@3]: arp-ipv4: via 6.0.0.2 af_packet0 |
| 508 | * [1] [@3]: arp-ipv4: via 6.0.0.2 af_packet0 |
| 509 | * [2] [@3]: arp-ipv4: via 6.0.0.2 af_packet0 |
| 510 | * [3] [@3]: arp-ipv4: via 6.0.0.1 af_packet0 |
| 511 | * 224.0.0.0/8 |
| 512 | * unicast-ip4-chain |
| 513 | * [@0]: dpo-load-balance: [index:3 buckets:1 uRPF:3 to:[0:0]] |
| 514 | * [0] [@0]: dpo-drop ip6 |
| 515 | * 240.0.0.0/8 |
| 516 | * unicast-ip4-chain |
| 517 | * [@0]: dpo-load-balance: [index:2 buckets:1 uRPF:2 to:[0:0]] |
| 518 | * [0] [@0]: dpo-drop ip6 |
| 519 | * 255.255.255.255/32 |
| 520 | * unicast-ip4-chain |
| 521 | * [@0]: dpo-load-balance: [index:4 buckets:1 uRPF:4 to:[0:0]] |
| 522 | * [0] [@0]: dpo-drop ip6 |
| 523 | * ipv4-VRF:7, fib_index 1, flow hash: src dst sport dport proto |
| 524 | * 0.0.0.0/0 |
| 525 | * unicast-ip4-chain |
| 526 | * [@0]: dpo-load-balance: [index:12 buckets:1 uRPF:11 to:[0:0]] |
| 527 | * [0] [@0]: dpo-drop ip6 |
| 528 | * 0.0.0.0/32 |
| 529 | * unicast-ip4-chain |
| 530 | * [@0]: dpo-load-balance: [index:13 buckets:1 uRPF:12 to:[0:0]] |
| 531 | * [0] [@0]: dpo-drop ip6 |
| 532 | * 172.16.1.0/24 |
| 533 | * unicast-ip4-chain |
| 534 | * [@0]: dpo-load-balance: [index:17 buckets:1 uRPF:16 to:[0:0]] |
| 535 | * [0] [@4]: ipv4-glean: af_packet0 |
| 536 | * 172.16.1.1/32 |
| 537 | * unicast-ip4-chain |
| 538 | * [@0]: dpo-load-balance: [index:18 buckets:1 uRPF:17 to:[1:84]] |
| 539 | * [0] [@2]: dpo-receive: 172.16.1.1 on af_packet0 |
| 540 | * 172.16.1.2/32 |
| 541 | * unicast-ip4-chain |
| 542 | * [@0]: dpo-load-balance: [index:21 buckets:1 uRPF:20 to:[0:0]] |
| 543 | * [0] [@5]: ipv4 via 172.16.1.2 af_packet0: IP4: 02:fe:9e:70:7a:2b -> 26:a5:f6:9c:3a:36 |
| 544 | * 172.16.2.0/24 |
| 545 | * unicast-ip4-chain |
| 546 | * [@0]: dpo-load-balance: [index:19 buckets:1 uRPF:18 to:[0:0]] |
| 547 | * [0] [@4]: ipv4-glean: af_packet1 |
| 548 | * 172.16.2.1/32 |
| 549 | * unicast-ip4-chain |
| 550 | * [@0]: dpo-load-balance: [index:20 buckets:1 uRPF:19 to:[0:0]] |
| 551 | * [0] [@2]: dpo-receive: 172.16.2.1 on af_packet1 |
| 552 | * 224.0.0.0/8 |
| 553 | * unicast-ip4-chain |
| 554 | * [@0]: dpo-load-balance: [index:15 buckets:1 uRPF:14 to:[0:0]] |
| 555 | * [0] [@0]: dpo-drop ip6 |
| 556 | * 240.0.0.0/8 |
| 557 | * unicast-ip4-chain |
| 558 | * [@0]: dpo-load-balance: [index:14 buckets:1 uRPF:13 to:[0:0]] |
| 559 | * [0] [@0]: dpo-drop ip6 |
| 560 | * 255.255.255.255/32 |
| 561 | * unicast-ip4-chain |
| 562 | * [@0]: dpo-load-balance: [index:16 buckets:1 uRPF:15 to:[0:0]] |
| 563 | * [0] [@0]: dpo-drop ip6 |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 564 | * @cliexend |
| 565 | * Example of how to display a single IPv4 FIB table: |
| 566 | * @cliexstart{show ip fib table 7} |
Billy McFall | ebb9a6a | 2016-10-17 11:35:32 -0400 | [diff] [blame] | 567 | * ipv4-VRF:7, fib_index 1, flow hash: src dst sport dport proto |
| 568 | * 0.0.0.0/0 |
| 569 | * unicast-ip4-chain |
| 570 | * [@0]: dpo-load-balance: [index:12 buckets:1 uRPF:11 to:[0:0]] |
| 571 | * [0] [@0]: dpo-drop ip6 |
| 572 | * 0.0.0.0/32 |
| 573 | * unicast-ip4-chain |
| 574 | * [@0]: dpo-load-balance: [index:13 buckets:1 uRPF:12 to:[0:0]] |
| 575 | * [0] [@0]: dpo-drop ip6 |
| 576 | * 172.16.1.0/24 |
| 577 | * unicast-ip4-chain |
| 578 | * [@0]: dpo-load-balance: [index:17 buckets:1 uRPF:16 to:[0:0]] |
| 579 | * [0] [@4]: ipv4-glean: af_packet0 |
| 580 | * 172.16.1.1/32 |
| 581 | * unicast-ip4-chain |
| 582 | * [@0]: dpo-load-balance: [index:18 buckets:1 uRPF:17 to:[1:84]] |
| 583 | * [0] [@2]: dpo-receive: 172.16.1.1 on af_packet0 |
| 584 | * 172.16.1.2/32 |
| 585 | * unicast-ip4-chain |
| 586 | * [@0]: dpo-load-balance: [index:21 buckets:1 uRPF:20 to:[0:0]] |
| 587 | * [0] [@5]: ipv4 via 172.16.1.2 af_packet0: IP4: 02:fe:9e:70:7a:2b -> 26:a5:f6:9c:3a:36 |
| 588 | * 172.16.2.0/24 |
| 589 | * unicast-ip4-chain |
| 590 | * [@0]: dpo-load-balance: [index:19 buckets:1 uRPF:18 to:[0:0]] |
| 591 | * [0] [@4]: ipv4-glean: af_packet1 |
| 592 | * 172.16.2.1/32 |
| 593 | * unicast-ip4-chain |
| 594 | * [@0]: dpo-load-balance: [index:20 buckets:1 uRPF:19 to:[0:0]] |
| 595 | * [0] [@2]: dpo-receive: 172.16.2.1 on af_packet1 |
| 596 | * 224.0.0.0/8 |
| 597 | * unicast-ip4-chain |
| 598 | * [@0]: dpo-load-balance: [index:15 buckets:1 uRPF:14 to:[0:0]] |
| 599 | * [0] [@0]: dpo-drop ip6 |
| 600 | * 240.0.0.0/8 |
| 601 | * unicast-ip4-chain |
| 602 | * [@0]: dpo-load-balance: [index:14 buckets:1 uRPF:13 to:[0:0]] |
| 603 | * [0] [@0]: dpo-drop ip6 |
| 604 | * 255.255.255.255/32 |
| 605 | * unicast-ip4-chain |
| 606 | * [@0]: dpo-load-balance: [index:16 buckets:1 uRPF:15 to:[0:0]] |
| 607 | * [0] [@0]: dpo-drop ip6 |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 608 | * @cliexend |
| 609 | * Example of how to display a summary of all IPv4 FIB tables: |
| 610 | * @cliexstart{show ip fib summary} |
Billy McFall | ebb9a6a | 2016-10-17 11:35:32 -0400 | [diff] [blame] | 611 | * ipv4-VRF:0, fib_index 0, flow hash: src dst sport dport proto |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 612 | * Prefix length Count |
Billy McFall | ebb9a6a | 2016-10-17 11:35:32 -0400 | [diff] [blame] | 613 | * 0 1 |
| 614 | * 8 2 |
| 615 | * 32 4 |
| 616 | * ipv4-VRF:7, fib_index 1, flow hash: src dst sport dport proto |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 617 | * Prefix length Count |
Billy McFall | ebb9a6a | 2016-10-17 11:35:32 -0400 | [diff] [blame] | 618 | * 0 1 |
| 619 | * 8 2 |
| 620 | * 24 2 |
| 621 | * 32 4 |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 622 | * @cliexend |
| 623 | ?*/ |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 624 | /* *INDENT-OFF* */ |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 625 | VLIB_CLI_COMMAND (ip4_show_fib_command, static) = { |
| 626 | .path = "show ip fib", |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 627 | .short_help = "show ip fib [summary] [table <table-id>] [index <fib-id>] [<ip4-addr>[/<mask>]] [mtrie] [detail]", |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 628 | .function = ip4_show_fib, |
| 629 | }; |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 630 | /* *INDENT-ON* */ |