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;
73 * The "supported_features" indicates the feature bits the
74 * vhost driver supports. The "features" indicates the feature
75 * bits after the rte_vhost_driver_features_disable/enable().
76 * It is also the final feature bits used for vhost-user
77 * features negotiation.
79 uint64_t supported_features;
82 struct vhost_device_ops const *notify_ops;
85 struct vhost_user_connection {
86 struct vhost_user_socket *vsocket;
90 TAILQ_ENTRY(vhost_user_connection) next;
93 #define MAX_VHOST_SOCKET 1024
95 struct vhost_user_socket *vsockets[MAX_VHOST_SOCKET];
98 pthread_mutex_t mutex;
101 #define MAX_VIRTIO_BACKLOG 128
103 static void vhost_user_server_new_connection(int fd, void *data, int *remove);
104 static void vhost_user_read_cb(int fd, void *dat, int *remove);
105 static int create_unix_socket(struct vhost_user_socket *vsocket);
106 static int vhost_user_start_client(struct vhost_user_socket *vsocket);
108 static struct vhost_user vhost_user = {
110 .fd = { [0 ... MAX_FDS - 1] = {-1, NULL, NULL, NULL, 0} },
111 .fd_mutex = PTHREAD_MUTEX_INITIALIZER,
115 .mutex = PTHREAD_MUTEX_INITIALIZER,
118 /* return bytes# of read on success or negative val on failure. */
120 read_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
124 size_t fdsize = fd_num * sizeof(int);
125 char control[CMSG_SPACE(fdsize)];
126 struct cmsghdr *cmsg;
129 memset(&msgh, 0, sizeof(msgh));
131 iov.iov_len = buflen;
135 msgh.msg_control = control;
136 msgh.msg_controllen = sizeof(control);
138 ret = recvmsg(sockfd, &msgh, 0);
140 RTE_LOG(ERR, VHOST_CONFIG, "recvmsg failed\n");
144 if (msgh.msg_flags & (MSG_TRUNC | MSG_CTRUNC)) {
145 RTE_LOG(ERR, VHOST_CONFIG, "truncted msg\n");
149 for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
150 cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
151 if ((cmsg->cmsg_level == SOL_SOCKET) &&
152 (cmsg->cmsg_type == SCM_RIGHTS)) {
153 memcpy(fds, CMSG_DATA(cmsg), fdsize);
162 send_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
167 size_t fdsize = fd_num * sizeof(int);
168 char control[CMSG_SPACE(fdsize)];
169 struct cmsghdr *cmsg;
172 memset(&msgh, 0, sizeof(msgh));
174 iov.iov_len = buflen;
179 if (fds && fd_num > 0) {
180 msgh.msg_control = control;
181 msgh.msg_controllen = sizeof(control);
182 cmsg = CMSG_FIRSTHDR(&msgh);
183 cmsg->cmsg_len = CMSG_LEN(fdsize);
184 cmsg->cmsg_level = SOL_SOCKET;
185 cmsg->cmsg_type = SCM_RIGHTS;
186 memcpy(CMSG_DATA(cmsg), fds, fdsize);
188 msgh.msg_control = NULL;
189 msgh.msg_controllen = 0;
193 ret = sendmsg(sockfd, &msgh, 0);
194 } while (ret < 0 && errno == EINTR);
197 RTE_LOG(ERR, VHOST_CONFIG, "sendmsg error\n");
205 vhost_user_add_connection(int fd, struct vhost_user_socket *vsocket)
209 struct vhost_user_connection *conn;
212 conn = malloc(sizeof(*conn));
218 vid = vhost_new_device();
223 size = strnlen(vsocket->path, PATH_MAX);
224 vhost_set_ifname(vid, vsocket->path, size);
226 if (vsocket->dequeue_zero_copy)
227 vhost_enable_dequeue_zero_copy(vid);
229 RTE_LOG(INFO, VHOST_CONFIG, "new device, handle is %d\n", vid);
231 if (vsocket->notify_ops->new_connection) {
232 ret = vsocket->notify_ops->new_connection(vid);
234 RTE_LOG(ERR, VHOST_CONFIG,
235 "failed to add vhost user connection with fd %d\n",
242 conn->vsocket = vsocket;
244 ret = fdset_add(&vhost_user.fdset, fd, vhost_user_read_cb,
247 RTE_LOG(ERR, VHOST_CONFIG,
248 "failed to add fd %d into vhost server fdset\n",
251 if (vsocket->notify_ops->destroy_connection)
252 vsocket->notify_ops->destroy_connection(conn->vid);
257 pthread_mutex_lock(&vsocket->conn_mutex);
258 TAILQ_INSERT_TAIL(&vsocket->conn_list, conn, next);
259 pthread_mutex_unlock(&vsocket->conn_mutex);
267 /* call back when there is new vhost-user connection from client */
269 vhost_user_server_new_connection(int fd, void *dat, int *remove __rte_unused)
271 struct vhost_user_socket *vsocket = dat;
273 fd = accept(fd, NULL, NULL);
277 RTE_LOG(INFO, VHOST_CONFIG, "new vhost user connection is %d\n", fd);
278 vhost_user_add_connection(fd, vsocket);
282 vhost_user_read_cb(int connfd, void *dat, int *remove)
284 struct vhost_user_connection *conn = dat;
285 struct vhost_user_socket *vsocket = conn->vsocket;
288 ret = vhost_user_msg_handler(conn->vid, connfd);
292 vhost_destroy_device(conn->vid);
294 if (vsocket->notify_ops->destroy_connection)
295 vsocket->notify_ops->destroy_connection(conn->vid);
297 pthread_mutex_lock(&vsocket->conn_mutex);
298 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
299 pthread_mutex_unlock(&vsocket->conn_mutex);
303 if (vsocket->reconnect) {
304 create_unix_socket(vsocket);
305 vhost_user_start_client(vsocket);
311 create_unix_socket(struct vhost_user_socket *vsocket)
314 struct sockaddr_un *un = &vsocket->un;
316 fd = socket(AF_UNIX, SOCK_STREAM, 0);
319 RTE_LOG(INFO, VHOST_CONFIG, "vhost-user %s: socket created, fd: %d\n",
320 vsocket->is_server ? "server" : "client", fd);
322 if (!vsocket->is_server && fcntl(fd, F_SETFL, O_NONBLOCK)) {
323 RTE_LOG(ERR, VHOST_CONFIG,
324 "vhost-user: can't set nonblocking mode for socket, fd: "
325 "%d (%s)\n", fd, strerror(errno));
330 memset(un, 0, sizeof(*un));
331 un->sun_family = AF_UNIX;
332 strncpy(un->sun_path, vsocket->path, sizeof(un->sun_path));
333 un->sun_path[sizeof(un->sun_path) - 1] = '\0';
335 vsocket->socket_fd = fd;
340 vhost_user_start_server(struct vhost_user_socket *vsocket)
343 int fd = vsocket->socket_fd;
344 const char *path = vsocket->path;
346 ret = bind(fd, (struct sockaddr *)&vsocket->un, sizeof(vsocket->un));
348 RTE_LOG(ERR, VHOST_CONFIG,
349 "failed to bind to %s: %s; remove it and try again\n",
350 path, strerror(errno));
353 RTE_LOG(INFO, VHOST_CONFIG, "bind to %s\n", path);
355 ret = listen(fd, MAX_VIRTIO_BACKLOG);
359 ret = fdset_add(&vhost_user.fdset, fd, vhost_user_server_new_connection,
362 RTE_LOG(ERR, VHOST_CONFIG,
363 "failed to add listen fd %d to vhost server fdset\n",
375 struct vhost_user_reconnect {
376 struct sockaddr_un un;
378 struct vhost_user_socket *vsocket;
380 TAILQ_ENTRY(vhost_user_reconnect) next;
383 TAILQ_HEAD(vhost_user_reconnect_tailq_list, vhost_user_reconnect);
384 struct vhost_user_reconnect_list {
385 struct vhost_user_reconnect_tailq_list head;
386 pthread_mutex_t mutex;
389 static struct vhost_user_reconnect_list reconn_list;
390 static pthread_t reconn_tid;
393 vhost_user_connect_nonblock(int fd, struct sockaddr *un, size_t sz)
397 ret = connect(fd, un, sz);
398 if (ret < 0 && errno != EISCONN)
401 flags = fcntl(fd, F_GETFL, 0);
403 RTE_LOG(ERR, VHOST_CONFIG,
404 "can't get flags for connfd %d\n", fd);
407 if ((flags & O_NONBLOCK) && fcntl(fd, F_SETFL, flags & ~O_NONBLOCK)) {
408 RTE_LOG(ERR, VHOST_CONFIG,
409 "can't disable nonblocking on fd %d\n", fd);
416 vhost_user_client_reconnect(void *arg __rte_unused)
419 struct vhost_user_reconnect *reconn, *next;
422 pthread_mutex_lock(&reconn_list.mutex);
425 * An equal implementation of TAILQ_FOREACH_SAFE,
426 * which does not exist on all platforms.
428 for (reconn = TAILQ_FIRST(&reconn_list.head);
429 reconn != NULL; reconn = next) {
430 next = TAILQ_NEXT(reconn, next);
432 ret = vhost_user_connect_nonblock(reconn->fd,
433 (struct sockaddr *)&reconn->un,
437 RTE_LOG(ERR, VHOST_CONFIG,
438 "reconnection for fd %d failed\n",
445 RTE_LOG(INFO, VHOST_CONFIG,
446 "%s: connected\n", reconn->vsocket->path);
447 vhost_user_add_connection(reconn->fd, reconn->vsocket);
449 TAILQ_REMOVE(&reconn_list.head, reconn, next);
453 pthread_mutex_unlock(&reconn_list.mutex);
461 vhost_user_reconnect_init(void)
465 ret = pthread_mutex_init(&reconn_list.mutex, NULL);
467 RTE_LOG(ERR, VHOST_CONFIG, "failed to initialize mutex");
470 TAILQ_INIT(&reconn_list.head);
472 ret = pthread_create(&reconn_tid, NULL,
473 vhost_user_client_reconnect, NULL);
475 RTE_LOG(ERR, VHOST_CONFIG, "failed to create reconnect thread");
476 if (pthread_mutex_destroy(&reconn_list.mutex)) {
477 RTE_LOG(ERR, VHOST_CONFIG,
478 "failed to destroy reconnect mutex");
486 vhost_user_start_client(struct vhost_user_socket *vsocket)
489 int fd = vsocket->socket_fd;
490 const char *path = vsocket->path;
491 struct vhost_user_reconnect *reconn;
493 ret = vhost_user_connect_nonblock(fd, (struct sockaddr *)&vsocket->un,
494 sizeof(vsocket->un));
496 vhost_user_add_connection(fd, vsocket);
500 RTE_LOG(WARNING, VHOST_CONFIG,
501 "failed to connect to %s: %s\n",
502 path, strerror(errno));
504 if (ret == -2 || !vsocket->reconnect) {
509 RTE_LOG(INFO, VHOST_CONFIG, "%s: reconnecting...\n", path);
510 reconn = malloc(sizeof(*reconn));
511 if (reconn == NULL) {
512 RTE_LOG(ERR, VHOST_CONFIG,
513 "failed to allocate memory for reconnect\n");
517 reconn->un = vsocket->un;
519 reconn->vsocket = vsocket;
520 pthread_mutex_lock(&reconn_list.mutex);
521 TAILQ_INSERT_TAIL(&reconn_list.head, reconn, next);
522 pthread_mutex_unlock(&reconn_list.mutex);
527 static struct vhost_user_socket *
528 find_vhost_user_socket(const char *path)
532 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
533 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
535 if (!strcmp(vsocket->path, path))
543 rte_vhost_driver_disable_features(const char *path, uint64_t features)
545 struct vhost_user_socket *vsocket;
547 pthread_mutex_lock(&vhost_user.mutex);
548 vsocket = find_vhost_user_socket(path);
550 vsocket->features &= ~features;
551 pthread_mutex_unlock(&vhost_user.mutex);
553 return vsocket ? 0 : -1;
557 rte_vhost_driver_enable_features(const char *path, uint64_t features)
559 struct vhost_user_socket *vsocket;
561 pthread_mutex_lock(&vhost_user.mutex);
562 vsocket = find_vhost_user_socket(path);
564 if ((vsocket->supported_features & features) != features) {
566 * trying to enable features the driver doesn't
569 pthread_mutex_unlock(&vhost_user.mutex);
572 vsocket->features |= features;
574 pthread_mutex_unlock(&vhost_user.mutex);
576 return vsocket ? 0 : -1;
580 rte_vhost_driver_set_features(const char *path, uint64_t features)
582 struct vhost_user_socket *vsocket;
584 pthread_mutex_lock(&vhost_user.mutex);
585 vsocket = find_vhost_user_socket(path);
587 vsocket->supported_features = features;
588 vsocket->features = features;
590 pthread_mutex_unlock(&vhost_user.mutex);
592 return vsocket ? 0 : -1;
596 rte_vhost_driver_get_features(const char *path, uint64_t *features)
598 struct vhost_user_socket *vsocket;
600 pthread_mutex_lock(&vhost_user.mutex);
601 vsocket = find_vhost_user_socket(path);
603 *features = vsocket->features;
604 pthread_mutex_unlock(&vhost_user.mutex);
607 RTE_LOG(ERR, VHOST_CONFIG,
608 "socket file %s is not registered yet.\n", path);
616 * Register a new vhost-user socket; here we could act as server
617 * (the default case), or client (when RTE_VHOST_USER_CLIENT) flag
621 rte_vhost_driver_register(const char *path, uint64_t flags)
624 struct vhost_user_socket *vsocket;
629 pthread_mutex_lock(&vhost_user.mutex);
631 if (vhost_user.vsocket_cnt == MAX_VHOST_SOCKET) {
632 RTE_LOG(ERR, VHOST_CONFIG,
633 "error: the number of vhost sockets reaches maximum\n");
637 vsocket = malloc(sizeof(struct vhost_user_socket));
640 memset(vsocket, 0, sizeof(struct vhost_user_socket));
641 vsocket->path = strdup(path);
642 if (vsocket->path == NULL) {
643 RTE_LOG(ERR, VHOST_CONFIG,
644 "error: failed to copy socket path string\n");
648 TAILQ_INIT(&vsocket->conn_list);
649 ret = pthread_mutex_init(&vsocket->conn_mutex, NULL);
651 RTE_LOG(ERR, VHOST_CONFIG,
652 "error: failed to init connection mutex\n");
655 vsocket->dequeue_zero_copy = flags & RTE_VHOST_USER_DEQUEUE_ZERO_COPY;
658 * Set the supported features correctly for the builtin vhost-user
661 * Applications know nothing about features the builtin virtio net
662 * driver (virtio_net.c) supports, thus it's not possible for them
663 * to invoke rte_vhost_driver_set_features(). To workaround it, here
664 * we set it unconditionally. If the application want to implement
665 * another vhost-user driver (say SCSI), it should call the
666 * rte_vhost_driver_set_features(), which will overwrite following
669 vsocket->supported_features = VIRTIO_NET_SUPPORTED_FEATURES;
670 vsocket->features = VIRTIO_NET_SUPPORTED_FEATURES;
672 if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
673 vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
674 if (vsocket->reconnect && reconn_tid == 0) {
675 if (vhost_user_reconnect_init() < 0) {
680 vsocket->is_server = true;
682 ret = create_unix_socket(vsocket);
687 vhost_user.vsockets[vhost_user.vsocket_cnt++] = vsocket;
689 pthread_mutex_unlock(&vhost_user.mutex);
693 if (pthread_mutex_destroy(&vsocket->conn_mutex)) {
694 RTE_LOG(ERR, VHOST_CONFIG,
695 "error: failed to destroy connection mutex\n");
701 pthread_mutex_unlock(&vhost_user.mutex);
707 vhost_user_remove_reconnect(struct vhost_user_socket *vsocket)
710 struct vhost_user_reconnect *reconn, *next;
712 pthread_mutex_lock(&reconn_list.mutex);
714 for (reconn = TAILQ_FIRST(&reconn_list.head);
715 reconn != NULL; reconn = next) {
716 next = TAILQ_NEXT(reconn, next);
718 if (reconn->vsocket == vsocket) {
719 TAILQ_REMOVE(&reconn_list.head, reconn, next);
726 pthread_mutex_unlock(&reconn_list.mutex);
731 * Unregister the specified vhost socket
734 rte_vhost_driver_unregister(const char *path)
738 struct vhost_user_connection *conn, *next;
740 pthread_mutex_lock(&vhost_user.mutex);
742 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
743 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
745 if (!strcmp(vsocket->path, path)) {
746 if (vsocket->is_server) {
747 fdset_del(&vhost_user.fdset, vsocket->socket_fd);
748 close(vsocket->socket_fd);
750 } else if (vsocket->reconnect) {
751 vhost_user_remove_reconnect(vsocket);
754 pthread_mutex_lock(&vsocket->conn_mutex);
755 for (conn = TAILQ_FIRST(&vsocket->conn_list);
758 next = TAILQ_NEXT(conn, next);
760 fdset_del(&vhost_user.fdset, conn->connfd);
761 RTE_LOG(INFO, VHOST_CONFIG,
762 "free connfd = %d for device '%s'\n",
765 vhost_destroy_device(conn->vid);
766 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
769 pthread_mutex_unlock(&vsocket->conn_mutex);
771 pthread_mutex_destroy(&vsocket->conn_mutex);
775 count = --vhost_user.vsocket_cnt;
776 vhost_user.vsockets[i] = vhost_user.vsockets[count];
777 vhost_user.vsockets[count] = NULL;
778 pthread_mutex_unlock(&vhost_user.mutex);
783 pthread_mutex_unlock(&vhost_user.mutex);
789 * Register ops so that we can add/remove device to data core.
792 rte_vhost_driver_callback_register(const char *path,
793 struct vhost_device_ops const * const ops)
795 struct vhost_user_socket *vsocket;
797 pthread_mutex_lock(&vhost_user.mutex);
798 vsocket = find_vhost_user_socket(path);
800 vsocket->notify_ops = ops;
801 pthread_mutex_unlock(&vhost_user.mutex);
803 return vsocket ? 0 : -1;
806 struct vhost_device_ops const *
807 vhost_driver_callback_get(const char *path)
809 struct vhost_user_socket *vsocket;
811 pthread_mutex_lock(&vhost_user.mutex);
812 vsocket = find_vhost_user_socket(path);
813 pthread_mutex_unlock(&vhost_user.mutex);
815 return vsocket ? vsocket->notify_ops : NULL;
819 rte_vhost_driver_start(const char *path)
821 struct vhost_user_socket *vsocket;
822 static pthread_t fdset_tid;
824 pthread_mutex_lock(&vhost_user.mutex);
825 vsocket = find_vhost_user_socket(path);
826 pthread_mutex_unlock(&vhost_user.mutex);
831 if (fdset_tid == 0) {
832 int ret = pthread_create(&fdset_tid, NULL, fdset_event_dispatch,
835 RTE_LOG(ERR, VHOST_CONFIG,
836 "failed to create fdset handling thread");
839 if (vsocket->is_server)
840 return vhost_user_start_server(vsocket);
842 return vhost_user_start_client(vsocket);