Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [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 | |
| 16 | /** |
| 17 | * @file |
| 18 | * @brief Local TCP/IP stack punt infrastructure. |
| 19 | * |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 20 | * Provides a set of VPP nodes together with the relevant APIs and CLI |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 21 | * commands in order to adjust and dispatch packets from the VPP data plane |
| 22 | * to the local TCP/IP stack |
| 23 | */ |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 24 | |
| 25 | #include <vnet/ip/ip.h> |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 26 | #include <vlib/vlib.h> |
| 27 | #include <vnet/pg/pg.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 28 | #include <vnet/udp/udp.h> |
Pierre Pfister | 7fe51f3 | 2017-09-20 08:48:36 +0200 | [diff] [blame] | 29 | #include <vnet/tcp/tcp.h> |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 30 | #include <vnet/ip/punt.h> |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 31 | #include <vppinfra/sparse_vec.h> |
| 32 | #include <vlib/unix/unix.h> |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 33 | |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 34 | #include <stdio.h> |
| 35 | #include <unistd.h> |
| 36 | #include <sys/socket.h> |
| 37 | #include <sys/un.h> |
Marco Varlese | 2234983 | 2017-09-08 10:40:34 +0200 | [diff] [blame] | 38 | #include <sys/uio.h> |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 39 | #include <stdlib.h> |
| 40 | #include <stdbool.h> |
| 41 | |
| 42 | #define foreach_punt_next \ |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 43 | _ (PUNT, "error-punt") |
| 44 | |
| 45 | typedef enum |
| 46 | { |
| 47 | #define _(s,n) PUNT_NEXT_##s, |
| 48 | foreach_punt_next |
| 49 | #undef _ |
| 50 | PUNT_N_NEXT, |
| 51 | } punt_next_t; |
| 52 | |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 53 | enum punt_socket_rx_next_e |
| 54 | { |
| 55 | PUNT_SOCKET_RX_NEXT_INTERFACE_OUTPUT, |
| 56 | PUNT_SOCKET_RX_NEXT_IP4_LOOKUP, |
| 57 | PUNT_SOCKET_RX_NEXT_IP6_LOOKUP, |
| 58 | PUNT_SOCKET_RX_N_NEXT |
| 59 | }; |
| 60 | |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 61 | vlib_node_registration_t udp4_punt_node; |
| 62 | vlib_node_registration_t udp6_punt_node; |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 63 | vlib_node_registration_t udp4_punt_socket_node; |
| 64 | vlib_node_registration_t udp6_punt_socket_node; |
| 65 | static vlib_node_registration_t punt_socket_rx_node; |
| 66 | |
| 67 | punt_main_t punt_main; |
| 68 | |
| 69 | char * |
| 70 | vnet_punt_get_server_pathname (void) |
| 71 | { |
| 72 | punt_main_t *pm = &punt_main; |
| 73 | return pm->sun_path; |
| 74 | } |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 75 | |
| 76 | /** @brief IPv4/IPv6 UDP punt node main loop. |
| 77 | |
| 78 | This is the main loop inline function for IPv4/IPv6 UDP punt |
| 79 | transition node. |
| 80 | |
| 81 | @param vm vlib_main_t corresponding to the current thread |
| 82 | @param node vlib_node_runtime_t |
| 83 | @param frame vlib_frame_t whose contents should be dispatched |
| 84 | @param is_ipv4 indicates if called for IPv4 or IPv6 node |
| 85 | */ |
| 86 | always_inline uword |
| 87 | udp46_punt_inline (vlib_main_t * vm, |
| 88 | vlib_node_runtime_t * node, |
| 89 | vlib_frame_t * from_frame, int is_ip4) |
| 90 | { |
| 91 | u32 n_left_from, *from, *to_next; |
| 92 | word advance; |
| 93 | |
| 94 | from = vlib_frame_vector_args (from_frame); |
| 95 | n_left_from = from_frame->n_vectors; |
| 96 | |
| 97 | /* udp[46]_lookup hands us the data payload, not the IP header */ |
| 98 | if (is_ip4) |
| 99 | advance = -(sizeof (ip4_header_t) + sizeof (udp_header_t)); |
| 100 | else |
| 101 | advance = -(sizeof (ip6_header_t) + sizeof (udp_header_t)); |
| 102 | |
| 103 | while (n_left_from > 0) |
| 104 | { |
| 105 | u32 n_left_to_next; |
| 106 | |
| 107 | vlib_get_next_frame (vm, node, PUNT_NEXT_PUNT, to_next, n_left_to_next); |
| 108 | |
| 109 | while (n_left_from > 0 && n_left_to_next > 0) |
| 110 | { |
| 111 | u32 bi0; |
| 112 | vlib_buffer_t *b0; |
| 113 | |
| 114 | bi0 = from[0]; |
| 115 | to_next[0] = bi0; |
| 116 | from += 1; |
| 117 | to_next += 1; |
| 118 | n_left_from -= 1; |
| 119 | n_left_to_next -= 1; |
| 120 | |
| 121 | b0 = vlib_get_buffer (vm, bi0); |
| 122 | vlib_buffer_advance (b0, advance); |
| 123 | b0->error = node->errors[PUNT_ERROR_UDP_PORT]; |
| 124 | } |
| 125 | |
| 126 | vlib_put_next_frame (vm, node, PUNT_NEXT_PUNT, n_left_to_next); |
| 127 | } |
| 128 | |
| 129 | return from_frame->n_vectors; |
| 130 | } |
| 131 | |
| 132 | static char *punt_error_strings[] = { |
| 133 | #define punt_error(n,s) s, |
| 134 | #include "punt_error.def" |
| 135 | #undef punt_error |
| 136 | }; |
| 137 | |
| 138 | /** @brief IPv4 UDP punt node. |
| 139 | @node ip4-udp-punt |
| 140 | |
| 141 | This is the IPv4 UDP punt transition node. It is registered as a next |
| 142 | node for the "ip4-udp-lookup" handling UDP port(s) requested for punt. |
| 143 | The buffer's current data pointer is adjusted to the original packet |
| 144 | IPv4 header. All buffers are dispatched to "error-punt". |
| 145 | |
| 146 | @param vm vlib_main_t corresponding to the current thread |
| 147 | @param node vlib_node_runtime_t |
| 148 | @param frame vlib_frame_t whose contents should be dispatched |
| 149 | |
| 150 | @par Graph mechanics: next index usage |
| 151 | |
| 152 | @em Sets: |
| 153 | - <code>vnet_buffer(b)->current_data</code> |
| 154 | - <code>vnet_buffer(b)->current_len</code> |
| 155 | |
| 156 | <em>Next Index:</em> |
| 157 | - Dispatches the packet to the "error-punt" node |
| 158 | */ |
| 159 | static uword |
| 160 | udp4_punt (vlib_main_t * vm, |
| 161 | vlib_node_runtime_t * node, vlib_frame_t * from_frame) |
| 162 | { |
| 163 | return udp46_punt_inline (vm, node, from_frame, 1 /* is_ip4 */ ); |
| 164 | } |
| 165 | |
| 166 | /** @brief IPv6 UDP punt node. |
| 167 | @node ip6-udp-punt |
| 168 | |
| 169 | This is the IPv6 UDP punt transition node. It is registered as a next |
| 170 | node for the "ip6-udp-lookup" handling UDP port(s) requested for punt. |
| 171 | The buffer's current data pointer is adjusted to the original packet |
| 172 | IPv6 header. All buffers are dispatched to "error-punt". |
| 173 | |
| 174 | @param vm vlib_main_t corresponding to the current thread |
| 175 | @param node vlib_node_runtime_t |
| 176 | @param frame vlib_frame_t whose contents should be dispatched |
| 177 | |
| 178 | @par Graph mechanics: next index usage |
| 179 | |
| 180 | @em Sets: |
| 181 | - <code>vnet_buffer(b)->current_data</code> |
| 182 | - <code>vnet_buffer(b)->current_len</code> |
| 183 | |
| 184 | <em>Next Index:</em> |
| 185 | - Dispatches the packet to the "error-punt" node |
| 186 | */ |
| 187 | static uword |
| 188 | udp6_punt (vlib_main_t * vm, |
| 189 | vlib_node_runtime_t * node, vlib_frame_t * from_frame) |
| 190 | { |
| 191 | return udp46_punt_inline (vm, node, from_frame, 0 /* is_ip4 */ ); |
| 192 | } |
| 193 | |
| 194 | /* *INDENT-OFF* */ |
| 195 | VLIB_REGISTER_NODE (udp4_punt_node) = { |
| 196 | .function = udp4_punt, |
| 197 | .name = "ip4-udp-punt", |
| 198 | /* Takes a vector of packets. */ |
| 199 | .vector_size = sizeof (u32), |
| 200 | |
| 201 | .n_errors = PUNT_N_ERROR, |
| 202 | .error_strings = punt_error_strings, |
| 203 | |
| 204 | .n_next_nodes = PUNT_N_NEXT, |
| 205 | .next_nodes = { |
| 206 | #define _(s,n) [PUNT_NEXT_##s] = n, |
| 207 | foreach_punt_next |
| 208 | #undef _ |
| 209 | }, |
| 210 | }; |
| 211 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 212 | VLIB_NODE_FUNCTION_MULTIARCH (udp4_punt_node, udp4_punt); |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 213 | |
| 214 | VLIB_REGISTER_NODE (udp6_punt_node) = { |
| 215 | .function = udp6_punt, |
| 216 | .name = "ip6-udp-punt", |
| 217 | /* Takes a vector of packets. */ |
| 218 | .vector_size = sizeof (u32), |
| 219 | |
| 220 | .n_errors = PUNT_N_ERROR, |
| 221 | .error_strings = punt_error_strings, |
| 222 | |
| 223 | .n_next_nodes = PUNT_N_NEXT, |
| 224 | .next_nodes = { |
| 225 | #define _(s,n) [PUNT_NEXT_##s] = n, |
| 226 | foreach_punt_next |
| 227 | #undef _ |
| 228 | }, |
| 229 | }; |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 230 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 231 | VLIB_NODE_FUNCTION_MULTIARCH (udp6_punt_node, udp6_punt);; |
| 232 | |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 233 | /* *INDENT-ON* */ |
| 234 | |
Klement Sekera | 3c37ad5 | 2017-11-03 10:25:23 +0100 | [diff] [blame] | 235 | static punt_client_t * |
| 236 | punt_client_get (bool is_ip4, u16 port) |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 237 | { |
| 238 | punt_main_t *pm = &punt_main; |
Klement Sekera | 3c37ad5 | 2017-11-03 10:25:23 +0100 | [diff] [blame] | 239 | punt_client_t *v = |
| 240 | is_ip4 ? pm->clients_by_dst_port4 : pm->clients_by_dst_port6; |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 241 | |
| 242 | u16 i = sparse_vec_index (v, port); |
| 243 | if (i == SPARSE_VEC_INVALID_INDEX) |
| 244 | return 0; |
| 245 | |
Klement Sekera | 3c37ad5 | 2017-11-03 10:25:23 +0100 | [diff] [blame] | 246 | return &vec_elt (v, i); |
| 247 | } |
| 248 | |
| 249 | static struct sockaddr_un * |
| 250 | punt_socket_get (bool is_ip4, u16 port) |
| 251 | { |
| 252 | punt_client_t *v = punt_client_get (is_ip4, port); |
| 253 | if (v) |
| 254 | return &v->caddr; |
| 255 | |
| 256 | return NULL; |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | static void |
| 260 | punt_socket_register (bool is_ip4, u8 protocol, u16 port, |
| 261 | char *client_pathname) |
| 262 | { |
| 263 | punt_main_t *pm = &punt_main; |
| 264 | punt_client_t c, *n; |
| 265 | punt_client_t *v = is_ip4 ? pm->clients_by_dst_port4 : |
| 266 | pm->clients_by_dst_port6; |
| 267 | |
| 268 | memset (&c, 0, sizeof (c)); |
| 269 | memcpy (c.caddr.sun_path, client_pathname, sizeof (c.caddr.sun_path)); |
| 270 | c.caddr.sun_family = AF_UNIX; |
| 271 | c.port = port; |
| 272 | n = sparse_vec_validate (v, port); |
| 273 | n[0] = c; |
| 274 | } |
| 275 | |
| 276 | /* $$$$ Just leaves the mapping in place for now */ |
| 277 | static void |
| 278 | punt_socket_unregister (bool is_ip4, u8 protocol, u16 port) |
| 279 | { |
| 280 | return; |
| 281 | } |
| 282 | |
Klement Sekera | 3c37ad5 | 2017-11-03 10:25:23 +0100 | [diff] [blame] | 283 | typedef struct |
| 284 | { |
| 285 | punt_client_t client; |
| 286 | u8 is_midchain; |
| 287 | } udp_punt_trace_t; |
| 288 | |
| 289 | u8 * |
| 290 | format_udp_punt_trace (u8 * s, va_list * args) |
| 291 | { |
| 292 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 293 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
| 294 | udp_punt_trace_t *t = va_arg (*args, udp_punt_trace_t *); |
| 295 | u32 indent = format_get_indent (s); |
| 296 | s = format (s, "to: %s", t->client.caddr.sun_path); |
| 297 | if (t->is_midchain) |
| 298 | { |
| 299 | s = format (s, "\n%U(buffer is part of chain)", format_white_space, |
| 300 | indent); |
| 301 | } |
| 302 | return s; |
| 303 | } |
| 304 | |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 305 | always_inline uword |
| 306 | udp46_punt_socket_inline (vlib_main_t * vm, |
| 307 | vlib_node_runtime_t * node, |
| 308 | vlib_frame_t * frame, bool is_ip4) |
| 309 | { |
| 310 | u32 *buffers = vlib_frame_args (frame); |
| 311 | uword n_packets = frame->n_vectors; |
| 312 | struct iovec *iovecs = 0; |
| 313 | punt_main_t *pm = &punt_main; |
| 314 | int i; |
| 315 | |
| 316 | u32 node_index = is_ip4 ? udp4_punt_socket_node.index : |
| 317 | udp6_punt_socket_node.index; |
| 318 | |
| 319 | for (i = 0; i < n_packets; i++) |
| 320 | { |
| 321 | struct iovec *iov; |
| 322 | vlib_buffer_t *b; |
| 323 | uword l; |
| 324 | punt_packetdesc_t packetdesc; |
| 325 | |
| 326 | b = vlib_get_buffer (vm, buffers[i]); |
| 327 | |
| 328 | /* Reverse UDP Punt advance */ |
| 329 | udp_header_t *udp; |
| 330 | if (is_ip4) |
| 331 | { |
| 332 | vlib_buffer_advance (b, -(sizeof (ip4_header_t) + |
| 333 | sizeof (udp_header_t))); |
| 334 | ip4_header_t *ip = vlib_buffer_get_current (b); |
| 335 | udp = (udp_header_t *) (ip + 1); |
| 336 | } |
| 337 | else |
| 338 | { |
| 339 | vlib_buffer_advance (b, -(sizeof (ip6_header_t) + |
| 340 | sizeof (udp_header_t))); |
| 341 | ip6_header_t *ip = vlib_buffer_get_current (b); |
| 342 | udp = (udp_header_t *) (ip + 1); |
| 343 | } |
| 344 | |
| 345 | u16 port = clib_net_to_host_u16 (udp->dst_port); |
| 346 | |
| 347 | /* |
| 348 | * Find registerered client |
| 349 | * If no registered client, drop packet and count |
| 350 | */ |
| 351 | struct sockaddr_un *caddr; |
| 352 | caddr = punt_socket_get (is_ip4, port); |
| 353 | if (!caddr) |
| 354 | { |
| 355 | vlib_node_increment_counter (vm, node_index, |
| 356 | PUNT_ERROR_SOCKET_TX_ERROR, 1); |
| 357 | goto error; |
| 358 | } |
| 359 | |
Klement Sekera | 3c37ad5 | 2017-11-03 10:25:23 +0100 | [diff] [blame] | 360 | punt_client_t *c = NULL; |
| 361 | if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED)) |
| 362 | { |
| 363 | if (!c) |
| 364 | { |
| 365 | c = punt_client_get (is_ip4, port); |
| 366 | } |
| 367 | udp_punt_trace_t *t; |
| 368 | t = vlib_add_trace (vm, node, b, sizeof (t[0])); |
| 369 | clib_memcpy (&t->client, c, sizeof (t->client)); |
| 370 | } |
| 371 | |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 372 | /* Re-set iovecs if present. */ |
| 373 | if (iovecs) |
| 374 | _vec_len (iovecs) = 0; |
| 375 | |
| 376 | /* Add packet descriptor */ |
| 377 | packetdesc.sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX]; |
| 378 | packetdesc.action = 0; |
| 379 | vec_add2 (iovecs, iov, 1); |
| 380 | iov->iov_base = &packetdesc; |
| 381 | iov->iov_len = sizeof (packetdesc); |
| 382 | |
| 383 | /** VLIB buffer chain -> Unix iovec(s). */ |
| 384 | vlib_buffer_advance (b, -(sizeof (ethernet_header_t))); |
| 385 | vec_add2 (iovecs, iov, 1); |
| 386 | iov->iov_base = b->data + b->current_data; |
| 387 | iov->iov_len = l = b->current_length; |
| 388 | |
| 389 | if (PREDICT_FALSE (b->flags & VLIB_BUFFER_NEXT_PRESENT)) |
| 390 | { |
| 391 | do |
| 392 | { |
| 393 | b = vlib_get_buffer (vm, b->next_buffer); |
Klement Sekera | 3c37ad5 | 2017-11-03 10:25:23 +0100 | [diff] [blame] | 394 | if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED)) |
| 395 | { |
| 396 | udp_punt_trace_t *t; |
| 397 | t = vlib_add_trace (vm, node, b, sizeof (t[0])); |
| 398 | clib_memcpy (&t->client, c, sizeof (t->client)); |
| 399 | t->is_midchain = 1; |
| 400 | } |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 401 | |
| 402 | vec_add2 (iovecs, iov, 1); |
| 403 | |
| 404 | iov->iov_base = b->data + b->current_data; |
| 405 | iov->iov_len = b->current_length; |
| 406 | l += b->current_length; |
| 407 | } |
| 408 | while (b->flags & VLIB_BUFFER_NEXT_PRESENT); |
| 409 | } |
| 410 | |
| 411 | struct msghdr msg = { |
| 412 | .msg_name = caddr, |
| 413 | .msg_namelen = sizeof (*caddr), |
| 414 | .msg_iov = iovecs, |
| 415 | .msg_iovlen = vec_len (iovecs), |
| 416 | }; |
| 417 | |
Klement Sekera | 6bd0bc3 | 2017-11-08 12:18:21 +0100 | [diff] [blame^] | 418 | if (sendmsg (pm->socket_fd, &msg, 0) < (ssize_t) l) |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 419 | vlib_node_increment_counter (vm, node_index, |
| 420 | PUNT_ERROR_SOCKET_TX_ERROR, 1); |
| 421 | } |
| 422 | |
| 423 | error: |
Klement Sekera | 0dc11a1 | 2017-11-03 06:39:28 +0100 | [diff] [blame] | 424 | vlib_buffer_free (vm, buffers, n_packets); |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 425 | |
| 426 | return n_packets; |
| 427 | } |
| 428 | |
| 429 | static uword |
| 430 | udp4_punt_socket (vlib_main_t * vm, |
| 431 | vlib_node_runtime_t * node, vlib_frame_t * from_frame) |
| 432 | { |
| 433 | return udp46_punt_socket_inline (vm, node, from_frame, true /* is_ip4 */ ); |
| 434 | } |
| 435 | |
| 436 | static uword |
| 437 | udp6_punt_socket (vlib_main_t * vm, |
| 438 | vlib_node_runtime_t * node, vlib_frame_t * from_frame) |
| 439 | { |
| 440 | return udp46_punt_socket_inline (vm, node, from_frame, false /* is_ip4 */ ); |
| 441 | } |
| 442 | |
| 443 | |
| 444 | /* *INDENT-OFF* */ |
| 445 | VLIB_REGISTER_NODE (udp4_punt_socket_node) = { |
| 446 | .function = udp4_punt_socket, |
| 447 | .name = "ip4-udp-punt-socket", |
Klement Sekera | 3c37ad5 | 2017-11-03 10:25:23 +0100 | [diff] [blame] | 448 | .format_trace = format_udp_punt_trace, |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 449 | .flags = VLIB_NODE_FLAG_IS_DROP, |
| 450 | /* Takes a vector of packets. */ |
| 451 | .vector_size = sizeof (u32), |
| 452 | .n_errors = PUNT_N_ERROR, |
| 453 | .error_strings = punt_error_strings, |
| 454 | }; |
| 455 | VLIB_REGISTER_NODE (udp6_punt_socket_node) = { |
| 456 | .function = udp6_punt_socket, |
| 457 | .name = "ip6-udp-punt-socket", |
Klement Sekera | 3c37ad5 | 2017-11-03 10:25:23 +0100 | [diff] [blame] | 458 | .format_trace = format_udp_punt_trace, |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 459 | .flags = VLIB_NODE_FLAG_IS_DROP, |
| 460 | .vector_size = sizeof (u32), |
| 461 | .n_errors = PUNT_N_ERROR, |
| 462 | .error_strings = punt_error_strings, |
| 463 | }; |
| 464 | /* *INDENT-ON* */ |
| 465 | |
| 466 | typedef struct |
| 467 | { |
| 468 | enum punt_action_e action; |
| 469 | u32 sw_if_index; |
| 470 | } punt_trace_t; |
| 471 | |
| 472 | static u8 * |
| 473 | format_punt_trace (u8 * s, va_list * va) |
| 474 | { |
| 475 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *); |
| 476 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *); |
| 477 | vnet_main_t *vnm = vnet_get_main (); |
| 478 | punt_trace_t *t = va_arg (*va, punt_trace_t *); |
| 479 | s = format (s, "%U Action: %d", format_vnet_sw_if_index_name, |
| 480 | vnm, t->sw_if_index, t->action); |
| 481 | return s; |
| 482 | } |
| 483 | |
| 484 | static uword |
| 485 | punt_socket_rx_fd (vlib_main_t * vm, vlib_node_runtime_t * node, u32 fd) |
| 486 | { |
| 487 | const uword buffer_size = VLIB_BUFFER_DATA_SIZE; |
| 488 | u32 n_trace = vlib_get_trace_count (vm, node); |
| 489 | u32 next = node->cached_next_index; |
| 490 | u32 n_left_to_next, next_index; |
| 491 | u32 *to_next; |
| 492 | u32 error = PUNT_ERROR_NONE; |
| 493 | vlib_get_next_frame (vm, node, next, to_next, n_left_to_next); |
| 494 | |
| 495 | /* $$$$ Only dealing with one buffer at the time for now */ |
| 496 | |
| 497 | u32 bi; |
| 498 | vlib_buffer_t *b; |
| 499 | punt_packetdesc_t packetdesc; |
| 500 | ssize_t size; |
| 501 | struct iovec io[2]; |
| 502 | |
| 503 | if (vlib_buffer_alloc (vm, &bi, 1) != 1) |
| 504 | { |
| 505 | error = PUNT_ERROR_NOBUFFER; |
| 506 | goto error; |
| 507 | } |
| 508 | |
| 509 | b = vlib_get_buffer (vm, bi); |
| 510 | io[0].iov_base = &packetdesc; |
| 511 | io[0].iov_len = sizeof (packetdesc); |
| 512 | io[1].iov_base = b->data; |
| 513 | io[1].iov_len = buffer_size; |
| 514 | |
| 515 | size = readv (fd, io, 2); |
| 516 | /* We need at least the packet descriptor plus a header */ |
| 517 | if (size <= (int) (sizeof (packetdesc) + sizeof (ip4_header_t))) |
| 518 | { |
| 519 | vlib_buffer_free (vm, &bi, 1); |
| 520 | error = PUNT_ERROR_READV; |
| 521 | goto error; |
| 522 | } |
| 523 | |
| 524 | b->flags = VNET_BUFFER_F_LOCALLY_ORIGINATED; |
| 525 | b->current_length = size - sizeof (packetdesc); |
| 526 | |
| 527 | VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b); |
| 528 | |
| 529 | switch (packetdesc.action) |
| 530 | { |
| 531 | case PUNT_L2: |
| 532 | vnet_buffer (b)->sw_if_index[VLIB_TX] = packetdesc.sw_if_index; |
| 533 | next_index = PUNT_SOCKET_RX_NEXT_INTERFACE_OUTPUT; |
| 534 | break; |
| 535 | |
| 536 | case PUNT_IP4_ROUTED: |
| 537 | vnet_buffer (b)->sw_if_index[VLIB_RX] = packetdesc.sw_if_index; |
| 538 | vnet_buffer (b)->sw_if_index[VLIB_TX] = ~0; |
| 539 | next_index = PUNT_SOCKET_RX_NEXT_IP4_LOOKUP; |
| 540 | break; |
| 541 | |
| 542 | case PUNT_IP6_ROUTED: |
| 543 | vnet_buffer (b)->sw_if_index[VLIB_RX] = packetdesc.sw_if_index; |
| 544 | vnet_buffer (b)->sw_if_index[VLIB_TX] = ~0; |
| 545 | next_index = PUNT_SOCKET_RX_NEXT_IP6_LOOKUP; |
| 546 | break; |
| 547 | |
| 548 | default: |
| 549 | error = PUNT_ERROR_ACTION; |
| 550 | vlib_buffer_free (vm, &bi, 1); |
| 551 | goto error; |
| 552 | } |
| 553 | |
| 554 | if (PREDICT_FALSE (n_trace > 0)) |
| 555 | { |
| 556 | punt_trace_t *t; |
| 557 | vlib_trace_buffer (vm, node, next_index, b, 1 /* follow_chain */ ); |
| 558 | vlib_set_trace_count (vm, node, --n_trace); |
| 559 | t = vlib_add_trace (vm, node, b, sizeof (*t)); |
| 560 | t->sw_if_index = packetdesc.sw_if_index; |
| 561 | t->action = packetdesc.action; |
| 562 | } |
| 563 | |
| 564 | to_next[0] = bi; |
| 565 | to_next++; |
| 566 | n_left_to_next--; |
| 567 | |
| 568 | vlib_validate_buffer_enqueue_x1 (vm, node, next, to_next, n_left_to_next, |
| 569 | bi, next_index); |
| 570 | vlib_put_next_frame (vm, node, next, n_left_to_next); |
| 571 | return 1; |
| 572 | |
| 573 | error: |
| 574 | vlib_node_increment_counter (vm, punt_socket_rx_node.index, error, 1); |
| 575 | return 0; |
| 576 | } |
| 577 | |
| 578 | static uword |
| 579 | punt_socket_rx (vlib_main_t * vm, |
| 580 | vlib_node_runtime_t * node, vlib_frame_t * frame) |
| 581 | { |
| 582 | punt_main_t *pm = &punt_main; |
| 583 | u32 total_count = 0; |
| 584 | int i; |
| 585 | |
| 586 | for (i = 0; i < vec_len (pm->ready_fds); i++) |
| 587 | { |
| 588 | total_count += punt_socket_rx_fd (vm, node, pm->ready_fds[i]); |
| 589 | vec_del1 (pm->ready_fds, i); |
| 590 | } |
| 591 | return total_count; |
| 592 | } |
| 593 | |
| 594 | VLIB_REGISTER_NODE (punt_socket_rx_node, static) = |
| 595 | { |
| 596 | .function = punt_socket_rx,.name = "punt-socket-rx",.type = |
| 597 | VLIB_NODE_TYPE_INPUT,.state = VLIB_NODE_STATE_INTERRUPT,.vector_size = |
| 598 | 1,.n_errors = PUNT_N_ERROR,.error_strings = |
| 599 | punt_error_strings,.n_next_nodes = PUNT_SOCKET_RX_N_NEXT,.next_nodes = |
| 600 | { |
| 601 | [PUNT_SOCKET_RX_NEXT_INTERFACE_OUTPUT] = "interface-output", |
| 602 | [PUNT_SOCKET_RX_NEXT_IP4_LOOKUP] = "ip4-lookup", |
| 603 | [PUNT_SOCKET_RX_NEXT_IP6_LOOKUP] = "ip6-lookup",},.format_trace = |
| 604 | format_punt_trace,}; |
| 605 | |
| 606 | static clib_error_t * |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 607 | punt_socket_read_ready (clib_file_t * uf) |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 608 | { |
| 609 | vlib_main_t *vm = vlib_get_main (); |
| 610 | punt_main_t *pm = &punt_main; |
| 611 | |
| 612 | /** Schedule the rx node */ |
| 613 | vlib_node_set_interrupt_pending (vm, punt_socket_rx_node.index); |
| 614 | vec_add1 (pm->ready_fds, uf->file_descriptor); |
| 615 | |
| 616 | return 0; |
| 617 | } |
| 618 | |
| 619 | clib_error_t * |
| 620 | vnet_punt_socket_add (vlib_main_t * vm, u32 header_version, |
| 621 | bool is_ip4, u8 protocol, u16 port, |
| 622 | char *client_pathname) |
| 623 | { |
| 624 | punt_main_t *pm = &punt_main; |
| 625 | |
| 626 | if (!pm->is_configured) |
| 627 | return clib_error_return (0, "socket is not configured"); |
| 628 | |
| 629 | if (header_version != PUNT_PACKETDESC_VERSION) |
| 630 | return clib_error_return (0, "Invalid packet descriptor version"); |
| 631 | |
| 632 | /* For now we only support UDP punt */ |
| 633 | if (protocol != IP_PROTOCOL_UDP) |
| 634 | return clib_error_return (0, |
| 635 | "only UDP protocol (%d) is supported, got %d", |
| 636 | IP_PROTOCOL_UDP, protocol); |
| 637 | |
| 638 | if (port == (u16) ~ 0) |
| 639 | return clib_error_return (0, "UDP port number required"); |
| 640 | |
| 641 | /* Register client */ |
| 642 | punt_socket_register (is_ip4, protocol, port, client_pathname); |
| 643 | |
| 644 | u32 node_index = is_ip4 ? udp4_punt_socket_node.index : |
| 645 | udp6_punt_socket_node.index; |
| 646 | |
| 647 | udp_register_dst_port (vm, port, node_index, is_ip4); |
| 648 | |
| 649 | return 0; |
| 650 | } |
| 651 | |
| 652 | clib_error_t * |
| 653 | vnet_punt_socket_del (vlib_main_t * vm, bool is_ip4, u8 l4_protocol, u16 port) |
| 654 | { |
| 655 | punt_main_t *pm = &punt_main; |
| 656 | |
| 657 | if (!pm->is_configured) |
| 658 | return clib_error_return (0, "socket is not configured"); |
| 659 | |
| 660 | punt_socket_unregister (is_ip4, l4_protocol, port); |
| 661 | udp_unregister_dst_port (vm, port, is_ip4); |
| 662 | |
| 663 | return 0; |
| 664 | } |
| 665 | |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 666 | /** |
| 667 | * @brief Request IP traffic punt to the local TCP/IP stack. |
| 668 | * |
| 669 | * @em Note |
Pierre Pfister | 7fe51f3 | 2017-09-20 08:48:36 +0200 | [diff] [blame] | 670 | * - UDP and TCP are the only protocols supported in the current implementation |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 671 | * |
| 672 | * @param vm vlib_main_t corresponding to the current thread |
| 673 | * @param ipv IP protcol version. |
| 674 | * 4 - IPv4, 6 - IPv6, ~0 for both IPv6 and IPv4 |
| 675 | * @param protocol 8-bits L4 protocol value |
Pierre Pfister | 7fe51f3 | 2017-09-20 08:48:36 +0200 | [diff] [blame] | 676 | * UDP is 17 |
| 677 | * TCP is 1 |
| 678 | * @param port 16-bits L4 (TCP/IP) port number when applicable (UDP only) |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 679 | * |
| 680 | * @returns 0 on success, non-zero value otherwise |
| 681 | */ |
| 682 | clib_error_t * |
| 683 | vnet_punt_add_del (vlib_main_t * vm, u8 ipv, u8 protocol, u16 port, |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 684 | bool is_add) |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 685 | { |
Pierre Pfister | 7fe51f3 | 2017-09-20 08:48:36 +0200 | [diff] [blame] | 686 | |
Alexander Popovsky (apopovsk) | 740bcdb | 2016-11-15 15:36:23 -0800 | [diff] [blame] | 687 | /* For now we only support UDP punt */ |
Pierre Pfister | 7fe51f3 | 2017-09-20 08:48:36 +0200 | [diff] [blame] | 688 | if (protocol != IP_PROTOCOL_UDP && protocol != IP_PROTOCOL_TCP) |
Alexander Popovsky (apopovsk) | 740bcdb | 2016-11-15 15:36:23 -0800 | [diff] [blame] | 689 | return clib_error_return (0, |
Pierre Pfister | 7fe51f3 | 2017-09-20 08:48:36 +0200 | [diff] [blame] | 690 | "only UDP (%d) and TCP (%d) protocols are supported, got %d", |
| 691 | IP_PROTOCOL_UDP, IP_PROTOCOL_TCP, protocol); |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 692 | |
Alexander Popovsky (apopovsk) | 740bcdb | 2016-11-15 15:36:23 -0800 | [diff] [blame] | 693 | if (ipv != (u8) ~ 0 && ipv != 4 && ipv != 6) |
| 694 | return clib_error_return (0, "IP version must be 4 or 6, got %d", ipv); |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 695 | |
Alexander Popovsky (apopovsk) | 740bcdb | 2016-11-15 15:36:23 -0800 | [diff] [blame] | 696 | if (port == (u16) ~ 0) |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 697 | { |
Pierre Pfister | 7fe51f3 | 2017-09-20 08:48:36 +0200 | [diff] [blame] | 698 | if ((ipv == 4) || (ipv == (u8) ~ 0)) |
| 699 | { |
| 700 | if (protocol == IP_PROTOCOL_UDP) |
| 701 | udp_punt_unknown (vm, 1, is_add); |
| 702 | else if (protocol == IP_PROTOCOL_TCP) |
| 703 | tcp_punt_unknown (vm, 1, is_add); |
| 704 | } |
Alexander Popovsky (apopovsk) | 740bcdb | 2016-11-15 15:36:23 -0800 | [diff] [blame] | 705 | |
Pierre Pfister | 7fe51f3 | 2017-09-20 08:48:36 +0200 | [diff] [blame] | 706 | if ((ipv == 6) || (ipv == (u8) ~ 0)) |
| 707 | { |
| 708 | if (protocol == IP_PROTOCOL_UDP) |
| 709 | udp_punt_unknown (vm, 0, is_add); |
| 710 | else if (protocol == IP_PROTOCOL_TCP) |
| 711 | tcp_punt_unknown (vm, 0, is_add); |
| 712 | } |
Alexander Popovsky (apopovsk) | 740bcdb | 2016-11-15 15:36:23 -0800 | [diff] [blame] | 713 | |
| 714 | return 0; |
| 715 | } |
| 716 | |
| 717 | else if (is_add) |
| 718 | { |
Pierre Pfister | 7fe51f3 | 2017-09-20 08:48:36 +0200 | [diff] [blame] | 719 | if (protocol == IP_PROTOCOL_TCP) |
| 720 | return clib_error_return (0, "punt TCP ports is not supported yet"); |
| 721 | |
Alexander Popovsky (apopovsk) | 740bcdb | 2016-11-15 15:36:23 -0800 | [diff] [blame] | 722 | if (ipv == 4 || ipv == (u8) ~ 0) |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 723 | udp_register_dst_port (vm, port, udp4_punt_node.index, 1); |
Alexander Popovsky (apopovsk) | 740bcdb | 2016-11-15 15:36:23 -0800 | [diff] [blame] | 724 | |
| 725 | if (ipv == 6 || ipv == (u8) ~ 0) |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 726 | udp_register_dst_port (vm, port, udp6_punt_node.index, 0); |
Alexander Popovsky (apopovsk) | 740bcdb | 2016-11-15 15:36:23 -0800 | [diff] [blame] | 727 | |
| 728 | return 0; |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 729 | } |
| 730 | else |
Alexander Popovsky (apopovsk) | 740bcdb | 2016-11-15 15:36:23 -0800 | [diff] [blame] | 731 | return clib_error_return (0, "punt delete is not supported yet"); |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | static clib_error_t * |
Pierre Pfister | 7fe51f3 | 2017-09-20 08:48:36 +0200 | [diff] [blame] | 735 | punt_cli (vlib_main_t * vm, |
| 736 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 737 | { |
Pierre Pfister | 7fe51f3 | 2017-09-20 08:48:36 +0200 | [diff] [blame] | 738 | u32 port; |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 739 | bool is_add = true; |
Pierre Pfister | 7fe51f3 | 2017-09-20 08:48:36 +0200 | [diff] [blame] | 740 | u32 protocol = ~0; |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 741 | clib_error_t *error; |
| 742 | |
| 743 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 744 | { |
| 745 | if (unformat (input, "del")) |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 746 | is_add = false; |
Pierre Pfister | 7fe51f3 | 2017-09-20 08:48:36 +0200 | [diff] [blame] | 747 | else if (unformat (input, "all")) |
Alexander Popovsky (apopovsk) | 740bcdb | 2016-11-15 15:36:23 -0800 | [diff] [blame] | 748 | { |
| 749 | /* punt both IPv6 and IPv4 when used in CLI */ |
Pierre Pfister | 7fe51f3 | 2017-09-20 08:48:36 +0200 | [diff] [blame] | 750 | error = vnet_punt_add_del (vm, ~0, protocol, ~0, is_add); |
Alexander Popovsky (apopovsk) | 740bcdb | 2016-11-15 15:36:23 -0800 | [diff] [blame] | 751 | if (error) |
| 752 | clib_error_report (error); |
| 753 | } |
Pierre Pfister | 7fe51f3 | 2017-09-20 08:48:36 +0200 | [diff] [blame] | 754 | else if (unformat (input, "%d", &port)) |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 755 | { |
| 756 | /* punt both IPv6 and IPv4 when used in CLI */ |
Pierre Pfister | 7fe51f3 | 2017-09-20 08:48:36 +0200 | [diff] [blame] | 757 | error = vnet_punt_add_del (vm, ~0, protocol, port, is_add); |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 758 | if (error) |
| 759 | clib_error_report (error); |
| 760 | } |
Pierre Pfister | 7fe51f3 | 2017-09-20 08:48:36 +0200 | [diff] [blame] | 761 | else if (unformat (input, "udp")) |
| 762 | protocol = IP_PROTOCOL_UDP; |
| 763 | else if (unformat (input, "tcp")) |
| 764 | protocol = IP_PROTOCOL_TCP; |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | return 0; |
| 768 | } |
| 769 | |
| 770 | /*? |
| 771 | * The set of '<em>set punt</em>' commands allows specific IP traffic to |
| 772 | * be punted to the host TCP/IP stack |
| 773 | * |
| 774 | * @em Note |
| 775 | * - UDP is the only protocol supported in the current implementation |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 776 | * - All TCP traffic is currently punted to the host by default |
| 777 | * |
| 778 | * @cliexpar |
| 779 | * @parblock |
| 780 | * Example of how to request NTP traffic to be punted |
| 781 | * @cliexcmd{set punt udp 125} |
| 782 | * |
Alexander Popovsky (apopovsk) | 740bcdb | 2016-11-15 15:36:23 -0800 | [diff] [blame] | 783 | * Example of how to request all 'unknown' UDP traffic to be punted |
| 784 | * @cliexcmd{set punt udp all} |
| 785 | * |
| 786 | * Example of how to stop all 'unknown' UDP traffic to be punted |
| 787 | * @cliexcmd{set punt udp del all} |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 788 | * @endparblock |
| 789 | ?*/ |
| 790 | /* *INDENT-OFF* */ |
Pierre Pfister | 7fe51f3 | 2017-09-20 08:48:36 +0200 | [diff] [blame] | 791 | VLIB_CLI_COMMAND (punt_command, static) = { |
| 792 | .path = "set punt", |
| 793 | .short_help = "set punt [udp|tcp] [del] <all | port-num1 [port-num2 ...]>", |
| 794 | .function = punt_cli, |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 795 | }; |
| 796 | /* *INDENT-ON* */ |
| 797 | |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 798 | clib_error_t * |
| 799 | punt_init (vlib_main_t * vm) |
| 800 | { |
| 801 | punt_main_t *pm = &punt_main; |
| 802 | |
| 803 | pm->clients_by_dst_port6 = sparse_vec_new |
| 804 | (sizeof (pm->clients_by_dst_port6[0]), |
| 805 | BITS (((udp_header_t *) 0)->dst_port)); |
| 806 | pm->clients_by_dst_port4 = sparse_vec_new |
| 807 | (sizeof (pm->clients_by_dst_port4[0]), |
| 808 | BITS (((udp_header_t *) 0)->dst_port)); |
| 809 | |
| 810 | pm->is_configured = false; |
| 811 | pm->interface_output_node = vlib_get_node_by_name (vm, |
| 812 | (u8 *) |
| 813 | "interface-output"); |
| 814 | return 0; |
| 815 | } |
| 816 | |
| 817 | VLIB_INIT_FUNCTION (punt_init); |
| 818 | |
| 819 | static clib_error_t * |
| 820 | punt_config (vlib_main_t * vm, unformat_input_t * input) |
| 821 | { |
| 822 | punt_main_t *pm = &punt_main; |
| 823 | char *socket_path = 0; |
| 824 | |
| 825 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 826 | { |
| 827 | if (unformat (input, "socket %s", &socket_path)) |
| 828 | strncpy (pm->sun_path, socket_path, 108 - 1); |
| 829 | else |
| 830 | return clib_error_return (0, "unknown input `%U'", |
| 831 | format_unformat_error, input); |
| 832 | } |
| 833 | |
| 834 | if (socket_path == 0) |
| 835 | return 0; |
| 836 | |
| 837 | /* UNIX domain socket */ |
| 838 | struct sockaddr_un addr; |
| 839 | if ((pm->socket_fd = socket (AF_UNIX, SOCK_DGRAM | SOCK_NONBLOCK, 0)) == -1) |
| 840 | { |
| 841 | return clib_error_return (0, "socket error"); |
| 842 | } |
| 843 | |
| 844 | memset (&addr, 0, sizeof (addr)); |
| 845 | addr.sun_family = AF_UNIX; |
| 846 | if (*socket_path == '\0') |
| 847 | { |
| 848 | *addr.sun_path = '\0'; |
| 849 | strncpy (addr.sun_path + 1, socket_path + 1, |
| 850 | sizeof (addr.sun_path) - 2); |
| 851 | } |
| 852 | else |
| 853 | { |
| 854 | strncpy (addr.sun_path, socket_path, sizeof (addr.sun_path) - 1); |
| 855 | unlink (socket_path); |
| 856 | } |
| 857 | |
| 858 | if (bind (pm->socket_fd, (struct sockaddr *) &addr, sizeof (addr)) == -1) |
| 859 | { |
| 860 | return clib_error_return (0, "bind error"); |
| 861 | } |
| 862 | |
| 863 | /* Register socket */ |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 864 | clib_file_main_t *fm = &file_main; |
| 865 | clib_file_t template = { 0 }; |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 866 | template.read_function = punt_socket_read_ready; |
| 867 | template.file_descriptor = pm->socket_fd; |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 868 | pm->clib_file_index = clib_file_add (fm, &template); |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 869 | |
| 870 | pm->is_configured = true; |
| 871 | |
| 872 | return 0; |
| 873 | } |
| 874 | |
| 875 | VLIB_CONFIG_FUNCTION (punt_config, "punt"); |
| 876 | |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 877 | /* |
| 878 | * fd.io coding-style-patch-verification: ON |
| 879 | * |
| 880 | * Local Variables: |
| 881 | * eval: (c-set-style "gnu") |
| 882 | * End: |
| 883 | */ |