Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 1 | /* |
| 2 | * src/vnet/ip/ip_neighboor.c: ip neighbor generic handling |
| 3 | * |
| 4 | * Copyright (c) 2018 Cisco and/or its affiliates. |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at: |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include <vppinfra/llist.h> |
| 19 | |
| 20 | #include <vnet/ip-neighbor/ip_neighbor.h> |
| 21 | #include <vnet/ip-neighbor/ip4_neighbor.h> |
| 22 | #include <vnet/ip-neighbor/ip6_neighbor.h> |
| 23 | #include <vnet/ip-neighbor/ip_neighbor_watch.h> |
| 24 | |
| 25 | #include <vnet/ip/ip6_ll_table.h> |
| 26 | #include <vnet/fib/fib_table.h> |
| 27 | #include <vnet/adj/adj_mcast.h> |
| 28 | |
| 29 | /** Pool for All IP neighbors */ |
| 30 | static ip_neighbor_t *ip_neighbor_pool; |
| 31 | |
| 32 | /** protocol specific lists of time sorted neighbors */ |
| 33 | index_t ip_neighbor_list_head[IP46_N_TYPES]; |
| 34 | |
| 35 | typedef struct ip_neighbor_elt_t_ |
| 36 | { |
| 37 | clib_llist_anchor_t ipne_anchor; |
| 38 | index_t ipne_index; |
| 39 | } ip_neighbor_elt_t; |
| 40 | |
| 41 | /** Pool of linked list elemeents */ |
| 42 | ip_neighbor_elt_t *ip_neighbor_elt_pool; |
| 43 | |
| 44 | typedef struct ip_neighbor_db_t_ |
| 45 | { |
| 46 | /** per interface hash */ |
| 47 | uword **ipndb_hash; |
| 48 | /** per-protocol limit - max number of neighbors*/ |
| 49 | u32 ipndb_limit; |
| 50 | /** max age of a neighbor before it's forcibly evicted */ |
| 51 | u32 ipndb_age; |
| 52 | /** when the limit is reached and new neighbors are created, should |
| 53 | * we recycle an old one */ |
| 54 | bool ipndb_recycle; |
| 55 | /** per-protocol number of elements */ |
| 56 | u32 ipndb_n_elts; |
| 57 | /** per-protocol number of elements per-fib-index*/ |
| 58 | u32 *ipndb_n_elts_per_fib; |
| 59 | } ip_neighbor_db_t; |
| 60 | |
| 61 | static vlib_log_class_t ipn_logger; |
| 62 | |
| 63 | /* DBs of neighbours one per AF */ |
| 64 | /* *INDENT-OFF* */ |
| 65 | static ip_neighbor_db_t ip_neighbor_db[IP46_N_TYPES] = { |
| 66 | [IP46_TYPE_IP4] = { |
| 67 | .ipndb_limit = 50000, |
| 68 | /* Default to not aging and not recycling */ |
| 69 | .ipndb_age = 0, |
| 70 | .ipndb_recycle = false, |
| 71 | }, |
| 72 | [IP46_TYPE_IP6] = { |
| 73 | .ipndb_limit = 50000, |
| 74 | /* Default to not aging and not recycling */ |
| 75 | .ipndb_age = 0, |
| 76 | .ipndb_recycle = false, |
| 77 | } |
| 78 | }; |
| 79 | /* *INDENT-ON* */ |
| 80 | |
| 81 | #define IP_NEIGHBOR_DBG(...) \ |
| 82 | vlib_log_debug (ipn_logger, __VA_ARGS__); |
| 83 | |
| 84 | #define IP_NEIGHBOR_INFO(...) \ |
| 85 | vlib_log_notice (ipn_logger, __VA_ARGS__); |
| 86 | |
| 87 | ip_neighbor_t * |
| 88 | ip_neighbor_get (index_t ipni) |
| 89 | { |
| 90 | if (pool_is_free_index (ip_neighbor_pool, ipni)) |
| 91 | return (NULL); |
| 92 | |
| 93 | return (pool_elt_at_index (ip_neighbor_pool, ipni)); |
| 94 | } |
| 95 | |
| 96 | static index_t |
| 97 | ip_neighbor_get_index (const ip_neighbor_t * ipn) |
| 98 | { |
| 99 | return (ipn - ip_neighbor_pool); |
| 100 | } |
| 101 | |
Neale Ranns | c87fbb4 | 2020-04-02 17:08:28 +0000 | [diff] [blame^] | 102 | static void |
| 103 | ip_neighbor_touch (ip_neighbor_t * ipn) |
| 104 | { |
| 105 | ipn->ipn_flags &= ~IP_NEIGHBOR_FLAG_STALE; |
| 106 | } |
| 107 | |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 108 | static bool |
| 109 | ip_neighbor_is_dynamic (const ip_neighbor_t * ipn) |
| 110 | { |
| 111 | return (ipn->ipn_flags & IP_NEIGHBOR_FLAG_DYNAMIC); |
| 112 | } |
| 113 | |
| 114 | const ip46_address_t * |
| 115 | ip_neighbor_get_ip (const ip_neighbor_t * ipn) |
| 116 | { |
| 117 | return (&ipn->ipn_key->ipnk_ip); |
| 118 | } |
| 119 | |
| 120 | const mac_address_t * |
| 121 | ip_neighbor_get_mac (const ip_neighbor_t * ipn) |
| 122 | { |
| 123 | return (&ipn->ipn_mac); |
| 124 | } |
| 125 | |
| 126 | const u32 |
| 127 | ip_neighbor_get_sw_if_index (const ip_neighbor_t * ipn) |
| 128 | { |
| 129 | return (ipn->ipn_key->ipnk_sw_if_index); |
| 130 | } |
| 131 | |
| 132 | static void |
| 133 | ip_neighbor_list_remove (ip_neighbor_t * ipn) |
| 134 | { |
| 135 | /* new neighbours, are added to the head of the list, since the |
| 136 | * list is time sorted, newest first */ |
| 137 | ip_neighbor_elt_t *elt; |
| 138 | |
| 139 | if (~0 != ipn->ipn_elt) |
| 140 | { |
| 141 | elt = pool_elt_at_index (ip_neighbor_elt_pool, ipn->ipn_elt); |
| 142 | |
| 143 | clib_llist_remove (ip_neighbor_elt_pool, ipne_anchor, elt); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | static void |
| 148 | ip_neighbor_refresh (ip_neighbor_t * ipn) |
| 149 | { |
| 150 | /* new neighbours, are added to the head of the list, since the |
| 151 | * list is time sorted, newest first */ |
| 152 | ip_neighbor_elt_t *elt, *head; |
| 153 | |
Neale Ranns | c87fbb4 | 2020-04-02 17:08:28 +0000 | [diff] [blame^] | 154 | ip_neighbor_touch (ipn); |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 155 | ipn->ipn_time_last_updated = vlib_time_now (vlib_get_main ()); |
| 156 | ipn->ipn_n_probes = 0; |
| 157 | |
| 158 | if (ip_neighbor_is_dynamic (ipn)) |
| 159 | { |
| 160 | if (~0 == ipn->ipn_elt) |
| 161 | /* first time insertion */ |
| 162 | pool_get_zero (ip_neighbor_elt_pool, elt); |
| 163 | else |
| 164 | { |
| 165 | /* already inserted - extract first */ |
| 166 | elt = pool_elt_at_index (ip_neighbor_elt_pool, ipn->ipn_elt); |
| 167 | |
| 168 | clib_llist_remove (ip_neighbor_elt_pool, ipne_anchor, elt); |
| 169 | } |
| 170 | head = pool_elt_at_index (ip_neighbor_elt_pool, |
| 171 | ip_neighbor_list_head[ipn-> |
| 172 | ipn_key->ipnk_type]); |
| 173 | |
| 174 | elt->ipne_index = ip_neighbor_get_index (ipn); |
| 175 | clib_llist_add (ip_neighbor_elt_pool, ipne_anchor, elt, head); |
| 176 | ipn->ipn_elt = elt - ip_neighbor_elt_pool; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | static void |
| 181 | ip_neighbor_db_add (const ip_neighbor_t * ipn) |
| 182 | { |
| 183 | vec_validate (ip_neighbor_db[ipn->ipn_key->ipnk_type].ipndb_hash, |
| 184 | ipn->ipn_key->ipnk_sw_if_index); |
| 185 | |
| 186 | if (!ip_neighbor_db[ipn->ipn_key->ipnk_type].ipndb_hash |
| 187 | [ipn->ipn_key->ipnk_sw_if_index]) |
| 188 | ip_neighbor_db[ipn->ipn_key->ipnk_type].ipndb_hash[ipn-> |
| 189 | ipn_key->ipnk_sw_if_index] |
| 190 | = hash_create_mem (0, sizeof (ip_neighbor_key_t), sizeof (index_t)); |
| 191 | |
| 192 | hash_set_mem (ip_neighbor_db[ipn->ipn_key->ipnk_type].ipndb_hash |
| 193 | [ipn->ipn_key->ipnk_sw_if_index], ipn->ipn_key, |
| 194 | ip_neighbor_get_index (ipn)); |
| 195 | |
| 196 | ip_neighbor_db[ipn->ipn_key->ipnk_type].ipndb_n_elts++; |
| 197 | } |
| 198 | |
| 199 | static void |
| 200 | ip_neighbor_db_remove (const ip_neighbor_key_t * key) |
| 201 | { |
| 202 | vec_validate (ip_neighbor_db[key->ipnk_type].ipndb_hash, |
| 203 | key->ipnk_sw_if_index); |
| 204 | |
| 205 | hash_unset_mem (ip_neighbor_db[key->ipnk_type].ipndb_hash |
| 206 | [key->ipnk_sw_if_index], key); |
| 207 | |
| 208 | ip_neighbor_db[key->ipnk_type].ipndb_n_elts--; |
| 209 | } |
| 210 | |
| 211 | static ip_neighbor_t * |
| 212 | ip_neighbor_db_find (const ip_neighbor_key_t * key) |
| 213 | { |
| 214 | uword *p; |
| 215 | |
| 216 | if (key->ipnk_sw_if_index >= |
| 217 | vec_len (ip_neighbor_db[key->ipnk_type].ipndb_hash)) |
| 218 | return NULL; |
| 219 | |
| 220 | p = |
| 221 | hash_get_mem (ip_neighbor_db[key->ipnk_type].ipndb_hash |
| 222 | [key->ipnk_sw_if_index], key); |
| 223 | |
| 224 | if (p) |
| 225 | return ip_neighbor_get (p[0]); |
| 226 | |
| 227 | return (NULL); |
| 228 | } |
| 229 | |
| 230 | static u8 |
| 231 | ip46_type_pfx_len (ip46_type_t type) |
| 232 | { |
| 233 | return (type == IP46_TYPE_IP4 ? 32 : 128); |
| 234 | } |
| 235 | |
| 236 | static void |
| 237 | ip_neighbor_adj_fib_add (ip_neighbor_t * ipn, u32 fib_index) |
| 238 | { |
| 239 | if (ipn->ipn_key->ipnk_type == IP46_TYPE_IP6 && |
| 240 | ip6_address_is_link_local_unicast (&ipn->ipn_key->ipnk_ip.ip6)) |
| 241 | { |
| 242 | ip6_ll_prefix_t pfx = { |
| 243 | .ilp_addr = ipn->ipn_key->ipnk_ip.ip6, |
| 244 | .ilp_sw_if_index = ipn->ipn_key->ipnk_sw_if_index, |
| 245 | }; |
| 246 | ipn->ipn_fib_entry_index = |
| 247 | ip6_ll_table_entry_update (&pfx, FIB_ROUTE_PATH_FLAG_NONE); |
| 248 | } |
| 249 | else |
| 250 | { |
| 251 | fib_protocol_t fproto; |
| 252 | |
| 253 | fproto = fib_proto_from_ip46 (ipn->ipn_key->ipnk_type); |
| 254 | |
| 255 | fib_prefix_t pfx = { |
| 256 | .fp_len = ip46_type_pfx_len (ipn->ipn_key->ipnk_type), |
| 257 | .fp_proto = fproto, |
| 258 | .fp_addr = ipn->ipn_key->ipnk_ip, |
| 259 | }; |
| 260 | |
| 261 | ipn->ipn_fib_entry_index = |
| 262 | fib_table_entry_path_add (fib_index, &pfx, FIB_SOURCE_ADJ, |
| 263 | FIB_ENTRY_FLAG_ATTACHED, |
| 264 | fib_proto_to_dpo (fproto), |
| 265 | &pfx.fp_addr, |
| 266 | ipn->ipn_key->ipnk_sw_if_index, |
| 267 | ~0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE); |
| 268 | |
| 269 | vec_validate (ip_neighbor_db |
| 270 | [ipn->ipn_key->ipnk_type].ipndb_n_elts_per_fib, |
| 271 | fib_index); |
| 272 | |
| 273 | ip_neighbor_db[ipn->ipn_key-> |
| 274 | ipnk_type].ipndb_n_elts_per_fib[fib_index]++; |
| 275 | |
| 276 | if (1 == |
| 277 | ip_neighbor_db[ipn->ipn_key-> |
| 278 | ipnk_type].ipndb_n_elts_per_fib[fib_index]) |
| 279 | fib_table_lock (fib_index, fproto, FIB_SOURCE_ADJ); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | static void |
| 284 | ip_neighbor_adj_fib_remove (ip_neighbor_t * ipn, u32 fib_index) |
| 285 | { |
| 286 | if (FIB_NODE_INDEX_INVALID != ipn->ipn_fib_entry_index) |
| 287 | { |
| 288 | if (ipn->ipn_key->ipnk_type == IP46_TYPE_IP6 && |
| 289 | ip6_address_is_link_local_unicast (&ipn->ipn_key->ipnk_ip.ip6)) |
| 290 | { |
| 291 | ip6_ll_prefix_t pfx = { |
| 292 | .ilp_addr = ipn->ipn_key->ipnk_ip.ip6, |
| 293 | .ilp_sw_if_index = ipn->ipn_key->ipnk_sw_if_index, |
| 294 | }; |
| 295 | ip6_ll_table_entry_delete (&pfx); |
| 296 | } |
| 297 | else |
| 298 | { |
| 299 | fib_protocol_t fproto; |
| 300 | |
| 301 | fproto = fib_proto_from_ip46 (ipn->ipn_key->ipnk_type); |
| 302 | |
| 303 | fib_prefix_t pfx = { |
| 304 | .fp_len = ip46_type_pfx_len (ipn->ipn_key->ipnk_type), |
| 305 | .fp_proto = fproto, |
| 306 | .fp_addr = ipn->ipn_key->ipnk_ip, |
| 307 | }; |
| 308 | |
| 309 | fib_table_entry_path_remove (fib_index, |
| 310 | &pfx, |
| 311 | FIB_SOURCE_ADJ, |
| 312 | fib_proto_to_dpo (fproto), |
| 313 | &pfx.fp_addr, |
| 314 | ipn->ipn_key->ipnk_sw_if_index, |
| 315 | ~0, 1, FIB_ROUTE_PATH_FLAG_NONE); |
| 316 | |
| 317 | ip_neighbor_db[ipn->ipn_key-> |
| 318 | ipnk_type].ipndb_n_elts_per_fib[fib_index]--; |
| 319 | |
| 320 | if (0 == |
| 321 | ip_neighbor_db[ipn->ipn_key-> |
| 322 | ipnk_type].ipndb_n_elts_per_fib[fib_index]) |
| 323 | fib_table_unlock (fib_index, fproto, FIB_SOURCE_ADJ); |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | static void |
| 329 | ip_neighbor_mk_complete (adj_index_t ai, ip_neighbor_t * ipn) |
| 330 | { |
| 331 | adj_nbr_update_rewrite (ai, ADJ_NBR_REWRITE_FLAG_COMPLETE, |
| 332 | ethernet_build_rewrite (vnet_get_main (), |
| 333 | ipn-> |
| 334 | ipn_key->ipnk_sw_if_index, |
| 335 | adj_get_link_type (ai), |
| 336 | ipn->ipn_mac.bytes)); |
| 337 | } |
| 338 | |
| 339 | static void |
| 340 | ip_neighbor_mk_incomplete (adj_index_t ai) |
| 341 | { |
| 342 | ip_adjacency_t *adj = adj_get (ai); |
| 343 | |
| 344 | adj_nbr_update_rewrite (ai, |
| 345 | ADJ_NBR_REWRITE_FLAG_INCOMPLETE, |
| 346 | ethernet_build_rewrite (vnet_get_main (), |
| 347 | adj-> |
| 348 | rewrite_header.sw_if_index, |
Neale Ranns | fca3c6a | 2019-12-31 03:49:34 +0000 | [diff] [blame] | 349 | VNET_LINK_ARP, |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 350 | VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST)); |
| 351 | } |
| 352 | |
| 353 | static adj_walk_rc_t |
| 354 | ip_neighbor_mk_complete_walk (adj_index_t ai, void *ctx) |
| 355 | { |
| 356 | ip_neighbor_t *ipn = ctx; |
| 357 | |
| 358 | ip_neighbor_mk_complete (ai, ipn); |
| 359 | |
| 360 | return (ADJ_WALK_RC_CONTINUE); |
| 361 | } |
| 362 | |
| 363 | static adj_walk_rc_t |
| 364 | ip_neighbor_mk_incomplete_walk (adj_index_t ai, void *ctx) |
| 365 | { |
| 366 | ip_neighbor_mk_incomplete (ai); |
| 367 | |
| 368 | return (ADJ_WALK_RC_CONTINUE); |
| 369 | } |
| 370 | |
| 371 | static void |
| 372 | ip_neighbor_free (ip_neighbor_t * ipn) |
| 373 | { |
| 374 | IP_NEIGHBOR_DBG ("free: %U", format_ip_neighbor, |
| 375 | ip_neighbor_get_index (ipn)); |
| 376 | |
| 377 | adj_nbr_walk_nh (ipn->ipn_key->ipnk_sw_if_index, |
| 378 | fib_proto_from_ip46 (ipn->ipn_key->ipnk_type), |
| 379 | &ipn->ipn_key->ipnk_ip, |
| 380 | ip_neighbor_mk_incomplete_walk, ipn); |
| 381 | ip_neighbor_adj_fib_remove |
| 382 | (ipn, |
| 383 | fib_table_get_index_for_sw_if_index |
| 384 | (fib_proto_from_ip46 (ipn->ipn_key->ipnk_type), |
| 385 | ipn->ipn_key->ipnk_sw_if_index)); |
| 386 | |
| 387 | ip_neighbor_list_remove (ipn); |
| 388 | ip_neighbor_db_remove (ipn->ipn_key); |
| 389 | clib_mem_free (ipn->ipn_key); |
| 390 | |
| 391 | pool_put (ip_neighbor_pool, ipn); |
| 392 | } |
| 393 | |
| 394 | static bool |
| 395 | ip_neighbor_force_reuse (ip46_type_t type) |
| 396 | { |
| 397 | if (!ip_neighbor_db[type].ipndb_recycle) |
| 398 | return false; |
| 399 | |
| 400 | /* pluck the oldest entry, which is the one from the end of the list */ |
| 401 | ip_neighbor_elt_t *elt, *head; |
| 402 | |
| 403 | head = |
| 404 | pool_elt_at_index (ip_neighbor_elt_pool, ip_neighbor_list_head[type]); |
| 405 | |
| 406 | if (clib_llist_is_empty (ip_neighbor_elt_pool, ipne_anchor, head)) |
| 407 | return (false); |
| 408 | |
| 409 | elt = clib_llist_prev (ip_neighbor_elt_pool, ipne_anchor, head); |
| 410 | ip_neighbor_free (ip_neighbor_get (elt->ipne_index)); |
| 411 | |
| 412 | return (true); |
| 413 | } |
| 414 | |
| 415 | static ip_neighbor_t * |
| 416 | ip_neighbor_alloc (const ip_neighbor_key_t * key, |
| 417 | const mac_address_t * mac, ip_neighbor_flags_t flags) |
| 418 | { |
| 419 | ip_neighbor_t *ipn; |
| 420 | |
| 421 | if (ip_neighbor_db[key->ipnk_type].ipndb_limit && |
| 422 | (ip_neighbor_db[key->ipnk_type].ipndb_n_elts >= |
| 423 | ip_neighbor_db[key->ipnk_type].ipndb_limit)) |
| 424 | { |
| 425 | if (!ip_neighbor_force_reuse (key->ipnk_type)) |
| 426 | return (NULL); |
| 427 | } |
| 428 | |
| 429 | pool_get_zero (ip_neighbor_pool, ipn); |
| 430 | |
| 431 | ipn->ipn_key = clib_mem_alloc (sizeof (*ipn->ipn_key)); |
| 432 | clib_memcpy (ipn->ipn_key, key, sizeof (*ipn->ipn_key)); |
| 433 | |
| 434 | ipn->ipn_fib_entry_index = FIB_NODE_INDEX_INVALID; |
| 435 | ipn->ipn_flags = flags; |
| 436 | ipn->ipn_elt = ~0; |
| 437 | |
| 438 | mac_address_copy (&ipn->ipn_mac, mac); |
| 439 | |
| 440 | ip_neighbor_db_add (ipn); |
| 441 | |
| 442 | /* create the adj-fib. the entry in the FIB table for the peer's interface */ |
| 443 | if (!(ipn->ipn_flags & IP_NEIGHBOR_FLAG_NO_FIB_ENTRY)) |
| 444 | ip_neighbor_adj_fib_add |
| 445 | (ipn, fib_table_get_index_for_sw_if_index |
| 446 | (fib_proto_from_ip46 (ipn->ipn_key->ipnk_type), |
| 447 | ipn->ipn_key->ipnk_sw_if_index)); |
| 448 | |
| 449 | return (ipn); |
| 450 | } |
| 451 | |
| 452 | int |
| 453 | ip_neighbor_add (const ip46_address_t * ip, |
| 454 | ip46_type_t type, |
| 455 | const mac_address_t * mac, |
| 456 | u32 sw_if_index, |
| 457 | ip_neighbor_flags_t flags, u32 * stats_index) |
| 458 | { |
| 459 | fib_protocol_t fproto; |
| 460 | ip_neighbor_t *ipn; |
| 461 | |
| 462 | /* main thread only */ |
| 463 | ASSERT (0 == vlib_get_thread_index ()); |
| 464 | |
| 465 | fproto = fib_proto_from_ip46 (type); |
| 466 | |
| 467 | const ip_neighbor_key_t key = { |
| 468 | .ipnk_ip = *ip, |
| 469 | .ipnk_sw_if_index = sw_if_index, |
| 470 | .ipnk_type = type, |
| 471 | }; |
| 472 | |
| 473 | ipn = ip_neighbor_db_find (&key); |
| 474 | |
| 475 | if (ipn) |
| 476 | { |
| 477 | IP_NEIGHBOR_DBG ("update: %U, %U", |
| 478 | format_vnet_sw_if_index_name, vnet_get_main (), |
| 479 | sw_if_index, format_ip46_address, ip, type, |
| 480 | format_ip_neighbor_flags, flags, format_mac_address_t, |
| 481 | mac); |
| 482 | |
Neale Ranns | c87fbb4 | 2020-04-02 17:08:28 +0000 | [diff] [blame^] | 483 | ip_neighbor_touch (ipn); |
| 484 | |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 485 | /* Refuse to over-write static neighbor entry. */ |
| 486 | if (!(flags & IP_NEIGHBOR_FLAG_STATIC) && |
| 487 | (ipn->ipn_flags & IP_NEIGHBOR_FLAG_STATIC)) |
| 488 | { |
| 489 | /* if MAC address match, still check to send event */ |
| 490 | if (0 == mac_address_cmp (&ipn->ipn_mac, mac)) |
| 491 | goto check_customers; |
| 492 | return -2; |
| 493 | } |
| 494 | |
| 495 | /* |
| 496 | * prevent a DoS attack from the data-plane that |
| 497 | * spams us with no-op updates to the MAC address |
| 498 | */ |
| 499 | if (0 == mac_address_cmp (&ipn->ipn_mac, mac)) |
| 500 | { |
| 501 | ip_neighbor_refresh (ipn); |
| 502 | goto check_customers; |
| 503 | } |
| 504 | |
| 505 | mac_address_copy (&ipn->ipn_mac, mac); |
| 506 | |
| 507 | /* A dynamic entry can become static, but not vice-versa. |
| 508 | * i.e. since if it was programmed by the CP then it must |
| 509 | * be removed by the CP */ |
| 510 | if ((flags & IP_NEIGHBOR_FLAG_STATIC) && |
| 511 | !(ipn->ipn_flags & IP_NEIGHBOR_FLAG_STATIC)) |
| 512 | { |
| 513 | ip_neighbor_list_remove (ipn); |
| 514 | ipn->ipn_flags |= IP_NEIGHBOR_FLAG_STATIC; |
| 515 | ipn->ipn_flags &= ~IP_NEIGHBOR_FLAG_DYNAMIC; |
| 516 | } |
| 517 | } |
| 518 | else |
| 519 | { |
| 520 | IP_NEIGHBOR_INFO ("add: %U, %U", |
| 521 | format_vnet_sw_if_index_name, vnet_get_main (), |
| 522 | sw_if_index, format_ip46_address, ip, type, |
| 523 | format_ip_neighbor_flags, flags, format_mac_address_t, |
| 524 | mac); |
| 525 | |
| 526 | ipn = ip_neighbor_alloc (&key, mac, flags); |
| 527 | |
| 528 | if (NULL == ipn) |
| 529 | return VNET_API_ERROR_LIMIT_EXCEEDED; |
| 530 | } |
| 531 | |
| 532 | /* Update time stamp and flags. */ |
| 533 | ip_neighbor_refresh (ipn); |
| 534 | |
| 535 | adj_nbr_walk_nh (ipn->ipn_key->ipnk_sw_if_index, |
| 536 | fproto, &ipn->ipn_key->ipnk_ip, |
| 537 | ip_neighbor_mk_complete_walk, ipn); |
| 538 | |
| 539 | check_customers: |
| 540 | /* Customer(s) requesting event for this address? */ |
| 541 | ip_neighbor_publish (ip_neighbor_get_index (ipn)); |
| 542 | |
| 543 | if (stats_index) |
| 544 | *stats_index = adj_nbr_find (fproto, |
| 545 | fib_proto_to_link (fproto), |
| 546 | &ipn->ipn_key->ipnk_ip, |
| 547 | ipn->ipn_key->ipnk_sw_if_index); |
| 548 | return 0; |
| 549 | } |
| 550 | |
| 551 | int |
| 552 | ip_neighbor_del (const ip46_address_t * ip, ip46_type_t type, u32 sw_if_index) |
| 553 | { |
| 554 | ip_neighbor_t *ipn; |
| 555 | |
| 556 | /* main thread only */ |
| 557 | ASSERT (0 == vlib_get_thread_index ()); |
| 558 | |
| 559 | IP_NEIGHBOR_INFO ("delete: %U, %U", |
| 560 | format_vnet_sw_if_index_name, vnet_get_main (), |
| 561 | sw_if_index, format_ip46_address, ip, type); |
| 562 | |
| 563 | const ip_neighbor_key_t key = { |
| 564 | .ipnk_ip = *ip, |
| 565 | .ipnk_sw_if_index = sw_if_index, |
| 566 | .ipnk_type = type, |
| 567 | }; |
| 568 | |
| 569 | ipn = ip_neighbor_db_find (&key); |
| 570 | |
| 571 | if (NULL == ipn) |
| 572 | return (VNET_API_ERROR_NO_SUCH_ENTRY); |
| 573 | |
| 574 | ip_neighbor_free (ipn); |
| 575 | |
| 576 | return (0); |
| 577 | } |
| 578 | |
| 579 | void |
| 580 | ip_neighbor_update (vnet_main_t * vnm, adj_index_t ai) |
| 581 | { |
| 582 | ip_neighbor_t *ipn; |
| 583 | ip_adjacency_t *adj; |
| 584 | |
| 585 | adj = adj_get (ai); |
| 586 | |
| 587 | ip_neighbor_key_t key = { |
| 588 | .ipnk_ip = adj->sub_type.nbr.next_hop, |
| 589 | .ipnk_type = fib_proto_to_ip46 (adj->ia_nh_proto), |
| 590 | .ipnk_sw_if_index = adj->rewrite_header.sw_if_index, |
| 591 | }; |
| 592 | ipn = ip_neighbor_db_find (&key); |
| 593 | |
| 594 | switch (adj->lookup_next_index) |
| 595 | { |
| 596 | case IP_LOOKUP_NEXT_ARP: |
| 597 | if (NULL != ipn) |
| 598 | { |
| 599 | adj_nbr_walk_nh (adj->rewrite_header.sw_if_index, |
| 600 | adj->ia_nh_proto, |
| 601 | &ipn->ipn_key->ipnk_ip, |
| 602 | ip_neighbor_mk_complete_walk, ipn); |
| 603 | } |
| 604 | else |
| 605 | { |
| 606 | /* |
| 607 | * no matching ARP entry. |
| 608 | * construct the rewrite required to for an ARP packet, and stick |
| 609 | * that in the adj's pipe to smoke. |
| 610 | */ |
| 611 | adj_nbr_update_rewrite |
| 612 | (ai, |
| 613 | ADJ_NBR_REWRITE_FLAG_INCOMPLETE, |
| 614 | ethernet_build_rewrite |
| 615 | (vnm, |
| 616 | adj->rewrite_header.sw_if_index, |
| 617 | VNET_LINK_ARP, |
| 618 | VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST)); |
| 619 | |
| 620 | /* |
| 621 | * since the FIB has added this adj for a route, it makes sense it |
| 622 | * may want to forward traffic sometime soon. Let's send a |
| 623 | * speculative ARP. just one. If we were to do periodically that |
| 624 | * wouldn't be bad either, but that's more code than i'm prepared to |
| 625 | * write at this time for relatively little reward. |
| 626 | */ |
Steven Luong | 3d5f622 | 2020-01-30 09:11:18 -0800 | [diff] [blame] | 627 | /* |
| 628 | * adj_nbr_update_rewrite may actually call fib_walk_sync. |
| 629 | * fib_walk_sync may allocate a new adjacency and potentially cause |
| 630 | * a realloc for adj_pool. When that happens, adj pointer is no |
| 631 | * longer valid here.x We refresh adj pointer accordingly. |
| 632 | */ |
| 633 | adj = adj_get (ai); |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 634 | ip_neighbor_probe (adj); |
| 635 | } |
| 636 | break; |
| 637 | case IP_LOOKUP_NEXT_GLEAN: |
| 638 | case IP_LOOKUP_NEXT_BCAST: |
| 639 | case IP_LOOKUP_NEXT_MCAST: |
| 640 | case IP_LOOKUP_NEXT_DROP: |
| 641 | case IP_LOOKUP_NEXT_PUNT: |
| 642 | case IP_LOOKUP_NEXT_LOCAL: |
| 643 | case IP_LOOKUP_NEXT_REWRITE: |
| 644 | case IP_LOOKUP_NEXT_MCAST_MIDCHAIN: |
| 645 | case IP_LOOKUP_NEXT_MIDCHAIN: |
| 646 | case IP_LOOKUP_NEXT_ICMP_ERROR: |
| 647 | case IP_LOOKUP_N_NEXT: |
| 648 | ASSERT (0); |
| 649 | break; |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | void |
| 654 | ip_neighbor_learn (const ip_neighbor_learn_t * l) |
| 655 | { |
| 656 | ip_neighbor_add (&l->ip, l->type, &l->mac, l->sw_if_index, |
| 657 | IP_NEIGHBOR_FLAG_DYNAMIC, NULL); |
| 658 | } |
| 659 | |
| 660 | static clib_error_t * |
| 661 | ip_neighbor_cmd (vlib_main_t * vm, |
| 662 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 663 | { |
| 664 | ip46_address_t ip = ip46_address_initializer; |
| 665 | mac_address_t mac = ZERO_MAC_ADDRESS; |
| 666 | vnet_main_t *vnm = vnet_get_main (); |
| 667 | ip_neighbor_flags_t flags; |
| 668 | u32 sw_if_index = ~0; |
| 669 | int is_add = 1; |
| 670 | int count = 1; |
| 671 | |
| 672 | flags = IP_NEIGHBOR_FLAG_DYNAMIC; |
| 673 | |
| 674 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 675 | { |
| 676 | /* set ip arp TenGigE1/1/0/1 1.2.3.4 aa:bb:... or aabb.ccdd... */ |
| 677 | if (unformat (input, "%U %U %U", |
| 678 | unformat_vnet_sw_interface, vnm, &sw_if_index, |
| 679 | unformat_ip46_address, &ip, IP46_TYPE_ANY, |
| 680 | unformat_mac_address_t, &mac)) |
| 681 | ; |
| 682 | else if (unformat (input, "delete") || unformat (input, "del")) |
| 683 | is_add = 0; |
| 684 | else if (unformat (input, "static")) |
| 685 | { |
| 686 | flags |= IP_NEIGHBOR_FLAG_STATIC; |
| 687 | flags &= ~IP_NEIGHBOR_FLAG_DYNAMIC; |
| 688 | } |
| 689 | else if (unformat (input, "no-fib-entry")) |
| 690 | flags |= IP_NEIGHBOR_FLAG_NO_FIB_ENTRY; |
| 691 | else if (unformat (input, "count %d", &count)) |
| 692 | ; |
| 693 | else |
| 694 | break; |
| 695 | } |
| 696 | |
| 697 | if (sw_if_index == ~0 || |
| 698 | ip46_address_is_zero (&ip) || mac_address_is_zero (&mac)) |
| 699 | return clib_error_return (0, |
| 700 | "specify interface, IP address and MAC: `%U'", |
| 701 | format_unformat_error, input); |
| 702 | |
| 703 | while (count) |
| 704 | { |
| 705 | if (is_add) |
| 706 | ip_neighbor_add (&ip, ip46_address_get_type (&ip), &mac, sw_if_index, |
| 707 | flags, NULL); |
| 708 | else |
| 709 | ip_neighbor_del (&ip, ip46_address_get_type (&ip), sw_if_index); |
| 710 | |
| 711 | ip46_address_increment (ip46_address_get_type (&ip), &ip); |
| 712 | mac_address_increment (&mac); |
| 713 | |
| 714 | --count; |
| 715 | } |
| 716 | |
| 717 | return NULL; |
| 718 | } |
| 719 | |
| 720 | /* *INDENT-OFF* */ |
| 721 | /*? |
| 722 | * Add or delete IPv4 ARP cache entries. |
| 723 | * |
| 724 | * @note 'set ip neighbor' options (e.g. delete, static, 'fib-id <id>', |
| 725 | * 'count <number>', 'interface ip4_addr mac_addr') can be added in |
| 726 | * any order and combination. |
| 727 | * |
| 728 | * @cliexpar |
| 729 | * @parblock |
| 730 | * Add or delete IPv4 ARP cache entries as follows. MAC Address can be in |
| 731 | * either aa:bb:cc:dd:ee:ff format or aabb.ccdd.eeff format. |
| 732 | * @cliexcmd{set ip neighbor GigabitEthernet2/0/0 6.0.0.3 dead.beef.babe} |
| 733 | * @cliexcmd{set ip neighbor delete GigabitEthernet2/0/0 6.0.0.3 de:ad:be:ef:ba:be} |
| 734 | * |
| 735 | * To add or delete an IPv4 ARP cache entry to or from a specific fib |
| 736 | * table: |
| 737 | * @cliexcmd{set ip neighbor fib-id 1 GigabitEthernet2/0/0 6.0.0.3 dead.beef.babe} |
| 738 | * @cliexcmd{set ip neighbor fib-id 1 delete GigabitEthernet2/0/0 6.0.0.3 dead.beef.babe} |
| 739 | * |
| 740 | * Add or delete IPv4 static ARP cache entries as follows: |
| 741 | * @cliexcmd{set ip neighbor static GigabitEthernet2/0/0 6.0.0.3 dead.beef.babe} |
| 742 | * @cliexcmd{set ip neighbor static delete GigabitEthernet2/0/0 6.0.0.3 dead.beef.babe} |
| 743 | * |
| 744 | * For testing / debugging purposes, the 'set ip neighbor' command can add or |
| 745 | * delete multiple entries. Supply the 'count N' parameter: |
| 746 | * @cliexcmd{set ip neighbor count 10 GigabitEthernet2/0/0 6.0.0.3 dead.beef.babe} |
| 747 | * @endparblock |
| 748 | ?*/ |
| 749 | VLIB_CLI_COMMAND (ip_neighbor_command, static) = { |
| 750 | .path = "set ip neighbor", |
| 751 | .short_help = |
| 752 | "set ip neighbor [del] <intfc> <ip-address> <mac-address> [static] [no-fib-entry] [count <count>] [fib-id <fib-id>] [proxy <lo-addr> - <hi-addr>]", |
| 753 | .function = ip_neighbor_cmd, |
| 754 | }; |
| 755 | VLIB_CLI_COMMAND (ip_neighbor_command2, static) = { |
| 756 | .path = "ip neighbor", |
| 757 | .short_help = |
| 758 | "ip neighbor [del] <intfc> <ip-address> <mac-address> [static] [no-fib-entry] [count <count>] [fib-id <fib-id>] [proxy <lo-addr> - <hi-addr>]", |
| 759 | .function = ip_neighbor_cmd, |
| 760 | }; |
| 761 | /* *INDENT-ON* */ |
| 762 | |
| 763 | static int |
| 764 | ip_neighbor_sort (void *a1, void *a2) |
| 765 | { |
| 766 | index_t *ipni1 = a1, *ipni2 = a2; |
| 767 | ip_neighbor_t *ipn1, *ipn2; |
| 768 | int cmp; |
| 769 | |
| 770 | ipn1 = ip_neighbor_get (*ipni1); |
| 771 | ipn2 = ip_neighbor_get (*ipni2); |
| 772 | |
| 773 | cmp = vnet_sw_interface_compare (vnet_get_main (), |
| 774 | ipn1->ipn_key->ipnk_sw_if_index, |
| 775 | ipn2->ipn_key->ipnk_sw_if_index); |
| 776 | if (!cmp) |
| 777 | cmp = ip46_address_cmp (&ipn1->ipn_key->ipnk_ip, &ipn2->ipn_key->ipnk_ip); |
| 778 | return cmp; |
| 779 | } |
| 780 | |
| 781 | static index_t * |
| 782 | ip_neighbor_entries (u32 sw_if_index, ip46_type_t type) |
| 783 | { |
| 784 | index_t *ipnis = NULL; |
| 785 | ip_neighbor_t *ipn; |
| 786 | |
| 787 | /* *INDENT-OFF* */ |
| 788 | pool_foreach (ipn, ip_neighbor_pool, |
| 789 | ({ |
| 790 | if (sw_if_index != ~0 && |
| 791 | ipn->ipn_key->ipnk_sw_if_index != sw_if_index && |
| 792 | (IP46_TYPE_ANY == type || |
| 793 | (ipn->ipn_key->ipnk_type == type))) |
| 794 | continue; |
| 795 | vec_add1 (ipnis, ip_neighbor_get_index(ipn)); |
| 796 | })); |
| 797 | |
| 798 | /* *INDENT-ON* */ |
| 799 | |
| 800 | if (ipnis) |
| 801 | vec_sort_with_function (ipnis, ip_neighbor_sort); |
| 802 | return ipnis; |
| 803 | } |
| 804 | |
| 805 | static clib_error_t * |
| 806 | ip_neighbor_show_sorted_i (vlib_main_t * vm, |
| 807 | unformat_input_t * input, |
| 808 | vlib_cli_command_t * cmd, ip46_type_t type) |
| 809 | { |
| 810 | ip_neighbor_elt_t *elt, *head; |
| 811 | |
| 812 | head = pool_elt_at_index (ip_neighbor_elt_pool, |
| 813 | ip_neighbor_list_head[type]); |
| 814 | |
| 815 | |
| 816 | vlib_cli_output (vm, "%=12s%=40s%=6s%=20s%=24s", "Time", "IP", |
| 817 | "Flags", "Ethernet", "Interface"); |
| 818 | |
| 819 | /* *INDENT-OFF*/ |
| 820 | /* the list is time sorted, newest first, so start from the back |
| 821 | * and work forwards. Stop when we get to one that is alive */ |
| 822 | clib_llist_foreach_reverse(ip_neighbor_elt_pool, |
| 823 | ipne_anchor, head, elt, |
| 824 | ({ |
| 825 | vlib_cli_output (vm, "%U", format_ip_neighbor, elt->ipne_index); |
| 826 | })); |
| 827 | /* *INDENT-ON*/ |
| 828 | |
| 829 | return (NULL); |
| 830 | } |
| 831 | |
| 832 | static clib_error_t * |
| 833 | ip_neighbor_show_i (vlib_main_t * vm, |
| 834 | unformat_input_t * input, |
| 835 | vlib_cli_command_t * cmd, ip46_type_t type) |
| 836 | { |
| 837 | index_t *ipni, *ipnis = NULL; |
| 838 | u32 sw_if_index; |
| 839 | |
| 840 | /* Filter entries by interface if given. */ |
| 841 | sw_if_index = ~0; |
| 842 | (void) unformat_user (input, unformat_vnet_sw_interface, vnet_get_main (), |
| 843 | &sw_if_index); |
| 844 | |
| 845 | ipnis = ip_neighbor_entries (sw_if_index, type); |
| 846 | |
| 847 | if (ipnis) |
| 848 | vlib_cli_output (vm, "%=12s%=40s%=6s%=20s%=24s", "Time", "IP", |
| 849 | "Flags", "Ethernet", "Interface"); |
| 850 | |
| 851 | vec_foreach (ipni, ipnis) |
| 852 | { |
| 853 | vlib_cli_output (vm, "%U", format_ip_neighbor, *ipni); |
| 854 | } |
| 855 | vec_free (ipnis); |
| 856 | |
| 857 | return (NULL); |
| 858 | } |
| 859 | |
| 860 | static clib_error_t * |
| 861 | ip_neighbor_show (vlib_main_t * vm, |
| 862 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 863 | { |
| 864 | return (ip_neighbor_show_i (vm, input, cmd, IP46_TYPE_ANY)); |
| 865 | } |
| 866 | |
| 867 | static clib_error_t * |
| 868 | ip6_neighbor_show (vlib_main_t * vm, |
| 869 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 870 | { |
| 871 | return (ip_neighbor_show_i (vm, input, cmd, IP46_TYPE_IP6)); |
| 872 | } |
| 873 | |
| 874 | static clib_error_t * |
| 875 | ip4_neighbor_show (vlib_main_t * vm, |
| 876 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 877 | { |
| 878 | return (ip_neighbor_show_i (vm, input, cmd, IP46_TYPE_IP4)); |
| 879 | } |
| 880 | |
| 881 | static clib_error_t * |
| 882 | ip6_neighbor_show_sorted (vlib_main_t * vm, |
| 883 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 884 | { |
| 885 | return (ip_neighbor_show_sorted_i (vm, input, cmd, IP46_TYPE_IP6)); |
| 886 | } |
| 887 | |
| 888 | static clib_error_t * |
| 889 | ip4_neighbor_show_sorted (vlib_main_t * vm, |
| 890 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 891 | { |
| 892 | return (ip_neighbor_show_sorted_i (vm, input, cmd, IP46_TYPE_IP4)); |
| 893 | } |
| 894 | |
| 895 | /*? |
| 896 | * Display all the IP neighbor entries. |
| 897 | * |
| 898 | * @cliexpar |
| 899 | * Example of how to display the IPv4 ARP table: |
| 900 | * @cliexstart{show ip neighbor} |
| 901 | * Time FIB IP4 Flags Ethernet Interface |
| 902 | * 346.3028 0 6.1.1.3 de:ad:be:ef:ba:be GigabitEthernet2/0/0 |
| 903 | * 3077.4271 0 6.1.1.4 S de:ad:be:ef:ff:ff GigabitEthernet2/0/0 |
| 904 | * 2998.6409 1 6.2.2.3 de:ad:be:ef:00:01 GigabitEthernet2/0/0 |
| 905 | * Proxy arps enabled for: |
| 906 | * Fib_index 0 6.0.0.1 - 6.0.0.11 |
| 907 | * @cliexend |
| 908 | ?*/ |
| 909 | /* *INDENT-OFF* */ |
| 910 | VLIB_CLI_COMMAND (show_ip_neighbors_cmd_node, static) = { |
| 911 | .path = "show ip neighbors", |
| 912 | .function = ip_neighbor_show, |
| 913 | .short_help = "show ip neighbors [interface]", |
| 914 | }; |
| 915 | VLIB_CLI_COMMAND (show_ip4_neighbors_cmd_node, static) = { |
| 916 | .path = "show ip4 neighbors", |
| 917 | .function = ip4_neighbor_show, |
| 918 | .short_help = "show ip4 neighbors [interface]", |
| 919 | }; |
| 920 | VLIB_CLI_COMMAND (show_ip6_neighbors_cmd_node, static) = { |
| 921 | .path = "show ip6 neighbors", |
| 922 | .function = ip6_neighbor_show, |
| 923 | .short_help = "show ip6 neighbors [interface]", |
| 924 | }; |
| 925 | VLIB_CLI_COMMAND (show_ip_neighbor_cmd_node, static) = { |
| 926 | .path = "show ip neighbor", |
| 927 | .function = ip_neighbor_show, |
| 928 | .short_help = "show ip neighbor [interface]", |
| 929 | }; |
| 930 | VLIB_CLI_COMMAND (show_ip4_neighbor_cmd_node, static) = { |
| 931 | .path = "show ip4 neighbor", |
| 932 | .function = ip4_neighbor_show, |
| 933 | .short_help = "show ip4 neighbor [interface]", |
| 934 | }; |
| 935 | VLIB_CLI_COMMAND (show_ip6_neighbor_cmd_node, static) = { |
| 936 | .path = "show ip6 neighbor", |
| 937 | .function = ip6_neighbor_show, |
| 938 | .short_help = "show ip6 neighbor [interface]", |
| 939 | }; |
| 940 | VLIB_CLI_COMMAND (show_ip4_neighbor_sorted_cmd_node, static) = { |
| 941 | .path = "show ip4 neighbor-sorted", |
| 942 | .function = ip4_neighbor_show_sorted, |
| 943 | .short_help = "show ip4 neighbor-sorted", |
| 944 | }; |
| 945 | VLIB_CLI_COMMAND (show_ip6_neighbor_sorted_cmd_node, static) = { |
| 946 | .path = "show ip6 neighbor-sorted", |
| 947 | .function = ip6_neighbor_show_sorted, |
| 948 | .short_help = "show ip6 neighbor-sorted", |
| 949 | }; |
| 950 | /* *INDENT-ON* */ |
| 951 | |
| 952 | static ip_neighbor_vft_t ip_nbr_vfts[IP46_N_TYPES]; |
| 953 | |
| 954 | void |
| 955 | ip_neighbor_register (ip46_type_t type, const ip_neighbor_vft_t * vft) |
| 956 | { |
| 957 | ip_nbr_vfts[type] = *vft; |
| 958 | } |
| 959 | |
| 960 | void |
| 961 | ip_neighbor_probe_dst (const ip_adjacency_t * adj, const ip46_address_t * dst) |
| 962 | { |
| 963 | if (!vnet_sw_interface_is_admin_up (vnet_get_main (), |
| 964 | adj->rewrite_header.sw_if_index)) |
| 965 | return; |
| 966 | |
| 967 | switch (adj->ia_nh_proto) |
| 968 | { |
| 969 | case FIB_PROTOCOL_IP6: |
| 970 | ip6_neighbor_probe_dst (adj, &dst->ip6); |
| 971 | break; |
| 972 | case FIB_PROTOCOL_IP4: |
| 973 | ip4_neighbor_probe_dst (adj, &dst->ip4); |
| 974 | break; |
| 975 | case FIB_PROTOCOL_MPLS: |
| 976 | ASSERT (0); |
| 977 | break; |
| 978 | } |
| 979 | } |
| 980 | |
| 981 | void |
| 982 | ip_neighbor_probe (const ip_adjacency_t * adj) |
| 983 | { |
| 984 | ip_neighbor_probe_dst (adj, &adj->sub_type.nbr.next_hop); |
| 985 | } |
| 986 | |
| 987 | void |
| 988 | ip_neighbor_advertise (vlib_main_t * vm, |
| 989 | ip46_type_t type, |
| 990 | const ip46_address_t * addr, u32 sw_if_index) |
| 991 | { |
| 992 | vnet_main_t *vnm = vnet_get_main (); |
| 993 | |
| 994 | if (type == IP46_TYPE_IP4 || type == IP46_TYPE_BOTH) |
Matthew Smith | 5002e7f | 2019-12-23 16:25:11 -0600 | [diff] [blame] | 995 | ip4_neighbor_advertise (vm, vnm, sw_if_index, (addr) ? &addr->ip4 : NULL); |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 996 | if (type == IP46_TYPE_IP6 || type == IP46_TYPE_BOTH) |
Matthew Smith | 5002e7f | 2019-12-23 16:25:11 -0600 | [diff] [blame] | 997 | ip6_neighbor_advertise (vm, vnm, sw_if_index, (addr) ? &addr->ip6 : NULL); |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 998 | } |
| 999 | |
| 1000 | void |
| 1001 | ip_neighbor_walk (ip46_type_t type, |
| 1002 | u32 sw_if_index, ip_neighbor_walk_cb_t cb, void *ctx) |
| 1003 | { |
| 1004 | ip_neighbor_key_t *key; |
| 1005 | index_t ipni; |
| 1006 | |
| 1007 | if (~0 == sw_if_index) |
| 1008 | { |
| 1009 | uword **hash; |
| 1010 | |
| 1011 | vec_foreach (hash, ip_neighbor_db[type].ipndb_hash) |
| 1012 | { |
| 1013 | /* *INDENT-OFF* */ |
| 1014 | hash_foreach (key, ipni, *hash, |
| 1015 | ({ |
| 1016 | cb (ipni, ctx); |
| 1017 | })); |
| 1018 | /* *INDENT-ON* */ |
| 1019 | } |
| 1020 | } |
| 1021 | else |
| 1022 | { |
| 1023 | uword *hash; |
| 1024 | |
| 1025 | if (vec_len (ip_neighbor_db[type].ipndb_hash) <= sw_if_index) |
| 1026 | return; |
| 1027 | hash = ip_neighbor_db[type].ipndb_hash[sw_if_index]; |
| 1028 | |
| 1029 | /* *INDENT-OFF* */ |
| 1030 | hash_foreach (key, ipni, hash, |
| 1031 | ({ |
| 1032 | cb (ipni, ctx); |
| 1033 | })); |
| 1034 | /* *INDENT-ON* */ |
| 1035 | } |
| 1036 | } |
| 1037 | |
| 1038 | int |
| 1039 | ip4_neighbor_proxy_add (u32 fib_index, |
| 1040 | const ip4_address_t * start, |
| 1041 | const ip4_address_t * end) |
| 1042 | { |
| 1043 | if (ip_nbr_vfts[IP46_TYPE_IP4].inv_proxy4_add) |
| 1044 | { |
| 1045 | return (ip_nbr_vfts[IP46_TYPE_IP4].inv_proxy4_add |
| 1046 | (fib_index, start, end)); |
| 1047 | } |
| 1048 | |
| 1049 | return (-1); |
| 1050 | } |
| 1051 | |
| 1052 | int |
| 1053 | ip4_neighbor_proxy_delete (u32 fib_index, |
| 1054 | const ip4_address_t * start, |
| 1055 | const ip4_address_t * end) |
| 1056 | { |
| 1057 | if (ip_nbr_vfts[IP46_TYPE_IP4].inv_proxy4_del) |
| 1058 | { |
| 1059 | return (ip_nbr_vfts[IP46_TYPE_IP4].inv_proxy4_del |
| 1060 | (fib_index, start, end)); |
| 1061 | } |
| 1062 | return -1; |
| 1063 | } |
| 1064 | |
| 1065 | int |
| 1066 | ip4_neighbor_proxy_enable (u32 sw_if_index) |
| 1067 | { |
| 1068 | if (ip_nbr_vfts[IP46_TYPE_IP4].inv_proxy4_enable) |
| 1069 | { |
| 1070 | return (ip_nbr_vfts[IP46_TYPE_IP4].inv_proxy4_enable (sw_if_index)); |
| 1071 | } |
| 1072 | return -1; |
| 1073 | } |
| 1074 | |
| 1075 | int |
| 1076 | ip4_neighbor_proxy_disable (u32 sw_if_index) |
| 1077 | { |
| 1078 | if (ip_nbr_vfts[IP46_TYPE_IP4].inv_proxy4_disable) |
| 1079 | { |
| 1080 | return (ip_nbr_vfts[IP46_TYPE_IP4].inv_proxy4_disable (sw_if_index)); |
| 1081 | } |
| 1082 | return -1; |
| 1083 | } |
| 1084 | |
| 1085 | int |
| 1086 | ip6_neighbor_proxy_add (u32 sw_if_index, const ip6_address_t * addr) |
| 1087 | { |
| 1088 | if (ip_nbr_vfts[IP46_TYPE_IP6].inv_proxy6_add) |
| 1089 | { |
| 1090 | return (ip_nbr_vfts[IP46_TYPE_IP6].inv_proxy6_add (sw_if_index, addr)); |
| 1091 | } |
| 1092 | return -1; |
| 1093 | } |
| 1094 | |
| 1095 | int |
| 1096 | ip6_neighbor_proxy_del (u32 sw_if_index, const ip6_address_t * addr) |
| 1097 | { |
| 1098 | if (ip_nbr_vfts[IP46_TYPE_IP6].inv_proxy6_del) |
| 1099 | { |
| 1100 | return (ip_nbr_vfts[IP46_TYPE_IP6].inv_proxy6_del (sw_if_index, addr)); |
| 1101 | } |
| 1102 | return -1; |
| 1103 | } |
| 1104 | |
| 1105 | static void |
| 1106 | ip_neighbor_ethernet_change_mac (ethernet_main_t * em, |
| 1107 | u32 sw_if_index, uword opaque) |
| 1108 | { |
| 1109 | ip_neighbor_t *ipn; |
| 1110 | adj_index_t ai; |
| 1111 | |
| 1112 | IP_NEIGHBOR_DBG ("mac-change: %U", |
| 1113 | format_vnet_sw_if_index_name, vnet_get_main (), |
| 1114 | sw_if_index); |
| 1115 | |
| 1116 | /* *INDENT-OFF* */ |
| 1117 | pool_foreach (ipn, ip_neighbor_pool, |
| 1118 | ({ |
| 1119 | if (ipn->ipn_key->ipnk_sw_if_index == sw_if_index) |
| 1120 | adj_nbr_walk_nh (ipn->ipn_key->ipnk_sw_if_index, |
| 1121 | fib_proto_from_ip46(ipn->ipn_key->ipnk_type), |
| 1122 | &ipn->ipn_key->ipnk_ip, |
| 1123 | ip_neighbor_mk_complete_walk, |
| 1124 | ipn); |
| 1125 | })); |
| 1126 | /* *INDENT-ON* */ |
| 1127 | |
| 1128 | ai = adj_glean_get (FIB_PROTOCOL_IP4, sw_if_index); |
| 1129 | |
| 1130 | if (ADJ_INDEX_INVALID != ai) |
| 1131 | adj_glean_update_rewrite (ai); |
| 1132 | } |
| 1133 | |
| 1134 | void |
| 1135 | ip_neighbor_populate (ip46_type_t type, u32 sw_if_index) |
| 1136 | { |
| 1137 | index_t *ipnis = NULL, *ipni; |
| 1138 | ip_neighbor_t *ipn; |
| 1139 | |
| 1140 | IP_NEIGHBOR_DBG ("populate: %U %U", |
| 1141 | format_vnet_sw_if_index_name, vnet_get_main (), |
| 1142 | sw_if_index, format_ip46_type, type); |
| 1143 | |
| 1144 | /* *INDENT-OFF* */ |
| 1145 | pool_foreach (ipn, ip_neighbor_pool, |
| 1146 | ({ |
| 1147 | if (ipn->ipn_key->ipnk_type == type && |
| 1148 | ipn->ipn_key->ipnk_sw_if_index == sw_if_index) |
| 1149 | vec_add1 (ipnis, ipn - ip_neighbor_pool); |
| 1150 | })); |
| 1151 | /* *INDENT-ON* */ |
| 1152 | |
| 1153 | vec_foreach (ipni, ipnis) |
| 1154 | { |
| 1155 | ipn = ip_neighbor_get (*ipni); |
| 1156 | |
| 1157 | adj_nbr_walk_nh (ipn->ipn_key->ipnk_sw_if_index, |
| 1158 | fib_proto_from_ip46 (ipn->ipn_key->ipnk_type), |
| 1159 | &ipn->ipn_key->ipnk_ip, |
| 1160 | ip_neighbor_mk_complete_walk, ipn); |
| 1161 | } |
| 1162 | vec_free (ipnis); |
| 1163 | } |
| 1164 | |
| 1165 | void |
| 1166 | ip_neighbor_flush (ip46_type_t type, u32 sw_if_index) |
| 1167 | { |
| 1168 | index_t *ipnis = NULL, *ipni; |
| 1169 | ip_neighbor_t *ipn; |
| 1170 | |
| 1171 | IP_NEIGHBOR_DBG ("flush: %U %U", |
| 1172 | format_vnet_sw_if_index_name, vnet_get_main (), |
| 1173 | sw_if_index, format_ip46_type, type); |
| 1174 | |
| 1175 | /* *INDENT-OFF* */ |
| 1176 | pool_foreach (ipn, ip_neighbor_pool, |
| 1177 | ({ |
| 1178 | if (ipn->ipn_key->ipnk_type == type && |
| 1179 | ipn->ipn_key->ipnk_sw_if_index == sw_if_index && |
| 1180 | ip_neighbor_is_dynamic (ipn)) |
| 1181 | vec_add1 (ipnis, ipn - ip_neighbor_pool); |
| 1182 | })); |
| 1183 | /* *INDENT-ON* */ |
| 1184 | |
| 1185 | vec_foreach (ipni, ipnis) ip_neighbor_free (ip_neighbor_get (*ipni)); |
| 1186 | vec_free (ipnis); |
| 1187 | } |
| 1188 | |
Neale Ranns | c87fbb4 | 2020-04-02 17:08:28 +0000 | [diff] [blame^] | 1189 | static walk_rc_t |
| 1190 | ip_neighbor_mark_one (index_t ipni, void *ctx) |
| 1191 | { |
| 1192 | ip_neighbor_t *ipn; |
| 1193 | |
| 1194 | ipn = ip_neighbor_get (ipni); |
| 1195 | |
| 1196 | ipn->ipn_flags |= IP_NEIGHBOR_FLAG_STALE; |
| 1197 | |
| 1198 | return (WALK_CONTINUE); |
| 1199 | } |
| 1200 | |
| 1201 | void |
| 1202 | ip_neighbor_mark (ip46_type_t type) |
| 1203 | { |
| 1204 | ip_neighbor_walk (type, ~0, ip_neighbor_mark_one, NULL); |
| 1205 | } |
| 1206 | |
| 1207 | typedef struct ip_neighbor_sweep_ctx_t_ |
| 1208 | { |
| 1209 | index_t *ipnsc_stale; |
| 1210 | } ip_neighbor_sweep_ctx_t; |
| 1211 | |
| 1212 | static walk_rc_t |
| 1213 | ip_neighbor_sweep_one (index_t ipni, void *arg) |
| 1214 | { |
| 1215 | ip_neighbor_sweep_ctx_t *ctx = arg; |
| 1216 | ip_neighbor_t *ipn; |
| 1217 | |
| 1218 | ipn = ip_neighbor_get (ipni); |
| 1219 | |
| 1220 | if (ipn->ipn_flags & IP_NEIGHBOR_FLAG_STALE) |
| 1221 | { |
| 1222 | vec_add1 (ctx->ipnsc_stale, ipni); |
| 1223 | } |
| 1224 | |
| 1225 | return (WALK_CONTINUE); |
| 1226 | } |
| 1227 | |
| 1228 | void |
| 1229 | ip_neighbor_sweep (ip46_type_t type) |
| 1230 | { |
| 1231 | ip_neighbor_sweep_ctx_t ctx = { }; |
| 1232 | index_t *ipni; |
| 1233 | |
| 1234 | ip_neighbor_walk (type, ~0, ip_neighbor_sweep_one, &ctx); |
| 1235 | |
| 1236 | vec_foreach (ipni, ctx.ipnsc_stale) |
| 1237 | { |
| 1238 | ip_neighbor_free (ip_neighbor_get (*ipni)); |
| 1239 | } |
| 1240 | vec_free (ctx.ipnsc_stale); |
| 1241 | } |
| 1242 | |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 1243 | /* |
| 1244 | * Remove any arp entries associated with the specified interface |
| 1245 | */ |
| 1246 | static clib_error_t * |
| 1247 | ip_neighbor_interface_admin_change (vnet_main_t * vnm, |
| 1248 | u32 sw_if_index, u32 flags) |
| 1249 | { |
| 1250 | ip46_type_t type; |
| 1251 | |
| 1252 | IP_NEIGHBOR_DBG ("interface-admin: %U %s", |
| 1253 | format_vnet_sw_if_index_name, vnet_get_main (), |
| 1254 | sw_if_index, |
| 1255 | (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP ? "up" : "down")); |
| 1256 | |
| 1257 | if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) |
| 1258 | { |
| 1259 | FOREACH_IP46_TYPE (type) ip_neighbor_populate (type, sw_if_index); |
| 1260 | } |
| 1261 | else |
| 1262 | { |
| 1263 | /* admin down, flush all neighbours */ |
| 1264 | FOREACH_IP46_TYPE (type) ip_neighbor_flush (type, sw_if_index); |
| 1265 | } |
| 1266 | |
| 1267 | return (NULL); |
| 1268 | } |
| 1269 | |
| 1270 | VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ip_neighbor_interface_admin_change); |
| 1271 | |
| 1272 | /* |
| 1273 | * Remove any arp entries associated with the specified interface |
| 1274 | */ |
| 1275 | static clib_error_t * |
| 1276 | ip_neighbor_delete_sw_interface (vnet_main_t * vnm, |
| 1277 | u32 sw_if_index, u32 is_add) |
| 1278 | { |
| 1279 | IP_NEIGHBOR_DBG ("interface-change: %U %s", |
| 1280 | format_vnet_sw_if_index_name, vnet_get_main (), |
| 1281 | sw_if_index, (is_add ? "add" : "del")); |
| 1282 | |
| 1283 | if (!is_add && sw_if_index != ~0) |
| 1284 | { |
| 1285 | ip46_type_t type; |
| 1286 | |
| 1287 | FOREACH_IP46_TYPE (type) ip_neighbor_flush (type, sw_if_index); |
| 1288 | } |
| 1289 | |
| 1290 | return (NULL); |
| 1291 | } |
| 1292 | |
| 1293 | VNET_SW_INTERFACE_ADD_DEL_FUNCTION (ip_neighbor_delete_sw_interface); |
| 1294 | |
| 1295 | typedef struct ip_neighbor_walk_covered_ctx_t_ |
| 1296 | { |
| 1297 | ip46_type_t type; |
| 1298 | ip46_address_t addr; |
| 1299 | u32 length; |
| 1300 | index_t *ipnis; |
| 1301 | } ip_neighbor_walk_covered_ctx_t; |
| 1302 | |
| 1303 | static walk_rc_t |
| 1304 | ip_neighbor_walk_covered (index_t ipni, void *arg) |
| 1305 | { |
| 1306 | ip_neighbor_walk_covered_ctx_t *ctx = arg; |
| 1307 | ip_neighbor_t *ipn; |
| 1308 | |
| 1309 | ipn = ip_neighbor_get (ipni); |
| 1310 | |
| 1311 | ASSERT (ipn->ipn_key->ipnk_type == ctx->type); |
| 1312 | |
| 1313 | if (IP46_TYPE_IP4 == ctx->type) |
| 1314 | { |
| 1315 | if (ip4_destination_matches_route (&ip4_main, |
| 1316 | &ipn->ipn_key->ipnk_ip.ip4, |
| 1317 | &ctx->addr.ip4, |
| 1318 | ctx->length) && |
| 1319 | ip_neighbor_is_dynamic (ipn)) |
| 1320 | { |
| 1321 | vec_add1 (ctx->ipnis, ip_neighbor_get_index (ipn)); |
| 1322 | } |
| 1323 | } |
| 1324 | return (WALK_CONTINUE); |
| 1325 | } |
| 1326 | |
| 1327 | |
| 1328 | /* |
| 1329 | * callback when an interface address is added or deleted |
| 1330 | */ |
| 1331 | static void |
| 1332 | ip_neighbor_add_del_interface_address_v4 (ip4_main_t * im, |
| 1333 | uword opaque, |
| 1334 | u32 sw_if_index, |
| 1335 | ip4_address_t * address, |
| 1336 | u32 address_length, |
| 1337 | u32 if_address_index, u32 is_del) |
| 1338 | { |
| 1339 | /* |
| 1340 | * Flush the ARP cache of all entries covered by the address |
| 1341 | * that is being removed. |
| 1342 | */ |
| 1343 | IP_NEIGHBOR_DBG ("addr-%d: %U, %U/%d", |
| 1344 | (is_del ? "del" : "add"), |
| 1345 | format_vnet_sw_if_index_name, vnet_get_main (), |
| 1346 | sw_if_index, format_ip4_address, address, address_length); |
| 1347 | |
| 1348 | if (is_del) |
| 1349 | { |
| 1350 | ip_neighbor_walk_covered_ctx_t ctx = { |
| 1351 | .addr.ip4 = *address, |
| 1352 | .type = IP46_TYPE_IP4, |
| 1353 | .length = address_length, |
| 1354 | }; |
| 1355 | index_t *ipni; |
| 1356 | |
| 1357 | ip_neighbor_walk (IP46_TYPE_IP4, sw_if_index, |
| 1358 | ip_neighbor_walk_covered, &ctx); |
| 1359 | |
| 1360 | vec_foreach (ipni, ctx.ipnis) |
| 1361 | ip_neighbor_free (ip_neighbor_get (*ipni)); |
| 1362 | |
| 1363 | vec_free (ctx.ipnis); |
| 1364 | } |
| 1365 | } |
| 1366 | |
| 1367 | /* |
| 1368 | * callback when an interface address is added or deleted |
| 1369 | */ |
| 1370 | static void |
| 1371 | ip_neighbor_add_del_interface_address_v6 (ip6_main_t * im, |
| 1372 | uword opaque, |
| 1373 | u32 sw_if_index, |
| 1374 | ip6_address_t * address, |
| 1375 | u32 address_length, |
| 1376 | u32 if_address_index, u32 is_del) |
| 1377 | { |
| 1378 | /* |
| 1379 | * Flush the ARP cache of all entries covered by the address |
| 1380 | * that is being removed. |
| 1381 | */ |
| 1382 | IP_NEIGHBOR_DBG ("addr-change: %U, %U/%d %s", |
| 1383 | format_vnet_sw_if_index_name, vnet_get_main (), |
| 1384 | sw_if_index, format_ip6_address, address, address_length, |
| 1385 | (is_del ? "del" : "add")); |
| 1386 | |
| 1387 | if (is_del) |
| 1388 | { |
| 1389 | ip_neighbor_walk_covered_ctx_t ctx = { |
| 1390 | .addr.ip6 = *address, |
| 1391 | .type = IP46_TYPE_IP6, |
| 1392 | .length = address_length, |
| 1393 | }; |
| 1394 | index_t *ipni; |
| 1395 | |
| 1396 | ip_neighbor_walk (IP46_TYPE_IP6, sw_if_index, |
| 1397 | ip_neighbor_walk_covered, &ctx); |
| 1398 | |
| 1399 | vec_foreach (ipni, ctx.ipnis) |
| 1400 | ip_neighbor_free (ip_neighbor_get (*ipni)); |
| 1401 | |
| 1402 | vec_free (ctx.ipnis); |
| 1403 | } |
| 1404 | } |
| 1405 | |
| 1406 | typedef struct ip_neighbor_table_bind_ctx_t_ |
| 1407 | { |
| 1408 | u32 new_fib_index; |
| 1409 | u32 old_fib_index; |
| 1410 | } ip_neighbor_table_bind_ctx_t; |
| 1411 | |
| 1412 | static walk_rc_t |
| 1413 | ip_neighbor_walk_table_bind (index_t ipni, void *arg) |
| 1414 | { |
| 1415 | ip_neighbor_table_bind_ctx_t *ctx = arg; |
| 1416 | ip_neighbor_t *ipn; |
| 1417 | |
| 1418 | ipn = ip_neighbor_get (ipni); |
| 1419 | ip_neighbor_adj_fib_remove (ipn, ctx->old_fib_index); |
| 1420 | ip_neighbor_adj_fib_add (ipn, ctx->new_fib_index); |
| 1421 | |
| 1422 | return (WALK_CONTINUE); |
| 1423 | } |
| 1424 | |
| 1425 | static void |
| 1426 | ip_neighbor_table_bind_v4 (ip4_main_t * im, |
| 1427 | uword opaque, |
| 1428 | u32 sw_if_index, |
| 1429 | u32 new_fib_index, u32 old_fib_index) |
| 1430 | { |
| 1431 | ip_neighbor_table_bind_ctx_t ctx = { |
| 1432 | .old_fib_index = old_fib_index, |
| 1433 | .new_fib_index = new_fib_index, |
| 1434 | }; |
| 1435 | |
| 1436 | ip_neighbor_walk (IP46_TYPE_IP4, sw_if_index, |
| 1437 | ip_neighbor_walk_table_bind, &ctx); |
| 1438 | } |
| 1439 | |
| 1440 | static void |
| 1441 | ip_neighbor_table_bind_v6 (ip6_main_t * im, |
| 1442 | uword opaque, |
| 1443 | u32 sw_if_index, |
| 1444 | u32 new_fib_index, u32 old_fib_index) |
| 1445 | { |
| 1446 | ip_neighbor_table_bind_ctx_t ctx = { |
| 1447 | .old_fib_index = old_fib_index, |
| 1448 | .new_fib_index = new_fib_index, |
| 1449 | }; |
| 1450 | |
| 1451 | ip_neighbor_walk (IP46_TYPE_IP6, sw_if_index, |
| 1452 | ip_neighbor_walk_table_bind, &ctx); |
| 1453 | } |
| 1454 | |
| 1455 | typedef enum ip_neighbor_age_state_t_ |
| 1456 | { |
| 1457 | IP_NEIGHBOR_AGE_ALIVE, |
| 1458 | IP_NEIGHBOR_AGE_PROBE, |
| 1459 | IP_NEIGHBOR_AGE_DEAD, |
| 1460 | } ip_neighbor_age_state_t; |
| 1461 | |
| 1462 | #define IP_NEIGHBOR_PROCESS_SLEEP_LONG (0) |
| 1463 | |
| 1464 | static ip_neighbor_age_state_t |
| 1465 | ip_neighbour_age_out (index_t ipni, f64 now, f64 * wait) |
| 1466 | { |
| 1467 | ip_neighbor_t *ipn; |
Vladimir Isaev | 1284f8c | 2020-02-18 15:26:12 +0300 | [diff] [blame] | 1468 | u32 ipndb_age; |
| 1469 | u32 ttl; |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 1470 | |
| 1471 | ipn = ip_neighbor_get (ipni); |
Vladimir Isaev | 1284f8c | 2020-02-18 15:26:12 +0300 | [diff] [blame] | 1472 | ipndb_age = ip_neighbor_db[ipn->ipn_key->ipnk_type].ipndb_age; |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 1473 | ttl = now - ipn->ipn_time_last_updated; |
Vladimir Isaev | 1284f8c | 2020-02-18 15:26:12 +0300 | [diff] [blame] | 1474 | *wait = ipndb_age; |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 1475 | |
Vladimir Isaev | 1284f8c | 2020-02-18 15:26:12 +0300 | [diff] [blame] | 1476 | if (ttl > ipndb_age) |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 1477 | { |
| 1478 | IP_NEIGHBOR_DBG ("aged: %U @%f - %f > %d", |
| 1479 | format_ip_neighbor, ipni, now, |
Vladimir Isaev | 1284f8c | 2020-02-18 15:26:12 +0300 | [diff] [blame] | 1480 | ipn->ipn_time_last_updated, ipndb_age); |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 1481 | if (ipn->ipn_n_probes > 2) |
| 1482 | { |
| 1483 | /* 3 strikes and yea-re out */ |
| 1484 | IP_NEIGHBOR_DBG ("dead: %U", format_ip_neighbor, ipni); |
Vladimir Isaev | 1284f8c | 2020-02-18 15:26:12 +0300 | [diff] [blame] | 1485 | *wait = 1; |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 1486 | return (IP_NEIGHBOR_AGE_DEAD); |
| 1487 | } |
| 1488 | else |
| 1489 | { |
| 1490 | adj_index_t ai; |
| 1491 | |
| 1492 | ai = adj_glean_get (fib_proto_from_ip46 (ipn->ipn_key->ipnk_type), |
| 1493 | ip_neighbor_get_sw_if_index (ipn)); |
| 1494 | |
| 1495 | if (ADJ_INDEX_INVALID != ai) |
| 1496 | ip_neighbor_probe_dst (adj_get (ai), ip_neighbor_get_ip (ipn)); |
| 1497 | |
| 1498 | ipn->ipn_n_probes++; |
| 1499 | *wait = 1; |
| 1500 | } |
| 1501 | } |
| 1502 | else |
| 1503 | { |
Vladimir Isaev | 1284f8c | 2020-02-18 15:26:12 +0300 | [diff] [blame] | 1504 | /* here we are sure that ttl <= ipndb_age */ |
| 1505 | *wait = ipndb_age - ttl + 1; |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 1506 | return (IP_NEIGHBOR_AGE_ALIVE); |
| 1507 | } |
| 1508 | |
| 1509 | return (IP_NEIGHBOR_AGE_PROBE); |
| 1510 | } |
| 1511 | |
| 1512 | typedef enum ip_neighbor_process_event_t_ |
| 1513 | { |
| 1514 | IP_NEIGHBOR_AGE_PROCESS_WAKEUP, |
| 1515 | } ip_neighbor_process_event_t; |
| 1516 | |
| 1517 | static uword |
| 1518 | ip_neighbor_age_loop (vlib_main_t * vm, |
| 1519 | vlib_node_runtime_t * rt, |
| 1520 | vlib_frame_t * f, ip46_type_t type) |
| 1521 | { |
| 1522 | uword event_type, *event_data = NULL; |
| 1523 | f64 timeout; |
| 1524 | |
| 1525 | /* Set the timeout to an effectively infinite value when the process starts */ |
| 1526 | timeout = IP_NEIGHBOR_PROCESS_SLEEP_LONG; |
| 1527 | |
| 1528 | while (1) |
| 1529 | { |
| 1530 | f64 now; |
| 1531 | |
| 1532 | if (!timeout) |
| 1533 | vlib_process_wait_for_event (vm); |
| 1534 | else |
| 1535 | vlib_process_wait_for_event_or_clock (vm, timeout); |
| 1536 | |
| 1537 | event_type = vlib_process_get_events (vm, &event_data); |
| 1538 | vec_reset_length (event_data); |
| 1539 | |
| 1540 | now = vlib_time_now (vm); |
| 1541 | |
| 1542 | switch (event_type) |
| 1543 | { |
| 1544 | case ~0: |
| 1545 | { |
| 1546 | /* timer expired */ |
| 1547 | ip_neighbor_elt_t *elt, *head; |
| 1548 | f64 wait; |
| 1549 | |
Vladimir Isaev | 1284f8c | 2020-02-18 15:26:12 +0300 | [diff] [blame] | 1550 | timeout = ip_neighbor_db[type].ipndb_age; |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 1551 | head = pool_elt_at_index (ip_neighbor_elt_pool, |
| 1552 | ip_neighbor_list_head[type]); |
| 1553 | |
| 1554 | /* *INDENT-OFF*/ |
| 1555 | /* the list is time sorted, newest first, so start from the back |
| 1556 | * and work forwards. Stop when we get to one that is alive */ |
| 1557 | restart: |
| 1558 | clib_llist_foreach_reverse(ip_neighbor_elt_pool, |
| 1559 | ipne_anchor, head, elt, |
| 1560 | ({ |
| 1561 | ip_neighbor_age_state_t res; |
| 1562 | |
| 1563 | res = ip_neighbour_age_out(elt->ipne_index, now, &wait); |
| 1564 | |
| 1565 | if (IP_NEIGHBOR_AGE_ALIVE == res) { |
| 1566 | /* the oldest neighbor has not yet expired, go back to sleep */ |
Vladimir Isaev | 1284f8c | 2020-02-18 15:26:12 +0300 | [diff] [blame] | 1567 | timeout = clib_min (wait, timeout); |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 1568 | break; |
| 1569 | } |
| 1570 | else if (IP_NEIGHBOR_AGE_DEAD == res) { |
| 1571 | /* the oldest neighbor is dead, pop it, then restart the walk |
| 1572 | * again from the back */ |
| 1573 | ip_neighbor_free (ip_neighbor_get(elt->ipne_index)); |
| 1574 | goto restart; |
| 1575 | } |
| 1576 | |
| 1577 | timeout = clib_min (wait, timeout); |
| 1578 | })); |
| 1579 | /* *INDENT-ON* */ |
| 1580 | break; |
| 1581 | } |
| 1582 | case IP_NEIGHBOR_AGE_PROCESS_WAKEUP: |
| 1583 | { |
| 1584 | |
| 1585 | if (!ip_neighbor_db[type].ipndb_age) |
| 1586 | { |
| 1587 | /* aging has been disabled */ |
| 1588 | timeout = 0; |
| 1589 | break; |
| 1590 | } |
| 1591 | ip_neighbor_elt_t *elt, *head; |
| 1592 | |
| 1593 | head = pool_elt_at_index (ip_neighbor_elt_pool, |
| 1594 | ip_neighbor_list_head[type]); |
Vladimir Isaev | 1284f8c | 2020-02-18 15:26:12 +0300 | [diff] [blame] | 1595 | /* no neighbors yet */ |
| 1596 | if (clib_llist_is_empty (ip_neighbor_elt_pool, ipne_anchor, head)) |
| 1597 | { |
| 1598 | timeout = ip_neighbor_db[type].ipndb_age; |
| 1599 | break; |
| 1600 | } |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 1601 | |
| 1602 | /* poke the oldset neighbour for aging, which returns how long we sleep for */ |
Vladimir Isaev | 1284f8c | 2020-02-18 15:26:12 +0300 | [diff] [blame] | 1603 | elt = clib_llist_prev (ip_neighbor_elt_pool, ipne_anchor, head); |
| 1604 | ip_neighbour_age_out (elt->ipne_index, now, &timeout); |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 1605 | break; |
| 1606 | } |
| 1607 | } |
| 1608 | } |
| 1609 | return 0; |
| 1610 | } |
| 1611 | |
| 1612 | static uword |
| 1613 | ip4_neighbor_age_process (vlib_main_t * vm, |
| 1614 | vlib_node_runtime_t * rt, vlib_frame_t * f) |
| 1615 | { |
| 1616 | return (ip_neighbor_age_loop (vm, rt, f, IP46_TYPE_IP4)); |
| 1617 | } |
| 1618 | |
| 1619 | static uword |
| 1620 | ip6_neighbor_age_process (vlib_main_t * vm, |
| 1621 | vlib_node_runtime_t * rt, vlib_frame_t * f) |
| 1622 | { |
| 1623 | return (ip_neighbor_age_loop (vm, rt, f, IP46_TYPE_IP6)); |
| 1624 | } |
| 1625 | |
| 1626 | /* *INDENT-OFF* */ |
| 1627 | VLIB_REGISTER_NODE (ip4_neighbor_age_process_node,static) = { |
| 1628 | .function = ip4_neighbor_age_process, |
| 1629 | .type = VLIB_NODE_TYPE_PROCESS, |
| 1630 | .name = "ip4-neighbor-age-process", |
| 1631 | }; |
| 1632 | VLIB_REGISTER_NODE (ip6_neighbor_age_process_node,static) = { |
| 1633 | .function = ip6_neighbor_age_process, |
| 1634 | .type = VLIB_NODE_TYPE_PROCESS, |
| 1635 | .name = "ip6-neighbor-age-process", |
| 1636 | }; |
| 1637 | /* *INDENT-ON* */ |
| 1638 | |
| 1639 | int |
| 1640 | ip_neighbor_config (ip46_type_t type, u32 limit, u32 age, bool recycle) |
| 1641 | { |
| 1642 | ip_neighbor_db[type].ipndb_limit = limit; |
| 1643 | ip_neighbor_db[type].ipndb_recycle = recycle; |
| 1644 | ip_neighbor_db[type].ipndb_age = age; |
| 1645 | |
| 1646 | vlib_process_signal_event (vlib_get_main (), |
| 1647 | (IP46_TYPE_IP4 == type ? |
| 1648 | ip4_neighbor_age_process_node.index : |
| 1649 | ip6_neighbor_age_process_node.index), |
| 1650 | IP_NEIGHBOR_AGE_PROCESS_WAKEUP, 0); |
| 1651 | |
| 1652 | return (0); |
| 1653 | } |
| 1654 | |
| 1655 | static clib_error_t * |
| 1656 | ip_neighbor_config_show (vlib_main_t * vm, |
| 1657 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 1658 | { |
| 1659 | ip46_type_t type; |
| 1660 | |
| 1661 | /* *INDENT-OFF* */ |
| 1662 | FOREACH_IP46_TYPE(type) { |
| 1663 | vlib_cli_output (vm, "%U:", format_ip46_type, type); |
| 1664 | vlib_cli_output (vm, " limit:%d, age:%d, recycle:%d", |
| 1665 | ip_neighbor_db[type].ipndb_limit, |
| 1666 | ip_neighbor_db[type].ipndb_age, |
| 1667 | ip_neighbor_db[type].ipndb_recycle); |
| 1668 | } |
| 1669 | |
| 1670 | /* *INDENT-ON* */ |
| 1671 | return (NULL); |
| 1672 | } |
| 1673 | |
| 1674 | /* *INDENT-OFF* */ |
| 1675 | VLIB_CLI_COMMAND (show_ip_neighbor_cfg_cmd_node, static) = { |
| 1676 | .path = "show ip neighbor-config", |
| 1677 | .function = ip_neighbor_config_show, |
| 1678 | .short_help = "show ip neighbor-config", |
| 1679 | }; |
| 1680 | /* *INDENT-ON* */ |
| 1681 | |
| 1682 | static clib_error_t * |
| 1683 | ip_neighbor_init (vlib_main_t * vm) |
| 1684 | { |
| 1685 | { |
| 1686 | ip4_add_del_interface_address_callback_t cb = { |
| 1687 | .function = ip_neighbor_add_del_interface_address_v4, |
| 1688 | }; |
| 1689 | vec_add1 (ip4_main.add_del_interface_address_callbacks, cb); |
| 1690 | } |
| 1691 | { |
| 1692 | ip6_add_del_interface_address_callback_t cb = { |
| 1693 | .function = ip_neighbor_add_del_interface_address_v6, |
| 1694 | }; |
| 1695 | vec_add1 (ip6_main.add_del_interface_address_callbacks, cb); |
| 1696 | } |
| 1697 | { |
| 1698 | ip4_table_bind_callback_t cb = { |
| 1699 | .function = ip_neighbor_table_bind_v4, |
| 1700 | }; |
| 1701 | vec_add1 (ip4_main.table_bind_callbacks, cb); |
| 1702 | } |
| 1703 | { |
| 1704 | ip6_table_bind_callback_t cb = { |
| 1705 | .function = ip_neighbor_table_bind_v6, |
| 1706 | }; |
| 1707 | vec_add1 (ip6_main.table_bind_callbacks, cb); |
| 1708 | } |
| 1709 | { |
| 1710 | ethernet_address_change_ctx_t ctx = { |
| 1711 | .function = ip_neighbor_ethernet_change_mac, |
| 1712 | .function_opaque = 0, |
| 1713 | }; |
| 1714 | vec_add1 (ethernet_main.address_change_callbacks, ctx); |
| 1715 | } |
| 1716 | |
| 1717 | ipn_logger = vlib_log_register_class ("ip", "neighbor"); |
| 1718 | |
| 1719 | ip46_type_t type; |
| 1720 | |
| 1721 | FOREACH_IP46_TYPE (type) |
| 1722 | ip_neighbor_list_head[type] = |
| 1723 | clib_llist_make_head (ip_neighbor_elt_pool, ipne_anchor); |
| 1724 | |
| 1725 | return (NULL); |
| 1726 | } |
| 1727 | |
| 1728 | /* *INDENT-OFF* */ |
| 1729 | VLIB_INIT_FUNCTION (ip_neighbor_init) = |
| 1730 | { |
| 1731 | .runs_after = VLIB_INITS("ip_main_init"), |
| 1732 | }; |
| 1733 | /* *INDENT-ON* */ |
| 1734 | |
| 1735 | /* |
| 1736 | * fd.io coding-style-patch-verification: ON |
| 1737 | * |
| 1738 | * Local Variables: |
| 1739 | * eval: (c-set-style "gnu") |
| 1740 | * End: |
| 1741 | */ |