1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2016 Intel Corporation
11 #include <sys/types.h>
12 #include <sys/socket.h>
14 #include <sys/queue.h>
23 #include "vhost_user.h"
26 TAILQ_HEAD(vhost_user_connection_list, vhost_user_connection);
29 * Every time rte_vhost_driver_register() is invoked, an associated
30 * vhost_user_socket struct will be created.
32 struct vhost_user_socket {
33 struct vhost_user_connection_list conn_list;
34 pthread_mutex_t conn_mutex;
37 struct sockaddr_un un;
40 bool dequeue_zero_copy;
42 bool use_builtin_virtio_net;
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.
51 uint64_t supported_features;
55 * Device id to identify a specific backend device.
56 * It's set to -1 for the default software implementation.
57 * If valid, one socket can have 1 connection only.
61 struct vhost_device_ops const *notify_ops;
64 struct vhost_user_connection {
65 struct vhost_user_socket *vsocket;
69 TAILQ_ENTRY(vhost_user_connection) next;
72 #define MAX_VHOST_SOCKET 1024
74 struct vhost_user_socket *vsockets[MAX_VHOST_SOCKET];
77 pthread_mutex_t mutex;
80 #define MAX_VIRTIO_BACKLOG 128
82 static void vhost_user_server_new_connection(int fd, void *data, int *remove);
83 static void vhost_user_read_cb(int fd, void *dat, int *remove);
84 static int create_unix_socket(struct vhost_user_socket *vsocket);
85 static int vhost_user_start_client(struct vhost_user_socket *vsocket);
87 static struct vhost_user vhost_user = {
89 .fd = { [0 ... MAX_FDS - 1] = {-1, NULL, NULL, NULL, 0} },
90 .fd_mutex = PTHREAD_MUTEX_INITIALIZER,
94 .mutex = PTHREAD_MUTEX_INITIALIZER,
98 * return bytes# of read on success or negative val on failure. Update fdnum
99 * with number of fds read.
102 read_fd_message(int sockfd, char *buf, int buflen, int *fds, int max_fds,
107 char control[CMSG_SPACE(max_fds * sizeof(int))];
108 struct cmsghdr *cmsg;
114 memset(&msgh, 0, sizeof(msgh));
116 iov.iov_len = buflen;
120 msgh.msg_control = control;
121 msgh.msg_controllen = sizeof(control);
123 ret = recvmsg(sockfd, &msgh, 0);
125 RTE_LOG(ERR, VHOST_CONFIG, "recvmsg failed\n");
129 if (msgh.msg_flags & (MSG_TRUNC | MSG_CTRUNC)) {
130 RTE_LOG(ERR, VHOST_CONFIG, "truncted msg\n");
134 for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
135 cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
136 if ((cmsg->cmsg_level == SOL_SOCKET) &&
137 (cmsg->cmsg_type == SCM_RIGHTS)) {
138 got_fds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
140 memcpy(fds, CMSG_DATA(cmsg), got_fds * sizeof(int));
145 /* Clear out unused file descriptors */
146 while (got_fds < max_fds)
153 send_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
158 size_t fdsize = fd_num * sizeof(int);
159 char control[CMSG_SPACE(fdsize)];
160 struct cmsghdr *cmsg;
163 memset(&msgh, 0, sizeof(msgh));
165 iov.iov_len = buflen;
170 if (fds && fd_num > 0) {
171 msgh.msg_control = control;
172 msgh.msg_controllen = sizeof(control);
173 cmsg = CMSG_FIRSTHDR(&msgh);
175 RTE_LOG(ERR, VHOST_CONFIG, "cmsg == NULL\n");
179 cmsg->cmsg_len = CMSG_LEN(fdsize);
180 cmsg->cmsg_level = SOL_SOCKET;
181 cmsg->cmsg_type = SCM_RIGHTS;
182 memcpy(CMSG_DATA(cmsg), fds, fdsize);
184 msgh.msg_control = NULL;
185 msgh.msg_controllen = 0;
189 ret = sendmsg(sockfd, &msgh, MSG_NOSIGNAL);
190 } while (ret < 0 && errno == EINTR);
193 RTE_LOG(ERR, VHOST_CONFIG, "sendmsg error\n");
201 vhost_user_add_connection(int fd, struct vhost_user_socket *vsocket)
205 struct vhost_user_connection *conn;
211 conn = malloc(sizeof(*conn));
217 vid = vhost_new_device();
222 size = strnlen(vsocket->path, PATH_MAX);
223 vhost_set_ifname(vid, vsocket->path, size);
225 vhost_set_builtin_virtio_net(vid, vsocket->use_builtin_virtio_net);
227 vhost_attach_vdpa_device(vid, vsocket->vdpa_dev_id);
229 if (vsocket->dequeue_zero_copy)
230 vhost_enable_dequeue_zero_copy(vid);
232 RTE_LOG(INFO, VHOST_CONFIG, "new device, handle is %d\n", vid);
234 if (vsocket->notify_ops->new_connection) {
235 ret = vsocket->notify_ops->new_connection(vid);
237 RTE_LOG(ERR, VHOST_CONFIG,
238 "failed to add vhost user connection with fd %d\n",
245 conn->vsocket = vsocket;
247 ret = fdset_add(&vhost_user.fdset, fd, vhost_user_read_cb,
250 RTE_LOG(ERR, VHOST_CONFIG,
251 "failed to add fd %d into vhost server fdset\n",
254 if (vsocket->notify_ops->destroy_connection)
255 vsocket->notify_ops->destroy_connection(conn->vid);
260 pthread_mutex_lock(&vsocket->conn_mutex);
261 TAILQ_INSERT_TAIL(&vsocket->conn_list, conn, next);
262 pthread_mutex_unlock(&vsocket->conn_mutex);
264 fdset_pipe_notify(&vhost_user.fdset);
272 /* call back when there is new vhost-user connection from client */
274 vhost_user_server_new_connection(int fd, void *dat, int *remove __rte_unused)
276 struct vhost_user_socket *vsocket = dat;
278 fd = accept(fd, NULL, NULL);
282 RTE_LOG(INFO, VHOST_CONFIG, "new vhost user connection is %d\n", fd);
283 vhost_user_add_connection(fd, vsocket);
287 vhost_user_read_cb(int connfd, void *dat, int *remove)
289 struct vhost_user_connection *conn = dat;
290 struct vhost_user_socket *vsocket = conn->vsocket;
293 ret = vhost_user_msg_handler(conn->vid, connfd);
297 vhost_destroy_device(conn->vid);
299 if (vsocket->notify_ops->destroy_connection)
300 vsocket->notify_ops->destroy_connection(conn->vid);
302 pthread_mutex_lock(&vsocket->conn_mutex);
303 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
304 pthread_mutex_unlock(&vsocket->conn_mutex);
308 if (vsocket->reconnect) {
309 create_unix_socket(vsocket);
310 vhost_user_start_client(vsocket);
316 create_unix_socket(struct vhost_user_socket *vsocket)
319 struct sockaddr_un *un = &vsocket->un;
321 fd = socket(AF_UNIX, SOCK_STREAM, 0);
324 RTE_LOG(INFO, VHOST_CONFIG, "vhost-user %s: socket created, fd: %d\n",
325 vsocket->is_server ? "server" : "client", fd);
327 if (!vsocket->is_server && fcntl(fd, F_SETFL, O_NONBLOCK)) {
328 RTE_LOG(ERR, VHOST_CONFIG,
329 "vhost-user: can't set nonblocking mode for socket, fd: "
330 "%d (%s)\n", fd, strerror(errno));
335 memset(un, 0, sizeof(*un));
336 un->sun_family = AF_UNIX;
337 strncpy(un->sun_path, vsocket->path, sizeof(un->sun_path));
338 un->sun_path[sizeof(un->sun_path) - 1] = '\0';
340 vsocket->socket_fd = fd;
345 vhost_user_start_server(struct vhost_user_socket *vsocket)
348 int fd = vsocket->socket_fd;
349 const char *path = vsocket->path;
352 * bind () may fail if the socket file with the same name already
353 * exists. But the library obviously should not delete the file
354 * provided by the user, since we can not be sure that it is not
355 * being used by other applications. Moreover, many applications form
356 * socket names based on user input, which is prone to errors.
358 * The user must ensure that the socket does not exist before
359 * registering the vhost driver in server mode.
361 ret = bind(fd, (struct sockaddr *)&vsocket->un, sizeof(vsocket->un));
363 RTE_LOG(ERR, VHOST_CONFIG,
364 "failed to bind to %s: %s; remove it and try again\n",
365 path, strerror(errno));
368 RTE_LOG(INFO, VHOST_CONFIG, "bind to %s\n", path);
370 ret = listen(fd, MAX_VIRTIO_BACKLOG);
374 ret = fdset_add(&vhost_user.fdset, fd, vhost_user_server_new_connection,
377 RTE_LOG(ERR, VHOST_CONFIG,
378 "failed to add listen fd %d to vhost server fdset\n",
390 struct vhost_user_reconnect {
391 struct sockaddr_un un;
393 struct vhost_user_socket *vsocket;
395 TAILQ_ENTRY(vhost_user_reconnect) next;
398 TAILQ_HEAD(vhost_user_reconnect_tailq_list, vhost_user_reconnect);
399 struct vhost_user_reconnect_list {
400 struct vhost_user_reconnect_tailq_list head;
401 pthread_mutex_t mutex;
404 static struct vhost_user_reconnect_list reconn_list;
405 static pthread_t reconn_tid;
408 vhost_user_connect_nonblock(int fd, struct sockaddr *un, size_t sz)
412 ret = connect(fd, un, sz);
413 if (ret < 0 && errno != EISCONN)
416 flags = fcntl(fd, F_GETFL, 0);
418 RTE_LOG(ERR, VHOST_CONFIG,
419 "can't get flags for connfd %d\n", fd);
422 if ((flags & O_NONBLOCK) && fcntl(fd, F_SETFL, flags & ~O_NONBLOCK)) {
423 RTE_LOG(ERR, VHOST_CONFIG,
424 "can't disable nonblocking on fd %d\n", fd);
431 vhost_user_client_reconnect(void *arg __rte_unused)
434 struct vhost_user_reconnect *reconn, *next;
437 pthread_mutex_lock(&reconn_list.mutex);
440 * An equal implementation of TAILQ_FOREACH_SAFE,
441 * which does not exist on all platforms.
443 for (reconn = TAILQ_FIRST(&reconn_list.head);
444 reconn != NULL; reconn = next) {
445 next = TAILQ_NEXT(reconn, next);
447 ret = vhost_user_connect_nonblock(reconn->fd,
448 (struct sockaddr *)&reconn->un,
452 RTE_LOG(ERR, VHOST_CONFIG,
453 "reconnection for fd %d failed\n",
460 RTE_LOG(INFO, VHOST_CONFIG,
461 "%s: connected\n", reconn->vsocket->path);
462 vhost_user_add_connection(reconn->fd, reconn->vsocket);
464 TAILQ_REMOVE(&reconn_list.head, reconn, next);
468 pthread_mutex_unlock(&reconn_list.mutex);
476 vhost_user_reconnect_init(void)
480 ret = pthread_mutex_init(&reconn_list.mutex, NULL);
482 RTE_LOG(ERR, VHOST_CONFIG, "failed to initialize mutex");
485 TAILQ_INIT(&reconn_list.head);
487 ret = rte_ctrl_thread_create(&reconn_tid, "vhost_reconn", NULL,
488 vhost_user_client_reconnect, NULL);
490 RTE_LOG(ERR, VHOST_CONFIG, "failed to create reconnect thread");
491 if (pthread_mutex_destroy(&reconn_list.mutex)) {
492 RTE_LOG(ERR, VHOST_CONFIG,
493 "failed to destroy reconnect mutex");
501 vhost_user_start_client(struct vhost_user_socket *vsocket)
504 int fd = vsocket->socket_fd;
505 const char *path = vsocket->path;
506 struct vhost_user_reconnect *reconn;
508 ret = vhost_user_connect_nonblock(fd, (struct sockaddr *)&vsocket->un,
509 sizeof(vsocket->un));
511 vhost_user_add_connection(fd, vsocket);
515 RTE_LOG(WARNING, VHOST_CONFIG,
516 "failed to connect to %s: %s\n",
517 path, strerror(errno));
519 if (ret == -2 || !vsocket->reconnect) {
524 RTE_LOG(INFO, VHOST_CONFIG, "%s: reconnecting...\n", path);
525 reconn = malloc(sizeof(*reconn));
526 if (reconn == NULL) {
527 RTE_LOG(ERR, VHOST_CONFIG,
528 "failed to allocate memory for reconnect\n");
532 reconn->un = vsocket->un;
534 reconn->vsocket = vsocket;
535 pthread_mutex_lock(&reconn_list.mutex);
536 TAILQ_INSERT_TAIL(&reconn_list.head, reconn, next);
537 pthread_mutex_unlock(&reconn_list.mutex);
542 static struct vhost_user_socket *
543 find_vhost_user_socket(const char *path)
547 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
548 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
550 if (!strcmp(vsocket->path, path))
558 rte_vhost_driver_attach_vdpa_device(const char *path, int did)
560 struct vhost_user_socket *vsocket;
562 if (rte_vdpa_get_device(did) == NULL)
565 pthread_mutex_lock(&vhost_user.mutex);
566 vsocket = find_vhost_user_socket(path);
568 vsocket->vdpa_dev_id = did;
569 pthread_mutex_unlock(&vhost_user.mutex);
571 return vsocket ? 0 : -1;
575 rte_vhost_driver_detach_vdpa_device(const char *path)
577 struct vhost_user_socket *vsocket;
579 pthread_mutex_lock(&vhost_user.mutex);
580 vsocket = find_vhost_user_socket(path);
582 vsocket->vdpa_dev_id = -1;
583 pthread_mutex_unlock(&vhost_user.mutex);
585 return vsocket ? 0 : -1;
589 rte_vhost_driver_get_vdpa_device_id(const char *path)
591 struct vhost_user_socket *vsocket;
594 pthread_mutex_lock(&vhost_user.mutex);
595 vsocket = find_vhost_user_socket(path);
597 did = vsocket->vdpa_dev_id;
598 pthread_mutex_unlock(&vhost_user.mutex);
604 rte_vhost_driver_disable_features(const char *path, uint64_t features)
606 struct vhost_user_socket *vsocket;
608 pthread_mutex_lock(&vhost_user.mutex);
609 vsocket = find_vhost_user_socket(path);
611 /* Note that use_builtin_virtio_net is not affected by this function
612 * since callers may want to selectively disable features of the
613 * built-in vhost net device backend.
617 vsocket->features &= ~features;
618 pthread_mutex_unlock(&vhost_user.mutex);
620 return vsocket ? 0 : -1;
624 rte_vhost_driver_enable_features(const char *path, uint64_t features)
626 struct vhost_user_socket *vsocket;
628 pthread_mutex_lock(&vhost_user.mutex);
629 vsocket = find_vhost_user_socket(path);
631 if ((vsocket->supported_features & features) != features) {
633 * trying to enable features the driver doesn't
636 pthread_mutex_unlock(&vhost_user.mutex);
639 vsocket->features |= features;
641 pthread_mutex_unlock(&vhost_user.mutex);
643 return vsocket ? 0 : -1;
647 rte_vhost_driver_set_features(const char *path, uint64_t features)
649 struct vhost_user_socket *vsocket;
651 pthread_mutex_lock(&vhost_user.mutex);
652 vsocket = find_vhost_user_socket(path);
654 vsocket->supported_features = features;
655 vsocket->features = features;
657 /* Anyone setting feature bits is implementing their own vhost
660 vsocket->use_builtin_virtio_net = false;
662 pthread_mutex_unlock(&vhost_user.mutex);
664 return vsocket ? 0 : -1;
668 rte_vhost_driver_get_features(const char *path, uint64_t *features)
670 struct vhost_user_socket *vsocket;
671 uint64_t vdpa_features;
672 struct rte_vdpa_device *vdpa_dev;
676 pthread_mutex_lock(&vhost_user.mutex);
677 vsocket = find_vhost_user_socket(path);
679 RTE_LOG(ERR, VHOST_CONFIG,
680 "socket file %s is not registered yet.\n", path);
685 did = vsocket->vdpa_dev_id;
686 vdpa_dev = rte_vdpa_get_device(did);
687 if (!vdpa_dev || !vdpa_dev->ops->get_features) {
688 *features = vsocket->features;
692 if (vdpa_dev->ops->get_features(did, &vdpa_features) < 0) {
693 RTE_LOG(ERR, VHOST_CONFIG,
694 "failed to get vdpa features "
695 "for socket file %s.\n", path);
700 *features = vsocket->features & vdpa_features;
703 pthread_mutex_unlock(&vhost_user.mutex);
708 rte_vhost_driver_get_protocol_features(const char *path,
709 uint64_t *protocol_features)
711 struct vhost_user_socket *vsocket;
712 uint64_t vdpa_protocol_features;
713 struct rte_vdpa_device *vdpa_dev;
717 pthread_mutex_lock(&vhost_user.mutex);
718 vsocket = find_vhost_user_socket(path);
720 RTE_LOG(ERR, VHOST_CONFIG,
721 "socket file %s is not registered yet.\n", path);
726 did = vsocket->vdpa_dev_id;
727 vdpa_dev = rte_vdpa_get_device(did);
728 if (!vdpa_dev || !vdpa_dev->ops->get_protocol_features) {
729 *protocol_features = VHOST_USER_PROTOCOL_FEATURES;
733 if (vdpa_dev->ops->get_protocol_features(did,
734 &vdpa_protocol_features) < 0) {
735 RTE_LOG(ERR, VHOST_CONFIG,
736 "failed to get vdpa protocol features "
737 "for socket file %s.\n", path);
742 *protocol_features = VHOST_USER_PROTOCOL_FEATURES
743 & vdpa_protocol_features;
746 pthread_mutex_unlock(&vhost_user.mutex);
751 rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num)
753 struct vhost_user_socket *vsocket;
754 uint32_t vdpa_queue_num;
755 struct rte_vdpa_device *vdpa_dev;
759 pthread_mutex_lock(&vhost_user.mutex);
760 vsocket = find_vhost_user_socket(path);
762 RTE_LOG(ERR, VHOST_CONFIG,
763 "socket file %s is not registered yet.\n", path);
768 did = vsocket->vdpa_dev_id;
769 vdpa_dev = rte_vdpa_get_device(did);
770 if (!vdpa_dev || !vdpa_dev->ops->get_queue_num) {
771 *queue_num = VHOST_MAX_QUEUE_PAIRS;
775 if (vdpa_dev->ops->get_queue_num(did, &vdpa_queue_num) < 0) {
776 RTE_LOG(ERR, VHOST_CONFIG,
777 "failed to get vdpa queue number "
778 "for socket file %s.\n", path);
783 *queue_num = RTE_MIN((uint32_t)VHOST_MAX_QUEUE_PAIRS, vdpa_queue_num);
786 pthread_mutex_unlock(&vhost_user.mutex);
791 vhost_user_socket_mem_free(struct vhost_user_socket *vsocket)
793 if (vsocket && vsocket->path) {
795 vsocket->path = NULL;
805 * Register a new vhost-user socket; here we could act as server
806 * (the default case), or client (when RTE_VHOST_USER_CLIENT) flag
810 rte_vhost_driver_register(const char *path, uint64_t flags)
813 struct vhost_user_socket *vsocket;
818 pthread_mutex_lock(&vhost_user.mutex);
820 if (vhost_user.vsocket_cnt == MAX_VHOST_SOCKET) {
821 RTE_LOG(ERR, VHOST_CONFIG,
822 "error: the number of vhost sockets reaches maximum\n");
826 vsocket = malloc(sizeof(struct vhost_user_socket));
829 memset(vsocket, 0, sizeof(struct vhost_user_socket));
830 vsocket->path = strdup(path);
831 if (vsocket->path == NULL) {
832 RTE_LOG(ERR, VHOST_CONFIG,
833 "error: failed to copy socket path string\n");
834 vhost_user_socket_mem_free(vsocket);
837 TAILQ_INIT(&vsocket->conn_list);
838 ret = pthread_mutex_init(&vsocket->conn_mutex, NULL);
840 RTE_LOG(ERR, VHOST_CONFIG,
841 "error: failed to init connection mutex\n");
844 vsocket->dequeue_zero_copy = flags & RTE_VHOST_USER_DEQUEUE_ZERO_COPY;
847 * Set the supported features correctly for the builtin vhost-user
850 * Applications know nothing about features the builtin virtio net
851 * driver (virtio_net.c) supports, thus it's not possible for them
852 * to invoke rte_vhost_driver_set_features(). To workaround it, here
853 * we set it unconditionally. If the application want to implement
854 * another vhost-user driver (say SCSI), it should call the
855 * rte_vhost_driver_set_features(), which will overwrite following
858 vsocket->use_builtin_virtio_net = true;
859 vsocket->supported_features = VIRTIO_NET_SUPPORTED_FEATURES;
860 vsocket->features = VIRTIO_NET_SUPPORTED_FEATURES;
862 /* Dequeue zero copy can't assure descriptors returned in order */
863 if (vsocket->dequeue_zero_copy) {
864 vsocket->supported_features &= ~(1ULL << VIRTIO_F_IN_ORDER);
865 vsocket->features &= ~(1ULL << VIRTIO_F_IN_ORDER);
868 if (!(flags & RTE_VHOST_USER_IOMMU_SUPPORT)) {
869 vsocket->supported_features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
870 vsocket->features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
873 if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
874 vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
875 if (vsocket->reconnect && reconn_tid == 0) {
876 if (vhost_user_reconnect_init() != 0)
880 vsocket->is_server = true;
882 ret = create_unix_socket(vsocket);
887 vhost_user.vsockets[vhost_user.vsocket_cnt++] = vsocket;
889 pthread_mutex_unlock(&vhost_user.mutex);
893 if (pthread_mutex_destroy(&vsocket->conn_mutex)) {
894 RTE_LOG(ERR, VHOST_CONFIG,
895 "error: failed to destroy connection mutex\n");
898 vhost_user_socket_mem_free(vsocket);
900 pthread_mutex_unlock(&vhost_user.mutex);
906 vhost_user_remove_reconnect(struct vhost_user_socket *vsocket)
909 struct vhost_user_reconnect *reconn, *next;
911 pthread_mutex_lock(&reconn_list.mutex);
913 for (reconn = TAILQ_FIRST(&reconn_list.head);
914 reconn != NULL; reconn = next) {
915 next = TAILQ_NEXT(reconn, next);
917 if (reconn->vsocket == vsocket) {
918 TAILQ_REMOVE(&reconn_list.head, reconn, next);
925 pthread_mutex_unlock(&reconn_list.mutex);
930 * Unregister the specified vhost socket
933 rte_vhost_driver_unregister(const char *path)
937 struct vhost_user_connection *conn, *next;
939 pthread_mutex_lock(&vhost_user.mutex);
941 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
942 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
944 if (!strcmp(vsocket->path, path)) {
946 pthread_mutex_lock(&vsocket->conn_mutex);
947 for (conn = TAILQ_FIRST(&vsocket->conn_list);
950 next = TAILQ_NEXT(conn, next);
953 * If r/wcb is executing, release the
954 * conn_mutex lock, and try again since
955 * the r/wcb may use the conn_mutex lock.
957 if (fdset_try_del(&vhost_user.fdset,
958 conn->connfd) == -1) {
959 pthread_mutex_unlock(
960 &vsocket->conn_mutex);
964 RTE_LOG(INFO, VHOST_CONFIG,
965 "free connfd = %d for device '%s'\n",
968 vhost_destroy_device(conn->vid);
969 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
972 pthread_mutex_unlock(&vsocket->conn_mutex);
974 if (vsocket->is_server) {
975 fdset_del(&vhost_user.fdset,
977 close(vsocket->socket_fd);
979 } else if (vsocket->reconnect) {
980 vhost_user_remove_reconnect(vsocket);
983 pthread_mutex_destroy(&vsocket->conn_mutex);
984 vhost_user_socket_mem_free(vsocket);
986 count = --vhost_user.vsocket_cnt;
987 vhost_user.vsockets[i] = vhost_user.vsockets[count];
988 vhost_user.vsockets[count] = NULL;
989 pthread_mutex_unlock(&vhost_user.mutex);
994 pthread_mutex_unlock(&vhost_user.mutex);
1000 * Register ops so that we can add/remove device to data core.
1003 rte_vhost_driver_callback_register(const char *path,
1004 struct vhost_device_ops const * const ops)
1006 struct vhost_user_socket *vsocket;
1008 pthread_mutex_lock(&vhost_user.mutex);
1009 vsocket = find_vhost_user_socket(path);
1011 vsocket->notify_ops = ops;
1012 pthread_mutex_unlock(&vhost_user.mutex);
1014 return vsocket ? 0 : -1;
1017 struct vhost_device_ops const *
1018 vhost_driver_callback_get(const char *path)
1020 struct vhost_user_socket *vsocket;
1022 pthread_mutex_lock(&vhost_user.mutex);
1023 vsocket = find_vhost_user_socket(path);
1024 pthread_mutex_unlock(&vhost_user.mutex);
1026 return vsocket ? vsocket->notify_ops : NULL;
1030 rte_vhost_driver_start(const char *path)
1032 struct vhost_user_socket *vsocket;
1033 static pthread_t fdset_tid;
1035 pthread_mutex_lock(&vhost_user.mutex);
1036 vsocket = find_vhost_user_socket(path);
1037 pthread_mutex_unlock(&vhost_user.mutex);
1042 if (fdset_tid == 0) {
1044 * create a pipe which will be waited by poll and notified to
1045 * rebuild the wait list of poll.
1047 if (fdset_pipe_init(&vhost_user.fdset) < 0) {
1048 RTE_LOG(ERR, VHOST_CONFIG,
1049 "failed to create pipe for vhost fdset\n");
1053 int ret = rte_ctrl_thread_create(&fdset_tid,
1054 "vhost-events", NULL, fdset_event_dispatch,
1057 RTE_LOG(ERR, VHOST_CONFIG,
1058 "failed to create fdset handling thread");
1060 fdset_pipe_uninit(&vhost_user.fdset);
1065 if (vsocket->is_server)
1066 return vhost_user_start_server(vsocket);
1068 return vhost_user_start_client(vsocket);