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