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;
69 bool dequeue_zero_copy;
72 * The "supported_features" indicates the feature bits the
73 * vhost driver supports. The "features" indicates the feature
74 * bits after the rte_vhost_driver_features_disable/enable().
75 * It is also the final feature bits used for vhost-user
76 * features negotiation.
78 uint64_t supported_features;
82 struct vhost_user_connection {
83 struct vhost_user_socket *vsocket;
87 TAILQ_ENTRY(vhost_user_connection) next;
90 #define MAX_VHOST_SOCKET 1024
92 struct vhost_user_socket *vsockets[MAX_VHOST_SOCKET];
95 pthread_mutex_t mutex;
98 #define MAX_VIRTIO_BACKLOG 128
100 static void vhost_user_server_new_connection(int fd, void *data, int *remove);
101 static void vhost_user_read_cb(int fd, void *dat, int *remove);
102 static int vhost_user_create_client(struct vhost_user_socket *vsocket);
104 static struct vhost_user vhost_user = {
106 .fd = { [0 ... MAX_FDS - 1] = {-1, NULL, NULL, NULL, 0} },
107 .fd_mutex = PTHREAD_MUTEX_INITIALIZER,
111 .mutex = PTHREAD_MUTEX_INITIALIZER,
114 /* return bytes# of read on success or negative val on failure. */
116 read_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
120 size_t fdsize = fd_num * sizeof(int);
121 char control[CMSG_SPACE(fdsize)];
122 struct cmsghdr *cmsg;
125 memset(&msgh, 0, sizeof(msgh));
127 iov.iov_len = buflen;
131 msgh.msg_control = control;
132 msgh.msg_controllen = sizeof(control);
134 ret = recvmsg(sockfd, &msgh, 0);
136 RTE_LOG(ERR, VHOST_CONFIG, "recvmsg failed\n");
140 if (msgh.msg_flags & (MSG_TRUNC | MSG_CTRUNC)) {
141 RTE_LOG(ERR, VHOST_CONFIG, "truncted msg\n");
145 for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
146 cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
147 if ((cmsg->cmsg_level == SOL_SOCKET) &&
148 (cmsg->cmsg_type == SCM_RIGHTS)) {
149 memcpy(fds, CMSG_DATA(cmsg), fdsize);
158 send_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
163 size_t fdsize = fd_num * sizeof(int);
164 char control[CMSG_SPACE(fdsize)];
165 struct cmsghdr *cmsg;
168 memset(&msgh, 0, sizeof(msgh));
170 iov.iov_len = buflen;
175 if (fds && fd_num > 0) {
176 msgh.msg_control = control;
177 msgh.msg_controllen = sizeof(control);
178 cmsg = CMSG_FIRSTHDR(&msgh);
179 cmsg->cmsg_len = CMSG_LEN(fdsize);
180 cmsg->cmsg_level = SOL_SOCKET;
181 cmsg->cmsg_type = SCM_RIGHTS;
182 memcpy(CMSG_DATA(cmsg), fds, fdsize);
184 msgh.msg_control = NULL;
185 msgh.msg_controllen = 0;
189 ret = sendmsg(sockfd, &msgh, 0);
190 } while (ret < 0 && errno == EINTR);
193 RTE_LOG(ERR, VHOST_CONFIG, "sendmsg error\n");
201 vhost_user_add_connection(int fd, struct vhost_user_socket *vsocket)
205 struct vhost_user_connection *conn;
208 conn = malloc(sizeof(*conn));
214 vid = vhost_new_device();
221 size = strnlen(vsocket->path, PATH_MAX);
222 vhost_set_ifname(vid, vsocket->path, size);
224 if (vsocket->dequeue_zero_copy)
225 vhost_enable_dequeue_zero_copy(vid);
227 RTE_LOG(INFO, VHOST_CONFIG, "new device, handle is %d\n", vid);
230 conn->vsocket = vsocket;
232 ret = fdset_add(&vhost_user.fdset, fd, vhost_user_read_cb,
238 RTE_LOG(ERR, VHOST_CONFIG,
239 "failed to add fd %d into vhost server fdset\n",
243 pthread_mutex_lock(&vsocket->conn_mutex);
244 TAILQ_INSERT_TAIL(&vsocket->conn_list, conn, next);
245 pthread_mutex_unlock(&vsocket->conn_mutex);
248 /* call back when there is new vhost-user connection from client */
250 vhost_user_server_new_connection(int fd, void *dat, int *remove __rte_unused)
252 struct vhost_user_socket *vsocket = dat;
254 fd = accept(fd, NULL, NULL);
258 RTE_LOG(INFO, VHOST_CONFIG, "new vhost user connection is %d\n", fd);
259 vhost_user_add_connection(fd, vsocket);
263 vhost_user_read_cb(int connfd, void *dat, int *remove)
265 struct vhost_user_connection *conn = dat;
266 struct vhost_user_socket *vsocket = conn->vsocket;
269 ret = vhost_user_msg_handler(conn->vid, connfd);
273 vhost_destroy_device(conn->vid);
275 pthread_mutex_lock(&vsocket->conn_mutex);
276 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
277 pthread_mutex_unlock(&vsocket->conn_mutex);
281 if (vsocket->reconnect)
282 vhost_user_create_client(vsocket);
287 create_unix_socket(const char *path, struct sockaddr_un *un, bool is_server)
291 fd = socket(AF_UNIX, SOCK_STREAM, 0);
294 RTE_LOG(INFO, VHOST_CONFIG, "vhost-user %s: socket created, fd: %d\n",
295 is_server ? "server" : "client", fd);
297 if (!is_server && fcntl(fd, F_SETFL, O_NONBLOCK)) {
298 RTE_LOG(ERR, VHOST_CONFIG,
299 "vhost-user: can't set nonblocking mode for socket, fd: "
300 "%d (%s)\n", fd, strerror(errno));
305 memset(un, 0, sizeof(*un));
306 un->sun_family = AF_UNIX;
307 strncpy(un->sun_path, path, sizeof(un->sun_path));
308 un->sun_path[sizeof(un->sun_path) - 1] = '\0';
314 vhost_user_create_server(struct vhost_user_socket *vsocket)
318 struct sockaddr_un un;
319 const char *path = vsocket->path;
321 fd = create_unix_socket(path, &un, vsocket->is_server);
325 ret = bind(fd, (struct sockaddr *)&un, sizeof(un));
327 RTE_LOG(ERR, VHOST_CONFIG,
328 "failed to bind to %s: %s; remove it and try again\n",
329 path, strerror(errno));
332 RTE_LOG(INFO, VHOST_CONFIG, "bind to %s\n", path);
334 ret = listen(fd, MAX_VIRTIO_BACKLOG);
338 vsocket->listenfd = fd;
339 ret = fdset_add(&vhost_user.fdset, fd, vhost_user_server_new_connection,
342 RTE_LOG(ERR, VHOST_CONFIG,
343 "failed to add listen fd %d to vhost server fdset\n",
355 struct vhost_user_reconnect {
356 struct sockaddr_un un;
358 struct vhost_user_socket *vsocket;
360 TAILQ_ENTRY(vhost_user_reconnect) next;
363 TAILQ_HEAD(vhost_user_reconnect_tailq_list, vhost_user_reconnect);
364 struct vhost_user_reconnect_list {
365 struct vhost_user_reconnect_tailq_list head;
366 pthread_mutex_t mutex;
369 static struct vhost_user_reconnect_list reconn_list;
370 static pthread_t reconn_tid;
373 vhost_user_connect_nonblock(int fd, struct sockaddr *un, size_t sz)
377 ret = connect(fd, un, sz);
378 if (ret < 0 && errno != EISCONN)
381 flags = fcntl(fd, F_GETFL, 0);
383 RTE_LOG(ERR, VHOST_CONFIG,
384 "can't get flags for connfd %d\n", fd);
387 if ((flags & O_NONBLOCK) && fcntl(fd, F_SETFL, flags & ~O_NONBLOCK)) {
388 RTE_LOG(ERR, VHOST_CONFIG,
389 "can't disable nonblocking on fd %d\n", fd);
396 vhost_user_client_reconnect(void *arg __rte_unused)
399 struct vhost_user_reconnect *reconn, *next;
402 pthread_mutex_lock(&reconn_list.mutex);
405 * An equal implementation of TAILQ_FOREACH_SAFE,
406 * which does not exist on all platforms.
408 for (reconn = TAILQ_FIRST(&reconn_list.head);
409 reconn != NULL; reconn = next) {
410 next = TAILQ_NEXT(reconn, next);
412 ret = vhost_user_connect_nonblock(reconn->fd,
413 (struct sockaddr *)&reconn->un,
417 RTE_LOG(ERR, VHOST_CONFIG,
418 "reconnection for fd %d failed\n",
425 RTE_LOG(INFO, VHOST_CONFIG,
426 "%s: connected\n", reconn->vsocket->path);
427 vhost_user_add_connection(reconn->fd, reconn->vsocket);
429 TAILQ_REMOVE(&reconn_list.head, reconn, next);
433 pthread_mutex_unlock(&reconn_list.mutex);
441 vhost_user_reconnect_init(void)
445 pthread_mutex_init(&reconn_list.mutex, NULL);
446 TAILQ_INIT(&reconn_list.head);
448 ret = pthread_create(&reconn_tid, NULL,
449 vhost_user_client_reconnect, NULL);
451 RTE_LOG(ERR, VHOST_CONFIG, "failed to create reconnect thread");
457 vhost_user_create_client(struct vhost_user_socket *vsocket)
461 struct sockaddr_un un;
462 const char *path = vsocket->path;
463 struct vhost_user_reconnect *reconn;
465 fd = create_unix_socket(path, &un, vsocket->is_server);
469 ret = vhost_user_connect_nonblock(fd, (struct sockaddr *)&un,
472 vhost_user_add_connection(fd, vsocket);
476 RTE_LOG(WARNING, VHOST_CONFIG,
477 "failed to connect to %s: %s\n",
478 path, strerror(errno));
480 if (ret == -2 || !vsocket->reconnect) {
485 RTE_LOG(INFO, VHOST_CONFIG, "%s: reconnecting...\n", path);
486 reconn = malloc(sizeof(*reconn));
487 if (reconn == NULL) {
488 RTE_LOG(ERR, VHOST_CONFIG,
489 "failed to allocate memory for reconnect\n");
495 reconn->vsocket = vsocket;
496 pthread_mutex_lock(&reconn_list.mutex);
497 TAILQ_INSERT_TAIL(&reconn_list.head, reconn, next);
498 pthread_mutex_unlock(&reconn_list.mutex);
503 static struct vhost_user_socket *
504 find_vhost_user_socket(const char *path)
508 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
509 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
511 if (!strcmp(vsocket->path, path))
519 rte_vhost_driver_disable_features(const char *path, uint64_t features)
521 struct vhost_user_socket *vsocket;
523 pthread_mutex_lock(&vhost_user.mutex);
524 vsocket = find_vhost_user_socket(path);
526 vsocket->features &= ~features;
527 pthread_mutex_unlock(&vhost_user.mutex);
529 return vsocket ? 0 : -1;
533 rte_vhost_driver_enable_features(const char *path, uint64_t features)
535 struct vhost_user_socket *vsocket;
537 pthread_mutex_lock(&vhost_user.mutex);
538 vsocket = find_vhost_user_socket(path);
540 if ((vsocket->supported_features & features) != features) {
542 * trying to enable features the driver doesn't
545 pthread_mutex_unlock(&vhost_user.mutex);
548 vsocket->features |= features;
550 pthread_mutex_unlock(&vhost_user.mutex);
552 return vsocket ? 0 : -1;
556 rte_vhost_driver_set_features(const char *path, uint64_t features)
558 struct vhost_user_socket *vsocket;
560 pthread_mutex_lock(&vhost_user.mutex);
561 vsocket = find_vhost_user_socket(path);
563 vsocket->supported_features = features;
564 vsocket->features = features;
566 pthread_mutex_unlock(&vhost_user.mutex);
568 return vsocket ? 0 : -1;
572 rte_vhost_driver_get_features(const char *path, uint64_t *features)
574 struct vhost_user_socket *vsocket;
576 pthread_mutex_lock(&vhost_user.mutex);
577 vsocket = find_vhost_user_socket(path);
579 *features = vsocket->features;
580 pthread_mutex_unlock(&vhost_user.mutex);
582 return vsocket ? 0 : -1;
586 * Register a new vhost-user socket; here we could act as server
587 * (the default case), or client (when RTE_VHOST_USER_CLIENT) flag
591 rte_vhost_driver_register(const char *path, uint64_t flags)
594 struct vhost_user_socket *vsocket;
599 pthread_mutex_lock(&vhost_user.mutex);
601 if (vhost_user.vsocket_cnt == MAX_VHOST_SOCKET) {
602 RTE_LOG(ERR, VHOST_CONFIG,
603 "error: the number of vhost sockets reaches maximum\n");
607 vsocket = malloc(sizeof(struct vhost_user_socket));
610 memset(vsocket, 0, sizeof(struct vhost_user_socket));
611 vsocket->path = strdup(path);
612 TAILQ_INIT(&vsocket->conn_list);
613 pthread_mutex_init(&vsocket->conn_mutex, NULL);
614 vsocket->dequeue_zero_copy = flags & RTE_VHOST_USER_DEQUEUE_ZERO_COPY;
616 if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
617 vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
618 if (vsocket->reconnect && reconn_tid == 0) {
619 if (vhost_user_reconnect_init() < 0) {
625 ret = vhost_user_create_client(vsocket);
627 vsocket->is_server = true;
628 ret = vhost_user_create_server(vsocket);
636 vhost_user.vsockets[vhost_user.vsocket_cnt++] = vsocket;
639 pthread_mutex_unlock(&vhost_user.mutex);
645 vhost_user_remove_reconnect(struct vhost_user_socket *vsocket)
648 struct vhost_user_reconnect *reconn, *next;
650 pthread_mutex_lock(&reconn_list.mutex);
652 for (reconn = TAILQ_FIRST(&reconn_list.head);
653 reconn != NULL; reconn = next) {
654 next = TAILQ_NEXT(reconn, next);
656 if (reconn->vsocket == vsocket) {
657 TAILQ_REMOVE(&reconn_list.head, reconn, next);
664 pthread_mutex_unlock(&reconn_list.mutex);
669 * Unregister the specified vhost socket
672 rte_vhost_driver_unregister(const char *path)
676 struct vhost_user_connection *conn, *next;
678 pthread_mutex_lock(&vhost_user.mutex);
680 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
681 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
683 if (!strcmp(vsocket->path, path)) {
684 if (vsocket->is_server) {
685 fdset_del(&vhost_user.fdset, vsocket->listenfd);
686 close(vsocket->listenfd);
688 } else if (vsocket->reconnect) {
689 vhost_user_remove_reconnect(vsocket);
692 pthread_mutex_lock(&vsocket->conn_mutex);
693 for (conn = TAILQ_FIRST(&vsocket->conn_list);
696 next = TAILQ_NEXT(conn, next);
698 fdset_del(&vhost_user.fdset, conn->connfd);
699 RTE_LOG(INFO, VHOST_CONFIG,
700 "free connfd = %d for device '%s'\n",
703 vhost_destroy_device(conn->vid);
704 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
707 pthread_mutex_unlock(&vsocket->conn_mutex);
712 count = --vhost_user.vsocket_cnt;
713 vhost_user.vsockets[i] = vhost_user.vsockets[count];
714 vhost_user.vsockets[count] = NULL;
715 pthread_mutex_unlock(&vhost_user.mutex);
720 pthread_mutex_unlock(&vhost_user.mutex);
726 rte_vhost_driver_session_start(void)
728 fdset_event_dispatch(&vhost_user.fdset);