xref: /qemu/tests/qtest/libqos/virtio-9p.c (revision 92eecfff)
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     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));
42     }
43     g_assert(local_test_path);
44     g_free(pwd);
45 }
46 
47 void virtio_9p_create_local_test_dir(void)
48 {
49     struct stat st;
50     int res;
51 
52     init_local_test_path();
53 
54     g_assert(local_test_path != NULL);
55     res = mkdir(local_test_path, 0777);
56     if (res < 0) {
57         g_test_message("mkdir('%s') failed: %s", local_test_path,
58                        strerror(errno));
59     }
60 
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);
65 }
66 
67 void virtio_9p_remove_local_test_dir(void)
68 {
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);
72     if (res < 0) {
73         /* ignore error, dummy check to prevent compiler error */
74     }
75     g_free(cmd);
76 }
77 
78 char *virtio_9p_test_path(const char *path)
79 {
80     g_assert(local_test_path);
81     return concat_path(local_test_path, path);
82 }
83 
84 static void virtio_9p_cleanup(QVirtio9P *interface)
85 {
86     qvirtqueue_cleanup(interface->vdev->bus, interface->vq, alloc);
87 }
88 
89 static void virtio_9p_setup(QVirtio9P *interface)
90 {
91     uint64_t features;
92 
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);
96 
97     interface->vq = qvirtqueue_setup(interface->vdev, alloc, 0);
98     qvirtio_set_driver_ok(interface->vdev);
99 }
100 
101 /* virtio-9p-device */
102 static void virtio_9p_device_destructor(QOSGraphObject *obj)
103 {
104     QVirtio9PDevice *v_9p = (QVirtio9PDevice *) obj;
105     QVirtio9P *v9p = &v_9p->v9p;
106 
107     virtio_9p_cleanup(v9p);
108 }
109 
110 static void virtio_9p_device_start_hw(QOSGraphObject *obj)
111 {
112     QVirtio9PDevice *v_9p = (QVirtio9PDevice *) obj;
113     QVirtio9P *v9p = &v_9p->v9p;
114 
115     virtio_9p_setup(v9p);
116 }
117 
118 static void *virtio_9p_get_driver(QVirtio9P *v_9p,
119                                          const char *interface)
120 {
121     if (!g_strcmp0(interface, "virtio-9p")) {
122         return v_9p;
123     }
124     if (!g_strcmp0(interface, "virtio")) {
125         return v_9p->vdev;
126     }
127 
128     fprintf(stderr, "%s not present in virtio-9p-device\n", interface);
129     g_assert_not_reached();
130 }
131 
132 static void *virtio_9p_device_get_driver(void *object, const char *interface)
133 {
134     QVirtio9PDevice *v_9p = object;
135     return virtio_9p_get_driver(&v_9p->v9p, interface);
136 }
137 
138 static void *virtio_9p_device_create(void *virtio_dev,
139                                      QGuestAllocator *t_alloc,
140                                      void *addr)
141 {
142     QVirtio9PDevice *virtio_device = g_new0(QVirtio9PDevice, 1);
143     QVirtio9P *interface = &virtio_device->v9p;
144 
145     interface->vdev = virtio_dev;
146     alloc = t_alloc;
147 
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;
151 
152     return &virtio_device->obj;
153 }
154 
155 /* virtio-9p-pci */
156 static void virtio_9p_pci_destructor(QOSGraphObject *obj)
157 {
158     QVirtio9PPCI *v9_pci = (QVirtio9PPCI *) obj;
159     QVirtio9P *interface = &v9_pci->v9p;
160     QOSGraphObject *pci_vobj =  &v9_pci->pci_vdev.obj;
161 
162     virtio_9p_cleanup(interface);
163     qvirtio_pci_destructor(pci_vobj);
164 }
165 
166 static void virtio_9p_pci_start_hw(QOSGraphObject *obj)
167 {
168     QVirtio9PPCI *v9_pci = (QVirtio9PPCI *) obj;
169     QVirtio9P *interface = &v9_pci->v9p;
170     QOSGraphObject *pci_vobj =  &v9_pci->pci_vdev.obj;
171 
172     qvirtio_pci_start_hw(pci_vobj);
173     virtio_9p_setup(interface);
174 }
175 
176 static void *virtio_9p_pci_get_driver(void *object, const char *interface)
177 {
178     QVirtio9PPCI *v_9p = object;
179     if (!g_strcmp0(interface, "pci-device")) {
180         return v_9p->pci_vdev.pdev;
181     }
182     return virtio_9p_get_driver(&v_9p->v9p, interface);
183 }
184 
185 static void *virtio_9p_pci_create(void *pci_bus, QGuestAllocator *t_alloc,
186                                   void *addr)
187 {
188     QVirtio9PPCI *v9_pci = g_new0(QVirtio9PPCI, 1);
189     QVirtio9P *interface = &v9_pci->v9p;
190     QOSGraphObject *obj = &v9_pci->pci_vdev.obj;
191 
192     virtio_pci_init(&v9_pci->pci_vdev, pci_bus, addr);
193     interface->vdev = &v9_pci->pci_vdev.vdev;
194     alloc = t_alloc;
195 
196     g_assert_cmphex(interface->vdev->device_type, ==, VIRTIO_ID_9P);
197 
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;
201 
202     return obj;
203 }
204 
205 /**
206  * Performs regular expression based search and replace on @a haystack.
207  *
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
214  *                      replacement string
215  */
216 static void regex_replace(GString *haystack, const char *pattern,
217                           const char *replace_fmt, ...)
218 {
219     GRegex *regex;
220     char *replace, *s;
221     va_list argp;
222 
223     va_start(argp, replace_fmt);
224     replace = g_strdup_vprintf(replace_fmt, argp);
225     va_end(argp);
226 
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);
230     g_free(s);
231     g_regex_unref(regex);
232     g_free(replace);
233 }
234 
235 void virtio_9p_assign_local_driver(GString *cmd_line, const char *args)
236 {
237     g_assert_nonnull(local_test_path);
238 
239     /* replace 'synth' driver by 'local' driver */
240     regex_replace(cmd_line, "-fsdev synth,", "-fsdev local,");
241 
242     /* append 'path=...' to '-fsdev ...' group */
243     regex_replace(cmd_line, "(-fsdev \\w[^ ]*)", "\\1,path='%s'",
244                   local_test_path);
245 
246     if (!args) {
247         return;
248     }
249 
250     /* append passed args to '-fsdev ...' group */
251     regex_replace(cmd_line, "(-fsdev \\w[^ ]*)", "\\1,%s", args);
252 }
253 
254 static void virtio_9p_register_nodes(void)
255 {
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;
258 
259     QPCIAddress addr = {
260         .devfn = QPCI_DEVFN(4, 0),
261     };
262 
263     QOSGraphEdgeOptions opts = {
264         .before_cmd_line = "-fsdev synth,id=fsdev0",
265     };
266 
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");
273 
274     /* virtio-9p-pci */
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");
282 
283 }
284 
285 libqos_init(virtio_9p_register_nodes);
286