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