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