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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @brief Local TCP/IP stack punt infrastructure.
20 * Provides a set of VPP nodes together with the relevant APIs and CLI
21 * commands in order to adjust and dispatch packets from the VPP data plane
22 * to the local TCP/IP stack
25 #include <vnet/ip/ip.h>
26 #include <vlib/vlib.h>
27 #include <vnet/pg/pg.h>
28 #include <vnet/udp/udp.h>
29 #include <vnet/tcp/tcp.h>
30 #include <vnet/sctp/sctp.h>
31 #include <vnet/ip/punt.h>
32 #include <vlib/unix/unix.h>
36 #include <sys/socket.h>
42 #define punt_error(n,s) PUNT_ERROR_##n,
43 #include <vnet/ip/punt_error.def>
48 #define foreach_punt_next \
49 _ (PUNT4, "ip4-punt") \
54 #define _(s,n) PUNT_NEXT_##s,
60 enum punt_socket_rx_next_e
62 PUNT_SOCKET_RX_NEXT_INTERFACE_OUTPUT,
63 PUNT_SOCKET_RX_NEXT_IP4_LOOKUP,
64 PUNT_SOCKET_RX_NEXT_IP6_LOOKUP,
68 #define punt_next_punt(is_ip4) (is_ip4 ? PUNT_NEXT_PUNT4 : PUNT_NEXT_PUNT6)
70 /** @brief IPv4/IPv6 UDP punt node main loop.
72 This is the main loop inline function for IPv4/IPv6 UDP punt
75 @param vm vlib_main_t corresponding to the current thread
76 @param node vlib_node_runtime_t
77 @param frame vlib_frame_t whose contents should be dispatched
78 @param is_ipv4 indicates if called for IPv4 or IPv6 node
81 udp46_punt_inline (vlib_main_t * vm,
82 vlib_node_runtime_t * node,
83 vlib_frame_t * from_frame, int is_ip4)
85 u32 n_left_from, *from, *to_next;
88 from = vlib_frame_vector_args (from_frame);
89 n_left_from = from_frame->n_vectors;
91 /* udp[46]_lookup hands us the data payload, not the IP header */
93 advance = -(sizeof (ip4_header_t) + sizeof (udp_header_t));
95 advance = -(sizeof (ip6_header_t) + sizeof (udp_header_t));
97 while (n_left_from > 0)
101 vlib_get_next_frame (vm, node, punt_next_punt (is_ip4), to_next,
104 while (n_left_from > 0 && n_left_to_next > 0)
116 b0 = vlib_get_buffer (vm, bi0);
117 vlib_buffer_advance (b0, advance);
118 b0->error = node->errors[PUNT_ERROR_UDP_PORT];
121 vlib_put_next_frame (vm, node, punt_next_punt (is_ip4), n_left_to_next);
124 return from_frame->n_vectors;
127 static char *punt_error_strings[] = {
128 #define punt_error(n,s) s,
129 #include "punt_error.def"
133 /** @brief IPv4 UDP punt node.
136 This is the IPv4 UDP punt transition node. It is registered as a next
137 node for the "ip4-udp-lookup" handling UDP port(s) requested for punt.
138 The buffer's current data pointer is adjusted to the original packet
139 IPv4 header. All buffers are dispatched to "error-punt".
141 @param vm vlib_main_t corresponding to the current thread
142 @param node vlib_node_runtime_t
143 @param frame vlib_frame_t whose contents should be dispatched
145 @par Graph mechanics: next index usage
148 - <code>vnet_buffer(b)->current_data</code>
149 - <code>vnet_buffer(b)->current_len</code>
152 - Dispatches the packet to the "error-punt" node
154 VLIB_NODE_FN (udp4_punt_node) (vlib_main_t * vm,
155 vlib_node_runtime_t * node,
156 vlib_frame_t * from_frame)
158 return udp46_punt_inline (vm, node, from_frame, 1 /* is_ip4 */ );
161 /** @brief IPv6 UDP punt node.
164 This is the IPv6 UDP punt transition node. It is registered as a next
165 node for the "ip6-udp-lookup" handling UDP port(s) requested for punt.
166 The buffer's current data pointer is adjusted to the original packet
167 IPv6 header. All buffers are dispatched to "error-punt".
169 @param vm vlib_main_t corresponding to the current thread
170 @param node vlib_node_runtime_t
171 @param frame vlib_frame_t whose contents should be dispatched
173 @par Graph mechanics: next index usage
176 - <code>vnet_buffer(b)->current_data</code>
177 - <code>vnet_buffer(b)->current_len</code>
180 - Dispatches the packet to the "error-punt" node
182 VLIB_NODE_FN (udp6_punt_node) (vlib_main_t * vm,
183 vlib_node_runtime_t * node,
184 vlib_frame_t * from_frame)
186 return udp46_punt_inline (vm, node, from_frame, 0 /* is_ip4 */ );
190 VLIB_REGISTER_NODE (udp4_punt_node) = {
191 .name = "ip4-udp-punt",
192 /* Takes a vector of packets. */
193 .vector_size = sizeof (u32),
195 .n_errors = PUNT_N_ERROR,
196 .error_strings = punt_error_strings,
198 .n_next_nodes = PUNT_N_NEXT,
200 #define _(s,n) [PUNT_NEXT_##s] = n,
206 VLIB_REGISTER_NODE (udp6_punt_node) = {
207 .name = "ip6-udp-punt",
208 /* Takes a vector of packets. */
209 .vector_size = sizeof (u32),
211 .n_errors = PUNT_N_ERROR,
212 .error_strings = punt_error_strings,
214 .n_next_nodes = PUNT_N_NEXT,
216 #define _(s,n) [PUNT_NEXT_##s] = n,
225 punt_client_t client;
230 format_udp_punt_trace (u8 * s, va_list * args)
232 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
233 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
234 udp_punt_trace_t *t = va_arg (*args, udp_punt_trace_t *);
235 u32 indent = format_get_indent (s);
236 s = format (s, "to: %s", t->client.caddr.sun_path);
239 s = format (s, "\n%U(buffer is part of chain)", format_white_space,
246 punt_socket_inline (vlib_main_t * vm,
247 vlib_node_runtime_t * node,
248 vlib_frame_t * frame,
249 punt_type_t pt, ip_address_family_t af)
251 u32 *buffers = vlib_frame_vector_args (frame);
252 uword n_packets = frame->n_vectors;
253 struct iovec *iovecs = 0;
254 punt_main_t *pm = &punt_main;
257 u32 node_index = AF_IP4 == af ? udp4_punt_socket_node.index :
258 udp6_punt_socket_node.index;
260 for (i = 0; i < n_packets; i++)
265 punt_packetdesc_t packetdesc;
268 b = vlib_get_buffer (vm, buffers[i]);
270 if (PUNT_TYPE_L4 == pt)
272 /* Reverse UDP Punt advance */
276 vlib_buffer_advance (b, -(sizeof (ip4_header_t) +
277 sizeof (udp_header_t)));
278 ip4_header_t *ip = vlib_buffer_get_current (b);
279 udp = (udp_header_t *) (ip + 1);
283 vlib_buffer_advance (b, -(sizeof (ip6_header_t) +
284 sizeof (udp_header_t)));
285 ip6_header_t *ip = vlib_buffer_get_current (b);
286 udp = (udp_header_t *) (ip + 1);
289 u16 port = clib_net_to_host_u16 (udp->dst_port);
292 * Find registerered client
293 * If no registered client, drop packet and count
295 c = punt_client_l4_get (af, port);
297 else if (PUNT_TYPE_EXCEPTION == pt)
299 c = punt_client_exception_get (b->punt_reason);
304 if (PREDICT_FALSE (NULL == c))
306 vlib_node_increment_counter (vm, node_index,
307 PUNT_ERROR_SOCKET_TX_ERROR, 1);
311 struct sockaddr_un *caddr = &c->caddr;
313 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
316 t = vlib_add_trace (vm, node, b, sizeof (t[0]));
317 clib_memcpy_fast (&t->client, c, sizeof (t->client));
320 /* Re-set iovecs if present. */
322 _vec_len (iovecs) = 0;
324 /* Add packet descriptor */
325 packetdesc.sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
326 packetdesc.action = 0;
327 vec_add2 (iovecs, iov, 1);
328 iov->iov_base = &packetdesc;
329 iov->iov_len = sizeof (packetdesc);
331 /** VLIB buffer chain -> Unix iovec(s). */
332 vlib_buffer_advance (b, -(sizeof (ethernet_header_t)));
333 vec_add2 (iovecs, iov, 1);
334 iov->iov_base = b->data + b->current_data;
335 iov->iov_len = l = b->current_length;
337 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_NEXT_PRESENT))
341 b = vlib_get_buffer (vm, b->next_buffer);
342 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
345 t = vlib_add_trace (vm, node, b, sizeof (t[0]));
346 clib_memcpy_fast (&t->client, c, sizeof (t->client));
350 vec_add2 (iovecs, iov, 1);
352 iov->iov_base = b->data + b->current_data;
353 iov->iov_len = b->current_length;
354 l += b->current_length;
356 while (b->flags & VLIB_BUFFER_NEXT_PRESENT);
359 struct msghdr msg = {
361 .msg_namelen = sizeof (*caddr),
363 .msg_iovlen = vec_len (iovecs),
366 if (sendmsg (pm->socket_fd, &msg, 0) < (ssize_t) l)
367 vlib_node_increment_counter (vm, node_index,
368 PUNT_ERROR_SOCKET_TX_ERROR, 1);
370 vlib_node_increment_counter (vm, node_index, PUNT_ERROR_SOCKET_TX, 1);
374 vlib_buffer_free (vm, buffers, n_packets);
380 udp4_punt_socket (vlib_main_t * vm,
381 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
383 return punt_socket_inline (vm, node, from_frame, PUNT_TYPE_L4, AF_IP4);
387 udp6_punt_socket (vlib_main_t * vm,
388 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
390 return punt_socket_inline (vm, node, from_frame, PUNT_TYPE_L4, AF_IP6);
394 exception_punt_socket (vlib_main_t * vm,
395 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
397 return punt_socket_inline (vm, node, from_frame,
398 PUNT_TYPE_EXCEPTION, AF_IP4);
403 VLIB_REGISTER_NODE (udp4_punt_socket_node) = {
404 .function = udp4_punt_socket,
405 .name = "ip4-udp-punt-socket",
406 .format_trace = format_udp_punt_trace,
407 .flags = VLIB_NODE_FLAG_IS_DROP,
408 /* Takes a vector of packets. */
409 .vector_size = sizeof (u32),
410 .n_errors = PUNT_N_ERROR,
411 .error_strings = punt_error_strings,
413 VLIB_REGISTER_NODE (udp6_punt_socket_node) = {
414 .function = udp6_punt_socket,
415 .name = "ip6-udp-punt-socket",
416 .format_trace = format_udp_punt_trace,
417 .flags = VLIB_NODE_FLAG_IS_DROP,
418 .vector_size = sizeof (u32),
419 .n_errors = PUNT_N_ERROR,
420 .error_strings = punt_error_strings,
422 VLIB_REGISTER_NODE (exception_punt_socket_node) = {
423 .function = exception_punt_socket,
424 .name = "exception-punt-socket",
425 .format_trace = format_udp_punt_trace,
426 .flags = VLIB_NODE_FLAG_IS_DROP,
427 .vector_size = sizeof (u32),
428 .n_errors = PUNT_N_ERROR,
429 .error_strings = punt_error_strings,
435 enum punt_action_e action;
440 format_punt_trace (u8 * s, va_list * va)
442 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
443 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
444 vnet_main_t *vnm = vnet_get_main ();
445 punt_trace_t *t = va_arg (*va, punt_trace_t *);
446 s = format (s, "%U Action: %d", format_vnet_sw_if_index_name,
447 vnm, t->sw_if_index, t->action);
452 punt_socket_rx_fd (vlib_main_t * vm, vlib_node_runtime_t * node, u32 fd)
454 const uword buffer_size = vlib_buffer_get_default_data_size (vm);
455 u32 n_trace = vlib_get_trace_count (vm, node);
456 u32 next = node->cached_next_index;
457 u32 n_left_to_next, next_index;
459 u32 error = PUNT_ERROR_NONE;
460 vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
462 /* $$$$ Only dealing with one buffer at the time for now */
466 punt_packetdesc_t packetdesc;
470 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
472 error = PUNT_ERROR_NOBUFFER;
476 b = vlib_get_buffer (vm, bi);
477 io[0].iov_base = &packetdesc;
478 io[0].iov_len = sizeof (packetdesc);
479 io[1].iov_base = b->data;
480 io[1].iov_len = buffer_size;
482 size = readv (fd, io, 2);
483 /* We need at least the packet descriptor plus a header */
484 if (size <= (int) (sizeof (packetdesc) + sizeof (ip4_header_t)))
486 vlib_buffer_free (vm, &bi, 1);
487 error = PUNT_ERROR_READV;
491 b->flags = VNET_BUFFER_F_LOCALLY_ORIGINATED;
492 b->current_length = size - sizeof (packetdesc);
494 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b);
496 switch (packetdesc.action)
499 vnet_buffer (b)->sw_if_index[VLIB_TX] = packetdesc.sw_if_index;
500 next_index = PUNT_SOCKET_RX_NEXT_INTERFACE_OUTPUT;
503 case PUNT_IP4_ROUTED:
504 vnet_buffer (b)->sw_if_index[VLIB_RX] = packetdesc.sw_if_index;
505 vnet_buffer (b)->sw_if_index[VLIB_TX] = ~0;
506 next_index = PUNT_SOCKET_RX_NEXT_IP4_LOOKUP;
509 case PUNT_IP6_ROUTED:
510 vnet_buffer (b)->sw_if_index[VLIB_RX] = packetdesc.sw_if_index;
511 vnet_buffer (b)->sw_if_index[VLIB_TX] = ~0;
512 next_index = PUNT_SOCKET_RX_NEXT_IP6_LOOKUP;
516 error = PUNT_ERROR_ACTION;
517 vlib_buffer_free (vm, &bi, 1);
521 if (PREDICT_FALSE (n_trace > 0))
524 vlib_trace_buffer (vm, node, next_index, b, 1 /* follow_chain */ );
525 vlib_set_trace_count (vm, node, --n_trace);
526 t = vlib_add_trace (vm, node, b, sizeof (*t));
527 t->sw_if_index = packetdesc.sw_if_index;
528 t->action = packetdesc.action;
535 vlib_validate_buffer_enqueue_x1 (vm, node, next, to_next, n_left_to_next,
537 vlib_put_next_frame (vm, node, next, n_left_to_next);
541 vlib_node_increment_counter (vm, punt_socket_rx_node.index, error, 1);
546 punt_socket_rx (vlib_main_t * vm,
547 vlib_node_runtime_t * node, vlib_frame_t * frame)
549 punt_main_t *pm = &punt_main;
553 for (i = 0; i < vec_len (pm->ready_fds); i++)
555 total_count += punt_socket_rx_fd (vm, node, pm->ready_fds[i]);
556 vec_del1 (pm->ready_fds, i);
562 VLIB_REGISTER_NODE (punt_socket_rx_node) =
564 .function = punt_socket_rx,
565 .name = "punt-socket-rx",
566 .type = VLIB_NODE_TYPE_INPUT,
567 .state = VLIB_NODE_STATE_INTERRUPT,
569 .n_errors = PUNT_N_ERROR,
570 .error_strings = punt_error_strings,
571 .n_next_nodes = PUNT_SOCKET_RX_N_NEXT,
573 [PUNT_SOCKET_RX_NEXT_INTERFACE_OUTPUT] = "interface-output",
574 [PUNT_SOCKET_RX_NEXT_IP4_LOOKUP] = "ip4-lookup",
575 [PUNT_SOCKET_RX_NEXT_IP6_LOOKUP] = "ip6-lookup",
577 .format_trace = format_punt_trace,
582 * fd.io coding-style-patch-verification: ON
585 * eval: (c-set-style "gnu")