peterdelevoryas / rpms / qemu

Forked from rpms/qemu 2 years ago
Clone

Blame 0196-net-asynchronous-send-receive-infrastructure-for-net.patch

5544c1
From 176b159d70cb26b24ce928497ae269b294e503d8 Mon Sep 17 00:00:00 2001
5544c1
From: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
5544c1
Date: Mon, 20 Aug 2012 10:21:54 +0100
5544c1
Subject: [PATCH] net: asynchronous send/receive infrastructure for
5544c1
 net/socket.c
5544c1
5544c1
The net/socket.c net client is not truly asynchronous.  This patch
5544c1
borrows the qemu_set_fd_handler2() code from net/tap.c as the basis for
5544c1
proper asynchronous send/receive.
5544c1
5544c1
Only read packets from the socket when the peer is able to receive.
5544c1
This avoids needless queuing.
5544c1
5544c1
Later patches implement asynchronous send.
5544c1
5544c1
Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
5544c1
(cherry picked from commit 863f678fba4191f3b695620f41056cb7c124425d)
5544c1
5544c1
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
5544c1
---
5544c1
 net/socket.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++------
5544c1
 1 file changed, 52 insertions(+), 6 deletions(-)
5544c1
5544c1
diff --git a/net/socket.c b/net/socket.c
5544c1
index c172c24..54e32f0 100644
5544c1
--- a/net/socket.c
5544c1
+++ b/net/socket.c
5544c1
@@ -42,9 +42,51 @@ typedef struct NetSocketState {
5544c1
     unsigned int packet_len;
5544c1
     uint8_t buf[4096];
5544c1
     struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
5544c1
+    IOHandler *send_fn;           /* differs between SOCK_STREAM/SOCK_DGRAM */
5544c1
+    bool read_poll;               /* waiting to receive data? */
5544c1
+    bool write_poll;              /* waiting to transmit data? */
5544c1
 } NetSocketState;
5544c1
 
5544c1
 static void net_socket_accept(void *opaque);
5544c1
+static void net_socket_writable(void *opaque);
5544c1
+
5544c1
+/* Only read packets from socket when peer can receive them */
5544c1
+static int net_socket_can_send(void *opaque)
5544c1
+{
5544c1
+    NetSocketState *s = opaque;
5544c1
+
5544c1
+    return qemu_can_send_packet(&s->nc);
5544c1
+}
5544c1
+
5544c1
+static void net_socket_update_fd_handler(NetSocketState *s)
5544c1
+{
5544c1
+    qemu_set_fd_handler2(s->fd,
5544c1
+                         s->read_poll  ? net_socket_can_send : NULL,
5544c1
+                         s->read_poll  ? s->send_fn : NULL,
5544c1
+                         s->write_poll ? net_socket_writable : NULL,
5544c1
+                         s);
5544c1
+}
5544c1
+
5544c1
+static void net_socket_read_poll(NetSocketState *s, bool enable)
5544c1
+{
5544c1
+    s->read_poll = enable;
5544c1
+    net_socket_update_fd_handler(s);
5544c1
+}
5544c1
+
5544c1
+static void net_socket_write_poll(NetSocketState *s, bool enable)
5544c1
+{
5544c1
+    s->write_poll = enable;
5544c1
+    net_socket_update_fd_handler(s);
5544c1
+}
5544c1
+
5544c1
+static void net_socket_writable(void *opaque)
5544c1
+{
5544c1
+    NetSocketState *s = opaque;
5544c1
+
5544c1
+    net_socket_write_poll(s, false);
5544c1
+
5544c1
+    qemu_flush_queued_packets(&s->nc);
5544c1
+}
5544c1
 
5544c1
 /* XXX: we consider we can send the whole packet without blocking */
5544c1
 static ssize_t net_socket_receive(NetClientState *nc, const uint8_t *buf, size_t size)
5544c1
@@ -81,7 +123,8 @@ static void net_socket_send(void *opaque)
5544c1
     } else if (size == 0) {
5544c1
         /* end of connection */
5544c1
     eoc:
5544c1
-        qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
5544c1
+        net_socket_read_poll(s, false);
5544c1
+        net_socket_write_poll(s, false);
5544c1
         if (s->listen_fd != -1) {
5544c1
             qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s);
5544c1
         }
5544c1
@@ -152,7 +195,8 @@ static void net_socket_send_dgram(void *opaque)
5544c1
         return;
5544c1
     if (size == 0) {
5544c1
         /* end of connection */
5544c1
-        qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
5544c1
+        net_socket_read_poll(s, false);
5544c1
+        net_socket_write_poll(s, false);
5544c1
         return;
5544c1
     }
5544c1
     qemu_send_packet(&s->nc, s->buf, size);
5544c1
@@ -243,7 +287,8 @@ static void net_socket_cleanup(NetClientState *nc)
5544c1
 {
5544c1
     NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc);
5544c1
     if (s->fd != -1) {
5544c1
-        qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
5544c1
+        net_socket_read_poll(s, false);
5544c1
+        net_socket_write_poll(s, false);
5544c1
         close(s->fd);
5544c1
         s->fd = -1;
5544c1
     }
5544c1
@@ -314,8 +359,8 @@ static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer,
5544c1
 
5544c1
     s->fd = fd;
5544c1
     s->listen_fd = -1;
5544c1
-
5544c1
-    qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s);
5544c1
+    s->send_fn = net_socket_send_dgram;
5544c1
+    net_socket_read_poll(s, true);
5544c1
 
5544c1
     /* mcast: save bound address as dst */
5544c1
     if (is_connected) {
5544c1
@@ -332,7 +377,8 @@ err:
5544c1
 static void net_socket_connect(void *opaque)
5544c1
 {
5544c1
     NetSocketState *s = opaque;
5544c1
-    qemu_set_fd_handler(s->fd, net_socket_send, NULL, s);
5544c1
+    s->send_fn = net_socket_send;
5544c1
+    net_socket_read_poll(s, true);
5544c1
 }
5544c1
 
5544c1
 static NetClientInfo net_socket_info = {
5544c1
-- 
5544c1
1.7.12.1
5544c1