08e770132f02719b8cf1c3901279b5eb9c0416fe
[vpp.git] / vnet / 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 <vnet/ip/ping.h>
17 #include <vnet/fib/ip6_fib.h>
18 #include <vnet/fib/ip4_fib.h>
19 #include <vnet/fib/fib_entry.h>
20
21 /**
22  * @file
23  * @brief IPv4 and IPv6 ICMP Ping.
24  *
25  * This file contains code to suppport IPv4 or IPv6 ICMP ECHO_REQUEST to
26  * network hosts.
27  *
28  */
29
30
31 u8 *
32 format_icmp_echo_trace (u8 * s, va_list * va)
33 {
34   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
35   CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
36   icmp_echo_trace_t *t = va_arg (*va, icmp_echo_trace_t *);
37
38   s = format (s, "ICMP echo id %d seq %d%s",
39               clib_net_to_host_u16(t->id),
40               clib_net_to_host_u16(t->seq),
41               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 /* get first interface address */
218 static ip6_address_t *
219 ip6_interface_first_address (ip6_main_t * im, u32 sw_if_index)
220 {
221   ip_lookup_main_t *lm = &im->lookup_main;
222   ip_interface_address_t *ia = 0;
223   ip6_address_t *result = 0;
224
225   foreach_ip_interface_address (lm, ia, sw_if_index,
226                                 1 /* honor unnumbered */ ,
227                                 (
228                                   {
229                                   ip6_address_t * a =
230                                   ip_interface_address_get_address (lm, ia);
231                                   result = a;
232                                   break;
233                                   }
234                                 ));
235   return result;
236 }
237
238 /* Fill in the ICMP ECHO structure, return the safety-checked and possibly shrunk data_len */
239 static u16
240 init_icmp46_echo_request (icmp46_echo_request_t * icmp46_echo,
241                           u16 seq_host, u16 id_host, u16 data_len)
242 {
243   int i;
244   icmp46_echo->seq = clib_host_to_net_u16 (seq_host);
245   icmp46_echo->id = clib_host_to_net_u16 (id_host);
246
247   for (i = 0; i < sizeof (icmp46_echo->data); i++)
248     {
249       icmp46_echo->data[i] = i % 256;
250     }
251
252   if (data_len > sizeof (icmp46_echo_request_t))
253     {
254       data_len = sizeof (icmp46_echo_request_t);
255     }
256   return data_len;
257 }
258
259 static send_ip46_ping_result_t
260 send_ip6_ping (vlib_main_t * vm, ip6_main_t * im,
261                u32 table_id, ip6_address_t * pa6,
262                u32 sw_if_index, u16 seq_host, u16 id_host, u16 data_len,
263                u8 verbose)
264 {
265   icmp6_echo_request_header_t *h0;
266   u32 bi0 = 0;
267   int bogus_length = 0;
268   vlib_buffer_t *p0;
269   vlib_frame_t *f;
270   u32 *to_next;
271
272   if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
273     return SEND_PING_ALLOC_FAIL;
274
275   p0 = vlib_get_buffer (vm, bi0);
276
277   /*
278    * if the user did not provide a source interface, use the any interface
279    * that the destination resolves via.
280    */
281   if (~0 == sw_if_index)
282     {
283       fib_node_index_t fib_entry_index;
284       u32 fib_index;
285
286       fib_index = ip6_fib_index_from_table_id(table_id);
287
288       if (~0 == fib_index)
289       {
290           vlib_buffer_free (vm, &bi0, 1);
291           return SEND_PING_NO_TABLE;
292       }
293
294       fib_entry_index = ip6_fib_table_lookup(fib_index, pa6, 128);
295       sw_if_index = fib_entry_get_resolving_interface(fib_entry_index);
296       /*
297        * Set the TX interface to force ip-lookup to use its table ID
298        */
299       vnet_buffer (p0)->sw_if_index[VLIB_TX] = fib_index;
300     }
301   else
302     {
303       /*
304        * force an IP lookup in the table bound to the user's chosen
305        * source interface.
306        */
307       vnet_buffer (p0)->sw_if_index[VLIB_TX] =
308           ip6_fib_table_get_index_for_sw_if_index(sw_if_index);
309     }
310
311   if (~0 == sw_if_index)
312     {
313       vlib_buffer_free (vm, &bi0, 1);
314       return SEND_PING_NO_INTERFACE;
315     }
316
317   vnet_buffer (p0)->sw_if_index[VLIB_RX] = sw_if_index;
318
319   h0 = vlib_buffer_get_current (p0);
320
321   /* Fill in ip6 header fields */
322   h0->ip6.ip_version_traffic_class_and_flow_label =
323     clib_host_to_net_u32 (0x6 << 28);
324   h0->ip6.payload_length = 0;   /* Set below */
325   h0->ip6.protocol = IP_PROTOCOL_ICMP6;
326   h0->ip6.hop_limit = 255;
327   h0->ip6.dst_address = *pa6;
328   h0->ip6.src_address = *pa6;
329
330   /* Fill in the correct source now */
331   ip6_address_t *a = ip6_interface_first_address (im, sw_if_index);
332   h0->ip6.src_address = a[0];
333
334   /* Fill in icmp fields */
335   h0->icmp.type = ICMP6_echo_request;
336   h0->icmp.code = 0;
337   h0->icmp.checksum = 0;
338
339   data_len =
340     init_icmp46_echo_request (&h0->icmp_echo, seq_host, id_host, data_len);
341   h0->icmp_echo.time_sent = vlib_time_now (vm);
342
343   /* Fix up the lengths */
344   h0->ip6.payload_length =
345     clib_host_to_net_u16 (data_len + sizeof (icmp46_header_t));
346
347   p0->current_length = clib_net_to_host_u16 (h0->ip6.payload_length) +
348     STRUCT_OFFSET_OF (icmp6_echo_request_header_t, icmp);
349
350   /* Calculate the ICMP checksum */
351   h0->icmp.checksum = 0;
352   h0->icmp.checksum =
353     ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h0->ip6, &bogus_length);
354
355   /* Enqueue the packet right now */
356   f = vlib_get_frame_to_node (vm, ip6_lookup_node.index);
357   to_next = vlib_frame_vector_args (f);
358   to_next[0] = bi0;
359   f->n_vectors = 1;
360   vlib_put_frame_to_node (vm, ip6_lookup_node.index, f);
361
362   return SEND_PING_OK;
363 }
364
365 static send_ip46_ping_result_t
366 send_ip4_ping (vlib_main_t * vm,
367                ip4_main_t * im,
368                u32 table_id,
369                ip4_address_t * pa4,
370                u32 sw_if_index,
371                u16 seq_host, u16 id_host, u16 data_len, u8 verbose)
372 {
373   icmp4_echo_request_header_t *h0;
374   u32 bi0 = 0;
375   ip_lookup_main_t *lm = &im->lookup_main;
376   vlib_buffer_t *p0;
377   vlib_frame_t *f;
378   u32 *to_next;
379   u32 if_add_index0;
380
381   if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
382     return SEND_PING_ALLOC_FAIL;
383
384   p0 = vlib_get_buffer (vm, bi0);
385
386   /*
387    * if the user did not provide a source interface, use the any interface
388    * that the destination resolves via.
389    */
390   if (~0 == sw_if_index)
391   {
392       fib_node_index_t fib_entry_index;
393       u32 fib_index;
394
395       fib_index = ip4_fib_index_from_table_id(table_id);
396
397       if (~0 == fib_index)
398       {
399           vlib_buffer_free (vm, &bi0, 1);
400           return SEND_PING_NO_TABLE;
401       }
402
403       fib_entry_index = ip4_fib_table_lookup(ip4_fib_get(fib_index), pa4, 32);
404       sw_if_index = fib_entry_get_resolving_interface(fib_entry_index);
405       /*
406        * Set the TX interface to force ip-lookup to use the user's table ID
407        */
408       vnet_buffer (p0)->sw_if_index[VLIB_TX] = fib_index;
409     }
410   else
411     {
412       /*
413        * force an IP lookup in the table bound to the user's chosen
414        * source interface.
415        */
416       vnet_buffer (p0)->sw_if_index[VLIB_TX] =
417           ip4_fib_table_get_index_for_sw_if_index(sw_if_index);
418     }
419
420   if (~0 == sw_if_index)
421     {
422       vlib_buffer_free (vm, &bi0, 1);
423       return SEND_PING_NO_INTERFACE;
424     }
425
426   vnet_buffer (p0)->sw_if_index[VLIB_RX] = sw_if_index;
427
428   h0 = vlib_buffer_get_current (p0);
429
430   /* Fill in ip4 header fields */
431   h0->ip4.checksum = 0;
432   h0->ip4.ip_version_and_header_length = 0x45;
433   h0->ip4.tos = 0;
434   h0->ip4.length = 0;           /* Set below */
435   h0->ip4.fragment_id = 0;
436   h0->ip4.flags_and_fragment_offset = 0;
437   h0->ip4.ttl = 0xff;
438   h0->ip4.protocol = IP_PROTOCOL_ICMP;
439   h0->ip4.dst_address = *pa4;
440   h0->ip4.src_address = *pa4;
441
442   /* Fill in the correct source now */
443   if_add_index0 = lm->if_address_pool_index_by_sw_if_index[sw_if_index];
444   if (PREDICT_TRUE (if_add_index0 != ~0))
445     {
446       ip_interface_address_t *if_add =
447         pool_elt_at_index (lm->if_address_pool, if_add_index0);
448       ip4_address_t *if_ip = ip_interface_address_get_address (lm, if_add);
449       h0->ip4.src_address = *if_ip;
450       if (verbose)
451         {
452           vlib_cli_output (vm, "Source address: %U",
453                            format_ip4_address, &h0->ip4.src_address);
454         }
455     }
456
457   /* Fill in icmp fields */
458   h0->icmp.type = ICMP4_echo_request;
459   h0->icmp.code = 0;
460   h0->icmp.checksum = 0;
461
462   data_len =
463     init_icmp46_echo_request (&h0->icmp_echo, seq_host, id_host, data_len);
464   h0->icmp_echo.time_sent = vlib_time_now (vm);
465
466   /* Fix up the lengths */
467   h0->ip4.length =
468     clib_host_to_net_u16 (data_len + sizeof (icmp46_header_t) +
469                           sizeof (ip4_header_t));
470
471   p0->current_length = clib_net_to_host_u16 (h0->ip4.length);
472
473   /* Calculate the IP and ICMP checksums */
474   h0->ip4.checksum = ip4_header_checksum (&(h0->ip4));
475   h0->icmp.checksum =
476     ~ip_csum_fold (ip_incremental_checksum (0, &(h0->icmp),
477                     p0->current_length - sizeof (ip4_header_t)));
478
479   /* Enqueue the packet right now */
480   f = vlib_get_frame_to_node (vm, ip4_lookup_node.index);
481   to_next = vlib_frame_vector_args (f);
482   to_next[0] = bi0;
483   f->n_vectors = 1;
484   vlib_put_frame_to_node (vm, ip4_lookup_node.index, f);
485
486   return SEND_PING_OK;
487 }
488
489
490 static void
491 print_ip6_icmp_reply (vlib_main_t * vm, u32 bi0)
492 {
493   vlib_buffer_t *b0 = vlib_get_buffer (vm,
494                                        bi0);
495   icmp6_echo_request_header_t *h0 = vlib_buffer_get_current (b0);
496   f64 rtt = vlib_time_now (vm) - h0->icmp_echo.time_sent;
497
498   vlib_cli_output (vm,
499                    "%d bytes from %U: icmp_seq=%d ttl=%d time=%.4f ms",
500                    clib_host_to_net_u16 (h0->ip6.payload_length),
501                    format_ip6_address,
502                    &h0->ip6.src_address,
503                    clib_host_to_net_u16 (h0->icmp_echo.seq),
504                    h0->ip6.hop_limit, rtt * 1000.0);
505 }
506
507 static void
508 print_ip4_icmp_reply (vlib_main_t * vm, u32 bi0)
509 {
510   vlib_buffer_t *b0 = vlib_get_buffer (vm,
511                                        bi0);
512   icmp4_echo_request_header_t *h0 = vlib_buffer_get_current (b0);
513   f64 rtt = vlib_time_now (vm) - h0->icmp_echo.time_sent;
514   u32 rcvd_icmp_len =
515     clib_host_to_net_u16 (h0->ip4.length) -
516     (4 * (0xF & h0->ip4.ip_version_and_header_length));
517
518   vlib_cli_output (vm,
519                    "%d bytes from %U: icmp_seq=%d ttl=%d time=%.4f ms",
520                    rcvd_icmp_len,
521                    format_ip4_address,
522                    &h0->ip4.src_address,
523                    clib_host_to_net_u16 (h0->icmp_echo.seq),
524                    h0->ip4.ttl, rtt * 1000.0);
525 }
526
527
528 /*
529  * Perform the ping run with the given parameters in the current CLI process.
530  * Depending on whether pa4 or pa6 is set, runs IPv4 or IPv6 ping.
531  * The amusing side effect is of course if both are set, then both pings are sent.
532  * This behavior can be used to ping a dualstack host over IPv4 and IPv6 at once.
533  */
534
535 static void
536 run_ping_ip46_address (vlib_main_t * vm, u32 table_id, ip4_address_t * pa4,
537                        ip6_address_t * pa6, u32 sw_if_index,
538                        f64 ping_interval, u32 ping_repeat, u32 data_len,
539                        u32 verbose)
540 {
541   int i;
542   ping_main_t *pm = &ping_main;
543   uword curr_proc = vlib_current_process (vm);
544   u32 n_replies = 0;
545   u32 n_requests = 0;
546   ping_run_t *pr = 0;
547   u32 ping_run_index = 0;
548   u16 icmp_id;
549
550   static u32 rand_seed = 0;
551
552   if (PREDICT_FALSE(!rand_seed))
553       rand_seed = random_default_seed();
554
555   icmp_id = random_u32(&rand_seed) & 0xffff;
556
557   while (hash_get (pm->ping_run_by_icmp_id, icmp_id))
558     {
559       vlib_cli_output (vm, "ICMP ID collision at %d, incrementing", icmp_id);
560       icmp_id++;
561     }
562   pool_get (pm->ping_runs, pr);
563   ping_run_index = pr - pm->ping_runs;
564   pr->cli_process_id = curr_proc;
565   pr->icmp_id = icmp_id;
566   hash_set (pm->ping_run_by_icmp_id, icmp_id, ping_run_index);
567   for (i = 1; i <= ping_repeat; i++)
568     {
569       f64 sleep_interval;
570       f64 time_ping_sent = vlib_time_now (vm);
571       /* Reset pr: running ping in other process could have changed pm->ping_runs */
572       pr = vec_elt_at_index (pm->ping_runs, ping_run_index);
573       pr->curr_seq = i;
574       if (pa6 &&
575           (SEND_PING_OK == send_ip6_ping (vm, ping_main.ip6_main, table_id, pa6,
576                                           sw_if_index, i, icmp_id, data_len,
577                                           verbose)))
578         {
579           n_requests++;
580         }
581       if (pa4 &&
582           (SEND_PING_OK == send_ip4_ping (vm, ping_main.ip4_main, table_id, pa4,
583                                           sw_if_index, i, icmp_id, data_len,
584                                           verbose)))
585         {
586           n_requests++;
587         }
588       while ((i <= ping_repeat)
589              &&
590              ((sleep_interval =
591                time_ping_sent + ping_interval - vlib_time_now (vm)) > 0.0))
592         {
593           uword event_type, *event_data = 0;
594           vlib_process_wait_for_event_or_clock (vm, sleep_interval);
595           event_type = vlib_process_get_events (vm, &event_data);
596           switch (event_type)
597             {
598             case ~0:           /* no events => timeout */
599               break;
600             case PING_RESPONSE_IP6:
601               {
602                 int i;
603                 for (i = 0; i < vec_len (event_data); i++)
604                   {
605                     u32 bi0 = event_data[0];
606                     print_ip6_icmp_reply (vm, bi0);
607                     n_replies++;
608                     if (0 != bi0)
609                       {
610                         vlib_buffer_free (vm, &bi0, 1);
611                       }
612                   }
613               }
614               break;
615             case PING_RESPONSE_IP4:
616               {
617                 int i;
618                 for (i = 0; i < vec_len (event_data); i++)
619                   {
620                     u32 bi0 = event_data[0];
621                     print_ip4_icmp_reply (vm, bi0);
622                     n_replies++;
623                     if (0 != bi0)
624                       {
625                         vlib_buffer_free (vm, &bi0, 1);
626                       }
627                   }
628               }
629               break;
630             default:
631               /* someone pressed a key, abort */
632               vlib_cli_output (vm, "Aborted due to a keypress.");
633               i = 1 + ping_repeat;
634               break;
635             }
636         }
637     }
638   vlib_cli_output (vm, "\n");
639   {
640     float loss =
641       (0 ==
642        n_requests) ? 0 : 100.0 * ((float) n_requests -
643                                   (float) n_replies) / (float) n_requests;
644     vlib_cli_output (vm,
645                      "Statistics: %u sent, %u received, %f%% packet loss\n",
646                      n_requests, n_replies, loss);
647     /* Reset pr: running ping in other process could have changed pm->ping_runs */
648     pr = vec_elt_at_index (pm->ping_runs, ping_run_index);
649     hash_unset (pm->ping_run_by_icmp_id, icmp_id);
650     pool_put (pm->ping_runs, pr);
651   }
652 }
653
654
655
656
657
658 static clib_error_t *
659 ping_ip_address (vlib_main_t * vm,
660                  unformat_input_t * input, vlib_cli_command_t * cmd)
661 {
662   ip4_address_t a4;
663   ip6_address_t a6;
664   clib_error_t *error = 0;
665   u32 ping_repeat = 5;
666   u8 ping_ip4, ping_ip6;
667   vnet_main_t *vnm = vnet_get_main ();
668   u32 data_len = PING_DEFAULT_DATA_LEN;
669   u32 verbose = 0;
670   f64 ping_interval = PING_DEFAULT_INTERVAL;
671   u32 sw_if_index, table_id;
672
673   table_id = 0;
674   ping_ip4 = ping_ip6 = 0;
675   sw_if_index = ~0;
676
677   if (unformat (input, "%U", unformat_ip4_address, &a4))
678     {
679       ping_ip4 = 1;
680     }
681   else if (unformat (input, "%U", unformat_ip6_address, &a6))
682     {
683       ping_ip6 = 1;
684     }
685   else if (unformat (input, "ipv4"))
686     {
687       if (unformat (input, "%U", unformat_ip4_address, &a4))
688         {
689           ping_ip4 = 1;
690         }
691       else
692         {
693           error =
694             clib_error_return (0,
695                                "expecting IPv4 address but got `%U'",
696                                format_unformat_error, input);
697         }
698     }
699   else if (unformat (input, "ipv6"))
700     {
701       if (unformat (input, "%U", unformat_ip6_address, &a6))
702         {
703           ping_ip6 = 1;
704         }
705       else
706         {
707           error =
708             clib_error_return (0,
709                                "expecting IPv6 address but got `%U'",
710                                format_unformat_error, input);
711         }
712     }
713   else
714     {
715       error =
716         clib_error_return (0,
717                            "expecting IP4/IP6 address `%U'. Usage: ping <addr> [source <intf>] [size <datasz>] [repeat <count>] [verbose]",
718                            format_unformat_error, input);
719       goto done;
720     }
721
722   /* allow for the second AF in the same ping */
723   if (!ping_ip4 && (unformat (input, "ipv4")))
724     {
725       if (unformat (input, "%U", unformat_ip4_address, &a4))
726         {
727           ping_ip4 = 1;
728         }
729     }
730   else if (!ping_ip6 && (unformat (input, "ipv6")))
731     {
732       if (unformat (input, "%U", unformat_ip6_address, &a6))
733         {
734           ping_ip6 = 1;
735         }
736     }
737
738   /* parse the rest of the parameters  in a cycle */
739   while (!unformat_eof (input, NULL))
740     {
741       if (unformat (input, "source"))
742         {
743           if (!unformat_user
744               (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
745             {
746               error =
747                 clib_error_return (0,
748                                    "unknown interface `%U'",
749                                    format_unformat_error, input);
750               goto done;
751             }
752         }
753       else if (unformat (input, "size"))
754         {
755           if (!unformat (input, "%u", &data_len))
756             {
757               error =
758                 clib_error_return (0,
759                                    "expecting size but got `%U'",
760                                    format_unformat_error, input);
761               goto done;
762             }
763         }
764       else if (unformat (input, "table-id"))
765         {
766           if (!unformat (input, "du", &table_id))
767             {
768               error =
769                 clib_error_return (0,
770                                    "expecting table-id but got `%U'",
771                                    format_unformat_error, input);
772               goto done;
773             }
774         }
775       else if (unformat (input, "interval"))
776         {
777           if (!unformat (input, "%f", &ping_interval))
778             {
779               error =
780                 clib_error_return (0,
781                                    "expecting interval (floating point number) got `%U'",
782                                    format_unformat_error, input);
783               goto done;
784             }
785         }
786       else if (unformat (input, "repeat"))
787         {
788           if (!unformat (input, "%u", &ping_repeat))
789             {
790               error =
791                 clib_error_return (0,
792                                    "expecting repeat count but got `%U'",
793                                    format_unformat_error, input);
794               goto done;
795             }
796         }
797       else if (unformat (input, "verbose"))
798         {
799           verbose = 1;
800         }
801       else
802         {
803           error = clib_error_return (0, "unknown input `%U'",
804                                      format_unformat_error, input);
805           goto done;
806         }
807     }
808
809   run_ping_ip46_address (vm, table_id, ping_ip4 ? &a4 : NULL, ping_ip6 ? &a6 : NULL,
810                          sw_if_index, ping_interval, ping_repeat, data_len,
811                          verbose);
812 done:
813   return error;
814 }
815
816 /*?
817  * This command sends an ICMP ECHO_REQUEST to network hosts. The address
818  * can be an IPv4 or IPv6 address (or both at the same time).
819  *
820  * @cliexpar
821  * @parblock
822  * Example of how ping an IPv4 address:
823  * @cliexstart{ping 172.16.1.2 source GigabitEthernet2/0/0 repeat 2}
824  * 64 bytes from 172.16.1.2: icmp_seq=1 ttl=64 time=.1090 ms
825  * 64 bytes from 172.16.1.2: icmp_seq=2 ttl=64 time=.0914 ms
826  *
827  * Statistics: 2 sent, 2 received, 0% packet loss
828  * @cliexend
829  *
830  * Example of how ping both an IPv4 address and IPv6 address at the same time:
831  * @cliexstart{ping 172.16.1.2 ipv6 fe80::24a5:f6ff:fe9c:3a36 source GigabitEthernet2/0/0 repeat 2 verbose}
832  * Adjacency index: 10, sw_if_index: 1
833  * Adj: ip6-discover-neighbor
834  * Adj Interface: 0
835  * Forced set interface: 1
836  * Adjacency index: 0, sw_if_index: 4294967295
837  * Adj: ip4-miss
838  * Adj Interface: 0
839  * Forced set interface: 1
840  * Source address: 172.16.1.1
841  * 64 bytes from 172.16.1.2: icmp_seq=1 ttl=64 time=.1899 ms
842  * Adjacency index: 10, sw_if_index: 1
843  * Adj: ip6-discover-neighbor
844  * Adj Interface: 0
845  * Forced set interface: 1
846  * Adjacency index: 0, sw_if_index: 4294967295
847  * Adj: ip4-miss
848  * Adj Interface: 0
849  * Forced set interface: 1
850  * Source address: 172.16.1.1
851  * 64 bytes from 172.16.1.2: icmp_seq=2 ttl=64 time=.0910 ms
852  *
853  * Statistics: 4 sent, 2 received, 50% packet loss
854  * @cliexend
855  * @endparblock
856 ?*/
857 /* *INDENT-OFF* */
858 VLIB_CLI_COMMAND (ping_command, static) =
859 {
860   .path = "ping",
861   .function = ping_ip_address,
862   .short_help = "ping {<ip-addr> | ipv4 <ip4-addr> | ipv6 <ip6-addr>} [ipv4 <ip4-addr> | ipv6 <ip6-addr>] [source <interface>] [size <pktsize>] [interval <sec>] [repeat <cnt>] [table-id <id>] [verbose]",
863 };
864 /* *INDENT-ON* */
865
866 static clib_error_t *
867 ping_cli_init (vlib_main_t * vm)
868 {
869   ping_main_t *pm = &ping_main;
870   pm->ip6_main = &ip6_main;
871   pm->ip4_main = &ip4_main;
872   icmp6_register_type (vm, ICMP6_echo_reply, ip6_icmp_echo_reply_node.index);
873   ip4_icmp_register_type (vm, ICMP4_echo_reply,
874                           ip4_icmp_echo_reply_node.index);
875   return 0;
876 }
877
878 VLIB_INIT_FUNCTION (ping_cli_init);