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.
40 #include <sys/types.h>
41 #include <sys/socket.h>
47 #include <rte_virtio_net.h>
50 #include "vhost-net-user.h"
51 #include "vhost-net.h"
52 #include "virtio-net-user.h"
54 #define MAX_VIRTIO_BACKLOG 128
56 static void vserver_new_vq_conn(int fd, void *data, int *remove);
57 static void vserver_message_handler(int fd, void *dat, int *remove);
58 struct vhost_net_device_ops const *ops;
61 struct vhost_server *vserver;
65 #define MAX_VHOST_SERVER 1024
66 struct _vhost_server {
67 struct vhost_server *server[MAX_VHOST_SERVER];
70 pthread_mutex_t server_mutex;
73 static struct _vhost_server g_vhost_server = {
75 .fd = { [0 ... MAX_FDS - 1] = {-1, NULL, NULL, NULL, 0} },
76 .fd_mutex = PTHREAD_MUTEX_INITIALIZER,
80 .server_mutex = PTHREAD_MUTEX_INITIALIZER,
83 static const char *vhost_message_str[VHOST_USER_MAX] = {
84 [VHOST_USER_NONE] = "VHOST_USER_NONE",
85 [VHOST_USER_GET_FEATURES] = "VHOST_USER_GET_FEATURES",
86 [VHOST_USER_SET_FEATURES] = "VHOST_USER_SET_FEATURES",
87 [VHOST_USER_SET_OWNER] = "VHOST_USER_SET_OWNER",
88 [VHOST_USER_RESET_OWNER] = "VHOST_USER_RESET_OWNER",
89 [VHOST_USER_SET_MEM_TABLE] = "VHOST_USER_SET_MEM_TABLE",
90 [VHOST_USER_SET_LOG_BASE] = "VHOST_USER_SET_LOG_BASE",
91 [VHOST_USER_SET_LOG_FD] = "VHOST_USER_SET_LOG_FD",
92 [VHOST_USER_SET_VRING_NUM] = "VHOST_USER_SET_VRING_NUM",
93 [VHOST_USER_SET_VRING_ADDR] = "VHOST_USER_SET_VRING_ADDR",
94 [VHOST_USER_SET_VRING_BASE] = "VHOST_USER_SET_VRING_BASE",
95 [VHOST_USER_GET_VRING_BASE] = "VHOST_USER_GET_VRING_BASE",
96 [VHOST_USER_SET_VRING_KICK] = "VHOST_USER_SET_VRING_KICK",
97 [VHOST_USER_SET_VRING_CALL] = "VHOST_USER_SET_VRING_CALL",
98 [VHOST_USER_SET_VRING_ERR] = "VHOST_USER_SET_VRING_ERR",
99 [VHOST_USER_GET_PROTOCOL_FEATURES] = "VHOST_USER_GET_PROTOCOL_FEATURES",
100 [VHOST_USER_SET_PROTOCOL_FEATURES] = "VHOST_USER_SET_PROTOCOL_FEATURES",
101 [VHOST_USER_GET_QUEUE_NUM] = "VHOST_USER_GET_QUEUE_NUM",
102 [VHOST_USER_SET_VRING_ENABLE] = "VHOST_USER_SET_VRING_ENABLE",
103 [VHOST_USER_SEND_RARP] = "VHOST_USER_SEND_RARP",
107 * Create a unix domain socket, bind to path and listen for connection.
109 * socket fd or -1 on failure
112 uds_socket(const char *path)
114 struct sockaddr_un un;
121 sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
124 RTE_LOG(INFO, VHOST_CONFIG, "socket created, fd:%d\n", sockfd);
126 memset(&un, 0, sizeof(un));
127 un.sun_family = AF_UNIX;
128 snprintf(un.sun_path, sizeof(un.sun_path), "%s", path);
129 ret = bind(sockfd, (struct sockaddr *)&un, sizeof(un));
131 RTE_LOG(ERR, VHOST_CONFIG, "fail to bind fd:%d, remove file:%s and try again.\n",
135 RTE_LOG(INFO, VHOST_CONFIG, "bind to %s\n", path);
137 ret = listen(sockfd, MAX_VIRTIO_BACKLOG);
148 /* return bytes# of read on success or negative val on failure. */
150 read_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
154 size_t fdsize = fd_num * sizeof(int);
155 char control[CMSG_SPACE(fdsize)];
156 struct cmsghdr *cmsg;
159 memset(&msgh, 0, sizeof(msgh));
161 iov.iov_len = buflen;
165 msgh.msg_control = control;
166 msgh.msg_controllen = sizeof(control);
168 ret = recvmsg(sockfd, &msgh, 0);
170 RTE_LOG(ERR, VHOST_CONFIG, "recvmsg failed\n");
174 if (msgh.msg_flags & (MSG_TRUNC | MSG_CTRUNC)) {
175 RTE_LOG(ERR, VHOST_CONFIG, "truncted msg\n");
179 for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
180 cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
181 if ((cmsg->cmsg_level == SOL_SOCKET) &&
182 (cmsg->cmsg_type == SCM_RIGHTS)) {
183 memcpy(fds, CMSG_DATA(cmsg), fdsize);
191 /* return bytes# of read on success or negative val on failure. */
193 read_vhost_message(int sockfd, struct VhostUserMsg *msg)
197 ret = read_fd_message(sockfd, (char *)msg, VHOST_USER_HDR_SIZE,
198 msg->fds, VHOST_MEMORY_MAX_NREGIONS);
202 if (msg && msg->size) {
203 if (msg->size > sizeof(msg->payload)) {
204 RTE_LOG(ERR, VHOST_CONFIG,
205 "invalid msg size: %d\n", msg->size);
208 ret = read(sockfd, &msg->payload, msg->size);
211 if (ret != (int)msg->size) {
212 RTE_LOG(ERR, VHOST_CONFIG,
213 "read control message failed\n");
222 send_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
227 size_t fdsize = fd_num * sizeof(int);
228 char control[CMSG_SPACE(fdsize)];
229 struct cmsghdr *cmsg;
232 memset(&msgh, 0, sizeof(msgh));
234 iov.iov_len = buflen;
239 if (fds && fd_num > 0) {
240 msgh.msg_control = control;
241 msgh.msg_controllen = sizeof(control);
242 cmsg = CMSG_FIRSTHDR(&msgh);
243 cmsg->cmsg_len = CMSG_LEN(fdsize);
244 cmsg->cmsg_level = SOL_SOCKET;
245 cmsg->cmsg_type = SCM_RIGHTS;
246 memcpy(CMSG_DATA(cmsg), fds, fdsize);
248 msgh.msg_control = NULL;
249 msgh.msg_controllen = 0;
253 ret = sendmsg(sockfd, &msgh, 0);
254 } while (ret < 0 && errno == EINTR);
257 RTE_LOG(ERR, VHOST_CONFIG, "sendmsg error\n");
265 send_vhost_message(int sockfd, struct VhostUserMsg *msg)
272 msg->flags &= ~VHOST_USER_VERSION_MASK;
273 msg->flags |= VHOST_USER_VERSION;
274 msg->flags |= VHOST_USER_REPLY_MASK;
276 ret = send_fd_message(sockfd, (char *)msg,
277 VHOST_USER_HDR_SIZE + msg->size, NULL, 0);
282 /* call back when there is new virtio connection. */
284 vserver_new_vq_conn(int fd, void *dat, __rte_unused int *remove)
286 struct vhost_server *vserver = (struct vhost_server *)dat;
288 struct connfd_ctx *ctx;
290 struct vhost_device_ctx vdev_ctx = { (pid_t)0, 0 };
293 conn_fd = accept(fd, NULL, NULL);
294 RTE_LOG(INFO, VHOST_CONFIG,
295 "new virtio connection is %d\n", conn_fd);
299 ctx = calloc(1, sizeof(*ctx));
305 fh = ops->new_device(vdev_ctx);
313 size = strnlen(vserver->path, PATH_MAX);
314 ops->set_ifname(vdev_ctx, vserver->path,
317 RTE_LOG(INFO, VHOST_CONFIG, "new device, handle is %d\n", fh);
319 ctx->vserver = vserver;
321 fdset_add(&g_vhost_server.fdset,
322 conn_fd, vserver_message_handler, NULL, ctx);
325 /* callback when there is message on the connfd */
327 vserver_message_handler(int connfd, void *dat, int *remove)
329 struct vhost_device_ctx ctx;
330 struct connfd_ctx *cfd_ctx = (struct connfd_ctx *)dat;
331 struct VhostUserMsg msg;
335 ctx.fh = cfd_ctx->fh;
336 ret = read_vhost_message(connfd, &msg);
337 if (ret <= 0 || msg.request >= VHOST_USER_MAX) {
339 RTE_LOG(ERR, VHOST_CONFIG,
340 "vhost read message failed\n");
342 RTE_LOG(INFO, VHOST_CONFIG,
343 "vhost peer closed\n");
345 RTE_LOG(ERR, VHOST_CONFIG,
346 "vhost read incorrect message\n");
351 user_destroy_device(ctx);
352 ops->destroy_device(ctx);
357 RTE_LOG(INFO, VHOST_CONFIG, "read message %s\n",
358 vhost_message_str[msg.request]);
359 switch (msg.request) {
360 case VHOST_USER_GET_FEATURES:
361 ret = ops->get_features(ctx, &features);
362 msg.payload.u64 = features;
363 msg.size = sizeof(msg.payload.u64);
364 send_vhost_message(connfd, &msg);
366 case VHOST_USER_SET_FEATURES:
367 features = msg.payload.u64;
368 ops->set_features(ctx, &features);
371 case VHOST_USER_GET_PROTOCOL_FEATURES:
372 msg.payload.u64 = VHOST_USER_PROTOCOL_FEATURES;
373 msg.size = sizeof(msg.payload.u64);
374 send_vhost_message(connfd, &msg);
376 case VHOST_USER_SET_PROTOCOL_FEATURES:
377 user_set_protocol_features(ctx, msg.payload.u64);
380 case VHOST_USER_SET_OWNER:
383 case VHOST_USER_RESET_OWNER:
384 ops->reset_owner(ctx);
387 case VHOST_USER_SET_MEM_TABLE:
388 user_set_mem_table(ctx, &msg);
391 case VHOST_USER_SET_LOG_BASE:
392 user_set_log_base(ctx, &msg);
394 /* it needs a reply */
395 msg.size = sizeof(msg.payload.u64);
396 send_vhost_message(connfd, &msg);
398 case VHOST_USER_SET_LOG_FD:
400 RTE_LOG(INFO, VHOST_CONFIG, "not implemented.\n");
403 case VHOST_USER_SET_VRING_NUM:
404 ops->set_vring_num(ctx, &msg.payload.state);
406 case VHOST_USER_SET_VRING_ADDR:
407 ops->set_vring_addr(ctx, &msg.payload.addr);
409 case VHOST_USER_SET_VRING_BASE:
410 ops->set_vring_base(ctx, &msg.payload.state);
413 case VHOST_USER_GET_VRING_BASE:
414 ret = user_get_vring_base(ctx, &msg.payload.state);
415 msg.size = sizeof(msg.payload.state);
416 send_vhost_message(connfd, &msg);
419 case VHOST_USER_SET_VRING_KICK:
420 user_set_vring_kick(ctx, &msg);
422 case VHOST_USER_SET_VRING_CALL:
423 user_set_vring_call(ctx, &msg);
426 case VHOST_USER_SET_VRING_ERR:
427 if (!(msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK))
429 RTE_LOG(INFO, VHOST_CONFIG, "not implemented\n");
432 case VHOST_USER_GET_QUEUE_NUM:
433 msg.payload.u64 = VHOST_MAX_QUEUE_PAIRS;
434 msg.size = sizeof(msg.payload.u64);
435 send_vhost_message(connfd, &msg);
438 case VHOST_USER_SET_VRING_ENABLE:
439 user_set_vring_enable(ctx, &msg.payload.state);
441 case VHOST_USER_SEND_RARP:
442 user_send_rarp(&msg);
452 * Creates and initialise the vhost server.
455 rte_vhost_driver_register(const char *path)
457 struct vhost_server *vserver;
459 pthread_mutex_lock(&g_vhost_server.server_mutex);
461 ops = get_virtio_net_callbacks();
463 if (g_vhost_server.vserver_cnt == MAX_VHOST_SERVER) {
464 RTE_LOG(ERR, VHOST_CONFIG,
465 "error: the number of servers reaches maximum\n");
466 pthread_mutex_unlock(&g_vhost_server.server_mutex);
470 vserver = calloc(sizeof(struct vhost_server), 1);
471 if (vserver == NULL) {
472 pthread_mutex_unlock(&g_vhost_server.server_mutex);
476 vserver->listenfd = uds_socket(path);
477 if (vserver->listenfd < 0) {
479 pthread_mutex_unlock(&g_vhost_server.server_mutex);
483 vserver->path = strdup(path);
485 fdset_add(&g_vhost_server.fdset, vserver->listenfd,
486 vserver_new_vq_conn, NULL, vserver);
488 g_vhost_server.server[g_vhost_server.vserver_cnt++] = vserver;
489 pthread_mutex_unlock(&g_vhost_server.server_mutex);
496 * Unregister the specified vhost server
499 rte_vhost_driver_unregister(const char *path)
504 pthread_mutex_lock(&g_vhost_server.server_mutex);
506 for (i = 0; i < g_vhost_server.vserver_cnt; i++) {
507 if (!strcmp(g_vhost_server.server[i]->path, path)) {
508 fdset_del(&g_vhost_server.fdset,
509 g_vhost_server.server[i]->listenfd);
511 close(g_vhost_server.server[i]->listenfd);
512 free(g_vhost_server.server[i]->path);
513 free(g_vhost_server.server[i]);
517 count = --g_vhost_server.vserver_cnt;
518 g_vhost_server.server[i] = g_vhost_server.server[count];
519 g_vhost_server.server[count] = NULL;
520 pthread_mutex_unlock(&g_vhost_server.server_mutex);
525 pthread_mutex_unlock(&g_vhost_server.server_mutex);
531 rte_vhost_driver_session_start(void)
533 fdset_event_dispatch(&g_vhost_server.fdset);