4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
35 #include <fuse/cuse_lowlevel.h>
36 #include <linux/limits.h>
37 #include <linux/vhost.h>
42 #include <rte_ethdev.h>
44 #include <rte_string_fns.h>
47 #include "vhost-net-cdev.h"
49 #define FUSE_OPT_DUMMY "\0\0"
50 #define FUSE_OPT_FORE "-f\0\0"
51 #define FUSE_OPT_NOMULTI "-s\0\0"
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";
58 static struct fuse_session *session;
59 static struct vhost_net_device_ops const *ops;
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.
65 static struct vhost_device_ctx
66 fuse_req_to_vhost_ctx(fuse_req_t req, struct fuse_file_info *fi)
68 struct vhost_device_ctx ctx;
69 struct fuse_ctx const *const req_ctx = fuse_req_ctx(req);
71 ctx.pid = req_ctx->pid;
78 * When the device is created in QEMU it gets initialised here and added to the device linked list.
81 vhost_net_open(fuse_req_t req, struct fuse_file_info *fi)
83 struct vhost_device_ctx ctx = fuse_req_to_vhost_ctx(req, fi);
86 err = ops->new_device(ctx);
88 fuse_reply_err(req, EPERM);
94 RTE_LOG(INFO, VHOST_CONFIG, "(%"PRIu64") Device configuration started\n", fi->fh);
95 fuse_reply_open(req, fi);
99 * When QEMU is shutdown or killed the device gets released.
102 vhost_net_release(fuse_req_t req, struct fuse_file_info *fi)
105 struct vhost_device_ctx ctx = fuse_req_to_vhost_ctx(req, fi);
107 ops->destroy_device(ctx);
108 RTE_LOG(INFO, VHOST_CONFIG, "(%"PRIu64") Device released\n", ctx.fh);
109 fuse_reply_err(req, err);
113 * Boilerplate code for CUSE IOCTL
114 * Implicit arguments: ctx, req, result.
116 #define VHOST_IOCTL(func) do { \
117 result = (func)(ctx); \
118 fuse_reply_ioctl(req, result, NULL, 0); \
122 * Boilerplate IOCTL RETRY
123 * Implicit arguments: req.
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); \
132 * Boilerplate code for CUSE Read IOCTL
133 * Implicit arguments: ctx, req, result, in_bufsz, in_buf.
135 #define VHOST_IOCTL_R(type, var, func) do { \
137 VHOST_IOCTL_RETRY(sizeof(type), 0); \
139 (var) = *(const type * ) in_buf; \
140 result = func(ctx, &(var)); \
141 fuse_reply_ioctl(req, result, NULL, 0); \
146 * Boilerplate code for CUSE Write IOCTL
147 * Implicit arguments: ctx, req, result, out_bufsz.
149 #define VHOST_IOCTL_W(type, var, func) do { \
151 VHOST_IOCTL_RETRY(0, sizeof(type)); \
153 result = (func)(ctx, &(var)); \
154 fuse_reply_ioctl(req, result, &(var), sizeof(type)); \
159 * Boilerplate code for CUSE Read/Write IOCTL
160 * Implicit arguments: ctx, req, result, in_bufsz, in_buf.
162 #define VHOST_IOCTL_RW(type1, var1, type2, var2, func) do { \
164 VHOST_IOCTL_RETRY(sizeof(type1), sizeof(type2)); \
166 (var1) = *(const type1* ) (in_buf); \
167 result = (func)(ctx, (var1), &(var2)); \
168 fuse_reply_ioctl(req, result, &(var2), sizeof(type2)); \
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.
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)
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;
192 case VHOST_NET_SET_BACKEND:
193 LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") IOCTL: VHOST_NET_SET_BACKEND\n", ctx.fh);
194 VHOST_IOCTL_R(struct vhost_vring_file, file, ops->set_backend);
197 case VHOST_GET_FEATURES:
198 LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") IOCTL: VHOST_GET_FEATURES\n", ctx.fh);
199 VHOST_IOCTL_W(uint64_t, features, ops->get_features);
202 case VHOST_SET_FEATURES:
203 LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") IOCTL: VHOST_SET_FEATURES\n", ctx.fh);
204 VHOST_IOCTL_R(uint64_t, features, ops->set_features);
207 case VHOST_RESET_OWNER:
208 LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") IOCTL: VHOST_RESET_OWNER\n", ctx.fh);
209 VHOST_IOCTL(ops->reset_owner);
212 case VHOST_SET_OWNER:
213 LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") IOCTL: VHOST_SET_OWNER\n", ctx.fh);
214 VHOST_IOCTL(ops->set_owner);
217 case VHOST_SET_MEM_TABLE:
218 LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") IOCTL: VHOST_SET_MEM_TABLE\n", ctx.fh);
219 static struct vhost_memory mem_temp;
223 VHOST_IOCTL_RETRY(sizeof(struct vhost_memory), 0);
226 case sizeof(struct vhost_memory):
227 mem_temp = *(const struct vhost_memory *) in_buf;
229 if (mem_temp.nregions > 0) {
230 VHOST_IOCTL_RETRY(sizeof(struct vhost_memory) + (sizeof(struct vhost_memory_region) * mem_temp.nregions), 0);
233 fuse_reply_ioctl(req, result, NULL, 0);
238 result = ops->set_mem_table(ctx, in_buf, mem_temp.nregions);
240 fuse_reply_err(req, EINVAL);
242 fuse_reply_ioctl(req, result, NULL, 0);
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);
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);
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);
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);
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);
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);
279 RTE_LOG(ERR, VHOST_CONFIG, "(%"PRIu64") IOCTL: DOESN NOT EXIST\n", ctx.fh);
281 fuse_reply_ioctl(req, result, NULL, 0);
285 LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") IOCTL: FAIL\n", ctx.fh);
287 LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") IOCTL: SUCCESS\n", ctx.fh);
292 * Structure handling open, release and ioctl function pointers is populated.
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,
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.
305 register_cuse_device(const char *base_name, int index, struct vhost_net_device_ops const * const pops)
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 };
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};
317 if (access(cuse_device_name, R_OK | W_OK) < 0) {
318 RTE_LOG(ERR, VHOST_CONFIG, "Character device %s can't be accessed, maybe not exist\n", cuse_device_name);
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.
327 if (strncmp(base_name, default_cdev, PATH_MAX)!=0) {
328 snprintf(device_name, PATH_MAX, "DEVNAME=%s-%d", base_name, index);
329 snprintf(char_device_name, PATH_MAX, "/dev/%s-%d", base_name, index);
331 snprintf(device_name, PATH_MAX, "DEVNAME=%s", base_name);
332 snprintf(char_device_name, PATH_MAX, "/dev/%s", base_name);
335 /* Check if device already exists. */
336 if (access(char_device_name, F_OK) != -1) {
337 RTE_LOG(ERR, VHOST_CONFIG, "Character device %s already exists\n", char_device_name);
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;
350 session = cuse_lowlevel_setup(3, fuse_argv,
351 &cuse_info, &vhost_net_ops, 0, NULL);
359 * The CUSE session is launched allowing the application to receive open, release and ioctl calls.
362 start_cuse_session_loop(void)
364 fuse_session_loop(session);