Skip to contentSkip to conversation

kvm tools: Respect guest tcp window size

PATCH In review Asias He 2 participants 5 messages Aug 23, 2012 KVM

lore
Share & tools
lore

Asias He

author
#0 lore raw

PATCH 1/2 kvm tools: Respect guest tcp window size

Respect guest tcp window size and stop sending tcp segments to guest if guest's receive window is closed.

This fixes the TCP hang I'm seeing where guest and host are transferring big chuck of data.

This problem was not triggered when guest and external host communicates, probably because guest to external host communication walks through real network and is much slower than guest and host communication. Thus, guest's receive window has little chance to be closed.

---
 tools/kvm/include/kvm/uip.h |  1 +
 tools/kvm/net/uip/tcp.c     | 28 ++++++++++++++++++++++------
 2 files changed, 23 insertions(+), 6 deletions(-)
tools/kvm/include/kvm/uip.h +1 -0
index 4497f6a..191029b 100644
@@ -235,6 +235,7 @@ struct uip_tcp_socket {
 	pthread_t thread;
 	u32 dport, sport;
 	u32 guest_acked;
+	u16 window_size;
 	/*
 	 * Initial Sequence Number
 	 */
tools/kvm/net/uip/tcp.c +22 -6
index 586a45c..cf46bac 100644
@@ -167,25 +167,38 @@ static int uip_tcp_payload_send(struct uip_tcp_socket *sk, u8 flag, u16 payload_
 static void *uip_tcp_socket_thread(void *p)
 {
 	struct uip_tcp_socket *sk;
-	u8 *payload;
-	int ret;
+	u8 *payload, *pos;
+	int len, left, ret;
 
 	sk = p;
 
 	payload = malloc(UIP_MAX_TCP_PAYLOAD);
-	sk->payload = payload;
-	if (!sk->payload)
+	if (!payload)
 		goto out;
 
 	while (1) {
+		pos = payload;
 
 		ret = read(sk->fd, payload, UIP_MAX_TCP_PAYLOAD);
 
 		if (ret <= 0 || ret > UIP_MAX_TCP_PAYLOAD)
 			goto out;
 
-		uip_tcp_payload_send(sk, UIP_TCP_FLAG_ACK, ret);
+		left = ret;
+
+		while (left > 0) {
+			while ((len = sk->guest_acked + sk->window_size - sk->seq_server) <= 0)
+				usleep(100);
+			sk->payload = pos;
+			if (len > left)
+				len = left;
+			if (len > UIP_MAX_TCP_PAYLOAD)
+				len = UIP_MAX_TCP_PAYLOAD;
+			left -= len;
 
+			pos += len;
+			uip_tcp_payload_send(sk, UIP_TCP_FLAG_ACK, len);
+		}
 	}
 
 out:
@@ -199,7 +212,7 @@ out:
 
 	sk->read_done = 1;
 
-	free(sk->payload);
+	free(payload);
 	pthread_exit(NULL);
 
 	return NULL;
@@ -250,6 +263,8 @@ int uip_tx_do_ipv4_tcp(struct uip_tx_arg *arg)
 		if (!sk)
 			return -1;
 
+		sk->window_size = ntohs(tcp->win);
+
 		/*
 		 * Setup ISN number
 		 */
@@ -276,6 +291,7 @@ int uip_tx_do_ipv4_tcp(struct uip_tx_arg *arg)
 	if (!sk)
 		return -1;
 
+	sk->window_size = ntohs(tcp->win);
 	sk->guest_acked = ntohl(tcp->ack);
 
 	if (uip_tcp_is_fin(tcp)) {
Signature · 1 line
1.7.11.4
author
#1 lore raw

PATCH 2/2 kvm tools: Make tcp between guest/host virtual ip work in UIP mode

This pach makes tcp between 'guest ip' and 'host virtual ip' work in UIP mode. (The defulat guest ip is 192.168.33.15, host virtual ip is 192.168.33.1)

guest$ wget http://192.168.33.1/file
guest$ ssh 192.168.33.1

Without this patch, user has to figure out the ip address of host's interface (e.g. eth0) and use that ip address to access host.

With this patch, user can simply use the virtual host ip.

---
 tools/kvm/net/uip/tcp.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
tools/kvm/net/uip/tcp.c +5 -1
index cf46bac..b966d8e 100644
@@ -3,6 +3,7 @@
 #include <linux/virtio_net.h>
 #include <linux/kernel.h>
 #include <linux/list.h>
+#include <arpa/inet.h>
 
 static int uip_tcp_socket_close(struct uip_tcp_socket *sk, int how)
 {
@@ -66,8 +67,11 @@ static struct uip_tcp_socket *uip_tcp_socket_alloc(struct uip_tx_arg *arg, u32 s
 
 	sk->fd				= socket(AF_INET, SOCK_STREAM, 0);
 	sk->addr.sin_family		= AF_INET;
-	sk->addr.sin_addr.s_addr	= dip;
 	sk->addr.sin_port		= dport;
+	sk->addr.sin_addr.s_addr	= dip;
+
+	if (ntohl(dip) == arg->info->host_ip)
+		sk->addr.sin_addr.s_addr = inet_addr("127.0.0.1");
 
 	ret = connect(sk->fd, (struct sockaddr *)&sk->addr, sizeof(sk->addr));
 	if (ret) {
Signature · 1 line
1.7.11.4
#2 lore raw
In reply to Asias He
Quoted 5 lines · Asias He

Respect guest tcp window size and stop sending tcp segments to guest if guest's receive window is closed.

This fixes the TCP hang I'm seeing where guest and host are transferring big chuck of data.

Awesome!

Quoted 49 lines

This problem was not triggered when guest and external host communicates, probably because guest to external host communication walks through real network and is much slower than guest and host communication. Thus, guest's receive window has little chance to be closed.

---
 tools/kvm/include/kvm/uip.h |  1 +
 tools/kvm/net/uip/tcp.c     | 28 ++++++++++++++++++++++------
 2 files changed, 23 insertions(+), 6 deletions(-)
tools/kvm/include/kvm/uip.h +1 -0
index 4497f6a..191029b 100644
@@ -235,6 +235,7 @@ struct uip_tcp_socket {
        pthread_t thread;
        u32 dport, sport;
        u32 guest_acked;
+       u16 window_size;
        /*
         * Initial Sequence Number
         */
tools/kvm/net/uip/tcp.c +4 -4
index 586a45c..cf46bac 100644
@@ -167,25 +167,38 @@ static int uip_tcp_payload_send(struct uip_tcp_socket *sk, u8 flag, u16 payload_
 static void *uip_tcp_socket_thread(void *p)
 {
        struct uip_tcp_socket *sk;
-       u8 *payload;
-       int ret;
+       u8 *payload, *pos;
+       int len, left, ret;
 
        sk = p;
 
        payload = malloc(UIP_MAX_TCP_PAYLOAD);
-       sk->payload = payload;
-       if (!sk->payload)
+       if (!payload)
                goto out;
 
        while (1) {
+               pos = payload;
 
                ret = read(sk->fd, payload, UIP_MAX_TCP_PAYLOAD);

tools/kvm/net/uip/tcp.c static int uip_tcp_payload_send(struct uip_tcp_socket *sk, u8 flag, u16 payload_ · line 191

Quoted 10 lines
tools/kvm/net/uip/tcp.c +5 -1
 
                if (ret <= 0 || ret > UIP_MAX_TCP_PAYLOAD)
                        goto out;
 
-               uip_tcp_payload_send(sk, UIP_TCP_FLAG_ACK, ret);
+               left = ret;
+
+               while (left > 0) {
+                       while ((len = sk->guest_acked + sk->window_size - sk->seq_server) <= 0)
+                               usleep(100);

So what exactly is this piece of code trying to accomplish? Surely there's some more reasonable way to handle it?

Quoted 46 lines
tools/kvm/net/uip/tcp.c +13 -2
+                       sk->payload = pos;
+                       if (len > left)
+                               len = left;
+                       if (len > UIP_MAX_TCP_PAYLOAD)
+                               len = UIP_MAX_TCP_PAYLOAD;
+                       left -= len;
 
+                       pos += len;
+                       uip_tcp_payload_send(sk, UIP_TCP_FLAG_ACK, len);
+               }
        }
 
 out:
@@ -199,7 +212,7 @@ out:
 
        sk->read_done = 1;
 
-       free(sk->payload);
+       free(payload);
        pthread_exit(NULL);
 
        return NULL;
@@ -250,6 +263,8 @@ int uip_tx_do_ipv4_tcp(struct uip_tx_arg *arg)
                if (!sk)
                        return -1;
 
+               sk->window_size = ntohs(tcp->win);
+
                /*
                 * Setup ISN number
                 */
@@ -276,6 +291,7 @@ int uip_tx_do_ipv4_tcp(struct uip_tx_arg *arg)
        if (!sk)
                return -1;
 
+       sk->window_size = ntohs(tcp->win);
        sk->guest_acked = ntohl(tcp->ack);
 
        if (uip_tcp_is_fin(tcp)) {
--

1.7.11.4

--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

author
#3 lore raw
Quoted 62 lines · Pekka Enberg
Quoted 5 lines · Asias He

Respect guest tcp window size and stop sending tcp segments to guest if guest's receive window is closed.

This fixes the TCP hang I'm seeing where guest and host are transferring big chuck of data.

Awesome!

Quoted 53 lines

This problem was not triggered when guest and external host communicates, probably because guest to external host communication walks through real network and is much slower than guest and host communication. Thus, guest's receive window has little chance to be closed.

---
 tools/kvm/include/kvm/uip.h |  1 +
 tools/kvm/net/uip/tcp.c     | 28 ++++++++++++++++++++++------
 2 files changed, 23 insertions(+), 6 deletions(-)
tools/kvm/include/kvm/uip.h +1 -0
index 4497f6a..191029b 100644
@@ -235,6 +235,7 @@ struct uip_tcp_socket {
        pthread_t thread;
        u32 dport, sport;
        u32 guest_acked;
+       u16 window_size;
        /*
         * Initial Sequence Number
         */
tools/kvm/net/uip/tcp.c +4 -4
index 586a45c..cf46bac 100644
@@ -167,25 +167,38 @@ static int uip_tcp_payload_send(struct uip_tcp_socket *sk, u8 flag, u16 payload_
 static void *uip_tcp_socket_thread(void *p)
 {
        struct uip_tcp_socket *sk;
-       u8 *payload;
-       int ret;
+       u8 *payload, *pos;
+       int len, left, ret;
 
        sk = p;
 
        payload = malloc(UIP_MAX_TCP_PAYLOAD);
-       sk->payload = payload;
-       if (!sk->payload)
+       if (!payload)
                goto out;
 
        while (1) {
+               pos = payload;
 
                ret = read(sk->fd, payload, UIP_MAX_TCP_PAYLOAD);
 
                if (ret <= 0 || ret > UIP_MAX_TCP_PAYLOAD)
                        goto out;
 
Quoted 9 lines

tools/kvm/net/uip/tcp.c static int uip_tcp_payload_send(struct uip_tcp_socket *sk, u8 flag, u16 payload_ · line 191

Quoted 6 lines
tools/kvm/net/uip/tcp.c +5 -1
-               uip_tcp_payload_send(sk, UIP_TCP_FLAG_ACK, ret);
+               left = ret;
+
+               while (left > 0) {
+                       while ((len = sk->guest_acked + sk->window_size - sk->seq_server) <= 0)
+                               usleep(100);

So what exactly is this piece of code trying to accomplish? Surely there's some more reasonable way to handle it?

Wait until the guest is available to receive some more tcp data.

Yes, we can introduce some sync ops here to do this. But for now, I'd prefer this simple method ;-)

Quoted 46 lines
Quoted 46 lines
tools/kvm/net/uip/tcp.c +13 -2
+                       sk->payload = pos;
+                       if (len > left)
+                               len = left;
+                       if (len > UIP_MAX_TCP_PAYLOAD)
+                               len = UIP_MAX_TCP_PAYLOAD;
+                       left -= len;
 
+                       pos += len;
+                       uip_tcp_payload_send(sk, UIP_TCP_FLAG_ACK, len);
+               }
        }
 
 out:
@@ -199,7 +212,7 @@ out:
 
        sk->read_done = 1;
 
-       free(sk->payload);
+       free(payload);
        pthread_exit(NULL);
 
        return NULL;
@@ -250,6 +263,8 @@ int uip_tx_do_ipv4_tcp(struct uip_tx_arg *arg)
                if (!sk)
                        return -1;
 
+               sk->window_size = ntohs(tcp->win);
+
                /*
                 * Setup ISN number
                 */
@@ -276,6 +291,7 @@ int uip_tx_do_ipv4_tcp(struct uip_tx_arg *arg)
        if (!sk)
                return -1;
 
+       sk->window_size = ntohs(tcp->win);
        sk->guest_acked = ntohl(tcp->ack);
 
        if (uip_tcp_is_fin(tcp)) {
--

1.7.11.4

--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Signature · 1 line
Asias He
#4 lore raw
Quoted 15 lines · Asias He
Quoted 15 lines
Quoted 14 lines
        while (1) {
tools/kvm/net/uip/tcp.c +6 -1
+               pos = payload;
 
                ret = read(sk->fd, payload, UIP_MAX_TCP_PAYLOAD);
 
                if (ret <= 0 || ret > UIP_MAX_TCP_PAYLOAD)
                        goto out;
 
-               uip_tcp_payload_send(sk, UIP_TCP_FLAG_ACK, ret);
+               left = ret;
+
+               while (left > 0) {
+                       while ((len = sk->guest_acked + sk->window_size - sk->seq_server) <= 0)
+                               usleep(100);
Quoted 7 lines
Quoted 2 lines

So what exactly is this piece of code trying to accomplish? Surely there's some more reasonable way to handle it?

Wait until the guest is available to receive some more tcp data.

Yes, we can introduce some sync ops here to do this. But for now, I'd prefer this simple method ;-)

It doesn't look all that simple and since there's no locking, it's not safe. Can we use pthread condition variables here?

Pekka