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",
247 pthread_mutex_lock(&vsocket->conn_mutex);
248 TAILQ_INSERT_TAIL(&vsocket->conn_list, conn, next);
249 pthread_mutex_unlock(&vsocket->conn_mutex);
252 /* call back when there is new vhost-user connection from client */
254 vhost_user_server_new_connection(int fd, void *dat, int *remove __rte_unused)
256 struct vhost_user_socket *vsocket = dat;
258 fd = accept(fd, NULL, NULL);
262 RTE_LOG(INFO, VHOST_CONFIG, "new vhost user connection is %d\n", fd);
263 vhost_user_add_connection(fd, vsocket);
267 vhost_user_read_cb(int connfd, void *dat, int *remove)
269 struct vhost_user_connection *conn = dat;
270 struct vhost_user_socket *vsocket = conn->vsocket;
273 ret = vhost_user_msg_handler(conn->vid, connfd);
277 vhost_destroy_device(conn->vid);
279 pthread_mutex_lock(&vsocket->conn_mutex);
280 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
281 pthread_mutex_unlock(&vsocket->conn_mutex);
285 if (vsocket->reconnect) {
286 create_unix_socket(vsocket);
287 vhost_user_start_client(vsocket);
293 create_unix_socket(struct vhost_user_socket *vsocket)
296 struct sockaddr_un *un = &vsocket->un;
298 fd = socket(AF_UNIX, SOCK_STREAM, 0);
301 RTE_LOG(INFO, VHOST_CONFIG, "vhost-user %s: socket created, fd: %d\n",
302 vsocket->is_server ? "server" : "client", fd);
304 if (!vsocket->is_server && fcntl(fd, F_SETFL, O_NONBLOCK)) {
305 RTE_LOG(ERR, VHOST_CONFIG,
306 "vhost-user: can't set nonblocking mode for socket, fd: "
307 "%d (%s)\n", fd, strerror(errno));
312 memset(un, 0, sizeof(*un));
313 un->sun_family = AF_UNIX;
314 strncpy(un->sun_path, vsocket->path, sizeof(un->sun_path));
315 un->sun_path[sizeof(un->sun_path) - 1] = '\0';
317 vsocket->socket_fd = fd;
322 vhost_user_start_server(struct vhost_user_socket *vsocket)
325 int fd = vsocket->socket_fd;
326 const char *path = vsocket->path;
328 ret = bind(fd, (struct sockaddr *)&vsocket->un, sizeof(vsocket->un));
330 RTE_LOG(ERR, VHOST_CONFIG,
331 "failed to bind to %s: %s; remove it and try again\n",
332 path, strerror(errno));
335 RTE_LOG(INFO, VHOST_CONFIG, "bind to %s\n", path);
337 ret = listen(fd, MAX_VIRTIO_BACKLOG);
341 ret = fdset_add(&vhost_user.fdset, fd, vhost_user_server_new_connection,
344 RTE_LOG(ERR, VHOST_CONFIG,
345 "failed to add listen fd %d to vhost server fdset\n",
357 struct vhost_user_reconnect {
358 struct sockaddr_un un;
360 struct vhost_user_socket *vsocket;
362 TAILQ_ENTRY(vhost_user_reconnect) next;
365 TAILQ_HEAD(vhost_user_reconnect_tailq_list, vhost_user_reconnect);
366 struct vhost_user_reconnect_list {
367 struct vhost_user_reconnect_tailq_list head;
368 pthread_mutex_t mutex;
371 static struct vhost_user_reconnect_list reconn_list;
372 static pthread_t reconn_tid;
375 vhost_user_connect_nonblock(int fd, struct sockaddr *un, size_t sz)
379 ret = connect(fd, un, sz);
380 if (ret < 0 && errno != EISCONN)
383 flags = fcntl(fd, F_GETFL, 0);
385 RTE_LOG(ERR, VHOST_CONFIG,
386 "can't get flags for connfd %d\n", fd);
389 if ((flags & O_NONBLOCK) && fcntl(fd, F_SETFL, flags & ~O_NONBLOCK)) {
390 RTE_LOG(ERR, VHOST_CONFIG,
391 "can't disable nonblocking on fd %d\n", fd);
398 vhost_user_client_reconnect(void *arg __rte_unused)
401 struct vhost_user_reconnect *reconn, *next;
404 pthread_mutex_lock(&reconn_list.mutex);
407 * An equal implementation of TAILQ_FOREACH_SAFE,
408 * which does not exist on all platforms.
410 for (reconn = TAILQ_FIRST(&reconn_list.head);
411 reconn != NULL; reconn = next) {
412 next = TAILQ_NEXT(reconn, next);
414 ret = vhost_user_connect_nonblock(reconn->fd,
415 (struct sockaddr *)&reconn->un,
419 RTE_LOG(ERR, VHOST_CONFIG,
420 "reconnection for fd %d failed\n",
427 RTE_LOG(INFO, VHOST_CONFIG,
428 "%s: connected\n", reconn->vsocket->path);
429 vhost_user_add_connection(reconn->fd, reconn->vsocket);
431 TAILQ_REMOVE(&reconn_list.head, reconn, next);
435 pthread_mutex_unlock(&reconn_list.mutex);
443 vhost_user_reconnect_init(void)
447 pthread_mutex_init(&reconn_list.mutex, NULL);
448 TAILQ_INIT(&reconn_list.head);
450 ret = pthread_create(&reconn_tid, NULL,
451 vhost_user_client_reconnect, NULL);
453 RTE_LOG(ERR, VHOST_CONFIG, "failed to create reconnect thread");
459 vhost_user_start_client(struct vhost_user_socket *vsocket)
462 int fd = vsocket->socket_fd;
463 const char *path = vsocket->path;
464 struct vhost_user_reconnect *reconn;
466 ret = vhost_user_connect_nonblock(fd, (struct sockaddr *)&vsocket->un,
467 sizeof(vsocket->un));
469 vhost_user_add_connection(fd, vsocket);
473 RTE_LOG(WARNING, VHOST_CONFIG,
474 "failed to connect to %s: %s\n",
475 path, strerror(errno));
477 if (ret == -2 || !vsocket->reconnect) {
482 RTE_LOG(INFO, VHOST_CONFIG, "%s: reconnecting...\n", path);
483 reconn = malloc(sizeof(*reconn));
484 if (reconn == NULL) {
485 RTE_LOG(ERR, VHOST_CONFIG,
486 "failed to allocate memory for reconnect\n");
490 reconn->un = vsocket->un;
492 reconn->vsocket = vsocket;
493 pthread_mutex_lock(&reconn_list.mutex);
494 TAILQ_INSERT_TAIL(&reconn_list.head, reconn, next);
495 pthread_mutex_unlock(&reconn_list.mutex);
500 static struct vhost_user_socket *
501 find_vhost_user_socket(const char *path)
505 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
506 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
508 if (!strcmp(vsocket->path, path))
516 rte_vhost_driver_disable_features(const char *path, uint64_t features)
518 struct vhost_user_socket *vsocket;
520 pthread_mutex_lock(&vhost_user.mutex);
521 vsocket = find_vhost_user_socket(path);
523 vsocket->features &= ~features;
524 pthread_mutex_unlock(&vhost_user.mutex);
526 return vsocket ? 0 : -1;
530 rte_vhost_driver_enable_features(const char *path, uint64_t features)
532 struct vhost_user_socket *vsocket;
534 pthread_mutex_lock(&vhost_user.mutex);
535 vsocket = find_vhost_user_socket(path);
537 if ((vsocket->supported_features & features) != features) {
539 * trying to enable features the driver doesn't
542 pthread_mutex_unlock(&vhost_user.mutex);
545 vsocket->features |= features;
547 pthread_mutex_unlock(&vhost_user.mutex);
549 return vsocket ? 0 : -1;
553 rte_vhost_driver_set_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 vsocket->supported_features = features;
561 vsocket->features = features;
563 pthread_mutex_unlock(&vhost_user.mutex);
565 return vsocket ? 0 : -1;
569 rte_vhost_driver_get_features(const char *path, uint64_t *features)
571 struct vhost_user_socket *vsocket;
573 pthread_mutex_lock(&vhost_user.mutex);
574 vsocket = find_vhost_user_socket(path);
576 *features = vsocket->features;
577 pthread_mutex_unlock(&vhost_user.mutex);
580 RTE_LOG(ERR, VHOST_CONFIG,
581 "socket file %s is not registered yet.\n", path);
589 * Register a new vhost-user socket; here we could act as server
590 * (the default case), or client (when RTE_VHOST_USER_CLIENT) flag
594 rte_vhost_driver_register(const char *path, uint64_t flags)
597 struct vhost_user_socket *vsocket;
602 pthread_mutex_lock(&vhost_user.mutex);
604 if (vhost_user.vsocket_cnt == MAX_VHOST_SOCKET) {
605 RTE_LOG(ERR, VHOST_CONFIG,
606 "error: the number of vhost sockets reaches maximum\n");
610 vsocket = malloc(sizeof(struct vhost_user_socket));
613 memset(vsocket, 0, sizeof(struct vhost_user_socket));
614 vsocket->path = strdup(path);
615 TAILQ_INIT(&vsocket->conn_list);
616 pthread_mutex_init(&vsocket->conn_mutex, NULL);
617 vsocket->dequeue_zero_copy = flags & RTE_VHOST_USER_DEQUEUE_ZERO_COPY;
620 * Set the supported features correctly for the builtin vhost-user
623 * Applications know nothing about features the builtin virtio net
624 * driver (virtio_net.c) supports, thus it's not possible for them
625 * to invoke rte_vhost_driver_set_features(). To workaround it, here
626 * we set it unconditionally. If the application want to implement
627 * another vhost-user driver (say SCSI), it should call the
628 * rte_vhost_driver_set_features(), which will overwrite following
631 vsocket->supported_features = VIRTIO_NET_SUPPORTED_FEATURES;
632 vsocket->features = VIRTIO_NET_SUPPORTED_FEATURES;
634 if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
635 vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
636 if (vsocket->reconnect && reconn_tid == 0) {
637 if (vhost_user_reconnect_init() < 0) {
644 vsocket->is_server = true;
646 ret = create_unix_socket(vsocket);
653 vhost_user.vsockets[vhost_user.vsocket_cnt++] = vsocket;
656 pthread_mutex_unlock(&vhost_user.mutex);
662 vhost_user_remove_reconnect(struct vhost_user_socket *vsocket)
665 struct vhost_user_reconnect *reconn, *next;
667 pthread_mutex_lock(&reconn_list.mutex);
669 for (reconn = TAILQ_FIRST(&reconn_list.head);
670 reconn != NULL; reconn = next) {
671 next = TAILQ_NEXT(reconn, next);
673 if (reconn->vsocket == vsocket) {
674 TAILQ_REMOVE(&reconn_list.head, reconn, next);
681 pthread_mutex_unlock(&reconn_list.mutex);
686 * Unregister the specified vhost socket
689 rte_vhost_driver_unregister(const char *path)
693 struct vhost_user_connection *conn, *next;
695 pthread_mutex_lock(&vhost_user.mutex);
697 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
698 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
700 if (!strcmp(vsocket->path, path)) {
701 if (vsocket->is_server) {
702 fdset_del(&vhost_user.fdset, vsocket->socket_fd);
703 close(vsocket->socket_fd);
705 } else if (vsocket->reconnect) {
706 vhost_user_remove_reconnect(vsocket);
709 pthread_mutex_lock(&vsocket->conn_mutex);
710 for (conn = TAILQ_FIRST(&vsocket->conn_list);
713 next = TAILQ_NEXT(conn, next);
715 fdset_del(&vhost_user.fdset, conn->connfd);
716 RTE_LOG(INFO, VHOST_CONFIG,
717 "free connfd = %d for device '%s'\n",
720 vhost_destroy_device(conn->vid);
721 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
724 pthread_mutex_unlock(&vsocket->conn_mutex);
729 count = --vhost_user.vsocket_cnt;
730 vhost_user.vsockets[i] = vhost_user.vsockets[count];
731 vhost_user.vsockets[count] = NULL;
732 pthread_mutex_unlock(&vhost_user.mutex);
737 pthread_mutex_unlock(&vhost_user.mutex);
743 * Register ops so that we can add/remove device to data core.
746 rte_vhost_driver_callback_register(const char *path,
747 struct vhost_device_ops const * const ops)
749 struct vhost_user_socket *vsocket;
751 pthread_mutex_lock(&vhost_user.mutex);
752 vsocket = find_vhost_user_socket(path);
754 vsocket->notify_ops = ops;
755 pthread_mutex_unlock(&vhost_user.mutex);
757 return vsocket ? 0 : -1;
760 struct vhost_device_ops const *
761 vhost_driver_callback_get(const char *path)
763 struct vhost_user_socket *vsocket;
765 pthread_mutex_lock(&vhost_user.mutex);
766 vsocket = find_vhost_user_socket(path);
767 pthread_mutex_unlock(&vhost_user.mutex);
769 return vsocket ? vsocket->notify_ops : NULL;
773 rte_vhost_driver_start(const char *path)
775 struct vhost_user_socket *vsocket;
776 static pthread_t fdset_tid;
778 pthread_mutex_lock(&vhost_user.mutex);
779 vsocket = find_vhost_user_socket(path);
780 pthread_mutex_unlock(&vhost_user.mutex);
785 if (fdset_tid == 0) {
786 int ret = pthread_create(&fdset_tid, NULL, fdset_event_dispatch,
789 RTE_LOG(ERR, VHOST_CONFIG,
790 "failed to create fdset handling thread");
793 if (vsocket->is_server)
794 return vhost_user_start_server(vsocket);
796 return vhost_user_start_client(vsocket);