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