31f1215a91027fa842f799b1570af34693cb24d7
[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 #include <pthread.h>
45
46 #include <rte_log.h>
47 #include <rte_virtio_net.h>
48
49 #include "fd_man.h"
50 #include "vhost-net-user.h"
51 #include "vhost-net.h"
52 #include "virtio-net-user.h"
53
54 #define MAX_VIRTIO_BACKLOG 128
55
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;
59
60 struct connfd_ctx {
61         struct vhost_server *vserver;
62         uint32_t fh;
63 };
64
65 #define MAX_VHOST_SERVER 1024
66 struct _vhost_server {
67         struct vhost_server *server[MAX_VHOST_SERVER];
68         struct fdset fdset;
69 };
70
71 static struct _vhost_server g_vhost_server = {
72         .fdset = {
73                 .fd = { [0 ... MAX_FDS - 1] = {-1, NULL, NULL, NULL, 0} },
74                 .fd_mutex = PTHREAD_MUTEX_INITIALIZER,
75                 .num = 0
76         },
77 };
78
79 static int vserver_idx;
80
81 static const char *vhost_message_str[VHOST_USER_MAX] = {
82         [VHOST_USER_NONE] = "VHOST_USER_NONE",
83         [VHOST_USER_GET_FEATURES] = "VHOST_USER_GET_FEATURES",
84         [VHOST_USER_SET_FEATURES] = "VHOST_USER_SET_FEATURES",
85         [VHOST_USER_SET_OWNER] = "VHOST_USER_SET_OWNER",
86         [VHOST_USER_RESET_OWNER] = "VHOST_USER_RESET_OWNER",
87         [VHOST_USER_SET_MEM_TABLE] = "VHOST_USER_SET_MEM_TABLE",
88         [VHOST_USER_SET_LOG_BASE] = "VHOST_USER_SET_LOG_BASE",
89         [VHOST_USER_SET_LOG_FD] = "VHOST_USER_SET_LOG_FD",
90         [VHOST_USER_SET_VRING_NUM] = "VHOST_USER_SET_VRING_NUM",
91         [VHOST_USER_SET_VRING_ADDR] = "VHOST_USER_SET_VRING_ADDR",
92         [VHOST_USER_SET_VRING_BASE] = "VHOST_USER_SET_VRING_BASE",
93         [VHOST_USER_GET_VRING_BASE] = "VHOST_USER_GET_VRING_BASE",
94         [VHOST_USER_SET_VRING_KICK] = "VHOST_USER_SET_VRING_KICK",
95         [VHOST_USER_SET_VRING_CALL] = "VHOST_USER_SET_VRING_CALL",
96         [VHOST_USER_SET_VRING_ERR]  = "VHOST_USER_SET_VRING_ERR"
97 };
98
99 /**
100  * Create a unix domain socket, bind to path and listen for connection.
101  * @return
102  *  socket fd or -1 on failure
103  */
104 static int
105 uds_socket(const char *path)
106 {
107         struct sockaddr_un un;
108         int sockfd;
109         int ret;
110
111         if (path == NULL)
112                 return -1;
113
114         sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
115         if (sockfd < 0)
116                 return -1;
117         RTE_LOG(INFO, VHOST_CONFIG, "socket created, fd:%d\n", sockfd);
118
119         memset(&un, 0, sizeof(un));
120         un.sun_family = AF_UNIX;
121         snprintf(un.sun_path, sizeof(un.sun_path), "%s", path);
122         ret = bind(sockfd, (struct sockaddr *)&un, sizeof(un));
123         if (ret == -1)
124                 goto err;
125         RTE_LOG(INFO, VHOST_CONFIG, "bind to %s\n", path);
126
127         ret = listen(sockfd, MAX_VIRTIO_BACKLOG);
128         if (ret == -1)
129                 goto err;
130
131         return sockfd;
132
133 err:
134         close(sockfd);
135         return -1;
136 }
137
138 /* return bytes# of read on success or negative val on failure. */
139 static int
140 read_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
141 {
142         struct iovec iov;
143         struct msghdr msgh;
144         size_t fdsize = fd_num * sizeof(int);
145         char control[CMSG_SPACE(fdsize)];
146         struct cmsghdr *cmsg;
147         int ret;
148
149         memset(&msgh, 0, sizeof(msgh));
150         iov.iov_base = buf;
151         iov.iov_len  = buflen;
152
153         msgh.msg_iov = &iov;
154         msgh.msg_iovlen = 1;
155         msgh.msg_control = control;
156         msgh.msg_controllen = sizeof(control);
157
158         ret = recvmsg(sockfd, &msgh, 0);
159         if (ret <= 0) {
160                 RTE_LOG(ERR, VHOST_CONFIG, "recvmsg failed\n");
161                 return ret;
162         }
163
164         if (msgh.msg_flags & (MSG_TRUNC | MSG_CTRUNC)) {
165                 RTE_LOG(ERR, VHOST_CONFIG, "truncted msg\n");
166                 return -1;
167         }
168
169         for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
170                 cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
171                 if ((cmsg->cmsg_level == SOL_SOCKET) &&
172                         (cmsg->cmsg_type == SCM_RIGHTS)) {
173                         memcpy(fds, CMSG_DATA(cmsg), fdsize);
174                         break;
175                 }
176         }
177
178         return ret;
179 }
180
181 /* return bytes# of read on success or negative val on failure. */
182 static int
183 read_vhost_message(int sockfd, struct VhostUserMsg *msg)
184 {
185         int ret;
186
187         ret = read_fd_message(sockfd, (char *)msg, VHOST_USER_HDR_SIZE,
188                 msg->fds, VHOST_MEMORY_MAX_NREGIONS);
189         if (ret <= 0)
190                 return ret;
191
192         if (msg && msg->size) {
193                 if (msg->size > sizeof(msg->payload)) {
194                         RTE_LOG(ERR, VHOST_CONFIG,
195                                 "invalid msg size: %d\n", msg->size);
196                         return -1;
197                 }
198                 ret = read(sockfd, &msg->payload, msg->size);
199                 if (ret <= 0)
200                         return ret;
201                 if (ret != (int)msg->size) {
202                         RTE_LOG(ERR, VHOST_CONFIG,
203                                 "read control message failed\n");
204                         return -1;
205                 }
206         }
207
208         return ret;
209 }
210
211 static int
212 send_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
213 {
214
215         struct iovec iov;
216         struct msghdr msgh;
217         size_t fdsize = fd_num * sizeof(int);
218         char control[CMSG_SPACE(fdsize)];
219         struct cmsghdr *cmsg;
220         int ret;
221
222         memset(&msgh, 0, sizeof(msgh));
223         iov.iov_base = buf;
224         iov.iov_len = buflen;
225
226         msgh.msg_iov = &iov;
227         msgh.msg_iovlen = 1;
228
229         if (fds && fd_num > 0) {
230                 msgh.msg_control = control;
231                 msgh.msg_controllen = sizeof(control);
232                 cmsg = CMSG_FIRSTHDR(&msgh);
233                 cmsg->cmsg_len = CMSG_LEN(fdsize);
234                 cmsg->cmsg_level = SOL_SOCKET;
235                 cmsg->cmsg_type = SCM_RIGHTS;
236                 memcpy(CMSG_DATA(cmsg), fds, fdsize);
237         } else {
238                 msgh.msg_control = NULL;
239                 msgh.msg_controllen = 0;
240         }
241
242         do {
243                 ret = sendmsg(sockfd, &msgh, 0);
244         } while (ret < 0 && errno == EINTR);
245
246         if (ret < 0) {
247                 RTE_LOG(ERR, VHOST_CONFIG,  "sendmsg error\n");
248                 return ret;
249         }
250
251         return ret;
252 }
253
254 static int
255 send_vhost_message(int sockfd, struct VhostUserMsg *msg)
256 {
257         int ret;
258
259         if (!msg)
260                 return 0;
261
262         msg->flags &= ~VHOST_USER_VERSION_MASK;
263         msg->flags |= VHOST_USER_VERSION;
264         msg->flags |= VHOST_USER_REPLY_MASK;
265
266         ret = send_fd_message(sockfd, (char *)msg,
267                 VHOST_USER_HDR_SIZE + msg->size, NULL, 0);
268
269         return ret;
270 }
271
272 /* call back when there is new virtio connection.  */
273 static void
274 vserver_new_vq_conn(int fd, void *dat, __rte_unused int *remove)
275 {
276         struct vhost_server *vserver = (struct vhost_server *)dat;
277         int conn_fd;
278         struct connfd_ctx *ctx;
279         int fh;
280         struct vhost_device_ctx vdev_ctx = { (pid_t)0, 0 };
281         unsigned int size;
282
283         conn_fd = accept(fd, NULL, NULL);
284         RTE_LOG(INFO, VHOST_CONFIG,
285                 "new virtio connection is %d\n", conn_fd);
286         if (conn_fd < 0)
287                 return;
288
289         ctx = calloc(1, sizeof(*ctx));
290         if (ctx == NULL) {
291                 close(conn_fd);
292                 return;
293         }
294
295         fh = ops->new_device(vdev_ctx);
296         if (fh == -1) {
297                 free(ctx);
298                 close(conn_fd);
299                 return;
300         }
301
302         vdev_ctx.fh = fh;
303         size = strnlen(vserver->path, PATH_MAX);
304         ops->set_ifname(vdev_ctx, vserver->path,
305                 size);
306
307         RTE_LOG(INFO, VHOST_CONFIG, "new device, handle is %d\n", fh);
308
309         ctx->vserver = vserver;
310         ctx->fh = fh;
311         fdset_add(&g_vhost_server.fdset,
312                 conn_fd, vserver_message_handler, NULL, ctx);
313 }
314
315 /* callback when there is message on the connfd */
316 static void
317 vserver_message_handler(int connfd, void *dat, int *remove)
318 {
319         struct vhost_device_ctx ctx;
320         struct connfd_ctx *cfd_ctx = (struct connfd_ctx *)dat;
321         struct VhostUserMsg msg;
322         uint64_t features;
323         int ret;
324
325         ctx.fh = cfd_ctx->fh;
326         ret = read_vhost_message(connfd, &msg);
327         if (ret < 0) {
328                 RTE_LOG(ERR, VHOST_CONFIG,
329                         "vhost read message failed\n");
330
331                 close(connfd);
332                 *remove = 1;
333                 free(cfd_ctx);
334                 user_destroy_device(ctx);
335                 ops->destroy_device(ctx);
336
337                 return;
338         } else if (ret == 0) {
339                 RTE_LOG(INFO, VHOST_CONFIG,
340                         "vhost peer closed\n");
341
342                 close(connfd);
343                 *remove = 1;
344                 free(cfd_ctx);
345                 user_destroy_device(ctx);
346                 ops->destroy_device(ctx);
347
348                 return;
349         }
350         if (msg.request > VHOST_USER_MAX) {
351                 RTE_LOG(ERR, VHOST_CONFIG,
352                         "vhost read incorrect message\n");
353
354                 close(connfd);
355                 *remove = 1;
356                 free(cfd_ctx);
357                 user_destroy_device(ctx);
358                 ops->destroy_device(ctx);
359
360                 return;
361         }
362
363         RTE_LOG(INFO, VHOST_CONFIG, "read message %s\n",
364                 vhost_message_str[msg.request]);
365         switch (msg.request) {
366         case VHOST_USER_GET_FEATURES:
367                 ret = ops->get_features(ctx, &features);
368                 msg.payload.u64 = features;
369                 msg.size = sizeof(msg.payload.u64);
370                 send_vhost_message(connfd, &msg);
371                 break;
372         case VHOST_USER_SET_FEATURES:
373                 features = msg.payload.u64;
374                 ops->set_features(ctx, &features);
375                 break;
376
377         case VHOST_USER_SET_OWNER:
378                 ops->set_owner(ctx);
379                 break;
380         case VHOST_USER_RESET_OWNER:
381                 ops->reset_owner(ctx);
382                 break;
383
384         case VHOST_USER_SET_MEM_TABLE:
385                 user_set_mem_table(ctx, &msg);
386                 break;
387
388         case VHOST_USER_SET_LOG_BASE:
389                 RTE_LOG(INFO, VHOST_CONFIG, "not implemented.\n");
390         case VHOST_USER_SET_LOG_FD:
391                 close(msg.fds[0]);
392                 RTE_LOG(INFO, VHOST_CONFIG, "not implemented.\n");
393                 break;
394
395         case VHOST_USER_SET_VRING_NUM:
396                 ops->set_vring_num(ctx, &msg.payload.state);
397                 break;
398         case VHOST_USER_SET_VRING_ADDR:
399                 ops->set_vring_addr(ctx, &msg.payload.addr);
400                 break;
401         case VHOST_USER_SET_VRING_BASE:
402                 ops->set_vring_base(ctx, &msg.payload.state);
403                 break;
404
405         case VHOST_USER_GET_VRING_BASE:
406                 ret = user_get_vring_base(ctx, &msg.payload.state);
407                 msg.size = sizeof(msg.payload.state);
408                 send_vhost_message(connfd, &msg);
409                 break;
410
411         case VHOST_USER_SET_VRING_KICK:
412                 user_set_vring_kick(ctx, &msg);
413                 break;
414         case VHOST_USER_SET_VRING_CALL:
415                 user_set_vring_call(ctx, &msg);
416                 break;
417
418         case VHOST_USER_SET_VRING_ERR:
419                 if (!(msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK))
420                         close(msg.fds[0]);
421                 RTE_LOG(INFO, VHOST_CONFIG, "not implemented\n");
422                 break;
423
424         default:
425                 break;
426
427         }
428 }
429
430
431 /**
432  * Creates and initialise the vhost server.
433  */
434 int
435 rte_vhost_driver_register(const char *path)
436 {
437         struct vhost_server *vserver;
438
439         if (vserver_idx == 0)
440                 ops = get_virtio_net_callbacks();
441         if (vserver_idx == MAX_VHOST_SERVER)
442                 return -1;
443
444         vserver = calloc(sizeof(struct vhost_server), 1);
445         if (vserver == NULL)
446                 return -1;
447
448         unlink(path);
449
450         vserver->listenfd = uds_socket(path);
451         if (vserver->listenfd < 0) {
452                 free(vserver);
453                 return -1;
454         }
455         vserver->path = path;
456
457         fdset_add(&g_vhost_server.fdset, vserver->listenfd,
458                 vserver_new_vq_conn, NULL,
459                 vserver);
460
461         g_vhost_server.server[vserver_idx++] = vserver;
462
463         return 0;
464 }
465
466
467 int
468 rte_vhost_driver_session_start(void)
469 {
470         fdset_event_dispatch(&g_vhost_server.fdset);
471         return 0;
472 }