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
;
29 static int net_msg_warn
; /* Unused, but still a sysctl */
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 int proc_do_rss_key(struct ctl_table
*table
, int write
,
221 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
223 struct ctl_table fake_table
;
224 char buf
[NETDEV_RSS_KEY_LEN
* 3];
226 snprintf(buf
, sizeof(buf
), "%*phC", NETDEV_RSS_KEY_LEN
, netdev_rss_key
);
227 fake_table
.data
= buf
;
228 fake_table
.maxlen
= sizeof(buf
);
229 return proc_dostring(&fake_table
, write
, buffer
, lenp
, ppos
);
232 static struct ctl_table net_core_table
[] = {
235 .procname
= "wmem_max",
236 .data
= &sysctl_wmem_max
,
237 .maxlen
= sizeof(int),
239 .proc_handler
= proc_dointvec_minmax
,
243 .procname
= "rmem_max",
244 .data
= &sysctl_rmem_max
,
245 .maxlen
= sizeof(int),
247 .proc_handler
= proc_dointvec_minmax
,
251 .procname
= "wmem_default",
252 .data
= &sysctl_wmem_default
,
253 .maxlen
= sizeof(int),
255 .proc_handler
= proc_dointvec_minmax
,
259 .procname
= "rmem_default",
260 .data
= &sysctl_rmem_default
,
261 .maxlen
= sizeof(int),
263 .proc_handler
= proc_dointvec_minmax
,
267 .procname
= "dev_weight",
269 .maxlen
= sizeof(int),
271 .proc_handler
= proc_dointvec
274 .procname
= "netdev_max_backlog",
275 .data
= &netdev_max_backlog
,
276 .maxlen
= sizeof(int),
278 .proc_handler
= proc_dointvec
281 .procname
= "netdev_rss_key",
282 .data
= &netdev_rss_key
,
283 .maxlen
= sizeof(int),
285 .proc_handler
= proc_do_rss_key
,
287 #ifdef CONFIG_BPF_JIT
289 .procname
= "bpf_jit_enable",
290 .data
= &bpf_jit_enable
,
291 .maxlen
= sizeof(int),
293 .proc_handler
= proc_dointvec
297 .procname
= "netdev_tstamp_prequeue",
298 .data
= &netdev_tstamp_prequeue
,
299 .maxlen
= sizeof(int),
301 .proc_handler
= proc_dointvec
304 .procname
= "message_cost",
305 .data
= &net_ratelimit_state
.interval
,
306 .maxlen
= sizeof(int),
308 .proc_handler
= proc_dointvec_jiffies
,
311 .procname
= "message_burst",
312 .data
= &net_ratelimit_state
.burst
,
313 .maxlen
= sizeof(int),
315 .proc_handler
= proc_dointvec
,
318 .procname
= "optmem_max",
319 .data
= &sysctl_optmem_max
,
320 .maxlen
= sizeof(int),
322 .proc_handler
= proc_dointvec
326 .procname
= "rps_sock_flow_entries",
327 .maxlen
= sizeof(int),
329 .proc_handler
= rps_sock_flow_sysctl
332 #ifdef CONFIG_NET_FLOW_LIMIT
334 .procname
= "flow_limit_cpu_bitmap",
336 .proc_handler
= flow_limit_cpu_sysctl
339 .procname
= "flow_limit_table_len",
340 .data
= &netdev_flow_limit_table_len
,
341 .maxlen
= sizeof(int),
343 .proc_handler
= flow_limit_table_len_sysctl
345 #endif /* CONFIG_NET_FLOW_LIMIT */
346 #ifdef CONFIG_NET_RX_BUSY_POLL
348 .procname
= "busy_poll",
349 .data
= &sysctl_net_busy_poll
,
350 .maxlen
= sizeof(unsigned int),
352 .proc_handler
= proc_dointvec
355 .procname
= "busy_read",
356 .data
= &sysctl_net_busy_read
,
357 .maxlen
= sizeof(unsigned int),
359 .proc_handler
= proc_dointvec
362 #ifdef CONFIG_NET_SCHED
364 .procname
= "default_qdisc",
367 .proc_handler
= set_default_qdisc
370 #endif /* CONFIG_NET */
372 .procname
= "netdev_budget",
373 .data
= &netdev_budget
,
374 .maxlen
= sizeof(int),
376 .proc_handler
= proc_dointvec
379 .procname
= "warnings",
380 .data
= &net_msg_warn
,
381 .maxlen
= sizeof(int),
383 .proc_handler
= proc_dointvec
388 static struct ctl_table netns_core_table
[] = {
390 .procname
= "somaxconn",
391 .data
= &init_net
.core
.sysctl_somaxconn
,
392 .maxlen
= sizeof(int),
395 .extra2
= &ushort_max
,
396 .proc_handler
= proc_dointvec_minmax
401 static __net_init
int sysctl_core_net_init(struct net
*net
)
403 struct ctl_table
*tbl
;
405 net
->core
.sysctl_somaxconn
= SOMAXCONN
;
407 tbl
= netns_core_table
;
408 if (!net_eq(net
, &init_net
)) {
409 tbl
= kmemdup(tbl
, sizeof(netns_core_table
), GFP_KERNEL
);
413 tbl
[0].data
= &net
->core
.sysctl_somaxconn
;
415 /* Don't export any sysctls to unprivileged users */
416 if (net
->user_ns
!= &init_user_ns
) {
417 tbl
[0].procname
= NULL
;
421 net
->core
.sysctl_hdr
= register_net_sysctl(net
, "net/core", tbl
);
422 if (net
->core
.sysctl_hdr
== NULL
)
428 if (tbl
!= netns_core_table
)
434 static __net_exit
void sysctl_core_net_exit(struct net
*net
)
436 struct ctl_table
*tbl
;
438 tbl
= net
->core
.sysctl_hdr
->ctl_table_arg
;
439 unregister_net_sysctl_table(net
->core
.sysctl_hdr
);
440 BUG_ON(tbl
== netns_core_table
);
444 static __net_initdata
struct pernet_operations sysctl_core_ops
= {
445 .init
= sysctl_core_net_init
,
446 .exit
= sysctl_core_net_exit
,
449 static __init
int sysctl_core_init(void)
451 register_net_sysctl(&init_net
, "net/core", net_core_table
);
452 return register_pernet_subsys(&sysctl_core_ops
);
455 fs_initcall(sysctl_core_init
);