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>
45 /* The version of the protocol we support */
46 #define VHOST_USER_VERSION 0x1
48 #define VHOST_MEMORY_MAX_NREGIONS 8
52 struct vhost_memory_region regions[VHOST_MEMORY_MAX_NREGIONS];
55 struct vhost_user_msg {
56 enum vhost_user_request request;
58 #define VHOST_USER_VERSION_MASK 0x3
59 #define VHOST_USER_REPLY_MASK (0x1 << 2)
61 uint32_t size; /* the following payload size */
63 #define VHOST_USER_VRING_IDX_MASK 0xff
64 #define VHOST_USER_VRING_NOFD_MASK (0x1 << 8)
66 struct vhost_vring_state state;
67 struct vhost_vring_addr addr;
68 struct vhost_memory memory;
70 int fds[VHOST_MEMORY_MAX_NREGIONS];
71 } __attribute((packed));
73 #define VHOST_USER_HDR_SIZE offsetof(struct vhost_user_msg, payload.u64)
74 #define VHOST_USER_PAYLOAD_SIZE \
75 (sizeof(struct vhost_user_msg) - VHOST_USER_HDR_SIZE)
78 vhost_user_write(int fd, void *buf, int len, int *fds, int fd_num)
83 size_t fd_size = fd_num * sizeof(int);
84 char control[CMSG_SPACE(fd_size)];
87 memset(&msgh, 0, sizeof(msgh));
88 memset(control, 0, sizeof(control));
90 iov.iov_base = (uint8_t *)buf;
95 msgh.msg_control = control;
96 msgh.msg_controllen = sizeof(control);
98 cmsg = CMSG_FIRSTHDR(&msgh);
99 cmsg->cmsg_len = CMSG_LEN(fd_size);
100 cmsg->cmsg_level = SOL_SOCKET;
101 cmsg->cmsg_type = SCM_RIGHTS;
102 memcpy(CMSG_DATA(cmsg), fds, fd_size);
105 r = sendmsg(fd, &msgh, 0);
106 } while (r < 0 && errno == EINTR);
112 vhost_user_read(int fd, struct vhost_user_msg *msg)
114 uint32_t valid_flags = VHOST_USER_REPLY_MASK | VHOST_USER_VERSION;
115 int ret, sz_hdr = VHOST_USER_HDR_SIZE, sz_payload;
117 ret = recv(fd, (void *)msg, sz_hdr, 0);
119 PMD_DRV_LOG(ERR, "Failed to recv msg hdr: %d instead of %d.",
124 /* validate msg flags */
125 if (msg->flags != (valid_flags)) {
126 PMD_DRV_LOG(ERR, "Failed to recv msg: flags %x instead of %x.",
127 msg->flags, valid_flags);
131 sz_payload = msg->size;
133 ret = recv(fd, (void *)((char *)msg + sz_hdr), sz_payload, 0);
134 if (ret < sz_payload) {
136 "Failed to recv msg payload: %d instead of %d.",
148 struct hugepage_file_info {
149 uint64_t addr; /**< virtual addr */
150 size_t size; /**< the file size */
151 char path[PATH_MAX]; /**< path to backing file */
154 /* Two possible options:
155 * 1. Match HUGEPAGE_INFO_FMT to find the file storing struct hugepage_file
156 * array. This is simple but cannot be used in secondary process because
157 * secondary process will close and munmap that file.
158 * 2. Match HUGEFILE_FMT to find hugepage files directly.
160 * We choose option 2.
163 get_hugepage_file_info(struct hugepage_file_info huges[], int max)
167 char buf[BUFSIZ], *tmp, *tail;
168 char *str_underline, *str_start;
170 uint64_t v_start, v_end;
172 f = fopen("/proc/self/maps", "r");
174 PMD_DRV_LOG(ERR, "cannot open /proc/self/maps");
179 while (fgets(buf, sizeof(buf), f) != NULL) {
180 if (sscanf(buf, "%" PRIx64 "-%" PRIx64, &v_start, &v_end) < 2) {
181 PMD_DRV_LOG(ERR, "Failed to parse address");
185 tmp = strchr(buf, ' ') + 1; /** skip address */
186 tmp = strchr(tmp, ' ') + 1; /** skip perm */
187 tmp = strchr(tmp, ' ') + 1; /** skip offset */
188 tmp = strchr(tmp, ' ') + 1; /** skip dev */
189 tmp = strchr(tmp, ' ') + 1; /** skip inode */
190 while (*tmp == ' ') /** skip spaces */
192 tail = strrchr(tmp, '\n'); /** remove newline if exists */
196 /* Match HUGEFILE_FMT, aka "%s/%smap_%d",
197 * which is defined in eal_filesystem.h
199 str_underline = strrchr(tmp, '_');
203 str_start = str_underline - strlen("map");
207 if (sscanf(str_start, "map_%d", &huge_index) != 1)
211 PMD_DRV_LOG(ERR, "Exceed maximum of %d", max);
214 huges[idx].addr = v_start;
215 huges[idx].size = v_end - v_start;
216 snprintf(huges[idx].path, PATH_MAX, "%s", tmp);
229 prepare_vhost_memory_user(struct vhost_user_msg *msg, int fds[])
232 struct hugepage_file_info huges[VHOST_MEMORY_MAX_NREGIONS];
233 struct vhost_memory_region *mr;
235 num = get_hugepage_file_info(huges, VHOST_MEMORY_MAX_NREGIONS);
237 PMD_INIT_LOG(ERR, "Failed to prepare memory for vhost-user");
241 for (i = 0; i < num; ++i) {
242 mr = &msg->payload.memory.regions[i];
243 mr->guest_phys_addr = huges[i].addr; /* use vaddr! */
244 mr->userspace_addr = huges[i].addr;
245 mr->memory_size = huges[i].size;
247 fds[i] = open(huges[i].path, O_RDWR);
250 msg->payload.memory.nregions = num;
251 msg->payload.memory.padding = 0;
256 static struct vhost_user_msg m;
258 static const char * const vhost_msg_strings[] = {
259 [VHOST_USER_SET_OWNER] = "VHOST_USER_SET_OWNER",
260 [VHOST_USER_RESET_OWNER] = "VHOST_USER_RESET_OWNER",
261 [VHOST_USER_SET_FEATURES] = "VHOST_USER_SET_FEATURES",
262 [VHOST_USER_GET_FEATURES] = "VHOST_USER_GET_FEATURES",
263 [VHOST_USER_SET_VRING_CALL] = "VHOST_USER_SET_VRING_CALL",
264 [VHOST_USER_SET_VRING_NUM] = "VHOST_USER_SET_VRING_NUM",
265 [VHOST_USER_SET_VRING_BASE] = "VHOST_USER_SET_VRING_BASE",
266 [VHOST_USER_GET_VRING_BASE] = "VHOST_USER_GET_VRING_BASE",
267 [VHOST_USER_SET_VRING_ADDR] = "VHOST_USER_SET_VRING_ADDR",
268 [VHOST_USER_SET_VRING_KICK] = "VHOST_USER_SET_VRING_KICK",
269 [VHOST_USER_SET_MEM_TABLE] = "VHOST_USER_SET_MEM_TABLE",
270 [VHOST_USER_SET_VRING_ENABLE] = "VHOST_USER_SET_VRING_ENABLE",
275 vhost_user_sock(int vhostfd, enum vhost_user_request req, void *arg)
277 struct vhost_user_msg msg;
278 struct vhost_vring_file *file = 0;
280 int fds[VHOST_MEMORY_MAX_NREGIONS];
285 RTE_SET_USED(vhost_msg_strings);
287 PMD_DRV_LOG(INFO, "%s", vhost_msg_strings[req]);
290 msg.flags = VHOST_USER_VERSION;
294 case VHOST_USER_GET_FEATURES:
298 case VHOST_USER_SET_FEATURES:
299 case VHOST_USER_SET_LOG_BASE:
300 msg.payload.u64 = *((__u64 *)arg);
301 msg.size = sizeof(m.payload.u64);
304 case VHOST_USER_SET_OWNER:
305 case VHOST_USER_RESET_OWNER:
308 case VHOST_USER_SET_MEM_TABLE:
309 if (prepare_vhost_memory_user(&msg, fds) < 0)
311 fd_num = msg.payload.memory.nregions;
312 msg.size = sizeof(m.payload.memory.nregions);
313 msg.size += sizeof(m.payload.memory.padding);
314 msg.size += fd_num * sizeof(struct vhost_memory_region);
317 case VHOST_USER_SET_LOG_FD:
318 fds[fd_num++] = *((int *)arg);
321 case VHOST_USER_SET_VRING_NUM:
322 case VHOST_USER_SET_VRING_BASE:
323 case VHOST_USER_SET_VRING_ENABLE:
324 memcpy(&msg.payload.state, arg, sizeof(msg.payload.state));
325 msg.size = sizeof(m.payload.state);
328 case VHOST_USER_GET_VRING_BASE:
329 memcpy(&msg.payload.state, arg, sizeof(msg.payload.state));
330 msg.size = sizeof(m.payload.state);
334 case VHOST_USER_SET_VRING_ADDR:
335 memcpy(&msg.payload.addr, arg, sizeof(msg.payload.addr));
336 msg.size = sizeof(m.payload.addr);
339 case VHOST_USER_SET_VRING_KICK:
340 case VHOST_USER_SET_VRING_CALL:
341 case VHOST_USER_SET_VRING_ERR:
343 msg.payload.u64 = file->index & VHOST_USER_VRING_IDX_MASK;
344 msg.size = sizeof(m.payload.u64);
346 fds[fd_num++] = file->fd;
348 msg.payload.u64 |= VHOST_USER_VRING_NOFD_MASK;
352 PMD_DRV_LOG(ERR, "trying to send unhandled msg type");
356 len = VHOST_USER_HDR_SIZE + msg.size;
357 if (vhost_user_write(vhostfd, &msg, len, fds, fd_num) < 0) {
358 PMD_DRV_LOG(ERR, "%s failed: %s",
359 vhost_msg_strings[req], strerror(errno));
363 if (req == VHOST_USER_SET_MEM_TABLE)
364 for (i = 0; i < fd_num; ++i)
368 if (vhost_user_read(vhostfd, &msg) < 0) {
369 PMD_DRV_LOG(ERR, "Received msg failed: %s",
374 if (req != msg.request) {
375 PMD_DRV_LOG(ERR, "Received unexpected msg type");
380 case VHOST_USER_GET_FEATURES:
381 if (msg.size != sizeof(m.payload.u64)) {
382 PMD_DRV_LOG(ERR, "Received bad msg size");
385 *((__u64 *)arg) = msg.payload.u64;
387 case VHOST_USER_GET_VRING_BASE:
388 if (msg.size != sizeof(m.payload.state)) {
389 PMD_DRV_LOG(ERR, "Received bad msg size");
392 memcpy(arg, &msg.payload.state,
393 sizeof(struct vhost_vring_state));
396 PMD_DRV_LOG(ERR, "Received unexpected msg type");
405 * Set up environment to talk with a vhost user backend.
407 * - The path to vhost user unix socket file.
410 * - (-1) if fail to set up;
411 * - (>=0) if successful, and it is the fd to vhostfd.
414 vhost_user_setup(const char *path)
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", path);
433 if (connect(fd, (struct sockaddr *)&un, sizeof(un)) < 0) {
434 PMD_DRV_LOG(ERR, "connect error, %s", strerror(errno));
443 vhost_user_enable_queue_pair(int vhostfd, uint16_t pair_idx, int enable)
447 for (i = 0; i < 2; ++i) {
448 struct vhost_vring_state state = {
449 .index = pair_idx * 2 + i,
453 if (vhost_user_sock(vhostfd,
454 VHOST_USER_SET_VRING_ENABLE, &state))