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;
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 rte_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(char *ifname, 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, "(%s) recvmsg failed on fd %d (%s)\n",
127 ifname, sockfd, strerror(errno));
131 if (msgh.msg_flags & (MSG_TRUNC | MSG_CTRUNC)) {
132 VHOST_LOG_CONFIG(ERR, "(%s) truncated msg (fd %d)\n", ifname, sockfd);
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(char *ifname, 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, "(%s) cmsg == NULL\n", ifname);
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, "(%s) sendmsg error on fd %d (%s)\n",
196 ifname, sockfd, strerror(errno));
204 vhost_user_add_connection(int fd, struct vhost_user_socket *vsocket)
208 struct vhost_user_connection *conn;
210 struct virtio_net *dev;
215 conn = malloc(sizeof(*conn));
221 vid = vhost_new_device();
226 size = strnlen(vsocket->path, PATH_MAX);
227 vhost_set_ifname(vid, vsocket->path, size);
229 vhost_setup_virtio_net(vid, vsocket->use_builtin_virtio_net,
230 vsocket->net_compliant_ol_flags);
232 vhost_attach_vdpa_device(vid, vsocket->vdpa_dev);
235 vhost_enable_extbuf(vid);
237 if (vsocket->linearbuf)
238 vhost_enable_linearbuf(vid);
240 if (vsocket->async_copy) {
241 dev = get_device(vid);
247 VHOST_LOG_CONFIG(INFO, "(%s) new device, handle is %d\n", vsocket->path, vid);
249 if (vsocket->notify_ops->new_connection) {
250 ret = vsocket->notify_ops->new_connection(vid);
252 VHOST_LOG_CONFIG(ERR,
253 "(%s) failed to add vhost user connection with fd %d\n",
260 conn->vsocket = vsocket;
262 ret = fdset_add(&vhost_user.fdset, fd, vhost_user_read_cb,
265 VHOST_LOG_CONFIG(ERR, "(%s) failed to add fd %d into vhost server fdset\n",
268 if (vsocket->notify_ops->destroy_connection)
269 vsocket->notify_ops->destroy_connection(conn->vid);
274 pthread_mutex_lock(&vsocket->conn_mutex);
275 TAILQ_INSERT_TAIL(&vsocket->conn_list, conn, next);
276 pthread_mutex_unlock(&vsocket->conn_mutex);
278 fdset_pipe_notify(&vhost_user.fdset);
282 vhost_destroy_device(vid);
288 /* call back when there is new vhost-user connection from client */
290 vhost_user_server_new_connection(int fd, void *dat, int *remove __rte_unused)
292 struct vhost_user_socket *vsocket = dat;
294 fd = accept(fd, NULL, NULL);
298 VHOST_LOG_CONFIG(INFO, "(%s) new vhost user connection is %d\n",
300 vhost_user_add_connection(fd, vsocket);
304 vhost_user_read_cb(int connfd, void *dat, int *remove)
306 struct vhost_user_connection *conn = dat;
307 struct vhost_user_socket *vsocket = conn->vsocket;
310 ret = vhost_user_msg_handler(conn->vid, connfd);
312 struct virtio_net *dev = get_device(conn->vid);
318 vhost_destroy_device_notify(dev);
320 if (vsocket->notify_ops->destroy_connection)
321 vsocket->notify_ops->destroy_connection(conn->vid);
323 vhost_destroy_device(conn->vid);
325 if (vsocket->reconnect) {
326 create_unix_socket(vsocket);
327 vhost_user_start_client(vsocket);
330 pthread_mutex_lock(&vsocket->conn_mutex);
331 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
332 pthread_mutex_unlock(&vsocket->conn_mutex);
339 create_unix_socket(struct vhost_user_socket *vsocket)
342 struct sockaddr_un *un = &vsocket->un;
344 fd = socket(AF_UNIX, SOCK_STREAM, 0);
347 VHOST_LOG_CONFIG(INFO, "(%s) vhost-user %s: socket created, fd: %d\n",
348 vsocket->path, vsocket->is_server ? "server" : "client", fd);
350 if (!vsocket->is_server && fcntl(fd, F_SETFL, O_NONBLOCK)) {
351 VHOST_LOG_CONFIG(ERR,
352 "(%s) vhost-user: can't set nonblocking mode for socket, fd: %d (%s)\n",
353 vsocket->path, fd, strerror(errno));
358 memset(un, 0, sizeof(*un));
359 un->sun_family = AF_UNIX;
360 strncpy(un->sun_path, vsocket->path, sizeof(un->sun_path));
361 un->sun_path[sizeof(un->sun_path) - 1] = '\0';
363 vsocket->socket_fd = fd;
368 vhost_user_start_server(struct vhost_user_socket *vsocket)
371 int fd = vsocket->socket_fd;
372 const char *path = vsocket->path;
375 * bind () may fail if the socket file with the same name already
376 * exists. But the library obviously should not delete the file
377 * provided by the user, since we can not be sure that it is not
378 * being used by other applications. Moreover, many applications form
379 * socket names based on user input, which is prone to errors.
381 * The user must ensure that the socket does not exist before
382 * registering the vhost driver in server mode.
384 ret = bind(fd, (struct sockaddr *)&vsocket->un, sizeof(vsocket->un));
386 VHOST_LOG_CONFIG(ERR, "(%s) failed to bind: %s; remove it and try again\n",
387 path, strerror(errno));
390 VHOST_LOG_CONFIG(INFO, "(%s) binding succeeded\n", path);
392 ret = listen(fd, MAX_VIRTIO_BACKLOG);
396 ret = fdset_add(&vhost_user.fdset, fd, vhost_user_server_new_connection,
399 VHOST_LOG_CONFIG(ERR,
400 "(%s) failed to add listen fd %d to vhost server fdset\n",
412 struct vhost_user_reconnect {
413 struct sockaddr_un un;
415 struct vhost_user_socket *vsocket;
417 TAILQ_ENTRY(vhost_user_reconnect) next;
420 TAILQ_HEAD(vhost_user_reconnect_tailq_list, vhost_user_reconnect);
421 struct vhost_user_reconnect_list {
422 struct vhost_user_reconnect_tailq_list head;
423 pthread_mutex_t mutex;
426 static struct vhost_user_reconnect_list reconn_list;
427 static pthread_t reconn_tid;
430 vhost_user_connect_nonblock(char *path, int fd, struct sockaddr *un, size_t sz)
434 ret = connect(fd, un, sz);
435 if (ret < 0 && errno != EISCONN)
438 flags = fcntl(fd, F_GETFL, 0);
440 VHOST_LOG_CONFIG(ERR, "(%s) can't get flags for connfd %d (%s)\n",
441 path, fd, strerror(errno));
444 if ((flags & O_NONBLOCK) && fcntl(fd, F_SETFL, flags & ~O_NONBLOCK)) {
445 VHOST_LOG_CONFIG(ERR, "(%s) can't disable nonblocking on fd %d\n", path, fd);
452 vhost_user_client_reconnect(void *arg __rte_unused)
455 struct vhost_user_reconnect *reconn, *next;
458 pthread_mutex_lock(&reconn_list.mutex);
461 * An equal implementation of TAILQ_FOREACH_SAFE,
462 * which does not exist on all platforms.
464 for (reconn = TAILQ_FIRST(&reconn_list.head);
465 reconn != NULL; reconn = next) {
466 next = TAILQ_NEXT(reconn, next);
468 ret = vhost_user_connect_nonblock(reconn->vsocket->path, reconn->fd,
469 (struct sockaddr *)&reconn->un,
473 VHOST_LOG_CONFIG(ERR, "(%s) reconnection for fd %d failed\n",
474 reconn->vsocket->path, reconn->fd);
480 VHOST_LOG_CONFIG(INFO, "(%s) connected\n", reconn->vsocket->path);
481 vhost_user_add_connection(reconn->fd, reconn->vsocket);
483 TAILQ_REMOVE(&reconn_list.head, reconn, next);
487 pthread_mutex_unlock(&reconn_list.mutex);
495 vhost_user_reconnect_init(void)
499 ret = pthread_mutex_init(&reconn_list.mutex, NULL);
501 VHOST_LOG_CONFIG(ERR, "%s: failed to initialize mutex", __func__);
504 TAILQ_INIT(&reconn_list.head);
506 ret = rte_ctrl_thread_create(&reconn_tid, "vhost_reconn", NULL,
507 vhost_user_client_reconnect, NULL);
509 VHOST_LOG_CONFIG(ERR, "failed to create reconnect thread");
510 if (pthread_mutex_destroy(&reconn_list.mutex))
511 VHOST_LOG_CONFIG(ERR, "%s: failed to destroy reconnect mutex", __func__);
518 vhost_user_start_client(struct vhost_user_socket *vsocket)
521 int fd = vsocket->socket_fd;
522 const char *path = vsocket->path;
523 struct vhost_user_reconnect *reconn;
525 ret = vhost_user_connect_nonblock(vsocket->path, fd, (struct sockaddr *)&vsocket->un,
526 sizeof(vsocket->un));
528 vhost_user_add_connection(fd, vsocket);
532 VHOST_LOG_CONFIG(WARNING, "(%s) failed to connect: %s\n", path, strerror(errno));
534 if (ret == -2 || !vsocket->reconnect) {
539 VHOST_LOG_CONFIG(INFO, "(%s) reconnecting...\n", path);
540 reconn = malloc(sizeof(*reconn));
541 if (reconn == NULL) {
542 VHOST_LOG_CONFIG(ERR, "(%s) failed to allocate memory for reconnect\n", path);
546 reconn->un = vsocket->un;
548 reconn->vsocket = vsocket;
549 pthread_mutex_lock(&reconn_list.mutex);
550 TAILQ_INSERT_TAIL(&reconn_list.head, reconn, next);
551 pthread_mutex_unlock(&reconn_list.mutex);
556 static struct vhost_user_socket *
557 find_vhost_user_socket(const char *path)
564 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
565 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
567 if (!strcmp(vsocket->path, path))
575 rte_vhost_driver_attach_vdpa_device(const char *path,
576 struct rte_vdpa_device *dev)
578 struct vhost_user_socket *vsocket;
580 if (dev == NULL || path == NULL)
583 pthread_mutex_lock(&vhost_user.mutex);
584 vsocket = find_vhost_user_socket(path);
586 vsocket->vdpa_dev = dev;
587 pthread_mutex_unlock(&vhost_user.mutex);
589 return vsocket ? 0 : -1;
593 rte_vhost_driver_detach_vdpa_device(const char *path)
595 struct vhost_user_socket *vsocket;
597 pthread_mutex_lock(&vhost_user.mutex);
598 vsocket = find_vhost_user_socket(path);
600 vsocket->vdpa_dev = NULL;
601 pthread_mutex_unlock(&vhost_user.mutex);
603 return vsocket ? 0 : -1;
606 struct rte_vdpa_device *
607 rte_vhost_driver_get_vdpa_device(const char *path)
609 struct vhost_user_socket *vsocket;
610 struct rte_vdpa_device *dev = NULL;
612 pthread_mutex_lock(&vhost_user.mutex);
613 vsocket = find_vhost_user_socket(path);
615 dev = vsocket->vdpa_dev;
616 pthread_mutex_unlock(&vhost_user.mutex);
622 rte_vhost_driver_disable_features(const char *path, uint64_t features)
624 struct vhost_user_socket *vsocket;
626 pthread_mutex_lock(&vhost_user.mutex);
627 vsocket = find_vhost_user_socket(path);
629 /* Note that use_builtin_virtio_net is not affected by this function
630 * since callers may want to selectively disable features of the
631 * built-in vhost net device backend.
635 vsocket->features &= ~features;
636 pthread_mutex_unlock(&vhost_user.mutex);
638 return vsocket ? 0 : -1;
642 rte_vhost_driver_enable_features(const char *path, uint64_t features)
644 struct vhost_user_socket *vsocket;
646 pthread_mutex_lock(&vhost_user.mutex);
647 vsocket = find_vhost_user_socket(path);
649 if ((vsocket->supported_features & features) != features) {
651 * trying to enable features the driver doesn't
654 pthread_mutex_unlock(&vhost_user.mutex);
657 vsocket->features |= features;
659 pthread_mutex_unlock(&vhost_user.mutex);
661 return vsocket ? 0 : -1;
665 rte_vhost_driver_set_features(const char *path, uint64_t features)
667 struct vhost_user_socket *vsocket;
669 pthread_mutex_lock(&vhost_user.mutex);
670 vsocket = find_vhost_user_socket(path);
672 vsocket->supported_features = features;
673 vsocket->features = features;
675 /* Anyone setting feature bits is implementing their own vhost
678 vsocket->use_builtin_virtio_net = false;
680 pthread_mutex_unlock(&vhost_user.mutex);
682 return vsocket ? 0 : -1;
686 rte_vhost_driver_get_features(const char *path, uint64_t *features)
688 struct vhost_user_socket *vsocket;
689 uint64_t vdpa_features;
690 struct rte_vdpa_device *vdpa_dev;
693 pthread_mutex_lock(&vhost_user.mutex);
694 vsocket = find_vhost_user_socket(path);
696 VHOST_LOG_CONFIG(ERR, "(%s) socket file is not registered yet.\n", path);
701 vdpa_dev = vsocket->vdpa_dev;
703 *features = vsocket->features;
707 if (vdpa_dev->ops->get_features(vdpa_dev, &vdpa_features) < 0) {
708 VHOST_LOG_CONFIG(ERR, "(%s) failed to get vdpa features for socket file.\n", path);
713 *features = vsocket->features & vdpa_features;
716 pthread_mutex_unlock(&vhost_user.mutex);
721 rte_vhost_driver_set_protocol_features(const char *path,
722 uint64_t protocol_features)
724 struct vhost_user_socket *vsocket;
726 pthread_mutex_lock(&vhost_user.mutex);
727 vsocket = find_vhost_user_socket(path);
729 vsocket->protocol_features = protocol_features;
730 pthread_mutex_unlock(&vhost_user.mutex);
731 return vsocket ? 0 : -1;
735 rte_vhost_driver_get_protocol_features(const char *path,
736 uint64_t *protocol_features)
738 struct vhost_user_socket *vsocket;
739 uint64_t vdpa_protocol_features;
740 struct rte_vdpa_device *vdpa_dev;
743 pthread_mutex_lock(&vhost_user.mutex);
744 vsocket = find_vhost_user_socket(path);
746 VHOST_LOG_CONFIG(ERR, "(%s) socket file is not registered yet.\n", path);
751 vdpa_dev = vsocket->vdpa_dev;
753 *protocol_features = vsocket->protocol_features;
757 if (vdpa_dev->ops->get_protocol_features(vdpa_dev,
758 &vdpa_protocol_features) < 0) {
759 VHOST_LOG_CONFIG(ERR, "(%s) failed to get vdpa protocol features.\n",
765 *protocol_features = vsocket->protocol_features
766 & vdpa_protocol_features;
769 pthread_mutex_unlock(&vhost_user.mutex);
774 rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num)
776 struct vhost_user_socket *vsocket;
777 uint32_t vdpa_queue_num;
778 struct rte_vdpa_device *vdpa_dev;
781 pthread_mutex_lock(&vhost_user.mutex);
782 vsocket = find_vhost_user_socket(path);
784 VHOST_LOG_CONFIG(ERR, "(%s) socket file is not registered yet.\n", path);
789 vdpa_dev = vsocket->vdpa_dev;
791 *queue_num = VHOST_MAX_QUEUE_PAIRS;
795 if (vdpa_dev->ops->get_queue_num(vdpa_dev, &vdpa_queue_num) < 0) {
796 VHOST_LOG_CONFIG(ERR, "(%s) failed to get vdpa queue number.\n",
802 *queue_num = RTE_MIN((uint32_t)VHOST_MAX_QUEUE_PAIRS, vdpa_queue_num);
805 pthread_mutex_unlock(&vhost_user.mutex);
810 vhost_user_socket_mem_free(struct vhost_user_socket *vsocket)
812 if (vsocket && vsocket->path) {
814 vsocket->path = NULL;
824 * Register a new vhost-user socket; here we could act as server
825 * (the default case), or client (when RTE_VHOST_USER_CLIENT) flag
829 rte_vhost_driver_register(const char *path, uint64_t flags)
832 struct vhost_user_socket *vsocket;
837 pthread_mutex_lock(&vhost_user.mutex);
839 if (vhost_user.vsocket_cnt == MAX_VHOST_SOCKET) {
840 VHOST_LOG_CONFIG(ERR, "(%s) the number of vhost sockets reaches maximum\n",
845 vsocket = malloc(sizeof(struct vhost_user_socket));
848 memset(vsocket, 0, sizeof(struct vhost_user_socket));
849 vsocket->path = strdup(path);
850 if (vsocket->path == NULL) {
851 VHOST_LOG_CONFIG(ERR, "(%s) failed to copy socket path string\n", path);
852 vhost_user_socket_mem_free(vsocket);
855 TAILQ_INIT(&vsocket->conn_list);
856 ret = pthread_mutex_init(&vsocket->conn_mutex, NULL);
858 VHOST_LOG_CONFIG(ERR, "(%s) failed to init connection mutex\n", path);
861 vsocket->vdpa_dev = NULL;
862 vsocket->extbuf = flags & RTE_VHOST_USER_EXTBUF_SUPPORT;
863 vsocket->linearbuf = flags & RTE_VHOST_USER_LINEARBUF_SUPPORT;
864 vsocket->async_copy = flags & RTE_VHOST_USER_ASYNC_COPY;
865 vsocket->net_compliant_ol_flags = flags & RTE_VHOST_USER_NET_COMPLIANT_OL_FLAGS;
867 if (vsocket->async_copy &&
868 (flags & (RTE_VHOST_USER_IOMMU_SUPPORT |
869 RTE_VHOST_USER_POSTCOPY_SUPPORT))) {
870 VHOST_LOG_CONFIG(ERR, "(%s) async copy with IOMMU or post-copy not supported\n",
876 * Set the supported features correctly for the builtin vhost-user
879 * Applications know nothing about features the builtin virtio net
880 * driver (virtio_net.c) supports, thus it's not possible for them
881 * to invoke rte_vhost_driver_set_features(). To workaround it, here
882 * we set it unconditionally. If the application want to implement
883 * another vhost-user driver (say SCSI), it should call the
884 * rte_vhost_driver_set_features(), which will overwrite following
887 vsocket->use_builtin_virtio_net = true;
888 vsocket->supported_features = VIRTIO_NET_SUPPORTED_FEATURES;
889 vsocket->features = VIRTIO_NET_SUPPORTED_FEATURES;
890 vsocket->protocol_features = VHOST_USER_PROTOCOL_FEATURES;
892 if (vsocket->async_copy) {
893 vsocket->supported_features &= ~(1ULL << VHOST_F_LOG_ALL);
894 vsocket->features &= ~(1ULL << VHOST_F_LOG_ALL);
895 VHOST_LOG_CONFIG(INFO, "(%s) logging feature is disabled in async copy mode\n",
900 * We'll not be able to receive a buffer from guest in linear mode
901 * without external buffer if it will not fit in a single mbuf, which is
902 * likely if segmentation offloading enabled.
904 if (vsocket->linearbuf && !vsocket->extbuf) {
905 uint64_t seg_offload_features =
906 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
907 (1ULL << VIRTIO_NET_F_HOST_TSO6) |
908 (1ULL << VIRTIO_NET_F_HOST_UFO);
910 VHOST_LOG_CONFIG(INFO, "(%s) Linear buffers requested without external buffers,\n",
912 VHOST_LOG_CONFIG(INFO, "(%s) disabling host segmentation offloading support\n",
914 vsocket->supported_features &= ~seg_offload_features;
915 vsocket->features &= ~seg_offload_features;
918 if (!(flags & RTE_VHOST_USER_IOMMU_SUPPORT)) {
919 vsocket->supported_features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
920 vsocket->features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
923 if (!(flags & RTE_VHOST_USER_POSTCOPY_SUPPORT)) {
924 vsocket->protocol_features &=
925 ~(1ULL << VHOST_USER_PROTOCOL_F_PAGEFAULT);
927 #ifndef RTE_LIBRTE_VHOST_POSTCOPY
928 VHOST_LOG_CONFIG(ERR, "(%s) Postcopy requested but not compiled\n", path);
934 if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
935 vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
936 if (vsocket->reconnect && reconn_tid == 0) {
937 if (vhost_user_reconnect_init() != 0)
941 vsocket->is_server = true;
943 ret = create_unix_socket(vsocket);
948 vhost_user.vsockets[vhost_user.vsocket_cnt++] = vsocket;
950 pthread_mutex_unlock(&vhost_user.mutex);
954 if (pthread_mutex_destroy(&vsocket->conn_mutex)) {
955 VHOST_LOG_CONFIG(ERR, "(%s) failed to destroy connection mutex\n", path);
958 vhost_user_socket_mem_free(vsocket);
960 pthread_mutex_unlock(&vhost_user.mutex);
966 vhost_user_remove_reconnect(struct vhost_user_socket *vsocket)
969 struct vhost_user_reconnect *reconn, *next;
971 pthread_mutex_lock(&reconn_list.mutex);
973 for (reconn = TAILQ_FIRST(&reconn_list.head);
974 reconn != NULL; reconn = next) {
975 next = TAILQ_NEXT(reconn, next);
977 if (reconn->vsocket == vsocket) {
978 TAILQ_REMOVE(&reconn_list.head, reconn, next);
985 pthread_mutex_unlock(&reconn_list.mutex);
990 * Unregister the specified vhost socket
993 rte_vhost_driver_unregister(const char *path)
997 struct vhost_user_connection *conn, *next;
1003 pthread_mutex_lock(&vhost_user.mutex);
1005 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
1006 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
1007 if (strcmp(vsocket->path, path))
1010 if (vsocket->is_server) {
1012 * If r/wcb is executing, release vhost_user's
1013 * mutex lock, and try again since the r/wcb
1014 * may use the mutex lock.
1016 if (fdset_try_del(&vhost_user.fdset, vsocket->socket_fd) == -1) {
1017 pthread_mutex_unlock(&vhost_user.mutex);
1020 } else if (vsocket->reconnect) {
1021 vhost_user_remove_reconnect(vsocket);
1024 pthread_mutex_lock(&vsocket->conn_mutex);
1025 for (conn = TAILQ_FIRST(&vsocket->conn_list);
1028 next = TAILQ_NEXT(conn, next);
1031 * If r/wcb is executing, release vsocket's
1032 * conn_mutex and vhost_user's mutex locks, and
1033 * try again since the r/wcb may use the
1034 * conn_mutex and mutex locks.
1036 if (fdset_try_del(&vhost_user.fdset,
1037 conn->connfd) == -1) {
1038 pthread_mutex_unlock(&vsocket->conn_mutex);
1039 pthread_mutex_unlock(&vhost_user.mutex);
1043 VHOST_LOG_CONFIG(INFO, "(%s) free connfd %d\n", path, conn->connfd);
1044 close(conn->connfd);
1045 vhost_destroy_device(conn->vid);
1046 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
1049 pthread_mutex_unlock(&vsocket->conn_mutex);
1051 if (vsocket->is_server) {
1052 close(vsocket->socket_fd);
1056 pthread_mutex_destroy(&vsocket->conn_mutex);
1057 vhost_user_socket_mem_free(vsocket);
1059 count = --vhost_user.vsocket_cnt;
1060 vhost_user.vsockets[i] = vhost_user.vsockets[count];
1061 vhost_user.vsockets[count] = NULL;
1062 pthread_mutex_unlock(&vhost_user.mutex);
1065 pthread_mutex_unlock(&vhost_user.mutex);
1071 * Register ops so that we can add/remove device to data core.
1074 rte_vhost_driver_callback_register(const char *path,
1075 struct rte_vhost_device_ops const * const ops)
1077 struct vhost_user_socket *vsocket;
1079 pthread_mutex_lock(&vhost_user.mutex);
1080 vsocket = find_vhost_user_socket(path);
1082 vsocket->notify_ops = ops;
1083 pthread_mutex_unlock(&vhost_user.mutex);
1085 return vsocket ? 0 : -1;
1088 struct rte_vhost_device_ops const *
1089 vhost_driver_callback_get(const char *path)
1091 struct vhost_user_socket *vsocket;
1093 pthread_mutex_lock(&vhost_user.mutex);
1094 vsocket = find_vhost_user_socket(path);
1095 pthread_mutex_unlock(&vhost_user.mutex);
1097 return vsocket ? vsocket->notify_ops : NULL;
1101 rte_vhost_driver_start(const char *path)
1103 struct vhost_user_socket *vsocket;
1104 static pthread_t fdset_tid;
1106 pthread_mutex_lock(&vhost_user.mutex);
1107 vsocket = find_vhost_user_socket(path);
1108 pthread_mutex_unlock(&vhost_user.mutex);
1113 if (fdset_tid == 0) {
1115 * create a pipe which will be waited by poll and notified to
1116 * rebuild the wait list of poll.
1118 if (fdset_pipe_init(&vhost_user.fdset) < 0) {
1119 VHOST_LOG_CONFIG(ERR, "(%s) failed to create pipe for vhost fdset\n", path);
1123 int ret = rte_ctrl_thread_create(&fdset_tid,
1124 "vhost-events", NULL, fdset_event_dispatch,
1127 VHOST_LOG_CONFIG(ERR, "(%s) failed to create fdset handling thread", path);
1129 fdset_pipe_uninit(&vhost_user.fdset);
1134 if (vsocket->is_server)
1135 return vhost_user_start_server(vsocket);
1137 return vhost_user_start_client(vsocket);