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;
30 static int net_msg_warn; /* Unused, but still a sysctl */
33 static int rps_sock_flow_sysctl(struct ctl_table *table, int write,
34 void __user *buffer, size_t *lenp, loff_t *ppos)
36 unsigned int orig_size, size;
38 struct ctl_table tmp = {
40 .maxlen = sizeof(size),
43 struct rps_sock_flow_table *orig_sock_table, *sock_table;
44 static DEFINE_MUTEX(sock_flow_mutex);
46 mutex_lock(&sock_flow_mutex);
48 orig_sock_table = rcu_dereference_protected(rps_sock_flow_table,
49 lockdep_is_held(&sock_flow_mutex));
50 size = orig_size = orig_sock_table ? orig_sock_table->mask + 1 : 0;
52 ret = proc_dointvec(&tmp, write, buffer, lenp, ppos);
57 /* Enforce limit to prevent overflow */
58 mutex_unlock(&sock_flow_mutex);
61 size = roundup_pow_of_two(size);
62 if (size != orig_size) {
64 vmalloc(RPS_SOCK_FLOW_TABLE_SIZE(size));
66 mutex_unlock(&sock_flow_mutex);
69 rps_cpu_mask = roundup_pow_of_two(nr_cpu_ids) - 1;
70 sock_table->mask = size - 1;
72 sock_table = orig_sock_table;
74 for (i = 0; i < size; i++)
75 sock_table->ents[i] = RPS_NO_CPU;
79 if (sock_table != orig_sock_table) {
80 rcu_assign_pointer(rps_sock_flow_table, sock_table);
82 static_key_slow_inc(&rps_needed);
83 if (orig_sock_table) {
84 static_key_slow_dec(&rps_needed);
86 vfree(orig_sock_table);
91 mutex_unlock(&sock_flow_mutex);
95 #endif /* CONFIG_RPS */
97 #ifdef CONFIG_NET_FLOW_LIMIT
98 static DEFINE_MUTEX(flow_limit_update_mutex);
100 static int flow_limit_cpu_sysctl(struct ctl_table *table, int write,
101 void __user *buffer, size_t *lenp,
104 struct sd_flow_limit *cur;
105 struct softnet_data *sd;
109 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
113 ret = cpumask_parse_user(buffer, *lenp, mask);
117 mutex_lock(&flow_limit_update_mutex);
118 len = sizeof(*cur) + netdev_flow_limit_table_len;
119 for_each_possible_cpu(i) {
120 sd = &per_cpu(softnet_data, i);
121 cur = rcu_dereference_protected(sd->flow_limit,
122 lockdep_is_held(&flow_limit_update_mutex));
123 if (cur && !cpumask_test_cpu(i, mask)) {
124 RCU_INIT_POINTER(sd->flow_limit, NULL);
127 } else if (!cur && cpumask_test_cpu(i, mask)) {
128 cur = kzalloc_node(len, GFP_KERNEL,
131 /* not unwinding previous changes */
135 cur->num_buckets = netdev_flow_limit_table_len;
136 rcu_assign_pointer(sd->flow_limit, cur);
140 mutex_unlock(&flow_limit_update_mutex);
144 if (*ppos || !*lenp) {
151 for_each_possible_cpu(i) {
152 sd = &per_cpu(softnet_data, i);
153 if (rcu_dereference(sd->flow_limit))
154 cpumask_set_cpu(i, mask);
158 len = min(sizeof(kbuf) - 1, *lenp);
159 len = scnprintf(kbuf, len, "%*pb", cpumask_pr_args(mask));
166 if (copy_to_user(buffer, kbuf, len)) {
175 free_cpumask_var(mask);
179 static int flow_limit_table_len_sysctl(struct ctl_table *table, int write,
180 void __user *buffer, size_t *lenp,
183 unsigned int old, *ptr;
186 mutex_lock(&flow_limit_update_mutex);
190 ret = proc_dointvec(table, write, buffer, lenp, ppos);
191 if (!ret && write && !is_power_of_2(*ptr)) {
196 mutex_unlock(&flow_limit_update_mutex);
199 #endif /* CONFIG_NET_FLOW_LIMIT */
201 #ifdef CONFIG_NET_SCHED
202 static int set_default_qdisc(struct ctl_table *table, int write,
203 void __user *buffer, size_t *lenp, loff_t *ppos)
206 struct ctl_table tbl = {
212 qdisc_get_default(id, IFNAMSIZ);
214 ret = proc_dostring(&tbl, write, buffer, lenp, ppos);
215 if (write && ret == 0)
216 ret = qdisc_set_default(id);
221 static int proc_do_rss_key(struct ctl_table *table, int write,
222 void __user *buffer, size_t *lenp, loff_t *ppos)
224 struct ctl_table fake_table;
225 char buf[NETDEV_RSS_KEY_LEN * 3];
227 snprintf(buf, sizeof(buf), "%*phC", NETDEV_RSS_KEY_LEN, netdev_rss_key);
228 fake_table.data = buf;
229 fake_table.maxlen = sizeof(buf);
230 return proc_dostring(&fake_table, write, buffer, lenp, ppos);
233 static struct ctl_table net_core_table[] = {
236 .procname = "wmem_max",
237 .data = &sysctl_wmem_max,
238 .maxlen = sizeof(int),
240 .proc_handler = proc_dointvec_minmax,
241 .extra1 = &min_sndbuf,
244 .procname = "rmem_max",
245 .data = &sysctl_rmem_max,
246 .maxlen = sizeof(int),
248 .proc_handler = proc_dointvec_minmax,
249 .extra1 = &min_rcvbuf,
252 .procname = "wmem_default",
253 .data = &sysctl_wmem_default,
254 .maxlen = sizeof(int),
256 .proc_handler = proc_dointvec_minmax,
257 .extra1 = &min_sndbuf,
260 .procname = "rmem_default",
261 .data = &sysctl_rmem_default,
262 .maxlen = sizeof(int),
264 .proc_handler = proc_dointvec_minmax,
265 .extra1 = &min_rcvbuf,
268 .procname = "dev_weight",
270 .maxlen = sizeof(int),
272 .proc_handler = proc_dointvec
275 .procname = "netdev_max_backlog",
276 .data = &netdev_max_backlog,
277 .maxlen = sizeof(int),
279 .proc_handler = proc_dointvec
282 .procname = "netdev_rss_key",
283 .data = &netdev_rss_key,
284 .maxlen = sizeof(int),
286 .proc_handler = proc_do_rss_key,
288 #ifdef CONFIG_BPF_JIT
290 .procname = "bpf_jit_enable",
291 .data = &bpf_jit_enable,
292 .maxlen = sizeof(int),
294 .proc_handler = proc_dointvec
298 .procname = "netdev_tstamp_prequeue",
299 .data = &netdev_tstamp_prequeue,
300 .maxlen = sizeof(int),
302 .proc_handler = proc_dointvec
305 .procname = "message_cost",
306 .data = &net_ratelimit_state.interval,
307 .maxlen = sizeof(int),
309 .proc_handler = proc_dointvec_jiffies,
312 .procname = "message_burst",
313 .data = &net_ratelimit_state.burst,
314 .maxlen = sizeof(int),
316 .proc_handler = proc_dointvec,
319 .procname = "optmem_max",
320 .data = &sysctl_optmem_max,
321 .maxlen = sizeof(int),
323 .proc_handler = proc_dointvec
326 .procname = "tstamp_allow_data",
327 .data = &sysctl_tstamp_allow_data,
328 .maxlen = sizeof(int),
330 .proc_handler = proc_dointvec_minmax,
336 .procname = "rps_sock_flow_entries",
337 .maxlen = sizeof(int),
339 .proc_handler = rps_sock_flow_sysctl
342 #ifdef CONFIG_NET_FLOW_LIMIT
344 .procname = "flow_limit_cpu_bitmap",
346 .proc_handler = flow_limit_cpu_sysctl
349 .procname = "flow_limit_table_len",
350 .data = &netdev_flow_limit_table_len,
351 .maxlen = sizeof(int),
353 .proc_handler = flow_limit_table_len_sysctl
355 #endif /* CONFIG_NET_FLOW_LIMIT */
356 #ifdef CONFIG_NET_RX_BUSY_POLL
358 .procname = "busy_poll",
359 .data = &sysctl_net_busy_poll,
360 .maxlen = sizeof(unsigned int),
362 .proc_handler = proc_dointvec
365 .procname = "busy_read",
366 .data = &sysctl_net_busy_read,
367 .maxlen = sizeof(unsigned int),
369 .proc_handler = proc_dointvec
372 #ifdef CONFIG_NET_SCHED
374 .procname = "default_qdisc",
377 .proc_handler = set_default_qdisc
380 #endif /* CONFIG_NET */
382 .procname = "netdev_budget",
383 .data = &netdev_budget,
384 .maxlen = sizeof(int),
386 .proc_handler = proc_dointvec
389 .procname = "warnings",
390 .data = &net_msg_warn,
391 .maxlen = sizeof(int),
393 .proc_handler = proc_dointvec
398 static struct ctl_table netns_core_table[] = {
400 .procname = "somaxconn",
401 .data = &init_net.core.sysctl_somaxconn,
402 .maxlen = sizeof(int),
405 .proc_handler = proc_dointvec_minmax
410 static __net_init int sysctl_core_net_init(struct net *net)
412 struct ctl_table *tbl;
414 net->core.sysctl_somaxconn = SOMAXCONN;
416 tbl = netns_core_table;
417 if (!net_eq(net, &init_net)) {
418 tbl = kmemdup(tbl, sizeof(netns_core_table), GFP_KERNEL);
422 tbl[0].data = &net->core.sysctl_somaxconn;
424 /* Don't export any sysctls to unprivileged users */
425 if (net->user_ns != &init_user_ns) {
426 tbl[0].procname = NULL;
430 net->core.sysctl_hdr = register_net_sysctl(net, "net/core", tbl);
431 if (net->core.sysctl_hdr == NULL)
437 if (tbl != netns_core_table)
443 static __net_exit void sysctl_core_net_exit(struct net *net)
445 struct ctl_table *tbl;
447 tbl = net->core.sysctl_hdr->ctl_table_arg;
448 unregister_net_sysctl_table(net->core.sysctl_hdr);
449 BUG_ON(tbl == netns_core_table);
453 static __net_initdata struct pernet_operations sysctl_core_ops = {
454 .init = sysctl_core_net_init,
455 .exit = sysctl_core_net_exit,
458 static __init int sysctl_core_init(void)
460 register_net_sysctl(&init_net, "net/core", net_core_table);
461 return register_pernet_subsys(&sysctl_core_ops);
464 fs_initcall(sysctl_core_init);