xref: /linux/net/core/sysctl_net_core.c (revision e5c86679d5e864947a52fb31e45a425dea3e7fa9)
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 #include <linux/kmemleak.h>
18 
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>
24 
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;
29 static int max_skb_frags = MAX_SKB_FRAGS;
30 
31 static int net_msg_warn;	/* Unused, but still a sysctl */
32 
33 #ifdef CONFIG_RPS
34 static int rps_sock_flow_sysctl(struct ctl_table *table, int write,
35 				void __user *buffer, size_t *lenp, loff_t *ppos)
36 {
37 	unsigned int orig_size, size;
38 	int ret, i;
39 	struct ctl_table tmp = {
40 		.data = &size,
41 		.maxlen = sizeof(size),
42 		.mode = table->mode
43 	};
44 	struct rps_sock_flow_table *orig_sock_table, *sock_table;
45 	static DEFINE_MUTEX(sock_flow_mutex);
46 
47 	mutex_lock(&sock_flow_mutex);
48 
49 	orig_sock_table = rcu_dereference_protected(rps_sock_flow_table,
50 					lockdep_is_held(&sock_flow_mutex));
51 	size = orig_size = orig_sock_table ? orig_sock_table->mask + 1 : 0;
52 
53 	ret = proc_dointvec(&tmp, write, buffer, lenp, ppos);
54 
55 	if (write) {
56 		if (size) {
57 			if (size > 1<<29) {
58 				/* Enforce limit to prevent overflow */
59 				mutex_unlock(&sock_flow_mutex);
60 				return -EINVAL;
61 			}
62 			size = roundup_pow_of_two(size);
63 			if (size != orig_size) {
64 				sock_table =
65 				    vmalloc(RPS_SOCK_FLOW_TABLE_SIZE(size));
66 				if (!sock_table) {
67 					mutex_unlock(&sock_flow_mutex);
68 					return -ENOMEM;
69 				}
70 				rps_cpu_mask = roundup_pow_of_two(nr_cpu_ids) - 1;
71 				sock_table->mask = size - 1;
72 			} else
73 				sock_table = orig_sock_table;
74 
75 			for (i = 0; i < size; i++)
76 				sock_table->ents[i] = RPS_NO_CPU;
77 		} else
78 			sock_table = NULL;
79 
80 		if (sock_table != orig_sock_table) {
81 			rcu_assign_pointer(rps_sock_flow_table, sock_table);
82 			if (sock_table) {
83 				static_key_slow_inc(&rps_needed);
84 				static_key_slow_inc(&rfs_needed);
85 			}
86 			if (orig_sock_table) {
87 				static_key_slow_dec(&rps_needed);
88 				static_key_slow_dec(&rfs_needed);
89 				synchronize_rcu();
90 				vfree(orig_sock_table);
91 			}
92 		}
93 	}
94 
95 	mutex_unlock(&sock_flow_mutex);
96 
97 	return ret;
98 }
99 #endif /* CONFIG_RPS */
100 
101 #ifdef CONFIG_NET_FLOW_LIMIT
102 static DEFINE_MUTEX(flow_limit_update_mutex);
103 
104 static int flow_limit_cpu_sysctl(struct ctl_table *table, int write,
105 				 void __user *buffer, size_t *lenp,
106 				 loff_t *ppos)
107 {
108 	struct sd_flow_limit *cur;
109 	struct softnet_data *sd;
110 	cpumask_var_t mask;
111 	int i, len, ret = 0;
112 
113 	if (!alloc_cpumask_var(&mask, GFP_KERNEL))
114 		return -ENOMEM;
115 
116 	if (write) {
117 		ret = cpumask_parse_user(buffer, *lenp, mask);
118 		if (ret)
119 			goto done;
120 
121 		mutex_lock(&flow_limit_update_mutex);
122 		len = sizeof(*cur) + netdev_flow_limit_table_len;
123 		for_each_possible_cpu(i) {
124 			sd = &per_cpu(softnet_data, i);
125 			cur = rcu_dereference_protected(sd->flow_limit,
126 				     lockdep_is_held(&flow_limit_update_mutex));
127 			if (cur && !cpumask_test_cpu(i, mask)) {
128 				RCU_INIT_POINTER(sd->flow_limit, NULL);
129 				synchronize_rcu();
130 				kfree(cur);
131 			} else if (!cur && cpumask_test_cpu(i, mask)) {
132 				cur = kzalloc_node(len, GFP_KERNEL,
133 						   cpu_to_node(i));
134 				if (!cur) {
135 					/* not unwinding previous changes */
136 					ret = -ENOMEM;
137 					goto write_unlock;
138 				}
139 				cur->num_buckets = netdev_flow_limit_table_len;
140 				rcu_assign_pointer(sd->flow_limit, cur);
141 			}
142 		}
143 write_unlock:
144 		mutex_unlock(&flow_limit_update_mutex);
145 	} else {
146 		char kbuf[128];
147 
148 		if (*ppos || !*lenp) {
149 			*lenp = 0;
150 			goto done;
151 		}
152 
153 		cpumask_clear(mask);
154 		rcu_read_lock();
155 		for_each_possible_cpu(i) {
156 			sd = &per_cpu(softnet_data, i);
157 			if (rcu_dereference(sd->flow_limit))
158 				cpumask_set_cpu(i, mask);
159 		}
160 		rcu_read_unlock();
161 
162 		len = min(sizeof(kbuf) - 1, *lenp);
163 		len = scnprintf(kbuf, len, "%*pb", cpumask_pr_args(mask));
164 		if (!len) {
165 			*lenp = 0;
166 			goto done;
167 		}
168 		if (len < *lenp)
169 			kbuf[len++] = '\n';
170 		if (copy_to_user(buffer, kbuf, len)) {
171 			ret = -EFAULT;
172 			goto done;
173 		}
174 		*lenp = len;
175 		*ppos += len;
176 	}
177 
178 done:
179 	free_cpumask_var(mask);
180 	return ret;
181 }
182 
183 static int flow_limit_table_len_sysctl(struct ctl_table *table, int write,
184 				       void __user *buffer, size_t *lenp,
185 				       loff_t *ppos)
186 {
187 	unsigned int old, *ptr;
188 	int ret;
189 
190 	mutex_lock(&flow_limit_update_mutex);
191 
192 	ptr = table->data;
193 	old = *ptr;
194 	ret = proc_dointvec(table, write, buffer, lenp, ppos);
195 	if (!ret && write && !is_power_of_2(*ptr)) {
196 		*ptr = old;
197 		ret = -EINVAL;
198 	}
199 
200 	mutex_unlock(&flow_limit_update_mutex);
201 	return ret;
202 }
203 #endif /* CONFIG_NET_FLOW_LIMIT */
204 
205 #ifdef CONFIG_NET_SCHED
206 static int set_default_qdisc(struct ctl_table *table, int write,
207 			     void __user *buffer, size_t *lenp, loff_t *ppos)
208 {
209 	char id[IFNAMSIZ];
210 	struct ctl_table tbl = {
211 		.data = id,
212 		.maxlen = IFNAMSIZ,
213 	};
214 	int ret;
215 
216 	qdisc_get_default(id, IFNAMSIZ);
217 
218 	ret = proc_dostring(&tbl, write, buffer, lenp, ppos);
219 	if (write && ret == 0)
220 		ret = qdisc_set_default(id);
221 	return ret;
222 }
223 #endif
224 
225 static int proc_do_dev_weight(struct ctl_table *table, int write,
226 			   void __user *buffer, size_t *lenp, loff_t *ppos)
227 {
228 	int ret;
229 
230 	ret = proc_dointvec(table, write, buffer, lenp, ppos);
231 	if (ret != 0)
232 		return ret;
233 
234 	dev_rx_weight = weight_p * dev_weight_rx_bias;
235 	dev_tx_weight = weight_p * dev_weight_tx_bias;
236 
237 	return ret;
238 }
239 
240 static int proc_do_rss_key(struct ctl_table *table, int write,
241 			   void __user *buffer, size_t *lenp, loff_t *ppos)
242 {
243 	struct ctl_table fake_table;
244 	char buf[NETDEV_RSS_KEY_LEN * 3];
245 
246 	snprintf(buf, sizeof(buf), "%*phC", NETDEV_RSS_KEY_LEN, netdev_rss_key);
247 	fake_table.data = buf;
248 	fake_table.maxlen = sizeof(buf);
249 	return proc_dostring(&fake_table, write, buffer, lenp, ppos);
250 }
251 
252 static struct ctl_table net_core_table[] = {
253 #ifdef CONFIG_NET
254 	{
255 		.procname	= "wmem_max",
256 		.data		= &sysctl_wmem_max,
257 		.maxlen		= sizeof(int),
258 		.mode		= 0644,
259 		.proc_handler	= proc_dointvec_minmax,
260 		.extra1		= &min_sndbuf,
261 	},
262 	{
263 		.procname	= "rmem_max",
264 		.data		= &sysctl_rmem_max,
265 		.maxlen		= sizeof(int),
266 		.mode		= 0644,
267 		.proc_handler	= proc_dointvec_minmax,
268 		.extra1		= &min_rcvbuf,
269 	},
270 	{
271 		.procname	= "wmem_default",
272 		.data		= &sysctl_wmem_default,
273 		.maxlen		= sizeof(int),
274 		.mode		= 0644,
275 		.proc_handler	= proc_dointvec_minmax,
276 		.extra1		= &min_sndbuf,
277 	},
278 	{
279 		.procname	= "rmem_default",
280 		.data		= &sysctl_rmem_default,
281 		.maxlen		= sizeof(int),
282 		.mode		= 0644,
283 		.proc_handler	= proc_dointvec_minmax,
284 		.extra1		= &min_rcvbuf,
285 	},
286 	{
287 		.procname	= "dev_weight",
288 		.data		= &weight_p,
289 		.maxlen		= sizeof(int),
290 		.mode		= 0644,
291 		.proc_handler	= proc_do_dev_weight,
292 	},
293 	{
294 		.procname	= "dev_weight_rx_bias",
295 		.data		= &dev_weight_rx_bias,
296 		.maxlen		= sizeof(int),
297 		.mode		= 0644,
298 		.proc_handler	= proc_do_dev_weight,
299 	},
300 	{
301 		.procname	= "dev_weight_tx_bias",
302 		.data		= &dev_weight_tx_bias,
303 		.maxlen		= sizeof(int),
304 		.mode		= 0644,
305 		.proc_handler	= proc_do_dev_weight,
306 	},
307 	{
308 		.procname	= "netdev_max_backlog",
309 		.data		= &netdev_max_backlog,
310 		.maxlen		= sizeof(int),
311 		.mode		= 0644,
312 		.proc_handler	= proc_dointvec
313 	},
314 	{
315 		.procname	= "netdev_rss_key",
316 		.data		= &netdev_rss_key,
317 		.maxlen		= sizeof(int),
318 		.mode		= 0444,
319 		.proc_handler	= proc_do_rss_key,
320 	},
321 #ifdef CONFIG_BPF_JIT
322 	{
323 		.procname	= "bpf_jit_enable",
324 		.data		= &bpf_jit_enable,
325 		.maxlen		= sizeof(int),
326 		.mode		= 0644,
327 		.proc_handler	= proc_dointvec
328 	},
329 # ifdef CONFIG_HAVE_EBPF_JIT
330 	{
331 		.procname	= "bpf_jit_harden",
332 		.data		= &bpf_jit_harden,
333 		.maxlen		= sizeof(int),
334 		.mode		= 0600,
335 		.proc_handler	= proc_dointvec,
336 	},
337 	{
338 		.procname	= "bpf_jit_kallsyms",
339 		.data		= &bpf_jit_kallsyms,
340 		.maxlen		= sizeof(int),
341 		.mode		= 0600,
342 		.proc_handler	= proc_dointvec,
343 	},
344 # endif
345 #endif
346 	{
347 		.procname	= "netdev_tstamp_prequeue",
348 		.data		= &netdev_tstamp_prequeue,
349 		.maxlen		= sizeof(int),
350 		.mode		= 0644,
351 		.proc_handler	= proc_dointvec
352 	},
353 	{
354 		.procname	= "message_cost",
355 		.data		= &net_ratelimit_state.interval,
356 		.maxlen		= sizeof(int),
357 		.mode		= 0644,
358 		.proc_handler	= proc_dointvec_jiffies,
359 	},
360 	{
361 		.procname	= "message_burst",
362 		.data		= &net_ratelimit_state.burst,
363 		.maxlen		= sizeof(int),
364 		.mode		= 0644,
365 		.proc_handler	= proc_dointvec,
366 	},
367 	{
368 		.procname	= "optmem_max",
369 		.data		= &sysctl_optmem_max,
370 		.maxlen		= sizeof(int),
371 		.mode		= 0644,
372 		.proc_handler	= proc_dointvec
373 	},
374 	{
375 		.procname	= "tstamp_allow_data",
376 		.data		= &sysctl_tstamp_allow_data,
377 		.maxlen		= sizeof(int),
378 		.mode		= 0644,
379 		.proc_handler	= proc_dointvec_minmax,
380 		.extra1		= &zero,
381 		.extra2		= &one
382 	},
383 #ifdef CONFIG_RPS
384 	{
385 		.procname	= "rps_sock_flow_entries",
386 		.maxlen		= sizeof(int),
387 		.mode		= 0644,
388 		.proc_handler	= rps_sock_flow_sysctl
389 	},
390 #endif
391 #ifdef CONFIG_NET_FLOW_LIMIT
392 	{
393 		.procname	= "flow_limit_cpu_bitmap",
394 		.mode		= 0644,
395 		.proc_handler	= flow_limit_cpu_sysctl
396 	},
397 	{
398 		.procname	= "flow_limit_table_len",
399 		.data		= &netdev_flow_limit_table_len,
400 		.maxlen		= sizeof(int),
401 		.mode		= 0644,
402 		.proc_handler	= flow_limit_table_len_sysctl
403 	},
404 #endif /* CONFIG_NET_FLOW_LIMIT */
405 #ifdef CONFIG_NET_RX_BUSY_POLL
406 	{
407 		.procname	= "busy_poll",
408 		.data		= &sysctl_net_busy_poll,
409 		.maxlen		= sizeof(unsigned int),
410 		.mode		= 0644,
411 		.proc_handler	= proc_dointvec_minmax,
412 		.extra1		= &zero,
413 	},
414 	{
415 		.procname	= "busy_read",
416 		.data		= &sysctl_net_busy_read,
417 		.maxlen		= sizeof(unsigned int),
418 		.mode		= 0644,
419 		.proc_handler	= proc_dointvec_minmax,
420 		.extra1		= &zero,
421 	},
422 #endif
423 #ifdef CONFIG_NET_SCHED
424 	{
425 		.procname	= "default_qdisc",
426 		.mode		= 0644,
427 		.maxlen		= IFNAMSIZ,
428 		.proc_handler	= set_default_qdisc
429 	},
430 #endif
431 #endif /* CONFIG_NET */
432 	{
433 		.procname	= "netdev_budget",
434 		.data		= &netdev_budget,
435 		.maxlen		= sizeof(int),
436 		.mode		= 0644,
437 		.proc_handler	= proc_dointvec
438 	},
439 	{
440 		.procname	= "warnings",
441 		.data		= &net_msg_warn,
442 		.maxlen		= sizeof(int),
443 		.mode		= 0644,
444 		.proc_handler	= proc_dointvec
445 	},
446 	{
447 		.procname	= "max_skb_frags",
448 		.data		= &sysctl_max_skb_frags,
449 		.maxlen		= sizeof(int),
450 		.mode		= 0644,
451 		.proc_handler	= proc_dointvec_minmax,
452 		.extra1		= &one,
453 		.extra2		= &max_skb_frags,
454 	},
455 	{ }
456 };
457 
458 static struct ctl_table netns_core_table[] = {
459 	{
460 		.procname	= "somaxconn",
461 		.data		= &init_net.core.sysctl_somaxconn,
462 		.maxlen		= sizeof(int),
463 		.mode		= 0644,
464 		.extra1		= &zero,
465 		.proc_handler	= proc_dointvec_minmax
466 	},
467 	{ }
468 };
469 
470 static __net_init int sysctl_core_net_init(struct net *net)
471 {
472 	struct ctl_table *tbl;
473 
474 	net->core.sysctl_somaxconn = SOMAXCONN;
475 
476 	tbl = netns_core_table;
477 	if (!net_eq(net, &init_net)) {
478 		tbl = kmemdup(tbl, sizeof(netns_core_table), GFP_KERNEL);
479 		if (tbl == NULL)
480 			goto err_dup;
481 
482 		tbl[0].data = &net->core.sysctl_somaxconn;
483 
484 		/* Don't export any sysctls to unprivileged users */
485 		if (net->user_ns != &init_user_ns) {
486 			tbl[0].procname = NULL;
487 		}
488 	}
489 
490 	net->core.sysctl_hdr = register_net_sysctl(net, "net/core", tbl);
491 	if (net->core.sysctl_hdr == NULL)
492 		goto err_reg;
493 
494 	return 0;
495 
496 err_reg:
497 	if (tbl != netns_core_table)
498 		kfree(tbl);
499 err_dup:
500 	return -ENOMEM;
501 }
502 
503 static __net_exit void sysctl_core_net_exit(struct net *net)
504 {
505 	struct ctl_table *tbl;
506 
507 	tbl = net->core.sysctl_hdr->ctl_table_arg;
508 	unregister_net_sysctl_table(net->core.sysctl_hdr);
509 	BUG_ON(tbl == netns_core_table);
510 	kfree(tbl);
511 }
512 
513 static __net_initdata struct pernet_operations sysctl_core_ops = {
514 	.init = sysctl_core_net_init,
515 	.exit = sysctl_core_net_exit,
516 };
517 
518 static __init int sysctl_core_init(void)
519 {
520 	register_net_sysctl(&init_net, "net/core", net_core_table);
521 	return register_pernet_subsys(&sysctl_core_ops);
522 }
523 
524 fs_initcall(sysctl_core_init);
525