Commit 72cd43ba authored by Eric Dumazet's avatar Eric Dumazet Committed by David S. Miller

tcp: free batches of packets in tcp_prune_ofo_queue()

Juha-Matti Tilli reported that malicious peers could inject tiny
packets in out_of_order_queue, forcing very expensive calls
to tcp_collapse_ofo_queue() and tcp_prune_ofo_queue() for
every incoming packet. out_of_order_queue rb-tree can contain
thousands of nodes, iterating over all of them is not nice.

Before linux-4.9, we would have pruned all packets in ofo_queue
in one go, every XXXX packets. XXXX depends on sk_rcvbuf and skbs
truesize, but is about 7000 packets with tcp_rmem[2] default of 6 MB.

Since we plan to increase tcp_rmem[2] in the future to cope with
modern BDP, can not revert to the old behavior, without great pain.

Strategy taken in this patch is to purge ~12.5 % of the queue capacity.

Fixes: 36a6503f ("tcp: refine tcp_prune_ofo_queue() to not drop all packets")
Signed-off-by: default avatarEric Dumazet <edumazet@google.com>
Reported-by: default avatarJuha-Matti Tilli <juha-matti.tilli@iki.fi>
Acked-by: default avatarYuchung Cheng <ycheng@google.com>
Acked-by: default avatarSoheil Hassas Yeganeh <soheil@google.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 3dd1c9a1
...@@ -4942,6 +4942,7 @@ static void tcp_collapse_ofo_queue(struct sock *sk) ...@@ -4942,6 +4942,7 @@ static void tcp_collapse_ofo_queue(struct sock *sk)
* 2) not add too big latencies if thousands of packets sit there. * 2) not add too big latencies if thousands of packets sit there.
* (But if application shrinks SO_RCVBUF, we could still end up * (But if application shrinks SO_RCVBUF, we could still end up
* freeing whole queue here) * freeing whole queue here)
* 3) Drop at least 12.5 % of sk_rcvbuf to avoid malicious attacks.
* *
* Return true if queue has shrunk. * Return true if queue has shrunk.
*/ */
...@@ -4949,20 +4950,26 @@ static bool tcp_prune_ofo_queue(struct sock *sk) ...@@ -4949,20 +4950,26 @@ static bool tcp_prune_ofo_queue(struct sock *sk)
{ {
struct tcp_sock *tp = tcp_sk(sk); struct tcp_sock *tp = tcp_sk(sk);
struct rb_node *node, *prev; struct rb_node *node, *prev;
int goal;
if (RB_EMPTY_ROOT(&tp->out_of_order_queue)) if (RB_EMPTY_ROOT(&tp->out_of_order_queue))
return false; return false;
NET_INC_STATS(sock_net(sk), LINUX_MIB_OFOPRUNED); NET_INC_STATS(sock_net(sk), LINUX_MIB_OFOPRUNED);
goal = sk->sk_rcvbuf >> 3;
node = &tp->ooo_last_skb->rbnode; node = &tp->ooo_last_skb->rbnode;
do { do {
prev = rb_prev(node); prev = rb_prev(node);
rb_erase(node, &tp->out_of_order_queue); rb_erase(node, &tp->out_of_order_queue);
goal -= rb_to_skb(node)->truesize;
tcp_drop(sk, rb_to_skb(node)); tcp_drop(sk, rb_to_skb(node));
sk_mem_reclaim(sk); if (!prev || goal <= 0) {
if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf && sk_mem_reclaim(sk);
!tcp_under_memory_pressure(sk)) if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
break; !tcp_under_memory_pressure(sk))
break;
goal = sk->sk_rcvbuf >> 3;
}
node = prev; node = prev;
} while (node); } while (node);
tp->ooo_last_skb = rb_to_skb(prev); tp->ooo_last_skb = rb_to_skb(prev);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment