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;
332 ret = bind(fd, (struct sockaddr *)&vsocket->un, sizeof(vsocket->un));
334 RTE_LOG(ERR, VHOST_CONFIG,
335 "failed to bind to %s: %s; remove it and try again\n",
336 path, strerror(errno));
339 RTE_LOG(INFO, VHOST_CONFIG, "bind to %s\n", path);
341 ret = listen(fd, MAX_VIRTIO_BACKLOG);
345 ret = fdset_add(&vhost_user.fdset, fd, vhost_user_server_new_connection,
348 RTE_LOG(ERR, VHOST_CONFIG,
349 "failed to add listen fd %d to vhost server fdset\n",
361 struct vhost_user_reconnect {
362 struct sockaddr_un un;
364 struct vhost_user_socket *vsocket;
366 TAILQ_ENTRY(vhost_user_reconnect) next;
369 TAILQ_HEAD(vhost_user_reconnect_tailq_list, vhost_user_reconnect);
370 struct vhost_user_reconnect_list {
371 struct vhost_user_reconnect_tailq_list head;
372 pthread_mutex_t mutex;
375 static struct vhost_user_reconnect_list reconn_list;
376 static pthread_t reconn_tid;
379 vhost_user_connect_nonblock(int fd, struct sockaddr *un, size_t sz)
383 ret = connect(fd, un, sz);
384 if (ret < 0 && errno != EISCONN)
387 flags = fcntl(fd, F_GETFL, 0);
389 RTE_LOG(ERR, VHOST_CONFIG,
390 "can't get flags for connfd %d\n", fd);
393 if ((flags & O_NONBLOCK) && fcntl(fd, F_SETFL, flags & ~O_NONBLOCK)) {
394 RTE_LOG(ERR, VHOST_CONFIG,
395 "can't disable nonblocking on fd %d\n", fd);
402 vhost_user_client_reconnect(void *arg __rte_unused)
405 struct vhost_user_reconnect *reconn, *next;
408 pthread_mutex_lock(&reconn_list.mutex);
411 * An equal implementation of TAILQ_FOREACH_SAFE,
412 * which does not exist on all platforms.
414 for (reconn = TAILQ_FIRST(&reconn_list.head);
415 reconn != NULL; reconn = next) {
416 next = TAILQ_NEXT(reconn, next);
418 ret = vhost_user_connect_nonblock(reconn->fd,
419 (struct sockaddr *)&reconn->un,
423 RTE_LOG(ERR, VHOST_CONFIG,
424 "reconnection for fd %d failed\n",
431 RTE_LOG(INFO, VHOST_CONFIG,
432 "%s: connected\n", reconn->vsocket->path);
433 vhost_user_add_connection(reconn->fd, reconn->vsocket);
435 TAILQ_REMOVE(&reconn_list.head, reconn, next);
439 pthread_mutex_unlock(&reconn_list.mutex);
447 vhost_user_reconnect_init(void)
450 char thread_name[RTE_MAX_THREAD_NAME_LEN];
452 ret = pthread_mutex_init(&reconn_list.mutex, NULL);
454 RTE_LOG(ERR, VHOST_CONFIG, "failed to initialize mutex");
457 TAILQ_INIT(&reconn_list.head);
459 ret = pthread_create(&reconn_tid, NULL,
460 vhost_user_client_reconnect, NULL);
462 RTE_LOG(ERR, VHOST_CONFIG, "failed to create reconnect thread");
463 if (pthread_mutex_destroy(&reconn_list.mutex)) {
464 RTE_LOG(ERR, VHOST_CONFIG,
465 "failed to destroy reconnect mutex");
468 snprintf(thread_name, RTE_MAX_THREAD_NAME_LEN,
471 if (rte_thread_setname(reconn_tid, thread_name)) {
472 RTE_LOG(DEBUG, VHOST_CONFIG,
473 "failed to set reconnect thread name");
481 vhost_user_start_client(struct vhost_user_socket *vsocket)
484 int fd = vsocket->socket_fd;
485 const char *path = vsocket->path;
486 struct vhost_user_reconnect *reconn;
488 ret = vhost_user_connect_nonblock(fd, (struct sockaddr *)&vsocket->un,
489 sizeof(vsocket->un));
491 vhost_user_add_connection(fd, vsocket);
495 RTE_LOG(WARNING, VHOST_CONFIG,
496 "failed to connect to %s: %s\n",
497 path, strerror(errno));
499 if (ret == -2 || !vsocket->reconnect) {
504 RTE_LOG(INFO, VHOST_CONFIG, "%s: reconnecting...\n", path);
505 reconn = malloc(sizeof(*reconn));
506 if (reconn == NULL) {
507 RTE_LOG(ERR, VHOST_CONFIG,
508 "failed to allocate memory for reconnect\n");
512 reconn->un = vsocket->un;
514 reconn->vsocket = vsocket;
515 pthread_mutex_lock(&reconn_list.mutex);
516 TAILQ_INSERT_TAIL(&reconn_list.head, reconn, next);
517 pthread_mutex_unlock(&reconn_list.mutex);
522 static struct vhost_user_socket *
523 find_vhost_user_socket(const char *path)
527 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
528 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
530 if (!strcmp(vsocket->path, path))
538 rte_vhost_driver_disable_features(const char *path, uint64_t features)
540 struct vhost_user_socket *vsocket;
542 pthread_mutex_lock(&vhost_user.mutex);
543 vsocket = find_vhost_user_socket(path);
545 /* Note that use_builtin_virtio_net is not affected by this function
546 * since callers may want to selectively disable features of the
547 * built-in vhost net device backend.
551 vsocket->features &= ~features;
552 pthread_mutex_unlock(&vhost_user.mutex);
554 return vsocket ? 0 : -1;
558 rte_vhost_driver_enable_features(const char *path, uint64_t features)
560 struct vhost_user_socket *vsocket;
562 pthread_mutex_lock(&vhost_user.mutex);
563 vsocket = find_vhost_user_socket(path);
565 if ((vsocket->supported_features & features) != features) {
567 * trying to enable features the driver doesn't
570 pthread_mutex_unlock(&vhost_user.mutex);
573 vsocket->features |= features;
575 pthread_mutex_unlock(&vhost_user.mutex);
577 return vsocket ? 0 : -1;
581 rte_vhost_driver_set_features(const char *path, uint64_t features)
583 struct vhost_user_socket *vsocket;
585 pthread_mutex_lock(&vhost_user.mutex);
586 vsocket = find_vhost_user_socket(path);
588 vsocket->supported_features = features;
589 vsocket->features = features;
591 /* Anyone setting feature bits is implementing their own vhost
594 vsocket->use_builtin_virtio_net = false;
596 pthread_mutex_unlock(&vhost_user.mutex);
598 return vsocket ? 0 : -1;
602 rte_vhost_driver_get_features(const char *path, uint64_t *features)
604 struct vhost_user_socket *vsocket;
606 pthread_mutex_lock(&vhost_user.mutex);
607 vsocket = find_vhost_user_socket(path);
609 *features = vsocket->features;
610 pthread_mutex_unlock(&vhost_user.mutex);
613 RTE_LOG(ERR, VHOST_CONFIG,
614 "socket file %s is not registered yet.\n", path);
622 * Register a new vhost-user socket; here we could act as server
623 * (the default case), or client (when RTE_VHOST_USER_CLIENT) flag
627 rte_vhost_driver_register(const char *path, uint64_t flags)
630 struct vhost_user_socket *vsocket;
635 pthread_mutex_lock(&vhost_user.mutex);
637 if (vhost_user.vsocket_cnt == MAX_VHOST_SOCKET) {
638 RTE_LOG(ERR, VHOST_CONFIG,
639 "error: the number of vhost sockets reaches maximum\n");
643 vsocket = malloc(sizeof(struct vhost_user_socket));
646 memset(vsocket, 0, sizeof(struct vhost_user_socket));
647 vsocket->path = strdup(path);
648 if (vsocket->path == NULL) {
649 RTE_LOG(ERR, VHOST_CONFIG,
650 "error: failed to copy socket path string\n");
654 TAILQ_INIT(&vsocket->conn_list);
655 ret = pthread_mutex_init(&vsocket->conn_mutex, NULL);
657 RTE_LOG(ERR, VHOST_CONFIG,
658 "error: failed to init connection mutex\n");
661 vsocket->dequeue_zero_copy = flags & RTE_VHOST_USER_DEQUEUE_ZERO_COPY;
664 * Set the supported features correctly for the builtin vhost-user
667 * Applications know nothing about features the builtin virtio net
668 * driver (virtio_net.c) supports, thus it's not possible for them
669 * to invoke rte_vhost_driver_set_features(). To workaround it, here
670 * we set it unconditionally. If the application want to implement
671 * another vhost-user driver (say SCSI), it should call the
672 * rte_vhost_driver_set_features(), which will overwrite following
675 vsocket->use_builtin_virtio_net = true;
676 vsocket->supported_features = VIRTIO_NET_SUPPORTED_FEATURES;
677 vsocket->features = VIRTIO_NET_SUPPORTED_FEATURES;
679 if (!(flags & RTE_VHOST_USER_IOMMU_SUPPORT)) {
680 vsocket->supported_features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
681 vsocket->features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
684 if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
685 vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
686 if (vsocket->reconnect && reconn_tid == 0) {
687 if (vhost_user_reconnect_init() != 0)
691 vsocket->is_server = true;
693 ret = create_unix_socket(vsocket);
698 vhost_user.vsockets[vhost_user.vsocket_cnt++] = vsocket;
700 pthread_mutex_unlock(&vhost_user.mutex);
704 if (pthread_mutex_destroy(&vsocket->conn_mutex)) {
705 RTE_LOG(ERR, VHOST_CONFIG,
706 "error: failed to destroy connection mutex\n");
712 pthread_mutex_unlock(&vhost_user.mutex);
718 vhost_user_remove_reconnect(struct vhost_user_socket *vsocket)
721 struct vhost_user_reconnect *reconn, *next;
723 pthread_mutex_lock(&reconn_list.mutex);
725 for (reconn = TAILQ_FIRST(&reconn_list.head);
726 reconn != NULL; reconn = next) {
727 next = TAILQ_NEXT(reconn, next);
729 if (reconn->vsocket == vsocket) {
730 TAILQ_REMOVE(&reconn_list.head, reconn, next);
737 pthread_mutex_unlock(&reconn_list.mutex);
742 * Unregister the specified vhost socket
745 rte_vhost_driver_unregister(const char *path)
749 struct vhost_user_connection *conn, *next;
751 pthread_mutex_lock(&vhost_user.mutex);
753 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
754 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
756 if (!strcmp(vsocket->path, path)) {
757 if (vsocket->is_server) {
758 fdset_del(&vhost_user.fdset, vsocket->socket_fd);
759 close(vsocket->socket_fd);
761 } else if (vsocket->reconnect) {
762 vhost_user_remove_reconnect(vsocket);
765 pthread_mutex_lock(&vsocket->conn_mutex);
766 for (conn = TAILQ_FIRST(&vsocket->conn_list);
769 next = TAILQ_NEXT(conn, next);
771 fdset_del(&vhost_user.fdset, conn->connfd);
772 RTE_LOG(INFO, VHOST_CONFIG,
773 "free connfd = %d for device '%s'\n",
776 vhost_destroy_device(conn->vid);
777 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
780 pthread_mutex_unlock(&vsocket->conn_mutex);
782 pthread_mutex_destroy(&vsocket->conn_mutex);
786 count = --vhost_user.vsocket_cnt;
787 vhost_user.vsockets[i] = vhost_user.vsockets[count];
788 vhost_user.vsockets[count] = NULL;
789 pthread_mutex_unlock(&vhost_user.mutex);
794 pthread_mutex_unlock(&vhost_user.mutex);
800 * Register ops so that we can add/remove device to data core.
803 rte_vhost_driver_callback_register(const char *path,
804 struct vhost_device_ops const * const ops)
806 struct vhost_user_socket *vsocket;
808 pthread_mutex_lock(&vhost_user.mutex);
809 vsocket = find_vhost_user_socket(path);
811 vsocket->notify_ops = ops;
812 pthread_mutex_unlock(&vhost_user.mutex);
814 return vsocket ? 0 : -1;
817 struct vhost_device_ops const *
818 vhost_driver_callback_get(const char *path)
820 struct vhost_user_socket *vsocket;
822 pthread_mutex_lock(&vhost_user.mutex);
823 vsocket = find_vhost_user_socket(path);
824 pthread_mutex_unlock(&vhost_user.mutex);
826 return vsocket ? vsocket->notify_ops : NULL;
830 rte_vhost_driver_start(const char *path)
832 struct vhost_user_socket *vsocket;
833 static pthread_t fdset_tid;
835 pthread_mutex_lock(&vhost_user.mutex);
836 vsocket = find_vhost_user_socket(path);
837 pthread_mutex_unlock(&vhost_user.mutex);
842 if (fdset_tid == 0) {
843 int ret = pthread_create(&fdset_tid, NULL, fdset_event_dispatch,
846 RTE_LOG(ERR, VHOST_CONFIG,
847 "failed to create fdset handling thread");
850 if (vsocket->is_server)
851 return vhost_user_start_server(vsocket);
853 return vhost_user_start_client(vsocket);