4 * Copyright(c) 2010-2014 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>
50 #include <rte_virtio_net.h>
53 #include "vhost-net-user.h"
54 #include "vhost-net.h"
55 #include "virtio-net-user.h"
58 * Every time rte_vhost_driver_register() is invoked, an associated
59 * vhost_user_socket struct will be created.
61 struct vhost_user_socket {
69 struct vhost_user_connection {
70 struct vhost_user_socket *vsocket;
74 #define MAX_VHOST_SOCKET 1024
76 struct vhost_user_socket *vsockets[MAX_VHOST_SOCKET];
79 pthread_mutex_t mutex;
82 #define MAX_VIRTIO_BACKLOG 128
84 static void vhost_user_server_new_connection(int fd, void *data, int *remove);
85 static void vhost_user_msg_handler(int fd, void *dat, int *remove);
86 static int vhost_user_create_client(struct vhost_user_socket *vsocket);
88 static struct vhost_user vhost_user = {
90 .fd = { [0 ... MAX_FDS - 1] = {-1, NULL, NULL, NULL, 0} },
91 .fd_mutex = PTHREAD_MUTEX_INITIALIZER,
95 .mutex = PTHREAD_MUTEX_INITIALIZER,
98 static const char *vhost_message_str[VHOST_USER_MAX] = {
99 [VHOST_USER_NONE] = "VHOST_USER_NONE",
100 [VHOST_USER_GET_FEATURES] = "VHOST_USER_GET_FEATURES",
101 [VHOST_USER_SET_FEATURES] = "VHOST_USER_SET_FEATURES",
102 [VHOST_USER_SET_OWNER] = "VHOST_USER_SET_OWNER",
103 [VHOST_USER_RESET_OWNER] = "VHOST_USER_RESET_OWNER",
104 [VHOST_USER_SET_MEM_TABLE] = "VHOST_USER_SET_MEM_TABLE",
105 [VHOST_USER_SET_LOG_BASE] = "VHOST_USER_SET_LOG_BASE",
106 [VHOST_USER_SET_LOG_FD] = "VHOST_USER_SET_LOG_FD",
107 [VHOST_USER_SET_VRING_NUM] = "VHOST_USER_SET_VRING_NUM",
108 [VHOST_USER_SET_VRING_ADDR] = "VHOST_USER_SET_VRING_ADDR",
109 [VHOST_USER_SET_VRING_BASE] = "VHOST_USER_SET_VRING_BASE",
110 [VHOST_USER_GET_VRING_BASE] = "VHOST_USER_GET_VRING_BASE",
111 [VHOST_USER_SET_VRING_KICK] = "VHOST_USER_SET_VRING_KICK",
112 [VHOST_USER_SET_VRING_CALL] = "VHOST_USER_SET_VRING_CALL",
113 [VHOST_USER_SET_VRING_ERR] = "VHOST_USER_SET_VRING_ERR",
114 [VHOST_USER_GET_PROTOCOL_FEATURES] = "VHOST_USER_GET_PROTOCOL_FEATURES",
115 [VHOST_USER_SET_PROTOCOL_FEATURES] = "VHOST_USER_SET_PROTOCOL_FEATURES",
116 [VHOST_USER_GET_QUEUE_NUM] = "VHOST_USER_GET_QUEUE_NUM",
117 [VHOST_USER_SET_VRING_ENABLE] = "VHOST_USER_SET_VRING_ENABLE",
118 [VHOST_USER_SEND_RARP] = "VHOST_USER_SEND_RARP",
121 /* return bytes# of read on success or negative val on failure. */
123 read_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
127 size_t fdsize = fd_num * sizeof(int);
128 char control[CMSG_SPACE(fdsize)];
129 struct cmsghdr *cmsg;
132 memset(&msgh, 0, sizeof(msgh));
134 iov.iov_len = buflen;
138 msgh.msg_control = control;
139 msgh.msg_controllen = sizeof(control);
141 ret = recvmsg(sockfd, &msgh, 0);
143 RTE_LOG(ERR, VHOST_CONFIG, "recvmsg failed\n");
147 if (msgh.msg_flags & (MSG_TRUNC | MSG_CTRUNC)) {
148 RTE_LOG(ERR, VHOST_CONFIG, "truncted msg\n");
152 for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
153 cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
154 if ((cmsg->cmsg_level == SOL_SOCKET) &&
155 (cmsg->cmsg_type == SCM_RIGHTS)) {
156 memcpy(fds, CMSG_DATA(cmsg), fdsize);
164 /* return bytes# of read on success or negative val on failure. */
166 read_vhost_message(int sockfd, struct VhostUserMsg *msg)
170 ret = read_fd_message(sockfd, (char *)msg, VHOST_USER_HDR_SIZE,
171 msg->fds, VHOST_MEMORY_MAX_NREGIONS);
175 if (msg && msg->size) {
176 if (msg->size > sizeof(msg->payload)) {
177 RTE_LOG(ERR, VHOST_CONFIG,
178 "invalid msg size: %d\n", msg->size);
181 ret = read(sockfd, &msg->payload, msg->size);
184 if (ret != (int)msg->size) {
185 RTE_LOG(ERR, VHOST_CONFIG,
186 "read control message failed\n");
195 send_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
200 size_t fdsize = fd_num * sizeof(int);
201 char control[CMSG_SPACE(fdsize)];
202 struct cmsghdr *cmsg;
205 memset(&msgh, 0, sizeof(msgh));
207 iov.iov_len = buflen;
212 if (fds && fd_num > 0) {
213 msgh.msg_control = control;
214 msgh.msg_controllen = sizeof(control);
215 cmsg = CMSG_FIRSTHDR(&msgh);
216 cmsg->cmsg_len = CMSG_LEN(fdsize);
217 cmsg->cmsg_level = SOL_SOCKET;
218 cmsg->cmsg_type = SCM_RIGHTS;
219 memcpy(CMSG_DATA(cmsg), fds, fdsize);
221 msgh.msg_control = NULL;
222 msgh.msg_controllen = 0;
226 ret = sendmsg(sockfd, &msgh, 0);
227 } while (ret < 0 && errno == EINTR);
230 RTE_LOG(ERR, VHOST_CONFIG, "sendmsg error\n");
238 send_vhost_message(int sockfd, struct VhostUserMsg *msg)
245 msg->flags &= ~VHOST_USER_VERSION_MASK;
246 msg->flags |= VHOST_USER_VERSION;
247 msg->flags |= VHOST_USER_REPLY_MASK;
249 ret = send_fd_message(sockfd, (char *)msg,
250 VHOST_USER_HDR_SIZE + msg->size, NULL, 0);
257 vhost_user_add_connection(int fd, struct vhost_user_socket *vsocket)
261 struct vhost_user_connection *conn;
264 conn = malloc(sizeof(*conn));
270 vid = vhost_new_device();
277 size = strnlen(vsocket->path, PATH_MAX);
278 vhost_set_ifname(vid, vsocket->path, size);
280 RTE_LOG(INFO, VHOST_CONFIG, "new device, handle is %d\n", vid);
282 vsocket->connfd = fd;
283 conn->vsocket = vsocket;
285 ret = fdset_add(&vhost_user.fdset, fd, vhost_user_msg_handler,
288 vsocket->connfd = -1;
291 RTE_LOG(ERR, VHOST_CONFIG,
292 "failed to add fd %d into vhost server fdset\n",
297 /* call back when there is new vhost-user connection from client */
299 vhost_user_server_new_connection(int fd, void *dat, int *remove __rte_unused)
301 struct vhost_user_socket *vsocket = dat;
303 fd = accept(fd, NULL, NULL);
307 RTE_LOG(INFO, VHOST_CONFIG, "new vhost user connection is %d\n", fd);
308 vhost_user_add_connection(fd, vsocket);
311 /* callback when there is message on the connfd */
313 vhost_user_msg_handler(int connfd, void *dat, int *remove)
316 struct vhost_user_connection *conn = dat;
317 struct VhostUserMsg msg;
322 ret = read_vhost_message(connfd, &msg);
323 if (ret <= 0 || msg.request >= VHOST_USER_MAX) {
324 struct vhost_user_socket *vsocket = conn->vsocket;
327 RTE_LOG(ERR, VHOST_CONFIG,
328 "vhost read message failed\n");
330 RTE_LOG(INFO, VHOST_CONFIG,
331 "vhost peer closed\n");
333 RTE_LOG(ERR, VHOST_CONFIG,
334 "vhost read incorrect message\n");
336 vsocket->connfd = -1;
340 vhost_destroy_device(vid);
342 if (vsocket->reconnect)
343 vhost_user_create_client(vsocket);
348 RTE_LOG(INFO, VHOST_CONFIG, "read message %s\n",
349 vhost_message_str[msg.request]);
350 switch (msg.request) {
351 case VHOST_USER_GET_FEATURES:
352 ret = vhost_get_features(vid, &features);
353 msg.payload.u64 = features;
354 msg.size = sizeof(msg.payload.u64);
355 send_vhost_message(connfd, &msg);
357 case VHOST_USER_SET_FEATURES:
358 features = msg.payload.u64;
359 vhost_set_features(vid, &features);
362 case VHOST_USER_GET_PROTOCOL_FEATURES:
363 msg.payload.u64 = VHOST_USER_PROTOCOL_FEATURES;
364 msg.size = sizeof(msg.payload.u64);
365 send_vhost_message(connfd, &msg);
367 case VHOST_USER_SET_PROTOCOL_FEATURES:
368 user_set_protocol_features(vid, msg.payload.u64);
371 case VHOST_USER_SET_OWNER:
372 vhost_set_owner(vid);
374 case VHOST_USER_RESET_OWNER:
375 vhost_reset_owner(vid);
378 case VHOST_USER_SET_MEM_TABLE:
379 user_set_mem_table(vid, &msg);
382 case VHOST_USER_SET_LOG_BASE:
383 user_set_log_base(vid, &msg);
385 /* it needs a reply */
386 msg.size = sizeof(msg.payload.u64);
387 send_vhost_message(connfd, &msg);
389 case VHOST_USER_SET_LOG_FD:
391 RTE_LOG(INFO, VHOST_CONFIG, "not implemented.\n");
394 case VHOST_USER_SET_VRING_NUM:
395 vhost_set_vring_num(vid, &msg.payload.state);
397 case VHOST_USER_SET_VRING_ADDR:
398 vhost_set_vring_addr(vid, &msg.payload.addr);
400 case VHOST_USER_SET_VRING_BASE:
401 vhost_set_vring_base(vid, &msg.payload.state);
404 case VHOST_USER_GET_VRING_BASE:
405 ret = user_get_vring_base(vid, &msg.payload.state);
406 msg.size = sizeof(msg.payload.state);
407 send_vhost_message(connfd, &msg);
410 case VHOST_USER_SET_VRING_KICK:
411 user_set_vring_kick(vid, &msg);
413 case VHOST_USER_SET_VRING_CALL:
414 user_set_vring_call(vid, &msg);
417 case VHOST_USER_SET_VRING_ERR:
418 if (!(msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK))
420 RTE_LOG(INFO, VHOST_CONFIG, "not implemented\n");
423 case VHOST_USER_GET_QUEUE_NUM:
424 msg.payload.u64 = VHOST_MAX_QUEUE_PAIRS;
425 msg.size = sizeof(msg.payload.u64);
426 send_vhost_message(connfd, &msg);
429 case VHOST_USER_SET_VRING_ENABLE:
430 user_set_vring_enable(vid, &msg.payload.state);
432 case VHOST_USER_SEND_RARP:
433 user_send_rarp(vid, &msg);
443 create_unix_socket(const char *path, struct sockaddr_un *un, bool is_server)
447 fd = socket(AF_UNIX, SOCK_STREAM, 0);
450 RTE_LOG(INFO, VHOST_CONFIG, "vhost-user %s: socket created, fd: %d\n",
451 is_server ? "server" : "client", fd);
453 if (!is_server && fcntl(fd, F_SETFL, O_NONBLOCK)) {
454 RTE_LOG(ERR, VHOST_CONFIG,
455 "vhost-user: can't set nonblocking mode for socket, fd: "
456 "%d (%s)\n", fd, strerror(errno));
461 memset(un, 0, sizeof(*un));
462 un->sun_family = AF_UNIX;
463 strncpy(un->sun_path, path, sizeof(un->sun_path));
464 un->sun_path[sizeof(un->sun_path) - 1] = '\0';
470 vhost_user_create_server(struct vhost_user_socket *vsocket)
474 struct sockaddr_un un;
475 const char *path = vsocket->path;
477 fd = create_unix_socket(path, &un, vsocket->is_server);
481 ret = bind(fd, (struct sockaddr *)&un, sizeof(un));
483 RTE_LOG(ERR, VHOST_CONFIG,
484 "failed to bind to %s: %s; remove it and try again\n",
485 path, strerror(errno));
488 RTE_LOG(INFO, VHOST_CONFIG, "bind to %s\n", path);
490 ret = listen(fd, MAX_VIRTIO_BACKLOG);
494 vsocket->listenfd = fd;
495 ret = fdset_add(&vhost_user.fdset, fd, vhost_user_server_new_connection,
498 RTE_LOG(ERR, VHOST_CONFIG,
499 "failed to add listen fd %d to vhost server fdset\n",
511 struct vhost_user_reconnect {
512 struct sockaddr_un un;
514 struct vhost_user_socket *vsocket;
516 TAILQ_ENTRY(vhost_user_reconnect) next;
519 TAILQ_HEAD(vhost_user_reconnect_tailq_list, vhost_user_reconnect);
520 struct vhost_user_reconnect_list {
521 struct vhost_user_reconnect_tailq_list head;
522 pthread_mutex_t mutex;
525 static struct vhost_user_reconnect_list reconn_list;
526 static pthread_t reconn_tid;
529 vhost_user_connect_nonblock(int fd, struct sockaddr *un, size_t sz)
533 ret = connect(fd, un, sz);
534 if (ret < 0 && errno != EISCONN)
537 flags = fcntl(fd, F_GETFL, 0);
539 RTE_LOG(ERR, VHOST_CONFIG,
540 "can't get flags for connfd %d\n", fd);
543 if ((flags & O_NONBLOCK) && fcntl(fd, F_SETFL, flags & ~O_NONBLOCK)) {
544 RTE_LOG(ERR, VHOST_CONFIG,
545 "can't disable nonblocking on fd %d\n", fd);
552 vhost_user_client_reconnect(void *arg __rte_unused)
555 struct vhost_user_reconnect *reconn, *next;
558 pthread_mutex_lock(&reconn_list.mutex);
561 * An equal implementation of TAILQ_FOREACH_SAFE,
562 * which does not exist on all platforms.
564 for (reconn = TAILQ_FIRST(&reconn_list.head);
565 reconn != NULL; reconn = next) {
566 next = TAILQ_NEXT(reconn, next);
568 ret = vhost_user_connect_nonblock(reconn->fd,
569 (struct sockaddr *)&reconn->un,
573 RTE_LOG(ERR, VHOST_CONFIG,
574 "reconnection for fd %d failed\n",
581 RTE_LOG(INFO, VHOST_CONFIG,
582 "%s: connected\n", reconn->vsocket->path);
583 vhost_user_add_connection(reconn->fd, reconn->vsocket);
585 TAILQ_REMOVE(&reconn_list.head, reconn, next);
589 pthread_mutex_unlock(&reconn_list.mutex);
597 vhost_user_reconnect_init(void)
601 pthread_mutex_init(&reconn_list.mutex, NULL);
602 TAILQ_INIT(&reconn_list.head);
604 ret = pthread_create(&reconn_tid, NULL,
605 vhost_user_client_reconnect, NULL);
607 RTE_LOG(ERR, VHOST_CONFIG, "failed to create reconnect thread");
613 vhost_user_create_client(struct vhost_user_socket *vsocket)
617 struct sockaddr_un un;
618 const char *path = vsocket->path;
619 struct vhost_user_reconnect *reconn;
621 fd = create_unix_socket(path, &un, vsocket->is_server);
625 ret = vhost_user_connect_nonblock(fd, (struct sockaddr *)&un,
628 vhost_user_add_connection(fd, vsocket);
632 RTE_LOG(ERR, VHOST_CONFIG,
633 "failed to connect to %s: %s\n",
634 path, strerror(errno));
636 if (ret == -2 || !vsocket->reconnect) {
641 RTE_LOG(ERR, VHOST_CONFIG, "%s: reconnecting...\n", path);
642 reconn = malloc(sizeof(*reconn));
643 if (reconn == NULL) {
644 RTE_LOG(ERR, VHOST_CONFIG,
645 "failed to allocate memory for reconnect\n");
651 reconn->vsocket = vsocket;
652 pthread_mutex_lock(&reconn_list.mutex);
653 TAILQ_INSERT_TAIL(&reconn_list.head, reconn, next);
654 pthread_mutex_unlock(&reconn_list.mutex);
660 * Register a new vhost-user socket; here we could act as server
661 * (the default case), or client (when RTE_VHOST_USER_CLIENT) flag
665 rte_vhost_driver_register(const char *path, uint64_t flags)
668 struct vhost_user_socket *vsocket;
673 pthread_mutex_lock(&vhost_user.mutex);
675 if (vhost_user.vsocket_cnt == MAX_VHOST_SOCKET) {
676 RTE_LOG(ERR, VHOST_CONFIG,
677 "error: the number of vhost sockets reaches maximum\n");
681 vsocket = malloc(sizeof(struct vhost_user_socket));
684 memset(vsocket, 0, sizeof(struct vhost_user_socket));
685 vsocket->path = strdup(path);
686 vsocket->connfd = -1;
688 if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
689 vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
690 if (vsocket->reconnect && reconn_tid == 0) {
691 if (vhost_user_reconnect_init() < 0) {
697 ret = vhost_user_create_client(vsocket);
699 vsocket->is_server = true;
700 ret = vhost_user_create_server(vsocket);
708 vhost_user.vsockets[vhost_user.vsocket_cnt++] = vsocket;
711 pthread_mutex_unlock(&vhost_user.mutex);
717 vhost_user_remove_reconnect(struct vhost_user_socket *vsocket)
720 struct vhost_user_reconnect *reconn, *next;
722 pthread_mutex_lock(&reconn_list.mutex);
724 for (reconn = TAILQ_FIRST(&reconn_list.head);
725 reconn != NULL; reconn = next) {
726 next = TAILQ_NEXT(reconn, next);
728 if (reconn->vsocket == vsocket) {
729 TAILQ_REMOVE(&reconn_list.head, reconn, next);
736 pthread_mutex_unlock(&reconn_list.mutex);
741 * Unregister the specified vhost socket
744 rte_vhost_driver_unregister(const char *path)
748 struct vhost_user_connection *conn;
750 pthread_mutex_lock(&vhost_user.mutex);
752 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
753 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
755 if (!strcmp(vsocket->path, path)) {
756 if (vsocket->is_server) {
757 fdset_del(&vhost_user.fdset, vsocket->listenfd);
758 close(vsocket->listenfd);
760 } else if (vsocket->reconnect) {
761 vhost_user_remove_reconnect(vsocket);
764 conn = fdset_del(&vhost_user.fdset, vsocket->connfd);
766 RTE_LOG(INFO, VHOST_CONFIG,
767 "free connfd = %d for device '%s'\n",
768 vsocket->connfd, path);
769 close(vsocket->connfd);
770 vhost_destroy_device(conn->vid);
777 count = --vhost_user.vsocket_cnt;
778 vhost_user.vsockets[i] = vhost_user.vsockets[count];
779 vhost_user.vsockets[count] = NULL;
780 pthread_mutex_unlock(&vhost_user.mutex);
785 pthread_mutex_unlock(&vhost_user.mutex);
791 rte_vhost_driver_session_start(void)
793 fdset_event_dispatch(&vhost_user.fdset);