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_node(len
, GFP_KERNEL
,
128 /* not unwinding previous changes */
132 cur
->num_buckets
= netdev_flow_limit_table_len
;
133 rcu_assign_pointer(sd
->flow_limit
, cur
);
137 mutex_unlock(&flow_limit_update_mutex
);
141 if (*ppos
|| !*lenp
) {
148 for_each_possible_cpu(i
) {
149 sd
= &per_cpu(softnet_data
, i
);
150 if (rcu_dereference(sd
->flow_limit
))
151 cpumask_set_cpu(i
, mask
);
155 len
= min(sizeof(kbuf
) - 1, *lenp
);
156 len
= cpumask_scnprintf(kbuf
, len
, mask
);
163 if (copy_to_user(buffer
, kbuf
, len
)) {
172 free_cpumask_var(mask
);
176 static int flow_limit_table_len_sysctl(struct ctl_table
*table
, int write
,
177 void __user
*buffer
, size_t *lenp
,
180 unsigned int old
, *ptr
;
183 mutex_lock(&flow_limit_update_mutex
);
187 ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
188 if (!ret
&& write
&& !is_power_of_2(*ptr
)) {
193 mutex_unlock(&flow_limit_update_mutex
);
196 #endif /* CONFIG_NET_FLOW_LIMIT */
198 #ifdef CONFIG_NET_SCHED
199 static int set_default_qdisc(struct ctl_table
*table
, int write
,
200 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
203 struct ctl_table tbl
= {
209 qdisc_get_default(id
, IFNAMSIZ
);
211 ret
= proc_dostring(&tbl
, write
, buffer
, lenp
, ppos
);
212 if (write
&& ret
== 0)
213 ret
= qdisc_set_default(id
);
218 static struct ctl_table net_core_table
[] = {
221 .procname
= "wmem_max",
222 .data
= &sysctl_wmem_max
,
223 .maxlen
= sizeof(int),
225 .proc_handler
= proc_dointvec_minmax
,
229 .procname
= "rmem_max",
230 .data
= &sysctl_rmem_max
,
231 .maxlen
= sizeof(int),
233 .proc_handler
= proc_dointvec_minmax
,
237 .procname
= "wmem_default",
238 .data
= &sysctl_wmem_default
,
239 .maxlen
= sizeof(int),
241 .proc_handler
= proc_dointvec_minmax
,
245 .procname
= "rmem_default",
246 .data
= &sysctl_rmem_default
,
247 .maxlen
= sizeof(int),
249 .proc_handler
= proc_dointvec_minmax
,
253 .procname
= "dev_weight",
255 .maxlen
= sizeof(int),
257 .proc_handler
= proc_dointvec
260 .procname
= "netdev_max_backlog",
261 .data
= &netdev_max_backlog
,
262 .maxlen
= sizeof(int),
264 .proc_handler
= proc_dointvec
266 #ifdef CONFIG_BPF_JIT
268 .procname
= "bpf_jit_enable",
269 .data
= &bpf_jit_enable
,
270 .maxlen
= sizeof(int),
272 .proc_handler
= proc_dointvec
276 .procname
= "netdev_tstamp_prequeue",
277 .data
= &netdev_tstamp_prequeue
,
278 .maxlen
= sizeof(int),
280 .proc_handler
= proc_dointvec
283 .procname
= "message_cost",
284 .data
= &net_ratelimit_state
.interval
,
285 .maxlen
= sizeof(int),
287 .proc_handler
= proc_dointvec_jiffies
,
290 .procname
= "message_burst",
291 .data
= &net_ratelimit_state
.burst
,
292 .maxlen
= sizeof(int),
294 .proc_handler
= proc_dointvec
,
297 .procname
= "optmem_max",
298 .data
= &sysctl_optmem_max
,
299 .maxlen
= sizeof(int),
301 .proc_handler
= proc_dointvec
305 .procname
= "rps_sock_flow_entries",
306 .maxlen
= sizeof(int),
308 .proc_handler
= rps_sock_flow_sysctl
311 #ifdef CONFIG_NET_FLOW_LIMIT
313 .procname
= "flow_limit_cpu_bitmap",
315 .proc_handler
= flow_limit_cpu_sysctl
318 .procname
= "flow_limit_table_len",
319 .data
= &netdev_flow_limit_table_len
,
320 .maxlen
= sizeof(int),
322 .proc_handler
= flow_limit_table_len_sysctl
324 #endif /* CONFIG_NET_FLOW_LIMIT */
325 #ifdef CONFIG_NET_RX_BUSY_POLL
327 .procname
= "busy_poll",
328 .data
= &sysctl_net_busy_poll
,
329 .maxlen
= sizeof(unsigned int),
331 .proc_handler
= proc_dointvec
334 .procname
= "busy_read",
335 .data
= &sysctl_net_busy_read
,
336 .maxlen
= sizeof(unsigned int),
338 .proc_handler
= proc_dointvec
341 #ifdef CONFIG_NET_SCHED
343 .procname
= "default_qdisc",
346 .proc_handler
= set_default_qdisc
349 #endif /* CONFIG_NET */
351 .procname
= "netdev_budget",
352 .data
= &netdev_budget
,
353 .maxlen
= sizeof(int),
355 .proc_handler
= proc_dointvec
358 .procname
= "warnings",
359 .data
= &net_msg_warn
,
360 .maxlen
= sizeof(int),
362 .proc_handler
= proc_dointvec
367 static struct ctl_table netns_core_table
[] = {
369 .procname
= "somaxconn",
370 .data
= &init_net
.core
.sysctl_somaxconn
,
371 .maxlen
= sizeof(int),
374 .extra2
= &ushort_max
,
375 .proc_handler
= proc_dointvec_minmax
380 static __net_init
int sysctl_core_net_init(struct net
*net
)
382 struct ctl_table
*tbl
;
384 net
->core
.sysctl_somaxconn
= SOMAXCONN
;
386 tbl
= netns_core_table
;
387 if (!net_eq(net
, &init_net
)) {
388 tbl
= kmemdup(tbl
, sizeof(netns_core_table
), GFP_KERNEL
);
392 tbl
[0].data
= &net
->core
.sysctl_somaxconn
;
394 /* Don't export any sysctls to unprivileged users */
395 if (net
->user_ns
!= &init_user_ns
) {
396 tbl
[0].procname
= NULL
;
400 net
->core
.sysctl_hdr
= register_net_sysctl(net
, "net/core", tbl
);
401 if (net
->core
.sysctl_hdr
== NULL
)
407 if (tbl
!= netns_core_table
)
413 static __net_exit
void sysctl_core_net_exit(struct net
*net
)
415 struct ctl_table
*tbl
;
417 tbl
= net
->core
.sysctl_hdr
->ctl_table_arg
;
418 unregister_net_sysctl_table(net
->core
.sysctl_hdr
);
419 BUG_ON(tbl
== netns_core_table
);
423 static __net_initdata
struct pernet_operations sysctl_core_ops
= {
424 .init
= sysctl_core_net_init
,
425 .exit
= sysctl_core_net_exit
,
428 static __init
int sysctl_core_init(void)
430 register_net_sysctl(&init_net
, "net/core", net_core_table
);
431 return register_pernet_subsys(&sysctl_core_ops
);
434 fs_initcall(sysctl_core_init
);