blob: 92a92e50201d0b012070248e9cfa3a849846ed26 [file] [log] [blame]
Andrew Yourtchenko7e682202016-08-04 13:39:35 +00001/*
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
Andrew Yourtchenko61459c92017-01-28 15:31:19 +000016#include <stddef.h>
Andrew Yourtchenko7e682202016-08-04 13:39:35 +000017#include <vnet/ip/ping.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010018#include <vnet/fib/ip6_fib.h>
19#include <vnet/fib/ip4_fib.h>
20#include <vnet/fib/fib_entry.h>
Mohammed Hawari03a62132017-07-18 09:25:01 +020021#include <vlib/vlib.h>
Andrew Yourtchenko7e682202016-08-04 13:39:35 +000022
Dave Wallace71612d62017-10-24 01:32:41 -040023ping_main_t ping_main;
24
Billy McFall0683c9c2016-10-13 08:27:31 -040025/**
26 * @file
27 * @brief IPv4 and IPv6 ICMP Ping.
28 *
29 * This file contains code to suppport IPv4 or IPv6 ICMP ECHO_REQUEST to
30 * network hosts.
31 *
32 */
33
Andrew Yourtchenkocc8fa162018-10-26 15:13:56 +020034typedef struct
35{
36 u16 id;
37 u16 seq;
38 u32 cli_process_node;
39 u8 is_ip6;
40} icmp_echo_trace_t;
41
Billy McFall0683c9c2016-10-13 08:27:31 -040042
Andrew Yourtchenko7e682202016-08-04 13:39:35 +000043u8 *
Alexander Popovskyc90dfdc2016-12-04 02:39:38 -080044format_icmp_echo_trace (u8 * s, va_list * va)
Andrew Yourtchenko7e682202016-08-04 13:39:35 +000045{
46 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
47 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
Alexander Popovskyc90dfdc2016-12-04 02:39:38 -080048 icmp_echo_trace_t *t = va_arg (*va, icmp_echo_trace_t *);
Andrew Yourtchenko7e682202016-08-04 13:39:35 +000049
Andrew Yourtchenkocc8fa162018-10-26 15:13:56 +020050 s =
51 format (s, "ICMP%s echo id %d seq %d", t->is_ip6 ? "6" : "4", t->id,
52 t->seq);
53 if (t->cli_process_node == PING_CLI_UNKNOWN_NODE)
54 {
55 s = format (s, " (unknown)");
56 }
57 else
58 {
59 s = format (s, " send to cli node %d", t->cli_process_node);
60 }
Andrew Yourtchenko7e682202016-08-04 13:39:35 +000061
62 return s;
63}
64
Andrew Yourtchenkocc8fa162018-10-26 15:13:56 +020065
Neale Rannse9239c92018-10-15 05:47:58 -070066static u8 *
67format_ip46_ping_result (u8 * s, va_list * args)
68{
69 send_ip46_ping_result_t res = va_arg (*args, send_ip46_ping_result_t);
70
71 switch (res)
72 {
Andrew Yourtchenkocc8fa162018-10-26 15:13:56 +020073#define _(v, n) case SEND_PING_##v: s = format(s, "%s", n);break;
Neale Rannse9239c92018-10-15 05:47:58 -070074 foreach_ip46_ping_result
75#undef _
76 }
77
78 return (s);
79}
80
Andrew Yourtchenkocc8fa162018-10-26 15:13:56 +020081
Andrew Yourtchenko7e682202016-08-04 13:39:35 +000082/*
Andrew Yourtchenkocc8fa162018-10-26 15:13:56 +020083 * Poor man's get-set-clear functions
84 * for manipulation of icmp_id -> cli_process_id
85 * mappings.
86 *
87 * There should normally be very few (0..1..2) of these
88 * mappings, so the linear search is a good strategy.
89 *
90 * Make them thread-safe via a simple spinlock.
Andrew Yourtchenko7e682202016-08-04 13:39:35 +000091 *
92 */
93
Andrew Yourtchenkocc8fa162018-10-26 15:13:56 +020094
95static_always_inline uword
96get_cli_process_id_by_icmp_id_mt (vlib_main_t * vm, u16 icmp_id)
Andrew Yourtchenko7e682202016-08-04 13:39:35 +000097{
98 ping_main_t *pm = &ping_main;
Andrew Yourtchenkocc8fa162018-10-26 15:13:56 +020099 uword cli_process_id = PING_CLI_UNKNOWN_NODE;
100 ping_run_t *pr;
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000101
Andrew Yourtchenkocc8fa162018-10-26 15:13:56 +0200102 clib_spinlock_lock_if_init (&pm->ping_run_check_lock);
103 vec_foreach (pr, pm->active_ping_runs)
104 {
105 if (pr->icmp_id == icmp_id)
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000106 {
Andrew Yourtchenkocc8fa162018-10-26 15:13:56 +0200107 cli_process_id = pr->cli_process_id;
108 break;
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000109 }
Andrew Yourtchenkocc8fa162018-10-26 15:13:56 +0200110 }
111 clib_spinlock_unlock_if_init (&pm->ping_run_check_lock);
112 return cli_process_id;
113}
114
115
116static_always_inline void
117set_cli_process_id_by_icmp_id_mt (vlib_main_t * vm, u16 icmp_id,
118 uword cli_process_id)
119{
120 ping_main_t *pm = &ping_main;
121 ping_run_t *pr;
122
123 clib_spinlock_lock_if_init (&pm->ping_run_check_lock);
124 vec_foreach (pr, pm->active_ping_runs)
125 {
126 if (pr->icmp_id == icmp_id)
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000127 {
Andrew Yourtchenkocc8fa162018-10-26 15:13:56 +0200128 pr->cli_process_id = cli_process_id;
129 goto have_found_and_set;
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000130 }
Andrew Yourtchenkocc8fa162018-10-26 15:13:56 +0200131 }
132 /* no such key yet - add a new one */
133 ping_run_t new_pr = {.icmp_id = icmp_id,.cli_process_id = cli_process_id };
134 vec_add1 (pm->active_ping_runs, new_pr);
135have_found_and_set:
136 clib_spinlock_unlock_if_init (&pm->ping_run_check_lock);
137}
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000138
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000139
Andrew Yourtchenkocc8fa162018-10-26 15:13:56 +0200140static_always_inline void
141clear_cli_process_id_by_icmp_id_mt (vlib_main_t * vm, u16 icmp_id)
142{
143 ping_main_t *pm = &ping_main;
144 ping_run_t *pr;
145
146 clib_spinlock_lock_if_init (&pm->ping_run_check_lock);
147 vec_foreach (pr, pm->active_ping_runs)
148 {
149 if (pr->icmp_id == icmp_id)
150 {
151 vec_del1 (pm->active_ping_runs, pm->active_ping_runs - pr);
152 break;
153 }
154 }
155 clib_spinlock_unlock_if_init (&pm->ping_run_check_lock);
156}
157
158static_always_inline int
159ip46_get_icmp_id_and_seq (vlib_main_t * vm, vlib_buffer_t * b0,
160 u16 * out_icmp_id, u16 * out_icmp_seq, int is_ip6)
161{
162 int l4_offset;
163 if (is_ip6)
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000164 {
Andrew Yourtchenkocc8fa162018-10-26 15:13:56 +0200165 ip6_header_t *ip6 = vlib_buffer_get_current (b0);
166 if (ip6->protocol != IP_PROTOCOL_ICMP6)
167 {
168 return 0;
169 }
170 l4_offset = sizeof (*ip6); // IPv6 EH
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000171 }
Andrew Yourtchenkocc8fa162018-10-26 15:13:56 +0200172 else
173 {
174 ip4_header_t *ip4 = vlib_buffer_get_current (b0);
175 l4_offset = ip4_header_bytes (ip4);
176
177 }
178 icmp46_header_t *icmp46 = vlib_buffer_get_current (b0) + l4_offset;
179 icmp46_echo_request_t *icmp46_echo = (icmp46_echo_request_t *) (icmp46 + 1);
180
181 *out_icmp_id = clib_net_to_host_u16 (icmp46_echo->id);
182 *out_icmp_seq = clib_net_to_host_u16 (icmp46_echo->seq);
Alexander Popovskyc90dfdc2016-12-04 02:39:38 -0800183 return 1;
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000184}
185
186/*
Andrew Yourtchenkocc8fa162018-10-26 15:13:56 +0200187 * post the buffer to a given cli process node - the caller should forget bi0 after return.
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000188 */
Andrew Yourtchenkocc8fa162018-10-26 15:13:56 +0200189
190static_always_inline void
191ip46_post_icmp_reply_event (vlib_main_t * vm, uword cli_process_id, u32 bi0,
192 int is_ip6)
193{
194 vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
195 u64 nowts = clib_cpu_time_now ();
196
197 /* Pass the timestamp to the cli_process thanks to the vnet_buffer unused metadata field */
198
199 /* Camping on unused data... just ensure statically that there is enough space */
200 STATIC_ASSERT (ARRAY_LEN (vnet_buffer (b0)->unused) *
201 sizeof (vnet_buffer (b0)->unused[0]) > sizeof (nowts),
202 "ping reply timestamp fits within remaining space of vnet_buffer unused data");
203 u64 *pnowts = (void *) &vnet_buffer (b0)->unused[0];
204 *pnowts = nowts;
205
206 u32 event_id = is_ip6 ? PING_RESPONSE_IP6 : PING_RESPONSE_IP4;
207 vlib_process_signal_event_mt (vm, cli_process_id, event_id, bi0);
208}
209
210
211static_always_inline void
212ip46_echo_reply_maybe_trace_buffer (vlib_main_t * vm,
213 vlib_node_runtime_t * node,
214 uword cli_process_id, u16 id, u16 seq,
215 vlib_buffer_t * b0, int is_ip6)
216{
217 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
218 {
219 icmp_echo_trace_t *tr = vlib_add_trace (vm, node, b0, sizeof (*tr));
220 tr->id = id;
221 tr->seq = seq;
222 tr->cli_process_node = cli_process_id;
223 tr->is_ip6 = is_ip6;
224 }
225}
226
227
228static_always_inline uword
229ip46_icmp_echo_reply_inner_node_fn (vlib_main_t * vm,
230 vlib_node_runtime_t * node,
231 vlib_frame_t * frame, int do_trace,
232 int is_ip6)
233{
234 u32 n_left_from, *from, *to_next;
235 icmp46_echo_reply_next_t next_index;
236
237 from = vlib_frame_vector_args (frame);
238 n_left_from = frame->n_vectors;
239
240 next_index = node->cached_next_index;
241
242 while (n_left_from > 0)
243 {
244 u32 n_left_to_next;
245 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
246
247 while (n_left_from > 0 && n_left_to_next > 0)
248 {
249 u32 bi0;
250 vlib_buffer_t *b0;
251 /*
252 * The buffers (replies) are either posted to the CLI thread
253 * awaiting for them for subsequent analysis and disposal,
254 * or are sent to the punt node.
255 *
256 * So the only "next" node is a punt, normally.
257 */
258 u32 next0 = ICMP46_ECHO_REPLY_NEXT_PUNT;
259
260 bi0 = from[0];
261 b0 = vlib_get_buffer (vm, bi0);
262 from += 1;
263 n_left_from -= 1;
264
265 u16 icmp_id = ~0;
266 u16 icmp_seq = ~0;
267 uword cli_process_id = PING_CLI_UNKNOWN_NODE;
268
269 if (ip46_get_icmp_id_and_seq (vm, b0, &icmp_id, &icmp_seq, is_ip6))
270 {
271 cli_process_id = get_cli_process_id_by_icmp_id_mt (vm, icmp_id);
272 }
273
274 if (do_trace)
275 ip46_echo_reply_maybe_trace_buffer (vm, node, cli_process_id,
276 icmp_id, icmp_seq, b0,
277 is_ip6);
278
279 if (~0 == cli_process_id)
280 {
281 /* no outstanding requests for this reply, punt */
282 /* speculatively enqueue b0 to the current next frame */
283 to_next[0] = bi0;
284 to_next += 1;
285 n_left_to_next -= 1;
286 /* verify speculative enqueue, maybe switch current next frame */
287 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
288 to_next, n_left_to_next,
289 bi0, next0);
290 }
291 else
292 {
293 /* Post the buffer to CLI thread. It will take care of freeing it. */
294 ip46_post_icmp_reply_event (vm, cli_process_id, bi0, is_ip6);
295 }
296 }
297 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
298 }
299 return frame->n_vectors;
300}
301
302/*
303 * select "with-trace" or "without-trace" codepaths upfront.
304 */
305static_always_inline uword
306ip46_icmp_echo_reply_outer_node_fn (vlib_main_t * vm,
307 vlib_node_runtime_t * node,
308 vlib_frame_t * frame, int is_ip6)
309{
310 if (node->flags & VLIB_NODE_FLAG_TRACE)
311 return ip46_icmp_echo_reply_inner_node_fn (vm, node, frame,
312 1 /* do_trace */ , is_ip6);
313 else
314 return ip46_icmp_echo_reply_inner_node_fn (vm, node, frame,
315 0 /* do_trace */ , is_ip6);
316}
317
318static uword
319ip4_icmp_echo_reply_node_fn (vlib_main_t * vm,
320 vlib_node_runtime_t * node, vlib_frame_t * frame)
321{
322 return ip46_icmp_echo_reply_outer_node_fn (vm, node, frame,
323 0 /* is_ip6 */ );
324}
325
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000326static uword
327ip6_icmp_echo_reply_node_fn (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500328 vlib_node_runtime_t * node, vlib_frame_t * frame)
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000329{
Andrew Yourtchenkocc8fa162018-10-26 15:13:56 +0200330 return ip46_icmp_echo_reply_outer_node_fn (vm, node, frame,
331 1 /* is_ip6 */ );
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000332}
333
334/* *INDENT-OFF* */
335VLIB_REGISTER_NODE (ip6_icmp_echo_reply_node, static) =
336{
337 .function = ip6_icmp_echo_reply_node_fn,
338 .name = "ip6-icmp-echo-reply",
339 .vector_size = sizeof (u32),
Alexander Popovskyc90dfdc2016-12-04 02:39:38 -0800340 .format_trace = format_icmp_echo_trace,
Andrew Yourtchenkocc8fa162018-10-26 15:13:56 +0200341 .n_next_nodes = ICMP46_ECHO_REPLY_N_NEXT,
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000342 .next_nodes = {
Andrew Yourtchenkocc8fa162018-10-26 15:13:56 +0200343 [ICMP46_ECHO_REPLY_NEXT_DROP] = "ip6-drop",
344 [ICMP46_ECHO_REPLY_NEXT_PUNT] = "ip6-punt",
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000345 },
346};
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000347
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000348VLIB_REGISTER_NODE (ip4_icmp_echo_reply_node, static) =
349{
350 .function = ip4_icmp_echo_reply_node_fn,
351 .name = "ip4-icmp-echo-reply",
352 .vector_size = sizeof (u32),
Alexander Popovskyc90dfdc2016-12-04 02:39:38 -0800353 .format_trace = format_icmp_echo_trace,
Andrew Yourtchenkocc8fa162018-10-26 15:13:56 +0200354 .n_next_nodes = ICMP46_ECHO_REPLY_N_NEXT,
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000355 .next_nodes = {
Andrew Yourtchenkocc8fa162018-10-26 15:13:56 +0200356 [ICMP46_ECHO_REPLY_NEXT_DROP] = "ip4-drop",
357 [ICMP46_ECHO_REPLY_NEXT_PUNT] = "ip4-punt",
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000358 },
359};
360/* *INDENT-ON* */
361
Andrew Yourtchenkocc8fa162018-10-26 15:13:56 +0200362
363/*
364 * A swarm of address-family agnostic helper functions
365 * for building and sending the ICMP echo request.
366 *
367 * Deliberately mostly "static" rather than "static inline"
368 * so one can trace them sanely if needed in debugger, if needed.
369 *
370 */
371
372static_always_inline u8
373get_icmp_echo_payload_byte (int offset)
374{
375 return (offset % 256);
376}
377
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000378/* Fill in the ICMP ECHO structure, return the safety-checked and possibly shrunk data_len */
379static u16
Andrew Yourtchenkocc8fa162018-10-26 15:13:56 +0200380init_icmp46_echo_request (vlib_main_t * vm, vlib_buffer_t * b0,
381 int l4_header_offset,
382 icmp46_echo_request_t * icmp46_echo, u16 seq_host,
383 u16 id_host, u64 now, u16 data_len)
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000384{
385 int i;
Andrew Yourtchenkocc8fa162018-10-26 15:13:56 +0200386
387
388 int l34_len =
389 l4_header_offset + sizeof (icmp46_header_t) +
390 offsetof (icmp46_echo_request_t, data);
391 int max_data_len = VLIB_BUFFER_DATA_SIZE - l34_len;
392
393 int first_buf_data_len = data_len < max_data_len ? data_len : max_data_len;
394
395 int payload_offset = 0;
396 for (i = 0; i < first_buf_data_len; i++)
397 icmp46_echo->data[i] = get_icmp_echo_payload_byte (payload_offset++);
398
399 /* inspired by vlib_buffer_add_data */
400 vlib_buffer_t *hb = b0;
401 int remaining_data_len = data_len - first_buf_data_len;
402 while (remaining_data_len)
403 {
404 int this_buf_data_len =
405 remaining_data_len <
406 VLIB_BUFFER_DATA_SIZE ? remaining_data_len : VLIB_BUFFER_DATA_SIZE;
Damjan Marion36eb7c22019-01-18 20:45:30 +0100407 int n_alloc = vlib_buffer_alloc (vm, &b0->next_buffer, 1);
Andrew Yourtchenkocc8fa162018-10-26 15:13:56 +0200408 if (n_alloc < 1)
409 {
410 /* That is how much we have so far - return it... */
411 return (data_len - remaining_data_len);
412 }
413 b0->flags |= VLIB_BUFFER_NEXT_PRESENT;
414 /* move on to the newly acquired buffer */
415 b0 = vlib_get_buffer (vm, b0->next_buffer);
416 /* initialize the data */
417 for (i = 0; i < this_buf_data_len; i++)
418 {
419 b0->data[i] = get_icmp_echo_payload_byte (payload_offset++);
420 }
421 b0->current_length = this_buf_data_len;
422 b0->current_data = 0;
423 remaining_data_len -= this_buf_data_len;
424 }
425 hb->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
426 hb->current_length = l34_len + first_buf_data_len;
427 hb->total_length_not_including_first_buffer = data_len - first_buf_data_len;
428
429 icmp46_echo->time_sent = now;
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000430 icmp46_echo->seq = clib_host_to_net_u16 (seq_host);
431 icmp46_echo->id = clib_host_to_net_u16 (id_host);
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000432 return data_len;
433}
434
Andrew Yourtchenkocc8fa162018-10-26 15:13:56 +0200435
436static u32
437ip46_fib_index_from_table_id (u32 table_id, int is_ip6)
438{
439 u32 fib_index = is_ip6 ?
440 ip6_fib_index_from_table_id (table_id) :
441 ip4_fib_index_from_table_id (table_id);
442 return fib_index;
443}
444
445static fib_node_index_t
446ip46_fib_table_lookup_host (u32 fib_index, ip46_address_t * pa46, int is_ip6)
447{
448 fib_node_index_t fib_entry_index = is_ip6 ?
449 ip6_fib_table_lookup (fib_index, &pa46->ip6, 128) :
450 ip4_fib_table_lookup (ip4_fib_get (fib_index), &pa46->ip4, 32);
451 return fib_entry_index;
452}
453
454static u32
455ip46_get_resolving_interface (u32 fib_index, ip46_address_t * pa46,
456 int is_ip6)
457{
458 u32 sw_if_index = ~0;
459 if (~0 != fib_index)
460 {
461 fib_node_index_t fib_entry_index;
462 fib_entry_index = ip46_fib_table_lookup_host (fib_index, pa46, is_ip6);
463 sw_if_index = fib_entry_get_resolving_interface (fib_entry_index);
464 }
465 return sw_if_index;
466}
467
468static u32
469ip46_fib_table_get_index_for_sw_if_index (u32 sw_if_index, int is_ip6)
470{
471 u32 fib_table_index = is_ip6 ?
472 ip6_fib_table_get_index_for_sw_if_index (sw_if_index) :
473 ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
474 return fib_table_index;
475
476}
477
478
479static int
480ip46_fill_l3_header (ip46_address_t * pa46, vlib_buffer_t * b0, int is_ip6)
481{
482 if (is_ip6)
483 {
484 ip6_header_t *ip6 = vlib_buffer_get_current (b0);
485 /* Fill in ip6 header fields */
486 ip6->ip_version_traffic_class_and_flow_label =
487 clib_host_to_net_u32 (0x6 << 28);
488 ip6->payload_length = 0; /* will be set later */
489 ip6->protocol = IP_PROTOCOL_ICMP6;
490 ip6->hop_limit = 255;
491 ip6->dst_address = pa46->ip6;
492 ip6->src_address = pa46->ip6;
493 return (sizeof (ip6_header_t));
494 }
495 else
496 {
497 ip4_header_t *ip4 = vlib_buffer_get_current (b0);
498 /* Fill in ip4 header fields */
499 ip4->checksum = 0;
500 ip4->ip_version_and_header_length = 0x45;
501 ip4->tos = 0;
502 ip4->length = 0; /* will be set later */
503 ip4->fragment_id = 0;
504 ip4->flags_and_fragment_offset = 0;
505 ip4->ttl = 0xff;
506 ip4->protocol = IP_PROTOCOL_ICMP;
507 ip4->src_address = pa46->ip4;
508 ip4->dst_address = pa46->ip4;
509 return (sizeof (ip4_header_t));
510 }
511}
512
513static int
514ip46_set_src_address (u32 sw_if_index, vlib_buffer_t * b0, int is_ip6)
515{
516 int res;
517 if (is_ip6)
518 {
519 ip6_main_t *im = &ip6_main;
520 ip6_header_t *ip6 = vlib_buffer_get_current (b0);
521 res =
522 ip6_src_address_for_packet (&im->lookup_main, sw_if_index,
523 &ip6->dst_address, &ip6->src_address);
524 }
525 else
526 {
527 ip4_main_t *im = &ip4_main;
528 ip4_header_t *ip4 = vlib_buffer_get_current (b0);
529 res =
530 ip4_src_address_for_packet (&im->lookup_main, sw_if_index,
531 &ip4->src_address);
532 /* IP4 and IP6 paths have the inverse logic. Harmonize. */
533 res = !res;
534 }
535 return res;
536}
537
538static void
539ip46_print_buffer_src_address (vlib_main_t * vm, vlib_buffer_t * b0,
540 int is_ip6)
541{
542 void *format_addr_func;
543 void *paddr;
544 if (is_ip6)
545 {
546 ip6_header_t *ip6 = vlib_buffer_get_current (b0);
547 format_addr_func = format_ip6_address;
548 paddr = &ip6->src_address;
549 }
550 else
551 {
552 ip4_header_t *ip4 = vlib_buffer_get_current (b0);
553 format_addr_func = format_ip4_address;
554 paddr = &ip4->src_address;
555 }
556 vlib_cli_output (vm, "Source address: %U ", format_addr_func, paddr);
557}
558
559static u16
560ip46_fill_icmp_request_at (vlib_main_t * vm, int l4_offset, u16 seq_host,
561 u16 id_host, u16 data_len, vlib_buffer_t * b0,
562 int is_ip6)
563{
564 icmp46_header_t *icmp46 = vlib_buffer_get_current (b0) + l4_offset;
565
566 icmp46->type = is_ip6 ? ICMP6_echo_request : ICMP4_echo_request;
567 icmp46->code = 0;
568 icmp46->checksum = 0;
569
570 icmp46_echo_request_t *icmp46_echo = (icmp46_echo_request_t *) (icmp46 + 1);
571
572 data_len =
573 init_icmp46_echo_request (vm, b0, l4_offset, icmp46_echo, seq_host,
574 id_host, clib_cpu_time_now (), data_len);
575 return data_len;
576}
577
578
579/* Compute ICMP4 checksum with multibuffer support. */
580u16
581ip4_icmp_compute_checksum (vlib_main_t * vm, vlib_buffer_t * p0,
582 ip4_header_t * ip0)
583{
584 ip_csum_t sum0;
585 u32 ip_header_length, payload_length_host_byte_order;
586 u32 n_this_buffer, n_bytes_left, n_ip_bytes_this_buffer;
587 u16 sum16;
588 void *data_this_buffer;
589
590 ip_header_length = ip4_header_bytes (ip0);
591 payload_length_host_byte_order =
592 clib_net_to_host_u16 (ip0->length) - ip_header_length;
593
594 /* ICMP4 checksum does not include the IP header */
595 sum0 = 0;
596
597 n_bytes_left = n_this_buffer = payload_length_host_byte_order;
598 data_this_buffer = (void *) ip0 + ip_header_length;
599 n_ip_bytes_this_buffer =
600 p0->current_length - (((u8 *) ip0 - p0->data) - p0->current_data);
601 if (n_this_buffer + ip_header_length > n_ip_bytes_this_buffer)
602 {
603 n_this_buffer = n_ip_bytes_this_buffer > ip_header_length ?
604 n_ip_bytes_this_buffer - ip_header_length : 0;
605 }
606 while (1)
607 {
608 sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
609 n_bytes_left -= n_this_buffer;
610 if (n_bytes_left == 0)
611 break;
612
613 ASSERT (p0->flags & VLIB_BUFFER_NEXT_PRESENT);
614 p0 = vlib_get_buffer (vm, p0->next_buffer);
615 data_this_buffer = vlib_buffer_get_current (p0);
616 n_this_buffer = p0->current_length;
617 }
618
619 sum16 = ~ip_csum_fold (sum0);
620
621 return sum16;
622}
623
624
625static void
626ip46_fix_len_and_csum (vlib_main_t * vm, int l4_offset, u16 data_len,
627 vlib_buffer_t * b0, int is_ip6)
628{
629 u16 payload_length =
630 data_len + sizeof (icmp46_header_t) + offsetof (icmp46_echo_request_t,
631 data);
632 u16 total_length = payload_length + l4_offset;
633 icmp46_header_t *icmp46 = vlib_buffer_get_current (b0) + l4_offset;
634 icmp46->checksum = 0;
635
636 if (is_ip6)
637 {
638 ip6_header_t *ip6 = vlib_buffer_get_current (b0);
639 ip6->payload_length = clib_host_to_net_u16 (payload_length);
640
641 int bogus_length = 0;
642 icmp46->checksum =
643 ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip6, &bogus_length);
644 }
645 else
646 {
647 ip4_header_t *ip4 = vlib_buffer_get_current (b0);
648 ip4->length = clib_host_to_net_u16 (total_length);
649
650 ip4->checksum = ip4_header_checksum (ip4);
651 icmp46->checksum = ip4_icmp_compute_checksum (vm, b0, ip4);
652 }
653}
654
655static u16
656at_most_a_frame (u32 count)
657{
658 return count > VLIB_FRAME_SIZE ? VLIB_FRAME_SIZE : count;
659}
660
661static int
662ip46_enqueue_packet (vlib_main_t * vm, vlib_buffer_t * b0, u32 burst,
663 int is_ip6)
664{
665 vlib_frame_t *f = 0;
666 u32 lookup_node_index =
667 is_ip6 ? ip6_lookup_node.index : ip4_lookup_node.index;
668 int n_sent = 0;
669
670 u16 n_to_send;
671
672 /*
673 * Enqueue the packet, possibly as one or more frames of copies to make
674 * bursts. We enqueue b0 as the very last buffer, when there is no possibility
675 * for error in vlib_buffer_copy, so as to allow the caller to free it
676 * in case we encounter the error in the middle of the loop.
677 */
678 for (n_to_send = at_most_a_frame (burst), burst -= n_to_send; n_to_send > 0;
679 n_to_send = at_most_a_frame (burst), burst -= n_to_send)
680 {
681 f = vlib_get_frame_to_node (vm, lookup_node_index);
682 /* f can not be NULL here - frame allocation failure causes panic */
683
684 u32 *to_next = vlib_frame_vector_args (f);
685 f->n_vectors = n_to_send;
686
687 while (n_to_send > 1)
688 {
689 vlib_buffer_t *b0copy = vlib_buffer_copy (vm, b0);
690 if (PREDICT_FALSE (b0copy == NULL))
691 goto ship_and_ret;
692 *to_next++ = vlib_get_buffer_index (vm, b0copy);
693 n_to_send--;
694 n_sent++;
695 }
696
697 /* n_to_send is guaranteed to equal 1 here */
698 if (burst > 0)
699 {
700 /* not the last burst, so still make a copy for the last buffer */
701 vlib_buffer_t *b0copy = vlib_buffer_copy (vm, b0);
702 if (PREDICT_FALSE (b0copy == NULL))
703 goto ship_and_ret;
704 n_to_send--;
705 *to_next++ = vlib_get_buffer_index (vm, b0copy);
706 }
707 else
708 {
709 /* put the original buffer as the last one of an error-free run */
710 *to_next++ = vlib_get_buffer_index (vm, b0);
711 }
712 vlib_put_frame_to_node (vm, lookup_node_index, f);
713 n_sent += f->n_vectors;
714 }
715 return n_sent;
716 /*
717 * We reach here in case we already enqueued one or more buffers
718 * and maybe one or more frames but could not make more copies.
719 * There is an outstanding frame - so ship it and return.
720 * Caller will have to free the b0 in this case, since
721 * we did not enqueue it here yet.
722 */
723ship_and_ret:
724 n_sent += f->n_vectors;
725 vlib_put_frame_to_node (vm, lookup_node_index, f);
726 return n_sent;
727}
728
729
730/*
731 * An address-family agnostic ping send function.
732 */
733
734#define ERROR_OUT(e) do { err = e; goto done; } while (0)
735
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000736static send_ip46_ping_result_t
Andrew Yourtchenkocc8fa162018-10-26 15:13:56 +0200737send_ip46_ping (vlib_main_t * vm,
738 u32 table_id,
739 ip46_address_t * pa46,
740 u32 sw_if_index,
741 u16 seq_host, u16 id_host, u16 data_len, u32 burst,
742 u8 verbose, int is_ip6)
743{
744 int err = SEND_PING_OK;
745 u32 bi0 = 0;
746 int n_buf0 = 0;
747 vlib_buffer_t *b0;
748 vlib_buffer_free_list_t *fl;
749
750 n_buf0 = vlib_buffer_alloc (vm, &bi0, 1);
751 if (n_buf0 < 1)
752 ERROR_OUT (SEND_PING_ALLOC_FAIL);
753
754 b0 = vlib_get_buffer (vm, bi0);
755 fl = vlib_buffer_get_free_list (vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
756 vlib_buffer_init_for_free_list (b0, fl);
757 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
758
759 /*
760 * if the user did not provide a source interface,
761 * perform a resolution and use an interface
762 * via which it succeeds.
763 */
764 u32 fib_index;
765 if (~0 == sw_if_index)
766 {
767 fib_index = ip46_fib_index_from_table_id (table_id, is_ip6);
768 sw_if_index = ip46_get_resolving_interface (fib_index, pa46, is_ip6);
769 }
770 else
771 fib_index =
772 ip46_fib_table_get_index_for_sw_if_index (sw_if_index, is_ip6);
773
774 if (~0 == fib_index)
775 ERROR_OUT (SEND_PING_NO_TABLE);
776 if (~0 == sw_if_index)
777 ERROR_OUT (SEND_PING_NO_INTERFACE);
778
779 vnet_buffer (b0)->sw_if_index[VLIB_RX] = sw_if_index;
780 vnet_buffer (b0)->sw_if_index[VLIB_TX] = fib_index;
781
782 int l4_header_offset = ip46_fill_l3_header (pa46, b0, is_ip6);
783
784 /* set the src address in the buffer */
785 if (!ip46_set_src_address (sw_if_index, b0, is_ip6))
786 ERROR_OUT (SEND_PING_NO_SRC_ADDRESS);
787 if (verbose)
788 ip46_print_buffer_src_address (vm, b0, is_ip6);
789
790 data_len =
791 ip46_fill_icmp_request_at (vm, l4_header_offset, seq_host, id_host,
792 data_len, b0, is_ip6);
793
794 ip46_fix_len_and_csum (vm, l4_header_offset, data_len, b0, is_ip6);
795
796 int n_sent = ip46_enqueue_packet (vm, b0, burst, is_ip6);
797 if (n_sent < burst)
798 err = SEND_PING_NO_BUFFERS;
799
800done:
801 if (err != SEND_PING_OK)
802 {
803 if (n_buf0 > 0)
804 vlib_buffer_free (vm, &bi0, 1);
805 }
806 return err;
807}
808
809static send_ip46_ping_result_t
810send_ip6_ping (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500811 u32 table_id, ip6_address_t * pa6,
812 u32 sw_if_index, u16 seq_host, u16 id_host, u16 data_len,
Andrew Yourtchenko864b25d2017-03-22 10:51:14 +0100813 u32 burst, u8 verbose)
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000814{
Andrew Yourtchenkocc8fa162018-10-26 15:13:56 +0200815 ip46_address_t target;
816 target.ip6 = *pa6;
817 return send_ip46_ping (vm, table_id, &target, sw_if_index, seq_host,
818 id_host, data_len, burst, verbose, 1 /* is_ip6 */ );
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000819}
820
821static send_ip46_ping_result_t
822send_ip4_ping (vlib_main_t * vm,
Andrew Yourtchenkocc8fa162018-10-26 15:13:56 +0200823 u32 table_id, ip4_address_t * pa4,
824 u32 sw_if_index, u16 seq_host, u16 id_host, u16 data_len,
825 u32 burst, u8 verbose)
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000826{
Andrew Yourtchenkocc8fa162018-10-26 15:13:56 +0200827 ip46_address_t target;
828 ip46_address_set_ip4 (&target, pa4);
829 return send_ip46_ping (vm, table_id, &target, sw_if_index, seq_host,
830 id_host, data_len, burst, verbose, 0 /* is_ip6 */ );
831}
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000832
Andrew Yourtchenkocc8fa162018-10-26 15:13:56 +0200833static void
834print_ip46_icmp_reply (vlib_main_t * vm, u32 bi0, int is_ip6)
835{
836 vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
837 int l4_offset;
838 void *paddr;
839 void *format_addr_func;
840 u16 payload_length;
841 u8 ttl;
842 if (is_ip6)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500843 {
Andrew Yourtchenkocc8fa162018-10-26 15:13:56 +0200844 ip6_header_t *ip6 = vlib_buffer_get_current (b0);
845 paddr = (void *) &ip6->src_address;
846 format_addr_func = (void *) format_ip6_address;
847 ttl = ip6->hop_limit;
848 l4_offset = sizeof (ip6_header_t); // FIXME - EH processing ?
849 payload_length = clib_net_to_host_u16 (ip6->payload_length);
Neale Rannsf06aea52016-11-29 06:51:37 -0800850 }
851 else
852 {
Andrew Yourtchenkocc8fa162018-10-26 15:13:56 +0200853 ip4_header_t *ip4 = vlib_buffer_get_current (b0);
854 paddr = (void *) &ip4->src_address;
855 format_addr_func = (void *) format_ip4_address;
856 ttl = ip4->ttl;
857 l4_offset = ip4_header_bytes (ip4);
858 payload_length =
859 clib_net_to_host_u16 (ip4->length) + ip4_header_bytes (ip4);
Neale Rannsf06aea52016-11-29 06:51:37 -0800860 }
Andrew Yourtchenkocc8fa162018-10-26 15:13:56 +0200861 icmp46_header_t *icmp = vlib_buffer_get_current (b0) + l4_offset;
862 icmp46_echo_request_t *icmp_echo = (icmp46_echo_request_t *) (icmp + 1);
863 u64 *dataplane_ts = (u64 *) & vnet_buffer (b0)->unused[0];
Neale Rannsf06aea52016-11-29 06:51:37 -0800864
Andrew Yourtchenkocc8fa162018-10-26 15:13:56 +0200865 f64 clocks_per_second = ((f64) vm->clib_time.clocks_per_second);
866 f64 rtt =
867 ((f64) (*dataplane_ts - icmp_echo->time_sent)) / clocks_per_second;
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000868
869 vlib_cli_output (vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500870 "%d bytes from %U: icmp_seq=%d ttl=%d time=%.4f ms",
Andrew Yourtchenkocc8fa162018-10-26 15:13:56 +0200871 payload_length,
872 format_addr_func,
873 paddr,
874 clib_host_to_net_u16 (icmp_echo->seq), ttl, rtt * 1000.0);
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000875}
876
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000877/*
878 * Perform the ping run with the given parameters in the current CLI process.
879 * Depending on whether pa4 or pa6 is set, runs IPv4 or IPv6 ping.
880 * The amusing side effect is of course if both are set, then both pings are sent.
881 * This behavior can be used to ping a dualstack host over IPv4 and IPv6 at once.
882 */
883
884static void
Neale Rannsf06aea52016-11-29 06:51:37 -0800885run_ping_ip46_address (vlib_main_t * vm, u32 table_id, ip4_address_t * pa4,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500886 ip6_address_t * pa6, u32 sw_if_index,
887 f64 ping_interval, u32 ping_repeat, u32 data_len,
Andrew Yourtchenko864b25d2017-03-22 10:51:14 +0100888 u32 ping_burst, u32 verbose)
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000889{
890 int i;
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000891 uword curr_proc = vlib_current_process (vm);
892 u32 n_replies = 0;
893 u32 n_requests = 0;
Andrew Yourtchenkoacfb47d2016-09-14 15:51:16 +0000894 u16 icmp_id;
895
896 static u32 rand_seed = 0;
897
Dave Barachd7cb1b52016-12-09 09:52:16 -0500898 if (PREDICT_FALSE (!rand_seed))
899 rand_seed = random_default_seed ();
Andrew Yourtchenkoacfb47d2016-09-14 15:51:16 +0000900
Dave Barachd7cb1b52016-12-09 09:52:16 -0500901 icmp_id = random_u32 (&rand_seed) & 0xffff;
Andrew Yourtchenkoacfb47d2016-09-14 15:51:16 +0000902
Andrew Yourtchenkocc8fa162018-10-26 15:13:56 +0200903 while (~0 != get_cli_process_id_by_icmp_id_mt (vm, icmp_id))
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000904 {
905 vlib_cli_output (vm, "ICMP ID collision at %d, incrementing", icmp_id);
906 icmp_id++;
907 }
Andrew Yourtchenkocc8fa162018-10-26 15:13:56 +0200908
909 set_cli_process_id_by_icmp_id_mt (vm, icmp_id, curr_proc);
910
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000911 for (i = 1; i <= ping_repeat; i++)
912 {
Neale Rannse9239c92018-10-15 05:47:58 -0700913 send_ip46_ping_result_t res = SEND_PING_OK;
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000914 f64 sleep_interval;
915 f64 time_ping_sent = vlib_time_now (vm);
Neale Rannse9239c92018-10-15 05:47:58 -0700916 if (pa6)
917 {
Andrew Yourtchenkocc8fa162018-10-26 15:13:56 +0200918 res = send_ip6_ping (vm, table_id,
Neale Rannse9239c92018-10-15 05:47:58 -0700919 pa6, sw_if_index, i, icmp_id,
920 data_len, ping_burst, verbose);
Andrew Yourtchenkocc8fa162018-10-26 15:13:56 +0200921 if (SEND_PING_OK == res)
922 n_requests += ping_burst;
923 else
924 vlib_cli_output (vm, "Failed: %U", format_ip46_ping_result, res);
Neale Rannse9239c92018-10-15 05:47:58 -0700925 }
926 if (pa4)
927 {
Andrew Yourtchenkocc8fa162018-10-26 15:13:56 +0200928 res = send_ip4_ping (vm, table_id, pa4,
Neale Rannse9239c92018-10-15 05:47:58 -0700929 sw_if_index, i, icmp_id, data_len,
930 ping_burst, verbose);
Andrew Yourtchenkocc8fa162018-10-26 15:13:56 +0200931 if (SEND_PING_OK == res)
932 n_requests += ping_burst;
933 else
934 vlib_cli_output (vm, "Failed: %U", format_ip46_ping_result, res);
Neale Rannse9239c92018-10-15 05:47:58 -0700935 }
Andrew Yourtchenkocc8fa162018-10-26 15:13:56 +0200936
937 /* Collect and print the responses until it is time to send a next ping */
938
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000939 while ((i <= ping_repeat)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500940 &&
941 ((sleep_interval =
942 time_ping_sent + ping_interval - vlib_time_now (vm)) > 0.0))
943 {
944 uword event_type, *event_data = 0;
945 vlib_process_wait_for_event_or_clock (vm, sleep_interval);
946 event_type = vlib_process_get_events (vm, &event_data);
947 switch (event_type)
948 {
949 case ~0: /* no events => timeout */
950 break;
951 case PING_RESPONSE_IP6:
Andrew Yourtchenkocc8fa162018-10-26 15:13:56 +0200952 /* fall-through */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500953 case PING_RESPONSE_IP4:
954 {
Andrew Yourtchenkocc8fa162018-10-26 15:13:56 +0200955 int ii;
956 int is_ip6 = (event_type == PING_RESPONSE_IP6);
957 for (ii = 0; ii < vec_len (event_data); ii++)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500958 {
Andrew Yourtchenkocc8fa162018-10-26 15:13:56 +0200959 u32 bi0 = event_data[ii];
960 print_ip46_icmp_reply (vm, bi0, is_ip6);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500961 n_replies++;
962 if (0 != bi0)
Andrew Yourtchenkocc8fa162018-10-26 15:13:56 +0200963 vlib_buffer_free (vm, &bi0, 1);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500964 }
965 }
966 break;
967 default:
968 /* someone pressed a key, abort */
969 vlib_cli_output (vm, "Aborted due to a keypress.");
Andrew Yourtchenkocc8fa162018-10-26 15:13:56 +0200970 goto double_break;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500971 break;
972 }
Dave Barach69128d02017-09-26 10:54:34 -0400973 vec_free (event_data);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500974 }
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000975 }
Andrew Yourtchenkocc8fa162018-10-26 15:13:56 +0200976double_break:
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000977 vlib_cli_output (vm, "\n");
978 {
979 float loss =
980 (0 ==
981 n_requests) ? 0 : 100.0 * ((float) n_requests -
Dave Barachd7cb1b52016-12-09 09:52:16 -0500982 (float) n_replies) / (float) n_requests;
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000983 vlib_cli_output (vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500984 "Statistics: %u sent, %u received, %f%% packet loss\n",
985 n_requests, n_replies, loss);
Andrew Yourtchenkocc8fa162018-10-26 15:13:56 +0200986 clear_cli_process_id_by_icmp_id_mt (vm, icmp_id);
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000987 }
988}
989
990
991
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000992static clib_error_t *
993ping_ip_address (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500994 unformat_input_t * input, vlib_cli_command_t * cmd)
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000995{
996 ip4_address_t a4;
997 ip6_address_t a6;
998 clib_error_t *error = 0;
999 u32 ping_repeat = 5;
Andrew Yourtchenko864b25d2017-03-22 10:51:14 +01001000 u32 ping_burst = 1;
Andrew Yourtchenko7e682202016-08-04 13:39:35 +00001001 u8 ping_ip4, ping_ip6;
1002 vnet_main_t *vnm = vnet_get_main ();
1003 u32 data_len = PING_DEFAULT_DATA_LEN;
1004 u32 verbose = 0;
1005 f64 ping_interval = PING_DEFAULT_INTERVAL;
Neale Rannsf06aea52016-11-29 06:51:37 -08001006 u32 sw_if_index, table_id;
1007
1008 table_id = 0;
Andrew Yourtchenko7e682202016-08-04 13:39:35 +00001009 ping_ip4 = ping_ip6 = 0;
Andrew Yourtchenko7e682202016-08-04 13:39:35 +00001010 sw_if_index = ~0;
Neale Rannsf06aea52016-11-29 06:51:37 -08001011
Andrew Yourtchenko7e682202016-08-04 13:39:35 +00001012 if (unformat (input, "%U", unformat_ip4_address, &a4))
1013 {
1014 ping_ip4 = 1;
1015 }
1016 else if (unformat (input, "%U", unformat_ip6_address, &a6))
1017 {
1018 ping_ip6 = 1;
1019 }
1020 else if (unformat (input, "ipv4"))
1021 {
1022 if (unformat (input, "%U", unformat_ip4_address, &a4))
Dave Barachd7cb1b52016-12-09 09:52:16 -05001023 {
1024 ping_ip4 = 1;
1025 }
Andrew Yourtchenko7e682202016-08-04 13:39:35 +00001026 else
Dave Barachd7cb1b52016-12-09 09:52:16 -05001027 {
1028 error =
1029 clib_error_return (0,
1030 "expecting IPv4 address but got `%U'",
1031 format_unformat_error, input);
1032 }
Andrew Yourtchenko7e682202016-08-04 13:39:35 +00001033 }
1034 else if (unformat (input, "ipv6"))
1035 {
1036 if (unformat (input, "%U", unformat_ip6_address, &a6))
Dave Barachd7cb1b52016-12-09 09:52:16 -05001037 {
1038 ping_ip6 = 1;
1039 }
Andrew Yourtchenko7e682202016-08-04 13:39:35 +00001040 else
Dave Barachd7cb1b52016-12-09 09:52:16 -05001041 {
1042 error =
1043 clib_error_return (0,
1044 "expecting IPv6 address but got `%U'",
1045 format_unformat_error, input);
1046 }
Andrew Yourtchenko7e682202016-08-04 13:39:35 +00001047 }
1048 else
1049 {
1050 error =
Dave Barachd7cb1b52016-12-09 09:52:16 -05001051 clib_error_return (0,
1052 "expecting IP4/IP6 address `%U'. Usage: ping <addr> [source <intf>] [size <datasz>] [repeat <count>] [verbose]",
1053 format_unformat_error, input);
Andrew Yourtchenko7e682202016-08-04 13:39:35 +00001054 goto done;
1055 }
1056
1057 /* allow for the second AF in the same ping */
1058 if (!ping_ip4 && (unformat (input, "ipv4")))
1059 {
1060 if (unformat (input, "%U", unformat_ip4_address, &a4))
Dave Barachd7cb1b52016-12-09 09:52:16 -05001061 {
1062 ping_ip4 = 1;
1063 }
Andrew Yourtchenko7e682202016-08-04 13:39:35 +00001064 }
1065 else if (!ping_ip6 && (unformat (input, "ipv6")))
1066 {
1067 if (unformat (input, "%U", unformat_ip6_address, &a6))
Dave Barachd7cb1b52016-12-09 09:52:16 -05001068 {
1069 ping_ip6 = 1;
1070 }
Andrew Yourtchenko7e682202016-08-04 13:39:35 +00001071 }
1072
1073 /* parse the rest of the parameters in a cycle */
1074 while (!unformat_eof (input, NULL))
1075 {
1076 if (unformat (input, "source"))
Dave Barachd7cb1b52016-12-09 09:52:16 -05001077 {
1078 if (!unformat_user
1079 (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
1080 {
1081 error =
1082 clib_error_return (0,
1083 "unknown interface `%U'",
1084 format_unformat_error, input);
1085 goto done;
1086 }
1087 }
Andrew Yourtchenko7e682202016-08-04 13:39:35 +00001088 else if (unformat (input, "size"))
Dave Barachd7cb1b52016-12-09 09:52:16 -05001089 {
1090 if (!unformat (input, "%u", &data_len))
1091 {
1092 error =
1093 clib_error_return (0,
1094 "expecting size but got `%U'",
1095 format_unformat_error, input);
1096 goto done;
1097 }
Andrew Yourtchenko61459c92017-01-28 15:31:19 +00001098 if (data_len > PING_MAXIMUM_DATA_SIZE)
1099 {
1100 error =
1101 clib_error_return (0,
1102 "%d is bigger than maximum allowed payload size %d",
1103 data_len, PING_MAXIMUM_DATA_SIZE);
1104 goto done;
1105 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001106 }
Neale Rannsf06aea52016-11-29 06:51:37 -08001107 else if (unformat (input, "table-id"))
Dave Barachd7cb1b52016-12-09 09:52:16 -05001108 {
wenxian li6e19b372017-06-04 13:52:07 +00001109 if (!unformat (input, "%u", &table_id))
Dave Barachd7cb1b52016-12-09 09:52:16 -05001110 {
1111 error =
1112 clib_error_return (0,
1113 "expecting table-id but got `%U'",
1114 format_unformat_error, input);
1115 goto done;
1116 }
1117 }
Andrew Yourtchenko7e682202016-08-04 13:39:35 +00001118 else if (unformat (input, "interval"))
Dave Barachd7cb1b52016-12-09 09:52:16 -05001119 {
1120 if (!unformat (input, "%f", &ping_interval))
1121 {
1122 error =
1123 clib_error_return (0,
1124 "expecting interval (floating point number) got `%U'",
1125 format_unformat_error, input);
1126 goto done;
1127 }
1128 }
Andrew Yourtchenko7e682202016-08-04 13:39:35 +00001129 else if (unformat (input, "repeat"))
Dave Barachd7cb1b52016-12-09 09:52:16 -05001130 {
1131 if (!unformat (input, "%u", &ping_repeat))
1132 {
1133 error =
1134 clib_error_return (0,
1135 "expecting repeat count but got `%U'",
1136 format_unformat_error, input);
1137 goto done;
1138 }
1139 }
Andrew Yourtchenko864b25d2017-03-22 10:51:14 +01001140 else if (unformat (input, "burst"))
1141 {
1142 if (!unformat (input, "%u", &ping_burst))
1143 {
1144 error =
1145 clib_error_return (0,
1146 "expecting burst count but got `%U'",
1147 format_unformat_error, input);
1148 goto done;
1149 }
1150 }
Andrew Yourtchenko7e682202016-08-04 13:39:35 +00001151 else if (unformat (input, "verbose"))
Dave Barachd7cb1b52016-12-09 09:52:16 -05001152 {
1153 verbose = 1;
1154 }
Andrew Yourtchenko7e682202016-08-04 13:39:35 +00001155 else
Dave Barachd7cb1b52016-12-09 09:52:16 -05001156 {
1157 error = clib_error_return (0, "unknown input `%U'",
1158 format_unformat_error, input);
1159 goto done;
1160 }
Andrew Yourtchenko7e682202016-08-04 13:39:35 +00001161 }
1162
Andrew Yourtchenkocc8fa162018-10-26 15:13:56 +02001163/*
1164 * Operationally, one won't (and shouldn't) need to send more than a frame worth of pings.
1165 * But it may be handy during the debugging.
1166 */
1167
1168#ifdef CLIB_DEBUG
1169#define MAX_PING_BURST (10*VLIB_FRAME_SIZE)
1170#else
1171#define MAX_PING_BURST (VLIB_FRAME_SIZE)
1172#endif
1173
1174 if (ping_burst < 1 || ping_burst > MAX_PING_BURST)
Andrew Yourtchenko864b25d2017-03-22 10:51:14 +01001175 return clib_error_return (0, "burst size must be between 1 and %u",
Andrew Yourtchenkocc8fa162018-10-26 15:13:56 +02001176 MAX_PING_BURST);
Andrew Yourtchenko864b25d2017-03-22 10:51:14 +01001177
Dave Barachd7cb1b52016-12-09 09:52:16 -05001178 run_ping_ip46_address (vm, table_id, ping_ip4 ? &a4 : NULL,
1179 ping_ip6 ? &a6 : NULL, sw_if_index, ping_interval,
Andrew Yourtchenkoe3d52802017-03-24 17:46:42 +01001180 ping_repeat, data_len, ping_burst, verbose);
Andrew Yourtchenko7e682202016-08-04 13:39:35 +00001181done:
1182 return error;
1183}
1184
Billy McFall0683c9c2016-10-13 08:27:31 -04001185/*?
1186 * This command sends an ICMP ECHO_REQUEST to network hosts. The address
1187 * can be an IPv4 or IPv6 address (or both at the same time).
1188 *
1189 * @cliexpar
1190 * @parblock
1191 * Example of how ping an IPv4 address:
1192 * @cliexstart{ping 172.16.1.2 source GigabitEthernet2/0/0 repeat 2}
1193 * 64 bytes from 172.16.1.2: icmp_seq=1 ttl=64 time=.1090 ms
1194 * 64 bytes from 172.16.1.2: icmp_seq=2 ttl=64 time=.0914 ms
1195 *
1196 * Statistics: 2 sent, 2 received, 0% packet loss
1197 * @cliexend
1198 *
1199 * Example of how ping both an IPv4 address and IPv6 address at the same time:
1200 * @cliexstart{ping 172.16.1.2 ipv6 fe80::24a5:f6ff:fe9c:3a36 source GigabitEthernet2/0/0 repeat 2 verbose}
1201 * Adjacency index: 10, sw_if_index: 1
1202 * Adj: ip6-discover-neighbor
1203 * Adj Interface: 0
1204 * Forced set interface: 1
1205 * Adjacency index: 0, sw_if_index: 4294967295
1206 * Adj: ip4-miss
1207 * Adj Interface: 0
1208 * Forced set interface: 1
1209 * Source address: 172.16.1.1
1210 * 64 bytes from 172.16.1.2: icmp_seq=1 ttl=64 time=.1899 ms
1211 * Adjacency index: 10, sw_if_index: 1
1212 * Adj: ip6-discover-neighbor
1213 * Adj Interface: 0
1214 * Forced set interface: 1
1215 * Adjacency index: 0, sw_if_index: 4294967295
1216 * Adj: ip4-miss
1217 * Adj Interface: 0
1218 * Forced set interface: 1
1219 * Source address: 172.16.1.1
1220 * 64 bytes from 172.16.1.2: icmp_seq=2 ttl=64 time=.0910 ms
1221 *
1222 * Statistics: 4 sent, 2 received, 50% packet loss
1223 * @cliexend
1224 * @endparblock
1225?*/
Andrew Yourtchenko7e682202016-08-04 13:39:35 +00001226/* *INDENT-OFF* */
1227VLIB_CLI_COMMAND (ping_command, static) =
1228{
1229 .path = "ping",
1230 .function = ping_ip_address,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001231 .short_help = "ping {<ip-addr> | ipv4 <ip4-addr> | ipv6 <ip6-addr>}"
1232 " [ipv4 <ip4-addr> | ipv6 <ip6-addr>] [source <interface>]"
1233 " [size <pktsize>] [interval <sec>] [repeat <cnt>] [table-id <id>]"
1234 " [verbose]",
flyingeagle2392a838b2017-05-15 16:57:20 +08001235 .is_mp_safe = 1,
Andrew Yourtchenko7e682202016-08-04 13:39:35 +00001236};
1237/* *INDENT-ON* */
1238
1239static clib_error_t *
1240ping_cli_init (vlib_main_t * vm)
1241{
Andrew Yourtchenkocc8fa162018-10-26 15:13:56 +02001242 vlib_thread_main_t *tm = vlib_get_thread_main ();
Andrew Yourtchenko7e682202016-08-04 13:39:35 +00001243 ping_main_t *pm = &ping_main;
Andrew Yourtchenkocc8fa162018-10-26 15:13:56 +02001244
Andrew Yourtchenko7e682202016-08-04 13:39:35 +00001245 pm->ip6_main = &ip6_main;
1246 pm->ip4_main = &ip4_main;
1247 icmp6_register_type (vm, ICMP6_echo_reply, ip6_icmp_echo_reply_node.index);
1248 ip4_icmp_register_type (vm, ICMP4_echo_reply,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001249 ip4_icmp_echo_reply_node.index);
Andrew Yourtchenkocc8fa162018-10-26 15:13:56 +02001250 if (tm->n_vlib_mains > 1)
1251 clib_spinlock_init (&pm->ping_run_check_lock);
Andrew Yourtchenko7e682202016-08-04 13:39:35 +00001252 return 0;
1253}
1254
1255VLIB_INIT_FUNCTION (ping_cli_init);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001256
1257/*
1258 * fd.io coding-style-patch-verification: ON
1259 *
1260 * Local Variables:
1261 * eval: (c-set-style "gnu")
1262 * End:
1263 */