net: remove NICInfo::private
[qemu.git] / net.c
blob599e5b05144cdeb3befbcd5c48049d5c1c5d3755
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 "monitor.h"
34 #include "sysemu.h"
35 #include "qemu-common.h"
36 #include "qemu_socket.h"
38 static QTAILQ_HEAD(, VLANState) vlans;
39 static QTAILQ_HEAD(, VLANClientState) non_vlan_clients;
41 /***********************************************************/
42 /* network device redirectors */
44 #if defined(DEBUG_NET)
45 static void hex_dump(FILE *f, const uint8_t *buf, int size)
47 int len, i, j, c;
49 for(i=0;i<size;i+=16) {
50 len = size - i;
51 if (len > 16)
52 len = 16;
53 fprintf(f, "%08x ", i);
54 for(j=0;j<16;j++) {
55 if (j < len)
56 fprintf(f, " %02x", buf[i+j]);
57 else
58 fprintf(f, " ");
60 fprintf(f, " ");
61 for(j=0;j<len;j++) {
62 c = buf[i+j];
63 if (c < ' ' || c > '~')
64 c = '.';
65 fprintf(f, "%c", c);
67 fprintf(f, "\n");
70 #endif
72 static int parse_macaddr(uint8_t *macaddr, const char *p)
74 int i;
75 char *last_char;
76 long int offset;
78 errno = 0;
79 offset = strtol(p, &last_char, 0);
80 if (0 == errno && '\0' == *last_char &&
81 offset >= 0 && offset <= 0xFFFFFF) {
82 macaddr[3] = (offset & 0xFF0000) >> 16;
83 macaddr[4] = (offset & 0xFF00) >> 8;
84 macaddr[5] = offset & 0xFF;
85 return 0;
86 } else {
87 for(i = 0; i < 6; i++) {
88 macaddr[i] = strtol(p, (char **)&p, 16);
89 if (i == 5) {
90 if (*p != '\0')
91 return -1;
92 } else {
93 if (*p != ':' && *p != '-')
94 return -1;
95 p++;
98 return 0;
101 return -1;
104 static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
106 const char *p, *p1;
107 int len;
108 p = *pp;
109 p1 = strchr(p, sep);
110 if (!p1)
111 return -1;
112 len = p1 - p;
113 p1++;
114 if (buf_size > 0) {
115 if (len > buf_size - 1)
116 len = buf_size - 1;
117 memcpy(buf, p, len);
118 buf[len] = '\0';
120 *pp = p1;
121 return 0;
124 int parse_host_src_port(struct sockaddr_in *haddr,
125 struct sockaddr_in *saddr,
126 const char *input_str)
128 char *str = strdup(input_str);
129 char *host_str = str;
130 char *src_str;
131 const char *src_str2;
132 char *ptr;
135 * Chop off any extra arguments at the end of the string which
136 * would start with a comma, then fill in the src port information
137 * if it was provided else use the "any address" and "any port".
139 if ((ptr = strchr(str,',')))
140 *ptr = '\0';
142 if ((src_str = strchr(input_str,'@'))) {
143 *src_str = '\0';
144 src_str++;
147 if (parse_host_port(haddr, host_str) < 0)
148 goto fail;
150 src_str2 = src_str;
151 if (!src_str || *src_str == '\0')
152 src_str2 = ":0";
154 if (parse_host_port(saddr, src_str2) < 0)
155 goto fail;
157 free(str);
158 return(0);
160 fail:
161 free(str);
162 return -1;
165 int parse_host_port(struct sockaddr_in *saddr, const char *str)
167 char buf[512];
168 struct hostent *he;
169 const char *p, *r;
170 int port;
172 p = str;
173 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
174 return -1;
175 saddr->sin_family = AF_INET;
176 if (buf[0] == '\0') {
177 saddr->sin_addr.s_addr = 0;
178 } else {
179 if (qemu_isdigit(buf[0])) {
180 if (!inet_aton(buf, &saddr->sin_addr))
181 return -1;
182 } else {
183 if ((he = gethostbyname(buf)) == NULL)
184 return - 1;
185 saddr->sin_addr = *(struct in_addr *)he->h_addr;
188 port = strtol(p, (char **)&r, 0);
189 if (r == p)
190 return -1;
191 saddr->sin_port = htons(port);
192 return 0;
195 void qemu_format_nic_info_str(VLANClientState *vc, uint8_t macaddr[6])
197 snprintf(vc->info_str, sizeof(vc->info_str),
198 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
199 vc->model,
200 macaddr[0], macaddr[1], macaddr[2],
201 macaddr[3], macaddr[4], macaddr[5]);
204 void qemu_macaddr_default_if_unset(MACAddr *macaddr)
206 static int index = 0;
207 static const MACAddr zero = { .a = { 0,0,0,0,0,0 } };
209 if (memcmp(macaddr, &zero, sizeof(zero)) != 0)
210 return;
211 macaddr->a[0] = 0x52;
212 macaddr->a[1] = 0x54;
213 macaddr->a[2] = 0x00;
214 macaddr->a[3] = 0x12;
215 macaddr->a[4] = 0x34;
216 macaddr->a[5] = 0x56 + index++;
219 static char *assign_name(VLANClientState *vc1, const char *model)
221 VLANState *vlan;
222 char buf[256];
223 int id = 0;
225 QTAILQ_FOREACH(vlan, &vlans, next) {
226 VLANClientState *vc;
228 QTAILQ_FOREACH(vc, &vlan->clients, next) {
229 if (vc != vc1 && strcmp(vc->model, model) == 0) {
230 id++;
235 snprintf(buf, sizeof(buf), "%s.%d", model, id);
237 return qemu_strdup(buf);
240 static ssize_t qemu_deliver_packet(VLANClientState *sender,
241 unsigned flags,
242 const uint8_t *data,
243 size_t size,
244 void *opaque);
245 static ssize_t qemu_deliver_packet_iov(VLANClientState *sender,
246 unsigned flags,
247 const struct iovec *iov,
248 int iovcnt,
249 void *opaque);
251 VLANClientState *qemu_new_vlan_client(net_client_type type,
252 VLANState *vlan,
253 VLANClientState *peer,
254 const char *model,
255 const char *name,
256 NetCanReceive *can_receive,
257 NetReceive *receive,
258 NetReceive *receive_raw,
259 NetReceiveIOV *receive_iov,
260 NetCleanup *cleanup,
261 void *opaque)
263 VLANClientState *vc;
265 vc = qemu_mallocz(sizeof(VLANClientState));
267 vc->type = type;
268 vc->model = qemu_strdup(model);
269 if (name)
270 vc->name = qemu_strdup(name);
271 else
272 vc->name = assign_name(vc, model);
273 vc->can_receive = can_receive;
274 vc->receive = receive;
275 vc->receive_raw = receive_raw;
276 vc->receive_iov = receive_iov;
277 vc->cleanup = cleanup;
278 vc->opaque = opaque;
280 if (vlan) {
281 assert(!peer);
282 vc->vlan = vlan;
283 QTAILQ_INSERT_TAIL(&vc->vlan->clients, vc, next);
284 } else {
285 if (peer) {
286 vc->peer = peer;
287 peer->peer = vc;
289 QTAILQ_INSERT_TAIL(&non_vlan_clients, vc, next);
291 vc->send_queue = qemu_new_net_queue(qemu_deliver_packet,
292 qemu_deliver_packet_iov,
293 vc);
296 return vc;
299 void qemu_del_vlan_client(VLANClientState *vc)
301 if (vc->vlan) {
302 QTAILQ_REMOVE(&vc->vlan->clients, vc, next);
303 } else {
304 if (vc->send_queue) {
305 qemu_del_net_queue(vc->send_queue);
307 QTAILQ_REMOVE(&non_vlan_clients, vc, next);
308 if (vc->peer) {
309 vc->peer->peer = NULL;
313 if (vc->cleanup) {
314 vc->cleanup(vc);
317 qemu_free(vc->name);
318 qemu_free(vc->model);
319 qemu_free(vc);
322 VLANClientState *qemu_find_vlan_client(VLANState *vlan, void *opaque)
324 VLANClientState *vc;
326 QTAILQ_FOREACH(vc, &vlan->clients, next) {
327 if (vc->opaque == opaque) {
328 return vc;
332 return NULL;
335 VLANClientState *
336 qemu_find_vlan_client_by_name(Monitor *mon, int vlan_id,
337 const char *client_str)
339 VLANState *vlan;
340 VLANClientState *vc;
342 vlan = qemu_find_vlan(vlan_id, 0);
343 if (!vlan) {
344 monitor_printf(mon, "unknown VLAN %d\n", vlan_id);
345 return NULL;
348 QTAILQ_FOREACH(vc, &vlan->clients, next) {
349 if (!strcmp(vc->name, client_str)) {
350 break;
353 if (!vc) {
354 monitor_printf(mon, "can't find device %s on VLAN %d\n",
355 client_str, vlan_id);
358 return vc;
361 int qemu_can_send_packet(VLANClientState *sender)
363 VLANState *vlan = sender->vlan;
364 VLANClientState *vc;
366 if (sender->peer) {
367 if (sender->peer->receive_disabled) {
368 return 0;
369 } else if (sender->peer->can_receive &&
370 !sender->peer->can_receive(sender->peer)) {
371 return 0;
372 } else {
373 return 1;
377 if (!sender->vlan) {
378 return 1;
381 QTAILQ_FOREACH(vc, &vlan->clients, next) {
382 if (vc == sender) {
383 continue;
386 /* no can_receive() handler, they can always receive */
387 if (!vc->can_receive || vc->can_receive(vc)) {
388 return 1;
391 return 0;
394 static ssize_t qemu_deliver_packet(VLANClientState *sender,
395 unsigned flags,
396 const uint8_t *data,
397 size_t size,
398 void *opaque)
400 VLANClientState *vc = opaque;
401 ssize_t ret;
403 if (vc->link_down) {
404 return size;
407 if (vc->receive_disabled) {
408 return 0;
411 if (flags & QEMU_NET_PACKET_FLAG_RAW && vc->receive_raw) {
412 ret = vc->receive_raw(vc, data, size);
413 } else {
414 ret = vc->receive(vc, data, size);
417 if (ret == 0) {
418 vc->receive_disabled = 1;
421 return ret;
424 static ssize_t qemu_vlan_deliver_packet(VLANClientState *sender,
425 unsigned flags,
426 const uint8_t *buf,
427 size_t size,
428 void *opaque)
430 VLANState *vlan = opaque;
431 VLANClientState *vc;
432 ssize_t ret = -1;
434 QTAILQ_FOREACH(vc, &vlan->clients, next) {
435 ssize_t len;
437 if (vc == sender) {
438 continue;
441 if (vc->link_down) {
442 ret = size;
443 continue;
446 if (vc->receive_disabled) {
447 ret = 0;
448 continue;
451 if (flags & QEMU_NET_PACKET_FLAG_RAW && vc->receive_raw) {
452 len = vc->receive_raw(vc, buf, size);
453 } else {
454 len = vc->receive(vc, buf, size);
457 if (len == 0) {
458 vc->receive_disabled = 1;
461 ret = (ret >= 0) ? ret : len;
465 return ret;
468 void qemu_purge_queued_packets(VLANClientState *vc)
470 NetQueue *queue;
472 if (!vc->peer && !vc->vlan) {
473 return;
476 if (vc->peer) {
477 queue = vc->peer->send_queue;
478 } else {
479 queue = vc->vlan->send_queue;
482 qemu_net_queue_purge(queue, vc);
485 void qemu_flush_queued_packets(VLANClientState *vc)
487 NetQueue *queue;
489 vc->receive_disabled = 0;
491 if (vc->vlan) {
492 queue = vc->vlan->send_queue;
493 } else {
494 queue = vc->send_queue;
497 qemu_net_queue_flush(queue);
500 static ssize_t qemu_send_packet_async_with_flags(VLANClientState *sender,
501 unsigned flags,
502 const uint8_t *buf, int size,
503 NetPacketSent *sent_cb)
505 NetQueue *queue;
507 #ifdef DEBUG_NET
508 printf("qemu_send_packet_async:\n");
509 hex_dump(stdout, buf, size);
510 #endif
512 if (sender->link_down || (!sender->peer && !sender->vlan)) {
513 return size;
516 if (sender->peer) {
517 queue = sender->peer->send_queue;
518 } else {
519 queue = sender->vlan->send_queue;
522 return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb);
525 ssize_t qemu_send_packet_async(VLANClientState *sender,
526 const uint8_t *buf, int size,
527 NetPacketSent *sent_cb)
529 return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE,
530 buf, size, sent_cb);
533 void qemu_send_packet(VLANClientState *vc, const uint8_t *buf, int size)
535 qemu_send_packet_async(vc, buf, size, NULL);
538 ssize_t qemu_send_packet_raw(VLANClientState *vc, const uint8_t *buf, int size)
540 return qemu_send_packet_async_with_flags(vc, QEMU_NET_PACKET_FLAG_RAW,
541 buf, size, NULL);
544 static ssize_t vc_sendv_compat(VLANClientState *vc, const struct iovec *iov,
545 int iovcnt)
547 uint8_t buffer[4096];
548 size_t offset = 0;
549 int i;
551 for (i = 0; i < iovcnt; i++) {
552 size_t len;
554 len = MIN(sizeof(buffer) - offset, iov[i].iov_len);
555 memcpy(buffer + offset, iov[i].iov_base, len);
556 offset += len;
559 return vc->receive(vc, buffer, offset);
562 static ssize_t calc_iov_length(const struct iovec *iov, int iovcnt)
564 size_t offset = 0;
565 int i;
567 for (i = 0; i < iovcnt; i++)
568 offset += iov[i].iov_len;
569 return offset;
572 static ssize_t qemu_deliver_packet_iov(VLANClientState *sender,
573 unsigned flags,
574 const struct iovec *iov,
575 int iovcnt,
576 void *opaque)
578 VLANClientState *vc = opaque;
580 if (vc->link_down) {
581 return calc_iov_length(iov, iovcnt);
584 if (vc->receive_iov) {
585 return vc->receive_iov(vc, iov, iovcnt);
586 } else {
587 return vc_sendv_compat(vc, iov, iovcnt);
591 static ssize_t qemu_vlan_deliver_packet_iov(VLANClientState *sender,
592 unsigned flags,
593 const struct iovec *iov,
594 int iovcnt,
595 void *opaque)
597 VLANState *vlan = opaque;
598 VLANClientState *vc;
599 ssize_t ret = -1;
601 QTAILQ_FOREACH(vc, &vlan->clients, next) {
602 ssize_t len;
604 if (vc == sender) {
605 continue;
608 if (vc->link_down) {
609 ret = calc_iov_length(iov, iovcnt);
610 continue;
613 assert(!(flags & QEMU_NET_PACKET_FLAG_RAW));
615 if (vc->receive_iov) {
616 len = vc->receive_iov(vc, iov, iovcnt);
617 } else {
618 len = vc_sendv_compat(vc, iov, iovcnt);
621 ret = (ret >= 0) ? ret : len;
624 return ret;
627 ssize_t qemu_sendv_packet_async(VLANClientState *sender,
628 const struct iovec *iov, int iovcnt,
629 NetPacketSent *sent_cb)
631 NetQueue *queue;
633 if (sender->link_down || (!sender->peer && !sender->vlan)) {
634 return calc_iov_length(iov, iovcnt);
637 if (sender->peer) {
638 queue = sender->peer->send_queue;
639 } else {
640 queue = sender->vlan->send_queue;
643 return qemu_net_queue_send_iov(queue, sender,
644 QEMU_NET_PACKET_FLAG_NONE,
645 iov, iovcnt, sent_cb);
648 ssize_t
649 qemu_sendv_packet(VLANClientState *vc, const struct iovec *iov, int iovcnt)
651 return qemu_sendv_packet_async(vc, iov, iovcnt, NULL);
654 /* find or alloc a new VLAN */
655 VLANState *qemu_find_vlan(int id, int allocate)
657 VLANState *vlan;
659 QTAILQ_FOREACH(vlan, &vlans, next) {
660 if (vlan->id == id) {
661 return vlan;
665 if (!allocate) {
666 return NULL;
669 vlan = qemu_mallocz(sizeof(VLANState));
670 vlan->id = id;
671 QTAILQ_INIT(&vlan->clients);
673 vlan->send_queue = qemu_new_net_queue(qemu_vlan_deliver_packet,
674 qemu_vlan_deliver_packet_iov,
675 vlan);
677 QTAILQ_INSERT_TAIL(&vlans, vlan, next);
679 return vlan;
682 VLANClientState *qemu_find_netdev(const char *id)
684 VLANClientState *vc;
686 QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
687 if (!strcmp(vc->name, id)) {
688 return vc;
692 return NULL;
695 static int nic_get_free_idx(void)
697 int index;
699 for (index = 0; index < MAX_NICS; index++)
700 if (!nd_table[index].used)
701 return index;
702 return -1;
705 int qemu_show_nic_models(const char *arg, const char *const *models)
707 int i;
709 if (!arg || strcmp(arg, "?"))
710 return 0;
712 fprintf(stderr, "qemu: Supported NIC models: ");
713 for (i = 0 ; models[i]; i++)
714 fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n');
715 return 1;
718 void qemu_check_nic_model(NICInfo *nd, const char *model)
720 const char *models[2];
722 models[0] = model;
723 models[1] = NULL;
725 if (qemu_show_nic_models(nd->model, models))
726 exit(0);
727 if (qemu_find_nic_model(nd, models, model) < 0)
728 exit(1);
731 int qemu_find_nic_model(NICInfo *nd, const char * const *models,
732 const char *default_model)
734 int i;
736 if (!nd->model)
737 nd->model = qemu_strdup(default_model);
739 for (i = 0 ; models[i]; i++) {
740 if (strcmp(nd->model, models[i]) == 0)
741 return i;
744 qemu_error("qemu: Unsupported NIC model: %s\n", nd->model);
745 return -1;
748 int net_handle_fd_param(Monitor *mon, const char *param)
750 if (!qemu_isdigit(param[0])) {
751 int fd;
753 fd = monitor_get_fd(mon, param);
754 if (fd == -1) {
755 qemu_error("No file descriptor named %s found", param);
756 return -1;
759 return fd;
760 } else {
761 return strtol(param, NULL, 0);
765 static int net_init_nic(QemuOpts *opts,
766 Monitor *mon,
767 const char *name,
768 VLANState *vlan)
770 int idx;
771 NICInfo *nd;
772 const char *netdev;
774 idx = nic_get_free_idx();
775 if (idx == -1 || nb_nics >= MAX_NICS) {
776 qemu_error("Too Many NICs\n");
777 return -1;
780 nd = &nd_table[idx];
782 memset(nd, 0, sizeof(*nd));
784 if ((netdev = qemu_opt_get(opts, "netdev"))) {
785 nd->netdev = qemu_find_netdev(netdev);
786 if (!nd->netdev) {
787 qemu_error("netdev '%s' not found\n", netdev);
788 return -1;
790 } else {
791 assert(vlan);
792 nd->vlan = vlan;
794 if (name) {
795 nd->name = qemu_strdup(name);
797 if (qemu_opt_get(opts, "model")) {
798 nd->model = qemu_strdup(qemu_opt_get(opts, "model"));
800 if (qemu_opt_get(opts, "addr")) {
801 nd->devaddr = qemu_strdup(qemu_opt_get(opts, "addr"));
804 nd->macaddr[0] = 0x52;
805 nd->macaddr[1] = 0x54;
806 nd->macaddr[2] = 0x00;
807 nd->macaddr[3] = 0x12;
808 nd->macaddr[4] = 0x34;
809 nd->macaddr[5] = 0x56 + idx;
811 if (qemu_opt_get(opts, "macaddr") &&
812 parse_macaddr(nd->macaddr, qemu_opt_get(opts, "macaddr")) < 0) {
813 qemu_error("invalid syntax for ethernet address\n");
814 return -1;
817 nd->nvectors = qemu_opt_get_number(opts, "vectors", NIC_NVECTORS_UNSPECIFIED);
818 if (nd->nvectors != NIC_NVECTORS_UNSPECIFIED &&
819 (nd->nvectors < 0 || nd->nvectors > 0x7ffffff)) {
820 qemu_error("invalid # of vectors: %d\n", nd->nvectors);
821 return -1;
824 nd->used = 1;
825 if (vlan) {
826 nd->vlan->nb_guest_devs++;
828 nb_nics++;
830 return idx;
833 #define NET_COMMON_PARAMS_DESC \
835 .name = "type", \
836 .type = QEMU_OPT_STRING, \
837 .help = "net client type (nic, tap etc.)", \
838 }, { \
839 .name = "vlan", \
840 .type = QEMU_OPT_NUMBER, \
841 .help = "vlan number", \
842 }, { \
843 .name = "name", \
844 .type = QEMU_OPT_STRING, \
845 .help = "identifier for monitor commands", \
848 typedef int (*net_client_init_func)(QemuOpts *opts,
849 Monitor *mon,
850 const char *name,
851 VLANState *vlan);
853 /* magic number, but compiler will warn if too small */
854 #define NET_MAX_DESC 20
856 static struct {
857 const char *type;
858 net_client_init_func init;
859 QemuOptDesc desc[NET_MAX_DESC];
860 } net_client_types[] = {
862 .type = "none",
863 .desc = {
864 NET_COMMON_PARAMS_DESC,
865 { /* end of list */ }
867 }, {
868 .type = "nic",
869 .init = net_init_nic,
870 .desc = {
871 NET_COMMON_PARAMS_DESC,
873 .name = "netdev",
874 .type = QEMU_OPT_STRING,
875 .help = "id of -netdev to connect to",
878 .name = "macaddr",
879 .type = QEMU_OPT_STRING,
880 .help = "MAC address",
881 }, {
882 .name = "model",
883 .type = QEMU_OPT_STRING,
884 .help = "device model (e1000, rtl8139, virtio etc.)",
885 }, {
886 .name = "addr",
887 .type = QEMU_OPT_STRING,
888 .help = "PCI device address",
889 }, {
890 .name = "vectors",
891 .type = QEMU_OPT_NUMBER,
892 .help = "number of MSI-x vectors, 0 to disable MSI-X",
894 { /* end of list */ }
896 #ifdef CONFIG_SLIRP
897 }, {
898 .type = "user",
899 .init = net_init_slirp,
900 .desc = {
901 NET_COMMON_PARAMS_DESC,
903 .name = "hostname",
904 .type = QEMU_OPT_STRING,
905 .help = "client hostname reported by the builtin DHCP server",
906 }, {
907 .name = "restrict",
908 .type = QEMU_OPT_STRING,
909 .help = "isolate the guest from the host (y|yes|n|no)",
910 }, {
911 .name = "ip",
912 .type = QEMU_OPT_STRING,
913 .help = "legacy parameter, use net= instead",
914 }, {
915 .name = "net",
916 .type = QEMU_OPT_STRING,
917 .help = "IP address and optional netmask",
918 }, {
919 .name = "host",
920 .type = QEMU_OPT_STRING,
921 .help = "guest-visible address of the host",
922 }, {
923 .name = "tftp",
924 .type = QEMU_OPT_STRING,
925 .help = "root directory of the built-in TFTP server",
926 }, {
927 .name = "bootfile",
928 .type = QEMU_OPT_STRING,
929 .help = "BOOTP filename, for use with tftp=",
930 }, {
931 .name = "dhcpstart",
932 .type = QEMU_OPT_STRING,
933 .help = "the first of the 16 IPs the built-in DHCP server can assign",
934 }, {
935 .name = "dns",
936 .type = QEMU_OPT_STRING,
937 .help = "guest-visible address of the virtual nameserver",
938 }, {
939 .name = "smb",
940 .type = QEMU_OPT_STRING,
941 .help = "root directory of the built-in SMB server",
942 }, {
943 .name = "smbserver",
944 .type = QEMU_OPT_STRING,
945 .help = "IP address of the built-in SMB server",
946 }, {
947 .name = "hostfwd",
948 .type = QEMU_OPT_STRING,
949 .help = "guest port number to forward incoming TCP or UDP connections",
950 }, {
951 .name = "guestfwd",
952 .type = QEMU_OPT_STRING,
953 .help = "IP address and port to forward guest TCP connections",
955 { /* end of list */ }
957 #endif
958 }, {
959 .type = "tap",
960 .init = net_init_tap,
961 .desc = {
962 NET_COMMON_PARAMS_DESC,
964 .name = "ifname",
965 .type = QEMU_OPT_STRING,
966 .help = "interface name",
968 #ifndef _WIN32
970 .name = "fd",
971 .type = QEMU_OPT_STRING,
972 .help = "file descriptor of an already opened tap",
973 }, {
974 .name = "script",
975 .type = QEMU_OPT_STRING,
976 .help = "script to initialize the interface",
977 }, {
978 .name = "downscript",
979 .type = QEMU_OPT_STRING,
980 .help = "script to shut down the interface",
981 }, {
982 .name = "sndbuf",
983 .type = QEMU_OPT_SIZE,
984 .help = "send buffer limit"
985 }, {
986 .name = "vnet_hdr",
987 .type = QEMU_OPT_BOOL,
988 .help = "enable the IFF_VNET_HDR flag on the tap interface"
990 #endif /* _WIN32 */
991 { /* end of list */ }
993 }, {
994 .type = "socket",
995 .init = net_init_socket,
996 .desc = {
997 NET_COMMON_PARAMS_DESC,
999 .name = "fd",
1000 .type = QEMU_OPT_STRING,
1001 .help = "file descriptor of an already opened socket",
1002 }, {
1003 .name = "listen",
1004 .type = QEMU_OPT_STRING,
1005 .help = "port number, and optional hostname, to listen on",
1006 }, {
1007 .name = "connect",
1008 .type = QEMU_OPT_STRING,
1009 .help = "port number, and optional hostname, to connect to",
1010 }, {
1011 .name = "mcast",
1012 .type = QEMU_OPT_STRING,
1013 .help = "UDP multicast address and port number",
1015 { /* end of list */ }
1017 #ifdef CONFIG_VDE
1018 }, {
1019 .type = "vde",
1020 .init = net_init_vde,
1021 .desc = {
1022 NET_COMMON_PARAMS_DESC,
1024 .name = "sock",
1025 .type = QEMU_OPT_STRING,
1026 .help = "socket path",
1027 }, {
1028 .name = "port",
1029 .type = QEMU_OPT_NUMBER,
1030 .help = "port number",
1031 }, {
1032 .name = "group",
1033 .type = QEMU_OPT_STRING,
1034 .help = "group owner of socket",
1035 }, {
1036 .name = "mode",
1037 .type = QEMU_OPT_NUMBER,
1038 .help = "permissions for socket",
1040 { /* end of list */ }
1042 #endif
1043 }, {
1044 .type = "dump",
1045 .init = net_init_dump,
1046 .desc = {
1047 NET_COMMON_PARAMS_DESC,
1049 .name = "len",
1050 .type = QEMU_OPT_SIZE,
1051 .help = "per-packet size limit (64k default)",
1052 }, {
1053 .name = "file",
1054 .type = QEMU_OPT_STRING,
1055 .help = "dump file path (default is qemu-vlan0.pcap)",
1057 { /* end of list */ }
1060 { /* end of list */ }
1063 int net_client_init(Monitor *mon, QemuOpts *opts, int is_netdev)
1065 const char *name;
1066 const char *type;
1067 int i;
1069 type = qemu_opt_get(opts, "type");
1070 if (!type) {
1071 qemu_error("No type specified for -net\n");
1072 return -1;
1075 if (is_netdev) {
1076 if (strcmp(type, "tap") != 0 &&
1077 #ifdef CONFIG_SLIRP
1078 strcmp(type, "user") != 0 &&
1079 #endif
1080 #ifdef CONFIG_VDE
1081 strcmp(type, "vde") != 0 &&
1082 #endif
1083 strcmp(type, "socket") != 0) {
1084 qemu_error("The '%s' network backend type is not valid with -netdev\n",
1085 type);
1086 return -1;
1089 if (qemu_opt_get(opts, "vlan")) {
1090 qemu_error("The 'vlan' parameter is not valid with -netdev\n");
1091 return -1;
1093 if (qemu_opt_get(opts, "name")) {
1094 qemu_error("The 'name' parameter is not valid with -netdev\n");
1095 return -1;
1097 if (!qemu_opts_id(opts)) {
1098 qemu_error("The id= parameter is required with -netdev\n");
1099 return -1;
1103 name = qemu_opts_id(opts);
1104 if (!name) {
1105 name = qemu_opt_get(opts, "name");
1108 for (i = 0; net_client_types[i].type != NULL; i++) {
1109 if (!strcmp(net_client_types[i].type, type)) {
1110 VLANState *vlan = NULL;
1112 if (qemu_opts_validate(opts, &net_client_types[i].desc[0]) == -1) {
1113 return -1;
1116 /* Do not add to a vlan if it's a -netdev or a nic with a
1117 * netdev= parameter. */
1118 if (!(is_netdev ||
1119 (strcmp(type, "nic") == 0 && qemu_opt_get(opts, "netdev")))) {
1120 vlan = qemu_find_vlan(qemu_opt_get_number(opts, "vlan", 0), 1);
1123 if (net_client_types[i].init) {
1124 return net_client_types[i].init(opts, mon, name, vlan);
1125 } else {
1126 return 0;
1131 qemu_error("Invalid -net type '%s'\n", type);
1132 return -1;
1135 void net_client_uninit(NICInfo *nd)
1137 if (nd->vlan) {
1138 nd->vlan->nb_guest_devs--;
1140 nb_nics--;
1142 qemu_free(nd->model);
1143 qemu_free(nd->name);
1144 qemu_free(nd->devaddr);
1146 nd->used = 0;
1149 static int net_host_check_device(const char *device)
1151 int i;
1152 const char *valid_param_list[] = { "tap", "socket", "dump"
1153 #ifdef CONFIG_SLIRP
1154 ,"user"
1155 #endif
1156 #ifdef CONFIG_VDE
1157 ,"vde"
1158 #endif
1160 for (i = 0; i < sizeof(valid_param_list) / sizeof(char *); i++) {
1161 if (!strncmp(valid_param_list[i], device,
1162 strlen(valid_param_list[i])))
1163 return 1;
1166 return 0;
1169 void net_host_device_add(Monitor *mon, const QDict *qdict)
1171 const char *device = qdict_get_str(qdict, "device");
1172 const char *opts_str = qdict_get_try_str(qdict, "opts");
1173 QemuOpts *opts;
1175 if (!net_host_check_device(device)) {
1176 monitor_printf(mon, "invalid host network device %s\n", device);
1177 return;
1180 opts = qemu_opts_parse(&qemu_net_opts, opts_str ? opts_str : "", NULL);
1181 if (!opts) {
1182 monitor_printf(mon, "parsing network options '%s' failed\n",
1183 opts_str ? opts_str : "");
1184 return;
1187 qemu_opt_set(opts, "type", device);
1189 if (net_client_init(mon, opts, 0) < 0) {
1190 monitor_printf(mon, "adding host network device %s failed\n", device);
1194 void net_host_device_remove(Monitor *mon, const QDict *qdict)
1196 VLANClientState *vc;
1197 int vlan_id = qdict_get_int(qdict, "vlan_id");
1198 const char *device = qdict_get_str(qdict, "device");
1200 vc = qemu_find_vlan_client_by_name(mon, vlan_id, device);
1201 if (!vc) {
1202 return;
1204 if (!net_host_check_device(vc->model)) {
1205 monitor_printf(mon, "invalid host network device %s\n", device);
1206 return;
1208 qemu_del_vlan_client(vc);
1211 void net_set_boot_mask(int net_boot_mask)
1213 int i;
1215 /* Only the first four NICs may be bootable */
1216 net_boot_mask = net_boot_mask & 0xF;
1218 for (i = 0; i < nb_nics; i++) {
1219 if (net_boot_mask & (1 << i)) {
1220 nd_table[i].bootable = 1;
1221 net_boot_mask &= ~(1 << i);
1225 if (net_boot_mask) {
1226 fprintf(stderr, "Cannot boot from non-existent NIC\n");
1227 exit(1);
1231 void do_info_network(Monitor *mon)
1233 VLANState *vlan;
1235 QTAILQ_FOREACH(vlan, &vlans, next) {
1236 VLANClientState *vc;
1238 monitor_printf(mon, "VLAN %d devices:\n", vlan->id);
1240 QTAILQ_FOREACH(vc, &vlan->clients, next) {
1241 monitor_printf(mon, " %s: %s\n", vc->name, vc->info_str);
1246 void do_set_link(Monitor *mon, const QDict *qdict)
1248 VLANState *vlan;
1249 VLANClientState *vc = NULL;
1250 const char *name = qdict_get_str(qdict, "name");
1251 const char *up_or_down = qdict_get_str(qdict, "up_or_down");
1253 QTAILQ_FOREACH(vlan, &vlans, next) {
1254 QTAILQ_FOREACH(vc, &vlan->clients, next) {
1255 if (strcmp(vc->name, name) == 0) {
1256 goto done;
1260 done:
1262 if (!vc) {
1263 monitor_printf(mon, "could not find network device '%s'\n", name);
1264 return;
1267 if (strcmp(up_or_down, "up") == 0)
1268 vc->link_down = 0;
1269 else if (strcmp(up_or_down, "down") == 0)
1270 vc->link_down = 1;
1271 else
1272 monitor_printf(mon, "invalid link status '%s'; only 'up' or 'down' "
1273 "valid\n", up_or_down);
1275 if (vc->link_status_changed)
1276 vc->link_status_changed(vc);
1279 void net_cleanup(void)
1281 VLANState *vlan;
1282 VLANClientState *vc, *next_vc;
1284 QTAILQ_FOREACH(vlan, &vlans, next) {
1285 QTAILQ_FOREACH_SAFE(vc, &vlan->clients, next, next_vc) {
1286 qemu_del_vlan_client(vc);
1290 QTAILQ_FOREACH_SAFE(vc, &non_vlan_clients, next, next_vc) {
1291 qemu_del_vlan_client(vc);
1295 static void net_check_clients(void)
1297 VLANState *vlan;
1299 QTAILQ_FOREACH(vlan, &vlans, next) {
1300 if (vlan->nb_guest_devs == 0 && vlan->nb_host_devs == 0)
1301 continue;
1302 if (vlan->nb_guest_devs == 0)
1303 fprintf(stderr, "Warning: vlan %d with no nics\n", vlan->id);
1304 if (vlan->nb_host_devs == 0)
1305 fprintf(stderr,
1306 "Warning: vlan %d is not connected to host network\n",
1307 vlan->id);
1311 static int net_init_client(QemuOpts *opts, void *dummy)
1313 if (net_client_init(NULL, opts, 0) < 0)
1314 return -1;
1315 return 0;
1318 static int net_init_netdev(QemuOpts *opts, void *dummy)
1320 return net_client_init(NULL, opts, 1);
1323 int net_init_clients(void)
1325 if (QTAILQ_EMPTY(&qemu_net_opts.head)) {
1326 /* if no clients, we use a default config */
1327 qemu_opts_set(&qemu_net_opts, NULL, "type", "nic");
1328 #ifdef CONFIG_SLIRP
1329 qemu_opts_set(&qemu_net_opts, NULL, "type", "user");
1330 #endif
1333 QTAILQ_INIT(&vlans);
1334 QTAILQ_INIT(&non_vlan_clients);
1336 if (qemu_opts_foreach(&qemu_netdev_opts, net_init_netdev, NULL, 1) == -1)
1337 return -1;
1339 if (qemu_opts_foreach(&qemu_net_opts, net_init_client, NULL, 1) == -1) {
1340 return -1;
1343 net_check_clients();
1345 return 0;
1348 int net_client_parse(QemuOptsList *opts_list, const char *optarg)
1350 #if defined(CONFIG_SLIRP)
1351 int ret;
1352 if (net_slirp_parse_legacy(opts_list, optarg, &ret)) {
1353 return ret;
1355 #endif
1357 if (!qemu_opts_parse(opts_list, optarg, "type")) {
1358 return -1;
1361 return 0;