http: ignore http_proxy env in tests
[vpp.git] / src / plugins / ping / 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
18 #include <vlib/vlib.h>
19 #include <vlib/unix/unix.h>
20 #include <vnet/fib/ip6_fib.h>
21 #include <vnet/fib/ip4_fib.h>
22 #include <vnet/ip/ip_sas.h>
23 #include <vnet/ip/ip6_link.h>
24 #include <vnet/ip/ip6_ll_table.h>
25 #include <vnet/plugin/plugin.h>
26 #include <vpp/app/version.h>
27
28 #include <vnet/ip/icmp4.h>
29 #include <ping/ping.h>
30
31 ping_main_t ping_main;
32
33 /**
34  * @file
35  * @brief IPv4 and IPv6 ICMP Ping.
36  *
37  * This file contains code to support IPv4 or IPv6 ICMP ECHO_REQUEST to
38  * network hosts.
39  *
40  */
41
42 typedef struct
43 {
44   u16 id;
45   u16 seq;
46   u32 cli_process_node;
47   u8 is_ip6;
48 } icmp_echo_trace_t;
49
50
51 u8 *
52 format_icmp_echo_trace (u8 * s, va_list * va)
53 {
54   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
55   CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
56   icmp_echo_trace_t *t = va_arg (*va, icmp_echo_trace_t *);
57
58   s =
59     format (s, "ICMP%s echo id %d seq %d", t->is_ip6 ? "6" : "4", t->id,
60             t->seq);
61   if (t->cli_process_node == PING_CLI_UNKNOWN_NODE)
62     {
63       s = format (s, " (unknown)");
64     }
65   else
66     {
67       s = format (s, " send to cli node %d", t->cli_process_node);
68     }
69
70   return s;
71 }
72
73
74 static u8 *
75 format_ip46_ping_result (u8 * s, va_list * args)
76 {
77   send_ip46_ping_result_t res = va_arg (*args, send_ip46_ping_result_t);
78
79   switch (res)
80     {
81 #define _(v, n) case SEND_PING_##v: s = format(s, "%s", n);break;
82       foreach_ip46_ping_result
83 #undef _
84     }
85
86   return (s);
87 }
88
89
90 /*
91  * Poor man's get-set-clear functions
92  * for manipulation of icmp_id -> cli_process_id
93  * mappings.
94  *
95  * There should normally be very few (0..1..2) of these
96  * mappings, so the linear search is a good strategy.
97  *
98  * Make them thread-safe via a simple spinlock.
99  *
100  */
101
102 static_always_inline int
103 ip46_get_icmp_id_and_seq (vlib_main_t * vm, vlib_buffer_t * b0,
104                           u16 * out_icmp_id, u16 * out_icmp_seq, int is_ip6)
105 {
106   int l4_offset;
107   if (is_ip6)
108     {
109       ip6_header_t *ip6 = vlib_buffer_get_current (b0);
110       if (ip6->protocol != IP_PROTOCOL_ICMP6)
111         {
112           return 0;
113         }
114       l4_offset = sizeof (*ip6);        // IPv6 EH
115     }
116   else
117     {
118       ip4_header_t *ip4 = vlib_buffer_get_current (b0);
119       l4_offset = ip4_header_bytes (ip4);
120
121     }
122   icmp46_header_t *icmp46 = vlib_buffer_get_current (b0) + l4_offset;
123   icmp46_echo_request_t *icmp46_echo = (icmp46_echo_request_t *) (icmp46 + 1);
124
125   *out_icmp_id = clib_net_to_host_u16 (icmp46_echo->id);
126   *out_icmp_seq = clib_net_to_host_u16 (icmp46_echo->seq);
127   return 1;
128 }
129
130 /*
131  * post the buffer to a given cli process node - the caller should forget bi0 after return.
132  */
133
134 static_always_inline void
135 ip46_post_icmp_reply_event (vlib_main_t * vm, uword cli_process_id, u32 bi0,
136                             int is_ip6)
137 {
138   vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
139   u64 nowts = clib_cpu_time_now ();
140
141   /* Pass the timestamp to the cli_process thanks to the vnet_buffer unused metadata field */
142
143   /* Camping on unused data... just ensure statically that there is enough space */
144   STATIC_ASSERT (ARRAY_LEN (vnet_buffer (b0)->unused) *
145                  sizeof (vnet_buffer (b0)->unused[0]) > sizeof (nowts),
146                  "ping reply timestamp fits within remaining space of vnet_buffer unused data");
147   u64 *pnowts = (void *) &vnet_buffer (b0)->unused[0];
148   *pnowts = nowts;
149
150   u32 event_id = is_ip6 ? PING_RESPONSE_IP6 : PING_RESPONSE_IP4;
151   vlib_process_signal_event_mt (vm, cli_process_id, event_id, bi0);
152 }
153
154
155 static_always_inline void
156 ip46_echo_reply_maybe_trace_buffer (vlib_main_t * vm,
157                                     vlib_node_runtime_t * node,
158                                     uword cli_process_id, u16 id, u16 seq,
159                                     vlib_buffer_t * b0, int is_ip6)
160 {
161   if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
162     {
163       icmp_echo_trace_t *tr = vlib_add_trace (vm, node, b0, sizeof (*tr));
164       tr->id = id;
165       tr->seq = seq;
166       tr->cli_process_node = cli_process_id;
167       tr->is_ip6 = is_ip6;
168     }
169 }
170
171
172 static_always_inline uword
173 ip46_icmp_echo_reply_inner_node_fn (vlib_main_t * vm,
174                                     vlib_node_runtime_t * node,
175                                     vlib_frame_t * frame, int do_trace,
176                                     int is_ip6)
177 {
178   u32 n_left_from, *from, *to_next;
179   icmp46_echo_reply_next_t next_index;
180
181   from = vlib_frame_vector_args (frame);
182   n_left_from = frame->n_vectors;
183
184   next_index = node->cached_next_index;
185
186   while (n_left_from > 0)
187     {
188       u32 n_left_to_next;
189       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
190
191       while (n_left_from > 0 && n_left_to_next > 0)
192         {
193           u32 bi0;
194           vlib_buffer_t *b0;
195           /*
196            * The buffers (replies) are either posted to the CLI thread
197            * awaiting for them for subsequent analysis and disposal,
198            * or are sent to the punt node.
199            *
200            * So the only "next" node is a punt, normally.
201            */
202           u32 next0 = ICMP46_ECHO_REPLY_NEXT_PUNT;
203
204           bi0 = from[0];
205           b0 = vlib_get_buffer (vm, bi0);
206           from += 1;
207           n_left_from -= 1;
208
209           u16 icmp_id = ~0;
210           u16 icmp_seq = ~0;
211           uword cli_process_id = PING_CLI_UNKNOWN_NODE;
212
213           if (ip46_get_icmp_id_and_seq (vm, b0, &icmp_id, &icmp_seq, is_ip6))
214             {
215               cli_process_id = get_cli_process_id_by_icmp_id_mt (vm, icmp_id);
216             }
217
218           if (do_trace)
219             ip46_echo_reply_maybe_trace_buffer (vm, node, cli_process_id,
220                                                 icmp_id, icmp_seq, b0,
221                                                 is_ip6);
222
223           if (~0 == cli_process_id)
224             {
225               /* no outstanding requests for this reply, punt */
226               /* speculatively enqueue b0 to the current next frame */
227               to_next[0] = bi0;
228               to_next += 1;
229               n_left_to_next -= 1;
230               /* verify speculative enqueue, maybe switch current next frame */
231               vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
232                                                to_next, n_left_to_next,
233                                                bi0, next0);
234             }
235           else
236             {
237               /* Post the buffer to CLI thread. It will take care of freeing it. */
238               ip46_post_icmp_reply_event (vm, cli_process_id, bi0, is_ip6);
239             }
240         }
241       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
242     }
243   return frame->n_vectors;
244 }
245
246 /*
247  * select "with-trace" or "without-trace" codepaths upfront.
248  */
249 static_always_inline uword
250 ip46_icmp_echo_reply_outer_node_fn (vlib_main_t * vm,
251                                     vlib_node_runtime_t * node,
252                                     vlib_frame_t * frame, int is_ip6)
253 {
254   if (node->flags & VLIB_NODE_FLAG_TRACE)
255     return ip46_icmp_echo_reply_inner_node_fn (vm, node, frame,
256                                                1 /* do_trace */ , is_ip6);
257   else
258     return ip46_icmp_echo_reply_inner_node_fn (vm, node, frame,
259                                                0 /* do_trace */ , is_ip6);
260 }
261
262 static uword
263 ip4_icmp_echo_reply_node_fn (vlib_main_t * vm,
264                              vlib_node_runtime_t * node, vlib_frame_t * frame)
265 {
266   return ip46_icmp_echo_reply_outer_node_fn (vm, node, frame,
267                                              0 /* is_ip6 */ );
268 }
269
270 static uword
271 ip6_icmp_echo_reply_node_fn (vlib_main_t * vm,
272                              vlib_node_runtime_t * node, vlib_frame_t * frame)
273 {
274   return ip46_icmp_echo_reply_outer_node_fn (vm, node, frame,
275                                              1 /* is_ip6 */ );
276 }
277
278 VLIB_REGISTER_NODE (ip6_icmp_echo_reply_node, static) =
279 {
280   .function = ip6_icmp_echo_reply_node_fn,
281   .name = "ip6-icmp-echo-reply",
282   .vector_size = sizeof (u32),
283   .format_trace = format_icmp_echo_trace,
284   .n_next_nodes = ICMP46_ECHO_REPLY_N_NEXT,
285   .next_nodes = {
286     [ICMP46_ECHO_REPLY_NEXT_DROP] = "ip6-drop",
287     [ICMP46_ECHO_REPLY_NEXT_PUNT] = "ip6-punt",
288   },
289 };
290
291 VLIB_REGISTER_NODE (ip4_icmp_echo_reply_node, static) =
292 {
293   .function = ip4_icmp_echo_reply_node_fn,
294   .name = "ip4-icmp-echo-reply",
295   .vector_size = sizeof (u32),
296   .format_trace = format_icmp_echo_trace,
297   .n_next_nodes = ICMP46_ECHO_REPLY_N_NEXT,
298   .next_nodes = {
299     [ICMP46_ECHO_REPLY_NEXT_DROP] = "ip4-drop",
300     [ICMP46_ECHO_REPLY_NEXT_PUNT] = "ip4-punt",
301   },
302 };
303
304 static uword
305 ip4_icmp_echo_request (vlib_main_t * vm,
306                        vlib_node_runtime_t * node, vlib_frame_t * frame)
307 {
308   uword n_packets = frame->n_vectors;
309   u32 *from, *to_next;
310   u32 n_left_from, n_left_to_next, next;
311   ip4_main_t *i4m = &ip4_main;
312   u16 *fragment_ids, *fid;
313   u8 host_config_ttl = i4m->host_config.ttl;
314
315   from = vlib_frame_vector_args (frame);
316   n_left_from = n_packets;
317   next = node->cached_next_index;
318
319   if (node->flags & VLIB_NODE_FLAG_TRACE)
320     vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
321                                    /* stride */ 1,
322                                    sizeof (icmp_input_trace_t));
323
324   /* Get random fragment IDs for replies. */
325   fid = fragment_ids = clib_random_buffer_get_data (&vm->random_buffer,
326                                                     n_packets *
327                                                     sizeof (fragment_ids[0]));
328
329   while (n_left_from > 0)
330     {
331       vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
332
333       while (n_left_from > 2 && n_left_to_next > 2)
334         {
335           vlib_buffer_t *p0, *p1;
336           ip4_header_t *ip0, *ip1;
337           icmp46_header_t *icmp0, *icmp1;
338           u32 bi0, src0, dst0;
339           u32 bi1, src1, dst1;
340           ip_csum_t sum0, sum1;
341
342           bi0 = to_next[0] = from[0];
343           bi1 = to_next[1] = from[1];
344
345           from += 2;
346           n_left_from -= 2;
347           to_next += 2;
348           n_left_to_next -= 2;
349
350           p0 = vlib_get_buffer (vm, bi0);
351           p1 = vlib_get_buffer (vm, bi1);
352           ip0 = vlib_buffer_get_current (p0);
353           ip1 = vlib_buffer_get_current (p1);
354           icmp0 = ip4_next_header (ip0);
355           icmp1 = ip4_next_header (ip1);
356
357           vnet_buffer (p0)->sw_if_index[VLIB_RX] =
358             vnet_main.local_interface_sw_if_index;
359           vnet_buffer (p1)->sw_if_index[VLIB_RX] =
360             vnet_main.local_interface_sw_if_index;
361
362           /* Update ICMP checksum. */
363           sum0 = icmp0->checksum;
364           sum1 = icmp1->checksum;
365
366           ASSERT (icmp0->type == ICMP4_echo_request);
367           ASSERT (icmp1->type == ICMP4_echo_request);
368           sum0 = ip_csum_update (sum0, ICMP4_echo_request, ICMP4_echo_reply,
369                                  icmp46_header_t, type);
370           sum1 = ip_csum_update (sum1, ICMP4_echo_request, ICMP4_echo_reply,
371                                  icmp46_header_t, type);
372           icmp0->type = ICMP4_echo_reply;
373           icmp1->type = ICMP4_echo_reply;
374
375           icmp0->checksum = ip_csum_fold (sum0);
376           icmp1->checksum = ip_csum_fold (sum1);
377
378           src0 = ip0->src_address.data_u32;
379           src1 = ip1->src_address.data_u32;
380           dst0 = ip0->dst_address.data_u32;
381           dst1 = ip1->dst_address.data_u32;
382
383           /* Swap source and destination address.
384              Does not change checksum. */
385           ip0->src_address.data_u32 = dst0;
386           ip1->src_address.data_u32 = dst1;
387           ip0->dst_address.data_u32 = src0;
388           ip1->dst_address.data_u32 = src1;
389
390           /* Update IP checksum. */
391           sum0 = ip0->checksum;
392           sum1 = ip1->checksum;
393
394           sum0 = ip_csum_update (sum0, ip0->ttl, host_config_ttl,
395                                  ip4_header_t, ttl);
396           sum1 = ip_csum_update (sum1, ip1->ttl, host_config_ttl,
397                                  ip4_header_t, ttl);
398           ip0->ttl = host_config_ttl;
399           ip1->ttl = host_config_ttl;
400
401           /* New fragment id. */
402           sum0 = ip_csum_update (sum0, ip0->fragment_id, fid[0],
403                                  ip4_header_t, fragment_id);
404           sum1 = ip_csum_update (sum1, ip1->fragment_id, fid[1],
405                                  ip4_header_t, fragment_id);
406           ip0->fragment_id = fid[0];
407           ip1->fragment_id = fid[1];
408           fid += 2;
409
410           ip0->checksum = ip_csum_fold (sum0);
411           ip1->checksum = ip_csum_fold (sum1);
412
413           ASSERT (ip4_header_checksum_is_valid (ip0));
414           ASSERT (ip4_header_checksum_is_valid (ip1));
415
416           p0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
417           p1->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
418         }
419
420       while (n_left_from > 0 && n_left_to_next > 0)
421         {
422           vlib_buffer_t *p0;
423           ip4_header_t *ip0;
424           icmp46_header_t *icmp0;
425           u32 bi0, src0, dst0;
426           ip_csum_t sum0;
427
428           bi0 = to_next[0] = from[0];
429
430           from += 1;
431           n_left_from -= 1;
432           to_next += 1;
433           n_left_to_next -= 1;
434
435           p0 = vlib_get_buffer (vm, bi0);
436           ip0 = vlib_buffer_get_current (p0);
437           icmp0 = ip4_next_header (ip0);
438
439           vnet_buffer (p0)->sw_if_index[VLIB_RX] =
440             vnet_main.local_interface_sw_if_index;
441
442           /* Update ICMP checksum. */
443           sum0 = icmp0->checksum;
444
445           ASSERT (icmp0->type == ICMP4_echo_request);
446           sum0 = ip_csum_update (sum0, ICMP4_echo_request, ICMP4_echo_reply,
447                                  icmp46_header_t, type);
448           icmp0->type = ICMP4_echo_reply;
449           icmp0->checksum = ip_csum_fold (sum0);
450
451           src0 = ip0->src_address.data_u32;
452           dst0 = ip0->dst_address.data_u32;
453           ip0->src_address.data_u32 = dst0;
454           ip0->dst_address.data_u32 = src0;
455
456           /* Update IP checksum. */
457           sum0 = ip0->checksum;
458
459           sum0 = ip_csum_update (sum0, ip0->ttl, host_config_ttl,
460                                  ip4_header_t, ttl);
461           ip0->ttl = host_config_ttl;
462
463           sum0 = ip_csum_update (sum0, ip0->fragment_id, fid[0],
464                                  ip4_header_t, fragment_id);
465           ip0->fragment_id = fid[0];
466           fid += 1;
467
468           ip0->checksum = ip_csum_fold (sum0);
469
470           ASSERT (ip4_header_checksum_is_valid (ip0));
471
472           p0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
473         }
474
475       vlib_put_next_frame (vm, node, next, n_left_to_next);
476     }
477
478   vlib_error_count (vm, ip4_icmp_input_node.index,
479                     ICMP4_ERROR_ECHO_REPLIES_SENT, frame->n_vectors);
480
481   return frame->n_vectors;
482 }
483
484 static u8 *
485 format_icmp_input_trace (u8 * s, va_list * va)
486 {
487   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
488   CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
489   icmp_input_trace_t *t = va_arg (*va, icmp_input_trace_t *);
490
491   s = format (s, "%U",
492               format_ip4_header, t->packet_data, sizeof (t->packet_data));
493
494   return s;
495 }
496
497 VLIB_REGISTER_NODE (ip4_icmp_echo_request_node,static) = {
498   .function = ip4_icmp_echo_request,
499   .name = "ip4-icmp-echo-request",
500
501   .vector_size = sizeof (u32),
502
503   .format_trace = format_icmp_input_trace,
504
505   .n_next_nodes = 1,
506   .next_nodes = {
507     [0] = "ip4-load-balance",
508   },
509 };
510
511 typedef enum
512 {
513   ICMP6_ECHO_REQUEST_NEXT_LOOKUP,
514   ICMP6_ECHO_REQUEST_NEXT_OUTPUT,
515   ICMP6_ECHO_REQUEST_N_NEXT,
516 } icmp6_echo_request_next_t;
517
518 static uword
519 ip6_icmp_echo_request (vlib_main_t *vm, vlib_node_runtime_t *node,
520                        vlib_frame_t *frame)
521 {
522   u32 *from, *to_next;
523   u32 n_left_from, n_left_to_next, next_index;
524   ip6_main_t *im = &ip6_main;
525
526   from = vlib_frame_vector_args (frame);
527   n_left_from = frame->n_vectors;
528   next_index = node->cached_next_index;
529
530   if (node->flags & VLIB_NODE_FLAG_TRACE)
531     vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
532                                    /* stride */ 1,
533                                    sizeof (icmp6_input_trace_t));
534
535   while (n_left_from > 0)
536     {
537       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
538
539       while (n_left_from > 2 && n_left_to_next > 2)
540         {
541           vlib_buffer_t *p0, *p1;
542           ip6_header_t *ip0, *ip1;
543           icmp46_header_t *icmp0, *icmp1;
544           ip6_address_t tmp0, tmp1;
545           ip_csum_t sum0, sum1;
546           u32 bi0, bi1;
547           u32 fib_index0, fib_index1;
548           u32 next0 = ICMP6_ECHO_REQUEST_NEXT_LOOKUP;
549           u32 next1 = ICMP6_ECHO_REQUEST_NEXT_LOOKUP;
550
551           bi0 = to_next[0] = from[0];
552           bi1 = to_next[1] = from[1];
553
554           from += 2;
555           n_left_from -= 2;
556           to_next += 2;
557           n_left_to_next -= 2;
558
559           p0 = vlib_get_buffer (vm, bi0);
560           p1 = vlib_get_buffer (vm, bi1);
561           ip0 = vlib_buffer_get_current (p0);
562           ip1 = vlib_buffer_get_current (p1);
563           icmp0 = ip6_next_header (ip0);
564           icmp1 = ip6_next_header (ip1);
565
566           /* Check icmp type to echo reply and update icmp checksum. */
567           sum0 = icmp0->checksum;
568           sum1 = icmp1->checksum;
569
570           ASSERT (icmp0->type == ICMP6_echo_request);
571           ASSERT (icmp1->type == ICMP6_echo_request);
572           sum0 = ip_csum_update (sum0, ICMP6_echo_request, ICMP6_echo_reply,
573                                  icmp46_header_t, type);
574           sum1 = ip_csum_update (sum1, ICMP6_echo_request, ICMP6_echo_reply,
575                                  icmp46_header_t, type);
576
577           icmp0->checksum = ip_csum_fold (sum0);
578           icmp1->checksum = ip_csum_fold (sum1);
579
580           icmp0->type = ICMP6_echo_reply;
581           icmp1->type = ICMP6_echo_reply;
582
583           /* Swap source and destination address. */
584           tmp0 = ip0->src_address;
585           tmp1 = ip1->src_address;
586
587           ip0->src_address = ip0->dst_address;
588           ip1->src_address = ip1->dst_address;
589
590           ip0->dst_address = tmp0;
591           ip1->dst_address = tmp1;
592
593           /* New hop count. */
594           ip0->hop_limit = im->host_config.ttl;
595           ip1->hop_limit = im->host_config.ttl;
596
597           if (ip6_address_is_link_local_unicast (&ip0->src_address) &&
598               !ip6_address_is_link_local_unicast (&ip0->dst_address))
599             {
600               fib_index0 = vec_elt (im->fib_index_by_sw_if_index,
601                                     vnet_buffer (p0)->sw_if_index[VLIB_RX]);
602               vnet_buffer (p0)->sw_if_index[VLIB_TX] = fib_index0;
603             }
604           if (ip6_address_is_link_local_unicast (&ip1->src_address) &&
605               !ip6_address_is_link_local_unicast (&ip1->dst_address))
606             {
607               fib_index1 = vec_elt (im->fib_index_by_sw_if_index,
608                                     vnet_buffer (p1)->sw_if_index[VLIB_RX]);
609               vnet_buffer (p1)->sw_if_index[VLIB_TX] = fib_index1;
610             }
611           p0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
612           p1->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
613
614           /* verify speculative enqueues, maybe switch current next frame */
615           /* if next0==next1==next_index then nothing special needs to be done
616            */
617           vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
618                                            n_left_to_next, bi0, bi1, next0,
619                                            next1);
620         }
621
622       while (n_left_from > 0 && n_left_to_next > 0)
623         {
624           vlib_buffer_t *p0;
625           ip6_header_t *ip0;
626           icmp46_header_t *icmp0;
627           u32 bi0;
628           ip6_address_t tmp0;
629           ip_csum_t sum0;
630           u32 fib_index0;
631           u32 next0 = ICMP6_ECHO_REQUEST_NEXT_LOOKUP;
632
633           bi0 = to_next[0] = from[0];
634
635           from += 1;
636           n_left_from -= 1;
637           to_next += 1;
638           n_left_to_next -= 1;
639
640           p0 = vlib_get_buffer (vm, bi0);
641           ip0 = vlib_buffer_get_current (p0);
642           icmp0 = ip6_next_header (ip0);
643
644           /* Check icmp type to echo reply and update icmp checksum. */
645           sum0 = icmp0->checksum;
646
647           ASSERT (icmp0->type == ICMP6_echo_request);
648           sum0 = ip_csum_update (sum0, ICMP6_echo_request, ICMP6_echo_reply,
649                                  icmp46_header_t, type);
650
651           icmp0->checksum = ip_csum_fold (sum0);
652
653           icmp0->type = ICMP6_echo_reply;
654
655           /* Swap source and destination address. */
656           tmp0 = ip0->src_address;
657           ip0->src_address = ip0->dst_address;
658           ip0->dst_address = tmp0;
659
660           ip0->hop_limit = im->host_config.ttl;
661
662           if (ip6_address_is_link_local_unicast (&ip0->src_address) &&
663               !ip6_address_is_link_local_unicast (&ip0->dst_address))
664             {
665               /* if original packet was to the link local, then the
666                * fib index is that of the LL table, we can't use that
667                * to foward the response if the new destination
668                * is global, so reset to the fib index of the link.
669                * In other case, the fib index we need has been written
670                * to the buffer already. */
671               fib_index0 = vec_elt (im->fib_index_by_sw_if_index,
672                                     vnet_buffer (p0)->sw_if_index[VLIB_RX]);
673               vnet_buffer (p0)->sw_if_index[VLIB_TX] = fib_index0;
674             }
675           p0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
676           /* Verify speculative enqueue, maybe switch current next frame */
677           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
678                                            n_left_to_next, bi0, next0);
679         }
680
681       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
682     }
683
684   vlib_error_count (vm, ip6_icmp_input_node.index,
685                     ICMP6_ERROR_ECHO_REPLIES_SENT, frame->n_vectors);
686
687   return frame->n_vectors;
688 }
689
690 VLIB_REGISTER_NODE (ip6_icmp_echo_request_node,static) = {
691   .function = ip6_icmp_echo_request,
692   .name = "ip6-icmp-echo-request",
693
694   .vector_size = sizeof (u32),
695
696   .format_trace = format_icmp6_input_trace,
697
698   .n_next_nodes = ICMP6_ECHO_REQUEST_N_NEXT,
699   .next_nodes = {
700     [ICMP6_ECHO_REQUEST_NEXT_LOOKUP] = "ip6-lookup",
701     [ICMP6_ECHO_REQUEST_NEXT_OUTPUT] = "interface-output",
702   },
703 };
704
705 /*
706  * A swarm of address-family agnostic helper functions
707  * for building and sending the ICMP echo request.
708  *
709  * Deliberately mostly "static" rather than "static inline"
710  * so one can trace them sanely if needed in debugger, if needed.
711  *
712  */
713
714 static_always_inline u8
715 get_icmp_echo_payload_byte (int offset)
716 {
717   return (offset % 256);
718 }
719
720 /* Fill in the ICMP ECHO structure, return the safety-checked and possibly shrunk data_len */
721 static u16
722 init_icmp46_echo_request (vlib_main_t * vm, vlib_buffer_t * b0,
723                           int l4_header_offset,
724                           icmp46_echo_request_t * icmp46_echo, u16 seq_host,
725                           u16 id_host, u64 now, u16 data_len)
726 {
727   int i;
728
729
730   int l34_len =
731     l4_header_offset + sizeof (icmp46_header_t) +
732     offsetof (icmp46_echo_request_t, data);
733   int max_data_len = vlib_buffer_get_default_data_size (vm) - l34_len;
734
735   int first_buf_data_len = data_len < max_data_len ? data_len : max_data_len;
736
737   int payload_offset = 0;
738   for (i = 0; i < first_buf_data_len; i++)
739     icmp46_echo->data[i] = get_icmp_echo_payload_byte (payload_offset++);
740
741   /* inspired by vlib_buffer_add_data */
742   vlib_buffer_t *hb = b0;
743   int remaining_data_len = data_len - first_buf_data_len;
744   while (remaining_data_len)
745     {
746       int this_buf_data_len =
747         remaining_data_len <
748         vlib_buffer_get_default_data_size (vm) ? remaining_data_len :
749         vlib_buffer_get_default_data_size (vm);
750       int n_alloc = vlib_buffer_alloc (vm, &b0->next_buffer, 1);
751       if (n_alloc < 1)
752         {
753           /* That is how much we have so far - return it... */
754           return (data_len - remaining_data_len);
755         }
756       b0->flags |= VLIB_BUFFER_NEXT_PRESENT;
757       /* move on to the newly acquired buffer */
758       b0 = vlib_get_buffer (vm, b0->next_buffer);
759       /* initialize the data */
760       for (i = 0; i < this_buf_data_len; i++)
761         {
762           b0->data[i] = get_icmp_echo_payload_byte (payload_offset++);
763         }
764       b0->current_length = this_buf_data_len;
765       b0->current_data = 0;
766       remaining_data_len -= this_buf_data_len;
767     }
768   hb->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
769   hb->current_length = l34_len + first_buf_data_len;
770   hb->total_length_not_including_first_buffer = data_len - first_buf_data_len;
771
772   icmp46_echo->time_sent = now;
773   icmp46_echo->seq = clib_host_to_net_u16 (seq_host);
774   icmp46_echo->id = clib_host_to_net_u16 (id_host);
775   return data_len;
776 }
777
778
779 static u32
780 ip46_fib_index_from_table_id (u32 table_id, int is_ip6)
781 {
782   u32 fib_index = is_ip6 ?
783     ip6_fib_index_from_table_id (table_id) :
784     ip4_fib_index_from_table_id (table_id);
785   return fib_index;
786 }
787
788 static fib_node_index_t
789 ip46_fib_table_lookup_host (u32 fib_index, ip46_address_t * pa46, int is_ip6)
790 {
791   fib_node_index_t fib_entry_index = is_ip6 ?
792     ip6_fib_table_lookup (fib_index, &pa46->ip6, 128) :
793     ip4_fib_table_lookup (ip4_fib_get (fib_index), &pa46->ip4, 32);
794   return fib_entry_index;
795 }
796
797 static u32
798 ip46_get_resolving_interface (u32 fib_index, ip46_address_t * pa46,
799                               int is_ip6)
800 {
801   u32 sw_if_index = ~0;
802   if (~0 != fib_index)
803     {
804       fib_node_index_t fib_entry_index;
805       fib_entry_index = ip46_fib_table_lookup_host (fib_index, pa46, is_ip6);
806       sw_if_index = fib_entry_get_resolving_interface (fib_entry_index);
807     }
808   return sw_if_index;
809 }
810
811 static u32
812 ip46_fib_table_get_index_for_sw_if_index (u32 sw_if_index, int is_ip6,
813                                           ip46_address_t *pa46)
814 {
815   if (is_ip6)
816     {
817       if (ip6_address_is_link_local_unicast (&pa46->ip6))
818         return ip6_ll_fib_get (sw_if_index);
819       return ip6_fib_table_get_index_for_sw_if_index (sw_if_index);
820     }
821   return ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
822 }
823
824
825 static int
826 ip46_fill_l3_header (ip46_address_t * pa46, vlib_buffer_t * b0, int is_ip6)
827 {
828   if (is_ip6)
829     {
830       ip6_header_t *ip6 = vlib_buffer_get_current (b0);
831       /* Fill in ip6 header fields */
832       ip6->ip_version_traffic_class_and_flow_label =
833         clib_host_to_net_u32 (0x6 << 28);
834       ip6->payload_length = 0;  /* will be set later */
835       ip6->protocol = IP_PROTOCOL_ICMP6;
836       ip6->hop_limit = 255;
837       ip6->dst_address = pa46->ip6;
838       ip6->src_address = pa46->ip6;
839       return (sizeof (ip6_header_t));
840     }
841   else
842     {
843       ip4_header_t *ip4 = vlib_buffer_get_current (b0);
844       /* Fill in ip4 header fields */
845       ip4->checksum = 0;
846       ip4->ip_version_and_header_length = 0x45;
847       ip4->tos = 0;
848       ip4->length = 0;          /* will be set later */
849       ip4->fragment_id = 0;
850       ip4->flags_and_fragment_offset = 0;
851       ip4->ttl = 0xff;
852       ip4->protocol = IP_PROTOCOL_ICMP;
853       ip4->src_address = pa46->ip4;
854       ip4->dst_address = pa46->ip4;
855       return (sizeof (ip4_header_t));
856     }
857 }
858
859 static bool
860 ip46_set_src_address (u32 sw_if_index, vlib_buffer_t * b0, int is_ip6)
861 {
862   bool res = false;
863
864   if (is_ip6)
865     {
866       ip6_header_t *ip6 = vlib_buffer_get_current (b0);
867
868       res = ip6_sas_by_sw_if_index (sw_if_index, &ip6->dst_address,
869                                     &ip6->src_address);
870     }
871   else
872     {
873       ip4_header_t *ip4 = vlib_buffer_get_current (b0);
874
875       res = ip4_sas_by_sw_if_index (sw_if_index, &ip4->dst_address,
876                                     &ip4->src_address);
877     }
878   return res;
879 }
880
881 static void
882 ip46_print_buffer_src_address (vlib_main_t * vm, vlib_buffer_t * b0,
883                                int is_ip6)
884 {
885   void *format_addr_func;
886   void *paddr;
887   if (is_ip6)
888     {
889       ip6_header_t *ip6 = vlib_buffer_get_current (b0);
890       format_addr_func = format_ip6_address;
891       paddr = &ip6->src_address;
892     }
893   else
894     {
895       ip4_header_t *ip4 = vlib_buffer_get_current (b0);
896       format_addr_func = format_ip4_address;
897       paddr = &ip4->src_address;
898     }
899   vlib_cli_output (vm, "Source address: %U ", format_addr_func, paddr);
900 }
901
902 static u16
903 ip46_fill_icmp_request_at (vlib_main_t * vm, int l4_offset, u16 seq_host,
904                            u16 id_host, u16 data_len, vlib_buffer_t * b0,
905                            int is_ip6)
906 {
907   icmp46_header_t *icmp46 = vlib_buffer_get_current (b0) + l4_offset;
908
909   icmp46->type = is_ip6 ? ICMP6_echo_request : ICMP4_echo_request;
910   icmp46->code = 0;
911   icmp46->checksum = 0;
912
913   icmp46_echo_request_t *icmp46_echo = (icmp46_echo_request_t *) (icmp46 + 1);
914
915   data_len =
916     init_icmp46_echo_request (vm, b0, l4_offset, icmp46_echo, seq_host,
917                               id_host, clib_cpu_time_now (), data_len);
918   return data_len;
919 }
920
921
922 /* Compute ICMP4 checksum with multibuffer support. */
923 u16
924 ip4_icmp_compute_checksum (vlib_main_t * vm, vlib_buffer_t * p0,
925                            ip4_header_t * ip0)
926 {
927   ip_csum_t sum0;
928   u32 ip_header_length, payload_length_host_byte_order;
929   u32 n_this_buffer, n_bytes_left, n_ip_bytes_this_buffer;
930   u16 sum16;
931   void *data_this_buffer;
932
933   ip_header_length = ip4_header_bytes (ip0);
934   payload_length_host_byte_order =
935     clib_net_to_host_u16 (ip0->length) - ip_header_length;
936
937   /* ICMP4 checksum does not include the IP header */
938   sum0 = 0;
939
940   n_bytes_left = n_this_buffer = payload_length_host_byte_order;
941   data_this_buffer = (void *) ip0 + ip_header_length;
942   n_ip_bytes_this_buffer =
943     p0->current_length - (((u8 *) ip0 - p0->data) - p0->current_data);
944   if (n_this_buffer + ip_header_length > n_ip_bytes_this_buffer)
945     {
946       n_this_buffer = n_ip_bytes_this_buffer > ip_header_length ?
947         n_ip_bytes_this_buffer - ip_header_length : 0;
948     }
949   while (1)
950     {
951       sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
952       n_bytes_left -= n_this_buffer;
953       if (n_bytes_left == 0)
954         break;
955
956       ASSERT (p0->flags & VLIB_BUFFER_NEXT_PRESENT);
957       p0 = vlib_get_buffer (vm, p0->next_buffer);
958       data_this_buffer = vlib_buffer_get_current (p0);
959       n_this_buffer = p0->current_length;
960     }
961
962   sum16 = ~ip_csum_fold (sum0);
963
964   return sum16;
965 }
966
967
968 static void
969 ip46_fix_len_and_csum (vlib_main_t * vm, int l4_offset, u16 data_len,
970                        vlib_buffer_t * b0, int is_ip6)
971 {
972   u16 payload_length =
973     data_len + sizeof (icmp46_header_t) + offsetof (icmp46_echo_request_t,
974                                                     data);
975   u16 total_length = payload_length + l4_offset;
976   icmp46_header_t *icmp46 = vlib_buffer_get_current (b0) + l4_offset;
977   icmp46->checksum = 0;
978
979   if (is_ip6)
980     {
981       ip6_header_t *ip6 = vlib_buffer_get_current (b0);
982       ip6->payload_length = clib_host_to_net_u16 (payload_length);
983
984       int bogus_length = 0;
985       icmp46->checksum =
986         ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip6, &bogus_length);
987     }
988   else
989     {
990       ip4_header_t *ip4 = vlib_buffer_get_current (b0);
991       ip4->length = clib_host_to_net_u16 (total_length);
992
993       ip4->checksum = ip4_header_checksum (ip4);
994       icmp46->checksum = ip4_icmp_compute_checksum (vm, b0, ip4);
995     }
996 }
997
998 static u16
999 at_most_a_frame (u32 count)
1000 {
1001   return count > VLIB_FRAME_SIZE ? VLIB_FRAME_SIZE : count;
1002 }
1003
1004 static int
1005 ip46_enqueue_packet (vlib_main_t *vm, vlib_buffer_t *b0, u32 burst,
1006                      u32 lookup_node_index)
1007 {
1008   vlib_frame_t *f = 0;
1009   int n_sent = 0;
1010
1011   u16 n_to_send;
1012
1013   /*
1014    * Enqueue the packet, possibly as one or more frames of copies to make
1015    * bursts. We enqueue b0 as the very last buffer, when there is no possibility
1016    * for error in vlib_buffer_copy, so as to allow the caller to free it
1017    * in case we encounter the error in the middle of the loop.
1018    */
1019   for (n_to_send = at_most_a_frame (burst), burst -= n_to_send; n_to_send > 0;
1020        n_to_send = at_most_a_frame (burst), burst -= n_to_send)
1021     {
1022       f = vlib_get_frame_to_node (vm, lookup_node_index);
1023       /* f can not be NULL here - frame allocation failure causes panic */
1024
1025       u32 *to_next = vlib_frame_vector_args (f);
1026       f->n_vectors = n_to_send;
1027
1028       while (n_to_send > 1)
1029         {
1030           vlib_buffer_t *b0copy = vlib_buffer_copy (vm, b0);
1031           if (PREDICT_FALSE (b0copy == NULL))
1032             goto ship_and_ret;
1033           *to_next++ = vlib_get_buffer_index (vm, b0copy);
1034           n_to_send--;
1035           n_sent++;
1036         }
1037
1038       /* n_to_send is guaranteed to equal 1 here */
1039       if (burst > 0)
1040         {
1041           /* not the last burst, so still make a copy for the last buffer */
1042           vlib_buffer_t *b0copy = vlib_buffer_copy (vm, b0);
1043           if (PREDICT_FALSE (b0copy == NULL))
1044             goto ship_and_ret;
1045           n_to_send--;
1046           *to_next++ = vlib_get_buffer_index (vm, b0copy);
1047         }
1048       else
1049         {
1050           /* put the original buffer as the last one of an error-free run */
1051           *to_next++ = vlib_get_buffer_index (vm, b0);
1052         }
1053       vlib_put_frame_to_node (vm, lookup_node_index, f);
1054       n_sent += f->n_vectors;
1055     }
1056   return n_sent;
1057   /*
1058    * We reach here in case we already enqueued one or more buffers
1059    * and maybe one or more frames but could not make more copies.
1060    * There is an outstanding frame - so ship it and return.
1061    * Caller will have to free the b0 in this case, since
1062    * we did not enqueue it here yet.
1063    */
1064 ship_and_ret:
1065   ASSERT (n_to_send <= f->n_vectors);
1066   f->n_vectors -= n_to_send;
1067   n_sent += f->n_vectors;
1068   vlib_put_frame_to_node (vm, lookup_node_index, f);
1069   return n_sent;
1070 }
1071
1072
1073 /*
1074  * An address-family agnostic ping send function.
1075  */
1076
1077 #define ERROR_OUT(e) do { err = e; goto done; } while (0)
1078
1079 static send_ip46_ping_result_t
1080 send_ip46_ping (vlib_main_t * vm,
1081                 u32 table_id,
1082                 ip46_address_t * pa46,
1083                 u32 sw_if_index,
1084                 u16 seq_host, u16 id_host, u16 data_len, u32 burst,
1085                 u8 verbose, int is_ip6)
1086 {
1087   int err = SEND_PING_OK;
1088   u32 bi0 = 0;
1089   int n_buf0 = 0;
1090   vlib_buffer_t *b0;
1091
1092   n_buf0 = vlib_buffer_alloc (vm, &bi0, 1);
1093   if (n_buf0 < 1)
1094     ERROR_OUT (SEND_PING_ALLOC_FAIL);
1095
1096   b0 = vlib_get_buffer (vm, bi0);
1097
1098   /*
1099    * if the user did not provide a source interface,
1100    * perform a resolution and use an interface
1101    * via which it succeeds.
1102    */
1103   u32 fib_index;
1104   if (~0 == sw_if_index)
1105     {
1106       fib_index = ip46_fib_index_from_table_id (table_id, is_ip6);
1107       sw_if_index = ip46_get_resolving_interface (fib_index, pa46, is_ip6);
1108     }
1109   else
1110     fib_index =
1111       ip46_fib_table_get_index_for_sw_if_index (sw_if_index, is_ip6, pa46);
1112
1113   if (~0 == fib_index)
1114     ERROR_OUT (SEND_PING_NO_TABLE);
1115   if (~0 == sw_if_index)
1116     ERROR_OUT (SEND_PING_NO_INTERFACE);
1117
1118   vnet_buffer (b0)->sw_if_index[VLIB_RX] = sw_if_index;
1119   vnet_buffer (b0)->sw_if_index[VLIB_TX] = fib_index;
1120
1121   int l4_header_offset = ip46_fill_l3_header (pa46, b0, is_ip6);
1122
1123   /* set the src address in the buffer */
1124   if (!ip46_set_src_address (sw_if_index, b0, is_ip6))
1125     ERROR_OUT (SEND_PING_NO_SRC_ADDRESS);
1126   if (verbose)
1127     ip46_print_buffer_src_address (vm, b0, is_ip6);
1128
1129   data_len =
1130     ip46_fill_icmp_request_at (vm, l4_header_offset, seq_host, id_host,
1131                                data_len, b0, is_ip6);
1132
1133   ip46_fix_len_and_csum (vm, l4_header_offset, data_len, b0, is_ip6);
1134
1135   u32 node_index = ip6_lookup_node.index;
1136   if (is_ip6)
1137     {
1138       if (pa46->ip6.as_u32[0] == clib_host_to_net_u32 (0xff020000))
1139         {
1140           node_index = ip6_rewrite_mcast_node.index;
1141           vnet_buffer (b0)->sw_if_index[VLIB_RX] = sw_if_index;
1142           vnet_buffer (b0)->sw_if_index[VLIB_TX] = sw_if_index;
1143           vnet_buffer (b0)->ip.adj_index[VLIB_TX] =
1144             ip6_link_get_mcast_adj (sw_if_index);
1145         }
1146     }
1147   else
1148     {
1149       node_index = ip4_lookup_node.index;
1150     }
1151   int n_sent = ip46_enqueue_packet (vm, b0, burst, node_index);
1152   if (n_sent < burst)
1153     err = SEND_PING_NO_BUFFERS;
1154
1155 done:
1156   if (err != SEND_PING_OK)
1157     {
1158       if (n_buf0 > 0)
1159         vlib_buffer_free (vm, &bi0, 1);
1160     }
1161   return err;
1162 }
1163
1164 send_ip46_ping_result_t
1165 send_ip6_ping (vlib_main_t *vm, u32 table_id, ip6_address_t *pa6,
1166                u32 sw_if_index, u16 seq_host, u16 id_host, u16 data_len,
1167                u32 burst, u8 verbose)
1168 {
1169   ip46_address_t target;
1170   target.ip6 = *pa6;
1171   return send_ip46_ping (vm, table_id, &target, sw_if_index, seq_host,
1172                          id_host, data_len, burst, verbose, 1 /* is_ip6 */ );
1173 }
1174
1175 send_ip46_ping_result_t
1176 send_ip4_ping (vlib_main_t *vm, u32 table_id, ip4_address_t *pa4,
1177                u32 sw_if_index, u16 seq_host, u16 id_host, u16 data_len,
1178                u32 burst, u8 verbose)
1179 {
1180   ip46_address_t target;
1181   ip46_address_set_ip4 (&target, pa4);
1182   return send_ip46_ping (vm, table_id, &target, sw_if_index, seq_host,
1183                          id_host, data_len, burst, verbose, 0 /* is_ip6 */ );
1184 }
1185
1186 static void
1187 print_ip46_icmp_reply (vlib_main_t * vm, u32 bi0, int is_ip6)
1188 {
1189   vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
1190   int l4_offset;
1191   void *paddr;
1192   void *format_addr_func;
1193   u16 payload_length;
1194   u8 ttl;
1195   if (is_ip6)
1196     {
1197       ip6_header_t *ip6 = vlib_buffer_get_current (b0);
1198       paddr = (void *) &ip6->src_address;
1199       format_addr_func = (void *) format_ip6_address;
1200       ttl = ip6->hop_limit;
1201       l4_offset = sizeof (ip6_header_t);        // FIXME - EH processing ?
1202       payload_length = clib_net_to_host_u16 (ip6->payload_length);
1203     }
1204   else
1205     {
1206       ip4_header_t *ip4 = vlib_buffer_get_current (b0);
1207       paddr = (void *) &ip4->src_address;
1208       format_addr_func = (void *) format_ip4_address;
1209       ttl = ip4->ttl;
1210       l4_offset = ip4_header_bytes (ip4);
1211       payload_length =
1212         clib_net_to_host_u16 (ip4->length) + ip4_header_bytes (ip4);
1213     }
1214   icmp46_header_t *icmp = vlib_buffer_get_current (b0) + l4_offset;
1215   icmp46_echo_request_t *icmp_echo = (icmp46_echo_request_t *) (icmp + 1);
1216   u64 *dataplane_ts = (u64 *) & vnet_buffer (b0)->unused[0];
1217
1218   f64 clocks_per_second = ((f64) vm->clib_time.clocks_per_second);
1219   f64 rtt =
1220     ((f64) (*dataplane_ts - icmp_echo->time_sent)) / clocks_per_second;
1221
1222   vlib_cli_output (vm,
1223                    "%d bytes from %U: icmp_seq=%d ttl=%d time=%.4f ms",
1224                    payload_length,
1225                    format_addr_func,
1226                    paddr,
1227                    clib_host_to_net_u16 (icmp_echo->seq), ttl, rtt * 1000.0);
1228 }
1229
1230 /*
1231  * Perform the ping run with the given parameters in the current CLI process.
1232  * Depending on whether pa4 or pa6 is set, runs IPv4 or IPv6 ping.
1233  * The amusing side effect is of course if both are set, then both pings are sent.
1234  * This behavior can be used to ping a dualstack host over IPv4 and IPv6 at once.
1235  */
1236
1237 static void
1238 run_ping_ip46_address (vlib_main_t * vm, u32 table_id, ip4_address_t * pa4,
1239                        ip6_address_t * pa6, u32 sw_if_index,
1240                        f64 ping_interval, u32 ping_repeat, u32 data_len,
1241                        u32 ping_burst, u32 verbose)
1242 {
1243   int i;
1244   uword curr_proc = vlib_current_process (vm);
1245   u32 n_replies = 0;
1246   u32 n_requests = 0;
1247   u16 icmp_id;
1248
1249   static u32 rand_seed = 0;
1250
1251   if (PREDICT_FALSE (!rand_seed))
1252     rand_seed = random_default_seed ();
1253
1254   icmp_id = random_u32 (&rand_seed) & 0xffff;
1255
1256   while (~0 != get_cli_process_id_by_icmp_id_mt (vm, icmp_id))
1257     {
1258       vlib_cli_output (vm, "ICMP ID collision at %d, incrementing", icmp_id);
1259       icmp_id++;
1260     }
1261
1262   set_cli_process_id_by_icmp_id_mt (vm, icmp_id, curr_proc);
1263
1264   for (i = 1; i <= ping_repeat; i++)
1265     {
1266       send_ip46_ping_result_t res = SEND_PING_OK;
1267       f64 sleep_interval;
1268       f64 time_ping_sent = vlib_time_now (vm);
1269       if (pa6)
1270         {
1271           res = send_ip6_ping (vm, table_id,
1272                                pa6, sw_if_index, i, icmp_id,
1273                                data_len, ping_burst, verbose);
1274           if (SEND_PING_OK == res)
1275             n_requests += ping_burst;
1276           else
1277             vlib_cli_output (vm, "Failed: %U", format_ip46_ping_result, res);
1278         }
1279       if (pa4)
1280         {
1281           res = send_ip4_ping (vm, table_id, pa4,
1282                                sw_if_index, i, icmp_id, data_len,
1283                                ping_burst, verbose);
1284           if (SEND_PING_OK == res)
1285             n_requests += ping_burst;
1286           else
1287             vlib_cli_output (vm, "Failed: %U", format_ip46_ping_result, res);
1288         }
1289
1290       /* Collect and print the responses until it is time to send a next ping */
1291
1292       while ((i <= ping_repeat)
1293              &&
1294              ((sleep_interval =
1295                time_ping_sent + ping_interval - vlib_time_now (vm)) > 0.0))
1296         {
1297           uword event_type, *event_data = 0;
1298           vlib_process_wait_for_event_or_clock (vm, sleep_interval);
1299           event_type = vlib_process_get_events (vm, &event_data);
1300           switch (event_type)
1301             {
1302             case ~0:            /* no events => timeout */
1303               break;
1304             case PING_RESPONSE_IP6:
1305               /* fall-through */
1306             case PING_RESPONSE_IP4:
1307               {
1308                 int ii;
1309                 int is_ip6 = (event_type == PING_RESPONSE_IP6);
1310                 for (ii = 0; ii < vec_len (event_data); ii++)
1311                   {
1312                     u32 bi0 = event_data[ii];
1313                     print_ip46_icmp_reply (vm, bi0, is_ip6);
1314                     n_replies++;
1315                     if (0 != bi0)
1316                       vlib_buffer_free (vm, &bi0, 1);
1317                   }
1318               }
1319               break;
1320             case UNIX_CLI_PROCESS_EVENT_READ_READY:
1321             case UNIX_CLI_PROCESS_EVENT_QUIT:
1322               /* someone pressed a key, abort */
1323               vlib_cli_output (vm, "Aborted due to a keypress.");
1324               goto double_break;
1325             }
1326           vec_free (event_data);
1327         }
1328     }
1329 double_break:
1330   vlib_cli_output (vm, "\n");
1331   {
1332     float loss =
1333       (0 ==
1334        n_requests) ? 0 : 100.0 * ((float) n_requests -
1335                                   (float) n_replies) / (float) n_requests;
1336     vlib_cli_output (vm,
1337                      "Statistics: %u sent, %u received, %f%% packet loss\n",
1338                      n_requests, n_replies, loss);
1339     clear_cli_process_id_by_icmp_id_mt (vm, icmp_id);
1340   }
1341 }
1342
1343
1344
1345 static clib_error_t *
1346 ping_ip_address (vlib_main_t * vm,
1347                  unformat_input_t * input, vlib_cli_command_t * cmd)
1348 {
1349   ip4_address_t a4;
1350   ip6_address_t a6;
1351   clib_error_t *error = 0;
1352   u32 ping_repeat = 5;
1353   u32 ping_burst = 1;
1354   u8 ping_ip4, ping_ip6;
1355   vnet_main_t *vnm = vnet_get_main ();
1356   u32 data_len = PING_DEFAULT_DATA_LEN;
1357   u32 verbose = 0;
1358   f64 ping_interval = PING_DEFAULT_INTERVAL;
1359   u32 sw_if_index, table_id;
1360
1361   table_id = 0;
1362   ping_ip4 = ping_ip6 = 0;
1363   sw_if_index = ~0;
1364
1365   if (unformat (input, "%U", unformat_ip4_address, &a4))
1366     {
1367       ping_ip4 = 1;
1368     }
1369   else if (unformat (input, "%U", unformat_ip6_address, &a6))
1370     {
1371       ping_ip6 = 1;
1372     }
1373   else if (unformat (input, "ipv4"))
1374     {
1375       if (unformat (input, "%U", unformat_ip4_address, &a4))
1376         {
1377           ping_ip4 = 1;
1378         }
1379       else
1380         {
1381           error =
1382             clib_error_return (0,
1383                                "expecting IPv4 address but got `%U'",
1384                                format_unformat_error, input);
1385         }
1386     }
1387   else if (unformat (input, "ipv6"))
1388     {
1389       if (unformat (input, "%U", unformat_ip6_address, &a6))
1390         {
1391           ping_ip6 = 1;
1392         }
1393       else
1394         {
1395           error =
1396             clib_error_return (0,
1397                                "expecting IPv6 address but got `%U'",
1398                                format_unformat_error, input);
1399         }
1400     }
1401   else
1402     {
1403       error =
1404         clib_error_return (0,
1405                            "expecting IP4/IP6 address `%U'. Usage: ping <addr> [source <intf>] [size <datasz>] [repeat <count>] [verbose]",
1406                            format_unformat_error, input);
1407       goto done;
1408     }
1409
1410   /* allow for the second AF in the same ping */
1411   if (!ping_ip4 && (unformat (input, "ipv4")))
1412     {
1413       if (unformat (input, "%U", unformat_ip4_address, &a4))
1414         {
1415           ping_ip4 = 1;
1416         }
1417     }
1418   else if (!ping_ip6 && (unformat (input, "ipv6")))
1419     {
1420       if (unformat (input, "%U", unformat_ip6_address, &a6))
1421         {
1422           ping_ip6 = 1;
1423         }
1424     }
1425
1426   /* parse the rest of the parameters  in a cycle */
1427   while (!unformat_eof (input, NULL))
1428     {
1429       if (unformat (input, "source"))
1430         {
1431           if (!unformat_user
1432               (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
1433             {
1434               error =
1435                 clib_error_return (0,
1436                                    "unknown interface `%U'",
1437                                    format_unformat_error, input);
1438               goto done;
1439             }
1440         }
1441       else if (unformat (input, "size"))
1442         {
1443           if (!unformat (input, "%u", &data_len))
1444             {
1445               error =
1446                 clib_error_return (0,
1447                                    "expecting size but got `%U'",
1448                                    format_unformat_error, input);
1449               goto done;
1450             }
1451           if (data_len > PING_MAXIMUM_DATA_SIZE)
1452             {
1453               error =
1454                 clib_error_return (0,
1455                                    "%d is bigger than maximum allowed payload size %d",
1456                                    data_len, PING_MAXIMUM_DATA_SIZE);
1457               goto done;
1458             }
1459         }
1460       else if (unformat (input, "table-id"))
1461         {
1462           if (!unformat (input, "%u", &table_id))
1463             {
1464               error =
1465                 clib_error_return (0,
1466                                    "expecting table-id but got `%U'",
1467                                    format_unformat_error, input);
1468               goto done;
1469             }
1470         }
1471       else if (unformat (input, "interval"))
1472         {
1473           if (!unformat (input, "%f", &ping_interval))
1474             {
1475               error =
1476                 clib_error_return (0,
1477                                    "expecting interval (floating point number) got `%U'",
1478                                    format_unformat_error, input);
1479               goto done;
1480             }
1481         }
1482       else if (unformat (input, "repeat"))
1483         {
1484           if (!unformat (input, "%u", &ping_repeat))
1485             {
1486               error =
1487                 clib_error_return (0,
1488                                    "expecting repeat count but got `%U'",
1489                                    format_unformat_error, input);
1490               goto done;
1491             }
1492         }
1493       else if (unformat (input, "burst"))
1494         {
1495           if (!unformat (input, "%u", &ping_burst))
1496             {
1497               error =
1498                 clib_error_return (0,
1499                                    "expecting burst count but got `%U'",
1500                                    format_unformat_error, input);
1501               goto done;
1502             }
1503         }
1504       else if (unformat (input, "verbose"))
1505         {
1506           verbose = 1;
1507         }
1508       else
1509         {
1510           error = clib_error_return (0, "unknown input `%U'",
1511                                      format_unformat_error, input);
1512           goto done;
1513         }
1514     }
1515
1516 /*
1517  * Operationally, one won't (and shouldn't) need to send more than a frame worth of pings.
1518  * But it may be handy during the debugging.
1519  */
1520
1521 #ifdef CLIB_DEBUG
1522 #define MAX_PING_BURST (10*VLIB_FRAME_SIZE)
1523 #else
1524 #define MAX_PING_BURST (VLIB_FRAME_SIZE)
1525 #endif
1526
1527   if (ping_burst < 1 || ping_burst > MAX_PING_BURST)
1528     return clib_error_return (0, "burst size must be between 1 and %u",
1529                               MAX_PING_BURST);
1530
1531   run_ping_ip46_address (vm, table_id, ping_ip4 ? &a4 : NULL,
1532                          ping_ip6 ? &a6 : NULL, sw_if_index, ping_interval,
1533                          ping_repeat, data_len, ping_burst, verbose);
1534 done:
1535   return error;
1536 }
1537
1538 /*?
1539  * This command sends an ICMP ECHO_REQUEST to network hosts. The address
1540  * can be an IPv4 or IPv6 address (or both at the same time).
1541  *
1542  * @cliexpar
1543  * @parblock
1544  * Example of how ping an IPv4 address:
1545  * @cliexstart{ping 172.16.1.2 source GigabitEthernet2/0/0 repeat 2}
1546  * 64 bytes from 172.16.1.2: icmp_seq=1 ttl=64 time=.1090 ms
1547  * 64 bytes from 172.16.1.2: icmp_seq=2 ttl=64 time=.0914 ms
1548  *
1549  * Statistics: 2 sent, 2 received, 0% packet loss
1550  * @cliexend
1551  *
1552  * Example of how ping both an IPv4 address and IPv6 address at the same time:
1553  * @cliexstart{ping 172.16.1.2 ipv6 fe80::24a5:f6ff:fe9c:3a36 source GigabitEthernet2/0/0 repeat 2 verbose}
1554  * Adjacency index: 10, sw_if_index: 1
1555  * Adj: ip6-discover-neighbor
1556  * Adj Interface: 0
1557  * Forced set interface: 1
1558  * Adjacency index: 0, sw_if_index: 4294967295
1559  * Adj: ip4-miss
1560  * Adj Interface: 0
1561  * Forced set interface: 1
1562  * Source address: 172.16.1.1
1563  * 64 bytes from 172.16.1.2: icmp_seq=1 ttl=64 time=.1899 ms
1564  * Adjacency index: 10, sw_if_index: 1
1565  * Adj: ip6-discover-neighbor
1566  * Adj Interface: 0
1567  * Forced set interface: 1
1568  * Adjacency index: 0, sw_if_index: 4294967295
1569  * Adj: ip4-miss
1570  * Adj Interface: 0
1571  * Forced set interface: 1
1572  * Source address: 172.16.1.1
1573  * 64 bytes from 172.16.1.2: icmp_seq=2 ttl=64 time=.0910 ms
1574  *
1575  * Statistics: 4 sent, 2 received, 50% packet loss
1576  * @cliexend
1577  * @endparblock
1578 ?*/
1579 VLIB_CLI_COMMAND (ping_command, static) =
1580 {
1581   .path = "ping",
1582   .function = ping_ip_address,
1583   .short_help = "ping {<ip-addr> | ipv4 <ip4-addr> | ipv6 <ip6-addr>}"
1584   " [ipv4 <ip4-addr> | ipv6 <ip6-addr>] [source <interface>]"
1585   " [size <pktsize:60>] [interval <sec:1>] [repeat <cnt:5>] [table-id <id:0>]"
1586   " [burst <count:1>] [verbose]",
1587   .is_mp_safe = 1,
1588 };
1589
1590 static clib_error_t *
1591 ping_cli_init (vlib_main_t * vm)
1592 {
1593   vlib_thread_main_t *tm = vlib_get_thread_main ();
1594   ping_main_t *pm = &ping_main;
1595
1596   pm->ip6_main = &ip6_main;
1597   pm->ip4_main = &ip4_main;
1598   icmp6_register_type (vm, ICMP6_echo_reply, ip6_icmp_echo_reply_node.index);
1599   ip4_icmp_register_type (vm, ICMP4_echo_reply,
1600                           ip4_icmp_echo_reply_node.index);
1601   if (tm->n_vlib_mains > 1)
1602     clib_spinlock_init (&pm->ping_run_check_lock);
1603
1604   ip4_icmp_register_type (vm, ICMP4_echo_request,
1605                           ip4_icmp_echo_request_node.index);
1606   icmp6_register_type (vm, ICMP6_echo_request,
1607                        ip6_icmp_echo_request_node.index);
1608
1609   ping_plugin_api_hookup (vm);
1610
1611   return 0;
1612 }
1613
1614 VLIB_INIT_FUNCTION (ping_cli_init);
1615
1616 VLIB_PLUGIN_REGISTER () = {
1617     .version = VPP_BUILD_VER,
1618     .description = "Ping (ping)",
1619 };
1620
1621 /*
1622  * fd.io coding-style-patch-verification: ON
1623  *
1624  * Local Variables:
1625  * eval: (c-set-style "gnu")
1626  * End:
1627  */