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