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 * Every time rte_vhost_driver_register() is invoked, an associated
57 * vhost_user_socket struct will be created.
59 struct vhost_user_socket {
65 bool dequeue_zero_copy;
68 struct vhost_user_connection {
69 struct vhost_user_socket *vsocket;
73 #define MAX_VHOST_SOCKET 1024
75 struct vhost_user_socket *vsockets[MAX_VHOST_SOCKET];
78 pthread_mutex_t mutex;
81 #define MAX_VIRTIO_BACKLOG 128
83 static void vhost_user_server_new_connection(int fd, void *data, int *remove);
84 static void vhost_user_read_cb(int fd, void *dat, int *remove);
85 static int vhost_user_create_client(struct vhost_user_socket *vsocket);
87 static struct vhost_user vhost_user = {
89 .fd = { [0 ... MAX_FDS - 1] = {-1, NULL, NULL, NULL, 0} },
90 .fd_mutex = PTHREAD_MUTEX_INITIALIZER,
94 .mutex = PTHREAD_MUTEX_INITIALIZER,
97 /* return bytes# of read on success or negative val on failure. */
99 read_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
103 size_t fdsize = fd_num * sizeof(int);
104 char control[CMSG_SPACE(fdsize)];
105 struct cmsghdr *cmsg;
108 memset(&msgh, 0, sizeof(msgh));
110 iov.iov_len = buflen;
114 msgh.msg_control = control;
115 msgh.msg_controllen = sizeof(control);
117 ret = recvmsg(sockfd, &msgh, 0);
119 RTE_LOG(ERR, VHOST_CONFIG, "recvmsg failed\n");
123 if (msgh.msg_flags & (MSG_TRUNC | MSG_CTRUNC)) {
124 RTE_LOG(ERR, VHOST_CONFIG, "truncted msg\n");
128 for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
129 cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
130 if ((cmsg->cmsg_level == SOL_SOCKET) &&
131 (cmsg->cmsg_type == SCM_RIGHTS)) {
132 memcpy(fds, CMSG_DATA(cmsg), fdsize);
141 send_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
146 size_t fdsize = fd_num * sizeof(int);
147 char control[CMSG_SPACE(fdsize)];
148 struct cmsghdr *cmsg;
151 memset(&msgh, 0, sizeof(msgh));
153 iov.iov_len = buflen;
158 if (fds && fd_num > 0) {
159 msgh.msg_control = control;
160 msgh.msg_controllen = sizeof(control);
161 cmsg = CMSG_FIRSTHDR(&msgh);
162 cmsg->cmsg_len = CMSG_LEN(fdsize);
163 cmsg->cmsg_level = SOL_SOCKET;
164 cmsg->cmsg_type = SCM_RIGHTS;
165 memcpy(CMSG_DATA(cmsg), fds, fdsize);
167 msgh.msg_control = NULL;
168 msgh.msg_controllen = 0;
172 ret = sendmsg(sockfd, &msgh, 0);
173 } while (ret < 0 && errno == EINTR);
176 RTE_LOG(ERR, VHOST_CONFIG, "sendmsg error\n");
184 vhost_user_add_connection(int fd, struct vhost_user_socket *vsocket)
188 struct vhost_user_connection *conn;
191 conn = malloc(sizeof(*conn));
197 vid = vhost_new_device();
204 size = strnlen(vsocket->path, PATH_MAX);
205 vhost_set_ifname(vid, vsocket->path, size);
207 if (vsocket->dequeue_zero_copy)
208 vhost_enable_dequeue_zero_copy(vid);
210 RTE_LOG(INFO, VHOST_CONFIG, "new device, handle is %d\n", vid);
212 vsocket->connfd = fd;
213 conn->vsocket = vsocket;
215 ret = fdset_add(&vhost_user.fdset, fd, vhost_user_read_cb,
218 vsocket->connfd = -1;
221 RTE_LOG(ERR, VHOST_CONFIG,
222 "failed to add fd %d into vhost server fdset\n",
227 /* call back when there is new vhost-user connection from client */
229 vhost_user_server_new_connection(int fd, void *dat, int *remove __rte_unused)
231 struct vhost_user_socket *vsocket = dat;
233 fd = accept(fd, NULL, NULL);
237 RTE_LOG(INFO, VHOST_CONFIG, "new vhost user connection is %d\n", fd);
238 vhost_user_add_connection(fd, vsocket);
242 vhost_user_read_cb(int connfd, void *dat, int *remove)
244 struct vhost_user_connection *conn = dat;
245 struct vhost_user_socket *vsocket = conn->vsocket;
248 ret = vhost_user_msg_handler(conn->vid, connfd);
250 vsocket->connfd = -1;
254 vhost_destroy_device(conn->vid);
256 if (vsocket->reconnect)
257 vhost_user_create_client(vsocket);
262 create_unix_socket(const char *path, struct sockaddr_un *un, bool is_server)
266 fd = socket(AF_UNIX, SOCK_STREAM, 0);
269 RTE_LOG(INFO, VHOST_CONFIG, "vhost-user %s: socket created, fd: %d\n",
270 is_server ? "server" : "client", fd);
272 if (!is_server && fcntl(fd, F_SETFL, O_NONBLOCK)) {
273 RTE_LOG(ERR, VHOST_CONFIG,
274 "vhost-user: can't set nonblocking mode for socket, fd: "
275 "%d (%s)\n", fd, strerror(errno));
280 memset(un, 0, sizeof(*un));
281 un->sun_family = AF_UNIX;
282 strncpy(un->sun_path, path, sizeof(un->sun_path));
283 un->sun_path[sizeof(un->sun_path) - 1] = '\0';
289 vhost_user_create_server(struct vhost_user_socket *vsocket)
293 struct sockaddr_un un;
294 const char *path = vsocket->path;
296 fd = create_unix_socket(path, &un, vsocket->is_server);
300 ret = bind(fd, (struct sockaddr *)&un, sizeof(un));
302 RTE_LOG(ERR, VHOST_CONFIG,
303 "failed to bind to %s: %s; remove it and try again\n",
304 path, strerror(errno));
307 RTE_LOG(INFO, VHOST_CONFIG, "bind to %s\n", path);
309 ret = listen(fd, MAX_VIRTIO_BACKLOG);
313 vsocket->listenfd = fd;
314 ret = fdset_add(&vhost_user.fdset, fd, vhost_user_server_new_connection,
317 RTE_LOG(ERR, VHOST_CONFIG,
318 "failed to add listen fd %d to vhost server fdset\n",
330 struct vhost_user_reconnect {
331 struct sockaddr_un un;
333 struct vhost_user_socket *vsocket;
335 TAILQ_ENTRY(vhost_user_reconnect) next;
338 TAILQ_HEAD(vhost_user_reconnect_tailq_list, vhost_user_reconnect);
339 struct vhost_user_reconnect_list {
340 struct vhost_user_reconnect_tailq_list head;
341 pthread_mutex_t mutex;
344 static struct vhost_user_reconnect_list reconn_list;
345 static pthread_t reconn_tid;
348 vhost_user_connect_nonblock(int fd, struct sockaddr *un, size_t sz)
352 ret = connect(fd, un, sz);
353 if (ret < 0 && errno != EISCONN)
356 flags = fcntl(fd, F_GETFL, 0);
358 RTE_LOG(ERR, VHOST_CONFIG,
359 "can't get flags for connfd %d\n", fd);
362 if ((flags & O_NONBLOCK) && fcntl(fd, F_SETFL, flags & ~O_NONBLOCK)) {
363 RTE_LOG(ERR, VHOST_CONFIG,
364 "can't disable nonblocking on fd %d\n", fd);
371 vhost_user_client_reconnect(void *arg __rte_unused)
374 struct vhost_user_reconnect *reconn, *next;
377 pthread_mutex_lock(&reconn_list.mutex);
380 * An equal implementation of TAILQ_FOREACH_SAFE,
381 * which does not exist on all platforms.
383 for (reconn = TAILQ_FIRST(&reconn_list.head);
384 reconn != NULL; reconn = next) {
385 next = TAILQ_NEXT(reconn, next);
387 ret = vhost_user_connect_nonblock(reconn->fd,
388 (struct sockaddr *)&reconn->un,
392 RTE_LOG(ERR, VHOST_CONFIG,
393 "reconnection for fd %d failed\n",
400 RTE_LOG(INFO, VHOST_CONFIG,
401 "%s: connected\n", reconn->vsocket->path);
402 vhost_user_add_connection(reconn->fd, reconn->vsocket);
404 TAILQ_REMOVE(&reconn_list.head, reconn, next);
408 pthread_mutex_unlock(&reconn_list.mutex);
416 vhost_user_reconnect_init(void)
420 pthread_mutex_init(&reconn_list.mutex, NULL);
421 TAILQ_INIT(&reconn_list.head);
423 ret = pthread_create(&reconn_tid, NULL,
424 vhost_user_client_reconnect, NULL);
426 RTE_LOG(ERR, VHOST_CONFIG, "failed to create reconnect thread");
432 vhost_user_create_client(struct vhost_user_socket *vsocket)
436 struct sockaddr_un un;
437 const char *path = vsocket->path;
438 struct vhost_user_reconnect *reconn;
440 fd = create_unix_socket(path, &un, vsocket->is_server);
444 ret = vhost_user_connect_nonblock(fd, (struct sockaddr *)&un,
447 vhost_user_add_connection(fd, vsocket);
451 RTE_LOG(ERR, VHOST_CONFIG,
452 "failed to connect to %s: %s\n",
453 path, strerror(errno));
455 if (ret == -2 || !vsocket->reconnect) {
460 RTE_LOG(ERR, VHOST_CONFIG, "%s: reconnecting...\n", path);
461 reconn = malloc(sizeof(*reconn));
462 if (reconn == NULL) {
463 RTE_LOG(ERR, VHOST_CONFIG,
464 "failed to allocate memory for reconnect\n");
470 reconn->vsocket = vsocket;
471 pthread_mutex_lock(&reconn_list.mutex);
472 TAILQ_INSERT_TAIL(&reconn_list.head, reconn, next);
473 pthread_mutex_unlock(&reconn_list.mutex);
479 * Register a new vhost-user socket; here we could act as server
480 * (the default case), or client (when RTE_VHOST_USER_CLIENT) flag
484 rte_vhost_driver_register(const char *path, uint64_t flags)
487 struct vhost_user_socket *vsocket;
492 pthread_mutex_lock(&vhost_user.mutex);
494 if (vhost_user.vsocket_cnt == MAX_VHOST_SOCKET) {
495 RTE_LOG(ERR, VHOST_CONFIG,
496 "error: the number of vhost sockets reaches maximum\n");
500 vsocket = malloc(sizeof(struct vhost_user_socket));
503 memset(vsocket, 0, sizeof(struct vhost_user_socket));
504 vsocket->path = strdup(path);
505 vsocket->connfd = -1;
506 vsocket->dequeue_zero_copy = flags & RTE_VHOST_USER_DEQUEUE_ZERO_COPY;
508 if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
509 vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
510 if (vsocket->reconnect && reconn_tid == 0) {
511 if (vhost_user_reconnect_init() < 0) {
517 ret = vhost_user_create_client(vsocket);
519 vsocket->is_server = true;
520 ret = vhost_user_create_server(vsocket);
528 vhost_user.vsockets[vhost_user.vsocket_cnt++] = vsocket;
531 pthread_mutex_unlock(&vhost_user.mutex);
537 vhost_user_remove_reconnect(struct vhost_user_socket *vsocket)
540 struct vhost_user_reconnect *reconn, *next;
542 pthread_mutex_lock(&reconn_list.mutex);
544 for (reconn = TAILQ_FIRST(&reconn_list.head);
545 reconn != NULL; reconn = next) {
546 next = TAILQ_NEXT(reconn, next);
548 if (reconn->vsocket == vsocket) {
549 TAILQ_REMOVE(&reconn_list.head, reconn, next);
556 pthread_mutex_unlock(&reconn_list.mutex);
561 * Unregister the specified vhost socket
564 rte_vhost_driver_unregister(const char *path)
568 struct vhost_user_connection *conn;
570 pthread_mutex_lock(&vhost_user.mutex);
572 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
573 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
575 if (!strcmp(vsocket->path, path)) {
576 if (vsocket->is_server) {
577 fdset_del(&vhost_user.fdset, vsocket->listenfd);
578 close(vsocket->listenfd);
580 } else if (vsocket->reconnect) {
581 vhost_user_remove_reconnect(vsocket);
584 conn = fdset_del(&vhost_user.fdset, vsocket->connfd);
586 RTE_LOG(INFO, VHOST_CONFIG,
587 "free connfd = %d for device '%s'\n",
588 vsocket->connfd, path);
589 close(vsocket->connfd);
590 vhost_destroy_device(conn->vid);
597 count = --vhost_user.vsocket_cnt;
598 vhost_user.vsockets[i] = vhost_user.vsockets[count];
599 vhost_user.vsockets[count] = NULL;
600 pthread_mutex_unlock(&vhost_user.mutex);
605 pthread_mutex_unlock(&vhost_user.mutex);
611 rte_vhost_driver_session_start(void)
613 fdset_event_dispatch(&vhost_user.fdset);