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