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); \ 75 if (strncmp (netns,
"pid:", 4) == 0)
76 s =
format (0,
"/proc/%u/ns/net%c", atoi (netns + 4), 0);
77 else if (netns[0] ==
'/')
78 s =
format (0,
"%s%c", netns, 0);
80 s =
format (0,
"/var/run/netns/%s%c", netns, 0);
82 fd = open ((
char *) s, O_RDONLY);
87 #define TAP_MAX_INSTANCE 1024 132 int old_netns_fd = -1;
133 struct ifreq ifr = {.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_VNET_HDR };
135 struct vhost_memory *vhost_mem = 0;
138 unsigned int tap_features;
139 int tfd, vfd, nfd = -1;
140 char *host_if_name = 0;
141 unsigned int offload = 0;
148 args->
rv = VNET_API_ERROR_INVALID_INTERFACE;
160 args->
rv = VNET_API_ERROR_UNSPECIFIED;
173 if ((vif->
tap_fd = tfd = open (
"/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
175 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_2;
181 _IOCTL (tfd, TUNGETFEATURES, &tap_features);
182 tap_log_dbg (vif,
"TUNGETFEATURES: features 0x%lx", tap_features);
183 if ((tap_features & IFF_VNET_HDR) == 0)
185 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_2;
190 if ((tap_features & IFF_MULTI_QUEUE) == 0)
194 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_2;
201 ifr.ifr_flags |= IFF_MULTI_QUEUE;
203 hdrsz =
sizeof (
struct virtio_net_hdr_v1);
206 offload = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
211 offload = TUN_F_CSUM;
215 _IOCTL (tfd, TUNSETIFF, (
void *) &ifr);
216 tap_log_dbg (vif,
"TUNSETIFF fd %d name %s flags 0x%x", tfd,
217 ifr.ifr_ifrn.ifrn_name, ifr.ifr_flags);
223 host_if_name = ifr.ifr_ifrn.ifrn_name;
227 if (fcntl (tfd, F_SETFL, O_NONBLOCK) < 0)
234 tap_log_dbg (vif,
"TUNSETVNETHDRSZ: fd %d vnet_hdr_sz %u", tfd, hdrsz);
235 _IOCTL (tfd, TUNSETVNETHDRSZ, &hdrsz);
238 tap_log_dbg (vif,
"TUNSETSNDBUF: fd %d sndbuf %d", tfd, i);
239 _IOCTL (tfd, TUNSETSNDBUF, &i);
241 tap_log_dbg (vif,
"TUNSETOFFLOAD: fd %d offload 0x%lx", tfd, offload);
242 _IOCTL (tfd, TUNSETOFFLOAD, offload);
245 for (i = 0; i < num_q_pairs; i++)
247 if ((vfd = open (
"/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0)
249 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_1;
255 _IOCTL (vfd, VHOST_SET_OWNER, 0);
265 args->
rv = VNET_API_ERROR_UNSUPPORTED;
267 "VIRTIO_NET_F_MRG_RXBUF feature");
274 args->
rv = VNET_API_ERROR_UNSUPPORTED;
276 "VIRTIO_RING_F_INDIRECT_DESC feature");
282 args->
rv = VNET_API_ERROR_UNSUPPORTED;
284 "VIRTIO_F_VERSION_1 features");
298 old_netns_fd = open (
"/proc/self/ns/net", O_RDONLY);
301 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_2;
310 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
313 if (setns (nfd, CLONE_NEWNET) == -1)
315 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_3;
322 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_3;
336 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
348 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
358 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
370 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
382 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
390 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
399 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
409 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
420 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
430 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
440 if (setns (old_netns_fd, CLONE_NEWNET) == -1)
442 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_2;
449 for (i = 0; i < num_q_pairs; i++)
451 if (i < vif->num_rxqs && (args->
error =
455 args->
rv = VNET_API_ERROR_INIT_FAILED;
459 if (i < vif->num_txqs && (args->
error =
463 args->
rv = VNET_API_ERROR_INIT_FAILED;
469 i =
sizeof (
struct vhost_memory) + sizeof (struct vhost_memory_region);
472 vhost_mem->nregions = 1;
473 vhost_mem->regions[0].memory_size = vpm->
max_size;
474 vhost_mem->regions[0].guest_phys_addr = vpm->
base_addr;
475 vhost_mem->regions[0].userspace_addr =
476 vhost_mem->regions[0].guest_phys_addr;
478 for (i = 0; i < vhost_mem->nregions; i++)
480 "guest_phys_addr 0x%lx userspace_addr 0x%lx", i,
481 vhost_mem->regions[0].memory_size,
482 vhost_mem->regions[0].guest_phys_addr,
483 vhost_mem->regions[0].userspace_addr);
486 for (i = 0; i < num_q_pairs; i++)
489 _IOCTL (fd, VHOST_SET_FEATURES, &vif->
features);
492 _IOCTL (fd, VHOST_SET_MEM_TABLE, vhost_mem);
497 for (i = 0; i < num_q_pairs * 2; i++)
499 struct vhost_vring_addr addr = { 0 };
500 struct vhost_vring_state state = { 0 };
501 struct vhost_vring_file file = { 0 };
519 addr.index = state.index = file.index = vring->
queue_id & 1;
520 state.num = vring->
size;
522 state.index, state.num);
523 _IOCTL (fd, VHOST_SET_VRING_NUM, &state);
531 "desc_user_addr 0x%lx avail_user_addr 0x%lx " 532 "used_user_addr 0x%lx", fd, addr.index,
533 addr.
flags, addr.desc_user_addr, addr.avail_user_addr,
534 addr.used_user_addr);
535 _IOCTL (fd, VHOST_SET_VRING_ADDR, &addr);
539 fd, file.index, file.fd);
540 _IOCTL (fd, VHOST_SET_VRING_CALL, &file);
544 fd, file.index, file.fd);
545 _IOCTL (fd, VHOST_SET_VRING_KICK, &file);
549 fd, file.index, file.fd);
550 _IOCTL (fd, VHOST_NET_SET_BACKEND, &file);
577 args->
rv = VNET_API_ERROR_INVALID_REGISTRATION;
609 vif->
flags |= VIRTIO_IF_FLAG_ADMIN_UP;
621 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_3;
629 if (old_netns_fd != -1)
630 close (old_netns_fd);
646 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
651 return VNET_API_ERROR_INVALID_INTERFACE;
680 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
684 const unsigned int csum_offload_on = TUN_F_CSUM;
685 const unsigned int csum_offload_off = 0;
686 unsigned int offload = enable_disable ? csum_offload_on : csum_offload_off;
687 _IOCTL (vif->
tap_fd, TUNSETOFFLOAD, offload);
716 clib_warning (
"Error %s checksum offload on sw_if_index %d",
717 enable_disable ?
"enabling" :
"disabling", sw_if_index);
718 return VNET_API_ERROR_SYSCALL_ERROR_3;
735 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
739 const unsigned int gso_on = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
740 const unsigned int gso_off = 0;
741 unsigned int offload = enable_disable ? gso_on : gso_off;
742 _IOCTL (vif->
tap_fd, TUNSETOFFLOAD, offload);
766 enable_disable ?
"enabling" :
"disabling", sw_if_index);
767 return VNET_API_ERROR_SYSCALL_ERROR_3;
794 strlen ((
const char *) hi->
name)));
810 strlen ((
const char *) vif->
net_ns)));
828 *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)
#define vec_foreach_index(var, v)
Iterate over vector indices.
u8 * format_clib_error(u8 *s, va_list *va)
vlib_node_registration_t virtio_input_node
(constructor) VLIB_REGISTER_NODE (virtio_input_node)
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)
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...)
#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
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
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
#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)