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