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;
41 bool use_builtin_virtio_net;
47 * The "supported_features" indicates the feature bits the
48 * vhost driver supports. The "features" indicates the feature
49 * bits after the rte_vhost_driver_features_disable/enable().
50 * It is also the final feature bits used for vhost-user
51 * features negotiation.
53 uint64_t supported_features;
56 uint64_t protocol_features;
58 struct rte_vdpa_device *vdpa_dev;
60 struct vhost_device_ops const *notify_ops;
63 struct vhost_user_connection {
64 struct vhost_user_socket *vsocket;
68 TAILQ_ENTRY(vhost_user_connection) next;
71 #define MAX_VHOST_SOCKET 1024
73 struct vhost_user_socket *vsockets[MAX_VHOST_SOCKET];
76 pthread_mutex_t mutex;
79 #define MAX_VIRTIO_BACKLOG 128
81 static void vhost_user_server_new_connection(int fd, void *data, int *remove);
82 static void vhost_user_read_cb(int fd, void *dat, int *remove);
83 static int create_unix_socket(struct vhost_user_socket *vsocket);
84 static int vhost_user_start_client(struct vhost_user_socket *vsocket);
86 static struct vhost_user vhost_user = {
88 .fd = { [0 ... MAX_FDS - 1] = {-1, NULL, NULL, NULL, 0} },
89 .fd_mutex = PTHREAD_MUTEX_INITIALIZER,
90 .fd_pooling_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);
126 VHOST_LOG_CONFIG(ERR, "recvmsg failed\n");
130 if (msgh.msg_flags & (MSG_TRUNC | MSG_CTRUNC)) {
131 VHOST_LOG_CONFIG(ERR, "truncated msg\n");
135 for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
136 cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
137 if ((cmsg->cmsg_level == SOL_SOCKET) &&
138 (cmsg->cmsg_type == SCM_RIGHTS)) {
139 got_fds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
141 memcpy(fds, CMSG_DATA(cmsg), got_fds * sizeof(int));
146 /* Clear out unused file descriptors */
147 while (got_fds < max_fds)
154 send_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
159 size_t fdsize = fd_num * sizeof(int);
160 char control[CMSG_SPACE(fdsize)];
161 struct cmsghdr *cmsg;
164 memset(&msgh, 0, sizeof(msgh));
166 iov.iov_len = buflen;
171 if (fds && fd_num > 0) {
172 msgh.msg_control = control;
173 msgh.msg_controllen = sizeof(control);
174 cmsg = CMSG_FIRSTHDR(&msgh);
176 VHOST_LOG_CONFIG(ERR, "cmsg == NULL\n");
180 cmsg->cmsg_len = CMSG_LEN(fdsize);
181 cmsg->cmsg_level = SOL_SOCKET;
182 cmsg->cmsg_type = SCM_RIGHTS;
183 memcpy(CMSG_DATA(cmsg), fds, fdsize);
185 msgh.msg_control = NULL;
186 msgh.msg_controllen = 0;
190 ret = sendmsg(sockfd, &msgh, MSG_NOSIGNAL);
191 } while (ret < 0 && errno == EINTR);
194 VHOST_LOG_CONFIG(ERR, "sendmsg error\n");
202 vhost_user_add_connection(int fd, struct vhost_user_socket *vsocket)
206 struct vhost_user_connection *conn;
208 struct virtio_net *dev;
213 conn = malloc(sizeof(*conn));
219 vid = vhost_new_device();
224 size = strnlen(vsocket->path, PATH_MAX);
225 vhost_set_ifname(vid, vsocket->path, size);
227 vhost_set_builtin_virtio_net(vid, vsocket->use_builtin_virtio_net);
229 vhost_attach_vdpa_device(vid, vsocket->vdpa_dev);
232 vhost_enable_extbuf(vid);
234 if (vsocket->linearbuf)
235 vhost_enable_linearbuf(vid);
237 if (vsocket->async_copy) {
238 dev = get_device(vid);
244 VHOST_LOG_CONFIG(INFO, "new device, handle is %d\n", vid);
246 if (vsocket->notify_ops->new_connection) {
247 ret = vsocket->notify_ops->new_connection(vid);
249 VHOST_LOG_CONFIG(ERR,
250 "failed to add vhost user connection with fd %d\n",
257 conn->vsocket = vsocket;
259 ret = fdset_add(&vhost_user.fdset, fd, vhost_user_read_cb,
262 VHOST_LOG_CONFIG(ERR,
263 "failed to add fd %d into vhost server fdset\n",
266 if (vsocket->notify_ops->destroy_connection)
267 vsocket->notify_ops->destroy_connection(conn->vid);
272 pthread_mutex_lock(&vsocket->conn_mutex);
273 TAILQ_INSERT_TAIL(&vsocket->conn_list, conn, next);
274 pthread_mutex_unlock(&vsocket->conn_mutex);
276 fdset_pipe_notify(&vhost_user.fdset);
280 vhost_destroy_device(vid);
286 /* call back when there is new vhost-user connection from client */
288 vhost_user_server_new_connection(int fd, void *dat, int *remove __rte_unused)
290 struct vhost_user_socket *vsocket = dat;
292 fd = accept(fd, NULL, NULL);
296 VHOST_LOG_CONFIG(INFO, "new vhost user connection is %d\n", fd);
297 vhost_user_add_connection(fd, vsocket);
301 vhost_user_read_cb(int connfd, void *dat, int *remove)
303 struct vhost_user_connection *conn = dat;
304 struct vhost_user_socket *vsocket = conn->vsocket;
307 ret = vhost_user_msg_handler(conn->vid, connfd);
309 struct virtio_net *dev = get_device(conn->vid);
315 vhost_destroy_device_notify(dev);
317 if (vsocket->notify_ops->destroy_connection)
318 vsocket->notify_ops->destroy_connection(conn->vid);
320 vhost_destroy_device(conn->vid);
322 if (vsocket->reconnect) {
323 create_unix_socket(vsocket);
324 vhost_user_start_client(vsocket);
327 pthread_mutex_lock(&vsocket->conn_mutex);
328 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
329 pthread_mutex_unlock(&vsocket->conn_mutex);
336 create_unix_socket(struct vhost_user_socket *vsocket)
339 struct sockaddr_un *un = &vsocket->un;
341 fd = socket(AF_UNIX, SOCK_STREAM, 0);
344 VHOST_LOG_CONFIG(INFO, "vhost-user %s: socket created, fd: %d\n",
345 vsocket->is_server ? "server" : "client", fd);
347 if (!vsocket->is_server && fcntl(fd, F_SETFL, O_NONBLOCK)) {
348 VHOST_LOG_CONFIG(ERR,
349 "vhost-user: can't set nonblocking mode for socket, fd: "
350 "%d (%s)\n", fd, strerror(errno));
355 memset(un, 0, sizeof(*un));
356 un->sun_family = AF_UNIX;
357 strncpy(un->sun_path, vsocket->path, sizeof(un->sun_path));
358 un->sun_path[sizeof(un->sun_path) - 1] = '\0';
360 vsocket->socket_fd = fd;
365 vhost_user_start_server(struct vhost_user_socket *vsocket)
368 int fd = vsocket->socket_fd;
369 const char *path = vsocket->path;
372 * bind () may fail if the socket file with the same name already
373 * exists. But the library obviously should not delete the file
374 * provided by the user, since we can not be sure that it is not
375 * being used by other applications. Moreover, many applications form
376 * socket names based on user input, which is prone to errors.
378 * The user must ensure that the socket does not exist before
379 * registering the vhost driver in server mode.
381 ret = bind(fd, (struct sockaddr *)&vsocket->un, sizeof(vsocket->un));
383 VHOST_LOG_CONFIG(ERR,
384 "failed to bind to %s: %s; remove it and try again\n",
385 path, strerror(errno));
388 VHOST_LOG_CONFIG(INFO, "bind to %s\n", path);
390 ret = listen(fd, MAX_VIRTIO_BACKLOG);
394 ret = fdset_add(&vhost_user.fdset, fd, vhost_user_server_new_connection,
397 VHOST_LOG_CONFIG(ERR,
398 "failed to add listen fd %d to vhost server fdset\n",
410 struct vhost_user_reconnect {
411 struct sockaddr_un un;
413 struct vhost_user_socket *vsocket;
415 TAILQ_ENTRY(vhost_user_reconnect) next;
418 TAILQ_HEAD(vhost_user_reconnect_tailq_list, vhost_user_reconnect);
419 struct vhost_user_reconnect_list {
420 struct vhost_user_reconnect_tailq_list head;
421 pthread_mutex_t mutex;
424 static struct vhost_user_reconnect_list reconn_list;
425 static pthread_t reconn_tid;
428 vhost_user_connect_nonblock(int fd, struct sockaddr *un, size_t sz)
432 ret = connect(fd, un, sz);
433 if (ret < 0 && errno != EISCONN)
436 flags = fcntl(fd, F_GETFL, 0);
438 VHOST_LOG_CONFIG(ERR,
439 "can't get flags for connfd %d\n", fd);
442 if ((flags & O_NONBLOCK) && fcntl(fd, F_SETFL, flags & ~O_NONBLOCK)) {
443 VHOST_LOG_CONFIG(ERR,
444 "can't disable nonblocking on fd %d\n", fd);
451 vhost_user_client_reconnect(void *arg __rte_unused)
454 struct vhost_user_reconnect *reconn, *next;
457 pthread_mutex_lock(&reconn_list.mutex);
460 * An equal implementation of TAILQ_FOREACH_SAFE,
461 * which does not exist on all platforms.
463 for (reconn = TAILQ_FIRST(&reconn_list.head);
464 reconn != NULL; reconn = next) {
465 next = TAILQ_NEXT(reconn, next);
467 ret = vhost_user_connect_nonblock(reconn->fd,
468 (struct sockaddr *)&reconn->un,
472 VHOST_LOG_CONFIG(ERR,
473 "reconnection for fd %d failed\n",
480 VHOST_LOG_CONFIG(INFO,
481 "%s: connected\n", reconn->vsocket->path);
482 vhost_user_add_connection(reconn->fd, reconn->vsocket);
484 TAILQ_REMOVE(&reconn_list.head, reconn, next);
488 pthread_mutex_unlock(&reconn_list.mutex);
496 vhost_user_reconnect_init(void)
500 ret = pthread_mutex_init(&reconn_list.mutex, NULL);
502 VHOST_LOG_CONFIG(ERR, "failed to initialize mutex");
505 TAILQ_INIT(&reconn_list.head);
507 ret = rte_ctrl_thread_create(&reconn_tid, "vhost_reconn", NULL,
508 vhost_user_client_reconnect, NULL);
510 VHOST_LOG_CONFIG(ERR, "failed to create reconnect thread");
511 if (pthread_mutex_destroy(&reconn_list.mutex)) {
512 VHOST_LOG_CONFIG(ERR,
513 "failed to destroy reconnect mutex");
521 vhost_user_start_client(struct vhost_user_socket *vsocket)
524 int fd = vsocket->socket_fd;
525 const char *path = vsocket->path;
526 struct vhost_user_reconnect *reconn;
528 ret = vhost_user_connect_nonblock(fd, (struct sockaddr *)&vsocket->un,
529 sizeof(vsocket->un));
531 vhost_user_add_connection(fd, vsocket);
535 VHOST_LOG_CONFIG(WARNING,
536 "failed to connect to %s: %s\n",
537 path, strerror(errno));
539 if (ret == -2 || !vsocket->reconnect) {
544 VHOST_LOG_CONFIG(INFO, "%s: reconnecting...\n", path);
545 reconn = malloc(sizeof(*reconn));
546 if (reconn == NULL) {
547 VHOST_LOG_CONFIG(ERR,
548 "failed to allocate memory for reconnect\n");
552 reconn->un = vsocket->un;
554 reconn->vsocket = vsocket;
555 pthread_mutex_lock(&reconn_list.mutex);
556 TAILQ_INSERT_TAIL(&reconn_list.head, reconn, next);
557 pthread_mutex_unlock(&reconn_list.mutex);
562 static struct vhost_user_socket *
563 find_vhost_user_socket(const char *path)
570 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
571 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
573 if (!strcmp(vsocket->path, path))
581 rte_vhost_driver_attach_vdpa_device(const char *path,
582 struct rte_vdpa_device *dev)
584 struct vhost_user_socket *vsocket;
586 if (dev == NULL || path == NULL)
589 pthread_mutex_lock(&vhost_user.mutex);
590 vsocket = find_vhost_user_socket(path);
592 vsocket->vdpa_dev = dev;
593 pthread_mutex_unlock(&vhost_user.mutex);
595 return vsocket ? 0 : -1;
599 rte_vhost_driver_detach_vdpa_device(const char *path)
601 struct vhost_user_socket *vsocket;
603 pthread_mutex_lock(&vhost_user.mutex);
604 vsocket = find_vhost_user_socket(path);
606 vsocket->vdpa_dev = NULL;
607 pthread_mutex_unlock(&vhost_user.mutex);
609 return vsocket ? 0 : -1;
612 struct rte_vdpa_device *
613 rte_vhost_driver_get_vdpa_device(const char *path)
615 struct vhost_user_socket *vsocket;
616 struct rte_vdpa_device *dev = NULL;
618 pthread_mutex_lock(&vhost_user.mutex);
619 vsocket = find_vhost_user_socket(path);
621 dev = vsocket->vdpa_dev;
622 pthread_mutex_unlock(&vhost_user.mutex);
628 rte_vhost_driver_disable_features(const char *path, uint64_t features)
630 struct vhost_user_socket *vsocket;
632 pthread_mutex_lock(&vhost_user.mutex);
633 vsocket = find_vhost_user_socket(path);
635 /* Note that use_builtin_virtio_net is not affected by this function
636 * since callers may want to selectively disable features of the
637 * built-in vhost net device backend.
641 vsocket->features &= ~features;
642 pthread_mutex_unlock(&vhost_user.mutex);
644 return vsocket ? 0 : -1;
648 rte_vhost_driver_enable_features(const char *path, uint64_t features)
650 struct vhost_user_socket *vsocket;
652 pthread_mutex_lock(&vhost_user.mutex);
653 vsocket = find_vhost_user_socket(path);
655 if ((vsocket->supported_features & features) != features) {
657 * trying to enable features the driver doesn't
660 pthread_mutex_unlock(&vhost_user.mutex);
663 vsocket->features |= features;
665 pthread_mutex_unlock(&vhost_user.mutex);
667 return vsocket ? 0 : -1;
671 rte_vhost_driver_set_features(const char *path, uint64_t features)
673 struct vhost_user_socket *vsocket;
675 pthread_mutex_lock(&vhost_user.mutex);
676 vsocket = find_vhost_user_socket(path);
678 vsocket->supported_features = features;
679 vsocket->features = features;
681 /* Anyone setting feature bits is implementing their own vhost
684 vsocket->use_builtin_virtio_net = false;
686 pthread_mutex_unlock(&vhost_user.mutex);
688 return vsocket ? 0 : -1;
692 rte_vhost_driver_get_features(const char *path, uint64_t *features)
694 struct vhost_user_socket *vsocket;
695 uint64_t vdpa_features;
696 struct rte_vdpa_device *vdpa_dev;
699 pthread_mutex_lock(&vhost_user.mutex);
700 vsocket = find_vhost_user_socket(path);
702 VHOST_LOG_CONFIG(ERR,
703 "socket file %s is not registered yet.\n", path);
708 vdpa_dev = vsocket->vdpa_dev;
710 *features = vsocket->features;
714 if (vdpa_dev->ops->get_features(vdpa_dev, &vdpa_features) < 0) {
715 VHOST_LOG_CONFIG(ERR,
716 "failed to get vdpa features "
717 "for socket file %s.\n", path);
722 *features = vsocket->features & vdpa_features;
725 pthread_mutex_unlock(&vhost_user.mutex);
730 rte_vhost_driver_set_protocol_features(const char *path,
731 uint64_t protocol_features)
733 struct vhost_user_socket *vsocket;
735 pthread_mutex_lock(&vhost_user.mutex);
736 vsocket = find_vhost_user_socket(path);
738 vsocket->protocol_features = protocol_features;
739 pthread_mutex_unlock(&vhost_user.mutex);
740 return vsocket ? 0 : -1;
744 rte_vhost_driver_get_protocol_features(const char *path,
745 uint64_t *protocol_features)
747 struct vhost_user_socket *vsocket;
748 uint64_t vdpa_protocol_features;
749 struct rte_vdpa_device *vdpa_dev;
752 pthread_mutex_lock(&vhost_user.mutex);
753 vsocket = find_vhost_user_socket(path);
755 VHOST_LOG_CONFIG(ERR,
756 "socket file %s is not registered yet.\n", path);
761 vdpa_dev = vsocket->vdpa_dev;
763 *protocol_features = vsocket->protocol_features;
767 if (vdpa_dev->ops->get_protocol_features(vdpa_dev,
768 &vdpa_protocol_features) < 0) {
769 VHOST_LOG_CONFIG(ERR,
770 "failed to get vdpa protocol features "
771 "for socket file %s.\n", path);
776 *protocol_features = vsocket->protocol_features
777 & vdpa_protocol_features;
780 pthread_mutex_unlock(&vhost_user.mutex);
785 rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num)
787 struct vhost_user_socket *vsocket;
788 uint32_t vdpa_queue_num;
789 struct rte_vdpa_device *vdpa_dev;
792 pthread_mutex_lock(&vhost_user.mutex);
793 vsocket = find_vhost_user_socket(path);
795 VHOST_LOG_CONFIG(ERR,
796 "socket file %s is not registered yet.\n", path);
801 vdpa_dev = vsocket->vdpa_dev;
803 *queue_num = VHOST_MAX_QUEUE_PAIRS;
807 if (vdpa_dev->ops->get_queue_num(vdpa_dev, &vdpa_queue_num) < 0) {
808 VHOST_LOG_CONFIG(ERR,
809 "failed to get vdpa queue number "
810 "for socket file %s.\n", path);
815 *queue_num = RTE_MIN((uint32_t)VHOST_MAX_QUEUE_PAIRS, vdpa_queue_num);
818 pthread_mutex_unlock(&vhost_user.mutex);
823 vhost_user_socket_mem_free(struct vhost_user_socket *vsocket)
825 if (vsocket && vsocket->path) {
827 vsocket->path = NULL;
837 * Register a new vhost-user socket; here we could act as server
838 * (the default case), or client (when RTE_VHOST_USER_CLIENT) flag
842 rte_vhost_driver_register(const char *path, uint64_t flags)
845 struct vhost_user_socket *vsocket;
850 pthread_mutex_lock(&vhost_user.mutex);
852 if (vhost_user.vsocket_cnt == MAX_VHOST_SOCKET) {
853 VHOST_LOG_CONFIG(ERR,
854 "error: the number of vhost sockets reaches maximum\n");
858 vsocket = malloc(sizeof(struct vhost_user_socket));
861 memset(vsocket, 0, sizeof(struct vhost_user_socket));
862 vsocket->path = strdup(path);
863 if (vsocket->path == NULL) {
864 VHOST_LOG_CONFIG(ERR,
865 "error: failed to copy socket path string\n");
866 vhost_user_socket_mem_free(vsocket);
869 TAILQ_INIT(&vsocket->conn_list);
870 ret = pthread_mutex_init(&vsocket->conn_mutex, NULL);
872 VHOST_LOG_CONFIG(ERR,
873 "error: failed to init connection mutex\n");
876 vsocket->vdpa_dev = NULL;
877 vsocket->extbuf = flags & RTE_VHOST_USER_EXTBUF_SUPPORT;
878 vsocket->linearbuf = flags & RTE_VHOST_USER_LINEARBUF_SUPPORT;
879 vsocket->async_copy = flags & RTE_VHOST_USER_ASYNC_COPY;
881 if (vsocket->async_copy &&
882 (flags & (RTE_VHOST_USER_IOMMU_SUPPORT |
883 RTE_VHOST_USER_POSTCOPY_SUPPORT))) {
884 VHOST_LOG_CONFIG(ERR, "error: enabling async copy and IOMMU "
885 "or post-copy feature simultaneously is not "
891 * Set the supported features correctly for the builtin vhost-user
894 * Applications know nothing about features the builtin virtio net
895 * driver (virtio_net.c) supports, thus it's not possible for them
896 * to invoke rte_vhost_driver_set_features(). To workaround it, here
897 * we set it unconditionally. If the application want to implement
898 * another vhost-user driver (say SCSI), it should call the
899 * rte_vhost_driver_set_features(), which will overwrite following
902 vsocket->use_builtin_virtio_net = true;
903 vsocket->supported_features = VIRTIO_NET_SUPPORTED_FEATURES;
904 vsocket->features = VIRTIO_NET_SUPPORTED_FEATURES;
905 vsocket->protocol_features = VHOST_USER_PROTOCOL_FEATURES;
907 if (vsocket->async_copy) {
908 vsocket->supported_features &= ~(1ULL << VHOST_F_LOG_ALL);
909 vsocket->features &= ~(1ULL << VHOST_F_LOG_ALL);
910 VHOST_LOG_CONFIG(INFO,
911 "Logging feature is disabled in async copy mode\n");
915 * We'll not be able to receive a buffer from guest in linear mode
916 * without external buffer if it will not fit in a single mbuf, which is
917 * likely if segmentation offloading enabled.
919 if (vsocket->linearbuf && !vsocket->extbuf) {
920 uint64_t seg_offload_features =
921 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
922 (1ULL << VIRTIO_NET_F_HOST_TSO6) |
923 (1ULL << VIRTIO_NET_F_HOST_UFO);
925 VHOST_LOG_CONFIG(INFO,
926 "Linear buffers requested without external buffers, "
927 "disabling host segmentation offloading support\n");
928 vsocket->supported_features &= ~seg_offload_features;
929 vsocket->features &= ~seg_offload_features;
932 if (!(flags & RTE_VHOST_USER_IOMMU_SUPPORT)) {
933 vsocket->supported_features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
934 vsocket->features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
937 if (!(flags & RTE_VHOST_USER_POSTCOPY_SUPPORT)) {
938 vsocket->protocol_features &=
939 ~(1ULL << VHOST_USER_PROTOCOL_F_PAGEFAULT);
941 #ifndef RTE_LIBRTE_VHOST_POSTCOPY
942 VHOST_LOG_CONFIG(ERR,
943 "Postcopy requested but not compiled\n");
949 if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
950 vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
951 if (vsocket->reconnect && reconn_tid == 0) {
952 if (vhost_user_reconnect_init() != 0)
956 vsocket->is_server = true;
958 ret = create_unix_socket(vsocket);
963 vhost_user.vsockets[vhost_user.vsocket_cnt++] = vsocket;
965 pthread_mutex_unlock(&vhost_user.mutex);
969 if (pthread_mutex_destroy(&vsocket->conn_mutex)) {
970 VHOST_LOG_CONFIG(ERR,
971 "error: failed to destroy connection mutex\n");
974 vhost_user_socket_mem_free(vsocket);
976 pthread_mutex_unlock(&vhost_user.mutex);
982 vhost_user_remove_reconnect(struct vhost_user_socket *vsocket)
985 struct vhost_user_reconnect *reconn, *next;
987 pthread_mutex_lock(&reconn_list.mutex);
989 for (reconn = TAILQ_FIRST(&reconn_list.head);
990 reconn != NULL; reconn = next) {
991 next = TAILQ_NEXT(reconn, next);
993 if (reconn->vsocket == vsocket) {
994 TAILQ_REMOVE(&reconn_list.head, reconn, next);
1001 pthread_mutex_unlock(&reconn_list.mutex);
1006 * Unregister the specified vhost socket
1009 rte_vhost_driver_unregister(const char *path)
1013 struct vhost_user_connection *conn, *next;
1019 pthread_mutex_lock(&vhost_user.mutex);
1021 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
1022 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
1024 if (!strcmp(vsocket->path, path)) {
1025 pthread_mutex_lock(&vsocket->conn_mutex);
1026 for (conn = TAILQ_FIRST(&vsocket->conn_list);
1029 next = TAILQ_NEXT(conn, next);
1032 * If r/wcb is executing, release vsocket's
1033 * conn_mutex and vhost_user's mutex locks, and
1034 * try again since the r/wcb may use the
1035 * conn_mutex and mutex locks.
1037 if (fdset_try_del(&vhost_user.fdset,
1038 conn->connfd) == -1) {
1039 pthread_mutex_unlock(
1040 &vsocket->conn_mutex);
1041 pthread_mutex_unlock(&vhost_user.mutex);
1045 VHOST_LOG_CONFIG(INFO,
1046 "free connfd = %d for device '%s'\n",
1047 conn->connfd, path);
1048 close(conn->connfd);
1049 vhost_destroy_device(conn->vid);
1050 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
1053 pthread_mutex_unlock(&vsocket->conn_mutex);
1055 if (vsocket->is_server) {
1057 * If r/wcb is executing, release vhost_user's
1058 * mutex lock, and try again since the r/wcb
1059 * may use the mutex lock.
1061 if (fdset_try_del(&vhost_user.fdset,
1062 vsocket->socket_fd) == -1) {
1063 pthread_mutex_unlock(&vhost_user.mutex);
1067 close(vsocket->socket_fd);
1069 } else if (vsocket->reconnect) {
1070 vhost_user_remove_reconnect(vsocket);
1073 pthread_mutex_destroy(&vsocket->conn_mutex);
1074 vhost_user_socket_mem_free(vsocket);
1076 count = --vhost_user.vsocket_cnt;
1077 vhost_user.vsockets[i] = vhost_user.vsockets[count];
1078 vhost_user.vsockets[count] = NULL;
1079 pthread_mutex_unlock(&vhost_user.mutex);
1084 pthread_mutex_unlock(&vhost_user.mutex);
1090 * Register ops so that we can add/remove device to data core.
1093 rte_vhost_driver_callback_register(const char *path,
1094 struct vhost_device_ops const * const ops)
1096 struct vhost_user_socket *vsocket;
1098 pthread_mutex_lock(&vhost_user.mutex);
1099 vsocket = find_vhost_user_socket(path);
1101 vsocket->notify_ops = ops;
1102 pthread_mutex_unlock(&vhost_user.mutex);
1104 return vsocket ? 0 : -1;
1107 struct vhost_device_ops const *
1108 vhost_driver_callback_get(const char *path)
1110 struct vhost_user_socket *vsocket;
1112 pthread_mutex_lock(&vhost_user.mutex);
1113 vsocket = find_vhost_user_socket(path);
1114 pthread_mutex_unlock(&vhost_user.mutex);
1116 return vsocket ? vsocket->notify_ops : NULL;
1120 rte_vhost_driver_start(const char *path)
1122 struct vhost_user_socket *vsocket;
1123 static pthread_t fdset_tid;
1125 pthread_mutex_lock(&vhost_user.mutex);
1126 vsocket = find_vhost_user_socket(path);
1127 pthread_mutex_unlock(&vhost_user.mutex);
1132 if (fdset_tid == 0) {
1134 * create a pipe which will be waited by poll and notified to
1135 * rebuild the wait list of poll.
1137 if (fdset_pipe_init(&vhost_user.fdset) < 0) {
1138 VHOST_LOG_CONFIG(ERR,
1139 "failed to create pipe for vhost fdset\n");
1143 int ret = rte_ctrl_thread_create(&fdset_tid,
1144 "vhost-events", NULL, fdset_event_dispatch,
1147 VHOST_LOG_CONFIG(ERR,
1148 "failed to create fdset handling thread");
1150 fdset_pipe_uninit(&vhost_user.fdset);
1155 if (vsocket->is_server)
1156 return vhost_user_start_server(vsocket);
1158 return vhost_user_start_client(vsocket);