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 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 <getopt.h>
21 #include "libqtest-single.h"
22 #include "qapi/qmp/qdict.h"
23 #include "qapi/qmp/qbool.h"
24 #include "qapi/qmp/qstring.h"
25 #include "qemu/module.h"
26 #include "qapi/qmp/qlist.h"
27 #include "libqos/malloc.h"
28 #include "libqos/qgraph.h"
29 #include "libqos/qgraph_internal.h"
30 #include "libqos/qos_external.h"
31 
32 static char *old_path;
33 
34 
35 
36 /**
37  * qos_set_machines_devices_available(): sets availability of qgraph
38  * machines and devices.
39  *
40  * This function firstly starts QEMU with "-machine none" option,
41  * and then executes the QMP protocol asking for the list of devices
42  * and machines available.
43  *
44  * for each of these items, it looks up the corresponding qgraph node,
45  * setting it as available. The list currently returns all devices that
46  * are either machines or QEDGE_CONSUMED_BY other nodes.
47  * Therefore, in order to mark all other nodes, it recursively sets
48  * all its QEDGE_CONTAINS and QEDGE_PRODUCES child as available too.
49  */
qos_set_machines_devices_available(void)50 static void qos_set_machines_devices_available(void)
51 {
52     QDict *response;
53     QDict *args = qdict_new();
54     QList *list;
55 
56     qtest_start("-machine none");
57     response = qmp("{ 'execute': 'query-machines' }");
58     list = qdict_get_qlist(response, "return");
59 
60     apply_to_qlist(list, true);
61 
62     qobject_unref(response);
63 
64     qdict_put_bool(args, "abstract", true);
65     qdict_put_str(args, "implements", "device");
66 
67     response = qmp("{'execute': 'qom-list-types',"
68                    " 'arguments': %p }", args);
69     g_assert(qdict_haskey(response, "return"));
70     list = qdict_get_qlist(response, "return");
71 
72     apply_to_qlist(list, false);
73 
74     qtest_end();
75     qobject_unref(response);
76 }
77 
78 
restart_qemu_or_continue(char * path)79 static void restart_qemu_or_continue(char *path)
80 {
81     /* compares the current command line with the
82      * one previously executed: if they are the same,
83      * don't restart QEMU, if they differ, stop previous
84      * QEMU subprocess (if active) and start over with
85      * the new command line
86      */
87     if (g_strcmp0(old_path, path)) {
88         qtest_end();
89         qos_invalidate_command_line();
90         old_path = g_strdup(path);
91         qtest_start(path);
92     } else { /* if cmd line is the same, reset the guest */
93         qobject_unref(qmp("{ 'execute': 'system_reset' }"));
94         qmp_eventwait("RESET");
95     }
96 }
97 
qos_invalidate_command_line(void)98 void qos_invalidate_command_line(void)
99 {
100     g_free(old_path);
101     old_path = NULL;
102 }
103 
104 
105 /* The argument to run_one_test, which is the test function that is registered
106  * with GTest, is a vector of strings.  The first item is the initial command
107  * line (before it is modified by the test's "before" function), the remaining
108  * items are node names forming the path to the test node.
109  */
110 static char **current_path;
111 
qos_get_current_command_line(void)112 const char *qos_get_current_command_line(void)
113 {
114     return current_path[0];
115 }
116 
qos_allocate_objects(QTestState * qts,QGuestAllocator ** p_alloc)117 void *qos_allocate_objects(QTestState *qts, QGuestAllocator **p_alloc)
118 {
119     return allocate_objects(qts, current_path + 1, p_alloc);
120 }
121 
122 /**
123  * run_one_test(): given an array of nodes @arg,
124  * walks the path invoking all constructors and
125  * passing the corresponding parameter in order to
126  * continue the objects allocation.
127  * Once the test is reached, its function is executed.
128  *
129  * Since the machine and QEDGE_CONSUMED_BY nodes allocate
130  * memory in the constructor, g_test_queue_destroy is used so
131  * that after execution they can be safely free'd.  The test's
132  * ->before callback is also welcome to use g_test_queue_destroy.
133  *
134  * Note: as specified in walk_path() too, @arg is an array of
135  * char *, where arg[0] is a pointer to the command line
136  * string that will be used to properly start QEMU when executing
137  * the test, and the remaining elements represent the actual objects
138  * that will be allocated.
139  *
140  * The order of execution is the following:
141  * 1) @before test function as defined in the given QOSGraphTestOptions
142  * 2) start QEMU
143  * 3) call all nodes constructor and get_driver/get_device depending on edge,
144  *    start the hardware (*_device_enable functions)
145  * 4) start test
146  */
run_one_test(const void * arg)147 static void run_one_test(const void *arg)
148 {
149     QOSGraphNode *test_node;
150     QGuestAllocator *alloc = NULL;
151     void *obj;
152     char **path = (char **) arg;
153     GString *cmd_line = g_string_new(path[0]);
154     void *test_arg;
155 
156     /* Before test */
157     current_path = path;
158     test_node = qos_graph_get_node(path[(g_strv_length(path) - 1)]);
159     test_arg = test_node->u.test.arg;
160     if (test_node->u.test.before) {
161         test_arg = test_node->u.test.before(cmd_line, test_arg);
162     }
163 
164     restart_qemu_or_continue(cmd_line->str);
165     g_string_free(cmd_line, true);
166 
167     obj = qos_allocate_objects(global_qtest, &alloc);
168     test_node->u.test.function(obj, test_arg, alloc);
169 }
170 
subprocess_run_one_test(const void * arg)171 static void subprocess_run_one_test(const void *arg)
172 {
173     const gchar *path = arg;
174     g_test_trap_subprocess(path, 0, 0);
175     g_test_trap_assert_passed();
176 }
177 
178 /*
179  * in this function, 2 path will be built:
180  * path_str, a one-string path (ex "pc/i440FX-pcihost/...")
181  * path_vec, a string-array path (ex [0] = "pc", [1] = "i440FX-pcihost").
182  *
183  * path_str will be only used to build the test name, and won't need the
184  * architecture name at beginning, since it will be added by qtest_add_func().
185  *
186  * path_vec is used to allocate all constructors of the path nodes.
187  * Each name in this array except position 0 must correspond to a valid
188  * QOSGraphNode name.
189  * Position 0 is special, initially contains just the <machine> name of
190  * the node, (ex for "x86_64/pc" it will be "pc"), used to build the test
191  * path (see below). After it will contain the command line used to start
192  * qemu with all required devices.
193  *
194  * Note that the machine node name must be with format <arch>/<machine>
195  * (ex "x86_64/pc"), because it will identify the node "x86_64/pc"
196  * and start QEMU with "-M pc". For this reason,
197  * when building path_str, path_vec
198  * initially contains the <machine> at position 0 ("pc"),
199  * and the node name at position 1 (<arch>/<machine>)
200  * ("x86_64/pc"), followed by the rest of the nodes.
201  */
walk_path(QOSGraphNode * orig_path,int len)202 static void walk_path(QOSGraphNode *orig_path, int len)
203 {
204     QOSGraphNode *path;
205     QOSGraphEdge *edge;
206 
207     /* etype set to QEDGE_CONSUMED_BY so that machine can add to the command line */
208     QOSEdgeType etype = QEDGE_CONSUMED_BY;
209 
210     /* twice QOS_PATH_MAX_ELEMENT_SIZE since each edge can have its arg */
211     char **path_vec = g_new0(char *, (QOS_PATH_MAX_ELEMENT_SIZE * 2));
212     int path_vec_size = 0;
213 
214     char *after_cmd, *before_cmd, *after_device;
215     GString *after_device_str = g_string_new("");
216     char *node_name = orig_path->name, *path_str;
217 
218     GString *cmd_line = g_string_new("");
219     GString *cmd_line2 = g_string_new("");
220 
221     path = qos_graph_get_node(node_name); /* root */
222     node_name = qos_graph_edge_get_dest(path->path_edge); /* machine name */
223 
224     path_vec[path_vec_size++] = node_name;
225     path_vec[path_vec_size++] = qos_get_machine_type(node_name);
226 
227     for (;;) {
228         path = qos_graph_get_node(node_name);
229         if (!path->path_edge) {
230             break;
231         }
232 
233         node_name = qos_graph_edge_get_dest(path->path_edge);
234 
235         /* append node command line + previous edge command line */
236         if (path->command_line && etype == QEDGE_CONSUMED_BY) {
237             g_string_append(cmd_line, path->command_line);
238             g_string_append(cmd_line, after_device_str->str);
239             g_string_truncate(after_device_str, 0);
240         }
241 
242         path_vec[path_vec_size++] = qos_graph_edge_get_name(path->path_edge);
243         /* detect if edge has command line args */
244         after_cmd = qos_graph_edge_get_after_cmd_line(path->path_edge);
245         after_device = qos_graph_edge_get_extra_device_opts(path->path_edge);
246         before_cmd = qos_graph_edge_get_before_cmd_line(path->path_edge);
247         edge = qos_graph_get_edge(path->name, node_name);
248         etype = qos_graph_edge_get_type(edge);
249 
250         if (before_cmd) {
251             g_string_append(cmd_line, before_cmd);
252         }
253         if (after_cmd) {
254             g_string_append(cmd_line2, after_cmd);
255         }
256         if (after_device) {
257             g_string_append(after_device_str, after_device);
258         }
259     }
260 
261     path_vec[path_vec_size++] = NULL;
262     g_string_append(cmd_line, after_device_str->str);
263     g_string_free(after_device_str, true);
264 
265     g_string_append(cmd_line, cmd_line2->str);
266     g_string_free(cmd_line2, true);
267 
268     /* here position 0 has <arch>/<machine>, position 1 has <machine>.
269      * The path must not have the <arch>, qtest_add_data_func adds it.
270      */
271     path_str = g_strjoinv("/", path_vec + 1);
272 
273     /* put arch/machine in position 1 so run_one_test can do its work
274      * and add the command line at position 0.
275      */
276     path_vec[1] = path_vec[0];
277     path_vec[0] = g_string_free(cmd_line, false);
278 
279     if (path->u.test.subprocess) {
280         gchar *subprocess_path = g_strdup_printf("/%s/%s/subprocess",
281                                                  qtest_get_arch(), path_str);
282         qtest_add_data_func(path_str, subprocess_path, subprocess_run_one_test);
283         g_test_add_data_func(subprocess_path, path_vec, run_one_test);
284     } else {
285         qtest_add_data_func(path_str, path_vec, run_one_test);
286     }
287 
288     g_free(path_str);
289 }
290 
291 
292 
293 /**
294  * main(): heart of the qgraph framework.
295  *
296  * - Initializes the glib test framework
297  * - Creates the graph by invoking the various _init constructors
298  * - Starts QEMU to mark the available devices
299  * - Walks the graph, and each path is added to
300  *   the glib test framework (walk_path)
301  * - Runs the tests, calling allocate_object() and allocating the
302  *   machine/drivers/test objects
303  * - Cleans up everything
304  */
main(int argc,char ** argv)305 int main(int argc, char **argv)
306 {
307     g_test_init(&argc, &argv, NULL);
308     qos_graph_init();
309     module_call_init(MODULE_INIT_QOM);
310     module_call_init(MODULE_INIT_LIBQOS);
311     qos_set_machines_devices_available();
312 
313     qos_graph_foreach_test_path(walk_path);
314     g_test_run();
315     qtest_end();
316     qos_graph_destroy();
317     g_free(old_path);
318     return 0;
319 }
320