blob: 741db5fc78066dd7938a57718eacb27330de2548 [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>
Shan Wei7426a562012-04-18 18:05:46 +000017#include <linux/kmemleak.h>
Ingo Molnar3fff4c42009-09-22 16:18:09 +020018
Hannes Eder63d819c2009-02-25 10:32:14 +000019#include <net/ip.h>
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -030020#include <net/sock.h>
David S. Millerc5c177b2011-05-27 13:41:33 -040021#include <net/net_ratelimit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
bingtian.ly@taobao.comcdda8892013-01-23 20:35:28 +000023static int one = 1;
24
Tom Herbertfec5e652010-04-16 16:01:27 -070025#ifdef CONFIG_RPS
26static int rps_sock_flow_sysctl(ctl_table *table, int write,
27 void __user *buffer, size_t *lenp, loff_t *ppos)
28{
29 unsigned int orig_size, size;
30 int ret, i;
31 ctl_table tmp = {
32 .data = &size,
33 .maxlen = sizeof(size),
34 .mode = table->mode
35 };
36 struct rps_sock_flow_table *orig_sock_table, *sock_table;
37 static DEFINE_MUTEX(sock_flow_mutex);
38
39 mutex_lock(&sock_flow_mutex);
40
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +000041 orig_sock_table = rcu_dereference_protected(rps_sock_flow_table,
42 lockdep_is_held(&sock_flow_mutex));
Tom Herbertfec5e652010-04-16 16:01:27 -070043 size = orig_size = orig_sock_table ? orig_sock_table->mask + 1 : 0;
44
45 ret = proc_dointvec(&tmp, write, buffer, lenp, ppos);
46
47 if (write) {
48 if (size) {
49 if (size > 1<<30) {
50 /* Enforce limit to prevent overflow */
51 mutex_unlock(&sock_flow_mutex);
52 return -EINVAL;
53 }
54 size = roundup_pow_of_two(size);
55 if (size != orig_size) {
56 sock_table =
57 vmalloc(RPS_SOCK_FLOW_TABLE_SIZE(size));
58 if (!sock_table) {
59 mutex_unlock(&sock_flow_mutex);
60 return -ENOMEM;
61 }
62
63 sock_table->mask = size - 1;
64 } else
65 sock_table = orig_sock_table;
66
67 for (i = 0; i < size; i++)
68 sock_table->ents[i] = RPS_NO_CPU;
69 } else
70 sock_table = NULL;
71
72 if (sock_table != orig_sock_table) {
73 rcu_assign_pointer(rps_sock_flow_table, sock_table);
Eric Dumazetadc93002011-11-17 03:13:26 +000074 if (sock_table)
Ingo Molnarc5905af2012-02-24 08:31:31 +010075 static_key_slow_inc(&rps_needed);
Eric Dumazetadc93002011-11-17 03:13:26 +000076 if (orig_sock_table) {
Ingo Molnarc5905af2012-02-24 08:31:31 +010077 static_key_slow_dec(&rps_needed);
Eric Dumazetadc93002011-11-17 03:13:26 +000078 synchronize_rcu();
79 vfree(orig_sock_table);
80 }
Tom Herbertfec5e652010-04-16 16:01:27 -070081 }
82 }
83
84 mutex_unlock(&sock_flow_mutex);
85
86 return ret;
87}
88#endif /* CONFIG_RPS */
89
Willem de Bruijn99bbc702013-05-20 04:02:32 +000090#ifdef CONFIG_NET_FLOW_LIMIT
91static DEFINE_MUTEX(flow_limit_update_mutex);
92
93static int flow_limit_cpu_sysctl(ctl_table *table, int write,
94 void __user *buffer, size_t *lenp,
95 loff_t *ppos)
96{
97 struct sd_flow_limit *cur;
98 struct softnet_data *sd;
99 cpumask_var_t mask;
100 int i, len, ret = 0;
101
102 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
103 return -ENOMEM;
104
105 if (write) {
106 ret = cpumask_parse_user(buffer, *lenp, mask);
107 if (ret)
108 goto done;
109
110 mutex_lock(&flow_limit_update_mutex);
111 len = sizeof(*cur) + netdev_flow_limit_table_len;
112 for_each_possible_cpu(i) {
113 sd = &per_cpu(softnet_data, i);
114 cur = rcu_dereference_protected(sd->flow_limit,
115 lockdep_is_held(&flow_limit_update_mutex));
116 if (cur && !cpumask_test_cpu(i, mask)) {
117 RCU_INIT_POINTER(sd->flow_limit, NULL);
118 synchronize_rcu();
119 kfree(cur);
120 } else if (!cur && cpumask_test_cpu(i, mask)) {
121 cur = kzalloc(len, GFP_KERNEL);
122 if (!cur) {
123 /* not unwinding previous changes */
124 ret = -ENOMEM;
125 goto write_unlock;
126 }
127 cur->num_buckets = netdev_flow_limit_table_len;
128 rcu_assign_pointer(sd->flow_limit, cur);
129 }
130 }
131write_unlock:
132 mutex_unlock(&flow_limit_update_mutex);
133 } else {
134 if (*ppos || !*lenp) {
135 *lenp = 0;
136 goto done;
137 }
138
139 cpumask_clear(mask);
140 rcu_read_lock();
141 for_each_possible_cpu(i) {
142 sd = &per_cpu(softnet_data, i);
143 if (rcu_dereference(sd->flow_limit))
144 cpumask_set_cpu(i, mask);
145 }
146 rcu_read_unlock();
147
148 len = cpumask_scnprintf(buffer, *lenp, mask);
149 *lenp = len + 1;
150 *ppos += len + 1;
151 }
152
153done:
154 free_cpumask_var(mask);
155 return ret;
156}
157
158static int flow_limit_table_len_sysctl(ctl_table *table, int write,
159 void __user *buffer, size_t *lenp,
160 loff_t *ppos)
161{
162 unsigned int old, *ptr;
163 int ret;
164
165 mutex_lock(&flow_limit_update_mutex);
166
167 ptr = table->data;
168 old = *ptr;
169 ret = proc_dointvec(table, write, buffer, lenp, ppos);
170 if (!ret && write && !is_power_of_2(*ptr)) {
171 *ptr = old;
172 ret = -EINVAL;
173 }
174
175 mutex_unlock(&flow_limit_update_mutex);
176 return ret;
177}
178#endif /* CONFIG_NET_FLOW_LIMIT */
179
Pavel Emelyanov33eb9cf2007-12-05 01:37:34 -0800180static struct ctl_table net_core_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181#ifdef CONFIG_NET
182 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 .procname = "wmem_max",
184 .data = &sysctl_wmem_max,
185 .maxlen = sizeof(int),
186 .mode = 0644,
bingtian.ly@taobao.comcdda8892013-01-23 20:35:28 +0000187 .proc_handler = proc_dointvec_minmax,
188 .extra1 = &one,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 },
190 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 .procname = "rmem_max",
192 .data = &sysctl_rmem_max,
193 .maxlen = sizeof(int),
194 .mode = 0644,
bingtian.ly@taobao.comcdda8892013-01-23 20:35:28 +0000195 .proc_handler = proc_dointvec_minmax,
196 .extra1 = &one,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 },
198 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 .procname = "wmem_default",
200 .data = &sysctl_wmem_default,
201 .maxlen = sizeof(int),
202 .mode = 0644,
bingtian.ly@taobao.comcdda8892013-01-23 20:35:28 +0000203 .proc_handler = proc_dointvec_minmax,
204 .extra1 = &one,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 },
206 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 .procname = "rmem_default",
208 .data = &sysctl_rmem_default,
209 .maxlen = sizeof(int),
210 .mode = 0644,
bingtian.ly@taobao.comcdda8892013-01-23 20:35:28 +0000211 .proc_handler = proc_dointvec_minmax,
212 .extra1 = &one,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 },
214 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 .procname = "dev_weight",
216 .data = &weight_p,
217 .maxlen = sizeof(int),
218 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800219 .proc_handler = proc_dointvec
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 },
221 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 .procname = "netdev_max_backlog",
223 .data = &netdev_max_backlog,
224 .maxlen = sizeof(int),
225 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800226 .proc_handler = proc_dointvec
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 },
Eric Dumazet0a148422011-04-20 09:27:32 +0000228#ifdef CONFIG_BPF_JIT
229 {
230 .procname = "bpf_jit_enable",
231 .data = &bpf_jit_enable,
232 .maxlen = sizeof(int),
233 .mode = 0644,
234 .proc_handler = proc_dointvec
235 },
236#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 {
Eric Dumazet3b098e22010-05-15 23:57:10 -0700238 .procname = "netdev_tstamp_prequeue",
239 .data = &netdev_tstamp_prequeue,
240 .maxlen = sizeof(int),
241 .mode = 0644,
242 .proc_handler = proc_dointvec
243 },
244 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 .procname = "message_cost",
Dave Young717115e2008-07-25 01:45:58 -0700246 .data = &net_ratelimit_state.interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 .maxlen = sizeof(int),
248 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800249 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 },
251 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 .procname = "message_burst",
Dave Young717115e2008-07-25 01:45:58 -0700253 .data = &net_ratelimit_state.burst,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 .maxlen = sizeof(int),
255 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800256 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 },
258 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 .procname = "optmem_max",
260 .data = &sysctl_optmem_max,
261 .maxlen = sizeof(int),
262 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800263 .proc_handler = proc_dointvec
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 },
Tom Herbertfec5e652010-04-16 16:01:27 -0700265#ifdef CONFIG_RPS
266 {
267 .procname = "rps_sock_flow_entries",
268 .maxlen = sizeof(int),
269 .mode = 0644,
270 .proc_handler = rps_sock_flow_sysctl
271 },
272#endif
Willem de Bruijn99bbc702013-05-20 04:02:32 +0000273#ifdef CONFIG_NET_FLOW_LIMIT
274 {
275 .procname = "flow_limit_cpu_bitmap",
276 .mode = 0644,
277 .proc_handler = flow_limit_cpu_sysctl
278 },
279 {
280 .procname = "flow_limit_table_len",
281 .data = &netdev_flow_limit_table_len,
282 .maxlen = sizeof(int),
283 .mode = 0644,
284 .proc_handler = flow_limit_table_len_sysctl
285 },
286#endif /* CONFIG_NET_FLOW_LIMIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287#endif /* CONFIG_NET */
288 {
Stephen Hemminger51b0bde2005-06-23 20:14:40 -0700289 .procname = "netdev_budget",
290 .data = &netdev_budget,
291 .maxlen = sizeof(int),
292 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800293 .proc_handler = proc_dointvec
Stephen Hemminger51b0bde2005-06-23 20:14:40 -0700294 },
Stephen Hemmingera2a316f2007-03-08 20:41:08 -0800295 {
Stephen Hemmingera2a316f2007-03-08 20:41:08 -0800296 .procname = "warnings",
297 .data = &net_msg_warn,
298 .maxlen = sizeof(int),
299 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800300 .proc_handler = proc_dointvec
Stephen Hemmingera2a316f2007-03-08 20:41:08 -0800301 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800302 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303};
Pavel Emelyanov33eb9cf2007-12-05 01:37:34 -0800304
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700305static struct ctl_table netns_core_table[] = {
306 {
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700307 .procname = "somaxconn",
308 .data = &init_net.core.sysctl_somaxconn,
309 .maxlen = sizeof(int),
310 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800311 .proc_handler = proc_dointvec
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700312 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800313 { }
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700314};
315
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800316static __net_init int sysctl_core_net_init(struct net *net)
317{
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700318 struct ctl_table *tbl;
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800319
Pavel Emelyanov8efa6e92008-03-31 19:41:14 -0700320 net->core.sysctl_somaxconn = SOMAXCONN;
Pavel Emelyanovb8e1f9b2007-12-08 00:12:33 -0800321
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700322 tbl = netns_core_table;
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800323 if (!net_eq(net, &init_net)) {
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700324 tbl = kmemdup(tbl, sizeof(netns_core_table), GFP_KERNEL);
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800325 if (tbl == NULL)
326 goto err_dup;
327
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700328 tbl[0].data = &net->core.sysctl_somaxconn;
Eric W. Biederman464dc802012-11-16 03:02:59 +0000329
330 /* Don't export any sysctls to unprivileged users */
331 if (net->user_ns != &init_user_ns) {
332 tbl[0].procname = NULL;
333 }
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800334 }
335
Eric W. Biedermanec8f23c2012-04-19 13:44:49 +0000336 net->core.sysctl_hdr = register_net_sysctl(net, "net/core", tbl);
Pavel Emelyanov8efa6e92008-03-31 19:41:14 -0700337 if (net->core.sysctl_hdr == NULL)
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800338 goto err_reg;
339
340 return 0;
341
342err_reg:
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700343 if (tbl != netns_core_table)
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800344 kfree(tbl);
345err_dup:
346 return -ENOMEM;
347}
348
349static __net_exit void sysctl_core_net_exit(struct net *net)
350{
351 struct ctl_table *tbl;
352
Pavel Emelyanov8efa6e92008-03-31 19:41:14 -0700353 tbl = net->core.sysctl_hdr->ctl_table_arg;
354 unregister_net_sysctl_table(net->core.sysctl_hdr);
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700355 BUG_ON(tbl == netns_core_table);
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800356 kfree(tbl);
357}
358
359static __net_initdata struct pernet_operations sysctl_core_ops = {
360 .init = sysctl_core_net_init,
361 .exit = sysctl_core_net_exit,
362};
363
Pavel Emelyanov33eb9cf2007-12-05 01:37:34 -0800364static __init int sysctl_core_init(void)
365{
Eric W. Biederman43444752012-04-19 13:22:55 +0000366 register_net_sysctl(&init_net, "net/core", net_core_table);
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800367 return register_pernet_subsys(&sysctl_core_ops);
Pavel Emelyanov33eb9cf2007-12-05 01:37:34 -0800368}
369
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800370fs_initcall(sysctl_core_init);