]> git.proxmox.com Git - mirror_qemu.git/blame - net/net.c
Revert "audio: fix pc speaker init"
[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 */
e688df6b 24
2744d920 25#include "qemu/osdep.h"
d40cdb10 26
1422e32d 27#include "net/net.h"
fd9400b3
PB
28#include "clients.h"
29#include "hub.h"
1422e32d 30#include "net/slirp.h"
d60b20cf 31#include "net/eth.h"
fd9400b3 32#include "util.h"
a245fc18 33
83c9089e 34#include "monitor/monitor.h"
f348b6d1 35#include "qemu/help_option.h"
9af23989
MA
36#include "qapi/qapi-commands-net.h"
37#include "qapi/qapi-visit-net.h"
452fcdbc 38#include "qapi/qmp/qdict.h"
cc7a8ea7 39#include "qapi/qmp/qerror.h"
d49b6836 40#include "qemu/error-report.h"
1de7afc9 41#include "qemu/sockets.h"
f348b6d1 42#include "qemu/cutils.h"
1de7afc9 43#include "qemu/config-file.h"
75422b0d 44#include "hw/qdev.h"
1de7afc9 45#include "qemu/iov.h"
6a1751b7 46#include "qemu/main-loop.h"
922a01a0 47#include "qemu/option.h"
e688df6b 48#include "qapi/error.h"
6687b79d 49#include "qapi/opts-visitor.h"
e1d64c08 50#include "sysemu/sysemu.h"
559964a1 51#include "sysemu/qtest.h"
fdccce45 52#include "net/filter.h"
aa9156f4 53#include "qapi/string-output-visitor.h"
511d2b14 54
2944e4ec
SW
55/* Net bridge is currently not supported for W32. */
56#if !defined(_WIN32)
57# define CONFIG_NET_BRIDGE
58#endif
59
ca77d85e 60static VMChangeStateEntry *net_change_state_entry;
4e68f7a0 61static QTAILQ_HEAD(, NetClientState) net_clients;
63a01ef8
AL
62
63/***********************************************************/
64/* network device redirectors */
65
63a01ef8
AL
66static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
67{
68 const char *p, *p1;
69 int len;
70 p = *pp;
71 p1 = strchr(p, sep);
72 if (!p1)
73 return -1;
74 len = p1 - p;
75 p1++;
76 if (buf_size > 0) {
77 if (len > buf_size - 1)
78 len = buf_size - 1;
79 memcpy(buf, p, len);
80 buf[len] = '\0';
81 }
82 *pp = p1;
83 return 0;
84}
85
bcd4dfd6
MZ
86int parse_host_port(struct sockaddr_in *saddr, const char *str,
87 Error **errp)
63a01ef8
AL
88{
89 char buf[512];
90 struct hostent *he;
91 const char *p, *r;
92 int port;
93
94 p = str;
bcd4dfd6
MZ
95 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
96 error_setg(errp, "host address '%s' doesn't contain ':' "
97 "separating host from port", str);
63a01ef8 98 return -1;
bcd4dfd6 99 }
63a01ef8
AL
100 saddr->sin_family = AF_INET;
101 if (buf[0] == '\0') {
102 saddr->sin_addr.s_addr = 0;
103 } else {
cd390083 104 if (qemu_isdigit(buf[0])) {
bcd4dfd6
MZ
105 if (!inet_aton(buf, &saddr->sin_addr)) {
106 error_setg(errp, "host address '%s' is not a valid "
107 "IPv4 address", buf);
63a01ef8 108 return -1;
bcd4dfd6 109 }
63a01ef8 110 } else {
bcd4dfd6
MZ
111 he = gethostbyname(buf);
112 if (he == NULL) {
113 error_setg(errp, "can't resolve host address '%s'", buf);
63a01ef8 114 return - 1;
bcd4dfd6 115 }
63a01ef8
AL
116 saddr->sin_addr = *(struct in_addr *)he->h_addr;
117 }
118 }
119 port = strtol(p, (char **)&r, 0);
bcd4dfd6
MZ
120 if (r == p) {
121 error_setg(errp, "port number '%s' is invalid", p);
63a01ef8 122 return -1;
bcd4dfd6 123 }
63a01ef8
AL
124 saddr->sin_port = htons(port);
125 return 0;
126}
127
890ee6ab
SF
128char *qemu_mac_strdup_printf(const uint8_t *macaddr)
129{
130 return g_strdup_printf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
131 macaddr[0], macaddr[1], macaddr[2],
132 macaddr[3], macaddr[4], macaddr[5]);
133}
134
35277d14 135void qemu_format_nic_info_str(NetClientState *nc, uint8_t macaddr[6])
7cb7434b 136{
35277d14 137 snprintf(nc->info_str, sizeof(nc->info_str),
4dda4063 138 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
35277d14 139 nc->model,
7cb7434b
AL
140 macaddr[0], macaddr[1], macaddr[2],
141 macaddr[3], macaddr[4], macaddr[5]);
142}
143
2bc22a58
SZ
144static int mac_table[256] = {0};
145
146static void qemu_macaddr_set_used(MACAddr *macaddr)
147{
148 int index;
149
150 for (index = 0x56; index < 0xFF; index++) {
151 if (macaddr->a[5] == index) {
152 mac_table[index]++;
153 }
154 }
155}
156
157static void qemu_macaddr_set_free(MACAddr *macaddr)
158{
159 int index;
160 static const MACAddr base = { .a = { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } };
161
162 if (memcmp(macaddr->a, &base.a, (sizeof(base.a) - 1)) != 0) {
163 return;
164 }
165 for (index = 0x56; index < 0xFF; index++) {
166 if (macaddr->a[5] == index) {
167 mac_table[index]--;
168 }
169 }
170}
171
172static int qemu_macaddr_get_free(void)
173{
174 int index;
175
176 for (index = 0x56; index < 0xFF; index++) {
177 if (mac_table[index] == 0) {
178 return index;
179 }
180 }
181
182 return -1;
183}
184
76d32cba
GH
185void qemu_macaddr_default_if_unset(MACAddr *macaddr)
186{
76d32cba 187 static const MACAddr zero = { .a = { 0,0,0,0,0,0 } };
2bc22a58
SZ
188 static const MACAddr base = { .a = { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } };
189
190 if (memcmp(macaddr, &zero, sizeof(zero)) != 0) {
191 if (memcmp(macaddr->a, &base.a, (sizeof(base.a) - 1)) != 0) {
192 return;
193 } else {
194 qemu_macaddr_set_used(macaddr);
195 return;
196 }
197 }
76d32cba 198
76d32cba
GH
199 macaddr->a[0] = 0x52;
200 macaddr->a[1] = 0x54;
201 macaddr->a[2] = 0x00;
202 macaddr->a[3] = 0x12;
203 macaddr->a[4] = 0x34;
2bc22a58
SZ
204 macaddr->a[5] = qemu_macaddr_get_free();
205 qemu_macaddr_set_used(macaddr);
76d32cba
GH
206}
207
d33d93b2
SH
208/**
209 * Generate a name for net client
210 *
c963530a 211 * Only net clients created with the legacy -net option and NICs need this.
d33d93b2 212 */
35277d14 213static char *assign_name(NetClientState *nc1, const char *model)
676cff29 214{
35277d14 215 NetClientState *nc;
676cff29
AL
216 int id = 0;
217
35277d14
SH
218 QTAILQ_FOREACH(nc, &net_clients, next) {
219 if (nc == nc1) {
d33d93b2 220 continue;
5610c3aa 221 }
c963530a 222 if (strcmp(nc->model, model) == 0) {
53e51d85
MA
223 id++;
224 }
225 }
226
4bf2c138 227 return g_strdup_printf("%s.%d", model, id);
676cff29
AL
228}
229
f7860455
JW
230static void qemu_net_client_destructor(NetClientState *nc)
231{
232 g_free(nc);
233}
25c01bd1
JW
234static ssize_t qemu_deliver_packet_iov(NetClientState *sender,
235 unsigned flags,
236 const struct iovec *iov,
237 int iovcnt,
238 void *opaque);
f7860455 239
18a1541a
JW
240static void qemu_net_client_setup(NetClientState *nc,
241 NetClientInfo *info,
242 NetClientState *peer,
243 const char *model,
f7860455
JW
244 const char *name,
245 NetClientDestructor *destructor)
63a01ef8 246{
35277d14
SH
247 nc->info = info;
248 nc->model = g_strdup(model);
45460d1a 249 if (name) {
35277d14 250 nc->name = g_strdup(name);
45460d1a 251 } else {
35277d14 252 nc->name = assign_name(nc, model);
45460d1a 253 }
5610c3aa 254
ab5f3f84
SH
255 if (peer) {
256 assert(!peer->peer);
35277d14
SH
257 nc->peer = peer;
258 peer->peer = nc;
d80b9fc6 259 }
35277d14 260 QTAILQ_INSERT_TAIL(&net_clients, nc, next);
63a01ef8 261
3e033a46 262 nc->incoming_queue = qemu_new_net_queue(qemu_deliver_packet_iov, nc);
f7860455 263 nc->destructor = destructor;
fdccce45 264 QTAILQ_INIT(&nc->filters);
18a1541a
JW
265}
266
267NetClientState *qemu_new_net_client(NetClientInfo *info,
268 NetClientState *peer,
269 const char *model,
270 const char *name)
271{
272 NetClientState *nc;
273
274 assert(info->size >= sizeof(NetClientState));
275
276 nc = g_malloc0(info->size);
f7860455
JW
277 qemu_net_client_setup(nc, info, peer, model, name,
278 qemu_net_client_destructor);
18a1541a 279
35277d14 280 return nc;
63a01ef8
AL
281}
282
ebef2c09
MM
283NICState *qemu_new_nic(NetClientInfo *info,
284 NICConf *conf,
285 const char *model,
286 const char *name,
287 void *opaque)
288{
1ceef9f2 289 NetClientState **peers = conf->peers.ncs;
ebef2c09 290 NICState *nic;
575a1c0e 291 int i, queues = MAX(1, conf->peers.queues);
ebef2c09 292
f394b2e2 293 assert(info->type == NET_CLIENT_DRIVER_NIC);
ebef2c09
MM
294 assert(info->size >= sizeof(NICState));
295
f6b26cf2
JW
296 nic = g_malloc0(info->size + sizeof(NetClientState) * queues);
297 nic->ncs = (void *)nic + info->size;
ebef2c09
MM
298 nic->conf = conf;
299 nic->opaque = opaque;
300
f6b26cf2
JW
301 for (i = 0; i < queues; i++) {
302 qemu_net_client_setup(&nic->ncs[i], info, peers[i], model, name,
1ceef9f2
JW
303 NULL);
304 nic->ncs[i].queue_index = i;
305 }
306
ebef2c09
MM
307 return nic;
308}
309
1ceef9f2
JW
310NetClientState *qemu_get_subqueue(NICState *nic, int queue_index)
311{
f6b26cf2 312 return nic->ncs + queue_index;
1ceef9f2
JW
313}
314
b356f76d
JW
315NetClientState *qemu_get_queue(NICState *nic)
316{
1ceef9f2 317 return qemu_get_subqueue(nic, 0);
b356f76d
JW
318}
319
cc1f0f45
JW
320NICState *qemu_get_nic(NetClientState *nc)
321{
1ceef9f2
JW
322 NetClientState *nc0 = nc - nc->queue_index;
323
f6b26cf2 324 return (NICState *)((void *)nc0 - nc->info->size);
cc1f0f45
JW
325}
326
327void *qemu_get_nic_opaque(NetClientState *nc)
328{
329 NICState *nic = qemu_get_nic(nc);
330
331 return nic->opaque;
332}
333
b20c6b9e 334static void qemu_cleanup_net_client(NetClientState *nc)
63a01ef8 335{
35277d14 336 QTAILQ_REMOVE(&net_clients, nc, next);
63a01ef8 337
cc2a9043
AF
338 if (nc->info->cleanup) {
339 nc->info->cleanup(nc);
340 }
a083a89d 341}
5610c3aa 342
b20c6b9e 343static void qemu_free_net_client(NetClientState *nc)
a083a89d 344{
067404be
JK
345 if (nc->incoming_queue) {
346 qemu_del_net_queue(nc->incoming_queue);
a005d073 347 }
35277d14
SH
348 if (nc->peer) {
349 nc->peer->peer = NULL;
a083a89d 350 }
35277d14
SH
351 g_free(nc->name);
352 g_free(nc->model);
f7860455
JW
353 if (nc->destructor) {
354 nc->destructor(nc);
355 }
63a01ef8
AL
356}
357
b20c6b9e 358void qemu_del_net_client(NetClientState *nc)
a083a89d 359{
1ceef9f2
JW
360 NetClientState *ncs[MAX_QUEUE_NUM];
361 int queues, i;
fdccce45 362 NetFilterState *nf, *next;
1ceef9f2 363
f394b2e2 364 assert(nc->info->type != NET_CLIENT_DRIVER_NIC);
7fb43911 365
1ceef9f2
JW
366 /* If the NetClientState belongs to a multiqueue backend, we will change all
367 * other NetClientStates also.
368 */
369 queues = qemu_find_net_clients_except(nc->name, ncs,
f394b2e2 370 NET_CLIENT_DRIVER_NIC,
1ceef9f2
JW
371 MAX_QUEUE_NUM);
372 assert(queues != 0);
373
fdccce45
YH
374 QTAILQ_FOREACH_SAFE(nf, &nc->filters, next, next) {
375 object_unparent(OBJECT(nf));
376 }
377
a083a89d 378 /* If there is a peer NIC, delete and cleanup client, but do not free. */
f394b2e2 379 if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_NIC) {
cc1f0f45 380 NICState *nic = qemu_get_nic(nc->peer);
a083a89d
MT
381 if (nic->peer_deleted) {
382 return;
383 }
384 nic->peer_deleted = true;
1ceef9f2
JW
385
386 for (i = 0; i < queues; i++) {
387 ncs[i]->peer->link_down = true;
388 }
389
35277d14
SH
390 if (nc->peer->info->link_status_changed) {
391 nc->peer->info->link_status_changed(nc->peer);
a083a89d 392 }
1ceef9f2
JW
393
394 for (i = 0; i < queues; i++) {
395 qemu_cleanup_net_client(ncs[i]);
396 }
397
a083a89d
MT
398 return;
399 }
400
1ceef9f2
JW
401 for (i = 0; i < queues; i++) {
402 qemu_cleanup_net_client(ncs[i]);
403 qemu_free_net_client(ncs[i]);
404 }
948ecf21
JW
405}
406
407void qemu_del_nic(NICState *nic)
408{
575a1c0e 409 int i, queues = MAX(nic->conf->peers.queues, 1);
1ceef9f2 410
2bc22a58
SZ
411 qemu_macaddr_set_free(&nic->conf->macaddr);
412
a083a89d 413 /* If this is a peer NIC and peer has already been deleted, free it now. */
1ceef9f2
JW
414 if (nic->peer_deleted) {
415 for (i = 0; i < queues; i++) {
416 qemu_free_net_client(qemu_get_subqueue(nic, i)->peer);
1a609520
JK
417 }
418 }
1a609520 419
1ceef9f2
JW
420 for (i = queues - 1; i >= 0; i--) {
421 NetClientState *nc = qemu_get_subqueue(nic, i);
422
423 qemu_cleanup_net_client(nc);
424 qemu_free_net_client(nc);
425 }
f6b26cf2
JW
426
427 g_free(nic);
1a609520
JK
428}
429
57f9ef17
MM
430void qemu_foreach_nic(qemu_nic_foreach func, void *opaque)
431{
4e68f7a0 432 NetClientState *nc;
57f9ef17 433
94878994 434 QTAILQ_FOREACH(nc, &net_clients, next) {
f394b2e2 435 if (nc->info->type == NET_CLIENT_DRIVER_NIC) {
1ceef9f2
JW
436 if (nc->queue_index == 0) {
437 func(qemu_get_nic(nc), opaque);
438 }
57f9ef17
MM
439 }
440 }
57f9ef17
MM
441}
442
d6085e3a 443bool qemu_has_ufo(NetClientState *nc)
1f55ac45 444{
d6085e3a 445 if (!nc || !nc->info->has_ufo) {
1f55ac45
VM
446 return false;
447 }
448
d6085e3a 449 return nc->info->has_ufo(nc);
1f55ac45
VM
450}
451
d6085e3a 452bool qemu_has_vnet_hdr(NetClientState *nc)
1f55ac45 453{
d6085e3a 454 if (!nc || !nc->info->has_vnet_hdr) {
1f55ac45
VM
455 return false;
456 }
457
d6085e3a 458 return nc->info->has_vnet_hdr(nc);
1f55ac45
VM
459}
460
d6085e3a 461bool qemu_has_vnet_hdr_len(NetClientState *nc, int len)
1f55ac45 462{
d6085e3a 463 if (!nc || !nc->info->has_vnet_hdr_len) {
1f55ac45
VM
464 return false;
465 }
466
d6085e3a 467 return nc->info->has_vnet_hdr_len(nc, len);
1f55ac45
VM
468}
469
d6085e3a 470void qemu_using_vnet_hdr(NetClientState *nc, bool enable)
1f55ac45 471{
d6085e3a 472 if (!nc || !nc->info->using_vnet_hdr) {
1f55ac45
VM
473 return;
474 }
475
d6085e3a 476 nc->info->using_vnet_hdr(nc, enable);
1f55ac45
VM
477}
478
d6085e3a 479void qemu_set_offload(NetClientState *nc, int csum, int tso4, int tso6,
1f55ac45
VM
480 int ecn, int ufo)
481{
d6085e3a 482 if (!nc || !nc->info->set_offload) {
1f55ac45
VM
483 return;
484 }
485
d6085e3a 486 nc->info->set_offload(nc, csum, tso4, tso6, ecn, ufo);
1f55ac45
VM
487}
488
d6085e3a 489void qemu_set_vnet_hdr_len(NetClientState *nc, int len)
1f55ac45 490{
d6085e3a 491 if (!nc || !nc->info->set_vnet_hdr_len) {
1f55ac45
VM
492 return;
493 }
494
d6b732e9 495 nc->vnet_hdr_len = len;
d6085e3a 496 nc->info->set_vnet_hdr_len(nc, len);
1f55ac45
VM
497}
498
c80cd6bb
GK
499int qemu_set_vnet_le(NetClientState *nc, bool is_le)
500{
052bd52f 501#ifdef HOST_WORDS_BIGENDIAN
c80cd6bb
GK
502 if (!nc || !nc->info->set_vnet_le) {
503 return -ENOSYS;
504 }
505
506 return nc->info->set_vnet_le(nc, is_le);
052bd52f
MT
507#else
508 return 0;
509#endif
c80cd6bb
GK
510}
511
512int qemu_set_vnet_be(NetClientState *nc, bool is_be)
513{
052bd52f
MT
514#ifdef HOST_WORDS_BIGENDIAN
515 return 0;
516#else
c80cd6bb
GK
517 if (!nc || !nc->info->set_vnet_be) {
518 return -ENOSYS;
519 }
520
521 return nc->info->set_vnet_be(nc, is_be);
052bd52f 522#endif
c80cd6bb
GK
523}
524
4e68f7a0 525int qemu_can_send_packet(NetClientState *sender)
63a01ef8 526{
e1d64c08
HZ
527 int vm_running = runstate_is_running();
528
529 if (!vm_running) {
530 return 0;
531 }
532
a005d073 533 if (!sender->peer) {
d80b9fc6
MM
534 return 1;
535 }
536
a005d073
SH
537 if (sender->peer->receive_disabled) {
538 return 0;
539 } else if (sender->peer->info->can_receive &&
540 !sender->peer->info->can_receive(sender->peer)) {
541 return 0;
63a01ef8 542 }
60c07d93 543 return 1;
63a01ef8
AL
544}
545
e64c770d
YH
546static ssize_t filter_receive_iov(NetClientState *nc,
547 NetFilterDirection direction,
548 NetClientState *sender,
549 unsigned flags,
550 const struct iovec *iov,
551 int iovcnt,
552 NetPacketSent *sent_cb)
553{
554 ssize_t ret = 0;
555 NetFilterState *nf = NULL;
556
25aaadf0
LZ
557 if (direction == NET_FILTER_DIRECTION_TX) {
558 QTAILQ_FOREACH(nf, &nc->filters, next) {
559 ret = qemu_netfilter_receive(nf, direction, sender, flags, iov,
560 iovcnt, sent_cb);
561 if (ret) {
562 return ret;
563 }
564 }
565 } else {
eae3eb3e 566 QTAILQ_FOREACH_REVERSE(nf, &nc->filters, next) {
25aaadf0
LZ
567 ret = qemu_netfilter_receive(nf, direction, sender, flags, iov,
568 iovcnt, sent_cb);
569 if (ret) {
570 return ret;
571 }
e64c770d
YH
572 }
573 }
574
575 return ret;
576}
577
578static ssize_t filter_receive(NetClientState *nc,
579 NetFilterDirection direction,
580 NetClientState *sender,
581 unsigned flags,
582 const uint8_t *data,
583 size_t size,
584 NetPacketSent *sent_cb)
585{
586 struct iovec iov = {
587 .iov_base = (void *)data,
588 .iov_len = size
589 };
590
591 return filter_receive_iov(nc, direction, sender, flags, &iov, 1, sent_cb);
592}
593
35277d14 594void qemu_purge_queued_packets(NetClientState *nc)
8cad5516 595{
35277d14 596 if (!nc->peer) {
d80b9fc6 597 return;
9a6ecb30 598 }
d80b9fc6 599
067404be 600 qemu_net_queue_purge(nc->peer->incoming_queue, nc);
8cad5516
MM
601}
602
ca77d85e 603void qemu_flush_or_purge_queued_packets(NetClientState *nc, bool purge)
e94667b9 604{
35277d14 605 nc->receive_disabled = 0;
9a6ecb30 606
f394b2e2 607 if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_HUBPORT) {
199ee608
LR
608 if (net_hub_flush(nc->peer)) {
609 qemu_notify_event();
610 }
199ee608 611 }
067404be 612 if (qemu_net_queue_flush(nc->incoming_queue)) {
987a9b48
PB
613 /* We emptied the queue successfully, signal to the IO thread to repoll
614 * the file descriptor (for tap, for example).
615 */
616 qemu_notify_event();
ca77d85e
MT
617 } else if (purge) {
618 /* Unable to empty the queue, purge remaining packets */
619 qemu_net_queue_purge(nc->incoming_queue, nc);
987a9b48 620 }
e94667b9
MM
621}
622
ca77d85e
MT
623void qemu_flush_queued_packets(NetClientState *nc)
624{
625 qemu_flush_or_purge_queued_packets(nc, false);
626}
627
4e68f7a0 628static ssize_t qemu_send_packet_async_with_flags(NetClientState *sender,
ca77d175
MM
629 unsigned flags,
630 const uint8_t *buf, int size,
631 NetPacketSent *sent_cb)
764a4d1d 632{
9a6ecb30 633 NetQueue *queue;
e64c770d 634 int ret;
436e5e53 635
63a01ef8 636#ifdef DEBUG_NET
d80b9fc6 637 printf("qemu_send_packet_async:\n");
a1555559 638 qemu_hexdump((const char *)buf, stdout, "net", size);
63a01ef8 639#endif
f3b6c7fc 640
a005d073 641 if (sender->link_down || !sender->peer) {
9a6ecb30
MM
642 return size;
643 }
644
e64c770d
YH
645 /* Let filters handle the packet first */
646 ret = filter_receive(sender, NET_FILTER_DIRECTION_TX,
647 sender, flags, buf, size, sent_cb);
648 if (ret) {
649 return ret;
650 }
651
652 ret = filter_receive(sender->peer, NET_FILTER_DIRECTION_RX,
653 sender, flags, buf, size, sent_cb);
654 if (ret) {
655 return ret;
656 }
657
067404be 658 queue = sender->peer->incoming_queue;
9a6ecb30 659
ca77d175
MM
660 return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb);
661}
662
4e68f7a0 663ssize_t qemu_send_packet_async(NetClientState *sender,
ca77d175
MM
664 const uint8_t *buf, int size,
665 NetPacketSent *sent_cb)
666{
667 return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE,
668 buf, size, sent_cb);
f3b6c7fc
MM
669}
670
625a526b 671ssize_t qemu_send_packet(NetClientState *nc, const uint8_t *buf, int size)
f3b6c7fc 672{
625a526b 673 return qemu_send_packet_async(nc, buf, size, NULL);
63a01ef8
AL
674}
675
35277d14 676ssize_t qemu_send_packet_raw(NetClientState *nc, const uint8_t *buf, int size)
ca77d175 677{
35277d14 678 return qemu_send_packet_async_with_flags(nc, QEMU_NET_PACKET_FLAG_RAW,
ca77d175
MM
679 buf, size, NULL);
680}
681
35277d14 682static ssize_t nc_sendv_compat(NetClientState *nc, const struct iovec *iov,
fefe2a78 683 int iovcnt, unsigned flags)
fbe78f4f 684{
74044c8f 685 uint8_t *buf = NULL;
fefe2a78 686 uint8_t *buffer;
ce053661 687 size_t offset;
74044c8f 688 ssize_t ret;
fbe78f4f 689
fefe2a78
YH
690 if (iovcnt == 1) {
691 buffer = iov[0].iov_base;
692 offset = iov[0].iov_len;
693 } else {
47f9f158
PL
694 offset = iov_size(iov, iovcnt);
695 if (offset > NET_BUFSIZE) {
696 return -1;
697 }
698 buf = g_malloc(offset);
fefe2a78 699 buffer = buf;
47f9f158 700 offset = iov_to_buf(iov, iovcnt, 0, buf, offset);
fefe2a78 701 }
fbe78f4f 702
fefe2a78 703 if (flags & QEMU_NET_PACKET_FLAG_RAW && nc->info->receive_raw) {
74044c8f 704 ret = nc->info->receive_raw(nc, buffer, offset);
fefe2a78 705 } else {
74044c8f 706 ret = nc->info->receive(nc, buffer, offset);
fefe2a78 707 }
74044c8f
PD
708
709 g_free(buf);
710 return ret;
fbe78f4f
AL
711}
712
25c01bd1
JW
713static ssize_t qemu_deliver_packet_iov(NetClientState *sender,
714 unsigned flags,
715 const struct iovec *iov,
716 int iovcnt,
717 void *opaque)
9a6ecb30 718{
35277d14 719 NetClientState *nc = opaque;
c67f5dc1 720 int ret;
9a6ecb30 721
1592a994 722
35277d14 723 if (nc->link_down) {
25c01bd1 724 return iov_size(iov, iovcnt);
9a6ecb30
MM
725 }
726
c67f5dc1
SH
727 if (nc->receive_disabled) {
728 return 0;
729 }
730
ca1ee3d6 731 if (nc->info->receive_iov && !(flags & QEMU_NET_PACKET_FLAG_RAW)) {
c67f5dc1 732 ret = nc->info->receive_iov(nc, iov, iovcnt);
9a6ecb30 733 } else {
fefe2a78 734 ret = nc_sendv_compat(nc, iov, iovcnt, flags);
c67f5dc1
SH
735 }
736
737 if (ret == 0) {
738 nc->receive_disabled = 1;
e94667b9 739 }
c67f5dc1
SH
740
741 return ret;
e94667b9
MM
742}
743
4e68f7a0 744ssize_t qemu_sendv_packet_async(NetClientState *sender,
f3b6c7fc
MM
745 const struct iovec *iov, int iovcnt,
746 NetPacketSent *sent_cb)
e94667b9 747{
9a6ecb30 748 NetQueue *queue;
25c01bd1 749 size_t size = iov_size(iov, iovcnt);
e64c770d 750 int ret;
9a6ecb30 751
25c01bd1
JW
752 if (size > NET_BUFSIZE) {
753 return size;
754 }
755
a005d073 756 if (sender->link_down || !sender->peer) {
25c01bd1 757 return size;
e94667b9
MM
758 }
759
e64c770d
YH
760 /* Let filters handle the packet first */
761 ret = filter_receive_iov(sender, NET_FILTER_DIRECTION_TX, sender,
762 QEMU_NET_PACKET_FLAG_NONE, iov, iovcnt, sent_cb);
763 if (ret) {
764 return ret;
765 }
766
767 ret = filter_receive_iov(sender->peer, NET_FILTER_DIRECTION_RX, sender,
768 QEMU_NET_PACKET_FLAG_NONE, iov, iovcnt, sent_cb);
769 if (ret) {
770 return ret;
771 }
772
067404be 773 queue = sender->peer->incoming_queue;
9a6ecb30 774
c0b8e49c
MM
775 return qemu_net_queue_send_iov(queue, sender,
776 QEMU_NET_PACKET_FLAG_NONE,
777 iov, iovcnt, sent_cb);
fbe78f4f
AL
778}
779
f3b6c7fc 780ssize_t
35277d14 781qemu_sendv_packet(NetClientState *nc, const struct iovec *iov, int iovcnt)
f3b6c7fc 782{
35277d14 783 return qemu_sendv_packet_async(nc, iov, iovcnt, NULL);
63a01ef8
AL
784}
785
4e68f7a0 786NetClientState *qemu_find_netdev(const char *id)
5869c4d5 787{
35277d14 788 NetClientState *nc;
5869c4d5 789
35277d14 790 QTAILQ_FOREACH(nc, &net_clients, next) {
f394b2e2 791 if (nc->info->type == NET_CLIENT_DRIVER_NIC)
85dde9a9 792 continue;
35277d14
SH
793 if (!strcmp(nc->name, id)) {
794 return nc;
5869c4d5
MM
795 }
796 }
797
798 return NULL;
799}
800
6c51ae73 801int qemu_find_net_clients_except(const char *id, NetClientState **ncs,
f394b2e2 802 NetClientDriver type, int max)
6c51ae73
JW
803{
804 NetClientState *nc;
805 int ret = 0;
806
807 QTAILQ_FOREACH(nc, &net_clients, next) {
808 if (nc->info->type == type) {
809 continue;
810 }
40d19394 811 if (!id || !strcmp(nc->name, id)) {
6c51ae73
JW
812 if (ret < max) {
813 ncs[ret] = nc;
814 }
815 ret++;
816 }
817 }
818
819 return ret;
820}
821
7697079b
AL
822static int nic_get_free_idx(void)
823{
824 int index;
825
826 for (index = 0; index < MAX_NICS; index++)
827 if (!nd_table[index].used)
828 return index;
829 return -1;
830}
831
07caea31
MA
832int qemu_show_nic_models(const char *arg, const char *const *models)
833{
834 int i;
835
c8057f95 836 if (!arg || !is_help_option(arg)) {
07caea31 837 return 0;
c8057f95 838 }
07caea31
MA
839
840 fprintf(stderr, "qemu: Supported NIC models: ");
841 for (i = 0 ; models[i]; i++)
842 fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n');
843 return 1;
844}
845
d07f22c5
AL
846void qemu_check_nic_model(NICInfo *nd, const char *model)
847{
848 const char *models[2];
849
850 models[0] = model;
851 models[1] = NULL;
852
07caea31
MA
853 if (qemu_show_nic_models(nd->model, models))
854 exit(0);
855 if (qemu_find_nic_model(nd, models, model) < 0)
856 exit(1);
d07f22c5
AL
857}
858
07caea31
MA
859int qemu_find_nic_model(NICInfo *nd, const char * const *models,
860 const char *default_model)
d07f22c5 861{
07caea31 862 int i;
d07f22c5
AL
863
864 if (!nd->model)
7267c094 865 nd->model = g_strdup(default_model);
d07f22c5 866
07caea31
MA
867 for (i = 0 ; models[i]; i++) {
868 if (strcmp(nd->model, models[i]) == 0)
869 return i;
d07f22c5
AL
870 }
871
6daf194d 872 error_report("Unsupported NIC model: %s", nd->model);
07caea31 873 return -1;
d07f22c5
AL
874}
875
cebea510 876static int net_init_nic(const Netdev *netdev, const char *name,
a30ecde6 877 NetClientState *peer, Error **errp)
f83c6e10
MM
878{
879 int idx;
880 NICInfo *nd;
2456f36f
LE
881 const NetLegacyNicOptions *nic;
882
f394b2e2
EB
883 assert(netdev->type == NET_CLIENT_DRIVER_NIC);
884 nic = &netdev->u.nic;
f83c6e10
MM
885
886 idx = nic_get_free_idx();
887 if (idx == -1 || nb_nics >= MAX_NICS) {
66308868 888 error_setg(errp, "too many NICs");
f83c6e10
MM
889 return -1;
890 }
891
892 nd = &nd_table[idx];
893
894 memset(nd, 0, sizeof(*nd));
895
2456f36f
LE
896 if (nic->has_netdev) {
897 nd->netdev = qemu_find_netdev(nic->netdev);
5869c4d5 898 if (!nd->netdev) {
66308868 899 error_setg(errp, "netdev '%s' not found", nic->netdev);
5869c4d5
MM
900 return -1;
901 }
902 } else {
d33d93b2
SH
903 assert(peer);
904 nd->netdev = peer;
5869c4d5 905 }
c64f50d1 906 nd->name = g_strdup(name);
2456f36f
LE
907 if (nic->has_model) {
908 nd->model = g_strdup(nic->model);
f83c6e10 909 }
2456f36f
LE
910 if (nic->has_addr) {
911 nd->devaddr = g_strdup(nic->addr);
f83c6e10
MM
912 }
913
2456f36f
LE
914 if (nic->has_macaddr &&
915 net_parse_macaddr(nd->macaddr.a, nic->macaddr) < 0) {
66308868 916 error_setg(errp, "invalid syntax for ethernet address");
f83c6e10
MM
917 return -1;
918 }
d60b20cf
DK
919 if (nic->has_macaddr &&
920 is_multicast_ether_addr(nd->macaddr.a)) {
66308868
MA
921 error_setg(errp,
922 "NIC cannot have multicast MAC address (odd 1st byte)");
d60b20cf
DK
923 return -1;
924 }
6eed1856 925 qemu_macaddr_default_if_unset(&nd->macaddr);
f83c6e10 926
2456f36f
LE
927 if (nic->has_vectors) {
928 if (nic->vectors > 0x7ffffff) {
66308868 929 error_setg(errp, "invalid # of vectors: %"PRIu32, nic->vectors);
2456f36f
LE
930 return -1;
931 }
932 nd->nvectors = nic->vectors;
933 } else {
934 nd->nvectors = DEV_NVECTORS_UNSPECIFIED;
f83c6e10
MM
935 }
936
937 nd->used = 1;
f83c6e10
MM
938 nb_nics++;
939
940 return idx;
941}
942
6687b79d 943
f394b2e2 944static int (* const net_client_init_fun[NET_CLIENT_DRIVER__MAX])(
cebea510 945 const Netdev *netdev,
6687b79d 946 const char *name,
a30ecde6 947 NetClientState *peer, Error **errp) = {
f394b2e2 948 [NET_CLIENT_DRIVER_NIC] = net_init_nic,
ec302ffd 949#ifdef CONFIG_SLIRP
f394b2e2 950 [NET_CLIENT_DRIVER_USER] = net_init_slirp,
2944e4ec 951#endif
f394b2e2
EB
952 [NET_CLIENT_DRIVER_TAP] = net_init_tap,
953 [NET_CLIENT_DRIVER_SOCKET] = net_init_socket,
dd51058d 954#ifdef CONFIG_VDE
f394b2e2 955 [NET_CLIENT_DRIVER_VDE] = net_init_vde,
58952137
VM
956#endif
957#ifdef CONFIG_NETMAP
f394b2e2 958 [NET_CLIENT_DRIVER_NETMAP] = net_init_netmap,
dd51058d 959#endif
2944e4ec 960#ifdef CONFIG_NET_BRIDGE
f394b2e2 961 [NET_CLIENT_DRIVER_BRIDGE] = net_init_bridge,
6687b79d 962#endif
f394b2e2 963 [NET_CLIENT_DRIVER_HUBPORT] = net_init_hubport,
56f41de7 964#ifdef CONFIG_VHOST_NET_USER
f394b2e2 965 [NET_CLIENT_DRIVER_VHOST_USER] = net_init_vhost_user,
03ce5744 966#endif
015a33bd 967#ifdef CONFIG_L2TPV3
f394b2e2 968 [NET_CLIENT_DRIVER_L2TPV3] = net_init_l2tpv3,
3fb69aa1 969#endif
f83c6e10
MM
970};
971
6687b79d 972
0e55c381 973static int net_client_init1(const void *object, bool is_netdev, Error **errp)
f83c6e10 974{
cebea510
KZ
975 Netdev legacy = {0};
976 const Netdev *netdev;
6d952ebe 977 const char *name;
4ef0defb 978 NetClientState *peer = NULL;
f83c6e10 979
5294e2c7 980 if (is_netdev) {
cebea510 981 netdev = object;
1e81aba5 982 name = netdev->id;
6687b79d 983
857d2087 984 if (netdev->type == NET_CLIENT_DRIVER_NIC ||
f394b2e2 985 !net_client_init_fun[netdev->type]) {
c6bd8c70
MA
986 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "type",
987 "a netdev backend type");
f6b134ac
MM
988 return -1;
989 }
6687b79d 990 } else {
1e81aba5 991 const NetLegacy *net = object;
f394b2e2 992 const NetLegacyOptions *opts = net->opts;
cebea510 993 legacy.id = net->id;
cebea510 994 netdev = &legacy;
6687b79d 995 /* missing optional values have been initialized to "all bits zero" */
1e81aba5 996 name = net->has_id ? net->id : net->name;
d139e9a6 997
101625a4
TH
998 if (net->has_name) {
999 warn_report("The 'name' parameter is deprecated, use 'id' instead");
1000 }
1001
f394b2e2
EB
1002 /* Map the old options to the new flat type */
1003 switch (opts->type) {
d3be4b57 1004 case NET_LEGACY_OPTIONS_TYPE_NONE:
d139e9a6 1005 return 0; /* nothing to do */
d3be4b57 1006 case NET_LEGACY_OPTIONS_TYPE_NIC:
f394b2e2 1007 legacy.type = NET_CLIENT_DRIVER_NIC;
d3be4b57 1008 legacy.u.nic = opts->u.nic;
f394b2e2 1009 break;
d3be4b57 1010 case NET_LEGACY_OPTIONS_TYPE_USER:
f394b2e2 1011 legacy.type = NET_CLIENT_DRIVER_USER;
d3be4b57 1012 legacy.u.user = opts->u.user;
f394b2e2 1013 break;
d3be4b57 1014 case NET_LEGACY_OPTIONS_TYPE_TAP:
f394b2e2 1015 legacy.type = NET_CLIENT_DRIVER_TAP;
d3be4b57 1016 legacy.u.tap = opts->u.tap;
f394b2e2 1017 break;
d3be4b57 1018 case NET_LEGACY_OPTIONS_TYPE_L2TPV3:
f394b2e2 1019 legacy.type = NET_CLIENT_DRIVER_L2TPV3;
d3be4b57 1020 legacy.u.l2tpv3 = opts->u.l2tpv3;
f394b2e2 1021 break;
d3be4b57 1022 case NET_LEGACY_OPTIONS_TYPE_SOCKET:
f394b2e2 1023 legacy.type = NET_CLIENT_DRIVER_SOCKET;
d3be4b57 1024 legacy.u.socket = opts->u.socket;
f394b2e2 1025 break;
d3be4b57 1026 case NET_LEGACY_OPTIONS_TYPE_VDE:
f394b2e2 1027 legacy.type = NET_CLIENT_DRIVER_VDE;
d3be4b57 1028 legacy.u.vde = opts->u.vde;
f394b2e2 1029 break;
d3be4b57 1030 case NET_LEGACY_OPTIONS_TYPE_BRIDGE:
f394b2e2 1031 legacy.type = NET_CLIENT_DRIVER_BRIDGE;
d3be4b57 1032 legacy.u.bridge = opts->u.bridge;
f394b2e2 1033 break;
d3be4b57 1034 case NET_LEGACY_OPTIONS_TYPE_NETMAP:
f394b2e2 1035 legacy.type = NET_CLIENT_DRIVER_NETMAP;
d3be4b57 1036 legacy.u.netmap = opts->u.netmap;
f394b2e2 1037 break;
d3be4b57 1038 case NET_LEGACY_OPTIONS_TYPE_VHOST_USER:
f394b2e2 1039 legacy.type = NET_CLIENT_DRIVER_VHOST_USER;
d3be4b57 1040 legacy.u.vhost_user = opts->u.vhost_user;
f394b2e2
EB
1041 break;
1042 default:
1043 abort();
1e81aba5 1044 }
d139e9a6 1045
f394b2e2 1046 if (!net_client_init_fun[netdev->type]) {
d139e9a6
SH
1047 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "type",
1048 "a net backend type (maybe it is not compiled "
1049 "into this binary)");
1050 return -1;
1051 }
f6b134ac 1052
af1a5c3e 1053 /* Do not add to a hub if it's a nic with a netdev= parameter. */
f394b2e2 1054 if (netdev->type != NET_CLIENT_DRIVER_NIC ||
d3be4b57 1055 !opts->u.nic.has_netdev) {
af1a5c3e 1056 peer = net_hub_add_port(0, NULL, NULL);
a2dbe135 1057 }
4ef0defb 1058 }
6687b79d 1059
f394b2e2 1060 if (net_client_init_fun[netdev->type](netdev, name, peer, errp) < 0) {
4ef0defb
SH
1061 /* FIXME drop when all init functions store an Error */
1062 if (errp && !*errp) {
1063 error_setg(errp, QERR_DEVICE_INIT_FAILED,
977c736f 1064 NetClientDriver_str(netdev->type));
f6b134ac 1065 }
4ef0defb 1066 return -1;
f6b134ac 1067 }
6687b79d
LE
1068 return 0;
1069}
1070
547203ea
TH
1071static void show_netdevs(void)
1072{
1073 int idx;
1074 const char *available_netdevs[] = {
1075 "socket",
1076 "hubport",
1077 "tap",
1078#ifdef CONFIG_SLIRP
1079 "user",
1080#endif
1081#ifdef CONFIG_L2TPV3
1082 "l2tpv3",
1083#endif
1084#ifdef CONFIG_VDE
1085 "vde",
1086#endif
1087#ifdef CONFIG_NET_BRIDGE
1088 "bridge",
1089#endif
1090#ifdef CONFIG_NETMAP
1091 "netmap",
1092#endif
1093#ifdef CONFIG_POSIX
1094 "vhost-user",
1095#endif
1096 };
1097
1098 printf("Available netdev backend types:\n");
1099 for (idx = 0; idx < ARRAY_SIZE(available_netdevs); idx++) {
1100 puts(available_netdevs[idx]);
1101 }
1102}
f6b134ac 1103
aa09a485 1104static int net_client_init(QemuOpts *opts, bool is_netdev, Error **errp)
6687b79d
LE
1105{
1106 void *object = NULL;
1107 Error *err = NULL;
1108 int ret = -1;
09204eac 1109 Visitor *v = opts_visitor_new(opts);
f83c6e10 1110
8b43f964
LM
1111 const char *type = qemu_opt_get(opts, "type");
1112
1113 if (is_netdev && type && is_help_option(type)) {
547203ea
TH
1114 show_netdevs();
1115 exit(0);
1116 } else {
7aac531e 1117 /* Parse convenience option format ip6-net=fec0::0[/64] */
891a2bb5 1118 const char *ip6_net = qemu_opt_get(opts, "ipv6-net");
7aac531e
YB
1119
1120 if (ip6_net) {
1121 char buf[strlen(ip6_net) + 1];
1122
1123 if (get_str_sep(buf, sizeof(buf), &ip6_net, '/') < 0) {
1124 /* Default 64bit prefix length. */
891a2bb5
ST
1125 qemu_opt_set(opts, "ipv6-prefix", ip6_net, &error_abort);
1126 qemu_opt_set_number(opts, "ipv6-prefixlen", 64, &error_abort);
7aac531e
YB
1127 } else {
1128 /* User-specified prefix length. */
1129 unsigned long len;
1130 int err;
1131
891a2bb5 1132 qemu_opt_set(opts, "ipv6-prefix", buf, &error_abort);
7aac531e
YB
1133 err = qemu_strtoul(ip6_net, NULL, 10, &len);
1134
1135 if (err) {
1136 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
891a2bb5 1137 "ipv6-prefix", "a number");
7aac531e 1138 } else {
891a2bb5 1139 qemu_opt_set_number(opts, "ipv6-prefixlen", len,
7aac531e
YB
1140 &error_abort);
1141 }
1142 }
891a2bb5 1143 qemu_opt_unset(opts, "ipv6-net");
7aac531e
YB
1144 }
1145 }
1146
96a1616c
EB
1147 if (is_netdev) {
1148 visit_type_Netdev(v, NULL, (Netdev **)&object, &err);
1149 } else {
1150 visit_type_NetLegacy(v, NULL, (NetLegacy **)&object, &err);
f83c6e10
MM
1151 }
1152
6687b79d 1153 if (!err) {
1a0c0958 1154 ret = net_client_init1(object, is_netdev, &err);
6687b79d
LE
1155 }
1156
96a1616c
EB
1157 if (is_netdev) {
1158 qapi_free_Netdev(object);
1159 } else {
1160 qapi_free_NetLegacy(object);
6687b79d
LE
1161 }
1162
1163 error_propagate(errp, err);
09204eac 1164 visit_free(v);
6687b79d 1165 return ret;
f83c6e10
MM
1166}
1167
928059a3
LC
1168void netdev_add(QemuOpts *opts, Error **errp)
1169{
0e55c381 1170 net_client_init(opts, true, errp);
928059a3
LC
1171}
1172
485febc6 1173void qmp_netdev_add(QDict *qdict, QObject **ret, Error **errp)
ae82d324 1174{
4e89978e 1175 Error *local_err = NULL;
928059a3 1176 QemuOptsList *opts_list;
ae82d324 1177 QemuOpts *opts;
ae82d324 1178
928059a3 1179 opts_list = qemu_find_opts_err("netdev", &local_err);
84d18f06 1180 if (local_err) {
485febc6 1181 goto out;
ae82d324
MA
1182 }
1183
928059a3 1184 opts = qemu_opts_from_qdict(opts_list, qdict, &local_err);
84d18f06 1185 if (local_err) {
485febc6 1186 goto out;
928059a3
LC
1187 }
1188
1189 netdev_add(opts, &local_err);
84d18f06 1190 if (local_err) {
410cbafe 1191 qemu_opts_del(opts);
485febc6 1192 goto out;
410cbafe
YT
1193 }
1194
485febc6
MA
1195out:
1196 error_propagate(errp, local_err);
ae82d324
MA
1197}
1198
5f964155 1199void qmp_netdev_del(const char *id, Error **errp)
ae82d324 1200{
35277d14 1201 NetClientState *nc;
645c9496 1202 QemuOpts *opts;
ae82d324 1203
35277d14
SH
1204 nc = qemu_find_netdev(id);
1205 if (!nc) {
75158ebb
MA
1206 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1207 "Device '%s' not found", id);
5f964155 1208 return;
ae82d324 1209 }
5f964155 1210
645c9496
SH
1211 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), id);
1212 if (!opts) {
1213 error_setg(errp, "Device '%s' is not a netdev", id);
1214 return;
1215 }
1216
b20c6b9e 1217 qemu_del_net_client(nc);
645c9496 1218 qemu_opts_del(opts);
ae82d324
MA
1219}
1220
aa9156f4
HZ
1221static void netfilter_print_info(Monitor *mon, NetFilterState *nf)
1222{
1223 char *str;
1224 ObjectProperty *prop;
1225 ObjectPropertyIterator iter;
3b098d56 1226 Visitor *v;
aa9156f4
HZ
1227
1228 /* generate info str */
1229 object_property_iter_init(&iter, OBJECT(nf));
1230 while ((prop = object_property_iter_next(&iter))) {
1231 if (!strcmp(prop->name, "type")) {
1232 continue;
1233 }
3b098d56
EB
1234 v = string_output_visitor_new(false, &str);
1235 object_property_get(OBJECT(nf), v, prop->name, NULL);
1236 visit_complete(v, &str);
1237 visit_free(v);
aa9156f4
HZ
1238 monitor_printf(mon, ",%s=%s", prop->name, str);
1239 g_free(str);
1240 }
1241 monitor_printf(mon, "\n");
1242}
1243
1a859593 1244void print_net_client(Monitor *mon, NetClientState *nc)
44e798d3 1245{
a4960f52
YH
1246 NetFilterState *nf;
1247
1ceef9f2
JW
1248 monitor_printf(mon, "%s: index=%d,type=%s,%s\n", nc->name,
1249 nc->queue_index,
977c736f 1250 NetClientDriver_str(nc->info->type),
1ceef9f2 1251 nc->info_str);
a4960f52
YH
1252 if (!QTAILQ_EMPTY(&nc->filters)) {
1253 monitor_printf(mon, "filters:\n");
1254 }
1255 QTAILQ_FOREACH(nf, &nc->filters, next) {
a3e8a3f3 1256 char *path = object_get_canonical_path_component(OBJECT(nf));
aa9156f4
HZ
1257
1258 monitor_printf(mon, " - %s: type=%s", path,
1259 object_get_typename(OBJECT(nf)));
1260 netfilter_print_info(mon, nf);
a3e8a3f3 1261 g_free(path);
a4960f52 1262 }
44e798d3
JK
1263}
1264
b1be4280
AK
1265RxFilterInfoList *qmp_query_rx_filter(bool has_name, const char *name,
1266 Error **errp)
1267{
1268 NetClientState *nc;
1269 RxFilterInfoList *filter_list = NULL, *last_entry = NULL;
1270
1271 QTAILQ_FOREACH(nc, &net_clients, next) {
1272 RxFilterInfoList *entry;
1273 RxFilterInfo *info;
1274
1275 if (has_name && strcmp(nc->name, name) != 0) {
1276 continue;
1277 }
1278
1279 /* only query rx-filter information of NIC */
f394b2e2 1280 if (nc->info->type != NET_CLIENT_DRIVER_NIC) {
b1be4280
AK
1281 if (has_name) {
1282 error_setg(errp, "net client(%s) isn't a NIC", name);
9083da1d 1283 return NULL;
b1be4280
AK
1284 }
1285 continue;
1286 }
1287
5320c2ca
VY
1288 /* only query information on queue 0 since the info is per nic,
1289 * not per queue
1290 */
1291 if (nc->queue_index != 0)
1292 continue;
1293
b1be4280
AK
1294 if (nc->info->query_rx_filter) {
1295 info = nc->info->query_rx_filter(nc);
1296 entry = g_malloc0(sizeof(*entry));
1297 entry->value = info;
1298
1299 if (!filter_list) {
1300 filter_list = entry;
1301 } else {
1302 last_entry->next = entry;
1303 }
1304 last_entry = entry;
1305 } else if (has_name) {
1306 error_setg(errp, "net client(%s) doesn't support"
1307 " rx-filter querying", name);
9083da1d 1308 return NULL;
b1be4280 1309 }
638fb141
MA
1310
1311 if (has_name) {
1312 break;
1313 }
b1be4280
AK
1314 }
1315
9083da1d 1316 if (filter_list == NULL && has_name) {
b1be4280
AK
1317 error_setg(errp, "invalid net client name: %s", name);
1318 }
1319
1320 return filter_list;
1321}
1322
1ce6be24 1323void hmp_info_network(Monitor *mon, const QDict *qdict)
63a01ef8 1324{
35277d14 1325 NetClientState *nc, *peer;
f394b2e2 1326 NetClientDriver type;
63a01ef8 1327
1a859593
ZYW
1328 net_hub_info(mon);
1329
35277d14
SH
1330 QTAILQ_FOREACH(nc, &net_clients, next) {
1331 peer = nc->peer;
1332 type = nc->info->type;
5610c3aa 1333
1a859593
ZYW
1334 /* Skip if already printed in hub info */
1335 if (net_hub_id_for_client(nc, NULL) == 0) {
1336 continue;
5610c3aa 1337 }
1a859593 1338
f394b2e2 1339 if (!peer || type == NET_CLIENT_DRIVER_NIC) {
35277d14 1340 print_net_client(mon, nc);
19061e63 1341 } /* else it's a netdev connected to a NIC, printed with the NIC */
f394b2e2 1342 if (peer && type == NET_CLIENT_DRIVER_NIC) {
1a859593 1343 monitor_printf(mon, " \\ ");
44e798d3 1344 print_net_client(mon, peer);
a0104e0e 1345 }
a0104e0e 1346 }
63a01ef8
AL
1347}
1348
5fbba3d6
ZC
1349void colo_notify_filters_event(int event, Error **errp)
1350{
1351 NetClientState *nc;
1352 NetFilterState *nf;
1353 NetFilterClass *nfc = NULL;
1354 Error *local_err = NULL;
1355
1356 QTAILQ_FOREACH(nc, &net_clients, next) {
1357 QTAILQ_FOREACH(nf, &nc->filters, next) {
1358 nfc = NETFILTER_GET_CLASS(OBJECT(nf));
1359 nfc->handle_event(nf, event, &local_err);
1360 if (local_err) {
1361 error_propagate(errp, local_err);
1362 return;
1363 }
1364 }
1365 }
1366}
1367
4b37156c 1368void qmp_set_link(const char *name, bool up, Error **errp)
436e5e53 1369{
1ceef9f2
JW
1370 NetClientState *ncs[MAX_QUEUE_NUM];
1371 NetClientState *nc;
1372 int queues, i;
436e5e53 1373
1ceef9f2 1374 queues = qemu_find_net_clients_except(name, ncs,
f394b2e2 1375 NET_CLIENT_DRIVER__MAX,
1ceef9f2
JW
1376 MAX_QUEUE_NUM);
1377
1378 if (queues == 0) {
75158ebb
MA
1379 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1380 "Device '%s' not found", name);
4b37156c 1381 return;
436e5e53 1382 }
1ceef9f2 1383 nc = ncs[0];
436e5e53 1384
1ceef9f2
JW
1385 for (i = 0; i < queues; i++) {
1386 ncs[i]->link_down = !up;
1387 }
436e5e53 1388
35277d14
SH
1389 if (nc->info->link_status_changed) {
1390 nc->info->link_status_changed(nc);
665a3b07 1391 }
ab1cbe1c 1392
02d38fcb
VY
1393 if (nc->peer) {
1394 /* Change peer link only if the peer is NIC and then notify peer.
1395 * If the peer is a HUBPORT or a backend, we do not change the
1396 * link status.
1397 *
af1a5c3e 1398 * This behavior is compatible with qemu hubs where there could be
02d38fcb
VY
1399 * multiple clients that can still communicate with each other in
1400 * disconnected mode. For now maintain this compatibility.
1401 */
f394b2e2 1402 if (nc->peer->info->type == NET_CLIENT_DRIVER_NIC) {
02d38fcb
VY
1403 for (i = 0; i < queues; i++) {
1404 ncs[i]->peer->link_down = !up;
1405 }
1406 }
1407 if (nc->peer->info->link_status_changed) {
1408 nc->peer->info->link_status_changed(nc->peer);
1409 }
ab1cbe1c 1410 }
436e5e53
AL
1411}
1412
ca77d85e
MT
1413static void net_vm_change_state_handler(void *opaque, int running,
1414 RunState state)
1415{
625de449
FZ
1416 NetClientState *nc;
1417 NetClientState *tmp;
ca77d85e 1418
625de449
FZ
1419 QTAILQ_FOREACH_SAFE(nc, &net_clients, next, tmp) {
1420 if (running) {
1421 /* Flush queued packets and wake up backends. */
1422 if (nc->peer && qemu_can_send_packet(nc)) {
1423 qemu_flush_queued_packets(nc->peer);
1424 }
1425 } else {
1426 /* Complete all queued packets, to guarantee we don't modify
1427 * state later when VM is not running.
1428 */
ca77d85e
MT
1429 qemu_flush_or_purge_queued_packets(nc, true);
1430 }
1431 }
1432}
1433
63a01ef8
AL
1434void net_cleanup(void)
1435{
1ceef9f2 1436 NetClientState *nc;
577c4af9 1437
1ceef9f2
JW
1438 /* We may del multiple entries during qemu_del_net_client(),
1439 * so QTAILQ_FOREACH_SAFE() is also not safe here.
1440 */
1441 while (!QTAILQ_EMPTY(&net_clients)) {
1442 nc = QTAILQ_FIRST(&net_clients);
f394b2e2 1443 if (nc->info->type == NET_CLIENT_DRIVER_NIC) {
948ecf21
JW
1444 qemu_del_nic(qemu_get_nic(nc));
1445 } else {
1446 qemu_del_net_client(nc);
1447 }
577c4af9 1448 }
ca77d85e
MT
1449
1450 qemu_del_vm_change_state_handler(net_change_state_entry);
63a01ef8
AL
1451}
1452
668680f7 1453void net_check_clients(void)
63a01ef8 1454{
35277d14 1455 NetClientState *nc;
48e2faf2 1456 int i;
63a01ef8 1457
81017645 1458 net_hub_check_clients();
ac60cc18 1459
35277d14
SH
1460 QTAILQ_FOREACH(nc, &net_clients, next) {
1461 if (!nc->peer) {
8297be80 1462 warn_report("%s %s has no peer",
b62e39b4
AF
1463 nc->info->type == NET_CLIENT_DRIVER_NIC
1464 ? "nic" : "netdev",
1465 nc->name);
efe32fdd
MA
1466 }
1467 }
48e2faf2
PM
1468
1469 /* Check that all NICs requested via -net nic actually got created.
1470 * NICs created via -device don't need to be checked here because
1471 * they are always instantiated.
1472 */
1473 for (i = 0; i < MAX_NICS; i++) {
1474 NICInfo *nd = &nd_table[i];
1475 if (nd->used && !nd->instantiated) {
8297be80
AF
1476 warn_report("requested NIC (%s, model %s) "
1477 "was not created (not supported by this machine?)",
1478 nd->name ? nd->name : "anonymous",
1479 nd->model ? nd->model : "unspecified");
48e2faf2
PM
1480 }
1481 }
63a01ef8 1482}
dc1c9fe8 1483
28d0de7a 1484static int net_init_client(void *dummy, QemuOpts *opts, Error **errp)
dc1c9fe8 1485{
34f708b0 1486 return net_client_init(opts, false, errp);
f6b134ac
MM
1487}
1488
28d0de7a 1489static int net_init_netdev(void *dummy, QemuOpts *opts, Error **errp)
f6b134ac 1490{
34f708b0 1491 return net_client_init(opts, true, errp);
dc1c9fe8 1492}
4559a1db 1493
78cd6f7b
TH
1494/* For the convenience "--nic" parameter */
1495static int net_param_nic(void *dummy, QemuOpts *opts, Error **errp)
1496{
1497 char *mac, *nd_id;
1498 int idx, ret;
1499 NICInfo *ni;
1500 const char *type;
1501
1502 type = qemu_opt_get(opts, "type");
1503 if (type && g_str_equal(type, "none")) {
1504 return 0; /* Nothing to do, default_net is cleared in vl.c */
1505 }
1506
1507 idx = nic_get_free_idx();
1508 if (idx == -1 || nb_nics >= MAX_NICS) {
1509 error_setg(errp, "no more on-board/default NIC slots available");
4559a1db
LC
1510 return -1;
1511 }
1512
78cd6f7b
TH
1513 if (!type) {
1514 qemu_opt_set(opts, "type", "user", &error_abort);
1515 }
1516
1517 ni = &nd_table[idx];
1518 memset(ni, 0, sizeof(*ni));
1519 ni->model = qemu_opt_get_del(opts, "model");
1520
1521 /* Create an ID if the user did not specify one */
1522 nd_id = g_strdup(qemu_opts_id(opts));
1523 if (!nd_id) {
1524 nd_id = g_strdup_printf("__org.qemu.nic%i\n", idx);
1525 qemu_opts_set_id(opts, nd_id);
1526 }
1527
1528 /* Handle MAC address */
1529 mac = qemu_opt_get_del(opts, "mac");
1530 if (mac) {
1531 ret = net_parse_macaddr(ni->macaddr.a, mac);
1532 g_free(mac);
1533 if (ret) {
1534 error_setg(errp, "invalid syntax for ethernet address");
9d946191 1535 goto out;
78cd6f7b
TH
1536 }
1537 if (is_multicast_ether_addr(ni->macaddr.a)) {
1538 error_setg(errp, "NIC cannot have multicast MAC address");
9d946191
TH
1539 ret = -1;
1540 goto out;
78cd6f7b
TH
1541 }
1542 }
1543 qemu_macaddr_default_if_unset(&ni->macaddr);
1544
1545 ret = net_client_init(opts, true, errp);
1546 if (ret == 0) {
1547 ni->netdev = qemu_find_netdev(nd_id);
1548 ni->used = true;
1549 nb_nics++;
1550 }
1551
9d946191 1552out:
78cd6f7b 1553 g_free(nd_id);
4559a1db 1554 return ret;
dc1c9fe8
MM
1555}
1556
34f708b0 1557int net_init_clients(Error **errp)
dc1c9fe8 1558{
ca77d85e
MT
1559 net_change_state_entry =
1560 qemu_add_vm_change_state_handler(net_vm_change_state_handler, NULL);
1561
94878994 1562 QTAILQ_INIT(&net_clients);
5610c3aa 1563
28d0de7a 1564 if (qemu_opts_foreach(qemu_find_opts("netdev"),
34f708b0 1565 net_init_netdev, NULL, errp)) {
f6b134ac 1566 return -1;
a4c7367f 1567 }
f6b134ac 1568
78cd6f7b
TH
1569 if (qemu_opts_foreach(qemu_find_opts("nic"), net_param_nic, NULL, errp)) {
1570 return -1;
1571 }
1572
34f708b0 1573 if (qemu_opts_foreach(qemu_find_opts("net"), net_init_client, NULL, errp)) {
dc1c9fe8
MM
1574 return -1;
1575 }
1576
dc1c9fe8
MM
1577 return 0;
1578}
1579
7f161aae 1580int net_client_parse(QemuOptsList *opts_list, const char *optarg)
dc1c9fe8 1581{
70b94331 1582 if (!qemu_opts_parse_noisily(opts_list, optarg, true)) {
dc1c9fe8
MM
1583 return -1;
1584 }
1585
1586 return 0;
1587}
7fc8d918
JW
1588
1589/* From FreeBSD */
1590/* XXX: optimize */
eaba8f34 1591uint32_t net_crc32(const uint8_t *p, int len)
7fc8d918
JW
1592{
1593 uint32_t crc;
1594 int carry, i, j;
1595 uint8_t b;
1596
1597 crc = 0xffffffff;
eaba8f34
MCA
1598 for (i = 0; i < len; i++) {
1599 b = *p++;
7fc8d918
JW
1600 for (j = 0; j < 8; j++) {
1601 carry = ((crc & 0x80000000L) ? 1 : 0) ^ (b & 0x01);
1602 crc <<= 1;
1603 b >>= 1;
1604 if (carry) {
eaba8f34 1605 crc = ((crc ^ POLYNOMIAL_BE) | carry);
7fc8d918
JW
1606 }
1607 }
1608 }
eaba8f34
MCA
1609
1610 return crc;
1611}
1612
f1a7deb9
MCA
1613uint32_t net_crc32_le(const uint8_t *p, int len)
1614{
1615 uint32_t crc;
1616 int carry, i, j;
1617 uint8_t b;
1618
1619 crc = 0xffffffff;
1620 for (i = 0; i < len; i++) {
1621 b = *p++;
1622 for (j = 0; j < 8; j++) {
1623 carry = (crc & 0x1) ^ (b & 0x01);
1624 crc >>= 1;
1625 b >>= 1;
1626 if (carry) {
1627 crc ^= POLYNOMIAL_LE;
1628 }
1629 }
1630 }
1631
1632 return crc;
1633}
1634
4d454574
PB
1635QemuOptsList qemu_netdev_opts = {
1636 .name = "netdev",
1637 .implied_opt_name = "type",
1638 .head = QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts.head),
78cd6f7b
TH
1639 .desc = {
1640 /*
1641 * no elements => accept any params
1642 * validation will happen later
1643 */
1644 { /* end of list */ }
1645 },
1646};
1647
1648QemuOptsList qemu_nic_opts = {
1649 .name = "nic",
1650 .implied_opt_name = "type",
1651 .head = QTAILQ_HEAD_INITIALIZER(qemu_nic_opts.head),
4d454574
PB
1652 .desc = {
1653 /*
1654 * no elements => accept any params
1655 * validation will happen later
1656 */
1657 { /* end of list */ }
1658 },
1659};
1660
1661QemuOptsList qemu_net_opts = {
1662 .name = "net",
1663 .implied_opt_name = "type",
1664 .head = QTAILQ_HEAD_INITIALIZER(qemu_net_opts.head),
1665 .desc = {
1666 /*
1667 * no elements => accept any params
1668 * validation will happen later
1669 */
1670 { /* end of list */ }
1671 },
1672};
16a3df40
ZC
1673
1674void net_socket_rs_init(SocketReadState *rs,
3cde5ea2
ZC
1675 SocketReadStateFinalize *finalize,
1676 bool vnet_hdr)
16a3df40
ZC
1677{
1678 rs->state = 0;
3cde5ea2 1679 rs->vnet_hdr = vnet_hdr;
16a3df40
ZC
1680 rs->index = 0;
1681 rs->packet_len = 0;
3cde5ea2 1682 rs->vnet_hdr_len = 0;
16a3df40
ZC
1683 memset(rs->buf, 0, sizeof(rs->buf));
1684 rs->finalize = finalize;
1685}
1686
1687/*
1688 * Returns
e9e0a585
ZC
1689 * 0: success
1690 * -1: error occurs
16a3df40
ZC
1691 */
1692int net_fill_rstate(SocketReadState *rs, const uint8_t *buf, int size)
1693{
1694 unsigned int l;
1695
1696 while (size > 0) {
3cde5ea2
ZC
1697 /* Reassemble a packet from the network.
1698 * 0 = getting length.
1699 * 1 = getting vnet header length.
1700 * 2 = getting data.
1701 */
1702 switch (rs->state) {
16a3df40
ZC
1703 case 0:
1704 l = 4 - rs->index;
1705 if (l > size) {
1706 l = size;
1707 }
1708 memcpy(rs->buf + rs->index, buf, l);
1709 buf += l;
1710 size -= l;
1711 rs->index += l;
1712 if (rs->index == 4) {
1713 /* got length */
1714 rs->packet_len = ntohl(*(uint32_t *)rs->buf);
1715 rs->index = 0;
3cde5ea2
ZC
1716 if (rs->vnet_hdr) {
1717 rs->state = 1;
1718 } else {
1719 rs->state = 2;
1720 rs->vnet_hdr_len = 0;
1721 }
16a3df40
ZC
1722 }
1723 break;
1724 case 1:
3cde5ea2
ZC
1725 l = 4 - rs->index;
1726 if (l > size) {
1727 l = size;
1728 }
1729 memcpy(rs->buf + rs->index, buf, l);
1730 buf += l;
1731 size -= l;
1732 rs->index += l;
1733 if (rs->index == 4) {
1734 /* got vnet header length */
1735 rs->vnet_hdr_len = ntohl(*(uint32_t *)rs->buf);
1736 rs->index = 0;
1737 rs->state = 2;
1738 }
1739 break;
1740 case 2:
16a3df40
ZC
1741 l = rs->packet_len - rs->index;
1742 if (l > size) {
1743 l = size;
1744 }
1745 if (rs->index + l <= sizeof(rs->buf)) {
1746 memcpy(rs->buf + rs->index, buf, l);
1747 } else {
1748 fprintf(stderr, "serious error: oversized packet received,"
1749 "connection terminated.\n");
1750 rs->index = rs->state = 0;
1751 return -1;
1752 }
1753
1754 rs->index += l;
1755 buf += l;
1756 size -= l;
1757 if (rs->index >= rs->packet_len) {
1758 rs->index = 0;
1759 rs->state = 0;
e79cd406
DB
1760 assert(rs->finalize);
1761 rs->finalize(rs);
16a3df40
ZC
1762 }
1763 break;
1764 }
1765 }
e9e0a585
ZC
1766
1767 assert(size == 0);
16a3df40
ZC
1768 return 0;
1769}