root / net.c @ 136be99e
History | View | Annotate | Download (40.3 kB)
1 | 63a01ef8 | aliguori | /*
|
---|---|---|---|
2 | 63a01ef8 | aliguori | * QEMU System Emulator
|
3 | 63a01ef8 | aliguori | *
|
4 | 63a01ef8 | aliguori | * Copyright (c) 2003-2008 Fabrice Bellard
|
5 | 63a01ef8 | aliguori | *
|
6 | 63a01ef8 | aliguori | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 63a01ef8 | aliguori | * of this software and associated documentation files (the "Software"), to deal
|
8 | 63a01ef8 | aliguori | * in the Software without restriction, including without limitation the rights
|
9 | 63a01ef8 | aliguori | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | 63a01ef8 | aliguori | * copies of the Software, and to permit persons to whom the Software is
|
11 | 63a01ef8 | aliguori | * furnished to do so, subject to the following conditions:
|
12 | 63a01ef8 | aliguori | *
|
13 | 63a01ef8 | aliguori | * The above copyright notice and this permission notice shall be included in
|
14 | 63a01ef8 | aliguori | * all copies or substantial portions of the Software.
|
15 | 63a01ef8 | aliguori | *
|
16 | 63a01ef8 | aliguori | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | 63a01ef8 | aliguori | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | 63a01ef8 | aliguori | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | 63a01ef8 | aliguori | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | 63a01ef8 | aliguori | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | 63a01ef8 | aliguori | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | 63a01ef8 | aliguori | * THE SOFTWARE.
|
23 | 63a01ef8 | aliguori | */
|
24 | 1df49e04 | Mark McLoughlin | #include "net.h" |
25 | 63a01ef8 | aliguori | |
26 | d40cdb10 | blueswir1 | #include "config-host.h" |
27 | d40cdb10 | blueswir1 | |
28 | a8ed73f7 | Mark McLoughlin | #include "net/tap.h" |
29 | 42281ac9 | Mark McLoughlin | #include "net/socket.h" |
30 | 1abecf77 | Mark McLoughlin | #include "net/dump.h" |
31 | 68ac40d2 | Mark McLoughlin | #include "net/slirp.h" |
32 | 5c361cc3 | Mark McLoughlin | #include "net/vde.h" |
33 | f1d078c3 | Mark McLoughlin | #include "net/util.h" |
34 | 511d2b14 | blueswir1 | #include "monitor.h" |
35 | 1df49e04 | Mark McLoughlin | #include "qemu-common.h" |
36 | 511d2b14 | blueswir1 | #include "qemu_socket.h" |
37 | 4b37156c | Luiz Capitulino | #include "qmp-commands.h" |
38 | 75422b0d | Amit Shah | #include "hw/qdev.h" |
39 | ce053661 | Benjamin Poirier | #include "iov.h" |
40 | 511d2b14 | blueswir1 | |
41 | 2944e4ec | Stefan Weil | /* Net bridge is currently not supported for W32. */
|
42 | 2944e4ec | Stefan Weil | #if !defined(_WIN32)
|
43 | 2944e4ec | Stefan Weil | # define CONFIG_NET_BRIDGE
|
44 | 2944e4ec | Stefan Weil | #endif
|
45 | 2944e4ec | Stefan Weil | |
46 | 5610c3aa | Mark McLoughlin | static QTAILQ_HEAD(, VLANState) vlans;
|
47 | 577c4af9 | Mark McLoughlin | static QTAILQ_HEAD(, VLANClientState) non_vlan_clients;
|
48 | 63a01ef8 | aliguori | |
49 | cb4522cc | Gerd Hoffmann | int default_net = 1; |
50 | cb4522cc | Gerd Hoffmann | |
51 | 63a01ef8 | aliguori | /***********************************************************/
|
52 | 63a01ef8 | aliguori | /* network device redirectors */
|
53 | 63a01ef8 | aliguori | |
54 | 68ac40d2 | Mark McLoughlin | #if defined(DEBUG_NET)
|
55 | 63a01ef8 | aliguori | static void hex_dump(FILE *f, const uint8_t *buf, int size) |
56 | 63a01ef8 | aliguori | { |
57 | 63a01ef8 | aliguori | int len, i, j, c;
|
58 | 63a01ef8 | aliguori | |
59 | 63a01ef8 | aliguori | for(i=0;i<size;i+=16) { |
60 | 63a01ef8 | aliguori | len = size - i; |
61 | 63a01ef8 | aliguori | if (len > 16) |
62 | 63a01ef8 | aliguori | len = 16;
|
63 | 63a01ef8 | aliguori | fprintf(f, "%08x ", i);
|
64 | 63a01ef8 | aliguori | for(j=0;j<16;j++) { |
65 | 63a01ef8 | aliguori | if (j < len)
|
66 | 63a01ef8 | aliguori | fprintf(f, " %02x", buf[i+j]);
|
67 | 63a01ef8 | aliguori | else
|
68 | 63a01ef8 | aliguori | fprintf(f, " ");
|
69 | 63a01ef8 | aliguori | } |
70 | 63a01ef8 | aliguori | fprintf(f, " ");
|
71 | 63a01ef8 | aliguori | for(j=0;j<len;j++) { |
72 | 63a01ef8 | aliguori | c = buf[i+j]; |
73 | 63a01ef8 | aliguori | if (c < ' ' || c > '~') |
74 | 63a01ef8 | aliguori | c = '.';
|
75 | 63a01ef8 | aliguori | fprintf(f, "%c", c);
|
76 | 63a01ef8 | aliguori | } |
77 | 63a01ef8 | aliguori | fprintf(f, "\n");
|
78 | 63a01ef8 | aliguori | } |
79 | 63a01ef8 | aliguori | } |
80 | 63a01ef8 | aliguori | #endif
|
81 | 63a01ef8 | aliguori | |
82 | 63a01ef8 | aliguori | static int get_str_sep(char *buf, int buf_size, const char **pp, int sep) |
83 | 63a01ef8 | aliguori | { |
84 | 63a01ef8 | aliguori | const char *p, *p1; |
85 | 63a01ef8 | aliguori | int len;
|
86 | 63a01ef8 | aliguori | p = *pp; |
87 | 63a01ef8 | aliguori | p1 = strchr(p, sep); |
88 | 63a01ef8 | aliguori | if (!p1)
|
89 | 63a01ef8 | aliguori | return -1; |
90 | 63a01ef8 | aliguori | len = p1 - p; |
91 | 63a01ef8 | aliguori | p1++; |
92 | 63a01ef8 | aliguori | if (buf_size > 0) { |
93 | 63a01ef8 | aliguori | if (len > buf_size - 1) |
94 | 63a01ef8 | aliguori | len = buf_size - 1;
|
95 | 63a01ef8 | aliguori | memcpy(buf, p, len); |
96 | 63a01ef8 | aliguori | buf[len] = '\0';
|
97 | 63a01ef8 | aliguori | } |
98 | 63a01ef8 | aliguori | *pp = p1; |
99 | 63a01ef8 | aliguori | return 0; |
100 | 63a01ef8 | aliguori | } |
101 | 63a01ef8 | aliguori | |
102 | 63a01ef8 | aliguori | int parse_host_port(struct sockaddr_in *saddr, const char *str) |
103 | 63a01ef8 | aliguori | { |
104 | 63a01ef8 | aliguori | char buf[512]; |
105 | 63a01ef8 | aliguori | struct hostent *he;
|
106 | 63a01ef8 | aliguori | const char *p, *r; |
107 | 63a01ef8 | aliguori | int port;
|
108 | 63a01ef8 | aliguori | |
109 | 63a01ef8 | aliguori | p = str; |
110 | 63a01ef8 | aliguori | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
111 | 63a01ef8 | aliguori | return -1; |
112 | 63a01ef8 | aliguori | saddr->sin_family = AF_INET; |
113 | 63a01ef8 | aliguori | if (buf[0] == '\0') { |
114 | 63a01ef8 | aliguori | saddr->sin_addr.s_addr = 0;
|
115 | 63a01ef8 | aliguori | } else {
|
116 | cd390083 | blueswir1 | if (qemu_isdigit(buf[0])) { |
117 | 63a01ef8 | aliguori | if (!inet_aton(buf, &saddr->sin_addr))
|
118 | 63a01ef8 | aliguori | return -1; |
119 | 63a01ef8 | aliguori | } else {
|
120 | 63a01ef8 | aliguori | if ((he = gethostbyname(buf)) == NULL) |
121 | 63a01ef8 | aliguori | return - 1; |
122 | 63a01ef8 | aliguori | saddr->sin_addr = *(struct in_addr *)he->h_addr;
|
123 | 63a01ef8 | aliguori | } |
124 | 63a01ef8 | aliguori | } |
125 | 63a01ef8 | aliguori | port = strtol(p, (char **)&r, 0); |
126 | 63a01ef8 | aliguori | if (r == p)
|
127 | 63a01ef8 | aliguori | return -1; |
128 | 63a01ef8 | aliguori | saddr->sin_port = htons(port); |
129 | 63a01ef8 | aliguori | return 0; |
130 | 63a01ef8 | aliguori | } |
131 | 63a01ef8 | aliguori | |
132 | 7cb7434b | aliguori | void qemu_format_nic_info_str(VLANClientState *vc, uint8_t macaddr[6]) |
133 | 7cb7434b | aliguori | { |
134 | 7cb7434b | aliguori | snprintf(vc->info_str, sizeof(vc->info_str),
|
135 | 4dda4063 | aliguori | "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
|
136 | 4dda4063 | aliguori | vc->model, |
137 | 7cb7434b | aliguori | macaddr[0], macaddr[1], macaddr[2], |
138 | 7cb7434b | aliguori | macaddr[3], macaddr[4], macaddr[5]); |
139 | 7cb7434b | aliguori | } |
140 | 7cb7434b | aliguori | |
141 | 76d32cba | Gerd Hoffmann | void qemu_macaddr_default_if_unset(MACAddr *macaddr)
|
142 | 76d32cba | Gerd Hoffmann | { |
143 | 76d32cba | Gerd Hoffmann | static int index = 0; |
144 | 76d32cba | Gerd Hoffmann | static const MACAddr zero = { .a = { 0,0,0,0,0,0 } }; |
145 | 76d32cba | Gerd Hoffmann | |
146 | 76d32cba | Gerd Hoffmann | if (memcmp(macaddr, &zero, sizeof(zero)) != 0) |
147 | 76d32cba | Gerd Hoffmann | return;
|
148 | 76d32cba | Gerd Hoffmann | macaddr->a[0] = 0x52; |
149 | 76d32cba | Gerd Hoffmann | macaddr->a[1] = 0x54; |
150 | 76d32cba | Gerd Hoffmann | macaddr->a[2] = 0x00; |
151 | 76d32cba | Gerd Hoffmann | macaddr->a[3] = 0x12; |
152 | 76d32cba | Gerd Hoffmann | macaddr->a[4] = 0x34; |
153 | 76d32cba | Gerd Hoffmann | macaddr->a[5] = 0x56 + index++; |
154 | 76d32cba | Gerd Hoffmann | } |
155 | 76d32cba | Gerd Hoffmann | |
156 | 676cff29 | aliguori | static char *assign_name(VLANClientState *vc1, const char *model) |
157 | 676cff29 | aliguori | { |
158 | 676cff29 | aliguori | VLANState *vlan; |
159 | 53e51d85 | Markus Armbruster | VLANClientState *vc; |
160 | 676cff29 | aliguori | char buf[256]; |
161 | 676cff29 | aliguori | int id = 0; |
162 | 676cff29 | aliguori | |
163 | 5610c3aa | Mark McLoughlin | QTAILQ_FOREACH(vlan, &vlans, next) { |
164 | 5610c3aa | Mark McLoughlin | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
165 | 5610c3aa | Mark McLoughlin | if (vc != vc1 && strcmp(vc->model, model) == 0) { |
166 | 676cff29 | aliguori | id++; |
167 | 5610c3aa | Mark McLoughlin | } |
168 | 5610c3aa | Mark McLoughlin | } |
169 | 676cff29 | aliguori | } |
170 | 676cff29 | aliguori | |
171 | 53e51d85 | Markus Armbruster | QTAILQ_FOREACH(vc, &non_vlan_clients, next) { |
172 | 53e51d85 | Markus Armbruster | if (vc != vc1 && strcmp(vc->model, model) == 0) { |
173 | 53e51d85 | Markus Armbruster | id++; |
174 | 53e51d85 | Markus Armbruster | } |
175 | 53e51d85 | Markus Armbruster | } |
176 | 53e51d85 | Markus Armbruster | |
177 | 676cff29 | aliguori | snprintf(buf, sizeof(buf), "%s.%d", model, id); |
178 | 676cff29 | aliguori | |
179 | 7267c094 | Anthony Liguori | return g_strdup(buf);
|
180 | 676cff29 | aliguori | } |
181 | 676cff29 | aliguori | |
182 | 9a6ecb30 | Mark McLoughlin | static ssize_t qemu_deliver_packet(VLANClientState *sender,
|
183 | c0b8e49c | Mark McLoughlin | unsigned flags,
|
184 | 9a6ecb30 | Mark McLoughlin | const uint8_t *data,
|
185 | 9a6ecb30 | Mark McLoughlin | size_t size, |
186 | 9a6ecb30 | Mark McLoughlin | void *opaque);
|
187 | 9a6ecb30 | Mark McLoughlin | static ssize_t qemu_deliver_packet_iov(VLANClientState *sender,
|
188 | c0b8e49c | Mark McLoughlin | unsigned flags,
|
189 | 9a6ecb30 | Mark McLoughlin | const struct iovec *iov, |
190 | 9a6ecb30 | Mark McLoughlin | int iovcnt,
|
191 | 9a6ecb30 | Mark McLoughlin | void *opaque);
|
192 | 9a6ecb30 | Mark McLoughlin | |
193 | 45460d1a | Mark McLoughlin | VLANClientState *qemu_new_net_client(NetClientInfo *info, |
194 | 45460d1a | Mark McLoughlin | VLANState *vlan, |
195 | 45460d1a | Mark McLoughlin | VLANClientState *peer, |
196 | 45460d1a | Mark McLoughlin | const char *model, |
197 | 45460d1a | Mark McLoughlin | const char *name) |
198 | 63a01ef8 | aliguori | { |
199 | 5610c3aa | Mark McLoughlin | VLANClientState *vc; |
200 | 5610c3aa | Mark McLoughlin | |
201 | 45460d1a | Mark McLoughlin | assert(info->size >= sizeof(VLANClientState));
|
202 | 45460d1a | Mark McLoughlin | |
203 | 7267c094 | Anthony Liguori | vc = g_malloc0(info->size); |
204 | 5610c3aa | Mark McLoughlin | |
205 | 665a3b07 | Mark McLoughlin | vc->info = info; |
206 | 7267c094 | Anthony Liguori | vc->model = g_strdup(model); |
207 | 45460d1a | Mark McLoughlin | if (name) {
|
208 | 7267c094 | Anthony Liguori | vc->name = g_strdup(name); |
209 | 45460d1a | Mark McLoughlin | } else {
|
210 | 7a9f6e4a | aliguori | vc->name = assign_name(vc, model); |
211 | 45460d1a | Mark McLoughlin | } |
212 | 5610c3aa | Mark McLoughlin | |
213 | d80b9fc6 | Mark McLoughlin | if (vlan) {
|
214 | 283c7c63 | Mark McLoughlin | assert(!peer); |
215 | d80b9fc6 | Mark McLoughlin | vc->vlan = vlan; |
216 | d80b9fc6 | Mark McLoughlin | QTAILQ_INSERT_TAIL(&vc->vlan->clients, vc, next); |
217 | 577c4af9 | Mark McLoughlin | } else {
|
218 | 283c7c63 | Mark McLoughlin | if (peer) {
|
219 | 27f3f8a3 | Markus Armbruster | assert(!peer->peer); |
220 | 283c7c63 | Mark McLoughlin | vc->peer = peer; |
221 | 283c7c63 | Mark McLoughlin | peer->peer = vc; |
222 | 283c7c63 | Mark McLoughlin | } |
223 | 577c4af9 | Mark McLoughlin | QTAILQ_INSERT_TAIL(&non_vlan_clients, vc, next); |
224 | 9a6ecb30 | Mark McLoughlin | |
225 | 9a6ecb30 | Mark McLoughlin | vc->send_queue = qemu_new_net_queue(qemu_deliver_packet, |
226 | 9a6ecb30 | Mark McLoughlin | qemu_deliver_packet_iov, |
227 | 9a6ecb30 | Mark McLoughlin | vc); |
228 | d80b9fc6 | Mark McLoughlin | } |
229 | 63a01ef8 | aliguori | |
230 | 63a01ef8 | aliguori | return vc;
|
231 | 63a01ef8 | aliguori | } |
232 | 63a01ef8 | aliguori | |
233 | ebef2c09 | Mark McLoughlin | NICState *qemu_new_nic(NetClientInfo *info, |
234 | ebef2c09 | Mark McLoughlin | NICConf *conf, |
235 | ebef2c09 | Mark McLoughlin | const char *model, |
236 | ebef2c09 | Mark McLoughlin | const char *name, |
237 | ebef2c09 | Mark McLoughlin | void *opaque)
|
238 | ebef2c09 | Mark McLoughlin | { |
239 | ebef2c09 | Mark McLoughlin | VLANClientState *nc; |
240 | ebef2c09 | Mark McLoughlin | NICState *nic; |
241 | ebef2c09 | Mark McLoughlin | |
242 | ebef2c09 | Mark McLoughlin | assert(info->type == NET_CLIENT_TYPE_NIC); |
243 | ebef2c09 | Mark McLoughlin | assert(info->size >= sizeof(NICState));
|
244 | ebef2c09 | Mark McLoughlin | |
245 | ebef2c09 | Mark McLoughlin | nc = qemu_new_net_client(info, conf->vlan, conf->peer, model, name); |
246 | ebef2c09 | Mark McLoughlin | |
247 | ebef2c09 | Mark McLoughlin | nic = DO_UPCAST(NICState, nc, nc); |
248 | ebef2c09 | Mark McLoughlin | nic->conf = conf; |
249 | ebef2c09 | Mark McLoughlin | nic->opaque = opaque; |
250 | ebef2c09 | Mark McLoughlin | |
251 | ebef2c09 | Mark McLoughlin | return nic;
|
252 | ebef2c09 | Mark McLoughlin | } |
253 | ebef2c09 | Mark McLoughlin | |
254 | a083a89d | Michael S. Tsirkin | static void qemu_cleanup_vlan_client(VLANClientState *vc) |
255 | 63a01ef8 | aliguori | { |
256 | d80b9fc6 | Mark McLoughlin | if (vc->vlan) {
|
257 | d80b9fc6 | Mark McLoughlin | QTAILQ_REMOVE(&vc->vlan->clients, vc, next); |
258 | 577c4af9 | Mark McLoughlin | } else {
|
259 | 577c4af9 | Mark McLoughlin | QTAILQ_REMOVE(&non_vlan_clients, vc, next); |
260 | d80b9fc6 | Mark McLoughlin | } |
261 | 63a01ef8 | aliguori | |
262 | 665a3b07 | Mark McLoughlin | if (vc->info->cleanup) {
|
263 | 665a3b07 | Mark McLoughlin | vc->info->cleanup(vc); |
264 | 5610c3aa | Mark McLoughlin | } |
265 | a083a89d | Michael S. Tsirkin | } |
266 | 5610c3aa | Mark McLoughlin | |
267 | a083a89d | Michael S. Tsirkin | static void qemu_free_vlan_client(VLANClientState *vc) |
268 | a083a89d | Michael S. Tsirkin | { |
269 | a083a89d | Michael S. Tsirkin | if (!vc->vlan) {
|
270 | a083a89d | Michael S. Tsirkin | if (vc->send_queue) {
|
271 | a083a89d | Michael S. Tsirkin | qemu_del_net_queue(vc->send_queue); |
272 | a083a89d | Michael S. Tsirkin | } |
273 | a083a89d | Michael S. Tsirkin | if (vc->peer) {
|
274 | a083a89d | Michael S. Tsirkin | vc->peer->peer = NULL;
|
275 | a083a89d | Michael S. Tsirkin | } |
276 | a083a89d | Michael S. Tsirkin | } |
277 | 7267c094 | Anthony Liguori | g_free(vc->name); |
278 | 7267c094 | Anthony Liguori | g_free(vc->model); |
279 | 7267c094 | Anthony Liguori | g_free(vc); |
280 | 63a01ef8 | aliguori | } |
281 | 63a01ef8 | aliguori | |
282 | a083a89d | Michael S. Tsirkin | void qemu_del_vlan_client(VLANClientState *vc)
|
283 | a083a89d | Michael S. Tsirkin | { |
284 | a083a89d | Michael S. Tsirkin | /* If there is a peer NIC, delete and cleanup client, but do not free. */
|
285 | a083a89d | Michael S. Tsirkin | if (!vc->vlan && vc->peer && vc->peer->info->type == NET_CLIENT_TYPE_NIC) {
|
286 | a083a89d | Michael S. Tsirkin | NICState *nic = DO_UPCAST(NICState, nc, vc->peer); |
287 | a083a89d | Michael S. Tsirkin | if (nic->peer_deleted) {
|
288 | a083a89d | Michael S. Tsirkin | return;
|
289 | a083a89d | Michael S. Tsirkin | } |
290 | a083a89d | Michael S. Tsirkin | nic->peer_deleted = true;
|
291 | a083a89d | Michael S. Tsirkin | /* Let NIC know peer is gone. */
|
292 | a083a89d | Michael S. Tsirkin | vc->peer->link_down = true;
|
293 | a083a89d | Michael S. Tsirkin | if (vc->peer->info->link_status_changed) {
|
294 | a083a89d | Michael S. Tsirkin | vc->peer->info->link_status_changed(vc->peer); |
295 | a083a89d | Michael S. Tsirkin | } |
296 | a083a89d | Michael S. Tsirkin | qemu_cleanup_vlan_client(vc); |
297 | a083a89d | Michael S. Tsirkin | return;
|
298 | a083a89d | Michael S. Tsirkin | } |
299 | a083a89d | Michael S. Tsirkin | |
300 | a083a89d | Michael S. Tsirkin | /* If this is a peer NIC and peer has already been deleted, free it now. */
|
301 | a083a89d | Michael S. Tsirkin | if (!vc->vlan && vc->peer && vc->info->type == NET_CLIENT_TYPE_NIC) {
|
302 | a083a89d | Michael S. Tsirkin | NICState *nic = DO_UPCAST(NICState, nc, vc); |
303 | a083a89d | Michael S. Tsirkin | if (nic->peer_deleted) {
|
304 | a083a89d | Michael S. Tsirkin | qemu_free_vlan_client(vc->peer); |
305 | a083a89d | Michael S. Tsirkin | } |
306 | a083a89d | Michael S. Tsirkin | } |
307 | a083a89d | Michael S. Tsirkin | |
308 | a083a89d | Michael S. Tsirkin | qemu_cleanup_vlan_client(vc); |
309 | a083a89d | Michael S. Tsirkin | qemu_free_vlan_client(vc); |
310 | a083a89d | Michael S. Tsirkin | } |
311 | a083a89d | Michael S. Tsirkin | |
312 | 68ac40d2 | Mark McLoughlin | VLANClientState * |
313 | 1a609520 | Jan Kiszka | qemu_find_vlan_client_by_name(Monitor *mon, int vlan_id,
|
314 | 1a609520 | Jan Kiszka | const char *client_str) |
315 | 1a609520 | Jan Kiszka | { |
316 | 1a609520 | Jan Kiszka | VLANState *vlan; |
317 | 1a609520 | Jan Kiszka | VLANClientState *vc; |
318 | 1a609520 | Jan Kiszka | |
319 | 1a609520 | Jan Kiszka | vlan = qemu_find_vlan(vlan_id, 0);
|
320 | 1a609520 | Jan Kiszka | if (!vlan) {
|
321 | 1a609520 | Jan Kiszka | monitor_printf(mon, "unknown VLAN %d\n", vlan_id);
|
322 | 1a609520 | Jan Kiszka | return NULL; |
323 | 1a609520 | Jan Kiszka | } |
324 | 1a609520 | Jan Kiszka | |
325 | 5610c3aa | Mark McLoughlin | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
326 | 1a609520 | Jan Kiszka | if (!strcmp(vc->name, client_str)) {
|
327 | 1a609520 | Jan Kiszka | break;
|
328 | 1a609520 | Jan Kiszka | } |
329 | 1a609520 | Jan Kiszka | } |
330 | 1a609520 | Jan Kiszka | if (!vc) {
|
331 | 1a609520 | Jan Kiszka | monitor_printf(mon, "can't find device %s on VLAN %d\n",
|
332 | 1a609520 | Jan Kiszka | client_str, vlan_id); |
333 | 1a609520 | Jan Kiszka | } |
334 | 1a609520 | Jan Kiszka | |
335 | 1a609520 | Jan Kiszka | return vc;
|
336 | 1a609520 | Jan Kiszka | } |
337 | 1a609520 | Jan Kiszka | |
338 | 57f9ef17 | Mark McLoughlin | void qemu_foreach_nic(qemu_nic_foreach func, void *opaque) |
339 | 57f9ef17 | Mark McLoughlin | { |
340 | 57f9ef17 | Mark McLoughlin | VLANClientState *nc; |
341 | 57f9ef17 | Mark McLoughlin | VLANState *vlan; |
342 | 57f9ef17 | Mark McLoughlin | |
343 | 57f9ef17 | Mark McLoughlin | QTAILQ_FOREACH(nc, &non_vlan_clients, next) { |
344 | 57f9ef17 | Mark McLoughlin | if (nc->info->type == NET_CLIENT_TYPE_NIC) {
|
345 | 57f9ef17 | Mark McLoughlin | func(DO_UPCAST(NICState, nc, nc), opaque); |
346 | 57f9ef17 | Mark McLoughlin | } |
347 | 57f9ef17 | Mark McLoughlin | } |
348 | 57f9ef17 | Mark McLoughlin | |
349 | 57f9ef17 | Mark McLoughlin | QTAILQ_FOREACH(vlan, &vlans, next) { |
350 | 57f9ef17 | Mark McLoughlin | QTAILQ_FOREACH(nc, &vlan->clients, next) { |
351 | 57f9ef17 | Mark McLoughlin | if (nc->info->type == NET_CLIENT_TYPE_NIC) {
|
352 | 57f9ef17 | Mark McLoughlin | func(DO_UPCAST(NICState, nc, nc), opaque); |
353 | 57f9ef17 | Mark McLoughlin | } |
354 | 57f9ef17 | Mark McLoughlin | } |
355 | 57f9ef17 | Mark McLoughlin | } |
356 | 57f9ef17 | Mark McLoughlin | } |
357 | 57f9ef17 | Mark McLoughlin | |
358 | 2e1e0641 | Mark McLoughlin | int qemu_can_send_packet(VLANClientState *sender)
|
359 | 63a01ef8 | aliguori | { |
360 | 2e1e0641 | Mark McLoughlin | VLANState *vlan = sender->vlan; |
361 | 63a01ef8 | aliguori | VLANClientState *vc; |
362 | 63a01ef8 | aliguori | |
363 | 9a6ecb30 | Mark McLoughlin | if (sender->peer) {
|
364 | 893379ef | Mark McLoughlin | if (sender->peer->receive_disabled) {
|
365 | 893379ef | Mark McLoughlin | return 0; |
366 | 665a3b07 | Mark McLoughlin | } else if (sender->peer->info->can_receive && |
367 | 665a3b07 | Mark McLoughlin | !sender->peer->info->can_receive(sender->peer)) { |
368 | 9a6ecb30 | Mark McLoughlin | return 0; |
369 | 893379ef | Mark McLoughlin | } else {
|
370 | 893379ef | Mark McLoughlin | return 1; |
371 | 9a6ecb30 | Mark McLoughlin | } |
372 | 9a6ecb30 | Mark McLoughlin | } |
373 | 9a6ecb30 | Mark McLoughlin | |
374 | d80b9fc6 | Mark McLoughlin | if (!sender->vlan) {
|
375 | d80b9fc6 | Mark McLoughlin | return 1; |
376 | d80b9fc6 | Mark McLoughlin | } |
377 | d80b9fc6 | Mark McLoughlin | |
378 | 5610c3aa | Mark McLoughlin | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
379 | 2e1e0641 | Mark McLoughlin | if (vc == sender) {
|
380 | 2e1e0641 | Mark McLoughlin | continue;
|
381 | 2e1e0641 | Mark McLoughlin | } |
382 | 2e1e0641 | Mark McLoughlin | |
383 | cda9046b | Mark McLoughlin | /* no can_receive() handler, they can always receive */
|
384 | 60c07d93 | Vincent Palatin | if (vc->info->can_receive && !vc->info->can_receive(vc)) {
|
385 | 60c07d93 | Vincent Palatin | return 0; |
386 | 63a01ef8 | aliguori | } |
387 | 63a01ef8 | aliguori | } |
388 | 60c07d93 | Vincent Palatin | return 1; |
389 | 63a01ef8 | aliguori | } |
390 | 63a01ef8 | aliguori | |
391 | 9a6ecb30 | Mark McLoughlin | static ssize_t qemu_deliver_packet(VLANClientState *sender,
|
392 | c0b8e49c | Mark McLoughlin | unsigned flags,
|
393 | 9a6ecb30 | Mark McLoughlin | const uint8_t *data,
|
394 | 9a6ecb30 | Mark McLoughlin | size_t size, |
395 | 9a6ecb30 | Mark McLoughlin | void *opaque)
|
396 | 9a6ecb30 | Mark McLoughlin | { |
397 | 9a6ecb30 | Mark McLoughlin | VLANClientState *vc = opaque; |
398 | 893379ef | Mark McLoughlin | ssize_t ret; |
399 | 9a6ecb30 | Mark McLoughlin | |
400 | 9a6ecb30 | Mark McLoughlin | if (vc->link_down) {
|
401 | 9a6ecb30 | Mark McLoughlin | return size;
|
402 | 9a6ecb30 | Mark McLoughlin | } |
403 | 9a6ecb30 | Mark McLoughlin | |
404 | 893379ef | Mark McLoughlin | if (vc->receive_disabled) {
|
405 | 893379ef | Mark McLoughlin | return 0; |
406 | 893379ef | Mark McLoughlin | } |
407 | 893379ef | Mark McLoughlin | |
408 | 665a3b07 | Mark McLoughlin | if (flags & QEMU_NET_PACKET_FLAG_RAW && vc->info->receive_raw) {
|
409 | 665a3b07 | Mark McLoughlin | ret = vc->info->receive_raw(vc, data, size); |
410 | 893379ef | Mark McLoughlin | } else {
|
411 | 665a3b07 | Mark McLoughlin | ret = vc->info->receive(vc, data, size); |
412 | 893379ef | Mark McLoughlin | } |
413 | 893379ef | Mark McLoughlin | |
414 | 893379ef | Mark McLoughlin | if (ret == 0) { |
415 | 893379ef | Mark McLoughlin | vc->receive_disabled = 1;
|
416 | 893379ef | Mark McLoughlin | }; |
417 | 893379ef | Mark McLoughlin | |
418 | 893379ef | Mark McLoughlin | return ret;
|
419 | 9a6ecb30 | Mark McLoughlin | } |
420 | 9a6ecb30 | Mark McLoughlin | |
421 | f7105843 | Mark McLoughlin | static ssize_t qemu_vlan_deliver_packet(VLANClientState *sender,
|
422 | c0b8e49c | Mark McLoughlin | unsigned flags,
|
423 | f7105843 | Mark McLoughlin | const uint8_t *buf,
|
424 | f7105843 | Mark McLoughlin | size_t size, |
425 | f7105843 | Mark McLoughlin | void *opaque)
|
426 | 63a01ef8 | aliguori | { |
427 | f7105843 | Mark McLoughlin | VLANState *vlan = opaque; |
428 | 63a01ef8 | aliguori | VLANClientState *vc; |
429 | 893379ef | Mark McLoughlin | ssize_t ret = -1;
|
430 | 63a01ef8 | aliguori | |
431 | f7105843 | Mark McLoughlin | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
432 | 3e021d40 | Mark McLoughlin | ssize_t len; |
433 | 3e021d40 | Mark McLoughlin | |
434 | 3e021d40 | Mark McLoughlin | if (vc == sender) {
|
435 | 3e021d40 | Mark McLoughlin | continue;
|
436 | 764a4d1d | aliguori | } |
437 | 3e021d40 | Mark McLoughlin | |
438 | 3e021d40 | Mark McLoughlin | if (vc->link_down) {
|
439 | 3e021d40 | Mark McLoughlin | ret = size; |
440 | 3e021d40 | Mark McLoughlin | continue;
|
441 | 3e021d40 | Mark McLoughlin | } |
442 | 3e021d40 | Mark McLoughlin | |
443 | 893379ef | Mark McLoughlin | if (vc->receive_disabled) {
|
444 | 893379ef | Mark McLoughlin | ret = 0;
|
445 | 893379ef | Mark McLoughlin | continue;
|
446 | 893379ef | Mark McLoughlin | } |
447 | 893379ef | Mark McLoughlin | |
448 | 665a3b07 | Mark McLoughlin | if (flags & QEMU_NET_PACKET_FLAG_RAW && vc->info->receive_raw) {
|
449 | 665a3b07 | Mark McLoughlin | len = vc->info->receive_raw(vc, buf, size); |
450 | 893379ef | Mark McLoughlin | } else {
|
451 | 665a3b07 | Mark McLoughlin | len = vc->info->receive(vc, buf, size); |
452 | 893379ef | Mark McLoughlin | } |
453 | 893379ef | Mark McLoughlin | |
454 | 893379ef | Mark McLoughlin | if (len == 0) { |
455 | 893379ef | Mark McLoughlin | vc->receive_disabled = 1;
|
456 | 893379ef | Mark McLoughlin | } |
457 | 3e021d40 | Mark McLoughlin | |
458 | 3e021d40 | Mark McLoughlin | ret = (ret >= 0) ? ret : len;
|
459 | 893379ef | Mark McLoughlin | |
460 | 764a4d1d | aliguori | } |
461 | 3e021d40 | Mark McLoughlin | |
462 | 3e021d40 | Mark McLoughlin | return ret;
|
463 | 764a4d1d | aliguori | } |
464 | 764a4d1d | aliguori | |
465 | 8cad5516 | Mark McLoughlin | void qemu_purge_queued_packets(VLANClientState *vc)
|
466 | 8cad5516 | Mark McLoughlin | { |
467 | 9a6ecb30 | Mark McLoughlin | NetQueue *queue; |
468 | 9a6ecb30 | Mark McLoughlin | |
469 | 9a6ecb30 | Mark McLoughlin | if (!vc->peer && !vc->vlan) {
|
470 | d80b9fc6 | Mark McLoughlin | return;
|
471 | 9a6ecb30 | Mark McLoughlin | } |
472 | d80b9fc6 | Mark McLoughlin | |
473 | 9a6ecb30 | Mark McLoughlin | if (vc->peer) {
|
474 | 9a6ecb30 | Mark McLoughlin | queue = vc->peer->send_queue; |
475 | 9a6ecb30 | Mark McLoughlin | } else {
|
476 | 9a6ecb30 | Mark McLoughlin | queue = vc->vlan->send_queue; |
477 | 9a6ecb30 | Mark McLoughlin | } |
478 | 9a6ecb30 | Mark McLoughlin | |
479 | 9a6ecb30 | Mark McLoughlin | qemu_net_queue_purge(queue, vc); |
480 | 8cad5516 | Mark McLoughlin | } |
481 | 8cad5516 | Mark McLoughlin | |
482 | f3b6c7fc | Mark McLoughlin | void qemu_flush_queued_packets(VLANClientState *vc)
|
483 | e94667b9 | Mark McLoughlin | { |
484 | 9a6ecb30 | Mark McLoughlin | NetQueue *queue; |
485 | 9a6ecb30 | Mark McLoughlin | |
486 | 893379ef | Mark McLoughlin | vc->receive_disabled = 0;
|
487 | 893379ef | Mark McLoughlin | |
488 | 9a6ecb30 | Mark McLoughlin | if (vc->vlan) {
|
489 | 9a6ecb30 | Mark McLoughlin | queue = vc->vlan->send_queue; |
490 | 9a6ecb30 | Mark McLoughlin | } else {
|
491 | 9a6ecb30 | Mark McLoughlin | queue = vc->send_queue; |
492 | 9a6ecb30 | Mark McLoughlin | } |
493 | d80b9fc6 | Mark McLoughlin | |
494 | 9a6ecb30 | Mark McLoughlin | qemu_net_queue_flush(queue); |
495 | e94667b9 | Mark McLoughlin | } |
496 | e94667b9 | Mark McLoughlin | |
497 | ca77d175 | Mark McLoughlin | static ssize_t qemu_send_packet_async_with_flags(VLANClientState *sender,
|
498 | ca77d175 | Mark McLoughlin | unsigned flags,
|
499 | ca77d175 | Mark McLoughlin | const uint8_t *buf, int size, |
500 | ca77d175 | Mark McLoughlin | NetPacketSent *sent_cb) |
501 | 764a4d1d | aliguori | { |
502 | 9a6ecb30 | Mark McLoughlin | NetQueue *queue; |
503 | 436e5e53 | aliguori | |
504 | 63a01ef8 | aliguori | #ifdef DEBUG_NET
|
505 | d80b9fc6 | Mark McLoughlin | printf("qemu_send_packet_async:\n");
|
506 | 63a01ef8 | aliguori | hex_dump(stdout, buf, size); |
507 | 63a01ef8 | aliguori | #endif
|
508 | f3b6c7fc | Mark McLoughlin | |
509 | 9a6ecb30 | Mark McLoughlin | if (sender->link_down || (!sender->peer && !sender->vlan)) {
|
510 | 9a6ecb30 | Mark McLoughlin | return size;
|
511 | 9a6ecb30 | Mark McLoughlin | } |
512 | 9a6ecb30 | Mark McLoughlin | |
513 | 9a6ecb30 | Mark McLoughlin | if (sender->peer) {
|
514 | 9a6ecb30 | Mark McLoughlin | queue = sender->peer->send_queue; |
515 | 9a6ecb30 | Mark McLoughlin | } else {
|
516 | 9a6ecb30 | Mark McLoughlin | queue = sender->vlan->send_queue; |
517 | 9a6ecb30 | Mark McLoughlin | } |
518 | 9a6ecb30 | Mark McLoughlin | |
519 | ca77d175 | Mark McLoughlin | return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb);
|
520 | ca77d175 | Mark McLoughlin | } |
521 | ca77d175 | Mark McLoughlin | |
522 | ca77d175 | Mark McLoughlin | ssize_t qemu_send_packet_async(VLANClientState *sender, |
523 | ca77d175 | Mark McLoughlin | const uint8_t *buf, int size, |
524 | ca77d175 | Mark McLoughlin | NetPacketSent *sent_cb) |
525 | ca77d175 | Mark McLoughlin | { |
526 | ca77d175 | Mark McLoughlin | return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE,
|
527 | ca77d175 | Mark McLoughlin | buf, size, sent_cb); |
528 | f3b6c7fc | Mark McLoughlin | } |
529 | f3b6c7fc | Mark McLoughlin | |
530 | f3b6c7fc | Mark McLoughlin | void qemu_send_packet(VLANClientState *vc, const uint8_t *buf, int size) |
531 | f3b6c7fc | Mark McLoughlin | { |
532 | f3b6c7fc | Mark McLoughlin | qemu_send_packet_async(vc, buf, size, NULL);
|
533 | 63a01ef8 | aliguori | } |
534 | 63a01ef8 | aliguori | |
535 | ca77d175 | Mark McLoughlin | ssize_t qemu_send_packet_raw(VLANClientState *vc, const uint8_t *buf, int size) |
536 | ca77d175 | Mark McLoughlin | { |
537 | ca77d175 | Mark McLoughlin | return qemu_send_packet_async_with_flags(vc, QEMU_NET_PACKET_FLAG_RAW,
|
538 | ca77d175 | Mark McLoughlin | buf, size, NULL);
|
539 | ca77d175 | Mark McLoughlin | } |
540 | ca77d175 | Mark McLoughlin | |
541 | fbe78f4f | aliguori | static ssize_t vc_sendv_compat(VLANClientState *vc, const struct iovec *iov, |
542 | fbe78f4f | aliguori | int iovcnt)
|
543 | fbe78f4f | aliguori | { |
544 | fbe78f4f | aliguori | uint8_t buffer[4096];
|
545 | ce053661 | Benjamin Poirier | size_t offset; |
546 | fbe78f4f | aliguori | |
547 | ce053661 | Benjamin Poirier | offset = iov_to_buf(iov, iovcnt, buffer, 0, sizeof(buffer)); |
548 | fbe78f4f | aliguori | |
549 | 665a3b07 | Mark McLoughlin | return vc->info->receive(vc, buffer, offset);
|
550 | fbe78f4f | aliguori | } |
551 | fbe78f4f | aliguori | |
552 | 9a6ecb30 | Mark McLoughlin | static ssize_t qemu_deliver_packet_iov(VLANClientState *sender,
|
553 | c0b8e49c | Mark McLoughlin | unsigned flags,
|
554 | 9a6ecb30 | Mark McLoughlin | const struct iovec *iov, |
555 | 9a6ecb30 | Mark McLoughlin | int iovcnt,
|
556 | 9a6ecb30 | Mark McLoughlin | void *opaque)
|
557 | 9a6ecb30 | Mark McLoughlin | { |
558 | 9a6ecb30 | Mark McLoughlin | VLANClientState *vc = opaque; |
559 | 9a6ecb30 | Mark McLoughlin | |
560 | 9a6ecb30 | Mark McLoughlin | if (vc->link_down) {
|
561 | ce053661 | Benjamin Poirier | return iov_size(iov, iovcnt);
|
562 | 9a6ecb30 | Mark McLoughlin | } |
563 | 9a6ecb30 | Mark McLoughlin | |
564 | 665a3b07 | Mark McLoughlin | if (vc->info->receive_iov) {
|
565 | 665a3b07 | Mark McLoughlin | return vc->info->receive_iov(vc, iov, iovcnt);
|
566 | 9a6ecb30 | Mark McLoughlin | } else {
|
567 | 9a6ecb30 | Mark McLoughlin | return vc_sendv_compat(vc, iov, iovcnt);
|
568 | 9a6ecb30 | Mark McLoughlin | } |
569 | 9a6ecb30 | Mark McLoughlin | } |
570 | 9a6ecb30 | Mark McLoughlin | |
571 | f7105843 | Mark McLoughlin | static ssize_t qemu_vlan_deliver_packet_iov(VLANClientState *sender,
|
572 | c0b8e49c | Mark McLoughlin | unsigned flags,
|
573 | f7105843 | Mark McLoughlin | const struct iovec *iov, |
574 | f7105843 | Mark McLoughlin | int iovcnt,
|
575 | f7105843 | Mark McLoughlin | void *opaque)
|
576 | fbe78f4f | aliguori | { |
577 | f7105843 | Mark McLoughlin | VLANState *vlan = opaque; |
578 | fbe78f4f | aliguori | VLANClientState *vc; |
579 | f7105843 | Mark McLoughlin | ssize_t ret = -1;
|
580 | e94667b9 | Mark McLoughlin | |
581 | f7105843 | Mark McLoughlin | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
582 | e94667b9 | Mark McLoughlin | ssize_t len; |
583 | e94667b9 | Mark McLoughlin | |
584 | e94667b9 | Mark McLoughlin | if (vc == sender) {
|
585 | e94667b9 | Mark McLoughlin | continue;
|
586 | e94667b9 | Mark McLoughlin | } |
587 | e94667b9 | Mark McLoughlin | |
588 | e94667b9 | Mark McLoughlin | if (vc->link_down) {
|
589 | ce053661 | Benjamin Poirier | ret = iov_size(iov, iovcnt); |
590 | e94667b9 | Mark McLoughlin | continue;
|
591 | e94667b9 | Mark McLoughlin | } |
592 | e94667b9 | Mark McLoughlin | |
593 | ca77d175 | Mark McLoughlin | assert(!(flags & QEMU_NET_PACKET_FLAG_RAW)); |
594 | ca77d175 | Mark McLoughlin | |
595 | 665a3b07 | Mark McLoughlin | if (vc->info->receive_iov) {
|
596 | 665a3b07 | Mark McLoughlin | len = vc->info->receive_iov(vc, iov, iovcnt); |
597 | e94667b9 | Mark McLoughlin | } else {
|
598 | e94667b9 | Mark McLoughlin | len = vc_sendv_compat(vc, iov, iovcnt); |
599 | e94667b9 | Mark McLoughlin | } |
600 | e94667b9 | Mark McLoughlin | |
601 | e94667b9 | Mark McLoughlin | ret = (ret >= 0) ? ret : len;
|
602 | e94667b9 | Mark McLoughlin | } |
603 | e94667b9 | Mark McLoughlin | |
604 | e94667b9 | Mark McLoughlin | return ret;
|
605 | e94667b9 | Mark McLoughlin | } |
606 | e94667b9 | Mark McLoughlin | |
607 | f3b6c7fc | Mark McLoughlin | ssize_t qemu_sendv_packet_async(VLANClientState *sender, |
608 | f3b6c7fc | Mark McLoughlin | const struct iovec *iov, int iovcnt, |
609 | f3b6c7fc | Mark McLoughlin | NetPacketSent *sent_cb) |
610 | e94667b9 | Mark McLoughlin | { |
611 | 9a6ecb30 | Mark McLoughlin | NetQueue *queue; |
612 | 9a6ecb30 | Mark McLoughlin | |
613 | 9a6ecb30 | Mark McLoughlin | if (sender->link_down || (!sender->peer && !sender->vlan)) {
|
614 | ce053661 | Benjamin Poirier | return iov_size(iov, iovcnt);
|
615 | e94667b9 | Mark McLoughlin | } |
616 | e94667b9 | Mark McLoughlin | |
617 | 9a6ecb30 | Mark McLoughlin | if (sender->peer) {
|
618 | 9a6ecb30 | Mark McLoughlin | queue = sender->peer->send_queue; |
619 | 9a6ecb30 | Mark McLoughlin | } else {
|
620 | 9a6ecb30 | Mark McLoughlin | queue = sender->vlan->send_queue; |
621 | 9a6ecb30 | Mark McLoughlin | } |
622 | 9a6ecb30 | Mark McLoughlin | |
623 | c0b8e49c | Mark McLoughlin | return qemu_net_queue_send_iov(queue, sender,
|
624 | c0b8e49c | Mark McLoughlin | QEMU_NET_PACKET_FLAG_NONE, |
625 | c0b8e49c | Mark McLoughlin | iov, iovcnt, sent_cb); |
626 | fbe78f4f | aliguori | } |
627 | fbe78f4f | aliguori | |
628 | f3b6c7fc | Mark McLoughlin | ssize_t |
629 | f3b6c7fc | Mark McLoughlin | qemu_sendv_packet(VLANClientState *vc, const struct iovec *iov, int iovcnt) |
630 | f3b6c7fc | Mark McLoughlin | { |
631 | f3b6c7fc | Mark McLoughlin | return qemu_sendv_packet_async(vc, iov, iovcnt, NULL); |
632 | f3b6c7fc | Mark McLoughlin | } |
633 | f3b6c7fc | Mark McLoughlin | |
634 | 63a01ef8 | aliguori | /* find or alloc a new VLAN */
|
635 | 1a609520 | Jan Kiszka | VLANState *qemu_find_vlan(int id, int allocate) |
636 | 63a01ef8 | aliguori | { |
637 | 5610c3aa | Mark McLoughlin | VLANState *vlan; |
638 | 5610c3aa | Mark McLoughlin | |
639 | 5610c3aa | Mark McLoughlin | QTAILQ_FOREACH(vlan, &vlans, next) { |
640 | 5610c3aa | Mark McLoughlin | if (vlan->id == id) {
|
641 | 63a01ef8 | aliguori | return vlan;
|
642 | 5610c3aa | Mark McLoughlin | } |
643 | 63a01ef8 | aliguori | } |
644 | 5610c3aa | Mark McLoughlin | |
645 | 1a609520 | Jan Kiszka | if (!allocate) {
|
646 | 1a609520 | Jan Kiszka | return NULL; |
647 | 1a609520 | Jan Kiszka | } |
648 | 5610c3aa | Mark McLoughlin | |
649 | 7267c094 | Anthony Liguori | vlan = g_malloc0(sizeof(VLANState));
|
650 | 63a01ef8 | aliguori | vlan->id = id; |
651 | 5610c3aa | Mark McLoughlin | QTAILQ_INIT(&vlan->clients); |
652 | f7105843 | Mark McLoughlin | |
653 | f7105843 | Mark McLoughlin | vlan->send_queue = qemu_new_net_queue(qemu_vlan_deliver_packet, |
654 | f7105843 | Mark McLoughlin | qemu_vlan_deliver_packet_iov, |
655 | f7105843 | Mark McLoughlin | vlan); |
656 | 5610c3aa | Mark McLoughlin | |
657 | 5610c3aa | Mark McLoughlin | QTAILQ_INSERT_TAIL(&vlans, vlan, next); |
658 | 5610c3aa | Mark McLoughlin | |
659 | 63a01ef8 | aliguori | return vlan;
|
660 | 63a01ef8 | aliguori | } |
661 | 63a01ef8 | aliguori | |
662 | 2ef924b4 | Gerd Hoffmann | VLANClientState *qemu_find_netdev(const char *id) |
663 | 5869c4d5 | Mark McLoughlin | { |
664 | 5869c4d5 | Mark McLoughlin | VLANClientState *vc; |
665 | 5869c4d5 | Mark McLoughlin | |
666 | 5869c4d5 | Mark McLoughlin | QTAILQ_FOREACH(vc, &non_vlan_clients, next) { |
667 | 85dde9a9 | Markus Armbruster | if (vc->info->type == NET_CLIENT_TYPE_NIC)
|
668 | 85dde9a9 | Markus Armbruster | continue;
|
669 | 5869c4d5 | Mark McLoughlin | if (!strcmp(vc->name, id)) {
|
670 | 5869c4d5 | Mark McLoughlin | return vc;
|
671 | 5869c4d5 | Mark McLoughlin | } |
672 | 5869c4d5 | Mark McLoughlin | } |
673 | 5869c4d5 | Mark McLoughlin | |
674 | 5869c4d5 | Mark McLoughlin | return NULL; |
675 | 5869c4d5 | Mark McLoughlin | } |
676 | 5869c4d5 | Mark McLoughlin | |
677 | 7697079b | aliguori | static int nic_get_free_idx(void) |
678 | 7697079b | aliguori | { |
679 | 7697079b | aliguori | int index;
|
680 | 7697079b | aliguori | |
681 | 7697079b | aliguori | for (index = 0; index < MAX_NICS; index++) |
682 | 7697079b | aliguori | if (!nd_table[index].used)
|
683 | 7697079b | aliguori | return index;
|
684 | 7697079b | aliguori | return -1; |
685 | 7697079b | aliguori | } |
686 | 7697079b | aliguori | |
687 | 07caea31 | Markus Armbruster | int qemu_show_nic_models(const char *arg, const char *const *models) |
688 | 07caea31 | Markus Armbruster | { |
689 | 07caea31 | Markus Armbruster | int i;
|
690 | 07caea31 | Markus Armbruster | |
691 | 07caea31 | Markus Armbruster | if (!arg || strcmp(arg, "?")) |
692 | 07caea31 | Markus Armbruster | return 0; |
693 | 07caea31 | Markus Armbruster | |
694 | 07caea31 | Markus Armbruster | fprintf(stderr, "qemu: Supported NIC models: ");
|
695 | 07caea31 | Markus Armbruster | for (i = 0 ; models[i]; i++) |
696 | 07caea31 | Markus Armbruster | fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n'); |
697 | 07caea31 | Markus Armbruster | return 1; |
698 | 07caea31 | Markus Armbruster | } |
699 | 07caea31 | Markus Armbruster | |
700 | d07f22c5 | aliguori | void qemu_check_nic_model(NICInfo *nd, const char *model) |
701 | d07f22c5 | aliguori | { |
702 | d07f22c5 | aliguori | const char *models[2]; |
703 | d07f22c5 | aliguori | |
704 | d07f22c5 | aliguori | models[0] = model;
|
705 | d07f22c5 | aliguori | models[1] = NULL; |
706 | d07f22c5 | aliguori | |
707 | 07caea31 | Markus Armbruster | if (qemu_show_nic_models(nd->model, models))
|
708 | 07caea31 | Markus Armbruster | exit(0);
|
709 | 07caea31 | Markus Armbruster | if (qemu_find_nic_model(nd, models, model) < 0) |
710 | 07caea31 | Markus Armbruster | exit(1);
|
711 | d07f22c5 | aliguori | } |
712 | d07f22c5 | aliguori | |
713 | 07caea31 | Markus Armbruster | int qemu_find_nic_model(NICInfo *nd, const char * const *models, |
714 | 07caea31 | Markus Armbruster | const char *default_model) |
715 | d07f22c5 | aliguori | { |
716 | 07caea31 | Markus Armbruster | int i;
|
717 | d07f22c5 | aliguori | |
718 | d07f22c5 | aliguori | if (!nd->model)
|
719 | 7267c094 | Anthony Liguori | nd->model = g_strdup(default_model); |
720 | d07f22c5 | aliguori | |
721 | 07caea31 | Markus Armbruster | for (i = 0 ; models[i]; i++) { |
722 | 07caea31 | Markus Armbruster | if (strcmp(nd->model, models[i]) == 0) |
723 | 07caea31 | Markus Armbruster | return i;
|
724 | d07f22c5 | aliguori | } |
725 | d07f22c5 | aliguori | |
726 | 6daf194d | Markus Armbruster | error_report("Unsupported NIC model: %s", nd->model);
|
727 | 07caea31 | Markus Armbruster | return -1; |
728 | d07f22c5 | aliguori | } |
729 | d07f22c5 | aliguori | |
730 | 5281d757 | Mark McLoughlin | int net_handle_fd_param(Monitor *mon, const char *param) |
731 | c1d6eed7 | Mark McLoughlin | { |
732 | f7c31d63 | Jason Wang | int fd;
|
733 | f7c31d63 | Jason Wang | |
734 | f7c31d63 | Jason Wang | if (!qemu_isdigit(param[0]) && mon) { |
735 | c1d6eed7 | Mark McLoughlin | |
736 | c1d6eed7 | Mark McLoughlin | fd = monitor_get_fd(mon, param); |
737 | c1d6eed7 | Mark McLoughlin | if (fd == -1) { |
738 | 1ecda02b | Markus Armbruster | error_report("No file descriptor named %s found", param);
|
739 | c1d6eed7 | Mark McLoughlin | return -1; |
740 | c1d6eed7 | Mark McLoughlin | } |
741 | c1d6eed7 | Mark McLoughlin | } else {
|
742 | 443916d1 | Stefan Berger | fd = qemu_parse_fd(param); |
743 | c1d6eed7 | Mark McLoughlin | } |
744 | f7c31d63 | Jason Wang | |
745 | f7c31d63 | Jason Wang | return fd;
|
746 | c1d6eed7 | Mark McLoughlin | } |
747 | c1d6eed7 | Mark McLoughlin | |
748 | f6b134ac | Mark McLoughlin | static int net_init_nic(QemuOpts *opts, |
749 | f6b134ac | Mark McLoughlin | Monitor *mon, |
750 | f6b134ac | Mark McLoughlin | const char *name, |
751 | f6b134ac | Mark McLoughlin | VLANState *vlan) |
752 | f83c6e10 | Mark McLoughlin | { |
753 | f83c6e10 | Mark McLoughlin | int idx;
|
754 | f83c6e10 | Mark McLoughlin | NICInfo *nd; |
755 | 5869c4d5 | Mark McLoughlin | const char *netdev; |
756 | f83c6e10 | Mark McLoughlin | |
757 | f83c6e10 | Mark McLoughlin | idx = nic_get_free_idx(); |
758 | f83c6e10 | Mark McLoughlin | if (idx == -1 || nb_nics >= MAX_NICS) { |
759 | 1ecda02b | Markus Armbruster | error_report("Too Many NICs");
|
760 | f83c6e10 | Mark McLoughlin | return -1; |
761 | f83c6e10 | Mark McLoughlin | } |
762 | f83c6e10 | Mark McLoughlin | |
763 | f83c6e10 | Mark McLoughlin | nd = &nd_table[idx]; |
764 | f83c6e10 | Mark McLoughlin | |
765 | f83c6e10 | Mark McLoughlin | memset(nd, 0, sizeof(*nd)); |
766 | f83c6e10 | Mark McLoughlin | |
767 | 5869c4d5 | Mark McLoughlin | if ((netdev = qemu_opt_get(opts, "netdev"))) { |
768 | 5869c4d5 | Mark McLoughlin | nd->netdev = qemu_find_netdev(netdev); |
769 | 5869c4d5 | Mark McLoughlin | if (!nd->netdev) {
|
770 | 1ecda02b | Markus Armbruster | error_report("netdev '%s' not found", netdev);
|
771 | 5869c4d5 | Mark McLoughlin | return -1; |
772 | 5869c4d5 | Mark McLoughlin | } |
773 | 5869c4d5 | Mark McLoughlin | } else {
|
774 | 5869c4d5 | Mark McLoughlin | assert(vlan); |
775 | 5869c4d5 | Mark McLoughlin | nd->vlan = vlan; |
776 | 5869c4d5 | Mark McLoughlin | } |
777 | 6d952ebe | Mark McLoughlin | if (name) {
|
778 | 7267c094 | Anthony Liguori | nd->name = g_strdup(name); |
779 | 6d952ebe | Mark McLoughlin | } |
780 | f83c6e10 | Mark McLoughlin | if (qemu_opt_get(opts, "model")) { |
781 | 7267c094 | Anthony Liguori | nd->model = g_strdup(qemu_opt_get(opts, "model"));
|
782 | f83c6e10 | Mark McLoughlin | } |
783 | f83c6e10 | Mark McLoughlin | if (qemu_opt_get(opts, "addr")) { |
784 | 7267c094 | Anthony Liguori | nd->devaddr = g_strdup(qemu_opt_get(opts, "addr"));
|
785 | f83c6e10 | Mark McLoughlin | } |
786 | f83c6e10 | Mark McLoughlin | |
787 | f83c6e10 | Mark McLoughlin | if (qemu_opt_get(opts, "macaddr") && |
788 | 6eed1856 | Jan Kiszka | net_parse_macaddr(nd->macaddr.a, qemu_opt_get(opts, "macaddr")) < 0) { |
789 | 1ecda02b | Markus Armbruster | error_report("invalid syntax for ethernet address");
|
790 | f83c6e10 | Mark McLoughlin | return -1; |
791 | f83c6e10 | Mark McLoughlin | } |
792 | 6eed1856 | Jan Kiszka | qemu_macaddr_default_if_unset(&nd->macaddr); |
793 | f83c6e10 | Mark McLoughlin | |
794 | 75422b0d | Amit Shah | nd->nvectors = qemu_opt_get_number(opts, "vectors",
|
795 | 75422b0d | Amit Shah | DEV_NVECTORS_UNSPECIFIED); |
796 | 75422b0d | Amit Shah | if (nd->nvectors != DEV_NVECTORS_UNSPECIFIED &&
|
797 | f83c6e10 | Mark McLoughlin | (nd->nvectors < 0 || nd->nvectors > 0x7ffffff)) { |
798 | 1ecda02b | Markus Armbruster | error_report("invalid # of vectors: %d", nd->nvectors);
|
799 | f83c6e10 | Mark McLoughlin | return -1; |
800 | f83c6e10 | Mark McLoughlin | } |
801 | f83c6e10 | Mark McLoughlin | |
802 | f83c6e10 | Mark McLoughlin | nd->used = 1;
|
803 | f83c6e10 | Mark McLoughlin | nb_nics++; |
804 | f83c6e10 | Mark McLoughlin | |
805 | f83c6e10 | Mark McLoughlin | return idx;
|
806 | f83c6e10 | Mark McLoughlin | } |
807 | f83c6e10 | Mark McLoughlin | |
808 | f83c6e10 | Mark McLoughlin | #define NET_COMMON_PARAMS_DESC \
|
809 | f83c6e10 | Mark McLoughlin | { \ |
810 | f83c6e10 | Mark McLoughlin | .name = "type", \
|
811 | f83c6e10 | Mark McLoughlin | .type = QEMU_OPT_STRING, \ |
812 | f83c6e10 | Mark McLoughlin | .help = "net client type (nic, tap etc.)", \
|
813 | f83c6e10 | Mark McLoughlin | }, { \ |
814 | f83c6e10 | Mark McLoughlin | .name = "vlan", \
|
815 | f83c6e10 | Mark McLoughlin | .type = QEMU_OPT_NUMBER, \ |
816 | f83c6e10 | Mark McLoughlin | .help = "vlan number", \
|
817 | f83c6e10 | Mark McLoughlin | }, { \ |
818 | f83c6e10 | Mark McLoughlin | .name = "name", \
|
819 | f83c6e10 | Mark McLoughlin | .type = QEMU_OPT_STRING, \ |
820 | f83c6e10 | Mark McLoughlin | .help = "identifier for monitor commands", \
|
821 | f83c6e10 | Mark McLoughlin | } |
822 | f83c6e10 | Mark McLoughlin | |
823 | 6d952ebe | Mark McLoughlin | typedef int (*net_client_init_func)(QemuOpts *opts, |
824 | 6d952ebe | Mark McLoughlin | Monitor *mon, |
825 | f6b134ac | Mark McLoughlin | const char *name, |
826 | f6b134ac | Mark McLoughlin | VLANState *vlan); |
827 | f83c6e10 | Mark McLoughlin | |
828 | f83c6e10 | Mark McLoughlin | /* magic number, but compiler will warn if too small */
|
829 | f83c6e10 | Mark McLoughlin | #define NET_MAX_DESC 20 |
830 | f83c6e10 | Mark McLoughlin | |
831 | 238431a9 | Blue Swirl | static const struct { |
832 | f83c6e10 | Mark McLoughlin | const char *type; |
833 | f83c6e10 | Mark McLoughlin | net_client_init_func init; |
834 | f83c6e10 | Mark McLoughlin | QemuOptDesc desc[NET_MAX_DESC]; |
835 | 6f7b3b1b | Jan Kiszka | } net_client_types[NET_CLIENT_TYPE_MAX] = { |
836 | 6f7b3b1b | Jan Kiszka | [NET_CLIENT_TYPE_NONE] = { |
837 | f83c6e10 | Mark McLoughlin | .type = "none",
|
838 | f83c6e10 | Mark McLoughlin | .desc = { |
839 | f83c6e10 | Mark McLoughlin | NET_COMMON_PARAMS_DESC, |
840 | f83c6e10 | Mark McLoughlin | { /* end of list */ }
|
841 | f83c6e10 | Mark McLoughlin | }, |
842 | 6f7b3b1b | Jan Kiszka | }, |
843 | 6f7b3b1b | Jan Kiszka | [NET_CLIENT_TYPE_NIC] = { |
844 | f83c6e10 | Mark McLoughlin | .type = "nic",
|
845 | f83c6e10 | Mark McLoughlin | .init = net_init_nic, |
846 | f83c6e10 | Mark McLoughlin | .desc = { |
847 | f83c6e10 | Mark McLoughlin | NET_COMMON_PARAMS_DESC, |
848 | f83c6e10 | Mark McLoughlin | { |
849 | 5869c4d5 | Mark McLoughlin | .name = "netdev",
|
850 | 5869c4d5 | Mark McLoughlin | .type = QEMU_OPT_STRING, |
851 | 5869c4d5 | Mark McLoughlin | .help = "id of -netdev to connect to",
|
852 | 5869c4d5 | Mark McLoughlin | }, |
853 | 5869c4d5 | Mark McLoughlin | { |
854 | f83c6e10 | Mark McLoughlin | .name = "macaddr",
|
855 | f83c6e10 | Mark McLoughlin | .type = QEMU_OPT_STRING, |
856 | f83c6e10 | Mark McLoughlin | .help = "MAC address",
|
857 | f83c6e10 | Mark McLoughlin | }, { |
858 | f83c6e10 | Mark McLoughlin | .name = "model",
|
859 | f83c6e10 | Mark McLoughlin | .type = QEMU_OPT_STRING, |
860 | f83c6e10 | Mark McLoughlin | .help = "device model (e1000, rtl8139, virtio etc.)",
|
861 | f83c6e10 | Mark McLoughlin | }, { |
862 | f83c6e10 | Mark McLoughlin | .name = "addr",
|
863 | f83c6e10 | Mark McLoughlin | .type = QEMU_OPT_STRING, |
864 | f83c6e10 | Mark McLoughlin | .help = "PCI device address",
|
865 | f83c6e10 | Mark McLoughlin | }, { |
866 | f83c6e10 | Mark McLoughlin | .name = "vectors",
|
867 | f83c6e10 | Mark McLoughlin | .type = QEMU_OPT_NUMBER, |
868 | f83c6e10 | Mark McLoughlin | .help = "number of MSI-x vectors, 0 to disable MSI-X",
|
869 | f83c6e10 | Mark McLoughlin | }, |
870 | f83c6e10 | Mark McLoughlin | { /* end of list */ }
|
871 | f83c6e10 | Mark McLoughlin | }, |
872 | 6f7b3b1b | Jan Kiszka | }, |
873 | ec302ffd | Mark McLoughlin | #ifdef CONFIG_SLIRP
|
874 | 6f7b3b1b | Jan Kiszka | [NET_CLIENT_TYPE_USER] = { |
875 | ec302ffd | Mark McLoughlin | .type = "user",
|
876 | ec302ffd | Mark McLoughlin | .init = net_init_slirp, |
877 | ec302ffd | Mark McLoughlin | .desc = { |
878 | ec302ffd | Mark McLoughlin | NET_COMMON_PARAMS_DESC, |
879 | ec302ffd | Mark McLoughlin | { |
880 | ec302ffd | Mark McLoughlin | .name = "hostname",
|
881 | ec302ffd | Mark McLoughlin | .type = QEMU_OPT_STRING, |
882 | ec302ffd | Mark McLoughlin | .help = "client hostname reported by the builtin DHCP server",
|
883 | ec302ffd | Mark McLoughlin | }, { |
884 | ec302ffd | Mark McLoughlin | .name = "restrict",
|
885 | ec302ffd | Mark McLoughlin | .type = QEMU_OPT_STRING, |
886 | ec302ffd | Mark McLoughlin | .help = "isolate the guest from the host (y|yes|n|no)",
|
887 | ec302ffd | Mark McLoughlin | }, { |
888 | ec302ffd | Mark McLoughlin | .name = "ip",
|
889 | ec302ffd | Mark McLoughlin | .type = QEMU_OPT_STRING, |
890 | ec302ffd | Mark McLoughlin | .help = "legacy parameter, use net= instead",
|
891 | ec302ffd | Mark McLoughlin | }, { |
892 | ec302ffd | Mark McLoughlin | .name = "net",
|
893 | ec302ffd | Mark McLoughlin | .type = QEMU_OPT_STRING, |
894 | ec302ffd | Mark McLoughlin | .help = "IP address and optional netmask",
|
895 | ec302ffd | Mark McLoughlin | }, { |
896 | ec302ffd | Mark McLoughlin | .name = "host",
|
897 | ec302ffd | Mark McLoughlin | .type = QEMU_OPT_STRING, |
898 | ec302ffd | Mark McLoughlin | .help = "guest-visible address of the host",
|
899 | ec302ffd | Mark McLoughlin | }, { |
900 | ec302ffd | Mark McLoughlin | .name = "tftp",
|
901 | ec302ffd | Mark McLoughlin | .type = QEMU_OPT_STRING, |
902 | ec302ffd | Mark McLoughlin | .help = "root directory of the built-in TFTP server",
|
903 | ec302ffd | Mark McLoughlin | }, { |
904 | ec302ffd | Mark McLoughlin | .name = "bootfile",
|
905 | ec302ffd | Mark McLoughlin | .type = QEMU_OPT_STRING, |
906 | ec302ffd | Mark McLoughlin | .help = "BOOTP filename, for use with tftp=",
|
907 | ec302ffd | Mark McLoughlin | }, { |
908 | ec302ffd | Mark McLoughlin | .name = "dhcpstart",
|
909 | ec302ffd | Mark McLoughlin | .type = QEMU_OPT_STRING, |
910 | ec302ffd | Mark McLoughlin | .help = "the first of the 16 IPs the built-in DHCP server can assign",
|
911 | ec302ffd | Mark McLoughlin | }, { |
912 | ec302ffd | Mark McLoughlin | .name = "dns",
|
913 | ec302ffd | Mark McLoughlin | .type = QEMU_OPT_STRING, |
914 | ec302ffd | Mark McLoughlin | .help = "guest-visible address of the virtual nameserver",
|
915 | ec302ffd | Mark McLoughlin | }, { |
916 | ec302ffd | Mark McLoughlin | .name = "smb",
|
917 | ec302ffd | Mark McLoughlin | .type = QEMU_OPT_STRING, |
918 | ec302ffd | Mark McLoughlin | .help = "root directory of the built-in SMB server",
|
919 | ec302ffd | Mark McLoughlin | }, { |
920 | ec302ffd | Mark McLoughlin | .name = "smbserver",
|
921 | ec302ffd | Mark McLoughlin | .type = QEMU_OPT_STRING, |
922 | ec302ffd | Mark McLoughlin | .help = "IP address of the built-in SMB server",
|
923 | ec302ffd | Mark McLoughlin | }, { |
924 | ec302ffd | Mark McLoughlin | .name = "hostfwd",
|
925 | ec302ffd | Mark McLoughlin | .type = QEMU_OPT_STRING, |
926 | ec302ffd | Mark McLoughlin | .help = "guest port number to forward incoming TCP or UDP connections",
|
927 | ec302ffd | Mark McLoughlin | }, { |
928 | ec302ffd | Mark McLoughlin | .name = "guestfwd",
|
929 | ec302ffd | Mark McLoughlin | .type = QEMU_OPT_STRING, |
930 | ec302ffd | Mark McLoughlin | .help = "IP address and port to forward guest TCP connections",
|
931 | ec302ffd | Mark McLoughlin | }, |
932 | ec302ffd | Mark McLoughlin | { /* end of list */ }
|
933 | ec302ffd | Mark McLoughlin | }, |
934 | 6f7b3b1b | Jan Kiszka | }, |
935 | ec302ffd | Mark McLoughlin | #endif
|
936 | 6f7b3b1b | Jan Kiszka | [NET_CLIENT_TYPE_TAP] = { |
937 | 8a1c5235 | Mark McLoughlin | .type = "tap",
|
938 | a8ed73f7 | Mark McLoughlin | .init = net_init_tap, |
939 | 8a1c5235 | Mark McLoughlin | .desc = { |
940 | 8a1c5235 | Mark McLoughlin | NET_COMMON_PARAMS_DESC, |
941 | 8a1c5235 | Mark McLoughlin | { |
942 | 8a1c5235 | Mark McLoughlin | .name = "ifname",
|
943 | 8a1c5235 | Mark McLoughlin | .type = QEMU_OPT_STRING, |
944 | 8a1c5235 | Mark McLoughlin | .help = "interface name",
|
945 | 8a1c5235 | Mark McLoughlin | }, |
946 | a8ed73f7 | Mark McLoughlin | #ifndef _WIN32
|
947 | 8a1c5235 | Mark McLoughlin | { |
948 | 8a1c5235 | Mark McLoughlin | .name = "fd",
|
949 | 8a1c5235 | Mark McLoughlin | .type = QEMU_OPT_STRING, |
950 | 8a1c5235 | Mark McLoughlin | .help = "file descriptor of an already opened tap",
|
951 | 8a1c5235 | Mark McLoughlin | }, { |
952 | 8a1c5235 | Mark McLoughlin | .name = "script",
|
953 | 8a1c5235 | Mark McLoughlin | .type = QEMU_OPT_STRING, |
954 | 8a1c5235 | Mark McLoughlin | .help = "script to initialize the interface",
|
955 | 8a1c5235 | Mark McLoughlin | }, { |
956 | 8a1c5235 | Mark McLoughlin | .name = "downscript",
|
957 | 8a1c5235 | Mark McLoughlin | .type = QEMU_OPT_STRING, |
958 | 8a1c5235 | Mark McLoughlin | .help = "script to shut down the interface",
|
959 | 8a1c5235 | Mark McLoughlin | }, { |
960 | 2944e4ec | Stefan Weil | #ifdef CONFIG_NET_BRIDGE
|
961 | a7c36ee4 | Corey Bryant | .name = "helper",
|
962 | a7c36ee4 | Corey Bryant | .type = QEMU_OPT_STRING, |
963 | a7c36ee4 | Corey Bryant | .help = "command to execute to configure bridge",
|
964 | a7c36ee4 | Corey Bryant | }, { |
965 | 2944e4ec | Stefan Weil | #endif
|
966 | 8a1c5235 | Mark McLoughlin | .name = "sndbuf",
|
967 | 8a1c5235 | Mark McLoughlin | .type = QEMU_OPT_SIZE, |
968 | 8a1c5235 | Mark McLoughlin | .help = "send buffer limit"
|
969 | baf74c95 | Mark McLoughlin | }, { |
970 | baf74c95 | Mark McLoughlin | .name = "vnet_hdr",
|
971 | baf74c95 | Mark McLoughlin | .type = QEMU_OPT_BOOL, |
972 | baf74c95 | Mark McLoughlin | .help = "enable the IFF_VNET_HDR flag on the tap interface"
|
973 | 82b0d80e | Michael S. Tsirkin | }, { |
974 | 82b0d80e | Michael S. Tsirkin | .name = "vhost",
|
975 | 82b0d80e | Michael S. Tsirkin | .type = QEMU_OPT_BOOL, |
976 | 82b0d80e | Michael S. Tsirkin | .help = "enable vhost-net network accelerator",
|
977 | 82b0d80e | Michael S. Tsirkin | }, { |
978 | 82b0d80e | Michael S. Tsirkin | .name = "vhostfd",
|
979 | 82b0d80e | Michael S. Tsirkin | .type = QEMU_OPT_STRING, |
980 | 82b0d80e | Michael S. Tsirkin | .help = "file descriptor of an already opened vhost net device",
|
981 | 96c94b29 | Jason Wang | }, { |
982 | 96c94b29 | Jason Wang | .name = "vhostforce",
|
983 | 96c94b29 | Jason Wang | .type = QEMU_OPT_BOOL, |
984 | 96c94b29 | Jason Wang | .help = "force vhost on for non-MSIX virtio guests",
|
985 | 96c94b29 | Jason Wang | }, |
986 | a8ed73f7 | Mark McLoughlin | #endif /* _WIN32 */ |
987 | 8a1c5235 | Mark McLoughlin | { /* end of list */ }
|
988 | 8a1c5235 | Mark McLoughlin | }, |
989 | 6f7b3b1b | Jan Kiszka | }, |
990 | 6f7b3b1b | Jan Kiszka | [NET_CLIENT_TYPE_SOCKET] = { |
991 | 88ce16ca | Mark McLoughlin | .type = "socket",
|
992 | 88ce16ca | Mark McLoughlin | .init = net_init_socket, |
993 | 88ce16ca | Mark McLoughlin | .desc = { |
994 | 88ce16ca | Mark McLoughlin | NET_COMMON_PARAMS_DESC, |
995 | 88ce16ca | Mark McLoughlin | { |
996 | 88ce16ca | Mark McLoughlin | .name = "fd",
|
997 | 88ce16ca | Mark McLoughlin | .type = QEMU_OPT_STRING, |
998 | 88ce16ca | Mark McLoughlin | .help = "file descriptor of an already opened socket",
|
999 | 88ce16ca | Mark McLoughlin | }, { |
1000 | 88ce16ca | Mark McLoughlin | .name = "listen",
|
1001 | 88ce16ca | Mark McLoughlin | .type = QEMU_OPT_STRING, |
1002 | 88ce16ca | Mark McLoughlin | .help = "port number, and optional hostname, to listen on",
|
1003 | 88ce16ca | Mark McLoughlin | }, { |
1004 | 88ce16ca | Mark McLoughlin | .name = "connect",
|
1005 | 88ce16ca | Mark McLoughlin | .type = QEMU_OPT_STRING, |
1006 | 88ce16ca | Mark McLoughlin | .help = "port number, and optional hostname, to connect to",
|
1007 | 88ce16ca | Mark McLoughlin | }, { |
1008 | 88ce16ca | Mark McLoughlin | .name = "mcast",
|
1009 | 88ce16ca | Mark McLoughlin | .type = QEMU_OPT_STRING, |
1010 | 88ce16ca | Mark McLoughlin | .help = "UDP multicast address and port number",
|
1011 | 3a75e74c | Mike Ryan | }, { |
1012 | 3a75e74c | Mike Ryan | .name = "localaddr",
|
1013 | 3a75e74c | Mike Ryan | .type = QEMU_OPT_STRING, |
1014 | 0e0e7fac | Benjamin | .help = "source address and port for multicast and udp packets",
|
1015 | 0e0e7fac | Benjamin | }, { |
1016 | 0e0e7fac | Benjamin | .name = "udp",
|
1017 | 0e0e7fac | Benjamin | .type = QEMU_OPT_STRING, |
1018 | 0e0e7fac | Benjamin | .help = "UDP unicast address and port number",
|
1019 | 88ce16ca | Mark McLoughlin | }, |
1020 | 88ce16ca | Mark McLoughlin | { /* end of list */ }
|
1021 | 88ce16ca | Mark McLoughlin | }, |
1022 | 6f7b3b1b | Jan Kiszka | }, |
1023 | dd51058d | Mark McLoughlin | #ifdef CONFIG_VDE
|
1024 | 6f7b3b1b | Jan Kiszka | [NET_CLIENT_TYPE_VDE] = { |
1025 | dd51058d | Mark McLoughlin | .type = "vde",
|
1026 | dd51058d | Mark McLoughlin | .init = net_init_vde, |
1027 | dd51058d | Mark McLoughlin | .desc = { |
1028 | dd51058d | Mark McLoughlin | NET_COMMON_PARAMS_DESC, |
1029 | dd51058d | Mark McLoughlin | { |
1030 | dd51058d | Mark McLoughlin | .name = "sock",
|
1031 | dd51058d | Mark McLoughlin | .type = QEMU_OPT_STRING, |
1032 | dd51058d | Mark McLoughlin | .help = "socket path",
|
1033 | dd51058d | Mark McLoughlin | }, { |
1034 | dd51058d | Mark McLoughlin | .name = "port",
|
1035 | dd51058d | Mark McLoughlin | .type = QEMU_OPT_NUMBER, |
1036 | dd51058d | Mark McLoughlin | .help = "port number",
|
1037 | dd51058d | Mark McLoughlin | }, { |
1038 | dd51058d | Mark McLoughlin | .name = "group",
|
1039 | dd51058d | Mark McLoughlin | .type = QEMU_OPT_STRING, |
1040 | dd51058d | Mark McLoughlin | .help = "group owner of socket",
|
1041 | dd51058d | Mark McLoughlin | }, { |
1042 | dd51058d | Mark McLoughlin | .name = "mode",
|
1043 | dd51058d | Mark McLoughlin | .type = QEMU_OPT_NUMBER, |
1044 | dd51058d | Mark McLoughlin | .help = "permissions for socket",
|
1045 | dd51058d | Mark McLoughlin | }, |
1046 | dd51058d | Mark McLoughlin | { /* end of list */ }
|
1047 | dd51058d | Mark McLoughlin | }, |
1048 | 6f7b3b1b | Jan Kiszka | }, |
1049 | dd51058d | Mark McLoughlin | #endif
|
1050 | 6f7b3b1b | Jan Kiszka | [NET_CLIENT_TYPE_DUMP] = { |
1051 | ed2955c2 | Mark McLoughlin | .type = "dump",
|
1052 | ed2955c2 | Mark McLoughlin | .init = net_init_dump, |
1053 | ed2955c2 | Mark McLoughlin | .desc = { |
1054 | ed2955c2 | Mark McLoughlin | NET_COMMON_PARAMS_DESC, |
1055 | ed2955c2 | Mark McLoughlin | { |
1056 | ed2955c2 | Mark McLoughlin | .name = "len",
|
1057 | ed2955c2 | Mark McLoughlin | .type = QEMU_OPT_SIZE, |
1058 | ed2955c2 | Mark McLoughlin | .help = "per-packet size limit (64k default)",
|
1059 | ed2955c2 | Mark McLoughlin | }, { |
1060 | ed2955c2 | Mark McLoughlin | .name = "file",
|
1061 | ed2955c2 | Mark McLoughlin | .type = QEMU_OPT_STRING, |
1062 | ed2955c2 | Mark McLoughlin | .help = "dump file path (default is qemu-vlan0.pcap)",
|
1063 | ed2955c2 | Mark McLoughlin | }, |
1064 | ed2955c2 | Mark McLoughlin | { /* end of list */ }
|
1065 | ed2955c2 | Mark McLoughlin | }, |
1066 | f83c6e10 | Mark McLoughlin | }, |
1067 | 2944e4ec | Stefan Weil | #ifdef CONFIG_NET_BRIDGE
|
1068 | a7c36ee4 | Corey Bryant | [NET_CLIENT_TYPE_BRIDGE] = { |
1069 | a7c36ee4 | Corey Bryant | .type = "bridge",
|
1070 | a7c36ee4 | Corey Bryant | .init = net_init_bridge, |
1071 | a7c36ee4 | Corey Bryant | .desc = { |
1072 | a7c36ee4 | Corey Bryant | NET_COMMON_PARAMS_DESC, |
1073 | a7c36ee4 | Corey Bryant | { |
1074 | a7c36ee4 | Corey Bryant | .name = "br",
|
1075 | a7c36ee4 | Corey Bryant | .type = QEMU_OPT_STRING, |
1076 | a7c36ee4 | Corey Bryant | .help = "bridge name",
|
1077 | a7c36ee4 | Corey Bryant | }, { |
1078 | a7c36ee4 | Corey Bryant | .name = "helper",
|
1079 | a7c36ee4 | Corey Bryant | .type = QEMU_OPT_STRING, |
1080 | a7c36ee4 | Corey Bryant | .help = "command to execute to configure bridge",
|
1081 | a7c36ee4 | Corey Bryant | }, |
1082 | a7c36ee4 | Corey Bryant | { /* end of list */ }
|
1083 | a7c36ee4 | Corey Bryant | }, |
1084 | a7c36ee4 | Corey Bryant | }, |
1085 | 2944e4ec | Stefan Weil | #endif /* CONFIG_NET_BRIDGE */ |
1086 | f83c6e10 | Mark McLoughlin | }; |
1087 | f83c6e10 | Mark McLoughlin | |
1088 | f6b134ac | Mark McLoughlin | int net_client_init(Monitor *mon, QemuOpts *opts, int is_netdev) |
1089 | f83c6e10 | Mark McLoughlin | { |
1090 | 6d952ebe | Mark McLoughlin | const char *name; |
1091 | f83c6e10 | Mark McLoughlin | const char *type; |
1092 | f83c6e10 | Mark McLoughlin | int i;
|
1093 | f83c6e10 | Mark McLoughlin | |
1094 | f83c6e10 | Mark McLoughlin | type = qemu_opt_get(opts, "type");
|
1095 | 5294e2c7 | Markus Armbruster | if (!type) {
|
1096 | 5294e2c7 | Markus Armbruster | qerror_report(QERR_MISSING_PARAMETER, "type");
|
1097 | 5294e2c7 | Markus Armbruster | return -1; |
1098 | 5294e2c7 | Markus Armbruster | } |
1099 | f83c6e10 | Mark McLoughlin | |
1100 | 5294e2c7 | Markus Armbruster | if (is_netdev) {
|
1101 | f6b134ac | Mark McLoughlin | if (strcmp(type, "tap") != 0 && |
1102 | 2944e4ec | Stefan Weil | #ifdef CONFIG_NET_BRIDGE
|
1103 | 2944e4ec | Stefan Weil | strcmp(type, "bridge") != 0 && |
1104 | 2944e4ec | Stefan Weil | #endif
|
1105 | f6b134ac | Mark McLoughlin | #ifdef CONFIG_SLIRP
|
1106 | f6b134ac | Mark McLoughlin | strcmp(type, "user") != 0 && |
1107 | f6b134ac | Mark McLoughlin | #endif
|
1108 | f6b134ac | Mark McLoughlin | #ifdef CONFIG_VDE
|
1109 | f6b134ac | Mark McLoughlin | strcmp(type, "vde") != 0 && |
1110 | f6b134ac | Mark McLoughlin | #endif
|
1111 | 2944e4ec | Stefan Weil | strcmp(type, "socket") != 0) { |
1112 | 5294e2c7 | Markus Armbruster | qerror_report(QERR_INVALID_PARAMETER_VALUE, "type",
|
1113 | 5294e2c7 | Markus Armbruster | "a netdev backend type");
|
1114 | f6b134ac | Mark McLoughlin | return -1; |
1115 | f6b134ac | Mark McLoughlin | } |
1116 | f6b134ac | Mark McLoughlin | |
1117 | f6b134ac | Mark McLoughlin | if (qemu_opt_get(opts, "vlan")) { |
1118 | 5294e2c7 | Markus Armbruster | qerror_report(QERR_INVALID_PARAMETER, "vlan");
|
1119 | f6b134ac | Mark McLoughlin | return -1; |
1120 | f6b134ac | Mark McLoughlin | } |
1121 | f6b134ac | Mark McLoughlin | if (qemu_opt_get(opts, "name")) { |
1122 | 5294e2c7 | Markus Armbruster | qerror_report(QERR_INVALID_PARAMETER, "name");
|
1123 | f6b134ac | Mark McLoughlin | return -1; |
1124 | f6b134ac | Mark McLoughlin | } |
1125 | f6b134ac | Mark McLoughlin | if (!qemu_opts_id(opts)) {
|
1126 | 5294e2c7 | Markus Armbruster | qerror_report(QERR_MISSING_PARAMETER, "id");
|
1127 | f6b134ac | Mark McLoughlin | return -1; |
1128 | f6b134ac | Mark McLoughlin | } |
1129 | f6b134ac | Mark McLoughlin | } |
1130 | f6b134ac | Mark McLoughlin | |
1131 | 6d952ebe | Mark McLoughlin | name = qemu_opts_id(opts); |
1132 | 6d952ebe | Mark McLoughlin | if (!name) {
|
1133 | 6d952ebe | Mark McLoughlin | name = qemu_opt_get(opts, "name");
|
1134 | 6d952ebe | Mark McLoughlin | } |
1135 | 6d952ebe | Mark McLoughlin | |
1136 | 6f7b3b1b | Jan Kiszka | for (i = 0; i < NET_CLIENT_TYPE_MAX; i++) { |
1137 | 6f7b3b1b | Jan Kiszka | if (net_client_types[i].type != NULL && |
1138 | 6f7b3b1b | Jan Kiszka | !strcmp(net_client_types[i].type, type)) { |
1139 | f6b134ac | Mark McLoughlin | VLANState *vlan = NULL;
|
1140 | 50e32ea8 | Amit Shah | int ret;
|
1141 | f6b134ac | Mark McLoughlin | |
1142 | f83c6e10 | Mark McLoughlin | if (qemu_opts_validate(opts, &net_client_types[i].desc[0]) == -1) { |
1143 | f83c6e10 | Mark McLoughlin | return -1; |
1144 | f83c6e10 | Mark McLoughlin | } |
1145 | f83c6e10 | Mark McLoughlin | |
1146 | 5869c4d5 | Mark McLoughlin | /* Do not add to a vlan if it's a -netdev or a nic with a
|
1147 | 5869c4d5 | Mark McLoughlin | * netdev= parameter. */
|
1148 | 5869c4d5 | Mark McLoughlin | if (!(is_netdev ||
|
1149 | 5869c4d5 | Mark McLoughlin | (strcmp(type, "nic") == 0 && qemu_opt_get(opts, "netdev")))) { |
1150 | f6b134ac | Mark McLoughlin | vlan = qemu_find_vlan(qemu_opt_get_number(opts, "vlan", 0), 1); |
1151 | f6b134ac | Mark McLoughlin | } |
1152 | f6b134ac | Mark McLoughlin | |
1153 | 03c71553 | Amit Shah | ret = 0;
|
1154 | f83c6e10 | Mark McLoughlin | if (net_client_types[i].init) {
|
1155 | 50e32ea8 | Amit Shah | ret = net_client_types[i].init(opts, mon, name, vlan); |
1156 | 50e32ea8 | Amit Shah | if (ret < 0) { |
1157 | 5294e2c7 | Markus Armbruster | /* TODO push error reporting into init() methods */
|
1158 | 5294e2c7 | Markus Armbruster | qerror_report(QERR_DEVICE_INIT_FAILED, type); |
1159 | 5294e2c7 | Markus Armbruster | return -1; |
1160 | 5294e2c7 | Markus Armbruster | } |
1161 | f83c6e10 | Mark McLoughlin | } |
1162 | 50e32ea8 | Amit Shah | return ret;
|
1163 | f83c6e10 | Mark McLoughlin | } |
1164 | f83c6e10 | Mark McLoughlin | } |
1165 | f83c6e10 | Mark McLoughlin | |
1166 | 5294e2c7 | Markus Armbruster | qerror_report(QERR_INVALID_PARAMETER_VALUE, "type",
|
1167 | 5294e2c7 | Markus Armbruster | "a network client type");
|
1168 | f83c6e10 | Mark McLoughlin | return -1; |
1169 | f83c6e10 | Mark McLoughlin | } |
1170 | f83c6e10 | Mark McLoughlin | |
1171 | 6f338c34 | aliguori | static int net_host_check_device(const char *device) |
1172 | 6f338c34 | aliguori | { |
1173 | 6f338c34 | aliguori | int i;
|
1174 | bb9ea79e | aliguori | const char *valid_param_list[] = { "tap", "socket", "dump" |
1175 | 2944e4ec | Stefan Weil | #ifdef CONFIG_NET_BRIDGE
|
1176 | 2944e4ec | Stefan Weil | , "bridge"
|
1177 | 2944e4ec | Stefan Weil | #endif
|
1178 | 6f338c34 | aliguori | #ifdef CONFIG_SLIRP
|
1179 | 6f338c34 | aliguori | ,"user"
|
1180 | 6f338c34 | aliguori | #endif
|
1181 | 6f338c34 | aliguori | #ifdef CONFIG_VDE
|
1182 | 6f338c34 | aliguori | ,"vde"
|
1183 | 6f338c34 | aliguori | #endif
|
1184 | 6f338c34 | aliguori | }; |
1185 | 6f338c34 | aliguori | for (i = 0; i < sizeof(valid_param_list) / sizeof(char *); i++) { |
1186 | 6f338c34 | aliguori | if (!strncmp(valid_param_list[i], device,
|
1187 | 6f338c34 | aliguori | strlen(valid_param_list[i]))) |
1188 | 6f338c34 | aliguori | return 1; |
1189 | 6f338c34 | aliguori | } |
1190 | 6f338c34 | aliguori | |
1191 | 6f338c34 | aliguori | return 0; |
1192 | 6f338c34 | aliguori | } |
1193 | 6f338c34 | aliguori | |
1194 | f18c16de | Luiz Capitulino | void net_host_device_add(Monitor *mon, const QDict *qdict) |
1195 | 6f338c34 | aliguori | { |
1196 | f18c16de | Luiz Capitulino | const char *device = qdict_get_str(qdict, "device"); |
1197 | 7f1c9d20 | Mark McLoughlin | const char *opts_str = qdict_get_try_str(qdict, "opts"); |
1198 | 7f1c9d20 | Mark McLoughlin | QemuOpts *opts; |
1199 | f18c16de | Luiz Capitulino | |
1200 | 6f338c34 | aliguori | if (!net_host_check_device(device)) {
|
1201 | 376253ec | aliguori | monitor_printf(mon, "invalid host network device %s\n", device);
|
1202 | 6f338c34 | aliguori | return;
|
1203 | 6f338c34 | aliguori | } |
1204 | 7f1c9d20 | Mark McLoughlin | |
1205 | 3329f07b | Gerd Hoffmann | opts = qemu_opts_parse(qemu_find_opts("net"), opts_str ? opts_str : "", 0); |
1206 | 7f1c9d20 | Mark McLoughlin | if (!opts) {
|
1207 | 7f1c9d20 | Mark McLoughlin | return;
|
1208 | 7f1c9d20 | Mark McLoughlin | } |
1209 | 7f1c9d20 | Mark McLoughlin | |
1210 | 7f1c9d20 | Mark McLoughlin | qemu_opt_set(opts, "type", device);
|
1211 | 7f1c9d20 | Mark McLoughlin | |
1212 | f6b134ac | Mark McLoughlin | if (net_client_init(mon, opts, 0) < 0) { |
1213 | 5c8be678 | aliguori | monitor_printf(mon, "adding host network device %s failed\n", device);
|
1214 | 5c8be678 | aliguori | } |
1215 | 6f338c34 | aliguori | } |
1216 | 6f338c34 | aliguori | |
1217 | f18c16de | Luiz Capitulino | void net_host_device_remove(Monitor *mon, const QDict *qdict) |
1218 | 6f338c34 | aliguori | { |
1219 | 6f338c34 | aliguori | VLANClientState *vc; |
1220 | f18c16de | Luiz Capitulino | int vlan_id = qdict_get_int(qdict, "vlan_id"); |
1221 | f18c16de | Luiz Capitulino | const char *device = qdict_get_str(qdict, "device"); |
1222 | 6f338c34 | aliguori | |
1223 | 1a609520 | Jan Kiszka | vc = qemu_find_vlan_client_by_name(mon, vlan_id, device); |
1224 | 6f338c34 | aliguori | if (!vc) {
|
1225 | 6f338c34 | aliguori | return;
|
1226 | 6f338c34 | aliguori | } |
1227 | e8f1f9db | aliguori | if (!net_host_check_device(vc->model)) {
|
1228 | e8f1f9db | aliguori | monitor_printf(mon, "invalid host network device %s\n", device);
|
1229 | e8f1f9db | aliguori | return;
|
1230 | e8f1f9db | aliguori | } |
1231 | 6f338c34 | aliguori | qemu_del_vlan_client(vc); |
1232 | 6f338c34 | aliguori | } |
1233 | 6f338c34 | aliguori | |
1234 | ae82d324 | Markus Armbruster | int do_netdev_add(Monitor *mon, const QDict *qdict, QObject **ret_data) |
1235 | ae82d324 | Markus Armbruster | { |
1236 | ae82d324 | Markus Armbruster | QemuOpts *opts; |
1237 | ae82d324 | Markus Armbruster | int res;
|
1238 | ae82d324 | Markus Armbruster | |
1239 | 3329f07b | Gerd Hoffmann | opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict);
|
1240 | ae82d324 | Markus Armbruster | if (!opts) {
|
1241 | ae82d324 | Markus Armbruster | return -1; |
1242 | ae82d324 | Markus Armbruster | } |
1243 | ae82d324 | Markus Armbruster | |
1244 | ae82d324 | Markus Armbruster | res = net_client_init(mon, opts, 1);
|
1245 | 410cbafe | Yoshiaki Tamura | if (res < 0) { |
1246 | 410cbafe | Yoshiaki Tamura | qemu_opts_del(opts); |
1247 | 410cbafe | Yoshiaki Tamura | } |
1248 | 410cbafe | Yoshiaki Tamura | |
1249 | ae82d324 | Markus Armbruster | return res;
|
1250 | ae82d324 | Markus Armbruster | } |
1251 | ae82d324 | Markus Armbruster | |
1252 | ae82d324 | Markus Armbruster | int do_netdev_del(Monitor *mon, const QDict *qdict, QObject **ret_data) |
1253 | ae82d324 | Markus Armbruster | { |
1254 | ae82d324 | Markus Armbruster | const char *id = qdict_get_str(qdict, "id"); |
1255 | ae82d324 | Markus Armbruster | VLANClientState *vc; |
1256 | ae82d324 | Markus Armbruster | |
1257 | ae82d324 | Markus Armbruster | vc = qemu_find_netdev(id); |
1258 | 85dde9a9 | Markus Armbruster | if (!vc) {
|
1259 | ae82d324 | Markus Armbruster | qerror_report(QERR_DEVICE_NOT_FOUND, id); |
1260 | ae82d324 | Markus Armbruster | return -1; |
1261 | ae82d324 | Markus Armbruster | } |
1262 | ae82d324 | Markus Armbruster | qemu_del_vlan_client(vc); |
1263 | 3329f07b | Gerd Hoffmann | qemu_opts_del(qemu_opts_find(qemu_find_opts("netdev"), id));
|
1264 | ae82d324 | Markus Armbruster | return 0; |
1265 | ae82d324 | Markus Armbruster | } |
1266 | ae82d324 | Markus Armbruster | |
1267 | 44e798d3 | Jan Kiszka | static void print_net_client(Monitor *mon, VLANClientState *vc) |
1268 | 44e798d3 | Jan Kiszka | { |
1269 | 44e798d3 | Jan Kiszka | monitor_printf(mon, "%s: type=%s,%s\n", vc->name,
|
1270 | 44e798d3 | Jan Kiszka | net_client_types[vc->info->type].type, vc->info_str); |
1271 | 44e798d3 | Jan Kiszka | } |
1272 | 44e798d3 | Jan Kiszka | |
1273 | 376253ec | aliguori | void do_info_network(Monitor *mon)
|
1274 | 63a01ef8 | aliguori | { |
1275 | 63a01ef8 | aliguori | VLANState *vlan; |
1276 | 19061e63 | Jan Kiszka | VLANClientState *vc, *peer; |
1277 | 19061e63 | Jan Kiszka | net_client_type type; |
1278 | 63a01ef8 | aliguori | |
1279 | 5610c3aa | Mark McLoughlin | QTAILQ_FOREACH(vlan, &vlans, next) { |
1280 | 376253ec | aliguori | monitor_printf(mon, "VLAN %d devices:\n", vlan->id);
|
1281 | 5610c3aa | Mark McLoughlin | |
1282 | 5610c3aa | Mark McLoughlin | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
1283 | 44e798d3 | Jan Kiszka | monitor_printf(mon, " ");
|
1284 | 44e798d3 | Jan Kiszka | print_net_client(mon, vc); |
1285 | 5610c3aa | Mark McLoughlin | } |
1286 | 63a01ef8 | aliguori | } |
1287 | a0104e0e | Markus Armbruster | monitor_printf(mon, "Devices not on any VLAN:\n");
|
1288 | a0104e0e | Markus Armbruster | QTAILQ_FOREACH(vc, &non_vlan_clients, next) { |
1289 | 19061e63 | Jan Kiszka | peer = vc->peer; |
1290 | 19061e63 | Jan Kiszka | type = vc->info->type; |
1291 | 19061e63 | Jan Kiszka | if (!peer || type == NET_CLIENT_TYPE_NIC) {
|
1292 | 44e798d3 | Jan Kiszka | monitor_printf(mon, " ");
|
1293 | 44e798d3 | Jan Kiszka | print_net_client(mon, vc); |
1294 | 19061e63 | Jan Kiszka | } /* else it's a netdev connected to a NIC, printed with the NIC */
|
1295 | 19061e63 | Jan Kiszka | if (peer && type == NET_CLIENT_TYPE_NIC) {
|
1296 | 44e798d3 | Jan Kiszka | monitor_printf(mon, " \\ ");
|
1297 | 44e798d3 | Jan Kiszka | print_net_client(mon, peer); |
1298 | a0104e0e | Markus Armbruster | } |
1299 | a0104e0e | Markus Armbruster | } |
1300 | 63a01ef8 | aliguori | } |
1301 | 63a01ef8 | aliguori | |
1302 | 4b37156c | Luiz Capitulino | void qmp_set_link(const char *name, bool up, Error **errp) |
1303 | 436e5e53 | aliguori | { |
1304 | 436e5e53 | aliguori | VLANState *vlan; |
1305 | 436e5e53 | aliguori | VLANClientState *vc = NULL;
|
1306 | 436e5e53 | aliguori | |
1307 | 5610c3aa | Mark McLoughlin | QTAILQ_FOREACH(vlan, &vlans, next) { |
1308 | 5610c3aa | Mark McLoughlin | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
1309 | 5610c3aa | Mark McLoughlin | if (strcmp(vc->name, name) == 0) { |
1310 | dd5de373 | edgar_igl | goto done;
|
1311 | 5610c3aa | Mark McLoughlin | } |
1312 | 5610c3aa | Mark McLoughlin | } |
1313 | 5610c3aa | Mark McLoughlin | } |
1314 | 85dde9a9 | Markus Armbruster | QTAILQ_FOREACH(vc, &non_vlan_clients, next) { |
1315 | 85dde9a9 | Markus Armbruster | if (!strcmp(vc->name, name)) {
|
1316 | 85dde9a9 | Markus Armbruster | goto done;
|
1317 | 85dde9a9 | Markus Armbruster | } |
1318 | 85dde9a9 | Markus Armbruster | } |
1319 | dd5de373 | edgar_igl | done:
|
1320 | 436e5e53 | aliguori | |
1321 | 436e5e53 | aliguori | if (!vc) {
|
1322 | 4b37156c | Luiz Capitulino | error_set(errp, QERR_DEVICE_NOT_FOUND, name); |
1323 | 4b37156c | Luiz Capitulino | return;
|
1324 | 436e5e53 | aliguori | } |
1325 | 436e5e53 | aliguori | |
1326 | c9b26a4c | Markus Armbruster | vc->link_down = !up; |
1327 | 436e5e53 | aliguori | |
1328 | 665a3b07 | Mark McLoughlin | if (vc->info->link_status_changed) {
|
1329 | 665a3b07 | Mark McLoughlin | vc->info->link_status_changed(vc); |
1330 | 665a3b07 | Mark McLoughlin | } |
1331 | ab1cbe1c | Michael S. Tsirkin | |
1332 | ab1cbe1c | Michael S. Tsirkin | /* Notify peer. Don't update peer link status: this makes it possible to
|
1333 | ab1cbe1c | Michael S. Tsirkin | * disconnect from host network without notifying the guest.
|
1334 | ab1cbe1c | Michael S. Tsirkin | * FIXME: is disconnected link status change operation useful?
|
1335 | ab1cbe1c | Michael S. Tsirkin | *
|
1336 | ab1cbe1c | Michael S. Tsirkin | * Current behaviour is compatible with qemu vlans where there could be
|
1337 | ab1cbe1c | Michael S. Tsirkin | * multiple clients that can still communicate with each other in
|
1338 | ab1cbe1c | Michael S. Tsirkin | * disconnected mode. For now maintain this compatibility. */
|
1339 | ab1cbe1c | Michael S. Tsirkin | if (vc->peer && vc->peer->info->link_status_changed) {
|
1340 | ab1cbe1c | Michael S. Tsirkin | vc->peer->info->link_status_changed(vc->peer); |
1341 | ab1cbe1c | Michael S. Tsirkin | } |
1342 | 436e5e53 | aliguori | } |
1343 | 436e5e53 | aliguori | |
1344 | 63a01ef8 | aliguori | void net_cleanup(void) |
1345 | 63a01ef8 | aliguori | { |
1346 | 63a01ef8 | aliguori | VLANState *vlan; |
1347 | 577c4af9 | Mark McLoughlin | VLANClientState *vc, *next_vc; |
1348 | 63a01ef8 | aliguori | |
1349 | 5610c3aa | Mark McLoughlin | QTAILQ_FOREACH(vlan, &vlans, next) { |
1350 | 5610c3aa | Mark McLoughlin | QTAILQ_FOREACH_SAFE(vc, &vlan->clients, next, next_vc) { |
1351 | b946a153 | aliguori | qemu_del_vlan_client(vc); |
1352 | 63a01ef8 | aliguori | } |
1353 | 63a01ef8 | aliguori | } |
1354 | 577c4af9 | Mark McLoughlin | |
1355 | 577c4af9 | Mark McLoughlin | QTAILQ_FOREACH_SAFE(vc, &non_vlan_clients, next, next_vc) { |
1356 | 577c4af9 | Mark McLoughlin | qemu_del_vlan_client(vc); |
1357 | 577c4af9 | Mark McLoughlin | } |
1358 | 63a01ef8 | aliguori | } |
1359 | 63a01ef8 | aliguori | |
1360 | 668680f7 | Markus Armbruster | void net_check_clients(void) |
1361 | 63a01ef8 | aliguori | { |
1362 | 63a01ef8 | aliguori | VLANState *vlan; |
1363 | 62112d18 | Markus Armbruster | VLANClientState *vc; |
1364 | 48e2faf2 | Peter Maydell | int i;
|
1365 | 63a01ef8 | aliguori | |
1366 | 641f6eae | Peter Maydell | /* Don't warn about the default network setup that you get if
|
1367 | 641f6eae | Peter Maydell | * no command line -net or -netdev options are specified. There
|
1368 | 641f6eae | Peter Maydell | * are two cases that we would otherwise complain about:
|
1369 | 641f6eae | Peter Maydell | * (1) board doesn't support a NIC but the implicit "-net nic"
|
1370 | 641f6eae | Peter Maydell | * requested one
|
1371 | 641f6eae | Peter Maydell | * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
|
1372 | 641f6eae | Peter Maydell | * sets up a nic that isn't connected to anything.
|
1373 | 641f6eae | Peter Maydell | */
|
1374 | 641f6eae | Peter Maydell | if (default_net) {
|
1375 | 641f6eae | Peter Maydell | return;
|
1376 | 641f6eae | Peter Maydell | } |
1377 | 641f6eae | Peter Maydell | |
1378 | 5610c3aa | Mark McLoughlin | QTAILQ_FOREACH(vlan, &vlans, next) { |
1379 | ac60cc18 | Tristan Gingold | int has_nic = 0, has_host_dev = 0; |
1380 | ac60cc18 | Tristan Gingold | |
1381 | 62112d18 | Markus Armbruster | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
1382 | 62112d18 | Markus Armbruster | switch (vc->info->type) {
|
1383 | 62112d18 | Markus Armbruster | case NET_CLIENT_TYPE_NIC:
|
1384 | 62112d18 | Markus Armbruster | has_nic = 1;
|
1385 | 62112d18 | Markus Armbruster | break;
|
1386 | 6f7b3b1b | Jan Kiszka | case NET_CLIENT_TYPE_USER:
|
1387 | 62112d18 | Markus Armbruster | case NET_CLIENT_TYPE_TAP:
|
1388 | 62112d18 | Markus Armbruster | case NET_CLIENT_TYPE_SOCKET:
|
1389 | 62112d18 | Markus Armbruster | case NET_CLIENT_TYPE_VDE:
|
1390 | 62112d18 | Markus Armbruster | has_host_dev = 1;
|
1391 | 62112d18 | Markus Armbruster | break;
|
1392 | 62112d18 | Markus Armbruster | default: ;
|
1393 | 62112d18 | Markus Armbruster | } |
1394 | 62112d18 | Markus Armbruster | } |
1395 | 62112d18 | Markus Armbruster | if (has_host_dev && !has_nic)
|
1396 | 63a01ef8 | aliguori | fprintf(stderr, "Warning: vlan %d with no nics\n", vlan->id);
|
1397 | 62112d18 | Markus Armbruster | if (has_nic && !has_host_dev)
|
1398 | 63a01ef8 | aliguori | fprintf(stderr, |
1399 | 63a01ef8 | aliguori | "Warning: vlan %d is not connected to host network\n",
|
1400 | 63a01ef8 | aliguori | vlan->id); |
1401 | 63a01ef8 | aliguori | } |
1402 | efe32fdd | Markus Armbruster | QTAILQ_FOREACH(vc, &non_vlan_clients, next) { |
1403 | efe32fdd | Markus Armbruster | if (!vc->peer) {
|
1404 | efe32fdd | Markus Armbruster | fprintf(stderr, "Warning: %s %s has no peer\n",
|
1405 | efe32fdd | Markus Armbruster | vc->info->type == NET_CLIENT_TYPE_NIC ? "nic" : "netdev", |
1406 | efe32fdd | Markus Armbruster | vc->name); |
1407 | efe32fdd | Markus Armbruster | } |
1408 | efe32fdd | Markus Armbruster | } |
1409 | 48e2faf2 | Peter Maydell | |
1410 | 48e2faf2 | Peter Maydell | /* Check that all NICs requested via -net nic actually got created.
|
1411 | 48e2faf2 | Peter Maydell | * NICs created via -device don't need to be checked here because
|
1412 | 48e2faf2 | Peter Maydell | * they are always instantiated.
|
1413 | 48e2faf2 | Peter Maydell | */
|
1414 | 48e2faf2 | Peter Maydell | for (i = 0; i < MAX_NICS; i++) { |
1415 | 48e2faf2 | Peter Maydell | NICInfo *nd = &nd_table[i]; |
1416 | 48e2faf2 | Peter Maydell | if (nd->used && !nd->instantiated) {
|
1417 | 48e2faf2 | Peter Maydell | fprintf(stderr, "Warning: requested NIC (%s, model %s) "
|
1418 | 48e2faf2 | Peter Maydell | "was not created (not supported by this machine?)\n",
|
1419 | 48e2faf2 | Peter Maydell | nd->name ? nd->name : "anonymous",
|
1420 | 48e2faf2 | Peter Maydell | nd->model ? nd->model : "unspecified");
|
1421 | 48e2faf2 | Peter Maydell | } |
1422 | 48e2faf2 | Peter Maydell | } |
1423 | 63a01ef8 | aliguori | } |
1424 | dc1c9fe8 | Mark McLoughlin | |
1425 | dc1c9fe8 | Mark McLoughlin | static int net_init_client(QemuOpts *opts, void *dummy) |
1426 | dc1c9fe8 | Mark McLoughlin | { |
1427 | c1671a08 | Mark McLoughlin | if (net_client_init(NULL, opts, 0) < 0) |
1428 | c1671a08 | Mark McLoughlin | return -1; |
1429 | c1671a08 | Mark McLoughlin | return 0; |
1430 | f6b134ac | Mark McLoughlin | } |
1431 | f6b134ac | Mark McLoughlin | |
1432 | f6b134ac | Mark McLoughlin | static int net_init_netdev(QemuOpts *opts, void *dummy) |
1433 | f6b134ac | Mark McLoughlin | { |
1434 | f6b134ac | Mark McLoughlin | return net_client_init(NULL, opts, 1); |
1435 | dc1c9fe8 | Mark McLoughlin | } |
1436 | dc1c9fe8 | Mark McLoughlin | |
1437 | dc1c9fe8 | Mark McLoughlin | int net_init_clients(void) |
1438 | dc1c9fe8 | Mark McLoughlin | { |
1439 | 3329f07b | Gerd Hoffmann | QemuOptsList *net = qemu_find_opts("net");
|
1440 | 3329f07b | Gerd Hoffmann | |
1441 | cb4522cc | Gerd Hoffmann | if (default_net) {
|
1442 | dc1c9fe8 | Mark McLoughlin | /* if no clients, we use a default config */
|
1443 | 3329f07b | Gerd Hoffmann | qemu_opts_set(net, NULL, "type", "nic"); |
1444 | dc1c9fe8 | Mark McLoughlin | #ifdef CONFIG_SLIRP
|
1445 | 3329f07b | Gerd Hoffmann | qemu_opts_set(net, NULL, "type", "user"); |
1446 | dc1c9fe8 | Mark McLoughlin | #endif
|
1447 | dc1c9fe8 | Mark McLoughlin | } |
1448 | dc1c9fe8 | Mark McLoughlin | |
1449 | 5610c3aa | Mark McLoughlin | QTAILQ_INIT(&vlans); |
1450 | 577c4af9 | Mark McLoughlin | QTAILQ_INIT(&non_vlan_clients); |
1451 | 5610c3aa | Mark McLoughlin | |
1452 | 3329f07b | Gerd Hoffmann | if (qemu_opts_foreach(qemu_find_opts("netdev"), net_init_netdev, NULL, 1) == -1) |
1453 | f6b134ac | Mark McLoughlin | return -1; |
1454 | f6b134ac | Mark McLoughlin | |
1455 | 3329f07b | Gerd Hoffmann | if (qemu_opts_foreach(net, net_init_client, NULL, 1) == -1) { |
1456 | dc1c9fe8 | Mark McLoughlin | return -1; |
1457 | dc1c9fe8 | Mark McLoughlin | } |
1458 | dc1c9fe8 | Mark McLoughlin | |
1459 | dc1c9fe8 | Mark McLoughlin | return 0; |
1460 | dc1c9fe8 | Mark McLoughlin | } |
1461 | dc1c9fe8 | Mark McLoughlin | |
1462 | 7f161aae | Mark McLoughlin | int net_client_parse(QemuOptsList *opts_list, const char *optarg) |
1463 | dc1c9fe8 | Mark McLoughlin | { |
1464 | a3a766e7 | Juan Quintela | #if defined(CONFIG_SLIRP)
|
1465 | 68ac40d2 | Mark McLoughlin | int ret;
|
1466 | 68ac40d2 | Mark McLoughlin | if (net_slirp_parse_legacy(opts_list, optarg, &ret)) {
|
1467 | dc1c9fe8 | Mark McLoughlin | return ret;
|
1468 | dc1c9fe8 | Mark McLoughlin | } |
1469 | a3a766e7 | Juan Quintela | #endif
|
1470 | 68ac40d2 | Mark McLoughlin | |
1471 | 8212c64f | Markus Armbruster | if (!qemu_opts_parse(opts_list, optarg, 1)) { |
1472 | dc1c9fe8 | Mark McLoughlin | return -1; |
1473 | dc1c9fe8 | Mark McLoughlin | } |
1474 | dc1c9fe8 | Mark McLoughlin | |
1475 | cb4522cc | Gerd Hoffmann | default_net = 0;
|
1476 | dc1c9fe8 | Mark McLoughlin | return 0; |
1477 | dc1c9fe8 | Mark McLoughlin | } |
1478 | 7fc8d918 | Jason Wang | |
1479 | 7fc8d918 | Jason Wang | /* From FreeBSD */
|
1480 | 7fc8d918 | Jason Wang | /* XXX: optimize */
|
1481 | 7fc8d918 | Jason Wang | unsigned compute_mcast_idx(const uint8_t *ep) |
1482 | 7fc8d918 | Jason Wang | { |
1483 | 7fc8d918 | Jason Wang | uint32_t crc; |
1484 | 7fc8d918 | Jason Wang | int carry, i, j;
|
1485 | 7fc8d918 | Jason Wang | uint8_t b; |
1486 | 7fc8d918 | Jason Wang | |
1487 | 7fc8d918 | Jason Wang | crc = 0xffffffff;
|
1488 | 7fc8d918 | Jason Wang | for (i = 0; i < 6; i++) { |
1489 | 7fc8d918 | Jason Wang | b = *ep++; |
1490 | 7fc8d918 | Jason Wang | for (j = 0; j < 8; j++) { |
1491 | 7fc8d918 | Jason Wang | carry = ((crc & 0x80000000L) ? 1 : 0) ^ (b & 0x01); |
1492 | 7fc8d918 | Jason Wang | crc <<= 1;
|
1493 | 7fc8d918 | Jason Wang | b >>= 1;
|
1494 | 7fc8d918 | Jason Wang | if (carry) {
|
1495 | 7fc8d918 | Jason Wang | crc = ((crc ^ POLYNOMIAL) | carry); |
1496 | 7fc8d918 | Jason Wang | } |
1497 | 7fc8d918 | Jason Wang | } |
1498 | 7fc8d918 | Jason Wang | } |
1499 | 7fc8d918 | Jason Wang | return crc >> 26; |
1500 | 7fc8d918 | Jason Wang | } |