New upstream version 16.11.7
[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
45 static int
46 vhost_user_write(int fd, void *buf, int len, int *fds, int fd_num)
47 {
48         int r;
49         struct msghdr msgh;
50         struct iovec iov;
51         size_t fd_size = fd_num * sizeof(int);
52         char control[CMSG_SPACE(fd_size)];
53         struct cmsghdr *cmsg;
54
55         memset(&msgh, 0, sizeof(msgh));
56         memset(control, 0, sizeof(control));
57
58         iov.iov_base = (uint8_t *)buf;
59         iov.iov_len = len;
60
61         msgh.msg_iov = &iov;
62         msgh.msg_iovlen = 1;
63         msgh.msg_control = control;
64         msgh.msg_controllen = sizeof(control);
65
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
72         do {
73                 r = sendmsg(fd, &msgh, 0);
74         } while (r < 0 && errno == EINTR);
75
76         return r;
77 }
78
79 static int
80 vhost_user_read(int fd, struct vhost_user_msg *msg)
81 {
82         uint32_t valid_flags = VHOST_USER_REPLY_MASK | VHOST_USER_VERSION;
83         int ret, sz_hdr = VHOST_USER_HDR_SIZE, sz_payload;
84
85         ret = recv(fd, (void *)msg, sz_hdr, 0);
86         if (ret < sz_hdr) {
87                 PMD_DRV_LOG(ERR, "Failed to recv msg hdr: %d instead of %d.",
88                             ret, sz_hdr);
89                 goto fail;
90         }
91
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);
96                 goto fail;
97         }
98
99         sz_payload = msg->size;
100
101         if ((size_t)sz_payload > sizeof(msg->payload))
102                 goto fail;
103
104         if (sz_payload) {
105                 ret = recv(fd, (void *)((char *)msg + sz_hdr), sz_payload, 0);
106                 if (ret < sz_payload) {
107                         PMD_DRV_LOG(ERR,
108                                 "Failed to recv msg payload: %d instead of %d.",
109                                 ret, msg->size);
110                         goto fail;
111                 }
112         }
113
114         return 0;
115
116 fail:
117         return -1;
118 }
119
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 */
124 };
125
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.
131  *
132  * We choose option 2.
133  */
134 static int
135 get_hugepage_file_info(struct hugepage_file_info huges[], int max)
136 {
137         int idx, k, exist;
138         FILE *f;
139         char buf[BUFSIZ], *tmp, *tail;
140         char *str_underline, *str_start;
141         int huge_index;
142         uint64_t v_start, v_end;
143         struct stat stats;
144
145         f = fopen("/proc/self/maps", "r");
146         if (!f) {
147                 PMD_DRV_LOG(ERR, "cannot open /proc/self/maps");
148                 return -1;
149         }
150
151         idx = 0;
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");
155                         goto error;
156                 }
157
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 */
164                         tmp++;
165                 tail = strrchr(tmp, '\n');  /** remove newline if exists */
166                 if (tail)
167                         *tail = '\0';
168
169                 /* Match HUGEFILE_FMT, aka "%s/%smap_%d",
170                  * which is defined in eal_filesystem.h
171                  */
172                 str_underline = strrchr(tmp, '_');
173                 if (!str_underline)
174                         continue;
175
176                 str_start = str_underline - strlen("map");
177                 if (str_start < tmp)
178                         continue;
179
180                 if (sscanf(str_start, "map_%d", &huge_index) != 1)
181                         continue;
182
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)) {
186                                 exist = k;
187                                 break;
188                         }
189                 }
190                 if (exist >= 0)
191                         continue;
192
193                 if (idx >= max) {
194                         PMD_DRV_LOG(ERR, "Exceed maximum of %d", max);
195                         goto error;
196                 }
197
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);
201                 idx++;
202         }
203
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));
209                         continue;
210                 }
211                 huges[k].size = stats.st_size;
212                 PMD_DRV_LOG(INFO, "file %s, size %zx\n",
213                             huges[k].path, huges[k].size);
214         }
215
216         fclose(f);
217         return idx;
218
219 error:
220         fclose(f);
221         return -1;
222 }
223
224 static int
225 prepare_vhost_memory_user(struct vhost_user_msg *msg, int fds[])
226 {
227         int i, num;
228         struct hugepage_file_info huges[VHOST_MEMORY_MAX_NREGIONS];
229         struct vhost_memory_region *mr;
230
231         num = get_hugepage_file_info(huges, VHOST_MEMORY_MAX_NREGIONS);
232         if (num < 0) {
233                 PMD_INIT_LOG(ERR, "Failed to prepare memory for vhost-user");
234                 return -1;
235         }
236
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;
242                 mr->mmap_offset = 0;
243                 fds[i] = open(huges[i].path, O_RDWR);
244         }
245
246         msg->payload.memory.nregions = num;
247         msg->payload.memory.padding = 0;
248
249         return 0;
250 }
251
252 static struct vhost_user_msg m;
253
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",
267         NULL,
268 };
269
270 int
271 vhost_user_sock(int vhostfd, enum vhost_user_request req, void *arg)
272 {
273         struct vhost_user_msg msg;
274         struct vhost_vring_file *file = 0;
275         int need_reply = 0;
276         int fds[VHOST_MEMORY_MAX_NREGIONS];
277         int fd_num = 0;
278         int i, len;
279
280         RTE_SET_USED(m);
281         RTE_SET_USED(vhost_msg_strings);
282
283         PMD_DRV_LOG(INFO, "%s", vhost_msg_strings[req]);
284
285         msg.request = req;
286         msg.flags = VHOST_USER_VERSION;
287         msg.size = 0;
288
289         switch (req) {
290         case VHOST_USER_GET_FEATURES:
291                 need_reply = 1;
292                 break;
293
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);
298                 break;
299
300         case VHOST_USER_SET_OWNER:
301         case VHOST_USER_RESET_OWNER:
302                 break;
303
304         case VHOST_USER_SET_MEM_TABLE:
305                 if (prepare_vhost_memory_user(&msg, fds) < 0)
306                         return -1;
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);
311                 break;
312
313         case VHOST_USER_SET_LOG_FD:
314                 fds[fd_num++] = *((int *)arg);
315                 break;
316
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);
322                 break;
323
324         case VHOST_USER_GET_VRING_BASE:
325                 memcpy(&msg.payload.state, arg, sizeof(msg.payload.state));
326                 msg.size = sizeof(m.payload.state);
327                 need_reply = 1;
328                 break;
329
330         case VHOST_USER_SET_VRING_ADDR:
331                 memcpy(&msg.payload.addr, arg, sizeof(msg.payload.addr));
332                 msg.size = sizeof(m.payload.addr);
333                 break;
334
335         case VHOST_USER_SET_VRING_KICK:
336         case VHOST_USER_SET_VRING_CALL:
337         case VHOST_USER_SET_VRING_ERR:
338                 file = arg;
339                 msg.payload.u64 = file->index & VHOST_USER_VRING_IDX_MASK;
340                 msg.size = sizeof(m.payload.u64);
341                 if (file->fd > 0)
342                         fds[fd_num++] = file->fd;
343                 else
344                         msg.payload.u64 |= VHOST_USER_VRING_NOFD_MASK;
345                 break;
346
347         default:
348                 PMD_DRV_LOG(ERR, "trying to send unhandled msg type");
349                 return -1;
350         }
351
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));
356                 return -1;
357         }
358
359         if (req == VHOST_USER_SET_MEM_TABLE)
360                 for (i = 0; i < fd_num; ++i)
361                         close(fds[i]);
362
363         if (need_reply) {
364                 if (vhost_user_read(vhostfd, &msg) < 0) {
365                         PMD_DRV_LOG(ERR, "Received msg failed: %s",
366                                     strerror(errno));
367                         return -1;
368                 }
369
370                 if (req != msg.request) {
371                         PMD_DRV_LOG(ERR, "Received unexpected msg type");
372                         return -1;
373                 }
374
375                 switch (req) {
376                 case VHOST_USER_GET_FEATURES:
377                         if (msg.size != sizeof(m.payload.u64)) {
378                                 PMD_DRV_LOG(ERR, "Received bad msg size");
379                                 return -1;
380                         }
381                         *((__u64 *)arg) = msg.payload.u64;
382                         break;
383                 case VHOST_USER_GET_VRING_BASE:
384                         if (msg.size != sizeof(m.payload.state)) {
385                                 PMD_DRV_LOG(ERR, "Received bad msg size");
386                                 return -1;
387                         }
388                         memcpy(arg, &msg.payload.state,
389                                sizeof(struct vhost_vring_state));
390                         break;
391                 default:
392                         PMD_DRV_LOG(ERR, "Received unexpected msg type");
393                         return -1;
394                 }
395         }
396
397         return 0;
398 }
399
400 /**
401  * Set up environment to talk with a vhost user backend.
402  * @param path
403  *   - The path to vhost user unix socket file.
404  *
405  * @return
406  *   - (-1) if fail to set up;
407  *   - (>=0) if successful, and it is the fd to vhostfd.
408  */
409 int
410 vhost_user_setup(const char *path)
411 {
412         int fd;
413         int flag;
414         struct sockaddr_un un;
415
416         fd = socket(AF_UNIX, SOCK_STREAM, 0);
417         if (fd < 0) {
418                 PMD_DRV_LOG(ERR, "socket() error, %s", strerror(errno));
419                 return -1;
420         }
421
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));
425
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));
431                 close(fd);
432                 return -1;
433         }
434
435         return fd;
436 }
437
438 int
439 vhost_user_enable_queue_pair(int vhostfd, uint16_t pair_idx, int enable)
440 {
441         int i;
442
443         for (i = 0; i < 2; ++i) {
444                 struct vhost_vring_state state = {
445                         .index = pair_idx * 2 + i,
446                         .num   = enable,
447                 };
448
449                 if (vhost_user_sock(vhostfd,
450                                     VHOST_USER_SET_VRING_ENABLE, &state))
451                         return -1;
452         }
453
454         return 0;
455 }