diff --git a/net.c b/net.c index eb26cd4656..8e75a29d98 100644 --- a/net.c +++ b/net.c @@ -1043,10 +1043,29 @@ typedef struct TAPState { char down_script[1024]; char down_script_arg[128]; uint8_t buf[4096]; + unsigned int read_poll : 1; } TAPState; static int launch_script(const char *setup_script, const char *ifname, int fd); +static int tap_can_send(void *opaque); +static void tap_send(void *opaque); + +static void tap_update_fd_handler(TAPState *s) +{ + qemu_set_fd_handler2(s->fd, + s->read_poll ? tap_can_send : NULL, + s->read_poll ? tap_send : NULL, + NULL, + s); +} + +static void tap_read_poll(TAPState *s, int enable) +{ + s->read_poll = !!enable; + tap_update_fd_handler(s); +} + static ssize_t tap_receive_iov(VLANClientState *vc, const struct iovec *iov, int iovcnt) { @@ -1097,13 +1116,10 @@ static ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen) } #endif -static void tap_send(void *opaque); - static void tap_send_completed(VLANClientState *vc) { TAPState *s = vc->opaque; - - qemu_set_fd_handler2(s->fd, tap_can_send, tap_send, NULL, s); + tap_read_poll(s, 1); } static void tap_send(void *opaque) @@ -1119,7 +1135,7 @@ static void tap_send(void *opaque) size = qemu_send_packet_async(s->vc, s->buf, size, tap_send_completed); if (size == 0) { - qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL); + tap_read_poll(s, 0); } } while (size > 0); } @@ -1133,7 +1149,7 @@ static void tap_cleanup(VLANClientState *vc) 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); + tap_read_poll(s, 0); close(s->fd); qemu_free(s); } @@ -1151,7 +1167,7 @@ static TAPState *net_tap_fd_init(VLANState *vlan, s->fd = fd; s->vc = qemu_new_vlan_client(vlan, model, name, NULL, tap_receive, tap_receive_iov, tap_cleanup, s); - qemu_set_fd_handler2(s->fd, tap_can_send, tap_send, NULL, s); + tap_read_poll(s, 1); snprintf(s->vc->info_str, sizeof(s->vc->info_str), "fd=%d", fd); return s; }