vhost: support vhost-user
[dpdk.git] / lib / librte_vhost / vhost_user / vhost-net-user.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33
34 #include <stdint.h>
35 #include <stdio.h>
36 #include <limits.h>
37 #include <stdlib.h>
38 #include <unistd.h>
39 #include <string.h>
40 #include <sys/types.h>
41 #include <sys/socket.h>
42 #include <sys/un.h>
43 #include <errno.h>
44
45 #include <rte_log.h>
46 #include <rte_virtio_net.h>
47
48 #include "fd_man.h"
49 #include "vhost-net-user.h"
50 #include "vhost-net.h"
51 #include "virtio-net-user.h"
52
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;
57
58 struct connfd_ctx {
59         struct vhost_server *vserver;
60         uint32_t fh;
61 };
62
63 #define MAX_VHOST_SERVER 1024
64 static struct {
65         struct vhost_server *server[MAX_VHOST_SERVER];
66         struct fdset fdset;     /**< The fd list this vhost server manages. */
67 } g_vhost_server;
68
69 static int vserver_idx;
70
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"
87 };
88
89 /**
90  * Create a unix domain socket, bind to path and listen for connection.
91  * @return
92  *  socket fd or -1 on failure
93  */
94 static int
95 uds_socket(const char *path)
96 {
97         struct sockaddr_un un;
98         int sockfd;
99         int ret;
100
101         if (path == NULL)
102                 return -1;
103
104         sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
105         if (sockfd < 0)
106                 return -1;
107         RTE_LOG(INFO, VHOST_CONFIG, "socket created, fd:%d\n", sockfd);
108
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));
113         if (ret == -1)
114                 goto err;
115         RTE_LOG(INFO, VHOST_CONFIG, "bind to %s\n", path);
116
117         ret = listen(sockfd, MAX_VIRTIO_BACKLOG);
118         if (ret == -1)
119                 goto err;
120
121         return sockfd;
122
123 err:
124         close(sockfd);
125         return -1;
126 }
127
128 /* return bytes# of read on success or negative val on failure. */
129 static int
130 read_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
131 {
132         struct iovec iov;
133         struct msghdr msgh;
134         size_t fdsize = fd_num * sizeof(int);
135         char control[CMSG_SPACE(fdsize)];
136         struct cmsghdr *cmsg;
137         int ret;
138
139         memset(&msgh, 0, sizeof(msgh));
140         iov.iov_base = buf;
141         iov.iov_len  = buflen;
142
143         msgh.msg_iov = &iov;
144         msgh.msg_iovlen = 1;
145         msgh.msg_control = control;
146         msgh.msg_controllen = sizeof(control);
147
148         ret = recvmsg(sockfd, &msgh, 0);
149         if (ret <= 0) {
150                 RTE_LOG(ERR, VHOST_CONFIG, "recvmsg failed\n");
151                 return ret;
152         }
153
154         if (msgh.msg_flags & (MSG_TRUNC | MSG_CTRUNC)) {
155                 RTE_LOG(ERR, VHOST_CONFIG, "truncted msg\n");
156                 return -1;
157         }
158
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);
164                         break;
165                 }
166         }
167
168         return ret;
169 }
170
171 /* return bytes# of read on success or negative val on failure. */
172 static int
173 read_vhost_message(int sockfd, struct VhostUserMsg *msg)
174 {
175         int ret;
176
177         ret = read_fd_message(sockfd, (char *)msg, VHOST_USER_HDR_SIZE,
178                 msg->fds, VHOST_MEMORY_MAX_NREGIONS);
179         if (ret <= 0)
180                 return ret;
181
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);
186                         return -1;
187                 }
188                 ret = read(sockfd, &msg->payload, msg->size);
189                 if (ret <= 0)
190                         return ret;
191                 if (ret != (int)msg->size) {
192                         RTE_LOG(ERR, VHOST_CONFIG,
193                                 "read control message failed\n");
194                         return -1;
195                 }
196         }
197
198         return ret;
199 }
200
201 static int
202 send_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
203 {
204
205         struct iovec iov;
206         struct msghdr msgh;
207         size_t fdsize = fd_num * sizeof(int);
208         char control[CMSG_SPACE(fdsize)];
209         struct cmsghdr *cmsg;
210         int ret;
211
212         memset(&msgh, 0, sizeof(msgh));
213         iov.iov_base = buf;
214         iov.iov_len = buflen;
215
216         msgh.msg_iov = &iov;
217         msgh.msg_iovlen = 1;
218
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);
227         } else {
228                 msgh.msg_control = NULL;
229                 msgh.msg_controllen = 0;
230         }
231
232         do {
233                 ret = sendmsg(sockfd, &msgh, 0);
234         } while (ret < 0 && errno == EINTR);
235
236         if (ret < 0) {
237                 RTE_LOG(ERR, VHOST_CONFIG,  "sendmsg error\n");
238                 return ret;
239         }
240
241         return ret;
242 }
243
244 static int
245 send_vhost_message(int sockfd, struct VhostUserMsg *msg)
246 {
247         int ret;
248
249         if (!msg)
250                 return 0;
251
252         msg->flags &= ~VHOST_USER_VERSION_MASK;
253         msg->flags |= VHOST_USER_VERSION;
254         msg->flags |= VHOST_USER_REPLY_MASK;
255
256         ret = send_fd_message(sockfd, (char *)msg,
257                 VHOST_USER_HDR_SIZE + msg->size, NULL, 0);
258
259         return ret;
260 }
261
262 /* call back when there is new virtio connection.  */
263 static void
264 vserver_new_vq_conn(int fd, void *dat)
265 {
266         struct vhost_server *vserver = (struct vhost_server *)dat;
267         int conn_fd;
268         struct connfd_ctx *ctx;
269         int fh;
270         struct vhost_device_ctx vdev_ctx = { 0 };
271
272         conn_fd = accept(fd, NULL, NULL);
273         RTE_LOG(INFO, VHOST_CONFIG,
274                 "new virtio connection is %d\n", conn_fd);
275         if (conn_fd < 0)
276                 return;
277
278         ctx = calloc(1, sizeof(*ctx));
279         if (ctx == NULL) {
280                 close(conn_fd);
281                 return;
282         }
283
284         fh = ops->new_device(vdev_ctx);
285         if (fh == -1) {
286                 free(ctx);
287                 close(conn_fd);
288                 return;
289         }
290         RTE_LOG(INFO, VHOST_CONFIG, "new device, handle is %d\n", fh);
291
292         ctx->vserver = vserver;
293         ctx->fh = fh;
294         fdset_add(&g_vhost_server.fdset,
295                 conn_fd, vserver_message_handler, NULL, ctx);
296 }
297
298 /* callback when there is message on the connfd */
299 static void
300 vserver_message_handler(int connfd, void *dat)
301 {
302         struct vhost_device_ctx ctx;
303         struct connfd_ctx *cfd_ctx = (struct connfd_ctx *)dat;
304         struct VhostUserMsg msg;
305         uint64_t features;
306         int ret;
307
308         ctx.fh = cfd_ctx->fh;
309         ret = read_vhost_message(connfd, &msg);
310         if (ret < 0) {
311                 RTE_LOG(ERR, VHOST_CONFIG,
312                         "vhost read message failed\n");
313
314                 close(connfd);
315                 fdset_del(&g_vhost_server.fdset, connfd);
316                 free(cfd_ctx);
317                 user_destroy_device(ctx);
318                 ops->destroy_device(ctx);
319
320                 return;
321         } else if (ret == 0) {
322                 RTE_LOG(INFO, VHOST_CONFIG,
323                         "vhost peer closed\n");
324
325                 close(connfd);
326                 fdset_del(&g_vhost_server.fdset, connfd);
327                 free(cfd_ctx);
328                 user_destroy_device(ctx);
329                 ops->destroy_device(ctx);
330
331                 return;
332         }
333         if (msg.request > VHOST_USER_MAX) {
334                 RTE_LOG(ERR, VHOST_CONFIG,
335                         "vhost read incorrect message\n");
336
337                 close(connfd);
338                 fdset_del(&g_vhost_server.fdset, connfd);
339                 free(cfd_ctx);
340                 user_destroy_device(ctx);
341                 ops->destroy_device(ctx);
342
343                 return;
344         }
345
346         RTE_LOG(INFO, VHOST_CONFIG, "read message %s\n",
347                 vhost_message_str[msg.request]);
348         switch (msg.request) {
349         case VHOST_USER_GET_FEATURES:
350                 ret = ops->get_features(ctx, &features);
351                 msg.payload.u64 = features;
352                 msg.size = sizeof(msg.payload.u64);
353                 send_vhost_message(connfd, &msg);
354                 break;
355         case VHOST_USER_SET_FEATURES:
356                 features = msg.payload.u64;
357                 ops->set_features(ctx, &features);
358                 break;
359
360         case VHOST_USER_SET_OWNER:
361                 ops->set_owner(ctx);
362                 break;
363         case VHOST_USER_RESET_OWNER:
364                 ops->reset_owner(ctx);
365                 break;
366
367         case VHOST_USER_SET_MEM_TABLE:
368                 user_set_mem_table(ctx, &msg);
369                 break;
370
371         case VHOST_USER_SET_LOG_BASE:
372                 RTE_LOG(INFO, VHOST_CONFIG, "not implemented.\n");
373         case VHOST_USER_SET_LOG_FD:
374                 close(msg.fds[0]);
375                 RTE_LOG(INFO, VHOST_CONFIG, "not implemented.\n");
376                 break;
377
378         case VHOST_USER_SET_VRING_NUM:
379                 ops->set_vring_num(ctx, &msg.payload.state);
380                 break;
381         case VHOST_USER_SET_VRING_ADDR:
382                 ops->set_vring_addr(ctx, &msg.payload.addr);
383                 break;
384         case VHOST_USER_SET_VRING_BASE:
385                 ops->set_vring_base(ctx, &msg.payload.state);
386                 break;
387
388         case VHOST_USER_GET_VRING_BASE:
389                 ret = user_get_vring_base(ctx, &msg.payload.state);
390                 msg.size = sizeof(msg.payload.state);
391                 send_vhost_message(connfd, &msg);
392                 break;
393
394         case VHOST_USER_SET_VRING_KICK:
395                 user_set_vring_kick(ctx, &msg);
396                 break;
397         case VHOST_USER_SET_VRING_CALL:
398                 user_set_vring_call(ctx, &msg);
399                 break;
400
401         case VHOST_USER_SET_VRING_ERR:
402                 if (!(msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK))
403                         close(msg.fds[0]);
404                 RTE_LOG(INFO, VHOST_CONFIG, "not implemented\n");
405                 break;
406
407         default:
408                 break;
409
410         }
411 }
412
413
414 /**
415  * Creates and initialise the vhost server.
416  */
417 int
418 rte_vhost_driver_register(const char *path)
419 {
420         struct vhost_server *vserver;
421
422         if (vserver_idx == 0) {
423                 fdset_init(&g_vhost_server.fdset);
424                 ops = get_virtio_net_callbacks();
425         }
426         if (vserver_idx == MAX_VHOST_SERVER)
427                 return -1;
428
429         vserver = calloc(sizeof(struct vhost_server), 1);
430         if (vserver == NULL)
431                 return -1;
432
433         unlink(path);
434
435         vserver->listenfd = uds_socket(path);
436         if (vserver->listenfd < 0) {
437                 free(vserver);
438                 return -1;
439         }
440         vserver->path = path;
441
442         fdset_add(&g_vhost_server.fdset, vserver->listenfd,
443                 vserver_new_vq_conn, NULL,
444                 vserver);
445
446         g_vhost_server.server[vserver_idx++] = vserver;
447
448         return 0;
449 }
450
451
452 int
453 rte_vhost_driver_session_start(void)
454 {
455         fdset_event_dispatch(&g_vhost_server.fdset);
456         return 0;
457 }