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