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/ip/punt.h>
28 #include <vlib/unix/unix.h>
32 #include <sys/socket.h>
38 #define punt_error(n,s) PUNT_ERROR_##n,
39 #include <vnet/ip/punt_error.def>
44 #define foreach_punt_next \
45 _ (PUNT4, "ip4-punt") \
50 #define _(s,n) PUNT_NEXT_##s,
56 enum punt_socket_rx_next_e
58 PUNT_SOCKET_RX_NEXT_INTERFACE_OUTPUT,
59 PUNT_SOCKET_RX_NEXT_IP4_LOOKUP,
60 PUNT_SOCKET_RX_NEXT_IP6_LOOKUP,
64 #define punt_next_punt(is_ip4) (is_ip4 ? PUNT_NEXT_PUNT4 : PUNT_NEXT_PUNT6)
66 /** @brief IPv4/IPv6 UDP punt node main loop.
68 This is the main loop inline function for IPv4/IPv6 UDP punt
71 @param vm vlib_main_t corresponding to the current thread
72 @param node vlib_node_runtime_t
73 @param frame vlib_frame_t whose contents should be dispatched
74 @param is_ipv4 indicates if called for IPv4 or IPv6 node
77 udp46_punt_inline (vlib_main_t * vm,
78 vlib_node_runtime_t * node,
79 vlib_frame_t * from_frame, int is_ip4)
81 u32 n_left_from, *from, *to_next;
84 from = vlib_frame_vector_args (from_frame);
85 n_left_from = from_frame->n_vectors;
87 /* udp[46]_lookup hands us the data payload, not the IP header */
89 advance = -(sizeof (ip4_header_t) + sizeof (udp_header_t));
91 advance = -(sizeof (ip6_header_t) + sizeof (udp_header_t));
93 while (n_left_from > 0)
97 vlib_get_next_frame (vm, node, punt_next_punt (is_ip4), to_next,
100 while (n_left_from > 0 && n_left_to_next > 0)
112 b0 = vlib_get_buffer (vm, bi0);
113 vlib_buffer_advance (b0, advance);
114 b0->error = node->errors[PUNT_ERROR_UDP_PORT];
117 vlib_put_next_frame (vm, node, punt_next_punt (is_ip4), n_left_to_next);
120 return from_frame->n_vectors;
123 static char *punt_error_strings[] = {
124 #define punt_error(n,s) s,
125 #include "punt_error.def"
129 /** @brief IPv4 UDP punt node.
132 This is the IPv4 UDP punt transition node. It is registered as a next
133 node for the "ip4-udp-lookup" handling UDP port(s) requested for punt.
134 The buffer's current data pointer is adjusted to the original packet
135 IPv4 header. All buffers are dispatched to "error-punt".
137 @param vm vlib_main_t corresponding to the current thread
138 @param node vlib_node_runtime_t
139 @param frame vlib_frame_t whose contents should be dispatched
141 @par Graph mechanics: next index usage
144 - <code>vnet_buffer(b)->current_data</code>
145 - <code>vnet_buffer(b)->current_len</code>
148 - Dispatches the packet to the "error-punt" node
150 VLIB_NODE_FN (udp4_punt_node) (vlib_main_t * vm,
151 vlib_node_runtime_t * node,
152 vlib_frame_t * from_frame)
154 return udp46_punt_inline (vm, node, from_frame, 1 /* is_ip4 */ );
157 /** @brief IPv6 UDP punt node.
160 This is the IPv6 UDP punt transition node. It is registered as a next
161 node for the "ip6-udp-lookup" handling UDP port(s) requested for punt.
162 The buffer's current data pointer is adjusted to the original packet
163 IPv6 header. All buffers are dispatched to "error-punt".
165 @param vm vlib_main_t corresponding to the current thread
166 @param node vlib_node_runtime_t
167 @param frame vlib_frame_t whose contents should be dispatched
169 @par Graph mechanics: next index usage
172 - <code>vnet_buffer(b)->current_data</code>
173 - <code>vnet_buffer(b)->current_len</code>
176 - Dispatches the packet to the "error-punt" node
178 VLIB_NODE_FN (udp6_punt_node) (vlib_main_t * vm,
179 vlib_node_runtime_t * node,
180 vlib_frame_t * from_frame)
182 return udp46_punt_inline (vm, node, from_frame, 0 /* is_ip4 */ );
186 VLIB_REGISTER_NODE (udp4_punt_node) = {
187 .name = "ip4-udp-punt",
188 /* Takes a vector of packets. */
189 .vector_size = sizeof (u32),
191 .n_errors = PUNT_N_ERROR,
192 .error_strings = punt_error_strings,
194 .n_next_nodes = PUNT_N_NEXT,
196 #define _(s,n) [PUNT_NEXT_##s] = n,
202 VLIB_REGISTER_NODE (udp6_punt_node) = {
203 .name = "ip6-udp-punt",
204 /* Takes a vector of packets. */
205 .vector_size = sizeof (u32),
207 .n_errors = PUNT_N_ERROR,
208 .error_strings = punt_error_strings,
210 .n_next_nodes = PUNT_N_NEXT,
212 #define _(s,n) [PUNT_NEXT_##s] = n,
221 punt_client_t client;
227 format_udp_punt_trace (u8 * s, va_list * args)
229 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
230 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
231 udp_punt_trace_t *t = va_arg (*args, udp_punt_trace_t *);
232 u32 indent = format_get_indent (s);
233 s = format (s, "to: %s", t->client.caddr.sun_path);
236 s = format (s, "\n%U(buffer is part of chain)", format_white_space,
239 s = format (s, "\n%U%U", format_white_space, indent,
240 format_hex_bytes, t->packet_data, sizeof (t->packet_data));
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 u32 thread_index = vm->thread_index;
253 uword n_packets = frame->n_vectors;
254 punt_main_t *pm = &punt_main;
257 punt_thread_data_t *ptd = &pm->thread_data[thread_index];
258 u32 node_index = (AF_IP4 == af ?
259 udp4_punt_socket_node.index :
260 udp6_punt_socket_node.index);
262 for (i = 0; i < n_packets; i++)
267 punt_packetdesc_t packetdesc;
270 b = vlib_get_buffer (vm, buffers[i]);
272 if (PUNT_TYPE_L4 == pt)
274 /* Reverse UDP Punt advance */
278 vlib_buffer_advance (b, -(sizeof (ip4_header_t) +
279 sizeof (udp_header_t)));
280 ip4_header_t *ip = vlib_buffer_get_current (b);
281 udp = (udp_header_t *) (ip + 1);
285 vlib_buffer_advance (b, -(sizeof (ip6_header_t) +
286 sizeof (udp_header_t)));
287 ip6_header_t *ip = vlib_buffer_get_current (b);
288 udp = (udp_header_t *) (ip + 1);
292 * Find registerered client
293 * If no registered client, drop packet and count
295 c = punt_client_l4_get (af, clib_net_to_host_u16 (udp->dst_port));
297 else if (PUNT_TYPE_IP_PROTO == pt)
299 /* Reverse UDP Punt advance */
304 ip4_header_t *ip = vlib_buffer_get_current (b);
305 proto = ip->protocol;
309 ip6_header_t *ip = vlib_buffer_get_current (b);
310 proto = ip->protocol;
313 c = punt_client_ip_proto_get (af, proto);
315 else if (PUNT_TYPE_EXCEPTION == pt)
317 c = punt_client_exception_get (b->punt_reason);
322 if (PREDICT_FALSE (NULL == c))
324 vlib_node_increment_counter (vm, node_index,
325 PUNT_ERROR_SOCKET_TX_ERROR, 1);
329 struct sockaddr_un *caddr = &c->caddr;
332 vec_reset_length (ptd->iovecs);
334 /* Add packet descriptor */
335 packetdesc.sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
336 packetdesc.action = 0;
337 vec_add2 (ptd->iovecs, iov, 1);
338 iov->iov_base = &packetdesc;
339 iov->iov_len = sizeof (packetdesc);
341 /** VLIB buffer chain -> Unix iovec(s). */
342 vlib_buffer_advance (b, -(sizeof (ethernet_header_t)));
343 vec_add2 (ptd->iovecs, iov, 1);
344 iov->iov_base = b->data + b->current_data;
345 iov->iov_len = l = b->current_length;
347 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
350 t = vlib_add_trace (vm, node, b, sizeof (t[0]));
351 clib_memcpy_fast (&t->client, c, sizeof (t->client));
352 clib_memcpy_fast (t->packet_data,
353 vlib_buffer_get_current (b),
354 sizeof (t->packet_data));
357 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_NEXT_PRESENT))
361 b = vlib_get_buffer (vm, b->next_buffer);
362 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
365 t = vlib_add_trace (vm, node, b, sizeof (t[0]));
366 clib_memcpy_fast (&t->client, c, sizeof (t->client));
370 vec_add2 (ptd->iovecs, iov, 1);
372 iov->iov_base = b->data + b->current_data;
373 iov->iov_len = b->current_length;
374 l += b->current_length;
376 while (b->flags & VLIB_BUFFER_NEXT_PRESENT);
379 struct msghdr msg = {
381 .msg_namelen = sizeof (*caddr),
382 .msg_iov = ptd->iovecs,
383 .msg_iovlen = vec_len (ptd->iovecs),
386 if (sendmsg (pm->socket_fd, &msg, 0) < (ssize_t) l)
387 vlib_node_increment_counter (vm, node_index,
388 PUNT_ERROR_SOCKET_TX_ERROR, 1);
390 vlib_node_increment_counter (vm, node_index, PUNT_ERROR_SOCKET_TX, 1);
394 vlib_buffer_free (vm, buffers, n_packets);
400 udp4_punt_socket (vlib_main_t * vm,
401 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
403 return punt_socket_inline (vm, node, from_frame, PUNT_TYPE_L4, AF_IP4);
407 udp6_punt_socket (vlib_main_t * vm,
408 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
410 return punt_socket_inline (vm, node, from_frame, PUNT_TYPE_L4, AF_IP6);
414 ip4_proto_punt_socket (vlib_main_t * vm,
415 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
417 return punt_socket_inline (vm, node, from_frame,
418 PUNT_TYPE_IP_PROTO, AF_IP4);
422 ip6_proto_punt_socket (vlib_main_t * vm,
423 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
425 return punt_socket_inline (vm, node, from_frame,
426 PUNT_TYPE_IP_PROTO, AF_IP6);
430 exception_punt_socket (vlib_main_t * vm,
431 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
433 return punt_socket_inline (vm, node, from_frame,
434 PUNT_TYPE_EXCEPTION, AF_IP4);
439 VLIB_REGISTER_NODE (udp4_punt_socket_node) = {
440 .function = udp4_punt_socket,
441 .name = "ip4-udp-punt-socket",
442 .format_trace = format_udp_punt_trace,
443 .flags = VLIB_NODE_FLAG_IS_DROP,
444 /* Takes a vector of packets. */
445 .vector_size = sizeof (u32),
446 .n_errors = PUNT_N_ERROR,
447 .error_strings = punt_error_strings,
449 VLIB_REGISTER_NODE (udp6_punt_socket_node) = {
450 .function = udp6_punt_socket,
451 .name = "ip6-udp-punt-socket",
452 .format_trace = format_udp_punt_trace,
453 .flags = VLIB_NODE_FLAG_IS_DROP,
454 .vector_size = sizeof (u32),
455 .n_errors = PUNT_N_ERROR,
456 .error_strings = punt_error_strings,
458 VLIB_REGISTER_NODE (ip4_proto_punt_socket_node) = {
459 .function = ip4_proto_punt_socket,
460 .name = "ip4-proto-punt-socket",
461 .format_trace = format_udp_punt_trace,
462 .flags = VLIB_NODE_FLAG_IS_DROP,
463 /* Takes a vector of packets. */
464 .vector_size = sizeof (u32),
465 .n_errors = PUNT_N_ERROR,
466 .error_strings = punt_error_strings,
468 VLIB_REGISTER_NODE (ip6_proto_punt_socket_node) = {
469 .function = ip6_proto_punt_socket,
470 .name = "ip6-proto-punt-socket",
471 .format_trace = format_udp_punt_trace,
472 .flags = VLIB_NODE_FLAG_IS_DROP,
473 .vector_size = sizeof (u32),
474 .n_errors = PUNT_N_ERROR,
475 .error_strings = punt_error_strings,
477 VLIB_REGISTER_NODE (exception_punt_socket_node) = {
478 .function = exception_punt_socket,
479 .name = "exception-punt-socket",
480 .format_trace = format_udp_punt_trace,
481 .flags = VLIB_NODE_FLAG_IS_DROP,
482 .vector_size = sizeof (u32),
483 .n_errors = PUNT_N_ERROR,
484 .error_strings = punt_error_strings,
490 enum punt_action_e action;
495 format_punt_trace (u8 * s, va_list * va)
497 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
498 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
499 vnet_main_t *vnm = vnet_get_main ();
500 punt_trace_t *t = va_arg (*va, punt_trace_t *);
501 s = format (s, "%U Action: %d", format_vnet_sw_if_index_name,
502 vnm, t->sw_if_index, t->action);
507 punt_socket_rx_fd (vlib_main_t * vm, vlib_node_runtime_t * node, u32 fd)
509 const uword buffer_size = vlib_buffer_get_default_data_size (vm);
510 u32 n_trace = vlib_get_trace_count (vm, node);
511 u32 next = node->cached_next_index;
512 u32 n_left_to_next, next_index;
514 u32 error = PUNT_ERROR_NONE;
515 vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
517 /* $$$$ Only dealing with one buffer at the time for now */
521 punt_packetdesc_t packetdesc;
525 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
527 error = PUNT_ERROR_NOBUFFER;
531 b = vlib_get_buffer (vm, bi);
532 io[0].iov_base = &packetdesc;
533 io[0].iov_len = sizeof (packetdesc);
534 io[1].iov_base = b->data;
535 io[1].iov_len = buffer_size;
537 size = readv (fd, io, 2);
538 /* We need at least the packet descriptor plus a header */
539 if (size <= (int) (sizeof (packetdesc) + sizeof (ip4_header_t)))
541 vlib_buffer_free (vm, &bi, 1);
542 error = PUNT_ERROR_READV;
546 b->flags = VNET_BUFFER_F_LOCALLY_ORIGINATED;
547 b->current_length = size - sizeof (packetdesc);
549 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b);
551 switch (packetdesc.action)
554 vnet_buffer (b)->sw_if_index[VLIB_TX] = packetdesc.sw_if_index;
555 next_index = PUNT_SOCKET_RX_NEXT_INTERFACE_OUTPUT;
558 case PUNT_IP4_ROUTED:
559 vnet_buffer (b)->sw_if_index[VLIB_RX] = packetdesc.sw_if_index;
560 vnet_buffer (b)->sw_if_index[VLIB_TX] = ~0;
561 next_index = PUNT_SOCKET_RX_NEXT_IP4_LOOKUP;
564 case PUNT_IP6_ROUTED:
565 vnet_buffer (b)->sw_if_index[VLIB_RX] = packetdesc.sw_if_index;
566 vnet_buffer (b)->sw_if_index[VLIB_TX] = ~0;
567 next_index = PUNT_SOCKET_RX_NEXT_IP6_LOOKUP;
571 error = PUNT_ERROR_ACTION;
572 vlib_buffer_free (vm, &bi, 1);
578 && vlib_trace_buffer (vm, node, next_index, b, 1 /* follow_chain */ )))
581 vlib_set_trace_count (vm, node, --n_trace);
582 t = vlib_add_trace (vm, node, b, sizeof (*t));
583 t->sw_if_index = packetdesc.sw_if_index;
584 t->action = packetdesc.action;
591 vlib_validate_buffer_enqueue_x1 (vm, node, next, to_next, n_left_to_next,
593 vlib_put_next_frame (vm, node, next, n_left_to_next);
598 vlib_put_next_frame (vm, node, next, n_left_to_next);
599 vlib_node_increment_counter (vm, punt_socket_rx_node.index, error, 1);
604 punt_socket_rx (vlib_main_t * vm,
605 vlib_node_runtime_t * node, vlib_frame_t * frame)
607 punt_main_t *pm = &punt_main;
611 for (i = 0; i < vec_len (pm->ready_fds); i++)
613 total_count += punt_socket_rx_fd (vm, node, pm->ready_fds[i]);
614 vec_del1 (pm->ready_fds, i);
620 VLIB_REGISTER_NODE (punt_socket_rx_node) =
622 .function = punt_socket_rx,
623 .name = "punt-socket-rx",
624 .flags = VLIB_NODE_FLAG_TRACE_SUPPORTED,
625 .type = VLIB_NODE_TYPE_INPUT,
626 .state = VLIB_NODE_STATE_INTERRUPT,
628 .n_errors = PUNT_N_ERROR,
629 .error_strings = punt_error_strings,
630 .n_next_nodes = PUNT_SOCKET_RX_N_NEXT,
632 [PUNT_SOCKET_RX_NEXT_INTERFACE_OUTPUT] = "interface-output",
633 [PUNT_SOCKET_RX_NEXT_IP4_LOOKUP] = "ip4-lookup",
634 [PUNT_SOCKET_RX_NEXT_IP6_LOOKUP] = "ip6-lookup",
636 .format_trace = format_punt_trace,
641 * fd.io coding-style-patch-verification: ON
644 * eval: (c-set-style "gnu")