a95fbfbb933c78d1c3e37621044e5627c0b04901
[deb_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                 return;
232         }
233
234         pthread_mutex_lock(&vsocket->conn_mutex);
235         TAILQ_INSERT_TAIL(&vsocket->conn_list, conn, next);
236         pthread_mutex_unlock(&vsocket->conn_mutex);
237 }
238
239 /* call back when there is new vhost-user connection from client  */
240 static void
241 vhost_user_server_new_connection(int fd, void *dat, int *remove __rte_unused)
242 {
243         struct vhost_user_socket *vsocket = dat;
244
245         fd = accept(fd, NULL, NULL);
246         if (fd < 0)
247                 return;
248
249         RTE_LOG(INFO, VHOST_CONFIG, "new vhost user connection is %d\n", fd);
250         vhost_user_add_connection(fd, vsocket);
251 }
252
253 static void
254 vhost_user_read_cb(int connfd, void *dat, int *remove)
255 {
256         struct vhost_user_connection *conn = dat;
257         struct vhost_user_socket *vsocket = conn->vsocket;
258         int ret;
259
260         ret = vhost_user_msg_handler(conn->vid, connfd);
261         if (ret < 0) {
262                 close(connfd);
263                 *remove = 1;
264                 vhost_destroy_device(conn->vid);
265
266                 pthread_mutex_lock(&vsocket->conn_mutex);
267                 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
268                 pthread_mutex_unlock(&vsocket->conn_mutex);
269
270                 free(conn);
271
272                 if (vsocket->reconnect)
273                         vhost_user_create_client(vsocket);
274         }
275 }
276
277 static int
278 create_unix_socket(const char *path, struct sockaddr_un *un, bool is_server)
279 {
280         int fd;
281
282         fd = socket(AF_UNIX, SOCK_STREAM, 0);
283         if (fd < 0)
284                 return -1;
285         RTE_LOG(INFO, VHOST_CONFIG, "vhost-user %s: socket created, fd: %d\n",
286                 is_server ? "server" : "client", fd);
287
288         if (!is_server && fcntl(fd, F_SETFL, O_NONBLOCK)) {
289                 RTE_LOG(ERR, VHOST_CONFIG,
290                         "vhost-user: can't set nonblocking mode for socket, fd: "
291                         "%d (%s)\n", fd, strerror(errno));
292                 close(fd);
293                 return -1;
294         }
295
296         memset(un, 0, sizeof(*un));
297         un->sun_family = AF_UNIX;
298         strncpy(un->sun_path, path, sizeof(un->sun_path));
299         un->sun_path[sizeof(un->sun_path) - 1] = '\0';
300
301         return fd;
302 }
303
304 static int
305 vhost_user_create_server(struct vhost_user_socket *vsocket)
306 {
307         int fd;
308         int ret;
309         struct sockaddr_un un;
310         const char *path = vsocket->path;
311
312         fd = create_unix_socket(path, &un, vsocket->is_server);
313         if (fd < 0)
314                 return -1;
315
316         ret = bind(fd, (struct sockaddr *)&un, sizeof(un));
317         if (ret < 0) {
318                 RTE_LOG(ERR, VHOST_CONFIG,
319                         "failed to bind to %s: %s; remove it and try again\n",
320                         path, strerror(errno));
321                 goto err;
322         }
323         RTE_LOG(INFO, VHOST_CONFIG, "bind to %s\n", path);
324
325         ret = listen(fd, MAX_VIRTIO_BACKLOG);
326         if (ret < 0)
327                 goto err;
328
329         vsocket->listenfd = fd;
330         ret = fdset_add(&vhost_user.fdset, fd, vhost_user_server_new_connection,
331                   NULL, vsocket);
332         if (ret < 0) {
333                 RTE_LOG(ERR, VHOST_CONFIG,
334                         "failed to add listen fd %d to vhost server fdset\n",
335                         fd);
336                 goto err;
337         }
338
339         return 0;
340
341 err:
342         close(fd);
343         return -1;
344 }
345
346 struct vhost_user_reconnect {
347         struct sockaddr_un un;
348         int fd;
349         struct vhost_user_socket *vsocket;
350
351         TAILQ_ENTRY(vhost_user_reconnect) next;
352 };
353
354 TAILQ_HEAD(vhost_user_reconnect_tailq_list, vhost_user_reconnect);
355 struct vhost_user_reconnect_list {
356         struct vhost_user_reconnect_tailq_list head;
357         pthread_mutex_t mutex;
358 };
359
360 static struct vhost_user_reconnect_list reconn_list;
361 static pthread_t reconn_tid;
362
363 static int
364 vhost_user_connect_nonblock(int fd, struct sockaddr *un, size_t sz)
365 {
366         int ret, flags;
367
368         ret = connect(fd, un, sz);
369         if (ret < 0 && errno != EISCONN)
370                 return -1;
371
372         flags = fcntl(fd, F_GETFL, 0);
373         if (flags < 0) {
374                 RTE_LOG(ERR, VHOST_CONFIG,
375                         "can't get flags for connfd %d\n", fd);
376                 return -2;
377         }
378         if ((flags & O_NONBLOCK) && fcntl(fd, F_SETFL, flags & ~O_NONBLOCK)) {
379                 RTE_LOG(ERR, VHOST_CONFIG,
380                                 "can't disable nonblocking on fd %d\n", fd);
381                 return -2;
382         }
383         return 0;
384 }
385
386 static void *
387 vhost_user_client_reconnect(void *arg __rte_unused)
388 {
389         int ret;
390         struct vhost_user_reconnect *reconn, *next;
391
392         while (1) {
393                 pthread_mutex_lock(&reconn_list.mutex);
394
395                 /*
396                  * An equal implementation of TAILQ_FOREACH_SAFE,
397                  * which does not exist on all platforms.
398                  */
399                 for (reconn = TAILQ_FIRST(&reconn_list.head);
400                      reconn != NULL; reconn = next) {
401                         next = TAILQ_NEXT(reconn, next);
402
403                         ret = vhost_user_connect_nonblock(reconn->fd,
404                                                 (struct sockaddr *)&reconn->un,
405                                                 sizeof(reconn->un));
406                         if (ret == -2) {
407                                 close(reconn->fd);
408                                 RTE_LOG(ERR, VHOST_CONFIG,
409                                         "reconnection for fd %d failed\n",
410                                         reconn->fd);
411                                 goto remove_fd;
412                         }
413                         if (ret == -1)
414                                 continue;
415
416                         RTE_LOG(INFO, VHOST_CONFIG,
417                                 "%s: connected\n", reconn->vsocket->path);
418                         vhost_user_add_connection(reconn->fd, reconn->vsocket);
419 remove_fd:
420                         TAILQ_REMOVE(&reconn_list.head, reconn, next);
421                         free(reconn);
422                 }
423
424                 pthread_mutex_unlock(&reconn_list.mutex);
425                 sleep(1);
426         }
427
428         return NULL;
429 }
430
431 static int
432 vhost_user_reconnect_init(void)
433 {
434         int ret;
435
436         pthread_mutex_init(&reconn_list.mutex, NULL);
437         TAILQ_INIT(&reconn_list.head);
438
439         ret = pthread_create(&reconn_tid, NULL,
440                              vhost_user_client_reconnect, NULL);
441         if (ret != 0)
442                 RTE_LOG(ERR, VHOST_CONFIG, "failed to create reconnect thread");
443
444         return ret;
445 }
446
447 static int
448 vhost_user_create_client(struct vhost_user_socket *vsocket)
449 {
450         int fd;
451         int ret;
452         struct sockaddr_un un;
453         const char *path = vsocket->path;
454         struct vhost_user_reconnect *reconn;
455
456         fd = create_unix_socket(path, &un, vsocket->is_server);
457         if (fd < 0)
458                 return -1;
459
460         ret = vhost_user_connect_nonblock(fd, (struct sockaddr *)&un,
461                                           sizeof(un));
462         if (ret == 0) {
463                 vhost_user_add_connection(fd, vsocket);
464                 return 0;
465         }
466
467         RTE_LOG(WARNING, VHOST_CONFIG,
468                 "failed to connect to %s: %s\n",
469                 path, strerror(errno));
470
471         if (ret == -2 || !vsocket->reconnect) {
472                 close(fd);
473                 return -1;
474         }
475
476         RTE_LOG(INFO, VHOST_CONFIG, "%s: reconnecting...\n", path);
477         reconn = malloc(sizeof(*reconn));
478         if (reconn == NULL) {
479                 RTE_LOG(ERR, VHOST_CONFIG,
480                         "failed to allocate memory for reconnect\n");
481                 close(fd);
482                 return -1;
483         }
484         reconn->un = un;
485         reconn->fd = fd;
486         reconn->vsocket = vsocket;
487         pthread_mutex_lock(&reconn_list.mutex);
488         TAILQ_INSERT_TAIL(&reconn_list.head, reconn, next);
489         pthread_mutex_unlock(&reconn_list.mutex);
490
491         return 0;
492 }
493
494 /*
495  * Register a new vhost-user socket; here we could act as server
496  * (the default case), or client (when RTE_VHOST_USER_CLIENT) flag
497  * is set.
498  */
499 int
500 rte_vhost_driver_register(const char *path, uint64_t flags)
501 {
502         int ret = -1;
503         struct vhost_user_socket *vsocket;
504
505         if (!path)
506                 return -1;
507
508         pthread_mutex_lock(&vhost_user.mutex);
509
510         if (vhost_user.vsocket_cnt == MAX_VHOST_SOCKET) {
511                 RTE_LOG(ERR, VHOST_CONFIG,
512                         "error: the number of vhost sockets reaches maximum\n");
513                 goto out;
514         }
515
516         vsocket = malloc(sizeof(struct vhost_user_socket));
517         if (!vsocket)
518                 goto out;
519         memset(vsocket, 0, sizeof(struct vhost_user_socket));
520         vsocket->path = strdup(path);
521         TAILQ_INIT(&vsocket->conn_list);
522         pthread_mutex_init(&vsocket->conn_mutex, NULL);
523         vsocket->dequeue_zero_copy = flags & RTE_VHOST_USER_DEQUEUE_ZERO_COPY;
524
525         if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
526                 vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
527                 if (vsocket->reconnect && reconn_tid == 0) {
528                         if (vhost_user_reconnect_init() != 0) {
529                                 free(vsocket->path);
530                                 free(vsocket);
531                                 goto out;
532                         }
533                 }
534                 ret = vhost_user_create_client(vsocket);
535         } else {
536                 vsocket->is_server = true;
537                 ret = vhost_user_create_server(vsocket);
538         }
539         if (ret < 0) {
540                 free(vsocket->path);
541                 free(vsocket);
542                 goto out;
543         }
544
545         vhost_user.vsockets[vhost_user.vsocket_cnt++] = vsocket;
546
547 out:
548         pthread_mutex_unlock(&vhost_user.mutex);
549
550         return ret;
551 }
552
553 static bool
554 vhost_user_remove_reconnect(struct vhost_user_socket *vsocket)
555 {
556         int found = false;
557         struct vhost_user_reconnect *reconn, *next;
558
559         pthread_mutex_lock(&reconn_list.mutex);
560
561         for (reconn = TAILQ_FIRST(&reconn_list.head);
562              reconn != NULL; reconn = next) {
563                 next = TAILQ_NEXT(reconn, next);
564
565                 if (reconn->vsocket == vsocket) {
566                         TAILQ_REMOVE(&reconn_list.head, reconn, next);
567                         close(reconn->fd);
568                         free(reconn);
569                         found = true;
570                         break;
571                 }
572         }
573         pthread_mutex_unlock(&reconn_list.mutex);
574         return found;
575 }
576
577 /**
578  * Unregister the specified vhost socket
579  */
580 int
581 rte_vhost_driver_unregister(const char *path)
582 {
583         int i;
584         int count;
585         struct vhost_user_connection *conn, *next;
586
587         pthread_mutex_lock(&vhost_user.mutex);
588
589         for (i = 0; i < vhost_user.vsocket_cnt; i++) {
590                 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
591
592                 if (!strcmp(vsocket->path, path)) {
593                         if (vsocket->is_server) {
594                                 fdset_del(&vhost_user.fdset, vsocket->listenfd);
595                                 close(vsocket->listenfd);
596                                 unlink(path);
597                         } else if (vsocket->reconnect) {
598                                 vhost_user_remove_reconnect(vsocket);
599                         }
600
601                         pthread_mutex_lock(&vsocket->conn_mutex);
602                         for (conn = TAILQ_FIRST(&vsocket->conn_list);
603                              conn != NULL;
604                              conn = next) {
605                                 next = TAILQ_NEXT(conn, next);
606
607                                 fdset_del(&vhost_user.fdset, conn->connfd);
608                                 RTE_LOG(INFO, VHOST_CONFIG,
609                                         "free connfd = %d for device '%s'\n",
610                                         conn->connfd, path);
611                                 close(conn->connfd);
612                                 vhost_destroy_device(conn->vid);
613                                 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
614                                 free(conn);
615                         }
616                         pthread_mutex_unlock(&vsocket->conn_mutex);
617
618                         free(vsocket->path);
619                         free(vsocket);
620
621                         count = --vhost_user.vsocket_cnt;
622                         vhost_user.vsockets[i] = vhost_user.vsockets[count];
623                         vhost_user.vsockets[count] = NULL;
624                         pthread_mutex_unlock(&vhost_user.mutex);
625
626                         return 0;
627                 }
628         }
629         pthread_mutex_unlock(&vhost_user.mutex);
630
631         return -1;
632 }
633
634 int
635 rte_vhost_driver_session_start(void)
636 {
637         fdset_event_dispatch(&vhost_user.fdset);
638         return 0;
639 }