sysctl_net_core.c 12 KB

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