aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEric Dumazet <eric.dumazet@gmail.com>2010-04-27 18:13:20 -0400
committerDavid S. Miller <davem@davemloft.net>2010-04-27 18:13:20 -0400
commitc377411f2494a931ff7facdbb3a6839b1266bcf6 (patch)
tree6846cdcec913f50839e3916856f78f7e059ff5fb
parent6e7676c1a76aed6e957611d8d7a9e5592e23aeba (diff)
net: sk_add_backlog() take rmem_alloc into account
Current socket backlog limit is not enough to really stop DDOS attacks, because user thread spend many time to process a full backlog each round, and user might crazy spin on socket lock. We should add backlog size and receive_queue size (aka rmem_alloc) to pace writers, and let user run without being slow down too much. Introduce a sk_rcvqueues_full() helper, to avoid taking socket lock in stress situations. Under huge stress from a multiqueue/RPS enabled NIC, a single flow udp receiver can now process ~200.000 pps (instead of ~100 pps before the patch) on a 8 core machine. Signed-off-by: Eric Dumazet <eric.dumazet@gmail.com> Signed-off-by: David S. Miller <davem@davemloft.net>
-rw-r--r--include/net/sock.h13
-rw-r--r--net/core/sock.c5
-rw-r--r--net/ipv4/udp.c4
-rw-r--r--net/ipv6/udp.c8
-rw-r--r--net/sctp/socket.c3
5 files changed, 27 insertions, 6 deletions
diff --git a/include/net/sock.h b/include/net/sock.h
index 07822280d953..cf12b1e61fa6 100644
--- a/include/net/sock.h
+++ b/include/net/sock.h
@@ -256,7 +256,6 @@ struct sock {
256 struct sk_buff *head; 256 struct sk_buff *head;
257 struct sk_buff *tail; 257 struct sk_buff *tail;
258 int len; 258 int len;
259 int limit;
260 } sk_backlog; 259 } sk_backlog;
261 wait_queue_head_t *sk_sleep; 260 wait_queue_head_t *sk_sleep;
262 struct dst_entry *sk_dst_cache; 261 struct dst_entry *sk_dst_cache;
@@ -608,10 +607,20 @@ static inline void __sk_add_backlog(struct sock *sk, struct sk_buff *skb)
608 skb->next = NULL; 607 skb->next = NULL;
609} 608}
610 609
610/*
611 * Take into account size of receive queue and backlog queue
612 */
613static inline bool sk_rcvqueues_full(const struct sock *sk, const struct sk_buff *skb)
614{
615 unsigned int qsize = sk->sk_backlog.len + atomic_read(&sk->sk_rmem_alloc);
616
617 return qsize + skb->truesize > sk->sk_rcvbuf;
618}
619
611/* The per-socket spinlock must be held here. */ 620/* The per-socket spinlock must be held here. */
612static inline __must_check int sk_add_backlog(struct sock *sk, struct sk_buff *skb) 621static inline __must_check int sk_add_backlog(struct sock *sk, struct sk_buff *skb)
613{ 622{
614 if (sk->sk_backlog.len >= max(sk->sk_backlog.limit, sk->sk_rcvbuf << 1)) 623 if (sk_rcvqueues_full(sk, skb))
615 return -ENOBUFS; 624 return -ENOBUFS;
616 625
617 __sk_add_backlog(sk, skb); 626 __sk_add_backlog(sk, skb);
diff --git a/net/core/sock.c b/net/core/sock.c
index 58ebd146ce5a..51041759517e 100644
--- a/net/core/sock.c
+++ b/net/core/sock.c
@@ -327,6 +327,10 @@ int sk_receive_skb(struct sock *sk, struct sk_buff *skb, const int nested)
327 327
328 skb->dev = NULL; 328 skb->dev = NULL;
329 329
330 if (sk_rcvqueues_full(sk, skb)) {
331 atomic_inc(&sk->sk_drops);
332 goto discard_and_relse;
333 }
330 if (nested) 334 if (nested)
331 bh_lock_sock_nested(sk); 335 bh_lock_sock_nested(sk);
332 else 336 else
@@ -1885,7 +1889,6 @@ void sock_init_data(struct socket *sock, struct sock *sk)
1885 sk->sk_allocation = GFP_KERNEL; 1889 sk->sk_allocation = GFP_KERNEL;
1886 sk->sk_rcvbuf = sysctl_rmem_default; 1890 sk->sk_rcvbuf = sysctl_rmem_default;
1887 sk->sk_sndbuf = sysctl_wmem_default; 1891 sk->sk_sndbuf = sysctl_wmem_default;
1888 sk->sk_backlog.limit = sk->sk_rcvbuf << 1;
1889 sk->sk_state = TCP_CLOSE; 1892 sk->sk_state = TCP_CLOSE;
1890 sk_set_socket(sk, sock); 1893 sk_set_socket(sk, sock);
1891 1894
diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c
index fa3d2874db41..63eb56b2d873 100644
--- a/net/ipv4/udp.c
+++ b/net/ipv4/udp.c
@@ -1372,6 +1372,10 @@ int udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
1372 goto drop; 1372 goto drop;
1373 } 1373 }
1374 1374
1375
1376 if (sk_rcvqueues_full(sk, skb))
1377 goto drop;
1378
1375 rc = 0; 1379 rc = 0;
1376 1380
1377 bh_lock_sock(sk); 1381 bh_lock_sock(sk);
diff --git a/net/ipv6/udp.c b/net/ipv6/udp.c
index 2850e35cee3d..3ead20ad9d07 100644
--- a/net/ipv6/udp.c
+++ b/net/ipv6/udp.c
@@ -584,6 +584,10 @@ static void flush_stack(struct sock **stack, unsigned int count,
584 584
585 sk = stack[i]; 585 sk = stack[i];
586 if (skb1) { 586 if (skb1) {
587 if (sk_rcvqueues_full(sk, skb)) {
588 kfree_skb(skb1);
589 goto drop;
590 }
587 bh_lock_sock(sk); 591 bh_lock_sock(sk);
588 if (!sock_owned_by_user(sk)) 592 if (!sock_owned_by_user(sk))
589 udpv6_queue_rcv_skb(sk, skb1); 593 udpv6_queue_rcv_skb(sk, skb1);
@@ -759,6 +763,10 @@ int __udp6_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
759 763
760 /* deliver */ 764 /* deliver */
761 765
766 if (sk_rcvqueues_full(sk, skb)) {
767 sock_put(sk);
768 goto discard;
769 }
762 bh_lock_sock(sk); 770 bh_lock_sock(sk);
763 if (!sock_owned_by_user(sk)) 771 if (!sock_owned_by_user(sk))
764 udpv6_queue_rcv_skb(sk, skb); 772 udpv6_queue_rcv_skb(sk, skb);
diff --git a/net/sctp/socket.c b/net/sctp/socket.c
index f34adcca8a8c..13d8229f3a9c 100644
--- a/net/sctp/socket.c
+++ b/net/sctp/socket.c
@@ -3721,9 +3721,6 @@ SCTP_STATIC int sctp_init_sock(struct sock *sk)
3721 SCTP_DBG_OBJCNT_INC(sock); 3721 SCTP_DBG_OBJCNT_INC(sock);
3722 percpu_counter_inc(&sctp_sockets_allocated); 3722 percpu_counter_inc(&sctp_sockets_allocated);
3723 3723
3724 /* Set socket backlog limit. */
3725 sk->sk_backlog.limit = sysctl_sctp_rmem[1];
3726
3727 local_bh_disable(); 3724 local_bh_disable();
3728 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); 3725 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
3729 local_bh_enable(); 3726 local_bh_enable();