2
0

qos-test.c 14 KB

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