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);
170 RTE_LOG(ERR, VHOST_CONFIG, "cmsg == NULL\n");
174 cmsg->cmsg_len = CMSG_LEN(fdsize);
175 cmsg->cmsg_level = SOL_SOCKET;
176 cmsg->cmsg_type = SCM_RIGHTS;
177 memcpy(CMSG_DATA(cmsg), fds, fdsize);
179 msgh.msg_control = NULL;
180 msgh.msg_controllen = 0;
184 ret = sendmsg(sockfd, &msgh, 0);
185 } while (ret < 0 && errno == EINTR);
188 RTE_LOG(ERR, VHOST_CONFIG, "sendmsg error\n");
196 vhost_user_add_connection(int fd, struct vhost_user_socket *vsocket)
200 struct vhost_user_connection *conn;
203 conn = malloc(sizeof(*conn));
209 vid = vhost_new_device();
216 size = strnlen(vsocket->path, PATH_MAX);
217 vhost_set_ifname(vid, vsocket->path, size);
219 if (vsocket->dequeue_zero_copy)
220 vhost_enable_dequeue_zero_copy(vid);
222 RTE_LOG(INFO, VHOST_CONFIG, "new device, handle is %d\n", vid);
225 conn->vsocket = vsocket;
227 ret = fdset_add(&vhost_user.fdset, fd, vhost_user_read_cb,
233 RTE_LOG(ERR, VHOST_CONFIG,
234 "failed to add fd %d into vhost server fdset\n",
239 pthread_mutex_lock(&vsocket->conn_mutex);
240 TAILQ_INSERT_TAIL(&vsocket->conn_list, conn, next);
241 pthread_mutex_unlock(&vsocket->conn_mutex);
244 /* call back when there is new vhost-user connection from client */
246 vhost_user_server_new_connection(int fd, void *dat, int *remove __rte_unused)
248 struct vhost_user_socket *vsocket = dat;
250 fd = accept(fd, NULL, NULL);
254 RTE_LOG(INFO, VHOST_CONFIG, "new vhost user connection is %d\n", fd);
255 vhost_user_add_connection(fd, vsocket);
259 vhost_user_read_cb(int connfd, void *dat, int *remove)
261 struct vhost_user_connection *conn = dat;
262 struct vhost_user_socket *vsocket = conn->vsocket;
265 ret = vhost_user_msg_handler(conn->vid, connfd);
269 vhost_destroy_device(conn->vid);
271 pthread_mutex_lock(&vsocket->conn_mutex);
272 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
273 pthread_mutex_unlock(&vsocket->conn_mutex);
277 if (vsocket->reconnect)
278 vhost_user_create_client(vsocket);
283 create_unix_socket(const char *path, struct sockaddr_un *un, bool is_server)
287 fd = socket(AF_UNIX, SOCK_STREAM, 0);
290 RTE_LOG(INFO, VHOST_CONFIG, "vhost-user %s: socket created, fd: %d\n",
291 is_server ? "server" : "client", fd);
293 if (!is_server && fcntl(fd, F_SETFL, O_NONBLOCK)) {
294 RTE_LOG(ERR, VHOST_CONFIG,
295 "vhost-user: can't set nonblocking mode for socket, fd: "
296 "%d (%s)\n", fd, strerror(errno));
301 memset(un, 0, sizeof(*un));
302 un->sun_family = AF_UNIX;
303 strncpy(un->sun_path, path, sizeof(un->sun_path));
304 un->sun_path[sizeof(un->sun_path) - 1] = '\0';
310 vhost_user_create_server(struct vhost_user_socket *vsocket)
314 struct sockaddr_un un;
315 const char *path = vsocket->path;
317 fd = create_unix_socket(path, &un, vsocket->is_server);
321 ret = bind(fd, (struct sockaddr *)&un, sizeof(un));
323 RTE_LOG(ERR, VHOST_CONFIG,
324 "failed to bind to %s: %s; remove it and try again\n",
325 path, strerror(errno));
328 RTE_LOG(INFO, VHOST_CONFIG, "bind to %s\n", path);
330 ret = listen(fd, MAX_VIRTIO_BACKLOG);
334 vsocket->listenfd = fd;
335 ret = fdset_add(&vhost_user.fdset, fd, vhost_user_server_new_connection,
338 RTE_LOG(ERR, VHOST_CONFIG,
339 "failed to add listen fd %d to vhost server fdset\n",
351 struct vhost_user_reconnect {
352 struct sockaddr_un un;
354 struct vhost_user_socket *vsocket;
356 TAILQ_ENTRY(vhost_user_reconnect) next;
359 TAILQ_HEAD(vhost_user_reconnect_tailq_list, vhost_user_reconnect);
360 struct vhost_user_reconnect_list {
361 struct vhost_user_reconnect_tailq_list head;
362 pthread_mutex_t mutex;
365 static struct vhost_user_reconnect_list reconn_list;
366 static pthread_t reconn_tid;
369 vhost_user_connect_nonblock(int fd, struct sockaddr *un, size_t sz)
373 ret = connect(fd, un, sz);
374 if (ret < 0 && errno != EISCONN)
377 flags = fcntl(fd, F_GETFL, 0);
379 RTE_LOG(ERR, VHOST_CONFIG,
380 "can't get flags for connfd %d\n", fd);
383 if ((flags & O_NONBLOCK) && fcntl(fd, F_SETFL, flags & ~O_NONBLOCK)) {
384 RTE_LOG(ERR, VHOST_CONFIG,
385 "can't disable nonblocking on fd %d\n", fd);
392 vhost_user_client_reconnect(void *arg __rte_unused)
395 struct vhost_user_reconnect *reconn, *next;
398 pthread_mutex_lock(&reconn_list.mutex);
401 * An equal implementation of TAILQ_FOREACH_SAFE,
402 * which does not exist on all platforms.
404 for (reconn = TAILQ_FIRST(&reconn_list.head);
405 reconn != NULL; reconn = next) {
406 next = TAILQ_NEXT(reconn, next);
408 ret = vhost_user_connect_nonblock(reconn->fd,
409 (struct sockaddr *)&reconn->un,
413 RTE_LOG(ERR, VHOST_CONFIG,
414 "reconnection for fd %d failed\n",
421 RTE_LOG(INFO, VHOST_CONFIG,
422 "%s: connected\n", reconn->vsocket->path);
423 vhost_user_add_connection(reconn->fd, reconn->vsocket);
425 TAILQ_REMOVE(&reconn_list.head, reconn, next);
429 pthread_mutex_unlock(&reconn_list.mutex);
437 vhost_user_reconnect_init(void)
441 pthread_mutex_init(&reconn_list.mutex, NULL);
442 TAILQ_INIT(&reconn_list.head);
444 ret = pthread_create(&reconn_tid, NULL,
445 vhost_user_client_reconnect, NULL);
447 RTE_LOG(ERR, VHOST_CONFIG, "failed to create reconnect thread");
453 vhost_user_create_client(struct vhost_user_socket *vsocket)
457 struct sockaddr_un un;
458 const char *path = vsocket->path;
459 struct vhost_user_reconnect *reconn;
461 fd = create_unix_socket(path, &un, vsocket->is_server);
465 ret = vhost_user_connect_nonblock(fd, (struct sockaddr *)&un,
468 vhost_user_add_connection(fd, vsocket);
472 RTE_LOG(WARNING, VHOST_CONFIG,
473 "failed to connect to %s: %s\n",
474 path, strerror(errno));
476 if (ret == -2 || !vsocket->reconnect) {
481 RTE_LOG(INFO, VHOST_CONFIG, "%s: reconnecting...\n", path);
482 reconn = malloc(sizeof(*reconn));
483 if (reconn == NULL) {
484 RTE_LOG(ERR, VHOST_CONFIG,
485 "failed to allocate memory for reconnect\n");
491 reconn->vsocket = vsocket;
492 pthread_mutex_lock(&reconn_list.mutex);
493 TAILQ_INSERT_TAIL(&reconn_list.head, reconn, next);
494 pthread_mutex_unlock(&reconn_list.mutex);
500 * Register a new vhost-user socket; here we could act as server
501 * (the default case), or client (when RTE_VHOST_USER_CLIENT) flag
505 rte_vhost_driver_register(const char *path, uint64_t flags)
508 struct vhost_user_socket *vsocket;
513 pthread_mutex_lock(&vhost_user.mutex);
515 if (vhost_user.vsocket_cnt == MAX_VHOST_SOCKET) {
516 RTE_LOG(ERR, VHOST_CONFIG,
517 "error: the number of vhost sockets reaches maximum\n");
521 vsocket = malloc(sizeof(struct vhost_user_socket));
524 memset(vsocket, 0, sizeof(struct vhost_user_socket));
525 vsocket->path = strdup(path);
526 TAILQ_INIT(&vsocket->conn_list);
527 pthread_mutex_init(&vsocket->conn_mutex, NULL);
528 vsocket->dequeue_zero_copy = flags & RTE_VHOST_USER_DEQUEUE_ZERO_COPY;
530 if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
531 vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
532 if (vsocket->reconnect && reconn_tid == 0) {
533 if (vhost_user_reconnect_init() != 0) {
539 ret = vhost_user_create_client(vsocket);
541 vsocket->is_server = true;
542 ret = vhost_user_create_server(vsocket);
550 vhost_user.vsockets[vhost_user.vsocket_cnt++] = vsocket;
553 pthread_mutex_unlock(&vhost_user.mutex);
559 vhost_user_remove_reconnect(struct vhost_user_socket *vsocket)
562 struct vhost_user_reconnect *reconn, *next;
564 pthread_mutex_lock(&reconn_list.mutex);
566 for (reconn = TAILQ_FIRST(&reconn_list.head);
567 reconn != NULL; reconn = next) {
568 next = TAILQ_NEXT(reconn, next);
570 if (reconn->vsocket == vsocket) {
571 TAILQ_REMOVE(&reconn_list.head, reconn, next);
578 pthread_mutex_unlock(&reconn_list.mutex);
583 * Unregister the specified vhost socket
586 rte_vhost_driver_unregister(const char *path)
590 struct vhost_user_connection *conn, *next;
592 pthread_mutex_lock(&vhost_user.mutex);
594 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
595 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
597 if (!strcmp(vsocket->path, path)) {
598 if (vsocket->is_server) {
599 fdset_del(&vhost_user.fdset, vsocket->listenfd);
600 close(vsocket->listenfd);
602 } else if (vsocket->reconnect) {
603 vhost_user_remove_reconnect(vsocket);
607 pthread_mutex_lock(&vsocket->conn_mutex);
608 for (conn = TAILQ_FIRST(&vsocket->conn_list);
611 next = TAILQ_NEXT(conn, next);
614 * If r/wcb is executing, release the
615 * conn_mutex lock, and try again since
616 * the r/wcb may use the conn_mutex lock.
618 if (fdset_try_del(&vhost_user.fdset,
619 conn->connfd) == -1) {
620 pthread_mutex_unlock(
621 &vsocket->conn_mutex);
625 RTE_LOG(INFO, VHOST_CONFIG,
626 "free connfd = %d for device '%s'\n",
629 vhost_destroy_device(conn->vid);
630 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
633 pthread_mutex_unlock(&vsocket->conn_mutex);
638 count = --vhost_user.vsocket_cnt;
639 vhost_user.vsockets[i] = vhost_user.vsockets[count];
640 vhost_user.vsockets[count] = NULL;
641 pthread_mutex_unlock(&vhost_user.mutex);
646 pthread_mutex_unlock(&vhost_user.mutex);
652 rte_vhost_driver_session_start(void)
654 fdset_event_dispatch(&vhost_user.fdset);