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;
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;
59 * Device id to identify a specific backend device.
60 * It's set to -1 for the default software implementation.
61 * If valid, one socket can have 1 connection only.
65 struct vhost_device_ops const *notify_ops;
68 struct vhost_user_connection {
69 struct vhost_user_socket *vsocket;
73 TAILQ_ENTRY(vhost_user_connection) next;
76 #define MAX_VHOST_SOCKET 1024
78 struct vhost_user_socket *vsockets[MAX_VHOST_SOCKET];
81 pthread_mutex_t mutex;
84 #define MAX_VIRTIO_BACKLOG 128
86 static void vhost_user_server_new_connection(int fd, void *data, int *remove);
87 static void vhost_user_read_cb(int fd, void *dat, int *remove);
88 static int create_unix_socket(struct vhost_user_socket *vsocket);
89 static int vhost_user_start_client(struct vhost_user_socket *vsocket);
91 static struct vhost_user vhost_user = {
93 .fd = { [0 ... MAX_FDS - 1] = {-1, NULL, NULL, NULL, 0} },
94 .fd_mutex = PTHREAD_MUTEX_INITIALIZER,
95 .fd_pooling_mutex = PTHREAD_MUTEX_INITIALIZER,
99 .mutex = PTHREAD_MUTEX_INITIALIZER,
103 * return bytes# of read on success or negative val on failure. Update fdnum
104 * with number of fds read.
107 read_fd_message(int sockfd, char *buf, int buflen, int *fds, int max_fds,
112 char control[CMSG_SPACE(max_fds * sizeof(int))];
113 struct cmsghdr *cmsg;
119 memset(&msgh, 0, sizeof(msgh));
121 iov.iov_len = buflen;
125 msgh.msg_control = control;
126 msgh.msg_controllen = sizeof(control);
128 ret = recvmsg(sockfd, &msgh, 0);
131 VHOST_LOG_CONFIG(ERR, "recvmsg failed\n");
135 if (msgh.msg_flags & (MSG_TRUNC | MSG_CTRUNC)) {
136 VHOST_LOG_CONFIG(ERR, "truncated msg\n");
140 for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
141 cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
142 if ((cmsg->cmsg_level == SOL_SOCKET) &&
143 (cmsg->cmsg_type == SCM_RIGHTS)) {
144 got_fds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
146 memcpy(fds, CMSG_DATA(cmsg), got_fds * sizeof(int));
151 /* Clear out unused file descriptors */
152 while (got_fds < max_fds)
159 send_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
164 size_t fdsize = fd_num * sizeof(int);
165 char control[CMSG_SPACE(fdsize)];
166 struct cmsghdr *cmsg;
169 memset(&msgh, 0, sizeof(msgh));
171 iov.iov_len = buflen;
176 if (fds && fd_num > 0) {
177 msgh.msg_control = control;
178 msgh.msg_controllen = sizeof(control);
179 cmsg = CMSG_FIRSTHDR(&msgh);
181 VHOST_LOG_CONFIG(ERR, "cmsg == NULL\n");
185 cmsg->cmsg_len = CMSG_LEN(fdsize);
186 cmsg->cmsg_level = SOL_SOCKET;
187 cmsg->cmsg_type = SCM_RIGHTS;
188 memcpy(CMSG_DATA(cmsg), fds, fdsize);
190 msgh.msg_control = NULL;
191 msgh.msg_controllen = 0;
195 ret = sendmsg(sockfd, &msgh, MSG_NOSIGNAL);
196 } while (ret < 0 && errno == EINTR);
199 VHOST_LOG_CONFIG(ERR, "sendmsg error\n");
207 vhost_user_add_connection(int fd, struct vhost_user_socket *vsocket)
211 struct vhost_user_connection *conn;
217 conn = malloc(sizeof(*conn));
223 vid = vhost_new_device();
228 size = strnlen(vsocket->path, PATH_MAX);
229 vhost_set_ifname(vid, vsocket->path, size);
231 vhost_set_builtin_virtio_net(vid, vsocket->use_builtin_virtio_net);
233 vhost_attach_vdpa_device(vid, vsocket->vdpa_dev_id);
235 if (vsocket->dequeue_zero_copy)
236 vhost_enable_dequeue_zero_copy(vid);
239 vhost_enable_extbuf(vid);
241 if (vsocket->linearbuf)
242 vhost_enable_linearbuf(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, int did)
583 struct vhost_user_socket *vsocket;
585 if (rte_vdpa_get_device(did) == NULL || path == NULL)
588 pthread_mutex_lock(&vhost_user.mutex);
589 vsocket = find_vhost_user_socket(path);
591 vsocket->vdpa_dev_id = did;
592 pthread_mutex_unlock(&vhost_user.mutex);
594 return vsocket ? 0 : -1;
598 rte_vhost_driver_detach_vdpa_device(const char *path)
600 struct vhost_user_socket *vsocket;
602 pthread_mutex_lock(&vhost_user.mutex);
603 vsocket = find_vhost_user_socket(path);
605 vsocket->vdpa_dev_id = -1;
606 pthread_mutex_unlock(&vhost_user.mutex);
608 return vsocket ? 0 : -1;
612 rte_vhost_driver_get_vdpa_device_id(const char *path)
614 struct vhost_user_socket *vsocket;
617 pthread_mutex_lock(&vhost_user.mutex);
618 vsocket = find_vhost_user_socket(path);
620 did = vsocket->vdpa_dev_id;
621 pthread_mutex_unlock(&vhost_user.mutex);
627 rte_vhost_driver_disable_features(const char *path, uint64_t features)
629 struct vhost_user_socket *vsocket;
631 pthread_mutex_lock(&vhost_user.mutex);
632 vsocket = find_vhost_user_socket(path);
634 /* Note that use_builtin_virtio_net is not affected by this function
635 * since callers may want to selectively disable features of the
636 * built-in vhost net device backend.
640 vsocket->features &= ~features;
641 pthread_mutex_unlock(&vhost_user.mutex);
643 return vsocket ? 0 : -1;
647 rte_vhost_driver_enable_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 if ((vsocket->supported_features & features) != features) {
656 * trying to enable features the driver doesn't
659 pthread_mutex_unlock(&vhost_user.mutex);
662 vsocket->features |= features;
664 pthread_mutex_unlock(&vhost_user.mutex);
666 return vsocket ? 0 : -1;
670 rte_vhost_driver_set_features(const char *path, uint64_t features)
672 struct vhost_user_socket *vsocket;
674 pthread_mutex_lock(&vhost_user.mutex);
675 vsocket = find_vhost_user_socket(path);
677 vsocket->supported_features = features;
678 vsocket->features = features;
680 /* Anyone setting feature bits is implementing their own vhost
683 vsocket->use_builtin_virtio_net = false;
685 pthread_mutex_unlock(&vhost_user.mutex);
687 return vsocket ? 0 : -1;
691 rte_vhost_driver_get_features(const char *path, uint64_t *features)
693 struct vhost_user_socket *vsocket;
694 uint64_t vdpa_features;
695 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 did = vsocket->vdpa_dev_id;
709 vdpa_dev = rte_vdpa_get_device(did);
710 if (!vdpa_dev || !vdpa_dev->ops->get_features) {
711 *features = vsocket->features;
715 if (vdpa_dev->ops->get_features(did, &vdpa_features) < 0) {
716 VHOST_LOG_CONFIG(ERR,
717 "failed to get vdpa features "
718 "for socket file %s.\n", path);
723 *features = vsocket->features & vdpa_features;
726 pthread_mutex_unlock(&vhost_user.mutex);
731 rte_vhost_driver_set_protocol_features(const char *path,
732 uint64_t protocol_features)
734 struct vhost_user_socket *vsocket;
736 pthread_mutex_lock(&vhost_user.mutex);
737 vsocket = find_vhost_user_socket(path);
739 vsocket->protocol_features = protocol_features;
740 pthread_mutex_unlock(&vhost_user.mutex);
741 return vsocket ? 0 : -1;
745 rte_vhost_driver_get_protocol_features(const char *path,
746 uint64_t *protocol_features)
748 struct vhost_user_socket *vsocket;
749 uint64_t vdpa_protocol_features;
750 struct rte_vdpa_device *vdpa_dev;
754 pthread_mutex_lock(&vhost_user.mutex);
755 vsocket = find_vhost_user_socket(path);
757 VHOST_LOG_CONFIG(ERR,
758 "socket file %s is not registered yet.\n", path);
763 did = vsocket->vdpa_dev_id;
764 vdpa_dev = rte_vdpa_get_device(did);
765 if (!vdpa_dev || !vdpa_dev->ops->get_protocol_features) {
766 *protocol_features = vsocket->protocol_features;
770 if (vdpa_dev->ops->get_protocol_features(did,
771 &vdpa_protocol_features) < 0) {
772 VHOST_LOG_CONFIG(ERR,
773 "failed to get vdpa protocol features "
774 "for socket file %s.\n", path);
779 *protocol_features = vsocket->protocol_features
780 & vdpa_protocol_features;
783 pthread_mutex_unlock(&vhost_user.mutex);
788 rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num)
790 struct vhost_user_socket *vsocket;
791 uint32_t vdpa_queue_num;
792 struct rte_vdpa_device *vdpa_dev;
796 pthread_mutex_lock(&vhost_user.mutex);
797 vsocket = find_vhost_user_socket(path);
799 VHOST_LOG_CONFIG(ERR,
800 "socket file %s is not registered yet.\n", path);
805 did = vsocket->vdpa_dev_id;
806 vdpa_dev = rte_vdpa_get_device(did);
807 if (!vdpa_dev || !vdpa_dev->ops->get_queue_num) {
808 *queue_num = VHOST_MAX_QUEUE_PAIRS;
812 if (vdpa_dev->ops->get_queue_num(did, &vdpa_queue_num) < 0) {
813 VHOST_LOG_CONFIG(ERR,
814 "failed to get vdpa queue number "
815 "for socket file %s.\n", path);
820 *queue_num = RTE_MIN((uint32_t)VHOST_MAX_QUEUE_PAIRS, vdpa_queue_num);
823 pthread_mutex_unlock(&vhost_user.mutex);
828 vhost_user_socket_mem_free(struct vhost_user_socket *vsocket)
830 if (vsocket && vsocket->path) {
832 vsocket->path = NULL;
842 * Register a new vhost-user socket; here we could act as server
843 * (the default case), or client (when RTE_VHOST_USER_CLIENT) flag
847 rte_vhost_driver_register(const char *path, uint64_t flags)
850 struct vhost_user_socket *vsocket;
855 pthread_mutex_lock(&vhost_user.mutex);
857 if (vhost_user.vsocket_cnt == MAX_VHOST_SOCKET) {
858 VHOST_LOG_CONFIG(ERR,
859 "error: the number of vhost sockets reaches maximum\n");
863 vsocket = malloc(sizeof(struct vhost_user_socket));
866 memset(vsocket, 0, sizeof(struct vhost_user_socket));
867 vsocket->path = strdup(path);
868 if (vsocket->path == NULL) {
869 VHOST_LOG_CONFIG(ERR,
870 "error: failed to copy socket path string\n");
871 vhost_user_socket_mem_free(vsocket);
874 TAILQ_INIT(&vsocket->conn_list);
875 ret = pthread_mutex_init(&vsocket->conn_mutex, NULL);
877 VHOST_LOG_CONFIG(ERR,
878 "error: failed to init connection mutex\n");
881 vsocket->vdpa_dev_id = -1;
882 vsocket->dequeue_zero_copy = flags & RTE_VHOST_USER_DEQUEUE_ZERO_COPY;
883 vsocket->extbuf = flags & RTE_VHOST_USER_EXTBUF_SUPPORT;
884 vsocket->linearbuf = flags & RTE_VHOST_USER_LINEARBUF_SUPPORT;
886 if (vsocket->dequeue_zero_copy &&
887 (flags & RTE_VHOST_USER_IOMMU_SUPPORT)) {
888 VHOST_LOG_CONFIG(ERR,
889 "error: enabling dequeue zero copy and IOMMU features "
890 "simultaneously is not supported\n");
895 * Set the supported features correctly for the builtin vhost-user
898 * Applications know nothing about features the builtin virtio net
899 * driver (virtio_net.c) supports, thus it's not possible for them
900 * to invoke rte_vhost_driver_set_features(). To workaround it, here
901 * we set it unconditionally. If the application want to implement
902 * another vhost-user driver (say SCSI), it should call the
903 * rte_vhost_driver_set_features(), which will overwrite following
906 vsocket->use_builtin_virtio_net = true;
907 vsocket->supported_features = VIRTIO_NET_SUPPORTED_FEATURES;
908 vsocket->features = VIRTIO_NET_SUPPORTED_FEATURES;
909 vsocket->protocol_features = VHOST_USER_PROTOCOL_FEATURES;
912 * Dequeue zero copy can't assure descriptors returned in order.
913 * Also, it requires that the guest memory is populated, which is
914 * not compatible with postcopy.
916 if (vsocket->dequeue_zero_copy) {
917 if (vsocket->extbuf) {
918 VHOST_LOG_CONFIG(ERR,
919 "error: zero copy is incompatible with external buffers\n");
923 if (vsocket->linearbuf) {
924 VHOST_LOG_CONFIG(ERR,
925 "error: zero copy is incompatible with linear buffers\n");
929 if (!vsocket->is_server) {
930 VHOST_LOG_CONFIG(ERR,
931 "error: zero copy is incompatible with vhost client mode\n");
935 vsocket->supported_features &= ~(1ULL << VIRTIO_F_IN_ORDER);
936 vsocket->features &= ~(1ULL << VIRTIO_F_IN_ORDER);
938 VHOST_LOG_CONFIG(INFO,
939 "Dequeue zero copy requested, disabling postcopy support\n");
940 vsocket->protocol_features &=
941 ~(1ULL << VHOST_USER_PROTOCOL_F_PAGEFAULT);
945 * We'll not be able to receive a buffer from guest in linear mode
946 * without external buffer if it will not fit in a single mbuf, which is
947 * likely if segmentation offloading enabled.
949 if (vsocket->linearbuf && !vsocket->extbuf) {
950 uint64_t seg_offload_features =
951 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
952 (1ULL << VIRTIO_NET_F_HOST_TSO6) |
953 (1ULL << VIRTIO_NET_F_HOST_UFO);
955 VHOST_LOG_CONFIG(INFO,
956 "Linear buffers requested without external buffers, "
957 "disabling host segmentation offloading support\n");
958 vsocket->supported_features &= ~seg_offload_features;
959 vsocket->features &= ~seg_offload_features;
962 if (!(flags & RTE_VHOST_USER_IOMMU_SUPPORT)) {
963 vsocket->supported_features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
964 vsocket->features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
967 if (!(flags & RTE_VHOST_USER_POSTCOPY_SUPPORT)) {
968 vsocket->protocol_features &=
969 ~(1ULL << VHOST_USER_PROTOCOL_F_PAGEFAULT);
971 #ifndef RTE_LIBRTE_VHOST_POSTCOPY
972 VHOST_LOG_CONFIG(ERR,
973 "Postcopy requested but not compiled\n");
979 if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
980 vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
981 if (vsocket->reconnect && reconn_tid == 0) {
982 if (vhost_user_reconnect_init() != 0)
986 vsocket->is_server = true;
988 ret = create_unix_socket(vsocket);
993 vhost_user.vsockets[vhost_user.vsocket_cnt++] = vsocket;
995 pthread_mutex_unlock(&vhost_user.mutex);
999 if (pthread_mutex_destroy(&vsocket->conn_mutex)) {
1000 VHOST_LOG_CONFIG(ERR,
1001 "error: failed to destroy connection mutex\n");
1004 vhost_user_socket_mem_free(vsocket);
1006 pthread_mutex_unlock(&vhost_user.mutex);
1012 vhost_user_remove_reconnect(struct vhost_user_socket *vsocket)
1015 struct vhost_user_reconnect *reconn, *next;
1017 pthread_mutex_lock(&reconn_list.mutex);
1019 for (reconn = TAILQ_FIRST(&reconn_list.head);
1020 reconn != NULL; reconn = next) {
1021 next = TAILQ_NEXT(reconn, next);
1023 if (reconn->vsocket == vsocket) {
1024 TAILQ_REMOVE(&reconn_list.head, reconn, next);
1031 pthread_mutex_unlock(&reconn_list.mutex);
1036 * Unregister the specified vhost socket
1039 rte_vhost_driver_unregister(const char *path)
1043 struct vhost_user_connection *conn, *next;
1049 pthread_mutex_lock(&vhost_user.mutex);
1051 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
1052 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
1054 if (!strcmp(vsocket->path, path)) {
1055 pthread_mutex_lock(&vsocket->conn_mutex);
1056 for (conn = TAILQ_FIRST(&vsocket->conn_list);
1059 next = TAILQ_NEXT(conn, next);
1062 * If r/wcb is executing, release vsocket's
1063 * conn_mutex and vhost_user's mutex locks, and
1064 * try again since the r/wcb may use the
1065 * conn_mutex and mutex locks.
1067 if (fdset_try_del(&vhost_user.fdset,
1068 conn->connfd) == -1) {
1069 pthread_mutex_unlock(
1070 &vsocket->conn_mutex);
1071 pthread_mutex_unlock(&vhost_user.mutex);
1075 VHOST_LOG_CONFIG(INFO,
1076 "free connfd = %d for device '%s'\n",
1077 conn->connfd, path);
1078 close(conn->connfd);
1079 vhost_destroy_device(conn->vid);
1080 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
1083 pthread_mutex_unlock(&vsocket->conn_mutex);
1085 if (vsocket->is_server) {
1087 * If r/wcb is executing, release vhost_user's
1088 * mutex lock, and try again since the r/wcb
1089 * may use the mutex lock.
1091 if (fdset_try_del(&vhost_user.fdset,
1092 vsocket->socket_fd) == -1) {
1093 pthread_mutex_unlock(&vhost_user.mutex);
1097 close(vsocket->socket_fd);
1099 } else if (vsocket->reconnect) {
1100 vhost_user_remove_reconnect(vsocket);
1103 pthread_mutex_destroy(&vsocket->conn_mutex);
1104 vhost_user_socket_mem_free(vsocket);
1106 count = --vhost_user.vsocket_cnt;
1107 vhost_user.vsockets[i] = vhost_user.vsockets[count];
1108 vhost_user.vsockets[count] = NULL;
1109 pthread_mutex_unlock(&vhost_user.mutex);
1114 pthread_mutex_unlock(&vhost_user.mutex);
1120 * Register ops so that we can add/remove device to data core.
1123 rte_vhost_driver_callback_register(const char *path,
1124 struct vhost_device_ops const * const ops)
1126 struct vhost_user_socket *vsocket;
1128 pthread_mutex_lock(&vhost_user.mutex);
1129 vsocket = find_vhost_user_socket(path);
1131 vsocket->notify_ops = ops;
1132 pthread_mutex_unlock(&vhost_user.mutex);
1134 return vsocket ? 0 : -1;
1137 struct vhost_device_ops const *
1138 vhost_driver_callback_get(const char *path)
1140 struct vhost_user_socket *vsocket;
1142 pthread_mutex_lock(&vhost_user.mutex);
1143 vsocket = find_vhost_user_socket(path);
1144 pthread_mutex_unlock(&vhost_user.mutex);
1146 return vsocket ? vsocket->notify_ops : NULL;
1150 rte_vhost_driver_start(const char *path)
1152 struct vhost_user_socket *vsocket;
1153 static pthread_t fdset_tid;
1155 pthread_mutex_lock(&vhost_user.mutex);
1156 vsocket = find_vhost_user_socket(path);
1157 pthread_mutex_unlock(&vhost_user.mutex);
1162 if (fdset_tid == 0) {
1164 * create a pipe which will be waited by poll and notified to
1165 * rebuild the wait list of poll.
1167 if (fdset_pipe_init(&vhost_user.fdset) < 0) {
1168 VHOST_LOG_CONFIG(ERR,
1169 "failed to create pipe for vhost fdset\n");
1173 int ret = rte_ctrl_thread_create(&fdset_tid,
1174 "vhost-events", NULL, fdset_event_dispatch,
1177 VHOST_LOG_CONFIG(ERR,
1178 "failed to create fdset handling thread");
1180 fdset_pipe_uninit(&vhost_user.fdset);
1185 if (vsocket->is_server)
1186 return vhost_user_start_server(vsocket);
1188 return vhost_user_start_client(vsocket);