]> git.proxmox.com Git - mirror_qemu.git/blob - net.c
net: Use hubs for the vlan feature
[mirror_qemu.git] / net.c
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 */
24 #include "net.h"
25
26 #include "config-host.h"
27
28 #include "net/tap.h"
29 #include "net/socket.h"
30 #include "net/dump.h"
31 #include "net/slirp.h"
32 #include "net/vde.h"
33 #include "net/hub.h"
34 #include "net/util.h"
35 #include "monitor.h"
36 #include "qemu-common.h"
37 #include "qemu_socket.h"
38 #include "qmp-commands.h"
39 #include "hw/qdev.h"
40 #include "iov.h"
41 #include "qapi-visit.h"
42 #include "qapi/opts-visitor.h"
43 #include "qapi/qapi-dealloc-visitor.h"
44
45 /* Net bridge is currently not supported for W32. */
46 #if !defined(_WIN32)
47 # define CONFIG_NET_BRIDGE
48 #endif
49
50 static QTAILQ_HEAD(, VLANState) vlans;
51 static QTAILQ_HEAD(, VLANClientState) non_vlan_clients;
52
53 int default_net = 1;
54
55 /***********************************************************/
56 /* network device redirectors */
57
58 #if defined(DEBUG_NET)
59 static 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
86 static 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
106 int 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 {
120 if (qemu_isdigit(buf[0])) {
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
136 void qemu_format_nic_info_str(VLANClientState *vc, uint8_t macaddr[6])
137 {
138 snprintf(vc->info_str, sizeof(vc->info_str),
139 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
140 vc->model,
141 macaddr[0], macaddr[1], macaddr[2],
142 macaddr[3], macaddr[4], macaddr[5]);
143 }
144
145 void 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
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 */
166 static char *assign_name(VLANClientState *vc1, const char *model)
167 {
168 VLANClientState *vc;
169 char buf[256];
170 int id = 0;
171
172 QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
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) {
179 id++;
180 }
181 }
182
183 snprintf(buf, sizeof(buf), "%s.%d", model, id);
184
185 return g_strdup(buf);
186 }
187
188 static ssize_t qemu_deliver_packet(VLANClientState *sender,
189 unsigned flags,
190 const uint8_t *data,
191 size_t size,
192 void *opaque);
193 static ssize_t qemu_deliver_packet_iov(VLANClientState *sender,
194 unsigned flags,
195 const struct iovec *iov,
196 int iovcnt,
197 void *opaque);
198
199 VLANClientState *qemu_new_net_client(NetClientInfo *info,
200 VLANState *vlan,
201 VLANClientState *peer,
202 const char *model,
203 const char *name)
204 {
205 VLANClientState *vc;
206
207 assert(info->size >= sizeof(VLANClientState));
208
209 vc = g_malloc0(info->size);
210
211 vc->info = info;
212 vc->model = g_strdup(model);
213 if (name) {
214 vc->name = g_strdup(name);
215 } else {
216 vc->name = assign_name(vc, model);
217 }
218
219 if (vlan) {
220 assert(!peer);
221 vc->vlan = vlan;
222 QTAILQ_INSERT_TAIL(&vc->vlan->clients, vc, next);
223 } else {
224 if (peer) {
225 assert(!peer->peer);
226 vc->peer = peer;
227 peer->peer = vc;
228 }
229 QTAILQ_INSERT_TAIL(&non_vlan_clients, vc, next);
230
231 vc->send_queue = qemu_new_net_queue(qemu_deliver_packet,
232 qemu_deliver_packet_iov,
233 vc);
234 }
235
236 return vc;
237 }
238
239 NICState *qemu_new_nic(NetClientInfo *info,
240 NICConf *conf,
241 const char *model,
242 const char *name,
243 void *opaque)
244 {
245 VLANClientState *nc;
246 NICState *nic;
247
248 assert(info->type == NET_CLIENT_OPTIONS_KIND_NIC);
249 assert(info->size >= sizeof(NICState));
250
251 nc = qemu_new_net_client(info, conf->vlan, conf->peer, model, name);
252
253 nic = DO_UPCAST(NICState, nc, nc);
254 nic->conf = conf;
255 nic->opaque = opaque;
256
257 return nic;
258 }
259
260 static void qemu_cleanup_vlan_client(VLANClientState *vc)
261 {
262 if (vc->vlan) {
263 QTAILQ_REMOVE(&vc->vlan->clients, vc, next);
264 } else {
265 QTAILQ_REMOVE(&non_vlan_clients, vc, next);
266 }
267
268 if (vc->info->cleanup) {
269 vc->info->cleanup(vc);
270 }
271 }
272
273 static void qemu_free_vlan_client(VLANClientState *vc)
274 {
275 if (!vc->vlan) {
276 if (vc->send_queue) {
277 qemu_del_net_queue(vc->send_queue);
278 }
279 if (vc->peer) {
280 vc->peer->peer = NULL;
281 }
282 }
283 g_free(vc->name);
284 g_free(vc->model);
285 g_free(vc);
286 }
287
288 void qemu_del_vlan_client(VLANClientState *vc)
289 {
290 /* If there is a peer NIC, delete and cleanup client, but do not free. */
291 if (!vc->vlan && vc->peer && vc->peer->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
292 NICState *nic = DO_UPCAST(NICState, nc, vc->peer);
293 if (nic->peer_deleted) {
294 return;
295 }
296 nic->peer_deleted = true;
297 /* Let NIC know peer is gone. */
298 vc->peer->link_down = true;
299 if (vc->peer->info->link_status_changed) {
300 vc->peer->info->link_status_changed(vc->peer);
301 }
302 qemu_cleanup_vlan_client(vc);
303 return;
304 }
305
306 /* If this is a peer NIC and peer has already been deleted, free it now. */
307 if (!vc->vlan && vc->peer && vc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
308 NICState *nic = DO_UPCAST(NICState, nc, vc);
309 if (nic->peer_deleted) {
310 qemu_free_vlan_client(vc->peer);
311 }
312 }
313
314 qemu_cleanup_vlan_client(vc);
315 qemu_free_vlan_client(vc);
316 }
317
318 VLANClientState *
319 qemu_find_vlan_client_by_name(Monitor *mon, int vlan_id,
320 const char *client_str)
321 {
322 VLANState *vlan;
323 VLANClientState *vc;
324
325 vlan = qemu_find_vlan(vlan_id, 0);
326 if (!vlan) {
327 monitor_printf(mon, "unknown VLAN %d\n", vlan_id);
328 return NULL;
329 }
330
331 QTAILQ_FOREACH(vc, &vlan->clients, next) {
332 if (!strcmp(vc->name, client_str)) {
333 break;
334 }
335 }
336 if (!vc) {
337 monitor_printf(mon, "can't find device %s on VLAN %d\n",
338 client_str, vlan_id);
339 }
340
341 return vc;
342 }
343
344 void qemu_foreach_nic(qemu_nic_foreach func, void *opaque)
345 {
346 VLANClientState *nc;
347 VLANState *vlan;
348
349 QTAILQ_FOREACH(nc, &non_vlan_clients, next) {
350 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
351 func(DO_UPCAST(NICState, nc, nc), opaque);
352 }
353 }
354
355 QTAILQ_FOREACH(vlan, &vlans, next) {
356 QTAILQ_FOREACH(nc, &vlan->clients, next) {
357 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
358 func(DO_UPCAST(NICState, nc, nc), opaque);
359 }
360 }
361 }
362 }
363
364 int qemu_can_send_packet(VLANClientState *sender)
365 {
366 VLANState *vlan = sender->vlan;
367 VLANClientState *vc;
368
369 if (sender->peer) {
370 if (sender->peer->receive_disabled) {
371 return 0;
372 } else if (sender->peer->info->can_receive &&
373 !sender->peer->info->can_receive(sender->peer)) {
374 return 0;
375 } else {
376 return 1;
377 }
378 }
379
380 if (!sender->vlan) {
381 return 1;
382 }
383
384 QTAILQ_FOREACH(vc, &vlan->clients, next) {
385 if (vc == sender) {
386 continue;
387 }
388
389 /* no can_receive() handler, they can always receive */
390 if (vc->info->can_receive && !vc->info->can_receive(vc)) {
391 return 0;
392 }
393 }
394 return 1;
395 }
396
397 static ssize_t qemu_deliver_packet(VLANClientState *sender,
398 unsigned flags,
399 const uint8_t *data,
400 size_t size,
401 void *opaque)
402 {
403 VLANClientState *vc = opaque;
404 ssize_t ret;
405
406 if (vc->link_down) {
407 return size;
408 }
409
410 if (vc->receive_disabled) {
411 return 0;
412 }
413
414 if (flags & QEMU_NET_PACKET_FLAG_RAW && vc->info->receive_raw) {
415 ret = vc->info->receive_raw(vc, data, size);
416 } else {
417 ret = vc->info->receive(vc, data, size);
418 }
419
420 if (ret == 0) {
421 vc->receive_disabled = 1;
422 };
423
424 return ret;
425 }
426
427 static ssize_t qemu_vlan_deliver_packet(VLANClientState *sender,
428 unsigned flags,
429 const uint8_t *buf,
430 size_t size,
431 void *opaque)
432 {
433 VLANState *vlan = opaque;
434 VLANClientState *vc;
435 ssize_t ret = -1;
436
437 QTAILQ_FOREACH(vc, &vlan->clients, next) {
438 ssize_t len;
439
440 if (vc == sender) {
441 continue;
442 }
443
444 if (vc->link_down) {
445 ret = size;
446 continue;
447 }
448
449 if (vc->receive_disabled) {
450 ret = 0;
451 continue;
452 }
453
454 if (flags & QEMU_NET_PACKET_FLAG_RAW && vc->info->receive_raw) {
455 len = vc->info->receive_raw(vc, buf, size);
456 } else {
457 len = vc->info->receive(vc, buf, size);
458 }
459
460 if (len == 0) {
461 vc->receive_disabled = 1;
462 }
463
464 ret = (ret >= 0) ? ret : len;
465
466 }
467
468 return ret;
469 }
470
471 void qemu_purge_queued_packets(VLANClientState *vc)
472 {
473 NetQueue *queue;
474
475 if (!vc->peer && !vc->vlan) {
476 return;
477 }
478
479 if (vc->peer) {
480 queue = vc->peer->send_queue;
481 } else {
482 queue = vc->vlan->send_queue;
483 }
484
485 qemu_net_queue_purge(queue, vc);
486 }
487
488 void qemu_flush_queued_packets(VLANClientState *vc)
489 {
490 NetQueue *queue;
491
492 vc->receive_disabled = 0;
493
494 if (vc->vlan) {
495 queue = vc->vlan->send_queue;
496 } else {
497 queue = vc->send_queue;
498 }
499
500 qemu_net_queue_flush(queue);
501 }
502
503 static ssize_t qemu_send_packet_async_with_flags(VLANClientState *sender,
504 unsigned flags,
505 const uint8_t *buf, int size,
506 NetPacketSent *sent_cb)
507 {
508 NetQueue *queue;
509
510 #ifdef DEBUG_NET
511 printf("qemu_send_packet_async:\n");
512 hex_dump(stdout, buf, size);
513 #endif
514
515 if (sender->link_down || (!sender->peer && !sender->vlan)) {
516 return size;
517 }
518
519 if (sender->peer) {
520 queue = sender->peer->send_queue;
521 } else {
522 queue = sender->vlan->send_queue;
523 }
524
525 return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb);
526 }
527
528 ssize_t qemu_send_packet_async(VLANClientState *sender,
529 const uint8_t *buf, int size,
530 NetPacketSent *sent_cb)
531 {
532 return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE,
533 buf, size, sent_cb);
534 }
535
536 void qemu_send_packet(VLANClientState *vc, const uint8_t *buf, int size)
537 {
538 qemu_send_packet_async(vc, buf, size, NULL);
539 }
540
541 ssize_t qemu_send_packet_raw(VLANClientState *vc, const uint8_t *buf, int size)
542 {
543 return qemu_send_packet_async_with_flags(vc, QEMU_NET_PACKET_FLAG_RAW,
544 buf, size, NULL);
545 }
546
547 static ssize_t vc_sendv_compat(VLANClientState *vc, const struct iovec *iov,
548 int iovcnt)
549 {
550 uint8_t buffer[4096];
551 size_t offset;
552
553 offset = iov_to_buf(iov, iovcnt, 0, buffer, sizeof(buffer));
554
555 return vc->info->receive(vc, buffer, offset);
556 }
557
558 static ssize_t qemu_deliver_packet_iov(VLANClientState *sender,
559 unsigned flags,
560 const struct iovec *iov,
561 int iovcnt,
562 void *opaque)
563 {
564 VLANClientState *vc = opaque;
565
566 if (vc->link_down) {
567 return iov_size(iov, iovcnt);
568 }
569
570 if (vc->info->receive_iov) {
571 return vc->info->receive_iov(vc, iov, iovcnt);
572 } else {
573 return vc_sendv_compat(vc, iov, iovcnt);
574 }
575 }
576
577 static ssize_t qemu_vlan_deliver_packet_iov(VLANClientState *sender,
578 unsigned flags,
579 const struct iovec *iov,
580 int iovcnt,
581 void *opaque)
582 {
583 VLANState *vlan = opaque;
584 VLANClientState *vc;
585 ssize_t ret = -1;
586
587 QTAILQ_FOREACH(vc, &vlan->clients, next) {
588 ssize_t len;
589
590 if (vc == sender) {
591 continue;
592 }
593
594 if (vc->link_down) {
595 ret = iov_size(iov, iovcnt);
596 continue;
597 }
598
599 assert(!(flags & QEMU_NET_PACKET_FLAG_RAW));
600
601 if (vc->info->receive_iov) {
602 len = vc->info->receive_iov(vc, iov, iovcnt);
603 } else {
604 len = vc_sendv_compat(vc, iov, iovcnt);
605 }
606
607 ret = (ret >= 0) ? ret : len;
608 }
609
610 return ret;
611 }
612
613 ssize_t qemu_sendv_packet_async(VLANClientState *sender,
614 const struct iovec *iov, int iovcnt,
615 NetPacketSent *sent_cb)
616 {
617 NetQueue *queue;
618
619 if (sender->link_down || (!sender->peer && !sender->vlan)) {
620 return iov_size(iov, iovcnt);
621 }
622
623 if (sender->peer) {
624 queue = sender->peer->send_queue;
625 } else {
626 queue = sender->vlan->send_queue;
627 }
628
629 return qemu_net_queue_send_iov(queue, sender,
630 QEMU_NET_PACKET_FLAG_NONE,
631 iov, iovcnt, sent_cb);
632 }
633
634 ssize_t
635 qemu_sendv_packet(VLANClientState *vc, const struct iovec *iov, int iovcnt)
636 {
637 return qemu_sendv_packet_async(vc, iov, iovcnt, NULL);
638 }
639
640 /* find or alloc a new VLAN */
641 VLANState *qemu_find_vlan(int id, int allocate)
642 {
643 VLANState *vlan;
644
645 QTAILQ_FOREACH(vlan, &vlans, next) {
646 if (vlan->id == id) {
647 return vlan;
648 }
649 }
650
651 if (!allocate) {
652 return NULL;
653 }
654
655 vlan = g_malloc0(sizeof(VLANState));
656 vlan->id = id;
657 QTAILQ_INIT(&vlan->clients);
658
659 vlan->send_queue = qemu_new_net_queue(qemu_vlan_deliver_packet,
660 qemu_vlan_deliver_packet_iov,
661 vlan);
662
663 QTAILQ_INSERT_TAIL(&vlans, vlan, next);
664
665 return vlan;
666 }
667
668 VLANClientState *qemu_find_netdev(const char *id)
669 {
670 VLANClientState *vc;
671
672 QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
673 if (vc->info->type == NET_CLIENT_OPTIONS_KIND_NIC)
674 continue;
675 if (!strcmp(vc->name, id)) {
676 return vc;
677 }
678 }
679
680 return NULL;
681 }
682
683 static int nic_get_free_idx(void)
684 {
685 int index;
686
687 for (index = 0; index < MAX_NICS; index++)
688 if (!nd_table[index].used)
689 return index;
690 return -1;
691 }
692
693 int qemu_show_nic_models(const char *arg, const char *const *models)
694 {
695 int i;
696
697 if (!arg || strcmp(arg, "?"))
698 return 0;
699
700 fprintf(stderr, "qemu: Supported NIC models: ");
701 for (i = 0 ; models[i]; i++)
702 fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n');
703 return 1;
704 }
705
706 void qemu_check_nic_model(NICInfo *nd, const char *model)
707 {
708 const char *models[2];
709
710 models[0] = model;
711 models[1] = NULL;
712
713 if (qemu_show_nic_models(nd->model, models))
714 exit(0);
715 if (qemu_find_nic_model(nd, models, model) < 0)
716 exit(1);
717 }
718
719 int qemu_find_nic_model(NICInfo *nd, const char * const *models,
720 const char *default_model)
721 {
722 int i;
723
724 if (!nd->model)
725 nd->model = g_strdup(default_model);
726
727 for (i = 0 ; models[i]; i++) {
728 if (strcmp(nd->model, models[i]) == 0)
729 return i;
730 }
731
732 error_report("Unsupported NIC model: %s", nd->model);
733 return -1;
734 }
735
736 int net_handle_fd_param(Monitor *mon, const char *param)
737 {
738 int fd;
739
740 if (!qemu_isdigit(param[0]) && mon) {
741
742 fd = monitor_get_fd(mon, param);
743 if (fd == -1) {
744 error_report("No file descriptor named %s found", param);
745 return -1;
746 }
747 } else {
748 fd = qemu_parse_fd(param);
749 }
750
751 return fd;
752 }
753
754 static int net_init_nic(const NetClientOptions *opts, const char *name,
755 VLANClientState *peer)
756 {
757 int idx;
758 NICInfo *nd;
759 const NetLegacyNicOptions *nic;
760
761 assert(opts->kind == NET_CLIENT_OPTIONS_KIND_NIC);
762 nic = opts->nic;
763
764 idx = nic_get_free_idx();
765 if (idx == -1 || nb_nics >= MAX_NICS) {
766 error_report("Too Many NICs");
767 return -1;
768 }
769
770 nd = &nd_table[idx];
771
772 memset(nd, 0, sizeof(*nd));
773
774 if (nic->has_netdev) {
775 nd->netdev = qemu_find_netdev(nic->netdev);
776 if (!nd->netdev) {
777 error_report("netdev '%s' not found", nic->netdev);
778 return -1;
779 }
780 } else {
781 assert(peer);
782 nd->netdev = peer;
783 }
784 if (name) {
785 nd->name = g_strdup(name);
786 }
787 if (nic->has_model) {
788 nd->model = g_strdup(nic->model);
789 }
790 if (nic->has_addr) {
791 nd->devaddr = g_strdup(nic->addr);
792 }
793
794 if (nic->has_macaddr &&
795 net_parse_macaddr(nd->macaddr.a, nic->macaddr) < 0) {
796 error_report("invalid syntax for ethernet address");
797 return -1;
798 }
799 qemu_macaddr_default_if_unset(&nd->macaddr);
800
801 if (nic->has_vectors) {
802 if (nic->vectors > 0x7ffffff) {
803 error_report("invalid # of vectors: %"PRIu32, nic->vectors);
804 return -1;
805 }
806 nd->nvectors = nic->vectors;
807 } else {
808 nd->nvectors = DEV_NVECTORS_UNSPECIFIED;
809 }
810
811 nd->used = 1;
812 nb_nics++;
813
814 return idx;
815 }
816
817
818 static int (* const net_client_init_fun[NET_CLIENT_OPTIONS_KIND_MAX])(
819 const NetClientOptions *opts,
820 const char *name,
821 VLANClientState *peer) = {
822 [NET_CLIENT_OPTIONS_KIND_NIC] = net_init_nic,
823 #ifdef CONFIG_SLIRP
824 [NET_CLIENT_OPTIONS_KIND_USER] = net_init_slirp,
825 #endif
826 [NET_CLIENT_OPTIONS_KIND_TAP] = net_init_tap,
827 [NET_CLIENT_OPTIONS_KIND_SOCKET] = net_init_socket,
828 #ifdef CONFIG_VDE
829 [NET_CLIENT_OPTIONS_KIND_VDE] = net_init_vde,
830 #endif
831 [NET_CLIENT_OPTIONS_KIND_DUMP] = net_init_dump,
832 #ifdef CONFIG_NET_BRIDGE
833 [NET_CLIENT_OPTIONS_KIND_BRIDGE] = net_init_bridge,
834 #endif
835 [NET_CLIENT_OPTIONS_KIND_HUBPORT] = net_init_hubport,
836 };
837
838
839 static int net_client_init1(const void *object, int is_netdev, Error **errp)
840 {
841 union {
842 const Netdev *netdev;
843 const NetLegacy *net;
844 } u;
845 const NetClientOptions *opts;
846 const char *name;
847
848 if (is_netdev) {
849 u.netdev = object;
850 opts = u.netdev->opts;
851 name = u.netdev->id;
852
853 switch (opts->kind) {
854 #ifdef CONFIG_SLIRP
855 case NET_CLIENT_OPTIONS_KIND_USER:
856 #endif
857 case NET_CLIENT_OPTIONS_KIND_TAP:
858 case NET_CLIENT_OPTIONS_KIND_SOCKET:
859 #ifdef CONFIG_VDE
860 case NET_CLIENT_OPTIONS_KIND_VDE:
861 #endif
862 #ifdef CONFIG_NET_BRIDGE
863 case NET_CLIENT_OPTIONS_KIND_BRIDGE:
864 #endif
865 case NET_CLIENT_OPTIONS_KIND_HUBPORT:
866 break;
867
868 default:
869 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "type",
870 "a netdev backend type");
871 return -1;
872 }
873 } else {
874 u.net = object;
875 opts = u.net->opts;
876 /* missing optional values have been initialized to "all bits zero" */
877 name = u.net->has_id ? u.net->id : u.net->name;
878 }
879
880 if (net_client_init_fun[opts->kind]) {
881 VLANClientState *peer = NULL;
882
883 /* Do not add to a vlan if it's a -netdev or a nic with a netdev=
884 * parameter. */
885 if (!is_netdev &&
886 (opts->kind != NET_CLIENT_OPTIONS_KIND_NIC ||
887 !opts->nic->has_netdev)) {
888 peer = net_hub_add_port(u.net->has_vlan ? u.net->vlan : 0, NULL);
889 }
890
891 if (net_client_init_fun[opts->kind](opts, name, peer) < 0) {
892 /* TODO push error reporting into init() methods */
893 error_set(errp, QERR_DEVICE_INIT_FAILED,
894 NetClientOptionsKind_lookup[opts->kind]);
895 return -1;
896 }
897 }
898 return 0;
899 }
900
901
902 static void net_visit(Visitor *v, int is_netdev, void **object, Error **errp)
903 {
904 if (is_netdev) {
905 visit_type_Netdev(v, (Netdev **)object, NULL, errp);
906 } else {
907 visit_type_NetLegacy(v, (NetLegacy **)object, NULL, errp);
908 }
909 }
910
911
912 int net_client_init(QemuOpts *opts, int is_netdev, Error **errp)
913 {
914 void *object = NULL;
915 Error *err = NULL;
916 int ret = -1;
917
918 {
919 OptsVisitor *ov = opts_visitor_new(opts);
920
921 net_visit(opts_get_visitor(ov), is_netdev, &object, &err);
922 opts_visitor_cleanup(ov);
923 }
924
925 if (!err) {
926 ret = net_client_init1(object, is_netdev, &err);
927 }
928
929 if (object) {
930 QapiDeallocVisitor *dv = qapi_dealloc_visitor_new();
931
932 net_visit(qapi_dealloc_get_visitor(dv), is_netdev, &object, NULL);
933 qapi_dealloc_visitor_cleanup(dv);
934 }
935
936 error_propagate(errp, err);
937 return ret;
938 }
939
940
941 static int net_host_check_device(const char *device)
942 {
943 int i;
944 const char *valid_param_list[] = { "tap", "socket", "dump"
945 #ifdef CONFIG_NET_BRIDGE
946 , "bridge"
947 #endif
948 #ifdef CONFIG_SLIRP
949 ,"user"
950 #endif
951 #ifdef CONFIG_VDE
952 ,"vde"
953 #endif
954 };
955 for (i = 0; i < sizeof(valid_param_list) / sizeof(char *); i++) {
956 if (!strncmp(valid_param_list[i], device,
957 strlen(valid_param_list[i])))
958 return 1;
959 }
960
961 return 0;
962 }
963
964 void net_host_device_add(Monitor *mon, const QDict *qdict)
965 {
966 const char *device = qdict_get_str(qdict, "device");
967 const char *opts_str = qdict_get_try_str(qdict, "opts");
968 Error *local_err = NULL;
969 QemuOpts *opts;
970
971 if (!net_host_check_device(device)) {
972 monitor_printf(mon, "invalid host network device %s\n", device);
973 return;
974 }
975
976 opts = qemu_opts_parse(qemu_find_opts("net"), opts_str ? opts_str : "", 0);
977 if (!opts) {
978 return;
979 }
980
981 qemu_opt_set(opts, "type", device);
982
983 net_client_init(opts, 0, &local_err);
984 if (error_is_set(&local_err)) {
985 qerror_report_err(local_err);
986 error_free(local_err);
987 monitor_printf(mon, "adding host network device %s failed\n", device);
988 }
989 }
990
991 void net_host_device_remove(Monitor *mon, const QDict *qdict)
992 {
993 VLANClientState *vc;
994 int vlan_id = qdict_get_int(qdict, "vlan_id");
995 const char *device = qdict_get_str(qdict, "device");
996
997 vc = qemu_find_vlan_client_by_name(mon, vlan_id, device);
998 if (!vc) {
999 return;
1000 }
1001 if (!net_host_check_device(vc->model)) {
1002 monitor_printf(mon, "invalid host network device %s\n", device);
1003 return;
1004 }
1005 qemu_del_vlan_client(vc);
1006 }
1007
1008 void netdev_add(QemuOpts *opts, Error **errp)
1009 {
1010 net_client_init(opts, 1, errp);
1011 }
1012
1013 int qmp_netdev_add(Monitor *mon, const QDict *qdict, QObject **ret)
1014 {
1015 Error *local_err = NULL;
1016 QemuOptsList *opts_list;
1017 QemuOpts *opts;
1018
1019 opts_list = qemu_find_opts_err("netdev", &local_err);
1020 if (error_is_set(&local_err)) {
1021 goto exit_err;
1022 }
1023
1024 opts = qemu_opts_from_qdict(opts_list, qdict, &local_err);
1025 if (error_is_set(&local_err)) {
1026 goto exit_err;
1027 }
1028
1029 netdev_add(opts, &local_err);
1030 if (error_is_set(&local_err)) {
1031 qemu_opts_del(opts);
1032 goto exit_err;
1033 }
1034
1035 return 0;
1036
1037 exit_err:
1038 qerror_report_err(local_err);
1039 error_free(local_err);
1040 return -1;
1041 }
1042
1043 void qmp_netdev_del(const char *id, Error **errp)
1044 {
1045 VLANClientState *vc;
1046
1047 vc = qemu_find_netdev(id);
1048 if (!vc) {
1049 error_set(errp, QERR_DEVICE_NOT_FOUND, id);
1050 return;
1051 }
1052
1053 qemu_del_vlan_client(vc);
1054 qemu_opts_del(qemu_opts_find(qemu_find_opts_err("netdev", errp), id));
1055 }
1056
1057 static void print_net_client(Monitor *mon, VLANClientState *vc)
1058 {
1059 monitor_printf(mon, "%s: type=%s,%s\n", vc->name,
1060 NetClientOptionsKind_lookup[vc->info->type], vc->info_str);
1061 }
1062
1063 void do_info_network(Monitor *mon)
1064 {
1065 VLANState *vlan;
1066 VLANClientState *vc, *peer;
1067 NetClientOptionsKind type;
1068
1069 QTAILQ_FOREACH(vlan, &vlans, next) {
1070 monitor_printf(mon, "VLAN %d devices:\n", vlan->id);
1071
1072 QTAILQ_FOREACH(vc, &vlan->clients, next) {
1073 monitor_printf(mon, " ");
1074 print_net_client(mon, vc);
1075 }
1076 }
1077 monitor_printf(mon, "Devices not on any VLAN:\n");
1078 QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
1079 peer = vc->peer;
1080 type = vc->info->type;
1081 if (!peer || type == NET_CLIENT_OPTIONS_KIND_NIC) {
1082 monitor_printf(mon, " ");
1083 print_net_client(mon, vc);
1084 } /* else it's a netdev connected to a NIC, printed with the NIC */
1085 if (peer && type == NET_CLIENT_OPTIONS_KIND_NIC) {
1086 monitor_printf(mon, " \\ ");
1087 print_net_client(mon, peer);
1088 }
1089 }
1090 net_hub_info(mon);
1091 }
1092
1093 void qmp_set_link(const char *name, bool up, Error **errp)
1094 {
1095 VLANState *vlan;
1096 VLANClientState *vc = NULL;
1097
1098 QTAILQ_FOREACH(vlan, &vlans, next) {
1099 QTAILQ_FOREACH(vc, &vlan->clients, next) {
1100 if (strcmp(vc->name, name) == 0) {
1101 goto done;
1102 }
1103 }
1104 }
1105 QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
1106 if (!strcmp(vc->name, name)) {
1107 goto done;
1108 }
1109 }
1110 done:
1111
1112 if (!vc) {
1113 error_set(errp, QERR_DEVICE_NOT_FOUND, name);
1114 return;
1115 }
1116
1117 vc->link_down = !up;
1118
1119 if (vc->info->link_status_changed) {
1120 vc->info->link_status_changed(vc);
1121 }
1122
1123 /* Notify peer. Don't update peer link status: this makes it possible to
1124 * disconnect from host network without notifying the guest.
1125 * FIXME: is disconnected link status change operation useful?
1126 *
1127 * Current behaviour is compatible with qemu vlans where there could be
1128 * multiple clients that can still communicate with each other in
1129 * disconnected mode. For now maintain this compatibility. */
1130 if (vc->peer && vc->peer->info->link_status_changed) {
1131 vc->peer->info->link_status_changed(vc->peer);
1132 }
1133 }
1134
1135 void net_cleanup(void)
1136 {
1137 VLANState *vlan;
1138 VLANClientState *vc, *next_vc;
1139
1140 QTAILQ_FOREACH(vlan, &vlans, next) {
1141 QTAILQ_FOREACH_SAFE(vc, &vlan->clients, next, next_vc) {
1142 qemu_del_vlan_client(vc);
1143 }
1144 }
1145
1146 QTAILQ_FOREACH_SAFE(vc, &non_vlan_clients, next, next_vc) {
1147 qemu_del_vlan_client(vc);
1148 }
1149 }
1150
1151 void net_check_clients(void)
1152 {
1153 VLANState *vlan;
1154 VLANClientState *vc;
1155 int i;
1156
1157 /* Don't warn about the default network setup that you get if
1158 * no command line -net or -netdev options are specified. There
1159 * are two cases that we would otherwise complain about:
1160 * (1) board doesn't support a NIC but the implicit "-net nic"
1161 * requested one
1162 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
1163 * sets up a nic that isn't connected to anything.
1164 */
1165 if (default_net) {
1166 return;
1167 }
1168
1169 QTAILQ_FOREACH(vlan, &vlans, next) {
1170 int has_nic = 0, has_host_dev = 0;
1171
1172 QTAILQ_FOREACH(vc, &vlan->clients, next) {
1173 switch (vc->info->type) {
1174 case NET_CLIENT_OPTIONS_KIND_NIC:
1175 has_nic = 1;
1176 break;
1177 case NET_CLIENT_OPTIONS_KIND_USER:
1178 case NET_CLIENT_OPTIONS_KIND_TAP:
1179 case NET_CLIENT_OPTIONS_KIND_SOCKET:
1180 case NET_CLIENT_OPTIONS_KIND_VDE:
1181 has_host_dev = 1;
1182 break;
1183 default: ;
1184 }
1185 }
1186 if (has_host_dev && !has_nic)
1187 fprintf(stderr, "Warning: vlan %d with no nics\n", vlan->id);
1188 if (has_nic && !has_host_dev)
1189 fprintf(stderr,
1190 "Warning: vlan %d is not connected to host network\n",
1191 vlan->id);
1192 }
1193 QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
1194 if (!vc->peer) {
1195 fprintf(stderr, "Warning: %s %s has no peer\n",
1196 vc->info->type == NET_CLIENT_OPTIONS_KIND_NIC ? "nic" : "netdev",
1197 vc->name);
1198 }
1199 }
1200
1201 /* Check that all NICs requested via -net nic actually got created.
1202 * NICs created via -device don't need to be checked here because
1203 * they are always instantiated.
1204 */
1205 for (i = 0; i < MAX_NICS; i++) {
1206 NICInfo *nd = &nd_table[i];
1207 if (nd->used && !nd->instantiated) {
1208 fprintf(stderr, "Warning: requested NIC (%s, model %s) "
1209 "was not created (not supported by this machine?)\n",
1210 nd->name ? nd->name : "anonymous",
1211 nd->model ? nd->model : "unspecified");
1212 }
1213 }
1214 }
1215
1216 static int net_init_client(QemuOpts *opts, void *dummy)
1217 {
1218 Error *local_err = NULL;
1219
1220 net_client_init(opts, 0, &local_err);
1221 if (error_is_set(&local_err)) {
1222 qerror_report_err(local_err);
1223 error_free(local_err);
1224 return -1;
1225 }
1226
1227 return 0;
1228 }
1229
1230 static int net_init_netdev(QemuOpts *opts, void *dummy)
1231 {
1232 Error *local_err = NULL;
1233 int ret;
1234
1235 ret = net_client_init(opts, 1, &local_err);
1236 if (error_is_set(&local_err)) {
1237 qerror_report_err(local_err);
1238 error_free(local_err);
1239 return -1;
1240 }
1241
1242 return ret;
1243 }
1244
1245 int net_init_clients(void)
1246 {
1247 QemuOptsList *net = qemu_find_opts("net");
1248
1249 if (default_net) {
1250 /* if no clients, we use a default config */
1251 qemu_opts_set(net, NULL, "type", "nic");
1252 #ifdef CONFIG_SLIRP
1253 qemu_opts_set(net, NULL, "type", "user");
1254 #endif
1255 }
1256
1257 QTAILQ_INIT(&vlans);
1258 QTAILQ_INIT(&non_vlan_clients);
1259
1260 if (qemu_opts_foreach(qemu_find_opts("netdev"), net_init_netdev, NULL, 1) == -1)
1261 return -1;
1262
1263 if (qemu_opts_foreach(net, net_init_client, NULL, 1) == -1) {
1264 return -1;
1265 }
1266
1267 return 0;
1268 }
1269
1270 int net_client_parse(QemuOptsList *opts_list, const char *optarg)
1271 {
1272 #if defined(CONFIG_SLIRP)
1273 int ret;
1274 if (net_slirp_parse_legacy(opts_list, optarg, &ret)) {
1275 return ret;
1276 }
1277 #endif
1278
1279 if (!qemu_opts_parse(opts_list, optarg, 1)) {
1280 return -1;
1281 }
1282
1283 default_net = 0;
1284 return 0;
1285 }
1286
1287 /* From FreeBSD */
1288 /* XXX: optimize */
1289 unsigned compute_mcast_idx(const uint8_t *ep)
1290 {
1291 uint32_t crc;
1292 int carry, i, j;
1293 uint8_t b;
1294
1295 crc = 0xffffffff;
1296 for (i = 0; i < 6; i++) {
1297 b = *ep++;
1298 for (j = 0; j < 8; j++) {
1299 carry = ((crc & 0x80000000L) ? 1 : 0) ^ (b & 0x01);
1300 crc <<= 1;
1301 b >>= 1;
1302 if (carry) {
1303 crc = ((crc ^ POLYNOMIAL) | carry);
1304 }
1305 }
1306 }
1307 return crc >> 26;
1308 }