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