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>
46 #include <rte_virtio_net.h>
49 #include "vhost-net-user.h"
50 #include "vhost-net.h"
51 #include "virtio-net-user.h"
53 #define MAX_VIRTIO_BACKLOG 128
54 static void vserver_new_vq_conn(int fd, void *data);
55 static void vserver_message_handler(int fd, void *dat);
56 struct vhost_net_device_ops const *ops;
59 struct vhost_server *vserver;
63 #define MAX_VHOST_SERVER 1024
65 struct vhost_server *server[MAX_VHOST_SERVER];
66 struct fdset fdset; /**< The fd list this vhost server manages. */
69 static int vserver_idx;
71 static const char *vhost_message_str[VHOST_USER_MAX] = {
72 [VHOST_USER_NONE] = "VHOST_USER_NONE",
73 [VHOST_USER_GET_FEATURES] = "VHOST_USER_GET_FEATURES",
74 [VHOST_USER_SET_FEATURES] = "VHOST_USER_SET_FEATURES",
75 [VHOST_USER_SET_OWNER] = "VHOST_USER_SET_OWNER",
76 [VHOST_USER_RESET_OWNER] = "VHOST_USER_RESET_OWNER",
77 [VHOST_USER_SET_MEM_TABLE] = "VHOST_USER_SET_MEM_TABLE",
78 [VHOST_USER_SET_LOG_BASE] = "VHOST_USER_SET_LOG_BASE",
79 [VHOST_USER_SET_LOG_FD] = "VHOST_USER_SET_LOG_FD",
80 [VHOST_USER_SET_VRING_NUM] = "VHOST_USER_SET_VRING_NUM",
81 [VHOST_USER_SET_VRING_ADDR] = "VHOST_USER_SET_VRING_ADDR",
82 [VHOST_USER_SET_VRING_BASE] = "VHOST_USER_SET_VRING_BASE",
83 [VHOST_USER_GET_VRING_BASE] = "VHOST_USER_GET_VRING_BASE",
84 [VHOST_USER_SET_VRING_KICK] = "VHOST_USER_SET_VRING_KICK",
85 [VHOST_USER_SET_VRING_CALL] = "VHOST_USER_SET_VRING_CALL",
86 [VHOST_USER_SET_VRING_ERR] = "VHOST_USER_SET_VRING_ERR"
90 * Create a unix domain socket, bind to path and listen for connection.
92 * socket fd or -1 on failure
95 uds_socket(const char *path)
97 struct sockaddr_un un;
104 sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
107 RTE_LOG(INFO, VHOST_CONFIG, "socket created, fd:%d\n", sockfd);
109 memset(&un, 0, sizeof(un));
110 un.sun_family = AF_UNIX;
111 snprintf(un.sun_path, sizeof(un.sun_path), "%s", path);
112 ret = bind(sockfd, (struct sockaddr *)&un, sizeof(un));
115 RTE_LOG(INFO, VHOST_CONFIG, "bind to %s\n", path);
117 ret = listen(sockfd, MAX_VIRTIO_BACKLOG);
128 /* return bytes# of read on success or negative val on failure. */
130 read_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
134 size_t fdsize = fd_num * sizeof(int);
135 char control[CMSG_SPACE(fdsize)];
136 struct cmsghdr *cmsg;
139 memset(&msgh, 0, sizeof(msgh));
141 iov.iov_len = buflen;
145 msgh.msg_control = control;
146 msgh.msg_controllen = sizeof(control);
148 ret = recvmsg(sockfd, &msgh, 0);
150 RTE_LOG(ERR, VHOST_CONFIG, "recvmsg failed\n");
154 if (msgh.msg_flags & (MSG_TRUNC | MSG_CTRUNC)) {
155 RTE_LOG(ERR, VHOST_CONFIG, "truncted msg\n");
159 for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
160 cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
161 if ((cmsg->cmsg_level == SOL_SOCKET) &&
162 (cmsg->cmsg_type == SCM_RIGHTS)) {
163 memcpy(fds, CMSG_DATA(cmsg), fdsize);
171 /* return bytes# of read on success or negative val on failure. */
173 read_vhost_message(int sockfd, struct VhostUserMsg *msg)
177 ret = read_fd_message(sockfd, (char *)msg, VHOST_USER_HDR_SIZE,
178 msg->fds, VHOST_MEMORY_MAX_NREGIONS);
182 if (msg && msg->size) {
183 if (msg->size > sizeof(msg->payload)) {
184 RTE_LOG(ERR, VHOST_CONFIG,
185 "invalid msg size: %d\n", msg->size);
188 ret = read(sockfd, &msg->payload, msg->size);
191 if (ret != (int)msg->size) {
192 RTE_LOG(ERR, VHOST_CONFIG,
193 "read control message failed\n");
202 send_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
207 size_t fdsize = fd_num * sizeof(int);
208 char control[CMSG_SPACE(fdsize)];
209 struct cmsghdr *cmsg;
212 memset(&msgh, 0, sizeof(msgh));
214 iov.iov_len = buflen;
219 if (fds && fd_num > 0) {
220 msgh.msg_control = control;
221 msgh.msg_controllen = sizeof(control);
222 cmsg = CMSG_FIRSTHDR(&msgh);
223 cmsg->cmsg_len = CMSG_LEN(fdsize);
224 cmsg->cmsg_level = SOL_SOCKET;
225 cmsg->cmsg_type = SCM_RIGHTS;
226 memcpy(CMSG_DATA(cmsg), fds, fdsize);
228 msgh.msg_control = NULL;
229 msgh.msg_controllen = 0;
233 ret = sendmsg(sockfd, &msgh, 0);
234 } while (ret < 0 && errno == EINTR);
237 RTE_LOG(ERR, VHOST_CONFIG, "sendmsg error\n");
245 send_vhost_message(int sockfd, struct VhostUserMsg *msg)
252 msg->flags &= ~VHOST_USER_VERSION_MASK;
253 msg->flags |= VHOST_USER_VERSION;
254 msg->flags |= VHOST_USER_REPLY_MASK;
256 ret = send_fd_message(sockfd, (char *)msg,
257 VHOST_USER_HDR_SIZE + msg->size, NULL, 0);
262 /* call back when there is new virtio connection. */
264 vserver_new_vq_conn(int fd, void *dat)
266 struct vhost_server *vserver = (struct vhost_server *)dat;
268 struct connfd_ctx *ctx;
270 struct vhost_device_ctx vdev_ctx = { 0 };
273 conn_fd = accept(fd, NULL, NULL);
274 RTE_LOG(INFO, VHOST_CONFIG,
275 "new virtio connection is %d\n", conn_fd);
279 ctx = calloc(1, sizeof(*ctx));
285 fh = ops->new_device(vdev_ctx);
293 size = strnlen(vserver->path, PATH_MAX);
294 ops->set_ifname(vdev_ctx, vserver->path,
297 RTE_LOG(INFO, VHOST_CONFIG, "new device, handle is %d\n", fh);
299 ctx->vserver = vserver;
301 fdset_add(&g_vhost_server.fdset,
302 conn_fd, vserver_message_handler, NULL, ctx);
305 /* callback when there is message on the connfd */
307 vserver_message_handler(int connfd, void *dat)
309 struct vhost_device_ctx ctx;
310 struct connfd_ctx *cfd_ctx = (struct connfd_ctx *)dat;
311 struct VhostUserMsg msg;
315 ctx.fh = cfd_ctx->fh;
316 ret = read_vhost_message(connfd, &msg);
318 RTE_LOG(ERR, VHOST_CONFIG,
319 "vhost read message failed\n");
322 fdset_del(&g_vhost_server.fdset, connfd);
324 user_destroy_device(ctx);
325 ops->destroy_device(ctx);
328 } else if (ret == 0) {
329 RTE_LOG(INFO, VHOST_CONFIG,
330 "vhost peer closed\n");
333 fdset_del(&g_vhost_server.fdset, connfd);
335 user_destroy_device(ctx);
336 ops->destroy_device(ctx);
340 if (msg.request > VHOST_USER_MAX) {
341 RTE_LOG(ERR, VHOST_CONFIG,
342 "vhost read incorrect message\n");
345 fdset_del(&g_vhost_server.fdset, connfd);
347 user_destroy_device(ctx);
348 ops->destroy_device(ctx);
353 RTE_LOG(INFO, VHOST_CONFIG, "read message %s\n",
354 vhost_message_str[msg.request]);
355 switch (msg.request) {
356 case VHOST_USER_GET_FEATURES:
357 ret = ops->get_features(ctx, &features);
358 msg.payload.u64 = features;
359 msg.size = sizeof(msg.payload.u64);
360 send_vhost_message(connfd, &msg);
362 case VHOST_USER_SET_FEATURES:
363 features = msg.payload.u64;
364 ops->set_features(ctx, &features);
367 case VHOST_USER_SET_OWNER:
370 case VHOST_USER_RESET_OWNER:
371 ops->reset_owner(ctx);
374 case VHOST_USER_SET_MEM_TABLE:
375 user_set_mem_table(ctx, &msg);
378 case VHOST_USER_SET_LOG_BASE:
379 RTE_LOG(INFO, VHOST_CONFIG, "not implemented.\n");
380 case VHOST_USER_SET_LOG_FD:
382 RTE_LOG(INFO, VHOST_CONFIG, "not implemented.\n");
385 case VHOST_USER_SET_VRING_NUM:
386 ops->set_vring_num(ctx, &msg.payload.state);
388 case VHOST_USER_SET_VRING_ADDR:
389 ops->set_vring_addr(ctx, &msg.payload.addr);
391 case VHOST_USER_SET_VRING_BASE:
392 ops->set_vring_base(ctx, &msg.payload.state);
395 case VHOST_USER_GET_VRING_BASE:
396 ret = user_get_vring_base(ctx, &msg.payload.state);
397 msg.size = sizeof(msg.payload.state);
398 send_vhost_message(connfd, &msg);
401 case VHOST_USER_SET_VRING_KICK:
402 user_set_vring_kick(ctx, &msg);
404 case VHOST_USER_SET_VRING_CALL:
405 user_set_vring_call(ctx, &msg);
408 case VHOST_USER_SET_VRING_ERR:
409 if (!(msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK))
411 RTE_LOG(INFO, VHOST_CONFIG, "not implemented\n");
422 * Creates and initialise the vhost server.
425 rte_vhost_driver_register(const char *path)
427 struct vhost_server *vserver;
429 if (vserver_idx == 0) {
430 fdset_init(&g_vhost_server.fdset);
431 ops = get_virtio_net_callbacks();
433 if (vserver_idx == MAX_VHOST_SERVER)
436 vserver = calloc(sizeof(struct vhost_server), 1);
442 vserver->listenfd = uds_socket(path);
443 if (vserver->listenfd < 0) {
447 vserver->path = path;
449 fdset_add(&g_vhost_server.fdset, vserver->listenfd,
450 vserver_new_vq_conn, NULL,
453 g_vhost_server.server[vserver_idx++] = vserver;
460 rte_vhost_driver_session_start(void)
462 fdset_event_dispatch(&g_vhost_server.fdset);