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/ip6_fib.h> |
| 17 | #include <vnet/fib/fib_table.h> |
| 18 | |
| 19 | static void |
| 20 | vnet_ip6_fib_init (u32 fib_index) |
| 21 | { |
| 22 | fib_prefix_t pfx = { |
| 23 | .fp_proto = FIB_PROTOCOL_IP6, |
| 24 | .fp_len = 0, |
| 25 | .fp_addr = { |
| 26 | .ip6 = { |
| 27 | { 0, 0, }, |
| 28 | }, |
| 29 | } |
| 30 | }; |
| 31 | |
| 32 | /* |
| 33 | * Add the default route. |
| 34 | */ |
| 35 | fib_table_entry_special_add(fib_index, |
| 36 | &pfx, |
| 37 | FIB_SOURCE_DEFAULT_ROUTE, |
Neale Ranns | a055830 | 2017-04-13 00:44:52 -0700 | [diff] [blame] | 38 | FIB_ENTRY_FLAG_DROP); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 39 | |
| 40 | /* |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 41 | * all link local for us |
| 42 | */ |
| 43 | pfx.fp_addr.ip6.as_u64[0] = clib_host_to_net_u64 (0xFE80000000000000ULL); |
| 44 | pfx.fp_addr.ip6.as_u64[1] = 0; |
| 45 | pfx.fp_len = 10; |
| 46 | fib_table_entry_special_add(fib_index, |
| 47 | &pfx, |
| 48 | FIB_SOURCE_SPECIAL, |
Neale Ranns | a055830 | 2017-04-13 00:44:52 -0700 | [diff] [blame] | 49 | FIB_ENTRY_FLAG_LOCAL); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | static u32 |
| 53 | create_fib_with_table_id (u32 table_id) |
| 54 | { |
| 55 | fib_table_t *fib_table; |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 56 | ip6_fib_t *v6_fib; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 57 | |
| 58 | pool_get_aligned(ip6_main.fibs, fib_table, CLIB_CACHE_LINE_BYTES); |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 59 | pool_get_aligned(ip6_main.v6_fibs, v6_fib, CLIB_CACHE_LINE_BYTES); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 60 | |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 61 | memset(fib_table, 0, sizeof(*fib_table)); |
| 62 | memset(v6_fib, 0, sizeof(*v6_fib)); |
| 63 | |
| 64 | ASSERT((fib_table - ip6_main.fibs) == |
| 65 | (v6_fib - ip6_main.v6_fibs)); |
| 66 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 67 | fib_table->ft_proto = FIB_PROTOCOL_IP6; |
| 68 | fib_table->ft_index = |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 69 | v6_fib->index = |
| 70 | (fib_table - ip6_main.fibs); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 71 | |
| 72 | hash_set(ip6_main.fib_index_by_table_id, table_id, fib_table->ft_index); |
| 73 | |
| 74 | fib_table->ft_table_id = |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 75 | v6_fib->table_id = |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 76 | table_id; |
Neale Ranns | 227038a | 2017-04-21 01:07:59 -0700 | [diff] [blame] | 77 | fib_table->ft_flow_hash_config = IP_FLOW_HASH_DEFAULT; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 78 | |
| 79 | vnet_ip6_fib_init(fib_table->ft_index); |
| 80 | fib_table_lock(fib_table->ft_index, FIB_PROTOCOL_IP6); |
| 81 | |
| 82 | return (fib_table->ft_index); |
| 83 | } |
| 84 | |
| 85 | u32 |
| 86 | ip6_fib_table_find_or_create_and_lock (u32 table_id) |
| 87 | { |
| 88 | uword * p; |
| 89 | |
| 90 | p = hash_get (ip6_main.fib_index_by_table_id, table_id); |
| 91 | if (NULL == p) |
| 92 | return create_fib_with_table_id(table_id); |
| 93 | |
| 94 | fib_table_lock(p[0], FIB_PROTOCOL_IP6); |
| 95 | |
| 96 | return (p[0]); |
| 97 | } |
| 98 | |
| 99 | u32 |
| 100 | ip6_fib_table_create_and_lock (void) |
| 101 | { |
| 102 | return (create_fib_with_table_id(~0)); |
| 103 | } |
| 104 | |
| 105 | void |
| 106 | ip6_fib_table_destroy (u32 fib_index) |
| 107 | { |
| 108 | fib_prefix_t pfx = { |
| 109 | .fp_proto = FIB_PROTOCOL_IP6, |
| 110 | .fp_len = 0, |
| 111 | .fp_addr = { |
| 112 | .ip6 = { |
| 113 | { 0, 0, }, |
| 114 | }, |
| 115 | } |
| 116 | }; |
| 117 | |
| 118 | /* |
| 119 | * the default route. |
| 120 | */ |
| 121 | fib_table_entry_special_remove(fib_index, |
| 122 | &pfx, |
| 123 | FIB_SOURCE_DEFAULT_ROUTE); |
| 124 | |
| 125 | |
| 126 | /* |
| 127 | * ff02::1:ff00:0/104 |
| 128 | */ |
| 129 | ip6_set_solicited_node_multicast_address(&pfx.fp_addr.ip6, 0); |
| 130 | pfx.fp_len = 104; |
| 131 | fib_table_entry_special_remove(fib_index, |
| 132 | &pfx, |
| 133 | FIB_SOURCE_SPECIAL); |
| 134 | |
| 135 | /* |
| 136 | * all-routers multicast address |
| 137 | */ |
| 138 | ip6_set_reserved_multicast_address (&pfx.fp_addr.ip6, |
| 139 | IP6_MULTICAST_SCOPE_link_local, |
| 140 | IP6_MULTICAST_GROUP_ID_all_routers); |
| 141 | pfx.fp_len = 128; |
| 142 | fib_table_entry_special_remove(fib_index, |
| 143 | &pfx, |
| 144 | FIB_SOURCE_SPECIAL); |
| 145 | |
| 146 | /* |
| 147 | * all-nodes multicast address |
| 148 | */ |
| 149 | ip6_set_reserved_multicast_address (&pfx.fp_addr.ip6, |
| 150 | IP6_MULTICAST_SCOPE_link_local, |
| 151 | IP6_MULTICAST_GROUP_ID_all_hosts); |
| 152 | pfx.fp_len = 128; |
| 153 | fib_table_entry_special_remove(fib_index, |
| 154 | &pfx, |
| 155 | FIB_SOURCE_SPECIAL); |
| 156 | |
| 157 | /* |
| 158 | * all-mldv2 multicast address |
| 159 | */ |
| 160 | ip6_set_reserved_multicast_address (&pfx.fp_addr.ip6, |
| 161 | IP6_MULTICAST_SCOPE_link_local, |
| 162 | IP6_MULTICAST_GROUP_ID_mldv2_routers); |
| 163 | pfx.fp_len = 128; |
| 164 | fib_table_entry_special_remove(fib_index, |
| 165 | &pfx, |
| 166 | FIB_SOURCE_SPECIAL); |
| 167 | |
| 168 | /* |
| 169 | * all link local |
| 170 | */ |
| 171 | pfx.fp_addr.ip6.as_u64[0] = clib_host_to_net_u64 (0xFE80000000000000ULL); |
| 172 | pfx.fp_addr.ip6.as_u64[1] = 0; |
| 173 | pfx.fp_len = 10; |
| 174 | fib_table_entry_special_remove(fib_index, |
| 175 | &pfx, |
| 176 | FIB_SOURCE_SPECIAL); |
| 177 | |
| 178 | fib_table_t *fib_table = fib_table_get(fib_index, FIB_PROTOCOL_IP6); |
| 179 | fib_source_t source; |
| 180 | |
| 181 | /* |
| 182 | * validate no more routes. |
| 183 | */ |
| 184 | ASSERT(0 == fib_table->ft_total_route_counts); |
| 185 | FOR_EACH_FIB_SOURCE(source) |
| 186 | { |
| 187 | ASSERT(0 == fib_table->ft_src_route_counts[source]); |
| 188 | } |
| 189 | |
| 190 | if (~0 != fib_table->ft_table_id) |
| 191 | { |
| 192 | hash_unset (ip6_main.fib_index_by_table_id, fib_table->ft_table_id); |
| 193 | } |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 194 | pool_put_index(ip6_main.v6_fibs, fib_table->ft_index); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 195 | pool_put(ip6_main.fibs, fib_table); |
| 196 | } |
| 197 | |
| 198 | fib_node_index_t |
| 199 | ip6_fib_table_lookup (u32 fib_index, |
| 200 | const ip6_address_t *addr, |
| 201 | u32 len) |
| 202 | { |
| 203 | const ip6_fib_table_instance_t *table; |
| 204 | BVT(clib_bihash_kv) kv, value; |
| 205 | int i, n_p, rv; |
| 206 | u64 fib; |
| 207 | |
| 208 | table = &ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING]; |
| 209 | n_p = vec_len (table->prefix_lengths_in_search_order); |
| 210 | |
| 211 | kv.key[0] = addr->as_u64[0]; |
| 212 | kv.key[1] = addr->as_u64[1]; |
| 213 | fib = ((u64)((fib_index))<<32); |
| 214 | |
| 215 | /* |
| 216 | * start search from a mask length same length or shorter. |
| 217 | * we don't want matches longer than the mask passed |
| 218 | */ |
| 219 | i = 0; |
| 220 | while (i < n_p && table->prefix_lengths_in_search_order[i] > len) |
| 221 | { |
| 222 | i++; |
| 223 | } |
| 224 | |
| 225 | for (; i < n_p; i++) |
| 226 | { |
| 227 | int dst_address_length = table->prefix_lengths_in_search_order[i]; |
| 228 | ip6_address_t * mask = &ip6_main.fib_masks[dst_address_length]; |
| 229 | |
| 230 | ASSERT(dst_address_length >= 0 && dst_address_length <= 128); |
| 231 | //As lengths are decreasing, masks are increasingly specific. |
| 232 | kv.key[0] &= mask->as_u64[0]; |
| 233 | kv.key[1] &= mask->as_u64[1]; |
| 234 | kv.key[2] = fib | dst_address_length; |
| 235 | |
| 236 | rv = BV(clib_bihash_search_inline_2)(&table->ip6_hash, &kv, &value); |
| 237 | if (rv == 0) |
| 238 | return value.value; |
| 239 | } |
| 240 | |
| 241 | return (FIB_NODE_INDEX_INVALID); |
| 242 | } |
| 243 | |
| 244 | fib_node_index_t |
| 245 | ip6_fib_table_lookup_exact_match (u32 fib_index, |
| 246 | const ip6_address_t *addr, |
| 247 | u32 len) |
| 248 | { |
| 249 | const ip6_fib_table_instance_t *table; |
| 250 | BVT(clib_bihash_kv) kv, value; |
| 251 | ip6_address_t *mask; |
| 252 | u64 fib; |
| 253 | int rv; |
| 254 | |
| 255 | table = &ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING]; |
| 256 | mask = &ip6_main.fib_masks[len]; |
| 257 | fib = ((u64)((fib_index))<<32); |
| 258 | |
| 259 | kv.key[0] = addr->as_u64[0] & mask->as_u64[0]; |
| 260 | kv.key[1] = addr->as_u64[1] & mask->as_u64[1]; |
| 261 | kv.key[2] = fib | len; |
| 262 | |
| 263 | rv = BV(clib_bihash_search_inline_2)(&table->ip6_hash, &kv, &value); |
| 264 | if (rv == 0) |
| 265 | return value.value; |
| 266 | |
| 267 | return (FIB_NODE_INDEX_INVALID); |
| 268 | } |
| 269 | |
| 270 | static void |
| 271 | compute_prefix_lengths_in_search_order (ip6_fib_table_instance_t *table) |
| 272 | { |
| 273 | int i; |
| 274 | vec_reset_length (table->prefix_lengths_in_search_order); |
| 275 | /* Note: bitmap reversed so this is in fact a longest prefix match */ |
| 276 | clib_bitmap_foreach (i, table->non_empty_dst_address_length_bitmap, |
| 277 | ({ |
| 278 | int dst_address_length = 128 - i; |
| 279 | vec_add1(table->prefix_lengths_in_search_order, dst_address_length); |
| 280 | })); |
| 281 | } |
| 282 | |
| 283 | void |
| 284 | ip6_fib_table_entry_remove (u32 fib_index, |
| 285 | const ip6_address_t *addr, |
| 286 | u32 len) |
| 287 | { |
| 288 | ip6_fib_table_instance_t *table; |
| 289 | BVT(clib_bihash_kv) kv; |
| 290 | ip6_address_t *mask; |
| 291 | u64 fib; |
| 292 | |
| 293 | table = &ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING]; |
| 294 | mask = &ip6_main.fib_masks[len]; |
| 295 | fib = ((u64)((fib_index))<<32); |
| 296 | |
| 297 | kv.key[0] = addr->as_u64[0] & mask->as_u64[0]; |
| 298 | kv.key[1] = addr->as_u64[1] & mask->as_u64[1]; |
| 299 | kv.key[2] = fib | len; |
| 300 | |
| 301 | BV(clib_bihash_add_del)(&table->ip6_hash, &kv, 0); |
| 302 | |
| 303 | /* refcount accounting */ |
| 304 | ASSERT (table->dst_address_length_refcounts[len] > 0); |
| 305 | if (--table->dst_address_length_refcounts[len] == 0) |
| 306 | { |
| 307 | table->non_empty_dst_address_length_bitmap = |
| 308 | clib_bitmap_set (table->non_empty_dst_address_length_bitmap, |
| 309 | 128 - len, 0); |
| 310 | compute_prefix_lengths_in_search_order (table); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | void |
| 315 | ip6_fib_table_entry_insert (u32 fib_index, |
| 316 | const ip6_address_t *addr, |
| 317 | u32 len, |
| 318 | fib_node_index_t fib_entry_index) |
| 319 | { |
| 320 | ip6_fib_table_instance_t *table; |
| 321 | BVT(clib_bihash_kv) kv; |
| 322 | ip6_address_t *mask; |
| 323 | u64 fib; |
| 324 | |
| 325 | table = &ip6_main.ip6_table[IP6_FIB_TABLE_NON_FWDING]; |
| 326 | mask = &ip6_main.fib_masks[len]; |
| 327 | fib = ((u64)((fib_index))<<32); |
| 328 | |
| 329 | kv.key[0] = addr->as_u64[0] & mask->as_u64[0]; |
| 330 | kv.key[1] = addr->as_u64[1] & mask->as_u64[1]; |
| 331 | kv.key[2] = fib | len; |
| 332 | kv.value = fib_entry_index; |
| 333 | |
| 334 | BV(clib_bihash_add_del)(&table->ip6_hash, &kv, 1); |
| 335 | |
| 336 | table->dst_address_length_refcounts[len]++; |
| 337 | |
| 338 | table->non_empty_dst_address_length_bitmap = |
| 339 | clib_bitmap_set (table->non_empty_dst_address_length_bitmap, |
| 340 | 128 - len, 1); |
| 341 | compute_prefix_lengths_in_search_order (table); |
| 342 | } |
| 343 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 344 | u32 ip6_fib_table_fwding_lookup_with_if_index (ip6_main_t * im, |
| 345 | u32 sw_if_index, |
| 346 | const ip6_address_t * dst) |
| 347 | { |
| 348 | u32 fib_index = vec_elt (im->fib_index_by_sw_if_index, sw_if_index); |
| 349 | return ip6_fib_table_fwding_lookup(im, fib_index, dst); |
| 350 | } |
| 351 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 352 | u32 |
| 353 | ip6_fib_table_get_index_for_sw_if_index (u32 sw_if_index) |
| 354 | { |
| 355 | if (sw_if_index >= vec_len(ip6_main.fib_index_by_sw_if_index)) |
| 356 | { |
| 357 | /* |
| 358 | * This is the case for interfaces that are not yet mapped to |
| 359 | * a IP table |
| 360 | */ |
| 361 | return (~0); |
| 362 | } |
| 363 | return (ip6_main.fib_index_by_sw_if_index[sw_if_index]); |
| 364 | } |
| 365 | |
| 366 | void |
| 367 | ip6_fib_table_fwding_dpo_update (u32 fib_index, |
| 368 | const ip6_address_t *addr, |
| 369 | u32 len, |
| 370 | const dpo_id_t *dpo) |
| 371 | { |
| 372 | ip6_fib_table_instance_t *table; |
| 373 | BVT(clib_bihash_kv) kv; |
| 374 | ip6_address_t *mask; |
| 375 | u64 fib; |
| 376 | |
| 377 | table = &ip6_main.ip6_table[IP6_FIB_TABLE_FWDING]; |
| 378 | mask = &ip6_main.fib_masks[len]; |
| 379 | fib = ((u64)((fib_index))<<32); |
| 380 | |
| 381 | kv.key[0] = addr->as_u64[0] & mask->as_u64[0]; |
| 382 | kv.key[1] = addr->as_u64[1] & mask->as_u64[1]; |
| 383 | kv.key[2] = fib | len; |
| 384 | kv.value = dpo->dpoi_index; |
| 385 | |
| 386 | BV(clib_bihash_add_del)(&table->ip6_hash, &kv, 1); |
| 387 | |
| 388 | table->dst_address_length_refcounts[len]++; |
| 389 | |
| 390 | table->non_empty_dst_address_length_bitmap = |
| 391 | clib_bitmap_set (table->non_empty_dst_address_length_bitmap, |
| 392 | 128 - len, 1); |
| 393 | compute_prefix_lengths_in_search_order (table); |
| 394 | } |
| 395 | |
| 396 | void |
| 397 | ip6_fib_table_fwding_dpo_remove (u32 fib_index, |
| 398 | const ip6_address_t *addr, |
| 399 | u32 len, |
| 400 | const dpo_id_t *dpo) |
| 401 | { |
| 402 | ip6_fib_table_instance_t *table; |
| 403 | BVT(clib_bihash_kv) kv; |
| 404 | ip6_address_t *mask; |
| 405 | u64 fib; |
| 406 | |
| 407 | table = &ip6_main.ip6_table[IP6_FIB_TABLE_FWDING]; |
| 408 | mask = &ip6_main.fib_masks[len]; |
| 409 | fib = ((u64)((fib_index))<<32); |
| 410 | |
| 411 | kv.key[0] = addr->as_u64[0] & mask->as_u64[0]; |
| 412 | kv.key[1] = addr->as_u64[1] & mask->as_u64[1]; |
| 413 | kv.key[2] = fib | len; |
| 414 | kv.value = dpo->dpoi_index; |
| 415 | |
| 416 | BV(clib_bihash_add_del)(&table->ip6_hash, &kv, 0); |
| 417 | |
| 418 | /* refcount accounting */ |
| 419 | ASSERT (table->dst_address_length_refcounts[len] > 0); |
| 420 | if (--table->dst_address_length_refcounts[len] == 0) |
| 421 | { |
| 422 | table->non_empty_dst_address_length_bitmap = |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 423 | clib_bitmap_set (table->non_empty_dst_address_length_bitmap, |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 424 | 128 - len, 0); |
| 425 | compute_prefix_lengths_in_search_order (table); |
| 426 | } |
| 427 | } |
| 428 | |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 429 | /** |
| 430 | * @brief Context when walking the IPv6 table. Since all VRFs are in the |
| 431 | * same hash table, we need to filter only those we need as we walk |
| 432 | */ |
| 433 | typedef struct ip6_fib_walk_ctx_t_ |
| 434 | { |
| 435 | u32 i6w_fib_index; |
| 436 | fib_table_walk_fn_t i6w_fn; |
| 437 | void *i6w_ctx; |
| 438 | } ip6_fib_walk_ctx_t; |
| 439 | |
| 440 | static int |
| 441 | ip6_fib_walk_cb (clib_bihash_kv_24_8_t * kvp, |
| 442 | void *arg) |
| 443 | { |
| 444 | ip6_fib_walk_ctx_t *ctx = arg; |
| 445 | |
| 446 | if ((kvp->key[2] >> 32) == ctx->i6w_fib_index) |
| 447 | { |
| 448 | ctx->i6w_fn(kvp->value, ctx->i6w_ctx); |
| 449 | } |
| 450 | |
| 451 | return (1); |
| 452 | } |
| 453 | |
| 454 | void |
| 455 | ip6_fib_table_walk (u32 fib_index, |
| 456 | fib_table_walk_fn_t fn, |
| 457 | void *arg) |
| 458 | { |
| 459 | ip6_fib_walk_ctx_t ctx = { |
| 460 | .i6w_fib_index = fib_index, |
| 461 | .i6w_fn = fn, |
| 462 | .i6w_ctx = arg, |
| 463 | }; |
| 464 | ip6_main_t *im = &ip6_main; |
| 465 | |
| 466 | BV(clib_bihash_foreach_key_value_pair)(&im->ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash, |
| 467 | ip6_fib_walk_cb, |
| 468 | &ctx); |
| 469 | |
| 470 | } |
| 471 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 472 | typedef struct ip6_fib_show_ctx_t_ { |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 473 | fib_node_index_t *entries; |
| 474 | } ip6_fib_show_ctx_t; |
| 475 | |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 476 | static int |
| 477 | ip6_fib_table_show_walk (fib_node_index_t fib_entry_index, |
| 478 | void *arg) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 479 | { |
| 480 | ip6_fib_show_ctx_t *ctx = arg; |
| 481 | |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 482 | vec_add1(ctx->entries, fib_entry_index); |
| 483 | |
| 484 | return (1); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | static void |
| 488 | ip6_fib_table_show_all (ip6_fib_t *fib, |
| 489 | vlib_main_t * vm) |
| 490 | { |
| 491 | fib_node_index_t *fib_entry_index; |
| 492 | ip6_fib_show_ctx_t ctx = { |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 493 | .entries = NULL, |
| 494 | }; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 495 | |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 496 | ip6_fib_table_walk(fib->index, ip6_fib_table_show_walk, &ctx); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 497 | vec_sort_with_function(ctx.entries, fib_entry_cmp_for_sort); |
| 498 | |
| 499 | vec_foreach(fib_entry_index, ctx.entries) |
| 500 | { |
| 501 | vlib_cli_output(vm, "%U", |
| 502 | format_fib_entry, |
| 503 | *fib_entry_index, |
| 504 | FIB_ENTRY_FORMAT_BRIEF); |
| 505 | } |
| 506 | |
| 507 | vec_free(ctx.entries); |
| 508 | } |
| 509 | |
| 510 | static void |
| 511 | ip6_fib_table_show_one (ip6_fib_t *fib, |
| 512 | vlib_main_t * vm, |
| 513 | ip6_address_t *address, |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 514 | u32 mask_len, |
| 515 | int detail) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 516 | { |
| 517 | vlib_cli_output(vm, "%U", |
| 518 | format_fib_entry, |
| 519 | ip6_fib_table_lookup(fib->index, address, mask_len), |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 520 | (detail ? |
| 521 | FIB_ENTRY_FORMAT_DETAIL2: |
| 522 | FIB_ENTRY_FORMAT_DETAIL)); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | typedef struct { |
| 526 | u32 fib_index; |
| 527 | u64 count_by_prefix_length[129]; |
| 528 | } count_routes_in_fib_at_prefix_length_arg_t; |
| 529 | |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 530 | static void |
| 531 | count_routes_in_fib_at_prefix_length (BVT(clib_bihash_kv) * kvp, |
| 532 | void *arg) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 533 | { |
| 534 | count_routes_in_fib_at_prefix_length_arg_t * ap = arg; |
| 535 | int mask_width; |
| 536 | |
| 537 | if ((kvp->key[2]>>32) != ap->fib_index) |
| 538 | return; |
| 539 | |
| 540 | mask_width = kvp->key[2] & 0xFF; |
| 541 | |
| 542 | ap->count_by_prefix_length[mask_width]++; |
| 543 | } |
| 544 | |
| 545 | static clib_error_t * |
| 546 | ip6_show_fib (vlib_main_t * vm, |
| 547 | unformat_input_t * input, |
| 548 | vlib_cli_command_t * cmd) |
| 549 | { |
| 550 | count_routes_in_fib_at_prefix_length_arg_t _ca, *ca = &_ca; |
| 551 | ip6_main_t * im6 = &ip6_main; |
| 552 | fib_table_t *fib_table; |
| 553 | ip6_fib_t * fib; |
| 554 | int verbose, matching; |
| 555 | ip6_address_t matching_address; |
| 556 | u32 mask_len = 128; |
| 557 | int table_id = -1, fib_index = ~0; |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 558 | int detail = 0; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 559 | |
| 560 | verbose = 1; |
| 561 | matching = 0; |
| 562 | |
| 563 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 564 | { |
| 565 | if (unformat (input, "brief") || |
| 566 | unformat (input, "summary") || |
| 567 | unformat (input, "sum")) |
| 568 | verbose = 0; |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 569 | |
| 570 | else if (unformat (input, "detail") || |
| 571 | unformat (input, "det")) |
| 572 | detail = 1; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 573 | |
| 574 | else if (unformat (input, "%U/%d", |
| 575 | unformat_ip6_address, &matching_address, &mask_len)) |
| 576 | matching = 1; |
| 577 | |
| 578 | else if (unformat (input, "%U", unformat_ip6_address, &matching_address)) |
| 579 | matching = 1; |
| 580 | |
| 581 | else if (unformat (input, "table %d", &table_id)) |
| 582 | ; |
| 583 | else if (unformat (input, "index %d", &fib_index)) |
| 584 | ; |
| 585 | else |
| 586 | break; |
| 587 | } |
| 588 | |
| 589 | pool_foreach (fib_table, im6->fibs, |
| 590 | ({ |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 591 | fib = pool_elt_at_index(im6->v6_fibs, fib_table->ft_index); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 592 | if (table_id >= 0 && table_id != (int)fib->table_id) |
| 593 | continue; |
| 594 | if (fib_index != ~0 && fib_index != (int)fib->index) |
| 595 | continue; |
| 596 | |
Neale Ranns | 13eaf3e | 2017-05-23 06:10:33 -0700 | [diff] [blame] | 597 | vlib_cli_output (vm, "%s, fib_index:%d, flow hash:[%U] locks:%d", |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 598 | fib_table->ft_desc, fib->index, |
Neale Ranns | 13eaf3e | 2017-05-23 06:10:33 -0700 | [diff] [blame] | 599 | format_ip_flow_hash_config, fib_table->ft_flow_hash_config, |
| 600 | fib_table->ft_locks); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 601 | |
| 602 | /* Show summary? */ |
| 603 | if (! verbose) |
| 604 | { |
| 605 | BVT(clib_bihash) * h = &im6->ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash; |
| 606 | int len; |
| 607 | |
| 608 | vlib_cli_output (vm, "%=20s%=16s", "Prefix length", "Count"); |
| 609 | |
| 610 | memset (ca, 0, sizeof(*ca)); |
| 611 | ca->fib_index = fib->index; |
| 612 | |
| 613 | BV(clib_bihash_foreach_key_value_pair) |
| 614 | (h, count_routes_in_fib_at_prefix_length, ca); |
| 615 | |
| 616 | for (len = 128; len >= 0; len--) |
| 617 | { |
| 618 | if (ca->count_by_prefix_length[len]) |
| 619 | vlib_cli_output (vm, "%=20d%=16lld", |
| 620 | len, ca->count_by_prefix_length[len]); |
| 621 | } |
| 622 | continue; |
| 623 | } |
| 624 | |
| 625 | if (!matching) |
| 626 | { |
| 627 | ip6_fib_table_show_all(fib, vm); |
| 628 | } |
| 629 | else |
| 630 | { |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 631 | ip6_fib_table_show_one(fib, vm, &matching_address, mask_len, detail); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 632 | } |
| 633 | })); |
| 634 | |
| 635 | return 0; |
| 636 | } |
| 637 | |
| 638 | /*? |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 639 | * This command displays the IPv6 FIB Tables (VRF Tables) and the route |
| 640 | * entries for each table. |
| 641 | * |
| 642 | * @note This command will run for a long time when the FIB tables are |
| 643 | * comprised of millions of entries. For those senarios, consider displaying |
| 644 | * in summary mode. |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 645 | * |
| 646 | * @cliexpar |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 647 | * @parblock |
| 648 | * Example of how to display all the IPv6 FIB tables: |
| 649 | * @cliexstart{show ip6 fib} |
Billy McFall | ebb9a6a | 2016-10-17 11:35:32 -0400 | [diff] [blame] | 650 | * ipv6-VRF:0, fib_index 0, flow hash: src dst sport dport proto |
| 651 | * @::/0 |
| 652 | * unicast-ip6-chain |
| 653 | * [@0]: dpo-load-balance: [index:5 buckets:1 uRPF:5 to:[0:0]] |
| 654 | * [0] [@0]: dpo-drop ip6 |
| 655 | * fe80::/10 |
| 656 | * unicast-ip6-chain |
| 657 | * [@0]: dpo-load-balance: [index:10 buckets:1 uRPF:10 to:[0:0]] |
| 658 | * [0] [@2]: dpo-receive |
| 659 | * ff02::1/128 |
| 660 | * unicast-ip6-chain |
| 661 | * [@0]: dpo-load-balance: [index:8 buckets:1 uRPF:8 to:[0:0]] |
| 662 | * [0] [@2]: dpo-receive |
| 663 | * ff02::2/128 |
| 664 | * unicast-ip6-chain |
| 665 | * [@0]: dpo-load-balance: [index:7 buckets:1 uRPF:7 to:[0:0]] |
| 666 | * [0] [@2]: dpo-receive |
| 667 | * ff02::16/128 |
| 668 | * unicast-ip6-chain |
| 669 | * [@0]: dpo-load-balance: [index:9 buckets:1 uRPF:9 to:[0:0]] |
| 670 | * [0] [@2]: dpo-receive |
| 671 | * ff02::1:ff00:0/104 |
| 672 | * unicast-ip6-chain |
| 673 | * [@0]: dpo-load-balance: [index:6 buckets:1 uRPF:6 to:[0:0]] |
| 674 | * [0] [@2]: dpo-receive |
| 675 | * ipv6-VRF:8, fib_index 1, flow hash: src dst sport dport proto |
| 676 | * @::/0 |
| 677 | * unicast-ip6-chain |
| 678 | * [@0]: dpo-load-balance: [index:21 buckets:1 uRPF:20 to:[0:0]] |
| 679 | * [0] [@0]: dpo-drop ip6 |
| 680 | * @::a:1:1:0:4/126 |
| 681 | * unicast-ip6-chain |
| 682 | * [@0]: dpo-load-balance: [index:27 buckets:1 uRPF:26 to:[0:0]] |
| 683 | * [0] [@4]: ipv6-glean: af_packet0 |
| 684 | * @::a:1:1:0:7/128 |
| 685 | * unicast-ip6-chain |
| 686 | * [@0]: dpo-load-balance: [index:28 buckets:1 uRPF:27 to:[0:0]] |
| 687 | * [0] [@2]: dpo-receive: @::a:1:1:0:7 on af_packet0 |
| 688 | * fe80::/10 |
| 689 | * unicast-ip6-chain |
| 690 | * [@0]: dpo-load-balance: [index:26 buckets:1 uRPF:25 to:[0:0]] |
| 691 | * [0] [@2]: dpo-receive |
| 692 | * fe80::fe:3eff:fe3e:9222/128 |
| 693 | * unicast-ip6-chain |
| 694 | * [@0]: dpo-load-balance: [index:29 buckets:1 uRPF:28 to:[0:0]] |
| 695 | * [0] [@2]: dpo-receive: fe80::fe:3eff:fe3e:9222 on af_packet0 |
| 696 | * ff02::1/128 |
| 697 | * unicast-ip6-chain |
| 698 | * [@0]: dpo-load-balance: [index:24 buckets:1 uRPF:23 to:[0:0]] |
| 699 | * [0] [@2]: dpo-receive |
| 700 | * ff02::2/128 |
| 701 | * unicast-ip6-chain |
| 702 | * [@0]: dpo-load-balance: [index:23 buckets:1 uRPF:22 to:[0:0]] |
| 703 | * [0] [@2]: dpo-receive |
| 704 | * ff02::16/128 |
| 705 | * unicast-ip6-chain |
| 706 | * [@0]: dpo-load-balance: [index:25 buckets:1 uRPF:24 to:[0:0]] |
| 707 | * [0] [@2]: dpo-receive |
| 708 | * ff02::1:ff00:0/104 |
| 709 | * unicast-ip6-chain |
| 710 | * [@0]: dpo-load-balance: [index:22 buckets:1 uRPF:21 to:[0:0]] |
| 711 | * [0] [@2]: dpo-receive |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 712 | * @cliexend |
Billy McFall | ebb9a6a | 2016-10-17 11:35:32 -0400 | [diff] [blame] | 713 | * |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 714 | * Example of how to display a summary of all IPv6 FIB tables: |
| 715 | * @cliexstart{show ip6 fib summary} |
Billy McFall | ebb9a6a | 2016-10-17 11:35:32 -0400 | [diff] [blame] | 716 | * ipv6-VRF:0, fib_index 0, flow hash: src dst sport dport proto |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 717 | * Prefix length Count |
| 718 | * 128 3 |
| 719 | * 104 1 |
Billy McFall | ebb9a6a | 2016-10-17 11:35:32 -0400 | [diff] [blame] | 720 | * 10 1 |
| 721 | * 0 1 |
| 722 | * ipv6-VRF:8, fib_index 1, flow hash: src dst sport dport proto |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 723 | * Prefix length Count |
| 724 | * 128 5 |
| 725 | * 126 1 |
| 726 | * 104 1 |
Billy McFall | ebb9a6a | 2016-10-17 11:35:32 -0400 | [diff] [blame] | 727 | * 10 1 |
| 728 | * 0 1 |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 729 | * @cliexend |
| 730 | * @endparblock |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 731 | ?*/ |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 732 | /* *INDENT-OFF* */ |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 733 | VLIB_CLI_COMMAND (ip6_show_fib_command, static) = { |
| 734 | .path = "show ip6 fib", |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 735 | .short_help = "show ip6 fib [summary] [table <table-id>] [index <fib-id>] [<ip6-addr>[/<width>]] [detail]", |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 736 | .function = ip6_show_fib, |
| 737 | }; |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 738 | /* *INDENT-ON* */ |