Cleanup kvm cpu loop
[qemu-kvm/fedora.git] / net.c
blob567dcdc1800623197e0f251e9eb185c66b648d3f
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 NetCanReceive *can_receive,
337 NetReceive *receive,
338 NetReceiveIOV *receive_iov,
339 NetCleanup *cleanup,
340 void *opaque)
342 VLANClientState *vc, **pvc;
343 vc = qemu_mallocz(sizeof(VLANClientState));
344 vc->model = strdup(model);
345 if (name)
346 vc->name = strdup(name);
347 else
348 vc->name = assign_name(vc, model);
349 vc->can_receive = can_receive;
350 vc->receive = receive;
351 vc->receive_iov = receive_iov;
352 vc->cleanup = cleanup;
353 vc->opaque = opaque;
354 vc->vlan = vlan;
356 vc->next = NULL;
357 pvc = &vlan->first_client;
358 while (*pvc != NULL)
359 pvc = &(*pvc)->next;
360 *pvc = vc;
361 return vc;
364 void qemu_del_vlan_client(VLANClientState *vc)
366 VLANClientState **pvc = &vc->vlan->first_client;
368 while (*pvc != NULL)
369 if (*pvc == vc) {
370 *pvc = vc->next;
371 if (vc->cleanup) {
372 vc->cleanup(vc);
374 free(vc->name);
375 free(vc->model);
376 qemu_free(vc);
377 break;
378 } else
379 pvc = &(*pvc)->next;
382 VLANClientState *qemu_find_vlan_client(VLANState *vlan, void *opaque)
384 VLANClientState **pvc = &vlan->first_client;
386 while (*pvc != NULL)
387 if ((*pvc)->opaque == opaque)
388 return *pvc;
389 else
390 pvc = &(*pvc)->next;
392 return NULL;
395 int qemu_can_send_packet(VLANClientState *sender)
397 VLANState *vlan = sender->vlan;
398 VLANClientState *vc;
400 for (vc = vlan->first_client; vc != NULL; vc = vc->next) {
401 if (vc == sender) {
402 continue;
405 /* no can_receive() handler, they can always receive */
406 if (!vc->can_receive || vc->can_receive(vc)) {
407 return 1;
410 return 0;
413 static int
414 qemu_deliver_packet(VLANClientState *sender, const uint8_t *buf, int size, int raw)
416 VLANClientState *vc;
417 int ret = -1;
419 sender->vlan->delivering = 1;
421 for (vc = sender->vlan->first_client; vc != NULL; vc = vc->next) {
422 ssize_t len;
424 if (vc == sender) {
425 continue;
428 if (vc->link_down) {
429 ret = size;
430 continue;
433 if (raw && vc->receive_raw) {
434 len = vc->receive_raw(vc, buf, size);
435 } else {
436 len = vc->receive(vc, buf, size);
439 ret = (ret >= 0) ? ret : len;
442 sender->vlan->delivering = 0;
444 return ret;
447 void qemu_flush_queued_packets(VLANClientState *vc)
449 VLANPacket *packet;
451 while ((packet = vc->vlan->send_queue) != NULL) {
452 int ret;
454 vc->vlan->send_queue = packet->next;
456 ret = qemu_deliver_packet(packet->sender, packet->data,
457 packet->size, packet->raw);
458 if (ret == 0 && packet->sent_cb != NULL) {
459 packet->next = vc->vlan->send_queue;
460 vc->vlan->send_queue = packet;
461 break;
464 if (packet->sent_cb)
465 packet->sent_cb(packet->sender);
467 qemu_free(packet);
471 static void qemu_enqueue_packet(VLANClientState *sender,
472 const uint8_t *buf, int size, int raw,
473 NetPacketSent *sent_cb)
475 VLANPacket *packet;
477 packet = qemu_malloc(sizeof(VLANPacket) + size);
478 packet->next = sender->vlan->send_queue;
479 packet->sender = sender;
480 packet->size = size;
481 packet->raw = raw;
482 packet->sent_cb = sent_cb;
483 memcpy(packet->data, buf, size);
484 sender->vlan->send_queue = packet;
487 static ssize_t qemu_send_packet_async2(VLANClientState *sender,
488 const uint8_t *buf, int size, int raw,
489 NetPacketSent *sent_cb)
491 int ret;
493 if (sender->link_down) {
494 return size;
497 #ifdef DEBUG_NET
498 printf("vlan %d send:\n", sender->vlan->id);
499 hex_dump(stdout, buf, size);
500 #endif
502 if (sender->vlan->delivering) {
503 qemu_enqueue_packet(sender, buf, size, raw, NULL);
504 return size;
507 ret = qemu_deliver_packet(sender, buf, size, raw);
508 if (ret == 0 && sent_cb != NULL) {
509 qemu_enqueue_packet(sender, buf, size, raw, sent_cb);
510 return 0;
513 qemu_flush_queued_packets(sender);
515 return ret;
518 ssize_t qemu_send_packet_async(VLANClientState *sender,
519 const uint8_t *buf, int size,
520 NetPacketSent *sent_cb)
522 return qemu_send_packet_async2(sender, buf, size, 0, sent_cb);
525 ssize_t qemu_send_packet(VLANClientState *sender, const uint8_t *buf, int size)
527 return qemu_send_packet_async2(sender, buf, size, 0, NULL);
530 ssize_t qemu_send_packet_raw(VLANClientState *sender, const uint8_t *buf, int size)
532 return qemu_send_packet_async2(sender, buf, size, 1, NULL);
535 static ssize_t vc_sendv_compat(VLANClientState *vc, const struct iovec *iov,
536 int iovcnt)
538 uint8_t buffer[4096];
539 size_t offset = 0;
540 int i;
542 for (i = 0; i < iovcnt; i++) {
543 size_t len;
545 len = MIN(sizeof(buffer) - offset, iov[i].iov_len);
546 memcpy(buffer + offset, iov[i].iov_base, len);
547 offset += len;
550 return vc->receive(vc, buffer, offset);
553 static ssize_t calc_iov_length(const struct iovec *iov, int iovcnt)
555 size_t offset = 0;
556 int i;
558 for (i = 0; i < iovcnt; i++)
559 offset += iov[i].iov_len;
560 return offset;
563 static int qemu_deliver_packet_iov(VLANClientState *sender,
564 const struct iovec *iov, int iovcnt)
566 VLANClientState *vc;
567 int ret = -1;
569 sender->vlan->delivering = 1;
571 for (vc = sender->vlan->first_client; vc != NULL; vc = vc->next) {
572 ssize_t len;
574 if (vc == sender) {
575 continue;
578 if (vc->link_down) {
579 ret = calc_iov_length(iov, iovcnt);
580 continue;
583 if (vc->receive_iov) {
584 len = vc->receive_iov(vc, iov, iovcnt);
585 } else {
586 len = vc_sendv_compat(vc, iov, iovcnt);
589 ret = (ret >= 0) ? ret : len;
592 sender->vlan->delivering = 0;
594 return ret;
597 static ssize_t qemu_enqueue_packet_iov(VLANClientState *sender,
598 const struct iovec *iov, int iovcnt,
599 NetPacketSent *sent_cb)
601 VLANPacket *packet;
602 size_t max_len = 0;
603 int i;
605 max_len = calc_iov_length(iov, iovcnt);
607 packet = qemu_malloc(sizeof(VLANPacket) + max_len);
608 packet->next = sender->vlan->send_queue;
609 packet->sender = sender;
610 packet->sent_cb = sent_cb;
611 packet->size = 0;
612 packet->raw = 0;
614 for (i = 0; i < iovcnt; i++) {
615 size_t len = iov[i].iov_len;
617 memcpy(packet->data + packet->size, iov[i].iov_base, len);
618 packet->size += len;
621 sender->vlan->send_queue = packet;
623 return packet->size;
626 ssize_t qemu_sendv_packet_async(VLANClientState *sender,
627 const struct iovec *iov, int iovcnt,
628 NetPacketSent *sent_cb)
630 int ret;
632 if (sender->link_down) {
633 return calc_iov_length(iov, iovcnt);
636 if (sender->vlan->delivering) {
637 return qemu_enqueue_packet_iov(sender, iov, iovcnt, NULL);
640 ret = qemu_deliver_packet_iov(sender, iov, iovcnt);
641 if (ret == 0 && sent_cb != NULL) {
642 qemu_enqueue_packet_iov(sender, iov, iovcnt, sent_cb);
643 return 0;
646 qemu_flush_queued_packets(sender);
648 return ret;
651 ssize_t
652 qemu_sendv_packet(VLANClientState *vc, const struct iovec *iov, int iovcnt)
654 return qemu_sendv_packet_async(vc, iov, iovcnt, NULL);
657 static void config_error(Monitor *mon, const char *fmt, ...)
659 va_list ap;
661 va_start(ap, fmt);
662 if (mon) {
663 monitor_vprintf(mon, fmt, ap);
664 } else {
665 fprintf(stderr, "qemu: ");
666 vfprintf(stderr, fmt, ap);
667 exit(1);
669 va_end(ap);
672 #if defined(CONFIG_SLIRP)
674 /* slirp network adapter */
676 struct slirp_config_str {
677 struct slirp_config_str *next;
678 const char *str;
681 static int slirp_inited;
682 static struct slirp_config_str *slirp_redirs;
683 #ifndef _WIN32
684 static const char *slirp_smb_export;
685 #endif
686 static VLANClientState *slirp_vc;
688 #ifndef _WIN32
689 static void slirp_smb(const char *exported_dir);
690 #endif
691 static void slirp_redirection(Monitor *mon, const char *redir_str);
693 int slirp_can_output(void)
695 return !slirp_vc || qemu_can_send_packet(slirp_vc);
698 void slirp_output(const uint8_t *pkt, int pkt_len)
700 #ifdef DEBUG_SLIRP
701 printf("slirp output:\n");
702 hex_dump(stdout, pkt, pkt_len);
703 #endif
704 if (!slirp_vc)
705 return;
706 qemu_send_packet(slirp_vc, pkt, pkt_len);
709 int slirp_is_inited(void)
711 return slirp_inited;
714 static ssize_t slirp_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
716 #ifdef DEBUG_SLIRP
717 printf("slirp input:\n");
718 hex_dump(stdout, buf, size);
719 #endif
720 slirp_input(buf, size);
721 return size;
724 static int slirp_in_use;
726 static void net_slirp_cleanup(VLANClientState *vc)
728 slirp_in_use = 0;
731 static int net_slirp_init(VLANState *vlan, const char *model, const char *name,
732 int restricted, const char *ip)
734 if (slirp_in_use) {
735 /* slirp only supports a single instance so far */
736 return -1;
738 if (!slirp_inited) {
739 slirp_inited = 1;
740 slirp_init(restricted, ip);
742 while (slirp_redirs) {
743 struct slirp_config_str *config = slirp_redirs;
745 slirp_redirection(NULL, config->str);
746 slirp_redirs = config->next;
747 qemu_free(config);
749 #ifndef _WIN32
750 if (slirp_smb_export) {
751 slirp_smb(slirp_smb_export);
753 #endif
756 slirp_vc = qemu_new_vlan_client(vlan, model, name, NULL, slirp_receive,
757 NULL, net_slirp_cleanup, NULL);
758 slirp_vc->info_str[0] = '\0';
759 slirp_in_use = 1;
760 return 0;
763 static void net_slirp_redir_print(void *opaque, int is_udp,
764 struct in_addr *laddr, u_int lport,
765 struct in_addr *faddr, u_int fport)
767 Monitor *mon = (Monitor *)opaque;
768 uint32_t h_addr;
769 uint32_t g_addr;
770 char buf[16];
772 h_addr = ntohl(faddr->s_addr);
773 g_addr = ntohl(laddr->s_addr);
775 monitor_printf(mon, " %s |", is_udp ? "udp" : "tcp" );
776 snprintf(buf, 15, "%d.%d.%d.%d", (h_addr >> 24) & 0xff,
777 (h_addr >> 16) & 0xff,
778 (h_addr >> 8) & 0xff,
779 (h_addr) & 0xff);
780 monitor_printf(mon, " %15s |", buf);
781 monitor_printf(mon, " %5d |", fport);
783 snprintf(buf, 15, "%d.%d.%d.%d", (g_addr >> 24) & 0xff,
784 (g_addr >> 16) & 0xff,
785 (g_addr >> 8) & 0xff,
786 (g_addr) & 0xff);
787 monitor_printf(mon, " %15s |", buf);
788 monitor_printf(mon, " %5d\n", lport);
792 static void net_slirp_redir_list(Monitor *mon)
794 if (!mon)
795 return;
797 monitor_printf(mon, " Prot | Host Addr | HPort | Guest Addr | GPort\n");
798 monitor_printf(mon, " | | | | \n");
799 slirp_redir_loop(net_slirp_redir_print, mon);
802 static void net_slirp_redir_rm(Monitor *mon, const char *port_str)
804 int host_port;
805 char buf[256] = "";
806 const char *p = port_str;
807 int is_udp = 0;
808 int n;
810 if (!mon)
811 return;
813 if (!port_str || !port_str[0])
814 goto fail_syntax;
816 get_str_sep(buf, sizeof(buf), &p, ':');
818 if (!strcmp(buf, "tcp") || buf[0] == '\0') {
819 is_udp = 0;
820 } else if (!strcmp(buf, "udp")) {
821 is_udp = 1;
822 } else {
823 goto fail_syntax;
826 host_port = atoi(p);
828 n = slirp_redir_rm(is_udp, host_port);
830 monitor_printf(mon, "removed %d redirections to %s port %d\n", n,
831 is_udp ? "udp" : "tcp", host_port);
832 return;
834 fail_syntax:
835 monitor_printf(mon, "invalid format\n");
838 static void slirp_redirection(Monitor *mon, const char *redir_str)
840 struct in_addr guest_addr;
841 int host_port, guest_port;
842 const char *p;
843 char buf[256], *r;
844 int is_udp;
846 p = redir_str;
847 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
848 goto fail_syntax;
850 if (!strcmp(buf, "tcp") || buf[0] == '\0') {
851 is_udp = 0;
852 } else if (!strcmp(buf, "udp")) {
853 is_udp = 1;
854 } else {
855 goto fail_syntax;
858 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
859 goto fail_syntax;
861 host_port = strtol(buf, &r, 0);
862 if (r == buf) {
863 goto fail_syntax;
866 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
867 goto fail_syntax;
869 if (buf[0] == '\0') {
870 pstrcpy(buf, sizeof(buf), "10.0.2.15");
872 if (!inet_aton(buf, &guest_addr)) {
873 goto fail_syntax;
876 guest_port = strtol(p, &r, 0);
877 if (r == p) {
878 goto fail_syntax;
881 if (slirp_redir(is_udp, host_port, guest_addr, guest_port) < 0) {
882 config_error(mon, "could not set up redirection '%s'\n", redir_str);
884 return;
886 fail_syntax:
887 config_error(mon, "invalid redirection format '%s'\n", redir_str);
890 void net_slirp_redir(Monitor *mon, const char *redir_str, const char *redir_opt2)
892 struct slirp_config_str *config;
894 if (!slirp_inited) {
895 if (mon) {
896 monitor_printf(mon, "user mode network stack not in use\n");
897 } else {
898 config = qemu_malloc(sizeof(*config));
899 config->str = redir_str;
900 config->next = slirp_redirs;
901 slirp_redirs = config;
903 return;
906 if (!strcmp(redir_str, "remove")) {
907 net_slirp_redir_rm(mon, redir_opt2);
908 return;
911 if (!strcmp(redir_str, "list")) {
912 net_slirp_redir_list(mon);
913 return;
916 slirp_redirection(mon, redir_str);
919 #ifndef _WIN32
921 static char smb_dir[1024];
923 static void erase_dir(char *dir_name)
925 DIR *d;
926 struct dirent *de;
927 char filename[1024];
929 /* erase all the files in the directory */
930 if ((d = opendir(dir_name)) != NULL) {
931 for(;;) {
932 de = readdir(d);
933 if (!de)
934 break;
935 if (strcmp(de->d_name, ".") != 0 &&
936 strcmp(de->d_name, "..") != 0) {
937 snprintf(filename, sizeof(filename), "%s/%s",
938 smb_dir, de->d_name);
939 if (unlink(filename) != 0) /* is it a directory? */
940 erase_dir(filename);
943 closedir(d);
944 rmdir(dir_name);
948 /* automatic user mode samba server configuration */
949 static void smb_exit(void)
951 erase_dir(smb_dir);
954 static void slirp_smb(const char *exported_dir)
956 char smb_conf[1024];
957 char smb_cmdline[1024];
958 FILE *f;
960 /* XXX: better tmp dir construction */
961 snprintf(smb_dir, sizeof(smb_dir), "/tmp/qemu-smb.%ld", (long)getpid());
962 if (mkdir(smb_dir, 0700) < 0) {
963 fprintf(stderr, "qemu: could not create samba server dir '%s'\n", smb_dir);
964 exit(1);
966 snprintf(smb_conf, sizeof(smb_conf), "%s/%s", smb_dir, "smb.conf");
968 f = fopen(smb_conf, "w");
969 if (!f) {
970 fprintf(stderr, "qemu: could not create samba server configuration file '%s'\n", smb_conf);
971 exit(1);
973 fprintf(f,
974 "[global]\n"
975 "private dir=%s\n"
976 "smb ports=0\n"
977 "socket address=127.0.0.1\n"
978 "pid directory=%s\n"
979 "lock directory=%s\n"
980 "log file=%s/log.smbd\n"
981 "smb passwd file=%s/smbpasswd\n"
982 "security = share\n"
983 "[qemu]\n"
984 "path=%s\n"
985 "read only=no\n"
986 "guest ok=yes\n",
987 smb_dir,
988 smb_dir,
989 smb_dir,
990 smb_dir,
991 smb_dir,
992 exported_dir
994 fclose(f);
995 atexit(smb_exit);
997 snprintf(smb_cmdline, sizeof(smb_cmdline), "%s -s %s",
998 SMBD_COMMAND, smb_conf);
1000 slirp_add_exec(0, smb_cmdline, 4, 139);
1003 /* automatic user mode samba server configuration */
1004 void net_slirp_smb(const char *exported_dir)
1006 if (slirp_smb_export) {
1007 fprintf(stderr, "-smb given twice\n");
1008 exit(1);
1010 slirp_smb_export = exported_dir;
1011 if (slirp_inited) {
1012 slirp_smb(exported_dir);
1016 #endif /* !defined(_WIN32) */
1018 void do_info_slirp(Monitor *mon)
1020 slirp_stats();
1023 struct VMChannel {
1024 CharDriverState *hd;
1025 int port;
1028 static int vmchannel_can_read(void *opaque)
1030 struct VMChannel *vmc = (struct VMChannel*)opaque;
1031 return slirp_socket_can_recv(4, vmc->port);
1034 static void vmchannel_read(void *opaque, const uint8_t *buf, int size)
1036 struct VMChannel *vmc = (struct VMChannel*)opaque;
1037 slirp_socket_recv(4, vmc->port, buf, size);
1040 #endif /* CONFIG_SLIRP */
1042 #ifdef _WIN32
1044 int tap_has_vnet_hdr(void *opaque)
1046 return 0;
1049 void tap_using_vnet_hdr(void *opaque, int using_vnet_hdr)
1053 #else /* !defined(_WIN32) */
1055 /* Maximum GSO packet size (64k) plus plenty of room for
1056 * the ethernet and virtio_net headers
1058 #define TAP_BUFSIZE (4096 + 65536)
1060 #ifdef IFF_VNET_HDR
1061 #include <linux/virtio_net.h>
1062 #endif
1064 typedef struct TAPState {
1065 VLANClientState *vc;
1066 int fd;
1067 char down_script[1024];
1068 char down_script_arg[128];
1069 uint8_t buf[TAP_BUFSIZE];
1070 unsigned int has_vnet_hdr : 1;
1071 unsigned int using_vnet_hdr : 1;
1072 } TAPState;
1074 static int launch_script(const char *setup_script, const char *ifname, int fd);
1076 static ssize_t tap_receive_iov(VLANClientState *vc, const struct iovec *iov,
1077 int iovcnt)
1079 TAPState *s = vc->opaque;
1080 ssize_t len;
1082 do {
1083 len = writev(s->fd, iov, iovcnt);
1084 } while (len == -1 && (errno == EINTR || errno == EAGAIN));
1086 return len;
1089 static ssize_t tap_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
1091 struct iovec iov[2];
1092 int i = 0;
1094 #ifdef IFF_VNET_HDR
1095 TAPState *s = vc->opaque;
1096 struct virtio_net_hdr hdr = { 0, };
1098 if (s->has_vnet_hdr && !s->using_vnet_hdr) {
1099 iov[i].iov_base = &hdr;
1100 iov[i].iov_len = sizeof(hdr);
1101 i++;
1103 #endif
1105 iov[i].iov_base = (char *) buf;
1106 iov[i].iov_len = size;
1107 i++;
1109 return tap_receive_iov(vc, iov, i);
1112 static ssize_t tap_receive_raw(VLANClientState *vc, const uint8_t *buf, size_t size)
1114 struct iovec iov[2];
1115 int i = 0;
1117 #ifdef IFF_VNET_HDR
1118 TAPState *s = vc->opaque;
1119 struct virtio_net_hdr hdr = { 0, };
1121 if (s->has_vnet_hdr && s->using_vnet_hdr) {
1122 iov[i].iov_base = &hdr;
1123 iov[i].iov_len = sizeof(hdr);
1124 i++;
1126 #endif
1128 iov[i].iov_base = (char *) buf;
1129 iov[i].iov_len = size;
1130 i++;
1132 return tap_receive_iov(vc, iov, i);
1135 static int tap_can_send(void *opaque)
1137 TAPState *s = opaque;
1139 return qemu_can_send_packet(s->vc);
1142 #ifdef __sun__
1143 static ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen)
1145 struct strbuf sbuf;
1146 int f = 0;
1148 sbuf.maxlen = maxlen;
1149 sbuf.buf = (char *)buf;
1151 return getmsg(tapfd, NULL, &sbuf, &f) >= 0 ? sbuf.len : -1;
1153 #else
1154 static ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen)
1156 return read(tapfd, buf, maxlen);
1158 #endif
1160 static void tap_send(void *opaque);
1162 static void tap_send_completed(VLANClientState *vc)
1164 TAPState *s = vc->opaque;
1166 qemu_set_fd_handler2(s->fd, tap_can_send, tap_send, NULL, s);
1169 static void tap_send(void *opaque)
1171 TAPState *s = opaque;
1172 int size;
1174 do {
1175 size = tap_read_packet(s->fd, s->buf, sizeof(s->buf));
1176 if (size <= 0) {
1177 break;
1180 size = qemu_send_packet_async(s->vc, s->buf, size, tap_send_completed);
1181 if (size == 0) {
1182 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
1184 } while (size > 0);
1187 int tap_has_vnet_hdr(void *opaque)
1189 VLANClientState *vc = opaque;
1190 TAPState *s = vc->opaque;
1192 return s ? s->has_vnet_hdr : 0;
1195 void tap_using_vnet_hdr(void *opaque, int using_vnet_hdr)
1197 VLANClientState *vc = opaque;
1198 TAPState *s = vc->opaque;
1200 if (!s || !s->has_vnet_hdr)
1201 return;
1203 s->using_vnet_hdr = using_vnet_hdr != 0;
1206 static int tap_probe_vnet_hdr(int fd)
1208 #if defined(TUNGETIFF) && defined(IFF_VNET_HDR)
1209 struct ifreq ifr;
1211 if (ioctl(fd, TUNGETIFF, &ifr) != 0) {
1212 fprintf(stderr, "TUNGETIFF ioctl() failed: %s\n", strerror(errno));
1213 return 0;
1216 return ifr.ifr_flags & IFF_VNET_HDR;
1217 #else
1218 return 0;
1219 #endif
1222 #ifdef TUNSETOFFLOAD
1223 static void tap_set_offload(VLANClientState *vc, int csum, int tso4, int tso6,
1224 int ecn)
1226 TAPState *s = vc->opaque;
1227 unsigned int offload = 0;
1229 if (csum) {
1230 offload |= TUN_F_CSUM;
1231 if (tso4)
1232 offload |= TUN_F_TSO4;
1233 if (tso6)
1234 offload |= TUN_F_TSO6;
1235 if ((tso4 || tso6) && ecn)
1236 offload |= TUN_F_TSO_ECN;
1239 if (ioctl(s->fd, TUNSETOFFLOAD, offload) != 0)
1240 fprintf(stderr, "TUNSETOFFLOAD ioctl() failed: %s\n",
1241 strerror(errno));
1243 #endif /* TUNSETOFFLOAD */
1245 static void tap_cleanup(VLANClientState *vc)
1247 TAPState *s = vc->opaque;
1249 if (s->down_script[0])
1250 launch_script(s->down_script, s->down_script_arg, s->fd);
1252 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1253 close(s->fd);
1254 qemu_free(s);
1257 /* fd support */
1259 static TAPState *net_tap_fd_init(VLANState *vlan,
1260 const char *model,
1261 const char *name,
1262 int fd,
1263 int vnet_hdr)
1265 TAPState *s;
1267 s = qemu_mallocz(sizeof(TAPState));
1268 s->fd = fd;
1269 s->has_vnet_hdr = vnet_hdr != 0;
1270 s->vc = qemu_new_vlan_client(vlan, model, name, NULL, tap_receive,
1271 tap_receive_iov, tap_cleanup, s);
1272 s->vc->receive_raw = tap_receive_raw;
1273 #ifdef TUNSETOFFLOAD
1274 s->vc->set_offload = tap_set_offload;
1275 tap_set_offload(s->vc, 0, 0, 0, 0);
1276 #endif
1277 qemu_set_fd_handler2(s->fd, tap_can_send, tap_send, NULL, s);
1278 snprintf(s->vc->info_str, sizeof(s->vc->info_str), "fd=%d", fd);
1279 return s;
1282 #if defined (HOST_BSD) || defined (__FreeBSD_kernel__)
1283 static int tap_open(char *ifname, int ifname_size)
1285 int fd;
1286 char *dev;
1287 struct stat s;
1289 TFR(fd = open("/dev/tap", O_RDWR));
1290 if (fd < 0) {
1291 fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n");
1292 return -1;
1295 fstat(fd, &s);
1296 dev = devname(s.st_rdev, S_IFCHR);
1297 pstrcpy(ifname, ifname_size, dev);
1299 fcntl(fd, F_SETFL, O_NONBLOCK);
1300 return fd;
1302 #elif defined(__sun__)
1303 #define TUNNEWPPA (('T'<<16) | 0x0001)
1305 * Allocate TAP device, returns opened fd.
1306 * Stores dev name in the first arg(must be large enough).
1308 static int tap_alloc(char *dev, size_t dev_size)
1310 int tap_fd, if_fd, ppa = -1;
1311 static int ip_fd = 0;
1312 char *ptr;
1314 static int arp_fd = 0;
1315 int ip_muxid, arp_muxid;
1316 struct strioctl strioc_if, strioc_ppa;
1317 int link_type = I_PLINK;;
1318 struct lifreq ifr;
1319 char actual_name[32] = "";
1321 memset(&ifr, 0x0, sizeof(ifr));
1323 if( *dev ){
1324 ptr = dev;
1325 while( *ptr && !qemu_isdigit((int)*ptr) ) ptr++;
1326 ppa = atoi(ptr);
1329 /* Check if IP device was opened */
1330 if( ip_fd )
1331 close(ip_fd);
1333 TFR(ip_fd = open("/dev/udp", O_RDWR, 0));
1334 if (ip_fd < 0) {
1335 syslog(LOG_ERR, "Can't open /dev/ip (actually /dev/udp)");
1336 return -1;
1339 TFR(tap_fd = open("/dev/tap", O_RDWR, 0));
1340 if (tap_fd < 0) {
1341 syslog(LOG_ERR, "Can't open /dev/tap");
1342 return -1;
1345 /* Assign a new PPA and get its unit number. */
1346 strioc_ppa.ic_cmd = TUNNEWPPA;
1347 strioc_ppa.ic_timout = 0;
1348 strioc_ppa.ic_len = sizeof(ppa);
1349 strioc_ppa.ic_dp = (char *)&ppa;
1350 if ((ppa = ioctl (tap_fd, I_STR, &strioc_ppa)) < 0)
1351 syslog (LOG_ERR, "Can't assign new interface");
1353 TFR(if_fd = open("/dev/tap", O_RDWR, 0));
1354 if (if_fd < 0) {
1355 syslog(LOG_ERR, "Can't open /dev/tap (2)");
1356 return -1;
1358 if(ioctl(if_fd, I_PUSH, "ip") < 0){
1359 syslog(LOG_ERR, "Can't push IP module");
1360 return -1;
1363 if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0)
1364 syslog(LOG_ERR, "Can't get flags\n");
1366 snprintf (actual_name, 32, "tap%d", ppa);
1367 pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name);
1369 ifr.lifr_ppa = ppa;
1370 /* Assign ppa according to the unit number returned by tun device */
1372 if (ioctl (if_fd, SIOCSLIFNAME, &ifr) < 0)
1373 syslog (LOG_ERR, "Can't set PPA %d", ppa);
1374 if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) <0)
1375 syslog (LOG_ERR, "Can't get flags\n");
1376 /* Push arp module to if_fd */
1377 if (ioctl (if_fd, I_PUSH, "arp") < 0)
1378 syslog (LOG_ERR, "Can't push ARP module (2)");
1380 /* Push arp module to ip_fd */
1381 if (ioctl (ip_fd, I_POP, NULL) < 0)
1382 syslog (LOG_ERR, "I_POP failed\n");
1383 if (ioctl (ip_fd, I_PUSH, "arp") < 0)
1384 syslog (LOG_ERR, "Can't push ARP module (3)\n");
1385 /* Open arp_fd */
1386 TFR(arp_fd = open ("/dev/tap", O_RDWR, 0));
1387 if (arp_fd < 0)
1388 syslog (LOG_ERR, "Can't open %s\n", "/dev/tap");
1390 /* Set ifname to arp */
1391 strioc_if.ic_cmd = SIOCSLIFNAME;
1392 strioc_if.ic_timout = 0;
1393 strioc_if.ic_len = sizeof(ifr);
1394 strioc_if.ic_dp = (char *)&ifr;
1395 if (ioctl(arp_fd, I_STR, &strioc_if) < 0){
1396 syslog (LOG_ERR, "Can't set ifname to arp\n");
1399 if((ip_muxid = ioctl(ip_fd, I_LINK, if_fd)) < 0){
1400 syslog(LOG_ERR, "Can't link TAP device to IP");
1401 return -1;
1404 if ((arp_muxid = ioctl (ip_fd, link_type, arp_fd)) < 0)
1405 syslog (LOG_ERR, "Can't link TAP device to ARP");
1407 close (if_fd);
1409 memset(&ifr, 0x0, sizeof(ifr));
1410 pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name);
1411 ifr.lifr_ip_muxid = ip_muxid;
1412 ifr.lifr_arp_muxid = arp_muxid;
1414 if (ioctl (ip_fd, SIOCSLIFMUXID, &ifr) < 0)
1416 ioctl (ip_fd, I_PUNLINK , arp_muxid);
1417 ioctl (ip_fd, I_PUNLINK, ip_muxid);
1418 syslog (LOG_ERR, "Can't set multiplexor id");
1421 snprintf(dev, dev_size, "tap%d", ppa);
1422 return tap_fd;
1425 static int tap_open(char *ifname, int ifname_size, int *vnet_hdr)
1427 char dev[10]="";
1428 int fd;
1429 if( (fd = tap_alloc(dev, sizeof(dev))) < 0 ){
1430 fprintf(stderr, "Cannot allocate TAP device\n");
1431 return -1;
1433 pstrcpy(ifname, ifname_size, dev);
1434 fcntl(fd, F_SETFL, O_NONBLOCK);
1435 return fd;
1437 #elif defined (_AIX)
1438 static int tap_open(char *ifname, int ifname_size)
1440 fprintf (stderr, "no tap on AIX\n");
1441 return -1;
1443 #else
1444 static int tap_open(char *ifname, int ifname_size, int *vnet_hdr)
1446 struct ifreq ifr;
1447 int fd, ret;
1449 TFR(fd = open("/dev/net/tun", O_RDWR));
1450 if (fd < 0) {
1451 fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
1452 return -1;
1454 memset(&ifr, 0, sizeof(ifr));
1455 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
1457 #if defined(TUNGETFEATURES) && defined(IFF_VNET_HDR)
1459 unsigned int features;
1461 if (ioctl(fd, TUNGETFEATURES, &features) == 0 &&
1462 features & IFF_VNET_HDR) {
1463 *vnet_hdr = 1;
1464 ifr.ifr_flags |= IFF_VNET_HDR;
1467 #endif
1469 if (ifname[0] != '\0')
1470 pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname);
1471 else
1472 pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d");
1473 ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
1474 if (ret != 0) {
1475 fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
1476 close(fd);
1477 return -1;
1479 pstrcpy(ifname, ifname_size, ifr.ifr_name);
1480 fcntl(fd, F_SETFL, O_NONBLOCK);
1481 return fd;
1483 #endif
1485 static int launch_script(const char *setup_script, const char *ifname, int fd)
1487 sigset_t oldmask, mask;
1488 int pid, status;
1489 char *args[3];
1490 char **parg;
1492 sigemptyset(&mask);
1493 sigaddset(&mask, SIGCHLD);
1494 sigprocmask(SIG_BLOCK, &mask, &oldmask);
1496 /* try to launch network script */
1497 pid = fork();
1498 if (pid == 0) {
1499 int open_max = sysconf(_SC_OPEN_MAX), i;
1501 for (i = 0; i < open_max; i++) {
1502 if (i != STDIN_FILENO &&
1503 i != STDOUT_FILENO &&
1504 i != STDERR_FILENO &&
1505 i != fd) {
1506 close(i);
1509 parg = args;
1510 *parg++ = (char *)setup_script;
1511 *parg++ = (char *)ifname;
1512 *parg++ = NULL;
1513 execv(setup_script, args);
1514 _exit(1);
1515 } else if (pid > 0) {
1516 while (waitpid(pid, &status, 0) != pid) {
1517 /* loop */
1519 sigprocmask(SIG_SETMASK, &oldmask, NULL);
1521 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
1522 return 0;
1525 fprintf(stderr, "%s: could not launch network script\n", setup_script);
1526 return -1;
1529 static int net_tap_init(VLANState *vlan, const char *model,
1530 const char *name, const char *ifname1,
1531 const char *setup_script, const char *down_script)
1533 TAPState *s;
1534 int fd;
1535 int vnet_hdr;
1536 char ifname[128];
1538 if (ifname1 != NULL)
1539 pstrcpy(ifname, sizeof(ifname), ifname1);
1540 else
1541 ifname[0] = '\0';
1542 vnet_hdr = 0;
1543 TFR(fd = tap_open(ifname, sizeof(ifname), &vnet_hdr));
1544 if (fd < 0)
1545 return -1;
1547 if (!setup_script || !strcmp(setup_script, "no"))
1548 setup_script = "";
1549 if (setup_script[0] != '\0') {
1550 if (launch_script(setup_script, ifname, fd))
1551 return -1;
1553 s = net_tap_fd_init(vlan, model, name, fd, vnet_hdr);
1554 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1555 "ifname=%s,script=%s,downscript=%s",
1556 ifname, setup_script, down_script);
1557 if (down_script && strcmp(down_script, "no")) {
1558 snprintf(s->down_script, sizeof(s->down_script), "%s", down_script);
1559 snprintf(s->down_script_arg, sizeof(s->down_script_arg), "%s", ifname);
1561 return 0;
1564 #endif /* !_WIN32 */
1566 #if defined(CONFIG_VDE)
1567 typedef struct VDEState {
1568 VLANClientState *vc;
1569 VDECONN *vde;
1570 } VDEState;
1572 static void vde_to_qemu(void *opaque)
1574 VDEState *s = opaque;
1575 uint8_t buf[4096];
1576 int size;
1578 size = vde_recv(s->vde, (char *)buf, sizeof(buf), 0);
1579 if (size > 0) {
1580 qemu_send_packet(s->vc, buf, size);
1584 static ssize_t vde_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
1586 VDEState *s = vc->opaque;
1587 ssize_t ret;
1589 do {
1590 ret = vde_send(s->vde, (const char *)buf, size, 0);
1591 } while (ret < 0 && errno == EINTR);
1593 return ret;
1596 static void vde_cleanup(VLANClientState *vc)
1598 VDEState *s = vc->opaque;
1599 qemu_set_fd_handler(vde_datafd(s->vde), NULL, NULL, NULL);
1600 vde_close(s->vde);
1601 qemu_free(s);
1604 static int net_vde_init(VLANState *vlan, const char *model,
1605 const char *name, const char *sock,
1606 int port, const char *group, int mode)
1608 VDEState *s;
1609 char *init_group = strlen(group) ? (char *)group : NULL;
1610 char *init_sock = strlen(sock) ? (char *)sock : NULL;
1612 struct vde_open_args args = {
1613 .port = port,
1614 .group = init_group,
1615 .mode = mode,
1618 s = qemu_mallocz(sizeof(VDEState));
1619 s->vde = vde_open(init_sock, (char *)"QEMU", &args);
1620 if (!s->vde){
1621 free(s);
1622 return -1;
1624 s->vc = qemu_new_vlan_client(vlan, model, name, NULL, vde_receive,
1625 NULL, vde_cleanup, s);
1626 qemu_set_fd_handler(vde_datafd(s->vde), vde_to_qemu, NULL, s);
1627 snprintf(s->vc->info_str, sizeof(s->vc->info_str), "sock=%s,fd=%d",
1628 sock, vde_datafd(s->vde));
1629 return 0;
1631 #endif
1633 /* network connection */
1634 typedef struct NetSocketState {
1635 VLANClientState *vc;
1636 int fd;
1637 int state; /* 0 = getting length, 1 = getting data */
1638 unsigned int index;
1639 unsigned int packet_len;
1640 uint8_t buf[4096];
1641 struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
1642 } NetSocketState;
1644 typedef struct NetSocketListenState {
1645 VLANState *vlan;
1646 char *model;
1647 char *name;
1648 int fd;
1649 } NetSocketListenState;
1651 /* XXX: we consider we can send the whole packet without blocking */
1652 static ssize_t net_socket_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
1654 NetSocketState *s = vc->opaque;
1655 uint32_t len;
1656 len = htonl(size);
1658 send_all(s->fd, (const uint8_t *)&len, sizeof(len));
1659 return send_all(s->fd, buf, size);
1662 static ssize_t net_socket_receive_dgram(VLANClientState *vc, const uint8_t *buf, size_t size)
1664 NetSocketState *s = vc->opaque;
1666 return sendto(s->fd, (const void *)buf, size, 0,
1667 (struct sockaddr *)&s->dgram_dst, sizeof(s->dgram_dst));
1670 static void net_socket_send(void *opaque)
1672 NetSocketState *s = opaque;
1673 int size, err;
1674 unsigned l;
1675 uint8_t buf1[4096];
1676 const uint8_t *buf;
1678 size = recv(s->fd, (void *)buf1, sizeof(buf1), 0);
1679 if (size < 0) {
1680 err = socket_error();
1681 if (err != EWOULDBLOCK)
1682 goto eoc;
1683 } else if (size == 0) {
1684 /* end of connection */
1685 eoc:
1686 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1687 closesocket(s->fd);
1688 return;
1690 buf = buf1;
1691 while (size > 0) {
1692 /* reassemble a packet from the network */
1693 switch(s->state) {
1694 case 0:
1695 l = 4 - s->index;
1696 if (l > size)
1697 l = size;
1698 memcpy(s->buf + s->index, buf, l);
1699 buf += l;
1700 size -= l;
1701 s->index += l;
1702 if (s->index == 4) {
1703 /* got length */
1704 s->packet_len = ntohl(*(uint32_t *)s->buf);
1705 s->index = 0;
1706 s->state = 1;
1708 break;
1709 case 1:
1710 l = s->packet_len - s->index;
1711 if (l > size)
1712 l = size;
1713 if (s->index + l <= sizeof(s->buf)) {
1714 memcpy(s->buf + s->index, buf, l);
1715 } else {
1716 fprintf(stderr, "serious error: oversized packet received,"
1717 "connection terminated.\n");
1718 s->state = 0;
1719 goto eoc;
1722 s->index += l;
1723 buf += l;
1724 size -= l;
1725 if (s->index >= s->packet_len) {
1726 qemu_send_packet(s->vc, s->buf, s->packet_len);
1727 s->index = 0;
1728 s->state = 0;
1730 break;
1735 static void net_socket_send_dgram(void *opaque)
1737 NetSocketState *s = opaque;
1738 int size;
1740 size = recv(s->fd, (void *)s->buf, sizeof(s->buf), 0);
1741 if (size < 0)
1742 return;
1743 if (size == 0) {
1744 /* end of connection */
1745 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1746 return;
1748 qemu_send_packet(s->vc, s->buf, size);
1751 static int net_socket_mcast_create(struct sockaddr_in *mcastaddr)
1753 struct ip_mreq imr;
1754 int fd;
1755 int val, ret;
1756 if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) {
1757 fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
1758 inet_ntoa(mcastaddr->sin_addr),
1759 (int)ntohl(mcastaddr->sin_addr.s_addr));
1760 return -1;
1763 fd = socket(PF_INET, SOCK_DGRAM, 0);
1764 if (fd < 0) {
1765 perror("socket(PF_INET, SOCK_DGRAM)");
1766 return -1;
1769 val = 1;
1770 ret=setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
1771 (const char *)&val, sizeof(val));
1772 if (ret < 0) {
1773 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
1774 goto fail;
1777 ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr));
1778 if (ret < 0) {
1779 perror("bind");
1780 goto fail;
1783 /* Add host to multicast group */
1784 imr.imr_multiaddr = mcastaddr->sin_addr;
1785 imr.imr_interface.s_addr = htonl(INADDR_ANY);
1787 ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1788 (const char *)&imr, sizeof(struct ip_mreq));
1789 if (ret < 0) {
1790 perror("setsockopt(IP_ADD_MEMBERSHIP)");
1791 goto fail;
1794 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
1795 val = 1;
1796 ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
1797 (const char *)&val, sizeof(val));
1798 if (ret < 0) {
1799 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
1800 goto fail;
1803 socket_set_nonblock(fd);
1804 return fd;
1805 fail:
1806 if (fd >= 0)
1807 closesocket(fd);
1808 return -1;
1811 static void net_socket_cleanup(VLANClientState *vc)
1813 NetSocketState *s = vc->opaque;
1814 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1815 close(s->fd);
1816 qemu_free(s);
1819 static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan,
1820 const char *model,
1821 const char *name,
1822 int fd, int is_connected)
1824 struct sockaddr_in saddr;
1825 int newfd;
1826 socklen_t saddr_len;
1827 NetSocketState *s;
1829 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
1830 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
1831 * by ONLY ONE process: we must "clone" this dgram socket --jjo
1834 if (is_connected) {
1835 if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) {
1836 /* must be bound */
1837 if (saddr.sin_addr.s_addr==0) {
1838 fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
1839 fd);
1840 return NULL;
1842 /* clone dgram socket */
1843 newfd = net_socket_mcast_create(&saddr);
1844 if (newfd < 0) {
1845 /* error already reported by net_socket_mcast_create() */
1846 close(fd);
1847 return NULL;
1849 /* clone newfd to fd, close newfd */
1850 dup2(newfd, fd);
1851 close(newfd);
1853 } else {
1854 fprintf(stderr, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
1855 fd, strerror(errno));
1856 return NULL;
1860 s = qemu_mallocz(sizeof(NetSocketState));
1861 s->fd = fd;
1863 s->vc = qemu_new_vlan_client(vlan, model, name, NULL, net_socket_receive_dgram,
1864 NULL, net_socket_cleanup, s);
1865 qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s);
1867 /* mcast: save bound address as dst */
1868 if (is_connected) s->dgram_dst=saddr;
1870 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1871 "socket: fd=%d (%s mcast=%s:%d)",
1872 fd, is_connected? "cloned" : "",
1873 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1874 return s;
1877 static void net_socket_connect(void *opaque)
1879 NetSocketState *s = opaque;
1880 qemu_set_fd_handler(s->fd, net_socket_send, NULL, s);
1883 static NetSocketState *net_socket_fd_init_stream(VLANState *vlan,
1884 const char *model,
1885 const char *name,
1886 int fd, int is_connected)
1888 NetSocketState *s;
1889 s = qemu_mallocz(sizeof(NetSocketState));
1890 s->fd = fd;
1891 s->vc = qemu_new_vlan_client(vlan, model, name, NULL, net_socket_receive,
1892 NULL, net_socket_cleanup, s);
1893 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1894 "socket: fd=%d", fd);
1895 if (is_connected) {
1896 net_socket_connect(s);
1897 } else {
1898 qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s);
1900 return s;
1903 static NetSocketState *net_socket_fd_init(VLANState *vlan,
1904 const char *model, const char *name,
1905 int fd, int is_connected)
1907 int so_type=-1, optlen=sizeof(so_type);
1909 if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type,
1910 (socklen_t *)&optlen)< 0) {
1911 fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd);
1912 return NULL;
1914 switch(so_type) {
1915 case SOCK_DGRAM:
1916 return net_socket_fd_init_dgram(vlan, model, name, fd, is_connected);
1917 case SOCK_STREAM:
1918 return net_socket_fd_init_stream(vlan, model, name, fd, is_connected);
1919 default:
1920 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
1921 fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd);
1922 return net_socket_fd_init_stream(vlan, model, name, fd, is_connected);
1924 return NULL;
1927 static void net_socket_accept(void *opaque)
1929 NetSocketListenState *s = opaque;
1930 NetSocketState *s1;
1931 struct sockaddr_in saddr;
1932 socklen_t len;
1933 int fd;
1935 for(;;) {
1936 len = sizeof(saddr);
1937 fd = accept(s->fd, (struct sockaddr *)&saddr, &len);
1938 if (fd < 0 && errno != EINTR) {
1939 return;
1940 } else if (fd >= 0) {
1941 break;
1944 s1 = net_socket_fd_init(s->vlan, s->model, s->name, fd, 1);
1945 if (!s1) {
1946 closesocket(fd);
1947 } else {
1948 snprintf(s1->vc->info_str, sizeof(s1->vc->info_str),
1949 "socket: connection from %s:%d",
1950 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1954 static int net_socket_listen_init(VLANState *vlan,
1955 const char *model,
1956 const char *name,
1957 const char *host_str)
1959 NetSocketListenState *s;
1960 int fd, val, ret;
1961 struct sockaddr_in saddr;
1963 if (parse_host_port(&saddr, host_str) < 0)
1964 return -1;
1966 s = qemu_mallocz(sizeof(NetSocketListenState));
1968 fd = socket(PF_INET, SOCK_STREAM, 0);
1969 if (fd < 0) {
1970 perror("socket");
1971 return -1;
1973 socket_set_nonblock(fd);
1975 /* allow fast reuse */
1976 val = 1;
1977 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
1979 ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
1980 if (ret < 0) {
1981 perror("bind");
1982 return -1;
1984 ret = listen(fd, 0);
1985 if (ret < 0) {
1986 perror("listen");
1987 return -1;
1989 s->vlan = vlan;
1990 s->model = strdup(model);
1991 s->name = name ? strdup(name) : NULL;
1992 s->fd = fd;
1993 qemu_set_fd_handler(fd, net_socket_accept, NULL, s);
1994 return 0;
1997 static int net_socket_connect_init(VLANState *vlan,
1998 const char *model,
1999 const char *name,
2000 const char *host_str)
2002 NetSocketState *s;
2003 int fd, connected, ret, err;
2004 struct sockaddr_in saddr;
2006 if (parse_host_port(&saddr, host_str) < 0)
2007 return -1;
2009 fd = socket(PF_INET, SOCK_STREAM, 0);
2010 if (fd < 0) {
2011 perror("socket");
2012 return -1;
2014 socket_set_nonblock(fd);
2016 connected = 0;
2017 for(;;) {
2018 ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
2019 if (ret < 0) {
2020 err = socket_error();
2021 if (err == EINTR || err == EWOULDBLOCK) {
2022 } else if (err == EINPROGRESS) {
2023 break;
2024 #ifdef _WIN32
2025 } else if (err == WSAEALREADY) {
2026 break;
2027 #endif
2028 } else {
2029 perror("connect");
2030 closesocket(fd);
2031 return -1;
2033 } else {
2034 connected = 1;
2035 break;
2038 s = net_socket_fd_init(vlan, model, name, fd, connected);
2039 if (!s)
2040 return -1;
2041 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
2042 "socket: connect to %s:%d",
2043 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
2044 return 0;
2047 static int net_socket_mcast_init(VLANState *vlan,
2048 const char *model,
2049 const char *name,
2050 const char *host_str)
2052 NetSocketState *s;
2053 int fd;
2054 struct sockaddr_in saddr;
2056 if (parse_host_port(&saddr, host_str) < 0)
2057 return -1;
2060 fd = net_socket_mcast_create(&saddr);
2061 if (fd < 0)
2062 return -1;
2064 s = net_socket_fd_init(vlan, model, name, fd, 0);
2065 if (!s)
2066 return -1;
2068 s->dgram_dst = saddr;
2070 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
2071 "socket: mcast=%s:%d",
2072 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
2073 return 0;
2077 typedef struct DumpState {
2078 VLANClientState *pcap_vc;
2079 int fd;
2080 int pcap_caplen;
2081 } DumpState;
2083 #define PCAP_MAGIC 0xa1b2c3d4
2085 struct pcap_file_hdr {
2086 uint32_t magic;
2087 uint16_t version_major;
2088 uint16_t version_minor;
2089 int32_t thiszone;
2090 uint32_t sigfigs;
2091 uint32_t snaplen;
2092 uint32_t linktype;
2095 struct pcap_sf_pkthdr {
2096 struct {
2097 int32_t tv_sec;
2098 int32_t tv_usec;
2099 } ts;
2100 uint32_t caplen;
2101 uint32_t len;
2104 static ssize_t dump_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
2106 DumpState *s = vc->opaque;
2107 struct pcap_sf_pkthdr hdr;
2108 int64_t ts;
2109 int caplen;
2111 /* Early return in case of previous error. */
2112 if (s->fd < 0) {
2113 return size;
2116 ts = muldiv64(qemu_get_clock(vm_clock), 1000000, ticks_per_sec);
2117 caplen = size > s->pcap_caplen ? s->pcap_caplen : size;
2119 hdr.ts.tv_sec = ts / 1000000;
2120 hdr.ts.tv_usec = ts % 1000000;
2121 hdr.caplen = caplen;
2122 hdr.len = size;
2123 if (write(s->fd, &hdr, sizeof(hdr)) != sizeof(hdr) ||
2124 write(s->fd, buf, caplen) != caplen) {
2125 qemu_log("-net dump write error - stop dump\n");
2126 close(s->fd);
2127 s->fd = -1;
2130 return size;
2133 static void net_dump_cleanup(VLANClientState *vc)
2135 DumpState *s = vc->opaque;
2137 close(s->fd);
2138 qemu_free(s);
2141 static int net_dump_init(Monitor *mon, VLANState *vlan, const char *device,
2142 const char *name, const char *filename, int len)
2144 struct pcap_file_hdr hdr;
2145 DumpState *s;
2147 s = qemu_malloc(sizeof(DumpState));
2149 s->fd = open(filename, O_CREAT | O_WRONLY, 0644);
2150 if (s->fd < 0) {
2151 config_error(mon, "-net dump: can't open %s\n", filename);
2152 return -1;
2155 s->pcap_caplen = len;
2157 hdr.magic = PCAP_MAGIC;
2158 hdr.version_major = 2;
2159 hdr.version_minor = 4;
2160 hdr.thiszone = 0;
2161 hdr.sigfigs = 0;
2162 hdr.snaplen = s->pcap_caplen;
2163 hdr.linktype = 1;
2165 if (write(s->fd, &hdr, sizeof(hdr)) < sizeof(hdr)) {
2166 config_error(mon, "-net dump write error: %s\n", strerror(errno));
2167 close(s->fd);
2168 qemu_free(s);
2169 return -1;
2172 s->pcap_vc = qemu_new_vlan_client(vlan, device, name, NULL, dump_receive, NULL,
2173 net_dump_cleanup, s);
2174 snprintf(s->pcap_vc->info_str, sizeof(s->pcap_vc->info_str),
2175 "dump to %s (len=%d)", filename, len);
2176 return 0;
2179 /* find or alloc a new VLAN */
2180 VLANState *qemu_find_vlan(int id)
2182 VLANState **pvlan, *vlan;
2183 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
2184 if (vlan->id == id)
2185 return vlan;
2187 vlan = qemu_mallocz(sizeof(VLANState));
2188 vlan->id = id;
2189 vlan->next = NULL;
2190 pvlan = &first_vlan;
2191 while (*pvlan != NULL)
2192 pvlan = &(*pvlan)->next;
2193 *pvlan = vlan;
2194 return vlan;
2197 static int nic_get_free_idx(void)
2199 int index;
2201 for (index = 0; index < MAX_NICS; index++)
2202 if (!nd_table[index].used)
2203 return index;
2204 return -1;
2207 void qemu_check_nic_model(NICInfo *nd, const char *model)
2209 const char *models[2];
2211 models[0] = model;
2212 models[1] = NULL;
2214 qemu_check_nic_model_list(nd, models, model);
2217 void qemu_check_nic_model_list(NICInfo *nd, const char * const *models,
2218 const char *default_model)
2220 int i, exit_status = 0;
2222 if (!nd->model)
2223 nd->model = strdup(default_model);
2225 if (strcmp(nd->model, "?") != 0) {
2226 for (i = 0 ; models[i]; i++)
2227 if (strcmp(nd->model, models[i]) == 0)
2228 return;
2230 fprintf(stderr, "qemu: Unsupported NIC model: %s\n", nd->model);
2231 exit_status = 1;
2234 fprintf(stderr, "qemu: Supported NIC models: ");
2235 for (i = 0 ; models[i]; i++)
2236 fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n');
2238 exit(exit_status);
2241 int net_client_init(Monitor *mon, const char *device, const char *p)
2243 static const char * const fd_params[] = {
2244 "vlan", "name", "fd", NULL
2246 char buf[1024];
2247 int vlan_id, ret;
2248 VLANState *vlan;
2249 char *name = NULL;
2251 vlan_id = 0;
2252 if (get_param_value(buf, sizeof(buf), "vlan", p)) {
2253 vlan_id = strtol(buf, NULL, 0);
2255 vlan = qemu_find_vlan(vlan_id);
2257 if (get_param_value(buf, sizeof(buf), "name", p)) {
2258 name = qemu_strdup(buf);
2260 if (!strcmp(device, "nic")) {
2261 static const char * const nic_params[] = {
2262 "vlan", "name", "macaddr", "model", NULL
2264 NICInfo *nd;
2265 uint8_t *macaddr;
2266 int idx = nic_get_free_idx();
2268 if (check_params(buf, sizeof(buf), nic_params, p) < 0) {
2269 config_error(mon, "invalid parameter '%s' in '%s'\n", buf, p);
2270 ret = -1;
2271 goto out;
2273 if (idx == -1 || nb_nics >= MAX_NICS) {
2274 config_error(mon, "Too Many NICs\n");
2275 ret = -1;
2276 goto out;
2278 nd = &nd_table[idx];
2279 macaddr = nd->macaddr;
2280 macaddr[0] = 0x52;
2281 macaddr[1] = 0x54;
2282 macaddr[2] = 0x00;
2283 macaddr[3] = 0x12;
2284 macaddr[4] = 0x34;
2285 macaddr[5] = 0x56 + idx;
2287 if (get_param_value(buf, sizeof(buf), "macaddr", p)) {
2288 if (parse_macaddr(macaddr, buf) < 0) {
2289 config_error(mon, "invalid syntax for ethernet address\n");
2290 ret = -1;
2291 goto out;
2294 if (get_param_value(buf, sizeof(buf), "model", p)) {
2295 nd->model = strdup(buf);
2297 nd->vlan = vlan;
2298 nd->name = name;
2299 nd->used = 1;
2300 name = NULL;
2301 nb_nics++;
2302 vlan->nb_guest_devs++;
2303 ret = idx;
2304 } else
2305 if (!strcmp(device, "none")) {
2306 if (*p != '\0') {
2307 config_error(mon, "'none' takes no parameters\n");
2308 ret = -1;
2309 goto out;
2311 /* does nothing. It is needed to signal that no network cards
2312 are wanted */
2313 ret = 0;
2314 } else
2315 #ifdef CONFIG_SLIRP
2316 if (!strcmp(device, "user")) {
2317 static const char * const slirp_params[] = {
2318 "vlan", "name", "hostname", "restrict", "ip", NULL
2320 int restricted = 0;
2321 char *ip = NULL;
2323 if (check_params(buf, sizeof(buf), slirp_params, p) < 0) {
2324 config_error(mon, "invalid parameter '%s' in '%s'\n", buf, p);
2325 ret = -1;
2326 goto out;
2328 if (get_param_value(buf, sizeof(buf), "hostname", p)) {
2329 pstrcpy(slirp_hostname, sizeof(slirp_hostname), buf);
2331 if (get_param_value(buf, sizeof(buf), "restrict", p)) {
2332 restricted = (buf[0] == 'y') ? 1 : 0;
2334 if (get_param_value(buf, sizeof(buf), "ip", p)) {
2335 ip = qemu_strdup(buf);
2337 vlan->nb_host_devs++;
2338 ret = net_slirp_init(vlan, device, name, restricted, ip);
2339 qemu_free(ip);
2340 } else if (!strcmp(device, "channel")) {
2341 long port;
2342 char name[20], *devname;
2343 struct VMChannel *vmc;
2345 port = strtol(p, &devname, 10);
2346 devname++;
2347 if (port < 1 || port > 65535) {
2348 config_error(mon, "vmchannel wrong port number\n");
2349 ret = -1;
2350 goto out;
2352 vmc = malloc(sizeof(struct VMChannel));
2353 snprintf(name, 20, "vmchannel%ld", port);
2354 vmc->hd = qemu_chr_open(name, devname, NULL);
2355 if (!vmc->hd) {
2356 config_error(mon, "could not open vmchannel device '%s'\n",
2357 devname);
2358 ret = -1;
2359 goto out;
2361 vmc->port = port;
2362 slirp_add_exec(3, vmc->hd, 4, port);
2363 qemu_chr_add_handlers(vmc->hd, vmchannel_can_read, vmchannel_read,
2364 NULL, vmc);
2365 ret = 0;
2366 } else
2367 #endif
2368 #ifdef _WIN32
2369 if (!strcmp(device, "tap")) {
2370 static const char * const tap_params[] = {
2371 "vlan", "name", "ifname", NULL
2373 char ifname[64];
2375 if (check_params(buf, sizeof(buf), tap_params, p) < 0) {
2376 config_error(mon, "invalid parameter '%s' in '%s'\n", buf, p);
2377 ret = -1;
2378 goto out;
2380 if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
2381 config_error(mon, "tap: no interface name\n");
2382 ret = -1;
2383 goto out;
2385 vlan->nb_host_devs++;
2386 ret = tap_win32_init(vlan, device, name, ifname);
2387 } else
2388 #elif defined (_AIX)
2389 #else
2390 if (!strcmp(device, "tap")) {
2391 char ifname[64], chkbuf[64];
2392 char setup_script[1024], down_script[1024];
2393 int fd;
2394 vlan->nb_host_devs++;
2395 if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
2396 if (check_params(chkbuf, sizeof(chkbuf), fd_params, p) < 0) {
2397 config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
2398 ret = -1;
2399 goto out;
2401 fd = strtol(buf, NULL, 0);
2402 fcntl(fd, F_SETFL, O_NONBLOCK);
2403 net_tap_fd_init(vlan, device, name, fd, tap_probe_vnet_hdr(fd));
2404 ret = 0;
2405 } else {
2406 static const char * const tap_params[] = {
2407 "vlan", "name", "ifname", "script", "downscript", NULL
2409 if (check_params(chkbuf, sizeof(chkbuf), tap_params, p) < 0) {
2410 config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
2411 ret = -1;
2412 goto out;
2414 if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
2415 ifname[0] = '\0';
2417 if (get_param_value(setup_script, sizeof(setup_script), "script", p) == 0) {
2418 pstrcpy(setup_script, sizeof(setup_script), DEFAULT_NETWORK_SCRIPT);
2420 if (get_param_value(down_script, sizeof(down_script), "downscript", p) == 0) {
2421 pstrcpy(down_script, sizeof(down_script), DEFAULT_NETWORK_DOWN_SCRIPT);
2423 ret = net_tap_init(vlan, device, name, ifname, setup_script, down_script);
2425 } else
2426 #endif
2427 if (!strcmp(device, "socket")) {
2428 char chkbuf[64];
2429 if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
2430 int fd;
2431 if (check_params(chkbuf, sizeof(chkbuf), fd_params, p) < 0) {
2432 config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
2433 ret = -1;
2434 goto out;
2436 fd = strtol(buf, NULL, 0);
2437 ret = -1;
2438 if (net_socket_fd_init(vlan, device, name, fd, 1))
2439 ret = 0;
2440 } else if (get_param_value(buf, sizeof(buf), "listen", p) > 0) {
2441 static const char * const listen_params[] = {
2442 "vlan", "name", "listen", NULL
2444 if (check_params(chkbuf, sizeof(chkbuf), listen_params, p) < 0) {
2445 config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
2446 ret = -1;
2447 goto out;
2449 ret = net_socket_listen_init(vlan, device, name, buf);
2450 } else if (get_param_value(buf, sizeof(buf), "connect", p) > 0) {
2451 static const char * const connect_params[] = {
2452 "vlan", "name", "connect", NULL
2454 if (check_params(chkbuf, sizeof(chkbuf), connect_params, p) < 0) {
2455 config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
2456 ret = -1;
2457 goto out;
2459 ret = net_socket_connect_init(vlan, device, name, buf);
2460 } else if (get_param_value(buf, sizeof(buf), "mcast", p) > 0) {
2461 static const char * const mcast_params[] = {
2462 "vlan", "name", "mcast", NULL
2464 if (check_params(chkbuf, sizeof(chkbuf), mcast_params, p) < 0) {
2465 config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
2466 ret = -1;
2467 goto out;
2469 ret = net_socket_mcast_init(vlan, device, name, buf);
2470 } else {
2471 config_error(mon, "Unknown socket options: %s\n", p);
2472 ret = -1;
2473 goto out;
2475 vlan->nb_host_devs++;
2476 } else
2477 #ifdef CONFIG_VDE
2478 if (!strcmp(device, "vde")) {
2479 static const char * const vde_params[] = {
2480 "vlan", "name", "sock", "port", "group", "mode", NULL
2482 char vde_sock[1024], vde_group[512];
2483 int vde_port, vde_mode;
2485 if (check_params(buf, sizeof(buf), vde_params, p) < 0) {
2486 config_error(mon, "invalid parameter '%s' in '%s'\n", buf, p);
2487 ret = -1;
2488 goto out;
2490 vlan->nb_host_devs++;
2491 if (get_param_value(vde_sock, sizeof(vde_sock), "sock", p) <= 0) {
2492 vde_sock[0] = '\0';
2494 if (get_param_value(buf, sizeof(buf), "port", p) > 0) {
2495 vde_port = strtol(buf, NULL, 10);
2496 } else {
2497 vde_port = 0;
2499 if (get_param_value(vde_group, sizeof(vde_group), "group", p) <= 0) {
2500 vde_group[0] = '\0';
2502 if (get_param_value(buf, sizeof(buf), "mode", p) > 0) {
2503 vde_mode = strtol(buf, NULL, 8);
2504 } else {
2505 vde_mode = 0700;
2507 ret = net_vde_init(vlan, device, name, vde_sock, vde_port, vde_group, vde_mode);
2508 } else
2509 #endif
2510 if (!strcmp(device, "dump")) {
2511 int len = 65536;
2513 if (get_param_value(buf, sizeof(buf), "len", p) > 0) {
2514 len = strtol(buf, NULL, 0);
2516 if (!get_param_value(buf, sizeof(buf), "file", p)) {
2517 snprintf(buf, sizeof(buf), "qemu-vlan%d.pcap", vlan_id);
2519 ret = net_dump_init(mon, vlan, device, name, buf, len);
2520 } else {
2521 config_error(mon, "Unknown network device: %s\n", device);
2522 ret = -1;
2523 goto out;
2525 if (ret < 0) {
2526 config_error(mon, "Could not initialize device '%s'\n", device);
2528 out:
2529 qemu_free(name);
2530 return ret;
2533 void net_client_uninit(NICInfo *nd)
2535 nd->vlan->nb_guest_devs--;
2536 nb_nics--;
2537 nd->used = 0;
2538 free((void *)nd->model);
2541 static int net_host_check_device(const char *device)
2543 int i;
2544 const char *valid_param_list[] = { "tap", "socket", "dump"
2545 #ifdef CONFIG_SLIRP
2546 ,"user"
2547 #endif
2548 #ifdef CONFIG_VDE
2549 ,"vde"
2550 #endif
2552 for (i = 0; i < sizeof(valid_param_list) / sizeof(char *); i++) {
2553 if (!strncmp(valid_param_list[i], device,
2554 strlen(valid_param_list[i])))
2555 return 1;
2558 return 0;
2561 void net_host_device_add(Monitor *mon, const char *device, const char *opts)
2563 if (!net_host_check_device(device)) {
2564 monitor_printf(mon, "invalid host network device %s\n", device);
2565 return;
2567 if (net_client_init(mon, device, opts ? opts : "") < 0) {
2568 monitor_printf(mon, "adding host network device %s failed\n", device);
2572 void net_host_device_remove(Monitor *mon, int vlan_id, const char *device)
2574 VLANState *vlan;
2575 VLANClientState *vc;
2577 vlan = qemu_find_vlan(vlan_id);
2579 for (vc = vlan->first_client; vc != NULL; vc = vc->next) {
2580 if (!strcmp(vc->name, device)) {
2581 break;
2585 if (!vc) {
2586 monitor_printf(mon, "can't find device %s\n", device);
2587 return;
2589 if (!net_host_check_device(vc->model)) {
2590 monitor_printf(mon, "invalid host network device %s\n", device);
2591 return;
2593 qemu_del_vlan_client(vc);
2596 int net_client_parse(const char *str)
2598 const char *p;
2599 char *q;
2600 char device[64];
2602 p = str;
2603 q = device;
2604 while (*p != '\0' && *p != ',') {
2605 if ((q - device) < sizeof(device) - 1)
2606 *q++ = *p;
2607 p++;
2609 *q = '\0';
2610 if (*p == ',')
2611 p++;
2613 return net_client_init(NULL, device, p);
2616 void do_info_network(Monitor *mon)
2618 VLANState *vlan;
2619 VLANClientState *vc;
2621 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
2622 monitor_printf(mon, "VLAN %d devices:\n", vlan->id);
2623 for(vc = vlan->first_client; vc != NULL; vc = vc->next)
2624 monitor_printf(mon, " %s: %s\n", vc->name, vc->info_str);
2628 int do_set_link(Monitor *mon, const char *name, const char *up_or_down)
2630 VLANState *vlan;
2631 VLANClientState *vc = NULL;
2633 for (vlan = first_vlan; vlan != NULL; vlan = vlan->next)
2634 for (vc = vlan->first_client; vc != NULL; vc = vc->next)
2635 if (strcmp(vc->name, name) == 0)
2636 goto done;
2637 done:
2639 if (!vc) {
2640 monitor_printf(mon, "could not find network device '%s'", name);
2641 return 0;
2644 if (strcmp(up_or_down, "up") == 0)
2645 vc->link_down = 0;
2646 else if (strcmp(up_or_down, "down") == 0)
2647 vc->link_down = 1;
2648 else
2649 monitor_printf(mon, "invalid link status '%s'; only 'up' or 'down' "
2650 "valid\n", up_or_down);
2652 if (vc->link_status_changed)
2653 vc->link_status_changed(vc);
2655 return 1;
2658 void net_cleanup(void)
2660 VLANState *vlan;
2662 /* close network clients */
2663 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
2664 VLANClientState *vc = vlan->first_client;
2666 while (vc) {
2667 VLANClientState *next = vc->next;
2669 qemu_del_vlan_client(vc);
2671 vc = next;
2676 void net_client_check(void)
2678 VLANState *vlan;
2680 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
2681 if (vlan->nb_guest_devs == 0 && vlan->nb_host_devs == 0)
2682 continue;
2683 if (vlan->nb_guest_devs == 0)
2684 fprintf(stderr, "Warning: vlan %d with no nics\n", vlan->id);
2685 if (vlan->nb_host_devs == 0)
2686 fprintf(stderr,
2687 "Warning: vlan %d is not connected to host network\n",
2688 vlan->id);