New upstream version 17.11-rc3
[deb_dpdk.git] / drivers / net / virtio / virtio_user / vhost_user.c
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 <sys/socket.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <unistd.h>
38 #include <fcntl.h>
39 #include <sys/un.h>
40 #include <string.h>
41 #include <errno.h>
42
43 #include "vhost.h"
44 #include "virtio_user_dev.h"
45
46 /* The version of the protocol we support */
47 #define VHOST_USER_VERSION    0x1
48
49 #define VHOST_MEMORY_MAX_NREGIONS 8
50 struct vhost_memory {
51         uint32_t nregions;
52         uint32_t padding;
53         struct vhost_memory_region regions[VHOST_MEMORY_MAX_NREGIONS];
54 };
55
56 struct vhost_user_msg {
57         enum vhost_user_request request;
58
59 #define VHOST_USER_VERSION_MASK     0x3
60 #define VHOST_USER_REPLY_MASK       (0x1 << 2)
61         uint32_t flags;
62         uint32_t size; /* the following payload size */
63         union {
64 #define VHOST_USER_VRING_IDX_MASK   0xff
65 #define VHOST_USER_VRING_NOFD_MASK  (0x1 << 8)
66                 uint64_t u64;
67                 struct vhost_vring_state state;
68                 struct vhost_vring_addr addr;
69                 struct vhost_memory memory;
70         } payload;
71         int fds[VHOST_MEMORY_MAX_NREGIONS];
72 } __attribute((packed));
73
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)
77
78 static int
79 vhost_user_write(int fd, void *buf, int len, int *fds, int fd_num)
80 {
81         int r;
82         struct msghdr msgh;
83         struct iovec iov;
84         size_t fd_size = fd_num * sizeof(int);
85         char control[CMSG_SPACE(fd_size)];
86         struct cmsghdr *cmsg;
87
88         memset(&msgh, 0, sizeof(msgh));
89         memset(control, 0, sizeof(control));
90
91         iov.iov_base = (uint8_t *)buf;
92         iov.iov_len = len;
93
94         msgh.msg_iov = &iov;
95         msgh.msg_iovlen = 1;
96         msgh.msg_control = control;
97         msgh.msg_controllen = sizeof(control);
98
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);
104
105         do {
106                 r = sendmsg(fd, &msgh, 0);
107         } while (r < 0 && errno == EINTR);
108
109         return r;
110 }
111
112 static int
113 vhost_user_read(int fd, struct vhost_user_msg *msg)
114 {
115         uint32_t valid_flags = VHOST_USER_REPLY_MASK | VHOST_USER_VERSION;
116         int ret, sz_hdr = VHOST_USER_HDR_SIZE, sz_payload;
117
118         ret = recv(fd, (void *)msg, sz_hdr, 0);
119         if (ret < sz_hdr) {
120                 PMD_DRV_LOG(ERR, "Failed to recv msg hdr: %d instead of %d.",
121                             ret, sz_hdr);
122                 goto fail;
123         }
124
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);
129                 goto fail;
130         }
131
132         sz_payload = msg->size;
133
134         if ((size_t)sz_payload > sizeof(msg->payload))
135                 goto fail;
136
137         if (sz_payload) {
138                 ret = recv(fd, (void *)((char *)msg + sz_hdr), sz_payload, 0);
139                 if (ret < sz_payload) {
140                         PMD_DRV_LOG(ERR,
141                                 "Failed to recv msg payload: %d instead of %d.",
142                                 ret, msg->size);
143                         goto fail;
144                 }
145         }
146
147         return 0;
148
149 fail:
150         return -1;
151 }
152
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 */
157 };
158
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.
164  *
165  * We choose option 2.
166  */
167 static int
168 get_hugepage_file_info(struct hugepage_file_info huges[], int max)
169 {
170         int idx;
171         FILE *f;
172         char buf[BUFSIZ], *tmp, *tail;
173         char *str_underline, *str_start;
174         int huge_index;
175         uint64_t v_start, v_end;
176
177         f = fopen("/proc/self/maps", "r");
178         if (!f) {
179                 PMD_DRV_LOG(ERR, "cannot open /proc/self/maps");
180                 return -1;
181         }
182
183         idx = 0;
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");
187                         goto error;
188                 }
189
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 */
196                         tmp++;
197                 tail = strrchr(tmp, '\n');  /** remove newline if exists */
198                 if (tail)
199                         *tail = '\0';
200
201                 /* Match HUGEFILE_FMT, aka "%s/%smap_%d",
202                  * which is defined in eal_filesystem.h
203                  */
204                 str_underline = strrchr(tmp, '_');
205                 if (!str_underline)
206                         continue;
207
208                 str_start = str_underline - strlen("map");
209                 if (str_start < tmp)
210                         continue;
211
212                 if (sscanf(str_start, "map_%d", &huge_index) != 1)
213                         continue;
214
215                 if (idx >= max) {
216                         PMD_DRV_LOG(ERR, "Exceed maximum of %d", max);
217                         goto error;
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         fclose(f);
226         return idx;
227
228 error:
229         fclose(f);
230         return -1;
231 }
232
233 static int
234 prepare_vhost_memory_user(struct vhost_user_msg *msg, int fds[])
235 {
236         int i, num;
237         struct hugepage_file_info huges[VHOST_MEMORY_MAX_NREGIONS];
238         struct vhost_memory_region *mr;
239
240         num = get_hugepage_file_info(huges, VHOST_MEMORY_MAX_NREGIONS);
241         if (num < 0) {
242                 PMD_INIT_LOG(ERR, "Failed to prepare memory for vhost-user");
243                 return -1;
244         }
245
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;
251                 mr->mmap_offset = 0;
252                 fds[i] = open(huges[i].path, O_RDWR);
253         }
254
255         msg->payload.memory.nregions = num;
256         msg->payload.memory.padding = 0;
257
258         return 0;
259 }
260
261 static struct vhost_user_msg m;
262
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",
276 };
277
278 static int
279 vhost_user_sock(struct virtio_user_dev *dev,
280                 enum vhost_user_request req,
281                 void *arg)
282 {
283         struct vhost_user_msg msg;
284         struct vhost_vring_file *file = 0;
285         int need_reply = 0;
286         int fds[VHOST_MEMORY_MAX_NREGIONS];
287         int fd_num = 0;
288         int i, len;
289         int vhostfd = dev->vhostfd;
290
291         RTE_SET_USED(m);
292
293         PMD_DRV_LOG(INFO, "%s", vhost_msg_strings[req]);
294
295         msg.request = req;
296         msg.flags = VHOST_USER_VERSION;
297         msg.size = 0;
298
299         switch (req) {
300         case VHOST_USER_GET_FEATURES:
301                 need_reply = 1;
302                 break;
303
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);
308                 break;
309
310         case VHOST_USER_SET_OWNER:
311         case VHOST_USER_RESET_OWNER:
312                 break;
313
314         case VHOST_USER_SET_MEM_TABLE:
315                 if (prepare_vhost_memory_user(&msg, fds) < 0)
316                         return -1;
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);
321                 break;
322
323         case VHOST_USER_SET_LOG_FD:
324                 fds[fd_num++] = *((int *)arg);
325                 break;
326
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);
332                 break;
333
334         case VHOST_USER_GET_VRING_BASE:
335                 memcpy(&msg.payload.state, arg, sizeof(msg.payload.state));
336                 msg.size = sizeof(m.payload.state);
337                 need_reply = 1;
338                 break;
339
340         case VHOST_USER_SET_VRING_ADDR:
341                 memcpy(&msg.payload.addr, arg, sizeof(msg.payload.addr));
342                 msg.size = sizeof(m.payload.addr);
343                 break;
344
345         case VHOST_USER_SET_VRING_KICK:
346         case VHOST_USER_SET_VRING_CALL:
347         case VHOST_USER_SET_VRING_ERR:
348                 file = arg;
349                 msg.payload.u64 = file->index & VHOST_USER_VRING_IDX_MASK;
350                 msg.size = sizeof(m.payload.u64);
351                 if (file->fd > 0)
352                         fds[fd_num++] = file->fd;
353                 else
354                         msg.payload.u64 |= VHOST_USER_VRING_NOFD_MASK;
355                 break;
356
357         default:
358                 PMD_DRV_LOG(ERR, "trying to send unhandled msg type");
359                 return -1;
360         }
361
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));
366                 return -1;
367         }
368
369         if (req == VHOST_USER_SET_MEM_TABLE)
370                 for (i = 0; i < fd_num; ++i)
371                         close(fds[i]);
372
373         if (need_reply) {
374                 if (vhost_user_read(vhostfd, &msg) < 0) {
375                         PMD_DRV_LOG(ERR, "Received msg failed: %s",
376                                     strerror(errno));
377                         return -1;
378                 }
379
380                 if (req != msg.request) {
381                         PMD_DRV_LOG(ERR, "Received unexpected msg type");
382                         return -1;
383                 }
384
385                 switch (req) {
386                 case VHOST_USER_GET_FEATURES:
387                         if (msg.size != sizeof(m.payload.u64)) {
388                                 PMD_DRV_LOG(ERR, "Received bad msg size");
389                                 return -1;
390                         }
391                         *((__u64 *)arg) = msg.payload.u64;
392                         break;
393                 case VHOST_USER_GET_VRING_BASE:
394                         if (msg.size != sizeof(m.payload.state)) {
395                                 PMD_DRV_LOG(ERR, "Received bad msg size");
396                                 return -1;
397                         }
398                         memcpy(arg, &msg.payload.state,
399                                sizeof(struct vhost_vring_state));
400                         break;
401                 default:
402                         PMD_DRV_LOG(ERR, "Received unexpected msg type");
403                         return -1;
404                 }
405         }
406
407         return 0;
408 }
409
410 /**
411  * Set up environment to talk with a vhost user backend.
412  *
413  * @return
414  *   - (-1) if fail;
415  *   - (0) if succeed.
416  */
417 static int
418 vhost_user_setup(struct virtio_user_dev *dev)
419 {
420         int fd;
421         int flag;
422         struct sockaddr_un un;
423
424         fd = socket(AF_UNIX, SOCK_STREAM, 0);
425         if (fd < 0) {
426                 PMD_DRV_LOG(ERR, "socket() error, %s", strerror(errno));
427                 return -1;
428         }
429
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));
433
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));
439                 close(fd);
440                 return -1;
441         }
442
443         dev->vhostfd = fd;
444         return 0;
445 }
446
447 static int
448 vhost_user_enable_queue_pair(struct virtio_user_dev *dev,
449                              uint16_t pair_idx,
450                              int enable)
451 {
452         int i;
453
454         for (i = 0; i < 2; ++i) {
455                 struct vhost_vring_state state = {
456                         .index = pair_idx * 2 + i,
457                         .num   = enable,
458                 };
459
460                 if (vhost_user_sock(dev, VHOST_USER_SET_VRING_ENABLE, &state))
461                         return -1;
462         }
463
464         return 0;
465 }
466
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
471 };