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> 30 #include <linux/netlink.h> 31 #include <linux/rtnetlink.h> 45 #define _IOCTL(fd,a,...) \ 46 if (ioctl (fd, a, __VA_ARGS__) < 0) \ 48 err = clib_error_return_unix (0, "ioctl(" #a ")"); \ 67 if (strncmp (netns,
"pid:", 4) == 0)
68 s =
format (0,
"/proc/%u/ns/net%c", atoi (netns + 4), 0);
69 else if (netns[0] ==
'/')
70 s =
format (0,
"%s%c", netns, 0);
72 s =
format (0,
"/var/run/netns/%s%c", netns, 0);
74 fd = open ((
char *) s, O_RDONLY);
79 #define TAP_MAX_INSTANCE 1024 91 int old_netns_fd = -1;
94 struct vhost_memory *vhost_mem = 0;
98 char *host_if_name = 0;
104 args->
rv = VNET_API_ERROR_INVALID_INTERFACE;
116 args->
rv = VNET_API_ERROR_UNSPECIFIED;
127 if ((vif->
fd = open (
"/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0)
129 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_1;
138 args->
rv = VNET_API_ERROR_UNSUPPORTED;
140 "VIRTIO_NET_F_MRG_RXBUF feature");
147 args->
rv = VNET_API_ERROR_UNSUPPORTED;
149 "VIRTIO_RING_F_INDIRECT_DESC feature");
155 args->
rv = VNET_API_ERROR_UNSUPPORTED;
157 "VIRTIO_F_VERSION_1 features");
167 _IOCTL (vif->
fd, VHOST_SET_FEATURES, &vif->
features);
169 if ((vif->
tap_fd = open (
"/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
171 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_2;
176 ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR;
177 _IOCTL (vif->
tap_fd, TUNSETIFF, (
void *) &ifr);
181 host_if_name = ifr.ifr_ifrn.ifrn_name;
185 unsigned int offload = 0;
186 hdrsz = sizeof (
struct virtio_net_hdr_v1);
189 offload = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
197 _IOCTL (vif->
tap_fd, TUNSETOFFLOAD, offload);
198 _IOCTL (vif->
tap_fd, TUNSETVNETHDRSZ, &hdrsz);
199 _IOCTL (vif->
fd, VHOST_SET_OWNER, 0);
205 old_netns_fd = open (
"/proc/self/ns/net", O_RDONLY);
208 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_2;
217 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
220 if (setns (fd, CLONE_NEWNET) == -1)
222 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_3;
229 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_3;
243 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
255 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
265 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
278 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
290 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
298 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
307 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
317 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
328 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
338 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
348 if (setns (old_netns_fd, CLONE_NEWNET) == -1)
350 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_2;
358 i =
sizeof (
struct vhost_memory) + sizeof (struct vhost_memory_region);
361 vhost_mem->nregions = 1;
362 vhost_mem->regions[0].memory_size = vpm->
max_size;
363 vhost_mem->regions[0].guest_phys_addr = vpm->
base_addr;
364 vhost_mem->regions[0].userspace_addr =
365 vhost_mem->regions[0].guest_phys_addr;
366 _IOCTL (vif->
fd, VHOST_SET_MEM_TABLE, vhost_mem);
371 args->
rv = VNET_API_ERROR_INIT_FAILED;
379 args->
rv = VNET_API_ERROR_INIT_FAILED;
409 args->
rv = VNET_API_ERROR_INVALID_REGISTRATION;
432 vif->
flags |= VIRTIO_IF_FLAG_ADMIN_UP;
444 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_3;
467 if (old_netns_fd != -1)
468 close (old_netns_fd);
485 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
490 return VNET_API_ERROR_INVALID_INTERFACE;
539 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
543 const unsigned int gso_on = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
544 const unsigned int gso_off = 0;
545 unsigned int offload = enable_disable ? gso_on : gso_off;
546 _IOCTL (vif->
tap_fd, TUNSETOFFLOAD, offload);
569 enable_disable ?
"enabling" :
"disabling", sw_if_index);
570 return VNET_API_ERROR_SYSCALL_ERROR_3;
597 strlen ((
const char *) hi->
name)));
613 strlen ((
const char *) vif->
net_ns)));
631 *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.
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)
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)
int tap_gso_enable_disable(vlib_main_t *vm, u32 sw_if_index, int enable_disable)
vnet_interface_main_t interface_main
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_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. ...
clib_error_t * vnet_netlink_set_link_name(int ifindex, char *new_ifname)
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
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)
#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.
#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
clib_error_t * vnet_netlink_add_ip6_route(void *dst, u8 dst_len, void *gw)
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)
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)
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)
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 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)