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>
23 #include <net/pkt_sched.h>
27 static int ushort_max
= USHRT_MAX
;
28 static int min_sndbuf
= SOCK_MIN_SNDBUF
;
29 static int min_rcvbuf
= SOCK_MIN_RCVBUF
;
32 static int rps_sock_flow_sysctl(struct ctl_table
*table
, int write
,
33 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
35 unsigned int orig_size
, size
;
37 struct ctl_table tmp
= {
39 .maxlen
= sizeof(size
),
42 struct rps_sock_flow_table
*orig_sock_table
, *sock_table
;
43 static DEFINE_MUTEX(sock_flow_mutex
);
45 mutex_lock(&sock_flow_mutex
);
47 orig_sock_table
= rcu_dereference_protected(rps_sock_flow_table
,
48 lockdep_is_held(&sock_flow_mutex
));
49 size
= orig_size
= orig_sock_table
? orig_sock_table
->mask
+ 1 : 0;
51 ret
= proc_dointvec(&tmp
, write
, buffer
, lenp
, ppos
);
56 /* Enforce limit to prevent overflow */
57 mutex_unlock(&sock_flow_mutex
);
60 size
= roundup_pow_of_two(size
);
61 if (size
!= orig_size
) {
63 vmalloc(RPS_SOCK_FLOW_TABLE_SIZE(size
));
65 mutex_unlock(&sock_flow_mutex
);
69 sock_table
->mask
= size
- 1;
71 sock_table
= orig_sock_table
;
73 for (i
= 0; i
< size
; i
++)
74 sock_table
->ents
[i
] = RPS_NO_CPU
;
78 if (sock_table
!= orig_sock_table
) {
79 rcu_assign_pointer(rps_sock_flow_table
, sock_table
);
81 static_key_slow_inc(&rps_needed
);
82 if (orig_sock_table
) {
83 static_key_slow_dec(&rps_needed
);
85 vfree(orig_sock_table
);
90 mutex_unlock(&sock_flow_mutex
);
94 #endif /* CONFIG_RPS */
96 #ifdef CONFIG_NET_FLOW_LIMIT
97 static DEFINE_MUTEX(flow_limit_update_mutex
);
99 static int flow_limit_cpu_sysctl(struct ctl_table
*table
, int write
,
100 void __user
*buffer
, size_t *lenp
,
103 struct sd_flow_limit
*cur
;
104 struct softnet_data
*sd
;
108 if (!alloc_cpumask_var(&mask
, GFP_KERNEL
))
112 ret
= cpumask_parse_user(buffer
, *lenp
, mask
);
116 mutex_lock(&flow_limit_update_mutex
);
117 len
= sizeof(*cur
) + netdev_flow_limit_table_len
;
118 for_each_possible_cpu(i
) {
119 sd
= &per_cpu(softnet_data
, i
);
120 cur
= rcu_dereference_protected(sd
->flow_limit
,
121 lockdep_is_held(&flow_limit_update_mutex
));
122 if (cur
&& !cpumask_test_cpu(i
, mask
)) {
123 RCU_INIT_POINTER(sd
->flow_limit
, NULL
);
126 } else if (!cur
&& cpumask_test_cpu(i
, mask
)) {
127 cur
= kzalloc_node(len
, GFP_KERNEL
,
130 /* not unwinding previous changes */
134 cur
->num_buckets
= netdev_flow_limit_table_len
;
135 rcu_assign_pointer(sd
->flow_limit
, cur
);
139 mutex_unlock(&flow_limit_update_mutex
);
143 if (*ppos
|| !*lenp
) {
150 for_each_possible_cpu(i
) {
151 sd
= &per_cpu(softnet_data
, i
);
152 if (rcu_dereference(sd
->flow_limit
))
153 cpumask_set_cpu(i
, mask
);
157 len
= min(sizeof(kbuf
) - 1, *lenp
);
158 len
= cpumask_scnprintf(kbuf
, len
, mask
);
165 if (copy_to_user(buffer
, kbuf
, len
)) {
174 free_cpumask_var(mask
);
178 static int flow_limit_table_len_sysctl(struct ctl_table
*table
, int write
,
179 void __user
*buffer
, size_t *lenp
,
182 unsigned int old
, *ptr
;
185 mutex_lock(&flow_limit_update_mutex
);
189 ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
190 if (!ret
&& write
&& !is_power_of_2(*ptr
)) {
195 mutex_unlock(&flow_limit_update_mutex
);
198 #endif /* CONFIG_NET_FLOW_LIMIT */
200 #ifdef CONFIG_NET_SCHED
201 static int set_default_qdisc(struct ctl_table
*table
, int write
,
202 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
205 struct ctl_table tbl
= {
211 qdisc_get_default(id
, IFNAMSIZ
);
213 ret
= proc_dostring(&tbl
, write
, buffer
, lenp
, ppos
);
214 if (write
&& ret
== 0)
215 ret
= qdisc_set_default(id
);
220 static struct ctl_table net_core_table
[] = {
223 .procname
= "wmem_max",
224 .data
= &sysctl_wmem_max
,
225 .maxlen
= sizeof(int),
227 .proc_handler
= proc_dointvec_minmax
,
228 .extra1
= &min_sndbuf
,
231 .procname
= "rmem_max",
232 .data
= &sysctl_rmem_max
,
233 .maxlen
= sizeof(int),
235 .proc_handler
= proc_dointvec_minmax
,
236 .extra1
= &min_rcvbuf
,
239 .procname
= "wmem_default",
240 .data
= &sysctl_wmem_default
,
241 .maxlen
= sizeof(int),
243 .proc_handler
= proc_dointvec_minmax
,
244 .extra1
= &min_sndbuf
,
247 .procname
= "rmem_default",
248 .data
= &sysctl_rmem_default
,
249 .maxlen
= sizeof(int),
251 .proc_handler
= proc_dointvec_minmax
,
252 .extra1
= &min_rcvbuf
,
255 .procname
= "dev_weight",
257 .maxlen
= sizeof(int),
259 .proc_handler
= proc_dointvec
262 .procname
= "netdev_max_backlog",
263 .data
= &netdev_max_backlog
,
264 .maxlen
= sizeof(int),
266 .proc_handler
= proc_dointvec
268 #ifdef CONFIG_BPF_JIT
270 .procname
= "bpf_jit_enable",
271 .data
= &bpf_jit_enable
,
272 .maxlen
= sizeof(int),
274 .proc_handler
= proc_dointvec
278 .procname
= "netdev_tstamp_prequeue",
279 .data
= &netdev_tstamp_prequeue
,
280 .maxlen
= sizeof(int),
282 .proc_handler
= proc_dointvec
285 .procname
= "message_cost",
286 .data
= &net_ratelimit_state
.interval
,
287 .maxlen
= sizeof(int),
289 .proc_handler
= proc_dointvec_jiffies
,
292 .procname
= "message_burst",
293 .data
= &net_ratelimit_state
.burst
,
294 .maxlen
= sizeof(int),
296 .proc_handler
= proc_dointvec
,
299 .procname
= "optmem_max",
300 .data
= &sysctl_optmem_max
,
301 .maxlen
= sizeof(int),
303 .proc_handler
= proc_dointvec
307 .procname
= "rps_sock_flow_entries",
308 .maxlen
= sizeof(int),
310 .proc_handler
= rps_sock_flow_sysctl
313 #ifdef CONFIG_NET_FLOW_LIMIT
315 .procname
= "flow_limit_cpu_bitmap",
317 .proc_handler
= flow_limit_cpu_sysctl
320 .procname
= "flow_limit_table_len",
321 .data
= &netdev_flow_limit_table_len
,
322 .maxlen
= sizeof(int),
324 .proc_handler
= flow_limit_table_len_sysctl
326 #endif /* CONFIG_NET_FLOW_LIMIT */
327 #ifdef CONFIG_NET_RX_BUSY_POLL
329 .procname
= "busy_poll",
330 .data
= &sysctl_net_busy_poll
,
331 .maxlen
= sizeof(unsigned int),
333 .proc_handler
= proc_dointvec
336 .procname
= "busy_read",
337 .data
= &sysctl_net_busy_read
,
338 .maxlen
= sizeof(unsigned int),
340 .proc_handler
= proc_dointvec
343 #ifdef CONFIG_NET_SCHED
345 .procname
= "default_qdisc",
348 .proc_handler
= set_default_qdisc
351 #endif /* CONFIG_NET */
353 .procname
= "netdev_budget",
354 .data
= &netdev_budget
,
355 .maxlen
= sizeof(int),
357 .proc_handler
= proc_dointvec
360 .procname
= "warnings",
361 .data
= &net_msg_warn
,
362 .maxlen
= sizeof(int),
364 .proc_handler
= proc_dointvec
369 static struct ctl_table netns_core_table
[] = {
371 .procname
= "somaxconn",
372 .data
= &init_net
.core
.sysctl_somaxconn
,
373 .maxlen
= sizeof(int),
376 .extra2
= &ushort_max
,
377 .proc_handler
= proc_dointvec_minmax
382 static __net_init
int sysctl_core_net_init(struct net
*net
)
384 struct ctl_table
*tbl
;
386 net
->core
.sysctl_somaxconn
= SOMAXCONN
;
388 tbl
= netns_core_table
;
389 if (!net_eq(net
, &init_net
)) {
390 tbl
= kmemdup(tbl
, sizeof(netns_core_table
), GFP_KERNEL
);
394 tbl
[0].data
= &net
->core
.sysctl_somaxconn
;
396 /* Don't export any sysctls to unprivileged users */
397 if (net
->user_ns
!= &init_user_ns
) {
398 tbl
[0].procname
= NULL
;
402 net
->core
.sysctl_hdr
= register_net_sysctl(net
, "net/core", tbl
);
403 if (net
->core
.sysctl_hdr
== NULL
)
409 if (tbl
!= netns_core_table
)
415 static __net_exit
void sysctl_core_net_exit(struct net
*net
)
417 struct ctl_table
*tbl
;
419 tbl
= net
->core
.sysctl_hdr
->ctl_table_arg
;
420 unregister_net_sysctl_table(net
->core
.sysctl_hdr
);
421 BUG_ON(tbl
== netns_core_table
);
425 static __net_initdata
struct pernet_operations sysctl_core_ops
= {
426 .init
= sysctl_core_net_init
,
427 .exit
= sysctl_core_net_exit
,
430 static __init
int sysctl_core_init(void)
432 register_net_sysctl(&init_net
, "net/core", net_core_table
);
433 return register_pernet_subsys(&sysctl_core_ops
);
436 fs_initcall(sysctl_core_init
);