4 * Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/socket.h>
35 #include <sys/types.h>
44 #include "virtio_user_dev.h"
46 /* The version of the protocol we support */
47 #define VHOST_USER_VERSION 0x1
49 #define VHOST_MEMORY_MAX_NREGIONS 8
53 struct vhost_memory_region regions[VHOST_MEMORY_MAX_NREGIONS];
56 struct vhost_user_msg {
57 enum vhost_user_request request;
59 #define VHOST_USER_VERSION_MASK 0x3
60 #define VHOST_USER_REPLY_MASK (0x1 << 2)
62 uint32_t size; /* the following payload size */
64 #define VHOST_USER_VRING_IDX_MASK 0xff
65 #define VHOST_USER_VRING_NOFD_MASK (0x1 << 8)
67 struct vhost_vring_state state;
68 struct vhost_vring_addr addr;
69 struct vhost_memory memory;
71 int fds[VHOST_MEMORY_MAX_NREGIONS];
72 } __attribute((packed));
74 #define VHOST_USER_HDR_SIZE offsetof(struct vhost_user_msg, payload.u64)
75 #define VHOST_USER_PAYLOAD_SIZE \
76 (sizeof(struct vhost_user_msg) - VHOST_USER_HDR_SIZE)
79 vhost_user_write(int fd, void *buf, int len, int *fds, int fd_num)
84 size_t fd_size = fd_num * sizeof(int);
85 char control[CMSG_SPACE(fd_size)];
88 memset(&msgh, 0, sizeof(msgh));
89 memset(control, 0, sizeof(control));
91 iov.iov_base = (uint8_t *)buf;
96 msgh.msg_control = control;
97 msgh.msg_controllen = sizeof(control);
99 cmsg = CMSG_FIRSTHDR(&msgh);
100 cmsg->cmsg_len = CMSG_LEN(fd_size);
101 cmsg->cmsg_level = SOL_SOCKET;
102 cmsg->cmsg_type = SCM_RIGHTS;
103 memcpy(CMSG_DATA(cmsg), fds, fd_size);
106 r = sendmsg(fd, &msgh, 0);
107 } while (r < 0 && errno == EINTR);
113 vhost_user_read(int fd, struct vhost_user_msg *msg)
115 uint32_t valid_flags = VHOST_USER_REPLY_MASK | VHOST_USER_VERSION;
116 int ret, sz_hdr = VHOST_USER_HDR_SIZE, sz_payload;
118 ret = recv(fd, (void *)msg, sz_hdr, 0);
120 PMD_DRV_LOG(ERR, "Failed to recv msg hdr: %d instead of %d.",
125 /* validate msg flags */
126 if (msg->flags != (valid_flags)) {
127 PMD_DRV_LOG(ERR, "Failed to recv msg: flags %x instead of %x.",
128 msg->flags, valid_flags);
132 sz_payload = msg->size;
134 if ((size_t)sz_payload > sizeof(msg->payload))
138 ret = recv(fd, (void *)((char *)msg + sz_hdr), sz_payload, 0);
139 if (ret < sz_payload) {
141 "Failed to recv msg payload: %d instead of %d.",
153 struct hugepage_file_info {
154 uint64_t addr; /**< virtual addr */
155 size_t size; /**< the file size */
156 char path[PATH_MAX]; /**< path to backing file */
159 /* Two possible options:
160 * 1. Match HUGEPAGE_INFO_FMT to find the file storing struct hugepage_file
161 * array. This is simple but cannot be used in secondary process because
162 * secondary process will close and munmap that file.
163 * 2. Match HUGEFILE_FMT to find hugepage files directly.
165 * We choose option 2.
168 get_hugepage_file_info(struct hugepage_file_info huges[], int max)
172 char buf[BUFSIZ], *tmp, *tail;
173 char *str_underline, *str_start;
175 uint64_t v_start, v_end;
177 f = fopen("/proc/self/maps", "r");
179 PMD_DRV_LOG(ERR, "cannot open /proc/self/maps");
184 while (fgets(buf, sizeof(buf), f) != NULL) {
185 if (sscanf(buf, "%" PRIx64 "-%" PRIx64, &v_start, &v_end) < 2) {
186 PMD_DRV_LOG(ERR, "Failed to parse address");
190 tmp = strchr(buf, ' ') + 1; /** skip address */
191 tmp = strchr(tmp, ' ') + 1; /** skip perm */
192 tmp = strchr(tmp, ' ') + 1; /** skip offset */
193 tmp = strchr(tmp, ' ') + 1; /** skip dev */
194 tmp = strchr(tmp, ' ') + 1; /** skip inode */
195 while (*tmp == ' ') /** skip spaces */
197 tail = strrchr(tmp, '\n'); /** remove newline if exists */
201 /* Match HUGEFILE_FMT, aka "%s/%smap_%d",
202 * which is defined in eal_filesystem.h
204 str_underline = strrchr(tmp, '_');
208 str_start = str_underline - strlen("map");
212 if (sscanf(str_start, "map_%d", &huge_index) != 1)
216 PMD_DRV_LOG(ERR, "Exceed maximum of %d", max);
219 huges[idx].addr = v_start;
220 huges[idx].size = v_end - v_start;
221 snprintf(huges[idx].path, PATH_MAX, "%s", tmp);
234 prepare_vhost_memory_user(struct vhost_user_msg *msg, int fds[])
237 struct hugepage_file_info huges[VHOST_MEMORY_MAX_NREGIONS];
238 struct vhost_memory_region *mr;
240 num = get_hugepage_file_info(huges, VHOST_MEMORY_MAX_NREGIONS);
242 PMD_INIT_LOG(ERR, "Failed to prepare memory for vhost-user");
246 for (i = 0; i < num; ++i) {
247 mr = &msg->payload.memory.regions[i];
248 mr->guest_phys_addr = huges[i].addr; /* use vaddr! */
249 mr->userspace_addr = huges[i].addr;
250 mr->memory_size = huges[i].size;
252 fds[i] = open(huges[i].path, O_RDWR);
255 msg->payload.memory.nregions = num;
256 msg->payload.memory.padding = 0;
261 static struct vhost_user_msg m;
263 const char * const vhost_msg_strings[] = {
264 [VHOST_USER_SET_OWNER] = "VHOST_SET_OWNER",
265 [VHOST_USER_RESET_OWNER] = "VHOST_RESET_OWNER",
266 [VHOST_USER_SET_FEATURES] = "VHOST_SET_FEATURES",
267 [VHOST_USER_GET_FEATURES] = "VHOST_GET_FEATURES",
268 [VHOST_USER_SET_VRING_CALL] = "VHOST_SET_VRING_CALL",
269 [VHOST_USER_SET_VRING_NUM] = "VHOST_SET_VRING_NUM",
270 [VHOST_USER_SET_VRING_BASE] = "VHOST_SET_VRING_BASE",
271 [VHOST_USER_GET_VRING_BASE] = "VHOST_GET_VRING_BASE",
272 [VHOST_USER_SET_VRING_ADDR] = "VHOST_SET_VRING_ADDR",
273 [VHOST_USER_SET_VRING_KICK] = "VHOST_SET_VRING_KICK",
274 [VHOST_USER_SET_MEM_TABLE] = "VHOST_SET_MEM_TABLE",
275 [VHOST_USER_SET_VRING_ENABLE] = "VHOST_SET_VRING_ENABLE",
279 vhost_user_sock(struct virtio_user_dev *dev,
280 enum vhost_user_request req,
283 struct vhost_user_msg msg;
284 struct vhost_vring_file *file = 0;
286 int fds[VHOST_MEMORY_MAX_NREGIONS];
289 int vhostfd = dev->vhostfd;
293 PMD_DRV_LOG(INFO, "%s", vhost_msg_strings[req]);
296 msg.flags = VHOST_USER_VERSION;
300 case VHOST_USER_GET_FEATURES:
304 case VHOST_USER_SET_FEATURES:
305 case VHOST_USER_SET_LOG_BASE:
306 msg.payload.u64 = *((__u64 *)arg);
307 msg.size = sizeof(m.payload.u64);
310 case VHOST_USER_SET_OWNER:
311 case VHOST_USER_RESET_OWNER:
314 case VHOST_USER_SET_MEM_TABLE:
315 if (prepare_vhost_memory_user(&msg, fds) < 0)
317 fd_num = msg.payload.memory.nregions;
318 msg.size = sizeof(m.payload.memory.nregions);
319 msg.size += sizeof(m.payload.memory.padding);
320 msg.size += fd_num * sizeof(struct vhost_memory_region);
323 case VHOST_USER_SET_LOG_FD:
324 fds[fd_num++] = *((int *)arg);
327 case VHOST_USER_SET_VRING_NUM:
328 case VHOST_USER_SET_VRING_BASE:
329 case VHOST_USER_SET_VRING_ENABLE:
330 memcpy(&msg.payload.state, arg, sizeof(msg.payload.state));
331 msg.size = sizeof(m.payload.state);
334 case VHOST_USER_GET_VRING_BASE:
335 memcpy(&msg.payload.state, arg, sizeof(msg.payload.state));
336 msg.size = sizeof(m.payload.state);
340 case VHOST_USER_SET_VRING_ADDR:
341 memcpy(&msg.payload.addr, arg, sizeof(msg.payload.addr));
342 msg.size = sizeof(m.payload.addr);
345 case VHOST_USER_SET_VRING_KICK:
346 case VHOST_USER_SET_VRING_CALL:
347 case VHOST_USER_SET_VRING_ERR:
349 msg.payload.u64 = file->index & VHOST_USER_VRING_IDX_MASK;
350 msg.size = sizeof(m.payload.u64);
352 fds[fd_num++] = file->fd;
354 msg.payload.u64 |= VHOST_USER_VRING_NOFD_MASK;
358 PMD_DRV_LOG(ERR, "trying to send unhandled msg type");
362 len = VHOST_USER_HDR_SIZE + msg.size;
363 if (vhost_user_write(vhostfd, &msg, len, fds, fd_num) < 0) {
364 PMD_DRV_LOG(ERR, "%s failed: %s",
365 vhost_msg_strings[req], strerror(errno));
369 if (req == VHOST_USER_SET_MEM_TABLE)
370 for (i = 0; i < fd_num; ++i)
374 if (vhost_user_read(vhostfd, &msg) < 0) {
375 PMD_DRV_LOG(ERR, "Received msg failed: %s",
380 if (req != msg.request) {
381 PMD_DRV_LOG(ERR, "Received unexpected msg type");
386 case VHOST_USER_GET_FEATURES:
387 if (msg.size != sizeof(m.payload.u64)) {
388 PMD_DRV_LOG(ERR, "Received bad msg size");
391 *((__u64 *)arg) = msg.payload.u64;
393 case VHOST_USER_GET_VRING_BASE:
394 if (msg.size != sizeof(m.payload.state)) {
395 PMD_DRV_LOG(ERR, "Received bad msg size");
398 memcpy(arg, &msg.payload.state,
399 sizeof(struct vhost_vring_state));
402 PMD_DRV_LOG(ERR, "Received unexpected msg type");
411 * Set up environment to talk with a vhost user backend.
418 vhost_user_setup(struct virtio_user_dev *dev)
422 struct sockaddr_un un;
424 fd = socket(AF_UNIX, SOCK_STREAM, 0);
426 PMD_DRV_LOG(ERR, "socket() error, %s", strerror(errno));
430 flag = fcntl(fd, F_GETFD);
431 if (fcntl(fd, F_SETFD, flag | FD_CLOEXEC) < 0)
432 PMD_DRV_LOG(WARNING, "fcntl failed, %s", strerror(errno));
434 memset(&un, 0, sizeof(un));
435 un.sun_family = AF_UNIX;
436 snprintf(un.sun_path, sizeof(un.sun_path), "%s", dev->path);
437 if (connect(fd, (struct sockaddr *)&un, sizeof(un)) < 0) {
438 PMD_DRV_LOG(ERR, "connect error, %s", strerror(errno));
448 vhost_user_enable_queue_pair(struct virtio_user_dev *dev,
454 for (i = 0; i < 2; ++i) {
455 struct vhost_vring_state state = {
456 .index = pair_idx * 2 + i,
460 if (vhost_user_sock(dev, VHOST_USER_SET_VRING_ENABLE, &state))
467 struct virtio_user_backend_ops ops_user = {
468 .setup = vhost_user_setup,
469 .send_request = vhost_user_sock,
470 .enable_qp = vhost_user_enable_queue_pair