2 * sysctl_net_core.c: sysctl interface to net core subsystem.
4 * Begun April 1, 1996, Mike Shaver.
5 * Added /proc/sys/net/core directory entry (empty =) ). [MS]
9 #include <linux/sysctl.h>
10 #include <linux/module.h>
11 #include <linux/socket.h>
12 #include <linux/netdevice.h>
13 #include <linux/ratelimit.h>
14 #include <linux/vmalloc.h>
15 #include <linux/init.h>
16 #include <linux/slab.h>
17 #include <linux/kmemleak.h>
21 #include <net/net_ratelimit.h>
22 #include <net/busy_poll.h>
26 static int ushort_max
= USHRT_MAX
;
29 static int rps_sock_flow_sysctl(struct ctl_table
*table
, int write
,
30 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
32 unsigned int orig_size
, size
;
34 struct ctl_table tmp
= {
36 .maxlen
= sizeof(size
),
39 struct rps_sock_flow_table
*orig_sock_table
, *sock_table
;
40 static DEFINE_MUTEX(sock_flow_mutex
);
42 mutex_lock(&sock_flow_mutex
);
44 orig_sock_table
= rcu_dereference_protected(rps_sock_flow_table
,
45 lockdep_is_held(&sock_flow_mutex
));
46 size
= orig_size
= orig_sock_table
? orig_sock_table
->mask
+ 1 : 0;
48 ret
= proc_dointvec(&tmp
, write
, buffer
, lenp
, ppos
);
53 /* Enforce limit to prevent overflow */
54 mutex_unlock(&sock_flow_mutex
);
57 size
= roundup_pow_of_two(size
);
58 if (size
!= orig_size
) {
60 vmalloc(RPS_SOCK_FLOW_TABLE_SIZE(size
));
62 mutex_unlock(&sock_flow_mutex
);
66 sock_table
->mask
= size
- 1;
68 sock_table
= orig_sock_table
;
70 for (i
= 0; i
< size
; i
++)
71 sock_table
->ents
[i
] = RPS_NO_CPU
;
75 if (sock_table
!= orig_sock_table
) {
76 rcu_assign_pointer(rps_sock_flow_table
, sock_table
);
78 static_key_slow_inc(&rps_needed
);
79 if (orig_sock_table
) {
80 static_key_slow_dec(&rps_needed
);
82 vfree(orig_sock_table
);
87 mutex_unlock(&sock_flow_mutex
);
91 #endif /* CONFIG_RPS */
93 #ifdef CONFIG_NET_FLOW_LIMIT
94 static DEFINE_MUTEX(flow_limit_update_mutex
);
96 static int flow_limit_cpu_sysctl(struct ctl_table
*table
, int write
,
97 void __user
*buffer
, size_t *lenp
,
100 struct sd_flow_limit
*cur
;
101 struct softnet_data
*sd
;
105 if (!alloc_cpumask_var(&mask
, GFP_KERNEL
))
109 ret
= cpumask_parse_user(buffer
, *lenp
, mask
);
113 mutex_lock(&flow_limit_update_mutex
);
114 len
= sizeof(*cur
) + netdev_flow_limit_table_len
;
115 for_each_possible_cpu(i
) {
116 sd
= &per_cpu(softnet_data
, i
);
117 cur
= rcu_dereference_protected(sd
->flow_limit
,
118 lockdep_is_held(&flow_limit_update_mutex
));
119 if (cur
&& !cpumask_test_cpu(i
, mask
)) {
120 RCU_INIT_POINTER(sd
->flow_limit
, NULL
);
123 } else if (!cur
&& cpumask_test_cpu(i
, mask
)) {
124 cur
= kzalloc(len
, GFP_KERNEL
);
126 /* not unwinding previous changes */
130 cur
->num_buckets
= netdev_flow_limit_table_len
;
131 rcu_assign_pointer(sd
->flow_limit
, cur
);
135 mutex_unlock(&flow_limit_update_mutex
);
139 if (*ppos
|| !*lenp
) {
146 for_each_possible_cpu(i
) {
147 sd
= &per_cpu(softnet_data
, i
);
148 if (rcu_dereference(sd
->flow_limit
))
149 cpumask_set_cpu(i
, mask
);
153 len
= min(sizeof(kbuf
) - 1, *lenp
);
154 len
= cpumask_scnprintf(kbuf
, len
, mask
);
161 if (copy_to_user(buffer
, kbuf
, len
)) {
170 free_cpumask_var(mask
);
174 static int flow_limit_table_len_sysctl(struct ctl_table
*table
, int write
,
175 void __user
*buffer
, size_t *lenp
,
178 unsigned int old
, *ptr
;
181 mutex_lock(&flow_limit_update_mutex
);
185 ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
186 if (!ret
&& write
&& !is_power_of_2(*ptr
)) {
191 mutex_unlock(&flow_limit_update_mutex
);
194 #endif /* CONFIG_NET_FLOW_LIMIT */
196 static struct ctl_table net_core_table
[] = {
199 .procname
= "wmem_max",
200 .data
= &sysctl_wmem_max
,
201 .maxlen
= sizeof(int),
203 .proc_handler
= proc_dointvec_minmax
,
207 .procname
= "rmem_max",
208 .data
= &sysctl_rmem_max
,
209 .maxlen
= sizeof(int),
211 .proc_handler
= proc_dointvec_minmax
,
215 .procname
= "wmem_default",
216 .data
= &sysctl_wmem_default
,
217 .maxlen
= sizeof(int),
219 .proc_handler
= proc_dointvec_minmax
,
223 .procname
= "rmem_default",
224 .data
= &sysctl_rmem_default
,
225 .maxlen
= sizeof(int),
227 .proc_handler
= proc_dointvec_minmax
,
231 .procname
= "dev_weight",
233 .maxlen
= sizeof(int),
235 .proc_handler
= proc_dointvec
238 .procname
= "netdev_max_backlog",
239 .data
= &netdev_max_backlog
,
240 .maxlen
= sizeof(int),
242 .proc_handler
= proc_dointvec
244 #ifdef CONFIG_BPF_JIT
246 .procname
= "bpf_jit_enable",
247 .data
= &bpf_jit_enable
,
248 .maxlen
= sizeof(int),
250 .proc_handler
= proc_dointvec
254 .procname
= "netdev_tstamp_prequeue",
255 .data
= &netdev_tstamp_prequeue
,
256 .maxlen
= sizeof(int),
258 .proc_handler
= proc_dointvec
261 .procname
= "message_cost",
262 .data
= &net_ratelimit_state
.interval
,
263 .maxlen
= sizeof(int),
265 .proc_handler
= proc_dointvec_jiffies
,
268 .procname
= "message_burst",
269 .data
= &net_ratelimit_state
.burst
,
270 .maxlen
= sizeof(int),
272 .proc_handler
= proc_dointvec
,
275 .procname
= "optmem_max",
276 .data
= &sysctl_optmem_max
,
277 .maxlen
= sizeof(int),
279 .proc_handler
= proc_dointvec
283 .procname
= "rps_sock_flow_entries",
284 .maxlen
= sizeof(int),
286 .proc_handler
= rps_sock_flow_sysctl
289 #ifdef CONFIG_NET_FLOW_LIMIT
291 .procname
= "flow_limit_cpu_bitmap",
293 .proc_handler
= flow_limit_cpu_sysctl
296 .procname
= "flow_limit_table_len",
297 .data
= &netdev_flow_limit_table_len
,
298 .maxlen
= sizeof(int),
300 .proc_handler
= flow_limit_table_len_sysctl
302 #endif /* CONFIG_NET_FLOW_LIMIT */
303 #ifdef CONFIG_NET_RX_BUSY_POLL
305 .procname
= "busy_poll",
306 .data
= &sysctl_net_busy_poll
,
307 .maxlen
= sizeof(unsigned int),
309 .proc_handler
= proc_dointvec
312 .procname
= "busy_read",
313 .data
= &sysctl_net_busy_read
,
314 .maxlen
= sizeof(unsigned int),
316 .proc_handler
= proc_dointvec
320 #endif /* CONFIG_NET */
322 .procname
= "netdev_budget",
323 .data
= &netdev_budget
,
324 .maxlen
= sizeof(int),
326 .proc_handler
= proc_dointvec
329 .procname
= "warnings",
330 .data
= &net_msg_warn
,
331 .maxlen
= sizeof(int),
333 .proc_handler
= proc_dointvec
338 static struct ctl_table netns_core_table
[] = {
340 .procname
= "somaxconn",
341 .data
= &init_net
.core
.sysctl_somaxconn
,
342 .maxlen
= sizeof(int),
345 .extra2
= &ushort_max
,
346 .proc_handler
= proc_dointvec_minmax
351 static __net_init
int sysctl_core_net_init(struct net
*net
)
353 struct ctl_table
*tbl
;
355 net
->core
.sysctl_somaxconn
= SOMAXCONN
;
357 tbl
= netns_core_table
;
358 if (!net_eq(net
, &init_net
)) {
359 tbl
= kmemdup(tbl
, sizeof(netns_core_table
), GFP_KERNEL
);
363 tbl
[0].data
= &net
->core
.sysctl_somaxconn
;
365 /* Don't export any sysctls to unprivileged users */
366 if (net
->user_ns
!= &init_user_ns
) {
367 tbl
[0].procname
= NULL
;
371 net
->core
.sysctl_hdr
= register_net_sysctl(net
, "net/core", tbl
);
372 if (net
->core
.sysctl_hdr
== NULL
)
378 if (tbl
!= netns_core_table
)
384 static __net_exit
void sysctl_core_net_exit(struct net
*net
)
386 struct ctl_table
*tbl
;
388 tbl
= net
->core
.sysctl_hdr
->ctl_table_arg
;
389 unregister_net_sysctl_table(net
->core
.sysctl_hdr
);
390 BUG_ON(tbl
== netns_core_table
);
394 static __net_initdata
struct pernet_operations sysctl_core_ops
= {
395 .init
= sysctl_core_net_init
,
396 .exit
= sysctl_core_net_exit
,
399 static __init
int sysctl_core_init(void)
401 register_net_sysctl(&init_net
, "net/core", net_core_table
);
402 return register_pernet_subsys(&sysctl_core_ops
);
405 fs_initcall(sysctl_core_init
);