1 // SPDX-License-Identifier: GPL-2.0
3 * sysctl_net_core.c: sysctl interface to net core subsystem.
5 * Begun April 1, 1996, Mike Shaver.
6 * Added /proc/sys/net/core directory entry (empty =) ). [MS]
10 #include <linux/sysctl.h>
11 #include <linux/module.h>
12 #include <linux/socket.h>
13 #include <linux/netdevice.h>
14 #include <linux/ratelimit.h>
15 #include <linux/vmalloc.h>
16 #include <linux/init.h>
17 #include <linux/slab.h>
21 #include <net/net_ratelimit.h>
22 #include <net/busy_poll.h>
23 #include <net/pkt_sched.h>
27 static int two __maybe_unused
= 2;
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
;
32 static int net_msg_warn
; /* Unused, but still a sysctl */
34 int sysctl_fb_tunnels_only_for_init_net __read_mostly
= 0;
35 EXPORT_SYMBOL(sysctl_fb_tunnels_only_for_init_net
);
38 static int rps_sock_flow_sysctl(struct ctl_table
*table
, int write
,
39 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
41 unsigned int orig_size
, size
;
43 struct ctl_table tmp
= {
45 .maxlen
= sizeof(size
),
48 struct rps_sock_flow_table
*orig_sock_table
, *sock_table
;
49 static DEFINE_MUTEX(sock_flow_mutex
);
51 mutex_lock(&sock_flow_mutex
);
53 orig_sock_table
= rcu_dereference_protected(rps_sock_flow_table
,
54 lockdep_is_held(&sock_flow_mutex
));
55 size
= orig_size
= orig_sock_table
? orig_sock_table
->mask
+ 1 : 0;
57 ret
= proc_dointvec(&tmp
, write
, buffer
, lenp
, ppos
);
62 /* Enforce limit to prevent overflow */
63 mutex_unlock(&sock_flow_mutex
);
66 size
= roundup_pow_of_two(size
);
67 if (size
!= orig_size
) {
69 vmalloc(RPS_SOCK_FLOW_TABLE_SIZE(size
));
71 mutex_unlock(&sock_flow_mutex
);
74 rps_cpu_mask
= roundup_pow_of_two(nr_cpu_ids
) - 1;
75 sock_table
->mask
= size
- 1;
77 sock_table
= orig_sock_table
;
79 for (i
= 0; i
< size
; i
++)
80 sock_table
->ents
[i
] = RPS_NO_CPU
;
84 if (sock_table
!= orig_sock_table
) {
85 rcu_assign_pointer(rps_sock_flow_table
, sock_table
);
87 static_key_slow_inc(&rps_needed
);
88 static_key_slow_inc(&rfs_needed
);
90 if (orig_sock_table
) {
91 static_key_slow_dec(&rps_needed
);
92 static_key_slow_dec(&rfs_needed
);
94 vfree(orig_sock_table
);
99 mutex_unlock(&sock_flow_mutex
);
103 #endif /* CONFIG_RPS */
105 #ifdef CONFIG_NET_FLOW_LIMIT
106 static DEFINE_MUTEX(flow_limit_update_mutex
);
108 static int flow_limit_cpu_sysctl(struct ctl_table
*table
, int write
,
109 void __user
*buffer
, size_t *lenp
,
112 struct sd_flow_limit
*cur
;
113 struct softnet_data
*sd
;
117 if (!alloc_cpumask_var(&mask
, GFP_KERNEL
))
121 ret
= cpumask_parse_user(buffer
, *lenp
, mask
);
125 mutex_lock(&flow_limit_update_mutex
);
126 len
= sizeof(*cur
) + netdev_flow_limit_table_len
;
127 for_each_possible_cpu(i
) {
128 sd
= &per_cpu(softnet_data
, i
);
129 cur
= rcu_dereference_protected(sd
->flow_limit
,
130 lockdep_is_held(&flow_limit_update_mutex
));
131 if (cur
&& !cpumask_test_cpu(i
, mask
)) {
132 RCU_INIT_POINTER(sd
->flow_limit
, NULL
);
135 } else if (!cur
&& cpumask_test_cpu(i
, mask
)) {
136 cur
= kzalloc_node(len
, GFP_KERNEL
,
139 /* not unwinding previous changes */
143 cur
->num_buckets
= netdev_flow_limit_table_len
;
144 rcu_assign_pointer(sd
->flow_limit
, cur
);
148 mutex_unlock(&flow_limit_update_mutex
);
152 if (*ppos
|| !*lenp
) {
159 for_each_possible_cpu(i
) {
160 sd
= &per_cpu(softnet_data
, i
);
161 if (rcu_dereference(sd
->flow_limit
))
162 cpumask_set_cpu(i
, mask
);
166 len
= min(sizeof(kbuf
) - 1, *lenp
);
167 len
= scnprintf(kbuf
, len
, "%*pb", cpumask_pr_args(mask
));
174 if (copy_to_user(buffer
, kbuf
, len
)) {
183 free_cpumask_var(mask
);
187 static int flow_limit_table_len_sysctl(struct ctl_table
*table
, int write
,
188 void __user
*buffer
, size_t *lenp
,
191 unsigned int old
, *ptr
;
194 mutex_lock(&flow_limit_update_mutex
);
198 ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
199 if (!ret
&& write
&& !is_power_of_2(*ptr
)) {
204 mutex_unlock(&flow_limit_update_mutex
);
207 #endif /* CONFIG_NET_FLOW_LIMIT */
209 #ifdef CONFIG_NET_SCHED
210 static int set_default_qdisc(struct ctl_table
*table
, int write
,
211 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
214 struct ctl_table tbl
= {
220 qdisc_get_default(id
, IFNAMSIZ
);
222 ret
= proc_dostring(&tbl
, write
, buffer
, lenp
, ppos
);
223 if (write
&& ret
== 0)
224 ret
= qdisc_set_default(id
);
229 static int proc_do_dev_weight(struct ctl_table
*table
, int write
,
230 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
234 ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
238 dev_rx_weight
= weight_p
* dev_weight_rx_bias
;
239 dev_tx_weight
= weight_p
* dev_weight_tx_bias
;
244 static int proc_do_rss_key(struct ctl_table
*table
, int write
,
245 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
247 struct ctl_table fake_table
;
248 char buf
[NETDEV_RSS_KEY_LEN
* 3];
250 snprintf(buf
, sizeof(buf
), "%*phC", NETDEV_RSS_KEY_LEN
, netdev_rss_key
);
251 fake_table
.data
= buf
;
252 fake_table
.maxlen
= sizeof(buf
);
253 return proc_dostring(&fake_table
, write
, buffer
, lenp
, ppos
);
256 #ifdef CONFIG_BPF_JIT
257 static int proc_dointvec_minmax_bpf_enable(struct ctl_table
*table
, int write
,
258 void __user
*buffer
, size_t *lenp
,
261 int ret
, jit_enable
= *(int *)table
->data
;
262 struct ctl_table tmp
= *table
;
264 if (write
&& !capable(CAP_SYS_ADMIN
))
267 tmp
.data
= &jit_enable
;
268 ret
= proc_dointvec_minmax(&tmp
, write
, buffer
, lenp
, ppos
);
270 if (jit_enable
< 2 ||
271 (jit_enable
== 2 && bpf_dump_raw_ok())) {
272 *(int *)table
->data
= jit_enable
;
274 pr_warn("bpf_jit_enable = 2 was set! NEVER use this in production, only for JIT debugging!\n");
282 # ifdef CONFIG_HAVE_EBPF_JIT
284 proc_dointvec_minmax_bpf_restricted(struct ctl_table
*table
, int write
,
285 void __user
*buffer
, size_t *lenp
,
288 if (!capable(CAP_SYS_ADMIN
))
291 return proc_dointvec_minmax(table
, write
, buffer
, lenp
, ppos
);
296 static struct ctl_table net_core_table
[] = {
299 .procname
= "wmem_max",
300 .data
= &sysctl_wmem_max
,
301 .maxlen
= sizeof(int),
303 .proc_handler
= proc_dointvec_minmax
,
304 .extra1
= &min_sndbuf
,
307 .procname
= "rmem_max",
308 .data
= &sysctl_rmem_max
,
309 .maxlen
= sizeof(int),
311 .proc_handler
= proc_dointvec_minmax
,
312 .extra1
= &min_rcvbuf
,
315 .procname
= "wmem_default",
316 .data
= &sysctl_wmem_default
,
317 .maxlen
= sizeof(int),
319 .proc_handler
= proc_dointvec_minmax
,
320 .extra1
= &min_sndbuf
,
323 .procname
= "rmem_default",
324 .data
= &sysctl_rmem_default
,
325 .maxlen
= sizeof(int),
327 .proc_handler
= proc_dointvec_minmax
,
328 .extra1
= &min_rcvbuf
,
331 .procname
= "dev_weight",
333 .maxlen
= sizeof(int),
335 .proc_handler
= proc_do_dev_weight
,
338 .procname
= "dev_weight_rx_bias",
339 .data
= &dev_weight_rx_bias
,
340 .maxlen
= sizeof(int),
342 .proc_handler
= proc_do_dev_weight
,
345 .procname
= "dev_weight_tx_bias",
346 .data
= &dev_weight_tx_bias
,
347 .maxlen
= sizeof(int),
349 .proc_handler
= proc_do_dev_weight
,
352 .procname
= "netdev_max_backlog",
353 .data
= &netdev_max_backlog
,
354 .maxlen
= sizeof(int),
356 .proc_handler
= proc_dointvec
359 .procname
= "netdev_rss_key",
360 .data
= &netdev_rss_key
,
361 .maxlen
= sizeof(int),
363 .proc_handler
= proc_do_rss_key
,
365 #ifdef CONFIG_BPF_JIT
367 .procname
= "bpf_jit_enable",
368 .data
= &bpf_jit_enable
,
369 .maxlen
= sizeof(int),
371 .proc_handler
= proc_dointvec_minmax_bpf_enable
,
372 # ifdef CONFIG_BPF_JIT_ALWAYS_ON
380 # ifdef CONFIG_HAVE_EBPF_JIT
382 .procname
= "bpf_jit_harden",
383 .data
= &bpf_jit_harden
,
384 .maxlen
= sizeof(int),
386 .proc_handler
= proc_dointvec_minmax_bpf_restricted
,
391 .procname
= "bpf_jit_kallsyms",
392 .data
= &bpf_jit_kallsyms
,
393 .maxlen
= sizeof(int),
395 .proc_handler
= proc_dointvec_minmax_bpf_restricted
,
402 .procname
= "netdev_tstamp_prequeue",
403 .data
= &netdev_tstamp_prequeue
,
404 .maxlen
= sizeof(int),
406 .proc_handler
= proc_dointvec
409 .procname
= "message_cost",
410 .data
= &net_ratelimit_state
.interval
,
411 .maxlen
= sizeof(int),
413 .proc_handler
= proc_dointvec_jiffies
,
416 .procname
= "message_burst",
417 .data
= &net_ratelimit_state
.burst
,
418 .maxlen
= sizeof(int),
420 .proc_handler
= proc_dointvec
,
423 .procname
= "optmem_max",
424 .data
= &sysctl_optmem_max
,
425 .maxlen
= sizeof(int),
427 .proc_handler
= proc_dointvec
430 .procname
= "tstamp_allow_data",
431 .data
= &sysctl_tstamp_allow_data
,
432 .maxlen
= sizeof(int),
434 .proc_handler
= proc_dointvec_minmax
,
440 .procname
= "rps_sock_flow_entries",
441 .maxlen
= sizeof(int),
443 .proc_handler
= rps_sock_flow_sysctl
446 #ifdef CONFIG_NET_FLOW_LIMIT
448 .procname
= "flow_limit_cpu_bitmap",
450 .proc_handler
= flow_limit_cpu_sysctl
453 .procname
= "flow_limit_table_len",
454 .data
= &netdev_flow_limit_table_len
,
455 .maxlen
= sizeof(int),
457 .proc_handler
= flow_limit_table_len_sysctl
459 #endif /* CONFIG_NET_FLOW_LIMIT */
460 #ifdef CONFIG_NET_RX_BUSY_POLL
462 .procname
= "busy_poll",
463 .data
= &sysctl_net_busy_poll
,
464 .maxlen
= sizeof(unsigned int),
466 .proc_handler
= proc_dointvec_minmax
,
470 .procname
= "busy_read",
471 .data
= &sysctl_net_busy_read
,
472 .maxlen
= sizeof(unsigned int),
474 .proc_handler
= proc_dointvec_minmax
,
478 #ifdef CONFIG_NET_SCHED
480 .procname
= "default_qdisc",
483 .proc_handler
= set_default_qdisc
486 #endif /* CONFIG_NET */
488 .procname
= "netdev_budget",
489 .data
= &netdev_budget
,
490 .maxlen
= sizeof(int),
492 .proc_handler
= proc_dointvec
495 .procname
= "warnings",
496 .data
= &net_msg_warn
,
497 .maxlen
= sizeof(int),
499 .proc_handler
= proc_dointvec
502 .procname
= "max_skb_frags",
503 .data
= &sysctl_max_skb_frags
,
504 .maxlen
= sizeof(int),
506 .proc_handler
= proc_dointvec_minmax
,
508 .extra2
= &max_skb_frags
,
511 .procname
= "netdev_budget_usecs",
512 .data
= &netdev_budget_usecs
,
513 .maxlen
= sizeof(unsigned int),
515 .proc_handler
= proc_dointvec_minmax
,
519 .procname
= "fb_tunnels_only_for_init_net",
520 .data
= &sysctl_fb_tunnels_only_for_init_net
,
521 .maxlen
= sizeof(int),
523 .proc_handler
= proc_dointvec_minmax
,
530 static struct ctl_table netns_core_table
[] = {
532 .procname
= "somaxconn",
533 .data
= &init_net
.core
.sysctl_somaxconn
,
534 .maxlen
= sizeof(int),
537 .proc_handler
= proc_dointvec_minmax
542 static __net_init
int sysctl_core_net_init(struct net
*net
)
544 struct ctl_table
*tbl
;
546 tbl
= netns_core_table
;
547 if (!net_eq(net
, &init_net
)) {
548 tbl
= kmemdup(tbl
, sizeof(netns_core_table
), GFP_KERNEL
);
552 tbl
[0].data
= &net
->core
.sysctl_somaxconn
;
554 /* Don't export any sysctls to unprivileged users */
555 if (net
->user_ns
!= &init_user_ns
) {
556 tbl
[0].procname
= NULL
;
560 net
->core
.sysctl_hdr
= register_net_sysctl(net
, "net/core", tbl
);
561 if (net
->core
.sysctl_hdr
== NULL
)
567 if (tbl
!= netns_core_table
)
573 static __net_exit
void sysctl_core_net_exit(struct net
*net
)
575 struct ctl_table
*tbl
;
577 tbl
= net
->core
.sysctl_hdr
->ctl_table_arg
;
578 unregister_net_sysctl_table(net
->core
.sysctl_hdr
);
579 BUG_ON(tbl
== netns_core_table
);
583 static __net_initdata
struct pernet_operations sysctl_core_ops
= {
584 .init
= sysctl_core_net_init
,
585 .exit
= sysctl_core_net_exit
,
588 static __init
int sysctl_core_init(void)
590 register_net_sysctl(&init_net
, "net/core", net_core_table
);
591 return register_pernet_subsys(&sysctl_core_ops
);
594 fs_initcall(sysctl_core_init
);