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