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)
436 char thread_name[RTE_MAX_THREAD_NAME_LEN];
438 ret = pthread_mutex_init(&reconn_list.mutex, NULL);
440 RTE_LOG(ERR, VHOST_CONFIG, "failed to initialize mutex");
443 TAILQ_INIT(&reconn_list.head);
445 ret = pthread_create(&reconn_tid, NULL,
446 vhost_user_client_reconnect, NULL);
448 RTE_LOG(ERR, VHOST_CONFIG, "failed to create reconnect thread");
449 if (pthread_mutex_destroy(&reconn_list.mutex)) {
450 RTE_LOG(ERR, VHOST_CONFIG,
451 "failed to destroy reconnect mutex");
454 snprintf(thread_name, RTE_MAX_THREAD_NAME_LEN,
457 if (rte_thread_setname(reconn_tid, thread_name)) {
458 RTE_LOG(DEBUG, VHOST_CONFIG,
459 "failed to set reconnect thread name");
467 vhost_user_start_client(struct vhost_user_socket *vsocket)
470 int fd = vsocket->socket_fd;
471 const char *path = vsocket->path;
472 struct vhost_user_reconnect *reconn;
474 ret = vhost_user_connect_nonblock(fd, (struct sockaddr *)&vsocket->un,
475 sizeof(vsocket->un));
477 vhost_user_add_connection(fd, vsocket);
481 RTE_LOG(WARNING, VHOST_CONFIG,
482 "failed to connect to %s: %s\n",
483 path, strerror(errno));
485 if (ret == -2 || !vsocket->reconnect) {
490 RTE_LOG(INFO, VHOST_CONFIG, "%s: reconnecting...\n", path);
491 reconn = malloc(sizeof(*reconn));
492 if (reconn == NULL) {
493 RTE_LOG(ERR, VHOST_CONFIG,
494 "failed to allocate memory for reconnect\n");
498 reconn->un = vsocket->un;
500 reconn->vsocket = vsocket;
501 pthread_mutex_lock(&reconn_list.mutex);
502 TAILQ_INSERT_TAIL(&reconn_list.head, reconn, next);
503 pthread_mutex_unlock(&reconn_list.mutex);
508 static struct vhost_user_socket *
509 find_vhost_user_socket(const char *path)
513 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
514 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
516 if (!strcmp(vsocket->path, path))
524 rte_vhost_driver_disable_features(const char *path, uint64_t features)
526 struct vhost_user_socket *vsocket;
528 pthread_mutex_lock(&vhost_user.mutex);
529 vsocket = find_vhost_user_socket(path);
531 vsocket->features &= ~features;
532 pthread_mutex_unlock(&vhost_user.mutex);
534 return vsocket ? 0 : -1;
538 rte_vhost_driver_enable_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 if ((vsocket->supported_features & features) != features) {
547 * trying to enable features the driver doesn't
550 pthread_mutex_unlock(&vhost_user.mutex);
553 vsocket->features |= features;
555 pthread_mutex_unlock(&vhost_user.mutex);
557 return vsocket ? 0 : -1;
561 rte_vhost_driver_set_features(const char *path, uint64_t features)
563 struct vhost_user_socket *vsocket;
565 pthread_mutex_lock(&vhost_user.mutex);
566 vsocket = find_vhost_user_socket(path);
568 vsocket->supported_features = features;
569 vsocket->features = features;
571 pthread_mutex_unlock(&vhost_user.mutex);
573 return vsocket ? 0 : -1;
577 rte_vhost_driver_get_features(const char *path, uint64_t *features)
579 struct vhost_user_socket *vsocket;
581 pthread_mutex_lock(&vhost_user.mutex);
582 vsocket = find_vhost_user_socket(path);
584 *features = vsocket->features;
585 pthread_mutex_unlock(&vhost_user.mutex);
588 RTE_LOG(ERR, VHOST_CONFIG,
589 "socket file %s is not registered yet.\n", path);
597 * Register a new vhost-user socket; here we could act as server
598 * (the default case), or client (when RTE_VHOST_USER_CLIENT) flag
602 rte_vhost_driver_register(const char *path, uint64_t flags)
605 struct vhost_user_socket *vsocket;
610 pthread_mutex_lock(&vhost_user.mutex);
612 if (vhost_user.vsocket_cnt == MAX_VHOST_SOCKET) {
613 RTE_LOG(ERR, VHOST_CONFIG,
614 "error: the number of vhost sockets reaches maximum\n");
618 vsocket = malloc(sizeof(struct vhost_user_socket));
621 memset(vsocket, 0, sizeof(struct vhost_user_socket));
622 vsocket->path = strdup(path);
623 if (vsocket->path == NULL) {
624 RTE_LOG(ERR, VHOST_CONFIG,
625 "error: failed to copy socket path string\n");
629 TAILQ_INIT(&vsocket->conn_list);
630 ret = pthread_mutex_init(&vsocket->conn_mutex, NULL);
632 RTE_LOG(ERR, VHOST_CONFIG,
633 "error: failed to init connection mutex\n");
636 vsocket->dequeue_zero_copy = flags & RTE_VHOST_USER_DEQUEUE_ZERO_COPY;
639 * Set the supported features correctly for the builtin vhost-user
642 * Applications know nothing about features the builtin virtio net
643 * driver (virtio_net.c) supports, thus it's not possible for them
644 * to invoke rte_vhost_driver_set_features(). To workaround it, here
645 * we set it unconditionally. If the application want to implement
646 * another vhost-user driver (say SCSI), it should call the
647 * rte_vhost_driver_set_features(), which will overwrite following
650 vsocket->supported_features = VIRTIO_NET_SUPPORTED_FEATURES;
651 vsocket->features = VIRTIO_NET_SUPPORTED_FEATURES;
653 if (!(flags & RTE_VHOST_USER_IOMMU_SUPPORT)) {
654 vsocket->supported_features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
655 vsocket->features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
658 if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
659 vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
660 if (vsocket->reconnect && reconn_tid == 0) {
661 if (vhost_user_reconnect_init() != 0)
665 vsocket->is_server = true;
667 ret = create_unix_socket(vsocket);
672 vhost_user.vsockets[vhost_user.vsocket_cnt++] = vsocket;
674 pthread_mutex_unlock(&vhost_user.mutex);
678 if (pthread_mutex_destroy(&vsocket->conn_mutex)) {
679 RTE_LOG(ERR, VHOST_CONFIG,
680 "error: failed to destroy connection mutex\n");
686 pthread_mutex_unlock(&vhost_user.mutex);
692 vhost_user_remove_reconnect(struct vhost_user_socket *vsocket)
695 struct vhost_user_reconnect *reconn, *next;
697 pthread_mutex_lock(&reconn_list.mutex);
699 for (reconn = TAILQ_FIRST(&reconn_list.head);
700 reconn != NULL; reconn = next) {
701 next = TAILQ_NEXT(reconn, next);
703 if (reconn->vsocket == vsocket) {
704 TAILQ_REMOVE(&reconn_list.head, reconn, next);
711 pthread_mutex_unlock(&reconn_list.mutex);
716 * Unregister the specified vhost socket
719 rte_vhost_driver_unregister(const char *path)
723 struct vhost_user_connection *conn, *next;
725 pthread_mutex_lock(&vhost_user.mutex);
727 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
728 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
730 if (!strcmp(vsocket->path, path)) {
731 if (vsocket->is_server) {
732 fdset_del(&vhost_user.fdset, vsocket->socket_fd);
733 close(vsocket->socket_fd);
735 } else if (vsocket->reconnect) {
736 vhost_user_remove_reconnect(vsocket);
739 pthread_mutex_lock(&vsocket->conn_mutex);
740 for (conn = TAILQ_FIRST(&vsocket->conn_list);
743 next = TAILQ_NEXT(conn, next);
745 fdset_del(&vhost_user.fdset, conn->connfd);
746 RTE_LOG(INFO, VHOST_CONFIG,
747 "free connfd = %d for device '%s'\n",
750 vhost_destroy_device(conn->vid);
751 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
754 pthread_mutex_unlock(&vsocket->conn_mutex);
756 pthread_mutex_destroy(&vsocket->conn_mutex);
760 count = --vhost_user.vsocket_cnt;
761 vhost_user.vsockets[i] = vhost_user.vsockets[count];
762 vhost_user.vsockets[count] = NULL;
763 pthread_mutex_unlock(&vhost_user.mutex);
768 pthread_mutex_unlock(&vhost_user.mutex);
774 * Register ops so that we can add/remove device to data core.
777 rte_vhost_driver_callback_register(const char *path,
778 struct vhost_device_ops const * const ops)
780 struct vhost_user_socket *vsocket;
782 pthread_mutex_lock(&vhost_user.mutex);
783 vsocket = find_vhost_user_socket(path);
785 vsocket->notify_ops = ops;
786 pthread_mutex_unlock(&vhost_user.mutex);
788 return vsocket ? 0 : -1;
791 struct vhost_device_ops const *
792 vhost_driver_callback_get(const char *path)
794 struct vhost_user_socket *vsocket;
796 pthread_mutex_lock(&vhost_user.mutex);
797 vsocket = find_vhost_user_socket(path);
798 pthread_mutex_unlock(&vhost_user.mutex);
800 return vsocket ? vsocket->notify_ops : NULL;
804 rte_vhost_driver_start(const char *path)
806 struct vhost_user_socket *vsocket;
807 static pthread_t fdset_tid;
809 pthread_mutex_lock(&vhost_user.mutex);
810 vsocket = find_vhost_user_socket(path);
811 pthread_mutex_unlock(&vhost_user.mutex);
816 if (fdset_tid == 0) {
817 int ret = pthread_create(&fdset_tid, NULL, fdset_event_dispatch,
820 RTE_LOG(ERR, VHOST_CONFIG,
821 "failed to create fdset handling thread");
824 if (vsocket->is_server)
825 return vhost_user_start_server(vsocket);
827 return vhost_user_start_client(vsocket);