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