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 <vlib/unix/unix.h>
36 #include <sys/socket.h>
40 punt_main_t punt_main;
43 vnet_punt_get_server_pathname (void)
45 punt_main_t *pm = &punt_main;
50 punt_client_l4_db_add (ip_address_family_t af, u16 port, u32 index)
52 punt_main_t *pm = &punt_main;
54 pm->db.clients_by_l4_port = hash_set (pm->db.clients_by_l4_port,
55 punt_client_l4_mk_key (af, port),
60 punt_client_l4_db_remove (ip_address_family_t af, u16 port)
62 punt_main_t *pm = &punt_main;
66 key = punt_client_l4_mk_key (af, port);
67 p = hash_get (pm->db.clients_by_l4_port, key);
72 hash_unset (pm->db.clients_by_l4_port, key);
78 punt_client_ip_proto_db_add (ip_address_family_t af,
79 ip_protocol_t proto, u32 index)
81 punt_main_t *pm = &punt_main;
83 pm->db.clients_by_ip_proto = hash_set (pm->db.clients_by_ip_proto,
84 punt_client_ip_proto_mk_key (af,
90 punt_client_ip_proto_db_remove (ip_address_family_t af, ip_protocol_t proto)
92 punt_main_t *pm = &punt_main;
96 key = punt_client_ip_proto_mk_key (af, proto);
97 p = hash_get (pm->db.clients_by_ip_proto, key);
102 hash_unset (pm->db.clients_by_ip_proto, key);
108 punt_client_exception_db_add (vlib_punt_reason_t reason, u32 pci)
110 punt_main_t *pm = &punt_main;
112 vec_validate_init_empty (pm->db.clients_by_exception, reason, ~0);
114 pm->db.clients_by_exception[reason] = pci;
118 punt_client_exception_db_remove (vlib_punt_reason_t reason)
120 punt_main_t *pm = &punt_main;
123 if (punt_client_exception_get (reason))
125 pci = pm->db.clients_by_exception[reason];
126 pm->db.clients_by_exception[reason] = ~0;
132 static clib_error_t *
133 punt_socket_read_ready (clib_file_t * uf)
135 vlib_main_t *vm = vlib_get_main ();
136 punt_main_t *pm = &punt_main;
138 /** Schedule the rx node */
139 vlib_node_set_interrupt_pending (vm, punt_socket_rx_node.index);
140 vec_add1 (pm->ready_fds, uf->file_descriptor);
145 static clib_error_t *
146 punt_socket_register_l4 (vlib_main_t * vm,
147 ip_address_family_t af,
148 u8 protocol, u16 port, char *client_pathname)
150 punt_main_t *pm = &punt_main;
153 /* For now we only support UDP punt */
154 if (protocol != IP_PROTOCOL_UDP)
155 return clib_error_return (0,
156 "only UDP protocol (%d) is supported, got %d",
157 IP_PROTOCOL_UDP, protocol);
159 if (port == (u16) ~ 0)
160 return clib_error_return (0, "UDP port number required");
162 c = punt_client_l4_get (af, port);
166 pool_get_zero (pm->punt_client_pool, c);
167 punt_client_l4_db_add (af, port, c - pm->punt_client_pool);
170 memcpy (c->caddr.sun_path, client_pathname, sizeof (c->caddr.sun_path));
171 c->caddr.sun_family = AF_UNIX;
172 c->reg.type = PUNT_TYPE_L4;
173 c->reg.punt.l4.port = port;
174 c->reg.punt.l4.protocol = protocol;
175 c->reg.punt.l4.af = af;
177 u32 node_index = (af == AF_IP4 ?
178 udp4_punt_socket_node.index :
179 udp6_punt_socket_node.index);
181 udp_register_dst_port (vm, port, node_index, af == AF_IP4);
186 static clib_error_t *
187 punt_socket_register_ip_proto (vlib_main_t * vm,
188 ip_address_family_t af,
189 ip_protocol_t proto, char *client_pathname)
191 punt_main_t *pm = &punt_main;
194 c = punt_client_ip_proto_get (af, proto);
198 pool_get_zero (pm->punt_client_pool, c);
199 punt_client_ip_proto_db_add (af, proto, c - pm->punt_client_pool);
202 memcpy (c->caddr.sun_path, client_pathname, sizeof (c->caddr.sun_path));
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 memcpy (pc->caddr.sun_path, client_pathname, sizeof (pc->caddr.sun_path));
233 pc->caddr.sun_family = AF_UNIX;
234 pc->reg.type = PUNT_TYPE_EXCEPTION;
235 pc->reg.punt.exception.reason = reason;
237 vlib_punt_register (pm->hdl,
238 pc->reg.punt.exception.reason, "exception-punt-socket");
243 static clib_error_t *
244 punt_socket_unregister_l4 (ip_address_family_t af,
245 ip_protocol_t protocol, u16 port)
249 udp_unregister_dst_port (vlib_get_main (), port, af == AF_IP4);
251 pci = punt_client_l4_db_remove (af, port);
254 pool_put_index (punt_main.punt_client_pool, pci);
259 static clib_error_t *
260 punt_socket_unregister_ip_proto (ip_address_family_t af, ip_protocol_t proto)
265 ip4_unregister_protocol (proto);
267 ip6_unregister_protocol (proto);
269 pci = punt_client_ip_proto_db_remove (af, proto);
272 pool_put_index (punt_main.punt_client_pool, pci);
277 static clib_error_t *
278 punt_socket_unregister_exception (vlib_punt_reason_t reason)
282 pci = punt_client_exception_db_remove (reason);
285 pool_put_index (punt_main.punt_client_pool, pci);
291 vnet_punt_socket_add (vlib_main_t * vm, u32 header_version,
292 const punt_reg_t * pr, char *client_pathname)
294 punt_main_t *pm = &punt_main;
296 if (!pm->is_configured)
297 return clib_error_return (0, "socket is not configured");
299 if (header_version != PUNT_PACKETDESC_VERSION)
300 return clib_error_return (0, "Invalid packet descriptor version");
302 if (strncmp (client_pathname, vnet_punt_get_server_pathname (),
304 return clib_error_return (0,
305 "Punt socket: Invalid client path: %s",
308 /* Register client */
312 return (punt_socket_register_l4 (vm,
314 pr->punt.l4.protocol,
315 pr->punt.l4.port, client_pathname));
316 case PUNT_TYPE_IP_PROTO:
317 return (punt_socket_register_ip_proto (vm,
318 pr->punt.ip_proto.af,
319 pr->punt.ip_proto.protocol,
321 case PUNT_TYPE_EXCEPTION:
322 return (punt_socket_register_exception (vm,
323 pr->punt.exception.reason,
331 vnet_punt_socket_del (vlib_main_t * vm, const punt_reg_t * pr)
333 punt_main_t *pm = &punt_main;
335 if (!pm->is_configured)
336 return clib_error_return (0, "socket is not configured");
341 return (punt_socket_unregister_l4 (pr->punt.l4.af,
342 pr->punt.l4.protocol,
344 case PUNT_TYPE_IP_PROTO:
345 return (punt_socket_unregister_ip_proto (pr->punt.ip_proto.af,
346 pr->punt.ip_proto.protocol));
347 case PUNT_TYPE_EXCEPTION:
348 return (punt_socket_unregister_exception (pr->punt.exception.reason));
355 * @brief Request IP traffic punt to the local TCP/IP stack.
358 * - UDP, TCP and SCTP are the only protocols supported in the current implementation
360 * @param vm vlib_main_t corresponding to the current thread
361 * @param af IP address family.
362 * @param protocol 8-bits L4 protocol value
365 * @param port 16-bits L4 (TCP/IP) port number when applicable (UDP only)
367 * @returns 0 on success, non-zero value otherwise
369 static clib_error_t *
370 punt_l4_add_del (vlib_main_t * vm,
371 ip_address_family_t af,
372 ip_protocol_t protocol, u16 port, bool is_add)
374 /* For now we only support TCP, UDP and SCTP punt */
375 if (protocol != IP_PROTOCOL_UDP &&
376 protocol != IP_PROTOCOL_TCP && protocol != IP_PROTOCOL_SCTP)
377 return clib_error_return (0,
378 "only UDP (%d), TCP (%d) and SCTP (%d) protocols are supported, got %d",
379 IP_PROTOCOL_UDP, IP_PROTOCOL_TCP,
380 IP_PROTOCOL_SCTP, protocol);
382 if (port == (u16) ~ 0)
384 if (protocol == IP_PROTOCOL_UDP)
385 udp_punt_unknown (vm, af == AF_IP4, is_add);
386 else if (protocol == IP_PROTOCOL_TCP)
387 tcp_punt_unknown (vm, af == AF_IP4, is_add);
388 else if (protocol == IP_PROTOCOL_SCTP)
389 sctp_punt_unknown (vm, af == AF_IP4, is_add);
396 if (protocol == IP_PROTOCOL_TCP || protocol == IP_PROTOCOL_SCTP)
397 return clib_error_return (0,
398 "punt TCP/SCTP ports is not supported yet");
400 udp_register_dst_port (vm, port, udp4_punt_node.index, af == AF_IP4);
406 if (protocol == IP_PROTOCOL_TCP || protocol == IP_PROTOCOL_SCTP)
407 return clib_error_return (0,
408 "punt TCP/SCTP ports is not supported yet");
410 udp_unregister_dst_port (vm, port, af == AF_IP4);
417 vnet_punt_add_del (vlib_main_t * vm, const punt_reg_t * pr, bool is_add)
422 return (punt_l4_add_del (vm, pr->punt.l4.af, pr->punt.l4.protocol,
423 pr->punt.l4.port, is_add));
424 case PUNT_TYPE_EXCEPTION:
425 case PUNT_TYPE_IP_PROTO:
429 return (clib_error_return (0, "Unsupported punt type: %d", pr->type));
432 static clib_error_t *
433 punt_cli (vlib_main_t * vm,
434 unformat_input_t * input, vlib_cli_command_t * cmd)
436 clib_error_t *error = NULL;
447 .type = PUNT_TYPE_L4,
452 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
454 if (unformat (input, "del"))
456 else if (unformat (input, "ipv6"))
457 pr.punt.l4.af = AF_IP6;
458 else if (unformat (input, "ip6"))
459 pr.punt.l4.af = AF_IP6;
460 else if (unformat (input, "%d", &port))
461 pr.punt.l4.port = port;
462 else if (unformat (input, "udp"))
463 pr.punt.l4.protocol = IP_PROTOCOL_UDP;
464 else if (unformat (input, "tcp"))
465 pr.punt.l4.protocol = IP_PROTOCOL_TCP;
468 error = clib_error_return (0, "parse error: '%U'",
469 format_unformat_error, input);
474 /* punt both IPv6 and IPv4 when used in CLI */
475 error = vnet_punt_add_del (vm, &pr, is_add);
478 clib_error_report (error);
486 * The set of '<em>set punt</em>' commands allows specific IP traffic to
487 * be punted to the host TCP/IP stack
490 * - UDP is the only protocol supported in the current implementation
491 * - All TCP traffic is currently punted to the host by default
495 * Example of how to request NTP traffic to be punted
496 * @cliexcmd{set punt udp 125}
498 * Example of how to request all 'unknown' UDP traffic to be punted
499 * @cliexcmd{set punt udp all}
501 * Example of how to stop all 'unknown' UDP traffic to be punted
502 * @cliexcmd{set punt udp del all}
506 VLIB_CLI_COMMAND (punt_command, static) = {
508 .short_help = "set punt [udp|tcp] [del] <all | port-num1 [port-num2 ...]>",
509 .function = punt_cli,
513 static clib_error_t *
514 punt_socket_register_cmd (vlib_main_t * vm,
515 unformat_input_t * input, vlib_cli_command_t * cmd)
518 clib_error_t *error = NULL;
528 .type = PUNT_TYPE_L4,
532 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
534 if (unformat (input, "ipv4"))
536 else if (unformat (input, "ipv6"))
537 pr.punt.l4.af = AF_IP6;
538 else if (unformat (input, "udp"))
539 pr.punt.l4.protocol = IP_PROTOCOL_UDP;
540 else if (unformat (input, "tcp"))
541 pr.punt.l4.protocol = IP_PROTOCOL_TCP;
542 else if (unformat (input, "%d", &pr.punt.l4.port))
544 else if (unformat (input, "socket %s", &socket_name))
548 error = clib_error_return (0, "parse error: '%U'",
549 format_unformat_error, input);
555 error = clib_error_return (0, "socket name not specified");
557 error = vnet_punt_socket_add (vm, 1, &pr, (char *) socket_name);
566 * @cliexcmd{punt socket register}
569 VLIB_CLI_COMMAND (punt_socket_register_command, static) =
571 .path = "punt socket register",
572 .function = punt_socket_register_cmd,
573 .short_help = "punt socket register [ipv4|ipv6] [udp|tcp]> <all | port-num1 [port-num2 ...]> <socket>",
578 static clib_error_t *
579 punt_socket_deregister_cmd (vlib_main_t * vm,
580 unformat_input_t * input,
581 vlib_cli_command_t * cmd)
583 clib_error_t *error = NULL;
593 .type = PUNT_TYPE_L4,
597 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
599 if (unformat (input, "ipv4"))
601 else if (unformat (input, "ipv6"))
602 pr.punt.l4.af = AF_IP6;
603 else if (unformat (input, "udp"))
604 pr.punt.l4.protocol = IP_PROTOCOL_UDP;
605 else if (unformat (input, "tcp"))
606 pr.punt.l4.protocol = IP_PROTOCOL_TCP;
607 else if (unformat (input, "%d", &pr.punt.l4.port))
611 error = clib_error_return (0, "parse error: '%U'",
612 format_unformat_error, input);
617 error = vnet_punt_socket_del (vm, &pr);
625 * @cliexcmd{punt socket register}
628 VLIB_CLI_COMMAND (punt_socket_deregister_command, static) =
630 .path = "punt socket deregister",
631 .function = punt_socket_deregister_cmd,
632 .short_help = "punt socket deregister [ipv4|ipv6] [udp|tcp]> <all | port-num1 [port-num2 ...]>",
638 punt_client_walk (punt_type_t pt, punt_client_walk_cb_t cb, void *ctx)
640 punt_main_t *pm = &punt_main;
649 hash_foreach(key, pci, pm->db.clients_by_l4_port,
651 cb (pool_elt_at_index(pm->punt_client_pool, pci), ctx);
656 case PUNT_TYPE_IP_PROTO:
661 hash_foreach(key, pci, pm->db.clients_by_ip_proto,
663 cb (pool_elt_at_index(pm->punt_client_pool, pci), ctx);
668 case PUNT_TYPE_EXCEPTION:
672 vec_foreach (pci, pm->db.clients_by_exception)
675 cb (pool_elt_at_index (pm->punt_client_pool, *pci), ctx);
684 format_punt_client (u8 * s, va_list * args)
686 punt_client_t *pc = va_arg (*args, punt_client_t *);
688 s = format (s, " punt ");
690 switch (pc->reg.type)
693 s = format (s, "%U %U port %d",
694 format_ip_address_family, pc->reg.punt.l4.af,
695 format_ip_protocol, pc->reg.punt.l4.protocol,
696 pc->reg.punt.l4.port);
698 case PUNT_TYPE_IP_PROTO:
699 s = format (s, "%U %U",
700 format_ip_address_family, pc->reg.punt.ip_proto.af,
701 format_ip_protocol, pc->reg.punt.ip_proto.protocol);
703 case PUNT_TYPE_EXCEPTION:
704 s = format (s, " %U", format_vlib_punt_reason,
705 pc->reg.punt.exception.reason);
709 s = format (s, " to socket %s \n", pc->caddr.sun_path);
715 punt_client_show_one (const punt_client_t * pc, void *ctx)
717 vlib_cli_output (ctx, "%U", format_punt_client, pc);
719 return (WALK_CONTINUE);
722 static clib_error_t *
723 punt_socket_show_cmd (vlib_main_t * vm,
724 unformat_input_t * input, vlib_cli_command_t * cmd)
726 clib_error_t *error = NULL;
731 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
733 if (unformat (input, "exception"))
734 pt = PUNT_TYPE_EXCEPTION;
735 else if (unformat (input, "l4"))
737 else if (unformat (input, "ip"))
738 pt = PUNT_TYPE_IP_PROTO;
741 error = clib_error_return (0, "parse error: '%U'",
742 format_unformat_error, input);
747 punt_client_walk (pt, punt_client_show_one, vm);
756 * @cliexcmd{show punt socket ipv4}
759 VLIB_CLI_COMMAND (show_punt_socket_registration_command, static) =
761 .path = "show punt socket registrations",
762 .function = punt_socket_show_cmd,
763 .short_help = "show punt socket registrations [l4|exception]",
769 ip_punt_init (vlib_main_t * vm)
771 clib_error_t *error = NULL;
772 punt_main_t *pm = &punt_main;
774 pm->is_configured = false;
775 pm->interface_output_node =
776 vlib_get_node_by_name (vm, (u8 *) "interface-output");
778 if ((error = vlib_call_init_function (vm, punt_init)))
781 pm->hdl = vlib_punt_client_register ("ip-punt");
786 VLIB_INIT_FUNCTION (ip_punt_init);
788 static clib_error_t *
789 punt_config (vlib_main_t * vm, unformat_input_t * input)
791 punt_main_t *pm = &punt_main;
792 char *socket_path = 0;
794 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
796 if (unformat (input, "socket %s", &socket_path))
797 strncpy (pm->sun_path, socket_path, UNIX_PATH_MAX - 1);
799 return clib_error_return (0, "unknown input `%U'",
800 format_unformat_error, input);
803 if (socket_path == 0)
806 /* UNIX domain socket */
807 struct sockaddr_un addr;
808 if ((pm->socket_fd = socket (AF_UNIX, SOCK_DGRAM | SOCK_NONBLOCK, 0)) == -1)
810 return clib_error_return (0, "socket error");
813 clib_memset (&addr, 0, sizeof (addr));
814 addr.sun_family = AF_UNIX;
815 if (*socket_path == '\0')
817 *addr.sun_path = '\0';
818 strncpy (addr.sun_path + 1, socket_path + 1,
819 sizeof (addr.sun_path) - 2);
823 strncpy (addr.sun_path, socket_path, sizeof (addr.sun_path) - 1);
824 unlink (socket_path);
827 if (bind (pm->socket_fd, (struct sockaddr *) &addr, sizeof (addr)) == -1)
829 return clib_error_return (0, "bind error");
832 int n_bytes = 0x10000;
835 (pm->socket_fd, SOL_SOCKET, SO_SNDBUF, &n_bytes,
836 sizeof (n_bytes)) == -1)
838 return clib_error_return (0, "setsockopt error");
841 /* Register socket */
842 clib_file_main_t *fm = &file_main;
843 clib_file_t template = { 0 };
844 template.read_function = punt_socket_read_ready;
845 template.file_descriptor = pm->socket_fd;
846 template.description = format (0, "%s", socket_path);
847 pm->clib_file_index = clib_file_add (fm, &template);
849 pm->is_configured = true;
854 VLIB_CONFIG_FUNCTION (punt_config, "punt");
857 * fd.io coding-style-patch-verification: ON
860 * eval: (c-set-style "gnu")