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>
18 #include <linux/kmemleak.h>
22 #include <net/net_ratelimit.h>
23 #include <net/busy_poll.h>
24 #include <net/pkt_sched.h>
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 */
35 static int rps_sock_flow_sysctl(struct ctl_table
*table
, int write
,
36 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
38 unsigned int orig_size
, size
;
40 struct ctl_table tmp
= {
42 .maxlen
= sizeof(size
),
45 struct rps_sock_flow_table
*orig_sock_table
, *sock_table
;
46 static DEFINE_MUTEX(sock_flow_mutex
);
48 mutex_lock(&sock_flow_mutex
);
50 orig_sock_table
= rcu_dereference_protected(rps_sock_flow_table
,
51 lockdep_is_held(&sock_flow_mutex
));
52 size
= orig_size
= orig_sock_table
? orig_sock_table
->mask
+ 1 : 0;
54 ret
= proc_dointvec(&tmp
, write
, buffer
, lenp
, ppos
);
59 /* Enforce limit to prevent overflow */
60 mutex_unlock(&sock_flow_mutex
);
63 size
= roundup_pow_of_two(size
);
64 if (size
!= orig_size
) {
66 vmalloc(RPS_SOCK_FLOW_TABLE_SIZE(size
));
68 mutex_unlock(&sock_flow_mutex
);
71 rps_cpu_mask
= roundup_pow_of_two(nr_cpu_ids
) - 1;
72 sock_table
->mask
= size
- 1;
74 sock_table
= orig_sock_table
;
76 for (i
= 0; i
< size
; i
++)
77 sock_table
->ents
[i
] = RPS_NO_CPU
;
81 if (sock_table
!= orig_sock_table
) {
82 rcu_assign_pointer(rps_sock_flow_table
, sock_table
);
84 static_key_slow_inc(&rps_needed
);
85 static_key_slow_inc(&rfs_needed
);
87 if (orig_sock_table
) {
88 static_key_slow_dec(&rps_needed
);
89 static_key_slow_dec(&rfs_needed
);
91 vfree(orig_sock_table
);
96 mutex_unlock(&sock_flow_mutex
);
100 #endif /* CONFIG_RPS */
102 #ifdef CONFIG_NET_FLOW_LIMIT
103 static DEFINE_MUTEX(flow_limit_update_mutex
);
105 static int flow_limit_cpu_sysctl(struct ctl_table
*table
, int write
,
106 void __user
*buffer
, size_t *lenp
,
109 struct sd_flow_limit
*cur
;
110 struct softnet_data
*sd
;
114 if (!alloc_cpumask_var(&mask
, GFP_KERNEL
))
118 ret
= cpumask_parse_user(buffer
, *lenp
, mask
);
122 mutex_lock(&flow_limit_update_mutex
);
123 len
= sizeof(*cur
) + netdev_flow_limit_table_len
;
124 for_each_possible_cpu(i
) {
125 sd
= &per_cpu(softnet_data
, i
);
126 cur
= rcu_dereference_protected(sd
->flow_limit
,
127 lockdep_is_held(&flow_limit_update_mutex
));
128 if (cur
&& !cpumask_test_cpu(i
, mask
)) {
129 RCU_INIT_POINTER(sd
->flow_limit
, NULL
);
132 } else if (!cur
&& cpumask_test_cpu(i
, mask
)) {
133 cur
= kzalloc_node(len
, GFP_KERNEL
,
136 /* not unwinding previous changes */
140 cur
->num_buckets
= netdev_flow_limit_table_len
;
141 rcu_assign_pointer(sd
->flow_limit
, cur
);
145 mutex_unlock(&flow_limit_update_mutex
);
149 if (*ppos
|| !*lenp
) {
156 for_each_possible_cpu(i
) {
157 sd
= &per_cpu(softnet_data
, i
);
158 if (rcu_dereference(sd
->flow_limit
))
159 cpumask_set_cpu(i
, mask
);
163 len
= min(sizeof(kbuf
) - 1, *lenp
);
164 len
= scnprintf(kbuf
, len
, "%*pb", cpumask_pr_args(mask
));
171 if (copy_to_user(buffer
, kbuf
, len
)) {
180 free_cpumask_var(mask
);
184 static int flow_limit_table_len_sysctl(struct ctl_table
*table
, int write
,
185 void __user
*buffer
, size_t *lenp
,
188 unsigned int old
, *ptr
;
191 mutex_lock(&flow_limit_update_mutex
);
195 ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
196 if (!ret
&& write
&& !is_power_of_2(*ptr
)) {
201 mutex_unlock(&flow_limit_update_mutex
);
204 #endif /* CONFIG_NET_FLOW_LIMIT */
206 #ifdef CONFIG_NET_SCHED
207 static int set_default_qdisc(struct ctl_table
*table
, int write
,
208 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
211 struct ctl_table tbl
= {
217 qdisc_get_default(id
, IFNAMSIZ
);
219 ret
= proc_dostring(&tbl
, write
, buffer
, lenp
, ppos
);
220 if (write
&& ret
== 0)
221 ret
= qdisc_set_default(id
);
226 static int proc_do_dev_weight(struct ctl_table
*table
, int write
,
227 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
231 ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
235 dev_rx_weight
= weight_p
* dev_weight_rx_bias
;
236 dev_tx_weight
= weight_p
* dev_weight_tx_bias
;
241 static int proc_do_rss_key(struct ctl_table
*table
, int write
,
242 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
244 struct ctl_table fake_table
;
245 char buf
[NETDEV_RSS_KEY_LEN
* 3];
247 snprintf(buf
, sizeof(buf
), "%*phC", NETDEV_RSS_KEY_LEN
, netdev_rss_key
);
248 fake_table
.data
= buf
;
249 fake_table
.maxlen
= sizeof(buf
);
250 return proc_dostring(&fake_table
, write
, buffer
, lenp
, ppos
);
253 static struct ctl_table net_core_table
[] = {
256 .procname
= "wmem_max",
257 .data
= &sysctl_wmem_max
,
258 .maxlen
= sizeof(int),
260 .proc_handler
= proc_dointvec_minmax
,
261 .extra1
= &min_sndbuf
,
264 .procname
= "rmem_max",
265 .data
= &sysctl_rmem_max
,
266 .maxlen
= sizeof(int),
268 .proc_handler
= proc_dointvec_minmax
,
269 .extra1
= &min_rcvbuf
,
272 .procname
= "wmem_default",
273 .data
= &sysctl_wmem_default
,
274 .maxlen
= sizeof(int),
276 .proc_handler
= proc_dointvec_minmax
,
277 .extra1
= &min_sndbuf
,
280 .procname
= "rmem_default",
281 .data
= &sysctl_rmem_default
,
282 .maxlen
= sizeof(int),
284 .proc_handler
= proc_dointvec_minmax
,
285 .extra1
= &min_rcvbuf
,
288 .procname
= "dev_weight",
290 .maxlen
= sizeof(int),
292 .proc_handler
= proc_do_dev_weight
,
295 .procname
= "dev_weight_rx_bias",
296 .data
= &dev_weight_rx_bias
,
297 .maxlen
= sizeof(int),
299 .proc_handler
= proc_do_dev_weight
,
302 .procname
= "dev_weight_tx_bias",
303 .data
= &dev_weight_tx_bias
,
304 .maxlen
= sizeof(int),
306 .proc_handler
= proc_do_dev_weight
,
309 .procname
= "netdev_max_backlog",
310 .data
= &netdev_max_backlog
,
311 .maxlen
= sizeof(int),
313 .proc_handler
= proc_dointvec
316 .procname
= "netdev_rss_key",
317 .data
= &netdev_rss_key
,
318 .maxlen
= sizeof(int),
320 .proc_handler
= proc_do_rss_key
,
322 #ifdef CONFIG_BPF_JIT
324 .procname
= "bpf_jit_enable",
325 .data
= &bpf_jit_enable
,
326 .maxlen
= sizeof(int),
328 #ifndef CONFIG_BPF_JIT_ALWAYS_ON
329 .proc_handler
= proc_dointvec
331 .proc_handler
= proc_dointvec_minmax
,
336 # ifdef CONFIG_HAVE_EBPF_JIT
338 .procname
= "bpf_jit_harden",
339 .data
= &bpf_jit_harden
,
340 .maxlen
= sizeof(int),
342 .proc_handler
= proc_dointvec
,
345 .procname
= "bpf_jit_kallsyms",
346 .data
= &bpf_jit_kallsyms
,
347 .maxlen
= sizeof(int),
349 .proc_handler
= proc_dointvec
,
354 .procname
= "netdev_tstamp_prequeue",
355 .data
= &netdev_tstamp_prequeue
,
356 .maxlen
= sizeof(int),
358 .proc_handler
= proc_dointvec
361 .procname
= "message_cost",
362 .data
= &net_ratelimit_state
.interval
,
363 .maxlen
= sizeof(int),
365 .proc_handler
= proc_dointvec_jiffies
,
368 .procname
= "message_burst",
369 .data
= &net_ratelimit_state
.burst
,
370 .maxlen
= sizeof(int),
372 .proc_handler
= proc_dointvec
,
375 .procname
= "optmem_max",
376 .data
= &sysctl_optmem_max
,
377 .maxlen
= sizeof(int),
379 .proc_handler
= proc_dointvec
382 .procname
= "tstamp_allow_data",
383 .data
= &sysctl_tstamp_allow_data
,
384 .maxlen
= sizeof(int),
386 .proc_handler
= proc_dointvec_minmax
,
392 .procname
= "rps_sock_flow_entries",
393 .maxlen
= sizeof(int),
395 .proc_handler
= rps_sock_flow_sysctl
398 #ifdef CONFIG_NET_FLOW_LIMIT
400 .procname
= "flow_limit_cpu_bitmap",
402 .proc_handler
= flow_limit_cpu_sysctl
405 .procname
= "flow_limit_table_len",
406 .data
= &netdev_flow_limit_table_len
,
407 .maxlen
= sizeof(int),
409 .proc_handler
= flow_limit_table_len_sysctl
411 #endif /* CONFIG_NET_FLOW_LIMIT */
412 #ifdef CONFIG_NET_RX_BUSY_POLL
414 .procname
= "busy_poll",
415 .data
= &sysctl_net_busy_poll
,
416 .maxlen
= sizeof(unsigned int),
418 .proc_handler
= proc_dointvec_minmax
,
422 .procname
= "busy_read",
423 .data
= &sysctl_net_busy_read
,
424 .maxlen
= sizeof(unsigned int),
426 .proc_handler
= proc_dointvec_minmax
,
430 #ifdef CONFIG_NET_SCHED
432 .procname
= "default_qdisc",
435 .proc_handler
= set_default_qdisc
438 #endif /* CONFIG_NET */
440 .procname
= "netdev_budget",
441 .data
= &netdev_budget
,
442 .maxlen
= sizeof(int),
444 .proc_handler
= proc_dointvec
447 .procname
= "warnings",
448 .data
= &net_msg_warn
,
449 .maxlen
= sizeof(int),
451 .proc_handler
= proc_dointvec
454 .procname
= "max_skb_frags",
455 .data
= &sysctl_max_skb_frags
,
456 .maxlen
= sizeof(int),
458 .proc_handler
= proc_dointvec_minmax
,
460 .extra2
= &max_skb_frags
,
463 .procname
= "netdev_budget_usecs",
464 .data
= &netdev_budget_usecs
,
465 .maxlen
= sizeof(unsigned int),
467 .proc_handler
= proc_dointvec_minmax
,
473 static struct ctl_table netns_core_table
[] = {
475 .procname
= "somaxconn",
476 .data
= &init_net
.core
.sysctl_somaxconn
,
477 .maxlen
= sizeof(int),
480 .proc_handler
= proc_dointvec_minmax
485 static __net_init
int sysctl_core_net_init(struct net
*net
)
487 struct ctl_table
*tbl
;
489 tbl
= netns_core_table
;
490 if (!net_eq(net
, &init_net
)) {
491 tbl
= kmemdup(tbl
, sizeof(netns_core_table
), GFP_KERNEL
);
495 tbl
[0].data
= &net
->core
.sysctl_somaxconn
;
497 /* Don't export any sysctls to unprivileged users */
498 if (net
->user_ns
!= &init_user_ns
) {
499 tbl
[0].procname
= NULL
;
503 net
->core
.sysctl_hdr
= register_net_sysctl(net
, "net/core", tbl
);
504 if (net
->core
.sysctl_hdr
== NULL
)
510 if (tbl
!= netns_core_table
)
516 static __net_exit
void sysctl_core_net_exit(struct net
*net
)
518 struct ctl_table
*tbl
;
520 tbl
= net
->core
.sysctl_hdr
->ctl_table_arg
;
521 unregister_net_sysctl_table(net
->core
.sysctl_hdr
);
522 BUG_ON(tbl
== netns_core_table
);
526 static __net_initdata
struct pernet_operations sysctl_core_ops
= {
527 .init
= sysctl_core_net_init
,
528 .exit
= sysctl_core_net_exit
,
531 static __init
int sysctl_core_init(void)
533 register_net_sysctl(&init_net
, "net/core", net_core_table
);
534 return register_pernet_subsys(&sysctl_core_ops
);
537 fs_initcall(sysctl_core_init
);