Skip to contentSkip to conversation

slirp: make it again a standalone project

PATCH Applied Marc-André Lureau 4 participants 57 messages Nov 21, 2018 – Dec 6, 2018

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v2 cover slirp: make it again a standalone project

Hi,

Based-on: https://people.debian.org/~sthibault/qemu.git/ slirp branch

This series goal is to allow building libslirp as an independent library.

While looking at making SLIRP a seperate running process, I thought that having an independent library from QEMU would be a first step.

There has been some attempts to make slirp a seperate project in the past.
(https://lists.gnu.org/archive/html/qemu-devel/2017-02/msg01092.html)
Unfortunately, they forked from QEMU and didn't provide enough compatibility for QEMU to make use of it (in particular, vmstate handling was removed, they lost git history etc). Furthermore, they are not maintained as far as I can see.

I would propose to make slirp a seperate project, that can initially be used by QEMU as a submodule, keeping Makefile.objs until a proper shared library with stability guarantees etc is ready..

The subproject could created by preserving git tags, and cleaning up the code style, this way:

git filter-branch --tree-filter "if ls * 1> /dev/null 2>&1; then clang-format -i * /dev/null; fi " -f --subdirectory-filter "slirp" --prune-empty --tag-name-filter cat -- --all (my clang-format https://gist.github.com/elmarco/cb20c8d92007df0e2fb8a2404678ac73)

v2:
- rebased on top of maintainer tree (thanks for picking patches,
and reducing the burden of long series!)
- added "slirp: drop <Vista compatibility" (although it is a bit problematic)
- follow a suggestion from Paolo, and refactor the exec_list/guestfwd
- added "slirp: use virtual time for packet expiration", following Paolo suggestion
- drop the "scale" argument from the timer API
- changed the build-sys to group objects in slirp.mo, to allow slirp-cflags, among other benefits
- move to glib logging, set G_LOG_DOMAIN, add SLIRP_DEBUG environment variable
- document the added callbacks, as requested by Stefan H.
- a few more cleanups

Marc-André Lureau (30):
fixup! net: do not depend on slirp internals
slirp: drop <Vista compatibility
glib-compat: add g_spawn_async_with_fds() fallback
slirp: simplify fork_exec()
slirp: rename exec_list
slirp: factor out guestfwd addition checks
slirp: generalize guestfwd with a callback based approach net/slirp: simplify checking for cmd: prefix net/slirp: fix a few memory leaks
slirp: use virtual time for packet expiration
slirp: add clock_get_ns() callback
slirp: add callbacks for timer
build-sys: use a seperate slirp-obj-y && slirp.mo
slirp: set G_LOG_DOMAIN
slirp: replace error_report() and a fprintf with g_critical()
slirp: replace some fprintf() with DEBUG_MISC
slirp: replace a DEBUG block with WITH_ICMP_ERROR_MSG
slirp: no need to make DPRINTF conditional on DEBUG
slirp: always build with debug statements
slirp: introduce SLIRP_DEBUG environment variable
slirp: use %p for pointers format
slirp: remove remaining DEBUG blocks
slirp: replace DEBUG_ARGS with DEBUG_ARG
slirp: call into g_debug() for DEBUG macros
slirp: replace trace functions with DEBUG calls
slirp: add a set_nonblock() callback
slirp: replace qemu_notify_event() with a callback
slirp: move QEMU state saving to a separate unit
slirp: replace remaining QEMU dependency
build-sys: add a basic meson build

 include/glib-compat.h |  56 +++++
 net/colo.h            |   1 +
 net/util.h            |  26 +++
 slirp/debug.h         |  45 ++--
 slirp/ip.h            |  14 +-
 slirp/ip6.h           |   5 +-
 slirp/ip6_icmp.h      |  16 +-
 slirp/libslirp.h      |  38 +++-
 slirp/misc.h          |  19 +-
 slirp/qtailq.h        | 218 +++++++++++++++++++
 slirp/sbuf.h          |   2 +-
 slirp/slirp.h         |  19 +-
 slirp/socket.h        |   5 +-
 slirp/state.h         |   9 +
 slirp/util.h          | 148 +++++++++++++
 net/slirp.c           |  94 +++++---
 slirp/arp_table.c     |  13 +-
 slirp/bootp.c         |   8 +-
 slirp/cksum.c         |   9 +-
 slirp/dhcpv6.c        |   8 +-
 slirp/dnssearch.c     |   1 -
 slirp/if.c            |   4 +-
 slirp/ip6_icmp.c      |  34 ++-
 slirp/ip6_input.c     |   3 +-
 slirp/ip6_output.c    |   6 +-
 slirp/ip_icmp.c       |  30 +--
 slirp/ip_input.c      |   1 -
 slirp/ip_output.c     |   1 -
 slirp/mbuf.c          |   1 -
 slirp/misc.c          | 132 ++++++------
 slirp/ncsi.c          |   1 -
 slirp/ndp_table.c     |  34 ++-
 slirp/sbuf.c          |   8 +-
 slirp/slirp.c         | 490 ++++++++----------------------------------
 slirp/socket.c        |  51 +++--
 slirp/state.c         | 394 +++++++++++++++++++++++++++++++++
 slirp/tcp_input.c     |  26 ++-
 slirp/tcp_output.c    |   3 +-
 slirp/tcp_subr.c      |  27 ++-
 slirp/tcp_timer.c     |   1 -
 slirp/tftp.c          |  14 +-
 slirp/udp.c           |  12 +-
 slirp/udp6.c          |  13 +-
 slirp/util.c          | 176 +++++++++++++++
 Makefile.objs         |   4 +-
 Makefile.target       |   5 +-
 slirp/Makefile.objs   |  39 +++-
 slirp/meson.build     |  48 +++++
 slirp/trace-events    |   5 -
 49 files changed, 1553 insertions(+), 764 deletions(-)
 create mode 100644 slirp/qtailq.h
 create mode 100644 slirp/state.h
 create mode 100644 slirp/util.h
 create mode 100644 slirp/state.c
 create mode 100644 slirp/util.c
 create mode 100644 slirp/meson.build
 delete mode 100644 slirp/trace-events
Signature · 1 line
2.20.0.rc1
#1 lore raw

PATCH v2 1/30 fixup! net: do not depend on slirp internals

Somehow, I broke the build and failed to detect it. Please squash!

---
 net/colo.h |  1 +
 net/util.h | 26 ++++++++++++++++++++++++++
 2 files changed, 27 insertions(+)
net/colo.h +1 -0
index 420d916af2..b2f60e4d56 100644
@@ -18,6 +18,7 @@
 #include "qemu/jhash.h"
 #include "qemu/timer.h"
 #include "net/eth.h"
+#include "util.h"
 
 #define HASHTABLE_MAX_SIZE 16384
 
net/util.h +26 -0
index 358185fd50..0e0748913a 100644
@@ -25,6 +25,9 @@
 #ifndef QEMU_NET_UTIL_H
 #define QEMU_NET_UTIL_H
 
+#include "qemu/osdep.h"
+
+typedef uint32_t tcp_seq;
 
 /*
  * Structure of an internet header, naked of options.
@@ -50,6 +53,29 @@ struct ip {
     struct in_addr ip_src, ip_dst;  /* source and dest address */
 } QEMU_PACKED;
 
+/*
+ * TCP header.
+ * Per RFC 793, September, 1981.
+ */
+#define tcphdr slirp_tcphdr
+struct tcphdr {
+    uint16_t th_sport;      /* source port */
+    uint16_t th_dport;      /* destination port */
+    tcp_seq th_seq;         /* sequence number */
+    tcp_seq th_ack;         /* acknowledgement number */
+#ifdef HOST_WORDS_BIGENDIAN
+    uint8_t th_off:4,       /* data offset */
+            th_x2:4;        /* (unused) */
+#else
+    uint8_t th_x2:4,        /* (unused) */
+            th_off:4;       /* data offset */
+#endif
+    uint8_t th_flags;
+    uint16_t th_win;        /* window */
+    uint16_t th_sum;        /* checksum */
+    uint16_t th_urp;        /* urgent pointer */
+};
+
 static inline bool in6_equal_net(const struct in6_addr *a,
                                  const struct in6_addr *b,
                                  int prefix_len)
Signature · 1 line
2.20.0.rc1
#2 lore raw

Re: [Qemu-devel] [PATCH v2 for-3.2 v2 01/30] fixup! net: do not depend on slirp internals

Marc-André Lureau, le jeu. 22 nov. 2018 02:06:18 +0400, a ecrit:

net/util.h line 30

Quoted 5 lines

Somehow, I broke the build and failed to detect it. Please squash!

net/util.h +3 -0
+#include "qemu/osdep.h"
+
+typedef uint32_t tcp_seq;

I preferred to just use uint32_t instead of tcp_seq in the couple of places where this showed up.

Quoted 19 lines

/*
* Structure of an internet header, naked of options.

net/util.h +13 -0
@@ -50,6 +53,29 @@ struct ip {
     struct in_addr ip_src, ip_dst;  /* source and dest address */
 } QEMU_PACKED;
 
+/*
+ * TCP header.
+ * Per RFC 793, September, 1981.
+ */
+#define tcphdr slirp_tcphdr
+struct tcphdr {
+    uint16_t th_sport;      /* source port */
+    uint16_t th_dport;      /* destination port */
+    tcp_seq th_seq;         /* sequence number */
+    tcp_seq th_ack;         /* acknowledgement number */
+#ifdef HOST_WORDS_BIGENDIAN
+    uint8_t th_off:4,       /* data offset */
+            th_x2:4;        /* (unused) */

net/util.h struct ip { · line 78

Quoted 10 lines
net/util.h +10 -0
+#else
+    uint8_t th_x2:4,        /* (unused) */
+            th_off:4;       /* data offset */
+#endif
+    uint8_t th_flags;
+    uint16_t th_win;        /* window */
+    uint16_t th_sum;        /* checksum */
+    uint16_t th_urp;        /* urgent pointer */
+};
+

We already have struct tcp_hdr, in include/net/eth.h, so I used that instead.

Too bad I have worked on that just a few minutes ago :)

Samuel

#3 lore raw

Re: [Qemu-devel] [PATCH v2 for-3.2 v2 01/30] fixup! net: do not depend on slirp internals

Hi

Quoted 44 lines · Samuel Thibault

Marc-André Lureau, le jeu. 22 nov. 2018 02:06:18 +0400, a ecrit:

net/util.h line 30

Quoted 5 lines

Somehow, I broke the build and failed to detect it. Please squash!

net/util.h +3 -0
+#include "qemu/osdep.h"
+
+typedef uint32_t tcp_seq;

I preferred to just use uint32_t instead of tcp_seq in the couple of places where this showed up.

Quoted 19 lines

/*
* Structure of an internet header, naked of options.

net/util.h +13 -0
@@ -50,6 +53,29 @@ struct ip {
     struct in_addr ip_src, ip_dst;  /* source and dest address */
 } QEMU_PACKED;
 
+/*
+ * TCP header.
+ * Per RFC 793, September, 1981.
+ */
+#define tcphdr slirp_tcphdr
+struct tcphdr {
+    uint16_t th_sport;      /* source port */
+    uint16_t th_dport;      /* destination port */
+    tcp_seq th_seq;         /* sequence number */
+    tcp_seq th_ack;         /* acknowledgement number */
+#ifdef HOST_WORDS_BIGENDIAN
+    uint8_t th_off:4,       /* data offset */
+            th_x2:4;        /* (unused) */

net/util.h struct ip { · line 78

Quoted 10 lines
net/util.h +10 -0
+#else
+    uint8_t th_x2:4,        /* (unused) */
+            th_off:4;       /* data offset */
+#endif
+    uint8_t th_flags;
+    uint16_t th_win;        /* window */
+    uint16_t th_sum;        /* checksum */
+    uint16_t th_urp;        /* urgent pointer */
+};
+

We already have struct tcp_hdr, in include/net/eth.h, so I used that instead.

Quoted 1 line

Too bad I have worked on that just a few minutes ago :)

Great, thank you! and I prefer your solution :)

Signature · 1 line
Marc-André Lureau
#4 lore raw

PATCH v2 2/30 slirp: drop <Vista compatibility

In reply to Marc-André Lureau

Since commit 12f8def0e02232d7c6416ad9b66640f973c531d1 (v2.9), qemu requires Vista. Let's remove some conditional code.

Note that this introduces a missing declaration warning with mingw.
warning: implicit declaration of function 'inet_ntop'

See also: https://sourceforge.net/p/mingw-w64/mailman/message/36473782/

We could workaround it by declaring it ourself depending on __MINGW64_VERSION_*:
WINSOCK_API_LINKAGE INT WSAAPI inet_pton(int Family, PCTSTR pszAddrString, PVOID pAddrBuf);

---
 net/slirp.c       | 22 ----------------------
 slirp/ip6_icmp.c  | 10 ++++------
 slirp/ndp_table.c | 14 ++++++--------
 3 files changed, 10 insertions(+), 36 deletions(-)
net/slirp.c +0 -22
index 30eda2cca1..4d890287ba 100644
@@ -290,17 +290,6 @@ static int net_slirp_init(NetClientState *peer, const char *model,
     }
 #endif
 
-#if defined(_WIN32) && (_WIN32_WINNT < 0x0600)
-    /* No inet_pton helper before Vista... */
-    if (vprefix6) {
-        /* Unsupported */
-        error_setg(errp, "IPv6 prefix not supported");
-        return -1;
-    }
-    memset(&ip6_prefix, 0, sizeof(ip6_prefix));
-    ip6_prefix.s6_addr[0] = 0xfe;
-    ip6_prefix.s6_addr[1] = 0xc0;
-#else
     if (!vprefix6) {
         vprefix6 = "fec0::";
     }
@@ -308,7 +297,6 @@ static int net_slirp_init(NetClientState *peer, const char *model,
         error_setg(errp, "Failed to parse IPv6 prefix");
         return -1;
     }
-#endif
 
     if (!vprefix6_len) {
         vprefix6_len = 64;
@@ -320,10 +308,6 @@ static int net_slirp_init(NetClientState *peer, const char *model,
     }
 
     if (vhost6) {
-#if defined(_WIN32) && (_WIN32_WINNT < 0x0600)
-        error_setg(errp, "IPv6 host not supported");
-        return -1;
-#else
         if (!inet_pton(AF_INET6, vhost6, &ip6_host)) {
             error_setg(errp, "Failed to parse IPv6 host");
             return -1;
@@ -332,17 +316,12 @@ static int net_slirp_init(NetClientState *peer, const char *model,
             error_setg(errp, "IPv6 Host doesn't belong to network");
             return -1;
         }
-#endif
     } else {
         ip6_host = ip6_prefix;
         ip6_host.s6_addr[15] |= 2;
     }
 
     if (vnameserver6) {
-#if defined(_WIN32) && (_WIN32_WINNT < 0x0600)
-        error_setg(errp, "IPv6 DNS not supported");
-        return -1;
-#else
         if (!inet_pton(AF_INET6, vnameserver6, &ip6_dns)) {
             error_setg(errp, "Failed to parse IPv6 DNS");
             return -1;
@@ -351,7 +330,6 @@ static int net_slirp_init(NetClientState *peer, const char *model,
             error_setg(errp, "IPv6 DNS doesn't belong to network");
             return -1;
         }
-#endif
     } else {
         ip6_dns = ip6_prefix;
         ip6_dns.s6_addr[15] |= 3;
slirp/ip6_icmp.c +4 -6
index 3f74d172f4..595a62c8d4 100644
@@ -74,6 +74,7 @@ void icmp6_send_error(struct mbuf *m, uint8_t type, uint8_t code)
     Slirp *slirp = m->slirp;
     struct mbuf *t;
     struct ip6 *ip = mtod(m, struct ip6 *);
+    char addrstr[INET6_ADDRSTRLEN];
 
     DEBUG_CALL("icmp6_send_error");
     DEBUG_ARGS(" type = %d, code = %d\n", type, code);
@@ -90,11 +91,8 @@ void icmp6_send_error(struct mbuf *m, uint8_t type, uint8_t code)
     struct ip6 *rip = mtod(t, struct ip6 *);
     rip->ip_src = (struct in6_addr)LINKLOCAL_ADDR;
     rip->ip_dst = ip->ip_src;
-#if !defined(_WIN32) || (_WIN32_WINNT >= 0x0600)
-    char addrstr[INET6_ADDRSTRLEN];
     inet_ntop(AF_INET6, &rip->ip_dst, addrstr, INET6_ADDRSTRLEN);
     DEBUG_ARG("target = %s", addrstr);
-#endif
 
     rip->ip_nh = IPPROTO_ICMPV6;
     const int error_data_len = MIN(m->m_len,
@@ -222,12 +220,12 @@ void ndp_send_ra(Slirp *slirp)
  */
 void ndp_send_ns(Slirp *slirp, struct in6_addr addr)
 {
-    DEBUG_CALL("ndp_send_ns");
-#if !defined(_WIN32) || (_WIN32_WINNT >= 0x0600)
     char addrstr[INET6_ADDRSTRLEN];
+
     inet_ntop(AF_INET6, &addr, addrstr, INET6_ADDRSTRLEN);
+
+    DEBUG_CALL("ndp_send_ns");
     DEBUG_ARG("target = %s", addrstr);
-#endif
 
     /* Build IPv6 packet */
     struct mbuf *t = m_get(slirp);
slirp/ndp_table.c +6 -8
index a4e6421fd3..1401e1b322 100644
@@ -10,15 +10,14 @@
 void ndp_table_add(Slirp *slirp, struct in6_addr ip_addr,
                     uint8_t ethaddr[ETH_ALEN])
 {
+    char addrstr[INET6_ADDRSTRLEN];
     NdpTable *ndp_table = &slirp->ndp_table;
     int i;
 
-    DEBUG_CALL("ndp_table_add");
-#if !defined(_WIN32) || (_WIN32_WINNT >= 0x0600)
-    char addrstr[INET6_ADDRSTRLEN];
     inet_ntop(AF_INET6, &(ip_addr), addrstr, INET6_ADDRSTRLEN);
+
+    DEBUG_CALL("ndp_table_add");
     DEBUG_ARG("ip = %s", addrstr);
-#endif
     DEBUG_ARGS(" hw addr = %02x:%02x:%02x:%02x:%02x:%02x\n",
                ethaddr[0], ethaddr[1], ethaddr[2],
                ethaddr[3], ethaddr[4], ethaddr[5]);
@@ -50,15 +49,14 @@ void ndp_table_add(Slirp *slirp, struct in6_addr ip_addr,
 bool ndp_table_search(Slirp *slirp, struct in6_addr ip_addr,
                       uint8_t out_ethaddr[ETH_ALEN])
 {
+    char addrstr[INET6_ADDRSTRLEN];
     NdpTable *ndp_table = &slirp->ndp_table;
     int i;
 
-    DEBUG_CALL("ndp_table_search");
-#if !defined(_WIN32) || (_WIN32_WINNT >= 0x0600)
-    char addrstr[INET6_ADDRSTRLEN];
     inet_ntop(AF_INET6, &(ip_addr), addrstr, INET6_ADDRSTRLEN);
+
+    DEBUG_CALL("ndp_table_search");
     DEBUG_ARG("ip = %s", addrstr);
-#endif
 
     assert(!in6_zero(&ip_addr));
 
Signature · 1 line
2.20.0.rc1
#5 lore raw

Re: [Qemu-devel] [PATCH v2 for-3.2 v2 02/30] slirp: drop <Vista compatibility

Marc-André Lureau, le jeu. 22 nov. 2018 02:06:19 +0400, a ecrit:

Quoted 6 lines

Since commit 12f8def0e02232d7c6416ad9b66640f973c531d1 (v2.9), qemu requires Vista. Let's remove some conditional code.

Note that this introduces a missing declaration warning with mingw.
warning: implicit declaration of function 'inet_ntop'

Quoted 6 lines

See also: https://sourceforge.net/p/mingw-w64/mailman/message/36473782/

We could workaround it by declaring it ourself depending on __MINGW64_VERSION_*:
WINSOCK_API_LINKAGE INT WSAAPI inet_pton(int Family, PCTSTR pszAddrString, PVOID pAddrBuf);

Applied to my tree, thanks!

#6 lore raw

Re: [Qemu-devel] [PATCH v2 for-3.2 v2 02/30] slirp: drop <Vista compatibility

In reply to Marc-André Lureau

Hi

Quoted 11 lines · Marc-André Lureau

Since commit 12f8def0e02232d7c6416ad9b66640f973c531d1 (v2.9), qemu requires Vista. Let's remove some conditional code.

Note that this introduces a missing declaration warning with mingw.
warning: implicit declaration of function 'inet_ntop'

See also: https://sourceforge.net/p/mingw-w64/mailman/message/36473782/

We could workaround it by declaring it ourself depending on __MINGW64_VERSION_*:
WINSOCK_API_LINKAGE INT WSAAPI inet_pton(int Family, PCTSTR pszAddrString, PVOID pAddrBuf);

It turns out the warning is because _WIN32_WINNT is not defined to a recent enough version.
I am sending a tiny seperate win32 build-sys series, it should be applied before this patch, to avoid the temporary regression. And you can drop this Note from the commit message.

thanks

Quoted 161 lines
---
 net/slirp.c       | 22 ----------------------
 slirp/ip6_icmp.c  | 10 ++++------
 slirp/ndp_table.c | 14 ++++++--------
 3 files changed, 10 insertions(+), 36 deletions(-)
net/slirp.c +0 -22
index 30eda2cca1..4d890287ba 100644
@@ -290,17 +290,6 @@ static int net_slirp_init(NetClientState *peer, const char *model,
     }
 #endif
 
-#if defined(_WIN32) && (_WIN32_WINNT < 0x0600)
-    /* No inet_pton helper before Vista... */
-    if (vprefix6) {
-        /* Unsupported */
-        error_setg(errp, "IPv6 prefix not supported");
-        return -1;
-    }
-    memset(&ip6_prefix, 0, sizeof(ip6_prefix));
-    ip6_prefix.s6_addr[0] = 0xfe;
-    ip6_prefix.s6_addr[1] = 0xc0;
-#else
     if (!vprefix6) {
         vprefix6 = "fec0::";
     }
@@ -308,7 +297,6 @@ static int net_slirp_init(NetClientState *peer, const char *model,
         error_setg(errp, "Failed to parse IPv6 prefix");
         return -1;
     }
-#endif
 
     if (!vprefix6_len) {
         vprefix6_len = 64;
@@ -320,10 +308,6 @@ static int net_slirp_init(NetClientState *peer, const char *model,
     }
 
     if (vhost6) {
-#if defined(_WIN32) && (_WIN32_WINNT < 0x0600)
-        error_setg(errp, "IPv6 host not supported");
-        return -1;
-#else
         if (!inet_pton(AF_INET6, vhost6, &ip6_host)) {
             error_setg(errp, "Failed to parse IPv6 host");
             return -1;
@@ -332,17 +316,12 @@ static int net_slirp_init(NetClientState *peer, const char *model,
             error_setg(errp, "IPv6 Host doesn't belong to network");
             return -1;
         }
-#endif
     } else {
         ip6_host = ip6_prefix;
         ip6_host.s6_addr[15] |= 2;
     }
 
     if (vnameserver6) {
-#if defined(_WIN32) && (_WIN32_WINNT < 0x0600)
-        error_setg(errp, "IPv6 DNS not supported");
-        return -1;
-#else
         if (!inet_pton(AF_INET6, vnameserver6, &ip6_dns)) {
             error_setg(errp, "Failed to parse IPv6 DNS");
             return -1;
@@ -351,7 +330,6 @@ static int net_slirp_init(NetClientState *peer, const char *model,
             error_setg(errp, "IPv6 DNS doesn't belong to network");
             return -1;
         }
-#endif
     } else {
         ip6_dns = ip6_prefix;
         ip6_dns.s6_addr[15] |= 3;
slirp/ip6_icmp.c +4 -6
index 3f74d172f4..595a62c8d4 100644
@@ -74,6 +74,7 @@ void icmp6_send_error(struct mbuf *m, uint8_t type, uint8_t code)
     Slirp *slirp = m->slirp;
     struct mbuf *t;
     struct ip6 *ip = mtod(m, struct ip6 *);
+    char addrstr[INET6_ADDRSTRLEN];
 
     DEBUG_CALL("icmp6_send_error");
     DEBUG_ARGS(" type = %d, code = %d\n", type, code);
@@ -90,11 +91,8 @@ void icmp6_send_error(struct mbuf *m, uint8_t type, uint8_t code)
     struct ip6 *rip = mtod(t, struct ip6 *);
     rip->ip_src = (struct in6_addr)LINKLOCAL_ADDR;
     rip->ip_dst = ip->ip_src;
-#if !defined(_WIN32) || (_WIN32_WINNT >= 0x0600)
-    char addrstr[INET6_ADDRSTRLEN];
     inet_ntop(AF_INET6, &rip->ip_dst, addrstr, INET6_ADDRSTRLEN);
     DEBUG_ARG("target = %s", addrstr);
-#endif
 
     rip->ip_nh = IPPROTO_ICMPV6;
     const int error_data_len = MIN(m->m_len,
@@ -222,12 +220,12 @@ void ndp_send_ra(Slirp *slirp)
  */
 void ndp_send_ns(Slirp *slirp, struct in6_addr addr)
 {
-    DEBUG_CALL("ndp_send_ns");
-#if !defined(_WIN32) || (_WIN32_WINNT >= 0x0600)
     char addrstr[INET6_ADDRSTRLEN];
+
     inet_ntop(AF_INET6, &addr, addrstr, INET6_ADDRSTRLEN);
+
+    DEBUG_CALL("ndp_send_ns");
     DEBUG_ARG("target = %s", addrstr);
-#endif
 
     /* Build IPv6 packet */
     struct mbuf *t = m_get(slirp);
slirp/ndp_table.c +6 -9
index a4e6421fd3..1401e1b322 100644
@@ -10,15 +10,14 @@
 void ndp_table_add(Slirp *slirp, struct in6_addr ip_addr,
                     uint8_t ethaddr[ETH_ALEN])
 {
+    char addrstr[INET6_ADDRSTRLEN];
     NdpTable *ndp_table = &slirp->ndp_table;
     int i;
 
-    DEBUG_CALL("ndp_table_add");
-#if !defined(_WIN32) || (_WIN32_WINNT >= 0x0600)
-    char addrstr[INET6_ADDRSTRLEN];
     inet_ntop(AF_INET6, &(ip_addr), addrstr, INET6_ADDRSTRLEN);
+
+    DEBUG_CALL("ndp_table_add");
     DEBUG_ARG("ip = %s", addrstr);
-#endif
     DEBUG_ARGS(" hw addr = %02x:%02x:%02x:%02x:%02x:%02x\n",
                ethaddr[0], ethaddr[1], ethaddr[2],
                ethaddr[3], ethaddr[4], ethaddr[5]);
@@ -50,15 +49,14 @@ void ndp_table_add(Slirp *slirp, struct in6_addr ip_addr,
 bool ndp_table_search(Slirp *slirp, struct in6_addr ip_addr,
                       uint8_t out_ethaddr[ETH_ALEN])
 {
+    char addrstr[INET6_ADDRSTRLEN];
     NdpTable *ndp_table = &slirp->ndp_table;
     int i;
 
-    DEBUG_CALL("ndp_table_search");
-#if !defined(_WIN32) || (_WIN32_WINNT >= 0x0600)
-    char addrstr[INET6_ADDRSTRLEN];
     inet_ntop(AF_INET6, &(ip_addr), addrstr, INET6_ADDRSTRLEN);
+
+    DEBUG_CALL("ndp_table_search");
     DEBUG_ARG("ip = %s", addrstr);
-#endif
 
     assert(!in6_zero(&ip_addr));
 
--

2.20.0.rc1

Signature · 1 line
Marc-André Lureau
#7 lore raw

PATCH v2 3/30 glib-compat: add g_spawn_async_with_fds() fallback

In reply to Marc-André Lureau
---
 include/glib-compat.h | 56 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 56 insertions(+)
include/glib-compat.h +56 -0
index fdf95a255d..8a078c5288 100644
@@ -83,6 +83,62 @@ static inline gboolean g_strv_contains_qemu(const gchar *const *strv,
 }
 #define g_strv_contains(a, b) g_strv_contains_qemu(a, b)
 
+#if !GLIB_CHECK_VERSION(2, 58, 0)
+typedef struct QemuGSpawnFds {
+    GSpawnChildSetupFunc child_setup;
+    gpointer user_data;
+    gint stdin_fd;
+    gint stdout_fd;
+    gint stderr_fd;
+} QemuGSpawnFds;
+
+static inline void
+qemu_gspawn_fds_setup(gpointer user_data)
+{
+    QemuGSpawnFds *q = (QemuGSpawnFds *)user_data;
+
+    dup2(q->stdin_fd, 0);
+    dup2(q->stdout_fd, 1);
+    dup2(q->stderr_fd, 2);
+    q->child_setup(q->user_data);
+}
+#endif
+
+static inline gboolean
+g_spawn_async_with_fds_qemu(const gchar *working_directory,
+                            gchar **argv,
+                            gchar **envp,
+                            GSpawnFlags flags,
+                            GSpawnChildSetupFunc child_setup,
+                            gpointer user_data,
+                            GPid *child_pid,
+                            gint stdin_fd,
+                            gint stdout_fd,
+                            gint stderr_fd,
+                            GError **error)
+{
+#if GLIB_CHECK_VERSION(2, 58, 0)
+    return g_spawn_async_with_fds(working_directory, argv, envp, flags,
+                                  child_setup, user_data,
+                                  child_pid, stdin_fd, stdout_fd, stderr_fd,
+                                  error);
+#else
+    QemuGSpawnFds setup = {
+        .child_setup = child_setup,
+        .user_data = user_data,
+        .stdin_fd = stdin_fd,
+        .stdout_fd = stdout_fd,
+        .stderr_fd = stderr_fd,
+    };
+
+    return g_spawn_async(working_directory, argv, envp, flags,
+                         qemu_gspawn_fds_setup, &setup,
+                         child_pid, error);
+#endif
+}
+
+#define g_spawn_async_with_fds(wd, argv, env, f, c, d, p, ifd, ofd, efd, err) \
+    g_spawn_async_with_fds_qemu(wd, argv, env, f, c, d, p, ifd, ofd, efd, err)
 
 #if defined(_WIN32) && !GLIB_CHECK_VERSION(2, 50, 0)
 /*
Signature · 1 line
2.20.0.rc1
#9 lore raw

PATCH v2 4/30 slirp: simplify fork_exec()

In reply to Marc-André Lureau

Use g_spawn_async_with_fds() to setup the child.

GSpawn handles reaping the child, and closing parent file descriptors.

---
 slirp/misc.c | 75 +++++++++++++++++++++++++---------------------------
 1 file changed, 36 insertions(+), 39 deletions(-)
slirp/misc.c +36 -39
index ee3492a2ae..753b3da25b 100644
@@ -129,56 +129,53 @@ err:
     return -1;
 }
 
+static void
+fork_exec_child_setup(gpointer data)
+{
+    setsid();
+}
+
 int
 fork_exec(struct socket *so, const char *ex)
 {
-	char **argv;
-	int opt, c, sp[2];
-	pid_t pid;
+    GError *err = NULL;
+    char **argv;
+    int opt, sp[2];
 
-	DEBUG_CALL("fork_exec");
-	DEBUG_ARG("so = %p", so);
-	DEBUG_ARG("ex = %p", ex);
+    DEBUG_CALL("fork_exec");
+    DEBUG_ARG("so = %p", so);
+    DEBUG_ARG("ex = %p", ex);
 
     if (slirp_socketpair_with_oob(sp) < 0) {
         return 0;
     }
 
-	pid = fork();
-	switch(pid) {
-	 case -1:
-		error_report("Error: fork failed: %s", strerror(errno));
-		closesocket(sp[0]);
-		closesocket(sp[1]);
-		return 0;
-
-	 case 0:
-		setsid();
-		dup2(sp[1], 0);
-		dup2(sp[1], 1);
-		dup2(sp[1], 2);
-		for (c = getdtablesize() - 1; c >= 3; c--)
-		   close(c);
+    argv = g_strsplit(ex, " ", -1);
+    g_spawn_async_with_fds(NULL /* cwd */,
+                           argv,
+                           NULL /* env */,
+                           G_SPAWN_SEARCH_PATH,
+                           fork_exec_child_setup, NULL /* data */,
+                           NULL /* child_pid */,
+                           sp[1], sp[1], sp[1],
+                           &err);
+    g_strfreev(argv);
 
-                argv = g_strsplit(ex, " ", -1);
-		execvp(argv[0], (char **)argv);
-
-		/* Ooops, failed, let's tell the user why */
-        fprintf(stderr, "Error: execvp of %s failed: %s\n",
-                argv[0], strerror(errno));
-		close(0); close(1); close(2); /* XXX */
-		exit(1);
+    if (err) {
+        error_report("%s", err->message);
+        g_error_free(err);
+        closesocket(sp[0]);
+        closesocket(sp[1]);
+        return 0;
+    }
 
-	 default:
-		so->s = sp[0];
-		closesocket(sp[1]);
-		qemu_add_child_watch(pid);
-		socket_set_fast_reuse(so->s);
-		opt = 1;
-		qemu_setsockopt(so->s, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(int));
-		qemu_set_nonblock(so->s);
-		return 1;
-	}
+    so->s = sp[0];
+    closesocket(sp[1]);
+    socket_set_fast_reuse(so->s);
+    opt = 1;
+    qemu_setsockopt(so->s, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(int));
+    qemu_set_nonblock(so->s);
+    return 1;
 }
 #endif
 
Signature · 1 line
2.20.0.rc1
#10 lore raw

Re: [Qemu-devel] [PATCH v2 for-3.2 v2 04/30] slirp: simplify fork_exec()

Marc-André Lureau, le jeu. 22 nov. 2018 02:06:21 +0400, a ecrit:

Quoted 5 lines

Use g_spawn_async_with_fds() to setup the child.

GSpawn handles reaping the child, and closing parent file descriptors.

Ditto.

Samuel

#11 lore raw

PATCH v2 5/30 slirp: rename exec_list

In reply to Marc-André Lureau

This list is not only used to handle command to execute on guest connection, it can also redirect to an arbitrary object, such as a chardev. Let's rename the struct and the field to "guestfwd".

---
 slirp/misc.h      |  6 +++---
 slirp/slirp.h     |  2 +-
 slirp/misc.c      |  6 +++---
 slirp/slirp.c     | 18 +++++++++---------
 slirp/tcp_input.c |  6 +++---
 slirp/tcp_subr.c  |  4 ++--
 6 files changed, 21 insertions(+), 21 deletions(-)
slirp/misc.h +3 -3
index 0bc5e74bc5..1df707c052 100644
@@ -8,12 +8,12 @@
 #ifndef MISC_H
 #define MISC_H
 
-struct ex_list {
+struct gfwd_list {
 	void *ex_chardev;
 	struct in_addr ex_addr;		/* Server address */
 	int ex_fport;                   /* Port to telnet to */
 	char *ex_exec;                  /* Command line of what to exec */
-	struct ex_list *ex_next;
+	struct gfwd_list *ex_next;
 };
 
 #define EMU_NONE 0x0
@@ -51,7 +51,7 @@ struct slirp_quehead {
 
 void slirp_insque(void *, void *);
 void slirp_remque(void *);
-int add_exec(struct ex_list **, void *, const char *, struct in_addr, int);
+int add_exec(struct gfwd_list **, void *, const char *, struct in_addr, int);
 int fork_exec(struct socket *so, const char *ex);
 
 #endif
slirp/slirp.h +1 -1
index fdf397256b..9aa245715d 100644
@@ -147,7 +147,7 @@ struct Slirp {
     char client_hostname[33];
 
     int restricted;
-    struct ex_list *exec_list;
+    struct gfwd_list *guestfwd_list;
 
     /* mbuf states */
     struct quehead m_freelist;
slirp/misc.c +3 -3
index 753b3da25b..cf7cc3a787 100644
@@ -36,10 +36,10 @@ remque(void *a)
   element->qh_rlink = NULL;
 }
 
-int add_exec(struct ex_list **ex_ptr, void *chardev, const char *cmdline,
+int add_exec(struct gfwd_list **ex_ptr, void *chardev, const char *cmdline,
              struct in_addr addr, int port)
 {
-	struct ex_list *tmp_ptr;
+	struct gfwd_list *tmp_ptr;
 
 	/* First, check if the port is "bound" */
 	for (tmp_ptr = *ex_ptr; tmp_ptr; tmp_ptr = tmp_ptr->ex_next) {
@@ -49,7 +49,7 @@ int add_exec(struct ex_list **ex_ptr, void *chardev, const char *cmdline,
 	}
 
 	tmp_ptr = *ex_ptr;
-	*ex_ptr = g_new0(struct ex_list, 1);
+	*ex_ptr = g_new0(struct gfwd_list, 1);
 	(*ex_ptr)->ex_fport = port;
 	(*ex_ptr)->ex_addr = addr;
 	if (chardev) {
slirp/slirp.c +9 -9
index 8da528afaa..4949d17c73 100644
@@ -345,9 +345,9 @@ Slirp *slirp_init(int restricted, bool in_enabled, struct in_addr vnetwork,
 
 void slirp_cleanup(Slirp *slirp)
 {
-    struct ex_list *e, *next;
+    struct gfwd_list *e, *next;
 
-    for (e = slirp->exec_list; e; e = next) {
+    for (e = slirp->guestfwd_list; e; e = next) {
         next = e->ex_next;
         g_free(e->ex_exec);
         g_free(e);
@@ -760,7 +760,7 @@ static void arp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len)
     struct ethhdr *reh = (struct ethhdr *)arp_reply;
     struct slirp_arphdr *rah = (struct slirp_arphdr *)(arp_reply + ETH_HLEN);
     int ar_op;
-    struct ex_list *ex_ptr;
+    struct gfwd_list *ex_ptr;
 
     if (!slirp->in_enabled) {
         return;
@@ -780,7 +780,7 @@ static void arp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len)
             if (ah->ar_tip == slirp->vnameserver_addr.s_addr ||
                 ah->ar_tip == slirp->vhost_addr.s_addr)
                 goto arp_ok;
-            for (ex_ptr = slirp->exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
+            for (ex_ptr = slirp->guestfwd_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
                 if (ex_ptr->ex_addr.s_addr == ah->ar_tip)
                     goto arp_ok;
             }
@@ -1052,7 +1052,7 @@ int slirp_add_exec(Slirp *slirp, void *chardev, const char *cmdline,
         return -1;
     }
 
-    return add_exec(&slirp->exec_list, chardev, cmdline, *guest_addr,
+    return add_exec(&slirp->guestfwd_list, chardev, cmdline, *guest_addr,
                     htons(guest_port));
 }
 
@@ -1423,9 +1423,9 @@ static const VMStateDescription vmstate_slirp = {
 static void slirp_state_save(QEMUFile *f, void *opaque)
 {
     Slirp *slirp = opaque;
-    struct ex_list *ex_ptr;
+    struct gfwd_list *ex_ptr;
 
-    for (ex_ptr = slirp->exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next)
+    for (ex_ptr = slirp->guestfwd_list; ex_ptr; ex_ptr = ex_ptr->ex_next)
         if (ex_ptr->ex_chardev) {
             struct socket *so;
             so = slirp_find_ctl_socket(slirp, ex_ptr->ex_addr,
@@ -1445,7 +1445,7 @@ static void slirp_state_save(QEMUFile *f, void *opaque)
 static int slirp_state_load(QEMUFile *f, void *opaque, int version_id)
 {
     Slirp *slirp = opaque;
-    struct ex_list *ex_ptr;
+    struct gfwd_list *ex_ptr;
 
     while (qemu_get_byte(f)) {
         int ret;
@@ -1460,7 +1460,7 @@ static int slirp_state_load(QEMUFile *f, void *opaque, int version_id)
             slirp->vnetwork_addr.s_addr) {
             return -EINVAL;
         }
-        for (ex_ptr = slirp->exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
+        for (ex_ptr = slirp->guestfwd_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
             if (ex_ptr->ex_chardev &&
                 so->so_faddr.s_addr == ex_ptr->ex_addr.s_addr &&
                 so->so_fport == ex_ptr->ex_fport) {
slirp/tcp_input.c +3 -3
index b67ec24075..bc4aa60046 100644
@@ -211,7 +211,7 @@ tcp_input(struct mbuf *m, int iphlen, struct socket *inso, unsigned short af)
 	struct sockaddr_storage lhost, fhost;
 	struct sockaddr_in *lhost4, *fhost4;
 	struct sockaddr_in6 *lhost6, *fhost6;
-    struct ex_list *ex_ptr;
+    struct gfwd_list *ex_ptr;
     Slirp *slirp;
 
 	DEBUG_CALL("tcp_input");
@@ -394,7 +394,7 @@ findso:
              * for non-hostfwd connections. These should be dropped, unless it
              * happens to be a guestfwd.
              */
-            for (ex_ptr = slirp->exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
+            for (ex_ptr = slirp->guestfwd_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
                 if (ex_ptr->ex_fport == ti->ti_dport &&
                     ti->ti_dst.s_addr == ex_ptr->ex_addr.s_addr) {
                     break;
@@ -616,7 +616,7 @@ findso:
 	    if (so->so_faddr.s_addr != slirp->vhost_addr.s_addr &&
 		so->so_faddr.s_addr != slirp->vnameserver_addr.s_addr) {
 		/* May be an add exec */
-		for (ex_ptr = slirp->exec_list; ex_ptr;
+		for (ex_ptr = slirp->guestfwd_list; ex_ptr;
 		     ex_ptr = ex_ptr->ex_next) {
 		  if(ex_ptr->ex_fport == so->so_fport &&
 		     so->so_faddr.s_addr == ex_ptr->ex_addr.s_addr) {
slirp/tcp_subr.c +2 -2
index 00cee73163..3b14684107 100644
@@ -949,14 +949,14 @@ int tcp_ctl(struct socket *so)
 {
     Slirp *slirp = so->slirp;
     struct sbuf *sb = &so->so_snd;
-    struct ex_list *ex_ptr;
+    struct gfwd_list *ex_ptr;
 
     DEBUG_CALL("tcp_ctl");
     DEBUG_ARG("so = %p", so);
 
     if (so->so_faddr.s_addr != slirp->vhost_addr.s_addr) {
         /* Check if it's pty_exec */
-        for (ex_ptr = slirp->exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
+        for (ex_ptr = slirp->guestfwd_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
             if (ex_ptr->ex_fport == so->so_fport &&
                 so->so_faddr.s_addr == ex_ptr->ex_addr.s_addr) {
                 if (ex_ptr->ex_chardev) {
Signature · 1 line
2.20.0.rc1
#12 lore raw

Re: [Qemu-devel] [PATCH v2 for-3.2 v2 05/30] slirp: rename exec_list

Marc-André Lureau, le jeu. 22 nov. 2018 02:06:22 +0400, a ecrit:

Quoted 5 lines

This list is not only used to handle command to execute on guest connection, it can also redirect to an arbitrary object, such as a chardev. Let's rename the struct and the field to "guestfwd".

Applied to my tree, thanks!

#13 lore raw

PATCH v2 6/30 slirp: factor out guestfwd addition checks

In reply to Marc-André Lureau

This will allow reusing the function in a following patch.

---
 slirp/misc.c  |  7 -------
 slirp/slirp.c | 23 +++++++++++++++++++++--
 2 files changed, 21 insertions(+), 9 deletions(-)
slirp/misc.c +0 -7
index cf7cc3a787..03bfa31f20 100644
@@ -41,13 +41,6 @@ int add_exec(struct gfwd_list **ex_ptr, void *chardev, const char *cmdline,
 {
 	struct gfwd_list *tmp_ptr;
 
-	/* First, check if the port is "bound" */
-	for (tmp_ptr = *ex_ptr; tmp_ptr; tmp_ptr = tmp_ptr->ex_next) {
-		if (port == tmp_ptr->ex_fport &&
-		    addr.s_addr == tmp_ptr->ex_addr.s_addr)
-			return -1;
-	}
-
 	tmp_ptr = *ex_ptr;
 	*ex_ptr = g_new0(struct gfwd_list, 1);
 	(*ex_ptr)->ex_fport = port;
slirp/slirp.c +21 -2
index 4949d17c73..e7256ec05c 100644
@@ -1038,9 +1038,11 @@ int slirp_add_hostfwd(Slirp *slirp, int is_udp, struct in_addr host_addr,
     return 0;
 }
 
-int slirp_add_exec(Slirp *slirp, void *chardev, const char *cmdline,
-                   struct in_addr *guest_addr, int guest_port)
+static bool
+check_guestfwd(Slirp *slirp, struct in_addr *guest_addr, int guest_port)
 {
+    struct gfwd_list *tmp_ptr;
+
     if (!guest_addr->s_addr) {
         guest_addr->s_addr = slirp->vnetwork_addr.s_addr |
             (htonl(0x0204) & ~slirp->vnetwork_mask.s_addr);
@@ -1049,6 +1051,23 @@ int slirp_add_exec(Slirp *slirp, void *chardev, const char *cmdline,
         slirp->vnetwork_addr.s_addr ||
         guest_addr->s_addr == slirp->vhost_addr.s_addr ||
         guest_addr->s_addr == slirp->vnameserver_addr.s_addr) {
+        return false;
+    }
+
+    /* check if the port is "bound" */
+    for (tmp_ptr = slirp->guestfwd_list; tmp_ptr; tmp_ptr = tmp_ptr->ex_next) {
+        if (guest_port == tmp_ptr->ex_fport &&
+            guest_addr->s_addr == tmp_ptr->ex_addr.s_addr)
+            return false;
+    }
+
+    return true;
+}
+
+int slirp_add_exec(Slirp *slirp, void *chardev, const char *cmdline,
+                   struct in_addr *guest_addr, int guest_port)
+{
+    if (!check_guestfwd(slirp, guest_addr, guest_port)) {
         return -1;
     }
 
Signature · 1 line
2.20.0.rc1
#14 lore raw

Re: [Qemu-devel] [PATCH v2 for-3.2 v2 06/30] slirp: factor out guestfwd addition checks

Marc-André Lureau, le jeu. 22 nov. 2018 02:06:23 +0400, a ecrit:

Quoted 3 lines

This will allow reusing the function in a following patch.

Applied to my tree, thanks!

#15 lore raw

PATCH v2 7/30 slirp: generalize guestfwd with a callback based approach

In reply to Marc-André Lureau

Instead of calling into QEMU chardev directly, and mixing it with slirp_add_exec() handling, add a new function slirp_add_guestfwd() which takes a write callback.

---
 slirp/libslirp.h |  6 +++++-
 slirp/misc.h     | 15 +++++++++++++--
 slirp/socket.h   |  4 +++-
 net/slirp.c      | 14 ++++++++++----
 slirp/misc.c     | 37 +++++++++++++++++++++++--------------
 slirp/slirp.c    | 27 +++++++++++++++++++--------
 slirp/tcp_subr.c |  4 ++--
 7 files changed, 75 insertions(+), 32 deletions(-)
slirp/libslirp.h +5 -1
index 3e0aa19f4b..d7c2b1ae71 100644
@@ -5,6 +5,8 @@
 
 typedef struct Slirp Slirp;
 
+typedef int (*SlirpWriteCb)(const void *buf, size_t len, void *opaque);
+
 /*
  * Callbacks from slirp
  *
@@ -43,7 +45,9 @@ int slirp_add_hostfwd(Slirp *slirp, int is_udp,
                       struct in_addr guest_addr, int guest_port);
 int slirp_remove_hostfwd(Slirp *slirp, int is_udp,
                          struct in_addr host_addr, int host_port);
-int slirp_add_exec(Slirp *slirp, void *chardev, const char *cmdline,
+int slirp_add_exec(Slirp *slirp, const char *cmdline,
+                   struct in_addr *guest_addr, int guest_port);
+int slirp_add_guestfwd(Slirp *slirp, SlirpWriteCb write_cb, void *opaque,
                    struct in_addr *guest_addr, int guest_port);
 
 char *slirp_connection_info(Slirp *slirp);
slirp/misc.h +13 -2
index 1df707c052..c2ceadb591 100644
@@ -8,8 +8,11 @@
 #ifndef MISC_H
 #define MISC_H
 
+#include "libslirp.h"
+
 struct gfwd_list {
-	void *ex_chardev;
+	SlirpWriteCb write_cb;
+	void *opaque;
 	struct in_addr ex_addr;		/* Server address */
 	int ex_fport;                   /* Port to telnet to */
 	char *ex_exec;                  /* Command line of what to exec */
@@ -51,7 +54,15 @@ struct slirp_quehead {
 
 void slirp_insque(void *, void *);
 void slirp_remque(void *);
-int add_exec(struct gfwd_list **, void *, const char *, struct in_addr, int);
 int fork_exec(struct socket *so, const char *ex);
 
+struct gfwd_list *
+add_guestfwd(struct gfwd_list **ex_ptr,
+             SlirpWriteCb write_cb, void *opaque,
+             struct in_addr addr, int port);
+
+struct gfwd_list *
+add_exec(struct gfwd_list **ex_ptr, const char *cmdline,
+         struct in_addr addr, int port);
+
 #endif
slirp/socket.h +3 -1
index 930ed95972..fc35ca5f72 100644
@@ -8,6 +8,8 @@
 #ifndef SLIRP_SOCKET_H
 #define SLIRP_SOCKET_H
 
+#include "misc.h"
+
 #define SO_EXPIRE 240000
 #define SO_EXPIREFAST 10000
 
@@ -25,6 +27,7 @@ struct socket {
   struct socket *so_next,*so_prev;      /* For a linked list of sockets */
 
   int s;                           /* The actual socket */
+  struct gfwd_list *guestfwd;
 
   int pollfds_idx;                 /* GPollFD GArray index */
 
@@ -67,7 +70,6 @@ struct socket {
 
   struct sbuf so_rcv;		/* Receive buffer */
   struct sbuf so_snd;		/* Send buffer */
-  void * chardev;
 };
 
 
net/slirp.c +10 -4
index 4d890287ba..53adb92dd5 100644
@@ -698,8 +698,8 @@ static int slirp_smb(SlirpState* s, const char *exported_dir,
              CONFIG_SMBD_COMMAND, s->smb_dir, smb_conf);
     g_free(smb_conf);
 
-    if (slirp_add_exec(s->slirp, NULL, smb_cmdline, &vserver_addr, 139) < 0 ||
-        slirp_add_exec(s->slirp, NULL, smb_cmdline, &vserver_addr, 445) < 0) {
+    if (slirp_add_exec(s->slirp, smb_cmdline, &vserver_addr, 139) < 0 ||
+        slirp_add_exec(s->slirp, smb_cmdline, &vserver_addr, 445) < 0) {
         slirp_smb_cleanup(s);
         g_free(smb_cmdline);
         error_setg(errp, "Conflicting/invalid smbserver address");
@@ -730,6 +730,11 @@ static void guestfwd_read(void *opaque, const uint8_t *buf, int size)
     slirp_socket_recv(fwd->slirp, fwd->server, fwd->port, buf, size);
 }
 
+static int guestfwd_write(const void *buf, size_t len, void *chr)
+{
+    return qemu_chr_fe_write_all(chr, buf, len);
+}
+
 static int slirp_guestfwd(SlirpState *s, const char *config_str, Error **errp)
 {
     struct in_addr server = { .s_addr = 0 };
@@ -763,7 +768,7 @@ static int slirp_guestfwd(SlirpState *s, const char *config_str, Error **errp)
     snprintf(buf, sizeof(buf), "guestfwd.tcp.%d", port);
 
     if ((strlen(p) > 4) && !strncmp(p, "cmd:", 4)) {
-        if (slirp_add_exec(s->slirp, NULL, &p[4], &server, port) < 0) {
+        if (slirp_add_exec(s->slirp, &p[4], &server, port) < 0) {
             error_setg(errp, "Conflicting/invalid host:port in guest "
                        "forwarding rule '%s'", config_str);
             return -1;
@@ -790,7 +795,8 @@ static int slirp_guestfwd(SlirpState *s, const char *config_str, Error **errp)
             return -1;
         }
 
-        if (slirp_add_exec(s->slirp, &fwd->hd, NULL, &server, port) < 0) {
+        if (slirp_add_guestfwd(s->slirp, guestfwd_write, &fwd->hd,
+                               &server, port) < 0) {
             error_setg(errp, "Conflicting/invalid host:port in guest "
                        "forwarding rule '%s'", config_str);
             g_free(fwd);
slirp/misc.c +23 -14
index 03bfa31f20..7c2ed8c67f 100644
@@ -36,24 +36,33 @@ remque(void *a)
   element->qh_rlink = NULL;
 }
 
-int add_exec(struct gfwd_list **ex_ptr, void *chardev, const char *cmdline,
+struct gfwd_list *
+add_guestfwd(struct gfwd_list **ex_ptr,
+             SlirpWriteCb write_cb, void *opaque,
              struct in_addr addr, int port)
 {
-	struct gfwd_list *tmp_ptr;
-
-	tmp_ptr = *ex_ptr;
-	*ex_ptr = g_new0(struct gfwd_list, 1);
-	(*ex_ptr)->ex_fport = port;
-	(*ex_ptr)->ex_addr = addr;
-	if (chardev) {
-		(*ex_ptr)->ex_chardev = chardev;
-	} else {
-		(*ex_ptr)->ex_exec = g_strdup(cmdline);
-	}
-	(*ex_ptr)->ex_next = tmp_ptr;
-	return 0;
+    struct gfwd_list *f = g_new0(struct gfwd_list, 1);
+
+    f->write_cb = write_cb;
+    f->opaque = opaque;
+    f->ex_fport = port;
+    f->ex_addr = addr;
+    f->ex_next = *ex_ptr;
+    *ex_ptr = f;
+
+    return f;
 }
 
+struct gfwd_list *
+add_exec(struct gfwd_list **ex_ptr, const char *cmdline,
+         struct in_addr addr, int port)
+{
+    struct gfwd_list *f = add_guestfwd(ex_ptr, NULL, NULL, addr, port);
+
+    f->ex_exec = g_strdup(cmdline);
+
+    return f;
+}
 
 #ifdef _WIN32
 
slirp/slirp.c +19 -8
index e7256ec05c..cc361885ed 100644
@@ -25,7 +25,6 @@
 #include "qemu-common.h"
 #include "qemu/timer.h"
 #include "qemu/error-report.h"
-#include "chardev/char-fe.h"
 #include "migration/register.h"
 #include "slirp.h"
 #include "hw/hw.h"
@@ -1064,23 +1063,35 @@ check_guestfwd(Slirp *slirp, struct in_addr *guest_addr, int guest_port)
     return true;
 }
 
-int slirp_add_exec(Slirp *slirp, void *chardev, const char *cmdline,
+int slirp_add_exec(Slirp *slirp, const char *cmdline,
                    struct in_addr *guest_addr, int guest_port)
 {
     if (!check_guestfwd(slirp, guest_addr, guest_port)) {
         return -1;
     }
 
-    return add_exec(&slirp->guestfwd_list, chardev, cmdline, *guest_addr,
-                    htons(guest_port));
+    add_exec(&slirp->guestfwd_list, cmdline, *guest_addr, htons(guest_port));
+    return 0;
+}
+
+int slirp_add_guestfwd(Slirp *slirp, SlirpWriteCb write_cb, void *opaque,
+                       struct in_addr *guest_addr, int guest_port)
+{
+    if (!check_guestfwd(slirp, guest_addr, guest_port)) {
+        return -1;
+    }
+
+    add_guestfwd(&slirp->guestfwd_list, write_cb, opaque,
+                 *guest_addr, htons(guest_port));
+    return 0;
 }
 
 ssize_t slirp_send(struct socket *so, const void *buf, size_t len, int flags)
 {
-    if (so->s == -1 && so->chardev) {
+    if (so->s == -1 && so->guestfwd) {
         /* XXX this blocks entire thread. Rewrite to use
          * qemu_chr_fe_write and background I/O callbacks */
-        qemu_chr_fe_write_all(so->chardev, buf, len);
+        so->guestfwd->write_cb(buf, len, so->guestfwd->opaque);
         return len;
     }
 
@@ -1445,7 +1456,7 @@ static void slirp_state_save(QEMUFile *f, void *opaque)
     struct gfwd_list *ex_ptr;
 
     for (ex_ptr = slirp->guestfwd_list; ex_ptr; ex_ptr = ex_ptr->ex_next)
-        if (ex_ptr->ex_chardev) {
+        if (ex_ptr->write_cb) {
             struct socket *so;
             so = slirp_find_ctl_socket(slirp, ex_ptr->ex_addr,
                                        ntohs(ex_ptr->ex_fport));
@@ -1480,7 +1491,7 @@ static int slirp_state_load(QEMUFile *f, void *opaque, int version_id)
             return -EINVAL;
         }
         for (ex_ptr = slirp->guestfwd_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
-            if (ex_ptr->ex_chardev &&
+            if (ex_ptr->write_cb &&
                 so->so_faddr.s_addr == ex_ptr->ex_addr.s_addr &&
                 so->so_fport == ex_ptr->ex_fport) {
                 break;
slirp/tcp_subr.c +2 -2
index 3b14684107..042fa1e4fb 100644
@@ -959,9 +959,9 @@ int tcp_ctl(struct socket *so)
         for (ex_ptr = slirp->guestfwd_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
             if (ex_ptr->ex_fport == so->so_fport &&
                 so->so_faddr.s_addr == ex_ptr->ex_addr.s_addr) {
-                if (ex_ptr->ex_chardev) {
+                if (ex_ptr->write_cb) {
                     so->s = -1;
-                    so->chardev = ex_ptr->ex_chardev;
+                    so->guestfwd = ex_ptr;
                     return 1;
                 }
                 DEBUG_MISC(" executing %s\n", ex_ptr->ex_exec);
Signature · 1 line
2.20.0.rc1
#16 lore raw

PATCH v2 8/30 net/slirp: simplify checking for cmd: prefix

In reply to Marc-André Lureau
---
 net/slirp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/slirp.c +1 -1
index 53adb92dd5..9f07923925 100644
@@ -767,7 +767,7 @@ static int slirp_guestfwd(SlirpState *s, const char *config_str, Error **errp)
 
     snprintf(buf, sizeof(buf), "guestfwd.tcp.%d", port);
 
-    if ((strlen(p) > 4) && !strncmp(p, "cmd:", 4)) {
+    if (g_str_has_prefix(p, "cmd:")) {
         if (slirp_add_exec(s->slirp, &p[4], &server, port) < 0) {
             error_setg(errp, "Conflicting/invalid host:port in guest "
                        "forwarding rule '%s'", config_str);
Signature · 1 line
2.20.0.rc1
#17 lore raw

PATCH v2 9/30 net/slirp: fix a few memory leaks

In reply to Marc-André Lureau

Handle chardev removal on failure to register the forward rule.

Free all the guestfwd data on cleanup.

---
 net/slirp.c | 27 ++++++++++++++++++++-------
 1 file changed, 20 insertions(+), 7 deletions(-)
net/slirp.c +20 -7
index 9f07923925..6eeb54d7b1 100644
@@ -75,6 +75,13 @@ struct slirp_config_str {
     char str[1024];
 };
 
+struct GuestFwd {
+    CharBackend hd;
+    struct in_addr server;
+    int port;
+    Slirp *slirp;
+};
+
 typedef struct SlirpState {
     NetClientState nc;
     QTAILQ_ENTRY(SlirpState) entry;
@@ -83,6 +90,7 @@ typedef struct SlirpState {
 #ifndef _WIN32
     gchar *smb_dir;
 #endif
+    GSList *fwd;
 } SlirpState;
 
 static struct slirp_config_str *slirp_configs;
@@ -122,10 +130,19 @@ static void slirp_smb_exit(Notifier *n, void *data)
     slirp_smb_cleanup(s);
 }
 
+static void slirp_free_fwd(gpointer data)
+{
+    struct GuestFwd *fwd = data;
+
+    qemu_chr_fe_deinit(&fwd->hd, true);
+    g_free(data);
+}
+
 static void net_slirp_cleanup(NetClientState *nc)
 {
     SlirpState *s = DO_UPCAST(SlirpState, nc, nc);
 
+    g_slist_free_full(s->fwd, slirp_free_fwd);
     slirp_cleanup(s->slirp);
     if (s->exit_notifier.notify) {
         qemu_remove_exit_notifier(&s->exit_notifier);
@@ -711,13 +728,6 @@ static int slirp_smb(SlirpState* s, const char *exported_dir,
 
 #endif /* !defined(_WIN32) */
 
-struct GuestFwd {
-    CharBackend hd;
-    struct in_addr server;
-    int port;
-    Slirp *slirp;
-};
-
 static int guestfwd_can_read(void *opaque)
 {
     struct GuestFwd *fwd = opaque;
@@ -791,6 +801,7 @@ static int slirp_guestfwd(SlirpState *s, const char *config_str, Error **errp)
         qemu_chr_fe_init(&fwd->hd, chr, &err);
         if (err) {
             error_propagate(errp, err);
+            object_unparent(OBJECT(chr));
             g_free(fwd);
             return -1;
         }
@@ -799,6 +810,7 @@ static int slirp_guestfwd(SlirpState *s, const char *config_str, Error **errp)
                                &server, port) < 0) {
             error_setg(errp, "Conflicting/invalid host:port in guest "
                        "forwarding rule '%s'", config_str);
+            qemu_chr_fe_deinit(&fwd->hd, true);
             g_free(fwd);
             return -1;
         }
@@ -808,6 +820,7 @@ static int slirp_guestfwd(SlirpState *s, const char *config_str, Error **errp)
 
         qemu_chr_fe_set_handlers(&fwd->hd, guestfwd_can_read, guestfwd_read,
                                  NULL, NULL, fwd, NULL, true);
+        s->fwd = g_slist_append(s->fwd, fwd);
     }
     return 0;
 
Signature · 1 line
2.20.0.rc1
#18 lore raw

PATCH v2 10/30 slirp: use virtual time for packet expiration

In reply to Marc-André Lureau

Make all packets expiration time based on virtual clock.

---
 slirp/if.c    | 2 +-
 slirp/slirp.c | 7 ++++---
 2 files changed, 5 insertions(+), 4 deletions(-)
slirp/if.c +1 -1
index aa88cc4e76..ce4f5fac53 100644
@@ -148,7 +148,7 @@ diddit:
  */
 void if_start(Slirp *slirp)
 {
-    uint64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
+    uint64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
     bool from_batchq = false;
     struct mbuf *ifm, *ifm_next, *ifqt;
 
slirp/slirp.c +4 -3
index cc361885ed..9ace0e3d4b 100644
@@ -581,7 +581,7 @@ void slirp_pollfds_poll(GArray *pollfds, int select_error)
         return;
     }
 
-    curtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
+    curtime = qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL);
 
     QTAILQ_FOREACH(slirp, &slirp_instances, entry) {
         /*
@@ -908,7 +908,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 = qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + 1000000000ULL;
+            ifm->expiration_date =
+                qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 1000000000ULL;
         }
         return 0;
     } else {
@@ -935,7 +936,7 @@ static int if_encap6(Slirp *slirp, struct mbuf *ifm, struct ethhdr *eh,
             ndp_send_ns(slirp, ip6h->ip_dst);
             ifm->resolution_requested = true;
             ifm->expiration_date =
-                qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + 1000000000ULL;
+                qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 1000000000ULL;
         }
         return 0;
     } else {
Signature · 1 line
2.20.0.rc1
#20 lore raw

PATCH v2 11/30 slirp: add clock_get_ns() callback

In reply to Marc-André Lureau
---
 slirp/libslirp.h |  2 ++
 net/slirp.c      |  6 ++++++
 slirp/if.c       |  2 +-
 slirp/ip6_icmp.c |  4 ++--
 slirp/slirp.c    | 12 +++++-------
 5 files changed, 16 insertions(+), 10 deletions(-)
slirp/libslirp.h +2 -0
index d7c2b1ae71..ea019828e8 100644
@@ -17,6 +17,8 @@ typedef struct SlirpCb {
     void (*output)(void *opaque, const uint8_t *pkt, int pkt_len);
     /* Print a message for an error due to guest misbehavior.  */
     void (*guest_error)(const char *msg);
+    /* Return the virtual clock value in nanoseconds */
+    int64_t (*clock_get_ns)(void);
 } SlirpCb;
 
 
net/slirp.c +6 -0
index 6eeb54d7b1..c7062b51c2 100644
@@ -163,9 +163,15 @@ static void net_slirp_guest_error(const char *msg)
     qemu_log_mask(LOG_GUEST_ERROR, "%s", msg);
 }
 
+static int64_t net_slirp_clock_get_ns(void)
+{
+    return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
+}
+
 static const SlirpCb slirp_cb = {
     .output = net_slirp_output,
     .guest_error = net_slirp_guest_error,
+    .clock_get_ns = net_slirp_clock_get_ns,
 };
 
 static int net_slirp_init(NetClientState *peer, const char *model,
slirp/if.c +1 -1
index ce4f5fac53..73e3705740 100644
@@ -148,7 +148,7 @@ diddit:
  */
 void if_start(Slirp *slirp)
 {
-    uint64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
+    uint64_t now = slirp->cb->clock_get_ns();
     bool from_batchq = false;
     struct mbuf *ifm, *ifm_next, *ifqt;
 
slirp/ip6_icmp.c +2 -2
index 595a62c8d4..6e2d952f17 100644
@@ -17,7 +17,7 @@ static void ra_timer_handler(void *opaque)
 {
     Slirp *slirp = opaque;
     timer_mod(slirp->ra_timer,
-              qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + NDP_Interval);
+              slirp->cb->clock_get_ns() / SCALE_MS + NDP_Interval);
     ndp_send_ra(slirp);
 }
 
@@ -31,7 +31,7 @@ void icmp6_init(Slirp *slirp)
                                      SCALE_MS, QEMU_TIMER_ATTR_EXTERNAL,
                                      ra_timer_handler, slirp);
     timer_mod(slirp->ra_timer,
-              qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + NDP_Interval);
+              slirp->cb->clock_get_ns() / SCALE_MS + NDP_Interval);
 }
 
 void icmp6_cleanup(Slirp *slirp)
slirp/slirp.c +5 -7
index 9ace0e3d4b..76b1f2f3e0 100644
@@ -573,15 +573,15 @@ void slirp_pollfds_fill(GArray *pollfds, uint32_t *timeout)
 
 void slirp_pollfds_poll(GArray *pollfds, int select_error)
 {
-    Slirp *slirp;
+    Slirp *slirp = QTAILQ_FIRST(&slirp_instances);
     struct socket *so, *so_next;
     int ret;
 
-    if (QTAILQ_EMPTY(&slirp_instances)) {
+    if (!slirp) {
         return;
     }
 
-    curtime = qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL);
+    curtime = slirp->cb->clock_get_ns() / SCALE_MS;
 
     QTAILQ_FOREACH(slirp, &slirp_instances, entry) {
         /*
@@ -908,8 +908,7 @@ 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 =
-                qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 1000000000ULL;
+            ifm->expiration_date = slirp->cb->clock_get_ns() + 1000000000ULL;
         }
         return 0;
     } else {
@@ -935,8 +934,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 =
-                qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 1000000000ULL;
+            ifm->expiration_date = slirp->cb->clock_get_ns() + 1000000000ULL;
         }
         return 0;
     } else {
Signature · 1 line
2.20.0.rc1
#22 lore raw

PATCH v2 12/30 slirp: add callbacks for timer

In reply to Marc-André Lureau
---
 slirp/libslirp.h | 14 +++++++++++---
 slirp/slirp.h    |  2 +-
 net/slirp.c      | 21 +++++++++++++++++++++
 slirp/ip6_icmp.c | 16 +++++++---------
 4 files changed, 40 insertions(+), 13 deletions(-)
slirp/libslirp.h +11 -3
index ea019828e8..3e75dadfa3 100644
@@ -6,19 +6,27 @@
 typedef struct Slirp Slirp;
 
 typedef int (*SlirpWriteCb)(const void *buf, size_t len, void *opaque);
+typedef void (*SlirpTimerCb)(void *opaque);
 
 /*
  * Callbacks from slirp
- *
- * The opaque parameter comes from the opaque parameter given to slirp_init().
  */
 typedef struct SlirpCb {
-    /* Send an ethernet frame to the guest network.  */
+    /*
+     * Send an ethernet frame to the guest network. The opaque parameter
+     * is the one given to slirp_init().
+     */
     void (*output)(void *opaque, const uint8_t *pkt, int pkt_len);
     /* Print a message for an error due to guest misbehavior.  */
     void (*guest_error)(const char *msg);
     /* Return the virtual clock value in nanoseconds */
     int64_t (*clock_get_ns)(void);
+    /* Create a new timer with the given callback and opaque data */
+    void *(*timer_new)(SlirpTimerCb cb, void *opaque);
+    /* Remove and free a timer */
+    void (*timer_free)(void *timer);
+    /* Modify a timer to expire at @expire_time */
+    void (*timer_mod)(void *timer, int64_t expire_time);
 } SlirpCb;
 
 
slirp/slirp.h +1 -1
index 9aa245715d..17056f4b83 100644
@@ -193,7 +193,7 @@ struct Slirp {
     NdpTable ndp_table;
 
     GRand *grand;
-    QEMUTimer *ra_timer;
+    void *ra_timer;
 
     const SlirpCb *cb;
     void *opaque;
net/slirp.c +21 -0
index c7062b51c2..f7cdf8398b 100644
@@ -168,10 +168,31 @@ static int64_t net_slirp_clock_get_ns(void)
     return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
 }
 
+static void *net_slirp_timer_new(SlirpTimerCb cb, void *opaque)
+{
+    return timer_new_full(NULL, QEMU_CLOCK_VIRTUAL,
+                          SCALE_MS, QEMU_TIMER_ATTR_EXTERNAL,
+                          cb, opaque);
+}
+
+static void net_slirp_timer_free(void *timer)
+{
+    timer_del(timer);
+    timer_free(timer);
+}
+
+static void net_slirp_timer_mod(void *timer, int64_t expire_timer)
+{
+    timer_mod(timer, expire_timer);
+}
+
 static const SlirpCb slirp_cb = {
     .output = net_slirp_output,
     .guest_error = net_slirp_guest_error,
     .clock_get_ns = net_slirp_clock_get_ns,
+    .timer_new = net_slirp_timer_new,
+    .timer_free = net_slirp_timer_free,
+    .timer_mod = net_slirp_timer_mod,
 };
 
 static int net_slirp_init(NetClientState *peer, const char *model,
slirp/ip6_icmp.c +7 -9
index 6e2d952f17..0acdcc26a2 100644
@@ -16,8 +16,9 @@
 static void ra_timer_handler(void *opaque)
 {
     Slirp *slirp = opaque;
-    timer_mod(slirp->ra_timer,
-              slirp->cb->clock_get_ns() / SCALE_MS + NDP_Interval);
+
+    slirp->cb->timer_mod(slirp->ra_timer,
+                         slirp->cb->clock_get_ns() / SCALE_MS + NDP_Interval);
     ndp_send_ra(slirp);
 }
 
@@ -27,11 +28,9 @@ void icmp6_init(Slirp *slirp)
         return;
     }
 
-    slirp->ra_timer = timer_new_full(NULL, QEMU_CLOCK_VIRTUAL,
-                                     SCALE_MS, QEMU_TIMER_ATTR_EXTERNAL,
-                                     ra_timer_handler, slirp);
-    timer_mod(slirp->ra_timer,
-              slirp->cb->clock_get_ns() / SCALE_MS + NDP_Interval);
+    slirp->ra_timer = slirp->cb->timer_new(ra_timer_handler, slirp);
+    slirp->cb->timer_mod(slirp->ra_timer,
+                         slirp->cb->clock_get_ns() / SCALE_MS + NDP_Interval);
 }
 
 void icmp6_cleanup(Slirp *slirp)
@@ -40,8 +39,7 @@ void icmp6_cleanup(Slirp *slirp)
         return;
     }
 
-    timer_del(slirp->ra_timer);
-    timer_free(slirp->ra_timer);
+    slirp->cb->timer_free(slirp->ra_timer);
 }
 
 static void icmp6_send_echoreply(struct mbuf *m, Slirp *slirp, struct ip6 *ip,
Signature · 1 line
2.20.0.rc1
#23 lore raw

PATCH v2 13/30 build-sys: use a seperate slirp-obj-y && slirp.mo

In reply to Marc-André Lureau

This will allow to have cflags for the whole slirp.mo -objs. It makes it possible to build tests that links only with slirp-obj-y (and not the whole common-obj).

It is also a step towards building slirp as a shared library, although this requires a bit more thoughts to build with
net/slirp.o (CONFIG_SLIRP would need to be 'm') and other build issues.

---
 Makefile.objs       |  3 +--
 Makefile.target     |  5 ++++-
 slirp/Makefile.objs | 35 ++++++++++++++++++++++++++++++-----
 3 files changed, 35 insertions(+), 8 deletions(-)
Makefile.objs +1 -2
index 31852eaf8f..a11050a7aa 100644
@@ -61,6 +61,7 @@ util-obj-y += qapi/qapi-events-ui.o
 util-obj-y += qapi/qapi-introspect.o
 
 chardev-obj-y = chardev/
+slirp-obj-$(CONFIG_SLIRP) = slirp/
 
 #######################################################################
 # block-obj-y is code used by both qemu system emulation and qemu-img
@@ -123,8 +124,6 @@ common-obj-y += vl.o
 vl.o-cflags := $(GPROF_CFLAGS) $(SDL_CFLAGS)
 common-obj-$(CONFIG_TPM) += tpm.o
 
-common-obj-$(CONFIG_SLIRP) += slirp/
-
 common-obj-y += backends/
 common-obj-y += chardev/
 
Makefile.target +4 -1
index 4d56298bbf..3a970b1b77 100644
@@ -168,6 +168,7 @@ target-obj-y :=
 block-obj-y :=
 common-obj-y :=
 chardev-obj-y :=
+slirp-obj-y :=
 include $(SRC_PATH)/Makefile.objs
 dummy := $(call unnest-vars,,target-obj-y)
 target-obj-y-save := $(target-obj-y)
@@ -180,9 +181,11 @@ dummy := $(call unnest-vars,.., \
                qom-obj-y \
                io-obj-y \
                common-obj-y \
-               common-obj-m)
+               common-obj-m \
+               slirp-obj-y)
 target-obj-y := $(target-obj-y-save)
 all-obj-y += $(common-obj-y)
+all-obj-y += $(slirp-obj-y)
 all-obj-y += $(target-obj-y)
 all-obj-y += $(qom-obj-y)
 all-obj-$(CONFIG_SOFTMMU) += $(block-obj-y) $(chardev-obj-y)
slirp/Makefile.objs +30 -5
index 28049b03cd..21653f69e9 100644
@@ -1,5 +1,30 @@
-common-obj-y = cksum.o if.o ip_icmp.o ip6_icmp.o ip6_input.o ip6_output.o \
-               ip_input.o ip_output.o dnssearch.o dhcpv6.o
-common-obj-y += slirp.o mbuf.o misc.o sbuf.o socket.o tcp_input.o tcp_output.o
-common-obj-y += tcp_subr.o tcp_timer.o udp.o udp6.o bootp.o tftp.o arp_table.o \
-                ndp_table.o ncsi.o
+slirp-obj-y = slirp.mo
+
+slirp.mo-objs = \
+	arp_table.o \
+	bootp.o \
+	cksum.o \
+	dhcpv6.o \
+	dnssearch.o \
+	if.o \
+	ip6_icmp.o \
+	ip6_input.o \
+	ip6_output.o \
+	ip_icmp.o \
+	ip_input.o \
+	ip_output.o \
+	mbuf.o \
+	misc.o \
+	ncsi.o \
+	ndp_table.o \
+	sbuf.o \
+	slirp.o \
+	socket.o \
+	tcp_input.o \
+	tcp_output.o \
+	tcp_subr.o \
+	tcp_timer.o \
+	tftp.o \
+	udp.o \
+	udp6.o \
+	$(NULL)
Signature · 1 line
2.20.0.rc1
#24 lore raw

Re: [Qemu-devel] [PATCH v2 for-3.2 v2 13/30] build-sys: use a seperate slirp-obj-y && slirp.mo

Marc-André Lureau, le jeu. 22 nov. 2018 02:06:30 +0400, a ecrit:

Quoted 9 lines

This will allow to have cflags for the whole slirp.mo -objs. It makes it possible to build tests that links only with slirp-obj-y (and not the whole common-obj).

It is also a step towards building slirp as a shared library, although this requires a bit more thoughts to build with
net/slirp.o (CONFIG_SLIRP would need to be 'm') and other build issues.

Applied to my tree, thanks!

#25 lore raw

PATCH v2 14/30 slirp: set G_LOG_DOMAIN

In reply to Marc-André Lureau

We are moving to g_log() facilities to log errors and probably debug messages too. Let's have the "Slirp" prefix on messages slirp produces.

---
 slirp/Makefile.objs | 2 ++
 1 file changed, 2 insertions(+)
slirp/Makefile.objs +2 -0
index 21653f69e9..959558c732 100644
@@ -28,3 +28,5 @@ slirp.mo-objs = \
 	udp.o \
 	udp6.o \
 	$(NULL)
+
+slirp.mo-cflags = -DG_LOG_DOMAIN=\"Slirp\"
Signature · 1 line
2.20.0.rc1
#26 lore raw

Re: [Qemu-devel] [PATCH v2 for-3.2 v2 14/30] slirp: set G_LOG_DOMAIN

Marc-André Lureau, le jeu. 22 nov. 2018 02:06:31 +0400, a ecrit:

Quoted 4 lines

We are moving to g_log() facilities to log errors and probably debug messages too. Let's have the "Slirp" prefix on messages slirp produces.

Applied to my tree, thanks!

#27 lore raw

PATCH v2 15/30 slirp: replace error_report() and a fprintf with g_critical()

In reply to Marc-André Lureau

Reduce dependency on QEMU. QEMU could use a custom glib log handler if it wants to redirect/filter it.

---
 slirp/misc.c   | 2 +-
 slirp/socket.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
slirp/misc.c +1 -1
index 7c2ed8c67f..6ee0ff0231 100644
@@ -164,7 +164,7 @@ fork_exec(struct socket *so, const char *ex)
     g_strfreev(argv);
 
     if (err) {
-        error_report("%s", err->message);
+        g_critical("fork_exec: %s", err->message);
         g_error_free(err);
         closesocket(sp[0]);
         closesocket(sp[1]);
slirp/socket.c +1 -1
index 677fd20c9d..08a065f6a7 100644
@@ -285,7 +285,7 @@ err:
 
     sofcantrcvmore(so);
     tcp_sockclosed(sototcpcb(so));
-    fprintf(stderr, "soreadbuf buffer to small");
+    g_critical("soreadbuf buffer too small");
     return -1;
 }
 
Signature · 1 line
2.20.0.rc1
#28 lore raw

Re: [Qemu-devel] [PATCH v2 for-3.2 v2 15/30] slirp: replace error_report() and a fprintf with g_critical()

Marc-André Lureau, le jeu. 22 nov. 2018 02:06:32 +0400, a ecrit:

Quoted 4 lines

Reduce dependency on QEMU. QEMU could use a custom glib log handler if it wants to redirect/filter it.

Applied, thanks!

#29 lore raw

PATCH v2 16/30 slirp: replace some fprintf() with DEBUG_MISC

In reply to Marc-André Lureau

Remove some clutter, and avoids direct call to fprintf().

---
 slirp/slirp.c | 17 ++++++-----------
 1 file changed, 6 insertions(+), 11 deletions(-)
slirp/slirp.c +6 -11
index 76b1f2f3e0..432adb02c4 100644
@@ -163,9 +163,7 @@ static int get_dns_addr_resolv_conf(int af, void *pdns_addr, void *cached_addr,
     if (!f)
         return -1;
 
-#ifdef DEBUG
-    fprintf(stderr, "IP address of your DNS(s): ");
-#endif
+    DEBUG_MISC("IP address of your DNS(s): ");
     while (fgets(buff, 512, f) != NULL) {
         if (sscanf(buff, "nameserver%*[ \t]%256s", buff2) == 1) {
             char *c = strchr(buff2, '%');
@@ -187,15 +185,12 @@ static int get_dns_addr_resolv_conf(int af, void *pdns_addr, void *cached_addr,
                     *scope_id = if_index;
                 }
                 *cached_time = curtime;
+            } else {
+                DEBUG_MISC(", ");
             }
-#ifdef DEBUG
-            else
-                fprintf(stderr, ", ");
-#endif
+
             if (++found > 3) {
-#ifdef DEBUG
-                fprintf(stderr, "(more)");
-#endif
+                DEBUG_MISC("(more)");
                 break;
             }
 #ifdef DEBUG
@@ -205,7 +200,7 @@ static int get_dns_addr_resolv_conf(int af, void *pdns_addr, void *cached_addr,
                 if (!res) {
                     res = "(string conversion error)";
                 }
-                fprintf(stderr, "%s", res);
+                DEBUG_MISC("%s", res);
             }
 #endif
         }
Signature · 1 line
2.20.0.rc1
#30 lore raw

Re: [Qemu-devel] [PATCH v2 for-3.2 v2 16/30] slirp: replace some fprintf() with DEBUG_MISC

Marc-André Lureau, le jeu. 22 nov. 2018 02:06:33 +0400, a ecrit:

Quoted 3 lines

Remove some clutter, and avoids direct call to fprintf().

Applied to my tree, thanks!

#31 lore raw

PATCH v2 17/30 slirp: replace a DEBUG block with WITH_ICMP_ERROR_MSG

In reply to Marc-André Lureau

icmp_send_error() doesnt actually log messages when DEBUG is enabled. Let's use a different define that describes better the tweaked behaviour of the function, and avoid uncompiled code.

---
 slirp/ip_icmp.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
slirp/ip_icmp.c +5 -3
index af11cfcefe..6c7e375ff8 100644
@@ -34,6 +34,10 @@
 #include "slirp.h"
 #include "ip_icmp.h"
 
+#ifndef WITH_ICMP_ERROR_MSG
+#define WITH_ICMP_ERROR_MSG 0
+#endif
+
 /* The message sent when emulating PING */
 /* Be nice and tell them it's just a pseudo-ping packet */
 static const char icmp_ping_msg[] = "This is a pseudo-PING packet used by Slirp to emulate ICMP ECHO-REQUEST packets.\n";
@@ -319,8 +323,7 @@ icmp_send_error(struct mbuf *msrc, u_char type, u_char code, int minsize,
   HTONS(icp->icmp_ip.ip_id);
   HTONS(icp->icmp_ip.ip_off);
 
-#ifdef DEBUG
-  if(message) {           /* DEBUG : append message to ICMP packet */
+  if (message && WITH_ICMP_ERROR_MSG) { /* append message to ICMP packet */
     int message_len;
     char *cpnt;
     message_len=strlen(message);
@@ -329,7 +332,6 @@ icmp_send_error(struct mbuf *msrc, u_char type, u_char code, int minsize,
     memcpy(cpnt, message, message_len);
     m->m_len+=message_len;
   }
-#endif
 
   icp->icmp_cksum = 0;
   icp->icmp_cksum = cksum(m, m->m_len);
Signature · 1 line
2.20.0.rc1
#32 lore raw

Re: [Qemu-devel] [PATCH v2 for-3.2 v2 17/30] slirp: replace a DEBUG block with WITH_ICMP_ERROR_MSG

Marc-André Lureau, le jeu. 22 nov. 2018 02:06:34 +0400, a ecrit:

Quoted 5 lines

icmp_send_error() doesnt actually log messages when DEBUG is enabled. Let's use a different define that describes better the tweaked behaviour of the function, and avoid uncompiled code.

Applied to my tree, thanks!

#33 lore raw

PATCH v2 18/30 slirp: no need to make DPRINTF conditional on DEBUG

In reply to Marc-André Lureau

DEBUG_CALL is already handled conditionally.

---
 slirp/bootp.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
slirp/bootp.c +2 -5
index 5ab6692038..4c9a77eb98 100644
@@ -36,11 +36,7 @@
 
 static const uint8_t rfc1533_cookie[] = { RFC1533_COOKIE };
 
-#ifdef DEBUG
 #define DPRINTF(fmt, ...) DEBUG_CALL(fmt, ##__VA_ARGS__)
-#else
-#define DPRINTF(fmt, ...) do{}while(0)
-#endif
 
 static BOOTPClient *get_new_addr(Slirp *slirp, struct in_addr *paddr,
                                  const uint8_t *macaddr)
@@ -166,8 +162,9 @@ static void bootp_reply(Slirp *slirp, const struct bootp_t *bp)
     DPRINTF("bootp packet op=%d msgtype=%d", bp->bp_op, dhcp_msg_type);
     if (preq_addr.s_addr != htonl(0L))
         DPRINTF(" req_addr=%08" PRIx32 "\n", ntohl(preq_addr.s_addr));
-    else
+    else {
         DPRINTF("\n");
+    }
 
     if (dhcp_msg_type == 0)
         dhcp_msg_type = DHCPREQUEST; /* Force reply for old BOOTP clients */
Signature · 1 line
2.20.0.rc1
#34 lore raw

Re: [Qemu-devel] [PATCH v2 for-3.2 v2 18/30] slirp: no need to make DPRINTF conditional on DEBUG

Marc-André Lureau, le jeu. 22 nov. 2018 02:06:35 +0400, a ecrit:

Quoted 3 lines

DEBUG_CALL is already handled conditionally.

Applied to my tree, thanks!

#35 lore raw

PATCH v2 19/30 slirp: always build with debug statements

In reply to Marc-André Lureau

Make debug statements condiitonal only on slirp_debug flags, instead of the pre-processor DEBUG blocks, as it may introduce breakage easily, since the debug code isn't always compiled.

---
 slirp/debug.h | 15 +++------------
 slirp/misc.c  |  6 +++++-
 2 files changed, 8 insertions(+), 13 deletions(-)
slirp/debug.h +3 -12
index ca3a4b04da..ff920f0b87 100644
@@ -5,9 +5,8 @@
  * terms and conditions of the copyright.
  */
 
-//#define DEBUG 1
-
-#ifdef DEBUG
+#ifndef DEBUG_H_
+#define DEBUG_H_
 
 #define DBG_CALL 0x1
 #define DBG_MISC 0x2
@@ -50,12 +49,4 @@ extern int slirp_debug;
     }                                           \
 } while (0)
 
-#else
-
-#define DEBUG_CALL(fmt, ...)
-#define DEBUG_ARG(fmt, ...)
-#define DEBUG_ARGS(fmt, ...)
-#define DEBUG_MISC(fmt, ...)
-#define DEBUG_ERROR(fmt, ...)
-
-#endif
+#endif /* DEBUG_H_ */
slirp/misc.c +5 -1
index 6ee0ff0231..0b7ec89595 100644
@@ -12,9 +12,13 @@
 #include "qemu/main-loop.h"
 
 #ifdef DEBUG
-int slirp_debug = DBG_CALL|DBG_MISC|DBG_ERROR;
+#define SLIRP_DEBUG (DBG_CALL | DBG_MISC | DBG_ERROR)
+#else
+#define SLIRP_DEBUG 0
 #endif
 
+int slirp_debug = SLIRP_DEBUG;
+
 inline void
 insque(void *a, void *b)
 {
Signature · 1 line
2.20.0.rc1
#36 lore raw

Re: [Qemu-devel] [PATCH v2 for-3.2 v2 19/30] slirp: always build with debug statements

Marc-André Lureau, le jeu. 22 nov. 2018 02:06:36 +0400, a ecrit:

Quoted 5 lines

Make debug statements condiitonal only on slirp_debug flags, instead of the pre-processor DEBUG blocks, as it may introduce breakage easily, since the debug code isn't always compiled.

Applied to my tree, thanks!

#37 lore raw

PATCH v2 20/30 slirp: introduce SLIRP_DEBUG environment variable

In reply to Marc-André Lureau

Learn to read SLIRP_DEBUG=call,misc,error (all or help also handled) to set the slirp_debug flags.

---
 slirp/misc.c  |  8 --------
 slirp/slirp.c | 15 +++++++++++++++
 2 files changed, 15 insertions(+), 8 deletions(-)
slirp/misc.c +0 -8
index 0b7ec89595..f9b2c64541 100644
@@ -11,14 +11,6 @@
 #include "qemu/error-report.h"
 #include "qemu/main-loop.h"
 
-#ifdef DEBUG
-#define SLIRP_DEBUG (DBG_CALL | DBG_MISC | DBG_ERROR)
-#else
-#define SLIRP_DEBUG 0
-#endif
-
-int slirp_debug = SLIRP_DEBUG;
-
 inline void
 insque(void *a, void *b)
 {
slirp/slirp.c +15 -0
index 432adb02c4..51c5813c7c 100644
@@ -34,6 +34,8 @@
 #include <net/if.h>
 #endif
 
+int slirp_debug;
+
 /* Define to 1 if you want KEEPALIVE timers */
 bool slirp_do_keepalive;
 
@@ -249,6 +251,7 @@ int get_dns6_addr(struct in6_addr *pdns6_addr, uint32_t *scope_id)
 static void slirp_init_once(void)
 {
     static int initialized;
+    const char *debug;
 #ifdef _WIN32
     WSADATA Data;
 #endif
@@ -265,6 +268,18 @@ static void slirp_init_once(void)
 
     loopback_addr.s_addr = htonl(INADDR_LOOPBACK);
     loopback_mask = htonl(IN_CLASSA_NET);
+
+    debug = g_getenv("SLIRP_DEBUG");
+    if (debug) {
+        const GDebugKey keys[] = {
+            { "call", DBG_CALL },
+            { "misc", DBG_MISC },
+            { "error", DBG_ERROR },
+        };
+        slirp_debug = g_parse_debug_string(debug, keys, G_N_ELEMENTS(keys));
+    }
+
+
 }
 
 static void slirp_state_save(QEMUFile *f, void *opaque);
Signature · 1 line
2.20.0.rc1
#38 lore raw

Re: [Qemu-devel] [PATCH v2 for-3.2 v2 20/30] slirp: introduce SLIRP_DEBUG environment variable

Marc-André Lureau, le jeu. 22 nov. 2018 02:06:37 +0400, a ecrit:

Quoted 4 lines

Learn to read SLIRP_DEBUG=call,misc,error (all or help also handled) to set the slirp_debug flags.

Applied to my tree, thanks!

#39 lore raw

PATCH v2 21/30 slirp: use %p for pointers format

In reply to Marc-André Lureau

This fixes some compilation warnings on mingw64.

---
 slirp/ip6_icmp.c   | 2 +-
 slirp/ip6_input.c  | 2 +-
 slirp/ip6_output.c | 4 ++--
 slirp/udp6.c       | 6 +++---
 4 files changed, 7 insertions(+), 7 deletions(-)
slirp/ip6_icmp.c +1 -1
index 0acdcc26a2..95f8fddacc 100644
@@ -388,7 +388,7 @@ void icmp6_input(struct mbuf *m)
     int hlen = sizeof(struct ip6);
 
     DEBUG_CALL("icmp6_input");
-    DEBUG_ARG("m = %lx", (long) m);
+    DEBUG_ARG("m = %p", m);
     DEBUG_ARG("m_len = %d", m->m_len);
 
     if (ntohs(ip->ip_pl) < ICMP6_MINLEN) {
slirp/ip6_input.c +1 -1
index ac2e3ea882..ab656a0a9d 100644
@@ -31,7 +31,7 @@ void ip6_input(struct mbuf *m)
     }
 
     DEBUG_CALL("ip6_input");
-    DEBUG_ARG("m = %lx", (long)m);
+    DEBUG_ARG("m = %p", m);
     DEBUG_ARG("m_len = %d", m->m_len);
 
     if (m->m_len < sizeof(struct ip6)) {
slirp/ip6_output.c +2 -2
index 762cbfe89c..52c88ad691 100644
@@ -19,8 +19,8 @@ int ip6_output(struct socket *so, struct mbuf *m, int fast)
     struct ip6 *ip = mtod(m, struct ip6 *);
 
     DEBUG_CALL("ip6_output");
-    DEBUG_ARG("so = %lx", (long)so);
-    DEBUG_ARG("m = %lx", (long)m);
+    DEBUG_ARG("so = %p", so);
+    DEBUG_ARG("m = %p", m);
 
     /* Fill IPv6 header */
     ip->ip_v = IP6VERSION;
slirp/udp6.c +3 -3
index 473ba1586e..8cdb1892e2 100644
@@ -20,7 +20,7 @@ void udp6_input(struct mbuf *m)
     struct sockaddr_in6 lhost;
 
     DEBUG_CALL("udp6_input");
-    DEBUG_ARG("m = %lx", (long)m);
+    DEBUG_ARG("m = %p", m);
 
     if (slirp->restricted) {
         goto bad;
@@ -144,8 +144,8 @@ int udp6_output(struct socket *so, struct mbuf *m,
     struct udphdr *uh;
 
     DEBUG_CALL("udp6_output");
-    DEBUG_ARG("so = %lx", (long)so);
-    DEBUG_ARG("m = %lx", (long)m);
+    DEBUG_ARG("so = %p", so);
+    DEBUG_ARG("m = %p", m);
 
     /* adjust for header */
     m->m_data -= sizeof(struct udphdr);
Signature · 1 line
2.20.0.rc1
#40 lore raw

Re: [Qemu-devel] [PATCH v2 for-3.2 v2 21/30] slirp: use %p for pointers format

Marc-André Lureau, le jeu. 22 nov. 2018 02:06:38 +0400, a ecrit:

Quoted 3 lines

This fixes some compilation warnings on mingw64.

Applied to my tree, thanks!

#41 lore raw

PATCH v2 22/30 slirp: remove remaining DEBUG blocks

In reply to Marc-André Lureau

Instead, rely on slirp_debug flags, or compile unconditionally (the substraction in cksum is unlikely to affect any benchmark result).

---
 slirp/cksum.c   | 4 ----
 slirp/ip_icmp.c | 5 ++---
 slirp/slirp.c   | 5 +----
 3 files changed, 3 insertions(+), 11 deletions(-)
slirp/cksum.c +0 -4
index b9466485b5..0a988b845d 100644
@@ -70,9 +70,7 @@ int cksum(struct mbuf *m, int len)
 
 	if (len < mlen)
 	   mlen = len;
-#ifdef DEBUG
 	len -= mlen;
-#endif
 	/*
 	 * Force to even boundary.
 	 */
@@ -122,12 +120,10 @@ int cksum(struct mbuf *m, int len)
 	   s_util.c[0] = *(uint8_t *)w;
 
 cont:
-#ifdef DEBUG
 	if (len) {
 		DEBUG_ERROR("cksum: out of data\n");
 		DEBUG_ERROR(" len = %d\n", len);
 	}
-#endif
 	if (mlen == -1) {
 		/* The last mbuf has odd # of bytes. Follow the
 		 standard (the odd byte may be shifted left by 8 bits
slirp/ip_icmp.c +2 -3
index 6c7e375ff8..cd2faeacb6 100644
@@ -257,13 +257,12 @@ icmp_send_error(struct mbuf *msrc, u_char type, u_char code, int minsize,
   /* check msrc */
   if(!msrc) goto end_error;
   ip = mtod(msrc, struct ip *);
-#ifdef DEBUG
-  { char bufa[20], bufb[20];
+  if (slirp_debug & DBG_MISC) {
+    char bufa[20], bufb[20];
     strcpy(bufa, inet_ntoa(ip->ip_src));
     strcpy(bufb, inet_ntoa(ip->ip_dst));
     DEBUG_MISC(" %.16s to %.16s\n", bufa, bufb);
   }
-#endif
   if(ip->ip_off & IP_OFFMASK) goto end_error;    /* Only reply to fragment 0 */
 
   /* Do not reply to source-only IPs */
slirp/slirp.c +1 -4
index 51c5813c7c..5954a99d3c 100644
@@ -194,9 +194,7 @@ static int get_dns_addr_resolv_conf(int af, void *pdns_addr, void *cached_addr,
             if (++found > 3) {
                 DEBUG_MISC("(more)");
                 break;
-            }
-#ifdef DEBUG
-            else {
+            } else if (slirp_debug & DBG_MISC) {
                 char s[INET6_ADDRSTRLEN];
                 const char *res = inet_ntop(af, tmp_addr, s, sizeof(s));
                 if (!res) {
@@ -204,7 +202,6 @@ static int get_dns_addr_resolv_conf(int af, void *pdns_addr, void *cached_addr,
                 }
                 DEBUG_MISC("%s", res);
             }
-#endif
         }
     }
     fclose(f);
Signature · 1 line
2.20.0.rc1
#42 lore raw

Re: [Qemu-devel] [PATCH v2 for-3.2 v2 22/30] slirp: remove remaining DEBUG blocks

Marc-André Lureau, le jeu. 22 nov. 2018 02:06:39 +0400, a ecrit:

Quoted 4 lines

Instead, rely on slirp_debug flags, or compile unconditionally (the substraction in cksum is unlikely to affect any benchmark result).

Applied to my tree, thanks!

#43 lore raw

PATCH v2 23/30 slirp: replace DEBUG_ARGS with DEBUG_ARG

In reply to Marc-André Lureau

There is no clear benefit in calling an alias DEBUG_ARGS(). Replace calls with DEBUG_ARG(), and fix the white-spacing while at it.

---
 slirp/debug.h     |  2 --
 slirp/arp_table.c | 12 ++++++------
 slirp/ip6_icmp.c  |  2 +-
 slirp/ndp_table.c | 18 +++++++++---------
 slirp/slirp.c     | 12 ++++++------
 slirp/tcp_input.c |  6 +++---
 6 files changed, 25 insertions(+), 27 deletions(-)
slirp/debug.h +0 -2
index ff920f0b87..50f30898fb 100644
@@ -33,8 +33,6 @@ extern int slirp_debug;
     }                                           \
 } while (0)
 
-#define DEBUG_ARGS(fmt, ...) DEBUG_ARG(fmt, ##__VA_ARGS__)
-
 #define DEBUG_MISC(fmt, ...) do {               \
     if (slirp_debug & DBG_MISC) {               \
         fprintf(dfd, fmt, ##__VA_ARGS__);       \
slirp/arp_table.c +6 -6
index ce19e6e7c0..bf71b984ad 100644
@@ -34,9 +34,9 @@ void arp_table_add(Slirp *slirp, uint32_t ip_addr, uint8_t ethaddr[ETH_ALEN])
 
     DEBUG_CALL("arp_table_add");
     DEBUG_ARG("ip = %s", inet_ntoa((struct in_addr){.s_addr = ip_addr}));
-    DEBUG_ARGS(" hw addr = %02x:%02x:%02x:%02x:%02x:%02x\n",
-               ethaddr[0], ethaddr[1], ethaddr[2],
-               ethaddr[3], ethaddr[4], ethaddr[5]);
+    DEBUG_ARG("hw addr = %02x:%02x:%02x:%02x:%02x:%02x",
+              ethaddr[0], ethaddr[1], ethaddr[2],
+              ethaddr[3], ethaddr[4], ethaddr[5]);
 
     if (ip_addr == 0 || ip_addr == 0xffffffff || ip_addr == broadcast_addr) {
         /* Do not register broadcast addresses */
@@ -79,9 +79,9 @@ bool arp_table_search(Slirp *slirp, uint32_t ip_addr,
     for (i = 0; i < ARP_TABLE_SIZE; i++) {
         if (arptbl->table[i].ar_sip == ip_addr) {
             memcpy(out_ethaddr, arptbl->table[i].ar_sha,  ETH_ALEN);
-            DEBUG_ARGS(" found hw addr = %02x:%02x:%02x:%02x:%02x:%02x\n",
-                       out_ethaddr[0], out_ethaddr[1], out_ethaddr[2],
-                       out_ethaddr[3], out_ethaddr[4], out_ethaddr[5]);
+            DEBUG_ARG("found hw addr = %02x:%02x:%02x:%02x:%02x:%02x",
+                      out_ethaddr[0], out_ethaddr[1], out_ethaddr[2],
+                      out_ethaddr[3], out_ethaddr[4], out_ethaddr[5]);
             return 1;
         }
     }
slirp/ip6_icmp.c +1 -1
index 95f8fddacc..e72c57a81d 100644
@@ -75,7 +75,7 @@ void icmp6_send_error(struct mbuf *m, uint8_t type, uint8_t code)
     char addrstr[INET6_ADDRSTRLEN];
 
     DEBUG_CALL("icmp6_send_error");
-    DEBUG_ARGS(" type = %d, code = %d\n", type, code);
+    DEBUG_ARG("type = %d, code = %d", type, code);
 
     if (IN6_IS_ADDR_MULTICAST(&ip->ip_src) ||
             in6_zero(&ip->ip_src)) {
slirp/ndp_table.c +9 -9
index 1401e1b322..b7b73722f7 100644
@@ -18,9 +18,9 @@ void ndp_table_add(Slirp *slirp, struct in6_addr ip_addr,
 
     DEBUG_CALL("ndp_table_add");
     DEBUG_ARG("ip = %s", addrstr);
-    DEBUG_ARGS(" hw addr = %02x:%02x:%02x:%02x:%02x:%02x\n",
-               ethaddr[0], ethaddr[1], ethaddr[2],
-               ethaddr[3], ethaddr[4], ethaddr[5]);
+    DEBUG_ARG("hw addr = %02x:%02x:%02x:%02x:%02x:%02x",
+              ethaddr[0], ethaddr[1], ethaddr[2],
+              ethaddr[3], ethaddr[4], ethaddr[5]);
 
     if (IN6_IS_ADDR_MULTICAST(&ip_addr) || in6_zero(&ip_addr)) {
         /* Do not register multicast or unspecified addresses */
@@ -67,18 +67,18 @@ bool ndp_table_search(Slirp *slirp, struct in6_addr ip_addr,
         out_ethaddr[3] = ip_addr.s6_addr[13];
         out_ethaddr[4] = ip_addr.s6_addr[14];
         out_ethaddr[5] = ip_addr.s6_addr[15];
-        DEBUG_ARGS(" multicast addr = %02x:%02x:%02x:%02x:%02x:%02x\n",
-                   out_ethaddr[0], out_ethaddr[1], out_ethaddr[2],
-                   out_ethaddr[3], out_ethaddr[4], out_ethaddr[5]);
+        DEBUG_ARG("multicast addr = %02x:%02x:%02x:%02x:%02x:%02x",
+                  out_ethaddr[0], out_ethaddr[1], out_ethaddr[2],
+                  out_ethaddr[3], out_ethaddr[4], out_ethaddr[5]);
         return 1;
     }
 
     for (i = 0; i < NDP_TABLE_SIZE; i++) {
         if (in6_equal(&ndp_table->table[i].ip_addr, &ip_addr)) {
             memcpy(out_ethaddr, ndp_table->table[i].eth_addr,  ETH_ALEN);
-            DEBUG_ARGS(" found hw addr = %02x:%02x:%02x:%02x:%02x:%02x\n",
-                       out_ethaddr[0], out_ethaddr[1], out_ethaddr[2],
-                       out_ethaddr[3], out_ethaddr[4], out_ethaddr[5]);
+            DEBUG_ARG("found hw addr = %02x:%02x:%02x:%02x:%02x:%02x",
+                      out_ethaddr[0], out_ethaddr[1], out_ethaddr[2],
+                      out_ethaddr[3], out_ethaddr[4], out_ethaddr[5]);
             return 1;
         }
     }
slirp/slirp.c +6 -6
index 5954a99d3c..12aad702c2 100644
@@ -989,12 +989,12 @@ int if_encap(Slirp *slirp, struct mbuf *ifm)
     }
 
     memcpy(eh->h_dest, ethaddr, ETH_ALEN);
-    DEBUG_ARGS(" src = %02x:%02x:%02x:%02x:%02x:%02x\n",
-               eh->h_source[0], eh->h_source[1], eh->h_source[2],
-               eh->h_source[3], eh->h_source[4], eh->h_source[5]);
-    DEBUG_ARGS(" dst = %02x:%02x:%02x:%02x:%02x:%02x\n",
-               eh->h_dest[0], eh->h_dest[1], eh->h_dest[2],
-               eh->h_dest[3], eh->h_dest[4], eh->h_dest[5]);
+    DEBUG_ARG("src = %02x:%02x:%02x:%02x:%02x:%02x",
+              eh->h_source[0], eh->h_source[1], eh->h_source[2],
+              eh->h_source[3], eh->h_source[4], eh->h_source[5]);
+    DEBUG_ARG("dst = %02x:%02x:%02x:%02x:%02x:%02x",
+              eh->h_dest[0], eh->h_dest[1], eh->h_dest[2],
+              eh->h_dest[3], eh->h_dest[4], eh->h_dest[5]);
     memcpy(buf + sizeof(struct ethhdr), ifm->m_data, ifm->m_len);
     slirp->cb->output(slirp->opaque, buf, ifm->m_len + ETH_HLEN);
     return 1;
slirp/tcp_input.c +3 -3
index bc4aa60046..d24295813a 100644
@@ -215,8 +215,8 @@ tcp_input(struct mbuf *m, int iphlen, struct socket *inso, unsigned short af)
     Slirp *slirp;
 
 	DEBUG_CALL("tcp_input");
-	DEBUG_ARGS(" m = %p  iphlen = %2d  inso = %p\n",
-               m, iphlen, inso);
+	DEBUG_ARG("m = %p  iphlen = %2d  inso = %p",
+              m, iphlen, inso);
 
 	/*
 	 * If called with m == 0, then we're continuing the connect
@@ -1389,7 +1389,7 @@ tcp_dooptions(struct tcpcb *tp, u_char *cp, int cnt, struct tcpiphdr *ti)
 	int opt, optlen;
 
 	DEBUG_CALL("tcp_dooptions");
-	DEBUG_ARGS(" tp = %p  cnt=%i\n", tp, cnt);
+	DEBUG_ARG("tp = %p  cnt=%i", tp, cnt);
 
 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
 		opt = cp[0];
Signature · 1 line
2.20.0.rc1
#44 lore raw

Re: [Qemu-devel] [PATCH v2 for-3.2 v2 23/30] slirp: replace DEBUG_ARGS with DEBUG_ARG

Marc-André Lureau, le jeu. 22 nov. 2018 02:06:40 +0400, a ecrit:

Quoted 4 lines

There is no clear benefit in calling an alias DEBUG_ARGS(). Replace calls with DEBUG_ARG(), and fix the white-spacing while at it.

Applied to my tree, thanks!

#45 lore raw

PATCH v2 24/30 slirp: call into g_debug() for DEBUG macros

In reply to Marc-André Lureau

Make slirp use GLib logging, instead of fprintf(), so that applications can filter log, process it etc.

With recent versions of glib, G_MESSAGES_DEBUG must be set to "all" or "Slirp" to see slirp debug messages.

Reformat DEBUG_MISC & DEBUG_ERROR calls to not need \n ending.

---
 slirp/debug.h      | 17 ++++-------------
 slirp/cksum.c      |  4 ++--
 slirp/dhcpv6.c     |  6 +++---
 slirp/ip_icmp.c    | 10 +++++-----
 slirp/slirp.c      | 10 ++++------
 slirp/socket.c     | 24 ++++++++++++------------
 slirp/tcp_input.c  |  7 +++----
 slirp/tcp_output.c |  2 +-
 slirp/tcp_subr.c   |  2 +-
 slirp/udp.c        |  5 ++---
 slirp/udp6.c       |  5 ++---
 11 files changed, 39 insertions(+), 53 deletions(-)
slirp/debug.h +4 -13
index 50f30898fb..25a5d59439 100644
@@ -12,38 +12,29 @@
 #define DBG_MISC 0x2
 #define DBG_ERROR 0x4
 
-#define dfd stderr
-
 extern int slirp_debug;
 
 #define DEBUG_CALL(fmt, ...) do {               \
     if (slirp_debug & DBG_CALL) {               \
-        fprintf(dfd, fmt, ##__VA_ARGS__);       \
-        fprintf(dfd, "...\n");                  \
-        fflush(dfd);                            \
+        g_debug(fmt "...", ##__VA_ARGS__);      \
     }                                           \
 } while (0)
 
 #define DEBUG_ARG(fmt, ...) do {                \
     if (slirp_debug & DBG_CALL) {               \
-        fputc(' ', dfd);                        \
-        fprintf(dfd, fmt, ##__VA_ARGS__);       \
-        fputc('\n', dfd);                       \
-        fflush(dfd);                            \
+        g_debug(" " fmt, ##__VA_ARGS__);        \
     }                                           \
 } while (0)
 
 #define DEBUG_MISC(fmt, ...) do {               \
     if (slirp_debug & DBG_MISC) {               \
-        fprintf(dfd, fmt, ##__VA_ARGS__);       \
-        fflush(dfd);                            \
+        g_debug(fmt, ##__VA_ARGS__);            \
     }                                           \
 } while (0)
 
 #define DEBUG_ERROR(fmt, ...) do {              \
     if (slirp_debug & DBG_ERROR) {              \
-        fprintf(dfd, fmt, ##__VA_ARGS__);       \
-        fflush(dfd);                            \
+        g_debug(fmt, ##__VA_ARGS__);            \
     }                                           \
 } while (0)
 
slirp/cksum.c +2 -2
index 0a988b845d..84c858fafb 100644
@@ -121,8 +121,8 @@ int cksum(struct mbuf *m, int len)
 
 cont:
 	if (len) {
-		DEBUG_ERROR("cksum: out of data\n");
-		DEBUG_ERROR(" len = %d\n", len);
+		DEBUG_ERROR("cksum: out of data");
+		DEBUG_ERROR(" len = %d", len);
 	}
 	if (mlen == -1) {
 		/* The last mbuf has odd # of bytes. Follow the
slirp/dhcpv6.c +3 -3
index 5d703e8ae6..752df40536 100644
@@ -92,13 +92,13 @@ static int dhcpv6_parse_info_request(Slirp *slirp, uint8_t *odata, int olen,
                     ri->want_boot_url = true;
                     break;
                 default:
-                    DEBUG_MISC("dhcpv6: Unsupported option request %d\n",
+                    DEBUG_MISC("dhcpv6: Unsupported option request %d",
                                req_opt);
                 }
             }
             break;
         default:
-            DEBUG_MISC("dhcpv6 info req: Unsupported option %d, len=%d\n",
+            DEBUG_MISC("dhcpv6 info req: Unsupported option %d, len=%d",
                        option, len);
         }
 
@@ -203,6 +203,6 @@ void dhcpv6_input(struct sockaddr_in6 *srcsas, struct mbuf *m)
         dhcpv6_info_request(m->slirp, srcsas, xid, &data[4], data_len - 4);
         break;
     default:
-        DEBUG_MISC("dhcpv6_input: Unsupported message type 0x%x\n", data[0]);
+        DEBUG_MISC("dhcpv6_input: Unsupported message type 0x%x", data[0]);
     }
 }
slirp/ip_icmp.c +5 -5
index cd2faeacb6..7c7e042049 100644
@@ -103,7 +103,7 @@ static int icmp_send(struct socket *so, struct mbuf *m, int hlen)
 
     if (sendto(so->s, m->m_data + hlen, m->m_len - hlen, 0,
                (struct sockaddr *)&addr, sizeof(addr)) == -1) {
-        DEBUG_MISC("icmp_input icmp sendto tx errno = %d-%s\n",
+        DEBUG_MISC("icmp_input icmp sendto tx errno = %d-%s",
                    errno, strerror(errno));
         icmp_send_error(m, ICMP_UNREACH, ICMP_UNREACH_NET, 0, strerror(errno));
         icmp_detach(so);
@@ -169,7 +169,7 @@ icmp_input(struct mbuf *m, int hlen)
         return;
       }
       if (udp_attach(so, AF_INET) == -1) {
-	DEBUG_MISC("icmp_input udp_attach errno = %d-%s\n",
+	DEBUG_MISC("icmp_input udp_attach errno = %d-%s",
                errno,strerror(errno));
 	sofree(so);
 	m_free(m);
@@ -192,7 +192,7 @@ icmp_input(struct mbuf *m, int hlen)
 
       if(sendto(so->s, icmp_ping_msg, strlen(icmp_ping_msg), 0,
 		(struct sockaddr *)&addr, sockaddr_size(&addr)) == -1) {
-	DEBUG_MISC("icmp_input udp sendto tx errno = %d-%s\n",
+	DEBUG_MISC("icmp_input udp sendto tx errno = %d-%s",
                errno,strerror(errno));
 	icmp_send_error(m, ICMP_UNREACH, ICMP_UNREACH_NET, 0, strerror(errno));
 	udp_detach(so);
@@ -261,7 +261,7 @@ icmp_send_error(struct mbuf *msrc, u_char type, u_char code, int minsize,
     char bufa[20], bufb[20];
     strcpy(bufa, inet_ntoa(ip->ip_src));
     strcpy(bufb, inet_ntoa(ip->ip_dst));
-    DEBUG_MISC(" %.16s to %.16s\n", bufa, bufb);
+    DEBUG_MISC(" %.16s to %.16s", bufa, bufb);
   }
   if(ip->ip_off & IP_OFFMASK) goto end_error;    /* Only reply to fragment 0 */
 
@@ -458,7 +458,7 @@ void icmp_receive(struct socket *so)
         } else {
             error_code = ICMP_UNREACH_HOST;
         }
-        DEBUG_MISC(" udp icmp rx errno = %d-%s\n", errno,
+        DEBUG_MISC(" udp icmp rx errno = %d-%s", errno,
                    strerror(errno));
         icmp_send_error(so->so_m, ICMP_UNREACH, error_code, 0, strerror(errno));
     } else {
slirp/slirp.c +4 -6
index 12aad702c2..f254a23a5a 100644
@@ -165,7 +165,7 @@ static int get_dns_addr_resolv_conf(int af, void *pdns_addr, void *cached_addr,
     if (!f)
         return -1;
 
-    DEBUG_MISC("IP address of your DNS(s): ");
+    DEBUG_MISC("IP address of your DNS(s):");
     while (fgets(buff, 512, f) != NULL) {
         if (sscanf(buff, "nameserver%*[ \t]%256s", buff2) == 1) {
             char *c = strchr(buff2, '%');
@@ -187,20 +187,18 @@ static int get_dns_addr_resolv_conf(int af, void *pdns_addr, void *cached_addr,
                     *scope_id = if_index;
                 }
                 *cached_time = curtime;
-            } else {
-                DEBUG_MISC(", ");
             }
 
             if (++found > 3) {
-                DEBUG_MISC("(more)");
+                DEBUG_MISC("  (more)");
                 break;
             } else if (slirp_debug & DBG_MISC) {
                 char s[INET6_ADDRSTRLEN];
                 const char *res = inet_ntop(af, tmp_addr, s, sizeof(s));
                 if (!res) {
-                    res = "(string conversion error)";
+                    res = "  (string conversion error)";
                 }
-                DEBUG_MISC("%s", res);
+                DEBUG_MISC("  %s", res);
             }
         }
     }
slirp/socket.c +12 -12
index 08a065f6a7..5ffbaa064a 100644
@@ -208,7 +208,8 @@ soread(struct socket *so)
 				}
 			}
 
-			DEBUG_MISC(" --- soread() disconnected, nn = %d, errno = %d-%s\n", nn, errno,strerror(errno));
+			DEBUG_MISC(" --- soread() disconnected, nn = %d, errno = %d-%s",
+                       nn, errno,strerror(errno));
 			sofcantrcvmore(so);
 
 			if (err == ECONNRESET || err == ECONNREFUSED
@@ -237,7 +238,7 @@ soread(struct socket *so)
                 nn += ret;
         }
 
-	DEBUG_MISC(" ... read nn = %d bytes\n", nn);
+	DEBUG_MISC(" ... read nn = %d bytes", nn);
 
 	/* Update fields */
 	sb->sb_cc += nn;
@@ -370,7 +371,7 @@ sosendoob(struct socket *so)
 		n = slirp_send(so, buff, len, (MSG_OOB)); /* |MSG_DONTWAIT)); */
 #ifdef DEBUG
 		if (n != len) {
-			DEBUG_ERROR("Didn't send all data urgently XXXXX\n");
+			DEBUG_ERROR("Didn't send all data urgently XXXXX");
 		}
 #endif
 	}
@@ -379,7 +380,7 @@ sosendoob(struct socket *so)
 		return n;
 	}
 	so->so_urgc -= n;
-	DEBUG_MISC(" ---2 sent %d bytes urgent data, %d urgent bytes left\n", n, so->so_urgc);
+	DEBUG_MISC(" ---2 sent %d bytes urgent data, %d urgent bytes left", n, so->so_urgc);
 
 	sb->sb_cc -= n;
 	sb->sb_rptr += n;
@@ -460,7 +461,7 @@ sowrite(struct socket *so)
             if (ret > 0)
                 nn += ret;
         }
-        DEBUG_MISC("  ... wrote nn = %d bytes\n", nn);
+        DEBUG_MISC("  ... wrote nn = %d bytes", nn);
 
 	/* Update sbuf */
 	sb->sb_cc -= nn;
@@ -478,7 +479,7 @@ sowrite(struct socket *so)
 	return nn;
 
 err_disconnected:
-	DEBUG_MISC(" --- sowrite disconnected, so->so_state = %x, errno = %d\n",
+	DEBUG_MISC(" --- sowrite disconnected, so->so_state = %x, errno = %d",
                so->so_state, errno);
 	sofcantsendmore(so);
 	tcp_sockclosed(sototcpcb(so));
@@ -512,7 +513,7 @@ sorecvfrom(struct socket *so)
 	    if(errno == EHOSTUNREACH) code=ICMP_UNREACH_HOST;
 	    else if(errno == ENETUNREACH) code=ICMP_UNREACH_NET;
 
-	    DEBUG_MISC(" udp icmp rx errno = %d-%s\n",
+	    DEBUG_MISC(" udp icmp rx errno = %d-%s",
                    errno,strerror(errno));
 	    icmp_send_error(so->so_m, ICMP_UNREACH, code, 0, strerror(errno));
 	  } else {
@@ -564,7 +565,7 @@ sorecvfrom(struct socket *so)
 
 	  m->m_len = recvfrom(so->s, m->m_data, len, 0,
 			      (struct sockaddr *)&addr, &addrlen);
-	  DEBUG_MISC(" did recvfrom %d, errno = %d-%s\n",
+	  DEBUG_MISC(" did recvfrom %d, errno = %d-%s",
                  m->m_len, errno,strerror(errno));
 	  if(m->m_len<0) {
 	    /* Report error as ICMP */
@@ -579,7 +580,7 @@ sorecvfrom(struct socket *so)
 		code = ICMP_UNREACH_NET;
 	      }
 
-	      DEBUG_MISC(" rx error, tx icmp ICMP_UNREACH:%i\n", code);
+	      DEBUG_MISC(" rx error, tx icmp ICMP_UNREACH:%i", code);
 	      icmp_send_error(so->so_m, ICMP_UNREACH, code, 0, strerror(errno));
 	      break;
 	    case AF_INET6:
@@ -591,7 +592,7 @@ sorecvfrom(struct socket *so)
 		code = ICMP6_UNREACH_NO_ROUTE;
 	      }
 
-	      DEBUG_MISC(" rx error, tx icmp6 ICMP_UNREACH:%i\n", code);
+	      DEBUG_MISC(" rx error, tx icmp6 ICMP_UNREACH:%i", code);
 	      icmp6_send_error(so->so_m, ICMP6_UNREACH, code);
 	      break;
 	    default:
@@ -839,8 +840,7 @@ void sotranslate_out(struct socket *so, struct sockaddr_storage *addr)
             }
         }
 
-        DEBUG_MISC(" addr.sin_port=%d, "
-                   "addr.sin_addr.s_addr=%.16s\n",
+        DEBUG_MISC(" addr.sin_port=%d, addr.sin_addr.s_addr=%.16s",
                    ntohs(sin->sin_port), inet_ntoa(sin->sin_addr));
         break;
 
slirp/tcp_input.c +3 -4
index d24295813a..a6596b63ac 100644
@@ -641,8 +641,7 @@ findso:
               (errno != EINPROGRESS) && (errno != EWOULDBLOCK)
           ) {
 	    uint8_t code;
-	    DEBUG_MISC(" tcp fconnect errno = %d-%s\n",
-                   errno,strerror(errno));
+	    DEBUG_MISC(" tcp fconnect errno = %d-%s", errno, strerror(errno));
 	    if(errno == ECONNREFUSED) {
 	      /* ACK the SYN, send RST to refuse the connection */
 	      tcp_respond(tp, ti, m, ti->ti_seq + 1, (tcp_seq) 0,
@@ -1011,7 +1010,7 @@ trimthenstep6:
 
 		if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) {
 			if (ti->ti_len == 0 && tiwin == tp->snd_wnd) {
-			  DEBUG_MISC(" dup ack  m = %p  so = %p\n", m, so);
+			  DEBUG_MISC(" dup ack  m = %p  so = %p", m, so);
 				/*
 				 * If we have outstanding data (other than
 				 * a window probe), this is a completely
@@ -1550,7 +1549,7 @@ tcp_mss(struct tcpcb *tp, u_int offer)
                                                (mss - (TCP_RCVSPACE % mss)) :
                                                0));
 
-	DEBUG_MISC(" returning mss = %d\n", mss);
+	DEBUG_MISC(" returning mss = %d", mss);
 
 	return mss;
 }
slirp/tcp_output.c +1 -1
index 44da8a4e47..2f7f90d67e 100644
@@ -92,7 +92,7 @@ again:
 
 	flags = tcp_outflags[tp->t_state];
 
-	DEBUG_MISC(" --- tcp_output flags = 0x%x\n", flags);
+	DEBUG_MISC(" --- tcp_output flags = 0x%x", flags);
 
 	/*
 	 * If in persist timeout with window of 0, send 1 byte.
slirp/tcp_subr.c +1 -1
index 042fa1e4fb..5dcf979c3c 100644
@@ -964,7 +964,7 @@ int tcp_ctl(struct socket *so)
                     so->guestfwd = ex_ptr;
                     return 1;
                 }
-                DEBUG_MISC(" executing %s\n", ex_ptr->ex_exec);
+                DEBUG_MISC(" executing %s", ex_ptr->ex_exec);
                 return fork_exec(so, ex_ptr->ex_exec);
             }
         }
slirp/udp.c +2 -3
index a45ad81dda..4cf0d34d64 100644
@@ -172,8 +172,7 @@ udp_input(register struct mbuf *m, int iphlen)
 	   */
 	  so = socreate(slirp);
 	  if (udp_attach(so, AF_INET) == -1) {
-	    DEBUG_MISC(" udp_attach errno = %d-%s\n",
-                   errno, strerror(errno));
+	    DEBUG_MISC(" udp_attach errno = %d-%s", errno, strerror(errno));
 	    sofree(so);
 	    goto bad;
 	  }
@@ -209,7 +208,7 @@ udp_input(register struct mbuf *m, int iphlen)
 	  m->m_len += iphlen;
 	  m->m_data -= iphlen;
 	  *ip=save_ip;
-	  DEBUG_MISC("udp tx errno = %d-%s\n", errno, strerror(errno));
+	  DEBUG_MISC("udp tx errno = %d-%s", errno, strerror(errno));
 	  icmp_send_error(m, ICMP_UNREACH, ICMP_UNREACH_NET, 0,
 	                  strerror(errno));
 	  goto bad;
slirp/udp6.c +2 -3
index 8cdb1892e2..fa531e03c4 100644
@@ -92,8 +92,7 @@ void udp6_input(struct mbuf *m)
         /* If there's no socket for this packet, create one. */
         so = socreate(slirp);
         if (udp_attach(so, AF_INET6) == -1) {
-            DEBUG_MISC(" udp6_attach errno = %d-%s\n",
-                       errno, strerror(errno));
+            DEBUG_MISC(" udp6_attach errno = %d-%s", errno, strerror(errno));
             sofree(so);
             goto bad;
         }
@@ -119,7 +118,7 @@ void udp6_input(struct mbuf *m)
         m->m_len += iphlen;
         m->m_data -= iphlen;
         *ip = save_ip;
-        DEBUG_MISC("udp tx errno = %d-%s\n", errno, strerror(errno));
+        DEBUG_MISC("udp tx errno = %d-%s", errno, strerror(errno));
         icmp6_send_error(m, ICMP6_UNREACH, ICMP6_UNREACH_NO_ROUTE);
         goto bad;
     }
Signature · 1 line
2.20.0.rc1
#46 lore raw

Re: [Qemu-devel] [PATCH v2 for-3.2 v2 24/30] slirp: call into g_debug() for DEBUG macros

Marc-André Lureau, le jeu. 22 nov. 2018 02:06:41 +0400, a ecrit:

Quoted 9 lines

Make slirp use GLib logging, instead of fprintf(), so that applications can filter log, process it etc.

With recent versions of glib, G_MESSAGES_DEBUG must be set to "all" or "Slirp" to see slirp debug messages.

Reformat DEBUG_MISC & DEBUG_ERROR calls to not need \n ending.

Applied to my tree, thanks!

#47 lore raw

PATCH v2 25/30 slirp: replace trace functions with DEBUG calls

In reply to Marc-André Lureau

Remove a dependency on QEMU. Use the existing logging facilities. Set SLIRP_DEBUG=tftp to get tftp log.

---
 slirp/debug.h      | 13 ++++++++++---
 slirp/slirp.c      |  1 +
 slirp/tftp.c       |  7 ++++---
 Makefile.objs      |  1 -
 slirp/trace-events |  5 -----
 5 files changed, 15 insertions(+), 12 deletions(-)
 delete mode 100644 slirp/trace-events
slirp/debug.h +10 -3
index 25a5d59439..20557f9866 100644
@@ -8,9 +8,10 @@
 #ifndef DEBUG_H_
 #define DEBUG_H_
 
-#define DBG_CALL 0x1
-#define DBG_MISC 0x2
-#define DBG_ERROR 0x4
+#define DBG_CALL (1 << 0)
+#define DBG_MISC (1 << 1)
+#define DBG_ERROR (1 << 2)
+#define DBG_TFTP (1 << 3)
 
 extern int slirp_debug;
 
@@ -38,4 +39,10 @@ extern int slirp_debug;
     }                                           \
 } while (0)
 
+#define DEBUG_TFTP(fmt, ...) do {               \
+    if (slirp_debug & DBG_TFTP) {               \
+        g_debug(fmt, ##__VA_ARGS__);            \
+    }                                           \
+} while (0)
+
 #endif /* DEBUG_H_ */
slirp/slirp.c +1 -0
index f254a23a5a..cd0ad81f26 100644
@@ -270,6 +270,7 @@ static void slirp_init_once(void)
             { "call", DBG_CALL },
             { "misc", DBG_MISC },
             { "error", DBG_ERROR },
+            { "tftp", DBG_TFTP },
         };
         slirp_debug = g_parse_debug_string(debug, keys, G_N_ELEMENTS(keys));
     }
slirp/tftp.c +4 -3
index a9ba1480db..6fb381ef33 100644
@@ -26,7 +26,6 @@
 #include "slirp.h"
 #include "qemu-common.h"
 #include "qemu/cutils.h"
-#include "trace.h"
 
 static inline int tftp_session_in_use(struct tftp_session *spt)
 {
@@ -205,7 +204,8 @@ static void tftp_send_error(struct tftp_session *spt,
   struct mbuf *m;
   struct tftp_t *tp;
 
-  trace_slirp_tftp_error(msg);
+  DEBUG_TFTP("tftp error msg: %s", msg);
+
   m = m_get(spt->slirp);
 
   if (!m) {
@@ -325,7 +325,8 @@ static void tftp_handle_rrq(Slirp *slirp, struct sockaddr_storage *srcsas,
       break;
     }
   }
-  trace_slirp_tftp_rrq(req_fname);
+
+  DEBUG_TFTP("tftp rrq file: %s", req_fname);
 
   /* check mode */
   if ((pktlen - k) < 6) {
Makefile.objs +0 -1
index a11050a7aa..1cb2faab5e 100644
@@ -250,7 +250,6 @@ trace-events-subdirs += net
 trace-events-subdirs += qapi
 trace-events-subdirs += qom
 trace-events-subdirs += scsi
-trace-events-subdirs += slirp
 trace-events-subdirs += target/arm
 trace-events-subdirs += target/i386
 trace-events-subdirs += target/mips
slirp/trace-events deleted +0 -5
deleted file mode 100644
index ff8f656e8c..0000000000
@@ -1,5 +0,0 @@
-# See docs/devel/tracing.txt for syntax documentation.
-
-# slirp/tftp.c
-slirp_tftp_rrq(const char *file) "file: %s"
-slirp_tftp_error(const char *file) "msg: %s"
Signature · 1 line
2.20.0.rc1
#48 lore raw

Re: [Qemu-devel] [PATCH v2 for-3.2 v2 25/30] slirp: replace trace functions with DEBUG calls

Hello,

Marc-André Lureau, le jeu. 22 nov. 2018 02:06:42 +0400, a ecrit:

Quoted 2 lines

Remove a dependency on QEMU. Use the existing logging facilities. Set SLIRP_DEBUG=tftp to get tftp log.

As mentioned before, I was hesitating to replace the tracing with a DEBUG call. Now that I have merged to my tree the DEBUG rework which allows to just set SLIRP_DEBUG=tftp to get tftp log, Gerd, are you fine with this replacement?

Samuel

Quoted 109 lines
---
 slirp/debug.h      | 13 ++++++++++---
 slirp/slirp.c      |  1 +
 slirp/tftp.c       |  7 ++++---
 Makefile.objs      |  1 -
 slirp/trace-events |  5 -----
 5 files changed, 15 insertions(+), 12 deletions(-)
 delete mode 100644 slirp/trace-events
slirp/debug.h +10 -3
index 25a5d59439..20557f9866 100644
@@ -8,9 +8,10 @@
 #ifndef DEBUG_H_
 #define DEBUG_H_
 
-#define DBG_CALL 0x1
-#define DBG_MISC 0x2
-#define DBG_ERROR 0x4
+#define DBG_CALL (1 << 0)
+#define DBG_MISC (1 << 1)
+#define DBG_ERROR (1 << 2)
+#define DBG_TFTP (1 << 3)
 
 extern int slirp_debug;
 
@@ -38,4 +39,10 @@ extern int slirp_debug;
     }                                           \
 } while (0)
 
+#define DEBUG_TFTP(fmt, ...) do {               \
+    if (slirp_debug & DBG_TFTP) {               \
+        g_debug(fmt, ##__VA_ARGS__);            \
+    }                                           \
+} while (0)
+
 #endif /* DEBUG_H_ */
slirp/slirp.c +1 -0
index f254a23a5a..cd0ad81f26 100644
@@ -270,6 +270,7 @@ static void slirp_init_once(void)
             { "call", DBG_CALL },
             { "misc", DBG_MISC },
             { "error", DBG_ERROR },
+            { "tftp", DBG_TFTP },
         };
         slirp_debug = g_parse_debug_string(debug, keys, G_N_ELEMENTS(keys));
     }
slirp/tftp.c +4 -3
index a9ba1480db..6fb381ef33 100644
@@ -26,7 +26,6 @@
 #include "slirp.h"
 #include "qemu-common.h"
 #include "qemu/cutils.h"
-#include "trace.h"
 
 static inline int tftp_session_in_use(struct tftp_session *spt)
 {
@@ -205,7 +204,8 @@ static void tftp_send_error(struct tftp_session *spt,
   struct mbuf *m;
   struct tftp_t *tp;
 
-  trace_slirp_tftp_error(msg);
+  DEBUG_TFTP("tftp error msg: %s", msg);
+
   m = m_get(spt->slirp);
 
   if (!m) {
@@ -325,7 +325,8 @@ static void tftp_handle_rrq(Slirp *slirp, struct sockaddr_storage *srcsas,
       break;
     }
   }
-  trace_slirp_tftp_rrq(req_fname);
+
+  DEBUG_TFTP("tftp rrq file: %s", req_fname);
 
   /* check mode */
   if ((pktlen - k) < 6) {
Makefile.objs +0 -1
index a11050a7aa..1cb2faab5e 100644
@@ -250,7 +250,6 @@ trace-events-subdirs += net
 trace-events-subdirs += qapi
 trace-events-subdirs += qom
 trace-events-subdirs += scsi
-trace-events-subdirs += slirp
 trace-events-subdirs += target/arm
 trace-events-subdirs += target/i386
 trace-events-subdirs += target/mips
slirp/trace-events deleted +0 -5
deleted file mode 100644
index ff8f656e8c..0000000000
@@ -1,5 +0,0 @@
-# See docs/devel/tracing.txt for syntax documentation.
-
-# slirp/tftp.c
-slirp_tftp_rrq(const char *file) "file: %s"
-slirp_tftp_error(const char *file) "msg: %s"
Signature · 1 line
2.20.0.rc1
Signature · 3 lines
Samuel
In mutt, type cthis
Dans mutt, taper cceci
#49 lore raw

Re: [Qemu-devel] [PATCH v2 for-3.2 v2 25/30] slirp: replace trace functions with DEBUG calls

Quoted 11 lines · Samuel Thibault

Hello,

Marc-André Lureau, le jeu. 22 nov. 2018 02:06:42 +0400, a ecrit:

Quoted 2 lines

Remove a dependency on QEMU. Use the existing logging facilities. Set SLIRP_DEBUG=tftp to get tftp log.

As mentioned before, I was hesitating to replace the tracing with a DEBUG call. Now that I have merged to my tree the DEBUG rework which allows to just set SLIRP_DEBUG=tftp to get tftp log, Gerd, are you fine with this replacement?

Hmm, I'd prefer tracepoints, but I can see that this will be rather tricky to do when transforming slirp into a standalone library ...

So, yes, ok, go ahead.

cheers,
Gerd

#50 lore raw

PATCH v2 26/30 slirp: add a set_nonblock() callback

In reply to Marc-André Lureau

qemu_set_nonblock() does some event registration with the main loop on win32, let's have a callback.

---
 slirp/libslirp.h | 2 ++
 net/slirp.c      | 1 +
 slirp/misc.c     | 2 +-
 slirp/tcp_subr.c | 4 ++--
 4 files changed, 6 insertions(+), 3 deletions(-)
slirp/libslirp.h +2 -0
index 3e75dadfa3..e79a2d2b11 100644
@@ -27,6 +27,8 @@ typedef struct SlirpCb {
     void (*timer_free)(void *timer);
     /* Modify a timer to expire at @expire_time */
     void (*timer_mod)(void *timer, int64_t expire_time);
+    /* Put a fd in nonblocking mode and register it for future polling */
+    void (*set_nonblock)(int fd);
 } SlirpCb;
 
 
net/slirp.c +1 -0
index f7cdf8398b..a31193a25a 100644
@@ -193,6 +193,7 @@ static const SlirpCb slirp_cb = {
     .timer_new = net_slirp_timer_new,
     .timer_free = net_slirp_timer_free,
     .timer_mod = net_slirp_timer_mod,
+    .set_nonblock = qemu_set_nonblock,
 };
 
 static int net_slirp_init(NetClientState *peer, const char *model,
slirp/misc.c +1 -1
index f9b2c64541..7789cefefa 100644
@@ -172,7 +172,7 @@ fork_exec(struct socket *so, const char *ex)
     socket_set_fast_reuse(so->s);
     opt = 1;
     qemu_setsockopt(so->s, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(int));
-    qemu_set_nonblock(so->s);
+    so->slirp->cb->set_nonblock(so->s);
     return 1;
 }
 #endif
slirp/tcp_subr.c +2 -2
index 5dcf979c3c..3b15148187 100644
@@ -412,7 +412,7 @@ int tcp_fconnect(struct socket *so, unsigned short af)
     int opt, s=so->s;
     struct sockaddr_storage addr;
 
-    qemu_set_nonblock(s);
+    so->slirp->cb->set_nonblock(s);
     socket_set_fast_reuse(s);
     opt = 1;
     qemu_setsockopt(s, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(opt));
@@ -484,7 +484,7 @@ void tcp_connect(struct socket *inso)
         tcp_close(sototcpcb(so)); /* This will sofree() as well */
         return;
     }
-    qemu_set_nonblock(s);
+    so->slirp->cb->set_nonblock(s);
     socket_set_fast_reuse(s);
     opt = 1;
     qemu_setsockopt(s, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(int));
Signature · 1 line
2.20.0.rc1
#51 lore raw

PATCH v2 27/30 slirp: replace qemu_notify_event() with a callback

In reply to Marc-André Lureau

Introduce a SlirpCb callback to kick the main io-thread.

Add an intermediary sodrop() function that will call SlirpCb.notify callback when sbdrop() returns true.

---
 slirp/libslirp.h  | 2 ++
 slirp/sbuf.h      | 2 +-
 slirp/socket.h    | 1 +
 net/slirp.c       | 1 +
 slirp/sbuf.c      | 6 ++++--
 slirp/socket.c    | 7 +++++++
 slirp/tcp_input.c | 6 +++---
 7 files changed, 19 insertions(+), 6 deletions(-)
slirp/libslirp.h +2 -0
index e79a2d2b11..f2d71bd1f6 100644
@@ -29,6 +29,8 @@ typedef struct SlirpCb {
     void (*timer_mod)(void *timer, int64_t expire_time);
     /* Put a fd in nonblocking mode and register it for future polling */
     void (*set_nonblock)(int fd);
+    /* Kick the io-thread, to signal that new events may be processed */
+    void (*notify)(void);
 } SlirpCb;
 
 
slirp/sbuf.h +1 -1
index 644c201341..1cb9a42834 100644
@@ -21,7 +21,7 @@ struct sbuf {
 };
 
 void sbfree(struct sbuf *);
-void sbdrop(struct sbuf *, int);
+bool sbdrop(struct sbuf *, int);
 void sbreserve(struct sbuf *, int);
 void sbappend(struct socket *, struct mbuf *);
 void sbcopy(struct sbuf *, int, int, char *);
slirp/socket.h +1 -0
index fc35ca5f72..1c1c8b5871 100644
@@ -156,6 +156,7 @@ int soreadbuf(struct socket *so, const char *buf, int size);
 void sotranslate_out(struct socket *, struct sockaddr_storage *);
 void sotranslate_in(struct socket *, struct sockaddr_storage *);
 void sotranslate_accept(struct socket *);
+void sodrop(struct socket *, int num);
 
 
 #endif /* SLIRP_SOCKET_H */
net/slirp.c +1 -0
index a31193a25a..fbdc6ea9a0 100644
@@ -194,6 +194,7 @@ static const SlirpCb slirp_cb = {
     .timer_free = net_slirp_timer_free,
     .timer_mod = net_slirp_timer_mod,
     .set_nonblock = qemu_set_nonblock,
+    .notify = qemu_notify_event,
 };
 
 static int net_slirp_init(NetClientState *peer, const char *model,
slirp/sbuf.c +4 -2
index 912f235f65..17f28e97a6 100644
@@ -17,7 +17,7 @@ sbfree(struct sbuf *sb)
 	free(sb->sb_data);
 }
 
-void
+bool
 sbdrop(struct sbuf *sb, int num)
 {
     int limit = sb->sb_datalen / 2;
@@ -34,8 +34,10 @@ sbdrop(struct sbuf *sb, int num)
 		sb->sb_rptr -= sb->sb_datalen;
 
     if (sb->sb_cc < limit && sb->sb_cc + num >= limit) {
-        qemu_notify_event();
+        return true;
     }
+
+    return false;
 }
 
 void
slirp/socket.c +7 -0
index 5ffbaa064a..4d5be38747 100644
@@ -928,3 +928,10 @@ void sotranslate_accept(struct socket *so)
         break;
     }
 }
+
+void sodrop(struct socket *s, int num)
+{
+    if (sbdrop(&s->so_snd, num)) {
+        s->slirp->cb->notify();
+    }
+}
slirp/tcp_input.c +3 -3
index a6596b63ac..f205a1e6cb 100644
@@ -506,7 +506,7 @@ findso:
 				    SEQ_GT(ti->ti_ack, tp->t_rtseq))
 					tcp_xmit_timer(tp, tp->t_rtt);
 				acked = ti->ti_ack - tp->snd_una;
-				sbdrop(&so->so_snd, acked);
+				sodrop(so, acked);
 				tp->snd_una = ti->ti_ack;
 				m_free(m);
 
@@ -1118,10 +1118,10 @@ trimthenstep6:
 		}
 		if (acked > so->so_snd.sb_cc) {
 			tp->snd_wnd -= so->so_snd.sb_cc;
-			sbdrop(&so->so_snd, (int )so->so_snd.sb_cc);
+			sodrop(so, (int)so->so_snd.sb_cc);
 			ourfinisacked = 1;
 		} else {
-			sbdrop(&so->so_snd, acked);
+			sodrop(so, acked);
 			tp->snd_wnd -= acked;
 			ourfinisacked = 0;
 		}
Signature · 1 line
2.20.0.rc1
#52 lore raw

PATCH v2 28/30 slirp: move QEMU state saving to a separate unit

In reply to Marc-André Lureau

Make state saving optional: this will allow to build SLIRP without QEMU. (eventually, the vmstate helpers will be extracted, so an external project & process could save its state)

---
 slirp/slirp.h       |   3 +
 slirp/state.h       |   9 +
 slirp/slirp.c       | 372 ++---------------------------------------
 slirp/state.c       | 394 ++++++++++++++++++++++++++++++++++++++++++++
 slirp/Makefile.objs |   3 +-
 5 files changed, 419 insertions(+), 362 deletions(-)
 create mode 100644 slirp/state.h
 create mode 100644 slirp/state.c
slirp/slirp.h +3 -0
index 17056f4b83..6e7a663b6f 100644
@@ -270,4 +270,7 @@ int tcp_emu(struct socket *, struct mbuf *);
 int tcp_ctl(struct socket *);
 struct tcpcb *tcp_drop(struct tcpcb *tp, int err);
 
+struct socket *
+slirp_find_ctl_socket(Slirp *slirp, struct in_addr guest_addr, int guest_port);
+
 #endif
slirp/state.h new file +9 -0
new file mode 100644
index 0000000000..154866898f
@@ -0,0 +1,9 @@
+#ifndef SLIRP_STATE_H_
+#define SLIRP_STATE_H_
+
+#include "libslirp.h"
+
+void slirp_state_register(Slirp *slirp);
+void slirp_state_unregister(Slirp *slirp);
+
+#endif /* SLIRP_STATE_H_ */
slirp/slirp.c +11 -361

405 lines not on this page · raw message

slirp/state.c new file +394 -0

394 lines not on this page · raw message

slirp/Makefile.objs +2 -1
index 959558c732..7abc7549ab 100644
@@ -20,6 +20,7 @@ slirp.mo-objs = \
 	sbuf.o \
 	slirp.o \
 	socket.o \
+	state.o \
 	tcp_input.o \
 	tcp_output.o \
 	tcp_subr.o \
@@ -29,4 +30,4 @@ slirp.mo-objs = \
 	udp6.o \
 	$(NULL)
 
-slirp.mo-cflags = -DG_LOG_DOMAIN=\"Slirp\"
+slirp.mo-cflags = -DG_LOG_DOMAIN=\"Slirp\" -DWITH_QEMU=1
Signature · 1 line
2.20.0.rc1
#53 lore raw

PATCH v2 29/30 slirp: replace remaining QEMU dependency

In reply to Marc-André Lureau

Introduce utility header/object, and replace remaining qemu functions with SLIRP helpers.

---
 slirp/ip.h          |  14 +--
 slirp/ip6.h         |   5 +-
 slirp/ip6_icmp.h    |  16 ++--
 slirp/libslirp.h    |  12 ++-
 slirp/qtailq.h      | 218 ++++++++++++++++++++++++++++++++++++++++++++
 slirp/slirp.h       |  12 +--
 slirp/util.h        | 148 ++++++++++++++++++++++++++++++
 slirp/arp_table.c   |   1 -
 slirp/bootp.c       |   1 -
 slirp/cksum.c       |   1 -
 slirp/dhcpv6.c      |   2 -
 slirp/dnssearch.c   |   1 -
 slirp/if.c          |   2 -
 slirp/ip6_icmp.c    |   4 -
 slirp/ip6_input.c   |   1 -
 slirp/ip6_output.c  |   2 -
 slirp/ip_icmp.c     |   7 +-
 slirp/ip_input.c    |   1 -
 slirp/ip_output.c   |   1 -
 slirp/mbuf.c        |   1 -
 slirp/misc.c        |  21 ++---
 slirp/ncsi.c        |   1 -
 slirp/ndp_table.c   |   2 -
 slirp/sbuf.c        |   2 -
 slirp/slirp.c       |   7 --
 slirp/socket.c      |  18 ++--
 slirp/tcp_input.c   |   1 -
 slirp/tcp_output.c  |   1 -
 slirp/tcp_subr.c    |  13 ++-
 slirp/tcp_timer.c   |   1 -
 slirp/tftp.c        |   7 +-
 slirp/udp.c         |   7 +-
 slirp/udp6.c        |   2 -
 slirp/util.c        | 176 +++++++++++++++++++++++++++++++++++
 slirp/Makefile.objs |   1 +
 35 files changed, 609 insertions(+), 101 deletions(-)
 create mode 100644 slirp/qtailq.h
 create mode 100644 slirp/util.h
 create mode 100644 slirp/util.c
slirp/ip.h +7 -7
index 243b6c8b24..cd6ddf2bb7 100644
@@ -89,7 +89,7 @@ struct ip {
 	uint8_t ip_p;			/* protocol */
 	uint16_t	ip_sum;			/* checksum */
 	struct	in_addr ip_src,ip_dst;	/* source and dest address */
-} QEMU_PACKED;
+} SLIRP_PACKED;
 
 #define	IP_MAXPACKET	65535		/* maximum packet size */
 
@@ -151,7 +151,7 @@ struct	ip_timestamp {
 			n_long ipt_time;
 		} ipt_ta[1];
 	} ipt_timestamp;
-} QEMU_PACKED;
+} SLIRP_PACKED;
 
 /* flag bits for ipt_flg */
 #define	IPOPT_TS_TSONLY		0		/* timestamps only */
@@ -181,11 +181,11 @@ struct	ip_timestamp {
 struct mbuf_ptr {
 	struct mbuf *mptr;
 	uint32_t dummy;
-} QEMU_PACKED;
+} SLIRP_PACKED;
 #else
 struct mbuf_ptr {
 	struct mbuf *mptr;
-} QEMU_PACKED;
+} SLIRP_PACKED;
 #endif
 struct qlink {
 	void *next, *prev;
@@ -201,7 +201,7 @@ struct ipovly {
 	uint16_t	ih_len;			/* protocol length */
 	struct	in_addr ih_src;		/* source internet address */
 	struct	in_addr ih_dst;		/* destination internet address */
-} QEMU_PACKED;
+} SLIRP_PACKED;
 
 /*
  * Ip reassembly queue structure.  Each fragment
@@ -217,7 +217,7 @@ struct ipq {
 	uint8_t	ipq_p;			/* protocol of this fragment */
 	uint16_t	ipq_id;			/* sequence id for reassembly */
 	struct	in_addr ipq_src,ipq_dst;
-} QEMU_PACKED;
+} SLIRP_PACKED;
 
 /*
  * Ip header, when holding a fragment.
@@ -227,7 +227,7 @@ struct ipq {
 struct	ipasfrag {
 	struct qlink ipf_link;
 	struct ip ipf_ip;
-} QEMU_PACKED;
+} SLIRP_PACKED;
 
 #define ipf_off      ipf_ip.ip_off
 #define ipf_tos      ipf_ip.ip_tos
slirp/ip6.h +2 -3
index 14e9c78735..e0a13dec1c 100644
@@ -7,7 +7,6 @@
 #define SLIRP_IP6_H
 
 #include <glib.h>
-#include "net/eth.h"
 
 #define ALLNODES_MULTICAST  { .s6_addr = \
                             { 0xff, 0x02, 0x00, 0x00,\
@@ -133,7 +132,7 @@ struct ip6 {
     uint8_t     ip_nh;               /* next header */
     uint8_t     ip_hl;               /* hop limit */
     struct in6_addr ip_src, ip_dst;  /* source and dest address */
-} QEMU_PACKED;
+} SLIRP_PACKED;
 
 /*
  * IPv6 pseudo-header used by upper-layer protocols
@@ -145,7 +144,7 @@ struct ip6_pseudohdr {
     uint16_t    ih_zero_hi;       /* zero */
     uint8_t     ih_zero_lo;       /* zero */
     uint8_t     ih_nh;            /* next header */
-} QEMU_PACKED;
+} SLIRP_PACKED;
 
 
 #endif
slirp/ip6_icmp.h +8 -8
index 32b0914055..1f09b485e3 100644
@@ -48,12 +48,12 @@ struct ndp_ra {     /* Router Advertisement Message */
     uint16_t lifetime;      /* Router Lifetime */
     uint32_t reach_time;    /* Reachable Time */
     uint32_t retrans_time;  /* Retrans Timer */
-} QEMU_PACKED;
+} SLIRP_PACKED;
 
 struct ndp_ns {     /* Neighbor Solicitation Message */
     uint32_t reserved;
     struct in6_addr target; /* Target Address */
-} QEMU_PACKED;
+} SLIRP_PACKED;
 
 struct ndp_na {     /* Neighbor Advertisement Message */
 #if G_BYTE_ORDER == G_BIG_ENDIAN
@@ -72,13 +72,13 @@ struct ndp_na {     /* Neighbor Advertisement Message */
         reserved_lo:24;
 #endif
     struct in6_addr target; /* Target Address */
-} QEMU_PACKED;
+} SLIRP_PACKED;
 
 struct ndp_redirect {
     uint32_t reserved;
     struct in6_addr target; /* Target Address */
     struct in6_addr dest;   /* Destination Address */
-} QEMU_PACKED;
+} SLIRP_PACKED;
 
 /*
  * Structure of an icmpv6 header.
@@ -103,7 +103,7 @@ struct icmp6 {
 #define icmp6_nns icmp6_body.ndp_ns
 #define icmp6_nna icmp6_body.ndp_na
 #define icmp6_redirect icmp6_body.ndp_redirect
-} QEMU_PACKED;
+} SLIRP_PACKED;
 
 #define ICMP6_MINLEN    4
 #define ICMP6_ERROR_MINLEN  8
@@ -134,16 +134,16 @@ struct ndpopt {
             uint32_t    pref_lt;                /* Preferred Lifetime */
             uint32_t    reserved2;
             struct in6_addr prefix;
-        } QEMU_PACKED prefixinfo;
+        } SLIRP_PACKED prefixinfo;
 #define ndpopt_prefixinfo ndpopt_body.prefixinfo
         struct rdnss {
             uint16_t reserved;
             uint32_t lifetime;
             struct in6_addr addr;
-        } QEMU_PACKED rdnss;
+        } SLIRP_PACKED rdnss;
 #define ndpopt_rdnss ndpopt_body.rdnss
     } ndpopt_body;
-} QEMU_PACKED;
+} SLIRP_PACKED;
 
 /* NDP options type */
 #define NDPOPT_LINKLAYER_SOURCE     1   /* Source Link-Layer Address */
slirp/libslirp.h +11 -1
index f2d71bd1f6..2c90738f89 100644
@@ -1,7 +1,17 @@
 #ifndef LIBSLIRP_H
 #define LIBSLIRP_H
 
-#include "qemu-common.h"
+#include <glib.h>
+#include <stdint.h>
+#include <stdbool.h>
+
+#ifdef _WIN32
+#include <winsock2.h>
+#include <in6addr.h>
+#else
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#endif
 
 typedef struct Slirp Slirp;
 
slirp/qtailq.h new file +218 -0

218 lines not on this page · raw message

slirp/slirp.h +5 -7
index 6e7a663b6f..08ec331ee5 100644
@@ -5,8 +5,8 @@
 
 typedef char *caddr_t;
 
-# include <windows.h>
 # include <winsock2.h>
+# include <windows.h>
 # include <ws2tcpip.h>
 # include <sys/timeb.h>
 # include <iphlpapi.h>
@@ -45,10 +45,8 @@ typedef char *caddr_t;
 #define quehead slirp_quehead
 
 #include "debug.h"
-
-#include "qemu/queue.h"
-#include "qemu/sockets.h"
-#include "net/eth.h"
+#include "util.h"
+#include "qtailq.h"
 
 #include "libslirp.h"
 #include "ip.h"
@@ -93,7 +91,7 @@ struct slirp_arphdr {
     uint32_t      ar_sip;           /* sender IP address       */
     unsigned char ar_tha[ETH_ALEN]; /* target hardware address */
     uint32_t      ar_tip;           /* target IP address       */
-} QEMU_PACKED;
+} SLIRP_PACKED;
 
 #define ARP_TABLE_SIZE 16
 
@@ -110,7 +108,7 @@ bool arp_table_search(Slirp *slirp, uint32_t ip_addr,
 struct ndpentry {
     unsigned char   eth_addr[ETH_ALEN];     /* sender hardware address */
     struct in6_addr ip_addr;                /* sender IP address       */
-} QEMU_PACKED;
+} SLIRP_PACKED;
 
 #define NDP_TABLE_SIZE 16
 
slirp/util.h new file +148 -0

148 lines not on this page · raw message

slirp/arp_table.c +0 -1
index bf71b984ad..8ea655f79d 100644
@@ -22,7 +22,6 @@
  * THE SOFTWARE.
  */
 
-#include "qemu/osdep.h"
 #include "slirp.h"
 
 void arp_table_add(Slirp *slirp, uint32_t ip_addr, uint8_t ethaddr[ETH_ALEN])
slirp/bootp.c +0 -1
index 4c9a77eb98..d396849a05 100644
@@ -21,7 +21,6 @@
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  * THE SOFTWARE.
  */
-#include "qemu/osdep.h"
 #include "slirp.h"
 
 #if defined(_WIN32)
slirp/cksum.c +0 -1
index 84c858fafb..25bfa67348 100644
@@ -30,7 +30,6 @@
  * in_cksum.c,v 1.2 1994/08/02 07:48:16 davidg Exp
  */
 
-#include "qemu/osdep.h"
 #include "slirp.h"
 
 /*
slirp/dhcpv6.c +0 -2
index 752df40536..9ffba38e8f 100644
@@ -20,8 +20,6 @@
  * along with this program; if not, see <http://www.gnu.org/licenses/>.
  */
 
-#include "qemu/osdep.h"
-#include "qemu/log.h"
 #include "slirp.h"
 #include "dhcpv6.h"
 
slirp/dnssearch.c +0 -1
index 8fb563321b..c459cece8d 100644
@@ -22,7 +22,6 @@
  * THE SOFTWARE.
  */
 
-#include "qemu/osdep.h"
 #include "slirp.h"
 
 static const uint8_t RFC3397_OPT_DOMAIN_SEARCH = 119;
slirp/if.c +0 -2
index 73e3705740..2ad03b8a79 100644
@@ -5,9 +5,7 @@
  * terms and conditions of the copyright.
  */
 
-#include "qemu/osdep.h"
 #include "slirp.h"
-#include "qemu/timer.h"
 
 static void
 ifs_insque(struct mbuf *ifm, struct mbuf *ifmhead)
slirp/ip6_icmp.c +0 -4
index e72c57a81d..2a432ebbd4 100644
@@ -3,12 +3,8 @@
  * Guillaume Subiron, Yann Bordenave, Serigne Modou Wagne.
  */
 
-#include "qemu/osdep.h"
 #include "slirp.h"
 #include "ip6_icmp.h"
-#include "qemu/timer.h"
-#include "qemu/error-report.h"
-#include "qemu/log.h"
 
 #define NDP_Interval g_rand_int_range(slirp->grand, \
         NDP_MinRtrAdvInterval, NDP_MaxRtrAdvInterval)
slirp/ip6_input.c +0 -1
index ab656a0a9d..1b8c003c66 100644
@@ -3,7 +3,6 @@
  * Guillaume Subiron, Yann Bordenave, Serigne Modou Wagne.
  */
 
-#include "qemu/osdep.h"
 #include "slirp.h"
 #include "ip6_icmp.h"
 
slirp/ip6_output.c +0 -2
index 52c88ad691..19d1ae7748 100644
@@ -3,8 +3,6 @@
  * Guillaume Subiron, Yann Bordenave, Serigne Modou Wagne.
  */
 
-#include "qemu/osdep.h"
-#include "qemu-common.h"
 #include "slirp.h"
 
 /* Number of packets queued before we start sending
slirp/ip_icmp.c +3 -4
index 7c7e042049..e2e673ceaa 100644
@@ -30,7 +30,6 @@
  * ip_icmp.c,v 1.7 1995/05/30 08:09:42 rgrimes Exp
  */
 
-#include "qemu/osdep.h"
 #include "slirp.h"
 #include "ip_icmp.h"
 
@@ -83,7 +82,7 @@ static int icmp_send(struct socket *so, struct mbuf *m, int hlen)
     struct ip *ip = mtod(m, struct ip *);
     struct sockaddr_in addr;
 
-    so->s = qemu_socket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP);
+    so->s = slirp_socket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP);
     if (so->s == -1) {
         return -1;
     }
@@ -114,7 +113,7 @@ static int icmp_send(struct socket *so, struct mbuf *m, int hlen)
 
 void icmp_detach(struct socket *so)
 {
-    closesocket(so->s);
+    slirp_closesocket(so->s);
     sofree(so);
 }
 
@@ -421,7 +420,7 @@ void icmp_receive(struct socket *so)
     icp = mtod(m, struct icmp *);
 
     id = icp->icmp_id;
-    len = qemu_recv(so->s, icp, M_ROOM(m), 0);
+    len = slirp_recv(so->s, icp, M_ROOM(m), 0);
     /*
      * The behavior of reading SOCK_DGRAM+IPPROTO_ICMP sockets is inconsistent
      * between host OSes.  On Linux, only the ICMP header and payload is
slirp/ip_input.c +0 -1
index 6831526320..cfda30fccd 100644
@@ -38,7 +38,6 @@
  * terms and conditions of the copyright.
  */
 
-#include "qemu/osdep.h"
 #include "slirp.h"
 #include "ip_icmp.h"
 
slirp/ip_output.c +0 -1
index db403f04c1..f6ec141df5 100644
@@ -38,7 +38,6 @@
  * terms and conditions of the copyright.
  */
 
-#include "qemu/osdep.h"
 #include "slirp.h"
 
 /* Number of packets queued before we start sending
slirp/mbuf.c +0 -1
index d8d275e0e7..521c02c967 100644
@@ -15,7 +15,6 @@
  * the flags
  */
 
-#include "qemu/osdep.h"
 #include "slirp.h"
 
 #define MBUF_THRESH 30
slirp/misc.c +9 -12
index 7789cefefa..5b1c526f17 100644
@@ -5,11 +5,8 @@
  * terms and conditions of the copyright.
  */
 
-#include "qemu/osdep.h"
 #include "slirp.h"
 #include "libslirp.h"
-#include "qemu/error-report.h"
-#include "qemu/main-loop.h"
 
 inline void
 insque(void *a, void *b)
@@ -83,14 +80,14 @@ slirp_socketpair_with_oob(int sv[2])
     int ret, s;
 
     sv[1] = -1;
-    s = qemu_socket(AF_INET, SOCK_STREAM, 0);
+    s = slirp_socket(AF_INET, SOCK_STREAM, 0);
     if (s < 0 || bind(s, (struct sockaddr *)&addr, addrlen) < 0 ||
         listen(s, 1) < 0 ||
         getsockname(s, (struct sockaddr *)&addr, &addrlen) < 0) {
         goto err;
     }
 
-    sv[1] = qemu_socket(AF_INET, SOCK_STREAM, 0);
+    sv[1] = slirp_socket(AF_INET, SOCK_STREAM, 0);
     if (sv[1] < 0) {
         goto err;
     }
@@ -113,16 +110,16 @@ slirp_socketpair_with_oob(int sv[2])
         goto err;
     }
 
-    closesocket(s);
+    slirp_closesocket(s);
     return 0;
 
 err:
     g_critical("slirp_socketpair(): %s", strerror(errno));
     if (s >= 0) {
-        closesocket(s);
+        slirp_closesocket(s);
     }
     if (sv[1] >= 0) {
-        closesocket(sv[1]);
+        slirp_closesocket(sv[1]);
     }
     return -1;
 }
@@ -162,16 +159,16 @@ fork_exec(struct socket *so, const char *ex)
     if (err) {
         g_critical("fork_exec: %s", err->message);
         g_error_free(err);
-        closesocket(sp[0]);
-        closesocket(sp[1]);
+        slirp_closesocket(sp[0]);
+        slirp_closesocket(sp[1]);
         return 0;
     }
 
     so->s = sp[0];
-    closesocket(sp[1]);
+    slirp_closesocket(sp[1]);
     socket_set_fast_reuse(so->s);
     opt = 1;
-    qemu_setsockopt(so->s, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(int));
+    slirp_setsockopt(so->s, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(int));
     so->slirp->cb->set_nonblock(so->s);
     return 1;
 }
slirp/ncsi.c +0 -1
index 8594382270..327f17543c 100644
@@ -6,7 +6,6 @@
  * This code is licensed under the GPL version 2 or later. See the
  * COPYING file in the top-level directory.
  */
-#include "qemu/osdep.h"
 #include "slirp.h"
 
 #include "ncsi-pkt.h"
slirp/ndp_table.c +0 -2
index b7b73722f7..34ea4fdf1f 100644
@@ -3,8 +3,6 @@
  * Guillaume Subiron, Yann Bordenave, Serigne Modou Wagne.
  */
 
-#include "qemu/osdep.h"
-#include "qemu-common.h"
 #include "slirp.h"
 
 void ndp_table_add(Slirp *slirp, struct in6_addr ip_addr,
slirp/sbuf.c +0 -2
index 17f28e97a6..51a9f0cc7d 100644
@@ -5,9 +5,7 @@
  * terms and conditions of the copyright.
  */
 
-#include "qemu/osdep.h"
 #include "slirp.h"
-#include "qemu/main-loop.h"
 
 static void sbappendsb(struct sbuf *sb, struct mbuf *m);
 
slirp/slirp.c +0 -7
index a1f42c8b19..39c0370e19 100644
@@ -21,14 +21,7 @@
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  * THE SOFTWARE.
  */
-#include "qemu/osdep.h"
-#include "qemu-common.h"
-#include "qemu/timer.h"
-#include "qemu/error-report.h"
-#include "migration/register.h"
 #include "slirp.h"
-#include "hw/hw.h"
-#include "qemu/cutils.h"
 
 #ifdef WITH_QEMU
 #include "state.h"
slirp/socket.c +8 -10
index 4d5be38747..7ae88b8e87 100644
@@ -5,8 +5,6 @@
  * terms and conditions of the copyright.
  */
 
-#include "qemu/osdep.h"
-#include "qemu-common.h"
 #include "slirp.h"
 #include "ip_icmp.h"
 #ifdef __sun__
@@ -187,7 +185,7 @@ soread(struct socket *so)
 	 */
 	sopreprbuf(so, iov, &n);
 
-	nn = qemu_recv(so->s, iov[0].iov_base, iov[0].iov_len,0);
+	nn = slirp_recv(so->s, iov[0].iov_base, iov[0].iov_len,0);
 	if (nn <= 0) {
 		if (nn < 0 && (errno == EINTR || errno == EAGAIN))
 			return 0;
@@ -203,7 +201,7 @@ soread(struct socket *so)
 				if (getpeername(so->s, paddr, &alen) < 0) {
 					err = errno;
 				} else {
-					getsockopt(so->s, SOL_SOCKET, SO_ERROR,
+					slirp_getsockopt(so->s, SOL_SOCKET, SO_ERROR,
 						&err, &elen);
 				}
 			}
@@ -233,7 +231,7 @@ soread(struct socket *so)
 	 */
 	if (n == 2 && nn == iov[0].iov_len) {
             int ret;
-            ret = qemu_recv(so->s, iov[1].iov_base, iov[1].iov_len,0);
+            ret = slirp_recv(so->s, iov[1].iov_base, iov[1].iov_len,0);
             if (ret > 0)
                 nn += ret;
         }
@@ -554,7 +552,7 @@ sorecvfrom(struct socket *so)
 	   */
 	  len = M_FREEROOM(m);
 	  /* if (so->so_fport != htons(53)) { */
-	  ioctlsocket(so->s, FIONREAD, &n);
+	  slirp_ioctlsocket(so->s, FIONREAD, &n);
 
 	  if (n > len) {
 	    n = (m->m_data - m->m_dat) + m->m_len + n + 1;
@@ -719,14 +717,14 @@ tcp_listen(Slirp *slirp, uint32_t haddr, u_int hport, uint32_t laddr,
 	addr.sin_addr.s_addr = haddr;
 	addr.sin_port = hport;
 
-	if (((s = qemu_socket(AF_INET,SOCK_STREAM,0)) < 0) ||
+	if (((s = slirp_socket(AF_INET,SOCK_STREAM,0)) < 0) ||
 	    (socket_set_fast_reuse(s) < 0) ||
 	    (bind(s,(struct sockaddr *)&addr, sizeof(addr)) < 0) ||
 	    (listen(s,1) < 0)) {
 		int tmperrno = errno; /* Don't clobber the real reason we failed */
 
                 if (s >= 0) {
-                    closesocket(s);
+                    slirp_closesocket(s);
                 }
 		sofree(so);
 		/* Restore the real errno */
@@ -737,9 +735,9 @@ tcp_listen(Slirp *slirp, uint32_t haddr, u_int hport, uint32_t laddr,
 #endif
 		return NULL;
 	}
-	qemu_setsockopt(s, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(int));
+	slirp_setsockopt(s, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(int));
 	opt = 1;
-	qemu_setsockopt(s, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(int));
+	slirp_setsockopt(s, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(int));
 
 	getsockname(s,(struct sockaddr *)&addr,&addrlen);
 	so->so_ffamily = AF_INET;
slirp/tcp_input.c +0 -1
index f205a1e6cb..3cd6277672 100644
@@ -38,7 +38,6 @@
  * terms and conditions of the copyright.
  */
 
-#include "qemu/osdep.h"
 #include "slirp.h"
 #include "ip_icmp.h"
 
slirp/tcp_output.c +0 -1
index 2f7f90d67e..f02b94b8f0 100644
@@ -38,7 +38,6 @@
  * terms and conditions of the copyright.
  */
 
-#include "qemu/osdep.h"
 #include "slirp.h"
 
 static const u_char  tcp_outflags[TCP_NSTATES] = {
slirp/tcp_subr.c +6 -7
index 3b15148187..4e486093c0 100644
@@ -38,7 +38,6 @@
  * terms and conditions of the copyright.
  */
 
-#include "qemu/osdep.h"
 #include "slirp.h"
 
 /* patchable/settable parameters for tcp */
@@ -337,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;
-	closesocket(so->s);
+	slirp_closesocket(so->s);
 	sbfree(&so->so_rcv);
 	sbfree(&so->so_snd);
 	sofree(so);
@@ -407,7 +406,7 @@ int tcp_fconnect(struct socket *so, unsigned short af)
   DEBUG_CALL("tcp_fconnect");
   DEBUG_ARG("so = %p", so);
 
-  ret = so->s = qemu_socket(af, SOCK_STREAM, 0);
+  ret = so->s = slirp_socket(af, SOCK_STREAM, 0);
   if (ret >= 0) {
     int opt, s=so->s;
     struct sockaddr_storage addr;
@@ -415,9 +414,9 @@ int tcp_fconnect(struct socket *so, unsigned short af)
     so->slirp->cb->set_nonblock(s);
     socket_set_fast_reuse(s);
     opt = 1;
-    qemu_setsockopt(s, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(opt));
+    slirp_setsockopt(s, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(opt));
     opt = 1;
-    qemu_setsockopt(s, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt));
+    slirp_setsockopt(s, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt));
 
     addr = so->fhost.ss;
     DEBUG_CALL(" connect()ing");
@@ -487,7 +486,7 @@ void tcp_connect(struct socket *inso)
     so->slirp->cb->set_nonblock(s);
     socket_set_fast_reuse(s);
     opt = 1;
-    qemu_setsockopt(s, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(int));
+    slirp_setsockopt(s, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(int));
     socket_set_nodelay(s);
 
     so->fhost.ss = addr;
@@ -496,7 +495,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 */
-        closesocket(so->s);
+        slirp_closesocket(so->s);
 
         /* Don't select it yet, even though we have an FD */
         /* if it's not FACCEPTONCE, it's already NOFDREF */
slirp/tcp_timer.c +0 -1
index d953a16386..ebc9d16f06 100644
@@ -30,7 +30,6 @@
  * tcp_timer.c,v 1.2 1994/08/02 07:49:10 davidg Exp
  */
 
-#include "qemu/osdep.h"
 #include "slirp.h"
 
 static struct tcpcb *tcp_timers(register struct tcpcb *tp, int timer);
slirp/tftp.c +4 -3
index 6fb381ef33..eae7f7c7f2 100644
@@ -22,10 +22,11 @@
  * THE SOFTWARE.
  */
 
-#include "qemu/osdep.h"
 #include "slirp.h"
-#include "qemu-common.h"
-#include "qemu/cutils.h"
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
 
 static inline int tftp_session_in_use(struct tftp_session *spt)
 {
slirp/udp.c +3 -4
index 4cf0d34d64..e49a8862d4 100644
@@ -38,7 +38,6 @@
  * terms and conditions of the copyright.
  */
 
-#include "qemu/osdep.h"
 #include "slirp.h"
 #include "ip_icmp.h"
 
@@ -281,7 +280,7 @@ int udp_output(struct socket *so, struct mbuf *m,
 int
 udp_attach(struct socket *so, unsigned short af)
 {
-  so->s = qemu_socket(af, SOCK_DGRAM, 0);
+  so->s = slirp_socket(af, SOCK_DGRAM, 0);
   if (so->s != -1) {
     so->so_expire = curtime + SO_EXPIRE;
     insque(so, &so->slirp->udb);
@@ -292,7 +291,7 @@ udp_attach(struct socket *so, unsigned short af)
 void
 udp_detach(struct socket *so)
 {
-	closesocket(so->s);
+	slirp_closesocket(so->s);
 	sofree(so);
 }
 
@@ -327,7 +326,7 @@ udp_listen(Slirp *slirp, uint32_t haddr, u_int hport, uint32_t laddr,
 	socklen_t addrlen = sizeof(struct sockaddr_in);
 
 	so = socreate(slirp);
-	so->s = qemu_socket(AF_INET,SOCK_DGRAM,0);
+	so->s = slirp_socket(AF_INET,SOCK_DGRAM,0);
         if (so->s < 0) {
             sofree(so);
             return NULL;
slirp/udp6.c +0 -2
index fa531e03c4..be5cba1f54 100644
@@ -3,8 +3,6 @@
  * Guillaume Subiron
  */
 
-#include "qemu/osdep.h"
-#include "qemu-common.h"
 #include "slirp.h"
 #include "udp.h"
 #include "dhcpv6.h"
slirp/util.c new file +176 -0

176 lines not on this page · raw message

slirp/Makefile.objs +1 -0
index 7abc7549ab..61cdcc6877 100644
@@ -28,6 +28,7 @@ slirp.mo-objs = \
 	tftp.o \
 	udp.o \
 	udp6.o \
+	util.o \
 	$(NULL)
 
 slirp.mo-cflags = -DG_LOG_DOMAIN=\"Slirp\" -DWITH_QEMU=1
Signature · 1 line
2.20.0.rc1
#54 lore raw

Re: [Qemu-devel] [PATCH v2 for-3.2 v2 29/30] slirp: replace remaining QEMU dependency

Hi

Quoted 5 lines · Marc-André Lureau

Introduce utility header/object, and replace remaining qemu functions with SLIRP helpers.

self-nack,

not only this patch deserves to be split, but it also introduces regression.

Quoted 1,411 lines
---
 slirp/ip.h          |  14 +--
 slirp/ip6.h         |   5 +-
 slirp/ip6_icmp.h    |  16 ++--
 slirp/libslirp.h    |  12 ++-
 slirp/qtailq.h      | 218 ++++++++++++++++++++++++++++++++++++++++++++
 slirp/slirp.h       |  12 +--
 slirp/util.h        | 148 ++++++++++++++++++++++++++++++
 slirp/arp_table.c   |   1 -
 slirp/bootp.c       |   1 -
 slirp/cksum.c       |   1 -
 slirp/dhcpv6.c      |   2 -
 slirp/dnssearch.c   |   1 -
 slirp/if.c          |   2 -
 slirp/ip6_icmp.c    |   4 -
 slirp/ip6_input.c   |   1 -
 slirp/ip6_output.c  |   2 -
 slirp/ip_icmp.c     |   7 +-
 slirp/ip_input.c    |   1 -
 slirp/ip_output.c   |   1 -
 slirp/mbuf.c        |   1 -
 slirp/misc.c        |  21 ++---
 slirp/ncsi.c        |   1 -
 slirp/ndp_table.c   |   2 -
 slirp/sbuf.c        |   2 -
 slirp/slirp.c       |   7 --
 slirp/socket.c      |  18 ++--
 slirp/tcp_input.c   |   1 -
 slirp/tcp_output.c  |   1 -
 slirp/tcp_subr.c    |  13 ++-
 slirp/tcp_timer.c   |   1 -
 slirp/tftp.c        |   7 +-
 slirp/udp.c         |   7 +-
 slirp/udp6.c        |   2 -
 slirp/util.c        | 176 +++++++++++++++++++++++++++++++++++
 slirp/Makefile.objs |   1 +
 35 files changed, 609 insertions(+), 101 deletions(-)
 create mode 100644 slirp/qtailq.h
 create mode 100644 slirp/util.h
 create mode 100644 slirp/util.c
slirp/ip.h +7 -7
index 243b6c8b24..cd6ddf2bb7 100644
@@ -89,7 +89,7 @@ struct ip {
        uint8_t ip_p;                   /* protocol */
        uint16_t        ip_sum;                 /* checksum */
        struct  in_addr ip_src,ip_dst;  /* source and dest address */
-} QEMU_PACKED;
+} SLIRP_PACKED;
 
 #define        IP_MAXPACKET    65535           /* maximum packet size */
 
@@ -151,7 +151,7 @@ struct      ip_timestamp {
                        n_long ipt_time;
                } ipt_ta[1];
        } ipt_timestamp;
-} QEMU_PACKED;
+} SLIRP_PACKED;
 
 /* flag bits for ipt_flg */
 #define        IPOPT_TS_TSONLY         0               /* timestamps only */
@@ -181,11 +181,11 @@ struct    ip_timestamp {
 struct mbuf_ptr {
        struct mbuf *mptr;
        uint32_t dummy;
-} QEMU_PACKED;
+} SLIRP_PACKED;
 #else
 struct mbuf_ptr {
        struct mbuf *mptr;
-} QEMU_PACKED;
+} SLIRP_PACKED;
 #endif
 struct qlink {
        void *next, *prev;
@@ -201,7 +201,7 @@ struct ipovly {
        uint16_t        ih_len;                 /* protocol length */
        struct  in_addr ih_src;         /* source internet address */
        struct  in_addr ih_dst;         /* destination internet address */
-} QEMU_PACKED;
+} SLIRP_PACKED;
 
 /*
  * Ip reassembly queue structure.  Each fragment
@@ -217,7 +217,7 @@ struct ipq {
        uint8_t ipq_p;                  /* protocol of this fragment */
        uint16_t        ipq_id;                 /* sequence id for reassembly */
        struct  in_addr ipq_src,ipq_dst;
-} QEMU_PACKED;
+} SLIRP_PACKED;
 
 /*
  * Ip header, when holding a fragment.
@@ -227,7 +227,7 @@ struct ipq {
 struct ipasfrag {
        struct qlink ipf_link;
        struct ip ipf_ip;
-} QEMU_PACKED;
+} SLIRP_PACKED;
 
 #define ipf_off      ipf_ip.ip_off
 #define ipf_tos      ipf_ip.ip_tos
slirp/ip6.h +2 -3
index 14e9c78735..e0a13dec1c 100644
@@ -7,7 +7,6 @@
 #define SLIRP_IP6_H
 
 #include <glib.h>
-#include "net/eth.h"
 
 #define ALLNODES_MULTICAST  { .s6_addr = \
                             { 0xff, 0x02, 0x00, 0x00,\
@@ -133,7 +132,7 @@ struct ip6 {
     uint8_t     ip_nh;               /* next header */
     uint8_t     ip_hl;               /* hop limit */
     struct in6_addr ip_src, ip_dst;  /* source and dest address */
-} QEMU_PACKED;
+} SLIRP_PACKED;
 
 /*
  * IPv6 pseudo-header used by upper-layer protocols
@@ -145,7 +144,7 @@ struct ip6_pseudohdr {
     uint16_t    ih_zero_hi;       /* zero */
     uint8_t     ih_zero_lo;       /* zero */
     uint8_t     ih_nh;            /* next header */
-} QEMU_PACKED;
+} SLIRP_PACKED;
 
 
 #endif
slirp/ip6_icmp.h +8 -8
index 32b0914055..1f09b485e3 100644
@@ -48,12 +48,12 @@ struct ndp_ra {     /* Router Advertisement Message */
     uint16_t lifetime;      /* Router Lifetime */
     uint32_t reach_time;    /* Reachable Time */
     uint32_t retrans_time;  /* Retrans Timer */
-} QEMU_PACKED;
+} SLIRP_PACKED;
 
 struct ndp_ns {     /* Neighbor Solicitation Message */
     uint32_t reserved;
     struct in6_addr target; /* Target Address */
-} QEMU_PACKED;
+} SLIRP_PACKED;
 
 struct ndp_na {     /* Neighbor Advertisement Message */
 #if G_BYTE_ORDER == G_BIG_ENDIAN
@@ -72,13 +72,13 @@ struct ndp_na {     /* Neighbor Advertisement Message */
         reserved_lo:24;
 #endif
     struct in6_addr target; /* Target Address */
-} QEMU_PACKED;
+} SLIRP_PACKED;
 
 struct ndp_redirect {
     uint32_t reserved;
     struct in6_addr target; /* Target Address */
     struct in6_addr dest;   /* Destination Address */
-} QEMU_PACKED;
+} SLIRP_PACKED;
 
 /*
  * Structure of an icmpv6 header.
@@ -103,7 +103,7 @@ struct icmp6 {
 #define icmp6_nns icmp6_body.ndp_ns
 #define icmp6_nna icmp6_body.ndp_na
 #define icmp6_redirect icmp6_body.ndp_redirect
-} QEMU_PACKED;
+} SLIRP_PACKED;
 
 #define ICMP6_MINLEN    4
 #define ICMP6_ERROR_MINLEN  8
@@ -134,16 +134,16 @@ struct ndpopt {
             uint32_t    pref_lt;                /* Preferred Lifetime */
             uint32_t    reserved2;
             struct in6_addr prefix;
-        } QEMU_PACKED prefixinfo;
+        } SLIRP_PACKED prefixinfo;
 #define ndpopt_prefixinfo ndpopt_body.prefixinfo
         struct rdnss {
             uint16_t reserved;
             uint32_t lifetime;
             struct in6_addr addr;
-        } QEMU_PACKED rdnss;
+        } SLIRP_PACKED rdnss;
 #define ndpopt_rdnss ndpopt_body.rdnss
     } ndpopt_body;
-} QEMU_PACKED;
+} SLIRP_PACKED;
 
 /* NDP options type */
 #define NDPOPT_LINKLAYER_SOURCE     1   /* Source Link-Layer Address */
slirp/libslirp.h +11 -1
index f2d71bd1f6..2c90738f89 100644
@@ -1,7 +1,17 @@
 #ifndef LIBSLIRP_H
 #define LIBSLIRP_H
 
-#include "qemu-common.h"
+#include <glib.h>
+#include <stdint.h>
+#include <stdbool.h>
+
+#ifdef _WIN32
+#include <winsock2.h>
+#include <in6addr.h>
+#else
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#endif
 
 typedef struct Slirp Slirp;
 
slirp/qtailq.h new file +218 -0
new file mode 100644
index 0000000000..7c1d73a5bd
@@ -0,0 +1,218 @@
+/*      $NetBSD: queue.h,v 1.52 2009/04/20 09:56:08 mschuett Exp $ */
+
+/*
+ * slirp version: Copy from QEMU, removed all but tail queues.
+ */
+
+/*
+ * Copyright (c) 1991, 1993
+ *      The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ *      @(#)queue.h     8.5 (Berkeley) 8/20/94
+ */
+
+#ifndef QTAILQ_H
+#define QTAILQ_H
+
+/*
+ * A tail queue is headed by a pair of pointers, one to the head of the
+ * list and the other to the tail of the list. The elements are doubly
+ * linked so that an arbitrary element can be removed without a need to
+ * traverse the list. New elements can be added to the list before or
+ * after an existing element, at the head of the list, or at the end of
+ * the list. A tail queue may be traversed in either direction.
+ */
+
+#define Q_TAILQ_HEAD(name, type, qual)                                         \
+    struct name {                                                              \
+        qual type *tqh_first;      /* first element */                         \
+        qual type *qual *tqh_last; /* addr of last next element */             \
+    }
+#define QTAILQ_HEAD(name, type) Q_TAILQ_HEAD(name, struct type, )
+
+#define QTAILQ_HEAD_INITIALIZER(head)                                          \
+    {                                                                          \
+        NULL, &(head).tqh_first                                                \
+    }
+
+#define Q_TAILQ_ENTRY(type, qual)                                              \
+    struct {                                                                   \
+        qual type *tqe_next;       /* next element */                          \
+        qual type *qual *tqe_prev; /* address of previous next element */      \
+    }
+#define QTAILQ_ENTRY(type) Q_TAILQ_ENTRY(struct type, )
+
+/*
+ * Tail queue functions.
+ */
+#define QTAILQ_INIT(head)                                                      \
+    do {                                                                       \
+        (head)->tqh_first = NULL;                                              \
+        (head)->tqh_last = &(head)->tqh_first;                                 \
+    } while (/*CONSTCOND*/ 0)
+
+#define QTAILQ_INSERT_HEAD(head, elm, field)                                   \
+    do {                                                                       \
+        if (((elm)->field.tqe_next = (head)->tqh_first) != NULL)               \
+            (head)->tqh_first->field.tqe_prev = &(elm)->field.tqe_next;        \
+        else                                                                   \
+            (head)->tqh_last = &(elm)->field.tqe_next;                         \
+        (head)->tqh_first = (elm);                                             \
+        (elm)->field.tqe_prev = &(head)->tqh_first;                            \
+    } while (/*CONSTCOND*/ 0)
+
+#define QTAILQ_INSERT_TAIL(head, elm, field)                                   \
+    do {                                                                       \
+        (elm)->field.tqe_next = NULL;                                          \
+        (elm)->field.tqe_prev = (head)->tqh_last;                              \
+        *(head)->tqh_last = (elm);                                             \
+        (head)->tqh_last = &(elm)->field.tqe_next;                             \
+    } while (/*CONSTCOND*/ 0)
+
+#define QTAILQ_INSERT_AFTER(head, listelm, elm, field)                         \
+    do {                                                                       \
+        if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)       \
+            (elm)->field.tqe_next->field.tqe_prev = &(elm)->field.tqe_next;    \
+        else                                                                   \
+            (head)->tqh_last = &(elm)->field.tqe_next;                         \
+        (listelm)->field.tqe_next = (elm);                                     \
+        (elm)->field.tqe_prev = &(listelm)->field.tqe_next;                    \
+    } while (/*CONSTCOND*/ 0)
+
+#define QTAILQ_INSERT_BEFORE(listelm, elm, field)                              \
+    do {                                                                       \
+        (elm)->field.tqe_prev = (listelm)->field.tqe_prev;                     \
+        (elm)->field.tqe_next = (listelm);                                     \
+        *(listelm)->field.tqe_prev = (elm);                                    \
+        (listelm)->field.tqe_prev = &(elm)->field.tqe_next;                    \
+    } while (/*CONSTCOND*/ 0)
+
+#define QTAILQ_REMOVE(head, elm, field)                                        \
+    do {                                                                       \
+        if (((elm)->field.tqe_next) != NULL)                                   \
+            (elm)->field.tqe_next->field.tqe_prev = (elm)->field.tqe_prev;     \
+        else                                                                   \
+            (head)->tqh_last = (elm)->field.tqe_prev;                          \
+        *(elm)->field.tqe_prev = (elm)->field.tqe_next;                        \
+        (elm)->field.tqe_prev = NULL;                                          \
+    } while (/*CONSTCOND*/ 0)
+
+#define QTAILQ_FOREACH(var, head, field)                                       \
+    for ((var) = ((head)->tqh_first); (var); (var) = ((var)->field.tqe_next))
+
+#define QTAILQ_FOREACH_SAFE(var, head, field, next_var)                        \
+    for ((var) = ((head)->tqh_first);                                          \
+         (var) && ((next_var) = ((var)->field.tqe_next), 1);                   \
+         (var) = (next_var))
+
+#define QTAILQ_FOREACH_REVERSE(var, head, headname, field)                     \
+    for ((var) = (*(((struct headname *)((head)->tqh_last))->tqh_last));       \
+         (var);                                                                \
+         (var) = (*(((struct headname *)((var)->field.tqe_prev))->tqh_last)))
+
+#define QTAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, prev_var)      \
+    for ((var) = (*(((struct headname *)((head)->tqh_last))->tqh_last));       \
+         (var) &&                                                              \
+         ((prev_var) =                                                         \
+              (*(((struct headname *)((var)->field.tqe_prev))->tqh_last)),     \
+         1);                                                                   \
+         (var) = (prev_var))
+
+/*
+ * Tail queue access methods.
+ */
+#define QTAILQ_EMPTY(head) ((head)->tqh_first == NULL)
+#define QTAILQ_FIRST(head) ((head)->tqh_first)
+#define QTAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
+#define QTAILQ_IN_USE(elm, field) ((elm)->field.tqe_prev != NULL)
+
+#define QTAILQ_LAST(head, headname)                                            \
+    (*(((struct headname *)((head)->tqh_last))->tqh_last))
+#define QTAILQ_PREV(elm, headname, field)                                      \
+    (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
+
+#define field_at_offset(base, offset, type)                                    \
+    ((type)(((char *)(base)) + (offset)))
+
+typedef struct DUMMY_Q_ENTRY DUMMY_Q_ENTRY;
+typedef struct DUMMY_Q DUMMY_Q;
+
+struct DUMMY_Q_ENTRY {
+    QTAILQ_ENTRY(DUMMY_Q_ENTRY) next;
+};
+
+struct DUMMY_Q {
+    QTAILQ_HEAD(DUMMY_Q_HEAD, DUMMY_Q_ENTRY) head;
+};
+
+#define dummy_q ((DUMMY_Q *)0)
+#define dummy_qe ((DUMMY_Q_ENTRY *)0)
+
+/*
+ * Offsets of layout of a tail queue head.
+ */
+#define QTAILQ_FIRST_OFFSET (offsetof(typeof(dummy_q->head), tqh_first))
+#define QTAILQ_LAST_OFFSET (offsetof(typeof(dummy_q->head), tqh_last))
+/*
+ * Raw access of elements of a tail queue
+ */
+#define QTAILQ_RAW_FIRST(head)                                                 \
+    (*field_at_offset(head, QTAILQ_FIRST_OFFSET, void **))
+#define QTAILQ_RAW_TQH_LAST(head)                                              \
+    (*field_at_offset(head, QTAILQ_LAST_OFFSET, void ***))
+
+/*
+ * Offsets of layout of a tail queue element.
+ */
+#define QTAILQ_NEXT_OFFSET (offsetof(typeof(dummy_qe->next), tqe_next))
+#define QTAILQ_PREV_OFFSET (offsetof(typeof(dummy_qe->next), tqe_prev))
+
+/*
+ * Raw access of elements of a tail entry
+ */
+#define QTAILQ_RAW_NEXT(elm, entry)                                            \
+    (*field_at_offset(elm, entry + QTAILQ_NEXT_OFFSET, void **))
+#define QTAILQ_RAW_TQE_PREV(elm, entry)                                        \
+    (*field_at_offset(elm, entry + QTAILQ_PREV_OFFSET, void ***))
+/*
+ * Tail queue tranversal using pointer arithmetic.
+ */
+#define QTAILQ_RAW_FOREACH(elm, head, entry)                                   \
+    for ((elm) = QTAILQ_RAW_FIRST(head); (elm);                                \
+         (elm) = QTAILQ_RAW_NEXT(elm, entry))
+/*
+ * Tail queue insertion using pointer arithmetic.
+ */
+#define QTAILQ_RAW_INSERT_TAIL(head, elm, entry)                               \
+    do {                                                                       \
+        QTAILQ_RAW_NEXT(elm, entry) = NULL;                                    \
+        QTAILQ_RAW_TQE_PREV(elm, entry) = QTAILQ_RAW_TQH_LAST(head);           \
+        *QTAILQ_RAW_TQH_LAST(head) = (elm);                                    \
+        QTAILQ_RAW_TQH_LAST(head) = &QTAILQ_RAW_NEXT(elm, entry);              \
+    } while (/*CONSTCOND*/ 0)
+
+#endif /* QTAILQ_H */
slirp/slirp.h +5 -7
index 6e7a663b6f..08ec331ee5 100644
@@ -5,8 +5,8 @@
 
 typedef char *caddr_t;
 
-# include <windows.h>
 # include <winsock2.h>
+# include <windows.h>
 # include <ws2tcpip.h>
 # include <sys/timeb.h>
 # include <iphlpapi.h>
@@ -45,10 +45,8 @@ typedef char *caddr_t;
 #define quehead slirp_quehead
 
 #include "debug.h"
-
-#include "qemu/queue.h"
-#include "qemu/sockets.h"
-#include "net/eth.h"
+#include "util.h"
+#include "qtailq.h"
 
 #include "libslirp.h"
 #include "ip.h"
@@ -93,7 +91,7 @@ struct slirp_arphdr {
     uint32_t      ar_sip;           /* sender IP address       */
     unsigned char ar_tha[ETH_ALEN]; /* target hardware address */
     uint32_t      ar_tip;           /* target IP address       */
-} QEMU_PACKED;
+} SLIRP_PACKED;
 
 #define ARP_TABLE_SIZE 16
 
@@ -110,7 +108,7 @@ bool arp_table_search(Slirp *slirp, uint32_t ip_addr,
 struct ndpentry {
     unsigned char   eth_addr[ETH_ALEN];     /* sender hardware address */
     struct in6_addr ip_addr;                /* sender IP address       */
-} QEMU_PACKED;
+} SLIRP_PACKED;
 
 #define NDP_TABLE_SIZE 16
 
slirp/util.h new file +148 -0
new file mode 100644
index 0000000000..daf652d3e6
@@ -0,0 +1,148 @@
+/*
+ * Copyright (c) 2003-2008 Fabrice Bellard
+ * Copyright (c) 2010-2016 Red Hat, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+#ifndef UTIL_H_
+#define UTIL_H_
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <assert.h>
+#include <errno.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <inttypes.h>
+
+#ifdef _WIN32
+#include <winsock2.h>
+#include <windows.h>
+#else
+#include <sys/socket.h>
+#include <netinet/tcp.h>
+#include <netinet/in.h>
+#endif
+
+#if defined(_WIN32)
+# define SLIRP_PACKED __attribute__((gcc_struct, packed))
+#else
+# define SLIRP_PACKED __attribute__((packed))
+#endif
+
+#ifndef DIV_ROUND_UP
+#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
+#endif
+
+#define SCALE_MS 1000000
+
+#ifndef container_of
+#define container_of(ptr, type, member) (__extension__({                \
+                const typeof(((type *) 0)->member) *__mptr = (ptr);     \
+                (type *) ((char *) __mptr - offsetof(type, member));}))
+#endif
+
+#ifndef glue
+#define xglue(x, y) x ## y
+#define glue(x, y) xglue(x, y)
+#define stringify(s) tostring(s)
+#define tostring(s) #s
+#endif
+
+#if defined(_WIN32) /* CONFIG_IOVEC */
+# if !defined(IOV_MAX) /* XXX: to avoid duplicate with QEMU osdep.h */
+struct iovec {
+    void *iov_base;
+    size_t iov_len;
+};
+# endif
+#else
+#include <sys/uio.h>
+#endif
+
+#define ETH_ALEN    6
+#define ETH_HLEN    6
+#define ETH_P_IP                  (0x0800)      /* Internet Protocol packet  */
+#define ETH_P_ARP                 (0x0806)      /* Address Resolution packet */
+#define ETH_P_IPV6                (0x86dd)
+#define ETH_P_VLAN                (0x8100)
+#define ETH_P_DVLAN               (0x88a8)
+#define ETH_P_NCSI                (0x88f8)
+#define ETH_P_UNKNOWN             (0xffff)
+
+#ifdef _WIN32
+int slirp_closesocket(int fd);
+int slirp_ioctlsocket(int fd, int req, void *val);
+int inet_aton(const char *cp, struct in_addr *ia);
+#define slirp_getsockopt(sockfd, level, optname, optval, optlen) \
+    getsockopt(sockfd, level, optname, (void *)optval, optlen)
+#define slirp_setsockopt(sockfd, level, optname, optval, optlen)        \
+    setsockopt(sockfd, level, optname, (const void *)optval, optlen)
+#define slirp_recv(sockfd, buf, len, flags) recv(sockfd, (void *)buf, len, flags)
+#else
+#define slirp_setsockopt setsockopt
+#define slirp_getsockopt getsockopt
+#define slirp_recv recv
+#define slirp_closesocket close
+#define slirp_ioctlsocket ioctl
+#endif
+
+int slirp_socket(int domain, int type, int protocol);
+
+static inline int socket_set_nodelay(int fd)
+{
+    int v = 1;
+    return slirp_setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &v, sizeof(v));
+}
+
+static inline int socket_set_fast_reuse(int fd)
+{
+#ifndef _WIN32
+    int v = 1;
+    return slirp_setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v));
+#else
+    /* Enabling the reuse of an endpoint that was used by a socket still in
+     * TIME_WAIT state is usually performed by setting SO_REUSEADDR. On Windows
+     * fast reuse is the default and SO_REUSEADDR does strange things. So we
+     * don't have to do anything here. More info can be found at:
+     * http://msdn.microsoft.com/en-us/library/windows/desktop/ms740621.aspx */
+    return 0;
+#endif
+}
+
+static inline void pstrcpy(char *buf, int buf_size, const char *str)
+{
+    int c;
+    char *q = buf;
+
+    if (buf_size <= 0)
+        return;
+
+    for(;;) {
+        c = *str++;
+        if (c == 0 || q >= buf + buf_size - 1)
+            break;
+        *q++ = c;
+    }
+    *q = '\0';
+}
+
+#endif
slirp/arp_table.c +0 -1
index bf71b984ad..8ea655f79d 100644
@@ -22,7 +22,6 @@
  * THE SOFTWARE.
  */
 
-#include "qemu/osdep.h"
 #include "slirp.h"
 
 void arp_table_add(Slirp *slirp, uint32_t ip_addr, uint8_t ethaddr[ETH_ALEN])
slirp/bootp.c +0 -1
index 4c9a77eb98..d396849a05 100644
@@ -21,7 +21,6 @@
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  * THE SOFTWARE.
  */
-#include "qemu/osdep.h"
 #include "slirp.h"
 
 #if defined(_WIN32)
slirp/cksum.c +0 -1
index 84c858fafb..25bfa67348 100644
@@ -30,7 +30,6 @@
  * in_cksum.c,v 1.2 1994/08/02 07:48:16 davidg Exp
  */
 
-#include "qemu/osdep.h"
 #include "slirp.h"
 
 /*
slirp/dhcpv6.c +0 -2
index 752df40536..9ffba38e8f 100644
@@ -20,8 +20,6 @@
  * along with this program; if not, see <http://www.gnu.org/licenses/>.
  */
 
-#include "qemu/osdep.h"
-#include "qemu/log.h"
 #include "slirp.h"
 #include "dhcpv6.h"
 
slirp/dnssearch.c +0 -1
index 8fb563321b..c459cece8d 100644
@@ -22,7 +22,6 @@
  * THE SOFTWARE.
  */
 
-#include "qemu/osdep.h"
 #include "slirp.h"
 
 static const uint8_t RFC3397_OPT_DOMAIN_SEARCH = 119;
slirp/if.c +0 -2
index 73e3705740..2ad03b8a79 100644
@@ -5,9 +5,7 @@
  * terms and conditions of the copyright.
  */
 
-#include "qemu/osdep.h"
 #include "slirp.h"
-#include "qemu/timer.h"
 
 static void
 ifs_insque(struct mbuf *ifm, struct mbuf *ifmhead)
slirp/ip6_icmp.c +0 -4
index e72c57a81d..2a432ebbd4 100644
@@ -3,12 +3,8 @@
  * Guillaume Subiron, Yann Bordenave, Serigne Modou Wagne.
  */
 
-#include "qemu/osdep.h"
 #include "slirp.h"
 #include "ip6_icmp.h"
-#include "qemu/timer.h"
-#include "qemu/error-report.h"
-#include "qemu/log.h"
 
 #define NDP_Interval g_rand_int_range(slirp->grand, \
         NDP_MinRtrAdvInterval, NDP_MaxRtrAdvInterval)
slirp/ip6_input.c +0 -1
index ab656a0a9d..1b8c003c66 100644
@@ -3,7 +3,6 @@
  * Guillaume Subiron, Yann Bordenave, Serigne Modou Wagne.
  */
 
-#include "qemu/osdep.h"
 #include "slirp.h"
 #include "ip6_icmp.h"
 
slirp/ip6_output.c +0 -2
index 52c88ad691..19d1ae7748 100644
@@ -3,8 +3,6 @@
  * Guillaume Subiron, Yann Bordenave, Serigne Modou Wagne.
  */
 
-#include "qemu/osdep.h"
-#include "qemu-common.h"
 #include "slirp.h"
 
 /* Number of packets queued before we start sending
slirp/ip_icmp.c +3 -4
index 7c7e042049..e2e673ceaa 100644
@@ -30,7 +30,6 @@
  * ip_icmp.c,v 1.7 1995/05/30 08:09:42 rgrimes Exp
  */
 
-#include "qemu/osdep.h"
 #include "slirp.h"
 #include "ip_icmp.h"
 
@@ -83,7 +82,7 @@ static int icmp_send(struct socket *so, struct mbuf *m, int hlen)
     struct ip *ip = mtod(m, struct ip *);
     struct sockaddr_in addr;
 
-    so->s = qemu_socket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP);
+    so->s = slirp_socket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP);
     if (so->s == -1) {
         return -1;
     }
@@ -114,7 +113,7 @@ static int icmp_send(struct socket *so, struct mbuf *m, int hlen)
 
 void icmp_detach(struct socket *so)
 {
-    closesocket(so->s);
+    slirp_closesocket(so->s);
     sofree(so);
 }
 
@@ -421,7 +420,7 @@ void icmp_receive(struct socket *so)
     icp = mtod(m, struct icmp *);
 
     id = icp->icmp_id;
-    len = qemu_recv(so->s, icp, M_ROOM(m), 0);
+    len = slirp_recv(so->s, icp, M_ROOM(m), 0);
     /*
      * The behavior of reading SOCK_DGRAM+IPPROTO_ICMP sockets is inconsistent
      * between host OSes.  On Linux, only the ICMP header and payload is
slirp/ip_input.c +0 -1
index 6831526320..cfda30fccd 100644
@@ -38,7 +38,6 @@
  * terms and conditions of the copyright.
  */
 
-#include "qemu/osdep.h"
 #include "slirp.h"
 #include "ip_icmp.h"
 
slirp/ip_output.c +0 -1
index db403f04c1..f6ec141df5 100644
@@ -38,7 +38,6 @@
  * terms and conditions of the copyright.
  */
 
-#include "qemu/osdep.h"
 #include "slirp.h"
 
 /* Number of packets queued before we start sending
slirp/mbuf.c +0 -1
index d8d275e0e7..521c02c967 100644
@@ -15,7 +15,6 @@
  * the flags
  */
 
-#include "qemu/osdep.h"
 #include "slirp.h"
 
 #define MBUF_THRESH 30
slirp/misc.c +9 -12
index 7789cefefa..5b1c526f17 100644
@@ -5,11 +5,8 @@
  * terms and conditions of the copyright.
  */
 
-#include "qemu/osdep.h"
 #include "slirp.h"
 #include "libslirp.h"
-#include "qemu/error-report.h"
-#include "qemu/main-loop.h"
 
 inline void
 insque(void *a, void *b)
@@ -83,14 +80,14 @@ slirp_socketpair_with_oob(int sv[2])
     int ret, s;
 
     sv[1] = -1;
-    s = qemu_socket(AF_INET, SOCK_STREAM, 0);
+    s = slirp_socket(AF_INET, SOCK_STREAM, 0);
     if (s < 0 || bind(s, (struct sockaddr *)&addr, addrlen) < 0 ||
         listen(s, 1) < 0 ||
         getsockname(s, (struct sockaddr *)&addr, &addrlen) < 0) {
         goto err;
     }
 
-    sv[1] = qemu_socket(AF_INET, SOCK_STREAM, 0);
+    sv[1] = slirp_socket(AF_INET, SOCK_STREAM, 0);
     if (sv[1] < 0) {
         goto err;
     }
@@ -113,16 +110,16 @@ slirp_socketpair_with_oob(int sv[2])
         goto err;
     }
 
-    closesocket(s);
+    slirp_closesocket(s);
     return 0;
 
 err:
     g_critical("slirp_socketpair(): %s", strerror(errno));
     if (s >= 0) {
-        closesocket(s);
+        slirp_closesocket(s);
     }
     if (sv[1] >= 0) {
-        closesocket(sv[1]);
+        slirp_closesocket(sv[1]);
     }
     return -1;
 }
@@ -162,16 +159,16 @@ fork_exec(struct socket *so, const char *ex)
     if (err) {
         g_critical("fork_exec: %s", err->message);
         g_error_free(err);
-        closesocket(sp[0]);
-        closesocket(sp[1]);
+        slirp_closesocket(sp[0]);
+        slirp_closesocket(sp[1]);
         return 0;
     }
 
     so->s = sp[0];
-    closesocket(sp[1]);
+    slirp_closesocket(sp[1]);
     socket_set_fast_reuse(so->s);
     opt = 1;
-    qemu_setsockopt(so->s, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(int));
+    slirp_setsockopt(so->s, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(int));
     so->slirp->cb->set_nonblock(so->s);
     return 1;
 }
slirp/ncsi.c +0 -1
index 8594382270..327f17543c 100644
@@ -6,7 +6,6 @@
  * This code is licensed under the GPL version 2 or later. See the
  * COPYING file in the top-level directory.
  */
-#include "qemu/osdep.h"
 #include "slirp.h"
 
 #include "ncsi-pkt.h"
slirp/ndp_table.c +0 -2
index b7b73722f7..34ea4fdf1f 100644
@@ -3,8 +3,6 @@
  * Guillaume Subiron, Yann Bordenave, Serigne Modou Wagne.
  */
 
-#include "qemu/osdep.h"
-#include "qemu-common.h"
 #include "slirp.h"
 
 void ndp_table_add(Slirp *slirp, struct in6_addr ip_addr,
slirp/sbuf.c +0 -2
index 17f28e97a6..51a9f0cc7d 100644
@@ -5,9 +5,7 @@
  * terms and conditions of the copyright.
  */
 
-#include "qemu/osdep.h"
 #include "slirp.h"
-#include "qemu/main-loop.h"
 
 static void sbappendsb(struct sbuf *sb, struct mbuf *m);
 
slirp/slirp.c +0 -7
index a1f42c8b19..39c0370e19 100644
@@ -21,14 +21,7 @@
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  * THE SOFTWARE.
  */
-#include "qemu/osdep.h"
-#include "qemu-common.h"
-#include "qemu/timer.h"
-#include "qemu/error-report.h"
-#include "migration/register.h"
 #include "slirp.h"
-#include "hw/hw.h"
-#include "qemu/cutils.h"
 
 #ifdef WITH_QEMU
 #include "state.h"
slirp/socket.c +8 -10
index 4d5be38747..7ae88b8e87 100644
@@ -5,8 +5,6 @@
  * terms and conditions of the copyright.
  */
 
-#include "qemu/osdep.h"
-#include "qemu-common.h"
 #include "slirp.h"
 #include "ip_icmp.h"
 #ifdef __sun__
@@ -187,7 +185,7 @@ soread(struct socket *so)
         */
        sopreprbuf(so, iov, &n);
 
-       nn = qemu_recv(so->s, iov[0].iov_base, iov[0].iov_len,0);
+       nn = slirp_recv(so->s, iov[0].iov_base, iov[0].iov_len,0);
        if (nn <= 0) {
                if (nn < 0 && (errno == EINTR || errno == EAGAIN))
                        return 0;
@@ -203,7 +201,7 @@ soread(struct socket *so)
                                if (getpeername(so->s, paddr, &alen) < 0) {
                                        err = errno;
                                } else {
-                                       getsockopt(so->s, SOL_SOCKET, SO_ERROR,
+                                       slirp_getsockopt(so->s, SOL_SOCKET, SO_ERROR,
                                                &err, &elen);
                                }
                        }
@@ -233,7 +231,7 @@ soread(struct socket *so)
         */
        if (n == 2 && nn == iov[0].iov_len) {
             int ret;
-            ret = qemu_recv(so->s, iov[1].iov_base, iov[1].iov_len,0);
+            ret = slirp_recv(so->s, iov[1].iov_base, iov[1].iov_len,0);
             if (ret > 0)
                 nn += ret;
         }
@@ -554,7 +552,7 @@ sorecvfrom(struct socket *so)
           */
          len = M_FREEROOM(m);
          /* if (so->so_fport != htons(53)) { */
-         ioctlsocket(so->s, FIONREAD, &n);
+         slirp_ioctlsocket(so->s, FIONREAD, &n);
 
          if (n > len) {
            n = (m->m_data - m->m_dat) + m->m_len + n + 1;
@@ -719,14 +717,14 @@ tcp_listen(Slirp *slirp, uint32_t haddr, u_int hport, uint32_t laddr,
        addr.sin_addr.s_addr = haddr;
        addr.sin_port = hport;
 
-       if (((s = qemu_socket(AF_INET,SOCK_STREAM,0)) < 0) ||
+       if (((s = slirp_socket(AF_INET,SOCK_STREAM,0)) < 0) ||
            (socket_set_fast_reuse(s) < 0) ||
            (bind(s,(struct sockaddr *)&addr, sizeof(addr)) < 0) ||
            (listen(s,1) < 0)) {
                int tmperrno = errno; /* Don't clobber the real reason we failed */
 
                 if (s >= 0) {
-                    closesocket(s);
+                    slirp_closesocket(s);
                 }
                sofree(so);
                /* Restore the real errno */
@@ -737,9 +735,9 @@ tcp_listen(Slirp *slirp, uint32_t haddr, u_int hport, uint32_t laddr,
 #endif
                return NULL;
        }
-       qemu_setsockopt(s, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(int));
+       slirp_setsockopt(s, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(int));
        opt = 1;
-       qemu_setsockopt(s, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(int));
+       slirp_setsockopt(s, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(int));
 
        getsockname(s,(struct sockaddr *)&addr,&addrlen);
        so->so_ffamily = AF_INET;
slirp/tcp_input.c +0 -1
index f205a1e6cb..3cd6277672 100644
@@ -38,7 +38,6 @@
  * terms and conditions of the copyright.
  */
 
-#include "qemu/osdep.h"
 #include "slirp.h"
 #include "ip_icmp.h"
 
slirp/tcp_output.c +0 -1
index 2f7f90d67e..f02b94b8f0 100644
@@ -38,7 +38,6 @@
  * terms and conditions of the copyright.
  */
 
-#include "qemu/osdep.h"
 #include "slirp.h"
 
 static const u_char  tcp_outflags[TCP_NSTATES] = {
slirp/tcp_subr.c +6 -7
index 3b15148187..4e486093c0 100644
@@ -38,7 +38,6 @@
  * terms and conditions of the copyright.
  */
 
-#include "qemu/osdep.h"
 #include "slirp.h"
 
 /* patchable/settable parameters for tcp */
@@ -337,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;
-       closesocket(so->s);
+       slirp_closesocket(so->s);
        sbfree(&so->so_rcv);
        sbfree(&so->so_snd);
        sofree(so);
@@ -407,7 +406,7 @@ int tcp_fconnect(struct socket *so, unsigned short af)
   DEBUG_CALL("tcp_fconnect");
   DEBUG_ARG("so = %p", so);
 
-  ret = so->s = qemu_socket(af, SOCK_STREAM, 0);
+  ret = so->s = slirp_socket(af, SOCK_STREAM, 0);
   if (ret >= 0) {
     int opt, s=so->s;
     struct sockaddr_storage addr;
@@ -415,9 +414,9 @@ int tcp_fconnect(struct socket *so, unsigned short af)
     so->slirp->cb->set_nonblock(s);
     socket_set_fast_reuse(s);
     opt = 1;
-    qemu_setsockopt(s, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(opt));
+    slirp_setsockopt(s, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(opt));
     opt = 1;
-    qemu_setsockopt(s, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt));
+    slirp_setsockopt(s, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt));
 
     addr = so->fhost.ss;
     DEBUG_CALL(" connect()ing");
@@ -487,7 +486,7 @@ void tcp_connect(struct socket *inso)
     so->slirp->cb->set_nonblock(s);
     socket_set_fast_reuse(s);
     opt = 1;
-    qemu_setsockopt(s, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(int));
+    slirp_setsockopt(s, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(int));
     socket_set_nodelay(s);
 
     so->fhost.ss = addr;
@@ -496,7 +495,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 */
-        closesocket(so->s);
+        slirp_closesocket(so->s);
 
         /* Don't select it yet, even though we have an FD */
         /* if it's not FACCEPTONCE, it's already NOFDREF */
slirp/tcp_timer.c +0 -1
index d953a16386..ebc9d16f06 100644
@@ -30,7 +30,6 @@
  * tcp_timer.c,v 1.2 1994/08/02 07:49:10 davidg Exp
  */
 
-#include "qemu/osdep.h"
 #include "slirp.h"
 
 static struct tcpcb *tcp_timers(register struct tcpcb *tp, int timer);
slirp/tftp.c +4 -3
index 6fb381ef33..eae7f7c7f2 100644
@@ -22,10 +22,11 @@
  * THE SOFTWARE.
  */
 
-#include "qemu/osdep.h"
 #include "slirp.h"
-#include "qemu-common.h"
-#include "qemu/cutils.h"
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
 
 static inline int tftp_session_in_use(struct tftp_session *spt)
 {
slirp/udp.c +3 -4
index 4cf0d34d64..e49a8862d4 100644
@@ -38,7 +38,6 @@
  * terms and conditions of the copyright.
  */
 
-#include "qemu/osdep.h"
 #include "slirp.h"
 #include "ip_icmp.h"
 
@@ -281,7 +280,7 @@ int udp_output(struct socket *so, struct mbuf *m,
 int
 udp_attach(struct socket *so, unsigned short af)
 {
-  so->s = qemu_socket(af, SOCK_DGRAM, 0);
+  so->s = slirp_socket(af, SOCK_DGRAM, 0);
   if (so->s != -1) {
     so->so_expire = curtime + SO_EXPIRE;
     insque(so, &so->slirp->udb);
@@ -292,7 +291,7 @@ udp_attach(struct socket *so, unsigned short af)
 void
 udp_detach(struct socket *so)
 {
-       closesocket(so->s);
+       slirp_closesocket(so->s);
        sofree(so);
 }
 
@@ -327,7 +326,7 @@ udp_listen(Slirp *slirp, uint32_t haddr, u_int hport, uint32_t laddr,
        socklen_t addrlen = sizeof(struct sockaddr_in);
 
        so = socreate(slirp);
-       so->s = qemu_socket(AF_INET,SOCK_DGRAM,0);
+       so->s = slirp_socket(AF_INET,SOCK_DGRAM,0);
         if (so->s < 0) {
             sofree(so);
             return NULL;
slirp/udp6.c +0 -2
index fa531e03c4..be5cba1f54 100644
@@ -3,8 +3,6 @@
  * Guillaume Subiron
  */
 
-#include "qemu/osdep.h"
-#include "qemu-common.h"
 #include "slirp.h"
 #include "udp.h"
 #include "dhcpv6.h"
slirp/util.c new file +176 -0
new file mode 100644
index 0000000000..b1a36b27bc
@@ -0,0 +1,176 @@
+/*
+ * util.c (mostly based on QEMU os-win32.c)
+ *
+ * Copyright (c) 2003-2008 Fabrice Bellard
+ * Copyright (c) 2010-2016 Red Hat, Inc.
+ *
+ * QEMU library functions for win32 which are shared between QEMU and
+ * the QEMU tools.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+#include "util.h"
+
+#include <glib.h>
+#include <fcntl.h>
+#include <stdint.h>
+
+#if defined(_WIN32) && !defined(WITH_QEMU)
+int inet_aton(const char *cp, struct in_addr *ia)
+{
+    uint32_t addr = inet_addr(cp);
+    if (addr == 0xffffffff) {
+        return 0;
+    }
+    ia->s_addr = addr;
+    return 1;
+}
+#endif
+
+static void slirp_set_cloexec(int fd)
+{
+#ifndef _WIN32
+    int f;
+    f = fcntl(fd, F_GETFD);
+    assert(f != -1);
+    f = fcntl(fd, F_SETFD, f | FD_CLOEXEC);
+    assert(f != -1);
+#endif
+}
+
+/*
+ * Opens a socket with FD_CLOEXEC set
+ */
+int slirp_socket(int domain, int type, int protocol)
+{
+    int ret;
+
+#ifdef SOCK_CLOEXEC
+    ret = socket(domain, type | SOCK_CLOEXEC, protocol);
+    if (ret != -1 || errno != EINVAL) {
+        return ret;
+    }
+#endif
+    ret = socket(domain, type, protocol);
+    if (ret >= 0) {
+        slirp_set_cloexec(ret);
+    }
+
+    return ret;
+}
+
+#ifdef _WIN32
+static int socket_error(void)
+{
+    switch (WSAGetLastError()) {
+    case 0:
+        return 0;
+    case WSAEINTR:
+        return EINTR;
+    case WSAEINVAL:
+        return EINVAL;
+    case WSA_INVALID_HANDLE:
+        return EBADF;
+    case WSA_NOT_ENOUGH_MEMORY:
+        return ENOMEM;
+    case WSA_INVALID_PARAMETER:
+        return EINVAL;
+    case WSAENAMETOOLONG:
+        return ENAMETOOLONG;
+    case WSAENOTEMPTY:
+        return ENOTEMPTY;
+    case WSAEWOULDBLOCK:
+         /* not using EWOULDBLOCK as we don't want code to have
+          * to check both EWOULDBLOCK and EAGAIN */
+        return EAGAIN;
+    case WSAEINPROGRESS:
+        return EINPROGRESS;
+    case WSAEALREADY:
+        return EALREADY;
+    case WSAENOTSOCK:
+        return ENOTSOCK;
+    case WSAEDESTADDRREQ:
+        return EDESTADDRREQ;
+    case WSAEMSGSIZE:
+        return EMSGSIZE;
+    case WSAEPROTOTYPE:
+        return EPROTOTYPE;
+    case WSAENOPROTOOPT:
+        return ENOPROTOOPT;
+    case WSAEPROTONOSUPPORT:
+        return EPROTONOSUPPORT;
+    case WSAEOPNOTSUPP:
+        return EOPNOTSUPP;
+    case WSAEAFNOSUPPORT:
+        return EAFNOSUPPORT;
+    case WSAEADDRINUSE:
+        return EADDRINUSE;
+    case WSAEADDRNOTAVAIL:
+        return EADDRNOTAVAIL;
+    case WSAENETDOWN:
+        return ENETDOWN;
+    case WSAENETUNREACH:
+        return ENETUNREACH;
+    case WSAENETRESET:
+        return ENETRESET;
+    case WSAECONNABORTED:
+        return ECONNABORTED;
+    case WSAECONNRESET:
+        return ECONNRESET;
+    case WSAENOBUFS:
+        return ENOBUFS;
+    case WSAEISCONN:
+        return EISCONN;
+    case WSAENOTCONN:
+        return ENOTCONN;
+    case WSAETIMEDOUT:
+        return ETIMEDOUT;
+    case WSAECONNREFUSED:
+        return ECONNREFUSED;
+    case WSAELOOP:
+        return ELOOP;
+    case WSAEHOSTUNREACH:
+        return EHOSTUNREACH;
+    default:
+        return EIO;
+    }
+}
+
+#undef ioctlsocket
+int slirp_ioctlsocket(int fd, int req, void *val)
+{
+    int ret;
+    ret = ioctlsocket(fd, req, val);
+    if (ret < 0) {
+        errno = socket_error();
+    }
+    return ret;
+}
+
+#undef closesocket
+int slirp_closesocket(int fd)
+{
+    int ret;
+    ret = closesocket(fd);
+    if (ret < 0) {
+        errno = socket_error();
+    }
+    return ret;
+}
+#endif /* WIN32 */
slirp/Makefile.objs +1 -1
index 7abc7549ab..61cdcc6877 100644
@@ -28,6 +28,7 @@ slirp.mo-objs = \
        tftp.o \
        udp.o \
        udp6.o \
+       util.o \
        $(NULL)
 
 slirp.mo-cflags = -DG_LOG_DOMAIN=\"Slirp\" -DWITH_QEMU=1
--

2.20.0.rc1

Signature · 1 line
Marc-André Lureau
#55 lore raw

PATCH v2 30/30 build-sys: add a basic meson build

In reply to Marc-André Lureau

This is a minimal file to build a libslirp shared library.

It has been tested to build on Linux and with mingw64 cross-compilation.

---
 slirp/meson.build | 48 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)
 create mode 100644 slirp/meson.build
slirp/meson.build new file +48 -0
new file mode 100644
index 0000000000..aa6f1b39e2
@@ -0,0 +1,48 @@
+project('slirp', 'c', version : '3.1')
+
+add_global_arguments('-std=gnu99', language : 'c')
+
+host_system = host_machine.system()
+
+glib_dep = dependency('glib-2.0')
+
+cc = meson.get_compiler('c')
+
+platform_deps = []
+
+if host_system == 'windows'
+  platform_deps += [
+    cc.find_library('ws2_32'),
+    cc.find_library('iphlpapi')
+  ]
+endif
+
+shared_library('slirp',
+               'arp_table.c',
+               'bootp.c',
+               'cksum.c',
+               'dhcpv6.c',
+               'dnssearch.c',
+               'if.c',
+               'ip6_icmp.c',
+               'ip6_input.c',
+               'ip6_output.c',
+               'ip_icmp.c',
+               'ip_input.c',
+               'ip_output.c',
+               'mbuf.c',
+               'misc.c',
+               'ncsi.c',
+               'ndp_table.c',
+               'sbuf.c',
+               'slirp.c',
+               'socket.c',
+               'tcp_input.c',
+               'tcp_output.c',
+               'tcp_subr.c',
+               'tcp_timer.c',
+               'tftp.c',
+               'udp.c',
+               'udp6.c',
+               'util.c',
+               dependencies : [glib_dep, platform_deps])
Signature · 1 line
2.20.0.rc1
#56 lore raw
In reply to Marc-André Lureau
Quoted 10 lines

There has been some attempts to make slirp a seperate project in the past.
(https://lists.gnu.org/archive/html/qemu-devel/2017-02/msg01092.html)
Unfortunately, they forked from QEMU and didn't provide enough compatibility for QEMU to make use of it (in particular, vmstate handling was removed, they lost git history etc). Furthermore, they are not maintained as far as I can see.

I would propose to make slirp a seperate project, that can initially be used by QEMU as a submodule, keeping Makefile.objs until a proper shared library with stability guarantees etc is ready..

Hmm, I'd skip the submodule step if the long-term plan is to use slirp as pkg-config detectable dependency (like most other libs).

cheers,
Gerd