]> git.proxmox.com Git - mirror_qemu.git/blame - net.c
Use glib memory allocation and free functions
[mirror_qemu.git] / net.c
CommitLineData
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 40static QTAILQ_HEAD(, VLANState) vlans;
577c4af9 41static QTAILQ_HEAD(, VLANClientState) non_vlan_clients;
63a01ef8 42
cb4522cc
GH
43int default_net = 1;
44
63a01ef8
AL
45/***********************************************************/
46/* network device redirectors */
47
68ac40d2 48#if defined(DEBUG_NET)
63a01ef8
AL
49static 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
76static 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
96int 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
126void 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
135void 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
150static 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
7267c094 173 return g_strdup(buf);
676cff29
AL
174}
175
9a6ecb30 176static 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);
181static 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
187VLANClientState *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
7267c094 197 vc = g_malloc0(info->size);
5610c3aa 198
665a3b07 199 vc->info = info;
7267c094 200 vc->model = g_strdup(model);
45460d1a 201 if (name) {
7267c094 202 vc->name = g_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
227NICState *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 248static 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
261static 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 }
7267c094
AL
271 g_free(vc->name);
272 g_free(vc->model);
273 g_free(vc);
63a01ef8
AL
274}
275
a083a89d
MT
276void 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 306VLANClientState *
1a609520
JK
307qemu_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
332void 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 352int 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 385static 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 415static 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
459void 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 476void 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
491static 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
516ssize_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
524void 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
529ssize_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
535static 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 546static 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 565static 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
601ssize_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
622ssize_t
623qemu_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 629VLANState *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
7267c094 643 vlan = g_malloc0(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 656VLANClientState *qemu_find_netdev(const char *id)
5869c4d5
MM
657{
658 VLANClientState *vc;
659
660 QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
85dde9a9
MA
661 if (vc->info->type == NET_CLIENT_TYPE_NIC)
662 continue;
5869c4d5
MM
663 if (!strcmp(vc->name, id)) {
664 return vc;
665 }
666 }
667
668 return NULL;
669}
670
7697079b
AL
671static int nic_get_free_idx(void)
672{
673 int index;
674
675 for (index = 0; index < MAX_NICS; index++)
676 if (!nd_table[index].used)
677 return index;
678 return -1;
679}
680
07caea31
MA
681int qemu_show_nic_models(const char *arg, const char *const *models)
682{
683 int i;
684
685 if (!arg || strcmp(arg, "?"))
686 return 0;
687
688 fprintf(stderr, "qemu: Supported NIC models: ");
689 for (i = 0 ; models[i]; i++)
690 fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n');
691 return 1;
692}
693
d07f22c5
AL
694void qemu_check_nic_model(NICInfo *nd, const char *model)
695{
696 const char *models[2];
697
698 models[0] = model;
699 models[1] = NULL;
700
07caea31
MA
701 if (qemu_show_nic_models(nd->model, models))
702 exit(0);
703 if (qemu_find_nic_model(nd, models, model) < 0)
704 exit(1);
d07f22c5
AL
705}
706
07caea31
MA
707int qemu_find_nic_model(NICInfo *nd, const char * const *models,
708 const char *default_model)
d07f22c5 709{
07caea31 710 int i;
d07f22c5
AL
711
712 if (!nd->model)
7267c094 713 nd->model = g_strdup(default_model);
d07f22c5 714
07caea31
MA
715 for (i = 0 ; models[i]; i++) {
716 if (strcmp(nd->model, models[i]) == 0)
717 return i;
d07f22c5
AL
718 }
719
6daf194d 720 error_report("Unsupported NIC model: %s", nd->model);
07caea31 721 return -1;
d07f22c5
AL
722}
723
5281d757 724int net_handle_fd_param(Monitor *mon, const char *param)
c1d6eed7 725{
f7c31d63
JW
726 int fd;
727
728 if (!qemu_isdigit(param[0]) && mon) {
c1d6eed7
MM
729
730 fd = monitor_get_fd(mon, param);
731 if (fd == -1) {
1ecda02b 732 error_report("No file descriptor named %s found", param);
c1d6eed7
MM
733 return -1;
734 }
c1d6eed7 735 } else {
f7c31d63
JW
736 char *endptr = NULL;
737
738 fd = strtol(param, &endptr, 10);
739 if (*endptr || (fd == 0 && param == endptr)) {
740 return -1;
741 }
c1d6eed7 742 }
f7c31d63
JW
743
744 return fd;
c1d6eed7
MM
745}
746
f6b134ac
MM
747static int net_init_nic(QemuOpts *opts,
748 Monitor *mon,
749 const char *name,
750 VLANState *vlan)
f83c6e10
MM
751{
752 int idx;
753 NICInfo *nd;
5869c4d5 754 const char *netdev;
f83c6e10
MM
755
756 idx = nic_get_free_idx();
757 if (idx == -1 || nb_nics >= MAX_NICS) {
1ecda02b 758 error_report("Too Many NICs");
f83c6e10
MM
759 return -1;
760 }
761
762 nd = &nd_table[idx];
763
764 memset(nd, 0, sizeof(*nd));
765
5869c4d5
MM
766 if ((netdev = qemu_opt_get(opts, "netdev"))) {
767 nd->netdev = qemu_find_netdev(netdev);
768 if (!nd->netdev) {
1ecda02b 769 error_report("netdev '%s' not found", netdev);
5869c4d5
MM
770 return -1;
771 }
772 } else {
773 assert(vlan);
774 nd->vlan = vlan;
775 }
6d952ebe 776 if (name) {
7267c094 777 nd->name = g_strdup(name);
6d952ebe 778 }
f83c6e10 779 if (qemu_opt_get(opts, "model")) {
7267c094 780 nd->model = g_strdup(qemu_opt_get(opts, "model"));
f83c6e10
MM
781 }
782 if (qemu_opt_get(opts, "addr")) {
7267c094 783 nd->devaddr = g_strdup(qemu_opt_get(opts, "addr"));
f83c6e10
MM
784 }
785
f83c6e10 786 if (qemu_opt_get(opts, "macaddr") &&
6eed1856 787 net_parse_macaddr(nd->macaddr.a, qemu_opt_get(opts, "macaddr")) < 0) {
1ecda02b 788 error_report("invalid syntax for ethernet address");
f83c6e10
MM
789 return -1;
790 }
6eed1856 791 qemu_macaddr_default_if_unset(&nd->macaddr);
f83c6e10 792
75422b0d
AS
793 nd->nvectors = qemu_opt_get_number(opts, "vectors",
794 DEV_NVECTORS_UNSPECIFIED);
795 if (nd->nvectors != DEV_NVECTORS_UNSPECIFIED &&
f83c6e10 796 (nd->nvectors < 0 || nd->nvectors > 0x7ffffff)) {
1ecda02b 797 error_report("invalid # of vectors: %d", nd->nvectors);
f83c6e10
MM
798 return -1;
799 }
800
801 nd->used = 1;
f83c6e10
MM
802 nb_nics++;
803
804 return idx;
805}
806
807#define NET_COMMON_PARAMS_DESC \
808 { \
809 .name = "type", \
810 .type = QEMU_OPT_STRING, \
811 .help = "net client type (nic, tap etc.)", \
812 }, { \
813 .name = "vlan", \
814 .type = QEMU_OPT_NUMBER, \
815 .help = "vlan number", \
816 }, { \
817 .name = "name", \
818 .type = QEMU_OPT_STRING, \
819 .help = "identifier for monitor commands", \
820 }
821
6d952ebe
MM
822typedef int (*net_client_init_func)(QemuOpts *opts,
823 Monitor *mon,
f6b134ac
MM
824 const char *name,
825 VLANState *vlan);
f83c6e10
MM
826
827/* magic number, but compiler will warn if too small */
828#define NET_MAX_DESC 20
829
238431a9 830static const struct {
f83c6e10
MM
831 const char *type;
832 net_client_init_func init;
833 QemuOptDesc desc[NET_MAX_DESC];
6f7b3b1b
JK
834} net_client_types[NET_CLIENT_TYPE_MAX] = {
835 [NET_CLIENT_TYPE_NONE] = {
f83c6e10
MM
836 .type = "none",
837 .desc = {
838 NET_COMMON_PARAMS_DESC,
839 { /* end of list */ }
840 },
6f7b3b1b
JK
841 },
842 [NET_CLIENT_TYPE_NIC] = {
f83c6e10
MM
843 .type = "nic",
844 .init = net_init_nic,
845 .desc = {
846 NET_COMMON_PARAMS_DESC,
5869c4d5
MM
847 {
848 .name = "netdev",
849 .type = QEMU_OPT_STRING,
850 .help = "id of -netdev to connect to",
851 },
f83c6e10
MM
852 {
853 .name = "macaddr",
854 .type = QEMU_OPT_STRING,
855 .help = "MAC address",
856 }, {
857 .name = "model",
858 .type = QEMU_OPT_STRING,
859 .help = "device model (e1000, rtl8139, virtio etc.)",
860 }, {
861 .name = "addr",
862 .type = QEMU_OPT_STRING,
863 .help = "PCI device address",
864 }, {
865 .name = "vectors",
866 .type = QEMU_OPT_NUMBER,
867 .help = "number of MSI-x vectors, 0 to disable MSI-X",
868 },
869 { /* end of list */ }
870 },
6f7b3b1b 871 },
ec302ffd 872#ifdef CONFIG_SLIRP
6f7b3b1b 873 [NET_CLIENT_TYPE_USER] = {
ec302ffd
MM
874 .type = "user",
875 .init = net_init_slirp,
876 .desc = {
877 NET_COMMON_PARAMS_DESC,
878 {
879 .name = "hostname",
880 .type = QEMU_OPT_STRING,
881 .help = "client hostname reported by the builtin DHCP server",
882 }, {
883 .name = "restrict",
884 .type = QEMU_OPT_STRING,
885 .help = "isolate the guest from the host (y|yes|n|no)",
886 }, {
887 .name = "ip",
888 .type = QEMU_OPT_STRING,
889 .help = "legacy parameter, use net= instead",
890 }, {
891 .name = "net",
892 .type = QEMU_OPT_STRING,
893 .help = "IP address and optional netmask",
894 }, {
895 .name = "host",
896 .type = QEMU_OPT_STRING,
897 .help = "guest-visible address of the host",
898 }, {
899 .name = "tftp",
900 .type = QEMU_OPT_STRING,
901 .help = "root directory of the built-in TFTP server",
902 }, {
903 .name = "bootfile",
904 .type = QEMU_OPT_STRING,
905 .help = "BOOTP filename, for use with tftp=",
906 }, {
907 .name = "dhcpstart",
908 .type = QEMU_OPT_STRING,
909 .help = "the first of the 16 IPs the built-in DHCP server can assign",
910 }, {
911 .name = "dns",
912 .type = QEMU_OPT_STRING,
913 .help = "guest-visible address of the virtual nameserver",
914 }, {
915 .name = "smb",
916 .type = QEMU_OPT_STRING,
917 .help = "root directory of the built-in SMB server",
918 }, {
919 .name = "smbserver",
920 .type = QEMU_OPT_STRING,
921 .help = "IP address of the built-in SMB server",
922 }, {
923 .name = "hostfwd",
924 .type = QEMU_OPT_STRING,
925 .help = "guest port number to forward incoming TCP or UDP connections",
926 }, {
927 .name = "guestfwd",
928 .type = QEMU_OPT_STRING,
929 .help = "IP address and port to forward guest TCP connections",
930 },
931 { /* end of list */ }
932 },
6f7b3b1b 933 },
8a1c5235 934#endif
6f7b3b1b 935 [NET_CLIENT_TYPE_TAP] = {
8a1c5235 936 .type = "tap",
a8ed73f7 937 .init = net_init_tap,
8a1c5235
MM
938 .desc = {
939 NET_COMMON_PARAMS_DESC,
940 {
941 .name = "ifname",
942 .type = QEMU_OPT_STRING,
943 .help = "interface name",
944 },
a8ed73f7 945#ifndef _WIN32
8a1c5235
MM
946 {
947 .name = "fd",
948 .type = QEMU_OPT_STRING,
949 .help = "file descriptor of an already opened tap",
8a1c5235
MM
950 }, {
951 .name = "script",
952 .type = QEMU_OPT_STRING,
953 .help = "script to initialize the interface",
954 }, {
955 .name = "downscript",
956 .type = QEMU_OPT_STRING,
957 .help = "script to shut down the interface",
8a1c5235
MM
958 }, {
959 .name = "sndbuf",
960 .type = QEMU_OPT_SIZE,
961 .help = "send buffer limit"
baf74c95
MM
962 }, {
963 .name = "vnet_hdr",
964 .type = QEMU_OPT_BOOL,
965 .help = "enable the IFF_VNET_HDR flag on the tap interface"
82b0d80e
MT
966 }, {
967 .name = "vhost",
968 .type = QEMU_OPT_BOOL,
969 .help = "enable vhost-net network accelerator",
970 }, {
971 .name = "vhostfd",
972 .type = QEMU_OPT_STRING,
973 .help = "file descriptor of an already opened vhost net device",
96c94b29
JW
974 }, {
975 .name = "vhostforce",
976 .type = QEMU_OPT_BOOL,
977 .help = "force vhost on for non-MSIX virtio guests",
978 },
a8ed73f7 979#endif /* _WIN32 */
8a1c5235
MM
980 { /* end of list */ }
981 },
6f7b3b1b
JK
982 },
983 [NET_CLIENT_TYPE_SOCKET] = {
88ce16ca
MM
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 },
6f7b3b1b 1011 },
dd51058d 1012#ifdef CONFIG_VDE
6f7b3b1b 1013 [NET_CLIENT_TYPE_VDE] = {
dd51058d
MM
1014 .type = "vde",
1015 .init = net_init_vde,
1016 .desc = {
1017 NET_COMMON_PARAMS_DESC,
1018 {
1019 .name = "sock",
1020 .type = QEMU_OPT_STRING,
1021 .help = "socket path",
1022 }, {
1023 .name = "port",
1024 .type = QEMU_OPT_NUMBER,
1025 .help = "port number",
1026 }, {
1027 .name = "group",
1028 .type = QEMU_OPT_STRING,
1029 .help = "group owner of socket",
1030 }, {
1031 .name = "mode",
1032 .type = QEMU_OPT_NUMBER,
1033 .help = "permissions for socket",
1034 },
1035 { /* end of list */ }
1036 },
6f7b3b1b 1037 },
dd51058d 1038#endif
6f7b3b1b 1039 [NET_CLIENT_TYPE_DUMP] = {
ed2955c2
MM
1040 .type = "dump",
1041 .init = net_init_dump,
1042 .desc = {
1043 NET_COMMON_PARAMS_DESC,
1044 {
1045 .name = "len",
1046 .type = QEMU_OPT_SIZE,
1047 .help = "per-packet size limit (64k default)",
1048 }, {
1049 .name = "file",
1050 .type = QEMU_OPT_STRING,
1051 .help = "dump file path (default is qemu-vlan0.pcap)",
1052 },
1053 { /* end of list */ }
1054 },
f83c6e10 1055 },
f83c6e10
MM
1056};
1057
f6b134ac 1058int net_client_init(Monitor *mon, QemuOpts *opts, int is_netdev)
f83c6e10 1059{
6d952ebe 1060 const char *name;
f83c6e10
MM
1061 const char *type;
1062 int i;
1063
1064 type = qemu_opt_get(opts, "type");
5294e2c7
MA
1065 if (!type) {
1066 qerror_report(QERR_MISSING_PARAMETER, "type");
1067 return -1;
1068 }
f83c6e10 1069
5294e2c7 1070 if (is_netdev) {
f6b134ac
MM
1071 if (strcmp(type, "tap") != 0 &&
1072#ifdef CONFIG_SLIRP
1073 strcmp(type, "user") != 0 &&
1074#endif
1075#ifdef CONFIG_VDE
1076 strcmp(type, "vde") != 0 &&
1077#endif
1078 strcmp(type, "socket") != 0) {
5294e2c7
MA
1079 qerror_report(QERR_INVALID_PARAMETER_VALUE, "type",
1080 "a netdev backend type");
f6b134ac
MM
1081 return -1;
1082 }
1083
1084 if (qemu_opt_get(opts, "vlan")) {
5294e2c7 1085 qerror_report(QERR_INVALID_PARAMETER, "vlan");
f6b134ac
MM
1086 return -1;
1087 }
1088 if (qemu_opt_get(opts, "name")) {
5294e2c7 1089 qerror_report(QERR_INVALID_PARAMETER, "name");
f6b134ac
MM
1090 return -1;
1091 }
1092 if (!qemu_opts_id(opts)) {
5294e2c7 1093 qerror_report(QERR_MISSING_PARAMETER, "id");
f6b134ac
MM
1094 return -1;
1095 }
1096 }
1097
6d952ebe
MM
1098 name = qemu_opts_id(opts);
1099 if (!name) {
1100 name = qemu_opt_get(opts, "name");
1101 }
1102
6f7b3b1b
JK
1103 for (i = 0; i < NET_CLIENT_TYPE_MAX; i++) {
1104 if (net_client_types[i].type != NULL &&
1105 !strcmp(net_client_types[i].type, type)) {
f6b134ac 1106 VLANState *vlan = NULL;
50e32ea8 1107 int ret;
f6b134ac 1108
f83c6e10
MM
1109 if (qemu_opts_validate(opts, &net_client_types[i].desc[0]) == -1) {
1110 return -1;
1111 }
1112
5869c4d5
MM
1113 /* Do not add to a vlan if it's a -netdev or a nic with a
1114 * netdev= parameter. */
1115 if (!(is_netdev ||
1116 (strcmp(type, "nic") == 0 && qemu_opt_get(opts, "netdev")))) {
f6b134ac
MM
1117 vlan = qemu_find_vlan(qemu_opt_get_number(opts, "vlan", 0), 1);
1118 }
1119
03c71553 1120 ret = 0;
f83c6e10 1121 if (net_client_types[i].init) {
50e32ea8
AS
1122 ret = net_client_types[i].init(opts, mon, name, vlan);
1123 if (ret < 0) {
5294e2c7
MA
1124 /* TODO push error reporting into init() methods */
1125 qerror_report(QERR_DEVICE_INIT_FAILED, type);
1126 return -1;
1127 }
f83c6e10 1128 }
50e32ea8 1129 return ret;
f83c6e10
MM
1130 }
1131 }
1132
5294e2c7
MA
1133 qerror_report(QERR_INVALID_PARAMETER_VALUE, "type",
1134 "a network client type");
f83c6e10
MM
1135 return -1;
1136}
1137
6f338c34
AL
1138static int net_host_check_device(const char *device)
1139{
1140 int i;
bb9ea79e 1141 const char *valid_param_list[] = { "tap", "socket", "dump"
6f338c34
AL
1142#ifdef CONFIG_SLIRP
1143 ,"user"
1144#endif
1145#ifdef CONFIG_VDE
1146 ,"vde"
1147#endif
1148 };
1149 for (i = 0; i < sizeof(valid_param_list) / sizeof(char *); i++) {
1150 if (!strncmp(valid_param_list[i], device,
1151 strlen(valid_param_list[i])))
1152 return 1;
1153 }
1154
1155 return 0;
1156}
1157
f18c16de 1158void net_host_device_add(Monitor *mon, const QDict *qdict)
6f338c34 1159{
f18c16de 1160 const char *device = qdict_get_str(qdict, "device");
7f1c9d20
MM
1161 const char *opts_str = qdict_get_try_str(qdict, "opts");
1162 QemuOpts *opts;
f18c16de 1163
6f338c34 1164 if (!net_host_check_device(device)) {
376253ec 1165 monitor_printf(mon, "invalid host network device %s\n", device);
6f338c34
AL
1166 return;
1167 }
7f1c9d20 1168
3329f07b 1169 opts = qemu_opts_parse(qemu_find_opts("net"), opts_str ? opts_str : "", 0);
7f1c9d20 1170 if (!opts) {
7f1c9d20
MM
1171 return;
1172 }
1173
1174 qemu_opt_set(opts, "type", device);
1175
f6b134ac 1176 if (net_client_init(mon, opts, 0) < 0) {
5c8be678
AL
1177 monitor_printf(mon, "adding host network device %s failed\n", device);
1178 }
6f338c34
AL
1179}
1180
f18c16de 1181void net_host_device_remove(Monitor *mon, const QDict *qdict)
6f338c34 1182{
6f338c34 1183 VLANClientState *vc;
f18c16de
LC
1184 int vlan_id = qdict_get_int(qdict, "vlan_id");
1185 const char *device = qdict_get_str(qdict, "device");
6f338c34 1186
1a609520 1187 vc = qemu_find_vlan_client_by_name(mon, vlan_id, device);
6f338c34 1188 if (!vc) {
6f338c34
AL
1189 return;
1190 }
e8f1f9db
AL
1191 if (!net_host_check_device(vc->model)) {
1192 monitor_printf(mon, "invalid host network device %s\n", device);
1193 return;
1194 }
6f338c34
AL
1195 qemu_del_vlan_client(vc);
1196}
1197
ae82d324
MA
1198int do_netdev_add(Monitor *mon, const QDict *qdict, QObject **ret_data)
1199{
1200 QemuOpts *opts;
1201 int res;
1202
3329f07b 1203 opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict);
ae82d324
MA
1204 if (!opts) {
1205 return -1;
1206 }
1207
1208 res = net_client_init(mon, opts, 1);
410cbafe
YT
1209 if (res < 0) {
1210 qemu_opts_del(opts);
1211 }
1212
ae82d324
MA
1213 return res;
1214}
1215
ae82d324
MA
1216int do_netdev_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
1217{
1218 const char *id = qdict_get_str(qdict, "id");
1219 VLANClientState *vc;
1220
1221 vc = qemu_find_netdev(id);
85dde9a9 1222 if (!vc) {
ae82d324
MA
1223 qerror_report(QERR_DEVICE_NOT_FOUND, id);
1224 return -1;
1225 }
ae82d324 1226 qemu_del_vlan_client(vc);
3329f07b 1227 qemu_opts_del(qemu_opts_find(qemu_find_opts("netdev"), id));
ae82d324
MA
1228 return 0;
1229}
1230
44e798d3
JK
1231static void print_net_client(Monitor *mon, VLANClientState *vc)
1232{
1233 monitor_printf(mon, "%s: type=%s,%s\n", vc->name,
1234 net_client_types[vc->info->type].type, vc->info_str);
1235}
1236
376253ec 1237void do_info_network(Monitor *mon)
63a01ef8
AL
1238{
1239 VLANState *vlan;
19061e63
JK
1240 VLANClientState *vc, *peer;
1241 net_client_type type;
63a01ef8 1242
5610c3aa 1243 QTAILQ_FOREACH(vlan, &vlans, next) {
376253ec 1244 monitor_printf(mon, "VLAN %d devices:\n", vlan->id);
5610c3aa
MM
1245
1246 QTAILQ_FOREACH(vc, &vlan->clients, next) {
44e798d3
JK
1247 monitor_printf(mon, " ");
1248 print_net_client(mon, vc);
5610c3aa 1249 }
63a01ef8 1250 }
a0104e0e
MA
1251 monitor_printf(mon, "Devices not on any VLAN:\n");
1252 QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
19061e63
JK
1253 peer = vc->peer;
1254 type = vc->info->type;
1255 if (!peer || type == NET_CLIENT_TYPE_NIC) {
44e798d3
JK
1256 monitor_printf(mon, " ");
1257 print_net_client(mon, vc);
19061e63
JK
1258 } /* else it's a netdev connected to a NIC, printed with the NIC */
1259 if (peer && type == NET_CLIENT_TYPE_NIC) {
44e798d3
JK
1260 monitor_printf(mon, " \\ ");
1261 print_net_client(mon, peer);
a0104e0e 1262 }
a0104e0e 1263 }
63a01ef8
AL
1264}
1265
5369e3c0 1266int do_set_link(Monitor *mon, const QDict *qdict, QObject **ret_data)
436e5e53
AL
1267{
1268 VLANState *vlan;
1269 VLANClientState *vc = NULL;
f18c16de 1270 const char *name = qdict_get_str(qdict, "name");
c9b26a4c 1271 int up = qdict_get_bool(qdict, "up");
436e5e53 1272
5610c3aa
MM
1273 QTAILQ_FOREACH(vlan, &vlans, next) {
1274 QTAILQ_FOREACH(vc, &vlan->clients, next) {
1275 if (strcmp(vc->name, name) == 0) {
dd5de373 1276 goto done;
5610c3aa
MM
1277 }
1278 }
1279 }
85dde9a9
MA
1280 QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
1281 if (!strcmp(vc->name, name)) {
1282 goto done;
1283 }
1284 }
dd5de373 1285done:
436e5e53
AL
1286
1287 if (!vc) {
5369e3c0
MA
1288 qerror_report(QERR_DEVICE_NOT_FOUND, name);
1289 return -1;
436e5e53
AL
1290 }
1291
c9b26a4c 1292 vc->link_down = !up;
436e5e53 1293
665a3b07
MM
1294 if (vc->info->link_status_changed) {
1295 vc->info->link_status_changed(vc);
1296 }
ab1cbe1c
MT
1297
1298 /* Notify peer. Don't update peer link status: this makes it possible to
1299 * disconnect from host network without notifying the guest.
1300 * FIXME: is disconnected link status change operation useful?
1301 *
1302 * Current behaviour is compatible with qemu vlans where there could be
1303 * multiple clients that can still communicate with each other in
1304 * disconnected mode. For now maintain this compatibility. */
1305 if (vc->peer && vc->peer->info->link_status_changed) {
1306 vc->peer->info->link_status_changed(vc->peer);
1307 }
5369e3c0 1308 return 0;
436e5e53
AL
1309}
1310
63a01ef8
AL
1311void net_cleanup(void)
1312{
1313 VLANState *vlan;
577c4af9 1314 VLANClientState *vc, *next_vc;
63a01ef8 1315
5610c3aa 1316 QTAILQ_FOREACH(vlan, &vlans, next) {
5610c3aa 1317 QTAILQ_FOREACH_SAFE(vc, &vlan->clients, next, next_vc) {
b946a153 1318 qemu_del_vlan_client(vc);
63a01ef8
AL
1319 }
1320 }
577c4af9
MM
1321
1322 QTAILQ_FOREACH_SAFE(vc, &non_vlan_clients, next, next_vc) {
1323 qemu_del_vlan_client(vc);
1324 }
63a01ef8
AL
1325}
1326
668680f7 1327void net_check_clients(void)
63a01ef8
AL
1328{
1329 VLANState *vlan;
62112d18 1330 VLANClientState *vc;
48e2faf2 1331 int i;
63a01ef8 1332
641f6eae
PM
1333 /* Don't warn about the default network setup that you get if
1334 * no command line -net or -netdev options are specified. There
1335 * are two cases that we would otherwise complain about:
1336 * (1) board doesn't support a NIC but the implicit "-net nic"
1337 * requested one
1338 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
1339 * sets up a nic that isn't connected to anything.
1340 */
1341 if (default_net) {
1342 return;
1343 }
1344
5610c3aa 1345 QTAILQ_FOREACH(vlan, &vlans, next) {
ac60cc18
TG
1346 int has_nic = 0, has_host_dev = 0;
1347
62112d18
MA
1348 QTAILQ_FOREACH(vc, &vlan->clients, next) {
1349 switch (vc->info->type) {
1350 case NET_CLIENT_TYPE_NIC:
1351 has_nic = 1;
1352 break;
6f7b3b1b 1353 case NET_CLIENT_TYPE_USER:
62112d18
MA
1354 case NET_CLIENT_TYPE_TAP:
1355 case NET_CLIENT_TYPE_SOCKET:
1356 case NET_CLIENT_TYPE_VDE:
1357 has_host_dev = 1;
1358 break;
1359 default: ;
1360 }
1361 }
1362 if (has_host_dev && !has_nic)
63a01ef8 1363 fprintf(stderr, "Warning: vlan %d with no nics\n", vlan->id);
62112d18 1364 if (has_nic && !has_host_dev)
63a01ef8
AL
1365 fprintf(stderr,
1366 "Warning: vlan %d is not connected to host network\n",
1367 vlan->id);
1368 }
efe32fdd
MA
1369 QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
1370 if (!vc->peer) {
1371 fprintf(stderr, "Warning: %s %s has no peer\n",
1372 vc->info->type == NET_CLIENT_TYPE_NIC ? "nic" : "netdev",
1373 vc->name);
1374 }
1375 }
48e2faf2
PM
1376
1377 /* Check that all NICs requested via -net nic actually got created.
1378 * NICs created via -device don't need to be checked here because
1379 * they are always instantiated.
1380 */
1381 for (i = 0; i < MAX_NICS; i++) {
1382 NICInfo *nd = &nd_table[i];
1383 if (nd->used && !nd->instantiated) {
1384 fprintf(stderr, "Warning: requested NIC (%s, model %s) "
1385 "was not created (not supported by this machine?)\n",
1386 nd->name ? nd->name : "anonymous",
1387 nd->model ? nd->model : "unspecified");
1388 }
1389 }
63a01ef8 1390}
dc1c9fe8
MM
1391
1392static int net_init_client(QemuOpts *opts, void *dummy)
1393{
c1671a08
MM
1394 if (net_client_init(NULL, opts, 0) < 0)
1395 return -1;
1396 return 0;
f6b134ac
MM
1397}
1398
1399static int net_init_netdev(QemuOpts *opts, void *dummy)
1400{
1401 return net_client_init(NULL, opts, 1);
dc1c9fe8
MM
1402}
1403
1404int net_init_clients(void)
1405{
3329f07b
GH
1406 QemuOptsList *net = qemu_find_opts("net");
1407
cb4522cc 1408 if (default_net) {
dc1c9fe8 1409 /* if no clients, we use a default config */
3329f07b 1410 qemu_opts_set(net, NULL, "type", "nic");
dc1c9fe8 1411#ifdef CONFIG_SLIRP
3329f07b 1412 qemu_opts_set(net, NULL, "type", "user");
dc1c9fe8
MM
1413#endif
1414 }
1415
5610c3aa 1416 QTAILQ_INIT(&vlans);
577c4af9 1417 QTAILQ_INIT(&non_vlan_clients);
5610c3aa 1418
3329f07b 1419 if (qemu_opts_foreach(qemu_find_opts("netdev"), net_init_netdev, NULL, 1) == -1)
f6b134ac
MM
1420 return -1;
1421
3329f07b 1422 if (qemu_opts_foreach(net, net_init_client, NULL, 1) == -1) {
dc1c9fe8
MM
1423 return -1;
1424 }
1425
dc1c9fe8
MM
1426 return 0;
1427}
1428
7f161aae 1429int net_client_parse(QemuOptsList *opts_list, const char *optarg)
dc1c9fe8 1430{
a3a766e7 1431#if defined(CONFIG_SLIRP)
68ac40d2
MM
1432 int ret;
1433 if (net_slirp_parse_legacy(opts_list, optarg, &ret)) {
dc1c9fe8
MM
1434 return ret;
1435 }
a3a766e7 1436#endif
68ac40d2 1437
8212c64f 1438 if (!qemu_opts_parse(opts_list, optarg, 1)) {
dc1c9fe8
MM
1439 return -1;
1440 }
1441
cb4522cc 1442 default_net = 0;
dc1c9fe8
MM
1443 return 0;
1444}