From patchwork Thu Feb 7 14:03:15 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Samuel Thibault X-Patchwork-Id: 10801325 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 4AB0A1390 for ; Thu, 7 Feb 2019 14:14:48 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 371E52DA2B for ; Thu, 7 Feb 2019 14:14:48 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3547E2D9F4; Thu, 7 Feb 2019 14:14:48 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 142AD2DA06 for ; Thu, 7 Feb 2019 14:14:46 +0000 (UTC) Received: from localhost ([127.0.0.1]:40729 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1grkRy-0002ev-5h for patchwork-qemu-devel@patchwork.kernel.org; Thu, 07 Feb 2019 09:14:46 -0500 Received: from eggs.gnu.org ([209.51.188.92]:57498) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1grkHW-00029B-Vc for qemu-devel@nongnu.org; Thu, 07 Feb 2019 09:04:04 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1grkHS-000121-VY for qemu-devel@nongnu.org; Thu, 07 Feb 2019 09:03:58 -0500 Received: from hera.aquilenet.fr ([2a0c:e300::1]:49876) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1grkHS-0000zR-96 for qemu-devel@nongnu.org; Thu, 07 Feb 2019 09:03:54 -0500 Received: from localhost (localhost [127.0.0.1]) by hera.aquilenet.fr (Postfix) with ESMTP id 844ECCD3D; Thu, 7 Feb 2019 15:03:45 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at aquilenet.fr Received: from hera.aquilenet.fr ([127.0.0.1]) by localhost (hera.aquilenet.fr [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 7DCOXGfQhT58; Thu, 7 Feb 2019 15:03:44 +0100 (CET) Received: from function (unknown [160.40.205.85]) by hera.aquilenet.fr (Postfix) with ESMTPSA id D95D0CD3F; Thu, 7 Feb 2019 15:03:20 +0100 (CET) Received: from samy by function with local (Exim 4.92-RC4) (envelope-from ) id 1grkGs-0004EF-8n; Thu, 07 Feb 2019 15:03:18 +0100 From: Samuel Thibault To: qemu-devel@nongnu.org, peter.maydell@linaro.org Date: Thu, 7 Feb 2019 16:03:15 +0200 Message-Id: <20190207140316.16103-32-samuel.thibault@ens-lyon.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190207140316.16103-1-samuel.thibault@ens-lyon.org> References: <20190207140316.16103-1-samuel.thibault@ens-lyon.org> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a0c:e300::1 Subject: [Qemu-devel] [PULLv4 31/32] slirp: pass opaque to all callbacks X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: =?utf-8?q?Marc-Andr=C3=A9_Lureau?= , Samuel Thibault , stefanha@redhat.com, jan.kiszka@siemens.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP From: Marc-André Lureau This is friendlier for FFI bindings. Signed-off-by: Marc-André Lureau Signed-off-by: Samuel Thibault --- net/slirp.c | 25 ++++++++++++++++--------- slirp/dhcpv6.c | 2 +- slirp/if.c | 2 +- slirp/ip6_icmp.c | 15 +++++++++------ slirp/ip_icmp.c | 2 +- slirp/libslirp.h | 16 ++++++++-------- slirp/misc.c | 2 +- slirp/slirp.c | 13 ++++++------- slirp/socket.c | 2 +- slirp/tcp_subr.c | 8 ++++---- slirp/udp.c | 2 +- 11 files changed, 49 insertions(+), 40 deletions(-) diff --git a/net/slirp.c b/net/slirp.c index a85e42ff43..7a16d8d615 100644 --- a/net/slirp.c +++ b/net/slirp.c @@ -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) diff --git a/slirp/dhcpv6.c b/slirp/dhcpv6.c index 9ffba38e8f..e655c7d5b1 100644 --- a/slirp/dhcpv6.c +++ b/slirp/dhcpv6.c @@ -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; } diff --git a/slirp/if.c b/slirp/if.c index 2ad03b8a79..1830cc396c 100644 --- a/slirp/if.c +++ b/slirp/if.c @@ -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; diff --git a/slirp/ip6_icmp.c b/slirp/ip6_icmp.c index 2a432ebbd4..c1e3d30470 100644 --- a/slirp/ip6_icmp.c +++ b/slirp/ip6_icmp.c @@ -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; } } diff --git a/slirp/ip_icmp.c b/slirp/ip_icmp.c index 7c5cb75ae5..ce79c0b051 100644 --- a/slirp/ip_icmp.c +++ b/slirp/ip_icmp.c @@ -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); } diff --git a/slirp/libslirp.h b/slirp/libslirp.h index b5c1b2122b..9b13d8250c 100644 --- a/slirp/libslirp.h +++ b/slirp/libslirp.h @@ -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; diff --git a/slirp/misc.c b/slirp/misc.c index 3c02510101..3f4cd852f8 100644 --- a/slirp/misc.c +++ b/slirp/misc.c @@ -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; } diff --git a/slirp/slirp.c b/slirp/slirp.c index eedbce23dc..a746c6fd86 100644 --- a/slirp/slirp.c +++ b/slirp/slirp.c @@ -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; diff --git a/slirp/socket.c b/slirp/socket.c index c896fa6da3..ce1d6ffa1d 100644 --- a/slirp/socket.c +++ b/slirp/socket.c @@ -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); } } diff --git a/slirp/tcp_subr.c b/slirp/tcp_subr.c index e35628a892..cda94815f6 100644 --- a/slirp/tcp_subr.c +++ b/slirp/tcp_subr.c @@ -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 */ diff --git a/slirp/udp.c b/slirp/udp.c index 5baa604b33..29a31e9400 100644 --- a/slirp/udp.c +++ b/slirp/udp.c @@ -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); }