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