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.
41 #include <sys/types.h>
42 #include <sys/socket.h>
48 #include <rte_virtio_net.h>
51 #include "vhost-net-user.h"
52 #include "vhost-net.h"
53 #include "virtio-net-user.h"
56 * Every time rte_vhost_driver_register() is invoked, an associated
57 * vhost_user_socket struct will be created.
59 struct vhost_user_socket {
65 struct vhost_user_connection {
66 struct vhost_user_socket *vsocket;
70 #define MAX_VHOST_SOCKET 1024
72 struct vhost_user_socket *vsockets[MAX_VHOST_SOCKET];
75 pthread_mutex_t mutex;
78 #define MAX_VIRTIO_BACKLOG 128
80 static void vhost_user_server_new_connection(int fd, void *data, int *remove);
81 static void vhost_user_msg_handler(int fd, void *dat, int *remove);
83 static struct vhost_user vhost_user = {
85 .fd = { [0 ... MAX_FDS - 1] = {-1, NULL, NULL, NULL, 0} },
86 .fd_mutex = PTHREAD_MUTEX_INITIALIZER,
90 .mutex = PTHREAD_MUTEX_INITIALIZER,
93 static const char *vhost_message_str[VHOST_USER_MAX] = {
94 [VHOST_USER_NONE] = "VHOST_USER_NONE",
95 [VHOST_USER_GET_FEATURES] = "VHOST_USER_GET_FEATURES",
96 [VHOST_USER_SET_FEATURES] = "VHOST_USER_SET_FEATURES",
97 [VHOST_USER_SET_OWNER] = "VHOST_USER_SET_OWNER",
98 [VHOST_USER_RESET_OWNER] = "VHOST_USER_RESET_OWNER",
99 [VHOST_USER_SET_MEM_TABLE] = "VHOST_USER_SET_MEM_TABLE",
100 [VHOST_USER_SET_LOG_BASE] = "VHOST_USER_SET_LOG_BASE",
101 [VHOST_USER_SET_LOG_FD] = "VHOST_USER_SET_LOG_FD",
102 [VHOST_USER_SET_VRING_NUM] = "VHOST_USER_SET_VRING_NUM",
103 [VHOST_USER_SET_VRING_ADDR] = "VHOST_USER_SET_VRING_ADDR",
104 [VHOST_USER_SET_VRING_BASE] = "VHOST_USER_SET_VRING_BASE",
105 [VHOST_USER_GET_VRING_BASE] = "VHOST_USER_GET_VRING_BASE",
106 [VHOST_USER_SET_VRING_KICK] = "VHOST_USER_SET_VRING_KICK",
107 [VHOST_USER_SET_VRING_CALL] = "VHOST_USER_SET_VRING_CALL",
108 [VHOST_USER_SET_VRING_ERR] = "VHOST_USER_SET_VRING_ERR",
109 [VHOST_USER_GET_PROTOCOL_FEATURES] = "VHOST_USER_GET_PROTOCOL_FEATURES",
110 [VHOST_USER_SET_PROTOCOL_FEATURES] = "VHOST_USER_SET_PROTOCOL_FEATURES",
111 [VHOST_USER_GET_QUEUE_NUM] = "VHOST_USER_GET_QUEUE_NUM",
112 [VHOST_USER_SET_VRING_ENABLE] = "VHOST_USER_SET_VRING_ENABLE",
113 [VHOST_USER_SEND_RARP] = "VHOST_USER_SEND_RARP",
116 /* return bytes# of read on success or negative val on failure. */
118 read_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
122 size_t fdsize = fd_num * sizeof(int);
123 char control[CMSG_SPACE(fdsize)];
124 struct cmsghdr *cmsg;
127 memset(&msgh, 0, sizeof(msgh));
129 iov.iov_len = buflen;
133 msgh.msg_control = control;
134 msgh.msg_controllen = sizeof(control);
136 ret = recvmsg(sockfd, &msgh, 0);
138 RTE_LOG(ERR, VHOST_CONFIG, "recvmsg failed\n");
142 if (msgh.msg_flags & (MSG_TRUNC | MSG_CTRUNC)) {
143 RTE_LOG(ERR, VHOST_CONFIG, "truncted msg\n");
147 for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
148 cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
149 if ((cmsg->cmsg_level == SOL_SOCKET) &&
150 (cmsg->cmsg_type == SCM_RIGHTS)) {
151 memcpy(fds, CMSG_DATA(cmsg), fdsize);
159 /* return bytes# of read on success or negative val on failure. */
161 read_vhost_message(int sockfd, struct VhostUserMsg *msg)
165 ret = read_fd_message(sockfd, (char *)msg, VHOST_USER_HDR_SIZE,
166 msg->fds, VHOST_MEMORY_MAX_NREGIONS);
170 if (msg && msg->size) {
171 if (msg->size > sizeof(msg->payload)) {
172 RTE_LOG(ERR, VHOST_CONFIG,
173 "invalid msg size: %d\n", msg->size);
176 ret = read(sockfd, &msg->payload, msg->size);
179 if (ret != (int)msg->size) {
180 RTE_LOG(ERR, VHOST_CONFIG,
181 "read control message failed\n");
190 send_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
195 size_t fdsize = fd_num * sizeof(int);
196 char control[CMSG_SPACE(fdsize)];
197 struct cmsghdr *cmsg;
200 memset(&msgh, 0, sizeof(msgh));
202 iov.iov_len = buflen;
207 if (fds && fd_num > 0) {
208 msgh.msg_control = control;
209 msgh.msg_controllen = sizeof(control);
210 cmsg = CMSG_FIRSTHDR(&msgh);
211 cmsg->cmsg_len = CMSG_LEN(fdsize);
212 cmsg->cmsg_level = SOL_SOCKET;
213 cmsg->cmsg_type = SCM_RIGHTS;
214 memcpy(CMSG_DATA(cmsg), fds, fdsize);
216 msgh.msg_control = NULL;
217 msgh.msg_controllen = 0;
221 ret = sendmsg(sockfd, &msgh, 0);
222 } while (ret < 0 && errno == EINTR);
225 RTE_LOG(ERR, VHOST_CONFIG, "sendmsg error\n");
233 send_vhost_message(int sockfd, struct VhostUserMsg *msg)
240 msg->flags &= ~VHOST_USER_VERSION_MASK;
241 msg->flags |= VHOST_USER_VERSION;
242 msg->flags |= VHOST_USER_REPLY_MASK;
244 ret = send_fd_message(sockfd, (char *)msg,
245 VHOST_USER_HDR_SIZE + msg->size, NULL, 0);
252 vhost_user_add_connection(int fd, struct vhost_user_socket *vsocket)
256 struct vhost_user_connection *conn;
258 conn = malloc(sizeof(*conn));
264 vid = vhost_new_device();
271 size = strnlen(vsocket->path, PATH_MAX);
272 vhost_set_ifname(vid, vsocket->path, size);
274 RTE_LOG(INFO, VHOST_CONFIG, "new device, handle is %d\n", vid);
276 conn->vsocket = vsocket;
278 fdset_add(&vhost_user.fdset, fd, vhost_user_msg_handler, NULL, conn);
281 /* call back when there is new vhost-user connection from client */
283 vhost_user_server_new_connection(int fd, void *dat, int *remove __rte_unused)
285 struct vhost_user_socket *vsocket = dat;
287 fd = accept(fd, NULL, NULL);
291 RTE_LOG(INFO, VHOST_CONFIG, "new vhost user connection is %d\n", fd);
292 vhost_user_add_connection(fd, vsocket);
295 /* callback when there is message on the connfd */
297 vhost_user_msg_handler(int connfd, void *dat, int *remove)
300 struct vhost_user_connection *conn = dat;
301 struct VhostUserMsg msg;
306 ret = read_vhost_message(connfd, &msg);
307 if (ret <= 0 || msg.request >= VHOST_USER_MAX) {
309 RTE_LOG(ERR, VHOST_CONFIG,
310 "vhost read message failed\n");
312 RTE_LOG(INFO, VHOST_CONFIG,
313 "vhost peer closed\n");
315 RTE_LOG(ERR, VHOST_CONFIG,
316 "vhost read incorrect message\n");
321 vhost_destroy_device(vid);
326 RTE_LOG(INFO, VHOST_CONFIG, "read message %s\n",
327 vhost_message_str[msg.request]);
328 switch (msg.request) {
329 case VHOST_USER_GET_FEATURES:
330 ret = vhost_get_features(vid, &features);
331 msg.payload.u64 = features;
332 msg.size = sizeof(msg.payload.u64);
333 send_vhost_message(connfd, &msg);
335 case VHOST_USER_SET_FEATURES:
336 features = msg.payload.u64;
337 vhost_set_features(vid, &features);
340 case VHOST_USER_GET_PROTOCOL_FEATURES:
341 msg.payload.u64 = VHOST_USER_PROTOCOL_FEATURES;
342 msg.size = sizeof(msg.payload.u64);
343 send_vhost_message(connfd, &msg);
345 case VHOST_USER_SET_PROTOCOL_FEATURES:
346 user_set_protocol_features(vid, msg.payload.u64);
349 case VHOST_USER_SET_OWNER:
350 vhost_set_owner(vid);
352 case VHOST_USER_RESET_OWNER:
353 vhost_reset_owner(vid);
356 case VHOST_USER_SET_MEM_TABLE:
357 user_set_mem_table(vid, &msg);
360 case VHOST_USER_SET_LOG_BASE:
361 user_set_log_base(vid, &msg);
363 /* it needs a reply */
364 msg.size = sizeof(msg.payload.u64);
365 send_vhost_message(connfd, &msg);
367 case VHOST_USER_SET_LOG_FD:
369 RTE_LOG(INFO, VHOST_CONFIG, "not implemented.\n");
372 case VHOST_USER_SET_VRING_NUM:
373 vhost_set_vring_num(vid, &msg.payload.state);
375 case VHOST_USER_SET_VRING_ADDR:
376 vhost_set_vring_addr(vid, &msg.payload.addr);
378 case VHOST_USER_SET_VRING_BASE:
379 vhost_set_vring_base(vid, &msg.payload.state);
382 case VHOST_USER_GET_VRING_BASE:
383 ret = user_get_vring_base(vid, &msg.payload.state);
384 msg.size = sizeof(msg.payload.state);
385 send_vhost_message(connfd, &msg);
388 case VHOST_USER_SET_VRING_KICK:
389 user_set_vring_kick(vid, &msg);
391 case VHOST_USER_SET_VRING_CALL:
392 user_set_vring_call(vid, &msg);
395 case VHOST_USER_SET_VRING_ERR:
396 if (!(msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK))
398 RTE_LOG(INFO, VHOST_CONFIG, "not implemented\n");
401 case VHOST_USER_GET_QUEUE_NUM:
402 msg.payload.u64 = VHOST_MAX_QUEUE_PAIRS;
403 msg.size = sizeof(msg.payload.u64);
404 send_vhost_message(connfd, &msg);
407 case VHOST_USER_SET_VRING_ENABLE:
408 user_set_vring_enable(vid, &msg.payload.state);
410 case VHOST_USER_SEND_RARP:
411 user_send_rarp(vid, &msg);
421 create_unix_socket(const char *path, struct sockaddr_un *un, bool is_server)
425 fd = socket(AF_UNIX, SOCK_STREAM, 0);
428 RTE_LOG(INFO, VHOST_CONFIG, "vhost-user %s: socket created, fd: %d\n",
429 is_server ? "server" : "client", fd);
431 memset(un, 0, sizeof(*un));
432 un->sun_family = AF_UNIX;
433 strncpy(un->sun_path, path, sizeof(un->sun_path));
439 vhost_user_create_server(struct vhost_user_socket *vsocket)
443 struct sockaddr_un un;
444 const char *path = vsocket->path;
446 fd = create_unix_socket(path, &un, vsocket->is_server);
450 ret = bind(fd, (struct sockaddr *)&un, sizeof(un));
452 RTE_LOG(ERR, VHOST_CONFIG,
453 "failed to bind to %s: %s; remove it and try again\n",
454 path, strerror(errno));
457 RTE_LOG(INFO, VHOST_CONFIG, "bind to %s\n", path);
459 ret = listen(fd, MAX_VIRTIO_BACKLOG);
463 vsocket->listenfd = fd;
464 fdset_add(&vhost_user.fdset, fd, vhost_user_server_new_connection,
475 vhost_user_create_client(struct vhost_user_socket *vsocket)
479 struct sockaddr_un un;
480 const char *path = vsocket->path;
482 fd = create_unix_socket(path, &un, vsocket->is_server);
486 ret = connect(fd, (struct sockaddr *)&un, sizeof(un));
488 RTE_LOG(ERR, VHOST_CONFIG, "failed to connect to %s: %s\n",
489 path, strerror(errno));
494 vhost_user_add_connection(fd, vsocket);
500 * Register a new vhost-user socket; here we could act as server
501 * (the default case), or client (when RTE_VHOST_USER_CLIENT) flag
505 rte_vhost_driver_register(const char *path, uint64_t flags)
508 struct vhost_user_socket *vsocket;
513 pthread_mutex_lock(&vhost_user.mutex);
515 if (vhost_user.vsocket_cnt == MAX_VHOST_SOCKET) {
516 RTE_LOG(ERR, VHOST_CONFIG,
517 "error: the number of vhost sockets reaches maximum\n");
521 vsocket = malloc(sizeof(struct vhost_user_socket));
524 memset(vsocket, 0, sizeof(struct vhost_user_socket));
525 vsocket->path = strdup(path);
527 if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
528 ret = vhost_user_create_client(vsocket);
530 vsocket->is_server = true;
531 ret = vhost_user_create_server(vsocket);
539 vhost_user.vsockets[vhost_user.vsocket_cnt++] = vsocket;
542 pthread_mutex_unlock(&vhost_user.mutex);
548 * Unregister the specified vhost socket
551 rte_vhost_driver_unregister(const char *path)
556 pthread_mutex_lock(&vhost_user.mutex);
558 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
559 if (!strcmp(vhost_user.vsockets[i]->path, path)) {
560 if (vhost_user.vsockets[i]->is_server) {
561 fdset_del(&vhost_user.fdset,
562 vhost_user.vsockets[i]->listenfd);
563 close(vhost_user.vsockets[i]->listenfd);
567 free(vhost_user.vsockets[i]->path);
568 free(vhost_user.vsockets[i]);
570 count = --vhost_user.vsocket_cnt;
571 vhost_user.vsockets[i] = vhost_user.vsockets[count];
572 vhost_user.vsockets[count] = NULL;
573 pthread_mutex_unlock(&vhost_user.mutex);
578 pthread_mutex_unlock(&vhost_user.mutex);
584 rte_vhost_driver_session_start(void)
586 fdset_event_dispatch(&vhost_user.fdset);