416b1fdc3effae07297a9d887ed0c209b99e2ff5
[dpdk.git] / lib / librte_vhost / socket.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <stdint.h>
35 #include <stdio.h>
36 #include <stdbool.h>
37 #include <limits.h>
38 #include <stdlib.h>
39 #include <unistd.h>
40 #include <string.h>
41 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <sys/un.h>
44 #include <sys/queue.h>
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <pthread.h>
48
49 #include <rte_log.h>
50
51 #include "fd_man.h"
52 #include "vhost.h"
53 #include "vhost_user.h"
54
55
56 TAILQ_HEAD(vhost_user_connection_list, vhost_user_connection);
57
58 /*
59  * Every time rte_vhost_driver_register() is invoked, an associated
60  * vhost_user_socket struct will be created.
61  */
62 struct vhost_user_socket {
63         struct vhost_user_connection_list conn_list;
64         pthread_mutex_t conn_mutex;
65         char *path;
66         int listenfd;
67         bool is_server;
68         bool reconnect;
69         bool dequeue_zero_copy;
70
71         /*
72          * The "supported_features" indicates the feature bits the
73          * vhost driver supports. The "features" indicates the feature
74          * bits after the rte_vhost_driver_features_disable/enable().
75          * It is also the final feature bits used for vhost-user
76          * features negotiation.
77          */
78         uint64_t supported_features;
79         uint64_t features;
80 };
81
82 struct vhost_user_connection {
83         struct vhost_user_socket *vsocket;
84         int connfd;
85         int vid;
86
87         TAILQ_ENTRY(vhost_user_connection) next;
88 };
89
90 #define MAX_VHOST_SOCKET 1024
91 struct vhost_user {
92         struct vhost_user_socket *vsockets[MAX_VHOST_SOCKET];
93         struct fdset fdset;
94         int vsocket_cnt;
95         pthread_mutex_t mutex;
96 };
97
98 #define MAX_VIRTIO_BACKLOG 128
99
100 static void vhost_user_server_new_connection(int fd, void *data, int *remove);
101 static void vhost_user_read_cb(int fd, void *dat, int *remove);
102 static int vhost_user_create_client(struct vhost_user_socket *vsocket);
103
104 static struct vhost_user vhost_user = {
105         .fdset = {
106                 .fd = { [0 ... MAX_FDS - 1] = {-1, NULL, NULL, NULL, 0} },
107                 .fd_mutex = PTHREAD_MUTEX_INITIALIZER,
108                 .num = 0
109         },
110         .vsocket_cnt = 0,
111         .mutex = PTHREAD_MUTEX_INITIALIZER,
112 };
113
114 /* return bytes# of read on success or negative val on failure. */
115 int
116 read_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
117 {
118         struct iovec iov;
119         struct msghdr msgh;
120         size_t fdsize = fd_num * sizeof(int);
121         char control[CMSG_SPACE(fdsize)];
122         struct cmsghdr *cmsg;
123         int ret;
124
125         memset(&msgh, 0, sizeof(msgh));
126         iov.iov_base = buf;
127         iov.iov_len  = buflen;
128
129         msgh.msg_iov = &iov;
130         msgh.msg_iovlen = 1;
131         msgh.msg_control = control;
132         msgh.msg_controllen = sizeof(control);
133
134         ret = recvmsg(sockfd, &msgh, 0);
135         if (ret <= 0) {
136                 RTE_LOG(ERR, VHOST_CONFIG, "recvmsg failed\n");
137                 return ret;
138         }
139
140         if (msgh.msg_flags & (MSG_TRUNC | MSG_CTRUNC)) {
141                 RTE_LOG(ERR, VHOST_CONFIG, "truncted msg\n");
142                 return -1;
143         }
144
145         for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
146                 cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
147                 if ((cmsg->cmsg_level == SOL_SOCKET) &&
148                         (cmsg->cmsg_type == SCM_RIGHTS)) {
149                         memcpy(fds, CMSG_DATA(cmsg), fdsize);
150                         break;
151                 }
152         }
153
154         return ret;
155 }
156
157 int
158 send_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
159 {
160
161         struct iovec iov;
162         struct msghdr msgh;
163         size_t fdsize = fd_num * sizeof(int);
164         char control[CMSG_SPACE(fdsize)];
165         struct cmsghdr *cmsg;
166         int ret;
167
168         memset(&msgh, 0, sizeof(msgh));
169         iov.iov_base = buf;
170         iov.iov_len = buflen;
171
172         msgh.msg_iov = &iov;
173         msgh.msg_iovlen = 1;
174
175         if (fds && fd_num > 0) {
176                 msgh.msg_control = control;
177                 msgh.msg_controllen = sizeof(control);
178                 cmsg = CMSG_FIRSTHDR(&msgh);
179                 cmsg->cmsg_len = CMSG_LEN(fdsize);
180                 cmsg->cmsg_level = SOL_SOCKET;
181                 cmsg->cmsg_type = SCM_RIGHTS;
182                 memcpy(CMSG_DATA(cmsg), fds, fdsize);
183         } else {
184                 msgh.msg_control = NULL;
185                 msgh.msg_controllen = 0;
186         }
187
188         do {
189                 ret = sendmsg(sockfd, &msgh, 0);
190         } while (ret < 0 && errno == EINTR);
191
192         if (ret < 0) {
193                 RTE_LOG(ERR, VHOST_CONFIG,  "sendmsg error\n");
194                 return ret;
195         }
196
197         return ret;
198 }
199
200 static void
201 vhost_user_add_connection(int fd, struct vhost_user_socket *vsocket)
202 {
203         int vid;
204         size_t size;
205         struct vhost_user_connection *conn;
206         int ret;
207
208         conn = malloc(sizeof(*conn));
209         if (conn == NULL) {
210                 close(fd);
211                 return;
212         }
213
214         vid = vhost_new_device();
215         if (vid == -1) {
216                 close(fd);
217                 free(conn);
218                 return;
219         }
220
221         size = strnlen(vsocket->path, PATH_MAX);
222         vhost_set_ifname(vid, vsocket->path, size);
223
224         if (vsocket->dequeue_zero_copy)
225                 vhost_enable_dequeue_zero_copy(vid);
226
227         RTE_LOG(INFO, VHOST_CONFIG, "new device, handle is %d\n", vid);
228
229         conn->connfd = fd;
230         conn->vsocket = vsocket;
231         conn->vid = vid;
232         ret = fdset_add(&vhost_user.fdset, fd, vhost_user_read_cb,
233                         NULL, conn);
234         if (ret < 0) {
235                 conn->connfd = -1;
236                 free(conn);
237                 close(fd);
238                 RTE_LOG(ERR, VHOST_CONFIG,
239                         "failed to add fd %d into vhost server fdset\n",
240                         fd);
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 }
247
248 /* call back when there is new vhost-user connection from client  */
249 static void
250 vhost_user_server_new_connection(int fd, void *dat, int *remove __rte_unused)
251 {
252         struct vhost_user_socket *vsocket = dat;
253
254         fd = accept(fd, NULL, NULL);
255         if (fd < 0)
256                 return;
257
258         RTE_LOG(INFO, VHOST_CONFIG, "new vhost user connection is %d\n", fd);
259         vhost_user_add_connection(fd, vsocket);
260 }
261
262 static void
263 vhost_user_read_cb(int connfd, void *dat, int *remove)
264 {
265         struct vhost_user_connection *conn = dat;
266         struct vhost_user_socket *vsocket = conn->vsocket;
267         int ret;
268
269         ret = vhost_user_msg_handler(conn->vid, connfd);
270         if (ret < 0) {
271                 close(connfd);
272                 *remove = 1;
273                 vhost_destroy_device(conn->vid);
274
275                 pthread_mutex_lock(&vsocket->conn_mutex);
276                 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
277                 pthread_mutex_unlock(&vsocket->conn_mutex);
278
279                 free(conn);
280
281                 if (vsocket->reconnect)
282                         vhost_user_create_client(vsocket);
283         }
284 }
285
286 static int
287 create_unix_socket(const char *path, struct sockaddr_un *un, bool is_server)
288 {
289         int fd;
290
291         fd = socket(AF_UNIX, SOCK_STREAM, 0);
292         if (fd < 0)
293                 return -1;
294         RTE_LOG(INFO, VHOST_CONFIG, "vhost-user %s: socket created, fd: %d\n",
295                 is_server ? "server" : "client", fd);
296
297         if (!is_server && fcntl(fd, F_SETFL, O_NONBLOCK)) {
298                 RTE_LOG(ERR, VHOST_CONFIG,
299                         "vhost-user: can't set nonblocking mode for socket, fd: "
300                         "%d (%s)\n", fd, strerror(errno));
301                 close(fd);
302                 return -1;
303         }
304
305         memset(un, 0, sizeof(*un));
306         un->sun_family = AF_UNIX;
307         strncpy(un->sun_path, path, sizeof(un->sun_path));
308         un->sun_path[sizeof(un->sun_path) - 1] = '\0';
309
310         return fd;
311 }
312
313 static int
314 vhost_user_create_server(struct vhost_user_socket *vsocket)
315 {
316         int fd;
317         int ret;
318         struct sockaddr_un un;
319         const char *path = vsocket->path;
320
321         fd = create_unix_socket(path, &un, vsocket->is_server);
322         if (fd < 0)
323                 return -1;
324
325         ret = bind(fd, (struct sockaddr *)&un, sizeof(un));
326         if (ret < 0) {
327                 RTE_LOG(ERR, VHOST_CONFIG,
328                         "failed to bind to %s: %s; remove it and try again\n",
329                         path, strerror(errno));
330                 goto err;
331         }
332         RTE_LOG(INFO, VHOST_CONFIG, "bind to %s\n", path);
333
334         ret = listen(fd, MAX_VIRTIO_BACKLOG);
335         if (ret < 0)
336                 goto err;
337
338         vsocket->listenfd = fd;
339         ret = fdset_add(&vhost_user.fdset, fd, vhost_user_server_new_connection,
340                   NULL, vsocket);
341         if (ret < 0) {
342                 RTE_LOG(ERR, VHOST_CONFIG,
343                         "failed to add listen fd %d to vhost server fdset\n",
344                         fd);
345                 goto err;
346         }
347
348         return 0;
349
350 err:
351         close(fd);
352         return -1;
353 }
354
355 struct vhost_user_reconnect {
356         struct sockaddr_un un;
357         int fd;
358         struct vhost_user_socket *vsocket;
359
360         TAILQ_ENTRY(vhost_user_reconnect) next;
361 };
362
363 TAILQ_HEAD(vhost_user_reconnect_tailq_list, vhost_user_reconnect);
364 struct vhost_user_reconnect_list {
365         struct vhost_user_reconnect_tailq_list head;
366         pthread_mutex_t mutex;
367 };
368
369 static struct vhost_user_reconnect_list reconn_list;
370 static pthread_t reconn_tid;
371
372 static int
373 vhost_user_connect_nonblock(int fd, struct sockaddr *un, size_t sz)
374 {
375         int ret, flags;
376
377         ret = connect(fd, un, sz);
378         if (ret < 0 && errno != EISCONN)
379                 return -1;
380
381         flags = fcntl(fd, F_GETFL, 0);
382         if (flags < 0) {
383                 RTE_LOG(ERR, VHOST_CONFIG,
384                         "can't get flags for connfd %d\n", fd);
385                 return -2;
386         }
387         if ((flags & O_NONBLOCK) && fcntl(fd, F_SETFL, flags & ~O_NONBLOCK)) {
388                 RTE_LOG(ERR, VHOST_CONFIG,
389                                 "can't disable nonblocking on fd %d\n", fd);
390                 return -2;
391         }
392         return 0;
393 }
394
395 static void *
396 vhost_user_client_reconnect(void *arg __rte_unused)
397 {
398         int ret;
399         struct vhost_user_reconnect *reconn, *next;
400
401         while (1) {
402                 pthread_mutex_lock(&reconn_list.mutex);
403
404                 /*
405                  * An equal implementation of TAILQ_FOREACH_SAFE,
406                  * which does not exist on all platforms.
407                  */
408                 for (reconn = TAILQ_FIRST(&reconn_list.head);
409                      reconn != NULL; reconn = next) {
410                         next = TAILQ_NEXT(reconn, next);
411
412                         ret = vhost_user_connect_nonblock(reconn->fd,
413                                                 (struct sockaddr *)&reconn->un,
414                                                 sizeof(reconn->un));
415                         if (ret == -2) {
416                                 close(reconn->fd);
417                                 RTE_LOG(ERR, VHOST_CONFIG,
418                                         "reconnection for fd %d failed\n",
419                                         reconn->fd);
420                                 goto remove_fd;
421                         }
422                         if (ret == -1)
423                                 continue;
424
425                         RTE_LOG(INFO, VHOST_CONFIG,
426                                 "%s: connected\n", reconn->vsocket->path);
427                         vhost_user_add_connection(reconn->fd, reconn->vsocket);
428 remove_fd:
429                         TAILQ_REMOVE(&reconn_list.head, reconn, next);
430                         free(reconn);
431                 }
432
433                 pthread_mutex_unlock(&reconn_list.mutex);
434                 sleep(1);
435         }
436
437         return NULL;
438 }
439
440 static int
441 vhost_user_reconnect_init(void)
442 {
443         int ret;
444
445         pthread_mutex_init(&reconn_list.mutex, NULL);
446         TAILQ_INIT(&reconn_list.head);
447
448         ret = pthread_create(&reconn_tid, NULL,
449                              vhost_user_client_reconnect, NULL);
450         if (ret < 0)
451                 RTE_LOG(ERR, VHOST_CONFIG, "failed to create reconnect thread");
452
453         return ret;
454 }
455
456 static int
457 vhost_user_create_client(struct vhost_user_socket *vsocket)
458 {
459         int fd;
460         int ret;
461         struct sockaddr_un un;
462         const char *path = vsocket->path;
463         struct vhost_user_reconnect *reconn;
464
465         fd = create_unix_socket(path, &un, vsocket->is_server);
466         if (fd < 0)
467                 return -1;
468
469         ret = vhost_user_connect_nonblock(fd, (struct sockaddr *)&un,
470                                           sizeof(un));
471         if (ret == 0) {
472                 vhost_user_add_connection(fd, vsocket);
473                 return 0;
474         }
475
476         RTE_LOG(WARNING, VHOST_CONFIG,
477                 "failed to connect to %s: %s\n",
478                 path, strerror(errno));
479
480         if (ret == -2 || !vsocket->reconnect) {
481                 close(fd);
482                 return -1;
483         }
484
485         RTE_LOG(INFO, VHOST_CONFIG, "%s: reconnecting...\n", path);
486         reconn = malloc(sizeof(*reconn));
487         if (reconn == NULL) {
488                 RTE_LOG(ERR, VHOST_CONFIG,
489                         "failed to allocate memory for reconnect\n");
490                 close(fd);
491                 return -1;
492         }
493         reconn->un = un;
494         reconn->fd = fd;
495         reconn->vsocket = vsocket;
496         pthread_mutex_lock(&reconn_list.mutex);
497         TAILQ_INSERT_TAIL(&reconn_list.head, reconn, next);
498         pthread_mutex_unlock(&reconn_list.mutex);
499
500         return 0;
501 }
502
503 static struct vhost_user_socket *
504 find_vhost_user_socket(const char *path)
505 {
506         int i;
507
508         for (i = 0; i < vhost_user.vsocket_cnt; i++) {
509                 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
510
511                 if (!strcmp(vsocket->path, path))
512                         return vsocket;
513         }
514
515         return NULL;
516 }
517
518 int
519 rte_vhost_driver_disable_features(const char *path, uint64_t features)
520 {
521         struct vhost_user_socket *vsocket;
522
523         pthread_mutex_lock(&vhost_user.mutex);
524         vsocket = find_vhost_user_socket(path);
525         if (vsocket)
526                 vsocket->features &= ~features;
527         pthread_mutex_unlock(&vhost_user.mutex);
528
529         return vsocket ? 0 : -1;
530 }
531
532 int
533 rte_vhost_driver_enable_features(const char *path, uint64_t features)
534 {
535         struct vhost_user_socket *vsocket;
536
537         pthread_mutex_lock(&vhost_user.mutex);
538         vsocket = find_vhost_user_socket(path);
539         if (vsocket) {
540                 if ((vsocket->supported_features & features) != features) {
541                         /*
542                          * trying to enable features the driver doesn't
543                          * support.
544                          */
545                         pthread_mutex_unlock(&vhost_user.mutex);
546                         return -1;
547                 }
548                 vsocket->features |= features;
549         }
550         pthread_mutex_unlock(&vhost_user.mutex);
551
552         return vsocket ? 0 : -1;
553 }
554
555 int
556 rte_vhost_driver_set_features(const char *path, uint64_t features)
557 {
558         struct vhost_user_socket *vsocket;
559
560         pthread_mutex_lock(&vhost_user.mutex);
561         vsocket = find_vhost_user_socket(path);
562         if (vsocket) {
563                 vsocket->supported_features = features;
564                 vsocket->features = features;
565         }
566         pthread_mutex_unlock(&vhost_user.mutex);
567
568         return vsocket ? 0 : -1;
569 }
570
571 int
572 rte_vhost_driver_get_features(const char *path, uint64_t *features)
573 {
574         struct vhost_user_socket *vsocket;
575
576         pthread_mutex_lock(&vhost_user.mutex);
577         vsocket = find_vhost_user_socket(path);
578         if (vsocket)
579                 *features = vsocket->features;
580         pthread_mutex_unlock(&vhost_user.mutex);
581
582         if (!vsocket) {
583                 RTE_LOG(ERR, VHOST_CONFIG,
584                         "socket file %s is not registered yet.\n", path);
585                 return -1;
586         } else {
587                 return 0;
588         }
589 }
590
591 /*
592  * Register a new vhost-user socket; here we could act as server
593  * (the default case), or client (when RTE_VHOST_USER_CLIENT) flag
594  * is set.
595  */
596 int
597 rte_vhost_driver_register(const char *path, uint64_t flags)
598 {
599         int ret = -1;
600         struct vhost_user_socket *vsocket;
601
602         if (!path)
603                 return -1;
604
605         pthread_mutex_lock(&vhost_user.mutex);
606
607         if (vhost_user.vsocket_cnt == MAX_VHOST_SOCKET) {
608                 RTE_LOG(ERR, VHOST_CONFIG,
609                         "error: the number of vhost sockets reaches maximum\n");
610                 goto out;
611         }
612
613         vsocket = malloc(sizeof(struct vhost_user_socket));
614         if (!vsocket)
615                 goto out;
616         memset(vsocket, 0, sizeof(struct vhost_user_socket));
617         vsocket->path = strdup(path);
618         TAILQ_INIT(&vsocket->conn_list);
619         pthread_mutex_init(&vsocket->conn_mutex, NULL);
620         vsocket->dequeue_zero_copy = flags & RTE_VHOST_USER_DEQUEUE_ZERO_COPY;
621
622         /*
623          * Set the supported features correctly for the builtin vhost-user
624          * net driver.
625          *
626          * Applications know nothing about features the builtin virtio net
627          * driver (virtio_net.c) supports, thus it's not possible for them
628          * to invoke rte_vhost_driver_set_features(). To workaround it, here
629          * we set it unconditionally. If the application want to implement
630          * another vhost-user driver (say SCSI), it should call the
631          * rte_vhost_driver_set_features(), which will overwrite following
632          * two values.
633          */
634         vsocket->supported_features = VIRTIO_NET_SUPPORTED_FEATURES;
635         vsocket->features           = VIRTIO_NET_SUPPORTED_FEATURES;
636
637         if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
638                 vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
639                 if (vsocket->reconnect && reconn_tid == 0) {
640                         if (vhost_user_reconnect_init() < 0) {
641                                 free(vsocket->path);
642                                 free(vsocket);
643                                 goto out;
644                         }
645                 }
646                 ret = vhost_user_create_client(vsocket);
647         } else {
648                 vsocket->is_server = true;
649                 ret = vhost_user_create_server(vsocket);
650         }
651         if (ret < 0) {
652                 free(vsocket->path);
653                 free(vsocket);
654                 goto out;
655         }
656
657         vhost_user.vsockets[vhost_user.vsocket_cnt++] = vsocket;
658
659 out:
660         pthread_mutex_unlock(&vhost_user.mutex);
661
662         return ret;
663 }
664
665 static bool
666 vhost_user_remove_reconnect(struct vhost_user_socket *vsocket)
667 {
668         int found = false;
669         struct vhost_user_reconnect *reconn, *next;
670
671         pthread_mutex_lock(&reconn_list.mutex);
672
673         for (reconn = TAILQ_FIRST(&reconn_list.head);
674              reconn != NULL; reconn = next) {
675                 next = TAILQ_NEXT(reconn, next);
676
677                 if (reconn->vsocket == vsocket) {
678                         TAILQ_REMOVE(&reconn_list.head, reconn, next);
679                         close(reconn->fd);
680                         free(reconn);
681                         found = true;
682                         break;
683                 }
684         }
685         pthread_mutex_unlock(&reconn_list.mutex);
686         return found;
687 }
688
689 /**
690  * Unregister the specified vhost socket
691  */
692 int
693 rte_vhost_driver_unregister(const char *path)
694 {
695         int i;
696         int count;
697         struct vhost_user_connection *conn, *next;
698
699         pthread_mutex_lock(&vhost_user.mutex);
700
701         for (i = 0; i < vhost_user.vsocket_cnt; i++) {
702                 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
703
704                 if (!strcmp(vsocket->path, path)) {
705                         if (vsocket->is_server) {
706                                 fdset_del(&vhost_user.fdset, vsocket->listenfd);
707                                 close(vsocket->listenfd);
708                                 unlink(path);
709                         } else if (vsocket->reconnect) {
710                                 vhost_user_remove_reconnect(vsocket);
711                         }
712
713                         pthread_mutex_lock(&vsocket->conn_mutex);
714                         for (conn = TAILQ_FIRST(&vsocket->conn_list);
715                              conn != NULL;
716                              conn = next) {
717                                 next = TAILQ_NEXT(conn, next);
718
719                                 fdset_del(&vhost_user.fdset, conn->connfd);
720                                 RTE_LOG(INFO, VHOST_CONFIG,
721                                         "free connfd = %d for device '%s'\n",
722                                         conn->connfd, path);
723                                 close(conn->connfd);
724                                 vhost_destroy_device(conn->vid);
725                                 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
726                                 free(conn);
727                         }
728                         pthread_mutex_unlock(&vsocket->conn_mutex);
729
730                         free(vsocket->path);
731                         free(vsocket);
732
733                         count = --vhost_user.vsocket_cnt;
734                         vhost_user.vsockets[i] = vhost_user.vsockets[count];
735                         vhost_user.vsockets[count] = NULL;
736                         pthread_mutex_unlock(&vhost_user.mutex);
737
738                         return 0;
739                 }
740         }
741         pthread_mutex_unlock(&vhost_user.mutex);
742
743         return -1;
744 }
745
746 int
747 rte_vhost_driver_session_start(void)
748 {
749         fdset_event_dispatch(&vhost_user.fdset);
750         return 0;
751 }