sysctl_net_core.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. if (orig_sock_table) {
  74. static_key_slow_dec(&rps_needed);
  75. synchronize_rcu();
  76. vfree(orig_sock_table);
  77. }
  78. }
  79. }
  80. mutex_unlock(&sock_flow_mutex);
  81. return ret;
  82. }
  83. #endif /* CONFIG_RPS */
  84. #ifdef CONFIG_NET_FLOW_LIMIT
  85. static DEFINE_MUTEX(flow_limit_update_mutex);
  86. static int flow_limit_cpu_sysctl(struct ctl_table *table, int write,
  87. void __user *buffer, size_t *lenp,
  88. loff_t *ppos)
  89. {
  90. struct sd_flow_limit *cur;
  91. struct softnet_data *sd;
  92. cpumask_var_t mask;
  93. int i, len, ret = 0;
  94. if (!alloc_cpumask_var(&mask, GFP_KERNEL))
  95. return -ENOMEM;
  96. if (write) {
  97. ret = cpumask_parse_user(buffer, *lenp, mask);
  98. if (ret)
  99. goto done;
  100. mutex_lock(&flow_limit_update_mutex);
  101. len = sizeof(*cur) + netdev_flow_limit_table_len;
  102. for_each_possible_cpu(i) {
  103. sd = &per_cpu(softnet_data, i);
  104. cur = rcu_dereference_protected(sd->flow_limit,
  105. lockdep_is_held(&flow_limit_update_mutex));
  106. if (cur && !cpumask_test_cpu(i, mask)) {
  107. RCU_INIT_POINTER(sd->flow_limit, NULL);
  108. synchronize_rcu();
  109. kfree(cur);
  110. } else if (!cur && cpumask_test_cpu(i, mask)) {
  111. cur = kzalloc_node(len, GFP_KERNEL,
  112. cpu_to_node(i));
  113. if (!cur) {
  114. /* not unwinding previous changes */
  115. ret = -ENOMEM;
  116. goto write_unlock;
  117. }
  118. cur->num_buckets = netdev_flow_limit_table_len;
  119. rcu_assign_pointer(sd->flow_limit, cur);
  120. }
  121. }
  122. write_unlock:
  123. mutex_unlock(&flow_limit_update_mutex);
  124. } else {
  125. char kbuf[128];
  126. if (*ppos || !*lenp) {
  127. *lenp = 0;
  128. goto done;
  129. }
  130. cpumask_clear(mask);
  131. rcu_read_lock();
  132. for_each_possible_cpu(i) {
  133. sd = &per_cpu(softnet_data, i);
  134. if (rcu_dereference(sd->flow_limit))
  135. cpumask_set_cpu(i, mask);
  136. }
  137. rcu_read_unlock();
  138. len = min(sizeof(kbuf) - 1, *lenp);
  139. len = scnprintf(kbuf, len, "%*pb", cpumask_pr_args(mask));
  140. if (!len) {
  141. *lenp = 0;
  142. goto done;
  143. }
  144. if (len < *lenp)
  145. kbuf[len++] = '\n';
  146. if (copy_to_user(buffer, kbuf, len)) {
  147. ret = -EFAULT;
  148. goto done;
  149. }
  150. *lenp = len;
  151. *ppos += len;
  152. }
  153. done:
  154. free_cpumask_var(mask);
  155. return ret;
  156. }
  157. static int flow_limit_table_len_sysctl(struct ctl_table *table, int write,
  158. void __user *buffer, size_t *lenp,
  159. loff_t *ppos)
  160. {
  161. unsigned int old, *ptr;
  162. int ret;
  163. mutex_lock(&flow_limit_update_mutex);
  164. ptr = table->data;
  165. old = *ptr;
  166. ret = proc_dointvec(table, write, buffer, lenp, ppos);
  167. if (!ret && write && !is_power_of_2(*ptr)) {
  168. *ptr = old;
  169. ret = -EINVAL;
  170. }
  171. mutex_unlock(&flow_limit_update_mutex);
  172. return ret;
  173. }
  174. #endif /* CONFIG_NET_FLOW_LIMIT */
  175. #ifdef CONFIG_NET_SCHED
  176. static int set_default_qdisc(struct ctl_table *table, int write,
  177. void __user *buffer, size_t *lenp, loff_t *ppos)
  178. {
  179. char id[IFNAMSIZ];
  180. struct ctl_table tbl = {
  181. .data = id,
  182. .maxlen = IFNAMSIZ,
  183. };
  184. int ret;
  185. qdisc_get_default(id, IFNAMSIZ);
  186. ret = proc_dostring(&tbl, write, buffer, lenp, ppos);
  187. if (write && ret == 0)
  188. ret = qdisc_set_default(id);
  189. return ret;
  190. }
  191. #endif
  192. static int proc_do_rss_key(struct ctl_table *table, int write,
  193. void __user *buffer, size_t *lenp, loff_t *ppos)
  194. {
  195. struct ctl_table fake_table;
  196. char buf[NETDEV_RSS_KEY_LEN * 3];
  197. snprintf(buf, sizeof(buf), "%*phC", NETDEV_RSS_KEY_LEN, netdev_rss_key);
  198. fake_table.data = buf;
  199. fake_table.maxlen = sizeof(buf);
  200. return proc_dostring(&fake_table, write, buffer, lenp, ppos);
  201. }
  202. static struct ctl_table net_core_table[] = {
  203. #ifdef CONFIG_NET
  204. {
  205. .procname = "wmem_max",
  206. .data = &sysctl_wmem_max,
  207. .maxlen = sizeof(int),
  208. .mode = 0644,
  209. .proc_handler = proc_dointvec_minmax,
  210. .extra1 = &min_sndbuf,
  211. },
  212. {
  213. .procname = "rmem_max",
  214. .data = &sysctl_rmem_max,
  215. .maxlen = sizeof(int),
  216. .mode = 0644,
  217. .proc_handler = proc_dointvec_minmax,
  218. .extra1 = &min_rcvbuf,
  219. },
  220. {
  221. .procname = "wmem_default",
  222. .data = &sysctl_wmem_default,
  223. .maxlen = sizeof(int),
  224. .mode = 0644,
  225. .proc_handler = proc_dointvec_minmax,
  226. .extra1 = &min_sndbuf,
  227. },
  228. {
  229. .procname = "rmem_default",
  230. .data = &sysctl_rmem_default,
  231. .maxlen = sizeof(int),
  232. .mode = 0644,
  233. .proc_handler = proc_dointvec_minmax,
  234. .extra1 = &min_rcvbuf,
  235. },
  236. {
  237. .procname = "dev_weight",
  238. .data = &weight_p,
  239. .maxlen = sizeof(int),
  240. .mode = 0644,
  241. .proc_handler = proc_dointvec
  242. },
  243. {
  244. .procname = "netdev_max_backlog",
  245. .data = &netdev_max_backlog,
  246. .maxlen = sizeof(int),
  247. .mode = 0644,
  248. .proc_handler = proc_dointvec
  249. },
  250. {
  251. .procname = "netdev_rss_key",
  252. .data = &netdev_rss_key,
  253. .maxlen = sizeof(int),
  254. .mode = 0444,
  255. .proc_handler = proc_do_rss_key,
  256. },
  257. #ifdef CONFIG_BPF_JIT
  258. {
  259. .procname = "bpf_jit_enable",
  260. .data = &bpf_jit_enable,
  261. .maxlen = sizeof(int),
  262. .mode = 0644,
  263. #ifndef CONFIG_BPF_JIT_ALWAYS_ON
  264. .proc_handler = proc_dointvec
  265. #else
  266. .proc_handler = proc_dointvec_minmax,
  267. .extra1 = &one,
  268. .extra2 = &one,
  269. #endif
  270. },
  271. # ifdef CONFIG_HAVE_EBPF_JIT
  272. {
  273. .procname = "bpf_jit_harden",
  274. .data = &bpf_jit_harden,
  275. .maxlen = sizeof(int),
  276. .mode = 0600,
  277. .proc_handler = proc_dointvec,
  278. },
  279. # endif
  280. #endif
  281. {
  282. .procname = "netdev_tstamp_prequeue",
  283. .data = &netdev_tstamp_prequeue,
  284. .maxlen = sizeof(int),
  285. .mode = 0644,
  286. .proc_handler = proc_dointvec
  287. },
  288. {
  289. .procname = "message_cost",
  290. .data = &net_ratelimit_state.interval,
  291. .maxlen = sizeof(int),
  292. .mode = 0644,
  293. .proc_handler = proc_dointvec_jiffies,
  294. },
  295. {
  296. .procname = "message_burst",
  297. .data = &net_ratelimit_state.burst,
  298. .maxlen = sizeof(int),
  299. .mode = 0644,
  300. .proc_handler = proc_dointvec,
  301. },
  302. {
  303. .procname = "optmem_max",
  304. .data = &sysctl_optmem_max,
  305. .maxlen = sizeof(int),
  306. .mode = 0644,
  307. .proc_handler = proc_dointvec
  308. },
  309. {
  310. .procname = "tstamp_allow_data",
  311. .data = &sysctl_tstamp_allow_data,
  312. .maxlen = sizeof(int),
  313. .mode = 0644,
  314. .proc_handler = proc_dointvec_minmax,
  315. .extra1 = &zero,
  316. .extra2 = &one
  317. },
  318. #ifdef CONFIG_RPS
  319. {
  320. .procname = "rps_sock_flow_entries",
  321. .maxlen = sizeof(int),
  322. .mode = 0644,
  323. .proc_handler = rps_sock_flow_sysctl
  324. },
  325. #endif
  326. #ifdef CONFIG_NET_FLOW_LIMIT
  327. {
  328. .procname = "flow_limit_cpu_bitmap",
  329. .mode = 0644,
  330. .proc_handler = flow_limit_cpu_sysctl
  331. },
  332. {
  333. .procname = "flow_limit_table_len",
  334. .data = &netdev_flow_limit_table_len,
  335. .maxlen = sizeof(int),
  336. .mode = 0644,
  337. .proc_handler = flow_limit_table_len_sysctl
  338. },
  339. #endif /* CONFIG_NET_FLOW_LIMIT */
  340. #ifdef CONFIG_NET_RX_BUSY_POLL
  341. {
  342. .procname = "busy_poll",
  343. .data = &sysctl_net_busy_poll,
  344. .maxlen = sizeof(unsigned int),
  345. .mode = 0644,
  346. .proc_handler = proc_dointvec_minmax,
  347. .extra1 = &zero,
  348. },
  349. {
  350. .procname = "busy_read",
  351. .data = &sysctl_net_busy_read,
  352. .maxlen = sizeof(unsigned int),
  353. .mode = 0644,
  354. .proc_handler = proc_dointvec_minmax,
  355. .extra1 = &zero,
  356. },
  357. #endif
  358. #ifdef CONFIG_NET_SCHED
  359. {
  360. .procname = "default_qdisc",
  361. .mode = 0644,
  362. .maxlen = IFNAMSIZ,
  363. .proc_handler = set_default_qdisc
  364. },
  365. #endif
  366. #endif /* CONFIG_NET */
  367. {
  368. .procname = "netdev_budget",
  369. .data = &netdev_budget,
  370. .maxlen = sizeof(int),
  371. .mode = 0644,
  372. .proc_handler = proc_dointvec
  373. },
  374. {
  375. .procname = "warnings",
  376. .data = &net_msg_warn,
  377. .maxlen = sizeof(int),
  378. .mode = 0644,
  379. .proc_handler = proc_dointvec
  380. },
  381. {
  382. .procname = "max_skb_frags",
  383. .data = &sysctl_max_skb_frags,
  384. .maxlen = sizeof(int),
  385. .mode = 0644,
  386. .proc_handler = proc_dointvec_minmax,
  387. .extra1 = &one,
  388. .extra2 = &max_skb_frags,
  389. },
  390. { }
  391. };
  392. static struct ctl_table netns_core_table[] = {
  393. {
  394. .procname = "somaxconn",
  395. .data = &init_net.core.sysctl_somaxconn,
  396. .maxlen = sizeof(int),
  397. .mode = 0644,
  398. .extra1 = &zero,
  399. .proc_handler = proc_dointvec_minmax
  400. },
  401. { }
  402. };
  403. static __net_init int sysctl_core_net_init(struct net *net)
  404. {
  405. struct ctl_table *tbl;
  406. tbl = netns_core_table;
  407. if (!net_eq(net, &init_net)) {
  408. tbl = kmemdup(tbl, sizeof(netns_core_table), GFP_KERNEL);
  409. if (tbl == NULL)
  410. goto err_dup;
  411. tbl[0].data = &net->core.sysctl_somaxconn;
  412. /* Don't export any sysctls to unprivileged users */
  413. if (net->user_ns != &init_user_ns) {
  414. tbl[0].procname = NULL;
  415. }
  416. }
  417. net->core.sysctl_hdr = register_net_sysctl(net, "net/core", tbl);
  418. if (net->core.sysctl_hdr == NULL)
  419. goto err_reg;
  420. return 0;
  421. err_reg:
  422. if (tbl != netns_core_table)
  423. kfree(tbl);
  424. err_dup:
  425. return -ENOMEM;
  426. }
  427. static __net_exit void sysctl_core_net_exit(struct net *net)
  428. {
  429. struct ctl_table *tbl;
  430. tbl = net->core.sysctl_hdr->ctl_table_arg;
  431. unregister_net_sysctl_table(net->core.sysctl_hdr);
  432. BUG_ON(tbl == netns_core_table);
  433. kfree(tbl);
  434. }
  435. static __net_initdata struct pernet_operations sysctl_core_ops = {
  436. .init = sysctl_core_net_init,
  437. .exit = sysctl_core_net_exit,
  438. };
  439. static __init int sysctl_core_init(void)
  440. {
  441. register_net_sysctl(&init_net, "net/core", net_core_table);
  442. return register_pernet_subsys(&sysctl_core_ops);
  443. }
  444. fs_initcall(sysctl_core_init);