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 | /* |
| 21 | * A table of pefixes to be added to tables and the sources for them |
| 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 | |
| 102 | |
| 103 | static u32 |
| 104 | ip4_create_fib_with_table_id (u32 table_id) |
| 105 | { |
| 106 | fib_table_t *fib_table; |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 107 | ip4_fib_t *v4_fib; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 108 | |
| 109 | pool_get_aligned(ip4_main.fibs, fib_table, CLIB_CACHE_LINE_BYTES); |
| 110 | memset(fib_table, 0, sizeof(*fib_table)); |
| 111 | |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 112 | pool_get_aligned(ip4_main.v4_fibs, v4_fib, CLIB_CACHE_LINE_BYTES); |
| 113 | |
| 114 | ASSERT((fib_table - ip4_main.fibs) == |
| 115 | (v4_fib - ip4_main.v4_fibs)); |
| 116 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 117 | fib_table->ft_proto = FIB_PROTOCOL_IP4; |
| 118 | fib_table->ft_index = |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 119 | v4_fib->index = |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 120 | (fib_table - ip4_main.fibs); |
| 121 | |
| 122 | hash_set (ip4_main.fib_index_by_table_id, table_id, fib_table->ft_index); |
| 123 | |
| 124 | fib_table->ft_table_id = |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 125 | v4_fib->table_id = |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 126 | table_id; |
Neale Ranns | 227038a | 2017-04-21 01:07:59 -0700 | [diff] [blame] | 127 | fib_table->ft_flow_hash_config = IP_FLOW_HASH_DEFAULT; |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 128 | v4_fib->fwd_classify_table_index = ~0; |
| 129 | v4_fib->rev_classify_table_index = ~0; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 130 | |
| 131 | fib_table_lock(fib_table->ft_index, FIB_PROTOCOL_IP4); |
| 132 | |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 133 | ip4_mtrie_init(&v4_fib->mtrie); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 134 | |
| 135 | /* |
| 136 | * add the special entries into the new FIB |
| 137 | */ |
| 138 | int ii; |
| 139 | |
| 140 | for (ii = 0; ii < ARRAY_LEN(ip4_specials); ii++) |
| 141 | { |
| 142 | fib_prefix_t prefix = ip4_specials[ii].ift_prefix; |
| 143 | |
| 144 | prefix.fp_addr.ip4.data_u32 = |
| 145 | clib_host_to_net_u32(prefix.fp_addr.ip4.data_u32); |
| 146 | |
| 147 | fib_table_entry_special_add(fib_table->ft_index, |
| 148 | &prefix, |
| 149 | ip4_specials[ii].ift_source, |
Neale Ranns | a055830 | 2017-04-13 00:44:52 -0700 | [diff] [blame] | 150 | ip4_specials[ii].ift_flag); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | return (fib_table->ft_index); |
| 154 | } |
| 155 | |
| 156 | void |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 157 | ip4_fib_table_destroy (u32 fib_index) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 158 | { |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 159 | fib_table_t *fib_table = pool_elt_at_index(ip4_main.fibs, fib_index); |
| 160 | ip4_fib_t *v4_fib = pool_elt_at_index(ip4_main.v4_fibs, fib_index); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 161 | int ii; |
| 162 | |
| 163 | /* |
| 164 | * remove all the specials we added when the table was created. |
Neale Ranns | 04a75e3 | 2017-03-23 06:46:01 -0700 | [diff] [blame] | 165 | * In reverse order so the default route is last. |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 166 | */ |
Neale Ranns | 04a75e3 | 2017-03-23 06:46:01 -0700 | [diff] [blame] | 167 | for (ii = ARRAY_LEN(ip4_specials) - 1; ii >= 0; ii--) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 168 | { |
| 169 | fib_prefix_t prefix = ip4_specials[ii].ift_prefix; |
| 170 | |
| 171 | prefix.fp_addr.ip4.data_u32 = |
| 172 | clib_host_to_net_u32(prefix.fp_addr.ip4.data_u32); |
| 173 | |
| 174 | fib_table_entry_special_remove(fib_table->ft_index, |
| 175 | &prefix, |
| 176 | ip4_specials[ii].ift_source); |
| 177 | } |
| 178 | |
| 179 | /* |
| 180 | * validate no more routes. |
| 181 | */ |
| 182 | ASSERT(0 == fib_table->ft_total_route_counts); |
| 183 | FOR_EACH_FIB_SOURCE(ii) |
| 184 | { |
| 185 | ASSERT(0 == fib_table->ft_src_route_counts[ii]); |
| 186 | } |
| 187 | |
| 188 | if (~0 != fib_table->ft_table_id) |
| 189 | { |
| 190 | hash_unset (ip4_main.fib_index_by_table_id, fib_table->ft_table_id); |
| 191 | } |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 192 | |
| 193 | ip4_mtrie_free(&v4_fib->mtrie); |
| 194 | |
| 195 | pool_put(ip4_main.v4_fibs, v4_fib); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 196 | pool_put(ip4_main.fibs, fib_table); |
| 197 | } |
| 198 | |
| 199 | |
| 200 | u32 |
| 201 | ip4_fib_table_find_or_create_and_lock (u32 table_id) |
| 202 | { |
| 203 | u32 index; |
| 204 | |
| 205 | index = ip4_fib_index_from_table_id(table_id); |
| 206 | if (~0 == index) |
| 207 | return ip4_create_fib_with_table_id(table_id); |
| 208 | |
| 209 | fib_table_lock(index, FIB_PROTOCOL_IP4); |
| 210 | |
| 211 | return (index); |
| 212 | } |
| 213 | |
| 214 | u32 |
| 215 | ip4_fib_table_create_and_lock (void) |
| 216 | { |
| 217 | return (ip4_create_fib_with_table_id(~0)); |
| 218 | } |
| 219 | |
| 220 | u32 |
| 221 | ip4_fib_table_get_index_for_sw_if_index (u32 sw_if_index) |
| 222 | { |
| 223 | if (sw_if_index >= vec_len(ip4_main.fib_index_by_sw_if_index)) |
| 224 | { |
| 225 | /* |
| 226 | * This is the case for interfaces that are not yet mapped to |
| 227 | * a IP table |
| 228 | */ |
| 229 | return (~0); |
| 230 | } |
| 231 | return (ip4_main.fib_index_by_sw_if_index[sw_if_index]); |
| 232 | } |
| 233 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 234 | /* |
| 235 | * ip4_fib_table_lookup_exact_match |
| 236 | * |
| 237 | * Exact match prefix lookup |
| 238 | */ |
| 239 | fib_node_index_t |
| 240 | ip4_fib_table_lookup_exact_match (const ip4_fib_t *fib, |
| 241 | const ip4_address_t *addr, |
| 242 | u32 len) |
| 243 | { |
| 244 | uword * hash, * result; |
| 245 | u32 key; |
| 246 | |
| 247 | hash = fib->fib_entry_by_dst_address[len]; |
| 248 | key = (addr->data_u32 & ip4_main.fib_masks[len]); |
| 249 | |
| 250 | result = hash_get(hash, key); |
| 251 | |
| 252 | if (NULL != result) { |
| 253 | return (result[0]); |
| 254 | } |
| 255 | return (FIB_NODE_INDEX_INVALID); |
| 256 | } |
| 257 | |
| 258 | /* |
| 259 | * ip4_fib_table_lookup_adj |
| 260 | * |
| 261 | * Longest prefix match |
| 262 | */ |
| 263 | index_t |
| 264 | ip4_fib_table_lookup_lb (ip4_fib_t *fib, |
| 265 | const ip4_address_t *addr) |
| 266 | { |
| 267 | fib_node_index_t fei; |
| 268 | |
| 269 | fei = ip4_fib_table_lookup(fib, addr, 32); |
| 270 | |
| 271 | if (FIB_NODE_INDEX_INVALID != fei) |
| 272 | { |
| 273 | const dpo_id_t *dpo; |
| 274 | |
| 275 | dpo = fib_entry_contribute_ip_forwarding(fei); |
| 276 | |
| 277 | return (dpo->dpoi_index); |
| 278 | } |
| 279 | return (INDEX_INVALID); |
| 280 | } |
| 281 | |
| 282 | /* |
| 283 | * ip4_fib_table_lookup |
| 284 | * |
| 285 | * Longest prefix match |
| 286 | */ |
| 287 | fib_node_index_t |
| 288 | ip4_fib_table_lookup (const ip4_fib_t *fib, |
| 289 | const ip4_address_t *addr, |
| 290 | u32 len) |
| 291 | { |
| 292 | uword * hash, * result; |
| 293 | i32 mask_len; |
| 294 | u32 key; |
| 295 | |
| 296 | for (mask_len = len; mask_len >= 0; mask_len--) |
| 297 | { |
| 298 | hash = fib->fib_entry_by_dst_address[mask_len]; |
| 299 | key = (addr->data_u32 & ip4_main.fib_masks[mask_len]); |
| 300 | |
| 301 | result = hash_get (hash, key); |
| 302 | |
| 303 | if (NULL != result) { |
| 304 | return (result[0]); |
| 305 | } |
| 306 | } |
| 307 | return (FIB_NODE_INDEX_INVALID); |
| 308 | } |
| 309 | |
| 310 | void |
| 311 | ip4_fib_table_entry_insert (ip4_fib_t *fib, |
| 312 | const ip4_address_t *addr, |
| 313 | u32 len, |
| 314 | fib_node_index_t fib_entry_index) |
| 315 | { |
| 316 | uword * hash, * result; |
| 317 | u32 key; |
| 318 | |
| 319 | key = (addr->data_u32 & ip4_main.fib_masks[len]); |
| 320 | hash = fib->fib_entry_by_dst_address[len]; |
| 321 | result = hash_get (hash, key); |
| 322 | |
| 323 | if (NULL == result) { |
| 324 | /* |
| 325 | * adding a new entry |
| 326 | */ |
| 327 | if (NULL == hash) { |
| 328 | hash = hash_create (32 /* elts */, sizeof (uword)); |
| 329 | hash_set_flags (hash, HASH_FLAG_NO_AUTO_SHRINK); |
| 330 | } |
| 331 | hash = hash_set(hash, key, fib_entry_index); |
| 332 | fib->fib_entry_by_dst_address[len] = hash; |
| 333 | } |
| 334 | else |
| 335 | { |
| 336 | ASSERT(0); |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | void |
| 341 | ip4_fib_table_entry_remove (ip4_fib_t *fib, |
| 342 | const ip4_address_t *addr, |
| 343 | u32 len) |
| 344 | { |
| 345 | uword * hash, * result; |
| 346 | u32 key; |
| 347 | |
| 348 | key = (addr->data_u32 & ip4_main.fib_masks[len]); |
| 349 | hash = fib->fib_entry_by_dst_address[len]; |
| 350 | result = hash_get (hash, key); |
| 351 | |
| 352 | if (NULL == result) |
| 353 | { |
| 354 | /* |
| 355 | * removing a non-existant entry. i'll allow it. |
| 356 | */ |
| 357 | } |
| 358 | else |
| 359 | { |
| 360 | hash_unset(hash, key); |
| 361 | } |
| 362 | |
| 363 | fib->fib_entry_by_dst_address[len] = hash; |
| 364 | } |
| 365 | |
| 366 | void |
| 367 | ip4_fib_table_fwding_dpo_update (ip4_fib_t *fib, |
| 368 | const ip4_address_t *addr, |
| 369 | u32 len, |
| 370 | const dpo_id_t *dpo) |
| 371 | { |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 372 | ip4_fib_mtrie_route_add(&fib->mtrie, addr, len, dpo->dpoi_index); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | void |
| 376 | ip4_fib_table_fwding_dpo_remove (ip4_fib_t *fib, |
| 377 | const ip4_address_t *addr, |
| 378 | u32 len, |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 379 | const dpo_id_t *dpo, |
| 380 | u32 cover_index) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 381 | { |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 382 | fib_prefix_t cover_prefix = { |
| 383 | .fp_len = 0, |
| 384 | }; |
| 385 | const dpo_id_t *cover_dpo; |
| 386 | |
| 387 | /* |
| 388 | * We need to pass the MTRIE the LB index and address length of the |
| 389 | * covering prefix, so it can fill the plys with the correct replacement |
| 390 | * for the entry being removed |
| 391 | */ |
| 392 | fib_entry_get_prefix(cover_index, &cover_prefix); |
| 393 | cover_dpo = fib_entry_contribute_ip_forwarding(cover_index); |
| 394 | |
| 395 | ip4_fib_mtrie_route_del(&fib->mtrie, |
| 396 | addr, len, dpo->dpoi_index, |
| 397 | cover_prefix.fp_len, |
| 398 | cover_dpo->dpoi_index); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 399 | } |
| 400 | |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 401 | void |
| 402 | ip4_fib_table_walk (ip4_fib_t *fib, |
| 403 | fib_table_walk_fn_t fn, |
| 404 | void *ctx) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 405 | { |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 406 | int i; |
| 407 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 408 | for (i = 0; i < ARRAY_LEN (fib->fib_entry_by_dst_address); i++) |
| 409 | { |
| 410 | uword * hash = fib->fib_entry_by_dst_address[i]; |
| 411 | |
| 412 | if (NULL != hash) |
| 413 | { |
| 414 | hash_pair_t * p; |
| 415 | |
| 416 | hash_foreach_pair (p, hash, |
| 417 | ({ |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 418 | fn(p->value[0], ctx); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 419 | })); |
| 420 | } |
| 421 | } |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 422 | } |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 423 | |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 424 | /** |
| 425 | * Walk show context |
| 426 | */ |
| 427 | typedef struct ip4_fib_show_walk_ctx_t_ |
| 428 | { |
| 429 | fib_node_index_t *ifsw_indicies; |
| 430 | } ip4_fib_show_walk_ctx_t; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 431 | |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 432 | static int |
| 433 | ip4_fib_show_walk_cb (fib_node_index_t fib_entry_index, |
| 434 | void *arg) |
| 435 | { |
| 436 | ip4_fib_show_walk_ctx_t *ctx = arg; |
| 437 | |
| 438 | vec_add1(ctx->ifsw_indicies, fib_entry_index); |
| 439 | |
| 440 | return (1); |
| 441 | } |
| 442 | |
| 443 | static void |
| 444 | ip4_fib_table_show_all (ip4_fib_t *fib, |
| 445 | vlib_main_t * vm) |
| 446 | { |
| 447 | ip4_fib_show_walk_ctx_t ctx = { |
| 448 | .ifsw_indicies = NULL, |
| 449 | }; |
| 450 | fib_node_index_t *fib_entry_index; |
| 451 | |
| 452 | ip4_fib_table_walk(fib, ip4_fib_show_walk_cb, &ctx); |
| 453 | vec_sort_with_function(ctx.ifsw_indicies, |
| 454 | fib_entry_cmp_for_sort); |
| 455 | |
| 456 | vec_foreach(fib_entry_index, ctx.ifsw_indicies) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 457 | { |
| 458 | vlib_cli_output(vm, "%U", |
| 459 | format_fib_entry, |
| 460 | *fib_entry_index, |
| 461 | FIB_ENTRY_FORMAT_BRIEF); |
| 462 | } |
| 463 | |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 464 | vec_free(ctx.ifsw_indicies); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | static void |
| 468 | ip4_fib_table_show_one (ip4_fib_t *fib, |
| 469 | vlib_main_t * vm, |
| 470 | ip4_address_t *address, |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 471 | u32 mask_len, |
| 472 | int detail) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 473 | { |
| 474 | vlib_cli_output(vm, "%U", |
| 475 | format_fib_entry, |
| 476 | ip4_fib_table_lookup(fib, address, mask_len), |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 477 | (detail ? |
| 478 | FIB_ENTRY_FORMAT_DETAIL2 : |
| 479 | FIB_ENTRY_FORMAT_DETAIL)); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | static clib_error_t * |
| 483 | ip4_show_fib (vlib_main_t * vm, |
| 484 | unformat_input_t * input, |
| 485 | vlib_cli_command_t * cmd) |
| 486 | { |
| 487 | ip4_main_t * im4 = &ip4_main; |
| 488 | fib_table_t * fib_table; |
| 489 | int verbose, matching, mtrie; |
| 490 | ip4_address_t matching_address; |
| 491 | u32 matching_mask = 32; |
| 492 | int i, table_id = -1, fib_index = ~0; |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 493 | int detail = 0; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 494 | |
| 495 | verbose = 1; |
| 496 | matching = 0; |
| 497 | mtrie = 0; |
| 498 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 499 | { |
| 500 | if (unformat (input, "brief") || unformat (input, "summary") |
| 501 | || unformat (input, "sum")) |
| 502 | verbose = 0; |
| 503 | |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 504 | else if (unformat (input, "detail") || unformat (input, "det")) |
| 505 | detail = 1; |
| 506 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 507 | else if (unformat (input, "mtrie")) |
| 508 | mtrie = 1; |
| 509 | |
| 510 | else if (unformat (input, "%U/%d", |
| 511 | unformat_ip4_address, &matching_address, &matching_mask)) |
| 512 | matching = 1; |
| 513 | |
| 514 | else if (unformat (input, "%U", unformat_ip4_address, &matching_address)) |
| 515 | matching = 1; |
| 516 | |
| 517 | else if (unformat (input, "table %d", &table_id)) |
| 518 | ; |
| 519 | else if (unformat (input, "index %d", &fib_index)) |
| 520 | ; |
| 521 | else |
| 522 | break; |
| 523 | } |
| 524 | |
| 525 | pool_foreach (fib_table, im4->fibs, |
| 526 | ({ |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 527 | ip4_fib_t *fib = pool_elt_at_index(im4->v4_fibs, fib_table->ft_index); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 528 | |
| 529 | if (table_id >= 0 && table_id != (int)fib->table_id) |
| 530 | continue; |
| 531 | if (fib_index != ~0 && fib_index != (int)fib->index) |
| 532 | continue; |
| 533 | |
Neale Ranns | 13eaf3e | 2017-05-23 06:10:33 -0700 | [diff] [blame] | 534 | vlib_cli_output (vm, "%U, fib_index:%d, flow hash:[%U] locks:%d", |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 535 | format_fib_table_name, fib->index, FIB_PROTOCOL_IP4, |
| 536 | fib->index, |
Neale Ranns | 13eaf3e | 2017-05-23 06:10:33 -0700 | [diff] [blame] | 537 | format_ip_flow_hash_config, fib_table->ft_flow_hash_config, |
| 538 | fib_table->ft_locks); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 539 | |
| 540 | /* Show summary? */ |
| 541 | if (! verbose) |
| 542 | { |
| 543 | vlib_cli_output (vm, "%=20s%=16s", "Prefix length", "Count"); |
| 544 | for (i = 0; i < ARRAY_LEN (fib->fib_entry_by_dst_address); i++) |
| 545 | { |
| 546 | uword * hash = fib->fib_entry_by_dst_address[i]; |
| 547 | uword n_elts = hash_elts (hash); |
| 548 | if (n_elts > 0) |
| 549 | vlib_cli_output (vm, "%20d%16d", i, n_elts); |
| 550 | } |
| 551 | continue; |
| 552 | } |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 553 | if (mtrie) |
| 554 | { |
| 555 | vlib_cli_output (vm, "%U", format_ip4_fib_mtrie, &fib->mtrie); |
| 556 | continue; |
| 557 | } |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 558 | |
| 559 | if (!matching) |
| 560 | { |
| 561 | ip4_fib_table_show_all(fib, vm); |
| 562 | } |
| 563 | else |
| 564 | { |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 565 | ip4_fib_table_show_one(fib, vm, &matching_address, |
| 566 | matching_mask, detail); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 567 | } |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 568 | })); |
| 569 | |
| 570 | return 0; |
| 571 | } |
| 572 | |
| 573 | /*? |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 574 | * This command displays the IPv4 FIB Tables (VRF Tables) and the route |
| 575 | * entries for each table. |
| 576 | * |
| 577 | * @note This command will run for a long time when the FIB tables are |
| 578 | * comprised of millions of entries. For those senarios, consider displaying |
| 579 | * a single table or summary mode. |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 580 | * |
| 581 | * @cliexpar |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 582 | * Example of how to display all the IPv4 FIB tables: |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 583 | * @cliexstart{show ip fib} |
Billy McFall | ebb9a6a | 2016-10-17 11:35:32 -0400 | [diff] [blame] | 584 | * ipv4-VRF:0, fib_index 0, flow hash: src dst sport dport proto |
| 585 | * 0.0.0.0/0 |
| 586 | * unicast-ip4-chain |
| 587 | * [@0]: dpo-load-balance: [index:0 buckets:1 uRPF:0 to:[0:0]] |
| 588 | * [0] [@0]: dpo-drop ip6 |
| 589 | * 0.0.0.0/32 |
| 590 | * unicast-ip4-chain |
| 591 | * [@0]: dpo-load-balance: [index:1 buckets:1 uRPF:1 to:[0:0]] |
| 592 | * [0] [@0]: dpo-drop ip6 |
| 593 | * 6.0.1.2/32 |
| 594 | * unicast-ip4-chain |
| 595 | * [@0]: dpo-load-balance: [index:30 buckets:1 uRPF:29 to:[0:0]] |
| 596 | * [0] [@3]: arp-ipv4: via 6.0.0.1 af_packet0 |
| 597 | * 7.0.0.1/32 |
| 598 | * unicast-ip4-chain |
| 599 | * [@0]: dpo-load-balance: [index:31 buckets:4 uRPF:30 to:[0:0]] |
| 600 | * [0] [@3]: arp-ipv4: via 6.0.0.2 af_packet0 |
| 601 | * [1] [@3]: arp-ipv4: via 6.0.0.2 af_packet0 |
| 602 | * [2] [@3]: arp-ipv4: via 6.0.0.2 af_packet0 |
| 603 | * [3] [@3]: arp-ipv4: via 6.0.0.1 af_packet0 |
| 604 | * 224.0.0.0/8 |
| 605 | * unicast-ip4-chain |
| 606 | * [@0]: dpo-load-balance: [index:3 buckets:1 uRPF:3 to:[0:0]] |
| 607 | * [0] [@0]: dpo-drop ip6 |
| 608 | * 240.0.0.0/8 |
| 609 | * unicast-ip4-chain |
| 610 | * [@0]: dpo-load-balance: [index:2 buckets:1 uRPF:2 to:[0:0]] |
| 611 | * [0] [@0]: dpo-drop ip6 |
| 612 | * 255.255.255.255/32 |
| 613 | * unicast-ip4-chain |
| 614 | * [@0]: dpo-load-balance: [index:4 buckets:1 uRPF:4 to:[0:0]] |
| 615 | * [0] [@0]: dpo-drop ip6 |
| 616 | * ipv4-VRF:7, fib_index 1, flow hash: src dst sport dport proto |
| 617 | * 0.0.0.0/0 |
| 618 | * unicast-ip4-chain |
| 619 | * [@0]: dpo-load-balance: [index:12 buckets:1 uRPF:11 to:[0:0]] |
| 620 | * [0] [@0]: dpo-drop ip6 |
| 621 | * 0.0.0.0/32 |
| 622 | * unicast-ip4-chain |
| 623 | * [@0]: dpo-load-balance: [index:13 buckets:1 uRPF:12 to:[0:0]] |
| 624 | * [0] [@0]: dpo-drop ip6 |
| 625 | * 172.16.1.0/24 |
| 626 | * unicast-ip4-chain |
| 627 | * [@0]: dpo-load-balance: [index:17 buckets:1 uRPF:16 to:[0:0]] |
| 628 | * [0] [@4]: ipv4-glean: af_packet0 |
| 629 | * 172.16.1.1/32 |
| 630 | * unicast-ip4-chain |
| 631 | * [@0]: dpo-load-balance: [index:18 buckets:1 uRPF:17 to:[1:84]] |
| 632 | * [0] [@2]: dpo-receive: 172.16.1.1 on af_packet0 |
| 633 | * 172.16.1.2/32 |
| 634 | * unicast-ip4-chain |
| 635 | * [@0]: dpo-load-balance: [index:21 buckets:1 uRPF:20 to:[0:0]] |
| 636 | * [0] [@5]: ipv4 via 172.16.1.2 af_packet0: IP4: 02:fe:9e:70:7a:2b -> 26:a5:f6:9c:3a:36 |
| 637 | * 172.16.2.0/24 |
| 638 | * unicast-ip4-chain |
| 639 | * [@0]: dpo-load-balance: [index:19 buckets:1 uRPF:18 to:[0:0]] |
| 640 | * [0] [@4]: ipv4-glean: af_packet1 |
| 641 | * 172.16.2.1/32 |
| 642 | * unicast-ip4-chain |
| 643 | * [@0]: dpo-load-balance: [index:20 buckets:1 uRPF:19 to:[0:0]] |
| 644 | * [0] [@2]: dpo-receive: 172.16.2.1 on af_packet1 |
| 645 | * 224.0.0.0/8 |
| 646 | * unicast-ip4-chain |
| 647 | * [@0]: dpo-load-balance: [index:15 buckets:1 uRPF:14 to:[0:0]] |
| 648 | * [0] [@0]: dpo-drop ip6 |
| 649 | * 240.0.0.0/8 |
| 650 | * unicast-ip4-chain |
| 651 | * [@0]: dpo-load-balance: [index:14 buckets:1 uRPF:13 to:[0:0]] |
| 652 | * [0] [@0]: dpo-drop ip6 |
| 653 | * 255.255.255.255/32 |
| 654 | * unicast-ip4-chain |
| 655 | * [@0]: dpo-load-balance: [index:16 buckets:1 uRPF:15 to:[0:0]] |
| 656 | * [0] [@0]: dpo-drop ip6 |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 657 | * @cliexend |
| 658 | * Example of how to display a single IPv4 FIB table: |
| 659 | * @cliexstart{show ip fib table 7} |
Billy McFall | ebb9a6a | 2016-10-17 11:35:32 -0400 | [diff] [blame] | 660 | * ipv4-VRF:7, fib_index 1, flow hash: src dst sport dport proto |
| 661 | * 0.0.0.0/0 |
| 662 | * unicast-ip4-chain |
| 663 | * [@0]: dpo-load-balance: [index:12 buckets:1 uRPF:11 to:[0:0]] |
| 664 | * [0] [@0]: dpo-drop ip6 |
| 665 | * 0.0.0.0/32 |
| 666 | * unicast-ip4-chain |
| 667 | * [@0]: dpo-load-balance: [index:13 buckets:1 uRPF:12 to:[0:0]] |
| 668 | * [0] [@0]: dpo-drop ip6 |
| 669 | * 172.16.1.0/24 |
| 670 | * unicast-ip4-chain |
| 671 | * [@0]: dpo-load-balance: [index:17 buckets:1 uRPF:16 to:[0:0]] |
| 672 | * [0] [@4]: ipv4-glean: af_packet0 |
| 673 | * 172.16.1.1/32 |
| 674 | * unicast-ip4-chain |
| 675 | * [@0]: dpo-load-balance: [index:18 buckets:1 uRPF:17 to:[1:84]] |
| 676 | * [0] [@2]: dpo-receive: 172.16.1.1 on af_packet0 |
| 677 | * 172.16.1.2/32 |
| 678 | * unicast-ip4-chain |
| 679 | * [@0]: dpo-load-balance: [index:21 buckets:1 uRPF:20 to:[0:0]] |
| 680 | * [0] [@5]: ipv4 via 172.16.1.2 af_packet0: IP4: 02:fe:9e:70:7a:2b -> 26:a5:f6:9c:3a:36 |
| 681 | * 172.16.2.0/24 |
| 682 | * unicast-ip4-chain |
| 683 | * [@0]: dpo-load-balance: [index:19 buckets:1 uRPF:18 to:[0:0]] |
| 684 | * [0] [@4]: ipv4-glean: af_packet1 |
| 685 | * 172.16.2.1/32 |
| 686 | * unicast-ip4-chain |
| 687 | * [@0]: dpo-load-balance: [index:20 buckets:1 uRPF:19 to:[0:0]] |
| 688 | * [0] [@2]: dpo-receive: 172.16.2.1 on af_packet1 |
| 689 | * 224.0.0.0/8 |
| 690 | * unicast-ip4-chain |
| 691 | * [@0]: dpo-load-balance: [index:15 buckets:1 uRPF:14 to:[0:0]] |
| 692 | * [0] [@0]: dpo-drop ip6 |
| 693 | * 240.0.0.0/8 |
| 694 | * unicast-ip4-chain |
| 695 | * [@0]: dpo-load-balance: [index:14 buckets:1 uRPF:13 to:[0:0]] |
| 696 | * [0] [@0]: dpo-drop ip6 |
| 697 | * 255.255.255.255/32 |
| 698 | * unicast-ip4-chain |
| 699 | * [@0]: dpo-load-balance: [index:16 buckets:1 uRPF:15 to:[0:0]] |
| 700 | * [0] [@0]: dpo-drop ip6 |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 701 | * @cliexend |
| 702 | * Example of how to display a summary of all IPv4 FIB tables: |
| 703 | * @cliexstart{show ip fib summary} |
Billy McFall | ebb9a6a | 2016-10-17 11:35:32 -0400 | [diff] [blame] | 704 | * 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] | 705 | * Prefix length Count |
Billy McFall | ebb9a6a | 2016-10-17 11:35:32 -0400 | [diff] [blame] | 706 | * 0 1 |
| 707 | * 8 2 |
| 708 | * 32 4 |
| 709 | * 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] | 710 | * Prefix length Count |
Billy McFall | ebb9a6a | 2016-10-17 11:35:32 -0400 | [diff] [blame] | 711 | * 0 1 |
| 712 | * 8 2 |
| 713 | * 24 2 |
| 714 | * 32 4 |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 715 | * @cliexend |
| 716 | ?*/ |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 717 | /* *INDENT-OFF* */ |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 718 | VLIB_CLI_COMMAND (ip4_show_fib_command, static) = { |
| 719 | .path = "show ip fib", |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 720 | .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] | 721 | .function = ip4_show_fib, |
| 722 | }; |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 723 | /* *INDENT-ON* */ |