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