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