|
FD.io VPP
v21.06-3-gbb25fbf28
Vector Packet Processing
|
Go to the documentation of this file.
19 #include <sys/types.h>
21 #include <sys/socket.h>
24 #include <linux/if_tun.h>
25 #include <sys/ioctl.h>
26 #include <linux/ethtool.h>
27 #include <linux/sockios.h>
28 #include <sys/eventfd.h>
29 #include <net/if_arp.h>
33 #include <linux/netlink.h>
34 #include <linux/rtnetlink.h>
48 #define tap_log_err(dev, f, ...) \
49 vlib_log (VLIB_LOG_LEVEL_ERR, tap_main.log_default, "tap%u: " f, dev->dev_instance, ## __VA_ARGS__)
50 #define tap_log_dbg(dev, f, ...) \
51 vlib_log (VLIB_LOG_LEVEL_DEBUG, tap_main.log_default, "tap%u: " f, dev->dev_instance, ## __VA_ARGS__)
53 #define _IOCTL(fd,a,...) \
54 if (ioctl (fd, a, __VA_ARGS__) < 0) \
56 err = clib_error_return_unix (0, "ioctl(" #a ")"); \
57 tap_log_err (vif, "%U", format_clib_error, err); \
84 if (strncmp (netns,
"pid:", 4) == 0)
85 s =
format (0,
"/proc/%u/ns/net%c", atoi (netns + 4), 0);
86 else if (netns[0] ==
'/')
87 s =
format (0,
"%s%c", netns, 0);
89 s =
format (0,
"/var/run/netns/%s%c", netns, 0);
91 fd = open ((
char *) s, O_RDONLY);
96 #define TAP_MAX_INSTANCE 1024
117 _IOCTL (vif->
tap_fds[0], TUNSETPERSIST, (
void *) (uintptr_t) 0);
146 int i, num_vhost_queues;
147 int old_netns_fd = -1;
148 struct ifreq ifr = {.ifr_flags = IFF_NO_PI | IFF_VNET_HDR };
149 struct ifreq get_ifr = {.ifr_flags = 0 };
154 unsigned int tap_features;
155 int tfd = -1, qfd = -1, vfd = -1, nfd = -1;
156 char *host_if_name = 0;
157 unsigned int offload = 0;
164 args->
rv = VNET_API_ERROR_INVALID_INTERFACE;
176 args->
rv = VNET_API_ERROR_UNSPECIFIED;
185 vif->
type = VIRTIO_IF_TYPE_TUN;
186 ifr.ifr_flags |= IFF_TUN;
196 sndbuf = INT_MAX - 1;
200 vif->
type = VIRTIO_IF_TYPE_TAP;
201 ifr.ifr_flags |= IFF_TAP;
220 args->
rv = VNET_API_ERROR_NO_MATCHING_INTERFACE;
226 old_netns_fd = open (
"/proc/self/ns/net", O_RDONLY);
229 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_2;
234 if (setns (nfd, CLONE_NEWNET) == -1)
236 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_3;
244 if ((tfd = open (
"/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
246 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_2;
253 _IOCTL (tfd, TUNGETFEATURES, &tap_features);
254 tap_log_dbg (vif,
"TUNGETFEATURES: features 0x%lx", tap_features);
255 if ((tap_features & IFF_VNET_HDR) == 0)
257 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_2;
262 if ((tap_features & IFF_MULTI_QUEUE) == 0)
266 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_2;
273 ifr.ifr_flags |= IFF_MULTI_QUEUE;
275 hdrsz =
sizeof (virtio_net_hdr_v1_t);
278 offload = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
281 else if (args->
tap_flags & TAP_FLAG_CSUM_OFFLOAD)
283 offload = TUN_F_CSUM;
287 _IOCTL (tfd, TUNSETIFF, (
void *) &ifr);
288 tap_log_dbg (vif,
"TUNSETIFF fd %d name %s flags 0x%x", tfd,
289 ifr.ifr_ifrn.ifrn_name, ifr.ifr_flags);
295 host_if_name = ifr.ifr_ifrn.ifrn_name;
305 _IOCTL (tfd, TUNSETPERSIST, (
void *) (uintptr_t) 0);
312 _IOCTL (tfd, TUNSETPERSIST, (
void *) (uintptr_t) 1);
316 _IOCTL (tfd, TUNGETIFF, (
void *) &get_ifr);
317 tap_log_dbg (vif,
"TUNGETIFF: flags 0x%lx", get_ifr.ifr_flags);
318 if ((get_ifr.ifr_flags & IFF_PERSIST) == 0)
320 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_2;
331 if ((qfd = open (
"/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
333 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_2;
337 _IOCTL (qfd, TUNSETIFF, (
void *) &ifr);
338 tap_log_dbg (vif,
"TUNSETIFF fd %d name %s flags 0x%x", qfd,
339 ifr.ifr_ifrn.ifrn_name, ifr.ifr_flags);
345 tap_log_dbg (vif,
"TUNSETVNETHDRSZ: fd %d vnet_hdr_sz %u",
347 _IOCTL (vif->
tap_fds[
i], TUNSETVNETHDRSZ, &hdrsz);
351 _IOCTL (vif->
tap_fds[
i], TUNSETSNDBUF, &sndbuf);
355 _IOCTL (vif->
tap_fds[
i], TUNSETOFFLOAD, offload);
357 if (fcntl (vif->
tap_fds[
i], F_SETFL, O_NONBLOCK) < 0)
367 for (
i = 0;
i < num_vhost_queues;
i++)
369 if ((vfd = open (
"/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0)
371 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_1;
387 args->
rv = VNET_API_ERROR_UNSUPPORTED;
389 "VIRTIO_NET_F_MRG_RXBUF feature");
396 args->
rv = VNET_API_ERROR_UNSUPPORTED;
398 "VIRTIO_RING_F_INDIRECT_DESC feature");
404 args->
rv = VNET_API_ERROR_UNSUPPORTED;
406 "VIRTIO_F_VERSION_1 features");
416 if (!(args->
tap_flags & TAP_FLAG_ATTACH))
422 old_netns_fd = open (
"/proc/self/ns/net", O_RDONLY);
425 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_2;
434 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
437 if (setns (nfd, CLONE_NEWNET) == -1)
439 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_3;
446 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_3;
452 else if (host_if_name)
458 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
464 if (vif->
type == VIRTIO_IF_TYPE_TAP)
472 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
483 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
496 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
508 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
516 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
525 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
535 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
546 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
556 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
566 if (setns (old_netns_fd, CLONE_NEWNET) == -1)
568 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_2;
575 for (
i = 0;
i < num_vhost_queues;
i++)
577 if (i < vif->num_rxqs && (args->
error =
581 args->
rv = VNET_API_ERROR_INIT_FAILED;
585 if (i < vif->num_txqs && (args->
error =
589 args->
rv = VNET_API_ERROR_INIT_FAILED;
606 "guest_phys_addr 0x%lx userspace_addr 0x%lx",
i,
612 for (
i = 0;
i < num_vhost_queues;
i++)
623 for (
i = 0;
i < num_vhost_queues * 2;
i++)
657 "desc_user_addr 0x%lx avail_user_addr 0x%lx "
679 if (vif->
type == VIRTIO_IF_TYPE_TAP)
699 if (vif->
type != VIRTIO_IF_TYPE_TUN)
708 args->
rv = VNET_API_ERROR_INVALID_REGISTRATION;
718 tun_device_hw_interface_class.index, vif->
dev_instance);
734 else if (args->
tap_flags & TAP_FLAG_CSUM_OFFLOAD)
740 && (args->
tap_flags & TAP_FLAG_GRO_COALESCE))
748 vif->
flags |= VIRTIO_IF_FLAG_ADMIN_UP;
766 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_3;
774 if (old_netns_fd != -1)
775 close (old_netns_fd);
790 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
794 if ((vif->
type != VIRTIO_IF_TYPE_TAP) && (vif->
type != VIRTIO_IF_TYPE_TUN))
795 return VNET_API_ERROR_INVALID_INTERFACE;
801 if (vif->
type == VIRTIO_IF_TYPE_TAP)
826 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
830 const unsigned int csum_offload_on = TUN_F_CSUM;
831 const unsigned int csum_offload_off = 0;
832 unsigned int offload = enable_disable ? csum_offload_on : csum_offload_off;
834 _IOCTL (vif->
tap_fds[
i], TUNSETOFFLOAD, offload);
856 clib_warning (
"Error %s checksum offload on sw_if_index %d",
857 enable_disable ?
"enabling" :
"disabling",
sw_if_index);
858 return VNET_API_ERROR_SYSCALL_ERROR_3;
865 int is_packet_coalesce)
877 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
881 const unsigned int gso_on = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
882 const unsigned int gso_off = 0;
883 unsigned int offload = enable_disable ? gso_on : gso_off;
885 _IOCTL (vif->
tap_fds[
i], TUNSETOFFLOAD, offload);
895 if (is_packet_coalesce)
914 enable_disable ?
"enabling" :
"disabling",
sw_if_index);
915 return VNET_API_ERROR_SYSCALL_ERROR_3;
933 if ((vif->
type != VIRTIO_IF_TYPE_TAP)
934 && (vif->
type != VIRTIO_IF_TYPE_TUN))
977 *out_tapids = r_tapids;
1003 ret = ioctl (*fd, TUNSETCARRIER, &carrier_up);
1006 clib_warning (
"ioctl (TUNSETCARRIER) returned %d", ret);
1044 int old_netns_fd = -1;
1048 struct ethtool_cmd ecmd;
1055 old_netns_fd = open (
"/proc/self/ns/net", O_RDONLY);
1061 if (setns (nfd, CLONE_NEWNET) == -1)
1068 if ((ctl_fd = socket (AF_INET, SOCK_STREAM, 0)) == -1)
1074 ecmd.cmd = ETHTOOL_GSET;
1078 ifr.ifr_data = (
void *) &ecmd;
1079 if ((ret = ioctl (ctl_fd, SIOCETHTOOL, &ifr)) < 0)
1085 if (ethtool_cmd_speed (&ecmd) != speed)
1087 ecmd.cmd = ETHTOOL_SSET;
1088 ethtool_cmd_speed_set (&ecmd, speed);
1089 if ((ret = ioctl (ctl_fd, SIOCETHTOOL, &ifr)) < 0)
1097 if (old_netns_fd != -1)
1099 if (setns (old_netns_fd, CLONE_NEWNET) == -1)
1103 close (old_netns_fd);
ip6_address_t host_ip6_addr
vnet_device_class_t virtio_device_class
ip4_address_t host_ip4_gw
#define VHOST_SET_VRING_KICK
clib_error_t * vnet_netlink_add_ip4_route(void *dst, u8 dst_len, void *gw)
clib_error_t * virtio_vring_free_rx(vlib_main_t *vm, virtio_if_t *vif, u32 idx)
clib_error_t * vnet_sw_interface_set_flags(vnet_main_t *vnm, u32 sw_if_index, vnet_sw_interface_flags_t flags)
static clib_error_t * tap_init(vlib_main_t *vm)
static clib_error_t * tap_mtu_config(vlib_main_t *vm, unformat_input_t *input)
int tap_gso_enable_disable(vlib_main_t *vm, u32 sw_if_index, int enable_disable, int is_packet_coalesce)
ip4_address_t host_ip4_addr
#define VNET_HW_INTERFACE_CAP_SUPPORTS_L4_TX_CKSUM
clib_error_t * virtio_vring_free_tx(vlib_main_t *vm, virtio_if_t *vif, u32 idx)
vnet_hw_interface_capabilities_t caps
#define TX_QUEUE_ACCESS(X)
#define clib_memcpy(d, s, n)
void tap_create_if(vlib_main_t *vm, tap_create_if_args_t *args)
static uword pointer_to_uword(const void *p)
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
clib_error_t * vnet_netlink_set_link_netns(int ifindex, int netns_fd, char *new_ifname)
#define VHOST_SET_VRING_ADDR
vlib_log_class_t vlib_log_register_class(char *class, char *subclass)
static void clib_mem_free(void *p)
#define clib_error_return(e, args...)
unsigned int if_nametoindex(const char *ifname)
u32 per_interface_next_index
#define pool_put(P, E)
Free an object E in pool P.
VNET_HW_INTERFACE_CLASS(tun_device_hw_interface_class, static)
static int ethernet_mac_address_is_zero(const u8 *mac)
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
clib_error_t * vnet_netlink_add_ip4_addr(int ifindex, void *addr, int pfx_len)
@ VNET_HW_INTERFACE_FLAG_LINK_UP
#define VIRTIO_FEATURE(X)
vl_api_dhcp_client_state_t state
clib_error_t * vnet_netlink_set_link_addr(int ifindex, u8 *mac)
ip6_address_t host_ip6_gw
static uword clib_bitmap_first_clear(uword *ai)
Return the lowest numbered clear bit in a bitmap.
clib_error_t * virtio_vring_init(vlib_main_t *vm, virtio_if_t *vif, u16 idx, u16 sz)
mac_address_t host_mac_addr
__clib_export u8 * format_clib_error(u8 *s, va_list *va)
#define VHOST_SET_VRING_NUM
@ VNET_HW_INTERFACE_CAP_SUPPORTS_TX_TCP_CKSUM
#define VLIB_CONFIG_FUNCTION(x, n,...)
ip6_address_t host_ip6_addr
virtio_main_t virtio_main
void virtio_set_packet_coalesce(virtio_if_t *vif)
#define pool_foreach(VAR, POOL)
Iterate through pool.
int tap_csum_offload_enable_disable(vlib_main_t *vm, u32 sw_if_index, int enable_disable)
ip4_address_t host_ip4_addr
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
virtio_vring_t * txq_vrings
static uword clib_bitmap_get(uword *ai, uword i)
Gets the ith bit value from a bitmap.
vlib_physmem_main_t physmem_main
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
ip6_address_t host_ip6_addr
#define VHOST_SET_VRING_CALL
clib_error_t * vnet_netlink_set_link_mtu(int ifindex, int mtu)
clib_error_t * vnet_netlink_set_link_state(int ifindex, int up)
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
static vnet_hw_interface_t * vnet_get_hw_interface(vnet_main_t *vnm, u32 hw_if_index)
vnet_main_t * vnet_get_main(void)
@ VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO
vlib_log_class_t log_default
#define tap_log_dbg(dev, f,...)
#define VHOST_SET_MEM_TABLE
static vnet_hw_interface_t * vnet_get_sup_hw_interface_api_visible_or_null(vnet_main_t *vnm, u32 sw_if_index)
#define vec_foreach_index(var, v)
Iterate over vector indices.
void vnet_delete_hw_interface(vnet_main_t *vnm, u32 hw_if_index)
if(node->flags &VLIB_NODE_FLAG_TRACE) vnet_interface_output_trace(vm
#define VHOST_SET_FEATURES
ip4_address_t host_ip4_addr
int tap_delete_if(vlib_main_t *vm, u32 sw_if_index)
void virtio_set_net_hdr_size(virtio_if_t *vif)
sll srl srl sll sra u16x4 i
#define vlib_log_debug(...)
#define VHOST_NET_SET_BACKEND
static uword * clib_bitmap_set(uword *ai, uword i, uword value)
Sets the ith bit of a bitmap to new_value Removes trailing zeros from the bitmap.
clib_error_t * vnet_netlink_set_link_name(int ifindex, char *new_ifname)
#define VHOST_GET_FEATURES
#define vec_free(V)
Free vector's memory (no header).
clib_error_t * vnet_netlink_add_ip6_addr(int ifindex, void *addr, int pfx_len)
static void ethernet_mac_address_generate(u8 *mac)
#define virtio_log_debug(vif, f,...)
virtio_vring_t * cxq_vring
#define RX_QUEUE_ACCESS(X)
description fragment has unexpected format
static vnet_sw_interface_t * vnet_get_hw_sw_interface(vnet_main_t *vnm, u32 hw_if_index)
#define VLIB_INIT_FUNCTION(x)
void ethernet_delete_interface(vnet_main_t *vnm, u32 hw_if_index)
TAP interface details struct.
int tap_set_carrier(u32 hw_if_index, u32 carrier_up)
static void tap_free(vlib_main_t *vm, virtio_if_t *vif)
#define vec_foreach(var, vec)
Vector iterator.
static int open_netns_fd(char *netns)
#define clib_error_return_unix(e, args...)
#define tap_log_err(dev, f,...)
virtio_vring_t * rxq_vrings
clib_error_t * vnet_netlink_set_link_master(int ifindex, char *master_ifname)
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
#define pool_get_zero(P, E)
Allocate an object E from a pool P and zero it.
clib_error_t * vnet_hw_interface_set_flags(vnet_main_t *vnm, u32 hw_if_index, vnet_hw_interface_flags_t flags)
clib_error_t *() vlib_init_function_t(struct vlib_main_t *vm)
@ VNET_HW_INTERFACE_CAP_SUPPORTS_TX_UDP_CKSUM
@ VNET_HW_INTERFACE_CLASS_FLAG_P2P
a point 2 point interface
#define clib_warning(format, args...)
@ VNET_HW_INTERFACE_CAP_SUPPORTS_INT_MODE
#define clib_error_free(e)
static u32 virtio_eth_flag_change(vnet_main_t *vnm, vnet_hw_interface_t *hi, u32 flags)
int tap_dump_ifs(tap_interface_details_t **out_tapids)
void virtio_vring_set_rx_queues(vlib_main_t *vm, virtio_if_t *vif)
static vlib_thread_main_t * vlib_get_thread_main()
vl_api_interface_index_t sw_if_index
vhost_memory_region_t regions[0]
static void * clib_mem_alloc(uword size)
u32 vnet_register_interface(vnet_main_t *vnm, u32 dev_class_index, u32 dev_instance, u32 hw_class_index, u32 hw_instance)
clib_error_t * ethernet_register_interface(vnet_main_t *vnm, u32 dev_class_index, u32 dev_instance, const u8 *address, u32 *hw_if_index_return, ethernet_flag_change_function_t flag_change)
clib_error_t * vnet_netlink_add_ip6_route(void *dst, u8 dst_len, void *gw)
mac_address_t host_mac_addr
int tap_set_speed(u32 hw_if_index, u32 speed)
vl_api_wireguard_peer_flags_t flags