blob: cf9cd13509a7d531f9e4a455d2909f5dfb731923 [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>
stephen hemminger6da7c8f2013-08-27 16:19:08 -070023#include <net/pkt_sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Roman Gushchin5f671d62013-08-02 18:36:40 +040025static int zero = 0;
bingtian.ly@taobao.comcdda8892013-01-23 20:35:28 +000026static int one = 1;
Roman Gushchin5f671d62013-08-02 18:36:40 +040027static int ushort_max = USHRT_MAX;
bingtian.ly@taobao.comcdda8892013-01-23 20:35:28 +000028
Tom Herbertfec5e652010-04-16 16:01:27 -070029#ifdef CONFIG_RPS
Joe Perchesfe2c6332013-06-11 23:04:25 -070030static int rps_sock_flow_sysctl(struct ctl_table *table, int write,
Tom Herbertfec5e652010-04-16 16:01:27 -070031 void __user *buffer, size_t *lenp, loff_t *ppos)
32{
33 unsigned int orig_size, size;
34 int ret, i;
Joe Perchesfe2c6332013-06-11 23:04:25 -070035 struct ctl_table tmp = {
Tom Herbertfec5e652010-04-16 16:01:27 -070036 .data = &size,
37 .maxlen = sizeof(size),
38 .mode = table->mode
39 };
40 struct rps_sock_flow_table *orig_sock_table, *sock_table;
41 static DEFINE_MUTEX(sock_flow_mutex);
42
43 mutex_lock(&sock_flow_mutex);
44
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +000045 orig_sock_table = rcu_dereference_protected(rps_sock_flow_table,
46 lockdep_is_held(&sock_flow_mutex));
Tom Herbertfec5e652010-04-16 16:01:27 -070047 size = orig_size = orig_sock_table ? orig_sock_table->mask + 1 : 0;
48
49 ret = proc_dointvec(&tmp, write, buffer, lenp, ppos);
50
51 if (write) {
52 if (size) {
53 if (size > 1<<30) {
54 /* Enforce limit to prevent overflow */
55 mutex_unlock(&sock_flow_mutex);
56 return -EINVAL;
57 }
58 size = roundup_pow_of_two(size);
59 if (size != orig_size) {
60 sock_table =
61 vmalloc(RPS_SOCK_FLOW_TABLE_SIZE(size));
62 if (!sock_table) {
63 mutex_unlock(&sock_flow_mutex);
64 return -ENOMEM;
65 }
66
67 sock_table->mask = size - 1;
68 } else
69 sock_table = orig_sock_table;
70
71 for (i = 0; i < size; i++)
72 sock_table->ents[i] = RPS_NO_CPU;
73 } else
74 sock_table = NULL;
75
76 if (sock_table != orig_sock_table) {
77 rcu_assign_pointer(rps_sock_flow_table, sock_table);
Eric Dumazetadc93002011-11-17 03:13:26 +000078 if (sock_table)
Ingo Molnarc5905af2012-02-24 08:31:31 +010079 static_key_slow_inc(&rps_needed);
Eric Dumazetadc93002011-11-17 03:13:26 +000080 if (orig_sock_table) {
Ingo Molnarc5905af2012-02-24 08:31:31 +010081 static_key_slow_dec(&rps_needed);
Eric Dumazetadc93002011-11-17 03:13:26 +000082 synchronize_rcu();
83 vfree(orig_sock_table);
84 }
Tom Herbertfec5e652010-04-16 16:01:27 -070085 }
86 }
87
88 mutex_unlock(&sock_flow_mutex);
89
90 return ret;
91}
92#endif /* CONFIG_RPS */
93
Willem de Bruijn99bbc702013-05-20 04:02:32 +000094#ifdef CONFIG_NET_FLOW_LIMIT
95static DEFINE_MUTEX(flow_limit_update_mutex);
96
Joe Perchesfe2c6332013-06-11 23:04:25 -070097static int flow_limit_cpu_sysctl(struct ctl_table *table, int write,
Willem de Bruijn99bbc702013-05-20 04:02:32 +000098 void __user *buffer, size_t *lenp,
99 loff_t *ppos)
100{
101 struct sd_flow_limit *cur;
102 struct softnet_data *sd;
103 cpumask_var_t mask;
104 int i, len, ret = 0;
105
106 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
107 return -ENOMEM;
108
109 if (write) {
110 ret = cpumask_parse_user(buffer, *lenp, mask);
111 if (ret)
112 goto done;
113
114 mutex_lock(&flow_limit_update_mutex);
115 len = sizeof(*cur) + netdev_flow_limit_table_len;
116 for_each_possible_cpu(i) {
117 sd = &per_cpu(softnet_data, i);
118 cur = rcu_dereference_protected(sd->flow_limit,
119 lockdep_is_held(&flow_limit_update_mutex));
120 if (cur && !cpumask_test_cpu(i, mask)) {
121 RCU_INIT_POINTER(sd->flow_limit, NULL);
122 synchronize_rcu();
123 kfree(cur);
124 } else if (!cur && cpumask_test_cpu(i, mask)) {
Eric Dumazet5b59d462013-12-18 06:46:23 -0800125 cur = kzalloc_node(len, GFP_KERNEL,
126 cpu_to_node(i));
Willem de Bruijn99bbc702013-05-20 04:02:32 +0000127 if (!cur) {
128 /* not unwinding previous changes */
129 ret = -ENOMEM;
130 goto write_unlock;
131 }
132 cur->num_buckets = netdev_flow_limit_table_len;
133 rcu_assign_pointer(sd->flow_limit, cur);
134 }
135 }
136write_unlock:
137 mutex_unlock(&flow_limit_update_mutex);
138 } else {
Willem de Bruijn5f121b92013-06-13 15:29:38 -0400139 char kbuf[128];
140
Willem de Bruijn99bbc702013-05-20 04:02:32 +0000141 if (*ppos || !*lenp) {
142 *lenp = 0;
143 goto done;
144 }
145
146 cpumask_clear(mask);
147 rcu_read_lock();
148 for_each_possible_cpu(i) {
149 sd = &per_cpu(softnet_data, i);
150 if (rcu_dereference(sd->flow_limit))
151 cpumask_set_cpu(i, mask);
152 }
153 rcu_read_unlock();
154
Willem de Bruijn5f121b92013-06-13 15:29:38 -0400155 len = min(sizeof(kbuf) - 1, *lenp);
156 len = cpumask_scnprintf(kbuf, len, mask);
157 if (!len) {
158 *lenp = 0;
159 goto done;
160 }
161 if (len < *lenp)
162 kbuf[len++] = '\n';
163 if (copy_to_user(buffer, kbuf, len)) {
164 ret = -EFAULT;
165 goto done;
166 }
167 *lenp = len;
168 *ppos += len;
Willem de Bruijn99bbc702013-05-20 04:02:32 +0000169 }
170
171done:
172 free_cpumask_var(mask);
173 return ret;
174}
175
Joe Perchesfe2c6332013-06-11 23:04:25 -0700176static int flow_limit_table_len_sysctl(struct ctl_table *table, int write,
Willem de Bruijn99bbc702013-05-20 04:02:32 +0000177 void __user *buffer, size_t *lenp,
178 loff_t *ppos)
179{
180 unsigned int old, *ptr;
181 int ret;
182
183 mutex_lock(&flow_limit_update_mutex);
184
185 ptr = table->data;
186 old = *ptr;
187 ret = proc_dointvec(table, write, buffer, lenp, ppos);
188 if (!ret && write && !is_power_of_2(*ptr)) {
189 *ptr = old;
190 ret = -EINVAL;
191 }
192
193 mutex_unlock(&flow_limit_update_mutex);
194 return ret;
195}
196#endif /* CONFIG_NET_FLOW_LIMIT */
197
stephen hemminger6da7c8f2013-08-27 16:19:08 -0700198#ifdef CONFIG_NET_SCHED
199static int set_default_qdisc(struct ctl_table *table, int write,
200 void __user *buffer, size_t *lenp, loff_t *ppos)
201{
202 char id[IFNAMSIZ];
203 struct ctl_table tbl = {
204 .data = id,
205 .maxlen = IFNAMSIZ,
206 };
207 int ret;
208
209 qdisc_get_default(id, IFNAMSIZ);
210
211 ret = proc_dostring(&tbl, write, buffer, lenp, ppos);
212 if (write && ret == 0)
213 ret = qdisc_set_default(id);
214 return ret;
215}
216#endif
217
Pavel Emelyanov33eb9cf2007-12-05 01:37:34 -0800218static struct ctl_table net_core_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219#ifdef CONFIG_NET
220 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 .procname = "wmem_max",
222 .data = &sysctl_wmem_max,
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 = "rmem_max",
230 .data = &sysctl_rmem_max,
231 .maxlen = sizeof(int),
232 .mode = 0644,
bingtian.ly@taobao.comcdda8892013-01-23 20:35:28 +0000233 .proc_handler = proc_dointvec_minmax,
234 .extra1 = &one,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 },
236 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 .procname = "wmem_default",
238 .data = &sysctl_wmem_default,
239 .maxlen = sizeof(int),
240 .mode = 0644,
bingtian.ly@taobao.comcdda8892013-01-23 20:35:28 +0000241 .proc_handler = proc_dointvec_minmax,
242 .extra1 = &one,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 },
244 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 .procname = "rmem_default",
246 .data = &sysctl_rmem_default,
247 .maxlen = sizeof(int),
248 .mode = 0644,
bingtian.ly@taobao.comcdda8892013-01-23 20:35:28 +0000249 .proc_handler = proc_dointvec_minmax,
250 .extra1 = &one,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 },
252 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 .procname = "dev_weight",
254 .data = &weight_p,
255 .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 = "netdev_max_backlog",
261 .data = &netdev_max_backlog,
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 },
Eric Dumazet0a148422011-04-20 09:27:32 +0000266#ifdef CONFIG_BPF_JIT
267 {
268 .procname = "bpf_jit_enable",
269 .data = &bpf_jit_enable,
270 .maxlen = sizeof(int),
271 .mode = 0644,
272 .proc_handler = proc_dointvec
273 },
274#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 {
Eric Dumazet3b098e22010-05-15 23:57:10 -0700276 .procname = "netdev_tstamp_prequeue",
277 .data = &netdev_tstamp_prequeue,
278 .maxlen = sizeof(int),
279 .mode = 0644,
280 .proc_handler = proc_dointvec
281 },
282 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 .procname = "message_cost",
Dave Young717115e2008-07-25 01:45:58 -0700284 .data = &net_ratelimit_state.interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 .maxlen = sizeof(int),
286 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800287 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 },
289 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 .procname = "message_burst",
Dave Young717115e2008-07-25 01:45:58 -0700291 .data = &net_ratelimit_state.burst,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 .maxlen = sizeof(int),
293 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800294 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 },
296 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 .procname = "optmem_max",
298 .data = &sysctl_optmem_max,
299 .maxlen = sizeof(int),
300 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800301 .proc_handler = proc_dointvec
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 },
Tom Herbertfec5e652010-04-16 16:01:27 -0700303#ifdef CONFIG_RPS
304 {
305 .procname = "rps_sock_flow_entries",
306 .maxlen = sizeof(int),
307 .mode = 0644,
308 .proc_handler = rps_sock_flow_sysctl
309 },
310#endif
Willem de Bruijn99bbc702013-05-20 04:02:32 +0000311#ifdef CONFIG_NET_FLOW_LIMIT
312 {
313 .procname = "flow_limit_cpu_bitmap",
314 .mode = 0644,
315 .proc_handler = flow_limit_cpu_sysctl
316 },
317 {
318 .procname = "flow_limit_table_len",
319 .data = &netdev_flow_limit_table_len,
320 .maxlen = sizeof(int),
321 .mode = 0644,
322 .proc_handler = flow_limit_table_len_sysctl
323 },
324#endif /* CONFIG_NET_FLOW_LIMIT */
Cong Wange0d10952013-08-01 11:10:25 +0800325#ifdef CONFIG_NET_RX_BUSY_POLL
Eliezer Tamir06021292013-06-10 11:39:50 +0300326 {
Eliezer Tamir64b0dc52013-07-10 17:13:36 +0300327 .procname = "busy_poll",
328 .data = &sysctl_net_busy_poll,
Eliezer Tamireb6db622013-06-14 16:33:25 +0300329 .maxlen = sizeof(unsigned int),
Eliezer Tamir06021292013-06-10 11:39:50 +0300330 .mode = 0644,
Eliezer Tamireb6db622013-06-14 16:33:25 +0300331 .proc_handler = proc_dointvec
Eliezer Tamir06021292013-06-10 11:39:50 +0300332 },
Eliezer Tamir2d48d672013-06-24 10:28:03 +0300333 {
Eliezer Tamir64b0dc52013-07-10 17:13:36 +0300334 .procname = "busy_read",
335 .data = &sysctl_net_busy_read,
Eliezer Tamir2d48d672013-06-24 10:28:03 +0300336 .maxlen = sizeof(unsigned int),
337 .mode = 0644,
338 .proc_handler = proc_dointvec
339 },
stephen hemminger6da7c8f2013-08-27 16:19:08 -0700340#endif
341#ifdef CONFIG_NET_SCHED
342 {
343 .procname = "default_qdisc",
344 .mode = 0644,
345 .maxlen = IFNAMSIZ,
346 .proc_handler = set_default_qdisc
347 },
Eliezer Tamir06021292013-06-10 11:39:50 +0300348#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349#endif /* CONFIG_NET */
350 {
Stephen Hemminger51b0bde2005-06-23 20:14:40 -0700351 .procname = "netdev_budget",
352 .data = &netdev_budget,
353 .maxlen = sizeof(int),
354 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800355 .proc_handler = proc_dointvec
Stephen Hemminger51b0bde2005-06-23 20:14:40 -0700356 },
Stephen Hemmingera2a316f2007-03-08 20:41:08 -0800357 {
Stephen Hemmingera2a316f2007-03-08 20:41:08 -0800358 .procname = "warnings",
359 .data = &net_msg_warn,
360 .maxlen = sizeof(int),
361 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800362 .proc_handler = proc_dointvec
Stephen Hemmingera2a316f2007-03-08 20:41:08 -0800363 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800364 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365};
Pavel Emelyanov33eb9cf2007-12-05 01:37:34 -0800366
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700367static struct ctl_table netns_core_table[] = {
368 {
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700369 .procname = "somaxconn",
370 .data = &init_net.core.sysctl_somaxconn,
371 .maxlen = sizeof(int),
372 .mode = 0644,
Roman Gushchin5f671d62013-08-02 18:36:40 +0400373 .extra1 = &zero,
374 .extra2 = &ushort_max,
375 .proc_handler = proc_dointvec_minmax
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700376 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800377 { }
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700378};
379
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800380static __net_init int sysctl_core_net_init(struct net *net)
381{
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700382 struct ctl_table *tbl;
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800383
Pavel Emelyanov8efa6e92008-03-31 19:41:14 -0700384 net->core.sysctl_somaxconn = SOMAXCONN;
Pavel Emelyanovb8e1f9b2007-12-08 00:12:33 -0800385
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700386 tbl = netns_core_table;
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800387 if (!net_eq(net, &init_net)) {
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700388 tbl = kmemdup(tbl, sizeof(netns_core_table), GFP_KERNEL);
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800389 if (tbl == NULL)
390 goto err_dup;
391
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700392 tbl[0].data = &net->core.sysctl_somaxconn;
Eric W. Biederman464dc802012-11-16 03:02:59 +0000393
394 /* Don't export any sysctls to unprivileged users */
395 if (net->user_ns != &init_user_ns) {
396 tbl[0].procname = NULL;
397 }
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800398 }
399
Eric W. Biedermanec8f23c2012-04-19 13:44:49 +0000400 net->core.sysctl_hdr = register_net_sysctl(net, "net/core", tbl);
Pavel Emelyanov8efa6e92008-03-31 19:41:14 -0700401 if (net->core.sysctl_hdr == NULL)
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800402 goto err_reg;
403
404 return 0;
405
406err_reg:
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700407 if (tbl != netns_core_table)
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800408 kfree(tbl);
409err_dup:
410 return -ENOMEM;
411}
412
413static __net_exit void sysctl_core_net_exit(struct net *net)
414{
415 struct ctl_table *tbl;
416
Pavel Emelyanov8efa6e92008-03-31 19:41:14 -0700417 tbl = net->core.sysctl_hdr->ctl_table_arg;
418 unregister_net_sysctl_table(net->core.sysctl_hdr);
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700419 BUG_ON(tbl == netns_core_table);
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800420 kfree(tbl);
421}
422
423static __net_initdata struct pernet_operations sysctl_core_ops = {
424 .init = sysctl_core_net_init,
425 .exit = sysctl_core_net_exit,
426};
427
Pavel Emelyanov33eb9cf2007-12-05 01:37:34 -0800428static __init int sysctl_core_init(void)
429{
Eric W. Biederman43444752012-04-19 13:22:55 +0000430 register_net_sysctl(&init_net, "net/core", net_core_table);
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800431 return register_pernet_subsys(&sysctl_core_ops);
Pavel Emelyanov33eb9cf2007-12-05 01:37:34 -0800432}
433
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800434fs_initcall(sysctl_core_init);