Coverage Report

Created: 2026-10-31 08:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qemu/net/net-hmp-cmds.c
Line
Count
Source
1
/*
2
 * Human Monitor Interface commands
3
 *
4
 * Copyright IBM, Corp. 2011
5
 *
6
 * Authors:
7
 *  Anthony Liguori   <aliguori@us.ibm.com>
8
 *
9
 * This work is licensed under the terms of the GNU GPL, version 2.  See
10
 * the COPYING file in the top-level directory.
11
 *
12
 * Contributions after 2012-01-13 are licensed under the terms of the
13
 * GNU GPL, version 2 or (at your option) any later version.
14
 */
15
16
#include "qemu/osdep.h"
17
#include "migration/misc.h"
18
#include "monitor/hmp.h"
19
#include "monitor/hmp-completion.h"
20
#include "monitor/monitor.h"
21
#include "net/net.h"
22
#include "qapi/clone-visitor.h"
23
#include "qapi/qapi-commands-net.h"
24
#include "qapi/qapi-visit-net.h"
25
#include "qapi/error.h"
26
#include "qobject/qdict.h"
27
#include "qemu/config-file.h"
28
#include "qemu/help_option.h"
29
#include "qemu/option.h"
30
31
static void hmp_print_client_info(MonitorHMP *hmp, NetworkClientInfo *ci)
32
0
{
33
0
    NetFilterInfoList *f;
34
35
0
    monitor_hmp_printf(hmp, "%s: index=%" PRIu32 ",type=%s,%s\n",
36
0
                       ci->name, ci->queue_index,
37
0
                       NetClientDriver_str(ci->type), ci->info_str);
38
0
    if (ci->filters) {
39
0
        monitor_hmp_printf(hmp, "filters:\n");
40
0
        for (f = ci->filters; f; f = f->next) {
41
0
            monitor_hmp_printf(hmp, "  - %s: type=%s%s%s\n",
42
0
                               f->value->name, f->value->type,
43
0
                               f->value->info[0] ? "," : "", f->value->info);
44
0
        }
45
0
    }
46
0
}
47
48
void hmp_info_network(MonitorHMP *hmp, const QDict *qdict)
49
0
{
50
0
    Error *err = NULL;
51
0
    g_autoptr(NetworkInfo) info = qmp_x_query_network(&err);
52
0
    NetHubInfoList *h;
53
0
    NetworkClientInfoList *entry;
54
55
0
    if (hmp_handle_error(hmp, err)) {
56
0
        return;
57
0
    }
58
59
0
    for (h = info->hubs; h; h = h->next) {
60
0
        NetHubPortInfoList *p;
61
62
0
        monitor_hmp_printf(hmp, "hub %d\n", (int)h->value->id);
63
0
        for (p = h->value->ports; p; p = p->next) {
64
0
            if (p->value->peer) {
65
0
                monitor_hmp_printf(hmp, " \\ %s: ", p->value->name);
66
0
                hmp_print_client_info(hmp, p->value->peer);
67
0
            } else {
68
0
                monitor_hmp_printf(hmp, " \\ %s\n", p->value->name);
69
0
            }
70
0
        }
71
0
    }
72
73
0
    for (entry = info->clients; entry; entry = entry->next) {
74
0
        NetworkClientInfo *ci = entry->value;
75
76
0
        if (!ci->peer || ci->type == NET_CLIENT_DRIVER_NIC) {
77
0
            hmp_print_client_info(hmp, ci);
78
0
        } /* else it's a netdev connected to a NIC, printed with the NIC */
79
0
        if (ci->peer && ci->type == NET_CLIENT_DRIVER_NIC) {
80
0
            monitor_hmp_printf(hmp, " \\ ");
81
0
            hmp_print_client_info(hmp, ci->peer);
82
0
        }
83
0
    }
84
0
}
85
86
void hmp_set_link(MonitorHMP *hmp, const QDict *qdict)
87
0
{
88
0
    const char *name = qdict_get_str(qdict, "name");
89
0
    bool up = qdict_get_bool(qdict, "up");
90
0
    Error *err = NULL;
91
92
0
    qmp_set_link(name, up, &err);
93
0
    hmp_handle_error(hmp, err);
94
0
}
95
96
97
void hmp_announce_self(MonitorHMP *hmp, const QDict *qdict)
98
0
{
99
0
    const char *interfaces_str = qdict_get_try_str(qdict, "interfaces");
100
0
    const char *id = qdict_get_try_str(qdict, "id");
101
0
    AnnounceParameters *params = QAPI_CLONE(AnnounceParameters,
102
0
                                            migrate_announce_params());
103
104
0
    qapi_free_strList(params->interfaces);
105
0
    params->interfaces = hmp_split_at_comma(interfaces_str);
106
0
    params->has_interfaces = params->interfaces != NULL;
107
0
    params->id = g_strdup(id);
108
0
    qmp_announce_self(params, NULL);
109
0
    qapi_free_AnnounceParameters(params);
110
0
}
111
112
void hmp_netdev_add(MonitorHMP *hmp, const QDict *qdict)
113
0
{
114
0
    Error *err = NULL;
115
0
    QemuOpts *opts;
116
0
    const char *type = qdict_get_try_str(qdict, "type");
117
118
0
    if (type && is_help_option(type)) {
119
0
        show_netdevs();
120
0
        return;
121
0
    }
122
0
    opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict, &err);
123
0
    if (err) {
124
0
        goto out;
125
0
    }
126
127
0
    netdev_add(opts, &err);
128
0
    if (err) {
129
0
        qemu_opts_del(opts);
130
0
    }
131
132
0
out:
133
0
    hmp_handle_error(hmp, err);
134
0
}
135
136
void hmp_netdev_del(MonitorHMP *hmp, const QDict *qdict)
137
0
{
138
0
    const char *id = qdict_get_str(qdict, "id");
139
0
    Error *err = NULL;
140
141
0
    qmp_netdev_del(id, &err);
142
0
    hmp_handle_error(hmp, err);
143
0
}
144
145
146
void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
147
0
{
148
0
    size_t len;
149
0
    int i;
150
151
0
    if (nb_args != 2) {
152
0
        return;
153
0
    }
154
0
    len = strlen(str);
155
0
    readline_set_completion_index(rs, len);
156
0
    for (i = 0; i < NET_CLIENT_DRIVER__MAX; i++) {
157
0
        readline_add_completion_of(rs, str, NetClientDriver_str(i));
158
0
    }
159
0
}
160
161
void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
162
0
{
163
0
    size_t len;
164
165
0
    len = strlen(str);
166
0
    readline_set_completion_index(rs, len);
167
0
    if (nb_args == 2) {
168
0
        NetClientState *ncs[MAX_QUEUE_NUM];
169
0
        int count, i;
170
0
        count = qemu_find_net_clients_except(NULL, ncs,
171
0
                                             NET_CLIENT_DRIVER_NONE,
172
0
                                             MAX_QUEUE_NUM);
173
0
        for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
174
0
            readline_add_completion_of(rs, str, ncs[i]->name);
175
0
        }
176
0
    } else if (nb_args == 3) {
177
0
        readline_add_completion_of(rs, str, "on");
178
0
        readline_add_completion_of(rs, str, "off");
179
0
    }
180
0
}
181
182
void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
183
0
{
184
0
    int len, count, i;
185
0
    NetClientState *ncs[MAX_QUEUE_NUM];
186
187
0
    if (nb_args != 2) {
188
0
        return;
189
0
    }
190
191
0
    len = strlen(str);
192
0
    readline_set_completion_index(rs, len);
193
0
    count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_DRIVER_NIC,
194
0
                                         MAX_QUEUE_NUM);
195
0
    for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
196
0
        if (ncs[i]->is_netdev) {
197
0
            readline_add_completion_of(rs, str, ncs[i]->name);
198
0
        }
199
0
    }
200
0
}