]> git.proxmox.com Git - mirror_qemu.git/blob - tests/qtest/libqos/virtio-9p.c
ee331166de884fa8332d5273df676f81a09de450
[mirror_qemu.git] / tests / qtest / libqos / virtio-9p.c
1 /*
2 * libqos driver framework
3 *
4 * Copyright (c) 2018 Emanuele Giuseppe Esposito <e.emanuelegiuseppe@gmail.com>
5 *
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.
9 *
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.
14 *
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/>
17 */
18
19 #include "qemu/osdep.h"
20 #include "libqtest.h"
21 #include "qemu/module.h"
22 #include "standard-headers/linux/virtio_ids.h"
23 #include "virtio-9p.h"
24 #include "qgraph.h"
25
26 static QGuestAllocator *alloc;
27 static char *local_test_path;
28
29 /* Concatenates the passed 2 pathes. Returned result must be freed. */
30 static char *concat_path(const char* a, const char* b)
31 {
32 return g_build_filename(a, b, NULL);
33 }
34
35 static void init_local_test_path(void)
36 {
37 char *pwd = g_get_current_dir();
38 local_test_path = concat_path(pwd, "qtest-9p-local");
39 g_free(pwd);
40 }
41
42 /* Creates the directory for the 9pfs 'local' filesystem driver to access. */
43 static void create_local_test_dir(void)
44 {
45 struct stat st;
46
47 g_assert(local_test_path != NULL);
48 mkdir(local_test_path, 0777);
49
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);
54 }
55
56 static void virtio_9p_cleanup(QVirtio9P *interface)
57 {
58 qvirtqueue_cleanup(interface->vdev->bus, interface->vq, alloc);
59 }
60
61 static void virtio_9p_setup(QVirtio9P *interface)
62 {
63 uint64_t features;
64
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);
68
69 interface->vq = qvirtqueue_setup(interface->vdev, alloc, 0);
70 qvirtio_set_driver_ok(interface->vdev);
71 }
72
73 /* virtio-9p-device */
74 static void virtio_9p_device_destructor(QOSGraphObject *obj)
75 {
76 QVirtio9PDevice *v_9p = (QVirtio9PDevice *) obj;
77 QVirtio9P *v9p = &v_9p->v9p;
78
79 virtio_9p_cleanup(v9p);
80 }
81
82 static void virtio_9p_device_start_hw(QOSGraphObject *obj)
83 {
84 QVirtio9PDevice *v_9p = (QVirtio9PDevice *) obj;
85 QVirtio9P *v9p = &v_9p->v9p;
86
87 virtio_9p_setup(v9p);
88 }
89
90 static void *virtio_9p_get_driver(QVirtio9P *v_9p,
91 const char *interface)
92 {
93 if (!g_strcmp0(interface, "virtio-9p")) {
94 return v_9p;
95 }
96 if (!g_strcmp0(interface, "virtio")) {
97 return v_9p->vdev;
98 }
99
100 fprintf(stderr, "%s not present in virtio-9p-device\n", interface);
101 g_assert_not_reached();
102 }
103
104 static void *virtio_9p_device_get_driver(void *object, const char *interface)
105 {
106 QVirtio9PDevice *v_9p = object;
107 return virtio_9p_get_driver(&v_9p->v9p, interface);
108 }
109
110 static void *virtio_9p_device_create(void *virtio_dev,
111 QGuestAllocator *t_alloc,
112 void *addr)
113 {
114 QVirtio9PDevice *virtio_device = g_new0(QVirtio9PDevice, 1);
115 QVirtio9P *interface = &virtio_device->v9p;
116
117 interface->vdev = virtio_dev;
118 alloc = t_alloc;
119
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;
123
124 return &virtio_device->obj;
125 }
126
127 /* virtio-9p-pci */
128 static void virtio_9p_pci_destructor(QOSGraphObject *obj)
129 {
130 QVirtio9PPCI *v9_pci = (QVirtio9PPCI *) obj;
131 QVirtio9P *interface = &v9_pci->v9p;
132 QOSGraphObject *pci_vobj = &v9_pci->pci_vdev.obj;
133
134 virtio_9p_cleanup(interface);
135 qvirtio_pci_destructor(pci_vobj);
136 }
137
138 static void virtio_9p_pci_start_hw(QOSGraphObject *obj)
139 {
140 QVirtio9PPCI *v9_pci = (QVirtio9PPCI *) obj;
141 QVirtio9P *interface = &v9_pci->v9p;
142 QOSGraphObject *pci_vobj = &v9_pci->pci_vdev.obj;
143
144 qvirtio_pci_start_hw(pci_vobj);
145 virtio_9p_setup(interface);
146 }
147
148 static void *virtio_9p_pci_get_driver(void *object, const char *interface)
149 {
150 QVirtio9PPCI *v_9p = object;
151 if (!g_strcmp0(interface, "pci-device")) {
152 return v_9p->pci_vdev.pdev;
153 }
154 return virtio_9p_get_driver(&v_9p->v9p, interface);
155 }
156
157 static void *virtio_9p_pci_create(void *pci_bus, QGuestAllocator *t_alloc,
158 void *addr)
159 {
160 QVirtio9PPCI *v9_pci = g_new0(QVirtio9PPCI, 1);
161 QVirtio9P *interface = &v9_pci->v9p;
162 QOSGraphObject *obj = &v9_pci->pci_vdev.obj;
163
164 virtio_pci_init(&v9_pci->pci_vdev, pci_bus, addr);
165 interface->vdev = &v9_pci->pci_vdev.vdev;
166 alloc = t_alloc;
167
168 g_assert_cmphex(interface->vdev->device_type, ==, VIRTIO_ID_9P);
169
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;
173
174 return obj;
175 }
176
177 /**
178 * Performs regular expression based search and replace on @a haystack.
179 *
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
186 * replacement string
187 */
188 static void regex_replace(GString *haystack, const char *pattern,
189 const char *replace_fmt, ...)
190 {
191 GRegex *regex;
192 char *replace, *s;
193 va_list argp;
194
195 va_start(argp, replace_fmt);
196 replace = g_strdup_vprintf(replace_fmt, argp);
197 va_end(argp);
198
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);
202 g_free(s);
203 g_regex_unref(regex);
204 g_free(replace);
205 }
206
207 void virtio_9p_assign_local_driver(GString *cmd_line, const char *args)
208 {
209 g_assert_nonnull(local_test_path);
210
211 /* replace 'synth' driver by 'local' driver */
212 regex_replace(cmd_line, "-fsdev synth,", "-fsdev local,");
213
214 /* append 'path=...' to '-fsdev ...' group */
215 regex_replace(cmd_line, "(-fsdev \\w[^ ]*)", "\\1,path='%s'",
216 local_test_path);
217
218 if (!args) {
219 return;
220 }
221
222 /* append passed args to '-fsdev ...' group */
223 regex_replace(cmd_line, "(-fsdev \\w[^ ]*)", "\\1,%s", args);
224 }
225
226 static void virtio_9p_register_nodes(void)
227 {
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;
230
231 /* make sure test dir for the 'local' tests exists and is clean */
232 init_local_test_path();
233 create_local_test_dir();
234
235 QPCIAddress addr = {
236 .devfn = QPCI_DEVFN(4, 0),
237 };
238
239 QOSGraphEdgeOptions opts = {
240 .before_cmd_line = "-fsdev synth,id=fsdev0",
241 };
242
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");
249
250 /* virtio-9p-pci */
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");
258
259 }
260
261 libqos_init(virtio_9p_register_nodes);