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 <vppinfra/sparse_vec.h>
33 #include <vlib/unix/unix.h>
37 #include <sys/socket.h>
43 #define foreach_punt_next \
44 _ (PUNT4, "ip4-punt") \
49 #define _(s,n) PUNT_NEXT_##s,
55 enum punt_socket_rx_next_e
57 PUNT_SOCKET_RX_NEXT_INTERFACE_OUTPUT,
58 PUNT_SOCKET_RX_NEXT_IP4_LOOKUP,
59 PUNT_SOCKET_RX_NEXT_IP6_LOOKUP,
63 #define punt_next_punt(is_ip4) (is_ip4 ? PUNT_NEXT_PUNT4 : PUNT_NEXT_PUNT6)
65 vlib_node_registration_t udp4_punt_node;
66 vlib_node_registration_t udp6_punt_node;
67 vlib_node_registration_t udp4_punt_socket_node;
68 vlib_node_registration_t udp6_punt_socket_node;
69 static vlib_node_registration_t punt_socket_rx_node;
71 punt_main_t punt_main;
74 vnet_punt_get_server_pathname (void)
76 punt_main_t *pm = &punt_main;
80 /** @brief IPv4/IPv6 UDP punt node main loop.
82 This is the main loop inline function for IPv4/IPv6 UDP punt
85 @param vm vlib_main_t corresponding to the current thread
86 @param node vlib_node_runtime_t
87 @param frame vlib_frame_t whose contents should be dispatched
88 @param is_ipv4 indicates if called for IPv4 or IPv6 node
91 udp46_punt_inline (vlib_main_t * vm,
92 vlib_node_runtime_t * node,
93 vlib_frame_t * from_frame, int is_ip4)
95 u32 n_left_from, *from, *to_next;
98 from = vlib_frame_vector_args (from_frame);
99 n_left_from = from_frame->n_vectors;
101 /* udp[46]_lookup hands us the data payload, not the IP header */
103 advance = -(sizeof (ip4_header_t) + sizeof (udp_header_t));
105 advance = -(sizeof (ip6_header_t) + sizeof (udp_header_t));
107 while (n_left_from > 0)
111 vlib_get_next_frame (vm, node, punt_next_punt (is_ip4), to_next,
114 while (n_left_from > 0 && n_left_to_next > 0)
126 b0 = vlib_get_buffer (vm, bi0);
127 vlib_buffer_advance (b0, advance);
128 b0->error = node->errors[PUNT_ERROR_UDP_PORT];
131 vlib_put_next_frame (vm, node, punt_next_punt (is_ip4), n_left_to_next);
134 return from_frame->n_vectors;
137 static char *punt_error_strings[] = {
138 #define punt_error(n,s) s,
139 #include "punt_error.def"
143 /** @brief IPv4 UDP punt node.
146 This is the IPv4 UDP punt transition node. It is registered as a next
147 node for the "ip4-udp-lookup" handling UDP port(s) requested for punt.
148 The buffer's current data pointer is adjusted to the original packet
149 IPv4 header. All buffers are dispatched to "error-punt".
151 @param vm vlib_main_t corresponding to the current thread
152 @param node vlib_node_runtime_t
153 @param frame vlib_frame_t whose contents should be dispatched
155 @par Graph mechanics: next index usage
158 - <code>vnet_buffer(b)->current_data</code>
159 - <code>vnet_buffer(b)->current_len</code>
162 - Dispatches the packet to the "error-punt" node
165 udp4_punt (vlib_main_t * vm,
166 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
168 return udp46_punt_inline (vm, node, from_frame, 1 /* is_ip4 */ );
171 /** @brief IPv6 UDP punt node.
174 This is the IPv6 UDP punt transition node. It is registered as a next
175 node for the "ip6-udp-lookup" handling UDP port(s) requested for punt.
176 The buffer's current data pointer is adjusted to the original packet
177 IPv6 header. All buffers are dispatched to "error-punt".
179 @param vm vlib_main_t corresponding to the current thread
180 @param node vlib_node_runtime_t
181 @param frame vlib_frame_t whose contents should be dispatched
183 @par Graph mechanics: next index usage
186 - <code>vnet_buffer(b)->current_data</code>
187 - <code>vnet_buffer(b)->current_len</code>
190 - Dispatches the packet to the "error-punt" node
193 udp6_punt (vlib_main_t * vm,
194 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
196 return udp46_punt_inline (vm, node, from_frame, 0 /* is_ip4 */ );
200 VLIB_REGISTER_NODE (udp4_punt_node) = {
201 .function = udp4_punt,
202 .name = "ip4-udp-punt",
203 /* Takes a vector of packets. */
204 .vector_size = sizeof (u32),
206 .n_errors = PUNT_N_ERROR,
207 .error_strings = punt_error_strings,
209 .n_next_nodes = PUNT_N_NEXT,
211 #define _(s,n) [PUNT_NEXT_##s] = n,
217 VLIB_NODE_FUNCTION_MULTIARCH (udp4_punt_node, udp4_punt);
219 VLIB_REGISTER_NODE (udp6_punt_node) = {
220 .function = udp6_punt,
221 .name = "ip6-udp-punt",
222 /* Takes a vector of packets. */
223 .vector_size = sizeof (u32),
225 .n_errors = PUNT_N_ERROR,
226 .error_strings = punt_error_strings,
228 .n_next_nodes = PUNT_N_NEXT,
230 #define _(s,n) [PUNT_NEXT_##s] = n,
236 VLIB_NODE_FUNCTION_MULTIARCH (udp6_punt_node, udp6_punt);;
240 static punt_client_t *
241 punt_client_get (bool is_ip4, u16 port)
243 punt_main_t *pm = &punt_main;
245 is_ip4 ? pm->clients_by_dst_port4 : pm->clients_by_dst_port6;
247 u16 i = sparse_vec_index (v, port);
248 if (i == SPARSE_VEC_INVALID_INDEX)
251 return &vec_elt (v, i);
254 static struct sockaddr_un *
255 punt_socket_get (bool is_ip4, u16 port)
257 punt_client_t *v = punt_client_get (is_ip4, port);
265 punt_socket_register (bool is_ip4, u8 protocol, u16 port,
266 char *client_pathname)
268 punt_main_t *pm = &punt_main;
270 punt_client_t *v = is_ip4 ? pm->clients_by_dst_port4 :
271 pm->clients_by_dst_port6;
273 memset (&c, 0, sizeof (c));
274 memcpy (c.caddr.sun_path, client_pathname, sizeof (c.caddr.sun_path));
275 c.caddr.sun_family = AF_UNIX;
277 n = sparse_vec_validate (v, port);
281 /* $$$$ Just leaves the mapping in place for now */
283 punt_socket_unregister (bool is_ip4, u8 protocol, u16 port)
290 punt_client_t client;
295 format_udp_punt_trace (u8 * s, va_list * args)
297 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
298 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
299 udp_punt_trace_t *t = va_arg (*args, udp_punt_trace_t *);
300 u32 indent = format_get_indent (s);
301 s = format (s, "to: %s", t->client.caddr.sun_path);
304 s = format (s, "\n%U(buffer is part of chain)", format_white_space,
311 udp46_punt_socket_inline (vlib_main_t * vm,
312 vlib_node_runtime_t * node,
313 vlib_frame_t * frame, bool is_ip4)
315 u32 *buffers = vlib_frame_args (frame);
316 uword n_packets = frame->n_vectors;
317 struct iovec *iovecs = 0;
318 punt_main_t *pm = &punt_main;
321 u32 node_index = is_ip4 ? udp4_punt_socket_node.index :
322 udp6_punt_socket_node.index;
324 for (i = 0; i < n_packets; i++)
329 punt_packetdesc_t packetdesc;
331 b = vlib_get_buffer (vm, buffers[i]);
333 /* Reverse UDP Punt advance */
337 vlib_buffer_advance (b, -(sizeof (ip4_header_t) +
338 sizeof (udp_header_t)));
339 ip4_header_t *ip = vlib_buffer_get_current (b);
340 udp = (udp_header_t *) (ip + 1);
344 vlib_buffer_advance (b, -(sizeof (ip6_header_t) +
345 sizeof (udp_header_t)));
346 ip6_header_t *ip = vlib_buffer_get_current (b);
347 udp = (udp_header_t *) (ip + 1);
350 u16 port = clib_net_to_host_u16 (udp->dst_port);
353 * Find registerered client
354 * If no registered client, drop packet and count
356 struct sockaddr_un *caddr;
357 caddr = punt_socket_get (is_ip4, port);
360 vlib_node_increment_counter (vm, node_index,
361 PUNT_ERROR_SOCKET_TX_ERROR, 1);
365 punt_client_t *c = NULL;
366 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
368 c = punt_client_get (is_ip4, port);
370 t = vlib_add_trace (vm, node, b, sizeof (t[0]));
371 clib_memcpy (&t->client, c, sizeof (t->client));
374 /* Re-set iovecs if present. */
376 _vec_len (iovecs) = 0;
378 /* Add packet descriptor */
379 packetdesc.sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
380 packetdesc.action = 0;
381 vec_add2 (iovecs, iov, 1);
382 iov->iov_base = &packetdesc;
383 iov->iov_len = sizeof (packetdesc);
385 /** VLIB buffer chain -> Unix iovec(s). */
386 vlib_buffer_advance (b, -(sizeof (ethernet_header_t)));
387 vec_add2 (iovecs, iov, 1);
388 iov->iov_base = b->data + b->current_data;
389 iov->iov_len = l = b->current_length;
391 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_NEXT_PRESENT))
395 b = vlib_get_buffer (vm, b->next_buffer);
396 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
398 if (PREDICT_FALSE (!c))
400 c = punt_client_get (is_ip4, port);
403 t = vlib_add_trace (vm, node, b, sizeof (t[0]));
404 clib_memcpy (&t->client, c, sizeof (t->client));
408 vec_add2 (iovecs, iov, 1);
410 iov->iov_base = b->data + b->current_data;
411 iov->iov_len = b->current_length;
412 l += b->current_length;
414 while (b->flags & VLIB_BUFFER_NEXT_PRESENT);
417 struct msghdr msg = {
419 .msg_namelen = sizeof (*caddr),
421 .msg_iovlen = vec_len (iovecs),
424 if (sendmsg (pm->socket_fd, &msg, 0) < (ssize_t) l)
425 vlib_node_increment_counter (vm, node_index,
426 PUNT_ERROR_SOCKET_TX_ERROR, 1);
430 vlib_buffer_free (vm, buffers, n_packets);
436 udp4_punt_socket (vlib_main_t * vm,
437 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
439 return udp46_punt_socket_inline (vm, node, from_frame, true /* is_ip4 */ );
443 udp6_punt_socket (vlib_main_t * vm,
444 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
446 return udp46_punt_socket_inline (vm, node, from_frame, false /* is_ip4 */ );
451 VLIB_REGISTER_NODE (udp4_punt_socket_node) = {
452 .function = udp4_punt_socket,
453 .name = "ip4-udp-punt-socket",
454 .format_trace = format_udp_punt_trace,
455 .flags = VLIB_NODE_FLAG_IS_DROP,
456 /* Takes a vector of packets. */
457 .vector_size = sizeof (u32),
458 .n_errors = PUNT_N_ERROR,
459 .error_strings = punt_error_strings,
461 VLIB_REGISTER_NODE (udp6_punt_socket_node) = {
462 .function = udp6_punt_socket,
463 .name = "ip6-udp-punt-socket",
464 .format_trace = format_udp_punt_trace,
465 .flags = VLIB_NODE_FLAG_IS_DROP,
466 .vector_size = sizeof (u32),
467 .n_errors = PUNT_N_ERROR,
468 .error_strings = punt_error_strings,
474 enum punt_action_e action;
479 format_punt_trace (u8 * s, va_list * va)
481 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
482 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
483 vnet_main_t *vnm = vnet_get_main ();
484 punt_trace_t *t = va_arg (*va, punt_trace_t *);
485 s = format (s, "%U Action: %d", format_vnet_sw_if_index_name,
486 vnm, t->sw_if_index, t->action);
491 punt_socket_rx_fd (vlib_main_t * vm, vlib_node_runtime_t * node, u32 fd)
493 const uword buffer_size = VLIB_BUFFER_DATA_SIZE;
494 u32 n_trace = vlib_get_trace_count (vm, node);
495 u32 next = node->cached_next_index;
496 u32 n_left_to_next, next_index;
498 u32 error = PUNT_ERROR_NONE;
499 vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
501 /* $$$$ Only dealing with one buffer at the time for now */
505 punt_packetdesc_t packetdesc;
509 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
511 error = PUNT_ERROR_NOBUFFER;
515 b = vlib_get_buffer (vm, bi);
516 io[0].iov_base = &packetdesc;
517 io[0].iov_len = sizeof (packetdesc);
518 io[1].iov_base = b->data;
519 io[1].iov_len = buffer_size;
521 size = readv (fd, io, 2);
522 /* We need at least the packet descriptor plus a header */
523 if (size <= (int) (sizeof (packetdesc) + sizeof (ip4_header_t)))
525 vlib_buffer_free (vm, &bi, 1);
526 error = PUNT_ERROR_READV;
530 b->flags = VNET_BUFFER_F_LOCALLY_ORIGINATED;
531 b->current_length = size - sizeof (packetdesc);
533 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b);
535 switch (packetdesc.action)
538 vnet_buffer (b)->sw_if_index[VLIB_TX] = packetdesc.sw_if_index;
539 next_index = PUNT_SOCKET_RX_NEXT_INTERFACE_OUTPUT;
542 case PUNT_IP4_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_IP4_LOOKUP;
548 case PUNT_IP6_ROUTED:
549 vnet_buffer (b)->sw_if_index[VLIB_RX] = packetdesc.sw_if_index;
550 vnet_buffer (b)->sw_if_index[VLIB_TX] = ~0;
551 next_index = PUNT_SOCKET_RX_NEXT_IP6_LOOKUP;
555 error = PUNT_ERROR_ACTION;
556 vlib_buffer_free (vm, &bi, 1);
560 if (PREDICT_FALSE (n_trace > 0))
563 vlib_trace_buffer (vm, node, next_index, b, 1 /* follow_chain */ );
564 vlib_set_trace_count (vm, node, --n_trace);
565 t = vlib_add_trace (vm, node, b, sizeof (*t));
566 t->sw_if_index = packetdesc.sw_if_index;
567 t->action = packetdesc.action;
574 vlib_validate_buffer_enqueue_x1 (vm, node, next, to_next, n_left_to_next,
576 vlib_put_next_frame (vm, node, next, n_left_to_next);
580 vlib_node_increment_counter (vm, punt_socket_rx_node.index, error, 1);
585 punt_socket_rx (vlib_main_t * vm,
586 vlib_node_runtime_t * node, vlib_frame_t * frame)
588 punt_main_t *pm = &punt_main;
592 for (i = 0; i < vec_len (pm->ready_fds); i++)
594 total_count += punt_socket_rx_fd (vm, node, pm->ready_fds[i]);
595 vec_del1 (pm->ready_fds, i);
600 VLIB_REGISTER_NODE (punt_socket_rx_node, static) =
602 .function = punt_socket_rx,.name = "punt-socket-rx",.type =
603 VLIB_NODE_TYPE_INPUT,.state = VLIB_NODE_STATE_INTERRUPT,.vector_size =
604 1,.n_errors = PUNT_N_ERROR,.error_strings =
605 punt_error_strings,.n_next_nodes = PUNT_SOCKET_RX_N_NEXT,.next_nodes =
607 [PUNT_SOCKET_RX_NEXT_INTERFACE_OUTPUT] = "interface-output",
608 [PUNT_SOCKET_RX_NEXT_IP4_LOOKUP] = "ip4-lookup",
609 [PUNT_SOCKET_RX_NEXT_IP6_LOOKUP] = "ip6-lookup",},.format_trace =
612 static clib_error_t *
613 punt_socket_read_ready (clib_file_t * uf)
615 vlib_main_t *vm = vlib_get_main ();
616 punt_main_t *pm = &punt_main;
618 /** Schedule the rx node */
619 vlib_node_set_interrupt_pending (vm, punt_socket_rx_node.index);
620 vec_add1 (pm->ready_fds, uf->file_descriptor);
626 vnet_punt_socket_add (vlib_main_t * vm, u32 header_version,
627 bool is_ip4, u8 protocol, u16 port,
628 char *client_pathname)
630 punt_main_t *pm = &punt_main;
632 if (!pm->is_configured)
633 return clib_error_return (0, "socket is not configured");
635 if (header_version != PUNT_PACKETDESC_VERSION)
636 return clib_error_return (0, "Invalid packet descriptor version");
638 /* For now we only support UDP punt */
639 if (protocol != IP_PROTOCOL_UDP)
640 return clib_error_return (0,
641 "only UDP protocol (%d) is supported, got %d",
642 IP_PROTOCOL_UDP, protocol);
644 if (port == (u16) ~ 0)
645 return clib_error_return (0, "UDP port number required");
647 /* Register client */
648 punt_socket_register (is_ip4, protocol, port, client_pathname);
650 u32 node_index = is_ip4 ? udp4_punt_socket_node.index :
651 udp6_punt_socket_node.index;
653 udp_register_dst_port (vm, port, node_index, is_ip4);
659 vnet_punt_socket_del (vlib_main_t * vm, bool is_ip4, u8 l4_protocol, u16 port)
661 punt_main_t *pm = &punt_main;
663 if (!pm->is_configured)
664 return clib_error_return (0, "socket is not configured");
666 punt_socket_unregister (is_ip4, l4_protocol, port);
667 udp_unregister_dst_port (vm, port, is_ip4);
673 * @brief Request IP traffic punt to the local TCP/IP stack.
676 * - UDP and TCP are the only protocols supported in the current implementation
678 * @param vm vlib_main_t corresponding to the current thread
679 * @param ipv IP protcol version.
680 * 4 - IPv4, 6 - IPv6, ~0 for both IPv6 and IPv4
681 * @param protocol 8-bits L4 protocol value
684 * @param port 16-bits L4 (TCP/IP) port number when applicable (UDP only)
686 * @returns 0 on success, non-zero value otherwise
689 vnet_punt_add_del (vlib_main_t * vm, u8 ipv, u8 protocol, u16 port,
693 /* For now we only support TCP, UDP and SCTP punt */
694 if (protocol != IP_PROTOCOL_UDP &&
695 protocol != IP_PROTOCOL_TCP && protocol != IP_PROTOCOL_SCTP)
696 return clib_error_return (0,
697 "only UDP (%d), TCP (%d) and SCTP (%d) protocols are supported, got %d",
698 IP_PROTOCOL_UDP, IP_PROTOCOL_TCP,
699 IP_PROTOCOL_SCTP, protocol);
701 if (ipv != (u8) ~ 0 && ipv != 4 && ipv != 6)
702 return clib_error_return (0, "IP version must be 4 or 6, got %d", ipv);
704 if (port == (u16) ~ 0)
706 if ((ipv == 4) || (ipv == (u8) ~ 0))
708 if (protocol == IP_PROTOCOL_UDP)
709 udp_punt_unknown (vm, 1, is_add);
710 else if (protocol == IP_PROTOCOL_TCP)
711 tcp_punt_unknown (vm, 1, is_add);
712 else if (protocol == IP_PROTOCOL_SCTP)
713 sctp_punt_unknown (vm, 1, is_add);
716 if ((ipv == 6) || (ipv == (u8) ~ 0))
718 if (protocol == IP_PROTOCOL_UDP)
719 udp_punt_unknown (vm, 0, is_add);
720 else if (protocol == IP_PROTOCOL_TCP)
721 tcp_punt_unknown (vm, 0, is_add);
722 else if (protocol == IP_PROTOCOL_SCTP)
723 sctp_punt_unknown (vm, 0, is_add);
731 if (protocol == IP_PROTOCOL_TCP || protocol == IP_PROTOCOL_SCTP)
732 return clib_error_return (0,
733 "punt TCP/SCTP ports is not supported yet");
735 if (ipv == 4 || ipv == (u8) ~ 0)
736 udp_register_dst_port (vm, port, udp4_punt_node.index, 1);
738 if (ipv == 6 || ipv == (u8) ~ 0)
739 udp_register_dst_port (vm, port, udp6_punt_node.index, 0);
744 return clib_error_return (0, "punt delete is not supported yet");
747 static clib_error_t *
748 punt_cli (vlib_main_t * vm,
749 unformat_input_t * input, vlib_cli_command_t * cmd)
754 clib_error_t *error = NULL;
756 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
758 if (unformat (input, "del"))
760 else if (unformat (input, "all"))
762 /* punt both IPv6 and IPv4 when used in CLI */
763 error = vnet_punt_add_del (vm, ~0, protocol, ~0, is_add);
766 clib_error_report (error);
770 else if (unformat (input, "%d", &port))
772 /* punt both IPv6 and IPv4 when used in CLI */
773 error = vnet_punt_add_del (vm, ~0, protocol, port, is_add);
776 clib_error_report (error);
780 else if (unformat (input, "udp"))
781 protocol = IP_PROTOCOL_UDP;
782 else if (unformat (input, "tcp"))
783 protocol = IP_PROTOCOL_TCP;
786 error = clib_error_return (0, "parse error: '%U'",
787 format_unformat_error, input);
796 * The set of '<em>set punt</em>' commands allows specific IP traffic to
797 * be punted to the host TCP/IP stack
800 * - UDP is the only protocol supported in the current implementation
801 * - All TCP traffic is currently punted to the host by default
805 * Example of how to request NTP traffic to be punted
806 * @cliexcmd{set punt udp 125}
808 * Example of how to request all 'unknown' UDP traffic to be punted
809 * @cliexcmd{set punt udp all}
811 * Example of how to stop all 'unknown' UDP traffic to be punted
812 * @cliexcmd{set punt udp del all}
816 VLIB_CLI_COMMAND (punt_command, static) = {
818 .short_help = "set punt [udp|tcp] [del] <all | port-num1 [port-num2 ...]>",
819 .function = punt_cli,
824 punt_init (vlib_main_t * vm)
826 punt_main_t *pm = &punt_main;
828 pm->clients_by_dst_port6 = sparse_vec_new
829 (sizeof (pm->clients_by_dst_port6[0]),
830 BITS (((udp_header_t *) 0)->dst_port));
831 pm->clients_by_dst_port4 = sparse_vec_new
832 (sizeof (pm->clients_by_dst_port4[0]),
833 BITS (((udp_header_t *) 0)->dst_port));
835 pm->is_configured = false;
836 pm->interface_output_node = vlib_get_node_by_name (vm,
842 VLIB_INIT_FUNCTION (punt_init);
844 static clib_error_t *
845 punt_config (vlib_main_t * vm, unformat_input_t * input)
847 punt_main_t *pm = &punt_main;
848 char *socket_path = 0;
850 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
852 if (unformat (input, "socket %s", &socket_path))
853 strncpy (pm->sun_path, socket_path, 108 - 1);
855 return clib_error_return (0, "unknown input `%U'",
856 format_unformat_error, input);
859 if (socket_path == 0)
862 /* UNIX domain socket */
863 struct sockaddr_un addr;
864 if ((pm->socket_fd = socket (AF_UNIX, SOCK_DGRAM | SOCK_NONBLOCK, 0)) == -1)
866 return clib_error_return (0, "socket error");
869 memset (&addr, 0, sizeof (addr));
870 addr.sun_family = AF_UNIX;
871 if (*socket_path == '\0')
873 *addr.sun_path = '\0';
874 strncpy (addr.sun_path + 1, socket_path + 1,
875 sizeof (addr.sun_path) - 2);
879 strncpy (addr.sun_path, socket_path, sizeof (addr.sun_path) - 1);
880 unlink (socket_path);
883 if (bind (pm->socket_fd, (struct sockaddr *) &addr, sizeof (addr)) == -1)
885 return clib_error_return (0, "bind error");
888 /* Register socket */
889 clib_file_main_t *fm = &file_main;
890 clib_file_t template = { 0 };
891 template.read_function = punt_socket_read_ready;
892 template.file_descriptor = pm->socket_fd;
893 template.description = format (0, "%s", socket_path);
894 pm->clib_file_index = clib_file_add (fm, &template);
896 pm->is_configured = true;
901 VLIB_CONFIG_FUNCTION (punt_config, "punt");
904 * fd.io coding-style-patch-verification: ON
907 * eval: (c-set-style "gnu")