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 <vnet/ethernet/ethernet.h>
27 #include <vlib/vlib.h>
28 #include <vnet/ip/punt.h>
29 #include <vlib/unix/unix.h>
33 #include <sys/socket.h>
39 #define punt_error(n,s) PUNT_ERROR_##n,
40 #include <vnet/ip/punt_error.def>
45 #define foreach_punt_next \
46 _ (PUNT4, "ip4-punt") \
51 #define _(s,n) PUNT_NEXT_##s,
57 enum punt_socket_rx_next_e
59 PUNT_SOCKET_RX_NEXT_INTERFACE_OUTPUT,
60 PUNT_SOCKET_RX_NEXT_IP4_LOOKUP,
61 PUNT_SOCKET_RX_NEXT_IP6_LOOKUP,
65 #define punt_next_punt(is_ip4) (is_ip4 ? PUNT_NEXT_PUNT4 : PUNT_NEXT_PUNT6)
67 /** @brief IPv4/IPv6 UDP punt node main loop.
69 This is the main loop inline function for IPv4/IPv6 UDP punt
72 @param vm vlib_main_t corresponding to the current thread
73 @param node vlib_node_runtime_t
74 @param frame vlib_frame_t whose contents should be dispatched
75 @param is_ipv4 indicates if called for IPv4 or IPv6 node
78 udp46_punt_inline (vlib_main_t * vm,
79 vlib_node_runtime_t * node,
80 vlib_frame_t * from_frame, int is_ip4)
82 u32 n_left_from, *from, *to_next;
85 from = vlib_frame_vector_args (from_frame);
86 n_left_from = from_frame->n_vectors;
88 /* udp[46]_lookup hands us the data payload, not the IP header */
90 advance = -(sizeof (ip4_header_t) + sizeof (udp_header_t));
92 advance = -(sizeof (ip6_header_t) + sizeof (udp_header_t));
94 while (n_left_from > 0)
98 vlib_get_next_frame (vm, node, punt_next_punt (is_ip4), to_next,
101 while (n_left_from > 0 && n_left_to_next > 0)
113 b0 = vlib_get_buffer (vm, bi0);
114 vlib_buffer_advance (b0, advance);
115 b0->error = node->errors[PUNT_ERROR_UDP_PORT];
118 vlib_put_next_frame (vm, node, punt_next_punt (is_ip4), n_left_to_next);
121 return from_frame->n_vectors;
124 static char *punt_error_strings[] = {
125 #define punt_error(n,s) s,
126 #include "punt_error.def"
130 /** @brief IPv4 UDP punt node.
133 This is the IPv4 UDP punt transition node. It is registered as a next
134 node for the "ip4-udp-lookup" handling UDP port(s) requested for punt.
135 The buffer's current data pointer is adjusted to the original packet
136 IPv4 header. All buffers are dispatched to "error-punt".
138 @param vm vlib_main_t corresponding to the current thread
139 @param node vlib_node_runtime_t
140 @param frame vlib_frame_t whose contents should be dispatched
142 @par Graph mechanics: next index usage
145 - <code>vnet_buffer(b)->current_data</code>
146 - <code>vnet_buffer(b)->current_len</code>
149 - Dispatches the packet to the "error-punt" node
151 VLIB_NODE_FN (udp4_punt_node) (vlib_main_t * vm,
152 vlib_node_runtime_t * node,
153 vlib_frame_t * from_frame)
155 return udp46_punt_inline (vm, node, from_frame, 1 /* is_ip4 */ );
158 /** @brief IPv6 UDP punt node.
161 This is the IPv6 UDP punt transition node. It is registered as a next
162 node for the "ip6-udp-lookup" handling UDP port(s) requested for punt.
163 The buffer's current data pointer is adjusted to the original packet
164 IPv6 header. All buffers are dispatched to "error-punt".
166 @param vm vlib_main_t corresponding to the current thread
167 @param node vlib_node_runtime_t
168 @param frame vlib_frame_t whose contents should be dispatched
170 @par Graph mechanics: next index usage
173 - <code>vnet_buffer(b)->current_data</code>
174 - <code>vnet_buffer(b)->current_len</code>
177 - Dispatches the packet to the "error-punt" node
179 VLIB_NODE_FN (udp6_punt_node) (vlib_main_t * vm,
180 vlib_node_runtime_t * node,
181 vlib_frame_t * from_frame)
183 return udp46_punt_inline (vm, node, from_frame, 0 /* is_ip4 */ );
187 VLIB_REGISTER_NODE (udp4_punt_node) = {
188 .name = "ip4-udp-punt",
189 /* Takes a vector of packets. */
190 .vector_size = sizeof (u32),
192 .n_errors = PUNT_N_ERROR,
193 .error_strings = punt_error_strings,
195 .n_next_nodes = PUNT_N_NEXT,
197 #define _(s,n) [PUNT_NEXT_##s] = n,
203 VLIB_REGISTER_NODE (udp6_punt_node) = {
204 .name = "ip6-udp-punt",
205 /* Takes a vector of packets. */
206 .vector_size = sizeof (u32),
208 .n_errors = PUNT_N_ERROR,
209 .error_strings = punt_error_strings,
211 .n_next_nodes = PUNT_N_NEXT,
213 #define _(s,n) [PUNT_NEXT_##s] = n,
222 punt_client_t client;
228 format_udp_punt_trace (u8 * s, va_list * args)
230 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
231 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
232 udp_punt_trace_t *t = va_arg (*args, udp_punt_trace_t *);
233 u32 indent = format_get_indent (s);
234 s = format (s, "to: %s", t->client.caddr.sun_path);
237 s = format (s, "\n%U(buffer is part of chain)", format_white_space,
240 s = format (s, "\n%U%U", format_white_space, indent,
241 format_hex_bytes, t->packet_data, sizeof (t->packet_data));
247 punt_socket_inline (vlib_main_t * vm,
248 vlib_node_runtime_t * node,
249 vlib_frame_t * frame,
250 punt_type_t pt, ip_address_family_t af)
252 u32 *buffers = vlib_frame_vector_args (frame);
253 u32 thread_index = vm->thread_index;
254 uword n_packets = frame->n_vectors;
255 punt_main_t *pm = &punt_main;
258 punt_thread_data_t *ptd = &pm->thread_data[thread_index];
259 u32 node_index = (AF_IP4 == af ?
260 udp4_punt_socket_node.index :
261 udp6_punt_socket_node.index);
263 for (i = 0; i < n_packets; i++)
268 punt_packetdesc_t packetdesc;
271 b = vlib_get_buffer (vm, buffers[i]);
273 if (PUNT_TYPE_L4 == pt)
275 /* Reverse UDP Punt advance */
279 vlib_buffer_advance (b, -(sizeof (ip4_header_t) +
280 sizeof (udp_header_t)));
281 ip4_header_t *ip = vlib_buffer_get_current (b);
282 udp = (udp_header_t *) (ip + 1);
286 vlib_buffer_advance (b, -(sizeof (ip6_header_t) +
287 sizeof (udp_header_t)));
288 ip6_header_t *ip = vlib_buffer_get_current (b);
289 udp = (udp_header_t *) (ip + 1);
293 * Find registerered client
294 * If no registered client, drop packet and count
296 c = punt_client_l4_get (af, clib_net_to_host_u16 (udp->dst_port));
298 else if (PUNT_TYPE_IP_PROTO == pt)
300 /* Reverse UDP Punt advance */
305 ip4_header_t *ip = vlib_buffer_get_current (b);
306 proto = ip->protocol;
310 ip6_header_t *ip = vlib_buffer_get_current (b);
311 proto = ip->protocol;
314 c = punt_client_ip_proto_get (af, proto);
316 else if (PUNT_TYPE_EXCEPTION == pt)
318 c = punt_client_exception_get (b->punt_reason);
323 if (PREDICT_FALSE (NULL == c))
325 vlib_node_increment_counter (vm, node_index,
326 PUNT_ERROR_SOCKET_TX_ERROR, 1);
330 struct sockaddr_un *caddr = &c->caddr;
333 vec_reset_length (ptd->iovecs);
335 /* Add packet descriptor */
336 packetdesc.sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
337 packetdesc.action = 0;
338 vec_add2 (ptd->iovecs, iov, 1);
339 iov->iov_base = &packetdesc;
340 iov->iov_len = sizeof (packetdesc);
342 /** VLIB buffer chain -> Unix iovec(s). */
343 vlib_buffer_advance (b, -ethernet_buffer_header_size (b));
344 vec_add2 (ptd->iovecs, iov, 1);
345 iov->iov_base = b->data + b->current_data;
346 iov->iov_len = l = b->current_length;
348 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
351 t = vlib_add_trace (vm, node, b, sizeof (t[0]));
352 clib_memcpy_fast (&t->client, c, sizeof (t->client));
353 clib_memcpy_fast (t->packet_data,
354 vlib_buffer_get_current (b),
355 sizeof (t->packet_data));
358 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_NEXT_PRESENT))
362 b = vlib_get_buffer (vm, b->next_buffer);
363 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
366 t = vlib_add_trace (vm, node, b, sizeof (t[0]));
367 clib_memcpy_fast (&t->client, c, sizeof (t->client));
371 vec_add2 (ptd->iovecs, iov, 1);
373 iov->iov_base = b->data + b->current_data;
374 iov->iov_len = b->current_length;
375 l += b->current_length;
377 while (b->flags & VLIB_BUFFER_NEXT_PRESENT);
380 struct msghdr msg = {
382 .msg_namelen = sizeof (*caddr),
383 .msg_iov = ptd->iovecs,
384 .msg_iovlen = vec_len (ptd->iovecs),
387 if (sendmsg (pm->socket_fd, &msg, 0) < (ssize_t) l)
388 vlib_node_increment_counter (vm, node_index,
389 PUNT_ERROR_SOCKET_TX_ERROR, 1);
391 vlib_node_increment_counter (vm, node_index, PUNT_ERROR_SOCKET_TX, 1);
395 vlib_buffer_free (vm, buffers, n_packets);
401 udp4_punt_socket (vlib_main_t * vm,
402 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
404 return punt_socket_inline (vm, node, from_frame, PUNT_TYPE_L4, AF_IP4);
408 udp6_punt_socket (vlib_main_t * vm,
409 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
411 return punt_socket_inline (vm, node, from_frame, PUNT_TYPE_L4, AF_IP6);
415 ip4_proto_punt_socket (vlib_main_t * vm,
416 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
418 return punt_socket_inline (vm, node, from_frame,
419 PUNT_TYPE_IP_PROTO, AF_IP4);
423 ip6_proto_punt_socket (vlib_main_t * vm,
424 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
426 return punt_socket_inline (vm, node, from_frame,
427 PUNT_TYPE_IP_PROTO, AF_IP6);
431 exception_punt_socket (vlib_main_t * vm,
432 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
434 return punt_socket_inline (vm, node, from_frame,
435 PUNT_TYPE_EXCEPTION, AF_IP4);
440 VLIB_REGISTER_NODE (udp4_punt_socket_node) = {
441 .function = udp4_punt_socket,
442 .name = "ip4-udp-punt-socket",
443 .format_trace = format_udp_punt_trace,
444 .flags = VLIB_NODE_FLAG_IS_DROP,
445 /* Takes a vector of packets. */
446 .vector_size = sizeof (u32),
447 .n_errors = PUNT_N_ERROR,
448 .error_strings = punt_error_strings,
450 VLIB_REGISTER_NODE (udp6_punt_socket_node) = {
451 .function = udp6_punt_socket,
452 .name = "ip6-udp-punt-socket",
453 .format_trace = format_udp_punt_trace,
454 .flags = VLIB_NODE_FLAG_IS_DROP,
455 .vector_size = sizeof (u32),
456 .n_errors = PUNT_N_ERROR,
457 .error_strings = punt_error_strings,
459 VLIB_REGISTER_NODE (ip4_proto_punt_socket_node) = {
460 .function = ip4_proto_punt_socket,
461 .name = "ip4-proto-punt-socket",
462 .format_trace = format_udp_punt_trace,
463 .flags = VLIB_NODE_FLAG_IS_DROP,
464 /* Takes a vector of packets. */
465 .vector_size = sizeof (u32),
466 .n_errors = PUNT_N_ERROR,
467 .error_strings = punt_error_strings,
469 VLIB_REGISTER_NODE (ip6_proto_punt_socket_node) = {
470 .function = ip6_proto_punt_socket,
471 .name = "ip6-proto-punt-socket",
472 .format_trace = format_udp_punt_trace,
473 .flags = VLIB_NODE_FLAG_IS_DROP,
474 .vector_size = sizeof (u32),
475 .n_errors = PUNT_N_ERROR,
476 .error_strings = punt_error_strings,
478 VLIB_REGISTER_NODE (exception_punt_socket_node) = {
479 .function = exception_punt_socket,
480 .name = "exception-punt-socket",
481 .format_trace = format_udp_punt_trace,
482 .flags = VLIB_NODE_FLAG_IS_DROP,
483 .vector_size = sizeof (u32),
484 .n_errors = PUNT_N_ERROR,
485 .error_strings = punt_error_strings,
491 enum punt_action_e action;
496 format_punt_trace (u8 * s, va_list * va)
498 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
499 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
500 vnet_main_t *vnm = vnet_get_main ();
501 punt_trace_t *t = va_arg (*va, punt_trace_t *);
502 s = format (s, "%U Action: %d", format_vnet_sw_if_index_name,
503 vnm, t->sw_if_index, t->action);
508 punt_socket_rx_fd (vlib_main_t * vm, vlib_node_runtime_t * node, u32 fd)
510 const uword buffer_size = vlib_buffer_get_default_data_size (vm);
511 u32 n_trace = vlib_get_trace_count (vm, node);
512 u32 next = node->cached_next_index;
513 u32 n_left_to_next, next_index;
515 u32 error = PUNT_ERROR_NONE;
516 vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
518 /* $$$$ Only dealing with one buffer at the time for now */
522 punt_packetdesc_t packetdesc;
526 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
528 error = PUNT_ERROR_NOBUFFER;
532 b = vlib_get_buffer (vm, bi);
533 io[0].iov_base = &packetdesc;
534 io[0].iov_len = sizeof (packetdesc);
535 io[1].iov_base = b->data;
536 io[1].iov_len = buffer_size;
538 size = readv (fd, io, 2);
539 /* We need at least the packet descriptor plus a header */
540 if (size <= (int) (sizeof (packetdesc) + sizeof (ip4_header_t)))
542 vlib_buffer_free (vm, &bi, 1);
543 error = PUNT_ERROR_READV;
547 b->flags = VNET_BUFFER_F_LOCALLY_ORIGINATED;
548 b->current_length = size - sizeof (packetdesc);
550 switch (packetdesc.action)
553 vnet_buffer (b)->sw_if_index[VLIB_TX] = packetdesc.sw_if_index;
554 next_index = PUNT_SOCKET_RX_NEXT_INTERFACE_OUTPUT;
557 case PUNT_IP4_ROUTED:
558 vnet_buffer (b)->sw_if_index[VLIB_RX] = packetdesc.sw_if_index;
559 vnet_buffer (b)->sw_if_index[VLIB_TX] = ~0;
560 next_index = PUNT_SOCKET_RX_NEXT_IP4_LOOKUP;
563 case PUNT_IP6_ROUTED:
564 vnet_buffer (b)->sw_if_index[VLIB_RX] = packetdesc.sw_if_index;
565 vnet_buffer (b)->sw_if_index[VLIB_TX] = ~0;
566 next_index = PUNT_SOCKET_RX_NEXT_IP6_LOOKUP;
570 error = PUNT_ERROR_ACTION;
571 vlib_buffer_free (vm, &bi, 1);
577 && vlib_trace_buffer (vm, node, next_index, b, 1 /* follow_chain */ )))
580 vlib_set_trace_count (vm, node, --n_trace);
581 t = vlib_add_trace (vm, node, b, sizeof (*t));
582 t->sw_if_index = packetdesc.sw_if_index;
583 t->action = packetdesc.action;
590 vlib_validate_buffer_enqueue_x1 (vm, node, next, to_next, n_left_to_next,
592 vlib_put_next_frame (vm, node, next, n_left_to_next);
597 vlib_put_next_frame (vm, node, next, n_left_to_next);
598 vlib_node_increment_counter (vm, punt_socket_rx_node.index, error, 1);
603 punt_socket_rx (vlib_main_t * vm,
604 vlib_node_runtime_t * node, vlib_frame_t * frame)
606 punt_main_t *pm = &punt_main;
610 for (i = 0; i < vec_len (pm->ready_fds); i++)
612 total_count += punt_socket_rx_fd (vm, node, pm->ready_fds[i]);
613 vec_del1 (pm->ready_fds, i);
619 VLIB_REGISTER_NODE (punt_socket_rx_node) =
621 .function = punt_socket_rx,
622 .name = "punt-socket-rx",
623 .flags = VLIB_NODE_FLAG_TRACE_SUPPORTED,
624 .type = VLIB_NODE_TYPE_INPUT,
625 .state = VLIB_NODE_STATE_INTERRUPT,
627 .n_errors = PUNT_N_ERROR,
628 .error_strings = punt_error_strings,
629 .n_next_nodes = PUNT_SOCKET_RX_N_NEXT,
631 [PUNT_SOCKET_RX_NEXT_INTERFACE_OUTPUT] = "interface-output",
632 [PUNT_SOCKET_RX_NEXT_IP4_LOOKUP] = "ip4-lookup",
633 [PUNT_SOCKET_RX_NEXT_IP6_LOOKUP] = "ip6-lookup",
635 .format_trace = format_punt_trace,
640 * fd.io coding-style-patch-verification: ON
643 * eval: (c-set-style "gnu")