blob: 4b48f39582b02b36352f70e262c6239b5dc43170 [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>
Eliezer Tamir06021292013-06-10 11:39:50 +030022#include <net/ll_poll.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
bingtian.ly@taobao.comcdda8892013-01-23 20:35:28 +000024static int one = 1;
25
Tom Herbertfec5e652010-04-16 16:01:27 -070026#ifdef CONFIG_RPS
27static int rps_sock_flow_sysctl(ctl_table *table, int write,
28 void __user *buffer, size_t *lenp, loff_t *ppos)
29{
30 unsigned int orig_size, size;
31 int ret, i;
32 ctl_table tmp = {
33 .data = &size,
34 .maxlen = sizeof(size),
35 .mode = table->mode
36 };
37 struct rps_sock_flow_table *orig_sock_table, *sock_table;
38 static DEFINE_MUTEX(sock_flow_mutex);
39
40 mutex_lock(&sock_flow_mutex);
41
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +000042 orig_sock_table = rcu_dereference_protected(rps_sock_flow_table,
43 lockdep_is_held(&sock_flow_mutex));
Tom Herbertfec5e652010-04-16 16:01:27 -070044 size = orig_size = orig_sock_table ? orig_sock_table->mask + 1 : 0;
45
46 ret = proc_dointvec(&tmp, write, buffer, lenp, ppos);
47
48 if (write) {
49 if (size) {
50 if (size > 1<<30) {
51 /* Enforce limit to prevent overflow */
52 mutex_unlock(&sock_flow_mutex);
53 return -EINVAL;
54 }
55 size = roundup_pow_of_two(size);
56 if (size != orig_size) {
57 sock_table =
58 vmalloc(RPS_SOCK_FLOW_TABLE_SIZE(size));
59 if (!sock_table) {
60 mutex_unlock(&sock_flow_mutex);
61 return -ENOMEM;
62 }
63
64 sock_table->mask = size - 1;
65 } else
66 sock_table = orig_sock_table;
67
68 for (i = 0; i < size; i++)
69 sock_table->ents[i] = RPS_NO_CPU;
70 } else
71 sock_table = NULL;
72
73 if (sock_table != orig_sock_table) {
74 rcu_assign_pointer(rps_sock_flow_table, sock_table);
Eric Dumazetadc93002011-11-17 03:13:26 +000075 if (sock_table)
Ingo Molnarc5905af2012-02-24 08:31:31 +010076 static_key_slow_inc(&rps_needed);
Eric Dumazetadc93002011-11-17 03:13:26 +000077 if (orig_sock_table) {
Ingo Molnarc5905af2012-02-24 08:31:31 +010078 static_key_slow_dec(&rps_needed);
Eric Dumazetadc93002011-11-17 03:13:26 +000079 synchronize_rcu();
80 vfree(orig_sock_table);
81 }
Tom Herbertfec5e652010-04-16 16:01:27 -070082 }
83 }
84
85 mutex_unlock(&sock_flow_mutex);
86
87 return ret;
88}
89#endif /* CONFIG_RPS */
90
Willem de Bruijn99bbc702013-05-20 04:02:32 +000091#ifdef CONFIG_NET_FLOW_LIMIT
92static DEFINE_MUTEX(flow_limit_update_mutex);
93
94static int flow_limit_cpu_sysctl(ctl_table *table, int write,
95 void __user *buffer, size_t *lenp,
96 loff_t *ppos)
97{
98 struct sd_flow_limit *cur;
99 struct softnet_data *sd;
100 cpumask_var_t mask;
101 int i, len, ret = 0;
102
103 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
104 return -ENOMEM;
105
106 if (write) {
107 ret = cpumask_parse_user(buffer, *lenp, mask);
108 if (ret)
109 goto done;
110
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);
119 synchronize_rcu();
120 kfree(cur);
121 } else if (!cur && cpumask_test_cpu(i, mask)) {
122 cur = kzalloc(len, GFP_KERNEL);
123 if (!cur) {
124 /* not unwinding previous changes */
125 ret = -ENOMEM;
126 goto write_unlock;
127 }
128 cur->num_buckets = netdev_flow_limit_table_len;
129 rcu_assign_pointer(sd->flow_limit, cur);
130 }
131 }
132write_unlock:
133 mutex_unlock(&flow_limit_update_mutex);
134 } else {
135 if (*ppos || !*lenp) {
136 *lenp = 0;
137 goto done;
138 }
139
140 cpumask_clear(mask);
141 rcu_read_lock();
142 for_each_possible_cpu(i) {
143 sd = &per_cpu(softnet_data, i);
144 if (rcu_dereference(sd->flow_limit))
145 cpumask_set_cpu(i, mask);
146 }
147 rcu_read_unlock();
148
149 len = cpumask_scnprintf(buffer, *lenp, mask);
150 *lenp = len + 1;
151 *ppos += len + 1;
152 }
153
154done:
155 free_cpumask_var(mask);
156 return ret;
157}
158
159static int flow_limit_table_len_sysctl(ctl_table *table, int write,
160 void __user *buffer, size_t *lenp,
161 loff_t *ppos)
162{
163 unsigned int old, *ptr;
164 int ret;
165
166 mutex_lock(&flow_limit_update_mutex);
167
168 ptr = table->data;
169 old = *ptr;
170 ret = proc_dointvec(table, write, buffer, lenp, ppos);
171 if (!ret && write && !is_power_of_2(*ptr)) {
172 *ptr = old;
173 ret = -EINVAL;
174 }
175
176 mutex_unlock(&flow_limit_update_mutex);
177 return ret;
178}
179#endif /* CONFIG_NET_FLOW_LIMIT */
180
Pavel Emelyanov33eb9cf2007-12-05 01:37:34 -0800181static struct ctl_table net_core_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182#ifdef CONFIG_NET
183 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 .procname = "wmem_max",
185 .data = &sysctl_wmem_max,
186 .maxlen = sizeof(int),
187 .mode = 0644,
bingtian.ly@taobao.comcdda8892013-01-23 20:35:28 +0000188 .proc_handler = proc_dointvec_minmax,
189 .extra1 = &one,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 },
191 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 .procname = "rmem_max",
193 .data = &sysctl_rmem_max,
194 .maxlen = sizeof(int),
195 .mode = 0644,
bingtian.ly@taobao.comcdda8892013-01-23 20:35:28 +0000196 .proc_handler = proc_dointvec_minmax,
197 .extra1 = &one,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 },
199 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 .procname = "wmem_default",
201 .data = &sysctl_wmem_default,
202 .maxlen = sizeof(int),
203 .mode = 0644,
bingtian.ly@taobao.comcdda8892013-01-23 20:35:28 +0000204 .proc_handler = proc_dointvec_minmax,
205 .extra1 = &one,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 },
207 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 .procname = "rmem_default",
209 .data = &sysctl_rmem_default,
210 .maxlen = sizeof(int),
211 .mode = 0644,
bingtian.ly@taobao.comcdda8892013-01-23 20:35:28 +0000212 .proc_handler = proc_dointvec_minmax,
213 .extra1 = &one,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 },
215 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 .procname = "dev_weight",
217 .data = &weight_p,
218 .maxlen = sizeof(int),
219 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800220 .proc_handler = proc_dointvec
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 },
222 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 .procname = "netdev_max_backlog",
224 .data = &netdev_max_backlog,
225 .maxlen = sizeof(int),
226 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800227 .proc_handler = proc_dointvec
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 },
Eric Dumazet0a148422011-04-20 09:27:32 +0000229#ifdef CONFIG_BPF_JIT
230 {
231 .procname = "bpf_jit_enable",
232 .data = &bpf_jit_enable,
233 .maxlen = sizeof(int),
234 .mode = 0644,
235 .proc_handler = proc_dointvec
236 },
237#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 {
Eric Dumazet3b098e22010-05-15 23:57:10 -0700239 .procname = "netdev_tstamp_prequeue",
240 .data = &netdev_tstamp_prequeue,
241 .maxlen = sizeof(int),
242 .mode = 0644,
243 .proc_handler = proc_dointvec
244 },
245 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 .procname = "message_cost",
Dave Young717115e2008-07-25 01:45:58 -0700247 .data = &net_ratelimit_state.interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 .maxlen = sizeof(int),
249 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800250 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 },
252 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 .procname = "message_burst",
Dave Young717115e2008-07-25 01:45:58 -0700254 .data = &net_ratelimit_state.burst,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 .maxlen = sizeof(int),
256 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800257 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 },
259 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 .procname = "optmem_max",
261 .data = &sysctl_optmem_max,
262 .maxlen = sizeof(int),
263 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800264 .proc_handler = proc_dointvec
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 },
Tom Herbertfec5e652010-04-16 16:01:27 -0700266#ifdef CONFIG_RPS
267 {
268 .procname = "rps_sock_flow_entries",
269 .maxlen = sizeof(int),
270 .mode = 0644,
271 .proc_handler = rps_sock_flow_sysctl
272 },
273#endif
Willem de Bruijn99bbc702013-05-20 04:02:32 +0000274#ifdef CONFIG_NET_FLOW_LIMIT
275 {
276 .procname = "flow_limit_cpu_bitmap",
277 .mode = 0644,
278 .proc_handler = flow_limit_cpu_sysctl
279 },
280 {
281 .procname = "flow_limit_table_len",
282 .data = &netdev_flow_limit_table_len,
283 .maxlen = sizeof(int),
284 .mode = 0644,
285 .proc_handler = flow_limit_table_len_sysctl
286 },
287#endif /* CONFIG_NET_FLOW_LIMIT */
Eliezer Tamir06021292013-06-10 11:39:50 +0300288#ifdef CONFIG_NET_LL_RX_POLL
289 {
290 .procname = "low_latency_poll",
291 .data = &sysctl_net_ll_poll,
292 .maxlen = sizeof(unsigned long),
293 .mode = 0644,
294 .proc_handler = proc_doulongvec_minmax
295 },
296#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297#endif /* CONFIG_NET */
298 {
Stephen Hemminger51b0bde2005-06-23 20:14:40 -0700299 .procname = "netdev_budget",
300 .data = &netdev_budget,
301 .maxlen = sizeof(int),
302 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800303 .proc_handler = proc_dointvec
Stephen Hemminger51b0bde2005-06-23 20:14:40 -0700304 },
Stephen Hemmingera2a316f2007-03-08 20:41:08 -0800305 {
Stephen Hemmingera2a316f2007-03-08 20:41:08 -0800306 .procname = "warnings",
307 .data = &net_msg_warn,
308 .maxlen = sizeof(int),
309 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800310 .proc_handler = proc_dointvec
Stephen Hemmingera2a316f2007-03-08 20:41:08 -0800311 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800312 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313};
Pavel Emelyanov33eb9cf2007-12-05 01:37:34 -0800314
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700315static struct ctl_table netns_core_table[] = {
316 {
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700317 .procname = "somaxconn",
318 .data = &init_net.core.sysctl_somaxconn,
319 .maxlen = sizeof(int),
320 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800321 .proc_handler = proc_dointvec
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700322 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800323 { }
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700324};
325
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800326static __net_init int sysctl_core_net_init(struct net *net)
327{
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700328 struct ctl_table *tbl;
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800329
Pavel Emelyanov8efa6e92008-03-31 19:41:14 -0700330 net->core.sysctl_somaxconn = SOMAXCONN;
Pavel Emelyanovb8e1f9b2007-12-08 00:12:33 -0800331
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700332 tbl = netns_core_table;
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800333 if (!net_eq(net, &init_net)) {
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700334 tbl = kmemdup(tbl, sizeof(netns_core_table), GFP_KERNEL);
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800335 if (tbl == NULL)
336 goto err_dup;
337
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700338 tbl[0].data = &net->core.sysctl_somaxconn;
Eric W. Biederman464dc802012-11-16 03:02:59 +0000339
340 /* Don't export any sysctls to unprivileged users */
341 if (net->user_ns != &init_user_ns) {
342 tbl[0].procname = NULL;
343 }
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800344 }
345
Eric W. Biedermanec8f23c2012-04-19 13:44:49 +0000346 net->core.sysctl_hdr = register_net_sysctl(net, "net/core", tbl);
Pavel Emelyanov8efa6e92008-03-31 19:41:14 -0700347 if (net->core.sysctl_hdr == NULL)
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800348 goto err_reg;
349
350 return 0;
351
352err_reg:
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700353 if (tbl != netns_core_table)
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800354 kfree(tbl);
355err_dup:
356 return -ENOMEM;
357}
358
359static __net_exit void sysctl_core_net_exit(struct net *net)
360{
361 struct ctl_table *tbl;
362
Pavel Emelyanov8efa6e92008-03-31 19:41:14 -0700363 tbl = net->core.sysctl_hdr->ctl_table_arg;
364 unregister_net_sysctl_table(net->core.sysctl_hdr);
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700365 BUG_ON(tbl == netns_core_table);
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800366 kfree(tbl);
367}
368
369static __net_initdata struct pernet_operations sysctl_core_ops = {
370 .init = sysctl_core_net_init,
371 .exit = sysctl_core_net_exit,
372};
373
Pavel Emelyanov33eb9cf2007-12-05 01:37:34 -0800374static __init int sysctl_core_init(void)
375{
Eric W. Biederman43444752012-04-19 13:22:55 +0000376 register_net_sysctl(&init_net, "net/core", net_core_table);
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800377 return register_pernet_subsys(&sysctl_core_ops);
Pavel Emelyanov33eb9cf2007-12-05 01:37:34 -0800378}
379
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800380fs_initcall(sysctl_core_init);