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 snprintf (c->caddr.sun_path, sizeof (c->caddr.sun_path), "%s",
170 c->caddr.sun_family = AF_UNIX;
171 c->reg.type = PUNT_TYPE_L4;
172 c->reg.punt.l4.port = port;
173 c->reg.punt.l4.protocol = protocol;
174 c->reg.punt.l4.af = af;
176 u32 node_index = (af == AF_IP4 ?
177 udp4_punt_socket_node.index :
178 udp6_punt_socket_node.index);
180 udp_register_dst_port (vm, port, node_index, af == AF_IP4);
185 static clib_error_t *
186 punt_socket_register_ip_proto (vlib_main_t * vm,
187 ip_address_family_t af,
188 ip_protocol_t proto, char *client_pathname)
190 punt_main_t *pm = &punt_main;
193 c = punt_client_ip_proto_get (af, proto);
197 pool_get_zero (pm->punt_client_pool, c);
198 punt_client_ip_proto_db_add (af, proto, c - pm->punt_client_pool);
201 snprintf (c->caddr.sun_path, sizeof (c->caddr.sun_path), "%s",
203 c->caddr.sun_family = AF_UNIX;
204 c->reg.type = PUNT_TYPE_IP_PROTO;
205 c->reg.punt.ip_proto.protocol = proto;
206 c->reg.punt.ip_proto.af = af;
209 ip4_register_protocol (proto, ip4_proto_punt_socket_node.index);
211 ip6_register_protocol (proto, ip6_proto_punt_socket_node.index);
216 static clib_error_t *
217 punt_socket_register_exception (vlib_main_t * vm,
218 vlib_punt_reason_t reason,
219 char *client_pathname)
221 punt_main_t *pm = &punt_main;
224 pc = punt_client_exception_get (reason);
228 pool_get_zero (pm->punt_client_pool, pc);
229 punt_client_exception_db_add (reason, pc - pm->punt_client_pool);
232 snprintf (pc->caddr.sun_path, sizeof (pc->caddr.sun_path), "%s",
234 pc->caddr.sun_family = AF_UNIX;
235 pc->reg.type = PUNT_TYPE_EXCEPTION;
236 pc->reg.punt.exception.reason = reason;
238 vlib_punt_register (pm->hdl,
239 pc->reg.punt.exception.reason, "exception-punt-socket");
244 static clib_error_t *
245 punt_socket_unregister_l4 (ip_address_family_t af,
246 ip_protocol_t protocol, u16 port)
250 udp_unregister_dst_port (vlib_get_main (), port, af == AF_IP4);
252 pci = punt_client_l4_db_remove (af, port);
255 pool_put_index (punt_main.punt_client_pool, pci);
260 static clib_error_t *
261 punt_socket_unregister_ip_proto (ip_address_family_t af, ip_protocol_t proto)
266 ip4_unregister_protocol (proto);
268 ip6_unregister_protocol (proto);
270 pci = punt_client_ip_proto_db_remove (af, proto);
273 pool_put_index (punt_main.punt_client_pool, pci);
278 static clib_error_t *
279 punt_socket_unregister_exception (vlib_punt_reason_t reason)
283 pci = punt_client_exception_db_remove (reason);
286 pool_put_index (punt_main.punt_client_pool, pci);
292 vnet_punt_socket_add (vlib_main_t * vm, u32 header_version,
293 const punt_reg_t * pr, char *client_pathname)
295 punt_main_t *pm = &punt_main;
297 if (!pm->is_configured)
298 return clib_error_return (0, "socket is not configured");
300 if (header_version != PUNT_PACKETDESC_VERSION)
301 return clib_error_return (0, "Invalid packet descriptor version");
303 if (strncmp (client_pathname, vnet_punt_get_server_pathname (),
305 return clib_error_return (0,
306 "Punt socket: Invalid client path: %s",
309 /* Register client */
313 return (punt_socket_register_l4 (vm,
315 pr->punt.l4.protocol,
316 pr->punt.l4.port, client_pathname));
317 case PUNT_TYPE_IP_PROTO:
318 return (punt_socket_register_ip_proto (vm,
319 pr->punt.ip_proto.af,
320 pr->punt.ip_proto.protocol,
322 case PUNT_TYPE_EXCEPTION:
323 return (punt_socket_register_exception (vm,
324 pr->punt.exception.reason,
332 vnet_punt_socket_del (vlib_main_t * vm, const punt_reg_t * pr)
334 punt_main_t *pm = &punt_main;
336 if (!pm->is_configured)
337 return clib_error_return (0, "socket is not configured");
342 return (punt_socket_unregister_l4 (pr->punt.l4.af,
343 pr->punt.l4.protocol,
345 case PUNT_TYPE_IP_PROTO:
346 return (punt_socket_unregister_ip_proto (pr->punt.ip_proto.af,
347 pr->punt.ip_proto.protocol));
348 case PUNT_TYPE_EXCEPTION:
349 return (punt_socket_unregister_exception (pr->punt.exception.reason));
356 * @brief Request IP L4 traffic punt to the local TCP/IP stack.
359 * - UDP is the only protocol supported in the current implementation
361 * @param vm vlib_main_t corresponding to the current thread
362 * @param af IP address family.
363 * @param protocol 8-bits L4 protocol value
366 * @param port 16-bits L4 (TCP/IP) port number when applicable (UDP only)
368 * @returns 0 on success, non-zero value otherwise
370 static clib_error_t *
371 punt_l4_add_del (vlib_main_t * vm,
372 ip_address_family_t af,
373 ip_protocol_t protocol, u16 port, bool is_add)
375 int is_ip4 = af == AF_IP4;
377 /* For now we only support TCP and UDP punt */
378 if (protocol != IP_PROTOCOL_UDP && protocol != IP_PROTOCOL_TCP)
379 return clib_error_return (0,
380 "only UDP (%d) and TCP (%d) protocols are supported, got %d",
381 IP_PROTOCOL_UDP, IP_PROTOCOL_TCP, protocol);
383 if (port == (u16) ~ 0)
385 if (protocol == IP_PROTOCOL_UDP)
386 udp_punt_unknown (vm, is_ip4, is_add);
387 else if (protocol == IP_PROTOCOL_TCP)
388 tcp_punt_unknown (vm, is_ip4, is_add);
395 const vlib_node_registration_t *punt_node =
396 is_ip4 ? &udp4_punt_node : &udp6_punt_node;
398 if (protocol == IP_PROTOCOL_TCP)
399 return clib_error_return (0, "punt TCP ports is not supported yet");
401 udp_register_dst_port (vm, port, punt_node->index, is_ip4);
407 if (protocol == IP_PROTOCOL_TCP)
408 return clib_error_return (0, "punt TCP ports is not supported yet");
410 udp_unregister_dst_port (vm, port, is_ip4);
417 * @brief Request exception traffic punt.
419 * @param reason Punting reason
421 * @returns 0 on success, non-zero value otherwise
423 static clib_error_t *
424 punt_exception_add_del (vlib_punt_reason_t reason, bool is_add)
426 punt_main_t *pm = &punt_main;
428 vnet_punt_reason_flag_t flag = vlib_punt_reason_get_flags (reason);
429 const char *node_name =
430 vnet_punt_reason_flag_is_IP6_PACKET (flag) ? "ip6-punt" : "ip4-punt";
432 rv = vlib_punt_register (pm->hdl, reason, node_name);
434 rv = vlib_punt_unregister (pm->hdl, reason, node_name);
438 return clib_error_return (0, is_add ? "Existing punting registration..." :
439 "Punting registration not found...");
443 vnet_punt_add_del (vlib_main_t * vm, const punt_reg_t * pr, bool is_add)
448 return (punt_l4_add_del (vm, pr->punt.l4.af, pr->punt.l4.protocol,
449 pr->punt.l4.port, is_add));
450 case PUNT_TYPE_EXCEPTION:
451 return punt_exception_add_del (pr->punt.exception.reason, is_add);
452 case PUNT_TYPE_IP_PROTO:
456 return (clib_error_return (0, "Unsupported punt type: %d", pr->type));
459 static clib_error_t *
460 punt_cli (vlib_main_t * vm,
461 unformat_input_t * input__, vlib_cli_command_t * cmd)
463 unformat_input_t line_input, *input = &line_input;
464 clib_error_t *error = NULL;
472 .protocol = IP_PROTOCOL_UDP,
475 .type = PUNT_TYPE_L4,
480 if (!unformat_user (input__, unformat_line_input, input))
483 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
485 if (unformat (input, "del"))
487 else if (unformat (input, "reason %U", unformat_punt_reason,
488 &pr.punt.exception.reason))
489 pr.type = PUNT_TYPE_EXCEPTION;
490 else if (unformat (input, "ipv4"))
491 pr.punt.l4.af = AF_IP4;
492 else if (unformat (input, "ipv6"))
493 pr.punt.l4.af = AF_IP6;
494 else if (unformat (input, "ip6"))
495 pr.punt.l4.af = AF_IP6;
496 else if (unformat (input, "%d", &port))
497 pr.punt.l4.port = port;
498 else if (unformat (input, "all"))
499 pr.punt.l4.port = ~0;
500 else if (unformat (input, "udp"))
501 pr.punt.l4.protocol = IP_PROTOCOL_UDP;
502 else if (unformat (input, "tcp"))
503 pr.punt.l4.protocol = IP_PROTOCOL_TCP;
506 error = clib_error_return (0, "parse error: '%U'",
507 format_unformat_error, input);
512 /* punt both IPv6 and IPv4 when used in CLI */
513 error = vnet_punt_add_del (vm, &pr, is_add);
516 clib_error_report (error);
520 unformat_free (input);
525 * The set of '<em>set punt</em>' commands allows specific IP traffic to
526 * be punted to the host TCP/IP stack
529 * - UDP is the only protocol supported in the current implementation
530 * - All TCP traffic is currently punted to the host by default
534 * Example of how to request NTP traffic to be punted
535 * @cliexcmd{set punt udp 125}
537 * Example of how to request all 'unknown' UDP traffic to be punted
538 * @cliexcmd{set punt udp all}
540 * Example of how to stop all 'unknown' UDP traffic to be punted
541 * @cliexcmd{set punt udp del all}
545 VLIB_CLI_COMMAND (punt_command, static) = {
547 .short_help = "set punt [IPV4|ip6|ipv6] [UDP|tcp] [del] [ALL|<port-num>]",
548 .function = punt_cli,
552 static clib_error_t *
553 punt_socket_register_cmd (vlib_main_t * vm,
554 unformat_input_t * input__,
555 vlib_cli_command_t * cmd)
557 unformat_input_t line_input, *input = &line_input;
559 clib_error_t *error = NULL;
566 .protocol = IP_PROTOCOL_UDP,
569 .type = PUNT_TYPE_L4,
573 if (!unformat_user (input__, unformat_line_input, input))
576 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
578 if (unformat (input, "ipv4"))
579 pr.punt.l4.af = AF_IP4;
580 else if (unformat (input, "ipv6"))
581 pr.punt.l4.af = AF_IP6;
582 else if (unformat (input, "udp"))
583 pr.punt.l4.protocol = IP_PROTOCOL_UDP;
584 else if (unformat (input, "tcp"))
585 pr.punt.l4.protocol = IP_PROTOCOL_TCP;
586 else if (unformat (input, "%d", &pr.punt.l4.port))
588 else if (unformat (input, "all"))
589 pr.punt.l4.port = ~0;
590 else if (unformat (input, "socket %s", &socket_name))
592 else if (unformat (input, "reason %U", unformat_punt_reason,
593 &pr.punt.exception.reason))
594 pr.type = PUNT_TYPE_EXCEPTION;
597 error = clib_error_return (0, "parse error: '%U'",
598 format_unformat_error, input);
604 error = clib_error_return (0, "socket name not specified");
606 error = vnet_punt_socket_add (vm, 1, &pr, (char *) socket_name);
609 unformat_free (input);
616 * @cliexcmd{punt socket register socket punt_l4_foo.sock}
620 VLIB_CLI_COMMAND (punt_socket_register_command, static) =
622 .path = "punt socket register",
623 .function = punt_socket_register_cmd,
624 .short_help = "punt socket register [IPV4|ipv6] [UDP|tcp] [ALL|<port-num>] socket <socket>",
629 static clib_error_t *
630 punt_socket_deregister_cmd (vlib_main_t * vm,
631 unformat_input_t * input__,
632 vlib_cli_command_t * cmd)
634 unformat_input_t line_input, *input = &line_input;
635 clib_error_t *error = NULL;
642 .protocol = IP_PROTOCOL_UDP,
645 .type = PUNT_TYPE_L4,
649 if (!unformat_user (input__, unformat_line_input, input))
652 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
654 if (unformat (input, "ipv4"))
655 pr.punt.l4.af = AF_IP4;
656 else if (unformat (input, "ipv6"))
657 pr.punt.l4.af = AF_IP6;
658 else if (unformat (input, "udp"))
659 pr.punt.l4.protocol = IP_PROTOCOL_UDP;
660 else if (unformat (input, "tcp"))
661 pr.punt.l4.protocol = IP_PROTOCOL_TCP;
662 else if (unformat (input, "%d", &pr.punt.l4.port))
664 else if (unformat (input, "all"))
665 pr.punt.l4.port = ~0;
666 else if (unformat (input, "reason %U", unformat_punt_reason,
667 &pr.punt.exception.reason))
668 pr.type = PUNT_TYPE_EXCEPTION;
671 error = clib_error_return (0, "parse error: '%U'",
672 format_unformat_error, input);
677 error = vnet_punt_socket_del (vm, &pr);
679 unformat_free (input);
686 * @cliexcmd{punt socket register}
689 VLIB_CLI_COMMAND (punt_socket_deregister_command, static) =
691 .path = "punt socket deregister",
692 .function = punt_socket_deregister_cmd,
693 .short_help = "punt socket deregister [IPV4|ipv6] [UDP|tcp] [ALL|<port-num>]",
699 punt_client_walk (punt_type_t pt, punt_client_walk_cb_t cb, void *ctx)
701 punt_main_t *pm = &punt_main;
710 hash_foreach(key, pci, pm->db.clients_by_l4_port,
712 cb (pool_elt_at_index(pm->punt_client_pool, pci), ctx);
717 case PUNT_TYPE_IP_PROTO:
722 hash_foreach(key, pci, pm->db.clients_by_ip_proto,
724 cb (pool_elt_at_index(pm->punt_client_pool, pci), ctx);
729 case PUNT_TYPE_EXCEPTION:
733 vec_foreach (pci, pm->db.clients_by_exception)
736 cb (pool_elt_at_index (pm->punt_client_pool, *pci), ctx);
745 format_punt_client (u8 * s, va_list * args)
747 punt_client_t *pc = va_arg (*args, punt_client_t *);
749 s = format (s, " punt ");
751 switch (pc->reg.type)
754 s = format (s, "%U %U port %d",
755 format_ip_address_family, pc->reg.punt.l4.af,
756 format_ip_protocol, pc->reg.punt.l4.protocol,
757 pc->reg.punt.l4.port);
759 case PUNT_TYPE_IP_PROTO:
760 s = format (s, "%U %U",
761 format_ip_address_family, pc->reg.punt.ip_proto.af,
762 format_ip_protocol, pc->reg.punt.ip_proto.protocol);
764 case PUNT_TYPE_EXCEPTION:
765 s = format (s, " %U", format_vlib_punt_reason,
766 pc->reg.punt.exception.reason);
770 s = format (s, " to socket %s \n", pc->caddr.sun_path);
776 punt_client_show_one (const punt_client_t * pc, void *ctx)
778 vlib_cli_output (ctx, "%U", format_punt_client, pc);
780 return (WALK_CONTINUE);
783 static clib_error_t *
784 punt_socket_show_cmd (vlib_main_t * vm,
785 unformat_input_t * input__, vlib_cli_command_t * cmd)
787 unformat_input_t line_input, *input = &line_input;
788 clib_error_t *error = NULL;
793 if (!unformat_user (input__, unformat_line_input, input))
796 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
798 if (unformat (input, "exception"))
799 pt = PUNT_TYPE_EXCEPTION;
800 else if (unformat (input, "l4"))
802 else if (unformat (input, "ip"))
803 pt = PUNT_TYPE_IP_PROTO;
806 error = clib_error_return (0, "parse error: '%U'",
807 format_unformat_error, input);
812 punt_client_walk (pt, punt_client_show_one, vm);
815 unformat_free (input);
822 * @cliexcmd{show punt socket ipv4}
825 VLIB_CLI_COMMAND (show_punt_socket_registration_command, static) =
827 .path = "show punt socket registrations",
828 .function = punt_socket_show_cmd,
829 .short_help = "show punt socket registrations [l4|exception]",
835 ip_punt_init (vlib_main_t * vm)
837 clib_error_t *error = NULL;
838 punt_main_t *pm = &punt_main;
839 vlib_thread_main_t *tm = vlib_get_thread_main ();
841 pm->is_configured = false;
842 pm->interface_output_node =
843 vlib_get_node_by_name (vm, (u8 *) "interface-output");
845 if ((error = vlib_call_init_function (vm, punt_init)))
848 pm->hdl = vlib_punt_client_register ("ip-punt");
850 vec_validate_aligned (pm->thread_data, tm->n_vlib_mains,
851 CLIB_CACHE_LINE_BYTES);
857 format_vnet_punt_reason_flags (u8 *s, va_list *args)
859 vnet_punt_reason_flag_t flag = va_arg (*args, int);
860 #define _(pos, len, value, name, str) \
861 if (vnet_punt_reason_flag_is_##name (flag)) \
862 s = format (s, "%s ", str);
864 foreach_vnet_punt_reason_flag
869 VLIB_INIT_FUNCTION (ip_punt_init);
871 static clib_error_t *
872 punt_config (vlib_main_t * vm, unformat_input_t * input)
874 punt_main_t *pm = &punt_main;
875 char *socket_path = 0;
877 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
879 if (unformat (input, "socket %s", &socket_path))
880 strncpy (pm->sun_path, socket_path, UNIX_PATH_MAX - 1);
882 return clib_error_return (0, "unknown input `%U'",
883 format_unformat_error, input);
886 if (socket_path == 0)
889 /* UNIX domain socket */
890 struct sockaddr_un addr;
891 if ((pm->socket_fd = socket (AF_UNIX, SOCK_DGRAM | SOCK_NONBLOCK, 0)) == -1)
893 return clib_error_return (0, "socket error");
896 clib_memset (&addr, 0, sizeof (addr));
897 addr.sun_family = AF_UNIX;
898 if (*socket_path == '\0')
900 *addr.sun_path = '\0';
901 strncpy (addr.sun_path + 1, socket_path + 1,
902 sizeof (addr.sun_path) - 2);
906 strncpy (addr.sun_path, socket_path, sizeof (addr.sun_path) - 1);
907 unlink (socket_path);
910 if (bind (pm->socket_fd, (struct sockaddr *) &addr, sizeof (addr)) == -1)
912 return clib_error_return (0, "bind error");
915 int n_bytes = 0x10000;
918 (pm->socket_fd, SOL_SOCKET, SO_SNDBUF, &n_bytes,
919 sizeof (n_bytes)) == -1)
921 return clib_error_return (0, "setsockopt error");
924 /* Register socket */
925 clib_file_main_t *fm = &file_main;
926 clib_file_t template = { 0 };
927 template.read_function = punt_socket_read_ready;
928 template.file_descriptor = pm->socket_fd;
929 template.description = format (0, "punt socket %s", socket_path);
930 pm->clib_file_index = clib_file_add (fm, &template);
932 pm->is_configured = true;
937 VLIB_CONFIG_FUNCTION (punt_config, "punt");
940 * fd.io coding-style-patch-verification: ON
943 * eval: (c-set-style "gnu")