vhost: introduce API to start a specific driver
[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         }
246
247         pthread_mutex_lock(&vsocket->conn_mutex);
248         TAILQ_INSERT_TAIL(&vsocket->conn_list, conn, next);
249         pthread_mutex_unlock(&vsocket->conn_mutex);
250 }
251
252 /* call back when there is new vhost-user connection from client  */
253 static void
254 vhost_user_server_new_connection(int fd, void *dat, int *remove __rte_unused)
255 {
256         struct vhost_user_socket *vsocket = dat;
257
258         fd = accept(fd, NULL, NULL);
259         if (fd < 0)
260                 return;
261
262         RTE_LOG(INFO, VHOST_CONFIG, "new vhost user connection is %d\n", fd);
263         vhost_user_add_connection(fd, vsocket);
264 }
265
266 static void
267 vhost_user_read_cb(int connfd, void *dat, int *remove)
268 {
269         struct vhost_user_connection *conn = dat;
270         struct vhost_user_socket *vsocket = conn->vsocket;
271         int ret;
272
273         ret = vhost_user_msg_handler(conn->vid, connfd);
274         if (ret < 0) {
275                 close(connfd);
276                 *remove = 1;
277                 vhost_destroy_device(conn->vid);
278
279                 pthread_mutex_lock(&vsocket->conn_mutex);
280                 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
281                 pthread_mutex_unlock(&vsocket->conn_mutex);
282
283                 free(conn);
284
285                 if (vsocket->reconnect) {
286                         create_unix_socket(vsocket);
287                         vhost_user_start_client(vsocket);
288                 }
289         }
290 }
291
292 static int
293 create_unix_socket(struct vhost_user_socket *vsocket)
294 {
295         int fd;
296         struct sockaddr_un *un = &vsocket->un;
297
298         fd = socket(AF_UNIX, SOCK_STREAM, 0);
299         if (fd < 0)
300                 return -1;
301         RTE_LOG(INFO, VHOST_CONFIG, "vhost-user %s: socket created, fd: %d\n",
302                 vsocket->is_server ? "server" : "client", fd);
303
304         if (!vsocket->is_server && fcntl(fd, F_SETFL, O_NONBLOCK)) {
305                 RTE_LOG(ERR, VHOST_CONFIG,
306                         "vhost-user: can't set nonblocking mode for socket, fd: "
307                         "%d (%s)\n", fd, strerror(errno));
308                 close(fd);
309                 return -1;
310         }
311
312         memset(un, 0, sizeof(*un));
313         un->sun_family = AF_UNIX;
314         strncpy(un->sun_path, vsocket->path, sizeof(un->sun_path));
315         un->sun_path[sizeof(un->sun_path) - 1] = '\0';
316
317         vsocket->socket_fd = fd;
318         return 0;
319 }
320
321 static int
322 vhost_user_start_server(struct vhost_user_socket *vsocket)
323 {
324         int ret;
325         int fd = vsocket->socket_fd;
326         const char *path = vsocket->path;
327
328         ret = bind(fd, (struct sockaddr *)&vsocket->un, sizeof(vsocket->un));
329         if (ret < 0) {
330                 RTE_LOG(ERR, VHOST_CONFIG,
331                         "failed to bind to %s: %s; remove it and try again\n",
332                         path, strerror(errno));
333                 goto err;
334         }
335         RTE_LOG(INFO, VHOST_CONFIG, "bind to %s\n", path);
336
337         ret = listen(fd, MAX_VIRTIO_BACKLOG);
338         if (ret < 0)
339                 goto err;
340
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_start_client(struct vhost_user_socket *vsocket)
460 {
461         int ret;
462         int fd = vsocket->socket_fd;
463         const char *path = vsocket->path;
464         struct vhost_user_reconnect *reconn;
465
466         ret = vhost_user_connect_nonblock(fd, (struct sockaddr *)&vsocket->un,
467                                           sizeof(vsocket->un));
468         if (ret == 0) {
469                 vhost_user_add_connection(fd, vsocket);
470                 return 0;
471         }
472
473         RTE_LOG(WARNING, VHOST_CONFIG,
474                 "failed to connect to %s: %s\n",
475                 path, strerror(errno));
476
477         if (ret == -2 || !vsocket->reconnect) {
478                 close(fd);
479                 return -1;
480         }
481
482         RTE_LOG(INFO, VHOST_CONFIG, "%s: reconnecting...\n", path);
483         reconn = malloc(sizeof(*reconn));
484         if (reconn == NULL) {
485                 RTE_LOG(ERR, VHOST_CONFIG,
486                         "failed to allocate memory for reconnect\n");
487                 close(fd);
488                 return -1;
489         }
490         reconn->un = vsocket->un;
491         reconn->fd = fd;
492         reconn->vsocket = vsocket;
493         pthread_mutex_lock(&reconn_list.mutex);
494         TAILQ_INSERT_TAIL(&reconn_list.head, reconn, next);
495         pthread_mutex_unlock(&reconn_list.mutex);
496
497         return 0;
498 }
499
500 static struct vhost_user_socket *
501 find_vhost_user_socket(const char *path)
502 {
503         int i;
504
505         for (i = 0; i < vhost_user.vsocket_cnt; i++) {
506                 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
507
508                 if (!strcmp(vsocket->path, path))
509                         return vsocket;
510         }
511
512         return NULL;
513 }
514
515 int
516 rte_vhost_driver_disable_features(const char *path, uint64_t features)
517 {
518         struct vhost_user_socket *vsocket;
519
520         pthread_mutex_lock(&vhost_user.mutex);
521         vsocket = find_vhost_user_socket(path);
522         if (vsocket)
523                 vsocket->features &= ~features;
524         pthread_mutex_unlock(&vhost_user.mutex);
525
526         return vsocket ? 0 : -1;
527 }
528
529 int
530 rte_vhost_driver_enable_features(const char *path, uint64_t features)
531 {
532         struct vhost_user_socket *vsocket;
533
534         pthread_mutex_lock(&vhost_user.mutex);
535         vsocket = find_vhost_user_socket(path);
536         if (vsocket) {
537                 if ((vsocket->supported_features & features) != features) {
538                         /*
539                          * trying to enable features the driver doesn't
540                          * support.
541                          */
542                         pthread_mutex_unlock(&vhost_user.mutex);
543                         return -1;
544                 }
545                 vsocket->features |= features;
546         }
547         pthread_mutex_unlock(&vhost_user.mutex);
548
549         return vsocket ? 0 : -1;
550 }
551
552 int
553 rte_vhost_driver_set_features(const char *path, uint64_t features)
554 {
555         struct vhost_user_socket *vsocket;
556
557         pthread_mutex_lock(&vhost_user.mutex);
558         vsocket = find_vhost_user_socket(path);
559         if (vsocket) {
560                 vsocket->supported_features = features;
561                 vsocket->features = features;
562         }
563         pthread_mutex_unlock(&vhost_user.mutex);
564
565         return vsocket ? 0 : -1;
566 }
567
568 int
569 rte_vhost_driver_get_features(const char *path, uint64_t *features)
570 {
571         struct vhost_user_socket *vsocket;
572
573         pthread_mutex_lock(&vhost_user.mutex);
574         vsocket = find_vhost_user_socket(path);
575         if (vsocket)
576                 *features = vsocket->features;
577         pthread_mutex_unlock(&vhost_user.mutex);
578
579         if (!vsocket) {
580                 RTE_LOG(ERR, VHOST_CONFIG,
581                         "socket file %s is not registered yet.\n", path);
582                 return -1;
583         } else {
584                 return 0;
585         }
586 }
587
588 /*
589  * Register a new vhost-user socket; here we could act as server
590  * (the default case), or client (when RTE_VHOST_USER_CLIENT) flag
591  * is set.
592  */
593 int
594 rte_vhost_driver_register(const char *path, uint64_t flags)
595 {
596         int ret = -1;
597         struct vhost_user_socket *vsocket;
598
599         if (!path)
600                 return -1;
601
602         pthread_mutex_lock(&vhost_user.mutex);
603
604         if (vhost_user.vsocket_cnt == MAX_VHOST_SOCKET) {
605                 RTE_LOG(ERR, VHOST_CONFIG,
606                         "error: the number of vhost sockets reaches maximum\n");
607                 goto out;
608         }
609
610         vsocket = malloc(sizeof(struct vhost_user_socket));
611         if (!vsocket)
612                 goto out;
613         memset(vsocket, 0, sizeof(struct vhost_user_socket));
614         vsocket->path = strdup(path);
615         TAILQ_INIT(&vsocket->conn_list);
616         pthread_mutex_init(&vsocket->conn_mutex, NULL);
617         vsocket->dequeue_zero_copy = flags & RTE_VHOST_USER_DEQUEUE_ZERO_COPY;
618
619         /*
620          * Set the supported features correctly for the builtin vhost-user
621          * net driver.
622          *
623          * Applications know nothing about features the builtin virtio net
624          * driver (virtio_net.c) supports, thus it's not possible for them
625          * to invoke rte_vhost_driver_set_features(). To workaround it, here
626          * we set it unconditionally. If the application want to implement
627          * another vhost-user driver (say SCSI), it should call the
628          * rte_vhost_driver_set_features(), which will overwrite following
629          * two values.
630          */
631         vsocket->supported_features = VIRTIO_NET_SUPPORTED_FEATURES;
632         vsocket->features           = VIRTIO_NET_SUPPORTED_FEATURES;
633
634         if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
635                 vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
636                 if (vsocket->reconnect && reconn_tid == 0) {
637                         if (vhost_user_reconnect_init() < 0) {
638                                 free(vsocket->path);
639                                 free(vsocket);
640                                 goto out;
641                         }
642                 }
643         } else {
644                 vsocket->is_server = true;
645         }
646         ret = create_unix_socket(vsocket);
647         if (ret < 0) {
648                 free(vsocket->path);
649                 free(vsocket);
650                 goto out;
651         }
652
653         vhost_user.vsockets[vhost_user.vsocket_cnt++] = vsocket;
654
655 out:
656         pthread_mutex_unlock(&vhost_user.mutex);
657
658         return ret;
659 }
660
661 static bool
662 vhost_user_remove_reconnect(struct vhost_user_socket *vsocket)
663 {
664         int found = false;
665         struct vhost_user_reconnect *reconn, *next;
666
667         pthread_mutex_lock(&reconn_list.mutex);
668
669         for (reconn = TAILQ_FIRST(&reconn_list.head);
670              reconn != NULL; reconn = next) {
671                 next = TAILQ_NEXT(reconn, next);
672
673                 if (reconn->vsocket == vsocket) {
674                         TAILQ_REMOVE(&reconn_list.head, reconn, next);
675                         close(reconn->fd);
676                         free(reconn);
677                         found = true;
678                         break;
679                 }
680         }
681         pthread_mutex_unlock(&reconn_list.mutex);
682         return found;
683 }
684
685 /**
686  * Unregister the specified vhost socket
687  */
688 int
689 rte_vhost_driver_unregister(const char *path)
690 {
691         int i;
692         int count;
693         struct vhost_user_connection *conn, *next;
694
695         pthread_mutex_lock(&vhost_user.mutex);
696
697         for (i = 0; i < vhost_user.vsocket_cnt; i++) {
698                 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
699
700                 if (!strcmp(vsocket->path, path)) {
701                         if (vsocket->is_server) {
702                                 fdset_del(&vhost_user.fdset, vsocket->socket_fd);
703                                 close(vsocket->socket_fd);
704                                 unlink(path);
705                         } else if (vsocket->reconnect) {
706                                 vhost_user_remove_reconnect(vsocket);
707                         }
708
709                         pthread_mutex_lock(&vsocket->conn_mutex);
710                         for (conn = TAILQ_FIRST(&vsocket->conn_list);
711                              conn != NULL;
712                              conn = next) {
713                                 next = TAILQ_NEXT(conn, next);
714
715                                 fdset_del(&vhost_user.fdset, conn->connfd);
716                                 RTE_LOG(INFO, VHOST_CONFIG,
717                                         "free connfd = %d for device '%s'\n",
718                                         conn->connfd, path);
719                                 close(conn->connfd);
720                                 vhost_destroy_device(conn->vid);
721                                 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
722                                 free(conn);
723                         }
724                         pthread_mutex_unlock(&vsocket->conn_mutex);
725
726                         free(vsocket->path);
727                         free(vsocket);
728
729                         count = --vhost_user.vsocket_cnt;
730                         vhost_user.vsockets[i] = vhost_user.vsockets[count];
731                         vhost_user.vsockets[count] = NULL;
732                         pthread_mutex_unlock(&vhost_user.mutex);
733
734                         return 0;
735                 }
736         }
737         pthread_mutex_unlock(&vhost_user.mutex);
738
739         return -1;
740 }
741
742 /*
743  * Register ops so that we can add/remove device to data core.
744  */
745 int
746 rte_vhost_driver_callback_register(const char *path,
747         struct vhost_device_ops const * const ops)
748 {
749         struct vhost_user_socket *vsocket;
750
751         pthread_mutex_lock(&vhost_user.mutex);
752         vsocket = find_vhost_user_socket(path);
753         if (vsocket)
754                 vsocket->notify_ops = ops;
755         pthread_mutex_unlock(&vhost_user.mutex);
756
757         return vsocket ? 0 : -1;
758 }
759
760 struct vhost_device_ops const *
761 vhost_driver_callback_get(const char *path)
762 {
763         struct vhost_user_socket *vsocket;
764
765         pthread_mutex_lock(&vhost_user.mutex);
766         vsocket = find_vhost_user_socket(path);
767         pthread_mutex_unlock(&vhost_user.mutex);
768
769         return vsocket ? vsocket->notify_ops : NULL;
770 }
771
772 int
773 rte_vhost_driver_start(const char *path)
774 {
775         struct vhost_user_socket *vsocket;
776         static pthread_t fdset_tid;
777
778         pthread_mutex_lock(&vhost_user.mutex);
779         vsocket = find_vhost_user_socket(path);
780         pthread_mutex_unlock(&vhost_user.mutex);
781
782         if (!vsocket)
783                 return -1;
784
785         if (fdset_tid == 0) {
786                 int ret = pthread_create(&fdset_tid, NULL, fdset_event_dispatch,
787                                      &vhost_user.fdset);
788                 if (ret < 0)
789                         RTE_LOG(ERR, VHOST_CONFIG,
790                                 "failed to create fdset handling thread");
791         }
792
793         if (vsocket->is_server)
794                 return vhost_user_start_server(vsocket);
795         else
796                 return vhost_user_start_client(vsocket);
797 }