1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2016 Intel Corporation
11 #include <sys/types.h>
12 #include <sys/socket.h>
14 #include <sys/queue.h>
23 #include "vhost_user.h"
26 TAILQ_HEAD(vhost_user_connection_list, vhost_user_connection);
29 * Every time rte_vhost_driver_register() is invoked, an associated
30 * vhost_user_socket struct will be created.
32 struct vhost_user_socket {
33 struct vhost_user_connection_list conn_list;
34 pthread_mutex_t conn_mutex;
37 struct sockaddr_un un;
41 bool use_builtin_virtio_net;
45 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);
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_disable_features(const char *path, uint64_t features)
625 struct vhost_user_socket *vsocket;
627 pthread_mutex_lock(&vhost_user.mutex);
628 vsocket = find_vhost_user_socket(path);
630 /* Note that use_builtin_virtio_net is not affected by this function
631 * since callers may want to selectively disable features of the
632 * built-in vhost net device backend.
636 vsocket->features &= ~features;
637 pthread_mutex_unlock(&vhost_user.mutex);
639 return vsocket ? 0 : -1;
643 rte_vhost_driver_enable_features(const char *path, uint64_t features)
645 struct vhost_user_socket *vsocket;
647 pthread_mutex_lock(&vhost_user.mutex);
648 vsocket = find_vhost_user_socket(path);
650 if ((vsocket->supported_features & features) != features) {
652 * trying to enable features the driver doesn't
655 pthread_mutex_unlock(&vhost_user.mutex);
658 vsocket->features |= features;
660 pthread_mutex_unlock(&vhost_user.mutex);
662 return vsocket ? 0 : -1;
666 rte_vhost_driver_set_features(const char *path, uint64_t features)
668 struct vhost_user_socket *vsocket;
670 pthread_mutex_lock(&vhost_user.mutex);
671 vsocket = find_vhost_user_socket(path);
673 vsocket->supported_features = features;
674 vsocket->features = features;
676 /* Anyone setting feature bits is implementing their own vhost
679 vsocket->use_builtin_virtio_net = false;
681 pthread_mutex_unlock(&vhost_user.mutex);
683 return vsocket ? 0 : -1;
687 rte_vhost_driver_get_features(const char *path, uint64_t *features)
689 struct vhost_user_socket *vsocket;
690 uint64_t vdpa_features;
691 struct rte_vdpa_device *vdpa_dev;
694 pthread_mutex_lock(&vhost_user.mutex);
695 vsocket = find_vhost_user_socket(path);
697 VHOST_LOG_CONFIG(ERR, "(%s) socket file is not registered yet.\n", path);
702 vdpa_dev = vsocket->vdpa_dev;
704 *features = vsocket->features;
708 if (vdpa_dev->ops->get_features(vdpa_dev, &vdpa_features) < 0) {
709 VHOST_LOG_CONFIG(ERR, "(%s) failed to get vdpa features for socket file.\n", path);
714 *features = vsocket->features & vdpa_features;
717 pthread_mutex_unlock(&vhost_user.mutex);
722 rte_vhost_driver_set_protocol_features(const char *path,
723 uint64_t protocol_features)
725 struct vhost_user_socket *vsocket;
727 pthread_mutex_lock(&vhost_user.mutex);
728 vsocket = find_vhost_user_socket(path);
730 vsocket->protocol_features = protocol_features;
731 pthread_mutex_unlock(&vhost_user.mutex);
732 return vsocket ? 0 : -1;
736 rte_vhost_driver_get_protocol_features(const char *path,
737 uint64_t *protocol_features)
739 struct vhost_user_socket *vsocket;
740 uint64_t vdpa_protocol_features;
741 struct rte_vdpa_device *vdpa_dev;
744 pthread_mutex_lock(&vhost_user.mutex);
745 vsocket = find_vhost_user_socket(path);
747 VHOST_LOG_CONFIG(ERR, "(%s) socket file is not registered yet.\n", path);
752 vdpa_dev = vsocket->vdpa_dev;
754 *protocol_features = vsocket->protocol_features;
758 if (vdpa_dev->ops->get_protocol_features(vdpa_dev,
759 &vdpa_protocol_features) < 0) {
760 VHOST_LOG_CONFIG(ERR, "(%s) failed to get vdpa protocol features.\n",
766 *protocol_features = vsocket->protocol_features
767 & vdpa_protocol_features;
770 pthread_mutex_unlock(&vhost_user.mutex);
775 rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num)
777 struct vhost_user_socket *vsocket;
778 uint32_t vdpa_queue_num;
779 struct rte_vdpa_device *vdpa_dev;
782 pthread_mutex_lock(&vhost_user.mutex);
783 vsocket = find_vhost_user_socket(path);
785 VHOST_LOG_CONFIG(ERR, "(%s) socket file is not registered yet.\n", path);
790 vdpa_dev = vsocket->vdpa_dev;
792 *queue_num = VHOST_MAX_QUEUE_PAIRS;
796 if (vdpa_dev->ops->get_queue_num(vdpa_dev, &vdpa_queue_num) < 0) {
797 VHOST_LOG_CONFIG(ERR, "(%s) failed to get vdpa queue number.\n",
803 *queue_num = RTE_MIN((uint32_t)VHOST_MAX_QUEUE_PAIRS, vdpa_queue_num);
806 pthread_mutex_unlock(&vhost_user.mutex);
811 vhost_user_socket_mem_free(struct vhost_user_socket *vsocket)
813 if (vsocket && vsocket->path) {
815 vsocket->path = NULL;
825 * Register a new vhost-user socket; here we could act as server
826 * (the default case), or client (when RTE_VHOST_USER_CLIENT) flag
830 rte_vhost_driver_register(const char *path, uint64_t flags)
833 struct vhost_user_socket *vsocket;
838 pthread_mutex_lock(&vhost_user.mutex);
840 if (vhost_user.vsocket_cnt == MAX_VHOST_SOCKET) {
841 VHOST_LOG_CONFIG(ERR, "(%s) the number of vhost sockets reaches maximum\n",
846 vsocket = malloc(sizeof(struct vhost_user_socket));
849 memset(vsocket, 0, sizeof(struct vhost_user_socket));
850 vsocket->path = strdup(path);
851 if (vsocket->path == NULL) {
852 VHOST_LOG_CONFIG(ERR, "(%s) failed to copy socket path string\n", path);
853 vhost_user_socket_mem_free(vsocket);
856 TAILQ_INIT(&vsocket->conn_list);
857 ret = pthread_mutex_init(&vsocket->conn_mutex, NULL);
859 VHOST_LOG_CONFIG(ERR, "(%s) failed to init connection mutex\n", path);
862 vsocket->vdpa_dev = NULL;
863 vsocket->extbuf = flags & RTE_VHOST_USER_EXTBUF_SUPPORT;
864 vsocket->linearbuf = flags & RTE_VHOST_USER_LINEARBUF_SUPPORT;
865 vsocket->async_copy = flags & RTE_VHOST_USER_ASYNC_COPY;
866 vsocket->net_compliant_ol_flags = flags & RTE_VHOST_USER_NET_COMPLIANT_OL_FLAGS;
868 if (vsocket->async_copy &&
869 (flags & (RTE_VHOST_USER_IOMMU_SUPPORT |
870 RTE_VHOST_USER_POSTCOPY_SUPPORT))) {
871 VHOST_LOG_CONFIG(ERR, "(%s) enabling async copy and IOMMU "
872 "or post-copy feature simultaneously is not supported\n", path);
877 * Set the supported features correctly for the builtin vhost-user
880 * Applications know nothing about features the builtin virtio net
881 * driver (virtio_net.c) supports, thus it's not possible for them
882 * to invoke rte_vhost_driver_set_features(). To workaround it, here
883 * we set it unconditionally. If the application want to implement
884 * another vhost-user driver (say SCSI), it should call the
885 * rte_vhost_driver_set_features(), which will overwrite following
888 vsocket->use_builtin_virtio_net = true;
889 vsocket->supported_features = VIRTIO_NET_SUPPORTED_FEATURES;
890 vsocket->features = VIRTIO_NET_SUPPORTED_FEATURES;
891 vsocket->protocol_features = VHOST_USER_PROTOCOL_FEATURES;
893 if (vsocket->async_copy) {
894 vsocket->supported_features &= ~(1ULL << VHOST_F_LOG_ALL);
895 vsocket->features &= ~(1ULL << VHOST_F_LOG_ALL);
896 VHOST_LOG_CONFIG(INFO, "(%s) logging feature is disabled in async copy mode\n",
901 * We'll not be able to receive a buffer from guest in linear mode
902 * without external buffer if it will not fit in a single mbuf, which is
903 * likely if segmentation offloading enabled.
905 if (vsocket->linearbuf && !vsocket->extbuf) {
906 uint64_t seg_offload_features =
907 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
908 (1ULL << VIRTIO_NET_F_HOST_TSO6) |
909 (1ULL << VIRTIO_NET_F_HOST_UFO);
911 VHOST_LOG_CONFIG(INFO, "(%s) Linear buffers requested without external buffers, "
912 "disabling host segmentation offloading support\n", path);
913 vsocket->supported_features &= ~seg_offload_features;
914 vsocket->features &= ~seg_offload_features;
917 if (!(flags & RTE_VHOST_USER_IOMMU_SUPPORT)) {
918 vsocket->supported_features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
919 vsocket->features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
922 if (!(flags & RTE_VHOST_USER_POSTCOPY_SUPPORT)) {
923 vsocket->protocol_features &=
924 ~(1ULL << VHOST_USER_PROTOCOL_F_PAGEFAULT);
926 #ifndef RTE_LIBRTE_VHOST_POSTCOPY
927 VHOST_LOG_CONFIG(ERR, "(%s) Postcopy requested but not compiled\n", path);
933 if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
934 vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
935 if (vsocket->reconnect && reconn_tid == 0) {
936 if (vhost_user_reconnect_init() != 0)
940 vsocket->is_server = true;
942 ret = create_unix_socket(vsocket);
947 vhost_user.vsockets[vhost_user.vsocket_cnt++] = vsocket;
949 pthread_mutex_unlock(&vhost_user.mutex);
953 if (pthread_mutex_destroy(&vsocket->conn_mutex)) {
954 VHOST_LOG_CONFIG(ERR, "(%s) failed to destroy connection mutex\n", path);
957 vhost_user_socket_mem_free(vsocket);
959 pthread_mutex_unlock(&vhost_user.mutex);
965 vhost_user_remove_reconnect(struct vhost_user_socket *vsocket)
968 struct vhost_user_reconnect *reconn, *next;
970 pthread_mutex_lock(&reconn_list.mutex);
972 for (reconn = TAILQ_FIRST(&reconn_list.head);
973 reconn != NULL; reconn = next) {
974 next = TAILQ_NEXT(reconn, next);
976 if (reconn->vsocket == vsocket) {
977 TAILQ_REMOVE(&reconn_list.head, reconn, next);
984 pthread_mutex_unlock(&reconn_list.mutex);
989 * Unregister the specified vhost socket
992 rte_vhost_driver_unregister(const char *path)
996 struct vhost_user_connection *conn, *next;
1002 pthread_mutex_lock(&vhost_user.mutex);
1004 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
1005 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
1006 if (strcmp(vsocket->path, path))
1009 if (vsocket->is_server) {
1011 * If r/wcb is executing, release vhost_user's
1012 * mutex lock, and try again since the r/wcb
1013 * may use the mutex lock.
1015 if (fdset_try_del(&vhost_user.fdset, vsocket->socket_fd) == -1) {
1016 pthread_mutex_unlock(&vhost_user.mutex);
1019 } else if (vsocket->reconnect) {
1020 vhost_user_remove_reconnect(vsocket);
1023 pthread_mutex_lock(&vsocket->conn_mutex);
1024 for (conn = TAILQ_FIRST(&vsocket->conn_list);
1027 next = TAILQ_NEXT(conn, next);
1030 * If r/wcb is executing, release vsocket's
1031 * conn_mutex and vhost_user's mutex locks, and
1032 * try again since the r/wcb may use the
1033 * conn_mutex and mutex locks.
1035 if (fdset_try_del(&vhost_user.fdset,
1036 conn->connfd) == -1) {
1037 pthread_mutex_unlock(&vsocket->conn_mutex);
1038 pthread_mutex_unlock(&vhost_user.mutex);
1042 VHOST_LOG_CONFIG(INFO, "(%s) free connfd %d\n", path, conn->connfd);
1043 close(conn->connfd);
1044 vhost_destroy_device(conn->vid);
1045 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
1048 pthread_mutex_unlock(&vsocket->conn_mutex);
1050 if (vsocket->is_server) {
1051 close(vsocket->socket_fd);
1055 pthread_mutex_destroy(&vsocket->conn_mutex);
1056 vhost_user_socket_mem_free(vsocket);
1058 count = --vhost_user.vsocket_cnt;
1059 vhost_user.vsockets[i] = vhost_user.vsockets[count];
1060 vhost_user.vsockets[count] = NULL;
1061 pthread_mutex_unlock(&vhost_user.mutex);
1064 pthread_mutex_unlock(&vhost_user.mutex);
1070 * Register ops so that we can add/remove device to data core.
1073 rte_vhost_driver_callback_register(const char *path,
1074 struct rte_vhost_device_ops const * const ops)
1076 struct vhost_user_socket *vsocket;
1078 pthread_mutex_lock(&vhost_user.mutex);
1079 vsocket = find_vhost_user_socket(path);
1081 vsocket->notify_ops = ops;
1082 pthread_mutex_unlock(&vhost_user.mutex);
1084 return vsocket ? 0 : -1;
1087 struct rte_vhost_device_ops const *
1088 vhost_driver_callback_get(const char *path)
1090 struct vhost_user_socket *vsocket;
1092 pthread_mutex_lock(&vhost_user.mutex);
1093 vsocket = find_vhost_user_socket(path);
1094 pthread_mutex_unlock(&vhost_user.mutex);
1096 return vsocket ? vsocket->notify_ops : NULL;
1100 rte_vhost_driver_start(const char *path)
1102 struct vhost_user_socket *vsocket;
1103 static pthread_t fdset_tid;
1105 pthread_mutex_lock(&vhost_user.mutex);
1106 vsocket = find_vhost_user_socket(path);
1107 pthread_mutex_unlock(&vhost_user.mutex);
1112 if (fdset_tid == 0) {
1114 * create a pipe which will be waited by poll and notified to
1115 * rebuild the wait list of poll.
1117 if (fdset_pipe_init(&vhost_user.fdset) < 0) {
1118 VHOST_LOG_CONFIG(ERR, "(%s) failed to create pipe for vhost fdset\n", path);
1122 int ret = rte_ctrl_thread_create(&fdset_tid,
1123 "vhost-events", NULL, fdset_event_dispatch,
1126 VHOST_LOG_CONFIG(ERR, "(%s) failed to create fdset handling thread", path);
1128 fdset_pipe_uninit(&vhost_user.fdset);
1133 if (vsocket->is_server)
1134 return vhost_user_start_server(vsocket);
1136 return vhost_user_start_client(vsocket);