2017-05-30 21:13:50 +00:00
|
|
|
/*-
|
|
|
|
* BSD LICENSE
|
|
|
|
*
|
|
|
|
* Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* * Neither the name of Intel Corporation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2017-10-18 17:14:30 +00:00
|
|
|
#include "spdk/stdinc.h"
|
2017-05-30 21:13:50 +00:00
|
|
|
|
2020-06-03 07:52:12 +00:00
|
|
|
#include "vhost_user.h"
|
2017-05-30 21:13:50 +00:00
|
|
|
|
2017-10-16 21:46:01 +00:00
|
|
|
#include "spdk/string.h"
|
2019-02-25 07:38:15 +00:00
|
|
|
#include "spdk_internal/vhost_user.h"
|
2017-10-16 21:46:01 +00:00
|
|
|
|
2017-05-30 21:13:50 +00:00
|
|
|
/* The version of the protocol we support */
|
|
|
|
#define VHOST_USER_VERSION 0x1
|
|
|
|
|
|
|
|
static int
|
|
|
|
vhost_user_write(int fd, void *buf, int len, int *fds, int fd_num)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
struct msghdr msgh;
|
|
|
|
struct iovec iov;
|
|
|
|
size_t fd_size = fd_num * sizeof(int);
|
|
|
|
char control[CMSG_SPACE(fd_size)];
|
|
|
|
struct cmsghdr *cmsg;
|
|
|
|
|
|
|
|
memset(&msgh, 0, sizeof(msgh));
|
|
|
|
memset(control, 0, sizeof(control));
|
|
|
|
|
|
|
|
iov.iov_base = (uint8_t *)buf;
|
|
|
|
iov.iov_len = len;
|
|
|
|
|
|
|
|
msgh.msg_iov = &iov;
|
|
|
|
msgh.msg_iovlen = 1;
|
|
|
|
|
|
|
|
if (fds && fd_num > 0) {
|
|
|
|
msgh.msg_control = control;
|
|
|
|
msgh.msg_controllen = sizeof(control);
|
|
|
|
cmsg = CMSG_FIRSTHDR(&msgh);
|
|
|
|
cmsg->cmsg_len = CMSG_LEN(fd_size);
|
|
|
|
cmsg->cmsg_level = SOL_SOCKET;
|
|
|
|
cmsg->cmsg_type = SCM_RIGHTS;
|
|
|
|
memcpy(CMSG_DATA(cmsg), fds, fd_size);
|
|
|
|
} else {
|
|
|
|
msgh.msg_control = NULL;
|
|
|
|
msgh.msg_controllen = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
r = sendmsg(fd, &msgh, 0);
|
|
|
|
} while (r < 0 && errno == EINTR);
|
|
|
|
|
2018-07-11 16:54:35 +00:00
|
|
|
if (r == -1) {
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2017-05-30 21:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
vhost_user_read(int fd, struct vhost_user_msg *msg)
|
|
|
|
{
|
|
|
|
uint32_t valid_flags = VHOST_USER_REPLY_MASK | VHOST_USER_VERSION;
|
2017-10-24 19:21:02 +00:00
|
|
|
ssize_t ret;
|
|
|
|
size_t sz_hdr = VHOST_USER_HDR_SIZE, sz_payload;
|
2017-05-30 21:13:50 +00:00
|
|
|
|
|
|
|
ret = recv(fd, (void *)msg, sz_hdr, 0);
|
2017-10-24 19:21:02 +00:00
|
|
|
if ((size_t)ret != sz_hdr) {
|
|
|
|
SPDK_WARNLOG("Failed to recv msg hdr: %zd instead of %zu.\n",
|
2017-10-18 17:06:51 +00:00
|
|
|
ret, sz_hdr);
|
2018-07-11 16:54:35 +00:00
|
|
|
if (ret == -1) {
|
|
|
|
return -errno;
|
|
|
|
} else {
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
2017-05-30 21:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* validate msg flags */
|
|
|
|
if (msg->flags != (valid_flags)) {
|
2017-10-14 10:43:58 +00:00
|
|
|
SPDK_WARNLOG("Failed to recv msg: flags %"PRIx32" instead of %"PRIx32".\n",
|
|
|
|
msg->flags, valid_flags);
|
2018-07-11 16:54:35 +00:00
|
|
|
return -EIO;
|
2017-05-30 21:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sz_payload = msg->size;
|
2017-10-24 19:27:03 +00:00
|
|
|
|
2019-02-25 07:44:21 +00:00
|
|
|
if (sz_payload > VHOST_USER_PAYLOAD_SIZE) {
|
2017-10-24 19:27:03 +00:00
|
|
|
SPDK_WARNLOG("Received oversized msg: payload size %zu > available space %zu\n",
|
2019-02-25 07:44:21 +00:00
|
|
|
sz_payload, VHOST_USER_PAYLOAD_SIZE);
|
2018-07-11 16:54:35 +00:00
|
|
|
return -EIO;
|
2017-10-24 19:27:03 +00:00
|
|
|
}
|
|
|
|
|
2017-05-30 21:13:50 +00:00
|
|
|
if (sz_payload) {
|
|
|
|
ret = recv(fd, (void *)((char *)msg + sz_hdr), sz_payload, 0);
|
2017-10-24 19:21:02 +00:00
|
|
|
if ((size_t)ret != sz_payload) {
|
|
|
|
SPDK_WARNLOG("Failed to recv msg payload: %zd instead of %"PRIu32".\n",
|
2017-10-14 10:43:58 +00:00
|
|
|
ret, msg->size);
|
2018-07-11 16:54:35 +00:00
|
|
|
if (ret == -1) {
|
|
|
|
return -errno;
|
|
|
|
} else {
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
2017-05-30 21:13:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct hugepage_file_info {
|
|
|
|
uint64_t addr; /**< virtual addr */
|
|
|
|
size_t size; /**< the file size */
|
|
|
|
char path[PATH_MAX]; /**< path to backing file */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Two possible options:
|
|
|
|
* 1. Match HUGEPAGE_INFO_FMT to find the file storing struct hugepage_file
|
|
|
|
* array. This is simple but cannot be used in secondary process because
|
|
|
|
* secondary process will close and munmap that file.
|
|
|
|
* 2. Match HUGEFILE_FMT to find hugepage files directly.
|
|
|
|
*
|
|
|
|
* We choose option 2.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
get_hugepage_file_info(struct hugepage_file_info huges[], int max)
|
|
|
|
{
|
2018-07-11 16:54:35 +00:00
|
|
|
int idx, rc;
|
2017-05-30 21:13:50 +00:00
|
|
|
FILE *f;
|
|
|
|
char buf[BUFSIZ], *tmp, *tail;
|
|
|
|
char *str_underline, *str_start;
|
|
|
|
int huge_index;
|
|
|
|
uint64_t v_start, v_end;
|
|
|
|
|
|
|
|
f = fopen("/proc/self/maps", "r");
|
|
|
|
if (!f) {
|
2017-10-14 10:43:58 +00:00
|
|
|
SPDK_ERRLOG("cannot open /proc/self/maps\n");
|
2018-07-11 16:54:35 +00:00
|
|
|
rc = -errno;
|
|
|
|
assert(rc < 0); /* scan-build hack */
|
|
|
|
return rc;
|
2017-05-30 21:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
idx = 0;
|
|
|
|
while (fgets(buf, sizeof(buf), f) != NULL) {
|
|
|
|
if (sscanf(buf, "%" PRIx64 "-%" PRIx64, &v_start, &v_end) < 2) {
|
2017-10-14 10:43:58 +00:00
|
|
|
SPDK_ERRLOG("Failed to parse address\n");
|
2018-07-11 16:54:35 +00:00
|
|
|
rc = -EIO;
|
|
|
|
goto out;
|
2017-05-30 21:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tmp = strchr(buf, ' ') + 1; /** skip address */
|
|
|
|
tmp = strchr(tmp, ' ') + 1; /** skip perm */
|
|
|
|
tmp = strchr(tmp, ' ') + 1; /** skip offset */
|
|
|
|
tmp = strchr(tmp, ' ') + 1; /** skip dev */
|
|
|
|
tmp = strchr(tmp, ' ') + 1; /** skip inode */
|
2017-12-07 23:23:48 +00:00
|
|
|
while (*tmp == ' ') { /** skip spaces */
|
2017-05-30 21:13:50 +00:00
|
|
|
tmp++;
|
2017-12-07 23:23:48 +00:00
|
|
|
}
|
2017-05-30 21:13:50 +00:00
|
|
|
tail = strrchr(tmp, '\n'); /** remove newline if exists */
|
2017-12-07 23:23:48 +00:00
|
|
|
if (tail) {
|
2017-05-30 21:13:50 +00:00
|
|
|
*tail = '\0';
|
2017-12-07 23:23:48 +00:00
|
|
|
}
|
2017-05-30 21:13:50 +00:00
|
|
|
|
|
|
|
/* Match HUGEFILE_FMT, aka "%s/%smap_%d",
|
|
|
|
* which is defined in eal_filesystem.h
|
|
|
|
*/
|
|
|
|
str_underline = strrchr(tmp, '_');
|
2017-12-07 23:23:48 +00:00
|
|
|
if (!str_underline) {
|
2017-05-30 21:13:50 +00:00
|
|
|
continue;
|
2017-12-07 23:23:48 +00:00
|
|
|
}
|
2017-05-30 21:13:50 +00:00
|
|
|
|
|
|
|
str_start = str_underline - strlen("map");
|
2017-12-07 23:23:48 +00:00
|
|
|
if (str_start < tmp) {
|
2017-05-30 21:13:50 +00:00
|
|
|
continue;
|
2017-12-07 23:23:48 +00:00
|
|
|
}
|
2017-05-30 21:13:50 +00:00
|
|
|
|
2017-12-07 23:23:48 +00:00
|
|
|
if (sscanf(str_start, "map_%d", &huge_index) != 1) {
|
2017-05-30 21:13:50 +00:00
|
|
|
continue;
|
2017-12-07 23:23:48 +00:00
|
|
|
}
|
2017-05-30 21:13:50 +00:00
|
|
|
|
|
|
|
if (idx >= max) {
|
2017-10-14 10:43:58 +00:00
|
|
|
SPDK_ERRLOG("Exceed maximum of %d\n", max);
|
2018-07-11 16:54:35 +00:00
|
|
|
rc = -ENOSPC;
|
|
|
|
goto out;
|
2017-05-30 21:13:50 +00:00
|
|
|
}
|
2018-05-31 22:49:49 +00:00
|
|
|
|
|
|
|
if (idx > 0 &&
|
|
|
|
strncmp(tmp, huges[idx - 1].path, PATH_MAX) == 0 &&
|
|
|
|
v_start == huges[idx - 1].addr + huges[idx - 1].size) {
|
|
|
|
huges[idx - 1].size += (v_end - v_start);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-05-30 21:13:50 +00:00
|
|
|
huges[idx].addr = v_start;
|
|
|
|
huges[idx].size = v_end - v_start;
|
|
|
|
snprintf(huges[idx].path, PATH_MAX, "%s", tmp);
|
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
|
2018-07-11 16:54:35 +00:00
|
|
|
rc = idx;
|
|
|
|
out:
|
2017-05-30 21:13:50 +00:00
|
|
|
fclose(f);
|
2018-07-11 16:54:35 +00:00
|
|
|
return rc;
|
2017-05-30 21:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
prepare_vhost_memory_user(struct vhost_user_msg *msg, int fds[])
|
|
|
|
{
|
|
|
|
int i, num;
|
2019-02-25 07:44:21 +00:00
|
|
|
struct hugepage_file_info huges[VHOST_USER_MEMORY_MAX_NREGIONS];
|
2017-05-30 21:13:50 +00:00
|
|
|
|
2019-02-25 07:44:21 +00:00
|
|
|
num = get_hugepage_file_info(huges, VHOST_USER_MEMORY_MAX_NREGIONS);
|
2017-05-30 21:13:50 +00:00
|
|
|
if (num < 0) {
|
2017-10-14 10:43:58 +00:00
|
|
|
SPDK_ERRLOG("Failed to prepare memory for vhost-user\n");
|
2018-07-11 16:54:35 +00:00
|
|
|
return num;
|
2017-05-30 21:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < num; ++i) {
|
2017-10-05 15:53:43 +00:00
|
|
|
/* the memory regions are unaligned */
|
|
|
|
msg->payload.memory.regions[i].guest_phys_addr = huges[i].addr; /* use vaddr! */
|
|
|
|
msg->payload.memory.regions[i].userspace_addr = huges[i].addr;
|
|
|
|
msg->payload.memory.regions[i].memory_size = huges[i].size;
|
2017-11-03 11:17:43 +00:00
|
|
|
msg->payload.memory.regions[i].flags_padding = 0;
|
2017-05-30 21:13:50 +00:00
|
|
|
fds[i] = open(huges[i].path, O_RDWR);
|
|
|
|
}
|
|
|
|
|
|
|
|
msg->payload.memory.nregions = num;
|
|
|
|
msg->payload.memory.padding = 0;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-11-29 23:18:56 +00:00
|
|
|
static const char *const vhost_msg_strings[VHOST_USER_MAX] = {
|
2017-05-30 21:13:50 +00:00
|
|
|
[VHOST_USER_SET_OWNER] = "VHOST_SET_OWNER",
|
|
|
|
[VHOST_USER_RESET_OWNER] = "VHOST_RESET_OWNER",
|
|
|
|
[VHOST_USER_SET_FEATURES] = "VHOST_SET_FEATURES",
|
|
|
|
[VHOST_USER_GET_FEATURES] = "VHOST_GET_FEATURES",
|
|
|
|
[VHOST_USER_SET_VRING_CALL] = "VHOST_SET_VRING_CALL",
|
2019-12-19 04:56:00 +00:00
|
|
|
[VHOST_USER_GET_PROTOCOL_FEATURES] = "VHOST_USER_GET_PROTOCOL_FEATURES",
|
|
|
|
[VHOST_USER_SET_PROTOCOL_FEATURES] = "VHOST_USER_SET_PROTOCOL_FEATURES",
|
2017-05-30 21:13:50 +00:00
|
|
|
[VHOST_USER_SET_VRING_NUM] = "VHOST_SET_VRING_NUM",
|
|
|
|
[VHOST_USER_SET_VRING_BASE] = "VHOST_SET_VRING_BASE",
|
|
|
|
[VHOST_USER_GET_VRING_BASE] = "VHOST_GET_VRING_BASE",
|
|
|
|
[VHOST_USER_SET_VRING_ADDR] = "VHOST_SET_VRING_ADDR",
|
|
|
|
[VHOST_USER_SET_VRING_KICK] = "VHOST_SET_VRING_KICK",
|
|
|
|
[VHOST_USER_SET_MEM_TABLE] = "VHOST_SET_MEM_TABLE",
|
|
|
|
[VHOST_USER_SET_VRING_ENABLE] = "VHOST_SET_VRING_ENABLE",
|
2018-02-07 15:08:13 +00:00
|
|
|
[VHOST_USER_GET_QUEUE_NUM] = "VHOST_USER_GET_QUEUE_NUM",
|
|
|
|
[VHOST_USER_GET_CONFIG] = "VHOST_USER_GET_CONFIG",
|
|
|
|
[VHOST_USER_SET_CONFIG] = "VHOST_USER_SET_CONFIG",
|
2017-05-30 21:13:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
vhost_user_sock(struct virtio_user_dev *dev,
|
|
|
|
enum vhost_user_request req,
|
|
|
|
void *arg)
|
|
|
|
{
|
|
|
|
struct vhost_user_msg msg;
|
|
|
|
struct vhost_vring_file *file = 0;
|
|
|
|
int need_reply = 0;
|
2019-02-25 07:44:21 +00:00
|
|
|
int fds[VHOST_USER_MEMORY_MAX_NREGIONS];
|
2017-05-30 21:13:50 +00:00
|
|
|
int fd_num = 0;
|
2018-07-11 16:54:35 +00:00
|
|
|
int i, len, rc;
|
2017-05-30 21:13:50 +00:00
|
|
|
int vhostfd = dev->vhostfd;
|
|
|
|
|
2020-09-04 11:27:29 +00:00
|
|
|
SPDK_DEBUGLOG(virtio_user, "sent message %d = %s\n", req, vhost_msg_strings[req]);
|
2017-05-30 21:13:50 +00:00
|
|
|
|
|
|
|
msg.request = req;
|
|
|
|
msg.flags = VHOST_USER_VERSION;
|
|
|
|
msg.size = 0;
|
|
|
|
|
|
|
|
switch (req) {
|
|
|
|
case VHOST_USER_GET_FEATURES:
|
2018-07-02 10:43:05 +00:00
|
|
|
case VHOST_USER_GET_PROTOCOL_FEATURES:
|
2017-05-30 21:13:50 +00:00
|
|
|
case VHOST_USER_GET_QUEUE_NUM:
|
|
|
|
need_reply = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VHOST_USER_SET_FEATURES:
|
|
|
|
case VHOST_USER_SET_LOG_BASE:
|
2018-07-02 10:43:05 +00:00
|
|
|
case VHOST_USER_SET_PROTOCOL_FEATURES:
|
2017-05-30 21:13:50 +00:00
|
|
|
msg.payload.u64 = *((__u64 *)arg);
|
2017-11-29 22:48:39 +00:00
|
|
|
msg.size = sizeof(msg.payload.u64);
|
2017-05-30 21:13:50 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case VHOST_USER_SET_OWNER:
|
|
|
|
case VHOST_USER_RESET_OWNER:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VHOST_USER_SET_MEM_TABLE:
|
2018-07-11 16:54:35 +00:00
|
|
|
rc = prepare_vhost_memory_user(&msg, fds);
|
|
|
|
if (rc < 0) {
|
|
|
|
return rc;
|
2017-12-07 23:23:48 +00:00
|
|
|
}
|
2017-05-30 21:13:50 +00:00
|
|
|
fd_num = msg.payload.memory.nregions;
|
2017-11-29 22:48:39 +00:00
|
|
|
msg.size = sizeof(msg.payload.memory.nregions);
|
|
|
|
msg.size += sizeof(msg.payload.memory.padding);
|
2017-05-30 21:13:50 +00:00
|
|
|
msg.size += fd_num * sizeof(struct vhost_memory_region);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VHOST_USER_SET_LOG_FD:
|
|
|
|
fds[fd_num++] = *((int *)arg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VHOST_USER_SET_VRING_NUM:
|
|
|
|
case VHOST_USER_SET_VRING_BASE:
|
|
|
|
case VHOST_USER_SET_VRING_ENABLE:
|
|
|
|
memcpy(&msg.payload.state, arg, sizeof(msg.payload.state));
|
2017-11-29 22:48:39 +00:00
|
|
|
msg.size = sizeof(msg.payload.state);
|
2017-05-30 21:13:50 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case VHOST_USER_GET_VRING_BASE:
|
|
|
|
memcpy(&msg.payload.state, arg, sizeof(msg.payload.state));
|
2017-11-29 22:48:39 +00:00
|
|
|
msg.size = sizeof(msg.payload.state);
|
2017-05-30 21:13:50 +00:00
|
|
|
need_reply = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VHOST_USER_SET_VRING_ADDR:
|
|
|
|
memcpy(&msg.payload.addr, arg, sizeof(msg.payload.addr));
|
2017-11-29 22:48:39 +00:00
|
|
|
msg.size = sizeof(msg.payload.addr);
|
2017-05-30 21:13:50 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case VHOST_USER_SET_VRING_KICK:
|
|
|
|
case VHOST_USER_SET_VRING_CALL:
|
|
|
|
case VHOST_USER_SET_VRING_ERR:
|
|
|
|
file = arg;
|
|
|
|
msg.payload.u64 = file->index & VHOST_USER_VRING_IDX_MASK;
|
2017-11-29 22:48:39 +00:00
|
|
|
msg.size = sizeof(msg.payload.u64);
|
2017-12-07 23:23:48 +00:00
|
|
|
if (file->fd > 0) {
|
2017-05-30 21:13:50 +00:00
|
|
|
fds[fd_num++] = file->fd;
|
2017-12-07 23:23:48 +00:00
|
|
|
} else {
|
2017-05-30 21:13:50 +00:00
|
|
|
msg.payload.u64 |= VHOST_USER_VRING_NOFD_MASK;
|
2017-12-07 23:23:48 +00:00
|
|
|
}
|
2017-05-30 21:13:50 +00:00
|
|
|
break;
|
|
|
|
|
2018-02-07 15:08:13 +00:00
|
|
|
case VHOST_USER_GET_CONFIG:
|
|
|
|
memcpy(&msg.payload.cfg, arg, sizeof(msg.payload.cfg));
|
|
|
|
msg.size = sizeof(msg.payload.cfg);
|
|
|
|
need_reply = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VHOST_USER_SET_CONFIG:
|
|
|
|
memcpy(&msg.payload.cfg, arg, sizeof(msg.payload.cfg));
|
|
|
|
msg.size = sizeof(msg.payload.cfg);
|
|
|
|
break;
|
|
|
|
|
2017-05-30 21:13:50 +00:00
|
|
|
default:
|
2018-07-11 16:54:35 +00:00
|
|
|
SPDK_ERRLOG("trying to send unknown msg\n");
|
|
|
|
return -EINVAL;
|
2017-05-30 21:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
len = VHOST_USER_HDR_SIZE + msg.size;
|
2018-07-11 16:54:35 +00:00
|
|
|
rc = vhost_user_write(vhostfd, &msg, len, fds, fd_num);
|
|
|
|
if (rc < 0) {
|
2017-10-14 10:43:58 +00:00
|
|
|
SPDK_ERRLOG("%s failed: %s\n",
|
2018-07-11 16:54:35 +00:00
|
|
|
vhost_msg_strings[req], spdk_strerror(-rc));
|
|
|
|
return rc;
|
2017-05-30 21:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (req == VHOST_USER_SET_MEM_TABLE)
|
2017-12-07 23:23:48 +00:00
|
|
|
for (i = 0; i < fd_num; ++i) {
|
2017-05-30 21:13:50 +00:00
|
|
|
close(fds[i]);
|
2017-12-07 23:23:48 +00:00
|
|
|
}
|
2017-05-30 21:13:50 +00:00
|
|
|
|
|
|
|
if (need_reply) {
|
2018-07-11 16:54:35 +00:00
|
|
|
rc = vhost_user_read(vhostfd, &msg);
|
|
|
|
if (rc < 0) {
|
|
|
|
SPDK_WARNLOG("Received msg failed: %s\n", spdk_strerror(-rc));
|
|
|
|
return rc;
|
2017-05-30 21:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (req != msg.request) {
|
2017-10-14 10:43:58 +00:00
|
|
|
SPDK_WARNLOG("Received unexpected msg type\n");
|
2018-07-11 16:54:35 +00:00
|
|
|
return -EIO;
|
2017-05-30 21:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (req) {
|
|
|
|
case VHOST_USER_GET_FEATURES:
|
2018-07-02 10:43:05 +00:00
|
|
|
case VHOST_USER_GET_PROTOCOL_FEATURES:
|
2017-05-30 21:13:50 +00:00
|
|
|
case VHOST_USER_GET_QUEUE_NUM:
|
2017-11-29 22:48:39 +00:00
|
|
|
if (msg.size != sizeof(msg.payload.u64)) {
|
2017-10-14 10:43:58 +00:00
|
|
|
SPDK_WARNLOG("Received bad msg size\n");
|
2018-07-11 16:54:35 +00:00
|
|
|
return -EIO;
|
2017-05-30 21:13:50 +00:00
|
|
|
}
|
|
|
|
*((__u64 *)arg) = msg.payload.u64;
|
|
|
|
break;
|
|
|
|
case VHOST_USER_GET_VRING_BASE:
|
2017-11-29 22:48:39 +00:00
|
|
|
if (msg.size != sizeof(msg.payload.state)) {
|
2017-10-14 10:43:58 +00:00
|
|
|
SPDK_WARNLOG("Received bad msg size\n");
|
2018-07-11 16:54:35 +00:00
|
|
|
return -EIO;
|
2017-05-30 21:13:50 +00:00
|
|
|
}
|
|
|
|
memcpy(arg, &msg.payload.state,
|
|
|
|
sizeof(struct vhost_vring_state));
|
|
|
|
break;
|
2018-02-07 15:08:13 +00:00
|
|
|
case VHOST_USER_GET_CONFIG:
|
|
|
|
if (msg.size != sizeof(msg.payload.cfg)) {
|
|
|
|
SPDK_WARNLOG("Received bad msg size\n");
|
2018-07-11 16:54:35 +00:00
|
|
|
return -EIO;
|
2018-02-07 15:08:13 +00:00
|
|
|
}
|
|
|
|
memcpy(arg, &msg.payload.cfg, sizeof(msg.payload.cfg));
|
|
|
|
break;
|
2017-05-30 21:13:50 +00:00
|
|
|
default:
|
2017-10-14 10:43:58 +00:00
|
|
|
SPDK_WARNLOG("Received unexpected msg type\n");
|
2018-07-11 16:54:35 +00:00
|
|
|
return -EBADMSG;
|
2017-05-30 21:13:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set up environment to talk with a vhost user backend.
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* - (-1) if fail;
|
|
|
|
* - (0) if succeed.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
vhost_user_setup(struct virtio_user_dev *dev)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
int flag;
|
|
|
|
struct sockaddr_un un;
|
2017-08-22 16:46:14 +00:00
|
|
|
ssize_t rc;
|
2017-05-30 21:13:50 +00:00
|
|
|
|
|
|
|
fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
if (fd < 0) {
|
2017-11-07 12:56:52 +00:00
|
|
|
SPDK_ERRLOG("socket() error, %s\n", spdk_strerror(errno));
|
2018-07-11 16:54:35 +00:00
|
|
|
return -errno;
|
2017-05-30 21:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
flag = fcntl(fd, F_GETFD);
|
2017-10-18 11:30:18 +00:00
|
|
|
if (fcntl(fd, F_SETFD, flag | FD_CLOEXEC) < 0) {
|
2017-11-07 12:56:52 +00:00
|
|
|
SPDK_ERRLOG("fcntl failed, %s\n", spdk_strerror(errno));
|
2017-10-18 11:30:18 +00:00
|
|
|
}
|
2017-05-30 21:13:50 +00:00
|
|
|
|
|
|
|
memset(&un, 0, sizeof(un));
|
|
|
|
un.sun_family = AF_UNIX;
|
2017-08-22 16:46:14 +00:00
|
|
|
rc = snprintf(un.sun_path, sizeof(un.sun_path), "%s", dev->path);
|
|
|
|
if (rc < 0 || (size_t)rc >= sizeof(un.sun_path)) {
|
2017-10-14 10:43:58 +00:00
|
|
|
SPDK_ERRLOG("socket path too long\n");
|
2017-08-22 16:46:14 +00:00
|
|
|
close(fd);
|
2018-07-11 16:54:35 +00:00
|
|
|
if (rc < 0) {
|
|
|
|
return -errno;
|
|
|
|
} else {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2017-08-22 16:46:14 +00:00
|
|
|
}
|
2017-05-30 21:13:50 +00:00
|
|
|
if (connect(fd, (struct sockaddr *)&un, sizeof(un)) < 0) {
|
2017-11-07 12:56:52 +00:00
|
|
|
SPDK_ERRLOG("connect error, %s\n", spdk_strerror(errno));
|
2017-05-30 21:13:50 +00:00
|
|
|
close(fd);
|
2018-07-11 16:54:35 +00:00
|
|
|
return -errno;
|
2017-05-30 21:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dev->vhostfd = fd;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct virtio_user_backend_ops ops_user = {
|
|
|
|
.setup = vhost_user_setup,
|
|
|
|
.send_request = vhost_user_sock,
|
|
|
|
};
|
2017-10-14 10:43:58 +00:00
|
|
|
|
2020-09-04 11:27:29 +00:00
|
|
|
SPDK_LOG_REGISTER_COMPONENT(virtio_user)
|