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