xref: /openbmc/qemu/net/net-hmp-cmds.c (revision 2030ca36)
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 "net/net.h"
20 #include "qapi/clone-visitor.h"
21 #include "qapi/qapi-commands-net.h"
22 #include "qapi/qapi-visit-net.h"
23 #include "qapi/qmp/qdict.h"
24 #include "qemu/config-file.h"
25 #include "qemu/help_option.h"
26 #include "qemu/option.h"
27 
28 void hmp_set_link(Monitor *mon, const QDict *qdict)
29 {
30     const char *name = qdict_get_str(qdict, "name");
31     bool up = qdict_get_bool(qdict, "up");
32     Error *err = NULL;
33 
34     qmp_set_link(name, up, &err);
35     hmp_handle_error(mon, err);
36 }
37 
38 
39 void hmp_announce_self(Monitor *mon, const QDict *qdict)
40 {
41     const char *interfaces_str = qdict_get_try_str(qdict, "interfaces");
42     const char *id = qdict_get_try_str(qdict, "id");
43     AnnounceParameters *params = QAPI_CLONE(AnnounceParameters,
44                                             migrate_announce_params());
45 
46     qapi_free_strList(params->interfaces);
47     params->interfaces = hmp_split_at_comma(interfaces_str);
48     params->has_interfaces = params->interfaces != NULL;
49     params->id = g_strdup(id);
50     qmp_announce_self(params, NULL);
51     qapi_free_AnnounceParameters(params);
52 }
53 
54 void hmp_netdev_add(Monitor *mon, const QDict *qdict)
55 {
56     Error *err = NULL;
57     QemuOpts *opts;
58     const char *type = qdict_get_try_str(qdict, "type");
59 
60     if (type && is_help_option(type)) {
61         show_netdevs();
62         return;
63     }
64     opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict, &err);
65     if (err) {
66         goto out;
67     }
68 
69     netdev_add(opts, &err);
70     if (err) {
71         qemu_opts_del(opts);
72     }
73 
74 out:
75     hmp_handle_error(mon, err);
76 }
77 
78 void hmp_netdev_del(Monitor *mon, const QDict *qdict)
79 {
80     const char *id = qdict_get_str(qdict, "id");
81     Error *err = NULL;
82 
83     qmp_netdev_del(id, &err);
84     hmp_handle_error(mon, err);
85 }
86 
87 
88 void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
89 {
90     size_t len;
91     int i;
92 
93     if (nb_args != 2) {
94         return;
95     }
96     len = strlen(str);
97     readline_set_completion_index(rs, len);
98     for (i = 0; i < NET_CLIENT_DRIVER__MAX; i++) {
99         readline_add_completion_of(rs, str, NetClientDriver_str(i));
100     }
101 }
102 
103 void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
104 {
105     size_t len;
106 
107     len = strlen(str);
108     readline_set_completion_index(rs, len);
109     if (nb_args == 2) {
110         NetClientState *ncs[MAX_QUEUE_NUM];
111         int count, i;
112         count = qemu_find_net_clients_except(NULL, ncs,
113                                              NET_CLIENT_DRIVER_NONE,
114                                              MAX_QUEUE_NUM);
115         for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
116             readline_add_completion_of(rs, str, ncs[i]->name);
117         }
118     } else if (nb_args == 3) {
119         readline_add_completion_of(rs, str, "on");
120         readline_add_completion_of(rs, str, "off");
121     }
122 }
123 
124 void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
125 {
126     int len, count, i;
127     NetClientState *ncs[MAX_QUEUE_NUM];
128 
129     if (nb_args != 2) {
130         return;
131     }
132 
133     len = strlen(str);
134     readline_set_completion_index(rs, len);
135     count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_DRIVER_NIC,
136                                          MAX_QUEUE_NUM);
137     for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
138         if (ncs[i]->is_netdev) {
139             readline_add_completion_of(rs, str, ncs[i]->name);
140         }
141     }
142 }
143