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 struct vhost_user_connection {
73 struct vhost_user_socket *vsocket;
77 TAILQ_ENTRY(vhost_user_connection) next;
80 #define MAX_VHOST_SOCKET 1024
82 struct vhost_user_socket *vsockets[MAX_VHOST_SOCKET];
85 pthread_mutex_t mutex;
88 #define MAX_VIRTIO_BACKLOG 128
90 static void vhost_user_server_new_connection(int fd, void *data, int *remove);
91 static void vhost_user_read_cb(int fd, void *dat, int *remove);
92 static int vhost_user_create_client(struct vhost_user_socket *vsocket);
94 static struct vhost_user vhost_user = {
96 .fd = { [0 ... MAX_FDS - 1] = {-1, NULL, NULL, NULL, 0} },
97 .fd_mutex = PTHREAD_MUTEX_INITIALIZER,
101 .mutex = PTHREAD_MUTEX_INITIALIZER,
104 /* return bytes# of read on success or negative val on failure. */
106 read_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
110 size_t fdsize = fd_num * sizeof(int);
111 char control[CMSG_SPACE(fdsize)];
112 struct cmsghdr *cmsg;
115 memset(&msgh, 0, sizeof(msgh));
117 iov.iov_len = buflen;
121 msgh.msg_control = control;
122 msgh.msg_controllen = sizeof(control);
124 ret = recvmsg(sockfd, &msgh, 0);
126 RTE_LOG(ERR, VHOST_CONFIG, "recvmsg failed\n");
130 if (msgh.msg_flags & (MSG_TRUNC | MSG_CTRUNC)) {
131 RTE_LOG(ERR, VHOST_CONFIG, "truncted msg\n");
135 for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
136 cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
137 if ((cmsg->cmsg_level == SOL_SOCKET) &&
138 (cmsg->cmsg_type == SCM_RIGHTS)) {
139 memcpy(fds, CMSG_DATA(cmsg), fdsize);
148 send_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
153 size_t fdsize = fd_num * sizeof(int);
154 char control[CMSG_SPACE(fdsize)];
155 struct cmsghdr *cmsg;
158 memset(&msgh, 0, sizeof(msgh));
160 iov.iov_len = buflen;
165 if (fds && fd_num > 0) {
166 msgh.msg_control = control;
167 msgh.msg_controllen = sizeof(control);
168 cmsg = CMSG_FIRSTHDR(&msgh);
169 cmsg->cmsg_len = CMSG_LEN(fdsize);
170 cmsg->cmsg_level = SOL_SOCKET;
171 cmsg->cmsg_type = SCM_RIGHTS;
172 memcpy(CMSG_DATA(cmsg), fds, fdsize);
174 msgh.msg_control = NULL;
175 msgh.msg_controllen = 0;
179 ret = sendmsg(sockfd, &msgh, 0);
180 } while (ret < 0 && errno == EINTR);
183 RTE_LOG(ERR, VHOST_CONFIG, "sendmsg error\n");
191 vhost_user_add_connection(int fd, struct vhost_user_socket *vsocket)
195 struct vhost_user_connection *conn;
198 conn = malloc(sizeof(*conn));
204 vid = vhost_new_device();
211 size = strnlen(vsocket->path, PATH_MAX);
212 vhost_set_ifname(vid, vsocket->path, size);
214 if (vsocket->dequeue_zero_copy)
215 vhost_enable_dequeue_zero_copy(vid);
217 RTE_LOG(INFO, VHOST_CONFIG, "new device, handle is %d\n", vid);
220 conn->vsocket = vsocket;
222 ret = fdset_add(&vhost_user.fdset, fd, vhost_user_read_cb,
228 RTE_LOG(ERR, VHOST_CONFIG,
229 "failed to add fd %d into vhost server fdset\n",
234 pthread_mutex_lock(&vsocket->conn_mutex);
235 TAILQ_INSERT_TAIL(&vsocket->conn_list, conn, next);
236 pthread_mutex_unlock(&vsocket->conn_mutex);
239 /* call back when there is new vhost-user connection from client */
241 vhost_user_server_new_connection(int fd, void *dat, int *remove __rte_unused)
243 struct vhost_user_socket *vsocket = dat;
245 fd = accept(fd, NULL, NULL);
249 RTE_LOG(INFO, VHOST_CONFIG, "new vhost user connection is %d\n", fd);
250 vhost_user_add_connection(fd, vsocket);
254 vhost_user_read_cb(int connfd, void *dat, int *remove)
256 struct vhost_user_connection *conn = dat;
257 struct vhost_user_socket *vsocket = conn->vsocket;
260 ret = vhost_user_msg_handler(conn->vid, connfd);
264 vhost_destroy_device(conn->vid);
266 pthread_mutex_lock(&vsocket->conn_mutex);
267 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
268 pthread_mutex_unlock(&vsocket->conn_mutex);
272 if (vsocket->reconnect)
273 vhost_user_create_client(vsocket);
278 create_unix_socket(const char *path, struct sockaddr_un *un, bool is_server)
282 fd = socket(AF_UNIX, SOCK_STREAM, 0);
285 RTE_LOG(INFO, VHOST_CONFIG, "vhost-user %s: socket created, fd: %d\n",
286 is_server ? "server" : "client", fd);
288 if (!is_server && fcntl(fd, F_SETFL, O_NONBLOCK)) {
289 RTE_LOG(ERR, VHOST_CONFIG,
290 "vhost-user: can't set nonblocking mode for socket, fd: "
291 "%d (%s)\n", fd, strerror(errno));
296 memset(un, 0, sizeof(*un));
297 un->sun_family = AF_UNIX;
298 strncpy(un->sun_path, path, sizeof(un->sun_path));
299 un->sun_path[sizeof(un->sun_path) - 1] = '\0';
305 vhost_user_create_server(struct vhost_user_socket *vsocket)
309 struct sockaddr_un un;
310 const char *path = vsocket->path;
312 fd = create_unix_socket(path, &un, vsocket->is_server);
316 ret = bind(fd, (struct sockaddr *)&un, sizeof(un));
318 RTE_LOG(ERR, VHOST_CONFIG,
319 "failed to bind to %s: %s; remove it and try again\n",
320 path, strerror(errno));
323 RTE_LOG(INFO, VHOST_CONFIG, "bind to %s\n", path);
325 ret = listen(fd, MAX_VIRTIO_BACKLOG);
329 vsocket->listenfd = fd;
330 ret = fdset_add(&vhost_user.fdset, fd, vhost_user_server_new_connection,
333 RTE_LOG(ERR, VHOST_CONFIG,
334 "failed to add listen fd %d to vhost server fdset\n",
346 struct vhost_user_reconnect {
347 struct sockaddr_un un;
349 struct vhost_user_socket *vsocket;
351 TAILQ_ENTRY(vhost_user_reconnect) next;
354 TAILQ_HEAD(vhost_user_reconnect_tailq_list, vhost_user_reconnect);
355 struct vhost_user_reconnect_list {
356 struct vhost_user_reconnect_tailq_list head;
357 pthread_mutex_t mutex;
360 static struct vhost_user_reconnect_list reconn_list;
361 static pthread_t reconn_tid;
364 vhost_user_connect_nonblock(int fd, struct sockaddr *un, size_t sz)
368 ret = connect(fd, un, sz);
369 if (ret < 0 && errno != EISCONN)
372 flags = fcntl(fd, F_GETFL, 0);
374 RTE_LOG(ERR, VHOST_CONFIG,
375 "can't get flags for connfd %d\n", fd);
378 if ((flags & O_NONBLOCK) && fcntl(fd, F_SETFL, flags & ~O_NONBLOCK)) {
379 RTE_LOG(ERR, VHOST_CONFIG,
380 "can't disable nonblocking on fd %d\n", fd);
387 vhost_user_client_reconnect(void *arg __rte_unused)
390 struct vhost_user_reconnect *reconn, *next;
393 pthread_mutex_lock(&reconn_list.mutex);
396 * An equal implementation of TAILQ_FOREACH_SAFE,
397 * which does not exist on all platforms.
399 for (reconn = TAILQ_FIRST(&reconn_list.head);
400 reconn != NULL; reconn = next) {
401 next = TAILQ_NEXT(reconn, next);
403 ret = vhost_user_connect_nonblock(reconn->fd,
404 (struct sockaddr *)&reconn->un,
408 RTE_LOG(ERR, VHOST_CONFIG,
409 "reconnection for fd %d failed\n",
416 RTE_LOG(INFO, VHOST_CONFIG,
417 "%s: connected\n", reconn->vsocket->path);
418 vhost_user_add_connection(reconn->fd, reconn->vsocket);
420 TAILQ_REMOVE(&reconn_list.head, reconn, next);
424 pthread_mutex_unlock(&reconn_list.mutex);
432 vhost_user_reconnect_init(void)
436 pthread_mutex_init(&reconn_list.mutex, NULL);
437 TAILQ_INIT(&reconn_list.head);
439 ret = pthread_create(&reconn_tid, NULL,
440 vhost_user_client_reconnect, NULL);
442 RTE_LOG(ERR, VHOST_CONFIG, "failed to create reconnect thread");
448 vhost_user_create_client(struct vhost_user_socket *vsocket)
452 struct sockaddr_un un;
453 const char *path = vsocket->path;
454 struct vhost_user_reconnect *reconn;
456 fd = create_unix_socket(path, &un, vsocket->is_server);
460 ret = vhost_user_connect_nonblock(fd, (struct sockaddr *)&un,
463 vhost_user_add_connection(fd, vsocket);
467 RTE_LOG(WARNING, VHOST_CONFIG,
468 "failed to connect to %s: %s\n",
469 path, strerror(errno));
471 if (ret == -2 || !vsocket->reconnect) {
476 RTE_LOG(INFO, VHOST_CONFIG, "%s: reconnecting...\n", path);
477 reconn = malloc(sizeof(*reconn));
478 if (reconn == NULL) {
479 RTE_LOG(ERR, VHOST_CONFIG,
480 "failed to allocate memory for reconnect\n");
486 reconn->vsocket = vsocket;
487 pthread_mutex_lock(&reconn_list.mutex);
488 TAILQ_INSERT_TAIL(&reconn_list.head, reconn, next);
489 pthread_mutex_unlock(&reconn_list.mutex);
495 * Register a new vhost-user socket; here we could act as server
496 * (the default case), or client (when RTE_VHOST_USER_CLIENT) flag
500 rte_vhost_driver_register(const char *path, uint64_t flags)
503 struct vhost_user_socket *vsocket;
508 pthread_mutex_lock(&vhost_user.mutex);
510 if (vhost_user.vsocket_cnt == MAX_VHOST_SOCKET) {
511 RTE_LOG(ERR, VHOST_CONFIG,
512 "error: the number of vhost sockets reaches maximum\n");
516 vsocket = malloc(sizeof(struct vhost_user_socket));
519 memset(vsocket, 0, sizeof(struct vhost_user_socket));
520 vsocket->path = strdup(path);
521 TAILQ_INIT(&vsocket->conn_list);
522 pthread_mutex_init(&vsocket->conn_mutex, NULL);
523 vsocket->dequeue_zero_copy = flags & RTE_VHOST_USER_DEQUEUE_ZERO_COPY;
525 if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
526 vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
527 if (vsocket->reconnect && reconn_tid == 0) {
528 if (vhost_user_reconnect_init() < 0) {
534 ret = vhost_user_create_client(vsocket);
536 vsocket->is_server = true;
537 ret = vhost_user_create_server(vsocket);
545 vhost_user.vsockets[vhost_user.vsocket_cnt++] = vsocket;
548 pthread_mutex_unlock(&vhost_user.mutex);
554 vhost_user_remove_reconnect(struct vhost_user_socket *vsocket)
557 struct vhost_user_reconnect *reconn, *next;
559 pthread_mutex_lock(&reconn_list.mutex);
561 for (reconn = TAILQ_FIRST(&reconn_list.head);
562 reconn != NULL; reconn = next) {
563 next = TAILQ_NEXT(reconn, next);
565 if (reconn->vsocket == vsocket) {
566 TAILQ_REMOVE(&reconn_list.head, reconn, next);
573 pthread_mutex_unlock(&reconn_list.mutex);
578 * Unregister the specified vhost socket
581 rte_vhost_driver_unregister(const char *path)
585 struct vhost_user_connection *conn, *next;
587 pthread_mutex_lock(&vhost_user.mutex);
589 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
590 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
592 if (!strcmp(vsocket->path, path)) {
593 if (vsocket->is_server) {
594 fdset_del(&vhost_user.fdset, vsocket->listenfd);
595 close(vsocket->listenfd);
597 } else if (vsocket->reconnect) {
598 vhost_user_remove_reconnect(vsocket);
601 pthread_mutex_lock(&vsocket->conn_mutex);
602 for (conn = TAILQ_FIRST(&vsocket->conn_list);
605 next = TAILQ_NEXT(conn, next);
607 fdset_del(&vhost_user.fdset, conn->connfd);
608 RTE_LOG(INFO, VHOST_CONFIG,
609 "free connfd = %d for device '%s'\n",
612 vhost_destroy_device(conn->vid);
613 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
616 pthread_mutex_unlock(&vsocket->conn_mutex);
621 count = --vhost_user.vsocket_cnt;
622 vhost_user.vsockets[i] = vhost_user.vsockets[count];
623 vhost_user.vsockets[count] = NULL;
624 pthread_mutex_unlock(&vhost_user.mutex);
629 pthread_mutex_unlock(&vhost_user.mutex);
635 rte_vhost_driver_session_start(void)
637 fdset_event_dispatch(&vhost_user.fdset);