.gitignore: add qemu-bridge-helper and option rom build products
[qemu/opensuse.git] / net.c
blobc34474fe5ffd4ebada824ffe5081c2ae32be0db5
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
24 #include "net.h"
26 #include "config-host.h"
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/util.h"
34 #include "monitor.h"
35 #include "qemu-common.h"
36 #include "qemu_socket.h"
37 #include "qmp-commands.h"
38 #include "hw/qdev.h"
39 #include "iov.h"
41 /* Net bridge is currently not supported for W32. */
42 #if !defined(_WIN32)
43 # define CONFIG_NET_BRIDGE
44 #endif
46 static QTAILQ_HEAD(, VLANState) vlans;
47 static QTAILQ_HEAD(, VLANClientState) non_vlan_clients;
49 int default_net = 1;
51 /***********************************************************/
52 /* network device redirectors */
54 #if defined(DEBUG_NET)
55 static void hex_dump(FILE *f, const uint8_t *buf, int size)
57 int len, i, j, c;
59 for(i=0;i<size;i+=16) {
60 len = size - i;
61 if (len > 16)
62 len = 16;
63 fprintf(f, "%08x ", i);
64 for(j=0;j<16;j++) {
65 if (j < len)
66 fprintf(f, " %02x", buf[i+j]);
67 else
68 fprintf(f, " ");
70 fprintf(f, " ");
71 for(j=0;j<len;j++) {
72 c = buf[i+j];
73 if (c < ' ' || c > '~')
74 c = '.';
75 fprintf(f, "%c", c);
77 fprintf(f, "\n");
80 #endif
82 static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
84 const char *p, *p1;
85 int len;
86 p = *pp;
87 p1 = strchr(p, sep);
88 if (!p1)
89 return -1;
90 len = p1 - p;
91 p1++;
92 if (buf_size > 0) {
93 if (len > buf_size - 1)
94 len = buf_size - 1;
95 memcpy(buf, p, len);
96 buf[len] = '\0';
98 *pp = p1;
99 return 0;
102 int parse_host_port(struct sockaddr_in *saddr, const char *str)
104 char buf[512];
105 struct hostent *he;
106 const char *p, *r;
107 int port;
109 p = str;
110 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
111 return -1;
112 saddr->sin_family = AF_INET;
113 if (buf[0] == '\0') {
114 saddr->sin_addr.s_addr = 0;
115 } else {
116 if (qemu_isdigit(buf[0])) {
117 if (!inet_aton(buf, &saddr->sin_addr))
118 return -1;
119 } else {
120 if ((he = gethostbyname(buf)) == NULL)
121 return - 1;
122 saddr->sin_addr = *(struct in_addr *)he->h_addr;
125 port = strtol(p, (char **)&r, 0);
126 if (r == p)
127 return -1;
128 saddr->sin_port = htons(port);
129 return 0;
132 void qemu_format_nic_info_str(VLANClientState *vc, uint8_t macaddr[6])
134 snprintf(vc->info_str, sizeof(vc->info_str),
135 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
136 vc->model,
137 macaddr[0], macaddr[1], macaddr[2],
138 macaddr[3], macaddr[4], macaddr[5]);
141 void qemu_macaddr_default_if_unset(MACAddr *macaddr)
143 static int index = 0;
144 static const MACAddr zero = { .a = { 0,0,0,0,0,0 } };
146 if (memcmp(macaddr, &zero, sizeof(zero)) != 0)
147 return;
148 macaddr->a[0] = 0x52;
149 macaddr->a[1] = 0x54;
150 macaddr->a[2] = 0x00;
151 macaddr->a[3] = 0x12;
152 macaddr->a[4] = 0x34;
153 macaddr->a[5] = 0x56 + index++;
156 static char *assign_name(VLANClientState *vc1, const char *model)
158 VLANState *vlan;
159 VLANClientState *vc;
160 char buf[256];
161 int id = 0;
163 QTAILQ_FOREACH(vlan, &vlans, next) {
164 QTAILQ_FOREACH(vc, &vlan->clients, next) {
165 if (vc != vc1 && strcmp(vc->model, model) == 0) {
166 id++;
171 QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
172 if (vc != vc1 && strcmp(vc->model, model) == 0) {
173 id++;
177 snprintf(buf, sizeof(buf), "%s.%d", model, id);
179 return g_strdup(buf);
182 static ssize_t qemu_deliver_packet(VLANClientState *sender,
183 unsigned flags,
184 const uint8_t *data,
185 size_t size,
186 void *opaque);
187 static ssize_t qemu_deliver_packet_iov(VLANClientState *sender,
188 unsigned flags,
189 const struct iovec *iov,
190 int iovcnt,
191 void *opaque);
193 VLANClientState *qemu_new_net_client(NetClientInfo *info,
194 VLANState *vlan,
195 VLANClientState *peer,
196 const char *model,
197 const char *name)
199 VLANClientState *vc;
201 assert(info->size >= sizeof(VLANClientState));
203 vc = g_malloc0(info->size);
205 vc->info = info;
206 vc->model = g_strdup(model);
207 if (name) {
208 vc->name = g_strdup(name);
209 } else {
210 vc->name = assign_name(vc, model);
213 if (vlan) {
214 assert(!peer);
215 vc->vlan = vlan;
216 QTAILQ_INSERT_TAIL(&vc->vlan->clients, vc, next);
217 } else {
218 if (peer) {
219 assert(!peer->peer);
220 vc->peer = peer;
221 peer->peer = vc;
223 QTAILQ_INSERT_TAIL(&non_vlan_clients, vc, next);
225 vc->send_queue = qemu_new_net_queue(qemu_deliver_packet,
226 qemu_deliver_packet_iov,
227 vc);
230 return vc;
233 NICState *qemu_new_nic(NetClientInfo *info,
234 NICConf *conf,
235 const char *model,
236 const char *name,
237 void *opaque)
239 VLANClientState *nc;
240 NICState *nic;
242 assert(info->type == NET_CLIENT_TYPE_NIC);
243 assert(info->size >= sizeof(NICState));
245 nc = qemu_new_net_client(info, conf->vlan, conf->peer, model, name);
247 nic = DO_UPCAST(NICState, nc, nc);
248 nic->conf = conf;
249 nic->opaque = opaque;
251 return nic;
254 static void qemu_cleanup_vlan_client(VLANClientState *vc)
256 if (vc->vlan) {
257 QTAILQ_REMOVE(&vc->vlan->clients, vc, next);
258 } else {
259 QTAILQ_REMOVE(&non_vlan_clients, vc, next);
262 if (vc->info->cleanup) {
263 vc->info->cleanup(vc);
267 static void qemu_free_vlan_client(VLANClientState *vc)
269 if (!vc->vlan) {
270 if (vc->send_queue) {
271 qemu_del_net_queue(vc->send_queue);
273 if (vc->peer) {
274 vc->peer->peer = NULL;
277 g_free(vc->name);
278 g_free(vc->model);
279 g_free(vc);
282 void qemu_del_vlan_client(VLANClientState *vc)
284 /* If there is a peer NIC, delete and cleanup client, but do not free. */
285 if (!vc->vlan && vc->peer && vc->peer->info->type == NET_CLIENT_TYPE_NIC) {
286 NICState *nic = DO_UPCAST(NICState, nc, vc->peer);
287 if (nic->peer_deleted) {
288 return;
290 nic->peer_deleted = true;
291 /* Let NIC know peer is gone. */
292 vc->peer->link_down = true;
293 if (vc->peer->info->link_status_changed) {
294 vc->peer->info->link_status_changed(vc->peer);
296 qemu_cleanup_vlan_client(vc);
297 return;
300 /* If this is a peer NIC and peer has already been deleted, free it now. */
301 if (!vc->vlan && vc->peer && vc->info->type == NET_CLIENT_TYPE_NIC) {
302 NICState *nic = DO_UPCAST(NICState, nc, vc);
303 if (nic->peer_deleted) {
304 qemu_free_vlan_client(vc->peer);
308 qemu_cleanup_vlan_client(vc);
309 qemu_free_vlan_client(vc);
312 VLANClientState *
313 qemu_find_vlan_client_by_name(Monitor *mon, int vlan_id,
314 const char *client_str)
316 VLANState *vlan;
317 VLANClientState *vc;
319 vlan = qemu_find_vlan(vlan_id, 0);
320 if (!vlan) {
321 monitor_printf(mon, "unknown VLAN %d\n", vlan_id);
322 return NULL;
325 QTAILQ_FOREACH(vc, &vlan->clients, next) {
326 if (!strcmp(vc->name, client_str)) {
327 break;
330 if (!vc) {
331 monitor_printf(mon, "can't find device %s on VLAN %d\n",
332 client_str, vlan_id);
335 return vc;
338 void qemu_foreach_nic(qemu_nic_foreach func, void *opaque)
340 VLANClientState *nc;
341 VLANState *vlan;
343 QTAILQ_FOREACH(nc, &non_vlan_clients, next) {
344 if (nc->info->type == NET_CLIENT_TYPE_NIC) {
345 func(DO_UPCAST(NICState, nc, nc), opaque);
349 QTAILQ_FOREACH(vlan, &vlans, next) {
350 QTAILQ_FOREACH(nc, &vlan->clients, next) {
351 if (nc->info->type == NET_CLIENT_TYPE_NIC) {
352 func(DO_UPCAST(NICState, nc, nc), opaque);
358 int qemu_can_send_packet(VLANClientState *sender)
360 VLANState *vlan = sender->vlan;
361 VLANClientState *vc;
363 if (sender->peer) {
364 if (sender->peer->receive_disabled) {
365 return 0;
366 } else if (sender->peer->info->can_receive &&
367 !sender->peer->info->can_receive(sender->peer)) {
368 return 0;
369 } else {
370 return 1;
374 if (!sender->vlan) {
375 return 1;
378 QTAILQ_FOREACH(vc, &vlan->clients, next) {
379 if (vc == sender) {
380 continue;
383 /* no can_receive() handler, they can always receive */
384 if (vc->info->can_receive && !vc->info->can_receive(vc)) {
385 return 0;
388 return 1;
391 static ssize_t qemu_deliver_packet(VLANClientState *sender,
392 unsigned flags,
393 const uint8_t *data,
394 size_t size,
395 void *opaque)
397 VLANClientState *vc = opaque;
398 ssize_t ret;
400 if (vc->link_down) {
401 return size;
404 if (vc->receive_disabled) {
405 return 0;
408 if (flags & QEMU_NET_PACKET_FLAG_RAW && vc->info->receive_raw) {
409 ret = vc->info->receive_raw(vc, data, size);
410 } else {
411 ret = vc->info->receive(vc, data, size);
414 if (ret == 0) {
415 vc->receive_disabled = 1;
418 return ret;
421 static ssize_t qemu_vlan_deliver_packet(VLANClientState *sender,
422 unsigned flags,
423 const uint8_t *buf,
424 size_t size,
425 void *opaque)
427 VLANState *vlan = opaque;
428 VLANClientState *vc;
429 ssize_t ret = -1;
431 QTAILQ_FOREACH(vc, &vlan->clients, next) {
432 ssize_t len;
434 if (vc == sender) {
435 continue;
438 if (vc->link_down) {
439 ret = size;
440 continue;
443 if (vc->receive_disabled) {
444 ret = 0;
445 continue;
448 if (flags & QEMU_NET_PACKET_FLAG_RAW && vc->info->receive_raw) {
449 len = vc->info->receive_raw(vc, buf, size);
450 } else {
451 len = vc->info->receive(vc, buf, size);
454 if (len == 0) {
455 vc->receive_disabled = 1;
458 ret = (ret >= 0) ? ret : len;
462 return ret;
465 void qemu_purge_queued_packets(VLANClientState *vc)
467 NetQueue *queue;
469 if (!vc->peer && !vc->vlan) {
470 return;
473 if (vc->peer) {
474 queue = vc->peer->send_queue;
475 } else {
476 queue = vc->vlan->send_queue;
479 qemu_net_queue_purge(queue, vc);
482 void qemu_flush_queued_packets(VLANClientState *vc)
484 NetQueue *queue;
486 vc->receive_disabled = 0;
488 if (vc->vlan) {
489 queue = vc->vlan->send_queue;
490 } else {
491 queue = vc->send_queue;
494 qemu_net_queue_flush(queue);
497 static ssize_t qemu_send_packet_async_with_flags(VLANClientState *sender,
498 unsigned flags,
499 const uint8_t *buf, int size,
500 NetPacketSent *sent_cb)
502 NetQueue *queue;
504 #ifdef DEBUG_NET
505 printf("qemu_send_packet_async:\n");
506 hex_dump(stdout, buf, size);
507 #endif
509 if (sender->link_down || (!sender->peer && !sender->vlan)) {
510 return size;
513 if (sender->peer) {
514 queue = sender->peer->send_queue;
515 } else {
516 queue = sender->vlan->send_queue;
519 return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb);
522 ssize_t qemu_send_packet_async(VLANClientState *sender,
523 const uint8_t *buf, int size,
524 NetPacketSent *sent_cb)
526 return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE,
527 buf, size, sent_cb);
530 void qemu_send_packet(VLANClientState *vc, const uint8_t *buf, int size)
532 qemu_send_packet_async(vc, buf, size, NULL);
535 ssize_t qemu_send_packet_raw(VLANClientState *vc, const uint8_t *buf, int size)
537 return qemu_send_packet_async_with_flags(vc, QEMU_NET_PACKET_FLAG_RAW,
538 buf, size, NULL);
541 static ssize_t vc_sendv_compat(VLANClientState *vc, const struct iovec *iov,
542 int iovcnt)
544 uint8_t buffer[4096];
545 size_t offset;
547 offset = iov_to_buf(iov, iovcnt, buffer, 0, sizeof(buffer));
549 return vc->info->receive(vc, buffer, offset);
552 static ssize_t qemu_deliver_packet_iov(VLANClientState *sender,
553 unsigned flags,
554 const struct iovec *iov,
555 int iovcnt,
556 void *opaque)
558 VLANClientState *vc = opaque;
560 if (vc->link_down) {
561 return iov_size(iov, iovcnt);
564 if (vc->info->receive_iov) {
565 return vc->info->receive_iov(vc, iov, iovcnt);
566 } else {
567 return vc_sendv_compat(vc, iov, iovcnt);
571 static ssize_t qemu_vlan_deliver_packet_iov(VLANClientState *sender,
572 unsigned flags,
573 const struct iovec *iov,
574 int iovcnt,
575 void *opaque)
577 VLANState *vlan = opaque;
578 VLANClientState *vc;
579 ssize_t ret = -1;
581 QTAILQ_FOREACH(vc, &vlan->clients, next) {
582 ssize_t len;
584 if (vc == sender) {
585 continue;
588 if (vc->link_down) {
589 ret = iov_size(iov, iovcnt);
590 continue;
593 assert(!(flags & QEMU_NET_PACKET_FLAG_RAW));
595 if (vc->info->receive_iov) {
596 len = vc->info->receive_iov(vc, iov, iovcnt);
597 } else {
598 len = vc_sendv_compat(vc, iov, iovcnt);
601 ret = (ret >= 0) ? ret : len;
604 return ret;
607 ssize_t qemu_sendv_packet_async(VLANClientState *sender,
608 const struct iovec *iov, int iovcnt,
609 NetPacketSent *sent_cb)
611 NetQueue *queue;
613 if (sender->link_down || (!sender->peer && !sender->vlan)) {
614 return iov_size(iov, iovcnt);
617 if (sender->peer) {
618 queue = sender->peer->send_queue;
619 } else {
620 queue = sender->vlan->send_queue;
623 return qemu_net_queue_send_iov(queue, sender,
624 QEMU_NET_PACKET_FLAG_NONE,
625 iov, iovcnt, sent_cb);
628 ssize_t
629 qemu_sendv_packet(VLANClientState *vc, const struct iovec *iov, int iovcnt)
631 return qemu_sendv_packet_async(vc, iov, iovcnt, NULL);
634 /* find or alloc a new VLAN */
635 VLANState *qemu_find_vlan(int id, int allocate)
637 VLANState *vlan;
639 QTAILQ_FOREACH(vlan, &vlans, next) {
640 if (vlan->id == id) {
641 return vlan;
645 if (!allocate) {
646 return NULL;
649 vlan = g_malloc0(sizeof(VLANState));
650 vlan->id = id;
651 QTAILQ_INIT(&vlan->clients);
653 vlan->send_queue = qemu_new_net_queue(qemu_vlan_deliver_packet,
654 qemu_vlan_deliver_packet_iov,
655 vlan);
657 QTAILQ_INSERT_TAIL(&vlans, vlan, next);
659 return vlan;
662 VLANClientState *qemu_find_netdev(const char *id)
664 VLANClientState *vc;
666 QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
667 if (vc->info->type == NET_CLIENT_TYPE_NIC)
668 continue;
669 if (!strcmp(vc->name, id)) {
670 return vc;
674 return NULL;
677 static int nic_get_free_idx(void)
679 int index;
681 for (index = 0; index < MAX_NICS; index++)
682 if (!nd_table[index].used)
683 return index;
684 return -1;
687 int qemu_show_nic_models(const char *arg, const char *const *models)
689 int i;
691 if (!arg || strcmp(arg, "?"))
692 return 0;
694 fprintf(stderr, "qemu: Supported NIC models: ");
695 for (i = 0 ; models[i]; i++)
696 fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n');
697 return 1;
700 void qemu_check_nic_model(NICInfo *nd, const char *model)
702 const char *models[2];
704 models[0] = model;
705 models[1] = NULL;
707 if (qemu_show_nic_models(nd->model, models))
708 exit(0);
709 if (qemu_find_nic_model(nd, models, model) < 0)
710 exit(1);
713 int qemu_find_nic_model(NICInfo *nd, const char * const *models,
714 const char *default_model)
716 int i;
718 if (!nd->model)
719 nd->model = g_strdup(default_model);
721 for (i = 0 ; models[i]; i++) {
722 if (strcmp(nd->model, models[i]) == 0)
723 return i;
726 error_report("Unsupported NIC model: %s", nd->model);
727 return -1;
730 int net_handle_fd_param(Monitor *mon, const char *param)
732 int fd;
734 if (!qemu_isdigit(param[0]) && mon) {
736 fd = monitor_get_fd(mon, param);
737 if (fd == -1) {
738 error_report("No file descriptor named %s found", param);
739 return -1;
741 } else {
742 fd = qemu_parse_fd(param);
745 return fd;
748 static int net_init_nic(QemuOpts *opts,
749 Monitor *mon,
750 const char *name,
751 VLANState *vlan)
753 int idx;
754 NICInfo *nd;
755 const char *netdev;
757 idx = nic_get_free_idx();
758 if (idx == -1 || nb_nics >= MAX_NICS) {
759 error_report("Too Many NICs");
760 return -1;
763 nd = &nd_table[idx];
765 memset(nd, 0, sizeof(*nd));
767 if ((netdev = qemu_opt_get(opts, "netdev"))) {
768 nd->netdev = qemu_find_netdev(netdev);
769 if (!nd->netdev) {
770 error_report("netdev '%s' not found", netdev);
771 return -1;
773 } else {
774 assert(vlan);
775 nd->vlan = vlan;
777 if (name) {
778 nd->name = g_strdup(name);
780 if (qemu_opt_get(opts, "model")) {
781 nd->model = g_strdup(qemu_opt_get(opts, "model"));
783 if (qemu_opt_get(opts, "addr")) {
784 nd->devaddr = g_strdup(qemu_opt_get(opts, "addr"));
787 if (qemu_opt_get(opts, "macaddr") &&
788 net_parse_macaddr(nd->macaddr.a, qemu_opt_get(opts, "macaddr")) < 0) {
789 error_report("invalid syntax for ethernet address");
790 return -1;
792 qemu_macaddr_default_if_unset(&nd->macaddr);
794 nd->nvectors = qemu_opt_get_number(opts, "vectors",
795 DEV_NVECTORS_UNSPECIFIED);
796 if (nd->nvectors != DEV_NVECTORS_UNSPECIFIED &&
797 (nd->nvectors < 0 || nd->nvectors > 0x7ffffff)) {
798 error_report("invalid # of vectors: %d", nd->nvectors);
799 return -1;
802 nd->used = 1;
803 nb_nics++;
805 return idx;
808 #define NET_COMMON_PARAMS_DESC \
810 .name = "type", \
811 .type = QEMU_OPT_STRING, \
812 .help = "net client type (nic, tap etc.)", \
813 }, { \
814 .name = "vlan", \
815 .type = QEMU_OPT_NUMBER, \
816 .help = "vlan number", \
817 }, { \
818 .name = "name", \
819 .type = QEMU_OPT_STRING, \
820 .help = "identifier for monitor commands", \
823 typedef int (*net_client_init_func)(QemuOpts *opts,
824 Monitor *mon,
825 const char *name,
826 VLANState *vlan);
828 /* magic number, but compiler will warn if too small */
829 #define NET_MAX_DESC 20
831 static const struct {
832 const char *type;
833 net_client_init_func init;
834 QemuOptDesc desc[NET_MAX_DESC];
835 } net_client_types[NET_CLIENT_TYPE_MAX] = {
836 [NET_CLIENT_TYPE_NONE] = {
837 .type = "none",
838 .desc = {
839 NET_COMMON_PARAMS_DESC,
840 { /* end of list */ }
843 [NET_CLIENT_TYPE_NIC] = {
844 .type = "nic",
845 .init = net_init_nic,
846 .desc = {
847 NET_COMMON_PARAMS_DESC,
849 .name = "netdev",
850 .type = QEMU_OPT_STRING,
851 .help = "id of -netdev to connect to",
854 .name = "macaddr",
855 .type = QEMU_OPT_STRING,
856 .help = "MAC address",
857 }, {
858 .name = "model",
859 .type = QEMU_OPT_STRING,
860 .help = "device model (e1000, rtl8139, virtio etc.)",
861 }, {
862 .name = "addr",
863 .type = QEMU_OPT_STRING,
864 .help = "PCI device address",
865 }, {
866 .name = "vectors",
867 .type = QEMU_OPT_NUMBER,
868 .help = "number of MSI-x vectors, 0 to disable MSI-X",
870 { /* end of list */ }
873 #ifdef CONFIG_SLIRP
874 [NET_CLIENT_TYPE_USER] = {
875 .type = "user",
876 .init = net_init_slirp,
877 .desc = {
878 NET_COMMON_PARAMS_DESC,
880 .name = "hostname",
881 .type = QEMU_OPT_STRING,
882 .help = "client hostname reported by the builtin DHCP server",
883 }, {
884 .name = "restrict",
885 .type = QEMU_OPT_STRING,
886 .help = "isolate the guest from the host (y|yes|n|no)",
887 }, {
888 .name = "ip",
889 .type = QEMU_OPT_STRING,
890 .help = "legacy parameter, use net= instead",
891 }, {
892 .name = "net",
893 .type = QEMU_OPT_STRING,
894 .help = "IP address and optional netmask",
895 }, {
896 .name = "host",
897 .type = QEMU_OPT_STRING,
898 .help = "guest-visible address of the host",
899 }, {
900 .name = "tftp",
901 .type = QEMU_OPT_STRING,
902 .help = "root directory of the built-in TFTP server",
903 }, {
904 .name = "bootfile",
905 .type = QEMU_OPT_STRING,
906 .help = "BOOTP filename, for use with tftp=",
907 }, {
908 .name = "dhcpstart",
909 .type = QEMU_OPT_STRING,
910 .help = "the first of the 16 IPs the built-in DHCP server can assign",
911 }, {
912 .name = "dns",
913 .type = QEMU_OPT_STRING,
914 .help = "guest-visible address of the virtual nameserver",
915 }, {
916 .name = "smb",
917 .type = QEMU_OPT_STRING,
918 .help = "root directory of the built-in SMB server",
919 }, {
920 .name = "smbserver",
921 .type = QEMU_OPT_STRING,
922 .help = "IP address of the built-in SMB server",
923 }, {
924 .name = "hostfwd",
925 .type = QEMU_OPT_STRING,
926 .help = "guest port number to forward incoming TCP or UDP connections",
927 }, {
928 .name = "guestfwd",
929 .type = QEMU_OPT_STRING,
930 .help = "IP address and port to forward guest TCP connections",
932 { /* end of list */ }
935 #endif
936 [NET_CLIENT_TYPE_TAP] = {
937 .type = "tap",
938 .init = net_init_tap,
939 .desc = {
940 NET_COMMON_PARAMS_DESC,
942 .name = "ifname",
943 .type = QEMU_OPT_STRING,
944 .help = "interface name",
946 #ifndef _WIN32
948 .name = "fd",
949 .type = QEMU_OPT_STRING,
950 .help = "file descriptor of an already opened tap",
951 }, {
952 .name = "script",
953 .type = QEMU_OPT_STRING,
954 .help = "script to initialize the interface",
955 }, {
956 .name = "downscript",
957 .type = QEMU_OPT_STRING,
958 .help = "script to shut down the interface",
959 }, {
960 #ifdef CONFIG_NET_BRIDGE
961 .name = "helper",
962 .type = QEMU_OPT_STRING,
963 .help = "command to execute to configure bridge",
964 }, {
965 #endif
966 .name = "sndbuf",
967 .type = QEMU_OPT_SIZE,
968 .help = "send buffer limit"
969 }, {
970 .name = "vnet_hdr",
971 .type = QEMU_OPT_BOOL,
972 .help = "enable the IFF_VNET_HDR flag on the tap interface"
973 }, {
974 .name = "vhost",
975 .type = QEMU_OPT_BOOL,
976 .help = "enable vhost-net network accelerator",
977 }, {
978 .name = "vhostfd",
979 .type = QEMU_OPT_STRING,
980 .help = "file descriptor of an already opened vhost net device",
981 }, {
982 .name = "vhostforce",
983 .type = QEMU_OPT_BOOL,
984 .help = "force vhost on for non-MSIX virtio guests",
986 #endif /* _WIN32 */
987 { /* end of list */ }
990 [NET_CLIENT_TYPE_SOCKET] = {
991 .type = "socket",
992 .init = net_init_socket,
993 .desc = {
994 NET_COMMON_PARAMS_DESC,
996 .name = "fd",
997 .type = QEMU_OPT_STRING,
998 .help = "file descriptor of an already opened socket",
999 }, {
1000 .name = "listen",
1001 .type = QEMU_OPT_STRING,
1002 .help = "port number, and optional hostname, to listen on",
1003 }, {
1004 .name = "connect",
1005 .type = QEMU_OPT_STRING,
1006 .help = "port number, and optional hostname, to connect to",
1007 }, {
1008 .name = "mcast",
1009 .type = QEMU_OPT_STRING,
1010 .help = "UDP multicast address and port number",
1011 }, {
1012 .name = "localaddr",
1013 .type = QEMU_OPT_STRING,
1014 .help = "source address and port for multicast and udp packets",
1015 }, {
1016 .name = "udp",
1017 .type = QEMU_OPT_STRING,
1018 .help = "UDP unicast address and port number",
1020 { /* end of list */ }
1023 #ifdef CONFIG_VDE
1024 [NET_CLIENT_TYPE_VDE] = {
1025 .type = "vde",
1026 .init = net_init_vde,
1027 .desc = {
1028 NET_COMMON_PARAMS_DESC,
1030 .name = "sock",
1031 .type = QEMU_OPT_STRING,
1032 .help = "socket path",
1033 }, {
1034 .name = "port",
1035 .type = QEMU_OPT_NUMBER,
1036 .help = "port number",
1037 }, {
1038 .name = "group",
1039 .type = QEMU_OPT_STRING,
1040 .help = "group owner of socket",
1041 }, {
1042 .name = "mode",
1043 .type = QEMU_OPT_NUMBER,
1044 .help = "permissions for socket",
1046 { /* end of list */ }
1049 #endif
1050 [NET_CLIENT_TYPE_DUMP] = {
1051 .type = "dump",
1052 .init = net_init_dump,
1053 .desc = {
1054 NET_COMMON_PARAMS_DESC,
1056 .name = "len",
1057 .type = QEMU_OPT_SIZE,
1058 .help = "per-packet size limit (64k default)",
1059 }, {
1060 .name = "file",
1061 .type = QEMU_OPT_STRING,
1062 .help = "dump file path (default is qemu-vlan0.pcap)",
1064 { /* end of list */ }
1067 #ifdef CONFIG_NET_BRIDGE
1068 [NET_CLIENT_TYPE_BRIDGE] = {
1069 .type = "bridge",
1070 .init = net_init_bridge,
1071 .desc = {
1072 NET_COMMON_PARAMS_DESC,
1074 .name = "br",
1075 .type = QEMU_OPT_STRING,
1076 .help = "bridge name",
1077 }, {
1078 .name = "helper",
1079 .type = QEMU_OPT_STRING,
1080 .help = "command to execute to configure bridge",
1082 { /* end of list */ }
1085 #endif /* CONFIG_NET_BRIDGE */
1088 int net_client_init(Monitor *mon, QemuOpts *opts, int is_netdev)
1090 const char *name;
1091 const char *type;
1092 int i;
1094 type = qemu_opt_get(opts, "type");
1095 if (!type) {
1096 qerror_report(QERR_MISSING_PARAMETER, "type");
1097 return -1;
1100 if (is_netdev) {
1101 if (strcmp(type, "tap") != 0 &&
1102 #ifdef CONFIG_NET_BRIDGE
1103 strcmp(type, "bridge") != 0 &&
1104 #endif
1105 #ifdef CONFIG_SLIRP
1106 strcmp(type, "user") != 0 &&
1107 #endif
1108 #ifdef CONFIG_VDE
1109 strcmp(type, "vde") != 0 &&
1110 #endif
1111 strcmp(type, "socket") != 0) {
1112 qerror_report(QERR_INVALID_PARAMETER_VALUE, "type",
1113 "a netdev backend type");
1114 return -1;
1117 if (qemu_opt_get(opts, "vlan")) {
1118 qerror_report(QERR_INVALID_PARAMETER, "vlan");
1119 return -1;
1121 if (qemu_opt_get(opts, "name")) {
1122 qerror_report(QERR_INVALID_PARAMETER, "name");
1123 return -1;
1125 if (!qemu_opts_id(opts)) {
1126 qerror_report(QERR_MISSING_PARAMETER, "id");
1127 return -1;
1131 name = qemu_opts_id(opts);
1132 if (!name) {
1133 name = qemu_opt_get(opts, "name");
1136 for (i = 0; i < NET_CLIENT_TYPE_MAX; i++) {
1137 if (net_client_types[i].type != NULL &&
1138 !strcmp(net_client_types[i].type, type)) {
1139 VLANState *vlan = NULL;
1140 int ret;
1142 if (qemu_opts_validate(opts, &net_client_types[i].desc[0]) == -1) {
1143 return -1;
1146 /* Do not add to a vlan if it's a -netdev or a nic with a
1147 * netdev= parameter. */
1148 if (!(is_netdev ||
1149 (strcmp(type, "nic") == 0 && qemu_opt_get(opts, "netdev")))) {
1150 vlan = qemu_find_vlan(qemu_opt_get_number(opts, "vlan", 0), 1);
1153 ret = 0;
1154 if (net_client_types[i].init) {
1155 ret = net_client_types[i].init(opts, mon, name, vlan);
1156 if (ret < 0) {
1157 /* TODO push error reporting into init() methods */
1158 qerror_report(QERR_DEVICE_INIT_FAILED, type);
1159 return -1;
1162 return ret;
1166 qerror_report(QERR_INVALID_PARAMETER_VALUE, "type",
1167 "a network client type");
1168 return -1;
1171 static int net_host_check_device(const char *device)
1173 int i;
1174 const char *valid_param_list[] = { "tap", "socket", "dump"
1175 #ifdef CONFIG_NET_BRIDGE
1176 , "bridge"
1177 #endif
1178 #ifdef CONFIG_SLIRP
1179 ,"user"
1180 #endif
1181 #ifdef CONFIG_VDE
1182 ,"vde"
1183 #endif
1185 for (i = 0; i < sizeof(valid_param_list) / sizeof(char *); i++) {
1186 if (!strncmp(valid_param_list[i], device,
1187 strlen(valid_param_list[i])))
1188 return 1;
1191 return 0;
1194 void net_host_device_add(Monitor *mon, const QDict *qdict)
1196 const char *device = qdict_get_str(qdict, "device");
1197 const char *opts_str = qdict_get_try_str(qdict, "opts");
1198 QemuOpts *opts;
1200 if (!net_host_check_device(device)) {
1201 monitor_printf(mon, "invalid host network device %s\n", device);
1202 return;
1205 opts = qemu_opts_parse(qemu_find_opts("net"), opts_str ? opts_str : "", 0);
1206 if (!opts) {
1207 return;
1210 qemu_opt_set(opts, "type", device);
1212 if (net_client_init(mon, opts, 0) < 0) {
1213 monitor_printf(mon, "adding host network device %s failed\n", device);
1217 void net_host_device_remove(Monitor *mon, const QDict *qdict)
1219 VLANClientState *vc;
1220 int vlan_id = qdict_get_int(qdict, "vlan_id");
1221 const char *device = qdict_get_str(qdict, "device");
1223 vc = qemu_find_vlan_client_by_name(mon, vlan_id, device);
1224 if (!vc) {
1225 return;
1227 if (!net_host_check_device(vc->model)) {
1228 monitor_printf(mon, "invalid host network device %s\n", device);
1229 return;
1231 qemu_del_vlan_client(vc);
1234 int do_netdev_add(Monitor *mon, const QDict *qdict, QObject **ret_data)
1236 QemuOpts *opts;
1237 int res;
1239 opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict);
1240 if (!opts) {
1241 return -1;
1244 res = net_client_init(mon, opts, 1);
1245 if (res < 0) {
1246 qemu_opts_del(opts);
1249 return res;
1252 int do_netdev_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
1254 const char *id = qdict_get_str(qdict, "id");
1255 VLANClientState *vc;
1257 vc = qemu_find_netdev(id);
1258 if (!vc) {
1259 qerror_report(QERR_DEVICE_NOT_FOUND, id);
1260 return -1;
1262 qemu_del_vlan_client(vc);
1263 qemu_opts_del(qemu_opts_find(qemu_find_opts("netdev"), id));
1264 return 0;
1267 static void print_net_client(Monitor *mon, VLANClientState *vc)
1269 monitor_printf(mon, "%s: type=%s,%s\n", vc->name,
1270 net_client_types[vc->info->type].type, vc->info_str);
1273 void do_info_network(Monitor *mon)
1275 VLANState *vlan;
1276 VLANClientState *vc, *peer;
1277 net_client_type type;
1279 QTAILQ_FOREACH(vlan, &vlans, next) {
1280 monitor_printf(mon, "VLAN %d devices:\n", vlan->id);
1282 QTAILQ_FOREACH(vc, &vlan->clients, next) {
1283 monitor_printf(mon, " ");
1284 print_net_client(mon, vc);
1287 monitor_printf(mon, "Devices not on any VLAN:\n");
1288 QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
1289 peer = vc->peer;
1290 type = vc->info->type;
1291 if (!peer || type == NET_CLIENT_TYPE_NIC) {
1292 monitor_printf(mon, " ");
1293 print_net_client(mon, vc);
1294 } /* else it's a netdev connected to a NIC, printed with the NIC */
1295 if (peer && type == NET_CLIENT_TYPE_NIC) {
1296 monitor_printf(mon, " \\ ");
1297 print_net_client(mon, peer);
1302 void qmp_set_link(const char *name, bool up, Error **errp)
1304 VLANState *vlan;
1305 VLANClientState *vc = NULL;
1307 QTAILQ_FOREACH(vlan, &vlans, next) {
1308 QTAILQ_FOREACH(vc, &vlan->clients, next) {
1309 if (strcmp(vc->name, name) == 0) {
1310 goto done;
1314 QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
1315 if (!strcmp(vc->name, name)) {
1316 goto done;
1319 done:
1321 if (!vc) {
1322 error_set(errp, QERR_DEVICE_NOT_FOUND, name);
1323 return;
1326 vc->link_down = !up;
1328 if (vc->info->link_status_changed) {
1329 vc->info->link_status_changed(vc);
1332 /* Notify peer. Don't update peer link status: this makes it possible to
1333 * disconnect from host network without notifying the guest.
1334 * FIXME: is disconnected link status change operation useful?
1336 * Current behaviour is compatible with qemu vlans where there could be
1337 * multiple clients that can still communicate with each other in
1338 * disconnected mode. For now maintain this compatibility. */
1339 if (vc->peer && vc->peer->info->link_status_changed) {
1340 vc->peer->info->link_status_changed(vc->peer);
1344 void net_cleanup(void)
1346 VLANState *vlan;
1347 VLANClientState *vc, *next_vc;
1349 QTAILQ_FOREACH(vlan, &vlans, next) {
1350 QTAILQ_FOREACH_SAFE(vc, &vlan->clients, next, next_vc) {
1351 qemu_del_vlan_client(vc);
1355 QTAILQ_FOREACH_SAFE(vc, &non_vlan_clients, next, next_vc) {
1356 qemu_del_vlan_client(vc);
1360 void net_check_clients(void)
1362 VLANState *vlan;
1363 VLANClientState *vc;
1364 int i;
1366 /* Don't warn about the default network setup that you get if
1367 * no command line -net or -netdev options are specified. There
1368 * are two cases that we would otherwise complain about:
1369 * (1) board doesn't support a NIC but the implicit "-net nic"
1370 * requested one
1371 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
1372 * sets up a nic that isn't connected to anything.
1374 if (default_net) {
1375 return;
1378 QTAILQ_FOREACH(vlan, &vlans, next) {
1379 int has_nic = 0, has_host_dev = 0;
1381 QTAILQ_FOREACH(vc, &vlan->clients, next) {
1382 switch (vc->info->type) {
1383 case NET_CLIENT_TYPE_NIC:
1384 has_nic = 1;
1385 break;
1386 case NET_CLIENT_TYPE_USER:
1387 case NET_CLIENT_TYPE_TAP:
1388 case NET_CLIENT_TYPE_SOCKET:
1389 case NET_CLIENT_TYPE_VDE:
1390 has_host_dev = 1;
1391 break;
1392 default: ;
1395 if (has_host_dev && !has_nic)
1396 fprintf(stderr, "Warning: vlan %d with no nics\n", vlan->id);
1397 if (has_nic && !has_host_dev)
1398 fprintf(stderr,
1399 "Warning: vlan %d is not connected to host network\n",
1400 vlan->id);
1402 QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
1403 if (!vc->peer) {
1404 fprintf(stderr, "Warning: %s %s has no peer\n",
1405 vc->info->type == NET_CLIENT_TYPE_NIC ? "nic" : "netdev",
1406 vc->name);
1410 /* Check that all NICs requested via -net nic actually got created.
1411 * NICs created via -device don't need to be checked here because
1412 * they are always instantiated.
1414 for (i = 0; i < MAX_NICS; i++) {
1415 NICInfo *nd = &nd_table[i];
1416 if (nd->used && !nd->instantiated) {
1417 fprintf(stderr, "Warning: requested NIC (%s, model %s) "
1418 "was not created (not supported by this machine?)\n",
1419 nd->name ? nd->name : "anonymous",
1420 nd->model ? nd->model : "unspecified");
1425 static int net_init_client(QemuOpts *opts, void *dummy)
1427 if (net_client_init(NULL, opts, 0) < 0)
1428 return -1;
1429 return 0;
1432 static int net_init_netdev(QemuOpts *opts, void *dummy)
1434 return net_client_init(NULL, opts, 1);
1437 int net_init_clients(void)
1439 QemuOptsList *net = qemu_find_opts("net");
1441 if (default_net) {
1442 /* if no clients, we use a default config */
1443 qemu_opts_set(net, NULL, "type", "nic");
1444 #ifdef CONFIG_SLIRP
1445 qemu_opts_set(net, NULL, "type", "user");
1446 #endif
1449 QTAILQ_INIT(&vlans);
1450 QTAILQ_INIT(&non_vlan_clients);
1452 if (qemu_opts_foreach(qemu_find_opts("netdev"), net_init_netdev, NULL, 1) == -1)
1453 return -1;
1455 if (qemu_opts_foreach(net, net_init_client, NULL, 1) == -1) {
1456 return -1;
1459 return 0;
1462 int net_client_parse(QemuOptsList *opts_list, const char *optarg)
1464 #if defined(CONFIG_SLIRP)
1465 int ret;
1466 if (net_slirp_parse_legacy(opts_list, optarg, &ret)) {
1467 return ret;
1469 #endif
1471 if (!qemu_opts_parse(opts_list, optarg, 1)) {
1472 return -1;
1475 default_net = 0;
1476 return 0;