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