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)
552 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
553 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
555 if (!strcmp(vsocket->path, path))
563 rte_vhost_driver_attach_vdpa_device(const char *path, int did)
565 struct vhost_user_socket *vsocket;
567 if (rte_vdpa_get_device(did) == NULL)
570 pthread_mutex_lock(&vhost_user.mutex);
571 vsocket = find_vhost_user_socket(path);
573 vsocket->vdpa_dev_id = did;
574 pthread_mutex_unlock(&vhost_user.mutex);
576 return vsocket ? 0 : -1;
580 rte_vhost_driver_detach_vdpa_device(const char *path)
582 struct vhost_user_socket *vsocket;
584 pthread_mutex_lock(&vhost_user.mutex);
585 vsocket = find_vhost_user_socket(path);
587 vsocket->vdpa_dev_id = -1;
588 pthread_mutex_unlock(&vhost_user.mutex);
590 return vsocket ? 0 : -1;
594 rte_vhost_driver_get_vdpa_device_id(const char *path)
596 struct vhost_user_socket *vsocket;
599 pthread_mutex_lock(&vhost_user.mutex);
600 vsocket = find_vhost_user_socket(path);
602 did = vsocket->vdpa_dev_id;
603 pthread_mutex_unlock(&vhost_user.mutex);
609 rte_vhost_driver_disable_features(const char *path, uint64_t features)
611 struct vhost_user_socket *vsocket;
613 pthread_mutex_lock(&vhost_user.mutex);
614 vsocket = find_vhost_user_socket(path);
616 /* Note that use_builtin_virtio_net is not affected by this function
617 * since callers may want to selectively disable features of the
618 * built-in vhost net device backend.
622 vsocket->features &= ~features;
623 pthread_mutex_unlock(&vhost_user.mutex);
625 return vsocket ? 0 : -1;
629 rte_vhost_driver_enable_features(const char *path, uint64_t features)
631 struct vhost_user_socket *vsocket;
633 pthread_mutex_lock(&vhost_user.mutex);
634 vsocket = find_vhost_user_socket(path);
636 if ((vsocket->supported_features & features) != features) {
638 * trying to enable features the driver doesn't
641 pthread_mutex_unlock(&vhost_user.mutex);
644 vsocket->features |= features;
646 pthread_mutex_unlock(&vhost_user.mutex);
648 return vsocket ? 0 : -1;
652 rte_vhost_driver_set_features(const char *path, uint64_t features)
654 struct vhost_user_socket *vsocket;
656 pthread_mutex_lock(&vhost_user.mutex);
657 vsocket = find_vhost_user_socket(path);
659 vsocket->supported_features = features;
660 vsocket->features = features;
662 /* Anyone setting feature bits is implementing their own vhost
665 vsocket->use_builtin_virtio_net = false;
667 pthread_mutex_unlock(&vhost_user.mutex);
669 return vsocket ? 0 : -1;
673 rte_vhost_driver_get_features(const char *path, uint64_t *features)
675 struct vhost_user_socket *vsocket;
676 uint64_t vdpa_features;
677 struct rte_vdpa_device *vdpa_dev;
681 pthread_mutex_lock(&vhost_user.mutex);
682 vsocket = find_vhost_user_socket(path);
684 RTE_LOG(ERR, VHOST_CONFIG,
685 "socket file %s is not registered yet.\n", path);
690 did = vsocket->vdpa_dev_id;
691 vdpa_dev = rte_vdpa_get_device(did);
692 if (!vdpa_dev || !vdpa_dev->ops->get_features) {
693 *features = vsocket->features;
697 if (vdpa_dev->ops->get_features(did, &vdpa_features) < 0) {
698 RTE_LOG(ERR, VHOST_CONFIG,
699 "failed to get vdpa features "
700 "for socket file %s.\n", path);
705 *features = vsocket->features & vdpa_features;
708 pthread_mutex_unlock(&vhost_user.mutex);
713 rte_vhost_driver_set_protocol_features(const char *path,
714 uint64_t protocol_features)
716 struct vhost_user_socket *vsocket;
718 pthread_mutex_lock(&vhost_user.mutex);
719 vsocket = find_vhost_user_socket(path);
721 vsocket->protocol_features = protocol_features;
722 pthread_mutex_unlock(&vhost_user.mutex);
723 return vsocket ? 0 : -1;
727 rte_vhost_driver_get_protocol_features(const char *path,
728 uint64_t *protocol_features)
730 struct vhost_user_socket *vsocket;
731 uint64_t vdpa_protocol_features;
732 struct rte_vdpa_device *vdpa_dev;
736 pthread_mutex_lock(&vhost_user.mutex);
737 vsocket = find_vhost_user_socket(path);
739 RTE_LOG(ERR, VHOST_CONFIG,
740 "socket file %s is not registered yet.\n", path);
745 did = vsocket->vdpa_dev_id;
746 vdpa_dev = rte_vdpa_get_device(did);
747 if (!vdpa_dev || !vdpa_dev->ops->get_protocol_features) {
748 *protocol_features = vsocket->protocol_features;
752 if (vdpa_dev->ops->get_protocol_features(did,
753 &vdpa_protocol_features) < 0) {
754 RTE_LOG(ERR, VHOST_CONFIG,
755 "failed to get vdpa protocol features "
756 "for socket file %s.\n", path);
761 *protocol_features = vsocket->protocol_features
762 & vdpa_protocol_features;
765 pthread_mutex_unlock(&vhost_user.mutex);
770 rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num)
772 struct vhost_user_socket *vsocket;
773 uint32_t vdpa_queue_num;
774 struct rte_vdpa_device *vdpa_dev;
778 pthread_mutex_lock(&vhost_user.mutex);
779 vsocket = find_vhost_user_socket(path);
781 RTE_LOG(ERR, VHOST_CONFIG,
782 "socket file %s is not registered yet.\n", path);
787 did = vsocket->vdpa_dev_id;
788 vdpa_dev = rte_vdpa_get_device(did);
789 if (!vdpa_dev || !vdpa_dev->ops->get_queue_num) {
790 *queue_num = VHOST_MAX_QUEUE_PAIRS;
794 if (vdpa_dev->ops->get_queue_num(did, &vdpa_queue_num) < 0) {
795 RTE_LOG(ERR, VHOST_CONFIG,
796 "failed to get vdpa queue number "
797 "for socket file %s.\n", path);
802 *queue_num = RTE_MIN((uint32_t)VHOST_MAX_QUEUE_PAIRS, vdpa_queue_num);
805 pthread_mutex_unlock(&vhost_user.mutex);
810 vhost_user_socket_mem_free(struct vhost_user_socket *vsocket)
812 if (vsocket && vsocket->path) {
814 vsocket->path = NULL;
824 * Register a new vhost-user socket; here we could act as server
825 * (the default case), or client (when RTE_VHOST_USER_CLIENT) flag
829 rte_vhost_driver_register(const char *path, uint64_t flags)
832 struct vhost_user_socket *vsocket;
837 pthread_mutex_lock(&vhost_user.mutex);
839 if (vhost_user.vsocket_cnt == MAX_VHOST_SOCKET) {
840 RTE_LOG(ERR, VHOST_CONFIG,
841 "error: the number of vhost sockets reaches maximum\n");
845 vsocket = malloc(sizeof(struct vhost_user_socket));
848 memset(vsocket, 0, sizeof(struct vhost_user_socket));
849 vsocket->path = strdup(path);
850 if (vsocket->path == NULL) {
851 RTE_LOG(ERR, VHOST_CONFIG,
852 "error: failed to copy socket path string\n");
853 vhost_user_socket_mem_free(vsocket);
856 TAILQ_INIT(&vsocket->conn_list);
857 ret = pthread_mutex_init(&vsocket->conn_mutex, NULL);
859 RTE_LOG(ERR, VHOST_CONFIG,
860 "error: failed to init connection mutex\n");
863 vsocket->dequeue_zero_copy = flags & RTE_VHOST_USER_DEQUEUE_ZERO_COPY;
866 * Set the supported features correctly for the builtin vhost-user
869 * Applications know nothing about features the builtin virtio net
870 * driver (virtio_net.c) supports, thus it's not possible for them
871 * to invoke rte_vhost_driver_set_features(). To workaround it, here
872 * we set it unconditionally. If the application want to implement
873 * another vhost-user driver (say SCSI), it should call the
874 * rte_vhost_driver_set_features(), which will overwrite following
877 vsocket->use_builtin_virtio_net = true;
878 vsocket->supported_features = VIRTIO_NET_SUPPORTED_FEATURES;
879 vsocket->features = VIRTIO_NET_SUPPORTED_FEATURES;
880 vsocket->protocol_features = VHOST_USER_PROTOCOL_FEATURES;
883 * Dequeue zero copy can't assure descriptors returned in order.
884 * Also, it requires that the guest memory is populated, which is
885 * not compatible with postcopy.
887 if (vsocket->dequeue_zero_copy) {
888 vsocket->supported_features &= ~(1ULL << VIRTIO_F_IN_ORDER);
889 vsocket->features &= ~(1ULL << VIRTIO_F_IN_ORDER);
891 RTE_LOG(INFO, VHOST_CONFIG,
892 "Dequeue zero copy requested, disabling postcopy support\n");
893 vsocket->protocol_features &=
894 ~(1ULL << VHOST_USER_PROTOCOL_F_PAGEFAULT);
897 if (!(flags & RTE_VHOST_USER_IOMMU_SUPPORT)) {
898 vsocket->supported_features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
899 vsocket->features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
902 if (!(flags & RTE_VHOST_USER_POSTCOPY_SUPPORT)) {
903 vsocket->protocol_features &=
904 ~(1ULL << VHOST_USER_PROTOCOL_F_PAGEFAULT);
906 #ifndef RTE_LIBRTE_VHOST_POSTCOPY
907 RTE_LOG(ERR, VHOST_CONFIG,
908 "Postcopy requested but not compiled\n");
914 if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
915 vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
916 if (vsocket->reconnect && reconn_tid == 0) {
917 if (vhost_user_reconnect_init() != 0)
921 vsocket->is_server = true;
923 ret = create_unix_socket(vsocket);
928 vhost_user.vsockets[vhost_user.vsocket_cnt++] = vsocket;
930 pthread_mutex_unlock(&vhost_user.mutex);
934 if (pthread_mutex_destroy(&vsocket->conn_mutex)) {
935 RTE_LOG(ERR, VHOST_CONFIG,
936 "error: failed to destroy connection mutex\n");
939 vhost_user_socket_mem_free(vsocket);
941 pthread_mutex_unlock(&vhost_user.mutex);
947 vhost_user_remove_reconnect(struct vhost_user_socket *vsocket)
950 struct vhost_user_reconnect *reconn, *next;
952 pthread_mutex_lock(&reconn_list.mutex);
954 for (reconn = TAILQ_FIRST(&reconn_list.head);
955 reconn != NULL; reconn = next) {
956 next = TAILQ_NEXT(reconn, next);
958 if (reconn->vsocket == vsocket) {
959 TAILQ_REMOVE(&reconn_list.head, reconn, next);
966 pthread_mutex_unlock(&reconn_list.mutex);
971 * Unregister the specified vhost socket
974 rte_vhost_driver_unregister(const char *path)
978 struct vhost_user_connection *conn, *next;
981 pthread_mutex_lock(&vhost_user.mutex);
983 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
984 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
986 if (!strcmp(vsocket->path, path)) {
987 pthread_mutex_lock(&vsocket->conn_mutex);
988 for (conn = TAILQ_FIRST(&vsocket->conn_list);
991 next = TAILQ_NEXT(conn, next);
994 * If r/wcb is executing, release the
995 * conn_mutex lock, and try again since
996 * the r/wcb may use the conn_mutex lock.
998 if (fdset_try_del(&vhost_user.fdset,
999 conn->connfd) == -1) {
1000 pthread_mutex_unlock(
1001 &vsocket->conn_mutex);
1002 pthread_mutex_unlock(&vhost_user.mutex);
1006 RTE_LOG(INFO, VHOST_CONFIG,
1007 "free connfd = %d for device '%s'\n",
1008 conn->connfd, path);
1009 close(conn->connfd);
1010 vhost_destroy_device(conn->vid);
1011 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
1014 pthread_mutex_unlock(&vsocket->conn_mutex);
1016 if (vsocket->is_server) {
1017 fdset_del(&vhost_user.fdset,
1018 vsocket->socket_fd);
1019 close(vsocket->socket_fd);
1021 } else if (vsocket->reconnect) {
1022 vhost_user_remove_reconnect(vsocket);
1025 pthread_mutex_destroy(&vsocket->conn_mutex);
1026 vhost_user_socket_mem_free(vsocket);
1028 count = --vhost_user.vsocket_cnt;
1029 vhost_user.vsockets[i] = vhost_user.vsockets[count];
1030 vhost_user.vsockets[count] = NULL;
1031 pthread_mutex_unlock(&vhost_user.mutex);
1036 pthread_mutex_unlock(&vhost_user.mutex);
1042 * Register ops so that we can add/remove device to data core.
1045 rte_vhost_driver_callback_register(const char *path,
1046 struct vhost_device_ops const * const ops)
1048 struct vhost_user_socket *vsocket;
1050 pthread_mutex_lock(&vhost_user.mutex);
1051 vsocket = find_vhost_user_socket(path);
1053 vsocket->notify_ops = ops;
1054 pthread_mutex_unlock(&vhost_user.mutex);
1056 return vsocket ? 0 : -1;
1059 struct vhost_device_ops const *
1060 vhost_driver_callback_get(const char *path)
1062 struct vhost_user_socket *vsocket;
1064 pthread_mutex_lock(&vhost_user.mutex);
1065 vsocket = find_vhost_user_socket(path);
1066 pthread_mutex_unlock(&vhost_user.mutex);
1068 return vsocket ? vsocket->notify_ops : NULL;
1072 rte_vhost_driver_start(const char *path)
1074 struct vhost_user_socket *vsocket;
1075 static pthread_t fdset_tid;
1077 pthread_mutex_lock(&vhost_user.mutex);
1078 vsocket = find_vhost_user_socket(path);
1079 pthread_mutex_unlock(&vhost_user.mutex);
1084 if (fdset_tid == 0) {
1086 * create a pipe which will be waited by poll and notified to
1087 * rebuild the wait list of poll.
1089 if (fdset_pipe_init(&vhost_user.fdset) < 0) {
1090 RTE_LOG(ERR, VHOST_CONFIG,
1091 "failed to create pipe for vhost fdset\n");
1095 int ret = rte_ctrl_thread_create(&fdset_tid,
1096 "vhost-events", NULL, fdset_event_dispatch,
1099 RTE_LOG(ERR, VHOST_CONFIG,
1100 "failed to create fdset handling thread");
1102 fdset_pipe_uninit(&vhost_user.fdset);
1107 if (vsocket->is_server)
1108 return vhost_user_start_server(vsocket);
1110 return vhost_user_start_client(vsocket);