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