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;
45 * The "supported_features" indicates the feature bits the
46 * vhost driver supports. The "features" indicates the feature
47 * bits after the rte_vhost_driver_features_disable/enable().
48 * It is also the final feature bits used for vhost-user
49 * features negotiation.
51 uint64_t supported_features;
54 struct vhost_device_ops const *notify_ops;
57 struct vhost_user_connection {
58 struct vhost_user_socket *vsocket;
62 TAILQ_ENTRY(vhost_user_connection) next;
65 #define MAX_VHOST_SOCKET 1024
67 struct vhost_user_socket *vsockets[MAX_VHOST_SOCKET];
70 pthread_mutex_t mutex;
73 #define MAX_VIRTIO_BACKLOG 128
75 static void vhost_user_server_new_connection(int fd, void *data, int *remove);
76 static void vhost_user_read_cb(int fd, void *dat, int *remove);
77 static int create_unix_socket(struct vhost_user_socket *vsocket);
78 static int vhost_user_start_client(struct vhost_user_socket *vsocket);
80 static struct vhost_user vhost_user = {
82 .fd = { [0 ... MAX_FDS - 1] = {-1, NULL, NULL, NULL, 0} },
83 .fd_mutex = PTHREAD_MUTEX_INITIALIZER,
87 .mutex = PTHREAD_MUTEX_INITIALIZER,
90 /* return bytes# of read on success or negative val on failure. */
92 read_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
96 size_t fdsize = fd_num * sizeof(int);
97 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 got_fds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
127 memcpy(fds, CMSG_DATA(cmsg), got_fds * sizeof(int));
132 /* Clear out unused file descriptors */
133 while (got_fds < fd_num)
140 send_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
145 size_t fdsize = fd_num * sizeof(int);
146 char control[CMSG_SPACE(fdsize)];
147 struct cmsghdr *cmsg;
150 memset(&msgh, 0, sizeof(msgh));
152 iov.iov_len = buflen;
157 if (fds && fd_num > 0) {
158 msgh.msg_control = control;
159 msgh.msg_controllen = sizeof(control);
160 cmsg = CMSG_FIRSTHDR(&msgh);
162 RTE_LOG(ERR, VHOST_CONFIG, "cmsg == NULL\n");
166 cmsg->cmsg_len = CMSG_LEN(fdsize);
167 cmsg->cmsg_level = SOL_SOCKET;
168 cmsg->cmsg_type = SCM_RIGHTS;
169 memcpy(CMSG_DATA(cmsg), fds, fdsize);
171 msgh.msg_control = NULL;
172 msgh.msg_controllen = 0;
176 ret = sendmsg(sockfd, &msgh, MSG_NOSIGNAL);
177 } while (ret < 0 && errno == EINTR);
180 RTE_LOG(ERR, VHOST_CONFIG, "sendmsg error\n");
188 vhost_user_add_connection(int fd, struct vhost_user_socket *vsocket)
192 struct vhost_user_connection *conn;
195 conn = malloc(sizeof(*conn));
201 vid = vhost_new_device();
206 size = strnlen(vsocket->path, PATH_MAX);
207 vhost_set_ifname(vid, vsocket->path, size);
209 vhost_set_builtin_virtio_net(vid, vsocket->use_builtin_virtio_net);
211 if (vsocket->dequeue_zero_copy)
212 vhost_enable_dequeue_zero_copy(vid);
214 RTE_LOG(INFO, VHOST_CONFIG, "new device, handle is %d\n", vid);
216 if (vsocket->notify_ops->new_connection) {
217 ret = vsocket->notify_ops->new_connection(vid);
219 RTE_LOG(ERR, VHOST_CONFIG,
220 "failed to add vhost user connection with fd %d\n",
227 conn->vsocket = vsocket;
229 ret = fdset_add(&vhost_user.fdset, fd, vhost_user_read_cb,
232 RTE_LOG(ERR, VHOST_CONFIG,
233 "failed to add fd %d into vhost server fdset\n",
236 if (vsocket->notify_ops->destroy_connection)
237 vsocket->notify_ops->destroy_connection(conn->vid);
242 pthread_mutex_lock(&vsocket->conn_mutex);
243 TAILQ_INSERT_TAIL(&vsocket->conn_list, conn, next);
244 pthread_mutex_unlock(&vsocket->conn_mutex);
252 /* call back when there is new vhost-user connection from client */
254 vhost_user_server_new_connection(int fd, void *dat, int *remove __rte_unused)
256 struct vhost_user_socket *vsocket = dat;
258 fd = accept(fd, NULL, NULL);
262 RTE_LOG(INFO, VHOST_CONFIG, "new vhost user connection is %d\n", fd);
263 vhost_user_add_connection(fd, vsocket);
267 vhost_user_read_cb(int connfd, void *dat, int *remove)
269 struct vhost_user_connection *conn = dat;
270 struct vhost_user_socket *vsocket = conn->vsocket;
273 ret = vhost_user_msg_handler(conn->vid, connfd);
277 vhost_destroy_device(conn->vid);
279 if (vsocket->notify_ops->destroy_connection)
280 vsocket->notify_ops->destroy_connection(conn->vid);
282 pthread_mutex_lock(&vsocket->conn_mutex);
283 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
284 pthread_mutex_unlock(&vsocket->conn_mutex);
288 if (vsocket->reconnect) {
289 create_unix_socket(vsocket);
290 vhost_user_start_client(vsocket);
296 create_unix_socket(struct vhost_user_socket *vsocket)
299 struct sockaddr_un *un = &vsocket->un;
301 fd = socket(AF_UNIX, SOCK_STREAM, 0);
304 RTE_LOG(INFO, VHOST_CONFIG, "vhost-user %s: socket created, fd: %d\n",
305 vsocket->is_server ? "server" : "client", fd);
307 if (!vsocket->is_server && fcntl(fd, F_SETFL, O_NONBLOCK)) {
308 RTE_LOG(ERR, VHOST_CONFIG,
309 "vhost-user: can't set nonblocking mode for socket, fd: "
310 "%d (%s)\n", fd, strerror(errno));
315 memset(un, 0, sizeof(*un));
316 un->sun_family = AF_UNIX;
317 strncpy(un->sun_path, vsocket->path, sizeof(un->sun_path));
318 un->sun_path[sizeof(un->sun_path) - 1] = '\0';
320 vsocket->socket_fd = fd;
325 vhost_user_start_server(struct vhost_user_socket *vsocket)
328 int fd = vsocket->socket_fd;
329 const char *path = vsocket->path;
332 * bind () may fail if the socket file with the same name already
333 * exists. But the library obviously should not delete the file
334 * provided by the user, since we can not be sure that it is not
335 * being used by other applications. Moreover, many applications form
336 * socket names based on user input, which is prone to errors.
338 * The user must ensure that the socket does not exist before
339 * registering the vhost driver in server mode.
341 ret = bind(fd, (struct sockaddr *)&vsocket->un, sizeof(vsocket->un));
343 RTE_LOG(ERR, VHOST_CONFIG,
344 "failed to bind to %s: %s; remove it and try again\n",
345 path, strerror(errno));
348 RTE_LOG(INFO, VHOST_CONFIG, "bind to %s\n", path);
350 ret = listen(fd, MAX_VIRTIO_BACKLOG);
354 ret = fdset_add(&vhost_user.fdset, fd, vhost_user_server_new_connection,
357 RTE_LOG(ERR, VHOST_CONFIG,
358 "failed to add listen fd %d to vhost server fdset\n",
370 struct vhost_user_reconnect {
371 struct sockaddr_un un;
373 struct vhost_user_socket *vsocket;
375 TAILQ_ENTRY(vhost_user_reconnect) next;
378 TAILQ_HEAD(vhost_user_reconnect_tailq_list, vhost_user_reconnect);
379 struct vhost_user_reconnect_list {
380 struct vhost_user_reconnect_tailq_list head;
381 pthread_mutex_t mutex;
384 static struct vhost_user_reconnect_list reconn_list;
385 static pthread_t reconn_tid;
388 vhost_user_connect_nonblock(int fd, struct sockaddr *un, size_t sz)
392 ret = connect(fd, un, sz);
393 if (ret < 0 && errno != EISCONN)
396 flags = fcntl(fd, F_GETFL, 0);
398 RTE_LOG(ERR, VHOST_CONFIG,
399 "can't get flags for connfd %d\n", fd);
402 if ((flags & O_NONBLOCK) && fcntl(fd, F_SETFL, flags & ~O_NONBLOCK)) {
403 RTE_LOG(ERR, VHOST_CONFIG,
404 "can't disable nonblocking on fd %d\n", fd);
411 vhost_user_client_reconnect(void *arg __rte_unused)
414 struct vhost_user_reconnect *reconn, *next;
417 pthread_mutex_lock(&reconn_list.mutex);
420 * An equal implementation of TAILQ_FOREACH_SAFE,
421 * which does not exist on all platforms.
423 for (reconn = TAILQ_FIRST(&reconn_list.head);
424 reconn != NULL; reconn = next) {
425 next = TAILQ_NEXT(reconn, next);
427 ret = vhost_user_connect_nonblock(reconn->fd,
428 (struct sockaddr *)&reconn->un,
432 RTE_LOG(ERR, VHOST_CONFIG,
433 "reconnection for fd %d failed\n",
440 RTE_LOG(INFO, VHOST_CONFIG,
441 "%s: connected\n", reconn->vsocket->path);
442 vhost_user_add_connection(reconn->fd, reconn->vsocket);
444 TAILQ_REMOVE(&reconn_list.head, reconn, next);
448 pthread_mutex_unlock(&reconn_list.mutex);
456 vhost_user_reconnect_init(void)
459 char thread_name[RTE_MAX_THREAD_NAME_LEN];
461 ret = pthread_mutex_init(&reconn_list.mutex, NULL);
463 RTE_LOG(ERR, VHOST_CONFIG, "failed to initialize mutex");
466 TAILQ_INIT(&reconn_list.head);
468 ret = pthread_create(&reconn_tid, NULL,
469 vhost_user_client_reconnect, NULL);
471 RTE_LOG(ERR, VHOST_CONFIG, "failed to create reconnect thread");
472 if (pthread_mutex_destroy(&reconn_list.mutex)) {
473 RTE_LOG(ERR, VHOST_CONFIG,
474 "failed to destroy reconnect mutex");
477 snprintf(thread_name, RTE_MAX_THREAD_NAME_LEN,
480 if (rte_thread_setname(reconn_tid, thread_name)) {
481 RTE_LOG(DEBUG, VHOST_CONFIG,
482 "failed to set reconnect thread name");
490 vhost_user_start_client(struct vhost_user_socket *vsocket)
493 int fd = vsocket->socket_fd;
494 const char *path = vsocket->path;
495 struct vhost_user_reconnect *reconn;
497 ret = vhost_user_connect_nonblock(fd, (struct sockaddr *)&vsocket->un,
498 sizeof(vsocket->un));
500 vhost_user_add_connection(fd, vsocket);
504 RTE_LOG(WARNING, VHOST_CONFIG,
505 "failed to connect to %s: %s\n",
506 path, strerror(errno));
508 if (ret == -2 || !vsocket->reconnect) {
513 RTE_LOG(INFO, VHOST_CONFIG, "%s: reconnecting...\n", path);
514 reconn = malloc(sizeof(*reconn));
515 if (reconn == NULL) {
516 RTE_LOG(ERR, VHOST_CONFIG,
517 "failed to allocate memory for reconnect\n");
521 reconn->un = vsocket->un;
523 reconn->vsocket = vsocket;
524 pthread_mutex_lock(&reconn_list.mutex);
525 TAILQ_INSERT_TAIL(&reconn_list.head, reconn, next);
526 pthread_mutex_unlock(&reconn_list.mutex);
531 static struct vhost_user_socket *
532 find_vhost_user_socket(const char *path)
536 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
537 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
539 if (!strcmp(vsocket->path, path))
547 rte_vhost_driver_disable_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 /* Note that use_builtin_virtio_net is not affected by this function
555 * since callers may want to selectively disable features of the
556 * built-in vhost net device backend.
560 vsocket->features &= ~features;
561 pthread_mutex_unlock(&vhost_user.mutex);
563 return vsocket ? 0 : -1;
567 rte_vhost_driver_enable_features(const char *path, uint64_t features)
569 struct vhost_user_socket *vsocket;
571 pthread_mutex_lock(&vhost_user.mutex);
572 vsocket = find_vhost_user_socket(path);
574 if ((vsocket->supported_features & features) != features) {
576 * trying to enable features the driver doesn't
579 pthread_mutex_unlock(&vhost_user.mutex);
582 vsocket->features |= features;
584 pthread_mutex_unlock(&vhost_user.mutex);
586 return vsocket ? 0 : -1;
590 rte_vhost_driver_set_features(const char *path, uint64_t features)
592 struct vhost_user_socket *vsocket;
594 pthread_mutex_lock(&vhost_user.mutex);
595 vsocket = find_vhost_user_socket(path);
597 vsocket->supported_features = features;
598 vsocket->features = features;
600 /* Anyone setting feature bits is implementing their own vhost
603 vsocket->use_builtin_virtio_net = false;
605 pthread_mutex_unlock(&vhost_user.mutex);
607 return vsocket ? 0 : -1;
611 rte_vhost_driver_get_features(const char *path, uint64_t *features)
613 struct vhost_user_socket *vsocket;
615 pthread_mutex_lock(&vhost_user.mutex);
616 vsocket = find_vhost_user_socket(path);
618 *features = vsocket->features;
619 pthread_mutex_unlock(&vhost_user.mutex);
622 RTE_LOG(ERR, VHOST_CONFIG,
623 "socket file %s is not registered yet.\n", path);
631 * Register a new vhost-user socket; here we could act as server
632 * (the default case), or client (when RTE_VHOST_USER_CLIENT) flag
636 rte_vhost_driver_register(const char *path, uint64_t flags)
639 struct vhost_user_socket *vsocket;
644 pthread_mutex_lock(&vhost_user.mutex);
646 if (vhost_user.vsocket_cnt == MAX_VHOST_SOCKET) {
647 RTE_LOG(ERR, VHOST_CONFIG,
648 "error: the number of vhost sockets reaches maximum\n");
652 vsocket = malloc(sizeof(struct vhost_user_socket));
655 memset(vsocket, 0, sizeof(struct vhost_user_socket));
656 vsocket->path = strdup(path);
657 if (vsocket->path == NULL) {
658 RTE_LOG(ERR, VHOST_CONFIG,
659 "error: failed to copy socket path string\n");
663 TAILQ_INIT(&vsocket->conn_list);
664 ret = pthread_mutex_init(&vsocket->conn_mutex, NULL);
666 RTE_LOG(ERR, VHOST_CONFIG,
667 "error: failed to init connection mutex\n");
670 vsocket->dequeue_zero_copy = flags & RTE_VHOST_USER_DEQUEUE_ZERO_COPY;
673 * Set the supported features correctly for the builtin vhost-user
676 * Applications know nothing about features the builtin virtio net
677 * driver (virtio_net.c) supports, thus it's not possible for them
678 * to invoke rte_vhost_driver_set_features(). To workaround it, here
679 * we set it unconditionally. If the application want to implement
680 * another vhost-user driver (say SCSI), it should call the
681 * rte_vhost_driver_set_features(), which will overwrite following
684 vsocket->use_builtin_virtio_net = true;
685 vsocket->supported_features = VIRTIO_NET_SUPPORTED_FEATURES;
686 vsocket->features = VIRTIO_NET_SUPPORTED_FEATURES;
688 if (!(flags & RTE_VHOST_USER_IOMMU_SUPPORT)) {
689 vsocket->supported_features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
690 vsocket->features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
693 if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
694 vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
695 if (vsocket->reconnect && reconn_tid == 0) {
696 if (vhost_user_reconnect_init() != 0)
700 vsocket->is_server = true;
702 ret = create_unix_socket(vsocket);
707 vhost_user.vsockets[vhost_user.vsocket_cnt++] = vsocket;
709 pthread_mutex_unlock(&vhost_user.mutex);
713 if (pthread_mutex_destroy(&vsocket->conn_mutex)) {
714 RTE_LOG(ERR, VHOST_CONFIG,
715 "error: failed to destroy connection mutex\n");
721 pthread_mutex_unlock(&vhost_user.mutex);
727 vhost_user_remove_reconnect(struct vhost_user_socket *vsocket)
730 struct vhost_user_reconnect *reconn, *next;
732 pthread_mutex_lock(&reconn_list.mutex);
734 for (reconn = TAILQ_FIRST(&reconn_list.head);
735 reconn != NULL; reconn = next) {
736 next = TAILQ_NEXT(reconn, next);
738 if (reconn->vsocket == vsocket) {
739 TAILQ_REMOVE(&reconn_list.head, reconn, next);
746 pthread_mutex_unlock(&reconn_list.mutex);
751 * Unregister the specified vhost socket
754 rte_vhost_driver_unregister(const char *path)
758 struct vhost_user_connection *conn, *next;
760 pthread_mutex_lock(&vhost_user.mutex);
762 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
763 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
765 if (!strcmp(vsocket->path, path)) {
766 if (vsocket->is_server) {
767 fdset_del(&vhost_user.fdset, vsocket->socket_fd);
768 close(vsocket->socket_fd);
770 } else if (vsocket->reconnect) {
771 vhost_user_remove_reconnect(vsocket);
774 pthread_mutex_lock(&vsocket->conn_mutex);
775 for (conn = TAILQ_FIRST(&vsocket->conn_list);
778 next = TAILQ_NEXT(conn, next);
780 fdset_del(&vhost_user.fdset, conn->connfd);
781 RTE_LOG(INFO, VHOST_CONFIG,
782 "free connfd = %d for device '%s'\n",
785 vhost_destroy_device(conn->vid);
786 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
789 pthread_mutex_unlock(&vsocket->conn_mutex);
791 pthread_mutex_destroy(&vsocket->conn_mutex);
795 count = --vhost_user.vsocket_cnt;
796 vhost_user.vsockets[i] = vhost_user.vsockets[count];
797 vhost_user.vsockets[count] = NULL;
798 pthread_mutex_unlock(&vhost_user.mutex);
803 pthread_mutex_unlock(&vhost_user.mutex);
809 * Register ops so that we can add/remove device to data core.
812 rte_vhost_driver_callback_register(const char *path,
813 struct vhost_device_ops const * const ops)
815 struct vhost_user_socket *vsocket;
817 pthread_mutex_lock(&vhost_user.mutex);
818 vsocket = find_vhost_user_socket(path);
820 vsocket->notify_ops = ops;
821 pthread_mutex_unlock(&vhost_user.mutex);
823 return vsocket ? 0 : -1;
826 struct vhost_device_ops const *
827 vhost_driver_callback_get(const char *path)
829 struct vhost_user_socket *vsocket;
831 pthread_mutex_lock(&vhost_user.mutex);
832 vsocket = find_vhost_user_socket(path);
833 pthread_mutex_unlock(&vhost_user.mutex);
835 return vsocket ? vsocket->notify_ops : NULL;
839 rte_vhost_driver_start(const char *path)
841 struct vhost_user_socket *vsocket;
842 static pthread_t fdset_tid;
843 char thread_name[RTE_MAX_THREAD_NAME_LEN];
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 snprintf(thread_name, RTE_MAX_THREAD_NAME_LEN,
863 if (rte_thread_setname(fdset_tid, thread_name)) {
864 RTE_LOG(DEBUG, VHOST_CONFIG,
865 "failed to set vhost-event thread name");
870 if (vsocket->is_server)
871 return vhost_user_start_server(vsocket);
873 return vhost_user_start_client(vsocket);