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 min_sndbuf
= SOCK_MIN_SNDBUF
;
28 static int min_rcvbuf
= SOCK_MIN_RCVBUF
;
29 static int max_skb_frags
= MAX_SKB_FRAGS
;
31 static int net_msg_warn
; /* Unused, but still a sysctl */
34 static int rps_sock_flow_sysctl(struct ctl_table
*table
, int write
,
35 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
37 unsigned int orig_size
, size
;
39 struct ctl_table tmp
= {
41 .maxlen
= sizeof(size
),
44 struct rps_sock_flow_table
*orig_sock_table
, *sock_table
;
45 static DEFINE_MUTEX(sock_flow_mutex
);
47 mutex_lock(&sock_flow_mutex
);
49 orig_sock_table
= rcu_dereference_protected(rps_sock_flow_table
,
50 lockdep_is_held(&sock_flow_mutex
));
51 size
= orig_size
= orig_sock_table
? orig_sock_table
->mask
+ 1 : 0;
53 ret
= proc_dointvec(&tmp
, write
, buffer
, lenp
, ppos
);
58 /* Enforce limit to prevent overflow */
59 mutex_unlock(&sock_flow_mutex
);
62 size
= roundup_pow_of_two(size
);
63 if (size
!= orig_size
) {
65 vmalloc(RPS_SOCK_FLOW_TABLE_SIZE(size
));
67 mutex_unlock(&sock_flow_mutex
);
70 rps_cpu_mask
= roundup_pow_of_two(nr_cpu_ids
) - 1;
71 sock_table
->mask
= size
- 1;
73 sock_table
= orig_sock_table
;
75 for (i
= 0; i
< size
; i
++)
76 sock_table
->ents
[i
] = RPS_NO_CPU
;
80 if (sock_table
!= orig_sock_table
) {
81 rcu_assign_pointer(rps_sock_flow_table
, sock_table
);
83 static_key_slow_inc(&rps_needed
);
84 if (orig_sock_table
) {
85 static_key_slow_dec(&rps_needed
);
87 vfree(orig_sock_table
);
92 mutex_unlock(&sock_flow_mutex
);
96 #endif /* CONFIG_RPS */
98 #ifdef CONFIG_NET_FLOW_LIMIT
99 static DEFINE_MUTEX(flow_limit_update_mutex
);
101 static int flow_limit_cpu_sysctl(struct ctl_table
*table
, int write
,
102 void __user
*buffer
, size_t *lenp
,
105 struct sd_flow_limit
*cur
;
106 struct softnet_data
*sd
;
110 if (!alloc_cpumask_var(&mask
, GFP_KERNEL
))
114 ret
= cpumask_parse_user(buffer
, *lenp
, mask
);
118 mutex_lock(&flow_limit_update_mutex
);
119 len
= sizeof(*cur
) + netdev_flow_limit_table_len
;
120 for_each_possible_cpu(i
) {
121 sd
= &per_cpu(softnet_data
, i
);
122 cur
= rcu_dereference_protected(sd
->flow_limit
,
123 lockdep_is_held(&flow_limit_update_mutex
));
124 if (cur
&& !cpumask_test_cpu(i
, mask
)) {
125 RCU_INIT_POINTER(sd
->flow_limit
, NULL
);
128 } else if (!cur
&& cpumask_test_cpu(i
, mask
)) {
129 cur
= kzalloc_node(len
, GFP_KERNEL
,
132 /* not unwinding previous changes */
136 cur
->num_buckets
= netdev_flow_limit_table_len
;
137 rcu_assign_pointer(sd
->flow_limit
, cur
);
141 mutex_unlock(&flow_limit_update_mutex
);
145 if (*ppos
|| !*lenp
) {
152 for_each_possible_cpu(i
) {
153 sd
= &per_cpu(softnet_data
, i
);
154 if (rcu_dereference(sd
->flow_limit
))
155 cpumask_set_cpu(i
, mask
);
159 len
= min(sizeof(kbuf
) - 1, *lenp
);
160 len
= scnprintf(kbuf
, len
, "%*pb", cpumask_pr_args(mask
));
167 if (copy_to_user(buffer
, kbuf
, len
)) {
176 free_cpumask_var(mask
);
180 static int flow_limit_table_len_sysctl(struct ctl_table
*table
, int write
,
181 void __user
*buffer
, size_t *lenp
,
184 unsigned int old
, *ptr
;
187 mutex_lock(&flow_limit_update_mutex
);
191 ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
192 if (!ret
&& write
&& !is_power_of_2(*ptr
)) {
197 mutex_unlock(&flow_limit_update_mutex
);
200 #endif /* CONFIG_NET_FLOW_LIMIT */
202 #ifdef CONFIG_NET_SCHED
203 static int set_default_qdisc(struct ctl_table
*table
, int write
,
204 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
207 struct ctl_table tbl
= {
213 qdisc_get_default(id
, IFNAMSIZ
);
215 ret
= proc_dostring(&tbl
, write
, buffer
, lenp
, ppos
);
216 if (write
&& ret
== 0)
217 ret
= qdisc_set_default(id
);
222 static int proc_do_rss_key(struct ctl_table
*table
, int write
,
223 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
225 struct ctl_table fake_table
;
226 char buf
[NETDEV_RSS_KEY_LEN
* 3];
228 snprintf(buf
, sizeof(buf
), "%*phC", NETDEV_RSS_KEY_LEN
, netdev_rss_key
);
229 fake_table
.data
= buf
;
230 fake_table
.maxlen
= sizeof(buf
);
231 return proc_dostring(&fake_table
, write
, buffer
, lenp
, ppos
);
234 static struct ctl_table net_core_table
[] = {
237 .procname
= "wmem_max",
238 .data
= &sysctl_wmem_max
,
239 .maxlen
= sizeof(int),
241 .proc_handler
= proc_dointvec_minmax
,
242 .extra1
= &min_sndbuf
,
245 .procname
= "rmem_max",
246 .data
= &sysctl_rmem_max
,
247 .maxlen
= sizeof(int),
249 .proc_handler
= proc_dointvec_minmax
,
250 .extra1
= &min_rcvbuf
,
253 .procname
= "wmem_default",
254 .data
= &sysctl_wmem_default
,
255 .maxlen
= sizeof(int),
257 .proc_handler
= proc_dointvec_minmax
,
258 .extra1
= &min_sndbuf
,
261 .procname
= "rmem_default",
262 .data
= &sysctl_rmem_default
,
263 .maxlen
= sizeof(int),
265 .proc_handler
= proc_dointvec_minmax
,
266 .extra1
= &min_rcvbuf
,
269 .procname
= "dev_weight",
271 .maxlen
= sizeof(int),
273 .proc_handler
= proc_dointvec
276 .procname
= "netdev_max_backlog",
277 .data
= &netdev_max_backlog
,
278 .maxlen
= sizeof(int),
280 .proc_handler
= proc_dointvec
283 .procname
= "netdev_rss_key",
284 .data
= &netdev_rss_key
,
285 .maxlen
= sizeof(int),
287 .proc_handler
= proc_do_rss_key
,
289 #ifdef CONFIG_BPF_JIT
291 .procname
= "bpf_jit_enable",
292 .data
= &bpf_jit_enable
,
293 .maxlen
= sizeof(int),
295 .proc_handler
= proc_dointvec
297 # ifdef CONFIG_HAVE_EBPF_JIT
299 .procname
= "bpf_jit_harden",
300 .data
= &bpf_jit_harden
,
301 .maxlen
= sizeof(int),
303 .proc_handler
= proc_dointvec
,
308 .procname
= "netdev_tstamp_prequeue",
309 .data
= &netdev_tstamp_prequeue
,
310 .maxlen
= sizeof(int),
312 .proc_handler
= proc_dointvec
315 .procname
= "message_cost",
316 .data
= &net_ratelimit_state
.interval
,
317 .maxlen
= sizeof(int),
319 .proc_handler
= proc_dointvec_jiffies
,
322 .procname
= "message_burst",
323 .data
= &net_ratelimit_state
.burst
,
324 .maxlen
= sizeof(int),
326 .proc_handler
= proc_dointvec
,
329 .procname
= "optmem_max",
330 .data
= &sysctl_optmem_max
,
331 .maxlen
= sizeof(int),
333 .proc_handler
= proc_dointvec
336 .procname
= "tstamp_allow_data",
337 .data
= &sysctl_tstamp_allow_data
,
338 .maxlen
= sizeof(int),
340 .proc_handler
= proc_dointvec_minmax
,
346 .procname
= "rps_sock_flow_entries",
347 .maxlen
= sizeof(int),
349 .proc_handler
= rps_sock_flow_sysctl
352 #ifdef CONFIG_NET_FLOW_LIMIT
354 .procname
= "flow_limit_cpu_bitmap",
356 .proc_handler
= flow_limit_cpu_sysctl
359 .procname
= "flow_limit_table_len",
360 .data
= &netdev_flow_limit_table_len
,
361 .maxlen
= sizeof(int),
363 .proc_handler
= flow_limit_table_len_sysctl
365 #endif /* CONFIG_NET_FLOW_LIMIT */
366 #ifdef CONFIG_NET_RX_BUSY_POLL
368 .procname
= "busy_poll",
369 .data
= &sysctl_net_busy_poll
,
370 .maxlen
= sizeof(unsigned int),
372 .proc_handler
= proc_dointvec
375 .procname
= "busy_read",
376 .data
= &sysctl_net_busy_read
,
377 .maxlen
= sizeof(unsigned int),
379 .proc_handler
= proc_dointvec
382 #ifdef CONFIG_NET_SCHED
384 .procname
= "default_qdisc",
387 .proc_handler
= set_default_qdisc
390 #endif /* CONFIG_NET */
392 .procname
= "netdev_budget",
393 .data
= &netdev_budget
,
394 .maxlen
= sizeof(int),
396 .proc_handler
= proc_dointvec
399 .procname
= "warnings",
400 .data
= &net_msg_warn
,
401 .maxlen
= sizeof(int),
403 .proc_handler
= proc_dointvec
406 .procname
= "max_skb_frags",
407 .data
= &sysctl_max_skb_frags
,
408 .maxlen
= sizeof(int),
410 .proc_handler
= proc_dointvec_minmax
,
412 .extra2
= &max_skb_frags
,
417 static struct ctl_table netns_core_table
[] = {
419 .procname
= "somaxconn",
420 .data
= &init_net
.core
.sysctl_somaxconn
,
421 .maxlen
= sizeof(int),
424 .proc_handler
= proc_dointvec_minmax
429 static __net_init
int sysctl_core_net_init(struct net
*net
)
431 struct ctl_table
*tbl
;
433 net
->core
.sysctl_somaxconn
= SOMAXCONN
;
435 tbl
= netns_core_table
;
436 if (!net_eq(net
, &init_net
)) {
437 tbl
= kmemdup(tbl
, sizeof(netns_core_table
), GFP_KERNEL
);
441 tbl
[0].data
= &net
->core
.sysctl_somaxconn
;
443 /* Don't export any sysctls to unprivileged users */
444 if (net
->user_ns
!= &init_user_ns
) {
445 tbl
[0].procname
= NULL
;
449 net
->core
.sysctl_hdr
= register_net_sysctl(net
, "net/core", tbl
);
450 if (net
->core
.sysctl_hdr
== NULL
)
456 if (tbl
!= netns_core_table
)
462 static __net_exit
void sysctl_core_net_exit(struct net
*net
)
464 struct ctl_table
*tbl
;
466 tbl
= net
->core
.sysctl_hdr
->ctl_table_arg
;
467 unregister_net_sysctl_table(net
->core
.sysctl_hdr
);
468 BUG_ON(tbl
== netns_core_table
);
472 static __net_initdata
struct pernet_operations sysctl_core_ops
= {
473 .init
= sysctl_core_net_init
,
474 .exit
= sysctl_core_net_exit
,
477 static __init
int sysctl_core_init(void)
479 register_net_sysctl(&init_net
, "net/core", net_core_table
);
480 return register_pernet_subsys(&sysctl_core_ops
);
483 fs_initcall(sysctl_core_init
);