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>
49 #include <rte_virtio_net.h>
52 #include "vhost-net-user.h"
53 #include "vhost-net.h"
54 #include "virtio-net-user.h"
57 * Every time rte_vhost_driver_register() is invoked, an associated
58 * vhost_user_socket struct will be created.
60 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_msg_handler(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 static const char *vhost_message_str[VHOST_USER_MAX] = {
97 [VHOST_USER_NONE] = "VHOST_USER_NONE",
98 [VHOST_USER_GET_FEATURES] = "VHOST_USER_GET_FEATURES",
99 [VHOST_USER_SET_FEATURES] = "VHOST_USER_SET_FEATURES",
100 [VHOST_USER_SET_OWNER] = "VHOST_USER_SET_OWNER",
101 [VHOST_USER_RESET_OWNER] = "VHOST_USER_RESET_OWNER",
102 [VHOST_USER_SET_MEM_TABLE] = "VHOST_USER_SET_MEM_TABLE",
103 [VHOST_USER_SET_LOG_BASE] = "VHOST_USER_SET_LOG_BASE",
104 [VHOST_USER_SET_LOG_FD] = "VHOST_USER_SET_LOG_FD",
105 [VHOST_USER_SET_VRING_NUM] = "VHOST_USER_SET_VRING_NUM",
106 [VHOST_USER_SET_VRING_ADDR] = "VHOST_USER_SET_VRING_ADDR",
107 [VHOST_USER_SET_VRING_BASE] = "VHOST_USER_SET_VRING_BASE",
108 [VHOST_USER_GET_VRING_BASE] = "VHOST_USER_GET_VRING_BASE",
109 [VHOST_USER_SET_VRING_KICK] = "VHOST_USER_SET_VRING_KICK",
110 [VHOST_USER_SET_VRING_CALL] = "VHOST_USER_SET_VRING_CALL",
111 [VHOST_USER_SET_VRING_ERR] = "VHOST_USER_SET_VRING_ERR",
112 [VHOST_USER_GET_PROTOCOL_FEATURES] = "VHOST_USER_GET_PROTOCOL_FEATURES",
113 [VHOST_USER_SET_PROTOCOL_FEATURES] = "VHOST_USER_SET_PROTOCOL_FEATURES",
114 [VHOST_USER_GET_QUEUE_NUM] = "VHOST_USER_GET_QUEUE_NUM",
115 [VHOST_USER_SET_VRING_ENABLE] = "VHOST_USER_SET_VRING_ENABLE",
116 [VHOST_USER_SEND_RARP] = "VHOST_USER_SEND_RARP",
119 /* return bytes# of read on success or negative val on failure. */
121 read_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
125 size_t fdsize = fd_num * sizeof(int);
126 char control[CMSG_SPACE(fdsize)];
127 struct cmsghdr *cmsg;
130 memset(&msgh, 0, sizeof(msgh));
132 iov.iov_len = buflen;
136 msgh.msg_control = control;
137 msgh.msg_controllen = sizeof(control);
139 ret = recvmsg(sockfd, &msgh, 0);
141 RTE_LOG(ERR, VHOST_CONFIG, "recvmsg failed\n");
145 if (msgh.msg_flags & (MSG_TRUNC | MSG_CTRUNC)) {
146 RTE_LOG(ERR, VHOST_CONFIG, "truncted msg\n");
150 for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
151 cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
152 if ((cmsg->cmsg_level == SOL_SOCKET) &&
153 (cmsg->cmsg_type == SCM_RIGHTS)) {
154 memcpy(fds, CMSG_DATA(cmsg), fdsize);
162 /* return bytes# of read on success or negative val on failure. */
164 read_vhost_message(int sockfd, struct VhostUserMsg *msg)
168 ret = read_fd_message(sockfd, (char *)msg, VHOST_USER_HDR_SIZE,
169 msg->fds, VHOST_MEMORY_MAX_NREGIONS);
173 if (msg && msg->size) {
174 if (msg->size > sizeof(msg->payload)) {
175 RTE_LOG(ERR, VHOST_CONFIG,
176 "invalid msg size: %d\n", msg->size);
179 ret = read(sockfd, &msg->payload, msg->size);
182 if (ret != (int)msg->size) {
183 RTE_LOG(ERR, VHOST_CONFIG,
184 "read control message failed\n");
193 send_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
198 size_t fdsize = fd_num * sizeof(int);
199 char control[CMSG_SPACE(fdsize)];
200 struct cmsghdr *cmsg;
203 memset(&msgh, 0, sizeof(msgh));
205 iov.iov_len = buflen;
210 if (fds && fd_num > 0) {
211 msgh.msg_control = control;
212 msgh.msg_controllen = sizeof(control);
213 cmsg = CMSG_FIRSTHDR(&msgh);
214 cmsg->cmsg_len = CMSG_LEN(fdsize);
215 cmsg->cmsg_level = SOL_SOCKET;
216 cmsg->cmsg_type = SCM_RIGHTS;
217 memcpy(CMSG_DATA(cmsg), fds, fdsize);
219 msgh.msg_control = NULL;
220 msgh.msg_controllen = 0;
224 ret = sendmsg(sockfd, &msgh, 0);
225 } while (ret < 0 && errno == EINTR);
228 RTE_LOG(ERR, VHOST_CONFIG, "sendmsg error\n");
236 send_vhost_message(int sockfd, struct VhostUserMsg *msg)
243 msg->flags &= ~VHOST_USER_VERSION_MASK;
244 msg->flags |= VHOST_USER_VERSION;
245 msg->flags |= VHOST_USER_REPLY_MASK;
247 ret = send_fd_message(sockfd, (char *)msg,
248 VHOST_USER_HDR_SIZE + msg->size, NULL, 0);
255 vhost_user_add_connection(int fd, struct vhost_user_socket *vsocket)
259 struct vhost_user_connection *conn;
262 conn = malloc(sizeof(*conn));
268 vid = vhost_new_device();
275 size = strnlen(vsocket->path, PATH_MAX);
276 vhost_set_ifname(vid, vsocket->path, size);
278 RTE_LOG(INFO, VHOST_CONFIG, "new device, handle is %d\n", vid);
280 conn->vsocket = vsocket;
282 ret = fdset_add(&vhost_user.fdset, fd, vhost_user_msg_handler,
287 RTE_LOG(ERR, VHOST_CONFIG,
288 "failed to add fd %d into vhost server fdset\n",
293 /* call back when there is new vhost-user connection from client */
295 vhost_user_server_new_connection(int fd, void *dat, int *remove __rte_unused)
297 struct vhost_user_socket *vsocket = dat;
299 fd = accept(fd, NULL, NULL);
303 RTE_LOG(INFO, VHOST_CONFIG, "new vhost user connection is %d\n", fd);
304 vhost_user_add_connection(fd, vsocket);
307 /* callback when there is message on the connfd */
309 vhost_user_msg_handler(int connfd, void *dat, int *remove)
312 struct vhost_user_connection *conn = dat;
313 struct VhostUserMsg msg;
318 ret = read_vhost_message(connfd, &msg);
319 if (ret <= 0 || msg.request >= VHOST_USER_MAX) {
320 struct vhost_user_socket *vsocket = conn->vsocket;
323 RTE_LOG(ERR, VHOST_CONFIG,
324 "vhost read message failed\n");
326 RTE_LOG(INFO, VHOST_CONFIG,
327 "vhost peer closed\n");
329 RTE_LOG(ERR, VHOST_CONFIG,
330 "vhost read incorrect message\n");
335 vhost_destroy_device(vid);
337 if (vsocket->reconnect)
338 vhost_user_create_client(vsocket);
343 RTE_LOG(INFO, VHOST_CONFIG, "read message %s\n",
344 vhost_message_str[msg.request]);
345 switch (msg.request) {
346 case VHOST_USER_GET_FEATURES:
347 ret = vhost_get_features(vid, &features);
348 msg.payload.u64 = features;
349 msg.size = sizeof(msg.payload.u64);
350 send_vhost_message(connfd, &msg);
352 case VHOST_USER_SET_FEATURES:
353 features = msg.payload.u64;
354 vhost_set_features(vid, &features);
357 case VHOST_USER_GET_PROTOCOL_FEATURES:
358 msg.payload.u64 = VHOST_USER_PROTOCOL_FEATURES;
359 msg.size = sizeof(msg.payload.u64);
360 send_vhost_message(connfd, &msg);
362 case VHOST_USER_SET_PROTOCOL_FEATURES:
363 user_set_protocol_features(vid, msg.payload.u64);
366 case VHOST_USER_SET_OWNER:
367 vhost_set_owner(vid);
369 case VHOST_USER_RESET_OWNER:
370 vhost_reset_owner(vid);
373 case VHOST_USER_SET_MEM_TABLE:
374 user_set_mem_table(vid, &msg);
377 case VHOST_USER_SET_LOG_BASE:
378 user_set_log_base(vid, &msg);
380 /* it needs a reply */
381 msg.size = sizeof(msg.payload.u64);
382 send_vhost_message(connfd, &msg);
384 case VHOST_USER_SET_LOG_FD:
386 RTE_LOG(INFO, VHOST_CONFIG, "not implemented.\n");
389 case VHOST_USER_SET_VRING_NUM:
390 vhost_set_vring_num(vid, &msg.payload.state);
392 case VHOST_USER_SET_VRING_ADDR:
393 vhost_set_vring_addr(vid, &msg.payload.addr);
395 case VHOST_USER_SET_VRING_BASE:
396 vhost_set_vring_base(vid, &msg.payload.state);
399 case VHOST_USER_GET_VRING_BASE:
400 ret = user_get_vring_base(vid, &msg.payload.state);
401 msg.size = sizeof(msg.payload.state);
402 send_vhost_message(connfd, &msg);
405 case VHOST_USER_SET_VRING_KICK:
406 user_set_vring_kick(vid, &msg);
408 case VHOST_USER_SET_VRING_CALL:
409 user_set_vring_call(vid, &msg);
412 case VHOST_USER_SET_VRING_ERR:
413 if (!(msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK))
415 RTE_LOG(INFO, VHOST_CONFIG, "not implemented\n");
418 case VHOST_USER_GET_QUEUE_NUM:
419 msg.payload.u64 = VHOST_MAX_QUEUE_PAIRS;
420 msg.size = sizeof(msg.payload.u64);
421 send_vhost_message(connfd, &msg);
424 case VHOST_USER_SET_VRING_ENABLE:
425 user_set_vring_enable(vid, &msg.payload.state);
427 case VHOST_USER_SEND_RARP:
428 user_send_rarp(vid, &msg);
438 create_unix_socket(const char *path, struct sockaddr_un *un, bool is_server)
442 fd = socket(AF_UNIX, SOCK_STREAM, 0);
445 RTE_LOG(INFO, VHOST_CONFIG, "vhost-user %s: socket created, fd: %d\n",
446 is_server ? "server" : "client", fd);
448 memset(un, 0, sizeof(*un));
449 un->sun_family = AF_UNIX;
450 strncpy(un->sun_path, path, sizeof(un->sun_path));
451 un->sun_path[sizeof(un->sun_path) - 1] = '\0';
457 vhost_user_create_server(struct vhost_user_socket *vsocket)
461 struct sockaddr_un un;
462 const char *path = vsocket->path;
464 fd = create_unix_socket(path, &un, vsocket->is_server);
468 ret = bind(fd, (struct sockaddr *)&un, sizeof(un));
470 RTE_LOG(ERR, VHOST_CONFIG,
471 "failed to bind to %s: %s; remove it and try again\n",
472 path, strerror(errno));
475 RTE_LOG(INFO, VHOST_CONFIG, "bind to %s\n", path);
477 ret = listen(fd, MAX_VIRTIO_BACKLOG);
481 vsocket->listenfd = fd;
482 ret = fdset_add(&vhost_user.fdset, fd, vhost_user_server_new_connection,
485 RTE_LOG(ERR, VHOST_CONFIG,
486 "failed to add listen fd %d to vhost server fdset\n",
498 struct vhost_user_reconnect {
499 struct sockaddr_un un;
501 struct vhost_user_socket *vsocket;
503 TAILQ_ENTRY(vhost_user_reconnect) next;
506 TAILQ_HEAD(vhost_user_reconnect_tailq_list, vhost_user_reconnect);
507 struct vhost_user_reconnect_list {
508 struct vhost_user_reconnect_tailq_list head;
509 pthread_mutex_t mutex;
512 static struct vhost_user_reconnect_list reconn_list;
513 static pthread_t reconn_tid;
516 vhost_user_client_reconnect(void *arg __rte_unused)
518 struct vhost_user_reconnect *reconn, *next;
521 pthread_mutex_lock(&reconn_list.mutex);
524 * An equal implementation of TAILQ_FOREACH_SAFE,
525 * which does not exist on all platforms.
527 for (reconn = TAILQ_FIRST(&reconn_list.head);
528 reconn != NULL; reconn = next) {
529 next = TAILQ_NEXT(reconn, next);
531 if (connect(reconn->fd, (struct sockaddr *)&reconn->un,
532 sizeof(reconn->un)) < 0)
535 RTE_LOG(INFO, VHOST_CONFIG,
536 "%s: connected\n", reconn->vsocket->path);
537 vhost_user_add_connection(reconn->fd, reconn->vsocket);
538 TAILQ_REMOVE(&reconn_list.head, reconn, next);
542 pthread_mutex_unlock(&reconn_list.mutex);
550 vhost_user_reconnect_init(void)
554 pthread_mutex_init(&reconn_list.mutex, NULL);
555 TAILQ_INIT(&reconn_list.head);
557 ret = pthread_create(&reconn_tid, NULL,
558 vhost_user_client_reconnect, NULL);
560 RTE_LOG(ERR, VHOST_CONFIG, "failed to create reconnect thread");
566 vhost_user_create_client(struct vhost_user_socket *vsocket)
570 struct sockaddr_un un;
571 const char *path = vsocket->path;
572 struct vhost_user_reconnect *reconn;
574 fd = create_unix_socket(path, &un, vsocket->is_server);
578 ret = connect(fd, (struct sockaddr *)&un, sizeof(un));
580 vhost_user_add_connection(fd, vsocket);
584 RTE_LOG(ERR, VHOST_CONFIG,
585 "failed to connect to %s: %s\n",
586 path, strerror(errno));
588 if (!vsocket->reconnect) {
593 RTE_LOG(ERR, VHOST_CONFIG, "%s: reconnecting...\n", path);
594 reconn = malloc(sizeof(*reconn));
595 if (reconn == NULL) {
596 RTE_LOG(ERR, VHOST_CONFIG,
597 "failed to allocate memory for reconnect\n");
603 reconn->vsocket = vsocket;
604 pthread_mutex_lock(&reconn_list.mutex);
605 TAILQ_INSERT_TAIL(&reconn_list.head, reconn, next);
606 pthread_mutex_unlock(&reconn_list.mutex);
612 * Register a new vhost-user socket; here we could act as server
613 * (the default case), or client (when RTE_VHOST_USER_CLIENT) flag
617 rte_vhost_driver_register(const char *path, uint64_t flags)
620 struct vhost_user_socket *vsocket;
625 pthread_mutex_lock(&vhost_user.mutex);
627 if (vhost_user.vsocket_cnt == MAX_VHOST_SOCKET) {
628 RTE_LOG(ERR, VHOST_CONFIG,
629 "error: the number of vhost sockets reaches maximum\n");
633 vsocket = malloc(sizeof(struct vhost_user_socket));
636 memset(vsocket, 0, sizeof(struct vhost_user_socket));
637 vsocket->path = strdup(path);
639 if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
640 vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
641 if (vsocket->reconnect && reconn_tid == 0) {
642 if (vhost_user_reconnect_init() < 0) {
648 ret = vhost_user_create_client(vsocket);
650 vsocket->is_server = true;
651 ret = vhost_user_create_server(vsocket);
659 vhost_user.vsockets[vhost_user.vsocket_cnt++] = vsocket;
662 pthread_mutex_unlock(&vhost_user.mutex);
668 * Unregister the specified vhost socket
671 rte_vhost_driver_unregister(const char *path)
676 pthread_mutex_lock(&vhost_user.mutex);
678 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
679 if (!strcmp(vhost_user.vsockets[i]->path, path)) {
680 if (vhost_user.vsockets[i]->is_server) {
681 fdset_del(&vhost_user.fdset,
682 vhost_user.vsockets[i]->listenfd);
683 close(vhost_user.vsockets[i]->listenfd);
687 free(vhost_user.vsockets[i]->path);
688 free(vhost_user.vsockets[i]);
690 count = --vhost_user.vsocket_cnt;
691 vhost_user.vsockets[i] = vhost_user.vsockets[count];
692 vhost_user.vsockets[count] = NULL;
693 pthread_mutex_unlock(&vhost_user.mutex);
698 pthread_mutex_unlock(&vhost_user.mutex);
704 rte_vhost_driver_session_start(void)
706 fdset_event_dispatch(&vhost_user.fdset);