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];
71 static struct _vhost_server g_vhost_server = {
73 .fd = { [0 ... MAX_FDS - 1] = {-1, NULL, NULL, NULL, 0} },
74 .fd_mutex = PTHREAD_MUTEX_INITIALIZER,
79 static int vserver_idx;
81 static const char *vhost_message_str[VHOST_USER_MAX] = {
82 [VHOST_USER_NONE] = "VHOST_USER_NONE",
83 [VHOST_USER_GET_FEATURES] = "VHOST_USER_GET_FEATURES",
84 [VHOST_USER_SET_FEATURES] = "VHOST_USER_SET_FEATURES",
85 [VHOST_USER_SET_OWNER] = "VHOST_USER_SET_OWNER",
86 [VHOST_USER_RESET_OWNER] = "VHOST_USER_RESET_OWNER",
87 [VHOST_USER_SET_MEM_TABLE] = "VHOST_USER_SET_MEM_TABLE",
88 [VHOST_USER_SET_LOG_BASE] = "VHOST_USER_SET_LOG_BASE",
89 [VHOST_USER_SET_LOG_FD] = "VHOST_USER_SET_LOG_FD",
90 [VHOST_USER_SET_VRING_NUM] = "VHOST_USER_SET_VRING_NUM",
91 [VHOST_USER_SET_VRING_ADDR] = "VHOST_USER_SET_VRING_ADDR",
92 [VHOST_USER_SET_VRING_BASE] = "VHOST_USER_SET_VRING_BASE",
93 [VHOST_USER_GET_VRING_BASE] = "VHOST_USER_GET_VRING_BASE",
94 [VHOST_USER_SET_VRING_KICK] = "VHOST_USER_SET_VRING_KICK",
95 [VHOST_USER_SET_VRING_CALL] = "VHOST_USER_SET_VRING_CALL",
96 [VHOST_USER_SET_VRING_ERR] = "VHOST_USER_SET_VRING_ERR"
100 * Create a unix domain socket, bind to path and listen for connection.
102 * socket fd or -1 on failure
105 uds_socket(const char *path)
107 struct sockaddr_un un;
114 sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
117 RTE_LOG(INFO, VHOST_CONFIG, "socket created, fd:%d\n", sockfd);
119 memset(&un, 0, sizeof(un));
120 un.sun_family = AF_UNIX;
121 snprintf(un.sun_path, sizeof(un.sun_path), "%s", path);
122 ret = bind(sockfd, (struct sockaddr *)&un, sizeof(un));
125 RTE_LOG(INFO, VHOST_CONFIG, "bind to %s\n", path);
127 ret = listen(sockfd, MAX_VIRTIO_BACKLOG);
138 /* return bytes# of read on success or negative val on failure. */
140 read_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
144 size_t fdsize = fd_num * sizeof(int);
145 char control[CMSG_SPACE(fdsize)];
146 struct cmsghdr *cmsg;
149 memset(&msgh, 0, sizeof(msgh));
151 iov.iov_len = buflen;
155 msgh.msg_control = control;
156 msgh.msg_controllen = sizeof(control);
158 ret = recvmsg(sockfd, &msgh, 0);
160 RTE_LOG(ERR, VHOST_CONFIG, "recvmsg failed\n");
164 if (msgh.msg_flags & (MSG_TRUNC | MSG_CTRUNC)) {
165 RTE_LOG(ERR, VHOST_CONFIG, "truncted msg\n");
169 for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
170 cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
171 if ((cmsg->cmsg_level == SOL_SOCKET) &&
172 (cmsg->cmsg_type == SCM_RIGHTS)) {
173 memcpy(fds, CMSG_DATA(cmsg), fdsize);
181 /* return bytes# of read on success or negative val on failure. */
183 read_vhost_message(int sockfd, struct VhostUserMsg *msg)
187 ret = read_fd_message(sockfd, (char *)msg, VHOST_USER_HDR_SIZE,
188 msg->fds, VHOST_MEMORY_MAX_NREGIONS);
192 if (msg && msg->size) {
193 if (msg->size > sizeof(msg->payload)) {
194 RTE_LOG(ERR, VHOST_CONFIG,
195 "invalid msg size: %d\n", msg->size);
198 ret = read(sockfd, &msg->payload, msg->size);
201 if (ret != (int)msg->size) {
202 RTE_LOG(ERR, VHOST_CONFIG,
203 "read control message failed\n");
212 send_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
217 size_t fdsize = fd_num * sizeof(int);
218 char control[CMSG_SPACE(fdsize)];
219 struct cmsghdr *cmsg;
222 memset(&msgh, 0, sizeof(msgh));
224 iov.iov_len = buflen;
229 if (fds && fd_num > 0) {
230 msgh.msg_control = control;
231 msgh.msg_controllen = sizeof(control);
232 cmsg = CMSG_FIRSTHDR(&msgh);
233 cmsg->cmsg_len = CMSG_LEN(fdsize);
234 cmsg->cmsg_level = SOL_SOCKET;
235 cmsg->cmsg_type = SCM_RIGHTS;
236 memcpy(CMSG_DATA(cmsg), fds, fdsize);
238 msgh.msg_control = NULL;
239 msgh.msg_controllen = 0;
243 ret = sendmsg(sockfd, &msgh, 0);
244 } while (ret < 0 && errno == EINTR);
247 RTE_LOG(ERR, VHOST_CONFIG, "sendmsg error\n");
255 send_vhost_message(int sockfd, struct VhostUserMsg *msg)
262 msg->flags &= ~VHOST_USER_VERSION_MASK;
263 msg->flags |= VHOST_USER_VERSION;
264 msg->flags |= VHOST_USER_REPLY_MASK;
266 ret = send_fd_message(sockfd, (char *)msg,
267 VHOST_USER_HDR_SIZE + msg->size, NULL, 0);
272 /* call back when there is new virtio connection. */
274 vserver_new_vq_conn(int fd, void *dat, __rte_unused int *remove)
276 struct vhost_server *vserver = (struct vhost_server *)dat;
278 struct connfd_ctx *ctx;
280 struct vhost_device_ctx vdev_ctx = { (pid_t)0, 0 };
283 conn_fd = accept(fd, NULL, NULL);
284 RTE_LOG(INFO, VHOST_CONFIG,
285 "new virtio connection is %d\n", conn_fd);
289 ctx = calloc(1, sizeof(*ctx));
295 fh = ops->new_device(vdev_ctx);
303 size = strnlen(vserver->path, PATH_MAX);
304 ops->set_ifname(vdev_ctx, vserver->path,
307 RTE_LOG(INFO, VHOST_CONFIG, "new device, handle is %d\n", fh);
309 ctx->vserver = vserver;
311 fdset_add(&g_vhost_server.fdset,
312 conn_fd, vserver_message_handler, NULL, ctx);
315 /* callback when there is message on the connfd */
317 vserver_message_handler(int connfd, void *dat, int *remove)
319 struct vhost_device_ctx ctx;
320 struct connfd_ctx *cfd_ctx = (struct connfd_ctx *)dat;
321 struct VhostUserMsg msg;
325 ctx.fh = cfd_ctx->fh;
326 ret = read_vhost_message(connfd, &msg);
328 RTE_LOG(ERR, VHOST_CONFIG,
329 "vhost read message failed\n");
334 user_destroy_device(ctx);
335 ops->destroy_device(ctx);
338 } else if (ret == 0) {
339 RTE_LOG(INFO, VHOST_CONFIG,
340 "vhost peer closed\n");
345 user_destroy_device(ctx);
346 ops->destroy_device(ctx);
350 if (msg.request > VHOST_USER_MAX) {
351 RTE_LOG(ERR, VHOST_CONFIG,
352 "vhost read incorrect message\n");
357 user_destroy_device(ctx);
358 ops->destroy_device(ctx);
363 RTE_LOG(INFO, VHOST_CONFIG, "read message %s\n",
364 vhost_message_str[msg.request]);
365 switch (msg.request) {
366 case VHOST_USER_GET_FEATURES:
367 ret = ops->get_features(ctx, &features);
368 msg.payload.u64 = features;
369 msg.size = sizeof(msg.payload.u64);
370 send_vhost_message(connfd, &msg);
372 case VHOST_USER_SET_FEATURES:
373 features = msg.payload.u64;
374 ops->set_features(ctx, &features);
377 case VHOST_USER_SET_OWNER:
380 case VHOST_USER_RESET_OWNER:
381 ops->reset_owner(ctx);
384 case VHOST_USER_SET_MEM_TABLE:
385 user_set_mem_table(ctx, &msg);
388 case VHOST_USER_SET_LOG_BASE:
389 RTE_LOG(INFO, VHOST_CONFIG, "not implemented.\n");
390 case VHOST_USER_SET_LOG_FD:
392 RTE_LOG(INFO, VHOST_CONFIG, "not implemented.\n");
395 case VHOST_USER_SET_VRING_NUM:
396 ops->set_vring_num(ctx, &msg.payload.state);
398 case VHOST_USER_SET_VRING_ADDR:
399 ops->set_vring_addr(ctx, &msg.payload.addr);
401 case VHOST_USER_SET_VRING_BASE:
402 ops->set_vring_base(ctx, &msg.payload.state);
405 case VHOST_USER_GET_VRING_BASE:
406 ret = user_get_vring_base(ctx, &msg.payload.state);
407 msg.size = sizeof(msg.payload.state);
408 send_vhost_message(connfd, &msg);
411 case VHOST_USER_SET_VRING_KICK:
412 user_set_vring_kick(ctx, &msg);
414 case VHOST_USER_SET_VRING_CALL:
415 user_set_vring_call(ctx, &msg);
418 case VHOST_USER_SET_VRING_ERR:
419 if (!(msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK))
421 RTE_LOG(INFO, VHOST_CONFIG, "not implemented\n");
432 * Creates and initialise the vhost server.
435 rte_vhost_driver_register(const char *path)
437 struct vhost_server *vserver;
439 if (vserver_idx == 0)
440 ops = get_virtio_net_callbacks();
441 if (vserver_idx == MAX_VHOST_SERVER)
444 vserver = calloc(sizeof(struct vhost_server), 1);
450 vserver->listenfd = uds_socket(path);
451 if (vserver->listenfd < 0) {
455 vserver->path = path;
457 fdset_add(&g_vhost_server.fdset, vserver->listenfd,
458 vserver_new_vq_conn, NULL,
461 g_vhost_server.server[vserver_idx++] = vserver;
468 rte_vhost_driver_session_start(void)
470 fdset_event_dispatch(&g_vhost_server.fdset);