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 local_test_path
= concat_path(pwd
, "qtest-9p-local");
42 /* Creates the directory for the 9pfs 'local' filesystem driver to access. */
43 static void create_local_test_dir(void)
47 g_assert(local_test_path
!= NULL
);
48 mkdir(local_test_path
, 0777);
50 /* ensure test directory exists now ... */
51 g_assert(stat(local_test_path
, &st
) == 0);
52 /* ... and is actually a directory */
53 g_assert((st
.st_mode
& S_IFMT
) == S_IFDIR
);
56 static void virtio_9p_cleanup(QVirtio9P
*interface
)
58 qvirtqueue_cleanup(interface
->vdev
->bus
, interface
->vq
, alloc
);
61 static void virtio_9p_setup(QVirtio9P
*interface
)
65 features
= qvirtio_get_features(interface
->vdev
);
66 features
&= ~(QVIRTIO_F_BAD_FEATURE
| (1ull << VIRTIO_RING_F_EVENT_IDX
));
67 qvirtio_set_features(interface
->vdev
, features
);
69 interface
->vq
= qvirtqueue_setup(interface
->vdev
, alloc
, 0);
70 qvirtio_set_driver_ok(interface
->vdev
);
73 /* virtio-9p-device */
74 static void virtio_9p_device_destructor(QOSGraphObject
*obj
)
76 QVirtio9PDevice
*v_9p
= (QVirtio9PDevice
*) obj
;
77 QVirtio9P
*v9p
= &v_9p
->v9p
;
79 virtio_9p_cleanup(v9p
);
82 static void virtio_9p_device_start_hw(QOSGraphObject
*obj
)
84 QVirtio9PDevice
*v_9p
= (QVirtio9PDevice
*) obj
;
85 QVirtio9P
*v9p
= &v_9p
->v9p
;
90 static void *virtio_9p_get_driver(QVirtio9P
*v_9p
,
91 const char *interface
)
93 if (!g_strcmp0(interface
, "virtio-9p")) {
96 if (!g_strcmp0(interface
, "virtio")) {
100 fprintf(stderr
, "%s not present in virtio-9p-device\n", interface
);
101 g_assert_not_reached();
104 static void *virtio_9p_device_get_driver(void *object
, const char *interface
)
106 QVirtio9PDevice
*v_9p
= object
;
107 return virtio_9p_get_driver(&v_9p
->v9p
, interface
);
110 static void *virtio_9p_device_create(void *virtio_dev
,
111 QGuestAllocator
*t_alloc
,
114 QVirtio9PDevice
*virtio_device
= g_new0(QVirtio9PDevice
, 1);
115 QVirtio9P
*interface
= &virtio_device
->v9p
;
117 interface
->vdev
= virtio_dev
;
120 virtio_device
->obj
.destructor
= virtio_9p_device_destructor
;
121 virtio_device
->obj
.get_driver
= virtio_9p_device_get_driver
;
122 virtio_device
->obj
.start_hw
= virtio_9p_device_start_hw
;
124 return &virtio_device
->obj
;
128 static void virtio_9p_pci_destructor(QOSGraphObject
*obj
)
130 QVirtio9PPCI
*v9_pci
= (QVirtio9PPCI
*) obj
;
131 QVirtio9P
*interface
= &v9_pci
->v9p
;
132 QOSGraphObject
*pci_vobj
= &v9_pci
->pci_vdev
.obj
;
134 virtio_9p_cleanup(interface
);
135 qvirtio_pci_destructor(pci_vobj
);
138 static void virtio_9p_pci_start_hw(QOSGraphObject
*obj
)
140 QVirtio9PPCI
*v9_pci
= (QVirtio9PPCI
*) obj
;
141 QVirtio9P
*interface
= &v9_pci
->v9p
;
142 QOSGraphObject
*pci_vobj
= &v9_pci
->pci_vdev
.obj
;
144 qvirtio_pci_start_hw(pci_vobj
);
145 virtio_9p_setup(interface
);
148 static void *virtio_9p_pci_get_driver(void *object
, const char *interface
)
150 QVirtio9PPCI
*v_9p
= object
;
151 if (!g_strcmp0(interface
, "pci-device")) {
152 return v_9p
->pci_vdev
.pdev
;
154 return virtio_9p_get_driver(&v_9p
->v9p
, interface
);
157 static void *virtio_9p_pci_create(void *pci_bus
, QGuestAllocator
*t_alloc
,
160 QVirtio9PPCI
*v9_pci
= g_new0(QVirtio9PPCI
, 1);
161 QVirtio9P
*interface
= &v9_pci
->v9p
;
162 QOSGraphObject
*obj
= &v9_pci
->pci_vdev
.obj
;
164 virtio_pci_init(&v9_pci
->pci_vdev
, pci_bus
, addr
);
165 interface
->vdev
= &v9_pci
->pci_vdev
.vdev
;
168 g_assert_cmphex(interface
->vdev
->device_type
, ==, VIRTIO_ID_9P
);
170 obj
->destructor
= virtio_9p_pci_destructor
;
171 obj
->start_hw
= virtio_9p_pci_start_hw
;
172 obj
->get_driver
= virtio_9p_pci_get_driver
;
178 * Performs regular expression based search and replace on @a haystack.
180 * @param haystack - input string to be parsed, result of replacement is
181 * stored back to @a haystack
182 * @param pattern - the regular expression pattern for scanning @a haystack
183 * @param replace_fmt - matches of supplied @a pattern are replaced by this,
184 * if necessary glib printf format can be used to add
185 * variable arguments of this function to this
188 static void regex_replace(GString
*haystack
, const char *pattern
,
189 const char *replace_fmt
, ...)
195 va_start(argp
, replace_fmt
);
196 replace
= g_strdup_vprintf(replace_fmt
, argp
);
199 regex
= g_regex_new(pattern
, 0, 0, NULL
);
200 s
= g_regex_replace(regex
, haystack
->str
, -1, 0, replace
, 0, NULL
);
201 g_string_assign(haystack
, s
);
203 g_regex_unref(regex
);
207 void virtio_9p_assign_local_driver(GString
*cmd_line
, const char *args
)
209 g_assert_nonnull(local_test_path
);
211 /* replace 'synth' driver by 'local' driver */
212 regex_replace(cmd_line
, "-fsdev synth,", "-fsdev local,");
214 /* append 'path=...' to '-fsdev ...' group */
215 regex_replace(cmd_line
, "(-fsdev \\w[^ ]*)", "\\1,path='%s'",
222 /* append passed args to '-fsdev ...' group */
223 regex_replace(cmd_line
, "(-fsdev \\w[^ ]*)", "\\1,%s", args
);
226 static void virtio_9p_register_nodes(void)
228 const char *str_simple
= "fsdev=fsdev0,mount_tag=" MOUNT_TAG
;
229 const char *str_addr
= "fsdev=fsdev0,addr=04.0,mount_tag=" MOUNT_TAG
;
231 /* make sure test dir for the 'local' tests exists and is clean */
232 init_local_test_path();
233 create_local_test_dir();
236 .devfn
= QPCI_DEVFN(4, 0),
239 QOSGraphEdgeOptions opts
= {
240 .before_cmd_line
= "-fsdev synth,id=fsdev0",
243 /* virtio-9p-device */
244 opts
.extra_device_opts
= str_simple
,
245 qos_node_create_driver("virtio-9p-device", virtio_9p_device_create
);
246 qos_node_consumes("virtio-9p-device", "virtio-bus", &opts
);
247 qos_node_produces("virtio-9p-device", "virtio");
248 qos_node_produces("virtio-9p-device", "virtio-9p");
251 opts
.extra_device_opts
= str_addr
;
252 add_qpci_address(&opts
, &addr
);
253 qos_node_create_driver("virtio-9p-pci", virtio_9p_pci_create
);
254 qos_node_consumes("virtio-9p-pci", "pci-bus", &opts
);
255 qos_node_produces("virtio-9p-pci", "pci-device");
256 qos_node_produces("virtio-9p-pci", "virtio");
257 qos_node_produces("virtio-9p-pci", "virtio-9p");
261 libqos_init(virtio_9p_register_nodes
);