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