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
32 /* Needed early for HOST_BSD etc. */
33 #include "config-host.h"
36 #include <sys/times.h>
40 #include <sys/ioctl.h>
41 #include <sys/resource.h>
42 #include <sys/socket.h>
43 #include <netinet/in.h>
46 #include <net/if_tap.h>
49 #include <linux/if_tun.h>
51 #include <arpa/inet.h>
54 #include <sys/select.h>
57 #if defined(__FreeBSD__) || defined(__DragonFly__)
62 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
63 #include <freebsd/stdlib.h>
68 #include <linux/rtc.h>
70 /* For the benefit of older linux systems which don't supply it,
71 we use a local copy of hpet.h. */
72 /* #include <linux/hpet.h> */
75 #include <linux/ppdev.h>
76 #include <linux/parport.h>
80 #include <sys/ethernet.h>
81 #include <sys/sockio.h>
82 #include <netinet/arp.h>
83 #include <netinet/in.h>
84 #include <netinet/in_systm.h>
85 #include <netinet/ip.h>
86 #include <netinet/ip_icmp.h> // must come after ip.h
87 #include <netinet/udp.h>
88 #include <netinet/tcp.h>
96 #if defined(__OpenBSD__)
100 #if defined(CONFIG_VDE)
101 #include <libvdeplug.h>
107 #include <sys/timeb.h>
108 #include <mmsystem.h>
109 #define getopt_long_only getopt_long
110 #define memalign(align, size) malloc(size)
113 #include "qemu-common.h"
117 #include "qemu-timer.h"
118 #include "qemu-char.h"
119 #include "audio/audio.h"
120 #include "qemu_socket.h"
121 #include "qemu-log.h"
123 #if defined(CONFIG_SLIRP)
124 #include "libslirp.h"
128 static VLANState *first_vlan;
130 /***********************************************************/
131 /* network device redirectors */
133 #if defined(DEBUG_NET) || defined(DEBUG_SLIRP)
134 static void hex_dump(FILE *f, const uint8_t *buf, int size)
138 for(i=0;i<size;i+=16) {
142 fprintf(f, "%08x ", i);
145 fprintf(f, " %02x", buf[i+j]);
152 if (c < ' ' || c > '~')
161 static int parse_macaddr(uint8_t *macaddr, const char *p)
168 offset = strtol(p, &last_char, 0);
169 if (0 == errno && '\0' == *last_char &&
170 offset >= 0 && offset <= 0xFFFFFF) {
171 macaddr[3] = (offset & 0xFF0000) >> 16;
172 macaddr[4] = (offset & 0xFF00) >> 8;
173 macaddr[5] = offset & 0xFF;
176 for(i = 0; i < 6; i++) {
177 macaddr[i] = strtol(p, (char **)&p, 16);
182 if (*p != ':' && *p != '-')
193 static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
204 if (len > buf_size - 1)
213 int parse_host_src_port(struct sockaddr_in *haddr,
214 struct sockaddr_in *saddr,
215 const char *input_str)
217 char *str = strdup(input_str);
218 char *host_str = str;
220 const char *src_str2;
224 * Chop off any extra arguments at the end of the string which
225 * would start with a comma, then fill in the src port information
226 * if it was provided else use the "any address" and "any port".
228 if ((ptr = strchr(str,',')))
231 if ((src_str = strchr(input_str,'@'))) {
236 if (parse_host_port(haddr, host_str) < 0)
240 if (!src_str || *src_str == '\0')
243 if (parse_host_port(saddr, src_str2) < 0)
254 int parse_host_port(struct sockaddr_in *saddr, const char *str)
262 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
264 saddr->sin_family = AF_INET;
265 if (buf[0] == '\0') {
266 saddr->sin_addr.s_addr = 0;
268 if (qemu_isdigit(buf[0])) {
269 if (!inet_aton(buf, &saddr->sin_addr))
272 if ((he = gethostbyname(buf)) == NULL)
274 saddr->sin_addr = *(struct in_addr *)he->h_addr;
277 port = strtol(p, (char **)&r, 0);
280 saddr->sin_port = htons(port);
284 #if !defined(_WIN32) && 0
285 static int parse_unix_path(struct sockaddr_un *uaddr, const char *str)
290 len = MIN(108, strlen(str));
291 p = strchr(str, ',');
293 len = MIN(len, p - str);
295 memset(uaddr, 0, sizeof(*uaddr));
297 uaddr->sun_family = AF_UNIX;
298 memcpy(uaddr->sun_path, str, len);
304 void qemu_format_nic_info_str(VLANClientState *vc, uint8_t macaddr[6])
306 snprintf(vc->info_str, sizeof(vc->info_str),
307 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
309 macaddr[0], macaddr[1], macaddr[2],
310 macaddr[3], macaddr[4], macaddr[5]);
313 static char *assign_name(VLANClientState *vc1, const char *model)
319 for (vlan = first_vlan; vlan; vlan = vlan->next) {
322 for (vc = vlan->first_client; vc; vc = vc->next)
323 if (vc != vc1 && strcmp(vc->model, model) == 0)
327 snprintf(buf, sizeof(buf), "%s.%d", model, id);
332 VLANClientState *qemu_new_vlan_client(VLANState *vlan,
335 IOReadHandler *fd_read,
336 IOCanRWHandler *fd_can_read,
340 VLANClientState *vc, **pvc;
341 vc = qemu_mallocz(sizeof(VLANClientState));
342 vc->model = strdup(model);
344 vc->name = strdup(name);
346 vc->name = assign_name(vc, model);
347 vc->fd_read = fd_read;
348 vc->fd_can_read = fd_can_read;
349 vc->cleanup = cleanup;
354 pvc = &vlan->first_client;
361 void qemu_del_vlan_client(VLANClientState *vc)
363 VLANClientState **pvc = &vc->vlan->first_client;
379 VLANClientState *qemu_find_vlan_client(VLANState *vlan, void *opaque)
381 VLANClientState **pvc = &vlan->first_client;
384 if ((*pvc)->opaque == opaque)
392 int qemu_can_send_packet(VLANClientState *vc1)
394 VLANState *vlan = vc1->vlan;
397 for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
399 if (vc->fd_can_read && vc->fd_can_read(vc->opaque))
407 qemu_deliver_packet(VLANClientState *sender, const uint8_t *buf, int size)
411 for (vc = sender->vlan->first_client; vc != NULL; vc = vc->next) {
412 if (vc != sender && !vc->link_down) {
413 vc->fd_read(vc->opaque, buf, size);
418 void qemu_send_packet(VLANClientState *vc, const uint8_t *buf, int size)
420 VLANState *vlan = vc->vlan;
427 printf("vlan %d send:\n", vlan->id);
428 hex_dump(stdout, buf, size);
430 if (vlan->delivering) {
431 packet = qemu_malloc(sizeof(VLANPacket) + size);
432 packet->next = vlan->send_queue;
435 memcpy(packet->data, buf, size);
436 vlan->send_queue = packet;
438 vlan->delivering = 1;
439 qemu_deliver_packet(vc, buf, size);
440 while ((packet = vlan->send_queue) != NULL) {
441 qemu_deliver_packet(packet->sender, packet->data, packet->size);
442 vlan->send_queue = packet->next;
445 vlan->delivering = 0;
449 static ssize_t vc_sendv_compat(VLANClientState *vc, const struct iovec *iov,
452 uint8_t buffer[4096];
456 for (i = 0; i < iovcnt; i++) {
459 len = MIN(sizeof(buffer) - offset, iov[i].iov_len);
460 memcpy(buffer + offset, iov[i].iov_base, len);
464 vc->fd_read(vc->opaque, buffer, offset);
469 static ssize_t calc_iov_length(const struct iovec *iov, int iovcnt)
474 for (i = 0; i < iovcnt; i++)
475 offset += iov[i].iov_len;
479 ssize_t qemu_sendv_packet(VLANClientState *vc1, const struct iovec *iov,
482 VLANState *vlan = vc1->vlan;
487 return calc_iov_length(iov, iovcnt);
489 for (vc = vlan->first_client; vc != NULL; vc = vc->next) {
496 len = calc_iov_length(iov, iovcnt);
498 len = vc->fd_readv(vc->opaque, iov, iovcnt);
499 else if (vc->fd_read)
500 len = vc_sendv_compat(vc, iov, iovcnt);
502 max_len = MAX(max_len, len);
508 #if defined(CONFIG_SLIRP)
510 /* slirp network adapter */
512 static int slirp_inited;
513 static int slirp_restrict;
514 static char *slirp_ip;
515 static VLANClientState *slirp_vc;
517 int slirp_can_output(void)
519 return !slirp_vc || qemu_can_send_packet(slirp_vc);
522 void slirp_output(const uint8_t *pkt, int pkt_len)
525 printf("slirp output:\n");
526 hex_dump(stdout, pkt, pkt_len);
530 qemu_send_packet(slirp_vc, pkt, pkt_len);
533 int slirp_is_inited(void)
538 static void slirp_receive(void *opaque, const uint8_t *buf, int size)
541 printf("slirp input:\n");
542 hex_dump(stdout, buf, size);
544 slirp_input(buf, size);
547 static int slirp_in_use;
549 static void net_slirp_cleanup(VLANClientState *vc)
554 static int net_slirp_init(VLANState *vlan, const char *model, const char *name)
557 /* slirp only supports a single instance so far */
562 slirp_init(slirp_restrict, slirp_ip);
564 slirp_vc = qemu_new_vlan_client(vlan, model, name,
565 slirp_receive, NULL, net_slirp_cleanup, NULL);
566 slirp_vc->info_str[0] = '\0';
571 static void net_slirp_redir_print(void *opaque, int is_udp,
572 struct in_addr *laddr, u_int lport,
573 struct in_addr *faddr, u_int fport)
575 Monitor *mon = (Monitor *)opaque;
580 h_addr = ntohl(faddr->s_addr);
581 g_addr = ntohl(laddr->s_addr);
583 monitor_printf(mon, " %s |", is_udp ? "udp" : "tcp" );
584 snprintf(buf, 15, "%d.%d.%d.%d", (h_addr >> 24) & 0xff,
585 (h_addr >> 16) & 0xff,
586 (h_addr >> 8) & 0xff,
588 monitor_printf(mon, " %15s |", buf);
589 monitor_printf(mon, " %5d |", fport);
591 snprintf(buf, 15, "%d.%d.%d.%d", (g_addr >> 24) & 0xff,
592 (g_addr >> 16) & 0xff,
593 (g_addr >> 8) & 0xff,
595 monitor_printf(mon, " %15s |", buf);
596 monitor_printf(mon, " %5d\n", lport);
600 static void net_slirp_redir_list(Monitor *mon)
605 monitor_printf(mon, " Prot | Host Addr | HPort | Guest Addr | GPort\n");
606 monitor_printf(mon, " | | | | \n");
607 slirp_redir_loop(net_slirp_redir_print, mon);
610 static void net_slirp_redir_rm(Monitor *mon, const char *port_str)
614 const char *p = port_str;
621 if (!port_str || !port_str[0])
624 get_str_sep(buf, sizeof(buf), &p, ':');
626 if (!strcmp(buf, "tcp") || buf[0] == '\0') {
628 } else if (!strcmp(buf, "udp")) {
636 n = slirp_redir_rm(is_udp, host_port);
638 monitor_printf(mon, "removed %d redirections to %s port %d\n", n,
639 is_udp ? "udp" : "tcp", host_port);
643 monitor_printf(mon, "invalid format\n");
646 void net_slirp_redir(Monitor *mon, const char *redir_str, const char *redir_opt2)
650 const char *p, *errmsg;
651 struct in_addr guest_addr;
652 int host_port, guest_port;
656 slirp_init(slirp_restrict, slirp_ip);
659 if (!strcmp(redir_str, "remove")) {
660 net_slirp_redir_rm(mon, redir_opt2);
664 if (!strcmp(redir_str, "list")) {
665 net_slirp_redir_list(mon);
670 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
672 if (!strcmp(buf, "tcp") || buf[0] == '\0') {
674 } else if (!strcmp(buf, "udp")) {
680 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
682 host_port = strtol(buf, &r, 0);
686 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
688 if (buf[0] == '\0') {
689 pstrcpy(buf, sizeof(buf), "10.0.2.15");
691 if (!inet_aton(buf, &guest_addr))
694 guest_port = strtol(p, &r, 0);
698 if (slirp_redir(is_udp, host_port, guest_addr, guest_port) < 0) {
699 errmsg = "could not set up redirection\n";
705 errmsg = "invalid redirection format\n";
708 monitor_printf(mon, "%s", errmsg);
710 fprintf(stderr, "qemu: %s", errmsg);
717 static char smb_dir[1024];
719 static void erase_dir(char *dir_name)
725 /* erase all the files in the directory */
726 if ((d = opendir(dir_name)) != NULL) {
731 if (strcmp(de->d_name, ".") != 0 &&
732 strcmp(de->d_name, "..") != 0) {
733 snprintf(filename, sizeof(filename), "%s/%s",
734 smb_dir, de->d_name);
735 if (unlink(filename) != 0) /* is it a directory? */
744 /* automatic user mode samba server configuration */
745 static void smb_exit(void)
750 /* automatic user mode samba server configuration */
751 void net_slirp_smb(const char *exported_dir)
754 char smb_cmdline[1024];
759 slirp_init(slirp_restrict, slirp_ip);
762 /* XXX: better tmp dir construction */
763 snprintf(smb_dir, sizeof(smb_dir), "/tmp/qemu-smb.%ld", (long)getpid());
764 if (mkdir(smb_dir, 0700) < 0) {
765 fprintf(stderr, "qemu: could not create samba server dir '%s'\n", smb_dir);
768 snprintf(smb_conf, sizeof(smb_conf), "%s/%s", smb_dir, "smb.conf");
770 f = fopen(smb_conf, "w");
772 fprintf(stderr, "qemu: could not create samba server configuration file '%s'\n", smb_conf);
779 "socket address=127.0.0.1\n"
781 "lock directory=%s\n"
782 "log file=%s/log.smbd\n"
783 "smb passwd file=%s/smbpasswd\n"
799 snprintf(smb_cmdline, sizeof(smb_cmdline), "%s -s %s",
800 SMBD_COMMAND, smb_conf);
802 slirp_add_exec(0, smb_cmdline, 4, 139);
805 #endif /* !defined(_WIN32) */
806 void do_info_slirp(Monitor *mon)
816 static int vmchannel_can_read(void *opaque)
818 struct VMChannel *vmc = (struct VMChannel*)opaque;
819 return slirp_socket_can_recv(4, vmc->port);
822 static void vmchannel_read(void *opaque, const uint8_t *buf, int size)
824 struct VMChannel *vmc = (struct VMChannel*)opaque;
825 slirp_socket_recv(4, vmc->port, buf, size);
828 #endif /* CONFIG_SLIRP */
832 typedef struct TAPState {
835 char down_script[1024];
836 char down_script_arg[128];
839 static int launch_script(const char *setup_script, const char *ifname, int fd);
841 static ssize_t tap_receive_iov(void *opaque, const struct iovec *iov,
844 TAPState *s = opaque;
848 len = writev(s->fd, iov, iovcnt);
849 } while (len == -1 && (errno == EINTR || errno == EAGAIN));
854 static void tap_receive(void *opaque, const uint8_t *buf, int size)
856 TAPState *s = opaque;
859 ret = write(s->fd, buf, size);
860 if (ret < 0 && (errno == EINTR || errno == EAGAIN)) {
867 static void tap_send(void *opaque)
869 TAPState *s = opaque;
876 sbuf.maxlen = sizeof(buf);
877 sbuf.buf = (char *)buf;
878 size = getmsg(s->fd, NULL, &sbuf, &f) >=0 ? sbuf.len : -1;
880 size = read(s->fd, buf, sizeof(buf));
883 qemu_send_packet(s->vc, buf, size);
887 static void tap_cleanup(VLANClientState *vc)
889 TAPState *s = vc->opaque;
891 if (s->down_script[0])
892 launch_script(s->down_script, s->down_script_arg, s->fd);
894 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
901 static TAPState *net_tap_fd_init(VLANState *vlan,
908 s = qemu_mallocz(sizeof(TAPState));
910 s->vc = qemu_new_vlan_client(vlan, model, name, tap_receive,
911 NULL, tap_cleanup, s);
912 s->vc->fd_readv = tap_receive_iov;
913 qemu_set_fd_handler(s->fd, tap_send, NULL, s);
914 snprintf(s->vc->info_str, sizeof(s->vc->info_str), "fd=%d", fd);
918 #if defined (HOST_BSD) || defined (__FreeBSD_kernel__)
919 static int tap_open(char *ifname, int ifname_size)
925 TFR(fd = open("/dev/tap", O_RDWR));
927 fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n");
932 dev = devname(s.st_rdev, S_IFCHR);
933 pstrcpy(ifname, ifname_size, dev);
935 fcntl(fd, F_SETFL, O_NONBLOCK);
938 #elif defined(__sun__)
939 #define TUNNEWPPA (('T'<<16) | 0x0001)
941 * Allocate TAP device, returns opened fd.
942 * Stores dev name in the first arg(must be large enough).
944 static int tap_alloc(char *dev, size_t dev_size)
946 int tap_fd, if_fd, ppa = -1;
947 static int ip_fd = 0;
950 static int arp_fd = 0;
951 int ip_muxid, arp_muxid;
952 struct strioctl strioc_if, strioc_ppa;
953 int link_type = I_PLINK;;
955 char actual_name[32] = "";
957 memset(&ifr, 0x0, sizeof(ifr));
961 while( *ptr && !qemu_isdigit((int)*ptr) ) ptr++;
965 /* Check if IP device was opened */
969 TFR(ip_fd = open("/dev/udp", O_RDWR, 0));
971 syslog(LOG_ERR, "Can't open /dev/ip (actually /dev/udp)");
975 TFR(tap_fd = open("/dev/tap", O_RDWR, 0));
977 syslog(LOG_ERR, "Can't open /dev/tap");
981 /* Assign a new PPA and get its unit number. */
982 strioc_ppa.ic_cmd = TUNNEWPPA;
983 strioc_ppa.ic_timout = 0;
984 strioc_ppa.ic_len = sizeof(ppa);
985 strioc_ppa.ic_dp = (char *)&ppa;
986 if ((ppa = ioctl (tap_fd, I_STR, &strioc_ppa)) < 0)
987 syslog (LOG_ERR, "Can't assign new interface");
989 TFR(if_fd = open("/dev/tap", O_RDWR, 0));
991 syslog(LOG_ERR, "Can't open /dev/tap (2)");
994 if(ioctl(if_fd, I_PUSH, "ip") < 0){
995 syslog(LOG_ERR, "Can't push IP module");
999 if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0)
1000 syslog(LOG_ERR, "Can't get flags\n");
1002 snprintf (actual_name, 32, "tap%d", ppa);
1003 pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name);
1006 /* Assign ppa according to the unit number returned by tun device */
1008 if (ioctl (if_fd, SIOCSLIFNAME, &ifr) < 0)
1009 syslog (LOG_ERR, "Can't set PPA %d", ppa);
1010 if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) <0)
1011 syslog (LOG_ERR, "Can't get flags\n");
1012 /* Push arp module to if_fd */
1013 if (ioctl (if_fd, I_PUSH, "arp") < 0)
1014 syslog (LOG_ERR, "Can't push ARP module (2)");
1016 /* Push arp module to ip_fd */
1017 if (ioctl (ip_fd, I_POP, NULL) < 0)
1018 syslog (LOG_ERR, "I_POP failed\n");
1019 if (ioctl (ip_fd, I_PUSH, "arp") < 0)
1020 syslog (LOG_ERR, "Can't push ARP module (3)\n");
1022 TFR(arp_fd = open ("/dev/tap", O_RDWR, 0));
1024 syslog (LOG_ERR, "Can't open %s\n", "/dev/tap");
1026 /* Set ifname to arp */
1027 strioc_if.ic_cmd = SIOCSLIFNAME;
1028 strioc_if.ic_timout = 0;
1029 strioc_if.ic_len = sizeof(ifr);
1030 strioc_if.ic_dp = (char *)𝔦
1031 if (ioctl(arp_fd, I_STR, &strioc_if) < 0){
1032 syslog (LOG_ERR, "Can't set ifname to arp\n");
1035 if((ip_muxid = ioctl(ip_fd, I_LINK, if_fd)) < 0){
1036 syslog(LOG_ERR, "Can't link TAP device to IP");
1040 if ((arp_muxid = ioctl (ip_fd, link_type, arp_fd)) < 0)
1041 syslog (LOG_ERR, "Can't link TAP device to ARP");
1045 memset(&ifr, 0x0, sizeof(ifr));
1046 pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name);
1047 ifr.lifr_ip_muxid = ip_muxid;
1048 ifr.lifr_arp_muxid = arp_muxid;
1050 if (ioctl (ip_fd, SIOCSLIFMUXID, &ifr) < 0)
1052 ioctl (ip_fd, I_PUNLINK , arp_muxid);
1053 ioctl (ip_fd, I_PUNLINK, ip_muxid);
1054 syslog (LOG_ERR, "Can't set multiplexor id");
1057 snprintf(dev, dev_size, "tap%d", ppa);
1061 static int tap_open(char *ifname, int ifname_size)
1065 if( (fd = tap_alloc(dev, sizeof(dev))) < 0 ){
1066 fprintf(stderr, "Cannot allocate TAP device\n");
1069 pstrcpy(ifname, ifname_size, dev);
1070 fcntl(fd, F_SETFL, O_NONBLOCK);
1073 #elif defined (_AIX)
1074 static int tap_open(char *ifname, int ifname_size)
1076 fprintf (stderr, "no tap on AIX\n");
1080 static int tap_open(char *ifname, int ifname_size)
1085 TFR(fd = open("/dev/net/tun", O_RDWR));
1087 fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
1090 memset(&ifr, 0, sizeof(ifr));
1091 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
1092 if (ifname[0] != '\0')
1093 pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname);
1095 pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d");
1096 ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
1098 fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
1102 pstrcpy(ifname, ifname_size, ifr.ifr_name);
1103 fcntl(fd, F_SETFL, O_NONBLOCK);
1108 static int launch_script(const char *setup_script, const char *ifname, int fd)
1114 /* try to launch network script */
1118 int open_max = sysconf (_SC_OPEN_MAX), i;
1119 for (i = 0; i < open_max; i++)
1120 if (i != STDIN_FILENO &&
1121 i != STDOUT_FILENO &&
1122 i != STDERR_FILENO &&
1127 *parg++ = (char *)setup_script;
1128 *parg++ = (char *)ifname;
1130 execv(setup_script, args);
1133 while (waitpid(pid, &status, 0) != pid);
1134 if (!WIFEXITED(status) ||
1135 WEXITSTATUS(status) != 0) {
1136 fprintf(stderr, "%s: could not launch network script\n",
1144 static int net_tap_init(VLANState *vlan, const char *model,
1145 const char *name, const char *ifname1,
1146 const char *setup_script, const char *down_script)
1152 if (ifname1 != NULL)
1153 pstrcpy(ifname, sizeof(ifname), ifname1);
1156 TFR(fd = tap_open(ifname, sizeof(ifname)));
1160 if (!setup_script || !strcmp(setup_script, "no"))
1162 if (setup_script[0] != '\0') {
1163 if (launch_script(setup_script, ifname, fd))
1166 s = net_tap_fd_init(vlan, model, name, fd);
1167 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1168 "ifname=%s,script=%s,downscript=%s",
1169 ifname, setup_script, down_script);
1170 if (down_script && strcmp(down_script, "no")) {
1171 snprintf(s->down_script, sizeof(s->down_script), "%s", down_script);
1172 snprintf(s->down_script_arg, sizeof(s->down_script_arg), "%s", ifname);
1177 #endif /* !_WIN32 */
1179 #if defined(CONFIG_VDE)
1180 typedef struct VDEState {
1181 VLANClientState *vc;
1185 static void vde_to_qemu(void *opaque)
1187 VDEState *s = opaque;
1191 size = vde_recv(s->vde, (char *)buf, sizeof(buf), 0);
1193 qemu_send_packet(s->vc, buf, size);
1197 static void vde_from_qemu(void *opaque, const uint8_t *buf, int size)
1199 VDEState *s = opaque;
1202 ret = vde_send(s->vde, (const char *)buf, size, 0);
1203 if (ret < 0 && errno == EINTR) {
1210 static void vde_cleanup(VLANClientState *vc)
1212 VDEState *s = vc->opaque;
1213 qemu_set_fd_handler(vde_datafd(s->vde), NULL, NULL, NULL);
1218 static int net_vde_init(VLANState *vlan, const char *model,
1219 const char *name, const char *sock,
1220 int port, const char *group, int mode)
1223 char *init_group = strlen(group) ? (char *)group : NULL;
1224 char *init_sock = strlen(sock) ? (char *)sock : NULL;
1226 struct vde_open_args args = {
1228 .group = init_group,
1232 s = qemu_mallocz(sizeof(VDEState));
1233 s->vde = vde_open(init_sock, (char *)"QEMU", &args);
1238 s->vc = qemu_new_vlan_client(vlan, model, name, vde_from_qemu,
1239 NULL, vde_cleanup, s);
1240 qemu_set_fd_handler(vde_datafd(s->vde), vde_to_qemu, NULL, s);
1241 snprintf(s->vc->info_str, sizeof(s->vc->info_str), "sock=%s,fd=%d",
1242 sock, vde_datafd(s->vde));
1247 /* network connection */
1248 typedef struct NetSocketState {
1249 VLANClientState *vc;
1251 int state; /* 0 = getting length, 1 = getting data */
1253 unsigned int packet_len;
1255 struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
1258 typedef struct NetSocketListenState {
1263 } NetSocketListenState;
1265 /* XXX: we consider we can send the whole packet without blocking */
1266 static void net_socket_receive(void *opaque, const uint8_t *buf, int size)
1268 NetSocketState *s = opaque;
1272 send_all(s->fd, (const uint8_t *)&len, sizeof(len));
1273 send_all(s->fd, buf, size);
1276 static void net_socket_receive_dgram(void *opaque, const uint8_t *buf, int size)
1278 NetSocketState *s = opaque;
1279 sendto(s->fd, buf, size, 0,
1280 (struct sockaddr *)&s->dgram_dst, sizeof(s->dgram_dst));
1283 static void net_socket_send(void *opaque)
1285 NetSocketState *s = opaque;
1291 size = recv(s->fd, buf1, sizeof(buf1), 0);
1293 err = socket_error();
1294 if (err != EWOULDBLOCK)
1296 } else if (size == 0) {
1297 /* end of connection */
1299 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1305 /* reassemble a packet from the network */
1311 memcpy(s->buf + s->index, buf, l);
1315 if (s->index == 4) {
1317 s->packet_len = ntohl(*(uint32_t *)s->buf);
1323 l = s->packet_len - s->index;
1326 if (s->index + l <= sizeof(s->buf)) {
1327 memcpy(s->buf + s->index, buf, l);
1329 fprintf(stderr, "serious error: oversized packet received,"
1330 "connection terminated.\n");
1338 if (s->index >= s->packet_len) {
1339 qemu_send_packet(s->vc, s->buf, s->packet_len);
1348 static void net_socket_send_dgram(void *opaque)
1350 NetSocketState *s = opaque;
1353 size = recv(s->fd, s->buf, sizeof(s->buf), 0);
1357 /* end of connection */
1358 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1361 qemu_send_packet(s->vc, s->buf, size);
1364 static int net_socket_mcast_create(struct sockaddr_in *mcastaddr)
1369 if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) {
1370 fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
1371 inet_ntoa(mcastaddr->sin_addr),
1372 (int)ntohl(mcastaddr->sin_addr.s_addr));
1376 fd = socket(PF_INET, SOCK_DGRAM, 0);
1378 perror("socket(PF_INET, SOCK_DGRAM)");
1383 ret=setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
1384 (const char *)&val, sizeof(val));
1386 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
1390 ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr));
1396 /* Add host to multicast group */
1397 imr.imr_multiaddr = mcastaddr->sin_addr;
1398 imr.imr_interface.s_addr = htonl(INADDR_ANY);
1400 ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1401 (const char *)&imr, sizeof(struct ip_mreq));
1403 perror("setsockopt(IP_ADD_MEMBERSHIP)");
1407 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
1409 ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
1410 (const char *)&val, sizeof(val));
1412 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
1416 socket_set_nonblock(fd);
1424 static void net_socket_cleanup(VLANClientState *vc)
1426 NetSocketState *s = vc->opaque;
1427 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1432 static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan,
1435 int fd, int is_connected)
1437 struct sockaddr_in saddr;
1439 socklen_t saddr_len;
1442 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
1443 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
1444 * by ONLY ONE process: we must "clone" this dgram socket --jjo
1448 if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) {
1450 if (saddr.sin_addr.s_addr==0) {
1451 fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
1455 /* clone dgram socket */
1456 newfd = net_socket_mcast_create(&saddr);
1458 /* error already reported by net_socket_mcast_create() */
1462 /* clone newfd to fd, close newfd */
1467 fprintf(stderr, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
1468 fd, strerror(errno));
1473 s = qemu_mallocz(sizeof(NetSocketState));
1476 s->vc = qemu_new_vlan_client(vlan, model, name, net_socket_receive_dgram,
1477 NULL, net_socket_cleanup, s);
1478 qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s);
1480 /* mcast: save bound address as dst */
1481 if (is_connected) s->dgram_dst=saddr;
1483 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1484 "socket: fd=%d (%s mcast=%s:%d)",
1485 fd, is_connected? "cloned" : "",
1486 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1490 static void net_socket_connect(void *opaque)
1492 NetSocketState *s = opaque;
1493 qemu_set_fd_handler(s->fd, net_socket_send, NULL, s);
1496 static NetSocketState *net_socket_fd_init_stream(VLANState *vlan,
1499 int fd, int is_connected)
1502 s = qemu_mallocz(sizeof(NetSocketState));
1504 s->vc = qemu_new_vlan_client(vlan, model, name, net_socket_receive,
1505 NULL, net_socket_cleanup, s);
1506 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1507 "socket: fd=%d", fd);
1509 net_socket_connect(s);
1511 qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s);
1516 static NetSocketState *net_socket_fd_init(VLANState *vlan,
1517 const char *model, const char *name,
1518 int fd, int is_connected)
1520 int so_type=-1, optlen=sizeof(so_type);
1522 if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type,
1523 (socklen_t *)&optlen)< 0) {
1524 fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd);
1529 return net_socket_fd_init_dgram(vlan, model, name, fd, is_connected);
1531 return net_socket_fd_init_stream(vlan, model, name, fd, is_connected);
1533 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
1534 fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd);
1535 return net_socket_fd_init_stream(vlan, model, name, fd, is_connected);
1540 static void net_socket_accept(void *opaque)
1542 NetSocketListenState *s = opaque;
1544 struct sockaddr_in saddr;
1549 len = sizeof(saddr);
1550 fd = accept(s->fd, (struct sockaddr *)&saddr, &len);
1551 if (fd < 0 && errno != EINTR) {
1553 } else if (fd >= 0) {
1557 s1 = net_socket_fd_init(s->vlan, s->model, s->name, fd, 1);
1561 snprintf(s1->vc->info_str, sizeof(s1->vc->info_str),
1562 "socket: connection from %s:%d",
1563 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1567 static int net_socket_listen_init(VLANState *vlan,
1570 const char *host_str)
1572 NetSocketListenState *s;
1574 struct sockaddr_in saddr;
1576 if (parse_host_port(&saddr, host_str) < 0)
1579 s = qemu_mallocz(sizeof(NetSocketListenState));
1581 fd = socket(PF_INET, SOCK_STREAM, 0);
1586 socket_set_nonblock(fd);
1588 /* allow fast reuse */
1590 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
1592 ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
1597 ret = listen(fd, 0);
1603 s->model = strdup(model);
1604 s->name = name ? strdup(name) : NULL;
1606 qemu_set_fd_handler(fd, net_socket_accept, NULL, s);
1610 static int net_socket_connect_init(VLANState *vlan,
1613 const char *host_str)
1616 int fd, connected, ret, err;
1617 struct sockaddr_in saddr;
1619 if (parse_host_port(&saddr, host_str) < 0)
1622 fd = socket(PF_INET, SOCK_STREAM, 0);
1627 socket_set_nonblock(fd);
1631 ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
1633 err = socket_error();
1634 if (err == EINTR || err == EWOULDBLOCK) {
1635 } else if (err == EINPROGRESS) {
1638 } else if (err == WSAEALREADY) {
1651 s = net_socket_fd_init(vlan, model, name, fd, connected);
1654 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1655 "socket: connect to %s:%d",
1656 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1660 static int net_socket_mcast_init(VLANState *vlan,
1663 const char *host_str)
1667 struct sockaddr_in saddr;
1669 if (parse_host_port(&saddr, host_str) < 0)
1673 fd = net_socket_mcast_create(&saddr);
1677 s = net_socket_fd_init(vlan, model, name, fd, 0);
1681 s->dgram_dst = saddr;
1683 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1684 "socket: mcast=%s:%d",
1685 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1690 typedef struct DumpState {
1691 VLANClientState *pcap_vc;
1696 #define PCAP_MAGIC 0xa1b2c3d4
1698 struct pcap_file_hdr {
1700 uint16_t version_major;
1701 uint16_t version_minor;
1708 struct pcap_sf_pkthdr {
1717 static void dump_receive(void *opaque, const uint8_t *buf, int size)
1719 DumpState *s = opaque;
1720 struct pcap_sf_pkthdr hdr;
1724 /* Early return in case of previous error. */
1729 ts = muldiv64(qemu_get_clock(vm_clock), 1000000, ticks_per_sec);
1730 caplen = size > s->pcap_caplen ? s->pcap_caplen : size;
1732 hdr.ts.tv_sec = ts / 1000000;
1733 hdr.ts.tv_usec = ts % 1000000;
1734 hdr.caplen = caplen;
1736 if (write(s->fd, &hdr, sizeof(hdr)) != sizeof(hdr) ||
1737 write(s->fd, buf, caplen) != caplen) {
1738 qemu_log("-net dump write error - stop dump\n");
1744 static void net_dump_cleanup(VLANClientState *vc)
1746 DumpState *s = vc->opaque;
1752 static int net_dump_init(VLANState *vlan, const char *device,
1753 const char *name, const char *filename, int len)
1755 struct pcap_file_hdr hdr;
1758 s = qemu_malloc(sizeof(DumpState));
1760 s->fd = open(filename, O_CREAT | O_WRONLY, 0644);
1762 fprintf(stderr, "-net dump: can't open %s\n", filename);
1766 s->pcap_caplen = len;
1768 hdr.magic = PCAP_MAGIC;
1769 hdr.version_major = 2;
1770 hdr.version_minor = 4;
1773 hdr.snaplen = s->pcap_caplen;
1776 if (write(s->fd, &hdr, sizeof(hdr)) < sizeof(hdr)) {
1777 perror("-net dump write error");
1783 s->pcap_vc = qemu_new_vlan_client(vlan, device, name, dump_receive, NULL,
1784 net_dump_cleanup, s);
1785 snprintf(s->pcap_vc->info_str, sizeof(s->pcap_vc->info_str),
1786 "dump to %s (len=%d)", filename, len);
1790 /* find or alloc a new VLAN */
1791 VLANState *qemu_find_vlan(int id)
1793 VLANState **pvlan, *vlan;
1794 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
1798 vlan = qemu_mallocz(sizeof(VLANState));
1801 pvlan = &first_vlan;
1802 while (*pvlan != NULL)
1803 pvlan = &(*pvlan)->next;
1808 static int nic_get_free_idx(void)
1812 for (index = 0; index < MAX_NICS; index++)
1813 if (!nd_table[index].used)
1818 void qemu_check_nic_model(NICInfo *nd, const char *model)
1820 const char *models[2];
1825 qemu_check_nic_model_list(nd, models, model);
1828 void qemu_check_nic_model_list(NICInfo *nd, const char * const *models,
1829 const char *default_model)
1831 int i, exit_status = 0;
1834 nd->model = strdup(default_model);
1836 if (strcmp(nd->model, "?") != 0) {
1837 for (i = 0 ; models[i]; i++)
1838 if (strcmp(nd->model, models[i]) == 0)
1841 fprintf(stderr, "qemu: Unsupported NIC model: %s\n", nd->model);
1845 fprintf(stderr, "qemu: Supported NIC models: ");
1846 for (i = 0 ; models[i]; i++)
1847 fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n');
1852 int net_client_init(const char *device, const char *p)
1854 static const char * const fd_params[] = {
1855 "vlan", "name", "fd", NULL
1863 if (get_param_value(buf, sizeof(buf), "vlan", p)) {
1864 vlan_id = strtol(buf, NULL, 0);
1866 vlan = qemu_find_vlan(vlan_id);
1868 if (get_param_value(buf, sizeof(buf), "name", p)) {
1871 if (!strcmp(device, "nic")) {
1872 static const char * const nic_params[] = {
1873 "vlan", "name", "macaddr", "model", NULL
1877 int idx = nic_get_free_idx();
1879 if (check_params(nic_params, p) < 0) {
1880 fprintf(stderr, "qemu: invalid parameter in '%s'\n", p);
1883 if (idx == -1 || nb_nics >= MAX_NICS) {
1884 fprintf(stderr, "Too Many NICs\n");
1888 nd = &nd_table[idx];
1889 macaddr = nd->macaddr;
1895 macaddr[5] = 0x56 + idx;
1897 if (get_param_value(buf, sizeof(buf), "macaddr", p)) {
1898 if (parse_macaddr(macaddr, buf) < 0) {
1899 fprintf(stderr, "invalid syntax for ethernet address\n");
1904 if (get_param_value(buf, sizeof(buf), "model", p)) {
1905 nd->model = strdup(buf);
1912 vlan->nb_guest_devs++;
1915 if (!strcmp(device, "none")) {
1917 fprintf(stderr, "qemu: 'none' takes no parameters\n");
1920 /* does nothing. It is needed to signal that no network cards
1925 if (!strcmp(device, "user")) {
1926 static const char * const slirp_params[] = {
1927 "vlan", "name", "hostname", "restrict", "ip", NULL
1929 if (check_params(slirp_params, p) < 0) {
1930 fprintf(stderr, "qemu: invalid parameter in '%s'\n", p);
1933 if (get_param_value(buf, sizeof(buf), "hostname", p)) {
1934 pstrcpy(slirp_hostname, sizeof(slirp_hostname), buf);
1936 if (get_param_value(buf, sizeof(buf), "restrict", p)) {
1937 slirp_restrict = (buf[0] == 'y') ? 1 : 0;
1939 if (get_param_value(buf, sizeof(buf), "ip", p)) {
1940 slirp_ip = strdup(buf);
1942 vlan->nb_host_devs++;
1943 ret = net_slirp_init(vlan, device, name);
1944 } else if (!strcmp(device, "channel")) {
1946 char name[20], *devname;
1947 struct VMChannel *vmc;
1949 port = strtol(p, &devname, 10);
1951 if (port < 1 || port > 65535) {
1952 fprintf(stderr, "vmchannel wrong port number\n");
1956 vmc = malloc(sizeof(struct VMChannel));
1957 snprintf(name, 20, "vmchannel%ld", port);
1958 vmc->hd = qemu_chr_open(name, devname, NULL);
1960 fprintf(stderr, "qemu: could not open vmchannel device"
1966 slirp_add_exec(3, vmc->hd, 4, port);
1967 qemu_chr_add_handlers(vmc->hd, vmchannel_can_read, vmchannel_read,
1973 if (!strcmp(device, "tap")) {
1974 static const char * const tap_params[] = {
1975 "vlan", "name", "ifname", NULL
1979 if (check_params(tap_params, p) < 0) {
1980 fprintf(stderr, "qemu: invalid parameter in '%s'\n", p);
1983 if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
1984 fprintf(stderr, "tap: no interface name\n");
1988 vlan->nb_host_devs++;
1989 ret = tap_win32_init(vlan, device, name, ifname);
1991 #elif defined (_AIX)
1993 if (!strcmp(device, "tap")) {
1995 char setup_script[1024], down_script[1024];
1997 vlan->nb_host_devs++;
1998 if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
1999 if (check_params(fd_params, p) < 0) {
2000 fprintf(stderr, "qemu: invalid parameter in '%s'\n", p);
2003 fd = strtol(buf, NULL, 0);
2004 fcntl(fd, F_SETFL, O_NONBLOCK);
2005 net_tap_fd_init(vlan, device, name, fd);
2008 static const char * const tap_params[] = {
2009 "vlan", "name", "ifname", "script", "downscript", NULL
2011 if (check_params(tap_params, p) < 0) {
2012 fprintf(stderr, "qemu: invalid parameter in '%s'\n", p);
2015 if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
2018 if (get_param_value(setup_script, sizeof(setup_script), "script", p) == 0) {
2019 pstrcpy(setup_script, sizeof(setup_script), DEFAULT_NETWORK_SCRIPT);
2021 if (get_param_value(down_script, sizeof(down_script), "downscript", p) == 0) {
2022 pstrcpy(down_script, sizeof(down_script), DEFAULT_NETWORK_DOWN_SCRIPT);
2024 ret = net_tap_init(vlan, device, name, ifname, setup_script, down_script);
2028 if (!strcmp(device, "socket")) {
2029 if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
2031 if (check_params(fd_params, p) < 0) {
2032 fprintf(stderr, "qemu: invalid parameter in '%s'\n", p);
2035 fd = strtol(buf, NULL, 0);
2037 if (net_socket_fd_init(vlan, device, name, fd, 1))
2039 } else if (get_param_value(buf, sizeof(buf), "listen", p) > 0) {
2040 static const char * const listen_params[] = {
2041 "vlan", "name", "listen", NULL
2043 if (check_params(listen_params, p) < 0) {
2044 fprintf(stderr, "qemu: invalid parameter in '%s'\n", p);
2047 ret = net_socket_listen_init(vlan, device, name, buf);
2048 } else if (get_param_value(buf, sizeof(buf), "connect", p) > 0) {
2049 static const char * const connect_params[] = {
2050 "vlan", "name", "connect", NULL
2052 if (check_params(connect_params, p) < 0) {
2053 fprintf(stderr, "qemu: invalid parameter in '%s'\n", p);
2056 ret = net_socket_connect_init(vlan, device, name, buf);
2057 } else if (get_param_value(buf, sizeof(buf), "mcast", p) > 0) {
2058 static const char * const mcast_params[] = {
2059 "vlan", "name", "mcast", NULL
2061 if (check_params(mcast_params, p) < 0) {
2062 fprintf(stderr, "qemu: invalid parameter in '%s'\n", p);
2065 ret = net_socket_mcast_init(vlan, device, name, buf);
2067 fprintf(stderr, "Unknown socket options: %s\n", p);
2071 vlan->nb_host_devs++;
2074 if (!strcmp(device, "vde")) {
2075 static const char * const vde_params[] = {
2076 "vlan", "name", "sock", "port", "group", "mode", NULL
2078 char vde_sock[1024], vde_group[512];
2079 int vde_port, vde_mode;
2081 if (check_params(vde_params, p) < 0) {
2082 fprintf(stderr, "qemu: invalid parameter in '%s'\n", p);
2085 vlan->nb_host_devs++;
2086 if (get_param_value(vde_sock, sizeof(vde_sock), "sock", p) <= 0) {
2089 if (get_param_value(buf, sizeof(buf), "port", p) > 0) {
2090 vde_port = strtol(buf, NULL, 10);
2094 if (get_param_value(vde_group, sizeof(vde_group), "group", p) <= 0) {
2095 vde_group[0] = '\0';
2097 if (get_param_value(buf, sizeof(buf), "mode", p) > 0) {
2098 vde_mode = strtol(buf, NULL, 8);
2102 ret = net_vde_init(vlan, device, name, vde_sock, vde_port, vde_group, vde_mode);
2105 if (!strcmp(device, "dump")) {
2108 if (get_param_value(buf, sizeof(buf), "len", p) > 0) {
2109 len = strtol(buf, NULL, 0);
2111 if (!get_param_value(buf, sizeof(buf), "file", p)) {
2112 snprintf(buf, sizeof(buf), "qemu-vlan%d.pcap", vlan_id);
2114 ret = net_dump_init(vlan, device, name, buf, len);
2116 fprintf(stderr, "Unknown network device: %s\n", device);
2121 fprintf(stderr, "Could not initialize device '%s'\n", device);
2129 void net_client_uninit(NICInfo *nd)
2131 nd->vlan->nb_guest_devs--;
2134 free((void *)nd->model);
2137 static int net_host_check_device(const char *device)
2140 const char *valid_param_list[] = { "tap", "socket", "dump"
2148 for (i = 0; i < sizeof(valid_param_list) / sizeof(char *); i++) {
2149 if (!strncmp(valid_param_list[i], device,
2150 strlen(valid_param_list[i])))
2157 void net_host_device_add(Monitor *mon, const char *device, const char *opts)
2159 if (!net_host_check_device(device)) {
2160 monitor_printf(mon, "invalid host network device %s\n", device);
2163 if (net_client_init(device, opts ? opts : "") < 0) {
2164 monitor_printf(mon, "adding host network device %s failed\n", device);
2168 void net_host_device_remove(Monitor *mon, int vlan_id, const char *device)
2171 VLANClientState *vc;
2173 vlan = qemu_find_vlan(vlan_id);
2175 for (vc = vlan->first_client; vc != NULL; vc = vc->next) {
2176 if (!strcmp(vc->name, device)) {
2182 monitor_printf(mon, "can't find device %s\n", device);
2185 if (!net_host_check_device(vc->model)) {
2186 monitor_printf(mon, "invalid host network device %s\n", device);
2189 qemu_del_vlan_client(vc);
2192 int net_client_parse(const char *str)
2200 while (*p != '\0' && *p != ',') {
2201 if ((q - device) < sizeof(device) - 1)
2209 return net_client_init(device, p);
2212 void do_info_network(Monitor *mon)
2215 VLANClientState *vc;
2217 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
2218 monitor_printf(mon, "VLAN %d devices:\n", vlan->id);
2219 for(vc = vlan->first_client; vc != NULL; vc = vc->next)
2220 monitor_printf(mon, " %s: %s\n", vc->name, vc->info_str);
2224 int do_set_link(Monitor *mon, const char *name, const char *up_or_down)
2227 VLANClientState *vc = NULL;
2229 for (vlan = first_vlan; vlan != NULL; vlan = vlan->next)
2230 for (vc = vlan->first_client; vc != NULL; vc = vc->next)
2231 if (strcmp(vc->name, name) == 0)
2236 monitor_printf(mon, "could not find network device '%s'", name);
2240 if (strcmp(up_or_down, "up") == 0)
2242 else if (strcmp(up_or_down, "down") == 0)
2245 monitor_printf(mon, "invalid link status '%s'; only 'up' or 'down' "
2246 "valid\n", up_or_down);
2248 if (vc->link_status_changed)
2249 vc->link_status_changed(vc);
2254 void net_cleanup(void)
2258 /* close network clients */
2259 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
2260 VLANClientState *vc = vlan->first_client;
2263 VLANClientState *next = vc->next;
2265 qemu_del_vlan_client(vc);
2272 void net_client_check(void)
2276 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
2277 if (vlan->nb_guest_devs == 0 && vlan->nb_host_devs == 0)
2279 if (vlan->nb_guest_devs == 0)
2280 fprintf(stderr, "Warning: vlan %d with no nics\n", vlan->id);
2281 if (vlan->nb_host_devs == 0)
2283 "Warning: vlan %d is not connected to host network\n",