blob: bd0144101ed032116f9e1008c913c771a95c2269 [file] [log] [blame]
John Lo7f358b32018-04-28 01:19:24 -04001/*
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 Ranns0053de62018-05-22 08:40:52 -070022#include <vnet/ethernet/arp.h>
John Lo7f358b32018-04-28 01:19:24 -040023
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
38typedef 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
48static ip_neighbor_scan_config_t ip_neighbor_scan_conf;
49
BenoƮt Ganneadbaf7b2019-07-10 15:40:33 +020050u8 *
51format_ip_neighbor_flags (u8 * s, va_list * args)
52{
53 const ip_neighbor_flags_t flags = va_arg (*args, int);
54
55 if (flags & IP_NEIGHBOR_FLAG_STATIC)
56 s = format (s, "S");
57
58 if (flags & IP_NEIGHBOR_FLAG_DYNAMIC)
59 s = format (s, "D");
60
61 if (flags & IP_NEIGHBOR_FLAG_NO_FIB_ENTRY)
62 s = format (s, "N");
63
64 return s;
65}
66
Neale Ranns0bdd3192018-09-07 11:04:52 -070067int
68ip_neighbor_add (const ip46_address_t * ip,
Neale Ranns37029302018-08-10 05:30:06 -070069 ip46_type_t type,
70 const mac_address_t * mac,
Neale Ranns14260392018-09-28 05:00:57 -070071 u32 sw_if_index,
72 ip_neighbor_flags_t flags, u32 * stats_index)
Neale Ranns0bdd3192018-09-07 11:04:52 -070073{
Neale Ranns14260392018-09-28 05:00:57 -070074 fib_protocol_t fproto;
75 vnet_link_t linkt;
Neale Ranns0bdd3192018-09-07 11:04:52 -070076 int rv;
77
78 /*
79 * there's no validation here of the ND/ARP entry being added.
80 * The expectation is that the FIB will ensure that nothing bad
81 * will come of adding bogus entries.
82 */
Neale Ranns37029302018-08-10 05:30:06 -070083 if (IP46_TYPE_IP6 == type)
Neale Ranns0bdd3192018-09-07 11:04:52 -070084 {
85 rv = vnet_set_ip6_ethernet_neighbor (vlib_get_main (),
Neale Ranns37029302018-08-10 05:30:06 -070086 sw_if_index, &ip->ip6, mac, flags);
Neale Ranns14260392018-09-28 05:00:57 -070087 fproto = FIB_PROTOCOL_IP6;
88 linkt = VNET_LINK_IP6;
Neale Ranns0bdd3192018-09-07 11:04:52 -070089 }
90 else
91 {
92 ethernet_arp_ip4_over_ethernet_address_t a = {
93 .ip4 = ip->ip4,
Neale Ranns37029302018-08-10 05:30:06 -070094 .mac = *mac,
Neale Ranns0bdd3192018-09-07 11:04:52 -070095 };
96
Neale Ranns37029302018-08-10 05:30:06 -070097 rv =
98 vnet_arp_set_ip4_over_ethernet (vnet_get_main (), sw_if_index, &a,
99 flags);
Neale Ranns14260392018-09-28 05:00:57 -0700100 fproto = FIB_PROTOCOL_IP4;
101 linkt = VNET_LINK_IP4;
Neale Ranns0bdd3192018-09-07 11:04:52 -0700102 }
103
Neale Ranns14260392018-09-28 05:00:57 -0700104 if (0 == rv && stats_index)
105 *stats_index = adj_nbr_find (fproto, linkt, ip, sw_if_index);
106
Neale Ranns0bdd3192018-09-07 11:04:52 -0700107 return (rv);
108}
109
110int
Neale Ranns37029302018-08-10 05:30:06 -0700111ip_neighbor_del (const ip46_address_t * ip, ip46_type_t type, u32 sw_if_index)
Neale Ranns0bdd3192018-09-07 11:04:52 -0700112{
113 int rv;
114
Neale Ranns37029302018-08-10 05:30:06 -0700115 if (IP46_TYPE_IP6 == type)
Neale Ranns0bdd3192018-09-07 11:04:52 -0700116 {
117 rv = vnet_unset_ip6_ethernet_neighbor (vlib_get_main (),
118 sw_if_index, &ip->ip6);
119 }
120 else
121 {
122 ethernet_arp_ip4_over_ethernet_address_t a = {
123 .ip4 = ip->ip4,
124 };
125
126 rv =
127 vnet_arp_unset_ip4_over_ethernet (vnet_get_main (), sw_if_index, &a);
128 }
129
130 return (rv);
131}
132
John Lo7f358b32018-04-28 01:19:24 -0400133void
134ip_neighbor_scan_enable_disable (ip_neighbor_scan_arg_t * arg)
135{
136 ip_neighbor_scan_config_t *cfg = &ip_neighbor_scan_conf;
137
138 cfg->mode = arg->mode;
139
140 if (arg->mode)
141 {
142 cfg->scan_interval = arg->scan_interval ?
John Lo3c22aff2018-06-07 12:00:43 -0400143 arg->scan_interval * 60.0 : IP_NEIGHBOR_DEF_SCAN_INTERVAL;
John Lo7f358b32018-04-28 01:19:24 -0400144 cfg->max_proc_time = arg->max_proc_time ?
145 arg->max_proc_time * 1e-6 : IP_NEIGHBOR_DEF_MAX_PROC_TIME;
146 cfg->scan_int_delay = arg->scan_int_delay ?
147 arg->scan_int_delay * 1e-3 : IP_NEIGHBOR_DEF_SCAN_INT_DELAY;
148 cfg->stale_threshold = arg->stale_threshold ?
John Lo3c22aff2018-06-07 12:00:43 -0400149 arg->stale_threshold * 60.0 : cfg->scan_interval * 4;
John Lo7f358b32018-04-28 01:19:24 -0400150 cfg->max_update = arg->max_update ?
151 cfg->max_update : IP_NEIGHBOR_DEF_MAX_UPDATE;
152 }
153 else
154 cfg->scan_interval = IP_NEIGHBOR_DEF_SCAN_INTERVAL;
155}
156
157static_always_inline u32
158ip_neighbor_scan (vlib_main_t * vm, f64 start_time, u32 start_idx,
159 u8 is_ip6, u8 delete_stale, u8 * update_count)
160{
161 vnet_main_t *vnm = vnet_get_main ();
162 ip_neighbor_scan_config_t *cfg = &ip_neighbor_scan_conf;
163 ethernet_arp_ip4_entry_t *np4 = ip4_neighbors_pool ();
164 ip6_neighbor_t *np6 = ip6_neighbors_pool ();
165 ethernet_arp_ip4_entry_t *n4;
166 ip6_neighbor_t *n6;
167 u32 curr_idx = start_idx;
168 u32 loop_count = 0;
169 f64 delta, update_time;
170
171 if (!is_ip6)
172 {
173 if (pool_is_free_index (np4, start_idx))
174 curr_idx = pool_next_index (np4, start_idx);
175 }
176 else
177 {
178 if (pool_is_free_index (np6, start_idx))
179 curr_idx = pool_next_index (np6, start_idx);
180 }
181
182 while (curr_idx != ~0)
183 {
184 /* allow no more than 10 neighbor updates or 20 usec of scan */
185 if ((update_count[0] >= cfg->max_update) ||
186 (((loop_count % 100) == 0) &&
187 ((vlib_time_now (vm) - start_time) > cfg->max_proc_time)))
188 break;
189
190 if (!is_ip6)
191 {
192 n4 = pool_elt_at_index (np4, curr_idx);
Neale Ranns37029302018-08-10 05:30:06 -0700193 if (n4->flags & IP_NEIGHBOR_FLAG_STATIC)
John Lo7f358b32018-04-28 01:19:24 -0400194 goto next_neighbor;
195 update_time = n4->time_last_updated;
196 }
197 else
198 {
199 n6 = pool_elt_at_index (np6, curr_idx);
Neale Ranns37029302018-08-10 05:30:06 -0700200 if (n6->flags & IP_NEIGHBOR_FLAG_STATIC)
John Lo7f358b32018-04-28 01:19:24 -0400201 goto next_neighbor;
202 update_time = n6->time_last_updated;
203 }
204
205 delta = start_time - update_time;
206 if (delete_stale && (delta >= cfg->stale_threshold))
207 {
208 update_count[0]++;
209 /* delete stale neighbor */
210 if (!is_ip6)
211 {
Neale Ranns37029302018-08-10 05:30:06 -0700212 ethernet_arp_ip4_over_ethernet_address_t delme = {
213 .ip4.as_u32 = n4->ip4_address.as_u32,
214 .mac = n4->mac,
215 };
216
John Lo7f358b32018-04-28 01:19:24 -0400217 vnet_arp_unset_ip4_over_ethernet (vnm, n4->sw_if_index, &delme);
218 }
219 else
220 {
221 vnet_unset_ip6_ethernet_neighbor
Neale Ranns0bdd3192018-09-07 11:04:52 -0700222 (vm, n6->key.sw_if_index, &n6->key.ip6_address);
John Lo7f358b32018-04-28 01:19:24 -0400223 }
224 }
225 else if (delta >= cfg->scan_interval)
226 {
227 update_count[0]++;
228 /* probe neighbor */
229 if (!is_ip6)
John Lo86376342018-06-11 20:14:49 -0400230 ip4_probe_neighbor (vm, &n4->ip4_address, n4->sw_if_index, 1);
John Lo7f358b32018-04-28 01:19:24 -0400231 else
232 ip6_probe_neighbor (vm, &n6->key.ip6_address,
John Lo86376342018-06-11 20:14:49 -0400233 n6->key.sw_if_index, 1);
John Lo7f358b32018-04-28 01:19:24 -0400234 }
235
236 next_neighbor:
237 loop_count++;
238
239 if (!is_ip6)
240 curr_idx = pool_next_index (np4, curr_idx);
241 else
242 curr_idx = pool_next_index (np6, curr_idx);
243 }
244
245 return curr_idx;
246}
247
248static uword
249neighbor_scan_process (vlib_main_t * vm,
250 vlib_node_runtime_t * rt, vlib_frame_t * f)
251{
252 ip_neighbor_scan_config_t *cfg = &ip_neighbor_scan_conf;
253 f64 timeout = IP_NEIGHBOR_DEF_SCAN_INTERVAL;
254 f64 start, next_scan = CLIB_TIME_MAX;
255 u32 ip4_nidx = 0; /* ip4 neighbor pool index */
256 u32 ip6_nidx = 0; /* ip6 neighbor pool index */
257 uword *event_data = 0;
258 u8 purge4 = 0, purge6 = 0; /* flags to purge stale entry during scan */
259 u8 update;
260
261 cfg->mode = IP_SCAN_DISABLED;
262 cfg->scan_interval = IP_NEIGHBOR_DEF_SCAN_INTERVAL;
263 cfg->scan_int_delay = IP_NEIGHBOR_DEF_SCAN_INTERVAL;
264
265 while (1)
266 {
267 vlib_process_wait_for_event_or_clock (vm, timeout);
268 vlib_process_get_events (vm, &event_data);
269 vec_reset_length (event_data);
270
271 start = vlib_time_now (vm);
272 update = 0;
273
274 if ((ip4_nidx == 0) && (ip6_nidx == 0)) /* starting a fresh scan */
275 next_scan = start + cfg->scan_interval;
276
277 if ((cfg->mode & IP_SCAN_V4_NEIGHBORS) == 0)
278 ip4_nidx = ~0; /* disable ip4 neighbor scan */
279
280 if ((cfg->mode & IP_SCAN_V6_NEIGHBORS) == 0)
281 ip6_nidx = ~0; /* disable ip6 neighbor scan */
282
283 if (ip4_nidx != ~0) /* scan ip4 neighbors */
284 ip4_nidx = ip_neighbor_scan (vm, start, ip4_nidx, /* ip4 */ 0,
285 purge4, &update);
286
287 if (ip6_nidx != ~0) /* scan ip6 neighbors */
288 ip6_nidx = ip_neighbor_scan (vm, start, ip6_nidx, /* ip6 */ 1,
289 purge6, &update);
290
291 if ((ip4_nidx == ~0) && (ip6_nidx == ~0))
292 { /* scan complete */
293 timeout = next_scan - vlib_time_now (vm);
294 ip4_nidx = ip6_nidx = 0;
295 purge4 = cfg->mode & IP_SCAN_V4_NEIGHBORS;
296 purge6 = cfg->mode & IP_SCAN_V6_NEIGHBORS;
297 }
298 else /* scan incomplete */
299 timeout = cfg->scan_int_delay;
300
301 if (timeout > cfg->scan_interval)
302 timeout = cfg->scan_interval;
303 else if (timeout < cfg->scan_int_delay)
304 timeout = cfg->scan_int_delay;
305
306 }
307 return 0;
308}
309
310/* *INDENT-OFF* */
311VLIB_REGISTER_NODE (neighbor_scan_process_node,static) = {
312 .function = neighbor_scan_process,
313 .type = VLIB_NODE_TYPE_PROCESS,
314 .name = "ip-neighbor-scan-process",
315};
316/* *INDENT-ON* */
317
318static clib_error_t *
319ip_neighbor_scan_cli (vlib_main_t * vm, unformat_input_t * input,
320 vlib_cli_command_t * cmd)
321{
322 unformat_input_t _line_input, *line_input = &_line_input;
323 clib_error_t *error = 0;
324 u32 interval = 0, time = 0, update = 0, delay = 0, stale = 0;
325 ip_neighbor_scan_arg_t arg;
326
Dave Barachb7b92992018-10-17 10:38:51 -0400327 clib_memset (&arg, 0, sizeof (arg));
John Lo7f358b32018-04-28 01:19:24 -0400328 arg.mode = IP_SCAN_V46_NEIGHBORS;
329
330 /* Get a line of input. */
331 if (!unformat_user (input, unformat_line_input, line_input))
332 {
333 ip_neighbor_scan_enable_disable (&arg);
334 return error;
335 }
336
337 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
338 {
339 if (unformat (line_input, "ip4"))
340 arg.mode = IP_SCAN_V4_NEIGHBORS;
341
342 else if (unformat (line_input, "ip6"))
343 arg.mode = IP_SCAN_V6_NEIGHBORS;
344
345 else if (unformat (line_input, "both"))
346 arg.mode = IP_SCAN_V46_NEIGHBORS;
347
348 else if (unformat (line_input, "disable"))
349 arg.mode = IP_SCAN_DISABLED;
350
351 else if (unformat (line_input, "interval %d", &interval))
352 arg.scan_interval = interval;
353
354 else if (unformat (line_input, "max-time %d", &time))
355 arg.max_proc_time = time;
356
357 else if (unformat (line_input, "max-update %d", &update))
358 arg.max_update = update;
359
360 else if (unformat (line_input, "delay %d", &delay))
361 arg.scan_int_delay = delay;
362
363 else if (unformat (line_input, "stale %d", &stale))
364 arg.stale_threshold = stale;
365
366 else
367 {
368 error = clib_error_return (0, "unknown input '%U'",
369 format_unformat_error, line_input);
370 goto done;
371 }
372 }
373
374 if (interval > 255)
375 {
376 error = clib_error_return (0, "interval cannot exceed 255 minutes.");
377 goto done;
378 }
379 if (time > 255)
380 {
381 error = clib_error_return (0, "max-time cannot exceed 255 usec.");
382 goto done;
383 }
384 if (update > 255)
385 {
386 error = clib_error_return (0, "max-update cannot exceed 255.");
387 goto done;
388 }
389 if (delay > 255)
390 {
391 error = clib_error_return (0, "delay cannot exceed 255 msec.");
392 goto done;
393 }
394 if (stale > 255)
395 {
396 error = clib_error_return (0, "stale cannot exceed 255 minutes.");
397 goto done;
398 }
399
400 ip_neighbor_scan_enable_disable (&arg);
401
402done:
403 unformat_free (line_input);
404
405 return error;
406}
407
408/*?
409 * The '<em>ip scan-neighbor</em>' command can be used to enable and disable
410 * periodic IP neighbor scan and change various scan parameneters.
411 *
412 * @note The default parameters used for IP neighbor scan should work fine
413 * under normal conditions. They should not be changed from the default unless
414 * properly tested to work as desied.
415 *
416 * @cliexpar
417 * Example of enabling IP neighbor scan:
418 * @cliexcmd{ip neighbor-scan enable}
419?*/
420/* *INDENT-OFF* */
421VLIB_CLI_COMMAND (ip_scan_neighbor_command, static) = {
422 .path = "ip scan-neighbor",
423 .function = ip_neighbor_scan_cli,
424 .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>]",
425 .is_mp_safe = 1,
426};
427/* *INDENT-ON* */
428
429static u8 *
430format_ip_scan_mode (u8 * s, va_list * args)
431{
432 u8 mode = va_arg (*args, u32);
433 switch (mode)
434 {
435 case IP_SCAN_V4_NEIGHBORS:
436 return format (s, "IPv4");
437 case IP_SCAN_V6_NEIGHBORS:
438 return format (s, "IPv6");
439 case IP_SCAN_V46_NEIGHBORS:
440 return format (s, "IPv4 and IPv6");
441 }
442 return format (s, "unknown");
443}
444
445static clib_error_t *
446show_ip_neighbor_scan (vlib_main_t * vm, unformat_input_t * input,
447 vlib_cli_command_t * cmd)
448{
449 ip_neighbor_scan_config_t *cfg = &ip_neighbor_scan_conf;
450
451 if (cfg->mode == 0)
452 vlib_cli_output (vm,
453 "IP neighbor scan disabled - current time is %.4f sec",
454 vlib_time_now (vm));
455 else
456 vlib_cli_output (vm, "IP neighbor scan enabled for %U neighbors - "
457 "current time is %.4f sec\n "
458 "Full_scan_interval: %f min "
459 "Stale_purge_threshod: %f min\n "
460 "Max_process_time: %f usec Max_updates %d "
461 "Delay_to_resume_after_max_limit: %f msec",
462 format_ip_scan_mode, cfg->mode,
463 vlib_time_now (vm), cfg->scan_interval / 60.0,
464 cfg->stale_threshold / 60.0, cfg->max_proc_time / 1e-6,
465 cfg->max_update, cfg->scan_int_delay / 1e-3);
466 return 0;
467}
468
469/*?
470 * The '<em>show ip scan-neighbor</em>' command can be used to show the current
471 * periodic IP neighbor scan parameters
472 *
473 * @cliexpar
474 * Example of showing IP neighbor scan current parameters:
475 * @cliexcmd{show ip neighbor-scan}
476?*/
477/* *INDENT-OFF* */
478VLIB_CLI_COMMAND (show_ip_scan_neighbor_command, static) = {
479 .path = "show ip scan-neighbor",
480 .function = show_ip_neighbor_scan,
481 .short_help = "show ip scan-neighbor",
482 .is_mp_safe = 1,
483};
484/* *INDENT-ON* */
485
486/*
487 * fd.io coding-style-patch-verification: ON
488 *
489 * Local Variables:
490 * eval: (c-set-style "gnu")
491 * End:
492 */