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.
18 #include <vlib/vlib.h>
19 #include <vlib/unix/unix.h>
20 #include <vnet/fib/ip6_fib.h>
21 #include <vnet/fib/ip4_fib.h>
22 #include <vnet/ip/ip_sas.h>
23 #include <vnet/ip/ip6_link.h>
24 #include <vnet/ip/ip6_ll_table.h>
25 #include <vnet/plugin/plugin.h>
26 #include <vpp/app/version.h>
28 #include <vnet/ip/icmp4.h>
29 #include <ping/ping.h>
31 ping_main_t ping_main;
35 * @brief IPv4 and IPv6 ICMP Ping.
37 * This file contains code to support IPv4 or IPv6 ICMP ECHO_REQUEST to
52 format_icmp_echo_trace (u8 * s, va_list * va)
54 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
55 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
56 icmp_echo_trace_t *t = va_arg (*va, icmp_echo_trace_t *);
59 format (s, "ICMP%s echo id %d seq %d", t->is_ip6 ? "6" : "4", t->id,
61 if (t->cli_process_node == PING_CLI_UNKNOWN_NODE)
63 s = format (s, " (unknown)");
67 s = format (s, " send to cli node %d", t->cli_process_node);
75 format_ip46_ping_result (u8 * s, va_list * args)
77 send_ip46_ping_result_t res = va_arg (*args, send_ip46_ping_result_t);
81 #define _(v, n) case SEND_PING_##v: s = format(s, "%s", n);break;
82 foreach_ip46_ping_result
91 * Poor man's get-set-clear functions
92 * for manipulation of icmp_id -> cli_process_id
95 * There should normally be very few (0..1..2) of these
96 * mappings, so the linear search is a good strategy.
98 * Make them thread-safe via a simple spinlock.
103 static_always_inline uword
104 get_cli_process_id_by_icmp_id_mt (vlib_main_t * vm, u16 icmp_id)
106 ping_main_t *pm = &ping_main;
107 uword cli_process_id = PING_CLI_UNKNOWN_NODE;
110 clib_spinlock_lock_if_init (&pm->ping_run_check_lock);
111 vec_foreach (pr, pm->active_ping_runs)
113 if (pr->icmp_id == icmp_id)
115 cli_process_id = pr->cli_process_id;
119 clib_spinlock_unlock_if_init (&pm->ping_run_check_lock);
120 return cli_process_id;
124 static_always_inline void
125 set_cli_process_id_by_icmp_id_mt (vlib_main_t * vm, u16 icmp_id,
126 uword cli_process_id)
128 ping_main_t *pm = &ping_main;
131 clib_spinlock_lock_if_init (&pm->ping_run_check_lock);
132 vec_foreach (pr, pm->active_ping_runs)
134 if (pr->icmp_id == icmp_id)
136 pr->cli_process_id = cli_process_id;
137 goto have_found_and_set;
140 /* no such key yet - add a new one */
141 ping_run_t new_pr = {.icmp_id = icmp_id,.cli_process_id = cli_process_id };
142 vec_add1 (pm->active_ping_runs, new_pr);
144 clib_spinlock_unlock_if_init (&pm->ping_run_check_lock);
148 static_always_inline void
149 clear_cli_process_id_by_icmp_id_mt (vlib_main_t * vm, u16 icmp_id)
151 ping_main_t *pm = &ping_main;
154 clib_spinlock_lock_if_init (&pm->ping_run_check_lock);
155 vec_foreach (pr, pm->active_ping_runs)
157 if (pr->icmp_id == icmp_id)
159 vec_del1 (pm->active_ping_runs, pr - pm->active_ping_runs);
163 clib_spinlock_unlock_if_init (&pm->ping_run_check_lock);
166 static_always_inline int
167 ip46_get_icmp_id_and_seq (vlib_main_t * vm, vlib_buffer_t * b0,
168 u16 * out_icmp_id, u16 * out_icmp_seq, int is_ip6)
173 ip6_header_t *ip6 = vlib_buffer_get_current (b0);
174 if (ip6->protocol != IP_PROTOCOL_ICMP6)
178 l4_offset = sizeof (*ip6); // IPv6 EH
182 ip4_header_t *ip4 = vlib_buffer_get_current (b0);
183 l4_offset = ip4_header_bytes (ip4);
186 icmp46_header_t *icmp46 = vlib_buffer_get_current (b0) + l4_offset;
187 icmp46_echo_request_t *icmp46_echo = (icmp46_echo_request_t *) (icmp46 + 1);
189 *out_icmp_id = clib_net_to_host_u16 (icmp46_echo->id);
190 *out_icmp_seq = clib_net_to_host_u16 (icmp46_echo->seq);
195 * post the buffer to a given cli process node - the caller should forget bi0 after return.
198 static_always_inline void
199 ip46_post_icmp_reply_event (vlib_main_t * vm, uword cli_process_id, u32 bi0,
202 vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
203 u64 nowts = clib_cpu_time_now ();
205 /* Pass the timestamp to the cli_process thanks to the vnet_buffer unused metadata field */
207 /* Camping on unused data... just ensure statically that there is enough space */
208 STATIC_ASSERT (ARRAY_LEN (vnet_buffer (b0)->unused) *
209 sizeof (vnet_buffer (b0)->unused[0]) > sizeof (nowts),
210 "ping reply timestamp fits within remaining space of vnet_buffer unused data");
211 u64 *pnowts = (void *) &vnet_buffer (b0)->unused[0];
214 u32 event_id = is_ip6 ? PING_RESPONSE_IP6 : PING_RESPONSE_IP4;
215 vlib_process_signal_event_mt (vm, cli_process_id, event_id, bi0);
219 static_always_inline void
220 ip46_echo_reply_maybe_trace_buffer (vlib_main_t * vm,
221 vlib_node_runtime_t * node,
222 uword cli_process_id, u16 id, u16 seq,
223 vlib_buffer_t * b0, int is_ip6)
225 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
227 icmp_echo_trace_t *tr = vlib_add_trace (vm, node, b0, sizeof (*tr));
230 tr->cli_process_node = cli_process_id;
236 static_always_inline uword
237 ip46_icmp_echo_reply_inner_node_fn (vlib_main_t * vm,
238 vlib_node_runtime_t * node,
239 vlib_frame_t * frame, int do_trace,
242 u32 n_left_from, *from, *to_next;
243 icmp46_echo_reply_next_t next_index;
245 from = vlib_frame_vector_args (frame);
246 n_left_from = frame->n_vectors;
248 next_index = node->cached_next_index;
250 while (n_left_from > 0)
253 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
255 while (n_left_from > 0 && n_left_to_next > 0)
260 * The buffers (replies) are either posted to the CLI thread
261 * awaiting for them for subsequent analysis and disposal,
262 * or are sent to the punt node.
264 * So the only "next" node is a punt, normally.
266 u32 next0 = ICMP46_ECHO_REPLY_NEXT_PUNT;
269 b0 = vlib_get_buffer (vm, bi0);
275 uword cli_process_id = PING_CLI_UNKNOWN_NODE;
277 if (ip46_get_icmp_id_and_seq (vm, b0, &icmp_id, &icmp_seq, is_ip6))
279 cli_process_id = get_cli_process_id_by_icmp_id_mt (vm, icmp_id);
283 ip46_echo_reply_maybe_trace_buffer (vm, node, cli_process_id,
284 icmp_id, icmp_seq, b0,
287 if (~0 == cli_process_id)
289 /* no outstanding requests for this reply, punt */
290 /* speculatively enqueue b0 to the current next frame */
294 /* verify speculative enqueue, maybe switch current next frame */
295 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
296 to_next, n_left_to_next,
301 /* Post the buffer to CLI thread. It will take care of freeing it. */
302 ip46_post_icmp_reply_event (vm, cli_process_id, bi0, is_ip6);
305 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
307 return frame->n_vectors;
311 * select "with-trace" or "without-trace" codepaths upfront.
313 static_always_inline uword
314 ip46_icmp_echo_reply_outer_node_fn (vlib_main_t * vm,
315 vlib_node_runtime_t * node,
316 vlib_frame_t * frame, int is_ip6)
318 if (node->flags & VLIB_NODE_FLAG_TRACE)
319 return ip46_icmp_echo_reply_inner_node_fn (vm, node, frame,
320 1 /* do_trace */ , is_ip6);
322 return ip46_icmp_echo_reply_inner_node_fn (vm, node, frame,
323 0 /* do_trace */ , is_ip6);
327 ip4_icmp_echo_reply_node_fn (vlib_main_t * vm,
328 vlib_node_runtime_t * node, vlib_frame_t * frame)
330 return ip46_icmp_echo_reply_outer_node_fn (vm, node, frame,
335 ip6_icmp_echo_reply_node_fn (vlib_main_t * vm,
336 vlib_node_runtime_t * node, vlib_frame_t * frame)
338 return ip46_icmp_echo_reply_outer_node_fn (vm, node, frame,
343 VLIB_REGISTER_NODE (ip6_icmp_echo_reply_node, static) =
345 .function = ip6_icmp_echo_reply_node_fn,
346 .name = "ip6-icmp-echo-reply",
347 .vector_size = sizeof (u32),
348 .format_trace = format_icmp_echo_trace,
349 .n_next_nodes = ICMP46_ECHO_REPLY_N_NEXT,
351 [ICMP46_ECHO_REPLY_NEXT_DROP] = "ip6-drop",
352 [ICMP46_ECHO_REPLY_NEXT_PUNT] = "ip6-punt",
356 VLIB_REGISTER_NODE (ip4_icmp_echo_reply_node, static) =
358 .function = ip4_icmp_echo_reply_node_fn,
359 .name = "ip4-icmp-echo-reply",
360 .vector_size = sizeof (u32),
361 .format_trace = format_icmp_echo_trace,
362 .n_next_nodes = ICMP46_ECHO_REPLY_N_NEXT,
364 [ICMP46_ECHO_REPLY_NEXT_DROP] = "ip4-drop",
365 [ICMP46_ECHO_REPLY_NEXT_PUNT] = "ip4-punt",
371 ip4_icmp_echo_request (vlib_main_t * vm,
372 vlib_node_runtime_t * node, vlib_frame_t * frame)
374 uword n_packets = frame->n_vectors;
376 u32 n_left_from, n_left_to_next, next;
377 ip4_main_t *i4m = &ip4_main;
378 u16 *fragment_ids, *fid;
379 u8 host_config_ttl = i4m->host_config.ttl;
381 from = vlib_frame_vector_args (frame);
382 n_left_from = n_packets;
383 next = node->cached_next_index;
385 if (node->flags & VLIB_NODE_FLAG_TRACE)
386 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
388 sizeof (icmp_input_trace_t));
390 /* Get random fragment IDs for replies. */
391 fid = fragment_ids = clib_random_buffer_get_data (&vm->random_buffer,
393 sizeof (fragment_ids[0]));
395 while (n_left_from > 0)
397 vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
399 while (n_left_from > 2 && n_left_to_next > 2)
401 vlib_buffer_t *p0, *p1;
402 ip4_header_t *ip0, *ip1;
403 icmp46_header_t *icmp0, *icmp1;
406 ip_csum_t sum0, sum1;
408 bi0 = to_next[0] = from[0];
409 bi1 = to_next[1] = from[1];
416 p0 = vlib_get_buffer (vm, bi0);
417 p1 = vlib_get_buffer (vm, bi1);
418 ip0 = vlib_buffer_get_current (p0);
419 ip1 = vlib_buffer_get_current (p1);
420 icmp0 = ip4_next_header (ip0);
421 icmp1 = ip4_next_header (ip1);
423 vnet_buffer (p0)->sw_if_index[VLIB_RX] =
424 vnet_main.local_interface_sw_if_index;
425 vnet_buffer (p1)->sw_if_index[VLIB_RX] =
426 vnet_main.local_interface_sw_if_index;
428 /* Update ICMP checksum. */
429 sum0 = icmp0->checksum;
430 sum1 = icmp1->checksum;
432 ASSERT (icmp0->type == ICMP4_echo_request);
433 ASSERT (icmp1->type == ICMP4_echo_request);
434 sum0 = ip_csum_update (sum0, ICMP4_echo_request, ICMP4_echo_reply,
435 icmp46_header_t, type);
436 sum1 = ip_csum_update (sum1, ICMP4_echo_request, ICMP4_echo_reply,
437 icmp46_header_t, type);
438 icmp0->type = ICMP4_echo_reply;
439 icmp1->type = ICMP4_echo_reply;
441 icmp0->checksum = ip_csum_fold (sum0);
442 icmp1->checksum = ip_csum_fold (sum1);
444 src0 = ip0->src_address.data_u32;
445 src1 = ip1->src_address.data_u32;
446 dst0 = ip0->dst_address.data_u32;
447 dst1 = ip1->dst_address.data_u32;
449 /* Swap source and destination address.
450 Does not change checksum. */
451 ip0->src_address.data_u32 = dst0;
452 ip1->src_address.data_u32 = dst1;
453 ip0->dst_address.data_u32 = src0;
454 ip1->dst_address.data_u32 = src1;
456 /* Update IP checksum. */
457 sum0 = ip0->checksum;
458 sum1 = ip1->checksum;
460 sum0 = ip_csum_update (sum0, ip0->ttl, host_config_ttl,
462 sum1 = ip_csum_update (sum1, ip1->ttl, host_config_ttl,
464 ip0->ttl = host_config_ttl;
465 ip1->ttl = host_config_ttl;
467 /* New fragment id. */
468 sum0 = ip_csum_update (sum0, ip0->fragment_id, fid[0],
469 ip4_header_t, fragment_id);
470 sum1 = ip_csum_update (sum1, ip1->fragment_id, fid[1],
471 ip4_header_t, fragment_id);
472 ip0->fragment_id = fid[0];
473 ip1->fragment_id = fid[1];
476 ip0->checksum = ip_csum_fold (sum0);
477 ip1->checksum = ip_csum_fold (sum1);
479 ASSERT (ip4_header_checksum_is_valid (ip0));
480 ASSERT (ip4_header_checksum_is_valid (ip1));
482 p0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
483 p1->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
486 while (n_left_from > 0 && n_left_to_next > 0)
490 icmp46_header_t *icmp0;
494 bi0 = to_next[0] = from[0];
501 p0 = vlib_get_buffer (vm, bi0);
502 ip0 = vlib_buffer_get_current (p0);
503 icmp0 = ip4_next_header (ip0);
505 vnet_buffer (p0)->sw_if_index[VLIB_RX] =
506 vnet_main.local_interface_sw_if_index;
508 /* Update ICMP checksum. */
509 sum0 = icmp0->checksum;
511 ASSERT (icmp0->type == ICMP4_echo_request);
512 sum0 = ip_csum_update (sum0, ICMP4_echo_request, ICMP4_echo_reply,
513 icmp46_header_t, type);
514 icmp0->type = ICMP4_echo_reply;
515 icmp0->checksum = ip_csum_fold (sum0);
517 src0 = ip0->src_address.data_u32;
518 dst0 = ip0->dst_address.data_u32;
519 ip0->src_address.data_u32 = dst0;
520 ip0->dst_address.data_u32 = src0;
522 /* Update IP checksum. */
523 sum0 = ip0->checksum;
525 sum0 = ip_csum_update (sum0, ip0->ttl, host_config_ttl,
527 ip0->ttl = host_config_ttl;
529 sum0 = ip_csum_update (sum0, ip0->fragment_id, fid[0],
530 ip4_header_t, fragment_id);
531 ip0->fragment_id = fid[0];
534 ip0->checksum = ip_csum_fold (sum0);
536 ASSERT (ip4_header_checksum_is_valid (ip0));
538 p0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
541 vlib_put_next_frame (vm, node, next, n_left_to_next);
544 vlib_error_count (vm, ip4_icmp_input_node.index,
545 ICMP4_ERROR_ECHO_REPLIES_SENT, frame->n_vectors);
547 return frame->n_vectors;
551 format_icmp_input_trace (u8 * s, va_list * va)
553 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
554 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
555 icmp_input_trace_t *t = va_arg (*va, icmp_input_trace_t *);
558 format_ip4_header, t->packet_data, sizeof (t->packet_data));
564 VLIB_REGISTER_NODE (ip4_icmp_echo_request_node,static) = {
565 .function = ip4_icmp_echo_request,
566 .name = "ip4-icmp-echo-request",
568 .vector_size = sizeof (u32),
570 .format_trace = format_icmp_input_trace,
574 [0] = "ip4-load-balance",
581 ICMP6_ECHO_REQUEST_NEXT_LOOKUP,
582 ICMP6_ECHO_REQUEST_NEXT_OUTPUT,
583 ICMP6_ECHO_REQUEST_N_NEXT,
584 } icmp6_echo_request_next_t;
587 ip6_icmp_echo_request (vlib_main_t *vm, vlib_node_runtime_t *node,
591 u32 n_left_from, n_left_to_next, next_index;
592 ip6_main_t *im = &ip6_main;
594 from = vlib_frame_vector_args (frame);
595 n_left_from = frame->n_vectors;
596 next_index = node->cached_next_index;
598 if (node->flags & VLIB_NODE_FLAG_TRACE)
599 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
601 sizeof (icmp6_input_trace_t));
603 while (n_left_from > 0)
605 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
607 while (n_left_from > 2 && n_left_to_next > 2)
609 vlib_buffer_t *p0, *p1;
610 ip6_header_t *ip0, *ip1;
611 icmp46_header_t *icmp0, *icmp1;
612 ip6_address_t tmp0, tmp1;
613 ip_csum_t sum0, sum1;
615 u32 fib_index0, fib_index1;
616 u32 next0 = ICMP6_ECHO_REQUEST_NEXT_LOOKUP;
617 u32 next1 = ICMP6_ECHO_REQUEST_NEXT_LOOKUP;
619 bi0 = to_next[0] = from[0];
620 bi1 = to_next[1] = from[1];
627 p0 = vlib_get_buffer (vm, bi0);
628 p1 = vlib_get_buffer (vm, bi1);
629 ip0 = vlib_buffer_get_current (p0);
630 ip1 = vlib_buffer_get_current (p1);
631 icmp0 = ip6_next_header (ip0);
632 icmp1 = ip6_next_header (ip1);
634 /* Check icmp type to echo reply and update icmp checksum. */
635 sum0 = icmp0->checksum;
636 sum1 = icmp1->checksum;
638 ASSERT (icmp0->type == ICMP6_echo_request);
639 ASSERT (icmp1->type == ICMP6_echo_request);
640 sum0 = ip_csum_update (sum0, ICMP6_echo_request, ICMP6_echo_reply,
641 icmp46_header_t, type);
642 sum1 = ip_csum_update (sum1, ICMP6_echo_request, ICMP6_echo_reply,
643 icmp46_header_t, type);
645 icmp0->checksum = ip_csum_fold (sum0);
646 icmp1->checksum = ip_csum_fold (sum1);
648 icmp0->type = ICMP6_echo_reply;
649 icmp1->type = ICMP6_echo_reply;
651 /* Swap source and destination address. */
652 tmp0 = ip0->src_address;
653 tmp1 = ip1->src_address;
655 ip0->src_address = ip0->dst_address;
656 ip1->src_address = ip1->dst_address;
658 ip0->dst_address = tmp0;
659 ip1->dst_address = tmp1;
662 ip0->hop_limit = im->host_config.ttl;
663 ip1->hop_limit = im->host_config.ttl;
665 /* Determine the correct lookup fib indices... */
666 fib_index0 = vec_elt (im->fib_index_by_sw_if_index,
667 vnet_buffer (p0)->sw_if_index[VLIB_RX]);
668 vnet_buffer (p0)->sw_if_index[VLIB_TX] = fib_index0;
669 /* Determine the correct lookup fib indices... */
670 fib_index1 = vec_elt (im->fib_index_by_sw_if_index,
671 vnet_buffer (p1)->sw_if_index[VLIB_RX]);
672 vnet_buffer (p1)->sw_if_index[VLIB_TX] = fib_index1;
674 p0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
675 p1->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
677 /* verify speculative enqueues, maybe switch current next frame */
678 /* if next0==next1==next_index then nothing special needs to be done
680 vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
681 n_left_to_next, bi0, bi1, next0,
685 while (n_left_from > 0 && n_left_to_next > 0)
689 icmp46_header_t *icmp0;
694 u32 next0 = ICMP6_ECHO_REQUEST_NEXT_LOOKUP;
696 bi0 = to_next[0] = from[0];
703 p0 = vlib_get_buffer (vm, bi0);
704 ip0 = vlib_buffer_get_current (p0);
705 icmp0 = ip6_next_header (ip0);
707 /* Check icmp type to echo reply and update icmp checksum. */
708 sum0 = icmp0->checksum;
710 ASSERT (icmp0->type == ICMP6_echo_request);
711 sum0 = ip_csum_update (sum0, ICMP6_echo_request, ICMP6_echo_reply,
712 icmp46_header_t, type);
714 icmp0->checksum = ip_csum_fold (sum0);
716 icmp0->type = ICMP6_echo_reply;
718 /* Swap source and destination address. */
719 tmp0 = ip0->src_address;
720 ip0->src_address = ip0->dst_address;
721 ip0->dst_address = tmp0;
723 ip0->hop_limit = im->host_config.ttl;
725 /* if the packet is link local, we'll bounce through the link-local
726 * table with the RX interface correctly set */
727 fib_index0 = vec_elt (im->fib_index_by_sw_if_index,
728 vnet_buffer (p0)->sw_if_index[VLIB_RX]);
729 vnet_buffer (p0)->sw_if_index[VLIB_TX] = fib_index0;
731 p0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
732 /* Verify speculative enqueue, maybe switch current next frame */
733 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
734 n_left_to_next, bi0, next0);
737 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
740 vlib_error_count (vm, ip6_icmp_input_node.index,
741 ICMP6_ERROR_ECHO_REPLIES_SENT, frame->n_vectors);
743 return frame->n_vectors;
746 VLIB_REGISTER_NODE (ip6_icmp_echo_request_node,static) = {
747 .function = ip6_icmp_echo_request,
748 .name = "ip6-icmp-echo-request",
750 .vector_size = sizeof (u32),
752 .format_trace = format_icmp6_input_trace,
754 .n_next_nodes = ICMP6_ECHO_REQUEST_N_NEXT,
756 [ICMP6_ECHO_REQUEST_NEXT_LOOKUP] = "ip6-lookup",
757 [ICMP6_ECHO_REQUEST_NEXT_OUTPUT] = "interface-output",
762 * A swarm of address-family agnostic helper functions
763 * for building and sending the ICMP echo request.
765 * Deliberately mostly "static" rather than "static inline"
766 * so one can trace them sanely if needed in debugger, if needed.
770 static_always_inline u8
771 get_icmp_echo_payload_byte (int offset)
773 return (offset % 256);
776 /* Fill in the ICMP ECHO structure, return the safety-checked and possibly shrunk data_len */
778 init_icmp46_echo_request (vlib_main_t * vm, vlib_buffer_t * b0,
779 int l4_header_offset,
780 icmp46_echo_request_t * icmp46_echo, u16 seq_host,
781 u16 id_host, u64 now, u16 data_len)
787 l4_header_offset + sizeof (icmp46_header_t) +
788 offsetof (icmp46_echo_request_t, data);
789 int max_data_len = vlib_buffer_get_default_data_size (vm) - l34_len;
791 int first_buf_data_len = data_len < max_data_len ? data_len : max_data_len;
793 int payload_offset = 0;
794 for (i = 0; i < first_buf_data_len; i++)
795 icmp46_echo->data[i] = get_icmp_echo_payload_byte (payload_offset++);
797 /* inspired by vlib_buffer_add_data */
798 vlib_buffer_t *hb = b0;
799 int remaining_data_len = data_len - first_buf_data_len;
800 while (remaining_data_len)
802 int this_buf_data_len =
804 vlib_buffer_get_default_data_size (vm) ? remaining_data_len :
805 vlib_buffer_get_default_data_size (vm);
806 int n_alloc = vlib_buffer_alloc (vm, &b0->next_buffer, 1);
809 /* That is how much we have so far - return it... */
810 return (data_len - remaining_data_len);
812 b0->flags |= VLIB_BUFFER_NEXT_PRESENT;
813 /* move on to the newly acquired buffer */
814 b0 = vlib_get_buffer (vm, b0->next_buffer);
815 /* initialize the data */
816 for (i = 0; i < this_buf_data_len; i++)
818 b0->data[i] = get_icmp_echo_payload_byte (payload_offset++);
820 b0->current_length = this_buf_data_len;
821 b0->current_data = 0;
822 remaining_data_len -= this_buf_data_len;
824 hb->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
825 hb->current_length = l34_len + first_buf_data_len;
826 hb->total_length_not_including_first_buffer = data_len - first_buf_data_len;
828 icmp46_echo->time_sent = now;
829 icmp46_echo->seq = clib_host_to_net_u16 (seq_host);
830 icmp46_echo->id = clib_host_to_net_u16 (id_host);
836 ip46_fib_index_from_table_id (u32 table_id, int is_ip6)
838 u32 fib_index = is_ip6 ?
839 ip6_fib_index_from_table_id (table_id) :
840 ip4_fib_index_from_table_id (table_id);
844 static fib_node_index_t
845 ip46_fib_table_lookup_host (u32 fib_index, ip46_address_t * pa46, int is_ip6)
847 fib_node_index_t fib_entry_index = is_ip6 ?
848 ip6_fib_table_lookup (fib_index, &pa46->ip6, 128) :
849 ip4_fib_table_lookup (ip4_fib_get (fib_index), &pa46->ip4, 32);
850 return fib_entry_index;
854 ip46_get_resolving_interface (u32 fib_index, ip46_address_t * pa46,
857 u32 sw_if_index = ~0;
860 fib_node_index_t fib_entry_index;
861 fib_entry_index = ip46_fib_table_lookup_host (fib_index, pa46, is_ip6);
862 sw_if_index = fib_entry_get_resolving_interface (fib_entry_index);
868 ip46_fib_table_get_index_for_sw_if_index (u32 sw_if_index, int is_ip6,
869 ip46_address_t *pa46)
873 if (ip6_address_is_link_local_unicast (&pa46->ip6))
874 return ip6_ll_fib_get (sw_if_index);
875 return ip6_fib_table_get_index_for_sw_if_index (sw_if_index);
877 return ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
882 ip46_fill_l3_header (ip46_address_t * pa46, vlib_buffer_t * b0, int is_ip6)
886 ip6_header_t *ip6 = vlib_buffer_get_current (b0);
887 /* Fill in ip6 header fields */
888 ip6->ip_version_traffic_class_and_flow_label =
889 clib_host_to_net_u32 (0x6 << 28);
890 ip6->payload_length = 0; /* will be set later */
891 ip6->protocol = IP_PROTOCOL_ICMP6;
892 ip6->hop_limit = 255;
893 ip6->dst_address = pa46->ip6;
894 ip6->src_address = pa46->ip6;
895 return (sizeof (ip6_header_t));
899 ip4_header_t *ip4 = vlib_buffer_get_current (b0);
900 /* Fill in ip4 header fields */
902 ip4->ip_version_and_header_length = 0x45;
904 ip4->length = 0; /* will be set later */
905 ip4->fragment_id = 0;
906 ip4->flags_and_fragment_offset = 0;
908 ip4->protocol = IP_PROTOCOL_ICMP;
909 ip4->src_address = pa46->ip4;
910 ip4->dst_address = pa46->ip4;
911 return (sizeof (ip4_header_t));
916 ip46_set_src_address (u32 sw_if_index, vlib_buffer_t * b0, int is_ip6)
922 ip6_header_t *ip6 = vlib_buffer_get_current (b0);
924 res = ip6_sas_by_sw_if_index (sw_if_index, &ip6->dst_address,
929 ip4_header_t *ip4 = vlib_buffer_get_current (b0);
931 res = ip4_sas_by_sw_if_index (sw_if_index, &ip4->dst_address,
938 ip46_print_buffer_src_address (vlib_main_t * vm, vlib_buffer_t * b0,
941 void *format_addr_func;
945 ip6_header_t *ip6 = vlib_buffer_get_current (b0);
946 format_addr_func = format_ip6_address;
947 paddr = &ip6->src_address;
951 ip4_header_t *ip4 = vlib_buffer_get_current (b0);
952 format_addr_func = format_ip4_address;
953 paddr = &ip4->src_address;
955 vlib_cli_output (vm, "Source address: %U ", format_addr_func, paddr);
959 ip46_fill_icmp_request_at (vlib_main_t * vm, int l4_offset, u16 seq_host,
960 u16 id_host, u16 data_len, vlib_buffer_t * b0,
963 icmp46_header_t *icmp46 = vlib_buffer_get_current (b0) + l4_offset;
965 icmp46->type = is_ip6 ? ICMP6_echo_request : ICMP4_echo_request;
967 icmp46->checksum = 0;
969 icmp46_echo_request_t *icmp46_echo = (icmp46_echo_request_t *) (icmp46 + 1);
972 init_icmp46_echo_request (vm, b0, l4_offset, icmp46_echo, seq_host,
973 id_host, clib_cpu_time_now (), data_len);
978 /* Compute ICMP4 checksum with multibuffer support. */
980 ip4_icmp_compute_checksum (vlib_main_t * vm, vlib_buffer_t * p0,
984 u32 ip_header_length, payload_length_host_byte_order;
985 u32 n_this_buffer, n_bytes_left, n_ip_bytes_this_buffer;
987 void *data_this_buffer;
989 ip_header_length = ip4_header_bytes (ip0);
990 payload_length_host_byte_order =
991 clib_net_to_host_u16 (ip0->length) - ip_header_length;
993 /* ICMP4 checksum does not include the IP header */
996 n_bytes_left = n_this_buffer = payload_length_host_byte_order;
997 data_this_buffer = (void *) ip0 + ip_header_length;
998 n_ip_bytes_this_buffer =
999 p0->current_length - (((u8 *) ip0 - p0->data) - p0->current_data);
1000 if (n_this_buffer + ip_header_length > n_ip_bytes_this_buffer)
1002 n_this_buffer = n_ip_bytes_this_buffer > ip_header_length ?
1003 n_ip_bytes_this_buffer - ip_header_length : 0;
1007 sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
1008 n_bytes_left -= n_this_buffer;
1009 if (n_bytes_left == 0)
1012 ASSERT (p0->flags & VLIB_BUFFER_NEXT_PRESENT);
1013 p0 = vlib_get_buffer (vm, p0->next_buffer);
1014 data_this_buffer = vlib_buffer_get_current (p0);
1015 n_this_buffer = p0->current_length;
1018 sum16 = ~ip_csum_fold (sum0);
1025 ip46_fix_len_and_csum (vlib_main_t * vm, int l4_offset, u16 data_len,
1026 vlib_buffer_t * b0, int is_ip6)
1028 u16 payload_length =
1029 data_len + sizeof (icmp46_header_t) + offsetof (icmp46_echo_request_t,
1031 u16 total_length = payload_length + l4_offset;
1032 icmp46_header_t *icmp46 = vlib_buffer_get_current (b0) + l4_offset;
1033 icmp46->checksum = 0;
1037 ip6_header_t *ip6 = vlib_buffer_get_current (b0);
1038 ip6->payload_length = clib_host_to_net_u16 (payload_length);
1040 int bogus_length = 0;
1042 ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip6, &bogus_length);
1046 ip4_header_t *ip4 = vlib_buffer_get_current (b0);
1047 ip4->length = clib_host_to_net_u16 (total_length);
1049 ip4->checksum = ip4_header_checksum (ip4);
1050 icmp46->checksum = ip4_icmp_compute_checksum (vm, b0, ip4);
1055 at_most_a_frame (u32 count)
1057 return count > VLIB_FRAME_SIZE ? VLIB_FRAME_SIZE : count;
1061 ip46_enqueue_packet (vlib_main_t *vm, vlib_buffer_t *b0, u32 burst,
1062 u32 lookup_node_index)
1064 vlib_frame_t *f = 0;
1070 * Enqueue the packet, possibly as one or more frames of copies to make
1071 * bursts. We enqueue b0 as the very last buffer, when there is no possibility
1072 * for error in vlib_buffer_copy, so as to allow the caller to free it
1073 * in case we encounter the error in the middle of the loop.
1075 for (n_to_send = at_most_a_frame (burst), burst -= n_to_send; n_to_send > 0;
1076 n_to_send = at_most_a_frame (burst), burst -= n_to_send)
1078 f = vlib_get_frame_to_node (vm, lookup_node_index);
1079 /* f can not be NULL here - frame allocation failure causes panic */
1081 u32 *to_next = vlib_frame_vector_args (f);
1082 f->n_vectors = n_to_send;
1084 while (n_to_send > 1)
1086 vlib_buffer_t *b0copy = vlib_buffer_copy (vm, b0);
1087 if (PREDICT_FALSE (b0copy == NULL))
1089 *to_next++ = vlib_get_buffer_index (vm, b0copy);
1094 /* n_to_send is guaranteed to equal 1 here */
1097 /* not the last burst, so still make a copy for the last buffer */
1098 vlib_buffer_t *b0copy = vlib_buffer_copy (vm, b0);
1099 if (PREDICT_FALSE (b0copy == NULL))
1102 *to_next++ = vlib_get_buffer_index (vm, b0copy);
1106 /* put the original buffer as the last one of an error-free run */
1107 *to_next++ = vlib_get_buffer_index (vm, b0);
1109 vlib_put_frame_to_node (vm, lookup_node_index, f);
1110 n_sent += f->n_vectors;
1114 * We reach here in case we already enqueued one or more buffers
1115 * and maybe one or more frames but could not make more copies.
1116 * There is an outstanding frame - so ship it and return.
1117 * Caller will have to free the b0 in this case, since
1118 * we did not enqueue it here yet.
1121 ASSERT (n_to_send <= f->n_vectors);
1122 f->n_vectors -= n_to_send;
1123 n_sent += f->n_vectors;
1124 vlib_put_frame_to_node (vm, lookup_node_index, f);
1130 * An address-family agnostic ping send function.
1133 #define ERROR_OUT(e) do { err = e; goto done; } while (0)
1135 static send_ip46_ping_result_t
1136 send_ip46_ping (vlib_main_t * vm,
1138 ip46_address_t * pa46,
1140 u16 seq_host, u16 id_host, u16 data_len, u32 burst,
1141 u8 verbose, int is_ip6)
1143 int err = SEND_PING_OK;
1148 n_buf0 = vlib_buffer_alloc (vm, &bi0, 1);
1150 ERROR_OUT (SEND_PING_ALLOC_FAIL);
1152 b0 = vlib_get_buffer (vm, bi0);
1155 * if the user did not provide a source interface,
1156 * perform a resolution and use an interface
1157 * via which it succeeds.
1160 if (~0 == sw_if_index)
1162 fib_index = ip46_fib_index_from_table_id (table_id, is_ip6);
1163 sw_if_index = ip46_get_resolving_interface (fib_index, pa46, is_ip6);
1167 ip46_fib_table_get_index_for_sw_if_index (sw_if_index, is_ip6, pa46);
1169 if (~0 == fib_index)
1170 ERROR_OUT (SEND_PING_NO_TABLE);
1171 if (~0 == sw_if_index)
1172 ERROR_OUT (SEND_PING_NO_INTERFACE);
1174 vnet_buffer (b0)->sw_if_index[VLIB_RX] = sw_if_index;
1175 vnet_buffer (b0)->sw_if_index[VLIB_TX] = fib_index;
1177 int l4_header_offset = ip46_fill_l3_header (pa46, b0, is_ip6);
1179 /* set the src address in the buffer */
1180 if (!ip46_set_src_address (sw_if_index, b0, is_ip6))
1181 ERROR_OUT (SEND_PING_NO_SRC_ADDRESS);
1183 ip46_print_buffer_src_address (vm, b0, is_ip6);
1186 ip46_fill_icmp_request_at (vm, l4_header_offset, seq_host, id_host,
1187 data_len, b0, is_ip6);
1189 ip46_fix_len_and_csum (vm, l4_header_offset, data_len, b0, is_ip6);
1191 u32 node_index = ip6_lookup_node.index;
1194 if (pa46->ip6.as_u32[0] == clib_host_to_net_u32 (0xff020000))
1196 node_index = ip6_rewrite_mcast_node.index;
1197 vnet_buffer (b0)->sw_if_index[VLIB_RX] = sw_if_index;
1198 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sw_if_index;
1199 vnet_buffer (b0)->ip.adj_index[VLIB_TX] =
1200 ip6_link_get_mcast_adj (sw_if_index);
1205 node_index = ip4_lookup_node.index;
1207 int n_sent = ip46_enqueue_packet (vm, b0, burst, node_index);
1209 err = SEND_PING_NO_BUFFERS;
1212 if (err != SEND_PING_OK)
1215 vlib_buffer_free (vm, &bi0, 1);
1220 static send_ip46_ping_result_t
1221 send_ip6_ping (vlib_main_t * vm,
1222 u32 table_id, ip6_address_t * pa6,
1223 u32 sw_if_index, u16 seq_host, u16 id_host, u16 data_len,
1224 u32 burst, u8 verbose)
1226 ip46_address_t target;
1228 return send_ip46_ping (vm, table_id, &target, sw_if_index, seq_host,
1229 id_host, data_len, burst, verbose, 1 /* is_ip6 */ );
1232 static send_ip46_ping_result_t
1233 send_ip4_ping (vlib_main_t * vm,
1234 u32 table_id, ip4_address_t * pa4,
1235 u32 sw_if_index, u16 seq_host, u16 id_host, u16 data_len,
1236 u32 burst, u8 verbose)
1238 ip46_address_t target;
1239 ip46_address_set_ip4 (&target, pa4);
1240 return send_ip46_ping (vm, table_id, &target, sw_if_index, seq_host,
1241 id_host, data_len, burst, verbose, 0 /* is_ip6 */ );
1245 print_ip46_icmp_reply (vlib_main_t * vm, u32 bi0, int is_ip6)
1247 vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
1250 void *format_addr_func;
1255 ip6_header_t *ip6 = vlib_buffer_get_current (b0);
1256 paddr = (void *) &ip6->src_address;
1257 format_addr_func = (void *) format_ip6_address;
1258 ttl = ip6->hop_limit;
1259 l4_offset = sizeof (ip6_header_t); // FIXME - EH processing ?
1260 payload_length = clib_net_to_host_u16 (ip6->payload_length);
1264 ip4_header_t *ip4 = vlib_buffer_get_current (b0);
1265 paddr = (void *) &ip4->src_address;
1266 format_addr_func = (void *) format_ip4_address;
1268 l4_offset = ip4_header_bytes (ip4);
1270 clib_net_to_host_u16 (ip4->length) + ip4_header_bytes (ip4);
1272 icmp46_header_t *icmp = vlib_buffer_get_current (b0) + l4_offset;
1273 icmp46_echo_request_t *icmp_echo = (icmp46_echo_request_t *) (icmp + 1);
1274 u64 *dataplane_ts = (u64 *) & vnet_buffer (b0)->unused[0];
1276 f64 clocks_per_second = ((f64) vm->clib_time.clocks_per_second);
1278 ((f64) (*dataplane_ts - icmp_echo->time_sent)) / clocks_per_second;
1280 vlib_cli_output (vm,
1281 "%d bytes from %U: icmp_seq=%d ttl=%d time=%.4f ms",
1285 clib_host_to_net_u16 (icmp_echo->seq), ttl, rtt * 1000.0);
1289 * Perform the ping run with the given parameters in the current CLI process.
1290 * Depending on whether pa4 or pa6 is set, runs IPv4 or IPv6 ping.
1291 * The amusing side effect is of course if both are set, then both pings are sent.
1292 * This behavior can be used to ping a dualstack host over IPv4 and IPv6 at once.
1296 run_ping_ip46_address (vlib_main_t * vm, u32 table_id, ip4_address_t * pa4,
1297 ip6_address_t * pa6, u32 sw_if_index,
1298 f64 ping_interval, u32 ping_repeat, u32 data_len,
1299 u32 ping_burst, u32 verbose)
1302 uword curr_proc = vlib_current_process (vm);
1307 static u32 rand_seed = 0;
1309 if (PREDICT_FALSE (!rand_seed))
1310 rand_seed = random_default_seed ();
1312 icmp_id = random_u32 (&rand_seed) & 0xffff;
1314 while (~0 != get_cli_process_id_by_icmp_id_mt (vm, icmp_id))
1316 vlib_cli_output (vm, "ICMP ID collision at %d, incrementing", icmp_id);
1320 set_cli_process_id_by_icmp_id_mt (vm, icmp_id, curr_proc);
1322 for (i = 1; i <= ping_repeat; i++)
1324 send_ip46_ping_result_t res = SEND_PING_OK;
1326 f64 time_ping_sent = vlib_time_now (vm);
1329 res = send_ip6_ping (vm, table_id,
1330 pa6, sw_if_index, i, icmp_id,
1331 data_len, ping_burst, verbose);
1332 if (SEND_PING_OK == res)
1333 n_requests += ping_burst;
1335 vlib_cli_output (vm, "Failed: %U", format_ip46_ping_result, res);
1339 res = send_ip4_ping (vm, table_id, pa4,
1340 sw_if_index, i, icmp_id, data_len,
1341 ping_burst, verbose);
1342 if (SEND_PING_OK == res)
1343 n_requests += ping_burst;
1345 vlib_cli_output (vm, "Failed: %U", format_ip46_ping_result, res);
1348 /* Collect and print the responses until it is time to send a next ping */
1350 while ((i <= ping_repeat)
1353 time_ping_sent + ping_interval - vlib_time_now (vm)) > 0.0))
1355 uword event_type, *event_data = 0;
1356 vlib_process_wait_for_event_or_clock (vm, sleep_interval);
1357 event_type = vlib_process_get_events (vm, &event_data);
1360 case ~0: /* no events => timeout */
1362 case PING_RESPONSE_IP6:
1364 case PING_RESPONSE_IP4:
1367 int is_ip6 = (event_type == PING_RESPONSE_IP6);
1368 for (ii = 0; ii < vec_len (event_data); ii++)
1370 u32 bi0 = event_data[ii];
1371 print_ip46_icmp_reply (vm, bi0, is_ip6);
1374 vlib_buffer_free (vm, &bi0, 1);
1378 case UNIX_CLI_PROCESS_EVENT_READ_READY:
1379 case UNIX_CLI_PROCESS_EVENT_QUIT:
1380 /* someone pressed a key, abort */
1381 vlib_cli_output (vm, "Aborted due to a keypress.");
1384 vec_free (event_data);
1388 vlib_cli_output (vm, "\n");
1392 n_requests) ? 0 : 100.0 * ((float) n_requests -
1393 (float) n_replies) / (float) n_requests;
1394 vlib_cli_output (vm,
1395 "Statistics: %u sent, %u received, %f%% packet loss\n",
1396 n_requests, n_replies, loss);
1397 clear_cli_process_id_by_icmp_id_mt (vm, icmp_id);
1403 static clib_error_t *
1404 ping_ip_address (vlib_main_t * vm,
1405 unformat_input_t * input, vlib_cli_command_t * cmd)
1409 clib_error_t *error = 0;
1410 u32 ping_repeat = 5;
1412 u8 ping_ip4, ping_ip6;
1413 vnet_main_t *vnm = vnet_get_main ();
1414 u32 data_len = PING_DEFAULT_DATA_LEN;
1416 f64 ping_interval = PING_DEFAULT_INTERVAL;
1417 u32 sw_if_index, table_id;
1420 ping_ip4 = ping_ip6 = 0;
1423 if (unformat (input, "%U", unformat_ip4_address, &a4))
1427 else if (unformat (input, "%U", unformat_ip6_address, &a6))
1431 else if (unformat (input, "ipv4"))
1433 if (unformat (input, "%U", unformat_ip4_address, &a4))
1440 clib_error_return (0,
1441 "expecting IPv4 address but got `%U'",
1442 format_unformat_error, input);
1445 else if (unformat (input, "ipv6"))
1447 if (unformat (input, "%U", unformat_ip6_address, &a6))
1454 clib_error_return (0,
1455 "expecting IPv6 address but got `%U'",
1456 format_unformat_error, input);
1462 clib_error_return (0,
1463 "expecting IP4/IP6 address `%U'. Usage: ping <addr> [source <intf>] [size <datasz>] [repeat <count>] [verbose]",
1464 format_unformat_error, input);
1468 /* allow for the second AF in the same ping */
1469 if (!ping_ip4 && (unformat (input, "ipv4")))
1471 if (unformat (input, "%U", unformat_ip4_address, &a4))
1476 else if (!ping_ip6 && (unformat (input, "ipv6")))
1478 if (unformat (input, "%U", unformat_ip6_address, &a6))
1484 /* parse the rest of the parameters in a cycle */
1485 while (!unformat_eof (input, NULL))
1487 if (unformat (input, "source"))
1490 (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
1493 clib_error_return (0,
1494 "unknown interface `%U'",
1495 format_unformat_error, input);
1499 else if (unformat (input, "size"))
1501 if (!unformat (input, "%u", &data_len))
1504 clib_error_return (0,
1505 "expecting size but got `%U'",
1506 format_unformat_error, input);
1509 if (data_len > PING_MAXIMUM_DATA_SIZE)
1512 clib_error_return (0,
1513 "%d is bigger than maximum allowed payload size %d",
1514 data_len, PING_MAXIMUM_DATA_SIZE);
1518 else if (unformat (input, "table-id"))
1520 if (!unformat (input, "%u", &table_id))
1523 clib_error_return (0,
1524 "expecting table-id but got `%U'",
1525 format_unformat_error, input);
1529 else if (unformat (input, "interval"))
1531 if (!unformat (input, "%f", &ping_interval))
1534 clib_error_return (0,
1535 "expecting interval (floating point number) got `%U'",
1536 format_unformat_error, input);
1540 else if (unformat (input, "repeat"))
1542 if (!unformat (input, "%u", &ping_repeat))
1545 clib_error_return (0,
1546 "expecting repeat count but got `%U'",
1547 format_unformat_error, input);
1551 else if (unformat (input, "burst"))
1553 if (!unformat (input, "%u", &ping_burst))
1556 clib_error_return (0,
1557 "expecting burst count but got `%U'",
1558 format_unformat_error, input);
1562 else if (unformat (input, "verbose"))
1568 error = clib_error_return (0, "unknown input `%U'",
1569 format_unformat_error, input);
1575 * Operationally, one won't (and shouldn't) need to send more than a frame worth of pings.
1576 * But it may be handy during the debugging.
1580 #define MAX_PING_BURST (10*VLIB_FRAME_SIZE)
1582 #define MAX_PING_BURST (VLIB_FRAME_SIZE)
1585 if (ping_burst < 1 || ping_burst > MAX_PING_BURST)
1586 return clib_error_return (0, "burst size must be between 1 and %u",
1589 run_ping_ip46_address (vm, table_id, ping_ip4 ? &a4 : NULL,
1590 ping_ip6 ? &a6 : NULL, sw_if_index, ping_interval,
1591 ping_repeat, data_len, ping_burst, verbose);
1597 * This command sends an ICMP ECHO_REQUEST to network hosts. The address
1598 * can be an IPv4 or IPv6 address (or both at the same time).
1602 * Example of how ping an IPv4 address:
1603 * @cliexstart{ping 172.16.1.2 source GigabitEthernet2/0/0 repeat 2}
1604 * 64 bytes from 172.16.1.2: icmp_seq=1 ttl=64 time=.1090 ms
1605 * 64 bytes from 172.16.1.2: icmp_seq=2 ttl=64 time=.0914 ms
1607 * Statistics: 2 sent, 2 received, 0% packet loss
1610 * Example of how ping both an IPv4 address and IPv6 address at the same time:
1611 * @cliexstart{ping 172.16.1.2 ipv6 fe80::24a5:f6ff:fe9c:3a36 source GigabitEthernet2/0/0 repeat 2 verbose}
1612 * Adjacency index: 10, sw_if_index: 1
1613 * Adj: ip6-discover-neighbor
1615 * Forced set interface: 1
1616 * Adjacency index: 0, sw_if_index: 4294967295
1619 * Forced set interface: 1
1620 * Source address: 172.16.1.1
1621 * 64 bytes from 172.16.1.2: icmp_seq=1 ttl=64 time=.1899 ms
1622 * Adjacency index: 10, sw_if_index: 1
1623 * Adj: ip6-discover-neighbor
1625 * Forced set interface: 1
1626 * Adjacency index: 0, sw_if_index: 4294967295
1629 * Forced set interface: 1
1630 * Source address: 172.16.1.1
1631 * 64 bytes from 172.16.1.2: icmp_seq=2 ttl=64 time=.0910 ms
1633 * Statistics: 4 sent, 2 received, 50% packet loss
1638 VLIB_CLI_COMMAND (ping_command, static) =
1641 .function = ping_ip_address,
1642 .short_help = "ping {<ip-addr> | ipv4 <ip4-addr> | ipv6 <ip6-addr>}"
1643 " [ipv4 <ip4-addr> | ipv6 <ip6-addr>] [source <interface>]"
1644 " [size <pktsize:60>] [interval <sec:1>] [repeat <cnt:5>] [table-id <id:0>]"
1645 " [burst <count:1>] [verbose]",
1650 static clib_error_t *
1651 ping_cli_init (vlib_main_t * vm)
1653 vlib_thread_main_t *tm = vlib_get_thread_main ();
1654 ping_main_t *pm = &ping_main;
1656 pm->ip6_main = &ip6_main;
1657 pm->ip4_main = &ip4_main;
1658 icmp6_register_type (vm, ICMP6_echo_reply, ip6_icmp_echo_reply_node.index);
1659 ip4_icmp_register_type (vm, ICMP4_echo_reply,
1660 ip4_icmp_echo_reply_node.index);
1661 if (tm->n_vlib_mains > 1)
1662 clib_spinlock_init (&pm->ping_run_check_lock);
1664 ip4_icmp_register_type (vm, ICMP4_echo_request,
1665 ip4_icmp_echo_request_node.index);
1666 icmp6_register_type (vm, ICMP6_echo_request,
1667 ip6_icmp_echo_request_node.index);
1672 VLIB_INIT_FUNCTION (ping_cli_init);
1675 VLIB_PLUGIN_REGISTER () = {
1676 .version = VPP_BUILD_VER,
1677 .description = "Ping (ping)",
1682 * fd.io coding-style-patch-verification: ON
1685 * eval: (c-set-style "gnu")