tcp/dccp: lockless listener
Subsystems (5)
cover tcp/dccp: lockless listener
TCP listener refactoring : this is becoming interesting !
This patch series takes the steps to use normal TCP/DCCP ehash table to store SYN_RECV requests, instead of the private per-listener hash table we had until now.
SYNACK skb are now attached to their syn_recv request socket, so that we no longer heavily modify listener sk_wmem_alloc.
listener lock is no longer held in fast path, including SYNCOOKIE mode.
During my tests, my server was able to process 3,500,000 SYN packets per second on one listener and still had available cpu cycles.
That is about 2 to 3 order of magnitude what we had with older kernels.
This effort started two years ago and I am pleased to reach expectations.
We'll probably extend SO_REUSEPORT to add proper cpu/numa affinities, so that heavy duty TCP servers can get proper siloing thanks to multi-queues NIC.
Eric Dumazet (17):
tcp: add a spinlock to protect struct request_sock_queue
tcp: move qlen/young out of struct listen_sock
tcp: move synflood_warned into struct request_sock_queue
tcp: call sk_mark_napi_id() on the child, not the listener tcp/dccp: init sk_prot and call sk_node_init() in reqsk_alloc()
tcp: cleanup tcp_v[46]_inbound_md5_hash()
tcp: remove BUG_ON() in tcp_check_req()
tcp: get_openreq[46]() changes
tcp/dccp: remove inet_csk_reqsk_queue_added() timeout argument tcp/dccp: install syn_recv requests into ehash table tcp/dccp: shrink struct listen_sock
ipv6: remove obsolete inet6 functions
tcp: attach SYNACK messages to request sockets instead of listener tcp/dccp: remove struct listen_sock
tcp: remove max_qlen_log
tcp/dccp: add a reschedule point in inet_csk_listen_stop()
tcp: do not lock listener to process SYN packets
include/net/inet6_connection_sock.h | 9 -- include/net/inet_connection_sock.h | 9 +- include/net/inet_hashtables.h | 1 + include/net/request_sock.h | 132 ++++++++++-------------------- include/net/tcp.h | 10 +-- net/core/request_sock.c | 84 +------------------ net/dccp/ipv4.c | 64 +++++---------- net/dccp/ipv6.c | 72 +++++----------- net/ipv4/inet_connection_sock.c | 145 ++++++++------------------------ net/ipv4/inet_diag.c | 96 ++-------------------- net/ipv4/inet_hashtables.c | 14 +++- net/ipv4/syncookies.c | 4 + net/ipv4/tcp_fastopen.c | 4 +- net/ipv4/tcp_input.c | 30 +++---- net/ipv4/tcp_ipv4.c | 159 +++++++++++++----------------------- net/ipv4/tcp_minisocks.c | 2 - net/ipv4/tcp_output.c | 22 +++-- net/ipv6/inet6_connection_sock.c | 67 --------------- net/ipv6/tcp_ipv6.c | 117 +++++++++++++------------- net/sched/sch_fq.c | 12 +-- 20 files changed, 307 insertions(+), 746 deletions(-)
Signature · 1 line
2.6.0.rc2.230.g3dd15c0
PATCH 1/17 tcp: add a spinlock to protect struct request_sock_queue
struct request_sock_queue fields are currently protected by the listener 'lock' (not a real spinlock)
We need to add a private spinlock instead, so that softirq handlers creating children do not have to worry with backlog notion that the listener 'lock' carries.
--- include/net/request_sock.h | 37 ++++++++++++++++++------------------- net/core/request_sock.c | 1 + net/ipv4/inet_connection_sock.c | 21 +++++++-------------- 3 files changed, 26 insertions(+), 33 deletions(-)
include/net/request_sock.h +18 -19
@@ -176,9 +176,11 @@ struct fastopen_queue {* */ struct request_sock_queue { +spinlock_t rskq_lock; +u8 rskq_defer_accept; +struct request_sock *rskq_accept_head;struct request_sock *rskq_accept_tail; -u8 rskq_defer_accept;struct listen_sock *listen_opt;struct fastopen_queue fastopenq;/* Check max_qlen != 0 to determine * if TFO is enabled.
@@ -196,16 +198,7 @@ void reqsk_queue_destroy(struct request_sock_queue *queue);void reqsk_fastopen_remove (struct sock *sk,struct request_sock *req,bool reset); -static inline struct request_sock * -reqsk_queue_yank_acceptq (struct request_sock_queue *queue) -{ -struct request_sock *req = queue->rskq_accept_head; - - queue->rskq_accept_head =NULL ; -return req; -} - -static inline int reqsk_queue_empty (struct request_sock_queue *queue) +static inline bool reqsk_queue_empty (const struct request_sock_queue *queue) {return queue->rskq_accept_head ==NULL ; }
@@ -215,6 +208,7 @@ static inline void reqsk_queue_add(struct request_sock_queue *queue,struct sock *parent, struct sock *child) { + spin_lock(&queue->rskq_lock); req->sk = child;sk_acceptq_added (parent);
@@ -225,18 +219,23 @@ static inline void reqsk_queue_add(struct request_sock_queue *queue, queue->rskq_accept_tail = req; req->dl_next =NULL ; +spin_unlock (&queue->rskq_lock); } -static inline struct request_sock *reqsk_queue_remove (struct request_sock_queue *queue) +static inline struct request_sock *reqsk_queue_remove (struct request_sock_queue *queue, +struct sock *parent) { -struct request_sock *req = queue->rskq_accept_head; - -WARN_ON (req ==NULL ); - - queue->rskq_accept_head = req->dl_next; -if (queue->rskq_accept_head ==NULL ) - queue->rskq_accept_tail =NULL ; +struct request_sock *req; +spin_lock_bh (&queue->rskq_lock); + req = queue->rskq_accept_head; +if (req) { +sk_acceptq_removed (parent); + queue->rskq_accept_head = req->dl_next; +if (queue->rskq_accept_head ==NULL ) + queue->rskq_accept_tail =NULL ; + } +spin_unlock_bh (&queue->rskq_lock);return req; }
net/core/request_sock.c +1 -0
@@ -58,6 +58,7 @@ int reqsk_queue_alloc(struct request_sock_queue *queue,return -ENOMEM ;get_random_bytes (&lopt->hash_rnd,sizeof (lopt->hash_rnd)); +spin_lock_init (&queue->rskq_lock);spin_lock_init (&queue->syn_wait_lock);spin_lock_init (&queue->fastopenq.lock);
net/ipv4/inet_connection_sock.c +7 -14
@@ -330,10 +330,9 @@ struct sock *inet_csk_accept(struct sock *sk, int flags, int *err)if (error) goto out_err; } - req =reqsk_queue_remove (queue); + req =reqsk_queue_remove (queue, sk); newsk = req->sk; -sk_acceptq_removed (sk);if (sk->sk_protocol ==IPPROTO_TCP &&tcp_rsk (req)->tfo_listener) {spin_lock_bh (&queue->fastopenq.lock);
@@ -832,11 +831,7 @@ void inet_csk_listen_stop(struct sock *sk) {struct inet_connection_sock *icsk =inet_csk (sk);struct request_sock_queue *queue = &icsk->icsk_accept_queue; -struct request_sock *acc_req; -struct request_sock *req; - -/* make all the listen_opt local to us */ - acc_req =reqsk_queue_yank_acceptq (queue); +struct request_sock *next, *req;/* Following specs, it would be better either to send FIN * (and enter FIN-WAIT-1, it is normal close)
@@ -848,11 +843,9 @@ void inet_csk_listen_stop(struct sock *sk)*/ reqsk_queue_destroy (queue); -while ((req = acc_req) !=NULL ) { +while ((req =reqsk_queue_remove (queue, sk)) !=NULL ) {struct sock *child = req->sk; - acc_req = req->dl_next; -local_bh_disable ();bh_lock_sock (child);WARN_ON (sock_owned_by_user (child));
@@ -882,18 +875,18 @@ void inet_csk_listen_stop(struct sock *sk)local_bh_enable ();sock_put (child); -sk_acceptq_removed (sk);reqsk_put (req); }if (queue->fastopenq.rskq_rst_head) {/* Free all the reqs queued in rskq_rst_head. */ spin_lock_bh (&queue->fastopenq.lock); - acc_req = queue->fastopenq.rskq_rst_head; + req = queue->fastopenq.rskq_rst_head; queue->fastopenq.rskq_rst_head =NULL ;spin_unlock_bh (&queue->fastopenq.lock); -while ((req = acc_req) !=NULL ) { - acc_req = req->dl_next; +while (req !=NULL ) { + next = req->dl_next;reqsk_put (req); + req = next; } }WARN_ON (sk->sk_ack_backlog);
Signature · 1 line
2.6.0.rc2.230.g3dd15c0
PATCH 2/17 tcp: move qlen/young out of struct listen_sock
In reply to Eric Dumazetqlen_inc & young_inc were protected by listener lock, while qlen_dec & young_dec were atomic fields.
Everything needs to be atomic for upcoming lockless listener.
Also move qlen/young in request_sock_queue as we'll get rid of struct listen_sock eventually.
--- include/net/request_sock.h | 40 ++++++++++------------------------------ net/core/request_sock.c | 8 ++++---- net/ipv4/inet_connection_sock.c | 6 +++--- net/ipv4/inet_diag.c | 2 +- 4 files changed, 18 insertions(+), 38 deletions(-)
include/net/request_sock.h +10 -30
@@ -122,14 +122,7 @@ extern int sysctl_max_syn_backlog;* @max_qlen_log - log_2 of maximal queued SYNs/REQUESTs */ struct listen_sock { -int qlen_inc;/* protected by listener lock */ -int young_inc;/* protected by listener lock */ - -/* following fields can be updated by timer */ -atomic_t qlen_dec;/* qlen = qlen_inc - qlen_dec */ -atomic_t young_dec; - -u32 max_qlen_log____cacheline_aligned_in_smp ; +u32 max_qlen_log;u32 synflood_warned;u32 hash_rnd;u32 nr_table_entries;
@@ -179,6 +172,9 @@ struct request_sock_queue {spinlock_t rskq_lock;u8 rskq_defer_accept; +atomic_t qlen; +atomic_t young; +struct request_sock *rskq_accept_head;struct request_sock *rskq_accept_tail;struct listen_sock *listen_opt;
@@ -242,41 +238,25 @@ static inline struct request_sock *reqsk_queue_remove(struct request_sock_queuestatic inline void reqsk_queue_removed (struct request_sock_queue *queue,const struct request_sock *req) { -struct listen_sock *lopt = queue->listen_opt; -if (req->num_timeout ==0 ) -atomic_inc (&lopt->young_dec); -atomic_inc (&lopt->qlen_dec); +atomic_dec (&queue->young); +atomic_dec (&queue->qlen); }static inline void reqsk_queue_added (struct request_sock_queue *queue) { -struct listen_sock *lopt = queue->listen_opt; - - lopt->young_inc++; - lopt->qlen_inc++; -} - -static inline int listen_sock_qlen (const struct listen_sock *lopt) -{ -return lopt->qlen_inc -atomic_read (&lopt->qlen_dec); -} - -static inline int listen_sock_young (const struct listen_sock *lopt) -{ -return lopt->young_inc -atomic_read (&lopt->young_dec); +atomic_inc (&queue->young); +atomic_inc (&queue->qlen); }static inline int reqsk_queue_len (const struct request_sock_queue *queue) { -const struct listen_sock *lopt = queue->listen_opt; - -return lopt ?listen_sock_qlen (lopt) :0 ; +return atomic_read (&queue->qlen); }static inline int reqsk_queue_len_young (const struct request_sock_queue *queue) { -return listen_sock_young (queue->listen_opt); +return atomic_read (&queue->young); }static inline int reqsk_queue_is_full (const struct request_sock_queue *queue)
net/core/request_sock.c +4 -4
@@ -102,7 +102,7 @@ void reqsk_queue_destroy(struct request_sock_queue *queue)/* make all the listen_opt local to us */ struct listen_sock *lopt =reqsk_queue_yank_listen_sk (queue); -if (listen_sock_qlen (lopt) !=0 ) { +if (reqsk_queue_len (queue) !=0 ) {unsigned int i;for (i =0 ; i < lopt->nr_table_entries; i++) {
@@ -116,7 +116,7 @@ void reqsk_queue_destroy(struct request_sock_queue *queue)* or risk a dead lock. */ spin_unlock_bh (&queue->syn_wait_lock); -atomic_inc (&lopt->qlen_dec); +atomic_dec (&queue->qlen);if (del_timer_sync (&req->rsk_timer))reqsk_put (req);reqsk_put (req);
@@ -126,8 +126,8 @@ void reqsk_queue_destroy(struct request_sock_queue *queue) } } -if (WARN_ON (listen_sock_qlen (lopt) !=0 )) -pr_err ("qlen %u\n" ,listen_sock_qlen (lopt)); +if (WARN_ON (reqsk_queue_len (queue) !=0 )) +pr_err ("qlen %u\n" ,reqsk_queue_len (queue));kvfree (lopt); }
net/ipv4/inet_connection_sock.c +3 -3
@@ -640,9 +640,9 @@ static void reqsk_timer_handler(unsigned long data)* embrions; and abort old ones without pity, if old * ones are about to clog our table. */ - qlen =listen_sock_qlen (lopt); + qlen =reqsk_queue_len (queue);if (qlen >> (lopt->max_qlen_log -1 )) { -int young =listen_sock_young (lopt) <<1 ; +int young =reqsk_queue_len_young (queue) <<1 ;while (thresh >2 ) {if (qlen < young)
@@ -664,7 +664,7 @@ static void reqsk_timer_handler(unsigned long data)unsigned long timeo;if (req->num_timeout++ ==0 ) -atomic_inc (&lopt->young_dec); +atomic_dec (&queue->young); timeo =min (TCP_TIMEOUT_INIT << req->num_timeout,TCP_RTO_MAX );mod_timer_pinned (&req->rsk_timer, jiffies + timeo);return ;
net/ipv4/inet_diag.c +1 -1
@@ -753,7 +753,7 @@ static int inet_diag_dump_reqs(struct sk_buff *skb, struct sock *sk,spin_lock (&icsk->icsk_accept_queue.syn_wait_lock); lopt = icsk->icsk_accept_queue.listen_opt; -if (!lopt || !listen_sock_qlen (lopt)) +if (!lopt || !reqsk_queue_len (&icsk->icsk_accept_queue)) goto out;if (bc) {
Signature · 1 line
2.6.0.rc2.230.g3dd15c0
PATCH 3/17 tcp: move synflood_warned into struct request_sock_queue
In reply to Eric Dumazetlong term plan is to remove struct listen_sock when its hash table is no longer there.
--- include/net/request_sock.h | 2 +- net/ipv4/tcp_input.c | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-)
include/net/request_sock.h +1 -1
@@ -123,7 +123,6 @@ extern int sysctl_max_syn_backlog;*/ struct listen_sock {u32 max_qlen_log; -u32 synflood_warned;u32 hash_rnd;u32 nr_table_entries;struct request_sock *syn_table[0 ];
@@ -171,6 +170,7 @@ struct fastopen_queue {struct request_sock_queue {spinlock_t rskq_lock;u8 rskq_defer_accept; +u32 synflood_warned;atomic_t qlen;atomic_t young;
net/ipv4/tcp_input.c +3 -4
@@ -6068,9 +6068,9 @@ static bool tcp_syn_flood_action(const struct sock *sk,const struct sk_buff *skb, const char *proto) { + struct request_sock_queue *queue = &inet_csk(sk)->icsk_accept_queue;const char *msg ="Dropping request" ;bool want_cookie = false; -struct listen_sock *lopt; #ifdef CONFIG_SYN_COOKIES if (sysctl_tcp_syncookies) {
@@ -6081,10 +6081,9 @@ static bool tcp_syn_flood_action(const struct sock *sk,#endif NET_INC_STATS_BH (sock_net (sk),LINUX_MIB_TCPREQQFULLDROP ); - lopt =inet_csk (sk)->icsk_accept_queue.listen_opt; -if (!lopt->synflood_warned && +if (!queue->synflood_warned && sysctl_tcp_syncookies !=2 && -xchg (&lopt->synflood_warned,1 ) ==0 ) +xchg (&queue->synflood_warned,1 ) ==0 )pr_info ("%s: Possible SYN flooding on port %d. %s. Check SNMP counters.\n" , proto,ntohs (tcp_hdr (skb)->dest), msg);
Signature · 1 line
2.6.0.rc2.230.g3dd15c0
PATCH 4/17 tcp: call sk_mark_napi_id() on the child, not the listener
In reply to Eric DumazetThis fixes a typo : We want to store the NAPI id on child socket. Presumably nobody really uses busy polling, on short lived flows.
--- net/ipv4/tcp_ipv4.c | 2 +- net/ipv6/tcp_ipv6.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
net/ipv4/tcp_ipv4.c +1 -1
@@ -1411,7 +1411,7 @@ int tcp_v4_do_rcv(struct sock *sk, struct sk_buff *skb)if (nsk != sk) {sock_rps_save_rxhash (nsk, skb); -sk_mark_napi_id (sk, skb); +sk_mark_napi_id (nsk, skb);if (tcp_child_process (sk, nsk, skb)) { rsk = nsk; goto reset;
net/ipv6/tcp_ipv6.c +1 -1
@@ -1265,7 +1265,7 @@ static int tcp_v6_do_rcv(struct sock *sk, struct sk_buff *skb)*/ if (nsk != sk) {sock_rps_save_rxhash (nsk, skb); -sk_mark_napi_id (sk, skb); +sk_mark_napi_id (nsk, skb);if (tcp_child_process (sk, nsk, skb)) goto reset;if (opt_skb)
Signature · 1 line
2.6.0.rc2.230.g3dd15c0
PATCH 5/17 tcp/dccp: init sk_prot and call sk_node_init() in reqsk_alloc()
In reply to Eric DumazetWe plan to use generic functions to insert request sockets into ehash table.
sk_prot needs to be set (to retrieve sk_prot->h.hashinfo) sk_node needs to be cleared.
--- include/net/request_sock.h | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-)
include/net/request_sock.h +12 -10
@@ -69,6 +69,16 @@ struct request_sock {u32 peer_secid; }; +static inline struct request_sock *inet_reqsk (struct sock *sk) +{ +return (struct request_sock *)sk; +} + +static inline struct sock *req_to_sk (struct request_sock *req) +{ +return (struct sock *)req; +} +static inline struct request_sock *reqsk_alloc (const struct request_sock_ops *ops,struct sock *sk_listener) {
@@ -78,6 +88,8 @@ reqsk_alloc(const struct request_sock_ops *ops, struct sock *sk_listener) req->rsk_ops = ops;sock_hold (sk_listener); req->rsk_listener = sk_listener; +req_to_sk (req)->sk_prot = sk_listener->sk_prot; +sk_node_init (&req_to_sk (req)->sk_node); req->saved_syn =NULL ;/* Following is temporary. It is coupled with debugging * helpers in reqsk_put() & reqsk_free()
@@ -87,16 +99,6 @@ reqsk_alloc(const struct request_sock_ops *ops, struct sock *sk_listener)return req; } -static inline struct request_sock *inet_reqsk (struct sock *sk) -{ -return (struct request_sock *)sk; -} - -static inline struct sock *req_to_sk (struct request_sock *req) -{ -return (struct sock *)req; -} -static inline void reqsk_free (struct request_sock *req) {/* temporary debugging */
Signature · 1 line
2.6.0.rc2.230.g3dd15c0
PATCH 6/17 tcp: cleanup tcp_v[46]_inbound_md5_hash()
In reply to Eric DumazetWe'll soon have to call tcp_v[46]_inbound_md5_hash() twice. Also add const attribute to the socket, as it might be the unlocked listener for SYN packets.
--- net/ipv4/tcp_ipv4.c | 16 ++++++---------- net/ipv6/tcp_ipv6.c | 10 ++++++---- 2 files changed, 12 insertions(+), 14 deletions(-)
net/ipv4/tcp_ipv4.c +6 -10
@@ -1112,10 +1112,13 @@ clear_hash_noput: }EXPORT_SYMBOL (tcp_v4_md5_hash_skb); +#endif +/* Called with rcu_read_lock() */ -static bool tcp_v4_inbound_md5_hash (struct sock *sk, +static bool tcp_v4_inbound_md5_hash (const struct sock *sk,const struct sk_buff *skb) { +#ifdef CONFIG_TCP_MD5SIG /* * This gets called for each TCP segment that arrives * so we want to be efficient.
@@ -1165,8 +1168,9 @@ static bool tcp_v4_inbound_md5_hash(struct sock *sk,return true; }return false; -}#endif +return false; +} static void tcp_v4_init_req(struct request_sock *req, const struct sock *sk_listener,
@@ -1607,16 +1611,8 @@ process:if (!xfrm4_policy_check (sk,XFRM_POLICY_IN , skb)) goto discard_and_relse; -#ifdef CONFIG_TCP_MD5SIG -/* -* We really want to reject the packet as early as possible -* if: -* o We're expecting an MD5'd packet and this is no MD5 tcp option -* o There is an MD5 option and we're not expecting one -*/ if (tcp_v4_inbound_md5_hash (sk, skb)) goto discard_and_relse; -#endif nf_reset (skb);
net/ipv6/tcp_ipv6.c +6 -4
@@ -622,8 +622,12 @@ clear_hash_noput: return 1; } -static bool tcp_v6_inbound_md5_hash (struct sock *sk,const struct sk_buff *skb) +#endif + +static bool tcp_v6_inbound_md5_hash (const struct sock *sk, +const struct sk_buff *skb) { +#ifdef CONFIG_TCP_MD5SIG const __u8 *hash_location =NULL ;struct tcp_md5sig_key *hash_expected;const struct ipv6hdr *ip6h =ipv6_hdr (skb);
@@ -660,9 +664,9 @@ static bool tcp_v6_inbound_md5_hash(struct sock *sk, const struct sk_buff *skb) &ip6h->daddr, ntohs(th->dest));return true; } +#endif return false; } -#endif static void tcp_v6_init_req(struct request_sock *req, const struct sock *sk_listener,
@@ -1408,10 +1412,8 @@ process:tcp_v6_fill_cb (skb, hdr, th); -#ifdef CONFIG_TCP_MD5SIG if (tcp_v6_inbound_md5_hash (sk, skb)) goto discard_and_relse; -#endif if (sk_filter (sk, skb)) goto discard_and_relse;
Signature · 1 line
2.6.0.rc2.230.g3dd15c0
PATCH 7/17 tcp: remove BUG_ON() in tcp_check_req()
In reply to Eric DumazetOnce listener is lockless, its sk_state can change anytime.
--- net/ipv4/tcp_minisocks.c | 2 -- 1 file changed, 2 deletions(-)
net/ipv4/tcp_minisocks.c +0 -2
@@ -578,8 +578,6 @@ struct sock *tcp_check_req(struct sock *sk, struct sk_buff *skb,__be32 flg =tcp_flag_word (th) & (TCP_FLAG_RST |TCP_FLAG_SYN |TCP_FLAG_ACK );bool paws_reject = false; -BUG_ON (fastopen == (sk->sk_state ==TCP_LISTEN )); - tmp_opt.saw_tstamp =0 ;if (th->doff > (sizeof (struct tcphdr )>>2 )) {tcp_parse_options (skb, &tmp_opt,0 ,NULL );
Signature · 1 line
2.6.0.rc2.230.g3dd15c0
PATCH 8/17 tcp: get_openreq[46]() changes
In reply to Eric DumazetWhen request sockets are no longer in a per listener hash table but on regular TCP ehash, we need to access listener uid through req->rsk_listener
get_openreq6() also gets a const for its request socket argument.
--- include/net/tcp.h | 1 - net/ipv4/tcp_ipv4.c | 8 ++++---- net/ipv6/tcp_ipv6.c | 7 ++++--- 3 files changed, 8 insertions(+), 8 deletions(-)
include/net/tcp.h +0 -1
@@ -1637,7 +1637,6 @@ struct tcp_iter_state {enum tcp_seq_states state;struct sock *syn_wait_sk;int bucket, offset, sbucket, num; -kuid_t uid;loff_t last_pos; };
net/ipv4/tcp_ipv4.c +4 -4
@@ -1871,7 +1871,6 @@ get_sk:spin_lock_bh (&icsk->icsk_accept_queue.syn_wait_lock);if (reqsk_queue_len (&icsk->icsk_accept_queue)) { start_req: - st->uid =sock_i_uid (sk); st->syn_wait_sk = sk; st->state =TCP_SEQ_STATE_OPENREQ ; st->sbucket =0 ;
@@ -2151,7 +2150,7 @@ void tcp_proc_unregister(struct net *net, struct tcp_seq_afinfo *afinfo)EXPORT_SYMBOL (tcp_proc_unregister);static void get_openreq4 (const struct request_sock *req, -struct seq_file *f,int i,kuid_t uid) +struct seq_file *f,int i) {const struct inet_request_sock *ireq =inet_rsk (req);long delta = req->rsk_timer.expires - jiffies;
@@ -2168,7 +2167,8 @@ static void get_openreq4(const struct request_sock *req,1 ,/* timers active (only the expire timer) */ jiffies_delta_to_clock_t (delta), req->num_timeout, -from_kuid_munged (seq_user_ns (f), uid), +from_kuid_munged (seq_user_ns (f), +sock_i_uid (req->rsk_listener)),0 ,/* non standard timer */ 0 ,/* open_requests have no inode */ 0,
@@ -2278,7 +2278,7 @@ static int tcp4_seq_show(struct seq_file *seq, void *v)get_tcp4_sock (v, seq, st->num);break ;case TCP_SEQ_STATE_OPENREQ : -get_openreq4 (v, seq, st->num, st->uid); +get_openreq4 (v, seq, st->num);break ; } out:
net/ipv6/tcp_ipv6.c +4 -3
@@ -1635,7 +1635,7 @@ static void tcp_v6_destroy_sock(struct sock *sk)#ifdef CONFIG_PROC_FS /* Proc filesystem TCPv6 sock list dumping. */ static void get_openreq6 (struct seq_file *seq, -struct request_sock *req,int i,kuid_t uid) +const struct request_sock *req,int i) {long ttd = req->rsk_timer.expires - jiffies;const struct in6_addr *src = &inet_rsk (req)->ir_v6_loc_addr;
@@ -1659,7 +1659,8 @@ static void get_openreq6(struct seq_file *seq,1 ,/* timers active (only the expire timer) */ jiffies_to_clock_t (ttd), req->num_timeout, -from_kuid_munged (seq_user_ns (seq), uid), +from_kuid_munged (seq_user_ns (seq), +sock_i_uid (req->rsk_listener)),0 ,/* non standard timer */ 0 ,/* open_requests have no inode */ 0, req);
@@ -1773,7 +1774,7 @@ static int tcp6_seq_show(struct seq_file *seq, void *v)get_tcp6_sock (seq, v, st->num);break ;case TCP_SEQ_STATE_OPENREQ : -get_openreq6 (seq, v, st->num, st->uid); +get_openreq6 (seq, v, st->num);break ; } out:
Signature · 1 line
2.6.0.rc2.230.g3dd15c0
PATCH 9/17 tcp/dccp: remove inet_csk_reqsk_queue_added() timeout argument
In reply to Eric DumazetThis is no longer used.
--- include/net/inet_connection_sock.h | 3 +-- net/ipv4/inet_connection_sock.c | 2 +- net/ipv6/inet6_connection_sock.c | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-)
include/net/inet_connection_sock.h +1 -2
@@ -282,8 +282,7 @@ static inline void inet_csk_reqsk_queue_add(struct sock *sk,void inet_csk_reqsk_queue_hash_add (struct sock *sk,struct request_sock *req,unsigned long timeout); -static inline void inet_csk_reqsk_queue_added (struct sock *sk, -const unsigned long timeout) +static inline void inet_csk_reqsk_queue_added (struct sock *sk) {reqsk_queue_added (&inet_csk (sk)->icsk_accept_queue); }
net/ipv4/inet_connection_sock.c +1 -1
@@ -531,7 +531,7 @@ void inet_csk_reqsk_queue_hash_add(struct sock *sk, struct request_sock *req, lopt->hash_rnd, lopt->nr_table_entries);reqsk_queue_hash_req (&icsk->icsk_accept_queue, h, req, timeout); -inet_csk_reqsk_queue_added (sk, timeout); +inet_csk_reqsk_queue_added (sk); }EXPORT_SYMBOL_GPL (inet_csk_reqsk_queue_hash_add);
net/ipv6/inet6_connection_sock.c +1 -1
@@ -157,7 +157,7 @@ void inet6_csk_reqsk_queue_hash_add(struct sock *sk, lopt->hash_rnd, lopt->nr_table_entries);reqsk_queue_hash_req (&icsk->icsk_accept_queue, h, req, timeout); -inet_csk_reqsk_queue_added (sk, timeout); +inet_csk_reqsk_queue_added (sk); }EXPORT_SYMBOL_GPL (inet6_csk_reqsk_queue_hash_add);
Signature · 1 line
2.6.0.rc2.230.g3dd15c0
PATCH 10/17 tcp/dccp: install syn_recv requests into ehash table
In reply to Eric DumazetIn this patch, we insert request sockets into TCP/DCCP regular ehash table (where ESTABLISHED and TIMEWAIT sockets are) instead of using the per listener hash table.
ACK packets find SYN_RECV pseudo sockets without having to find and lock the listener.
In nominal conditions, this halves pressure on listener lock.
Note that this will allow for SO_REUSEPORT refinements, so that we can select a listener using cpu/numa affinities instead of the prior 'consistent hash', since only SYN packets will apply this selection logic.
We will shrink listen_sock in the following patch to ease code review.
--- include/net/inet_connection_sock.h | 4 -- include/net/inet_hashtables.h | 1 + include/net/request_sock.h | 4 -- include/net/tcp.h | 3 - net/core/request_sock.c | 28 +-------- net/dccp/ipv4.c | 64 +++++++------------- net/dccp/ipv6.c | 72 +++++++---------------- net/ipv4/inet_connection_sock.c | 103 +++++++------------------------- net/ipv4/inet_diag.c | 96 +++--------------------------- net/ipv4/inet_hashtables.c | 14 ++++- net/ipv4/syncookies.c | 4 ++ net/ipv4/tcp_input.c | 2 +- net/ipv4/tcp_ipv4.c | 117 +++++++++++-------------------------- net/ipv6/inet6_connection_sock.c | 67 --------------------- net/ipv6/tcp_ipv6.c | 82 ++++++++++++-------------- 15 files changed, 160 insertions(+), 501 deletions(-)
include/net/inet_connection_sock.h +0 -4
@@ -258,10 +258,6 @@ inet_csk_rto_backoff(const struct inet_connection_sock *icsk,struct sock *inet_csk_accept (struct sock *sk,int flags,int *err); -struct request_sock *inet_csk_search_req (struct sock *sk, -const __be16 rport, -const __be32 raddr, -const __be32 laddr);int inet_csk_bind_conflict (const struct sock *sk,const struct inet_bind_bucket *tb,bool relax);int inet_csk_get_port (struct sock *sk,unsigned short snum);
include/net/inet_hashtables.h +1 -0
@@ -205,6 +205,7 @@ void inet_put_port(struct sock *sk);void inet_hashinfo_init (struct inet_hashinfo *h); +int inet_ehash_insert (struct sock *sk,struct sock *osk);void __inet_hash_nolisten (struct sock *sk,struct sock *osk);void __inet_hash (struct sock *sk,struct sock *osk);void inet_hash (struct sock *sk);
include/net/request_sock.h +0 -4
@@ -266,8 +266,4 @@ static inline int reqsk_queue_is_full(const struct request_sock_queue *queue) return reqsk_queue_len(queue) >> queue->listen_opt->max_qlen_log; } -void reqsk_queue_hash_req (struct request_sock_queue *queue, -u32 hash,struct request_sock *req, -unsigned long timeout); -#endif /* _REQUEST_SOCK_H */
include/net/tcp.h +0 -3
@@ -1618,7 +1618,6 @@ static inline bool tcp_stream_is_thin(struct tcp_sock *tp)/* /proc */ enum tcp_seq_states {TCP_SEQ_STATE_LISTENING , -TCP_SEQ_STATE_OPENREQ ,TCP_SEQ_STATE_ESTABLISHED , };
@@ -1717,8 +1716,6 @@ struct tcp_request_sock_ops {int (*send_synack)(const struct sock *sk,struct dst_entry *dst,struct flowi *fl,struct request_sock *req,u16 queue_mapping,struct tcp_fastopen_cookie *foc); -void (*queue_hash_add)(struct sock *sk,struct request_sock *req, -const unsigned long timeout); };#ifdef CONFIG_SYN_COOKIES
net/core/request_sock.c +1 -27
@@ -99,35 +99,9 @@ static inline struct listen_sock *reqsk_queue_yank_listen_sk(void reqsk_queue_destroy (struct request_sock_queue *queue) { -/* make all the listen_opt local to us */ struct listen_sock *lopt =reqsk_queue_yank_listen_sk (queue); -if (reqsk_queue_len (queue) !=0 ) { -unsigned int i; - -for (i =0 ; i < lopt->nr_table_entries; i++) { -struct request_sock *req; - -spin_lock_bh (&queue->syn_wait_lock); -while ((req = lopt->syn_table[i]) !=NULL ) { - lopt->syn_table[i] = req->dl_next; -/* Because of following del_timer_sync(), -* we must release the spinlock here -* or risk a dead lock. -*/ -spin_unlock_bh (&queue->syn_wait_lock); -atomic_dec (&queue->qlen); -if (del_timer_sync (&req->rsk_timer)) -reqsk_put (req); -reqsk_put (req); -spin_lock_bh (&queue->syn_wait_lock); - } -spin_unlock_bh (&queue->syn_wait_lock); - } - } - -if (WARN_ON (reqsk_queue_len (queue) !=0 )) -pr_err ("qlen %u\n" ,reqsk_queue_len (queue)); +/* cleaning is done by req timers */ kvfree (lopt); }
net/dccp/ipv4.c +21 -43
88 lines not on this page · raw message
net/dccp/ipv6.c +22 -50
96 lines not on this page · raw message
net/ipv4/inet_connection_sock.c +21 -82
158 lines not on this page · raw message
net/ipv4/inet_diag.c +8 -88
134 lines not on this page · raw message
net/ipv4/inet_hashtables.c +12 -2
@@ -398,14 +398,18 @@ static u32 inet_sk_port_offset(const struct sock *sk) inet->inet_dport); } -void __inet_hash_nolisten (struct sock *sk,struct sock *osk) +/* insert a socket into ehash, and eventually remove another one +* (The another one can be a SYN_RECV or TIMEWAIT +*/ +int inet_ehash_insert (struct sock *sk,struct sock *osk) {struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo;struct hlist_nulls_head *list;struct inet_ehash_bucket *head;spinlock_t *lock; +int ret =0 ; -WARN_ON (!sk_unhashed (sk)); +WARN_ON_ONCE (!sk_unhashed (sk)); sk->sk_hash =sk_ehashfn (sk); head =inet_ehash_bucket (hashinfo, sk->sk_hash);
@@ -419,6 +423,12 @@ void __inet_hash_nolisten(struct sock *sk, struct sock *osk)sk_nulls_del_node_init_rcu (osk); }spin_unlock (lock); +return ret; +} + +void __inet_hash_nolisten (struct sock *sk,struct sock *osk) +{ +inet_ehash_insert (sk, osk);sock_prot_inuse_add (sock_net (sk), sk->sk_prot,1 ); }EXPORT_SYMBOL_GPL (__inet_hash_nolisten);
net/ipv4/syncookies.c +4 -0
@@ -284,6 +284,10 @@ bool cookie_ecn_ok(const struct tcp_options_received *tcp_opt, }EXPORT_SYMBOL (cookie_ecn_ok); +/* On input, sk is a listener. +* Output is listener if incoming packet would not create a child +* NULL if memory could not be allocated. +*/ struct sock *cookie_v4_check (struct sock *sk,struct sk_buff *skb) {struct ip_options *opt = &TCP_SKB_CB (skb)->header.h4.opt;
net/ipv4/tcp_input.c +1 -1
@@ -6241,7 +6241,7 @@ int tcp_conn_request(struct request_sock_ops *rsk_ops, goto drop_and_free;tcp_rsk (req)->tfo_listener = false; - af_ops->queue_hash_add (sk, req,TCP_TIMEOUT_INIT ); +inet_csk_reqsk_queue_hash_add (sk, req,TCP_TIMEOUT_INIT ); }tcp_reqsk_record_syn (sk, req, skb);
net/ipv4/tcp_ipv4.c +33 -84
183 lines not on this page · raw message
net/ipv6/inet6_connection_sock.c +0 -67
73 lines not on this page · raw message
net/ipv6/tcp_ipv6.c +36 -46
119 lines not on this page · raw message
Signature · 1 line
2.6.0.rc2.230.g3dd15c0
PATCH 11/17 tcp/dccp: shrink struct listen_sock
In reply to Eric DumazetWe no longer use hash_rnd, nr_table_entries and syn_table[]
For a listener with a backlog of 10 millions sockets, this saves 80 MBytes of vmalloced memory.
--- include/net/request_sock.h | 3 --- net/core/request_sock.c | 14 +++----------- 2 files changed, 3 insertions(+), 14 deletions(-)
include/net/request_sock.h +0 -3
@@ -125,9 +125,6 @@ extern int sysctl_max_syn_backlog;*/ struct listen_sock {u32 max_qlen_log; -u32 hash_rnd; -u32 nr_table_entries; -struct request_sock *syn_table[0 ]; };/*
net/core/request_sock.c +3 -11
@@ -46,18 +46,11 @@ int reqsk_queue_alloc(struct request_sock_queue *queue, nr_table_entries =min_t (u32, nr_table_entries, sysctl_max_syn_backlog); nr_table_entries =max_t (u32, nr_table_entries,8 ); nr_table_entries =roundup_pow_of_two (nr_table_entries +1 ); - lopt_size += nr_table_entries *sizeof (struct request_sock *); -if (lopt_size <= (PAGE_SIZE <<PAGE_ALLOC_COSTLY_ORDER )) - lopt =kzalloc (lopt_size,GFP_KERNEL | - __GFP_NOWARN | - __GFP_NORETRY); -if (!lopt) - lopt =vzalloc (lopt_size); + lopt =kzalloc (lopt_size,GFP_KERNEL );if (!lopt)return -ENOMEM ; -get_random_bytes (&lopt->hash_rnd,sizeof (lopt->hash_rnd));spin_lock_init (&queue->rskq_lock);spin_lock_init (&queue->syn_wait_lock);
@@ -68,7 +61,6 @@ int reqsk_queue_alloc(struct request_sock_queue *queue, queue->fastopenq.max_qlen =0 ; queue->rskq_accept_head =NULL ; - lopt->nr_table_entries = nr_table_entries; lopt->max_qlen_log =ilog2 (nr_table_entries);spin_lock_bh (&queue->syn_wait_lock);
@@ -81,7 +73,7 @@ int reqsk_queue_alloc(struct request_sock_queue *queue,void __reqsk_queue_destroy (struct request_sock_queue *queue) {/* This is an error recovery path only, no locking needed */ -kvfree (queue->listen_opt); +kfree (queue->listen_opt); } static inline struct listen_sock *reqsk_queue_yank_listen_sk(
@@ -102,7 +94,7 @@ void reqsk_queue_destroy(struct request_sock_queue *queue)struct listen_sock *lopt =reqsk_queue_yank_listen_sk (queue);/* cleaning is done by req timers */ -kvfree (lopt); +kfree (lopt); }/*
Signature · 1 line
2.6.0.rc2.230.g3dd15c0
PATCH 12/17 ipv6: remove obsolete inet6 functions
In reply to Eric Dumazetinet6_csk_search_req() and inet6_csk_reqsk_queue_hash_add() no longer exist.
--- include/net/inet6_connection_sock.h | 9 --------- 1 file changed, 9 deletions(-)
include/net/inet6_connection_sock.h +0 -9
@@ -28,15 +28,6 @@ int inet6_csk_bind_conflict(const struct sock *sk,struct dst_entry *inet6_csk_route_req (const struct sock *sk,struct flowi6 *fl6,const struct request_sock *req,u8 proto); -struct request_sock *inet6_csk_search_req (struct sock *sk, -const __be16 rport, -const struct in6_addr *raddr, -const struct in6_addr *laddr, -const int iif); - -void inet6_csk_reqsk_queue_hash_add (struct sock *sk,struct request_sock *req, -const unsigned long timeout); -void inet6_csk_addr2sockaddr (struct sock *sk,struct sockaddr *uaddr);int inet6_csk_xmit (struct sock *sk,struct sk_buff *skb,struct flowi *fl);
Signature · 1 line
2.6.0.rc2.230.g3dd15c0
PATCH 13/17 tcp: attach SYNACK messages to request sockets instead of listener
In reply to Eric DumazetIf a listen backlog is very big (to avoid syncookies), then the listener sk->sk_wmem_alloc is the main source of false sharing, as we need to touch it twice per SYNACK re-transmit and TX completion.
(One SYN packet takes listener lock once, but up to 6 SYNACK are generated)
By attaching the skb to the request socket, we remove this source of contention.
Tested:
listen(fd, 10485760); // single listener (no SO_REUSEPORT) 16 RX/TX queue NIC
Sustain a SYNFLOOD attack of ~320,000 SYN per second, Sending ~1,400,000 SYNACK per second.
Perf profiles now show listener spinlock being next bottleneck.
20.29% [kernel] [k] queued_spin_lock_slowpath
10.06% [kernel] [k] __inet_lookup_established
5.12% [kernel] [k] reqsk_timer_handler
3.22% [kernel] [k] get_next_timer_interrupt
3.00% [kernel] [k] tcp_make_synack
2.77% [kernel] [k] ipt_do_table
2.70% [kernel] [k] run_timer_softirq
2.50% [kernel] [k] ip_finish_output
2.04% [kernel] [k] cascade--- include/net/tcp.h | 6 ++++-- net/ipv4/inet_connection_sock.c | 2 +- net/ipv4/tcp_fastopen.c | 4 ++-- net/ipv4/tcp_input.c | 23 ++++++++++++----------- net/ipv4/tcp_ipv4.c | 5 +++-- net/ipv4/tcp_output.c | 22 +++++++++++++++------- net/ipv6/tcp_ipv6.c | 5 +++-- net/sched/sch_fq.c | 12 +++++++----- 8 files changed, 47 insertions(+), 32 deletions(-)
include/net/tcp.h +4 -2
@@ -462,7 +462,8 @@ int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len);int tcp_connect (struct sock *sk);struct sk_buff *tcp_make_synack (const struct sock *sk,struct dst_entry *dst,struct request_sock *req, -struct tcp_fastopen_cookie *foc); +struct tcp_fastopen_cookie *foc, +bool attach_req);int tcp_disconnect (struct sock *sk,int flags);void tcp_finish_connect (struct sock *sk,struct sk_buff *skb);
@@ -1715,7 +1716,8 @@ struct tcp_request_sock_ops {__u32 (*init_seq)(const struct sk_buff *skb);int (*send_synack)(const struct sock *sk,struct dst_entry *dst,struct flowi *fl,struct request_sock *req, -u16 queue_mapping,struct tcp_fastopen_cookie *foc); +u16 queue_mapping,struct tcp_fastopen_cookie *foc, +bool attach_req); };#ifdef CONFIG_SYN_COOKIES
net/ipv4/inet_connection_sock.c +1 -1
@@ -628,7 +628,7 @@ static void reqsk_queue_hash_req(struct request_sock *req,* are committed to memory and refcnt initialized. */ smp_wmb (); -atomic_set (&req->rsk_refcnt,2 ); +atomic_set (&req->rsk_refcnt,2 +1 ); } void inet_csk_reqsk_queue_hash_add(struct sock *sk, struct request_sock *req,
net/ipv4/tcp_fastopen.c +2 -2
@@ -161,13 +161,13 @@ static struct sock *tcp_fastopen_create_child(struct sock *sk, tp->snd_wnd =ntohs (tcp_hdr (skb)->window);/* Activate the retrans timer so that SYNACK can be retransmitted. -* The request socket is not added to the SYN table of the parent +* The request socket is not added to the ehash * because it's been added to the accept queue directly. */ inet_csk_reset_xmit_timer (child,ICSK_TIME_RETRANS ,TCP_TIMEOUT_INIT ,TCP_RTO_MAX ); -atomic_set (&req->rsk_refcnt,1 ); +atomic_set (&req->rsk_refcnt,2 );/* Add the child socket directly into the accept queue */ inet_csk_reqsk_queue_add (sk, req, child);
net/ipv4/tcp_input.c +12 -11
@@ -6120,8 +6120,6 @@ int tcp_conn_request(struct request_sock_ops *rsk_ops,struct request_sock *req;bool want_cookie = false;struct flowi fl; -int err; -/* TW buckets are converted to open requests without * limitations, they conserve resources and peer is
@@ -6230,21 +6228,24 @@ int tcp_conn_request(struct request_sock_ops *rsk_ops,tcp_rsk (req)->snt_isn = isn;tcp_rsk (req)->txhash =net_tx_rndhash ();tcp_openreq_init_rwin (req, sk, dst); -if (!want_cookie) +if (!want_cookie) { fastopen_sk =tcp_try_fastopen (sk, skb, req, &foc, dst); - err = af_ops->send_synack (fastopen_sk ?: sk, dst, &fl, req, -skb_get_queue_mapping (skb), &foc); +tcp_reqsk_record_syn (sk, req, skb); + }if (fastopen_sk) { + af_ops->send_synack (fastopen_sk, dst, &fl, req, +skb_get_queue_mapping (skb), &foc, false);sock_put (fastopen_sk); }else { -if (err || want_cookie) - goto drop_and_free; -tcp_rsk (req)->tfo_listener = false; -inet_csk_reqsk_queue_hash_add (sk, req,TCP_TIMEOUT_INIT ); +if (!want_cookie) +inet_csk_reqsk_queue_hash_add (sk, req,TCP_TIMEOUT_INIT ); + af_ops->send_synack (sk, dst, &fl, req, +skb_get_queue_mapping (skb), &foc, !want_cookie); +if (want_cookie) + goto drop_and_free; } -tcp_reqsk_record_syn (sk, req, skb); - +reqsk_put (req);return 0 ; drop_and_release:
net/ipv4/tcp_ipv4.c +3 -2
@@ -822,7 +822,8 @@ static int tcp_v4_send_synack(const struct sock *sk, struct dst_entry *dst,struct flowi *fl, struct request_sock *req, u16 queue_mapping, - struct tcp_fastopen_cookie *foc) + struct tcp_fastopen_cookie *foc, + bool attach_req) { const struct inet_request_sock *ireq = inet_rsk(req);struct flowi4 fl4;
@@ -833,7 +834,7 @@ static int tcp_v4_send_synack(const struct sock *sk, struct dst_entry *dst,if (!dst && (dst =inet_csk_route_req (sk, &fl4, req)) ==NULL )return -1 ; - skb =tcp_make_synack (sk, dst, req, foc); + skb =tcp_make_synack (sk, dst, req, foc, attach_req);if (skb) {__tcp_v4_send_check (skb, ireq->ir_loc_addr, ireq->ir_rmt_addr);
net/ipv4/tcp_output.c +15 -7
@@ -2947,7 +2947,8 @@ int tcp_send_synack(struct sock *sk)*/ struct sk_buff *tcp_make_synack (const struct sock *sk,struct dst_entry *dst,struct request_sock *req, -struct tcp_fastopen_cookie *foc) +struct tcp_fastopen_cookie *foc, +bool attach_req) {struct inet_request_sock *ireq =inet_rsk (req);const struct tcp_sock *tp =tcp_sk (sk);
@@ -2959,11 +2960,7 @@ struct sk_buff *tcp_make_synack(const struct sock *sk, struct dst_entry *dst,u16 user_mss;int mss; -/* sk is a const pointer, because we want to express multiple cpus -* might call us concurrently. -* sock_wmalloc() will change sk->sk_wmem_alloc in an atomic way. -*/ - skb =sock_wmalloc ((struct sock *)sk,MAX_TCP_HEADER ,1 ,GFP_ATOMIC ); + skb =alloc_skb (MAX_TCP_HEADER ,GFP_ATOMIC );if (unlikely (!skb)) {dst_release (dst);return NULL ;
@@ -2971,6 +2968,17 @@ struct sk_buff *tcp_make_synack(const struct sock *sk, struct dst_entry *dst,/* Reserve space for headers. */ skb_reserve (skb,MAX_TCP_HEADER ); +if (attach_req) { + skb->destructor = sock_edemux; +sock_hold (req_to_sk (req)); + skb->sk =req_to_sk (req); + }else { +/* sk is a const pointer, because we want to express multiple +* cpu might call us concurrently. +* sk->sk_wmem_alloc in an atomic, we can promote to rw. +*/ +skb_set_owner_w (skb, (struct sock *)sk); + }skb_dst_set (skb, dst); mss =dst_metric_advmss (dst);
@@ -3510,7 +3518,7 @@ int tcp_rtx_synack(const struct sock *sk, struct request_sock *req)int res;tcp_rsk (req)->txhash =net_tx_rndhash (); - res = af_ops->send_synack (sk,NULL , &fl, req,0 ,NULL ); + res = af_ops->send_synack (sk,NULL , &fl, req,0 ,NULL , true);if (!res) {TCP_INC_STATS_BH (sock_net (sk),TCP_MIB_RETRANSSEGS );NET_INC_STATS_BH (sock_net (sk),LINUX_MIB_TCPSYNRETRANS );
net/ipv6/tcp_ipv6.c +3 -2
@@ -438,7 +438,8 @@ static int tcp_v6_send_synack(const struct sock *sk, struct dst_entry *dst,struct flowi *fl, struct request_sock *req, u16 queue_mapping, - struct tcp_fastopen_cookie *foc) + struct tcp_fastopen_cookie *foc, + bool attach_req) { struct inet_request_sock *ireq = inet_rsk(req);struct ipv6_pinfo *np =inet6_sk (sk);
@@ -451,7 +452,7 @@ static int tcp_v6_send_synack(const struct sock *sk, struct dst_entry *dst, IPPROTO_TCP)) == NULL) goto done; - skb =tcp_make_synack (sk, dst, req, foc); + skb =tcp_make_synack (sk, dst, req, foc, attach_req);if (skb) { __tcp_v6_send_check(skb, &ireq->ir_v6_loc_addr,
net/sched/sch_fq.c +7 -5
@@ -224,13 +224,15 @@ static struct fq_flow *fq_classify(struct sk_buff *skb, struct fq_sched_data *q)if (unlikely ((skb->priority &TC_PRIO_MAX ) ==TC_PRIO_CONTROL ))return &q->internal; -/* SYNACK messages are attached to a listener socket. -* 1) They are not part of a 'flow' yet -* 2) We do not want to rate limit them (eg SYNFLOOD attack), +/* SYNACK messages are attached to a TCP_NEW_SYN_RECV request socket +* 1) request sockets are not full blown, +* they do not contain sk_pacing_rate +* 2) They are not part of a 'flow' yet +* 3) We do not want to rate limit them (eg SYNFLOOD attack), * especially if the listener set SO_MAX_PACING_RATE -* 3) We pretend they are orphaned +* 4) We pretend they are orphaned */ -if (!sk || sk->sk_state ==TCP_LISTEN ) { +if (!sk || sk->sk_state ==TCP_NEW_SYN_RECV ) {unsigned long hash =skb_get_hash (skb) & q->orphan_mask;/* By forcing low order bit to 1, we make sure to not
Signature · 1 line
2.6.0.rc2.230.g3dd15c0
PATCH 14/17 tcp/dccp: remove struct listen_sock
In reply to Eric DumazetIt is enough to check listener sk_state, no need for an extra condition.
max_qlen_log can be moved into struct request_sock_queue
We can remove syn_wait_lock and the alignment it enforced.
--- include/net/request_sock.h | 26 ++++------------------- net/core/request_sock.c | 47 +++-------------------------------------- net/ipv4/inet_connection_sock.c | 14 ++++-------- 3 files changed, 11 insertions(+), 76 deletions(-)
include/net/request_sock.h +4 -22
@@ -119,14 +119,6 @@ static inline void reqsk_put(struct request_sock *req)extern int sysctl_max_syn_backlog; -/** struct listen_sock - listen state -* -* @max_qlen_log - log_2 of maximal queued SYNs/REQUESTs -*/ -struct listen_sock { -u32 max_qlen_log; -}; -/* * For a TCP Fast Open listener - * lock - protects the access to all the reqsk, which is co-owned by
@@ -160,36 +152,26 @@ struct fastopen_queue {* @rskq_accept_head - FIFO head of established children * @rskq_accept_tail - FIFO tail of established children * @rskq_defer_accept - User waits for some data after accept() -* @syn_wait_lock - serializer -* -* %syn_wait_lock is necessary only to avoid proc interface having to grab the main -* lock sock while browsing the listening hash (otherwise it's deadlock prone). * */ struct request_sock_queue {spinlock_t rskq_lock;u8 rskq_defer_accept; +u8 max_qlen_log;u32 synflood_warned; -atomic_t qlen;atomic_t young;struct request_sock *rskq_accept_head;struct request_sock *rskq_accept_tail; -struct listen_sock *listen_opt;struct fastopen_queue fastopenq;/* Check max_qlen != 0 to determine * if TFO is enabled. */ - -/* temporary alignment, our goal is to get rid of this lock */ -spinlock_t syn_wait_lock____cacheline_aligned_in_smp ; }; -int reqsk_queue_alloc (struct request_sock_queue *queue, -unsigned int nr_table_entries); +void reqsk_queue_alloc (struct request_sock_queue *queue, +unsigned int nr_table_entries); -void __reqsk_queue_destroy (struct request_sock_queue *queue); -void reqsk_queue_destroy (struct request_sock_queue *queue);void reqsk_fastopen_remove (struct sock *sk,struct request_sock *req,bool reset);
@@ -260,7 +242,7 @@ static inline int reqsk_queue_len_young(const struct request_sock_queue *queue)static inline int reqsk_queue_is_full (const struct request_sock_queue *queue) { -return reqsk_queue_len (queue) >> queue->listen_opt->max_qlen_log; +return reqsk_queue_len (queue) >> queue->max_qlen_log; }#endif /* _REQUEST_SOCK_H */
net/core/request_sock.c +3 -44
65 lines not on this page · raw message
net/ipv4/inet_connection_sock.c +4 -10
@@ -552,12 +552,11 @@ static void reqsk_timer_handler(unsigned long data)struct sock *sk_listener = req->rsk_listener;struct inet_connection_sock *icsk =inet_csk (sk_listener);struct request_sock_queue *queue = &icsk->icsk_accept_queue; -struct listen_sock *lopt = queue->listen_opt;int qlen, expire =0 , resend =0 ;int max_retries, thresh;u8 defer_accept; -if (sk_listener->sk_state !=TCP_LISTEN || !lopt) +if (sk_listener->sk_state !=TCP_LISTEN ) goto drop; max_retries = icsk->icsk_syn_retries ? : sysctl_tcp_synack_retries;
@@ -580,7 +579,7 @@ static void reqsk_timer_handler(unsigned long data)* ones are about to clog our table. */ qlen =reqsk_queue_len (queue); -if (qlen >> (lopt->max_qlen_log -1 )) { +if (qlen >> (queue->max_qlen_log -1 )) {int young =reqsk_queue_len_young (queue) <<1 ;while (thresh >2 ) {
@@ -730,12 +729,10 @@ EXPORT_SYMBOL(inet_csk_prepare_forced_close);int inet_csk_listen_start (struct sock *sk,const int nr_table_entries) { -struct inet_sock *inet =inet_sk (sk);struct inet_connection_sock *icsk =inet_csk (sk); -int rc =reqsk_queue_alloc (&icsk->icsk_accept_queue, nr_table_entries); +struct inet_sock *inet =inet_sk (sk); -if (rc !=0 ) -return rc; +reqsk_queue_alloc (&icsk->icsk_accept_queue, nr_table_entries); sk->sk_max_ack_backlog =0 ; sk->sk_ack_backlog =0 ;
@@ -757,7 +754,6 @@ int inet_csk_listen_start(struct sock *sk, const int nr_table_entries) } sk->sk_state =TCP_CLOSE ; -__reqsk_queue_destroy (&icsk->icsk_accept_queue);return -EADDRINUSE ; }EXPORT_SYMBOL_GPL (inet_csk_listen_start);
@@ -780,8 +776,6 @@ void inet_csk_listen_stop(struct sock *sk)* To be honest, we are not able to make either * of the variants now. --ANK */ -reqsk_queue_destroy (queue); -while ((req =reqsk_queue_remove (queue, sk)) !=NULL ) {struct sock *child = req->sk;
Signature · 1 line
2.6.0.rc2.230.g3dd15c0
PATCH 15/17 tcp: remove max_qlen_log
In reply to Eric DumazetThis control variable was set at first listen(fd, backlog) call, but not updated if application tried to increase or decrease backlog. It made sense at the time listener had a non resizeable hash table.
Also rounding to powers of two was not very friendly.
--- include/net/inet_connection_sock.h | 2 +- include/net/request_sock.h | 10 ++-------- net/core/request_sock.c | 8 +------- net/ipv4/inet_connection_sock.c | 4 ++-- 4 files changed, 6 insertions(+), 18 deletions(-)
include/net/inet_connection_sock.h +1 -1
@@ -295,7 +295,7 @@ static inline int inet_csk_reqsk_queue_young(const struct sock *sk)static inline int inet_csk_reqsk_queue_is_full (const struct sock *sk) { -return reqsk_queue_is_full (&inet_csk (sk)->icsk_accept_queue); +return inet_csk_reqsk_queue_len (sk) >= sk->sk_max_ack_backlog; }void inet_csk_reqsk_queue_drop (struct sock *sk,struct request_sock *req);
include/net/request_sock.h +2 -8
@@ -157,7 +157,7 @@ struct fastopen_queue {struct request_sock_queue {spinlock_t rskq_lock;u8 rskq_defer_accept; -u8 max_qlen_log; +u32 synflood_warned;atomic_t qlen;atomic_t young;
@@ -169,8 +169,7 @@ struct request_sock_queue {*/ }; -void reqsk_queue_alloc (struct request_sock_queue *queue, -unsigned int nr_table_entries); +void reqsk_queue_alloc (struct request_sock_queue *queue);void reqsk_fastopen_remove (struct sock *sk,struct request_sock *req,bool reset);
@@ -240,9 +239,4 @@ static inline int reqsk_queue_len_young(const struct request_sock_queue *queue) return atomic_read(&queue->young); } -static inline int reqsk_queue_is_full (const struct request_sock_queue *queue) -{ -return reqsk_queue_len (queue) >> queue->max_qlen_log; -} -#endif /* _REQUEST_SOCK_H */
net/core/request_sock.c +1 -7
@@ -37,13 +37,8 @@int sysctl_max_syn_backlog =256 ;EXPORT_SYMBOL (sysctl_max_syn_backlog); -void reqsk_queue_alloc (struct request_sock_queue *queue, -unsigned int nr_table_entries) +void reqsk_queue_alloc (struct request_sock_queue *queue) { - nr_table_entries =min_t (u32, nr_table_entries, sysctl_max_syn_backlog); - nr_table_entries =max_t (u32, nr_table_entries,8 ); - nr_table_entries =roundup_pow_of_two (nr_table_entries +1 ); -spin_lock_init (&queue->rskq_lock);spin_lock_init (&queue->fastopenq.lock);
@@ -53,7 +48,6 @@ void reqsk_queue_alloc(struct request_sock_queue *queue, queue->fastopenq.max_qlen =0 ; queue->rskq_accept_head =NULL ; - queue->max_qlen_log =ilog2 (nr_table_entries); }/*
net/ipv4/inet_connection_sock.c +2 -2
@@ -579,7 +579,7 @@ static void reqsk_timer_handler(unsigned long data)* ones are about to clog our table. */ qlen =reqsk_queue_len (queue); -if (qlen >> (queue->max_qlen_log -1 )) { +if ((qlen <<1 ) > sk_listener->sk_max_ack_backlog) {int young =reqsk_queue_len_young (queue) <<1 ;while (thresh >2 ) {
@@ -732,7 +732,7 @@ int inet_csk_listen_start(struct sock *sk, const int nr_table_entries)struct inet_connection_sock *icsk =inet_csk (sk);struct inet_sock *inet =inet_sk (sk); -reqsk_queue_alloc (&icsk->icsk_accept_queue, nr_table_entries); +reqsk_queue_alloc (&icsk->icsk_accept_queue); sk->sk_max_ack_backlog =0 ; sk->sk_ack_backlog =0 ;
Signature · 1 line
2.6.0.rc2.230.g3dd15c0
PATCH 16/17 tcp/dccp: add a reschedule point in inet_csk_listen_stop()
In reply to Eric DumazetIf a listener with thousands of children in accept queue is dismantled, it can take a while to close all of them.
--- net/ipv4/inet_connection_sock.c | 1 + 1 file changed, 1 insertion(+)
net/ipv4/inet_connection_sock.c +1 -0
@@ -809,6 +809,7 @@ void inet_csk_listen_stop(struct sock *sk)sock_put (child);reqsk_put (req); +cond_resched (); }if (queue->fastopenq.rskq_rst_head) {/* Free all the reqs queued in rskq_rst_head. */
Signature · 1 line
2.6.0.rc2.230.g3dd15c0
PATCH 17/17 tcp: do not lock listener to process SYN packets
In reply to Eric DumazetEverything should now be ready to finally allow SYN packets processing without holding listener lock.
Tested:
3.5 Mpps SYNFLOOD. Plenty of cpu cycles available.
Next bottleneck is the refcount taken on listener, that could be avoided if we remove SLAB_DESTROY_BY_RCU strict semantic for listeners, and use regular RCU.
13.18% [kernel] [k] __inet_lookup_listener
9.61% [kernel] [k] tcp_conn_request
8.16% [kernel] [k] sha_transform
5.30% [kernel] [k] inet_reqsk_alloc
4.22% [kernel] [k] sock_put
3.74% [kernel] [k] tcp_make_synack
2.88% [kernel] [k] ipt_do_table
2.56% [kernel] [k] memcpy_erms
2.53% [kernel] [k] sock_wfree
2.40% [kernel] [k] tcp_v4_rcv
2.08% [kernel] [k] fib_table_lookup
1.84% [kernel] [k] tcp_openreq_init_rwin--- net/ipv4/tcp_ipv4.c | 11 +++++++++-- net/ipv6/tcp_ipv6.c | 11 +++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-)
net/ipv4/tcp_ipv4.c +9 -2
@@ -1355,7 +1355,7 @@ static struct sock *tcp_v4_cookie_check(struct sock *sk, struct sk_buff *skb) }/* The socket must have it's spinlock held when we get -* here. +* here, unless it is a TCP_LISTEN socket. * * We have a potential double-lock case here, so even when * doing backlog processing we use the BH locking scheme.
@@ -1619,9 +1619,15 @@ process:if (sk_filter (sk, skb)) goto discard_and_relse; -sk_incoming_cpu_update (sk); skb->dev =NULL ; +if (sk->sk_state ==TCP_LISTEN ) { + ret =tcp_v4_do_rcv (sk, skb); + goto put_and_return; + } + +sk_incoming_cpu_update (sk); +bh_lock_sock_nested (sk);tcp_sk (sk)->segs_in +=max_t (u16,1 ,skb_shinfo (skb)->gso_segs); ret =0 ;
@@ -1636,6 +1642,7 @@ process: }bh_unlock_sock (sk); +put_and_return:sock_put (sk);return ret;
net/ipv6/tcp_ipv6.c +9 -2
@@ -1161,7 +1161,7 @@ out: }/* The socket must have it's spinlock held when we get -* here. +* here, unless it is a TCP_LISTEN socket. * * We have a potential double-lock case here, so even when * doing backlog processing we use the BH locking scheme.
@@ -1415,9 +1415,15 @@ process:if (sk_filter (sk, skb)) goto discard_and_relse; -sk_incoming_cpu_update (sk); skb->dev =NULL ; +if (sk->sk_state ==TCP_LISTEN ) { + ret =tcp_v6_do_rcv (sk, skb); + goto put_and_return; + } + +sk_incoming_cpu_update (sk); +bh_lock_sock_nested (sk);tcp_sk (sk)->segs_in +=max_t (u16,1 ,skb_shinfo (skb)->gso_segs); ret =0 ;
@@ -1432,6 +1438,7 @@ process: }bh_unlock_sock (sk); +put_and_return:sock_put (sk);return ret ?-1 :0 ;
Signature · 1 line
2.6.0.rc2.230.g3dd15c0
David Miller
maintainerFrom: Eric Dumazet <[email protected]>
Date: Fri, 2 Oct 2015 11:43:22 -0700
Quoted 16 lines
TCP listener refactoring : this is becoming interesting !
This patch series takes the steps to use normal TCP/DCCP ehash table to store
SYN_RECVrequests, instead of the private per-listener hash table we had until now.SYNACK skb are now attached to their
syn_recvrequest socket, so that we no longer heavily modify listenersk_wmem_alloc.listener lock is no longer held in fast path, including SYNCOOKIE mode.
During my tests, my server was able to process 3,500,000 SYN packets per second on one listener and still had available cpu cycles.
Quoted 7 lines
That is about 2 to 3 order of magnitude what we had with older kernels.
This effort started two years ago and I am pleased to reach expectations.
We'll probably extend
SO_REUSEPORTto add proper cpu/numa affinities, so that heavy duty TCP servers can get proper siloing thanks to multi-queues NIC.
This looks amazing, series applied, thanks Eric!
Quoted 1 line · David Miller
This looks amazing, series applied, thanks Eric!
Thanks David.
While splitting my patches, it seems I somehow missed one change, adding SLAB_DESTROY_BY_RCU flag for request kmem_cache.
I will send the patch in few minutes.