/* * Called from CUSE IOCTL: VHOST_SET_VRING_KICK * The virtio device sends an eventfd that it can use to notify us. * This fd gets copied into our process space. */ static int set_vring_kick(struct vhost_device_ctx ctx, struct vhost_vring_file *file) { struct virtio_net *dev; struct eventfd_copy eventfd_call; struct vhost_virtqueue *vq; dev = get_device(ctx); if (dev == NULL) return -1; /* file->index refers to the queue index. The txq is 1, rxq is 0. */ vq = dev->virtqueue[file->index]; if (vq->callfd) close((int)vq->callfd); /* Populate the eventfd_copy structure and call eventfd_copy. */ vq->callfd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC); eventfd_call.source_fd = vq->callfd; eventfd_call.target_fd = file->fd; eventfd_call.target_pid = ctx.pid; if (eventfd_copy(dev, &eventfd_call)) return -1; return 0; }
/* * Function to get the tap device name from the provided file descriptor and * save it in the device structure. */ static int get_ifname(struct virtio_net *dev, int tap_fd, int pid) { struct eventfd_copy fd_tap; struct ifreq ifr; uint32_t size, ifr_size; int ret; fd_tap.source_fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC); fd_tap.target_fd = tap_fd; fd_tap.target_pid = pid; if (eventfd_copy(dev, &fd_tap)) return -1; ret = ioctl(fd_tap.source_fd, TUNGETIFF, &ifr); if (close(fd_tap.source_fd) < 0) RTE_LOG(ERR, VHOST_CONFIG, "(%"PRIu64") fd close failed\n", dev->device_fh); if (ret >= 0) { ifr_size = strnlen(ifr.ifr_name, sizeof(ifr.ifr_name)); size = ifr_size > sizeof(dev->ifname) ? sizeof(dev->ifname) : ifr_size; strncpy(dev->ifname, ifr.ifr_name, size); } else RTE_LOG(ERR, VHOST_CONFIG, "(%"PRIu64") TUNGETIFF ioctl failed\n", dev->device_fh); return 0; }
/* * The IOCTLs are handled using CUSE/FUSE in userspace. Depending on the type * of IOCTL a buffer is requested to read or to write. This request is handled * by FUSE and the buffer is then given to CUSE. */ static void vhost_net_ioctl(fuse_req_t req, int cmd, void *arg, struct fuse_file_info *fi, __rte_unused unsigned flags, const void *in_buf, size_t in_bufsz, size_t out_bufsz) { struct vhost_device_ctx ctx = fuse_req_to_vhost_ctx(req, fi); struct vhost_vring_file file; struct vhost_vring_state state; struct vhost_vring_addr addr; uint64_t features; uint32_t index; int result = 0; switch (cmd) { case VHOST_NET_SET_BACKEND: LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") IOCTL: VHOST_NET_SET_BACKEND\n", ctx.fh); if (!in_buf) { VHOST_IOCTL_RETRY(sizeof(file), 0); break; } file = *(const struct vhost_vring_file *)in_buf; result = cuse_set_backend(ctx, &file); fuse_reply_ioctl(req, result, NULL, 0); break; case VHOST_GET_FEATURES: LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") IOCTL: VHOST_GET_FEATURES\n", ctx.fh); VHOST_IOCTL_W(uint64_t, features, vhost_get_features); break; case VHOST_SET_FEATURES: LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") IOCTL: VHOST_SET_FEATURES\n", ctx.fh); VHOST_IOCTL_R(uint64_t, features, vhost_set_features); break; case VHOST_RESET_OWNER: LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") IOCTL: VHOST_RESET_OWNER\n", ctx.fh); VHOST_IOCTL(vhost_reset_owner); break; case VHOST_SET_OWNER: LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") IOCTL: VHOST_SET_OWNER\n", ctx.fh); VHOST_IOCTL(vhost_set_owner); break; case VHOST_SET_MEM_TABLE: /*TODO fix race condition.*/ LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") IOCTL: VHOST_SET_MEM_TABLE\n", ctx.fh); static struct vhost_memory mem_temp; switch (in_bufsz) { case 0: VHOST_IOCTL_RETRY(sizeof(struct vhost_memory), 0); break; case sizeof(struct vhost_memory): mem_temp = *(const struct vhost_memory *) in_buf; if (mem_temp.nregions > 0) { VHOST_IOCTL_RETRY(sizeof(struct vhost_memory) + (sizeof(struct vhost_memory_region) * mem_temp.nregions), 0); } else { result = -1; fuse_reply_ioctl(req, result, NULL, 0); } break; default: result = cuse_set_mem_table(ctx, in_buf, mem_temp.nregions); if (result) fuse_reply_err(req, EINVAL); else fuse_reply_ioctl(req, result, NULL, 0); } break; case VHOST_SET_VRING_NUM: LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") IOCTL: VHOST_SET_VRING_NUM\n", ctx.fh); VHOST_IOCTL_R(struct vhost_vring_state, state, vhost_set_vring_num); break; case VHOST_SET_VRING_BASE: LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") IOCTL: VHOST_SET_VRING_BASE\n", ctx.fh); VHOST_IOCTL_R(struct vhost_vring_state, state, vhost_set_vring_base); break; case VHOST_GET_VRING_BASE: LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") IOCTL: VHOST_GET_VRING_BASE\n", ctx.fh); VHOST_IOCTL_RW(uint32_t, index, struct vhost_vring_state, state, vhost_get_vring_base); break; case VHOST_SET_VRING_ADDR: LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") IOCTL: VHOST_SET_VRING_ADDR\n", ctx.fh); VHOST_IOCTL_R(struct vhost_vring_addr, addr, vhost_set_vring_addr); break; case VHOST_SET_VRING_KICK: case VHOST_SET_VRING_CALL: if (cmd == VHOST_SET_VRING_KICK) LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") IOCTL: VHOST_SET_VRING_KICK\n", ctx.fh); else LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") IOCTL: VHOST_SET_VRING_CALL\n", ctx.fh); if (!in_buf) VHOST_IOCTL_RETRY(sizeof(struct vhost_vring_file), 0); else { int fd; file = *(const struct vhost_vring_file *)in_buf; LOG_DEBUG(VHOST_CONFIG, "idx:%d fd:%d\n", file.index, file.fd); fd = eventfd_copy(file.fd, ctx.pid); if (fd < 0) { fuse_reply_ioctl(req, -1, NULL, 0); result = -1; break; } file.fd = fd; if (cmd == VHOST_SET_VRING_KICK) { result = vhost_set_vring_kick(ctx, &file); fuse_reply_ioctl(req, result, NULL, 0); } else { result = vhost_set_vring_call(ctx, &file); fuse_reply_ioctl(req, result, NULL, 0); } } break; default: RTE_LOG(ERR, VHOST_CONFIG, "(%"PRIu64") IOCTL: DOESN NOT EXIST\n", ctx.fh); result = -1; fuse_reply_ioctl(req, result, NULL, 0); } if (result < 0) LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") IOCTL: FAIL\n", ctx.fh); else LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") IOCTL: SUCCESS\n", ctx.fh); }