s390: add 'install' target to 'make help'
[linux/fpc-iii.git] / net / core / sysctl_net_core.c
blob95b6139d710c46825d1e43f825188d81fcb70f60
1 /* -*- linux-c -*-
2 * sysctl_net_core.c: sysctl interface to net core subsystem.
4 * Begun April 1, 1996, Mike Shaver.
5 * Added /proc/sys/net/core directory entry (empty =) ). [MS]
6 */
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 #include <linux/kmemleak.h>
19 #include <net/ip.h>
20 #include <net/sock.h>
21 #include <net/net_ratelimit.h>
22 #include <net/busy_poll.h>
23 #include <net/pkt_sched.h>
25 static int zero = 0;
26 static int one = 1;
27 static int min_sndbuf = SOCK_MIN_SNDBUF;
28 static int min_rcvbuf = SOCK_MIN_RCVBUF;
30 static int net_msg_warn; /* Unused, but still a sysctl */
32 #ifdef CONFIG_RPS
33 static int rps_sock_flow_sysctl(struct ctl_table *table, int write,
34 void __user *buffer, size_t *lenp, loff_t *ppos)
36 unsigned int orig_size, size;
37 int ret, i;
38 struct ctl_table tmp = {
39 .data = &size,
40 .maxlen = sizeof(size),
41 .mode = table->mode
43 struct rps_sock_flow_table *orig_sock_table, *sock_table;
44 static DEFINE_MUTEX(sock_flow_mutex);
46 mutex_lock(&sock_flow_mutex);
48 orig_sock_table = rcu_dereference_protected(rps_sock_flow_table,
49 lockdep_is_held(&sock_flow_mutex));
50 size = orig_size = orig_sock_table ? orig_sock_table->mask + 1 : 0;
52 ret = proc_dointvec(&tmp, write, buffer, lenp, ppos);
54 if (write) {
55 if (size) {
56 if (size > 1<<29) {
57 /* Enforce limit to prevent overflow */
58 mutex_unlock(&sock_flow_mutex);
59 return -EINVAL;
61 size = roundup_pow_of_two(size);
62 if (size != orig_size) {
63 sock_table =
64 vmalloc(RPS_SOCK_FLOW_TABLE_SIZE(size));
65 if (!sock_table) {
66 mutex_unlock(&sock_flow_mutex);
67 return -ENOMEM;
69 rps_cpu_mask = roundup_pow_of_two(nr_cpu_ids) - 1;
70 sock_table->mask = size - 1;
71 } else
72 sock_table = orig_sock_table;
74 for (i = 0; i < size; i++)
75 sock_table->ents[i] = RPS_NO_CPU;
76 } else
77 sock_table = NULL;
79 if (sock_table != orig_sock_table) {
80 rcu_assign_pointer(rps_sock_flow_table, sock_table);
81 if (sock_table)
82 static_key_slow_inc(&rps_needed);
83 if (orig_sock_table) {
84 static_key_slow_dec(&rps_needed);
85 synchronize_rcu();
86 vfree(orig_sock_table);
91 mutex_unlock(&sock_flow_mutex);
93 return ret;
95 #endif /* CONFIG_RPS */
97 #ifdef CONFIG_NET_FLOW_LIMIT
98 static DEFINE_MUTEX(flow_limit_update_mutex);
100 static int flow_limit_cpu_sysctl(struct ctl_table *table, int write,
101 void __user *buffer, size_t *lenp,
102 loff_t *ppos)
104 struct sd_flow_limit *cur;
105 struct softnet_data *sd;
106 cpumask_var_t mask;
107 int i, len, ret = 0;
109 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
110 return -ENOMEM;
112 if (write) {
113 ret = cpumask_parse_user(buffer, *lenp, mask);
114 if (ret)
115 goto done;
117 mutex_lock(&flow_limit_update_mutex);
118 len = sizeof(*cur) + netdev_flow_limit_table_len;
119 for_each_possible_cpu(i) {
120 sd = &per_cpu(softnet_data, i);
121 cur = rcu_dereference_protected(sd->flow_limit,
122 lockdep_is_held(&flow_limit_update_mutex));
123 if (cur && !cpumask_test_cpu(i, mask)) {
124 RCU_INIT_POINTER(sd->flow_limit, NULL);
125 synchronize_rcu();
126 kfree(cur);
127 } else if (!cur && cpumask_test_cpu(i, mask)) {
128 cur = kzalloc_node(len, GFP_KERNEL,
129 cpu_to_node(i));
130 if (!cur) {
131 /* not unwinding previous changes */
132 ret = -ENOMEM;
133 goto write_unlock;
135 cur->num_buckets = netdev_flow_limit_table_len;
136 rcu_assign_pointer(sd->flow_limit, cur);
139 write_unlock:
140 mutex_unlock(&flow_limit_update_mutex);
141 } else {
142 char kbuf[128];
144 if (*ppos || !*lenp) {
145 *lenp = 0;
146 goto done;
149 cpumask_clear(mask);
150 rcu_read_lock();
151 for_each_possible_cpu(i) {
152 sd = &per_cpu(softnet_data, i);
153 if (rcu_dereference(sd->flow_limit))
154 cpumask_set_cpu(i, mask);
156 rcu_read_unlock();
158 len = min(sizeof(kbuf) - 1, *lenp);
159 len = scnprintf(kbuf, len, "%*pb", cpumask_pr_args(mask));
160 if (!len) {
161 *lenp = 0;
162 goto done;
164 if (len < *lenp)
165 kbuf[len++] = '\n';
166 if (copy_to_user(buffer, kbuf, len)) {
167 ret = -EFAULT;
168 goto done;
170 *lenp = len;
171 *ppos += len;
174 done:
175 free_cpumask_var(mask);
176 return ret;
179 static int flow_limit_table_len_sysctl(struct ctl_table *table, int write,
180 void __user *buffer, size_t *lenp,
181 loff_t *ppos)
183 unsigned int old, *ptr;
184 int ret;
186 mutex_lock(&flow_limit_update_mutex);
188 ptr = table->data;
189 old = *ptr;
190 ret = proc_dointvec(table, write, buffer, lenp, ppos);
191 if (!ret && write && !is_power_of_2(*ptr)) {
192 *ptr = old;
193 ret = -EINVAL;
196 mutex_unlock(&flow_limit_update_mutex);
197 return ret;
199 #endif /* CONFIG_NET_FLOW_LIMIT */
201 #ifdef CONFIG_NET_SCHED
202 static int set_default_qdisc(struct ctl_table *table, int write,
203 void __user *buffer, size_t *lenp, loff_t *ppos)
205 char id[IFNAMSIZ];
206 struct ctl_table tbl = {
207 .data = id,
208 .maxlen = IFNAMSIZ,
210 int ret;
212 qdisc_get_default(id, IFNAMSIZ);
214 ret = proc_dostring(&tbl, write, buffer, lenp, ppos);
215 if (write && ret == 0)
216 ret = qdisc_set_default(id);
217 return ret;
219 #endif
221 static int proc_do_rss_key(struct ctl_table *table, int write,
222 void __user *buffer, size_t *lenp, loff_t *ppos)
224 struct ctl_table fake_table;
225 char buf[NETDEV_RSS_KEY_LEN * 3];
227 snprintf(buf, sizeof(buf), "%*phC", NETDEV_RSS_KEY_LEN, netdev_rss_key);
228 fake_table.data = buf;
229 fake_table.maxlen = sizeof(buf);
230 return proc_dostring(&fake_table, write, buffer, lenp, ppos);
233 static struct ctl_table net_core_table[] = {
234 #ifdef CONFIG_NET
236 .procname = "wmem_max",
237 .data = &sysctl_wmem_max,
238 .maxlen = sizeof(int),
239 .mode = 0644,
240 .proc_handler = proc_dointvec_minmax,
241 .extra1 = &min_sndbuf,
244 .procname = "rmem_max",
245 .data = &sysctl_rmem_max,
246 .maxlen = sizeof(int),
247 .mode = 0644,
248 .proc_handler = proc_dointvec_minmax,
249 .extra1 = &min_rcvbuf,
252 .procname = "wmem_default",
253 .data = &sysctl_wmem_default,
254 .maxlen = sizeof(int),
255 .mode = 0644,
256 .proc_handler = proc_dointvec_minmax,
257 .extra1 = &min_sndbuf,
260 .procname = "rmem_default",
261 .data = &sysctl_rmem_default,
262 .maxlen = sizeof(int),
263 .mode = 0644,
264 .proc_handler = proc_dointvec_minmax,
265 .extra1 = &min_rcvbuf,
268 .procname = "dev_weight",
269 .data = &weight_p,
270 .maxlen = sizeof(int),
271 .mode = 0644,
272 .proc_handler = proc_dointvec
275 .procname = "netdev_max_backlog",
276 .data = &netdev_max_backlog,
277 .maxlen = sizeof(int),
278 .mode = 0644,
279 .proc_handler = proc_dointvec
282 .procname = "netdev_rss_key",
283 .data = &netdev_rss_key,
284 .maxlen = sizeof(int),
285 .mode = 0444,
286 .proc_handler = proc_do_rss_key,
288 #ifdef CONFIG_BPF_JIT
290 .procname = "bpf_jit_enable",
291 .data = &bpf_jit_enable,
292 .maxlen = sizeof(int),
293 .mode = 0644,
294 .proc_handler = proc_dointvec
296 #endif
298 .procname = "netdev_tstamp_prequeue",
299 .data = &netdev_tstamp_prequeue,
300 .maxlen = sizeof(int),
301 .mode = 0644,
302 .proc_handler = proc_dointvec
305 .procname = "message_cost",
306 .data = &net_ratelimit_state.interval,
307 .maxlen = sizeof(int),
308 .mode = 0644,
309 .proc_handler = proc_dointvec_jiffies,
312 .procname = "message_burst",
313 .data = &net_ratelimit_state.burst,
314 .maxlen = sizeof(int),
315 .mode = 0644,
316 .proc_handler = proc_dointvec,
319 .procname = "optmem_max",
320 .data = &sysctl_optmem_max,
321 .maxlen = sizeof(int),
322 .mode = 0644,
323 .proc_handler = proc_dointvec
326 .procname = "tstamp_allow_data",
327 .data = &sysctl_tstamp_allow_data,
328 .maxlen = sizeof(int),
329 .mode = 0644,
330 .proc_handler = proc_dointvec_minmax,
331 .extra1 = &zero,
332 .extra2 = &one
334 #ifdef CONFIG_RPS
336 .procname = "rps_sock_flow_entries",
337 .maxlen = sizeof(int),
338 .mode = 0644,
339 .proc_handler = rps_sock_flow_sysctl
341 #endif
342 #ifdef CONFIG_NET_FLOW_LIMIT
344 .procname = "flow_limit_cpu_bitmap",
345 .mode = 0644,
346 .proc_handler = flow_limit_cpu_sysctl
349 .procname = "flow_limit_table_len",
350 .data = &netdev_flow_limit_table_len,
351 .maxlen = sizeof(int),
352 .mode = 0644,
353 .proc_handler = flow_limit_table_len_sysctl
355 #endif /* CONFIG_NET_FLOW_LIMIT */
356 #ifdef CONFIG_NET_RX_BUSY_POLL
358 .procname = "busy_poll",
359 .data = &sysctl_net_busy_poll,
360 .maxlen = sizeof(unsigned int),
361 .mode = 0644,
362 .proc_handler = proc_dointvec
365 .procname = "busy_read",
366 .data = &sysctl_net_busy_read,
367 .maxlen = sizeof(unsigned int),
368 .mode = 0644,
369 .proc_handler = proc_dointvec
371 #endif
372 #ifdef CONFIG_NET_SCHED
374 .procname = "default_qdisc",
375 .mode = 0644,
376 .maxlen = IFNAMSIZ,
377 .proc_handler = set_default_qdisc
379 #endif
380 #endif /* CONFIG_NET */
382 .procname = "netdev_budget",
383 .data = &netdev_budget,
384 .maxlen = sizeof(int),
385 .mode = 0644,
386 .proc_handler = proc_dointvec
389 .procname = "warnings",
390 .data = &net_msg_warn,
391 .maxlen = sizeof(int),
392 .mode = 0644,
393 .proc_handler = proc_dointvec
398 static struct ctl_table netns_core_table[] = {
400 .procname = "somaxconn",
401 .data = &init_net.core.sysctl_somaxconn,
402 .maxlen = sizeof(int),
403 .mode = 0644,
404 .extra1 = &zero,
405 .proc_handler = proc_dointvec_minmax
410 static __net_init int sysctl_core_net_init(struct net *net)
412 struct ctl_table *tbl;
414 net->core.sysctl_somaxconn = SOMAXCONN;
416 tbl = netns_core_table;
417 if (!net_eq(net, &init_net)) {
418 tbl = kmemdup(tbl, sizeof(netns_core_table), GFP_KERNEL);
419 if (tbl == NULL)
420 goto err_dup;
422 tbl[0].data = &net->core.sysctl_somaxconn;
424 /* Don't export any sysctls to unprivileged users */
425 if (net->user_ns != &init_user_ns) {
426 tbl[0].procname = NULL;
430 net->core.sysctl_hdr = register_net_sysctl(net, "net/core", tbl);
431 if (net->core.sysctl_hdr == NULL)
432 goto err_reg;
434 return 0;
436 err_reg:
437 if (tbl != netns_core_table)
438 kfree(tbl);
439 err_dup:
440 return -ENOMEM;
443 static __net_exit void sysctl_core_net_exit(struct net *net)
445 struct ctl_table *tbl;
447 tbl = net->core.sysctl_hdr->ctl_table_arg;
448 unregister_net_sysctl_table(net->core.sysctl_hdr);
449 BUG_ON(tbl == netns_core_table);
450 kfree(tbl);
453 static __net_initdata struct pernet_operations sysctl_core_ops = {
454 .init = sysctl_core_net_init,
455 .exit = sysctl_core_net_exit,
458 static __init int sysctl_core_init(void)
460 register_net_sysctl(&init_net, "net/core", net_core_table);
461 return register_pernet_subsys(&sysctl_core_ops);
464 fs_initcall(sysctl_core_init);