xref: /openbmc/qemu/tests/qtest/libqos/virtio-9p.c (revision 3db29dcac23da85486704ef9e7a8e7217f7829cd)
11cf4323eSThomas Huth /*
21cf4323eSThomas Huth  * libqos driver framework
31cf4323eSThomas Huth  *
41cf4323eSThomas Huth  * Copyright (c) 2018 Emanuele Giuseppe Esposito <e.emanuelegiuseppe@gmail.com>
51cf4323eSThomas Huth  *
61cf4323eSThomas Huth  * This library is free software; you can redistribute it and/or
71cf4323eSThomas Huth  * modify it under the terms of the GNU Lesser General Public
8dc0ad02dSThomas Huth  * License version 2.1 as published by the Free Software Foundation.
91cf4323eSThomas Huth  *
101cf4323eSThomas Huth  * This library is distributed in the hope that it will be useful,
111cf4323eSThomas Huth  * but WITHOUT ANY WARRANTY; without even the implied warranty of
121cf4323eSThomas Huth  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
131cf4323eSThomas Huth  * Lesser General Public License for more details.
141cf4323eSThomas Huth  *
151cf4323eSThomas Huth  * You should have received a copy of the GNU Lesser General Public
161cf4323eSThomas Huth  * License along with this library; if not, see <http://www.gnu.org/licenses/>
171cf4323eSThomas Huth  */
181cf4323eSThomas Huth 
196f569084SChristian Schoenebeck /*
206f569084SChristian Schoenebeck  * Not so fast! You might want to read the 9p developer docs first:
216f569084SChristian Schoenebeck  * https://wiki.qemu.org/Documentation/9p
226f569084SChristian Schoenebeck  */
236f569084SChristian Schoenebeck 
241cf4323eSThomas Huth #include "qemu/osdep.h"
25907b5105SMarc-André Lureau #include "../libqtest.h"
261cf4323eSThomas Huth #include "qemu/module.h"
271cf4323eSThomas Huth #include "standard-headers/linux/virtio_ids.h"
28a2ce7dbdSPaolo Bonzini #include "virtio-9p.h"
29a2ce7dbdSPaolo Bonzini #include "qgraph.h"
301cf4323eSThomas Huth 
311cf4323eSThomas Huth static QGuestAllocator *alloc;
323a565c64SChristian Schoenebeck static char *local_test_path;
333a565c64SChristian Schoenebeck 
342cb40d44SStefan Weil /* Concatenates the passed 2 paths. Returned result must be freed. */
concat_path(const char * a,const char * b)353a565c64SChristian Schoenebeck static char *concat_path(const char* a, const char* b)
363a565c64SChristian Schoenebeck {
373a565c64SChristian Schoenebeck     return g_build_filename(a, b, NULL);
383a565c64SChristian Schoenebeck }
393a565c64SChristian Schoenebeck 
virtio_9p_create_local_test_dir(void)4068c66a96SChristian Schoenebeck void virtio_9p_create_local_test_dir(void)
413a565c64SChristian Schoenebeck {
42ba6112e4SGreg Kurz     g_assert(local_test_path == NULL);
4368c66a96SChristian Schoenebeck     struct stat st;
44494fbbd3SGreg Kurz     g_autofree char *pwd = g_get_current_dir();
45ba6112e4SGreg Kurz     /*
46ba6112e4SGreg Kurz      * template gets cached into local_test_path and freed in
47ba6112e4SGreg Kurz      * virtio_9p_remove_local_test_dir().
48ba6112e4SGreg Kurz      */
49136b7af2SChristian Schoenebeck     char *template = concat_path(pwd, "qtest-9p-local-XXXXXX");
5068c66a96SChristian Schoenebeck 
513c239aa7SBin Meng     local_test_path = g_mkdtemp(template);
52136b7af2SChristian Schoenebeck     if (!local_test_path) {
533c239aa7SBin Meng         g_test_message("g_mkdtemp('%s') failed: %s", template, strerror(errno));
54136b7af2SChristian Schoenebeck     }
55136b7af2SChristian Schoenebeck 
563a565c64SChristian Schoenebeck     g_assert(local_test_path != NULL);
573a565c64SChristian Schoenebeck 
583a565c64SChristian Schoenebeck     /* ensure test directory exists now ... */
593a565c64SChristian Schoenebeck     g_assert(stat(local_test_path, &st) == 0);
603a565c64SChristian Schoenebeck     /* ... and is actually a directory */
613a565c64SChristian Schoenebeck     g_assert((st.st_mode & S_IFMT) == S_IFDIR);
623a565c64SChristian Schoenebeck }
631cf4323eSThomas Huth 
virtio_9p_remove_local_test_dir(void)648db193c4SChristian Schoenebeck void virtio_9p_remove_local_test_dir(void)
65051f0e5bSChristian Schoenebeck {
66051f0e5bSChristian Schoenebeck     g_assert(local_test_path != NULL);
67494fbbd3SGreg Kurz     g_autofree char *cmd = g_strdup_printf("rm -fr '%s'\n", local_test_path);
68051f0e5bSChristian Schoenebeck     int res = system(cmd);
69051f0e5bSChristian Schoenebeck     if (res < 0) {
70051f0e5bSChristian Schoenebeck         /* ignore error, dummy check to prevent compiler error */
71051f0e5bSChristian Schoenebeck     }
72ba6112e4SGreg Kurz     g_free(local_test_path);
73ba6112e4SGreg Kurz     local_test_path = NULL;
74051f0e5bSChristian Schoenebeck }
75051f0e5bSChristian Schoenebeck 
virtio_9p_test_path(const char * path)76e55b178bSChristian Schoenebeck char *virtio_9p_test_path(const char *path)
77e55b178bSChristian Schoenebeck {
78e55b178bSChristian Schoenebeck     g_assert(local_test_path);
79e55b178bSChristian Schoenebeck     return concat_path(local_test_path, path);
80e55b178bSChristian Schoenebeck }
81e55b178bSChristian Schoenebeck 
virtio_9p_cleanup(QVirtio9P * interface)821cf4323eSThomas Huth static void virtio_9p_cleanup(QVirtio9P *interface)
831cf4323eSThomas Huth {
841cf4323eSThomas Huth     qvirtqueue_cleanup(interface->vdev->bus, interface->vq, alloc);
851cf4323eSThomas Huth }
861cf4323eSThomas Huth 
virtio_9p_setup(QVirtio9P * interface)871cf4323eSThomas Huth static void virtio_9p_setup(QVirtio9P *interface)
881cf4323eSThomas Huth {
891cf4323eSThomas Huth     uint64_t features;
901cf4323eSThomas Huth 
911cf4323eSThomas Huth     features = qvirtio_get_features(interface->vdev);
921cf4323eSThomas Huth     features &= ~(QVIRTIO_F_BAD_FEATURE | (1ull << VIRTIO_RING_F_EVENT_IDX));
931cf4323eSThomas Huth     qvirtio_set_features(interface->vdev, features);
941cf4323eSThomas Huth 
951cf4323eSThomas Huth     interface->vq = qvirtqueue_setup(interface->vdev, alloc, 0);
961cf4323eSThomas Huth     qvirtio_set_driver_ok(interface->vdev);
971cf4323eSThomas Huth }
981cf4323eSThomas Huth 
991cf4323eSThomas Huth /* virtio-9p-device */
virtio_9p_device_destructor(QOSGraphObject * obj)1001cf4323eSThomas Huth static void virtio_9p_device_destructor(QOSGraphObject *obj)
1011cf4323eSThomas Huth {
1021cf4323eSThomas Huth     QVirtio9PDevice *v_9p = (QVirtio9PDevice *) obj;
1031cf4323eSThomas Huth     QVirtio9P *v9p = &v_9p->v9p;
1041cf4323eSThomas Huth 
1051cf4323eSThomas Huth     virtio_9p_cleanup(v9p);
1061cf4323eSThomas Huth }
1071cf4323eSThomas Huth 
virtio_9p_device_start_hw(QOSGraphObject * obj)1081cf4323eSThomas Huth static void virtio_9p_device_start_hw(QOSGraphObject *obj)
1091cf4323eSThomas Huth {
1101cf4323eSThomas Huth     QVirtio9PDevice *v_9p = (QVirtio9PDevice *) obj;
1111cf4323eSThomas Huth     QVirtio9P *v9p = &v_9p->v9p;
1121cf4323eSThomas Huth 
1131cf4323eSThomas Huth     virtio_9p_setup(v9p);
1141cf4323eSThomas Huth }
1151cf4323eSThomas Huth 
virtio_9p_get_driver(QVirtio9P * v_9p,const char * interface)1161cf4323eSThomas Huth static void *virtio_9p_get_driver(QVirtio9P *v_9p,
1171cf4323eSThomas Huth                                          const char *interface)
1181cf4323eSThomas Huth {
1191cf4323eSThomas Huth     if (!g_strcmp0(interface, "virtio-9p")) {
1201cf4323eSThomas Huth         return v_9p;
1211cf4323eSThomas Huth     }
1221cf4323eSThomas Huth     if (!g_strcmp0(interface, "virtio")) {
1231cf4323eSThomas Huth         return v_9p->vdev;
1241cf4323eSThomas Huth     }
1251cf4323eSThomas Huth 
1261cf4323eSThomas Huth     fprintf(stderr, "%s not present in virtio-9p-device\n", interface);
1271cf4323eSThomas Huth     g_assert_not_reached();
1281cf4323eSThomas Huth }
1291cf4323eSThomas Huth 
virtio_9p_device_get_driver(void * object,const char * interface)1301cf4323eSThomas Huth static void *virtio_9p_device_get_driver(void *object, const char *interface)
1311cf4323eSThomas Huth {
1321cf4323eSThomas Huth     QVirtio9PDevice *v_9p = object;
1331cf4323eSThomas Huth     return virtio_9p_get_driver(&v_9p->v9p, interface);
1341cf4323eSThomas Huth }
1351cf4323eSThomas Huth 
virtio_9p_device_create(void * virtio_dev,QGuestAllocator * t_alloc,void * addr)1361cf4323eSThomas Huth static void *virtio_9p_device_create(void *virtio_dev,
1371cf4323eSThomas Huth                                      QGuestAllocator *t_alloc,
1381cf4323eSThomas Huth                                      void *addr)
1391cf4323eSThomas Huth {
1401cf4323eSThomas Huth     QVirtio9PDevice *virtio_device = g_new0(QVirtio9PDevice, 1);
1411cf4323eSThomas Huth     QVirtio9P *interface = &virtio_device->v9p;
1421cf4323eSThomas Huth 
1431cf4323eSThomas Huth     interface->vdev = virtio_dev;
1441cf4323eSThomas Huth     alloc = t_alloc;
1451cf4323eSThomas Huth 
1461cf4323eSThomas Huth     virtio_device->obj.destructor = virtio_9p_device_destructor;
1471cf4323eSThomas Huth     virtio_device->obj.get_driver = virtio_9p_device_get_driver;
1481cf4323eSThomas Huth     virtio_device->obj.start_hw = virtio_9p_device_start_hw;
1491cf4323eSThomas Huth 
1501cf4323eSThomas Huth     return &virtio_device->obj;
1511cf4323eSThomas Huth }
1521cf4323eSThomas Huth 
1531cf4323eSThomas Huth /* virtio-9p-pci */
virtio_9p_pci_destructor(QOSGraphObject * obj)1541cf4323eSThomas Huth static void virtio_9p_pci_destructor(QOSGraphObject *obj)
1551cf4323eSThomas Huth {
1561cf4323eSThomas Huth     QVirtio9PPCI *v9_pci = (QVirtio9PPCI *) obj;
1571cf4323eSThomas Huth     QVirtio9P *interface = &v9_pci->v9p;
1581cf4323eSThomas Huth     QOSGraphObject *pci_vobj =  &v9_pci->pci_vdev.obj;
1591cf4323eSThomas Huth 
1601cf4323eSThomas Huth     virtio_9p_cleanup(interface);
1611cf4323eSThomas Huth     qvirtio_pci_destructor(pci_vobj);
1621cf4323eSThomas Huth }
1631cf4323eSThomas Huth 
virtio_9p_pci_start_hw(QOSGraphObject * obj)1641cf4323eSThomas Huth static void virtio_9p_pci_start_hw(QOSGraphObject *obj)
1651cf4323eSThomas Huth {
1661cf4323eSThomas Huth     QVirtio9PPCI *v9_pci = (QVirtio9PPCI *) obj;
1671cf4323eSThomas Huth     QVirtio9P *interface = &v9_pci->v9p;
1681cf4323eSThomas Huth     QOSGraphObject *pci_vobj =  &v9_pci->pci_vdev.obj;
1691cf4323eSThomas Huth 
1701cf4323eSThomas Huth     qvirtio_pci_start_hw(pci_vobj);
1711cf4323eSThomas Huth     virtio_9p_setup(interface);
1721cf4323eSThomas Huth }
1731cf4323eSThomas Huth 
virtio_9p_pci_get_driver(void * object,const char * interface)1741cf4323eSThomas Huth static void *virtio_9p_pci_get_driver(void *object, const char *interface)
1751cf4323eSThomas Huth {
1761cf4323eSThomas Huth     QVirtio9PPCI *v_9p = object;
1771cf4323eSThomas Huth     if (!g_strcmp0(interface, "pci-device")) {
1781cf4323eSThomas Huth         return v_9p->pci_vdev.pdev;
1791cf4323eSThomas Huth     }
1801cf4323eSThomas Huth     return virtio_9p_get_driver(&v_9p->v9p, interface);
1811cf4323eSThomas Huth }
1821cf4323eSThomas Huth 
virtio_9p_pci_create(void * pci_bus,QGuestAllocator * t_alloc,void * addr)1831cf4323eSThomas Huth static void *virtio_9p_pci_create(void *pci_bus, QGuestAllocator *t_alloc,
1841cf4323eSThomas Huth                                   void *addr)
1851cf4323eSThomas Huth {
1861cf4323eSThomas Huth     QVirtio9PPCI *v9_pci = g_new0(QVirtio9PPCI, 1);
1871cf4323eSThomas Huth     QVirtio9P *interface = &v9_pci->v9p;
1881cf4323eSThomas Huth     QOSGraphObject *obj = &v9_pci->pci_vdev.obj;
1891cf4323eSThomas Huth 
1901cf4323eSThomas Huth     virtio_pci_init(&v9_pci->pci_vdev, pci_bus, addr);
1911cf4323eSThomas Huth     interface->vdev = &v9_pci->pci_vdev.vdev;
1921cf4323eSThomas Huth     alloc = t_alloc;
1931cf4323eSThomas Huth 
1941cf4323eSThomas Huth     g_assert_cmphex(interface->vdev->device_type, ==, VIRTIO_ID_9P);
1951cf4323eSThomas Huth 
1961cf4323eSThomas Huth     obj->destructor = virtio_9p_pci_destructor;
1971cf4323eSThomas Huth     obj->start_hw = virtio_9p_pci_start_hw;
1981cf4323eSThomas Huth     obj->get_driver = virtio_9p_pci_get_driver;
1991cf4323eSThomas Huth 
2001cf4323eSThomas Huth     return obj;
2011cf4323eSThomas Huth }
2021cf4323eSThomas Huth 
2033a565c64SChristian Schoenebeck /**
2043a565c64SChristian Schoenebeck  * Performs regular expression based search and replace on @a haystack.
2053a565c64SChristian Schoenebeck  *
2063a565c64SChristian Schoenebeck  * @param haystack - input string to be parsed, result of replacement is
2073a565c64SChristian Schoenebeck  *                   stored back to @a haystack
2083a565c64SChristian Schoenebeck  * @param pattern - the regular expression pattern for scanning @a haystack
2093a565c64SChristian Schoenebeck  * @param replace_fmt - matches of supplied @a pattern are replaced by this,
2103a565c64SChristian Schoenebeck  *                      if necessary glib printf format can be used to add
2113a565c64SChristian Schoenebeck  *                      variable arguments of this function to this
2123a565c64SChristian Schoenebeck  *                      replacement string
2133a565c64SChristian Schoenebeck  */
214*0472b2e5SDaniel P. Berrangé G_GNUC_PRINTF(3, 4)
regex_replace(GString * haystack,const char * pattern,const char * replace_fmt,...)2153a565c64SChristian Schoenebeck static void regex_replace(GString *haystack, const char *pattern,
2163a565c64SChristian Schoenebeck                           const char *replace_fmt, ...)
2173a565c64SChristian Schoenebeck {
218494fbbd3SGreg Kurz     g_autoptr(GRegex) regex = NULL;
219494fbbd3SGreg Kurz     g_autofree char *replace = NULL, *s = NULL;
2203a565c64SChristian Schoenebeck     va_list argp;
2213a565c64SChristian Schoenebeck 
2223a565c64SChristian Schoenebeck     va_start(argp, replace_fmt);
2233a565c64SChristian Schoenebeck     replace = g_strdup_vprintf(replace_fmt, argp);
2243a565c64SChristian Schoenebeck     va_end(argp);
2253a565c64SChristian Schoenebeck 
2263a565c64SChristian Schoenebeck     regex = g_regex_new(pattern, 0, 0, NULL);
2273a565c64SChristian Schoenebeck     s = g_regex_replace(regex, haystack->str, -1, 0, replace, 0, NULL);
2283a565c64SChristian Schoenebeck     g_string_assign(haystack, s);
2293a565c64SChristian Schoenebeck }
2303a565c64SChristian Schoenebeck 
virtio_9p_assign_local_driver(GString * cmd_line,const char * args)2313a565c64SChristian Schoenebeck void virtio_9p_assign_local_driver(GString *cmd_line, const char *args)
2323a565c64SChristian Schoenebeck {
2333a565c64SChristian Schoenebeck     g_assert_nonnull(local_test_path);
2343a565c64SChristian Schoenebeck 
2353a565c64SChristian Schoenebeck     /* replace 'synth' driver by 'local' driver */
2363a565c64SChristian Schoenebeck     regex_replace(cmd_line, "-fsdev synth,", "-fsdev local,");
2373a565c64SChristian Schoenebeck 
2383a565c64SChristian Schoenebeck     /* append 'path=...' to '-fsdev ...' group */
2393a565c64SChristian Schoenebeck     regex_replace(cmd_line, "(-fsdev \\w[^ ]*)", "\\1,path='%s'",
2403a565c64SChristian Schoenebeck                   local_test_path);
2413a565c64SChristian Schoenebeck 
2423a565c64SChristian Schoenebeck     if (!args) {
2433a565c64SChristian Schoenebeck         return;
2443a565c64SChristian Schoenebeck     }
2453a565c64SChristian Schoenebeck 
2463a565c64SChristian Schoenebeck     /* append passed args to '-fsdev ...' group */
2473a565c64SChristian Schoenebeck     regex_replace(cmd_line, "(-fsdev \\w[^ ]*)", "\\1,%s", args);
2483a565c64SChristian Schoenebeck }
2493a565c64SChristian Schoenebeck 
virtio_9p_register_nodes(void)2501cf4323eSThomas Huth static void virtio_9p_register_nodes(void)
2511cf4323eSThomas Huth {
2521cf4323eSThomas Huth     const char *str_simple = "fsdev=fsdev0,mount_tag=" MOUNT_TAG;
2531cf4323eSThomas Huth     const char *str_addr = "fsdev=fsdev0,addr=04.0,mount_tag=" MOUNT_TAG;
2541cf4323eSThomas Huth 
2551cf4323eSThomas Huth     QPCIAddress addr = {
2561cf4323eSThomas Huth         .devfn = QPCI_DEVFN(4, 0),
2571cf4323eSThomas Huth     };
2581cf4323eSThomas Huth 
2591cf4323eSThomas Huth     QOSGraphEdgeOptions opts = {
2601cf4323eSThomas Huth         .before_cmd_line = "-fsdev synth,id=fsdev0",
2611cf4323eSThomas Huth     };
2621cf4323eSThomas Huth 
2631cf4323eSThomas Huth     /* virtio-9p-device */
2641cf4323eSThomas Huth     opts.extra_device_opts = str_simple,
2651cf4323eSThomas Huth     qos_node_create_driver("virtio-9p-device", virtio_9p_device_create);
2661cf4323eSThomas Huth     qos_node_consumes("virtio-9p-device", "virtio-bus", &opts);
2671cf4323eSThomas Huth     qos_node_produces("virtio-9p-device", "virtio");
2681cf4323eSThomas Huth     qos_node_produces("virtio-9p-device", "virtio-9p");
2691cf4323eSThomas Huth 
2701cf4323eSThomas Huth     /* virtio-9p-pci */
2711cf4323eSThomas Huth     opts.extra_device_opts = str_addr;
2721cf4323eSThomas Huth     add_qpci_address(&opts, &addr);
2731cf4323eSThomas Huth     qos_node_create_driver("virtio-9p-pci", virtio_9p_pci_create);
2741cf4323eSThomas Huth     qos_node_consumes("virtio-9p-pci", "pci-bus", &opts);
2751cf4323eSThomas Huth     qos_node_produces("virtio-9p-pci", "pci-device");
2761cf4323eSThomas Huth     qos_node_produces("virtio-9p-pci", "virtio");
2771cf4323eSThomas Huth     qos_node_produces("virtio-9p-pci", "virtio-9p");
2781cf4323eSThomas Huth 
2791cf4323eSThomas Huth }
2801cf4323eSThomas Huth 
2811cf4323eSThomas Huth libqos_init(virtio_9p_register_nodes);
282