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