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 if (port == (u16) ~ 0)
152 return clib_error_return (0, "Port number required");
157 case IP_PROTOCOL_UDP:
158 node_index = (af == AF_IP4 ? udp4_punt_socket_node.index :
159 udp6_punt_socket_node.index);
160 udp_register_dst_port (vm, port, node_index, af == AF_IP4);
162 case IP_PROTOCOL_ICMP6:
164 return clib_error_return (
165 0, "only UDP or ICMP6 protocol (%d, %d) is supported, got %d",
166 IP_PROTOCOL_UDP, IP_PROTOCOL_ICMP6, protocol);
168 node_index = icmp6_punt_socket_node.index;
169 icmp6_register_type (vm, port, node_index);
172 return clib_error_return (
173 0, "only UDP or ICMP6 protocol (%d) is supported, got %d",
174 IP_PROTOCOL_UDP, protocol);
177 c = punt_client_l4_get (af, port);
181 pool_get_zero (pm->punt_client_pool, c);
182 punt_client_l4_db_add (af, port, c - pm->punt_client_pool);
185 snprintf (c->caddr.sun_path, sizeof (c->caddr.sun_path), "%s",
187 c->caddr.sun_family = AF_UNIX;
188 c->reg.type = PUNT_TYPE_L4;
189 c->reg.punt.l4.port = port;
190 c->reg.punt.l4.protocol = protocol;
191 c->reg.punt.l4.af = af;
196 static clib_error_t *
197 punt_socket_register_ip_proto (vlib_main_t * vm,
198 ip_address_family_t af,
199 ip_protocol_t proto, char *client_pathname)
201 punt_main_t *pm = &punt_main;
204 c = punt_client_ip_proto_get (af, proto);
208 pool_get_zero (pm->punt_client_pool, c);
209 punt_client_ip_proto_db_add (af, proto, c - pm->punt_client_pool);
212 snprintf (c->caddr.sun_path, sizeof (c->caddr.sun_path), "%s",
214 c->caddr.sun_family = AF_UNIX;
215 c->reg.type = PUNT_TYPE_IP_PROTO;
216 c->reg.punt.ip_proto.protocol = proto;
217 c->reg.punt.ip_proto.af = af;
220 ip4_register_protocol (proto, ip4_proto_punt_socket_node.index);
222 ip6_register_protocol (proto, ip6_proto_punt_socket_node.index);
227 static clib_error_t *
228 punt_socket_register_exception (vlib_main_t * vm,
229 vlib_punt_reason_t reason,
230 char *client_pathname)
232 punt_main_t *pm = &punt_main;
235 pc = punt_client_exception_get (reason);
239 pool_get_zero (pm->punt_client_pool, pc);
240 punt_client_exception_db_add (reason, pc - pm->punt_client_pool);
243 snprintf (pc->caddr.sun_path, sizeof (pc->caddr.sun_path), "%s",
245 pc->caddr.sun_family = AF_UNIX;
246 pc->reg.type = PUNT_TYPE_EXCEPTION;
247 pc->reg.punt.exception.reason = reason;
249 vlib_punt_register (pm->hdl,
250 pc->reg.punt.exception.reason, "exception-punt-socket");
255 static clib_error_t *
256 punt_socket_unregister_l4 (ip_address_family_t af,
257 ip_protocol_t protocol, u16 port)
261 udp_unregister_dst_port (vlib_get_main (), port, af == AF_IP4);
263 pci = punt_client_l4_db_remove (af, port);
266 pool_put_index (punt_main.punt_client_pool, pci);
271 static clib_error_t *
272 punt_socket_unregister_ip_proto (ip_address_family_t af, ip_protocol_t proto)
277 ip4_unregister_protocol (proto);
279 ip6_unregister_protocol (proto);
281 pci = punt_client_ip_proto_db_remove (af, proto);
284 pool_put_index (punt_main.punt_client_pool, pci);
289 static clib_error_t *
290 punt_socket_unregister_exception (vlib_punt_reason_t reason)
294 pci = punt_client_exception_db_remove (reason);
297 pool_put_index (punt_main.punt_client_pool, pci);
303 vnet_punt_socket_add (vlib_main_t * vm, u32 header_version,
304 const punt_reg_t * pr, char *client_pathname)
306 punt_main_t *pm = &punt_main;
308 if (!pm->is_configured)
309 return clib_error_return (0, "socket is not configured");
311 if (header_version != PUNT_PACKETDESC_VERSION)
312 return clib_error_return (0, "Invalid packet descriptor version");
314 if (strncmp (client_pathname, vnet_punt_get_server_pathname (),
316 return clib_error_return (0,
317 "Punt socket: Invalid client path: %s",
320 /* Register client */
324 return (punt_socket_register_l4 (vm,
326 pr->punt.l4.protocol,
327 pr->punt.l4.port, client_pathname));
328 case PUNT_TYPE_IP_PROTO:
329 return (punt_socket_register_ip_proto (vm,
330 pr->punt.ip_proto.af,
331 pr->punt.ip_proto.protocol,
333 case PUNT_TYPE_EXCEPTION:
334 return (punt_socket_register_exception (vm,
335 pr->punt.exception.reason,
343 vnet_punt_socket_del (vlib_main_t * vm, const punt_reg_t * pr)
345 punt_main_t *pm = &punt_main;
347 if (!pm->is_configured)
348 return clib_error_return (0, "socket is not configured");
353 return (punt_socket_unregister_l4 (pr->punt.l4.af,
354 pr->punt.l4.protocol,
356 case PUNT_TYPE_IP_PROTO:
357 return (punt_socket_unregister_ip_proto (pr->punt.ip_proto.af,
358 pr->punt.ip_proto.protocol));
359 case PUNT_TYPE_EXCEPTION:
360 return (punt_socket_unregister_exception (pr->punt.exception.reason));
367 * @brief Request IP L4 traffic punt to the local TCP/IP stack.
370 * - UDP is the only protocol supported in the current implementation
372 * @param vm vlib_main_t corresponding to the current thread
373 * @param af IP address family.
374 * @param protocol 8-bits L4 protocol value
377 * @param port 16-bits L4 (TCP/IP) port number when applicable (UDP only)
379 * @returns 0 on success, non-zero value otherwise
381 static clib_error_t *
382 punt_l4_add_del (vlib_main_t * vm,
383 ip_address_family_t af,
384 ip_protocol_t protocol, u16 port, bool is_add)
386 int is_ip4 = af == AF_IP4;
388 /* For now we only support TCP and UDP punt */
389 if (protocol != IP_PROTOCOL_UDP && protocol != IP_PROTOCOL_TCP)
390 return clib_error_return (0,
391 "only UDP (%d) and TCP (%d) protocols are supported, got %d",
392 IP_PROTOCOL_UDP, IP_PROTOCOL_TCP, protocol);
394 if (port == (u16) ~ 0)
396 if (protocol == IP_PROTOCOL_UDP)
397 udp_punt_unknown (vm, is_ip4, is_add);
398 else if (protocol == IP_PROTOCOL_TCP)
399 tcp_punt_unknown (vm, is_ip4, is_add);
406 const vlib_node_registration_t *punt_node =
407 is_ip4 ? &udp4_punt_node : &udp6_punt_node;
409 if (protocol == IP_PROTOCOL_TCP)
410 return clib_error_return (0, "punt TCP ports is not supported yet");
412 udp_register_dst_port (vm, port, punt_node->index, is_ip4);
418 if (protocol == IP_PROTOCOL_TCP)
419 return clib_error_return (0, "punt TCP ports is not supported yet");
421 udp_unregister_dst_port (vm, port, is_ip4);
428 * @brief Request exception traffic punt.
430 * @param reason Punting reason
432 * @returns 0 on success, non-zero value otherwise
434 static clib_error_t *
435 punt_exception_add_del (vlib_punt_reason_t reason, bool is_add)
437 punt_main_t *pm = &punt_main;
439 vnet_punt_reason_flag_t flag = vlib_punt_reason_get_flags (reason);
440 const char *node_name =
441 vnet_punt_reason_flag_is_IP6_PACKET (flag) ? "ip6-punt" : "ip4-punt";
443 rv = vlib_punt_register (pm->hdl, reason, node_name);
445 rv = vlib_punt_unregister (pm->hdl, reason, node_name);
449 return clib_error_return (0, is_add ? "Existing punting registration..." :
450 "Punting registration not found...");
454 vnet_punt_add_del (vlib_main_t * vm, const punt_reg_t * pr, bool is_add)
459 return (punt_l4_add_del (vm, pr->punt.l4.af, pr->punt.l4.protocol,
460 pr->punt.l4.port, is_add));
461 case PUNT_TYPE_EXCEPTION:
462 return punt_exception_add_del (pr->punt.exception.reason, is_add);
463 case PUNT_TYPE_IP_PROTO:
467 return (clib_error_return (0, "Unsupported punt type: %d", pr->type));
470 static clib_error_t *
471 punt_cli (vlib_main_t * vm,
472 unformat_input_t * input__, vlib_cli_command_t * cmd)
474 unformat_input_t line_input, *input = &line_input;
475 clib_error_t *error = NULL;
482 .protocol = IP_PROTOCOL_UDP,
485 .type = PUNT_TYPE_L4,
489 if (!unformat_user (input__, unformat_line_input, input))
492 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
494 if (unformat (input, "del"))
496 else if (unformat (input, "reason %U", unformat_punt_reason,
497 &pr.punt.exception.reason))
498 pr.type = PUNT_TYPE_EXCEPTION;
499 else if (unformat (input, "ipv4"))
500 pr.punt.l4.af = AF_IP4;
501 else if (unformat (input, "ipv6"))
502 pr.punt.l4.af = AF_IP6;
503 else if (unformat (input, "ip6"))
504 pr.punt.l4.af = AF_IP6;
505 else if (unformat (input, "%d", &port))
506 pr.punt.l4.port = port;
507 else if (unformat (input, "all"))
508 pr.punt.l4.port = ~0;
509 else if (unformat (input, "udp"))
510 pr.punt.l4.protocol = IP_PROTOCOL_UDP;
511 else if (unformat (input, "tcp"))
512 pr.punt.l4.protocol = IP_PROTOCOL_TCP;
515 error = clib_error_return (0, "parse error: '%U'",
516 format_unformat_error, input);
521 /* punt both IPv6 and IPv4 when used in CLI */
522 error = vnet_punt_add_del (vm, &pr, is_add);
525 clib_error_report (error);
529 unformat_free (input);
534 * The set of '<em>set punt</em>' commands allows specific IP traffic to
535 * be punted to the host TCP/IP stack
538 * - UDP is the only protocol supported in the current implementation
539 * - All TCP traffic is currently punted to the host by default
543 * Example of how to request NTP traffic to be punted
544 * @cliexcmd{set punt udp 125}
546 * Example of how to request all 'unknown' UDP traffic to be punted
547 * @cliexcmd{set punt udp all}
549 * Example of how to stop all 'unknown' UDP traffic to be punted
550 * @cliexcmd{set punt udp del all}
553 VLIB_CLI_COMMAND (punt_command, static) = {
555 .short_help = "set punt [IPV4|ip6|ipv6] [UDP|tcp] [del] [ALL|<port-num>]",
556 .function = punt_cli,
559 static clib_error_t *
560 punt_socket_register_cmd (vlib_main_t * vm,
561 unformat_input_t * input__,
562 vlib_cli_command_t * cmd)
564 unformat_input_t line_input, *input = &line_input;
566 clib_error_t *error = NULL;
572 .protocol = IP_PROTOCOL_UDP,
575 .type = PUNT_TYPE_L4,
578 if (!unformat_user (input__, unformat_line_input, input))
581 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
583 if (unformat (input, "ipv4"))
584 pr.punt.l4.af = AF_IP4;
585 else if (unformat (input, "ipv6"))
586 pr.punt.l4.af = AF_IP6;
587 else if (unformat (input, "udp"))
588 pr.punt.l4.protocol = IP_PROTOCOL_UDP;
589 else if (unformat (input, "tcp"))
590 pr.punt.l4.protocol = IP_PROTOCOL_TCP;
591 else if (unformat (input, "%d", &pr.punt.l4.port))
593 else if (unformat (input, "all"))
594 pr.punt.l4.port = ~0;
595 else if (unformat (input, "socket %s", &socket_name))
597 else if (unformat (input, "reason %U", unformat_punt_reason,
598 &pr.punt.exception.reason))
599 pr.type = PUNT_TYPE_EXCEPTION;
602 error = clib_error_return (0, "parse error: '%U'",
603 format_unformat_error, input);
609 error = clib_error_return (0, "socket name not specified");
611 error = vnet_punt_socket_add (vm, 1, &pr, (char *) socket_name);
614 unformat_free (input);
621 * @cliexcmd{punt socket register socket punt_l4_foo.sock}
624 VLIB_CLI_COMMAND (punt_socket_register_command, static) =
626 .path = "punt socket register",
627 .function = punt_socket_register_cmd,
628 .short_help = "punt socket register [IPV4|ipv6] [UDP|tcp] [ALL|<port-num>] socket <socket>",
632 static clib_error_t *
633 punt_socket_deregister_cmd (vlib_main_t * vm,
634 unformat_input_t * input__,
635 vlib_cli_command_t * cmd)
637 unformat_input_t line_input, *input = &line_input;
638 clib_error_t *error = NULL;
644 .protocol = IP_PROTOCOL_UDP,
647 .type = PUNT_TYPE_L4,
650 if (!unformat_user (input__, unformat_line_input, input))
653 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
655 if (unformat (input, "ipv4"))
656 pr.punt.l4.af = AF_IP4;
657 else if (unformat (input, "ipv6"))
658 pr.punt.l4.af = AF_IP6;
659 else if (unformat (input, "udp"))
660 pr.punt.l4.protocol = IP_PROTOCOL_UDP;
661 else if (unformat (input, "tcp"))
662 pr.punt.l4.protocol = IP_PROTOCOL_TCP;
663 else if (unformat (input, "%d", &pr.punt.l4.port))
665 else if (unformat (input, "all"))
666 pr.punt.l4.port = ~0;
667 else if (unformat (input, "reason %U", unformat_punt_reason,
668 &pr.punt.exception.reason))
669 pr.type = PUNT_TYPE_EXCEPTION;
672 error = clib_error_return (0, "parse error: '%U'",
673 format_unformat_error, input);
678 error = vnet_punt_socket_del (vm, &pr);
680 unformat_free (input);
687 * @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>]",
698 punt_client_walk (punt_type_t pt, punt_client_walk_cb_t cb, void *ctx)
700 punt_main_t *pm = &punt_main;
708 hash_foreach(key, pci, pm->db.clients_by_l4_port,
710 cb (pool_elt_at_index(pm->punt_client_pool, pci), ctx);
714 case PUNT_TYPE_IP_PROTO:
718 hash_foreach(key, pci, pm->db.clients_by_ip_proto,
720 cb (pool_elt_at_index(pm->punt_client_pool, pci), ctx);
724 case PUNT_TYPE_EXCEPTION:
728 vec_foreach (pci, pm->db.clients_by_exception)
731 cb (pool_elt_at_index (pm->punt_client_pool, *pci), ctx);
740 format_punt_client (u8 * s, va_list * args)
742 punt_client_t *pc = va_arg (*args, punt_client_t *);
744 s = format (s, " punt ");
746 switch (pc->reg.type)
749 s = format (s, "%U %U port %d",
750 format_ip_address_family, pc->reg.punt.l4.af,
751 format_ip_protocol, pc->reg.punt.l4.protocol,
752 pc->reg.punt.l4.port);
754 case PUNT_TYPE_IP_PROTO:
755 s = format (s, "%U %U",
756 format_ip_address_family, pc->reg.punt.ip_proto.af,
757 format_ip_protocol, pc->reg.punt.ip_proto.protocol);
759 case PUNT_TYPE_EXCEPTION:
760 s = format (s, " %U", format_vlib_punt_reason,
761 pc->reg.punt.exception.reason);
765 s = format (s, " to socket %s \n", pc->caddr.sun_path);
771 punt_client_show_one (const punt_client_t * pc, void *ctx)
773 vlib_cli_output (ctx, "%U", format_punt_client, pc);
775 return (WALK_CONTINUE);
778 static clib_error_t *
779 punt_socket_show_cmd (vlib_main_t * vm,
780 unformat_input_t * input__, vlib_cli_command_t * cmd)
782 unformat_input_t line_input, *input = &line_input;
783 clib_error_t *error = NULL;
788 if (!unformat_user (input__, unformat_line_input, input))
791 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
793 if (unformat (input, "exception"))
794 pt = PUNT_TYPE_EXCEPTION;
795 else if (unformat (input, "l4"))
797 else if (unformat (input, "ip"))
798 pt = PUNT_TYPE_IP_PROTO;
801 error = clib_error_return (0, "parse error: '%U'",
802 format_unformat_error, input);
807 punt_client_walk (pt, punt_client_show_one, vm);
810 unformat_free (input);
817 * @cliexcmd{show punt socket ipv4}
819 VLIB_CLI_COMMAND (show_punt_socket_registration_command, static) =
821 .path = "show punt socket registrations",
822 .function = punt_socket_show_cmd,
823 .short_help = "show punt socket registrations [l4|exception]",
828 ip_punt_init (vlib_main_t * vm)
830 clib_error_t *error = NULL;
831 punt_main_t *pm = &punt_main;
832 vlib_thread_main_t *tm = vlib_get_thread_main ();
834 pm->is_configured = false;
835 pm->interface_output_node =
836 vlib_get_node_by_name (vm, (u8 *) "interface-output");
838 if ((error = vlib_call_init_function (vm, punt_init)))
841 pm->hdl = vlib_punt_client_register ("ip-punt");
843 vec_validate_aligned (pm->thread_data, tm->n_vlib_mains,
844 CLIB_CACHE_LINE_BYTES);
850 format_vnet_punt_reason_flags (u8 *s, va_list *args)
852 vnet_punt_reason_flag_t flag = va_arg (*args, int);
853 #define _(pos, len, value, name, str) \
854 if (vnet_punt_reason_flag_is_##name (flag)) \
855 s = format (s, "%s ", str);
857 foreach_vnet_punt_reason_flag
862 VLIB_INIT_FUNCTION (ip_punt_init);
864 static clib_error_t *
865 punt_config (vlib_main_t * vm, unformat_input_t * input)
867 punt_main_t *pm = &punt_main;
868 char *socket_path = 0;
870 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
872 if (unformat (input, "socket %s", &socket_path))
873 strncpy (pm->sun_path, socket_path, UNIX_PATH_MAX - 1);
875 return clib_error_return (0, "unknown input `%U'",
876 format_unformat_error, input);
879 if (socket_path == 0)
882 /* UNIX domain socket */
883 struct sockaddr_un addr;
884 if ((pm->socket_fd = socket (AF_UNIX, SOCK_DGRAM | SOCK_NONBLOCK, 0)) == -1)
886 return clib_error_return (0, "socket error");
889 clib_memset (&addr, 0, sizeof (addr));
890 addr.sun_family = AF_UNIX;
891 if (*socket_path == '\0')
893 *addr.sun_path = '\0';
894 strncpy (addr.sun_path + 1, socket_path + 1,
895 sizeof (addr.sun_path) - 2);
899 strncpy (addr.sun_path, socket_path, sizeof (addr.sun_path) - 1);
900 unlink (socket_path);
903 if (bind (pm->socket_fd, (struct sockaddr *) &addr, sizeof (addr)) == -1)
905 return clib_error_return (0, "bind error");
908 int n_bytes = 0x10000;
911 (pm->socket_fd, SOL_SOCKET, SO_SNDBUF, &n_bytes,
912 sizeof (n_bytes)) == -1)
914 return clib_error_return (0, "setsockopt error");
917 /* Register socket */
918 clib_file_main_t *fm = &file_main;
919 clib_file_t template = { 0 };
920 template.read_function = punt_socket_read_ready;
921 template.file_descriptor = pm->socket_fd;
922 template.description = format (0, "punt socket %s", socket_path);
923 pm->clib_file_index = clib_file_add (fm, &template);
925 pm->is_configured = true;
930 VLIB_CONFIG_FUNCTION (punt_config, "punt");
933 * fd.io coding-style-patch-verification: ON
936 * eval: (c-set-style "gnu")