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 static_key_slow_inc(&rfs_needed
);
86 if (orig_sock_table
) {
87 static_key_slow_dec(&rps_needed
);
88 static_key_slow_dec(&rfs_needed
);
90 vfree(orig_sock_table
);
95 mutex_unlock(&sock_flow_mutex
);
99 #endif /* CONFIG_RPS */
101 #ifdef CONFIG_NET_FLOW_LIMIT
102 static DEFINE_MUTEX(flow_limit_update_mutex
);
104 static int flow_limit_cpu_sysctl(struct ctl_table
*table
, int write
,
105 void __user
*buffer
, size_t *lenp
,
108 struct sd_flow_limit
*cur
;
109 struct softnet_data
*sd
;
113 if (!alloc_cpumask_var(&mask
, GFP_KERNEL
))
117 ret
= cpumask_parse_user(buffer
, *lenp
, mask
);
121 mutex_lock(&flow_limit_update_mutex
);
122 len
= sizeof(*cur
) + netdev_flow_limit_table_len
;
123 for_each_possible_cpu(i
) {
124 sd
= &per_cpu(softnet_data
, i
);
125 cur
= rcu_dereference_protected(sd
->flow_limit
,
126 lockdep_is_held(&flow_limit_update_mutex
));
127 if (cur
&& !cpumask_test_cpu(i
, mask
)) {
128 RCU_INIT_POINTER(sd
->flow_limit
, NULL
);
131 } else if (!cur
&& cpumask_test_cpu(i
, mask
)) {
132 cur
= kzalloc_node(len
, GFP_KERNEL
,
135 /* not unwinding previous changes */
139 cur
->num_buckets
= netdev_flow_limit_table_len
;
140 rcu_assign_pointer(sd
->flow_limit
, cur
);
144 mutex_unlock(&flow_limit_update_mutex
);
148 if (*ppos
|| !*lenp
) {
155 for_each_possible_cpu(i
) {
156 sd
= &per_cpu(softnet_data
, i
);
157 if (rcu_dereference(sd
->flow_limit
))
158 cpumask_set_cpu(i
, mask
);
162 len
= min(sizeof(kbuf
) - 1, *lenp
);
163 len
= scnprintf(kbuf
, len
, "%*pb", cpumask_pr_args(mask
));
170 if (copy_to_user(buffer
, kbuf
, len
)) {
179 free_cpumask_var(mask
);
183 static int flow_limit_table_len_sysctl(struct ctl_table
*table
, int write
,
184 void __user
*buffer
, size_t *lenp
,
187 unsigned int old
, *ptr
;
190 mutex_lock(&flow_limit_update_mutex
);
194 ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
195 if (!ret
&& write
&& !is_power_of_2(*ptr
)) {
200 mutex_unlock(&flow_limit_update_mutex
);
203 #endif /* CONFIG_NET_FLOW_LIMIT */
205 #ifdef CONFIG_NET_SCHED
206 static int set_default_qdisc(struct ctl_table
*table
, int write
,
207 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
210 struct ctl_table tbl
= {
216 qdisc_get_default(id
, IFNAMSIZ
);
218 ret
= proc_dostring(&tbl
, write
, buffer
, lenp
, ppos
);
219 if (write
&& ret
== 0)
220 ret
= qdisc_set_default(id
);
225 static int proc_do_rss_key(struct ctl_table
*table
, int write
,
226 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
228 struct ctl_table fake_table
;
229 char buf
[NETDEV_RSS_KEY_LEN
* 3];
231 snprintf(buf
, sizeof(buf
), "%*phC", NETDEV_RSS_KEY_LEN
, netdev_rss_key
);
232 fake_table
.data
= buf
;
233 fake_table
.maxlen
= sizeof(buf
);
234 return proc_dostring(&fake_table
, write
, buffer
, lenp
, ppos
);
237 static struct ctl_table net_core_table
[] = {
240 .procname
= "wmem_max",
241 .data
= &sysctl_wmem_max
,
242 .maxlen
= sizeof(int),
244 .proc_handler
= proc_dointvec_minmax
,
245 .extra1
= &min_sndbuf
,
248 .procname
= "rmem_max",
249 .data
= &sysctl_rmem_max
,
250 .maxlen
= sizeof(int),
252 .proc_handler
= proc_dointvec_minmax
,
253 .extra1
= &min_rcvbuf
,
256 .procname
= "wmem_default",
257 .data
= &sysctl_wmem_default
,
258 .maxlen
= sizeof(int),
260 .proc_handler
= proc_dointvec_minmax
,
261 .extra1
= &min_sndbuf
,
264 .procname
= "rmem_default",
265 .data
= &sysctl_rmem_default
,
266 .maxlen
= sizeof(int),
268 .proc_handler
= proc_dointvec_minmax
,
269 .extra1
= &min_rcvbuf
,
272 .procname
= "dev_weight",
274 .maxlen
= sizeof(int),
276 .proc_handler
= proc_dointvec
279 .procname
= "netdev_max_backlog",
280 .data
= &netdev_max_backlog
,
281 .maxlen
= sizeof(int),
283 .proc_handler
= proc_dointvec
286 .procname
= "netdev_rss_key",
287 .data
= &netdev_rss_key
,
288 .maxlen
= sizeof(int),
290 .proc_handler
= proc_do_rss_key
,
292 #ifdef CONFIG_BPF_JIT
294 .procname
= "bpf_jit_enable",
295 .data
= &bpf_jit_enable
,
296 .maxlen
= sizeof(int),
298 .proc_handler
= proc_dointvec
300 # ifdef CONFIG_HAVE_EBPF_JIT
302 .procname
= "bpf_jit_harden",
303 .data
= &bpf_jit_harden
,
304 .maxlen
= sizeof(int),
306 .proc_handler
= proc_dointvec
,
311 .procname
= "netdev_tstamp_prequeue",
312 .data
= &netdev_tstamp_prequeue
,
313 .maxlen
= sizeof(int),
315 .proc_handler
= proc_dointvec
318 .procname
= "message_cost",
319 .data
= &net_ratelimit_state
.interval
,
320 .maxlen
= sizeof(int),
322 .proc_handler
= proc_dointvec_jiffies
,
325 .procname
= "message_burst",
326 .data
= &net_ratelimit_state
.burst
,
327 .maxlen
= sizeof(int),
329 .proc_handler
= proc_dointvec
,
332 .procname
= "optmem_max",
333 .data
= &sysctl_optmem_max
,
334 .maxlen
= sizeof(int),
336 .proc_handler
= proc_dointvec
339 .procname
= "tstamp_allow_data",
340 .data
= &sysctl_tstamp_allow_data
,
341 .maxlen
= sizeof(int),
343 .proc_handler
= proc_dointvec_minmax
,
349 .procname
= "rps_sock_flow_entries",
350 .maxlen
= sizeof(int),
352 .proc_handler
= rps_sock_flow_sysctl
355 #ifdef CONFIG_NET_FLOW_LIMIT
357 .procname
= "flow_limit_cpu_bitmap",
359 .proc_handler
= flow_limit_cpu_sysctl
362 .procname
= "flow_limit_table_len",
363 .data
= &netdev_flow_limit_table_len
,
364 .maxlen
= sizeof(int),
366 .proc_handler
= flow_limit_table_len_sysctl
368 #endif /* CONFIG_NET_FLOW_LIMIT */
369 #ifdef CONFIG_NET_RX_BUSY_POLL
371 .procname
= "busy_poll",
372 .data
= &sysctl_net_busy_poll
,
373 .maxlen
= sizeof(unsigned int),
375 .proc_handler
= proc_dointvec
378 .procname
= "busy_read",
379 .data
= &sysctl_net_busy_read
,
380 .maxlen
= sizeof(unsigned int),
382 .proc_handler
= proc_dointvec
385 #ifdef CONFIG_NET_SCHED
387 .procname
= "default_qdisc",
390 .proc_handler
= set_default_qdisc
393 #endif /* CONFIG_NET */
395 .procname
= "netdev_budget",
396 .data
= &netdev_budget
,
397 .maxlen
= sizeof(int),
399 .proc_handler
= proc_dointvec
402 .procname
= "warnings",
403 .data
= &net_msg_warn
,
404 .maxlen
= sizeof(int),
406 .proc_handler
= proc_dointvec
409 .procname
= "max_skb_frags",
410 .data
= &sysctl_max_skb_frags
,
411 .maxlen
= sizeof(int),
413 .proc_handler
= proc_dointvec_minmax
,
415 .extra2
= &max_skb_frags
,
420 static struct ctl_table netns_core_table
[] = {
422 .procname
= "somaxconn",
423 .data
= &init_net
.core
.sysctl_somaxconn
,
424 .maxlen
= sizeof(int),
427 .proc_handler
= proc_dointvec_minmax
432 static __net_init
int sysctl_core_net_init(struct net
*net
)
434 struct ctl_table
*tbl
;
436 net
->core
.sysctl_somaxconn
= SOMAXCONN
;
438 tbl
= netns_core_table
;
439 if (!net_eq(net
, &init_net
)) {
440 tbl
= kmemdup(tbl
, sizeof(netns_core_table
), GFP_KERNEL
);
444 tbl
[0].data
= &net
->core
.sysctl_somaxconn
;
446 /* Don't export any sysctls to unprivileged users */
447 if (net
->user_ns
!= &init_user_ns
) {
448 tbl
[0].procname
= NULL
;
452 net
->core
.sysctl_hdr
= register_net_sysctl(net
, "net/core", tbl
);
453 if (net
->core
.sysctl_hdr
== NULL
)
459 if (tbl
!= netns_core_table
)
465 static __net_exit
void sysctl_core_net_exit(struct net
*net
)
467 struct ctl_table
*tbl
;
469 tbl
= net
->core
.sysctl_hdr
->ctl_table_arg
;
470 unregister_net_sysctl_table(net
->core
.sysctl_hdr
);
471 BUG_ON(tbl
== netns_core_table
);
475 static __net_initdata
struct pernet_operations sysctl_core_ops
= {
476 .init
= sysctl_core_net_init
,
477 .exit
= sysctl_core_net_exit
,
480 static __init
int sysctl_core_init(void)
482 register_net_sysctl(&init_net
, "net/core", net_core_table
);
483 return register_pernet_subsys(&sysctl_core_ops
);
486 fs_initcall(sysctl_core_init
);