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