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