19 #include <sys/types.h> 23 #include <linux/if_tun.h> 24 #include <sys/ioctl.h> 25 #include <sys/eventfd.h> 26 #include <net/if_arp.h> 30 #include <linux/netlink.h> 31 #include <linux/rtnetlink.h> 45 #define tap_log_err(dev, f, ...) \ 46 vlib_log (VLIB_LOG_LEVEL_ERR, tap_main.log_default, "tap%u: " f, dev->dev_instance, ## __VA_ARGS__) 47 #define tap_log_dbg(dev, f, ...) \ 48 vlib_log (VLIB_LOG_LEVEL_DEBUG, tap_main.log_default, "tap%u: " f, dev->dev_instance, ## __VA_ARGS__) 50 #define _IOCTL(fd,a,...) \ 51 if (ioctl (fd, a, __VA_ARGS__) < 0) \ 53 err = clib_error_return_unix (0, "ioctl(" #a ")"); \ 54 tap_log_err (vif, "%U", format_clib_error, err); \ 81 if (strncmp (netns,
"pid:", 4) == 0)
82 s =
format (0,
"/proc/%u/ns/net%c", atoi (netns + 4), 0);
83 else if (netns[0] ==
'/')
84 s =
format (0,
"%s%c", netns, 0);
86 s =
format (0,
"/var/run/netns/%s%c", netns, 0);
88 fd = open ((
char *) s, O_RDONLY);
93 #define TAP_MAX_INSTANCE 1024 112 _IOCTL (vif->
tap_fds[0], TUNSETPERSIST, (
void *) (uintptr_t) 0);
140 int i, num_vhost_queues;
141 int old_netns_fd = -1;
142 struct ifreq ifr = {.ifr_flags = IFF_NO_PI | IFF_VNET_HDR };
143 struct ifreq get_ifr = {.ifr_flags = 0 };
148 unsigned int tap_features;
149 int tfd = -1, qfd = -1, vfd = -1, nfd = -1;
150 char *host_if_name = 0;
151 unsigned int offload = 0;
158 args->
rv = VNET_API_ERROR_INVALID_INTERFACE;
170 args->
rv = VNET_API_ERROR_UNSPECIFIED;
179 vif->
type = VIRTIO_IF_TYPE_TUN;
180 ifr.ifr_flags |= IFF_TUN;
190 sndbuf = INT_MAX - 1;
194 vif->
type = VIRTIO_IF_TYPE_TAP;
195 ifr.ifr_flags |= IFF_TAP;
210 clib_min (IFNAMSIZ, strlen (host_if_name)));
214 args->
rv = VNET_API_ERROR_NO_MATCHING_INTERFACE;
220 old_netns_fd = open (
"/proc/self/ns/net", O_RDONLY);
223 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_2;
228 if (setns (nfd, CLONE_NEWNET) == -1)
230 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_3;
238 if ((tfd = open (
"/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
240 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_2;
247 _IOCTL (tfd, TUNGETFEATURES, &tap_features);
248 tap_log_dbg (vif,
"TUNGETFEATURES: features 0x%lx", tap_features);
249 if ((tap_features & IFF_VNET_HDR) == 0)
251 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_2;
256 if ((tap_features & IFF_MULTI_QUEUE) == 0)
260 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_2;
267 ifr.ifr_flags |= IFF_MULTI_QUEUE;
269 hdrsz =
sizeof (virtio_net_hdr_v1_t);
272 offload = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
275 else if (args->
tap_flags & TAP_FLAG_CSUM_OFFLOAD)
277 offload = TUN_F_CSUM;
281 _IOCTL (tfd, TUNSETIFF, (
void *) &ifr);
282 tap_log_dbg (vif,
"TUNSETIFF fd %d name %s flags 0x%x", tfd,
283 ifr.ifr_ifrn.ifrn_name, ifr.ifr_flags);
289 host_if_name = ifr.ifr_ifrn.ifrn_name;
299 _IOCTL (tfd, TUNSETPERSIST, (
void *) (uintptr_t) 0);
306 _IOCTL (tfd, TUNSETPERSIST, (
void *) (uintptr_t) 1);
310 _IOCTL (tfd, TUNGETIFF, (
void *) &get_ifr);
311 tap_log_dbg (vif,
"TUNGETIFF: flags 0x%lx", get_ifr.ifr_flags);
312 if ((get_ifr.ifr_flags & IFF_PERSIST) == 0)
314 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_2;
325 if ((qfd = open (
"/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
327 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_2;
331 _IOCTL (qfd, TUNSETIFF, (
void *) &ifr);
332 tap_log_dbg (vif,
"TUNSETIFF fd %d name %s flags 0x%x", qfd,
333 ifr.ifr_ifrn.ifrn_name, ifr.ifr_flags);
339 tap_log_dbg (vif,
"TUNSETVNETHDRSZ: fd %d vnet_hdr_sz %u",
341 _IOCTL (vif->
tap_fds[i], TUNSETVNETHDRSZ, &hdrsz);
345 _IOCTL (vif->
tap_fds[i], TUNSETSNDBUF, &sndbuf);
349 _IOCTL (vif->
tap_fds[i], TUNSETOFFLOAD, offload);
351 if (fcntl (vif->
tap_fds[i], F_SETFL, O_NONBLOCK) < 0)
361 for (i = 0; i < num_vhost_queues; i++)
363 if ((vfd = open (
"/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0)
365 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_1;
381 args->
rv = VNET_API_ERROR_UNSUPPORTED;
383 "VIRTIO_NET_F_MRG_RXBUF feature");
390 args->
rv = VNET_API_ERROR_UNSUPPORTED;
392 "VIRTIO_RING_F_INDIRECT_DESC feature");
398 args->
rv = VNET_API_ERROR_UNSUPPORTED;
400 "VIRTIO_F_VERSION_1 features");
410 if (!(args->
tap_flags & TAP_FLAG_ATTACH))
416 old_netns_fd = open (
"/proc/self/ns/net", O_RDONLY);
419 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_2;
428 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
431 if (setns (nfd, CLONE_NEWNET) == -1)
433 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_3;
440 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_3;
446 else if (host_if_name)
452 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
458 if (vif->
type == VIRTIO_IF_TYPE_TAP)
466 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
477 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
490 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
502 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
510 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
519 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
529 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
540 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
550 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
560 if (setns (old_netns_fd, CLONE_NEWNET) == -1)
562 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_2;
569 for (i = 0; i < num_vhost_queues; i++)
571 if (i < vif->num_rxqs && (args->
error =
575 args->
rv = VNET_API_ERROR_INIT_FAILED;
579 if (i < vif->num_txqs && (args->
error =
583 args->
rv = VNET_API_ERROR_INIT_FAILED;
598 for (i = 0; i < vhost_mem->
nregions; i++)
600 "guest_phys_addr 0x%lx userspace_addr 0x%lx", i,
606 for (i = 0; i < num_vhost_queues; i++)
617 for (i = 0; i < num_vhost_queues * 2; i++)
651 "desc_user_addr 0x%lx avail_user_addr 0x%lx " 652 "used_user_addr 0x%lx", fd, addr.
index,
673 if (vif->
type == VIRTIO_IF_TYPE_TAP)
693 if (vif->
type != VIRTIO_IF_TYPE_TUN)
702 args->
rv = VNET_API_ERROR_INVALID_REGISTRATION;
712 tun_device_hw_interface_class.index, vif->
dev_instance);
727 else if (args->
tap_flags & TAP_FLAG_CSUM_OFFLOAD)
732 && (args->
tap_flags & TAP_FLAG_GRO_COALESCE))
748 vif->
flags |= VIRTIO_IF_FLAG_ADMIN_UP;
760 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_3;
768 if (old_netns_fd != -1)
769 close (old_netns_fd);
785 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
789 if ((vif->
type != VIRTIO_IF_TYPE_TAP) && (vif->
type != VIRTIO_IF_TYPE_TUN))
790 return VNET_API_ERROR_INVALID_INTERFACE;
798 if (vif->
type == VIRTIO_IF_TYPE_TAP)
823 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
827 const unsigned int csum_offload_on = TUN_F_CSUM;
828 const unsigned int csum_offload_off = 0;
829 unsigned int offload = enable_disable ? csum_offload_on : csum_offload_off;
831 _IOCTL (vif->
tap_fds[i], TUNSETOFFLOAD, offload);
861 clib_warning (
"Error %s checksum offload on sw_if_index %d",
862 enable_disable ?
"enabling" :
"disabling", sw_if_index);
863 return VNET_API_ERROR_SYSCALL_ERROR_3;
870 int is_packet_coalesce)
882 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
886 const unsigned int gso_on = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
887 const unsigned int gso_off = 0;
888 unsigned int offload = enable_disable ? gso_on : gso_off;
890 _IOCTL (vif->
tap_fds[i], TUNSETOFFLOAD, offload);
900 if (is_packet_coalesce)
919 enable_disable ?
"enabling" :
"disabling", sw_if_index);
920 return VNET_API_ERROR_SYSCALL_ERROR_3;
938 if ((vif->
type != VIRTIO_IF_TYPE_TAP)
939 && (vif->
type != VIRTIO_IF_TYPE_TUN))
964 strlen ((
const char *) vif->
net_ns)));
982 *out_tapids = r_tapids;
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.
u8 * format_clib_error(u8 *s, va_list *va)
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
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.
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. ...
#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_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 pool_foreach(VAR, POOL, BODY)
Iterate through pool.
#define TX_QUEUE_ACCESS(X)
#define VLIB_INIT_FUNCTION(x)
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
u32 vnet_register_interface(vnet_main_t *vnm, u32 dev_class_index, u32 dev_instance, u32 hw_class_index, u32 hw_instance)
#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...)
#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)
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
void vnet_delete_hw_interface(vnet_main_t *vnm, u32 hw_if_index)
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)
clib_error_t * vnet_hw_interface_set_flags(vnet_main_t *vnm, u32 hw_if_index, vnet_hw_interface_flags_t flags)
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)
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)
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.
clib_error_t * vnet_sw_interface_set_flags(vnet_main_t *vnm, u32 sw_if_index, vnet_sw_interface_flags_t flags)
static vlib_thread_main_t * vlib_get_thread_main()
vl_api_dhcp_client_state_t state
static clib_error_t * tap_mtu_config(vlib_main_t *vm, unformat_input_t *input)
vhost_memory_region_t regions[0]
int vnet_hw_interface_set_rx_mode(vnet_main_t *vnm, u32 hw_if_index, u16 queue_id, vnet_hw_interface_rx_mode mode)
#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.
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)