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