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",
233 pthread_mutex_lock(&vsocket->conn_mutex);
234 TAILQ_INSERT_TAIL(&vsocket->conn_list, conn, next);
235 pthread_mutex_unlock(&vsocket->conn_mutex);
238 /* call back when there is new vhost-user connection from client */
240 vhost_user_server_new_connection(int fd, void *dat, int *remove __rte_unused)
242 struct vhost_user_socket *vsocket = dat;
244 fd = accept(fd, NULL, NULL);
248 RTE_LOG(INFO, VHOST_CONFIG, "new vhost user connection is %d\n", fd);
249 vhost_user_add_connection(fd, vsocket);
253 vhost_user_read_cb(int connfd, void *dat, int *remove)
255 struct vhost_user_connection *conn = dat;
256 struct vhost_user_socket *vsocket = conn->vsocket;
259 ret = vhost_user_msg_handler(conn->vid, connfd);
263 vhost_destroy_device(conn->vid);
265 pthread_mutex_lock(&vsocket->conn_mutex);
266 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
267 pthread_mutex_unlock(&vsocket->conn_mutex);
271 if (vsocket->reconnect)
272 vhost_user_create_client(vsocket);
277 create_unix_socket(const char *path, struct sockaddr_un *un, bool is_server)
281 fd = socket(AF_UNIX, SOCK_STREAM, 0);
284 RTE_LOG(INFO, VHOST_CONFIG, "vhost-user %s: socket created, fd: %d\n",
285 is_server ? "server" : "client", fd);
287 if (!is_server && fcntl(fd, F_SETFL, O_NONBLOCK)) {
288 RTE_LOG(ERR, VHOST_CONFIG,
289 "vhost-user: can't set nonblocking mode for socket, fd: "
290 "%d (%s)\n", fd, strerror(errno));
295 memset(un, 0, sizeof(*un));
296 un->sun_family = AF_UNIX;
297 strncpy(un->sun_path, path, sizeof(un->sun_path));
298 un->sun_path[sizeof(un->sun_path) - 1] = '\0';
304 vhost_user_create_server(struct vhost_user_socket *vsocket)
308 struct sockaddr_un un;
309 const char *path = vsocket->path;
311 fd = create_unix_socket(path, &un, vsocket->is_server);
315 ret = bind(fd, (struct sockaddr *)&un, sizeof(un));
317 RTE_LOG(ERR, VHOST_CONFIG,
318 "failed to bind to %s: %s; remove it and try again\n",
319 path, strerror(errno));
322 RTE_LOG(INFO, VHOST_CONFIG, "bind to %s\n", path);
324 ret = listen(fd, MAX_VIRTIO_BACKLOG);
328 vsocket->listenfd = fd;
329 ret = fdset_add(&vhost_user.fdset, fd, vhost_user_server_new_connection,
332 RTE_LOG(ERR, VHOST_CONFIG,
333 "failed to add listen fd %d to vhost server fdset\n",
345 struct vhost_user_reconnect {
346 struct sockaddr_un un;
348 struct vhost_user_socket *vsocket;
350 TAILQ_ENTRY(vhost_user_reconnect) next;
353 TAILQ_HEAD(vhost_user_reconnect_tailq_list, vhost_user_reconnect);
354 struct vhost_user_reconnect_list {
355 struct vhost_user_reconnect_tailq_list head;
356 pthread_mutex_t mutex;
359 static struct vhost_user_reconnect_list reconn_list;
360 static pthread_t reconn_tid;
363 vhost_user_connect_nonblock(int fd, struct sockaddr *un, size_t sz)
367 ret = connect(fd, un, sz);
368 if (ret < 0 && errno != EISCONN)
371 flags = fcntl(fd, F_GETFL, 0);
373 RTE_LOG(ERR, VHOST_CONFIG,
374 "can't get flags for connfd %d\n", fd);
377 if ((flags & O_NONBLOCK) && fcntl(fd, F_SETFL, flags & ~O_NONBLOCK)) {
378 RTE_LOG(ERR, VHOST_CONFIG,
379 "can't disable nonblocking on fd %d\n", fd);
386 vhost_user_client_reconnect(void *arg __rte_unused)
389 struct vhost_user_reconnect *reconn, *next;
392 pthread_mutex_lock(&reconn_list.mutex);
395 * An equal implementation of TAILQ_FOREACH_SAFE,
396 * which does not exist on all platforms.
398 for (reconn = TAILQ_FIRST(&reconn_list.head);
399 reconn != NULL; reconn = next) {
400 next = TAILQ_NEXT(reconn, next);
402 ret = vhost_user_connect_nonblock(reconn->fd,
403 (struct sockaddr *)&reconn->un,
407 RTE_LOG(ERR, VHOST_CONFIG,
408 "reconnection for fd %d failed\n",
415 RTE_LOG(INFO, VHOST_CONFIG,
416 "%s: connected\n", reconn->vsocket->path);
417 vhost_user_add_connection(reconn->fd, reconn->vsocket);
419 TAILQ_REMOVE(&reconn_list.head, reconn, next);
423 pthread_mutex_unlock(&reconn_list.mutex);
431 vhost_user_reconnect_init(void)
435 pthread_mutex_init(&reconn_list.mutex, NULL);
436 TAILQ_INIT(&reconn_list.head);
438 ret = pthread_create(&reconn_tid, NULL,
439 vhost_user_client_reconnect, NULL);
441 RTE_LOG(ERR, VHOST_CONFIG, "failed to create reconnect thread");
447 vhost_user_create_client(struct vhost_user_socket *vsocket)
451 struct sockaddr_un un;
452 const char *path = vsocket->path;
453 struct vhost_user_reconnect *reconn;
455 fd = create_unix_socket(path, &un, vsocket->is_server);
459 ret = vhost_user_connect_nonblock(fd, (struct sockaddr *)&un,
462 vhost_user_add_connection(fd, vsocket);
466 RTE_LOG(WARNING, VHOST_CONFIG,
467 "failed to connect to %s: %s\n",
468 path, strerror(errno));
470 if (ret == -2 || !vsocket->reconnect) {
475 RTE_LOG(INFO, VHOST_CONFIG, "%s: reconnecting...\n", path);
476 reconn = malloc(sizeof(*reconn));
477 if (reconn == NULL) {
478 RTE_LOG(ERR, VHOST_CONFIG,
479 "failed to allocate memory for reconnect\n");
485 reconn->vsocket = vsocket;
486 pthread_mutex_lock(&reconn_list.mutex);
487 TAILQ_INSERT_TAIL(&reconn_list.head, reconn, next);
488 pthread_mutex_unlock(&reconn_list.mutex);
494 * Register a new vhost-user socket; here we could act as server
495 * (the default case), or client (when RTE_VHOST_USER_CLIENT) flag
499 rte_vhost_driver_register(const char *path, uint64_t flags)
502 struct vhost_user_socket *vsocket;
507 pthread_mutex_lock(&vhost_user.mutex);
509 if (vhost_user.vsocket_cnt == MAX_VHOST_SOCKET) {
510 RTE_LOG(ERR, VHOST_CONFIG,
511 "error: the number of vhost sockets reaches maximum\n");
515 vsocket = malloc(sizeof(struct vhost_user_socket));
518 memset(vsocket, 0, sizeof(struct vhost_user_socket));
519 vsocket->path = strdup(path);
520 TAILQ_INIT(&vsocket->conn_list);
521 pthread_mutex_init(&vsocket->conn_mutex, NULL);
522 vsocket->dequeue_zero_copy = flags & RTE_VHOST_USER_DEQUEUE_ZERO_COPY;
524 if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
525 vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
526 if (vsocket->reconnect && reconn_tid == 0) {
527 if (vhost_user_reconnect_init() < 0) {
533 ret = vhost_user_create_client(vsocket);
535 vsocket->is_server = true;
536 ret = vhost_user_create_server(vsocket);
544 vhost_user.vsockets[vhost_user.vsocket_cnt++] = vsocket;
547 pthread_mutex_unlock(&vhost_user.mutex);
553 vhost_user_remove_reconnect(struct vhost_user_socket *vsocket)
556 struct vhost_user_reconnect *reconn, *next;
558 pthread_mutex_lock(&reconn_list.mutex);
560 for (reconn = TAILQ_FIRST(&reconn_list.head);
561 reconn != NULL; reconn = next) {
562 next = TAILQ_NEXT(reconn, next);
564 if (reconn->vsocket == vsocket) {
565 TAILQ_REMOVE(&reconn_list.head, reconn, next);
572 pthread_mutex_unlock(&reconn_list.mutex);
577 * Unregister the specified vhost socket
580 rte_vhost_driver_unregister(const char *path)
584 struct vhost_user_connection *conn, *next;
586 pthread_mutex_lock(&vhost_user.mutex);
588 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
589 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
591 if (!strcmp(vsocket->path, path)) {
592 if (vsocket->is_server) {
593 fdset_del(&vhost_user.fdset, vsocket->listenfd);
594 close(vsocket->listenfd);
596 } else if (vsocket->reconnect) {
597 vhost_user_remove_reconnect(vsocket);
600 pthread_mutex_lock(&vsocket->conn_mutex);
601 for (conn = TAILQ_FIRST(&vsocket->conn_list);
604 next = TAILQ_NEXT(conn, next);
606 fdset_del(&vhost_user.fdset, conn->connfd);
607 RTE_LOG(INFO, VHOST_CONFIG,
608 "free connfd = %d for device '%s'\n",
611 vhost_destroy_device(conn->vid);
612 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
615 pthread_mutex_unlock(&vsocket->conn_mutex);
620 count = --vhost_user.vsocket_cnt;
621 vhost_user.vsockets[i] = vhost_user.vsockets[count];
622 vhost_user.vsockets[count] = NULL;
623 pthread_mutex_unlock(&vhost_user.mutex);
628 pthread_mutex_unlock(&vhost_user.mutex);
634 rte_vhost_driver_session_start(void)
636 fdset_event_dispatch(&vhost_user.fdset);