vhost: comment identified issues
[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 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, VHOST_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, VHOST_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         case VHOST_NET_SET_BACKEND:
192                 LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") IOCTL: VHOST_NET_SET_BACKEND\n", ctx.fh);
193                 VHOST_IOCTL_R(struct vhost_vring_file, file, ops->set_backend);
194                 break;
195
196         case VHOST_GET_FEATURES:
197                 LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") IOCTL: VHOST_GET_FEATURES\n", ctx.fh);
198                 VHOST_IOCTL_W(uint64_t, features, ops->get_features);
199                 break;
200
201         case VHOST_SET_FEATURES:
202                 LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") IOCTL: VHOST_SET_FEATURES\n", ctx.fh);
203                 VHOST_IOCTL_R(uint64_t, features, ops->set_features);
204                 break;
205
206         case VHOST_RESET_OWNER:
207                 LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") IOCTL: VHOST_RESET_OWNER\n", ctx.fh);
208                 VHOST_IOCTL(ops->reset_owner);
209                 break;
210
211         case VHOST_SET_OWNER:
212                 LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") IOCTL: VHOST_SET_OWNER\n", ctx.fh);
213                 VHOST_IOCTL(ops->set_owner);
214                 break;
215
216         case VHOST_SET_MEM_TABLE:
217                 /*TODO fix race condition.*/
218                 LOG_DEBUG(VHOST_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(VHOST_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(VHOST_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(VHOST_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(VHOST_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(VHOST_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(VHOST_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, VHOST_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(VHOST_CONFIG, "(%"PRIu64") IOCTL: FAIL\n", ctx.fh);
286         else
287                 LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") IOCTL: SUCCESS\n", ctx.fh);
288 }
289
290 /*
291  * Structure handling open, release and ioctl function pointers is populated.
292  */
293 static const struct cuse_lowlevel_ops vhost_net_ops = {
294         .open           = vhost_net_open,
295         .release        = vhost_net_release,
296         .ioctl          = vhost_net_ioctl,
297 };
298
299 /*
300  * cuse_info is populated and used to register the cuse device. vhost_net_device_ops are
301  * also passed when the device is registered in main.c.
302  */
303 int
304 rte_vhost_driver_register(const char *dev_name)
305 {
306         struct cuse_info cuse_info;
307         char device_name[PATH_MAX] = "";
308         char char_device_name[PATH_MAX] = "";
309         const char *device_argv[] = { device_name };
310
311         char fuse_opt_dummy[] = FUSE_OPT_DUMMY;
312         char fuse_opt_fore[] = FUSE_OPT_FORE;
313         char fuse_opt_nomulti[] = FUSE_OPT_NOMULTI;
314         char *fuse_argv[] = {fuse_opt_dummy, fuse_opt_fore, fuse_opt_nomulti};
315
316         if (access(cuse_device_name, R_OK | W_OK) < 0) {
317                 RTE_LOG(ERR, VHOST_CONFIG, "Character device %s can't be accessed, maybe not exist\n", cuse_device_name);
318                 return -1;
319         }
320
321         /*
322          * The device name is created. This is passed to QEMU so that it can register
323          * the device with our application.
324          */
325         snprintf(device_name, PATH_MAX, "DEVNAME=%s", dev_name);
326         snprintf(char_device_name, PATH_MAX, "/dev/%s", dev_name);
327
328         /* Check if device already exists. */
329         if (access(char_device_name, F_OK) != -1) {
330                 RTE_LOG(ERR, VHOST_CONFIG, "Character device %s already exists\n", char_device_name);
331                 return -1;
332         }
333
334         memset(&cuse_info, 0, sizeof(cuse_info));
335         cuse_info.dev_major = default_major;
336         cuse_info.dev_minor = default_minor;
337         cuse_info.dev_info_argc = 1;
338         cuse_info.dev_info_argv = device_argv;
339         cuse_info.flags = CUSE_UNRESTRICTED_IOCTL;
340
341         ops = get_virtio_net_callbacks();
342
343         session = cuse_lowlevel_setup(3, fuse_argv,
344                                 &cuse_info, &vhost_net_ops, 0, NULL);
345         if (session == NULL)
346                 return -1;
347
348         return 0;
349 }
350
351 /**
352  * The CUSE session is launched allowing the application to receive open, release and ioctl calls.
353  */
354 int
355 rte_vhost_driver_session_start(void)
356 {
357         fuse_session_loop(session);
358
359         return 0;
360 }