sysctl_net_core.c 10 KB

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