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/udp/udp.h>
28 #include <vnet/tcp/tcp.h>
29 #include <vnet/ip/punt.h>
30 #include <vlib/unix/unix.h>
34 #include <sys/socket.h>
38 punt_main_t punt_main;
41 vnet_punt_get_server_pathname (void)
43 punt_main_t *pm = &punt_main;
48 punt_client_l4_db_add (ip_address_family_t af, u16 port, u32 index)
50 punt_main_t *pm = &punt_main;
52 pm->db.clients_by_l4_port = hash_set (pm->db.clients_by_l4_port,
53 punt_client_l4_mk_key (af, port),
58 punt_client_l4_db_remove (ip_address_family_t af, u16 port)
60 punt_main_t *pm = &punt_main;
64 key = punt_client_l4_mk_key (af, port);
65 p = hash_get (pm->db.clients_by_l4_port, key);
70 hash_unset (pm->db.clients_by_l4_port, key);
76 punt_client_ip_proto_db_add (ip_address_family_t af,
77 ip_protocol_t proto, u32 index)
79 punt_main_t *pm = &punt_main;
81 pm->db.clients_by_ip_proto = hash_set (pm->db.clients_by_ip_proto,
82 punt_client_ip_proto_mk_key (af,
88 punt_client_ip_proto_db_remove (ip_address_family_t af, ip_protocol_t proto)
90 punt_main_t *pm = &punt_main;
94 key = punt_client_ip_proto_mk_key (af, proto);
95 p = hash_get (pm->db.clients_by_ip_proto, key);
100 hash_unset (pm->db.clients_by_ip_proto, key);
106 punt_client_exception_db_add (vlib_punt_reason_t reason, u32 pci)
108 punt_main_t *pm = &punt_main;
110 vec_validate_init_empty (pm->db.clients_by_exception, reason, ~0);
112 pm->db.clients_by_exception[reason] = pci;
116 punt_client_exception_db_remove (vlib_punt_reason_t reason)
118 punt_main_t *pm = &punt_main;
121 if (punt_client_exception_get (reason))
123 pci = pm->db.clients_by_exception[reason];
124 pm->db.clients_by_exception[reason] = ~0;
130 static clib_error_t *
131 punt_socket_read_ready (clib_file_t * uf)
133 vlib_main_t *vm = vlib_get_main ();
134 punt_main_t *pm = &punt_main;
136 /** Schedule the rx node */
137 vlib_node_set_interrupt_pending (vm, punt_socket_rx_node.index);
138 vec_add1 (pm->ready_fds, uf->file_descriptor);
143 static clib_error_t *
144 punt_socket_register_l4 (vlib_main_t * vm,
145 ip_address_family_t af,
146 u8 protocol, u16 port, char *client_pathname)
148 punt_main_t *pm = &punt_main;
151 /* For now we only support UDP punt */
152 if (protocol != IP_PROTOCOL_UDP)
153 return clib_error_return (0,
154 "only UDP protocol (%d) is supported, got %d",
155 IP_PROTOCOL_UDP, protocol);
157 if (port == (u16) ~ 0)
158 return clib_error_return (0, "UDP port number required");
160 c = punt_client_l4_get (af, port);
164 pool_get_zero (pm->punt_client_pool, c);
165 punt_client_l4_db_add (af, port, c - pm->punt_client_pool);
168 memcpy (c->caddr.sun_path, client_pathname, sizeof (c->caddr.sun_path));
169 c->caddr.sun_family = AF_UNIX;
170 c->reg.type = PUNT_TYPE_L4;
171 c->reg.punt.l4.port = port;
172 c->reg.punt.l4.protocol = protocol;
173 c->reg.punt.l4.af = af;
175 u32 node_index = (af == AF_IP4 ?
176 udp4_punt_socket_node.index :
177 udp6_punt_socket_node.index);
179 udp_register_dst_port (vm, port, node_index, af == AF_IP4);
184 static clib_error_t *
185 punt_socket_register_ip_proto (vlib_main_t * vm,
186 ip_address_family_t af,
187 ip_protocol_t proto, char *client_pathname)
189 punt_main_t *pm = &punt_main;
192 c = punt_client_ip_proto_get (af, proto);
196 pool_get_zero (pm->punt_client_pool, c);
197 punt_client_ip_proto_db_add (af, proto, c - pm->punt_client_pool);
200 memcpy (c->caddr.sun_path, client_pathname, sizeof (c->caddr.sun_path));
201 c->caddr.sun_family = AF_UNIX;
202 c->reg.type = PUNT_TYPE_IP_PROTO;
203 c->reg.punt.ip_proto.protocol = proto;
204 c->reg.punt.ip_proto.af = af;
207 ip4_register_protocol (proto, ip4_proto_punt_socket_node.index);
209 ip6_register_protocol (proto, ip6_proto_punt_socket_node.index);
214 static clib_error_t *
215 punt_socket_register_exception (vlib_main_t * vm,
216 vlib_punt_reason_t reason,
217 char *client_pathname)
219 punt_main_t *pm = &punt_main;
222 pc = punt_client_exception_get (reason);
226 pool_get_zero (pm->punt_client_pool, pc);
227 punt_client_exception_db_add (reason, pc - pm->punt_client_pool);
230 memcpy (pc->caddr.sun_path, client_pathname, sizeof (pc->caddr.sun_path));
231 pc->caddr.sun_family = AF_UNIX;
232 pc->reg.type = PUNT_TYPE_EXCEPTION;
233 pc->reg.punt.exception.reason = reason;
235 vlib_punt_register (pm->hdl,
236 pc->reg.punt.exception.reason, "exception-punt-socket");
241 static clib_error_t *
242 punt_socket_unregister_l4 (ip_address_family_t af,
243 ip_protocol_t protocol, u16 port)
247 udp_unregister_dst_port (vlib_get_main (), port, af == AF_IP4);
249 pci = punt_client_l4_db_remove (af, port);
252 pool_put_index (punt_main.punt_client_pool, pci);
257 static clib_error_t *
258 punt_socket_unregister_ip_proto (ip_address_family_t af, ip_protocol_t proto)
263 ip4_unregister_protocol (proto);
265 ip6_unregister_protocol (proto);
267 pci = punt_client_ip_proto_db_remove (af, proto);
270 pool_put_index (punt_main.punt_client_pool, pci);
275 static clib_error_t *
276 punt_socket_unregister_exception (vlib_punt_reason_t reason)
280 pci = punt_client_exception_db_remove (reason);
283 pool_put_index (punt_main.punt_client_pool, pci);
289 vnet_punt_socket_add (vlib_main_t * vm, u32 header_version,
290 const punt_reg_t * pr, char *client_pathname)
292 punt_main_t *pm = &punt_main;
294 if (!pm->is_configured)
295 return clib_error_return (0, "socket is not configured");
297 if (header_version != PUNT_PACKETDESC_VERSION)
298 return clib_error_return (0, "Invalid packet descriptor version");
300 if (strncmp (client_pathname, vnet_punt_get_server_pathname (),
302 return clib_error_return (0,
303 "Punt socket: Invalid client path: %s",
306 /* Register client */
310 return (punt_socket_register_l4 (vm,
312 pr->punt.l4.protocol,
313 pr->punt.l4.port, client_pathname));
314 case PUNT_TYPE_IP_PROTO:
315 return (punt_socket_register_ip_proto (vm,
316 pr->punt.ip_proto.af,
317 pr->punt.ip_proto.protocol,
319 case PUNT_TYPE_EXCEPTION:
320 return (punt_socket_register_exception (vm,
321 pr->punt.exception.reason,
329 vnet_punt_socket_del (vlib_main_t * vm, const punt_reg_t * pr)
331 punt_main_t *pm = &punt_main;
333 if (!pm->is_configured)
334 return clib_error_return (0, "socket is not configured");
339 return (punt_socket_unregister_l4 (pr->punt.l4.af,
340 pr->punt.l4.protocol,
342 case PUNT_TYPE_IP_PROTO:
343 return (punt_socket_unregister_ip_proto (pr->punt.ip_proto.af,
344 pr->punt.ip_proto.protocol));
345 case PUNT_TYPE_EXCEPTION:
346 return (punt_socket_unregister_exception (pr->punt.exception.reason));
353 * @brief Request IP L4 traffic punt to the local TCP/IP stack.
356 * - UDP is the only protocol supported in the current implementation
358 * @param vm vlib_main_t corresponding to the current thread
359 * @param af IP address family.
360 * @param protocol 8-bits L4 protocol value
363 * @param port 16-bits L4 (TCP/IP) port number when applicable (UDP only)
365 * @returns 0 on success, non-zero value otherwise
367 static clib_error_t *
368 punt_l4_add_del (vlib_main_t * vm,
369 ip_address_family_t af,
370 ip_protocol_t protocol, u16 port, bool is_add)
372 /* For now we only support TCP and UDP punt */
373 if (protocol != IP_PROTOCOL_UDP && protocol != IP_PROTOCOL_TCP)
374 return clib_error_return (0,
375 "only UDP (%d) and TCP (%d) protocols are supported, got %d",
376 IP_PROTOCOL_UDP, IP_PROTOCOL_TCP, protocol);
378 if (port == (u16) ~ 0)
380 if (protocol == IP_PROTOCOL_UDP)
381 udp_punt_unknown (vm, af == AF_IP4, is_add);
382 else if (protocol == IP_PROTOCOL_TCP)
383 tcp_punt_unknown (vm, af == AF_IP4, is_add);
390 if (protocol == IP_PROTOCOL_TCP)
391 return clib_error_return (0, "punt TCP ports is not supported yet");
393 udp_register_dst_port (vm, port, udp4_punt_node.index, af == AF_IP4);
399 if (protocol == IP_PROTOCOL_TCP)
400 return clib_error_return (0, "punt TCP ports is not supported yet");
402 udp_unregister_dst_port (vm, port, af == AF_IP4);
409 * @brief Request exception traffic punt.
411 * @param reason Punting reason
413 * @returns 0 on success, non-zero value otherwise
415 static clib_error_t *
416 punt_exception_add_del (vlib_punt_reason_t reason, bool is_add)
418 punt_main_t *pm = &punt_main;
420 vnet_punt_reason_flag_t flag = vlib_punt_reason_get_flags (reason);
421 const char *node_name =
422 vnet_punt_reason_flag_is_IP6_PACKET (flag) ? "ip6-punt" : "ip4-punt";
424 rv = vlib_punt_register (pm->hdl, reason, node_name);
426 rv = vlib_punt_unregister (pm->hdl, reason, node_name);
430 return clib_error_return (0, is_add ? "Existing punting registration..." :
431 "Punting registration not found...");
435 vnet_punt_add_del (vlib_main_t * vm, const punt_reg_t * pr, bool is_add)
440 return (punt_l4_add_del (vm, pr->punt.l4.af, pr->punt.l4.protocol,
441 pr->punt.l4.port, is_add));
442 case PUNT_TYPE_EXCEPTION:
443 return punt_exception_add_del (pr->punt.exception.reason, is_add);
444 case PUNT_TYPE_IP_PROTO:
448 return (clib_error_return (0, "Unsupported punt type: %d", pr->type));
451 static clib_error_t *
452 punt_cli (vlib_main_t * vm,
453 unformat_input_t * input__, vlib_cli_command_t * cmd)
455 unformat_input_t line_input, *input = &line_input;
456 clib_error_t *error = NULL;
464 .protocol = IP_PROTOCOL_UDP,
467 .type = PUNT_TYPE_L4,
472 if (!unformat_user (input__, unformat_line_input, input))
475 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
477 if (unformat (input, "del"))
479 else if (unformat (input, "reason %U", unformat_punt_reason,
480 &pr.punt.exception.reason))
481 pr.type = PUNT_TYPE_EXCEPTION;
482 else if (unformat (input, "ipv4"))
483 pr.punt.l4.af = AF_IP4;
484 else if (unformat (input, "ipv6"))
485 pr.punt.l4.af = AF_IP6;
486 else if (unformat (input, "ip6"))
487 pr.punt.l4.af = AF_IP6;
488 else if (unformat (input, "%d", &port))
489 pr.punt.l4.port = port;
490 else if (unformat (input, "all"))
491 pr.punt.l4.port = ~0;
492 else if (unformat (input, "udp"))
493 pr.punt.l4.protocol = IP_PROTOCOL_UDP;
494 else if (unformat (input, "tcp"))
495 pr.punt.l4.protocol = IP_PROTOCOL_TCP;
498 error = clib_error_return (0, "parse error: '%U'",
499 format_unformat_error, input);
504 /* punt both IPv6 and IPv4 when used in CLI */
505 error = vnet_punt_add_del (vm, &pr, is_add);
508 clib_error_report (error);
512 unformat_free (input);
517 * The set of '<em>set punt</em>' commands allows specific IP traffic to
518 * be punted to the host TCP/IP stack
521 * - UDP is the only protocol supported in the current implementation
522 * - All TCP traffic is currently punted to the host by default
526 * Example of how to request NTP traffic to be punted
527 * @cliexcmd{set punt udp 125}
529 * Example of how to request all 'unknown' UDP traffic to be punted
530 * @cliexcmd{set punt udp all}
532 * Example of how to stop all 'unknown' UDP traffic to be punted
533 * @cliexcmd{set punt udp del all}
537 VLIB_CLI_COMMAND (punt_command, static) = {
539 .short_help = "set punt [IPV4|ip6|ipv6] [UDP|tcp] [del] [ALL|<port-num>]",
540 .function = punt_cli,
544 static clib_error_t *
545 punt_socket_register_cmd (vlib_main_t * vm,
546 unformat_input_t * input__,
547 vlib_cli_command_t * cmd)
549 unformat_input_t line_input, *input = &line_input;
551 clib_error_t *error = NULL;
558 .protocol = IP_PROTOCOL_UDP,
561 .type = PUNT_TYPE_L4,
565 if (!unformat_user (input__, unformat_line_input, input))
568 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
570 if (unformat (input, "ipv4"))
571 pr.punt.l4.af = AF_IP4;
572 else if (unformat (input, "ipv6"))
573 pr.punt.l4.af = AF_IP6;
574 else if (unformat (input, "udp"))
575 pr.punt.l4.protocol = IP_PROTOCOL_UDP;
576 else if (unformat (input, "tcp"))
577 pr.punt.l4.protocol = IP_PROTOCOL_TCP;
578 else if (unformat (input, "%d", &pr.punt.l4.port))
580 else if (unformat (input, "all"))
581 pr.punt.l4.port = ~0;
582 else if (unformat (input, "socket %s", &socket_name))
586 error = clib_error_return (0, "parse error: '%U'",
587 format_unformat_error, input);
593 error = clib_error_return (0, "socket name not specified");
595 error = vnet_punt_socket_add (vm, 1, &pr, (char *) socket_name);
598 unformat_free (input);
605 * @cliexcmd{punt socket register socket punt_l4_foo.sock}
609 VLIB_CLI_COMMAND (punt_socket_register_command, static) =
611 .path = "punt socket register",
612 .function = punt_socket_register_cmd,
613 .short_help = "punt socket register [IPV4|ipv6] [UDP|tcp] [ALL|<port-num>] socket <socket>",
618 static clib_error_t *
619 punt_socket_deregister_cmd (vlib_main_t * vm,
620 unformat_input_t * input__,
621 vlib_cli_command_t * cmd)
623 unformat_input_t line_input, *input = &line_input;
624 clib_error_t *error = NULL;
631 .protocol = IP_PROTOCOL_UDP,
634 .type = PUNT_TYPE_L4,
638 if (!unformat_user (input__, unformat_line_input, input))
641 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
643 if (unformat (input, "ipv4"))
644 pr.punt.l4.af = AF_IP4;
645 else if (unformat (input, "ipv6"))
646 pr.punt.l4.af = AF_IP6;
647 else if (unformat (input, "udp"))
648 pr.punt.l4.protocol = IP_PROTOCOL_UDP;
649 else if (unformat (input, "tcp"))
650 pr.punt.l4.protocol = IP_PROTOCOL_TCP;
651 else if (unformat (input, "%d", &pr.punt.l4.port))
653 else if (unformat (input, "all"))
654 pr.punt.l4.port = ~0;
657 error = clib_error_return (0, "parse error: '%U'",
658 format_unformat_error, input);
663 error = vnet_punt_socket_del (vm, &pr);
665 unformat_free (input);
672 * @cliexcmd{punt socket register}
675 VLIB_CLI_COMMAND (punt_socket_deregister_command, static) =
677 .path = "punt socket deregister",
678 .function = punt_socket_deregister_cmd,
679 .short_help = "punt socket deregister [IPV4|ipv6] [UDP|tcp] [ALL|<port-num>]",
685 punt_client_walk (punt_type_t pt, punt_client_walk_cb_t cb, void *ctx)
687 punt_main_t *pm = &punt_main;
696 hash_foreach(key, pci, pm->db.clients_by_l4_port,
698 cb (pool_elt_at_index(pm->punt_client_pool, pci), ctx);
703 case PUNT_TYPE_IP_PROTO:
708 hash_foreach(key, pci, pm->db.clients_by_ip_proto,
710 cb (pool_elt_at_index(pm->punt_client_pool, pci), ctx);
715 case PUNT_TYPE_EXCEPTION:
719 vec_foreach (pci, pm->db.clients_by_exception)
722 cb (pool_elt_at_index (pm->punt_client_pool, *pci), ctx);
731 format_punt_client (u8 * s, va_list * args)
733 punt_client_t *pc = va_arg (*args, punt_client_t *);
735 s = format (s, " punt ");
737 switch (pc->reg.type)
740 s = format (s, "%U %U port %d",
741 format_ip_address_family, pc->reg.punt.l4.af,
742 format_ip_protocol, pc->reg.punt.l4.protocol,
743 pc->reg.punt.l4.port);
745 case PUNT_TYPE_IP_PROTO:
746 s = format (s, "%U %U",
747 format_ip_address_family, pc->reg.punt.ip_proto.af,
748 format_ip_protocol, pc->reg.punt.ip_proto.protocol);
750 case PUNT_TYPE_EXCEPTION:
751 s = format (s, " %U", format_vlib_punt_reason,
752 pc->reg.punt.exception.reason);
756 s = format (s, " to socket %s \n", pc->caddr.sun_path);
762 punt_client_show_one (const punt_client_t * pc, void *ctx)
764 vlib_cli_output (ctx, "%U", format_punt_client, pc);
766 return (WALK_CONTINUE);
769 static clib_error_t *
770 punt_socket_show_cmd (vlib_main_t * vm,
771 unformat_input_t * input__, vlib_cli_command_t * cmd)
773 unformat_input_t line_input, *input = &line_input;
774 clib_error_t *error = NULL;
779 if (!unformat_user (input__, unformat_line_input, input))
782 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
784 if (unformat (input, "exception"))
785 pt = PUNT_TYPE_EXCEPTION;
786 else if (unformat (input, "l4"))
788 else if (unformat (input, "ip"))
789 pt = PUNT_TYPE_IP_PROTO;
792 error = clib_error_return (0, "parse error: '%U'",
793 format_unformat_error, input);
798 punt_client_walk (pt, punt_client_show_one, vm);
801 unformat_free (input);
808 * @cliexcmd{show punt socket ipv4}
811 VLIB_CLI_COMMAND (show_punt_socket_registration_command, static) =
813 .path = "show punt socket registrations",
814 .function = punt_socket_show_cmd,
815 .short_help = "show punt socket registrations [l4|exception]",
821 ip_punt_init (vlib_main_t * vm)
823 clib_error_t *error = NULL;
824 punt_main_t *pm = &punt_main;
825 vlib_thread_main_t *tm = vlib_get_thread_main ();
827 pm->is_configured = false;
828 pm->interface_output_node =
829 vlib_get_node_by_name (vm, (u8 *) "interface-output");
831 if ((error = vlib_call_init_function (vm, punt_init)))
834 pm->hdl = vlib_punt_client_register ("ip-punt");
836 vec_validate_aligned (pm->thread_data, tm->n_vlib_mains,
837 CLIB_CACHE_LINE_BYTES);
843 format_vnet_punt_reason_flags (u8 *s, va_list *args)
845 vnet_punt_reason_flag_t flag = va_arg (*args, int);
846 #define _(pos, len, value, name, str) \
847 if (vnet_punt_reason_flag_is_##name (flag)) \
848 s = format (s, "%s ", str);
850 foreach_vnet_punt_reason_flag
855 VLIB_INIT_FUNCTION (ip_punt_init);
857 static clib_error_t *
858 punt_config (vlib_main_t * vm, unformat_input_t * input)
860 punt_main_t *pm = &punt_main;
861 char *socket_path = 0;
863 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
865 if (unformat (input, "socket %s", &socket_path))
866 strncpy (pm->sun_path, socket_path, UNIX_PATH_MAX - 1);
868 return clib_error_return (0, "unknown input `%U'",
869 format_unformat_error, input);
872 if (socket_path == 0)
875 /* UNIX domain socket */
876 struct sockaddr_un addr;
877 if ((pm->socket_fd = socket (AF_UNIX, SOCK_DGRAM | SOCK_NONBLOCK, 0)) == -1)
879 return clib_error_return (0, "socket error");
882 clib_memset (&addr, 0, sizeof (addr));
883 addr.sun_family = AF_UNIX;
884 if (*socket_path == '\0')
886 *addr.sun_path = '\0';
887 strncpy (addr.sun_path + 1, socket_path + 1,
888 sizeof (addr.sun_path) - 2);
892 strncpy (addr.sun_path, socket_path, sizeof (addr.sun_path) - 1);
893 unlink (socket_path);
896 if (bind (pm->socket_fd, (struct sockaddr *) &addr, sizeof (addr)) == -1)
898 return clib_error_return (0, "bind error");
901 int n_bytes = 0x10000;
904 (pm->socket_fd, SOL_SOCKET, SO_SNDBUF, &n_bytes,
905 sizeof (n_bytes)) == -1)
907 return clib_error_return (0, "setsockopt error");
910 /* Register socket */
911 clib_file_main_t *fm = &file_main;
912 clib_file_t template = { 0 };
913 template.read_function = punt_socket_read_ready;
914 template.file_descriptor = pm->socket_fd;
915 template.description = format (0, "punt socket %s", socket_path);
916 pm->clib_file_index = clib_file_add (fm, &template);
918 pm->is_configured = true;
923 VLIB_CONFIG_FUNCTION (punt_config, "punt");
926 * fd.io coding-style-patch-verification: ON
929 * eval: (c-set-style "gnu")