vhost: add a flag to enable dequeue zero copy
[dpdk.git] / lib / librte_vhost / socket.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 <stdint.h>
35 #include <stdio.h>
36 #include <stdbool.h>
37 #include <limits.h>
38 #include <stdlib.h>
39 #include <unistd.h>
40 #include <string.h>
41 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <sys/un.h>
44 #include <sys/queue.h>
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <pthread.h>
48
49 #include <rte_log.h>
50
51 #include "fd_man.h"
52 #include "vhost.h"
53 #include "vhost_user.h"
54
55 /*
56  * Every time rte_vhost_driver_register() is invoked, an associated
57  * vhost_user_socket struct will be created.
58  */
59 struct vhost_user_socket {
60         char *path;
61         int listenfd;
62         int connfd;
63         bool is_server;
64         bool reconnect;
65         bool dequeue_zero_copy;
66 };
67
68 struct vhost_user_connection {
69         struct vhost_user_socket *vsocket;
70         int vid;
71 };
72
73 #define MAX_VHOST_SOCKET 1024
74 struct vhost_user {
75         struct vhost_user_socket *vsockets[MAX_VHOST_SOCKET];
76         struct fdset fdset;
77         int vsocket_cnt;
78         pthread_mutex_t mutex;
79 };
80
81 #define MAX_VIRTIO_BACKLOG 128
82
83 static void vhost_user_server_new_connection(int fd, void *data, int *remove);
84 static void vhost_user_read_cb(int fd, void *dat, int *remove);
85 static int vhost_user_create_client(struct vhost_user_socket *vsocket);
86
87 static struct vhost_user vhost_user = {
88         .fdset = {
89                 .fd = { [0 ... MAX_FDS - 1] = {-1, NULL, NULL, NULL, 0} },
90                 .fd_mutex = PTHREAD_MUTEX_INITIALIZER,
91                 .num = 0
92         },
93         .vsocket_cnt = 0,
94         .mutex = PTHREAD_MUTEX_INITIALIZER,
95 };
96
97 /* return bytes# of read on success or negative val on failure. */
98 int
99 read_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
100 {
101         struct iovec iov;
102         struct msghdr msgh;
103         size_t fdsize = fd_num * sizeof(int);
104         char control[CMSG_SPACE(fdsize)];
105         struct cmsghdr *cmsg;
106         int ret;
107
108         memset(&msgh, 0, sizeof(msgh));
109         iov.iov_base = buf;
110         iov.iov_len  = buflen;
111
112         msgh.msg_iov = &iov;
113         msgh.msg_iovlen = 1;
114         msgh.msg_control = control;
115         msgh.msg_controllen = sizeof(control);
116
117         ret = recvmsg(sockfd, &msgh, 0);
118         if (ret <= 0) {
119                 RTE_LOG(ERR, VHOST_CONFIG, "recvmsg failed\n");
120                 return ret;
121         }
122
123         if (msgh.msg_flags & (MSG_TRUNC | MSG_CTRUNC)) {
124                 RTE_LOG(ERR, VHOST_CONFIG, "truncted msg\n");
125                 return -1;
126         }
127
128         for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
129                 cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
130                 if ((cmsg->cmsg_level == SOL_SOCKET) &&
131                         (cmsg->cmsg_type == SCM_RIGHTS)) {
132                         memcpy(fds, CMSG_DATA(cmsg), fdsize);
133                         break;
134                 }
135         }
136
137         return ret;
138 }
139
140 int
141 send_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
142 {
143
144         struct iovec iov;
145         struct msghdr msgh;
146         size_t fdsize = fd_num * sizeof(int);
147         char control[CMSG_SPACE(fdsize)];
148         struct cmsghdr *cmsg;
149         int ret;
150
151         memset(&msgh, 0, sizeof(msgh));
152         iov.iov_base = buf;
153         iov.iov_len = buflen;
154
155         msgh.msg_iov = &iov;
156         msgh.msg_iovlen = 1;
157
158         if (fds && fd_num > 0) {
159                 msgh.msg_control = control;
160                 msgh.msg_controllen = sizeof(control);
161                 cmsg = CMSG_FIRSTHDR(&msgh);
162                 cmsg->cmsg_len = CMSG_LEN(fdsize);
163                 cmsg->cmsg_level = SOL_SOCKET;
164                 cmsg->cmsg_type = SCM_RIGHTS;
165                 memcpy(CMSG_DATA(cmsg), fds, fdsize);
166         } else {
167                 msgh.msg_control = NULL;
168                 msgh.msg_controllen = 0;
169         }
170
171         do {
172                 ret = sendmsg(sockfd, &msgh, 0);
173         } while (ret < 0 && errno == EINTR);
174
175         if (ret < 0) {
176                 RTE_LOG(ERR, VHOST_CONFIG,  "sendmsg error\n");
177                 return ret;
178         }
179
180         return ret;
181 }
182
183 static void
184 vhost_user_add_connection(int fd, struct vhost_user_socket *vsocket)
185 {
186         int vid;
187         size_t size;
188         struct vhost_user_connection *conn;
189         int ret;
190
191         conn = malloc(sizeof(*conn));
192         if (conn == NULL) {
193                 close(fd);
194                 return;
195         }
196
197         vid = vhost_new_device();
198         if (vid == -1) {
199                 close(fd);
200                 free(conn);
201                 return;
202         }
203
204         size = strnlen(vsocket->path, PATH_MAX);
205         vhost_set_ifname(vid, vsocket->path, size);
206
207         if (vsocket->dequeue_zero_copy)
208                 vhost_enable_dequeue_zero_copy(vid);
209
210         RTE_LOG(INFO, VHOST_CONFIG, "new device, handle is %d\n", vid);
211
212         vsocket->connfd = fd;
213         conn->vsocket = vsocket;
214         conn->vid = vid;
215         ret = fdset_add(&vhost_user.fdset, fd, vhost_user_read_cb,
216                         NULL, conn);
217         if (ret < 0) {
218                 vsocket->connfd = -1;
219                 free(conn);
220                 close(fd);
221                 RTE_LOG(ERR, VHOST_CONFIG,
222                         "failed to add fd %d into vhost server fdset\n",
223                         fd);
224         }
225 }
226
227 /* call back when there is new vhost-user connection from client  */
228 static void
229 vhost_user_server_new_connection(int fd, void *dat, int *remove __rte_unused)
230 {
231         struct vhost_user_socket *vsocket = dat;
232
233         fd = accept(fd, NULL, NULL);
234         if (fd < 0)
235                 return;
236
237         RTE_LOG(INFO, VHOST_CONFIG, "new vhost user connection is %d\n", fd);
238         vhost_user_add_connection(fd, vsocket);
239 }
240
241 static void
242 vhost_user_read_cb(int connfd, void *dat, int *remove)
243 {
244         struct vhost_user_connection *conn = dat;
245         struct vhost_user_socket *vsocket = conn->vsocket;
246         int ret;
247
248         ret = vhost_user_msg_handler(conn->vid, connfd);
249         if (ret < 0) {
250                 vsocket->connfd = -1;
251                 close(connfd);
252                 *remove = 1;
253                 free(conn);
254                 vhost_destroy_device(conn->vid);
255
256                 if (vsocket->reconnect)
257                         vhost_user_create_client(vsocket);
258         }
259 }
260
261 static int
262 create_unix_socket(const char *path, struct sockaddr_un *un, bool is_server)
263 {
264         int fd;
265
266         fd = socket(AF_UNIX, SOCK_STREAM, 0);
267         if (fd < 0)
268                 return -1;
269         RTE_LOG(INFO, VHOST_CONFIG, "vhost-user %s: socket created, fd: %d\n",
270                 is_server ? "server" : "client", fd);
271
272         if (!is_server && fcntl(fd, F_SETFL, O_NONBLOCK)) {
273                 RTE_LOG(ERR, VHOST_CONFIG,
274                         "vhost-user: can't set nonblocking mode for socket, fd: "
275                         "%d (%s)\n", fd, strerror(errno));
276                 close(fd);
277                 return -1;
278         }
279
280         memset(un, 0, sizeof(*un));
281         un->sun_family = AF_UNIX;
282         strncpy(un->sun_path, path, sizeof(un->sun_path));
283         un->sun_path[sizeof(un->sun_path) - 1] = '\0';
284
285         return fd;
286 }
287
288 static int
289 vhost_user_create_server(struct vhost_user_socket *vsocket)
290 {
291         int fd;
292         int ret;
293         struct sockaddr_un un;
294         const char *path = vsocket->path;
295
296         fd = create_unix_socket(path, &un, vsocket->is_server);
297         if (fd < 0)
298                 return -1;
299
300         ret = bind(fd, (struct sockaddr *)&un, sizeof(un));
301         if (ret < 0) {
302                 RTE_LOG(ERR, VHOST_CONFIG,
303                         "failed to bind to %s: %s; remove it and try again\n",
304                         path, strerror(errno));
305                 goto err;
306         }
307         RTE_LOG(INFO, VHOST_CONFIG, "bind to %s\n", path);
308
309         ret = listen(fd, MAX_VIRTIO_BACKLOG);
310         if (ret < 0)
311                 goto err;
312
313         vsocket->listenfd = fd;
314         ret = fdset_add(&vhost_user.fdset, fd, vhost_user_server_new_connection,
315                   NULL, vsocket);
316         if (ret < 0) {
317                 RTE_LOG(ERR, VHOST_CONFIG,
318                         "failed to add listen fd %d to vhost server fdset\n",
319                         fd);
320                 goto err;
321         }
322
323         return 0;
324
325 err:
326         close(fd);
327         return -1;
328 }
329
330 struct vhost_user_reconnect {
331         struct sockaddr_un un;
332         int fd;
333         struct vhost_user_socket *vsocket;
334
335         TAILQ_ENTRY(vhost_user_reconnect) next;
336 };
337
338 TAILQ_HEAD(vhost_user_reconnect_tailq_list, vhost_user_reconnect);
339 struct vhost_user_reconnect_list {
340         struct vhost_user_reconnect_tailq_list head;
341         pthread_mutex_t mutex;
342 };
343
344 static struct vhost_user_reconnect_list reconn_list;
345 static pthread_t reconn_tid;
346
347 static int
348 vhost_user_connect_nonblock(int fd, struct sockaddr *un, size_t sz)
349 {
350         int ret, flags;
351
352         ret = connect(fd, un, sz);
353         if (ret < 0 && errno != EISCONN)
354                 return -1;
355
356         flags = fcntl(fd, F_GETFL, 0);
357         if (flags < 0) {
358                 RTE_LOG(ERR, VHOST_CONFIG,
359                         "can't get flags for connfd %d\n", fd);
360                 return -2;
361         }
362         if ((flags & O_NONBLOCK) && fcntl(fd, F_SETFL, flags & ~O_NONBLOCK)) {
363                 RTE_LOG(ERR, VHOST_CONFIG,
364                                 "can't disable nonblocking on fd %d\n", fd);
365                 return -2;
366         }
367         return 0;
368 }
369
370 static void *
371 vhost_user_client_reconnect(void *arg __rte_unused)
372 {
373         int ret;
374         struct vhost_user_reconnect *reconn, *next;
375
376         while (1) {
377                 pthread_mutex_lock(&reconn_list.mutex);
378
379                 /*
380                  * An equal implementation of TAILQ_FOREACH_SAFE,
381                  * which does not exist on all platforms.
382                  */
383                 for (reconn = TAILQ_FIRST(&reconn_list.head);
384                      reconn != NULL; reconn = next) {
385                         next = TAILQ_NEXT(reconn, next);
386
387                         ret = vhost_user_connect_nonblock(reconn->fd,
388                                                 (struct sockaddr *)&reconn->un,
389                                                 sizeof(reconn->un));
390                         if (ret == -2) {
391                                 close(reconn->fd);
392                                 RTE_LOG(ERR, VHOST_CONFIG,
393                                         "reconnection for fd %d failed\n",
394                                         reconn->fd);
395                                 goto remove_fd;
396                         }
397                         if (ret == -1)
398                                 continue;
399
400                         RTE_LOG(INFO, VHOST_CONFIG,
401                                 "%s: connected\n", reconn->vsocket->path);
402                         vhost_user_add_connection(reconn->fd, reconn->vsocket);
403 remove_fd:
404                         TAILQ_REMOVE(&reconn_list.head, reconn, next);
405                         free(reconn);
406                 }
407
408                 pthread_mutex_unlock(&reconn_list.mutex);
409                 sleep(1);
410         }
411
412         return NULL;
413 }
414
415 static int
416 vhost_user_reconnect_init(void)
417 {
418         int ret;
419
420         pthread_mutex_init(&reconn_list.mutex, NULL);
421         TAILQ_INIT(&reconn_list.head);
422
423         ret = pthread_create(&reconn_tid, NULL,
424                              vhost_user_client_reconnect, NULL);
425         if (ret < 0)
426                 RTE_LOG(ERR, VHOST_CONFIG, "failed to create reconnect thread");
427
428         return ret;
429 }
430
431 static int
432 vhost_user_create_client(struct vhost_user_socket *vsocket)
433 {
434         int fd;
435         int ret;
436         struct sockaddr_un un;
437         const char *path = vsocket->path;
438         struct vhost_user_reconnect *reconn;
439
440         fd = create_unix_socket(path, &un, vsocket->is_server);
441         if (fd < 0)
442                 return -1;
443
444         ret = vhost_user_connect_nonblock(fd, (struct sockaddr *)&un,
445                                           sizeof(un));
446         if (ret == 0) {
447                 vhost_user_add_connection(fd, vsocket);
448                 return 0;
449         }
450
451         RTE_LOG(ERR, VHOST_CONFIG,
452                 "failed to connect to %s: %s\n",
453                 path, strerror(errno));
454
455         if (ret == -2 || !vsocket->reconnect) {
456                 close(fd);
457                 return -1;
458         }
459
460         RTE_LOG(ERR, VHOST_CONFIG, "%s: reconnecting...\n", path);
461         reconn = malloc(sizeof(*reconn));
462         if (reconn == NULL) {
463                 RTE_LOG(ERR, VHOST_CONFIG,
464                         "failed to allocate memory for reconnect\n");
465                 close(fd);
466                 return -1;
467         }
468         reconn->un = un;
469         reconn->fd = fd;
470         reconn->vsocket = vsocket;
471         pthread_mutex_lock(&reconn_list.mutex);
472         TAILQ_INSERT_TAIL(&reconn_list.head, reconn, next);
473         pthread_mutex_unlock(&reconn_list.mutex);
474
475         return 0;
476 }
477
478 /*
479  * Register a new vhost-user socket; here we could act as server
480  * (the default case), or client (when RTE_VHOST_USER_CLIENT) flag
481  * is set.
482  */
483 int
484 rte_vhost_driver_register(const char *path, uint64_t flags)
485 {
486         int ret = -1;
487         struct vhost_user_socket *vsocket;
488
489         if (!path)
490                 return -1;
491
492         pthread_mutex_lock(&vhost_user.mutex);
493
494         if (vhost_user.vsocket_cnt == MAX_VHOST_SOCKET) {
495                 RTE_LOG(ERR, VHOST_CONFIG,
496                         "error: the number of vhost sockets reaches maximum\n");
497                 goto out;
498         }
499
500         vsocket = malloc(sizeof(struct vhost_user_socket));
501         if (!vsocket)
502                 goto out;
503         memset(vsocket, 0, sizeof(struct vhost_user_socket));
504         vsocket->path = strdup(path);
505         vsocket->connfd = -1;
506         vsocket->dequeue_zero_copy = flags & RTE_VHOST_USER_DEQUEUE_ZERO_COPY;
507
508         if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
509                 vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
510                 if (vsocket->reconnect && reconn_tid == 0) {
511                         if (vhost_user_reconnect_init() < 0) {
512                                 free(vsocket->path);
513                                 free(vsocket);
514                                 goto out;
515                         }
516                 }
517                 ret = vhost_user_create_client(vsocket);
518         } else {
519                 vsocket->is_server = true;
520                 ret = vhost_user_create_server(vsocket);
521         }
522         if (ret < 0) {
523                 free(vsocket->path);
524                 free(vsocket);
525                 goto out;
526         }
527
528         vhost_user.vsockets[vhost_user.vsocket_cnt++] = vsocket;
529
530 out:
531         pthread_mutex_unlock(&vhost_user.mutex);
532
533         return ret;
534 }
535
536 static bool
537 vhost_user_remove_reconnect(struct vhost_user_socket *vsocket)
538 {
539         int found = false;
540         struct vhost_user_reconnect *reconn, *next;
541
542         pthread_mutex_lock(&reconn_list.mutex);
543
544         for (reconn = TAILQ_FIRST(&reconn_list.head);
545              reconn != NULL; reconn = next) {
546                 next = TAILQ_NEXT(reconn, next);
547
548                 if (reconn->vsocket == vsocket) {
549                         TAILQ_REMOVE(&reconn_list.head, reconn, next);
550                         close(reconn->fd);
551                         free(reconn);
552                         found = true;
553                         break;
554                 }
555         }
556         pthread_mutex_unlock(&reconn_list.mutex);
557         return found;
558 }
559
560 /**
561  * Unregister the specified vhost socket
562  */
563 int
564 rte_vhost_driver_unregister(const char *path)
565 {
566         int i;
567         int count;
568         struct vhost_user_connection *conn;
569
570         pthread_mutex_lock(&vhost_user.mutex);
571
572         for (i = 0; i < vhost_user.vsocket_cnt; i++) {
573                 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
574
575                 if (!strcmp(vsocket->path, path)) {
576                         if (vsocket->is_server) {
577                                 fdset_del(&vhost_user.fdset, vsocket->listenfd);
578                                 close(vsocket->listenfd);
579                                 unlink(path);
580                         } else if (vsocket->reconnect) {
581                                 vhost_user_remove_reconnect(vsocket);
582                         }
583
584                         conn = fdset_del(&vhost_user.fdset, vsocket->connfd);
585                         if (conn) {
586                                 RTE_LOG(INFO, VHOST_CONFIG,
587                                         "free connfd = %d for device '%s'\n",
588                                         vsocket->connfd, path);
589                                 close(vsocket->connfd);
590                                 vhost_destroy_device(conn->vid);
591                                 free(conn);
592                         }
593
594                         free(vsocket->path);
595                         free(vsocket);
596
597                         count = --vhost_user.vsocket_cnt;
598                         vhost_user.vsockets[i] = vhost_user.vsockets[count];
599                         vhost_user.vsockets[count] = NULL;
600                         pthread_mutex_unlock(&vhost_user.mutex);
601
602                         return 0;
603                 }
604         }
605         pthread_mutex_unlock(&vhost_user.mutex);
606
607         return -1;
608 }
609
610 int
611 rte_vhost_driver_session_start(void)
612 {
613         fdset_event_dispatch(&vhost_user.fdset);
614         return 0;
615 }