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 <vnet/fib/ip6_fib.h>
20 #include <vnet/fib/ip4_fib.h>
21 #include <vnet/fib/fib_entry.h>
22 #include <vnet/plugin/plugin.h>
23 #include <vpp/app/version.h>
25 #include <vnet/ip/icmp4.h>
26 #include <ping/ping.h>
28 ping_main_t ping_main;
32 * @brief IPv4 and IPv6 ICMP Ping.
34 * This file contains code to support IPv4 or IPv6 ICMP ECHO_REQUEST to
49 format_icmp_echo_trace (u8 * s, va_list * va)
51 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
52 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
53 icmp_echo_trace_t *t = va_arg (*va, icmp_echo_trace_t *);
56 format (s, "ICMP%s echo id %d seq %d", t->is_ip6 ? "6" : "4", t->id,
58 if (t->cli_process_node == PING_CLI_UNKNOWN_NODE)
60 s = format (s, " (unknown)");
64 s = format (s, " send to cli node %d", t->cli_process_node);
72 format_ip46_ping_result (u8 * s, va_list * args)
74 send_ip46_ping_result_t res = va_arg (*args, send_ip46_ping_result_t);
78 #define _(v, n) case SEND_PING_##v: s = format(s, "%s", n);break;
79 foreach_ip46_ping_result
88 * Poor man's get-set-clear functions
89 * for manipulation of icmp_id -> cli_process_id
92 * There should normally be very few (0..1..2) of these
93 * mappings, so the linear search is a good strategy.
95 * Make them thread-safe via a simple spinlock.
100 static_always_inline uword
101 get_cli_process_id_by_icmp_id_mt (vlib_main_t * vm, u16 icmp_id)
103 ping_main_t *pm = &ping_main;
104 uword cli_process_id = PING_CLI_UNKNOWN_NODE;
107 clib_spinlock_lock_if_init (&pm->ping_run_check_lock);
108 vec_foreach (pr, pm->active_ping_runs)
110 if (pr->icmp_id == icmp_id)
112 cli_process_id = pr->cli_process_id;
116 clib_spinlock_unlock_if_init (&pm->ping_run_check_lock);
117 return cli_process_id;
121 static_always_inline void
122 set_cli_process_id_by_icmp_id_mt (vlib_main_t * vm, u16 icmp_id,
123 uword cli_process_id)
125 ping_main_t *pm = &ping_main;
128 clib_spinlock_lock_if_init (&pm->ping_run_check_lock);
129 vec_foreach (pr, pm->active_ping_runs)
131 if (pr->icmp_id == icmp_id)
133 pr->cli_process_id = cli_process_id;
134 goto have_found_and_set;
137 /* no such key yet - add a new one */
138 ping_run_t new_pr = {.icmp_id = icmp_id,.cli_process_id = cli_process_id };
139 vec_add1 (pm->active_ping_runs, new_pr);
141 clib_spinlock_unlock_if_init (&pm->ping_run_check_lock);
145 static_always_inline void
146 clear_cli_process_id_by_icmp_id_mt (vlib_main_t * vm, u16 icmp_id)
148 ping_main_t *pm = &ping_main;
151 clib_spinlock_lock_if_init (&pm->ping_run_check_lock);
152 vec_foreach (pr, pm->active_ping_runs)
154 if (pr->icmp_id == icmp_id)
156 vec_del1 (pm->active_ping_runs, pm->active_ping_runs - pr);
160 clib_spinlock_unlock_if_init (&pm->ping_run_check_lock);
163 static_always_inline int
164 ip46_get_icmp_id_and_seq (vlib_main_t * vm, vlib_buffer_t * b0,
165 u16 * out_icmp_id, u16 * out_icmp_seq, int is_ip6)
170 ip6_header_t *ip6 = vlib_buffer_get_current (b0);
171 if (ip6->protocol != IP_PROTOCOL_ICMP6)
175 l4_offset = sizeof (*ip6); // IPv6 EH
179 ip4_header_t *ip4 = vlib_buffer_get_current (b0);
180 l4_offset = ip4_header_bytes (ip4);
183 icmp46_header_t *icmp46 = vlib_buffer_get_current (b0) + l4_offset;
184 icmp46_echo_request_t *icmp46_echo = (icmp46_echo_request_t *) (icmp46 + 1);
186 *out_icmp_id = clib_net_to_host_u16 (icmp46_echo->id);
187 *out_icmp_seq = clib_net_to_host_u16 (icmp46_echo->seq);
192 * post the buffer to a given cli process node - the caller should forget bi0 after return.
195 static_always_inline void
196 ip46_post_icmp_reply_event (vlib_main_t * vm, uword cli_process_id, u32 bi0,
199 vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
200 u64 nowts = clib_cpu_time_now ();
202 /* Pass the timestamp to the cli_process thanks to the vnet_buffer unused metadata field */
204 /* Camping on unused data... just ensure statically that there is enough space */
205 STATIC_ASSERT (ARRAY_LEN (vnet_buffer (b0)->unused) *
206 sizeof (vnet_buffer (b0)->unused[0]) > sizeof (nowts),
207 "ping reply timestamp fits within remaining space of vnet_buffer unused data");
208 u64 *pnowts = (void *) &vnet_buffer (b0)->unused[0];
211 u32 event_id = is_ip6 ? PING_RESPONSE_IP6 : PING_RESPONSE_IP4;
212 vlib_process_signal_event_mt (vm, cli_process_id, event_id, bi0);
216 static_always_inline void
217 ip46_echo_reply_maybe_trace_buffer (vlib_main_t * vm,
218 vlib_node_runtime_t * node,
219 uword cli_process_id, u16 id, u16 seq,
220 vlib_buffer_t * b0, int is_ip6)
222 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
224 icmp_echo_trace_t *tr = vlib_add_trace (vm, node, b0, sizeof (*tr));
227 tr->cli_process_node = cli_process_id;
233 static_always_inline uword
234 ip46_icmp_echo_reply_inner_node_fn (vlib_main_t * vm,
235 vlib_node_runtime_t * node,
236 vlib_frame_t * frame, int do_trace,
239 u32 n_left_from, *from, *to_next;
240 icmp46_echo_reply_next_t next_index;
242 from = vlib_frame_vector_args (frame);
243 n_left_from = frame->n_vectors;
245 next_index = node->cached_next_index;
247 while (n_left_from > 0)
250 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
252 while (n_left_from > 0 && n_left_to_next > 0)
257 * The buffers (replies) are either posted to the CLI thread
258 * awaiting for them for subsequent analysis and disposal,
259 * or are sent to the punt node.
261 * So the only "next" node is a punt, normally.
263 u32 next0 = ICMP46_ECHO_REPLY_NEXT_PUNT;
266 b0 = vlib_get_buffer (vm, bi0);
272 uword cli_process_id = PING_CLI_UNKNOWN_NODE;
274 if (ip46_get_icmp_id_and_seq (vm, b0, &icmp_id, &icmp_seq, is_ip6))
276 cli_process_id = get_cli_process_id_by_icmp_id_mt (vm, icmp_id);
280 ip46_echo_reply_maybe_trace_buffer (vm, node, cli_process_id,
281 icmp_id, icmp_seq, b0,
284 if (~0 == cli_process_id)
286 /* no outstanding requests for this reply, punt */
287 /* speculatively enqueue b0 to the current next frame */
291 /* verify speculative enqueue, maybe switch current next frame */
292 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
293 to_next, n_left_to_next,
298 /* Post the buffer to CLI thread. It will take care of freeing it. */
299 ip46_post_icmp_reply_event (vm, cli_process_id, bi0, is_ip6);
302 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
304 return frame->n_vectors;
308 * select "with-trace" or "without-trace" codepaths upfront.
310 static_always_inline uword
311 ip46_icmp_echo_reply_outer_node_fn (vlib_main_t * vm,
312 vlib_node_runtime_t * node,
313 vlib_frame_t * frame, int is_ip6)
315 if (node->flags & VLIB_NODE_FLAG_TRACE)
316 return ip46_icmp_echo_reply_inner_node_fn (vm, node, frame,
317 1 /* do_trace */ , is_ip6);
319 return ip46_icmp_echo_reply_inner_node_fn (vm, node, frame,
320 0 /* do_trace */ , is_ip6);
324 ip4_icmp_echo_reply_node_fn (vlib_main_t * vm,
325 vlib_node_runtime_t * node, vlib_frame_t * frame)
327 return ip46_icmp_echo_reply_outer_node_fn (vm, node, frame,
332 ip6_icmp_echo_reply_node_fn (vlib_main_t * vm,
333 vlib_node_runtime_t * node, vlib_frame_t * frame)
335 return ip46_icmp_echo_reply_outer_node_fn (vm, node, frame,
340 VLIB_REGISTER_NODE (ip6_icmp_echo_reply_node, static) =
342 .function = ip6_icmp_echo_reply_node_fn,
343 .name = "ip6-icmp-echo-reply",
344 .vector_size = sizeof (u32),
345 .format_trace = format_icmp_echo_trace,
346 .n_next_nodes = ICMP46_ECHO_REPLY_N_NEXT,
348 [ICMP46_ECHO_REPLY_NEXT_DROP] = "ip6-drop",
349 [ICMP46_ECHO_REPLY_NEXT_PUNT] = "ip6-punt",
353 VLIB_REGISTER_NODE (ip4_icmp_echo_reply_node, static) =
355 .function = ip4_icmp_echo_reply_node_fn,
356 .name = "ip4-icmp-echo-reply",
357 .vector_size = sizeof (u32),
358 .format_trace = format_icmp_echo_trace,
359 .n_next_nodes = ICMP46_ECHO_REPLY_N_NEXT,
361 [ICMP46_ECHO_REPLY_NEXT_DROP] = "ip4-drop",
362 [ICMP46_ECHO_REPLY_NEXT_PUNT] = "ip4-punt",
369 * A swarm of address-family agnostic helper functions
370 * for building and sending the ICMP echo request.
372 * Deliberately mostly "static" rather than "static inline"
373 * so one can trace them sanely if needed in debugger, if needed.
377 static_always_inline u8
378 get_icmp_echo_payload_byte (int offset)
380 return (offset % 256);
383 /* Fill in the ICMP ECHO structure, return the safety-checked and possibly shrunk data_len */
385 init_icmp46_echo_request (vlib_main_t * vm, vlib_buffer_t * b0,
386 int l4_header_offset,
387 icmp46_echo_request_t * icmp46_echo, u16 seq_host,
388 u16 id_host, u64 now, u16 data_len)
394 l4_header_offset + sizeof (icmp46_header_t) +
395 offsetof (icmp46_echo_request_t, data);
396 int max_data_len = vlib_buffer_get_default_data_size (vm) - l34_len;
398 int first_buf_data_len = data_len < max_data_len ? data_len : max_data_len;
400 int payload_offset = 0;
401 for (i = 0; i < first_buf_data_len; i++)
402 icmp46_echo->data[i] = get_icmp_echo_payload_byte (payload_offset++);
404 /* inspired by vlib_buffer_add_data */
405 vlib_buffer_t *hb = b0;
406 int remaining_data_len = data_len - first_buf_data_len;
407 while (remaining_data_len)
409 int this_buf_data_len =
411 vlib_buffer_get_default_data_size (vm) ? remaining_data_len :
412 vlib_buffer_get_default_data_size (vm);
413 int n_alloc = vlib_buffer_alloc (vm, &b0->next_buffer, 1);
416 /* That is how much we have so far - return it... */
417 return (data_len - remaining_data_len);
419 b0->flags |= VLIB_BUFFER_NEXT_PRESENT;
420 /* move on to the newly acquired buffer */
421 b0 = vlib_get_buffer (vm, b0->next_buffer);
422 /* initialize the data */
423 for (i = 0; i < this_buf_data_len; i++)
425 b0->data[i] = get_icmp_echo_payload_byte (payload_offset++);
427 b0->current_length = this_buf_data_len;
428 b0->current_data = 0;
429 remaining_data_len -= this_buf_data_len;
431 hb->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
432 hb->current_length = l34_len + first_buf_data_len;
433 hb->total_length_not_including_first_buffer = data_len - first_buf_data_len;
435 icmp46_echo->time_sent = now;
436 icmp46_echo->seq = clib_host_to_net_u16 (seq_host);
437 icmp46_echo->id = clib_host_to_net_u16 (id_host);
443 ip46_fib_index_from_table_id (u32 table_id, int is_ip6)
445 u32 fib_index = is_ip6 ?
446 ip6_fib_index_from_table_id (table_id) :
447 ip4_fib_index_from_table_id (table_id);
451 static fib_node_index_t
452 ip46_fib_table_lookup_host (u32 fib_index, ip46_address_t * pa46, int is_ip6)
454 fib_node_index_t fib_entry_index = is_ip6 ?
455 ip6_fib_table_lookup (fib_index, &pa46->ip6, 128) :
456 ip4_fib_table_lookup (ip4_fib_get (fib_index), &pa46->ip4, 32);
457 return fib_entry_index;
461 ip46_get_resolving_interface (u32 fib_index, ip46_address_t * pa46,
464 u32 sw_if_index = ~0;
467 fib_node_index_t fib_entry_index;
468 fib_entry_index = ip46_fib_table_lookup_host (fib_index, pa46, is_ip6);
469 sw_if_index = fib_entry_get_resolving_interface (fib_entry_index);
475 ip46_fib_table_get_index_for_sw_if_index (u32 sw_if_index, int is_ip6)
477 u32 fib_table_index = is_ip6 ?
478 ip6_fib_table_get_index_for_sw_if_index (sw_if_index) :
479 ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
480 return fib_table_index;
486 ip46_fill_l3_header (ip46_address_t * pa46, vlib_buffer_t * b0, int is_ip6)
490 ip6_header_t *ip6 = vlib_buffer_get_current (b0);
491 /* Fill in ip6 header fields */
492 ip6->ip_version_traffic_class_and_flow_label =
493 clib_host_to_net_u32 (0x6 << 28);
494 ip6->payload_length = 0; /* will be set later */
495 ip6->protocol = IP_PROTOCOL_ICMP6;
496 ip6->hop_limit = 255;
497 ip6->dst_address = pa46->ip6;
498 ip6->src_address = pa46->ip6;
499 return (sizeof (ip6_header_t));
503 ip4_header_t *ip4 = vlib_buffer_get_current (b0);
504 /* Fill in ip4 header fields */
506 ip4->ip_version_and_header_length = 0x45;
508 ip4->length = 0; /* will be set later */
509 ip4->fragment_id = 0;
510 ip4->flags_and_fragment_offset = 0;
512 ip4->protocol = IP_PROTOCOL_ICMP;
513 ip4->src_address = pa46->ip4;
514 ip4->dst_address = pa46->ip4;
515 return (sizeof (ip4_header_t));
520 ip46_set_src_address (u32 sw_if_index, vlib_buffer_t * b0, int is_ip6)
525 ip6_main_t *im = &ip6_main;
526 ip6_header_t *ip6 = vlib_buffer_get_current (b0);
528 ip6_src_address_for_packet (&im->lookup_main, sw_if_index,
529 &ip6->dst_address, &ip6->src_address);
533 ip4_main_t *im = &ip4_main;
534 ip4_header_t *ip4 = vlib_buffer_get_current (b0);
536 ip4_src_address_for_packet (&im->lookup_main, sw_if_index,
538 /* IP4 and IP6 paths have the inverse logic. Harmonize. */
545 ip46_print_buffer_src_address (vlib_main_t * vm, vlib_buffer_t * b0,
548 void *format_addr_func;
552 ip6_header_t *ip6 = vlib_buffer_get_current (b0);
553 format_addr_func = format_ip6_address;
554 paddr = &ip6->src_address;
558 ip4_header_t *ip4 = vlib_buffer_get_current (b0);
559 format_addr_func = format_ip4_address;
560 paddr = &ip4->src_address;
562 vlib_cli_output (vm, "Source address: %U ", format_addr_func, paddr);
566 ip46_fill_icmp_request_at (vlib_main_t * vm, int l4_offset, u16 seq_host,
567 u16 id_host, u16 data_len, vlib_buffer_t * b0,
570 icmp46_header_t *icmp46 = vlib_buffer_get_current (b0) + l4_offset;
572 icmp46->type = is_ip6 ? ICMP6_echo_request : ICMP4_echo_request;
574 icmp46->checksum = 0;
576 icmp46_echo_request_t *icmp46_echo = (icmp46_echo_request_t *) (icmp46 + 1);
579 init_icmp46_echo_request (vm, b0, l4_offset, icmp46_echo, seq_host,
580 id_host, clib_cpu_time_now (), data_len);
585 /* Compute ICMP4 checksum with multibuffer support. */
587 ip4_icmp_compute_checksum (vlib_main_t * vm, vlib_buffer_t * p0,
591 u32 ip_header_length, payload_length_host_byte_order;
592 u32 n_this_buffer, n_bytes_left, n_ip_bytes_this_buffer;
594 void *data_this_buffer;
596 ip_header_length = ip4_header_bytes (ip0);
597 payload_length_host_byte_order =
598 clib_net_to_host_u16 (ip0->length) - ip_header_length;
600 /* ICMP4 checksum does not include the IP header */
603 n_bytes_left = n_this_buffer = payload_length_host_byte_order;
604 data_this_buffer = (void *) ip0 + ip_header_length;
605 n_ip_bytes_this_buffer =
606 p0->current_length - (((u8 *) ip0 - p0->data) - p0->current_data);
607 if (n_this_buffer + ip_header_length > n_ip_bytes_this_buffer)
609 n_this_buffer = n_ip_bytes_this_buffer > ip_header_length ?
610 n_ip_bytes_this_buffer - ip_header_length : 0;
614 sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
615 n_bytes_left -= n_this_buffer;
616 if (n_bytes_left == 0)
619 ASSERT (p0->flags & VLIB_BUFFER_NEXT_PRESENT);
620 p0 = vlib_get_buffer (vm, p0->next_buffer);
621 data_this_buffer = vlib_buffer_get_current (p0);
622 n_this_buffer = p0->current_length;
625 sum16 = ~ip_csum_fold (sum0);
632 ip46_fix_len_and_csum (vlib_main_t * vm, int l4_offset, u16 data_len,
633 vlib_buffer_t * b0, int is_ip6)
636 data_len + sizeof (icmp46_header_t) + offsetof (icmp46_echo_request_t,
638 u16 total_length = payload_length + l4_offset;
639 icmp46_header_t *icmp46 = vlib_buffer_get_current (b0) + l4_offset;
640 icmp46->checksum = 0;
644 ip6_header_t *ip6 = vlib_buffer_get_current (b0);
645 ip6->payload_length = clib_host_to_net_u16 (payload_length);
647 int bogus_length = 0;
649 ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip6, &bogus_length);
653 ip4_header_t *ip4 = vlib_buffer_get_current (b0);
654 ip4->length = clib_host_to_net_u16 (total_length);
656 ip4->checksum = ip4_header_checksum (ip4);
657 icmp46->checksum = ip4_icmp_compute_checksum (vm, b0, ip4);
662 at_most_a_frame (u32 count)
664 return count > VLIB_FRAME_SIZE ? VLIB_FRAME_SIZE : count;
668 ip46_enqueue_packet (vlib_main_t * vm, vlib_buffer_t * b0, u32 burst,
672 u32 lookup_node_index =
673 is_ip6 ? ip6_lookup_node.index : ip4_lookup_node.index;
679 * Enqueue the packet, possibly as one or more frames of copies to make
680 * bursts. We enqueue b0 as the very last buffer, when there is no possibility
681 * for error in vlib_buffer_copy, so as to allow the caller to free it
682 * in case we encounter the error in the middle of the loop.
684 for (n_to_send = at_most_a_frame (burst), burst -= n_to_send; n_to_send > 0;
685 n_to_send = at_most_a_frame (burst), burst -= n_to_send)
687 f = vlib_get_frame_to_node (vm, lookup_node_index);
688 /* f can not be NULL here - frame allocation failure causes panic */
690 u32 *to_next = vlib_frame_vector_args (f);
691 f->n_vectors = n_to_send;
693 while (n_to_send > 1)
695 vlib_buffer_t *b0copy = vlib_buffer_copy (vm, b0);
696 if (PREDICT_FALSE (b0copy == NULL))
698 *to_next++ = vlib_get_buffer_index (vm, b0copy);
703 /* n_to_send is guaranteed to equal 1 here */
706 /* not the last burst, so still make a copy for the last buffer */
707 vlib_buffer_t *b0copy = vlib_buffer_copy (vm, b0);
708 if (PREDICT_FALSE (b0copy == NULL))
711 *to_next++ = vlib_get_buffer_index (vm, b0copy);
715 /* put the original buffer as the last one of an error-free run */
716 *to_next++ = vlib_get_buffer_index (vm, b0);
718 vlib_put_frame_to_node (vm, lookup_node_index, f);
719 n_sent += f->n_vectors;
723 * We reach here in case we already enqueued one or more buffers
724 * and maybe one or more frames but could not make more copies.
725 * There is an outstanding frame - so ship it and return.
726 * Caller will have to free the b0 in this case, since
727 * we did not enqueue it here yet.
730 n_sent += f->n_vectors;
731 vlib_put_frame_to_node (vm, lookup_node_index, f);
737 * An address-family agnostic ping send function.
740 #define ERROR_OUT(e) do { err = e; goto done; } while (0)
742 static send_ip46_ping_result_t
743 send_ip46_ping (vlib_main_t * vm,
745 ip46_address_t * pa46,
747 u16 seq_host, u16 id_host, u16 data_len, u32 burst,
748 u8 verbose, int is_ip6)
750 int err = SEND_PING_OK;
755 n_buf0 = vlib_buffer_alloc (vm, &bi0, 1);
757 ERROR_OUT (SEND_PING_ALLOC_FAIL);
759 b0 = vlib_get_buffer (vm, bi0);
760 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
763 * if the user did not provide a source interface,
764 * perform a resolution and use an interface
765 * via which it succeeds.
768 if (~0 == sw_if_index)
770 fib_index = ip46_fib_index_from_table_id (table_id, is_ip6);
771 sw_if_index = ip46_get_resolving_interface (fib_index, pa46, is_ip6);
775 ip46_fib_table_get_index_for_sw_if_index (sw_if_index, is_ip6);
778 ERROR_OUT (SEND_PING_NO_TABLE);
779 if (~0 == sw_if_index)
780 ERROR_OUT (SEND_PING_NO_INTERFACE);
782 vnet_buffer (b0)->sw_if_index[VLIB_RX] = sw_if_index;
783 vnet_buffer (b0)->sw_if_index[VLIB_TX] = fib_index;
785 int l4_header_offset = ip46_fill_l3_header (pa46, b0, is_ip6);
787 /* set the src address in the buffer */
788 if (!ip46_set_src_address (sw_if_index, b0, is_ip6))
789 ERROR_OUT (SEND_PING_NO_SRC_ADDRESS);
791 ip46_print_buffer_src_address (vm, b0, is_ip6);
794 ip46_fill_icmp_request_at (vm, l4_header_offset, seq_host, id_host,
795 data_len, b0, is_ip6);
797 ip46_fix_len_and_csum (vm, l4_header_offset, data_len, b0, is_ip6);
799 int n_sent = ip46_enqueue_packet (vm, b0, burst, is_ip6);
801 err = SEND_PING_NO_BUFFERS;
804 if (err != SEND_PING_OK)
807 vlib_buffer_free (vm, &bi0, 1);
812 static send_ip46_ping_result_t
813 send_ip6_ping (vlib_main_t * vm,
814 u32 table_id, ip6_address_t * pa6,
815 u32 sw_if_index, u16 seq_host, u16 id_host, u16 data_len,
816 u32 burst, u8 verbose)
818 ip46_address_t target;
820 return send_ip46_ping (vm, table_id, &target, sw_if_index, seq_host,
821 id_host, data_len, burst, verbose, 1 /* is_ip6 */ );
824 static send_ip46_ping_result_t
825 send_ip4_ping (vlib_main_t * vm,
826 u32 table_id, ip4_address_t * pa4,
827 u32 sw_if_index, u16 seq_host, u16 id_host, u16 data_len,
828 u32 burst, u8 verbose)
830 ip46_address_t target;
831 ip46_address_set_ip4 (&target, pa4);
832 return send_ip46_ping (vm, table_id, &target, sw_if_index, seq_host,
833 id_host, data_len, burst, verbose, 0 /* is_ip6 */ );
837 print_ip46_icmp_reply (vlib_main_t * vm, u32 bi0, int is_ip6)
839 vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
842 void *format_addr_func;
847 ip6_header_t *ip6 = vlib_buffer_get_current (b0);
848 paddr = (void *) &ip6->src_address;
849 format_addr_func = (void *) format_ip6_address;
850 ttl = ip6->hop_limit;
851 l4_offset = sizeof (ip6_header_t); // FIXME - EH processing ?
852 payload_length = clib_net_to_host_u16 (ip6->payload_length);
856 ip4_header_t *ip4 = vlib_buffer_get_current (b0);
857 paddr = (void *) &ip4->src_address;
858 format_addr_func = (void *) format_ip4_address;
860 l4_offset = ip4_header_bytes (ip4);
862 clib_net_to_host_u16 (ip4->length) + ip4_header_bytes (ip4);
864 icmp46_header_t *icmp = vlib_buffer_get_current (b0) + l4_offset;
865 icmp46_echo_request_t *icmp_echo = (icmp46_echo_request_t *) (icmp + 1);
866 u64 *dataplane_ts = (u64 *) & vnet_buffer (b0)->unused[0];
868 f64 clocks_per_second = ((f64) vm->clib_time.clocks_per_second);
870 ((f64) (*dataplane_ts - icmp_echo->time_sent)) / clocks_per_second;
873 "%d bytes from %U: icmp_seq=%d ttl=%d time=%.4f ms",
877 clib_host_to_net_u16 (icmp_echo->seq), ttl, rtt * 1000.0);
881 * Perform the ping run with the given parameters in the current CLI process.
882 * Depending on whether pa4 or pa6 is set, runs IPv4 or IPv6 ping.
883 * The amusing side effect is of course if both are set, then both pings are sent.
884 * This behavior can be used to ping a dualstack host over IPv4 and IPv6 at once.
888 run_ping_ip46_address (vlib_main_t * vm, u32 table_id, ip4_address_t * pa4,
889 ip6_address_t * pa6, u32 sw_if_index,
890 f64 ping_interval, u32 ping_repeat, u32 data_len,
891 u32 ping_burst, u32 verbose)
894 uword curr_proc = vlib_current_process (vm);
899 static u32 rand_seed = 0;
901 if (PREDICT_FALSE (!rand_seed))
902 rand_seed = random_default_seed ();
904 icmp_id = random_u32 (&rand_seed) & 0xffff;
906 while (~0 != get_cli_process_id_by_icmp_id_mt (vm, icmp_id))
908 vlib_cli_output (vm, "ICMP ID collision at %d, incrementing", icmp_id);
912 set_cli_process_id_by_icmp_id_mt (vm, icmp_id, curr_proc);
914 for (i = 1; i <= ping_repeat; i++)
916 send_ip46_ping_result_t res = SEND_PING_OK;
918 f64 time_ping_sent = vlib_time_now (vm);
921 res = send_ip6_ping (vm, table_id,
922 pa6, sw_if_index, i, icmp_id,
923 data_len, ping_burst, verbose);
924 if (SEND_PING_OK == res)
925 n_requests += ping_burst;
927 vlib_cli_output (vm, "Failed: %U", format_ip46_ping_result, res);
931 res = send_ip4_ping (vm, table_id, pa4,
932 sw_if_index, i, icmp_id, data_len,
933 ping_burst, verbose);
934 if (SEND_PING_OK == res)
935 n_requests += ping_burst;
937 vlib_cli_output (vm, "Failed: %U", format_ip46_ping_result, res);
940 /* Collect and print the responses until it is time to send a next ping */
942 while ((i <= ping_repeat)
945 time_ping_sent + ping_interval - vlib_time_now (vm)) > 0.0))
947 uword event_type, *event_data = 0;
948 vlib_process_wait_for_event_or_clock (vm, sleep_interval);
949 event_type = vlib_process_get_events (vm, &event_data);
952 case ~0: /* no events => timeout */
954 case PING_RESPONSE_IP6:
956 case PING_RESPONSE_IP4:
959 int is_ip6 = (event_type == PING_RESPONSE_IP6);
960 for (ii = 0; ii < vec_len (event_data); ii++)
962 u32 bi0 = event_data[ii];
963 print_ip46_icmp_reply (vm, bi0, is_ip6);
966 vlib_buffer_free (vm, &bi0, 1);
971 /* someone pressed a key, abort */
972 vlib_cli_output (vm, "Aborted due to a keypress.");
976 vec_free (event_data);
980 vlib_cli_output (vm, "\n");
984 n_requests) ? 0 : 100.0 * ((float) n_requests -
985 (float) n_replies) / (float) n_requests;
987 "Statistics: %u sent, %u received, %f%% packet loss\n",
988 n_requests, n_replies, loss);
989 clear_cli_process_id_by_icmp_id_mt (vm, icmp_id);
995 static clib_error_t *
996 ping_ip_address (vlib_main_t * vm,
997 unformat_input_t * input, vlib_cli_command_t * cmd)
1001 clib_error_t *error = 0;
1002 u32 ping_repeat = 5;
1004 u8 ping_ip4, ping_ip6;
1005 vnet_main_t *vnm = vnet_get_main ();
1006 u32 data_len = PING_DEFAULT_DATA_LEN;
1008 f64 ping_interval = PING_DEFAULT_INTERVAL;
1009 u32 sw_if_index, table_id;
1012 ping_ip4 = ping_ip6 = 0;
1015 if (unformat (input, "%U", unformat_ip4_address, &a4))
1019 else if (unformat (input, "%U", unformat_ip6_address, &a6))
1023 else if (unformat (input, "ipv4"))
1025 if (unformat (input, "%U", unformat_ip4_address, &a4))
1032 clib_error_return (0,
1033 "expecting IPv4 address but got `%U'",
1034 format_unformat_error, input);
1037 else if (unformat (input, "ipv6"))
1039 if (unformat (input, "%U", unformat_ip6_address, &a6))
1046 clib_error_return (0,
1047 "expecting IPv6 address but got `%U'",
1048 format_unformat_error, input);
1054 clib_error_return (0,
1055 "expecting IP4/IP6 address `%U'. Usage: ping <addr> [source <intf>] [size <datasz>] [repeat <count>] [verbose]",
1056 format_unformat_error, input);
1060 /* allow for the second AF in the same ping */
1061 if (!ping_ip4 && (unformat (input, "ipv4")))
1063 if (unformat (input, "%U", unformat_ip4_address, &a4))
1068 else if (!ping_ip6 && (unformat (input, "ipv6")))
1070 if (unformat (input, "%U", unformat_ip6_address, &a6))
1076 /* parse the rest of the parameters in a cycle */
1077 while (!unformat_eof (input, NULL))
1079 if (unformat (input, "source"))
1082 (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
1085 clib_error_return (0,
1086 "unknown interface `%U'",
1087 format_unformat_error, input);
1091 else if (unformat (input, "size"))
1093 if (!unformat (input, "%u", &data_len))
1096 clib_error_return (0,
1097 "expecting size but got `%U'",
1098 format_unformat_error, input);
1101 if (data_len > PING_MAXIMUM_DATA_SIZE)
1104 clib_error_return (0,
1105 "%d is bigger than maximum allowed payload size %d",
1106 data_len, PING_MAXIMUM_DATA_SIZE);
1110 else if (unformat (input, "table-id"))
1112 if (!unformat (input, "%u", &table_id))
1115 clib_error_return (0,
1116 "expecting table-id but got `%U'",
1117 format_unformat_error, input);
1121 else if (unformat (input, "interval"))
1123 if (!unformat (input, "%f", &ping_interval))
1126 clib_error_return (0,
1127 "expecting interval (floating point number) got `%U'",
1128 format_unformat_error, input);
1132 else if (unformat (input, "repeat"))
1134 if (!unformat (input, "%u", &ping_repeat))
1137 clib_error_return (0,
1138 "expecting repeat count but got `%U'",
1139 format_unformat_error, input);
1143 else if (unformat (input, "burst"))
1145 if (!unformat (input, "%u", &ping_burst))
1148 clib_error_return (0,
1149 "expecting burst count but got `%U'",
1150 format_unformat_error, input);
1154 else if (unformat (input, "verbose"))
1160 error = clib_error_return (0, "unknown input `%U'",
1161 format_unformat_error, input);
1167 * Operationally, one won't (and shouldn't) need to send more than a frame worth of pings.
1168 * But it may be handy during the debugging.
1172 #define MAX_PING_BURST (10*VLIB_FRAME_SIZE)
1174 #define MAX_PING_BURST (VLIB_FRAME_SIZE)
1177 if (ping_burst < 1 || ping_burst > MAX_PING_BURST)
1178 return clib_error_return (0, "burst size must be between 1 and %u",
1181 run_ping_ip46_address (vm, table_id, ping_ip4 ? &a4 : NULL,
1182 ping_ip6 ? &a6 : NULL, sw_if_index, ping_interval,
1183 ping_repeat, data_len, ping_burst, verbose);
1189 * This command sends an ICMP ECHO_REQUEST to network hosts. The address
1190 * can be an IPv4 or IPv6 address (or both at the same time).
1194 * Example of how ping an IPv4 address:
1195 * @cliexstart{ping 172.16.1.2 source GigabitEthernet2/0/0 repeat 2}
1196 * 64 bytes from 172.16.1.2: icmp_seq=1 ttl=64 time=.1090 ms
1197 * 64 bytes from 172.16.1.2: icmp_seq=2 ttl=64 time=.0914 ms
1199 * Statistics: 2 sent, 2 received, 0% packet loss
1202 * Example of how ping both an IPv4 address and IPv6 address at the same time:
1203 * @cliexstart{ping 172.16.1.2 ipv6 fe80::24a5:f6ff:fe9c:3a36 source GigabitEthernet2/0/0 repeat 2 verbose}
1204 * Adjacency index: 10, sw_if_index: 1
1205 * Adj: ip6-discover-neighbor
1207 * Forced set interface: 1
1208 * Adjacency index: 0, sw_if_index: 4294967295
1211 * Forced set interface: 1
1212 * Source address: 172.16.1.1
1213 * 64 bytes from 172.16.1.2: icmp_seq=1 ttl=64 time=.1899 ms
1214 * Adjacency index: 10, sw_if_index: 1
1215 * Adj: ip6-discover-neighbor
1217 * Forced set interface: 1
1218 * Adjacency index: 0, sw_if_index: 4294967295
1221 * Forced set interface: 1
1222 * Source address: 172.16.1.1
1223 * 64 bytes from 172.16.1.2: icmp_seq=2 ttl=64 time=.0910 ms
1225 * Statistics: 4 sent, 2 received, 50% packet loss
1230 VLIB_CLI_COMMAND (ping_command, static) =
1233 .function = ping_ip_address,
1234 .short_help = "ping {<ip-addr> | ipv4 <ip4-addr> | ipv6 <ip6-addr>}"
1235 " [ipv4 <ip4-addr> | ipv6 <ip6-addr>] [source <interface>]"
1236 " [size <pktsize:60>] [interval <sec:1>] [repeat <cnt:5>] [table-id <id:0>]"
1237 " [burst <count:1>] [verbose]",
1242 static clib_error_t *
1243 ping_cli_init (vlib_main_t * vm)
1245 vlib_thread_main_t *tm = vlib_get_thread_main ();
1246 ping_main_t *pm = &ping_main;
1248 pm->ip6_main = &ip6_main;
1249 pm->ip4_main = &ip4_main;
1250 icmp6_register_type (vm, ICMP6_echo_reply, ip6_icmp_echo_reply_node.index);
1251 ip4_icmp_register_type (vm, ICMP4_echo_reply,
1252 ip4_icmp_echo_reply_node.index);
1253 if (tm->n_vlib_mains > 1)
1254 clib_spinlock_init (&pm->ping_run_check_lock);
1258 VLIB_INIT_FUNCTION (ping_cli_init);
1261 VLIB_PLUGIN_REGISTER () = {
1262 .version = VPP_BUILD_VER,
1263 .description = "Ping (ping)",
1268 * fd.io coding-style-patch-verification: ON
1271 * eval: (c-set-style "gnu")