Skip to contentSkip to conversation

Mini-OS: netfront: Fix rx ring starvation in network_rx

PATCH Applied Samuel Thibault 3 participants 7 messages Dec 7, 2014 – Jan 13, 2015 5 messages not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

Hello,

Quoted 4 lines · Martin Lucina, le Thu 04 Dec 2014 14:55:50 +0100, a écrit :

following is a patch against vanilla Mini-OS in upstream xen.git for a problem we have found in the netfront driver. When subjected to load network receive would freeze due to the rx ring running out of free request slots.

Oh, indeed nr_consumed seems to have been wrongly initialized. Could you submit a patch that only moves the nr_consumed = 0 initialization and the bogus BUG_ON?

Review

Quoted 8 lines
(quoted diff) +0 -8
-        if (rx->flags & NETRXF_extra_info)
-        {
-            printk("+++++++++++++++++++++ we have extras!\n");
-            continue;
-        }
-
-
-        if (rx->status == NETIF_RSP_NULL) continue;

I believe we want to keep them. Or turn them into if (rx->status != NETIF_RSP_OKAY)
continue;

the HAVE_LIBC codepath will be fine with the change.

Quoted 2 lines

Further, I would like to clean this function up; the code is an impossible to follow mess. What I'd like to arrive at is at [2],

I agree with stuffing the "cons" initialization into the for loop, but:

Quoted 1 line

however this also needs(?) to address the -DHAVE_LIBC codepath.

Yes, and I introduced the "some" variable especially for this. The idea was to implement a basic support for opening the device with a tap behavior. But we didn't want to introduce dynamic buffers to store incoming packets. In that case we don't run network_rx from the interrupt, but netfront_select_handler instead, which will wake the select() call, the application then calls netfront_receive(), which eats exactly one packet only from the ring. Thus the "some" variable in order to break the loop when exactly one packet was eaten (and no, we can't use break or set cons to rp, since that'd would eat 0 or more than 1 packets).

Samuel

A message that is not in the archive (sent off-list or not yet received): [email protected]

#3 lore raw

Hi Samuel,

Comments and updated patch below.

Quoted 4 lines · [email protected] said:
Quoted 4 lines
(quoted diff) +0 -4
-        if (rx->flags & NETRXF_extra_info)
-        {
-            printk("+++++++++++++++++++++ we have extras!\n");
-            continue;
Quoted 8 lines

Review

Quoted 4 lines
(quoted diff) +0 -4
-        }
-
-
-        if (rx->status == NETIF_RSP_NULL) continue;

I believe we want to keep them. Or turn them into if (rx->status != NETIF_RSP_OKAY)
continue;

If we keep them then the following code calling gnttab_end_access() will not get called and the second loops calls to gnttab_grant_access() will not match up.

Rather, shouldn't the final if() in the loop (where data is sent up) be:

if(rx->status > NETIF_RSP_NULL) { ... }

*we can't use NETIF_RSP_OKAY as NETIF_RSP_NULL is defined to be 1 (yuck).

I don't see any value in keeping the printk(), the netfront driver already disclaims supporting extras in comments.

Quoted 10 lines

Yes, and I introduced the "some" variable especially for this. The idea was to implement a basic support for opening the device with a tap behavior. But we didn't want to introduce dynamic buffers to store incoming packets. In that case we don't run network_rx from the interrupt, but netfront_select_handler instead, which will wake the select() call, the application then calls netfront_receive(), which eats exactly one packet only from the ring. Thus the "some" variable in order to break the loop when exactly one packet was eaten (and no, we can't use break or set cons to rp, since that'd would eat 0 or more than 1 packets).

Ah, now I understand. The presence of the "some" variable was making it really hard to reason about the logic in that loop :-/

Assuming you don't want to duplicate some code and write a separate receive() function for the HAVE_LIBC codepath entirely then a minimal change which clears things up for future readers is to:

- make "some" explicit using #ifdef HAVE_LIBC
- take it out of the for() loop initialisation and condition
- set some = 1 and use break to exit the loop

I've done this in the updated patch, take a look and let me know what you think.

Martin

----

Quoted 1 line

From 3880f59159bf0a05b47b6e723091b1e7e4fb6814 Mon Sep 17 00:00:00 2001

From: Martin Lucina <[email protected]>
Date: Thu, 4 Dec 2014 14:33:53 +0100
Subject: [PATCH] Mini-OS: netfront: Fix rx ring starvation in network_rx

In network_rx() we must push the same amount of requests back onto the ring in the second loop that we consumed in the first loop. Otherwise the ring will eventually starve itself of free request slots and no packets will be delivered.

Further, we make the HAVE_LIBC codepath clearer to follow by removing the "some" variable from the for loop initialisation and conditions.

---
 extras/mini-os/netfront.c |   36 +++++++++++++++++-------------------
 1 file changed, 17 insertions(+), 19 deletions(-)
extras/mini-os/netfront.c +17 -19
index 44c3995..42bb103 100644
@@ -96,42 +96,35 @@ static inline int xennet_rxidx(RING_IDX idx)
 void network_rx(struct netfront_dev *dev)
 {
     RING_IDX rp,cons,req_prod;
-    struct netif_rx_response *rx;
-    int nr_consumed, some, more, i, notify;
-
+    int nr_consumed, more, i, notify;
+#ifdef HAVE_LIBC
+    int some;
+#endif
 
+    nr_consumed = 0;
 moretodo:
     rp = dev->rx.sring->rsp_prod;
     rmb(); /* Ensure we see queued responses up to 'rp'. */
-    cons = dev->rx.rsp_cons;
 
-    for (nr_consumed = 0, some = 0;
-         (cons != rp) && !some;
-         nr_consumed++, cons++)
+#ifdef HAVE_LIBC
+    some = 0;
+#endif
+    for (cons = dev->rx.rsp_cons; cons != rp; nr_consumed++, cons++)
     {
         struct net_buffer* buf;
         unsigned char* page;
         int id;
 
-        rx = RING_GET_RESPONSE(&dev->rx, cons);
-
-        if (rx->flags & NETRXF_extra_info)
-        {
-            printk("+++++++++++++++++++++ we have extras!\n");
-            continue;
-        }
-
-
-        if (rx->status == NETIF_RSP_NULL) continue;
+        struct netif_rx_response *rx = RING_GET_RESPONSE(&dev->rx, cons);
 
         id = rx->id;
-        BUG_ON(id >= NET_TX_RING_SIZE);
+        BUG_ON(id >= NET_RX_RING_SIZE);
 
         buf = &dev->rx_buffers[id];
         page = (unsigned char*)buf->page;
         gnttab_end_access(buf->gref);
 
-        if(rx->status>0)
+        if (rx->status > NETIF_RSP_NULL)
         {
 #ifdef HAVE_LIBC
 	    if (dev->netif_rx == NETIF_SELECT_RX) {
@@ -142,6 +135,7 @@ moretodo:
 		memcpy(dev->data, page+rx->offset, len);
 		dev->rlen = len;
 		some = 1;
+                break;
 	    } else
 #endif
 		dev->netif_rx(page+rx->offset,rx->status);
@@ -150,7 +144,11 @@ moretodo:
     dev->rx.rsp_cons=cons;
 
     RING_FINAL_CHECK_FOR_RESPONSES(&dev->rx,more);
+#ifdef HAVE_LIBC
     if(more && !some) goto moretodo;
+#else
+    if(more) goto moretodo;
+#endif
 
     req_prod = dev->rx.req_prod_pvt;
 
Signature · 1 line
1.7.10.4

A message that is not in the archive (sent off-list or not yet received): [email protected]

#5 lore raw
Quoted 8 lines · [email protected] said:

Rather, shouldn't the final if() in the loop (where data is sent up) be:

if(rx->status > NETIF_RSP_NULL) { ... }

*we can't use NETIF_RSP_OKAY as NETIF_RSP_NULL is defined to be 1 (yuck).

I don't see any value in keeping the printk(), the netfront driver already disclaims supporting extras in comments.

I just cross-checked with include/xen/io/netif.h and the Linux kernel xen-netfront.c. We will never see NETRXF_extra_info as we (correctly) don't advertise any of the relevant feature-* toggles in Xenstore.

AFAICT we should also never see NETIF_RSP_NULL as according to the documentation that is only used for responses to TX requests. If this is indeed the case then the if can be

if (rx->status > NETIF_RSP_OKAY)

it can't be tested with == as rx->status is overloaded to contain the rx packet size.

Martin

author Acked-by
#6 lore raw
In reply to a message not in the archive
Quoted 2 lines · Martin Lucina, le Thu 11 Dec 2014 13:10:00 +0100, a écrit :

I've done this in the updated patch, take a look and let me know what you think.

I like that :)

Quoted 10 lines

From 3880f59159bf0a05b47b6e723091b1e7e4fb6814 Mon Sep 17 00:00:00 2001
From: Martin Lucina <[email protected]>
Date: Thu, 4 Dec 2014 14:33:53 +0100
Subject: [PATCH] Mini-OS: netfront: Fix rx ring starvation in network_rx

In network_rx() we must push the same amount of requests back onto the ring in the second loop that we consumed in the first loop. Otherwise the ring will eventually starve itself of free request slots and no packets will be delivered.

Quoted 4 lines

Further, we make the HAVE_LIBC codepath clearer to follow by removing the "some" variable from the for loop initialisation and conditions.

Quoted 86 lines
---
 extras/mini-os/netfront.c |   36 +++++++++++++++++-------------------
 1 file changed, 17 insertions(+), 19 deletions(-)
extras/mini-os/netfront.c +17 -19
index 44c3995..42bb103 100644
@@ -96,42 +96,35 @@ static inline int xennet_rxidx(RING_IDX idx)
 void network_rx(struct netfront_dev *dev)
 {
     RING_IDX rp,cons,req_prod;
-    struct netif_rx_response *rx;
-    int nr_consumed, some, more, i, notify;
-
+    int nr_consumed, more, i, notify;
+#ifdef HAVE_LIBC
+    int some;
+#endif
 
+    nr_consumed = 0;
 moretodo:
     rp = dev->rx.sring->rsp_prod;
     rmb(); /* Ensure we see queued responses up to 'rp'. */
-    cons = dev->rx.rsp_cons;
 
-    for (nr_consumed = 0, some = 0;
-         (cons != rp) && !some;
-         nr_consumed++, cons++)
+#ifdef HAVE_LIBC
+    some = 0;
+#endif
+    for (cons = dev->rx.rsp_cons; cons != rp; nr_consumed++, cons++)
     {
         struct net_buffer* buf;
         unsigned char* page;
         int id;
 
-        rx = RING_GET_RESPONSE(&dev->rx, cons);
-
-        if (rx->flags & NETRXF_extra_info)
-        {
-            printk("+++++++++++++++++++++ we have extras!\n");
-            continue;
-        }
-
-
-        if (rx->status == NETIF_RSP_NULL) continue;
+        struct netif_rx_response *rx = RING_GET_RESPONSE(&dev->rx, cons);
 
         id = rx->id;
-        BUG_ON(id >= NET_TX_RING_SIZE);
+        BUG_ON(id >= NET_RX_RING_SIZE);
 
         buf = &dev->rx_buffers[id];
         page = (unsigned char*)buf->page;
         gnttab_end_access(buf->gref);
 
-        if(rx->status>0)
+        if (rx->status > NETIF_RSP_NULL)
         {
 #ifdef HAVE_LIBC
 	    if (dev->netif_rx == NETIF_SELECT_RX) {
@@ -142,6 +135,7 @@ moretodo:
 		memcpy(dev->data, page+rx->offset, len);
 		dev->rlen = len;
 		some = 1;
+                break;
 	    } else
 #endif
 		dev->netif_rx(page+rx->offset,rx->status);
@@ -150,7 +144,11 @@ moretodo:
     dev->rx.rsp_cons=cons;
 
     RING_FINAL_CHECK_FOR_RESPONSES(&dev->rx,more);
+#ifdef HAVE_LIBC
     if(more && !some) goto moretodo;
+#else
+    if(more) goto moretodo;
+#endif
 
     req_prod = dev->rx.req_prod_pvt;
 
Signature · 1 line
1.7.10.4
Signature · 3 lines
Samuel
<T> csp.tar.gz:     ascii text
 -+- #ens-mim - vive les browsers qui prennent des initiatives à la con -+-

A message that is not in the archive (sent off-list or not yet received): [email protected]

#8 lore raw
Quoted 9 lines · [email protected] said:
Quoted 9 lines

From: Martin Lucina <[email protected]>
Date: Thu, 4 Dec 2014 14:33:53 +0100
Subject: [PATCH] Mini-OS: netfront: Fix rx ring starvation in network_rx

In network_rx() we must push the same amount of requests back onto the ring in the second loop that we consumed in the first loop. Otherwise the ring will eventually starve itself of free request slots and no packets will be delivered.

Quoted 6 lines
Quoted 4 lines

Further, we make the HAVE_LIBC codepath clearer to follow by removing the "some" variable from the for loop initialisation and conditions.

Thanks. I will merge a suitable version of this patch into our downstream tree.

Is there anything else I need to do to ensure this change lands in xen.git?

Martin

Applied
#9 lore raw
In reply to a message not in the archive
Quoted 16 lines · Samuel Thibault
Quoted 2 lines · Martin Lucina, le Thu 11 Dec 2014 13:10:00 +0100, a écrit :

I've done this in the updated patch, take a look and let me know what you think.

I like that :)

Quoted 10 lines

From 3880f59159bf0a05b47b6e723091b1e7e4fb6814 Mon Sep 17 00:00:00 2001
From: Martin Lucina <[email protected]>
Date: Thu, 4 Dec 2014 14:33:53 +0100
Subject: [PATCH] Mini-OS: netfront: Fix rx ring starvation in network_rx

In network_rx() we must push the same amount of requests back onto the ring in the second loop that we consumed in the first loop. Otherwise the ring will eventually starve itself of free request slots and no packets will be delivered.

Quoted 6 lines
Quoted 4 lines

Further, we make the HAVE_LIBC codepath clearer to follow by removing the "some" variable from the for loop initialisation and conditions.

Applied, thanks.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

A message that is not in the archive (sent off-list or not yet received): [email protected]

#11 lore raw
Quoted 1 line · Ian Campbell, le Tue 13 Jan 2015 11:01:06 +0000, a écrit :

Applied, thanks.

BTW, it is perhaps worth backporting to previous versions of Xen.

Samuel