1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2016 Intel Corporation
11 #include <sys/socket.h>
13 #include <sys/queue.h>
22 #include "vhost_user.h"
25 TAILQ_HEAD(vhost_user_connection_list, vhost_user_connection);
28 * Every time rte_vhost_driver_register() is invoked, an associated
29 * vhost_user_socket struct will be created.
31 struct vhost_user_socket {
32 struct vhost_user_connection_list conn_list;
33 pthread_mutex_t conn_mutex;
36 struct sockaddr_un un;
40 bool use_builtin_virtio_net;
44 bool net_compliant_ol_flags;
48 * The "supported_features" indicates the feature bits the
49 * vhost driver supports. The "features" indicates the feature
50 * bits after the rte_vhost_driver_features_disable/enable().
51 * It is also the final feature bits used for vhost-user
52 * features negotiation.
54 uint64_t supported_features;
57 uint64_t protocol_features;
59 struct rte_vdpa_device *vdpa_dev;
61 struct rte_vhost_device_ops const *notify_ops;
64 struct vhost_user_connection {
65 struct vhost_user_socket *vsocket;
69 TAILQ_ENTRY(vhost_user_connection) next;
72 #define MAX_VHOST_SOCKET 1024
74 struct vhost_user_socket *vsockets[MAX_VHOST_SOCKET];
77 pthread_mutex_t mutex;
80 #define MAX_VIRTIO_BACKLOG 128
82 static void vhost_user_server_new_connection(int fd, void *data, int *remove);
83 static void vhost_user_read_cb(int fd, void *dat, int *remove);
84 static int create_unix_socket(struct vhost_user_socket *vsocket);
85 static int vhost_user_start_client(struct vhost_user_socket *vsocket);
87 static struct vhost_user vhost_user = {
89 .fd = { [0 ... MAX_FDS - 1] = {-1, NULL, NULL, NULL, 0} },
90 .fd_mutex = PTHREAD_MUTEX_INITIALIZER,
91 .fd_pooling_mutex = PTHREAD_MUTEX_INITIALIZER,
95 .mutex = PTHREAD_MUTEX_INITIALIZER,
99 * return bytes# of read on success or negative val on failure. Update fdnum
100 * with number of fds read.
103 read_fd_message(char *ifname, int sockfd, char *buf, int buflen, int *fds, int max_fds,
108 char control[CMSG_SPACE(max_fds * sizeof(int))];
109 struct cmsghdr *cmsg;
115 memset(&msgh, 0, sizeof(msgh));
117 iov.iov_len = buflen;
121 msgh.msg_control = control;
122 msgh.msg_controllen = sizeof(control);
124 ret = recvmsg(sockfd, &msgh, 0);
127 VHOST_LOG_CONFIG(ERR, "(%s) recvmsg failed on fd %d (%s)\n",
128 ifname, sockfd, strerror(errno));
132 if (msgh.msg_flags & (MSG_TRUNC | MSG_CTRUNC)) {
133 VHOST_LOG_CONFIG(ERR, "(%s) truncated msg (fd %d)\n", ifname, sockfd);
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(char *ifname, 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 VHOST_LOG_CONFIG(ERR, "(%s) cmsg == NULL\n", ifname);
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 VHOST_LOG_CONFIG(ERR, "(%s) sendmsg error on fd %d (%s)\n",
197 ifname, sockfd, strerror(errno));
205 vhost_user_add_connection(int fd, struct vhost_user_socket *vsocket)
209 struct vhost_user_connection *conn;
211 struct virtio_net *dev;
216 conn = malloc(sizeof(*conn));
222 vid = vhost_new_device();
227 size = strnlen(vsocket->path, PATH_MAX);
228 vhost_set_ifname(vid, vsocket->path, size);
230 vhost_setup_virtio_net(vid, vsocket->use_builtin_virtio_net,
231 vsocket->net_compliant_ol_flags, vsocket->stats_enabled);
233 vhost_attach_vdpa_device(vid, vsocket->vdpa_dev);
236 vhost_enable_extbuf(vid);
238 if (vsocket->linearbuf)
239 vhost_enable_linearbuf(vid);
241 if (vsocket->async_copy) {
242 dev = get_device(vid);
248 VHOST_LOG_CONFIG(INFO, "(%s) new device, handle is %d\n", vsocket->path, vid);
250 if (vsocket->notify_ops->new_connection) {
251 ret = vsocket->notify_ops->new_connection(vid);
253 VHOST_LOG_CONFIG(ERR,
254 "(%s) failed to add vhost user connection with fd %d\n",
261 conn->vsocket = vsocket;
263 ret = fdset_add(&vhost_user.fdset, fd, vhost_user_read_cb,
266 VHOST_LOG_CONFIG(ERR, "(%s) failed to add fd %d into vhost server fdset\n",
269 if (vsocket->notify_ops->destroy_connection)
270 vsocket->notify_ops->destroy_connection(conn->vid);
275 pthread_mutex_lock(&vsocket->conn_mutex);
276 TAILQ_INSERT_TAIL(&vsocket->conn_list, conn, next);
277 pthread_mutex_unlock(&vsocket->conn_mutex);
279 fdset_pipe_notify(&vhost_user.fdset);
283 vhost_destroy_device(vid);
289 /* call back when there is new vhost-user connection from client */
291 vhost_user_server_new_connection(int fd, void *dat, int *remove __rte_unused)
293 struct vhost_user_socket *vsocket = dat;
295 fd = accept(fd, NULL, NULL);
299 VHOST_LOG_CONFIG(INFO, "(%s) new vhost user connection is %d\n",
301 vhost_user_add_connection(fd, vsocket);
305 vhost_user_read_cb(int connfd, void *dat, int *remove)
307 struct vhost_user_connection *conn = dat;
308 struct vhost_user_socket *vsocket = conn->vsocket;
311 ret = vhost_user_msg_handler(conn->vid, connfd);
313 struct virtio_net *dev = get_device(conn->vid);
319 vhost_destroy_device_notify(dev);
321 if (vsocket->notify_ops->destroy_connection)
322 vsocket->notify_ops->destroy_connection(conn->vid);
324 vhost_destroy_device(conn->vid);
326 if (vsocket->reconnect) {
327 create_unix_socket(vsocket);
328 vhost_user_start_client(vsocket);
331 pthread_mutex_lock(&vsocket->conn_mutex);
332 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
333 pthread_mutex_unlock(&vsocket->conn_mutex);
340 create_unix_socket(struct vhost_user_socket *vsocket)
343 struct sockaddr_un *un = &vsocket->un;
345 fd = socket(AF_UNIX, SOCK_STREAM, 0);
348 VHOST_LOG_CONFIG(INFO, "(%s) vhost-user %s: socket created, fd: %d\n",
349 vsocket->path, vsocket->is_server ? "server" : "client", fd);
351 if (!vsocket->is_server && fcntl(fd, F_SETFL, O_NONBLOCK)) {
352 VHOST_LOG_CONFIG(ERR,
353 "(%s) vhost-user: can't set nonblocking mode for socket, fd: %d (%s)\n",
354 vsocket->path, fd, strerror(errno));
359 memset(un, 0, sizeof(*un));
360 un->sun_family = AF_UNIX;
361 strncpy(un->sun_path, vsocket->path, sizeof(un->sun_path));
362 un->sun_path[sizeof(un->sun_path) - 1] = '\0';
364 vsocket->socket_fd = fd;
369 vhost_user_start_server(struct vhost_user_socket *vsocket)
372 int fd = vsocket->socket_fd;
373 const char *path = vsocket->path;
376 * bind () may fail if the socket file with the same name already
377 * exists. But the library obviously should not delete the file
378 * provided by the user, since we can not be sure that it is not
379 * being used by other applications. Moreover, many applications form
380 * socket names based on user input, which is prone to errors.
382 * The user must ensure that the socket does not exist before
383 * registering the vhost driver in server mode.
385 ret = bind(fd, (struct sockaddr *)&vsocket->un, sizeof(vsocket->un));
387 VHOST_LOG_CONFIG(ERR, "(%s) failed to bind: %s; remove it and try again\n",
388 path, strerror(errno));
391 VHOST_LOG_CONFIG(INFO, "(%s) binding succeeded\n", path);
393 ret = listen(fd, MAX_VIRTIO_BACKLOG);
397 ret = fdset_add(&vhost_user.fdset, fd, vhost_user_server_new_connection,
400 VHOST_LOG_CONFIG(ERR,
401 "(%s) failed to add listen fd %d to vhost server fdset\n",
413 struct vhost_user_reconnect {
414 struct sockaddr_un un;
416 struct vhost_user_socket *vsocket;
418 TAILQ_ENTRY(vhost_user_reconnect) next;
421 TAILQ_HEAD(vhost_user_reconnect_tailq_list, vhost_user_reconnect);
422 struct vhost_user_reconnect_list {
423 struct vhost_user_reconnect_tailq_list head;
424 pthread_mutex_t mutex;
427 static struct vhost_user_reconnect_list reconn_list;
428 static pthread_t reconn_tid;
431 vhost_user_connect_nonblock(char *path, int fd, struct sockaddr *un, size_t sz)
435 ret = connect(fd, un, sz);
436 if (ret < 0 && errno != EISCONN)
439 flags = fcntl(fd, F_GETFL, 0);
441 VHOST_LOG_CONFIG(ERR, "(%s) can't get flags for connfd %d (%s)\n",
442 path, fd, strerror(errno));
445 if ((flags & O_NONBLOCK) && fcntl(fd, F_SETFL, flags & ~O_NONBLOCK)) {
446 VHOST_LOG_CONFIG(ERR, "(%s) can't disable nonblocking on fd %d\n", path, fd);
453 vhost_user_client_reconnect(void *arg __rte_unused)
456 struct vhost_user_reconnect *reconn, *next;
459 pthread_mutex_lock(&reconn_list.mutex);
462 * An equal implementation of TAILQ_FOREACH_SAFE,
463 * which does not exist on all platforms.
465 for (reconn = TAILQ_FIRST(&reconn_list.head);
466 reconn != NULL; reconn = next) {
467 next = TAILQ_NEXT(reconn, next);
469 ret = vhost_user_connect_nonblock(reconn->vsocket->path, reconn->fd,
470 (struct sockaddr *)&reconn->un,
474 VHOST_LOG_CONFIG(ERR, "(%s) reconnection for fd %d failed\n",
475 reconn->vsocket->path, reconn->fd);
481 VHOST_LOG_CONFIG(INFO, "(%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, "%s: failed to initialize mutex", __func__);
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, "%s: failed to destroy reconnect mutex", __func__);
519 vhost_user_start_client(struct vhost_user_socket *vsocket)
522 int fd = vsocket->socket_fd;
523 const char *path = vsocket->path;
524 struct vhost_user_reconnect *reconn;
526 ret = vhost_user_connect_nonblock(vsocket->path, fd, (struct sockaddr *)&vsocket->un,
527 sizeof(vsocket->un));
529 vhost_user_add_connection(fd, vsocket);
533 VHOST_LOG_CONFIG(WARNING, "(%s) failed to connect: %s\n", path, strerror(errno));
535 if (ret == -2 || !vsocket->reconnect) {
540 VHOST_LOG_CONFIG(INFO, "(%s) reconnecting...\n", path);
541 reconn = malloc(sizeof(*reconn));
542 if (reconn == NULL) {
543 VHOST_LOG_CONFIG(ERR, "(%s) failed to allocate memory for reconnect\n", path);
547 reconn->un = vsocket->un;
549 reconn->vsocket = vsocket;
550 pthread_mutex_lock(&reconn_list.mutex);
551 TAILQ_INSERT_TAIL(&reconn_list.head, reconn, next);
552 pthread_mutex_unlock(&reconn_list.mutex);
557 static struct vhost_user_socket *
558 find_vhost_user_socket(const char *path)
565 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
566 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
568 if (!strcmp(vsocket->path, path))
576 rte_vhost_driver_attach_vdpa_device(const char *path,
577 struct rte_vdpa_device *dev)
579 struct vhost_user_socket *vsocket;
581 if (dev == NULL || path == NULL)
584 pthread_mutex_lock(&vhost_user.mutex);
585 vsocket = find_vhost_user_socket(path);
587 vsocket->vdpa_dev = dev;
588 pthread_mutex_unlock(&vhost_user.mutex);
590 return vsocket ? 0 : -1;
594 rte_vhost_driver_detach_vdpa_device(const char *path)
596 struct vhost_user_socket *vsocket;
598 pthread_mutex_lock(&vhost_user.mutex);
599 vsocket = find_vhost_user_socket(path);
601 vsocket->vdpa_dev = NULL;
602 pthread_mutex_unlock(&vhost_user.mutex);
604 return vsocket ? 0 : -1;
607 struct rte_vdpa_device *
608 rte_vhost_driver_get_vdpa_device(const char *path)
610 struct vhost_user_socket *vsocket;
611 struct rte_vdpa_device *dev = NULL;
613 pthread_mutex_lock(&vhost_user.mutex);
614 vsocket = find_vhost_user_socket(path);
616 dev = vsocket->vdpa_dev;
617 pthread_mutex_unlock(&vhost_user.mutex);
623 rte_vhost_driver_get_vdpa_dev_type(const char *path, uint32_t *type)
625 struct vhost_user_socket *vsocket;
626 struct rte_vdpa_device *vdpa_dev;
627 uint32_t vdpa_type = 0;
630 pthread_mutex_lock(&vhost_user.mutex);
631 vsocket = find_vhost_user_socket(path);
633 VHOST_LOG_CONFIG(ERR,
634 "(%s) socket file is not registered yet.\n",
640 vdpa_dev = vsocket->vdpa_dev;
646 if (vdpa_dev->ops->get_dev_type) {
647 ret = vdpa_dev->ops->get_dev_type(vdpa_dev, &vdpa_type);
649 VHOST_LOG_CONFIG(ERR,
650 "(%s) failed to get vdpa dev type for socket file.\n",
656 vdpa_type = RTE_VHOST_VDPA_DEVICE_TYPE_NET;
662 pthread_mutex_unlock(&vhost_user.mutex);
667 rte_vhost_driver_disable_features(const char *path, uint64_t features)
669 struct vhost_user_socket *vsocket;
671 pthread_mutex_lock(&vhost_user.mutex);
672 vsocket = find_vhost_user_socket(path);
674 /* Note that use_builtin_virtio_net is not affected by this function
675 * since callers may want to selectively disable features of the
676 * built-in vhost net device backend.
680 vsocket->features &= ~features;
681 pthread_mutex_unlock(&vhost_user.mutex);
683 return vsocket ? 0 : -1;
687 rte_vhost_driver_enable_features(const char *path, uint64_t features)
689 struct vhost_user_socket *vsocket;
691 pthread_mutex_lock(&vhost_user.mutex);
692 vsocket = find_vhost_user_socket(path);
694 if ((vsocket->supported_features & features) != features) {
696 * trying to enable features the driver doesn't
699 pthread_mutex_unlock(&vhost_user.mutex);
702 vsocket->features |= features;
704 pthread_mutex_unlock(&vhost_user.mutex);
706 return vsocket ? 0 : -1;
710 rte_vhost_driver_set_features(const char *path, uint64_t features)
712 struct vhost_user_socket *vsocket;
714 pthread_mutex_lock(&vhost_user.mutex);
715 vsocket = find_vhost_user_socket(path);
717 vsocket->supported_features = features;
718 vsocket->features = features;
720 /* Anyone setting feature bits is implementing their own vhost
723 vsocket->use_builtin_virtio_net = false;
725 pthread_mutex_unlock(&vhost_user.mutex);
727 return vsocket ? 0 : -1;
731 rte_vhost_driver_get_features(const char *path, uint64_t *features)
733 struct vhost_user_socket *vsocket;
734 uint64_t vdpa_features;
735 struct rte_vdpa_device *vdpa_dev;
738 pthread_mutex_lock(&vhost_user.mutex);
739 vsocket = find_vhost_user_socket(path);
741 VHOST_LOG_CONFIG(ERR, "(%s) socket file is not registered yet.\n", path);
746 vdpa_dev = vsocket->vdpa_dev;
748 *features = vsocket->features;
752 if (vdpa_dev->ops->get_features(vdpa_dev, &vdpa_features) < 0) {
753 VHOST_LOG_CONFIG(ERR, "(%s) failed to get vdpa features for socket file.\n", path);
758 *features = vsocket->features & vdpa_features;
761 pthread_mutex_unlock(&vhost_user.mutex);
766 rte_vhost_driver_set_protocol_features(const char *path,
767 uint64_t protocol_features)
769 struct vhost_user_socket *vsocket;
771 pthread_mutex_lock(&vhost_user.mutex);
772 vsocket = find_vhost_user_socket(path);
774 vsocket->protocol_features = protocol_features;
775 pthread_mutex_unlock(&vhost_user.mutex);
776 return vsocket ? 0 : -1;
780 rte_vhost_driver_get_protocol_features(const char *path,
781 uint64_t *protocol_features)
783 struct vhost_user_socket *vsocket;
784 uint64_t vdpa_protocol_features;
785 struct rte_vdpa_device *vdpa_dev;
788 pthread_mutex_lock(&vhost_user.mutex);
789 vsocket = find_vhost_user_socket(path);
791 VHOST_LOG_CONFIG(ERR, "(%s) socket file is not registered yet.\n", path);
796 vdpa_dev = vsocket->vdpa_dev;
798 *protocol_features = vsocket->protocol_features;
802 if (vdpa_dev->ops->get_protocol_features(vdpa_dev,
803 &vdpa_protocol_features) < 0) {
804 VHOST_LOG_CONFIG(ERR, "(%s) failed to get vdpa protocol features.\n",
810 *protocol_features = vsocket->protocol_features
811 & vdpa_protocol_features;
814 pthread_mutex_unlock(&vhost_user.mutex);
819 rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num)
821 struct vhost_user_socket *vsocket;
822 uint32_t vdpa_queue_num;
823 struct rte_vdpa_device *vdpa_dev;
826 pthread_mutex_lock(&vhost_user.mutex);
827 vsocket = find_vhost_user_socket(path);
829 VHOST_LOG_CONFIG(ERR, "(%s) socket file is not registered yet.\n", path);
834 vdpa_dev = vsocket->vdpa_dev;
836 *queue_num = VHOST_MAX_QUEUE_PAIRS;
840 if (vdpa_dev->ops->get_queue_num(vdpa_dev, &vdpa_queue_num) < 0) {
841 VHOST_LOG_CONFIG(ERR, "(%s) failed to get vdpa queue number.\n",
847 *queue_num = RTE_MIN((uint32_t)VHOST_MAX_QUEUE_PAIRS, vdpa_queue_num);
850 pthread_mutex_unlock(&vhost_user.mutex);
855 vhost_user_socket_mem_free(struct vhost_user_socket *vsocket)
857 if (vsocket && vsocket->path) {
859 vsocket->path = NULL;
869 * Register a new vhost-user socket; here we could act as server
870 * (the default case), or client (when RTE_VHOST_USER_CLIENT) flag
874 rte_vhost_driver_register(const char *path, uint64_t flags)
877 struct vhost_user_socket *vsocket;
882 pthread_mutex_lock(&vhost_user.mutex);
884 if (vhost_user.vsocket_cnt == MAX_VHOST_SOCKET) {
885 VHOST_LOG_CONFIG(ERR, "(%s) the number of vhost sockets reaches maximum\n",
890 vsocket = malloc(sizeof(struct vhost_user_socket));
893 memset(vsocket, 0, sizeof(struct vhost_user_socket));
894 vsocket->path = strdup(path);
895 if (vsocket->path == NULL) {
896 VHOST_LOG_CONFIG(ERR, "(%s) failed to copy socket path string\n", path);
897 vhost_user_socket_mem_free(vsocket);
900 TAILQ_INIT(&vsocket->conn_list);
901 ret = pthread_mutex_init(&vsocket->conn_mutex, NULL);
903 VHOST_LOG_CONFIG(ERR, "(%s) failed to init connection mutex\n", path);
906 vsocket->vdpa_dev = NULL;
907 vsocket->extbuf = flags & RTE_VHOST_USER_EXTBUF_SUPPORT;
908 vsocket->linearbuf = flags & RTE_VHOST_USER_LINEARBUF_SUPPORT;
909 vsocket->async_copy = flags & RTE_VHOST_USER_ASYNC_COPY;
910 vsocket->net_compliant_ol_flags = flags & RTE_VHOST_USER_NET_COMPLIANT_OL_FLAGS;
911 vsocket->stats_enabled = flags & RTE_VHOST_USER_NET_STATS_ENABLE;
913 if (vsocket->async_copy &&
914 (flags & (RTE_VHOST_USER_IOMMU_SUPPORT |
915 RTE_VHOST_USER_POSTCOPY_SUPPORT))) {
916 VHOST_LOG_CONFIG(ERR, "(%s) async copy with IOMMU or post-copy not supported\n",
922 * Set the supported features correctly for the builtin vhost-user
925 * Applications know nothing about features the builtin virtio net
926 * driver (virtio_net.c) supports, thus it's not possible for them
927 * to invoke rte_vhost_driver_set_features(). To workaround it, here
928 * we set it unconditionally. If the application want to implement
929 * another vhost-user driver (say SCSI), it should call the
930 * rte_vhost_driver_set_features(), which will overwrite following
933 vsocket->use_builtin_virtio_net = true;
934 vsocket->supported_features = VIRTIO_NET_SUPPORTED_FEATURES;
935 vsocket->features = VIRTIO_NET_SUPPORTED_FEATURES;
936 vsocket->protocol_features = VHOST_USER_PROTOCOL_FEATURES;
938 if (vsocket->async_copy) {
939 vsocket->supported_features &= ~(1ULL << VHOST_F_LOG_ALL);
940 vsocket->features &= ~(1ULL << VHOST_F_LOG_ALL);
941 VHOST_LOG_CONFIG(INFO, "(%s) logging feature is disabled in async copy mode\n",
946 * We'll not be able to receive a buffer from guest in linear mode
947 * without external buffer if it will not fit in a single mbuf, which is
948 * likely if segmentation offloading enabled.
950 if (vsocket->linearbuf && !vsocket->extbuf) {
951 uint64_t seg_offload_features =
952 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
953 (1ULL << VIRTIO_NET_F_HOST_TSO6) |
954 (1ULL << VIRTIO_NET_F_HOST_UFO);
956 VHOST_LOG_CONFIG(INFO, "(%s) Linear buffers requested without external buffers,\n",
958 VHOST_LOG_CONFIG(INFO, "(%s) disabling host segmentation offloading support\n",
960 vsocket->supported_features &= ~seg_offload_features;
961 vsocket->features &= ~seg_offload_features;
964 if (!(flags & RTE_VHOST_USER_IOMMU_SUPPORT)) {
965 vsocket->supported_features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
966 vsocket->features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
969 if (!(flags & RTE_VHOST_USER_POSTCOPY_SUPPORT)) {
970 vsocket->protocol_features &=
971 ~(1ULL << VHOST_USER_PROTOCOL_F_PAGEFAULT);
973 #ifndef RTE_LIBRTE_VHOST_POSTCOPY
974 VHOST_LOG_CONFIG(ERR, "(%s) Postcopy requested but not compiled\n", path);
980 if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
981 vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
982 if (vsocket->reconnect && reconn_tid == 0) {
983 if (vhost_user_reconnect_init() != 0)
987 vsocket->is_server = true;
989 ret = create_unix_socket(vsocket);
994 vhost_user.vsockets[vhost_user.vsocket_cnt++] = vsocket;
996 pthread_mutex_unlock(&vhost_user.mutex);
1000 if (pthread_mutex_destroy(&vsocket->conn_mutex)) {
1001 VHOST_LOG_CONFIG(ERR, "(%s) failed to destroy connection mutex\n", path);
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];
1053 if (strcmp(vsocket->path, path))
1056 if (vsocket->is_server) {
1058 * If r/wcb is executing, release vhost_user's
1059 * mutex lock, and try again since the r/wcb
1060 * may use the mutex lock.
1062 if (fdset_try_del(&vhost_user.fdset, vsocket->socket_fd) == -1) {
1063 pthread_mutex_unlock(&vhost_user.mutex);
1066 } else if (vsocket->reconnect) {
1067 vhost_user_remove_reconnect(vsocket);
1070 pthread_mutex_lock(&vsocket->conn_mutex);
1071 for (conn = TAILQ_FIRST(&vsocket->conn_list);
1074 next = TAILQ_NEXT(conn, next);
1077 * If r/wcb is executing, release vsocket's
1078 * conn_mutex and vhost_user's mutex locks, and
1079 * try again since the r/wcb may use the
1080 * conn_mutex and mutex locks.
1082 if (fdset_try_del(&vhost_user.fdset,
1083 conn->connfd) == -1) {
1084 pthread_mutex_unlock(&vsocket->conn_mutex);
1085 pthread_mutex_unlock(&vhost_user.mutex);
1089 VHOST_LOG_CONFIG(INFO, "(%s) free connfd %d\n", path, conn->connfd);
1090 close(conn->connfd);
1091 vhost_destroy_device(conn->vid);
1092 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
1095 pthread_mutex_unlock(&vsocket->conn_mutex);
1097 if (vsocket->is_server) {
1098 close(vsocket->socket_fd);
1102 pthread_mutex_destroy(&vsocket->conn_mutex);
1103 vhost_user_socket_mem_free(vsocket);
1105 count = --vhost_user.vsocket_cnt;
1106 vhost_user.vsockets[i] = vhost_user.vsockets[count];
1107 vhost_user.vsockets[count] = NULL;
1108 pthread_mutex_unlock(&vhost_user.mutex);
1111 pthread_mutex_unlock(&vhost_user.mutex);
1117 * Register ops so that we can add/remove device to data core.
1120 rte_vhost_driver_callback_register(const char *path,
1121 struct rte_vhost_device_ops const * const ops)
1123 struct vhost_user_socket *vsocket;
1125 pthread_mutex_lock(&vhost_user.mutex);
1126 vsocket = find_vhost_user_socket(path);
1128 vsocket->notify_ops = ops;
1129 pthread_mutex_unlock(&vhost_user.mutex);
1131 return vsocket ? 0 : -1;
1134 struct rte_vhost_device_ops const *
1135 vhost_driver_callback_get(const char *path)
1137 struct vhost_user_socket *vsocket;
1139 pthread_mutex_lock(&vhost_user.mutex);
1140 vsocket = find_vhost_user_socket(path);
1141 pthread_mutex_unlock(&vhost_user.mutex);
1143 return vsocket ? vsocket->notify_ops : NULL;
1147 rte_vhost_driver_start(const char *path)
1149 struct vhost_user_socket *vsocket;
1150 static pthread_t fdset_tid;
1152 pthread_mutex_lock(&vhost_user.mutex);
1153 vsocket = find_vhost_user_socket(path);
1154 pthread_mutex_unlock(&vhost_user.mutex);
1159 if (fdset_tid == 0) {
1161 * create a pipe which will be waited by poll and notified to
1162 * rebuild the wait list of poll.
1164 if (fdset_pipe_init(&vhost_user.fdset) < 0) {
1165 VHOST_LOG_CONFIG(ERR, "(%s) failed to create pipe for vhost fdset\n", path);
1169 int ret = rte_ctrl_thread_create(&fdset_tid,
1170 "vhost-events", NULL, fdset_event_dispatch,
1173 VHOST_LOG_CONFIG(ERR, "(%s) failed to create fdset handling thread", path);
1175 fdset_pipe_uninit(&vhost_user.fdset);
1180 if (vsocket->is_server)
1181 return vhost_user_start_server(vsocket);
1183 return vhost_user_start_client(vsocket);