John Lo | 7f358b3 | 2018-04-28 01:19:24 -0400 | [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 <vnet/vnet.h> |
| 19 | #include <vnet/ip/ip.h> |
| 20 | #include <vnet/ip/ip6_neighbor.h> |
| 21 | #include <vnet/ip/ip_neighbor.h> |
Neale Ranns | 0053de6 | 2018-05-22 08:40:52 -0700 | [diff] [blame] | 22 | #include <vnet/ethernet/arp.h> |
John Lo | 7f358b3 | 2018-04-28 01:19:24 -0400 | [diff] [blame] | 23 | |
| 24 | /* |
| 25 | * IP neighbor scan parameter defaults are as follows: |
| 26 | * - Scan interval : 60 sec |
| 27 | * - Max processing allowed per run : 20 usec |
| 28 | * - Max probe/delete operations per run : 10 |
| 29 | * - Scan interrupt delay to resume scan : 1 msec |
| 30 | * - Neighbor stale threashold : 4 x scan-interval |
| 31 | */ |
| 32 | #define IP_NEIGHBOR_DEF_SCAN_INTERVAL (60.0) |
| 33 | #define IP_NEIGHBOR_DEF_MAX_PROC_TIME (20e-6) |
| 34 | #define IP_NEIGHBOR_DEF_SCAN_INT_DELAY (1e-3) |
| 35 | #define IP_NEIGHBOR_DEF_STALE (4*IP_NEIGHBOR_DEF_SCAN_INTERVAL) |
| 36 | #define IP_NEIGHBOR_DEF_MAX_UPDATE 10 |
| 37 | |
| 38 | typedef struct |
| 39 | { |
| 40 | f64 scan_interval; /* Periodic scan interval */ |
| 41 | f64 max_proc_time; /* Max processing time allowed per run */ |
| 42 | f64 scan_int_delay; /* Scan interrupt delay to resume scan */ |
| 43 | f64 stale_threshold; /* IP neighbor stale threshod */ |
| 44 | u8 max_update; /* Max probe/delete actions allowed per run */ |
| 45 | u8 mode; /* IP neighbor scan mode */ |
| 46 | } ip_neighbor_scan_config_t; |
| 47 | |
| 48 | static ip_neighbor_scan_config_t ip_neighbor_scan_conf; |
| 49 | |
Neale Ranns | 0bdd319 | 2018-09-07 11:04:52 -0700 | [diff] [blame] | 50 | int |
| 51 | ip_neighbor_add (const ip46_address_t * ip, |
| 52 | u8 is_ip6, |
| 53 | const u8 * mac, u32 sw_if_index, ip_neighbor_flags_t flags) |
| 54 | { |
| 55 | int rv; |
| 56 | |
| 57 | /* |
| 58 | * there's no validation here of the ND/ARP entry being added. |
| 59 | * The expectation is that the FIB will ensure that nothing bad |
| 60 | * will come of adding bogus entries. |
| 61 | */ |
| 62 | if (is_ip6) |
| 63 | { |
| 64 | rv = vnet_set_ip6_ethernet_neighbor (vlib_get_main (), |
| 65 | sw_if_index, &ip->ip6, mac, 6, |
| 66 | (flags & IP_NEIGHBOR_FLAG_STATIC), |
| 67 | (flags & |
| 68 | IP_NEIGHBOR_FLAG_NO_ADJ_FIB)); |
| 69 | |
| 70 | } |
| 71 | else |
| 72 | { |
| 73 | ethernet_arp_ip4_over_ethernet_address_t a = { |
| 74 | .ip4 = ip->ip4, |
| 75 | }; |
| 76 | |
| 77 | clib_memcpy (&a.ethernet, mac, 6); |
| 78 | |
| 79 | rv = vnet_arp_set_ip4_over_ethernet (vnet_get_main (), |
| 80 | sw_if_index, |
| 81 | &a, |
| 82 | (flags & IP_NEIGHBOR_FLAG_STATIC), |
| 83 | (flags & |
| 84 | IP_NEIGHBOR_FLAG_NO_ADJ_FIB)); |
| 85 | } |
| 86 | |
| 87 | return (rv); |
| 88 | } |
| 89 | |
| 90 | int |
| 91 | ip_neighbor_del (const ip46_address_t * ip, u8 is_ip6, u32 sw_if_index) |
| 92 | { |
| 93 | int rv; |
| 94 | |
| 95 | if (is_ip6) |
| 96 | { |
| 97 | rv = vnet_unset_ip6_ethernet_neighbor (vlib_get_main (), |
| 98 | sw_if_index, &ip->ip6); |
| 99 | } |
| 100 | else |
| 101 | { |
| 102 | ethernet_arp_ip4_over_ethernet_address_t a = { |
| 103 | .ip4 = ip->ip4, |
| 104 | }; |
| 105 | |
| 106 | rv = |
| 107 | vnet_arp_unset_ip4_over_ethernet (vnet_get_main (), sw_if_index, &a); |
| 108 | } |
| 109 | |
| 110 | return (rv); |
| 111 | } |
| 112 | |
John Lo | 7f358b3 | 2018-04-28 01:19:24 -0400 | [diff] [blame] | 113 | void |
| 114 | ip_neighbor_scan_enable_disable (ip_neighbor_scan_arg_t * arg) |
| 115 | { |
| 116 | ip_neighbor_scan_config_t *cfg = &ip_neighbor_scan_conf; |
| 117 | |
| 118 | cfg->mode = arg->mode; |
| 119 | |
| 120 | if (arg->mode) |
| 121 | { |
| 122 | cfg->scan_interval = arg->scan_interval ? |
John Lo | 3c22aff | 2018-06-07 12:00:43 -0400 | [diff] [blame] | 123 | arg->scan_interval * 60.0 : IP_NEIGHBOR_DEF_SCAN_INTERVAL; |
John Lo | 7f358b3 | 2018-04-28 01:19:24 -0400 | [diff] [blame] | 124 | cfg->max_proc_time = arg->max_proc_time ? |
| 125 | arg->max_proc_time * 1e-6 : IP_NEIGHBOR_DEF_MAX_PROC_TIME; |
| 126 | cfg->scan_int_delay = arg->scan_int_delay ? |
| 127 | arg->scan_int_delay * 1e-3 : IP_NEIGHBOR_DEF_SCAN_INT_DELAY; |
| 128 | cfg->stale_threshold = arg->stale_threshold ? |
John Lo | 3c22aff | 2018-06-07 12:00:43 -0400 | [diff] [blame] | 129 | arg->stale_threshold * 60.0 : cfg->scan_interval * 4; |
John Lo | 7f358b3 | 2018-04-28 01:19:24 -0400 | [diff] [blame] | 130 | cfg->max_update = arg->max_update ? |
| 131 | cfg->max_update : IP_NEIGHBOR_DEF_MAX_UPDATE; |
| 132 | } |
| 133 | else |
| 134 | cfg->scan_interval = IP_NEIGHBOR_DEF_SCAN_INTERVAL; |
| 135 | } |
| 136 | |
| 137 | static_always_inline u32 |
| 138 | ip_neighbor_scan (vlib_main_t * vm, f64 start_time, u32 start_idx, |
| 139 | u8 is_ip6, u8 delete_stale, u8 * update_count) |
| 140 | { |
| 141 | vnet_main_t *vnm = vnet_get_main (); |
| 142 | ip_neighbor_scan_config_t *cfg = &ip_neighbor_scan_conf; |
| 143 | ethernet_arp_ip4_entry_t *np4 = ip4_neighbors_pool (); |
| 144 | ip6_neighbor_t *np6 = ip6_neighbors_pool (); |
| 145 | ethernet_arp_ip4_entry_t *n4; |
| 146 | ip6_neighbor_t *n6; |
| 147 | u32 curr_idx = start_idx; |
| 148 | u32 loop_count = 0; |
| 149 | f64 delta, update_time; |
| 150 | |
| 151 | if (!is_ip6) |
| 152 | { |
| 153 | if (pool_is_free_index (np4, start_idx)) |
| 154 | curr_idx = pool_next_index (np4, start_idx); |
| 155 | } |
| 156 | else |
| 157 | { |
| 158 | if (pool_is_free_index (np6, start_idx)) |
| 159 | curr_idx = pool_next_index (np6, start_idx); |
| 160 | } |
| 161 | |
| 162 | while (curr_idx != ~0) |
| 163 | { |
| 164 | /* allow no more than 10 neighbor updates or 20 usec of scan */ |
| 165 | if ((update_count[0] >= cfg->max_update) || |
| 166 | (((loop_count % 100) == 0) && |
| 167 | ((vlib_time_now (vm) - start_time) > cfg->max_proc_time))) |
| 168 | break; |
| 169 | |
| 170 | if (!is_ip6) |
| 171 | { |
| 172 | n4 = pool_elt_at_index (np4, curr_idx); |
| 173 | if (n4->flags & ETHERNET_ARP_IP4_ENTRY_FLAG_STATIC) |
| 174 | goto next_neighbor; |
| 175 | update_time = n4->time_last_updated; |
| 176 | } |
| 177 | else |
| 178 | { |
| 179 | n6 = pool_elt_at_index (np6, curr_idx); |
| 180 | if (n6->flags & IP6_NEIGHBOR_FLAG_STATIC) |
| 181 | goto next_neighbor; |
| 182 | update_time = n6->time_last_updated; |
| 183 | } |
| 184 | |
| 185 | delta = start_time - update_time; |
| 186 | if (delete_stale && (delta >= cfg->stale_threshold)) |
| 187 | { |
| 188 | update_count[0]++; |
| 189 | /* delete stale neighbor */ |
| 190 | if (!is_ip6) |
| 191 | { |
| 192 | ethernet_arp_ip4_over_ethernet_address_t delme; |
| 193 | clib_memcpy (&delme.ethernet, n4->ethernet_address, 6); |
| 194 | delme.ip4.as_u32 = n4->ip4_address.as_u32; |
| 195 | vnet_arp_unset_ip4_over_ethernet (vnm, n4->sw_if_index, &delme); |
| 196 | } |
| 197 | else |
| 198 | { |
| 199 | vnet_unset_ip6_ethernet_neighbor |
Neale Ranns | 0bdd319 | 2018-09-07 11:04:52 -0700 | [diff] [blame] | 200 | (vm, n6->key.sw_if_index, &n6->key.ip6_address); |
John Lo | 7f358b3 | 2018-04-28 01:19:24 -0400 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | else if (delta >= cfg->scan_interval) |
| 204 | { |
| 205 | update_count[0]++; |
| 206 | /* probe neighbor */ |
| 207 | if (!is_ip6) |
John Lo | 8637634 | 2018-06-11 20:14:49 -0400 | [diff] [blame] | 208 | ip4_probe_neighbor (vm, &n4->ip4_address, n4->sw_if_index, 1); |
John Lo | 7f358b3 | 2018-04-28 01:19:24 -0400 | [diff] [blame] | 209 | else |
| 210 | ip6_probe_neighbor (vm, &n6->key.ip6_address, |
John Lo | 8637634 | 2018-06-11 20:14:49 -0400 | [diff] [blame] | 211 | n6->key.sw_if_index, 1); |
John Lo | 7f358b3 | 2018-04-28 01:19:24 -0400 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | next_neighbor: |
| 215 | loop_count++; |
| 216 | |
| 217 | if (!is_ip6) |
| 218 | curr_idx = pool_next_index (np4, curr_idx); |
| 219 | else |
| 220 | curr_idx = pool_next_index (np6, curr_idx); |
| 221 | } |
| 222 | |
| 223 | return curr_idx; |
| 224 | } |
| 225 | |
| 226 | static uword |
| 227 | neighbor_scan_process (vlib_main_t * vm, |
| 228 | vlib_node_runtime_t * rt, vlib_frame_t * f) |
| 229 | { |
| 230 | ip_neighbor_scan_config_t *cfg = &ip_neighbor_scan_conf; |
| 231 | f64 timeout = IP_NEIGHBOR_DEF_SCAN_INTERVAL; |
| 232 | f64 start, next_scan = CLIB_TIME_MAX; |
| 233 | u32 ip4_nidx = 0; /* ip4 neighbor pool index */ |
| 234 | u32 ip6_nidx = 0; /* ip6 neighbor pool index */ |
| 235 | uword *event_data = 0; |
| 236 | u8 purge4 = 0, purge6 = 0; /* flags to purge stale entry during scan */ |
| 237 | u8 update; |
| 238 | |
| 239 | cfg->mode = IP_SCAN_DISABLED; |
| 240 | cfg->scan_interval = IP_NEIGHBOR_DEF_SCAN_INTERVAL; |
| 241 | cfg->scan_int_delay = IP_NEIGHBOR_DEF_SCAN_INTERVAL; |
| 242 | |
| 243 | while (1) |
| 244 | { |
| 245 | vlib_process_wait_for_event_or_clock (vm, timeout); |
| 246 | vlib_process_get_events (vm, &event_data); |
| 247 | vec_reset_length (event_data); |
| 248 | |
| 249 | start = vlib_time_now (vm); |
| 250 | update = 0; |
| 251 | |
| 252 | if ((ip4_nidx == 0) && (ip6_nidx == 0)) /* starting a fresh scan */ |
| 253 | next_scan = start + cfg->scan_interval; |
| 254 | |
| 255 | if ((cfg->mode & IP_SCAN_V4_NEIGHBORS) == 0) |
| 256 | ip4_nidx = ~0; /* disable ip4 neighbor scan */ |
| 257 | |
| 258 | if ((cfg->mode & IP_SCAN_V6_NEIGHBORS) == 0) |
| 259 | ip6_nidx = ~0; /* disable ip6 neighbor scan */ |
| 260 | |
| 261 | if (ip4_nidx != ~0) /* scan ip4 neighbors */ |
| 262 | ip4_nidx = ip_neighbor_scan (vm, start, ip4_nidx, /* ip4 */ 0, |
| 263 | purge4, &update); |
| 264 | |
| 265 | if (ip6_nidx != ~0) /* scan ip6 neighbors */ |
| 266 | ip6_nidx = ip_neighbor_scan (vm, start, ip6_nidx, /* ip6 */ 1, |
| 267 | purge6, &update); |
| 268 | |
| 269 | if ((ip4_nidx == ~0) && (ip6_nidx == ~0)) |
| 270 | { /* scan complete */ |
| 271 | timeout = next_scan - vlib_time_now (vm); |
| 272 | ip4_nidx = ip6_nidx = 0; |
| 273 | purge4 = cfg->mode & IP_SCAN_V4_NEIGHBORS; |
| 274 | purge6 = cfg->mode & IP_SCAN_V6_NEIGHBORS; |
| 275 | } |
| 276 | else /* scan incomplete */ |
| 277 | timeout = cfg->scan_int_delay; |
| 278 | |
| 279 | if (timeout > cfg->scan_interval) |
| 280 | timeout = cfg->scan_interval; |
| 281 | else if (timeout < cfg->scan_int_delay) |
| 282 | timeout = cfg->scan_int_delay; |
| 283 | |
| 284 | } |
| 285 | return 0; |
| 286 | } |
| 287 | |
| 288 | /* *INDENT-OFF* */ |
| 289 | VLIB_REGISTER_NODE (neighbor_scan_process_node,static) = { |
| 290 | .function = neighbor_scan_process, |
| 291 | .type = VLIB_NODE_TYPE_PROCESS, |
| 292 | .name = "ip-neighbor-scan-process", |
| 293 | }; |
| 294 | /* *INDENT-ON* */ |
| 295 | |
| 296 | static clib_error_t * |
| 297 | ip_neighbor_scan_cli (vlib_main_t * vm, unformat_input_t * input, |
| 298 | vlib_cli_command_t * cmd) |
| 299 | { |
| 300 | unformat_input_t _line_input, *line_input = &_line_input; |
| 301 | clib_error_t *error = 0; |
| 302 | u32 interval = 0, time = 0, update = 0, delay = 0, stale = 0; |
| 303 | ip_neighbor_scan_arg_t arg; |
| 304 | |
| 305 | memset (&arg, 0, sizeof (arg)); |
| 306 | arg.mode = IP_SCAN_V46_NEIGHBORS; |
| 307 | |
| 308 | /* Get a line of input. */ |
| 309 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 310 | { |
| 311 | ip_neighbor_scan_enable_disable (&arg); |
| 312 | return error; |
| 313 | } |
| 314 | |
| 315 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 316 | { |
| 317 | if (unformat (line_input, "ip4")) |
| 318 | arg.mode = IP_SCAN_V4_NEIGHBORS; |
| 319 | |
| 320 | else if (unformat (line_input, "ip6")) |
| 321 | arg.mode = IP_SCAN_V6_NEIGHBORS; |
| 322 | |
| 323 | else if (unformat (line_input, "both")) |
| 324 | arg.mode = IP_SCAN_V46_NEIGHBORS; |
| 325 | |
| 326 | else if (unformat (line_input, "disable")) |
| 327 | arg.mode = IP_SCAN_DISABLED; |
| 328 | |
| 329 | else if (unformat (line_input, "interval %d", &interval)) |
| 330 | arg.scan_interval = interval; |
| 331 | |
| 332 | else if (unformat (line_input, "max-time %d", &time)) |
| 333 | arg.max_proc_time = time; |
| 334 | |
| 335 | else if (unformat (line_input, "max-update %d", &update)) |
| 336 | arg.max_update = update; |
| 337 | |
| 338 | else if (unformat (line_input, "delay %d", &delay)) |
| 339 | arg.scan_int_delay = delay; |
| 340 | |
| 341 | else if (unformat (line_input, "stale %d", &stale)) |
| 342 | arg.stale_threshold = stale; |
| 343 | |
| 344 | else |
| 345 | { |
| 346 | error = clib_error_return (0, "unknown input '%U'", |
| 347 | format_unformat_error, line_input); |
| 348 | goto done; |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | if (interval > 255) |
| 353 | { |
| 354 | error = clib_error_return (0, "interval cannot exceed 255 minutes."); |
| 355 | goto done; |
| 356 | } |
| 357 | if (time > 255) |
| 358 | { |
| 359 | error = clib_error_return (0, "max-time cannot exceed 255 usec."); |
| 360 | goto done; |
| 361 | } |
| 362 | if (update > 255) |
| 363 | { |
| 364 | error = clib_error_return (0, "max-update cannot exceed 255."); |
| 365 | goto done; |
| 366 | } |
| 367 | if (delay > 255) |
| 368 | { |
| 369 | error = clib_error_return (0, "delay cannot exceed 255 msec."); |
| 370 | goto done; |
| 371 | } |
| 372 | if (stale > 255) |
| 373 | { |
| 374 | error = clib_error_return (0, "stale cannot exceed 255 minutes."); |
| 375 | goto done; |
| 376 | } |
| 377 | |
| 378 | ip_neighbor_scan_enable_disable (&arg); |
| 379 | |
| 380 | done: |
| 381 | unformat_free (line_input); |
| 382 | |
| 383 | return error; |
| 384 | } |
| 385 | |
| 386 | /*? |
| 387 | * The '<em>ip scan-neighbor</em>' command can be used to enable and disable |
| 388 | * periodic IP neighbor scan and change various scan parameneters. |
| 389 | * |
| 390 | * @note The default parameters used for IP neighbor scan should work fine |
| 391 | * under normal conditions. They should not be changed from the default unless |
| 392 | * properly tested to work as desied. |
| 393 | * |
| 394 | * @cliexpar |
| 395 | * Example of enabling IP neighbor scan: |
| 396 | * @cliexcmd{ip neighbor-scan enable} |
| 397 | ?*/ |
| 398 | /* *INDENT-OFF* */ |
| 399 | VLIB_CLI_COMMAND (ip_scan_neighbor_command, static) = { |
| 400 | .path = "ip scan-neighbor", |
| 401 | .function = ip_neighbor_scan_cli, |
| 402 | .short_help = "ip scan-neighbor [ip4|ip6|both|disable] [interval <n-min>] [max-time <n-usec>] [max-update <n>] [delay <n-msec>] [stale <n-min>]", |
| 403 | .is_mp_safe = 1, |
| 404 | }; |
| 405 | /* *INDENT-ON* */ |
| 406 | |
| 407 | static u8 * |
| 408 | format_ip_scan_mode (u8 * s, va_list * args) |
| 409 | { |
| 410 | u8 mode = va_arg (*args, u32); |
| 411 | switch (mode) |
| 412 | { |
| 413 | case IP_SCAN_V4_NEIGHBORS: |
| 414 | return format (s, "IPv4"); |
| 415 | case IP_SCAN_V6_NEIGHBORS: |
| 416 | return format (s, "IPv6"); |
| 417 | case IP_SCAN_V46_NEIGHBORS: |
| 418 | return format (s, "IPv4 and IPv6"); |
| 419 | } |
| 420 | return format (s, "unknown"); |
| 421 | } |
| 422 | |
| 423 | static clib_error_t * |
| 424 | show_ip_neighbor_scan (vlib_main_t * vm, unformat_input_t * input, |
| 425 | vlib_cli_command_t * cmd) |
| 426 | { |
| 427 | ip_neighbor_scan_config_t *cfg = &ip_neighbor_scan_conf; |
| 428 | |
| 429 | if (cfg->mode == 0) |
| 430 | vlib_cli_output (vm, |
| 431 | "IP neighbor scan disabled - current time is %.4f sec", |
| 432 | vlib_time_now (vm)); |
| 433 | else |
| 434 | vlib_cli_output (vm, "IP neighbor scan enabled for %U neighbors - " |
| 435 | "current time is %.4f sec\n " |
| 436 | "Full_scan_interval: %f min " |
| 437 | "Stale_purge_threshod: %f min\n " |
| 438 | "Max_process_time: %f usec Max_updates %d " |
| 439 | "Delay_to_resume_after_max_limit: %f msec", |
| 440 | format_ip_scan_mode, cfg->mode, |
| 441 | vlib_time_now (vm), cfg->scan_interval / 60.0, |
| 442 | cfg->stale_threshold / 60.0, cfg->max_proc_time / 1e-6, |
| 443 | cfg->max_update, cfg->scan_int_delay / 1e-3); |
| 444 | return 0; |
| 445 | } |
| 446 | |
| 447 | /*? |
| 448 | * The '<em>show ip scan-neighbor</em>' command can be used to show the current |
| 449 | * periodic IP neighbor scan parameters |
| 450 | * |
| 451 | * @cliexpar |
| 452 | * Example of showing IP neighbor scan current parameters: |
| 453 | * @cliexcmd{show ip neighbor-scan} |
| 454 | ?*/ |
| 455 | /* *INDENT-OFF* */ |
| 456 | VLIB_CLI_COMMAND (show_ip_scan_neighbor_command, static) = { |
| 457 | .path = "show ip scan-neighbor", |
| 458 | .function = show_ip_neighbor_scan, |
| 459 | .short_help = "show ip scan-neighbor", |
| 460 | .is_mp_safe = 1, |
| 461 | }; |
| 462 | /* *INDENT-ON* */ |
| 463 | |
| 464 | /* |
| 465 | * fd.io coding-style-patch-verification: ON |
| 466 | * |
| 467 | * Local Variables: |
| 468 | * eval: (c-set-style "gnu") |
| 469 | * End: |
| 470 | */ |