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;
604 for (i = 0; i < vhost_mem->
nregions; i++)
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 " 658 "used_user_addr 0x%lx", fd, addr.
index,
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);
733 else if (args->
tap_flags & TAP_FLAG_CSUM_OFFLOAD)
738 && (args->
tap_flags & TAP_FLAG_GRO_COALESCE))
754 vif->
flags |= VIRTIO_IF_FLAG_ADMIN_UP;
772 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_3;
780 if (old_netns_fd != -1)
781 close (old_netns_fd);
797 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
801 if ((vif->
type != VIRTIO_IF_TYPE_TAP) && (vif->
type != VIRTIO_IF_TYPE_TUN))
802 return VNET_API_ERROR_INVALID_INTERFACE;
810 if (vif->
type == VIRTIO_IF_TYPE_TAP)
835 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
839 const unsigned int csum_offload_on = TUN_F_CSUM;
840 const unsigned int csum_offload_off = 0;
841 unsigned int offload = enable_disable ? csum_offload_on : csum_offload_off;
843 _IOCTL (vif->
tap_fds[i], TUNSETOFFLOAD, offload);
873 clib_warning (
"Error %s checksum offload on sw_if_index %d",
874 enable_disable ?
"enabling" :
"disabling", sw_if_index);
875 return VNET_API_ERROR_SYSCALL_ERROR_3;
882 int is_packet_coalesce)
894 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
898 const unsigned int gso_on = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
899 const unsigned int gso_off = 0;
900 unsigned int offload = enable_disable ? gso_on : gso_off;
902 _IOCTL (vif->
tap_fds[i], TUNSETOFFLOAD, offload);
912 if (is_packet_coalesce)
931 enable_disable ?
"enabling" :
"disabling", sw_if_index);
932 return VNET_API_ERROR_SYSCALL_ERROR_3;
950 if ((vif->
type != VIRTIO_IF_TYPE_TAP)
951 && (vif->
type != VIRTIO_IF_TYPE_TUN))
994 *out_tapids = r_tapids;
1010 #ifdef TUNSETCARRIER 1020 ret = ioctl (*fd, TUNSETCARRIER, &carrier_up);
1023 clib_warning (
"ioctl (TUNSETCARRIER) returned %d", ret);
1061 int old_netns_fd = -1;
1065 struct ethtool_cmd ecmd;
1072 old_netns_fd = open (
"/proc/self/ns/net", O_RDONLY);
1078 if (setns (nfd, CLONE_NEWNET) == -1)
1085 if ((ctl_fd = socket (AF_INET, SOCK_STREAM, 0)) == -1)
1091 ecmd.cmd = ETHTOOL_GSET;
1095 ifr.ifr_data = (
void *) &ecmd;
1096 if ((ret = ioctl (ctl_fd, SIOCETHTOOL, &ifr)) < 0)
1102 if (ethtool_cmd_speed (&ecmd) != speed)
1104 ecmd.cmd = ETHTOOL_SSET;
1105 ethtool_cmd_speed_set (&ecmd, speed);
1106 if ((ret = ioctl (ctl_fd, SIOCETHTOOL, &ifr)) < 0)
1114 if (old_netns_fd != -1)
1116 if (setns (old_netns_fd, CLONE_NEWNET) == -1)
1120 close (old_netns_fd);
u32 per_interface_next_index
vlib_log_class_t vlib_log_register_class(char *class, char *subclass)
clib_error_t * vnet_netlink_set_link_addr(int ifindex, u8 *mac)
vlib_node_registration_t virtio_input_node
(constructor) VLIB_REGISTER_NODE (virtio_input_node)
#define vec_foreach_index(var, v)
Iterate over vector indices.
void virtio_set_net_hdr_size(virtio_if_t *vif)
static u32 virtio_eth_flag_change(vnet_main_t *vnm, vnet_hw_interface_t *hi, u32 flags)
static void tap_free(vlib_main_t *vm, virtio_if_t *vif)
clib_error_t * vnet_netlink_set_link_state(int ifindex, int up)
VNET_HW_INTERFACE_CLASS(tun_device_hw_interface_class, static)
vlib_log_class_t log_default
ip4_address_t host_ip4_addr
vl_api_wireguard_peer_flags_t flags
clib_error_t * vnet_sw_interface_set_flags(vnet_main_t *vnm, u32 sw_if_index, vnet_sw_interface_flags_t flags)
ip4_address_t host_ip4_addr
void ethernet_delete_interface(vnet_main_t *vnm, u32 hw_if_index)
vnet_main_t * vnet_get_main(void)
#define pool_get_zero(P, E)
Allocate an object E from a pool P and zero it.
#define pool_foreach(VAR, POOL)
Iterate through pool.
vlib_physmem_main_t physmem_main
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
static vnet_hw_interface_t * vnet_get_hw_interface(vnet_main_t *vnm, u32 hw_if_index)
vnet_device_class_t virtio_device_class
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
void virtio_vring_set_numa_node(vlib_main_t *vm, virtio_if_t *vif, u32 idx)
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. ...
u32 vnet_register_interface(vnet_main_t *vnm, u32 dev_class_index, u32 dev_instance, u32 hw_class_index, u32 hw_instance)
#define tap_log_dbg(dev, f,...)
int tap_csum_offload_enable_disable(vlib_main_t *vm, u32 sw_if_index, int enable_disable)
#define VHOST_SET_MEM_TABLE
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 * vnet_netlink_set_link_name(int ifindex, char *new_ifname)
int tap_dump_ifs(tap_interface_details_t **out_tapids)
clib_error_t * virtio_vring_free_tx(vlib_main_t *vm, virtio_if_t *vif, u32 idx)
#define VIRTIO_FEATURE(X)
static vnet_sw_interface_t * vnet_get_hw_sw_interface(vnet_main_t *vnm, u32 hw_if_index)
#define clib_memcpy(d, s, n)
clib_error_t * vnet_netlink_set_link_netns(int ifindex, int netns_fd, char *new_ifname)
#define TX_QUEUE_ACCESS(X)
#define VLIB_INIT_FUNCTION(x)
description fragment has unexpected format
vnet_hw_interface_flags_t flags
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
int tap_delete_if(vlib_main_t *vm, u32 sw_if_index)
#define clib_error_return(e, args...)
#define VHOST_SET_VRING_ADDR
#define vlib_log_debug(...)
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
mac_address_t host_mac_addr
#define clib_error_return_unix(e, args...)
int vnet_hw_interface_set_rx_mode(vnet_main_t *vnm, u32 hw_if_index, u16 queue_id, vnet_hw_if_rx_mode mode)
#define pool_put(P, E)
Free an object E in pool P.
#define VLIB_CONFIG_FUNCTION(x, n,...)
ip4_address_t host_ip4_gw
ip6_address_t host_ip6_addr
#define virtio_log_debug(vif, f,...)
clib_error_t * vnet_netlink_add_ip6_route(void *dst, u8 dst_len, void *gw)
ip4_address_t host_ip4_addr
virtio_vring_t * rxq_vrings
sll srl srl sll sra u16x4 i
static int open_netns_fd(char *netns)
#define vec_free(V)
Free vector's memory (no header).
ip6_address_t host_ip6_addr
static vnet_hw_interface_t * vnet_get_sup_hw_interface_api_visible_or_null(vnet_main_t *vnm, u32 sw_if_index)
#define clib_warning(format, args...)
#define VHOST_NET_SET_BACKEND
static uword clib_bitmap_get(uword *ai, uword i)
Gets the ith bit value from a bitmap.
#define VHOST_SET_VRING_CALL
#define VHOST_GET_FEATURES
clib_error_t * vnet_netlink_set_link_mtu(int ifindex, int mtu)
void vnet_hw_interface_assign_rx_thread(vnet_main_t *vnm, u32 hw_if_index, u16 queue_id, uword thread_index)
clib_error_t * vnet_netlink_add_ip4_route(void *dst, u8 dst_len, void *gw)
ip6_address_t host_ip6_gw
clib_error_t * virtio_vring_init(vlib_main_t *vm, virtio_if_t *vif, u16 idx, u16 sz)
void vnet_delete_hw_interface(vnet_main_t *vnm, u32 hw_if_index)
static void clib_mem_free(void *p)
void virtio_set_packet_coalesce(virtio_if_t *vif)
unsigned int if_nametoindex(const char *ifname)
static void * clib_mem_alloc(uword size)
static uword pointer_to_uword(const void *p)
virtio_main_t virtio_main
clib_error_t * vnet_netlink_add_ip6_addr(int ifindex, void *addr, int pfx_len)
void tap_create_if(vlib_main_t *vm, tap_create_if_args_t *args)
#define RX_QUEUE_ACCESS(X)
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
#define tap_log_err(dev, f,...)
clib_error_t * vnet_netlink_add_ip4_addr(int ifindex, void *addr, int pfx_len)
static void ethernet_mac_address_generate(u8 *mac)
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)
mac_address_t host_mac_addr
int tap_gso_enable_disable(vlib_main_t *vm, u32 sw_if_index, int enable_disable, int is_packet_coalesce)
int tap_set_speed(u32 hw_if_index, u32 speed)
a point 2 point interface
#define clib_error_free(e)
ip6_address_t host_ip6_addr
int vnet_hw_interface_unassign_rx_thread(vnet_main_t *vnm, u32 hw_if_index, u16 queue_id)
int tap_set_carrier(u32 hw_if_index, u32 carrier_up)
virtio_vring_t * cxq_vring
#define VHOST_SET_VRING_NUM
static int ethernet_mac_address_is_zero(const u8 *mac)
clib_error_t * virtio_vring_free_rx(vlib_main_t *vm, virtio_if_t *vif, u32 idx)
TAP interface details struct.
static vlib_thread_main_t * vlib_get_thread_main()
vl_api_dhcp_client_state_t state
#define vec_foreach(var, vec)
Vector iterator.
static clib_error_t * tap_mtu_config(vlib_main_t *vm, unformat_input_t *input)
vhost_memory_region_t regions[0]
#define VHOST_SET_VRING_KICK
#define VHOST_SET_FEATURES
static uword clib_bitmap_first_clear(uword *ai)
Return the lowest numbered clear bit in a bitmap.
__clib_export u8 * format_clib_error(u8 *s, va_list *va)
static void vnet_hw_interface_set_input_node(vnet_main_t *vnm, u32 hw_if_index, u32 node_index)
vl_api_interface_index_t sw_if_index
clib_error_t * vnet_netlink_set_link_master(int ifindex, char *master_ifname)
virtio_vring_t * txq_vrings
static clib_error_t * tap_init(vlib_main_t *vm)