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