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]
9 #include <linux/filter.h>
11 #include <linux/sysctl.h>
12 #include <linux/module.h>
13 #include <linux/socket.h>
14 #include <linux/netdevice.h>
15 #include <linux/ratelimit.h>
16 #include <linux/vmalloc.h>
17 #include <linux/init.h>
18 #include <linux/slab.h>
19 #include <linux/sched/isolation.h>
23 #include <net/net_ratelimit.h>
24 #include <net/busy_poll.h>
25 #include <net/pkt_sched.h>
29 static int int_3600 = 3600;
30 static int min_sndbuf = SOCK_MIN_SNDBUF;
31 static int min_rcvbuf = SOCK_MIN_RCVBUF;
32 static int max_skb_frags = MAX_SKB_FRAGS;
33 static int min_mem_pcpu_rsv = SK_MEMORY_PCPU_RESERVE;
35 static int net_msg_warn; /* Unused, but still a sysctl */
37 int sysctl_fb_tunnels_only_for_init_net __read_mostly = 0;
38 EXPORT_SYMBOL(sysctl_fb_tunnels_only_for_init_net);
40 /* 0 - Keep current behavior:
41 * IPv4: inherit all current settings from init_net
42 * IPv6: reset all settings to default
43 * 1 - Both inherit all current settings from init_net
44 * 2 - Both reset all settings to default
45 * 3 - Both inherit all settings from current netns
47 int sysctl_devconf_inherit_init_net __read_mostly;
48 EXPORT_SYMBOL(sysctl_devconf_inherit_init_net);
50 #if IS_ENABLED(CONFIG_NET_FLOW_LIMIT) || IS_ENABLED(CONFIG_RPS)
51 static void dump_cpumask(void *buffer, size_t *lenp, loff_t *ppos,
57 if (*ppos || !*lenp) {
62 len = min(sizeof(kbuf) - 1, *lenp);
63 len = scnprintf(kbuf, len, "%*pb", cpumask_pr_args(mask));
71 memcpy(buffer, kbuf, len);
79 static struct cpumask *rps_default_mask_cow_alloc(struct net *net)
81 struct cpumask *rps_default_mask;
83 if (net->core.rps_default_mask)
84 return net->core.rps_default_mask;
86 rps_default_mask = kzalloc(cpumask_size(), GFP_KERNEL);
87 if (!rps_default_mask)
90 /* pairs with READ_ONCE in rx_queue_default_mask() */
91 WRITE_ONCE(net->core.rps_default_mask, rps_default_mask);
92 return rps_default_mask;
95 static int rps_default_mask_sysctl(struct ctl_table *table, int write,
96 void *buffer, size_t *lenp, loff_t *ppos)
98 struct net *net = (struct net *)table->data;
103 struct cpumask *rps_default_mask = rps_default_mask_cow_alloc(net);
106 if (!rps_default_mask)
109 err = cpumask_parse(buffer, rps_default_mask);
113 err = rps_cpumask_housekeeping(rps_default_mask);
117 dump_cpumask(buffer, lenp, ppos,
118 net->core.rps_default_mask ? : cpu_none_mask);
126 static int rps_sock_flow_sysctl(struct ctl_table *table, int write,
127 void *buffer, size_t *lenp, loff_t *ppos)
129 unsigned int orig_size, size;
131 struct ctl_table tmp = {
133 .maxlen = sizeof(size),
136 struct rps_sock_flow_table *orig_sock_table, *sock_table;
137 static DEFINE_MUTEX(sock_flow_mutex);
139 mutex_lock(&sock_flow_mutex);
141 orig_sock_table = rcu_dereference_protected(rps_sock_flow_table,
142 lockdep_is_held(&sock_flow_mutex));
143 size = orig_size = orig_sock_table ? orig_sock_table->mask + 1 : 0;
145 ret = proc_dointvec(&tmp, write, buffer, lenp, ppos);
150 /* Enforce limit to prevent overflow */
151 mutex_unlock(&sock_flow_mutex);
154 size = roundup_pow_of_two(size);
155 if (size != orig_size) {
157 vmalloc(RPS_SOCK_FLOW_TABLE_SIZE(size));
159 mutex_unlock(&sock_flow_mutex);
162 rps_cpu_mask = roundup_pow_of_two(nr_cpu_ids) - 1;
163 sock_table->mask = size - 1;
165 sock_table = orig_sock_table;
167 for (i = 0; i < size; i++)
168 sock_table->ents[i] = RPS_NO_CPU;
172 if (sock_table != orig_sock_table) {
173 rcu_assign_pointer(rps_sock_flow_table, sock_table);
175 static_branch_inc(&rps_needed);
176 static_branch_inc(&rfs_needed);
178 if (orig_sock_table) {
179 static_branch_dec(&rps_needed);
180 static_branch_dec(&rfs_needed);
181 kvfree_rcu_mightsleep(orig_sock_table);
186 mutex_unlock(&sock_flow_mutex);
190 #endif /* CONFIG_RPS */
192 #ifdef CONFIG_NET_FLOW_LIMIT
193 static DEFINE_MUTEX(flow_limit_update_mutex);
195 static int flow_limit_cpu_sysctl(struct ctl_table *table, int write,
196 void *buffer, size_t *lenp, loff_t *ppos)
198 struct sd_flow_limit *cur;
199 struct softnet_data *sd;
203 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
207 ret = cpumask_parse(buffer, mask);
211 mutex_lock(&flow_limit_update_mutex);
212 len = sizeof(*cur) + netdev_flow_limit_table_len;
213 for_each_possible_cpu(i) {
214 sd = &per_cpu(softnet_data, i);
215 cur = rcu_dereference_protected(sd->flow_limit,
216 lockdep_is_held(&flow_limit_update_mutex));
217 if (cur && !cpumask_test_cpu(i, mask)) {
218 RCU_INIT_POINTER(sd->flow_limit, NULL);
219 kfree_rcu_mightsleep(cur);
220 } else if (!cur && cpumask_test_cpu(i, mask)) {
221 cur = kzalloc_node(len, GFP_KERNEL,
224 /* not unwinding previous changes */
228 cur->num_buckets = netdev_flow_limit_table_len;
229 rcu_assign_pointer(sd->flow_limit, cur);
233 mutex_unlock(&flow_limit_update_mutex);
237 for_each_possible_cpu(i) {
238 sd = &per_cpu(softnet_data, i);
239 if (rcu_dereference(sd->flow_limit))
240 cpumask_set_cpu(i, mask);
244 dump_cpumask(buffer, lenp, ppos, mask);
248 free_cpumask_var(mask);
252 static int flow_limit_table_len_sysctl(struct ctl_table *table, int write,
253 void *buffer, size_t *lenp, loff_t *ppos)
255 unsigned int old, *ptr;
258 mutex_lock(&flow_limit_update_mutex);
262 ret = proc_dointvec(table, write, buffer, lenp, ppos);
263 if (!ret && write && !is_power_of_2(*ptr)) {
268 mutex_unlock(&flow_limit_update_mutex);
271 #endif /* CONFIG_NET_FLOW_LIMIT */
273 #ifdef CONFIG_NET_SCHED
274 static int set_default_qdisc(struct ctl_table *table, int write,
275 void *buffer, size_t *lenp, loff_t *ppos)
278 struct ctl_table tbl = {
284 qdisc_get_default(id, IFNAMSIZ);
286 ret = proc_dostring(&tbl, write, buffer, lenp, ppos);
287 if (write && ret == 0)
288 ret = qdisc_set_default(id);
293 static int proc_do_dev_weight(struct ctl_table *table, int write,
294 void *buffer, size_t *lenp, loff_t *ppos)
296 static DEFINE_MUTEX(dev_weight_mutex);
299 mutex_lock(&dev_weight_mutex);
300 ret = proc_dointvec(table, write, buffer, lenp, ppos);
302 weight = READ_ONCE(weight_p);
303 WRITE_ONCE(dev_rx_weight, weight * dev_weight_rx_bias);
304 WRITE_ONCE(dev_tx_weight, weight * dev_weight_tx_bias);
306 mutex_unlock(&dev_weight_mutex);
311 static int proc_do_rss_key(struct ctl_table *table, int write,
312 void *buffer, size_t *lenp, loff_t *ppos)
314 struct ctl_table fake_table;
315 char buf[NETDEV_RSS_KEY_LEN * 3];
317 snprintf(buf, sizeof(buf), "%*phC", NETDEV_RSS_KEY_LEN, netdev_rss_key);
318 fake_table.data = buf;
319 fake_table.maxlen = sizeof(buf);
320 return proc_dostring(&fake_table, write, buffer, lenp, ppos);
323 #ifdef CONFIG_BPF_JIT
324 static int proc_dointvec_minmax_bpf_enable(struct ctl_table *table, int write,
325 void *buffer, size_t *lenp,
328 int ret, jit_enable = *(int *)table->data;
329 int min = *(int *)table->extra1;
330 int max = *(int *)table->extra2;
331 struct ctl_table tmp = *table;
333 if (write && !capable(CAP_SYS_ADMIN))
336 tmp.data = &jit_enable;
337 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
339 if (jit_enable < 2 ||
340 (jit_enable == 2 && bpf_dump_raw_ok(current_cred()))) {
341 *(int *)table->data = jit_enable;
343 pr_warn("bpf_jit_enable = 2 was set! NEVER use this in production, only for JIT debugging!\n");
349 if (write && ret && min == max)
350 pr_info_once("CONFIG_BPF_JIT_ALWAYS_ON is enabled, bpf_jit_enable is permanently set to 1.\n");
355 # ifdef CONFIG_HAVE_EBPF_JIT
357 proc_dointvec_minmax_bpf_restricted(struct ctl_table *table, int write,
358 void *buffer, size_t *lenp, loff_t *ppos)
360 if (!capable(CAP_SYS_ADMIN))
363 return proc_dointvec_minmax(table, write, buffer, lenp, ppos);
365 # endif /* CONFIG_HAVE_EBPF_JIT */
368 proc_dolongvec_minmax_bpf_restricted(struct ctl_table *table, int write,
369 void *buffer, size_t *lenp, loff_t *ppos)
371 if (!capable(CAP_SYS_ADMIN))
374 return proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
378 static struct ctl_table net_core_table[] = {
380 .procname = "wmem_max",
381 .data = &sysctl_wmem_max,
382 .maxlen = sizeof(int),
384 .proc_handler = proc_dointvec_minmax,
385 .extra1 = &min_sndbuf,
388 .procname = "rmem_max",
389 .data = &sysctl_rmem_max,
390 .maxlen = sizeof(int),
392 .proc_handler = proc_dointvec_minmax,
393 .extra1 = &min_rcvbuf,
396 .procname = "wmem_default",
397 .data = &sysctl_wmem_default,
398 .maxlen = sizeof(int),
400 .proc_handler = proc_dointvec_minmax,
401 .extra1 = &min_sndbuf,
404 .procname = "rmem_default",
405 .data = &sysctl_rmem_default,
406 .maxlen = sizeof(int),
408 .proc_handler = proc_dointvec_minmax,
409 .extra1 = &min_rcvbuf,
412 .procname = "mem_pcpu_rsv",
413 .data = &sysctl_mem_pcpu_rsv,
414 .maxlen = sizeof(int),
416 .proc_handler = proc_dointvec_minmax,
417 .extra1 = &min_mem_pcpu_rsv,
420 .procname = "dev_weight",
422 .maxlen = sizeof(int),
424 .proc_handler = proc_do_dev_weight,
427 .procname = "dev_weight_rx_bias",
428 .data = &dev_weight_rx_bias,
429 .maxlen = sizeof(int),
431 .proc_handler = proc_do_dev_weight,
434 .procname = "dev_weight_tx_bias",
435 .data = &dev_weight_tx_bias,
436 .maxlen = sizeof(int),
438 .proc_handler = proc_do_dev_weight,
441 .procname = "netdev_max_backlog",
442 .data = &netdev_max_backlog,
443 .maxlen = sizeof(int),
445 .proc_handler = proc_dointvec
448 .procname = "netdev_rss_key",
449 .data = &netdev_rss_key,
450 .maxlen = sizeof(int),
452 .proc_handler = proc_do_rss_key,
454 #ifdef CONFIG_BPF_JIT
456 .procname = "bpf_jit_enable",
457 .data = &bpf_jit_enable,
458 .maxlen = sizeof(int),
460 .proc_handler = proc_dointvec_minmax_bpf_enable,
461 # ifdef CONFIG_BPF_JIT_ALWAYS_ON
462 .extra1 = SYSCTL_ONE,
463 .extra2 = SYSCTL_ONE,
465 .extra1 = SYSCTL_ZERO,
466 .extra2 = SYSCTL_TWO,
469 # ifdef CONFIG_HAVE_EBPF_JIT
471 .procname = "bpf_jit_harden",
472 .data = &bpf_jit_harden,
473 .maxlen = sizeof(int),
475 .proc_handler = proc_dointvec_minmax_bpf_restricted,
476 .extra1 = SYSCTL_ZERO,
477 .extra2 = SYSCTL_TWO,
480 .procname = "bpf_jit_kallsyms",
481 .data = &bpf_jit_kallsyms,
482 .maxlen = sizeof(int),
484 .proc_handler = proc_dointvec_minmax_bpf_restricted,
485 .extra1 = SYSCTL_ZERO,
486 .extra2 = SYSCTL_ONE,
490 .procname = "bpf_jit_limit",
491 .data = &bpf_jit_limit,
492 .maxlen = sizeof(long),
494 .proc_handler = proc_dolongvec_minmax_bpf_restricted,
495 .extra1 = SYSCTL_LONG_ONE,
496 .extra2 = &bpf_jit_limit_max,
500 .procname = "netdev_tstamp_prequeue",
501 .data = &netdev_tstamp_prequeue,
502 .maxlen = sizeof(int),
504 .proc_handler = proc_dointvec
507 .procname = "message_cost",
508 .data = &net_ratelimit_state.interval,
509 .maxlen = sizeof(int),
511 .proc_handler = proc_dointvec_jiffies,
514 .procname = "message_burst",
515 .data = &net_ratelimit_state.burst,
516 .maxlen = sizeof(int),
518 .proc_handler = proc_dointvec,
521 .procname = "tstamp_allow_data",
522 .data = &sysctl_tstamp_allow_data,
523 .maxlen = sizeof(int),
525 .proc_handler = proc_dointvec_minmax,
526 .extra1 = SYSCTL_ZERO,
531 .procname = "rps_sock_flow_entries",
532 .maxlen = sizeof(int),
534 .proc_handler = rps_sock_flow_sysctl
537 #ifdef CONFIG_NET_FLOW_LIMIT
539 .procname = "flow_limit_cpu_bitmap",
541 .proc_handler = flow_limit_cpu_sysctl
544 .procname = "flow_limit_table_len",
545 .data = &netdev_flow_limit_table_len,
546 .maxlen = sizeof(int),
548 .proc_handler = flow_limit_table_len_sysctl
550 #endif /* CONFIG_NET_FLOW_LIMIT */
551 #ifdef CONFIG_NET_RX_BUSY_POLL
553 .procname = "busy_poll",
554 .data = &sysctl_net_busy_poll,
555 .maxlen = sizeof(unsigned int),
557 .proc_handler = proc_dointvec_minmax,
558 .extra1 = SYSCTL_ZERO,
561 .procname = "busy_read",
562 .data = &sysctl_net_busy_read,
563 .maxlen = sizeof(unsigned int),
565 .proc_handler = proc_dointvec_minmax,
566 .extra1 = SYSCTL_ZERO,
569 #ifdef CONFIG_NET_SCHED
571 .procname = "default_qdisc",
574 .proc_handler = set_default_qdisc
578 .procname = "netdev_budget",
579 .data = &netdev_budget,
580 .maxlen = sizeof(int),
582 .proc_handler = proc_dointvec
585 .procname = "warnings",
586 .data = &net_msg_warn,
587 .maxlen = sizeof(int),
589 .proc_handler = proc_dointvec
592 .procname = "max_skb_frags",
593 .data = &sysctl_max_skb_frags,
594 .maxlen = sizeof(int),
596 .proc_handler = proc_dointvec_minmax,
597 .extra1 = SYSCTL_ONE,
598 .extra2 = &max_skb_frags,
601 .procname = "netdev_budget_usecs",
602 .data = &netdev_budget_usecs,
603 .maxlen = sizeof(unsigned int),
605 .proc_handler = proc_dointvec_minmax,
606 .extra1 = SYSCTL_ZERO,
609 .procname = "fb_tunnels_only_for_init_net",
610 .data = &sysctl_fb_tunnels_only_for_init_net,
611 .maxlen = sizeof(int),
613 .proc_handler = proc_dointvec_minmax,
614 .extra1 = SYSCTL_ZERO,
615 .extra2 = SYSCTL_TWO,
618 .procname = "devconf_inherit_init_net",
619 .data = &sysctl_devconf_inherit_init_net,
620 .maxlen = sizeof(int),
622 .proc_handler = proc_dointvec_minmax,
623 .extra1 = SYSCTL_ZERO,
624 .extra2 = SYSCTL_THREE,
627 .procname = "high_order_alloc_disable",
628 .data = &net_high_order_alloc_disable_key.key,
629 .maxlen = sizeof(net_high_order_alloc_disable_key),
631 .proc_handler = proc_do_static_key,
634 .procname = "gro_normal_batch",
635 .data = &gro_normal_batch,
636 .maxlen = sizeof(unsigned int),
638 .proc_handler = proc_dointvec_minmax,
639 .extra1 = SYSCTL_ONE,
642 .procname = "netdev_unregister_timeout_secs",
643 .data = &netdev_unregister_timeout_secs,
644 .maxlen = sizeof(unsigned int),
646 .proc_handler = proc_dointvec_minmax,
647 .extra1 = SYSCTL_ONE,
651 .procname = "skb_defer_max",
652 .data = &sysctl_skb_defer_max,
653 .maxlen = sizeof(unsigned int),
655 .proc_handler = proc_dointvec_minmax,
656 .extra1 = SYSCTL_ZERO,
661 static struct ctl_table netns_core_table[] = {
662 #if IS_ENABLED(CONFIG_RPS)
664 .procname = "rps_default_mask",
667 .proc_handler = rps_default_mask_sysctl
671 .procname = "somaxconn",
672 .data = &init_net.core.sysctl_somaxconn,
673 .maxlen = sizeof(int),
675 .extra1 = SYSCTL_ZERO,
676 .proc_handler = proc_dointvec_minmax
679 .procname = "optmem_max",
680 .data = &init_net.core.sysctl_optmem_max,
681 .maxlen = sizeof(int),
683 .extra1 = SYSCTL_ZERO,
684 .proc_handler = proc_dointvec_minmax
687 .procname = "txrehash",
688 .data = &init_net.core.sysctl_txrehash,
689 .maxlen = sizeof(u8),
691 .extra1 = SYSCTL_ZERO,
692 .extra2 = SYSCTL_ONE,
693 .proc_handler = proc_dou8vec_minmax,
698 static int __init fb_tunnels_only_for_init_net_sysctl_setup(char *str)
700 /* fallback tunnels for initns only */
701 if (!strncmp(str, "initns", 6))
702 sysctl_fb_tunnels_only_for_init_net = 1;
703 /* no fallback tunnels anywhere */
704 else if (!strncmp(str, "none", 4))
705 sysctl_fb_tunnels_only_for_init_net = 2;
709 __setup("fb_tunnels=", fb_tunnels_only_for_init_net_sysctl_setup);
711 static __net_init int sysctl_core_net_init(struct net *net)
713 struct ctl_table *tbl, *tmp;
715 tbl = netns_core_table;
716 if (!net_eq(net, &init_net)) {
717 tbl = kmemdup(tbl, sizeof(netns_core_table), GFP_KERNEL);
721 for (tmp = tbl; tmp->procname; tmp++)
722 tmp->data += (char *)net - (char *)&init_net;
725 net->core.sysctl_hdr = register_net_sysctl_sz(net, "net/core", tbl,
726 ARRAY_SIZE(netns_core_table));
727 if (net->core.sysctl_hdr == NULL)
733 if (tbl != netns_core_table)
739 static __net_exit void sysctl_core_net_exit(struct net *net)
741 struct ctl_table *tbl;
743 tbl = net->core.sysctl_hdr->ctl_table_arg;
744 unregister_net_sysctl_table(net->core.sysctl_hdr);
745 BUG_ON(tbl == netns_core_table);
746 #if IS_ENABLED(CONFIG_RPS)
747 kfree(net->core.rps_default_mask);
752 static __net_initdata struct pernet_operations sysctl_core_ops = {
753 .init = sysctl_core_net_init,
754 .exit = sysctl_core_net_exit,
757 static __init int sysctl_core_init(void)
759 register_net_sysctl(&init_net, "net/core", net_core_table);
760 return register_pernet_subsys(&sysctl_core_ops);
763 fs_initcall(sysctl_core_init);