slirp: pass opaque to all callbacks

This is friendlier for FFI bindings.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Signed-off-by: Samuel Thibault <samuel.thibault@ens-lyon.org>
This commit is contained in:
Marc-André Lureau 2019-01-17 15:43:58 +04:00 committed by Samuel Thibault
parent deaeb3f71d
commit 3e0fad3aa5
11 changed files with 49 additions and 40 deletions

View File

@ -161,44 +161,51 @@ static NetClientInfo net_slirp_info = {
.cleanup = net_slirp_cleanup,
};
static void net_slirp_guest_error(const char *msg)
static void net_slirp_guest_error(const char *msg, void *opaque)
{
qemu_log_mask(LOG_GUEST_ERROR, "%s", msg);
}
static int64_t net_slirp_clock_get_ns(void)
static int64_t net_slirp_clock_get_ns(void *opaque)
{
return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
}
static void *net_slirp_timer_new(SlirpTimerCb cb, void *opaque)
static void *net_slirp_timer_new(SlirpTimerCb cb,
void *cb_opaque, void *opaque)
{
return timer_new_full(NULL, QEMU_CLOCK_VIRTUAL,
SCALE_MS, QEMU_TIMER_ATTR_EXTERNAL,
cb, opaque);
cb, cb_opaque);
}
static void net_slirp_timer_free(void *timer)
static void net_slirp_timer_free(void *timer, void *opaque)
{
timer_del(timer);
timer_free(timer);
}
static void net_slirp_timer_mod(void *timer, int64_t expire_timer)
static void net_slirp_timer_mod(void *timer, int64_t expire_timer,
void *opaque)
{
timer_mod(timer, expire_timer);
}
static void net_slirp_register_poll_fd(int fd)
static void net_slirp_register_poll_fd(int fd, void *opaque)
{
qemu_fd_register(fd);
}
static void net_slirp_unregister_poll_fd(int fd)
static void net_slirp_unregister_poll_fd(int fd, void *opaque)
{
/* no qemu_fd_unregister */
}
static void net_slirp_notify(void *opaque)
{
qemu_notify_event();
}
static const SlirpCb slirp_cb = {
.send_packet = net_slirp_send_packet,
.guest_error = net_slirp_guest_error,
@ -208,7 +215,7 @@ static const SlirpCb slirp_cb = {
.timer_mod = net_slirp_timer_mod,
.register_poll_fd = net_slirp_register_poll_fd,
.unregister_poll_fd = net_slirp_unregister_poll_fd,
.notify = qemu_notify_event,
.notify = net_slirp_notify,
};
static int slirp_poll_to_gio(int events)

View File

@ -59,7 +59,7 @@ static int dhcpv6_parse_info_request(Slirp *slirp, uint8_t *odata, int olen,
int len = odata[2] << 8 | odata[3];
if (len + 4 > olen) {
slirp->cb->guest_error("Guest sent bad DHCPv6 packet!");
slirp->cb->guest_error("Guest sent bad DHCPv6 packet!", slirp->opaque);
return -E2BIG;
}

View File

@ -146,7 +146,7 @@ diddit:
*/
void if_start(Slirp *slirp)
{
uint64_t now = slirp->cb->clock_get_ns();
uint64_t now = slirp->cb->clock_get_ns(slirp->opaque);
bool from_batchq = false;
struct mbuf *ifm, *ifm_next, *ifqt;

View File

@ -14,7 +14,8 @@ static void ra_timer_handler(void *opaque)
Slirp *slirp = opaque;
slirp->cb->timer_mod(slirp->ra_timer,
slirp->cb->clock_get_ns() / SCALE_MS + NDP_Interval);
slirp->cb->clock_get_ns(slirp->opaque) / SCALE_MS + NDP_Interval,
slirp->opaque);
ndp_send_ra(slirp);
}
@ -24,9 +25,10 @@ void icmp6_init(Slirp *slirp)
return;
}
slirp->ra_timer = slirp->cb->timer_new(ra_timer_handler, slirp);
slirp->ra_timer = slirp->cb->timer_new(ra_timer_handler, slirp, slirp->opaque);
slirp->cb->timer_mod(slirp->ra_timer,
slirp->cb->clock_get_ns() / SCALE_MS + NDP_Interval);
slirp->cb->clock_get_ns(slirp->opaque) / SCALE_MS + NDP_Interval,
slirp->opaque);
}
void icmp6_cleanup(Slirp *slirp)
@ -35,7 +37,7 @@ void icmp6_cleanup(Slirp *slirp)
return;
}
slirp->cb->timer_free(slirp->ra_timer);
slirp->cb->timer_free(slirp->ra_timer, slirp->opaque);
}
static void icmp6_send_echoreply(struct mbuf *m, Slirp *slirp, struct ip6 *ip,
@ -334,7 +336,8 @@ static void ndp_input(struct mbuf *m, Slirp *slirp, struct ip6 *ip,
case ICMP6_NDP_RA:
DEBUG_CALL(" type = Router Advertisement");
slirp->cb->guest_error("Warning: guest sent NDP RA, but shouldn't");
slirp->cb->guest_error("Warning: guest sent NDP RA, but shouldn't",
slirp->opaque);
break;
case ICMP6_NDP_NS:
@ -368,7 +371,7 @@ static void ndp_input(struct mbuf *m, Slirp *slirp, struct ip6 *ip,
case ICMP6_NDP_REDIRECT:
DEBUG_CALL(" type = Redirect");
slirp->cb->guest_error(
"Warning: guest sent NDP REDIRECT, but shouldn't");
"Warning: guest sent NDP REDIRECT, but shouldn't", slirp->opaque);
break;
}
}

View File

@ -113,7 +113,7 @@ static int icmp_send(struct socket *so, struct mbuf *m, int hlen)
void icmp_detach(struct socket *so)
{
so->slirp->cb->unregister_poll_fd(so->s);
so->slirp->cb->unregister_poll_fd(so->s, so->slirp->opaque);
slirp_closesocket(so->s);
sofree(so);
}

View File

@ -40,21 +40,21 @@ typedef struct SlirpCb {
*/
SlirpWriteCb send_packet;
/* Print a message for an error due to guest misbehavior. */
void (*guest_error)(const char *msg);
void (*guest_error)(const char *msg, void *opaque);
/* Return the virtual clock value in nanoseconds */
int64_t (*clock_get_ns)(void);
int64_t (*clock_get_ns)(void *opaque);
/* Create a new timer with the given callback and opaque data */
void *(*timer_new)(SlirpTimerCb cb, void *opaque);
void *(*timer_new)(SlirpTimerCb cb, void *cb_opaque, void *opaque);
/* Remove and free a timer */
void (*timer_free)(void *timer);
void (*timer_free)(void *timer, void *opaque);
/* Modify a timer to expire at @expire_time */
void (*timer_mod)(void *timer, int64_t expire_time);
void (*timer_mod)(void *timer, int64_t expire_time, void *opaque);
/* Register a fd for future polling */
void (*register_poll_fd)(int fd);
void (*register_poll_fd)(int fd, void *opaque);
/* Unregister a fd */
void (*unregister_poll_fd)(int fd);
void (*unregister_poll_fd)(int fd, void *opaque);
/* Kick the io-thread, to signal that new events may be processed */
void (*notify)(void);
void (*notify)(void *opaque);
} SlirpCb;

View File

@ -222,7 +222,7 @@ fork_exec(struct socket *so, const char *ex)
opt = 1;
slirp_setsockopt(so->s, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(int));
slirp_set_nonblock(so->s);
so->slirp->cb->register_poll_fd(so->s);
so->slirp->cb->register_poll_fd(so->s, so->slirp->opaque);
return 1;
}

View File

@ -289,6 +289,7 @@ Slirp *slirp_init(int restricted, bool in_enabled, struct in_addr vnetwork,
slirp_init_once();
slirp->opaque = opaque;
slirp->cb = callbacks;
slirp->grand = g_rand_new();
slirp->restricted = restricted;
@ -325,12 +326,9 @@ Slirp *slirp_init(int restricted, bool in_enabled, struct in_addr vnetwork,
translate_dnssearch(slirp, vdnssearch);
}
slirp->opaque = opaque;
#ifdef WITH_QEMU
slirp_state_register(slirp);
#endif
return slirp;
}
@ -537,7 +535,7 @@ void slirp_pollfds_poll(Slirp *slirp, int select_error,
struct socket *so, *so_next;
int ret;
curtime = slirp->cb->clock_get_ns() / SCALE_MS;
curtime = slirp->cb->clock_get_ns(slirp->opaque) / SCALE_MS;
/*
* See if anything has timed out
@ -860,7 +858,8 @@ static int if_encap4(Slirp *slirp, struct mbuf *ifm, struct ethhdr *eh,
ifm->resolution_requested = true;
/* Expire request and drop outgoing packet after 1 second */
ifm->expiration_date = slirp->cb->clock_get_ns() + 1000000000ULL;
ifm->expiration_date =
slirp->cb->clock_get_ns(slirp->opaque) + 1000000000ULL;
}
return 0;
} else {
@ -886,7 +885,7 @@ static int if_encap6(Slirp *slirp, struct mbuf *ifm, struct ethhdr *eh,
if (!ifm->resolution_requested) {
ndp_send_ns(slirp, ip6h->ip_dst);
ifm->resolution_requested = true;
ifm->expiration_date = slirp->cb->clock_get_ns() + 1000000000ULL;
ifm->expiration_date = slirp->cb->clock_get_ns(slirp->opaque) + 1000000000ULL;
}
return 0;
} else {
@ -961,7 +960,7 @@ int slirp_remove_hostfwd(Slirp *slirp, int is_udp, struct in_addr host_addr,
getsockname(so->s, (struct sockaddr *)&addr, &addr_len) == 0 &&
addr.sin_addr.s_addr == host_addr.s_addr &&
addr.sin_port == port) {
so->slirp->cb->unregister_poll_fd(so->s);
so->slirp->cb->unregister_poll_fd(so->s, so->slirp->opaque);
slirp_closesocket(so->s);
sofree(so);
return 0;

View File

@ -930,6 +930,6 @@ void sotranslate_accept(struct socket *so)
void sodrop(struct socket *s, int num)
{
if (sbdrop(&s->so_snd, num)) {
s->slirp->cb->notify();
s->slirp->cb->notify(s->slirp->opaque);
}
}

View File

@ -336,7 +336,7 @@ tcp_close(struct tcpcb *tp)
/* clobber input socket cache if we're closing the cached connection */
if (so == slirp->tcp_last_so)
slirp->tcp_last_so = &slirp->tcb;
so->slirp->cb->unregister_poll_fd(so->s);
so->slirp->cb->unregister_poll_fd(so->s, so->slirp->opaque);
slirp_closesocket(so->s);
sbfree(&so->so_rcv);
sbfree(&so->so_snd);
@ -413,7 +413,7 @@ int tcp_fconnect(struct socket *so, unsigned short af)
struct sockaddr_storage addr;
slirp_set_nonblock(s);
so->slirp->cb->register_poll_fd(so->s);
so->slirp->cb->register_poll_fd(so->s, so->slirp->opaque);
slirp_socket_set_fast_reuse(s);
opt = 1;
slirp_setsockopt(s, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(opt));
@ -486,7 +486,7 @@ void tcp_connect(struct socket *inso)
return;
}
slirp_set_nonblock(s);
so->slirp->cb->register_poll_fd(so->s);
so->slirp->cb->register_poll_fd(so->s, so->slirp->opaque);
slirp_socket_set_fast_reuse(s);
opt = 1;
slirp_setsockopt(s, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(int));
@ -498,7 +498,7 @@ void tcp_connect(struct socket *inso)
/* Close the accept() socket, set right state */
if (inso->so_state & SS_FACCEPTONCE) {
/* If we only accept once, close the accept() socket */
so->slirp->cb->unregister_poll_fd(so->s);
so->slirp->cb->unregister_poll_fd(so->s, so->slirp->opaque);
slirp_closesocket(so->s);
/* Don't select it yet, even though we have an FD */

View File

@ -291,7 +291,7 @@ udp_attach(struct socket *so, unsigned short af)
void
udp_detach(struct socket *so)
{
so->slirp->cb->unregister_poll_fd(so->s);
so->slirp->cb->unregister_poll_fd(so->s, so->slirp->opaque);
slirp_closesocket(so->s);
sofree(so);
}