Skip to contentSkip to conversation

tcp: do not update snd_una if it is same with ack_seq

PATCH In review Yafang Shao 3 participants 5 messages Nov 3, 2018 – Nov 4, 2018

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

PATCH 1/1 tcp: do not update snd_una if it is same with ack_seq

In the slow path, TCP_SKB_SB(skb)->ack_seq may be same with tp->snd_una, and under this condition we don't need to update the snd_una.

Furthermore, tcp_ack_update_window() is only called in slow path, so introducing this check won't affect the fast path processing.

By the way, '&' is a little faster than '-', so I replaced after() with "flag & FLAG_SND_UNA_ADVANCED".

---
 net/ipv4/tcp_input.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
net/ipv4/tcp_input.c +3 -2
index 2868ef2..db5a6b7 100644
@@ -3376,7 +3376,8 @@ static int tcp_ack_update_window(struct sock *sk, const struct sk_buff *skb, u32
 		}
 	}
 
-	tcp_snd_una_update(tp, ack);
+	if (after(ack, tp->snd_una))
+		tcp_snd_una_update(tp, ack);
 
 	return flag;
 }
@@ -3610,7 +3611,7 @@ static int tcp_ack(struct sock *sk, const struct sk_buff *skb, int flag)
 	if (flag & FLAG_UPDATE_TS_RECENT)
 		tcp_replace_ts_recent(tp, TCP_SKB_CB(skb)->seq);
 
-	if (!(flag & FLAG_SLOWPATH) && after(ack, prior_snd_una)) {
+	if (!(flag & FLAG_SLOWPATH) && flag & FLAG_SND_UNA_ADVANCED) {
 		/* Window is constant, pure forward advance.
 		 * No more checks are required.
 		 * Note, we use the fact that SND.UNA>=SND.WL2.
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 5 lines · Yafang Shao

In the slow path, TCP_SKB_SB(skb)->ack_seq may be same with tp->snd_una, and under this condition we don't need to update the snd_una.

Furthermore, tcp_ack_update_window() is only called in slow path, so introducing this check won't affect the fast path processing.

[]

net/ipv4/tcp_input.c

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

[]

net/ipv4/tcp_input.c static int tcp_ack(struct sock *sk, const struct sk_buff *skb, int flag) · line 3614

Quoted 6 lines
net/ipv4/tcp_input.c +1 -1
@@ -3610,7 +3611,7 @@ static int tcp_ack(struct sock *sk, const struct sk_buff *skb, int flag)
 	if (flag & FLAG_UPDATE_TS_RECENT)
 		tcp_replace_ts_recent(tp, TCP_SKB_CB(skb)->seq);
 
-	if (!(flag & FLAG_SLOWPATH) && after(ack, prior_snd_una)) {
+	if (!(flag & FLAG_SLOWPATH) && flag & FLAG_SND_UNA_ADVANCED) {

stylistic nit:

While the precedence is correct in any case,
perhaps adding parentheses around
flag & FLAG_SND_UNA_ADVANCED
would make it more obvious.

#2 lore raw
Quoted 19 lines · Joe Perches
Quoted 5 lines · Yafang Shao

In the slow path, TCP_SKB_SB(skb)->ack_seq may be same with tp->snd_una, and under this condition we don't need to update the snd_una.

Furthermore, tcp_ack_update_window() is only called in slow path, so introducing this check won't affect the fast path processing.

[]

net/ipv4/tcp_input.c

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

[]

net/ipv4/tcp_input.c static int tcp_ack(struct sock *sk, const struct sk_buff *skb, int flag) · line 3614

Quoted 6 lines
net/ipv4/tcp_input.c +1 -1
@@ -3610,7 +3611,7 @@ static int tcp_ack(struct sock *sk, const struct sk_buff *skb, int flag)
      if (flag & FLAG_UPDATE_TS_RECENT)
              tcp_replace_ts_recent(tp, TCP_SKB_CB(skb)->seq);
 
-     if (!(flag & FLAG_SLOWPATH) && after(ack, prior_snd_una)) {
+     if (!(flag & FLAG_SLOWPATH) && flag & FLAG_SND_UNA_ADVANCED) {

stylistic nit:

Quoted 5 lines

While the precedence is correct in any case,
perhaps adding parentheses around
flag & FLAG_SND_UNA_ADVANCED
would make it more obvious.

Sure. will change it.

Thanks
Yafang

#3 lore raw
In reply to Yafang Shao
Quoted 19 lines · Yafang Shao

In the slow path, TCP_SKB_SB(skb)->ack_seq may be same with tp->snd_una, and under this condition we don't need to update the snd_una.

Furthermore, tcp_ack_update_window() is only called in slow path, so introducing this check won't affect the fast path processing.

By the way, '&' is a little faster than '-', so I replaced after() with "flag & FLAG_SND_UNA_ADVANCED".

---
 net/ipv4/tcp_input.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
net/ipv4/tcp_input.c +0 -0
index 2868ef2..db5a6b7 100644

net/ipv4/tcp_input.c static int tcp_ack_update_window(struct sock *sk, const struct sk_buff *skb, u32 · line 3381

Quoted 7 lines
net/ipv4/tcp_input.c +2 -1
@@ -3376,7 +3376,8 @@ static int tcp_ack_update_window(struct sock *sk, const struct sk_buff *skb, u32
 		}
 	}
 
-	tcp_snd_una_update(tp, ack);
+	if (after(ack, tp->snd_una))
+		tcp_snd_una_update(tp, ack);
 

Adding this after() here is confusing, how ack could be before snd_una ? That would be a serious bug.

I do not see why another conditional has any gain here.

You are trying to avoid very cheap operations :

    u32 delta = ack - tp->snd_una;

    tp->bytes_acked += delta;
    tp->snd_una = ack;

Maybe the real reason for this patch is not explained in the changelog ?

#4 lore raw
Quoted 26 lines · Eric Dumazet
Quoted 22 lines · Yafang Shao

In the slow path, TCP_SKB_SB(skb)->ack_seq may be same with tp->snd_una, and under this condition we don't need to update the snd_una.

Furthermore, tcp_ack_update_window() is only called in slow path, so introducing this check won't affect the fast path processing.

By the way, '&' is a little faster than '-', so I replaced after() with "flag & FLAG_SND_UNA_ADVANCED".

---
 net/ipv4/tcp_input.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
net/ipv4/tcp_input.c +0 -0
index 2868ef2..db5a6b7 100644
@@ -3376,7 +3376,8 @@ static int tcp_ack_update_window(struct sock *sk, const struct sk_buff *skb, u32
              }
      }
 
Quoted 8 lines

net/ipv4/tcp_input.c static int tcp_ack_update_window(struct sock *sk, const struct sk_buff *skb, u32 · line 3381

Quoted 4 lines
net/ipv4/tcp_input.c +2 -1
-     tcp_snd_una_update(tp, ack);
+     if (after(ack, tp->snd_una))
+             tcp_snd_una_update(tp, ack);
 

Adding this after() here is confusing, how ack could be before snd_una ? That would be a serious bug.

ack can't be before snd_una, but it can be equal with snd_una. Seems bellow change would be more specific,

    if (ack != tp->snd_una)
       tcp_snd_una_update(tp, ack);
Quoted 10 lines

I do not see why another conditional has any gain here.

You are trying to avoid very cheap operations :

    u32 delta = ack - tp->snd_una;

    tp->bytes_acked += delta;
    tp->snd_una = ack;

Maybe the real reason for this patch is not explained in the changelog ?

No additional reason. I just want to avoid these uneccessary operations. Because I find that this uncessary operations always happen, especially when head prediction fails and then the packet can't go to fast path processing.

Thanks
Yafang