Initialize entire cpuid2 structure
[qemu-kvm/fedora.git] / net.c
blobb4c92daa78aa2c0a56ba6881477df9d13dab5a26
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 // FIXME: #include "qemu-kvm.h"
125 static VLANState *first_vlan;
127 /***********************************************************/
128 /* network device redirectors */
130 #if defined(DEBUG_NET) || defined(DEBUG_SLIRP)
131 static void hex_dump(FILE *f, const uint8_t *buf, int size)
133 int len, i, j, c;
135 for(i=0;i<size;i+=16) {
136 len = size - i;
137 if (len > 16)
138 len = 16;
139 fprintf(f, "%08x ", i);
140 for(j=0;j<16;j++) {
141 if (j < len)
142 fprintf(f, " %02x", buf[i+j]);
143 else
144 fprintf(f, " ");
146 fprintf(f, " ");
147 for(j=0;j<len;j++) {
148 c = buf[i+j];
149 if (c < ' ' || c > '~')
150 c = '.';
151 fprintf(f, "%c", c);
153 fprintf(f, "\n");
156 #endif
158 static int parse_macaddr(uint8_t *macaddr, const char *p)
160 int i;
161 char *last_char;
162 long int offset;
164 errno = 0;
165 offset = strtol(p, &last_char, 0);
166 if (0 == errno && '\0' == *last_char &&
167 offset >= 0 && offset <= 0xFFFFFF) {
168 macaddr[3] = (offset & 0xFF0000) >> 16;
169 macaddr[4] = (offset & 0xFF00) >> 8;
170 macaddr[5] = offset & 0xFF;
171 return 0;
172 } else {
173 for(i = 0; i < 6; i++) {
174 macaddr[i] = strtol(p, (char **)&p, 16);
175 if (i == 5) {
176 if (*p != '\0')
177 return -1;
178 } else {
179 if (*p != ':' && *p != '-')
180 return -1;
181 p++;
184 return 0;
187 return -1;
190 static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
192 const char *p, *p1;
193 int len;
194 p = *pp;
195 p1 = strchr(p, sep);
196 if (!p1)
197 return -1;
198 len = p1 - p;
199 p1++;
200 if (buf_size > 0) {
201 if (len > buf_size - 1)
202 len = buf_size - 1;
203 memcpy(buf, p, len);
204 buf[len] = '\0';
206 *pp = p1;
207 return 0;
210 int parse_host_src_port(struct sockaddr_in *haddr,
211 struct sockaddr_in *saddr,
212 const char *input_str)
214 char *str = strdup(input_str);
215 char *host_str = str;
216 char *src_str;
217 const char *src_str2;
218 char *ptr;
221 * Chop off any extra arguments at the end of the string which
222 * would start with a comma, then fill in the src port information
223 * if it was provided else use the "any address" and "any port".
225 if ((ptr = strchr(str,',')))
226 *ptr = '\0';
228 if ((src_str = strchr(input_str,'@'))) {
229 *src_str = '\0';
230 src_str++;
233 if (parse_host_port(haddr, host_str) < 0)
234 goto fail;
236 src_str2 = src_str;
237 if (!src_str || *src_str == '\0')
238 src_str2 = ":0";
240 if (parse_host_port(saddr, src_str2) < 0)
241 goto fail;
243 free(str);
244 return(0);
246 fail:
247 free(str);
248 return -1;
251 int parse_host_port(struct sockaddr_in *saddr, const char *str)
253 char buf[512];
254 struct hostent *he;
255 const char *p, *r;
256 int port;
258 p = str;
259 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
260 return -1;
261 saddr->sin_family = AF_INET;
262 if (buf[0] == '\0') {
263 saddr->sin_addr.s_addr = 0;
264 } else {
265 if (qemu_isdigit(buf[0])) {
266 if (!inet_aton(buf, &saddr->sin_addr))
267 return -1;
268 } else {
269 if ((he = gethostbyname(buf)) == NULL)
270 return - 1;
271 saddr->sin_addr = *(struct in_addr *)he->h_addr;
274 port = strtol(p, (char **)&r, 0);
275 if (r == p)
276 return -1;
277 saddr->sin_port = htons(port);
278 return 0;
281 #if !defined(_WIN32) && 0
282 static int parse_unix_path(struct sockaddr_un *uaddr, const char *str)
284 const char *p;
285 int len;
287 len = MIN(108, strlen(str));
288 p = strchr(str, ',');
289 if (p)
290 len = MIN(len, p - str);
292 memset(uaddr, 0, sizeof(*uaddr));
294 uaddr->sun_family = AF_UNIX;
295 memcpy(uaddr->sun_path, str, len);
297 return 0;
299 #endif
301 void qemu_format_nic_info_str(VLANClientState *vc, uint8_t macaddr[6])
303 snprintf(vc->info_str, sizeof(vc->info_str),
304 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
305 vc->model,
306 macaddr[0], macaddr[1], macaddr[2],
307 macaddr[3], macaddr[4], macaddr[5]);
310 static char *assign_name(VLANClientState *vc1, const char *model)
312 VLANState *vlan;
313 char buf[256];
314 int id = 0;
316 for (vlan = first_vlan; vlan; vlan = vlan->next) {
317 VLANClientState *vc;
319 for (vc = vlan->first_client; vc; vc = vc->next)
320 if (vc != vc1 && strcmp(vc->model, model) == 0)
321 id++;
324 snprintf(buf, sizeof(buf), "%s.%d", model, id);
326 return strdup(buf);
329 VLANClientState *qemu_new_vlan_client(VLANState *vlan,
330 const char *model,
331 const char *name,
332 IOReadHandler *fd_read,
333 IOCanRWHandler *fd_can_read,
334 void *opaque)
336 VLANClientState *vc, **pvc;
337 vc = qemu_mallocz(sizeof(VLANClientState));
338 if (!vc)
339 return NULL;
340 vc->model = strdup(model);
341 if (name)
342 vc->name = strdup(name);
343 else
344 vc->name = assign_name(vc, model);
345 vc->fd_read = fd_read;
346 vc->fd_can_read = fd_can_read;
347 vc->opaque = opaque;
348 vc->vlan = vlan;
350 vc->next = NULL;
351 pvc = &vlan->first_client;
352 while (*pvc != NULL)
353 pvc = &(*pvc)->next;
354 *pvc = vc;
355 return vc;
358 void qemu_del_vlan_client(VLANClientState *vc)
360 VLANClientState **pvc = &vc->vlan->first_client;
362 while (*pvc != NULL)
363 if (*pvc == vc) {
364 *pvc = vc->next;
365 free(vc->name);
366 free(vc->model);
367 free(vc);
368 break;
369 } else
370 pvc = &(*pvc)->next;
373 int qemu_can_send_packet(VLANClientState *vc1)
375 VLANState *vlan = vc1->vlan;
376 VLANClientState *vc;
378 for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
379 if (vc != vc1) {
380 if (vc->fd_can_read && vc->fd_can_read(vc->opaque))
381 return 1;
384 return 0;
387 int qemu_send_packet(VLANClientState *vc1, const uint8_t *buf, int size)
389 VLANState *vlan = vc1->vlan;
390 VLANClientState *vc;
391 int ret = -EAGAIN;
393 if (vc1->link_down)
394 return 0;
396 #ifdef DEBUG_NET
397 printf("vlan %d send:\n", vlan->id);
398 hex_dump(stdout, buf, size);
399 #endif
400 for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
401 if (vc != vc1 && !vc->link_down) {
402 if (!vc->fd_can_read || vc->fd_can_read(vc->opaque)) {
403 vc->fd_read(vc->opaque, buf, size);
404 ret = 0;
408 return ret;
411 static ssize_t vc_sendv_compat(VLANClientState *vc, const struct iovec *iov,
412 int iovcnt)
414 uint8_t buffer[4096];
415 size_t offset = 0;
416 int i;
418 for (i = 0; i < iovcnt; i++) {
419 size_t len;
421 len = MIN(sizeof(buffer) - offset, iov[i].iov_len);
422 memcpy(buffer + offset, iov[i].iov_base, len);
423 offset += len;
426 vc->fd_read(vc->opaque, buffer, offset);
428 return offset;
431 static ssize_t calc_iov_length(const struct iovec *iov, int iovcnt)
433 size_t offset = 0;
434 int i;
436 for (i = 0; i < iovcnt; i++)
437 offset += iov[i].iov_len;
438 return offset;
441 ssize_t qemu_sendv_packet(VLANClientState *vc1, const struct iovec *iov,
442 int iovcnt)
444 VLANState *vlan = vc1->vlan;
445 VLANClientState *vc;
446 ssize_t max_len = 0;
448 if (vc1->link_down)
449 return calc_iov_length(iov, iovcnt);
451 for (vc = vlan->first_client; vc != NULL; vc = vc->next) {
452 ssize_t len = 0;
454 if (vc == vc1)
455 continue;
457 if (vc->link_down)
458 len = calc_iov_length(iov, iovcnt);
459 if (vc->fd_readv)
460 len = vc->fd_readv(vc->opaque, iov, iovcnt);
461 else if (vc->fd_read)
462 len = vc_sendv_compat(vc, iov, iovcnt);
464 max_len = MAX(max_len, len);
467 return max_len;
470 #if defined(CONFIG_SLIRP)
472 /* slirp network adapter */
474 static int slirp_inited;
475 static int slirp_restrict;
476 static char *slirp_ip;
477 static VLANClientState *slirp_vc;
479 int slirp_can_output(void)
481 return !slirp_vc || qemu_can_send_packet(slirp_vc);
484 void slirp_output(const uint8_t *pkt, int pkt_len)
486 #ifdef DEBUG_SLIRP
487 printf("slirp output:\n");
488 hex_dump(stdout, pkt, pkt_len);
489 #endif
490 if (!slirp_vc)
491 return;
492 qemu_send_packet(slirp_vc, pkt, pkt_len);
495 int slirp_is_inited(void)
497 return slirp_inited;
500 static void slirp_receive(void *opaque, const uint8_t *buf, int size)
502 #ifdef DEBUG_SLIRP
503 printf("slirp input:\n");
504 hex_dump(stdout, buf, size);
505 #endif
506 slirp_input(buf, size);
509 static int net_slirp_init(VLANState *vlan, const char *model, const char *name)
511 if (!slirp_inited) {
512 slirp_inited = 1;
513 slirp_init(slirp_restrict, slirp_ip);
515 slirp_vc = qemu_new_vlan_client(vlan, model, name,
516 slirp_receive, NULL, NULL);
517 slirp_vc->info_str[0] = '\0';
518 return 0;
521 void net_slirp_redir(const char *redir_str)
523 int is_udp;
524 char buf[256], *r;
525 const char *p;
526 struct in_addr guest_addr;
527 int host_port, guest_port;
529 if (!slirp_inited) {
530 slirp_inited = 1;
531 slirp_init(slirp_restrict, slirp_ip);
534 p = redir_str;
535 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
536 goto fail;
537 if (!strcmp(buf, "tcp")) {
538 is_udp = 0;
539 } else if (!strcmp(buf, "udp")) {
540 is_udp = 1;
541 } else {
542 goto fail;
545 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
546 goto fail;
547 host_port = strtol(buf, &r, 0);
548 if (r == buf)
549 goto fail;
551 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
552 goto fail;
553 if (buf[0] == '\0') {
554 pstrcpy(buf, sizeof(buf), "10.0.2.15");
556 if (!inet_aton(buf, &guest_addr))
557 goto fail;
559 guest_port = strtol(p, &r, 0);
560 if (r == p)
561 goto fail;
563 if (slirp_redir(is_udp, host_port, guest_addr, guest_port) < 0) {
564 fprintf(stderr, "qemu: could not set up redirection\n");
565 exit(1);
567 return;
568 fail:
569 fprintf(stderr, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
570 exit(1);
573 #ifndef _WIN32
575 static char smb_dir[1024];
577 static void erase_dir(char *dir_name)
579 DIR *d;
580 struct dirent *de;
581 char filename[1024];
583 /* erase all the files in the directory */
584 if ((d = opendir(dir_name)) != 0) {
585 for(;;) {
586 de = readdir(d);
587 if (!de)
588 break;
589 if (strcmp(de->d_name, ".") != 0 &&
590 strcmp(de->d_name, "..") != 0) {
591 snprintf(filename, sizeof(filename), "%s/%s",
592 smb_dir, de->d_name);
593 if (unlink(filename) != 0) /* is it a directory? */
594 erase_dir(filename);
597 closedir(d);
598 rmdir(dir_name);
602 /* automatic user mode samba server configuration */
603 static void smb_exit(void)
605 erase_dir(smb_dir);
608 /* automatic user mode samba server configuration */
609 void net_slirp_smb(const char *exported_dir)
611 char smb_conf[1024];
612 char smb_cmdline[1024];
613 FILE *f;
615 if (!slirp_inited) {
616 slirp_inited = 1;
617 slirp_init(slirp_restrict, slirp_ip);
620 /* XXX: better tmp dir construction */
621 snprintf(smb_dir, sizeof(smb_dir), "/tmp/qemu-smb.%d", getpid());
622 if (mkdir(smb_dir, 0700) < 0) {
623 fprintf(stderr, "qemu: could not create samba server dir '%s'\n", smb_dir);
624 exit(1);
626 snprintf(smb_conf, sizeof(smb_conf), "%s/%s", smb_dir, "smb.conf");
628 f = fopen(smb_conf, "w");
629 if (!f) {
630 fprintf(stderr, "qemu: could not create samba server configuration file '%s'\n", smb_conf);
631 exit(1);
633 fprintf(f,
634 "[global]\n"
635 "private dir=%s\n"
636 "smb ports=0\n"
637 "socket address=127.0.0.1\n"
638 "pid directory=%s\n"
639 "lock directory=%s\n"
640 "log file=%s/log.smbd\n"
641 "smb passwd file=%s/smbpasswd\n"
642 "security = share\n"
643 "[qemu]\n"
644 "path=%s\n"
645 "read only=no\n"
646 "guest ok=yes\n",
647 smb_dir,
648 smb_dir,
649 smb_dir,
650 smb_dir,
651 smb_dir,
652 exported_dir
654 fclose(f);
655 atexit(smb_exit);
657 snprintf(smb_cmdline, sizeof(smb_cmdline), "%s -s %s",
658 SMBD_COMMAND, smb_conf);
660 slirp_add_exec(0, smb_cmdline, 4, 139);
663 #endif /* !defined(_WIN32) */
664 void do_info_slirp(void)
666 slirp_stats();
669 #endif /* CONFIG_SLIRP */
671 #ifdef _WIN32
673 int tap_has_vnet_hdr(void *opaque)
675 return 0;
678 void tap_using_vnet_hdr(void *opaque, int using_vnet_hdr)
682 #else /* !defined(_WIN32) */
684 /* Maximum GSO packet size (64k) plus plenty of room for
685 * the ethernet and virtio_net headers
687 #define TAP_BUFSIZE (4096 + 65536)
689 #ifdef IFF_VNET_HDR
690 #include <linux/virtio_net.h>
691 #endif
693 typedef struct TAPState {
694 VLANClientState *vc;
695 int fd;
696 char down_script[1024];
697 char down_script_arg[128];
698 char buf[TAP_BUFSIZE];
699 int size;
700 unsigned int has_vnet_hdr : 1;
701 unsigned int using_vnet_hdr : 1;
702 } TAPState;
704 #ifdef HAVE_IOVEC
705 static ssize_t tap_receive_iov(void *opaque, const struct iovec *iov,
706 int iovcnt)
708 TAPState *s = opaque;
709 ssize_t len;
711 do {
712 len = writev(s->fd, iov, iovcnt);
713 } while (len == -1 && (errno == EINTR || errno == EAGAIN));
715 return len;
717 #endif
719 static void tap_receive(void *opaque, const uint8_t *buf, int size)
721 struct iovec iov[2];
722 int i = 0;
724 #ifdef IFF_VNET_HDR
725 TAPState *s = opaque;
726 struct virtio_net_hdr hdr = { 0, };
728 if (s->has_vnet_hdr && !s->using_vnet_hdr) {
729 iov[i].iov_base = &hdr;
730 iov[i].iov_len = sizeof(hdr);
731 i++;
733 #endif
735 iov[i].iov_base = (char *) buf;
736 iov[i].iov_len = size;
737 i++;
739 tap_receive_iov(opaque, iov, i);
742 static int tap_can_send(void *opaque)
744 TAPState *s = opaque;
745 VLANClientState *vc;
746 int can_receive = 0;
748 /* Check to see if any of our clients can receive a packet */
749 for (vc = s->vc->vlan->first_client; vc; vc = vc->next) {
750 /* Skip ourselves */
751 if (vc == s->vc)
752 continue;
754 if (!vc->fd_can_read) {
755 /* no fd_can_read handler, they always can receive */
756 can_receive = 1;
757 } else
758 can_receive = vc->fd_can_read(vc->opaque);
760 /* Once someone can receive, we try to send a packet */
761 if (can_receive)
762 break;
765 return can_receive;
768 static int tap_send_packet(TAPState *s)
770 uint8_t *buf = s->buf;
771 int size = s->size;
773 #ifdef IFF_VNET_HDR
774 if (s->has_vnet_hdr && !s->using_vnet_hdr) {
775 buf += sizeof(struct virtio_net_hdr);
776 size -= sizeof(struct virtio_net_hdr);
778 #endif
780 return qemu_send_packet(s->vc, buf, size);
783 static void tap_send(void *opaque)
785 TAPState *s = opaque;
787 /* First try to send any buffered packet */
788 if (s->size > 0) {
789 int err;
791 /* If noone can receive the packet, buffer it */
792 err = tap_send_packet(s);
793 if (err == -EAGAIN)
794 return;
797 /* Read packets until we hit EAGAIN */
798 do {
799 #ifdef __sun__
800 struct strbuf sbuf;
801 int f = 0;
802 sbuf.maxlen = sizeof(s->buf);
803 sbuf.buf = s->buf;
804 s->size = getmsg(s->fd, NULL, &sbuf, &f) >=0 ? sbuf.len : -1;
805 #else
806 // FIXME: kvm_sleep_begin();
807 s->size = read(s->fd, s->buf, sizeof(s->buf));
808 // FIXME: kvm_sleep_end();
809 #endif
811 if (s->size == -1 && errno == EINTR)
812 continue;
814 if (s->size > 0) {
815 int err;
817 /* If noone can receive the packet, buffer it */
818 err = tap_send_packet(s);
819 if (err == -EAGAIN)
820 break;
822 } while (s->size > 0);
825 int tap_has_vnet_hdr(void *opaque)
827 VLANClientState *vc = opaque;
828 TAPState *s = vc->opaque;
830 return s ? s->has_vnet_hdr : 0;
833 void tap_using_vnet_hdr(void *opaque, int using_vnet_hdr)
835 VLANClientState *vc = opaque;
836 TAPState *s = vc->opaque;
838 if (!s || !s->has_vnet_hdr)
839 return;
841 s->using_vnet_hdr = using_vnet_hdr != 0;
844 static int tap_probe_vnet_hdr(int fd)
846 #if defined(TUNGETIFF) && defined(IFF_VNET_HDR)
847 struct ifreq ifr;
849 if (ioctl(fd, TUNGETIFF, &ifr) != 0) {
850 fprintf(stderr, "TUNGETIFF ioctl() failed: %s\n", strerror(errno));
851 return 0;
854 return ifr.ifr_flags & IFF_VNET_HDR;
855 #else
856 return 0;
857 #endif
860 #ifdef TUNSETOFFLOAD
861 static void tap_set_offload(VLANClientState *vc, int csum, int tso4, int tso6,
862 int ecn)
864 TAPState *s = vc->opaque;
865 unsigned int offload = 0;
867 if (csum) {
868 offload |= TUN_F_CSUM;
869 if (tso4)
870 offload |= TUN_F_TSO4;
871 if (tso6)
872 offload |= TUN_F_TSO6;
873 if ((tso4 || tso6) && ecn)
874 offload |= TUN_F_TSO_ECN;
877 if (ioctl(s->fd, TUNSETOFFLOAD, offload) != 0)
878 fprintf(stderr, "TUNSETOFFLOAD ioctl() failed: %s\n",
879 strerror(errno));
881 #endif /* TUNSETOFFLOAD */
883 /* fd support */
885 static TAPState *net_tap_fd_init(VLANState *vlan,
886 const char *model,
887 const char *name,
888 int fd,
889 int vnet_hdr)
891 TAPState *s;
893 s = qemu_mallocz(sizeof(TAPState));
894 if (!s)
895 return NULL;
896 s->fd = fd;
897 s->has_vnet_hdr = vnet_hdr != 0;
898 s->vc = qemu_new_vlan_client(vlan, model, name, tap_receive, NULL, s);
899 #ifdef HAVE_IOVEC
900 s->vc->fd_readv = tap_receive_iov;
901 #endif
902 #ifdef TUNSETOFFLOAD
903 s->vc->set_offload = tap_set_offload;
904 #endif
905 qemu_set_fd_handler2(s->fd, tap_can_send, tap_send, NULL, s);
906 snprintf(s->vc->info_str, sizeof(s->vc->info_str), "fd=%d", fd);
907 return s;
910 #if defined (_BSD) || defined (__FreeBSD_kernel__)
911 static int tap_open(char *ifname, int ifname_size)
913 int fd;
914 char *dev;
915 struct stat s;
917 TFR(fd = open("/dev/tap", O_RDWR));
918 if (fd < 0) {
919 fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n");
920 return -1;
923 fstat(fd, &s);
924 dev = devname(s.st_rdev, S_IFCHR);
925 pstrcpy(ifname, ifname_size, dev);
927 fcntl(fd, F_SETFL, O_NONBLOCK);
928 return fd;
930 #elif defined(__sun__)
931 #define TUNNEWPPA (('T'<<16) | 0x0001)
933 * Allocate TAP device, returns opened fd.
934 * Stores dev name in the first arg(must be large enough).
936 int tap_alloc(char *dev, size_t dev_size)
938 int tap_fd, if_fd, ppa = -1;
939 static int ip_fd = 0;
940 char *ptr;
942 static int arp_fd = 0;
943 int ip_muxid, arp_muxid;
944 struct strioctl strioc_if, strioc_ppa;
945 int link_type = I_PLINK;;
946 struct lifreq ifr;
947 char actual_name[32] = "";
949 memset(&ifr, 0x0, sizeof(ifr));
951 if( *dev ){
952 ptr = dev;
953 while( *ptr && !qemu_isdigit((int)*ptr) ) ptr++;
954 ppa = atoi(ptr);
957 /* Check if IP device was opened */
958 if( ip_fd )
959 close(ip_fd);
961 TFR(ip_fd = open("/dev/udp", O_RDWR, 0));
962 if (ip_fd < 0) {
963 syslog(LOG_ERR, "Can't open /dev/ip (actually /dev/udp)");
964 return -1;
967 TFR(tap_fd = open("/dev/tap", O_RDWR, 0));
968 if (tap_fd < 0) {
969 syslog(LOG_ERR, "Can't open /dev/tap");
970 return -1;
973 /* Assign a new PPA and get its unit number. */
974 strioc_ppa.ic_cmd = TUNNEWPPA;
975 strioc_ppa.ic_timout = 0;
976 strioc_ppa.ic_len = sizeof(ppa);
977 strioc_ppa.ic_dp = (char *)&ppa;
978 if ((ppa = ioctl (tap_fd, I_STR, &strioc_ppa)) < 0)
979 syslog (LOG_ERR, "Can't assign new interface");
981 TFR(if_fd = open("/dev/tap", O_RDWR, 0));
982 if (if_fd < 0) {
983 syslog(LOG_ERR, "Can't open /dev/tap (2)");
984 return -1;
986 if(ioctl(if_fd, I_PUSH, "ip") < 0){
987 syslog(LOG_ERR, "Can't push IP module");
988 return -1;
991 if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0)
992 syslog(LOG_ERR, "Can't get flags\n");
994 snprintf (actual_name, 32, "tap%d", ppa);
995 pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name);
997 ifr.lifr_ppa = ppa;
998 /* Assign ppa according to the unit number returned by tun device */
1000 if (ioctl (if_fd, SIOCSLIFNAME, &ifr) < 0)
1001 syslog (LOG_ERR, "Can't set PPA %d", ppa);
1002 if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) <0)
1003 syslog (LOG_ERR, "Can't get flags\n");
1004 /* Push arp module to if_fd */
1005 if (ioctl (if_fd, I_PUSH, "arp") < 0)
1006 syslog (LOG_ERR, "Can't push ARP module (2)");
1008 /* Push arp module to ip_fd */
1009 if (ioctl (ip_fd, I_POP, NULL) < 0)
1010 syslog (LOG_ERR, "I_POP failed\n");
1011 if (ioctl (ip_fd, I_PUSH, "arp") < 0)
1012 syslog (LOG_ERR, "Can't push ARP module (3)\n");
1013 /* Open arp_fd */
1014 TFR(arp_fd = open ("/dev/tap", O_RDWR, 0));
1015 if (arp_fd < 0)
1016 syslog (LOG_ERR, "Can't open %s\n", "/dev/tap");
1018 /* Set ifname to arp */
1019 strioc_if.ic_cmd = SIOCSLIFNAME;
1020 strioc_if.ic_timout = 0;
1021 strioc_if.ic_len = sizeof(ifr);
1022 strioc_if.ic_dp = (char *)&ifr;
1023 if (ioctl(arp_fd, I_STR, &strioc_if) < 0){
1024 syslog (LOG_ERR, "Can't set ifname to arp\n");
1027 if((ip_muxid = ioctl(ip_fd, I_LINK, if_fd)) < 0){
1028 syslog(LOG_ERR, "Can't link TAP device to IP");
1029 return -1;
1032 if ((arp_muxid = ioctl (ip_fd, link_type, arp_fd)) < 0)
1033 syslog (LOG_ERR, "Can't link TAP device to ARP");
1035 close (if_fd);
1037 memset(&ifr, 0x0, sizeof(ifr));
1038 pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name);
1039 ifr.lifr_ip_muxid = ip_muxid;
1040 ifr.lifr_arp_muxid = arp_muxid;
1042 if (ioctl (ip_fd, SIOCSLIFMUXID, &ifr) < 0)
1044 ioctl (ip_fd, I_PUNLINK , arp_muxid);
1045 ioctl (ip_fd, I_PUNLINK, ip_muxid);
1046 syslog (LOG_ERR, "Can't set multiplexor id");
1049 snprintf(dev, dev_size, "tap%d", ppa);
1050 return tap_fd;
1053 static int tap_open(char *ifname, int ifname_size, int *vnet_hdr)
1055 char dev[10]="";
1056 int fd;
1057 if( (fd = tap_alloc(dev, sizeof(dev))) < 0 ){
1058 fprintf(stderr, "Cannot allocate TAP device\n");
1059 return -1;
1061 pstrcpy(ifname, ifname_size, dev);
1062 fcntl(fd, F_SETFL, O_NONBLOCK);
1063 return fd;
1065 #elif defined (_AIX)
1066 static int tap_open(char *ifname, int ifname_size)
1068 fprintf (stderr, "no tap on AIX\n");
1069 return -1;
1071 #else
1072 static int tap_open(char *ifname, int ifname_size, int *vnet_hdr)
1074 struct ifreq ifr;
1075 int fd, ret;
1077 TFR(fd = open("/dev/net/tun", O_RDWR));
1078 if (fd < 0) {
1079 fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
1080 return -1;
1082 memset(&ifr, 0, sizeof(ifr));
1083 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
1085 #if defined(TUNGETFEATURES) && defined(IFF_VNET_HDR)
1087 unsigned int features;
1089 if (ioctl(fd, TUNGETFEATURES, &features) == 0 &&
1090 features & IFF_VNET_HDR) {
1091 *vnet_hdr = 1;
1092 ifr.ifr_flags |= IFF_VNET_HDR;
1095 #endif
1097 if (ifname[0] != '\0')
1098 pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname);
1099 else
1100 pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d");
1101 ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
1102 if (ret != 0) {
1103 fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
1104 close(fd);
1105 return -1;
1107 pstrcpy(ifname, ifname_size, ifr.ifr_name);
1108 fcntl(fd, F_SETFL, O_NONBLOCK);
1109 return fd;
1111 #endif
1113 static int launch_script(const char *setup_script, const char *ifname, int fd)
1115 int pid, status;
1116 char *args[3];
1117 char **parg;
1119 /* try to launch network script */
1120 pid = fork();
1121 if (pid >= 0) {
1122 if (pid == 0) {
1123 int open_max = sysconf (_SC_OPEN_MAX), i;
1124 for (i = 0; i < open_max; i++)
1125 if (i != STDIN_FILENO &&
1126 i != STDOUT_FILENO &&
1127 i != STDERR_FILENO &&
1128 i != fd)
1129 close(i);
1131 parg = args;
1132 *parg++ = (char *)setup_script;
1133 *parg++ = (char *)ifname;
1134 *parg++ = NULL;
1135 execv(setup_script, args);
1136 _exit(1);
1138 while (waitpid(pid, &status, 0) != pid);
1139 if (!WIFEXITED(status) ||
1140 WEXITSTATUS(status) != 0) {
1141 fprintf(stderr, "%s: could not launch network script\n",
1142 setup_script);
1143 return -1;
1146 return 0;
1149 static int net_tap_init(VLANState *vlan, const char *model,
1150 const char *name, const char *ifname1,
1151 const char *setup_script, const char *down_script)
1153 TAPState *s;
1154 int fd;
1155 int vnet_hdr;
1156 char ifname[128];
1158 if (ifname1 != NULL)
1159 pstrcpy(ifname, sizeof(ifname), ifname1);
1160 else
1161 ifname[0] = '\0';
1162 vnet_hdr = 0;
1163 TFR(fd = tap_open(ifname, sizeof(ifname), &vnet_hdr));
1164 if (fd < 0)
1165 return -1;
1167 if (!setup_script || !strcmp(setup_script, "no"))
1168 setup_script = "";
1169 if (setup_script[0] != '\0') {
1170 if (launch_script(setup_script, ifname, fd))
1171 return -1;
1173 s = net_tap_fd_init(vlan, model, name, fd, vnet_hdr);
1174 if (!s)
1175 return -1;
1177 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1178 "ifname=%s,script=%s,downscript=%s",
1179 ifname, setup_script, down_script);
1180 if (down_script && strcmp(down_script, "no")) {
1181 snprintf(s->down_script, sizeof(s->down_script), "%s", down_script);
1182 snprintf(s->down_script_arg, sizeof(s->down_script_arg), "%s", ifname);
1184 return 0;
1187 #endif /* !_WIN32 */
1189 #if defined(CONFIG_VDE)
1190 typedef struct VDEState {
1191 VLANClientState *vc;
1192 VDECONN *vde;
1193 } VDEState;
1195 static void vde_to_qemu(void *opaque)
1197 VDEState *s = opaque;
1198 uint8_t buf[4096];
1199 int size;
1201 size = vde_recv(s->vde, buf, sizeof(buf), 0);
1202 if (size > 0) {
1203 qemu_send_packet(s->vc, buf, size);
1207 static void vde_from_qemu(void *opaque, const uint8_t *buf, int size)
1209 VDEState *s = opaque;
1210 int ret;
1211 for(;;) {
1212 ret = vde_send(s->vde, buf, size, 0);
1213 if (ret < 0 && errno == EINTR) {
1214 } else {
1215 break;
1220 static int net_vde_init(VLANState *vlan, const char *model,
1221 const char *name, const char *sock,
1222 int port, const char *group, int mode)
1224 VDEState *s;
1225 char *init_group = strlen(group) ? (char *)group : NULL;
1226 char *init_sock = strlen(sock) ? (char *)sock : NULL;
1228 struct vde_open_args args = {
1229 .port = port,
1230 .group = init_group,
1231 .mode = mode,
1234 s = qemu_mallocz(sizeof(VDEState));
1235 if (!s)
1236 return -1;
1237 s->vde = vde_open(init_sock, "QEMU", &args);
1238 if (!s->vde){
1239 free(s);
1240 return -1;
1242 s->vc = qemu_new_vlan_client(vlan, model, name, vde_from_qemu, NULL, s);
1243 qemu_set_fd_handler(vde_datafd(s->vde), vde_to_qemu, NULL, s);
1244 snprintf(s->vc->info_str, sizeof(s->vc->info_str), "sock=%s,fd=%d",
1245 sock, vde_datafd(s->vde));
1246 return 0;
1248 #endif
1250 /* network connection */
1251 typedef struct NetSocketState {
1252 VLANClientState *vc;
1253 int fd;
1254 int state; /* 0 = getting length, 1 = getting data */
1255 int index;
1256 int packet_len;
1257 uint8_t buf[4096];
1258 struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
1259 } NetSocketState;
1261 typedef struct NetSocketListenState {
1262 VLANState *vlan;
1263 char *model;
1264 char *name;
1265 int fd;
1266 } NetSocketListenState;
1268 /* XXX: we consider we can send the whole packet without blocking */
1269 static void net_socket_receive(void *opaque, const uint8_t *buf, int size)
1271 NetSocketState *s = opaque;
1272 uint32_t len;
1273 len = htonl(size);
1275 send_all(s->fd, (const uint8_t *)&len, sizeof(len));
1276 send_all(s->fd, buf, size);
1279 static void net_socket_receive_dgram(void *opaque, const uint8_t *buf, int size)
1281 NetSocketState *s = opaque;
1282 sendto(s->fd, buf, size, 0,
1283 (struct sockaddr *)&s->dgram_dst, sizeof(s->dgram_dst));
1286 static void net_socket_send(void *opaque)
1288 NetSocketState *s = opaque;
1289 int l, size, err;
1290 uint8_t buf1[4096];
1291 const uint8_t *buf;
1293 size = recv(s->fd, buf1, sizeof(buf1), 0);
1294 if (size < 0) {
1295 err = socket_error();
1296 if (err != EWOULDBLOCK)
1297 goto eoc;
1298 } else if (size == 0) {
1299 /* end of connection */
1300 eoc:
1301 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1302 closesocket(s->fd);
1303 return;
1305 buf = buf1;
1306 while (size > 0) {
1307 /* reassemble a packet from the network */
1308 switch(s->state) {
1309 case 0:
1310 l = 4 - s->index;
1311 if (l > size)
1312 l = size;
1313 memcpy(s->buf + s->index, buf, l);
1314 buf += l;
1315 size -= l;
1316 s->index += l;
1317 if (s->index == 4) {
1318 /* got length */
1319 s->packet_len = ntohl(*(uint32_t *)s->buf);
1320 s->index = 0;
1321 s->state = 1;
1323 break;
1324 case 1:
1325 l = s->packet_len - s->index;
1326 if (l > size)
1327 l = size;
1328 memcpy(s->buf + s->index, buf, l);
1329 s->index += l;
1330 buf += l;
1331 size -= l;
1332 if (s->index >= s->packet_len) {
1333 qemu_send_packet(s->vc, s->buf, s->packet_len);
1334 s->index = 0;
1335 s->state = 0;
1337 break;
1342 static void net_socket_send_dgram(void *opaque)
1344 NetSocketState *s = opaque;
1345 int size;
1347 size = recv(s->fd, s->buf, sizeof(s->buf), 0);
1348 if (size < 0)
1349 return;
1350 if (size == 0) {
1351 /* end of connection */
1352 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1353 return;
1355 qemu_send_packet(s->vc, s->buf, size);
1358 static int net_socket_mcast_create(struct sockaddr_in *mcastaddr)
1360 struct ip_mreq imr;
1361 int fd;
1362 int val, ret;
1363 if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) {
1364 fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
1365 inet_ntoa(mcastaddr->sin_addr),
1366 (int)ntohl(mcastaddr->sin_addr.s_addr));
1367 return -1;
1370 fd = socket(PF_INET, SOCK_DGRAM, 0);
1371 if (fd < 0) {
1372 perror("socket(PF_INET, SOCK_DGRAM)");
1373 return -1;
1376 val = 1;
1377 ret=setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
1378 (const char *)&val, sizeof(val));
1379 if (ret < 0) {
1380 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
1381 goto fail;
1384 ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr));
1385 if (ret < 0) {
1386 perror("bind");
1387 goto fail;
1390 /* Add host to multicast group */
1391 imr.imr_multiaddr = mcastaddr->sin_addr;
1392 imr.imr_interface.s_addr = htonl(INADDR_ANY);
1394 ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1395 (const char *)&imr, sizeof(struct ip_mreq));
1396 if (ret < 0) {
1397 perror("setsockopt(IP_ADD_MEMBERSHIP)");
1398 goto fail;
1401 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
1402 val = 1;
1403 ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
1404 (const char *)&val, sizeof(val));
1405 if (ret < 0) {
1406 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
1407 goto fail;
1410 socket_set_nonblock(fd);
1411 return fd;
1412 fail:
1413 if (fd >= 0)
1414 closesocket(fd);
1415 return -1;
1418 static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan,
1419 const char *model,
1420 const char *name,
1421 int fd, int is_connected)
1423 struct sockaddr_in saddr;
1424 int newfd;
1425 socklen_t saddr_len;
1426 NetSocketState *s;
1428 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
1429 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
1430 * by ONLY ONE process: we must "clone" this dgram socket --jjo
1433 if (is_connected) {
1434 if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) {
1435 /* must be bound */
1436 if (saddr.sin_addr.s_addr==0) {
1437 fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
1438 fd);
1439 return NULL;
1441 /* clone dgram socket */
1442 newfd = net_socket_mcast_create(&saddr);
1443 if (newfd < 0) {
1444 /* error already reported by net_socket_mcast_create() */
1445 close(fd);
1446 return NULL;
1448 /* clone newfd to fd, close newfd */
1449 dup2(newfd, fd);
1450 close(newfd);
1452 } else {
1453 fprintf(stderr, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
1454 fd, strerror(errno));
1455 return NULL;
1459 s = qemu_mallocz(sizeof(NetSocketState));
1460 if (!s)
1461 return NULL;
1462 s->fd = fd;
1464 s->vc = qemu_new_vlan_client(vlan, model, name, net_socket_receive_dgram, NULL, s);
1465 qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s);
1467 /* mcast: save bound address as dst */
1468 if (is_connected) s->dgram_dst=saddr;
1470 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1471 "socket: fd=%d (%s mcast=%s:%d)",
1472 fd, is_connected? "cloned" : "",
1473 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1474 return s;
1477 static void net_socket_connect(void *opaque)
1479 NetSocketState *s = opaque;
1480 qemu_set_fd_handler(s->fd, net_socket_send, NULL, s);
1483 static NetSocketState *net_socket_fd_init_stream(VLANState *vlan,
1484 const char *model,
1485 const char *name,
1486 int fd, int is_connected)
1488 NetSocketState *s;
1489 s = qemu_mallocz(sizeof(NetSocketState));
1490 if (!s)
1491 return NULL;
1492 s->fd = fd;
1493 s->vc = qemu_new_vlan_client(vlan, model, name,
1494 net_socket_receive, NULL, s);
1495 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1496 "socket: fd=%d", fd);
1497 if (is_connected) {
1498 net_socket_connect(s);
1499 } else {
1500 qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s);
1502 return s;
1505 static NetSocketState *net_socket_fd_init(VLANState *vlan,
1506 const char *model, const char *name,
1507 int fd, int is_connected)
1509 int so_type=-1, optlen=sizeof(so_type);
1511 if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type,
1512 (socklen_t *)&optlen)< 0) {
1513 fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd);
1514 return NULL;
1516 switch(so_type) {
1517 case SOCK_DGRAM:
1518 return net_socket_fd_init_dgram(vlan, model, name, fd, is_connected);
1519 case SOCK_STREAM:
1520 return net_socket_fd_init_stream(vlan, model, name, fd, is_connected);
1521 default:
1522 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
1523 fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd);
1524 return net_socket_fd_init_stream(vlan, model, name, fd, is_connected);
1526 return NULL;
1529 static void net_socket_accept(void *opaque)
1531 NetSocketListenState *s = opaque;
1532 NetSocketState *s1;
1533 struct sockaddr_in saddr;
1534 socklen_t len;
1535 int fd;
1537 for(;;) {
1538 len = sizeof(saddr);
1539 fd = accept(s->fd, (struct sockaddr *)&saddr, &len);
1540 if (fd < 0 && errno != EINTR) {
1541 return;
1542 } else if (fd >= 0) {
1543 break;
1546 s1 = net_socket_fd_init(s->vlan, s->model, s->name, fd, 1);
1547 if (!s1) {
1548 closesocket(fd);
1549 } else {
1550 snprintf(s1->vc->info_str, sizeof(s1->vc->info_str),
1551 "socket: connection from %s:%d",
1552 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1556 static int net_socket_listen_init(VLANState *vlan,
1557 const char *model,
1558 const char *name,
1559 const char *host_str)
1561 NetSocketListenState *s;
1562 int fd, val, ret;
1563 struct sockaddr_in saddr;
1565 if (parse_host_port(&saddr, host_str) < 0)
1566 return -1;
1568 s = qemu_mallocz(sizeof(NetSocketListenState));
1569 if (!s)
1570 return -1;
1572 fd = socket(PF_INET, SOCK_STREAM, 0);
1573 if (fd < 0) {
1574 perror("socket");
1575 return -1;
1577 socket_set_nonblock(fd);
1579 /* allow fast reuse */
1580 val = 1;
1581 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
1583 ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
1584 if (ret < 0) {
1585 perror("bind");
1586 return -1;
1588 ret = listen(fd, 0);
1589 if (ret < 0) {
1590 perror("listen");
1591 return -1;
1593 s->vlan = vlan;
1594 s->model = strdup(model);
1595 s->name = strdup(name);
1596 s->fd = fd;
1597 qemu_set_fd_handler(fd, net_socket_accept, NULL, s);
1598 return 0;
1601 static int net_socket_connect_init(VLANState *vlan,
1602 const char *model,
1603 const char *name,
1604 const char *host_str)
1606 NetSocketState *s;
1607 int fd, connected, ret, err;
1608 struct sockaddr_in saddr;
1610 if (parse_host_port(&saddr, host_str) < 0)
1611 return -1;
1613 fd = socket(PF_INET, SOCK_STREAM, 0);
1614 if (fd < 0) {
1615 perror("socket");
1616 return -1;
1618 socket_set_nonblock(fd);
1620 connected = 0;
1621 for(;;) {
1622 ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
1623 if (ret < 0) {
1624 err = socket_error();
1625 if (err == EINTR || err == EWOULDBLOCK) {
1626 } else if (err == EINPROGRESS) {
1627 break;
1628 #ifdef _WIN32
1629 } else if (err == WSAEALREADY) {
1630 break;
1631 #endif
1632 } else {
1633 perror("connect");
1634 closesocket(fd);
1635 return -1;
1637 } else {
1638 connected = 1;
1639 break;
1642 s = net_socket_fd_init(vlan, model, name, fd, connected);
1643 if (!s)
1644 return -1;
1645 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1646 "socket: connect to %s:%d",
1647 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1648 return 0;
1651 static int net_socket_mcast_init(VLANState *vlan,
1652 const char *model,
1653 const char *name,
1654 const char *host_str)
1656 NetSocketState *s;
1657 int fd;
1658 struct sockaddr_in saddr;
1660 if (parse_host_port(&saddr, host_str) < 0)
1661 return -1;
1664 fd = net_socket_mcast_create(&saddr);
1665 if (fd < 0)
1666 return -1;
1668 s = net_socket_fd_init(vlan, model, name, fd, 0);
1669 if (!s)
1670 return -1;
1672 s->dgram_dst = saddr;
1674 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1675 "socket: mcast=%s:%d",
1676 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1677 return 0;
1681 /* find or alloc a new VLAN */
1682 VLANState *qemu_find_vlan(int id)
1684 VLANState **pvlan, *vlan;
1685 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
1686 if (vlan->id == id)
1687 return vlan;
1689 vlan = qemu_mallocz(sizeof(VLANState));
1690 if (!vlan)
1691 return NULL;
1692 vlan->id = id;
1693 vlan->next = NULL;
1694 pvlan = &first_vlan;
1695 while (*pvlan != NULL)
1696 pvlan = &(*pvlan)->next;
1697 *pvlan = vlan;
1698 return vlan;
1701 void qemu_check_nic_model(NICInfo *nd, const char *model)
1703 const char *models[2];
1705 models[0] = model;
1706 models[1] = NULL;
1708 qemu_check_nic_model_list(nd, models, model);
1711 void qemu_check_nic_model_list(NICInfo *nd, const char * const *models,
1712 const char *default_model)
1714 int i, exit_status = 0;
1716 if (!nd->model)
1717 nd->model = strdup(default_model);
1719 if (strcmp(nd->model, "?") != 0) {
1720 for (i = 0 ; models[i]; i++)
1721 if (strcmp(nd->model, models[i]) == 0)
1722 return;
1724 fprintf(stderr, "qemu: Unsupported NIC model: %s\n", nd->model);
1725 exit_status = 1;
1728 fprintf(stderr, "qemu: Supported NIC models: ");
1729 for (i = 0 ; models[i]; i++)
1730 fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n');
1732 exit(exit_status);
1735 int net_client_init(const char *device, const char *p)
1737 char buf[1024];
1738 int vlan_id, ret;
1739 VLANState *vlan;
1740 char *name = NULL;
1742 vlan_id = 0;
1743 if (get_param_value(buf, sizeof(buf), "vlan", p)) {
1744 vlan_id = strtol(buf, NULL, 0);
1746 vlan = qemu_find_vlan(vlan_id);
1747 if (!vlan) {
1748 fprintf(stderr, "Could not create vlan %d\n", vlan_id);
1749 return -1;
1751 if (get_param_value(buf, sizeof(buf), "name", p)) {
1752 name = strdup(buf);
1754 if (!strcmp(device, "nic")) {
1755 NICInfo *nd;
1756 uint8_t *macaddr;
1758 if (nb_nics >= MAX_NICS) {
1759 fprintf(stderr, "Too Many NICs\n");
1760 return -1;
1762 nd = &nd_table[nb_nics];
1763 macaddr = nd->macaddr;
1764 macaddr[0] = 0x52;
1765 macaddr[1] = 0x54;
1766 macaddr[2] = 0x00;
1767 macaddr[3] = 0x12;
1768 macaddr[4] = 0x34;
1769 macaddr[5] = 0x56 + nb_nics;
1771 if (get_param_value(buf, sizeof(buf), "macaddr", p)) {
1772 if (parse_macaddr(macaddr, buf) < 0) {
1773 fprintf(stderr, "invalid syntax for ethernet address\n");
1774 return -1;
1777 if (get_param_value(buf, sizeof(buf), "model", p)) {
1778 nd->model = strdup(buf);
1780 nd->vlan = vlan;
1781 nd->name = name;
1782 name = NULL;
1783 nb_nics++;
1784 vlan->nb_guest_devs++;
1785 ret = 0;
1786 } else
1787 if (!strcmp(device, "none")) {
1788 /* does nothing. It is needed to signal that no network cards
1789 are wanted */
1790 ret = 0;
1791 } else
1792 #ifdef CONFIG_SLIRP
1793 if (!strcmp(device, "user")) {
1794 if (get_param_value(buf, sizeof(buf), "hostname", p)) {
1795 pstrcpy(slirp_hostname, sizeof(slirp_hostname), buf);
1797 if (get_param_value(buf, sizeof(buf), "restrict", p)) {
1798 slirp_restrict = (buf[0] == 'y') ? 1 : 0;
1800 if (get_param_value(buf, sizeof(buf), "ip", p)) {
1801 slirp_ip = strdup(buf);
1803 vlan->nb_host_devs++;
1804 ret = net_slirp_init(vlan, device, name);
1805 } else
1806 #endif
1807 #ifdef _WIN32
1808 if (!strcmp(device, "tap")) {
1809 char ifname[64];
1810 if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
1811 fprintf(stderr, "tap: no interface name\n");
1812 return -1;
1814 vlan->nb_host_devs++;
1815 ret = tap_win32_init(vlan, device, name, ifname);
1816 } else
1817 #elif defined (_AIX)
1818 #else
1819 if (!strcmp(device, "tap")) {
1820 char ifname[64];
1821 char setup_script[1024], down_script[1024];
1822 int fd;
1823 vlan->nb_host_devs++;
1824 if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
1825 fd = strtol(buf, NULL, 0);
1826 fcntl(fd, F_SETFL, O_NONBLOCK);
1827 ret = -1;
1828 if (net_tap_fd_init(vlan, device, name, fd,
1829 tap_probe_vnet_hdr(fd)))
1830 ret = 0;
1831 } else {
1832 if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
1833 ifname[0] = '\0';
1835 if (get_param_value(setup_script, sizeof(setup_script), "script", p) == 0) {
1836 pstrcpy(setup_script, sizeof(setup_script), DEFAULT_NETWORK_SCRIPT);
1838 if (get_param_value(down_script, sizeof(down_script), "downscript", p) == 0) {
1839 pstrcpy(down_script, sizeof(down_script), DEFAULT_NETWORK_DOWN_SCRIPT);
1841 ret = net_tap_init(vlan, device, name, ifname, setup_script, down_script);
1843 } else
1844 #endif
1845 if (!strcmp(device, "socket")) {
1846 if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
1847 int fd;
1848 fd = strtol(buf, NULL, 0);
1849 ret = -1;
1850 if (net_socket_fd_init(vlan, device, name, fd, 1))
1851 ret = 0;
1852 } else if (get_param_value(buf, sizeof(buf), "listen", p) > 0) {
1853 ret = net_socket_listen_init(vlan, device, name, buf);
1854 } else if (get_param_value(buf, sizeof(buf), "connect", p) > 0) {
1855 ret = net_socket_connect_init(vlan, device, name, buf);
1856 } else if (get_param_value(buf, sizeof(buf), "mcast", p) > 0) {
1857 ret = net_socket_mcast_init(vlan, device, name, buf);
1858 } else {
1859 fprintf(stderr, "Unknown socket options: %s\n", p);
1860 return -1;
1862 vlan->nb_host_devs++;
1863 } else
1864 #ifdef CONFIG_VDE
1865 if (!strcmp(device, "vde")) {
1866 char vde_sock[1024], vde_group[512];
1867 int vde_port, vde_mode;
1868 vlan->nb_host_devs++;
1869 if (get_param_value(vde_sock, sizeof(vde_sock), "sock", p) <= 0) {
1870 vde_sock[0] = '\0';
1872 if (get_param_value(buf, sizeof(buf), "port", p) > 0) {
1873 vde_port = strtol(buf, NULL, 10);
1874 } else {
1875 vde_port = 0;
1877 if (get_param_value(vde_group, sizeof(vde_group), "group", p) <= 0) {
1878 vde_group[0] = '\0';
1880 if (get_param_value(buf, sizeof(buf), "mode", p) > 0) {
1881 vde_mode = strtol(buf, NULL, 8);
1882 } else {
1883 vde_mode = 0700;
1885 ret = net_vde_init(vlan, device, name, vde_sock, vde_port, vde_group, vde_mode);
1886 } else
1887 #endif
1889 fprintf(stderr, "Unknown network device: %s\n", device);
1890 if (name)
1891 free(name);
1892 return -1;
1894 if (ret < 0) {
1895 fprintf(stderr, "Could not initialize device '%s'\n", device);
1897 if (name)
1898 free(name);
1899 return ret;
1902 void net_client_uninit(NICInfo *nd)
1904 nd->vlan->nb_guest_devs--; /* XXX: free vlan on last reference */
1905 nb_nics--;
1906 nd->used = 0;
1907 free((void *)nd->model);
1910 int net_client_parse(const char *str)
1912 const char *p;
1913 char *q;
1914 char device[64];
1916 p = str;
1917 q = device;
1918 while (*p != '\0' && *p != ',') {
1919 if ((q - device) < sizeof(device) - 1)
1920 *q++ = *p;
1921 p++;
1923 *q = '\0';
1924 if (*p == ',')
1925 p++;
1927 return net_client_init(device, p);
1930 void do_info_network(void)
1932 VLANState *vlan;
1933 VLANClientState *vc;
1935 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
1936 term_printf("VLAN %d devices:\n", vlan->id);
1937 for(vc = vlan->first_client; vc != NULL; vc = vc->next)
1938 term_printf(" %s: %s\n", vc->name, vc->info_str);
1942 int do_set_link(const char *name, const char *up_or_down)
1944 VLANState *vlan;
1945 VLANClientState *vc = NULL;
1947 for (vlan = first_vlan; vlan != NULL; vlan = vlan->next)
1948 for (vc = vlan->first_client; vc != NULL; vc = vc->next)
1949 if (strcmp(vc->name, name) == 0)
1950 goto done;
1951 done:
1953 if (!vc) {
1954 term_printf("could not find network device '%s'", name);
1955 return 0;
1958 if (strcmp(up_or_down, "up") == 0)
1959 vc->link_down = 0;
1960 else if (strcmp(up_or_down, "down") == 0)
1961 vc->link_down = 1;
1962 else
1963 term_printf("invalid link status '%s'; only 'up' or 'down' valid\n",
1964 up_or_down);
1966 if (vc->link_status_changed)
1967 vc->link_status_changed(vc);
1969 return 1;
1972 void net_cleanup(void)
1974 VLANState *vlan;
1976 #if !defined(_WIN32)
1977 /* close network clients */
1978 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
1979 VLANClientState *vc;
1981 for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
1982 if (vc->fd_read == tap_receive) {
1983 TAPState *s = vc->opaque;
1985 if (s->down_script[0])
1986 launch_script(s->down_script, s->down_script_arg, s->fd);
1988 #if defined(CONFIG_VDE)
1989 if (vc->fd_read == vde_from_qemu) {
1990 VDEState *s = vc->opaque;
1991 vde_close(s->vde);
1993 #endif
1996 #endif
1999 void net_client_check(void)
2001 VLANState *vlan;
2003 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
2004 if (vlan->nb_guest_devs == 0 && vlan->nb_host_devs == 0)
2005 continue;
2006 if (vlan->nb_guest_devs == 0)
2007 fprintf(stderr, "Warning: vlan %d with no nics\n", vlan->id);
2008 if (vlan->nb_host_devs == 0)
2009 fprintf(stderr,
2010 "Warning: vlan %d is not connected to host network\n",
2011 vlan->id);