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