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