Fix "pci_add" command info descriptor
[qemu-kvm/fedora.git] / net.c
blob5ead46766d28eca59d9fab65e1fb5fd44cadb790
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 "hw/hw.h"
25 #include "hw/boards.h"
26 #include "hw/usb.h"
27 #include "hw/pcmcia.h"
28 #include "hw/pc.h"
29 #include "hw/audiodev.h"
30 #include "hw/isa.h"
31 #include "hw/baum.h"
32 #include "hw/bt.h"
33 #include "net.h"
34 #include "console.h"
35 #include "sysemu.h"
36 #include "gdbstub.h"
37 #include "qemu-timer.h"
38 #include "qemu-char.h"
39 #include "block.h"
40 #include "audio/audio.h"
41 #include "migration.h"
43 #include <unistd.h>
44 #include <fcntl.h>
45 #include <signal.h>
46 #include <time.h>
47 #include <errno.h>
48 #include <sys/time.h>
49 #include <zlib.h>
51 #ifndef _WIN32
52 #include <sys/times.h>
53 #include <sys/wait.h>
54 #include <termios.h>
55 #include <sys/mman.h>
56 #include <sys/ioctl.h>
57 #include <sys/socket.h>
58 #include <netinet/in.h>
59 #include <dirent.h>
60 #include <netdb.h>
61 #include <sys/select.h>
62 #include <arpa/inet.h>
63 #ifdef _BSD
64 #include <sys/stat.h>
65 #if !defined(__APPLE__) && !defined(__OpenBSD__)
66 #include <libutil.h>
67 #endif
68 #ifdef __OpenBSD__
69 #include <net/if.h>
70 #endif
71 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
72 #include <freebsd/stdlib.h>
73 #else
74 #ifdef __linux__
75 #include <linux/if.h>
76 #include <linux/if_tun.h>
77 #include <pty.h>
78 #include <malloc.h>
79 #include <linux/rtc.h>
81 /* For the benefit of older linux systems which don't supply it,
82 we use a local copy of hpet.h. */
83 /* #include <linux/hpet.h> */
84 #include "hpet.h"
86 #include <linux/ppdev.h>
87 #include <linux/parport.h>
88 #endif
89 #ifdef __sun__
90 #include <sys/stat.h>
91 #include <sys/ethernet.h>
92 #include <sys/sockio.h>
93 #include <netinet/arp.h>
94 #include <netinet/in.h>
95 #include <netinet/in_systm.h>
96 #include <netinet/ip.h>
97 #include <netinet/ip_icmp.h> // must come after ip.h
98 #include <netinet/udp.h>
99 #include <netinet/tcp.h>
100 #include <net/if.h>
101 #include <syslog.h>
102 #include <stropts.h>
103 #endif
104 #endif
105 #endif
107 #include "qemu_socket.h"
109 #if defined(CONFIG_SLIRP)
110 #include "libslirp.h"
111 #endif
113 #if defined(__OpenBSD__)
114 #include <util.h>
115 #endif
117 #if defined(CONFIG_VDE)
118 #include <libvdeplug.h>
119 #endif
121 #ifdef _WIN32
122 #include <malloc.h>
123 #include <sys/timeb.h>
124 #include <mmsystem.h>
125 #define getopt_long_only getopt_long
126 #define memalign(align, size) malloc(size)
127 #endif
129 #include "qemu-kvm.h"
131 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
132 #define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown"
133 #ifdef __sun__
134 #define SMBD_COMMAND "/usr/sfw/sbin/smbd"
135 #else
136 #define SMBD_COMMAND "/usr/sbin/smbd"
137 #endif
139 static VLANState *first_vlan;
141 /***********************************************************/
142 /* network device redirectors */
144 #if defined(DEBUG_NET) || defined(DEBUG_SLIRP)
145 static void hex_dump(FILE *f, const uint8_t *buf, int size)
147 int len, i, j, c;
149 for(i=0;i<size;i+=16) {
150 len = size - i;
151 if (len > 16)
152 len = 16;
153 fprintf(f, "%08x ", i);
154 for(j=0;j<16;j++) {
155 if (j < len)
156 fprintf(f, " %02x", buf[i+j]);
157 else
158 fprintf(f, " ");
160 fprintf(f, " ");
161 for(j=0;j<len;j++) {
162 c = buf[i+j];
163 if (c < ' ' || c > '~')
164 c = '.';
165 fprintf(f, "%c", c);
167 fprintf(f, "\n");
170 #endif
172 static int parse_macaddr(uint8_t *macaddr, const char *p)
174 int i;
175 char *last_char;
176 long int offset;
178 errno = 0;
179 offset = strtol(p, &last_char, 0);
180 if (0 == errno && '\0' == *last_char &&
181 offset >= 0 && offset <= 0xFFFFFF) {
182 macaddr[3] = (offset & 0xFF0000) >> 16;
183 macaddr[4] = (offset & 0xFF00) >> 8;
184 macaddr[5] = offset & 0xFF;
185 return 0;
186 } else {
187 for(i = 0; i < 6; i++) {
188 macaddr[i] = strtol(p, (char **)&p, 16);
189 if (i == 5) {
190 if (*p != '\0')
191 return -1;
192 } else {
193 if (*p != ':' && *p != '-')
194 return -1;
195 p++;
198 return 0;
201 return -1;
204 static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
206 const char *p, *p1;
207 int len;
208 p = *pp;
209 p1 = strchr(p, sep);
210 if (!p1)
211 return -1;
212 len = p1 - p;
213 p1++;
214 if (buf_size > 0) {
215 if (len > buf_size - 1)
216 len = buf_size - 1;
217 memcpy(buf, p, len);
218 buf[len] = '\0';
220 *pp = p1;
221 return 0;
224 int parse_host_src_port(struct sockaddr_in *haddr,
225 struct sockaddr_in *saddr,
226 const char *input_str)
228 char *str = strdup(input_str);
229 char *host_str = str;
230 char *src_str;
231 const char *src_str2;
232 char *ptr;
235 * Chop off any extra arguments at the end of the string which
236 * would start with a comma, then fill in the src port information
237 * if it was provided else use the "any address" and "any port".
239 if ((ptr = strchr(str,',')))
240 *ptr = '\0';
242 if ((src_str = strchr(input_str,'@'))) {
243 *src_str = '\0';
244 src_str++;
247 if (parse_host_port(haddr, host_str) < 0)
248 goto fail;
250 src_str2 = src_str;
251 if (!src_str || *src_str == '\0')
252 src_str2 = ":0";
254 if (parse_host_port(saddr, src_str2) < 0)
255 goto fail;
257 free(str);
258 return(0);
260 fail:
261 free(str);
262 return -1;
265 int parse_host_port(struct sockaddr_in *saddr, const char *str)
267 char buf[512];
268 struct hostent *he;
269 const char *p, *r;
270 int port;
272 p = str;
273 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
274 return -1;
275 saddr->sin_family = AF_INET;
276 if (buf[0] == '\0') {
277 saddr->sin_addr.s_addr = 0;
278 } else {
279 if (isdigit(buf[0])) {
280 if (!inet_aton(buf, &saddr->sin_addr))
281 return -1;
282 } else {
283 if ((he = gethostbyname(buf)) == NULL)
284 return - 1;
285 saddr->sin_addr = *(struct in_addr *)he->h_addr;
288 port = strtol(p, (char **)&r, 0);
289 if (r == p)
290 return -1;
291 saddr->sin_port = htons(port);
292 return 0;
295 #ifndef _WIN32
296 int parse_unix_path(struct sockaddr_un *uaddr, const char *str)
298 const char *p;
299 int len;
301 len = MIN(108, strlen(str));
302 p = strchr(str, ',');
303 if (p)
304 len = MIN(len, p - str);
306 memset(uaddr, 0, sizeof(*uaddr));
308 uaddr->sun_family = AF_UNIX;
309 memcpy(uaddr->sun_path, str, len);
311 return 0;
313 #endif
315 VLANClientState *qemu_new_vlan_client(VLANState *vlan,
316 IOReadHandler *fd_read,
317 IOCanRWHandler *fd_can_read,
318 void *opaque)
320 VLANClientState *vc, **pvc;
321 vc = qemu_mallocz(sizeof(VLANClientState));
322 if (!vc)
323 return NULL;
324 vc->fd_read = fd_read;
325 vc->fd_can_read = fd_can_read;
326 vc->opaque = opaque;
327 vc->vlan = vlan;
329 vc->next = NULL;
330 pvc = &vlan->first_client;
331 while (*pvc != NULL)
332 pvc = &(*pvc)->next;
333 *pvc = vc;
334 return vc;
337 void qemu_del_vlan_client(VLANClientState *vc)
339 VLANClientState **pvc = &vc->vlan->first_client;
341 while (*pvc != NULL)
342 if (*pvc == vc) {
343 *pvc = vc->next;
344 free(vc);
345 break;
346 } else
347 pvc = &(*pvc)->next;
350 int qemu_can_send_packet(VLANClientState *vc1)
352 VLANState *vlan = vc1->vlan;
353 VLANClientState *vc;
355 for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
356 if (vc != vc1) {
357 if (vc->fd_can_read && vc->fd_can_read(vc->opaque))
358 return 1;
361 return 0;
364 int qemu_send_packet(VLANClientState *vc1, const uint8_t *buf, int size)
366 VLANState *vlan = vc1->vlan;
367 VLANClientState *vc;
368 int ret = -EAGAIN;
370 #ifdef DEBUG_NET
371 printf("vlan %d send:\n", vlan->id);
372 hex_dump(stdout, buf, size);
373 #endif
374 for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
375 if (vc != vc1) {
376 if (!vc->fd_can_read || vc->fd_can_read(vc->opaque)) {
377 vc->fd_read(vc->opaque, buf, size);
378 ret = 0;
382 return ret;
386 static ssize_t vc_sendv_compat(VLANClientState *vc, const struct iovec *iov,
387 int iovcnt)
389 char buffer[4096];
390 size_t offset = 0;
391 int i;
393 for (i = 0; i < iovcnt; i++) {
394 size_t len;
396 len = MIN(sizeof(buffer) - offset, iov[i].iov_len);
397 memcpy(buffer + offset, iov[i].iov_base, len);
398 offset += len;
401 vc->fd_read(vc->opaque, buffer, offset);
403 return offset;
406 ssize_t qemu_sendv_packet(VLANClientState *vc1, const struct iovec *iov,
407 int iovcnt)
409 VLANState *vlan = vc1->vlan;
410 VLANClientState *vc;
411 ssize_t max_len = 0;
413 for (vc = vlan->first_client; vc != NULL; vc = vc->next) {
414 ssize_t len = 0;
416 if (vc == vc1)
417 continue;
419 if (vc->fd_readv)
420 len = vc->fd_readv(vc->opaque, iov, iovcnt);
421 else if (vc->fd_read)
422 len = vc_sendv_compat(vc, iov, iovcnt);
424 max_len = MAX(max_len, len);
427 return max_len;
430 #if defined(CONFIG_SLIRP)
432 /* slirp network adapter */
434 static int slirp_inited;
435 static VLANClientState *slirp_vc;
437 int slirp_can_output(void)
439 return !slirp_vc || qemu_can_send_packet(slirp_vc);
442 void slirp_output(const uint8_t *pkt, int pkt_len)
444 #ifdef DEBUG_SLIRP
445 printf("slirp output:\n");
446 hex_dump(stdout, pkt, pkt_len);
447 #endif
448 if (!slirp_vc)
449 return;
450 qemu_send_packet(slirp_vc, pkt, pkt_len);
453 int slirp_is_inited(void)
455 return slirp_inited;
458 static void slirp_receive(void *opaque, const uint8_t *buf, int size)
460 #ifdef DEBUG_SLIRP
461 printf("slirp input:\n");
462 hex_dump(stdout, buf, size);
463 #endif
464 slirp_input(buf, size);
467 static int net_slirp_init(VLANState *vlan)
469 if (!slirp_inited) {
470 slirp_inited = 1;
471 slirp_init();
473 slirp_vc = qemu_new_vlan_client(vlan,
474 slirp_receive, NULL, NULL);
475 snprintf(slirp_vc->info_str, sizeof(slirp_vc->info_str), "user redirector");
476 return 0;
479 void net_slirp_redir(const char *redir_str)
481 int is_udp;
482 char buf[256], *r;
483 const char *p;
484 struct in_addr guest_addr;
485 int host_port, guest_port;
487 if (!slirp_inited) {
488 slirp_inited = 1;
489 slirp_init();
492 p = redir_str;
493 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
494 goto fail;
495 if (!strcmp(buf, "tcp")) {
496 is_udp = 0;
497 } else if (!strcmp(buf, "udp")) {
498 is_udp = 1;
499 } else {
500 goto fail;
503 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
504 goto fail;
505 host_port = strtol(buf, &r, 0);
506 if (r == buf)
507 goto fail;
509 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
510 goto fail;
511 if (buf[0] == '\0') {
512 pstrcpy(buf, sizeof(buf), "10.0.2.15");
514 if (!inet_aton(buf, &guest_addr))
515 goto fail;
517 guest_port = strtol(p, &r, 0);
518 if (r == p)
519 goto fail;
521 if (slirp_redir(is_udp, host_port, guest_addr, guest_port) < 0) {
522 fprintf(stderr, "qemu: could not set up redirection\n");
523 exit(1);
525 return;
526 fail:
527 fprintf(stderr, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
528 exit(1);
531 #ifndef _WIN32
533 static char smb_dir[1024];
535 static void erase_dir(char *dir_name)
537 DIR *d;
538 struct dirent *de;
539 char filename[1024];
541 /* erase all the files in the directory */
542 if ((d = opendir(dir_name)) != 0) {
543 for(;;) {
544 de = readdir(d);
545 if (!de)
546 break;
547 if (strcmp(de->d_name, ".") != 0 &&
548 strcmp(de->d_name, "..") != 0) {
549 snprintf(filename, sizeof(filename), "%s/%s",
550 smb_dir, de->d_name);
551 if (unlink(filename) != 0) /* is it a directory? */
552 erase_dir(filename);
555 closedir(d);
556 rmdir(dir_name);
560 /* automatic user mode samba server configuration */
561 static void smb_exit(void)
563 erase_dir(smb_dir);
566 /* automatic user mode samba server configuration */
567 void net_slirp_smb(const char *exported_dir)
569 char smb_conf[1024];
570 char smb_cmdline[1024];
571 FILE *f;
573 if (!slirp_inited) {
574 slirp_inited = 1;
575 slirp_init();
578 /* XXX: better tmp dir construction */
579 snprintf(smb_dir, sizeof(smb_dir), "/tmp/qemu-smb.%d", getpid());
580 if (mkdir(smb_dir, 0700) < 0) {
581 fprintf(stderr, "qemu: could not create samba server dir '%s'\n", smb_dir);
582 exit(1);
584 snprintf(smb_conf, sizeof(smb_conf), "%s/%s", smb_dir, "smb.conf");
586 f = fopen(smb_conf, "w");
587 if (!f) {
588 fprintf(stderr, "qemu: could not create samba server configuration file '%s'\n", smb_conf);
589 exit(1);
591 fprintf(f,
592 "[global]\n"
593 "private dir=%s\n"
594 "smb ports=0\n"
595 "socket address=127.0.0.1\n"
596 "pid directory=%s\n"
597 "lock directory=%s\n"
598 "log file=%s/log.smbd\n"
599 "smb passwd file=%s/smbpasswd\n"
600 "security = share\n"
601 "[qemu]\n"
602 "path=%s\n"
603 "read only=no\n"
604 "guest ok=yes\n",
605 smb_dir,
606 smb_dir,
607 smb_dir,
608 smb_dir,
609 smb_dir,
610 exported_dir
612 fclose(f);
613 atexit(smb_exit);
615 snprintf(smb_cmdline, sizeof(smb_cmdline), "%s -s %s",
616 SMBD_COMMAND, smb_conf);
618 slirp_add_exec(0, smb_cmdline, 4, 139);
621 #endif /* !defined(_WIN32) */
622 void do_info_slirp(void)
624 slirp_stats();
627 #endif /* CONFIG_SLIRP */
629 #ifdef _WIN32
631 int tap_has_vnet_hdr(void *opaque)
633 return 0;
636 void tap_using_vnet_hdr(void *opaque, int using_vnet_hdr)
640 #else /* !defined(_WIN32) */
642 /* Maximum GSO packet size (64k) plus plenty of room for
643 * the ethernet and virtio_net headers
645 #define TAP_BUFSIZE (4096 + 65536)
647 #ifdef IFF_VNET_HDR
648 #include <linux/virtio_net.h>
649 #endif
651 typedef struct TAPState {
652 VLANClientState *vc;
653 int fd;
654 char down_script[1024];
655 char buf[TAP_BUFSIZE];
656 int size;
657 unsigned int has_vnet_hdr : 1;
658 unsigned int using_vnet_hdr : 1;
659 } TAPState;
661 static ssize_t tap_writev(void *opaque, const struct iovec *iov,
662 int iovcnt)
664 TAPState *s = opaque;
665 ssize_t len;
667 do {
668 len = writev(s->fd, iov, iovcnt);
669 } while (len == -1 && (errno == EINTR || errno == EAGAIN));
671 return len;
674 static ssize_t tap_receive_iov(void *opaque, const struct iovec *iov,
675 int iovcnt)
677 #ifdef IFF_VNET_HDR
678 TAPState *s = opaque;
680 if (s->has_vnet_hdr && !s->using_vnet_hdr) {
681 struct iovec *iov_copy;
682 struct virtio_net_hdr hdr = { 0, };
684 iov_copy = alloca(sizeof(struct iovec) * (iovcnt + 1));
686 iov_copy[0].iov_base = &hdr;
687 iov_copy[0].iov_len = sizeof(hdr);
689 memcpy(&iov_copy[1], iov, sizeof(struct iovec) * iovcnt);
691 return tap_writev(opaque, iov_copy, iovcnt + 1);
693 #endif
695 return tap_writev(opaque, iov, iovcnt);
698 static void tap_receive(void *opaque, const uint8_t *buf, int size)
700 struct iovec iov[2];
701 int i = 0;
703 #ifdef IFF_VNET_HDR
704 TAPState *s = opaque;
705 struct virtio_net_hdr hdr = { 0, };
707 if (s->has_vnet_hdr && !s->using_vnet_hdr) {
708 iov[i].iov_base = &hdr;
709 iov[i].iov_len = sizeof(hdr);
710 i++;
712 #endif
714 iov[i].iov_base = (char *) buf;
715 iov[i].iov_len = size;
716 i++;
718 tap_writev(opaque, iov, i);
721 static int tap_can_send(void *opaque)
723 TAPState *s = opaque;
724 VLANClientState *vc;
725 int can_receive = 0;
727 /* Check to see if any of our clients can receive a packet */
728 for (vc = s->vc->vlan->first_client; vc; vc = vc->next) {
729 /* Skip ourselves */
730 if (vc == s->vc)
731 continue;
733 if (!vc->fd_can_read) {
734 /* no fd_can_read handler, they always can receive */
735 can_receive = 1;
736 } else
737 can_receive = vc->fd_can_read(vc->opaque);
739 /* Once someone can receive, we try to send a packet */
740 if (can_receive)
741 break;
744 return can_receive;
747 static int tap_send_packet(TAPState *s)
749 uint8_t *buf = s->buf;
750 int size = s->size;
752 #ifdef IFF_VNET_HDR
753 if (s->has_vnet_hdr && !s->using_vnet_hdr) {
754 buf += sizeof(struct virtio_net_hdr);
755 size -= sizeof(struct virtio_net_hdr);
757 #endif
759 return qemu_send_packet(s->vc, buf, size);
762 static void tap_send(void *opaque)
764 TAPState *s = opaque;
766 /* First try to send any buffered packet */
767 if (s->size > 0) {
768 int err;
770 /* If noone can receive the packet, buffer it */
771 err = tap_send_packet(s);
772 if (err == -EAGAIN)
773 return;
776 /* Read packets until we hit EAGAIN */
777 do {
778 #ifdef __sun__
779 struct strbuf sbuf;
780 int f = 0;
781 sbuf.maxlen = sizeof(s->buf);
782 sbuf.buf = s->buf;
783 s->size = getmsg(s->fd, NULL, &sbuf, &f) >=0 ? sbuf.len : -1;
784 #else
785 kvm_sleep_begin();
786 s->size = read(s->fd, s->buf, sizeof(s->buf));
787 kvm_sleep_end();
788 #endif
790 if (s->size == -1 && errno == EINTR)
791 continue;
793 if (s->size > 0) {
794 int err;
796 /* If noone can receive the packet, buffer it */
797 err = tap_send_packet(s);
798 if (err == -EAGAIN)
799 break;
801 } while (s->size > 0);
804 int tap_has_vnet_hdr(void *opaque)
806 VLANClientState *vc = opaque;
807 TAPState *s = vc->opaque;
809 return s ? s->has_vnet_hdr : 0;
812 void tap_using_vnet_hdr(void *opaque, int using_vnet_hdr)
814 VLANClientState *vc = opaque;
815 TAPState *s = vc->opaque;
817 if (!s || !s->has_vnet_hdr)
818 return;
820 s->using_vnet_hdr = using_vnet_hdr != 0;
823 static int tap_probe_vnet_hdr(int fd)
825 #if defined(TUNGETIFF) && defined(IFF_VNET_HDR)
826 struct ifreq ifr;
828 if (ioctl(fd, TUNGETIFF, &ifr) != 0) {
829 fprintf(stderr, "TUNGETIFF ioctl() failed: %s\n", strerror(errno));
830 return 0;
833 return ifr.ifr_flags & IFF_VNET_HDR;
834 #else
835 return 0;
836 #endif
839 #ifdef TUNSETOFFLOAD
840 static void tap_set_offload(VLANClientState *vc, int csum, int tso4, int tso6,
841 int ecn)
843 TAPState *s = vc->opaque;
844 unsigned int offload = 0;
846 if (csum) {
847 offload |= TUN_F_CSUM;
848 if (tso4)
849 offload |= TUN_F_TSO4;
850 if (tso6)
851 offload |= TUN_F_TSO6;
852 if ((tso4 || tso6) && ecn)
853 offload |= TUN_F_TSO_ECN;
856 if (ioctl(s->fd, TUNSETOFFLOAD, offload) != 0)
857 fprintf(stderr, "TUNSETOFFLOAD ioctl() failed: %s\n",
858 strerror(errno));
860 #endif /* TUNSETOFFLOAD */
862 /* fd support */
864 static TAPState *net_tap_fd_init(VLANState *vlan, int fd, int vnet_hdr)
866 TAPState *s;
868 s = qemu_mallocz(sizeof(TAPState));
869 if (!s)
870 return NULL;
871 s->fd = fd;
872 s->has_vnet_hdr = vnet_hdr != 0;
873 s->vc = qemu_new_vlan_client(vlan, tap_receive, NULL, s);
874 s->vc->fd_readv = tap_receive_iov;
875 #ifdef TUNSETOFFLOAD
876 s->vc->set_offload = tap_set_offload;
877 #endif
878 qemu_set_fd_handler2(s->fd, tap_can_send, tap_send, NULL, s);
879 snprintf(s->vc->info_str, sizeof(s->vc->info_str), "tap: fd=%d", fd);
880 return s;
883 #if defined (_BSD) || defined (__FreeBSD_kernel__)
884 static int tap_open(char *ifname, int ifname_size)
886 int fd;
887 char *dev;
888 struct stat s;
890 TFR(fd = open("/dev/tap", O_RDWR));
891 if (fd < 0) {
892 fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n");
893 return -1;
896 fstat(fd, &s);
897 dev = devname(s.st_rdev, S_IFCHR);
898 pstrcpy(ifname, ifname_size, dev);
900 fcntl(fd, F_SETFL, O_NONBLOCK);
901 return fd;
903 #elif defined(__sun__)
904 #define TUNNEWPPA (('T'<<16) | 0x0001)
906 * Allocate TAP device, returns opened fd.
907 * Stores dev name in the first arg(must be large enough).
909 int tap_alloc(char *dev, size_t dev_size)
911 int tap_fd, if_fd, ppa = -1;
912 static int ip_fd = 0;
913 char *ptr;
915 static int arp_fd = 0;
916 int ip_muxid, arp_muxid;
917 struct strioctl strioc_if, strioc_ppa;
918 int link_type = I_PLINK;;
919 struct lifreq ifr;
920 char actual_name[32] = "";
922 memset(&ifr, 0x0, sizeof(ifr));
924 if( *dev ){
925 ptr = dev;
926 while( *ptr && !isdigit((int)*ptr) ) ptr++;
927 ppa = atoi(ptr);
930 /* Check if IP device was opened */
931 if( ip_fd )
932 close(ip_fd);
934 TFR(ip_fd = open("/dev/udp", O_RDWR, 0));
935 if (ip_fd < 0) {
936 syslog(LOG_ERR, "Can't open /dev/ip (actually /dev/udp)");
937 return -1;
940 TFR(tap_fd = open("/dev/tap", O_RDWR, 0));
941 if (tap_fd < 0) {
942 syslog(LOG_ERR, "Can't open /dev/tap");
943 return -1;
946 /* Assign a new PPA and get its unit number. */
947 strioc_ppa.ic_cmd = TUNNEWPPA;
948 strioc_ppa.ic_timout = 0;
949 strioc_ppa.ic_len = sizeof(ppa);
950 strioc_ppa.ic_dp = (char *)&ppa;
951 if ((ppa = ioctl (tap_fd, I_STR, &strioc_ppa)) < 0)
952 syslog (LOG_ERR, "Can't assign new interface");
954 TFR(if_fd = open("/dev/tap", O_RDWR, 0));
955 if (if_fd < 0) {
956 syslog(LOG_ERR, "Can't open /dev/tap (2)");
957 return -1;
959 if(ioctl(if_fd, I_PUSH, "ip") < 0){
960 syslog(LOG_ERR, "Can't push IP module");
961 return -1;
964 if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0)
965 syslog(LOG_ERR, "Can't get flags\n");
967 snprintf (actual_name, 32, "tap%d", ppa);
968 pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name);
970 ifr.lifr_ppa = ppa;
971 /* Assign ppa according to the unit number returned by tun device */
973 if (ioctl (if_fd, SIOCSLIFNAME, &ifr) < 0)
974 syslog (LOG_ERR, "Can't set PPA %d", ppa);
975 if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) <0)
976 syslog (LOG_ERR, "Can't get flags\n");
977 /* Push arp module to if_fd */
978 if (ioctl (if_fd, I_PUSH, "arp") < 0)
979 syslog (LOG_ERR, "Can't push ARP module (2)");
981 /* Push arp module to ip_fd */
982 if (ioctl (ip_fd, I_POP, NULL) < 0)
983 syslog (LOG_ERR, "I_POP failed\n");
984 if (ioctl (ip_fd, I_PUSH, "arp") < 0)
985 syslog (LOG_ERR, "Can't push ARP module (3)\n");
986 /* Open arp_fd */
987 TFR(arp_fd = open ("/dev/tap", O_RDWR, 0));
988 if (arp_fd < 0)
989 syslog (LOG_ERR, "Can't open %s\n", "/dev/tap");
991 /* Set ifname to arp */
992 strioc_if.ic_cmd = SIOCSLIFNAME;
993 strioc_if.ic_timout = 0;
994 strioc_if.ic_len = sizeof(ifr);
995 strioc_if.ic_dp = (char *)&ifr;
996 if (ioctl(arp_fd, I_STR, &strioc_if) < 0){
997 syslog (LOG_ERR, "Can't set ifname to arp\n");
1000 if((ip_muxid = ioctl(ip_fd, I_LINK, if_fd)) < 0){
1001 syslog(LOG_ERR, "Can't link TAP device to IP");
1002 return -1;
1005 if ((arp_muxid = ioctl (ip_fd, link_type, arp_fd)) < 0)
1006 syslog (LOG_ERR, "Can't link TAP device to ARP");
1008 close (if_fd);
1010 memset(&ifr, 0x0, sizeof(ifr));
1011 pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name);
1012 ifr.lifr_ip_muxid = ip_muxid;
1013 ifr.lifr_arp_muxid = arp_muxid;
1015 if (ioctl (ip_fd, SIOCSLIFMUXID, &ifr) < 0)
1017 ioctl (ip_fd, I_PUNLINK , arp_muxid);
1018 ioctl (ip_fd, I_PUNLINK, ip_muxid);
1019 syslog (LOG_ERR, "Can't set multiplexor id");
1022 snprintf(dev, dev_size, "tap%d", ppa);
1023 return tap_fd;
1026 static int tap_open(char *ifname, int ifname_size, int *vnet_hdr)
1028 char dev[10]="";
1029 int fd;
1030 if( (fd = tap_alloc(dev, sizeof(dev))) < 0 ){
1031 fprintf(stderr, "Cannot allocate TAP device\n");
1032 return -1;
1034 pstrcpy(ifname, ifname_size, dev);
1035 fcntl(fd, F_SETFL, O_NONBLOCK);
1036 return fd;
1038 #else
1039 static int tap_open(char *ifname, int ifname_size, int *vnet_hdr)
1041 struct ifreq ifr;
1042 int fd, ret;
1044 TFR(fd = open("/dev/net/tun", O_RDWR));
1045 if (fd < 0) {
1046 fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
1047 return -1;
1049 memset(&ifr, 0, sizeof(ifr));
1050 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
1052 #if defined(TUNGETFEATURES) && defined(IFF_VNET_HDR)
1054 unsigned int features;
1056 if (ioctl(fd, TUNGETFEATURES, &features) == 0 &&
1057 features & IFF_VNET_HDR) {
1058 *vnet_hdr = 1;
1059 ifr.ifr_flags |= IFF_VNET_HDR;
1062 #endif
1064 if (ifname[0] != '\0')
1065 pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname);
1066 else
1067 pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d");
1068 ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
1069 if (ret != 0) {
1070 fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
1071 close(fd);
1072 return -1;
1074 pstrcpy(ifname, ifname_size, ifr.ifr_name);
1075 fcntl(fd, F_SETFL, O_NONBLOCK);
1076 return fd;
1078 #endif
1080 static int launch_script(const char *setup_script, const char *ifname, int fd)
1082 int pid, status;
1083 char *args[3];
1084 char **parg;
1086 /* try to launch network script */
1087 pid = fork();
1088 if (pid >= 0) {
1089 if (pid == 0) {
1090 int open_max = sysconf (_SC_OPEN_MAX), i;
1091 for (i = 0; i < open_max; i++)
1092 if (i != STDIN_FILENO &&
1093 i != STDOUT_FILENO &&
1094 i != STDERR_FILENO &&
1095 i != fd)
1096 close(i);
1098 parg = args;
1099 *parg++ = (char *)setup_script;
1100 *parg++ = (char *)ifname;
1101 *parg++ = NULL;
1102 execv(setup_script, args);
1103 _exit(1);
1105 while (waitpid(pid, &status, 0) != pid);
1106 if (!WIFEXITED(status) ||
1107 WEXITSTATUS(status) != 0) {
1108 fprintf(stderr, "%s: could not launch network script\n",
1109 setup_script);
1110 return -1;
1113 return 0;
1116 static int net_tap_init(VLANState *vlan, const char *ifname1,
1117 const char *setup_script, const char *down_script)
1119 TAPState *s;
1120 int fd;
1121 int vnet_hdr;
1122 char ifname[128];
1124 if (ifname1 != NULL)
1125 pstrcpy(ifname, sizeof(ifname), ifname1);
1126 else
1127 ifname[0] = '\0';
1128 vnet_hdr = 0;
1129 TFR(fd = tap_open(ifname, sizeof(ifname), &vnet_hdr));
1130 if (fd < 0)
1131 return -1;
1133 if (!setup_script || !strcmp(setup_script, "no"))
1134 setup_script = "";
1135 if (setup_script[0] != '\0') {
1136 if (launch_script(setup_script, ifname, fd))
1137 return -1;
1139 s = net_tap_fd_init(vlan, fd, vnet_hdr);
1140 if (!s)
1141 return -1;
1143 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1144 "tap: ifname=%s setup_script=%s", ifname, setup_script);
1145 if (down_script && strcmp(down_script, "no"))
1146 snprintf(s->down_script, sizeof(s->down_script), "%s", down_script);
1147 return 0;
1150 #endif /* !_WIN32 */
1152 #if defined(CONFIG_VDE)
1153 typedef struct VDEState {
1154 VLANClientState *vc;
1155 VDECONN *vde;
1156 } VDEState;
1158 static void vde_to_qemu(void *opaque)
1160 VDEState *s = opaque;
1161 uint8_t buf[4096];
1162 int size;
1164 size = vde_recv(s->vde, buf, sizeof(buf), 0);
1165 if (size > 0) {
1166 qemu_send_packet(s->vc, buf, size);
1170 static void vde_from_qemu(void *opaque, const uint8_t *buf, int size)
1172 VDEState *s = opaque;
1173 int ret;
1174 for(;;) {
1175 ret = vde_send(s->vde, buf, size, 0);
1176 if (ret < 0 && errno == EINTR) {
1177 } else {
1178 break;
1183 static int net_vde_init(VLANState *vlan, const char *sock, int port,
1184 const char *group, int mode)
1186 VDEState *s;
1187 char *init_group = strlen(group) ? (char *)group : NULL;
1188 char *init_sock = strlen(sock) ? (char *)sock : NULL;
1190 struct vde_open_args args = {
1191 .port = port,
1192 .group = init_group,
1193 .mode = mode,
1196 s = qemu_mallocz(sizeof(VDEState));
1197 if (!s)
1198 return -1;
1199 s->vde = vde_open(init_sock, "QEMU", &args);
1200 if (!s->vde){
1201 free(s);
1202 return -1;
1204 s->vc = qemu_new_vlan_client(vlan, vde_from_qemu, NULL, s);
1205 qemu_set_fd_handler(vde_datafd(s->vde), vde_to_qemu, NULL, s);
1206 snprintf(s->vc->info_str, sizeof(s->vc->info_str), "vde: sock=%s fd=%d",
1207 sock, vde_datafd(s->vde));
1208 return 0;
1210 #endif
1212 /* network connection */
1213 typedef struct NetSocketState {
1214 VLANClientState *vc;
1215 int fd;
1216 int state; /* 0 = getting length, 1 = getting data */
1217 int index;
1218 int packet_len;
1219 uint8_t buf[4096];
1220 struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
1221 } NetSocketState;
1223 typedef struct NetSocketListenState {
1224 VLANState *vlan;
1225 int fd;
1226 } NetSocketListenState;
1228 /* XXX: we consider we can send the whole packet without blocking */
1229 static void net_socket_receive(void *opaque, const uint8_t *buf, int size)
1231 NetSocketState *s = opaque;
1232 uint32_t len;
1233 len = htonl(size);
1235 send_all(s->fd, (const uint8_t *)&len, sizeof(len));
1236 send_all(s->fd, buf, size);
1239 static void net_socket_receive_dgram(void *opaque, const uint8_t *buf, int size)
1241 NetSocketState *s = opaque;
1242 sendto(s->fd, buf, size, 0,
1243 (struct sockaddr *)&s->dgram_dst, sizeof(s->dgram_dst));
1246 static void net_socket_send(void *opaque)
1248 NetSocketState *s = opaque;
1249 int l, size, err;
1250 uint8_t buf1[4096];
1251 const uint8_t *buf;
1253 size = recv(s->fd, buf1, sizeof(buf1), 0);
1254 if (size < 0) {
1255 err = socket_error();
1256 if (err != EWOULDBLOCK)
1257 goto eoc;
1258 } else if (size == 0) {
1259 /* end of connection */
1260 eoc:
1261 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1262 closesocket(s->fd);
1263 return;
1265 buf = buf1;
1266 while (size > 0) {
1267 /* reassemble a packet from the network */
1268 switch(s->state) {
1269 case 0:
1270 l = 4 - s->index;
1271 if (l > size)
1272 l = size;
1273 memcpy(s->buf + s->index, buf, l);
1274 buf += l;
1275 size -= l;
1276 s->index += l;
1277 if (s->index == 4) {
1278 /* got length */
1279 s->packet_len = ntohl(*(uint32_t *)s->buf);
1280 s->index = 0;
1281 s->state = 1;
1283 break;
1284 case 1:
1285 l = s->packet_len - s->index;
1286 if (l > size)
1287 l = size;
1288 memcpy(s->buf + s->index, buf, l);
1289 s->index += l;
1290 buf += l;
1291 size -= l;
1292 if (s->index >= s->packet_len) {
1293 qemu_send_packet(s->vc, s->buf, s->packet_len);
1294 s->index = 0;
1295 s->state = 0;
1297 break;
1302 static void net_socket_send_dgram(void *opaque)
1304 NetSocketState *s = opaque;
1305 int size;
1307 size = recv(s->fd, s->buf, sizeof(s->buf), 0);
1308 if (size < 0)
1309 return;
1310 if (size == 0) {
1311 /* end of connection */
1312 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1313 return;
1315 qemu_send_packet(s->vc, s->buf, size);
1318 static int net_socket_mcast_create(struct sockaddr_in *mcastaddr)
1320 struct ip_mreq imr;
1321 int fd;
1322 int val, ret;
1323 if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) {
1324 fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
1325 inet_ntoa(mcastaddr->sin_addr),
1326 (int)ntohl(mcastaddr->sin_addr.s_addr));
1327 return -1;
1330 fd = socket(PF_INET, SOCK_DGRAM, 0);
1331 if (fd < 0) {
1332 perror("socket(PF_INET, SOCK_DGRAM)");
1333 return -1;
1336 val = 1;
1337 ret=setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
1338 (const char *)&val, sizeof(val));
1339 if (ret < 0) {
1340 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
1341 goto fail;
1344 ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr));
1345 if (ret < 0) {
1346 perror("bind");
1347 goto fail;
1350 /* Add host to multicast group */
1351 imr.imr_multiaddr = mcastaddr->sin_addr;
1352 imr.imr_interface.s_addr = htonl(INADDR_ANY);
1354 ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1355 (const char *)&imr, sizeof(struct ip_mreq));
1356 if (ret < 0) {
1357 perror("setsockopt(IP_ADD_MEMBERSHIP)");
1358 goto fail;
1361 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
1362 val = 1;
1363 ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
1364 (const char *)&val, sizeof(val));
1365 if (ret < 0) {
1366 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
1367 goto fail;
1370 socket_set_nonblock(fd);
1371 return fd;
1372 fail:
1373 if (fd >= 0)
1374 closesocket(fd);
1375 return -1;
1378 static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan, int fd,
1379 int is_connected)
1381 struct sockaddr_in saddr;
1382 int newfd;
1383 socklen_t saddr_len;
1384 NetSocketState *s;
1386 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
1387 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
1388 * by ONLY ONE process: we must "clone" this dgram socket --jjo
1391 if (is_connected) {
1392 if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) {
1393 /* must be bound */
1394 if (saddr.sin_addr.s_addr==0) {
1395 fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
1396 fd);
1397 return NULL;
1399 /* clone dgram socket */
1400 newfd = net_socket_mcast_create(&saddr);
1401 if (newfd < 0) {
1402 /* error already reported by net_socket_mcast_create() */
1403 close(fd);
1404 return NULL;
1406 /* clone newfd to fd, close newfd */
1407 dup2(newfd, fd);
1408 close(newfd);
1410 } else {
1411 fprintf(stderr, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
1412 fd, strerror(errno));
1413 return NULL;
1417 s = qemu_mallocz(sizeof(NetSocketState));
1418 if (!s)
1419 return NULL;
1420 s->fd = fd;
1422 s->vc = qemu_new_vlan_client(vlan, net_socket_receive_dgram, NULL, s);
1423 qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s);
1425 /* mcast: save bound address as dst */
1426 if (is_connected) s->dgram_dst=saddr;
1428 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1429 "socket: fd=%d (%s mcast=%s:%d)",
1430 fd, is_connected? "cloned" : "",
1431 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1432 return s;
1435 static void net_socket_connect(void *opaque)
1437 NetSocketState *s = opaque;
1438 qemu_set_fd_handler(s->fd, net_socket_send, NULL, s);
1441 static NetSocketState *net_socket_fd_init_stream(VLANState *vlan, int fd,
1442 int is_connected)
1444 NetSocketState *s;
1445 s = qemu_mallocz(sizeof(NetSocketState));
1446 if (!s)
1447 return NULL;
1448 s->fd = fd;
1449 s->vc = qemu_new_vlan_client(vlan,
1450 net_socket_receive, NULL, s);
1451 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1452 "socket: fd=%d", fd);
1453 if (is_connected) {
1454 net_socket_connect(s);
1455 } else {
1456 qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s);
1458 return s;
1461 static NetSocketState *net_socket_fd_init(VLANState *vlan, int fd,
1462 int is_connected)
1464 int so_type=-1, optlen=sizeof(so_type);
1466 if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type,
1467 (socklen_t *)&optlen)< 0) {
1468 fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd);
1469 return NULL;
1471 switch(so_type) {
1472 case SOCK_DGRAM:
1473 return net_socket_fd_init_dgram(vlan, fd, is_connected);
1474 case SOCK_STREAM:
1475 return net_socket_fd_init_stream(vlan, fd, is_connected);
1476 default:
1477 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
1478 fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd);
1479 return net_socket_fd_init_stream(vlan, fd, is_connected);
1481 return NULL;
1484 static void net_socket_accept(void *opaque)
1486 NetSocketListenState *s = opaque;
1487 NetSocketState *s1;
1488 struct sockaddr_in saddr;
1489 socklen_t len;
1490 int fd;
1492 for(;;) {
1493 len = sizeof(saddr);
1494 fd = accept(s->fd, (struct sockaddr *)&saddr, &len);
1495 if (fd < 0 && errno != EINTR) {
1496 return;
1497 } else if (fd >= 0) {
1498 break;
1501 s1 = net_socket_fd_init(s->vlan, fd, 1);
1502 if (!s1) {
1503 closesocket(fd);
1504 } else {
1505 snprintf(s1->vc->info_str, sizeof(s1->vc->info_str),
1506 "socket: connection from %s:%d",
1507 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1511 static int net_socket_listen_init(VLANState *vlan, const char *host_str)
1513 NetSocketListenState *s;
1514 int fd, val, ret;
1515 struct sockaddr_in saddr;
1517 if (parse_host_port(&saddr, host_str) < 0)
1518 return -1;
1520 s = qemu_mallocz(sizeof(NetSocketListenState));
1521 if (!s)
1522 return -1;
1524 fd = socket(PF_INET, SOCK_STREAM, 0);
1525 if (fd < 0) {
1526 perror("socket");
1527 return -1;
1529 socket_set_nonblock(fd);
1531 /* allow fast reuse */
1532 val = 1;
1533 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
1535 ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
1536 if (ret < 0) {
1537 perror("bind");
1538 return -1;
1540 ret = listen(fd, 0);
1541 if (ret < 0) {
1542 perror("listen");
1543 return -1;
1545 s->vlan = vlan;
1546 s->fd = fd;
1547 qemu_set_fd_handler(fd, net_socket_accept, NULL, s);
1548 return 0;
1551 static int net_socket_connect_init(VLANState *vlan, const char *host_str)
1553 NetSocketState *s;
1554 int fd, connected, ret, err;
1555 struct sockaddr_in saddr;
1557 if (parse_host_port(&saddr, host_str) < 0)
1558 return -1;
1560 fd = socket(PF_INET, SOCK_STREAM, 0);
1561 if (fd < 0) {
1562 perror("socket");
1563 return -1;
1565 socket_set_nonblock(fd);
1567 connected = 0;
1568 for(;;) {
1569 ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
1570 if (ret < 0) {
1571 err = socket_error();
1572 if (err == EINTR || err == EWOULDBLOCK) {
1573 } else if (err == EINPROGRESS) {
1574 break;
1575 #ifdef _WIN32
1576 } else if (err == WSAEALREADY) {
1577 break;
1578 #endif
1579 } else {
1580 perror("connect");
1581 closesocket(fd);
1582 return -1;
1584 } else {
1585 connected = 1;
1586 break;
1589 s = net_socket_fd_init(vlan, fd, connected);
1590 if (!s)
1591 return -1;
1592 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1593 "socket: connect to %s:%d",
1594 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1595 return 0;
1598 static int net_socket_mcast_init(VLANState *vlan, const char *host_str)
1600 NetSocketState *s;
1601 int fd;
1602 struct sockaddr_in saddr;
1604 if (parse_host_port(&saddr, host_str) < 0)
1605 return -1;
1608 fd = net_socket_mcast_create(&saddr);
1609 if (fd < 0)
1610 return -1;
1612 s = net_socket_fd_init(vlan, fd, 0);
1613 if (!s)
1614 return -1;
1616 s->dgram_dst = saddr;
1618 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1619 "socket: mcast=%s:%d",
1620 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1621 return 0;
1625 /* find or alloc a new VLAN */
1626 VLANState *qemu_find_vlan(int id)
1628 VLANState **pvlan, *vlan;
1629 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
1630 if (vlan->id == id)
1631 return vlan;
1633 vlan = qemu_mallocz(sizeof(VLANState));
1634 if (!vlan)
1635 return NULL;
1636 vlan->id = id;
1637 vlan->next = NULL;
1638 pvlan = &first_vlan;
1639 while (*pvlan != NULL)
1640 pvlan = &(*pvlan)->next;
1641 *pvlan = vlan;
1642 return vlan;
1645 int net_client_init(const char *device, const char *p)
1647 char buf[1024];
1648 int vlan_id, ret;
1649 VLANState *vlan;
1651 vlan_id = 0;
1652 if (get_param_value(buf, sizeof(buf), "vlan", p)) {
1653 vlan_id = strtol(buf, NULL, 0);
1655 vlan = qemu_find_vlan(vlan_id);
1656 if (!vlan) {
1657 fprintf(stderr, "Could not create vlan %d\n", vlan_id);
1658 return -1;
1660 if (!strcmp(device, "nic")) {
1661 NICInfo *nd;
1662 uint8_t *macaddr;
1664 if (nb_nics >= MAX_NICS) {
1665 fprintf(stderr, "Too Many NICs\n");
1666 return -1;
1668 nd = &nd_table[nb_nics];
1669 macaddr = nd->macaddr;
1670 macaddr[0] = 0x52;
1671 macaddr[1] = 0x54;
1672 macaddr[2] = 0x00;
1673 macaddr[3] = 0x12;
1674 macaddr[4] = 0x34;
1675 macaddr[5] = 0x56 + nb_nics;
1677 if (get_param_value(buf, sizeof(buf), "macaddr", p)) {
1678 if (parse_macaddr(macaddr, buf) < 0) {
1679 fprintf(stderr, "invalid syntax for ethernet address\n");
1680 return -1;
1683 if (get_param_value(buf, sizeof(buf), "model", p)) {
1684 nd->model = strdup(buf);
1686 nd->vlan = vlan;
1687 nb_nics++;
1688 vlan->nb_guest_devs++;
1689 ret = 0;
1690 } else
1691 if (!strcmp(device, "none")) {
1692 /* does nothing. It is needed to signal that no network cards
1693 are wanted */
1694 ret = 0;
1695 } else
1696 #ifdef CONFIG_SLIRP
1697 if (!strcmp(device, "user")) {
1698 if (get_param_value(buf, sizeof(buf), "hostname", p)) {
1699 pstrcpy(slirp_hostname, sizeof(slirp_hostname), buf);
1701 vlan->nb_host_devs++;
1702 ret = net_slirp_init(vlan);
1703 } else
1704 #endif
1705 #ifdef _WIN32
1706 if (!strcmp(device, "tap")) {
1707 char ifname[64];
1708 if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
1709 fprintf(stderr, "tap: no interface name\n");
1710 return -1;
1712 vlan->nb_host_devs++;
1713 ret = tap_win32_init(vlan, ifname);
1714 } else
1715 #else
1716 if (!strcmp(device, "tap")) {
1717 char ifname[64];
1718 char setup_script[1024], down_script[1024];
1719 int fd;
1720 vlan->nb_host_devs++;
1721 if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
1722 fd = strtol(buf, NULL, 0);
1723 fcntl(fd, F_SETFL, O_NONBLOCK);
1724 ret = -1;
1725 if (net_tap_fd_init(vlan, fd, tap_probe_vnet_hdr(fd)))
1726 ret = 0;
1727 } else {
1728 if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
1729 ifname[0] = '\0';
1731 if (get_param_value(setup_script, sizeof(setup_script), "script", p) == 0) {
1732 pstrcpy(setup_script, sizeof(setup_script), DEFAULT_NETWORK_SCRIPT);
1734 if (get_param_value(down_script, sizeof(down_script), "downscript", p) == 0) {
1735 pstrcpy(down_script, sizeof(down_script), DEFAULT_NETWORK_DOWN_SCRIPT);
1737 ret = net_tap_init(vlan, ifname, setup_script, down_script);
1739 } else
1740 #endif
1741 if (!strcmp(device, "socket")) {
1742 if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
1743 int fd;
1744 fd = strtol(buf, NULL, 0);
1745 ret = -1;
1746 if (net_socket_fd_init(vlan, fd, 1))
1747 ret = 0;
1748 } else if (get_param_value(buf, sizeof(buf), "listen", p) > 0) {
1749 ret = net_socket_listen_init(vlan, buf);
1750 } else if (get_param_value(buf, sizeof(buf), "connect", p) > 0) {
1751 ret = net_socket_connect_init(vlan, buf);
1752 } else if (get_param_value(buf, sizeof(buf), "mcast", p) > 0) {
1753 ret = net_socket_mcast_init(vlan, buf);
1754 } else {
1755 fprintf(stderr, "Unknown socket options: %s\n", p);
1756 return -1;
1758 vlan->nb_host_devs++;
1759 } else
1760 #ifdef CONFIG_VDE
1761 if (!strcmp(device, "vde")) {
1762 char vde_sock[1024], vde_group[512];
1763 int vde_port, vde_mode;
1764 vlan->nb_host_devs++;
1765 if (get_param_value(vde_sock, sizeof(vde_sock), "sock", p) <= 0) {
1766 vde_sock[0] = '\0';
1768 if (get_param_value(buf, sizeof(buf), "port", p) > 0) {
1769 vde_port = strtol(buf, NULL, 10);
1770 } else {
1771 vde_port = 0;
1773 if (get_param_value(vde_group, sizeof(vde_group), "group", p) <= 0) {
1774 vde_group[0] = '\0';
1776 if (get_param_value(buf, sizeof(buf), "mode", p) > 0) {
1777 vde_mode = strtol(buf, NULL, 8);
1778 } else {
1779 vde_mode = 0700;
1781 ret = net_vde_init(vlan, vde_sock, vde_port, vde_group, vde_mode);
1782 } else
1783 #endif
1785 fprintf(stderr, "Unknown network device: %s\n", device);
1786 return -1;
1788 if (ret < 0) {
1789 fprintf(stderr, "Could not initialize device '%s'\n", device);
1792 return ret;
1795 void net_client_uninit(NICInfo *nd)
1797 nd->vlan->nb_guest_devs--; /* XXX: free vlan on last reference */
1798 nb_nics--;
1799 nd->used = 0;
1800 free((void *)nd->model);
1803 int net_client_parse(const char *str)
1805 const char *p;
1806 char *q;
1807 char device[64];
1809 p = str;
1810 q = device;
1811 while (*p != '\0' && *p != ',') {
1812 if ((q - device) < sizeof(device) - 1)
1813 *q++ = *p;
1814 p++;
1816 *q = '\0';
1817 if (*p == ',')
1818 p++;
1820 return net_client_init(device, p);
1823 void do_info_network(void)
1825 VLANState *vlan;
1826 VLANClientState *vc;
1828 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
1829 term_printf("VLAN %d devices:\n", vlan->id);
1830 for(vc = vlan->first_client; vc != NULL; vc = vc->next)
1831 term_printf(" %s\n", vc->info_str);
1835 void net_cleanup(void)
1837 VLANState *vlan;
1839 #if !defined(_WIN32)
1840 /* close network clients */
1841 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
1842 VLANClientState *vc;
1844 for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
1845 if (vc->fd_read == tap_receive) {
1846 char ifname[64];
1847 TAPState *s = vc->opaque;
1849 if (sscanf(vc->info_str, "tap: ifname=%63s ", ifname) == 1 &&
1850 s->down_script[0])
1851 launch_script(s->down_script, ifname, s->fd);
1853 #if defined(CONFIG_VDE)
1854 if (vc->fd_read == vde_from_qemu) {
1855 VDEState *s = vc->opaque;
1856 vde_close(s->vde);
1858 #endif
1861 #endif
1864 void net_client_check(void)
1866 VLANState *vlan;
1868 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
1869 if (vlan->nb_guest_devs == 0 && vlan->nb_host_devs == 0)
1870 continue;
1871 if (vlan->nb_guest_devs == 0)
1872 fprintf(stderr, "Warning: vlan %d with no nics\n", vlan->id);
1873 if (vlan->nb_host_devs == 0)
1874 fprintf(stderr,
1875 "Warning: vlan %d is not connected to host network\n",
1876 vlan->id);