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 {
67 struct vhost_user_connection {
68 struct vhost_user_socket *vsocket;
72 #define MAX_VHOST_SOCKET 1024
74 struct vhost_user_socket *vsockets[MAX_VHOST_SOCKET];
77 pthread_mutex_t mutex;
80 #define MAX_VIRTIO_BACKLOG 128
82 static void vhost_user_server_new_connection(int fd, void *data, int *remove);
83 static void vhost_user_read_cb(int fd, void *dat, int *remove);
84 static int vhost_user_create_client(struct vhost_user_socket *vsocket);
86 static struct vhost_user vhost_user = {
88 .fd = { [0 ... MAX_FDS - 1] = {-1, NULL, NULL, NULL, 0} },
89 .fd_mutex = PTHREAD_MUTEX_INITIALIZER,
93 .mutex = PTHREAD_MUTEX_INITIALIZER,
96 /* return bytes# of read on success or negative val on failure. */
98 read_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
102 size_t fdsize = fd_num * sizeof(int);
103 char control[CMSG_SPACE(fdsize)];
104 struct cmsghdr *cmsg;
107 memset(&msgh, 0, sizeof(msgh));
109 iov.iov_len = buflen;
113 msgh.msg_control = control;
114 msgh.msg_controllen = sizeof(control);
116 ret = recvmsg(sockfd, &msgh, 0);
118 RTE_LOG(ERR, VHOST_CONFIG, "recvmsg failed\n");
122 if (msgh.msg_flags & (MSG_TRUNC | MSG_CTRUNC)) {
123 RTE_LOG(ERR, VHOST_CONFIG, "truncted msg\n");
127 for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
128 cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
129 if ((cmsg->cmsg_level == SOL_SOCKET) &&
130 (cmsg->cmsg_type == SCM_RIGHTS)) {
131 memcpy(fds, CMSG_DATA(cmsg), fdsize);
140 send_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
145 size_t fdsize = fd_num * sizeof(int);
146 char control[CMSG_SPACE(fdsize)];
147 struct cmsghdr *cmsg;
150 memset(&msgh, 0, sizeof(msgh));
152 iov.iov_len = buflen;
157 if (fds && fd_num > 0) {
158 msgh.msg_control = control;
159 msgh.msg_controllen = sizeof(control);
160 cmsg = CMSG_FIRSTHDR(&msgh);
161 cmsg->cmsg_len = CMSG_LEN(fdsize);
162 cmsg->cmsg_level = SOL_SOCKET;
163 cmsg->cmsg_type = SCM_RIGHTS;
164 memcpy(CMSG_DATA(cmsg), fds, fdsize);
166 msgh.msg_control = NULL;
167 msgh.msg_controllen = 0;
171 ret = sendmsg(sockfd, &msgh, 0);
172 } while (ret < 0 && errno == EINTR);
175 RTE_LOG(ERR, VHOST_CONFIG, "sendmsg error\n");
183 vhost_user_add_connection(int fd, struct vhost_user_socket *vsocket)
187 struct vhost_user_connection *conn;
190 conn = malloc(sizeof(*conn));
196 vid = vhost_new_device();
203 size = strnlen(vsocket->path, PATH_MAX);
204 vhost_set_ifname(vid, vsocket->path, size);
206 RTE_LOG(INFO, VHOST_CONFIG, "new device, handle is %d\n", vid);
208 vsocket->connfd = fd;
209 conn->vsocket = vsocket;
211 ret = fdset_add(&vhost_user.fdset, fd, vhost_user_read_cb,
214 vsocket->connfd = -1;
217 RTE_LOG(ERR, VHOST_CONFIG,
218 "failed to add fd %d into vhost server fdset\n",
223 /* call back when there is new vhost-user connection from client */
225 vhost_user_server_new_connection(int fd, void *dat, int *remove __rte_unused)
227 struct vhost_user_socket *vsocket = dat;
229 fd = accept(fd, NULL, NULL);
233 RTE_LOG(INFO, VHOST_CONFIG, "new vhost user connection is %d\n", fd);
234 vhost_user_add_connection(fd, vsocket);
238 vhost_user_read_cb(int connfd, void *dat, int *remove)
240 struct vhost_user_connection *conn = dat;
241 struct vhost_user_socket *vsocket = conn->vsocket;
244 ret = vhost_user_msg_handler(conn->vid, connfd);
246 vsocket->connfd = -1;
250 vhost_destroy_device(conn->vid);
252 if (vsocket->reconnect)
253 vhost_user_create_client(vsocket);
258 create_unix_socket(const char *path, struct sockaddr_un *un, bool is_server)
262 fd = socket(AF_UNIX, SOCK_STREAM, 0);
265 RTE_LOG(INFO, VHOST_CONFIG, "vhost-user %s: socket created, fd: %d\n",
266 is_server ? "server" : "client", fd);
268 if (!is_server && fcntl(fd, F_SETFL, O_NONBLOCK)) {
269 RTE_LOG(ERR, VHOST_CONFIG,
270 "vhost-user: can't set nonblocking mode for socket, fd: "
271 "%d (%s)\n", fd, strerror(errno));
276 memset(un, 0, sizeof(*un));
277 un->sun_family = AF_UNIX;
278 strncpy(un->sun_path, path, sizeof(un->sun_path));
279 un->sun_path[sizeof(un->sun_path) - 1] = '\0';
285 vhost_user_create_server(struct vhost_user_socket *vsocket)
289 struct sockaddr_un un;
290 const char *path = vsocket->path;
292 fd = create_unix_socket(path, &un, vsocket->is_server);
296 ret = bind(fd, (struct sockaddr *)&un, sizeof(un));
298 RTE_LOG(ERR, VHOST_CONFIG,
299 "failed to bind to %s: %s; remove it and try again\n",
300 path, strerror(errno));
303 RTE_LOG(INFO, VHOST_CONFIG, "bind to %s\n", path);
305 ret = listen(fd, MAX_VIRTIO_BACKLOG);
309 vsocket->listenfd = fd;
310 ret = fdset_add(&vhost_user.fdset, fd, vhost_user_server_new_connection,
313 RTE_LOG(ERR, VHOST_CONFIG,
314 "failed to add listen fd %d to vhost server fdset\n",
326 struct vhost_user_reconnect {
327 struct sockaddr_un un;
329 struct vhost_user_socket *vsocket;
331 TAILQ_ENTRY(vhost_user_reconnect) next;
334 TAILQ_HEAD(vhost_user_reconnect_tailq_list, vhost_user_reconnect);
335 struct vhost_user_reconnect_list {
336 struct vhost_user_reconnect_tailq_list head;
337 pthread_mutex_t mutex;
340 static struct vhost_user_reconnect_list reconn_list;
341 static pthread_t reconn_tid;
344 vhost_user_connect_nonblock(int fd, struct sockaddr *un, size_t sz)
348 ret = connect(fd, un, sz);
349 if (ret < 0 && errno != EISCONN)
352 flags = fcntl(fd, F_GETFL, 0);
354 RTE_LOG(ERR, VHOST_CONFIG,
355 "can't get flags for connfd %d\n", fd);
358 if ((flags & O_NONBLOCK) && fcntl(fd, F_SETFL, flags & ~O_NONBLOCK)) {
359 RTE_LOG(ERR, VHOST_CONFIG,
360 "can't disable nonblocking on fd %d\n", fd);
367 vhost_user_client_reconnect(void *arg __rte_unused)
370 struct vhost_user_reconnect *reconn, *next;
373 pthread_mutex_lock(&reconn_list.mutex);
376 * An equal implementation of TAILQ_FOREACH_SAFE,
377 * which does not exist on all platforms.
379 for (reconn = TAILQ_FIRST(&reconn_list.head);
380 reconn != NULL; reconn = next) {
381 next = TAILQ_NEXT(reconn, next);
383 ret = vhost_user_connect_nonblock(reconn->fd,
384 (struct sockaddr *)&reconn->un,
388 RTE_LOG(ERR, VHOST_CONFIG,
389 "reconnection for fd %d failed\n",
396 RTE_LOG(INFO, VHOST_CONFIG,
397 "%s: connected\n", reconn->vsocket->path);
398 vhost_user_add_connection(reconn->fd, reconn->vsocket);
400 TAILQ_REMOVE(&reconn_list.head, reconn, next);
404 pthread_mutex_unlock(&reconn_list.mutex);
412 vhost_user_reconnect_init(void)
416 pthread_mutex_init(&reconn_list.mutex, NULL);
417 TAILQ_INIT(&reconn_list.head);
419 ret = pthread_create(&reconn_tid, NULL,
420 vhost_user_client_reconnect, NULL);
422 RTE_LOG(ERR, VHOST_CONFIG, "failed to create reconnect thread");
428 vhost_user_create_client(struct vhost_user_socket *vsocket)
432 struct sockaddr_un un;
433 const char *path = vsocket->path;
434 struct vhost_user_reconnect *reconn;
436 fd = create_unix_socket(path, &un, vsocket->is_server);
440 ret = vhost_user_connect_nonblock(fd, (struct sockaddr *)&un,
443 vhost_user_add_connection(fd, vsocket);
447 RTE_LOG(ERR, VHOST_CONFIG,
448 "failed to connect to %s: %s\n",
449 path, strerror(errno));
451 if (ret == -2 || !vsocket->reconnect) {
456 RTE_LOG(ERR, VHOST_CONFIG, "%s: reconnecting...\n", path);
457 reconn = malloc(sizeof(*reconn));
458 if (reconn == NULL) {
459 RTE_LOG(ERR, VHOST_CONFIG,
460 "failed to allocate memory for reconnect\n");
466 reconn->vsocket = vsocket;
467 pthread_mutex_lock(&reconn_list.mutex);
468 TAILQ_INSERT_TAIL(&reconn_list.head, reconn, next);
469 pthread_mutex_unlock(&reconn_list.mutex);
475 * Register a new vhost-user socket; here we could act as server
476 * (the default case), or client (when RTE_VHOST_USER_CLIENT) flag
480 rte_vhost_driver_register(const char *path, uint64_t flags)
483 struct vhost_user_socket *vsocket;
488 pthread_mutex_lock(&vhost_user.mutex);
490 if (vhost_user.vsocket_cnt == MAX_VHOST_SOCKET) {
491 RTE_LOG(ERR, VHOST_CONFIG,
492 "error: the number of vhost sockets reaches maximum\n");
496 vsocket = malloc(sizeof(struct vhost_user_socket));
499 memset(vsocket, 0, sizeof(struct vhost_user_socket));
500 vsocket->path = strdup(path);
501 vsocket->connfd = -1;
503 if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
504 vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
505 if (vsocket->reconnect && reconn_tid == 0) {
506 if (vhost_user_reconnect_init() < 0) {
512 ret = vhost_user_create_client(vsocket);
514 vsocket->is_server = true;
515 ret = vhost_user_create_server(vsocket);
523 vhost_user.vsockets[vhost_user.vsocket_cnt++] = vsocket;
526 pthread_mutex_unlock(&vhost_user.mutex);
532 vhost_user_remove_reconnect(struct vhost_user_socket *vsocket)
535 struct vhost_user_reconnect *reconn, *next;
537 pthread_mutex_lock(&reconn_list.mutex);
539 for (reconn = TAILQ_FIRST(&reconn_list.head);
540 reconn != NULL; reconn = next) {
541 next = TAILQ_NEXT(reconn, next);
543 if (reconn->vsocket == vsocket) {
544 TAILQ_REMOVE(&reconn_list.head, reconn, next);
551 pthread_mutex_unlock(&reconn_list.mutex);
556 * Unregister the specified vhost socket
559 rte_vhost_driver_unregister(const char *path)
563 struct vhost_user_connection *conn;
565 pthread_mutex_lock(&vhost_user.mutex);
567 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
568 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
570 if (!strcmp(vsocket->path, path)) {
571 if (vsocket->is_server) {
572 fdset_del(&vhost_user.fdset, vsocket->listenfd);
573 close(vsocket->listenfd);
575 } else if (vsocket->reconnect) {
576 vhost_user_remove_reconnect(vsocket);
579 conn = fdset_del(&vhost_user.fdset, vsocket->connfd);
581 RTE_LOG(INFO, VHOST_CONFIG,
582 "free connfd = %d for device '%s'\n",
583 vsocket->connfd, path);
584 close(vsocket->connfd);
585 vhost_destroy_device(conn->vid);
592 count = --vhost_user.vsocket_cnt;
593 vhost_user.vsockets[i] = vhost_user.vsockets[count];
594 vhost_user.vsockets[count] = NULL;
595 pthread_mutex_unlock(&vhost_user.mutex);
600 pthread_mutex_unlock(&vhost_user.mutex);
606 rte_vhost_driver_session_start(void)
608 fdset_event_dispatch(&vhost_user.fdset);