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> 44 #define _IOCTL(fd,a,...) \ 45 if (ioctl (fd, a, __VA_ARGS__) < 0) \ 47 err = clib_error_return_unix (0, "ioctl(" #a ")"); \ 66 if (strncmp (netns,
"pid:", 4) == 0)
67 s =
format (0,
"/proc/%u/ns/net%c", atoi (netns + 4), 0);
68 else if (netns[0] ==
'/')
69 s =
format (0,
"%s%c", netns, 0);
71 s =
format (0,
"/var/run/netns/%s%c", netns, 0);
73 fd = open ((
char *) s, O_RDONLY);
88 int old_netns_fd = -1;
91 struct vhost_memory *vhost_mem = 0;
101 args->
rv = VNET_API_ERROR_INVALID_INTERFACE;
119 args->
rv = VNET_API_ERROR_UNSPECIFIED;
126 memset (&ifr, 0,
sizeof (ifr));
134 if ((vif->
fd = open (
"/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0)
136 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_1;
145 args->
rv = VNET_API_ERROR_UNSUPPORTED;
147 "VIRTIO_NET_F_MRG_RXBUF feature");
151 if ((vif->
remote_features & (1ULL << VIRTIO_RING_F_INDIRECT_DESC)) == 0)
153 args->
rv = VNET_API_ERROR_UNSUPPORTED;
155 "VIRTIO_RING_F_INDIRECT_DESC feature");
161 args->
rv = VNET_API_ERROR_UNSUPPORTED;
163 "VIRTIO_F_VERSION_1 features");
167 vif->
features |= 1ULL << VIRTIO_NET_F_MRG_RXBUF;
168 vif->
features |= 1ULL << VIRTIO_F_VERSION_1;
169 vif->
features |= 1ULL << VIRTIO_RING_F_INDIRECT_DESC;
171 _IOCTL (vif->
fd, VHOST_SET_FEATURES, &vif->
features);
173 if ((vif->
tap_fd = open (
"/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
175 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_2;
180 ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR;
181 _IOCTL (vif->
tap_fd, TUNSETIFF, (
void *) &ifr);
184 unsigned int offload = 0;
185 hdrsz =
sizeof (
struct virtio_net_hdr_v1);
186 _IOCTL (vif->
tap_fd, TUNSETOFFLOAD, offload);
187 _IOCTL (vif->
tap_fd, TUNSETVNETHDRSZ, &hdrsz);
188 _IOCTL (vif->
fd, VHOST_SET_OWNER, 0);
195 old_netns_fd = open (
"/proc/self/ns/net", O_RDONLY);
198 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_2;
207 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
210 if (setns (fd, CLONE_NEWNET) == -1)
212 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_3;
220 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_3;
235 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
247 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
258 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
271 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
283 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
291 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
298 if (setns (old_netns_fd, CLONE_NEWNET) == -1)
300 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_2;
308 i =
sizeof (
struct vhost_memory) + sizeof (struct vhost_memory_region);
310 memset (vhost_mem, 0, i);
311 vhost_mem->nregions = 1;
312 vhost_mem->regions[0].memory_size = (1ULL << 47) - 4096;
313 _IOCTL (vif->
fd, VHOST_SET_MEM_TABLE, vhost_mem);
317 args->
rv = VNET_API_ERROR_INIT_FAILED;
323 args->
rv = VNET_API_ERROR_INIT_FAILED;
331 rnd = (
u32) (now * 1e6);
334 memcpy (args->
mac_addr + 2, &rnd, sizeof (rnd));
361 args->
rv = VNET_API_ERROR_INVALID_REGISTRATION;
377 vif->
flags |= VIRTIO_IF_FLAG_ADMIN_UP;
387 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_3;
400 if (old_netns_fd != -1)
401 close (old_netns_fd);
416 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
436 memset (vif, 0,
sizeof (*vif));
455 memset (tapid, 0,
sizeof (*tapid));
461 strlen ((
const char *) hi->
name)));
475 strlen ((
const char *) vif->
net_ns)));
492 *out_tapids = r_tapids;
u32 per_interface_next_index
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)
#define hash_set(h, key, value)
sll srl srl sll sra u16x4 i
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)
clib_error_t * vnet_hw_interface_set_flags(vnet_main_t *vnm, u32 hw_if_index, u32 flags)
ip4_address_t host_ip4_addr
#define hash_unset(h, key)
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)
static vnet_hw_interface_t * vnet_get_sup_hw_interface(vnet_main_t *vnm, u32 sw_if_index)
static f64 vlib_time_now(vlib_main_t *vm)
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.
#define VNET_HW_INTERFACE_FLAG_LINK_UP
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)
static vnet_sw_interface_t * vnet_get_hw_sw_interface(vnet_main_t *vnm, u32 hw_if_index)
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 VLIB_INIT_FUNCTION(x)
u32 last_used_interface_id
int tap_delete_if(vlib_main_t *vm, u32 sw_if_index)
#define clib_error_return(e, args...)
#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.
ip6_address_t host_ip6_addr
#define VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE
static int ethernet_mac_address_is_zero(u8 *mac)
static int open_netns_fd(char *netns)
#define vec_free(V)
Free vector's memory (no header).
ip6_address_t host_ip6_addr
#define clib_memcpy(a, b, c)
#define hash_create(elts, value_bytes)
void vnet_hw_interface_assign_rx_thread(vnet_main_t *vnm, u32 hw_if_index, u16 queue_id, uword thread_index)
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)
clib_error_t * ethernet_register_interface(vnet_main_t *vnm, u32 dev_class_index, u32 dev_instance, u8 *address, u32 *hw_if_index_return, ethernet_flag_change_function_t flag_change)
clib_error_t * virtio_vring_free(vlib_main_t *vm, virtio_if_t *vif, u32 idx)
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)
clib_error_t * vnet_netlink_add_ip4_addr(int ifindex, void *addr, int pfx_len)
static u32 random_u32(u32 *seed)
32-bit random number generator
TAP interface details struct.
clib_error_t * vnet_sw_interface_set_flags(vnet_main_t *vnm, u32 sw_if_index, u32 flags)
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 void vnet_hw_interface_set_input_node(vnet_main_t *vnm, u32 hw_if_index, u32 node_index)
uword * dev_instance_by_interface_id
clib_error_t * vnet_netlink_set_link_master(int ifindex, char *master_ifname)
static clib_error_t * tap_init(vlib_main_t *vm)