Coverage Report

Created: 2026-10-31 08:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qemu/tests/qtest/libqos/qgraph.c
Line
Count
Source
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/queue.h"
22
#include "qgraph_internal.h"
23
#include "qgraph.h"
24
25
#define QGRAPH_PRINT_DEBUG 0
26
915
#define QOS_ROOT ""
27
typedef struct QOSStackElement QOSStackElement;
28
29
/* Graph Edge.*/
30
struct QOSGraphEdge {
31
    QOSEdgeType type;
32
    char *dest;
33
    void *arg;                /* just for QEDGE_CONTAINS
34
                               * and QEDGE_CONSUMED_BY */
35
    char *extra_device_opts;  /* added to -device option, "," is
36
                               * automatically added
37
                               */
38
    char *before_cmd_line;    /* added before node cmd_line */
39
    char *after_cmd_line;     /* added after -device options */
40
    char *edge_name;          /* used by QEDGE_CONTAINS */
41
    QSLIST_ENTRY(QOSGraphEdge) edge_list;
42
};
43
44
typedef QSLIST_HEAD(, QOSGraphEdge) QOSGraphEdgeList;
45
46
/**
47
 * Stack used to keep track of the discovered path when using
48
 * the DFS algorithm
49
 */
50
struct QOSStackElement {
51
    QOSGraphNode *node;
52
    QOSStackElement *parent;
53
    QOSGraphEdge *parent_edge;
54
    int length;
55
};
56
57
/* Each entry in these hash table will consist of <string, node/edge> pair. */
58
static GHashTable *edge_table;
59
static GHashTable *node_table;
60
61
/* stack used by the DFS algorithm to store the path from machine to test */
62
static QOSStackElement qos_node_stack[QOS_PATH_MAX_ELEMENT_SIZE];
63
static int qos_node_tos;
64
65
/**
66
 * add_edge(): creates an edge of type @type
67
 * from @source to @dest node, and inserts it in the
68
 * edges hash table
69
 *
70
 * Nodes @source and @dest do not necessarily need to exist.
71
 * Possibility to add also options (see #QOSGraphEdgeOptions)
72
 * edge->edge_name is used as identifier for get_device relationships,
73
 * so by default is equal to @dest.
74
 */
75
static void add_edge(const char *source, const char *dest,
76
                     QOSEdgeType type, QOSGraphEdgeOptions *opts)
77
6.58k
{
78
6.58k
    char *key;
79
6.58k
    QOSGraphEdgeList *list = g_hash_table_lookup(edge_table, source);
80
6.58k
    QOSGraphEdgeOptions def_opts = { };
81
82
6.58k
    if (!list) {
83
3.23k
        list = g_new0(QOSGraphEdgeList, 1);
84
3.23k
        key = g_strdup(source);
85
3.23k
        g_hash_table_insert(edge_table, key, list);
86
3.23k
    }
87
88
6.58k
    if (!opts) {
89
4.88k
        opts = &def_opts;
90
4.88k
    }
91
92
6.58k
    QOSGraphEdge *edge = g_new0(QOSGraphEdge, 1);
93
6.58k
    edge->type = type;
94
6.58k
    edge->dest = g_strdup(dest);
95
6.58k
    edge->edge_name = g_strdup(opts->edge_name ?: dest);
96
6.58k
    edge->arg = g_memdup2(opts->arg, opts->size_arg);
97
98
6.58k
    edge->before_cmd_line =
99
6.58k
        opts->before_cmd_line ? g_strconcat(" ", opts->before_cmd_line, NULL) : NULL;
100
6.58k
    edge->extra_device_opts =
101
6.58k
        opts->extra_device_opts ? g_strconcat(",", opts->extra_device_opts, NULL) : NULL;
102
6.58k
    edge->after_cmd_line =
103
6.58k
        opts->after_cmd_line ? g_strconcat(" ", opts->after_cmd_line, NULL) : NULL;
104
105
6.58k
    QSLIST_INSERT_HEAD(list, edge, edge_list);
106
6.58k
}
107
108
/* destroy_edges(): frees all edges inside a given @list */
109
static void destroy_edges(void *list)
110
0
{
111
0
    QOSGraphEdge *temp;
112
0
    QOSGraphEdgeList *elist = list;
113
114
0
    while (!QSLIST_EMPTY(elist)) {
115
0
        temp = QSLIST_FIRST(elist);
116
0
        QSLIST_REMOVE_HEAD(elist, edge_list);
117
0
        g_free(temp->dest);
118
0
        g_free(temp->before_cmd_line);
119
0
        g_free(temp->after_cmd_line);
120
0
        g_free(temp->extra_device_opts);
121
0
        g_free(temp->edge_name);
122
0
        g_free(temp->arg);
123
0
        g_free(temp);
124
0
    }
125
0
    g_free(elist);
126
0
}
127
128
/**
129
 * create_node(): creates a node @name of type @type
130
 * and inserts it to the nodes hash table.
131
 * By default, node is not available.
132
 */
133
static QOSGraphNode *create_node(const char *name, QOSNodeType type)
134
4.39k
{
135
4.39k
    if (g_hash_table_lookup(node_table, name)) {
136
0
        g_printerr("Node %s already created\n", name);
137
0
        abort();
138
0
    }
139
140
4.39k
    QOSGraphNode *node = g_new0(QOSGraphNode, 1);
141
4.39k
    node->type = type;
142
4.39k
    node->available = false;
143
4.39k
    node->name = g_strdup(name);
144
4.39k
    g_hash_table_insert(node_table, node->name, node);
145
4.39k
    return node;
146
4.39k
}
147
148
/**
149
 * destroy_node(): frees a node @val from the nodes hash table.
150
 * Note that node->name is not free'd since it will represent the
151
 * hash table key
152
 */
153
static void destroy_node(void *val)
154
0
{
155
0
    QOSGraphNode *node = val;
156
0
    g_free(node->qemu_name);
157
0
    g_free(node->command_line);
158
0
    g_free(node);
159
0
}
160
161
/**
162
 * destroy_string(): frees @key from the nodes hash table.
163
 * Actually frees the node->name
164
 */
165
static void destroy_string(void *key)
166
0
{
167
0
    g_free(key);
168
0
}
169
170
/**
171
 * search_node(): search for a node @key in the nodes hash table
172
 * Returns the QOSGraphNode if found, #NULL otherwise
173
 */
174
static QOSGraphNode *search_node(const char *key)
175
4.33k
{
176
4.33k
    return g_hash_table_lookup(node_table, key);
177
4.33k
}
178
179
/**
180
 * get_edgelist(): returns the edge list (value) assigned to
181
 * the @key in the edge hash table.
182
 * This list will contain all edges with source equal to @key
183
 *
184
 * Returns: on success: the %QOSGraphEdgeList
185
 *          otherwise: abort()
186
 */
187
static QOSGraphEdgeList *get_edgelist(const char *key)
188
0
{
189
0
    return g_hash_table_lookup(edge_table, key);
190
0
}
191
192
/**
193
 * search_list_edges(): search for an edge with destination @dest
194
 * in the given @edgelist.
195
 *
196
 * Returns: on success: the %QOSGraphEdge
197
 *          otherwise: #NULL
198
 */
199
static QOSGraphEdge *search_list_edges(QOSGraphEdgeList *edgelist,
200
                                       const char *dest)
201
0
{
202
0
    QOSGraphEdge *tmp, *next;
203
0
    if (!edgelist) {
204
0
        return NULL;
205
0
    }
206
0
    QSLIST_FOREACH_SAFE(tmp, edgelist, edge_list, next) {
207
0
        if (g_strcmp0(tmp->dest, dest) == 0) {
208
0
            break;
209
0
        }
210
0
    }
211
0
    return tmp;
212
0
}
213
214
/**
215
 * search_machine(): search for a machine @name in the node hash
216
 * table. A machine is the child of the root node.
217
 * This function forces the research in the children of the root,
218
 * to check the node is a proper machine
219
 *
220
 * Returns: on success: the %QOSGraphNode
221
 *          otherwise: #NULL
222
 */
223
static QOSGraphNode *search_machine(const char *name)
224
0
{
225
0
    QOSGraphNode *n;
226
0
    QOSGraphEdgeList *root_list = get_edgelist(QOS_ROOT);
227
0
    QOSGraphEdge *e = search_list_edges(root_list, name);
228
0
    if (!e) {
229
0
        return NULL;
230
0
    }
231
0
    n = search_node(e->dest);
232
0
    if (n->type == QNODE_MACHINE) {
233
0
        return n;
234
0
    }
235
0
    return NULL;
236
0
}
237
238
/**
239
 * create_interface(): checks if there is already
240
 * a node @node in the node hash table, if not
241
 * creates a node @node of type #QNODE_INTERFACE
242
 * and inserts it. If there is one, check it's
243
 * a #QNODE_INTERFACE and abort() if it's not.
244
 */
245
static void create_interface(const char *node)
246
4.33k
{
247
4.33k
    QOSGraphNode *interface;
248
4.33k
    interface = search_node(node);
249
4.33k
    if (!interface) {
250
1.09k
        create_node(node, QNODE_INTERFACE);
251
3.23k
    } else if (interface->type != QNODE_INTERFACE) {
252
0
        fprintf(stderr, "Error: Node %s is not an interface\n", node);
253
0
        abort();
254
0
    }
255
4.33k
}
256
257
/**
258
 * build_machine_cmd_line(): builds the command line for the machine
259
 * @node. The node name must be a valid qemu identifier, since it
260
 * will be used to build the command line.
261
 *
262
 * It is also possible to pass an optional @args that will be
263
 * concatenated to the command line.
264
 *
265
 * For machines, prepend -M to the machine name. ", @rgs" is added
266
 * after the -M <machine> command.
267
 */
268
static void build_machine_cmd_line(QOSGraphNode *node, const char *args)
269
854
{
270
854
    char *machine = qos_get_machine_type(node->name);
271
854
    if (args) {
272
244
        node->command_line = g_strconcat("-M ", machine, ",", args, NULL);
273
610
    } else {
274
610
        node->command_line = g_strconcat("-M ", machine, " ", NULL);
275
610
    }
276
854
}
277
278
/**
279
 * build_driver_cmd_line(): builds the command line for the driver
280
 * @node. The node name must be a valid qemu identifier, since it
281
 * will be used to build the command line.
282
 *
283
 * Driver do not need additional command line, since it will be
284
 * provided by the edge options.
285
 *
286
 * For drivers, prepend -device to the node name.
287
 */
288
static void build_driver_cmd_line(QOSGraphNode *node)
289
2.13k
{
290
2.13k
    const char *name = node->qemu_name ?: node->name;
291
2.13k
    node->command_line = g_strconcat(" -device ", name, NULL);
292
2.13k
}
293
294
/* qos_print_cb(): callback prints all path found by the DFS algorithm. */
295
static void qos_print_cb(QOSGraphNode *path, int length)
296
0
{
297
    #if QGRAPH_PRINT_DEBUG
298
        printf("%d elements\n", length);
299
300
        if (!path) {
301
            return;
302
        }
303
304
        while (path->path_edge) {
305
            printf("%s ", path->name);
306
            switch (path->path_edge->type) {
307
            case QEDGE_PRODUCES:
308
                printf("--PRODUCES--> ");
309
                break;
310
            case QEDGE_CONSUMED_BY:
311
                printf("--CONSUMED_BY--> ");
312
                break;
313
            case QEDGE_CONTAINS:
314
                printf("--CONTAINS--> ");
315
                break;
316
            }
317
            path = search_node(path->path_edge->dest);
318
        }
319
320
        printf("%s\n\n", path->name);
321
    #endif
322
0
}
323
324
/* qos_push(): push a node @el and edge @e in the qos_node_stack */
325
static void qos_push(QOSGraphNode *el, QOSStackElement *parent,
326
                     QOSGraphEdge *e)
327
0
{
328
0
    int len = 0; /* root is not counted */
329
0
    if (qos_node_tos == QOS_PATH_MAX_ELEMENT_SIZE) {
330
0
        g_printerr("QOSStack: full stack, cannot push");
331
0
        abort();
332
0
    }
333
334
0
    if (parent) {
335
0
        len = parent->length + 1;
336
0
    }
337
0
    qos_node_stack[qos_node_tos++] = (QOSStackElement) {
338
0
        .node = el,
339
0
        .parent = parent,
340
0
        .parent_edge = e,
341
0
        .length = len,
342
0
    };
343
0
}
344
345
/* qos_tos(): returns the top of stack, without popping */
346
static QOSStackElement *qos_tos(void)
347
0
{
348
0
    return &qos_node_stack[qos_node_tos - 1];
349
0
}
350
351
/* qos_pop(): pops an element from the tos, setting it unvisited*/
352
static QOSStackElement *qos_pop(void)
353
0
{
354
0
    if (qos_node_tos == 0) {
355
0
        g_printerr("QOSStack: empty stack, cannot pop");
356
0
        abort();
357
0
    }
358
0
    QOSStackElement *e = qos_tos();
359
0
    e->node->visited = false;
360
0
    qos_node_tos--;
361
0
    return e;
362
0
}
363
364
/**
365
 * qos_reverse_path(): reverses the found path, going from
366
 * test-to-machine to machine-to-test
367
 */
368
static QOSGraphNode *qos_reverse_path(QOSStackElement *el)
369
0
{
370
0
    if (!el) {
371
0
        return NULL;
372
0
    }
373
374
0
    el->node->path_edge = NULL;
375
376
0
    while (el->parent) {
377
0
        el->parent->node->path_edge = el->parent_edge;
378
0
        el = el->parent;
379
0
    }
380
381
0
    return el->node;
382
0
}
383
384
/**
385
 * qos_traverse_graph(): graph-walking algorithm, using Depth First Search it
386
 * starts from the root @machine and walks all possible path until it
387
 * reaches a test node.
388
 * At that point, it reverses the path found and invokes the @callback.
389
 *
390
 * Being Depth First Search, time complexity is O(|V| + |E|), while
391
 * space is O(|V|). In this case, the maximum stack size is set by
392
 * QOS_PATH_MAX_ELEMENT_SIZE.
393
 */
394
static void qos_traverse_graph(QOSGraphNode *root, QOSTestCallback callback)
395
0
{
396
0
    QOSGraphNode *v, *dest_node, *path;
397
0
    QOSStackElement *s_el;
398
0
    QOSGraphEdge *e, *next;
399
0
    QOSGraphEdgeList *list;
400
401
0
    qos_push(root, NULL, NULL);
402
403
0
    while (qos_node_tos > 0) {
404
0
        s_el = qos_tos();
405
0
        v = s_el->node;
406
0
        if (v->visited) {
407
0
            qos_pop();
408
0
            continue;
409
0
        }
410
0
        v->visited = true;
411
0
        list = get_edgelist(v->name);
412
0
        if (!list) {
413
0
            qos_pop();
414
0
            if (v->type == QNODE_TEST) {
415
0
                v->visited = false;
416
0
                path = qos_reverse_path(s_el);
417
0
                callback(path, s_el->length);
418
0
            }
419
0
        } else {
420
0
            QSLIST_FOREACH_SAFE(e, list, edge_list, next) {
421
0
                dest_node = search_node(e->dest);
422
423
0
                if (!dest_node) {
424
0
                    fprintf(stderr, "node %s in %s -> %s does not exist\n",
425
0
                            e->dest, v->name, e->dest);
426
0
                    abort();
427
0
                }
428
429
0
                if (!dest_node->visited && dest_node->available) {
430
0
                    qos_push(dest_node, s_el, e);
431
0
                }
432
0
            }
433
0
        }
434
0
    }
435
0
}
436
437
/* QGRAPH API*/
438
439
QOSGraphNode *qos_graph_get_node(const char *key)
440
0
{
441
0
    return search_node(key);
442
0
}
443
444
bool qos_graph_has_node(const char *node)
445
0
{
446
0
    QOSGraphNode *n = search_node(node);
447
0
    return n != NULL;
448
0
}
449
450
QOSNodeType qos_graph_get_node_type(const char *node)
451
0
{
452
0
    QOSGraphNode *n = search_node(node);
453
0
    if (n) {
454
0
        return n->type;
455
0
    }
456
0
    return -1;
457
0
}
458
459
bool qos_graph_get_node_availability(const char *node)
460
0
{
461
0
    QOSGraphNode *n = search_node(node);
462
0
    if (n) {
463
0
        return n->available;
464
0
    }
465
0
    return false;
466
0
}
467
468
QOSGraphEdge *qos_graph_get_edge(const char *node, const char *dest)
469
0
{
470
0
    QOSGraphEdgeList *list = get_edgelist(node);
471
0
    return search_list_edges(list, dest);
472
0
}
473
474
QOSEdgeType qos_graph_edge_get_type(QOSGraphEdge *edge)
475
0
{
476
0
    if (!edge) {
477
0
        return -1;
478
0
    }
479
0
    return edge->type;
480
0
}
481
482
char *qos_graph_edge_get_dest(QOSGraphEdge *edge)
483
0
{
484
0
    if (!edge) {
485
0
        return NULL;
486
0
    }
487
0
    return edge->dest;
488
0
}
489
490
void *qos_graph_edge_get_arg(QOSGraphEdge *edge)
491
0
{
492
0
    if (!edge) {
493
0
        return NULL;
494
0
    }
495
0
    return edge->arg;
496
0
}
497
498
char *qos_graph_edge_get_after_cmd_line(QOSGraphEdge *edge)
499
0
{
500
0
    if (!edge) {
501
0
        return NULL;
502
0
    }
503
0
    return edge->after_cmd_line;
504
0
}
505
506
char *qos_graph_edge_get_before_cmd_line(QOSGraphEdge *edge)
507
0
{
508
0
    if (!edge) {
509
0
        return NULL;
510
0
    }
511
0
    return edge->before_cmd_line;
512
0
}
513
514
char *qos_graph_edge_get_extra_device_opts(QOSGraphEdge *edge)
515
0
{
516
0
    if (!edge) {
517
0
        return NULL;
518
0
    }
519
0
    return edge->extra_device_opts;
520
0
}
521
522
char *qos_graph_edge_get_name(QOSGraphEdge *edge)
523
0
{
524
0
    if (!edge) {
525
0
        return NULL;
526
0
    }
527
0
    return edge->edge_name;
528
0
}
529
530
bool qos_graph_has_edge(const char *start, const char *dest)
531
0
{
532
0
    QOSGraphEdgeList *list = get_edgelist(start);
533
0
    QOSGraphEdge *e = search_list_edges(list, dest);
534
0
    return e != NULL;
535
0
}
536
537
QOSGraphNode *qos_graph_get_machine(const char *node)
538
0
{
539
0
    return search_machine(node);
540
0
}
541
542
bool qos_graph_has_machine(const char *node)
543
0
{
544
0
    QOSGraphNode *m = search_machine(node);
545
0
    return m != NULL;
546
0
}
547
548
void qos_print_graph(void)
549
0
{
550
0
    qos_graph_foreach_test_path(qos_print_cb);
551
0
}
552
553
void qos_graph_init(void)
554
61
{
555
61
    if (!node_table) {
556
61
        node_table = g_hash_table_new_full(g_str_hash, g_str_equal,
557
61
                                           destroy_string, destroy_node);
558
61
        create_node(QOS_ROOT, QNODE_DRIVER);
559
61
    }
560
561
61
    if (!edge_table) {
562
61
        edge_table = g_hash_table_new_full(g_str_hash, g_str_equal,
563
61
                                           destroy_string, destroy_edges);
564
61
    }
565
61
}
566
567
void qos_graph_destroy(void)
568
0
{
569
0
    if (node_table) {
570
0
        g_hash_table_destroy(node_table);
571
0
    }
572
573
0
    if (edge_table) {
574
0
        g_hash_table_destroy(edge_table);
575
0
    }
576
577
0
    node_table = NULL;
578
0
    edge_table = NULL;
579
0
}
580
581
void qos_node_destroy(void *key)
582
0
{
583
0
    g_hash_table_remove(node_table, key);
584
0
}
585
586
void qos_edge_destroy(void *key)
587
0
{
588
0
    g_hash_table_remove(edge_table, key);
589
0
}
590
591
void qos_add_test(const char *name, const char *interface,
592
                  QOSTestFunc test_func, QOSGraphTestOptions *opts)
593
244
{
594
244
    QOSGraphNode *node;
595
244
    char *test_name = g_strdup_printf("%s-tests/%s", interface, name);
596
244
    QOSGraphTestOptions def_opts = { };
597
598
244
    if (!opts) {
599
0
        opts = &def_opts;
600
0
    }
601
244
    node = create_node(test_name, QNODE_TEST);
602
244
    node->u.test.function = test_func;
603
244
    node->u.test.arg = opts->arg;
604
244
    assert(!opts->edge.arg);
605
244
    assert(!opts->edge.size_arg);
606
607
244
    node->u.test.before = opts->before;
608
244
    node->u.test.subprocess = opts->subprocess;
609
244
    node->available = true;
610
244
    add_edge(interface, test_name, QEDGE_CONSUMED_BY, &opts->edge);
611
244
    g_free(test_name);
612
244
}
613
614
void qos_node_create_machine(const char *name, QOSCreateMachineFunc function)
615
610
{
616
610
    qos_node_create_machine_args(name, function, NULL);
617
610
}
618
619
void qos_node_create_machine_args(const char *name,
620
                                  QOSCreateMachineFunc function,
621
                                  const char *opts)
622
854
{
623
854
    QOSGraphNode *node = create_node(name, QNODE_MACHINE);
624
854
    build_machine_cmd_line(node, opts);
625
854
    node->u.machine.constructor = function;
626
854
    add_edge(QOS_ROOT, name, QEDGE_CONTAINS, NULL);
627
854
}
628
629
void qos_node_create_driver(const char *name, QOSCreateDriverFunc function)
630
2.13k
{
631
2.13k
    QOSGraphNode *node = create_node(name, QNODE_DRIVER);
632
2.13k
    build_driver_cmd_line(node);
633
2.13k
    node->u.driver.constructor = function;
634
2.13k
}
635
636
void qos_node_create_driver_named(const char *name, const char *qemu_name,
637
                                  QOSCreateDriverFunc function)
638
0
{
639
0
    QOSGraphNode *node = create_node(name, QNODE_DRIVER);
640
0
    node->qemu_name = g_strdup(qemu_name);
641
0
    build_driver_cmd_line(node);
642
0
    node->u.driver.constructor = function;
643
0
}
644
645
void qos_node_contains(const char *container, const char *contained,
646
                       QOSGraphEdgeOptions *opts, ...)
647
1.15k
{
648
1.15k
    va_list va;
649
650
1.15k
    if (opts == NULL) {
651
1.09k
        add_edge(container, contained, QEDGE_CONTAINS, NULL);
652
1.09k
        return;
653
1.09k
    }
654
655
1.15k
    va_start(va, opts);
656
61
    do {
657
61
        add_edge(container, contained, QEDGE_CONTAINS, opts);
658
61
        opts = va_arg(va, QOSGraphEdgeOptions *);
659
61
    } while (opts != NULL);
660
661
61
    va_end(va);
662
61
}
663
664
void qos_node_produces(const char *producer, const char *interface)
665
2.80k
{
666
2.80k
    create_interface(interface);
667
2.80k
    add_edge(producer, interface, QEDGE_PRODUCES, NULL);
668
2.80k
}
669
670
void qos_node_consumes(const char *consumer, const char *interface,
671
                       QOSGraphEdgeOptions *opts)
672
1.52k
{
673
1.52k
    create_interface(interface);
674
1.52k
    add_edge(interface, consumer, QEDGE_CONSUMED_BY, opts);
675
1.52k
}
676
677
static void qos_graph_node_set_availability_explicit(const char *node, bool av)
678
0
{
679
0
    QOSGraphEdgeList *elist;
680
0
    QOSGraphNode *n = search_node(node);
681
0
    QOSGraphEdge *e, *next;
682
0
    if (!n) {
683
0
        return;
684
0
    }
685
0
    n->available = av;
686
0
    elist = get_edgelist(node);
687
0
    if (!elist) {
688
0
        return;
689
0
    }
690
0
    QSLIST_FOREACH_SAFE(e, elist, edge_list, next) {
691
0
        if (e->type == QEDGE_CONTAINS || e->type == QEDGE_PRODUCES) {
692
0
            qos_graph_node_set_availability_explicit(e->dest, av);
693
0
        }
694
0
    }
695
0
}
696
697
/*
698
 * Behaves as qos_graph_node_set_availability_explicit(), except that the
699
 * former always matches by node name only, whereas this function matches both
700
 * by node name and node's optional 'qemu_name' field.
701
 */
702
void qos_graph_node_set_availability(const char *node, bool av)
703
0
{
704
0
    GList *l;
705
0
    QOSGraphEdgeList *elist;
706
0
    QOSGraphEdge *e, *next;
707
0
    QOSGraphNode *n;
708
0
    GList *keys = g_hash_table_get_keys(node_table);
709
710
0
    for (l = keys; l != NULL; l = l->next) {
711
0
        const gchar *key = l->data;
712
0
        n = g_hash_table_lookup(node_table, key);
713
        /*
714
         * node's 'qemu_name' is set if there is more than one device with
715
         * the same QEMU (QMP) device name
716
         */
717
0
        const char *node_name = n->qemu_name ?: n->name;
718
0
        if (g_strcmp0(node_name, node) == 0) {
719
0
            n->available = av;
720
0
            elist = get_edgelist(n->name);
721
0
            if (elist) {
722
0
                QSLIST_FOREACH_SAFE(e, elist, edge_list, next) {
723
0
                    if (e->type == QEDGE_CONTAINS || e->type == QEDGE_PRODUCES)
724
0
                    {
725
0
                        qos_graph_node_set_availability_explicit(e->dest, av);
726
0
                    }
727
0
                }
728
0
            }
729
0
        }
730
0
    }
731
0
    g_list_free(keys);
732
0
}
733
734
void qos_graph_foreach_test_path(QOSTestCallback fn)
735
0
{
736
0
    QOSGraphNode *root = qos_graph_get_node(QOS_ROOT);
737
0
    qos_traverse_graph(root, fn);
738
0
}
739
740
QOSGraphObject *qos_machine_new(QOSGraphNode *node, QTestState *qts)
741
0
{
742
0
    QOSGraphObject *obj;
743
744
0
    g_assert(node->type == QNODE_MACHINE);
745
0
    obj = node->u.machine.constructor(qts);
746
0
    obj->free = g_free;
747
0
    return obj;
748
0
}
749
750
QOSGraphObject *qos_driver_new(QOSGraphNode *node, QOSGraphObject *parent,
751
                               QGuestAllocator *alloc, void *arg)
752
0
{
753
0
    QOSGraphObject *obj;
754
755
0
    g_assert(node->type == QNODE_DRIVER);
756
0
    obj = node->u.driver.constructor(parent, alloc, arg);
757
0
    obj->free = g_free;
758
0
    return obj;
759
0
}
760
761
void qos_object_destroy(QOSGraphObject *obj)
762
0
{
763
0
    if (!obj) {
764
0
        return;
765
0
    }
766
0
    if (obj->destructor) {
767
0
        obj->destructor(obj);
768
0
    }
769
0
    if (obj->free) {
770
0
        obj->free(obj);
771
0
    }
772
0
}
773
774
void qos_object_queue_destroy(QOSGraphObject *obj)
775
0
{
776
0
    g_test_queue_destroy((GDestroyNotify) qos_object_destroy, obj);
777
0
}
778
779
void qos_object_start_hw(QOSGraphObject *obj)
780
0
{
781
0
    if (obj->start_hw) {
782
0
        obj->start_hw(obj);
783
0
    }
784
0
}
785
786
char *qos_get_machine_type(char *name)
787
854
{
788
5.24k
    while (*name != '\0' && *name != '/') {
789
4.39k
        name++;
790
4.39k
    }
791
792
854
    if (!*name || !name[1]) {
793
0
        fprintf(stderr, "Machine name has to be of the form <arch>/<machine>\n");
794
0
        abort();
795
0
    }
796
797
854
    return name + 1;
798
854
}
799
800
void qos_delete_cmd_line(const char *name)
801
0
{
802
0
    QOSGraphNode *node = search_node(name);
803
0
    if (node) {
804
0
        g_free(node->command_line);
805
0
        node->command_line = NULL;
806
0
    }
807
0
}
808
809
void qos_dump_graph(void)
810
0
{
811
0
    GList *keys;
812
0
    GList *l;
813
0
    QOSGraphEdgeList *list;
814
0
    QOSGraphEdge *e, *next;
815
0
    QOSGraphNode *dest_node, *node;
816
817
0
    qos_printf("ALL QGRAPH EDGES: {\n");
818
0
    keys = g_hash_table_get_keys(edge_table);
819
0
    for (l = keys; l != NULL; l = l->next) {
820
0
        const gchar *key = l->data;
821
0
        qos_printf("\t src='%s'\n", key);
822
0
        list = get_edgelist(key);
823
0
        QSLIST_FOREACH_SAFE(e, list, edge_list, next) {
824
0
            dest_node = g_hash_table_lookup(node_table, e->dest);
825
0
            qos_printf("\t\t|-> dest='%s' type=%d (node=%p)",
826
0
                       e->dest, e->type, dest_node);
827
0
            if (!dest_node) {
828
0
                qos_printf_literal(" <------- ERROR !");
829
0
            }
830
0
            qos_printf_literal("\n");
831
0
        }
832
0
    }
833
0
    g_list_free(keys);
834
0
    qos_printf("}\n");
835
836
0
    qos_printf("ALL QGRAPH NODES: {\n");
837
0
    keys = g_hash_table_get_keys(node_table);
838
0
    for (l = keys; l != NULL; l = l->next) {
839
0
        const gchar *key = l->data;
840
0
        node = g_hash_table_lookup(node_table, key);
841
0
        qos_printf("\t name='%s' ", key);
842
0
        if (node->qemu_name) {
843
0
            qos_printf_literal("qemu_name='%s' ", node->qemu_name);
844
0
        }
845
0
        qos_printf_literal("type=%d cmd_line='%s' [%s]\n",
846
0
                           node->type, node->command_line,
847
0
                           node->available ? "available" : "UNAVAILABLE"
848
0
        );
849
0
    }
850
0
    g_list_free(keys);
851
0
    qos_printf("}\n");
852
0
}