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