blob: 31107abd278391dfe6752a46fea03ec22978b604 [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
Roman Gushchin5f671d62013-08-02 18:36:40 +040024static int zero = 0;
bingtian.ly@taobao.comcdda8892013-01-23 20:35:28 +000025static int one = 1;
Roman Gushchin5f671d62013-08-02 18:36:40 +040026static int ushort_max = USHRT_MAX;
bingtian.ly@taobao.comcdda8892013-01-23 20:35:28 +000027
Tom Herbertfec5e652010-04-16 16:01:27 -070028#ifdef CONFIG_RPS
Joe Perchesfe2c6332013-06-11 23:04:25 -070029static int rps_sock_flow_sysctl(struct ctl_table *table, int write,
Tom Herbertfec5e652010-04-16 16:01:27 -070030 void __user *buffer, size_t *lenp, loff_t *ppos)
31{
32 unsigned int orig_size, size;
33 int ret, i;
Joe Perchesfe2c6332013-06-11 23:04:25 -070034 struct ctl_table tmp = {
Tom Herbertfec5e652010-04-16 16:01:27 -070035 .data = &size,
36 .maxlen = sizeof(size),
37 .mode = table->mode
38 };
39 struct rps_sock_flow_table *orig_sock_table, *sock_table;
40 static DEFINE_MUTEX(sock_flow_mutex);
41
42 mutex_lock(&sock_flow_mutex);
43
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +000044 orig_sock_table = rcu_dereference_protected(rps_sock_flow_table,
45 lockdep_is_held(&sock_flow_mutex));
Tom Herbertfec5e652010-04-16 16:01:27 -070046 size = orig_size = orig_sock_table ? orig_sock_table->mask + 1 : 0;
47
48 ret = proc_dointvec(&tmp, write, buffer, lenp, ppos);
49
50 if (write) {
51 if (size) {
52 if (size > 1<<30) {
53 /* Enforce limit to prevent overflow */
54 mutex_unlock(&sock_flow_mutex);
55 return -EINVAL;
56 }
57 size = roundup_pow_of_two(size);
58 if (size != orig_size) {
59 sock_table =
60 vmalloc(RPS_SOCK_FLOW_TABLE_SIZE(size));
61 if (!sock_table) {
62 mutex_unlock(&sock_flow_mutex);
63 return -ENOMEM;
64 }
65
66 sock_table->mask = size - 1;
67 } else
68 sock_table = orig_sock_table;
69
70 for (i = 0; i < size; i++)
71 sock_table->ents[i] = RPS_NO_CPU;
72 } else
73 sock_table = NULL;
74
75 if (sock_table != orig_sock_table) {
76 rcu_assign_pointer(rps_sock_flow_table, sock_table);
Eric Dumazetadc93002011-11-17 03:13:26 +000077 if (sock_table)
Ingo Molnarc5905af2012-02-24 08:31:31 +010078 static_key_slow_inc(&rps_needed);
Eric Dumazetadc93002011-11-17 03:13:26 +000079 if (orig_sock_table) {
Ingo Molnarc5905af2012-02-24 08:31:31 +010080 static_key_slow_dec(&rps_needed);
Eric Dumazetadc93002011-11-17 03:13:26 +000081 synchronize_rcu();
82 vfree(orig_sock_table);
83 }
Tom Herbertfec5e652010-04-16 16:01:27 -070084 }
85 }
86
87 mutex_unlock(&sock_flow_mutex);
88
89 return ret;
90}
91#endif /* CONFIG_RPS */
92
Willem de Bruijn99bbc702013-05-20 04:02:32 +000093#ifdef CONFIG_NET_FLOW_LIMIT
94static DEFINE_MUTEX(flow_limit_update_mutex);
95
Joe Perchesfe2c6332013-06-11 23:04:25 -070096static int flow_limit_cpu_sysctl(struct ctl_table *table, int write,
Willem de Bruijn99bbc702013-05-20 04:02:32 +000097 void __user *buffer, size_t *lenp,
98 loff_t *ppos)
99{
100 struct sd_flow_limit *cur;
101 struct softnet_data *sd;
102 cpumask_var_t mask;
103 int i, len, ret = 0;
104
105 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
106 return -ENOMEM;
107
108 if (write) {
109 ret = cpumask_parse_user(buffer, *lenp, mask);
110 if (ret)
111 goto done;
112
113 mutex_lock(&flow_limit_update_mutex);
114 len = sizeof(*cur) + netdev_flow_limit_table_len;
115 for_each_possible_cpu(i) {
116 sd = &per_cpu(softnet_data, i);
117 cur = rcu_dereference_protected(sd->flow_limit,
118 lockdep_is_held(&flow_limit_update_mutex));
119 if (cur && !cpumask_test_cpu(i, mask)) {
120 RCU_INIT_POINTER(sd->flow_limit, NULL);
121 synchronize_rcu();
122 kfree(cur);
123 } else if (!cur && cpumask_test_cpu(i, mask)) {
124 cur = kzalloc(len, GFP_KERNEL);
125 if (!cur) {
126 /* not unwinding previous changes */
127 ret = -ENOMEM;
128 goto write_unlock;
129 }
130 cur->num_buckets = netdev_flow_limit_table_len;
131 rcu_assign_pointer(sd->flow_limit, cur);
132 }
133 }
134write_unlock:
135 mutex_unlock(&flow_limit_update_mutex);
136 } else {
Willem de Bruijn5f121b92013-06-13 15:29:38 -0400137 char kbuf[128];
138
Willem de Bruijn99bbc702013-05-20 04:02:32 +0000139 if (*ppos || !*lenp) {
140 *lenp = 0;
141 goto done;
142 }
143
144 cpumask_clear(mask);
145 rcu_read_lock();
146 for_each_possible_cpu(i) {
147 sd = &per_cpu(softnet_data, i);
148 if (rcu_dereference(sd->flow_limit))
149 cpumask_set_cpu(i, mask);
150 }
151 rcu_read_unlock();
152
Willem de Bruijn5f121b92013-06-13 15:29:38 -0400153 len = min(sizeof(kbuf) - 1, *lenp);
154 len = cpumask_scnprintf(kbuf, len, mask);
155 if (!len) {
156 *lenp = 0;
157 goto done;
158 }
159 if (len < *lenp)
160 kbuf[len++] = '\n';
161 if (copy_to_user(buffer, kbuf, len)) {
162 ret = -EFAULT;
163 goto done;
164 }
165 *lenp = len;
166 *ppos += len;
Willem de Bruijn99bbc702013-05-20 04:02:32 +0000167 }
168
169done:
170 free_cpumask_var(mask);
171 return ret;
172}
173
Joe Perchesfe2c6332013-06-11 23:04:25 -0700174static int flow_limit_table_len_sysctl(struct ctl_table *table, int write,
Willem de Bruijn99bbc702013-05-20 04:02:32 +0000175 void __user *buffer, size_t *lenp,
176 loff_t *ppos)
177{
178 unsigned int old, *ptr;
179 int ret;
180
181 mutex_lock(&flow_limit_update_mutex);
182
183 ptr = table->data;
184 old = *ptr;
185 ret = proc_dointvec(table, write, buffer, lenp, ppos);
186 if (!ret && write && !is_power_of_2(*ptr)) {
187 *ptr = old;
188 ret = -EINVAL;
189 }
190
191 mutex_unlock(&flow_limit_update_mutex);
192 return ret;
193}
194#endif /* CONFIG_NET_FLOW_LIMIT */
195
Pavel Emelyanov33eb9cf2007-12-05 01:37:34 -0800196static struct ctl_table net_core_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197#ifdef CONFIG_NET
198 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 .procname = "wmem_max",
200 .data = &sysctl_wmem_max,
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_max",
208 .data = &sysctl_rmem_max,
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 = "wmem_default",
216 .data = &sysctl_wmem_default,
217 .maxlen = sizeof(int),
218 .mode = 0644,
bingtian.ly@taobao.comcdda8892013-01-23 20:35:28 +0000219 .proc_handler = proc_dointvec_minmax,
220 .extra1 = &one,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 },
222 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 .procname = "rmem_default",
224 .data = &sysctl_rmem_default,
225 .maxlen = sizeof(int),
226 .mode = 0644,
bingtian.ly@taobao.comcdda8892013-01-23 20:35:28 +0000227 .proc_handler = proc_dointvec_minmax,
228 .extra1 = &one,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 },
230 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 .procname = "dev_weight",
232 .data = &weight_p,
233 .maxlen = sizeof(int),
234 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800235 .proc_handler = proc_dointvec
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 },
237 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 .procname = "netdev_max_backlog",
239 .data = &netdev_max_backlog,
240 .maxlen = sizeof(int),
241 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800242 .proc_handler = proc_dointvec
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 },
Eric Dumazet0a148422011-04-20 09:27:32 +0000244#ifdef CONFIG_BPF_JIT
245 {
246 .procname = "bpf_jit_enable",
247 .data = &bpf_jit_enable,
248 .maxlen = sizeof(int),
249 .mode = 0644,
250 .proc_handler = proc_dointvec
251 },
252#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 {
Eric Dumazet3b098e22010-05-15 23:57:10 -0700254 .procname = "netdev_tstamp_prequeue",
255 .data = &netdev_tstamp_prequeue,
256 .maxlen = sizeof(int),
257 .mode = 0644,
258 .proc_handler = proc_dointvec
259 },
260 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 .procname = "message_cost",
Dave Young717115e2008-07-25 01:45:58 -0700262 .data = &net_ratelimit_state.interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 .maxlen = sizeof(int),
264 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800265 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 },
267 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 .procname = "message_burst",
Dave Young717115e2008-07-25 01:45:58 -0700269 .data = &net_ratelimit_state.burst,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 .maxlen = sizeof(int),
271 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800272 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 },
274 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 .procname = "optmem_max",
276 .data = &sysctl_optmem_max,
277 .maxlen = sizeof(int),
278 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800279 .proc_handler = proc_dointvec
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 },
Tom Herbertfec5e652010-04-16 16:01:27 -0700281#ifdef CONFIG_RPS
282 {
283 .procname = "rps_sock_flow_entries",
284 .maxlen = sizeof(int),
285 .mode = 0644,
286 .proc_handler = rps_sock_flow_sysctl
287 },
288#endif
Willem de Bruijn99bbc702013-05-20 04:02:32 +0000289#ifdef CONFIG_NET_FLOW_LIMIT
290 {
291 .procname = "flow_limit_cpu_bitmap",
292 .mode = 0644,
293 .proc_handler = flow_limit_cpu_sysctl
294 },
295 {
296 .procname = "flow_limit_table_len",
297 .data = &netdev_flow_limit_table_len,
298 .maxlen = sizeof(int),
299 .mode = 0644,
300 .proc_handler = flow_limit_table_len_sysctl
301 },
302#endif /* CONFIG_NET_FLOW_LIMIT */
Cong Wange0d10952013-08-01 11:10:25 +0800303#ifdef CONFIG_NET_RX_BUSY_POLL
Eliezer Tamir06021292013-06-10 11:39:50 +0300304 {
Eliezer Tamir64b0dc52013-07-10 17:13:36 +0300305 .procname = "busy_poll",
306 .data = &sysctl_net_busy_poll,
Eliezer Tamireb6db622013-06-14 16:33:25 +0300307 .maxlen = sizeof(unsigned int),
Eliezer Tamir06021292013-06-10 11:39:50 +0300308 .mode = 0644,
Eliezer Tamireb6db622013-06-14 16:33:25 +0300309 .proc_handler = proc_dointvec
Eliezer Tamir06021292013-06-10 11:39:50 +0300310 },
Eliezer Tamir2d48d672013-06-24 10:28:03 +0300311 {
Eliezer Tamir64b0dc52013-07-10 17:13:36 +0300312 .procname = "busy_read",
313 .data = &sysctl_net_busy_read,
Eliezer Tamir2d48d672013-06-24 10:28:03 +0300314 .maxlen = sizeof(unsigned int),
315 .mode = 0644,
316 .proc_handler = proc_dointvec
317 },
318#
Eliezer Tamir06021292013-06-10 11:39:50 +0300319#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320#endif /* CONFIG_NET */
321 {
Stephen Hemminger51b0bde2005-06-23 20:14:40 -0700322 .procname = "netdev_budget",
323 .data = &netdev_budget,
324 .maxlen = sizeof(int),
325 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800326 .proc_handler = proc_dointvec
Stephen Hemminger51b0bde2005-06-23 20:14:40 -0700327 },
Stephen Hemmingera2a316f2007-03-08 20:41:08 -0800328 {
Stephen Hemmingera2a316f2007-03-08 20:41:08 -0800329 .procname = "warnings",
330 .data = &net_msg_warn,
331 .maxlen = sizeof(int),
332 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800333 .proc_handler = proc_dointvec
Stephen Hemmingera2a316f2007-03-08 20:41:08 -0800334 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800335 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336};
Pavel Emelyanov33eb9cf2007-12-05 01:37:34 -0800337
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700338static struct ctl_table netns_core_table[] = {
339 {
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700340 .procname = "somaxconn",
341 .data = &init_net.core.sysctl_somaxconn,
342 .maxlen = sizeof(int),
343 .mode = 0644,
Roman Gushchin5f671d62013-08-02 18:36:40 +0400344 .extra1 = &zero,
345 .extra2 = &ushort_max,
346 .proc_handler = proc_dointvec_minmax
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700347 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800348 { }
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700349};
350
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800351static __net_init int sysctl_core_net_init(struct net *net)
352{
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700353 struct ctl_table *tbl;
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800354
Pavel Emelyanov8efa6e92008-03-31 19:41:14 -0700355 net->core.sysctl_somaxconn = SOMAXCONN;
Pavel Emelyanovb8e1f9b2007-12-08 00:12:33 -0800356
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700357 tbl = netns_core_table;
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800358 if (!net_eq(net, &init_net)) {
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700359 tbl = kmemdup(tbl, sizeof(netns_core_table), GFP_KERNEL);
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800360 if (tbl == NULL)
361 goto err_dup;
362
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700363 tbl[0].data = &net->core.sysctl_somaxconn;
Eric W. Biederman464dc802012-11-16 03:02:59 +0000364
365 /* Don't export any sysctls to unprivileged users */
366 if (net->user_ns != &init_user_ns) {
367 tbl[0].procname = NULL;
368 }
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800369 }
370
Eric W. Biedermanec8f23c2012-04-19 13:44:49 +0000371 net->core.sysctl_hdr = register_net_sysctl(net, "net/core", tbl);
Pavel Emelyanov8efa6e92008-03-31 19:41:14 -0700372 if (net->core.sysctl_hdr == NULL)
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800373 goto err_reg;
374
375 return 0;
376
377err_reg:
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700378 if (tbl != netns_core_table)
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800379 kfree(tbl);
380err_dup:
381 return -ENOMEM;
382}
383
384static __net_exit void sysctl_core_net_exit(struct net *net)
385{
386 struct ctl_table *tbl;
387
Pavel Emelyanov8efa6e92008-03-31 19:41:14 -0700388 tbl = net->core.sysctl_hdr->ctl_table_arg;
389 unregister_net_sysctl_table(net->core.sysctl_hdr);
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700390 BUG_ON(tbl == netns_core_table);
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800391 kfree(tbl);
392}
393
394static __net_initdata struct pernet_operations sysctl_core_ops = {
395 .init = sysctl_core_net_init,
396 .exit = sysctl_core_net_exit,
397};
398
Pavel Emelyanov33eb9cf2007-12-05 01:37:34 -0800399static __init int sysctl_core_init(void)
400{
Eric W. Biederman43444752012-04-19 13:22:55 +0000401 register_net_sysctl(&init_net, "net/core", net_core_table);
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800402 return register_pernet_subsys(&sysctl_core_ops);
Pavel Emelyanov33eb9cf2007-12-05 01:37:34 -0800403}
404
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800405fs_initcall(sysctl_core_init);