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
;
30 static int max_skb_frags
= MAX_SKB_FRAGS
;
33 static int rps_sock_flow_sysctl(struct ctl_table
*table
, int write
,
34 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
36 unsigned int orig_size
, size
;
38 struct ctl_table tmp
= {
40 .maxlen
= sizeof(size
),
43 struct rps_sock_flow_table
*orig_sock_table
, *sock_table
;
44 static DEFINE_MUTEX(sock_flow_mutex
);
46 mutex_lock(&sock_flow_mutex
);
48 orig_sock_table
= rcu_dereference_protected(rps_sock_flow_table
,
49 lockdep_is_held(&sock_flow_mutex
));
50 size
= orig_size
= orig_sock_table
? orig_sock_table
->mask
+ 1 : 0;
52 ret
= proc_dointvec(&tmp
, write
, buffer
, lenp
, ppos
);
57 /* Enforce limit to prevent overflow */
58 mutex_unlock(&sock_flow_mutex
);
61 size
= roundup_pow_of_two(size
);
62 if (size
!= orig_size
) {
64 vmalloc(RPS_SOCK_FLOW_TABLE_SIZE(size
));
66 mutex_unlock(&sock_flow_mutex
);
70 sock_table
->mask
= size
- 1;
72 sock_table
= orig_sock_table
;
74 for (i
= 0; i
< size
; i
++)
75 sock_table
->ents
[i
] = RPS_NO_CPU
;
79 if (sock_table
!= orig_sock_table
) {
80 rcu_assign_pointer(rps_sock_flow_table
, sock_table
);
82 static_key_slow_inc(&rps_needed
);
83 if (orig_sock_table
) {
84 static_key_slow_dec(&rps_needed
);
86 vfree(orig_sock_table
);
91 mutex_unlock(&sock_flow_mutex
);
95 #endif /* CONFIG_RPS */
97 #ifdef CONFIG_NET_FLOW_LIMIT
98 static DEFINE_MUTEX(flow_limit_update_mutex
);
100 static int flow_limit_cpu_sysctl(struct ctl_table
*table
, int write
,
101 void __user
*buffer
, size_t *lenp
,
104 struct sd_flow_limit
*cur
;
105 struct softnet_data
*sd
;
109 if (!alloc_cpumask_var(&mask
, GFP_KERNEL
))
113 ret
= cpumask_parse_user(buffer
, *lenp
, mask
);
117 mutex_lock(&flow_limit_update_mutex
);
118 len
= sizeof(*cur
) + netdev_flow_limit_table_len
;
119 for_each_possible_cpu(i
) {
120 sd
= &per_cpu(softnet_data
, i
);
121 cur
= rcu_dereference_protected(sd
->flow_limit
,
122 lockdep_is_held(&flow_limit_update_mutex
));
123 if (cur
&& !cpumask_test_cpu(i
, mask
)) {
124 RCU_INIT_POINTER(sd
->flow_limit
, NULL
);
127 } else if (!cur
&& cpumask_test_cpu(i
, mask
)) {
128 cur
= kzalloc_node(len
, GFP_KERNEL
,
131 /* not unwinding previous changes */
135 cur
->num_buckets
= netdev_flow_limit_table_len
;
136 rcu_assign_pointer(sd
->flow_limit
, cur
);
140 mutex_unlock(&flow_limit_update_mutex
);
144 if (*ppos
|| !*lenp
) {
151 for_each_possible_cpu(i
) {
152 sd
= &per_cpu(softnet_data
, i
);
153 if (rcu_dereference(sd
->flow_limit
))
154 cpumask_set_cpu(i
, mask
);
158 len
= min(sizeof(kbuf
) - 1, *lenp
);
159 len
= cpumask_scnprintf(kbuf
, len
, mask
);
166 if (copy_to_user(buffer
, kbuf
, len
)) {
175 free_cpumask_var(mask
);
179 static int flow_limit_table_len_sysctl(struct ctl_table
*table
, int write
,
180 void __user
*buffer
, size_t *lenp
,
183 unsigned int old
, *ptr
;
186 mutex_lock(&flow_limit_update_mutex
);
190 ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
191 if (!ret
&& write
&& !is_power_of_2(*ptr
)) {
196 mutex_unlock(&flow_limit_update_mutex
);
199 #endif /* CONFIG_NET_FLOW_LIMIT */
201 #ifdef CONFIG_NET_SCHED
202 static int set_default_qdisc(struct ctl_table
*table
, int write
,
203 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
206 struct ctl_table tbl
= {
212 qdisc_get_default(id
, IFNAMSIZ
);
214 ret
= proc_dostring(&tbl
, write
, buffer
, lenp
, ppos
);
215 if (write
&& ret
== 0)
216 ret
= qdisc_set_default(id
);
221 static struct ctl_table net_core_table
[] = {
224 .procname
= "wmem_max",
225 .data
= &sysctl_wmem_max
,
226 .maxlen
= sizeof(int),
228 .proc_handler
= proc_dointvec_minmax
,
229 .extra1
= &min_sndbuf
,
232 .procname
= "rmem_max",
233 .data
= &sysctl_rmem_max
,
234 .maxlen
= sizeof(int),
236 .proc_handler
= proc_dointvec_minmax
,
237 .extra1
= &min_rcvbuf
,
240 .procname
= "wmem_default",
241 .data
= &sysctl_wmem_default
,
242 .maxlen
= sizeof(int),
244 .proc_handler
= proc_dointvec_minmax
,
245 .extra1
= &min_sndbuf
,
248 .procname
= "rmem_default",
249 .data
= &sysctl_rmem_default
,
250 .maxlen
= sizeof(int),
252 .proc_handler
= proc_dointvec_minmax
,
253 .extra1
= &min_rcvbuf
,
256 .procname
= "dev_weight",
258 .maxlen
= sizeof(int),
260 .proc_handler
= proc_dointvec
263 .procname
= "netdev_max_backlog",
264 .data
= &netdev_max_backlog
,
265 .maxlen
= sizeof(int),
267 .proc_handler
= proc_dointvec
269 #ifdef CONFIG_BPF_JIT
271 .procname
= "bpf_jit_enable",
272 .data
= &bpf_jit_enable
,
273 .maxlen
= sizeof(int),
275 .proc_handler
= proc_dointvec
279 .procname
= "netdev_tstamp_prequeue",
280 .data
= &netdev_tstamp_prequeue
,
281 .maxlen
= sizeof(int),
283 .proc_handler
= proc_dointvec
286 .procname
= "message_cost",
287 .data
= &net_ratelimit_state
.interval
,
288 .maxlen
= sizeof(int),
290 .proc_handler
= proc_dointvec_jiffies
,
293 .procname
= "message_burst",
294 .data
= &net_ratelimit_state
.burst
,
295 .maxlen
= sizeof(int),
297 .proc_handler
= proc_dointvec
,
300 .procname
= "optmem_max",
301 .data
= &sysctl_optmem_max
,
302 .maxlen
= sizeof(int),
304 .proc_handler
= proc_dointvec
308 .procname
= "rps_sock_flow_entries",
309 .maxlen
= sizeof(int),
311 .proc_handler
= rps_sock_flow_sysctl
314 #ifdef CONFIG_NET_FLOW_LIMIT
316 .procname
= "flow_limit_cpu_bitmap",
318 .proc_handler
= flow_limit_cpu_sysctl
321 .procname
= "flow_limit_table_len",
322 .data
= &netdev_flow_limit_table_len
,
323 .maxlen
= sizeof(int),
325 .proc_handler
= flow_limit_table_len_sysctl
327 #endif /* CONFIG_NET_FLOW_LIMIT */
328 #ifdef CONFIG_NET_RX_BUSY_POLL
330 .procname
= "busy_poll",
331 .data
= &sysctl_net_busy_poll
,
332 .maxlen
= sizeof(unsigned int),
334 .proc_handler
= proc_dointvec
337 .procname
= "busy_read",
338 .data
= &sysctl_net_busy_read
,
339 .maxlen
= sizeof(unsigned int),
341 .proc_handler
= proc_dointvec
344 #ifdef CONFIG_NET_SCHED
346 .procname
= "default_qdisc",
349 .proc_handler
= set_default_qdisc
352 #endif /* CONFIG_NET */
354 .procname
= "netdev_budget",
355 .data
= &netdev_budget
,
356 .maxlen
= sizeof(int),
358 .proc_handler
= proc_dointvec
361 .procname
= "warnings",
362 .data
= &net_msg_warn
,
363 .maxlen
= sizeof(int),
365 .proc_handler
= proc_dointvec
368 .procname
= "max_skb_frags",
369 .data
= &sysctl_max_skb_frags
,
370 .maxlen
= sizeof(int),
372 .proc_handler
= proc_dointvec_minmax
,
374 .extra2
= &max_skb_frags
,
379 static struct ctl_table netns_core_table
[] = {
381 .procname
= "somaxconn",
382 .data
= &init_net
.core
.sysctl_somaxconn
,
383 .maxlen
= sizeof(int),
386 .extra2
= &ushort_max
,
387 .proc_handler
= proc_dointvec_minmax
392 static __net_init
int sysctl_core_net_init(struct net
*net
)
394 struct ctl_table
*tbl
;
396 net
->core
.sysctl_somaxconn
= SOMAXCONN
;
398 tbl
= netns_core_table
;
399 if (!net_eq(net
, &init_net
)) {
400 tbl
= kmemdup(tbl
, sizeof(netns_core_table
), GFP_KERNEL
);
404 tbl
[0].data
= &net
->core
.sysctl_somaxconn
;
406 /* Don't export any sysctls to unprivileged users */
407 if (net
->user_ns
!= &init_user_ns
) {
408 tbl
[0].procname
= NULL
;
412 net
->core
.sysctl_hdr
= register_net_sysctl(net
, "net/core", tbl
);
413 if (net
->core
.sysctl_hdr
== NULL
)
419 if (tbl
!= netns_core_table
)
425 static __net_exit
void sysctl_core_net_exit(struct net
*net
)
427 struct ctl_table
*tbl
;
429 tbl
= net
->core
.sysctl_hdr
->ctl_table_arg
;
430 unregister_net_sysctl_table(net
->core
.sysctl_hdr
);
431 BUG_ON(tbl
== netns_core_table
);
435 static __net_initdata
struct pernet_operations sysctl_core_ops
= {
436 .init
= sysctl_core_net_init
,
437 .exit
= sysctl_core_net_exit
,
440 static __init
int sysctl_core_init(void)
442 register_net_sysctl(&init_net
, "net/core", net_core_table
);
443 return register_pernet_subsys(&sysctl_core_ops
);
446 fs_initcall(sysctl_core_init
);