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"
55 * Every time rte_vhost_driver_register() is invoked, an associated
56 * vhost_user_socket struct will be created.
58 struct vhost_user_socket {
63 struct vhost_user_connection {
64 struct vhost_user_socket *vsocket;
68 #define MAX_VHOST_SOCKET 1024
70 struct vhost_user_socket *vsockets[MAX_VHOST_SOCKET];
73 pthread_mutex_t mutex;
76 #define MAX_VIRTIO_BACKLOG 128
78 static void vhost_user_new_connection(int fd, void *data, int *remove);
79 static void vhost_user_msg_handler(int fd, void *dat, int *remove);
81 static struct vhost_user vhost_user = {
83 .fd = { [0 ... MAX_FDS - 1] = {-1, NULL, NULL, NULL, 0} },
84 .fd_mutex = PTHREAD_MUTEX_INITIALIZER,
88 .mutex = PTHREAD_MUTEX_INITIALIZER,
91 static const char *vhost_message_str[VHOST_USER_MAX] = {
92 [VHOST_USER_NONE] = "VHOST_USER_NONE",
93 [VHOST_USER_GET_FEATURES] = "VHOST_USER_GET_FEATURES",
94 [VHOST_USER_SET_FEATURES] = "VHOST_USER_SET_FEATURES",
95 [VHOST_USER_SET_OWNER] = "VHOST_USER_SET_OWNER",
96 [VHOST_USER_RESET_OWNER] = "VHOST_USER_RESET_OWNER",
97 [VHOST_USER_SET_MEM_TABLE] = "VHOST_USER_SET_MEM_TABLE",
98 [VHOST_USER_SET_LOG_BASE] = "VHOST_USER_SET_LOG_BASE",
99 [VHOST_USER_SET_LOG_FD] = "VHOST_USER_SET_LOG_FD",
100 [VHOST_USER_SET_VRING_NUM] = "VHOST_USER_SET_VRING_NUM",
101 [VHOST_USER_SET_VRING_ADDR] = "VHOST_USER_SET_VRING_ADDR",
102 [VHOST_USER_SET_VRING_BASE] = "VHOST_USER_SET_VRING_BASE",
103 [VHOST_USER_GET_VRING_BASE] = "VHOST_USER_GET_VRING_BASE",
104 [VHOST_USER_SET_VRING_KICK] = "VHOST_USER_SET_VRING_KICK",
105 [VHOST_USER_SET_VRING_CALL] = "VHOST_USER_SET_VRING_CALL",
106 [VHOST_USER_SET_VRING_ERR] = "VHOST_USER_SET_VRING_ERR",
107 [VHOST_USER_GET_PROTOCOL_FEATURES] = "VHOST_USER_GET_PROTOCOL_FEATURES",
108 [VHOST_USER_SET_PROTOCOL_FEATURES] = "VHOST_USER_SET_PROTOCOL_FEATURES",
109 [VHOST_USER_GET_QUEUE_NUM] = "VHOST_USER_GET_QUEUE_NUM",
110 [VHOST_USER_SET_VRING_ENABLE] = "VHOST_USER_SET_VRING_ENABLE",
111 [VHOST_USER_SEND_RARP] = "VHOST_USER_SEND_RARP",
115 * Create a unix domain socket, bind to path and listen for connection.
117 * socket fd or -1 on failure
120 uds_socket(const char *path)
122 struct sockaddr_un un;
129 sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
132 RTE_LOG(INFO, VHOST_CONFIG, "socket created, fd:%d\n", sockfd);
134 memset(&un, 0, sizeof(un));
135 un.sun_family = AF_UNIX;
136 snprintf(un.sun_path, sizeof(un.sun_path), "%s", path);
137 ret = bind(sockfd, (struct sockaddr *)&un, sizeof(un));
139 RTE_LOG(ERR, VHOST_CONFIG, "fail to bind fd:%d, remove file:%s and try again.\n",
143 RTE_LOG(INFO, VHOST_CONFIG, "bind to %s\n", path);
145 ret = listen(sockfd, MAX_VIRTIO_BACKLOG);
156 /* return bytes# of read on success or negative val on failure. */
158 read_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
162 size_t fdsize = fd_num * sizeof(int);
163 char control[CMSG_SPACE(fdsize)];
164 struct cmsghdr *cmsg;
167 memset(&msgh, 0, sizeof(msgh));
169 iov.iov_len = buflen;
173 msgh.msg_control = control;
174 msgh.msg_controllen = sizeof(control);
176 ret = recvmsg(sockfd, &msgh, 0);
178 RTE_LOG(ERR, VHOST_CONFIG, "recvmsg failed\n");
182 if (msgh.msg_flags & (MSG_TRUNC | MSG_CTRUNC)) {
183 RTE_LOG(ERR, VHOST_CONFIG, "truncted msg\n");
187 for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
188 cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
189 if ((cmsg->cmsg_level == SOL_SOCKET) &&
190 (cmsg->cmsg_type == SCM_RIGHTS)) {
191 memcpy(fds, CMSG_DATA(cmsg), fdsize);
199 /* return bytes# of read on success or negative val on failure. */
201 read_vhost_message(int sockfd, struct VhostUserMsg *msg)
205 ret = read_fd_message(sockfd, (char *)msg, VHOST_USER_HDR_SIZE,
206 msg->fds, VHOST_MEMORY_MAX_NREGIONS);
210 if (msg && msg->size) {
211 if (msg->size > sizeof(msg->payload)) {
212 RTE_LOG(ERR, VHOST_CONFIG,
213 "invalid msg size: %d\n", msg->size);
216 ret = read(sockfd, &msg->payload, msg->size);
219 if (ret != (int)msg->size) {
220 RTE_LOG(ERR, VHOST_CONFIG,
221 "read control message failed\n");
230 send_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
235 size_t fdsize = fd_num * sizeof(int);
236 char control[CMSG_SPACE(fdsize)];
237 struct cmsghdr *cmsg;
240 memset(&msgh, 0, sizeof(msgh));
242 iov.iov_len = buflen;
247 if (fds && fd_num > 0) {
248 msgh.msg_control = control;
249 msgh.msg_controllen = sizeof(control);
250 cmsg = CMSG_FIRSTHDR(&msgh);
251 cmsg->cmsg_len = CMSG_LEN(fdsize);
252 cmsg->cmsg_level = SOL_SOCKET;
253 cmsg->cmsg_type = SCM_RIGHTS;
254 memcpy(CMSG_DATA(cmsg), fds, fdsize);
256 msgh.msg_control = NULL;
257 msgh.msg_controllen = 0;
261 ret = sendmsg(sockfd, &msgh, 0);
262 } while (ret < 0 && errno == EINTR);
265 RTE_LOG(ERR, VHOST_CONFIG, "sendmsg error\n");
273 send_vhost_message(int sockfd, struct VhostUserMsg *msg)
280 msg->flags &= ~VHOST_USER_VERSION_MASK;
281 msg->flags |= VHOST_USER_VERSION;
282 msg->flags |= VHOST_USER_REPLY_MASK;
284 ret = send_fd_message(sockfd, (char *)msg,
285 VHOST_USER_HDR_SIZE + msg->size, NULL, 0);
290 /* call back when there is new vhost-user connection. */
292 vhost_user_new_connection(int fd, void *dat, int *remove __rte_unused)
294 struct vhost_user_socket *vsocket = dat;
296 struct vhost_user_connection *conn;
300 conn_fd = accept(fd, NULL, NULL);
301 RTE_LOG(INFO, VHOST_CONFIG,
302 "new virtio connection is %d\n", conn_fd);
306 conn = calloc(1, sizeof(*conn));
312 vid = vhost_new_device();
319 size = strnlen(vsocket->path, PATH_MAX);
320 vhost_set_ifname(vid, vsocket->path, size);
322 RTE_LOG(INFO, VHOST_CONFIG, "new device, handle is %d\n", vid);
324 conn->vsocket = vsocket;
326 fdset_add(&vhost_user.fdset,
327 conn_fd, vhost_user_msg_handler, NULL, conn);
330 /* callback when there is message on the connfd */
332 vhost_user_msg_handler(int connfd, void *dat, int *remove)
335 struct vhost_user_connection *conn = dat;
336 struct VhostUserMsg msg;
341 ret = read_vhost_message(connfd, &msg);
342 if (ret <= 0 || msg.request >= VHOST_USER_MAX) {
344 RTE_LOG(ERR, VHOST_CONFIG,
345 "vhost read message failed\n");
347 RTE_LOG(INFO, VHOST_CONFIG,
348 "vhost peer closed\n");
350 RTE_LOG(ERR, VHOST_CONFIG,
351 "vhost read incorrect message\n");
356 vhost_destroy_device(vid);
361 RTE_LOG(INFO, VHOST_CONFIG, "read message %s\n",
362 vhost_message_str[msg.request]);
363 switch (msg.request) {
364 case VHOST_USER_GET_FEATURES:
365 ret = vhost_get_features(vid, &features);
366 msg.payload.u64 = features;
367 msg.size = sizeof(msg.payload.u64);
368 send_vhost_message(connfd, &msg);
370 case VHOST_USER_SET_FEATURES:
371 features = msg.payload.u64;
372 vhost_set_features(vid, &features);
375 case VHOST_USER_GET_PROTOCOL_FEATURES:
376 msg.payload.u64 = VHOST_USER_PROTOCOL_FEATURES;
377 msg.size = sizeof(msg.payload.u64);
378 send_vhost_message(connfd, &msg);
380 case VHOST_USER_SET_PROTOCOL_FEATURES:
381 user_set_protocol_features(vid, msg.payload.u64);
384 case VHOST_USER_SET_OWNER:
385 vhost_set_owner(vid);
387 case VHOST_USER_RESET_OWNER:
388 vhost_reset_owner(vid);
391 case VHOST_USER_SET_MEM_TABLE:
392 user_set_mem_table(vid, &msg);
395 case VHOST_USER_SET_LOG_BASE:
396 user_set_log_base(vid, &msg);
398 /* it needs a reply */
399 msg.size = sizeof(msg.payload.u64);
400 send_vhost_message(connfd, &msg);
402 case VHOST_USER_SET_LOG_FD:
404 RTE_LOG(INFO, VHOST_CONFIG, "not implemented.\n");
407 case VHOST_USER_SET_VRING_NUM:
408 vhost_set_vring_num(vid, &msg.payload.state);
410 case VHOST_USER_SET_VRING_ADDR:
411 vhost_set_vring_addr(vid, &msg.payload.addr);
413 case VHOST_USER_SET_VRING_BASE:
414 vhost_set_vring_base(vid, &msg.payload.state);
417 case VHOST_USER_GET_VRING_BASE:
418 ret = user_get_vring_base(vid, &msg.payload.state);
419 msg.size = sizeof(msg.payload.state);
420 send_vhost_message(connfd, &msg);
423 case VHOST_USER_SET_VRING_KICK:
424 user_set_vring_kick(vid, &msg);
426 case VHOST_USER_SET_VRING_CALL:
427 user_set_vring_call(vid, &msg);
430 case VHOST_USER_SET_VRING_ERR:
431 if (!(msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK))
433 RTE_LOG(INFO, VHOST_CONFIG, "not implemented\n");
436 case VHOST_USER_GET_QUEUE_NUM:
437 msg.payload.u64 = VHOST_MAX_QUEUE_PAIRS;
438 msg.size = sizeof(msg.payload.u64);
439 send_vhost_message(connfd, &msg);
442 case VHOST_USER_SET_VRING_ENABLE:
443 user_set_vring_enable(vid, &msg.payload.state);
445 case VHOST_USER_SEND_RARP:
446 user_send_rarp(vid, &msg);
456 * Creates and initialise the vhost server.
459 rte_vhost_driver_register(const char *path)
461 struct vhost_user_socket *vsocket;
463 pthread_mutex_lock(&vhost_user.mutex);
465 if (vhost_user.vsocket_cnt == MAX_VHOST_SOCKET) {
466 RTE_LOG(ERR, VHOST_CONFIG,
467 "error: the number of servers reaches maximum\n");
468 pthread_mutex_unlock(&vhost_user.mutex);
472 vsocket = calloc(sizeof(struct vhost_user_socket), 1);
473 if (vsocket == NULL) {
474 pthread_mutex_unlock(&vhost_user.mutex);
478 vsocket->listenfd = uds_socket(path);
479 if (vsocket->listenfd < 0) {
481 pthread_mutex_unlock(&vhost_user.mutex);
485 vsocket->path = strdup(path);
487 fdset_add(&vhost_user.fdset, vsocket->listenfd,
488 vhost_user_new_connection, NULL, vsocket);
490 vhost_user.vsockets[vhost_user.vsocket_cnt++] = vsocket;
491 pthread_mutex_unlock(&vhost_user.mutex);
498 * Unregister the specified vhost server
501 rte_vhost_driver_unregister(const char *path)
506 pthread_mutex_lock(&vhost_user.mutex);
508 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
509 if (!strcmp(vhost_user.vsockets[i]->path, path)) {
510 fdset_del(&vhost_user.fdset,
511 vhost_user.vsockets[i]->listenfd);
513 close(vhost_user.vsockets[i]->listenfd);
514 free(vhost_user.vsockets[i]->path);
515 free(vhost_user.vsockets[i]);
519 count = --vhost_user.vsocket_cnt;
520 vhost_user.vsockets[i] = vhost_user.vsockets[count];
521 vhost_user.vsockets[count] = NULL;
522 pthread_mutex_unlock(&vhost_user.mutex);
527 pthread_mutex_unlock(&vhost_user.mutex);
533 rte_vhost_driver_session_start(void)
535 fdset_event_dispatch(&vhost_user.fdset);