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;
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)];
101 memset(&msgh, 0, sizeof(msgh));
103 iov.iov_len = buflen;
107 msgh.msg_control = control;
108 msgh.msg_controllen = sizeof(control);
110 ret = recvmsg(sockfd, &msgh, 0);
112 RTE_LOG(ERR, VHOST_CONFIG, "recvmsg failed\n");
116 if (msgh.msg_flags & (MSG_TRUNC | MSG_CTRUNC)) {
117 RTE_LOG(ERR, VHOST_CONFIG, "truncted msg\n");
121 for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
122 cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
123 if ((cmsg->cmsg_level == SOL_SOCKET) &&
124 (cmsg->cmsg_type == SCM_RIGHTS)) {
125 memcpy(fds, CMSG_DATA(cmsg), fdsize);
134 send_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
139 size_t fdsize = fd_num * sizeof(int);
140 char control[CMSG_SPACE(fdsize)];
141 struct cmsghdr *cmsg;
144 memset(&msgh, 0, sizeof(msgh));
146 iov.iov_len = buflen;
151 if (fds && fd_num > 0) {
152 msgh.msg_control = control;
153 msgh.msg_controllen = sizeof(control);
154 cmsg = CMSG_FIRSTHDR(&msgh);
155 cmsg->cmsg_len = CMSG_LEN(fdsize);
156 cmsg->cmsg_level = SOL_SOCKET;
157 cmsg->cmsg_type = SCM_RIGHTS;
158 memcpy(CMSG_DATA(cmsg), fds, fdsize);
160 msgh.msg_control = NULL;
161 msgh.msg_controllen = 0;
165 ret = sendmsg(sockfd, &msgh, 0);
166 } while (ret < 0 && errno == EINTR);
169 RTE_LOG(ERR, VHOST_CONFIG, "sendmsg error\n");
177 vhost_user_add_connection(int fd, struct vhost_user_socket *vsocket)
181 struct vhost_user_connection *conn;
184 conn = malloc(sizeof(*conn));
190 vid = vhost_new_device();
195 size = strnlen(vsocket->path, PATH_MAX);
196 vhost_set_ifname(vid, vsocket->path, size);
198 if (vsocket->dequeue_zero_copy)
199 vhost_enable_dequeue_zero_copy(vid);
201 RTE_LOG(INFO, VHOST_CONFIG, "new device, handle is %d\n", vid);
203 if (vsocket->notify_ops->new_connection) {
204 ret = vsocket->notify_ops->new_connection(vid);
206 RTE_LOG(ERR, VHOST_CONFIG,
207 "failed to add vhost user connection with fd %d\n",
214 conn->vsocket = vsocket;
216 ret = fdset_add(&vhost_user.fdset, fd, vhost_user_read_cb,
219 RTE_LOG(ERR, VHOST_CONFIG,
220 "failed to add fd %d into vhost server fdset\n",
223 if (vsocket->notify_ops->destroy_connection)
224 vsocket->notify_ops->destroy_connection(conn->vid);
229 pthread_mutex_lock(&vsocket->conn_mutex);
230 TAILQ_INSERT_TAIL(&vsocket->conn_list, conn, next);
231 pthread_mutex_unlock(&vsocket->conn_mutex);
239 /* call back when there is new vhost-user connection from client */
241 vhost_user_server_new_connection(int fd, void *dat, int *remove __rte_unused)
243 struct vhost_user_socket *vsocket = dat;
245 fd = accept(fd, NULL, NULL);
249 RTE_LOG(INFO, VHOST_CONFIG, "new vhost user connection is %d\n", fd);
250 vhost_user_add_connection(fd, vsocket);
254 vhost_user_read_cb(int connfd, void *dat, int *remove)
256 struct vhost_user_connection *conn = dat;
257 struct vhost_user_socket *vsocket = conn->vsocket;
260 ret = vhost_user_msg_handler(conn->vid, connfd);
264 vhost_destroy_device(conn->vid);
266 if (vsocket->notify_ops->destroy_connection)
267 vsocket->notify_ops->destroy_connection(conn->vid);
269 pthread_mutex_lock(&vsocket->conn_mutex);
270 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
271 pthread_mutex_unlock(&vsocket->conn_mutex);
275 if (vsocket->reconnect) {
276 create_unix_socket(vsocket);
277 vhost_user_start_client(vsocket);
283 create_unix_socket(struct vhost_user_socket *vsocket)
286 struct sockaddr_un *un = &vsocket->un;
288 fd = socket(AF_UNIX, SOCK_STREAM, 0);
291 RTE_LOG(INFO, VHOST_CONFIG, "vhost-user %s: socket created, fd: %d\n",
292 vsocket->is_server ? "server" : "client", fd);
294 if (!vsocket->is_server && fcntl(fd, F_SETFL, O_NONBLOCK)) {
295 RTE_LOG(ERR, VHOST_CONFIG,
296 "vhost-user: can't set nonblocking mode for socket, fd: "
297 "%d (%s)\n", fd, strerror(errno));
302 memset(un, 0, sizeof(*un));
303 un->sun_family = AF_UNIX;
304 strncpy(un->sun_path, vsocket->path, sizeof(un->sun_path));
305 un->sun_path[sizeof(un->sun_path) - 1] = '\0';
307 vsocket->socket_fd = fd;
312 vhost_user_start_server(struct vhost_user_socket *vsocket)
315 int fd = vsocket->socket_fd;
316 const char *path = vsocket->path;
318 ret = bind(fd, (struct sockaddr *)&vsocket->un, sizeof(vsocket->un));
320 RTE_LOG(ERR, VHOST_CONFIG,
321 "failed to bind to %s: %s; remove it and try again\n",
322 path, strerror(errno));
325 RTE_LOG(INFO, VHOST_CONFIG, "bind to %s\n", path);
327 ret = listen(fd, MAX_VIRTIO_BACKLOG);
331 ret = fdset_add(&vhost_user.fdset, fd, vhost_user_server_new_connection,
334 RTE_LOG(ERR, VHOST_CONFIG,
335 "failed to add listen fd %d to vhost server fdset\n",
347 struct vhost_user_reconnect {
348 struct sockaddr_un un;
350 struct vhost_user_socket *vsocket;
352 TAILQ_ENTRY(vhost_user_reconnect) next;
355 TAILQ_HEAD(vhost_user_reconnect_tailq_list, vhost_user_reconnect);
356 struct vhost_user_reconnect_list {
357 struct vhost_user_reconnect_tailq_list head;
358 pthread_mutex_t mutex;
361 static struct vhost_user_reconnect_list reconn_list;
362 static pthread_t reconn_tid;
365 vhost_user_connect_nonblock(int fd, struct sockaddr *un, size_t sz)
369 ret = connect(fd, un, sz);
370 if (ret < 0 && errno != EISCONN)
373 flags = fcntl(fd, F_GETFL, 0);
375 RTE_LOG(ERR, VHOST_CONFIG,
376 "can't get flags for connfd %d\n", fd);
379 if ((flags & O_NONBLOCK) && fcntl(fd, F_SETFL, flags & ~O_NONBLOCK)) {
380 RTE_LOG(ERR, VHOST_CONFIG,
381 "can't disable nonblocking on fd %d\n", fd);
388 vhost_user_client_reconnect(void *arg __rte_unused)
391 struct vhost_user_reconnect *reconn, *next;
394 pthread_mutex_lock(&reconn_list.mutex);
397 * An equal implementation of TAILQ_FOREACH_SAFE,
398 * which does not exist on all platforms.
400 for (reconn = TAILQ_FIRST(&reconn_list.head);
401 reconn != NULL; reconn = next) {
402 next = TAILQ_NEXT(reconn, next);
404 ret = vhost_user_connect_nonblock(reconn->fd,
405 (struct sockaddr *)&reconn->un,
409 RTE_LOG(ERR, VHOST_CONFIG,
410 "reconnection for fd %d failed\n",
417 RTE_LOG(INFO, VHOST_CONFIG,
418 "%s: connected\n", reconn->vsocket->path);
419 vhost_user_add_connection(reconn->fd, reconn->vsocket);
421 TAILQ_REMOVE(&reconn_list.head, reconn, next);
425 pthread_mutex_unlock(&reconn_list.mutex);
433 vhost_user_reconnect_init(void)
437 ret = pthread_mutex_init(&reconn_list.mutex, NULL);
439 RTE_LOG(ERR, VHOST_CONFIG, "failed to initialize mutex");
442 TAILQ_INIT(&reconn_list.head);
444 ret = pthread_create(&reconn_tid, NULL,
445 vhost_user_client_reconnect, NULL);
447 RTE_LOG(ERR, VHOST_CONFIG, "failed to create reconnect thread");
448 if (pthread_mutex_destroy(&reconn_list.mutex)) {
449 RTE_LOG(ERR, VHOST_CONFIG,
450 "failed to destroy reconnect mutex");
458 vhost_user_start_client(struct vhost_user_socket *vsocket)
461 int fd = vsocket->socket_fd;
462 const char *path = vsocket->path;
463 struct vhost_user_reconnect *reconn;
465 ret = vhost_user_connect_nonblock(fd, (struct sockaddr *)&vsocket->un,
466 sizeof(vsocket->un));
468 vhost_user_add_connection(fd, vsocket);
472 RTE_LOG(WARNING, VHOST_CONFIG,
473 "failed to connect to %s: %s\n",
474 path, strerror(errno));
476 if (ret == -2 || !vsocket->reconnect) {
481 RTE_LOG(INFO, VHOST_CONFIG, "%s: reconnecting...\n", path);
482 reconn = malloc(sizeof(*reconn));
483 if (reconn == NULL) {
484 RTE_LOG(ERR, VHOST_CONFIG,
485 "failed to allocate memory for reconnect\n");
489 reconn->un = vsocket->un;
491 reconn->vsocket = vsocket;
492 pthread_mutex_lock(&reconn_list.mutex);
493 TAILQ_INSERT_TAIL(&reconn_list.head, reconn, next);
494 pthread_mutex_unlock(&reconn_list.mutex);
499 static struct vhost_user_socket *
500 find_vhost_user_socket(const char *path)
504 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
505 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
507 if (!strcmp(vsocket->path, path))
515 rte_vhost_driver_disable_features(const char *path, uint64_t features)
517 struct vhost_user_socket *vsocket;
519 pthread_mutex_lock(&vhost_user.mutex);
520 vsocket = find_vhost_user_socket(path);
522 vsocket->features &= ~features;
523 pthread_mutex_unlock(&vhost_user.mutex);
525 return vsocket ? 0 : -1;
529 rte_vhost_driver_enable_features(const char *path, uint64_t features)
531 struct vhost_user_socket *vsocket;
533 pthread_mutex_lock(&vhost_user.mutex);
534 vsocket = find_vhost_user_socket(path);
536 if ((vsocket->supported_features & features) != features) {
538 * trying to enable features the driver doesn't
541 pthread_mutex_unlock(&vhost_user.mutex);
544 vsocket->features |= features;
546 pthread_mutex_unlock(&vhost_user.mutex);
548 return vsocket ? 0 : -1;
552 rte_vhost_driver_set_features(const char *path, uint64_t features)
554 struct vhost_user_socket *vsocket;
556 pthread_mutex_lock(&vhost_user.mutex);
557 vsocket = find_vhost_user_socket(path);
559 vsocket->supported_features = features;
560 vsocket->features = features;
562 pthread_mutex_unlock(&vhost_user.mutex);
564 return vsocket ? 0 : -1;
568 rte_vhost_driver_get_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 *features = vsocket->features;
576 pthread_mutex_unlock(&vhost_user.mutex);
579 RTE_LOG(ERR, VHOST_CONFIG,
580 "socket file %s is not registered yet.\n", path);
588 * Register a new vhost-user socket; here we could act as server
589 * (the default case), or client (when RTE_VHOST_USER_CLIENT) flag
593 rte_vhost_driver_register(const char *path, uint64_t flags)
596 struct vhost_user_socket *vsocket;
601 pthread_mutex_lock(&vhost_user.mutex);
603 if (vhost_user.vsocket_cnt == MAX_VHOST_SOCKET) {
604 RTE_LOG(ERR, VHOST_CONFIG,
605 "error: the number of vhost sockets reaches maximum\n");
609 vsocket = malloc(sizeof(struct vhost_user_socket));
612 memset(vsocket, 0, sizeof(struct vhost_user_socket));
613 vsocket->path = strdup(path);
614 if (vsocket->path == NULL) {
615 RTE_LOG(ERR, VHOST_CONFIG,
616 "error: failed to copy socket path string\n");
620 TAILQ_INIT(&vsocket->conn_list);
621 ret = pthread_mutex_init(&vsocket->conn_mutex, NULL);
623 RTE_LOG(ERR, VHOST_CONFIG,
624 "error: failed to init connection mutex\n");
627 vsocket->dequeue_zero_copy = flags & RTE_VHOST_USER_DEQUEUE_ZERO_COPY;
630 * Set the supported features correctly for the builtin vhost-user
633 * Applications know nothing about features the builtin virtio net
634 * driver (virtio_net.c) supports, thus it's not possible for them
635 * to invoke rte_vhost_driver_set_features(). To workaround it, here
636 * we set it unconditionally. If the application want to implement
637 * another vhost-user driver (say SCSI), it should call the
638 * rte_vhost_driver_set_features(), which will overwrite following
641 vsocket->supported_features = VIRTIO_NET_SUPPORTED_FEATURES;
642 vsocket->features = VIRTIO_NET_SUPPORTED_FEATURES;
644 if (!(flags & RTE_VHOST_USER_IOMMU_SUPPORT)) {
645 vsocket->supported_features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
646 vsocket->features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
649 if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
650 vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
651 if (vsocket->reconnect && reconn_tid == 0) {
652 if (vhost_user_reconnect_init() < 0) {
657 vsocket->is_server = true;
659 ret = create_unix_socket(vsocket);
664 vhost_user.vsockets[vhost_user.vsocket_cnt++] = vsocket;
666 pthread_mutex_unlock(&vhost_user.mutex);
670 if (pthread_mutex_destroy(&vsocket->conn_mutex)) {
671 RTE_LOG(ERR, VHOST_CONFIG,
672 "error: failed to destroy connection mutex\n");
678 pthread_mutex_unlock(&vhost_user.mutex);
684 vhost_user_remove_reconnect(struct vhost_user_socket *vsocket)
687 struct vhost_user_reconnect *reconn, *next;
689 pthread_mutex_lock(&reconn_list.mutex);
691 for (reconn = TAILQ_FIRST(&reconn_list.head);
692 reconn != NULL; reconn = next) {
693 next = TAILQ_NEXT(reconn, next);
695 if (reconn->vsocket == vsocket) {
696 TAILQ_REMOVE(&reconn_list.head, reconn, next);
703 pthread_mutex_unlock(&reconn_list.mutex);
708 * Unregister the specified vhost socket
711 rte_vhost_driver_unregister(const char *path)
715 struct vhost_user_connection *conn, *next;
717 pthread_mutex_lock(&vhost_user.mutex);
719 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
720 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
722 if (!strcmp(vsocket->path, path)) {
723 if (vsocket->is_server) {
724 fdset_del(&vhost_user.fdset, vsocket->socket_fd);
725 close(vsocket->socket_fd);
727 } else if (vsocket->reconnect) {
728 vhost_user_remove_reconnect(vsocket);
731 pthread_mutex_lock(&vsocket->conn_mutex);
732 for (conn = TAILQ_FIRST(&vsocket->conn_list);
735 next = TAILQ_NEXT(conn, next);
737 fdset_del(&vhost_user.fdset, conn->connfd);
738 RTE_LOG(INFO, VHOST_CONFIG,
739 "free connfd = %d for device '%s'\n",
742 vhost_destroy_device(conn->vid);
743 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
746 pthread_mutex_unlock(&vsocket->conn_mutex);
748 pthread_mutex_destroy(&vsocket->conn_mutex);
752 count = --vhost_user.vsocket_cnt;
753 vhost_user.vsockets[i] = vhost_user.vsockets[count];
754 vhost_user.vsockets[count] = NULL;
755 pthread_mutex_unlock(&vhost_user.mutex);
760 pthread_mutex_unlock(&vhost_user.mutex);
766 * Register ops so that we can add/remove device to data core.
769 rte_vhost_driver_callback_register(const char *path,
770 struct vhost_device_ops const * const ops)
772 struct vhost_user_socket *vsocket;
774 pthread_mutex_lock(&vhost_user.mutex);
775 vsocket = find_vhost_user_socket(path);
777 vsocket->notify_ops = ops;
778 pthread_mutex_unlock(&vhost_user.mutex);
780 return vsocket ? 0 : -1;
783 struct vhost_device_ops const *
784 vhost_driver_callback_get(const char *path)
786 struct vhost_user_socket *vsocket;
788 pthread_mutex_lock(&vhost_user.mutex);
789 vsocket = find_vhost_user_socket(path);
790 pthread_mutex_unlock(&vhost_user.mutex);
792 return vsocket ? vsocket->notify_ops : NULL;
796 rte_vhost_driver_start(const char *path)
798 struct vhost_user_socket *vsocket;
799 static pthread_t fdset_tid;
801 pthread_mutex_lock(&vhost_user.mutex);
802 vsocket = find_vhost_user_socket(path);
803 pthread_mutex_unlock(&vhost_user.mutex);
808 if (fdset_tid == 0) {
809 int ret = pthread_create(&fdset_tid, NULL, fdset_event_dispatch,
812 RTE_LOG(ERR, VHOST_CONFIG,
813 "failed to create fdset handling thread");
816 if (vsocket->is_server)
817 return vhost_user_start_server(vsocket);
819 return vhost_user_start_client(vsocket);