ping: fixing wrong value when there are worker threads
[vpp.git] / src / vnet / ip / ping.c
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <stddef.h>
17 #include <vnet/ip/ping.h>
18 #include <vnet/fib/ip6_fib.h>
19 #include <vnet/fib/ip4_fib.h>
20 #include <vnet/fib/fib_entry.h>
21 #include <vlib/vlib.h>
22 #include <emmintrin.h>
23
24 /**
25  * @file
26  * @brief IPv4 and IPv6 ICMP Ping.
27  *
28  * This file contains code to suppport IPv4 or IPv6 ICMP ECHO_REQUEST to
29  * network hosts.
30  *
31  */
32
33
34 u8 *
35 format_icmp_echo_trace (u8 * s, va_list * va)
36 {
37   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
38   CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
39   icmp_echo_trace_t *t = va_arg (*va, icmp_echo_trace_t *);
40
41   s = format (s, "ICMP echo id %d seq %d%s",
42               clib_net_to_host_u16 (t->id),
43               clib_net_to_host_u16 (t->seq), t->bound ? "" : " (unknown)");
44
45   return s;
46 }
47
48 /*
49  * If we can find the ping run by an ICMP ID, then we send the signal
50  * to the CLI process referenced by that ping run, alongside with
51  * a freshly made copy of the packet.
52  * I opted for a packet copy to keep the main packet processing path
53  * the same as for all the other nodes.
54  *
55  */
56
57 static int
58 signal_ip46_icmp_reply_event (u8 event_type, vlib_buffer_t * b0)
59 {
60   ping_main_t *pm = &ping_main;
61   u16 net_icmp_id = 0;
62   u32 bi0_copy = 0;
63
64   switch (event_type)
65     {
66     case PING_RESPONSE_IP4:
67       {
68         icmp4_echo_request_header_t *h0 = vlib_buffer_get_current (b0);
69         net_icmp_id = h0->icmp_echo.id;
70       }
71       break;
72     case PING_RESPONSE_IP6:
73       {
74         icmp6_echo_request_header_t *h0 = vlib_buffer_get_current (b0);
75         net_icmp_id = h0->icmp_echo.id;
76       }
77       break;
78     default:
79       return 0;
80     }
81
82   uword *p = hash_get (pm->ping_run_by_icmp_id,
83                        clib_net_to_host_u16 (net_icmp_id));
84   if (!p)
85     return 0;
86
87   ping_run_t *pr = vec_elt_at_index (pm->ping_runs, p[0]);
88   vlib_main_t *vm = vlib_mains[pr->cli_thread_index];
89   if (vlib_buffer_alloc (vm, &bi0_copy, 1) == 1)
90     {
91       void *dst = vlib_buffer_get_current (vlib_get_buffer (vm,
92                                                             bi0_copy));
93       clib_memcpy (dst, vlib_buffer_get_current (b0), b0->current_length);
94     }
95   /* If buffer_alloc failed, bi0_copy == 0 - just signaling an event. */
96   f64 nowts = vlib_time_now (vm);
97   /* Pass the timestamp to the cli_process thanks to the vnet_buffer unused metadata field */
98   clib_memcpy (vnet_buffer
99                (vlib_get_buffer
100                 (vm, bi0_copy))->unused, &nowts, sizeof (nowts));
101   vlib_process_signal_event (vm, pr->cli_process_id, event_type, bi0_copy);
102   return 1;
103 }
104
105 /*
106  * Process ICMPv6 echo replies
107  */
108 static uword
109 ip6_icmp_echo_reply_node_fn (vlib_main_t * vm,
110                              vlib_node_runtime_t * node, vlib_frame_t * frame)
111 {
112   u32 n_left_from, *from;
113
114   from = vlib_frame_vector_args (frame);        /* array of buffer indices */
115   n_left_from = frame->n_vectors;       /* number of buffer indices */
116
117   while (n_left_from > 0)
118     {
119       u32 bi0;
120       vlib_buffer_t *b0;
121       u32 next0;
122
123       bi0 = from[0];
124       b0 = vlib_get_buffer (vm, bi0);
125
126       next0 = signal_ip46_icmp_reply_event (PING_RESPONSE_IP6, b0) ?
127         ICMP6_ECHO_REPLY_NEXT_DROP : ICMP6_ECHO_REPLY_NEXT_PUNT;
128
129       if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
130         {
131           icmp6_echo_request_header_t *h0 = vlib_buffer_get_current (b0);
132           icmp_echo_trace_t *tr = vlib_add_trace (vm, node, b0, sizeof (*tr));
133           tr->id = h0->icmp_echo.id;
134           tr->seq = h0->icmp_echo.seq;
135           tr->bound = (next0 == ICMP6_ECHO_REPLY_NEXT_DROP);
136         }
137
138       /* push this pkt to the next graph node */
139       vlib_set_next_frame_buffer (vm, node, next0, bi0);
140
141       from += 1;
142       n_left_from -= 1;
143     }
144
145   return frame->n_vectors;
146 }
147
148 /* *INDENT-OFF* */
149 VLIB_REGISTER_NODE (ip6_icmp_echo_reply_node, static) =
150 {
151   .function = ip6_icmp_echo_reply_node_fn,
152   .name = "ip6-icmp-echo-reply",
153   .vector_size = sizeof (u32),
154   .format_trace = format_icmp_echo_trace,
155   .n_next_nodes = ICMP6_ECHO_REPLY_N_NEXT,
156   .next_nodes = {
157     [ICMP6_ECHO_REPLY_NEXT_DROP] = "error-drop",
158     [ICMP6_ECHO_REPLY_NEXT_PUNT] = "error-punt",
159   },
160 };
161 /* *INDENT-ON* */
162
163 /*
164  * Process ICMPv4 echo replies
165  */
166 static uword
167 ip4_icmp_echo_reply_node_fn (vlib_main_t * vm,
168                              vlib_node_runtime_t * node, vlib_frame_t * frame)
169 {
170   u32 n_left_from, *from;
171
172   from = vlib_frame_vector_args (frame);        /* array of buffer indices */
173   n_left_from = frame->n_vectors;       /* number of buffer indices */
174
175   while (n_left_from > 0)
176     {
177       u32 bi0;
178       vlib_buffer_t *b0;
179       u32 next0;
180
181       bi0 = from[0];
182       b0 = vlib_get_buffer (vm, bi0);
183
184       next0 = signal_ip46_icmp_reply_event (PING_RESPONSE_IP4, b0) ?
185         ICMP4_ECHO_REPLY_NEXT_DROP : ICMP4_ECHO_REPLY_NEXT_PUNT;
186
187       if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
188         {
189           icmp4_echo_request_header_t *h0 = vlib_buffer_get_current (b0);
190           icmp_echo_trace_t *tr = vlib_add_trace (vm, node, b0, sizeof (*tr));
191           tr->id = h0->icmp_echo.id;
192           tr->seq = h0->icmp_echo.seq;
193           tr->bound = (next0 == ICMP4_ECHO_REPLY_NEXT_DROP);
194         }
195
196       /* push this pkt to the next graph node */
197       vlib_set_next_frame_buffer (vm, node, next0, bi0);
198
199       from += 1;
200       n_left_from -= 1;
201     }
202
203   return frame->n_vectors;
204 }
205
206 /* *INDENT-OFF* */
207 VLIB_REGISTER_NODE (ip4_icmp_echo_reply_node, static) =
208 {
209   .function = ip4_icmp_echo_reply_node_fn,
210   .name = "ip4-icmp-echo-reply",
211   .vector_size = sizeof (u32),
212   .format_trace = format_icmp_echo_trace,
213   .n_next_nodes = ICMP4_ECHO_REPLY_N_NEXT,
214   .next_nodes = {
215     [ICMP4_ECHO_REPLY_NEXT_DROP] = "error-drop",
216     [ICMP4_ECHO_REPLY_NEXT_PUNT] = "error-punt",
217   },
218 };
219 /* *INDENT-ON* */
220
221 char *ip6_lookup_next_nodes[] = IP6_LOOKUP_NEXT_NODES;
222 char *ip4_lookup_next_nodes[] = IP4_LOOKUP_NEXT_NODES;
223
224 /* Fill in the ICMP ECHO structure, return the safety-checked and possibly shrunk data_len */
225 static u16
226 init_icmp46_echo_request (icmp46_echo_request_t * icmp46_echo,
227                           u16 seq_host, u16 id_host, u16 data_len)
228 {
229   int i;
230   icmp46_echo->seq = clib_host_to_net_u16 (seq_host);
231   icmp46_echo->id = clib_host_to_net_u16 (id_host);
232
233   if (data_len > PING_MAXIMUM_DATA_SIZE)
234     data_len = PING_MAXIMUM_DATA_SIZE;
235   for (i = 0; i < data_len; i++)
236     icmp46_echo->data[i] = i % 256;
237   return data_len;
238 }
239
240 static send_ip46_ping_result_t
241 send_ip6_ping (vlib_main_t * vm, ip6_main_t * im,
242                u32 table_id, ip6_address_t * pa6,
243                u32 sw_if_index, u16 seq_host, u16 id_host, u16 data_len,
244                u32 burst, u8 verbose)
245 {
246   icmp6_echo_request_header_t *h0;
247   u32 bi0 = 0;
248   int bogus_length = 0;
249   vlib_buffer_t *p0;
250   vlib_frame_t *f;
251   u32 *to_next;
252   vlib_buffer_free_list_t *fl;
253
254   if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
255     return SEND_PING_ALLOC_FAIL;
256
257   p0 = vlib_get_buffer (vm, bi0);
258   fl = vlib_buffer_get_free_list (vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
259   vlib_buffer_init_for_free_list (p0, fl);
260   VLIB_BUFFER_TRACE_TRAJECTORY_INIT (p0);
261
262   /*
263    * if the user did not provide a source interface, use the any interface
264    * that the destination resolves via.
265    */
266   if (~0 == sw_if_index)
267     {
268       fib_node_index_t fib_entry_index;
269       u32 fib_index;
270
271       fib_index = ip6_fib_index_from_table_id (table_id);
272
273       if (~0 == fib_index)
274         {
275           vlib_buffer_free (vm, &bi0, 1);
276           return SEND_PING_NO_TABLE;
277         }
278
279       fib_entry_index = ip6_fib_table_lookup (fib_index, pa6, 128);
280       sw_if_index = fib_entry_get_resolving_interface (fib_entry_index);
281       /*
282        * Set the TX interface to force ip-lookup to use its table ID
283        */
284       vnet_buffer (p0)->sw_if_index[VLIB_TX] = fib_index;
285     }
286   else
287     {
288       /*
289        * force an IP lookup in the table bound to the user's chosen
290        * source interface.
291        */
292       vnet_buffer (p0)->sw_if_index[VLIB_TX] =
293         ip6_fib_table_get_index_for_sw_if_index (sw_if_index);
294     }
295
296   if (~0 == sw_if_index)
297     {
298       vlib_buffer_free (vm, &bi0, 1);
299       return SEND_PING_NO_INTERFACE;
300     }
301
302   vnet_buffer (p0)->sw_if_index[VLIB_RX] = sw_if_index;
303
304   h0 = vlib_buffer_get_current (p0);
305
306   /* Fill in ip6 header fields */
307   h0->ip6.ip_version_traffic_class_and_flow_label =
308     clib_host_to_net_u32 (0x6 << 28);
309   h0->ip6.payload_length = 0;   /* Set below */
310   h0->ip6.protocol = IP_PROTOCOL_ICMP6;
311   h0->ip6.hop_limit = 255;
312   h0->ip6.dst_address = *pa6;
313   h0->ip6.src_address = *pa6;
314
315   /* Fill in the correct source now */
316   ip6_address_t *a = ip6_interface_first_address (im, sw_if_index);
317   if (!a)
318     {
319       vlib_buffer_free (vm, &bi0, 1);
320       return SEND_PING_NO_SRC_ADDRESS;
321     }
322   h0->ip6.src_address = a[0];
323
324   /* Fill in icmp fields */
325   h0->icmp.type = ICMP6_echo_request;
326   h0->icmp.code = 0;
327   h0->icmp.checksum = 0;
328
329   data_len =
330     init_icmp46_echo_request (&h0->icmp_echo, seq_host, id_host, data_len);
331   h0->icmp_echo.time_sent = vlib_time_now (vm);
332
333   /* Fix up the lengths */
334   h0->ip6.payload_length =
335     clib_host_to_net_u16 (data_len + sizeof (icmp46_header_t));
336
337   p0->current_length = clib_net_to_host_u16 (h0->ip6.payload_length) +
338     STRUCT_OFFSET_OF (icmp6_echo_request_header_t, icmp);
339
340   /* Calculate the ICMP checksum */
341   h0->icmp.checksum = 0;
342   h0->icmp.checksum =
343     ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h0->ip6, &bogus_length);
344
345   /* Enqueue the packet right now */
346   f = vlib_get_frame_to_node (vm, ip6_lookup_node.index);
347   to_next = vlib_frame_vector_args (f);
348   to_next[0] = bi0;
349
350   ASSERT (burst <= VLIB_FRAME_SIZE);
351   f->n_vectors = burst;
352   while (--burst)
353     {
354       vlib_buffer_t *c0 = vlib_buffer_copy (vm, p0);
355       to_next++;
356       to_next[0] = vlib_get_buffer_index (vm, c0);
357     }
358   vlib_put_frame_to_node (vm, ip6_lookup_node.index, f);
359
360   return SEND_PING_OK;
361 }
362
363 static send_ip46_ping_result_t
364 send_ip4_ping (vlib_main_t * vm,
365                ip4_main_t * im,
366                u32 table_id,
367                ip4_address_t * pa4,
368                u32 sw_if_index,
369                u16 seq_host, u16 id_host, u16 data_len, u32 burst, u8 verbose)
370 {
371   icmp4_echo_request_header_t *h0;
372   u32 bi0 = 0;
373   ip_lookup_main_t *lm = &im->lookup_main;
374   vlib_buffer_t *p0;
375   vlib_frame_t *f;
376   u32 *to_next;
377   u32 if_add_index0;
378   vlib_buffer_free_list_t *fl;
379
380   if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
381     return SEND_PING_ALLOC_FAIL;
382
383   p0 = vlib_get_buffer (vm, bi0);
384   fl = vlib_buffer_get_free_list (vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
385   vlib_buffer_init_for_free_list (p0, fl);
386   VLIB_BUFFER_TRACE_TRAJECTORY_INIT (p0);
387
388   /*
389    * if the user did not provide a source interface, use the any interface
390    * that the destination resolves via.
391    */
392   if (~0 == sw_if_index)
393     {
394       fib_node_index_t fib_entry_index;
395       u32 fib_index;
396
397       fib_index = ip4_fib_index_from_table_id (table_id);
398
399       if (~0 == fib_index)
400         {
401           vlib_buffer_free (vm, &bi0, 1);
402           return SEND_PING_NO_TABLE;
403         }
404
405       fib_entry_index =
406         ip4_fib_table_lookup (ip4_fib_get (fib_index), pa4, 32);
407       sw_if_index = fib_entry_get_resolving_interface (fib_entry_index);
408       /*
409        * Set the TX interface to force ip-lookup to use the user's table ID
410        */
411       vnet_buffer (p0)->sw_if_index[VLIB_TX] = fib_index;
412     }
413   else
414     {
415       /*
416        * force an IP lookup in the table bound to the user's chosen
417        * source interface.
418        */
419       vnet_buffer (p0)->sw_if_index[VLIB_TX] =
420         ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
421     }
422
423   if (~0 == sw_if_index)
424     {
425       vlib_buffer_free (vm, &bi0, 1);
426       return SEND_PING_NO_INTERFACE;
427     }
428
429   vnet_buffer (p0)->sw_if_index[VLIB_RX] = sw_if_index;
430
431   h0 = vlib_buffer_get_current (p0);
432
433   /* Fill in ip4 header fields */
434   h0->ip4.checksum = 0;
435   h0->ip4.ip_version_and_header_length = 0x45;
436   h0->ip4.tos = 0;
437   h0->ip4.length = 0;           /* Set below */
438   h0->ip4.fragment_id = 0;
439   h0->ip4.flags_and_fragment_offset = 0;
440   h0->ip4.ttl = 0xff;
441   h0->ip4.protocol = IP_PROTOCOL_ICMP;
442   h0->ip4.dst_address = *pa4;
443   h0->ip4.src_address = *pa4;
444
445   /* Fill in the correct source now */
446   if_add_index0 = lm->if_address_pool_index_by_sw_if_index[sw_if_index];
447   if (PREDICT_TRUE (if_add_index0 != ~0))
448     {
449       ip_interface_address_t *if_add =
450         pool_elt_at_index (lm->if_address_pool, if_add_index0);
451       ip4_address_t *if_ip = ip_interface_address_get_address (lm, if_add);
452       h0->ip4.src_address = *if_ip;
453       if (verbose)
454         {
455           vlib_cli_output (vm, "Source address: %U",
456                            format_ip4_address, &h0->ip4.src_address);
457         }
458     }
459
460   /* Fill in icmp fields */
461   h0->icmp.type = ICMP4_echo_request;
462   h0->icmp.code = 0;
463   h0->icmp.checksum = 0;
464
465   data_len =
466     init_icmp46_echo_request (&h0->icmp_echo, seq_host, id_host, data_len);
467   h0->icmp_echo.time_sent = vlib_time_now (vm);
468
469   /* Fix up the lengths */
470   h0->ip4.length =
471     clib_host_to_net_u16 (data_len + sizeof (icmp46_header_t) +
472                           sizeof (ip4_header_t));
473
474   p0->current_length = clib_net_to_host_u16 (h0->ip4.length);
475
476   /* Calculate the IP and ICMP checksums */
477   h0->ip4.checksum = ip4_header_checksum (&(h0->ip4));
478   h0->icmp.checksum =
479     ~ip_csum_fold (ip_incremental_checksum (0, &(h0->icmp),
480                                             p0->current_length -
481                                             sizeof (ip4_header_t)));
482
483   /* Enqueue the packet right now */
484   f = vlib_get_frame_to_node (vm, ip4_lookup_node.index);
485   to_next = vlib_frame_vector_args (f);
486   to_next[0] = bi0;
487
488   ASSERT (burst <= VLIB_FRAME_SIZE);
489   f->n_vectors = burst;
490   while (--burst)
491     {
492       vlib_buffer_t *c0 = vlib_buffer_copy (vm, p0);
493       to_next++;
494       to_next[0] = vlib_get_buffer_index (vm, c0);
495     }
496   vlib_put_frame_to_node (vm, ip4_lookup_node.index, f);
497
498   return SEND_PING_OK;
499 }
500
501
502 static void
503 print_ip6_icmp_reply (vlib_main_t * vm, u32 bi0)
504 {
505   vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
506   icmp6_echo_request_header_t *h0 = vlib_buffer_get_current (b0);
507   f64 rtt = 0;
508   clib_memcpy (&rtt, vnet_buffer (b0)->unused, sizeof (rtt));
509   rtt -= h0->icmp_echo.time_sent;
510   vlib_cli_output (vm,
511                    "%d bytes from %U: icmp_seq=%d ttl=%d time=%.4f ms",
512                    clib_host_to_net_u16 (h0->ip6.payload_length),
513                    format_ip6_address,
514                    &h0->ip6.src_address,
515                    clib_host_to_net_u16 (h0->icmp_echo.seq),
516                    h0->ip6.hop_limit, rtt * 1000.0);
517 }
518
519 static void
520 print_ip4_icmp_reply (vlib_main_t * vm, u32 bi0)
521 {
522   vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
523   icmp4_echo_request_header_t *h0 = vlib_buffer_get_current (b0);
524   f64 rtt = 0;
525   clib_memcpy (&rtt, vnet_buffer (b0)->unused, sizeof (rtt));
526   rtt -= h0->icmp_echo.time_sent;
527   u32 rcvd_icmp_len =
528     clib_host_to_net_u16 (h0->ip4.length) -
529     (4 * (0xF & h0->ip4.ip_version_and_header_length));
530
531   vlib_cli_output (vm,
532                    "%d bytes from %U: icmp_seq=%d ttl=%d time=%.4f ms",
533                    rcvd_icmp_len,
534                    format_ip4_address,
535                    &h0->ip4.src_address,
536                    clib_host_to_net_u16 (h0->icmp_echo.seq),
537                    h0->ip4.ttl, rtt * 1000.0);
538 }
539
540
541 /*
542  * Perform the ping run with the given parameters in the current CLI process.
543  * Depending on whether pa4 or pa6 is set, runs IPv4 or IPv6 ping.
544  * The amusing side effect is of course if both are set, then both pings are sent.
545  * This behavior can be used to ping a dualstack host over IPv4 and IPv6 at once.
546  */
547
548 static void
549 run_ping_ip46_address (vlib_main_t * vm, u32 table_id, ip4_address_t * pa4,
550                        ip6_address_t * pa6, u32 sw_if_index,
551                        f64 ping_interval, u32 ping_repeat, u32 data_len,
552                        u32 ping_burst, u32 verbose)
553 {
554   int i;
555   ping_main_t *pm = &ping_main;
556   uword curr_proc = vlib_current_process (vm);
557   u32 n_replies = 0;
558   u32 n_requests = 0;
559   ping_run_t *pr = 0;
560   u32 ping_run_index = 0;
561   u16 icmp_id;
562
563   static u32 rand_seed = 0;
564
565   if (PREDICT_FALSE (!rand_seed))
566     rand_seed = random_default_seed ();
567
568   icmp_id = random_u32 (&rand_seed) & 0xffff;
569
570   while (hash_get (pm->ping_run_by_icmp_id, icmp_id))
571     {
572       vlib_cli_output (vm, "ICMP ID collision at %d, incrementing", icmp_id);
573       icmp_id++;
574     }
575   pool_get (pm->ping_runs, pr);
576   ping_run_index = pr - pm->ping_runs;
577   pr->cli_process_id = curr_proc;
578   pr->cli_thread_index = vlib_get_thread_index ();
579   pr->icmp_id = icmp_id;
580   hash_set (pm->ping_run_by_icmp_id, icmp_id, ping_run_index);
581   for (i = 1; i <= ping_repeat; i++)
582     {
583       f64 sleep_interval;
584       f64 time_ping_sent = vlib_time_now (vm);
585       /* Reset pr: running ping in other process could have changed pm->ping_runs */
586       pr = vec_elt_at_index (pm->ping_runs, ping_run_index);
587       pr->curr_seq = i;
588       if (pa6 &&
589           (SEND_PING_OK ==
590            send_ip6_ping (vm, ping_main.ip6_main, table_id, pa6, sw_if_index,
591                           i, icmp_id, data_len, ping_burst, verbose)))
592         {
593           n_requests += ping_burst;
594         }
595       if (pa4 &&
596           (SEND_PING_OK ==
597            send_ip4_ping (vm, ping_main.ip4_main, table_id, pa4, sw_if_index,
598                           i, icmp_id, data_len, ping_burst, verbose)))
599         {
600           n_requests += ping_burst;
601         }
602       while ((i <= ping_repeat)
603              &&
604              ((sleep_interval =
605                time_ping_sent + ping_interval - vlib_time_now (vm)) > 0.0))
606         {
607           uword event_type, *event_data = 0;
608           vlib_process_wait_for_event_or_clock (vm, sleep_interval);
609           event_type = vlib_process_get_events (vm, &event_data);
610           switch (event_type)
611             {
612             case ~0:            /* no events => timeout */
613               break;
614             case PING_RESPONSE_IP6:
615               {
616                 int i;
617                 for (i = 0; i < vec_len (event_data); i++)
618                   {
619                     u32 bi0 = event_data[i];
620                     print_ip6_icmp_reply (vm, bi0);
621                     n_replies++;
622                     if (0 != bi0)
623                       {
624                         vlib_buffer_free (vm, &bi0, 1);
625                       }
626                   }
627               }
628               break;
629             case PING_RESPONSE_IP4:
630               {
631                 int i;
632                 for (i = 0; i < vec_len (event_data); i++)
633                   {
634                     u32 bi0 = event_data[i];
635                     print_ip4_icmp_reply (vm, bi0);
636                     n_replies++;
637                     if (0 != bi0)
638                       {
639                         vlib_buffer_free (vm, &bi0, 1);
640                       }
641                   }
642               }
643               break;
644             default:
645               /* someone pressed a key, abort */
646               vlib_cli_output (vm, "Aborted due to a keypress.");
647               i = 1 + ping_repeat;
648               break;
649             }
650         }
651     }
652   vlib_cli_output (vm, "\n");
653   {
654     float loss =
655       (0 ==
656        n_requests) ? 0 : 100.0 * ((float) n_requests -
657                                   (float) n_replies) / (float) n_requests;
658     vlib_cli_output (vm,
659                      "Statistics: %u sent, %u received, %f%% packet loss\n",
660                      n_requests, n_replies, loss);
661     /* Reset pr: running ping in other process could have changed pm->ping_runs */
662     pr = vec_elt_at_index (pm->ping_runs, ping_run_index);
663     hash_unset (pm->ping_run_by_icmp_id, icmp_id);
664     pool_put (pm->ping_runs, pr);
665   }
666 }
667
668
669
670
671
672 static clib_error_t *
673 ping_ip_address (vlib_main_t * vm,
674                  unformat_input_t * input, vlib_cli_command_t * cmd)
675 {
676   ip4_address_t a4;
677   ip6_address_t a6;
678   clib_error_t *error = 0;
679   u32 ping_repeat = 5;
680   u32 ping_burst = 1;
681   u8 ping_ip4, ping_ip6;
682   vnet_main_t *vnm = vnet_get_main ();
683   u32 data_len = PING_DEFAULT_DATA_LEN;
684   u32 verbose = 0;
685   f64 ping_interval = PING_DEFAULT_INTERVAL;
686   u32 sw_if_index, table_id;
687
688   table_id = 0;
689   ping_ip4 = ping_ip6 = 0;
690   sw_if_index = ~0;
691
692   if (unformat (input, "%U", unformat_ip4_address, &a4))
693     {
694       ping_ip4 = 1;
695     }
696   else if (unformat (input, "%U", unformat_ip6_address, &a6))
697     {
698       ping_ip6 = 1;
699     }
700   else if (unformat (input, "ipv4"))
701     {
702       if (unformat (input, "%U", unformat_ip4_address, &a4))
703         {
704           ping_ip4 = 1;
705         }
706       else
707         {
708           error =
709             clib_error_return (0,
710                                "expecting IPv4 address but got `%U'",
711                                format_unformat_error, input);
712         }
713     }
714   else if (unformat (input, "ipv6"))
715     {
716       if (unformat (input, "%U", unformat_ip6_address, &a6))
717         {
718           ping_ip6 = 1;
719         }
720       else
721         {
722           error =
723             clib_error_return (0,
724                                "expecting IPv6 address but got `%U'",
725                                format_unformat_error, input);
726         }
727     }
728   else
729     {
730       error =
731         clib_error_return (0,
732                            "expecting IP4/IP6 address `%U'. Usage: ping <addr> [source <intf>] [size <datasz>] [repeat <count>] [verbose]",
733                            format_unformat_error, input);
734       goto done;
735     }
736
737   /* allow for the second AF in the same ping */
738   if (!ping_ip4 && (unformat (input, "ipv4")))
739     {
740       if (unformat (input, "%U", unformat_ip4_address, &a4))
741         {
742           ping_ip4 = 1;
743         }
744     }
745   else if (!ping_ip6 && (unformat (input, "ipv6")))
746     {
747       if (unformat (input, "%U", unformat_ip6_address, &a6))
748         {
749           ping_ip6 = 1;
750         }
751     }
752
753   /* parse the rest of the parameters  in a cycle */
754   while (!unformat_eof (input, NULL))
755     {
756       if (unformat (input, "source"))
757         {
758           if (!unformat_user
759               (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
760             {
761               error =
762                 clib_error_return (0,
763                                    "unknown interface `%U'",
764                                    format_unformat_error, input);
765               goto done;
766             }
767         }
768       else if (unformat (input, "size"))
769         {
770           if (!unformat (input, "%u", &data_len))
771             {
772               error =
773                 clib_error_return (0,
774                                    "expecting size but got `%U'",
775                                    format_unformat_error, input);
776               goto done;
777             }
778           if (data_len > PING_MAXIMUM_DATA_SIZE)
779             {
780               error =
781                 clib_error_return (0,
782                                    "%d is bigger than maximum allowed payload size %d",
783                                    data_len, PING_MAXIMUM_DATA_SIZE);
784               goto done;
785             }
786         }
787       else if (unformat (input, "table-id"))
788         {
789           if (!unformat (input, "%u", &table_id))
790             {
791               error =
792                 clib_error_return (0,
793                                    "expecting table-id but got `%U'",
794                                    format_unformat_error, input);
795               goto done;
796             }
797         }
798       else if (unformat (input, "interval"))
799         {
800           if (!unformat (input, "%f", &ping_interval))
801             {
802               error =
803                 clib_error_return (0,
804                                    "expecting interval (floating point number) got `%U'",
805                                    format_unformat_error, input);
806               goto done;
807             }
808         }
809       else if (unformat (input, "repeat"))
810         {
811           if (!unformat (input, "%u", &ping_repeat))
812             {
813               error =
814                 clib_error_return (0,
815                                    "expecting repeat count but got `%U'",
816                                    format_unformat_error, input);
817               goto done;
818             }
819         }
820       else if (unformat (input, "burst"))
821         {
822           if (!unformat (input, "%u", &ping_burst))
823             {
824               error =
825                 clib_error_return (0,
826                                    "expecting burst count but got `%U'",
827                                    format_unformat_error, input);
828               goto done;
829             }
830         }
831       else if (unformat (input, "verbose"))
832         {
833           verbose = 1;
834         }
835       else
836         {
837           error = clib_error_return (0, "unknown input `%U'",
838                                      format_unformat_error, input);
839           goto done;
840         }
841     }
842
843   if (ping_burst < 1 || ping_burst > VLIB_FRAME_SIZE)
844     return clib_error_return (0, "burst size must be between 1 and %u",
845                               VLIB_FRAME_SIZE);
846
847   run_ping_ip46_address (vm, table_id, ping_ip4 ? &a4 : NULL,
848                          ping_ip6 ? &a6 : NULL, sw_if_index, ping_interval,
849                          ping_repeat, data_len, ping_burst, verbose);
850 done:
851   return error;
852 }
853
854 /*?
855  * This command sends an ICMP ECHO_REQUEST to network hosts. The address
856  * can be an IPv4 or IPv6 address (or both at the same time).
857  *
858  * @cliexpar
859  * @parblock
860  * Example of how ping an IPv4 address:
861  * @cliexstart{ping 172.16.1.2 source GigabitEthernet2/0/0 repeat 2}
862  * 64 bytes from 172.16.1.2: icmp_seq=1 ttl=64 time=.1090 ms
863  * 64 bytes from 172.16.1.2: icmp_seq=2 ttl=64 time=.0914 ms
864  *
865  * Statistics: 2 sent, 2 received, 0% packet loss
866  * @cliexend
867  *
868  * Example of how ping both an IPv4 address and IPv6 address at the same time:
869  * @cliexstart{ping 172.16.1.2 ipv6 fe80::24a5:f6ff:fe9c:3a36 source GigabitEthernet2/0/0 repeat 2 verbose}
870  * Adjacency index: 10, sw_if_index: 1
871  * Adj: ip6-discover-neighbor
872  * Adj Interface: 0
873  * Forced set interface: 1
874  * Adjacency index: 0, sw_if_index: 4294967295
875  * Adj: ip4-miss
876  * Adj Interface: 0
877  * Forced set interface: 1
878  * Source address: 172.16.1.1
879  * 64 bytes from 172.16.1.2: icmp_seq=1 ttl=64 time=.1899 ms
880  * Adjacency index: 10, sw_if_index: 1
881  * Adj: ip6-discover-neighbor
882  * Adj Interface: 0
883  * Forced set interface: 1
884  * Adjacency index: 0, sw_if_index: 4294967295
885  * Adj: ip4-miss
886  * Adj Interface: 0
887  * Forced set interface: 1
888  * Source address: 172.16.1.1
889  * 64 bytes from 172.16.1.2: icmp_seq=2 ttl=64 time=.0910 ms
890  *
891  * Statistics: 4 sent, 2 received, 50% packet loss
892  * @cliexend
893  * @endparblock
894 ?*/
895 /* *INDENT-OFF* */
896 VLIB_CLI_COMMAND (ping_command, static) =
897 {
898   .path = "ping",
899   .function = ping_ip_address,
900   .short_help = "ping {<ip-addr> | ipv4 <ip4-addr> | ipv6 <ip6-addr>}"
901   " [ipv4 <ip4-addr> | ipv6 <ip6-addr>] [source <interface>]"
902   " [size <pktsize>] [interval <sec>] [repeat <cnt>] [table-id <id>]"
903   " [verbose]",
904   .is_mp_safe = 1,
905 };
906 /* *INDENT-ON* */
907
908 static clib_error_t *
909 ping_cli_init (vlib_main_t * vm)
910 {
911   ping_main_t *pm = &ping_main;
912   pm->ip6_main = &ip6_main;
913   pm->ip4_main = &ip4_main;
914   icmp6_register_type (vm, ICMP6_echo_reply, ip6_icmp_echo_reply_node.index);
915   ip4_icmp_register_type (vm, ICMP4_echo_reply,
916                           ip4_icmp_echo_reply_node.index);
917   return 0;
918 }
919
920 VLIB_INIT_FUNCTION (ping_cli_init);
921
922 /*
923  * fd.io coding-style-patch-verification: ON
924  *
925  * Local Variables:
926  * eval: (c-set-style "gnu")
927  * End:
928  */