]>
Commit | Line | Data |
---|---|---|
9d447257 EGE |
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 | |
dc0ad02d | 8 | * License version 2.1 as published by the Free Software Foundation. |
9d447257 EGE |
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" | |
0b8fa32f | 21 | #include "qemu/module.h" |
9d447257 | 22 | #include "standard-headers/linux/virtio_ids.h" |
a2ce7dbd PB |
23 | #include "virtio-9p.h" |
24 | #include "qgraph.h" | |
9d447257 EGE |
25 | |
26 | static QGuestAllocator *alloc; | |
3a565c64 CS |
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 | } | |
9d447257 | 55 | |
051f0e5b CS |
56 | /* Deletes directory previously created by create_local_test_dir(). */ |
57 | static void remove_local_test_dir(void) | |
58 | { | |
59 | g_assert(local_test_path != NULL); | |
60 | char *cmd = g_strdup_printf("rm -r '%s'\n", local_test_path); | |
61 | int res = system(cmd); | |
62 | if (res < 0) { | |
63 | /* ignore error, dummy check to prevent compiler error */ | |
64 | } | |
65 | g_free(cmd); | |
66 | } | |
67 | ||
9d447257 EGE |
68 | static void virtio_9p_cleanup(QVirtio9P *interface) |
69 | { | |
70 | qvirtqueue_cleanup(interface->vdev->bus, interface->vq, alloc); | |
71 | } | |
72 | ||
73 | static void virtio_9p_setup(QVirtio9P *interface) | |
74 | { | |
c5bd6d02 SH |
75 | uint64_t features; |
76 | ||
77 | features = qvirtio_get_features(interface->vdev); | |
78 | features &= ~(QVIRTIO_F_BAD_FEATURE | (1ull << VIRTIO_RING_F_EVENT_IDX)); | |
79 | qvirtio_set_features(interface->vdev, features); | |
80 | ||
9d447257 EGE |
81 | interface->vq = qvirtqueue_setup(interface->vdev, alloc, 0); |
82 | qvirtio_set_driver_ok(interface->vdev); | |
83 | } | |
84 | ||
85 | /* virtio-9p-device */ | |
86 | static void virtio_9p_device_destructor(QOSGraphObject *obj) | |
87 | { | |
88 | QVirtio9PDevice *v_9p = (QVirtio9PDevice *) obj; | |
89 | QVirtio9P *v9p = &v_9p->v9p; | |
90 | ||
91 | virtio_9p_cleanup(v9p); | |
92 | } | |
93 | ||
94 | static void virtio_9p_device_start_hw(QOSGraphObject *obj) | |
95 | { | |
96 | QVirtio9PDevice *v_9p = (QVirtio9PDevice *) obj; | |
97 | QVirtio9P *v9p = &v_9p->v9p; | |
98 | ||
99 | virtio_9p_setup(v9p); | |
100 | } | |
101 | ||
102 | static void *virtio_9p_get_driver(QVirtio9P *v_9p, | |
103 | const char *interface) | |
104 | { | |
105 | if (!g_strcmp0(interface, "virtio-9p")) { | |
106 | return v_9p; | |
107 | } | |
108 | if (!g_strcmp0(interface, "virtio")) { | |
109 | return v_9p->vdev; | |
110 | } | |
111 | ||
112 | fprintf(stderr, "%s not present in virtio-9p-device\n", interface); | |
113 | g_assert_not_reached(); | |
114 | } | |
115 | ||
116 | static void *virtio_9p_device_get_driver(void *object, const char *interface) | |
117 | { | |
118 | QVirtio9PDevice *v_9p = object; | |
119 | return virtio_9p_get_driver(&v_9p->v9p, interface); | |
120 | } | |
121 | ||
122 | static void *virtio_9p_device_create(void *virtio_dev, | |
123 | QGuestAllocator *t_alloc, | |
124 | void *addr) | |
125 | { | |
126 | QVirtio9PDevice *virtio_device = g_new0(QVirtio9PDevice, 1); | |
127 | QVirtio9P *interface = &virtio_device->v9p; | |
128 | ||
129 | interface->vdev = virtio_dev; | |
130 | alloc = t_alloc; | |
131 | ||
132 | virtio_device->obj.destructor = virtio_9p_device_destructor; | |
133 | virtio_device->obj.get_driver = virtio_9p_device_get_driver; | |
134 | virtio_device->obj.start_hw = virtio_9p_device_start_hw; | |
135 | ||
136 | return &virtio_device->obj; | |
137 | } | |
138 | ||
139 | /* virtio-9p-pci */ | |
140 | static void virtio_9p_pci_destructor(QOSGraphObject *obj) | |
141 | { | |
142 | QVirtio9PPCI *v9_pci = (QVirtio9PPCI *) obj; | |
143 | QVirtio9P *interface = &v9_pci->v9p; | |
144 | QOSGraphObject *pci_vobj = &v9_pci->pci_vdev.obj; | |
145 | ||
146 | virtio_9p_cleanup(interface); | |
147 | qvirtio_pci_destructor(pci_vobj); | |
148 | } | |
149 | ||
150 | static void virtio_9p_pci_start_hw(QOSGraphObject *obj) | |
151 | { | |
152 | QVirtio9PPCI *v9_pci = (QVirtio9PPCI *) obj; | |
153 | QVirtio9P *interface = &v9_pci->v9p; | |
154 | QOSGraphObject *pci_vobj = &v9_pci->pci_vdev.obj; | |
155 | ||
156 | qvirtio_pci_start_hw(pci_vobj); | |
157 | virtio_9p_setup(interface); | |
158 | } | |
159 | ||
160 | static void *virtio_9p_pci_get_driver(void *object, const char *interface) | |
161 | { | |
162 | QVirtio9PPCI *v_9p = object; | |
163 | if (!g_strcmp0(interface, "pci-device")) { | |
164 | return v_9p->pci_vdev.pdev; | |
165 | } | |
166 | return virtio_9p_get_driver(&v_9p->v9p, interface); | |
167 | } | |
168 | ||
169 | static void *virtio_9p_pci_create(void *pci_bus, QGuestAllocator *t_alloc, | |
170 | void *addr) | |
171 | { | |
172 | QVirtio9PPCI *v9_pci = g_new0(QVirtio9PPCI, 1); | |
173 | QVirtio9P *interface = &v9_pci->v9p; | |
174 | QOSGraphObject *obj = &v9_pci->pci_vdev.obj; | |
175 | ||
176 | virtio_pci_init(&v9_pci->pci_vdev, pci_bus, addr); | |
177 | interface->vdev = &v9_pci->pci_vdev.vdev; | |
178 | alloc = t_alloc; | |
179 | ||
180 | g_assert_cmphex(interface->vdev->device_type, ==, VIRTIO_ID_9P); | |
181 | ||
182 | obj->destructor = virtio_9p_pci_destructor; | |
183 | obj->start_hw = virtio_9p_pci_start_hw; | |
184 | obj->get_driver = virtio_9p_pci_get_driver; | |
185 | ||
186 | return obj; | |
187 | } | |
188 | ||
3a565c64 CS |
189 | /** |
190 | * Performs regular expression based search and replace on @a haystack. | |
191 | * | |
192 | * @param haystack - input string to be parsed, result of replacement is | |
193 | * stored back to @a haystack | |
194 | * @param pattern - the regular expression pattern for scanning @a haystack | |
195 | * @param replace_fmt - matches of supplied @a pattern are replaced by this, | |
196 | * if necessary glib printf format can be used to add | |
197 | * variable arguments of this function to this | |
198 | * replacement string | |
199 | */ | |
200 | static void regex_replace(GString *haystack, const char *pattern, | |
201 | const char *replace_fmt, ...) | |
202 | { | |
203 | GRegex *regex; | |
204 | char *replace, *s; | |
205 | va_list argp; | |
206 | ||
207 | va_start(argp, replace_fmt); | |
208 | replace = g_strdup_vprintf(replace_fmt, argp); | |
209 | va_end(argp); | |
210 | ||
211 | regex = g_regex_new(pattern, 0, 0, NULL); | |
212 | s = g_regex_replace(regex, haystack->str, -1, 0, replace, 0, NULL); | |
213 | g_string_assign(haystack, s); | |
214 | g_free(s); | |
215 | g_regex_unref(regex); | |
216 | g_free(replace); | |
217 | } | |
218 | ||
219 | void virtio_9p_assign_local_driver(GString *cmd_line, const char *args) | |
220 | { | |
221 | g_assert_nonnull(local_test_path); | |
222 | ||
223 | /* replace 'synth' driver by 'local' driver */ | |
224 | regex_replace(cmd_line, "-fsdev synth,", "-fsdev local,"); | |
225 | ||
226 | /* append 'path=...' to '-fsdev ...' group */ | |
227 | regex_replace(cmd_line, "(-fsdev \\w[^ ]*)", "\\1,path='%s'", | |
228 | local_test_path); | |
229 | ||
230 | if (!args) { | |
231 | return; | |
232 | } | |
233 | ||
234 | /* append passed args to '-fsdev ...' group */ | |
235 | regex_replace(cmd_line, "(-fsdev \\w[^ ]*)", "\\1,%s", args); | |
236 | } | |
237 | ||
9d447257 EGE |
238 | static void virtio_9p_register_nodes(void) |
239 | { | |
240 | const char *str_simple = "fsdev=fsdev0,mount_tag=" MOUNT_TAG; | |
241 | const char *str_addr = "fsdev=fsdev0,addr=04.0,mount_tag=" MOUNT_TAG; | |
242 | ||
3a565c64 CS |
243 | /* make sure test dir for the 'local' tests exists and is clean */ |
244 | init_local_test_path(); | |
051f0e5b | 245 | remove_local_test_dir(); |
3a565c64 CS |
246 | create_local_test_dir(); |
247 | ||
9d447257 EGE |
248 | QPCIAddress addr = { |
249 | .devfn = QPCI_DEVFN(4, 0), | |
250 | }; | |
251 | ||
252 | QOSGraphEdgeOptions opts = { | |
253 | .before_cmd_line = "-fsdev synth,id=fsdev0", | |
254 | }; | |
255 | ||
256 | /* virtio-9p-device */ | |
257 | opts.extra_device_opts = str_simple, | |
258 | qos_node_create_driver("virtio-9p-device", virtio_9p_device_create); | |
259 | qos_node_consumes("virtio-9p-device", "virtio-bus", &opts); | |
260 | qos_node_produces("virtio-9p-device", "virtio"); | |
261 | qos_node_produces("virtio-9p-device", "virtio-9p"); | |
262 | ||
263 | /* virtio-9p-pci */ | |
264 | opts.extra_device_opts = str_addr; | |
265 | add_qpci_address(&opts, &addr); | |
266 | qos_node_create_driver("virtio-9p-pci", virtio_9p_pci_create); | |
267 | qos_node_consumes("virtio-9p-pci", "pci-bus", &opts); | |
268 | qos_node_produces("virtio-9p-pci", "pci-device"); | |
269 | qos_node_produces("virtio-9p-pci", "virtio"); | |
270 | qos_node_produces("virtio-9p-pci", "virtio-9p"); | |
271 | ||
272 | } | |
273 | ||
274 | libqos_init(virtio_9p_register_nodes); |