Skip to contentSkip to conversation

slirp: fill mainloop with more precise timeout value

PATCH Superseded Liu Ping Fan 4 participants 11 messages Aug 21, 2013 – Aug 25, 2013

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover slirp: fill mainloop with more precise timeout value

With this series, we can set the mainloop timeout more precisely when slirp has to emulate tcp timeout problem.

v3:
fix comment: document timeout unit "milliseconds"
fix logic: no slirps, no timeout modifications in slirp_pollfds_fill()
v2:
fold slirp_update_timeout logic into slirp_pollfds_fill.

Liu Ping Fan (3):
slirp: make timeout local
slirp: define timeout as macro
slirp: set mainloop timeout with more precise value

 main-loop.c      |  3 +--
 slirp/libslirp.h |  3 +--
 slirp/slirp.c    | 55 +++++++++++++++++++++++++++++++++++++++----------------
 slirp/slirp.h    |  3 +++
 stubs/slirp.c    |  6 +-----
 5 files changed, 45 insertions(+), 25 deletions(-)
Signature · 1 line
1.8.1.4
#1 lore raw

PATCH v3 1/3 slirp: make timeout local

Each slirp has its own time to caculate timeout.

---
 slirp/slirp.c | 22 ++++++++++------------
 slirp/slirp.h |  3 +++
 2 files changed, 13 insertions(+), 12 deletions(-)
slirp/slirp.c +10 -12
index 80b28ea..b71c617 100644
@@ -40,8 +40,6 @@ static const uint8_t special_ethaddr[ETH_ALEN] = {
 static const uint8_t zero_ethaddr[ETH_ALEN] = { 0, 0, 0, 0, 0, 0 };
 
 u_int curtime;
-static u_int time_fasttimo, last_slowtimo;
-static int do_slowtimo;
 
 static QTAILQ_HEAD(slirp_instances, Slirp) slirp_instances =
     QTAILQ_HEAD_INITIALIZER(slirp_instances);
@@ -278,14 +276,13 @@ void slirp_pollfds_fill(GArray *pollfds)
     /*
      * First, TCP sockets
      */
-    do_slowtimo = 0;
 
     QTAILQ_FOREACH(slirp, &slirp_instances, entry) {
         /*
          * *_slowtimo needs calling if there are IP fragments
          * in the fragment queue, or there are TCP connections active
          */
-        do_slowtimo |= ((slirp->tcb.so_next != &slirp->tcb) ||
+        slirp->do_slowtimo = ((slirp->tcb.so_next != &slirp->tcb) ||
                 (&slirp->ipq.ip_link != slirp->ipq.ip_link.next));
 
         for (so = slirp->tcb.so_next; so != &slirp->tcb;
@@ -299,8 +296,9 @@ void slirp_pollfds_fill(GArray *pollfds)
             /*
              * See if we need a tcp_fasttimo
              */
-            if (time_fasttimo == 0 && so->so_tcpcb->t_flags & TF_DELACK) {
-                time_fasttimo = curtime; /* Flag when we want a fasttimo */
+            if (slirp->time_fasttimo == 0 &&
+                so->so_tcpcb->t_flags & TF_DELACK) {
+                slirp->time_fasttimo = curtime; /* Flag when want a fasttimo */
             }
 
             /*
@@ -381,7 +379,7 @@ void slirp_pollfds_fill(GArray *pollfds)
                     udp_detach(so);
                     continue;
                 } else {
-                    do_slowtimo = 1; /* Let socket expire */
+                    slirp->do_slowtimo = true; /* Let socket expire */
                 }
             }
 
@@ -422,7 +420,7 @@ void slirp_pollfds_fill(GArray *pollfds)
                     icmp_detach(so);
                     continue;
                 } else {
-                    do_slowtimo = 1; /* Let socket expire */
+                    slirp->do_slowtimo = true; /* Let socket expire */
                 }
             }
 
@@ -454,14 +452,14 @@ void slirp_pollfds_poll(GArray *pollfds, int select_error)
         /*
          * See if anything has timed out
          */
-        if (time_fasttimo && ((curtime - time_fasttimo) >= 2)) {
+        if (slirp->time_fasttimo && ((curtime - slirp->time_fasttimo) >= 2)) {
             tcp_fasttimo(slirp);
-            time_fasttimo = 0;
+            slirp->time_fasttimo = 0;
         }
-        if (do_slowtimo && ((curtime - last_slowtimo) >= 499)) {
+        if (slirp->do_slowtimo && ((curtime - slirp->last_slowtimo) >= 499)) {
             ip_slowtimo(slirp);
             tcp_slowtimo(slirp);
-            last_slowtimo = curtime;
+            slirp->last_slowtimo = curtime;
         }
 
         /*
slirp/slirp.h +3 -0
index fe0e65d..e4a1bd4 100644
@@ -203,6 +203,9 @@ bool arp_table_search(Slirp *slirp, uint32_t ip_addr,
 
 struct Slirp {
     QTAILQ_ENTRY(Slirp) entry;
+    u_int time_fasttimo;
+    u_int last_slowtimo;
+    bool do_slowtimo;
 
     /* virtual network configuration */
     struct in_addr vnetwork_addr;
Signature · 1 line
1.8.1.4
#2 lore raw

PATCH v3 2/3 slirp: define timeout as macro

In reply to Liu Ping Fan
---
 slirp/slirp.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
slirp/slirp.c +7 -2
index b71c617..1e8983e 100644
@@ -47,6 +47,9 @@ static QTAILQ_HEAD(slirp_instances, Slirp) slirp_instances =
 static struct in_addr dns_addr;
 static u_int dns_addr_time;
 
+#define TIMEOUT_FAST 2  /* milliseconds */
+#define TIMEOUT_SLOW 499  /* milliseconds */
+
 #ifdef _WIN32
 
 int get_dns_addr(struct in_addr *pdns_addr)
@@ -452,11 +455,13 @@ void slirp_pollfds_poll(GArray *pollfds, int select_error)
         /*
          * See if anything has timed out
          */
-        if (slirp->time_fasttimo && ((curtime - slirp->time_fasttimo) >= 2)) {
+        if (slirp->time_fasttimo &&
+            ((curtime - slirp->time_fasttimo) >= TIMEOUT_FAST)) {
             tcp_fasttimo(slirp);
             slirp->time_fasttimo = 0;
         }
-        if (slirp->do_slowtimo && ((curtime - slirp->last_slowtimo) >= 499)) {
+        if (slirp->do_slowtimo &&
+            ((curtime - slirp->last_slowtimo) >= TIMEOUT_SLOW)) {
             ip_slowtimo(slirp);
             tcp_slowtimo(slirp);
             slirp->last_slowtimo = curtime;
Signature · 1 line
1.8.1.4
#3 lore raw

PATCH v3 3/3 slirp: set mainloop timeout with more precise value

In reply to Liu Ping Fan

If slirp needs to emulate tcp timeout, then the timeout value for mainloop should be more precise, which is determined by slirp's fasttimo or slowtimo. Achieve this by swap the logic sequence of slirp_pollfds_fill and slirp_update_timeout.

---
 main-loop.c      |  3 +--
 slirp/libslirp.h |  3 +--
 slirp/slirp.c    | 28 ++++++++++++++++++++++++----
 stubs/slirp.c    |  6 +-----
 4 files changed, 27 insertions(+), 13 deletions(-)
main-loop.c +1 -2
index a44fff6..e258567 100644
@@ -458,8 +458,7 @@ int main_loop_wait(int nonblocking)
     g_array_set_size(gpollfds, 0); /* reset for new iteration */
     /* XXX: separate device handlers from system ones */
 #ifdef CONFIG_SLIRP
-    slirp_update_timeout(&timeout);
-    slirp_pollfds_fill(gpollfds);
+    slirp_pollfds_fill(gpollfds, &timeout);
 #endif
     qemu_iohandler_fill(gpollfds);
     ret = os_host_main_loop_wait(timeout);
slirp/libslirp.h +1 -2
index ceabff8..5bdcbd5 100644
@@ -16,8 +16,7 @@ Slirp *slirp_init(int restricted, struct in_addr vnetwork,
                   void *opaque);
 void slirp_cleanup(Slirp *slirp);
 
-void slirp_update_timeout(uint32_t *timeout);
-void slirp_pollfds_fill(GArray *pollfds);
+void slirp_pollfds_fill(GArray *pollfds, uint32_t *timeout);
 
 void slirp_pollfds_poll(GArray *pollfds, int select_error);
 
slirp/slirp.c +24 -4
index 1e8983e..f312a7d 100644
@@ -260,14 +260,33 @@ void slirp_cleanup(Slirp *slirp)
 #define CONN_CANFSEND(so) (((so)->so_state & (SS_FCANTSENDMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
 #define CONN_CANFRCV(so) (((so)->so_state & (SS_FCANTRCVMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
 
-void slirp_update_timeout(uint32_t *timeout)
+static void slirp_update_timeout(uint32_t *timeout)
 {
-    if (!QTAILQ_EMPTY(&slirp_instances)) {
-        *timeout = MIN(1000, *timeout);
+    Slirp *slirp;
+    uint32_t t;
+
+    *timeout = MIN(1000, *timeout);
+    if (*timeout <= TIMEOUT_FAST) {
+        return;
+    }
+    t = *timeout;
+
+    /* If we have tcp timeout with slirp, then we will fill @timeout with
+     * more precise value.
+     */
+    QTAILQ_FOREACH(slirp, &slirp_instances, entry) {
+        if (slirp->time_fasttimo) {
+            *timeout = TIMEOUT_FAST;
+            return;
+        }
+        if (slirp->do_slowtimo) {
+            t = MIN(TIMEOUT_SLOW, t);
+        }
     }
+    *timeout = t;
 }
 
-void slirp_pollfds_fill(GArray *pollfds)
+void slirp_pollfds_fill(GArray *pollfds, uint32_t *timeout)
 {
     Slirp *slirp;
     struct socket *so, *so_next;
@@ -437,6 +456,7 @@ void slirp_pollfds_fill(GArray *pollfds)
             }
         }
     }
+    slirp_update_timeout(timeout);
 }
 
 void slirp_pollfds_poll(GArray *pollfds, int select_error)
stubs/slirp.c +1 -5
index f1fc833..bd0ac7f 100644
@@ -1,11 +1,7 @@
 #include "qemu-common.h"
 #include "slirp/slirp.h"
 
-void slirp_update_timeout(uint32_t *timeout)
-{
-}
-
-void slirp_pollfds_fill(GArray *pollfds)
+void slirp_pollfds_fill(GArray *pollfds, uint32_t *timeout)
 {
 }
 
Signature · 1 line
1.8.1.4
#4 lore raw

Re: [Qemu-devel] [PATCH v3 3/3] slirp: set mainloop timeout with more precise value

slirp/slirp.c void slirp_cleanup(Slirp *slirp) · line 266

Quoted 5 lines · --On 21 August 2013 10:15:52 +0800 Liu Ping Fan <[email protected]> wrote:
slirp/slirp.c +1 -3
-void slirp_update_timeout(uint32_t *timeout)
+static void slirp_update_timeout(uint32_t *timeout)
 {
-    if (!QTAILQ_EMPTY(&slirp_instances)) {
-        *timeout = MIN(1000, *timeout);

If you are putting things in macros, you might as well change that 1000 as well, and hopefully comment why that particular magic value is there.

Quoted 8 lines
slirp/slirp.c +8 -0
+    Slirp *slirp;
+    uint32_t t;
+
+    *timeout = MIN(1000, *timeout);
+    if (*timeout <= TIMEOUT_FAST) {
+        return;
+    }
+    t = *timeout;
Signature · 1 line
Alex Bligh
#5 lore raw

Re: [Qemu-devel] [PATCH v3 3/3] slirp: set mainloop timeout with more precise value

Quoted 4 lines · Alex Bligh

--On 21 August 2013 10:15:52 +0800 Liu Ping Fan <[email protected]> wrote:

Quoted 8 lines

slirp/slirp.c void slirp_cleanup(Slirp *slirp) · line 266

Quoted 5 lines
slirp/slirp.c +1 -3
-void slirp_update_timeout(uint32_t *timeout)
+static void slirp_update_timeout(uint32_t *timeout)
 {
-    if (!QTAILQ_EMPTY(&slirp_instances)) {
-        *timeout = MIN(1000, *timeout);

If you are putting things in macros, you might as well change that

TIMEOUT_FAST/SLOW have definite meaning, and used more than one place in the code. For 1000ms, I do not know this magic value's meaning, but whatever, it just occurs once. So there is no trouble to read the code.

Quoted 18 lines

1000 as well, and hopefully comment why that particular magic value is there.

slirp/slirp.c void slirp_cleanup(Slirp *slirp) · line 272

Quoted 8 lines
slirp/slirp.c +8 -0
+    Slirp *slirp;
+    uint32_t t;
+
+    *timeout = MIN(1000, *timeout);
+    if (*timeout <= TIMEOUT_FAST) {
+        return;
+    }
+    t = *timeout;

--
Alex Bligh

#6 lore raw

Re: [Qemu-devel] [PATCH v3 3/3] slirp: set mainloop timeout with more precise value

Quoted 14 lines · liu ping fan
Quoted 4 lines · Alex Bligh

--On 21 August 2013 10:15:52 +0800 Liu Ping Fan <[email protected]> wrote:

Quoted 8 lines

slirp/slirp.c void slirp_cleanup(Slirp *slirp) · line 266

Quoted 5 lines
slirp/slirp.c +1 -3
-void slirp_update_timeout(uint32_t *timeout)
+static void slirp_update_timeout(uint32_t *timeout)
 {
-    if (!QTAILQ_EMPTY(&slirp_instances)) {
-        *timeout = MIN(1000, *timeout);

If you are putting things in macros, you might as well change that

Quoted 4 lines

TIMEOUT_FAST/SLOW have definite meaning, and used more than one place in the code. For 1000ms, I do not know this magic value's meaning, but whatever, it just occurs once. So there is no trouble to read the code.

You could name it ONE_SEC or so. Can be done as trivial patch on top.

IIRC, slirp requires regular polling for the aging of certain requests like DNS.

Jan

Quoted 19 lines
Quoted 19 lines

1000 as well, and hopefully comment why that particular magic value is there.

slirp/slirp.c void slirp_cleanup(Slirp *slirp) · line 272

Quoted 8 lines
slirp/slirp.c +8 -0
+    Slirp *slirp;
+    uint32_t t;
+
+    *timeout = MIN(1000, *timeout);
+    if (*timeout <= TIMEOUT_FAST) {
+        return;
+    }
+    t = *timeout;

--
Alex Bligh

Signature · 2 lines
Siemens AG, Corporate Technology, CT RTC ITP SES-DE
Corporate Competence Center Embedded Linux
#7 lore raw

Re: [Qemu-devel] [PATCH v3 3/3] slirp: set mainloop timeout with more precise value

Quoted 20 lines · Jan Kiszka
Quoted 14 lines · liu ping fan
Quoted 4 lines · Alex Bligh

--On 21 August 2013 10:15:52 +0800 Liu Ping Fan <[email protected]> wrote:

Quoted 8 lines

slirp/slirp.c void slirp_cleanup(Slirp *slirp) · line 266

Quoted 5 lines
slirp/slirp.c +1 -3
-void slirp_update_timeout(uint32_t *timeout)
+static void slirp_update_timeout(uint32_t *timeout)
 {
-    if (!QTAILQ_EMPTY(&slirp_instances)) {
-        *timeout = MIN(1000, *timeout);

If you are putting things in macros, you might as well change that

Quoted 4 lines

TIMEOUT_FAST/SLOW have definite meaning, and used more than one place in the code. For 1000ms, I do not know this magic value's meaning, but whatever, it just occurs once. So there is no trouble to read the code.

Quoted 5 lines

You could name it ONE_SEC or so. Can be done as trivial patch on top.

IIRC, slirp requires regular polling for the aging of certain requests like DNS.

Thanks for the explanation. Will fix and document it.

Regards,
Pingfan

#8 lore raw

Re: [Qemu-devel] [PATCH v3 3/3] slirp: set mainloop timeout with more precise value

In reply to Liu Ping Fan
Quoted 49 lines · Liu Ping Fan

If slirp needs to emulate tcp timeout, then the timeout value for mainloop should be more precise, which is determined by slirp's fasttimo or slowtimo. Achieve this by swap the logic sequence of slirp_pollfds_fill and slirp_update_timeout.

---
 main-loop.c      |  3 +--
 slirp/libslirp.h |  3 +--
 slirp/slirp.c    | 28 ++++++++++++++++++++++++----
 stubs/slirp.c    |  6 +-----
 4 files changed, 27 insertions(+), 13 deletions(-)
main-loop.c +1 -2
index a44fff6..e258567 100644
@@ -458,8 +458,7 @@ int main_loop_wait(int nonblocking)
     g_array_set_size(gpollfds, 0); /* reset for new iteration */
     /* XXX: separate device handlers from system ones */
 #ifdef CONFIG_SLIRP
-    slirp_update_timeout(&timeout);
-    slirp_pollfds_fill(gpollfds);
+    slirp_pollfds_fill(gpollfds, &timeout);
 #endif
     qemu_iohandler_fill(gpollfds);
     ret = os_host_main_loop_wait(timeout);
slirp/libslirp.h +1 -2
index ceabff8..5bdcbd5 100644
@@ -16,8 +16,7 @@ Slirp *slirp_init(int restricted, struct in_addr vnetwork,
                   void *opaque);
 void slirp_cleanup(Slirp *slirp);
 
-void slirp_update_timeout(uint32_t *timeout);
-void slirp_pollfds_fill(GArray *pollfds);
+void slirp_pollfds_fill(GArray *pollfds, uint32_t *timeout);
 
 void slirp_pollfds_poll(GArray *pollfds, int select_error);
 
slirp/slirp.c +0 -0
index 1e8983e..f312a7d 100644
@@ -260,14 +260,33 @@ void slirp_cleanup(Slirp *slirp)
 #define CONN_CANFSEND(so) (((so)->so_state & (SS_FCANTSENDMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
 #define CONN_CANFRCV(so) (((so)->so_state & (SS_FCANTRCVMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
 

slirp/slirp.c void slirp_cleanup(Slirp *slirp) · line 269

Quoted 10 lines
slirp/slirp.c +6 -3
-void slirp_update_timeout(uint32_t *timeout)
+static void slirp_update_timeout(uint32_t *timeout)
 {
-    if (!QTAILQ_EMPTY(&slirp_instances)) {
-        *timeout = MIN(1000, *timeout);
+    Slirp *slirp;
+    uint32_t t;
+
+    *timeout = MIN(1000, *timeout);
+    if (*timeout <= TIMEOUT_FAST) {

Nitpicking, sorry, but TIMEOUT_FAST is always smaller than 1000. So this check should come first, and then the MIN assignment (to t).

Jan

Quoted 50 lines
slirp/slirp.c +18 -1
+        return;
+    }
+    t = *timeout;
+
+    /* If we have tcp timeout with slirp, then we will fill @timeout with
+     * more precise value.
+     */
+    QTAILQ_FOREACH(slirp, &slirp_instances, entry) {
+        if (slirp->time_fasttimo) {
+            *timeout = TIMEOUT_FAST;
+            return;
+        }
+        if (slirp->do_slowtimo) {
+            t = MIN(TIMEOUT_SLOW, t);
+        }
     }
+    *timeout = t;
 }
 
-void slirp_pollfds_fill(GArray *pollfds)
+void slirp_pollfds_fill(GArray *pollfds, uint32_t *timeout)
 {
     Slirp *slirp;
     struct socket *so, *so_next;
@@ -437,6 +456,7 @@ void slirp_pollfds_fill(GArray *pollfds)
             }
         }
     }
+    slirp_update_timeout(timeout);
 }
 
 void slirp_pollfds_poll(GArray *pollfds, int select_error)
stubs/slirp.c +1 -5
index f1fc833..bd0ac7f 100644
@@ -1,11 +1,7 @@
 #include "qemu-common.h"
 #include "slirp/slirp.h"
 
-void slirp_update_timeout(uint32_t *timeout)
-{
-}
-
-void slirp_pollfds_fill(GArray *pollfds)
+void slirp_pollfds_fill(GArray *pollfds, uint32_t *timeout)
 {
 }
 
Signature · 2 lines
Siemens AG, Corporate Technology, CT RTC ITP SES-DE
Corporate Competence Center Embedded Linux
#9 lore raw

Re: [Qemu-devel] [PATCH v3 3/3] slirp: set mainloop timeout with more precise value

Quoted 50 lines · Jan Kiszka
Quoted 49 lines · Liu Ping Fan

If slirp needs to emulate tcp timeout, then the timeout value for mainloop should be more precise, which is determined by slirp's fasttimo or slowtimo. Achieve this by swap the logic sequence of slirp_pollfds_fill and slirp_update_timeout.

---
 main-loop.c      |  3 +--
 slirp/libslirp.h |  3 +--
 slirp/slirp.c    | 28 ++++++++++++++++++++++++----
 stubs/slirp.c    |  6 +-----
 4 files changed, 27 insertions(+), 13 deletions(-)
main-loop.c +1 -2
index a44fff6..e258567 100644
@@ -458,8 +458,7 @@ int main_loop_wait(int nonblocking)
     g_array_set_size(gpollfds, 0); /* reset for new iteration */
     /* XXX: separate device handlers from system ones */
 #ifdef CONFIG_SLIRP
-    slirp_update_timeout(&timeout);
-    slirp_pollfds_fill(gpollfds);
+    slirp_pollfds_fill(gpollfds, &timeout);
 #endif
     qemu_iohandler_fill(gpollfds);
     ret = os_host_main_loop_wait(timeout);
slirp/libslirp.h +1 -2
index ceabff8..5bdcbd5 100644
@@ -16,8 +16,7 @@ Slirp *slirp_init(int restricted, struct in_addr vnetwork,
                   void *opaque);
 void slirp_cleanup(Slirp *slirp);
 
-void slirp_update_timeout(uint32_t *timeout);
-void slirp_pollfds_fill(GArray *pollfds);
+void slirp_pollfds_fill(GArray *pollfds, uint32_t *timeout);
 
 void slirp_pollfds_poll(GArray *pollfds, int select_error);
 
slirp/slirp.c +0 -0
index 1e8983e..f312a7d 100644
@@ -260,14 +260,33 @@ void slirp_cleanup(Slirp *slirp)
 #define CONN_CANFSEND(so) (((so)->so_state & (SS_FCANTSENDMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
 #define CONN_CANFRCV(so) (((so)->so_state & (SS_FCANTRCVMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
 
Quoted 14 lines

slirp/slirp.c void slirp_cleanup(Slirp *slirp) · line 269

Quoted 10 lines
slirp/slirp.c +6 -3
-void slirp_update_timeout(uint32_t *timeout)
+static void slirp_update_timeout(uint32_t *timeout)
 {
-    if (!QTAILQ_EMPTY(&slirp_instances)) {
-        *timeout = MIN(1000, *timeout);
+    Slirp *slirp;
+    uint32_t t;
+
+    *timeout = MIN(1000, *timeout);
+    if (*timeout <= TIMEOUT_FAST) {

Nitpicking, sorry, but TIMEOUT_FAST is always smaller than 1000. So this check should come first, and then the MIN assignment (to t).

Will fix.

Regards,
Pingfan

#10 lore raw
In reply to Liu Ping Fan
Quoted 15 lines · Liu Ping Fan

With this series, we can set the mainloop timeout more precisely when slirp has to emulate tcp timeout problem.

v3:
fix comment: document timeout unit "milliseconds"
fix logic: no slirps, no timeout modifications in slirp_pollfds_fill()
v2:
fold slirp_update_timeout logic into slirp_pollfds_fill.

Liu Ping Fan (3):
slirp: make timeout local
slirp: define timeout as macro
slirp: set mainloop timeout with more precise value

Quoted 6 lines
 main-loop.c      |  3 +--
 slirp/libslirp.h |  3 +--
 slirp/slirp.c    | 55 +++++++++++++++++++++++++++++++++++++++----------------
 slirp/slirp.h    |  3 +++
 stubs/slirp.c    |  6 +-----
 5 files changed, 45 insertions(+), 25 deletions(-)

These patches look okay, I've skimmed them. Leaving detailed slirp review to Jan.

Stefan