diff mbox series

[PULLv3,31/32] slirp: pass opaque to all callbacks

Message ID 20190205182848.29887-32-samuel.thibault@ens-lyon.org (mailing list archive)
State New, archived
Headers show
Series [PULLv3,01/32] slirp: Avoid unaligned 16bit memory access | expand

Commit Message

Samuel Thibault Feb. 5, 2019, 6:28 p.m. UTC
From: Marc-André Lureau <marcandre.lureau@redhat.com>

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>
---
 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 mbox series

Patch

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 04886d05fd..93b2cd0141 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);
 }