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);
162 cmsg->cmsg_len = CMSG_LEN(fdsize);
163 cmsg->cmsg_level = SOL_SOCKET;
164 cmsg->cmsg_type = SCM_RIGHTS;
165 memcpy(CMSG_DATA(cmsg), fds, fdsize);
167 msgh.msg_control = NULL;
168 msgh.msg_controllen = 0;
172 ret = sendmsg(sockfd, &msgh, 0);
173 } while (ret < 0 && errno == EINTR);
176 RTE_LOG(ERR, VHOST_CONFIG, "sendmsg error\n");
184 vhost_user_add_connection(int fd, struct vhost_user_socket *vsocket)
188 struct vhost_user_connection *conn;
191 conn = malloc(sizeof(*conn));
197 vid = vhost_new_device();
202 size = strnlen(vsocket->path, PATH_MAX);
203 vhost_set_ifname(vid, vsocket->path, size);
205 vhost_set_builtin_virtio_net(vid, vsocket->use_builtin_virtio_net);
207 if (vsocket->dequeue_zero_copy)
208 vhost_enable_dequeue_zero_copy(vid);
210 RTE_LOG(INFO, VHOST_CONFIG, "new device, handle is %d\n", vid);
212 if (vsocket->notify_ops->new_connection) {
213 ret = vsocket->notify_ops->new_connection(vid);
215 RTE_LOG(ERR, VHOST_CONFIG,
216 "failed to add vhost user connection with fd %d\n",
223 conn->vsocket = vsocket;
225 ret = fdset_add(&vhost_user.fdset, fd, vhost_user_read_cb,
228 RTE_LOG(ERR, VHOST_CONFIG,
229 "failed to add fd %d into vhost server fdset\n",
232 if (vsocket->notify_ops->destroy_connection)
233 vsocket->notify_ops->destroy_connection(conn->vid);
238 pthread_mutex_lock(&vsocket->conn_mutex);
239 TAILQ_INSERT_TAIL(&vsocket->conn_list, conn, next);
240 pthread_mutex_unlock(&vsocket->conn_mutex);
248 /* call back when there is new vhost-user connection from client */
250 vhost_user_server_new_connection(int fd, void *dat, int *remove __rte_unused)
252 struct vhost_user_socket *vsocket = dat;
254 fd = accept(fd, NULL, NULL);
258 RTE_LOG(INFO, VHOST_CONFIG, "new vhost user connection is %d\n", fd);
259 vhost_user_add_connection(fd, vsocket);
263 vhost_user_read_cb(int connfd, void *dat, int *remove)
265 struct vhost_user_connection *conn = dat;
266 struct vhost_user_socket *vsocket = conn->vsocket;
269 ret = vhost_user_msg_handler(conn->vid, connfd);
273 vhost_destroy_device(conn->vid);
275 if (vsocket->notify_ops->destroy_connection)
276 vsocket->notify_ops->destroy_connection(conn->vid);
278 pthread_mutex_lock(&vsocket->conn_mutex);
279 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
280 pthread_mutex_unlock(&vsocket->conn_mutex);
284 if (vsocket->reconnect) {
285 create_unix_socket(vsocket);
286 vhost_user_start_client(vsocket);
292 create_unix_socket(struct vhost_user_socket *vsocket)
295 struct sockaddr_un *un = &vsocket->un;
297 fd = socket(AF_UNIX, SOCK_STREAM, 0);
300 RTE_LOG(INFO, VHOST_CONFIG, "vhost-user %s: socket created, fd: %d\n",
301 vsocket->is_server ? "server" : "client", fd);
303 if (!vsocket->is_server && fcntl(fd, F_SETFL, O_NONBLOCK)) {
304 RTE_LOG(ERR, VHOST_CONFIG,
305 "vhost-user: can't set nonblocking mode for socket, fd: "
306 "%d (%s)\n", fd, strerror(errno));
311 memset(un, 0, sizeof(*un));
312 un->sun_family = AF_UNIX;
313 strncpy(un->sun_path, vsocket->path, sizeof(un->sun_path));
314 un->sun_path[sizeof(un->sun_path) - 1] = '\0';
316 vsocket->socket_fd = fd;
321 vhost_user_start_server(struct vhost_user_socket *vsocket)
324 int fd = vsocket->socket_fd;
325 const char *path = vsocket->path;
327 ret = bind(fd, (struct sockaddr *)&vsocket->un, sizeof(vsocket->un));
329 RTE_LOG(ERR, VHOST_CONFIG,
330 "failed to bind to %s: %s; remove it and try again\n",
331 path, strerror(errno));
334 RTE_LOG(INFO, VHOST_CONFIG, "bind to %s\n", path);
336 ret = listen(fd, MAX_VIRTIO_BACKLOG);
340 ret = fdset_add(&vhost_user.fdset, fd, vhost_user_server_new_connection,
343 RTE_LOG(ERR, VHOST_CONFIG,
344 "failed to add listen fd %d to vhost server fdset\n",
356 struct vhost_user_reconnect {
357 struct sockaddr_un un;
359 struct vhost_user_socket *vsocket;
361 TAILQ_ENTRY(vhost_user_reconnect) next;
364 TAILQ_HEAD(vhost_user_reconnect_tailq_list, vhost_user_reconnect);
365 struct vhost_user_reconnect_list {
366 struct vhost_user_reconnect_tailq_list head;
367 pthread_mutex_t mutex;
370 static struct vhost_user_reconnect_list reconn_list;
371 static pthread_t reconn_tid;
374 vhost_user_connect_nonblock(int fd, struct sockaddr *un, size_t sz)
378 ret = connect(fd, un, sz);
379 if (ret < 0 && errno != EISCONN)
382 flags = fcntl(fd, F_GETFL, 0);
384 RTE_LOG(ERR, VHOST_CONFIG,
385 "can't get flags for connfd %d\n", fd);
388 if ((flags & O_NONBLOCK) && fcntl(fd, F_SETFL, flags & ~O_NONBLOCK)) {
389 RTE_LOG(ERR, VHOST_CONFIG,
390 "can't disable nonblocking on fd %d\n", fd);
397 vhost_user_client_reconnect(void *arg __rte_unused)
400 struct vhost_user_reconnect *reconn, *next;
403 pthread_mutex_lock(&reconn_list.mutex);
406 * An equal implementation of TAILQ_FOREACH_SAFE,
407 * which does not exist on all platforms.
409 for (reconn = TAILQ_FIRST(&reconn_list.head);
410 reconn != NULL; reconn = next) {
411 next = TAILQ_NEXT(reconn, next);
413 ret = vhost_user_connect_nonblock(reconn->fd,
414 (struct sockaddr *)&reconn->un,
418 RTE_LOG(ERR, VHOST_CONFIG,
419 "reconnection for fd %d failed\n",
426 RTE_LOG(INFO, VHOST_CONFIG,
427 "%s: connected\n", reconn->vsocket->path);
428 vhost_user_add_connection(reconn->fd, reconn->vsocket);
430 TAILQ_REMOVE(&reconn_list.head, reconn, next);
434 pthread_mutex_unlock(&reconn_list.mutex);
442 vhost_user_reconnect_init(void)
445 char thread_name[RTE_MAX_THREAD_NAME_LEN];
447 ret = pthread_mutex_init(&reconn_list.mutex, NULL);
449 RTE_LOG(ERR, VHOST_CONFIG, "failed to initialize mutex");
452 TAILQ_INIT(&reconn_list.head);
454 ret = pthread_create(&reconn_tid, NULL,
455 vhost_user_client_reconnect, NULL);
457 RTE_LOG(ERR, VHOST_CONFIG, "failed to create reconnect thread");
458 if (pthread_mutex_destroy(&reconn_list.mutex)) {
459 RTE_LOG(ERR, VHOST_CONFIG,
460 "failed to destroy reconnect mutex");
463 snprintf(thread_name, RTE_MAX_THREAD_NAME_LEN,
466 if (rte_thread_setname(reconn_tid, thread_name)) {
467 RTE_LOG(DEBUG, VHOST_CONFIG,
468 "failed to set reconnect thread name");
476 vhost_user_start_client(struct vhost_user_socket *vsocket)
479 int fd = vsocket->socket_fd;
480 const char *path = vsocket->path;
481 struct vhost_user_reconnect *reconn;
483 ret = vhost_user_connect_nonblock(fd, (struct sockaddr *)&vsocket->un,
484 sizeof(vsocket->un));
486 vhost_user_add_connection(fd, vsocket);
490 RTE_LOG(WARNING, VHOST_CONFIG,
491 "failed to connect to %s: %s\n",
492 path, strerror(errno));
494 if (ret == -2 || !vsocket->reconnect) {
499 RTE_LOG(INFO, VHOST_CONFIG, "%s: reconnecting...\n", path);
500 reconn = malloc(sizeof(*reconn));
501 if (reconn == NULL) {
502 RTE_LOG(ERR, VHOST_CONFIG,
503 "failed to allocate memory for reconnect\n");
507 reconn->un = vsocket->un;
509 reconn->vsocket = vsocket;
510 pthread_mutex_lock(&reconn_list.mutex);
511 TAILQ_INSERT_TAIL(&reconn_list.head, reconn, next);
512 pthread_mutex_unlock(&reconn_list.mutex);
517 static struct vhost_user_socket *
518 find_vhost_user_socket(const char *path)
522 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
523 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
525 if (!strcmp(vsocket->path, path))
533 rte_vhost_driver_disable_features(const char *path, uint64_t features)
535 struct vhost_user_socket *vsocket;
537 pthread_mutex_lock(&vhost_user.mutex);
538 vsocket = find_vhost_user_socket(path);
540 /* Note that use_builtin_virtio_net is not affected by this function
541 * since callers may want to selectively disable features of the
542 * built-in vhost net device backend.
546 vsocket->features &= ~features;
547 pthread_mutex_unlock(&vhost_user.mutex);
549 return vsocket ? 0 : -1;
553 rte_vhost_driver_enable_features(const char *path, uint64_t features)
555 struct vhost_user_socket *vsocket;
557 pthread_mutex_lock(&vhost_user.mutex);
558 vsocket = find_vhost_user_socket(path);
560 if ((vsocket->supported_features & features) != features) {
562 * trying to enable features the driver doesn't
565 pthread_mutex_unlock(&vhost_user.mutex);
568 vsocket->features |= features;
570 pthread_mutex_unlock(&vhost_user.mutex);
572 return vsocket ? 0 : -1;
576 rte_vhost_driver_set_features(const char *path, uint64_t features)
578 struct vhost_user_socket *vsocket;
580 pthread_mutex_lock(&vhost_user.mutex);
581 vsocket = find_vhost_user_socket(path);
583 vsocket->supported_features = features;
584 vsocket->features = features;
586 /* Anyone setting feature bits is implementing their own vhost
589 vsocket->use_builtin_virtio_net = false;
591 pthread_mutex_unlock(&vhost_user.mutex);
593 return vsocket ? 0 : -1;
597 rte_vhost_driver_get_features(const char *path, uint64_t *features)
599 struct vhost_user_socket *vsocket;
601 pthread_mutex_lock(&vhost_user.mutex);
602 vsocket = find_vhost_user_socket(path);
604 *features = vsocket->features;
605 pthread_mutex_unlock(&vhost_user.mutex);
608 RTE_LOG(ERR, VHOST_CONFIG,
609 "socket file %s is not registered yet.\n", path);
617 * Register a new vhost-user socket; here we could act as server
618 * (the default case), or client (when RTE_VHOST_USER_CLIENT) flag
622 rte_vhost_driver_register(const char *path, uint64_t flags)
625 struct vhost_user_socket *vsocket;
630 pthread_mutex_lock(&vhost_user.mutex);
632 if (vhost_user.vsocket_cnt == MAX_VHOST_SOCKET) {
633 RTE_LOG(ERR, VHOST_CONFIG,
634 "error: the number of vhost sockets reaches maximum\n");
638 vsocket = malloc(sizeof(struct vhost_user_socket));
641 memset(vsocket, 0, sizeof(struct vhost_user_socket));
642 vsocket->path = strdup(path);
643 if (vsocket->path == NULL) {
644 RTE_LOG(ERR, VHOST_CONFIG,
645 "error: failed to copy socket path string\n");
649 TAILQ_INIT(&vsocket->conn_list);
650 ret = pthread_mutex_init(&vsocket->conn_mutex, NULL);
652 RTE_LOG(ERR, VHOST_CONFIG,
653 "error: failed to init connection mutex\n");
656 vsocket->dequeue_zero_copy = flags & RTE_VHOST_USER_DEQUEUE_ZERO_COPY;
659 * Set the supported features correctly for the builtin vhost-user
662 * Applications know nothing about features the builtin virtio net
663 * driver (virtio_net.c) supports, thus it's not possible for them
664 * to invoke rte_vhost_driver_set_features(). To workaround it, here
665 * we set it unconditionally. If the application want to implement
666 * another vhost-user driver (say SCSI), it should call the
667 * rte_vhost_driver_set_features(), which will overwrite following
670 vsocket->use_builtin_virtio_net = true;
671 vsocket->supported_features = VIRTIO_NET_SUPPORTED_FEATURES;
672 vsocket->features = VIRTIO_NET_SUPPORTED_FEATURES;
674 if (!(flags & RTE_VHOST_USER_IOMMU_SUPPORT)) {
675 vsocket->supported_features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
676 vsocket->features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
679 if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
680 vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
681 if (vsocket->reconnect && reconn_tid == 0) {
682 if (vhost_user_reconnect_init() != 0)
686 vsocket->is_server = true;
688 ret = create_unix_socket(vsocket);
693 vhost_user.vsockets[vhost_user.vsocket_cnt++] = vsocket;
695 pthread_mutex_unlock(&vhost_user.mutex);
699 if (pthread_mutex_destroy(&vsocket->conn_mutex)) {
700 RTE_LOG(ERR, VHOST_CONFIG,
701 "error: failed to destroy connection mutex\n");
707 pthread_mutex_unlock(&vhost_user.mutex);
713 vhost_user_remove_reconnect(struct vhost_user_socket *vsocket)
716 struct vhost_user_reconnect *reconn, *next;
718 pthread_mutex_lock(&reconn_list.mutex);
720 for (reconn = TAILQ_FIRST(&reconn_list.head);
721 reconn != NULL; reconn = next) {
722 next = TAILQ_NEXT(reconn, next);
724 if (reconn->vsocket == vsocket) {
725 TAILQ_REMOVE(&reconn_list.head, reconn, next);
732 pthread_mutex_unlock(&reconn_list.mutex);
737 * Unregister the specified vhost socket
740 rte_vhost_driver_unregister(const char *path)
744 struct vhost_user_connection *conn, *next;
746 pthread_mutex_lock(&vhost_user.mutex);
748 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
749 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
751 if (!strcmp(vsocket->path, path)) {
752 if (vsocket->is_server) {
753 fdset_del(&vhost_user.fdset, vsocket->socket_fd);
754 close(vsocket->socket_fd);
756 } else if (vsocket->reconnect) {
757 vhost_user_remove_reconnect(vsocket);
760 pthread_mutex_lock(&vsocket->conn_mutex);
761 for (conn = TAILQ_FIRST(&vsocket->conn_list);
764 next = TAILQ_NEXT(conn, next);
766 fdset_del(&vhost_user.fdset, conn->connfd);
767 RTE_LOG(INFO, VHOST_CONFIG,
768 "free connfd = %d for device '%s'\n",
771 vhost_destroy_device(conn->vid);
772 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
775 pthread_mutex_unlock(&vsocket->conn_mutex);
777 pthread_mutex_destroy(&vsocket->conn_mutex);
781 count = --vhost_user.vsocket_cnt;
782 vhost_user.vsockets[i] = vhost_user.vsockets[count];
783 vhost_user.vsockets[count] = NULL;
784 pthread_mutex_unlock(&vhost_user.mutex);
789 pthread_mutex_unlock(&vhost_user.mutex);
795 * Register ops so that we can add/remove device to data core.
798 rte_vhost_driver_callback_register(const char *path,
799 struct vhost_device_ops const * const ops)
801 struct vhost_user_socket *vsocket;
803 pthread_mutex_lock(&vhost_user.mutex);
804 vsocket = find_vhost_user_socket(path);
806 vsocket->notify_ops = ops;
807 pthread_mutex_unlock(&vhost_user.mutex);
809 return vsocket ? 0 : -1;
812 struct vhost_device_ops const *
813 vhost_driver_callback_get(const char *path)
815 struct vhost_user_socket *vsocket;
817 pthread_mutex_lock(&vhost_user.mutex);
818 vsocket = find_vhost_user_socket(path);
819 pthread_mutex_unlock(&vhost_user.mutex);
821 return vsocket ? vsocket->notify_ops : NULL;
825 rte_vhost_driver_start(const char *path)
827 struct vhost_user_socket *vsocket;
828 static pthread_t fdset_tid;
830 pthread_mutex_lock(&vhost_user.mutex);
831 vsocket = find_vhost_user_socket(path);
832 pthread_mutex_unlock(&vhost_user.mutex);
837 if (fdset_tid == 0) {
838 int ret = pthread_create(&fdset_tid, NULL, fdset_event_dispatch,
841 RTE_LOG(ERR, VHOST_CONFIG,
842 "failed to create fdset handling thread");
845 if (vsocket->is_server)
846 return vhost_user_start_server(vsocket);
848 return vhost_user_start_client(vsocket);