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