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>
27 static int rps_sock_flow_sysctl(struct ctl_table
*table
, int write
,
28 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
30 unsigned int orig_size
, size
;
32 struct ctl_table tmp
= {
34 .maxlen
= sizeof(size
),
37 struct rps_sock_flow_table
*orig_sock_table
, *sock_table
;
38 static DEFINE_MUTEX(sock_flow_mutex
);
40 mutex_lock(&sock_flow_mutex
);
42 orig_sock_table
= rcu_dereference_protected(rps_sock_flow_table
,
43 lockdep_is_held(&sock_flow_mutex
));
44 size
= orig_size
= orig_sock_table
? orig_sock_table
->mask
+ 1 : 0;
46 ret
= proc_dointvec(&tmp
, write
, buffer
, lenp
, ppos
);
51 /* Enforce limit to prevent overflow */
52 mutex_unlock(&sock_flow_mutex
);
55 size
= roundup_pow_of_two(size
);
56 if (size
!= orig_size
) {
58 vmalloc(RPS_SOCK_FLOW_TABLE_SIZE(size
));
60 mutex_unlock(&sock_flow_mutex
);
64 sock_table
->mask
= size
- 1;
66 sock_table
= orig_sock_table
;
68 for (i
= 0; i
< size
; i
++)
69 sock_table
->ents
[i
] = RPS_NO_CPU
;
73 if (sock_table
!= orig_sock_table
) {
74 rcu_assign_pointer(rps_sock_flow_table
, sock_table
);
76 static_key_slow_inc(&rps_needed
);
77 if (orig_sock_table
) {
78 static_key_slow_dec(&rps_needed
);
80 vfree(orig_sock_table
);
85 mutex_unlock(&sock_flow_mutex
);
89 #endif /* CONFIG_RPS */
91 #ifdef CONFIG_NET_FLOW_LIMIT
92 static DEFINE_MUTEX(flow_limit_update_mutex
);
94 static int flow_limit_cpu_sysctl(struct ctl_table
*table
, int write
,
95 void __user
*buffer
, size_t *lenp
,
98 struct sd_flow_limit
*cur
;
99 struct softnet_data
*sd
;
103 if (!alloc_cpumask_var(&mask
, GFP_KERNEL
))
107 ret
= cpumask_parse_user(buffer
, *lenp
, mask
);
111 mutex_lock(&flow_limit_update_mutex
);
112 len
= sizeof(*cur
) + netdev_flow_limit_table_len
;
113 for_each_possible_cpu(i
) {
114 sd
= &per_cpu(softnet_data
, i
);
115 cur
= rcu_dereference_protected(sd
->flow_limit
,
116 lockdep_is_held(&flow_limit_update_mutex
));
117 if (cur
&& !cpumask_test_cpu(i
, mask
)) {
118 RCU_INIT_POINTER(sd
->flow_limit
, NULL
);
121 } else if (!cur
&& cpumask_test_cpu(i
, mask
)) {
122 cur
= kzalloc(len
, GFP_KERNEL
);
124 /* not unwinding previous changes */
128 cur
->num_buckets
= netdev_flow_limit_table_len
;
129 rcu_assign_pointer(sd
->flow_limit
, cur
);
133 mutex_unlock(&flow_limit_update_mutex
);
137 if (*ppos
|| !*lenp
) {
144 for_each_possible_cpu(i
) {
145 sd
= &per_cpu(softnet_data
, i
);
146 if (rcu_dereference(sd
->flow_limit
))
147 cpumask_set_cpu(i
, mask
);
151 len
= min(sizeof(kbuf
) - 1, *lenp
);
152 len
= cpumask_scnprintf(kbuf
, len
, mask
);
159 if (copy_to_user(buffer
, kbuf
, len
)) {
168 free_cpumask_var(mask
);
172 static int flow_limit_table_len_sysctl(struct ctl_table
*table
, int write
,
173 void __user
*buffer
, size_t *lenp
,
176 unsigned int old
, *ptr
;
179 mutex_lock(&flow_limit_update_mutex
);
183 ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
184 if (!ret
&& write
&& !is_power_of_2(*ptr
)) {
189 mutex_unlock(&flow_limit_update_mutex
);
192 #endif /* CONFIG_NET_FLOW_LIMIT */
194 static struct ctl_table net_core_table
[] = {
197 .procname
= "wmem_max",
198 .data
= &sysctl_wmem_max
,
199 .maxlen
= sizeof(int),
201 .proc_handler
= proc_dointvec_minmax
,
205 .procname
= "rmem_max",
206 .data
= &sysctl_rmem_max
,
207 .maxlen
= sizeof(int),
209 .proc_handler
= proc_dointvec_minmax
,
213 .procname
= "wmem_default",
214 .data
= &sysctl_wmem_default
,
215 .maxlen
= sizeof(int),
217 .proc_handler
= proc_dointvec_minmax
,
221 .procname
= "rmem_default",
222 .data
= &sysctl_rmem_default
,
223 .maxlen
= sizeof(int),
225 .proc_handler
= proc_dointvec_minmax
,
229 .procname
= "dev_weight",
231 .maxlen
= sizeof(int),
233 .proc_handler
= proc_dointvec
236 .procname
= "netdev_max_backlog",
237 .data
= &netdev_max_backlog
,
238 .maxlen
= sizeof(int),
240 .proc_handler
= proc_dointvec
242 #ifdef CONFIG_BPF_JIT
244 .procname
= "bpf_jit_enable",
245 .data
= &bpf_jit_enable
,
246 .maxlen
= sizeof(int),
248 .proc_handler
= proc_dointvec
252 .procname
= "netdev_tstamp_prequeue",
253 .data
= &netdev_tstamp_prequeue
,
254 .maxlen
= sizeof(int),
256 .proc_handler
= proc_dointvec
259 .procname
= "message_cost",
260 .data
= &net_ratelimit_state
.interval
,
261 .maxlen
= sizeof(int),
263 .proc_handler
= proc_dointvec_jiffies
,
266 .procname
= "message_burst",
267 .data
= &net_ratelimit_state
.burst
,
268 .maxlen
= sizeof(int),
270 .proc_handler
= proc_dointvec
,
273 .procname
= "optmem_max",
274 .data
= &sysctl_optmem_max
,
275 .maxlen
= sizeof(int),
277 .proc_handler
= proc_dointvec
281 .procname
= "rps_sock_flow_entries",
282 .maxlen
= sizeof(int),
284 .proc_handler
= rps_sock_flow_sysctl
287 #ifdef CONFIG_NET_FLOW_LIMIT
289 .procname
= "flow_limit_cpu_bitmap",
291 .proc_handler
= flow_limit_cpu_sysctl
294 .procname
= "flow_limit_table_len",
295 .data
= &netdev_flow_limit_table_len
,
296 .maxlen
= sizeof(int),
298 .proc_handler
= flow_limit_table_len_sysctl
300 #endif /* CONFIG_NET_FLOW_LIMIT */
301 #ifdef CONFIG_NET_LL_RX_POLL
303 .procname
= "busy_poll",
304 .data
= &sysctl_net_busy_poll
,
305 .maxlen
= sizeof(unsigned int),
307 .proc_handler
= proc_dointvec
310 .procname
= "busy_read",
311 .data
= &sysctl_net_busy_read
,
312 .maxlen
= sizeof(unsigned int),
314 .proc_handler
= proc_dointvec
318 #endif /* CONFIG_NET */
320 .procname
= "netdev_budget",
321 .data
= &netdev_budget
,
322 .maxlen
= sizeof(int),
324 .proc_handler
= proc_dointvec
327 .procname
= "warnings",
328 .data
= &net_msg_warn
,
329 .maxlen
= sizeof(int),
331 .proc_handler
= proc_dointvec
336 static struct ctl_table netns_core_table
[] = {
338 .procname
= "somaxconn",
339 .data
= &init_net
.core
.sysctl_somaxconn
,
340 .maxlen
= sizeof(int),
342 .proc_handler
= proc_dointvec
347 static __net_init
int sysctl_core_net_init(struct net
*net
)
349 struct ctl_table
*tbl
;
351 net
->core
.sysctl_somaxconn
= SOMAXCONN
;
353 tbl
= netns_core_table
;
354 if (!net_eq(net
, &init_net
)) {
355 tbl
= kmemdup(tbl
, sizeof(netns_core_table
), GFP_KERNEL
);
359 tbl
[0].data
= &net
->core
.sysctl_somaxconn
;
361 /* Don't export any sysctls to unprivileged users */
362 if (net
->user_ns
!= &init_user_ns
) {
363 tbl
[0].procname
= NULL
;
367 net
->core
.sysctl_hdr
= register_net_sysctl(net
, "net/core", tbl
);
368 if (net
->core
.sysctl_hdr
== NULL
)
374 if (tbl
!= netns_core_table
)
380 static __net_exit
void sysctl_core_net_exit(struct net
*net
)
382 struct ctl_table
*tbl
;
384 tbl
= net
->core
.sysctl_hdr
->ctl_table_arg
;
385 unregister_net_sysctl_table(net
->core
.sysctl_hdr
);
386 BUG_ON(tbl
== netns_core_table
);
390 static __net_initdata
struct pernet_operations sysctl_core_ops
= {
391 .init
= sysctl_core_net_init
,
392 .exit
= sysctl_core_net_exit
,
395 static __init
int sysctl_core_init(void)
397 register_net_sysctl(&init_net
, "net/core", net_core_table
);
398 return register_pernet_subsys(&sysctl_core_ops
);
401 fs_initcall(sysctl_core_init
);