Skip to contentSkip to conversation

Net patches

GIT PULL New Stefan Hajnoczi 1 participant 4 messages Sep 14, 2012

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

Fixes for hung NICs, USB network interface packet dropping, and inefficient netdev socket non-blocking I/O.

The following changes since commit 0c267217ca9985e6d118ec8368bebd382db7a099:

musicpal: Fix flash mapping (2012-09-08 10:17:57 +0000)

are available in the git repository at:

git://github.com/stefanha/qemu.git net

for you to fetch changes up to 0eb160d10e4f55cb5430ca3b6eb049977d0ce643:

net: EAGAIN handling for net/socket.c TCP (2012-09-10 08:12:34 +0100)

----------------------------------------------------------------
Bo Yang (1):
eepro100: Fix network hang when rx buffers run out

Paolo Bonzini (3):
net: notify iothread after flushing queue
e1000: flush queue whenever can_receive can go from false to true
xen: flush queue when getting an event

Stefan Hajnoczi (9):
net: add receive_disabled logic to iov delivery path
net: do not report queued packets as sent
net: add -netdev options to man page
net: clean up usbnet_receive()
net: fix usbnet_receive() packet drops
net: broadcast hub packets if at least one port can receive
net: asynchronous send/receive infrastructure for net/socket.c
net: EAGAIN handling for net/socket.c UDP
net: EAGAIN handling for net/socket.c TCP

 hw/e1000.c           |    4 ++
 hw/eepro100.c        |    4 +-
 hw/usb/dev-network.c |   49 ++++++++++++++--------
 hw/virtio-net.c      |    4 --
 hw/xen_nic.c         |    1 +
 net.c                |   22 ++++++++--
 net/hub.c            |    6 +--
 net/queue.c          |   40 +++++++++---------
 net/queue.h          |    2 +-
 net/socket.c         |  110 +++++++++++++++++++++++++++++++++++++++++++-------
 qemu-options.hx      |    7 ++++
 11 files changed, 186 insertions(+), 63 deletions(-)
Signature · 1 line
1.7.10.4
#1 lore raw

PATCH 9/13 net: fix usbnet_receive() packet drops

From: Stefan Hajnoczi <[email protected]>

The USB network interface has a single buffer which the guest reads from. This patch prevents multiple calls to usbnet_receive() from clobbering the input buffer. Instead we queue packets until buffer space becomes available again.

This is inspired by virtio-net and e1000 rxbuf handling.

---
 hw/usb/dev-network.c |   19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)
hw/usb/dev-network.c +16 -3
index 0b5cb71..e4a4359 100644
@@ -1001,6 +1001,13 @@ static int rndis_keepalive_response(USBNetState *s,
     return 0;
 }
 
+/* Prepare to receive the next packet */
+static void usb_net_reset_in_buf(USBNetState *s)
+{
+    s->in_ptr = s->in_len = 0;
+    qemu_flush_queued_packets(&s->nic->nc);
+}
+
 static int rndis_parse(USBNetState *s, uint8_t *data, int length)
 {
     uint32_t msg_type;
@@ -1025,7 +1032,8 @@ static int rndis_parse(USBNetState *s, uint8_t *data, int length)
 
     case RNDIS_RESET_MSG:
         rndis_clear_responsequeue(s);
-        s->out_ptr = s->in_ptr = s->in_len = 0;
+        s->out_ptr = 0;
+        usb_net_reset_in_buf(s);
         return rndis_reset_response(s, (rndis_reset_msg_type *) data);
 
     case RNDIS_KEEPALIVE_MSG:
@@ -1135,7 +1143,7 @@ static int usb_net_handle_datain(USBNetState *s, USBPacket *p)
     int ret = USB_RET_NAK;
 
     if (s->in_ptr > s->in_len) {
-        s->in_ptr = s->in_len = 0;
+        usb_net_reset_in_buf(s);
         ret = USB_RET_NAK;
         return ret;
     }
@@ -1152,7 +1160,7 @@ static int usb_net_handle_datain(USBNetState *s, USBPacket *p)
     if (s->in_ptr >= s->in_len &&
                     (is_rndis(s) || (s->in_len & (64 - 1)) || !ret)) {
         /* no short packet necessary */
-        s->in_ptr = s->in_len = 0;
+        usb_net_reset_in_buf(s);
     }
 
 #ifdef TRAFFIC_DEBUG
@@ -1263,6 +1271,11 @@ static ssize_t usbnet_receive(NetClientState *nc, const uint8_t *buf, size_t siz
         return -1;
     }
 
+    /* Only accept packet if input buffer is empty */
+    if (s->in_len > 0) {
+        return 0;
+    }
+
     if (is_rndis(s)) {
         struct rndis_packet_msg_type *msg;
 
Signature · 1 line
1.7.10.4
#2 lore raw

PATCH 13/13 net: EAGAIN handling for net/socket.c TCP

In reply to Stefan Hajnoczi

From: Stefan Hajnoczi <[email protected]>

Replace spinning send_all() with a proper non-blocking send. When the socket write buffer limit is reached, we should stop trying to send and wait for the socket to become writable again.

Non-blocking TCP sockets can return in two different ways when the write buffer limit is reached:

1. ret = -1 and errno = EAGAIN/EWOULDBLOCK. No data has been written.

2. ret < total_size. Short write, only part of the message was transmitted.

Handle both cases and keep track of how many bytes have been written in s->send_index. (This includes the 'length' header before the actual payload buffer.)

---
 net/socket.c |   36 +++++++++++++++++++++++++++++++-----
 1 file changed, 31 insertions(+), 5 deletions(-)
net/socket.c +31 -5
index e5e4e8d..c3e55b8 100644
@@ -32,6 +32,7 @@
 #include "qemu-error.h"
 #include "qemu-option.h"
 #include "qemu_socket.h"
+#include "iov.h"
 
 typedef struct NetSocketState {
     NetClientState nc;
@@ -40,6 +41,7 @@ typedef struct NetSocketState {
     int state; /* 0 = getting length, 1 = getting data */
     unsigned int index;
     unsigned int packet_len;
+    unsigned int send_index;      /* number of bytes sent (only SOCK_STREAM) */
     uint8_t buf[4096];
     struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
     IOHandler *send_fn;           /* differs between SOCK_STREAM/SOCK_DGRAM */
@@ -88,15 +90,39 @@ static void net_socket_writable(void *opaque)
     qemu_flush_queued_packets(&s->nc);
 }
 
-/* XXX: we consider we can send the whole packet without blocking */
 static ssize_t net_socket_receive(NetClientState *nc, const uint8_t *buf, size_t size)
 {
     NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc);
-    uint32_t len;
-    len = htonl(size);
+    uint32_t len = htonl(size);
+    struct iovec iov[] = {
+        {
+            .iov_base = &len,
+            .iov_len  = sizeof(len),
+        }, {
+            .iov_base = (void *)buf,
+            .iov_len  = size,
+        },
+    };
+    size_t remaining;
+    ssize_t ret;
+
+    remaining = iov_size(iov, 2) - s->send_index;
+    ret = iov_send(s->fd, iov, 2, s->send_index, remaining);
 
-    send_all(s->fd, (const uint8_t *)&len, sizeof(len));
-    return send_all(s->fd, buf, size);
+    if (ret == -1 && errno == EAGAIN) {
+        ret = 0; /* handled further down */
+    }
+    if (ret == -1) {
+        s->send_index = 0;
+        return -errno;
+    }
+    if (ret < (ssize_t)remaining) {
+        s->send_index += ret;
+        net_socket_write_poll(s, true);
+        return 0;
+    }
+    s->send_index = 0;
+    return size;
 }
 
 static ssize_t net_socket_receive_dgram(NetClientState *nc, const uint8_t *buf, size_t size)
Signature · 1 line
1.7.10.4
#3 lore raw
In reply to Stefan Hajnoczi
Quoted 7 lines · Stefan Hajnoczi

Fixes for hung NICs, USB network interface packet dropping, and inefficient netdev socket non-blocking I/O.

The following changes since commit 0c267217ca9985e6d118ec8368bebd382db7a099:

musicpal: Fix flash mapping (2012-09-08 10:17:57 +0000)

Quoted 7 lines

are available in the git repository at:

git://github.com/stefanha/qemu.git net

for you to fetch changes up to 0eb160d10e4f55cb5430ca3b6eb049977d0ce643:

net: EAGAIN handling for net/socket.c TCP (2012-09-10 08:12:34 +0100)

Please disregard this thread, there has been an issue with my mail queue.

I am going to resend this pull request to make sure all mails are properly delivered.

Stefan