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