New upstream version 18.02
[deb_dpdk.git] / lib / librte_vhost / socket.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2016 Intel Corporation
3  */
4
5 #include <stdint.h>
6 #include <stdio.h>
7 #include <stdbool.h>
8 #include <limits.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <string.h>
12 #include <sys/types.h>
13 #include <sys/socket.h>
14 #include <sys/un.h>
15 #include <sys/queue.h>
16 #include <errno.h>
17 #include <fcntl.h>
18 #include <pthread.h>
19
20 #include <rte_log.h>
21
22 #include "fd_man.h"
23 #include "vhost.h"
24 #include "vhost_user.h"
25
26
27 TAILQ_HEAD(vhost_user_connection_list, vhost_user_connection);
28
29 /*
30  * Every time rte_vhost_driver_register() is invoked, an associated
31  * vhost_user_socket struct will be created.
32  */
33 struct vhost_user_socket {
34         struct vhost_user_connection_list conn_list;
35         pthread_mutex_t conn_mutex;
36         char *path;
37         int socket_fd;
38         struct sockaddr_un un;
39         bool is_server;
40         bool reconnect;
41         bool dequeue_zero_copy;
42         bool iommu_support;
43         bool use_builtin_virtio_net;
44
45         /*
46          * The "supported_features" indicates the feature bits the
47          * vhost driver supports. The "features" indicates the feature
48          * bits after the rte_vhost_driver_features_disable/enable().
49          * It is also the final feature bits used for vhost-user
50          * features negotiation.
51          */
52         uint64_t supported_features;
53         uint64_t features;
54
55         struct vhost_device_ops const *notify_ops;
56 };
57
58 struct vhost_user_connection {
59         struct vhost_user_socket *vsocket;
60         int connfd;
61         int vid;
62
63         TAILQ_ENTRY(vhost_user_connection) next;
64 };
65
66 #define MAX_VHOST_SOCKET 1024
67 struct vhost_user {
68         struct vhost_user_socket *vsockets[MAX_VHOST_SOCKET];
69         struct fdset fdset;
70         int vsocket_cnt;
71         pthread_mutex_t mutex;
72 };
73
74 #define MAX_VIRTIO_BACKLOG 128
75
76 static void vhost_user_server_new_connection(int fd, void *data, int *remove);
77 static void vhost_user_read_cb(int fd, void *dat, int *remove);
78 static int create_unix_socket(struct vhost_user_socket *vsocket);
79 static int vhost_user_start_client(struct vhost_user_socket *vsocket);
80
81 static struct vhost_user vhost_user = {
82         .fdset = {
83                 .fd = { [0 ... MAX_FDS - 1] = {-1, NULL, NULL, NULL, 0} },
84                 .fd_mutex = PTHREAD_MUTEX_INITIALIZER,
85                 .num = 0
86         },
87         .vsocket_cnt = 0,
88         .mutex = PTHREAD_MUTEX_INITIALIZER,
89 };
90
91 /* return bytes# of read on success or negative val on failure. */
92 int
93 read_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
94 {
95         struct iovec iov;
96         struct msghdr msgh;
97         size_t fdsize = fd_num * sizeof(int);
98         char control[CMSG_SPACE(fdsize)];
99         struct cmsghdr *cmsg;
100         int ret;
101
102         memset(&msgh, 0, sizeof(msgh));
103         iov.iov_base = buf;
104         iov.iov_len  = buflen;
105
106         msgh.msg_iov = &iov;
107         msgh.msg_iovlen = 1;
108         msgh.msg_control = control;
109         msgh.msg_controllen = sizeof(control);
110
111         ret = recvmsg(sockfd, &msgh, 0);
112         if (ret <= 0) {
113                 RTE_LOG(ERR, VHOST_CONFIG, "recvmsg failed\n");
114                 return ret;
115         }
116
117         if (msgh.msg_flags & (MSG_TRUNC | MSG_CTRUNC)) {
118                 RTE_LOG(ERR, VHOST_CONFIG, "truncted msg\n");
119                 return -1;
120         }
121
122         for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
123                 cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
124                 if ((cmsg->cmsg_level == SOL_SOCKET) &&
125                         (cmsg->cmsg_type == SCM_RIGHTS)) {
126                         memcpy(fds, CMSG_DATA(cmsg), fdsize);
127                         break;
128                 }
129         }
130
131         return ret;
132 }
133
134 int
135 send_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
136 {
137
138         struct iovec iov;
139         struct msghdr msgh;
140         size_t fdsize = fd_num * sizeof(int);
141         char control[CMSG_SPACE(fdsize)];
142         struct cmsghdr *cmsg;
143         int ret;
144
145         memset(&msgh, 0, sizeof(msgh));
146         iov.iov_base = buf;
147         iov.iov_len = buflen;
148
149         msgh.msg_iov = &iov;
150         msgh.msg_iovlen = 1;
151
152         if (fds && fd_num > 0) {
153                 msgh.msg_control = control;
154                 msgh.msg_controllen = sizeof(control);
155                 cmsg = CMSG_FIRSTHDR(&msgh);
156                 cmsg->cmsg_len = CMSG_LEN(fdsize);
157                 cmsg->cmsg_level = SOL_SOCKET;
158                 cmsg->cmsg_type = SCM_RIGHTS;
159                 memcpy(CMSG_DATA(cmsg), fds, fdsize);
160         } else {
161                 msgh.msg_control = NULL;
162                 msgh.msg_controllen = 0;
163         }
164
165         do {
166                 ret = sendmsg(sockfd, &msgh, 0);
167         } while (ret < 0 && errno == EINTR);
168
169         if (ret < 0) {
170                 RTE_LOG(ERR, VHOST_CONFIG,  "sendmsg error\n");
171                 return ret;
172         }
173
174         return ret;
175 }
176
177 static void
178 vhost_user_add_connection(int fd, struct vhost_user_socket *vsocket)
179 {
180         int vid;
181         size_t size;
182         struct vhost_user_connection *conn;
183         int ret;
184
185         conn = malloc(sizeof(*conn));
186         if (conn == NULL) {
187                 close(fd);
188                 return;
189         }
190
191         vid = vhost_new_device();
192         if (vid == -1) {
193                 goto err;
194         }
195
196         size = strnlen(vsocket->path, PATH_MAX);
197         vhost_set_ifname(vid, vsocket->path, size);
198
199         vhost_set_builtin_virtio_net(vid, vsocket->use_builtin_virtio_net);
200
201         if (vsocket->dequeue_zero_copy)
202                 vhost_enable_dequeue_zero_copy(vid);
203
204         RTE_LOG(INFO, VHOST_CONFIG, "new device, handle is %d\n", vid);
205
206         if (vsocket->notify_ops->new_connection) {
207                 ret = vsocket->notify_ops->new_connection(vid);
208                 if (ret < 0) {
209                         RTE_LOG(ERR, VHOST_CONFIG,
210                                 "failed to add vhost user connection with fd %d\n",
211                                 fd);
212                         goto err;
213                 }
214         }
215
216         conn->connfd = fd;
217         conn->vsocket = vsocket;
218         conn->vid = vid;
219         ret = fdset_add(&vhost_user.fdset, fd, vhost_user_read_cb,
220                         NULL, conn);
221         if (ret < 0) {
222                 RTE_LOG(ERR, VHOST_CONFIG,
223                         "failed to add fd %d into vhost server fdset\n",
224                         fd);
225
226                 if (vsocket->notify_ops->destroy_connection)
227                         vsocket->notify_ops->destroy_connection(conn->vid);
228
229                 goto err;
230         }
231
232         pthread_mutex_lock(&vsocket->conn_mutex);
233         TAILQ_INSERT_TAIL(&vsocket->conn_list, conn, next);
234         pthread_mutex_unlock(&vsocket->conn_mutex);
235         return;
236
237 err:
238         free(conn);
239         close(fd);
240 }
241
242 /* call back when there is new vhost-user connection from client  */
243 static void
244 vhost_user_server_new_connection(int fd, void *dat, int *remove __rte_unused)
245 {
246         struct vhost_user_socket *vsocket = dat;
247
248         fd = accept(fd, NULL, NULL);
249         if (fd < 0)
250                 return;
251
252         RTE_LOG(INFO, VHOST_CONFIG, "new vhost user connection is %d\n", fd);
253         vhost_user_add_connection(fd, vsocket);
254 }
255
256 static void
257 vhost_user_read_cb(int connfd, void *dat, int *remove)
258 {
259         struct vhost_user_connection *conn = dat;
260         struct vhost_user_socket *vsocket = conn->vsocket;
261         int ret;
262
263         ret = vhost_user_msg_handler(conn->vid, connfd);
264         if (ret < 0) {
265                 close(connfd);
266                 *remove = 1;
267                 vhost_destroy_device(conn->vid);
268
269                 if (vsocket->notify_ops->destroy_connection)
270                         vsocket->notify_ops->destroy_connection(conn->vid);
271
272                 pthread_mutex_lock(&vsocket->conn_mutex);
273                 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
274                 pthread_mutex_unlock(&vsocket->conn_mutex);
275
276                 free(conn);
277
278                 if (vsocket->reconnect) {
279                         create_unix_socket(vsocket);
280                         vhost_user_start_client(vsocket);
281                 }
282         }
283 }
284
285 static int
286 create_unix_socket(struct vhost_user_socket *vsocket)
287 {
288         int fd;
289         struct sockaddr_un *un = &vsocket->un;
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                 vsocket->is_server ? "server" : "client", fd);
296
297         if (!vsocket->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, vsocket->path, sizeof(un->sun_path));
308         un->sun_path[sizeof(un->sun_path) - 1] = '\0';
309
310         vsocket->socket_fd = fd;
311         return 0;
312 }
313
314 static int
315 vhost_user_start_server(struct vhost_user_socket *vsocket)
316 {
317         int ret;
318         int fd = vsocket->socket_fd;
319         const char *path = vsocket->path;
320
321         ret = bind(fd, (struct sockaddr *)&vsocket->un, sizeof(vsocket->un));
322         if (ret < 0) {
323                 RTE_LOG(ERR, VHOST_CONFIG,
324                         "failed to bind to %s: %s; remove it and try again\n",
325                         path, strerror(errno));
326                 goto err;
327         }
328         RTE_LOG(INFO, VHOST_CONFIG, "bind to %s\n", path);
329
330         ret = listen(fd, MAX_VIRTIO_BACKLOG);
331         if (ret < 0)
332                 goto err;
333
334         ret = fdset_add(&vhost_user.fdset, fd, vhost_user_server_new_connection,
335                   NULL, vsocket);
336         if (ret < 0) {
337                 RTE_LOG(ERR, VHOST_CONFIG,
338                         "failed to add listen fd %d to vhost server fdset\n",
339                         fd);
340                 goto err;
341         }
342
343         return 0;
344
345 err:
346         close(fd);
347         return -1;
348 }
349
350 struct vhost_user_reconnect {
351         struct sockaddr_un un;
352         int fd;
353         struct vhost_user_socket *vsocket;
354
355         TAILQ_ENTRY(vhost_user_reconnect) next;
356 };
357
358 TAILQ_HEAD(vhost_user_reconnect_tailq_list, vhost_user_reconnect);
359 struct vhost_user_reconnect_list {
360         struct vhost_user_reconnect_tailq_list head;
361         pthread_mutex_t mutex;
362 };
363
364 static struct vhost_user_reconnect_list reconn_list;
365 static pthread_t reconn_tid;
366
367 static int
368 vhost_user_connect_nonblock(int fd, struct sockaddr *un, size_t sz)
369 {
370         int ret, flags;
371
372         ret = connect(fd, un, sz);
373         if (ret < 0 && errno != EISCONN)
374                 return -1;
375
376         flags = fcntl(fd, F_GETFL, 0);
377         if (flags < 0) {
378                 RTE_LOG(ERR, VHOST_CONFIG,
379                         "can't get flags for connfd %d\n", fd);
380                 return -2;
381         }
382         if ((flags & O_NONBLOCK) && fcntl(fd, F_SETFL, flags & ~O_NONBLOCK)) {
383                 RTE_LOG(ERR, VHOST_CONFIG,
384                                 "can't disable nonblocking on fd %d\n", fd);
385                 return -2;
386         }
387         return 0;
388 }
389
390 static void *
391 vhost_user_client_reconnect(void *arg __rte_unused)
392 {
393         int ret;
394         struct vhost_user_reconnect *reconn, *next;
395
396         while (1) {
397                 pthread_mutex_lock(&reconn_list.mutex);
398
399                 /*
400                  * An equal implementation of TAILQ_FOREACH_SAFE,
401                  * which does not exist on all platforms.
402                  */
403                 for (reconn = TAILQ_FIRST(&reconn_list.head);
404                      reconn != NULL; reconn = next) {
405                         next = TAILQ_NEXT(reconn, next);
406
407                         ret = vhost_user_connect_nonblock(reconn->fd,
408                                                 (struct sockaddr *)&reconn->un,
409                                                 sizeof(reconn->un));
410                         if (ret == -2) {
411                                 close(reconn->fd);
412                                 RTE_LOG(ERR, VHOST_CONFIG,
413                                         "reconnection for fd %d failed\n",
414                                         reconn->fd);
415                                 goto remove_fd;
416                         }
417                         if (ret == -1)
418                                 continue;
419
420                         RTE_LOG(INFO, VHOST_CONFIG,
421                                 "%s: connected\n", reconn->vsocket->path);
422                         vhost_user_add_connection(reconn->fd, reconn->vsocket);
423 remove_fd:
424                         TAILQ_REMOVE(&reconn_list.head, reconn, next);
425                         free(reconn);
426                 }
427
428                 pthread_mutex_unlock(&reconn_list.mutex);
429                 sleep(1);
430         }
431
432         return NULL;
433 }
434
435 static int
436 vhost_user_reconnect_init(void)
437 {
438         int ret;
439         char thread_name[RTE_MAX_THREAD_NAME_LEN];
440
441         ret = pthread_mutex_init(&reconn_list.mutex, NULL);
442         if (ret < 0) {
443                 RTE_LOG(ERR, VHOST_CONFIG, "failed to initialize mutex");
444                 return ret;
445         }
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                 if (pthread_mutex_destroy(&reconn_list.mutex)) {
453                         RTE_LOG(ERR, VHOST_CONFIG,
454                                 "failed to destroy reconnect mutex");
455                 }
456         } else {
457                 snprintf(thread_name, RTE_MAX_THREAD_NAME_LEN,
458                          "vhost-reconn");
459
460                 if (rte_thread_setname(reconn_tid, thread_name)) {
461                         RTE_LOG(DEBUG, VHOST_CONFIG,
462                                 "failed to set reconnect thread name");
463                 }
464         }
465
466         return ret;
467 }
468
469 static int
470 vhost_user_start_client(struct vhost_user_socket *vsocket)
471 {
472         int ret;
473         int fd = vsocket->socket_fd;
474         const char *path = vsocket->path;
475         struct vhost_user_reconnect *reconn;
476
477         ret = vhost_user_connect_nonblock(fd, (struct sockaddr *)&vsocket->un,
478                                           sizeof(vsocket->un));
479         if (ret == 0) {
480                 vhost_user_add_connection(fd, vsocket);
481                 return 0;
482         }
483
484         RTE_LOG(WARNING, VHOST_CONFIG,
485                 "failed to connect to %s: %s\n",
486                 path, strerror(errno));
487
488         if (ret == -2 || !vsocket->reconnect) {
489                 close(fd);
490                 return -1;
491         }
492
493         RTE_LOG(INFO, VHOST_CONFIG, "%s: reconnecting...\n", path);
494         reconn = malloc(sizeof(*reconn));
495         if (reconn == NULL) {
496                 RTE_LOG(ERR, VHOST_CONFIG,
497                         "failed to allocate memory for reconnect\n");
498                 close(fd);
499                 return -1;
500         }
501         reconn->un = vsocket->un;
502         reconn->fd = fd;
503         reconn->vsocket = vsocket;
504         pthread_mutex_lock(&reconn_list.mutex);
505         TAILQ_INSERT_TAIL(&reconn_list.head, reconn, next);
506         pthread_mutex_unlock(&reconn_list.mutex);
507
508         return 0;
509 }
510
511 static struct vhost_user_socket *
512 find_vhost_user_socket(const char *path)
513 {
514         int i;
515
516         for (i = 0; i < vhost_user.vsocket_cnt; i++) {
517                 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
518
519                 if (!strcmp(vsocket->path, path))
520                         return vsocket;
521         }
522
523         return NULL;
524 }
525
526 int
527 rte_vhost_driver_disable_features(const char *path, uint64_t features)
528 {
529         struct vhost_user_socket *vsocket;
530
531         pthread_mutex_lock(&vhost_user.mutex);
532         vsocket = find_vhost_user_socket(path);
533
534         /* Note that use_builtin_virtio_net is not affected by this function
535          * since callers may want to selectively disable features of the
536          * built-in vhost net device backend.
537          */
538
539         if (vsocket)
540                 vsocket->features &= ~features;
541         pthread_mutex_unlock(&vhost_user.mutex);
542
543         return vsocket ? 0 : -1;
544 }
545
546 int
547 rte_vhost_driver_enable_features(const char *path, uint64_t features)
548 {
549         struct vhost_user_socket *vsocket;
550
551         pthread_mutex_lock(&vhost_user.mutex);
552         vsocket = find_vhost_user_socket(path);
553         if (vsocket) {
554                 if ((vsocket->supported_features & features) != features) {
555                         /*
556                          * trying to enable features the driver doesn't
557                          * support.
558                          */
559                         pthread_mutex_unlock(&vhost_user.mutex);
560                         return -1;
561                 }
562                 vsocket->features |= features;
563         }
564         pthread_mutex_unlock(&vhost_user.mutex);
565
566         return vsocket ? 0 : -1;
567 }
568
569 int
570 rte_vhost_driver_set_features(const char *path, uint64_t features)
571 {
572         struct vhost_user_socket *vsocket;
573
574         pthread_mutex_lock(&vhost_user.mutex);
575         vsocket = find_vhost_user_socket(path);
576         if (vsocket) {
577                 vsocket->supported_features = features;
578                 vsocket->features = features;
579
580                 /* Anyone setting feature bits is implementing their own vhost
581                  * device backend.
582                  */
583                 vsocket->use_builtin_virtio_net = false;
584         }
585         pthread_mutex_unlock(&vhost_user.mutex);
586
587         return vsocket ? 0 : -1;
588 }
589
590 int
591 rte_vhost_driver_get_features(const char *path, uint64_t *features)
592 {
593         struct vhost_user_socket *vsocket;
594
595         pthread_mutex_lock(&vhost_user.mutex);
596         vsocket = find_vhost_user_socket(path);
597         if (vsocket)
598                 *features = vsocket->features;
599         pthread_mutex_unlock(&vhost_user.mutex);
600
601         if (!vsocket) {
602                 RTE_LOG(ERR, VHOST_CONFIG,
603                         "socket file %s is not registered yet.\n", path);
604                 return -1;
605         } else {
606                 return 0;
607         }
608 }
609
610 /*
611  * Register a new vhost-user socket; here we could act as server
612  * (the default case), or client (when RTE_VHOST_USER_CLIENT) flag
613  * is set.
614  */
615 int
616 rte_vhost_driver_register(const char *path, uint64_t flags)
617 {
618         int ret = -1;
619         struct vhost_user_socket *vsocket;
620
621         if (!path)
622                 return -1;
623
624         pthread_mutex_lock(&vhost_user.mutex);
625
626         if (vhost_user.vsocket_cnt == MAX_VHOST_SOCKET) {
627                 RTE_LOG(ERR, VHOST_CONFIG,
628                         "error: the number of vhost sockets reaches maximum\n");
629                 goto out;
630         }
631
632         vsocket = malloc(sizeof(struct vhost_user_socket));
633         if (!vsocket)
634                 goto out;
635         memset(vsocket, 0, sizeof(struct vhost_user_socket));
636         vsocket->path = strdup(path);
637         if (vsocket->path == NULL) {
638                 RTE_LOG(ERR, VHOST_CONFIG,
639                         "error: failed to copy socket path string\n");
640                 free(vsocket);
641                 goto out;
642         }
643         TAILQ_INIT(&vsocket->conn_list);
644         ret = pthread_mutex_init(&vsocket->conn_mutex, NULL);
645         if (ret) {
646                 RTE_LOG(ERR, VHOST_CONFIG,
647                         "error: failed to init connection mutex\n");
648                 goto out_free;
649         }
650         vsocket->dequeue_zero_copy = flags & RTE_VHOST_USER_DEQUEUE_ZERO_COPY;
651
652         /*
653          * Set the supported features correctly for the builtin vhost-user
654          * net driver.
655          *
656          * Applications know nothing about features the builtin virtio net
657          * driver (virtio_net.c) supports, thus it's not possible for them
658          * to invoke rte_vhost_driver_set_features(). To workaround it, here
659          * we set it unconditionally. If the application want to implement
660          * another vhost-user driver (say SCSI), it should call the
661          * rte_vhost_driver_set_features(), which will overwrite following
662          * two values.
663          */
664         vsocket->use_builtin_virtio_net = true;
665         vsocket->supported_features = VIRTIO_NET_SUPPORTED_FEATURES;
666         vsocket->features           = VIRTIO_NET_SUPPORTED_FEATURES;
667
668         if (!(flags & RTE_VHOST_USER_IOMMU_SUPPORT)) {
669                 vsocket->supported_features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
670                 vsocket->features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
671         }
672
673         if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
674                 vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
675                 if (vsocket->reconnect && reconn_tid == 0) {
676                         if (vhost_user_reconnect_init() != 0)
677                                 goto out_mutex;
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 }