blob: 6609686166379765233cdbfab0e3d580a3d23051 [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 Tamir076bb0c2013-07-10 17:13:17 +030022#include <net/busy_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
Joe Perchesfe2c6332013-06-11 23:04:25 -070027static int rps_sock_flow_sysctl(struct ctl_table *table, int write,
Tom Herbertfec5e652010-04-16 16:01:27 -070028 void __user *buffer, size_t *lenp, loff_t *ppos)
29{
30 unsigned int orig_size, size;
31 int ret, i;
Joe Perchesfe2c6332013-06-11 23:04:25 -070032 struct ctl_table tmp = {
Tom Herbertfec5e652010-04-16 16:01:27 -070033 .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
Joe Perchesfe2c6332013-06-11 23:04:25 -070094static int flow_limit_cpu_sysctl(struct ctl_table *table, int write,
Willem de Bruijn99bbc702013-05-20 04:02:32 +000095 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 {
Willem de Bruijn5f121b92013-06-13 15:29:38 -0400135 char kbuf[128];
136
Willem de Bruijn99bbc702013-05-20 04:02:32 +0000137 if (*ppos || !*lenp) {
138 *lenp = 0;
139 goto done;
140 }
141
142 cpumask_clear(mask);
143 rcu_read_lock();
144 for_each_possible_cpu(i) {
145 sd = &per_cpu(softnet_data, i);
146 if (rcu_dereference(sd->flow_limit))
147 cpumask_set_cpu(i, mask);
148 }
149 rcu_read_unlock();
150
Willem de Bruijn5f121b92013-06-13 15:29:38 -0400151 len = min(sizeof(kbuf) - 1, *lenp);
152 len = cpumask_scnprintf(kbuf, len, mask);
153 if (!len) {
154 *lenp = 0;
155 goto done;
156 }
157 if (len < *lenp)
158 kbuf[len++] = '\n';
159 if (copy_to_user(buffer, kbuf, len)) {
160 ret = -EFAULT;
161 goto done;
162 }
163 *lenp = len;
164 *ppos += len;
Willem de Bruijn99bbc702013-05-20 04:02:32 +0000165 }
166
167done:
168 free_cpumask_var(mask);
169 return ret;
170}
171
Joe Perchesfe2c6332013-06-11 23:04:25 -0700172static int flow_limit_table_len_sysctl(struct ctl_table *table, int write,
Willem de Bruijn99bbc702013-05-20 04:02:32 +0000173 void __user *buffer, size_t *lenp,
174 loff_t *ppos)
175{
176 unsigned int old, *ptr;
177 int ret;
178
179 mutex_lock(&flow_limit_update_mutex);
180
181 ptr = table->data;
182 old = *ptr;
183 ret = proc_dointvec(table, write, buffer, lenp, ppos);
184 if (!ret && write && !is_power_of_2(*ptr)) {
185 *ptr = old;
186 ret = -EINVAL;
187 }
188
189 mutex_unlock(&flow_limit_update_mutex);
190 return ret;
191}
192#endif /* CONFIG_NET_FLOW_LIMIT */
193
Pavel Emelyanov33eb9cf2007-12-05 01:37:34 -0800194static struct ctl_table net_core_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195#ifdef CONFIG_NET
196 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 .procname = "wmem_max",
198 .data = &sysctl_wmem_max,
199 .maxlen = sizeof(int),
200 .mode = 0644,
bingtian.ly@taobao.comcdda8892013-01-23 20:35:28 +0000201 .proc_handler = proc_dointvec_minmax,
202 .extra1 = &one,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 },
204 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 .procname = "rmem_max",
206 .data = &sysctl_rmem_max,
207 .maxlen = sizeof(int),
208 .mode = 0644,
bingtian.ly@taobao.comcdda8892013-01-23 20:35:28 +0000209 .proc_handler = proc_dointvec_minmax,
210 .extra1 = &one,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 },
212 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 .procname = "wmem_default",
214 .data = &sysctl_wmem_default,
215 .maxlen = sizeof(int),
216 .mode = 0644,
bingtian.ly@taobao.comcdda8892013-01-23 20:35:28 +0000217 .proc_handler = proc_dointvec_minmax,
218 .extra1 = &one,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 },
220 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 .procname = "rmem_default",
222 .data = &sysctl_rmem_default,
223 .maxlen = sizeof(int),
224 .mode = 0644,
bingtian.ly@taobao.comcdda8892013-01-23 20:35:28 +0000225 .proc_handler = proc_dointvec_minmax,
226 .extra1 = &one,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 },
228 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 .procname = "dev_weight",
230 .data = &weight_p,
231 .maxlen = sizeof(int),
232 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800233 .proc_handler = proc_dointvec
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 },
235 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 .procname = "netdev_max_backlog",
237 .data = &netdev_max_backlog,
238 .maxlen = sizeof(int),
239 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800240 .proc_handler = proc_dointvec
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 },
Eric Dumazet0a148422011-04-20 09:27:32 +0000242#ifdef CONFIG_BPF_JIT
243 {
244 .procname = "bpf_jit_enable",
245 .data = &bpf_jit_enable,
246 .maxlen = sizeof(int),
247 .mode = 0644,
248 .proc_handler = proc_dointvec
249 },
250#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 {
Eric Dumazet3b098e22010-05-15 23:57:10 -0700252 .procname = "netdev_tstamp_prequeue",
253 .data = &netdev_tstamp_prequeue,
254 .maxlen = sizeof(int),
255 .mode = 0644,
256 .proc_handler = proc_dointvec
257 },
258 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 .procname = "message_cost",
Dave Young717115e2008-07-25 01:45:58 -0700260 .data = &net_ratelimit_state.interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 .maxlen = sizeof(int),
262 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800263 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 },
265 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 .procname = "message_burst",
Dave Young717115e2008-07-25 01:45:58 -0700267 .data = &net_ratelimit_state.burst,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 .maxlen = sizeof(int),
269 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800270 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 },
272 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 .procname = "optmem_max",
274 .data = &sysctl_optmem_max,
275 .maxlen = sizeof(int),
276 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800277 .proc_handler = proc_dointvec
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 },
Tom Herbertfec5e652010-04-16 16:01:27 -0700279#ifdef CONFIG_RPS
280 {
281 .procname = "rps_sock_flow_entries",
282 .maxlen = sizeof(int),
283 .mode = 0644,
284 .proc_handler = rps_sock_flow_sysctl
285 },
286#endif
Willem de Bruijn99bbc702013-05-20 04:02:32 +0000287#ifdef CONFIG_NET_FLOW_LIMIT
288 {
289 .procname = "flow_limit_cpu_bitmap",
290 .mode = 0644,
291 .proc_handler = flow_limit_cpu_sysctl
292 },
293 {
294 .procname = "flow_limit_table_len",
295 .data = &netdev_flow_limit_table_len,
296 .maxlen = sizeof(int),
297 .mode = 0644,
298 .proc_handler = flow_limit_table_len_sysctl
299 },
300#endif /* CONFIG_NET_FLOW_LIMIT */
Eliezer Tamir06021292013-06-10 11:39:50 +0300301#ifdef CONFIG_NET_LL_RX_POLL
302 {
Eliezer Tamir64b0dc52013-07-10 17:13:36 +0300303 .procname = "busy_poll",
304 .data = &sysctl_net_busy_poll,
Eliezer Tamireb6db622013-06-14 16:33:25 +0300305 .maxlen = sizeof(unsigned int),
Eliezer Tamir06021292013-06-10 11:39:50 +0300306 .mode = 0644,
Eliezer Tamireb6db622013-06-14 16:33:25 +0300307 .proc_handler = proc_dointvec
Eliezer Tamir06021292013-06-10 11:39:50 +0300308 },
Eliezer Tamir2d48d672013-06-24 10:28:03 +0300309 {
Eliezer Tamir64b0dc52013-07-10 17:13:36 +0300310 .procname = "busy_read",
311 .data = &sysctl_net_busy_read,
Eliezer Tamir2d48d672013-06-24 10:28:03 +0300312 .maxlen = sizeof(unsigned int),
313 .mode = 0644,
314 .proc_handler = proc_dointvec
315 },
316#
Eliezer Tamir06021292013-06-10 11:39:50 +0300317#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318#endif /* CONFIG_NET */
319 {
Stephen Hemminger51b0bde2005-06-23 20:14:40 -0700320 .procname = "netdev_budget",
321 .data = &netdev_budget,
322 .maxlen = sizeof(int),
323 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800324 .proc_handler = proc_dointvec
Stephen Hemminger51b0bde2005-06-23 20:14:40 -0700325 },
Stephen Hemmingera2a316f2007-03-08 20:41:08 -0800326 {
Stephen Hemmingera2a316f2007-03-08 20:41:08 -0800327 .procname = "warnings",
328 .data = &net_msg_warn,
329 .maxlen = sizeof(int),
330 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800331 .proc_handler = proc_dointvec
Stephen Hemmingera2a316f2007-03-08 20:41:08 -0800332 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800333 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334};
Pavel Emelyanov33eb9cf2007-12-05 01:37:34 -0800335
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700336static struct ctl_table netns_core_table[] = {
337 {
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700338 .procname = "somaxconn",
339 .data = &init_net.core.sysctl_somaxconn,
340 .maxlen = sizeof(int),
341 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800342 .proc_handler = proc_dointvec
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700343 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800344 { }
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700345};
346
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800347static __net_init int sysctl_core_net_init(struct net *net)
348{
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700349 struct ctl_table *tbl;
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800350
Pavel Emelyanov8efa6e92008-03-31 19:41:14 -0700351 net->core.sysctl_somaxconn = SOMAXCONN;
Pavel Emelyanovb8e1f9b2007-12-08 00:12:33 -0800352
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700353 tbl = netns_core_table;
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800354 if (!net_eq(net, &init_net)) {
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700355 tbl = kmemdup(tbl, sizeof(netns_core_table), GFP_KERNEL);
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800356 if (tbl == NULL)
357 goto err_dup;
358
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700359 tbl[0].data = &net->core.sysctl_somaxconn;
Eric W. Biederman464dc802012-11-16 03:02:59 +0000360
361 /* Don't export any sysctls to unprivileged users */
362 if (net->user_ns != &init_user_ns) {
363 tbl[0].procname = NULL;
364 }
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800365 }
366
Eric W. Biedermanec8f23c2012-04-19 13:44:49 +0000367 net->core.sysctl_hdr = register_net_sysctl(net, "net/core", tbl);
Pavel Emelyanov8efa6e92008-03-31 19:41:14 -0700368 if (net->core.sysctl_hdr == NULL)
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800369 goto err_reg;
370
371 return 0;
372
373err_reg:
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700374 if (tbl != netns_core_table)
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800375 kfree(tbl);
376err_dup:
377 return -ENOMEM;
378}
379
380static __net_exit void sysctl_core_net_exit(struct net *net)
381{
382 struct ctl_table *tbl;
383
Pavel Emelyanov8efa6e92008-03-31 19:41:14 -0700384 tbl = net->core.sysctl_hdr->ctl_table_arg;
385 unregister_net_sysctl_table(net->core.sysctl_hdr);
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700386 BUG_ON(tbl == netns_core_table);
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800387 kfree(tbl);
388}
389
390static __net_initdata struct pernet_operations sysctl_core_ops = {
391 .init = sysctl_core_net_init,
392 .exit = sysctl_core_net_exit,
393};
394
Pavel Emelyanov33eb9cf2007-12-05 01:37:34 -0800395static __init int sysctl_core_init(void)
396{
Eric W. Biederman43444752012-04-19 13:22:55 +0000397 register_net_sysctl(&init_net, "net/core", net_core_table);
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800398 return register_pernet_subsys(&sysctl_core_ops);
Pavel Emelyanov33eb9cf2007-12-05 01:37:34 -0800399}
400
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800401fs_initcall(sysctl_core_init);