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;
54 uint64_t protocol_features;
57 * Device id to identify a specific backend device.
58 * It's set to -1 for the default software implementation.
59 * If valid, one socket can have 1 connection only.
63 struct vhost_device_ops const *notify_ops;
66 struct vhost_user_connection {
67 struct vhost_user_socket *vsocket;
71 TAILQ_ENTRY(vhost_user_connection) next;
74 #define MAX_VHOST_SOCKET 1024
76 struct vhost_user_socket *vsockets[MAX_VHOST_SOCKET];
79 pthread_mutex_t mutex;
82 #define MAX_VIRTIO_BACKLOG 128
84 static void vhost_user_server_new_connection(int fd, void *data, int *remove);
85 static void vhost_user_read_cb(int fd, void *dat, int *remove);
86 static int create_unix_socket(struct vhost_user_socket *vsocket);
87 static int vhost_user_start_client(struct vhost_user_socket *vsocket);
89 static struct vhost_user vhost_user = {
91 .fd = { [0 ... MAX_FDS - 1] = {-1, NULL, NULL, NULL, 0} },
92 .fd_mutex = PTHREAD_MUTEX_INITIALIZER,
93 .fd_pooling_mutex = PTHREAD_MUTEX_INITIALIZER,
97 .mutex = PTHREAD_MUTEX_INITIALIZER,
101 * return bytes# of read on success or negative val on failure. Update fdnum
102 * with number of fds read.
105 read_fd_message(int sockfd, char *buf, int buflen, int *fds, int max_fds,
110 char control[CMSG_SPACE(max_fds * sizeof(int))];
111 struct cmsghdr *cmsg;
117 memset(&msgh, 0, sizeof(msgh));
119 iov.iov_len = buflen;
123 msgh.msg_control = control;
124 msgh.msg_controllen = sizeof(control);
126 ret = recvmsg(sockfd, &msgh, 0);
128 RTE_LOG(ERR, VHOST_CONFIG, "recvmsg failed\n");
132 if (msgh.msg_flags & (MSG_TRUNC | MSG_CTRUNC)) {
133 RTE_LOG(ERR, VHOST_CONFIG, "truncted msg\n");
137 for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
138 cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
139 if ((cmsg->cmsg_level == SOL_SOCKET) &&
140 (cmsg->cmsg_type == SCM_RIGHTS)) {
141 got_fds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
143 memcpy(fds, CMSG_DATA(cmsg), got_fds * sizeof(int));
148 /* Clear out unused file descriptors */
149 while (got_fds < max_fds)
156 send_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
161 size_t fdsize = fd_num * sizeof(int);
162 char control[CMSG_SPACE(fdsize)];
163 struct cmsghdr *cmsg;
166 memset(&msgh, 0, sizeof(msgh));
168 iov.iov_len = buflen;
173 if (fds && fd_num > 0) {
174 msgh.msg_control = control;
175 msgh.msg_controllen = sizeof(control);
176 cmsg = CMSG_FIRSTHDR(&msgh);
178 RTE_LOG(ERR, VHOST_CONFIG, "cmsg == NULL\n");
182 cmsg->cmsg_len = CMSG_LEN(fdsize);
183 cmsg->cmsg_level = SOL_SOCKET;
184 cmsg->cmsg_type = SCM_RIGHTS;
185 memcpy(CMSG_DATA(cmsg), fds, fdsize);
187 msgh.msg_control = NULL;
188 msgh.msg_controllen = 0;
192 ret = sendmsg(sockfd, &msgh, MSG_NOSIGNAL);
193 } while (ret < 0 && errno == EINTR);
196 RTE_LOG(ERR, VHOST_CONFIG, "sendmsg error\n");
204 vhost_user_add_connection(int fd, struct vhost_user_socket *vsocket)
208 struct vhost_user_connection *conn;
214 conn = malloc(sizeof(*conn));
220 vid = vhost_new_device();
225 size = strnlen(vsocket->path, PATH_MAX);
226 vhost_set_ifname(vid, vsocket->path, size);
228 vhost_set_builtin_virtio_net(vid, vsocket->use_builtin_virtio_net);
230 vhost_attach_vdpa_device(vid, vsocket->vdpa_dev_id);
232 if (vsocket->dequeue_zero_copy)
233 vhost_enable_dequeue_zero_copy(vid);
235 RTE_LOG(INFO, VHOST_CONFIG, "new device, handle is %d\n", vid);
237 if (vsocket->notify_ops->new_connection) {
238 ret = vsocket->notify_ops->new_connection(vid);
240 RTE_LOG(ERR, VHOST_CONFIG,
241 "failed to add vhost user connection with fd %d\n",
248 conn->vsocket = vsocket;
250 ret = fdset_add(&vhost_user.fdset, fd, vhost_user_read_cb,
253 RTE_LOG(ERR, VHOST_CONFIG,
254 "failed to add fd %d into vhost server fdset\n",
257 if (vsocket->notify_ops->destroy_connection)
258 vsocket->notify_ops->destroy_connection(conn->vid);
263 pthread_mutex_lock(&vsocket->conn_mutex);
264 TAILQ_INSERT_TAIL(&vsocket->conn_list, conn, next);
265 pthread_mutex_unlock(&vsocket->conn_mutex);
267 fdset_pipe_notify(&vhost_user.fdset);
271 vhost_destroy_device(vid);
277 /* call back when there is new vhost-user connection from client */
279 vhost_user_server_new_connection(int fd, void *dat, int *remove __rte_unused)
281 struct vhost_user_socket *vsocket = dat;
283 fd = accept(fd, NULL, NULL);
287 RTE_LOG(INFO, VHOST_CONFIG, "new vhost user connection is %d\n", fd);
288 vhost_user_add_connection(fd, vsocket);
292 vhost_user_read_cb(int connfd, void *dat, int *remove)
294 struct vhost_user_connection *conn = dat;
295 struct vhost_user_socket *vsocket = conn->vsocket;
298 ret = vhost_user_msg_handler(conn->vid, connfd);
302 vhost_destroy_device(conn->vid);
304 if (vsocket->notify_ops->destroy_connection)
305 vsocket->notify_ops->destroy_connection(conn->vid);
307 pthread_mutex_lock(&vsocket->conn_mutex);
308 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
309 pthread_mutex_unlock(&vsocket->conn_mutex);
313 if (vsocket->reconnect) {
314 create_unix_socket(vsocket);
315 vhost_user_start_client(vsocket);
321 create_unix_socket(struct vhost_user_socket *vsocket)
324 struct sockaddr_un *un = &vsocket->un;
326 fd = socket(AF_UNIX, SOCK_STREAM, 0);
329 RTE_LOG(INFO, VHOST_CONFIG, "vhost-user %s: socket created, fd: %d\n",
330 vsocket->is_server ? "server" : "client", fd);
332 if (!vsocket->is_server && fcntl(fd, F_SETFL, O_NONBLOCK)) {
333 RTE_LOG(ERR, VHOST_CONFIG,
334 "vhost-user: can't set nonblocking mode for socket, fd: "
335 "%d (%s)\n", fd, strerror(errno));
340 memset(un, 0, sizeof(*un));
341 un->sun_family = AF_UNIX;
342 strncpy(un->sun_path, vsocket->path, sizeof(un->sun_path));
343 un->sun_path[sizeof(un->sun_path) - 1] = '\0';
345 vsocket->socket_fd = fd;
350 vhost_user_start_server(struct vhost_user_socket *vsocket)
353 int fd = vsocket->socket_fd;
354 const char *path = vsocket->path;
357 * bind () may fail if the socket file with the same name already
358 * exists. But the library obviously should not delete the file
359 * provided by the user, since we can not be sure that it is not
360 * being used by other applications. Moreover, many applications form
361 * socket names based on user input, which is prone to errors.
363 * The user must ensure that the socket does not exist before
364 * registering the vhost driver in server mode.
366 ret = bind(fd, (struct sockaddr *)&vsocket->un, sizeof(vsocket->un));
368 RTE_LOG(ERR, VHOST_CONFIG,
369 "failed to bind to %s: %s; remove it and try again\n",
370 path, strerror(errno));
373 RTE_LOG(INFO, VHOST_CONFIG, "bind to %s\n", path);
375 ret = listen(fd, MAX_VIRTIO_BACKLOG);
379 ret = fdset_add(&vhost_user.fdset, fd, vhost_user_server_new_connection,
382 RTE_LOG(ERR, VHOST_CONFIG,
383 "failed to add listen fd %d to vhost server fdset\n",
395 struct vhost_user_reconnect {
396 struct sockaddr_un un;
398 struct vhost_user_socket *vsocket;
400 TAILQ_ENTRY(vhost_user_reconnect) next;
403 TAILQ_HEAD(vhost_user_reconnect_tailq_list, vhost_user_reconnect);
404 struct vhost_user_reconnect_list {
405 struct vhost_user_reconnect_tailq_list head;
406 pthread_mutex_t mutex;
409 static struct vhost_user_reconnect_list reconn_list;
410 static pthread_t reconn_tid;
413 vhost_user_connect_nonblock(int fd, struct sockaddr *un, size_t sz)
417 ret = connect(fd, un, sz);
418 if (ret < 0 && errno != EISCONN)
421 flags = fcntl(fd, F_GETFL, 0);
423 RTE_LOG(ERR, VHOST_CONFIG,
424 "can't get flags for connfd %d\n", fd);
427 if ((flags & O_NONBLOCK) && fcntl(fd, F_SETFL, flags & ~O_NONBLOCK)) {
428 RTE_LOG(ERR, VHOST_CONFIG,
429 "can't disable nonblocking on fd %d\n", fd);
436 vhost_user_client_reconnect(void *arg __rte_unused)
439 struct vhost_user_reconnect *reconn, *next;
442 pthread_mutex_lock(&reconn_list.mutex);
445 * An equal implementation of TAILQ_FOREACH_SAFE,
446 * which does not exist on all platforms.
448 for (reconn = TAILQ_FIRST(&reconn_list.head);
449 reconn != NULL; reconn = next) {
450 next = TAILQ_NEXT(reconn, next);
452 ret = vhost_user_connect_nonblock(reconn->fd,
453 (struct sockaddr *)&reconn->un,
457 RTE_LOG(ERR, VHOST_CONFIG,
458 "reconnection for fd %d failed\n",
465 RTE_LOG(INFO, VHOST_CONFIG,
466 "%s: connected\n", reconn->vsocket->path);
467 vhost_user_add_connection(reconn->fd, reconn->vsocket);
469 TAILQ_REMOVE(&reconn_list.head, reconn, next);
473 pthread_mutex_unlock(&reconn_list.mutex);
481 vhost_user_reconnect_init(void)
485 ret = pthread_mutex_init(&reconn_list.mutex, NULL);
487 RTE_LOG(ERR, VHOST_CONFIG, "failed to initialize mutex");
490 TAILQ_INIT(&reconn_list.head);
492 ret = rte_ctrl_thread_create(&reconn_tid, "vhost_reconn", NULL,
493 vhost_user_client_reconnect, NULL);
495 RTE_LOG(ERR, VHOST_CONFIG, "failed to create reconnect thread");
496 if (pthread_mutex_destroy(&reconn_list.mutex)) {
497 RTE_LOG(ERR, VHOST_CONFIG,
498 "failed to destroy reconnect mutex");
506 vhost_user_start_client(struct vhost_user_socket *vsocket)
509 int fd = vsocket->socket_fd;
510 const char *path = vsocket->path;
511 struct vhost_user_reconnect *reconn;
513 ret = vhost_user_connect_nonblock(fd, (struct sockaddr *)&vsocket->un,
514 sizeof(vsocket->un));
516 vhost_user_add_connection(fd, vsocket);
520 RTE_LOG(WARNING, VHOST_CONFIG,
521 "failed to connect to %s: %s\n",
522 path, strerror(errno));
524 if (ret == -2 || !vsocket->reconnect) {
529 RTE_LOG(INFO, VHOST_CONFIG, "%s: reconnecting...\n", path);
530 reconn = malloc(sizeof(*reconn));
531 if (reconn == NULL) {
532 RTE_LOG(ERR, VHOST_CONFIG,
533 "failed to allocate memory for reconnect\n");
537 reconn->un = vsocket->un;
539 reconn->vsocket = vsocket;
540 pthread_mutex_lock(&reconn_list.mutex);
541 TAILQ_INSERT_TAIL(&reconn_list.head, reconn, next);
542 pthread_mutex_unlock(&reconn_list.mutex);
547 static struct vhost_user_socket *
548 find_vhost_user_socket(const char *path)
555 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
556 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
558 if (!strcmp(vsocket->path, path))
566 rte_vhost_driver_attach_vdpa_device(const char *path, int did)
568 struct vhost_user_socket *vsocket;
570 if (rte_vdpa_get_device(did) == NULL || path == NULL)
573 pthread_mutex_lock(&vhost_user.mutex);
574 vsocket = find_vhost_user_socket(path);
576 vsocket->vdpa_dev_id = did;
577 pthread_mutex_unlock(&vhost_user.mutex);
579 return vsocket ? 0 : -1;
583 rte_vhost_driver_detach_vdpa_device(const char *path)
585 struct vhost_user_socket *vsocket;
587 pthread_mutex_lock(&vhost_user.mutex);
588 vsocket = find_vhost_user_socket(path);
590 vsocket->vdpa_dev_id = -1;
591 pthread_mutex_unlock(&vhost_user.mutex);
593 return vsocket ? 0 : -1;
597 rte_vhost_driver_get_vdpa_device_id(const char *path)
599 struct vhost_user_socket *vsocket;
602 pthread_mutex_lock(&vhost_user.mutex);
603 vsocket = find_vhost_user_socket(path);
605 did = vsocket->vdpa_dev_id;
606 pthread_mutex_unlock(&vhost_user.mutex);
612 rte_vhost_driver_disable_features(const char *path, uint64_t features)
614 struct vhost_user_socket *vsocket;
616 pthread_mutex_lock(&vhost_user.mutex);
617 vsocket = find_vhost_user_socket(path);
619 /* Note that use_builtin_virtio_net is not affected by this function
620 * since callers may want to selectively disable features of the
621 * built-in vhost net device backend.
625 vsocket->features &= ~features;
626 pthread_mutex_unlock(&vhost_user.mutex);
628 return vsocket ? 0 : -1;
632 rte_vhost_driver_enable_features(const char *path, uint64_t features)
634 struct vhost_user_socket *vsocket;
636 pthread_mutex_lock(&vhost_user.mutex);
637 vsocket = find_vhost_user_socket(path);
639 if ((vsocket->supported_features & features) != features) {
641 * trying to enable features the driver doesn't
644 pthread_mutex_unlock(&vhost_user.mutex);
647 vsocket->features |= features;
649 pthread_mutex_unlock(&vhost_user.mutex);
651 return vsocket ? 0 : -1;
655 rte_vhost_driver_set_features(const char *path, uint64_t features)
657 struct vhost_user_socket *vsocket;
659 pthread_mutex_lock(&vhost_user.mutex);
660 vsocket = find_vhost_user_socket(path);
662 vsocket->supported_features = features;
663 vsocket->features = features;
665 /* Anyone setting feature bits is implementing their own vhost
668 vsocket->use_builtin_virtio_net = false;
670 pthread_mutex_unlock(&vhost_user.mutex);
672 return vsocket ? 0 : -1;
676 rte_vhost_driver_get_features(const char *path, uint64_t *features)
678 struct vhost_user_socket *vsocket;
679 uint64_t vdpa_features;
680 struct rte_vdpa_device *vdpa_dev;
684 pthread_mutex_lock(&vhost_user.mutex);
685 vsocket = find_vhost_user_socket(path);
687 RTE_LOG(ERR, VHOST_CONFIG,
688 "socket file %s is not registered yet.\n", path);
693 did = vsocket->vdpa_dev_id;
694 vdpa_dev = rte_vdpa_get_device(did);
695 if (!vdpa_dev || !vdpa_dev->ops->get_features) {
696 *features = vsocket->features;
700 if (vdpa_dev->ops->get_features(did, &vdpa_features) < 0) {
701 RTE_LOG(ERR, VHOST_CONFIG,
702 "failed to get vdpa features "
703 "for socket file %s.\n", path);
708 *features = vsocket->features & vdpa_features;
711 pthread_mutex_unlock(&vhost_user.mutex);
716 rte_vhost_driver_set_protocol_features(const char *path,
717 uint64_t protocol_features)
719 struct vhost_user_socket *vsocket;
721 pthread_mutex_lock(&vhost_user.mutex);
722 vsocket = find_vhost_user_socket(path);
724 vsocket->protocol_features = protocol_features;
725 pthread_mutex_unlock(&vhost_user.mutex);
726 return vsocket ? 0 : -1;
730 rte_vhost_driver_get_protocol_features(const char *path,
731 uint64_t *protocol_features)
733 struct vhost_user_socket *vsocket;
734 uint64_t vdpa_protocol_features;
735 struct rte_vdpa_device *vdpa_dev;
739 pthread_mutex_lock(&vhost_user.mutex);
740 vsocket = find_vhost_user_socket(path);
742 RTE_LOG(ERR, VHOST_CONFIG,
743 "socket file %s is not registered yet.\n", path);
748 did = vsocket->vdpa_dev_id;
749 vdpa_dev = rte_vdpa_get_device(did);
750 if (!vdpa_dev || !vdpa_dev->ops->get_protocol_features) {
751 *protocol_features = vsocket->protocol_features;
755 if (vdpa_dev->ops->get_protocol_features(did,
756 &vdpa_protocol_features) < 0) {
757 RTE_LOG(ERR, VHOST_CONFIG,
758 "failed to get vdpa protocol features "
759 "for socket file %s.\n", path);
764 *protocol_features = vsocket->protocol_features
765 & vdpa_protocol_features;
768 pthread_mutex_unlock(&vhost_user.mutex);
773 rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num)
775 struct vhost_user_socket *vsocket;
776 uint32_t vdpa_queue_num;
777 struct rte_vdpa_device *vdpa_dev;
781 pthread_mutex_lock(&vhost_user.mutex);
782 vsocket = find_vhost_user_socket(path);
784 RTE_LOG(ERR, VHOST_CONFIG,
785 "socket file %s is not registered yet.\n", path);
790 did = vsocket->vdpa_dev_id;
791 vdpa_dev = rte_vdpa_get_device(did);
792 if (!vdpa_dev || !vdpa_dev->ops->get_queue_num) {
793 *queue_num = VHOST_MAX_QUEUE_PAIRS;
797 if (vdpa_dev->ops->get_queue_num(did, &vdpa_queue_num) < 0) {
798 RTE_LOG(ERR, VHOST_CONFIG,
799 "failed to get vdpa queue number "
800 "for socket file %s.\n", path);
805 *queue_num = RTE_MIN((uint32_t)VHOST_MAX_QUEUE_PAIRS, vdpa_queue_num);
808 pthread_mutex_unlock(&vhost_user.mutex);
813 vhost_user_socket_mem_free(struct vhost_user_socket *vsocket)
815 if (vsocket && vsocket->path) {
817 vsocket->path = NULL;
827 * Register a new vhost-user socket; here we could act as server
828 * (the default case), or client (when RTE_VHOST_USER_CLIENT) flag
832 rte_vhost_driver_register(const char *path, uint64_t flags)
835 struct vhost_user_socket *vsocket;
840 pthread_mutex_lock(&vhost_user.mutex);
842 if (vhost_user.vsocket_cnt == MAX_VHOST_SOCKET) {
843 RTE_LOG(ERR, VHOST_CONFIG,
844 "error: the number of vhost sockets reaches maximum\n");
848 vsocket = malloc(sizeof(struct vhost_user_socket));
851 memset(vsocket, 0, sizeof(struct vhost_user_socket));
852 vsocket->path = strdup(path);
853 if (vsocket->path == NULL) {
854 RTE_LOG(ERR, VHOST_CONFIG,
855 "error: failed to copy socket path string\n");
856 vhost_user_socket_mem_free(vsocket);
859 TAILQ_INIT(&vsocket->conn_list);
860 ret = pthread_mutex_init(&vsocket->conn_mutex, NULL);
862 RTE_LOG(ERR, VHOST_CONFIG,
863 "error: failed to init connection mutex\n");
866 vsocket->dequeue_zero_copy = flags & RTE_VHOST_USER_DEQUEUE_ZERO_COPY;
869 * Set the supported features correctly for the builtin vhost-user
872 * Applications know nothing about features the builtin virtio net
873 * driver (virtio_net.c) supports, thus it's not possible for them
874 * to invoke rte_vhost_driver_set_features(). To workaround it, here
875 * we set it unconditionally. If the application want to implement
876 * another vhost-user driver (say SCSI), it should call the
877 * rte_vhost_driver_set_features(), which will overwrite following
880 vsocket->use_builtin_virtio_net = true;
881 vsocket->supported_features = VIRTIO_NET_SUPPORTED_FEATURES;
882 vsocket->features = VIRTIO_NET_SUPPORTED_FEATURES;
883 vsocket->protocol_features = VHOST_USER_PROTOCOL_FEATURES;
886 * Dequeue zero copy can't assure descriptors returned in order.
887 * Also, it requires that the guest memory is populated, which is
888 * not compatible with postcopy.
890 if (vsocket->dequeue_zero_copy) {
891 vsocket->supported_features &= ~(1ULL << VIRTIO_F_IN_ORDER);
892 vsocket->features &= ~(1ULL << VIRTIO_F_IN_ORDER);
894 RTE_LOG(INFO, VHOST_CONFIG,
895 "Dequeue zero copy requested, disabling postcopy support\n");
896 vsocket->protocol_features &=
897 ~(1ULL << VHOST_USER_PROTOCOL_F_PAGEFAULT);
900 if (!(flags & RTE_VHOST_USER_IOMMU_SUPPORT)) {
901 vsocket->supported_features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
902 vsocket->features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
905 if (!(flags & RTE_VHOST_USER_POSTCOPY_SUPPORT)) {
906 vsocket->protocol_features &=
907 ~(1ULL << VHOST_USER_PROTOCOL_F_PAGEFAULT);
909 #ifndef RTE_LIBRTE_VHOST_POSTCOPY
910 RTE_LOG(ERR, VHOST_CONFIG,
911 "Postcopy requested but not compiled\n");
917 if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
918 vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
919 if (vsocket->reconnect && reconn_tid == 0) {
920 if (vhost_user_reconnect_init() != 0)
924 vsocket->is_server = true;
926 ret = create_unix_socket(vsocket);
931 vhost_user.vsockets[vhost_user.vsocket_cnt++] = vsocket;
933 pthread_mutex_unlock(&vhost_user.mutex);
937 if (pthread_mutex_destroy(&vsocket->conn_mutex)) {
938 RTE_LOG(ERR, VHOST_CONFIG,
939 "error: failed to destroy connection mutex\n");
942 vhost_user_socket_mem_free(vsocket);
944 pthread_mutex_unlock(&vhost_user.mutex);
950 vhost_user_remove_reconnect(struct vhost_user_socket *vsocket)
953 struct vhost_user_reconnect *reconn, *next;
955 pthread_mutex_lock(&reconn_list.mutex);
957 for (reconn = TAILQ_FIRST(&reconn_list.head);
958 reconn != NULL; reconn = next) {
959 next = TAILQ_NEXT(reconn, next);
961 if (reconn->vsocket == vsocket) {
962 TAILQ_REMOVE(&reconn_list.head, reconn, next);
969 pthread_mutex_unlock(&reconn_list.mutex);
974 * Unregister the specified vhost socket
977 rte_vhost_driver_unregister(const char *path)
981 struct vhost_user_connection *conn, *next;
987 pthread_mutex_lock(&vhost_user.mutex);
989 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
990 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
992 if (!strcmp(vsocket->path, path)) {
993 pthread_mutex_lock(&vsocket->conn_mutex);
994 for (conn = TAILQ_FIRST(&vsocket->conn_list);
997 next = TAILQ_NEXT(conn, next);
1000 * If r/wcb is executing, release the
1001 * conn_mutex lock, and try again since
1002 * the r/wcb may use the conn_mutex lock.
1004 if (fdset_try_del(&vhost_user.fdset,
1005 conn->connfd) == -1) {
1006 pthread_mutex_unlock(
1007 &vsocket->conn_mutex);
1008 pthread_mutex_unlock(&vhost_user.mutex);
1012 RTE_LOG(INFO, VHOST_CONFIG,
1013 "free connfd = %d for device '%s'\n",
1014 conn->connfd, path);
1015 close(conn->connfd);
1016 vhost_destroy_device(conn->vid);
1017 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
1020 pthread_mutex_unlock(&vsocket->conn_mutex);
1022 if (vsocket->is_server) {
1023 fdset_del(&vhost_user.fdset,
1024 vsocket->socket_fd);
1025 close(vsocket->socket_fd);
1027 } else if (vsocket->reconnect) {
1028 vhost_user_remove_reconnect(vsocket);
1031 pthread_mutex_destroy(&vsocket->conn_mutex);
1032 vhost_user_socket_mem_free(vsocket);
1034 count = --vhost_user.vsocket_cnt;
1035 vhost_user.vsockets[i] = vhost_user.vsockets[count];
1036 vhost_user.vsockets[count] = NULL;
1037 pthread_mutex_unlock(&vhost_user.mutex);
1042 pthread_mutex_unlock(&vhost_user.mutex);
1048 * Register ops so that we can add/remove device to data core.
1051 rte_vhost_driver_callback_register(const char *path,
1052 struct vhost_device_ops const * const ops)
1054 struct vhost_user_socket *vsocket;
1056 pthread_mutex_lock(&vhost_user.mutex);
1057 vsocket = find_vhost_user_socket(path);
1059 vsocket->notify_ops = ops;
1060 pthread_mutex_unlock(&vhost_user.mutex);
1062 return vsocket ? 0 : -1;
1065 struct vhost_device_ops const *
1066 vhost_driver_callback_get(const char *path)
1068 struct vhost_user_socket *vsocket;
1070 pthread_mutex_lock(&vhost_user.mutex);
1071 vsocket = find_vhost_user_socket(path);
1072 pthread_mutex_unlock(&vhost_user.mutex);
1074 return vsocket ? vsocket->notify_ops : NULL;
1078 rte_vhost_driver_start(const char *path)
1080 struct vhost_user_socket *vsocket;
1081 static pthread_t fdset_tid;
1083 pthread_mutex_lock(&vhost_user.mutex);
1084 vsocket = find_vhost_user_socket(path);
1085 pthread_mutex_unlock(&vhost_user.mutex);
1090 if (fdset_tid == 0) {
1092 * create a pipe which will be waited by poll and notified to
1093 * rebuild the wait list of poll.
1095 if (fdset_pipe_init(&vhost_user.fdset) < 0) {
1096 RTE_LOG(ERR, VHOST_CONFIG,
1097 "failed to create pipe for vhost fdset\n");
1101 int ret = rte_ctrl_thread_create(&fdset_tid,
1102 "vhost-events", NULL, fdset_event_dispatch,
1105 RTE_LOG(ERR, VHOST_CONFIG,
1106 "failed to create fdset handling thread");
1108 fdset_pipe_uninit(&vhost_user.fdset);
1113 if (vsocket->is_server)
1114 return vhost_user_start_server(vsocket);
1116 return vhost_user_start_client(vsocket);