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