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