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);
246 fdset_pipe_notify(&vhost_user.fdset);
254 /* call back when there is new vhost-user connection from client */
256 vhost_user_server_new_connection(int fd, void *dat, int *remove __rte_unused)
258 struct vhost_user_socket *vsocket = dat;
260 fd = accept(fd, NULL, NULL);
264 RTE_LOG(INFO, VHOST_CONFIG, "new vhost user connection is %d\n", fd);
265 vhost_user_add_connection(fd, vsocket);
269 vhost_user_read_cb(int connfd, void *dat, int *remove)
271 struct vhost_user_connection *conn = dat;
272 struct vhost_user_socket *vsocket = conn->vsocket;
275 ret = vhost_user_msg_handler(conn->vid, connfd);
279 vhost_destroy_device(conn->vid);
281 if (vsocket->notify_ops->destroy_connection)
282 vsocket->notify_ops->destroy_connection(conn->vid);
284 pthread_mutex_lock(&vsocket->conn_mutex);
285 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
286 pthread_mutex_unlock(&vsocket->conn_mutex);
290 if (vsocket->reconnect) {
291 create_unix_socket(vsocket);
292 vhost_user_start_client(vsocket);
298 create_unix_socket(struct vhost_user_socket *vsocket)
301 struct sockaddr_un *un = &vsocket->un;
303 fd = socket(AF_UNIX, SOCK_STREAM, 0);
306 RTE_LOG(INFO, VHOST_CONFIG, "vhost-user %s: socket created, fd: %d\n",
307 vsocket->is_server ? "server" : "client", fd);
309 if (!vsocket->is_server && fcntl(fd, F_SETFL, O_NONBLOCK)) {
310 RTE_LOG(ERR, VHOST_CONFIG,
311 "vhost-user: can't set nonblocking mode for socket, fd: "
312 "%d (%s)\n", fd, strerror(errno));
317 memset(un, 0, sizeof(*un));
318 un->sun_family = AF_UNIX;
319 strncpy(un->sun_path, vsocket->path, sizeof(un->sun_path));
320 un->sun_path[sizeof(un->sun_path) - 1] = '\0';
322 vsocket->socket_fd = fd;
327 vhost_user_start_server(struct vhost_user_socket *vsocket)
330 int fd = vsocket->socket_fd;
331 const char *path = vsocket->path;
334 * bind () may fail if the socket file with the same name already
335 * exists. But the library obviously should not delete the file
336 * provided by the user, since we can not be sure that it is not
337 * being used by other applications. Moreover, many applications form
338 * socket names based on user input, which is prone to errors.
340 * The user must ensure that the socket does not exist before
341 * registering the vhost driver in server mode.
343 ret = bind(fd, (struct sockaddr *)&vsocket->un, sizeof(vsocket->un));
345 RTE_LOG(ERR, VHOST_CONFIG,
346 "failed to bind to %s: %s; remove it and try again\n",
347 path, strerror(errno));
350 RTE_LOG(INFO, VHOST_CONFIG, "bind to %s\n", path);
352 ret = listen(fd, MAX_VIRTIO_BACKLOG);
356 ret = fdset_add(&vhost_user.fdset, fd, vhost_user_server_new_connection,
359 RTE_LOG(ERR, VHOST_CONFIG,
360 "failed to add listen fd %d to vhost server fdset\n",
372 struct vhost_user_reconnect {
373 struct sockaddr_un un;
375 struct vhost_user_socket *vsocket;
377 TAILQ_ENTRY(vhost_user_reconnect) next;
380 TAILQ_HEAD(vhost_user_reconnect_tailq_list, vhost_user_reconnect);
381 struct vhost_user_reconnect_list {
382 struct vhost_user_reconnect_tailq_list head;
383 pthread_mutex_t mutex;
386 static struct vhost_user_reconnect_list reconn_list;
387 static pthread_t reconn_tid;
390 vhost_user_connect_nonblock(int fd, struct sockaddr *un, size_t sz)
394 ret = connect(fd, un, sz);
395 if (ret < 0 && errno != EISCONN)
398 flags = fcntl(fd, F_GETFL, 0);
400 RTE_LOG(ERR, VHOST_CONFIG,
401 "can't get flags for connfd %d\n", fd);
404 if ((flags & O_NONBLOCK) && fcntl(fd, F_SETFL, flags & ~O_NONBLOCK)) {
405 RTE_LOG(ERR, VHOST_CONFIG,
406 "can't disable nonblocking on fd %d\n", fd);
413 vhost_user_client_reconnect(void *arg __rte_unused)
416 struct vhost_user_reconnect *reconn, *next;
419 pthread_mutex_lock(&reconn_list.mutex);
422 * An equal implementation of TAILQ_FOREACH_SAFE,
423 * which does not exist on all platforms.
425 for (reconn = TAILQ_FIRST(&reconn_list.head);
426 reconn != NULL; reconn = next) {
427 next = TAILQ_NEXT(reconn, next);
429 ret = vhost_user_connect_nonblock(reconn->fd,
430 (struct sockaddr *)&reconn->un,
434 RTE_LOG(ERR, VHOST_CONFIG,
435 "reconnection for fd %d failed\n",
442 RTE_LOG(INFO, VHOST_CONFIG,
443 "%s: connected\n", reconn->vsocket->path);
444 vhost_user_add_connection(reconn->fd, reconn->vsocket);
446 TAILQ_REMOVE(&reconn_list.head, reconn, next);
450 pthread_mutex_unlock(&reconn_list.mutex);
458 vhost_user_reconnect_init(void)
461 char thread_name[RTE_MAX_THREAD_NAME_LEN];
463 ret = pthread_mutex_init(&reconn_list.mutex, NULL);
465 RTE_LOG(ERR, VHOST_CONFIG, "failed to initialize mutex");
468 TAILQ_INIT(&reconn_list.head);
470 ret = pthread_create(&reconn_tid, NULL,
471 vhost_user_client_reconnect, NULL);
473 RTE_LOG(ERR, VHOST_CONFIG, "failed to create reconnect thread");
474 if (pthread_mutex_destroy(&reconn_list.mutex)) {
475 RTE_LOG(ERR, VHOST_CONFIG,
476 "failed to destroy reconnect mutex");
479 snprintf(thread_name, RTE_MAX_THREAD_NAME_LEN,
482 if (rte_thread_setname(reconn_tid, thread_name)) {
483 RTE_LOG(DEBUG, VHOST_CONFIG,
484 "failed to set reconnect thread name");
492 vhost_user_start_client(struct vhost_user_socket *vsocket)
495 int fd = vsocket->socket_fd;
496 const char *path = vsocket->path;
497 struct vhost_user_reconnect *reconn;
499 ret = vhost_user_connect_nonblock(fd, (struct sockaddr *)&vsocket->un,
500 sizeof(vsocket->un));
502 vhost_user_add_connection(fd, vsocket);
506 RTE_LOG(WARNING, VHOST_CONFIG,
507 "failed to connect to %s: %s\n",
508 path, strerror(errno));
510 if (ret == -2 || !vsocket->reconnect) {
515 RTE_LOG(INFO, VHOST_CONFIG, "%s: reconnecting...\n", path);
516 reconn = malloc(sizeof(*reconn));
517 if (reconn == NULL) {
518 RTE_LOG(ERR, VHOST_CONFIG,
519 "failed to allocate memory for reconnect\n");
523 reconn->un = vsocket->un;
525 reconn->vsocket = vsocket;
526 pthread_mutex_lock(&reconn_list.mutex);
527 TAILQ_INSERT_TAIL(&reconn_list.head, reconn, next);
528 pthread_mutex_unlock(&reconn_list.mutex);
533 static struct vhost_user_socket *
534 find_vhost_user_socket(const char *path)
538 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
539 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
541 if (!strcmp(vsocket->path, path))
549 rte_vhost_driver_disable_features(const char *path, uint64_t features)
551 struct vhost_user_socket *vsocket;
553 pthread_mutex_lock(&vhost_user.mutex);
554 vsocket = find_vhost_user_socket(path);
556 /* Note that use_builtin_virtio_net is not affected by this function
557 * since callers may want to selectively disable features of the
558 * built-in vhost net device backend.
562 vsocket->features &= ~features;
563 pthread_mutex_unlock(&vhost_user.mutex);
565 return vsocket ? 0 : -1;
569 rte_vhost_driver_enable_features(const char *path, uint64_t features)
571 struct vhost_user_socket *vsocket;
573 pthread_mutex_lock(&vhost_user.mutex);
574 vsocket = find_vhost_user_socket(path);
576 if ((vsocket->supported_features & features) != features) {
578 * trying to enable features the driver doesn't
581 pthread_mutex_unlock(&vhost_user.mutex);
584 vsocket->features |= features;
586 pthread_mutex_unlock(&vhost_user.mutex);
588 return vsocket ? 0 : -1;
592 rte_vhost_driver_set_features(const char *path, uint64_t features)
594 struct vhost_user_socket *vsocket;
596 pthread_mutex_lock(&vhost_user.mutex);
597 vsocket = find_vhost_user_socket(path);
599 vsocket->supported_features = features;
600 vsocket->features = features;
602 /* Anyone setting feature bits is implementing their own vhost
605 vsocket->use_builtin_virtio_net = false;
607 pthread_mutex_unlock(&vhost_user.mutex);
609 return vsocket ? 0 : -1;
613 rte_vhost_driver_get_features(const char *path, uint64_t *features)
615 struct vhost_user_socket *vsocket;
617 pthread_mutex_lock(&vhost_user.mutex);
618 vsocket = find_vhost_user_socket(path);
620 *features = vsocket->features;
621 pthread_mutex_unlock(&vhost_user.mutex);
624 RTE_LOG(ERR, VHOST_CONFIG,
625 "socket file %s is not registered yet.\n", path);
633 * Register a new vhost-user socket; here we could act as server
634 * (the default case), or client (when RTE_VHOST_USER_CLIENT) flag
638 rte_vhost_driver_register(const char *path, uint64_t flags)
641 struct vhost_user_socket *vsocket;
646 pthread_mutex_lock(&vhost_user.mutex);
648 if (vhost_user.vsocket_cnt == MAX_VHOST_SOCKET) {
649 RTE_LOG(ERR, VHOST_CONFIG,
650 "error: the number of vhost sockets reaches maximum\n");
654 vsocket = malloc(sizeof(struct vhost_user_socket));
657 memset(vsocket, 0, sizeof(struct vhost_user_socket));
658 vsocket->path = strdup(path);
659 if (vsocket->path == NULL) {
660 RTE_LOG(ERR, VHOST_CONFIG,
661 "error: failed to copy socket path string\n");
665 TAILQ_INIT(&vsocket->conn_list);
666 ret = pthread_mutex_init(&vsocket->conn_mutex, NULL);
668 RTE_LOG(ERR, VHOST_CONFIG,
669 "error: failed to init connection mutex\n");
672 vsocket->dequeue_zero_copy = flags & RTE_VHOST_USER_DEQUEUE_ZERO_COPY;
675 * Set the supported features correctly for the builtin vhost-user
678 * Applications know nothing about features the builtin virtio net
679 * driver (virtio_net.c) supports, thus it's not possible for them
680 * to invoke rte_vhost_driver_set_features(). To workaround it, here
681 * we set it unconditionally. If the application want to implement
682 * another vhost-user driver (say SCSI), it should call the
683 * rte_vhost_driver_set_features(), which will overwrite following
686 vsocket->use_builtin_virtio_net = true;
687 vsocket->supported_features = VIRTIO_NET_SUPPORTED_FEATURES;
688 vsocket->features = VIRTIO_NET_SUPPORTED_FEATURES;
690 if (!(flags & RTE_VHOST_USER_IOMMU_SUPPORT)) {
691 vsocket->supported_features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
692 vsocket->features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
695 if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
696 vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
697 if (vsocket->reconnect && reconn_tid == 0) {
698 if (vhost_user_reconnect_init() != 0)
702 vsocket->is_server = true;
704 ret = create_unix_socket(vsocket);
709 vhost_user.vsockets[vhost_user.vsocket_cnt++] = vsocket;
711 pthread_mutex_unlock(&vhost_user.mutex);
715 if (pthread_mutex_destroy(&vsocket->conn_mutex)) {
716 RTE_LOG(ERR, VHOST_CONFIG,
717 "error: failed to destroy connection mutex\n");
723 pthread_mutex_unlock(&vhost_user.mutex);
729 vhost_user_remove_reconnect(struct vhost_user_socket *vsocket)
732 struct vhost_user_reconnect *reconn, *next;
734 pthread_mutex_lock(&reconn_list.mutex);
736 for (reconn = TAILQ_FIRST(&reconn_list.head);
737 reconn != NULL; reconn = next) {
738 next = TAILQ_NEXT(reconn, next);
740 if (reconn->vsocket == vsocket) {
741 TAILQ_REMOVE(&reconn_list.head, reconn, next);
748 pthread_mutex_unlock(&reconn_list.mutex);
753 * Unregister the specified vhost socket
756 rte_vhost_driver_unregister(const char *path)
760 struct vhost_user_connection *conn, *next;
762 pthread_mutex_lock(&vhost_user.mutex);
764 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
765 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
767 if (!strcmp(vsocket->path, path)) {
768 if (vsocket->is_server) {
769 fdset_del(&vhost_user.fdset, vsocket->socket_fd);
770 close(vsocket->socket_fd);
772 } else if (vsocket->reconnect) {
773 vhost_user_remove_reconnect(vsocket);
776 pthread_mutex_lock(&vsocket->conn_mutex);
777 for (conn = TAILQ_FIRST(&vsocket->conn_list);
780 next = TAILQ_NEXT(conn, next);
782 fdset_del(&vhost_user.fdset, conn->connfd);
783 RTE_LOG(INFO, VHOST_CONFIG,
784 "free connfd = %d for device '%s'\n",
787 vhost_destroy_device(conn->vid);
788 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
791 pthread_mutex_unlock(&vsocket->conn_mutex);
793 pthread_mutex_destroy(&vsocket->conn_mutex);
797 count = --vhost_user.vsocket_cnt;
798 vhost_user.vsockets[i] = vhost_user.vsockets[count];
799 vhost_user.vsockets[count] = NULL;
800 pthread_mutex_unlock(&vhost_user.mutex);
805 pthread_mutex_unlock(&vhost_user.mutex);
811 * Register ops so that we can add/remove device to data core.
814 rte_vhost_driver_callback_register(const char *path,
815 struct vhost_device_ops const * const ops)
817 struct vhost_user_socket *vsocket;
819 pthread_mutex_lock(&vhost_user.mutex);
820 vsocket = find_vhost_user_socket(path);
822 vsocket->notify_ops = ops;
823 pthread_mutex_unlock(&vhost_user.mutex);
825 return vsocket ? 0 : -1;
828 struct vhost_device_ops const *
829 vhost_driver_callback_get(const char *path)
831 struct vhost_user_socket *vsocket;
833 pthread_mutex_lock(&vhost_user.mutex);
834 vsocket = find_vhost_user_socket(path);
835 pthread_mutex_unlock(&vhost_user.mutex);
837 return vsocket ? vsocket->notify_ops : NULL;
841 rte_vhost_driver_start(const char *path)
843 struct vhost_user_socket *vsocket;
844 static pthread_t fdset_tid;
845 char thread_name[RTE_MAX_THREAD_NAME_LEN];
847 pthread_mutex_lock(&vhost_user.mutex);
848 vsocket = find_vhost_user_socket(path);
849 pthread_mutex_unlock(&vhost_user.mutex);
854 if (fdset_tid == 0) {
856 * create a pipe which will be waited by poll and notified to
857 * rebuild the wait list of poll.
859 if (fdset_pipe_init(&vhost_user.fdset) < 0) {
860 RTE_LOG(ERR, VHOST_CONFIG,
861 "failed to create pipe for vhost fdset\n");
865 int ret = pthread_create(&fdset_tid, NULL, fdset_event_dispatch,
868 RTE_LOG(ERR, VHOST_CONFIG,
869 "failed to create fdset handling thread");
871 fdset_pipe_uninit(&vhost_user.fdset);
874 snprintf(thread_name, RTE_MAX_THREAD_NAME_LEN,
877 if (rte_thread_setname(fdset_tid, thread_name)) {
878 RTE_LOG(DEBUG, VHOST_CONFIG,
879 "failed to set vhost-event thread name");
884 if (vsocket->is_server)
885 return vhost_user_start_server(vsocket);
887 return vhost_user_start_client(vsocket);