From: "Mian M. Hamayun" <m.hamayun@virtualopensystems.com>
To: qemu-devel@nongnu.org, snabb-devel@googlegroups.com
Cc: lukego@gmail.com,
Antonios Motakis <a.motakis@virtualopensystems.com>,
tech@virtualopensystems.com, n.nikolaev@virtualopensystems.com,
"Michael S. Tsirkin" <mst@redhat.com>
Subject: [Qemu-devel] [PATCH v4 5/7] Add vhost-user calls implementation
Date: Fri, 20 Dec 2013 15:10:38 +0100 [thread overview]
Message-ID: <1387548640-7120-6-git-send-email-m.hamayun@virtualopensystems.com> (raw)
In-Reply-To: <1387548640-7120-1-git-send-email-m.hamayun@virtualopensystems.com>
From: Antonios Motakis <a.motakis@virtualopensystems.com>
Each ioctl request of vhost-kernel has a vhost-user message equivalent,
which is sent over the control socket.
The general approach is to copy the data from the supplied argument
pointer to a designated field in the message. If a file descriptor is
to be passed, it should be placed in the fds array for inclusion in
the sendmsg control header.
VHOST_SET_MEM_TABLE ignores the supplied vhost_memory structure and scans
the global ram_list for ram blocks with a valid fd field set. This would
be set when the -mem-path option with shared=on property is used.
Signed-off-by: Antonios Motakis <a.motakis@virtualopensystems.com>
Signed-off-by: Nikolay Nikolaev <n.nikolaev@virtualopensystems.com>
---
hw/virtio/vhost-backend.c | 137 +++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 134 insertions(+), 3 deletions(-)
diff --git a/hw/virtio/vhost-backend.c b/hw/virtio/vhost-backend.c
index 96d3bf0..17f59ec 100644
--- a/hw/virtio/vhost-backend.c
+++ b/hw/virtio/vhost-backend.c
@@ -19,6 +19,7 @@
#include <linux/vhost.h>
#define VHOST_MEMORY_MAX_NREGIONS 8
+#define VHOST_USER_SOCKTO (1000) /* msec */
typedef enum VhostUserRequest {
VHOST_USER_NONE = 0,
@@ -66,6 +67,40 @@ typedef struct VhostUserMsg {
};
} VhostUserMsg;
+static unsigned long int ioctl_to_vhost_user_request[VHOST_USER_MAX] = {
+ -1, /* VHOST_USER_NONE */
+ VHOST_GET_FEATURES, /* VHOST_USER_GET_FEATURES */
+ VHOST_SET_FEATURES, /* VHOST_USER_SET_FEATURES */
+ VHOST_SET_OWNER, /* VHOST_USER_SET_OWNER */
+ VHOST_RESET_OWNER, /* VHOST_USER_RESET_OWNER */
+ VHOST_SET_MEM_TABLE, /* VHOST_USER_SET_MEM_TABLE */
+ VHOST_SET_LOG_BASE, /* VHOST_USER_SET_LOG_BASE */
+ VHOST_SET_LOG_FD, /* VHOST_USER_SET_LOG_FD */
+ VHOST_SET_VRING_NUM, /* VHOST_USER_SET_VRING_NUM */
+ VHOST_SET_VRING_ADDR, /* VHOST_USER_SET_VRING_ADDR */
+ VHOST_SET_VRING_BASE, /* VHOST_USER_SET_VRING_BASE */
+ VHOST_GET_VRING_BASE, /* VHOST_USER_GET_VRING_BASE */
+ VHOST_SET_VRING_KICK, /* VHOST_USER_SET_VRING_KICK */
+ VHOST_SET_VRING_CALL, /* VHOST_USER_SET_VRING_CALL */
+ VHOST_SET_VRING_ERR, /* VHOST_USER_SET_VRING_ERR */
+ VHOST_NET_SET_BACKEND /* VHOST_USER_NET_SET_BACKEND */
+};
+
+static int vhost_user_cleanup(struct vhost_dev *dev);
+
+static VhostUserRequest vhost_user_request_translate(unsigned long int request)
+{
+ VhostUserRequest idx;
+
+ for (idx = 0; idx < VHOST_USER_MAX; idx++) {
+ if (ioctl_to_vhost_user_request[idx] == request) {
+ break;
+ }
+ }
+
+ return (idx == VHOST_USER_MAX) ? VHOST_USER_NONE : idx;
+}
+
static int vhost_user_recv(int fd, VhostUserMsg *msg)
{
ssize_t r = read(fd, msg, sizeof(VhostUserMsg));
@@ -129,13 +164,74 @@ static int vhost_user_call(struct vhost_dev *dev, unsigned long int request,
{
int fd = dev->control;
VhostUserMsg msg;
+ RAMBlock *block = 0;
int result = 0, need_reply = 0;
int fds[VHOST_MEMORY_MAX_NREGIONS];
size_t fd_num = 0;
assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER);
+ msg.request = vhost_user_request_translate(request);
+ msg.flags = 0;
+
switch (request) {
+ case VHOST_GET_FEATURES:
+ case VHOST_GET_VRING_BASE:
+ need_reply = 1;
+ break;
+
+ case VHOST_SET_FEATURES:
+ case VHOST_SET_LOG_BASE:
+ msg.u64 = *((uint64_t *) arg);
+ break;
+
+ case VHOST_SET_OWNER:
+ case VHOST_RESET_OWNER:
+ break;
+
+ case VHOST_SET_MEM_TABLE:
+ QTAILQ_FOREACH(block, &ram_list.blocks, next)
+ {
+ if (block->fd > 0) {
+ msg.memory.regions[fd_num].userspace_addr = (__u64) block->host;
+ msg.memory.regions[fd_num].memory_size = block->length;
+ msg.memory.regions[fd_num].guest_phys_addr = block->offset;
+ fds[fd_num++] = block->fd;
+ }
+ }
+
+ msg.memory.nregions = fd_num;
+
+ if (!fd_num) {
+ fprintf(stderr, "Failed initializing vhost-user memory map\n"
+ "consider -mem-path and -mem-prealloc options\n");
+ return -1;
+ }
+ break;
+
+ case VHOST_SET_LOG_FD:
+ msg.fd = *((int *) arg);
+ break;
+
+ case VHOST_SET_VRING_NUM:
+ case VHOST_SET_VRING_BASE:
+ memcpy(&msg.state, arg, sizeof(struct vhost_vring_state));
+ break;
+
+ case VHOST_SET_VRING_ADDR:
+ memcpy(&msg.addr, arg, sizeof(struct vhost_vring_addr));
+ break;
+
+ case VHOST_SET_VRING_KICK:
+ case VHOST_SET_VRING_CALL:
+ case VHOST_SET_VRING_ERR:
+ case VHOST_NET_SET_BACKEND:
+ memcpy(&msg.file, arg, sizeof(struct vhost_vring_file));
+ if (msg.file.fd > 0) {
+ fds[0] = msg.file.fd;
+ fd_num = 1;
+ }
+ break;
default:
fprintf(stderr, "vhost-user trying to send unhandled ioctl\n");
return -1;
@@ -148,7 +244,11 @@ static int vhost_user_call(struct vhost_dev *dev, unsigned long int request,
result = vhost_user_recv(fd, &msg);
if (!result) {
switch (request) {
- default:
+ case VHOST_GET_FEATURES:
+ *((uint64_t *) arg) = msg.u64;
+ break;
+ case VHOST_GET_VRING_BASE:
+ memcpy(arg, &msg.state, sizeof(struct vhost_vring_state));
break;
}
}
@@ -161,6 +261,7 @@ static int vhost_user_init(struct vhost_dev *dev, const char *devpath)
{
int fd = -1;
struct sockaddr_un un;
+ struct timeval tv;
size_t len;
assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER);
@@ -180,19 +281,49 @@ static int vhost_user_init(struct vhost_dev *dev, const char *devpath)
/* Connect */
if (connect(fd, (struct sockaddr *) &un, len) == -1) {
perror("connect");
- return -1;
+ goto fail;
}
+ /* Set socket options */
+ tv.tv_sec = VHOST_USER_SOCKTO/1000;
+ tv.tv_usec = (VHOST_USER_SOCKTO%1000)*1000*1000;
+
+ if (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(struct timeval))
+ == -1) {
+ perror("setsockopt SO_SNDTIMEO");
+ goto fail;
+ }
+
+ if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval))
+ == -1) {
+ perror("setsockopt SO_RCVTIMEO");
+ goto fail;
+ }
+
+ /* cleanup if there is previous connection left */
+ if (dev->control >= 0) {
+ vhost_user_cleanup(dev);
+ }
dev->control = fd;
return fd;
+
+fail:
+ close(fd);
+ return -1;
+
}
static int vhost_user_cleanup(struct vhost_dev *dev)
{
+ int r;
+
assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER);
- return close(dev->control);
+ r = close(dev->control);
+ dev->control = -1;
+
+ return r;
}
static const VhostOps user_ops = {
--
1.8.3.2
next prev parent reply other threads:[~2013-12-20 14:11 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-12-20 14:10 [Qemu-devel] [PATCH v4 0/7] Vhost and vhost-net support for userspace based backends Mian M. Hamayun
2013-12-20 14:10 ` [Qemu-devel] [PATCH v4 1/7] Convert -mem-path to QemuOpts and add prealloc, share and unlink properties Mian M. Hamayun
2013-12-20 14:10 ` [Qemu-devel] [PATCH v4 2/7] Decouple vhost from kernel interface Mian M. Hamayun
2013-12-20 14:10 ` [Qemu-devel] [PATCH v4 3/7] Add vhost-user skeleton Mian M. Hamayun
2013-12-20 14:10 ` [Qemu-devel] [PATCH v4 4/7] Add domain socket communication for vhost-user backend Mian M. Hamayun
2013-12-23 16:39 ` Michael S. Tsirkin
2013-12-20 14:10 ` Mian M. Hamayun [this message]
2013-12-20 14:10 ` [Qemu-devel] [PATCH v4 6/7] Add new vhost-user netdev backend Mian M. Hamayun
2013-12-20 14:10 ` [Qemu-devel] [PATCH v4 7/7] Add vhost-user reconnection Mian M. Hamayun
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1387548640-7120-6-git-send-email-m.hamayun@virtualopensystems.com \
--to=m.hamayun@virtualopensystems.com \
--cc=a.motakis@virtualopensystems.com \
--cc=lukego@gmail.com \
--cc=mst@redhat.com \
--cc=n.nikolaev@virtualopensystems.com \
--cc=qemu-devel@nongnu.org \
--cc=snabb-devel@googlegroups.com \
--cc=tech@virtualopensystems.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).