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