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