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;
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 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(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, "recvmsg failed\n");
131 if (msgh.msg_flags & (MSG_TRUNC | MSG_CTRUNC)) {
132 VHOST_LOG_CONFIG(ERR, "truncated msg\n");
136 for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
137 cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
138 if ((cmsg->cmsg_level == SOL_SOCKET) &&
139 (cmsg->cmsg_type == SCM_RIGHTS)) {
140 got_fds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
142 memcpy(fds, CMSG_DATA(cmsg), got_fds * sizeof(int));
147 /* Clear out unused file descriptors */
148 while (got_fds < max_fds)
155 send_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
160 size_t fdsize = fd_num * sizeof(int);
161 char control[CMSG_SPACE(fdsize)];
162 struct cmsghdr *cmsg;
165 memset(&msgh, 0, sizeof(msgh));
167 iov.iov_len = buflen;
172 if (fds && fd_num > 0) {
173 msgh.msg_control = control;
174 msgh.msg_controllen = sizeof(control);
175 cmsg = CMSG_FIRSTHDR(&msgh);
177 VHOST_LOG_CONFIG(ERR, "cmsg == NULL\n");
181 cmsg->cmsg_len = CMSG_LEN(fdsize);
182 cmsg->cmsg_level = SOL_SOCKET;
183 cmsg->cmsg_type = SCM_RIGHTS;
184 memcpy(CMSG_DATA(cmsg), fds, fdsize);
186 msgh.msg_control = NULL;
187 msgh.msg_controllen = 0;
191 ret = sendmsg(sockfd, &msgh, MSG_NOSIGNAL);
192 } while (ret < 0 && errno == EINTR);
195 VHOST_LOG_CONFIG(ERR, "sendmsg error\n");
203 vhost_user_add_connection(int fd, struct vhost_user_socket *vsocket)
207 struct vhost_user_connection *conn;
209 struct virtio_net *dev;
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);
232 if (vsocket->dequeue_zero_copy)
233 vhost_enable_dequeue_zero_copy(vid);
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, "new device, handle is %d\n", vid);
250 if (vsocket->notify_ops->new_connection) {
251 ret = vsocket->notify_ops->new_connection(vid);
253 VHOST_LOG_CONFIG(ERR,
254 "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,
267 "failed to add fd %d into vhost server fdset\n",
270 if (vsocket->notify_ops->destroy_connection)
271 vsocket->notify_ops->destroy_connection(conn->vid);
276 pthread_mutex_lock(&vsocket->conn_mutex);
277 TAILQ_INSERT_TAIL(&vsocket->conn_list, conn, next);
278 pthread_mutex_unlock(&vsocket->conn_mutex);
280 fdset_pipe_notify(&vhost_user.fdset);
284 vhost_destroy_device(vid);
290 /* call back when there is new vhost-user connection from client */
292 vhost_user_server_new_connection(int fd, void *dat, int *remove __rte_unused)
294 struct vhost_user_socket *vsocket = dat;
296 fd = accept(fd, NULL, NULL);
300 VHOST_LOG_CONFIG(INFO, "new vhost user connection is %d\n", fd);
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, "vhost-user %s: socket created, fd: %d\n",
349 vsocket->is_server ? "server" : "client", fd);
351 if (!vsocket->is_server && fcntl(fd, F_SETFL, O_NONBLOCK)) {
352 VHOST_LOG_CONFIG(ERR,
353 "vhost-user: can't set nonblocking mode for socket, fd: "
354 "%d (%s)\n", 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,
388 "failed to bind to %s: %s; remove it and try again\n",
389 path, strerror(errno));
392 VHOST_LOG_CONFIG(INFO, "bind to %s\n", path);
394 ret = listen(fd, MAX_VIRTIO_BACKLOG);
398 ret = fdset_add(&vhost_user.fdset, fd, vhost_user_server_new_connection,
401 VHOST_LOG_CONFIG(ERR,
402 "failed to add listen fd %d to vhost server fdset\n",
414 struct vhost_user_reconnect {
415 struct sockaddr_un un;
417 struct vhost_user_socket *vsocket;
419 TAILQ_ENTRY(vhost_user_reconnect) next;
422 TAILQ_HEAD(vhost_user_reconnect_tailq_list, vhost_user_reconnect);
423 struct vhost_user_reconnect_list {
424 struct vhost_user_reconnect_tailq_list head;
425 pthread_mutex_t mutex;
428 static struct vhost_user_reconnect_list reconn_list;
429 static pthread_t reconn_tid;
432 vhost_user_connect_nonblock(int fd, struct sockaddr *un, size_t sz)
436 ret = connect(fd, un, sz);
437 if (ret < 0 && errno != EISCONN)
440 flags = fcntl(fd, F_GETFL, 0);
442 VHOST_LOG_CONFIG(ERR,
443 "can't get flags for connfd %d\n", fd);
446 if ((flags & O_NONBLOCK) && fcntl(fd, F_SETFL, flags & ~O_NONBLOCK)) {
447 VHOST_LOG_CONFIG(ERR,
448 "can't disable nonblocking on fd %d\n", fd);
455 vhost_user_client_reconnect(void *arg __rte_unused)
458 struct vhost_user_reconnect *reconn, *next;
461 pthread_mutex_lock(&reconn_list.mutex);
464 * An equal implementation of TAILQ_FOREACH_SAFE,
465 * which does not exist on all platforms.
467 for (reconn = TAILQ_FIRST(&reconn_list.head);
468 reconn != NULL; reconn = next) {
469 next = TAILQ_NEXT(reconn, next);
471 ret = vhost_user_connect_nonblock(reconn->fd,
472 (struct sockaddr *)&reconn->un,
476 VHOST_LOG_CONFIG(ERR,
477 "reconnection for fd %d failed\n",
484 VHOST_LOG_CONFIG(INFO,
485 "%s: connected\n", reconn->vsocket->path);
486 vhost_user_add_connection(reconn->fd, reconn->vsocket);
488 TAILQ_REMOVE(&reconn_list.head, reconn, next);
492 pthread_mutex_unlock(&reconn_list.mutex);
500 vhost_user_reconnect_init(void)
504 ret = pthread_mutex_init(&reconn_list.mutex, NULL);
506 VHOST_LOG_CONFIG(ERR, "failed to initialize mutex");
509 TAILQ_INIT(&reconn_list.head);
511 ret = rte_ctrl_thread_create(&reconn_tid, "vhost_reconn", NULL,
512 vhost_user_client_reconnect, NULL);
514 VHOST_LOG_CONFIG(ERR, "failed to create reconnect thread");
515 if (pthread_mutex_destroy(&reconn_list.mutex)) {
516 VHOST_LOG_CONFIG(ERR,
517 "failed to destroy reconnect mutex");
525 vhost_user_start_client(struct vhost_user_socket *vsocket)
528 int fd = vsocket->socket_fd;
529 const char *path = vsocket->path;
530 struct vhost_user_reconnect *reconn;
532 ret = vhost_user_connect_nonblock(fd, (struct sockaddr *)&vsocket->un,
533 sizeof(vsocket->un));
535 vhost_user_add_connection(fd, vsocket);
539 VHOST_LOG_CONFIG(WARNING,
540 "failed to connect to %s: %s\n",
541 path, strerror(errno));
543 if (ret == -2 || !vsocket->reconnect) {
548 VHOST_LOG_CONFIG(INFO, "%s: reconnecting...\n", path);
549 reconn = malloc(sizeof(*reconn));
550 if (reconn == NULL) {
551 VHOST_LOG_CONFIG(ERR,
552 "failed to allocate memory for reconnect\n");
556 reconn->un = vsocket->un;
558 reconn->vsocket = vsocket;
559 pthread_mutex_lock(&reconn_list.mutex);
560 TAILQ_INSERT_TAIL(&reconn_list.head, reconn, next);
561 pthread_mutex_unlock(&reconn_list.mutex);
566 static struct vhost_user_socket *
567 find_vhost_user_socket(const char *path)
574 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
575 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
577 if (!strcmp(vsocket->path, path))
585 rte_vhost_driver_attach_vdpa_device(const char *path,
586 struct rte_vdpa_device *dev)
588 struct vhost_user_socket *vsocket;
590 if (dev == NULL || path == NULL)
593 pthread_mutex_lock(&vhost_user.mutex);
594 vsocket = find_vhost_user_socket(path);
596 vsocket->vdpa_dev = dev;
597 pthread_mutex_unlock(&vhost_user.mutex);
599 return vsocket ? 0 : -1;
603 rte_vhost_driver_detach_vdpa_device(const char *path)
605 struct vhost_user_socket *vsocket;
607 pthread_mutex_lock(&vhost_user.mutex);
608 vsocket = find_vhost_user_socket(path);
610 vsocket->vdpa_dev = NULL;
611 pthread_mutex_unlock(&vhost_user.mutex);
613 return vsocket ? 0 : -1;
616 struct rte_vdpa_device *
617 rte_vhost_driver_get_vdpa_device(const char *path)
619 struct vhost_user_socket *vsocket;
620 struct rte_vdpa_device *dev = NULL;
622 pthread_mutex_lock(&vhost_user.mutex);
623 vsocket = find_vhost_user_socket(path);
625 dev = vsocket->vdpa_dev;
626 pthread_mutex_unlock(&vhost_user.mutex);
632 rte_vhost_driver_disable_features(const char *path, uint64_t features)
634 struct vhost_user_socket *vsocket;
636 pthread_mutex_lock(&vhost_user.mutex);
637 vsocket = find_vhost_user_socket(path);
639 /* Note that use_builtin_virtio_net is not affected by this function
640 * since callers may want to selectively disable features of the
641 * built-in vhost net device backend.
645 vsocket->features &= ~features;
646 pthread_mutex_unlock(&vhost_user.mutex);
648 return vsocket ? 0 : -1;
652 rte_vhost_driver_enable_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 if ((vsocket->supported_features & features) != features) {
661 * trying to enable features the driver doesn't
664 pthread_mutex_unlock(&vhost_user.mutex);
667 vsocket->features |= features;
669 pthread_mutex_unlock(&vhost_user.mutex);
671 return vsocket ? 0 : -1;
675 rte_vhost_driver_set_features(const char *path, uint64_t features)
677 struct vhost_user_socket *vsocket;
679 pthread_mutex_lock(&vhost_user.mutex);
680 vsocket = find_vhost_user_socket(path);
682 vsocket->supported_features = features;
683 vsocket->features = features;
685 /* Anyone setting feature bits is implementing their own vhost
688 vsocket->use_builtin_virtio_net = false;
690 pthread_mutex_unlock(&vhost_user.mutex);
692 return vsocket ? 0 : -1;
696 rte_vhost_driver_get_features(const char *path, uint64_t *features)
698 struct vhost_user_socket *vsocket;
699 uint64_t vdpa_features;
700 struct rte_vdpa_device *vdpa_dev;
703 pthread_mutex_lock(&vhost_user.mutex);
704 vsocket = find_vhost_user_socket(path);
706 VHOST_LOG_CONFIG(ERR,
707 "socket file %s is not registered yet.\n", path);
712 vdpa_dev = vsocket->vdpa_dev;
714 *features = vsocket->features;
718 if (vdpa_dev->ops->get_features(vdpa_dev, &vdpa_features) < 0) {
719 VHOST_LOG_CONFIG(ERR,
720 "failed to get vdpa features "
721 "for socket file %s.\n", path);
726 *features = vsocket->features & vdpa_features;
729 pthread_mutex_unlock(&vhost_user.mutex);
734 rte_vhost_driver_set_protocol_features(const char *path,
735 uint64_t protocol_features)
737 struct vhost_user_socket *vsocket;
739 pthread_mutex_lock(&vhost_user.mutex);
740 vsocket = find_vhost_user_socket(path);
742 vsocket->protocol_features = protocol_features;
743 pthread_mutex_unlock(&vhost_user.mutex);
744 return vsocket ? 0 : -1;
748 rte_vhost_driver_get_protocol_features(const char *path,
749 uint64_t *protocol_features)
751 struct vhost_user_socket *vsocket;
752 uint64_t vdpa_protocol_features;
753 struct rte_vdpa_device *vdpa_dev;
756 pthread_mutex_lock(&vhost_user.mutex);
757 vsocket = find_vhost_user_socket(path);
759 VHOST_LOG_CONFIG(ERR,
760 "socket file %s is not registered yet.\n", path);
765 vdpa_dev = vsocket->vdpa_dev;
767 *protocol_features = vsocket->protocol_features;
771 if (vdpa_dev->ops->get_protocol_features(vdpa_dev,
772 &vdpa_protocol_features) < 0) {
773 VHOST_LOG_CONFIG(ERR,
774 "failed to get vdpa protocol features "
775 "for socket file %s.\n", path);
780 *protocol_features = vsocket->protocol_features
781 & vdpa_protocol_features;
784 pthread_mutex_unlock(&vhost_user.mutex);
789 rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num)
791 struct vhost_user_socket *vsocket;
792 uint32_t vdpa_queue_num;
793 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 vdpa_dev = vsocket->vdpa_dev;
807 *queue_num = VHOST_MAX_QUEUE_PAIRS;
811 if (vdpa_dev->ops->get_queue_num(vdpa_dev, &vdpa_queue_num) < 0) {
812 VHOST_LOG_CONFIG(ERR,
813 "failed to get vdpa queue number "
814 "for socket file %s.\n", path);
819 *queue_num = RTE_MIN((uint32_t)VHOST_MAX_QUEUE_PAIRS, vdpa_queue_num);
822 pthread_mutex_unlock(&vhost_user.mutex);
827 vhost_user_socket_mem_free(struct vhost_user_socket *vsocket)
829 if (vsocket && vsocket->path) {
831 vsocket->path = NULL;
841 * Register a new vhost-user socket; here we could act as server
842 * (the default case), or client (when RTE_VHOST_USER_CLIENT) flag
846 rte_vhost_driver_register(const char *path, uint64_t flags)
849 struct vhost_user_socket *vsocket;
854 pthread_mutex_lock(&vhost_user.mutex);
856 if (vhost_user.vsocket_cnt == MAX_VHOST_SOCKET) {
857 VHOST_LOG_CONFIG(ERR,
858 "error: the number of vhost sockets reaches maximum\n");
862 vsocket = malloc(sizeof(struct vhost_user_socket));
865 memset(vsocket, 0, sizeof(struct vhost_user_socket));
866 vsocket->path = strdup(path);
867 if (vsocket->path == NULL) {
868 VHOST_LOG_CONFIG(ERR,
869 "error: failed to copy socket path string\n");
870 vhost_user_socket_mem_free(vsocket);
873 TAILQ_INIT(&vsocket->conn_list);
874 ret = pthread_mutex_init(&vsocket->conn_mutex, NULL);
876 VHOST_LOG_CONFIG(ERR,
877 "error: failed to init connection mutex\n");
880 vsocket->vdpa_dev = NULL;
881 vsocket->dequeue_zero_copy = flags & RTE_VHOST_USER_DEQUEUE_ZERO_COPY;
882 vsocket->extbuf = flags & RTE_VHOST_USER_EXTBUF_SUPPORT;
883 vsocket->linearbuf = flags & RTE_VHOST_USER_LINEARBUF_SUPPORT;
885 if (vsocket->dequeue_zero_copy &&
886 (flags & RTE_VHOST_USER_IOMMU_SUPPORT)) {
887 VHOST_LOG_CONFIG(ERR,
888 "error: enabling dequeue zero copy and IOMMU features "
889 "simultaneously is not supported\n");
893 vsocket->async_copy = flags & RTE_VHOST_USER_ASYNC_COPY;
895 if (vsocket->async_copy &&
896 (flags & (RTE_VHOST_USER_IOMMU_SUPPORT |
897 RTE_VHOST_USER_POSTCOPY_SUPPORT))) {
898 VHOST_LOG_CONFIG(ERR, "error: enabling async copy and IOMMU "
899 "or post-copy feature simultaneously is not "
905 * Set the supported features correctly for the builtin vhost-user
908 * Applications know nothing about features the builtin virtio net
909 * driver (virtio_net.c) supports, thus it's not possible for them
910 * to invoke rte_vhost_driver_set_features(). To workaround it, here
911 * we set it unconditionally. If the application want to implement
912 * another vhost-user driver (say SCSI), it should call the
913 * rte_vhost_driver_set_features(), which will overwrite following
916 vsocket->use_builtin_virtio_net = true;
917 vsocket->supported_features = VIRTIO_NET_SUPPORTED_FEATURES;
918 vsocket->features = VIRTIO_NET_SUPPORTED_FEATURES;
919 vsocket->protocol_features = VHOST_USER_PROTOCOL_FEATURES;
922 * Dequeue zero copy can't assure descriptors returned in order.
923 * Also, it requires that the guest memory is populated, which is
924 * not compatible with postcopy.
926 if (vsocket->dequeue_zero_copy) {
927 if (vsocket->extbuf) {
928 VHOST_LOG_CONFIG(ERR,
929 "error: zero copy is incompatible with external buffers\n");
933 if (vsocket->linearbuf) {
934 VHOST_LOG_CONFIG(ERR,
935 "error: zero copy is incompatible with linear buffers\n");
939 if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
940 VHOST_LOG_CONFIG(ERR,
941 "error: zero copy is incompatible with vhost client mode\n");
945 vsocket->supported_features &= ~(1ULL << VIRTIO_F_IN_ORDER);
946 vsocket->features &= ~(1ULL << VIRTIO_F_IN_ORDER);
948 VHOST_LOG_CONFIG(INFO,
949 "Dequeue zero copy requested, disabling postcopy support\n");
950 vsocket->protocol_features &=
951 ~(1ULL << VHOST_USER_PROTOCOL_F_PAGEFAULT);
954 if (vsocket->async_copy) {
955 vsocket->supported_features &= ~(1ULL << VHOST_F_LOG_ALL);
956 vsocket->features &= ~(1ULL << VHOST_F_LOG_ALL);
957 VHOST_LOG_CONFIG(INFO,
958 "Logging feature is disabled in async copy mode\n");
962 * We'll not be able to receive a buffer from guest in linear mode
963 * without external buffer if it will not fit in a single mbuf, which is
964 * likely if segmentation offloading enabled.
966 if (vsocket->linearbuf && !vsocket->extbuf) {
967 uint64_t seg_offload_features =
968 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
969 (1ULL << VIRTIO_NET_F_HOST_TSO6) |
970 (1ULL << VIRTIO_NET_F_HOST_UFO);
972 VHOST_LOG_CONFIG(INFO,
973 "Linear buffers requested without external buffers, "
974 "disabling host segmentation offloading support\n");
975 vsocket->supported_features &= ~seg_offload_features;
976 vsocket->features &= ~seg_offload_features;
979 if (!(flags & RTE_VHOST_USER_IOMMU_SUPPORT)) {
980 vsocket->supported_features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
981 vsocket->features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
984 if (!(flags & RTE_VHOST_USER_POSTCOPY_SUPPORT)) {
985 vsocket->protocol_features &=
986 ~(1ULL << VHOST_USER_PROTOCOL_F_PAGEFAULT);
988 #ifndef RTE_LIBRTE_VHOST_POSTCOPY
989 VHOST_LOG_CONFIG(ERR,
990 "Postcopy requested but not compiled\n");
996 if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
997 vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
998 if (vsocket->reconnect && reconn_tid == 0) {
999 if (vhost_user_reconnect_init() != 0)
1003 vsocket->is_server = true;
1005 ret = create_unix_socket(vsocket);
1010 vhost_user.vsockets[vhost_user.vsocket_cnt++] = vsocket;
1012 pthread_mutex_unlock(&vhost_user.mutex);
1016 if (pthread_mutex_destroy(&vsocket->conn_mutex)) {
1017 VHOST_LOG_CONFIG(ERR,
1018 "error: failed to destroy connection mutex\n");
1021 vhost_user_socket_mem_free(vsocket);
1023 pthread_mutex_unlock(&vhost_user.mutex);
1029 vhost_user_remove_reconnect(struct vhost_user_socket *vsocket)
1032 struct vhost_user_reconnect *reconn, *next;
1034 pthread_mutex_lock(&reconn_list.mutex);
1036 for (reconn = TAILQ_FIRST(&reconn_list.head);
1037 reconn != NULL; reconn = next) {
1038 next = TAILQ_NEXT(reconn, next);
1040 if (reconn->vsocket == vsocket) {
1041 TAILQ_REMOVE(&reconn_list.head, reconn, next);
1048 pthread_mutex_unlock(&reconn_list.mutex);
1053 * Unregister the specified vhost socket
1056 rte_vhost_driver_unregister(const char *path)
1060 struct vhost_user_connection *conn, *next;
1066 pthread_mutex_lock(&vhost_user.mutex);
1068 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
1069 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
1071 if (!strcmp(vsocket->path, path)) {
1072 pthread_mutex_lock(&vsocket->conn_mutex);
1073 for (conn = TAILQ_FIRST(&vsocket->conn_list);
1076 next = TAILQ_NEXT(conn, next);
1079 * If r/wcb is executing, release vsocket's
1080 * conn_mutex and vhost_user's mutex locks, and
1081 * try again since the r/wcb may use the
1082 * conn_mutex and mutex locks.
1084 if (fdset_try_del(&vhost_user.fdset,
1085 conn->connfd) == -1) {
1086 pthread_mutex_unlock(
1087 &vsocket->conn_mutex);
1088 pthread_mutex_unlock(&vhost_user.mutex);
1092 VHOST_LOG_CONFIG(INFO,
1093 "free connfd = %d for device '%s'\n",
1094 conn->connfd, path);
1095 close(conn->connfd);
1096 vhost_destroy_device(conn->vid);
1097 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
1100 pthread_mutex_unlock(&vsocket->conn_mutex);
1102 if (vsocket->is_server) {
1104 * If r/wcb is executing, release vhost_user's
1105 * mutex lock, and try again since the r/wcb
1106 * may use the mutex lock.
1108 if (fdset_try_del(&vhost_user.fdset,
1109 vsocket->socket_fd) == -1) {
1110 pthread_mutex_unlock(&vhost_user.mutex);
1114 close(vsocket->socket_fd);
1116 } else if (vsocket->reconnect) {
1117 vhost_user_remove_reconnect(vsocket);
1120 pthread_mutex_destroy(&vsocket->conn_mutex);
1121 vhost_user_socket_mem_free(vsocket);
1123 count = --vhost_user.vsocket_cnt;
1124 vhost_user.vsockets[i] = vhost_user.vsockets[count];
1125 vhost_user.vsockets[count] = NULL;
1126 pthread_mutex_unlock(&vhost_user.mutex);
1131 pthread_mutex_unlock(&vhost_user.mutex);
1137 * Register ops so that we can add/remove device to data core.
1140 rte_vhost_driver_callback_register(const char *path,
1141 struct vhost_device_ops const * const ops)
1143 struct vhost_user_socket *vsocket;
1145 pthread_mutex_lock(&vhost_user.mutex);
1146 vsocket = find_vhost_user_socket(path);
1148 vsocket->notify_ops = ops;
1149 pthread_mutex_unlock(&vhost_user.mutex);
1151 return vsocket ? 0 : -1;
1154 struct vhost_device_ops const *
1155 vhost_driver_callback_get(const char *path)
1157 struct vhost_user_socket *vsocket;
1159 pthread_mutex_lock(&vhost_user.mutex);
1160 vsocket = find_vhost_user_socket(path);
1161 pthread_mutex_unlock(&vhost_user.mutex);
1163 return vsocket ? vsocket->notify_ops : NULL;
1167 rte_vhost_driver_start(const char *path)
1169 struct vhost_user_socket *vsocket;
1170 static pthread_t fdset_tid;
1172 pthread_mutex_lock(&vhost_user.mutex);
1173 vsocket = find_vhost_user_socket(path);
1174 pthread_mutex_unlock(&vhost_user.mutex);
1179 if (fdset_tid == 0) {
1181 * create a pipe which will be waited by poll and notified to
1182 * rebuild the wait list of poll.
1184 if (fdset_pipe_init(&vhost_user.fdset) < 0) {
1185 VHOST_LOG_CONFIG(ERR,
1186 "failed to create pipe for vhost fdset\n");
1190 int ret = rte_ctrl_thread_create(&fdset_tid,
1191 "vhost-events", NULL, fdset_event_dispatch,
1194 VHOST_LOG_CONFIG(ERR,
1195 "failed to create fdset handling thread");
1197 fdset_pipe_uninit(&vhost_user.fdset);
1202 if (vsocket->is_server)
1203 return vhost_user_start_server(vsocket);
1205 return vhost_user_start_client(vsocket);