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);
60 struct vhost_server *vserver;
64 #define MAX_VHOST_SERVER 1024
65 struct _vhost_server {
66 struct vhost_server *server[MAX_VHOST_SERVER];
69 pthread_mutex_t server_mutex;
72 static struct _vhost_server g_vhost_server = {
74 .fd = { [0 ... MAX_FDS - 1] = {-1, NULL, NULL, NULL, 0} },
75 .fd_mutex = PTHREAD_MUTEX_INITIALIZER,
79 .server_mutex = PTHREAD_MUTEX_INITIALIZER,
82 static const char *vhost_message_str[VHOST_USER_MAX] = {
83 [VHOST_USER_NONE] = "VHOST_USER_NONE",
84 [VHOST_USER_GET_FEATURES] = "VHOST_USER_GET_FEATURES",
85 [VHOST_USER_SET_FEATURES] = "VHOST_USER_SET_FEATURES",
86 [VHOST_USER_SET_OWNER] = "VHOST_USER_SET_OWNER",
87 [VHOST_USER_RESET_OWNER] = "VHOST_USER_RESET_OWNER",
88 [VHOST_USER_SET_MEM_TABLE] = "VHOST_USER_SET_MEM_TABLE",
89 [VHOST_USER_SET_LOG_BASE] = "VHOST_USER_SET_LOG_BASE",
90 [VHOST_USER_SET_LOG_FD] = "VHOST_USER_SET_LOG_FD",
91 [VHOST_USER_SET_VRING_NUM] = "VHOST_USER_SET_VRING_NUM",
92 [VHOST_USER_SET_VRING_ADDR] = "VHOST_USER_SET_VRING_ADDR",
93 [VHOST_USER_SET_VRING_BASE] = "VHOST_USER_SET_VRING_BASE",
94 [VHOST_USER_GET_VRING_BASE] = "VHOST_USER_GET_VRING_BASE",
95 [VHOST_USER_SET_VRING_KICK] = "VHOST_USER_SET_VRING_KICK",
96 [VHOST_USER_SET_VRING_CALL] = "VHOST_USER_SET_VRING_CALL",
97 [VHOST_USER_SET_VRING_ERR] = "VHOST_USER_SET_VRING_ERR",
98 [VHOST_USER_GET_PROTOCOL_FEATURES] = "VHOST_USER_GET_PROTOCOL_FEATURES",
99 [VHOST_USER_SET_PROTOCOL_FEATURES] = "VHOST_USER_SET_PROTOCOL_FEATURES",
100 [VHOST_USER_GET_QUEUE_NUM] = "VHOST_USER_GET_QUEUE_NUM",
101 [VHOST_USER_SET_VRING_ENABLE] = "VHOST_USER_SET_VRING_ENABLE",
102 [VHOST_USER_SEND_RARP] = "VHOST_USER_SEND_RARP",
106 * Create a unix domain socket, bind to path and listen for connection.
108 * socket fd or -1 on failure
111 uds_socket(const char *path)
113 struct sockaddr_un un;
120 sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
123 RTE_LOG(INFO, VHOST_CONFIG, "socket created, fd:%d\n", sockfd);
125 memset(&un, 0, sizeof(un));
126 un.sun_family = AF_UNIX;
127 snprintf(un.sun_path, sizeof(un.sun_path), "%s", path);
128 ret = bind(sockfd, (struct sockaddr *)&un, sizeof(un));
130 RTE_LOG(ERR, VHOST_CONFIG, "fail to bind fd:%d, remove file:%s and try again.\n",
134 RTE_LOG(INFO, VHOST_CONFIG, "bind to %s\n", path);
136 ret = listen(sockfd, MAX_VIRTIO_BACKLOG);
147 /* return bytes# of read on success or negative val on failure. */
149 read_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
153 size_t fdsize = fd_num * sizeof(int);
154 char control[CMSG_SPACE(fdsize)];
155 struct cmsghdr *cmsg;
158 memset(&msgh, 0, sizeof(msgh));
160 iov.iov_len = buflen;
164 msgh.msg_control = control;
165 msgh.msg_controllen = sizeof(control);
167 ret = recvmsg(sockfd, &msgh, 0);
169 RTE_LOG(ERR, VHOST_CONFIG, "recvmsg failed\n");
173 if (msgh.msg_flags & (MSG_TRUNC | MSG_CTRUNC)) {
174 RTE_LOG(ERR, VHOST_CONFIG, "truncted msg\n");
178 for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
179 cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
180 if ((cmsg->cmsg_level == SOL_SOCKET) &&
181 (cmsg->cmsg_type == SCM_RIGHTS)) {
182 memcpy(fds, CMSG_DATA(cmsg), fdsize);
190 /* return bytes# of read on success or negative val on failure. */
192 read_vhost_message(int sockfd, struct VhostUserMsg *msg)
196 ret = read_fd_message(sockfd, (char *)msg, VHOST_USER_HDR_SIZE,
197 msg->fds, VHOST_MEMORY_MAX_NREGIONS);
201 if (msg && msg->size) {
202 if (msg->size > sizeof(msg->payload)) {
203 RTE_LOG(ERR, VHOST_CONFIG,
204 "invalid msg size: %d\n", msg->size);
207 ret = read(sockfd, &msg->payload, msg->size);
210 if (ret != (int)msg->size) {
211 RTE_LOG(ERR, VHOST_CONFIG,
212 "read control message failed\n");
221 send_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
226 size_t fdsize = fd_num * sizeof(int);
227 char control[CMSG_SPACE(fdsize)];
228 struct cmsghdr *cmsg;
231 memset(&msgh, 0, sizeof(msgh));
233 iov.iov_len = buflen;
238 if (fds && fd_num > 0) {
239 msgh.msg_control = control;
240 msgh.msg_controllen = sizeof(control);
241 cmsg = CMSG_FIRSTHDR(&msgh);
242 cmsg->cmsg_len = CMSG_LEN(fdsize);
243 cmsg->cmsg_level = SOL_SOCKET;
244 cmsg->cmsg_type = SCM_RIGHTS;
245 memcpy(CMSG_DATA(cmsg), fds, fdsize);
247 msgh.msg_control = NULL;
248 msgh.msg_controllen = 0;
252 ret = sendmsg(sockfd, &msgh, 0);
253 } while (ret < 0 && errno == EINTR);
256 RTE_LOG(ERR, VHOST_CONFIG, "sendmsg error\n");
264 send_vhost_message(int sockfd, struct VhostUserMsg *msg)
271 msg->flags &= ~VHOST_USER_VERSION_MASK;
272 msg->flags |= VHOST_USER_VERSION;
273 msg->flags |= VHOST_USER_REPLY_MASK;
275 ret = send_fd_message(sockfd, (char *)msg,
276 VHOST_USER_HDR_SIZE + msg->size, NULL, 0);
281 /* call back when there is new virtio connection. */
283 vserver_new_vq_conn(int fd, void *dat, __rte_unused int *remove)
285 struct vhost_server *vserver = (struct vhost_server *)dat;
287 struct connfd_ctx *ctx;
291 conn_fd = accept(fd, NULL, NULL);
292 RTE_LOG(INFO, VHOST_CONFIG,
293 "new virtio connection is %d\n", conn_fd);
297 ctx = calloc(1, sizeof(*ctx));
303 vid = vhost_new_device();
310 size = strnlen(vserver->path, PATH_MAX);
311 vhost_set_ifname(vid, vserver->path, size);
313 RTE_LOG(INFO, VHOST_CONFIG, "new device, handle is %d\n", vid);
315 ctx->vserver = vserver;
317 fdset_add(&g_vhost_server.fdset,
318 conn_fd, vserver_message_handler, NULL, ctx);
321 /* callback when there is message on the connfd */
323 vserver_message_handler(int connfd, void *dat, int *remove)
326 struct connfd_ctx *cfd_ctx = (struct connfd_ctx *)dat;
327 struct VhostUserMsg msg;
332 ret = read_vhost_message(connfd, &msg);
333 if (ret <= 0 || msg.request >= VHOST_USER_MAX) {
335 RTE_LOG(ERR, VHOST_CONFIG,
336 "vhost read message failed\n");
338 RTE_LOG(INFO, VHOST_CONFIG,
339 "vhost peer closed\n");
341 RTE_LOG(ERR, VHOST_CONFIG,
342 "vhost read incorrect message\n");
347 vhost_destroy_device(vid);
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 = vhost_get_features(vid, &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 vhost_set_features(vid, &features);
366 case VHOST_USER_GET_PROTOCOL_FEATURES:
367 msg.payload.u64 = VHOST_USER_PROTOCOL_FEATURES;
368 msg.size = sizeof(msg.payload.u64);
369 send_vhost_message(connfd, &msg);
371 case VHOST_USER_SET_PROTOCOL_FEATURES:
372 user_set_protocol_features(vid, msg.payload.u64);
375 case VHOST_USER_SET_OWNER:
376 vhost_set_owner(vid);
378 case VHOST_USER_RESET_OWNER:
379 vhost_reset_owner(vid);
382 case VHOST_USER_SET_MEM_TABLE:
383 user_set_mem_table(vid, &msg);
386 case VHOST_USER_SET_LOG_BASE:
387 user_set_log_base(vid, &msg);
389 /* it needs a reply */
390 msg.size = sizeof(msg.payload.u64);
391 send_vhost_message(connfd, &msg);
393 case VHOST_USER_SET_LOG_FD:
395 RTE_LOG(INFO, VHOST_CONFIG, "not implemented.\n");
398 case VHOST_USER_SET_VRING_NUM:
399 vhost_set_vring_num(vid, &msg.payload.state);
401 case VHOST_USER_SET_VRING_ADDR:
402 vhost_set_vring_addr(vid, &msg.payload.addr);
404 case VHOST_USER_SET_VRING_BASE:
405 vhost_set_vring_base(vid, &msg.payload.state);
408 case VHOST_USER_GET_VRING_BASE:
409 ret = user_get_vring_base(vid, &msg.payload.state);
410 msg.size = sizeof(msg.payload.state);
411 send_vhost_message(connfd, &msg);
414 case VHOST_USER_SET_VRING_KICK:
415 user_set_vring_kick(vid, &msg);
417 case VHOST_USER_SET_VRING_CALL:
418 user_set_vring_call(vid, &msg);
421 case VHOST_USER_SET_VRING_ERR:
422 if (!(msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK))
424 RTE_LOG(INFO, VHOST_CONFIG, "not implemented\n");
427 case VHOST_USER_GET_QUEUE_NUM:
428 msg.payload.u64 = VHOST_MAX_QUEUE_PAIRS;
429 msg.size = sizeof(msg.payload.u64);
430 send_vhost_message(connfd, &msg);
433 case VHOST_USER_SET_VRING_ENABLE:
434 user_set_vring_enable(vid, &msg.payload.state);
436 case VHOST_USER_SEND_RARP:
437 user_send_rarp(vid, &msg);
447 * Creates and initialise the vhost server.
450 rte_vhost_driver_register(const char *path)
452 struct vhost_server *vserver;
454 pthread_mutex_lock(&g_vhost_server.server_mutex);
456 if (g_vhost_server.vserver_cnt == MAX_VHOST_SERVER) {
457 RTE_LOG(ERR, VHOST_CONFIG,
458 "error: the number of servers reaches maximum\n");
459 pthread_mutex_unlock(&g_vhost_server.server_mutex);
463 vserver = calloc(sizeof(struct vhost_server), 1);
464 if (vserver == NULL) {
465 pthread_mutex_unlock(&g_vhost_server.server_mutex);
469 vserver->listenfd = uds_socket(path);
470 if (vserver->listenfd < 0) {
472 pthread_mutex_unlock(&g_vhost_server.server_mutex);
476 vserver->path = strdup(path);
478 fdset_add(&g_vhost_server.fdset, vserver->listenfd,
479 vserver_new_vq_conn, NULL, vserver);
481 g_vhost_server.server[g_vhost_server.vserver_cnt++] = vserver;
482 pthread_mutex_unlock(&g_vhost_server.server_mutex);
489 * Unregister the specified vhost server
492 rte_vhost_driver_unregister(const char *path)
497 pthread_mutex_lock(&g_vhost_server.server_mutex);
499 for (i = 0; i < g_vhost_server.vserver_cnt; i++) {
500 if (!strcmp(g_vhost_server.server[i]->path, path)) {
501 fdset_del(&g_vhost_server.fdset,
502 g_vhost_server.server[i]->listenfd);
504 close(g_vhost_server.server[i]->listenfd);
505 free(g_vhost_server.server[i]->path);
506 free(g_vhost_server.server[i]);
510 count = --g_vhost_server.vserver_cnt;
511 g_vhost_server.server[i] = g_vhost_server.server[count];
512 g_vhost_server.server[count] = NULL;
513 pthread_mutex_unlock(&g_vhost_server.server_mutex);
518 pthread_mutex_unlock(&g_vhost_server.server_mutex);
524 rte_vhost_driver_session_start(void)
526 fdset_event_dispatch(&g_vhost_server.fdset);