Linux 3.2.102
[pandora-kernel.git] / net / core / sysctl_net_core.c
1 /* -*- 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>
10 #include <linux/module.h>
11 #include <linux/socket.h>
12 #include <linux/netdevice.h>
13 #include <linux/ratelimit.h>
14 #include <linux/vmalloc.h>
15 #include <linux/init.h>
16 #include <linux/slab.h>
17
18 #include <net/ip.h>
19 #include <net/sock.h>
20 #include <net/net_ratelimit.h>
21
22 static int zero = 0;
23 static int ushort_max = USHRT_MAX;
24 static int min_sndbuf = SOCK_MIN_SNDBUF;
25 static int min_rcvbuf = SOCK_MIN_RCVBUF;
26
27 #ifdef CONFIG_RPS
28 static int rps_sock_flow_sysctl(ctl_table *table, int write,
29                                 void __user *buffer, size_t *lenp, loff_t *ppos)
30 {
31         unsigned int orig_size, size;
32         int ret, i;
33         ctl_table tmp = {
34                 .data = &size,
35                 .maxlen = sizeof(size),
36                 .mode = table->mode
37         };
38         struct rps_sock_flow_table *orig_sock_table, *sock_table;
39         static DEFINE_MUTEX(sock_flow_mutex);
40
41         mutex_lock(&sock_flow_mutex);
42
43         orig_sock_table = rcu_dereference_protected(rps_sock_flow_table,
44                                         lockdep_is_held(&sock_flow_mutex));
45         size = orig_size = orig_sock_table ? orig_sock_table->mask + 1 : 0;
46
47         ret = proc_dointvec(&tmp, write, buffer, lenp, ppos);
48
49         if (write) {
50                 if (size) {
51                         if (size > 1<<30) {
52                                 /* Enforce limit to prevent overflow */
53                                 mutex_unlock(&sock_flow_mutex);
54                                 return -EINVAL;
55                         }
56                         size = roundup_pow_of_two(size);
57                         if (size != orig_size) {
58                                 sock_table =
59                                     vmalloc(RPS_SOCK_FLOW_TABLE_SIZE(size));
60                                 if (!sock_table) {
61                                         mutex_unlock(&sock_flow_mutex);
62                                         return -ENOMEM;
63                                 }
64
65                                 sock_table->mask = size - 1;
66                         } else
67                                 sock_table = orig_sock_table;
68
69                         for (i = 0; i < size; i++)
70                                 sock_table->ents[i] = RPS_NO_CPU;
71                 } else
72                         sock_table = NULL;
73
74                 if (sock_table != orig_sock_table) {
75                         rcu_assign_pointer(rps_sock_flow_table, sock_table);
76                         synchronize_rcu();
77                         vfree(orig_sock_table);
78                 }
79         }
80
81         mutex_unlock(&sock_flow_mutex);
82
83         return ret;
84 }
85 #endif /* CONFIG_RPS */
86
87 static struct ctl_table net_core_table[] = {
88 #ifdef CONFIG_NET
89         {
90                 .procname       = "wmem_max",
91                 .data           = &sysctl_wmem_max,
92                 .maxlen         = sizeof(int),
93                 .mode           = 0644,
94                 .proc_handler   = proc_dointvec_minmax,
95                 .extra1         = &min_sndbuf,
96         },
97         {
98                 .procname       = "rmem_max",
99                 .data           = &sysctl_rmem_max,
100                 .maxlen         = sizeof(int),
101                 .mode           = 0644,
102                 .proc_handler   = proc_dointvec_minmax,
103                 .extra1         = &min_rcvbuf,
104         },
105         {
106                 .procname       = "wmem_default",
107                 .data           = &sysctl_wmem_default,
108                 .maxlen         = sizeof(int),
109                 .mode           = 0644,
110                 .proc_handler   = proc_dointvec_minmax,
111                 .extra1         = &min_sndbuf,
112         },
113         {
114                 .procname       = "rmem_default",
115                 .data           = &sysctl_rmem_default,
116                 .maxlen         = sizeof(int),
117                 .mode           = 0644,
118                 .proc_handler   = proc_dointvec_minmax,
119                 .extra1         = &min_rcvbuf,
120         },
121         {
122                 .procname       = "dev_weight",
123                 .data           = &weight_p,
124                 .maxlen         = sizeof(int),
125                 .mode           = 0644,
126                 .proc_handler   = proc_dointvec
127         },
128         {
129                 .procname       = "netdev_max_backlog",
130                 .data           = &netdev_max_backlog,
131                 .maxlen         = sizeof(int),
132                 .mode           = 0644,
133                 .proc_handler   = proc_dointvec
134         },
135 #ifdef CONFIG_BPF_JIT
136         {
137                 .procname       = "bpf_jit_enable",
138                 .data           = &bpf_jit_enable,
139                 .maxlen         = sizeof(int),
140                 .mode           = 0644,
141                 .proc_handler   = proc_dointvec
142         },
143 #endif
144         {
145                 .procname       = "netdev_tstamp_prequeue",
146                 .data           = &netdev_tstamp_prequeue,
147                 .maxlen         = sizeof(int),
148                 .mode           = 0644,
149                 .proc_handler   = proc_dointvec
150         },
151         {
152                 .procname       = "message_cost",
153                 .data           = &net_ratelimit_state.interval,
154                 .maxlen         = sizeof(int),
155                 .mode           = 0644,
156                 .proc_handler   = proc_dointvec_jiffies,
157         },
158         {
159                 .procname       = "message_burst",
160                 .data           = &net_ratelimit_state.burst,
161                 .maxlen         = sizeof(int),
162                 .mode           = 0644,
163                 .proc_handler   = proc_dointvec,
164         },
165         {
166                 .procname       = "optmem_max",
167                 .data           = &sysctl_optmem_max,
168                 .maxlen         = sizeof(int),
169                 .mode           = 0644,
170                 .proc_handler   = proc_dointvec
171         },
172 #ifdef CONFIG_RPS
173         {
174                 .procname       = "rps_sock_flow_entries",
175                 .maxlen         = sizeof(int),
176                 .mode           = 0644,
177                 .proc_handler   = rps_sock_flow_sysctl
178         },
179 #endif
180 #endif /* CONFIG_NET */
181         {
182                 .procname       = "netdev_budget",
183                 .data           = &netdev_budget,
184                 .maxlen         = sizeof(int),
185                 .mode           = 0644,
186                 .proc_handler   = proc_dointvec
187         },
188         {
189                 .procname       = "warnings",
190                 .data           = &net_msg_warn,
191                 .maxlen         = sizeof(int),
192                 .mode           = 0644,
193                 .proc_handler   = proc_dointvec
194         },
195         { }
196 };
197
198 static struct ctl_table netns_core_table[] = {
199         {
200                 .procname       = "somaxconn",
201                 .data           = &init_net.core.sysctl_somaxconn,
202                 .maxlen         = sizeof(int),
203                 .mode           = 0644,
204                 .extra1         = &zero,
205                 .extra2         = &ushort_max,
206                 .proc_handler   = proc_dointvec_minmax
207         },
208         { }
209 };
210
211 __net_initdata struct ctl_path net_core_path[] = {
212         { .procname = "net", },
213         { .procname = "core", },
214         { },
215 };
216
217 static __net_init int sysctl_core_net_init(struct net *net)
218 {
219         struct ctl_table *tbl;
220
221         net->core.sysctl_somaxconn = SOMAXCONN;
222
223         tbl = netns_core_table;
224         if (!net_eq(net, &init_net)) {
225                 tbl = kmemdup(tbl, sizeof(netns_core_table), GFP_KERNEL);
226                 if (tbl == NULL)
227                         goto err_dup;
228
229                 tbl[0].data = &net->core.sysctl_somaxconn;
230         }
231
232         net->core.sysctl_hdr = register_net_sysctl_table(net,
233                         net_core_path, tbl);
234         if (net->core.sysctl_hdr == NULL)
235                 goto err_reg;
236
237         return 0;
238
239 err_reg:
240         if (tbl != netns_core_table)
241                 kfree(tbl);
242 err_dup:
243         return -ENOMEM;
244 }
245
246 static __net_exit void sysctl_core_net_exit(struct net *net)
247 {
248         struct ctl_table *tbl;
249
250         tbl = net->core.sysctl_hdr->ctl_table_arg;
251         unregister_net_sysctl_table(net->core.sysctl_hdr);
252         BUG_ON(tbl == netns_core_table);
253         kfree(tbl);
254 }
255
256 static __net_initdata struct pernet_operations sysctl_core_ops = {
257         .init = sysctl_core_net_init,
258         .exit = sysctl_core_net_exit,
259 };
260
261 static __init int sysctl_core_init(void)
262 {
263         static struct ctl_table empty[1];
264
265         register_sysctl_paths(net_core_path, empty);
266         register_net_sysctl_rotable(net_core_path, net_core_table);
267         return register_pernet_subsys(&sysctl_core_ops);
268 }
269
270 fs_initcall(sysctl_core_init);