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