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>
46 vhost_user_write(int fd, void *buf, int len, int *fds, int fd_num)
51 size_t fd_size = fd_num * sizeof(int);
52 char control[CMSG_SPACE(fd_size)];
55 memset(&msgh, 0, sizeof(msgh));
56 memset(control, 0, sizeof(control));
58 iov.iov_base = (uint8_t *)buf;
63 msgh.msg_control = control;
64 msgh.msg_controllen = sizeof(control);
66 cmsg = CMSG_FIRSTHDR(&msgh);
67 cmsg->cmsg_len = CMSG_LEN(fd_size);
68 cmsg->cmsg_level = SOL_SOCKET;
69 cmsg->cmsg_type = SCM_RIGHTS;
70 memcpy(CMSG_DATA(cmsg), fds, fd_size);
73 r = sendmsg(fd, &msgh, 0);
74 } while (r < 0 && errno == EINTR);
80 vhost_user_read(int fd, struct vhost_user_msg *msg)
82 uint32_t valid_flags = VHOST_USER_REPLY_MASK | VHOST_USER_VERSION;
83 int ret, sz_hdr = VHOST_USER_HDR_SIZE, sz_payload;
85 ret = recv(fd, (void *)msg, sz_hdr, 0);
87 PMD_DRV_LOG(ERR, "Failed to recv msg hdr: %d instead of %d.",
92 /* validate msg flags */
93 if (msg->flags != (valid_flags)) {
94 PMD_DRV_LOG(ERR, "Failed to recv msg: flags %x instead of %x.",
95 msg->flags, valid_flags);
99 sz_payload = msg->size;
101 if ((size_t)sz_payload > sizeof(msg->payload))
105 ret = recv(fd, (void *)((char *)msg + sz_hdr), sz_payload, 0);
106 if (ret < sz_payload) {
108 "Failed to recv msg payload: %d instead of %d.",
120 struct hugepage_file_info {
121 uint64_t addr; /**< virtual addr */
122 size_t size; /**< the file size */
123 char path[PATH_MAX]; /**< path to backing file */
126 /* Two possible options:
127 * 1. Match HUGEPAGE_INFO_FMT to find the file storing struct hugepage_file
128 * array. This is simple but cannot be used in secondary process because
129 * secondary process will close and munmap that file.
130 * 2. Match HUGEFILE_FMT to find hugepage files directly.
132 * We choose option 2.
135 get_hugepage_file_info(struct hugepage_file_info huges[], int max)
139 char buf[BUFSIZ], *tmp, *tail;
140 char *str_underline, *str_start;
142 uint64_t v_start, v_end;
145 f = fopen("/proc/self/maps", "r");
147 PMD_DRV_LOG(ERR, "cannot open /proc/self/maps");
152 while (fgets(buf, sizeof(buf), f) != NULL) {
153 if (sscanf(buf, "%" PRIx64 "-%" PRIx64, &v_start, &v_end) < 2) {
154 PMD_DRV_LOG(ERR, "Failed to parse address");
158 tmp = strchr(buf, ' ') + 1; /** skip address */
159 tmp = strchr(tmp, ' ') + 1; /** skip perm */
160 tmp = strchr(tmp, ' ') + 1; /** skip offset */
161 tmp = strchr(tmp, ' ') + 1; /** skip dev */
162 tmp = strchr(tmp, ' ') + 1; /** skip inode */
163 while (*tmp == ' ') /** skip spaces */
165 tail = strrchr(tmp, '\n'); /** remove newline if exists */
169 /* Match HUGEFILE_FMT, aka "%s/%smap_%d",
170 * which is defined in eal_filesystem.h
172 str_underline = strrchr(tmp, '_');
176 str_start = str_underline - strlen("map");
180 if (sscanf(str_start, "map_%d", &huge_index) != 1)
183 /* skip duplicated file which is mapped to different regions */
184 for (k = 0, exist = -1; k < idx; ++k) {
185 if (!strcmp(huges[k].path, tmp)) {
194 PMD_DRV_LOG(ERR, "Exceed maximum of %d", max);
198 huges[idx].addr = v_start;
199 huges[idx].size = v_end - v_start; /* To be corrected later */
200 snprintf(huges[idx].path, PATH_MAX, "%s", tmp);
204 /* correct the size for files who have many regions */
205 for (k = 0; k < idx; ++k) {
206 if (stat(huges[k].path, &stats) < 0) {
207 PMD_DRV_LOG(ERR, "Failed to stat %s, %s\n",
208 huges[k].path, strerror(errno));
211 huges[k].size = stats.st_size;
212 PMD_DRV_LOG(INFO, "file %s, size %zx\n",
213 huges[k].path, huges[k].size);
225 prepare_vhost_memory_user(struct vhost_user_msg *msg, int fds[])
228 struct hugepage_file_info huges[VHOST_MEMORY_MAX_NREGIONS];
229 struct vhost_memory_region *mr;
231 num = get_hugepage_file_info(huges, VHOST_MEMORY_MAX_NREGIONS);
233 PMD_INIT_LOG(ERR, "Failed to prepare memory for vhost-user");
237 for (i = 0; i < num; ++i) {
238 mr = &msg->payload.memory.regions[i];
239 mr->guest_phys_addr = huges[i].addr; /* use vaddr! */
240 mr->userspace_addr = huges[i].addr;
241 mr->memory_size = huges[i].size;
243 fds[i] = open(huges[i].path, O_RDWR);
246 msg->payload.memory.nregions = num;
247 msg->payload.memory.padding = 0;
252 static struct vhost_user_msg m;
254 static const char * const vhost_msg_strings[] = {
255 [VHOST_USER_SET_OWNER] = "VHOST_USER_SET_OWNER",
256 [VHOST_USER_RESET_OWNER] = "VHOST_USER_RESET_OWNER",
257 [VHOST_USER_SET_FEATURES] = "VHOST_USER_SET_FEATURES",
258 [VHOST_USER_GET_FEATURES] = "VHOST_USER_GET_FEATURES",
259 [VHOST_USER_SET_VRING_CALL] = "VHOST_USER_SET_VRING_CALL",
260 [VHOST_USER_SET_VRING_NUM] = "VHOST_USER_SET_VRING_NUM",
261 [VHOST_USER_SET_VRING_BASE] = "VHOST_USER_SET_VRING_BASE",
262 [VHOST_USER_GET_VRING_BASE] = "VHOST_USER_GET_VRING_BASE",
263 [VHOST_USER_SET_VRING_ADDR] = "VHOST_USER_SET_VRING_ADDR",
264 [VHOST_USER_SET_VRING_KICK] = "VHOST_USER_SET_VRING_KICK",
265 [VHOST_USER_SET_MEM_TABLE] = "VHOST_USER_SET_MEM_TABLE",
266 [VHOST_USER_SET_VRING_ENABLE] = "VHOST_USER_SET_VRING_ENABLE",
271 vhost_user_sock(int vhostfd, enum vhost_user_request req, void *arg)
273 struct vhost_user_msg msg;
274 struct vhost_vring_file *file = 0;
276 int fds[VHOST_MEMORY_MAX_NREGIONS];
281 RTE_SET_USED(vhost_msg_strings);
283 PMD_DRV_LOG(INFO, "%s", vhost_msg_strings[req]);
286 msg.flags = VHOST_USER_VERSION;
290 case VHOST_USER_GET_FEATURES:
294 case VHOST_USER_SET_FEATURES:
295 case VHOST_USER_SET_LOG_BASE:
296 msg.payload.u64 = *((__u64 *)arg);
297 msg.size = sizeof(m.payload.u64);
300 case VHOST_USER_SET_OWNER:
301 case VHOST_USER_RESET_OWNER:
304 case VHOST_USER_SET_MEM_TABLE:
305 if (prepare_vhost_memory_user(&msg, fds) < 0)
307 fd_num = msg.payload.memory.nregions;
308 msg.size = sizeof(m.payload.memory.nregions);
309 msg.size += sizeof(m.payload.memory.padding);
310 msg.size += fd_num * sizeof(struct vhost_memory_region);
313 case VHOST_USER_SET_LOG_FD:
314 fds[fd_num++] = *((int *)arg);
317 case VHOST_USER_SET_VRING_NUM:
318 case VHOST_USER_SET_VRING_BASE:
319 case VHOST_USER_SET_VRING_ENABLE:
320 memcpy(&msg.payload.state, arg, sizeof(msg.payload.state));
321 msg.size = sizeof(m.payload.state);
324 case VHOST_USER_GET_VRING_BASE:
325 memcpy(&msg.payload.state, arg, sizeof(msg.payload.state));
326 msg.size = sizeof(m.payload.state);
330 case VHOST_USER_SET_VRING_ADDR:
331 memcpy(&msg.payload.addr, arg, sizeof(msg.payload.addr));
332 msg.size = sizeof(m.payload.addr);
335 case VHOST_USER_SET_VRING_KICK:
336 case VHOST_USER_SET_VRING_CALL:
337 case VHOST_USER_SET_VRING_ERR:
339 msg.payload.u64 = file->index & VHOST_USER_VRING_IDX_MASK;
340 msg.size = sizeof(m.payload.u64);
342 fds[fd_num++] = file->fd;
344 msg.payload.u64 |= VHOST_USER_VRING_NOFD_MASK;
348 PMD_DRV_LOG(ERR, "trying to send unhandled msg type");
352 len = VHOST_USER_HDR_SIZE + msg.size;
353 if (vhost_user_write(vhostfd, &msg, len, fds, fd_num) < 0) {
354 PMD_DRV_LOG(ERR, "%s failed: %s",
355 vhost_msg_strings[req], strerror(errno));
359 if (req == VHOST_USER_SET_MEM_TABLE)
360 for (i = 0; i < fd_num; ++i)
364 if (vhost_user_read(vhostfd, &msg) < 0) {
365 PMD_DRV_LOG(ERR, "Received msg failed: %s",
370 if (req != msg.request) {
371 PMD_DRV_LOG(ERR, "Received unexpected msg type");
376 case VHOST_USER_GET_FEATURES:
377 if (msg.size != sizeof(m.payload.u64)) {
378 PMD_DRV_LOG(ERR, "Received bad msg size");
381 *((__u64 *)arg) = msg.payload.u64;
383 case VHOST_USER_GET_VRING_BASE:
384 if (msg.size != sizeof(m.payload.state)) {
385 PMD_DRV_LOG(ERR, "Received bad msg size");
388 memcpy(arg, &msg.payload.state,
389 sizeof(struct vhost_vring_state));
392 PMD_DRV_LOG(ERR, "Received unexpected msg type");
401 * Set up environment to talk with a vhost user backend.
403 * - The path to vhost user unix socket file.
406 * - (-1) if fail to set up;
407 * - (>=0) if successful, and it is the fd to vhostfd.
410 vhost_user_setup(const char *path)
414 struct sockaddr_un un;
416 fd = socket(AF_UNIX, SOCK_STREAM, 0);
418 PMD_DRV_LOG(ERR, "socket() error, %s", strerror(errno));
422 flag = fcntl(fd, F_GETFD);
423 if (fcntl(fd, F_SETFD, flag | FD_CLOEXEC) < 0)
424 PMD_DRV_LOG(WARNING, "fcntl failed, %s", strerror(errno));
426 memset(&un, 0, sizeof(un));
427 un.sun_family = AF_UNIX;
428 snprintf(un.sun_path, sizeof(un.sun_path), "%s", path);
429 if (connect(fd, (struct sockaddr *)&un, sizeof(un)) < 0) {
430 PMD_DRV_LOG(ERR, "connect error, %s", strerror(errno));
439 vhost_user_enable_queue_pair(int vhostfd, uint16_t pair_idx, int enable)
443 for (i = 0; i < 2; ++i) {
444 struct vhost_vring_state state = {
445 .index = pair_idx * 2 + i,
449 if (vhost_user_sock(vhostfd,
450 VHOST_USER_SET_VRING_ENABLE, &state))