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);
184 cmsg->cmsg_len = CMSG_LEN(fdsize);
185 cmsg->cmsg_level = SOL_SOCKET;
186 cmsg->cmsg_type = SCM_RIGHTS;
187 memcpy(CMSG_DATA(cmsg), fds, fdsize);
189 msgh.msg_control = NULL;
190 msgh.msg_controllen = 0;
194 ret = sendmsg(sockfd, &msgh, 0);
195 } while (ret < 0 && errno == EINTR);
198 RTE_LOG(ERR, VHOST_CONFIG, "sendmsg error\n");
206 vhost_user_add_connection(int fd, struct vhost_user_socket *vsocket)
210 struct vhost_user_connection *conn;
213 conn = malloc(sizeof(*conn));
219 vid = vhost_new_device();
224 size = strnlen(vsocket->path, PATH_MAX);
225 vhost_set_ifname(vid, vsocket->path, size);
227 if (vsocket->dequeue_zero_copy)
228 vhost_enable_dequeue_zero_copy(vid);
230 RTE_LOG(INFO, VHOST_CONFIG, "new device, handle is %d\n", vid);
232 if (vsocket->notify_ops->new_connection) {
233 ret = vsocket->notify_ops->new_connection(vid);
235 RTE_LOG(ERR, VHOST_CONFIG,
236 "failed to add vhost user connection with fd %d\n",
243 conn->vsocket = vsocket;
245 ret = fdset_add(&vhost_user.fdset, fd, vhost_user_read_cb,
248 RTE_LOG(ERR, VHOST_CONFIG,
249 "failed to add fd %d into vhost server fdset\n",
252 if (vsocket->notify_ops->destroy_connection)
253 vsocket->notify_ops->destroy_connection(conn->vid);
258 pthread_mutex_lock(&vsocket->conn_mutex);
259 TAILQ_INSERT_TAIL(&vsocket->conn_list, conn, next);
260 pthread_mutex_unlock(&vsocket->conn_mutex);
268 /* call back when there is new vhost-user connection from client */
270 vhost_user_server_new_connection(int fd, void *dat, int *remove __rte_unused)
272 struct vhost_user_socket *vsocket = dat;
274 fd = accept(fd, NULL, NULL);
278 RTE_LOG(INFO, VHOST_CONFIG, "new vhost user connection is %d\n", fd);
279 vhost_user_add_connection(fd, vsocket);
283 vhost_user_read_cb(int connfd, void *dat, int *remove)
285 struct vhost_user_connection *conn = dat;
286 struct vhost_user_socket *vsocket = conn->vsocket;
289 ret = vhost_user_msg_handler(conn->vid, connfd);
293 vhost_destroy_device(conn->vid);
295 if (vsocket->notify_ops->destroy_connection)
296 vsocket->notify_ops->destroy_connection(conn->vid);
298 pthread_mutex_lock(&vsocket->conn_mutex);
299 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
300 pthread_mutex_unlock(&vsocket->conn_mutex);
304 if (vsocket->reconnect) {
305 create_unix_socket(vsocket);
306 vhost_user_start_client(vsocket);
312 create_unix_socket(struct vhost_user_socket *vsocket)
315 struct sockaddr_un *un = &vsocket->un;
317 fd = socket(AF_UNIX, SOCK_STREAM, 0);
320 RTE_LOG(INFO, VHOST_CONFIG, "vhost-user %s: socket created, fd: %d\n",
321 vsocket->is_server ? "server" : "client", fd);
323 if (!vsocket->is_server && fcntl(fd, F_SETFL, O_NONBLOCK)) {
324 RTE_LOG(ERR, VHOST_CONFIG,
325 "vhost-user: can't set nonblocking mode for socket, fd: "
326 "%d (%s)\n", fd, strerror(errno));
331 memset(un, 0, sizeof(*un));
332 un->sun_family = AF_UNIX;
333 strncpy(un->sun_path, vsocket->path, sizeof(un->sun_path));
334 un->sun_path[sizeof(un->sun_path) - 1] = '\0';
336 vsocket->socket_fd = fd;
341 vhost_user_start_server(struct vhost_user_socket *vsocket)
344 int fd = vsocket->socket_fd;
345 const char *path = vsocket->path;
347 ret = bind(fd, (struct sockaddr *)&vsocket->un, sizeof(vsocket->un));
349 RTE_LOG(ERR, VHOST_CONFIG,
350 "failed to bind to %s: %s; remove it and try again\n",
351 path, strerror(errno));
354 RTE_LOG(INFO, VHOST_CONFIG, "bind to %s\n", path);
356 ret = listen(fd, MAX_VIRTIO_BACKLOG);
360 ret = fdset_add(&vhost_user.fdset, fd, vhost_user_server_new_connection,
363 RTE_LOG(ERR, VHOST_CONFIG,
364 "failed to add listen fd %d to vhost server fdset\n",
376 struct vhost_user_reconnect {
377 struct sockaddr_un un;
379 struct vhost_user_socket *vsocket;
381 TAILQ_ENTRY(vhost_user_reconnect) next;
384 TAILQ_HEAD(vhost_user_reconnect_tailq_list, vhost_user_reconnect);
385 struct vhost_user_reconnect_list {
386 struct vhost_user_reconnect_tailq_list head;
387 pthread_mutex_t mutex;
390 static struct vhost_user_reconnect_list reconn_list;
391 static pthread_t reconn_tid;
394 vhost_user_connect_nonblock(int fd, struct sockaddr *un, size_t sz)
398 ret = connect(fd, un, sz);
399 if (ret < 0 && errno != EISCONN)
402 flags = fcntl(fd, F_GETFL, 0);
404 RTE_LOG(ERR, VHOST_CONFIG,
405 "can't get flags for connfd %d\n", fd);
408 if ((flags & O_NONBLOCK) && fcntl(fd, F_SETFL, flags & ~O_NONBLOCK)) {
409 RTE_LOG(ERR, VHOST_CONFIG,
410 "can't disable nonblocking on fd %d\n", fd);
417 vhost_user_client_reconnect(void *arg __rte_unused)
420 struct vhost_user_reconnect *reconn, *next;
423 pthread_mutex_lock(&reconn_list.mutex);
426 * An equal implementation of TAILQ_FOREACH_SAFE,
427 * which does not exist on all platforms.
429 for (reconn = TAILQ_FIRST(&reconn_list.head);
430 reconn != NULL; reconn = next) {
431 next = TAILQ_NEXT(reconn, next);
433 ret = vhost_user_connect_nonblock(reconn->fd,
434 (struct sockaddr *)&reconn->un,
438 RTE_LOG(ERR, VHOST_CONFIG,
439 "reconnection for fd %d failed\n",
446 RTE_LOG(INFO, VHOST_CONFIG,
447 "%s: connected\n", reconn->vsocket->path);
448 vhost_user_add_connection(reconn->fd, reconn->vsocket);
450 TAILQ_REMOVE(&reconn_list.head, reconn, next);
454 pthread_mutex_unlock(&reconn_list.mutex);
462 vhost_user_reconnect_init(void)
466 ret = pthread_mutex_init(&reconn_list.mutex, NULL);
468 RTE_LOG(ERR, VHOST_CONFIG, "failed to initialize mutex");
471 TAILQ_INIT(&reconn_list.head);
473 ret = pthread_create(&reconn_tid, NULL,
474 vhost_user_client_reconnect, NULL);
476 RTE_LOG(ERR, VHOST_CONFIG, "failed to create reconnect thread");
477 if (pthread_mutex_destroy(&reconn_list.mutex)) {
478 RTE_LOG(ERR, VHOST_CONFIG,
479 "failed to destroy reconnect mutex");
487 vhost_user_start_client(struct vhost_user_socket *vsocket)
490 int fd = vsocket->socket_fd;
491 const char *path = vsocket->path;
492 struct vhost_user_reconnect *reconn;
494 ret = vhost_user_connect_nonblock(fd, (struct sockaddr *)&vsocket->un,
495 sizeof(vsocket->un));
497 vhost_user_add_connection(fd, vsocket);
501 RTE_LOG(WARNING, VHOST_CONFIG,
502 "failed to connect to %s: %s\n",
503 path, strerror(errno));
505 if (ret == -2 || !vsocket->reconnect) {
510 RTE_LOG(INFO, VHOST_CONFIG, "%s: reconnecting...\n", path);
511 reconn = malloc(sizeof(*reconn));
512 if (reconn == NULL) {
513 RTE_LOG(ERR, VHOST_CONFIG,
514 "failed to allocate memory for reconnect\n");
518 reconn->un = vsocket->un;
520 reconn->vsocket = vsocket;
521 pthread_mutex_lock(&reconn_list.mutex);
522 TAILQ_INSERT_TAIL(&reconn_list.head, reconn, next);
523 pthread_mutex_unlock(&reconn_list.mutex);
528 static struct vhost_user_socket *
529 find_vhost_user_socket(const char *path)
533 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
534 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
536 if (!strcmp(vsocket->path, path))
544 rte_vhost_driver_disable_features(const char *path, uint64_t features)
546 struct vhost_user_socket *vsocket;
548 pthread_mutex_lock(&vhost_user.mutex);
549 vsocket = find_vhost_user_socket(path);
551 vsocket->features &= ~features;
552 pthread_mutex_unlock(&vhost_user.mutex);
554 return vsocket ? 0 : -1;
558 rte_vhost_driver_enable_features(const char *path, uint64_t features)
560 struct vhost_user_socket *vsocket;
562 pthread_mutex_lock(&vhost_user.mutex);
563 vsocket = find_vhost_user_socket(path);
565 if ((vsocket->supported_features & features) != features) {
567 * trying to enable features the driver doesn't
570 pthread_mutex_unlock(&vhost_user.mutex);
573 vsocket->features |= features;
575 pthread_mutex_unlock(&vhost_user.mutex);
577 return vsocket ? 0 : -1;
581 rte_vhost_driver_set_features(const char *path, uint64_t features)
583 struct vhost_user_socket *vsocket;
585 pthread_mutex_lock(&vhost_user.mutex);
586 vsocket = find_vhost_user_socket(path);
588 vsocket->supported_features = features;
589 vsocket->features = features;
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->supported_features = VIRTIO_NET_SUPPORTED_FEATURES;
671 vsocket->features = VIRTIO_NET_SUPPORTED_FEATURES;
673 if (!(flags & RTE_VHOST_USER_IOMMU_SUPPORT)) {
674 vsocket->supported_features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
675 vsocket->features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
678 if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
679 vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
680 if (vsocket->reconnect && reconn_tid == 0) {
681 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);