X-Git-Url: http://git.maemo.org/git/?a=blobdiff_plain;f=net.c;h=31ee95a4ff3daea30adb99a55ae8b80c0d7ef702;hb=72b675caacfc6f2fde17e6b51a668c0d5409a8f7;hp=35aeb1ece235ff5e5537f80c0cd5a1f9a382dc3b;hpb=24646c7ed8a94a2b39e03121f0079bb89e708ec7;p=qemu diff --git a/net.c b/net.c index 35aeb1e..31ee95a 100644 --- a/net.c +++ b/net.c @@ -21,25 +21,6 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ -#include "hw/hw.h" -#include "hw/boards.h" -#include "hw/usb.h" -#include "hw/pcmcia.h" -#include "hw/pc.h" -#include "hw/audiodev.h" -#include "hw/isa.h" -#include "hw/baum.h" -#include "hw/bt.h" -#include "net.h" -#include "console.h" -#include "sysemu.h" -#include "gdbstub.h" -#include "qemu-timer.h" -#include "qemu-char.h" -#include "block.h" -#include "audio/audio.h" -#include "migration.h" - #include #include #include @@ -48,6 +29,9 @@ #include #include +/* Needed early for HOST_BSD etc. */ +#include "config-host.h" + #ifndef _WIN32 #include #include @@ -68,9 +52,9 @@ #include #include #include -#ifdef _BSD +#ifdef HOST_BSD #include -#ifdef __FreeBSD__ +#if defined(__FreeBSD__) || defined(__DragonFly__) #include #else #include @@ -109,12 +93,6 @@ #endif #endif -#include "qemu_socket.h" - -#if defined(CONFIG_SLIRP) -#include "libslirp.h" -#endif - #if defined(__OpenBSD__) #include #endif @@ -124,6 +102,7 @@ #endif #ifdef _WIN32 +#include #include #include #include @@ -131,14 +110,21 @@ #define memalign(align, size) malloc(size) #endif -#define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup" -#define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown" -#ifdef __sun__ -#define SMBD_COMMAND "/usr/sfw/sbin/smbd" -#else -#define SMBD_COMMAND "/usr/sbin/smbd" +#include "qemu-common.h" +#include "net.h" +#include "monitor.h" +#include "sysemu.h" +#include "qemu-timer.h" +#include "qemu-char.h" +#include "audio/audio.h" +#include "qemu_socket.h" +#include "qemu-log.h" + +#if defined(CONFIG_SLIRP) +#include "libslirp.h" #endif + static VLANState *first_vlan; /***********************************************************/ @@ -279,7 +265,7 @@ int parse_host_port(struct sockaddr_in *saddr, const char *str) if (buf[0] == '\0') { saddr->sin_addr.s_addr = 0; } else { - if (isdigit(buf[0])) { + if (qemu_isdigit(buf[0])) { if (!inet_aton(buf, &saddr->sin_addr)) return -1; } else { @@ -295,8 +281,8 @@ int parse_host_port(struct sockaddr_in *saddr, const char *str) return 0; } -#ifndef _WIN32 -int parse_unix_path(struct sockaddr_un *uaddr, const char *str) +#if !defined(_WIN32) && 0 +static int parse_unix_path(struct sockaddr_un *uaddr, const char *str) { const char *p; int len; @@ -315,17 +301,52 @@ int parse_unix_path(struct sockaddr_un *uaddr, const char *str) } #endif +void qemu_format_nic_info_str(VLANClientState *vc, uint8_t macaddr[6]) +{ + snprintf(vc->info_str, sizeof(vc->info_str), + "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x", + vc->model, + macaddr[0], macaddr[1], macaddr[2], + macaddr[3], macaddr[4], macaddr[5]); +} + +static char *assign_name(VLANClientState *vc1, const char *model) +{ + VLANState *vlan; + char buf[256]; + int id = 0; + + for (vlan = first_vlan; vlan; vlan = vlan->next) { + VLANClientState *vc; + + for (vc = vlan->first_client; vc; vc = vc->next) + if (vc != vc1 && strcmp(vc->model, model) == 0) + id++; + } + + snprintf(buf, sizeof(buf), "%s.%d", model, id); + + return strdup(buf); +} + VLANClientState *qemu_new_vlan_client(VLANState *vlan, + const char *model, + const char *name, IOReadHandler *fd_read, IOCanRWHandler *fd_can_read, + NetCleanup *cleanup, void *opaque) { VLANClientState *vc, **pvc; vc = qemu_mallocz(sizeof(VLANClientState)); - if (!vc) - return NULL; + vc->model = strdup(model); + if (name) + vc->name = strdup(name); + else + vc->name = assign_name(vc, model); vc->fd_read = fd_read; vc->fd_can_read = fd_can_read; + vc->cleanup = cleanup; vc->opaque = opaque; vc->vlan = vlan; @@ -344,12 +365,30 @@ void qemu_del_vlan_client(VLANClientState *vc) while (*pvc != NULL) if (*pvc == vc) { *pvc = vc->next; - free(vc); + if (vc->cleanup) { + vc->cleanup(vc); + } + free(vc->name); + free(vc->model); + qemu_free(vc); break; } else pvc = &(*pvc)->next; } +VLANClientState *qemu_find_vlan_client(VLANState *vlan, void *opaque) +{ + VLANClientState **pvc = &vlan->first_client; + + while (*pvc != NULL) + if ((*pvc)->opaque == opaque) + return *pvc; + else + pvc = &(*pvc)->next; + + return NULL; +} + int qemu_can_send_packet(VLANClientState *vc1) { VLANState *vlan = vc1->vlan; @@ -364,27 +403,115 @@ int qemu_can_send_packet(VLANClientState *vc1) return 0; } -void qemu_send_packet(VLANClientState *vc1, const uint8_t *buf, int size) +static void +qemu_deliver_packet(VLANClientState *sender, const uint8_t *buf, int size) { - VLANState *vlan = vc1->vlan; VLANClientState *vc; + for (vc = sender->vlan->first_client; vc != NULL; vc = vc->next) { + if (vc != sender && !vc->link_down) { + vc->fd_read(vc->opaque, buf, size); + } + } +} + +void qemu_send_packet(VLANClientState *vc, const uint8_t *buf, int size) +{ + VLANState *vlan = vc->vlan; + VLANPacket *packet; + + if (vc->link_down) + return; + #ifdef DEBUG_NET printf("vlan %d send:\n", vlan->id); hex_dump(stdout, buf, size); #endif - for(vc = vlan->first_client; vc != NULL; vc = vc->next) { - if (vc != vc1) { - vc->fd_read(vc->opaque, buf, size); + if (vlan->delivering) { + packet = qemu_malloc(sizeof(VLANPacket) + size); + packet->next = vlan->send_queue; + packet->sender = vc; + packet->size = size; + memcpy(packet->data, buf, size); + vlan->send_queue = packet; + } else { + vlan->delivering = 1; + qemu_deliver_packet(vc, buf, size); + while ((packet = vlan->send_queue) != NULL) { + qemu_deliver_packet(packet->sender, packet->data, packet->size); + vlan->send_queue = packet->next; + qemu_free(packet); } + vlan->delivering = 0; } } +static ssize_t vc_sendv_compat(VLANClientState *vc, const struct iovec *iov, + int iovcnt) +{ + uint8_t buffer[4096]; + size_t offset = 0; + int i; + + for (i = 0; i < iovcnt; i++) { + size_t len; + + len = MIN(sizeof(buffer) - offset, iov[i].iov_len); + memcpy(buffer + offset, iov[i].iov_base, len); + offset += len; + } + + vc->fd_read(vc->opaque, buffer, offset); + + return offset; +} + +static ssize_t calc_iov_length(const struct iovec *iov, int iovcnt) +{ + size_t offset = 0; + int i; + + for (i = 0; i < iovcnt; i++) + offset += iov[i].iov_len; + return offset; +} + +ssize_t qemu_sendv_packet(VLANClientState *vc1, const struct iovec *iov, + int iovcnt) +{ + VLANState *vlan = vc1->vlan; + VLANClientState *vc; + ssize_t max_len = 0; + + if (vc1->link_down) + return calc_iov_length(iov, iovcnt); + + for (vc = vlan->first_client; vc != NULL; vc = vc->next) { + ssize_t len = 0; + + if (vc == vc1) + continue; + + if (vc->link_down) + len = calc_iov_length(iov, iovcnt); + if (vc->fd_readv) + len = vc->fd_readv(vc->opaque, iov, iovcnt); + else if (vc->fd_read) + len = vc_sendv_compat(vc, iov, iovcnt); + + max_len = MAX(max_len, len); + } + + return max_len; +} + #if defined(CONFIG_SLIRP) /* slirp network adapter */ static int slirp_inited; +static int slirp_restrict; +static char *slirp_ip; static VLANClientState *slirp_vc; int slirp_can_output(void) @@ -417,68 +544,87 @@ static void slirp_receive(void *opaque, const uint8_t *buf, int size) slirp_input(buf, size); } -static int net_slirp_init(VLANState *vlan) +static int slirp_in_use; + +static void net_slirp_cleanup(VLANClientState *vc) +{ + slirp_in_use = 0; +} + +static int net_slirp_init(VLANState *vlan, const char *model, const char *name) { + if (slirp_in_use) { + /* slirp only supports a single instance so far */ + return -1; + } if (!slirp_inited) { slirp_inited = 1; - slirp_init(); + slirp_init(slirp_restrict, slirp_ip); } - slirp_vc = qemu_new_vlan_client(vlan, - slirp_receive, NULL, NULL); - snprintf(slirp_vc->info_str, sizeof(slirp_vc->info_str), "user redirector"); + slirp_vc = qemu_new_vlan_client(vlan, model, name, + slirp_receive, NULL, net_slirp_cleanup, NULL); + slirp_vc->info_str[0] = '\0'; + slirp_in_use = 1; return 0; } -void net_slirp_redir(const char *redir_str) +void net_slirp_redir(Monitor *mon, const char *redir_str) { int is_udp; char buf[256], *r; - const char *p; + const char *p, *errmsg; struct in_addr guest_addr; int host_port, guest_port; if (!slirp_inited) { slirp_inited = 1; - slirp_init(); + slirp_init(slirp_restrict, slirp_ip); } p = redir_str; if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) - goto fail; - if (!strcmp(buf, "tcp")) { + goto fail_syntax; + if (!strcmp(buf, "tcp") || buf[0] == '\0') { is_udp = 0; } else if (!strcmp(buf, "udp")) { is_udp = 1; } else { - goto fail; + goto fail_syntax; } if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) - goto fail; + goto fail_syntax; host_port = strtol(buf, &r, 0); if (r == buf) - goto fail; + goto fail_syntax; if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) - goto fail; + goto fail_syntax; if (buf[0] == '\0') { pstrcpy(buf, sizeof(buf), "10.0.2.15"); } if (!inet_aton(buf, &guest_addr)) - goto fail; + goto fail_syntax; guest_port = strtol(p, &r, 0); if (r == p) - goto fail; + goto fail_syntax; if (slirp_redir(is_udp, host_port, guest_addr, guest_port) < 0) { - fprintf(stderr, "qemu: could not set up redirection\n"); - exit(1); + errmsg = "could not set up redirection\n"; + goto fail; } return; + + fail_syntax: + errmsg = "invalid redirection format\n"; fail: - fprintf(stderr, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n"); - exit(1); + if (mon) { + monitor_printf(mon, "%s", errmsg); + } else { + fprintf(stderr, "qemu: %s", errmsg); + exit(1); + } } #ifndef _WIN32 @@ -492,7 +638,7 @@ static void erase_dir(char *dir_name) char filename[1024]; /* erase all the files in the directory */ - if ((d = opendir(dir_name)) != 0) { + if ((d = opendir(dir_name)) != NULL) { for(;;) { de = readdir(d); if (!de) @@ -525,11 +671,11 @@ void net_slirp_smb(const char *exported_dir) if (!slirp_inited) { slirp_inited = 1; - slirp_init(); + slirp_init(slirp_restrict, slirp_ip); } /* XXX: better tmp dir construction */ - snprintf(smb_dir, sizeof(smb_dir), "/tmp/qemu-smb.%d", getpid()); + snprintf(smb_dir, sizeof(smb_dir), "/tmp/qemu-smb.%ld", (long)getpid()); if (mkdir(smb_dir, 0700) < 0) { fprintf(stderr, "qemu: could not create samba server dir '%s'\n", smb_dir); exit(1); @@ -572,11 +718,28 @@ void net_slirp_smb(const char *exported_dir) } #endif /* !defined(_WIN32) */ -void do_info_slirp(void) +void do_info_slirp(Monitor *mon) { slirp_stats(); } +struct VMChannel { + CharDriverState *hd; + int port; +}; + +static int vmchannel_can_read(void *opaque) +{ + struct VMChannel *vmc = (struct VMChannel*)opaque; + return slirp_socket_can_recv(4, vmc->port); +} + +static void vmchannel_read(void *opaque, const uint8_t *buf, int size) +{ + struct VMChannel *vmc = (struct VMChannel*)opaque; + slirp_socket_recv(4, vmc->port, buf, size); +} + #endif /* CONFIG_SLIRP */ #if !defined(_WIN32) @@ -585,8 +748,24 @@ typedef struct TAPState { VLANClientState *vc; int fd; char down_script[1024]; + char down_script_arg[128]; } TAPState; +static int launch_script(const char *setup_script, const char *ifname, int fd); + +static ssize_t tap_receive_iov(void *opaque, const struct iovec *iov, + int iovcnt) +{ + TAPState *s = opaque; + ssize_t len; + + do { + len = writev(s->fd, iov, iovcnt); + } while (len == -1 && (errno == EINTR || errno == EAGAIN)); + + return len; +} + static void tap_receive(void *opaque, const uint8_t *buf, int size) { TAPState *s = opaque; @@ -610,7 +789,7 @@ static void tap_send(void *opaque) struct strbuf sbuf; int f = 0; sbuf.maxlen = sizeof(buf); - sbuf.buf = buf; + sbuf.buf = (char *)buf; size = getmsg(s->fd, NULL, &sbuf, &f) >=0 ? sbuf.len : -1; #else size = read(s->fd, buf, sizeof(buf)); @@ -620,23 +799,38 @@ static void tap_send(void *opaque) } } +static void tap_cleanup(VLANClientState *vc) +{ + TAPState *s = vc->opaque; + + if (s->down_script[0]) + launch_script(s->down_script, s->down_script_arg, s->fd); + + qemu_set_fd_handler(s->fd, NULL, NULL, NULL); + close(s->fd); + qemu_free(s); +} + /* fd support */ -static TAPState *net_tap_fd_init(VLANState *vlan, int fd) +static TAPState *net_tap_fd_init(VLANState *vlan, + const char *model, + const char *name, + int fd) { TAPState *s; s = qemu_mallocz(sizeof(TAPState)); - if (!s) - return NULL; s->fd = fd; - s->vc = qemu_new_vlan_client(vlan, tap_receive, NULL, s); + s->vc = qemu_new_vlan_client(vlan, model, name, tap_receive, + NULL, tap_cleanup, s); + s->vc->fd_readv = tap_receive_iov; qemu_set_fd_handler(s->fd, tap_send, NULL, s); - snprintf(s->vc->info_str, sizeof(s->vc->info_str), "tap: fd=%d", fd); + snprintf(s->vc->info_str, sizeof(s->vc->info_str), "fd=%d", fd); return s; } -#if defined (_BSD) || defined (__FreeBSD_kernel__) +#if defined (HOST_BSD) || defined (__FreeBSD_kernel__) static int tap_open(char *ifname, int ifname_size) { int fd; @@ -662,7 +856,7 @@ static int tap_open(char *ifname, int ifname_size) * Allocate TAP device, returns opened fd. * Stores dev name in the first arg(must be large enough). */ -int tap_alloc(char *dev, size_t dev_size) +static int tap_alloc(char *dev, size_t dev_size) { int tap_fd, if_fd, ppa = -1; static int ip_fd = 0; @@ -679,7 +873,7 @@ int tap_alloc(char *dev, size_t dev_size) if( *dev ){ ptr = dev; - while( *ptr && !isdigit((int)*ptr) ) ptr++; + while( *ptr && !qemu_isdigit((int)*ptr) ) ptr++; ppa = atoi(ptr); } @@ -791,6 +985,12 @@ static int tap_open(char *ifname, int ifname_size) fcntl(fd, F_SETFL, O_NONBLOCK); return fd; } +#elif defined (_AIX) +static int tap_open(char *ifname, int ifname_size) +{ + fprintf (stderr, "no tap on AIX\n"); + return -1; +} #else static int tap_open(char *ifname, int ifname_size) { @@ -856,7 +1056,8 @@ static int launch_script(const char *setup_script, const char *ifname, int fd) return 0; } -static int net_tap_init(VLANState *vlan, const char *ifname1, +static int net_tap_init(VLANState *vlan, const char *model, + const char *name, const char *ifname1, const char *setup_script, const char *down_script) { TAPState *s; @@ -877,13 +1078,14 @@ static int net_tap_init(VLANState *vlan, const char *ifname1, if (launch_script(setup_script, ifname, fd)) return -1; } - s = net_tap_fd_init(vlan, fd); - if (!s) - return -1; + s = net_tap_fd_init(vlan, model, name, fd); snprintf(s->vc->info_str, sizeof(s->vc->info_str), - "tap: ifname=%s setup_script=%s", ifname, setup_script); - if (down_script && strcmp(down_script, "no")) + "ifname=%s,script=%s,downscript=%s", + ifname, setup_script, down_script); + if (down_script && strcmp(down_script, "no")) { snprintf(s->down_script, sizeof(s->down_script), "%s", down_script); + snprintf(s->down_script_arg, sizeof(s->down_script_arg), "%s", ifname); + } return 0; } @@ -901,7 +1103,7 @@ static void vde_to_qemu(void *opaque) uint8_t buf[4096]; int size; - size = vde_recv(s->vde, buf, sizeof(buf), 0); + size = vde_recv(s->vde, (char *)buf, sizeof(buf), 0); if (size > 0) { qemu_send_packet(s->vc, buf, size); } @@ -912,7 +1114,7 @@ static void vde_from_qemu(void *opaque, const uint8_t *buf, int size) VDEState *s = opaque; int ret; for(;;) { - ret = vde_send(s->vde, buf, size, 0); + ret = vde_send(s->vde, (const char *)buf, size, 0); if (ret < 0 && errno == EINTR) { } else { break; @@ -920,8 +1122,17 @@ static void vde_from_qemu(void *opaque, const uint8_t *buf, int size) } } -static int net_vde_init(VLANState *vlan, const char *sock, int port, - const char *group, int mode) +static void vde_cleanup(VLANClientState *vc) +{ + VDEState *s = vc->opaque; + qemu_set_fd_handler(vde_datafd(s->vde), NULL, NULL, NULL); + vde_close(s->vde); + qemu_free(s); +} + +static int net_vde_init(VLANState *vlan, const char *model, + const char *name, const char *sock, + int port, const char *group, int mode) { VDEState *s; char *init_group = strlen(group) ? (char *)group : NULL; @@ -934,16 +1145,15 @@ static int net_vde_init(VLANState *vlan, const char *sock, int port, }; s = qemu_mallocz(sizeof(VDEState)); - if (!s) - return -1; - s->vde = vde_open(init_sock, "QEMU", &args); + s->vde = vde_open(init_sock, (char *)"QEMU", &args); if (!s->vde){ free(s); return -1; } - s->vc = qemu_new_vlan_client(vlan, vde_from_qemu, NULL, s); + s->vc = qemu_new_vlan_client(vlan, model, name, vde_from_qemu, + NULL, vde_cleanup, s); qemu_set_fd_handler(vde_datafd(s->vde), vde_to_qemu, NULL, s); - snprintf(s->vc->info_str, sizeof(s->vc->info_str), "vde: sock=%s fd=%d", + snprintf(s->vc->info_str, sizeof(s->vc->info_str), "sock=%s,fd=%d", sock, vde_datafd(s->vde)); return 0; } @@ -954,14 +1164,16 @@ typedef struct NetSocketState { VLANClientState *vc; int fd; int state; /* 0 = getting length, 1 = getting data */ - int index; - int packet_len; + unsigned int index; + unsigned int packet_len; uint8_t buf[4096]; struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */ } NetSocketState; typedef struct NetSocketListenState { VLANState *vlan; + char *model; + char *name; int fd; } NetSocketListenState; @@ -986,7 +1198,8 @@ static void net_socket_receive_dgram(void *opaque, const uint8_t *buf, int size) static void net_socket_send(void *opaque) { NetSocketState *s = opaque; - int l, size, err; + int size, err; + unsigned l; uint8_t buf1[4096]; const uint8_t *buf; @@ -1025,7 +1238,15 @@ static void net_socket_send(void *opaque) l = s->packet_len - s->index; if (l > size) l = size; - memcpy(s->buf + s->index, buf, l); + if (s->index + l <= sizeof(s->buf)) { + memcpy(s->buf + s->index, buf, l); + } else { + fprintf(stderr, "serious error: oversized packet received," + "connection terminated.\n"); + s->state = 0; + goto eoc; + } + s->index += l; buf += l; size -= l; @@ -1115,8 +1336,18 @@ fail: return -1; } -static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan, int fd, - int is_connected) +static void net_socket_cleanup(VLANClientState *vc) +{ + NetSocketState *s = vc->opaque; + qemu_set_fd_handler(s->fd, NULL, NULL, NULL); + close(s->fd); + qemu_free(s); +} + +static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan, + const char *model, + const char *name, + int fd, int is_connected) { struct sockaddr_in saddr; int newfd; @@ -1155,11 +1386,10 @@ static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan, int fd, } s = qemu_mallocz(sizeof(NetSocketState)); - if (!s) - return NULL; s->fd = fd; - s->vc = qemu_new_vlan_client(vlan, net_socket_receive_dgram, NULL, s); + s->vc = qemu_new_vlan_client(vlan, model, name, net_socket_receive_dgram, + NULL, net_socket_cleanup, s); qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s); /* mcast: save bound address as dst */ @@ -1178,16 +1408,16 @@ static void net_socket_connect(void *opaque) qemu_set_fd_handler(s->fd, net_socket_send, NULL, s); } -static NetSocketState *net_socket_fd_init_stream(VLANState *vlan, int fd, - int is_connected) +static NetSocketState *net_socket_fd_init_stream(VLANState *vlan, + const char *model, + const char *name, + int fd, int is_connected) { NetSocketState *s; s = qemu_mallocz(sizeof(NetSocketState)); - if (!s) - return NULL; s->fd = fd; - s->vc = qemu_new_vlan_client(vlan, - net_socket_receive, NULL, s); + s->vc = qemu_new_vlan_client(vlan, model, name, net_socket_receive, + NULL, net_socket_cleanup, s); snprintf(s->vc->info_str, sizeof(s->vc->info_str), "socket: fd=%d", fd); if (is_connected) { @@ -1198,8 +1428,9 @@ static NetSocketState *net_socket_fd_init_stream(VLANState *vlan, int fd, return s; } -static NetSocketState *net_socket_fd_init(VLANState *vlan, int fd, - int is_connected) +static NetSocketState *net_socket_fd_init(VLANState *vlan, + const char *model, const char *name, + int fd, int is_connected) { int so_type=-1, optlen=sizeof(so_type); @@ -1210,13 +1441,13 @@ static NetSocketState *net_socket_fd_init(VLANState *vlan, int fd, } switch(so_type) { case SOCK_DGRAM: - return net_socket_fd_init_dgram(vlan, fd, is_connected); + return net_socket_fd_init_dgram(vlan, model, name, fd, is_connected); case SOCK_STREAM: - return net_socket_fd_init_stream(vlan, fd, is_connected); + return net_socket_fd_init_stream(vlan, model, name, fd, is_connected); default: /* who knows ... this could be a eg. a pty, do warn and continue as stream */ fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd); - return net_socket_fd_init_stream(vlan, fd, is_connected); + return net_socket_fd_init_stream(vlan, model, name, fd, is_connected); } return NULL; } @@ -1238,7 +1469,7 @@ static void net_socket_accept(void *opaque) break; } } - s1 = net_socket_fd_init(s->vlan, fd, 1); + s1 = net_socket_fd_init(s->vlan, s->model, s->name, fd, 1); if (!s1) { closesocket(fd); } else { @@ -1248,7 +1479,10 @@ static void net_socket_accept(void *opaque) } } -static int net_socket_listen_init(VLANState *vlan, const char *host_str) +static int net_socket_listen_init(VLANState *vlan, + const char *model, + const char *name, + const char *host_str) { NetSocketListenState *s; int fd, val, ret; @@ -1258,8 +1492,6 @@ static int net_socket_listen_init(VLANState *vlan, const char *host_str) return -1; s = qemu_mallocz(sizeof(NetSocketListenState)); - if (!s) - return -1; fd = socket(PF_INET, SOCK_STREAM, 0); if (fd < 0) { @@ -1283,12 +1515,17 @@ static int net_socket_listen_init(VLANState *vlan, const char *host_str) return -1; } s->vlan = vlan; + s->model = strdup(model); + s->name = name ? strdup(name) : NULL; s->fd = fd; qemu_set_fd_handler(fd, net_socket_accept, NULL, s); return 0; } -static int net_socket_connect_init(VLANState *vlan, const char *host_str) +static int net_socket_connect_init(VLANState *vlan, + const char *model, + const char *name, + const char *host_str) { NetSocketState *s; int fd, connected, ret, err; @@ -1326,7 +1563,7 @@ static int net_socket_connect_init(VLANState *vlan, const char *host_str) break; } } - s = net_socket_fd_init(vlan, fd, connected); + s = net_socket_fd_init(vlan, model, name, fd, connected); if (!s) return -1; snprintf(s->vc->info_str, sizeof(s->vc->info_str), @@ -1335,7 +1572,10 @@ static int net_socket_connect_init(VLANState *vlan, const char *host_str) return 0; } -static int net_socket_mcast_init(VLANState *vlan, const char *host_str) +static int net_socket_mcast_init(VLANState *vlan, + const char *model, + const char *name, + const char *host_str) { NetSocketState *s; int fd; @@ -1349,7 +1589,7 @@ static int net_socket_mcast_init(VLANState *vlan, const char *host_str) if (fd < 0) return -1; - s = net_socket_fd_init(vlan, fd, 0); + s = net_socket_fd_init(vlan, model, name, fd, 0); if (!s) return -1; @@ -1362,6 +1602,106 @@ static int net_socket_mcast_init(VLANState *vlan, const char *host_str) } +typedef struct DumpState { + VLANClientState *pcap_vc; + int fd; + int pcap_caplen; +} DumpState; + +#define PCAP_MAGIC 0xa1b2c3d4 + +struct pcap_file_hdr { + uint32_t magic; + uint16_t version_major; + uint16_t version_minor; + int32_t thiszone; + uint32_t sigfigs; + uint32_t snaplen; + uint32_t linktype; +}; + +struct pcap_sf_pkthdr { + struct { + int32_t tv_sec; + int32_t tv_usec; + } ts; + uint32_t caplen; + uint32_t len; +}; + +static void dump_receive(void *opaque, const uint8_t *buf, int size) +{ + DumpState *s = opaque; + struct pcap_sf_pkthdr hdr; + int64_t ts; + int caplen; + + /* Early return in case of previous error. */ + if (s->fd < 0) { + return; + } + + ts = muldiv64(qemu_get_clock(vm_clock), 1000000, ticks_per_sec); + caplen = size > s->pcap_caplen ? s->pcap_caplen : size; + + hdr.ts.tv_sec = ts / 1000000; + hdr.ts.tv_usec = ts % 1000000; + hdr.caplen = caplen; + hdr.len = size; + if (write(s->fd, &hdr, sizeof(hdr)) != sizeof(hdr) || + write(s->fd, buf, caplen) != caplen) { + qemu_log("-net dump write error - stop dump\n"); + close(s->fd); + s->fd = -1; + } +} + +static void net_dump_cleanup(VLANClientState *vc) +{ + DumpState *s = vc->opaque; + + close(s->fd); + qemu_free(s); +} + +static int net_dump_init(VLANState *vlan, const char *device, + const char *name, const char *filename, int len) +{ + struct pcap_file_hdr hdr; + DumpState *s; + + s = qemu_malloc(sizeof(DumpState)); + + s->fd = open(filename, O_CREAT | O_WRONLY, 0644); + if (s->fd < 0) { + fprintf(stderr, "-net dump: can't open %s\n", filename); + return -1; + } + + s->pcap_caplen = len; + + hdr.magic = PCAP_MAGIC; + hdr.version_major = 2; + hdr.version_minor = 4; + hdr.thiszone = 0; + hdr.sigfigs = 0; + hdr.snaplen = s->pcap_caplen; + hdr.linktype = 1; + + if (write(s->fd, &hdr, sizeof(hdr)) < sizeof(hdr)) { + perror("-net dump write error"); + close(s->fd); + qemu_free(s); + return -1; + } + + s->pcap_vc = qemu_new_vlan_client(vlan, device, name, dump_receive, NULL, + net_dump_cleanup, s); + snprintf(s->pcap_vc->info_str, sizeof(s->pcap_vc->info_str), + "dump to %s (len=%d)", filename, len); + return 0; +} + /* find or alloc a new VLAN */ VLANState *qemu_find_vlan(int id) { @@ -1371,8 +1711,6 @@ VLANState *qemu_find_vlan(int id) return vlan; } vlan = qemu_mallocz(sizeof(VLANState)); - if (!vlan) - return NULL; vlan->id = id; vlan->next = NULL; pvlan = &first_vlan; @@ -1382,76 +1720,193 @@ VLANState *qemu_find_vlan(int id) return vlan; } +static int nic_get_free_idx(void) +{ + int index; + + for (index = 0; index < MAX_NICS; index++) + if (!nd_table[index].used) + return index; + return -1; +} + +void qemu_check_nic_model(NICInfo *nd, const char *model) +{ + const char *models[2]; + + models[0] = model; + models[1] = NULL; + + qemu_check_nic_model_list(nd, models, model); +} + +void qemu_check_nic_model_list(NICInfo *nd, const char * const *models, + const char *default_model) +{ + int i, exit_status = 0; + + if (!nd->model) + nd->model = strdup(default_model); + + if (strcmp(nd->model, "?") != 0) { + for (i = 0 ; models[i]; i++) + if (strcmp(nd->model, models[i]) == 0) + return; + + fprintf(stderr, "qemu: Unsupported NIC model: %s\n", nd->model); + exit_status = 1; + } + + fprintf(stderr, "qemu: Supported NIC models: "); + for (i = 0 ; models[i]; i++) + fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n'); + + exit(exit_status); +} + int net_client_init(const char *device, const char *p) { + static const char * const fd_params[] = { + "vlan", "name", "fd", NULL + }; char buf[1024]; int vlan_id, ret; VLANState *vlan; + char *name = NULL; vlan_id = 0; if (get_param_value(buf, sizeof(buf), "vlan", p)) { vlan_id = strtol(buf, NULL, 0); } vlan = qemu_find_vlan(vlan_id); - if (!vlan) { - fprintf(stderr, "Could not create vlan %d\n", vlan_id); - return -1; + + if (get_param_value(buf, sizeof(buf), "name", p)) { + name = strdup(buf); } if (!strcmp(device, "nic")) { + static const char * const nic_params[] = { + "vlan", "name", "macaddr", "model", NULL + }; NICInfo *nd; uint8_t *macaddr; + int idx = nic_get_free_idx(); - if (nb_nics >= MAX_NICS) { - fprintf(stderr, "Too Many NICs\n"); + if (check_params(nic_params, p) < 0) { + fprintf(stderr, "qemu: invalid parameter '%s' in '%s'\n", + buf, p); return -1; } - nd = &nd_table[nb_nics]; + if (idx == -1 || nb_nics >= MAX_NICS) { + fprintf(stderr, "Too Many NICs\n"); + ret = -1; + goto out; + } + nd = &nd_table[idx]; macaddr = nd->macaddr; macaddr[0] = 0x52; macaddr[1] = 0x54; macaddr[2] = 0x00; macaddr[3] = 0x12; macaddr[4] = 0x34; - macaddr[5] = 0x56 + nb_nics; + macaddr[5] = 0x56 + idx; if (get_param_value(buf, sizeof(buf), "macaddr", p)) { if (parse_macaddr(macaddr, buf) < 0) { fprintf(stderr, "invalid syntax for ethernet address\n"); - return -1; + ret = -1; + goto out; } } if (get_param_value(buf, sizeof(buf), "model", p)) { nd->model = strdup(buf); } nd->vlan = vlan; + nd->name = name; + nd->used = 1; + name = NULL; nb_nics++; vlan->nb_guest_devs++; - ret = 0; + ret = idx; } else if (!strcmp(device, "none")) { + if (*p != '\0') { + fprintf(stderr, "qemu: 'none' takes no parameters\n"); + return -1; + } /* does nothing. It is needed to signal that no network cards are wanted */ ret = 0; } else #ifdef CONFIG_SLIRP if (!strcmp(device, "user")) { + static const char * const slirp_params[] = { + "vlan", "name", "hostname", "restrict", "ip", NULL + }; + if (check_params(slirp_params, p) < 0) { + fprintf(stderr, "qemu: invalid parameter '%s' in '%s'\n", + buf, p); + return -1; + } if (get_param_value(buf, sizeof(buf), "hostname", p)) { pstrcpy(slirp_hostname, sizeof(slirp_hostname), buf); } + if (get_param_value(buf, sizeof(buf), "restrict", p)) { + slirp_restrict = (buf[0] == 'y') ? 1 : 0; + } + if (get_param_value(buf, sizeof(buf), "ip", p)) { + slirp_ip = strdup(buf); + } vlan->nb_host_devs++; - ret = net_slirp_init(vlan); + ret = net_slirp_init(vlan, device, name); + } else if (!strcmp(device, "channel")) { + long port; + char name[20], *devname; + struct VMChannel *vmc; + + port = strtol(p, &devname, 10); + devname++; + if (port < 1 || port > 65535) { + fprintf(stderr, "vmchannel wrong port number\n"); + ret = -1; + goto out; + } + vmc = malloc(sizeof(struct VMChannel)); + snprintf(name, 20, "vmchannel%ld", port); + vmc->hd = qemu_chr_open(name, devname, NULL); + if (!vmc->hd) { + fprintf(stderr, "qemu: could not open vmchannel device" + "'%s'\n", devname); + ret = -1; + goto out; + } + vmc->port = port; + slirp_add_exec(3, vmc->hd, 4, port); + qemu_chr_add_handlers(vmc->hd, vmchannel_can_read, vmchannel_read, + NULL, vmc); + ret = 0; } else #endif #ifdef _WIN32 if (!strcmp(device, "tap")) { + static const char * const tap_params[] = { + "vlan", "name", "ifname", NULL + }; char ifname[64]; + + if (check_params(tap_params, p) < 0) { + fprintf(stderr, "qemu: invalid parameter '%s' in '%s'\n", + buf, p); + return -1; + } if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) { fprintf(stderr, "tap: no interface name\n"); - return -1; + ret = -1; + goto out; } vlan->nb_host_devs++; - ret = tap_win32_init(vlan, ifname); + ret = tap_win32_init(vlan, device, name, ifname); } else +#elif defined (_AIX) #else if (!strcmp(device, "tap")) { char ifname[64]; @@ -1459,12 +1914,24 @@ int net_client_init(const char *device, const char *p) int fd; vlan->nb_host_devs++; if (get_param_value(buf, sizeof(buf), "fd", p) > 0) { + if (check_params(fd_params, p) < 0) { + fprintf(stderr, "qemu: invalid parameter '%s' in '%s'\n", + buf, p); + return -1; + } fd = strtol(buf, NULL, 0); fcntl(fd, F_SETFL, O_NONBLOCK); - ret = -1; - if (net_tap_fd_init(vlan, fd)) - ret = 0; + net_tap_fd_init(vlan, device, name, fd); + ret = 0; } else { + static const char * const tap_params[] = { + "vlan", "name", "ifname", "script", "downscript", NULL + }; + if (check_params(tap_params, p) < 0) { + fprintf(stderr, "qemu: invalid parameter '%s' in '%s'\n", + buf, p); + return -1; + } if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) { ifname[0] = '\0'; } @@ -1474,33 +1941,72 @@ int net_client_init(const char *device, const char *p) if (get_param_value(down_script, sizeof(down_script), "downscript", p) == 0) { pstrcpy(down_script, sizeof(down_script), DEFAULT_NETWORK_DOWN_SCRIPT); } - ret = net_tap_init(vlan, ifname, setup_script, down_script); + ret = net_tap_init(vlan, device, name, ifname, setup_script, down_script); } } else #endif if (!strcmp(device, "socket")) { if (get_param_value(buf, sizeof(buf), "fd", p) > 0) { int fd; + if (check_params(fd_params, p) < 0) { + fprintf(stderr, "qemu: invalid parameter '%s' in '%s'\n", + buf, p); + return -1; + } fd = strtol(buf, NULL, 0); ret = -1; - if (net_socket_fd_init(vlan, fd, 1)) + if (net_socket_fd_init(vlan, device, name, fd, 1)) ret = 0; } else if (get_param_value(buf, sizeof(buf), "listen", p) > 0) { - ret = net_socket_listen_init(vlan, buf); + static const char * const listen_params[] = { + "vlan", "name", "listen", NULL + }; + if (check_params(listen_params, p) < 0) { + fprintf(stderr, "qemu: invalid parameter '%s' in '%s'\n", + buf, p); + return -1; + } + ret = net_socket_listen_init(vlan, device, name, buf); } else if (get_param_value(buf, sizeof(buf), "connect", p) > 0) { - ret = net_socket_connect_init(vlan, buf); + static const char * const connect_params[] = { + "vlan", "name", "connect", NULL + }; + if (check_params(connect_params, p) < 0) { + fprintf(stderr, "qemu: invalid parameter '%s' in '%s'\n", + buf, p); + return -1; + } + ret = net_socket_connect_init(vlan, device, name, buf); } else if (get_param_value(buf, sizeof(buf), "mcast", p) > 0) { - ret = net_socket_mcast_init(vlan, buf); + static const char * const mcast_params[] = { + "vlan", "name", "mcast", NULL + }; + if (check_params(mcast_params, p) < 0) { + fprintf(stderr, "qemu: invalid parameter '%s' in '%s'\n", + buf, p); + return -1; + } + ret = net_socket_mcast_init(vlan, device, name, buf); } else { fprintf(stderr, "Unknown socket options: %s\n", p); - return -1; + ret = -1; + goto out; } vlan->nb_host_devs++; } else #ifdef CONFIG_VDE if (!strcmp(device, "vde")) { + static const char * const vde_params[] = { + "vlan", "name", "sock", "port", "group", "mode", NULL + }; char vde_sock[1024], vde_group[512]; int vde_port, vde_mode; + + if (check_params(vde_params, p) < 0) { + fprintf(stderr, "qemu: invalid parameter '%s' in '%s'\n", + buf, p); + return -1; + } vlan->nb_host_devs++; if (get_param_value(vde_sock, sizeof(vde_sock), "sock", p) <= 0) { vde_sock[0] = '\0'; @@ -1518,20 +2024,96 @@ int net_client_init(const char *device, const char *p) } else { vde_mode = 0700; } - ret = net_vde_init(vlan, vde_sock, vde_port, vde_group, vde_mode); + ret = net_vde_init(vlan, device, name, vde_sock, vde_port, vde_group, vde_mode); } else #endif - { + if (!strcmp(device, "dump")) { + int len = 65536; + + if (get_param_value(buf, sizeof(buf), "len", p) > 0) { + len = strtol(buf, NULL, 0); + } + if (!get_param_value(buf, sizeof(buf), "file", p)) { + snprintf(buf, sizeof(buf), "qemu-vlan%d.pcap", vlan_id); + } + ret = net_dump_init(vlan, device, name, buf, len); + } else { fprintf(stderr, "Unknown network device: %s\n", device); - return -1; + ret = -1; + goto out; } if (ret < 0) { fprintf(stderr, "Could not initialize device '%s'\n", device); } - +out: + if (name) + free(name); return ret; } +void net_client_uninit(NICInfo *nd) +{ + nd->vlan->nb_guest_devs--; + nb_nics--; + nd->used = 0; + free((void *)nd->model); +} + +static int net_host_check_device(const char *device) +{ + int i; + const char *valid_param_list[] = { "tap", "socket", "dump" +#ifdef CONFIG_SLIRP + ,"user" +#endif +#ifdef CONFIG_VDE + ,"vde" +#endif + }; + for (i = 0; i < sizeof(valid_param_list) / sizeof(char *); i++) { + if (!strncmp(valid_param_list[i], device, + strlen(valid_param_list[i]))) + return 1; + } + + return 0; +} + +void net_host_device_add(Monitor *mon, const char *device, const char *opts) +{ + if (!net_host_check_device(device)) { + monitor_printf(mon, "invalid host network device %s\n", device); + return; + } + if (net_client_init(device, opts ? opts : "") < 0) { + monitor_printf(mon, "adding host network device %s failed\n", device); + } +} + +void net_host_device_remove(Monitor *mon, int vlan_id, const char *device) +{ + VLANState *vlan; + VLANClientState *vc; + + vlan = qemu_find_vlan(vlan_id); + + for (vc = vlan->first_client; vc != NULL; vc = vc->next) { + if (!strcmp(vc->name, device)) { + break; + } + } + + if (!vc) { + monitor_printf(mon, "can't find device %s\n", device); + return; + } + if (!net_host_check_device(vc->model)) { + monitor_printf(mon, "invalid host network device %s\n", device); + return; + } + qemu_del_vlan_client(vc); +} + int net_client_parse(const char *str) { const char *p; @@ -1552,45 +2134,64 @@ int net_client_parse(const char *str) return net_client_init(device, p); } -void do_info_network(void) +void do_info_network(Monitor *mon) { VLANState *vlan; VLANClientState *vc; for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) { - term_printf("VLAN %d devices:\n", vlan->id); + monitor_printf(mon, "VLAN %d devices:\n", vlan->id); for(vc = vlan->first_client; vc != NULL; vc = vc->next) - term_printf(" %s\n", vc->info_str); + monitor_printf(mon, " %s: %s\n", vc->name, vc->info_str); } } +int do_set_link(Monitor *mon, const char *name, const char *up_or_down) +{ + VLANState *vlan; + VLANClientState *vc = NULL; + + for (vlan = first_vlan; vlan != NULL; vlan = vlan->next) + for (vc = vlan->first_client; vc != NULL; vc = vc->next) + if (strcmp(vc->name, name) == 0) + goto done; +done: + + if (!vc) { + monitor_printf(mon, "could not find network device '%s'", name); + return 0; + } + + if (strcmp(up_or_down, "up") == 0) + vc->link_down = 0; + else if (strcmp(up_or_down, "down") == 0) + vc->link_down = 1; + else + monitor_printf(mon, "invalid link status '%s'; only 'up' or 'down' " + "valid\n", up_or_down); + + if (vc->link_status_changed) + vc->link_status_changed(vc); + + return 1; +} + void net_cleanup(void) { VLANState *vlan; -#if !defined(_WIN32) /* close network clients */ for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) { - VLANClientState *vc; + VLANClientState *vc = vlan->first_client; - for(vc = vlan->first_client; vc != NULL; vc = vc->next) { - if (vc->fd_read == tap_receive) { - char ifname[64]; - TAPState *s = vc->opaque; + while (vc) { + VLANClientState *next = vc->next; - if (sscanf(vc->info_str, "tap: ifname=%63s ", ifname) == 1 && - s->down_script[0]) - launch_script(s->down_script, ifname, s->fd); - } -#if defined(CONFIG_VDE) - if (vc->fd_read == vde_from_qemu) { - VDEState *s = vc->opaque; - vde_close(s->vde); - } -#endif + qemu_del_vlan_client(vc); + + vc = next; } } -#endif } void net_client_check(void)