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