net/virtio-user: move vhost-user specific code
[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 /* The version of the protocol we support */
46 #define VHOST_USER_VERSION    0x1
47
48 #define VHOST_MEMORY_MAX_NREGIONS 8
49 struct vhost_memory {
50         uint32_t nregions;
51         uint32_t padding;
52         struct vhost_memory_region regions[VHOST_MEMORY_MAX_NREGIONS];
53 };
54
55 struct vhost_user_msg {
56         enum vhost_user_request request;
57
58 #define VHOST_USER_VERSION_MASK     0x3
59 #define VHOST_USER_REPLY_MASK       (0x1 << 2)
60         uint32_t flags;
61         uint32_t size; /* the following payload size */
62         union {
63 #define VHOST_USER_VRING_IDX_MASK   0xff
64 #define VHOST_USER_VRING_NOFD_MASK  (0x1 << 8)
65                 uint64_t u64;
66                 struct vhost_vring_state state;
67                 struct vhost_vring_addr addr;
68                 struct vhost_memory memory;
69         } payload;
70         int fds[VHOST_MEMORY_MAX_NREGIONS];
71 } __attribute((packed));
72
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)
76
77 static int
78 vhost_user_write(int fd, void *buf, int len, int *fds, int fd_num)
79 {
80         int r;
81         struct msghdr msgh;
82         struct iovec iov;
83         size_t fd_size = fd_num * sizeof(int);
84         char control[CMSG_SPACE(fd_size)];
85         struct cmsghdr *cmsg;
86
87         memset(&msgh, 0, sizeof(msgh));
88         memset(control, 0, sizeof(control));
89
90         iov.iov_base = (uint8_t *)buf;
91         iov.iov_len = len;
92
93         msgh.msg_iov = &iov;
94         msgh.msg_iovlen = 1;
95         msgh.msg_control = control;
96         msgh.msg_controllen = sizeof(control);
97
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);
103
104         do {
105                 r = sendmsg(fd, &msgh, 0);
106         } while (r < 0 && errno == EINTR);
107
108         return r;
109 }
110
111 static int
112 vhost_user_read(int fd, struct vhost_user_msg *msg)
113 {
114         uint32_t valid_flags = VHOST_USER_REPLY_MASK | VHOST_USER_VERSION;
115         int ret, sz_hdr = VHOST_USER_HDR_SIZE, sz_payload;
116
117         ret = recv(fd, (void *)msg, sz_hdr, 0);
118         if (ret < sz_hdr) {
119                 PMD_DRV_LOG(ERR, "Failed to recv msg hdr: %d instead of %d.",
120                             ret, sz_hdr);
121                 goto fail;
122         }
123
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);
128                 goto fail;
129         }
130
131         sz_payload = msg->size;
132         if (sz_payload) {
133                 ret = recv(fd, (void *)((char *)msg + sz_hdr), sz_payload, 0);
134                 if (ret < sz_payload) {
135                         PMD_DRV_LOG(ERR,
136                                 "Failed to recv msg payload: %d instead of %d.",
137                                 ret, msg->size);
138                         goto fail;
139                 }
140         }
141
142         return 0;
143
144 fail:
145         return -1;
146 }
147
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 */
152 };
153
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.
159  *
160  * We choose option 2.
161  */
162 static int
163 get_hugepage_file_info(struct hugepage_file_info huges[], int max)
164 {
165         int idx;
166         FILE *f;
167         char buf[BUFSIZ], *tmp, *tail;
168         char *str_underline, *str_start;
169         int huge_index;
170         uint64_t v_start, v_end;
171
172         f = fopen("/proc/self/maps", "r");
173         if (!f) {
174                 PMD_DRV_LOG(ERR, "cannot open /proc/self/maps");
175                 return -1;
176         }
177
178         idx = 0;
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");
182                         goto error;
183                 }
184
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 */
191                         tmp++;
192                 tail = strrchr(tmp, '\n');  /** remove newline if exists */
193                 if (tail)
194                         *tail = '\0';
195
196                 /* Match HUGEFILE_FMT, aka "%s/%smap_%d",
197                  * which is defined in eal_filesystem.h
198                  */
199                 str_underline = strrchr(tmp, '_');
200                 if (!str_underline)
201                         continue;
202
203                 str_start = str_underline - strlen("map");
204                 if (str_start < tmp)
205                         continue;
206
207                 if (sscanf(str_start, "map_%d", &huge_index) != 1)
208                         continue;
209
210                 if (idx >= max) {
211                         PMD_DRV_LOG(ERR, "Exceed maximum of %d", max);
212                         goto error;
213                 }
214                 huges[idx].addr = v_start;
215                 huges[idx].size = v_end - v_start;
216                 snprintf(huges[idx].path, PATH_MAX, "%s", tmp);
217                 idx++;
218         }
219
220         fclose(f);
221         return idx;
222
223 error:
224         fclose(f);
225         return -1;
226 }
227
228 static int
229 prepare_vhost_memory_user(struct vhost_user_msg *msg, int fds[])
230 {
231         int i, num;
232         struct hugepage_file_info huges[VHOST_MEMORY_MAX_NREGIONS];
233         struct vhost_memory_region *mr;
234
235         num = get_hugepage_file_info(huges, VHOST_MEMORY_MAX_NREGIONS);
236         if (num < 0) {
237                 PMD_INIT_LOG(ERR, "Failed to prepare memory for vhost-user");
238                 return -1;
239         }
240
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;
246                 mr->mmap_offset = 0;
247                 fds[i] = open(huges[i].path, O_RDWR);
248         }
249
250         msg->payload.memory.nregions = num;
251         msg->payload.memory.padding = 0;
252
253         return 0;
254 }
255
256 static struct vhost_user_msg m;
257
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",
271         NULL,
272 };
273
274 int
275 vhost_user_sock(int vhostfd, enum vhost_user_request req, void *arg)
276 {
277         struct vhost_user_msg msg;
278         struct vhost_vring_file *file = 0;
279         int need_reply = 0;
280         int fds[VHOST_MEMORY_MAX_NREGIONS];
281         int fd_num = 0;
282         int i, len;
283
284         RTE_SET_USED(m);
285         RTE_SET_USED(vhost_msg_strings);
286
287         PMD_DRV_LOG(INFO, "%s", vhost_msg_strings[req]);
288
289         msg.request = req;
290         msg.flags = VHOST_USER_VERSION;
291         msg.size = 0;
292
293         switch (req) {
294         case VHOST_USER_GET_FEATURES:
295                 need_reply = 1;
296                 break;
297
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);
302                 break;
303
304         case VHOST_USER_SET_OWNER:
305         case VHOST_USER_RESET_OWNER:
306                 break;
307
308         case VHOST_USER_SET_MEM_TABLE:
309                 if (prepare_vhost_memory_user(&msg, fds) < 0)
310                         return -1;
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);
315                 break;
316
317         case VHOST_USER_SET_LOG_FD:
318                 fds[fd_num++] = *((int *)arg);
319                 break;
320
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);
326                 break;
327
328         case VHOST_USER_GET_VRING_BASE:
329                 memcpy(&msg.payload.state, arg, sizeof(msg.payload.state));
330                 msg.size = sizeof(m.payload.state);
331                 need_reply = 1;
332                 break;
333
334         case VHOST_USER_SET_VRING_ADDR:
335                 memcpy(&msg.payload.addr, arg, sizeof(msg.payload.addr));
336                 msg.size = sizeof(m.payload.addr);
337                 break;
338
339         case VHOST_USER_SET_VRING_KICK:
340         case VHOST_USER_SET_VRING_CALL:
341         case VHOST_USER_SET_VRING_ERR:
342                 file = arg;
343                 msg.payload.u64 = file->index & VHOST_USER_VRING_IDX_MASK;
344                 msg.size = sizeof(m.payload.u64);
345                 if (file->fd > 0)
346                         fds[fd_num++] = file->fd;
347                 else
348                         msg.payload.u64 |= VHOST_USER_VRING_NOFD_MASK;
349                 break;
350
351         default:
352                 PMD_DRV_LOG(ERR, "trying to send unhandled msg type");
353                 return -1;
354         }
355
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));
360                 return -1;
361         }
362
363         if (req == VHOST_USER_SET_MEM_TABLE)
364                 for (i = 0; i < fd_num; ++i)
365                         close(fds[i]);
366
367         if (need_reply) {
368                 if (vhost_user_read(vhostfd, &msg) < 0) {
369                         PMD_DRV_LOG(ERR, "Received msg failed: %s",
370                                     strerror(errno));
371                         return -1;
372                 }
373
374                 if (req != msg.request) {
375                         PMD_DRV_LOG(ERR, "Received unexpected msg type");
376                         return -1;
377                 }
378
379                 switch (req) {
380                 case VHOST_USER_GET_FEATURES:
381                         if (msg.size != sizeof(m.payload.u64)) {
382                                 PMD_DRV_LOG(ERR, "Received bad msg size");
383                                 return -1;
384                         }
385                         *((__u64 *)arg) = msg.payload.u64;
386                         break;
387                 case VHOST_USER_GET_VRING_BASE:
388                         if (msg.size != sizeof(m.payload.state)) {
389                                 PMD_DRV_LOG(ERR, "Received bad msg size");
390                                 return -1;
391                         }
392                         memcpy(arg, &msg.payload.state,
393                                sizeof(struct vhost_vring_state));
394                         break;
395                 default:
396                         PMD_DRV_LOG(ERR, "Received unexpected msg type");
397                         return -1;
398                 }
399         }
400
401         return 0;
402 }
403
404 /**
405  * Set up environment to talk with a vhost user backend.
406  * @param path
407  *   - The path to vhost user unix socket file.
408  *
409  * @return
410  *   - (-1) if fail to set up;
411  *   - (>=0) if successful, and it is the fd to vhostfd.
412  */
413 int
414 vhost_user_setup(const char *path)
415 {
416         int fd;
417         int flag;
418         struct sockaddr_un un;
419
420         fd = socket(AF_UNIX, SOCK_STREAM, 0);
421         if (fd < 0) {
422                 PMD_DRV_LOG(ERR, "socket() error, %s", strerror(errno));
423                 return -1;
424         }
425
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));
429
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));
435                 close(fd);
436                 return -1;
437         }
438
439         return fd;
440 }
441
442 int
443 vhost_user_enable_queue_pair(int vhostfd, uint16_t pair_idx, int enable)
444 {
445         int i;
446
447         for (i = 0; i < 2; ++i) {
448                 struct vhost_vring_state state = {
449                         .index = pair_idx * 2 + i,
450                         .num   = enable,
451                 };
452
453                 if (vhost_user_sock(vhostfd,
454                                     VHOST_USER_SET_VRING_ENABLE, &state))
455                         return -1;
456         }
457
458         return 0;
459 }