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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
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 #include <vlib/vlib.h>
23 ping_main_t ping_main;
27 * @brief IPv4 and IPv6 ICMP Ping.
29 * This file contains code to suppport IPv4 or IPv6 ICMP ECHO_REQUEST to
36 format_icmp_echo_trace (u8 * s, va_list * va)
38 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
39 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
40 icmp_echo_trace_t *t = va_arg (*va, icmp_echo_trace_t *);
42 s = format (s, "ICMP echo id %d seq %d%s",
43 clib_net_to_host_u16 (t->id),
44 clib_net_to_host_u16 (t->seq), t->bound ? "" : " (unknown)");
50 format_ip46_ping_result (u8 * s, va_list * args)
52 send_ip46_ping_result_t res = va_arg (*args, send_ip46_ping_result_t);
56 #define _(v, n) case SEND_PING_##v: s = format(s, "%s", n);
57 foreach_ip46_ping_result
65 * If we can find the ping run by an ICMP ID, then we send the signal
66 * to the CLI process referenced by that ping run, alongside with
67 * a freshly made copy of the packet.
68 * I opted for a packet copy to keep the main packet processing path
69 * the same as for all the other nodes.
74 signal_ip46_icmp_reply_event (u8 event_type, vlib_buffer_t * b0)
76 ping_main_t *pm = &ping_main;
82 case PING_RESPONSE_IP4:
84 icmp4_echo_request_header_t *h0 = vlib_buffer_get_current (b0);
85 net_icmp_id = h0->icmp_echo.id;
88 case PING_RESPONSE_IP6:
90 icmp6_echo_request_header_t *h0 = vlib_buffer_get_current (b0);
91 net_icmp_id = h0->icmp_echo.id;
98 uword *p = hash_get (pm->ping_run_by_icmp_id,
99 clib_net_to_host_u16 (net_icmp_id));
103 ping_run_t *pr = vec_elt_at_index (pm->ping_runs, p[0]);
104 vlib_main_t *vm = vlib_mains[pr->cli_thread_index];
105 if (vlib_buffer_alloc (vm, &bi0_copy, 1) == 1)
107 void *dst = vlib_buffer_get_current (vlib_get_buffer (vm,
109 clib_memcpy (dst, vlib_buffer_get_current (b0), b0->current_length);
111 /* If buffer_alloc failed, bi0_copy == 0 - just signaling an event. */
112 f64 nowts = vlib_time_now (vm);
113 /* Pass the timestamp to the cli_process thanks to the vnet_buffer unused metadata field */
114 clib_memcpy (vnet_buffer
116 (vm, bi0_copy))->unused, &nowts, sizeof (nowts));
117 vlib_process_signal_event_mt (vm, pr->cli_process_id, event_type, bi0_copy);
122 * Process ICMPv6 echo replies
125 ip6_icmp_echo_reply_node_fn (vlib_main_t * vm,
126 vlib_node_runtime_t * node, vlib_frame_t * frame)
128 u32 n_left_from, *from;
130 from = vlib_frame_vector_args (frame); /* array of buffer indices */
131 n_left_from = frame->n_vectors; /* number of buffer indices */
133 while (n_left_from > 0)
140 b0 = vlib_get_buffer (vm, bi0);
142 next0 = signal_ip46_icmp_reply_event (PING_RESPONSE_IP6, b0) ?
143 ICMP6_ECHO_REPLY_NEXT_DROP : ICMP6_ECHO_REPLY_NEXT_PUNT;
145 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
147 icmp6_echo_request_header_t *h0 = vlib_buffer_get_current (b0);
148 icmp_echo_trace_t *tr = vlib_add_trace (vm, node, b0, sizeof (*tr));
149 tr->id = h0->icmp_echo.id;
150 tr->seq = h0->icmp_echo.seq;
151 tr->bound = (next0 == ICMP6_ECHO_REPLY_NEXT_DROP);
154 /* push this pkt to the next graph node */
155 vlib_set_next_frame_buffer (vm, node, next0, bi0);
161 return frame->n_vectors;
165 VLIB_REGISTER_NODE (ip6_icmp_echo_reply_node, static) =
167 .function = ip6_icmp_echo_reply_node_fn,
168 .name = "ip6-icmp-echo-reply",
169 .vector_size = sizeof (u32),
170 .format_trace = format_icmp_echo_trace,
171 .n_next_nodes = ICMP6_ECHO_REPLY_N_NEXT,
173 [ICMP6_ECHO_REPLY_NEXT_DROP] = "ip6-drop",
174 [ICMP6_ECHO_REPLY_NEXT_PUNT] = "ip6-punt",
180 * Process ICMPv4 echo replies
183 ip4_icmp_echo_reply_node_fn (vlib_main_t * vm,
184 vlib_node_runtime_t * node, vlib_frame_t * frame)
186 u32 n_left_from, *from;
188 from = vlib_frame_vector_args (frame); /* array of buffer indices */
189 n_left_from = frame->n_vectors; /* number of buffer indices */
191 while (n_left_from > 0)
198 b0 = vlib_get_buffer (vm, bi0);
200 next0 = signal_ip46_icmp_reply_event (PING_RESPONSE_IP4, b0) ?
201 ICMP4_ECHO_REPLY_NEXT_DROP : ICMP4_ECHO_REPLY_NEXT_PUNT;
203 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
205 icmp4_echo_request_header_t *h0 = vlib_buffer_get_current (b0);
206 icmp_echo_trace_t *tr = vlib_add_trace (vm, node, b0, sizeof (*tr));
207 tr->id = h0->icmp_echo.id;
208 tr->seq = h0->icmp_echo.seq;
209 tr->bound = (next0 == ICMP4_ECHO_REPLY_NEXT_DROP);
212 /* push this pkt to the next graph node */
213 vlib_set_next_frame_buffer (vm, node, next0, bi0);
219 return frame->n_vectors;
223 VLIB_REGISTER_NODE (ip4_icmp_echo_reply_node, static) =
225 .function = ip4_icmp_echo_reply_node_fn,
226 .name = "ip4-icmp-echo-reply",
227 .vector_size = sizeof (u32),
228 .format_trace = format_icmp_echo_trace,
229 .n_next_nodes = ICMP4_ECHO_REPLY_N_NEXT,
231 [ICMP4_ECHO_REPLY_NEXT_DROP] = "ip4-drop",
232 [ICMP4_ECHO_REPLY_NEXT_PUNT] = "ip4-punt",
237 /* Fill in the ICMP ECHO structure, return the safety-checked and possibly shrunk data_len */
239 init_icmp46_echo_request (icmp46_echo_request_t * icmp46_echo,
240 u16 seq_host, u16 id_host, u16 data_len)
243 icmp46_echo->seq = clib_host_to_net_u16 (seq_host);
244 icmp46_echo->id = clib_host_to_net_u16 (id_host);
246 if (data_len > PING_MAXIMUM_DATA_SIZE)
247 data_len = PING_MAXIMUM_DATA_SIZE;
248 for (i = 0; i < data_len; i++)
249 icmp46_echo->data[i] = i % 256;
253 static send_ip46_ping_result_t
254 send_ip6_ping (vlib_main_t * vm, ip6_main_t * im,
255 u32 table_id, ip6_address_t * pa6,
256 u32 sw_if_index, u16 seq_host, u16 id_host, u16 data_len,
257 u32 burst, u8 verbose)
259 icmp6_echo_request_header_t *h0;
261 int bogus_length = 0;
265 vlib_buffer_free_list_t *fl;
267 if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
268 return SEND_PING_ALLOC_FAIL;
270 p0 = vlib_get_buffer (vm, bi0);
271 fl = vlib_buffer_get_free_list (vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
272 vlib_buffer_init_for_free_list (p0, fl);
273 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (p0);
276 * if the user did not provide a source interface, use the any interface
277 * that the destination resolves via.
279 if (~0 == sw_if_index)
281 fib_node_index_t fib_entry_index;
284 fib_index = ip6_fib_index_from_table_id (table_id);
288 vlib_buffer_free (vm, &bi0, 1);
289 return SEND_PING_NO_TABLE;
292 fib_entry_index = ip6_fib_table_lookup (fib_index, pa6, 128);
293 sw_if_index = fib_entry_get_resolving_interface (fib_entry_index);
295 * Set the TX interface to force ip-lookup to use its table ID
297 vnet_buffer (p0)->sw_if_index[VLIB_TX] = fib_index;
302 * force an IP lookup in the table bound to the user's chosen
305 vnet_buffer (p0)->sw_if_index[VLIB_TX] =
306 ip6_fib_table_get_index_for_sw_if_index (sw_if_index);
309 if (~0 == sw_if_index)
311 vlib_buffer_free (vm, &bi0, 1);
312 return SEND_PING_NO_INTERFACE;
315 vnet_buffer (p0)->sw_if_index[VLIB_RX] = sw_if_index;
317 h0 = vlib_buffer_get_current (p0);
319 /* Fill in ip6 header fields */
320 h0->ip6.ip_version_traffic_class_and_flow_label =
321 clib_host_to_net_u32 (0x6 << 28);
322 h0->ip6.payload_length = 0; /* Set below */
323 h0->ip6.protocol = IP_PROTOCOL_ICMP6;
324 h0->ip6.hop_limit = 255;
325 h0->ip6.dst_address = *pa6;
326 h0->ip6.src_address = *pa6;
328 /* Fill in the correct source now */
329 if (!ip6_src_address_for_packet (&im->lookup_main,
331 &h0->ip6.dst_address,
332 &h0->ip6.src_address))
334 vlib_buffer_free (vm, &bi0, 1);
335 return SEND_PING_NO_SRC_ADDRESS;
338 /* Fill in icmp fields */
339 h0->icmp.type = ICMP6_echo_request;
341 h0->icmp.checksum = 0;
344 init_icmp46_echo_request (&h0->icmp_echo, seq_host, id_host, data_len);
345 h0->icmp_echo.time_sent = vlib_time_now (vm);
347 /* Fix up the lengths */
348 h0->ip6.payload_length =
349 clib_host_to_net_u16 (data_len + sizeof (icmp46_header_t));
351 p0->current_length = clib_net_to_host_u16 (h0->ip6.payload_length) +
352 STRUCT_OFFSET_OF (icmp6_echo_request_header_t, icmp);
354 /* Calculate the ICMP checksum */
355 h0->icmp.checksum = 0;
357 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h0->ip6, &bogus_length);
359 /* Enqueue the packet right now */
360 f = vlib_get_frame_to_node (vm, ip6_lookup_node.index);
361 to_next = vlib_frame_vector_args (f);
364 ASSERT (burst <= VLIB_FRAME_SIZE);
365 f->n_vectors = burst;
368 vlib_buffer_t *c0 = vlib_buffer_copy (vm, p0);
370 to_next[0] = vlib_get_buffer_index (vm, c0);
372 vlib_put_frame_to_node (vm, ip6_lookup_node.index, f);
377 static send_ip46_ping_result_t
378 send_ip4_ping (vlib_main_t * vm,
383 u16 seq_host, u16 id_host, u16 data_len, u32 burst, u8 verbose)
385 icmp4_echo_request_header_t *h0;
387 ip_lookup_main_t *lm = &im->lookup_main;
392 vlib_buffer_free_list_t *fl;
394 if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
395 return SEND_PING_ALLOC_FAIL;
397 p0 = vlib_get_buffer (vm, bi0);
398 fl = vlib_buffer_get_free_list (vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
399 vlib_buffer_init_for_free_list (p0, fl);
400 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (p0);
403 * if the user did not provide a source interface, use the any interface
404 * that the destination resolves via.
406 if (~0 == sw_if_index)
408 fib_node_index_t fib_entry_index;
411 fib_index = ip4_fib_index_from_table_id (table_id);
415 vlib_buffer_free (vm, &bi0, 1);
416 return SEND_PING_NO_TABLE;
420 ip4_fib_table_lookup (ip4_fib_get (fib_index), pa4, 32);
421 sw_if_index = fib_entry_get_resolving_interface (fib_entry_index);
423 * Set the TX interface to force ip-lookup to use the user's table ID
425 vnet_buffer (p0)->sw_if_index[VLIB_TX] = fib_index;
430 * force an IP lookup in the table bound to the user's chosen
433 vnet_buffer (p0)->sw_if_index[VLIB_TX] =
434 ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
437 if (~0 == sw_if_index)
439 vlib_buffer_free (vm, &bi0, 1);
440 return SEND_PING_NO_INTERFACE;
443 vnet_buffer (p0)->sw_if_index[VLIB_RX] = sw_if_index;
445 h0 = vlib_buffer_get_current (p0);
447 /* Fill in ip4 header fields */
448 h0->ip4.checksum = 0;
449 h0->ip4.ip_version_and_header_length = 0x45;
451 h0->ip4.length = 0; /* Set below */
452 h0->ip4.fragment_id = 0;
453 h0->ip4.flags_and_fragment_offset = 0;
455 h0->ip4.protocol = IP_PROTOCOL_ICMP;
456 h0->ip4.dst_address = *pa4;
457 h0->ip4.src_address = *pa4;
459 /* Fill in the correct source now */
460 if_add_index0 = lm->if_address_pool_index_by_sw_if_index[sw_if_index];
461 if (PREDICT_TRUE (if_add_index0 != ~0))
463 ip_interface_address_t *if_add =
464 pool_elt_at_index (lm->if_address_pool, if_add_index0);
465 ip4_address_t *if_ip = ip_interface_address_get_address (lm, if_add);
466 h0->ip4.src_address = *if_ip;
469 vlib_cli_output (vm, "Source address: %U",
470 format_ip4_address, &h0->ip4.src_address);
474 /* Fill in icmp fields */
475 h0->icmp.type = ICMP4_echo_request;
477 h0->icmp.checksum = 0;
480 init_icmp46_echo_request (&h0->icmp_echo, seq_host, id_host, data_len);
481 h0->icmp_echo.time_sent = vlib_time_now (vm);
483 /* Fix up the lengths */
485 clib_host_to_net_u16 (data_len + sizeof (icmp46_header_t) +
486 sizeof (ip4_header_t));
488 p0->current_length = clib_net_to_host_u16 (h0->ip4.length);
490 /* Calculate the IP and ICMP checksums */
491 h0->ip4.checksum = ip4_header_checksum (&(h0->ip4));
493 ~ip_csum_fold (ip_incremental_checksum (0, &(h0->icmp),
495 sizeof (ip4_header_t)));
497 /* Enqueue the packet right now */
498 f = vlib_get_frame_to_node (vm, ip4_lookup_node.index);
499 to_next = vlib_frame_vector_args (f);
502 ASSERT (burst <= VLIB_FRAME_SIZE);
503 f->n_vectors = burst;
506 vlib_buffer_t *c0 = vlib_buffer_copy (vm, p0);
508 to_next[0] = vlib_get_buffer_index (vm, c0);
510 vlib_put_frame_to_node (vm, ip4_lookup_node.index, f);
517 print_ip6_icmp_reply (vlib_main_t * vm, u32 bi0)
519 vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
520 icmp6_echo_request_header_t *h0 = vlib_buffer_get_current (b0);
522 clib_memcpy (&rtt, vnet_buffer (b0)->unused, sizeof (rtt));
523 rtt -= h0->icmp_echo.time_sent;
525 "%d bytes from %U: icmp_seq=%d ttl=%d time=%.4f ms",
526 clib_host_to_net_u16 (h0->ip6.payload_length),
528 &h0->ip6.src_address,
529 clib_host_to_net_u16 (h0->icmp_echo.seq),
530 h0->ip6.hop_limit, rtt * 1000.0);
534 print_ip4_icmp_reply (vlib_main_t * vm, u32 bi0)
536 vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
537 icmp4_echo_request_header_t *h0 = vlib_buffer_get_current (b0);
539 clib_memcpy (&rtt, vnet_buffer (b0)->unused, sizeof (rtt));
540 rtt -= h0->icmp_echo.time_sent;
542 clib_host_to_net_u16 (h0->ip4.length) -
543 (4 * (0xF & h0->ip4.ip_version_and_header_length));
546 "%d bytes from %U: icmp_seq=%d ttl=%d time=%.4f ms",
549 &h0->ip4.src_address,
550 clib_host_to_net_u16 (h0->icmp_echo.seq),
551 h0->ip4.ttl, rtt * 1000.0);
556 * Perform the ping run with the given parameters in the current CLI process.
557 * Depending on whether pa4 or pa6 is set, runs IPv4 or IPv6 ping.
558 * The amusing side effect is of course if both are set, then both pings are sent.
559 * This behavior can be used to ping a dualstack host over IPv4 and IPv6 at once.
563 run_ping_ip46_address (vlib_main_t * vm, u32 table_id, ip4_address_t * pa4,
564 ip6_address_t * pa6, u32 sw_if_index,
565 f64 ping_interval, u32 ping_repeat, u32 data_len,
566 u32 ping_burst, u32 verbose)
569 ping_main_t *pm = &ping_main;
570 uword curr_proc = vlib_current_process (vm);
574 u32 ping_run_index = 0;
577 static u32 rand_seed = 0;
579 if (PREDICT_FALSE (!rand_seed))
580 rand_seed = random_default_seed ();
582 icmp_id = random_u32 (&rand_seed) & 0xffff;
584 while (hash_get (pm->ping_run_by_icmp_id, icmp_id))
586 vlib_cli_output (vm, "ICMP ID collision at %d, incrementing", icmp_id);
589 pool_get (pm->ping_runs, pr);
590 ping_run_index = pr - pm->ping_runs;
591 pr->cli_process_id = curr_proc;
592 pr->cli_thread_index = vm->thread_index;
593 pr->icmp_id = icmp_id;
594 hash_set (pm->ping_run_by_icmp_id, icmp_id, ping_run_index);
595 for (i = 1; i <= ping_repeat; i++)
597 send_ip46_ping_result_t res = SEND_PING_OK;
599 f64 time_ping_sent = vlib_time_now (vm);
600 /* Reset pr: running ping in other process could have changed pm->ping_runs */
601 pr = vec_elt_at_index (pm->ping_runs, ping_run_index);
605 res = send_ip6_ping (vm, ping_main.ip6_main, table_id,
606 pa6, sw_if_index, i, icmp_id,
607 data_len, ping_burst, verbose);
611 res = send_ip4_ping (vm, ping_main.ip4_main, table_id, pa4,
612 sw_if_index, i, icmp_id, data_len,
613 ping_burst, verbose);
615 if (SEND_PING_OK == res)
617 n_requests += ping_burst;
620 vlib_cli_output (vm, "Failed: %U", format_ip46_ping_result, res);
621 while ((i <= ping_repeat)
624 time_ping_sent + ping_interval - vlib_time_now (vm)) > 0.0))
626 uword event_type, *event_data = 0;
627 vlib_process_wait_for_event_or_clock (vm, sleep_interval);
628 event_type = vlib_process_get_events (vm, &event_data);
631 case ~0: /* no events => timeout */
633 case PING_RESPONSE_IP6:
636 for (i = 0; i < vec_len (event_data); i++)
638 u32 bi0 = event_data[i];
639 print_ip6_icmp_reply (vm, bi0);
643 vlib_buffer_free (vm, &bi0, 1);
648 case PING_RESPONSE_IP4:
651 for (i = 0; i < vec_len (event_data); i++)
653 u32 bi0 = event_data[i];
654 print_ip4_icmp_reply (vm, bi0);
658 vlib_buffer_free (vm, &bi0, 1);
664 /* someone pressed a key, abort */
665 vlib_cli_output (vm, "Aborted due to a keypress.");
669 vec_free (event_data);
672 vlib_cli_output (vm, "\n");
676 n_requests) ? 0 : 100.0 * ((float) n_requests -
677 (float) n_replies) / (float) n_requests;
679 "Statistics: %u sent, %u received, %f%% packet loss\n",
680 n_requests, n_replies, loss);
681 /* Reset pr: running ping in other process could have changed pm->ping_runs */
682 pr = vec_elt_at_index (pm->ping_runs, ping_run_index);
683 hash_unset (pm->ping_run_by_icmp_id, icmp_id);
684 pool_put (pm->ping_runs, pr);
692 static clib_error_t *
693 ping_ip_address (vlib_main_t * vm,
694 unformat_input_t * input, vlib_cli_command_t * cmd)
698 clib_error_t *error = 0;
701 u8 ping_ip4, ping_ip6;
702 vnet_main_t *vnm = vnet_get_main ();
703 u32 data_len = PING_DEFAULT_DATA_LEN;
705 f64 ping_interval = PING_DEFAULT_INTERVAL;
706 u32 sw_if_index, table_id;
709 ping_ip4 = ping_ip6 = 0;
712 if (unformat (input, "%U", unformat_ip4_address, &a4))
716 else if (unformat (input, "%U", unformat_ip6_address, &a6))
720 else if (unformat (input, "ipv4"))
722 if (unformat (input, "%U", unformat_ip4_address, &a4))
729 clib_error_return (0,
730 "expecting IPv4 address but got `%U'",
731 format_unformat_error, input);
734 else if (unformat (input, "ipv6"))
736 if (unformat (input, "%U", unformat_ip6_address, &a6))
743 clib_error_return (0,
744 "expecting IPv6 address but got `%U'",
745 format_unformat_error, input);
751 clib_error_return (0,
752 "expecting IP4/IP6 address `%U'. Usage: ping <addr> [source <intf>] [size <datasz>] [repeat <count>] [verbose]",
753 format_unformat_error, input);
757 /* allow for the second AF in the same ping */
758 if (!ping_ip4 && (unformat (input, "ipv4")))
760 if (unformat (input, "%U", unformat_ip4_address, &a4))
765 else if (!ping_ip6 && (unformat (input, "ipv6")))
767 if (unformat (input, "%U", unformat_ip6_address, &a6))
773 /* parse the rest of the parameters in a cycle */
774 while (!unformat_eof (input, NULL))
776 if (unformat (input, "source"))
779 (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
782 clib_error_return (0,
783 "unknown interface `%U'",
784 format_unformat_error, input);
788 else if (unformat (input, "size"))
790 if (!unformat (input, "%u", &data_len))
793 clib_error_return (0,
794 "expecting size but got `%U'",
795 format_unformat_error, input);
798 if (data_len > PING_MAXIMUM_DATA_SIZE)
801 clib_error_return (0,
802 "%d is bigger than maximum allowed payload size %d",
803 data_len, PING_MAXIMUM_DATA_SIZE);
807 else if (unformat (input, "table-id"))
809 if (!unformat (input, "%u", &table_id))
812 clib_error_return (0,
813 "expecting table-id but got `%U'",
814 format_unformat_error, input);
818 else if (unformat (input, "interval"))
820 if (!unformat (input, "%f", &ping_interval))
823 clib_error_return (0,
824 "expecting interval (floating point number) got `%U'",
825 format_unformat_error, input);
829 else if (unformat (input, "repeat"))
831 if (!unformat (input, "%u", &ping_repeat))
834 clib_error_return (0,
835 "expecting repeat count but got `%U'",
836 format_unformat_error, input);
840 else if (unformat (input, "burst"))
842 if (!unformat (input, "%u", &ping_burst))
845 clib_error_return (0,
846 "expecting burst count but got `%U'",
847 format_unformat_error, input);
851 else if (unformat (input, "verbose"))
857 error = clib_error_return (0, "unknown input `%U'",
858 format_unformat_error, input);
863 if (ping_burst < 1 || ping_burst > VLIB_FRAME_SIZE)
864 return clib_error_return (0, "burst size must be between 1 and %u",
867 run_ping_ip46_address (vm, table_id, ping_ip4 ? &a4 : NULL,
868 ping_ip6 ? &a6 : NULL, sw_if_index, ping_interval,
869 ping_repeat, data_len, ping_burst, verbose);
875 * This command sends an ICMP ECHO_REQUEST to network hosts. The address
876 * can be an IPv4 or IPv6 address (or both at the same time).
880 * Example of how ping an IPv4 address:
881 * @cliexstart{ping 172.16.1.2 source GigabitEthernet2/0/0 repeat 2}
882 * 64 bytes from 172.16.1.2: icmp_seq=1 ttl=64 time=.1090 ms
883 * 64 bytes from 172.16.1.2: icmp_seq=2 ttl=64 time=.0914 ms
885 * Statistics: 2 sent, 2 received, 0% packet loss
888 * Example of how ping both an IPv4 address and IPv6 address at the same time:
889 * @cliexstart{ping 172.16.1.2 ipv6 fe80::24a5:f6ff:fe9c:3a36 source GigabitEthernet2/0/0 repeat 2 verbose}
890 * Adjacency index: 10, sw_if_index: 1
891 * Adj: ip6-discover-neighbor
893 * Forced set interface: 1
894 * Adjacency index: 0, sw_if_index: 4294967295
897 * Forced set interface: 1
898 * Source address: 172.16.1.1
899 * 64 bytes from 172.16.1.2: icmp_seq=1 ttl=64 time=.1899 ms
900 * Adjacency index: 10, sw_if_index: 1
901 * Adj: ip6-discover-neighbor
903 * Forced set interface: 1
904 * Adjacency index: 0, sw_if_index: 4294967295
907 * Forced set interface: 1
908 * Source address: 172.16.1.1
909 * 64 bytes from 172.16.1.2: icmp_seq=2 ttl=64 time=.0910 ms
911 * Statistics: 4 sent, 2 received, 50% packet loss
916 VLIB_CLI_COMMAND (ping_command, static) =
919 .function = ping_ip_address,
920 .short_help = "ping {<ip-addr> | ipv4 <ip4-addr> | ipv6 <ip6-addr>}"
921 " [ipv4 <ip4-addr> | ipv6 <ip6-addr>] [source <interface>]"
922 " [size <pktsize>] [interval <sec>] [repeat <cnt>] [table-id <id>]"
928 static clib_error_t *
929 ping_cli_init (vlib_main_t * vm)
931 ping_main_t *pm = &ping_main;
932 pm->ip6_main = &ip6_main;
933 pm->ip4_main = &ip4_main;
934 icmp6_register_type (vm, ICMP6_echo_reply, ip6_icmp_echo_reply_node.index);
935 ip4_icmp_register_type (vm, ICMP4_echo_reply,
936 ip4_icmp_echo_reply_node.index);
940 VLIB_INIT_FUNCTION (ping_cli_init);
943 * fd.io coding-style-patch-verification: ON
946 * eval: (c-set-style "gnu")