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
;
30 static int rps_sock_flow_sysctl(struct ctl_table
*table
, int write
,
31 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
33 unsigned int orig_size
, size
;
35 struct ctl_table tmp
= {
37 .maxlen
= sizeof(size
),
40 struct rps_sock_flow_table
*orig_sock_table
, *sock_table
;
41 static DEFINE_MUTEX(sock_flow_mutex
);
43 mutex_lock(&sock_flow_mutex
);
45 orig_sock_table
= rcu_dereference_protected(rps_sock_flow_table
,
46 lockdep_is_held(&sock_flow_mutex
));
47 size
= orig_size
= orig_sock_table
? orig_sock_table
->mask
+ 1 : 0;
49 ret
= proc_dointvec(&tmp
, write
, buffer
, lenp
, ppos
);
54 /* Enforce limit to prevent overflow */
55 mutex_unlock(&sock_flow_mutex
);
58 size
= roundup_pow_of_two(size
);
59 if (size
!= orig_size
) {
61 vmalloc(RPS_SOCK_FLOW_TABLE_SIZE(size
));
63 mutex_unlock(&sock_flow_mutex
);
67 sock_table
->mask
= size
- 1;
69 sock_table
= orig_sock_table
;
71 for (i
= 0; i
< size
; i
++)
72 sock_table
->ents
[i
] = RPS_NO_CPU
;
76 if (sock_table
!= orig_sock_table
) {
77 rcu_assign_pointer(rps_sock_flow_table
, sock_table
);
79 static_key_slow_inc(&rps_needed
);
80 if (orig_sock_table
) {
81 static_key_slow_dec(&rps_needed
);
83 vfree(orig_sock_table
);
88 mutex_unlock(&sock_flow_mutex
);
92 #endif /* CONFIG_RPS */
94 #ifdef CONFIG_NET_FLOW_LIMIT
95 static DEFINE_MUTEX(flow_limit_update_mutex
);
97 static int flow_limit_cpu_sysctl(struct ctl_table
*table
, int write
,
98 void __user
*buffer
, size_t *lenp
,
101 struct sd_flow_limit
*cur
;
102 struct softnet_data
*sd
;
106 if (!alloc_cpumask_var(&mask
, GFP_KERNEL
))
110 ret
= cpumask_parse_user(buffer
, *lenp
, mask
);
114 mutex_lock(&flow_limit_update_mutex
);
115 len
= sizeof(*cur
) + netdev_flow_limit_table_len
;
116 for_each_possible_cpu(i
) {
117 sd
= &per_cpu(softnet_data
, i
);
118 cur
= rcu_dereference_protected(sd
->flow_limit
,
119 lockdep_is_held(&flow_limit_update_mutex
));
120 if (cur
&& !cpumask_test_cpu(i
, mask
)) {
121 RCU_INIT_POINTER(sd
->flow_limit
, NULL
);
124 } else if (!cur
&& cpumask_test_cpu(i
, mask
)) {
125 cur
= kzalloc(len
, GFP_KERNEL
);
127 /* not unwinding previous changes */
131 cur
->num_buckets
= netdev_flow_limit_table_len
;
132 rcu_assign_pointer(sd
->flow_limit
, cur
);
136 mutex_unlock(&flow_limit_update_mutex
);
140 if (*ppos
|| !*lenp
) {
147 for_each_possible_cpu(i
) {
148 sd
= &per_cpu(softnet_data
, i
);
149 if (rcu_dereference(sd
->flow_limit
))
150 cpumask_set_cpu(i
, mask
);
154 len
= min(sizeof(kbuf
) - 1, *lenp
);
155 len
= cpumask_scnprintf(kbuf
, len
, mask
);
162 if (copy_to_user(buffer
, kbuf
, len
)) {
171 free_cpumask_var(mask
);
175 static int flow_limit_table_len_sysctl(struct ctl_table
*table
, int write
,
176 void __user
*buffer
, size_t *lenp
,
179 unsigned int old
, *ptr
;
182 mutex_lock(&flow_limit_update_mutex
);
186 ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
187 if (!ret
&& write
&& !is_power_of_2(*ptr
)) {
192 mutex_unlock(&flow_limit_update_mutex
);
195 #endif /* CONFIG_NET_FLOW_LIMIT */
197 #ifdef CONFIG_NET_SCHED
198 static int set_default_qdisc(struct ctl_table
*table
, int write
,
199 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
202 struct ctl_table tbl
= {
208 qdisc_get_default(id
, IFNAMSIZ
);
210 ret
= proc_dostring(&tbl
, write
, buffer
, lenp
, ppos
);
211 if (write
&& ret
== 0)
212 ret
= qdisc_set_default(id
);
217 static struct ctl_table net_core_table
[] = {
220 .procname
= "wmem_max",
221 .data
= &sysctl_wmem_max
,
222 .maxlen
= sizeof(int),
224 .proc_handler
= proc_dointvec_minmax
,
228 .procname
= "rmem_max",
229 .data
= &sysctl_rmem_max
,
230 .maxlen
= sizeof(int),
232 .proc_handler
= proc_dointvec_minmax
,
236 .procname
= "wmem_default",
237 .data
= &sysctl_wmem_default
,
238 .maxlen
= sizeof(int),
240 .proc_handler
= proc_dointvec_minmax
,
244 .procname
= "rmem_default",
245 .data
= &sysctl_rmem_default
,
246 .maxlen
= sizeof(int),
248 .proc_handler
= proc_dointvec_minmax
,
252 .procname
= "dev_weight",
254 .maxlen
= sizeof(int),
256 .proc_handler
= proc_dointvec
259 .procname
= "netdev_max_backlog",
260 .data
= &netdev_max_backlog
,
261 .maxlen
= sizeof(int),
263 .proc_handler
= proc_dointvec
265 #ifdef CONFIG_BPF_JIT
267 .procname
= "bpf_jit_enable",
268 .data
= &bpf_jit_enable
,
269 .maxlen
= sizeof(int),
271 .proc_handler
= proc_dointvec
275 .procname
= "netdev_tstamp_prequeue",
276 .data
= &netdev_tstamp_prequeue
,
277 .maxlen
= sizeof(int),
279 .proc_handler
= proc_dointvec
282 .procname
= "message_cost",
283 .data
= &net_ratelimit_state
.interval
,
284 .maxlen
= sizeof(int),
286 .proc_handler
= proc_dointvec_jiffies
,
289 .procname
= "message_burst",
290 .data
= &net_ratelimit_state
.burst
,
291 .maxlen
= sizeof(int),
293 .proc_handler
= proc_dointvec
,
296 .procname
= "optmem_max",
297 .data
= &sysctl_optmem_max
,
298 .maxlen
= sizeof(int),
300 .proc_handler
= proc_dointvec
304 .procname
= "rps_sock_flow_entries",
305 .maxlen
= sizeof(int),
307 .proc_handler
= rps_sock_flow_sysctl
310 #ifdef CONFIG_NET_FLOW_LIMIT
312 .procname
= "flow_limit_cpu_bitmap",
314 .proc_handler
= flow_limit_cpu_sysctl
317 .procname
= "flow_limit_table_len",
318 .data
= &netdev_flow_limit_table_len
,
319 .maxlen
= sizeof(int),
321 .proc_handler
= flow_limit_table_len_sysctl
323 #endif /* CONFIG_NET_FLOW_LIMIT */
324 #ifdef CONFIG_NET_RX_BUSY_POLL
326 .procname
= "busy_poll",
327 .data
= &sysctl_net_busy_poll
,
328 .maxlen
= sizeof(unsigned int),
330 .proc_handler
= proc_dointvec
333 .procname
= "busy_read",
334 .data
= &sysctl_net_busy_read
,
335 .maxlen
= sizeof(unsigned int),
337 .proc_handler
= proc_dointvec
340 #ifdef CONFIG_NET_SCHED
342 .procname
= "default_qdisc",
345 .proc_handler
= set_default_qdisc
348 #endif /* CONFIG_NET */
350 .procname
= "netdev_budget",
351 .data
= &netdev_budget
,
352 .maxlen
= sizeof(int),
354 .proc_handler
= proc_dointvec
357 .procname
= "warnings",
358 .data
= &net_msg_warn
,
359 .maxlen
= sizeof(int),
361 .proc_handler
= proc_dointvec
366 static struct ctl_table netns_core_table
[] = {
368 .procname
= "somaxconn",
369 .data
= &init_net
.core
.sysctl_somaxconn
,
370 .maxlen
= sizeof(int),
373 .extra2
= &ushort_max
,
374 .proc_handler
= proc_dointvec_minmax
379 static __net_init
int sysctl_core_net_init(struct net
*net
)
381 struct ctl_table
*tbl
;
383 net
->core
.sysctl_somaxconn
= SOMAXCONN
;
385 tbl
= netns_core_table
;
386 if (!net_eq(net
, &init_net
)) {
387 tbl
= kmemdup(tbl
, sizeof(netns_core_table
), GFP_KERNEL
);
391 tbl
[0].data
= &net
->core
.sysctl_somaxconn
;
393 /* Don't export any sysctls to unprivileged users */
394 if (net
->user_ns
!= &init_user_ns
) {
395 tbl
[0].procname
= NULL
;
399 net
->core
.sysctl_hdr
= register_net_sysctl(net
, "net/core", tbl
);
400 if (net
->core
.sysctl_hdr
== NULL
)
406 if (tbl
!= netns_core_table
)
412 static __net_exit
void sysctl_core_net_exit(struct net
*net
)
414 struct ctl_table
*tbl
;
416 tbl
= net
->core
.sysctl_hdr
->ctl_table_arg
;
417 unregister_net_sysctl_table(net
->core
.sysctl_hdr
);
418 BUG_ON(tbl
== netns_core_table
);
422 static __net_initdata
struct pernet_operations sysctl_core_ops
= {
423 .init
= sysctl_core_net_init
,
424 .exit
= sysctl_core_net_exit
,
427 static __init
int sysctl_core_init(void)
429 register_net_sysctl(&init_net
, "net/core", net_core_table
);
430 return register_pernet_subsys(&sysctl_core_ops
);
433 fs_initcall(sysctl_core_init
);