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