X-Git-Url: http://git.maemo.org/git/?a=blobdiff_plain;f=net.c;h=395ee4f5fcbf33e18be94ceedfba079e66bbf8b5;hb=d9c9f02a6c674242a7d749340987ee4b078348ae;hp=e29379050f45ea8400f5229b0d9b15533ac1cb93;hpb=7cb7434b1e4aee549b55536208abe56e35159763;p=qemu diff --git a/net.c b/net.c index e293790..395ee4f 100644 --- a/net.c +++ b/net.c @@ -21,14 +21,6 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ -#include "qemu-common.h" -#include "net.h" -#include "console.h" -#include "sysemu.h" -#include "qemu-timer.h" -#include "qemu-char.h" -#include "audio/audio.h" - #include #include #include @@ -37,6 +29,9 @@ #include #include +/* Needed early for HOST_BSD etc. */ +#include "config-host.h" + #ifndef _WIN32 #include #include @@ -57,9 +52,9 @@ #include #include #include -#ifdef _BSD +#ifdef HOST_BSD #include -#ifdef __FreeBSD__ +#if defined(__FreeBSD__) || defined(__DragonFly__) #include #else #include @@ -98,12 +93,6 @@ #endif #endif -#include "qemu_socket.h" - -#if defined(CONFIG_SLIRP) -#include "libslirp.h" -#endif - #if defined(__OpenBSD__) #include #endif @@ -113,6 +102,7 @@ #endif #ifdef _WIN32 +#include #include #include #include @@ -120,6 +110,20 @@ #define memalign(align, size) malloc(size) #endif +#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" + +#if defined(CONFIG_SLIRP) +#include "libslirp.h" +#endif + + static VLANState *first_vlan; /***********************************************************/ @@ -299,7 +303,8 @@ static int parse_unix_path(struct sockaddr_un *uaddr, const char *str) void qemu_format_nic_info_str(VLANClientState *vc, uint8_t macaddr[6]) { snprintf(vc->info_str, sizeof(vc->info_str), - "macaddr=%02x:%02x:%02x:%02x:%02x:%02x", + "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x", + vc->model, macaddr[0], macaddr[1], macaddr[2], macaddr[3], macaddr[4], macaddr[5]); } @@ -325,16 +330,18 @@ static char *assign_name(VLANClientState *vc1, const char *model) VLANClientState *qemu_new_vlan_client(VLANState *vlan, const char *model, + const char *name, IOReadHandler *fd_read, IOCanRWHandler *fd_can_read, void *opaque) { VLANClientState *vc, **pvc; vc = qemu_mallocz(sizeof(VLANClientState)); - if (!vc) - return NULL; vc->model = strdup(model); - vc->name = assign_name(vc, 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->opaque = opaque; @@ -363,6 +370,19 @@ void qemu_del_vlan_client(VLANClientState *vc) 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; @@ -382,12 +402,15 @@ void qemu_send_packet(VLANClientState *vc1, const uint8_t *buf, int size) VLANState *vlan = vc1->vlan; VLANClientState *vc; + if (vc1->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) { + if (vc != vc1 && !vc->link_down) { vc->fd_read(vc->opaque, buf, size); } } @@ -413,6 +436,16 @@ static ssize_t vc_sendv_compat(VLANClientState *vc, const struct iovec *iov, 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) { @@ -420,12 +453,17 @@ ssize_t qemu_sendv_packet(VLANClientState *vc1, const struct iovec *iov, 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) @@ -442,6 +480,8 @@ ssize_t qemu_sendv_packet(VLANClientState *vc1, const struct iovec *iov, /* slirp network adapter */ static int slirp_inited; +static int slirp_restrict; +static char *slirp_ip; static VLANClientState *slirp_vc; int slirp_can_output(void) @@ -474,13 +514,13 @@ static void slirp_receive(void *opaque, const uint8_t *buf, int size) slirp_input(buf, size); } -static int net_slirp_init(VLANState *vlan, const char *model) +static int net_slirp_init(VLANState *vlan, const char *model, const char *name) { if (!slirp_inited) { slirp_inited = 1; - slirp_init(); + slirp_init(slirp_restrict, slirp_ip); } - slirp_vc = qemu_new_vlan_client(vlan, model, + slirp_vc = qemu_new_vlan_client(vlan, model, name, slirp_receive, NULL, NULL); slirp_vc->info_str[0] = '\0'; return 0; @@ -496,7 +536,7 @@ void net_slirp_redir(const char *redir_str) if (!slirp_inited) { slirp_inited = 1; - slirp_init(); + slirp_init(slirp_restrict, slirp_ip); } p = redir_str; @@ -549,7 +589,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) @@ -582,7 +622,7 @@ 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 */ @@ -629,11 +669,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) @@ -642,6 +699,7 @@ typedef struct TAPState { VLANClientState *vc; int fd; char down_script[1024]; + char down_script_arg[128]; } TAPState; #ifdef HAVE_IOVEC @@ -694,15 +752,16 @@ static void tap_send(void *opaque) /* fd support */ -static TAPState *net_tap_fd_init(VLANState *vlan, const char *model, 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, model, tap_receive, NULL, s); + s->vc = qemu_new_vlan_client(vlan, model, name, tap_receive, NULL, s); #ifdef HAVE_IOVEC s->vc->fd_readv = tap_receive_iov; #endif @@ -711,7 +770,7 @@ static TAPState *net_tap_fd_init(VLANState *vlan, const char *model, int 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; @@ -937,7 +996,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 *model, 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; @@ -958,14 +1018,16 @@ static int net_tap_init(VLANState *vlan, const char *model, const char *ifname1, if (launch_script(setup_script, ifname, fd)) return -1; } - s = net_tap_fd_init(vlan, model, fd); + s = net_tap_fd_init(vlan, model, name, fd); if (!s) return -1; snprintf(s->vc->info_str, sizeof(s->vc->info_str), "ifname=%s,script=%s,downscript=%s", ifname, setup_script, down_script); - if (down_script && strcmp(down_script, "no")) + 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; } @@ -1002,7 +1064,8 @@ static void vde_from_qemu(void *opaque, const uint8_t *buf, int size) } } -static int net_vde_init(VLANState *vlan, const char *model, const char *sock, +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; @@ -1016,14 +1079,12 @@ static int net_vde_init(VLANState *vlan, const char *model, const char *sock, }; s = qemu_mallocz(sizeof(VDEState)); - if (!s) - return -1; s->vde = vde_open(init_sock, "QEMU", &args); if (!s->vde){ free(s); return -1; } - s->vc = qemu_new_vlan_client(vlan, model, vde_from_qemu, NULL, s); + s->vc = qemu_new_vlan_client(vlan, model, name, vde_from_qemu, NULL, s); qemu_set_fd_handler(vde_datafd(s->vde), vde_to_qemu, NULL, s); snprintf(s->vc->info_str, sizeof(s->vc->info_str), "sock=%s,fd=%d", sock, vde_datafd(s->vde)); @@ -1036,8 +1097,8 @@ 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; @@ -1045,6 +1106,7 @@ typedef struct NetSocketState { typedef struct NetSocketListenState { VLANState *vlan; char *model; + char *name; int fd; } NetSocketListenState; @@ -1069,7 +1131,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; @@ -1108,7 +1171,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; @@ -1198,7 +1269,9 @@ fail: return -1; } -static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan, const char *model, +static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan, + const char *model, + const char *name, int fd, int is_connected) { struct sockaddr_in saddr; @@ -1238,11 +1311,9 @@ static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan, const char *mod } s = qemu_mallocz(sizeof(NetSocketState)); - if (!s) - return NULL; s->fd = fd; - s->vc = qemu_new_vlan_client(vlan, model, net_socket_receive_dgram, NULL, s); + s->vc = qemu_new_vlan_client(vlan, model, name, net_socket_receive_dgram, NULL, s); qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s); /* mcast: save bound address as dst */ @@ -1261,15 +1332,15 @@ 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, const char *model, +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, model, + s->vc = qemu_new_vlan_client(vlan, model, name, net_socket_receive, NULL, s); snprintf(s->vc->info_str, sizeof(s->vc->info_str), "socket: fd=%d", fd); @@ -1281,7 +1352,8 @@ static NetSocketState *net_socket_fd_init_stream(VLANState *vlan, const char *mo return s; } -static NetSocketState *net_socket_fd_init(VLANState *vlan, const char *model, +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); @@ -1293,13 +1365,13 @@ static NetSocketState *net_socket_fd_init(VLANState *vlan, const char *model, } switch(so_type) { case SOCK_DGRAM: - return net_socket_fd_init_dgram(vlan, model, 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, model, 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, model, fd, is_connected); + return net_socket_fd_init_stream(vlan, model, name, fd, is_connected); } return NULL; } @@ -1321,7 +1393,7 @@ static void net_socket_accept(void *opaque) break; } } - s1 = net_socket_fd_init(s->vlan, s->model, fd, 1); + s1 = net_socket_fd_init(s->vlan, s->model, s->name, fd, 1); if (!s1) { closesocket(fd); } else { @@ -1331,7 +1403,9 @@ static void net_socket_accept(void *opaque) } } -static int net_socket_listen_init(VLANState *vlan, const char *model, +static int net_socket_listen_init(VLANState *vlan, + const char *model, + const char *name, const char *host_str) { NetSocketListenState *s; @@ -1342,8 +1416,6 @@ static int net_socket_listen_init(VLANState *vlan, const char *model, return -1; s = qemu_mallocz(sizeof(NetSocketListenState)); - if (!s) - return -1; fd = socket(PF_INET, SOCK_STREAM, 0); if (fd < 0) { @@ -1368,12 +1440,15 @@ static int net_socket_listen_init(VLANState *vlan, const char *model, } s->vlan = vlan; s->model = strdup(model); + s->name = strdup(name); 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 *model, +static int net_socket_connect_init(VLANState *vlan, + const char *model, + const char *name, const char *host_str) { NetSocketState *s; @@ -1412,7 +1487,7 @@ static int net_socket_connect_init(VLANState *vlan, const char *model, break; } } - s = net_socket_fd_init(vlan, model, 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), @@ -1421,7 +1496,9 @@ static int net_socket_connect_init(VLANState *vlan, const char *model, return 0; } -static int net_socket_mcast_init(VLANState *vlan, const char *model, +static int net_socket_mcast_init(VLANState *vlan, + const char *model, + const char *name, const char *host_str) { NetSocketState *s; @@ -1436,7 +1513,7 @@ static int net_socket_mcast_init(VLANState *vlan, const char *model, if (fd < 0) return -1; - s = net_socket_fd_init(vlan, model, fd, 0); + s = net_socket_fd_init(vlan, model, name, fd, 0); if (!s) return -1; @@ -1458,8 +1535,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; @@ -1469,11 +1544,56 @@ 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) { char buf[1024]; int vlan_id, ret; VLANState *vlan; + char *name = NULL; vlan_id = 0; if (get_param_value(buf, sizeof(buf), "vlan", p)) { @@ -1484,22 +1604,26 @@ int net_client_init(const char *device, const char *p) 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")) { NICInfo *nd; uint8_t *macaddr; + int idx = nic_get_free_idx(); - if (nb_nics >= MAX_NICS) { + if (idx == -1 || nb_nics >= MAX_NICS) { fprintf(stderr, "Too Many NICs\n"); return -1; } - nd = &nd_table[nb_nics]; + 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) { @@ -1511,9 +1635,12 @@ int net_client_init(const char *device, const char *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")) { /* does nothing. It is needed to signal that no network cards @@ -1525,8 +1652,38 @@ int net_client_init(const char *device, const char *p) 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, device); + 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"); + return -1; + } + 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); + return -1; + } + 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 @@ -1537,7 +1694,7 @@ int net_client_init(const char *device, const char *p) return -1; } vlan->nb_host_devs++; - ret = tap_win32_init(vlan, device, ifname); + ret = tap_win32_init(vlan, device, name, ifname); } else #elif defined (_AIX) #else @@ -1550,7 +1707,7 @@ int net_client_init(const char *device, const char *p) fd = strtol(buf, NULL, 0); fcntl(fd, F_SETFL, O_NONBLOCK); ret = -1; - if (net_tap_fd_init(vlan, device, fd)) + if (net_tap_fd_init(vlan, device, name, fd)) ret = 0; } else { if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) { @@ -1562,7 +1719,7 @@ 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, device, ifname, setup_script, down_script); + ret = net_tap_init(vlan, device, name, ifname, setup_script, down_script); } } else #endif @@ -1571,14 +1728,14 @@ int net_client_init(const char *device, const char *p) int fd; fd = strtol(buf, NULL, 0); ret = -1; - if (net_socket_fd_init(vlan, device, 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, device, buf); + 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, device, buf); + 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, device, buf); + ret = net_socket_mcast_init(vlan, device, name, buf); } else { fprintf(stderr, "Unknown socket options: %s\n", p); return -1; @@ -1606,20 +1763,82 @@ int net_client_init(const char *device, const char *p) } else { vde_mode = 0700; } - ret = net_vde_init(vlan, device, 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 { fprintf(stderr, "Unknown network device: %s\n", device); + if (name) + free(name); return -1; } if (ret < 0) { fprintf(stderr, "Could not initialize device '%s'\n", device); } - + 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" +#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; + } + net_client_init(device, opts); +} + +void net_host_device_remove(Monitor *mon, int vlan_id, const char *device) +{ + VLANState *vlan; + VLANClientState *vc; + + vlan = qemu_find_vlan(vlan_id); + if (!vlan) { + monitor_printf(mon, "can't find vlan %d\n", vlan_id); + return; + } + + 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; + } + qemu_del_vlan_client(vc); +} + int net_client_parse(const char *str) { const char *p; @@ -1640,16 +1859,46 @@ 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: %s\n", vc->name, 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) @@ -1663,13 +1912,10 @@ void net_cleanup(void) for(vc = vlan->first_client; vc != NULL; vc = vc->next) { if (vc->fd_read == tap_receive) { - char ifname[64]; TAPState *s = vc->opaque; - if (strcmp(vc->model, "tap") == 0 && - sscanf(vc->info_str, "ifname=%63s ", ifname) == 1 && - s->down_script[0]) - launch_script(s->down_script, ifname, s->fd); + if (s->down_script[0]) + launch_script(s->down_script, s->down_script_arg, s->fd); } #if defined(CONFIG_VDE) if (vc->fd_read == vde_from_qemu) {