1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2016 Intel Corporation
12 #include <sys/types.h>
13 #include <sys/socket.h>
15 #include <sys/queue.h>
24 #include "vhost_user.h"
27 TAILQ_HEAD(vhost_user_connection_list, vhost_user_connection);
30 * Every time rte_vhost_driver_register() is invoked, an associated
31 * vhost_user_socket struct will be created.
33 struct vhost_user_socket {
34 struct vhost_user_connection_list conn_list;
35 pthread_mutex_t conn_mutex;
38 struct sockaddr_un un;
41 bool dequeue_zero_copy;
43 bool use_builtin_virtio_net;
46 * The "supported_features" indicates the feature bits the
47 * vhost driver supports. The "features" indicates the feature
48 * bits after the rte_vhost_driver_features_disable/enable().
49 * It is also the final feature bits used for vhost-user
50 * features negotiation.
52 uint64_t supported_features;
55 struct vhost_device_ops const *notify_ops;
58 struct vhost_user_connection {
59 struct vhost_user_socket *vsocket;
63 TAILQ_ENTRY(vhost_user_connection) next;
66 #define MAX_VHOST_SOCKET 1024
68 struct vhost_user_socket *vsockets[MAX_VHOST_SOCKET];
71 pthread_mutex_t mutex;
74 #define MAX_VIRTIO_BACKLOG 128
76 static void vhost_user_server_new_connection(int fd, void *data, int *remove);
77 static void vhost_user_read_cb(int fd, void *dat, int *remove);
78 static int create_unix_socket(struct vhost_user_socket *vsocket);
79 static int vhost_user_start_client(struct vhost_user_socket *vsocket);
81 static struct vhost_user vhost_user = {
83 .fd = { [0 ... MAX_FDS - 1] = {-1, NULL, NULL, NULL, 0} },
84 .fd_mutex = PTHREAD_MUTEX_INITIALIZER,
88 .mutex = PTHREAD_MUTEX_INITIALIZER,
91 /* return bytes# of read on success or negative val on failure. */
93 read_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
97 size_t fdsize = fd_num * sizeof(int);
98 char control[CMSG_SPACE(fdsize)];
102 memset(&msgh, 0, sizeof(msgh));
104 iov.iov_len = buflen;
108 msgh.msg_control = control;
109 msgh.msg_controllen = sizeof(control);
111 ret = recvmsg(sockfd, &msgh, 0);
113 RTE_LOG(ERR, VHOST_CONFIG, "recvmsg failed\n");
117 if (msgh.msg_flags & (MSG_TRUNC | MSG_CTRUNC)) {
118 RTE_LOG(ERR, VHOST_CONFIG, "truncted msg\n");
122 for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
123 cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
124 if ((cmsg->cmsg_level == SOL_SOCKET) &&
125 (cmsg->cmsg_type == SCM_RIGHTS)) {
126 memcpy(fds, CMSG_DATA(cmsg), fdsize);
135 send_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
140 size_t fdsize = fd_num * sizeof(int);
141 char control[CMSG_SPACE(fdsize)];
142 struct cmsghdr *cmsg;
145 memset(&msgh, 0, sizeof(msgh));
147 iov.iov_len = buflen;
152 if (fds && fd_num > 0) {
153 msgh.msg_control = control;
154 msgh.msg_controllen = sizeof(control);
155 cmsg = CMSG_FIRSTHDR(&msgh);
156 cmsg->cmsg_len = CMSG_LEN(fdsize);
157 cmsg->cmsg_level = SOL_SOCKET;
158 cmsg->cmsg_type = SCM_RIGHTS;
159 memcpy(CMSG_DATA(cmsg), fds, fdsize);
161 msgh.msg_control = NULL;
162 msgh.msg_controllen = 0;
166 ret = sendmsg(sockfd, &msgh, 0);
167 } while (ret < 0 && errno == EINTR);
170 RTE_LOG(ERR, VHOST_CONFIG, "sendmsg error\n");
178 vhost_user_add_connection(int fd, struct vhost_user_socket *vsocket)
182 struct vhost_user_connection *conn;
185 conn = malloc(sizeof(*conn));
191 vid = vhost_new_device();
196 size = strnlen(vsocket->path, PATH_MAX);
197 vhost_set_ifname(vid, vsocket->path, size);
199 vhost_set_builtin_virtio_net(vid, vsocket->use_builtin_virtio_net);
201 if (vsocket->dequeue_zero_copy)
202 vhost_enable_dequeue_zero_copy(vid);
204 RTE_LOG(INFO, VHOST_CONFIG, "new device, handle is %d\n", vid);
206 if (vsocket->notify_ops->new_connection) {
207 ret = vsocket->notify_ops->new_connection(vid);
209 RTE_LOG(ERR, VHOST_CONFIG,
210 "failed to add vhost user connection with fd %d\n",
217 conn->vsocket = vsocket;
219 ret = fdset_add(&vhost_user.fdset, fd, vhost_user_read_cb,
222 RTE_LOG(ERR, VHOST_CONFIG,
223 "failed to add fd %d into vhost server fdset\n",
226 if (vsocket->notify_ops->destroy_connection)
227 vsocket->notify_ops->destroy_connection(conn->vid);
232 pthread_mutex_lock(&vsocket->conn_mutex);
233 TAILQ_INSERT_TAIL(&vsocket->conn_list, conn, next);
234 pthread_mutex_unlock(&vsocket->conn_mutex);
242 /* call back when there is new vhost-user connection from client */
244 vhost_user_server_new_connection(int fd, void *dat, int *remove __rte_unused)
246 struct vhost_user_socket *vsocket = dat;
248 fd = accept(fd, NULL, NULL);
252 RTE_LOG(INFO, VHOST_CONFIG, "new vhost user connection is %d\n", fd);
253 vhost_user_add_connection(fd, vsocket);
257 vhost_user_read_cb(int connfd, void *dat, int *remove)
259 struct vhost_user_connection *conn = dat;
260 struct vhost_user_socket *vsocket = conn->vsocket;
263 ret = vhost_user_msg_handler(conn->vid, connfd);
267 vhost_destroy_device(conn->vid);
269 if (vsocket->notify_ops->destroy_connection)
270 vsocket->notify_ops->destroy_connection(conn->vid);
272 pthread_mutex_lock(&vsocket->conn_mutex);
273 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
274 pthread_mutex_unlock(&vsocket->conn_mutex);
278 if (vsocket->reconnect) {
279 create_unix_socket(vsocket);
280 vhost_user_start_client(vsocket);
286 create_unix_socket(struct vhost_user_socket *vsocket)
289 struct sockaddr_un *un = &vsocket->un;
291 fd = socket(AF_UNIX, SOCK_STREAM, 0);
294 RTE_LOG(INFO, VHOST_CONFIG, "vhost-user %s: socket created, fd: %d\n",
295 vsocket->is_server ? "server" : "client", fd);
297 if (!vsocket->is_server && fcntl(fd, F_SETFL, O_NONBLOCK)) {
298 RTE_LOG(ERR, VHOST_CONFIG,
299 "vhost-user: can't set nonblocking mode for socket, fd: "
300 "%d (%s)\n", fd, strerror(errno));
305 memset(un, 0, sizeof(*un));
306 un->sun_family = AF_UNIX;
307 strncpy(un->sun_path, vsocket->path, sizeof(un->sun_path));
308 un->sun_path[sizeof(un->sun_path) - 1] = '\0';
310 vsocket->socket_fd = fd;
315 vhost_user_start_server(struct vhost_user_socket *vsocket)
318 int fd = vsocket->socket_fd;
319 const char *path = vsocket->path;
321 ret = bind(fd, (struct sockaddr *)&vsocket->un, sizeof(vsocket->un));
323 RTE_LOG(ERR, VHOST_CONFIG,
324 "failed to bind to %s: %s; remove it and try again\n",
325 path, strerror(errno));
328 RTE_LOG(INFO, VHOST_CONFIG, "bind to %s\n", path);
330 ret = listen(fd, MAX_VIRTIO_BACKLOG);
334 ret = fdset_add(&vhost_user.fdset, fd, vhost_user_server_new_connection,
337 RTE_LOG(ERR, VHOST_CONFIG,
338 "failed to add listen fd %d to vhost server fdset\n",
350 struct vhost_user_reconnect {
351 struct sockaddr_un un;
353 struct vhost_user_socket *vsocket;
355 TAILQ_ENTRY(vhost_user_reconnect) next;
358 TAILQ_HEAD(vhost_user_reconnect_tailq_list, vhost_user_reconnect);
359 struct vhost_user_reconnect_list {
360 struct vhost_user_reconnect_tailq_list head;
361 pthread_mutex_t mutex;
364 static struct vhost_user_reconnect_list reconn_list;
365 static pthread_t reconn_tid;
368 vhost_user_connect_nonblock(int fd, struct sockaddr *un, size_t sz)
372 ret = connect(fd, un, sz);
373 if (ret < 0 && errno != EISCONN)
376 flags = fcntl(fd, F_GETFL, 0);
378 RTE_LOG(ERR, VHOST_CONFIG,
379 "can't get flags for connfd %d\n", fd);
382 if ((flags & O_NONBLOCK) && fcntl(fd, F_SETFL, flags & ~O_NONBLOCK)) {
383 RTE_LOG(ERR, VHOST_CONFIG,
384 "can't disable nonblocking on fd %d\n", fd);
391 vhost_user_client_reconnect(void *arg __rte_unused)
394 struct vhost_user_reconnect *reconn, *next;
397 pthread_mutex_lock(&reconn_list.mutex);
400 * An equal implementation of TAILQ_FOREACH_SAFE,
401 * which does not exist on all platforms.
403 for (reconn = TAILQ_FIRST(&reconn_list.head);
404 reconn != NULL; reconn = next) {
405 next = TAILQ_NEXT(reconn, next);
407 ret = vhost_user_connect_nonblock(reconn->fd,
408 (struct sockaddr *)&reconn->un,
412 RTE_LOG(ERR, VHOST_CONFIG,
413 "reconnection for fd %d failed\n",
420 RTE_LOG(INFO, VHOST_CONFIG,
421 "%s: connected\n", reconn->vsocket->path);
422 vhost_user_add_connection(reconn->fd, reconn->vsocket);
424 TAILQ_REMOVE(&reconn_list.head, reconn, next);
428 pthread_mutex_unlock(&reconn_list.mutex);
436 vhost_user_reconnect_init(void)
439 char thread_name[RTE_MAX_THREAD_NAME_LEN];
441 ret = pthread_mutex_init(&reconn_list.mutex, NULL);
443 RTE_LOG(ERR, VHOST_CONFIG, "failed to initialize mutex");
446 TAILQ_INIT(&reconn_list.head);
448 ret = pthread_create(&reconn_tid, NULL,
449 vhost_user_client_reconnect, NULL);
451 RTE_LOG(ERR, VHOST_CONFIG, "failed to create reconnect thread");
452 if (pthread_mutex_destroy(&reconn_list.mutex)) {
453 RTE_LOG(ERR, VHOST_CONFIG,
454 "failed to destroy reconnect mutex");
457 snprintf(thread_name, RTE_MAX_THREAD_NAME_LEN,
460 if (rte_thread_setname(reconn_tid, thread_name)) {
461 RTE_LOG(DEBUG, VHOST_CONFIG,
462 "failed to set reconnect thread name");
470 vhost_user_start_client(struct vhost_user_socket *vsocket)
473 int fd = vsocket->socket_fd;
474 const char *path = vsocket->path;
475 struct vhost_user_reconnect *reconn;
477 ret = vhost_user_connect_nonblock(fd, (struct sockaddr *)&vsocket->un,
478 sizeof(vsocket->un));
480 vhost_user_add_connection(fd, vsocket);
484 RTE_LOG(WARNING, VHOST_CONFIG,
485 "failed to connect to %s: %s\n",
486 path, strerror(errno));
488 if (ret == -2 || !vsocket->reconnect) {
493 RTE_LOG(INFO, VHOST_CONFIG, "%s: reconnecting...\n", path);
494 reconn = malloc(sizeof(*reconn));
495 if (reconn == NULL) {
496 RTE_LOG(ERR, VHOST_CONFIG,
497 "failed to allocate memory for reconnect\n");
501 reconn->un = vsocket->un;
503 reconn->vsocket = vsocket;
504 pthread_mutex_lock(&reconn_list.mutex);
505 TAILQ_INSERT_TAIL(&reconn_list.head, reconn, next);
506 pthread_mutex_unlock(&reconn_list.mutex);
511 static struct vhost_user_socket *
512 find_vhost_user_socket(const char *path)
516 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
517 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
519 if (!strcmp(vsocket->path, path))
527 rte_vhost_driver_disable_features(const char *path, uint64_t features)
529 struct vhost_user_socket *vsocket;
531 pthread_mutex_lock(&vhost_user.mutex);
532 vsocket = find_vhost_user_socket(path);
534 /* Note that use_builtin_virtio_net is not affected by this function
535 * since callers may want to selectively disable features of the
536 * built-in vhost net device backend.
540 vsocket->features &= ~features;
541 pthread_mutex_unlock(&vhost_user.mutex);
543 return vsocket ? 0 : -1;
547 rte_vhost_driver_enable_features(const char *path, uint64_t features)
549 struct vhost_user_socket *vsocket;
551 pthread_mutex_lock(&vhost_user.mutex);
552 vsocket = find_vhost_user_socket(path);
554 if ((vsocket->supported_features & features) != features) {
556 * trying to enable features the driver doesn't
559 pthread_mutex_unlock(&vhost_user.mutex);
562 vsocket->features |= features;
564 pthread_mutex_unlock(&vhost_user.mutex);
566 return vsocket ? 0 : -1;
570 rte_vhost_driver_set_features(const char *path, uint64_t features)
572 struct vhost_user_socket *vsocket;
574 pthread_mutex_lock(&vhost_user.mutex);
575 vsocket = find_vhost_user_socket(path);
577 vsocket->supported_features = features;
578 vsocket->features = features;
580 /* Anyone setting feature bits is implementing their own vhost
583 vsocket->use_builtin_virtio_net = false;
585 pthread_mutex_unlock(&vhost_user.mutex);
587 return vsocket ? 0 : -1;
591 rte_vhost_driver_get_features(const char *path, uint64_t *features)
593 struct vhost_user_socket *vsocket;
595 pthread_mutex_lock(&vhost_user.mutex);
596 vsocket = find_vhost_user_socket(path);
598 *features = vsocket->features;
599 pthread_mutex_unlock(&vhost_user.mutex);
602 RTE_LOG(ERR, VHOST_CONFIG,
603 "socket file %s is not registered yet.\n", path);
611 * Register a new vhost-user socket; here we could act as server
612 * (the default case), or client (when RTE_VHOST_USER_CLIENT) flag
616 rte_vhost_driver_register(const char *path, uint64_t flags)
619 struct vhost_user_socket *vsocket;
624 pthread_mutex_lock(&vhost_user.mutex);
626 if (vhost_user.vsocket_cnt == MAX_VHOST_SOCKET) {
627 RTE_LOG(ERR, VHOST_CONFIG,
628 "error: the number of vhost sockets reaches maximum\n");
632 vsocket = malloc(sizeof(struct vhost_user_socket));
635 memset(vsocket, 0, sizeof(struct vhost_user_socket));
636 vsocket->path = strdup(path);
637 if (vsocket->path == NULL) {
638 RTE_LOG(ERR, VHOST_CONFIG,
639 "error: failed to copy socket path string\n");
643 TAILQ_INIT(&vsocket->conn_list);
644 ret = pthread_mutex_init(&vsocket->conn_mutex, NULL);
646 RTE_LOG(ERR, VHOST_CONFIG,
647 "error: failed to init connection mutex\n");
650 vsocket->dequeue_zero_copy = flags & RTE_VHOST_USER_DEQUEUE_ZERO_COPY;
653 * Set the supported features correctly for the builtin vhost-user
656 * Applications know nothing about features the builtin virtio net
657 * driver (virtio_net.c) supports, thus it's not possible for them
658 * to invoke rte_vhost_driver_set_features(). To workaround it, here
659 * we set it unconditionally. If the application want to implement
660 * another vhost-user driver (say SCSI), it should call the
661 * rte_vhost_driver_set_features(), which will overwrite following
664 vsocket->use_builtin_virtio_net = true;
665 vsocket->supported_features = VIRTIO_NET_SUPPORTED_FEATURES;
666 vsocket->features = VIRTIO_NET_SUPPORTED_FEATURES;
668 if (!(flags & RTE_VHOST_USER_IOMMU_SUPPORT)) {
669 vsocket->supported_features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
670 vsocket->features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
673 if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
674 vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
675 if (vsocket->reconnect && reconn_tid == 0) {
676 if (vhost_user_reconnect_init() != 0)
680 vsocket->is_server = true;
682 ret = create_unix_socket(vsocket);
687 vhost_user.vsockets[vhost_user.vsocket_cnt++] = vsocket;
689 pthread_mutex_unlock(&vhost_user.mutex);
693 if (pthread_mutex_destroy(&vsocket->conn_mutex)) {
694 RTE_LOG(ERR, VHOST_CONFIG,
695 "error: failed to destroy connection mutex\n");
701 pthread_mutex_unlock(&vhost_user.mutex);
707 vhost_user_remove_reconnect(struct vhost_user_socket *vsocket)
710 struct vhost_user_reconnect *reconn, *next;
712 pthread_mutex_lock(&reconn_list.mutex);
714 for (reconn = TAILQ_FIRST(&reconn_list.head);
715 reconn != NULL; reconn = next) {
716 next = TAILQ_NEXT(reconn, next);
718 if (reconn->vsocket == vsocket) {
719 TAILQ_REMOVE(&reconn_list.head, reconn, next);
726 pthread_mutex_unlock(&reconn_list.mutex);
731 * Unregister the specified vhost socket
734 rte_vhost_driver_unregister(const char *path)
738 struct vhost_user_connection *conn, *next;
740 pthread_mutex_lock(&vhost_user.mutex);
742 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
743 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
745 if (!strcmp(vsocket->path, path)) {
746 if (vsocket->is_server) {
747 fdset_del(&vhost_user.fdset, vsocket->socket_fd);
748 close(vsocket->socket_fd);
750 } else if (vsocket->reconnect) {
751 vhost_user_remove_reconnect(vsocket);
754 pthread_mutex_lock(&vsocket->conn_mutex);
755 for (conn = TAILQ_FIRST(&vsocket->conn_list);
758 next = TAILQ_NEXT(conn, next);
760 fdset_del(&vhost_user.fdset, conn->connfd);
761 RTE_LOG(INFO, VHOST_CONFIG,
762 "free connfd = %d for device '%s'\n",
765 vhost_destroy_device(conn->vid);
766 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
769 pthread_mutex_unlock(&vsocket->conn_mutex);
771 pthread_mutex_destroy(&vsocket->conn_mutex);
775 count = --vhost_user.vsocket_cnt;
776 vhost_user.vsockets[i] = vhost_user.vsockets[count];
777 vhost_user.vsockets[count] = NULL;
778 pthread_mutex_unlock(&vhost_user.mutex);
783 pthread_mutex_unlock(&vhost_user.mutex);
789 * Register ops so that we can add/remove device to data core.
792 rte_vhost_driver_callback_register(const char *path,
793 struct vhost_device_ops const * const ops)
795 struct vhost_user_socket *vsocket;
797 pthread_mutex_lock(&vhost_user.mutex);
798 vsocket = find_vhost_user_socket(path);
800 vsocket->notify_ops = ops;
801 pthread_mutex_unlock(&vhost_user.mutex);
803 return vsocket ? 0 : -1;
806 struct vhost_device_ops const *
807 vhost_driver_callback_get(const char *path)
809 struct vhost_user_socket *vsocket;
811 pthread_mutex_lock(&vhost_user.mutex);
812 vsocket = find_vhost_user_socket(path);
813 pthread_mutex_unlock(&vhost_user.mutex);
815 return vsocket ? vsocket->notify_ops : NULL;
819 rte_vhost_driver_start(const char *path)
821 struct vhost_user_socket *vsocket;
822 static pthread_t fdset_tid;
824 pthread_mutex_lock(&vhost_user.mutex);
825 vsocket = find_vhost_user_socket(path);
826 pthread_mutex_unlock(&vhost_user.mutex);
831 if (fdset_tid == 0) {
832 int ret = pthread_create(&fdset_tid, NULL, fdset_event_dispatch,
835 RTE_LOG(ERR, VHOST_CONFIG,
836 "failed to create fdset handling thread");
839 if (vsocket->is_server)
840 return vhost_user_start_server(vsocket);
842 return vhost_user_start_client(vsocket);