19 #include <sys/types.h> 23 #include <linux/if_tun.h> 24 #include <sys/ioctl.h> 25 #include <linux/virtio_net.h> 26 #include <linux/vhost.h> 27 #include <sys/eventfd.h> 28 #include <net/if_arp.h> 32 #include <linux/netlink.h> 33 #include <linux/rtnetlink.h> 47 #define tap_log_err(dev, f, ...) \ 48 vlib_log (VLIB_LOG_LEVEL_ERR, tap_main.log_default, "tap%u: " f, dev->dev_instance, ## __VA_ARGS__) 49 #define tap_log_dbg(dev, f, ...) \ 50 vlib_log (VLIB_LOG_LEVEL_DEBUG, tap_main.log_default, "tap%u: " f, dev->dev_instance, ## __VA_ARGS__) 52 #define _IOCTL(fd,a,...) \ 53 if (ioctl (fd, a, __VA_ARGS__) < 0) \ 55 err = clib_error_return_unix (0, "ioctl(" #a ")"); \ 56 tap_log_err (vif, "%U", format_clib_error, err); \ 83 if (strncmp (netns,
"pid:", 4) == 0)
84 s =
format (0,
"/proc/%u/ns/net%c", atoi (netns + 4), 0);
85 else if (netns[0] ==
'/')
86 s =
format (0,
"%s%c", netns, 0);
88 s =
format (0,
"/var/run/netns/%s%c", netns, 0);
90 fd = open ((
char *) s, O_RDONLY);
95 #define TAP_MAX_INSTANCE 1024 114 _IOCTL (vif->
tap_fds[0], TUNSETPERSIST, (
void *) (uintptr_t) 0);
142 int i, num_vhost_queues;
143 int old_netns_fd = -1;
144 struct ifreq ifr = {.ifr_flags = IFF_NO_PI | IFF_VNET_HDR };
145 struct ifreq get_ifr = {.ifr_flags = 0 };
147 struct vhost_memory *vhost_mem = 0;
150 unsigned int tap_features;
151 int tfd = -1, qfd = -1, vfd = -1, nfd = -1;
152 char *host_if_name = 0;
153 unsigned int offload = 0;
160 args->
rv = VNET_API_ERROR_INVALID_INTERFACE;
172 args->
rv = VNET_API_ERROR_UNSPECIFIED;
181 vif->
type = VIRTIO_IF_TYPE_TUN;
182 ifr.ifr_flags |= IFF_TUN;
183 args->
tap_flags &= ~(TAP_FLAG_GSO | TAP_FLAG_CSUM_OFFLOAD);
193 sndbuf = INT_MAX - 1;
197 vif->
type = VIRTIO_IF_TYPE_TAP;
198 ifr.ifr_flags |= IFF_TAP;
213 clib_min (IFNAMSIZ, strlen (host_if_name)));
217 args->
rv = VNET_API_ERROR_NO_MATCHING_INTERFACE;
223 old_netns_fd = open (
"/proc/self/ns/net", O_RDONLY);
226 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_2;
231 if (setns (nfd, CLONE_NEWNET) == -1)
233 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_3;
241 if ((tfd = open (
"/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
243 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_2;
250 _IOCTL (tfd, TUNGETFEATURES, &tap_features);
251 tap_log_dbg (vif,
"TUNGETFEATURES: features 0x%lx", tap_features);
252 if ((tap_features & IFF_VNET_HDR) == 0)
254 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_2;
259 if ((tap_features & IFF_MULTI_QUEUE) == 0)
263 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_2;
270 ifr.ifr_flags |= IFF_MULTI_QUEUE;
272 hdrsz =
sizeof (
struct virtio_net_hdr_v1);
275 offload = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
278 else if (args->
tap_flags & TAP_FLAG_CSUM_OFFLOAD)
280 offload = TUN_F_CSUM;
284 _IOCTL (tfd, TUNSETIFF, (
void *) &ifr);
285 tap_log_dbg (vif,
"TUNSETIFF fd %d name %s flags 0x%x", tfd,
286 ifr.ifr_ifrn.ifrn_name, ifr.ifr_flags);
292 host_if_name = ifr.ifr_ifrn.ifrn_name;
302 _IOCTL (tfd, TUNSETPERSIST, (
void *) (uintptr_t) 0);
309 _IOCTL (tfd, TUNSETPERSIST, (
void *) (uintptr_t) 1);
313 _IOCTL (tfd, TUNGETIFF, (
void *) &get_ifr);
314 tap_log_dbg (vif,
"TUNGETIFF: flags 0x%lx", get_ifr.ifr_flags);
315 if ((get_ifr.ifr_flags & IFF_PERSIST) == 0)
317 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_2;
328 if ((qfd = open (
"/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
330 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_2;
334 _IOCTL (qfd, TUNSETIFF, (
void *) &ifr);
335 tap_log_dbg (vif,
"TUNSETIFF fd %d name %s flags 0x%x", qfd,
336 ifr.ifr_ifrn.ifrn_name, ifr.ifr_flags);
342 tap_log_dbg (vif,
"TUNSETVNETHDRSZ: fd %d vnet_hdr_sz %u",
344 _IOCTL (vif->
tap_fds[i], TUNSETVNETHDRSZ, &hdrsz);
348 _IOCTL (vif->
tap_fds[i], TUNSETSNDBUF, &sndbuf);
352 _IOCTL (vif->
tap_fds[i], TUNSETOFFLOAD, offload);
354 if (fcntl (vif->
tap_fds[i], F_SETFL, O_NONBLOCK) < 0)
364 for (i = 0; i < num_vhost_queues; i++)
366 if ((vfd = open (
"/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0)
368 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_1;
374 _IOCTL (vfd, VHOST_SET_OWNER, 0);
384 args->
rv = VNET_API_ERROR_UNSUPPORTED;
386 "VIRTIO_NET_F_MRG_RXBUF feature");
393 args->
rv = VNET_API_ERROR_UNSUPPORTED;
395 "VIRTIO_RING_F_INDIRECT_DESC feature");
401 args->
rv = VNET_API_ERROR_UNSUPPORTED;
403 "VIRTIO_F_VERSION_1 features");
413 if (!(args->
tap_flags & TAP_FLAG_ATTACH))
419 old_netns_fd = open (
"/proc/self/ns/net", O_RDONLY);
422 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_2;
431 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
434 if (setns (nfd, CLONE_NEWNET) == -1)
436 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_3;
443 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_3;
449 else if (host_if_name)
455 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
461 if (vif->
type == VIRTIO_IF_TYPE_TAP)
469 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
480 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
493 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
505 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
513 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
522 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
532 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
543 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
553 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
563 if (setns (old_netns_fd, CLONE_NEWNET) == -1)
565 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_2;
572 for (i = 0; i < num_vhost_queues; i++)
574 if (i < vif->num_rxqs && (args->
error =
578 args->
rv = VNET_API_ERROR_INIT_FAILED;
582 if (i < vif->num_txqs && (args->
error =
586 args->
rv = VNET_API_ERROR_INIT_FAILED;
592 i =
sizeof (
struct vhost_memory) + sizeof (struct vhost_memory_region);
595 vhost_mem->nregions = 1;
596 vhost_mem->regions[0].memory_size = vpm->
max_size;
597 vhost_mem->regions[0].guest_phys_addr = vpm->
base_addr;
598 vhost_mem->regions[0].userspace_addr =
599 vhost_mem->regions[0].guest_phys_addr;
601 for (i = 0; i < vhost_mem->nregions; i++)
603 "guest_phys_addr 0x%lx userspace_addr 0x%lx", i,
604 vhost_mem->regions[0].memory_size,
605 vhost_mem->regions[0].guest_phys_addr,
606 vhost_mem->regions[0].userspace_addr);
609 for (i = 0; i < num_vhost_queues; i++)
612 _IOCTL (fd, VHOST_SET_FEATURES, &vif->
features);
615 _IOCTL (fd, VHOST_SET_MEM_TABLE, vhost_mem);
620 for (i = 0; i < num_vhost_queues * 2; i++)
622 struct vhost_vring_addr addr = { 0 };
623 struct vhost_vring_state state = { 0 };
624 struct vhost_vring_file file = { 0 };
642 addr.index = state.index = file.index = vring->
queue_id & 1;
643 state.num = vring->
size;
645 state.index, state.num);
646 _IOCTL (fd, VHOST_SET_VRING_NUM, &state);
654 "desc_user_addr 0x%lx avail_user_addr 0x%lx " 655 "used_user_addr 0x%lx", fd, addr.index,
656 addr.
flags, addr.desc_user_addr, addr.avail_user_addr,
657 addr.used_user_addr);
658 _IOCTL (fd, VHOST_SET_VRING_ADDR, &addr);
662 fd, file.index, file.fd);
663 _IOCTL (fd, VHOST_SET_VRING_CALL, &file);
667 fd, file.index, file.fd);
668 _IOCTL (fd, VHOST_SET_VRING_KICK, &file);
672 fd, file.index, file.fd);
673 _IOCTL (fd, VHOST_NET_SET_BACKEND, &file);
676 if (vif->
type == VIRTIO_IF_TYPE_TAP)
696 if (vif->
type != VIRTIO_IF_TYPE_TUN)
705 args->
rv = VNET_API_ERROR_INVALID_REGISTRATION;
715 tun_device_hw_interface_class.index, vif->
dev_instance);
730 else if (args->
tap_flags & TAP_FLAG_CSUM_OFFLOAD)
746 vif->
flags |= VIRTIO_IF_FLAG_ADMIN_UP;
758 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_3;
766 if (old_netns_fd != -1)
767 close (old_netns_fd);
783 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
787 if ((vif->
type != VIRTIO_IF_TYPE_TAP) && (vif->
type != VIRTIO_IF_TYPE_TUN))
788 return VNET_API_ERROR_INVALID_INTERFACE;
796 if (vif->
type == VIRTIO_IF_TYPE_TAP)
821 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
825 if (vif->
type == VIRTIO_IF_TYPE_TUN)
826 return VNET_API_ERROR_UNIMPLEMENTED;
828 const unsigned int csum_offload_on = TUN_F_CSUM;
829 const unsigned int csum_offload_off = 0;
830 unsigned int offload = enable_disable ? csum_offload_on : csum_offload_off;
832 _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;
881 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
885 if (vif->
type == VIRTIO_IF_TYPE_TUN)
886 return VNET_API_ERROR_UNIMPLEMENTED;
888 const unsigned int gso_on = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
889 const unsigned int gso_off = 0;
890 unsigned int offload = enable_disable ? gso_on : gso_off;
892 _IOCTL (vif->
tap_fds[i], TUNSETOFFLOAD, offload);
916 enable_disable ?
"enabling" :
"disabling", sw_if_index);
917 return VNET_API_ERROR_SYSCALL_ERROR_3;
935 if ((vif->
type != VIRTIO_IF_TYPE_TAP)
936 && (vif->
type != VIRTIO_IF_TYPE_TUN))
945 strlen ((
const char *) hi->
name)));
962 strlen ((
const char *) vif->
net_ns)));
980 *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
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.
int tap_gso_enable_disable(vlib_main_t *vm, u32 sw_if_index, int enable_disable)
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)
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)
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)
struct vring_avail * avail
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
vl_api_interface_index_t sw_if_index
#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...)
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...)
static uword clib_bitmap_get(uword *ai, uword i)
Gets the ith bit value from a bitmap.
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
#define VIRTIO_FEATURE(X)
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)
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 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
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
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()
static clib_error_t * tap_mtu_config(vlib_main_t *vm, unformat_input_t *input)
int vnet_hw_interface_set_rx_mode(vnet_main_t *vnm, u32 hw_if_index, u16 queue_id, vnet_hw_interface_rx_mode mode)
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)
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)