kvm: bios: resolve memory device roll over reporting issues with >32G guests
[qemu-kvm/fedora.git] / net.c
blobc9a87703602b93172c4e2a4989baa536ab7282ef
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 "qemu-common.h"
25 #include "net.h"
26 #include "console.h"
27 #include "sysemu.h"
28 #include "qemu-timer.h"
29 #include "qemu-char.h"
30 #include "audio/audio.h"
32 #include <unistd.h>
33 #include <fcntl.h>
34 #include <signal.h>
35 #include <time.h>
36 #include <errno.h>
37 #include <sys/time.h>
38 #include <zlib.h>
40 #ifndef _WIN32
41 #include <sys/times.h>
42 #include <sys/wait.h>
43 #include <termios.h>
44 #include <sys/mman.h>
45 #include <sys/ioctl.h>
46 #include <sys/resource.h>
47 #include <sys/socket.h>
48 #include <netinet/in.h>
49 #include <net/if.h>
50 #ifdef __NetBSD__
51 #include <net/if_tap.h>
52 #endif
53 #ifdef __linux__
54 #include <linux/if_tun.h>
55 #endif
56 #include <arpa/inet.h>
57 #include <dirent.h>
58 #include <netdb.h>
59 #include <sys/select.h>
60 #ifdef _BSD
61 #include <sys/stat.h>
62 #ifdef __FreeBSD__
63 #include <libutil.h>
64 #else
65 #include <util.h>
66 #endif
67 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
68 #include <freebsd/stdlib.h>
69 #else
70 #ifdef __linux__
71 #include <pty.h>
72 #include <malloc.h>
73 #include <linux/rtc.h>
75 /* For the benefit of older linux systems which don't supply it,
76 we use a local copy of hpet.h. */
77 /* #include <linux/hpet.h> */
78 #include "hpet.h"
80 #include <linux/ppdev.h>
81 #include <linux/parport.h>
82 #endif
83 #ifdef __sun__
84 #include <sys/stat.h>
85 #include <sys/ethernet.h>
86 #include <sys/sockio.h>
87 #include <netinet/arp.h>
88 #include <netinet/in.h>
89 #include <netinet/in_systm.h>
90 #include <netinet/ip.h>
91 #include <netinet/ip_icmp.h> // must come after ip.h
92 #include <netinet/udp.h>
93 #include <netinet/tcp.h>
94 #include <net/if.h>
95 #include <syslog.h>
96 #include <stropts.h>
97 #endif
98 #endif
99 #endif
101 #include "qemu_socket.h"
103 #if defined(CONFIG_SLIRP)
104 #include "libslirp.h"
105 #endif
107 #if defined(__OpenBSD__)
108 #include <util.h>
109 #endif
111 #if defined(CONFIG_VDE)
112 #include <libvdeplug.h>
113 #endif
115 #ifdef _WIN32
116 #include <malloc.h>
117 #include <sys/timeb.h>
118 #include <mmsystem.h>
119 #define getopt_long_only getopt_long
120 #define memalign(align, size) malloc(size)
121 #endif
123 #include "qemu-kvm.h"
125 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
126 #define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown"
127 #ifdef __sun__
128 #define SMBD_COMMAND "/usr/sfw/sbin/smbd"
129 #else
130 #define SMBD_COMMAND "/usr/sbin/smbd"
131 #endif
133 static VLANState *first_vlan;
135 /***********************************************************/
136 /* network device redirectors */
138 #if defined(DEBUG_NET) || defined(DEBUG_SLIRP)
139 static void hex_dump(FILE *f, const uint8_t *buf, int size)
141 int len, i, j, c;
143 for(i=0;i<size;i+=16) {
144 len = size - i;
145 if (len > 16)
146 len = 16;
147 fprintf(f, "%08x ", i);
148 for(j=0;j<16;j++) {
149 if (j < len)
150 fprintf(f, " %02x", buf[i+j]);
151 else
152 fprintf(f, " ");
154 fprintf(f, " ");
155 for(j=0;j<len;j++) {
156 c = buf[i+j];
157 if (c < ' ' || c > '~')
158 c = '.';
159 fprintf(f, "%c", c);
161 fprintf(f, "\n");
164 #endif
166 static int parse_macaddr(uint8_t *macaddr, const char *p)
168 int i;
169 char *last_char;
170 long int offset;
172 errno = 0;
173 offset = strtol(p, &last_char, 0);
174 if (0 == errno && '\0' == *last_char &&
175 offset >= 0 && offset <= 0xFFFFFF) {
176 macaddr[3] = (offset & 0xFF0000) >> 16;
177 macaddr[4] = (offset & 0xFF00) >> 8;
178 macaddr[5] = offset & 0xFF;
179 return 0;
180 } else {
181 for(i = 0; i < 6; i++) {
182 macaddr[i] = strtol(p, (char **)&p, 16);
183 if (i == 5) {
184 if (*p != '\0')
185 return -1;
186 } else {
187 if (*p != ':' && *p != '-')
188 return -1;
189 p++;
192 return 0;
195 return -1;
198 static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
200 const char *p, *p1;
201 int len;
202 p = *pp;
203 p1 = strchr(p, sep);
204 if (!p1)
205 return -1;
206 len = p1 - p;
207 p1++;
208 if (buf_size > 0) {
209 if (len > buf_size - 1)
210 len = buf_size - 1;
211 memcpy(buf, p, len);
212 buf[len] = '\0';
214 *pp = p1;
215 return 0;
218 int parse_host_src_port(struct sockaddr_in *haddr,
219 struct sockaddr_in *saddr,
220 const char *input_str)
222 char *str = strdup(input_str);
223 char *host_str = str;
224 char *src_str;
225 const char *src_str2;
226 char *ptr;
229 * Chop off any extra arguments at the end of the string which
230 * would start with a comma, then fill in the src port information
231 * if it was provided else use the "any address" and "any port".
233 if ((ptr = strchr(str,',')))
234 *ptr = '\0';
236 if ((src_str = strchr(input_str,'@'))) {
237 *src_str = '\0';
238 src_str++;
241 if (parse_host_port(haddr, host_str) < 0)
242 goto fail;
244 src_str2 = src_str;
245 if (!src_str || *src_str == '\0')
246 src_str2 = ":0";
248 if (parse_host_port(saddr, src_str2) < 0)
249 goto fail;
251 free(str);
252 return(0);
254 fail:
255 free(str);
256 return -1;
259 int parse_host_port(struct sockaddr_in *saddr, const char *str)
261 char buf[512];
262 struct hostent *he;
263 const char *p, *r;
264 int port;
266 p = str;
267 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
268 return -1;
269 saddr->sin_family = AF_INET;
270 if (buf[0] == '\0') {
271 saddr->sin_addr.s_addr = 0;
272 } else {
273 if (isdigit(buf[0])) {
274 if (!inet_aton(buf, &saddr->sin_addr))
275 return -1;
276 } else {
277 if ((he = gethostbyname(buf)) == NULL)
278 return - 1;
279 saddr->sin_addr = *(struct in_addr *)he->h_addr;
282 port = strtol(p, (char **)&r, 0);
283 if (r == p)
284 return -1;
285 saddr->sin_port = htons(port);
286 return 0;
289 #ifndef _WIN32
290 int parse_unix_path(struct sockaddr_un *uaddr, const char *str)
292 const char *p;
293 int len;
295 len = MIN(108, strlen(str));
296 p = strchr(str, ',');
297 if (p)
298 len = MIN(len, p - str);
300 memset(uaddr, 0, sizeof(*uaddr));
302 uaddr->sun_family = AF_UNIX;
303 memcpy(uaddr->sun_path, str, len);
305 return 0;
307 #endif
309 VLANClientState *qemu_new_vlan_client(VLANState *vlan,
310 IOReadHandler *fd_read,
311 IOCanRWHandler *fd_can_read,
312 void *opaque)
314 VLANClientState *vc, **pvc;
315 vc = qemu_mallocz(sizeof(VLANClientState));
316 if (!vc)
317 return NULL;
318 vc->fd_read = fd_read;
319 vc->fd_can_read = fd_can_read;
320 vc->opaque = opaque;
321 vc->vlan = vlan;
323 vc->next = NULL;
324 pvc = &vlan->first_client;
325 while (*pvc != NULL)
326 pvc = &(*pvc)->next;
327 *pvc = vc;
328 return vc;
331 void qemu_del_vlan_client(VLANClientState *vc)
333 VLANClientState **pvc = &vc->vlan->first_client;
335 while (*pvc != NULL)
336 if (*pvc == vc) {
337 *pvc = vc->next;
338 free(vc);
339 break;
340 } else
341 pvc = &(*pvc)->next;
344 int qemu_can_send_packet(VLANClientState *vc1)
346 VLANState *vlan = vc1->vlan;
347 VLANClientState *vc;
349 for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
350 if (vc != vc1) {
351 if (vc->fd_can_read && vc->fd_can_read(vc->opaque))
352 return 1;
355 return 0;
358 int qemu_send_packet(VLANClientState *vc1, const uint8_t *buf, int size)
360 VLANState *vlan = vc1->vlan;
361 VLANClientState *vc;
362 int ret = -EAGAIN;
364 #ifdef DEBUG_NET
365 printf("vlan %d send:\n", vlan->id);
366 hex_dump(stdout, buf, size);
367 #endif
368 for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
369 if (vc != vc1) {
370 if (!vc->fd_can_read || vc->fd_can_read(vc->opaque)) {
371 vc->fd_read(vc->opaque, buf, size);
372 ret = 0;
376 return ret;
380 static ssize_t vc_sendv_compat(VLANClientState *vc, const struct iovec *iov,
381 int iovcnt)
383 char buffer[4096];
384 size_t offset = 0;
385 int i;
387 for (i = 0; i < iovcnt; i++) {
388 size_t len;
390 len = MIN(sizeof(buffer) - offset, iov[i].iov_len);
391 memcpy(buffer + offset, iov[i].iov_base, len);
392 offset += len;
395 vc->fd_read(vc->opaque, buffer, offset);
397 return offset;
400 ssize_t qemu_sendv_packet(VLANClientState *vc1, const struct iovec *iov,
401 int iovcnt)
403 VLANState *vlan = vc1->vlan;
404 VLANClientState *vc;
405 ssize_t max_len = 0;
407 for (vc = vlan->first_client; vc != NULL; vc = vc->next) {
408 ssize_t len = 0;
410 if (vc == vc1)
411 continue;
413 if (vc->fd_readv)
414 len = vc->fd_readv(vc->opaque, iov, iovcnt);
415 else if (vc->fd_read)
416 len = vc_sendv_compat(vc, iov, iovcnt);
418 max_len = MAX(max_len, len);
421 return max_len;
424 #if defined(CONFIG_SLIRP)
426 /* slirp network adapter */
428 static int slirp_inited;
429 static VLANClientState *slirp_vc;
431 int slirp_can_output(void)
433 return !slirp_vc || qemu_can_send_packet(slirp_vc);
436 void slirp_output(const uint8_t *pkt, int pkt_len)
438 #ifdef DEBUG_SLIRP
439 printf("slirp output:\n");
440 hex_dump(stdout, pkt, pkt_len);
441 #endif
442 if (!slirp_vc)
443 return;
444 qemu_send_packet(slirp_vc, pkt, pkt_len);
447 int slirp_is_inited(void)
449 return slirp_inited;
452 static void slirp_receive(void *opaque, const uint8_t *buf, int size)
454 #ifdef DEBUG_SLIRP
455 printf("slirp input:\n");
456 hex_dump(stdout, buf, size);
457 #endif
458 slirp_input(buf, size);
461 static int net_slirp_init(VLANState *vlan)
463 if (!slirp_inited) {
464 slirp_inited = 1;
465 slirp_init();
467 slirp_vc = qemu_new_vlan_client(vlan,
468 slirp_receive, NULL, NULL);
469 snprintf(slirp_vc->info_str, sizeof(slirp_vc->info_str), "user redirector");
470 return 0;
473 void net_slirp_redir(const char *redir_str)
475 int is_udp;
476 char buf[256], *r;
477 const char *p;
478 struct in_addr guest_addr;
479 int host_port, guest_port;
481 if (!slirp_inited) {
482 slirp_inited = 1;
483 slirp_init();
486 p = redir_str;
487 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
488 goto fail;
489 if (!strcmp(buf, "tcp")) {
490 is_udp = 0;
491 } else if (!strcmp(buf, "udp")) {
492 is_udp = 1;
493 } else {
494 goto fail;
497 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
498 goto fail;
499 host_port = strtol(buf, &r, 0);
500 if (r == buf)
501 goto fail;
503 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
504 goto fail;
505 if (buf[0] == '\0') {
506 pstrcpy(buf, sizeof(buf), "10.0.2.15");
508 if (!inet_aton(buf, &guest_addr))
509 goto fail;
511 guest_port = strtol(p, &r, 0);
512 if (r == p)
513 goto fail;
515 if (slirp_redir(is_udp, host_port, guest_addr, guest_port) < 0) {
516 fprintf(stderr, "qemu: could not set up redirection\n");
517 exit(1);
519 return;
520 fail:
521 fprintf(stderr, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
522 exit(1);
525 #ifndef _WIN32
527 static char smb_dir[1024];
529 static void erase_dir(char *dir_name)
531 DIR *d;
532 struct dirent *de;
533 char filename[1024];
535 /* erase all the files in the directory */
536 if ((d = opendir(dir_name)) != 0) {
537 for(;;) {
538 de = readdir(d);
539 if (!de)
540 break;
541 if (strcmp(de->d_name, ".") != 0 &&
542 strcmp(de->d_name, "..") != 0) {
543 snprintf(filename, sizeof(filename), "%s/%s",
544 smb_dir, de->d_name);
545 if (unlink(filename) != 0) /* is it a directory? */
546 erase_dir(filename);
549 closedir(d);
550 rmdir(dir_name);
554 /* automatic user mode samba server configuration */
555 static void smb_exit(void)
557 erase_dir(smb_dir);
560 /* automatic user mode samba server configuration */
561 void net_slirp_smb(const char *exported_dir)
563 char smb_conf[1024];
564 char smb_cmdline[1024];
565 FILE *f;
567 if (!slirp_inited) {
568 slirp_inited = 1;
569 slirp_init();
572 /* XXX: better tmp dir construction */
573 snprintf(smb_dir, sizeof(smb_dir), "/tmp/qemu-smb.%d", getpid());
574 if (mkdir(smb_dir, 0700) < 0) {
575 fprintf(stderr, "qemu: could not create samba server dir '%s'\n", smb_dir);
576 exit(1);
578 snprintf(smb_conf, sizeof(smb_conf), "%s/%s", smb_dir, "smb.conf");
580 f = fopen(smb_conf, "w");
581 if (!f) {
582 fprintf(stderr, "qemu: could not create samba server configuration file '%s'\n", smb_conf);
583 exit(1);
585 fprintf(f,
586 "[global]\n"
587 "private dir=%s\n"
588 "smb ports=0\n"
589 "socket address=127.0.0.1\n"
590 "pid directory=%s\n"
591 "lock directory=%s\n"
592 "log file=%s/log.smbd\n"
593 "smb passwd file=%s/smbpasswd\n"
594 "security = share\n"
595 "[qemu]\n"
596 "path=%s\n"
597 "read only=no\n"
598 "guest ok=yes\n",
599 smb_dir,
600 smb_dir,
601 smb_dir,
602 smb_dir,
603 smb_dir,
604 exported_dir
606 fclose(f);
607 atexit(smb_exit);
609 snprintf(smb_cmdline, sizeof(smb_cmdline), "%s -s %s",
610 SMBD_COMMAND, smb_conf);
612 slirp_add_exec(0, smb_cmdline, 4, 139);
615 #endif /* !defined(_WIN32) */
616 void do_info_slirp(void)
618 slirp_stats();
621 #endif /* CONFIG_SLIRP */
623 #ifdef _WIN32
625 int tap_has_vnet_hdr(void *opaque)
627 return 0;
630 void tap_using_vnet_hdr(void *opaque, int using_vnet_hdr)
634 #else /* !defined(_WIN32) */
636 /* Maximum GSO packet size (64k) plus plenty of room for
637 * the ethernet and virtio_net headers
639 #define TAP_BUFSIZE (4096 + 65536)
641 #ifdef IFF_VNET_HDR
642 #include <linux/virtio_net.h>
643 #endif
645 typedef struct TAPState {
646 VLANClientState *vc;
647 int fd;
648 char down_script[1024];
649 char buf[TAP_BUFSIZE];
650 int size;
651 unsigned int has_vnet_hdr : 1;
652 unsigned int using_vnet_hdr : 1;
653 } TAPState;
655 static ssize_t tap_writev(void *opaque, const struct iovec *iov,
656 int iovcnt)
658 TAPState *s = opaque;
659 ssize_t len;
661 do {
662 len = writev(s->fd, iov, iovcnt);
663 } while (len == -1 && (errno == EINTR || errno == EAGAIN));
665 return len;
668 static ssize_t tap_receive_iov(void *opaque, const struct iovec *iov,
669 int iovcnt)
671 #ifdef IFF_VNET_HDR
672 TAPState *s = opaque;
674 if (s->has_vnet_hdr && !s->using_vnet_hdr) {
675 struct iovec *iov_copy;
676 struct virtio_net_hdr hdr = { 0, };
678 iov_copy = alloca(sizeof(struct iovec) * (iovcnt + 1));
680 iov_copy[0].iov_base = &hdr;
681 iov_copy[0].iov_len = sizeof(hdr);
683 memcpy(&iov_copy[1], iov, sizeof(struct iovec) * iovcnt);
685 return tap_writev(opaque, iov_copy, iovcnt + 1);
687 #endif
689 return tap_writev(opaque, iov, iovcnt);
692 static void tap_receive(void *opaque, const uint8_t *buf, int size)
694 struct iovec iov[2];
695 int i = 0;
697 #ifdef IFF_VNET_HDR
698 TAPState *s = opaque;
699 struct virtio_net_hdr hdr = { 0, };
701 if (s->has_vnet_hdr && !s->using_vnet_hdr) {
702 iov[i].iov_base = &hdr;
703 iov[i].iov_len = sizeof(hdr);
704 i++;
706 #endif
708 iov[i].iov_base = (char *) buf;
709 iov[i].iov_len = size;
710 i++;
712 tap_writev(opaque, iov, i);
715 static int tap_can_send(void *opaque)
717 TAPState *s = opaque;
718 VLANClientState *vc;
719 int can_receive = 0;
721 /* Check to see if any of our clients can receive a packet */
722 for (vc = s->vc->vlan->first_client; vc; vc = vc->next) {
723 /* Skip ourselves */
724 if (vc == s->vc)
725 continue;
727 if (!vc->fd_can_read) {
728 /* no fd_can_read handler, they always can receive */
729 can_receive = 1;
730 } else
731 can_receive = vc->fd_can_read(vc->opaque);
733 /* Once someone can receive, we try to send a packet */
734 if (can_receive)
735 break;
738 return can_receive;
741 static int tap_send_packet(TAPState *s)
743 uint8_t *buf = s->buf;
744 int size = s->size;
746 #ifdef IFF_VNET_HDR
747 if (s->has_vnet_hdr && !s->using_vnet_hdr) {
748 buf += sizeof(struct virtio_net_hdr);
749 size -= sizeof(struct virtio_net_hdr);
751 #endif
753 return qemu_send_packet(s->vc, buf, size);
756 static void tap_send(void *opaque)
758 TAPState *s = opaque;
760 /* First try to send any buffered packet */
761 if (s->size > 0) {
762 int err;
764 /* If noone can receive the packet, buffer it */
765 err = tap_send_packet(s);
766 if (err == -EAGAIN)
767 return;
770 /* Read packets until we hit EAGAIN */
771 do {
772 #ifdef __sun__
773 struct strbuf sbuf;
774 int f = 0;
775 sbuf.maxlen = sizeof(s->buf);
776 sbuf.buf = s->buf;
777 s->size = getmsg(s->fd, NULL, &sbuf, &f) >=0 ? sbuf.len : -1;
778 #else
779 kvm_sleep_begin();
780 s->size = read(s->fd, s->buf, sizeof(s->buf));
781 kvm_sleep_end();
782 #endif
784 if (s->size == -1 && errno == EINTR)
785 continue;
787 if (s->size > 0) {
788 int err;
790 /* If noone can receive the packet, buffer it */
791 err = tap_send_packet(s);
792 if (err == -EAGAIN)
793 break;
795 } while (s->size > 0);
798 int tap_has_vnet_hdr(void *opaque)
800 VLANClientState *vc = opaque;
801 TAPState *s = vc->opaque;
803 return s ? s->has_vnet_hdr : 0;
806 void tap_using_vnet_hdr(void *opaque, int using_vnet_hdr)
808 VLANClientState *vc = opaque;
809 TAPState *s = vc->opaque;
811 if (!s || !s->has_vnet_hdr)
812 return;
814 s->using_vnet_hdr = using_vnet_hdr != 0;
817 static int tap_probe_vnet_hdr(int fd)
819 #if defined(TUNGETIFF) && defined(IFF_VNET_HDR)
820 struct ifreq ifr;
822 if (ioctl(fd, TUNGETIFF, &ifr) != 0) {
823 fprintf(stderr, "TUNGETIFF ioctl() failed: %s\n", strerror(errno));
824 return 0;
827 return ifr.ifr_flags & IFF_VNET_HDR;
828 #else
829 return 0;
830 #endif
833 #ifdef TUNSETOFFLOAD
834 static void tap_set_offload(VLANClientState *vc, int csum, int tso4, int tso6,
835 int ecn)
837 TAPState *s = vc->opaque;
838 unsigned int offload = 0;
840 if (csum) {
841 offload |= TUN_F_CSUM;
842 if (tso4)
843 offload |= TUN_F_TSO4;
844 if (tso6)
845 offload |= TUN_F_TSO6;
846 if ((tso4 || tso6) && ecn)
847 offload |= TUN_F_TSO_ECN;
850 if (ioctl(s->fd, TUNSETOFFLOAD, offload) != 0)
851 fprintf(stderr, "TUNSETOFFLOAD ioctl() failed: %s\n",
852 strerror(errno));
854 #endif /* TUNSETOFFLOAD */
856 /* fd support */
858 static TAPState *net_tap_fd_init(VLANState *vlan, int fd, int vnet_hdr)
860 TAPState *s;
862 s = qemu_mallocz(sizeof(TAPState));
863 if (!s)
864 return NULL;
865 s->fd = fd;
866 s->has_vnet_hdr = vnet_hdr != 0;
867 s->vc = qemu_new_vlan_client(vlan, tap_receive, NULL, s);
868 s->vc->fd_readv = tap_receive_iov;
869 #ifdef TUNSETOFFLOAD
870 s->vc->set_offload = tap_set_offload;
871 #endif
872 qemu_set_fd_handler2(s->fd, tap_can_send, tap_send, NULL, s);
873 snprintf(s->vc->info_str, sizeof(s->vc->info_str), "tap: fd=%d", fd);
874 return s;
877 #if defined (_BSD) || defined (__FreeBSD_kernel__)
878 static int tap_open(char *ifname, int ifname_size)
880 int fd;
881 char *dev;
882 struct stat s;
884 TFR(fd = open("/dev/tap", O_RDWR));
885 if (fd < 0) {
886 fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n");
887 return -1;
890 fstat(fd, &s);
891 dev = devname(s.st_rdev, S_IFCHR);
892 pstrcpy(ifname, ifname_size, dev);
894 fcntl(fd, F_SETFL, O_NONBLOCK);
895 return fd;
897 #elif defined(__sun__)
898 #define TUNNEWPPA (('T'<<16) | 0x0001)
900 * Allocate TAP device, returns opened fd.
901 * Stores dev name in the first arg(must be large enough).
903 int tap_alloc(char *dev, size_t dev_size)
905 int tap_fd, if_fd, ppa = -1;
906 static int ip_fd = 0;
907 char *ptr;
909 static int arp_fd = 0;
910 int ip_muxid, arp_muxid;
911 struct strioctl strioc_if, strioc_ppa;
912 int link_type = I_PLINK;;
913 struct lifreq ifr;
914 char actual_name[32] = "";
916 memset(&ifr, 0x0, sizeof(ifr));
918 if( *dev ){
919 ptr = dev;
920 while( *ptr && !isdigit((int)*ptr) ) ptr++;
921 ppa = atoi(ptr);
924 /* Check if IP device was opened */
925 if( ip_fd )
926 close(ip_fd);
928 TFR(ip_fd = open("/dev/udp", O_RDWR, 0));
929 if (ip_fd < 0) {
930 syslog(LOG_ERR, "Can't open /dev/ip (actually /dev/udp)");
931 return -1;
934 TFR(tap_fd = open("/dev/tap", O_RDWR, 0));
935 if (tap_fd < 0) {
936 syslog(LOG_ERR, "Can't open /dev/tap");
937 return -1;
940 /* Assign a new PPA and get its unit number. */
941 strioc_ppa.ic_cmd = TUNNEWPPA;
942 strioc_ppa.ic_timout = 0;
943 strioc_ppa.ic_len = sizeof(ppa);
944 strioc_ppa.ic_dp = (char *)&ppa;
945 if ((ppa = ioctl (tap_fd, I_STR, &strioc_ppa)) < 0)
946 syslog (LOG_ERR, "Can't assign new interface");
948 TFR(if_fd = open("/dev/tap", O_RDWR, 0));
949 if (if_fd < 0) {
950 syslog(LOG_ERR, "Can't open /dev/tap (2)");
951 return -1;
953 if(ioctl(if_fd, I_PUSH, "ip") < 0){
954 syslog(LOG_ERR, "Can't push IP module");
955 return -1;
958 if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0)
959 syslog(LOG_ERR, "Can't get flags\n");
961 snprintf (actual_name, 32, "tap%d", ppa);
962 pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name);
964 ifr.lifr_ppa = ppa;
965 /* Assign ppa according to the unit number returned by tun device */
967 if (ioctl (if_fd, SIOCSLIFNAME, &ifr) < 0)
968 syslog (LOG_ERR, "Can't set PPA %d", ppa);
969 if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) <0)
970 syslog (LOG_ERR, "Can't get flags\n");
971 /* Push arp module to if_fd */
972 if (ioctl (if_fd, I_PUSH, "arp") < 0)
973 syslog (LOG_ERR, "Can't push ARP module (2)");
975 /* Push arp module to ip_fd */
976 if (ioctl (ip_fd, I_POP, NULL) < 0)
977 syslog (LOG_ERR, "I_POP failed\n");
978 if (ioctl (ip_fd, I_PUSH, "arp") < 0)
979 syslog (LOG_ERR, "Can't push ARP module (3)\n");
980 /* Open arp_fd */
981 TFR(arp_fd = open ("/dev/tap", O_RDWR, 0));
982 if (arp_fd < 0)
983 syslog (LOG_ERR, "Can't open %s\n", "/dev/tap");
985 /* Set ifname to arp */
986 strioc_if.ic_cmd = SIOCSLIFNAME;
987 strioc_if.ic_timout = 0;
988 strioc_if.ic_len = sizeof(ifr);
989 strioc_if.ic_dp = (char *)&ifr;
990 if (ioctl(arp_fd, I_STR, &strioc_if) < 0){
991 syslog (LOG_ERR, "Can't set ifname to arp\n");
994 if((ip_muxid = ioctl(ip_fd, I_LINK, if_fd)) < 0){
995 syslog(LOG_ERR, "Can't link TAP device to IP");
996 return -1;
999 if ((arp_muxid = ioctl (ip_fd, link_type, arp_fd)) < 0)
1000 syslog (LOG_ERR, "Can't link TAP device to ARP");
1002 close (if_fd);
1004 memset(&ifr, 0x0, sizeof(ifr));
1005 pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name);
1006 ifr.lifr_ip_muxid = ip_muxid;
1007 ifr.lifr_arp_muxid = arp_muxid;
1009 if (ioctl (ip_fd, SIOCSLIFMUXID, &ifr) < 0)
1011 ioctl (ip_fd, I_PUNLINK , arp_muxid);
1012 ioctl (ip_fd, I_PUNLINK, ip_muxid);
1013 syslog (LOG_ERR, "Can't set multiplexor id");
1016 snprintf(dev, dev_size, "tap%d", ppa);
1017 return tap_fd;
1020 static int tap_open(char *ifname, int ifname_size, int *vnet_hdr)
1022 char dev[10]="";
1023 int fd;
1024 if( (fd = tap_alloc(dev, sizeof(dev))) < 0 ){
1025 fprintf(stderr, "Cannot allocate TAP device\n");
1026 return -1;
1028 pstrcpy(ifname, ifname_size, dev);
1029 fcntl(fd, F_SETFL, O_NONBLOCK);
1030 return fd;
1032 #else
1033 static int tap_open(char *ifname, int ifname_size, int *vnet_hdr)
1035 struct ifreq ifr;
1036 int fd, ret;
1038 TFR(fd = open("/dev/net/tun", O_RDWR));
1039 if (fd < 0) {
1040 fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
1041 return -1;
1043 memset(&ifr, 0, sizeof(ifr));
1044 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
1046 #if defined(TUNGETFEATURES) && defined(IFF_VNET_HDR)
1048 unsigned int features;
1050 if (ioctl(fd, TUNGETFEATURES, &features) == 0 &&
1051 features & IFF_VNET_HDR) {
1052 *vnet_hdr = 1;
1053 ifr.ifr_flags |= IFF_VNET_HDR;
1056 #endif
1058 if (ifname[0] != '\0')
1059 pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname);
1060 else
1061 pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d");
1062 ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
1063 if (ret != 0) {
1064 fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
1065 close(fd);
1066 return -1;
1068 pstrcpy(ifname, ifname_size, ifr.ifr_name);
1069 fcntl(fd, F_SETFL, O_NONBLOCK);
1070 return fd;
1072 #endif
1074 static int launch_script(const char *setup_script, const char *ifname, int fd)
1076 int pid, status;
1077 char *args[3];
1078 char **parg;
1080 /* try to launch network script */
1081 pid = fork();
1082 if (pid >= 0) {
1083 if (pid == 0) {
1084 int open_max = sysconf (_SC_OPEN_MAX), i;
1085 for (i = 0; i < open_max; i++)
1086 if (i != STDIN_FILENO &&
1087 i != STDOUT_FILENO &&
1088 i != STDERR_FILENO &&
1089 i != fd)
1090 close(i);
1092 parg = args;
1093 *parg++ = (char *)setup_script;
1094 *parg++ = (char *)ifname;
1095 *parg++ = NULL;
1096 execv(setup_script, args);
1097 _exit(1);
1099 while (waitpid(pid, &status, 0) != pid);
1100 if (!WIFEXITED(status) ||
1101 WEXITSTATUS(status) != 0) {
1102 fprintf(stderr, "%s: could not launch network script\n",
1103 setup_script);
1104 return -1;
1107 return 0;
1110 static int net_tap_init(VLANState *vlan, const char *ifname1,
1111 const char *setup_script, const char *down_script)
1113 TAPState *s;
1114 int fd;
1115 int vnet_hdr;
1116 char ifname[128];
1118 if (ifname1 != NULL)
1119 pstrcpy(ifname, sizeof(ifname), ifname1);
1120 else
1121 ifname[0] = '\0';
1122 vnet_hdr = 0;
1123 TFR(fd = tap_open(ifname, sizeof(ifname), &vnet_hdr));
1124 if (fd < 0)
1125 return -1;
1127 if (!setup_script || !strcmp(setup_script, "no"))
1128 setup_script = "";
1129 if (setup_script[0] != '\0') {
1130 if (launch_script(setup_script, ifname, fd))
1131 return -1;
1133 s = net_tap_fd_init(vlan, fd, vnet_hdr);
1134 if (!s)
1135 return -1;
1137 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1138 "tap: ifname=%s setup_script=%s", ifname, setup_script);
1139 if (down_script && strcmp(down_script, "no"))
1140 snprintf(s->down_script, sizeof(s->down_script), "%s", down_script);
1141 return 0;
1144 #endif /* !_WIN32 */
1146 #if defined(CONFIG_VDE)
1147 typedef struct VDEState {
1148 VLANClientState *vc;
1149 VDECONN *vde;
1150 } VDEState;
1152 static void vde_to_qemu(void *opaque)
1154 VDEState *s = opaque;
1155 uint8_t buf[4096];
1156 int size;
1158 size = vde_recv(s->vde, buf, sizeof(buf), 0);
1159 if (size > 0) {
1160 qemu_send_packet(s->vc, buf, size);
1164 static void vde_from_qemu(void *opaque, const uint8_t *buf, int size)
1166 VDEState *s = opaque;
1167 int ret;
1168 for(;;) {
1169 ret = vde_send(s->vde, buf, size, 0);
1170 if (ret < 0 && errno == EINTR) {
1171 } else {
1172 break;
1177 static int net_vde_init(VLANState *vlan, const char *sock, int port,
1178 const char *group, int mode)
1180 VDEState *s;
1181 char *init_group = strlen(group) ? (char *)group : NULL;
1182 char *init_sock = strlen(sock) ? (char *)sock : NULL;
1184 struct vde_open_args args = {
1185 .port = port,
1186 .group = init_group,
1187 .mode = mode,
1190 s = qemu_mallocz(sizeof(VDEState));
1191 if (!s)
1192 return -1;
1193 s->vde = vde_open(init_sock, "QEMU", &args);
1194 if (!s->vde){
1195 free(s);
1196 return -1;
1198 s->vc = qemu_new_vlan_client(vlan, vde_from_qemu, NULL, s);
1199 qemu_set_fd_handler(vde_datafd(s->vde), vde_to_qemu, NULL, s);
1200 snprintf(s->vc->info_str, sizeof(s->vc->info_str), "vde: sock=%s fd=%d",
1201 sock, vde_datafd(s->vde));
1202 return 0;
1204 #endif
1206 /* network connection */
1207 typedef struct NetSocketState {
1208 VLANClientState *vc;
1209 int fd;
1210 int state; /* 0 = getting length, 1 = getting data */
1211 int index;
1212 int packet_len;
1213 uint8_t buf[4096];
1214 struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
1215 } NetSocketState;
1217 typedef struct NetSocketListenState {
1218 VLANState *vlan;
1219 int fd;
1220 } NetSocketListenState;
1222 /* XXX: we consider we can send the whole packet without blocking */
1223 static void net_socket_receive(void *opaque, const uint8_t *buf, int size)
1225 NetSocketState *s = opaque;
1226 uint32_t len;
1227 len = htonl(size);
1229 send_all(s->fd, (const uint8_t *)&len, sizeof(len));
1230 send_all(s->fd, buf, size);
1233 static void net_socket_receive_dgram(void *opaque, const uint8_t *buf, int size)
1235 NetSocketState *s = opaque;
1236 sendto(s->fd, buf, size, 0,
1237 (struct sockaddr *)&s->dgram_dst, sizeof(s->dgram_dst));
1240 static void net_socket_send(void *opaque)
1242 NetSocketState *s = opaque;
1243 int l, size, err;
1244 uint8_t buf1[4096];
1245 const uint8_t *buf;
1247 size = recv(s->fd, buf1, sizeof(buf1), 0);
1248 if (size < 0) {
1249 err = socket_error();
1250 if (err != EWOULDBLOCK)
1251 goto eoc;
1252 } else if (size == 0) {
1253 /* end of connection */
1254 eoc:
1255 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1256 closesocket(s->fd);
1257 return;
1259 buf = buf1;
1260 while (size > 0) {
1261 /* reassemble a packet from the network */
1262 switch(s->state) {
1263 case 0:
1264 l = 4 - s->index;
1265 if (l > size)
1266 l = size;
1267 memcpy(s->buf + s->index, buf, l);
1268 buf += l;
1269 size -= l;
1270 s->index += l;
1271 if (s->index == 4) {
1272 /* got length */
1273 s->packet_len = ntohl(*(uint32_t *)s->buf);
1274 s->index = 0;
1275 s->state = 1;
1277 break;
1278 case 1:
1279 l = s->packet_len - s->index;
1280 if (l > size)
1281 l = size;
1282 memcpy(s->buf + s->index, buf, l);
1283 s->index += l;
1284 buf += l;
1285 size -= l;
1286 if (s->index >= s->packet_len) {
1287 qemu_send_packet(s->vc, s->buf, s->packet_len);
1288 s->index = 0;
1289 s->state = 0;
1291 break;
1296 static void net_socket_send_dgram(void *opaque)
1298 NetSocketState *s = opaque;
1299 int size;
1301 size = recv(s->fd, s->buf, sizeof(s->buf), 0);
1302 if (size < 0)
1303 return;
1304 if (size == 0) {
1305 /* end of connection */
1306 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1307 return;
1309 qemu_send_packet(s->vc, s->buf, size);
1312 static int net_socket_mcast_create(struct sockaddr_in *mcastaddr)
1314 struct ip_mreq imr;
1315 int fd;
1316 int val, ret;
1317 if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) {
1318 fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
1319 inet_ntoa(mcastaddr->sin_addr),
1320 (int)ntohl(mcastaddr->sin_addr.s_addr));
1321 return -1;
1324 fd = socket(PF_INET, SOCK_DGRAM, 0);
1325 if (fd < 0) {
1326 perror("socket(PF_INET, SOCK_DGRAM)");
1327 return -1;
1330 val = 1;
1331 ret=setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
1332 (const char *)&val, sizeof(val));
1333 if (ret < 0) {
1334 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
1335 goto fail;
1338 ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr));
1339 if (ret < 0) {
1340 perror("bind");
1341 goto fail;
1344 /* Add host to multicast group */
1345 imr.imr_multiaddr = mcastaddr->sin_addr;
1346 imr.imr_interface.s_addr = htonl(INADDR_ANY);
1348 ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1349 (const char *)&imr, sizeof(struct ip_mreq));
1350 if (ret < 0) {
1351 perror("setsockopt(IP_ADD_MEMBERSHIP)");
1352 goto fail;
1355 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
1356 val = 1;
1357 ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
1358 (const char *)&val, sizeof(val));
1359 if (ret < 0) {
1360 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
1361 goto fail;
1364 socket_set_nonblock(fd);
1365 return fd;
1366 fail:
1367 if (fd >= 0)
1368 closesocket(fd);
1369 return -1;
1372 static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan, int fd,
1373 int is_connected)
1375 struct sockaddr_in saddr;
1376 int newfd;
1377 socklen_t saddr_len;
1378 NetSocketState *s;
1380 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
1381 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
1382 * by ONLY ONE process: we must "clone" this dgram socket --jjo
1385 if (is_connected) {
1386 if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) {
1387 /* must be bound */
1388 if (saddr.sin_addr.s_addr==0) {
1389 fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
1390 fd);
1391 return NULL;
1393 /* clone dgram socket */
1394 newfd = net_socket_mcast_create(&saddr);
1395 if (newfd < 0) {
1396 /* error already reported by net_socket_mcast_create() */
1397 close(fd);
1398 return NULL;
1400 /* clone newfd to fd, close newfd */
1401 dup2(newfd, fd);
1402 close(newfd);
1404 } else {
1405 fprintf(stderr, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
1406 fd, strerror(errno));
1407 return NULL;
1411 s = qemu_mallocz(sizeof(NetSocketState));
1412 if (!s)
1413 return NULL;
1414 s->fd = fd;
1416 s->vc = qemu_new_vlan_client(vlan, net_socket_receive_dgram, NULL, s);
1417 qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s);
1419 /* mcast: save bound address as dst */
1420 if (is_connected) s->dgram_dst=saddr;
1422 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1423 "socket: fd=%d (%s mcast=%s:%d)",
1424 fd, is_connected? "cloned" : "",
1425 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1426 return s;
1429 static void net_socket_connect(void *opaque)
1431 NetSocketState *s = opaque;
1432 qemu_set_fd_handler(s->fd, net_socket_send, NULL, s);
1435 static NetSocketState *net_socket_fd_init_stream(VLANState *vlan, int fd,
1436 int is_connected)
1438 NetSocketState *s;
1439 s = qemu_mallocz(sizeof(NetSocketState));
1440 if (!s)
1441 return NULL;
1442 s->fd = fd;
1443 s->vc = qemu_new_vlan_client(vlan,
1444 net_socket_receive, NULL, s);
1445 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1446 "socket: fd=%d", fd);
1447 if (is_connected) {
1448 net_socket_connect(s);
1449 } else {
1450 qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s);
1452 return s;
1455 static NetSocketState *net_socket_fd_init(VLANState *vlan, int fd,
1456 int is_connected)
1458 int so_type=-1, optlen=sizeof(so_type);
1460 if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type,
1461 (socklen_t *)&optlen)< 0) {
1462 fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd);
1463 return NULL;
1465 switch(so_type) {
1466 case SOCK_DGRAM:
1467 return net_socket_fd_init_dgram(vlan, fd, is_connected);
1468 case SOCK_STREAM:
1469 return net_socket_fd_init_stream(vlan, fd, is_connected);
1470 default:
1471 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
1472 fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd);
1473 return net_socket_fd_init_stream(vlan, fd, is_connected);
1475 return NULL;
1478 static void net_socket_accept(void *opaque)
1480 NetSocketListenState *s = opaque;
1481 NetSocketState *s1;
1482 struct sockaddr_in saddr;
1483 socklen_t len;
1484 int fd;
1486 for(;;) {
1487 len = sizeof(saddr);
1488 fd = accept(s->fd, (struct sockaddr *)&saddr, &len);
1489 if (fd < 0 && errno != EINTR) {
1490 return;
1491 } else if (fd >= 0) {
1492 break;
1495 s1 = net_socket_fd_init(s->vlan, fd, 1);
1496 if (!s1) {
1497 closesocket(fd);
1498 } else {
1499 snprintf(s1->vc->info_str, sizeof(s1->vc->info_str),
1500 "socket: connection from %s:%d",
1501 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1505 static int net_socket_listen_init(VLANState *vlan, const char *host_str)
1507 NetSocketListenState *s;
1508 int fd, val, ret;
1509 struct sockaddr_in saddr;
1511 if (parse_host_port(&saddr, host_str) < 0)
1512 return -1;
1514 s = qemu_mallocz(sizeof(NetSocketListenState));
1515 if (!s)
1516 return -1;
1518 fd = socket(PF_INET, SOCK_STREAM, 0);
1519 if (fd < 0) {
1520 perror("socket");
1521 return -1;
1523 socket_set_nonblock(fd);
1525 /* allow fast reuse */
1526 val = 1;
1527 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
1529 ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
1530 if (ret < 0) {
1531 perror("bind");
1532 return -1;
1534 ret = listen(fd, 0);
1535 if (ret < 0) {
1536 perror("listen");
1537 return -1;
1539 s->vlan = vlan;
1540 s->fd = fd;
1541 qemu_set_fd_handler(fd, net_socket_accept, NULL, s);
1542 return 0;
1545 static int net_socket_connect_init(VLANState *vlan, const char *host_str)
1547 NetSocketState *s;
1548 int fd, connected, ret, err;
1549 struct sockaddr_in saddr;
1551 if (parse_host_port(&saddr, host_str) < 0)
1552 return -1;
1554 fd = socket(PF_INET, SOCK_STREAM, 0);
1555 if (fd < 0) {
1556 perror("socket");
1557 return -1;
1559 socket_set_nonblock(fd);
1561 connected = 0;
1562 for(;;) {
1563 ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
1564 if (ret < 0) {
1565 err = socket_error();
1566 if (err == EINTR || err == EWOULDBLOCK) {
1567 } else if (err == EINPROGRESS) {
1568 break;
1569 #ifdef _WIN32
1570 } else if (err == WSAEALREADY) {
1571 break;
1572 #endif
1573 } else {
1574 perror("connect");
1575 closesocket(fd);
1576 return -1;
1578 } else {
1579 connected = 1;
1580 break;
1583 s = net_socket_fd_init(vlan, fd, connected);
1584 if (!s)
1585 return -1;
1586 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1587 "socket: connect to %s:%d",
1588 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1589 return 0;
1592 static int net_socket_mcast_init(VLANState *vlan, const char *host_str)
1594 NetSocketState *s;
1595 int fd;
1596 struct sockaddr_in saddr;
1598 if (parse_host_port(&saddr, host_str) < 0)
1599 return -1;
1602 fd = net_socket_mcast_create(&saddr);
1603 if (fd < 0)
1604 return -1;
1606 s = net_socket_fd_init(vlan, fd, 0);
1607 if (!s)
1608 return -1;
1610 s->dgram_dst = saddr;
1612 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1613 "socket: mcast=%s:%d",
1614 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1615 return 0;
1619 /* find or alloc a new VLAN */
1620 VLANState *qemu_find_vlan(int id)
1622 VLANState **pvlan, *vlan;
1623 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
1624 if (vlan->id == id)
1625 return vlan;
1627 vlan = qemu_mallocz(sizeof(VLANState));
1628 if (!vlan)
1629 return NULL;
1630 vlan->id = id;
1631 vlan->next = NULL;
1632 pvlan = &first_vlan;
1633 while (*pvlan != NULL)
1634 pvlan = &(*pvlan)->next;
1635 *pvlan = vlan;
1636 return vlan;
1639 int net_client_init(const char *device, const char *p)
1641 char buf[1024];
1642 int vlan_id, ret;
1643 VLANState *vlan;
1645 vlan_id = 0;
1646 if (get_param_value(buf, sizeof(buf), "vlan", p)) {
1647 vlan_id = strtol(buf, NULL, 0);
1649 vlan = qemu_find_vlan(vlan_id);
1650 if (!vlan) {
1651 fprintf(stderr, "Could not create vlan %d\n", vlan_id);
1652 return -1;
1654 if (!strcmp(device, "nic")) {
1655 NICInfo *nd;
1656 uint8_t *macaddr;
1658 if (nb_nics >= MAX_NICS) {
1659 fprintf(stderr, "Too Many NICs\n");
1660 return -1;
1662 nd = &nd_table[nb_nics];
1663 macaddr = nd->macaddr;
1664 macaddr[0] = 0x52;
1665 macaddr[1] = 0x54;
1666 macaddr[2] = 0x00;
1667 macaddr[3] = 0x12;
1668 macaddr[4] = 0x34;
1669 macaddr[5] = 0x56 + nb_nics;
1671 if (get_param_value(buf, sizeof(buf), "macaddr", p)) {
1672 if (parse_macaddr(macaddr, buf) < 0) {
1673 fprintf(stderr, "invalid syntax for ethernet address\n");
1674 return -1;
1677 if (get_param_value(buf, sizeof(buf), "model", p)) {
1678 nd->model = strdup(buf);
1680 nd->vlan = vlan;
1681 nb_nics++;
1682 vlan->nb_guest_devs++;
1683 ret = 0;
1684 } else
1685 if (!strcmp(device, "none")) {
1686 /* does nothing. It is needed to signal that no network cards
1687 are wanted */
1688 ret = 0;
1689 } else
1690 #ifdef CONFIG_SLIRP
1691 if (!strcmp(device, "user")) {
1692 if (get_param_value(buf, sizeof(buf), "hostname", p)) {
1693 pstrcpy(slirp_hostname, sizeof(slirp_hostname), buf);
1695 vlan->nb_host_devs++;
1696 ret = net_slirp_init(vlan);
1697 } else
1698 #endif
1699 #ifdef _WIN32
1700 if (!strcmp(device, "tap")) {
1701 char ifname[64];
1702 if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
1703 fprintf(stderr, "tap: no interface name\n");
1704 return -1;
1706 vlan->nb_host_devs++;
1707 ret = tap_win32_init(vlan, ifname);
1708 } else
1709 #else
1710 if (!strcmp(device, "tap")) {
1711 char ifname[64];
1712 char setup_script[1024], down_script[1024];
1713 int fd;
1714 vlan->nb_host_devs++;
1715 if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
1716 fd = strtol(buf, NULL, 0);
1717 fcntl(fd, F_SETFL, O_NONBLOCK);
1718 ret = -1;
1719 if (net_tap_fd_init(vlan, fd, tap_probe_vnet_hdr(fd)))
1720 ret = 0;
1721 } else {
1722 if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
1723 ifname[0] = '\0';
1725 if (get_param_value(setup_script, sizeof(setup_script), "script", p) == 0) {
1726 pstrcpy(setup_script, sizeof(setup_script), DEFAULT_NETWORK_SCRIPT);
1728 if (get_param_value(down_script, sizeof(down_script), "downscript", p) == 0) {
1729 pstrcpy(down_script, sizeof(down_script), DEFAULT_NETWORK_DOWN_SCRIPT);
1731 ret = net_tap_init(vlan, ifname, setup_script, down_script);
1733 } else
1734 #endif
1735 if (!strcmp(device, "socket")) {
1736 if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
1737 int fd;
1738 fd = strtol(buf, NULL, 0);
1739 ret = -1;
1740 if (net_socket_fd_init(vlan, fd, 1))
1741 ret = 0;
1742 } else if (get_param_value(buf, sizeof(buf), "listen", p) > 0) {
1743 ret = net_socket_listen_init(vlan, buf);
1744 } else if (get_param_value(buf, sizeof(buf), "connect", p) > 0) {
1745 ret = net_socket_connect_init(vlan, buf);
1746 } else if (get_param_value(buf, sizeof(buf), "mcast", p) > 0) {
1747 ret = net_socket_mcast_init(vlan, buf);
1748 } else {
1749 fprintf(stderr, "Unknown socket options: %s\n", p);
1750 return -1;
1752 vlan->nb_host_devs++;
1753 } else
1754 #ifdef CONFIG_VDE
1755 if (!strcmp(device, "vde")) {
1756 char vde_sock[1024], vde_group[512];
1757 int vde_port, vde_mode;
1758 vlan->nb_host_devs++;
1759 if (get_param_value(vde_sock, sizeof(vde_sock), "sock", p) <= 0) {
1760 vde_sock[0] = '\0';
1762 if (get_param_value(buf, sizeof(buf), "port", p) > 0) {
1763 vde_port = strtol(buf, NULL, 10);
1764 } else {
1765 vde_port = 0;
1767 if (get_param_value(vde_group, sizeof(vde_group), "group", p) <= 0) {
1768 vde_group[0] = '\0';
1770 if (get_param_value(buf, sizeof(buf), "mode", p) > 0) {
1771 vde_mode = strtol(buf, NULL, 8);
1772 } else {
1773 vde_mode = 0700;
1775 ret = net_vde_init(vlan, vde_sock, vde_port, vde_group, vde_mode);
1776 } else
1777 #endif
1779 fprintf(stderr, "Unknown network device: %s\n", device);
1780 return -1;
1782 if (ret < 0) {
1783 fprintf(stderr, "Could not initialize device '%s'\n", device);
1786 return ret;
1789 void net_client_uninit(NICInfo *nd)
1791 nd->vlan->nb_guest_devs--; /* XXX: free vlan on last reference */
1792 nb_nics--;
1793 nd->used = 0;
1794 free((void *)nd->model);
1797 int net_client_parse(const char *str)
1799 const char *p;
1800 char *q;
1801 char device[64];
1803 p = str;
1804 q = device;
1805 while (*p != '\0' && *p != ',') {
1806 if ((q - device) < sizeof(device) - 1)
1807 *q++ = *p;
1808 p++;
1810 *q = '\0';
1811 if (*p == ',')
1812 p++;
1814 return net_client_init(device, p);
1817 void do_info_network(void)
1819 VLANState *vlan;
1820 VLANClientState *vc;
1822 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
1823 term_printf("VLAN %d devices:\n", vlan->id);
1824 for(vc = vlan->first_client; vc != NULL; vc = vc->next)
1825 term_printf(" %s\n", vc->info_str);
1829 void net_cleanup(void)
1831 VLANState *vlan;
1833 #if !defined(_WIN32)
1834 /* close network clients */
1835 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
1836 VLANClientState *vc;
1838 for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
1839 if (vc->fd_read == tap_receive) {
1840 char ifname[64];
1841 TAPState *s = vc->opaque;
1843 if (sscanf(vc->info_str, "tap: ifname=%63s ", ifname) == 1 &&
1844 s->down_script[0])
1845 launch_script(s->down_script, ifname, s->fd);
1847 #if defined(CONFIG_VDE)
1848 if (vc->fd_read == vde_from_qemu) {
1849 VDEState *s = vc->opaque;
1850 vde_close(s->vde);
1852 #endif
1855 #endif
1858 void net_client_check(void)
1860 VLANState *vlan;
1862 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
1863 if (vlan->nb_guest_devs == 0 && vlan->nb_host_devs == 0)
1864 continue;
1865 if (vlan->nb_guest_devs == 0)
1866 fprintf(stderr, "Warning: vlan %d with no nics\n", vlan->id);
1867 if (vlan->nb_host_devs == 0)
1868 fprintf(stderr,
1869 "Warning: vlan %d is not connected to host network\n",
1870 vlan->id);