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();
225 size = strnlen(vsocket->path, PATH_MAX);
226 vhost_set_ifname(vid, vsocket->path, size);
228 if (vsocket->dequeue_zero_copy)
229 vhost_enable_dequeue_zero_copy(vid);
231 RTE_LOG(INFO, VHOST_CONFIG, "new device, handle is %d\n", vid);
234 conn->vsocket = vsocket;
236 ret = fdset_add(&vhost_user.fdset, fd, vhost_user_read_cb,
242 RTE_LOG(ERR, VHOST_CONFIG,
243 "failed to add fd %d into vhost server fdset\n",
248 pthread_mutex_lock(&vsocket->conn_mutex);
249 TAILQ_INSERT_TAIL(&vsocket->conn_list, conn, next);
250 pthread_mutex_unlock(&vsocket->conn_mutex);
253 /* call back when there is new vhost-user connection from client */
255 vhost_user_server_new_connection(int fd, void *dat, int *remove __rte_unused)
257 struct vhost_user_socket *vsocket = dat;
259 fd = accept(fd, NULL, NULL);
263 RTE_LOG(INFO, VHOST_CONFIG, "new vhost user connection is %d\n", fd);
264 vhost_user_add_connection(fd, vsocket);
268 vhost_user_read_cb(int connfd, void *dat, int *remove)
270 struct vhost_user_connection *conn = dat;
271 struct vhost_user_socket *vsocket = conn->vsocket;
274 ret = vhost_user_msg_handler(conn->vid, connfd);
278 vhost_destroy_device(conn->vid);
280 pthread_mutex_lock(&vsocket->conn_mutex);
281 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
282 pthread_mutex_unlock(&vsocket->conn_mutex);
286 if (vsocket->reconnect) {
287 create_unix_socket(vsocket);
288 vhost_user_start_client(vsocket);
294 create_unix_socket(struct vhost_user_socket *vsocket)
297 struct sockaddr_un *un = &vsocket->un;
299 fd = socket(AF_UNIX, SOCK_STREAM, 0);
302 RTE_LOG(INFO, VHOST_CONFIG, "vhost-user %s: socket created, fd: %d\n",
303 vsocket->is_server ? "server" : "client", fd);
305 if (!vsocket->is_server && fcntl(fd, F_SETFL, O_NONBLOCK)) {
306 RTE_LOG(ERR, VHOST_CONFIG,
307 "vhost-user: can't set nonblocking mode for socket, fd: "
308 "%d (%s)\n", fd, strerror(errno));
313 memset(un, 0, sizeof(*un));
314 un->sun_family = AF_UNIX;
315 strncpy(un->sun_path, vsocket->path, sizeof(un->sun_path));
316 un->sun_path[sizeof(un->sun_path) - 1] = '\0';
318 vsocket->socket_fd = fd;
323 vhost_user_start_server(struct vhost_user_socket *vsocket)
326 int fd = vsocket->socket_fd;
327 const char *path = vsocket->path;
329 ret = bind(fd, (struct sockaddr *)&vsocket->un, sizeof(vsocket->un));
331 RTE_LOG(ERR, VHOST_CONFIG,
332 "failed to bind to %s: %s; remove it and try again\n",
333 path, strerror(errno));
336 RTE_LOG(INFO, VHOST_CONFIG, "bind to %s\n", path);
338 ret = listen(fd, MAX_VIRTIO_BACKLOG);
342 ret = fdset_add(&vhost_user.fdset, fd, vhost_user_server_new_connection,
345 RTE_LOG(ERR, VHOST_CONFIG,
346 "failed to add listen fd %d to vhost server fdset\n",
358 struct vhost_user_reconnect {
359 struct sockaddr_un un;
361 struct vhost_user_socket *vsocket;
363 TAILQ_ENTRY(vhost_user_reconnect) next;
366 TAILQ_HEAD(vhost_user_reconnect_tailq_list, vhost_user_reconnect);
367 struct vhost_user_reconnect_list {
368 struct vhost_user_reconnect_tailq_list head;
369 pthread_mutex_t mutex;
372 static struct vhost_user_reconnect_list reconn_list;
373 static pthread_t reconn_tid;
376 vhost_user_connect_nonblock(int fd, struct sockaddr *un, size_t sz)
380 ret = connect(fd, un, sz);
381 if (ret < 0 && errno != EISCONN)
384 flags = fcntl(fd, F_GETFL, 0);
386 RTE_LOG(ERR, VHOST_CONFIG,
387 "can't get flags for connfd %d\n", fd);
390 if ((flags & O_NONBLOCK) && fcntl(fd, F_SETFL, flags & ~O_NONBLOCK)) {
391 RTE_LOG(ERR, VHOST_CONFIG,
392 "can't disable nonblocking on fd %d\n", fd);
399 vhost_user_client_reconnect(void *arg __rte_unused)
402 struct vhost_user_reconnect *reconn, *next;
405 pthread_mutex_lock(&reconn_list.mutex);
408 * An equal implementation of TAILQ_FOREACH_SAFE,
409 * which does not exist on all platforms.
411 for (reconn = TAILQ_FIRST(&reconn_list.head);
412 reconn != NULL; reconn = next) {
413 next = TAILQ_NEXT(reconn, next);
415 ret = vhost_user_connect_nonblock(reconn->fd,
416 (struct sockaddr *)&reconn->un,
420 RTE_LOG(ERR, VHOST_CONFIG,
421 "reconnection for fd %d failed\n",
428 RTE_LOG(INFO, VHOST_CONFIG,
429 "%s: connected\n", reconn->vsocket->path);
430 vhost_user_add_connection(reconn->fd, reconn->vsocket);
432 TAILQ_REMOVE(&reconn_list.head, reconn, next);
436 pthread_mutex_unlock(&reconn_list.mutex);
444 vhost_user_reconnect_init(void)
448 pthread_mutex_init(&reconn_list.mutex, NULL);
449 TAILQ_INIT(&reconn_list.head);
451 ret = pthread_create(&reconn_tid, NULL,
452 vhost_user_client_reconnect, NULL);
454 RTE_LOG(ERR, VHOST_CONFIG, "failed to create reconnect thread");
460 vhost_user_start_client(struct vhost_user_socket *vsocket)
463 int fd = vsocket->socket_fd;
464 const char *path = vsocket->path;
465 struct vhost_user_reconnect *reconn;
467 ret = vhost_user_connect_nonblock(fd, (struct sockaddr *)&vsocket->un,
468 sizeof(vsocket->un));
470 vhost_user_add_connection(fd, vsocket);
474 RTE_LOG(WARNING, VHOST_CONFIG,
475 "failed to connect to %s: %s\n",
476 path, strerror(errno));
478 if (ret == -2 || !vsocket->reconnect) {
483 RTE_LOG(INFO, VHOST_CONFIG, "%s: reconnecting...\n", path);
484 reconn = malloc(sizeof(*reconn));
485 if (reconn == NULL) {
486 RTE_LOG(ERR, VHOST_CONFIG,
487 "failed to allocate memory for reconnect\n");
491 reconn->un = vsocket->un;
493 reconn->vsocket = vsocket;
494 pthread_mutex_lock(&reconn_list.mutex);
495 TAILQ_INSERT_TAIL(&reconn_list.head, reconn, next);
496 pthread_mutex_unlock(&reconn_list.mutex);
501 static struct vhost_user_socket *
502 find_vhost_user_socket(const char *path)
506 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
507 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
509 if (!strcmp(vsocket->path, path))
517 rte_vhost_driver_disable_features(const char *path, uint64_t features)
519 struct vhost_user_socket *vsocket;
521 pthread_mutex_lock(&vhost_user.mutex);
522 vsocket = find_vhost_user_socket(path);
524 vsocket->features &= ~features;
525 pthread_mutex_unlock(&vhost_user.mutex);
527 return vsocket ? 0 : -1;
531 rte_vhost_driver_enable_features(const char *path, uint64_t features)
533 struct vhost_user_socket *vsocket;
535 pthread_mutex_lock(&vhost_user.mutex);
536 vsocket = find_vhost_user_socket(path);
538 if ((vsocket->supported_features & features) != features) {
540 * trying to enable features the driver doesn't
543 pthread_mutex_unlock(&vhost_user.mutex);
546 vsocket->features |= features;
548 pthread_mutex_unlock(&vhost_user.mutex);
550 return vsocket ? 0 : -1;
554 rte_vhost_driver_set_features(const char *path, uint64_t features)
556 struct vhost_user_socket *vsocket;
558 pthread_mutex_lock(&vhost_user.mutex);
559 vsocket = find_vhost_user_socket(path);
561 vsocket->supported_features = features;
562 vsocket->features = features;
564 pthread_mutex_unlock(&vhost_user.mutex);
566 return vsocket ? 0 : -1;
570 rte_vhost_driver_get_features(const char *path, uint64_t *features)
572 struct vhost_user_socket *vsocket;
574 pthread_mutex_lock(&vhost_user.mutex);
575 vsocket = find_vhost_user_socket(path);
577 *features = vsocket->features;
578 pthread_mutex_unlock(&vhost_user.mutex);
581 RTE_LOG(ERR, VHOST_CONFIG,
582 "socket file %s is not registered yet.\n", path);
590 * Register a new vhost-user socket; here we could act as server
591 * (the default case), or client (when RTE_VHOST_USER_CLIENT) flag
595 rte_vhost_driver_register(const char *path, uint64_t flags)
598 struct vhost_user_socket *vsocket;
603 pthread_mutex_lock(&vhost_user.mutex);
605 if (vhost_user.vsocket_cnt == MAX_VHOST_SOCKET) {
606 RTE_LOG(ERR, VHOST_CONFIG,
607 "error: the number of vhost sockets reaches maximum\n");
611 vsocket = malloc(sizeof(struct vhost_user_socket));
614 memset(vsocket, 0, sizeof(struct vhost_user_socket));
615 vsocket->path = strdup(path);
616 TAILQ_INIT(&vsocket->conn_list);
617 pthread_mutex_init(&vsocket->conn_mutex, NULL);
618 vsocket->dequeue_zero_copy = flags & RTE_VHOST_USER_DEQUEUE_ZERO_COPY;
621 * Set the supported features correctly for the builtin vhost-user
624 * Applications know nothing about features the builtin virtio net
625 * driver (virtio_net.c) supports, thus it's not possible for them
626 * to invoke rte_vhost_driver_set_features(). To workaround it, here
627 * we set it unconditionally. If the application want to implement
628 * another vhost-user driver (say SCSI), it should call the
629 * rte_vhost_driver_set_features(), which will overwrite following
632 vsocket->supported_features = VIRTIO_NET_SUPPORTED_FEATURES;
633 vsocket->features = VIRTIO_NET_SUPPORTED_FEATURES;
635 if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
636 vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
637 if (vsocket->reconnect && reconn_tid == 0) {
638 if (vhost_user_reconnect_init() < 0) {
645 vsocket->is_server = true;
647 ret = create_unix_socket(vsocket);
654 vhost_user.vsockets[vhost_user.vsocket_cnt++] = vsocket;
657 pthread_mutex_unlock(&vhost_user.mutex);
663 vhost_user_remove_reconnect(struct vhost_user_socket *vsocket)
666 struct vhost_user_reconnect *reconn, *next;
668 pthread_mutex_lock(&reconn_list.mutex);
670 for (reconn = TAILQ_FIRST(&reconn_list.head);
671 reconn != NULL; reconn = next) {
672 next = TAILQ_NEXT(reconn, next);
674 if (reconn->vsocket == vsocket) {
675 TAILQ_REMOVE(&reconn_list.head, reconn, next);
682 pthread_mutex_unlock(&reconn_list.mutex);
687 * Unregister the specified vhost socket
690 rte_vhost_driver_unregister(const char *path)
694 struct vhost_user_connection *conn, *next;
696 pthread_mutex_lock(&vhost_user.mutex);
698 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
699 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
701 if (!strcmp(vsocket->path, path)) {
702 if (vsocket->is_server) {
703 fdset_del(&vhost_user.fdset, vsocket->socket_fd);
704 close(vsocket->socket_fd);
706 } else if (vsocket->reconnect) {
707 vhost_user_remove_reconnect(vsocket);
710 pthread_mutex_lock(&vsocket->conn_mutex);
711 for (conn = TAILQ_FIRST(&vsocket->conn_list);
714 next = TAILQ_NEXT(conn, next);
716 fdset_del(&vhost_user.fdset, conn->connfd);
717 RTE_LOG(INFO, VHOST_CONFIG,
718 "free connfd = %d for device '%s'\n",
721 vhost_destroy_device(conn->vid);
722 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
725 pthread_mutex_unlock(&vsocket->conn_mutex);
730 count = --vhost_user.vsocket_cnt;
731 vhost_user.vsockets[i] = vhost_user.vsockets[count];
732 vhost_user.vsockets[count] = NULL;
733 pthread_mutex_unlock(&vhost_user.mutex);
738 pthread_mutex_unlock(&vhost_user.mutex);
744 * Register ops so that we can add/remove device to data core.
747 rte_vhost_driver_callback_register(const char *path,
748 struct vhost_device_ops const * const ops)
750 struct vhost_user_socket *vsocket;
752 pthread_mutex_lock(&vhost_user.mutex);
753 vsocket = find_vhost_user_socket(path);
755 vsocket->notify_ops = ops;
756 pthread_mutex_unlock(&vhost_user.mutex);
758 return vsocket ? 0 : -1;
761 struct vhost_device_ops const *
762 vhost_driver_callback_get(const char *path)
764 struct vhost_user_socket *vsocket;
766 pthread_mutex_lock(&vhost_user.mutex);
767 vsocket = find_vhost_user_socket(path);
768 pthread_mutex_unlock(&vhost_user.mutex);
770 return vsocket ? vsocket->notify_ops : NULL;
774 rte_vhost_driver_start(const char *path)
776 struct vhost_user_socket *vsocket;
777 static pthread_t fdset_tid;
779 pthread_mutex_lock(&vhost_user.mutex);
780 vsocket = find_vhost_user_socket(path);
781 pthread_mutex_unlock(&vhost_user.mutex);
786 if (fdset_tid == 0) {
787 int ret = pthread_create(&fdset_tid, NULL, fdset_event_dispatch,
790 RTE_LOG(ERR, VHOST_CONFIG,
791 "failed to create fdset handling thread");
794 if (vsocket->is_server)
795 return vhost_user_start_server(vsocket);
797 return vhost_user_start_client(vsocket);