Skip to content

Commit 1946e67

Browse files
Haishuang Yandavem330
authored andcommitted
ipv4: Namespaceify tcp_tw_recycle and tcp_max_tw_buckets knob
Different namespace application might require fast recycling TIME-WAIT sockets independently of the host. Signed-off-by: Haishuang Yan <yanhaishuang@cmss.chinamobile.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 801822d commit 1946e67

12 files changed

Lines changed: 48 additions & 50 deletions

File tree

‎include/net/inet_timewait_sock.h‎

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,6 @@
2929

3030
#include <linux/atomic.h>
3131

32-
struct inet_hashinfo;
33-
34-
struct inet_timewait_death_row {
35-
atomic_t tw_count;
36-
37-
struct inet_hashinfo *hashinfo ____cacheline_aligned_in_smp;
38-
int sysctl_tw_recycle;
39-
int sysctl_max_tw_buckets;
40-
};
41-
4232
struct inet_bind_bucket;
4333

4434
/*
@@ -125,8 +115,7 @@ static inline void inet_twsk_reschedule(struct inet_timewait_sock *tw, int timeo
125115

126116
void inet_twsk_deschedule_put(struct inet_timewait_sock *tw);
127117

128-
void inet_twsk_purge(struct inet_hashinfo *hashinfo,
129-
struct inet_timewait_death_row *twdr, int family);
118+
void inet_twsk_purge(struct inet_hashinfo *hashinfo, int family);
130119

131120
static inline
132121
struct net *twsk_net(const struct inet_timewait_sock *twsk)

‎include/net/netns/ipv4.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ struct ping_group_range {
2727
kgid_t range[2];
2828
};
2929

30+
struct inet_hashinfo;
31+
32+
struct inet_timewait_death_row {
33+
atomic_t tw_count;
34+
35+
struct inet_hashinfo *hashinfo ____cacheline_aligned_in_smp;
36+
int sysctl_tw_recycle;
37+
int sysctl_max_tw_buckets;
38+
};
39+
3040
struct netns_ipv4 {
3141
#ifdef CONFIG_SYSCTL
3242
struct ctl_table_header *forw_hdr;
@@ -111,6 +121,7 @@ struct netns_ipv4 {
111121
int sysctl_tcp_fin_timeout;
112122
unsigned int sysctl_tcp_notsent_lowat;
113123
int sysctl_tcp_tw_reuse;
124+
struct inet_timewait_death_row tcp_death_row;
114125

115126
int sysctl_igmp_max_memberships;
116127
int sysctl_igmp_max_msf;

‎include/net/tcp.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,6 @@ void tcp_time_wait(struct sock *sk, int state, int timeo);
231231
*/
232232
#define TFO_SERVER_WO_SOCKOPT1 0x400
233233

234-
extern struct inet_timewait_death_row tcp_death_row;
235234

236235
/* sysctl variables for tcp */
237236
extern int sysctl_tcp_timestamps;

‎net/ipv4/af_inet.c‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1831,8 +1831,6 @@ static int __init inet_init(void)
18311831

18321832
ip_init();
18331833

1834-
tcp_v4_init();
1835-
18361834
/* Setup TCP slab cache for open requests. */
18371835
tcp_init();
18381836

‎net/ipv4/inet_timewait_sock.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,7 @@ void __inet_twsk_schedule(struct inet_timewait_sock *tw, int timeo, bool rearm)
257257
}
258258
EXPORT_SYMBOL_GPL(__inet_twsk_schedule);
259259

260-
void inet_twsk_purge(struct inet_hashinfo *hashinfo,
261-
struct inet_timewait_death_row *twdr, int family)
260+
void inet_twsk_purge(struct inet_hashinfo *hashinfo, int family)
262261
{
263262
struct inet_timewait_sock *tw;
264263
struct sock *sk;

‎net/ipv4/proc.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ static int sockstat_seq_show(struct seq_file *seq, void *v)
6565
socket_seq_show(seq);
6666
seq_printf(seq, "TCP: inuse %d orphan %d tw %d alloc %d mem %ld\n",
6767
sock_prot_inuse_get(net, &tcp_prot), orphans,
68-
atomic_read(&tcp_death_row.tw_count), sockets,
68+
atomic_read(&net->ipv4.tcp_death_row.tw_count), sockets,
6969
proto_memory_allocated(&tcp_prot));
7070
seq_printf(seq, "UDP: inuse %d mem %ld\n",
7171
sock_prot_inuse_get(net, &udp_prot),

‎net/ipv4/sysctl_net_ipv4.c‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -289,13 +289,6 @@ static struct ctl_table ipv4_table[] = {
289289
.mode = 0644,
290290
.proc_handler = proc_dointvec
291291
},
292-
{
293-
.procname = "tcp_max_tw_buckets",
294-
.data = &tcp_death_row.sysctl_max_tw_buckets,
295-
.maxlen = sizeof(int),
296-
.mode = 0644,
297-
.proc_handler = proc_dointvec
298-
},
299292
{
300293
.procname = "tcp_fastopen",
301294
.data = &sysctl_tcp_fastopen,
@@ -309,13 +302,6 @@ static struct ctl_table ipv4_table[] = {
309302
.maxlen = ((TCP_FASTOPEN_KEY_LENGTH * 2) + 10),
310303
.proc_handler = proc_tcp_fastopen_key,
311304
},
312-
{
313-
.procname = "tcp_tw_recycle",
314-
.data = &tcp_death_row.sysctl_tw_recycle,
315-
.maxlen = sizeof(int),
316-
.mode = 0644,
317-
.proc_handler = proc_dointvec
318-
},
319305
{
320306
.procname = "tcp_abort_on_overflow",
321307
.data = &sysctl_tcp_abort_on_overflow,
@@ -960,6 +946,20 @@ static struct ctl_table ipv4_net_table[] = {
960946
.mode = 0644,
961947
.proc_handler = proc_dointvec
962948
},
949+
{
950+
.procname = "tcp_max_tw_buckets",
951+
.data = &init_net.ipv4.tcp_death_row.sysctl_max_tw_buckets,
952+
.maxlen = sizeof(int),
953+
.mode = 0644,
954+
.proc_handler = proc_dointvec
955+
},
956+
{
957+
.procname = "tcp_tw_recycle",
958+
.data = &init_net.ipv4.tcp_death_row.sysctl_tw_recycle,
959+
.maxlen = sizeof(int),
960+
.mode = 0644,
961+
.proc_handler = proc_dointvec
962+
},
963963
#ifdef CONFIG_IP_ROUTE_MULTIPATH
964964
{
965965
.procname = "fib_multipath_use_neigh",

‎net/ipv4/tcp.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3334,6 +3334,7 @@ void __init tcp_init(void)
33343334

33353335
percpu_counter_init(&tcp_sockets_allocated, 0, GFP_KERNEL);
33363336
percpu_counter_init(&tcp_orphan_count, 0, GFP_KERNEL);
3337+
inet_hashinfo_init(&tcp_hashinfo);
33373338
tcp_hashinfo.bind_bucket_cachep =
33383339
kmem_cache_create("tcp_bind_bucket",
33393340
sizeof(struct inet_bind_bucket), 0,
@@ -3378,7 +3379,6 @@ void __init tcp_init(void)
33783379

33793380
cnt = tcp_hashinfo.ehash_mask + 1;
33803381

3381-
tcp_death_row.sysctl_max_tw_buckets = cnt / 2;
33823382
sysctl_tcp_max_orphans = cnt / 2;
33833383
sysctl_max_syn_backlog = max(128, cnt / 256);
33843384

@@ -3399,6 +3399,7 @@ void __init tcp_init(void)
33993399
pr_info("Hash tables configured (established %u bind %u)\n",
34003400
tcp_hashinfo.ehash_mask + 1, tcp_hashinfo.bhash_size);
34013401

3402+
tcp_v4_init();
34023403
tcp_metrics_init();
34033404
BUG_ON(tcp_register_congestion_control(&tcp_reno) != 0);
34043405
tcp_tasklet_init();

‎net/ipv4/tcp_input.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6363,7 +6363,7 @@ int tcp_conn_request(struct request_sock_ops *rsk_ops,
63636363
* timewait bucket, so that all the necessary checks
63646364
* are made in the function processing timewait state.
63656365
*/
6366-
if (tcp_death_row.sysctl_tw_recycle) {
6366+
if (net->ipv4.tcp_death_row.sysctl_tw_recycle) {
63676367
bool strict;
63686368

63696369
dst = af_ops->route_req(sk, &fl, req, &strict);

‎net/ipv4/tcp_ipv4.c‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
146146
struct rtable *rt;
147147
int err;
148148
struct ip_options_rcu *inet_opt;
149+
struct inet_timewait_death_row *tcp_death_row = &sock_net(sk)->ipv4.tcp_death_row;
149150

150151
if (addr_len < sizeof(struct sockaddr_in))
151152
return -EINVAL;
@@ -196,7 +197,7 @@ int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
196197
tp->write_seq = 0;
197198
}
198199

199-
if (tcp_death_row.sysctl_tw_recycle &&
200+
if (tcp_death_row->sysctl_tw_recycle &&
200201
!tp->rx_opt.ts_recent_stamp && fl4->daddr == daddr)
201202
tcp_fetch_timewait_stamp(sk, &rt->dst);
202203

@@ -215,7 +216,7 @@ int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
215216
* complete initialization after this.
216217
*/
217218
tcp_set_state(sk, TCP_SYN_SENT);
218-
err = inet_hash_connect(&tcp_death_row, sk);
219+
err = inet_hash_connect(tcp_death_row, sk);
219220
if (err)
220221
goto failure;
221222

@@ -2457,6 +2458,10 @@ static int __net_init tcp_sk_init(struct net *net)
24572458
net->ipv4.sysctl_tcp_notsent_lowat = UINT_MAX;
24582459
net->ipv4.sysctl_tcp_tw_reuse = 0;
24592460

2461+
net->ipv4.tcp_death_row.sysctl_tw_recycle = 0;
2462+
net->ipv4.tcp_death_row.sysctl_max_tw_buckets = (tcp_hashinfo.ehash_mask + 1) / 2;
2463+
net->ipv4.tcp_death_row.hashinfo = &tcp_hashinfo;
2464+
24602465
return 0;
24612466
fail:
24622467
tcp_sk_exit(net);
@@ -2466,7 +2471,7 @@ static int __net_init tcp_sk_init(struct net *net)
24662471

24672472
static void __net_exit tcp_sk_exit_batch(struct list_head *net_exit_list)
24682473
{
2469-
inet_twsk_purge(&tcp_hashinfo, &tcp_death_row, AF_INET);
2474+
inet_twsk_purge(&tcp_hashinfo, AF_INET);
24702475
}
24712476

24722477
static struct pernet_operations __net_initdata tcp_sk_ops = {
@@ -2477,7 +2482,6 @@ static struct pernet_operations __net_initdata tcp_sk_ops = {
24772482

24782483
void __init tcp_v4_init(void)
24792484
{
2480-
inet_hashinfo_init(&tcp_hashinfo);
24812485
if (register_pernet_subsys(&tcp_sk_ops))
24822486
panic("Failed to create the TCP control socket.\n");
24832487
}

0 commit comments

Comments
 (0)