From patchwork Sat Jun 4 21:33:15 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: 9154991 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 6CC3D60574 for ; Sat, 4 Jun 2016 21:47:34 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5DB7327F2B for ; Sat, 4 Jun 2016 21:47:34 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5259A28327; Sat, 4 Jun 2016 21:47:34 +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 6EE8E27F2B for ; Sat, 4 Jun 2016 21:47:33 +0000 (UTC) Received: from localhost ([::1]:34269 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b9JPo-0001uo-HN for patchwork-qemu-devel@patchwork.kernel.org; Sat, 04 Jun 2016 17:47:32 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:44781) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b9JCa-00063q-JI for qemu-devel@nongnu.org; Sat, 04 Jun 2016 17:34:01 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1b9JCQ-0005pZ-Cu for qemu-devel@nongnu.org; Sat, 04 Jun 2016 17:33:51 -0400 Received: from mail-wm0-x242.google.com ([2a00:1450:400c:c09::242]:34814) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b9JCP-0005pV-W6 for qemu-devel@nongnu.org; Sat, 04 Jun 2016 17:33:42 -0400 Received: by mail-wm0-x242.google.com with SMTP id n184so7287637wmn.1 for ; Sat, 04 Jun 2016 14:33:41 -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=HEhplPb7BHgeVkf8C0DMnnn53CzFYgeHjQgR1LGmWKw=; b=M1o7IsBU/J/inxrOipjjmXvgAstFEJcS7mtuMeVTnR3oKlvUaemOt45198JT5/vrF0 0UsknCd2BHxMkqgxJmGwyGbumAUnb/0/yX3AVv96m0OpVjqevA5gbbI6r7nAmye24x/O CrM8eJcYMwX2qZ0lStAdiJa3n1aYxwPaNq+1ie3xTwcTDb04mS1CuYKXau0YYE3oJw0i qSoZ4zrxSTeIP4FlcQ9O7UGmPZkEylg01LpMiF1Rk+LamR6gfBaKciqLrW3AxkhcReBk GLOKNKe7RzxCcKffvsrWQaSwElzIPyd1RunrWJUx8Leuq97GI03krXoT9CloDQVj8neg md+w== 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=HEhplPb7BHgeVkf8C0DMnnn53CzFYgeHjQgR1LGmWKw=; b=JKrIoxtG6M2JiE5jqj9CpxNscceIWMhsKW73mDFodaaJrQoefW3c2ge4AuIq+QxLD8 p8VAGsI7BCHb2am6Q6yLomhtEND28TFztY58NdDGDhI3887XSO0cTGA1ITaPm75EExAj Lyj1Jd2GuYKYKayPuCDrJ8BozILjS8FowDBNuUgmKTkD9NADu45qe/FHw+hyb50FIPC0 wmmZ0ciVtn+cFdI/T6rJgBf2JDszgkArtCiKuHCA55Bcj5IUe+SDs+RkVa1OZjsh+biA znXs4qjtY4KRWjQeeC9ZNuu6QFhvsjdh5simZhOAT6/eVck1dCb1BruHbs0eznVJYK78 nBRg== X-Gm-Message-State: ALyK8tJJpCCjuBvQhM3U7FrDuzrJSG1QagnA2UI9rIwiMh9iZuh4bgIU9kSnMX2l5m2fwA== X-Received: by 10.195.18.165 with SMTP id gn5mr5703576wjd.123.1465076021262; Sat, 04 Jun 2016 14:33: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 a128sm6245625wme.16.2016.06.04.14.33.38 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 04 Jun 2016 14:33:40 -0700 (PDT) From: marcandre.lureau@redhat.com To: qemu-devel@nongnu.org Date: Sat, 4 Jun 2016 23:33:15 +0200 Message-Id: <1465076003-26291-7-git-send-email-marcandre.lureau@redhat.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1465076003-26291-1-git-send-email-marcandre.lureau@redhat.com> References: <1465076003-26291-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] [RFC 06/14] contrib: add vhost-user-input 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?= , kraxel@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 Add a vhost-user input backend example, based on virtio-input-host device. It takes an evdev path as argument, and can be associated with a vhost-user-backend object, ex: -object vhost-user-backend,id=vuid,cmd="vhost-user-input /dev/input/event0" Signed-off-by: Marc-André Lureau --- Makefile | 3 + Makefile.objs | 1 + configure | 1 + contrib/vhost-user-input/Makefile.objs | 1 + contrib/vhost-user-input/main.c | 369 +++++++++++++++++++++++++++++++++ 5 files changed, 375 insertions(+) create mode 100644 contrib/vhost-user-input/Makefile.objs create mode 100644 contrib/vhost-user-input/main.c diff --git a/Makefile b/Makefile index 251217c..ae054de 100644 --- a/Makefile +++ b/Makefile @@ -152,6 +152,7 @@ dummy := $(call unnest-vars,, \ ivshmem-client-obj-y \ ivshmem-server-obj-y \ libvhost-user-obj-y \ + vhost-user-input-obj-y \ qga-vss-dll-obj-y \ block-obj-y \ block-obj-m \ @@ -332,6 +333,8 @@ ivshmem-client$(EXESUF): $(ivshmem-client-obj-y) libqemuutil.a libqemustub.a $(call LINK, $^) ivshmem-server$(EXESUF): $(ivshmem-server-obj-y) libqemuutil.a libqemustub.a $(call LINK, $^) +vhost-user-input$(EXESUF): $(vhost-user-input-obj-y) $(libvhost-user-obj-y) libqemuutil.a libqemustub.a + $(call LINK, $^) clean: # avoid old build problems by removing potentially incorrect old files diff --git a/Makefile.objs b/Makefile.objs index 5812da7..cdd48ca 100644 --- a/Makefile.objs +++ b/Makefile.objs @@ -116,3 +116,4 @@ qga-vss-dll-obj-y = qga/ ivshmem-client-obj-y = contrib/ivshmem-client/ ivshmem-server-obj-y = contrib/ivshmem-server/ libvhost-user-obj-y = contrib/libvhost-user/ +vhost-user-input-obj-y = contrib/vhost-user-input/ diff --git a/configure b/configure index b5aab72..b02c0f4 100755 --- a/configure +++ b/configure @@ -4602,6 +4602,7 @@ if test "$want_tools" = "yes" ; then if [ "$linux" = "yes" -o "$bsd" = "yes" -o "$solaris" = "yes" ] ; then tools="qemu-nbd\$(EXESUF) $tools" tools="ivshmem-client\$(EXESUF) ivshmem-server\$(EXESUF) $tools" + tools="vhost-user-input\$(EXESUF) $tools" fi fi if test "$softmmu" = yes ; then diff --git a/contrib/vhost-user-input/Makefile.objs b/contrib/vhost-user-input/Makefile.objs new file mode 100644 index 0000000..b1fad90 --- /dev/null +++ b/contrib/vhost-user-input/Makefile.objs @@ -0,0 +1 @@ +vhost-user-input-obj-y = main.o diff --git a/contrib/vhost-user-input/main.c b/contrib/vhost-user-input/main.c new file mode 100644 index 0000000..31eecb8 --- /dev/null +++ b/contrib/vhost-user-input/main.c @@ -0,0 +1,369 @@ +#include +#include + +#include "qemu/osdep.h" +#include "qemu/iov.h" +#include "qemu/bswap.h" +#include "contrib/libvhost-user/libvhost-user.h" +#include "standard-headers/linux/virtio_input.h" + +typedef struct virtio_input_event virtio_input_event; +typedef struct virtio_input_config virtio_input_config; + +typedef struct VuInput { + VuDev dev; + GSource *watches[16]; + int evdevfd; + GArray *config; + virtio_input_event *queue; + uint32_t qindex, qsize; +} VuInput; + +static void vi_input_send(VuInput *vi, struct virtio_input_event *event) +{ + VuDev *dev = &vi->dev; + VuVirtq *vq = vu_get_queue(dev, 0); + VuVirtqElement *elem; + unsigned have, need; + int i, len; + + /* queue up events ... */ + if (vi->qindex == vi->qsize) { + vi->qsize++; + vi->queue = realloc(vi->queue, vi->qsize * + sizeof(virtio_input_event)); + } + vi->queue[vi->qindex++] = *event; + + /* ... until we see a report sync ... */ + if (event->type != htole16(EV_SYN) || + event->code != htole16(SYN_REPORT)) { + return; + } + + /* ... then check available space ... */ + need = sizeof(virtio_input_event) * vi->qindex; + vu_queue_get_avail_bytes(dev, vq, &have, NULL, need, 0); + if (have < need) { + vi->qindex = 0; + g_warning("ENOSPC in vq, dropping events"); + return; + } + + /* ... and finally pass them to the guest */ + for (i = 0; i < vi->qindex; i++) { + elem = vu_queue_pop(dev, vq, sizeof(VuVirtqElement)); + if (!elem) { + /* should not happen, we've checked for space beforehand */ + g_warning("%s: Huh? No vq elem available ...\n", __func__); + return; + } + len = iov_from_buf(elem->in_sg, elem->in_num, + 0, vi->queue + i, sizeof(virtio_input_event)); + vu_queue_push(dev, vq, elem, len); + g_free(elem); + } + vu_queue_notify(&vi->dev, vq); + vi->qindex = 0; +} + +static void +vi_evdev_watch(VuDev *dev, int condition, void *data) +{ + VuInput *vi = data; + int fd = vi->evdevfd; + + g_debug("Got evdev condition %x", condition); + + struct virtio_input_event virtio; + struct input_event evdev; + int rc; + + for (;;) { + rc = read(fd, &evdev, sizeof(evdev)); + if (rc != sizeof(evdev)) { + break; + } + + g_debug("input %d %d %d", evdev.type, evdev.code, evdev.value); + + virtio.type = htole16(evdev.type); + virtio.code = htole16(evdev.code); + virtio.value = htole32(evdev.value); + vi_input_send(vi, &virtio); + } +} + +static void vi_handle_sts(VuDev *dev, int qidx) +{ + VuInput *vi = container_of(dev, VuInput, dev); + VuVirtq *vq = vu_get_queue(dev, qidx); + virtio_input_event event; + VuVirtqElement *elem; + int len; + + g_debug("%s", __func__); + + for (;;) { + elem = vu_queue_pop(dev, vq, sizeof(VuVirtqElement)); + if (!elem) { + break; + } + + memset(&event, 0, sizeof(event)); + len = iov_to_buf(elem->out_sg, elem->out_num, + 0, &event, sizeof(event)); + g_debug("TODO handle status %d %p", len, elem); + vu_queue_push(dev, vq, elem, len); + g_free(elem); + } + + vu_queue_notify(&vi->dev, vq); +} + +static void +vi_panic(VuDev *dev, const char *msg) +{ + g_critical("%s\n", msg); + exit(1); +} + +typedef struct Watch { + GSource source; + GIOCondition condition; + gpointer tag; + VuDev *dev; + guint id; +} Watch; + +static GIOCondition +vu_to_gio_condition(int condition) +{ + return (condition & VU_WATCH_IN ? G_IO_IN : 0) | + (condition & VU_WATCH_OUT ? G_IO_OUT : 0) | + (condition & VU_WATCH_PRI ? G_IO_PRI : 0) | + (condition & VU_WATCH_ERR ? G_IO_ERR : 0) | + (condition & VU_WATCH_HUP ? G_IO_HUP : 0); +} + +static GIOCondition +vu_from_gio_condition(int condition) +{ + return (condition & G_IO_IN ? VU_WATCH_IN : 0) | + (condition & G_IO_OUT ? VU_WATCH_OUT : 0) | + (condition & G_IO_PRI ? VU_WATCH_PRI : 0) | + (condition & G_IO_ERR ? VU_WATCH_ERR : 0) | + (condition & G_IO_HUP ? VU_WATCH_HUP : 0); +} + +static gboolean +watch_check(GSource *source) +{ + Watch *watch = (Watch *)source; + GIOCondition poll_condition = g_source_query_unix_fd(source, watch->tag); + + return poll_condition & watch->condition; +} + +static gboolean +watch_dispatch(GSource *source, + GSourceFunc callback, + gpointer user_data) + +{ + vu_watch_cb func = (vu_watch_cb)callback; + Watch *watch = (Watch *)source; + GIOCondition poll_condition = g_source_query_unix_fd(source, watch->tag); + int cond = vu_from_gio_condition(poll_condition & watch->condition); + + (*func) (watch->dev, cond, user_data); + + return G_SOURCE_CONTINUE; +} + +static GSourceFuncs watch_funcs = { + .check = watch_check, + .dispatch = watch_dispatch, +}; + +static void +set_fd_handler(VuDev *dev, int fd, GIOCondition condition, + vu_watch_cb cb, void *data) +{ + VuInput *vi = container_of(dev, VuInput, dev); + Watch *watch; + GSource *s; + + g_assert_cmpint(fd, <, G_N_ELEMENTS(vi->watches)); + + s = vi->watches[fd]; + if (cb) { + if (!s) { + s = g_source_new(&watch_funcs, sizeof(Watch)); + watch = (Watch *)s; + watch->dev = dev; + watch->condition = condition; + watch->tag = + g_source_add_unix_fd(s, fd, condition); + watch->id = g_source_attach(s, NULL); + vi->watches[fd] = s; + } else { + watch = (Watch *)s; + g_source_modify_unix_fd(s, watch->tag, condition); + } + + g_source_set_callback(s, (GSourceFunc)cb, data, NULL); + } else if (s) { + watch = (Watch *)s; + g_source_remove_unix_fd(s, watch->tag); + g_source_unref(s); + g_source_remove(watch->id); + vi->watches[fd] = NULL; + } +} + +static void +vi_add_watch(VuDev *dev, int fd, int condition, + vu_watch_cb cb, void *data) +{ + set_fd_handler(dev, fd, vu_to_gio_condition(condition), cb, data); +} + +static void +vi_remove_watch(VuDev *dev, int fd) +{ + set_fd_handler(dev, fd, 0, NULL, NULL); +} + +static void +vi_queue_set_started(VuDev *dev, int qidx, bool started) +{ + VuInput *vi = container_of(dev, VuInput, dev); + VuVirtq *vq = vu_get_queue(dev, qidx); + + g_debug("queue started %d:%d", qidx, started); + + if (qidx == 0) { + set_fd_handler(dev, vi->evdevfd, G_IO_IN, + started ? vi_evdev_watch : NULL, vi); + } else { + vu_set_queue_handler(dev, vq, started ? vi_handle_sts : NULL); + } +} + +static void +vi_vhost_watch(VuDev *dev, int condition, void *data) +{ + vu_dispatch(dev); +} + +static int +vi_process_msg(VuDev *dev, VhostUserMsg *vmsg, int *do_reply) +{ + VuInput *vi = container_of(dev, VuInput, dev); + + switch (vmsg->request) { + case VHOST_USER_INPUT_GET_CONFIG: + vmsg->size = vi->config->len * sizeof(virtio_input_config); + vmsg->data = g_memdup(vi->config->data, vmsg->size); + *do_reply = true; + return 1; + default: + return 0; + } +} + +static const VuDevIface vuiface = { + .queue_set_started = vi_queue_set_started, + .process_msg = vi_process_msg, +}; + +static void +vi_bits_config(VuInput *vi, int type, int count) +{ + virtio_input_config bits; + int rc, i, size = 0; + + memset(&bits, 0, sizeof(bits)); + rc = ioctl(vi->evdevfd, EVIOCGBIT(type, count / 8), bits.u.bitmap); + if (rc < 0) { + return; + } + + for (i = 0; i < count / 8; i++) { + if (bits.u.bitmap[i]) { + size = i + 1; + } + } + if (size == 0) { + return; + } + + bits.select = VIRTIO_INPUT_CFG_EV_BITS; + bits.subsel = type; + bits.size = size; + g_array_append_val(vi->config, bits); +} + +int +main(int argc, char *argv[]) +{ + GMainLoop *loop = NULL; + VuInput vi = { 0, }; + int rc, ver; + virtio_input_config id; + struct input_id ids; + + if (argc != 2) { + g_error("evdev path argument required"); + } + + vi.evdevfd = open(argv[1], O_RDWR); + if (vi.evdevfd < 0) { + g_error("Failed to open evdev: %s", g_strerror(errno)); + } + + rc = ioctl(vi.evdevfd, EVIOCGVERSION, &ver); + if (rc < 0) { + g_error("%s: is not an evdev device", argv[1]); + } + + rc = ioctl(vi.evdevfd, EVIOCGRAB, 1); + if (rc < 0) { + g_error("Failed to grab device"); + } + + vi.config = g_array_new(false, false, sizeof(virtio_input_config)); + memset(&id, 0, sizeof(id)); + ioctl(vi.evdevfd, EVIOCGNAME(sizeof(id.u.string) - 1), id.u.string); + id.select = VIRTIO_INPUT_CFG_ID_NAME; + id.size = strlen(id.u.string); + g_array_append_val(vi.config, id); + + if (ioctl(vi.evdevfd, EVIOCGID, &ids) == 0) { + memset(&id, 0, sizeof(id)); + id.select = VIRTIO_INPUT_CFG_ID_DEVIDS; + id.size = sizeof(struct virtio_input_devids); + id.u.ids.bustype = cpu_to_le16(ids.bustype); + id.u.ids.vendor = cpu_to_le16(ids.vendor); + id.u.ids.product = cpu_to_le16(ids.product); + id.u.ids.version = cpu_to_le16(ids.version); + g_array_append_val(vi.config, id); + } + + vi_bits_config(&vi, EV_KEY, KEY_CNT); + vi_bits_config(&vi, EV_REL, REL_CNT); + vi_bits_config(&vi, EV_ABS, ABS_CNT); + vi_bits_config(&vi, EV_MSC, MSC_CNT); + vi_bits_config(&vi, EV_SW, SW_CNT); + g_debug("config length: %u", vi.config->len); + + vu_init(&vi.dev, 3, vi_panic, vi_add_watch, vi_remove_watch, &vuiface); + set_fd_handler(&vi.dev, 3, G_IO_IN | G_IO_HUP, vi_vhost_watch, NULL); + + loop = g_main_loop_new(NULL, FALSE); + g_main_loop_run(loop); + g_main_loop_unref(loop); + + return 0; +}