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