]> git.proxmox.com Git - ceph.git/blame - ceph/src/spdk/lib/virtio/virtio_user/vhost_user.c
bump version to 15.2.11-pve1
[ceph.git] / ceph / src / spdk / lib / virtio / virtio_user / vhost_user.c
CommitLineData
11fdf7f2
TL
1/*-
2 * BSD LICENSE
3 *
4 * Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
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
16 * distribution.
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.
20 *
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.
32 */
33
34#include "spdk/stdinc.h"
35
36#include "vhost.h"
37
38#include "spdk/string.h"
9f95a23c 39#include "spdk_internal/vhost_user.h"
11fdf7f2
TL
40
41/* The version of the protocol we support */
42#define VHOST_USER_VERSION 0x1
43
11fdf7f2
TL
44static int
45vhost_user_write(int fd, void *buf, int len, int *fds, int fd_num)
46{
47 int r;
48 struct msghdr msgh;
49 struct iovec iov;
50 size_t fd_size = fd_num * sizeof(int);
51 char control[CMSG_SPACE(fd_size)];
52 struct cmsghdr *cmsg;
53
54 memset(&msgh, 0, sizeof(msgh));
55 memset(control, 0, sizeof(control));
56
57 iov.iov_base = (uint8_t *)buf;
58 iov.iov_len = len;
59
60 msgh.msg_iov = &iov;
61 msgh.msg_iovlen = 1;
62
63 if (fds && fd_num > 0) {
64 msgh.msg_control = control;
65 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);
71 } else {
72 msgh.msg_control = NULL;
73 msgh.msg_controllen = 0;
74 }
75
76 do {
77 r = sendmsg(fd, &msgh, 0);
78 } while (r < 0 && errno == EINTR);
79
80 if (r == -1) {
81 return -errno;
82 }
83
84 return 0;
85}
86
87static int
88vhost_user_read(int fd, struct vhost_user_msg *msg)
89{
90 uint32_t valid_flags = VHOST_USER_REPLY_MASK | VHOST_USER_VERSION;
91 ssize_t ret;
92 size_t sz_hdr = VHOST_USER_HDR_SIZE, sz_payload;
93
94 ret = recv(fd, (void *)msg, sz_hdr, 0);
95 if ((size_t)ret != sz_hdr) {
96 SPDK_WARNLOG("Failed to recv msg hdr: %zd instead of %zu.\n",
97 ret, sz_hdr);
98 if (ret == -1) {
99 return -errno;
100 } else {
101 return -EBUSY;
102 }
103 }
104
105 /* validate msg flags */
106 if (msg->flags != (valid_flags)) {
107 SPDK_WARNLOG("Failed to recv msg: flags %"PRIx32" instead of %"PRIx32".\n",
108 msg->flags, valid_flags);
109 return -EIO;
110 }
111
112 sz_payload = msg->size;
113
9f95a23c 114 if (sz_payload > VHOST_USER_PAYLOAD_SIZE) {
11fdf7f2 115 SPDK_WARNLOG("Received oversized msg: payload size %zu > available space %zu\n",
9f95a23c 116 sz_payload, VHOST_USER_PAYLOAD_SIZE);
11fdf7f2
TL
117 return -EIO;
118 }
119
120 if (sz_payload) {
121 ret = recv(fd, (void *)((char *)msg + sz_hdr), sz_payload, 0);
122 if ((size_t)ret != sz_payload) {
123 SPDK_WARNLOG("Failed to recv msg payload: %zd instead of %"PRIu32".\n",
124 ret, msg->size);
125 if (ret == -1) {
126 return -errno;
127 } else {
128 return -EBUSY;
129 }
130 }
131 }
132
133 return 0;
134}
135
136struct hugepage_file_info {
137 uint64_t addr; /**< virtual addr */
138 size_t size; /**< the file size */
139 char path[PATH_MAX]; /**< path to backing file */
140};
141
142/* Two possible options:
143 * 1. Match HUGEPAGE_INFO_FMT to find the file storing struct hugepage_file
144 * array. This is simple but cannot be used in secondary process because
145 * secondary process will close and munmap that file.
146 * 2. Match HUGEFILE_FMT to find hugepage files directly.
147 *
148 * We choose option 2.
149 */
150static int
151get_hugepage_file_info(struct hugepage_file_info huges[], int max)
152{
153 int idx, rc;
154 FILE *f;
155 char buf[BUFSIZ], *tmp, *tail;
156 char *str_underline, *str_start;
157 int huge_index;
158 uint64_t v_start, v_end;
159
160 f = fopen("/proc/self/maps", "r");
161 if (!f) {
162 SPDK_ERRLOG("cannot open /proc/self/maps\n");
163 rc = -errno;
164 assert(rc < 0); /* scan-build hack */
165 return rc;
166 }
167
168 idx = 0;
169 while (fgets(buf, sizeof(buf), f) != NULL) {
170 if (sscanf(buf, "%" PRIx64 "-%" PRIx64, &v_start, &v_end) < 2) {
171 SPDK_ERRLOG("Failed to parse address\n");
172 rc = -EIO;
173 goto out;
174 }
175
176 tmp = strchr(buf, ' ') + 1; /** skip address */
177 tmp = strchr(tmp, ' ') + 1; /** skip perm */
178 tmp = strchr(tmp, ' ') + 1; /** skip offset */
179 tmp = strchr(tmp, ' ') + 1; /** skip dev */
180 tmp = strchr(tmp, ' ') + 1; /** skip inode */
181 while (*tmp == ' ') { /** skip spaces */
182 tmp++;
183 }
184 tail = strrchr(tmp, '\n'); /** remove newline if exists */
185 if (tail) {
186 *tail = '\0';
187 }
188
189 /* Match HUGEFILE_FMT, aka "%s/%smap_%d",
190 * which is defined in eal_filesystem.h
191 */
192 str_underline = strrchr(tmp, '_');
193 if (!str_underline) {
194 continue;
195 }
196
197 str_start = str_underline - strlen("map");
198 if (str_start < tmp) {
199 continue;
200 }
201
202 if (sscanf(str_start, "map_%d", &huge_index) != 1) {
203 continue;
204 }
205
206 if (idx >= max) {
207 SPDK_ERRLOG("Exceed maximum of %d\n", max);
208 rc = -ENOSPC;
209 goto out;
210 }
211
212 if (idx > 0 &&
213 strncmp(tmp, huges[idx - 1].path, PATH_MAX) == 0 &&
214 v_start == huges[idx - 1].addr + huges[idx - 1].size) {
215 huges[idx - 1].size += (v_end - v_start);
216 continue;
217 }
218
219 huges[idx].addr = v_start;
220 huges[idx].size = v_end - v_start;
221 snprintf(huges[idx].path, PATH_MAX, "%s", tmp);
222 idx++;
223 }
224
225 rc = idx;
226out:
227 fclose(f);
228 return rc;
229}
230
231static int
232prepare_vhost_memory_user(struct vhost_user_msg *msg, int fds[])
233{
234 int i, num;
9f95a23c 235 struct hugepage_file_info huges[VHOST_USER_MEMORY_MAX_NREGIONS];
11fdf7f2 236
9f95a23c 237 num = get_hugepage_file_info(huges, VHOST_USER_MEMORY_MAX_NREGIONS);
11fdf7f2
TL
238 if (num < 0) {
239 SPDK_ERRLOG("Failed to prepare memory for vhost-user\n");
240 return num;
241 }
242
243 for (i = 0; i < num; ++i) {
244 /* the memory regions are unaligned */
245 msg->payload.memory.regions[i].guest_phys_addr = huges[i].addr; /* use vaddr! */
246 msg->payload.memory.regions[i].userspace_addr = huges[i].addr;
247 msg->payload.memory.regions[i].memory_size = huges[i].size;
248 msg->payload.memory.regions[i].flags_padding = 0;
249 fds[i] = open(huges[i].path, O_RDWR);
250 }
251
252 msg->payload.memory.nregions = num;
253 msg->payload.memory.padding = 0;
254
255 return 0;
256}
257
258static const char *const vhost_msg_strings[VHOST_USER_MAX] = {
259 [VHOST_USER_SET_OWNER] = "VHOST_SET_OWNER",
260 [VHOST_USER_RESET_OWNER] = "VHOST_RESET_OWNER",
261 [VHOST_USER_SET_FEATURES] = "VHOST_SET_FEATURES",
262 [VHOST_USER_GET_FEATURES] = "VHOST_GET_FEATURES",
263 [VHOST_USER_SET_VRING_CALL] = "VHOST_SET_VRING_CALL",
264 [VHOST_USER_SET_VRING_NUM] = "VHOST_SET_VRING_NUM",
265 [VHOST_USER_SET_VRING_BASE] = "VHOST_SET_VRING_BASE",
266 [VHOST_USER_GET_VRING_BASE] = "VHOST_GET_VRING_BASE",
267 [VHOST_USER_SET_VRING_ADDR] = "VHOST_SET_VRING_ADDR",
268 [VHOST_USER_SET_VRING_KICK] = "VHOST_SET_VRING_KICK",
269 [VHOST_USER_SET_MEM_TABLE] = "VHOST_SET_MEM_TABLE",
270 [VHOST_USER_SET_VRING_ENABLE] = "VHOST_SET_VRING_ENABLE",
271 [VHOST_USER_GET_QUEUE_NUM] = "VHOST_USER_GET_QUEUE_NUM",
272 [VHOST_USER_GET_CONFIG] = "VHOST_USER_GET_CONFIG",
273 [VHOST_USER_SET_CONFIG] = "VHOST_USER_SET_CONFIG",
274};
275
276static int
277vhost_user_sock(struct virtio_user_dev *dev,
278 enum vhost_user_request req,
279 void *arg)
280{
281 struct vhost_user_msg msg;
282 struct vhost_vring_file *file = 0;
283 int need_reply = 0;
9f95a23c 284 int fds[VHOST_USER_MEMORY_MAX_NREGIONS];
11fdf7f2
TL
285 int fd_num = 0;
286 int i, len, rc;
287 int vhostfd = dev->vhostfd;
288
289 SPDK_DEBUGLOG(SPDK_LOG_VIRTIO_USER, "sent message %d = %s\n", req, vhost_msg_strings[req]);
290
291 msg.request = req;
292 msg.flags = VHOST_USER_VERSION;
293 msg.size = 0;
294
295 switch (req) {
296 case VHOST_USER_GET_FEATURES:
297 case VHOST_USER_GET_PROTOCOL_FEATURES:
298 case VHOST_USER_GET_QUEUE_NUM:
299 need_reply = 1;
300 break;
301
302 case VHOST_USER_SET_FEATURES:
303 case VHOST_USER_SET_LOG_BASE:
304 case VHOST_USER_SET_PROTOCOL_FEATURES:
305 msg.payload.u64 = *((__u64 *)arg);
306 msg.size = sizeof(msg.payload.u64);
307 break;
308
309 case VHOST_USER_SET_OWNER:
310 case VHOST_USER_RESET_OWNER:
311 break;
312
313 case VHOST_USER_SET_MEM_TABLE:
314 rc = prepare_vhost_memory_user(&msg, fds);
315 if (rc < 0) {
316 return rc;
317 }
318 fd_num = msg.payload.memory.nregions;
319 msg.size = sizeof(msg.payload.memory.nregions);
320 msg.size += sizeof(msg.payload.memory.padding);
321 msg.size += fd_num * sizeof(struct vhost_memory_region);
322 break;
323
324 case VHOST_USER_SET_LOG_FD:
325 fds[fd_num++] = *((int *)arg);
326 break;
327
328 case VHOST_USER_SET_VRING_NUM:
329 case VHOST_USER_SET_VRING_BASE:
330 case VHOST_USER_SET_VRING_ENABLE:
331 memcpy(&msg.payload.state, arg, sizeof(msg.payload.state));
332 msg.size = sizeof(msg.payload.state);
333 break;
334
335 case VHOST_USER_GET_VRING_BASE:
336 memcpy(&msg.payload.state, arg, sizeof(msg.payload.state));
337 msg.size = sizeof(msg.payload.state);
338 need_reply = 1;
339 break;
340
341 case VHOST_USER_SET_VRING_ADDR:
342 memcpy(&msg.payload.addr, arg, sizeof(msg.payload.addr));
343 msg.size = sizeof(msg.payload.addr);
344 break;
345
346 case VHOST_USER_SET_VRING_KICK:
347 case VHOST_USER_SET_VRING_CALL:
348 case VHOST_USER_SET_VRING_ERR:
349 file = arg;
350 msg.payload.u64 = file->index & VHOST_USER_VRING_IDX_MASK;
351 msg.size = sizeof(msg.payload.u64);
352 if (file->fd > 0) {
353 fds[fd_num++] = file->fd;
354 } else {
355 msg.payload.u64 |= VHOST_USER_VRING_NOFD_MASK;
356 }
357 break;
358
359 case VHOST_USER_GET_CONFIG:
360 memcpy(&msg.payload.cfg, arg, sizeof(msg.payload.cfg));
361 msg.size = sizeof(msg.payload.cfg);
362 need_reply = 1;
363 break;
364
365 case VHOST_USER_SET_CONFIG:
366 memcpy(&msg.payload.cfg, arg, sizeof(msg.payload.cfg));
367 msg.size = sizeof(msg.payload.cfg);
368 break;
369
370 default:
371 SPDK_ERRLOG("trying to send unknown msg\n");
372 return -EINVAL;
373 }
374
375 len = VHOST_USER_HDR_SIZE + msg.size;
376 rc = vhost_user_write(vhostfd, &msg, len, fds, fd_num);
377 if (rc < 0) {
378 SPDK_ERRLOG("%s failed: %s\n",
379 vhost_msg_strings[req], spdk_strerror(-rc));
380 return rc;
381 }
382
383 if (req == VHOST_USER_SET_MEM_TABLE)
384 for (i = 0; i < fd_num; ++i) {
385 close(fds[i]);
386 }
387
388 if (need_reply) {
389 rc = vhost_user_read(vhostfd, &msg);
390 if (rc < 0) {
391 SPDK_WARNLOG("Received msg failed: %s\n", spdk_strerror(-rc));
392 return rc;
393 }
394
395 if (req != msg.request) {
396 SPDK_WARNLOG("Received unexpected msg type\n");
397 return -EIO;
398 }
399
400 switch (req) {
401 case VHOST_USER_GET_FEATURES:
402 case VHOST_USER_GET_PROTOCOL_FEATURES:
403 case VHOST_USER_GET_QUEUE_NUM:
404 if (msg.size != sizeof(msg.payload.u64)) {
405 SPDK_WARNLOG("Received bad msg size\n");
406 return -EIO;
407 }
408 *((__u64 *)arg) = msg.payload.u64;
409 break;
410 case VHOST_USER_GET_VRING_BASE:
411 if (msg.size != sizeof(msg.payload.state)) {
412 SPDK_WARNLOG("Received bad msg size\n");
413 return -EIO;
414 }
415 memcpy(arg, &msg.payload.state,
416 sizeof(struct vhost_vring_state));
417 break;
418 case VHOST_USER_GET_CONFIG:
419 if (msg.size != sizeof(msg.payload.cfg)) {
420 SPDK_WARNLOG("Received bad msg size\n");
421 return -EIO;
422 }
423 memcpy(arg, &msg.payload.cfg, sizeof(msg.payload.cfg));
424 break;
425 default:
426 SPDK_WARNLOG("Received unexpected msg type\n");
427 return -EBADMSG;
428 }
429 }
430
431 return 0;
432}
433
434/**
435 * Set up environment to talk with a vhost user backend.
436 *
437 * @return
438 * - (-1) if fail;
439 * - (0) if succeed.
440 */
441static int
442vhost_user_setup(struct virtio_user_dev *dev)
443{
444 int fd;
445 int flag;
446 struct sockaddr_un un;
447 ssize_t rc;
448
449 fd = socket(AF_UNIX, SOCK_STREAM, 0);
450 if (fd < 0) {
451 SPDK_ERRLOG("socket() error, %s\n", spdk_strerror(errno));
452 return -errno;
453 }
454
455 flag = fcntl(fd, F_GETFD);
456 if (fcntl(fd, F_SETFD, flag | FD_CLOEXEC) < 0) {
457 SPDK_ERRLOG("fcntl failed, %s\n", spdk_strerror(errno));
458 }
459
460 memset(&un, 0, sizeof(un));
461 un.sun_family = AF_UNIX;
462 rc = snprintf(un.sun_path, sizeof(un.sun_path), "%s", dev->path);
463 if (rc < 0 || (size_t)rc >= sizeof(un.sun_path)) {
464 SPDK_ERRLOG("socket path too long\n");
465 close(fd);
466 if (rc < 0) {
467 return -errno;
468 } else {
469 return -EINVAL;
470 }
471 }
472 if (connect(fd, (struct sockaddr *)&un, sizeof(un)) < 0) {
473 SPDK_ERRLOG("connect error, %s\n", spdk_strerror(errno));
474 close(fd);
475 return -errno;
476 }
477
478 dev->vhostfd = fd;
479 return 0;
480}
481
482struct virtio_user_backend_ops ops_user = {
483 .setup = vhost_user_setup,
484 .send_request = vhost_user_sock,
485};
486
487SPDK_LOG_REGISTER_COMPONENT("virtio_user", SPDK_LOG_VIRTIO_USER)