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