4 * Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 #include <sys/types.h>
42 #include <sys/socket.h>
44 #include <sys/queue.h>
53 #include "vhost_user.h"
56 TAILQ_HEAD(vhost_user_connection_list, vhost_user_connection);
59 * Every time rte_vhost_driver_register() is invoked, an associated
60 * vhost_user_socket struct will be created.
62 struct vhost_user_socket {
63 struct vhost_user_connection_list conn_list;
64 pthread_mutex_t conn_mutex;
67 struct sockaddr_un un;
70 bool dequeue_zero_copy;
74 * The "supported_features" indicates the feature bits the
75 * vhost driver supports. The "features" indicates the feature
76 * bits after the rte_vhost_driver_features_disable/enable().
77 * It is also the final feature bits used for vhost-user
78 * features negotiation.
80 uint64_t supported_features;
83 struct vhost_device_ops const *notify_ops;
86 struct vhost_user_connection {
87 struct vhost_user_socket *vsocket;
91 TAILQ_ENTRY(vhost_user_connection) next;
94 #define MAX_VHOST_SOCKET 1024
96 struct vhost_user_socket *vsockets[MAX_VHOST_SOCKET];
99 pthread_mutex_t mutex;
102 #define MAX_VIRTIO_BACKLOG 128
104 static void vhost_user_server_new_connection(int fd, void *data, int *remove);
105 static void vhost_user_read_cb(int fd, void *dat, int *remove);
106 static int create_unix_socket(struct vhost_user_socket *vsocket);
107 static int vhost_user_start_client(struct vhost_user_socket *vsocket);
109 static struct vhost_user vhost_user = {
111 .fd = { [0 ... MAX_FDS - 1] = {-1, NULL, NULL, NULL, 0} },
112 .fd_mutex = PTHREAD_MUTEX_INITIALIZER,
116 .mutex = PTHREAD_MUTEX_INITIALIZER,
119 /* return bytes# of read on success or negative val on failure. */
121 read_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
125 size_t fdsize = fd_num * sizeof(int);
126 char control[CMSG_SPACE(fdsize)];
127 struct cmsghdr *cmsg;
130 memset(&msgh, 0, sizeof(msgh));
132 iov.iov_len = buflen;
136 msgh.msg_control = control;
137 msgh.msg_controllen = sizeof(control);
139 ret = recvmsg(sockfd, &msgh, 0);
141 RTE_LOG(ERR, VHOST_CONFIG, "recvmsg failed\n");
145 if (msgh.msg_flags & (MSG_TRUNC | MSG_CTRUNC)) {
146 RTE_LOG(ERR, VHOST_CONFIG, "truncted msg\n");
150 for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
151 cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
152 if ((cmsg->cmsg_level == SOL_SOCKET) &&
153 (cmsg->cmsg_type == SCM_RIGHTS)) {
154 memcpy(fds, CMSG_DATA(cmsg), fdsize);
163 send_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
168 size_t fdsize = fd_num * sizeof(int);
169 char control[CMSG_SPACE(fdsize)];
170 struct cmsghdr *cmsg;
173 memset(&msgh, 0, sizeof(msgh));
175 iov.iov_len = buflen;
180 if (fds && fd_num > 0) {
181 msgh.msg_control = control;
182 msgh.msg_controllen = sizeof(control);
183 cmsg = CMSG_FIRSTHDR(&msgh);
185 RTE_LOG(ERR, VHOST_CONFIG, "cmsg == NULL\n");
189 cmsg->cmsg_len = CMSG_LEN(fdsize);
190 cmsg->cmsg_level = SOL_SOCKET;
191 cmsg->cmsg_type = SCM_RIGHTS;
192 memcpy(CMSG_DATA(cmsg), fds, fdsize);
194 msgh.msg_control = NULL;
195 msgh.msg_controllen = 0;
199 ret = sendmsg(sockfd, &msgh, 0);
200 } while (ret < 0 && errno == EINTR);
203 RTE_LOG(ERR, VHOST_CONFIG, "sendmsg error\n");
211 vhost_user_add_connection(int fd, struct vhost_user_socket *vsocket)
215 struct vhost_user_connection *conn;
218 conn = malloc(sizeof(*conn));
224 vid = vhost_new_device();
229 size = strnlen(vsocket->path, PATH_MAX);
230 vhost_set_ifname(vid, vsocket->path, size);
232 if (vsocket->dequeue_zero_copy)
233 vhost_enable_dequeue_zero_copy(vid);
235 RTE_LOG(INFO, VHOST_CONFIG, "new device, handle is %d\n", vid);
237 if (vsocket->notify_ops->new_connection) {
238 ret = vsocket->notify_ops->new_connection(vid);
240 RTE_LOG(ERR, VHOST_CONFIG,
241 "failed to add vhost user connection with fd %d\n",
248 conn->vsocket = vsocket;
250 ret = fdset_add(&vhost_user.fdset, fd, vhost_user_read_cb,
253 RTE_LOG(ERR, VHOST_CONFIG,
254 "failed to add fd %d into vhost server fdset\n",
257 if (vsocket->notify_ops->destroy_connection)
258 vsocket->notify_ops->destroy_connection(conn->vid);
263 pthread_mutex_lock(&vsocket->conn_mutex);
264 TAILQ_INSERT_TAIL(&vsocket->conn_list, conn, next);
265 pthread_mutex_unlock(&vsocket->conn_mutex);
273 /* call back when there is new vhost-user connection from client */
275 vhost_user_server_new_connection(int fd, void *dat, int *remove __rte_unused)
277 struct vhost_user_socket *vsocket = dat;
279 fd = accept(fd, NULL, NULL);
283 RTE_LOG(INFO, VHOST_CONFIG, "new vhost user connection is %d\n", fd);
284 vhost_user_add_connection(fd, vsocket);
288 vhost_user_read_cb(int connfd, void *dat, int *remove)
290 struct vhost_user_connection *conn = dat;
291 struct vhost_user_socket *vsocket = conn->vsocket;
294 ret = vhost_user_msg_handler(conn->vid, connfd);
298 vhost_destroy_device(conn->vid);
300 if (vsocket->notify_ops->destroy_connection)
301 vsocket->notify_ops->destroy_connection(conn->vid);
303 pthread_mutex_lock(&vsocket->conn_mutex);
304 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
305 pthread_mutex_unlock(&vsocket->conn_mutex);
309 if (vsocket->reconnect) {
310 create_unix_socket(vsocket);
311 vhost_user_start_client(vsocket);
317 create_unix_socket(struct vhost_user_socket *vsocket)
320 struct sockaddr_un *un = &vsocket->un;
322 fd = socket(AF_UNIX, SOCK_STREAM, 0);
325 RTE_LOG(INFO, VHOST_CONFIG, "vhost-user %s: socket created, fd: %d\n",
326 vsocket->is_server ? "server" : "client", fd);
328 if (!vsocket->is_server && fcntl(fd, F_SETFL, O_NONBLOCK)) {
329 RTE_LOG(ERR, VHOST_CONFIG,
330 "vhost-user: can't set nonblocking mode for socket, fd: "
331 "%d (%s)\n", fd, strerror(errno));
336 memset(un, 0, sizeof(*un));
337 un->sun_family = AF_UNIX;
338 strncpy(un->sun_path, vsocket->path, sizeof(un->sun_path));
339 un->sun_path[sizeof(un->sun_path) - 1] = '\0';
341 vsocket->socket_fd = fd;
346 vhost_user_start_server(struct vhost_user_socket *vsocket)
349 int fd = vsocket->socket_fd;
350 const char *path = vsocket->path;
352 ret = bind(fd, (struct sockaddr *)&vsocket->un, sizeof(vsocket->un));
354 RTE_LOG(ERR, VHOST_CONFIG,
355 "failed to bind to %s: %s; remove it and try again\n",
356 path, strerror(errno));
359 RTE_LOG(INFO, VHOST_CONFIG, "bind to %s\n", path);
361 ret = listen(fd, MAX_VIRTIO_BACKLOG);
365 ret = fdset_add(&vhost_user.fdset, fd, vhost_user_server_new_connection,
368 RTE_LOG(ERR, VHOST_CONFIG,
369 "failed to add listen fd %d to vhost server fdset\n",
381 struct vhost_user_reconnect {
382 struct sockaddr_un un;
384 struct vhost_user_socket *vsocket;
386 TAILQ_ENTRY(vhost_user_reconnect) next;
389 TAILQ_HEAD(vhost_user_reconnect_tailq_list, vhost_user_reconnect);
390 struct vhost_user_reconnect_list {
391 struct vhost_user_reconnect_tailq_list head;
392 pthread_mutex_t mutex;
395 static struct vhost_user_reconnect_list reconn_list;
396 static pthread_t reconn_tid;
399 vhost_user_connect_nonblock(int fd, struct sockaddr *un, size_t sz)
403 ret = connect(fd, un, sz);
404 if (ret < 0 && errno != EISCONN)
407 flags = fcntl(fd, F_GETFL, 0);
409 RTE_LOG(ERR, VHOST_CONFIG,
410 "can't get flags for connfd %d\n", fd);
413 if ((flags & O_NONBLOCK) && fcntl(fd, F_SETFL, flags & ~O_NONBLOCK)) {
414 RTE_LOG(ERR, VHOST_CONFIG,
415 "can't disable nonblocking on fd %d\n", fd);
422 vhost_user_client_reconnect(void *arg __rte_unused)
425 struct vhost_user_reconnect *reconn, *next;
428 pthread_mutex_lock(&reconn_list.mutex);
431 * An equal implementation of TAILQ_FOREACH_SAFE,
432 * which does not exist on all platforms.
434 for (reconn = TAILQ_FIRST(&reconn_list.head);
435 reconn != NULL; reconn = next) {
436 next = TAILQ_NEXT(reconn, next);
438 ret = vhost_user_connect_nonblock(reconn->fd,
439 (struct sockaddr *)&reconn->un,
443 RTE_LOG(ERR, VHOST_CONFIG,
444 "reconnection for fd %d failed\n",
451 RTE_LOG(INFO, VHOST_CONFIG,
452 "%s: connected\n", reconn->vsocket->path);
453 vhost_user_add_connection(reconn->fd, reconn->vsocket);
455 TAILQ_REMOVE(&reconn_list.head, reconn, next);
459 pthread_mutex_unlock(&reconn_list.mutex);
467 vhost_user_reconnect_init(void)
471 ret = pthread_mutex_init(&reconn_list.mutex, NULL);
473 RTE_LOG(ERR, VHOST_CONFIG, "failed to initialize mutex");
476 TAILQ_INIT(&reconn_list.head);
478 ret = pthread_create(&reconn_tid, NULL,
479 vhost_user_client_reconnect, NULL);
481 RTE_LOG(ERR, VHOST_CONFIG, "failed to create reconnect thread");
482 if (pthread_mutex_destroy(&reconn_list.mutex)) {
483 RTE_LOG(ERR, VHOST_CONFIG,
484 "failed to destroy reconnect mutex");
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 vsocket->features &= ~features;
557 pthread_mutex_unlock(&vhost_user.mutex);
559 return vsocket ? 0 : -1;
563 rte_vhost_driver_enable_features(const char *path, uint64_t features)
565 struct vhost_user_socket *vsocket;
567 pthread_mutex_lock(&vhost_user.mutex);
568 vsocket = find_vhost_user_socket(path);
570 if ((vsocket->supported_features & features) != features) {
572 * trying to enable features the driver doesn't
575 pthread_mutex_unlock(&vhost_user.mutex);
578 vsocket->features |= features;
580 pthread_mutex_unlock(&vhost_user.mutex);
582 return vsocket ? 0 : -1;
586 rte_vhost_driver_set_features(const char *path, uint64_t features)
588 struct vhost_user_socket *vsocket;
590 pthread_mutex_lock(&vhost_user.mutex);
591 vsocket = find_vhost_user_socket(path);
593 vsocket->supported_features = features;
594 vsocket->features = features;
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->supported_features = VIRTIO_NET_SUPPORTED_FEATURES;
676 vsocket->features = VIRTIO_NET_SUPPORTED_FEATURES;
678 if (!(flags & RTE_VHOST_USER_IOMMU_SUPPORT)) {
679 vsocket->supported_features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
680 vsocket->features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
683 if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
684 vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
685 if (vsocket->reconnect && reconn_tid == 0) {
686 if (vhost_user_reconnect_init() != 0)
690 vsocket->is_server = true;
692 ret = create_unix_socket(vsocket);
697 vhost_user.vsockets[vhost_user.vsocket_cnt++] = vsocket;
699 pthread_mutex_unlock(&vhost_user.mutex);
703 if (pthread_mutex_destroy(&vsocket->conn_mutex)) {
704 RTE_LOG(ERR, VHOST_CONFIG,
705 "error: failed to destroy connection mutex\n");
711 pthread_mutex_unlock(&vhost_user.mutex);
717 vhost_user_remove_reconnect(struct vhost_user_socket *vsocket)
720 struct vhost_user_reconnect *reconn, *next;
722 pthread_mutex_lock(&reconn_list.mutex);
724 for (reconn = TAILQ_FIRST(&reconn_list.head);
725 reconn != NULL; reconn = next) {
726 next = TAILQ_NEXT(reconn, next);
728 if (reconn->vsocket == vsocket) {
729 TAILQ_REMOVE(&reconn_list.head, reconn, next);
736 pthread_mutex_unlock(&reconn_list.mutex);
741 * Unregister the specified vhost socket
744 rte_vhost_driver_unregister(const char *path)
748 struct vhost_user_connection *conn, *next;
750 pthread_mutex_lock(&vhost_user.mutex);
752 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
753 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
755 if (!strcmp(vsocket->path, path)) {
756 if (vsocket->is_server) {
757 fdset_del(&vhost_user.fdset, vsocket->socket_fd);
758 close(vsocket->socket_fd);
760 } else if (vsocket->reconnect) {
761 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);
772 * If r/wcb is executing, release the
773 * conn_mutex lock, and try again since
774 * the r/wcb may use the conn_mutex lock.
776 if (fdset_try_del(&vhost_user.fdset,
777 conn->connfd) == -1) {
778 pthread_mutex_unlock(
779 &vsocket->conn_mutex);
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;
846 pthread_mutex_lock(&vhost_user.mutex);
847 vsocket = find_vhost_user_socket(path);
848 pthread_mutex_unlock(&vhost_user.mutex);
853 if (fdset_tid == 0) {
854 int ret = pthread_create(&fdset_tid, NULL, fdset_event_dispatch,
857 RTE_LOG(ERR, VHOST_CONFIG,
858 "failed to create fdset handling thread");
861 if (vsocket->is_server)
862 return vhost_user_start_server(vsocket);
864 return vhost_user_start_client(vsocket);