Skip to contentSkip to conversation

tcp: implement rb-tree based retransmit queue

PATCH In review Eric Dumazet 5 participants 19 messages Oct 6, 2017 – Feb 6, 2018

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

cover tcp: implement rb-tree based retransmit queue

This patch series implement RB-tree based retransmit queue for TCP, to better match modern BDP.

Tested:

On receiver :
netem on ingress : delay 150ms 200us loss 1
GRO disabled to force stress and SACK storms.

for f in seq 1 10
do
./netperf -H lpaa6 -l30 -- -K bbr -o THROUGHPUT|tail -1
done | awk '{print $0} {sum += $0} END {printf "%7u\n",sum}'

Before patch :

323.87  351.48  339.59  338.62  306.72
204.07  304.93  291.88  202.47  176.88

-> 2840

After patch:

1700.83 2207.98 2070.17 1544.26 2114.76
2124.89 1693.14 1080.91 2216.82 1299.94
-> 18053

Average of 1805 Mbits istead of 284 Mbits.

Eric Dumazet (7):
net: add rb_to_skb() and other rb tree helpers
tcp: uninline tcp_write_queue_purge()
tcp: tcp_tx_timestamp() cleanup
tcp: tcp_mark_head_lost() optimization
tcp: reduce tcp_fastretrans_alert() verbosity
tcp: pass previous skb to tcp_shifted_skb()
tcp: implement rb-tree based retransmit queue

 include/linux/skbuff.h  |  18 +++++
 include/net/sock.h      |   7 +-
 include/net/tcp.h       | 100 ++++++++++++--------------
 net/ipv4/tcp.c          |  63 ++++++++++++----
 net/ipv4/tcp_fastopen.c |   8 +--
 net/ipv4/tcp_input.c    | 187 ++++++++++++++++++++++++------------------------
 net/ipv4/tcp_ipv4.c     |   2 +-
 net/ipv4/tcp_output.c   | 137 +++++++++++++++++++----------------
 net/ipv4/tcp_timer.c    |  24 ++++---
 net/sched/sch_netem.c   |  14 ++--
 10 files changed, 311 insertions(+), 249 deletions(-)
Signature · 1 line
2.14.2.920.gcf0c67979c-goog
#1 lore raw

PATCH 1/7 net: add rb_to_skb() and other rb tree helpers

Geeralize private netem_rb_to_skb()

TCP rtx queue will soon be converted to rb-tree,
so we will need skb_rbtree_walk() helpers.

---
 include/linux/skbuff.h  | 18 ++++++++++++++++++
 net/ipv4/tcp_fastopen.c |  8 +++-----
 net/ipv4/tcp_input.c    | 33 ++++++++++++---------------------
 net/sched/sch_netem.c   | 14 ++++----------
 4 files changed, 37 insertions(+), 36 deletions(-)
include/linux/skbuff.h +18 -0
index 01a985937867935dd615d355f4a662a9f8674b83..03634ec2f9181bdf63e5acbd2fadc831086eaa87 100644
@@ -3158,6 +3158,12 @@ static inline int __skb_grow_rcsum(struct sk_buff *skb, unsigned int len)
 	return __skb_grow(skb, len);
 }
 
+#define rb_to_skb(rb) rb_entry_safe(rb, struct sk_buff, rbnode)
+#define skb_rb_first(root) rb_to_skb(rb_first(root))
+#define skb_rb_last(root)  rb_to_skb(rb_last(root))
+#define skb_rb_next(skb)   rb_to_skb(rb_next(&(skb)->rbnode))
+#define skb_rb_prev(skb)   rb_to_skb(rb_prev(&(skb)->rbnode))
+
 #define skb_queue_walk(queue, skb) \
 		for (skb = (queue)->next;					\
 		     skb != (struct sk_buff *)(queue);				\
@@ -3172,6 +3178,18 @@ static inline int __skb_grow_rcsum(struct sk_buff *skb, unsigned int len)
 		for (; skb != (struct sk_buff *)(queue);			\
 		     skb = skb->next)
 
+#define skb_rbtree_walk(skb, root)						\
+		for (skb = skb_rb_first(root); skb != NULL;			\
+		     skb = skb_rb_next(skb))
+
+#define skb_rbtree_walk_from(skb)						\
+		for (; skb != NULL;						\
+		     skb = skb_rb_next(skb))
+
+#define skb_rbtree_walk_from_safe(skb, tmp)					\
+		for (; tmp = skb ? skb_rb_next(skb) : NULL, (skb != NULL);	\
+		     skb = tmp)
+
 #define skb_queue_walk_from_safe(queue, skb, tmp)				\
 		for (tmp = skb->next;						\
 		     skb != (struct sk_buff *)(queue);				\
net/ipv4/tcp_fastopen.c +3 -5
index 29fff14d5a53db7ba34b0e2f2feaf46dfb55513e..7ee4aadcdd711e211a0a87e315328ba84d9defb3 100644
@@ -465,17 +465,15 @@ bool tcp_fastopen_active_should_disable(struct sock *sk)
 void tcp_fastopen_active_disable_ofo_check(struct sock *sk)
 {
 	struct tcp_sock *tp = tcp_sk(sk);
-	struct rb_node *p;
-	struct sk_buff *skb;
 	struct dst_entry *dst;
+	struct sk_buff *skb;
 
 	if (!tp->syn_fastopen)
 		return;
 
 	if (!tp->data_segs_in) {
-		p = rb_first(&tp->out_of_order_queue);
-		if (p && !rb_next(p)) {
-			skb = rb_entry(p, struct sk_buff, rbnode);
+		skb = skb_rb_first(&tp->out_of_order_queue);
+		if (skb && !skb_rb_next(skb)) {
 			if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN) {
 				tcp_fastopen_active_disable(sk);
 				return;
net/ipv4/tcp_input.c +12 -21
index fb0d7ed84b94110ee95b66befcad143505665ed5..90afe41435966fc62ae8cb5799ca4c99995076dc 100644
@@ -4335,7 +4335,7 @@ static void tcp_ofo_queue(struct sock *sk)
 
 	p = rb_first(&tp->out_of_order_queue);
 	while (p) {
-		skb = rb_entry(p, struct sk_buff, rbnode);
+		skb = rb_to_skb(p);
 		if (after(TCP_SKB_CB(skb)->seq, tp->rcv_nxt))
 			break;
 
@@ -4399,7 +4399,7 @@ static int tcp_try_rmem_schedule(struct sock *sk, struct sk_buff *skb,
 static void tcp_data_queue_ofo(struct sock *sk, struct sk_buff *skb)
 {
 	struct tcp_sock *tp = tcp_sk(sk);
-	struct rb_node **p, *q, *parent;
+	struct rb_node **p, *parent;
 	struct sk_buff *skb1;
 	u32 seq, end_seq;
 	bool fragstolen;
@@ -4458,7 +4458,7 @@ static void tcp_data_queue_ofo(struct sock *sk, struct sk_buff *skb)
 	parent = NULL;
 	while (*p) {
 		parent = *p;
-		skb1 = rb_entry(parent, struct sk_buff, rbnode);
+		skb1 = rb_to_skb(parent);
 		if (before(seq, TCP_SKB_CB(skb1)->seq)) {
 			p = &parent->rb_left;
 			continue;
@@ -4503,9 +4503,7 @@ static void tcp_data_queue_ofo(struct sock *sk, struct sk_buff *skb)
 
 merge_right:
 	/* Remove other segments covered by skb. */
-	while ((q = rb_next(&skb->rbnode)) != NULL) {
-		skb1 = rb_entry(q, struct sk_buff, rbnode);
-
+	while ((skb1 = skb_rb_next(skb)) != NULL) {
 		if (!after(end_seq, TCP_SKB_CB(skb1)->seq))
 			break;
 		if (before(end_seq, TCP_SKB_CB(skb1)->end_seq)) {
@@ -4520,7 +4518,7 @@ static void tcp_data_queue_ofo(struct sock *sk, struct sk_buff *skb)
 		tcp_drop(sk, skb1);
 	}
 	/* If there is no skb after us, we are the last_skb ! */
-	if (!q)
+	if (!skb1)
 		tp->ooo_last_skb = skb;
 
 add_sack:
@@ -4706,7 +4704,7 @@ static struct sk_buff *tcp_skb_next(struct sk_buff *skb, struct sk_buff_head *li
 	if (list)
 		return !skb_queue_is_last(list, skb) ? skb->next : NULL;
 
-	return rb_entry_safe(rb_next(&skb->rbnode), struct sk_buff, rbnode);
+	return skb_rb_next(skb);
 }
 
 static struct sk_buff *tcp_collapse_one(struct sock *sk, struct sk_buff *skb,
@@ -4735,7 +4733,7 @@ static void tcp_rbtree_insert(struct rb_root *root, struct sk_buff *skb)
 
 	while (*p) {
 		parent = *p;
-		skb1 = rb_entry(parent, struct sk_buff, rbnode);
+		skb1 = rb_to_skb(parent);
 		if (before(TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb1)->seq))
 			p = &parent->rb_left;
 		else
@@ -4854,26 +4852,19 @@ static void tcp_collapse_ofo_queue(struct sock *sk)
 {
 	struct tcp_sock *tp = tcp_sk(sk);
 	struct sk_buff *skb, *head;
-	struct rb_node *p;
 	u32 start, end;
 
-	p = rb_first(&tp->out_of_order_queue);
-	skb = rb_entry_safe(p, struct sk_buff, rbnode);
+	skb = skb_rb_first(&tp->out_of_order_queue);
 new_range:
 	if (!skb) {
-		p = rb_last(&tp->out_of_order_queue);
-		/* Note: This is possible p is NULL here. We do not
-		 * use rb_entry_safe(), as ooo_last_skb is valid only
-		 * if rbtree is not empty.
-		 */
-		tp->ooo_last_skb = rb_entry(p, struct sk_buff, rbnode);
+		tp->ooo_last_skb = skb_rb_last(&tp->out_of_order_queue);
 		return;
 	}
 	start = TCP_SKB_CB(skb)->seq;
 	end = TCP_SKB_CB(skb)->end_seq;
 
 	for (head = skb;;) {
-		skb = tcp_skb_next(skb, NULL);
+		skb = skb_rb_next(skb);
 
 		/* Range is terminated when we see a gap or when
 		 * we are at the queue end.
@@ -4916,14 +4907,14 @@ static bool tcp_prune_ofo_queue(struct sock *sk)
 	do {
 		prev = rb_prev(node);
 		rb_erase(node, &tp->out_of_order_queue);
-		tcp_drop(sk, rb_entry(node, struct sk_buff, rbnode));
+		tcp_drop(sk, rb_to_skb(node));
 		sk_mem_reclaim(sk);
 		if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
 		    !tcp_under_memory_pressure(sk))
 			break;
 		node = prev;
 	} while (node);
-	tp->ooo_last_skb = rb_entry(prev, struct sk_buff, rbnode);
+	tp->ooo_last_skb = rb_to_skb(prev);
 
 	/* Reset SACK state.  A conforming SACK implementation will
 	 * do the same at a timeout based retransmit.  When a connection
net/sched/sch_netem.c +4 -10
index 5a4f1008029068372019a965186e7a3c0a18aac3..db0228a65e8c58d770374a9d4324f07a7f8d8e0c 100644
@@ -148,12 +148,6 @@ struct netem_skb_cb {
 	psched_time_t	time_to_send;
 };
 
-
-static struct sk_buff *netem_rb_to_skb(struct rb_node *rb)
-{
-	return rb_entry(rb, struct sk_buff, rbnode);
-}
-
 static inline struct netem_skb_cb *netem_skb_cb(struct sk_buff *skb)
 {
 	/* we assume we can use skb next/prev/tstamp as storage for rb_node */
@@ -364,7 +358,7 @@ static void tfifo_reset(struct Qdisc *sch)
 	struct rb_node *p = rb_first(&q->t_root);
 
 	while (p) {
-		struct sk_buff *skb = netem_rb_to_skb(p);
+		struct sk_buff *skb = rb_to_skb(p);
 
 		p = rb_next(p);
 		rb_erase(&skb->rbnode, &q->t_root);
@@ -382,7 +376,7 @@ static void tfifo_enqueue(struct sk_buff *nskb, struct Qdisc *sch)
 		struct sk_buff *skb;
 
 		parent = *p;
-		skb = netem_rb_to_skb(parent);
+		skb = rb_to_skb(parent);
 		if (tnext >= netem_skb_cb(skb)->time_to_send)
 			p = &parent->rb_right;
 		else
@@ -538,7 +532,7 @@ static int netem_enqueue(struct sk_buff *skb, struct Qdisc *sch,
 				struct sk_buff *t_skb;
 				struct netem_skb_cb *t_last;
 
-				t_skb = netem_rb_to_skb(rb_last(&q->t_root));
+				t_skb = skb_rb_last(&q->t_root);
 				t_last = netem_skb_cb(t_skb);
 				if (!last ||
 				    t_last->time_to_send > last->time_to_send) {
@@ -617,7 +611,7 @@ static struct sk_buff *netem_dequeue(struct Qdisc *sch)
 	if (p) {
 		psched_time_t time_to_send;
 
-		skb = netem_rb_to_skb(p);
+		skb = rb_to_skb(p);
 
 		/* if more time remaining? */
 		time_to_send = netem_skb_cb(skb)->time_to_send;
Signature · 1 line
2.14.2.920.gcf0c67979c-goog
#2 lore raw

PATCH 2/7 tcp: uninline tcp_write_queue_purge()

In reply to Eric Dumazet

Since the upcoming rtx rbtree will add some extra code, it is time to not inline this fat function anymore.

---
 include/net/tcp.h | 15 +--------------
 net/ipv4/tcp.c    | 14 ++++++++++++++
 2 files changed, 15 insertions(+), 14 deletions(-)
include/net/tcp.h +1 -14
index 3b16f353b539a563dae0b37328a52d67e6476f31..744559b727847db79b9751e6c6bef57e83c141f8 100644
@@ -1606,20 +1606,7 @@ static inline void tcp_skb_tsorted_anchor_cleanup(struct sk_buff *skb)
 	skb->_skb_refdst = _save;		\
 }
 
-/* write queue abstraction */
-static inline void tcp_write_queue_purge(struct sock *sk)
-{
-	struct sk_buff *skb;
-
-	tcp_chrono_stop(sk, TCP_CHRONO_BUSY);
-	while ((skb = __skb_dequeue(&sk->sk_write_queue)) != NULL) {
-		tcp_skb_tsorted_anchor_cleanup(skb);
-		sk_wmem_free_skb(sk, skb);
-	}
-	INIT_LIST_HEAD(&tcp_sk(sk)->tsorted_sent_queue);
-	sk_mem_reclaim(sk);
-	tcp_clear_all_retrans_hints(tcp_sk(sk));
-}
+void tcp_write_queue_purge(struct sock *sk);
 
 static inline struct sk_buff *tcp_write_queue_head(const struct sock *sk)
 {
net/ipv4/tcp.c +14 -0
index 8cf742fd4f99d7eb1bd9632afcfb09c36ba1130e..f8ebae62f834ce6059d5bcf7dcb84988187dfbce 100644
@@ -2318,6 +2318,20 @@ static inline bool tcp_need_reset(int state)
 		TCPF_FIN_WAIT2 | TCPF_SYN_RECV);
 }
 
+void tcp_write_queue_purge(struct sock *sk)
+{
+	struct sk_buff *skb;
+
+	tcp_chrono_stop(sk, TCP_CHRONO_BUSY);
+	while ((skb = __skb_dequeue(&sk->sk_write_queue)) != NULL) {
+		tcp_skb_tsorted_anchor_cleanup(skb);
+		sk_wmem_free_skb(sk, skb);
+	}
+	INIT_LIST_HEAD(&tcp_sk(sk)->tsorted_sent_queue);
+	sk_mem_reclaim(sk);
+	tcp_clear_all_retrans_hints(tcp_sk(sk));
+}
+
 int tcp_disconnect(struct sock *sk, int flags)
 {
 	struct inet_sock *inet = inet_sk(sk);
Signature · 1 line
2.14.2.920.gcf0c67979c-goog
#3 lore raw

PATCH 3/7 tcp: tcp_tx_timestamp() cleanup

In reply to Eric Dumazet

tcp_write_queue_tail() call can be factorized.

---
 net/ipv4/tcp.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
net/ipv4/tcp.c +5 -3
index f8ebae62f834ce6059d5bcf7dcb84988187dfbce..b8d379c8093661ccc4ef7fb82fa92828ab5f6918 100644
@@ -469,8 +469,10 @@ void tcp_init_transfer(struct sock *sk, int bpf_op)
 	tcp_init_buffer_space(sk);
 }
 
-static void tcp_tx_timestamp(struct sock *sk, u16 tsflags, struct sk_buff *skb)
+static void tcp_tx_timestamp(struct sock *sk, u16 tsflags)
 {
+	struct sk_buff *skb = tcp_write_queue_tail(sk);
+
 	if (tsflags && skb) {
 		struct skb_shared_info *shinfo = skb_shinfo(skb);
 		struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
@@ -1041,7 +1043,7 @@ ssize_t do_tcp_sendpages(struct sock *sk, struct page *page, int offset,
 
 out:
 	if (copied) {
-		tcp_tx_timestamp(sk, sk->sk_tsflags, tcp_write_queue_tail(sk));
+		tcp_tx_timestamp(sk, sk->sk_tsflags);
 		if (!(flags & MSG_SENDPAGE_NOTLAST))
 			tcp_push(sk, flags, mss_now, tp->nonagle, size_goal);
 	}
@@ -1418,7 +1420,7 @@ int tcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t size)
 
 out:
 	if (copied) {
-		tcp_tx_timestamp(sk, sockc.tsflags, tcp_write_queue_tail(sk));
+		tcp_tx_timestamp(sk, sockc.tsflags);
 		tcp_push(sk, flags, mss_now, tp->nonagle, size_goal);
 	}
 out_nopush:
Signature · 1 line
2.14.2.920.gcf0c67979c-goog
#4 lore raw

PATCH 4/7 tcp: tcp_mark_head_lost() optimization

In reply to Eric Dumazet

It will be a bit more expensive to get the head of rtx queue once rtx queue is converted to an rb-tree.

We can avoid this extra cost in case tp->lost_skb_hint is set.

---
 net/ipv4/tcp_input.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
net/ipv4/tcp_input.c +4 -4
index 90afe41435966fc62ae8cb5799ca4c99995076dc..abc3b1db81f85a7feebda40cebc90c07afcef446 100644
@@ -2207,12 +2207,12 @@ static void tcp_mark_head_lost(struct sock *sk, int packets, int mark_head)
 	const u32 loss_high = tcp_is_sack(tp) ?  tp->snd_nxt : tp->high_seq;
 
 	WARN_ON(packets > tp->packets_out);
-	if (tp->lost_skb_hint) {
-		skb = tp->lost_skb_hint;
-		cnt = tp->lost_cnt_hint;
+	skb = tp->lost_skb_hint;
+	if (skb) {
 		/* Head already handled? */
-		if (mark_head && skb != tcp_write_queue_head(sk))
+		if (mark_head && after(TCP_SKB_CB(skb)->seq, tp->snd_una))
 			return;
+		cnt = tp->lost_cnt_hint;
 	} else {
 		skb = tcp_write_queue_head(sk);
 		cnt = 0;
Signature · 1 line
2.14.2.920.gcf0c67979c-goog
#5 lore raw

PATCH 5/7 tcp: reduce tcp_fastretrans_alert() verbosity

In reply to Eric Dumazet

With upcoming rb-tree implementation, the checks will trigger more often, and this is expected.

---
 net/ipv4/tcp_input.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/ipv4/tcp_input.c +2 -2
index abc3b1db81f85a7feebda40cebc90c07afcef446..be7644204cd4493d0574aa065afbd6cb82a6b3bb 100644
@@ -2804,9 +2804,9 @@ static void tcp_fastretrans_alert(struct sock *sk, const int acked,
 	bool do_lost = is_dupack || ((flag & FLAG_DATA_SACKED) &&
 				    (tcp_fackets_out(tp) > tp->reordering));
 
-	if (WARN_ON(!tp->packets_out && tp->sacked_out))
+	if (!tp->packets_out && tp->sacked_out)
 		tp->sacked_out = 0;
-	if (WARN_ON(!tp->sacked_out && tp->fackets_out))
+	if (!tp->sacked_out && tp->fackets_out)
 		tp->fackets_out = 0;
 
 	/* Now state machine starts.
Signature · 1 line
2.14.2.920.gcf0c67979c-goog
#6 lore raw

PATCH 6/7 tcp: pass previous skb to tcp_shifted_skb()

In reply to Eric Dumazet

No need to recompute previous skb, as it will be a bit more expensive when rtx queue is converted to RB tree.

---
 net/ipv4/tcp_input.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
net/ipv4/tcp_input.c +5 -4
index be7644204cd4493d0574aa065afbd6cb82a6b3bb..72c4732ae2da122a878e4673ce56812055de1972 100644
@@ -1288,13 +1288,13 @@ static u8 tcp_sacktag_one(struct sock *sk,
 /* Shift newly-SACKed bytes from this skb to the immediately previous
  * already-SACKed sk_buff. Mark the newly-SACKed bytes as such.
  */
-static bool tcp_shifted_skb(struct sock *sk, struct sk_buff *skb,
+static bool tcp_shifted_skb(struct sock *sk, struct sk_buff *prev,
+			    struct sk_buff *skb,
 			    struct tcp_sacktag_state *state,
 			    unsigned int pcount, int shifted, int mss,
 			    bool dup_sack)
 {
 	struct tcp_sock *tp = tcp_sk(sk);
-	struct sk_buff *prev = tcp_write_queue_prev(sk, skb);
 	u32 start_seq = TCP_SKB_CB(skb)->seq;	/* start of newly-SACKed */
 	u32 end_seq = start_seq + shifted;	/* end of newly-SACKed */
 
@@ -1495,7 +1495,7 @@ static struct sk_buff *tcp_shift_skb_data(struct sock *sk, struct sk_buff *skb,
 
 	if (!skb_shift(prev, skb, len))
 		goto fallback;
-	if (!tcp_shifted_skb(sk, skb, state, pcount, len, mss, dup_sack))
+	if (!tcp_shifted_skb(sk, prev, skb, state, pcount, len, mss, dup_sack))
 		goto out;
 
 	/* Hole filled allows collapsing with the next as well, this is very
@@ -1514,7 +1514,8 @@ static struct sk_buff *tcp_shift_skb_data(struct sock *sk, struct sk_buff *skb,
 	len = skb->len;
 	if (skb_shift(prev, skb, len)) {
 		pcount += tcp_skb_pcount(skb);
-		tcp_shifted_skb(sk, skb, state, tcp_skb_pcount(skb), len, mss, 0);
+		tcp_shifted_skb(sk, prev, skb, state, tcp_skb_pcount(skb),
+				len, mss, 0);
 	}
 
 out:
Signature · 1 line
2.14.2.920.gcf0c67979c-goog
#7 lore raw

PATCH 7/7 tcp: implement rb-tree based retransmit queue

In reply to Eric Dumazet

Using a linear list to store all skbs in write queue has been okay for quite a while : O(N) is not too bad when N < 500.

Things get messy when N is the order of 100,000 : Modern TCP stacks want 10Gbit+ of throughput even with 200 ms RTT flows.

40 ns per cache line miss means a full scan can use 4 ms, blowing away CPU caches.

SACK processing often can use various hints to avoid parsing whole retransmit queue. But with high packet losses and/or high reordering, hints no longer work.

Sender has to process thousands of unfriendly SACK, accumulating a huge socket backlog, burning a cpu and massively dropping packets.

Using an rb-tree for retransmit queue has been avoided for years because it added complexity and overhead, but now is the time to be more resistant and say no to quadratic behavior.

1) RTX queue is no longer part of the write queue : already sent skbs are stored in one rb-tree.

2) Since reaching the head of write queue no longer needs sk->sk_send_head, we added an union of sk_send_head and tcp_rtx_queue

Tested:

On receiver :
netem on ingress : delay 150ms 200us loss 1
GRO disabled to force stress and SACK storms.

for f in seq 1 10
do
./netperf -H lpaa6 -l30 -- -K bbr -o THROUGHPUT|tail -1
done | awk '{print $0} {sum += $0} END {printf "%7u\n",sum}'

Before patch :

323.87
351.48
339.59
338.62
306.72
204.07
304.93
291.88
202.47
176.88
2840

After patch:

1700.83
2207.98
2070.17
1544.26
2114.76
2124.89
1693.14
1080.91
2216.82
1299.94
18053

---
 include/net/sock.h    |   7 ++-
 include/net/tcp.h     |  89 ++++++++++++++++----------------
 net/ipv4/tcp.c        |  41 +++++++++++----
 net/ipv4/tcp_input.c  | 133 +++++++++++++++++++++++++-----------------------
 net/ipv4/tcp_ipv4.c   |   2 +-
 net/ipv4/tcp_output.c | 137 +++++++++++++++++++++++++++-----------------------
 net/ipv4/tcp_timer.c  |  24 +++++----
 7 files changed, 245 insertions(+), 188 deletions(-)
include/net/sock.h +5 -2
index a6b9a8d1a6df3f72df8f1aac0f577257fa6452d0..4827094f1db44fce1c044c6fb6a4a98f8dbd4b22 100644
@@ -60,7 +60,7 @@
 #include <linux/sched.h>
 #include <linux/wait.h>
 #include <linux/cgroup-defs.h>
-
+#include <linux/rbtree.h>
 #include <linux/filter.h>
 #include <linux/rculist_nulls.h>
 #include <linux/poll.h>
@@ -397,7 +397,10 @@ struct sock {
 	int			sk_wmem_queued;
 	refcount_t		sk_wmem_alloc;
 	unsigned long		sk_tsq_flags;
-	struct sk_buff		*sk_send_head;
+	union {
+		struct sk_buff	*sk_send_head;
+		struct rb_root	tcp_rtx_queue;
+	};
 	struct sk_buff_head	sk_write_queue;
 	__s32			sk_peek_off;
 	int			sk_write_pending;
include/net/tcp.h +47 -42

172 lines not on this page · raw message

net/ipv4/tcp.c +32 -9

108 lines not on this page · raw message

net/ipv4/tcp_input.c +71 -62

322 lines not on this page · raw message

net/ipv4/tcp_ipv4.c +1 -1
index c7460fd90884c2f04ae135547836bcff5108fc13..5418ecf03b787a645b1b7ae32207a155f24b87b0 100644
@@ -480,7 +480,7 @@ void tcp_v4_err(struct sk_buff *icmp_skb, u32 info)
 					       TCP_TIMEOUT_INIT;
 		icsk->icsk_rto = inet_csk_rto_backoff(icsk, TCP_RTO_MAX);
 
-		skb = tcp_write_queue_head(sk);
+		skb = tcp_rtx_queue_head(sk);
 		BUG_ON(!skb);
 
 		tcp_mstamp_refresh(tp);
net/ipv4/tcp_output.c +74 -63

366 lines not on this page · raw message

net/ipv4/tcp_timer.c +15 -9
index 655dd8d7f064f998f7caa323822dfffe9984ebdf..7014cc00c74c7107b8dd1ffcbdfeef8092792ba2 100644
@@ -156,8 +156,13 @@ static bool retransmits_timed_out(struct sock *sk,
 		return false;
 
 	start_ts = tcp_sk(sk)->retrans_stamp;
-	if (unlikely(!start_ts))
-		start_ts = tcp_skb_timestamp(tcp_write_queue_head(sk));
+	if (unlikely(!start_ts)) {
+		struct sk_buff *head = tcp_rtx_queue_head(sk);
+
+		if (!head)
+			return false;
+		start_ts = tcp_skb_timestamp(head);
+	}
 
 	if (likely(timeout == 0)) {
 		linear_backoff_thresh = ilog2(TCP_RTO_MAX/rto_base);
@@ -304,11 +309,12 @@ static void tcp_delack_timer(unsigned long data)
 static void tcp_probe_timer(struct sock *sk)
 {
 	struct inet_connection_sock *icsk = inet_csk(sk);
+	struct sk_buff *skb = tcp_send_head(sk);
 	struct tcp_sock *tp = tcp_sk(sk);
 	int max_probes;
 	u32 start_ts;
 
-	if (tp->packets_out || !tcp_send_head(sk)) {
+	if (tp->packets_out || !skb) {
 		icsk->icsk_probes_out = 0;
 		return;
 	}
@@ -321,9 +327,9 @@ static void tcp_probe_timer(struct sock *sk)
 	 * corresponding system limit. We also implement similar policy when
 	 * we use RTO to probe window in tcp_retransmit_timer().
 	 */
-	start_ts = tcp_skb_timestamp(tcp_send_head(sk));
+	start_ts = tcp_skb_timestamp(skb);
 	if (!start_ts)
-		tcp_send_head(sk)->skb_mstamp = tp->tcp_mstamp;
+		skb->skb_mstamp = tp->tcp_mstamp;
 	else if (icsk->icsk_user_timeout &&
 		 (s32)(tcp_time_stamp(tp) - start_ts) >
 		 jiffies_to_msecs(icsk->icsk_user_timeout))
@@ -408,7 +414,7 @@ void tcp_retransmit_timer(struct sock *sk)
 	if (!tp->packets_out)
 		goto out;
 
-	WARN_ON(tcp_write_queue_empty(sk));
+	WARN_ON(tcp_rtx_queue_empty(sk));
 
 	tp->tlp_high_seq = 0;
 
@@ -441,7 +447,7 @@ void tcp_retransmit_timer(struct sock *sk)
 			goto out;
 		}
 		tcp_enter_loss(sk);
-		tcp_retransmit_skb(sk, tcp_write_queue_head(sk), 1);
+		tcp_retransmit_skb(sk, tcp_rtx_queue_head(sk), 1);
 		__sk_dst_reset(sk);
 		goto out_reset_timer;
 	}
@@ -473,7 +479,7 @@ void tcp_retransmit_timer(struct sock *sk)
 
 	tcp_enter_loss(sk);
 
-	if (tcp_retransmit_skb(sk, tcp_write_queue_head(sk), 1) > 0) {
+	if (tcp_retransmit_skb(sk, tcp_rtx_queue_head(sk), 1) > 0) {
 		/* Retransmission failed because of local congestion,
 		 * do not backoff.
 		 */
@@ -647,7 +653,7 @@ static void tcp_keepalive_timer (unsigned long data)
 	elapsed = keepalive_time_when(tp);
 
 	/* It is alive without keepalive 8) */
-	if (tp->packets_out || tcp_send_head(sk))
+	if (tp->packets_out || !tcp_write_queue_empty(sk))
 		goto resched;
 
 	elapsed = keepalive_time_elapsed(tp);
Signature · 1 line
2.14.2.920.gcf0c67979c-goog
maintainer
#8 lore raw
In reply to Eric Dumazet

From: Eric Dumazet <[email protected]>
Date: Thu, 5 Oct 2017 22:21:20 -0700

Quoted 2 lines

This patch series implement RB-tree based retransmit queue for TCP, to better match modern BDP.

Indeed, there was a lot of resistence to this due to the overhead for small retransmit queue sizes, but with today's scale this is long overdue.

So, series applied, nice work!

Maybe we can look into dynamic schemes where when the queue never goes over N entries we elide the rbtree and use a list. I'm not so sure how practical that would be.

Thanks!

#9 lore raw

Hi Eric,
We have noticed a degradation on both of our drivers (mlx4 and mlx5) when running TCP. Exact scenario is single stream TCP with 1KB packets. The degradation is a steady 50% drop. We tracked the offending commit to be:
75c119a ("tcp: implement rb-tree based retransmit queue")

Since mlx4 and mlx5 code base is completely different and by looking at the changes in this commit, we believe the issue is external to the mlx4/5 drivers.

I see in the comment below you anticipated some overhead, but this may be a too common case to ignore.

Can you please review and consider reverting/fixing it?

Thanks,

Tal G.

Quoted 18 lines · David Miller

From: Eric Dumazet <[email protected]>
Date: Thu, 5 Oct 2017 22:21:20 -0700

Quoted 2 lines

This patch series implement RB-tree based retransmit queue for TCP, to better match modern BDP.

Indeed, there was a lot of resistence to this due to the overhead for small retransmit queue sizes, but with today's scale this is long overdue.

So, series applied, nice work!

Maybe we can look into dynamic schemes where when the queue never goes over N entries we elide the rbtree and use a list. I'm not so sure how practical that would be.

Thanks!

#10 lore raw
Quoted 7 lines · Tal Gilboa

Hi Eric,
We have noticed a degradation on both of our drivers (mlx4 and mlx5) when running TCP. Exact scenario is single stream TCP with 1KB packets. The degradation is a steady 50% drop.
We tracked the offending commit to be:
75c119a ("tcp: implement rb-tree based retransmit queue")

Quoted 9 lines

Since mlx4 and mlx5 code base is completely different and by looking at the changes in this commit, we believe the issue is external to the mlx4/5 drivers.

I see in the comment below you anticipated some overhead, but this may be a too common case to ignore.

Can you please review and consider reverting/fixing it?

Hi Tal

You have to provide way more details than a simple mail, asking for a " revert or a fix " ...

On our GFEs, we got a win, while I was expecting a small overhead, given the apparent complexity of dealing with RB tree instead of linear list.

And on the stress scenario described in my patch set, the win was absolutely abysmal.

A " single strean TCP with 1KB packets" is not something we need to optimize, unless there is some really strange setup for one of your customers ?

Here we deal with millions of TCP flows, and this is what we need to take care of.

Thanks.

Quoted 27 lines

Thanks,

Tal G.

Quoted 20 lines · David Miller

From: Eric Dumazet <[email protected]>
Date: Thu, 5 Oct 2017 22:21:20 -0700

Quoted 2 lines

This patch series implement RB-tree based retransmit queue for TCP, to better match modern BDP.

Indeed, there was a lot of resistence to this due to the overhead for small retransmit queue sizes, but with today's scale this is long overdue.

So, series applied, nice work!

Maybe we can look into dynamic schemes where when the queue never goes over N entries we elide the rbtree and use a list. I'm not so sure how practical that would be.

Thanks!

#11 lore raw

Hi Eric,
My choice of words in my comment was misplaced, and I apologies. It completely missed the point. I understand, of course, the importance of optimizing real-life scenarios.

We are currently evaluating this patch and if/how it might affect our customers. We would also evaluate your suggestion below.

We will contact you if and when we have a real concern. Thanks.

Quoted 65 lines · Eric Dumazet
Quoted 7 lines · Tal Gilboa

Hi Eric,
We have noticed a degradation on both of our drivers (mlx4 and mlx5) when running TCP. Exact scenario is single stream TCP with 1KB packets. The degradation is a steady 50% drop.
We tracked the offending commit to be:
75c119a ("tcp: implement rb-tree based retransmit queue")

Quoted 9 lines

Since mlx4 and mlx5 code base is completely different and by looking at the changes in this commit, we believe the issue is external to the mlx4/5 drivers.

I see in the comment below you anticipated some overhead, but this may be a too common case to ignore.

Can you please review and consider reverting/fixing it?

Hi Tal

You have to provide way more details than a simple mail, asking for a " revert or a fix " ...

On our GFEs, we got a win, while I was expecting a small overhead, given the apparent complexity of dealing with RB tree instead of linear list.

And on the stress scenario described in my patch set, the win was absolutely abysmal.

A " single strean TCP with 1KB packets" is not something we need to optimize, unless there is some really strange setup for one of your customers ?

Here we deal with millions of TCP flows, and this is what we need to take care of.

Thanks.

Quoted 27 lines

Thanks,

Tal G.

Quoted 20 lines · David Miller

From: Eric Dumazet <[email protected]>
Date: Thu, 5 Oct 2017 22:21:20 -0700

Quoted 2 lines

This patch series implement RB-tree based retransmit queue for TCP, to better match modern BDP.

Indeed, there was a lot of resistence to this due to the overhead for small retransmit queue sizes, but with today's scale this is long overdue.

So, series applied, nice work!

Maybe we can look into dynamic schemes where when the queue never goes over N entries we elide the rbtree and use a list. I'm not so sure how practical that would be.

Thanks!

#12 lore raw
Quoted 10 lines · Tal Gilboa

Hi Eric,
My choice of words in my comment was misplaced, and I apologies. It completely missed the point. I understand, of course, the importance of optimizing real-life scenarios.

We are currently evaluating this patch and if/how it might affect our customers. We would also evaluate your suggestion below.

We will contact you if and when we have a real concern. Thanks.

Sure, I am curious how a 50% regression can be possible as a matter of fact, so please update even if this caused by some specific synthetic test conditions.

Thanks.

#13 lore raw
Quoted 9 lines · Eric Dumazet
Quoted 8 lines · Tal Gilboa

Hi Eric,
My choice of words in my comment was misplaced, and I apologies. It completely missed the point. I understand, of course, the importance of optimizing real-life scenarios.

We are currently evaluating this patch and if/how it might affect our customers. We would also evaluate your suggestion below.

Quoted 9 lines
Quoted 2 lines

We will contact you if and when we have a real concern. Thanks.

Sure, I am curious how a 50% regression can be possible as a matter of fact, so please update even if this caused by some specific synthetic test conditions.

Thanks.

Sorry for the delay in the response. I ran super_netperf for 64B, 128B, 256B and 512B and 500, 1000, 2000 and 4000 streams and compared these (consecutive) commits:
Base - f331981 tcp: pass previous skb to tcp_shifted_skb()
rb_tree - 75c119a tcp: implement rb-tree based retransmit queue

I got lower BW with rb-tree for all cases.
Example - 2000 streams results (in Gb/s):

size | Base | rb-tree | degradation
 64  | 25.6 | 23.3    | -9%
 128 | 52.8 | 44.43   | -16%
 256 | 89.8 | 66.1    | -26.5%
 512 | 87.7 | 67.8    | -22.7%

I'm currently working on improving our CPU utilization in TX flow (by better utilizing payload aggregation mechanisms). It somewhat improves the rb-tree results when applied on top of it, but not for all cases and not to the "base" results.

#14 lore raw
Quoted 37 lines · Tal Gilboa
Quoted 15 lines · Eric Dumazet
Quoted 11 lines · Tal Gilboa

Hi Eric,
My choice of words in my comment was misplaced, and I apologies. It completely missed the point. I understand, of course, the importance of optimizing real-life scenarios.

We are currently evaluating this patch and if/how it might affect our customers. We would also evaluate your suggestion below.

We will contact you if and when we have a real concern. Thanks.

Quoted 6 lines

Sure, I am curious how a 50% regression can be possible as a matter of fact, so please update even if this caused by some specific synthetic test conditions.

Thanks.

Sorry for the delay in the response. I ran super_netperf for 64B, 128B, 256B and 512B and 500, 1000, 2000 and 4000 streams and compared these (consecutive) commits:
Base - f331981 tcp: pass previous skb to tcp_shifted_skb() rb_tree - 75c119a tcp: implement rb-tree based retransmit queue

I got lower BW with rb-tree for all cases.
Example - 2000 streams results (in Gb/s):

size | Base | rb-tree | degradation
 64  | 25.6 | 23.3    | -9%
 128 | 52.8 | 44.43   | -16%
 256 | 89.8 | 66.1    | -26.5%
 512 | 87.7 | 67.8    | -22.7%
Quoted 4 lines

I'm currently working on improving our CPU utilization in TX flow (by better utilizing payload aggregation mechanisms). It somewhat improves the rb-tree results when applied on top of it, but not for all cases and not to the "base" results.

Hi

Please give exact details.
Sending 64, 128, 256 or 512 bytes at a time on TCP_STREAM makes little sense. We are not optimizing stack for pathological cases, sorry.

If you are using MSG_EOR to force silly skbs in the rtx queue, then you should not do that ...

Thanks.

#15 lore raw

From: Eric Dumazet

Quoted 1 line

Sent: 06 February 2018 14:20

...

Quoted 3 lines

Please give exact details.
Sending 64, 128, 256 or 512 bytes at a time on TCP_STREAM makes little sense. We are not optimizing stack for pathological cases, sorry.

There are plenty of workloads which are not bulk data and where multiple small buffers get sent at unknown intervals (which may be back to back). Such connections have to have Nagle disabled because the Nagle delays are 'horrid'.
Clearly lost packets can cause delays, but they are rare on local networks.

David

#16 lore raw
Quoted 8 lines · David Laight

From: Eric Dumazet

Quoted 1 line

Sent: 06 February 2018 14:20

...

Quoted 3 lines

Please give exact details.
Sending 64, 128, 256 or 512 bytes at a time on TCP_STREAM makes little sense. We are not optimizing stack for pathological cases, sorry.

Quoted 5 lines

There are plenty of workloads which are not bulk data and where multiple small buffers get sent at unknown intervals (which may be back to back). Such connections have to have Nagle disabled because the Nagle delays are 'horrid'.
Clearly lost packets can cause delays, but they are rare on local networks.

Auto corking makes sure aggregation happens, even for when Nagle is in the picture.

netperf -- -m 256 will still cook 64KB TSO packets

netperf is not adding delays between each send(), unless it has been modified.

#17 lore raw
Quoted 15 lines · Eric Dumazet
Quoted 8 lines · David Laight

From: Eric Dumazet

Quoted 1 line

Sent: 06 February 2018 14:20

...

Quoted 3 lines

Please give exact details.
Sending 64, 128, 256 or 512 bytes at a time on TCP_STREAM makes little sense. We are not optimizing stack for pathological cases, sorry.

Quoted 5 lines

There are plenty of workloads which are not bulk data and where multiple small buffers get sent at unknown intervals (which may be back to back). Such connections have to have Nagle disabled because the Nagle delays are 'horrid'.
Clearly lost packets can cause delays, but they are rare on local networks.

Quoted 5 lines

Auto corking makes sure aggregation happens, even for when Nagle is in the picture.

netperf -- -m 256 will still cook 64KB TSO packets

This is what we would have liked to see, but auto corking isn't forcing 64KB TSO packets. Under certain conditions, specifically when TX queue is empty, it would send the SKB to transmit even if it isn't full:
static bool tcp_should_autocork(struct sock *sk, struct sk_buff *skb,
int size_goal)

{
	return skb->len < size_goal &&
	       sock_net(sk)->ipv4.sysctl_tcp_autocorking &&
	       skb != tcp_write_queue_head(sk) &&
	       refcount_read(&sk->sk_wmem_alloc) > skb->truesize;
}

When skb == tcp_write_queue_head(sk) corking is done. This is part of the optimization for mlx5 driver I've mentioned. If we can better utilize auto corking we shouldn't have an issue.

Quoted 5 lines

netperf is not adding delays between each send(), unless it has been modified.

I ran this command:
./super_netperf 2000 -H <IP> -l 30 -f g -- -m $size didn't change netperf in any way.

#18 lore raw
Quoted 30 lines · Tal Gilboa
Quoted 29 lines · Eric Dumazet
Quoted 14 lines · David Laight

From: Eric Dumazet

Quoted 2 lines

Sent: 06 February 2018 14:20

...

Quoted 5 lines

Please give exact details.
Sending 64, 128, 256 or 512 bytes at a time on TCP_STREAM makes little sense.
We are not optimizing stack for pathological cases, sorry.

Quoted 6 lines

There are plenty of workloads which are not bulk data and where multiple small buffers get sent at unknown intervals (which may be back to back). Such connections have to have Nagle disabled because the Nagle delays are 'horrid'.
Clearly lost packets can cause delays, but they are rare on local networks.

Auto corking makes sure aggregation happens, even for when Nagle is in the picture.

Quoted 6 lines
Quoted 1 line

netperf -- -m 256 will still cook 64KB TSO packets

This is what we would have liked to see, but auto corking isn't forcing 64KB TSO packets. Under certain conditions, specifically when TX queue is empty, it would send the SKB to transmit even if it isn't full:

Yes.

Auto corking does not predict the future, nor arm a high resolution timer when application
does a send(small_size)

This packet is sent immaediately, as instructed by application and TCP normal behavior.

But second or third packet would detect the condition.

Unless a driver does skb_orphan() too early, breaking back pressure.

Quoted 11 lines

static bool tcp_should_autocork(struct sock *sk, struct sk_buff *skb,
int size_goal)

{
        return skb->len < size_goal &&
               sock_net(sk)->ipv4.sysctl_tcp_autocorking &&
               skb != tcp_write_queue_head(sk) &&
               refcount_read(&sk->sk_wmem_alloc) > skb->truesize;
}

When skb == tcp_write_queue_head(sk) corking is done. This is part of the optimization for mlx5 driver I've mentioned. If we can better utilize auto corking we shouldn't have an issue.

Or not issue expensive system calls for small payloads. stdio was invented a while back :)

Meltdown/Spectre mitigation put high price to system calls nowadays.