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_exception_db_add (vlib_punt_reason_t reason, u32 pci)
80 punt_main_t *pm = &punt_main;
82 vec_validate_init_empty (pm->db.clients_by_exception, reason, ~0);
84 pm->db.clients_by_exception[reason] = pci;
88 punt_client_exception_db_remove (vlib_punt_reason_t reason)
90 punt_main_t *pm = &punt_main;
93 if (punt_client_exception_get (reason))
95 pci = pm->db.clients_by_exception[reason];
96 pm->db.clients_by_exception[reason] = ~0;
102 static clib_error_t *
103 punt_socket_read_ready (clib_file_t * uf)
105 vlib_main_t *vm = vlib_get_main ();
106 punt_main_t *pm = &punt_main;
108 /** Schedule the rx node */
109 vlib_node_set_interrupt_pending (vm, punt_socket_rx_node.index);
110 vec_add1 (pm->ready_fds, uf->file_descriptor);
115 static clib_error_t *
116 punt_socket_register_l4 (vlib_main_t * vm,
117 ip_address_family_t af,
118 u8 protocol, u16 port, char *client_pathname)
120 punt_main_t *pm = &punt_main;
123 /* For now we only support UDP punt */
124 if (protocol != IP_PROTOCOL_UDP)
125 return clib_error_return (0,
126 "only UDP protocol (%d) is supported, got %d",
127 IP_PROTOCOL_UDP, protocol);
129 if (port == (u16) ~ 0)
130 return clib_error_return (0, "UDP port number required");
132 if (strncmp (client_pathname, vnet_punt_get_server_pathname (),
134 return clib_error_return (0,
135 "Punt socket: Invalid client path: %s",
138 c = punt_client_l4_get (af, port);
142 pool_get_zero (pm->punt_client_pool, c);
143 punt_client_l4_db_add (af, port, c - pm->punt_client_pool);
146 memcpy (c->caddr.sun_path, client_pathname, sizeof (c->caddr.sun_path));
147 c->caddr.sun_family = AF_UNIX;
148 c->reg.type = PUNT_TYPE_L4;
149 c->reg.punt.l4.port = port;
150 c->reg.punt.l4.protocol = protocol;
151 c->reg.punt.l4.af = af;
153 u32 node_index = (af == AF_IP4 ?
154 udp4_punt_socket_node.index :
155 udp6_punt_socket_node.index);
157 udp_register_dst_port (vm, port, node_index, af == AF_IP4);
162 static clib_error_t *
163 punt_socket_register_exception (vlib_main_t * vm,
164 vlib_punt_reason_t reason,
165 char *client_pathname)
167 punt_main_t *pm = &punt_main;
170 pc = punt_client_exception_get (reason);
174 pool_get_zero (pm->punt_client_pool, pc);
175 punt_client_exception_db_add (reason, pc - pm->punt_client_pool);
178 memcpy (pc->caddr.sun_path, client_pathname, sizeof (pc->caddr.sun_path));
179 pc->caddr.sun_family = AF_UNIX;
180 pc->reg.type = PUNT_TYPE_EXCEPTION;
181 pc->reg.punt.exception.reason = reason;
183 vlib_punt_register (pm->hdl,
184 pc->reg.punt.exception.reason, "exception-punt-socket");
189 static clib_error_t *
190 punt_socket_unregister_l4 (ip_address_family_t af,
191 ip_protocol_t protocol, u16 port)
195 udp_unregister_dst_port (vlib_get_main (), port, af == AF_IP4);
197 pci = punt_client_l4_db_remove (af, port);
200 pool_put_index (punt_main.punt_client_pool, pci);
205 static clib_error_t *
206 punt_socket_unregister_exception (vlib_punt_reason_t reason)
210 pci = punt_client_exception_db_remove (reason);
213 pool_put_index (punt_main.punt_client_pool, pci);
219 vnet_punt_socket_add (vlib_main_t * vm, u32 header_version,
220 const punt_reg_t * pr, char *client_pathname)
222 punt_main_t *pm = &punt_main;
224 if (!pm->is_configured)
225 return clib_error_return (0, "socket is not configured");
227 if (header_version != PUNT_PACKETDESC_VERSION)
228 return clib_error_return (0, "Invalid packet descriptor version");
230 /* Register client */
234 return (punt_socket_register_l4 (vm,
236 pr->punt.l4.protocol,
237 pr->punt.l4.port, client_pathname));
238 case PUNT_TYPE_EXCEPTION:
239 return (punt_socket_register_exception (vm,
240 pr->punt.exception.reason,
248 vnet_punt_socket_del (vlib_main_t * vm, const punt_reg_t * pr)
250 punt_main_t *pm = &punt_main;
252 if (!pm->is_configured)
253 return clib_error_return (0, "socket is not configured");
258 return (punt_socket_unregister_l4 (pr->punt.l4.af,
259 pr->punt.l4.protocol,
261 case PUNT_TYPE_EXCEPTION:
262 return (punt_socket_unregister_exception (pr->punt.exception.reason));
269 * @brief Request IP traffic punt to the local TCP/IP stack.
272 * - UDP, TCP and SCTP are the only protocols supported in the current implementation
274 * @param vm vlib_main_t corresponding to the current thread
275 * @param af IP address family.
276 * @param protocol 8-bits L4 protocol value
279 * @param port 16-bits L4 (TCP/IP) port number when applicable (UDP only)
281 * @returns 0 on success, non-zero value otherwise
283 static clib_error_t *
284 punt_l4_add_del (vlib_main_t * vm,
285 ip_address_family_t af,
286 ip_protocol_t protocol, u16 port, bool is_add)
288 /* For now we only support TCP, UDP and SCTP punt */
289 if (protocol != IP_PROTOCOL_UDP &&
290 protocol != IP_PROTOCOL_TCP && protocol != IP_PROTOCOL_SCTP)
291 return clib_error_return (0,
292 "only UDP (%d), TCP (%d) and SCTP (%d) protocols are supported, got %d",
293 IP_PROTOCOL_UDP, IP_PROTOCOL_TCP,
294 IP_PROTOCOL_SCTP, protocol);
296 if (port == (u16) ~ 0)
298 if (protocol == IP_PROTOCOL_UDP)
299 udp_punt_unknown (vm, af == AF_IP4, is_add);
300 else if (protocol == IP_PROTOCOL_TCP)
301 tcp_punt_unknown (vm, af == AF_IP4, is_add);
302 else if (protocol == IP_PROTOCOL_SCTP)
303 sctp_punt_unknown (vm, af == AF_IP4, is_add);
310 if (protocol == IP_PROTOCOL_TCP || protocol == IP_PROTOCOL_SCTP)
311 return clib_error_return (0,
312 "punt TCP/SCTP ports is not supported yet");
314 if (!udp_is_valid_dst_port (port, af == AF_IP4))
315 return clib_error_return (0, "invalid port: %d", port);
317 udp_register_dst_port (vm, port, udp4_punt_node.index, af == AF_IP4);
323 if (protocol == IP_PROTOCOL_TCP || protocol == IP_PROTOCOL_SCTP)
324 return clib_error_return (0,
325 "punt TCP/SCTP ports is not supported yet");
327 udp_unregister_dst_port (vm, port, af == AF_IP4);
333 static clib_error_t *
334 punt_exception_add_del (vlib_main_t * vm,
335 vlib_punt_reason_t reason, bool is_add)
341 vnet_punt_add_del (vlib_main_t * vm, const punt_reg_t * pr, bool is_add)
346 return (punt_l4_add_del (vm, pr->punt.l4.af, pr->punt.l4.protocol,
347 pr->punt.l4.port, is_add));
348 case PUNT_TYPE_EXCEPTION:
349 return (punt_exception_add_del (vm, pr->punt.exception.reason, is_add));
352 return (clib_error_return (0, "Unsupported punt type: %d", pr->type));
355 static clib_error_t *
356 punt_cli (vlib_main_t * vm,
357 unformat_input_t * input, vlib_cli_command_t * cmd)
359 clib_error_t *error = NULL;
369 .type = PUNT_TYPE_L4,
372 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
374 if (unformat (input, "del"))
376 else if (unformat (input, "ipv6"))
377 pr.punt.l4.af = AF_IP6;
378 else if (unformat (input, "ip6"))
379 pr.punt.l4.af = AF_IP6;
380 else if (unformat (input, "%d", &pr.punt.l4.port))
382 else if (unformat (input, "udp"))
383 pr.punt.l4.protocol = IP_PROTOCOL_UDP;
384 else if (unformat (input, "tcp"))
385 pr.punt.l4.protocol = IP_PROTOCOL_TCP;
388 error = clib_error_return (0, "parse error: '%U'",
389 format_unformat_error, input);
394 /* punt both IPv6 and IPv4 when used in CLI */
395 error = vnet_punt_add_del (vm, &pr, is_add);
398 clib_error_report (error);
406 * The set of '<em>set punt</em>' commands allows specific IP traffic to
407 * be punted to the host TCP/IP stack
410 * - UDP is the only protocol supported in the current implementation
411 * - All TCP traffic is currently punted to the host by default
415 * Example of how to request NTP traffic to be punted
416 * @cliexcmd{set punt udp 125}
418 * Example of how to request all 'unknown' UDP traffic to be punted
419 * @cliexcmd{set punt udp all}
421 * Example of how to stop all 'unknown' UDP traffic to be punted
422 * @cliexcmd{set punt udp del all}
426 VLIB_CLI_COMMAND (punt_command, static) = {
428 .short_help = "set punt [udp|tcp] [del] <all | port-num1 [port-num2 ...]>",
429 .function = punt_cli,
433 static clib_error_t *
434 punt_socket_register_cmd (vlib_main_t * vm,
435 unformat_input_t * input, vlib_cli_command_t * cmd)
438 clib_error_t *error = NULL;
448 .type = PUNT_TYPE_L4,
452 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
454 if (unformat (input, "ipv4"))
456 else if (unformat (input, "ipv6"))
457 pr.punt.l4.af = AF_IP6;
458 else if (unformat (input, "udp"))
459 pr.punt.l4.protocol = IP_PROTOCOL_UDP;
460 else if (unformat (input, "tcp"))
461 pr.punt.l4.protocol = IP_PROTOCOL_TCP;
462 else if (unformat (input, "%d", &pr.punt.l4.port))
464 else if (unformat (input, "socket %s", &socket_name))
468 error = clib_error_return (0, "parse error: '%U'",
469 format_unformat_error, input);
474 error = vnet_punt_socket_add (vm, 1, &pr, (char *) socket_name);
483 * @cliexcmd{punt socket register}
486 VLIB_CLI_COMMAND (punt_socket_register_command, static) =
488 .path = "punt socket register",
489 .function = punt_socket_register_cmd,
490 .short_help = "punt socket register [ipv4|ipv6] [udp|tcp]> <all | port-num1 [port-num2 ...]> <socket>",
495 static clib_error_t *
496 punt_socket_deregister_cmd (vlib_main_t * vm,
497 unformat_input_t * input,
498 vlib_cli_command_t * cmd)
500 clib_error_t *error = NULL;
510 .type = PUNT_TYPE_L4,
514 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
516 if (unformat (input, "ipv4"))
518 else if (unformat (input, "ipv6"))
519 pr.punt.l4.af = AF_IP6;
520 else if (unformat (input, "udp"))
521 pr.punt.l4.protocol = IP_PROTOCOL_UDP;
522 else if (unformat (input, "tcp"))
523 pr.punt.l4.protocol = IP_PROTOCOL_TCP;
524 else if (unformat (input, "%d", &pr.punt.l4.port))
528 error = clib_error_return (0, "parse error: '%U'",
529 format_unformat_error, input);
534 error = vnet_punt_socket_del (vm, &pr);
542 * @cliexcmd{punt socket register}
545 VLIB_CLI_COMMAND (punt_socket_deregister_command, static) =
547 .path = "punt socket deregister",
548 .function = punt_socket_deregister_cmd,
549 .short_help = "punt socket deregister [ipv4|ipv6] [udp|tcp]> <all | port-num1 [port-num2 ...]>",
555 punt_client_walk (punt_type_t pt, punt_client_walk_cb_t cb, void *ctx)
557 punt_main_t *pm = &punt_main;
567 hash_foreach(port, pci, pm->db.clients_by_l4_port,
569 cb (pool_elt_at_index(pm->punt_client_pool, pci), ctx);
574 case PUNT_TYPE_EXCEPTION:
578 vec_foreach (pci, pm->db.clients_by_exception)
581 cb (pool_elt_at_index (pm->punt_client_pool, *pci), ctx);
590 format_punt_client (u8 * s, va_list * args)
592 punt_client_t *pc = va_arg (*args, punt_client_t *);
594 s = format (s, " punt ");
596 switch (pc->reg.type)
599 s = format (s, "%U %U port %d",
600 format_ip_address_family, pc->reg.punt.l4.af,
601 format_ip_protocol, pc->reg.punt.l4.protocol,
602 pc->reg.punt.l4.port);
604 case PUNT_TYPE_EXCEPTION:
605 s = format (s, " %U", format_vlib_punt_reason,
606 pc->reg.punt.exception.reason);
610 s = format (s, " to socket %s \n", pc->caddr.sun_path);
616 punt_client_show_one (const punt_client_t * pc, void *ctx)
618 vlib_cli_output (ctx, "%U", format_punt_client, pc);
620 return (WALK_CONTINUE);
623 static clib_error_t *
624 punt_socket_show_cmd (vlib_main_t * vm,
625 unformat_input_t * input, vlib_cli_command_t * cmd)
627 clib_error_t *error = NULL;
632 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
634 if (unformat (input, "exception"))
635 pt = PUNT_TYPE_EXCEPTION;
636 else if (unformat (input, "l4"))
640 error = clib_error_return (0, "parse error: '%U'",
641 format_unformat_error, input);
646 punt_client_walk (pt, punt_client_show_one, vm);
655 * @cliexcmd{show punt socket ipv4}
658 VLIB_CLI_COMMAND (show_punt_socket_registration_command, static) =
660 .path = "show punt socket registrations",
661 .function = punt_socket_show_cmd,
662 .short_help = "show punt socket registrations [l4|exception]",
668 ip_punt_init (vlib_main_t * vm)
670 clib_error_t *error = NULL;
671 punt_main_t *pm = &punt_main;
673 pm->is_configured = false;
674 pm->interface_output_node =
675 vlib_get_node_by_name (vm, (u8 *) "interface-output");
677 if ((error = vlib_call_init_function (vm, punt_init)))
680 pm->hdl = vlib_punt_client_register ("ip-punt");
685 VLIB_INIT_FUNCTION (ip_punt_init);
687 static clib_error_t *
688 punt_config (vlib_main_t * vm, unformat_input_t * input)
690 punt_main_t *pm = &punt_main;
691 char *socket_path = 0;
693 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
695 if (unformat (input, "socket %s", &socket_path))
696 strncpy (pm->sun_path, socket_path, UNIX_PATH_MAX - 1);
698 return clib_error_return (0, "unknown input `%U'",
699 format_unformat_error, input);
702 if (socket_path == 0)
705 /* UNIX domain socket */
706 struct sockaddr_un addr;
707 if ((pm->socket_fd = socket (AF_UNIX, SOCK_DGRAM | SOCK_NONBLOCK, 0)) == -1)
709 return clib_error_return (0, "socket error");
712 clib_memset (&addr, 0, sizeof (addr));
713 addr.sun_family = AF_UNIX;
714 if (*socket_path == '\0')
716 *addr.sun_path = '\0';
717 strncpy (addr.sun_path + 1, socket_path + 1,
718 sizeof (addr.sun_path) - 2);
722 strncpy (addr.sun_path, socket_path, sizeof (addr.sun_path) - 1);
723 unlink (socket_path);
726 if (bind (pm->socket_fd, (struct sockaddr *) &addr, sizeof (addr)) == -1)
728 return clib_error_return (0, "bind error");
731 int n_bytes = 0x10000;
734 (pm->socket_fd, SOL_SOCKET, SO_SNDBUF, &n_bytes,
735 sizeof (n_bytes)) == -1)
737 return clib_error_return (0, "setsockopt error");
740 /* Register socket */
741 clib_file_main_t *fm = &file_main;
742 clib_file_t template = { 0 };
743 template.read_function = punt_socket_read_ready;
744 template.file_descriptor = pm->socket_fd;
745 template.description = format (0, "%s", socket_path);
746 pm->clib_file_index = clib_file_add (fm, &template);
748 pm->is_configured = true;
753 VLIB_CONFIG_FUNCTION (punt_config, "punt");
756 * fd.io coding-style-patch-verification: ON
759 * eval: (c-set-style "gnu")