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