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