2 * libqos driver framework
4 * Copyright (c) 2018 Emanuele Giuseppe Esposito <e.emanuelegiuseppe@gmail.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License version 2.1 as published by the Free Software Foundation.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, see <http://www.gnu.org/licenses/>
19 #include "qemu/osdep.h"
21 #include "qemu/module.h"
22 #include "standard-headers/linux/virtio_ids.h"
23 #include "virtio-9p.h"
26 static QGuestAllocator
*alloc
;
27 static char *local_test_path
;
29 /* Concatenates the passed 2 pathes. Returned result must be freed. */
30 static char *concat_path(const char* a
, const char* b
)
32 return g_build_filename(a
, b
, NULL
);
35 static void init_local_test_path(void)
37 char *pwd
= g_get_current_dir();
38 char *template = concat_path(pwd
, "qtest-9p-local-XXXXXX");
39 local_test_path
= mkdtemp(template);
40 if (!local_test_path
) {
41 g_test_message("mkdtemp('%s') failed: %s", template, strerror(errno
));
43 g_assert(local_test_path
);
47 void virtio_9p_create_local_test_dir(void)
52 init_local_test_path();
54 g_assert(local_test_path
!= NULL
);
55 res
= mkdir(local_test_path
, 0777);
57 g_test_message("mkdir('%s') failed: %s", local_test_path
,
61 /* ensure test directory exists now ... */
62 g_assert(stat(local_test_path
, &st
) == 0);
63 /* ... and is actually a directory */
64 g_assert((st
.st_mode
& S_IFMT
) == S_IFDIR
);
67 void virtio_9p_remove_local_test_dir(void)
69 g_assert(local_test_path
!= NULL
);
70 char *cmd
= g_strdup_printf("rm -fr '%s'\n", local_test_path
);
71 int res
= system(cmd
);
73 /* ignore error, dummy check to prevent compiler error */
78 char *virtio_9p_test_path(const char *path
)
80 g_assert(local_test_path
);
81 return concat_path(local_test_path
, path
);
84 static void virtio_9p_cleanup(QVirtio9P
*interface
)
86 qvirtqueue_cleanup(interface
->vdev
->bus
, interface
->vq
, alloc
);
89 static void virtio_9p_setup(QVirtio9P
*interface
)
93 features
= qvirtio_get_features(interface
->vdev
);
94 features
&= ~(QVIRTIO_F_BAD_FEATURE
| (1ull << VIRTIO_RING_F_EVENT_IDX
));
95 qvirtio_set_features(interface
->vdev
, features
);
97 interface
->vq
= qvirtqueue_setup(interface
->vdev
, alloc
, 0);
98 qvirtio_set_driver_ok(interface
->vdev
);
101 /* virtio-9p-device */
102 static void virtio_9p_device_destructor(QOSGraphObject
*obj
)
104 QVirtio9PDevice
*v_9p
= (QVirtio9PDevice
*) obj
;
105 QVirtio9P
*v9p
= &v_9p
->v9p
;
107 virtio_9p_cleanup(v9p
);
110 static void virtio_9p_device_start_hw(QOSGraphObject
*obj
)
112 QVirtio9PDevice
*v_9p
= (QVirtio9PDevice
*) obj
;
113 QVirtio9P
*v9p
= &v_9p
->v9p
;
115 virtio_9p_setup(v9p
);
118 static void *virtio_9p_get_driver(QVirtio9P
*v_9p
,
119 const char *interface
)
121 if (!g_strcmp0(interface
, "virtio-9p")) {
124 if (!g_strcmp0(interface
, "virtio")) {
128 fprintf(stderr
, "%s not present in virtio-9p-device\n", interface
);
129 g_assert_not_reached();
132 static void *virtio_9p_device_get_driver(void *object
, const char *interface
)
134 QVirtio9PDevice
*v_9p
= object
;
135 return virtio_9p_get_driver(&v_9p
->v9p
, interface
);
138 static void *virtio_9p_device_create(void *virtio_dev
,
139 QGuestAllocator
*t_alloc
,
142 QVirtio9PDevice
*virtio_device
= g_new0(QVirtio9PDevice
, 1);
143 QVirtio9P
*interface
= &virtio_device
->v9p
;
145 interface
->vdev
= virtio_dev
;
148 virtio_device
->obj
.destructor
= virtio_9p_device_destructor
;
149 virtio_device
->obj
.get_driver
= virtio_9p_device_get_driver
;
150 virtio_device
->obj
.start_hw
= virtio_9p_device_start_hw
;
152 return &virtio_device
->obj
;
156 static void virtio_9p_pci_destructor(QOSGraphObject
*obj
)
158 QVirtio9PPCI
*v9_pci
= (QVirtio9PPCI
*) obj
;
159 QVirtio9P
*interface
= &v9_pci
->v9p
;
160 QOSGraphObject
*pci_vobj
= &v9_pci
->pci_vdev
.obj
;
162 virtio_9p_cleanup(interface
);
163 qvirtio_pci_destructor(pci_vobj
);
166 static void virtio_9p_pci_start_hw(QOSGraphObject
*obj
)
168 QVirtio9PPCI
*v9_pci
= (QVirtio9PPCI
*) obj
;
169 QVirtio9P
*interface
= &v9_pci
->v9p
;
170 QOSGraphObject
*pci_vobj
= &v9_pci
->pci_vdev
.obj
;
172 qvirtio_pci_start_hw(pci_vobj
);
173 virtio_9p_setup(interface
);
176 static void *virtio_9p_pci_get_driver(void *object
, const char *interface
)
178 QVirtio9PPCI
*v_9p
= object
;
179 if (!g_strcmp0(interface
, "pci-device")) {
180 return v_9p
->pci_vdev
.pdev
;
182 return virtio_9p_get_driver(&v_9p
->v9p
, interface
);
185 static void *virtio_9p_pci_create(void *pci_bus
, QGuestAllocator
*t_alloc
,
188 QVirtio9PPCI
*v9_pci
= g_new0(QVirtio9PPCI
, 1);
189 QVirtio9P
*interface
= &v9_pci
->v9p
;
190 QOSGraphObject
*obj
= &v9_pci
->pci_vdev
.obj
;
192 virtio_pci_init(&v9_pci
->pci_vdev
, pci_bus
, addr
);
193 interface
->vdev
= &v9_pci
->pci_vdev
.vdev
;
196 g_assert_cmphex(interface
->vdev
->device_type
, ==, VIRTIO_ID_9P
);
198 obj
->destructor
= virtio_9p_pci_destructor
;
199 obj
->start_hw
= virtio_9p_pci_start_hw
;
200 obj
->get_driver
= virtio_9p_pci_get_driver
;
206 * Performs regular expression based search and replace on @a haystack.
208 * @param haystack - input string to be parsed, result of replacement is
209 * stored back to @a haystack
210 * @param pattern - the regular expression pattern for scanning @a haystack
211 * @param replace_fmt - matches of supplied @a pattern are replaced by this,
212 * if necessary glib printf format can be used to add
213 * variable arguments of this function to this
216 static void regex_replace(GString
*haystack
, const char *pattern
,
217 const char *replace_fmt
, ...)
223 va_start(argp
, replace_fmt
);
224 replace
= g_strdup_vprintf(replace_fmt
, argp
);
227 regex
= g_regex_new(pattern
, 0, 0, NULL
);
228 s
= g_regex_replace(regex
, haystack
->str
, -1, 0, replace
, 0, NULL
);
229 g_string_assign(haystack
, s
);
231 g_regex_unref(regex
);
235 void virtio_9p_assign_local_driver(GString
*cmd_line
, const char *args
)
237 g_assert_nonnull(local_test_path
);
239 /* replace 'synth' driver by 'local' driver */
240 regex_replace(cmd_line
, "-fsdev synth,", "-fsdev local,");
242 /* append 'path=...' to '-fsdev ...' group */
243 regex_replace(cmd_line
, "(-fsdev \\w[^ ]*)", "\\1,path='%s'",
250 /* append passed args to '-fsdev ...' group */
251 regex_replace(cmd_line
, "(-fsdev \\w[^ ]*)", "\\1,%s", args
);
254 static void virtio_9p_register_nodes(void)
256 const char *str_simple
= "fsdev=fsdev0,mount_tag=" MOUNT_TAG
;
257 const char *str_addr
= "fsdev=fsdev0,addr=04.0,mount_tag=" MOUNT_TAG
;
260 .devfn
= QPCI_DEVFN(4, 0),
263 QOSGraphEdgeOptions opts
= {
264 .before_cmd_line
= "-fsdev synth,id=fsdev0",
267 /* virtio-9p-device */
268 opts
.extra_device_opts
= str_simple
,
269 qos_node_create_driver("virtio-9p-device", virtio_9p_device_create
);
270 qos_node_consumes("virtio-9p-device", "virtio-bus", &opts
);
271 qos_node_produces("virtio-9p-device", "virtio");
272 qos_node_produces("virtio-9p-device", "virtio-9p");
275 opts
.extra_device_opts
= str_addr
;
276 add_qpci_address(&opts
, &addr
);
277 qos_node_create_driver("virtio-9p-pci", virtio_9p_pci_create
);
278 qos_node_consumes("virtio-9p-pci", "pci-bus", &opts
);
279 qos_node_produces("virtio-9p-pci", "pci-device");
280 qos_node_produces("virtio-9p-pci", "virtio");
281 qos_node_produces("virtio-9p-pci", "virtio-9p");
285 libqos_init(virtio_9p_register_nodes
);