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