vhost: add fdset-event thread name
[dpdk.git] / lib / librte_vhost / socket.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2016 Intel Corporation
3  */
4
5 #include <stdint.h>
6 #include <stdio.h>
7 #include <stdbool.h>
8 #include <limits.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <string.h>
12 #include <sys/types.h>
13 #include <sys/socket.h>
14 #include <sys/un.h>
15 #include <sys/queue.h>
16 #include <errno.h>
17 #include <fcntl.h>
18 #include <pthread.h>
19
20 #include <rte_log.h>
21
22 #include "fd_man.h"
23 #include "vhost.h"
24 #include "vhost_user.h"
25
26
27 TAILQ_HEAD(vhost_user_connection_list, vhost_user_connection);
28
29 /*
30  * Every time rte_vhost_driver_register() is invoked, an associated
31  * vhost_user_socket struct will be created.
32  */
33 struct vhost_user_socket {
34         struct vhost_user_connection_list conn_list;
35         pthread_mutex_t conn_mutex;
36         char *path;
37         int socket_fd;
38         struct sockaddr_un un;
39         bool is_server;
40         bool reconnect;
41         bool dequeue_zero_copy;
42         bool iommu_support;
43         bool use_builtin_virtio_net;
44
45         /*
46          * The "supported_features" indicates the feature bits the
47          * vhost driver supports. The "features" indicates the feature
48          * bits after the rte_vhost_driver_features_disable/enable().
49          * It is also the final feature bits used for vhost-user
50          * features negotiation.
51          */
52         uint64_t supported_features;
53         uint64_t features;
54
55         struct vhost_device_ops const *notify_ops;
56 };
57
58 struct vhost_user_connection {
59         struct vhost_user_socket *vsocket;
60         int connfd;
61         int vid;
62
63         TAILQ_ENTRY(vhost_user_connection) next;
64 };
65
66 #define MAX_VHOST_SOCKET 1024
67 struct vhost_user {
68         struct vhost_user_socket *vsockets[MAX_VHOST_SOCKET];
69         struct fdset fdset;
70         int vsocket_cnt;
71         pthread_mutex_t mutex;
72 };
73
74 #define MAX_VIRTIO_BACKLOG 128
75
76 static void vhost_user_server_new_connection(int fd, void *data, int *remove);
77 static void vhost_user_read_cb(int fd, void *dat, int *remove);
78 static int create_unix_socket(struct vhost_user_socket *vsocket);
79 static int vhost_user_start_client(struct vhost_user_socket *vsocket);
80
81 static struct vhost_user vhost_user = {
82         .fdset = {
83                 .fd = { [0 ... MAX_FDS - 1] = {-1, NULL, NULL, NULL, 0} },
84                 .fd_mutex = PTHREAD_MUTEX_INITIALIZER,
85                 .num = 0
86         },
87         .vsocket_cnt = 0,
88         .mutex = PTHREAD_MUTEX_INITIALIZER,
89 };
90
91 /* return bytes# of read on success or negative val on failure. */
92 int
93 read_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
94 {
95         struct iovec iov;
96         struct msghdr msgh;
97         size_t fdsize = fd_num * sizeof(int);
98         char control[CMSG_SPACE(fdsize)];
99         struct cmsghdr *cmsg;
100         int got_fds = 0;
101         int ret;
102
103         memset(&msgh, 0, sizeof(msgh));
104         iov.iov_base = buf;
105         iov.iov_len  = buflen;
106
107         msgh.msg_iov = &iov;
108         msgh.msg_iovlen = 1;
109         msgh.msg_control = control;
110         msgh.msg_controllen = sizeof(control);
111
112         ret = recvmsg(sockfd, &msgh, 0);
113         if (ret <= 0) {
114                 RTE_LOG(ERR, VHOST_CONFIG, "recvmsg failed\n");
115                 return ret;
116         }
117
118         if (msgh.msg_flags & (MSG_TRUNC | MSG_CTRUNC)) {
119                 RTE_LOG(ERR, VHOST_CONFIG, "truncted msg\n");
120                 return -1;
121         }
122
123         for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
124                 cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
125                 if ((cmsg->cmsg_level == SOL_SOCKET) &&
126                         (cmsg->cmsg_type == SCM_RIGHTS)) {
127                         got_fds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
128                         memcpy(fds, CMSG_DATA(cmsg), got_fds * sizeof(int));
129                         break;
130                 }
131         }
132
133         /* Clear out unused file descriptors */
134         while (got_fds < fd_num)
135                 fds[got_fds++] = -1;
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                 if (cmsg == NULL) {
163                         RTE_LOG(ERR, VHOST_CONFIG, "cmsg == NULL\n");
164                         errno = EINVAL;
165                         return -1;
166                 }
167                 cmsg->cmsg_len = CMSG_LEN(fdsize);
168                 cmsg->cmsg_level = SOL_SOCKET;
169                 cmsg->cmsg_type = SCM_RIGHTS;
170                 memcpy(CMSG_DATA(cmsg), fds, fdsize);
171         } else {
172                 msgh.msg_control = NULL;
173                 msgh.msg_controllen = 0;
174         }
175
176         do {
177                 ret = sendmsg(sockfd, &msgh, MSG_NOSIGNAL);
178         } while (ret < 0 && errno == EINTR);
179
180         if (ret < 0) {
181                 RTE_LOG(ERR, VHOST_CONFIG,  "sendmsg error\n");
182                 return ret;
183         }
184
185         return ret;
186 }
187
188 static void
189 vhost_user_add_connection(int fd, struct vhost_user_socket *vsocket)
190 {
191         int vid;
192         size_t size;
193         struct vhost_user_connection *conn;
194         int ret;
195
196         conn = malloc(sizeof(*conn));
197         if (conn == NULL) {
198                 close(fd);
199                 return;
200         }
201
202         vid = vhost_new_device();
203         if (vid == -1) {
204                 goto err;
205         }
206
207         size = strnlen(vsocket->path, PATH_MAX);
208         vhost_set_ifname(vid, vsocket->path, size);
209
210         vhost_set_builtin_virtio_net(vid, vsocket->use_builtin_virtio_net);
211
212         if (vsocket->dequeue_zero_copy)
213                 vhost_enable_dequeue_zero_copy(vid);
214
215         RTE_LOG(INFO, VHOST_CONFIG, "new device, handle is %d\n", vid);
216
217         if (vsocket->notify_ops->new_connection) {
218                 ret = vsocket->notify_ops->new_connection(vid);
219                 if (ret < 0) {
220                         RTE_LOG(ERR, VHOST_CONFIG,
221                                 "failed to add vhost user connection with fd %d\n",
222                                 fd);
223                         goto err;
224                 }
225         }
226
227         conn->connfd = fd;
228         conn->vsocket = vsocket;
229         conn->vid = vid;
230         ret = fdset_add(&vhost_user.fdset, fd, vhost_user_read_cb,
231                         NULL, conn);
232         if (ret < 0) {
233                 RTE_LOG(ERR, VHOST_CONFIG,
234                         "failed to add fd %d into vhost server fdset\n",
235                         fd);
236
237                 if (vsocket->notify_ops->destroy_connection)
238                         vsocket->notify_ops->destroy_connection(conn->vid);
239
240                 goto err;
241         }
242
243         pthread_mutex_lock(&vsocket->conn_mutex);
244         TAILQ_INSERT_TAIL(&vsocket->conn_list, conn, next);
245         pthread_mutex_unlock(&vsocket->conn_mutex);
246         return;
247
248 err:
249         free(conn);
250         close(fd);
251 }
252
253 /* call back when there is new vhost-user connection from client  */
254 static void
255 vhost_user_server_new_connection(int fd, void *dat, int *remove __rte_unused)
256 {
257         struct vhost_user_socket *vsocket = dat;
258
259         fd = accept(fd, NULL, NULL);
260         if (fd < 0)
261                 return;
262
263         RTE_LOG(INFO, VHOST_CONFIG, "new vhost user connection is %d\n", fd);
264         vhost_user_add_connection(fd, vsocket);
265 }
266
267 static void
268 vhost_user_read_cb(int connfd, void *dat, int *remove)
269 {
270         struct vhost_user_connection *conn = dat;
271         struct vhost_user_socket *vsocket = conn->vsocket;
272         int ret;
273
274         ret = vhost_user_msg_handler(conn->vid, connfd);
275         if (ret < 0) {
276                 close(connfd);
277                 *remove = 1;
278                 vhost_destroy_device(conn->vid);
279
280                 if (vsocket->notify_ops->destroy_connection)
281                         vsocket->notify_ops->destroy_connection(conn->vid);
282
283                 pthread_mutex_lock(&vsocket->conn_mutex);
284                 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
285                 pthread_mutex_unlock(&vsocket->conn_mutex);
286
287                 free(conn);
288
289                 if (vsocket->reconnect) {
290                         create_unix_socket(vsocket);
291                         vhost_user_start_client(vsocket);
292                 }
293         }
294 }
295
296 static int
297 create_unix_socket(struct vhost_user_socket *vsocket)
298 {
299         int fd;
300         struct sockaddr_un *un = &vsocket->un;
301
302         fd = socket(AF_UNIX, SOCK_STREAM, 0);
303         if (fd < 0)
304                 return -1;
305         RTE_LOG(INFO, VHOST_CONFIG, "vhost-user %s: socket created, fd: %d\n",
306                 vsocket->is_server ? "server" : "client", fd);
307
308         if (!vsocket->is_server && fcntl(fd, F_SETFL, O_NONBLOCK)) {
309                 RTE_LOG(ERR, VHOST_CONFIG,
310                         "vhost-user: can't set nonblocking mode for socket, fd: "
311                         "%d (%s)\n", fd, strerror(errno));
312                 close(fd);
313                 return -1;
314         }
315
316         memset(un, 0, sizeof(*un));
317         un->sun_family = AF_UNIX;
318         strncpy(un->sun_path, vsocket->path, sizeof(un->sun_path));
319         un->sun_path[sizeof(un->sun_path) - 1] = '\0';
320
321         vsocket->socket_fd = fd;
322         return 0;
323 }
324
325 static int
326 vhost_user_start_server(struct vhost_user_socket *vsocket)
327 {
328         int ret;
329         int fd = vsocket->socket_fd;
330         const char *path = vsocket->path;
331
332         /*
333          * bind () may fail if the socket file with the same name already
334          * exists. But the library obviously should not delete the file
335          * provided by the user, since we can not be sure that it is not
336          * being used by other applications. Moreover, many applications form
337          * socket names based on user input, which is prone to errors.
338          *
339          * The user must ensure that the socket does not exist before
340          * registering the vhost driver in server mode.
341          */
342         ret = bind(fd, (struct sockaddr *)&vsocket->un, sizeof(vsocket->un));
343         if (ret < 0) {
344                 RTE_LOG(ERR, VHOST_CONFIG,
345                         "failed to bind to %s: %s; remove it and try again\n",
346                         path, strerror(errno));
347                 goto err;
348         }
349         RTE_LOG(INFO, VHOST_CONFIG, "bind to %s\n", path);
350
351         ret = listen(fd, MAX_VIRTIO_BACKLOG);
352         if (ret < 0)
353                 goto err;
354
355         ret = fdset_add(&vhost_user.fdset, fd, vhost_user_server_new_connection,
356                   NULL, vsocket);
357         if (ret < 0) {
358                 RTE_LOG(ERR, VHOST_CONFIG,
359                         "failed to add listen fd %d to vhost server fdset\n",
360                         fd);
361                 goto err;
362         }
363
364         return 0;
365
366 err:
367         close(fd);
368         return -1;
369 }
370
371 struct vhost_user_reconnect {
372         struct sockaddr_un un;
373         int fd;
374         struct vhost_user_socket *vsocket;
375
376         TAILQ_ENTRY(vhost_user_reconnect) next;
377 };
378
379 TAILQ_HEAD(vhost_user_reconnect_tailq_list, vhost_user_reconnect);
380 struct vhost_user_reconnect_list {
381         struct vhost_user_reconnect_tailq_list head;
382         pthread_mutex_t mutex;
383 };
384
385 static struct vhost_user_reconnect_list reconn_list;
386 static pthread_t reconn_tid;
387
388 static int
389 vhost_user_connect_nonblock(int fd, struct sockaddr *un, size_t sz)
390 {
391         int ret, flags;
392
393         ret = connect(fd, un, sz);
394         if (ret < 0 && errno != EISCONN)
395                 return -1;
396
397         flags = fcntl(fd, F_GETFL, 0);
398         if (flags < 0) {
399                 RTE_LOG(ERR, VHOST_CONFIG,
400                         "can't get flags for connfd %d\n", fd);
401                 return -2;
402         }
403         if ((flags & O_NONBLOCK) && fcntl(fd, F_SETFL, flags & ~O_NONBLOCK)) {
404                 RTE_LOG(ERR, VHOST_CONFIG,
405                                 "can't disable nonblocking on fd %d\n", fd);
406                 return -2;
407         }
408         return 0;
409 }
410
411 static void *
412 vhost_user_client_reconnect(void *arg __rte_unused)
413 {
414         int ret;
415         struct vhost_user_reconnect *reconn, *next;
416
417         while (1) {
418                 pthread_mutex_lock(&reconn_list.mutex);
419
420                 /*
421                  * An equal implementation of TAILQ_FOREACH_SAFE,
422                  * which does not exist on all platforms.
423                  */
424                 for (reconn = TAILQ_FIRST(&reconn_list.head);
425                      reconn != NULL; reconn = next) {
426                         next = TAILQ_NEXT(reconn, next);
427
428                         ret = vhost_user_connect_nonblock(reconn->fd,
429                                                 (struct sockaddr *)&reconn->un,
430                                                 sizeof(reconn->un));
431                         if (ret == -2) {
432                                 close(reconn->fd);
433                                 RTE_LOG(ERR, VHOST_CONFIG,
434                                         "reconnection for fd %d failed\n",
435                                         reconn->fd);
436                                 goto remove_fd;
437                         }
438                         if (ret == -1)
439                                 continue;
440
441                         RTE_LOG(INFO, VHOST_CONFIG,
442                                 "%s: connected\n", reconn->vsocket->path);
443                         vhost_user_add_connection(reconn->fd, reconn->vsocket);
444 remove_fd:
445                         TAILQ_REMOVE(&reconn_list.head, reconn, next);
446                         free(reconn);
447                 }
448
449                 pthread_mutex_unlock(&reconn_list.mutex);
450                 sleep(1);
451         }
452
453         return NULL;
454 }
455
456 static int
457 vhost_user_reconnect_init(void)
458 {
459         int ret;
460         char thread_name[RTE_MAX_THREAD_NAME_LEN];
461
462         ret = pthread_mutex_init(&reconn_list.mutex, NULL);
463         if (ret < 0) {
464                 RTE_LOG(ERR, VHOST_CONFIG, "failed to initialize mutex");
465                 return ret;
466         }
467         TAILQ_INIT(&reconn_list.head);
468
469         ret = pthread_create(&reconn_tid, NULL,
470                              vhost_user_client_reconnect, NULL);
471         if (ret != 0) {
472                 RTE_LOG(ERR, VHOST_CONFIG, "failed to create reconnect thread");
473                 if (pthread_mutex_destroy(&reconn_list.mutex)) {
474                         RTE_LOG(ERR, VHOST_CONFIG,
475                                 "failed to destroy reconnect mutex");
476                 }
477         } else {
478                 snprintf(thread_name, RTE_MAX_THREAD_NAME_LEN,
479                          "vhost-reconn");
480
481                 if (rte_thread_setname(reconn_tid, thread_name)) {
482                         RTE_LOG(DEBUG, VHOST_CONFIG,
483                                 "failed to set reconnect thread name");
484                 }
485         }
486
487         return ret;
488 }
489
490 static int
491 vhost_user_start_client(struct vhost_user_socket *vsocket)
492 {
493         int ret;
494         int fd = vsocket->socket_fd;
495         const char *path = vsocket->path;
496         struct vhost_user_reconnect *reconn;
497
498         ret = vhost_user_connect_nonblock(fd, (struct sockaddr *)&vsocket->un,
499                                           sizeof(vsocket->un));
500         if (ret == 0) {
501                 vhost_user_add_connection(fd, vsocket);
502                 return 0;
503         }
504
505         RTE_LOG(WARNING, VHOST_CONFIG,
506                 "failed to connect to %s: %s\n",
507                 path, strerror(errno));
508
509         if (ret == -2 || !vsocket->reconnect) {
510                 close(fd);
511                 return -1;
512         }
513
514         RTE_LOG(INFO, VHOST_CONFIG, "%s: reconnecting...\n", path);
515         reconn = malloc(sizeof(*reconn));
516         if (reconn == NULL) {
517                 RTE_LOG(ERR, VHOST_CONFIG,
518                         "failed to allocate memory for reconnect\n");
519                 close(fd);
520                 return -1;
521         }
522         reconn->un = vsocket->un;
523         reconn->fd = fd;
524         reconn->vsocket = vsocket;
525         pthread_mutex_lock(&reconn_list.mutex);
526         TAILQ_INSERT_TAIL(&reconn_list.head, reconn, next);
527         pthread_mutex_unlock(&reconn_list.mutex);
528
529         return 0;
530 }
531
532 static struct vhost_user_socket *
533 find_vhost_user_socket(const char *path)
534 {
535         int i;
536
537         for (i = 0; i < vhost_user.vsocket_cnt; i++) {
538                 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
539
540                 if (!strcmp(vsocket->path, path))
541                         return vsocket;
542         }
543
544         return NULL;
545 }
546
547 int
548 rte_vhost_driver_disable_features(const char *path, uint64_t features)
549 {
550         struct vhost_user_socket *vsocket;
551
552         pthread_mutex_lock(&vhost_user.mutex);
553         vsocket = find_vhost_user_socket(path);
554
555         /* Note that use_builtin_virtio_net is not affected by this function
556          * since callers may want to selectively disable features of the
557          * built-in vhost net device backend.
558          */
559
560         if (vsocket)
561                 vsocket->features &= ~features;
562         pthread_mutex_unlock(&vhost_user.mutex);
563
564         return vsocket ? 0 : -1;
565 }
566
567 int
568 rte_vhost_driver_enable_features(const char *path, uint64_t features)
569 {
570         struct vhost_user_socket *vsocket;
571
572         pthread_mutex_lock(&vhost_user.mutex);
573         vsocket = find_vhost_user_socket(path);
574         if (vsocket) {
575                 if ((vsocket->supported_features & features) != features) {
576                         /*
577                          * trying to enable features the driver doesn't
578                          * support.
579                          */
580                         pthread_mutex_unlock(&vhost_user.mutex);
581                         return -1;
582                 }
583                 vsocket->features |= features;
584         }
585         pthread_mutex_unlock(&vhost_user.mutex);
586
587         return vsocket ? 0 : -1;
588 }
589
590 int
591 rte_vhost_driver_set_features(const char *path, uint64_t features)
592 {
593         struct vhost_user_socket *vsocket;
594
595         pthread_mutex_lock(&vhost_user.mutex);
596         vsocket = find_vhost_user_socket(path);
597         if (vsocket) {
598                 vsocket->supported_features = features;
599                 vsocket->features = features;
600
601                 /* Anyone setting feature bits is implementing their own vhost
602                  * device backend.
603                  */
604                 vsocket->use_builtin_virtio_net = false;
605         }
606         pthread_mutex_unlock(&vhost_user.mutex);
607
608         return vsocket ? 0 : -1;
609 }
610
611 int
612 rte_vhost_driver_get_features(const char *path, uint64_t *features)
613 {
614         struct vhost_user_socket *vsocket;
615
616         pthread_mutex_lock(&vhost_user.mutex);
617         vsocket = find_vhost_user_socket(path);
618         if (vsocket)
619                 *features = vsocket->features;
620         pthread_mutex_unlock(&vhost_user.mutex);
621
622         if (!vsocket) {
623                 RTE_LOG(ERR, VHOST_CONFIG,
624                         "socket file %s is not registered yet.\n", path);
625                 return -1;
626         } else {
627                 return 0;
628         }
629 }
630
631 /*
632  * Register a new vhost-user socket; here we could act as server
633  * (the default case), or client (when RTE_VHOST_USER_CLIENT) flag
634  * is set.
635  */
636 int
637 rte_vhost_driver_register(const char *path, uint64_t flags)
638 {
639         int ret = -1;
640         struct vhost_user_socket *vsocket;
641
642         if (!path)
643                 return -1;
644
645         pthread_mutex_lock(&vhost_user.mutex);
646
647         if (vhost_user.vsocket_cnt == MAX_VHOST_SOCKET) {
648                 RTE_LOG(ERR, VHOST_CONFIG,
649                         "error: the number of vhost sockets reaches maximum\n");
650                 goto out;
651         }
652
653         vsocket = malloc(sizeof(struct vhost_user_socket));
654         if (!vsocket)
655                 goto out;
656         memset(vsocket, 0, sizeof(struct vhost_user_socket));
657         vsocket->path = strdup(path);
658         if (vsocket->path == NULL) {
659                 RTE_LOG(ERR, VHOST_CONFIG,
660                         "error: failed to copy socket path string\n");
661                 free(vsocket);
662                 goto out;
663         }
664         TAILQ_INIT(&vsocket->conn_list);
665         ret = pthread_mutex_init(&vsocket->conn_mutex, NULL);
666         if (ret) {
667                 RTE_LOG(ERR, VHOST_CONFIG,
668                         "error: failed to init connection mutex\n");
669                 goto out_free;
670         }
671         vsocket->dequeue_zero_copy = flags & RTE_VHOST_USER_DEQUEUE_ZERO_COPY;
672
673         /*
674          * Set the supported features correctly for the builtin vhost-user
675          * net driver.
676          *
677          * Applications know nothing about features the builtin virtio net
678          * driver (virtio_net.c) supports, thus it's not possible for them
679          * to invoke rte_vhost_driver_set_features(). To workaround it, here
680          * we set it unconditionally. If the application want to implement
681          * another vhost-user driver (say SCSI), it should call the
682          * rte_vhost_driver_set_features(), which will overwrite following
683          * two values.
684          */
685         vsocket->use_builtin_virtio_net = true;
686         vsocket->supported_features = VIRTIO_NET_SUPPORTED_FEATURES;
687         vsocket->features           = VIRTIO_NET_SUPPORTED_FEATURES;
688
689         if (!(flags & RTE_VHOST_USER_IOMMU_SUPPORT)) {
690                 vsocket->supported_features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
691                 vsocket->features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
692         }
693
694         if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
695                 vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
696                 if (vsocket->reconnect && reconn_tid == 0) {
697                         if (vhost_user_reconnect_init() != 0)
698                                 goto out_mutex;
699                 }
700         } else {
701                 vsocket->is_server = true;
702         }
703         ret = create_unix_socket(vsocket);
704         if (ret < 0) {
705                 goto out_mutex;
706         }
707
708         vhost_user.vsockets[vhost_user.vsocket_cnt++] = vsocket;
709
710         pthread_mutex_unlock(&vhost_user.mutex);
711         return ret;
712
713 out_mutex:
714         if (pthread_mutex_destroy(&vsocket->conn_mutex)) {
715                 RTE_LOG(ERR, VHOST_CONFIG,
716                         "error: failed to destroy connection mutex\n");
717         }
718 out_free:
719         free(vsocket->path);
720         free(vsocket);
721 out:
722         pthread_mutex_unlock(&vhost_user.mutex);
723
724         return ret;
725 }
726
727 static bool
728 vhost_user_remove_reconnect(struct vhost_user_socket *vsocket)
729 {
730         int found = false;
731         struct vhost_user_reconnect *reconn, *next;
732
733         pthread_mutex_lock(&reconn_list.mutex);
734
735         for (reconn = TAILQ_FIRST(&reconn_list.head);
736              reconn != NULL; reconn = next) {
737                 next = TAILQ_NEXT(reconn, next);
738
739                 if (reconn->vsocket == vsocket) {
740                         TAILQ_REMOVE(&reconn_list.head, reconn, next);
741                         close(reconn->fd);
742                         free(reconn);
743                         found = true;
744                         break;
745                 }
746         }
747         pthread_mutex_unlock(&reconn_list.mutex);
748         return found;
749 }
750
751 /**
752  * Unregister the specified vhost socket
753  */
754 int
755 rte_vhost_driver_unregister(const char *path)
756 {
757         int i;
758         int count;
759         struct vhost_user_connection *conn, *next;
760
761         pthread_mutex_lock(&vhost_user.mutex);
762
763         for (i = 0; i < vhost_user.vsocket_cnt; i++) {
764                 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
765
766                 if (!strcmp(vsocket->path, path)) {
767                         if (vsocket->is_server) {
768                                 fdset_del(&vhost_user.fdset, vsocket->socket_fd);
769                                 close(vsocket->socket_fd);
770                                 unlink(path);
771                         } else if (vsocket->reconnect) {
772                                 vhost_user_remove_reconnect(vsocket);
773                         }
774
775                         pthread_mutex_lock(&vsocket->conn_mutex);
776                         for (conn = TAILQ_FIRST(&vsocket->conn_list);
777                              conn != NULL;
778                              conn = next) {
779                                 next = TAILQ_NEXT(conn, next);
780
781                                 fdset_del(&vhost_user.fdset, conn->connfd);
782                                 RTE_LOG(INFO, VHOST_CONFIG,
783                                         "free connfd = %d for device '%s'\n",
784                                         conn->connfd, path);
785                                 close(conn->connfd);
786                                 vhost_destroy_device(conn->vid);
787                                 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
788                                 free(conn);
789                         }
790                         pthread_mutex_unlock(&vsocket->conn_mutex);
791
792                         pthread_mutex_destroy(&vsocket->conn_mutex);
793                         free(vsocket->path);
794                         free(vsocket);
795
796                         count = --vhost_user.vsocket_cnt;
797                         vhost_user.vsockets[i] = vhost_user.vsockets[count];
798                         vhost_user.vsockets[count] = NULL;
799                         pthread_mutex_unlock(&vhost_user.mutex);
800
801                         return 0;
802                 }
803         }
804         pthread_mutex_unlock(&vhost_user.mutex);
805
806         return -1;
807 }
808
809 /*
810  * Register ops so that we can add/remove device to data core.
811  */
812 int
813 rte_vhost_driver_callback_register(const char *path,
814         struct vhost_device_ops const * const ops)
815 {
816         struct vhost_user_socket *vsocket;
817
818         pthread_mutex_lock(&vhost_user.mutex);
819         vsocket = find_vhost_user_socket(path);
820         if (vsocket)
821                 vsocket->notify_ops = ops;
822         pthread_mutex_unlock(&vhost_user.mutex);
823
824         return vsocket ? 0 : -1;
825 }
826
827 struct vhost_device_ops const *
828 vhost_driver_callback_get(const char *path)
829 {
830         struct vhost_user_socket *vsocket;
831
832         pthread_mutex_lock(&vhost_user.mutex);
833         vsocket = find_vhost_user_socket(path);
834         pthread_mutex_unlock(&vhost_user.mutex);
835
836         return vsocket ? vsocket->notify_ops : NULL;
837 }
838
839 int
840 rte_vhost_driver_start(const char *path)
841 {
842         struct vhost_user_socket *vsocket;
843         static pthread_t fdset_tid;
844         char thread_name[RTE_MAX_THREAD_NAME_LEN];
845
846         pthread_mutex_lock(&vhost_user.mutex);
847         vsocket = find_vhost_user_socket(path);
848         pthread_mutex_unlock(&vhost_user.mutex);
849
850         if (!vsocket)
851                 return -1;
852
853         if (fdset_tid == 0) {
854                 int ret = pthread_create(&fdset_tid, NULL, fdset_event_dispatch,
855                                      &vhost_user.fdset);
856                 if (ret != 0) {
857                         RTE_LOG(ERR, VHOST_CONFIG,
858                                 "failed to create fdset handling thread");
859                         return -1;
860                 } else {
861                         snprintf(thread_name, RTE_MAX_THREAD_NAME_LEN,
862                                  "vhost-events");
863
864                         if (rte_thread_setname(fdset_tid, thread_name)) {
865                                 RTE_LOG(DEBUG, VHOST_CONFIG,
866                                         "failed to set vhost-event thread name");
867                         }
868                 }
869         }
870
871         if (vsocket->is_server)
872                 return vhost_user_start_server(vsocket);
873         else
874                 return vhost_user_start_client(vsocket);
875 }