From patchwork Sat Jun 4 21:05:21 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Marc-Andr=C3=A9_Lureau?= X-Patchwork-Id: 9154955 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 7F6CF60574 for ; Sat, 4 Jun 2016 21:10:10 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 62560281FE for ; Sat, 4 Jun 2016 21:10:10 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 43C6528327; Sat, 4 Jun 2016 21:10:10 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id EA478281FE for ; Sat, 4 Jun 2016 21:10:07 +0000 (UTC) Received: from localhost ([::1]:34117 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b9Ipa-00050O-OB for patchwork-qemu-devel@patchwork.kernel.org; Sat, 04 Jun 2016 17:10:06 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41067) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b9IlQ-0000me-3u for qemu-devel@nongnu.org; Sat, 04 Jun 2016 17:05:52 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1b9IlL-00070i-6D for qemu-devel@nongnu.org; Sat, 04 Jun 2016 17:05:47 -0400 Received: from mail-wm0-x242.google.com ([2a00:1450:400c:c09::242]:36324) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b9IlK-00070W-Ah for qemu-devel@nongnu.org; Sat, 04 Jun 2016 17:05:43 -0400 Received: by mail-wm0-x242.google.com with SMTP id m124so6109651wme.3 for ; Sat, 04 Jun 2016 14:05:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=sTe1My0qaOPAKpjR+jkK0qMbAG4xdTN1FeTSR+BF7PE=; b=V9FuQxG5bmv9nBgJ47GNTZh6Q+kfARKa2gGK3ZtDVJCNVGPc/wu/JiHTrD0Xn8tpgL goJ57B99OyNXNd8CXo7HFxlfqNiIQjumS4sbVg6Xiwq4wxZdkAyjcDYcctTsFCcHKw1O i6WrhKp4N897yq1kkhO0KgBVPKnwzXxpTUQb0w/nW7qcQgZYEzwd7Vo/qTJKlXoNAgSi sbFmEsslOJN8pqKNYLrO5tXKemdrVmkPWCNklD2hJh1HY/iYkRlPPeanCRDe1SBayUea mrwPt1xk+mrB37NBrRNOgTyR7KEENpCDqt8kSzSmn13pv8VLMCS5Re1JIz9InfY886E0 HW3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=sTe1My0qaOPAKpjR+jkK0qMbAG4xdTN1FeTSR+BF7PE=; b=ZmCgKPtN/ZIRm9SL2QQevF2emuGg463smW/xdJmLD8JPX0vdAW3S6n1Uub29bFhyYI 1HYGJXGNjhZ/zGp6ANC3mGZouafE85MCb8bSzVKWYOKGJPz1CIKuQwKGA0PzwmO2vQyV d4xcq26/osmunA1V3ZuzxEFWsPBXBM8CBbMQ8VrFE7zonMzjomWAq0ACEHY/s/EQ9UZ4 HXww0Gytr1tFqKrWmQKnGX9ik5YI8DgOwa+5EhcWD/I8y29irfmAsu0eKqAq7ZgEamlZ aSceV/LNc/UI4wYwOzKQr/NLSA+dU8QHy2bDXHVPtNBo4FqV9lJwNGW0arKXN4RYG5zM wOhw== X-Gm-Message-State: ALyK8tKXuHwBNVdgyCLHf9kb8dszj5zDgUTCk7Wp4e3P05dUVlI88rm0asFJG7IqM0h5eg== X-Received: by 10.28.89.6 with SMTP id n6mr4874354wmb.4.1465074341185; Sat, 04 Jun 2016 14:05:41 -0700 (PDT) Received: from localhost (APoitiers-110-1-1-235.w193-248.abo.wanadoo.fr. [193.248.103.235]) by smtp.gmail.com with ESMTPSA id zc7sm3309764wjb.42.2016.06.04.14.05.38 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 04 Jun 2016 14:05:40 -0700 (PDT) From: marcandre.lureau@redhat.com To: qemu-devel@nongnu.org Date: Sat, 4 Jun 2016 23:05:21 +0200 Message-Id: <1465074321-22507-7-git-send-email-marcandre.lureau@redhat.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1465074321-22507-1-git-send-email-marcandre.lureau@redhat.com> References: <1465074321-22507-1-git-send-email-marcandre.lureau@redhat.com> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:400c:c09::242 Subject: [Qemu-devel] [PATCH 6/6] test/vubr: use contrib/libvhost-user X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , victork@redhat.com, mst@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP From: Marc-André Lureau Use the libvhost-user library. This ended up being a rather large patch that cannot be easily splitted, due to massive code move and API changes. Signed-off-by: Marc-André Lureau --- tests/Makefile | 2 +- tests/vhost-user-bridge.c | 1167 +++++++++------------------------------------ 2 files changed, 223 insertions(+), 946 deletions(-) diff --git a/tests/Makefile b/tests/Makefile index a3e20e3..ac71ce1 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -595,7 +595,7 @@ tests/test-netfilter$(EXESUF): tests/test-netfilter.o $(qtest-obj-y) tests/test-filter-mirror$(EXESUF): tests/test-filter-mirror.o $(qtest-obj-y) tests/test-filter-redirector$(EXESUF): tests/test-filter-redirector.o $(qtest-obj-y) tests/ivshmem-test$(EXESUF): tests/ivshmem-test.o contrib/ivshmem-server/ivshmem-server.o $(libqos-pc-obj-y) -tests/vhost-user-bridge$(EXESUF): tests/vhost-user-bridge.o +tests/vhost-user-bridge$(EXESUF): tests/vhost-user-bridge.o contrib/libvhost-user/libvhost-user.o $(test-util-obj-y) ifeq ($(CONFIG_POSIX),y) LIBS += -lutil diff --git a/tests/vhost-user-bridge.c b/tests/vhost-user-bridge.c index c21cd79..6df3b6a 100644 --- a/tests/vhost-user-bridge.c +++ b/tests/vhost-user-bridge.c @@ -30,20 +30,9 @@ #define _FILE_OFFSET_BITS 64 #include "qemu/osdep.h" -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -#include "qemu/atomic.h" +#include "qemu/iov.h" #include "standard-headers/linux/virtio_net.h" -#include "standard-headers/linux/virtio_ring.h" +#include "contrib/libvhost-user/libvhost-user.h" #define VHOST_USER_BRIDGE_DEBUG 1 @@ -67,6 +56,17 @@ typedef struct Dispatcher { Event events[FD_SETSIZE]; } Dispatcher; +typedef struct VubrDev { + VuDev vudev; + Dispatcher dispatcher; + int backend_udp_sock; + struct sockaddr_in backend_udp_dest; + int hdrlen; + int sock; + int ready; + int quit; +} VubrDev; + static void vubr_die(const char *s) { @@ -158,1029 +158,313 @@ dispatcher_wait(Dispatcher *dispr, uint32_t timeout) return 0; } -typedef struct VubrVirtq { - int call_fd; - int kick_fd; - uint32_t size; - uint16_t last_avail_index; - uint16_t last_used_index; - struct vring_desc *desc; - struct vring_avail *avail; - struct vring_used *used; - uint64_t log_guest_addr; - int enable; -} VubrVirtq; - -/* Based on qemu/hw/virtio/vhost-user.c */ - -#define VHOST_MEMORY_MAX_NREGIONS 8 -#define VHOST_USER_F_PROTOCOL_FEATURES 30 -/* v1.0 compliant. */ -#define VIRTIO_F_VERSION_1 32 - -#define VHOST_LOG_PAGE 4096 - -enum VhostUserProtocolFeature { - VHOST_USER_PROTOCOL_F_MQ = 0, - VHOST_USER_PROTOCOL_F_LOG_SHMFD = 1, - VHOST_USER_PROTOCOL_F_RARP = 2, - - VHOST_USER_PROTOCOL_F_MAX -}; - -#define VHOST_USER_PROTOCOL_FEATURE_MASK ((1 << VHOST_USER_PROTOCOL_F_MAX) - 1) - -typedef enum VhostUserRequest { - VHOST_USER_NONE = 0, - VHOST_USER_GET_FEATURES = 1, - VHOST_USER_SET_FEATURES = 2, - VHOST_USER_SET_OWNER = 3, - VHOST_USER_RESET_OWNER = 4, - VHOST_USER_SET_MEM_TABLE = 5, - VHOST_USER_SET_LOG_BASE = 6, - VHOST_USER_SET_LOG_FD = 7, - VHOST_USER_SET_VRING_NUM = 8, - VHOST_USER_SET_VRING_ADDR = 9, - VHOST_USER_SET_VRING_BASE = 10, - VHOST_USER_GET_VRING_BASE = 11, - VHOST_USER_SET_VRING_KICK = 12, - VHOST_USER_SET_VRING_CALL = 13, - VHOST_USER_SET_VRING_ERR = 14, - VHOST_USER_GET_PROTOCOL_FEATURES = 15, - VHOST_USER_SET_PROTOCOL_FEATURES = 16, - VHOST_USER_GET_QUEUE_NUM = 17, - VHOST_USER_SET_VRING_ENABLE = 18, - VHOST_USER_SEND_RARP = 19, - VHOST_USER_MAX -} VhostUserRequest; - -typedef struct VhostUserMemoryRegion { - uint64_t guest_phys_addr; - uint64_t memory_size; - uint64_t userspace_addr; - uint64_t mmap_offset; -} VhostUserMemoryRegion; - -typedef struct VhostUserMemory { - uint32_t nregions; - uint32_t padding; - VhostUserMemoryRegion regions[VHOST_MEMORY_MAX_NREGIONS]; -} VhostUserMemory; - -typedef struct VhostUserLog { - uint64_t mmap_size; - uint64_t mmap_offset; -} VhostUserLog; - -typedef struct VhostUserMsg { - VhostUserRequest request; - -#define VHOST_USER_VERSION_MASK (0x3) -#define VHOST_USER_REPLY_MASK (0x1<<2) - uint32_t flags; - uint32_t size; /* the following payload size */ - union { -#define VHOST_USER_VRING_IDX_MASK (0xff) -#define VHOST_USER_VRING_NOFD_MASK (0x1<<8) - uint64_t u64; - struct vhost_vring_state state; - struct vhost_vring_addr addr; - VhostUserMemory memory; - VhostUserLog log; - } payload; - int fds[VHOST_MEMORY_MAX_NREGIONS]; - int fd_num; -} QEMU_PACKED VhostUserMsg; - -#define VHOST_USER_HDR_SIZE offsetof(VhostUserMsg, payload.u64) - -/* The version of the protocol we support */ -#define VHOST_USER_VERSION (0x1) - -#define MAX_NR_VIRTQUEUE (8) - -typedef struct VubrDevRegion { - /* Guest Physical address. */ - uint64_t gpa; - /* Memory region size. */ - uint64_t size; - /* QEMU virtual address (userspace). */ - uint64_t qva; - /* Starting offset in our mmaped space. */ - uint64_t mmap_offset; - /* Start address of mmaped space. */ - uint64_t mmap_addr; -} VubrDevRegion; - -typedef struct VubrDev { - int sock; - Dispatcher dispatcher; - uint32_t nregions; - VubrDevRegion regions[VHOST_MEMORY_MAX_NREGIONS]; - VubrVirtq vq[MAX_NR_VIRTQUEUE]; - int log_call_fd; - uint64_t log_size; - uint8_t *log_table; - int backend_udp_sock; - struct sockaddr_in backend_udp_dest; - int ready; - uint64_t features; - int hdrlen; -} VubrDev; - -static const char *vubr_request_str[] = { - [VHOST_USER_NONE] = "VHOST_USER_NONE", - [VHOST_USER_GET_FEATURES] = "VHOST_USER_GET_FEATURES", - [VHOST_USER_SET_FEATURES] = "VHOST_USER_SET_FEATURES", - [VHOST_USER_SET_OWNER] = "VHOST_USER_SET_OWNER", - [VHOST_USER_RESET_OWNER] = "VHOST_USER_RESET_OWNER", - [VHOST_USER_SET_MEM_TABLE] = "VHOST_USER_SET_MEM_TABLE", - [VHOST_USER_SET_LOG_BASE] = "VHOST_USER_SET_LOG_BASE", - [VHOST_USER_SET_LOG_FD] = "VHOST_USER_SET_LOG_FD", - [VHOST_USER_SET_VRING_NUM] = "VHOST_USER_SET_VRING_NUM", - [VHOST_USER_SET_VRING_ADDR] = "VHOST_USER_SET_VRING_ADDR", - [VHOST_USER_SET_VRING_BASE] = "VHOST_USER_SET_VRING_BASE", - [VHOST_USER_GET_VRING_BASE] = "VHOST_USER_GET_VRING_BASE", - [VHOST_USER_SET_VRING_KICK] = "VHOST_USER_SET_VRING_KICK", - [VHOST_USER_SET_VRING_CALL] = "VHOST_USER_SET_VRING_CALL", - [VHOST_USER_SET_VRING_ERR] = "VHOST_USER_SET_VRING_ERR", - [VHOST_USER_GET_PROTOCOL_FEATURES] = "VHOST_USER_GET_PROTOCOL_FEATURES", - [VHOST_USER_SET_PROTOCOL_FEATURES] = "VHOST_USER_SET_PROTOCOL_FEATURES", - [VHOST_USER_GET_QUEUE_NUM] = "VHOST_USER_GET_QUEUE_NUM", - [VHOST_USER_SET_VRING_ENABLE] = "VHOST_USER_SET_VRING_ENABLE", - [VHOST_USER_SEND_RARP] = "VHOST_USER_SEND_RARP", - [VHOST_USER_MAX] = "VHOST_USER_MAX", -}; - static void -print_buffer(uint8_t *buf, size_t len) +vubr_handle_tx(VuDev *dev, int qidx) { - int i; - printf("Raw buffer:\n"); - for (i = 0; i < len; i++) { - if (i % 16 == 0) { - printf("\n"); - } - if (i % 4 == 0) { - printf(" "); - } - printf("%02x ", buf[i]); - } - printf("\n............................................................\n"); -} + VuVirtq *vq = vu_get_queue(dev, qidx); + VubrDev *vubr = container_of(dev, VubrDev, vudev); + int hdrlen = vubr->hdrlen; + VuVirtqElement *elem = NULL; -/* Translate guest physical address to our virtual address. */ -static uint64_t -gpa_to_va(VubrDev *dev, uint64_t guest_addr) -{ - int i; + assert(qidx % 2); - /* Find matching memory region. */ - for (i = 0; i < dev->nregions; i++) { - VubrDevRegion *r = &dev->regions[i]; - - if ((guest_addr >= r->gpa) && (guest_addr < (r->gpa + r->size))) { - return guest_addr - r->gpa + r->mmap_addr + r->mmap_offset; - } - } - - assert(!"address not found in regions"); - return 0; -} - -/* Translate qemu virtual address to our virtual address. */ -static uint64_t -qva_to_va(VubrDev *dev, uint64_t qemu_addr) -{ - int i; + for (;;) { + ssize_t ret; + unsigned int out_num; + struct iovec sg[VIRTQUEUE_MAX_SIZE], *out_sg; - /* Find matching memory region. */ - for (i = 0; i < dev->nregions; i++) { - VubrDevRegion *r = &dev->regions[i]; - - if ((qemu_addr >= r->qva) && (qemu_addr < (r->qva + r->size))) { - return qemu_addr - r->qva + r->mmap_addr + r->mmap_offset; + elem = vu_queue_pop(dev, vq, sizeof(VuVirtqElement)); + if (!elem) { + break; } - } - - assert(!"address not found in regions"); - return 0; -} -static void -vubr_message_read(int conn_fd, VhostUserMsg *vmsg) -{ - char control[CMSG_SPACE(VHOST_MEMORY_MAX_NREGIONS * sizeof(int))] = { }; - struct iovec iov = { - .iov_base = (char *)vmsg, - .iov_len = VHOST_USER_HDR_SIZE, - }; - struct msghdr msg = { - .msg_iov = &iov, - .msg_iovlen = 1, - .msg_control = control, - .msg_controllen = sizeof(control), - }; - size_t fd_size; - struct cmsghdr *cmsg; - int rc; - - rc = recvmsg(conn_fd, &msg, 0); - - if (rc == 0) { - fprintf(stderr, "Peer disconnected.\n"); - exit(1); - } - if (rc < 0) { - vubr_die("recvmsg"); - } - - vmsg->fd_num = 0; - for (cmsg = CMSG_FIRSTHDR(&msg); - cmsg != NULL; - cmsg = CMSG_NXTHDR(&msg, cmsg)) - { - if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) { - fd_size = cmsg->cmsg_len - CMSG_LEN(0); - vmsg->fd_num = fd_size / sizeof(int); - memcpy(vmsg->fds, CMSG_DATA(cmsg), fd_size); + out_num = elem->out_num; + out_sg = elem->out_sg; + if (out_num < 1) { + fprintf(stderr, "virtio-net header not in first element\n"); break; } - } - - if (vmsg->size > sizeof(vmsg->payload)) { - fprintf(stderr, - "Error: too big message request: %d, size: vmsg->size: %u, " - "while sizeof(vmsg->payload) = %zu\n", - vmsg->request, vmsg->size, sizeof(vmsg->payload)); - exit(1); - } - - if (vmsg->size) { - rc = read(conn_fd, &vmsg->payload, vmsg->size); - if (rc == 0) { - vubr_die("recvmsg"); - fprintf(stderr, "Peer disconnected.\n"); - exit(1); + if (VHOST_USER_BRIDGE_DEBUG) { + iov_hexdump(out_sg, out_num, stderr, "TX:", 1024); } - if (rc < 0) { - vubr_die("recvmsg"); + + if (hdrlen) { + unsigned sg_num = iov_copy(sg, ARRAY_SIZE(sg), + out_sg, out_num, + hdrlen, -1); + out_num = sg_num; + out_sg = sg; } - assert(rc == vmsg->size); - } -} + struct msghdr msg = { + .msg_name = (struct sockaddr *) &vubr->backend_udp_dest, + .msg_namelen = sizeof(struct sockaddr_in), + .msg_iov = out_sg, + .msg_iovlen = out_num, + }; + do { + ret = sendmsg(vubr->backend_udp_sock, &msg, 0); + } while (ret == -1 && (errno == EAGAIN || errno == EINTR)); -static void -vubr_message_write(int conn_fd, VhostUserMsg *vmsg) -{ - int rc; + if (ret == -1) { + vubr_die("sendmsg()"); + } - do { - rc = write(conn_fd, vmsg, VHOST_USER_HDR_SIZE + vmsg->size); - } while (rc < 0 && errno == EINTR); + vu_queue_push(dev, vq, elem, 0); + vu_queue_notify(dev, vq); - if (rc < 0) { - vubr_die("write"); + free(elem); + elem = NULL; } -} -static void -vubr_backend_udp_sendbuf(VubrDev *dev, uint8_t *buf, size_t len) -{ - int slen = sizeof(struct sockaddr_in); - - if (sendto(dev->backend_udp_sock, buf, len, 0, - (struct sockaddr *) &dev->backend_udp_dest, slen) == -1) { - vubr_die("sendto()"); - } + free(elem); } -static int -vubr_backend_udp_recvbuf(VubrDev *dev, uint8_t *buf, size_t buflen) +static void +iov_restore_front(struct iovec *front, struct iovec *iov, size_t bytes) { - int slen = sizeof(struct sockaddr_in); - int rc; + struct iovec *cur; - rc = recvfrom(dev->backend_udp_sock, buf, buflen, 0, - (struct sockaddr *) &dev->backend_udp_dest, - (socklen_t *)&slen); - if (rc == -1) { - vubr_die("recvfrom()"); + for (cur = front; front != iov; cur++) { + bytes -= cur->iov_len; } - return rc; + cur->iov_base -= bytes; + cur->iov_len += bytes; } static void -vubr_consume_raw_packet(VubrDev *dev, uint8_t *buf, uint32_t len) +iov_truncate(struct iovec *iov, unsigned iovc, size_t bytes) { - int hdrlen = dev->hdrlen; - DPRINT(" hdrlen = %d\n", dev->hdrlen); + unsigned i; - if (VHOST_USER_BRIDGE_DEBUG) { - print_buffer(buf, len); - } - vubr_backend_udp_sendbuf(dev, buf + hdrlen, len - hdrlen); -} + for (i = 0; i < iovc; i++, iov++) { + if (bytes < iov->iov_len) { + iov->iov_len = bytes; + return; + } -/* Kick the log_call_fd if required. */ -static void -vubr_log_kick(VubrDev *dev) -{ - if (dev->log_call_fd != -1) { - DPRINT("Kicking the QEMU's log...\n"); - eventfd_write(dev->log_call_fd, 1); + bytes -= iov->iov_len; } -} -/* Kick the guest if necessary. */ -static void -vubr_virtqueue_kick(VubrVirtq *vq) -{ - if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)) { - DPRINT("Kicking the guest...\n"); - eventfd_write(vq->call_fd, 1); - } + assert(!"couldn't truncate iov"); } static void -vubr_log_page(uint8_t *log_table, uint64_t page) +vubr_backend_recv_cb(int sock, void *ctx) { - DPRINT("Logged dirty guest page: %"PRId64"\n", page); - atomic_or(&log_table[page / 8], 1 << (page % 8)); -} + VubrDev *vubr = (VubrDev *) ctx; + VuDev *dev = &vubr->vudev; + VuVirtq *vq = vu_get_queue(dev, 0); + VuVirtqElement *elem = NULL; + struct iovec mhdr_sg[VIRTQUEUE_MAX_SIZE]; + struct virtio_net_hdr_mrg_rxbuf mhdr; + unsigned mhdr_cnt = 0; + int hdrlen = vubr->hdrlen; + int i = 0; + struct virtio_net_hdr hdr = { + .flags = 0, + .gso_type = VIRTIO_NET_HDR_GSO_NONE + }; -static void -vubr_log_write(VubrDev *dev, uint64_t address, uint64_t length) -{ - uint64_t page; + DPRINT("\n\n *** IN UDP RECEIVE CALLBACK ***\n\n"); + DPRINT(" hdrlen = %d\n", hdrlen); - if (!(dev->features & (1ULL << VHOST_F_LOG_ALL)) || - !dev->log_table || !length) { + if (!vu_queue_enabled(dev, vq) || + !vu_queue_avail_bytes(dev, vq, hdrlen, 0)) { + DPRINT("Got UDP packet, but no available descriptors on RX virtq.\n"); return; } - assert(dev->log_size > ((address + length - 1) / VHOST_LOG_PAGE / 8)); - - page = address / VHOST_LOG_PAGE; - while (page * VHOST_LOG_PAGE < address + length) { - vubr_log_page(dev->log_table, page); - page += VHOST_LOG_PAGE; - } - vubr_log_kick(dev); -} - -static void -vubr_post_buffer(VubrDev *dev, VubrVirtq *vq, uint8_t *buf, int32_t len) -{ - struct vring_desc *desc = vq->desc; - struct vring_avail *avail = vq->avail; - struct vring_used *used = vq->used; - uint64_t log_guest_addr = vq->log_guest_addr; - int32_t remaining_len = len; - - unsigned int size = vq->size; - - uint16_t avail_index = atomic_mb_read(&avail->idx); - - /* We check the available descriptors before posting the - * buffer, so here we assume that enough available - * descriptors. */ - assert(vq->last_avail_index != avail_index); - uint16_t a_index = vq->last_avail_index % size; - uint16_t u_index = vq->last_used_index % size; - uint16_t d_index = avail->ring[a_index]; - - int i = d_index; - uint32_t written_len = 0; - do { - DPRINT("Post packet to guest on vq:\n"); - DPRINT(" size = %d\n", vq->size); - DPRINT(" last_avail_index = %d\n", vq->last_avail_index); - DPRINT(" last_used_index = %d\n", vq->last_used_index); - DPRINT(" a_index = %d\n", a_index); - DPRINT(" u_index = %d\n", u_index); - DPRINT(" d_index = %d\n", d_index); - DPRINT(" desc[%d].addr = 0x%016"PRIx64"\n", i, desc[i].addr); - DPRINT(" desc[%d].len = %d\n", i, desc[i].len); - DPRINT(" desc[%d].flags = %d\n", i, desc[i].flags); - DPRINT(" avail->idx = %d\n", avail_index); - DPRINT(" used->idx = %d\n", used->idx); - - if (!(desc[i].flags & VRING_DESC_F_WRITE)) { - /* FIXME: we should find writable descriptor. */ - fprintf(stderr, "Error: descriptor is not writable. Exiting.\n"); - exit(1); - } + struct iovec *sg; + ssize_t ret, total = 0; + unsigned int num; - void *chunk_start = (void *)(uintptr_t)gpa_to_va(dev, desc[i].addr); - uint32_t chunk_len = desc[i].len; - uint32_t chunk_write_len = MIN(remaining_len, chunk_len); - - memcpy(chunk_start, buf + written_len, chunk_write_len); - vubr_log_write(dev, desc[i].addr, chunk_write_len); - remaining_len -= chunk_write_len; - written_len += chunk_write_len; - - if ((remaining_len == 0) || !(desc[i].flags & VRING_DESC_F_NEXT)) { + elem = vu_queue_pop(dev, vq, sizeof(VuVirtqElement)); + if (!elem) { break; } - i = desc[i].next; - } while (1); - - if (remaining_len > 0) { - fprintf(stderr, - "Too long packet for RX, remaining_len = %d, Dropping...\n", - remaining_len); - return; - } - - /* Add descriptor to the used ring. */ - used->ring[u_index].id = d_index; - used->ring[u_index].len = len; - vubr_log_write(dev, - log_guest_addr + offsetof(struct vring_used, ring[u_index]), - sizeof(used->ring[u_index])); - - vq->last_avail_index++; - vq->last_used_index++; - - atomic_mb_set(&used->idx, vq->last_used_index); - vubr_log_write(dev, - log_guest_addr + offsetof(struct vring_used, idx), - sizeof(used->idx)); - - /* Kick the guest if necessary. */ - vubr_virtqueue_kick(vq); -} - -static int -vubr_process_desc(VubrDev *dev, VubrVirtq *vq) -{ - struct vring_desc *desc = vq->desc; - struct vring_avail *avail = vq->avail; - struct vring_used *used = vq->used; - uint64_t log_guest_addr = vq->log_guest_addr; - - unsigned int size = vq->size; - - uint16_t a_index = vq->last_avail_index % size; - uint16_t u_index = vq->last_used_index % size; - uint16_t d_index = avail->ring[a_index]; - - uint32_t i, len = 0; - size_t buf_size = 4096; - uint8_t buf[4096]; - - DPRINT("Chunks: "); - i = d_index; - do { - void *chunk_start = (void *)(uintptr_t)gpa_to_va(dev, desc[i].addr); - uint32_t chunk_len = desc[i].len; - - assert(!(desc[i].flags & VRING_DESC_F_WRITE)); - - if (len + chunk_len < buf_size) { - memcpy(buf + len, chunk_start, chunk_len); - DPRINT("%d ", chunk_len); - } else { - fprintf(stderr, "Error: too long packet. Dropping...\n"); + if (elem->in_num < 1) { + fprintf(stderr, "virtio-net contains no in buffers\n"); break; } - len += chunk_len; - - if (!(desc[i].flags & VRING_DESC_F_NEXT)) { - break; + sg = elem->in_sg; + num = elem->in_num; + if (i == 0) { + if (hdrlen == 12) { + mhdr_cnt = iov_copy(mhdr_sg, ARRAY_SIZE(mhdr_sg), + sg, elem->in_num, + offsetof(typeof(mhdr), num_buffers), + sizeof(mhdr.num_buffers)); + } + iov_from_buf(sg, elem->in_num, 0, &hdr, sizeof hdr); + total += hdrlen; + assert(iov_discard_front(&sg, &num, hdrlen) == hdrlen); } - i = desc[i].next; - } while (1); - DPRINT("\n"); - - if (!len) { - return -1; - } - - /* Add descriptor to the used ring. */ - used->ring[u_index].id = d_index; - used->ring[u_index].len = len; - vubr_log_write(dev, - log_guest_addr + offsetof(struct vring_used, ring[u_index]), - sizeof(used->ring[u_index])); - - vubr_consume_raw_packet(dev, buf, len); - - return 0; -} + struct msghdr msg = { + .msg_name = (struct sockaddr *) &vubr->backend_udp_dest, + .msg_namelen = sizeof(struct sockaddr_in), + .msg_iov = sg, + .msg_iovlen = elem->in_num, + .msg_flags = MSG_DONTWAIT, + }; + do { + ret = recvmsg(vubr->backend_udp_sock, &msg, 0); + } while (ret == -1 && (errno == EINTR)); -static void -vubr_process_avail(VubrDev *dev, VubrVirtq *vq) -{ - struct vring_avail *avail = vq->avail; - struct vring_used *used = vq->used; - uint64_t log_guest_addr = vq->log_guest_addr; - - while (vq->last_avail_index != atomic_mb_read(&avail->idx)) { - vubr_process_desc(dev, vq); - vq->last_avail_index++; - vq->last_used_index++; - } + if (i == 0) { + iov_restore_front(elem->in_sg, sg, hdrlen); + } - atomic_mb_set(&used->idx, vq->last_used_index); - vubr_log_write(dev, - log_guest_addr + offsetof(struct vring_used, idx), - sizeof(used->idx)); -} + if (ret == -1) { + if (errno == EWOULDBLOCK) { + vu_queue_discard(dev, vq); + break; + } -static void -vubr_backend_recv_cb(int sock, void *ctx) -{ - VubrDev *dev = (VubrDev *) ctx; - VubrVirtq *rx_vq = &dev->vq[0]; - uint8_t buf[4096]; - struct virtio_net_hdr_v1 *hdr = (struct virtio_net_hdr_v1 *)buf; - int hdrlen = dev->hdrlen; - int buflen = sizeof(buf); - int len; - - if (!dev->ready) { - return; - } + vubr_die("recvmsg()"); + } - DPRINT("\n\n *** IN UDP RECEIVE CALLBACK ***\n\n"); - DPRINT(" hdrlen = %d\n", hdrlen); + total += ret; + iov_truncate(elem->in_sg, elem->in_num, total); + vu_queue_fill(dev, vq, elem, total, i++); - uint16_t avail_index = atomic_mb_read(&rx_vq->avail->idx); + free(elem); + elem = NULL; + } while (false); /* could loop if DONTWAIT worked? */ - /* If there is no available descriptors, just do nothing. - * The buffer will be handled by next arrived UDP packet, - * or next kick on receive virtq. */ - if (rx_vq->last_avail_index == avail_index) { - DPRINT("Got UDP packet, but no available descriptors on RX virtq.\n"); - return; + if (mhdr_cnt) { + mhdr.num_buffers = i; + iov_from_buf(mhdr_sg, mhdr_cnt, + 0, + &mhdr.num_buffers, sizeof mhdr.num_buffers); } - memset(buf, 0, hdrlen); - /* TODO: support mergeable buffers. */ - if (hdrlen == 12) - hdr->num_buffers = 1; - len = vubr_backend_udp_recvbuf(dev, buf + hdrlen, buflen - hdrlen); + vu_queue_flush(dev, vq, i); + vu_queue_notify(dev, vq); - vubr_post_buffer(dev, rx_vq, buf, len + hdrlen); + free(elem); } static void -vubr_kick_cb(int sock, void *ctx) +vubr_receive_cb(int sock, void *ctx) { - VubrDev *dev = (VubrDev *) ctx; - eventfd_t kick_data; - ssize_t rc; + VubrDev *vubr = (VubrDev *)ctx; - rc = eventfd_read(sock, &kick_data); - if (rc == -1) { - vubr_die("eventfd_read()"); - } else { - DPRINT("Got kick_data: %016"PRIx64"\n", kick_data); - vubr_process_avail(dev, &dev->vq[1]); + if (!vu_dispatch(&vubr->vudev)) { + fprintf(stderr, "Error while dispatching\n"); } } -static int -vubr_none_exec(VubrDev *dev, VhostUserMsg *vmsg) -{ - DPRINT("Function %s() not implemented yet.\n", __func__); - return 0; -} +typedef struct WatchData { + VuDev *dev; + vu_watch_cb cb; + void *data; +} WatchData; -static int -vubr_get_features_exec(VubrDev *dev, VhostUserMsg *vmsg) +static void +watch_cb(int sock, void *ctx) { - vmsg->payload.u64 = - ((1ULL << VIRTIO_NET_F_MRG_RXBUF) | - (1ULL << VHOST_F_LOG_ALL) | - (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) | - (1ULL << VHOST_USER_F_PROTOCOL_FEATURES)); - - vmsg->size = sizeof(vmsg->payload.u64); - - DPRINT("Sending back to guest u64: 0x%016"PRIx64"\n", vmsg->payload.u64); + struct WatchData *wd = ctx; - /* Reply */ - return 1; + wd->cb(wd->dev, VU_WATCH_IN, wd->data); } -static int -vubr_set_features_exec(VubrDev *dev, VhostUserMsg *vmsg) +static void +vubr_add_watch(VuDev *dev, int fd, int condition, + vu_watch_cb cb, void *data) { - DPRINT("u64: 0x%016"PRIx64"\n", vmsg->payload.u64); - - dev->features = vmsg->payload.u64; - if ((dev->features & (1ULL << VIRTIO_F_VERSION_1)) || - (dev->features & (1ULL << VIRTIO_NET_F_MRG_RXBUF))) { - dev->hdrlen = 12; - } else { - dev->hdrlen = 10; - } + VubrDev *vubr = container_of(dev, VubrDev, vudev); + static WatchData watches[FD_SETSIZE]; + struct WatchData *wd = &watches[fd]; - return 0; -} - -static int -vubr_set_owner_exec(VubrDev *dev, VhostUserMsg *vmsg) -{ - return 0; + wd->cb = cb; + wd->data = data; + wd->dev = dev; + dispatcher_add(&vubr->dispatcher, fd, wd, watch_cb); } static void -vubr_close_log(VubrDev *dev) +vubr_remove_watch(VuDev *dev, int fd) { - if (dev->log_table) { - if (munmap(dev->log_table, dev->log_size) != 0) { - vubr_die("munmap()"); - } + VubrDev *vubr = container_of(dev, VubrDev, vudev); - dev->log_table = 0; - } - if (dev->log_call_fd != -1) { - close(dev->log_call_fd); - dev->log_call_fd = -1; - } + dispatcher_remove(&vubr->dispatcher, fd); } static int -vubr_reset_device_exec(VubrDev *dev, VhostUserMsg *vmsg) +vubr_send_rarp_exec(VuDev *dev, VhostUserMsg *vmsg) { - vubr_close_log(dev); - dev->ready = 0; - dev->features = 0; - return 0; -} - -static int -vubr_set_mem_table_exec(VubrDev *dev, VhostUserMsg *vmsg) -{ - int i; - VhostUserMemory *memory = &vmsg->payload.memory; - dev->nregions = memory->nregions; - - DPRINT("Nregions: %d\n", memory->nregions); - for (i = 0; i < dev->nregions; i++) { - void *mmap_addr; - VhostUserMemoryRegion *msg_region = &memory->regions[i]; - VubrDevRegion *dev_region = &dev->regions[i]; - - DPRINT("Region %d\n", i); - DPRINT(" guest_phys_addr: 0x%016"PRIx64"\n", - msg_region->guest_phys_addr); - DPRINT(" memory_size: 0x%016"PRIx64"\n", - msg_region->memory_size); - DPRINT(" userspace_addr 0x%016"PRIx64"\n", - msg_region->userspace_addr); - DPRINT(" mmap_offset 0x%016"PRIx64"\n", - msg_region->mmap_offset); - - dev_region->gpa = msg_region->guest_phys_addr; - dev_region->size = msg_region->memory_size; - dev_region->qva = msg_region->userspace_addr; - dev_region->mmap_offset = msg_region->mmap_offset; - - /* We don't use offset argument of mmap() since the - * mapped address has to be page aligned, and we use huge - * pages. */ - mmap_addr = mmap(0, dev_region->size + dev_region->mmap_offset, - PROT_READ | PROT_WRITE, MAP_SHARED, - vmsg->fds[i], 0); - - if (mmap_addr == MAP_FAILED) { - vubr_die("mmap"); - } - dev_region->mmap_addr = (uint64_t)(uintptr_t)mmap_addr; - DPRINT(" mmap_addr: 0x%016"PRIx64"\n", dev_region->mmap_addr); - - close(vmsg->fds[i]); - } - + DPRINT("Function %s() not implemented yet.\n", __func__); return 0; } static int -vubr_set_log_base_exec(VubrDev *dev, VhostUserMsg *vmsg) +vubr_process_msg(VuDev *dev, VhostUserMsg *vmsg, int *do_reply) { - int fd; - uint64_t log_mmap_size, log_mmap_offset; - void *rc; - - assert(vmsg->fd_num == 1); - fd = vmsg->fds[0]; - - assert(vmsg->size == sizeof(vmsg->payload.log)); - log_mmap_offset = vmsg->payload.log.mmap_offset; - log_mmap_size = vmsg->payload.log.mmap_size; - DPRINT("Log mmap_offset: %"PRId64"\n", log_mmap_offset); - DPRINT("Log mmap_size: %"PRId64"\n", log_mmap_size); - - rc = mmap(0, log_mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, - log_mmap_offset); - if (rc == MAP_FAILED) { - vubr_die("mmap"); + switch (vmsg->request) { + case VHOST_USER_SEND_RARP: + *do_reply = vubr_send_rarp_exec(dev, vmsg); + return 1; + default: + /* let the library handle the rest */ + return 0; } - dev->log_table = rc; - dev->log_size = log_mmap_size; - vmsg->size = sizeof(vmsg->payload.u64); - /* Reply */ - return 1; -} - -static int -vubr_set_log_fd_exec(VubrDev *dev, VhostUserMsg *vmsg) -{ - assert(vmsg->fd_num == 1); - dev->log_call_fd = vmsg->fds[0]; - DPRINT("Got log_call_fd: %d\n", vmsg->fds[0]); return 0; } -static int -vubr_set_vring_num_exec(VubrDev *dev, VhostUserMsg *vmsg) -{ - unsigned int index = vmsg->payload.state.index; - unsigned int num = vmsg->payload.state.num; - - DPRINT("State.index: %d\n", index); - DPRINT("State.num: %d\n", num); - dev->vq[index].size = num; - return 0; -} - -static int -vubr_set_vring_addr_exec(VubrDev *dev, VhostUserMsg *vmsg) -{ - struct vhost_vring_addr *vra = &vmsg->payload.addr; - unsigned int index = vra->index; - VubrVirtq *vq = &dev->vq[index]; - - DPRINT("vhost_vring_addr:\n"); - DPRINT(" index: %d\n", vra->index); - DPRINT(" flags: %d\n", vra->flags); - DPRINT(" desc_user_addr: 0x%016llx\n", vra->desc_user_addr); - DPRINT(" used_user_addr: 0x%016llx\n", vra->used_user_addr); - DPRINT(" avail_user_addr: 0x%016llx\n", vra->avail_user_addr); - DPRINT(" log_guest_addr: 0x%016llx\n", vra->log_guest_addr); - - vq->desc = (struct vring_desc *)(uintptr_t)qva_to_va(dev, vra->desc_user_addr); - vq->used = (struct vring_used *)(uintptr_t)qva_to_va(dev, vra->used_user_addr); - vq->avail = (struct vring_avail *)(uintptr_t)qva_to_va(dev, vra->avail_user_addr); - vq->log_guest_addr = vra->log_guest_addr; - - DPRINT("Setting virtq addresses:\n"); - DPRINT(" vring_desc at %p\n", vq->desc); - DPRINT(" vring_used at %p\n", vq->used); - DPRINT(" vring_avail at %p\n", vq->avail); - - vq->last_used_index = vq->used->idx; - return 0; -} - -static int -vubr_set_vring_base_exec(VubrDev *dev, VhostUserMsg *vmsg) +static void +vubr_set_features(VuDev *dev, uint64_t features) { - unsigned int index = vmsg->payload.state.index; - unsigned int num = vmsg->payload.state.num; + VubrDev *vubr = container_of(dev, VubrDev, vudev); - DPRINT("State.index: %d\n", index); - DPRINT("State.num: %d\n", num); - dev->vq[index].last_avail_index = num; - - return 0; -} - -static int -vubr_get_vring_base_exec(VubrDev *dev, VhostUserMsg *vmsg) -{ - unsigned int index = vmsg->payload.state.index; - - DPRINT("State.index: %d\n", index); - vmsg->payload.state.num = dev->vq[index].last_avail_index; - vmsg->size = sizeof(vmsg->payload.state); - /* FIXME: this is a work-around for a bug in QEMU enabling - * too early vrings. When protocol features are enabled, - * we have to respect * VHOST_USER_SET_VRING_ENABLE request. */ - dev->ready = 0; - - if (dev->vq[index].call_fd != -1) { - close(dev->vq[index].call_fd); - dev->vq[index].call_fd = -1; - } - if (dev->vq[index].kick_fd != -1) { - close(dev->vq[index].kick_fd); - dispatcher_remove(&dev->dispatcher, dev->vq[index].kick_fd); - dev->vq[index].kick_fd = -1; + if ((features & (1ULL << VIRTIO_F_VERSION_1)) || + (features & (1ULL << VIRTIO_NET_F_MRG_RXBUF))) { + vubr->hdrlen = 12; + } else { + vubr->hdrlen = 10; } - - /* Reply */ - return 1; } -static int -vubr_set_vring_kick_exec(VubrDev *dev, VhostUserMsg *vmsg) +static uint64_t +vubr_get_features(VuDev *dev) { - uint64_t u64_arg = vmsg->payload.u64; - int index = u64_arg & VHOST_USER_VRING_IDX_MASK; - - DPRINT("u64: 0x%016"PRIx64"\n", vmsg->payload.u64); - - assert((u64_arg & VHOST_USER_VRING_NOFD_MASK) == 0); - assert(vmsg->fd_num == 1); - - if (dev->vq[index].kick_fd != -1) { - close(dev->vq[index].kick_fd); - dispatcher_remove(&dev->dispatcher, dev->vq[index].kick_fd); - } - dev->vq[index].kick_fd = vmsg->fds[0]; - DPRINT("Got kick_fd: %d for vq: %d\n", vmsg->fds[0], index); - - if (index % 2 == 1) { - /* TX queue. */ - dispatcher_add(&dev->dispatcher, dev->vq[index].kick_fd, - dev, vubr_kick_cb); - - DPRINT("Waiting for kicks on fd: %d for vq: %d\n", - dev->vq[index].kick_fd, index); - } - /* We temporarily use this hack to determine that both TX and RX - * queues are set up and ready for processing. - * FIXME: we need to rely in VHOST_USER_SET_VRING_ENABLE and - * actual kicks. */ - if (dev->vq[0].kick_fd != -1 && - dev->vq[1].kick_fd != -1) { - dev->ready = 1; - DPRINT("vhost-user-bridge is ready for processing queues.\n"); - } - return 0; - + return 1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE | + 1ULL << VIRTIO_NET_F_MRG_RXBUF; } -static int -vubr_set_vring_call_exec(VubrDev *dev, VhostUserMsg *vmsg) +static void +vubr_queue_set_started(VuDev *dev, int qidx, bool started) { - uint64_t u64_arg = vmsg->payload.u64; - int index = u64_arg & VHOST_USER_VRING_IDX_MASK; + VuVirtq *vq = vu_get_queue(dev, qidx); - DPRINT("u64: 0x%016"PRIx64"\n", vmsg->payload.u64); - assert((u64_arg & VHOST_USER_VRING_NOFD_MASK) == 0); - assert(vmsg->fd_num == 1); - - if (dev->vq[index].call_fd != -1) { - close(dev->vq[index].call_fd); + if (qidx % 2 == 1) { + vu_set_queue_handler(dev, vq, started ? vubr_handle_tx : NULL); } - dev->vq[index].call_fd = vmsg->fds[0]; - DPRINT("Got call_fd: %d for vq: %d\n", vmsg->fds[0], index); - - return 0; -} - -static int -vubr_set_vring_err_exec(VubrDev *dev, VhostUserMsg *vmsg) -{ - DPRINT("u64: 0x%016"PRIx64"\n", vmsg->payload.u64); - return 0; -} - -static int -vubr_get_protocol_features_exec(VubrDev *dev, VhostUserMsg *vmsg) -{ - vmsg->payload.u64 = 1ULL << VHOST_USER_PROTOCOL_F_LOG_SHMFD; - DPRINT("u64: 0x%016"PRIx64"\n", vmsg->payload.u64); - vmsg->size = sizeof(vmsg->payload.u64); - - /* Reply */ - return 1; } -static int -vubr_set_protocol_features_exec(VubrDev *dev, VhostUserMsg *vmsg) -{ - /* FIXME: unimplented */ - DPRINT("u64: 0x%016"PRIx64"\n", vmsg->payload.u64); - return 0; -} - -static int -vubr_get_queue_num_exec(VubrDev *dev, VhostUserMsg *vmsg) -{ - DPRINT("Function %s() not implemented yet.\n", __func__); - return 0; -} - -static int -vubr_set_vring_enable_exec(VubrDev *dev, VhostUserMsg *vmsg) +static void +vubr_panic(VuDev *dev, const char *msg) { - unsigned int index = vmsg->payload.state.index; - unsigned int enable = vmsg->payload.state.num; + VubrDev *vubr = container_of(dev, VubrDev, vudev); - DPRINT("State.index: %d\n", index); - DPRINT("State.enable: %d\n", enable); - dev->vq[index].enable = enable; - return 0; -} + fprintf(stderr, "PANIC: %s\n", msg); -static int -vubr_send_rarp_exec(VubrDev *dev, VhostUserMsg *vmsg) -{ - DPRINT("Function %s() not implemented yet.\n", __func__); - return 0; + dispatcher_remove(&vubr->dispatcher, dev->sock); + vubr->quit = 1; } -static int -vubr_execute_request(VubrDev *dev, VhostUserMsg *vmsg) -{ - /* Print out generic part of the request. */ - DPRINT( - "================== Vhost user message from QEMU ==================\n"); - DPRINT("Request: %s (%d)\n", vubr_request_str[vmsg->request], - vmsg->request); - DPRINT("Flags: 0x%x\n", vmsg->flags); - DPRINT("Size: %d\n", vmsg->size); - - if (vmsg->fd_num) { - int i; - DPRINT("Fds:"); - for (i = 0; i < vmsg->fd_num; i++) { - DPRINT(" %d", vmsg->fds[i]); - } - DPRINT("\n"); - } - - switch (vmsg->request) { - case VHOST_USER_NONE: - return vubr_none_exec(dev, vmsg); - case VHOST_USER_GET_FEATURES: - return vubr_get_features_exec(dev, vmsg); - case VHOST_USER_SET_FEATURES: - return vubr_set_features_exec(dev, vmsg); - case VHOST_USER_SET_OWNER: - return vubr_set_owner_exec(dev, vmsg); - case VHOST_USER_RESET_OWNER: - return vubr_reset_device_exec(dev, vmsg); - case VHOST_USER_SET_MEM_TABLE: - return vubr_set_mem_table_exec(dev, vmsg); - case VHOST_USER_SET_LOG_BASE: - return vubr_set_log_base_exec(dev, vmsg); - case VHOST_USER_SET_LOG_FD: - return vubr_set_log_fd_exec(dev, vmsg); - case VHOST_USER_SET_VRING_NUM: - return vubr_set_vring_num_exec(dev, vmsg); - case VHOST_USER_SET_VRING_ADDR: - return vubr_set_vring_addr_exec(dev, vmsg); - case VHOST_USER_SET_VRING_BASE: - return vubr_set_vring_base_exec(dev, vmsg); - case VHOST_USER_GET_VRING_BASE: - return vubr_get_vring_base_exec(dev, vmsg); - case VHOST_USER_SET_VRING_KICK: - return vubr_set_vring_kick_exec(dev, vmsg); - case VHOST_USER_SET_VRING_CALL: - return vubr_set_vring_call_exec(dev, vmsg); - case VHOST_USER_SET_VRING_ERR: - return vubr_set_vring_err_exec(dev, vmsg); - case VHOST_USER_GET_PROTOCOL_FEATURES: - return vubr_get_protocol_features_exec(dev, vmsg); - case VHOST_USER_SET_PROTOCOL_FEATURES: - return vubr_set_protocol_features_exec(dev, vmsg); - case VHOST_USER_GET_QUEUE_NUM: - return vubr_get_queue_num_exec(dev, vmsg); - case VHOST_USER_SET_VRING_ENABLE: - return vubr_set_vring_enable_exec(dev, vmsg); - case VHOST_USER_SEND_RARP: - return vubr_send_rarp_exec(dev, vmsg); - - case VHOST_USER_MAX: - assert(vmsg->request != VHOST_USER_MAX); - } - return 0; -} - -static void -vubr_receive_cb(int sock, void *ctx) -{ - VubrDev *dev = (VubrDev *) ctx; - VhostUserMsg vmsg; - int reply_requested; - - vubr_message_read(sock, &vmsg); - reply_requested = vubr_execute_request(dev, &vmsg); - if (reply_requested) { - /* Set the version in the flags when sending the reply */ - vmsg.flags &= ~VHOST_USER_VERSION_MASK; - vmsg.flags |= VHOST_USER_VERSION; - vmsg.flags |= VHOST_USER_REPLY_MASK; - vubr_message_write(sock, &vmsg); - } -} +static const VuDevIface vuiface = { + .get_features = vubr_get_features, + .set_features = vubr_set_features, + .process_msg = vubr_process_msg, + .queue_set_started = vubr_queue_set_started, +}; static void vubr_accept_cb(int sock, void *ctx) @@ -1195,6 +479,14 @@ vubr_accept_cb(int sock, void *ctx) vubr_die("accept()"); } DPRINT("Got connection from remote peer on sock %d\n", conn_fd); + + vu_init(&dev->vudev, + conn_fd, + vubr_panic, + vubr_add_watch, + vubr_remove_watch, + &vuiface); + dispatcher_add(&dev->dispatcher, conn_fd, ctx, vubr_receive_cb); dispatcher_remove(&dev->dispatcher, sock); } @@ -1203,28 +495,9 @@ static VubrDev * vubr_new(const char *path) { VubrDev *dev = (VubrDev *) calloc(1, sizeof(VubrDev)); - dev->nregions = 0; - int i; struct sockaddr_un un; size_t len; - for (i = 0; i < MAX_NR_VIRTQUEUE; i++) { - dev->vq[i] = (VubrVirtq) { - .call_fd = -1, .kick_fd = -1, - .size = 0, - .last_avail_index = 0, .last_used_index = 0, - .desc = 0, .avail = 0, .used = 0, - .enable = 0, - }; - } - - /* Init log */ - dev->log_call_fd = -1; - dev->log_size = 0; - dev->log_table = 0; - dev->ready = 0; - dev->features = 0; - /* Get a UNIX socket. */ dev->sock = socket(AF_UNIX, SOCK_STREAM, 0); if (dev->sock == -1) { @@ -1245,6 +518,7 @@ vubr_new(const char *path) } dispatcher_init(&dev->dispatcher); + dispatcher_add(&dev->dispatcher, dev->sock, (void *)dev, vubr_accept_cb); @@ -1327,7 +601,7 @@ vubr_backend_udp_setup(VubrDev *dev, static void vubr_run(VubrDev *dev) { - while (1) { + while (!dev->quit) { /* timeout 200ms */ dispatcher_wait(&dev->dispatcher, 200000); /* Here one can try polling strategy. */ @@ -1398,6 +672,9 @@ main(int argc, char *argv[]) vubr_backend_udp_setup(dev, lhost, lport, rhost, rport); vubr_run(dev); + + vu_deinit(&dev->vudev); + return 0; out: