Fix warnings on 32 bit systems (qemu-kvm.c)
[qemu-kvm/fedora.git] / net.c
blob931def17e75fdaf97eba01e0c8e309b6a57360a2
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 <unistd.h>
25 #include <fcntl.h>
26 #include <signal.h>
27 #include <time.h>
28 #include <errno.h>
29 #include <sys/time.h>
30 #include <zlib.h>
32 /* Needed early for HOST_BSD etc. */
33 #include "config-host.h"
35 #ifndef _WIN32
36 #include <sys/times.h>
37 #include <sys/wait.h>
38 #include <termios.h>
39 #include <sys/mman.h>
40 #include <sys/ioctl.h>
41 #include <sys/resource.h>
42 #include <sys/socket.h>
43 #include <netinet/in.h>
44 #include <net/if.h>
45 #ifdef __NetBSD__
46 #include <net/if_tap.h>
47 #endif
48 #ifdef __linux__
49 #include <linux/if_tun.h>
50 #endif
51 #include <arpa/inet.h>
52 #include <dirent.h>
53 #include <netdb.h>
54 #include <sys/select.h>
55 #ifdef HOST_BSD
56 #include <sys/stat.h>
57 #if defined(__FreeBSD__) || defined(__DragonFly__)
58 #include <libutil.h>
59 #else
60 #include <util.h>
61 #endif
62 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
63 #include <freebsd/stdlib.h>
64 #else
65 #ifdef __linux__
66 #include <pty.h>
67 #include <malloc.h>
68 #include <linux/rtc.h>
70 /* For the benefit of older linux systems which don't supply it,
71 we use a local copy of hpet.h. */
72 /* #include <linux/hpet.h> */
73 #include "hpet.h"
75 #include <linux/ppdev.h>
76 #include <linux/parport.h>
77 #endif
78 #ifdef __sun__
79 #include <sys/stat.h>
80 #include <sys/ethernet.h>
81 #include <sys/sockio.h>
82 #include <netinet/arp.h>
83 #include <netinet/in.h>
84 #include <netinet/in_systm.h>
85 #include <netinet/ip.h>
86 #include <netinet/ip_icmp.h> // must come after ip.h
87 #include <netinet/udp.h>
88 #include <netinet/tcp.h>
89 #include <net/if.h>
90 #include <syslog.h>
91 #include <stropts.h>
92 #endif
93 #endif
94 #endif
96 #if defined(__OpenBSD__)
97 #include <util.h>
98 #endif
100 #if defined(CONFIG_VDE)
101 #include <libvdeplug.h>
102 #endif
104 #ifdef _WIN32
105 #include <windows.h>
106 #include <malloc.h>
107 #include <sys/timeb.h>
108 #include <mmsystem.h>
109 #define getopt_long_only getopt_long
110 #define memalign(align, size) malloc(size)
111 #endif
113 // FIXME: #include "qemu-kvm.h"
114 #include "qemu-common.h"
115 #include "net.h"
116 #include "monitor.h"
117 #include "sysemu.h"
118 #include "qemu-timer.h"
119 #include "qemu-char.h"
120 #include "audio/audio.h"
121 #include "qemu_socket.h"
122 #include "qemu-log.h"
124 #if defined(CONFIG_SLIRP)
125 #include "libslirp.h"
126 #endif
129 static VLANState *first_vlan;
131 /***********************************************************/
132 /* network device redirectors */
134 #if defined(DEBUG_NET) || defined(DEBUG_SLIRP)
135 static void hex_dump(FILE *f, const uint8_t *buf, int size)
137 int len, i, j, c;
139 for(i=0;i<size;i+=16) {
140 len = size - i;
141 if (len > 16)
142 len = 16;
143 fprintf(f, "%08x ", i);
144 for(j=0;j<16;j++) {
145 if (j < len)
146 fprintf(f, " %02x", buf[i+j]);
147 else
148 fprintf(f, " ");
150 fprintf(f, " ");
151 for(j=0;j<len;j++) {
152 c = buf[i+j];
153 if (c < ' ' || c > '~')
154 c = '.';
155 fprintf(f, "%c", c);
157 fprintf(f, "\n");
160 #endif
162 static int parse_macaddr(uint8_t *macaddr, const char *p)
164 int i;
165 char *last_char;
166 long int offset;
168 errno = 0;
169 offset = strtol(p, &last_char, 0);
170 if (0 == errno && '\0' == *last_char &&
171 offset >= 0 && offset <= 0xFFFFFF) {
172 macaddr[3] = (offset & 0xFF0000) >> 16;
173 macaddr[4] = (offset & 0xFF00) >> 8;
174 macaddr[5] = offset & 0xFF;
175 return 0;
176 } else {
177 for(i = 0; i < 6; i++) {
178 macaddr[i] = strtol(p, (char **)&p, 16);
179 if (i == 5) {
180 if (*p != '\0')
181 return -1;
182 } else {
183 if (*p != ':' && *p != '-')
184 return -1;
185 p++;
188 return 0;
191 return -1;
194 static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
196 const char *p, *p1;
197 int len;
198 p = *pp;
199 p1 = strchr(p, sep);
200 if (!p1)
201 return -1;
202 len = p1 - p;
203 p1++;
204 if (buf_size > 0) {
205 if (len > buf_size - 1)
206 len = buf_size - 1;
207 memcpy(buf, p, len);
208 buf[len] = '\0';
210 *pp = p1;
211 return 0;
214 int parse_host_src_port(struct sockaddr_in *haddr,
215 struct sockaddr_in *saddr,
216 const char *input_str)
218 char *str = strdup(input_str);
219 char *host_str = str;
220 char *src_str;
221 const char *src_str2;
222 char *ptr;
225 * Chop off any extra arguments at the end of the string which
226 * would start with a comma, then fill in the src port information
227 * if it was provided else use the "any address" and "any port".
229 if ((ptr = strchr(str,',')))
230 *ptr = '\0';
232 if ((src_str = strchr(input_str,'@'))) {
233 *src_str = '\0';
234 src_str++;
237 if (parse_host_port(haddr, host_str) < 0)
238 goto fail;
240 src_str2 = src_str;
241 if (!src_str || *src_str == '\0')
242 src_str2 = ":0";
244 if (parse_host_port(saddr, src_str2) < 0)
245 goto fail;
247 free(str);
248 return(0);
250 fail:
251 free(str);
252 return -1;
255 int parse_host_port(struct sockaddr_in *saddr, const char *str)
257 char buf[512];
258 struct hostent *he;
259 const char *p, *r;
260 int port;
262 p = str;
263 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
264 return -1;
265 saddr->sin_family = AF_INET;
266 if (buf[0] == '\0') {
267 saddr->sin_addr.s_addr = 0;
268 } else {
269 if (qemu_isdigit(buf[0])) {
270 if (!inet_aton(buf, &saddr->sin_addr))
271 return -1;
272 } else {
273 if ((he = gethostbyname(buf)) == NULL)
274 return - 1;
275 saddr->sin_addr = *(struct in_addr *)he->h_addr;
278 port = strtol(p, (char **)&r, 0);
279 if (r == p)
280 return -1;
281 saddr->sin_port = htons(port);
282 return 0;
285 #if !defined(_WIN32) && 0
286 static int parse_unix_path(struct sockaddr_un *uaddr, const char *str)
288 const char *p;
289 int len;
291 len = MIN(108, strlen(str));
292 p = strchr(str, ',');
293 if (p)
294 len = MIN(len, p - str);
296 memset(uaddr, 0, sizeof(*uaddr));
298 uaddr->sun_family = AF_UNIX;
299 memcpy(uaddr->sun_path, str, len);
301 return 0;
303 #endif
305 void qemu_format_nic_info_str(VLANClientState *vc, uint8_t macaddr[6])
307 snprintf(vc->info_str, sizeof(vc->info_str),
308 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
309 vc->model,
310 macaddr[0], macaddr[1], macaddr[2],
311 macaddr[3], macaddr[4], macaddr[5]);
314 static char *assign_name(VLANClientState *vc1, const char *model)
316 VLANState *vlan;
317 char buf[256];
318 int id = 0;
320 for (vlan = first_vlan; vlan; vlan = vlan->next) {
321 VLANClientState *vc;
323 for (vc = vlan->first_client; vc; vc = vc->next)
324 if (vc != vc1 && strcmp(vc->model, model) == 0)
325 id++;
328 snprintf(buf, sizeof(buf), "%s.%d", model, id);
330 return strdup(buf);
333 VLANClientState *qemu_new_vlan_client(VLANState *vlan,
334 const char *model,
335 const char *name,
336 IOReadHandler *fd_read,
337 IOCanRWHandler *fd_can_read,
338 NetCleanup *cleanup,
339 void *opaque)
341 VLANClientState *vc, **pvc;
342 vc = qemu_mallocz(sizeof(VLANClientState));
343 vc->model = strdup(model);
344 if (name)
345 vc->name = strdup(name);
346 else
347 vc->name = assign_name(vc, model);
348 vc->fd_read = fd_read;
349 vc->fd_can_read = fd_can_read;
350 vc->cleanup = cleanup;
351 vc->opaque = opaque;
352 vc->vlan = vlan;
354 vc->next = NULL;
355 pvc = &vlan->first_client;
356 while (*pvc != NULL)
357 pvc = &(*pvc)->next;
358 *pvc = vc;
359 return vc;
362 void qemu_del_vlan_client(VLANClientState *vc)
364 VLANClientState **pvc = &vc->vlan->first_client;
366 while (*pvc != NULL)
367 if (*pvc == vc) {
368 *pvc = vc->next;
369 if (vc->cleanup) {
370 vc->cleanup(vc);
372 free(vc->name);
373 free(vc->model);
374 qemu_free(vc);
375 break;
376 } else
377 pvc = &(*pvc)->next;
380 VLANClientState *qemu_find_vlan_client(VLANState *vlan, void *opaque)
382 VLANClientState **pvc = &vlan->first_client;
384 while (*pvc != NULL)
385 if ((*pvc)->opaque == opaque)
386 return *pvc;
387 else
388 pvc = &(*pvc)->next;
390 return NULL;
393 int qemu_can_send_packet(VLANClientState *vc1)
395 VLANState *vlan = vc1->vlan;
396 VLANClientState *vc;
398 for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
399 if (vc != vc1) {
400 if (vc->fd_can_read && vc->fd_can_read(vc->opaque))
401 return 1;
404 return 0;
407 static int
408 qemu_deliver_packet(VLANClientState *sender, const uint8_t *buf, int size)
410 VLANClientState *vc;
411 int ret = -EAGAIN;
413 for (vc = sender->vlan->first_client; vc != NULL; vc = vc->next) {
414 if (vc != sender) {
415 if (vc->link_down) {
416 ret = 0;
417 } else if (!vc->fd_can_read || vc->fd_can_read(vc->opaque)) {
418 vc->fd_read(vc->opaque, buf, size);
419 ret = 0;
423 return ret;
426 int qemu_send_packet(VLANClientState *vc, const uint8_t *buf, int size)
428 VLANState *vlan = vc->vlan;
429 VLANPacket *packet;
430 int ret = -EAGAIN;
432 if (vc->link_down)
433 return 0;
435 #ifdef DEBUG_NET
436 printf("vlan %d send:\n", vlan->id);
437 hex_dump(stdout, buf, size);
438 #endif
439 if (vlan->delivering) {
440 packet = qemu_malloc(sizeof(VLANPacket) + size);
441 packet->next = vlan->send_queue;
442 packet->sender = vc;
443 packet->size = size;
444 memcpy(packet->data, buf, size);
445 vlan->send_queue = packet;
446 } else {
447 vlan->delivering = 1;
448 ret = qemu_deliver_packet(vc, buf, size);
449 while ((packet = vlan->send_queue) != NULL) {
450 qemu_deliver_packet(packet->sender, packet->data, packet->size);
451 vlan->send_queue = packet->next;
452 qemu_free(packet);
454 vlan->delivering = 0;
456 return ret;
459 static ssize_t vc_sendv_compat(VLANClientState *vc, const struct iovec *iov,
460 int iovcnt)
462 uint8_t buffer[4096];
463 size_t offset = 0;
464 int i;
466 for (i = 0; i < iovcnt; i++) {
467 size_t len;
469 len = MIN(sizeof(buffer) - offset, iov[i].iov_len);
470 memcpy(buffer + offset, iov[i].iov_base, len);
471 offset += len;
474 vc->fd_read(vc->opaque, buffer, offset);
476 return offset;
479 static ssize_t calc_iov_length(const struct iovec *iov, int iovcnt)
481 size_t offset = 0;
482 int i;
484 for (i = 0; i < iovcnt; i++)
485 offset += iov[i].iov_len;
486 return offset;
489 ssize_t qemu_sendv_packet(VLANClientState *vc1, const struct iovec *iov,
490 int iovcnt)
492 VLANState *vlan = vc1->vlan;
493 VLANClientState *vc;
494 ssize_t max_len = 0;
496 if (vc1->link_down)
497 return calc_iov_length(iov, iovcnt);
499 for (vc = vlan->first_client; vc != NULL; vc = vc->next) {
500 ssize_t len = 0;
502 if (vc == vc1)
503 continue;
505 if (vc->link_down)
506 len = calc_iov_length(iov, iovcnt);
507 if (vc->fd_readv)
508 len = vc->fd_readv(vc->opaque, iov, iovcnt);
509 else if (vc->fd_read)
510 len = vc_sendv_compat(vc, iov, iovcnt);
512 max_len = MAX(max_len, len);
515 return max_len;
518 #if defined(CONFIG_SLIRP)
520 /* slirp network adapter */
522 static int slirp_inited;
523 static int slirp_restrict;
524 static char *slirp_ip;
525 static VLANClientState *slirp_vc;
527 int slirp_can_output(void)
529 return !slirp_vc || qemu_can_send_packet(slirp_vc);
532 void slirp_output(const uint8_t *pkt, int pkt_len)
534 #ifdef DEBUG_SLIRP
535 printf("slirp output:\n");
536 hex_dump(stdout, pkt, pkt_len);
537 #endif
538 if (!slirp_vc)
539 return;
540 qemu_send_packet(slirp_vc, pkt, pkt_len);
543 int slirp_is_inited(void)
545 return slirp_inited;
548 static void slirp_receive(void *opaque, const uint8_t *buf, int size)
550 #ifdef DEBUG_SLIRP
551 printf("slirp input:\n");
552 hex_dump(stdout, buf, size);
553 #endif
554 slirp_input(buf, size);
557 static int slirp_in_use;
559 static void net_slirp_cleanup(VLANClientState *vc)
561 slirp_in_use = 0;
564 static int net_slirp_init(VLANState *vlan, const char *model, const char *name)
566 if (slirp_in_use) {
567 /* slirp only supports a single instance so far */
568 return -1;
570 if (!slirp_inited) {
571 slirp_inited = 1;
572 slirp_init(slirp_restrict, slirp_ip);
574 slirp_vc = qemu_new_vlan_client(vlan, model, name,
575 slirp_receive, NULL, net_slirp_cleanup, NULL);
576 slirp_vc->info_str[0] = '\0';
577 slirp_in_use = 1;
578 return 0;
581 void net_slirp_redir(Monitor *mon, const char *redir_str)
583 int is_udp;
584 char buf[256], *r;
585 const char *p, *errmsg;
586 struct in_addr guest_addr;
587 int host_port, guest_port;
589 if (!slirp_inited) {
590 slirp_inited = 1;
591 slirp_init(slirp_restrict, slirp_ip);
594 p = redir_str;
595 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
596 goto fail_syntax;
597 if (!strcmp(buf, "tcp") || buf[0] == '\0') {
598 is_udp = 0;
599 } else if (!strcmp(buf, "udp")) {
600 is_udp = 1;
601 } else {
602 goto fail_syntax;
605 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
606 goto fail_syntax;
607 host_port = strtol(buf, &r, 0);
608 if (r == buf)
609 goto fail_syntax;
611 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
612 goto fail_syntax;
613 if (buf[0] == '\0') {
614 pstrcpy(buf, sizeof(buf), "10.0.2.15");
616 if (!inet_aton(buf, &guest_addr))
617 goto fail_syntax;
619 guest_port = strtol(p, &r, 0);
620 if (r == p)
621 goto fail_syntax;
623 if (slirp_redir(is_udp, host_port, guest_addr, guest_port) < 0) {
624 errmsg = "could not set up redirection\n";
625 goto fail;
627 return;
629 fail_syntax:
630 errmsg = "invalid redirection format\n";
631 fail:
632 if (mon) {
633 monitor_printf(mon, "%s", errmsg);
634 } else {
635 fprintf(stderr, "qemu: %s", errmsg);
636 exit(1);
640 #ifndef _WIN32
642 static char smb_dir[1024];
644 static void erase_dir(char *dir_name)
646 DIR *d;
647 struct dirent *de;
648 char filename[1024];
650 /* erase all the files in the directory */
651 if ((d = opendir(dir_name)) != NULL) {
652 for(;;) {
653 de = readdir(d);
654 if (!de)
655 break;
656 if (strcmp(de->d_name, ".") != 0 &&
657 strcmp(de->d_name, "..") != 0) {
658 snprintf(filename, sizeof(filename), "%s/%s",
659 smb_dir, de->d_name);
660 if (unlink(filename) != 0) /* is it a directory? */
661 erase_dir(filename);
664 closedir(d);
665 rmdir(dir_name);
669 /* automatic user mode samba server configuration */
670 static void smb_exit(void)
672 erase_dir(smb_dir);
675 /* automatic user mode samba server configuration */
676 void net_slirp_smb(const char *exported_dir)
678 char smb_conf[1024];
679 char smb_cmdline[1024];
680 FILE *f;
682 if (!slirp_inited) {
683 slirp_inited = 1;
684 slirp_init(slirp_restrict, slirp_ip);
687 /* XXX: better tmp dir construction */
688 snprintf(smb_dir, sizeof(smb_dir), "/tmp/qemu-smb.%ld", (long)getpid());
689 if (mkdir(smb_dir, 0700) < 0) {
690 fprintf(stderr, "qemu: could not create samba server dir '%s'\n", smb_dir);
691 exit(1);
693 snprintf(smb_conf, sizeof(smb_conf), "%s/%s", smb_dir, "smb.conf");
695 f = fopen(smb_conf, "w");
696 if (!f) {
697 fprintf(stderr, "qemu: could not create samba server configuration file '%s'\n", smb_conf);
698 exit(1);
700 fprintf(f,
701 "[global]\n"
702 "private dir=%s\n"
703 "smb ports=0\n"
704 "socket address=127.0.0.1\n"
705 "pid directory=%s\n"
706 "lock directory=%s\n"
707 "log file=%s/log.smbd\n"
708 "smb passwd file=%s/smbpasswd\n"
709 "security = share\n"
710 "[qemu]\n"
711 "path=%s\n"
712 "read only=no\n"
713 "guest ok=yes\n",
714 smb_dir,
715 smb_dir,
716 smb_dir,
717 smb_dir,
718 smb_dir,
719 exported_dir
721 fclose(f);
722 atexit(smb_exit);
724 snprintf(smb_cmdline, sizeof(smb_cmdline), "%s -s %s",
725 SMBD_COMMAND, smb_conf);
727 slirp_add_exec(0, smb_cmdline, 4, 139);
730 #endif /* !defined(_WIN32) */
731 void do_info_slirp(Monitor *mon)
733 slirp_stats();
736 struct VMChannel {
737 CharDriverState *hd;
738 int port;
741 static int vmchannel_can_read(void *opaque)
743 struct VMChannel *vmc = (struct VMChannel*)opaque;
744 return slirp_socket_can_recv(4, vmc->port);
747 static void vmchannel_read(void *opaque, const uint8_t *buf, int size)
749 struct VMChannel *vmc = (struct VMChannel*)opaque;
750 slirp_socket_recv(4, vmc->port, buf, size);
753 #endif /* CONFIG_SLIRP */
755 #ifdef _WIN32
757 int tap_has_vnet_hdr(void *opaque)
759 return 0;
762 void tap_using_vnet_hdr(void *opaque, int using_vnet_hdr)
766 #else /* !defined(_WIN32) */
768 /* Maximum GSO packet size (64k) plus plenty of room for
769 * the ethernet and virtio_net headers
771 #define TAP_BUFSIZE (4096 + 65536)
773 #ifdef IFF_VNET_HDR
774 #include <linux/virtio_net.h>
775 #endif
777 typedef struct TAPState {
778 VLANClientState *vc;
779 int fd;
780 char down_script[1024];
781 char down_script_arg[128];
782 char buf[TAP_BUFSIZE];
783 int size;
784 unsigned int has_vnet_hdr : 1;
785 unsigned int using_vnet_hdr : 1;
786 } TAPState;
788 static int launch_script(const char *setup_script, const char *ifname, int fd);
790 static ssize_t tap_receive_iov(void *opaque, const struct iovec *iov,
791 int iovcnt)
793 TAPState *s = opaque;
794 ssize_t len;
796 do {
797 len = writev(s->fd, iov, iovcnt);
798 } while (len == -1 && (errno == EINTR || errno == EAGAIN));
800 return len;
803 static void tap_receive(void *opaque, const uint8_t *buf, int size)
805 struct iovec iov[2];
806 int i = 0;
808 #ifdef IFF_VNET_HDR
809 TAPState *s = opaque;
810 struct virtio_net_hdr hdr = { 0, };
812 if (s->has_vnet_hdr && !s->using_vnet_hdr) {
813 iov[i].iov_base = &hdr;
814 iov[i].iov_len = sizeof(hdr);
815 i++;
817 #endif
819 iov[i].iov_base = (char *) buf;
820 iov[i].iov_len = size;
821 i++;
823 tap_receive_iov(opaque, iov, i);
826 static int tap_can_send(void *opaque)
828 TAPState *s = opaque;
829 VLANClientState *vc;
830 int can_receive = 0;
832 /* Check to see if any of our clients can receive a packet */
833 for (vc = s->vc->vlan->first_client; vc; vc = vc->next) {
834 /* Skip ourselves */
835 if (vc == s->vc)
836 continue;
838 if (!vc->fd_can_read) {
839 /* no fd_can_read handler, they always can receive */
840 can_receive = 1;
841 } else
842 can_receive = vc->fd_can_read(vc->opaque);
844 /* Once someone can receive, we try to send a packet */
845 if (can_receive)
846 break;
849 return can_receive;
852 static int tap_send_packet(TAPState *s)
854 uint8_t *buf = (uint8_t *)s->buf;
855 int size = s->size;
857 #ifdef IFF_VNET_HDR
858 if (s->has_vnet_hdr && !s->using_vnet_hdr) {
859 buf += sizeof(struct virtio_net_hdr);
860 size -= sizeof(struct virtio_net_hdr);
862 #endif
864 return qemu_send_packet(s->vc, buf, size);
867 static void tap_send(void *opaque)
869 TAPState *s = opaque;
871 /* First try to send any buffered packet */
872 if (s->size > 0) {
873 int err;
875 /* If noone can receive the packet, buffer it */
876 err = tap_send_packet(s);
877 if (err == -EAGAIN)
878 return;
881 /* Read packets until we hit EAGAIN */
882 do {
883 #ifdef __sun__
884 struct strbuf sbuf;
885 int f = 0;
886 sbuf.maxlen = sizeof(buf);
887 sbuf.buf = (char *)buf;
888 s->size = getmsg(s->fd, NULL, &sbuf, &f) >=0 ? sbuf.len : -1;
889 #else
890 // FIXME: kvm_sleep_begin();
891 s->size = read(s->fd, s->buf, sizeof(s->buf));
892 // FIXME: kvm_sleep_end();
893 #endif
895 if (s->size == -1 && errno == EINTR)
896 continue;
898 if (s->size > 0) {
899 int err;
901 /* If noone can receive the packet, buffer it */
902 err = tap_send_packet(s);
903 if (err == -EAGAIN)
904 break;
906 } while (s->size > 0);
909 int tap_has_vnet_hdr(void *opaque)
911 VLANClientState *vc = opaque;
912 TAPState *s = vc->opaque;
914 return s ? s->has_vnet_hdr : 0;
917 void tap_using_vnet_hdr(void *opaque, int using_vnet_hdr)
919 VLANClientState *vc = opaque;
920 TAPState *s = vc->opaque;
922 if (!s || !s->has_vnet_hdr)
923 return;
925 s->using_vnet_hdr = using_vnet_hdr != 0;
928 static int tap_probe_vnet_hdr(int fd)
930 #if defined(TUNGETIFF) && defined(IFF_VNET_HDR)
931 struct ifreq ifr;
933 if (ioctl(fd, TUNGETIFF, &ifr) != 0) {
934 fprintf(stderr, "TUNGETIFF ioctl() failed: %s\n", strerror(errno));
935 return 0;
938 return ifr.ifr_flags & IFF_VNET_HDR;
939 #else
940 return 0;
941 #endif
944 #ifdef TUNSETOFFLOAD
945 static void tap_set_offload(VLANClientState *vc, int csum, int tso4, int tso6,
946 int ecn)
948 TAPState *s = vc->opaque;
949 unsigned int offload = 0;
951 if (csum) {
952 offload |= TUN_F_CSUM;
953 if (tso4)
954 offload |= TUN_F_TSO4;
955 if (tso6)
956 offload |= TUN_F_TSO6;
957 if ((tso4 || tso6) && ecn)
958 offload |= TUN_F_TSO_ECN;
961 if (ioctl(s->fd, TUNSETOFFLOAD, offload) != 0)
962 fprintf(stderr, "TUNSETOFFLOAD ioctl() failed: %s\n",
963 strerror(errno));
965 #endif /* TUNSETOFFLOAD */
967 static void tap_cleanup(VLANClientState *vc)
969 TAPState *s = vc->opaque;
971 if (s->down_script[0])
972 launch_script(s->down_script, s->down_script_arg, s->fd);
974 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
975 close(s->fd);
976 qemu_free(s);
979 /* fd support */
981 static TAPState *net_tap_fd_init(VLANState *vlan,
982 const char *model,
983 const char *name,
984 int fd,
985 int vnet_hdr)
987 TAPState *s;
989 s = qemu_mallocz(sizeof(TAPState));
990 s->fd = fd;
991 s->has_vnet_hdr = vnet_hdr != 0;
992 s->vc = qemu_new_vlan_client(vlan, model, name, tap_receive,
993 NULL, tap_cleanup, s);
994 s->vc->fd_readv = tap_receive_iov;
995 #ifdef TUNSETOFFLOAD
996 s->vc->set_offload = tap_set_offload;
997 tap_set_offload(s->vc, 0, 0, 0, 0);
998 #endif
999 qemu_set_fd_handler2(s->fd, tap_can_send, tap_send, NULL, s);
1000 snprintf(s->vc->info_str, sizeof(s->vc->info_str), "fd=%d", fd);
1001 return s;
1004 #if defined (HOST_BSD) || defined (__FreeBSD_kernel__)
1005 static int tap_open(char *ifname, int ifname_size)
1007 int fd;
1008 char *dev;
1009 struct stat s;
1011 TFR(fd = open("/dev/tap", O_RDWR));
1012 if (fd < 0) {
1013 fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n");
1014 return -1;
1017 fstat(fd, &s);
1018 dev = devname(s.st_rdev, S_IFCHR);
1019 pstrcpy(ifname, ifname_size, dev);
1021 fcntl(fd, F_SETFL, O_NONBLOCK);
1022 return fd;
1024 #elif defined(__sun__)
1025 #define TUNNEWPPA (('T'<<16) | 0x0001)
1027 * Allocate TAP device, returns opened fd.
1028 * Stores dev name in the first arg(must be large enough).
1030 static int tap_alloc(char *dev, size_t dev_size)
1032 int tap_fd, if_fd, ppa = -1;
1033 static int ip_fd = 0;
1034 char *ptr;
1036 static int arp_fd = 0;
1037 int ip_muxid, arp_muxid;
1038 struct strioctl strioc_if, strioc_ppa;
1039 int link_type = I_PLINK;;
1040 struct lifreq ifr;
1041 char actual_name[32] = "";
1043 memset(&ifr, 0x0, sizeof(ifr));
1045 if( *dev ){
1046 ptr = dev;
1047 while( *ptr && !qemu_isdigit((int)*ptr) ) ptr++;
1048 ppa = atoi(ptr);
1051 /* Check if IP device was opened */
1052 if( ip_fd )
1053 close(ip_fd);
1055 TFR(ip_fd = open("/dev/udp", O_RDWR, 0));
1056 if (ip_fd < 0) {
1057 syslog(LOG_ERR, "Can't open /dev/ip (actually /dev/udp)");
1058 return -1;
1061 TFR(tap_fd = open("/dev/tap", O_RDWR, 0));
1062 if (tap_fd < 0) {
1063 syslog(LOG_ERR, "Can't open /dev/tap");
1064 return -1;
1067 /* Assign a new PPA and get its unit number. */
1068 strioc_ppa.ic_cmd = TUNNEWPPA;
1069 strioc_ppa.ic_timout = 0;
1070 strioc_ppa.ic_len = sizeof(ppa);
1071 strioc_ppa.ic_dp = (char *)&ppa;
1072 if ((ppa = ioctl (tap_fd, I_STR, &strioc_ppa)) < 0)
1073 syslog (LOG_ERR, "Can't assign new interface");
1075 TFR(if_fd = open("/dev/tap", O_RDWR, 0));
1076 if (if_fd < 0) {
1077 syslog(LOG_ERR, "Can't open /dev/tap (2)");
1078 return -1;
1080 if(ioctl(if_fd, I_PUSH, "ip") < 0){
1081 syslog(LOG_ERR, "Can't push IP module");
1082 return -1;
1085 if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0)
1086 syslog(LOG_ERR, "Can't get flags\n");
1088 snprintf (actual_name, 32, "tap%d", ppa);
1089 pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name);
1091 ifr.lifr_ppa = ppa;
1092 /* Assign ppa according to the unit number returned by tun device */
1094 if (ioctl (if_fd, SIOCSLIFNAME, &ifr) < 0)
1095 syslog (LOG_ERR, "Can't set PPA %d", ppa);
1096 if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) <0)
1097 syslog (LOG_ERR, "Can't get flags\n");
1098 /* Push arp module to if_fd */
1099 if (ioctl (if_fd, I_PUSH, "arp") < 0)
1100 syslog (LOG_ERR, "Can't push ARP module (2)");
1102 /* Push arp module to ip_fd */
1103 if (ioctl (ip_fd, I_POP, NULL) < 0)
1104 syslog (LOG_ERR, "I_POP failed\n");
1105 if (ioctl (ip_fd, I_PUSH, "arp") < 0)
1106 syslog (LOG_ERR, "Can't push ARP module (3)\n");
1107 /* Open arp_fd */
1108 TFR(arp_fd = open ("/dev/tap", O_RDWR, 0));
1109 if (arp_fd < 0)
1110 syslog (LOG_ERR, "Can't open %s\n", "/dev/tap");
1112 /* Set ifname to arp */
1113 strioc_if.ic_cmd = SIOCSLIFNAME;
1114 strioc_if.ic_timout = 0;
1115 strioc_if.ic_len = sizeof(ifr);
1116 strioc_if.ic_dp = (char *)&ifr;
1117 if (ioctl(arp_fd, I_STR, &strioc_if) < 0){
1118 syslog (LOG_ERR, "Can't set ifname to arp\n");
1121 if((ip_muxid = ioctl(ip_fd, I_LINK, if_fd)) < 0){
1122 syslog(LOG_ERR, "Can't link TAP device to IP");
1123 return -1;
1126 if ((arp_muxid = ioctl (ip_fd, link_type, arp_fd)) < 0)
1127 syslog (LOG_ERR, "Can't link TAP device to ARP");
1129 close (if_fd);
1131 memset(&ifr, 0x0, sizeof(ifr));
1132 pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name);
1133 ifr.lifr_ip_muxid = ip_muxid;
1134 ifr.lifr_arp_muxid = arp_muxid;
1136 if (ioctl (ip_fd, SIOCSLIFMUXID, &ifr) < 0)
1138 ioctl (ip_fd, I_PUNLINK , arp_muxid);
1139 ioctl (ip_fd, I_PUNLINK, ip_muxid);
1140 syslog (LOG_ERR, "Can't set multiplexor id");
1143 snprintf(dev, dev_size, "tap%d", ppa);
1144 return tap_fd;
1147 static int tap_open(char *ifname, int ifname_size, int *vnet_hdr)
1149 char dev[10]="";
1150 int fd;
1151 if( (fd = tap_alloc(dev, sizeof(dev))) < 0 ){
1152 fprintf(stderr, "Cannot allocate TAP device\n");
1153 return -1;
1155 pstrcpy(ifname, ifname_size, dev);
1156 fcntl(fd, F_SETFL, O_NONBLOCK);
1157 return fd;
1159 #elif defined (_AIX)
1160 static int tap_open(char *ifname, int ifname_size)
1162 fprintf (stderr, "no tap on AIX\n");
1163 return -1;
1165 #else
1166 static int tap_open(char *ifname, int ifname_size, int *vnet_hdr)
1168 struct ifreq ifr;
1169 int fd, ret;
1171 TFR(fd = open("/dev/net/tun", O_RDWR));
1172 if (fd < 0) {
1173 fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
1174 return -1;
1176 memset(&ifr, 0, sizeof(ifr));
1177 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
1179 #if defined(TUNGETFEATURES) && defined(IFF_VNET_HDR)
1181 unsigned int features;
1183 if (ioctl(fd, TUNGETFEATURES, &features) == 0 &&
1184 features & IFF_VNET_HDR) {
1185 *vnet_hdr = 1;
1186 ifr.ifr_flags |= IFF_VNET_HDR;
1189 #endif
1191 if (ifname[0] != '\0')
1192 pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname);
1193 else
1194 pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d");
1195 ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
1196 if (ret != 0) {
1197 fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
1198 close(fd);
1199 return -1;
1201 pstrcpy(ifname, ifname_size, ifr.ifr_name);
1202 fcntl(fd, F_SETFL, O_NONBLOCK);
1203 return fd;
1205 #endif
1207 static int launch_script(const char *setup_script, const char *ifname, int fd)
1209 int pid, status;
1210 char *args[3];
1211 char **parg;
1213 /* try to launch network script */
1214 pid = fork();
1215 if (pid >= 0) {
1216 if (pid == 0) {
1217 int open_max = sysconf (_SC_OPEN_MAX), i;
1218 for (i = 0; i < open_max; i++)
1219 if (i != STDIN_FILENO &&
1220 i != STDOUT_FILENO &&
1221 i != STDERR_FILENO &&
1222 i != fd)
1223 close(i);
1225 parg = args;
1226 *parg++ = (char *)setup_script;
1227 *parg++ = (char *)ifname;
1228 *parg++ = NULL;
1229 execv(setup_script, args);
1230 _exit(1);
1232 while (waitpid(pid, &status, 0) != pid);
1233 if (!WIFEXITED(status) ||
1234 WEXITSTATUS(status) != 0) {
1235 fprintf(stderr, "%s: could not launch network script\n",
1236 setup_script);
1237 return -1;
1240 return 0;
1243 static int net_tap_init(VLANState *vlan, const char *model,
1244 const char *name, const char *ifname1,
1245 const char *setup_script, const char *down_script)
1247 TAPState *s;
1248 int fd;
1249 int vnet_hdr;
1250 char ifname[128];
1252 if (ifname1 != NULL)
1253 pstrcpy(ifname, sizeof(ifname), ifname1);
1254 else
1255 ifname[0] = '\0';
1256 vnet_hdr = 0;
1257 TFR(fd = tap_open(ifname, sizeof(ifname), &vnet_hdr));
1258 if (fd < 0)
1259 return -1;
1261 if (!setup_script || !strcmp(setup_script, "no"))
1262 setup_script = "";
1263 if (setup_script[0] != '\0') {
1264 if (launch_script(setup_script, ifname, fd))
1265 return -1;
1267 s = net_tap_fd_init(vlan, model, name, fd, vnet_hdr);
1268 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1269 "ifname=%s,script=%s,downscript=%s",
1270 ifname, setup_script, down_script);
1271 if (down_script && strcmp(down_script, "no")) {
1272 snprintf(s->down_script, sizeof(s->down_script), "%s", down_script);
1273 snprintf(s->down_script_arg, sizeof(s->down_script_arg), "%s", ifname);
1275 return 0;
1278 #endif /* !_WIN32 */
1280 #if defined(CONFIG_VDE)
1281 typedef struct VDEState {
1282 VLANClientState *vc;
1283 VDECONN *vde;
1284 } VDEState;
1286 static void vde_to_qemu(void *opaque)
1288 VDEState *s = opaque;
1289 uint8_t buf[4096];
1290 int size;
1292 size = vde_recv(s->vde, (char *)buf, sizeof(buf), 0);
1293 if (size > 0) {
1294 qemu_send_packet(s->vc, buf, size);
1298 static void vde_from_qemu(void *opaque, const uint8_t *buf, int size)
1300 VDEState *s = opaque;
1301 int ret;
1302 for(;;) {
1303 ret = vde_send(s->vde, (const char *)buf, size, 0);
1304 if (ret < 0 && errno == EINTR) {
1305 } else {
1306 break;
1311 static void vde_cleanup(VLANClientState *vc)
1313 VDEState *s = vc->opaque;
1314 qemu_set_fd_handler(vde_datafd(s->vde), NULL, NULL, NULL);
1315 vde_close(s->vde);
1316 qemu_free(s);
1319 static int net_vde_init(VLANState *vlan, const char *model,
1320 const char *name, const char *sock,
1321 int port, const char *group, int mode)
1323 VDEState *s;
1324 char *init_group = strlen(group) ? (char *)group : NULL;
1325 char *init_sock = strlen(sock) ? (char *)sock : NULL;
1327 struct vde_open_args args = {
1328 .port = port,
1329 .group = init_group,
1330 .mode = mode,
1333 s = qemu_mallocz(sizeof(VDEState));
1334 s->vde = vde_open(init_sock, (char *)"QEMU", &args);
1335 if (!s->vde){
1336 free(s);
1337 return -1;
1339 s->vc = qemu_new_vlan_client(vlan, model, name, vde_from_qemu,
1340 NULL, vde_cleanup, s);
1341 qemu_set_fd_handler(vde_datafd(s->vde), vde_to_qemu, NULL, s);
1342 snprintf(s->vc->info_str, sizeof(s->vc->info_str), "sock=%s,fd=%d",
1343 sock, vde_datafd(s->vde));
1344 return 0;
1346 #endif
1348 /* network connection */
1349 typedef struct NetSocketState {
1350 VLANClientState *vc;
1351 int fd;
1352 int state; /* 0 = getting length, 1 = getting data */
1353 unsigned int index;
1354 unsigned int packet_len;
1355 uint8_t buf[4096];
1356 struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
1357 } NetSocketState;
1359 typedef struct NetSocketListenState {
1360 VLANState *vlan;
1361 char *model;
1362 char *name;
1363 int fd;
1364 } NetSocketListenState;
1366 /* XXX: we consider we can send the whole packet without blocking */
1367 static void net_socket_receive(void *opaque, const uint8_t *buf, int size)
1369 NetSocketState *s = opaque;
1370 uint32_t len;
1371 len = htonl(size);
1373 send_all(s->fd, (const uint8_t *)&len, sizeof(len));
1374 send_all(s->fd, buf, size);
1377 static void net_socket_receive_dgram(void *opaque, const uint8_t *buf, int size)
1379 NetSocketState *s = opaque;
1380 sendto(s->fd, buf, size, 0,
1381 (struct sockaddr *)&s->dgram_dst, sizeof(s->dgram_dst));
1384 static void net_socket_send(void *opaque)
1386 NetSocketState *s = opaque;
1387 int size, err;
1388 unsigned l;
1389 uint8_t buf1[4096];
1390 const uint8_t *buf;
1392 size = recv(s->fd, buf1, sizeof(buf1), 0);
1393 if (size < 0) {
1394 err = socket_error();
1395 if (err != EWOULDBLOCK)
1396 goto eoc;
1397 } else if (size == 0) {
1398 /* end of connection */
1399 eoc:
1400 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1401 closesocket(s->fd);
1402 return;
1404 buf = buf1;
1405 while (size > 0) {
1406 /* reassemble a packet from the network */
1407 switch(s->state) {
1408 case 0:
1409 l = 4 - s->index;
1410 if (l > size)
1411 l = size;
1412 memcpy(s->buf + s->index, buf, l);
1413 buf += l;
1414 size -= l;
1415 s->index += l;
1416 if (s->index == 4) {
1417 /* got length */
1418 s->packet_len = ntohl(*(uint32_t *)s->buf);
1419 s->index = 0;
1420 s->state = 1;
1422 break;
1423 case 1:
1424 l = s->packet_len - s->index;
1425 if (l > size)
1426 l = size;
1427 if (s->index + l <= sizeof(s->buf)) {
1428 memcpy(s->buf + s->index, buf, l);
1429 } else {
1430 fprintf(stderr, "serious error: oversized packet received,"
1431 "connection terminated.\n");
1432 s->state = 0;
1433 goto eoc;
1436 s->index += l;
1437 buf += l;
1438 size -= l;
1439 if (s->index >= s->packet_len) {
1440 qemu_send_packet(s->vc, s->buf, s->packet_len);
1441 s->index = 0;
1442 s->state = 0;
1444 break;
1449 static void net_socket_send_dgram(void *opaque)
1451 NetSocketState *s = opaque;
1452 int size;
1454 size = recv(s->fd, s->buf, sizeof(s->buf), 0);
1455 if (size < 0)
1456 return;
1457 if (size == 0) {
1458 /* end of connection */
1459 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1460 return;
1462 qemu_send_packet(s->vc, s->buf, size);
1465 static int net_socket_mcast_create(struct sockaddr_in *mcastaddr)
1467 struct ip_mreq imr;
1468 int fd;
1469 int val, ret;
1470 if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) {
1471 fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
1472 inet_ntoa(mcastaddr->sin_addr),
1473 (int)ntohl(mcastaddr->sin_addr.s_addr));
1474 return -1;
1477 fd = socket(PF_INET, SOCK_DGRAM, 0);
1478 if (fd < 0) {
1479 perror("socket(PF_INET, SOCK_DGRAM)");
1480 return -1;
1483 val = 1;
1484 ret=setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
1485 (const char *)&val, sizeof(val));
1486 if (ret < 0) {
1487 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
1488 goto fail;
1491 ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr));
1492 if (ret < 0) {
1493 perror("bind");
1494 goto fail;
1497 /* Add host to multicast group */
1498 imr.imr_multiaddr = mcastaddr->sin_addr;
1499 imr.imr_interface.s_addr = htonl(INADDR_ANY);
1501 ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1502 (const char *)&imr, sizeof(struct ip_mreq));
1503 if (ret < 0) {
1504 perror("setsockopt(IP_ADD_MEMBERSHIP)");
1505 goto fail;
1508 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
1509 val = 1;
1510 ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
1511 (const char *)&val, sizeof(val));
1512 if (ret < 0) {
1513 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
1514 goto fail;
1517 socket_set_nonblock(fd);
1518 return fd;
1519 fail:
1520 if (fd >= 0)
1521 closesocket(fd);
1522 return -1;
1525 static void net_socket_cleanup(VLANClientState *vc)
1527 NetSocketState *s = vc->opaque;
1528 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1529 close(s->fd);
1530 qemu_free(s);
1533 static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan,
1534 const char *model,
1535 const char *name,
1536 int fd, int is_connected)
1538 struct sockaddr_in saddr;
1539 int newfd;
1540 socklen_t saddr_len;
1541 NetSocketState *s;
1543 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
1544 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
1545 * by ONLY ONE process: we must "clone" this dgram socket --jjo
1548 if (is_connected) {
1549 if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) {
1550 /* must be bound */
1551 if (saddr.sin_addr.s_addr==0) {
1552 fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
1553 fd);
1554 return NULL;
1556 /* clone dgram socket */
1557 newfd = net_socket_mcast_create(&saddr);
1558 if (newfd < 0) {
1559 /* error already reported by net_socket_mcast_create() */
1560 close(fd);
1561 return NULL;
1563 /* clone newfd to fd, close newfd */
1564 dup2(newfd, fd);
1565 close(newfd);
1567 } else {
1568 fprintf(stderr, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
1569 fd, strerror(errno));
1570 return NULL;
1574 s = qemu_mallocz(sizeof(NetSocketState));
1575 s->fd = fd;
1577 s->vc = qemu_new_vlan_client(vlan, model, name, net_socket_receive_dgram,
1578 NULL, net_socket_cleanup, s);
1579 qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s);
1581 /* mcast: save bound address as dst */
1582 if (is_connected) s->dgram_dst=saddr;
1584 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1585 "socket: fd=%d (%s mcast=%s:%d)",
1586 fd, is_connected? "cloned" : "",
1587 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1588 return s;
1591 static void net_socket_connect(void *opaque)
1593 NetSocketState *s = opaque;
1594 qemu_set_fd_handler(s->fd, net_socket_send, NULL, s);
1597 static NetSocketState *net_socket_fd_init_stream(VLANState *vlan,
1598 const char *model,
1599 const char *name,
1600 int fd, int is_connected)
1602 NetSocketState *s;
1603 s = qemu_mallocz(sizeof(NetSocketState));
1604 s->fd = fd;
1605 s->vc = qemu_new_vlan_client(vlan, model, name, net_socket_receive,
1606 NULL, net_socket_cleanup, s);
1607 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1608 "socket: fd=%d", fd);
1609 if (is_connected) {
1610 net_socket_connect(s);
1611 } else {
1612 qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s);
1614 return s;
1617 static NetSocketState *net_socket_fd_init(VLANState *vlan,
1618 const char *model, const char *name,
1619 int fd, int is_connected)
1621 int so_type=-1, optlen=sizeof(so_type);
1623 if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type,
1624 (socklen_t *)&optlen)< 0) {
1625 fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd);
1626 return NULL;
1628 switch(so_type) {
1629 case SOCK_DGRAM:
1630 return net_socket_fd_init_dgram(vlan, model, name, fd, is_connected);
1631 case SOCK_STREAM:
1632 return net_socket_fd_init_stream(vlan, model, name, fd, is_connected);
1633 default:
1634 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
1635 fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd);
1636 return net_socket_fd_init_stream(vlan, model, name, fd, is_connected);
1638 return NULL;
1641 static void net_socket_accept(void *opaque)
1643 NetSocketListenState *s = opaque;
1644 NetSocketState *s1;
1645 struct sockaddr_in saddr;
1646 socklen_t len;
1647 int fd;
1649 for(;;) {
1650 len = sizeof(saddr);
1651 fd = accept(s->fd, (struct sockaddr *)&saddr, &len);
1652 if (fd < 0 && errno != EINTR) {
1653 return;
1654 } else if (fd >= 0) {
1655 break;
1658 s1 = net_socket_fd_init(s->vlan, s->model, s->name, fd, 1);
1659 if (!s1) {
1660 closesocket(fd);
1661 } else {
1662 snprintf(s1->vc->info_str, sizeof(s1->vc->info_str),
1663 "socket: connection from %s:%d",
1664 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1668 static int net_socket_listen_init(VLANState *vlan,
1669 const char *model,
1670 const char *name,
1671 const char *host_str)
1673 NetSocketListenState *s;
1674 int fd, val, ret;
1675 struct sockaddr_in saddr;
1677 if (parse_host_port(&saddr, host_str) < 0)
1678 return -1;
1680 s = qemu_mallocz(sizeof(NetSocketListenState));
1682 fd = socket(PF_INET, SOCK_STREAM, 0);
1683 if (fd < 0) {
1684 perror("socket");
1685 return -1;
1687 socket_set_nonblock(fd);
1689 /* allow fast reuse */
1690 val = 1;
1691 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
1693 ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
1694 if (ret < 0) {
1695 perror("bind");
1696 return -1;
1698 ret = listen(fd, 0);
1699 if (ret < 0) {
1700 perror("listen");
1701 return -1;
1703 s->vlan = vlan;
1704 s->model = strdup(model);
1705 s->name = name ? strdup(name) : NULL;
1706 s->fd = fd;
1707 qemu_set_fd_handler(fd, net_socket_accept, NULL, s);
1708 return 0;
1711 static int net_socket_connect_init(VLANState *vlan,
1712 const char *model,
1713 const char *name,
1714 const char *host_str)
1716 NetSocketState *s;
1717 int fd, connected, ret, err;
1718 struct sockaddr_in saddr;
1720 if (parse_host_port(&saddr, host_str) < 0)
1721 return -1;
1723 fd = socket(PF_INET, SOCK_STREAM, 0);
1724 if (fd < 0) {
1725 perror("socket");
1726 return -1;
1728 socket_set_nonblock(fd);
1730 connected = 0;
1731 for(;;) {
1732 ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
1733 if (ret < 0) {
1734 err = socket_error();
1735 if (err == EINTR || err == EWOULDBLOCK) {
1736 } else if (err == EINPROGRESS) {
1737 break;
1738 #ifdef _WIN32
1739 } else if (err == WSAEALREADY) {
1740 break;
1741 #endif
1742 } else {
1743 perror("connect");
1744 closesocket(fd);
1745 return -1;
1747 } else {
1748 connected = 1;
1749 break;
1752 s = net_socket_fd_init(vlan, model, name, fd, connected);
1753 if (!s)
1754 return -1;
1755 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1756 "socket: connect to %s:%d",
1757 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1758 return 0;
1761 static int net_socket_mcast_init(VLANState *vlan,
1762 const char *model,
1763 const char *name,
1764 const char *host_str)
1766 NetSocketState *s;
1767 int fd;
1768 struct sockaddr_in saddr;
1770 if (parse_host_port(&saddr, host_str) < 0)
1771 return -1;
1774 fd = net_socket_mcast_create(&saddr);
1775 if (fd < 0)
1776 return -1;
1778 s = net_socket_fd_init(vlan, model, name, fd, 0);
1779 if (!s)
1780 return -1;
1782 s->dgram_dst = saddr;
1784 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1785 "socket: mcast=%s:%d",
1786 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1787 return 0;
1791 typedef struct DumpState {
1792 VLANClientState *pcap_vc;
1793 int fd;
1794 int pcap_caplen;
1795 } DumpState;
1797 #define PCAP_MAGIC 0xa1b2c3d4
1799 struct pcap_file_hdr {
1800 uint32_t magic;
1801 uint16_t version_major;
1802 uint16_t version_minor;
1803 int32_t thiszone;
1804 uint32_t sigfigs;
1805 uint32_t snaplen;
1806 uint32_t linktype;
1809 struct pcap_sf_pkthdr {
1810 struct {
1811 int32_t tv_sec;
1812 int32_t tv_usec;
1813 } ts;
1814 uint32_t caplen;
1815 uint32_t len;
1818 static void dump_receive(void *opaque, const uint8_t *buf, int size)
1820 DumpState *s = opaque;
1821 struct pcap_sf_pkthdr hdr;
1822 int64_t ts;
1823 int caplen;
1825 /* Early return in case of previous error. */
1826 if (s->fd < 0) {
1827 return;
1830 ts = muldiv64 (qemu_get_clock(vm_clock),1000000, ticks_per_sec);
1831 caplen = size > s->pcap_caplen ? s->pcap_caplen : size;
1833 hdr.ts.tv_sec = ts / 1000000000LL;
1834 hdr.ts.tv_usec = ts % 1000000;
1835 hdr.caplen = caplen;
1836 hdr.len = size;
1837 if (write(s->fd, &hdr, sizeof(hdr)) != sizeof(hdr) ||
1838 write(s->fd, buf, caplen) != caplen) {
1839 qemu_log("-net dump write error - stop dump\n");
1840 close(s->fd);
1841 s->fd = -1;
1845 static void net_dump_cleanup(VLANClientState *vc)
1847 DumpState *s = vc->opaque;
1849 close(s->fd);
1850 qemu_free(s);
1853 static int net_dump_init(VLANState *vlan, const char *device,
1854 const char *name, const char *filename, int len)
1856 struct pcap_file_hdr hdr;
1857 DumpState *s;
1859 s = qemu_malloc(sizeof(DumpState));
1861 s->fd = open(filename, O_CREAT | O_WRONLY, 0644);
1862 if (s->fd < 0) {
1863 fprintf(stderr, "-net dump: can't open %s\n", filename);
1864 return -1;
1867 s->pcap_caplen = len;
1869 hdr.magic = PCAP_MAGIC;
1870 hdr.version_major = 2;
1871 hdr.version_minor = 4;
1872 hdr.thiszone = 0;
1873 hdr.sigfigs = 0;
1874 hdr.snaplen = s->pcap_caplen;
1875 hdr.linktype = 1;
1877 if (write(s->fd, &hdr, sizeof(hdr)) < sizeof(hdr)) {
1878 perror("-net dump write error");
1879 close(s->fd);
1880 qemu_free(s);
1881 return -1;
1884 s->pcap_vc = qemu_new_vlan_client(vlan, device, name, dump_receive, NULL,
1885 net_dump_cleanup, s);
1886 snprintf(s->pcap_vc->info_str, sizeof(s->pcap_vc->info_str),
1887 "dump to %s (len=%d)", filename, len);
1888 return 0;
1891 /* find or alloc a new VLAN */
1892 VLANState *qemu_find_vlan(int id)
1894 VLANState **pvlan, *vlan;
1895 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
1896 if (vlan->id == id)
1897 return vlan;
1899 vlan = qemu_mallocz(sizeof(VLANState));
1900 vlan->id = id;
1901 vlan->next = NULL;
1902 pvlan = &first_vlan;
1903 while (*pvlan != NULL)
1904 pvlan = &(*pvlan)->next;
1905 *pvlan = vlan;
1906 return vlan;
1909 static int nic_get_free_idx(void)
1911 int index;
1913 for (index = 0; index < MAX_NICS; index++)
1914 if (!nd_table[index].used)
1915 return index;
1916 return -1;
1919 void qemu_check_nic_model(NICInfo *nd, const char *model)
1921 const char *models[2];
1923 models[0] = model;
1924 models[1] = NULL;
1926 qemu_check_nic_model_list(nd, models, model);
1929 void qemu_check_nic_model_list(NICInfo *nd, const char * const *models,
1930 const char *default_model)
1932 int i, exit_status = 0;
1934 if (!nd->model)
1935 nd->model = strdup(default_model);
1937 if (strcmp(nd->model, "?") != 0) {
1938 for (i = 0 ; models[i]; i++)
1939 if (strcmp(nd->model, models[i]) == 0)
1940 return;
1942 fprintf(stderr, "qemu: Unsupported NIC model: %s\n", nd->model);
1943 exit_status = 1;
1946 fprintf(stderr, "qemu: Supported NIC models: ");
1947 for (i = 0 ; models[i]; i++)
1948 fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n');
1950 exit(exit_status);
1953 int net_client_init(const char *device, const char *p)
1955 static const char * const fd_params[] = {
1956 "vlan", "name", "fd", NULL
1958 char buf[1024];
1959 int vlan_id, ret;
1960 VLANState *vlan;
1961 char *name = NULL;
1963 vlan_id = 0;
1964 if (get_param_value(buf, sizeof(buf), "vlan", p)) {
1965 vlan_id = strtol(buf, NULL, 0);
1967 vlan = qemu_find_vlan(vlan_id);
1969 if (get_param_value(buf, sizeof(buf), "name", p)) {
1970 name = strdup(buf);
1972 if (!strcmp(device, "nic")) {
1973 static const char * const nic_params[] = {
1974 "vlan", "name", "macaddr", "model", NULL
1976 NICInfo *nd;
1977 uint8_t *macaddr;
1978 int idx = nic_get_free_idx();
1980 if (check_params(nic_params, p) < 0) {
1981 fprintf(stderr, "qemu: invalid parameter '%s' in '%s'\n",
1982 buf, p);
1983 return -1;
1985 if (idx == -1 || nb_nics >= MAX_NICS) {
1986 fprintf(stderr, "Too Many NICs\n");
1987 ret = -1;
1988 goto out;
1990 nd = &nd_table[idx];
1991 macaddr = nd->macaddr;
1992 macaddr[0] = 0x52;
1993 macaddr[1] = 0x54;
1994 macaddr[2] = 0x00;
1995 macaddr[3] = 0x12;
1996 macaddr[4] = 0x34;
1997 macaddr[5] = 0x56 + idx;
1999 if (get_param_value(buf, sizeof(buf), "macaddr", p)) {
2000 if (parse_macaddr(macaddr, buf) < 0) {
2001 fprintf(stderr, "invalid syntax for ethernet address\n");
2002 ret = -1;
2003 goto out;
2006 if (get_param_value(buf, sizeof(buf), "model", p)) {
2007 nd->model = strdup(buf);
2009 nd->vlan = vlan;
2010 nd->name = name;
2011 nd->used = 1;
2012 name = NULL;
2013 nb_nics++;
2014 vlan->nb_guest_devs++;
2015 ret = idx;
2016 } else
2017 if (!strcmp(device, "none")) {
2018 if (*p != '\0') {
2019 fprintf(stderr, "qemu: 'none' takes no parameters\n");
2020 return -1;
2022 /* does nothing. It is needed to signal that no network cards
2023 are wanted */
2024 ret = 0;
2025 } else
2026 #ifdef CONFIG_SLIRP
2027 if (!strcmp(device, "user")) {
2028 static const char * const slirp_params[] = {
2029 "vlan", "name", "hostname", "restrict", "ip", NULL
2031 if (check_params(slirp_params, p) < 0) {
2032 fprintf(stderr, "qemu: invalid parameter '%s' in '%s'\n",
2033 buf, p);
2034 return -1;
2036 if (get_param_value(buf, sizeof(buf), "hostname", p)) {
2037 pstrcpy(slirp_hostname, sizeof(slirp_hostname), buf);
2039 if (get_param_value(buf, sizeof(buf), "restrict", p)) {
2040 slirp_restrict = (buf[0] == 'y') ? 1 : 0;
2042 if (get_param_value(buf, sizeof(buf), "ip", p)) {
2043 slirp_ip = strdup(buf);
2045 vlan->nb_host_devs++;
2046 ret = net_slirp_init(vlan, device, name);
2047 } else if (!strcmp(device, "channel")) {
2048 long port;
2049 char name[20], *devname;
2050 struct VMChannel *vmc;
2052 port = strtol(p, &devname, 10);
2053 devname++;
2054 if (port < 1 || port > 65535) {
2055 fprintf(stderr, "vmchannel wrong port number\n");
2056 ret = -1;
2057 goto out;
2059 vmc = malloc(sizeof(struct VMChannel));
2060 snprintf(name, 20, "vmchannel%ld", port);
2061 vmc->hd = qemu_chr_open(name, devname, NULL);
2062 if (!vmc->hd) {
2063 fprintf(stderr, "qemu: could not open vmchannel device"
2064 "'%s'\n", devname);
2065 ret = -1;
2066 goto out;
2068 vmc->port = port;
2069 slirp_add_exec(3, vmc->hd, 4, port);
2070 qemu_chr_add_handlers(vmc->hd, vmchannel_can_read, vmchannel_read,
2071 NULL, vmc);
2072 ret = 0;
2073 } else
2074 #endif
2075 #ifdef _WIN32
2076 if (!strcmp(device, "tap")) {
2077 static const char * const tap_params[] = {
2078 "vlan", "name", "ifname", NULL
2080 char ifname[64];
2082 if (check_params(tap_params, p) < 0) {
2083 fprintf(stderr, "qemu: invalid parameter '%s' in '%s'\n",
2084 buf, p);
2085 return -1;
2087 if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
2088 fprintf(stderr, "tap: no interface name\n");
2089 ret = -1;
2090 goto out;
2092 vlan->nb_host_devs++;
2093 ret = tap_win32_init(vlan, device, name, ifname);
2094 } else
2095 #elif defined (_AIX)
2096 #else
2097 if (!strcmp(device, "tap")) {
2098 char ifname[64];
2099 char setup_script[1024], down_script[1024];
2100 int fd;
2101 vlan->nb_host_devs++;
2102 if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
2103 if (check_params(fd_params, p) < 0) {
2104 fprintf(stderr, "qemu: invalid parameter '%s' in '%s'\n",
2105 buf, p);
2106 return -1;
2108 fd = strtol(buf, NULL, 0);
2109 fcntl(fd, F_SETFL, O_NONBLOCK);
2110 net_tap_fd_init(vlan, device, name, fd, tap_probe_vnet_hdr(fd));
2111 ret = 0;
2112 } else {
2113 static const char * const tap_params[] = {
2114 "vlan", "name", "ifname", "script", "downscript", NULL
2116 if (check_params(tap_params, p) < 0) {
2117 fprintf(stderr, "qemu: invalid parameter '%s' in '%s'\n",
2118 buf, p);
2119 return -1;
2121 if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
2122 ifname[0] = '\0';
2124 if (get_param_value(setup_script, sizeof(setup_script), "script", p) == 0) {
2125 pstrcpy(setup_script, sizeof(setup_script), DEFAULT_NETWORK_SCRIPT);
2127 if (get_param_value(down_script, sizeof(down_script), "downscript", p) == 0) {
2128 pstrcpy(down_script, sizeof(down_script), DEFAULT_NETWORK_DOWN_SCRIPT);
2130 ret = net_tap_init(vlan, device, name, ifname, setup_script, down_script);
2132 } else
2133 #endif
2134 if (!strcmp(device, "socket")) {
2135 if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
2136 int fd;
2137 if (check_params(fd_params, p) < 0) {
2138 fprintf(stderr, "qemu: invalid parameter '%s' in '%s'\n",
2139 buf, p);
2140 return -1;
2142 fd = strtol(buf, NULL, 0);
2143 ret = -1;
2144 if (net_socket_fd_init(vlan, device, name, fd, 1))
2145 ret = 0;
2146 } else if (get_param_value(buf, sizeof(buf), "listen", p) > 0) {
2147 static const char * const listen_params[] = {
2148 "vlan", "name", "listen", NULL
2150 if (check_params(listen_params, p) < 0) {
2151 fprintf(stderr, "qemu: invalid parameter '%s' in '%s'\n",
2152 buf, p);
2153 return -1;
2155 ret = net_socket_listen_init(vlan, device, name, buf);
2156 } else if (get_param_value(buf, sizeof(buf), "connect", p) > 0) {
2157 static const char * const connect_params[] = {
2158 "vlan", "name", "connect", NULL
2160 if (check_params(connect_params, p) < 0) {
2161 fprintf(stderr, "qemu: invalid parameter '%s' in '%s'\n",
2162 buf, p);
2163 return -1;
2165 ret = net_socket_connect_init(vlan, device, name, buf);
2166 } else if (get_param_value(buf, sizeof(buf), "mcast", p) > 0) {
2167 static const char * const mcast_params[] = {
2168 "vlan", "name", "mcast", NULL
2170 if (check_params(mcast_params, p) < 0) {
2171 fprintf(stderr, "qemu: invalid parameter '%s' in '%s'\n",
2172 buf, p);
2173 return -1;
2175 ret = net_socket_mcast_init(vlan, device, name, buf);
2176 } else {
2177 fprintf(stderr, "Unknown socket options: %s\n", p);
2178 ret = -1;
2179 goto out;
2181 vlan->nb_host_devs++;
2182 } else
2183 #ifdef CONFIG_VDE
2184 if (!strcmp(device, "vde")) {
2185 static const char * const vde_params[] = {
2186 "vlan", "name", "sock", "port", "group", "mode", NULL
2188 char vde_sock[1024], vde_group[512];
2189 int vde_port, vde_mode;
2191 if (check_params(vde_params, p) < 0) {
2192 fprintf(stderr, "qemu: invalid parameter '%s' in '%s'\n",
2193 buf, p);
2194 return -1;
2196 vlan->nb_host_devs++;
2197 if (get_param_value(vde_sock, sizeof(vde_sock), "sock", p) <= 0) {
2198 vde_sock[0] = '\0';
2200 if (get_param_value(buf, sizeof(buf), "port", p) > 0) {
2201 vde_port = strtol(buf, NULL, 10);
2202 } else {
2203 vde_port = 0;
2205 if (get_param_value(vde_group, sizeof(vde_group), "group", p) <= 0) {
2206 vde_group[0] = '\0';
2208 if (get_param_value(buf, sizeof(buf), "mode", p) > 0) {
2209 vde_mode = strtol(buf, NULL, 8);
2210 } else {
2211 vde_mode = 0700;
2213 ret = net_vde_init(vlan, device, name, vde_sock, vde_port, vde_group, vde_mode);
2214 } else
2215 #endif
2216 if (!strcmp(device, "dump")) {
2217 int len = 65536;
2219 if (get_param_value(buf, sizeof(buf), "len", p) > 0) {
2220 len = strtol(buf, NULL, 0);
2222 if (!get_param_value(buf, sizeof(buf), "file", p)) {
2223 snprintf(buf, sizeof(buf), "qemu-vlan%d.pcap", vlan_id);
2225 ret = net_dump_init(vlan, device, name, buf, len);
2226 } else {
2227 fprintf(stderr, "Unknown network device: %s\n", device);
2228 ret = -1;
2229 goto out;
2231 if (ret < 0) {
2232 fprintf(stderr, "Could not initialize device '%s'\n", device);
2234 out:
2235 if (name)
2236 free(name);
2237 return ret;
2240 void net_client_uninit(NICInfo *nd)
2242 nd->vlan->nb_guest_devs--;
2243 nb_nics--;
2244 nd->used = 0;
2245 free((void *)nd->model);
2248 static int net_host_check_device(const char *device)
2250 int i;
2251 const char *valid_param_list[] = { "tap", "socket", "dump"
2252 #ifdef CONFIG_SLIRP
2253 ,"user"
2254 #endif
2255 #ifdef CONFIG_VDE
2256 ,"vde"
2257 #endif
2259 for (i = 0; i < sizeof(valid_param_list) / sizeof(char *); i++) {
2260 if (!strncmp(valid_param_list[i], device,
2261 strlen(valid_param_list[i])))
2262 return 1;
2265 return 0;
2268 void net_host_device_add(Monitor *mon, const char *device, const char *opts)
2270 if (!net_host_check_device(device)) {
2271 monitor_printf(mon, "invalid host network device %s\n", device);
2272 return;
2274 if (net_client_init(device, opts ? opts : "") < 0) {
2275 monitor_printf(mon, "adding host network device %s failed\n", device);
2279 void net_host_device_remove(Monitor *mon, int vlan_id, const char *device)
2281 VLANState *vlan;
2282 VLANClientState *vc;
2284 vlan = qemu_find_vlan(vlan_id);
2286 for (vc = vlan->first_client; vc != NULL; vc = vc->next) {
2287 if (!strcmp(vc->name, device)) {
2288 break;
2292 if (!vc) {
2293 monitor_printf(mon, "can't find device %s\n", device);
2294 return;
2296 if (!net_host_check_device(vc->model)) {
2297 monitor_printf(mon, "invalid host network device %s\n", device);
2298 return;
2300 qemu_del_vlan_client(vc);
2303 int net_client_parse(const char *str)
2305 const char *p;
2306 char *q;
2307 char device[64];
2309 p = str;
2310 q = device;
2311 while (*p != '\0' && *p != ',') {
2312 if ((q - device) < sizeof(device) - 1)
2313 *q++ = *p;
2314 p++;
2316 *q = '\0';
2317 if (*p == ',')
2318 p++;
2320 return net_client_init(device, p);
2323 void do_info_network(Monitor *mon)
2325 VLANState *vlan;
2326 VLANClientState *vc;
2328 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
2329 monitor_printf(mon, "VLAN %d devices:\n", vlan->id);
2330 for(vc = vlan->first_client; vc != NULL; vc = vc->next)
2331 monitor_printf(mon, " %s: %s\n", vc->name, vc->info_str);
2335 int do_set_link(Monitor *mon, const char *name, const char *up_or_down)
2337 VLANState *vlan;
2338 VLANClientState *vc = NULL;
2340 for (vlan = first_vlan; vlan != NULL; vlan = vlan->next)
2341 for (vc = vlan->first_client; vc != NULL; vc = vc->next)
2342 if (strcmp(vc->name, name) == 0)
2343 goto done;
2344 done:
2346 if (!vc) {
2347 monitor_printf(mon, "could not find network device '%s'", name);
2348 return 0;
2351 if (strcmp(up_or_down, "up") == 0)
2352 vc->link_down = 0;
2353 else if (strcmp(up_or_down, "down") == 0)
2354 vc->link_down = 1;
2355 else
2356 monitor_printf(mon, "invalid link status '%s'; only 'up' or 'down' "
2357 "valid\n", up_or_down);
2359 if (vc->link_status_changed)
2360 vc->link_status_changed(vc);
2362 return 1;
2365 void net_cleanup(void)
2367 VLANState *vlan;
2369 /* close network clients */
2370 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
2371 VLANClientState *vc = vlan->first_client;
2373 while (vc) {
2374 VLANClientState *next = vc->next;
2376 qemu_del_vlan_client(vc);
2378 vc = next;
2383 void net_client_check(void)
2385 VLANState *vlan;
2387 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
2388 if (vlan->nb_guest_devs == 0 && vlan->nb_host_devs == 0)
2389 continue;
2390 if (vlan->nb_guest_devs == 0)
2391 fprintf(stderr, "Warning: vlan %d with no nics\n", vlan->id);
2392 if (vlan->nb_host_devs == 0)
2393 fprintf(stderr,
2394 "Warning: vlan %d is not connected to host network\n",
2395 vlan->id);