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)];
103 memset(&msgh, 0, sizeof(msgh));
105 iov.iov_len = buflen;
109 msgh.msg_control = control;
110 msgh.msg_controllen = sizeof(control);
112 ret = recvmsg(sockfd, &msgh, 0);
114 RTE_LOG(ERR, VHOST_CONFIG, "recvmsg failed\n");
118 if (msgh.msg_flags & (MSG_TRUNC | MSG_CTRUNC)) {
119 RTE_LOG(ERR, VHOST_CONFIG, "truncted msg\n");
123 for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
124 cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
125 if ((cmsg->cmsg_level == SOL_SOCKET) &&
126 (cmsg->cmsg_type == SCM_RIGHTS)) {
127 got_fds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
128 memcpy(fds, CMSG_DATA(cmsg), got_fds * sizeof(int));
133 /* Clear out unused file descriptors */
134 while (got_fds < fd_num)
141 send_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
146 size_t fdsize = fd_num * sizeof(int);
147 char control[CMSG_SPACE(fdsize)];
148 struct cmsghdr *cmsg;
151 memset(&msgh, 0, sizeof(msgh));
153 iov.iov_len = buflen;
158 if (fds && fd_num > 0) {
159 msgh.msg_control = control;
160 msgh.msg_controllen = sizeof(control);
161 cmsg = CMSG_FIRSTHDR(&msgh);
163 RTE_LOG(ERR, VHOST_CONFIG, "cmsg == NULL\n");
167 cmsg->cmsg_len = CMSG_LEN(fdsize);
168 cmsg->cmsg_level = SOL_SOCKET;
169 cmsg->cmsg_type = SCM_RIGHTS;
170 memcpy(CMSG_DATA(cmsg), fds, fdsize);
172 msgh.msg_control = NULL;
173 msgh.msg_controllen = 0;
177 ret = sendmsg(sockfd, &msgh, 0);
178 } while (ret < 0 && errno == EINTR);
181 RTE_LOG(ERR, VHOST_CONFIG, "sendmsg error\n");
189 vhost_user_add_connection(int fd, struct vhost_user_socket *vsocket)
193 struct vhost_user_connection *conn;
196 conn = malloc(sizeof(*conn));
202 vid = vhost_new_device();
207 size = strnlen(vsocket->path, PATH_MAX);
208 vhost_set_ifname(vid, vsocket->path, size);
210 vhost_set_builtin_virtio_net(vid, vsocket->use_builtin_virtio_net);
212 if (vsocket->dequeue_zero_copy)
213 vhost_enable_dequeue_zero_copy(vid);
215 RTE_LOG(INFO, VHOST_CONFIG, "new device, handle is %d\n", vid);
217 if (vsocket->notify_ops->new_connection) {
218 ret = vsocket->notify_ops->new_connection(vid);
220 RTE_LOG(ERR, VHOST_CONFIG,
221 "failed to add vhost user connection with fd %d\n",
228 conn->vsocket = vsocket;
230 ret = fdset_add(&vhost_user.fdset, fd, vhost_user_read_cb,
233 RTE_LOG(ERR, VHOST_CONFIG,
234 "failed to add fd %d into vhost server fdset\n",
237 if (vsocket->notify_ops->destroy_connection)
238 vsocket->notify_ops->destroy_connection(conn->vid);
243 pthread_mutex_lock(&vsocket->conn_mutex);
244 TAILQ_INSERT_TAIL(&vsocket->conn_list, conn, next);
245 pthread_mutex_unlock(&vsocket->conn_mutex);
253 /* call back when there is new vhost-user connection from client */
255 vhost_user_server_new_connection(int fd, void *dat, int *remove __rte_unused)
257 struct vhost_user_socket *vsocket = dat;
259 fd = accept(fd, NULL, NULL);
263 RTE_LOG(INFO, VHOST_CONFIG, "new vhost user connection is %d\n", fd);
264 vhost_user_add_connection(fd, vsocket);
268 vhost_user_read_cb(int connfd, void *dat, int *remove)
270 struct vhost_user_connection *conn = dat;
271 struct vhost_user_socket *vsocket = conn->vsocket;
274 ret = vhost_user_msg_handler(conn->vid, connfd);
278 vhost_destroy_device(conn->vid);
280 if (vsocket->notify_ops->destroy_connection)
281 vsocket->notify_ops->destroy_connection(conn->vid);
283 pthread_mutex_lock(&vsocket->conn_mutex);
284 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
285 pthread_mutex_unlock(&vsocket->conn_mutex);
289 if (vsocket->reconnect) {
290 create_unix_socket(vsocket);
291 vhost_user_start_client(vsocket);
297 create_unix_socket(struct vhost_user_socket *vsocket)
300 struct sockaddr_un *un = &vsocket->un;
302 fd = socket(AF_UNIX, SOCK_STREAM, 0);
305 RTE_LOG(INFO, VHOST_CONFIG, "vhost-user %s: socket created, fd: %d\n",
306 vsocket->is_server ? "server" : "client", fd);
308 if (!vsocket->is_server && fcntl(fd, F_SETFL, O_NONBLOCK)) {
309 RTE_LOG(ERR, VHOST_CONFIG,
310 "vhost-user: can't set nonblocking mode for socket, fd: "
311 "%d (%s)\n", fd, strerror(errno));
316 memset(un, 0, sizeof(*un));
317 un->sun_family = AF_UNIX;
318 strncpy(un->sun_path, vsocket->path, sizeof(un->sun_path));
319 un->sun_path[sizeof(un->sun_path) - 1] = '\0';
321 vsocket->socket_fd = fd;
326 vhost_user_start_server(struct vhost_user_socket *vsocket)
329 int fd = vsocket->socket_fd;
330 const char *path = vsocket->path;
333 * bind () may fail if the socket file with the same name already
334 * exists. But the library obviously should not delete the file
335 * provided by the user, since we can not be sure that it is not
336 * being used by other applications. Moreover, many applications form
337 * socket names based on user input, which is prone to errors.
339 * The user must ensure that the socket does not exist before
340 * registering the vhost driver in server mode.
342 ret = bind(fd, (struct sockaddr *)&vsocket->un, sizeof(vsocket->un));
344 RTE_LOG(ERR, VHOST_CONFIG,
345 "failed to bind to %s: %s; remove it and try again\n",
346 path, strerror(errno));
349 RTE_LOG(INFO, VHOST_CONFIG, "bind to %s\n", path);
351 ret = listen(fd, MAX_VIRTIO_BACKLOG);
355 ret = fdset_add(&vhost_user.fdset, fd, vhost_user_server_new_connection,
358 RTE_LOG(ERR, VHOST_CONFIG,
359 "failed to add listen fd %d to vhost server fdset\n",
371 struct vhost_user_reconnect {
372 struct sockaddr_un un;
374 struct vhost_user_socket *vsocket;
376 TAILQ_ENTRY(vhost_user_reconnect) next;
379 TAILQ_HEAD(vhost_user_reconnect_tailq_list, vhost_user_reconnect);
380 struct vhost_user_reconnect_list {
381 struct vhost_user_reconnect_tailq_list head;
382 pthread_mutex_t mutex;
385 static struct vhost_user_reconnect_list reconn_list;
386 static pthread_t reconn_tid;
389 vhost_user_connect_nonblock(int fd, struct sockaddr *un, size_t sz)
393 ret = connect(fd, un, sz);
394 if (ret < 0 && errno != EISCONN)
397 flags = fcntl(fd, F_GETFL, 0);
399 RTE_LOG(ERR, VHOST_CONFIG,
400 "can't get flags for connfd %d\n", fd);
403 if ((flags & O_NONBLOCK) && fcntl(fd, F_SETFL, flags & ~O_NONBLOCK)) {
404 RTE_LOG(ERR, VHOST_CONFIG,
405 "can't disable nonblocking on fd %d\n", fd);
412 vhost_user_client_reconnect(void *arg __rte_unused)
415 struct vhost_user_reconnect *reconn, *next;
418 pthread_mutex_lock(&reconn_list.mutex);
421 * An equal implementation of TAILQ_FOREACH_SAFE,
422 * which does not exist on all platforms.
424 for (reconn = TAILQ_FIRST(&reconn_list.head);
425 reconn != NULL; reconn = next) {
426 next = TAILQ_NEXT(reconn, next);
428 ret = vhost_user_connect_nonblock(reconn->fd,
429 (struct sockaddr *)&reconn->un,
433 RTE_LOG(ERR, VHOST_CONFIG,
434 "reconnection for fd %d failed\n",
441 RTE_LOG(INFO, VHOST_CONFIG,
442 "%s: connected\n", reconn->vsocket->path);
443 vhost_user_add_connection(reconn->fd, reconn->vsocket);
445 TAILQ_REMOVE(&reconn_list.head, reconn, next);
449 pthread_mutex_unlock(&reconn_list.mutex);
457 vhost_user_reconnect_init(void)
460 char thread_name[RTE_MAX_THREAD_NAME_LEN];
462 ret = pthread_mutex_init(&reconn_list.mutex, NULL);
464 RTE_LOG(ERR, VHOST_CONFIG, "failed to initialize mutex");
467 TAILQ_INIT(&reconn_list.head);
469 ret = pthread_create(&reconn_tid, NULL,
470 vhost_user_client_reconnect, NULL);
472 RTE_LOG(ERR, VHOST_CONFIG, "failed to create reconnect thread");
473 if (pthread_mutex_destroy(&reconn_list.mutex)) {
474 RTE_LOG(ERR, VHOST_CONFIG,
475 "failed to destroy reconnect mutex");
478 snprintf(thread_name, RTE_MAX_THREAD_NAME_LEN,
481 if (rte_thread_setname(reconn_tid, thread_name)) {
482 RTE_LOG(DEBUG, VHOST_CONFIG,
483 "failed to set reconnect thread name");
491 vhost_user_start_client(struct vhost_user_socket *vsocket)
494 int fd = vsocket->socket_fd;
495 const char *path = vsocket->path;
496 struct vhost_user_reconnect *reconn;
498 ret = vhost_user_connect_nonblock(fd, (struct sockaddr *)&vsocket->un,
499 sizeof(vsocket->un));
501 vhost_user_add_connection(fd, vsocket);
505 RTE_LOG(WARNING, VHOST_CONFIG,
506 "failed to connect to %s: %s\n",
507 path, strerror(errno));
509 if (ret == -2 || !vsocket->reconnect) {
514 RTE_LOG(INFO, VHOST_CONFIG, "%s: reconnecting...\n", path);
515 reconn = malloc(sizeof(*reconn));
516 if (reconn == NULL) {
517 RTE_LOG(ERR, VHOST_CONFIG,
518 "failed to allocate memory for reconnect\n");
522 reconn->un = vsocket->un;
524 reconn->vsocket = vsocket;
525 pthread_mutex_lock(&reconn_list.mutex);
526 TAILQ_INSERT_TAIL(&reconn_list.head, reconn, next);
527 pthread_mutex_unlock(&reconn_list.mutex);
532 static struct vhost_user_socket *
533 find_vhost_user_socket(const char *path)
537 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
538 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
540 if (!strcmp(vsocket->path, path))
548 rte_vhost_driver_disable_features(const char *path, uint64_t features)
550 struct vhost_user_socket *vsocket;
552 pthread_mutex_lock(&vhost_user.mutex);
553 vsocket = find_vhost_user_socket(path);
555 /* Note that use_builtin_virtio_net is not affected by this function
556 * since callers may want to selectively disable features of the
557 * built-in vhost net device backend.
561 vsocket->features &= ~features;
562 pthread_mutex_unlock(&vhost_user.mutex);
564 return vsocket ? 0 : -1;
568 rte_vhost_driver_enable_features(const char *path, uint64_t features)
570 struct vhost_user_socket *vsocket;
572 pthread_mutex_lock(&vhost_user.mutex);
573 vsocket = find_vhost_user_socket(path);
575 if ((vsocket->supported_features & features) != features) {
577 * trying to enable features the driver doesn't
580 pthread_mutex_unlock(&vhost_user.mutex);
583 vsocket->features |= features;
585 pthread_mutex_unlock(&vhost_user.mutex);
587 return vsocket ? 0 : -1;
591 rte_vhost_driver_set_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 vsocket->supported_features = features;
599 vsocket->features = features;
601 /* Anyone setting feature bits is implementing their own vhost
604 vsocket->use_builtin_virtio_net = false;
606 pthread_mutex_unlock(&vhost_user.mutex);
608 return vsocket ? 0 : -1;
612 rte_vhost_driver_get_features(const char *path, uint64_t *features)
614 struct vhost_user_socket *vsocket;
616 pthread_mutex_lock(&vhost_user.mutex);
617 vsocket = find_vhost_user_socket(path);
619 *features = vsocket->features;
620 pthread_mutex_unlock(&vhost_user.mutex);
623 RTE_LOG(ERR, VHOST_CONFIG,
624 "socket file %s is not registered yet.\n", path);
632 * Register a new vhost-user socket; here we could act as server
633 * (the default case), or client (when RTE_VHOST_USER_CLIENT) flag
637 rte_vhost_driver_register(const char *path, uint64_t flags)
640 struct vhost_user_socket *vsocket;
645 pthread_mutex_lock(&vhost_user.mutex);
647 if (vhost_user.vsocket_cnt == MAX_VHOST_SOCKET) {
648 RTE_LOG(ERR, VHOST_CONFIG,
649 "error: the number of vhost sockets reaches maximum\n");
653 vsocket = malloc(sizeof(struct vhost_user_socket));
656 memset(vsocket, 0, sizeof(struct vhost_user_socket));
657 vsocket->path = strdup(path);
658 if (vsocket->path == NULL) {
659 RTE_LOG(ERR, VHOST_CONFIG,
660 "error: failed to copy socket path string\n");
664 TAILQ_INIT(&vsocket->conn_list);
665 ret = pthread_mutex_init(&vsocket->conn_mutex, NULL);
667 RTE_LOG(ERR, VHOST_CONFIG,
668 "error: failed to init connection mutex\n");
671 vsocket->dequeue_zero_copy = flags & RTE_VHOST_USER_DEQUEUE_ZERO_COPY;
674 * Set the supported features correctly for the builtin vhost-user
677 * Applications know nothing about features the builtin virtio net
678 * driver (virtio_net.c) supports, thus it's not possible for them
679 * to invoke rte_vhost_driver_set_features(). To workaround it, here
680 * we set it unconditionally. If the application want to implement
681 * another vhost-user driver (say SCSI), it should call the
682 * rte_vhost_driver_set_features(), which will overwrite following
685 vsocket->use_builtin_virtio_net = true;
686 vsocket->supported_features = VIRTIO_NET_SUPPORTED_FEATURES;
687 vsocket->features = VIRTIO_NET_SUPPORTED_FEATURES;
689 if (!(flags & RTE_VHOST_USER_IOMMU_SUPPORT)) {
690 vsocket->supported_features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
691 vsocket->features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
694 if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
695 vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
696 if (vsocket->reconnect && reconn_tid == 0) {
697 if (vhost_user_reconnect_init() != 0)
701 vsocket->is_server = true;
703 ret = create_unix_socket(vsocket);
708 vhost_user.vsockets[vhost_user.vsocket_cnt++] = vsocket;
710 pthread_mutex_unlock(&vhost_user.mutex);
714 if (pthread_mutex_destroy(&vsocket->conn_mutex)) {
715 RTE_LOG(ERR, VHOST_CONFIG,
716 "error: failed to destroy connection mutex\n");
722 pthread_mutex_unlock(&vhost_user.mutex);
728 vhost_user_remove_reconnect(struct vhost_user_socket *vsocket)
731 struct vhost_user_reconnect *reconn, *next;
733 pthread_mutex_lock(&reconn_list.mutex);
735 for (reconn = TAILQ_FIRST(&reconn_list.head);
736 reconn != NULL; reconn = next) {
737 next = TAILQ_NEXT(reconn, next);
739 if (reconn->vsocket == vsocket) {
740 TAILQ_REMOVE(&reconn_list.head, reconn, next);
747 pthread_mutex_unlock(&reconn_list.mutex);
752 * Unregister the specified vhost socket
755 rte_vhost_driver_unregister(const char *path)
759 struct vhost_user_connection *conn, *next;
761 pthread_mutex_lock(&vhost_user.mutex);
763 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
764 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
766 if (!strcmp(vsocket->path, path)) {
767 if (vsocket->is_server) {
768 fdset_del(&vhost_user.fdset, vsocket->socket_fd);
769 close(vsocket->socket_fd);
771 } else if (vsocket->reconnect) {
772 vhost_user_remove_reconnect(vsocket);
775 pthread_mutex_lock(&vsocket->conn_mutex);
776 for (conn = TAILQ_FIRST(&vsocket->conn_list);
779 next = TAILQ_NEXT(conn, next);
781 fdset_del(&vhost_user.fdset, conn->connfd);
782 RTE_LOG(INFO, VHOST_CONFIG,
783 "free connfd = %d for device '%s'\n",
786 vhost_destroy_device(conn->vid);
787 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
790 pthread_mutex_unlock(&vsocket->conn_mutex);
792 pthread_mutex_destroy(&vsocket->conn_mutex);
796 count = --vhost_user.vsocket_cnt;
797 vhost_user.vsockets[i] = vhost_user.vsockets[count];
798 vhost_user.vsockets[count] = NULL;
799 pthread_mutex_unlock(&vhost_user.mutex);
804 pthread_mutex_unlock(&vhost_user.mutex);
810 * Register ops so that we can add/remove device to data core.
813 rte_vhost_driver_callback_register(const char *path,
814 struct vhost_device_ops const * const ops)
816 struct vhost_user_socket *vsocket;
818 pthread_mutex_lock(&vhost_user.mutex);
819 vsocket = find_vhost_user_socket(path);
821 vsocket->notify_ops = ops;
822 pthread_mutex_unlock(&vhost_user.mutex);
824 return vsocket ? 0 : -1;
827 struct vhost_device_ops const *
828 vhost_driver_callback_get(const char *path)
830 struct vhost_user_socket *vsocket;
832 pthread_mutex_lock(&vhost_user.mutex);
833 vsocket = find_vhost_user_socket(path);
834 pthread_mutex_unlock(&vhost_user.mutex);
836 return vsocket ? vsocket->notify_ops : NULL;
840 rte_vhost_driver_start(const char *path)
842 struct vhost_user_socket *vsocket;
843 static pthread_t fdset_tid;
845 pthread_mutex_lock(&vhost_user.mutex);
846 vsocket = find_vhost_user_socket(path);
847 pthread_mutex_unlock(&vhost_user.mutex);
852 if (fdset_tid == 0) {
853 int ret = pthread_create(&fdset_tid, NULL, fdset_event_dispatch,
856 RTE_LOG(ERR, VHOST_CONFIG,
857 "failed to create fdset handling thread");
860 if (vsocket->is_server)
861 return vhost_user_start_server(vsocket);
863 return vhost_user_start_client(vsocket);