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