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