tcp: Add a 2nd listener hashtable (port+addr)
Subsystems (5)
cover tcp: Add a 2nd listener hashtable (port+addr)
This patch set adds a 2nd listener hashtable. It is to resolve the performance issue when a process is listening at many IP addresses with the same port (e.g. [IP1]:443, [IP2]:443... [IPN]:443)
Martin KaFai Lau (4):
inet: Add a count to struct inet_listen_hashbucket
udp: Move udp[46]_portaddr_hash() to net/ip[v6].h
inet: Add a 2nd listener hashtable (port+addr)
tcp: Enable 2nd listener hashtable in TCP
include/net/inet_connection_sock.h | 2 + include/net/inet_hashtables.h | 16 ++++ include/net/ip.h | 9 ++ include/net/ipv6.h | 17 ++++ net/ipv4/inet_hashtables.c | 173 +++++++++++++++++++++++++++++++++++-- net/ipv4/tcp.c | 3 + net/ipv4/udp.c | 22 ++--- net/ipv6/inet6_hashtables.c | 66 ++++++++++++++ net/ipv6/udp.c | 32 ++----- 9 files changed, 294 insertions(+), 46 deletions(-)
Signature · 1 line
2.9.5
PATCH 1/4 inet: Add a count to struct inet_listen_hashbucket
This patch adds a count to the 'struct inet_listen_hashbucket'. It counts how many sk is hashed to a bucket. It will be used to decide if the (to-be-added) portaddr listener's hashtable should be used during inet[6]_lookup_listener().
--- include/net/inet_hashtables.h | 1 + net/ipv4/inet_hashtables.c | 11 +++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-)
include/net/inet_hashtables.h +1 -0
@@ -111,6 +111,7 @@ struct inet_bind_hashbucket {*/ struct inet_listen_hashbucket {spinlock_t lock; +unsigned int count;struct hlist_head head; };
net/ipv4/inet_hashtables.c +9 -2
@@ -476,6 +476,7 @@ int __inet_hash(struct sock *sk, struct sock *osk)hlist_add_tail_rcu (&sk->sk_node, &ilb->head); else hlist_add_head_rcu(&sk->sk_node, &ilb->head); + ilb->count++;sock_set_flag (sk,SOCK_RCU_FREE );sock_prot_inuse_add (sock_net (sk), sk->sk_prot,1 ); unlock:
@@ -502,6 +503,7 @@ EXPORT_SYMBOL_GPL(inet_hash);void inet_unhash (struct sock *sk) {struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo; +struct inet_listen_hashbucket *ilb;spinlock_t *lock;bool listener = false;int done;
@@ -510,7 +512,8 @@ void inet_unhash(struct sock *sk)return ;if (sk->sk_state ==TCP_LISTEN ) { - lock = &hashinfo->listening_hash[inet_sk_listen_hashfn (sk)].lock; + ilb = &hashinfo->listening_hash[inet_sk_listen_hashfn (sk)]; + lock = &ilb->lock; listener = true; }else { lock =inet_ehash_lockp (hashinfo, sk->sk_hash);
@@ -522,8 +525,11 @@ void inet_unhash(struct sock *sk) done =__sk_del_node_init (sk); else done =__sk_nulls_del_node_init_rcu (sk); -if (done) +if (done) { +if (listener) + ilb->count--;sock_prot_inuse_add (sock_net (sk), sk->sk_prot,-1 ); + }spin_unlock_bh (lock); }EXPORT_SYMBOL_GPL (inet_unhash);
@@ -658,6 +664,7 @@ void inet_hashinfo_init(struct inet_hashinfo *h)for (i =0 ; i <INET_LHTABLE_SIZE ; i++) {spin_lock_init (&h->listening_hash[i].lock);INIT_HLIST_HEAD (&h->listening_hash[i].head); + h->listening_hash[i].count =0 ; } }EXPORT_SYMBOL_GPL (inet_hashinfo_init);
Signature · 1 line
2.9.5
PATCH 2/4 udp: Move udp[46]_portaddr_hash() to net/ip[v6].h
In reply to Martin KaFai LauThis patch moves the udp[46]_portaddr_hash()
to net/ip[v6].h. The function name is renamed to
ipv[46]_portaddr_hash().
It will be used by a later patch which adds a second listener hashtable hashed by the address and port.
--- include/net/ip.h | 9 +++++++++ include/net/ipv6.h | 17 +++++++++++++++++ net/ipv4/udp.c | 22 ++++++++-------------- net/ipv6/udp.c | 32 ++++++++------------------------ 4 files changed, 42 insertions(+), 38 deletions(-)
include/net/ip.h +9 -0
@@ -26,12 +26,14 @@#include <linux/ip.h> #include <linux/in.h> #include <linux/skbuff.h> +#include <linux/jhash.h> #include <net/inet_sock.h> #include <net/route.h> #include <net/snmp.h> #include <net/flow.h> #include <net/flow_dissector.h> +#include <net/netns/hash.h> #define IPV4_MAX_PMTU 65535U/* RFC 2675, Section 5.1 */
@@ -521,6 +523,13 @@ static inline unsigned int ipv4_addr_hash(__be32 ip)return (__force unsigned int ) ip; } +static inline u32 ipv4_portaddr_hash (const struct net *net, +__be32 saddr, +unsigned int port) +{ +return jhash_1word ((__force u32 )saddr,net_hash_mix (net)) ^ port; +} +bool ip_call_ra_chain (struct sk_buff *skb);/*
include/net/ipv6.h +17 -0
@@ -22,6 +22,7 @@#include <net/flow.h> #include <net/flow_dissector.h> #include <net/snmp.h> +#include <net/netns/hash.h> #define SIN6_LEN_RFC2133 24
@@ -673,6 +674,22 @@ static inline bool ipv6_addr_v4mapped(const struct in6_addr *a) cpu_to_be32(0x0000ffff))) == 0UL; } +static inline u32 ipv6_portaddr_hash (const struct net *net, +const struct in6_addr *addr6, +unsigned int port) +{ +unsigned int hash, mix =net_hash_mix (net); + +if (ipv6_addr_any (addr6)) + hash =jhash_1word (0 , mix); +else if (ipv6_addr_v4mapped (addr6)) + hash =jhash_1word ((__force u32 )addr6->s6_addr32[3 ], mix); +else + hash =jhash2 ((__force u32 *)addr6->s6_addr32,4 , mix); + +return hash ^ port; +} +/* * Check for a RFC 4843 ORCHID address * (Overlay Routable Cryptographic Hash Identifiers)
net/ipv4/udp.c +8 -14
@@ -357,18 +357,12 @@ int udp_lib_get_port(struct sock *sk, unsigned short snum, }EXPORT_SYMBOL (udp_lib_get_port); -static u32 udp4_portaddr_hash (const struct net *net,__be32 saddr, -unsigned int port) -{ -return jhash_1word ((__force u32 )saddr,net_hash_mix (net)) ^ port; -} -int udp_v4_get_port (struct sock *sk,unsigned short snum) {unsigned int hash2_nulladdr = -udp4_portaddr_hash (sock_net (sk),htonl (INADDR_ANY ), snum); +ipv4_portaddr_hash (sock_net (sk),htonl (INADDR_ANY ), snum);unsigned int hash2_partial = -udp4_portaddr_hash (sock_net (sk),inet_sk (sk)->inet_rcv_saddr,0 ); +ipv4_portaddr_hash (sock_net (sk),inet_sk (sk)->inet_rcv_saddr,0 );/* precompute partial secondary hash */ udp_sk (sk)->udp_portaddr_hash = hash2_partial;
@@ -485,7 +479,7 @@ struct sock *__udp4_lib_lookup(struct net *net, __be32 saddr,u32 hash =0 ;if (hslot->count >10 ) { - hash2 =udp4_portaddr_hash (net, daddr, hnum); + hash2 =ipv4_portaddr_hash (net, daddr, hnum); slot2 = hash2 & udptable->mask; hslot2 = &udptable->hash2[slot2];if (hslot->count < hslot2->count)
@@ -496,7 +490,7 @@ struct sock *__udp4_lib_lookup(struct net *net, __be32 saddr, exact_dif, hslot2, skb);if (!result) {unsigned int old_slot2 = slot2; - hash2 =udp4_portaddr_hash (net,htonl (INADDR_ANY ), hnum); + hash2 =ipv4_portaddr_hash (net,htonl (INADDR_ANY ), hnum); slot2 = hash2 & udptable->mask;/* avoid searching the same slot again. */ if (unlikely (slot2 == old_slot2))
@@ -1761,7 +1755,7 @@ EXPORT_SYMBOL(udp_lib_rehash);static void udp_v4_rehash (struct sock *sk) { -u16 new_hash =udp4_portaddr_hash (sock_net (sk), +u16 new_hash =ipv4_portaddr_hash (sock_net (sk),inet_sk (sk)->inet_rcv_saddr,inet_sk (sk)->inet_num);udp_lib_rehash (sk, new_hash);
@@ -1952,9 +1946,9 @@ static int __udp4_lib_mcast_deliver(struct net *net, struct sk_buff *skb,struct sk_buff *nskb;if (use_hash2) { - hash2_any =udp4_portaddr_hash (net,htonl (INADDR_ANY ), hnum) & + hash2_any =ipv4_portaddr_hash (net,htonl (INADDR_ANY ), hnum) & udptable->mask; - hash2 =udp4_portaddr_hash (net, daddr, hnum) & udptable->mask; + hash2 =ipv4_portaddr_hash (net, daddr, hnum) & udptable->mask; start_lookup: hslot = &udptable->hash2[hash2]; offset = offsetof(typeof(*sk), __sk_common.skc_portaddr_node);
@@ -2186,7 +2180,7 @@ static struct sock *__udp4_lib_demux_lookup(struct net *net, int dif, int sdif) {unsigned short hnum =ntohs (loc_port); -unsigned int hash2 =udp4_portaddr_hash (net, loc_addr, hnum); +unsigned int hash2 =ipv4_portaddr_hash (net, loc_addr, hnum);unsigned int slot2 = hash2 & udp_table.mask;struct udp_hslot *hslot2 = &udp_table.hash2[slot2];INET_ADDR_COOKIE (acookie, rmt_addr, loc_addr);
net/ipv6/udp.c +8 -24
@@ -89,28 +89,12 @@ static u32 udp6_ehashfn(const struct net *net, udp_ipv6_hash_secret + net_hash_mix(net)); } -static u32 udp6_portaddr_hash (const struct net *net, -const struct in6_addr *addr6, -unsigned int port) -{ -unsigned int hash, mix =net_hash_mix (net); - -if (ipv6_addr_any (addr6)) - hash =jhash_1word (0 , mix); -else if (ipv6_addr_v4mapped (addr6)) - hash =jhash_1word ((__force u32 )addr6->s6_addr32[3 ], mix); -else - hash =jhash2 ((__force u32 *)addr6->s6_addr32,4 , mix); - -return hash ^ port; -} -int udp_v6_get_port (struct sock *sk,unsigned short snum) {unsigned int hash2_nulladdr = -udp6_portaddr_hash (sock_net (sk), &in6addr_any, snum); +ipv6_portaddr_hash (sock_net (sk), &in6addr_any, snum);unsigned int hash2_partial = -udp6_portaddr_hash (sock_net (sk), &sk->sk_v6_rcv_saddr,0 ); +ipv6_portaddr_hash (sock_net (sk), &sk->sk_v6_rcv_saddr,0 );/* precompute partial secondary hash */ udp_sk (sk)->udp_portaddr_hash = hash2_partial;
@@ -119,7 +103,7 @@ int udp_v6_get_port(struct sock *sk, unsigned short snum)static void udp_v6_rehash (struct sock *sk) { -u16 new_hash =udp6_portaddr_hash (sock_net (sk), +u16 new_hash =ipv6_portaddr_hash (sock_net (sk), &sk->sk_v6_rcv_saddr,inet_sk (sk)->inet_num);
@@ -225,7 +209,7 @@ struct sock *__udp6_lib_lookup(struct net *net,u32 hash =0 ;if (hslot->count >10 ) { - hash2 =udp6_portaddr_hash (net, daddr, hnum); + hash2 =ipv6_portaddr_hash (net, daddr, hnum); slot2 = hash2 & udptable->mask; hslot2 = &udptable->hash2[slot2];if (hslot->count < hslot2->count)
@@ -236,7 +220,7 @@ struct sock *__udp6_lib_lookup(struct net *net, hslot2, skb);if (!result) {unsigned int old_slot2 = slot2; - hash2 =udp6_portaddr_hash (net, &in6addr_any, hnum); + hash2 =ipv6_portaddr_hash (net, &in6addr_any, hnum); slot2 = hash2 & udptable->mask;/* avoid searching the same slot again. */ if (unlikely (slot2 == old_slot2))
@@ -705,9 +689,9 @@ static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb,struct sk_buff *nskb;if (use_hash2) { - hash2_any =udp6_portaddr_hash (net, &in6addr_any, hnum) & + hash2_any =ipv6_portaddr_hash (net, &in6addr_any, hnum) & udptable->mask; - hash2 =udp6_portaddr_hash (net, daddr, hnum) & udptable->mask; + hash2 =ipv6_portaddr_hash (net, daddr, hnum) & udptable->mask; start_lookup: hslot = &udptable->hash2[hash2]; offset = offsetof(typeof(*sk), __sk_common.skc_portaddr_node);
@@ -895,7 +879,7 @@ static struct sock *__udp6_lib_demux_lookup(struct net *net, int dif, int sdif) {unsigned short hnum =ntohs (loc_port); -unsigned int hash2 =udp6_portaddr_hash (net, loc_addr, hnum); +unsigned int hash2 =ipv6_portaddr_hash (net, loc_addr, hnum);unsigned int slot2 = hash2 & udp_table.mask;struct udp_hslot *hslot2 = &udp_table.hash2[slot2];const __portpair ports =INET_COMBINED_PORTS (rmt_port, hnum);
Signature · 1 line
2.9.5
PATCH 3/4 inet: Add a 2nd listener hashtable (port+addr)
In reply to Martin KaFai LauThe current listener hashtable is hashed by port only.
When a process is listening at many IP addresses with the same port (e.g. [IP1]:443, [IP2]:443... [IPN]:443), the inet[6]_lookup_listener() performance is degraded to a link list. It is prone to syn attack.
UDP had a similar issue and a second hashtable was added to resolve it.
This patch adds a second hashtable for the listener's sockets. The second hashtable is hashed by port and address.
It cannot reuse the existing skc_portaddr_node which is shared with skc_bind_node. TCP listener needs to use skc_bind_node. Instead, this patch adds a hlist_node 'icsk_listen_portaddr_node' to the inet_connection_sock which the listener (like TCP) also belongs to.
The new portaddr hashtable may need two lookup (First by IP:PORT.
Second by INADDR_ANY:PORT if the IP:PORT is a not found). Hence,
it implements a similar cut off as UDP such that it will only consult the new portaddr hashtable if the current port-only hashtable has >10 sk in the link-list.
--- include/net/inet_connection_sock.h | 2 + include/net/inet_hashtables.h | 15 ++++ net/ipv4/inet_hashtables.c | 168 +++++++++++++++++++++++++++++++++++-- net/ipv6/inet6_hashtables.c | 66 +++++++++++++++ 4 files changed, 242 insertions(+), 9 deletions(-)
include/net/inet_connection_sock.h +2 -0
@@ -77,6 +77,7 @@ struct inet_connection_sock_af_ops {* @icsk_af_ops Operations which are AF_INET{4,6} specific * @icsk_ulp_ops Pluggable ULP control hook * @icsk_ulp_data ULP private data +* @icsk_listen_portaddr_node hash to the portaddr listener hashtable * @icsk_ca_state: Congestion control state * @icsk_retransmits: Number of unrecovered [RTO] timeouts * @icsk_pending: Scheduled timer event
@@ -101,6 +102,7 @@ struct inet_connection_sock {const struct inet_connection_sock_af_ops *icsk_af_ops;const struct tcp_ulp_ops *icsk_ulp_ops;void *icsk_ulp_data; +struct hlist_node icsk_listen_portaddr_node;unsigned int (*icsk_sync_mss)(struct sock *sk,u32 pmtu); __u8 icsk_ca_state:6, icsk_ca_setsockopt:1,
include/net/inet_hashtables.h +15 -0
@@ -152,8 +152,19 @@ struct inet_hashinfo {*/ struct inet_listen_hashbucket listening_hash[INET_LHTABLE_SIZE ]____cacheline_aligned_in_smp ; +struct inet_listen_hashbucket *lhash2; +unsigned int lhash2_mask; }; +#define inet_lhash2_for_each_icsk_rcu (__icsk, list) \ + hlist_for_each_entry_rcu(__icsk, list, icsk_listen_portaddr_node) + +static inline struct inet_listen_hashbucket * +inet_lhash2_bucket (struct inet_hashinfo *h,u32 hash) +{ +return &h->lhash2[hash & h->lhash2_mask]; +} +static inline struct inet_ehash_bucket *inet_ehash_bucket (struct inet_hashinfo *hashinfo,unsigned int hash)
@@ -209,6 +220,10 @@ int __inet_inherit_port(const struct sock *sk, struct sock *child);void inet_put_port (struct sock *sk);void inet_hashinfo_init (struct inet_hashinfo *h); +void inet_hashinfo2_init (struct inet_hashinfo *h,const char *name, +unsigned long numentries,int scale, +unsigned long low_limit, +unsigned long high_limit);bool inet_ehash_insert (struct sock *sk,struct sock *osk);bool inet_ehash_nolisten (struct sock *sk,struct sock *osk);
net/ipv4/inet_hashtables.c +159 -9
226 lines not on this page · raw message
net/ipv6/inet6_hashtables.c +66 -0
82 lines not on this page · raw message
Signature · 1 line
2.9.5
PATCH 4/4 tcp: Enable 2nd listener hashtable in TCP
In reply to Martin KaFai LauEnable the second listener hashtable in TCP. The scale is the same as UDP which is one slot per 2MB.
--- net/ipv4/tcp.c | 3 +++ 1 file changed, 3 insertions(+)
net/ipv4/tcp.c +3 -0
@@ -3577,6 +3577,9 @@ void __init tcp_init(void)percpu_counter_init (&tcp_sockets_allocated,0 ,GFP_KERNEL );percpu_counter_init (&tcp_orphan_count,0 ,GFP_KERNEL );inet_hashinfo_init (&tcp_hashinfo); +inet_hashinfo2_init (&tcp_hashinfo,"tcp_listen_portaddr_hash" , + thash_entries,21 ,/* one slot per 2 MB*/ +0 ,64 *1024 ); tcp_hashinfo.bind_bucket_cachep = kmem_cache_create("tcp_bind_bucket", sizeof(struct inet_bind_bucket), 0,
Signature · 1 line
2.9.5
Re: [PATCH net-next 1/4] inet: Add a count to struct inet_listen_hashbucket
In reply to Martin KaFai LauQuoted 8 lines · Martin KaFai Lau
This patch adds a count to the 'struct
inet_listen_hashbucket'. It counts how many sk is hashed to a bucket. It will be used to decide if the (to-be-added) portaddr listener's hashtable should be used during inet[6]_lookup_listener().Signed-off-by Martin KaFai Lau <[email protected]>---
Re: [PATCH net-next 2/4] udp: Move udp[46]_portaddr_hash() to net/ip[v6].h
In reply to Martin KaFai LauQuoted 9 lines · Martin KaFai Lau
This patch moves the udp[46]_portaddr_hash()
to net/ip[v6].h. The function name is renamed to
ipv[46]_portaddr_hash().It will be used by a later patch which adds a second listener hashtable hashed by the address and port.
Signed-off-by Martin KaFai Lau <[email protected]>---
Re: [PATCH net-next 3/4] inet: Add a 2nd listener hashtable (port+addr)
In reply to Martin KaFai LauQuoted 7 lines · Martin KaFai Lau
The current listener hashtable is hashed by port only. When a process is listening at many IP addresses with the same port (e.g.
[IP1]:443, [IP2]:443... [IPN]:443), the inet[6]_lookup_listener() performance is degraded to a link list. It is prone to syn attack.
...
Quoted 13 lines
include/net/inet_hashtables.h +0
-0@@ -152,8 +152,19 @@ struct inet_hashinfo {*/ struct
inet_listen_hashbucketlistening_hash[INET_LHTABLE_SIZE]
____cacheline_aligned_in_smp;include/net/inet_hashtables.h +2
-0+struct inet_listen_hashbucket *lhash2; +unsigned int lhash2_mask;
Quoted 2 lines
};
Please move these two fields before listening_hash[], to avoid adding another cache line to "struct inet_hashinfo"
( We have a 16-byte hole there, so lets use it ;) )
Re: [PATCH net-next 4/4] tcp: Enable 2nd listener hashtable in TCP
In reply to Martin KaFai LauQuoted 8 lines · Martin KaFai Lau
Enable the second listener hashtable in TCP. The scale is the same as UDP which is one slot per 2MB.
Signed-off-by Martin KaFai Lau <[email protected]>---
net/ipv4/tcp.c | 3 +++1 file changed, 3 insertions(+)
Re: [PATCH net-next 3/4] inet: Add a 2nd listener hashtable (port+addr)
In reply to Eric DumazetQuoted 30 lines · Eric Dumazet
Quoted 7 lines · Martin KaFai Lau
The current listener hashtable is hashed by port only. When a process is listening at many IP addresses with the same port (e.g.
[IP1]:443, [IP2]:443... [IPN]:443), the inet[6]_lookup_listener() performance is degraded to a link list. It is prone to syn attack....
Quoted 13 lines
include/net/inet_hashtables.h +0
-0@@ -152,8 +152,19 @@ struct inet_hashinfo {*/ struct
inet_listen_hashbucketlistening_hash[INET_LHTABLE_SIZE]
____cacheline_aligned_in_smp;include/net/inet_hashtables.h +2
-0+struct inet_listen_hashbucket *lhash2; +unsigned int lhash2_mask;Quoted 2 lines
};
Please move these two fields before listening_hash[], to avoid adding another cache line to "struct
inet_hashinfo"
Quoted 1 line
( We have a 16-byte hole there, so lets use it ;) )
Oops. Indeed. Thanks for pointing it out.