Merge commit '1f7babf6d5ca24460694556d617cd17ae8be2ed4' into upstream-merge
[qemu-kvm/fedora.git] / net.c
blobc8e05a897f4672a523059e5548c9b8ee50421e0e
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_purge_queued_packets(VLANClientState *vc)
449 VLANPacket **pp = &vc->vlan->send_queue;
451 while (*pp != NULL) {
452 VLANPacket *packet = *pp;
454 if (packet->sender == vc) {
455 *pp = packet->next;
456 qemu_free(packet);
457 } else {
458 pp = &packet->next;
463 void qemu_flush_queued_packets(VLANClientState *vc)
465 VLANPacket *packet;
467 while ((packet = vc->vlan->send_queue) != NULL) {
468 int ret;
470 vc->vlan->send_queue = packet->next;
472 ret = qemu_deliver_packet(packet->sender, packet->data,
473 packet->size, packet->raw);
474 if (ret == 0 && packet->sent_cb != NULL) {
475 packet->next = vc->vlan->send_queue;
476 vc->vlan->send_queue = packet;
477 break;
480 if (packet->sent_cb)
481 packet->sent_cb(packet->sender);
483 qemu_free(packet);
487 static void qemu_enqueue_packet(VLANClientState *sender,
488 const uint8_t *buf, int size, int raw,
489 NetPacketSent *sent_cb)
491 VLANPacket *packet;
493 packet = qemu_malloc(sizeof(VLANPacket) + size);
494 packet->next = sender->vlan->send_queue;
495 packet->sender = sender;
496 packet->size = size;
497 packet->raw = raw;
498 packet->sent_cb = sent_cb;
499 memcpy(packet->data, buf, size);
500 sender->vlan->send_queue = packet;
503 static ssize_t qemu_send_packet_async2(VLANClientState *sender,
504 const uint8_t *buf, int size, int raw,
505 NetPacketSent *sent_cb)
507 int ret;
509 if (sender->link_down) {
510 return size;
513 #ifdef DEBUG_NET
514 printf("vlan %d send:\n", sender->vlan->id);
515 hex_dump(stdout, buf, size);
516 #endif
518 if (sender->vlan->delivering) {
519 qemu_enqueue_packet(sender, buf, size, raw, NULL);
520 return size;
523 ret = qemu_deliver_packet(sender, buf, size, raw);
524 if (ret == 0 && sent_cb != NULL) {
525 qemu_enqueue_packet(sender, buf, size, raw, sent_cb);
526 return 0;
529 qemu_flush_queued_packets(sender);
531 return ret;
534 ssize_t qemu_send_packet_async(VLANClientState *sender,
535 const uint8_t *buf, int size,
536 NetPacketSent *sent_cb)
538 return qemu_send_packet_async2(sender, buf, size, 0, sent_cb);
541 ssize_t qemu_send_packet(VLANClientState *sender, const uint8_t *buf, int size)
543 return qemu_send_packet_async2(sender, buf, size, 0, NULL);
546 ssize_t qemu_send_packet_raw(VLANClientState *sender, const uint8_t *buf, int size)
548 return qemu_send_packet_async2(sender, buf, size, 1, NULL);
551 static ssize_t vc_sendv_compat(VLANClientState *vc, const struct iovec *iov,
552 int iovcnt)
554 uint8_t buffer[4096];
555 size_t offset = 0;
556 int i;
558 for (i = 0; i < iovcnt; i++) {
559 size_t len;
561 len = MIN(sizeof(buffer) - offset, iov[i].iov_len);
562 memcpy(buffer + offset, iov[i].iov_base, len);
563 offset += len;
566 return vc->receive(vc, buffer, offset);
569 static ssize_t calc_iov_length(const struct iovec *iov, int iovcnt)
571 size_t offset = 0;
572 int i;
574 for (i = 0; i < iovcnt; i++)
575 offset += iov[i].iov_len;
576 return offset;
579 static int qemu_deliver_packet_iov(VLANClientState *sender,
580 const struct iovec *iov, int iovcnt)
582 VLANClientState *vc;
583 int ret = -1;
585 sender->vlan->delivering = 1;
587 for (vc = sender->vlan->first_client; vc != NULL; vc = vc->next) {
588 ssize_t len;
590 if (vc == sender) {
591 continue;
594 if (vc->link_down) {
595 ret = calc_iov_length(iov, iovcnt);
596 continue;
599 if (vc->receive_iov) {
600 len = vc->receive_iov(vc, iov, iovcnt);
601 } else {
602 len = vc_sendv_compat(vc, iov, iovcnt);
605 ret = (ret >= 0) ? ret : len;
608 sender->vlan->delivering = 0;
610 return ret;
613 static ssize_t qemu_enqueue_packet_iov(VLANClientState *sender,
614 const struct iovec *iov, int iovcnt,
615 NetPacketSent *sent_cb)
617 VLANPacket *packet;
618 size_t max_len = 0;
619 int i;
621 max_len = calc_iov_length(iov, iovcnt);
623 packet = qemu_malloc(sizeof(VLANPacket) + max_len);
624 packet->next = sender->vlan->send_queue;
625 packet->sender = sender;
626 packet->sent_cb = sent_cb;
627 packet->size = 0;
628 packet->raw = 0;
630 for (i = 0; i < iovcnt; i++) {
631 size_t len = iov[i].iov_len;
633 memcpy(packet->data + packet->size, iov[i].iov_base, len);
634 packet->size += len;
637 sender->vlan->send_queue = packet;
639 return packet->size;
642 ssize_t qemu_sendv_packet_async(VLANClientState *sender,
643 const struct iovec *iov, int iovcnt,
644 NetPacketSent *sent_cb)
646 int ret;
648 if (sender->link_down) {
649 return calc_iov_length(iov, iovcnt);
652 if (sender->vlan->delivering) {
653 return qemu_enqueue_packet_iov(sender, iov, iovcnt, NULL);
656 ret = qemu_deliver_packet_iov(sender, iov, iovcnt);
657 if (ret == 0 && sent_cb != NULL) {
658 qemu_enqueue_packet_iov(sender, iov, iovcnt, sent_cb);
659 return 0;
662 qemu_flush_queued_packets(sender);
664 return ret;
667 ssize_t
668 qemu_sendv_packet(VLANClientState *vc, const struct iovec *iov, int iovcnt)
670 return qemu_sendv_packet_async(vc, iov, iovcnt, NULL);
673 static void config_error(Monitor *mon, const char *fmt, ...)
675 va_list ap;
677 va_start(ap, fmt);
678 if (mon) {
679 monitor_vprintf(mon, fmt, ap);
680 } else {
681 fprintf(stderr, "qemu: ");
682 vfprintf(stderr, fmt, ap);
683 exit(1);
685 va_end(ap);
688 #if defined(CONFIG_SLIRP)
690 /* slirp network adapter */
692 struct slirp_config_str {
693 struct slirp_config_str *next;
694 const char *str;
697 static int slirp_inited;
698 static struct slirp_config_str *slirp_redirs;
699 #ifndef _WIN32
700 static const char *slirp_smb_export;
701 #endif
702 static VLANClientState *slirp_vc;
704 #ifndef _WIN32
705 static void slirp_smb(const char *exported_dir);
706 #endif
707 static void slirp_redirection(Monitor *mon, const char *redir_str);
709 int slirp_can_output(void)
711 return !slirp_vc || qemu_can_send_packet(slirp_vc);
714 void slirp_output(const uint8_t *pkt, int pkt_len)
716 #ifdef DEBUG_SLIRP
717 printf("slirp output:\n");
718 hex_dump(stdout, pkt, pkt_len);
719 #endif
720 if (!slirp_vc)
721 return;
722 qemu_send_packet(slirp_vc, pkt, pkt_len);
725 int slirp_is_inited(void)
727 return slirp_inited;
730 static ssize_t slirp_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
732 #ifdef DEBUG_SLIRP
733 printf("slirp input:\n");
734 hex_dump(stdout, buf, size);
735 #endif
736 slirp_input(buf, size);
737 return size;
740 static int slirp_in_use;
742 static void net_slirp_cleanup(VLANClientState *vc)
744 slirp_in_use = 0;
747 static int net_slirp_init(VLANState *vlan, const char *model, const char *name,
748 int restricted, const char *ip)
750 if (slirp_in_use) {
751 /* slirp only supports a single instance so far */
752 return -1;
754 if (!slirp_inited) {
755 slirp_inited = 1;
756 slirp_init(restricted, ip);
758 while (slirp_redirs) {
759 struct slirp_config_str *config = slirp_redirs;
761 slirp_redirection(NULL, config->str);
762 slirp_redirs = config->next;
763 qemu_free(config);
765 #ifndef _WIN32
766 if (slirp_smb_export) {
767 slirp_smb(slirp_smb_export);
769 #endif
772 slirp_vc = qemu_new_vlan_client(vlan, model, name, NULL, slirp_receive,
773 NULL, net_slirp_cleanup, NULL);
774 slirp_vc->info_str[0] = '\0';
775 slirp_in_use = 1;
776 return 0;
779 static void net_slirp_redir_print(void *opaque, int is_udp,
780 struct in_addr *laddr, u_int lport,
781 struct in_addr *faddr, u_int fport)
783 Monitor *mon = (Monitor *)opaque;
784 uint32_t h_addr;
785 uint32_t g_addr;
786 char buf[16];
788 h_addr = ntohl(faddr->s_addr);
789 g_addr = ntohl(laddr->s_addr);
791 monitor_printf(mon, " %s |", is_udp ? "udp" : "tcp" );
792 snprintf(buf, 15, "%d.%d.%d.%d", (h_addr >> 24) & 0xff,
793 (h_addr >> 16) & 0xff,
794 (h_addr >> 8) & 0xff,
795 (h_addr) & 0xff);
796 monitor_printf(mon, " %15s |", buf);
797 monitor_printf(mon, " %5d |", fport);
799 snprintf(buf, 15, "%d.%d.%d.%d", (g_addr >> 24) & 0xff,
800 (g_addr >> 16) & 0xff,
801 (g_addr >> 8) & 0xff,
802 (g_addr) & 0xff);
803 monitor_printf(mon, " %15s |", buf);
804 monitor_printf(mon, " %5d\n", lport);
808 static void net_slirp_redir_list(Monitor *mon)
810 if (!mon)
811 return;
813 monitor_printf(mon, " Prot | Host Addr | HPort | Guest Addr | GPort\n");
814 monitor_printf(mon, " | | | | \n");
815 slirp_redir_loop(net_slirp_redir_print, mon);
818 static void net_slirp_redir_rm(Monitor *mon, const char *port_str)
820 int host_port;
821 char buf[256] = "";
822 const char *p = port_str;
823 int is_udp = 0;
824 int n;
826 if (!mon)
827 return;
829 if (!port_str || !port_str[0])
830 goto fail_syntax;
832 get_str_sep(buf, sizeof(buf), &p, ':');
834 if (!strcmp(buf, "tcp") || buf[0] == '\0') {
835 is_udp = 0;
836 } else if (!strcmp(buf, "udp")) {
837 is_udp = 1;
838 } else {
839 goto fail_syntax;
842 host_port = atoi(p);
844 n = slirp_redir_rm(is_udp, host_port);
846 monitor_printf(mon, "removed %d redirections to %s port %d\n", n,
847 is_udp ? "udp" : "tcp", host_port);
848 return;
850 fail_syntax:
851 monitor_printf(mon, "invalid format\n");
854 static void slirp_redirection(Monitor *mon, const char *redir_str)
856 struct in_addr guest_addr;
857 int host_port, guest_port;
858 const char *p;
859 char buf[256], *r;
860 int is_udp;
862 p = redir_str;
863 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
864 goto fail_syntax;
866 if (!strcmp(buf, "tcp") || buf[0] == '\0') {
867 is_udp = 0;
868 } else if (!strcmp(buf, "udp")) {
869 is_udp = 1;
870 } else {
871 goto fail_syntax;
874 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
875 goto fail_syntax;
877 host_port = strtol(buf, &r, 0);
878 if (r == buf) {
879 goto fail_syntax;
882 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
883 goto fail_syntax;
885 if (buf[0] == '\0') {
886 pstrcpy(buf, sizeof(buf), "10.0.2.15");
888 if (!inet_aton(buf, &guest_addr)) {
889 goto fail_syntax;
892 guest_port = strtol(p, &r, 0);
893 if (r == p) {
894 goto fail_syntax;
897 if (slirp_redir(is_udp, host_port, guest_addr, guest_port) < 0) {
898 config_error(mon, "could not set up redirection '%s'\n", redir_str);
900 return;
902 fail_syntax:
903 config_error(mon, "invalid redirection format '%s'\n", redir_str);
906 void net_slirp_redir(Monitor *mon, const char *redir_str, const char *redir_opt2)
908 struct slirp_config_str *config;
910 if (!slirp_inited) {
911 if (mon) {
912 monitor_printf(mon, "user mode network stack not in use\n");
913 } else {
914 config = qemu_malloc(sizeof(*config));
915 config->str = redir_str;
916 config->next = slirp_redirs;
917 slirp_redirs = config;
919 return;
922 if (!strcmp(redir_str, "remove")) {
923 net_slirp_redir_rm(mon, redir_opt2);
924 return;
927 if (!strcmp(redir_str, "list")) {
928 net_slirp_redir_list(mon);
929 return;
932 slirp_redirection(mon, redir_str);
935 #ifndef _WIN32
937 static char smb_dir[1024];
939 static void erase_dir(char *dir_name)
941 DIR *d;
942 struct dirent *de;
943 char filename[1024];
945 /* erase all the files in the directory */
946 if ((d = opendir(dir_name)) != NULL) {
947 for(;;) {
948 de = readdir(d);
949 if (!de)
950 break;
951 if (strcmp(de->d_name, ".") != 0 &&
952 strcmp(de->d_name, "..") != 0) {
953 snprintf(filename, sizeof(filename), "%s/%s",
954 smb_dir, de->d_name);
955 if (unlink(filename) != 0) /* is it a directory? */
956 erase_dir(filename);
959 closedir(d);
960 rmdir(dir_name);
964 /* automatic user mode samba server configuration */
965 static void smb_exit(void)
967 erase_dir(smb_dir);
970 static void slirp_smb(const char *exported_dir)
972 char smb_conf[1024];
973 char smb_cmdline[1024];
974 FILE *f;
976 /* XXX: better tmp dir construction */
977 snprintf(smb_dir, sizeof(smb_dir), "/tmp/qemu-smb.%ld", (long)getpid());
978 if (mkdir(smb_dir, 0700) < 0) {
979 fprintf(stderr, "qemu: could not create samba server dir '%s'\n", smb_dir);
980 exit(1);
982 snprintf(smb_conf, sizeof(smb_conf), "%s/%s", smb_dir, "smb.conf");
984 f = fopen(smb_conf, "w");
985 if (!f) {
986 fprintf(stderr, "qemu: could not create samba server configuration file '%s'\n", smb_conf);
987 exit(1);
989 fprintf(f,
990 "[global]\n"
991 "private dir=%s\n"
992 "smb ports=0\n"
993 "socket address=127.0.0.1\n"
994 "pid directory=%s\n"
995 "lock directory=%s\n"
996 "log file=%s/log.smbd\n"
997 "smb passwd file=%s/smbpasswd\n"
998 "security = share\n"
999 "[qemu]\n"
1000 "path=%s\n"
1001 "read only=no\n"
1002 "guest ok=yes\n",
1003 smb_dir,
1004 smb_dir,
1005 smb_dir,
1006 smb_dir,
1007 smb_dir,
1008 exported_dir
1010 fclose(f);
1011 atexit(smb_exit);
1013 snprintf(smb_cmdline, sizeof(smb_cmdline), "%s -s %s",
1014 SMBD_COMMAND, smb_conf);
1016 slirp_add_exec(0, smb_cmdline, 4, 139);
1019 /* automatic user mode samba server configuration */
1020 void net_slirp_smb(const char *exported_dir)
1022 if (slirp_smb_export) {
1023 fprintf(stderr, "-smb given twice\n");
1024 exit(1);
1026 slirp_smb_export = exported_dir;
1027 if (slirp_inited) {
1028 slirp_smb(exported_dir);
1032 #endif /* !defined(_WIN32) */
1034 void do_info_slirp(Monitor *mon)
1036 slirp_stats();
1039 struct VMChannel {
1040 CharDriverState *hd;
1041 int port;
1044 static int vmchannel_can_read(void *opaque)
1046 struct VMChannel *vmc = (struct VMChannel*)opaque;
1047 return slirp_socket_can_recv(4, vmc->port);
1050 static void vmchannel_read(void *opaque, const uint8_t *buf, int size)
1052 struct VMChannel *vmc = (struct VMChannel*)opaque;
1053 slirp_socket_recv(4, vmc->port, buf, size);
1056 #endif /* CONFIG_SLIRP */
1058 #ifdef _WIN32
1060 int tap_has_vnet_hdr(void *opaque)
1062 return 0;
1065 void tap_using_vnet_hdr(void *opaque, int using_vnet_hdr)
1069 #else /* !defined(_WIN32) */
1071 /* Maximum GSO packet size (64k) plus plenty of room for
1072 * the ethernet and virtio_net headers
1074 #define TAP_BUFSIZE (4096 + 65536)
1076 #ifdef IFF_VNET_HDR
1077 #include <linux/virtio_net.h>
1078 #endif
1080 typedef struct TAPState {
1081 VLANClientState *vc;
1082 int fd;
1083 char down_script[1024];
1084 char down_script_arg[128];
1085 uint8_t buf[TAP_BUFSIZE];
1086 unsigned int read_poll : 1;
1087 unsigned int write_poll : 1;
1088 unsigned int has_vnet_hdr : 1;
1089 unsigned int using_vnet_hdr : 1;
1090 } TAPState;
1092 static int launch_script(const char *setup_script, const char *ifname, int fd);
1094 static int tap_can_send(void *opaque);
1095 static void tap_send(void *opaque);
1096 static void tap_writable(void *opaque);
1098 static void tap_update_fd_handler(TAPState *s)
1100 qemu_set_fd_handler2(s->fd,
1101 s->read_poll ? tap_can_send : NULL,
1102 s->read_poll ? tap_send : NULL,
1103 s->write_poll ? tap_writable : NULL,
1107 static void tap_read_poll(TAPState *s, int enable)
1109 s->read_poll = !!enable;
1110 tap_update_fd_handler(s);
1113 static void tap_write_poll(TAPState *s, int enable)
1115 s->write_poll = !!enable;
1116 tap_update_fd_handler(s);
1119 static void tap_writable(void *opaque)
1121 TAPState *s = opaque;
1123 tap_write_poll(s, 0);
1125 qemu_flush_queued_packets(s->vc);
1128 static ssize_t tap_receive_iov(VLANClientState *vc, const struct iovec *iov,
1129 int iovcnt)
1131 TAPState *s = vc->opaque;
1132 ssize_t len;
1134 do {
1135 len = writev(s->fd, iov, iovcnt);
1136 } while (len == -1 && errno == EINTR);
1138 if (len == -1 && errno == EAGAIN) {
1139 tap_write_poll(s, 1);
1140 return 0;
1143 return len;
1146 static ssize_t tap_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
1148 struct iovec iov[2];
1149 int i = 0;
1151 #ifdef IFF_VNET_HDR
1152 TAPState *s = vc->opaque;
1153 struct virtio_net_hdr hdr = { 0, };
1155 if (s->has_vnet_hdr && !s->using_vnet_hdr) {
1156 iov[i].iov_base = &hdr;
1157 iov[i].iov_len = sizeof(hdr);
1158 i++;
1160 #endif
1162 iov[i].iov_base = (char *) buf;
1163 iov[i].iov_len = size;
1164 i++;
1166 return tap_receive_iov(vc, iov, i);
1169 static ssize_t tap_receive_raw(VLANClientState *vc, const uint8_t *buf, size_t size)
1171 struct iovec iov[2];
1172 int i = 0;
1174 #ifdef IFF_VNET_HDR
1175 TAPState *s = vc->opaque;
1176 struct virtio_net_hdr hdr = { 0, };
1178 if (s->has_vnet_hdr && s->using_vnet_hdr) {
1179 iov[i].iov_base = &hdr;
1180 iov[i].iov_len = sizeof(hdr);
1181 i++;
1183 #endif
1185 iov[i].iov_base = (char *) buf;
1186 iov[i].iov_len = size;
1187 i++;
1189 return tap_receive_iov(vc, iov, i);
1192 static int tap_can_send(void *opaque)
1194 TAPState *s = opaque;
1196 return qemu_can_send_packet(s->vc);
1199 #ifdef __sun__
1200 static ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen)
1202 struct strbuf sbuf;
1203 int f = 0;
1205 sbuf.maxlen = maxlen;
1206 sbuf.buf = (char *)buf;
1208 return getmsg(tapfd, NULL, &sbuf, &f) >= 0 ? sbuf.len : -1;
1210 #else
1211 static ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen)
1213 return read(tapfd, buf, maxlen);
1215 #endif
1217 static void tap_send_completed(VLANClientState *vc)
1219 TAPState *s = vc->opaque;
1220 tap_read_poll(s, 1);
1223 static void tap_send(void *opaque)
1225 TAPState *s = opaque;
1226 int size;
1228 do {
1229 size = tap_read_packet(s->fd, s->buf, sizeof(s->buf));
1230 if (size <= 0) {
1231 break;
1234 size = qemu_send_packet_async(s->vc, s->buf, size, tap_send_completed);
1235 if (size == 0) {
1236 tap_read_poll(s, 0);
1238 } while (size > 0);
1241 int tap_has_vnet_hdr(void *opaque)
1243 VLANClientState *vc = opaque;
1244 TAPState *s = vc->opaque;
1246 return s ? s->has_vnet_hdr : 0;
1249 void tap_using_vnet_hdr(void *opaque, int using_vnet_hdr)
1251 VLANClientState *vc = opaque;
1252 TAPState *s = vc->opaque;
1254 if (!s || !s->has_vnet_hdr)
1255 return;
1257 s->using_vnet_hdr = using_vnet_hdr != 0;
1260 static int tap_probe_vnet_hdr(int fd)
1262 #if defined(TUNGETIFF) && defined(IFF_VNET_HDR)
1263 struct ifreq ifr;
1265 if (ioctl(fd, TUNGETIFF, &ifr) != 0) {
1266 fprintf(stderr, "TUNGETIFF ioctl() failed: %s\n", strerror(errno));
1267 return 0;
1270 return ifr.ifr_flags & IFF_VNET_HDR;
1271 #else
1272 return 0;
1273 #endif
1276 #ifdef TUNSETOFFLOAD
1277 static void tap_set_offload(VLANClientState *vc, int csum, int tso4, int tso6,
1278 int ecn)
1280 TAPState *s = vc->opaque;
1281 unsigned int offload = 0;
1283 if (csum) {
1284 offload |= TUN_F_CSUM;
1285 if (tso4)
1286 offload |= TUN_F_TSO4;
1287 if (tso6)
1288 offload |= TUN_F_TSO6;
1289 if ((tso4 || tso6) && ecn)
1290 offload |= TUN_F_TSO_ECN;
1293 if (ioctl(s->fd, TUNSETOFFLOAD, offload) != 0)
1294 fprintf(stderr, "TUNSETOFFLOAD ioctl() failed: %s\n",
1295 strerror(errno));
1297 #endif /* TUNSETOFFLOAD */
1299 static void tap_cleanup(VLANClientState *vc)
1301 TAPState *s = vc->opaque;
1303 qemu_purge_queued_packets(vc);
1305 if (s->down_script[0])
1306 launch_script(s->down_script, s->down_script_arg, s->fd);
1308 tap_read_poll(s, 0);
1309 tap_write_poll(s, 0);
1310 close(s->fd);
1311 qemu_free(s);
1314 /* fd support */
1316 static TAPState *net_tap_fd_init(VLANState *vlan,
1317 const char *model,
1318 const char *name,
1319 int fd,
1320 int vnet_hdr)
1322 TAPState *s;
1324 s = qemu_mallocz(sizeof(TAPState));
1325 s->fd = fd;
1326 s->has_vnet_hdr = vnet_hdr != 0;
1327 s->vc = qemu_new_vlan_client(vlan, model, name, NULL, tap_receive,
1328 tap_receive_iov, tap_cleanup, s);
1329 s->vc->receive_raw = tap_receive_raw;
1330 #ifdef TUNSETOFFLOAD
1331 s->vc->set_offload = tap_set_offload;
1332 tap_set_offload(s->vc, 0, 0, 0, 0);
1333 #endif
1334 tap_read_poll(s, 1);
1335 snprintf(s->vc->info_str, sizeof(s->vc->info_str), "fd=%d", fd);
1336 return s;
1339 #if defined (HOST_BSD) || defined (__FreeBSD_kernel__)
1340 static int tap_open(char *ifname, int ifname_size)
1342 int fd;
1343 char *dev;
1344 struct stat s;
1346 TFR(fd = open("/dev/tap", O_RDWR));
1347 if (fd < 0) {
1348 fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n");
1349 return -1;
1352 fstat(fd, &s);
1353 dev = devname(s.st_rdev, S_IFCHR);
1354 pstrcpy(ifname, ifname_size, dev);
1356 fcntl(fd, F_SETFL, O_NONBLOCK);
1357 return fd;
1359 #elif defined(__sun__)
1360 #define TUNNEWPPA (('T'<<16) | 0x0001)
1362 * Allocate TAP device, returns opened fd.
1363 * Stores dev name in the first arg(must be large enough).
1365 static int tap_alloc(char *dev, size_t dev_size)
1367 int tap_fd, if_fd, ppa = -1;
1368 static int ip_fd = 0;
1369 char *ptr;
1371 static int arp_fd = 0;
1372 int ip_muxid, arp_muxid;
1373 struct strioctl strioc_if, strioc_ppa;
1374 int link_type = I_PLINK;;
1375 struct lifreq ifr;
1376 char actual_name[32] = "";
1378 memset(&ifr, 0x0, sizeof(ifr));
1380 if( *dev ){
1381 ptr = dev;
1382 while( *ptr && !qemu_isdigit((int)*ptr) ) ptr++;
1383 ppa = atoi(ptr);
1386 /* Check if IP device was opened */
1387 if( ip_fd )
1388 close(ip_fd);
1390 TFR(ip_fd = open("/dev/udp", O_RDWR, 0));
1391 if (ip_fd < 0) {
1392 syslog(LOG_ERR, "Can't open /dev/ip (actually /dev/udp)");
1393 return -1;
1396 TFR(tap_fd = open("/dev/tap", O_RDWR, 0));
1397 if (tap_fd < 0) {
1398 syslog(LOG_ERR, "Can't open /dev/tap");
1399 return -1;
1402 /* Assign a new PPA and get its unit number. */
1403 strioc_ppa.ic_cmd = TUNNEWPPA;
1404 strioc_ppa.ic_timout = 0;
1405 strioc_ppa.ic_len = sizeof(ppa);
1406 strioc_ppa.ic_dp = (char *)&ppa;
1407 if ((ppa = ioctl (tap_fd, I_STR, &strioc_ppa)) < 0)
1408 syslog (LOG_ERR, "Can't assign new interface");
1410 TFR(if_fd = open("/dev/tap", O_RDWR, 0));
1411 if (if_fd < 0) {
1412 syslog(LOG_ERR, "Can't open /dev/tap (2)");
1413 return -1;
1415 if(ioctl(if_fd, I_PUSH, "ip") < 0){
1416 syslog(LOG_ERR, "Can't push IP module");
1417 return -1;
1420 if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0)
1421 syslog(LOG_ERR, "Can't get flags\n");
1423 snprintf (actual_name, 32, "tap%d", ppa);
1424 pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name);
1426 ifr.lifr_ppa = ppa;
1427 /* Assign ppa according to the unit number returned by tun device */
1429 if (ioctl (if_fd, SIOCSLIFNAME, &ifr) < 0)
1430 syslog (LOG_ERR, "Can't set PPA %d", ppa);
1431 if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) <0)
1432 syslog (LOG_ERR, "Can't get flags\n");
1433 /* Push arp module to if_fd */
1434 if (ioctl (if_fd, I_PUSH, "arp") < 0)
1435 syslog (LOG_ERR, "Can't push ARP module (2)");
1437 /* Push arp module to ip_fd */
1438 if (ioctl (ip_fd, I_POP, NULL) < 0)
1439 syslog (LOG_ERR, "I_POP failed\n");
1440 if (ioctl (ip_fd, I_PUSH, "arp") < 0)
1441 syslog (LOG_ERR, "Can't push ARP module (3)\n");
1442 /* Open arp_fd */
1443 TFR(arp_fd = open ("/dev/tap", O_RDWR, 0));
1444 if (arp_fd < 0)
1445 syslog (LOG_ERR, "Can't open %s\n", "/dev/tap");
1447 /* Set ifname to arp */
1448 strioc_if.ic_cmd = SIOCSLIFNAME;
1449 strioc_if.ic_timout = 0;
1450 strioc_if.ic_len = sizeof(ifr);
1451 strioc_if.ic_dp = (char *)&ifr;
1452 if (ioctl(arp_fd, I_STR, &strioc_if) < 0){
1453 syslog (LOG_ERR, "Can't set ifname to arp\n");
1456 if((ip_muxid = ioctl(ip_fd, I_LINK, if_fd)) < 0){
1457 syslog(LOG_ERR, "Can't link TAP device to IP");
1458 return -1;
1461 if ((arp_muxid = ioctl (ip_fd, link_type, arp_fd)) < 0)
1462 syslog (LOG_ERR, "Can't link TAP device to ARP");
1464 close (if_fd);
1466 memset(&ifr, 0x0, sizeof(ifr));
1467 pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name);
1468 ifr.lifr_ip_muxid = ip_muxid;
1469 ifr.lifr_arp_muxid = arp_muxid;
1471 if (ioctl (ip_fd, SIOCSLIFMUXID, &ifr) < 0)
1473 ioctl (ip_fd, I_PUNLINK , arp_muxid);
1474 ioctl (ip_fd, I_PUNLINK, ip_muxid);
1475 syslog (LOG_ERR, "Can't set multiplexor id");
1478 snprintf(dev, dev_size, "tap%d", ppa);
1479 return tap_fd;
1482 static int tap_open(char *ifname, int ifname_size, int *vnet_hdr)
1484 char dev[10]="";
1485 int fd;
1486 if( (fd = tap_alloc(dev, sizeof(dev))) < 0 ){
1487 fprintf(stderr, "Cannot allocate TAP device\n");
1488 return -1;
1490 pstrcpy(ifname, ifname_size, dev);
1491 fcntl(fd, F_SETFL, O_NONBLOCK);
1492 return fd;
1494 #elif defined (_AIX)
1495 static int tap_open(char *ifname, int ifname_size)
1497 fprintf (stderr, "no tap on AIX\n");
1498 return -1;
1500 #else
1501 static int tap_open(char *ifname, int ifname_size, int *vnet_hdr)
1503 struct ifreq ifr;
1504 int fd, ret;
1506 TFR(fd = open("/dev/net/tun", O_RDWR));
1507 if (fd < 0) {
1508 fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
1509 return -1;
1511 memset(&ifr, 0, sizeof(ifr));
1512 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
1514 #if defined(TUNGETFEATURES) && defined(IFF_VNET_HDR)
1516 unsigned int features;
1518 if (ioctl(fd, TUNGETFEATURES, &features) == 0 &&
1519 features & IFF_VNET_HDR) {
1520 *vnet_hdr = 1;
1521 ifr.ifr_flags |= IFF_VNET_HDR;
1524 #endif
1526 if (ifname[0] != '\0')
1527 pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname);
1528 else
1529 pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d");
1530 ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
1531 if (ret != 0) {
1532 fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
1533 close(fd);
1534 return -1;
1536 pstrcpy(ifname, ifname_size, ifr.ifr_name);
1537 fcntl(fd, F_SETFL, O_NONBLOCK);
1538 return fd;
1540 #endif
1542 static int launch_script(const char *setup_script, const char *ifname, int fd)
1544 sigset_t oldmask, mask;
1545 int pid, status;
1546 char *args[3];
1547 char **parg;
1549 sigemptyset(&mask);
1550 sigaddset(&mask, SIGCHLD);
1551 sigprocmask(SIG_BLOCK, &mask, &oldmask);
1553 /* try to launch network script */
1554 pid = fork();
1555 if (pid == 0) {
1556 int open_max = sysconf(_SC_OPEN_MAX), i;
1558 for (i = 0; i < open_max; i++) {
1559 if (i != STDIN_FILENO &&
1560 i != STDOUT_FILENO &&
1561 i != STDERR_FILENO &&
1562 i != fd) {
1563 close(i);
1566 parg = args;
1567 *parg++ = (char *)setup_script;
1568 *parg++ = (char *)ifname;
1569 *parg++ = NULL;
1570 execv(setup_script, args);
1571 _exit(1);
1572 } else if (pid > 0) {
1573 while (waitpid(pid, &status, 0) != pid) {
1574 /* loop */
1576 sigprocmask(SIG_SETMASK, &oldmask, NULL);
1578 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
1579 return 0;
1582 fprintf(stderr, "%s: could not launch network script\n", setup_script);
1583 return -1;
1586 static int net_tap_init(VLANState *vlan, const char *model,
1587 const char *name, const char *ifname1,
1588 const char *setup_script, const char *down_script)
1590 TAPState *s;
1591 int fd;
1592 int vnet_hdr;
1593 char ifname[128];
1595 if (ifname1 != NULL)
1596 pstrcpy(ifname, sizeof(ifname), ifname1);
1597 else
1598 ifname[0] = '\0';
1599 vnet_hdr = 0;
1600 TFR(fd = tap_open(ifname, sizeof(ifname), &vnet_hdr));
1601 if (fd < 0)
1602 return -1;
1604 if (!setup_script || !strcmp(setup_script, "no"))
1605 setup_script = "";
1606 if (setup_script[0] != '\0') {
1607 if (launch_script(setup_script, ifname, fd))
1608 return -1;
1610 s = net_tap_fd_init(vlan, model, name, fd, vnet_hdr);
1611 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1612 "ifname=%s,script=%s,downscript=%s",
1613 ifname, setup_script, down_script);
1614 if (down_script && strcmp(down_script, "no")) {
1615 snprintf(s->down_script, sizeof(s->down_script), "%s", down_script);
1616 snprintf(s->down_script_arg, sizeof(s->down_script_arg), "%s", ifname);
1618 return 0;
1621 #endif /* !_WIN32 */
1623 #if defined(CONFIG_VDE)
1624 typedef struct VDEState {
1625 VLANClientState *vc;
1626 VDECONN *vde;
1627 } VDEState;
1629 static void vde_to_qemu(void *opaque)
1631 VDEState *s = opaque;
1632 uint8_t buf[4096];
1633 int size;
1635 size = vde_recv(s->vde, (char *)buf, sizeof(buf), 0);
1636 if (size > 0) {
1637 qemu_send_packet(s->vc, buf, size);
1641 static ssize_t vde_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
1643 VDEState *s = vc->opaque;
1644 ssize_t ret;
1646 do {
1647 ret = vde_send(s->vde, (const char *)buf, size, 0);
1648 } while (ret < 0 && errno == EINTR);
1650 return ret;
1653 static void vde_cleanup(VLANClientState *vc)
1655 VDEState *s = vc->opaque;
1656 qemu_set_fd_handler(vde_datafd(s->vde), NULL, NULL, NULL);
1657 vde_close(s->vde);
1658 qemu_free(s);
1661 static int net_vde_init(VLANState *vlan, const char *model,
1662 const char *name, const char *sock,
1663 int port, const char *group, int mode)
1665 VDEState *s;
1666 char *init_group = strlen(group) ? (char *)group : NULL;
1667 char *init_sock = strlen(sock) ? (char *)sock : NULL;
1669 struct vde_open_args args = {
1670 .port = port,
1671 .group = init_group,
1672 .mode = mode,
1675 s = qemu_mallocz(sizeof(VDEState));
1676 s->vde = vde_open(init_sock, (char *)"QEMU", &args);
1677 if (!s->vde){
1678 free(s);
1679 return -1;
1681 s->vc = qemu_new_vlan_client(vlan, model, name, NULL, vde_receive,
1682 NULL, vde_cleanup, s);
1683 qemu_set_fd_handler(vde_datafd(s->vde), vde_to_qemu, NULL, s);
1684 snprintf(s->vc->info_str, sizeof(s->vc->info_str), "sock=%s,fd=%d",
1685 sock, vde_datafd(s->vde));
1686 return 0;
1688 #endif
1690 /* network connection */
1691 typedef struct NetSocketState {
1692 VLANClientState *vc;
1693 int fd;
1694 int state; /* 0 = getting length, 1 = getting data */
1695 unsigned int index;
1696 unsigned int packet_len;
1697 uint8_t buf[4096];
1698 struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
1699 } NetSocketState;
1701 typedef struct NetSocketListenState {
1702 VLANState *vlan;
1703 char *model;
1704 char *name;
1705 int fd;
1706 } NetSocketListenState;
1708 /* XXX: we consider we can send the whole packet without blocking */
1709 static ssize_t net_socket_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
1711 NetSocketState *s = vc->opaque;
1712 uint32_t len;
1713 len = htonl(size);
1715 send_all(s->fd, (const uint8_t *)&len, sizeof(len));
1716 return send_all(s->fd, buf, size);
1719 static ssize_t net_socket_receive_dgram(VLANClientState *vc, const uint8_t *buf, size_t size)
1721 NetSocketState *s = vc->opaque;
1723 return sendto(s->fd, (const void *)buf, size, 0,
1724 (struct sockaddr *)&s->dgram_dst, sizeof(s->dgram_dst));
1727 static void net_socket_send(void *opaque)
1729 NetSocketState *s = opaque;
1730 int size, err;
1731 unsigned l;
1732 uint8_t buf1[4096];
1733 const uint8_t *buf;
1735 size = recv(s->fd, (void *)buf1, sizeof(buf1), 0);
1736 if (size < 0) {
1737 err = socket_error();
1738 if (err != EWOULDBLOCK)
1739 goto eoc;
1740 } else if (size == 0) {
1741 /* end of connection */
1742 eoc:
1743 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1744 closesocket(s->fd);
1745 return;
1747 buf = buf1;
1748 while (size > 0) {
1749 /* reassemble a packet from the network */
1750 switch(s->state) {
1751 case 0:
1752 l = 4 - s->index;
1753 if (l > size)
1754 l = size;
1755 memcpy(s->buf + s->index, buf, l);
1756 buf += l;
1757 size -= l;
1758 s->index += l;
1759 if (s->index == 4) {
1760 /* got length */
1761 s->packet_len = ntohl(*(uint32_t *)s->buf);
1762 s->index = 0;
1763 s->state = 1;
1765 break;
1766 case 1:
1767 l = s->packet_len - s->index;
1768 if (l > size)
1769 l = size;
1770 if (s->index + l <= sizeof(s->buf)) {
1771 memcpy(s->buf + s->index, buf, l);
1772 } else {
1773 fprintf(stderr, "serious error: oversized packet received,"
1774 "connection terminated.\n");
1775 s->state = 0;
1776 goto eoc;
1779 s->index += l;
1780 buf += l;
1781 size -= l;
1782 if (s->index >= s->packet_len) {
1783 qemu_send_packet(s->vc, s->buf, s->packet_len);
1784 s->index = 0;
1785 s->state = 0;
1787 break;
1792 static void net_socket_send_dgram(void *opaque)
1794 NetSocketState *s = opaque;
1795 int size;
1797 size = recv(s->fd, (void *)s->buf, sizeof(s->buf), 0);
1798 if (size < 0)
1799 return;
1800 if (size == 0) {
1801 /* end of connection */
1802 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1803 return;
1805 qemu_send_packet(s->vc, s->buf, size);
1808 static int net_socket_mcast_create(struct sockaddr_in *mcastaddr)
1810 struct ip_mreq imr;
1811 int fd;
1812 int val, ret;
1813 if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) {
1814 fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
1815 inet_ntoa(mcastaddr->sin_addr),
1816 (int)ntohl(mcastaddr->sin_addr.s_addr));
1817 return -1;
1820 fd = socket(PF_INET, SOCK_DGRAM, 0);
1821 if (fd < 0) {
1822 perror("socket(PF_INET, SOCK_DGRAM)");
1823 return -1;
1826 val = 1;
1827 ret=setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
1828 (const char *)&val, sizeof(val));
1829 if (ret < 0) {
1830 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
1831 goto fail;
1834 ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr));
1835 if (ret < 0) {
1836 perror("bind");
1837 goto fail;
1840 /* Add host to multicast group */
1841 imr.imr_multiaddr = mcastaddr->sin_addr;
1842 imr.imr_interface.s_addr = htonl(INADDR_ANY);
1844 ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1845 (const char *)&imr, sizeof(struct ip_mreq));
1846 if (ret < 0) {
1847 perror("setsockopt(IP_ADD_MEMBERSHIP)");
1848 goto fail;
1851 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
1852 val = 1;
1853 ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
1854 (const char *)&val, sizeof(val));
1855 if (ret < 0) {
1856 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
1857 goto fail;
1860 socket_set_nonblock(fd);
1861 return fd;
1862 fail:
1863 if (fd >= 0)
1864 closesocket(fd);
1865 return -1;
1868 static void net_socket_cleanup(VLANClientState *vc)
1870 NetSocketState *s = vc->opaque;
1871 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1872 close(s->fd);
1873 qemu_free(s);
1876 static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan,
1877 const char *model,
1878 const char *name,
1879 int fd, int is_connected)
1881 struct sockaddr_in saddr;
1882 int newfd;
1883 socklen_t saddr_len;
1884 NetSocketState *s;
1886 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
1887 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
1888 * by ONLY ONE process: we must "clone" this dgram socket --jjo
1891 if (is_connected) {
1892 if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) {
1893 /* must be bound */
1894 if (saddr.sin_addr.s_addr==0) {
1895 fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
1896 fd);
1897 return NULL;
1899 /* clone dgram socket */
1900 newfd = net_socket_mcast_create(&saddr);
1901 if (newfd < 0) {
1902 /* error already reported by net_socket_mcast_create() */
1903 close(fd);
1904 return NULL;
1906 /* clone newfd to fd, close newfd */
1907 dup2(newfd, fd);
1908 close(newfd);
1910 } else {
1911 fprintf(stderr, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
1912 fd, strerror(errno));
1913 return NULL;
1917 s = qemu_mallocz(sizeof(NetSocketState));
1918 s->fd = fd;
1920 s->vc = qemu_new_vlan_client(vlan, model, name, NULL, net_socket_receive_dgram,
1921 NULL, net_socket_cleanup, s);
1922 qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s);
1924 /* mcast: save bound address as dst */
1925 if (is_connected) s->dgram_dst=saddr;
1927 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1928 "socket: fd=%d (%s mcast=%s:%d)",
1929 fd, is_connected? "cloned" : "",
1930 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1931 return s;
1934 static void net_socket_connect(void *opaque)
1936 NetSocketState *s = opaque;
1937 qemu_set_fd_handler(s->fd, net_socket_send, NULL, s);
1940 static NetSocketState *net_socket_fd_init_stream(VLANState *vlan,
1941 const char *model,
1942 const char *name,
1943 int fd, int is_connected)
1945 NetSocketState *s;
1946 s = qemu_mallocz(sizeof(NetSocketState));
1947 s->fd = fd;
1948 s->vc = qemu_new_vlan_client(vlan, model, name, NULL, net_socket_receive,
1949 NULL, net_socket_cleanup, s);
1950 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1951 "socket: fd=%d", fd);
1952 if (is_connected) {
1953 net_socket_connect(s);
1954 } else {
1955 qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s);
1957 return s;
1960 static NetSocketState *net_socket_fd_init(VLANState *vlan,
1961 const char *model, const char *name,
1962 int fd, int is_connected)
1964 int so_type=-1, optlen=sizeof(so_type);
1966 if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type,
1967 (socklen_t *)&optlen)< 0) {
1968 fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd);
1969 return NULL;
1971 switch(so_type) {
1972 case SOCK_DGRAM:
1973 return net_socket_fd_init_dgram(vlan, model, name, fd, is_connected);
1974 case SOCK_STREAM:
1975 return net_socket_fd_init_stream(vlan, model, name, fd, is_connected);
1976 default:
1977 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
1978 fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd);
1979 return net_socket_fd_init_stream(vlan, model, name, fd, is_connected);
1981 return NULL;
1984 static void net_socket_accept(void *opaque)
1986 NetSocketListenState *s = opaque;
1987 NetSocketState *s1;
1988 struct sockaddr_in saddr;
1989 socklen_t len;
1990 int fd;
1992 for(;;) {
1993 len = sizeof(saddr);
1994 fd = accept(s->fd, (struct sockaddr *)&saddr, &len);
1995 if (fd < 0 && errno != EINTR) {
1996 return;
1997 } else if (fd >= 0) {
1998 break;
2001 s1 = net_socket_fd_init(s->vlan, s->model, s->name, fd, 1);
2002 if (!s1) {
2003 closesocket(fd);
2004 } else {
2005 snprintf(s1->vc->info_str, sizeof(s1->vc->info_str),
2006 "socket: connection from %s:%d",
2007 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
2011 static int net_socket_listen_init(VLANState *vlan,
2012 const char *model,
2013 const char *name,
2014 const char *host_str)
2016 NetSocketListenState *s;
2017 int fd, val, ret;
2018 struct sockaddr_in saddr;
2020 if (parse_host_port(&saddr, host_str) < 0)
2021 return -1;
2023 s = qemu_mallocz(sizeof(NetSocketListenState));
2025 fd = socket(PF_INET, SOCK_STREAM, 0);
2026 if (fd < 0) {
2027 perror("socket");
2028 return -1;
2030 socket_set_nonblock(fd);
2032 /* allow fast reuse */
2033 val = 1;
2034 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
2036 ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
2037 if (ret < 0) {
2038 perror("bind");
2039 return -1;
2041 ret = listen(fd, 0);
2042 if (ret < 0) {
2043 perror("listen");
2044 return -1;
2046 s->vlan = vlan;
2047 s->model = strdup(model);
2048 s->name = name ? strdup(name) : NULL;
2049 s->fd = fd;
2050 qemu_set_fd_handler(fd, net_socket_accept, NULL, s);
2051 return 0;
2054 static int net_socket_connect_init(VLANState *vlan,
2055 const char *model,
2056 const char *name,
2057 const char *host_str)
2059 NetSocketState *s;
2060 int fd, connected, ret, err;
2061 struct sockaddr_in saddr;
2063 if (parse_host_port(&saddr, host_str) < 0)
2064 return -1;
2066 fd = socket(PF_INET, SOCK_STREAM, 0);
2067 if (fd < 0) {
2068 perror("socket");
2069 return -1;
2071 socket_set_nonblock(fd);
2073 connected = 0;
2074 for(;;) {
2075 ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
2076 if (ret < 0) {
2077 err = socket_error();
2078 if (err == EINTR || err == EWOULDBLOCK) {
2079 } else if (err == EINPROGRESS) {
2080 break;
2081 #ifdef _WIN32
2082 } else if (err == WSAEALREADY) {
2083 break;
2084 #endif
2085 } else {
2086 perror("connect");
2087 closesocket(fd);
2088 return -1;
2090 } else {
2091 connected = 1;
2092 break;
2095 s = net_socket_fd_init(vlan, model, name, fd, connected);
2096 if (!s)
2097 return -1;
2098 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
2099 "socket: connect to %s:%d",
2100 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
2101 return 0;
2104 static int net_socket_mcast_init(VLANState *vlan,
2105 const char *model,
2106 const char *name,
2107 const char *host_str)
2109 NetSocketState *s;
2110 int fd;
2111 struct sockaddr_in saddr;
2113 if (parse_host_port(&saddr, host_str) < 0)
2114 return -1;
2117 fd = net_socket_mcast_create(&saddr);
2118 if (fd < 0)
2119 return -1;
2121 s = net_socket_fd_init(vlan, model, name, fd, 0);
2122 if (!s)
2123 return -1;
2125 s->dgram_dst = saddr;
2127 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
2128 "socket: mcast=%s:%d",
2129 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
2130 return 0;
2134 typedef struct DumpState {
2135 VLANClientState *pcap_vc;
2136 int fd;
2137 int pcap_caplen;
2138 } DumpState;
2140 #define PCAP_MAGIC 0xa1b2c3d4
2142 struct pcap_file_hdr {
2143 uint32_t magic;
2144 uint16_t version_major;
2145 uint16_t version_minor;
2146 int32_t thiszone;
2147 uint32_t sigfigs;
2148 uint32_t snaplen;
2149 uint32_t linktype;
2152 struct pcap_sf_pkthdr {
2153 struct {
2154 int32_t tv_sec;
2155 int32_t tv_usec;
2156 } ts;
2157 uint32_t caplen;
2158 uint32_t len;
2161 static ssize_t dump_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
2163 DumpState *s = vc->opaque;
2164 struct pcap_sf_pkthdr hdr;
2165 int64_t ts;
2166 int caplen;
2168 /* Early return in case of previous error. */
2169 if (s->fd < 0) {
2170 return size;
2173 ts = muldiv64(qemu_get_clock(vm_clock), 1000000, ticks_per_sec);
2174 caplen = size > s->pcap_caplen ? s->pcap_caplen : size;
2176 hdr.ts.tv_sec = ts / 1000000;
2177 hdr.ts.tv_usec = ts % 1000000;
2178 hdr.caplen = caplen;
2179 hdr.len = size;
2180 if (write(s->fd, &hdr, sizeof(hdr)) != sizeof(hdr) ||
2181 write(s->fd, buf, caplen) != caplen) {
2182 qemu_log("-net dump write error - stop dump\n");
2183 close(s->fd);
2184 s->fd = -1;
2187 return size;
2190 static void net_dump_cleanup(VLANClientState *vc)
2192 DumpState *s = vc->opaque;
2194 close(s->fd);
2195 qemu_free(s);
2198 static int net_dump_init(Monitor *mon, VLANState *vlan, const char *device,
2199 const char *name, const char *filename, int len)
2201 struct pcap_file_hdr hdr;
2202 DumpState *s;
2204 s = qemu_malloc(sizeof(DumpState));
2206 s->fd = open(filename, O_CREAT | O_WRONLY | O_BINARY, 0644);
2207 if (s->fd < 0) {
2208 config_error(mon, "-net dump: can't open %s\n", filename);
2209 return -1;
2212 s->pcap_caplen = len;
2214 hdr.magic = PCAP_MAGIC;
2215 hdr.version_major = 2;
2216 hdr.version_minor = 4;
2217 hdr.thiszone = 0;
2218 hdr.sigfigs = 0;
2219 hdr.snaplen = s->pcap_caplen;
2220 hdr.linktype = 1;
2222 if (write(s->fd, &hdr, sizeof(hdr)) < sizeof(hdr)) {
2223 config_error(mon, "-net dump write error: %s\n", strerror(errno));
2224 close(s->fd);
2225 qemu_free(s);
2226 return -1;
2229 s->pcap_vc = qemu_new_vlan_client(vlan, device, name, NULL, dump_receive, NULL,
2230 net_dump_cleanup, s);
2231 snprintf(s->pcap_vc->info_str, sizeof(s->pcap_vc->info_str),
2232 "dump to %s (len=%d)", filename, len);
2233 return 0;
2236 /* find or alloc a new VLAN */
2237 VLANState *qemu_find_vlan(int id)
2239 VLANState **pvlan, *vlan;
2240 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
2241 if (vlan->id == id)
2242 return vlan;
2244 vlan = qemu_mallocz(sizeof(VLANState));
2245 vlan->id = id;
2246 vlan->next = NULL;
2247 pvlan = &first_vlan;
2248 while (*pvlan != NULL)
2249 pvlan = &(*pvlan)->next;
2250 *pvlan = vlan;
2251 return vlan;
2254 static int nic_get_free_idx(void)
2256 int index;
2258 for (index = 0; index < MAX_NICS; index++)
2259 if (!nd_table[index].used)
2260 return index;
2261 return -1;
2264 void qemu_check_nic_model(NICInfo *nd, const char *model)
2266 const char *models[2];
2268 models[0] = model;
2269 models[1] = NULL;
2271 qemu_check_nic_model_list(nd, models, model);
2274 void qemu_check_nic_model_list(NICInfo *nd, const char * const *models,
2275 const char *default_model)
2277 int i, exit_status = 0;
2279 if (!nd->model)
2280 nd->model = strdup(default_model);
2282 if (strcmp(nd->model, "?") != 0) {
2283 for (i = 0 ; models[i]; i++)
2284 if (strcmp(nd->model, models[i]) == 0)
2285 return;
2287 fprintf(stderr, "qemu: Unsupported NIC model: %s\n", nd->model);
2288 exit_status = 1;
2291 fprintf(stderr, "qemu: Supported NIC models: ");
2292 for (i = 0 ; models[i]; i++)
2293 fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n');
2295 exit(exit_status);
2298 int net_client_init(Monitor *mon, const char *device, const char *p)
2300 static const char * const fd_params[] = {
2301 "vlan", "name", "fd", NULL
2303 char buf[1024];
2304 int vlan_id, ret;
2305 VLANState *vlan;
2306 char *name = NULL;
2308 vlan_id = 0;
2309 if (get_param_value(buf, sizeof(buf), "vlan", p)) {
2310 vlan_id = strtol(buf, NULL, 0);
2312 vlan = qemu_find_vlan(vlan_id);
2314 if (get_param_value(buf, sizeof(buf), "name", p)) {
2315 name = qemu_strdup(buf);
2317 if (!strcmp(device, "nic")) {
2318 static const char * const nic_params[] = {
2319 "vlan", "name", "macaddr", "model", "addr", NULL
2321 NICInfo *nd;
2322 uint8_t *macaddr;
2323 int idx = nic_get_free_idx();
2325 if (check_params(buf, sizeof(buf), nic_params, p) < 0) {
2326 config_error(mon, "invalid parameter '%s' in '%s'\n", buf, p);
2327 ret = -1;
2328 goto out;
2330 if (idx == -1 || nb_nics >= MAX_NICS) {
2331 config_error(mon, "Too Many NICs\n");
2332 ret = -1;
2333 goto out;
2335 nd = &nd_table[idx];
2336 macaddr = nd->macaddr;
2337 macaddr[0] = 0x52;
2338 macaddr[1] = 0x54;
2339 macaddr[2] = 0x00;
2340 macaddr[3] = 0x12;
2341 macaddr[4] = 0x34;
2342 macaddr[5] = 0x56 + idx;
2344 if (get_param_value(buf, sizeof(buf), "macaddr", p)) {
2345 if (parse_macaddr(macaddr, buf) < 0) {
2346 config_error(mon, "invalid syntax for ethernet address\n");
2347 ret = -1;
2348 goto out;
2351 if (get_param_value(buf, sizeof(buf), "model", p)) {
2352 nd->model = strdup(buf);
2354 if (get_param_value(buf, sizeof(buf), "addr", p)) {
2355 nd->devaddr = strdup(buf);
2357 nd->vlan = vlan;
2358 nd->name = name;
2359 nd->used = 1;
2360 name = NULL;
2361 nb_nics++;
2362 vlan->nb_guest_devs++;
2363 ret = idx;
2364 } else
2365 if (!strcmp(device, "none")) {
2366 if (*p != '\0') {
2367 config_error(mon, "'none' takes no parameters\n");
2368 ret = -1;
2369 goto out;
2371 /* does nothing. It is needed to signal that no network cards
2372 are wanted */
2373 ret = 0;
2374 } else
2375 #ifdef CONFIG_SLIRP
2376 if (!strcmp(device, "user")) {
2377 static const char * const slirp_params[] = {
2378 "vlan", "name", "hostname", "restrict", "ip", NULL
2380 int restricted = 0;
2381 char *ip = NULL;
2383 if (check_params(buf, sizeof(buf), slirp_params, p) < 0) {
2384 config_error(mon, "invalid parameter '%s' in '%s'\n", buf, p);
2385 ret = -1;
2386 goto out;
2388 if (get_param_value(buf, sizeof(buf), "hostname", p)) {
2389 pstrcpy(slirp_hostname, sizeof(slirp_hostname), buf);
2391 if (get_param_value(buf, sizeof(buf), "restrict", p)) {
2392 restricted = (buf[0] == 'y') ? 1 : 0;
2394 if (get_param_value(buf, sizeof(buf), "ip", p)) {
2395 ip = qemu_strdup(buf);
2397 vlan->nb_host_devs++;
2398 ret = net_slirp_init(vlan, device, name, restricted, ip);
2399 qemu_free(ip);
2400 } else if (!strcmp(device, "channel")) {
2401 long port;
2402 char name[20], *devname;
2403 struct VMChannel *vmc;
2405 port = strtol(p, &devname, 10);
2406 devname++;
2407 if (port < 1 || port > 65535) {
2408 config_error(mon, "vmchannel wrong port number\n");
2409 ret = -1;
2410 goto out;
2412 vmc = malloc(sizeof(struct VMChannel));
2413 snprintf(name, 20, "vmchannel%ld", port);
2414 vmc->hd = qemu_chr_open(name, devname, NULL);
2415 if (!vmc->hd) {
2416 config_error(mon, "could not open vmchannel device '%s'\n",
2417 devname);
2418 ret = -1;
2419 goto out;
2421 vmc->port = port;
2422 slirp_add_exec(3, vmc->hd, 4, port);
2423 qemu_chr_add_handlers(vmc->hd, vmchannel_can_read, vmchannel_read,
2424 NULL, vmc);
2425 ret = 0;
2426 } else
2427 #endif
2428 #ifdef _WIN32
2429 if (!strcmp(device, "tap")) {
2430 static const char * const tap_params[] = {
2431 "vlan", "name", "ifname", NULL
2433 char ifname[64];
2435 if (check_params(buf, sizeof(buf), tap_params, p) < 0) {
2436 config_error(mon, "invalid parameter '%s' in '%s'\n", buf, p);
2437 ret = -1;
2438 goto out;
2440 if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
2441 config_error(mon, "tap: no interface name\n");
2442 ret = -1;
2443 goto out;
2445 vlan->nb_host_devs++;
2446 ret = tap_win32_init(vlan, device, name, ifname);
2447 } else
2448 #elif defined (_AIX)
2449 #else
2450 if (!strcmp(device, "tap")) {
2451 char ifname[64], chkbuf[64];
2452 char setup_script[1024], down_script[1024];
2453 int fd;
2454 vlan->nb_host_devs++;
2455 if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
2456 if (check_params(chkbuf, sizeof(chkbuf), fd_params, p) < 0) {
2457 config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
2458 ret = -1;
2459 goto out;
2461 fd = strtol(buf, NULL, 0);
2462 fcntl(fd, F_SETFL, O_NONBLOCK);
2463 net_tap_fd_init(vlan, device, name, fd, tap_probe_vnet_hdr(fd));
2464 ret = 0;
2465 } else {
2466 static const char * const tap_params[] = {
2467 "vlan", "name", "ifname", "script", "downscript", NULL
2469 if (check_params(chkbuf, sizeof(chkbuf), tap_params, p) < 0) {
2470 config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
2471 ret = -1;
2472 goto out;
2474 if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
2475 ifname[0] = '\0';
2477 if (get_param_value(setup_script, sizeof(setup_script), "script", p) == 0) {
2478 pstrcpy(setup_script, sizeof(setup_script), DEFAULT_NETWORK_SCRIPT);
2480 if (get_param_value(down_script, sizeof(down_script), "downscript", p) == 0) {
2481 pstrcpy(down_script, sizeof(down_script), DEFAULT_NETWORK_DOWN_SCRIPT);
2483 ret = net_tap_init(vlan, device, name, ifname, setup_script, down_script);
2485 } else
2486 #endif
2487 if (!strcmp(device, "socket")) {
2488 char chkbuf[64];
2489 if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
2490 int fd;
2491 if (check_params(chkbuf, sizeof(chkbuf), fd_params, p) < 0) {
2492 config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
2493 ret = -1;
2494 goto out;
2496 fd = strtol(buf, NULL, 0);
2497 ret = -1;
2498 if (net_socket_fd_init(vlan, device, name, fd, 1))
2499 ret = 0;
2500 } else if (get_param_value(buf, sizeof(buf), "listen", p) > 0) {
2501 static const char * const listen_params[] = {
2502 "vlan", "name", "listen", NULL
2504 if (check_params(chkbuf, sizeof(chkbuf), listen_params, p) < 0) {
2505 config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
2506 ret = -1;
2507 goto out;
2509 ret = net_socket_listen_init(vlan, device, name, buf);
2510 } else if (get_param_value(buf, sizeof(buf), "connect", p) > 0) {
2511 static const char * const connect_params[] = {
2512 "vlan", "name", "connect", NULL
2514 if (check_params(chkbuf, sizeof(chkbuf), connect_params, p) < 0) {
2515 config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
2516 ret = -1;
2517 goto out;
2519 ret = net_socket_connect_init(vlan, device, name, buf);
2520 } else if (get_param_value(buf, sizeof(buf), "mcast", p) > 0) {
2521 static const char * const mcast_params[] = {
2522 "vlan", "name", "mcast", NULL
2524 if (check_params(chkbuf, sizeof(chkbuf), mcast_params, p) < 0) {
2525 config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
2526 ret = -1;
2527 goto out;
2529 ret = net_socket_mcast_init(vlan, device, name, buf);
2530 } else {
2531 config_error(mon, "Unknown socket options: %s\n", p);
2532 ret = -1;
2533 goto out;
2535 vlan->nb_host_devs++;
2536 } else
2537 #ifdef CONFIG_VDE
2538 if (!strcmp(device, "vde")) {
2539 static const char * const vde_params[] = {
2540 "vlan", "name", "sock", "port", "group", "mode", NULL
2542 char vde_sock[1024], vde_group[512];
2543 int vde_port, vde_mode;
2545 if (check_params(buf, sizeof(buf), vde_params, p) < 0) {
2546 config_error(mon, "invalid parameter '%s' in '%s'\n", buf, p);
2547 ret = -1;
2548 goto out;
2550 vlan->nb_host_devs++;
2551 if (get_param_value(vde_sock, sizeof(vde_sock), "sock", p) <= 0) {
2552 vde_sock[0] = '\0';
2554 if (get_param_value(buf, sizeof(buf), "port", p) > 0) {
2555 vde_port = strtol(buf, NULL, 10);
2556 } else {
2557 vde_port = 0;
2559 if (get_param_value(vde_group, sizeof(vde_group), "group", p) <= 0) {
2560 vde_group[0] = '\0';
2562 if (get_param_value(buf, sizeof(buf), "mode", p) > 0) {
2563 vde_mode = strtol(buf, NULL, 8);
2564 } else {
2565 vde_mode = 0700;
2567 ret = net_vde_init(vlan, device, name, vde_sock, vde_port, vde_group, vde_mode);
2568 } else
2569 #endif
2570 if (!strcmp(device, "dump")) {
2571 int len = 65536;
2573 if (get_param_value(buf, sizeof(buf), "len", p) > 0) {
2574 len = strtol(buf, NULL, 0);
2576 if (!get_param_value(buf, sizeof(buf), "file", p)) {
2577 snprintf(buf, sizeof(buf), "qemu-vlan%d.pcap", vlan_id);
2579 ret = net_dump_init(mon, vlan, device, name, buf, len);
2580 } else {
2581 config_error(mon, "Unknown network device: %s\n", device);
2582 ret = -1;
2583 goto out;
2585 if (ret < 0) {
2586 config_error(mon, "Could not initialize device '%s'\n", device);
2588 out:
2589 qemu_free(name);
2590 return ret;
2593 void net_client_uninit(NICInfo *nd)
2595 nd->vlan->nb_guest_devs--;
2596 nb_nics--;
2597 nd->used = 0;
2598 free((void *)nd->model);
2601 static int net_host_check_device(const char *device)
2603 int i;
2604 const char *valid_param_list[] = { "tap", "socket", "dump"
2605 #ifdef CONFIG_SLIRP
2606 ,"user"
2607 #endif
2608 #ifdef CONFIG_VDE
2609 ,"vde"
2610 #endif
2612 for (i = 0; i < sizeof(valid_param_list) / sizeof(char *); i++) {
2613 if (!strncmp(valid_param_list[i], device,
2614 strlen(valid_param_list[i])))
2615 return 1;
2618 return 0;
2621 void net_host_device_add(Monitor *mon, const char *device, const char *opts)
2623 if (!net_host_check_device(device)) {
2624 monitor_printf(mon, "invalid host network device %s\n", device);
2625 return;
2627 if (net_client_init(mon, device, opts ? opts : "") < 0) {
2628 monitor_printf(mon, "adding host network device %s failed\n", device);
2632 void net_host_device_remove(Monitor *mon, int vlan_id, const char *device)
2634 VLANState *vlan;
2635 VLANClientState *vc;
2637 vlan = qemu_find_vlan(vlan_id);
2639 for (vc = vlan->first_client; vc != NULL; vc = vc->next) {
2640 if (!strcmp(vc->name, device)) {
2641 break;
2645 if (!vc) {
2646 monitor_printf(mon, "can't find device %s\n", device);
2647 return;
2649 if (!net_host_check_device(vc->model)) {
2650 monitor_printf(mon, "invalid host network device %s\n", device);
2651 return;
2653 qemu_del_vlan_client(vc);
2656 int net_client_parse(const char *str)
2658 const char *p;
2659 char *q;
2660 char device[64];
2662 p = str;
2663 q = device;
2664 while (*p != '\0' && *p != ',') {
2665 if ((q - device) < sizeof(device) - 1)
2666 *q++ = *p;
2667 p++;
2669 *q = '\0';
2670 if (*p == ',')
2671 p++;
2673 return net_client_init(NULL, device, p);
2676 void net_set_boot_mask(int net_boot_mask)
2678 int i;
2680 /* Only the first four NICs may be bootable */
2681 net_boot_mask = net_boot_mask & 0xF;
2683 for (i = 0; i < nb_nics; i++) {
2684 if (net_boot_mask & (1 << i)) {
2685 nd_table[i].bootable = 1;
2686 net_boot_mask &= ~(1 << i);
2690 if (net_boot_mask) {
2691 fprintf(stderr, "Cannot boot from non-existent NIC\n");
2692 exit(1);
2696 void do_info_network(Monitor *mon)
2698 VLANState *vlan;
2699 VLANClientState *vc;
2701 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
2702 monitor_printf(mon, "VLAN %d devices:\n", vlan->id);
2703 for(vc = vlan->first_client; vc != NULL; vc = vc->next)
2704 monitor_printf(mon, " %s: %s\n", vc->name, vc->info_str);
2708 int do_set_link(Monitor *mon, const char *name, const char *up_or_down)
2710 VLANState *vlan;
2711 VLANClientState *vc = NULL;
2713 for (vlan = first_vlan; vlan != NULL; vlan = vlan->next)
2714 for (vc = vlan->first_client; vc != NULL; vc = vc->next)
2715 if (strcmp(vc->name, name) == 0)
2716 goto done;
2717 done:
2719 if (!vc) {
2720 monitor_printf(mon, "could not find network device '%s'", name);
2721 return 0;
2724 if (strcmp(up_or_down, "up") == 0)
2725 vc->link_down = 0;
2726 else if (strcmp(up_or_down, "down") == 0)
2727 vc->link_down = 1;
2728 else
2729 monitor_printf(mon, "invalid link status '%s'; only 'up' or 'down' "
2730 "valid\n", up_or_down);
2732 if (vc->link_status_changed)
2733 vc->link_status_changed(vc);
2735 return 1;
2738 void net_cleanup(void)
2740 VLANState *vlan;
2742 /* close network clients */
2743 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
2744 VLANClientState *vc = vlan->first_client;
2746 while (vc) {
2747 VLANClientState *next = vc->next;
2749 qemu_del_vlan_client(vc);
2751 vc = next;
2756 void net_client_check(void)
2758 VLANState *vlan;
2760 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
2761 if (vlan->nb_guest_devs == 0 && vlan->nb_host_devs == 0)
2762 continue;
2763 if (vlan->nb_guest_devs == 0)
2764 fprintf(stderr, "Warning: vlan %d with no nics\n", vlan->id);
2765 if (vlan->nb_host_devs == 0)
2766 fprintf(stderr,
2767 "Warning: vlan %d is not connected to host network\n",
2768 vlan->id);