blob: 77a65f031488b3f222e13d1f071a2d3f44218c70 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* -*- linux-c -*-
2 * sysctl_net_core.c: sysctl interface to net core subsystem.
3 *
4 * Begun April 1, 1996, Mike Shaver.
5 * Added /proc/sys/net/core directory entry (empty =) ). [MS]
6 */
7
8#include <linux/mm.h>
9#include <linux/sysctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/module.h>
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -030011#include <linux/socket.h>
Pavel Emelyanova37ae402007-10-23 21:13:53 -070012#include <linux/netdevice.h>
Ingo Molnar3fff4c42009-09-22 16:18:09 +020013#include <linux/ratelimit.h>
Tom Herbertfec5e652010-04-16 16:01:27 -070014#include <linux/vmalloc.h>
Pavel Emelyanov33eb9cf2007-12-05 01:37:34 -080015#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Ingo Molnar3fff4c42009-09-22 16:18:09 +020017
Hannes Eder63d819c2009-02-25 10:32:14 +000018#include <net/ip.h>
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -030019#include <net/sock.h>
David S. Millerc5c177b2011-05-27 13:41:33 -040020#include <net/net_ratelimit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Tom Herbertfec5e652010-04-16 16:01:27 -070022#ifdef CONFIG_RPS
23static int rps_sock_flow_sysctl(ctl_table *table, int write,
24 void __user *buffer, size_t *lenp, loff_t *ppos)
25{
26 unsigned int orig_size, size;
27 int ret, i;
28 ctl_table tmp = {
29 .data = &size,
30 .maxlen = sizeof(size),
31 .mode = table->mode
32 };
33 struct rps_sock_flow_table *orig_sock_table, *sock_table;
34 static DEFINE_MUTEX(sock_flow_mutex);
35
36 mutex_lock(&sock_flow_mutex);
37
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +000038 orig_sock_table = rcu_dereference_protected(rps_sock_flow_table,
39 lockdep_is_held(&sock_flow_mutex));
Tom Herbertfec5e652010-04-16 16:01:27 -070040 size = orig_size = orig_sock_table ? orig_sock_table->mask + 1 : 0;
41
42 ret = proc_dointvec(&tmp, write, buffer, lenp, ppos);
43
44 if (write) {
45 if (size) {
46 if (size > 1<<30) {
47 /* Enforce limit to prevent overflow */
48 mutex_unlock(&sock_flow_mutex);
49 return -EINVAL;
50 }
51 size = roundup_pow_of_two(size);
52 if (size != orig_size) {
53 sock_table =
54 vmalloc(RPS_SOCK_FLOW_TABLE_SIZE(size));
55 if (!sock_table) {
56 mutex_unlock(&sock_flow_mutex);
57 return -ENOMEM;
58 }
59
60 sock_table->mask = size - 1;
61 } else
62 sock_table = orig_sock_table;
63
64 for (i = 0; i < size; i++)
65 sock_table->ents[i] = RPS_NO_CPU;
66 } else
67 sock_table = NULL;
68
69 if (sock_table != orig_sock_table) {
70 rcu_assign_pointer(rps_sock_flow_table, sock_table);
71 synchronize_rcu();
72 vfree(orig_sock_table);
73 }
74 }
75
76 mutex_unlock(&sock_flow_mutex);
77
78 return ret;
79}
80#endif /* CONFIG_RPS */
81
Pavel Emelyanov33eb9cf2007-12-05 01:37:34 -080082static struct ctl_table net_core_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#ifdef CONFIG_NET
84 {
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 .procname = "wmem_max",
86 .data = &sysctl_wmem_max,
87 .maxlen = sizeof(int),
88 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -080089 .proc_handler = proc_dointvec
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 },
91 {
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 .procname = "rmem_max",
93 .data = &sysctl_rmem_max,
94 .maxlen = sizeof(int),
95 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -080096 .proc_handler = proc_dointvec
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 },
98 {
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 .procname = "wmem_default",
100 .data = &sysctl_wmem_default,
101 .maxlen = sizeof(int),
102 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800103 .proc_handler = proc_dointvec
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 },
105 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 .procname = "rmem_default",
107 .data = &sysctl_rmem_default,
108 .maxlen = sizeof(int),
109 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800110 .proc_handler = proc_dointvec
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 },
112 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 .procname = "dev_weight",
114 .data = &weight_p,
115 .maxlen = sizeof(int),
116 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800117 .proc_handler = proc_dointvec
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 },
119 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 .procname = "netdev_max_backlog",
121 .data = &netdev_max_backlog,
122 .maxlen = sizeof(int),
123 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800124 .proc_handler = proc_dointvec
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 },
Eric Dumazet0a148422011-04-20 09:27:32 +0000126#ifdef CONFIG_BPF_JIT
127 {
128 .procname = "bpf_jit_enable",
129 .data = &bpf_jit_enable,
130 .maxlen = sizeof(int),
131 .mode = 0644,
132 .proc_handler = proc_dointvec
133 },
134#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 {
Eric Dumazet3b098e22010-05-15 23:57:10 -0700136 .procname = "netdev_tstamp_prequeue",
137 .data = &netdev_tstamp_prequeue,
138 .maxlen = sizeof(int),
139 .mode = 0644,
140 .proc_handler = proc_dointvec
141 },
142 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 .procname = "message_cost",
Dave Young717115e2008-07-25 01:45:58 -0700144 .data = &net_ratelimit_state.interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 .maxlen = sizeof(int),
146 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800147 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 },
149 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 .procname = "message_burst",
Dave Young717115e2008-07-25 01:45:58 -0700151 .data = &net_ratelimit_state.burst,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 .maxlen = sizeof(int),
153 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800154 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 },
156 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 .procname = "optmem_max",
158 .data = &sysctl_optmem_max,
159 .maxlen = sizeof(int),
160 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800161 .proc_handler = proc_dointvec
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 },
Tom Herbertfec5e652010-04-16 16:01:27 -0700163#ifdef CONFIG_RPS
164 {
165 .procname = "rps_sock_flow_entries",
166 .maxlen = sizeof(int),
167 .mode = 0644,
168 .proc_handler = rps_sock_flow_sysctl
169 },
170#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171#endif /* CONFIG_NET */
172 {
Stephen Hemminger51b0bde2005-06-23 20:14:40 -0700173 .procname = "netdev_budget",
174 .data = &netdev_budget,
175 .maxlen = sizeof(int),
176 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800177 .proc_handler = proc_dointvec
Stephen Hemminger51b0bde2005-06-23 20:14:40 -0700178 },
Stephen Hemmingera2a316f2007-03-08 20:41:08 -0800179 {
Stephen Hemmingera2a316f2007-03-08 20:41:08 -0800180 .procname = "warnings",
181 .data = &net_msg_warn,
182 .maxlen = sizeof(int),
183 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800184 .proc_handler = proc_dointvec
Stephen Hemmingera2a316f2007-03-08 20:41:08 -0800185 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800186 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187};
Pavel Emelyanov33eb9cf2007-12-05 01:37:34 -0800188
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700189static struct ctl_table netns_core_table[] = {
190 {
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700191 .procname = "somaxconn",
192 .data = &init_net.core.sysctl_somaxconn,
193 .maxlen = sizeof(int),
194 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800195 .proc_handler = proc_dointvec
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700196 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800197 { }
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700198};
199
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800200__net_initdata struct ctl_path net_core_path[] = {
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800201 { .procname = "net", },
202 { .procname = "core", },
Pavel Emelyanov33eb9cf2007-12-05 01:37:34 -0800203 { },
204};
205
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800206static __net_init int sysctl_core_net_init(struct net *net)
207{
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700208 struct ctl_table *tbl;
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800209
Pavel Emelyanov8efa6e92008-03-31 19:41:14 -0700210 net->core.sysctl_somaxconn = SOMAXCONN;
Pavel Emelyanovb8e1f9b2007-12-08 00:12:33 -0800211
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700212 tbl = netns_core_table;
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800213 if (!net_eq(net, &init_net)) {
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700214 tbl = kmemdup(tbl, sizeof(netns_core_table), GFP_KERNEL);
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800215 if (tbl == NULL)
216 goto err_dup;
217
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700218 tbl[0].data = &net->core.sysctl_somaxconn;
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800219 }
220
Pavel Emelyanov8efa6e92008-03-31 19:41:14 -0700221 net->core.sysctl_hdr = register_net_sysctl_table(net,
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800222 net_core_path, tbl);
Pavel Emelyanov8efa6e92008-03-31 19:41:14 -0700223 if (net->core.sysctl_hdr == NULL)
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800224 goto err_reg;
225
226 return 0;
227
228err_reg:
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700229 if (tbl != netns_core_table)
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800230 kfree(tbl);
231err_dup:
232 return -ENOMEM;
233}
234
235static __net_exit void sysctl_core_net_exit(struct net *net)
236{
237 struct ctl_table *tbl;
238
Pavel Emelyanov8efa6e92008-03-31 19:41:14 -0700239 tbl = net->core.sysctl_hdr->ctl_table_arg;
240 unregister_net_sysctl_table(net->core.sysctl_hdr);
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700241 BUG_ON(tbl == netns_core_table);
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800242 kfree(tbl);
243}
244
245static __net_initdata struct pernet_operations sysctl_core_ops = {
246 .init = sysctl_core_net_init,
247 .exit = sysctl_core_net_exit,
248};
249
Pavel Emelyanov33eb9cf2007-12-05 01:37:34 -0800250static __init int sysctl_core_init(void)
251{
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800252 static struct ctl_table empty[1];
253
254 register_sysctl_paths(net_core_path, empty);
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700255 register_net_sysctl_rotable(net_core_path, net_core_table);
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800256 return register_pernet_subsys(&sysctl_core_ops);
Pavel Emelyanov33eb9cf2007-12-05 01:37:34 -0800257}
258
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800259fs_initcall(sysctl_core_init);