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 ret = recv(fd, (void *)((char *)msg + sz_hdr), sz_payload, 0);
135 if (ret < sz_payload) {
137 "Failed to recv msg payload: %d instead of %d.",
149 struct hugepage_file_info {
150 uint64_t addr; /**< virtual addr */
151 size_t size; /**< the file size */
152 char path[PATH_MAX]; /**< path to backing file */
155 /* Two possible options:
156 * 1. Match HUGEPAGE_INFO_FMT to find the file storing struct hugepage_file
157 * array. This is simple but cannot be used in secondary process because
158 * secondary process will close and munmap that file.
159 * 2. Match HUGEFILE_FMT to find hugepage files directly.
161 * We choose option 2.
164 get_hugepage_file_info(struct hugepage_file_info huges[], int max)
168 char buf[BUFSIZ], *tmp, *tail;
169 char *str_underline, *str_start;
171 uint64_t v_start, v_end;
173 f = fopen("/proc/self/maps", "r");
175 PMD_DRV_LOG(ERR, "cannot open /proc/self/maps");
180 while (fgets(buf, sizeof(buf), f) != NULL) {
181 if (sscanf(buf, "%" PRIx64 "-%" PRIx64, &v_start, &v_end) < 2) {
182 PMD_DRV_LOG(ERR, "Failed to parse address");
186 tmp = strchr(buf, ' ') + 1; /** skip address */
187 tmp = strchr(tmp, ' ') + 1; /** skip perm */
188 tmp = strchr(tmp, ' ') + 1; /** skip offset */
189 tmp = strchr(tmp, ' ') + 1; /** skip dev */
190 tmp = strchr(tmp, ' ') + 1; /** skip inode */
191 while (*tmp == ' ') /** skip spaces */
193 tail = strrchr(tmp, '\n'); /** remove newline if exists */
197 /* Match HUGEFILE_FMT, aka "%s/%smap_%d",
198 * which is defined in eal_filesystem.h
200 str_underline = strrchr(tmp, '_');
204 str_start = str_underline - strlen("map");
208 if (sscanf(str_start, "map_%d", &huge_index) != 1)
212 PMD_DRV_LOG(ERR, "Exceed maximum of %d", max);
215 huges[idx].addr = v_start;
216 huges[idx].size = v_end - v_start;
217 snprintf(huges[idx].path, PATH_MAX, "%s", tmp);
230 prepare_vhost_memory_user(struct vhost_user_msg *msg, int fds[])
233 struct hugepage_file_info huges[VHOST_MEMORY_MAX_NREGIONS];
234 struct vhost_memory_region *mr;
236 num = get_hugepage_file_info(huges, VHOST_MEMORY_MAX_NREGIONS);
238 PMD_INIT_LOG(ERR, "Failed to prepare memory for vhost-user");
242 for (i = 0; i < num; ++i) {
243 mr = &msg->payload.memory.regions[i];
244 mr->guest_phys_addr = huges[i].addr; /* use vaddr! */
245 mr->userspace_addr = huges[i].addr;
246 mr->memory_size = huges[i].size;
248 fds[i] = open(huges[i].path, O_RDWR);
251 msg->payload.memory.nregions = num;
252 msg->payload.memory.padding = 0;
257 static struct vhost_user_msg m;
259 const char * const vhost_msg_strings[] = {
260 [VHOST_USER_SET_OWNER] = "VHOST_SET_OWNER",
261 [VHOST_USER_RESET_OWNER] = "VHOST_RESET_OWNER",
262 [VHOST_USER_SET_FEATURES] = "VHOST_SET_FEATURES",
263 [VHOST_USER_GET_FEATURES] = "VHOST_GET_FEATURES",
264 [VHOST_USER_SET_VRING_CALL] = "VHOST_SET_VRING_CALL",
265 [VHOST_USER_SET_VRING_NUM] = "VHOST_SET_VRING_NUM",
266 [VHOST_USER_SET_VRING_BASE] = "VHOST_SET_VRING_BASE",
267 [VHOST_USER_GET_VRING_BASE] = "VHOST_GET_VRING_BASE",
268 [VHOST_USER_SET_VRING_ADDR] = "VHOST_SET_VRING_ADDR",
269 [VHOST_USER_SET_VRING_KICK] = "VHOST_SET_VRING_KICK",
270 [VHOST_USER_SET_MEM_TABLE] = "VHOST_SET_MEM_TABLE",
271 [VHOST_USER_SET_VRING_ENABLE] = "VHOST_SET_VRING_ENABLE",
275 vhost_user_sock(struct virtio_user_dev *dev,
276 enum vhost_user_request req,
279 struct vhost_user_msg msg;
280 struct vhost_vring_file *file = 0;
282 int fds[VHOST_MEMORY_MAX_NREGIONS];
285 int vhostfd = dev->vhostfd;
289 PMD_DRV_LOG(INFO, "%s", vhost_msg_strings[req]);
292 msg.flags = VHOST_USER_VERSION;
296 case VHOST_USER_GET_FEATURES:
300 case VHOST_USER_SET_FEATURES:
301 case VHOST_USER_SET_LOG_BASE:
302 msg.payload.u64 = *((__u64 *)arg);
303 msg.size = sizeof(m.payload.u64);
306 case VHOST_USER_SET_OWNER:
307 case VHOST_USER_RESET_OWNER:
310 case VHOST_USER_SET_MEM_TABLE:
311 if (prepare_vhost_memory_user(&msg, fds) < 0)
313 fd_num = msg.payload.memory.nregions;
314 msg.size = sizeof(m.payload.memory.nregions);
315 msg.size += sizeof(m.payload.memory.padding);
316 msg.size += fd_num * sizeof(struct vhost_memory_region);
319 case VHOST_USER_SET_LOG_FD:
320 fds[fd_num++] = *((int *)arg);
323 case VHOST_USER_SET_VRING_NUM:
324 case VHOST_USER_SET_VRING_BASE:
325 case VHOST_USER_SET_VRING_ENABLE:
326 memcpy(&msg.payload.state, arg, sizeof(msg.payload.state));
327 msg.size = sizeof(m.payload.state);
330 case VHOST_USER_GET_VRING_BASE:
331 memcpy(&msg.payload.state, arg, sizeof(msg.payload.state));
332 msg.size = sizeof(m.payload.state);
336 case VHOST_USER_SET_VRING_ADDR:
337 memcpy(&msg.payload.addr, arg, sizeof(msg.payload.addr));
338 msg.size = sizeof(m.payload.addr);
341 case VHOST_USER_SET_VRING_KICK:
342 case VHOST_USER_SET_VRING_CALL:
343 case VHOST_USER_SET_VRING_ERR:
345 msg.payload.u64 = file->index & VHOST_USER_VRING_IDX_MASK;
346 msg.size = sizeof(m.payload.u64);
348 fds[fd_num++] = file->fd;
350 msg.payload.u64 |= VHOST_USER_VRING_NOFD_MASK;
354 PMD_DRV_LOG(ERR, "trying to send unhandled msg type");
358 len = VHOST_USER_HDR_SIZE + msg.size;
359 if (vhost_user_write(vhostfd, &msg, len, fds, fd_num) < 0) {
360 PMD_DRV_LOG(ERR, "%s failed: %s",
361 vhost_msg_strings[req], strerror(errno));
365 if (req == VHOST_USER_SET_MEM_TABLE)
366 for (i = 0; i < fd_num; ++i)
370 if (vhost_user_read(vhostfd, &msg) < 0) {
371 PMD_DRV_LOG(ERR, "Received msg failed: %s",
376 if (req != msg.request) {
377 PMD_DRV_LOG(ERR, "Received unexpected msg type");
382 case VHOST_USER_GET_FEATURES:
383 if (msg.size != sizeof(m.payload.u64)) {
384 PMD_DRV_LOG(ERR, "Received bad msg size");
387 *((__u64 *)arg) = msg.payload.u64;
389 case VHOST_USER_GET_VRING_BASE:
390 if (msg.size != sizeof(m.payload.state)) {
391 PMD_DRV_LOG(ERR, "Received bad msg size");
394 memcpy(arg, &msg.payload.state,
395 sizeof(struct vhost_vring_state));
398 PMD_DRV_LOG(ERR, "Received unexpected msg type");
407 * Set up environment to talk with a vhost user backend.
414 vhost_user_setup(struct virtio_user_dev *dev)
418 struct sockaddr_un un;
420 fd = socket(AF_UNIX, SOCK_STREAM, 0);
422 PMD_DRV_LOG(ERR, "socket() error, %s", strerror(errno));
426 flag = fcntl(fd, F_GETFD);
427 if (fcntl(fd, F_SETFD, flag | FD_CLOEXEC) < 0)
428 PMD_DRV_LOG(WARNING, "fcntl failed, %s", strerror(errno));
430 memset(&un, 0, sizeof(un));
431 un.sun_family = AF_UNIX;
432 snprintf(un.sun_path, sizeof(un.sun_path), "%s", dev->path);
433 if (connect(fd, (struct sockaddr *)&un, sizeof(un)) < 0) {
434 PMD_DRV_LOG(ERR, "connect error, %s", strerror(errno));
444 vhost_user_enable_queue_pair(struct virtio_user_dev *dev,
450 for (i = 0; i < 2; ++i) {
451 struct vhost_vring_state state = {
452 .index = pair_idx * 2 + i,
456 if (vhost_user_sock(dev, VHOST_USER_SET_VRING_ENABLE, &state))
463 struct virtio_user_backend_ops ops_user = {
464 .setup = vhost_user_setup,
465 .send_request = vhost_user_sock,
466 .enable_qp = vhost_user_enable_queue_pair