]> git.proxmox.com Git - mirror_qemu.git/blame - net/net.c
QemuOpts: Convert qemu_opt_set_number() to Error, fix its use
[mirror_qemu.git] / net / 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 */
d40cdb10
BS
24#include "config-host.h"
25
1422e32d 26#include "net/net.h"
fd9400b3
PB
27#include "clients.h"
28#include "hub.h"
1422e32d 29#include "net/slirp.h"
d60b20cf 30#include "net/eth.h"
fd9400b3 31#include "util.h"
a245fc18 32
83c9089e 33#include "monitor/monitor.h"
1df49e04 34#include "qemu-common.h"
1de7afc9
PB
35#include "qemu/sockets.h"
36#include "qemu/config-file.h"
4b37156c 37#include "qmp-commands.h"
75422b0d 38#include "hw/qdev.h"
1de7afc9 39#include "qemu/iov.h"
6a1751b7 40#include "qemu/main-loop.h"
6687b79d
LE
41#include "qapi-visit.h"
42#include "qapi/opts-visitor.h"
7b1b5d19 43#include "qapi/dealloc-visitor.h"
e1d64c08 44#include "sysemu/sysemu.h"
511d2b14 45
2944e4ec
SW
46/* Net bridge is currently not supported for W32. */
47#if !defined(_WIN32)
48# define CONFIG_NET_BRIDGE
49#endif
50
ca77d85e 51static VMChangeStateEntry *net_change_state_entry;
4e68f7a0 52static QTAILQ_HEAD(, NetClientState) net_clients;
63a01ef8 53
84007e81
HB
54const char *host_net_devices[] = {
55 "tap",
56 "socket",
57 "dump",
58#ifdef CONFIG_NET_BRIDGE
59 "bridge",
60#endif
61#ifdef CONFIG_SLIRP
62 "user",
63#endif
64#ifdef CONFIG_VDE
65 "vde",
66#endif
03ce5744 67 "vhost-user",
84007e81
HB
68 NULL,
69};
70
cb4522cc
GH
71int default_net = 1;
72
63a01ef8
AL
73/***********************************************************/
74/* network device redirectors */
75
68ac40d2 76#if defined(DEBUG_NET)
63a01ef8
AL
77static void hex_dump(FILE *f, const uint8_t *buf, int size)
78{
79 int len, i, j, c;
80
81 for(i=0;i<size;i+=16) {
82 len = size - i;
83 if (len > 16)
84 len = 16;
85 fprintf(f, "%08x ", i);
86 for(j=0;j<16;j++) {
87 if (j < len)
88 fprintf(f, " %02x", buf[i+j]);
89 else
90 fprintf(f, " ");
91 }
92 fprintf(f, " ");
93 for(j=0;j<len;j++) {
94 c = buf[i+j];
95 if (c < ' ' || c > '~')
96 c = '.';
97 fprintf(f, "%c", c);
98 }
99 fprintf(f, "\n");
100 }
101}
102#endif
103
63a01ef8
AL
104static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
105{
106 const char *p, *p1;
107 int len;
108 p = *pp;
109 p1 = strchr(p, sep);
110 if (!p1)
111 return -1;
112 len = p1 - p;
113 p1++;
114 if (buf_size > 0) {
115 if (len > buf_size - 1)
116 len = buf_size - 1;
117 memcpy(buf, p, len);
118 buf[len] = '\0';
119 }
120 *pp = p1;
121 return 0;
122}
123
63a01ef8
AL
124int parse_host_port(struct sockaddr_in *saddr, const char *str)
125{
126 char buf[512];
127 struct hostent *he;
128 const char *p, *r;
129 int port;
130
131 p = str;
132 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
133 return -1;
134 saddr->sin_family = AF_INET;
135 if (buf[0] == '\0') {
136 saddr->sin_addr.s_addr = 0;
137 } else {
cd390083 138 if (qemu_isdigit(buf[0])) {
63a01ef8
AL
139 if (!inet_aton(buf, &saddr->sin_addr))
140 return -1;
141 } else {
142 if ((he = gethostbyname(buf)) == NULL)
143 return - 1;
144 saddr->sin_addr = *(struct in_addr *)he->h_addr;
145 }
146 }
147 port = strtol(p, (char **)&r, 0);
148 if (r == p)
149 return -1;
150 saddr->sin_port = htons(port);
151 return 0;
152}
153
35277d14 154void qemu_format_nic_info_str(NetClientState *nc, uint8_t macaddr[6])
7cb7434b 155{
35277d14 156 snprintf(nc->info_str, sizeof(nc->info_str),
4dda4063 157 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
35277d14 158 nc->model,
7cb7434b
AL
159 macaddr[0], macaddr[1], macaddr[2],
160 macaddr[3], macaddr[4], macaddr[5]);
161}
162
76d32cba
GH
163void qemu_macaddr_default_if_unset(MACAddr *macaddr)
164{
165 static int index = 0;
166 static const MACAddr zero = { .a = { 0,0,0,0,0,0 } };
167
168 if (memcmp(macaddr, &zero, sizeof(zero)) != 0)
169 return;
170 macaddr->a[0] = 0x52;
171 macaddr->a[1] = 0x54;
172 macaddr->a[2] = 0x00;
173 macaddr->a[3] = 0x12;
174 macaddr->a[4] = 0x34;
175 macaddr->a[5] = 0x56 + index++;
176}
177
d33d93b2
SH
178/**
179 * Generate a name for net client
180 *
c963530a 181 * Only net clients created with the legacy -net option and NICs need this.
d33d93b2 182 */
35277d14 183static char *assign_name(NetClientState *nc1, const char *model)
676cff29 184{
35277d14 185 NetClientState *nc;
676cff29
AL
186 int id = 0;
187
35277d14
SH
188 QTAILQ_FOREACH(nc, &net_clients, next) {
189 if (nc == nc1) {
d33d93b2 190 continue;
5610c3aa 191 }
c963530a 192 if (strcmp(nc->model, model) == 0) {
53e51d85
MA
193 id++;
194 }
195 }
196
4bf2c138 197 return g_strdup_printf("%s.%d", model, id);
676cff29
AL
198}
199
f7860455
JW
200static void qemu_net_client_destructor(NetClientState *nc)
201{
202 g_free(nc);
203}
204
18a1541a
JW
205static void qemu_net_client_setup(NetClientState *nc,
206 NetClientInfo *info,
207 NetClientState *peer,
208 const char *model,
f7860455
JW
209 const char *name,
210 NetClientDestructor *destructor)
63a01ef8 211{
35277d14
SH
212 nc->info = info;
213 nc->model = g_strdup(model);
45460d1a 214 if (name) {
35277d14 215 nc->name = g_strdup(name);
45460d1a 216 } else {
35277d14 217 nc->name = assign_name(nc, model);
45460d1a 218 }
5610c3aa 219
ab5f3f84
SH
220 if (peer) {
221 assert(!peer->peer);
35277d14
SH
222 nc->peer = peer;
223 peer->peer = nc;
d80b9fc6 224 }
35277d14 225 QTAILQ_INSERT_TAIL(&net_clients, nc, next);
63a01ef8 226
067404be 227 nc->incoming_queue = qemu_new_net_queue(nc);
f7860455 228 nc->destructor = destructor;
18a1541a
JW
229}
230
231NetClientState *qemu_new_net_client(NetClientInfo *info,
232 NetClientState *peer,
233 const char *model,
234 const char *name)
235{
236 NetClientState *nc;
237
238 assert(info->size >= sizeof(NetClientState));
239
240 nc = g_malloc0(info->size);
f7860455
JW
241 qemu_net_client_setup(nc, info, peer, model, name,
242 qemu_net_client_destructor);
18a1541a 243
35277d14 244 return nc;
63a01ef8
AL
245}
246
ebef2c09
MM
247NICState *qemu_new_nic(NetClientInfo *info,
248 NICConf *conf,
249 const char *model,
250 const char *name,
251 void *opaque)
252{
1ceef9f2 253 NetClientState **peers = conf->peers.ncs;
ebef2c09 254 NICState *nic;
575a1c0e 255 int i, queues = MAX(1, conf->peers.queues);
ebef2c09 256
2be64a68 257 assert(info->type == NET_CLIENT_OPTIONS_KIND_NIC);
ebef2c09
MM
258 assert(info->size >= sizeof(NICState));
259
f6b26cf2
JW
260 nic = g_malloc0(info->size + sizeof(NetClientState) * queues);
261 nic->ncs = (void *)nic + info->size;
ebef2c09
MM
262 nic->conf = conf;
263 nic->opaque = opaque;
264
f6b26cf2
JW
265 for (i = 0; i < queues; i++) {
266 qemu_net_client_setup(&nic->ncs[i], info, peers[i], model, name,
1ceef9f2
JW
267 NULL);
268 nic->ncs[i].queue_index = i;
269 }
270
ebef2c09
MM
271 return nic;
272}
273
1ceef9f2
JW
274NetClientState *qemu_get_subqueue(NICState *nic, int queue_index)
275{
f6b26cf2 276 return nic->ncs + queue_index;
1ceef9f2
JW
277}
278
b356f76d
JW
279NetClientState *qemu_get_queue(NICState *nic)
280{
1ceef9f2 281 return qemu_get_subqueue(nic, 0);
b356f76d
JW
282}
283
cc1f0f45
JW
284NICState *qemu_get_nic(NetClientState *nc)
285{
1ceef9f2
JW
286 NetClientState *nc0 = nc - nc->queue_index;
287
f6b26cf2 288 return (NICState *)((void *)nc0 - nc->info->size);
cc1f0f45
JW
289}
290
291void *qemu_get_nic_opaque(NetClientState *nc)
292{
293 NICState *nic = qemu_get_nic(nc);
294
295 return nic->opaque;
296}
297
b20c6b9e 298static void qemu_cleanup_net_client(NetClientState *nc)
63a01ef8 299{
35277d14 300 QTAILQ_REMOVE(&net_clients, nc, next);
63a01ef8 301
cc2a9043
AF
302 if (nc->info->cleanup) {
303 nc->info->cleanup(nc);
304 }
a083a89d 305}
5610c3aa 306
b20c6b9e 307static void qemu_free_net_client(NetClientState *nc)
a083a89d 308{
067404be
JK
309 if (nc->incoming_queue) {
310 qemu_del_net_queue(nc->incoming_queue);
a005d073 311 }
35277d14
SH
312 if (nc->peer) {
313 nc->peer->peer = NULL;
a083a89d 314 }
35277d14
SH
315 g_free(nc->name);
316 g_free(nc->model);
f7860455
JW
317 if (nc->destructor) {
318 nc->destructor(nc);
319 }
63a01ef8
AL
320}
321
b20c6b9e 322void qemu_del_net_client(NetClientState *nc)
a083a89d 323{
1ceef9f2
JW
324 NetClientState *ncs[MAX_QUEUE_NUM];
325 int queues, i;
326
327 /* If the NetClientState belongs to a multiqueue backend, we will change all
328 * other NetClientStates also.
329 */
330 queues = qemu_find_net_clients_except(nc->name, ncs,
331 NET_CLIENT_OPTIONS_KIND_NIC,
332 MAX_QUEUE_NUM);
333 assert(queues != 0);
334
a083a89d 335 /* If there is a peer NIC, delete and cleanup client, but do not free. */
35277d14 336 if (nc->peer && nc->peer->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
cc1f0f45 337 NICState *nic = qemu_get_nic(nc->peer);
a083a89d
MT
338 if (nic->peer_deleted) {
339 return;
340 }
341 nic->peer_deleted = true;
1ceef9f2
JW
342
343 for (i = 0; i < queues; i++) {
344 ncs[i]->peer->link_down = true;
345 }
346
35277d14
SH
347 if (nc->peer->info->link_status_changed) {
348 nc->peer->info->link_status_changed(nc->peer);
a083a89d 349 }
1ceef9f2
JW
350
351 for (i = 0; i < queues; i++) {
352 qemu_cleanup_net_client(ncs[i]);
353 }
354
a083a89d
MT
355 return;
356 }
357
948ecf21
JW
358 assert(nc->info->type != NET_CLIENT_OPTIONS_KIND_NIC);
359
1ceef9f2
JW
360 for (i = 0; i < queues; i++) {
361 qemu_cleanup_net_client(ncs[i]);
362 qemu_free_net_client(ncs[i]);
363 }
948ecf21
JW
364}
365
366void qemu_del_nic(NICState *nic)
367{
575a1c0e 368 int i, queues = MAX(nic->conf->peers.queues, 1);
1ceef9f2 369
a083a89d 370 /* If this is a peer NIC and peer has already been deleted, free it now. */
1ceef9f2
JW
371 if (nic->peer_deleted) {
372 for (i = 0; i < queues; i++) {
373 qemu_free_net_client(qemu_get_subqueue(nic, i)->peer);
1a609520
JK
374 }
375 }
1a609520 376
1ceef9f2
JW
377 for (i = queues - 1; i >= 0; i--) {
378 NetClientState *nc = qemu_get_subqueue(nic, i);
379
380 qemu_cleanup_net_client(nc);
381 qemu_free_net_client(nc);
382 }
f6b26cf2
JW
383
384 g_free(nic);
1a609520
JK
385}
386
57f9ef17
MM
387void qemu_foreach_nic(qemu_nic_foreach func, void *opaque)
388{
4e68f7a0 389 NetClientState *nc;
57f9ef17 390
94878994 391 QTAILQ_FOREACH(nc, &net_clients, next) {
2be64a68 392 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
1ceef9f2
JW
393 if (nc->queue_index == 0) {
394 func(qemu_get_nic(nc), opaque);
395 }
57f9ef17
MM
396 }
397 }
57f9ef17
MM
398}
399
d6085e3a 400bool qemu_has_ufo(NetClientState *nc)
1f55ac45 401{
d6085e3a 402 if (!nc || !nc->info->has_ufo) {
1f55ac45
VM
403 return false;
404 }
405
d6085e3a 406 return nc->info->has_ufo(nc);
1f55ac45
VM
407}
408
d6085e3a 409bool qemu_has_vnet_hdr(NetClientState *nc)
1f55ac45 410{
d6085e3a 411 if (!nc || !nc->info->has_vnet_hdr) {
1f55ac45
VM
412 return false;
413 }
414
d6085e3a 415 return nc->info->has_vnet_hdr(nc);
1f55ac45
VM
416}
417
d6085e3a 418bool qemu_has_vnet_hdr_len(NetClientState *nc, int len)
1f55ac45 419{
d6085e3a 420 if (!nc || !nc->info->has_vnet_hdr_len) {
1f55ac45
VM
421 return false;
422 }
423
d6085e3a 424 return nc->info->has_vnet_hdr_len(nc, len);
1f55ac45
VM
425}
426
d6085e3a 427void qemu_using_vnet_hdr(NetClientState *nc, bool enable)
1f55ac45 428{
d6085e3a 429 if (!nc || !nc->info->using_vnet_hdr) {
1f55ac45
VM
430 return;
431 }
432
d6085e3a 433 nc->info->using_vnet_hdr(nc, enable);
1f55ac45
VM
434}
435
d6085e3a 436void qemu_set_offload(NetClientState *nc, int csum, int tso4, int tso6,
1f55ac45
VM
437 int ecn, int ufo)
438{
d6085e3a 439 if (!nc || !nc->info->set_offload) {
1f55ac45
VM
440 return;
441 }
442
d6085e3a 443 nc->info->set_offload(nc, csum, tso4, tso6, ecn, ufo);
1f55ac45
VM
444}
445
d6085e3a 446void qemu_set_vnet_hdr_len(NetClientState *nc, int len)
1f55ac45 447{
d6085e3a 448 if (!nc || !nc->info->set_vnet_hdr_len) {
1f55ac45
VM
449 return;
450 }
451
d6085e3a 452 nc->info->set_vnet_hdr_len(nc, len);
1f55ac45
VM
453}
454
4e68f7a0 455int qemu_can_send_packet(NetClientState *sender)
63a01ef8 456{
e1d64c08
HZ
457 int vm_running = runstate_is_running();
458
459 if (!vm_running) {
460 return 0;
461 }
462
a005d073 463 if (!sender->peer) {
d80b9fc6
MM
464 return 1;
465 }
466
a005d073
SH
467 if (sender->peer->receive_disabled) {
468 return 0;
469 } else if (sender->peer->info->can_receive &&
470 !sender->peer->info->can_receive(sender->peer)) {
471 return 0;
63a01ef8 472 }
60c07d93 473 return 1;
63a01ef8
AL
474}
475
86a77c38
ZYW
476ssize_t qemu_deliver_packet(NetClientState *sender,
477 unsigned flags,
478 const uint8_t *data,
479 size_t size,
480 void *opaque)
9a6ecb30 481{
35277d14 482 NetClientState *nc = opaque;
893379ef 483 ssize_t ret;
9a6ecb30 484
35277d14 485 if (nc->link_down) {
9a6ecb30
MM
486 return size;
487 }
488
35277d14 489 if (nc->receive_disabled) {
893379ef
MM
490 return 0;
491 }
492
35277d14
SH
493 if (flags & QEMU_NET_PACKET_FLAG_RAW && nc->info->receive_raw) {
494 ret = nc->info->receive_raw(nc, data, size);
893379ef 495 } else {
35277d14 496 ret = nc->info->receive(nc, data, size);
893379ef
MM
497 }
498
499 if (ret == 0) {
35277d14 500 nc->receive_disabled = 1;
b9259652 501 }
893379ef
MM
502
503 return ret;
9a6ecb30
MM
504}
505
35277d14 506void qemu_purge_queued_packets(NetClientState *nc)
8cad5516 507{
35277d14 508 if (!nc->peer) {
d80b9fc6 509 return;
9a6ecb30 510 }
d80b9fc6 511
067404be 512 qemu_net_queue_purge(nc->peer->incoming_queue, nc);
8cad5516
MM
513}
514
ca77d85e
MT
515static
516void qemu_flush_or_purge_queued_packets(NetClientState *nc, bool purge)
e94667b9 517{
35277d14 518 nc->receive_disabled = 0;
9a6ecb30 519
199ee608
LR
520 if (nc->peer && nc->peer->info->type == NET_CLIENT_OPTIONS_KIND_HUBPORT) {
521 if (net_hub_flush(nc->peer)) {
522 qemu_notify_event();
523 }
199ee608 524 }
067404be 525 if (qemu_net_queue_flush(nc->incoming_queue)) {
987a9b48
PB
526 /* We emptied the queue successfully, signal to the IO thread to repoll
527 * the file descriptor (for tap, for example).
528 */
529 qemu_notify_event();
ca77d85e
MT
530 } else if (purge) {
531 /* Unable to empty the queue, purge remaining packets */
532 qemu_net_queue_purge(nc->incoming_queue, nc);
987a9b48 533 }
e94667b9
MM
534}
535
ca77d85e
MT
536void qemu_flush_queued_packets(NetClientState *nc)
537{
538 qemu_flush_or_purge_queued_packets(nc, false);
539}
540
4e68f7a0 541static ssize_t qemu_send_packet_async_with_flags(NetClientState *sender,
ca77d175
MM
542 unsigned flags,
543 const uint8_t *buf, int size,
544 NetPacketSent *sent_cb)
764a4d1d 545{
9a6ecb30 546 NetQueue *queue;
436e5e53 547
63a01ef8 548#ifdef DEBUG_NET
d80b9fc6 549 printf("qemu_send_packet_async:\n");
63a01ef8
AL
550 hex_dump(stdout, buf, size);
551#endif
f3b6c7fc 552
a005d073 553 if (sender->link_down || !sender->peer) {
9a6ecb30
MM
554 return size;
555 }
556
067404be 557 queue = sender->peer->incoming_queue;
9a6ecb30 558
ca77d175
MM
559 return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb);
560}
561
4e68f7a0 562ssize_t qemu_send_packet_async(NetClientState *sender,
ca77d175
MM
563 const uint8_t *buf, int size,
564 NetPacketSent *sent_cb)
565{
566 return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE,
567 buf, size, sent_cb);
f3b6c7fc
MM
568}
569
35277d14 570void qemu_send_packet(NetClientState *nc, const uint8_t *buf, int size)
f3b6c7fc 571{
35277d14 572 qemu_send_packet_async(nc, buf, size, NULL);
63a01ef8
AL
573}
574
35277d14 575ssize_t qemu_send_packet_raw(NetClientState *nc, const uint8_t *buf, int size)
ca77d175 576{
35277d14 577 return qemu_send_packet_async_with_flags(nc, QEMU_NET_PACKET_FLAG_RAW,
ca77d175
MM
578 buf, size, NULL);
579}
580
35277d14 581static ssize_t nc_sendv_compat(NetClientState *nc, const struct iovec *iov,
fbe78f4f
AL
582 int iovcnt)
583{
d32fcad3 584 uint8_t buffer[NET_BUFSIZE];
ce053661 585 size_t offset;
fbe78f4f 586
dcf6f5e1 587 offset = iov_to_buf(iov, iovcnt, 0, buffer, sizeof(buffer));
fbe78f4f 588
35277d14 589 return nc->info->receive(nc, buffer, offset);
fbe78f4f
AL
590}
591
86a77c38
ZYW
592ssize_t qemu_deliver_packet_iov(NetClientState *sender,
593 unsigned flags,
594 const struct iovec *iov,
595 int iovcnt,
596 void *opaque)
9a6ecb30 597{
35277d14 598 NetClientState *nc = opaque;
c67f5dc1 599 int ret;
9a6ecb30 600
35277d14 601 if (nc->link_down) {
ce053661 602 return iov_size(iov, iovcnt);
9a6ecb30
MM
603 }
604
c67f5dc1
SH
605 if (nc->receive_disabled) {
606 return 0;
607 }
608
35277d14 609 if (nc->info->receive_iov) {
c67f5dc1 610 ret = nc->info->receive_iov(nc, iov, iovcnt);
9a6ecb30 611 } else {
c67f5dc1
SH
612 ret = nc_sendv_compat(nc, iov, iovcnt);
613 }
614
615 if (ret == 0) {
616 nc->receive_disabled = 1;
e94667b9 617 }
c67f5dc1
SH
618
619 return ret;
e94667b9
MM
620}
621
4e68f7a0 622ssize_t qemu_sendv_packet_async(NetClientState *sender,
f3b6c7fc
MM
623 const struct iovec *iov, int iovcnt,
624 NetPacketSent *sent_cb)
e94667b9 625{
9a6ecb30
MM
626 NetQueue *queue;
627
a005d073 628 if (sender->link_down || !sender->peer) {
ce053661 629 return iov_size(iov, iovcnt);
e94667b9
MM
630 }
631
067404be 632 queue = sender->peer->incoming_queue;
9a6ecb30 633
c0b8e49c
MM
634 return qemu_net_queue_send_iov(queue, sender,
635 QEMU_NET_PACKET_FLAG_NONE,
636 iov, iovcnt, sent_cb);
fbe78f4f
AL
637}
638
f3b6c7fc 639ssize_t
35277d14 640qemu_sendv_packet(NetClientState *nc, const struct iovec *iov, int iovcnt)
f3b6c7fc 641{
35277d14 642 return qemu_sendv_packet_async(nc, iov, iovcnt, NULL);
63a01ef8
AL
643}
644
4e68f7a0 645NetClientState *qemu_find_netdev(const char *id)
5869c4d5 646{
35277d14 647 NetClientState *nc;
5869c4d5 648
35277d14
SH
649 QTAILQ_FOREACH(nc, &net_clients, next) {
650 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC)
85dde9a9 651 continue;
35277d14
SH
652 if (!strcmp(nc->name, id)) {
653 return nc;
5869c4d5
MM
654 }
655 }
656
657 return NULL;
658}
659
6c51ae73
JW
660int qemu_find_net_clients_except(const char *id, NetClientState **ncs,
661 NetClientOptionsKind type, int max)
662{
663 NetClientState *nc;
664 int ret = 0;
665
666 QTAILQ_FOREACH(nc, &net_clients, next) {
667 if (nc->info->type == type) {
668 continue;
669 }
40d19394 670 if (!id || !strcmp(nc->name, id)) {
6c51ae73
JW
671 if (ret < max) {
672 ncs[ret] = nc;
673 }
674 ret++;
675 }
676 }
677
678 return ret;
679}
680
7697079b
AL
681static int nic_get_free_idx(void)
682{
683 int index;
684
685 for (index = 0; index < MAX_NICS; index++)
686 if (!nd_table[index].used)
687 return index;
688 return -1;
689}
690
07caea31
MA
691int qemu_show_nic_models(const char *arg, const char *const *models)
692{
693 int i;
694
c8057f95 695 if (!arg || !is_help_option(arg)) {
07caea31 696 return 0;
c8057f95 697 }
07caea31
MA
698
699 fprintf(stderr, "qemu: Supported NIC models: ");
700 for (i = 0 ; models[i]; i++)
701 fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n');
702 return 1;
703}
704
d07f22c5
AL
705void qemu_check_nic_model(NICInfo *nd, const char *model)
706{
707 const char *models[2];
708
709 models[0] = model;
710 models[1] = NULL;
711
07caea31
MA
712 if (qemu_show_nic_models(nd->model, models))
713 exit(0);
714 if (qemu_find_nic_model(nd, models, model) < 0)
715 exit(1);
d07f22c5
AL
716}
717
07caea31
MA
718int qemu_find_nic_model(NICInfo *nd, const char * const *models,
719 const char *default_model)
d07f22c5 720{
07caea31 721 int i;
d07f22c5
AL
722
723 if (!nd->model)
7267c094 724 nd->model = g_strdup(default_model);
d07f22c5 725
07caea31
MA
726 for (i = 0 ; models[i]; i++) {
727 if (strcmp(nd->model, models[i]) == 0)
728 return i;
d07f22c5
AL
729 }
730
6daf194d 731 error_report("Unsupported NIC model: %s", nd->model);
07caea31 732 return -1;
d07f22c5
AL
733}
734
1a0c0958 735static int net_init_nic(const NetClientOptions *opts, const char *name,
4e68f7a0 736 NetClientState *peer)
f83c6e10
MM
737{
738 int idx;
739 NICInfo *nd;
2456f36f
LE
740 const NetLegacyNicOptions *nic;
741
742 assert(opts->kind == NET_CLIENT_OPTIONS_KIND_NIC);
743 nic = opts->nic;
f83c6e10
MM
744
745 idx = nic_get_free_idx();
746 if (idx == -1 || nb_nics >= MAX_NICS) {
1ecda02b 747 error_report("Too Many NICs");
f83c6e10
MM
748 return -1;
749 }
750
751 nd = &nd_table[idx];
752
753 memset(nd, 0, sizeof(*nd));
754
2456f36f
LE
755 if (nic->has_netdev) {
756 nd->netdev = qemu_find_netdev(nic->netdev);
5869c4d5 757 if (!nd->netdev) {
2456f36f 758 error_report("netdev '%s' not found", nic->netdev);
5869c4d5
MM
759 return -1;
760 }
761 } else {
d33d93b2
SH
762 assert(peer);
763 nd->netdev = peer;
5869c4d5 764 }
c64f50d1 765 nd->name = g_strdup(name);
2456f36f
LE
766 if (nic->has_model) {
767 nd->model = g_strdup(nic->model);
f83c6e10 768 }
2456f36f
LE
769 if (nic->has_addr) {
770 nd->devaddr = g_strdup(nic->addr);
f83c6e10
MM
771 }
772
2456f36f
LE
773 if (nic->has_macaddr &&
774 net_parse_macaddr(nd->macaddr.a, nic->macaddr) < 0) {
1ecda02b 775 error_report("invalid syntax for ethernet address");
f83c6e10
MM
776 return -1;
777 }
d60b20cf
DK
778 if (nic->has_macaddr &&
779 is_multicast_ether_addr(nd->macaddr.a)) {
780 error_report("NIC cannot have multicast MAC address (odd 1st byte)");
781 return -1;
782 }
6eed1856 783 qemu_macaddr_default_if_unset(&nd->macaddr);
f83c6e10 784
2456f36f
LE
785 if (nic->has_vectors) {
786 if (nic->vectors > 0x7ffffff) {
787 error_report("invalid # of vectors: %"PRIu32, nic->vectors);
788 return -1;
789 }
790 nd->nvectors = nic->vectors;
791 } else {
792 nd->nvectors = DEV_NVECTORS_UNSPECIFIED;
f83c6e10
MM
793 }
794
795 nd->used = 1;
f83c6e10
MM
796 nb_nics++;
797
798 return idx;
799}
800
6687b79d
LE
801
802static int (* const net_client_init_fun[NET_CLIENT_OPTIONS_KIND_MAX])(
1a0c0958 803 const NetClientOptions *opts,
6687b79d 804 const char *name,
4e68f7a0 805 NetClientState *peer) = {
f6c874e3 806 [NET_CLIENT_OPTIONS_KIND_NIC] = net_init_nic,
ec302ffd 807#ifdef CONFIG_SLIRP
f6c874e3 808 [NET_CLIENT_OPTIONS_KIND_USER] = net_init_slirp,
2944e4ec 809#endif
f6c874e3
SH
810 [NET_CLIENT_OPTIONS_KIND_TAP] = net_init_tap,
811 [NET_CLIENT_OPTIONS_KIND_SOCKET] = net_init_socket,
dd51058d 812#ifdef CONFIG_VDE
f6c874e3 813 [NET_CLIENT_OPTIONS_KIND_VDE] = net_init_vde,
58952137
VM
814#endif
815#ifdef CONFIG_NETMAP
816 [NET_CLIENT_OPTIONS_KIND_NETMAP] = net_init_netmap,
dd51058d 817#endif
f6c874e3 818 [NET_CLIENT_OPTIONS_KIND_DUMP] = net_init_dump,
2944e4ec 819#ifdef CONFIG_NET_BRIDGE
f6c874e3 820 [NET_CLIENT_OPTIONS_KIND_BRIDGE] = net_init_bridge,
6687b79d 821#endif
f6c874e3 822 [NET_CLIENT_OPTIONS_KIND_HUBPORT] = net_init_hubport,
03ce5744
NN
823#ifdef CONFIG_VHOST_NET_USED
824 [NET_CLIENT_OPTIONS_KIND_VHOST_USER] = net_init_vhost_user,
825#endif
015a33bd 826#ifdef CONFIG_L2TPV3
3fb69aa1
AI
827 [NET_CLIENT_OPTIONS_KIND_L2TPV3] = net_init_l2tpv3,
828#endif
f83c6e10
MM
829};
830
6687b79d 831
1a0c0958 832static int net_client_init1(const void *object, int is_netdev, Error **errp)
f83c6e10 833{
6687b79d
LE
834 union {
835 const Netdev *netdev;
836 const NetLegacy *net;
837 } u;
838 const NetClientOptions *opts;
6d952ebe 839 const char *name;
f83c6e10 840
5294e2c7 841 if (is_netdev) {
6687b79d
LE
842 u.netdev = object;
843 opts = u.netdev->opts;
844 name = u.netdev->id;
845
846 switch (opts->kind) {
f6b134ac 847#ifdef CONFIG_SLIRP
6687b79d 848 case NET_CLIENT_OPTIONS_KIND_USER:
f6b134ac 849#endif
6687b79d
LE
850 case NET_CLIENT_OPTIONS_KIND_TAP:
851 case NET_CLIENT_OPTIONS_KIND_SOCKET:
f6b134ac 852#ifdef CONFIG_VDE
6687b79d
LE
853 case NET_CLIENT_OPTIONS_KIND_VDE:
854#endif
58952137
VM
855#ifdef CONFIG_NETMAP
856 case NET_CLIENT_OPTIONS_KIND_NETMAP:
857#endif
6687b79d
LE
858#ifdef CONFIG_NET_BRIDGE
859 case NET_CLIENT_OPTIONS_KIND_BRIDGE:
f6b134ac 860#endif
f6c874e3 861 case NET_CLIENT_OPTIONS_KIND_HUBPORT:
03ce5744
NN
862#ifdef CONFIG_VHOST_NET_USED
863 case NET_CLIENT_OPTIONS_KIND_VHOST_USER:
3fb69aa1 864#endif
015a33bd 865#ifdef CONFIG_L2TPV3
3fb69aa1 866 case NET_CLIENT_OPTIONS_KIND_L2TPV3:
03ce5744 867#endif
6687b79d
LE
868 break;
869
870 default:
4559a1db
LC
871 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "type",
872 "a netdev backend type");
f6b134ac
MM
873 return -1;
874 }
6687b79d
LE
875 } else {
876 u.net = object;
877 opts = u.net->opts;
878 /* missing optional values have been initialized to "all bits zero" */
879 name = u.net->has_id ? u.net->id : u.net->name;
880 }
f6b134ac 881
6687b79d 882 if (net_client_init_fun[opts->kind]) {
4e68f7a0 883 NetClientState *peer = NULL;
6687b79d
LE
884
885 /* Do not add to a vlan if it's a -netdev or a nic with a netdev=
886 * parameter. */
887 if (!is_netdev &&
888 (opts->kind != NET_CLIENT_OPTIONS_KIND_NIC ||
889 !opts->nic->has_netdev)) {
d33d93b2 890 peer = net_hub_add_port(u.net->has_vlan ? u.net->vlan : 0, NULL);
f6b134ac 891 }
6687b79d 892
d33d93b2 893 if (net_client_init_fun[opts->kind](opts, name, peer) < 0) {
6687b79d
LE
894 /* TODO push error reporting into init() methods */
895 error_set(errp, QERR_DEVICE_INIT_FAILED,
896 NetClientOptionsKind_lookup[opts->kind]);
f6b134ac
MM
897 return -1;
898 }
899 }
6687b79d
LE
900 return 0;
901}
902
f6b134ac 903
6687b79d
LE
904static void net_visit(Visitor *v, int is_netdev, void **object, Error **errp)
905{
906 if (is_netdev) {
907 visit_type_Netdev(v, (Netdev **)object, NULL, errp);
908 } else {
909 visit_type_NetLegacy(v, (NetLegacy **)object, NULL, errp);
6d952ebe 910 }
6687b79d 911}
6d952ebe 912
f6b134ac 913
6687b79d
LE
914int net_client_init(QemuOpts *opts, int is_netdev, Error **errp)
915{
916 void *object = NULL;
917 Error *err = NULL;
918 int ret = -1;
f83c6e10 919
6687b79d
LE
920 {
921 OptsVisitor *ov = opts_visitor_new(opts);
f6b134ac 922
6687b79d
LE
923 net_visit(opts_get_visitor(ov), is_netdev, &object, &err);
924 opts_visitor_cleanup(ov);
f83c6e10
MM
925 }
926
6687b79d 927 if (!err) {
1a0c0958 928 ret = net_client_init1(object, is_netdev, &err);
6687b79d
LE
929 }
930
931 if (object) {
932 QapiDeallocVisitor *dv = qapi_dealloc_visitor_new();
933
934 net_visit(qapi_dealloc_get_visitor(dv), is_netdev, &object, NULL);
935 qapi_dealloc_visitor_cleanup(dv);
936 }
937
938 error_propagate(errp, err);
939 return ret;
f83c6e10
MM
940}
941
6687b79d 942
6f338c34
AL
943static int net_host_check_device(const char *device)
944{
945 int i;
84007e81
HB
946 for (i = 0; host_net_devices[i]; i++) {
947 if (!strncmp(host_net_devices[i], device,
948 strlen(host_net_devices[i]))) {
6f338c34 949 return 1;
84007e81 950 }
6f338c34
AL
951 }
952
953 return 0;
954}
955
3e5a50d6 956void hmp_host_net_add(Monitor *mon, const QDict *qdict)
6f338c34 957{
f18c16de 958 const char *device = qdict_get_str(qdict, "device");
7f1c9d20 959 const char *opts_str = qdict_get_try_str(qdict, "opts");
4559a1db 960 Error *local_err = NULL;
7f1c9d20 961 QemuOpts *opts;
f18c16de 962
6f338c34 963 if (!net_host_check_device(device)) {
376253ec 964 monitor_printf(mon, "invalid host network device %s\n", device);
6f338c34
AL
965 return;
966 }
7f1c9d20 967
3329f07b 968 opts = qemu_opts_parse(qemu_find_opts("net"), opts_str ? opts_str : "", 0);
7f1c9d20 969 if (!opts) {
7f1c9d20
MM
970 return;
971 }
972
973 qemu_opt_set(opts, "type", device);
974
4559a1db 975 net_client_init(opts, 0, &local_err);
84d18f06 976 if (local_err) {
12d0cc2d 977 error_report_err(local_err);
5c8be678
AL
978 monitor_printf(mon, "adding host network device %s failed\n", device);
979 }
6f338c34
AL
980}
981
3e5a50d6 982void hmp_host_net_remove(Monitor *mon, const QDict *qdict)
6f338c34 983{
35277d14 984 NetClientState *nc;
f18c16de
LC
985 int vlan_id = qdict_get_int(qdict, "vlan_id");
986 const char *device = qdict_get_str(qdict, "device");
6f338c34 987
35277d14
SH
988 nc = net_hub_find_client_by_name(vlan_id, device);
989 if (!nc) {
86e11772
HB
990 error_report("Host network device '%s' on hub '%d' not found",
991 device, vlan_id);
6f338c34
AL
992 return;
993 }
35277d14 994 if (!net_host_check_device(nc->model)) {
86e11772 995 error_report("invalid host network device '%s'", device);
e8f1f9db
AL
996 return;
997 }
64a55d60
JW
998
999 qemu_del_net_client(nc->peer);
b20c6b9e 1000 qemu_del_net_client(nc);
6f338c34
AL
1001}
1002
928059a3
LC
1003void netdev_add(QemuOpts *opts, Error **errp)
1004{
1005 net_client_init(opts, 1, errp);
1006}
1007
1008int qmp_netdev_add(Monitor *mon, const QDict *qdict, QObject **ret)
ae82d324 1009{
4e89978e 1010 Error *local_err = NULL;
928059a3 1011 QemuOptsList *opts_list;
ae82d324 1012 QemuOpts *opts;
ae82d324 1013
928059a3 1014 opts_list = qemu_find_opts_err("netdev", &local_err);
84d18f06 1015 if (local_err) {
928059a3 1016 goto exit_err;
ae82d324
MA
1017 }
1018
928059a3 1019 opts = qemu_opts_from_qdict(opts_list, qdict, &local_err);
84d18f06 1020 if (local_err) {
928059a3
LC
1021 goto exit_err;
1022 }
1023
1024 netdev_add(opts, &local_err);
84d18f06 1025 if (local_err) {
410cbafe 1026 qemu_opts_del(opts);
928059a3 1027 goto exit_err;
410cbafe
YT
1028 }
1029
928059a3
LC
1030 return 0;
1031
1032exit_err:
1033 qerror_report_err(local_err);
1034 error_free(local_err);
1035 return -1;
ae82d324
MA
1036}
1037
5f964155 1038void qmp_netdev_del(const char *id, Error **errp)
ae82d324 1039{
35277d14 1040 NetClientState *nc;
645c9496 1041 QemuOpts *opts;
ae82d324 1042
35277d14
SH
1043 nc = qemu_find_netdev(id);
1044 if (!nc) {
5f964155
LC
1045 error_set(errp, QERR_DEVICE_NOT_FOUND, id);
1046 return;
ae82d324 1047 }
5f964155 1048
645c9496
SH
1049 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), id);
1050 if (!opts) {
1051 error_setg(errp, "Device '%s' is not a netdev", id);
1052 return;
1053 }
1054
b20c6b9e 1055 qemu_del_net_client(nc);
645c9496 1056 qemu_opts_del(opts);
ae82d324
MA
1057}
1058
1a859593 1059void print_net_client(Monitor *mon, NetClientState *nc)
44e798d3 1060{
1ceef9f2
JW
1061 monitor_printf(mon, "%s: index=%d,type=%s,%s\n", nc->name,
1062 nc->queue_index,
1063 NetClientOptionsKind_lookup[nc->info->type],
1064 nc->info_str);
44e798d3
JK
1065}
1066
b1be4280
AK
1067RxFilterInfoList *qmp_query_rx_filter(bool has_name, const char *name,
1068 Error **errp)
1069{
1070 NetClientState *nc;
1071 RxFilterInfoList *filter_list = NULL, *last_entry = NULL;
1072
1073 QTAILQ_FOREACH(nc, &net_clients, next) {
1074 RxFilterInfoList *entry;
1075 RxFilterInfo *info;
1076
1077 if (has_name && strcmp(nc->name, name) != 0) {
1078 continue;
1079 }
1080
1081 /* only query rx-filter information of NIC */
1082 if (nc->info->type != NET_CLIENT_OPTIONS_KIND_NIC) {
1083 if (has_name) {
1084 error_setg(errp, "net client(%s) isn't a NIC", name);
9083da1d 1085 return NULL;
b1be4280
AK
1086 }
1087 continue;
1088 }
1089
1090 if (nc->info->query_rx_filter) {
1091 info = nc->info->query_rx_filter(nc);
1092 entry = g_malloc0(sizeof(*entry));
1093 entry->value = info;
1094
1095 if (!filter_list) {
1096 filter_list = entry;
1097 } else {
1098 last_entry->next = entry;
1099 }
1100 last_entry = entry;
1101 } else if (has_name) {
1102 error_setg(errp, "net client(%s) doesn't support"
1103 " rx-filter querying", name);
9083da1d 1104 return NULL;
b1be4280 1105 }
638fb141
MA
1106
1107 if (has_name) {
1108 break;
1109 }
b1be4280
AK
1110 }
1111
9083da1d 1112 if (filter_list == NULL && has_name) {
b1be4280
AK
1113 error_setg(errp, "invalid net client name: %s", name);
1114 }
1115
1116 return filter_list;
1117}
1118
1ce6be24 1119void hmp_info_network(Monitor *mon, const QDict *qdict)
63a01ef8 1120{
35277d14 1121 NetClientState *nc, *peer;
2be64a68 1122 NetClientOptionsKind type;
63a01ef8 1123
1a859593
ZYW
1124 net_hub_info(mon);
1125
35277d14
SH
1126 QTAILQ_FOREACH(nc, &net_clients, next) {
1127 peer = nc->peer;
1128 type = nc->info->type;
5610c3aa 1129
1a859593
ZYW
1130 /* Skip if already printed in hub info */
1131 if (net_hub_id_for_client(nc, NULL) == 0) {
1132 continue;
5610c3aa 1133 }
1a859593 1134
2be64a68 1135 if (!peer || type == NET_CLIENT_OPTIONS_KIND_NIC) {
35277d14 1136 print_net_client(mon, nc);
19061e63 1137 } /* else it's a netdev connected to a NIC, printed with the NIC */
2be64a68 1138 if (peer && type == NET_CLIENT_OPTIONS_KIND_NIC) {
1a859593 1139 monitor_printf(mon, " \\ ");
44e798d3 1140 print_net_client(mon, peer);
a0104e0e 1141 }
a0104e0e 1142 }
63a01ef8
AL
1143}
1144
4b37156c 1145void qmp_set_link(const char *name, bool up, Error **errp)
436e5e53 1146{
1ceef9f2
JW
1147 NetClientState *ncs[MAX_QUEUE_NUM];
1148 NetClientState *nc;
1149 int queues, i;
436e5e53 1150
1ceef9f2
JW
1151 queues = qemu_find_net_clients_except(name, ncs,
1152 NET_CLIENT_OPTIONS_KIND_MAX,
1153 MAX_QUEUE_NUM);
1154
1155 if (queues == 0) {
4b37156c
LC
1156 error_set(errp, QERR_DEVICE_NOT_FOUND, name);
1157 return;
436e5e53 1158 }
1ceef9f2 1159 nc = ncs[0];
436e5e53 1160
1ceef9f2
JW
1161 for (i = 0; i < queues; i++) {
1162 ncs[i]->link_down = !up;
1163 }
436e5e53 1164
35277d14
SH
1165 if (nc->info->link_status_changed) {
1166 nc->info->link_status_changed(nc);
665a3b07 1167 }
ab1cbe1c 1168
02d38fcb
VY
1169 if (nc->peer) {
1170 /* Change peer link only if the peer is NIC and then notify peer.
1171 * If the peer is a HUBPORT or a backend, we do not change the
1172 * link status.
1173 *
1174 * This behavior is compatible with qemu vlans where there could be
1175 * multiple clients that can still communicate with each other in
1176 * disconnected mode. For now maintain this compatibility.
1177 */
1178 if (nc->peer->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
1179 for (i = 0; i < queues; i++) {
1180 ncs[i]->peer->link_down = !up;
1181 }
1182 }
1183 if (nc->peer->info->link_status_changed) {
1184 nc->peer->info->link_status_changed(nc->peer);
1185 }
ab1cbe1c 1186 }
436e5e53
AL
1187}
1188
ca77d85e
MT
1189static void net_vm_change_state_handler(void *opaque, int running,
1190 RunState state)
1191{
1192 /* Complete all queued packets, to guarantee we don't modify
1193 * state later when VM is not running.
1194 */
1195 if (!running) {
1196 NetClientState *nc;
1197 NetClientState *tmp;
1198
1199 QTAILQ_FOREACH_SAFE(nc, &net_clients, next, tmp) {
1200 qemu_flush_or_purge_queued_packets(nc, true);
1201 }
1202 }
1203}
1204
63a01ef8
AL
1205void net_cleanup(void)
1206{
1ceef9f2 1207 NetClientState *nc;
577c4af9 1208
1ceef9f2
JW
1209 /* We may del multiple entries during qemu_del_net_client(),
1210 * so QTAILQ_FOREACH_SAFE() is also not safe here.
1211 */
1212 while (!QTAILQ_EMPTY(&net_clients)) {
1213 nc = QTAILQ_FIRST(&net_clients);
948ecf21
JW
1214 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
1215 qemu_del_nic(qemu_get_nic(nc));
1216 } else {
1217 qemu_del_net_client(nc);
1218 }
577c4af9 1219 }
ca77d85e
MT
1220
1221 qemu_del_vm_change_state_handler(net_change_state_entry);
63a01ef8
AL
1222}
1223
668680f7 1224void net_check_clients(void)
63a01ef8 1225{
35277d14 1226 NetClientState *nc;
48e2faf2 1227 int i;
63a01ef8 1228
641f6eae
PM
1229 /* Don't warn about the default network setup that you get if
1230 * no command line -net or -netdev options are specified. There
1231 * are two cases that we would otherwise complain about:
1232 * (1) board doesn't support a NIC but the implicit "-net nic"
1233 * requested one
1234 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
1235 * sets up a nic that isn't connected to anything.
1236 */
1237 if (default_net) {
1238 return;
1239 }
1240
81017645 1241 net_hub_check_clients();
ac60cc18 1242
35277d14
SH
1243 QTAILQ_FOREACH(nc, &net_clients, next) {
1244 if (!nc->peer) {
efe32fdd 1245 fprintf(stderr, "Warning: %s %s has no peer\n",
35277d14
SH
1246 nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC ?
1247 "nic" : "netdev", nc->name);
efe32fdd
MA
1248 }
1249 }
48e2faf2
PM
1250
1251 /* Check that all NICs requested via -net nic actually got created.
1252 * NICs created via -device don't need to be checked here because
1253 * they are always instantiated.
1254 */
1255 for (i = 0; i < MAX_NICS; i++) {
1256 NICInfo *nd = &nd_table[i];
1257 if (nd->used && !nd->instantiated) {
1258 fprintf(stderr, "Warning: requested NIC (%s, model %s) "
1259 "was not created (not supported by this machine?)\n",
1260 nd->name ? nd->name : "anonymous",
1261 nd->model ? nd->model : "unspecified");
1262 }
1263 }
63a01ef8 1264}
dc1c9fe8
MM
1265
1266static int net_init_client(QemuOpts *opts, void *dummy)
1267{
4559a1db
LC
1268 Error *local_err = NULL;
1269
1270 net_client_init(opts, 0, &local_err);
84d18f06 1271 if (local_err) {
12d0cc2d 1272 error_report_err(local_err);
c1671a08 1273 return -1;
4559a1db
LC
1274 }
1275
c1671a08 1276 return 0;
f6b134ac
MM
1277}
1278
1279static int net_init_netdev(QemuOpts *opts, void *dummy)
1280{
4559a1db
LC
1281 Error *local_err = NULL;
1282 int ret;
1283
1284 ret = net_client_init(opts, 1, &local_err);
84d18f06 1285 if (local_err) {
12d0cc2d 1286 error_report_err(local_err);
4559a1db
LC
1287 return -1;
1288 }
1289
1290 return ret;
dc1c9fe8
MM
1291}
1292
1293int net_init_clients(void)
1294{
3329f07b
GH
1295 QemuOptsList *net = qemu_find_opts("net");
1296
cb4522cc 1297 if (default_net) {
dc1c9fe8 1298 /* if no clients, we use a default config */
3329f07b 1299 qemu_opts_set(net, NULL, "type", "nic");
dc1c9fe8 1300#ifdef CONFIG_SLIRP
3329f07b 1301 qemu_opts_set(net, NULL, "type", "user");
dc1c9fe8
MM
1302#endif
1303 }
1304
ca77d85e
MT
1305 net_change_state_entry =
1306 qemu_add_vm_change_state_handler(net_vm_change_state_handler, NULL);
1307
94878994 1308 QTAILQ_INIT(&net_clients);
5610c3aa 1309
3329f07b 1310 if (qemu_opts_foreach(qemu_find_opts("netdev"), net_init_netdev, NULL, 1) == -1)
f6b134ac
MM
1311 return -1;
1312
3329f07b 1313 if (qemu_opts_foreach(net, net_init_client, NULL, 1) == -1) {
dc1c9fe8
MM
1314 return -1;
1315 }
1316
dc1c9fe8
MM
1317 return 0;
1318}
1319
7f161aae 1320int net_client_parse(QemuOptsList *opts_list, const char *optarg)
dc1c9fe8 1321{
a3a766e7 1322#if defined(CONFIG_SLIRP)
68ac40d2
MM
1323 int ret;
1324 if (net_slirp_parse_legacy(opts_list, optarg, &ret)) {
dc1c9fe8
MM
1325 return ret;
1326 }
a3a766e7 1327#endif
68ac40d2 1328
8212c64f 1329 if (!qemu_opts_parse(opts_list, optarg, 1)) {
dc1c9fe8
MM
1330 return -1;
1331 }
1332
cb4522cc 1333 default_net = 0;
dc1c9fe8
MM
1334 return 0;
1335}
7fc8d918
JW
1336
1337/* From FreeBSD */
1338/* XXX: optimize */
1339unsigned compute_mcast_idx(const uint8_t *ep)
1340{
1341 uint32_t crc;
1342 int carry, i, j;
1343 uint8_t b;
1344
1345 crc = 0xffffffff;
1346 for (i = 0; i < 6; i++) {
1347 b = *ep++;
1348 for (j = 0; j < 8; j++) {
1349 carry = ((crc & 0x80000000L) ? 1 : 0) ^ (b & 0x01);
1350 crc <<= 1;
1351 b >>= 1;
1352 if (carry) {
1353 crc = ((crc ^ POLYNOMIAL) | carry);
1354 }
1355 }
1356 }
1357 return crc >> 26;
1358}
4d454574
PB
1359
1360QemuOptsList qemu_netdev_opts = {
1361 .name = "netdev",
1362 .implied_opt_name = "type",
1363 .head = QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts.head),
1364 .desc = {
1365 /*
1366 * no elements => accept any params
1367 * validation will happen later
1368 */
1369 { /* end of list */ }
1370 },
1371};
1372
1373QemuOptsList qemu_net_opts = {
1374 .name = "net",
1375 .implied_opt_name = "type",
1376 .head = QTAILQ_HEAD_INITIALIZER(qemu_net_opts.head),
1377 .desc = {
1378 /*
1379 * no elements => accept any params
1380 * validation will happen later
1381 */
1382 { /* end of list */ }
1383 },
1384};