Skip to content

Commit e8f57a7

Browse files
committed
Merge branch 'tcp-even-faster-connect-under-stress'
Eric Dumazet says: ==================== tcp: even faster connect() under stress This is a followup on the prior series, "tcp: scale connect() under pressure" Now spinlocks are no longer in the picture, we see a very high cost of the inet6_ehashfn() function. In this series (of 2), I change how lport contributes to inet6_ehashfn() to ensure better cache locality and call inet6_ehashfn() only once per connect() system call. This brings an additional 229 % increase of performance for "neper/tcp_crr -6 -T 200 -F 30000" stress test, while greatly improving latency metrics. Before: latency_min=0.014131929 latency_max=17.895073144 latency_mean=0.505675853 latency_stddev=2.125164772 num_samples=307884 throughput=139866.80 After: latency_min=0.003041375 latency_max=7.056589232 latency_mean=0.141075048 latency_stddev=0.526900516 num_samples=312996 throughput=320677.21 ==================== Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
2 parents f8ece40 + d4438ce commit e8f57a7

File tree

4 files changed

+37
-18
lines changed

4 files changed

+37
-18
lines changed

include/net/inet_hashtables.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,10 +527,12 @@ static inline void sk_rcv_saddr_set(struct sock *sk, __be32 addr)
527527

528528
int __inet_hash_connect(struct inet_timewait_death_row *death_row,
529529
struct sock *sk, u64 port_offset,
530+
u32 hash_port0,
530531
int (*check_established)(struct inet_timewait_death_row *,
531532
struct sock *, __u16,
532533
struct inet_timewait_sock **,
533-
bool rcu_lookup));
534+
bool rcu_lookup,
535+
u32 hash));
534536

535537
int inet_hash_connect(struct inet_timewait_death_row *death_row,
536538
struct sock *sk);

include/net/ip.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ static inline void inet_get_local_port_range(const struct net *net, int *low, in
357357
bool inet_sk_get_local_port_range(const struct sock *sk, int *low, int *high);
358358

359359
#ifdef CONFIG_SYSCTL
360-
static inline bool inet_is_local_reserved_port(struct net *net, unsigned short port)
360+
static inline bool inet_is_local_reserved_port(const struct net *net, unsigned short port)
361361
{
362362
if (!net->ipv4.sysctl_local_reserved_ports)
363363
return false;

net/ipv4/inet_hashtables.c

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ u32 inet_ehashfn(const struct net *net, const __be32 laddr,
3535
{
3636
net_get_random_once(&inet_ehash_secret, sizeof(inet_ehash_secret));
3737

38-
return __inet_ehashfn(laddr, lport, faddr, fport,
39-
inet_ehash_secret + net_hash_mix(net));
38+
return lport + __inet_ehashfn(laddr, 0, faddr, fport,
39+
inet_ehash_secret + net_hash_mix(net));
4040
}
4141
EXPORT_SYMBOL_GPL(inet_ehashfn);
4242

@@ -538,7 +538,8 @@ EXPORT_SYMBOL_GPL(__inet_lookup_established);
538538
static int __inet_check_established(struct inet_timewait_death_row *death_row,
539539
struct sock *sk, __u16 lport,
540540
struct inet_timewait_sock **twp,
541-
bool rcu_lookup)
541+
bool rcu_lookup,
542+
u32 hash)
542543
{
543544
struct inet_hashinfo *hinfo = death_row->hashinfo;
544545
struct inet_sock *inet = inet_sk(sk);
@@ -549,8 +550,6 @@ static int __inet_check_established(struct inet_timewait_death_row *death_row,
549550
int sdif = l3mdev_master_ifindex_by_index(net, dif);
550551
INET_ADDR_COOKIE(acookie, saddr, daddr);
551552
const __portpair ports = INET_COMBINED_PORTS(inet->inet_dport, lport);
552-
unsigned int hash = inet_ehashfn(net, daddr, lport,
553-
saddr, inet->inet_dport);
554553
struct inet_ehash_bucket *head = inet_ehash_bucket(hinfo, hash);
555554
struct inet_timewait_sock *tw = NULL;
556555
const struct hlist_nulls_node *node;
@@ -1007,9 +1006,10 @@ static u32 *table_perturb;
10071006

10081007
int __inet_hash_connect(struct inet_timewait_death_row *death_row,
10091008
struct sock *sk, u64 port_offset,
1009+
u32 hash_port0,
10101010
int (*check_established)(struct inet_timewait_death_row *,
10111011
struct sock *, __u16, struct inet_timewait_sock **,
1012-
bool rcu_lookup))
1012+
bool rcu_lookup, u32 hash))
10131013
{
10141014
struct inet_hashinfo *hinfo = death_row->hashinfo;
10151015
struct inet_bind_hashbucket *head, *head2;
@@ -1027,7 +1027,8 @@ int __inet_hash_connect(struct inet_timewait_death_row *death_row,
10271027

10281028
if (port) {
10291029
local_bh_disable();
1030-
ret = check_established(death_row, sk, port, NULL, false);
1030+
ret = check_established(death_row, sk, port, NULL, false,
1031+
hash_port0 + port);
10311032
local_bh_enable();
10321033
return ret;
10331034
}
@@ -1071,7 +1072,8 @@ int __inet_hash_connect(struct inet_timewait_death_row *death_row,
10711072
rcu_read_unlock();
10721073
goto next_port;
10731074
}
1074-
if (!check_established(death_row, sk, port, &tw, true))
1075+
if (!check_established(death_row, sk, port, &tw, true,
1076+
hash_port0 + port))
10751077
break;
10761078
rcu_read_unlock();
10771079
goto next_port;
@@ -1090,7 +1092,8 @@ int __inet_hash_connect(struct inet_timewait_death_row *death_row,
10901092
goto next_port_unlock;
10911093
WARN_ON(hlist_empty(&tb->bhash2));
10921094
if (!check_established(death_row, sk,
1093-
port, &tw, false))
1095+
port, &tw, false,
1096+
hash_port0 + port))
10941097
goto ok;
10951098
goto next_port_unlock;
10961099
}
@@ -1197,11 +1200,18 @@ int __inet_hash_connect(struct inet_timewait_death_row *death_row,
11971200
int inet_hash_connect(struct inet_timewait_death_row *death_row,
11981201
struct sock *sk)
11991202
{
1203+
const struct inet_sock *inet = inet_sk(sk);
1204+
const struct net *net = sock_net(sk);
12001205
u64 port_offset = 0;
1206+
u32 hash_port0;
12011207

12021208
if (!inet_sk(sk)->inet_num)
12031209
port_offset = inet_sk_port_offset(sk);
1204-
return __inet_hash_connect(death_row, sk, port_offset,
1210+
1211+
hash_port0 = inet_ehashfn(net, inet->inet_rcv_saddr, 0,
1212+
inet->inet_daddr, inet->inet_dport);
1213+
1214+
return __inet_hash_connect(death_row, sk, port_offset, hash_port0,
12051215
__inet_check_established);
12061216
}
12071217
EXPORT_SYMBOL_GPL(inet_hash_connect);

net/ipv6/inet6_hashtables.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ u32 inet6_ehashfn(const struct net *net,
3535
lhash = (__force u32)laddr->s6_addr32[3];
3636
fhash = __ipv6_addr_jhash(faddr, tcp_ipv6_hash_secret);
3737

38-
return __inet6_ehashfn(lhash, lport, fhash, fport,
39-
inet6_ehash_secret + net_hash_mix(net));
38+
return lport + __inet6_ehashfn(lhash, 0, fhash, fport,
39+
inet6_ehash_secret + net_hash_mix(net));
4040
}
4141
EXPORT_SYMBOL_GPL(inet6_ehashfn);
4242

@@ -264,7 +264,8 @@ EXPORT_SYMBOL_GPL(inet6_lookup);
264264
static int __inet6_check_established(struct inet_timewait_death_row *death_row,
265265
struct sock *sk, const __u16 lport,
266266
struct inet_timewait_sock **twp,
267-
bool rcu_lookup)
267+
bool rcu_lookup,
268+
u32 hash)
268269
{
269270
struct inet_hashinfo *hinfo = death_row->hashinfo;
270271
struct inet_sock *inet = inet_sk(sk);
@@ -274,8 +275,6 @@ static int __inet6_check_established(struct inet_timewait_death_row *death_row,
274275
struct net *net = sock_net(sk);
275276
const int sdif = l3mdev_master_ifindex_by_index(net, dif);
276277
const __portpair ports = INET_COMBINED_PORTS(inet->inet_dport, lport);
277-
const unsigned int hash = inet6_ehashfn(net, daddr, lport, saddr,
278-
inet->inet_dport);
279278
struct inet_ehash_bucket *head = inet_ehash_bucket(hinfo, hash);
280279
struct inet_timewait_sock *tw = NULL;
281280
const struct hlist_nulls_node *node;
@@ -354,11 +353,19 @@ static u64 inet6_sk_port_offset(const struct sock *sk)
354353
int inet6_hash_connect(struct inet_timewait_death_row *death_row,
355354
struct sock *sk)
356355
{
356+
const struct in6_addr *daddr = &sk->sk_v6_rcv_saddr;
357+
const struct in6_addr *saddr = &sk->sk_v6_daddr;
358+
const struct inet_sock *inet = inet_sk(sk);
359+
const struct net *net = sock_net(sk);
357360
u64 port_offset = 0;
361+
u32 hash_port0;
358362

359363
if (!inet_sk(sk)->inet_num)
360364
port_offset = inet6_sk_port_offset(sk);
361-
return __inet_hash_connect(death_row, sk, port_offset,
365+
366+
hash_port0 = inet6_ehashfn(net, daddr, 0, saddr, inet->inet_dport);
367+
368+
return __inet_hash_connect(death_row, sk, port_offset, hash_port0,
362369
__inet6_check_established);
363370
}
364371
EXPORT_SYMBOL_GPL(inet6_hash_connect);

0 commit comments

Comments
 (0)