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"
102 * Create a unix domain socket, bind to path and listen for connection.
104 * socket fd or -1 on failure
107 uds_socket(const char *path)
109 struct sockaddr_un un;
116 sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
119 RTE_LOG(INFO, VHOST_CONFIG, "socket created, fd:%d\n", sockfd);
121 memset(&un, 0, sizeof(un));
122 un.sun_family = AF_UNIX;
123 snprintf(un.sun_path, sizeof(un.sun_path), "%s", path);
124 ret = bind(sockfd, (struct sockaddr *)&un, sizeof(un));
126 RTE_LOG(ERR, VHOST_CONFIG, "fail to bind fd:%d, remove file:%s and try again.\n",
130 RTE_LOG(INFO, VHOST_CONFIG, "bind to %s\n", path);
132 ret = listen(sockfd, MAX_VIRTIO_BACKLOG);
143 /* return bytes# of read on success or negative val on failure. */
145 read_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
149 size_t fdsize = fd_num * sizeof(int);
150 char control[CMSG_SPACE(fdsize)];
151 struct cmsghdr *cmsg;
154 memset(&msgh, 0, sizeof(msgh));
156 iov.iov_len = buflen;
160 msgh.msg_control = control;
161 msgh.msg_controllen = sizeof(control);
163 ret = recvmsg(sockfd, &msgh, 0);
165 RTE_LOG(ERR, VHOST_CONFIG, "recvmsg failed\n");
169 if (msgh.msg_flags & (MSG_TRUNC | MSG_CTRUNC)) {
170 RTE_LOG(ERR, VHOST_CONFIG, "truncted msg\n");
174 for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
175 cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
176 if ((cmsg->cmsg_level == SOL_SOCKET) &&
177 (cmsg->cmsg_type == SCM_RIGHTS)) {
178 memcpy(fds, CMSG_DATA(cmsg), fdsize);
186 /* return bytes# of read on success or negative val on failure. */
188 read_vhost_message(int sockfd, struct VhostUserMsg *msg)
192 ret = read_fd_message(sockfd, (char *)msg, VHOST_USER_HDR_SIZE,
193 msg->fds, VHOST_MEMORY_MAX_NREGIONS);
197 if (msg && msg->size) {
198 if (msg->size > sizeof(msg->payload)) {
199 RTE_LOG(ERR, VHOST_CONFIG,
200 "invalid msg size: %d\n", msg->size);
203 ret = read(sockfd, &msg->payload, msg->size);
206 if (ret != (int)msg->size) {
207 RTE_LOG(ERR, VHOST_CONFIG,
208 "read control message failed\n");
217 send_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
222 size_t fdsize = fd_num * sizeof(int);
223 char control[CMSG_SPACE(fdsize)];
224 struct cmsghdr *cmsg;
227 memset(&msgh, 0, sizeof(msgh));
229 iov.iov_len = buflen;
234 if (fds && fd_num > 0) {
235 msgh.msg_control = control;
236 msgh.msg_controllen = sizeof(control);
237 cmsg = CMSG_FIRSTHDR(&msgh);
238 cmsg->cmsg_len = CMSG_LEN(fdsize);
239 cmsg->cmsg_level = SOL_SOCKET;
240 cmsg->cmsg_type = SCM_RIGHTS;
241 memcpy(CMSG_DATA(cmsg), fds, fdsize);
243 msgh.msg_control = NULL;
244 msgh.msg_controllen = 0;
248 ret = sendmsg(sockfd, &msgh, 0);
249 } while (ret < 0 && errno == EINTR);
252 RTE_LOG(ERR, VHOST_CONFIG, "sendmsg error\n");
260 send_vhost_message(int sockfd, struct VhostUserMsg *msg)
267 msg->flags &= ~VHOST_USER_VERSION_MASK;
268 msg->flags |= VHOST_USER_VERSION;
269 msg->flags |= VHOST_USER_REPLY_MASK;
271 ret = send_fd_message(sockfd, (char *)msg,
272 VHOST_USER_HDR_SIZE + msg->size, NULL, 0);
277 /* call back when there is new virtio connection. */
279 vserver_new_vq_conn(int fd, void *dat, __rte_unused int *remove)
281 struct vhost_server *vserver = (struct vhost_server *)dat;
283 struct connfd_ctx *ctx;
285 struct vhost_device_ctx vdev_ctx = { (pid_t)0, 0 };
288 conn_fd = accept(fd, NULL, NULL);
289 RTE_LOG(INFO, VHOST_CONFIG,
290 "new virtio connection is %d\n", conn_fd);
294 ctx = calloc(1, sizeof(*ctx));
300 fh = ops->new_device(vdev_ctx);
308 size = strnlen(vserver->path, PATH_MAX);
309 ops->set_ifname(vdev_ctx, vserver->path,
312 RTE_LOG(INFO, VHOST_CONFIG, "new device, handle is %d\n", fh);
314 ctx->vserver = vserver;
316 fdset_add(&g_vhost_server.fdset,
317 conn_fd, vserver_message_handler, NULL, ctx);
320 /* callback when there is message on the connfd */
322 vserver_message_handler(int connfd, void *dat, int *remove)
324 struct vhost_device_ctx ctx;
325 struct connfd_ctx *cfd_ctx = (struct connfd_ctx *)dat;
326 struct VhostUserMsg msg;
330 ctx.fh = cfd_ctx->fh;
331 ret = read_vhost_message(connfd, &msg);
332 if (ret <= 0 || msg.request > VHOST_USER_MAX) {
334 RTE_LOG(ERR, VHOST_CONFIG,
335 "vhost read message failed\n");
337 RTE_LOG(INFO, VHOST_CONFIG,
338 "vhost peer closed\n");
340 RTE_LOG(ERR, VHOST_CONFIG,
341 "vhost read incorrect message\n");
346 user_destroy_device(ctx);
347 ops->destroy_device(ctx);
352 RTE_LOG(INFO, VHOST_CONFIG, "read message %s\n",
353 vhost_message_str[msg.request]);
354 switch (msg.request) {
355 case VHOST_USER_GET_FEATURES:
356 ret = ops->get_features(ctx, &features);
357 msg.payload.u64 = features;
358 msg.size = sizeof(msg.payload.u64);
359 send_vhost_message(connfd, &msg);
361 case VHOST_USER_SET_FEATURES:
362 features = msg.payload.u64;
363 ops->set_features(ctx, &features);
366 case VHOST_USER_SET_OWNER:
369 case VHOST_USER_RESET_OWNER:
370 ops->reset_owner(ctx);
373 case VHOST_USER_SET_MEM_TABLE:
374 user_set_mem_table(ctx, &msg);
377 case VHOST_USER_SET_LOG_BASE:
378 RTE_LOG(INFO, VHOST_CONFIG, "not implemented.\n");
379 case VHOST_USER_SET_LOG_FD:
381 RTE_LOG(INFO, VHOST_CONFIG, "not implemented.\n");
384 case VHOST_USER_SET_VRING_NUM:
385 ops->set_vring_num(ctx, &msg.payload.state);
387 case VHOST_USER_SET_VRING_ADDR:
388 ops->set_vring_addr(ctx, &msg.payload.addr);
390 case VHOST_USER_SET_VRING_BASE:
391 ops->set_vring_base(ctx, &msg.payload.state);
394 case VHOST_USER_GET_VRING_BASE:
395 ret = user_get_vring_base(ctx, &msg.payload.state);
396 msg.size = sizeof(msg.payload.state);
397 send_vhost_message(connfd, &msg);
400 case VHOST_USER_SET_VRING_KICK:
401 user_set_vring_kick(ctx, &msg);
403 case VHOST_USER_SET_VRING_CALL:
404 user_set_vring_call(ctx, &msg);
407 case VHOST_USER_SET_VRING_ERR:
408 if (!(msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK))
410 RTE_LOG(INFO, VHOST_CONFIG, "not implemented\n");
420 * Creates and initialise the vhost server.
423 rte_vhost_driver_register(const char *path)
425 struct vhost_server *vserver;
427 pthread_mutex_lock(&g_vhost_server.server_mutex);
429 ops = get_virtio_net_callbacks();
431 if (g_vhost_server.vserver_cnt == MAX_VHOST_SERVER) {
432 RTE_LOG(ERR, VHOST_CONFIG,
433 "error: the number of servers reaches maximum\n");
434 pthread_mutex_unlock(&g_vhost_server.server_mutex);
438 vserver = calloc(sizeof(struct vhost_server), 1);
439 if (vserver == NULL) {
440 pthread_mutex_unlock(&g_vhost_server.server_mutex);
444 vserver->listenfd = uds_socket(path);
445 if (vserver->listenfd < 0) {
447 pthread_mutex_unlock(&g_vhost_server.server_mutex);
451 vserver->path = strdup(path);
453 fdset_add(&g_vhost_server.fdset, vserver->listenfd,
454 vserver_new_vq_conn, NULL, vserver);
456 g_vhost_server.server[g_vhost_server.vserver_cnt++] = vserver;
457 pthread_mutex_unlock(&g_vhost_server.server_mutex);
464 * Unregister the specified vhost server
467 rte_vhost_driver_unregister(const char *path)
472 pthread_mutex_lock(&g_vhost_server.server_mutex);
474 for (i = 0; i < g_vhost_server.vserver_cnt; i++) {
475 if (!strcmp(g_vhost_server.server[i]->path, path)) {
476 fdset_del(&g_vhost_server.fdset,
477 g_vhost_server.server[i]->listenfd);
479 close(g_vhost_server.server[i]->listenfd);
480 free(g_vhost_server.server[i]->path);
481 free(g_vhost_server.server[i]);
485 count = --g_vhost_server.vserver_cnt;
486 g_vhost_server.server[i] = g_vhost_server.server[count];
487 g_vhost_server.server[count] = NULL;
488 pthread_mutex_unlock(&g_vhost_server.server_mutex);
493 pthread_mutex_unlock(&g_vhost_server.server_mutex);
499 rte_vhost_driver_session_start(void)
501 fdset_event_dispatch(&g_vhost_server.fdset);