vhost: support ifname for 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         unsigned int size;
272
273         conn_fd = accept(fd, NULL, NULL);
274         RTE_LOG(INFO, VHOST_CONFIG,
275                 "new virtio connection is %d\n", conn_fd);
276         if (conn_fd < 0)
277                 return;
278
279         ctx = calloc(1, sizeof(*ctx));
280         if (ctx == NULL) {
281                 close(conn_fd);
282                 return;
283         }
284
285         fh = ops->new_device(vdev_ctx);
286         if (fh == -1) {
287                 free(ctx);
288                 close(conn_fd);
289                 return;
290         }
291
292         vdev_ctx.fh = fh;
293         size = strnlen(vserver->path, PATH_MAX);
294         ops->set_ifname(vdev_ctx, vserver->path,
295                 size);
296
297         RTE_LOG(INFO, VHOST_CONFIG, "new device, handle is %d\n", fh);
298
299         ctx->vserver = vserver;
300         ctx->fh = fh;
301         fdset_add(&g_vhost_server.fdset,
302                 conn_fd, vserver_message_handler, NULL, ctx);
303 }
304
305 /* callback when there is message on the connfd */
306 static void
307 vserver_message_handler(int connfd, void *dat)
308 {
309         struct vhost_device_ctx ctx;
310         struct connfd_ctx *cfd_ctx = (struct connfd_ctx *)dat;
311         struct VhostUserMsg msg;
312         uint64_t features;
313         int ret;
314
315         ctx.fh = cfd_ctx->fh;
316         ret = read_vhost_message(connfd, &msg);
317         if (ret < 0) {
318                 RTE_LOG(ERR, VHOST_CONFIG,
319                         "vhost read message failed\n");
320
321                 close(connfd);
322                 fdset_del(&g_vhost_server.fdset, connfd);
323                 free(cfd_ctx);
324                 user_destroy_device(ctx);
325                 ops->destroy_device(ctx);
326
327                 return;
328         } else if (ret == 0) {
329                 RTE_LOG(INFO, VHOST_CONFIG,
330                         "vhost peer closed\n");
331
332                 close(connfd);
333                 fdset_del(&g_vhost_server.fdset, connfd);
334                 free(cfd_ctx);
335                 user_destroy_device(ctx);
336                 ops->destroy_device(ctx);
337
338                 return;
339         }
340         if (msg.request > VHOST_USER_MAX) {
341                 RTE_LOG(ERR, VHOST_CONFIG,
342                         "vhost read incorrect message\n");
343
344                 close(connfd);
345                 fdset_del(&g_vhost_server.fdset, connfd);
346                 free(cfd_ctx);
347                 user_destroy_device(ctx);
348                 ops->destroy_device(ctx);
349
350                 return;
351         }
352
353         RTE_LOG(INFO, VHOST_CONFIG, "read message %s\n",
354                 vhost_message_str[msg.request]);
355         switch (msg.request) {
356         case VHOST_USER_GET_FEATURES:
357                 ret = ops->get_features(ctx, &features);
358                 msg.payload.u64 = features;
359                 msg.size = sizeof(msg.payload.u64);
360                 send_vhost_message(connfd, &msg);
361                 break;
362         case VHOST_USER_SET_FEATURES:
363                 features = msg.payload.u64;
364                 ops->set_features(ctx, &features);
365                 break;
366
367         case VHOST_USER_SET_OWNER:
368                 ops->set_owner(ctx);
369                 break;
370         case VHOST_USER_RESET_OWNER:
371                 ops->reset_owner(ctx);
372                 break;
373
374         case VHOST_USER_SET_MEM_TABLE:
375                 user_set_mem_table(ctx, &msg);
376                 break;
377
378         case VHOST_USER_SET_LOG_BASE:
379                 RTE_LOG(INFO, VHOST_CONFIG, "not implemented.\n");
380         case VHOST_USER_SET_LOG_FD:
381                 close(msg.fds[0]);
382                 RTE_LOG(INFO, VHOST_CONFIG, "not implemented.\n");
383                 break;
384
385         case VHOST_USER_SET_VRING_NUM:
386                 ops->set_vring_num(ctx, &msg.payload.state);
387                 break;
388         case VHOST_USER_SET_VRING_ADDR:
389                 ops->set_vring_addr(ctx, &msg.payload.addr);
390                 break;
391         case VHOST_USER_SET_VRING_BASE:
392                 ops->set_vring_base(ctx, &msg.payload.state);
393                 break;
394
395         case VHOST_USER_GET_VRING_BASE:
396                 ret = user_get_vring_base(ctx, &msg.payload.state);
397                 msg.size = sizeof(msg.payload.state);
398                 send_vhost_message(connfd, &msg);
399                 break;
400
401         case VHOST_USER_SET_VRING_KICK:
402                 user_set_vring_kick(ctx, &msg);
403                 break;
404         case VHOST_USER_SET_VRING_CALL:
405                 user_set_vring_call(ctx, &msg);
406                 break;
407
408         case VHOST_USER_SET_VRING_ERR:
409                 if (!(msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK))
410                         close(msg.fds[0]);
411                 RTE_LOG(INFO, VHOST_CONFIG, "not implemented\n");
412                 break;
413
414         default:
415                 break;
416
417         }
418 }
419
420
421 /**
422  * Creates and initialise the vhost server.
423  */
424 int
425 rte_vhost_driver_register(const char *path)
426 {
427         struct vhost_server *vserver;
428
429         if (vserver_idx == 0) {
430                 fdset_init(&g_vhost_server.fdset);
431                 ops = get_virtio_net_callbacks();
432         }
433         if (vserver_idx == MAX_VHOST_SERVER)
434                 return -1;
435
436         vserver = calloc(sizeof(struct vhost_server), 1);
437         if (vserver == NULL)
438                 return -1;
439
440         unlink(path);
441
442         vserver->listenfd = uds_socket(path);
443         if (vserver->listenfd < 0) {
444                 free(vserver);
445                 return -1;
446         }
447         vserver->path = path;
448
449         fdset_add(&g_vhost_server.fdset, vserver->listenfd,
450                 vserver_new_vq_conn, NULL,
451                 vserver);
452
453         g_vhost_server.server[vserver_idx++] = vserver;
454
455         return 0;
456 }
457
458
459 int
460 rte_vhost_driver_session_start(void)
461 {
462         fdset_event_dispatch(&g_vhost_server.fdset);
463         return 0;
464 }