blob: 385b6095fdc4b1da8540c879d3a5c450954cb7f7 [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>
Ingo Molnar3fff4c42009-09-22 16:18:09 +020017
Hannes Eder63d819c2009-02-25 10:32:14 +000018#include <net/ip.h>
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -030019#include <net/sock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Tom Herbertfec5e652010-04-16 16:01:27 -070021#ifdef CONFIG_RPS
22static int rps_sock_flow_sysctl(ctl_table *table, int write,
23 void __user *buffer, size_t *lenp, loff_t *ppos)
24{
25 unsigned int orig_size, size;
26 int ret, i;
27 ctl_table tmp = {
28 .data = &size,
29 .maxlen = sizeof(size),
30 .mode = table->mode
31 };
32 struct rps_sock_flow_table *orig_sock_table, *sock_table;
33 static DEFINE_MUTEX(sock_flow_mutex);
34
35 mutex_lock(&sock_flow_mutex);
36
Eric Dumazet6e3f7fa2010-10-25 03:02:02 +000037 orig_sock_table = rcu_dereference_protected(rps_sock_flow_table,
38 lockdep_is_held(&sock_flow_mutex));
Tom Herbertfec5e652010-04-16 16:01:27 -070039 size = orig_size = orig_sock_table ? orig_sock_table->mask + 1 : 0;
40
41 ret = proc_dointvec(&tmp, write, buffer, lenp, ppos);
42
43 if (write) {
44 if (size) {
45 if (size > 1<<30) {
46 /* Enforce limit to prevent overflow */
47 mutex_unlock(&sock_flow_mutex);
48 return -EINVAL;
49 }
50 size = roundup_pow_of_two(size);
51 if (size != orig_size) {
52 sock_table =
53 vmalloc(RPS_SOCK_FLOW_TABLE_SIZE(size));
54 if (!sock_table) {
55 mutex_unlock(&sock_flow_mutex);
56 return -ENOMEM;
57 }
58
59 sock_table->mask = size - 1;
60 } else
61 sock_table = orig_sock_table;
62
63 for (i = 0; i < size; i++)
64 sock_table->ents[i] = RPS_NO_CPU;
65 } else
66 sock_table = NULL;
67
68 if (sock_table != orig_sock_table) {
69 rcu_assign_pointer(rps_sock_flow_table, sock_table);
70 synchronize_rcu();
71 vfree(orig_sock_table);
72 }
73 }
74
75 mutex_unlock(&sock_flow_mutex);
76
77 return ret;
78}
79#endif /* CONFIG_RPS */
80
Pavel Emelyanov33eb9cf2007-12-05 01:37:34 -080081static struct ctl_table net_core_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#ifdef CONFIG_NET
83 {
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 .procname = "wmem_max",
85 .data = &sysctl_wmem_max,
86 .maxlen = sizeof(int),
87 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -080088 .proc_handler = proc_dointvec
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 },
90 {
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 .procname = "rmem_max",
92 .data = &sysctl_rmem_max,
93 .maxlen = sizeof(int),
94 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -080095 .proc_handler = proc_dointvec
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 },
97 {
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 .procname = "wmem_default",
99 .data = &sysctl_wmem_default,
100 .maxlen = sizeof(int),
101 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800102 .proc_handler = proc_dointvec
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 },
104 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 .procname = "rmem_default",
106 .data = &sysctl_rmem_default,
107 .maxlen = sizeof(int),
108 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800109 .proc_handler = proc_dointvec
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 },
111 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 .procname = "dev_weight",
113 .data = &weight_p,
114 .maxlen = sizeof(int),
115 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800116 .proc_handler = proc_dointvec
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 },
118 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 .procname = "netdev_max_backlog",
120 .data = &netdev_max_backlog,
121 .maxlen = sizeof(int),
122 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800123 .proc_handler = proc_dointvec
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 },
125 {
Eric Dumazet3b098e22010-05-15 23:57:10 -0700126 .procname = "netdev_tstamp_prequeue",
127 .data = &netdev_tstamp_prequeue,
128 .maxlen = sizeof(int),
129 .mode = 0644,
130 .proc_handler = proc_dointvec
131 },
132 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 .procname = "message_cost",
Dave Young717115e2008-07-25 01:45:58 -0700134 .data = &net_ratelimit_state.interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 .maxlen = sizeof(int),
136 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800137 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 },
139 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 .procname = "message_burst",
Dave Young717115e2008-07-25 01:45:58 -0700141 .data = &net_ratelimit_state.burst,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 .maxlen = sizeof(int),
143 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800144 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 },
146 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 .procname = "optmem_max",
148 .data = &sysctl_optmem_max,
149 .maxlen = sizeof(int),
150 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800151 .proc_handler = proc_dointvec
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 },
Tom Herbertfec5e652010-04-16 16:01:27 -0700153#ifdef CONFIG_RPS
154 {
155 .procname = "rps_sock_flow_entries",
156 .maxlen = sizeof(int),
157 .mode = 0644,
158 .proc_handler = rps_sock_flow_sysctl
159 },
160#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161#endif /* CONFIG_NET */
162 {
Stephen Hemminger51b0bde2005-06-23 20:14:40 -0700163 .procname = "netdev_budget",
164 .data = &netdev_budget,
165 .maxlen = sizeof(int),
166 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800167 .proc_handler = proc_dointvec
Stephen Hemminger51b0bde2005-06-23 20:14:40 -0700168 },
Stephen Hemmingera2a316f2007-03-08 20:41:08 -0800169 {
Stephen Hemmingera2a316f2007-03-08 20:41:08 -0800170 .procname = "warnings",
171 .data = &net_msg_warn,
172 .maxlen = sizeof(int),
173 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800174 .proc_handler = proc_dointvec
Stephen Hemmingera2a316f2007-03-08 20:41:08 -0800175 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800176 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177};
Pavel Emelyanov33eb9cf2007-12-05 01:37:34 -0800178
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700179static struct ctl_table netns_core_table[] = {
180 {
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700181 .procname = "somaxconn",
182 .data = &init_net.core.sysctl_somaxconn,
183 .maxlen = sizeof(int),
184 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800185 .proc_handler = proc_dointvec
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700186 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800187 { }
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700188};
189
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800190__net_initdata struct ctl_path net_core_path[] = {
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800191 { .procname = "net", },
192 { .procname = "core", },
Pavel Emelyanov33eb9cf2007-12-05 01:37:34 -0800193 { },
194};
195
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800196static __net_init int sysctl_core_net_init(struct net *net)
197{
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700198 struct ctl_table *tbl;
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800199
Pavel Emelyanov8efa6e92008-03-31 19:41:14 -0700200 net->core.sysctl_somaxconn = SOMAXCONN;
Pavel Emelyanovb8e1f9b2007-12-08 00:12:33 -0800201
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700202 tbl = netns_core_table;
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800203 if (!net_eq(net, &init_net)) {
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700204 tbl = kmemdup(tbl, sizeof(netns_core_table), GFP_KERNEL);
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800205 if (tbl == NULL)
206 goto err_dup;
207
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700208 tbl[0].data = &net->core.sysctl_somaxconn;
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800209 }
210
Pavel Emelyanov8efa6e92008-03-31 19:41:14 -0700211 net->core.sysctl_hdr = register_net_sysctl_table(net,
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800212 net_core_path, tbl);
Pavel Emelyanov8efa6e92008-03-31 19:41:14 -0700213 if (net->core.sysctl_hdr == NULL)
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800214 goto err_reg;
215
216 return 0;
217
218err_reg:
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700219 if (tbl != netns_core_table)
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800220 kfree(tbl);
221err_dup:
222 return -ENOMEM;
223}
224
225static __net_exit void sysctl_core_net_exit(struct net *net)
226{
227 struct ctl_table *tbl;
228
Pavel Emelyanov8efa6e92008-03-31 19:41:14 -0700229 tbl = net->core.sysctl_hdr->ctl_table_arg;
230 unregister_net_sysctl_table(net->core.sysctl_hdr);
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700231 BUG_ON(tbl == netns_core_table);
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800232 kfree(tbl);
233}
234
235static __net_initdata struct pernet_operations sysctl_core_ops = {
236 .init = sysctl_core_net_init,
237 .exit = sysctl_core_net_exit,
238};
239
Pavel Emelyanov33eb9cf2007-12-05 01:37:34 -0800240static __init int sysctl_core_init(void)
241{
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800242 static struct ctl_table empty[1];
243
244 register_sysctl_paths(net_core_path, empty);
Pavel Emelyanovd5a45022008-05-19 13:49:52 -0700245 register_net_sysctl_rotable(net_core_path, net_core_table);
Pavel Emelyanov024626e2007-12-08 00:09:24 -0800246 return register_pernet_subsys(&sysctl_core_ops);
Pavel Emelyanov33eb9cf2007-12-05 01:37:34 -0800247}
248
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800249fs_initcall(sysctl_core_init);