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