vhost: enable virtio control channel Rx mode
[dpdk.git] / lib / librte_vhost / vhost-net-cdev.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 <errno.h>
35 #include <fuse/cuse_lowlevel.h>
36 #include <linux/limits.h>
37 #include <linux/vhost.h>
38 #include <stdint.h>
39 #include <string.h>
40 #include <unistd.h>
41
42 #include <rte_ethdev.h>
43 #include <rte_log.h>
44 #include <rte_string_fns.h>
45 #include <rte_virtio_net.h>
46
47 #include "vhost-net-cdev.h"
48
49 #define FUSE_OPT_DUMMY "\0\0"
50 #define FUSE_OPT_FORE  "-f\0\0"
51 #define FUSE_OPT_NOMULTI "-s\0\0"
52
53 static const uint32_t default_major = 231;
54 static const uint32_t default_minor = 1;
55 static const char cuse_device_name[] = "/dev/cuse";
56 static const char default_cdev[] = "vhost-net";
57
58 static struct fuse_session *session;
59 static struct vhost_net_device_ops const *ops;
60
61 /*
62  * Returns vhost_device_ctx from given fuse_req_t. The index is populated later
63  * when the device is added to the device linked list.
64  */
65 static struct vhost_device_ctx
66 fuse_req_to_vhost_ctx(fuse_req_t req, struct fuse_file_info *fi)
67 {
68         struct vhost_device_ctx ctx;
69         struct fuse_ctx const *const req_ctx = fuse_req_ctx(req);
70
71         ctx.pid = req_ctx->pid;
72         ctx.fh = fi->fh;
73
74         return ctx;
75 }
76
77 /*
78  * When the device is created in QEMU it gets initialised here and
79  * added to the device linked list.
80  */
81 static void
82 vhost_net_open(fuse_req_t req, struct fuse_file_info *fi)
83 {
84         struct vhost_device_ctx ctx = fuse_req_to_vhost_ctx(req, fi);
85         int err = 0;
86
87         err = ops->new_device(ctx);
88         if (err == -1) {
89                 fuse_reply_err(req, EPERM);
90                 return;
91         }
92
93         fi->fh = err;
94
95         RTE_LOG(INFO, VHOST_CONFIG,
96                 "(%"PRIu64") Device configuration started\n", fi->fh);
97         fuse_reply_open(req, fi);
98 }
99
100 /*
101  * When QEMU is shutdown or killed the device gets released.
102  */
103 static void
104 vhost_net_release(fuse_req_t req, struct fuse_file_info *fi)
105 {
106         int err = 0;
107         struct vhost_device_ctx ctx = fuse_req_to_vhost_ctx(req, fi);
108
109         ops->destroy_device(ctx);
110         RTE_LOG(INFO, VHOST_CONFIG, "(%"PRIu64") Device released\n", ctx.fh);
111         fuse_reply_err(req, err);
112 }
113
114 /*
115  * Boilerplate code for CUSE IOCTL
116  * Implicit arguments: ctx, req, result.
117  */
118 #define VHOST_IOCTL(func) do {  \
119         result = (func)(ctx);   \
120         fuse_reply_ioctl(req, result, NULL, 0); \
121 } while (0)
122
123 /*
124  * Boilerplate IOCTL RETRY
125  * Implicit arguments: req.
126  */
127 #define VHOST_IOCTL_RETRY(size_r, size_w) do {  \
128         struct iovec iov_r = { arg, (size_r) }; \
129         struct iovec iov_w = { arg, (size_w) }; \
130         fuse_reply_ioctl_retry(req, &iov_r,     \
131                 (size_r) ? 1 : 0, &iov_w, (size_w) ? 1 : 0);\
132 } while (0)
133
134 /*
135  * Boilerplate code for CUSE Read IOCTL
136  * Implicit arguments: ctx, req, result, in_bufsz, in_buf.
137  */
138 #define VHOST_IOCTL_R(type, var, func) do {     \
139         if (!in_bufsz) {        \
140                 VHOST_IOCTL_RETRY(sizeof(type), 0);\
141         } else {        \
142                 (var) = *(const type*)in_buf;   \
143                 result = func(ctx, &(var));     \
144                 fuse_reply_ioctl(req, result, NULL, 0);\
145         }       \
146 } while (0)
147
148 /*
149  * Boilerplate code for CUSE Write IOCTL
150  * Implicit arguments: ctx, req, result, out_bufsz.
151  */
152 #define VHOST_IOCTL_W(type, var, func) do {     \
153         if (!out_bufsz) {       \
154                 VHOST_IOCTL_RETRY(0, sizeof(type));\
155         } else {        \
156                 result = (func)(ctx, &(var));\
157                 fuse_reply_ioctl(req, result, &(var), sizeof(type));\
158         } \
159 } while (0)
160
161 /*
162  * Boilerplate code for CUSE Read/Write IOCTL
163  * Implicit arguments: ctx, req, result, in_bufsz, in_buf.
164  */
165 #define VHOST_IOCTL_RW(type1, var1, type2, var2, func) do {     \
166         if (!in_bufsz) {        \
167                 VHOST_IOCTL_RETRY(sizeof(type1), sizeof(type2));\
168         } else {        \
169                 (var1) = *(const type1*) (in_buf);      \
170                 result = (func)(ctx, (var1), &(var2));  \
171                 fuse_reply_ioctl(req, result, &(var2), sizeof(type2));\
172         }       \
173 } while (0)
174
175 /*
176  * The IOCTLs are handled using CUSE/FUSE in userspace. Depending on the type
177  * of IOCTL a buffer is requested to read or to write. This request is handled
178  * by FUSE and the buffer is then given to CUSE.
179  */
180 static void
181 vhost_net_ioctl(fuse_req_t req, int cmd, void *arg,
182                 struct fuse_file_info *fi, __rte_unused unsigned flags,
183                 const void *in_buf, size_t in_bufsz, size_t out_bufsz)
184 {
185         struct vhost_device_ctx ctx = fuse_req_to_vhost_ctx(req, fi);
186         struct vhost_vring_file file;
187         struct vhost_vring_state state;
188         struct vhost_vring_addr addr;
189         uint64_t features;
190         uint32_t index;
191         int result = 0;
192
193         switch (cmd) {
194         case VHOST_NET_SET_BACKEND:
195                 LOG_DEBUG(VHOST_CONFIG,
196                         "(%"PRIu64") IOCTL: VHOST_NET_SET_BACKEND\n", ctx.fh);
197                 VHOST_IOCTL_R(struct vhost_vring_file, file, ops->set_backend);
198                 break;
199
200         case VHOST_GET_FEATURES:
201                 LOG_DEBUG(VHOST_CONFIG,
202                         "(%"PRIu64") IOCTL: VHOST_GET_FEATURES\n", ctx.fh);
203                 VHOST_IOCTL_W(uint64_t, features, ops->get_features);
204                 break;
205
206         case VHOST_SET_FEATURES:
207                 LOG_DEBUG(VHOST_CONFIG,
208                         "(%"PRIu64") IOCTL: VHOST_SET_FEATURES\n", ctx.fh);
209                 VHOST_IOCTL_R(uint64_t, features, ops->set_features);
210                 break;
211
212         case VHOST_RESET_OWNER:
213                 LOG_DEBUG(VHOST_CONFIG,
214                         "(%"PRIu64") IOCTL: VHOST_RESET_OWNER\n", ctx.fh);
215                 VHOST_IOCTL(ops->reset_owner);
216                 break;
217
218         case VHOST_SET_OWNER:
219                 LOG_DEBUG(VHOST_CONFIG,
220                         "(%"PRIu64") IOCTL: VHOST_SET_OWNER\n", ctx.fh);
221                 VHOST_IOCTL(ops->set_owner);
222                 break;
223
224         case VHOST_SET_MEM_TABLE:
225                 /*TODO fix race condition.*/
226                 LOG_DEBUG(VHOST_CONFIG,
227                         "(%"PRIu64") IOCTL: VHOST_SET_MEM_TABLE\n", ctx.fh);
228                 static struct vhost_memory mem_temp;
229
230                 switch (in_bufsz) {
231                 case 0:
232                         VHOST_IOCTL_RETRY(sizeof(struct vhost_memory), 0);
233                         break;
234
235                 case sizeof(struct vhost_memory):
236                         mem_temp = *(const struct vhost_memory *) in_buf;
237
238                         if (mem_temp.nregions > 0) {
239                                 VHOST_IOCTL_RETRY(sizeof(struct vhost_memory) +
240                                         (sizeof(struct vhost_memory_region) *
241                                                 mem_temp.nregions), 0);
242                         } else {
243                                 result = -1;
244                                 fuse_reply_ioctl(req, result, NULL, 0);
245                         }
246                         break;
247
248                 default:
249                         result = ops->set_mem_table(ctx,
250                                         in_buf, mem_temp.nregions);
251                         if (result)
252                                 fuse_reply_err(req, EINVAL);
253                         else
254                                 fuse_reply_ioctl(req, result, NULL, 0);
255                 }
256                 break;
257
258         case VHOST_SET_VRING_NUM:
259                 LOG_DEBUG(VHOST_CONFIG,
260                         "(%"PRIu64") IOCTL: VHOST_SET_VRING_NUM\n", ctx.fh);
261                 VHOST_IOCTL_R(struct vhost_vring_state, state,
262                         ops->set_vring_num);
263                 break;
264
265         case VHOST_SET_VRING_BASE:
266                 LOG_DEBUG(VHOST_CONFIG,
267                         "(%"PRIu64") IOCTL: VHOST_SET_VRING_BASE\n", ctx.fh);
268                 VHOST_IOCTL_R(struct vhost_vring_state, state,
269                         ops->set_vring_base);
270                 break;
271
272         case VHOST_GET_VRING_BASE:
273                 LOG_DEBUG(VHOST_CONFIG,
274                         "(%"PRIu64") IOCTL: VHOST_GET_VRING_BASE\n", ctx.fh);
275                 VHOST_IOCTL_RW(uint32_t, index,
276                         struct vhost_vring_state, state, ops->get_vring_base);
277                 break;
278
279         case VHOST_SET_VRING_ADDR:
280                 LOG_DEBUG(VHOST_CONFIG,
281                         "(%"PRIu64") IOCTL: VHOST_SET_VRING_ADDR\n", ctx.fh);
282                 VHOST_IOCTL_R(struct vhost_vring_addr, addr,
283                         ops->set_vring_addr);
284                 break;
285
286         case VHOST_SET_VRING_KICK:
287                 LOG_DEBUG(VHOST_CONFIG,
288                         "(%"PRIu64") IOCTL: VHOST_SET_VRING_KICK\n", ctx.fh);
289                 VHOST_IOCTL_R(struct vhost_vring_file, file,
290                         ops->set_vring_kick);
291                 break;
292
293         case VHOST_SET_VRING_CALL:
294                 LOG_DEBUG(VHOST_CONFIG,
295                         "(%"PRIu64") IOCTL: VHOST_SET_VRING_CALL\n", ctx.fh);
296                 VHOST_IOCTL_R(struct vhost_vring_file, file,
297                         ops->set_vring_call);
298                 break;
299
300         default:
301                 RTE_LOG(ERR, VHOST_CONFIG,
302                         "(%"PRIu64") IOCTL: DOESN NOT EXIST\n", ctx.fh);
303                 result = -1;
304                 fuse_reply_ioctl(req, result, NULL, 0);
305         }
306
307         if (result < 0)
308                 LOG_DEBUG(VHOST_CONFIG,
309                         "(%"PRIu64") IOCTL: FAIL\n", ctx.fh);
310         else
311                 LOG_DEBUG(VHOST_CONFIG,
312                         "(%"PRIu64") IOCTL: SUCCESS\n", ctx.fh);
313 }
314
315 /*
316  * Structure handling open, release and ioctl function pointers is populated.
317  */
318 static const struct cuse_lowlevel_ops vhost_net_ops = {
319         .open           = vhost_net_open,
320         .release        = vhost_net_release,
321         .ioctl          = vhost_net_ioctl,
322 };
323
324 /*
325  * cuse_info is populated and used to register the cuse device.
326  * vhost_net_device_ops are also passed when the device is registered in app.
327  */
328 int
329 rte_vhost_driver_register(const char *dev_name)
330 {
331         struct cuse_info cuse_info;
332         char device_name[PATH_MAX] = "";
333         char char_device_name[PATH_MAX] = "";
334         const char *device_argv[] = { device_name };
335
336         char fuse_opt_dummy[] = FUSE_OPT_DUMMY;
337         char fuse_opt_fore[] = FUSE_OPT_FORE;
338         char fuse_opt_nomulti[] = FUSE_OPT_NOMULTI;
339         char *fuse_argv[] = {fuse_opt_dummy, fuse_opt_fore, fuse_opt_nomulti};
340
341         if (access(cuse_device_name, R_OK | W_OK) < 0) {
342                 RTE_LOG(ERR, VHOST_CONFIG,
343                         "char device %s can't be accessed, maybe not exist\n",
344                         cuse_device_name);
345                 return -1;
346         }
347
348         /*
349          * The device name is created. This is passed to QEMU so that it can
350          * register the device with our application.
351          */
352         snprintf(device_name, PATH_MAX, "DEVNAME=%s", dev_name);
353         snprintf(char_device_name, PATH_MAX, "/dev/%s", dev_name);
354
355         /* Check if device already exists. */
356         if (access(char_device_name, F_OK) != -1) {
357                 RTE_LOG(ERR, VHOST_CONFIG,
358                         "char device %s already exists\n", char_device_name);
359                 return -1;
360         }
361
362         memset(&cuse_info, 0, sizeof(cuse_info));
363         cuse_info.dev_major = default_major;
364         cuse_info.dev_minor = default_minor;
365         cuse_info.dev_info_argc = 1;
366         cuse_info.dev_info_argv = device_argv;
367         cuse_info.flags = CUSE_UNRESTRICTED_IOCTL;
368
369         ops = get_virtio_net_callbacks();
370
371         session = cuse_lowlevel_setup(3, fuse_argv,
372                         &cuse_info, &vhost_net_ops, 0, NULL);
373         if (session == NULL)
374                 return -1;
375
376         return 0;
377 }
378
379 /**
380  * The CUSE session is launched allowing the application to receive open,
381  * release and ioctl calls.
382  */
383 int
384 rte_vhost_driver_session_start(void)
385 {
386         fuse_session_loop(session);
387
388         return 0;
389 }