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