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