vhost: check cmsg not null
[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, 0);
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         ret = bind(fd, (struct sockaddr *)&vsocket->un, sizeof(vsocket->un));
333         if (ret < 0) {
334                 RTE_LOG(ERR, VHOST_CONFIG,
335                         "failed to bind to %s: %s; remove it and try again\n",
336                         path, strerror(errno));
337                 goto err;
338         }
339         RTE_LOG(INFO, VHOST_CONFIG, "bind to %s\n", path);
340
341         ret = listen(fd, MAX_VIRTIO_BACKLOG);
342         if (ret < 0)
343                 goto err;
344
345         ret = fdset_add(&vhost_user.fdset, fd, vhost_user_server_new_connection,
346                   NULL, vsocket);
347         if (ret < 0) {
348                 RTE_LOG(ERR, VHOST_CONFIG,
349                         "failed to add listen fd %d to vhost server fdset\n",
350                         fd);
351                 goto err;
352         }
353
354         return 0;
355
356 err:
357         close(fd);
358         return -1;
359 }
360
361 struct vhost_user_reconnect {
362         struct sockaddr_un un;
363         int fd;
364         struct vhost_user_socket *vsocket;
365
366         TAILQ_ENTRY(vhost_user_reconnect) next;
367 };
368
369 TAILQ_HEAD(vhost_user_reconnect_tailq_list, vhost_user_reconnect);
370 struct vhost_user_reconnect_list {
371         struct vhost_user_reconnect_tailq_list head;
372         pthread_mutex_t mutex;
373 };
374
375 static struct vhost_user_reconnect_list reconn_list;
376 static pthread_t reconn_tid;
377
378 static int
379 vhost_user_connect_nonblock(int fd, struct sockaddr *un, size_t sz)
380 {
381         int ret, flags;
382
383         ret = connect(fd, un, sz);
384         if (ret < 0 && errno != EISCONN)
385                 return -1;
386
387         flags = fcntl(fd, F_GETFL, 0);
388         if (flags < 0) {
389                 RTE_LOG(ERR, VHOST_CONFIG,
390                         "can't get flags for connfd %d\n", fd);
391                 return -2;
392         }
393         if ((flags & O_NONBLOCK) && fcntl(fd, F_SETFL, flags & ~O_NONBLOCK)) {
394                 RTE_LOG(ERR, VHOST_CONFIG,
395                                 "can't disable nonblocking on fd %d\n", fd);
396                 return -2;
397         }
398         return 0;
399 }
400
401 static void *
402 vhost_user_client_reconnect(void *arg __rte_unused)
403 {
404         int ret;
405         struct vhost_user_reconnect *reconn, *next;
406
407         while (1) {
408                 pthread_mutex_lock(&reconn_list.mutex);
409
410                 /*
411                  * An equal implementation of TAILQ_FOREACH_SAFE,
412                  * which does not exist on all platforms.
413                  */
414                 for (reconn = TAILQ_FIRST(&reconn_list.head);
415                      reconn != NULL; reconn = next) {
416                         next = TAILQ_NEXT(reconn, next);
417
418                         ret = vhost_user_connect_nonblock(reconn->fd,
419                                                 (struct sockaddr *)&reconn->un,
420                                                 sizeof(reconn->un));
421                         if (ret == -2) {
422                                 close(reconn->fd);
423                                 RTE_LOG(ERR, VHOST_CONFIG,
424                                         "reconnection for fd %d failed\n",
425                                         reconn->fd);
426                                 goto remove_fd;
427                         }
428                         if (ret == -1)
429                                 continue;
430
431                         RTE_LOG(INFO, VHOST_CONFIG,
432                                 "%s: connected\n", reconn->vsocket->path);
433                         vhost_user_add_connection(reconn->fd, reconn->vsocket);
434 remove_fd:
435                         TAILQ_REMOVE(&reconn_list.head, reconn, next);
436                         free(reconn);
437                 }
438
439                 pthread_mutex_unlock(&reconn_list.mutex);
440                 sleep(1);
441         }
442
443         return NULL;
444 }
445
446 static int
447 vhost_user_reconnect_init(void)
448 {
449         int ret;
450         char thread_name[RTE_MAX_THREAD_NAME_LEN];
451
452         ret = pthread_mutex_init(&reconn_list.mutex, NULL);
453         if (ret < 0) {
454                 RTE_LOG(ERR, VHOST_CONFIG, "failed to initialize mutex");
455                 return ret;
456         }
457         TAILQ_INIT(&reconn_list.head);
458
459         ret = pthread_create(&reconn_tid, NULL,
460                              vhost_user_client_reconnect, NULL);
461         if (ret != 0) {
462                 RTE_LOG(ERR, VHOST_CONFIG, "failed to create reconnect thread");
463                 if (pthread_mutex_destroy(&reconn_list.mutex)) {
464                         RTE_LOG(ERR, VHOST_CONFIG,
465                                 "failed to destroy reconnect mutex");
466                 }
467         } else {
468                 snprintf(thread_name, RTE_MAX_THREAD_NAME_LEN,
469                          "vhost-reconn");
470
471                 if (rte_thread_setname(reconn_tid, thread_name)) {
472                         RTE_LOG(DEBUG, VHOST_CONFIG,
473                                 "failed to set reconnect thread name");
474                 }
475         }
476
477         return ret;
478 }
479
480 static int
481 vhost_user_start_client(struct vhost_user_socket *vsocket)
482 {
483         int ret;
484         int fd = vsocket->socket_fd;
485         const char *path = vsocket->path;
486         struct vhost_user_reconnect *reconn;
487
488         ret = vhost_user_connect_nonblock(fd, (struct sockaddr *)&vsocket->un,
489                                           sizeof(vsocket->un));
490         if (ret == 0) {
491                 vhost_user_add_connection(fd, vsocket);
492                 return 0;
493         }
494
495         RTE_LOG(WARNING, VHOST_CONFIG,
496                 "failed to connect to %s: %s\n",
497                 path, strerror(errno));
498
499         if (ret == -2 || !vsocket->reconnect) {
500                 close(fd);
501                 return -1;
502         }
503
504         RTE_LOG(INFO, VHOST_CONFIG, "%s: reconnecting...\n", path);
505         reconn = malloc(sizeof(*reconn));
506         if (reconn == NULL) {
507                 RTE_LOG(ERR, VHOST_CONFIG,
508                         "failed to allocate memory for reconnect\n");
509                 close(fd);
510                 return -1;
511         }
512         reconn->un = vsocket->un;
513         reconn->fd = fd;
514         reconn->vsocket = vsocket;
515         pthread_mutex_lock(&reconn_list.mutex);
516         TAILQ_INSERT_TAIL(&reconn_list.head, reconn, next);
517         pthread_mutex_unlock(&reconn_list.mutex);
518
519         return 0;
520 }
521
522 static struct vhost_user_socket *
523 find_vhost_user_socket(const char *path)
524 {
525         int i;
526
527         for (i = 0; i < vhost_user.vsocket_cnt; i++) {
528                 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
529
530                 if (!strcmp(vsocket->path, path))
531                         return vsocket;
532         }
533
534         return NULL;
535 }
536
537 int
538 rte_vhost_driver_disable_features(const char *path, uint64_t features)
539 {
540         struct vhost_user_socket *vsocket;
541
542         pthread_mutex_lock(&vhost_user.mutex);
543         vsocket = find_vhost_user_socket(path);
544
545         /* Note that use_builtin_virtio_net is not affected by this function
546          * since callers may want to selectively disable features of the
547          * built-in vhost net device backend.
548          */
549
550         if (vsocket)
551                 vsocket->features &= ~features;
552         pthread_mutex_unlock(&vhost_user.mutex);
553
554         return vsocket ? 0 : -1;
555 }
556
557 int
558 rte_vhost_driver_enable_features(const char *path, uint64_t features)
559 {
560         struct vhost_user_socket *vsocket;
561
562         pthread_mutex_lock(&vhost_user.mutex);
563         vsocket = find_vhost_user_socket(path);
564         if (vsocket) {
565                 if ((vsocket->supported_features & features) != features) {
566                         /*
567                          * trying to enable features the driver doesn't
568                          * support.
569                          */
570                         pthread_mutex_unlock(&vhost_user.mutex);
571                         return -1;
572                 }
573                 vsocket->features |= features;
574         }
575         pthread_mutex_unlock(&vhost_user.mutex);
576
577         return vsocket ? 0 : -1;
578 }
579
580 int
581 rte_vhost_driver_set_features(const char *path, uint64_t features)
582 {
583         struct vhost_user_socket *vsocket;
584
585         pthread_mutex_lock(&vhost_user.mutex);
586         vsocket = find_vhost_user_socket(path);
587         if (vsocket) {
588                 vsocket->supported_features = features;
589                 vsocket->features = features;
590
591                 /* Anyone setting feature bits is implementing their own vhost
592                  * device backend.
593                  */
594                 vsocket->use_builtin_virtio_net = false;
595         }
596         pthread_mutex_unlock(&vhost_user.mutex);
597
598         return vsocket ? 0 : -1;
599 }
600
601 int
602 rte_vhost_driver_get_features(const char *path, uint64_t *features)
603 {
604         struct vhost_user_socket *vsocket;
605
606         pthread_mutex_lock(&vhost_user.mutex);
607         vsocket = find_vhost_user_socket(path);
608         if (vsocket)
609                 *features = vsocket->features;
610         pthread_mutex_unlock(&vhost_user.mutex);
611
612         if (!vsocket) {
613                 RTE_LOG(ERR, VHOST_CONFIG,
614                         "socket file %s is not registered yet.\n", path);
615                 return -1;
616         } else {
617                 return 0;
618         }
619 }
620
621 /*
622  * Register a new vhost-user socket; here we could act as server
623  * (the default case), or client (when RTE_VHOST_USER_CLIENT) flag
624  * is set.
625  */
626 int
627 rte_vhost_driver_register(const char *path, uint64_t flags)
628 {
629         int ret = -1;
630         struct vhost_user_socket *vsocket;
631
632         if (!path)
633                 return -1;
634
635         pthread_mutex_lock(&vhost_user.mutex);
636
637         if (vhost_user.vsocket_cnt == MAX_VHOST_SOCKET) {
638                 RTE_LOG(ERR, VHOST_CONFIG,
639                         "error: the number of vhost sockets reaches maximum\n");
640                 goto out;
641         }
642
643         vsocket = malloc(sizeof(struct vhost_user_socket));
644         if (!vsocket)
645                 goto out;
646         memset(vsocket, 0, sizeof(struct vhost_user_socket));
647         vsocket->path = strdup(path);
648         if (vsocket->path == NULL) {
649                 RTE_LOG(ERR, VHOST_CONFIG,
650                         "error: failed to copy socket path string\n");
651                 free(vsocket);
652                 goto out;
653         }
654         TAILQ_INIT(&vsocket->conn_list);
655         ret = pthread_mutex_init(&vsocket->conn_mutex, NULL);
656         if (ret) {
657                 RTE_LOG(ERR, VHOST_CONFIG,
658                         "error: failed to init connection mutex\n");
659                 goto out_free;
660         }
661         vsocket->dequeue_zero_copy = flags & RTE_VHOST_USER_DEQUEUE_ZERO_COPY;
662
663         /*
664          * Set the supported features correctly for the builtin vhost-user
665          * net driver.
666          *
667          * Applications know nothing about features the builtin virtio net
668          * driver (virtio_net.c) supports, thus it's not possible for them
669          * to invoke rte_vhost_driver_set_features(). To workaround it, here
670          * we set it unconditionally. If the application want to implement
671          * another vhost-user driver (say SCSI), it should call the
672          * rte_vhost_driver_set_features(), which will overwrite following
673          * two values.
674          */
675         vsocket->use_builtin_virtio_net = true;
676         vsocket->supported_features = VIRTIO_NET_SUPPORTED_FEATURES;
677         vsocket->features           = VIRTIO_NET_SUPPORTED_FEATURES;
678
679         if (!(flags & RTE_VHOST_USER_IOMMU_SUPPORT)) {
680                 vsocket->supported_features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
681                 vsocket->features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
682         }
683
684         if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
685                 vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
686                 if (vsocket->reconnect && reconn_tid == 0) {
687                         if (vhost_user_reconnect_init() != 0)
688                                 goto out_mutex;
689                 }
690         } else {
691                 vsocket->is_server = true;
692         }
693         ret = create_unix_socket(vsocket);
694         if (ret < 0) {
695                 goto out_mutex;
696         }
697
698         vhost_user.vsockets[vhost_user.vsocket_cnt++] = vsocket;
699
700         pthread_mutex_unlock(&vhost_user.mutex);
701         return ret;
702
703 out_mutex:
704         if (pthread_mutex_destroy(&vsocket->conn_mutex)) {
705                 RTE_LOG(ERR, VHOST_CONFIG,
706                         "error: failed to destroy connection mutex\n");
707         }
708 out_free:
709         free(vsocket->path);
710         free(vsocket);
711 out:
712         pthread_mutex_unlock(&vhost_user.mutex);
713
714         return ret;
715 }
716
717 static bool
718 vhost_user_remove_reconnect(struct vhost_user_socket *vsocket)
719 {
720         int found = false;
721         struct vhost_user_reconnect *reconn, *next;
722
723         pthread_mutex_lock(&reconn_list.mutex);
724
725         for (reconn = TAILQ_FIRST(&reconn_list.head);
726              reconn != NULL; reconn = next) {
727                 next = TAILQ_NEXT(reconn, next);
728
729                 if (reconn->vsocket == vsocket) {
730                         TAILQ_REMOVE(&reconn_list.head, reconn, next);
731                         close(reconn->fd);
732                         free(reconn);
733                         found = true;
734                         break;
735                 }
736         }
737         pthread_mutex_unlock(&reconn_list.mutex);
738         return found;
739 }
740
741 /**
742  * Unregister the specified vhost socket
743  */
744 int
745 rte_vhost_driver_unregister(const char *path)
746 {
747         int i;
748         int count;
749         struct vhost_user_connection *conn, *next;
750
751         pthread_mutex_lock(&vhost_user.mutex);
752
753         for (i = 0; i < vhost_user.vsocket_cnt; i++) {
754                 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
755
756                 if (!strcmp(vsocket->path, path)) {
757                         if (vsocket->is_server) {
758                                 fdset_del(&vhost_user.fdset, vsocket->socket_fd);
759                                 close(vsocket->socket_fd);
760                                 unlink(path);
761                         } else if (vsocket->reconnect) {
762                                 vhost_user_remove_reconnect(vsocket);
763                         }
764
765                         pthread_mutex_lock(&vsocket->conn_mutex);
766                         for (conn = TAILQ_FIRST(&vsocket->conn_list);
767                              conn != NULL;
768                              conn = next) {
769                                 next = TAILQ_NEXT(conn, next);
770
771                                 fdset_del(&vhost_user.fdset, conn->connfd);
772                                 RTE_LOG(INFO, VHOST_CONFIG,
773                                         "free connfd = %d for device '%s'\n",
774                                         conn->connfd, path);
775                                 close(conn->connfd);
776                                 vhost_destroy_device(conn->vid);
777                                 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
778                                 free(conn);
779                         }
780                         pthread_mutex_unlock(&vsocket->conn_mutex);
781
782                         pthread_mutex_destroy(&vsocket->conn_mutex);
783                         free(vsocket->path);
784                         free(vsocket);
785
786                         count = --vhost_user.vsocket_cnt;
787                         vhost_user.vsockets[i] = vhost_user.vsockets[count];
788                         vhost_user.vsockets[count] = NULL;
789                         pthread_mutex_unlock(&vhost_user.mutex);
790
791                         return 0;
792                 }
793         }
794         pthread_mutex_unlock(&vhost_user.mutex);
795
796         return -1;
797 }
798
799 /*
800  * Register ops so that we can add/remove device to data core.
801  */
802 int
803 rte_vhost_driver_callback_register(const char *path,
804         struct vhost_device_ops const * const ops)
805 {
806         struct vhost_user_socket *vsocket;
807
808         pthread_mutex_lock(&vhost_user.mutex);
809         vsocket = find_vhost_user_socket(path);
810         if (vsocket)
811                 vsocket->notify_ops = ops;
812         pthread_mutex_unlock(&vhost_user.mutex);
813
814         return vsocket ? 0 : -1;
815 }
816
817 struct vhost_device_ops const *
818 vhost_driver_callback_get(const char *path)
819 {
820         struct vhost_user_socket *vsocket;
821
822         pthread_mutex_lock(&vhost_user.mutex);
823         vsocket = find_vhost_user_socket(path);
824         pthread_mutex_unlock(&vhost_user.mutex);
825
826         return vsocket ? vsocket->notify_ops : NULL;
827 }
828
829 int
830 rte_vhost_driver_start(const char *path)
831 {
832         struct vhost_user_socket *vsocket;
833         static pthread_t fdset_tid;
834
835         pthread_mutex_lock(&vhost_user.mutex);
836         vsocket = find_vhost_user_socket(path);
837         pthread_mutex_unlock(&vhost_user.mutex);
838
839         if (!vsocket)
840                 return -1;
841
842         if (fdset_tid == 0) {
843                 int ret = pthread_create(&fdset_tid, NULL, fdset_event_dispatch,
844                                      &vhost_user.fdset);
845                 if (ret != 0)
846                         RTE_LOG(ERR, VHOST_CONFIG,
847                                 "failed to create fdset handling thread");
848         }
849
850         if (vsocket->is_server)
851                 return vhost_user_start_server(vsocket);
852         else
853                 return vhost_user_start_client(vsocket);
854 }