Skip to contentSkip to conversation

Fix sorting of SACK blocks

PATCH In review Baruch Even 3 participants 7 messages Jan 25, 2007 – Jan 26, 2007

Subsystems (3)
lore
Share & tools
lore
#1 lore raw
In reply to Baruch Even
Quoted 19 lines · On Thu, 25 Jan 2007 20:29:03 +0200 Baruch Even <[email protected]> wrote:

The sorting of SACK blocks actually munges them rather than sort, causing the TCP stack to ignore some SACK information and breaking the assumption of ordered SACK blocks after sorting.

The sort takes the data from a second buffer which isn't moved causing subsequent data moves to occur from the wrong location. The fix is to use a temporary buffer as a normal sort does.

diff -X 2.6-rc6/Documentation/dontdiff -ur 2.6-rc6/net/ipv4/tcp_input.c 2.6-mod/net/ipv4/tcp_input.c

net/ipv4/tcp_input.c +0 -2
@@ -1011,10 +1011,11 @@
 			for (j = 0; j < i; j++){
 				if (after(ntohl(sp[j].start_seq),
 					  ntohl(sp[j+1].start_seq))){
-					sp[j].start_seq = htonl(tp->recv_sack_cache[j+1].start_seq);
-					sp[j].end_seq = htonl(tp->recv_sack_cache[j+1].end_seq);

net/ipv4/tcp_input.c line 1021

Quoted 10 lines
net/ipv4/tcp_input.c +5 -2
-					sp[j+1].start_seq = htonl(tp->recv_sack_cache[j].start_seq);
-					sp[j+1].end_seq = htonl(tp->recv_sack_cache[j].end_seq);
+					struct tcp_sack_block_wire tmp;
+
+					tmp = sp[j];
+					sp[j] = sp[j+1];
+					sp[j+1] = tmp;
 				}
 
 			}

This looks okay, but is there a test case that can be run?

Signature · 1 line
Stephen Hemminger <[email protected]>
#0 lore raw

PATCH 1/1 Fix sorting of SACK blocks

The sorting of SACK blocks actually munges them rather than sort, causing the TCP stack to ignore some SACK information and breaking the assumption of ordered SACK blocks after sorting.

The sort takes the data from a second buffer which isn't moved causing subsequent data moves to occur from the wrong location. The fix is to use a temporary buffer as a normal sort does.

net/ipv4/tcp_input.c +5 -4
diff -X 2.6-rc6/Documentation/dontdiff -ur 2.6-rc6/net/ipv4/tcp_input.c 2.6-mod/net/ipv4/tcp_input.c
@@ -1011,10 +1011,11 @@
 			for (j = 0; j < i; j++){
 				if (after(ntohl(sp[j].start_seq),
 					  ntohl(sp[j+1].start_seq))){
-					sp[j].start_seq = htonl(tp->recv_sack_cache[j+1].start_seq);
-					sp[j].end_seq = htonl(tp->recv_sack_cache[j+1].end_seq);
-					sp[j+1].start_seq = htonl(tp->recv_sack_cache[j].start_seq);
-					sp[j+1].end_seq = htonl(tp->recv_sack_cache[j].end_seq);
+					struct tcp_sack_block_wire tmp;
+
+					tmp = sp[j];
+					sp[j] = sp[j+1];
+					sp[j+1] = tmp;
 				}
 
 			}
#2 lore raw
In reply to Stephen Hemminger
Quoted 31 lines · Stephen Hemminger
Quoted 28 lines · On Thu, 25 Jan 2007 20:29:03 +0200 Baruch Even <[email protected]> wrote:

The sorting of SACK blocks actually munges them rather than sort, causing the TCP stack to ignore some SACK information and breaking the assumption of ordered SACK blocks after sorting.

The sort takes the data from a second buffer which isn't moved causing subsequent data moves to occur from the wrong location. The fix is to use a temporary buffer as a normal sort does.

diff -X 2.6-rc6/Documentation/dontdiff -ur 2.6-rc6/net/ipv4/tcp_input.c 2.6-mod/net/ipv4/tcp_input.c

net/ipv4/tcp_input.c +5 -4
@@ -1011,10 +1011,11 @@
 			for (j = 0; j < i; j++){
 				if (after(ntohl(sp[j].start_seq),
 					  ntohl(sp[j+1].start_seq))){
-					sp[j].start_seq = htonl(tp->recv_sack_cache[j+1].start_seq);
-					sp[j].end_seq = htonl(tp->recv_sack_cache[j+1].end_seq);
-					sp[j+1].start_seq = htonl(tp->recv_sack_cache[j].start_seq);
-					sp[j+1].end_seq = htonl(tp->recv_sack_cache[j].end_seq);
+					struct tcp_sack_block_wire tmp;
+
+					tmp = sp[j];
+					sp[j] = sp[j+1];
+					sp[j+1] = tmp;
 				}
 
Quoted 3 lines

net/ipv4/tcp_input.c line 1021

Quoted 1 line
net/ipv4/tcp_input.c +0 -0
 			}

This looks okay, but is there a test case that can be run?

There is nothing visible that shows the problem, the only option is to add some code to print the SACK blocks after sorting and run it over a large BDP connection that can be saturated. You'll obviously need to have several holes, I believe that the bug will be visible when you have ACK packets with three SACK blocks where the first block is the highest which should be the normal case.

Cheers,
Baruch

maintainer
#3 lore raw
In reply to Baruch Even

From: Baruch Even <[email protected]>
Date: Thu, 25 Jan 2007 20:29:03 +0200

Quoted 9 lines

The sorting of SACK blocks actually munges them rather than sort, causing the TCP stack to ignore some SACK information and breaking the assumption of ordered SACK blocks after sorting.

The sort takes the data from a second buffer which isn't moved causing subsequent data moves to occur from the wrong location. The fix is to use a temporary buffer as a normal sort does.

Thanks for finding this bug Baruch.

It probably explains some weird TCP traces I've seen over the years :-)

I'll review this and apply it later today, thanks again.

maintainer
#4 lore raw
In reply to Baruch Even

From: Baruch Even <[email protected]>
Date: Thu, 25 Jan 2007 20:29:03 +0200

Quoted 9 lines

The sorting of SACK blocks actually munges them rather than sort, causing the TCP stack to ignore some SACK information and breaking the assumption of ordered SACK blocks after sorting.

The sort takes the data from a second buffer which isn't moved causing subsequent data moves to occur from the wrong location. The fix is to use a temporary buffer as a normal sort does.

BTW, in reviewing this I note that there is now only one remaining use of tp->recv_sack_cache[] and that is the code earlier in this function which is trying to detect if all we are doing is extending the leading edge of a SACK block.

It would be nice to be able to clear out that usage as well, and remove recv_sack_cache[] and thus make tcp_sock smaller.

#5 lore raw
Quoted 13 lines · David Miller

From: Baruch Even <[email protected]>
Date: Thu, 25 Jan 2007 20:29:03 +0200

Quoted 9 lines

The sorting of SACK blocks actually munges them rather than sort, causing the TCP stack to ignore some SACK information and breaking the assumption of ordered SACK blocks after sorting.

The sort takes the data from a second buffer which isn't moved causing subsequent data moves to occur from the wrong location. The fix is to use a temporary buffer as a normal sort does.

Quoted 7 lines

BTW, in reviewing this I note that there is now only one remaining use of tp->recv_sack_cache[] and that is the code earlier in this function which is trying to detect if all we are doing is extending the leading edge of a SACK block.

It would be nice to be able to clear out that usage as well, and remove recv_sack_cache[] and thus make tcp_sock smaller.

You actually need recv_sack_cache to detect if you can use the fast path. Another alternative is to somehow hash the values of the sack blocks but then you rely on probabilty that you will properly detect the ability to use the fast path. Hashing will save some space but you can't get rid of it completely unless you go back to the old and slow method of SACK processing.

There were thoughts thrown a while back about using a different data structure, I think you said you started working on something like that. If that comes to fruition the cache might go.

FWIW, my other mail about possible bugs actually says that you might need to add another value to check, the number of sack blocks in the cache.

Baruch

maintainer
#6 lore raw

From: Baruch Even <[email protected]>
Date: Fri, 26 Jan 2007 08:40:09 +0200

Quoted 10 lines

You actually need recv_sack_cache to detect if you can use the fast path. Another alternative is to somehow hash the values of the sack blocks but then you rely on probabilty that you will properly detect the ability to use the fast path. Hashing will save some space but you can't get rid of it completely unless you go back to the old and slow method of SACK processing.

There were thoughts thrown a while back about using a different data structure, I think you said you started working on something like that. If that comes to fruition the cache might go.

I don't know if those ideas will go anywhere, it's difficult to make it work properly and it has to be done right since we'll likely have to change drivers to handle an aggregated send queue cleanly and we only want to do that once.

Quoted 3 lines

FWIW, my other mail about possible bugs actually says that you might need to add another value to check, the number of sack blocks in the cache.

Yes I saw that, I was about to go over that and maybe come up with some patches.