sysctl_net_core.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. #endif
  272. {
  273. .procname = "netdev_tstamp_prequeue",
  274. .data = &netdev_tstamp_prequeue,
  275. .maxlen = sizeof(int),
  276. .mode = 0644,
  277. .proc_handler = proc_dointvec
  278. },
  279. {
  280. .procname = "message_cost",
  281. .data = &net_ratelimit_state.interval,
  282. .maxlen = sizeof(int),
  283. .mode = 0644,
  284. .proc_handler = proc_dointvec_jiffies,
  285. },
  286. {
  287. .procname = "message_burst",
  288. .data = &net_ratelimit_state.burst,
  289. .maxlen = sizeof(int),
  290. .mode = 0644,
  291. .proc_handler = proc_dointvec,
  292. },
  293. {
  294. .procname = "optmem_max",
  295. .data = &sysctl_optmem_max,
  296. .maxlen = sizeof(int),
  297. .mode = 0644,
  298. .proc_handler = proc_dointvec
  299. },
  300. {
  301. .procname = "tstamp_allow_data",
  302. .data = &sysctl_tstamp_allow_data,
  303. .maxlen = sizeof(int),
  304. .mode = 0644,
  305. .proc_handler = proc_dointvec_minmax,
  306. .extra1 = &zero,
  307. .extra2 = &one
  308. },
  309. #ifdef CONFIG_RPS
  310. {
  311. .procname = "rps_sock_flow_entries",
  312. .maxlen = sizeof(int),
  313. .mode = 0644,
  314. .proc_handler = rps_sock_flow_sysctl
  315. },
  316. #endif
  317. #ifdef CONFIG_NET_FLOW_LIMIT
  318. {
  319. .procname = "flow_limit_cpu_bitmap",
  320. .mode = 0644,
  321. .proc_handler = flow_limit_cpu_sysctl
  322. },
  323. {
  324. .procname = "flow_limit_table_len",
  325. .data = &netdev_flow_limit_table_len,
  326. .maxlen = sizeof(int),
  327. .mode = 0644,
  328. .proc_handler = flow_limit_table_len_sysctl
  329. },
  330. #endif /* CONFIG_NET_FLOW_LIMIT */
  331. #ifdef CONFIG_NET_RX_BUSY_POLL
  332. {
  333. .procname = "busy_poll",
  334. .data = &sysctl_net_busy_poll,
  335. .maxlen = sizeof(unsigned int),
  336. .mode = 0644,
  337. .proc_handler = proc_dointvec_minmax,
  338. .extra1 = &zero,
  339. },
  340. {
  341. .procname = "busy_read",
  342. .data = &sysctl_net_busy_read,
  343. .maxlen = sizeof(unsigned int),
  344. .mode = 0644,
  345. .proc_handler = proc_dointvec_minmax,
  346. .extra1 = &zero,
  347. },
  348. #endif
  349. #ifdef CONFIG_NET_SCHED
  350. {
  351. .procname = "default_qdisc",
  352. .mode = 0644,
  353. .maxlen = IFNAMSIZ,
  354. .proc_handler = set_default_qdisc
  355. },
  356. #endif
  357. #endif /* CONFIG_NET */
  358. {
  359. .procname = "netdev_budget",
  360. .data = &netdev_budget,
  361. .maxlen = sizeof(int),
  362. .mode = 0644,
  363. .proc_handler = proc_dointvec
  364. },
  365. {
  366. .procname = "warnings",
  367. .data = &net_msg_warn,
  368. .maxlen = sizeof(int),
  369. .mode = 0644,
  370. .proc_handler = proc_dointvec
  371. },
  372. {
  373. .procname = "max_skb_frags",
  374. .data = &sysctl_max_skb_frags,
  375. .maxlen = sizeof(int),
  376. .mode = 0644,
  377. .proc_handler = proc_dointvec_minmax,
  378. .extra1 = &one,
  379. .extra2 = &max_skb_frags,
  380. },
  381. { }
  382. };
  383. static struct ctl_table netns_core_table[] = {
  384. {
  385. .procname = "somaxconn",
  386. .data = &init_net.core.sysctl_somaxconn,
  387. .maxlen = sizeof(int),
  388. .mode = 0644,
  389. .extra1 = &zero,
  390. .proc_handler = proc_dointvec_minmax
  391. },
  392. { }
  393. };
  394. static __net_init int sysctl_core_net_init(struct net *net)
  395. {
  396. struct ctl_table *tbl;
  397. tbl = netns_core_table;
  398. if (!net_eq(net, &init_net)) {
  399. tbl = kmemdup(tbl, sizeof(netns_core_table), GFP_KERNEL);
  400. if (tbl == NULL)
  401. goto err_dup;
  402. tbl[0].data = &net->core.sysctl_somaxconn;
  403. /* Don't export any sysctls to unprivileged users */
  404. if (net->user_ns != &init_user_ns) {
  405. tbl[0].procname = NULL;
  406. }
  407. }
  408. net->core.sysctl_hdr = register_net_sysctl(net, "net/core", tbl);
  409. if (net->core.sysctl_hdr == NULL)
  410. goto err_reg;
  411. return 0;
  412. err_reg:
  413. if (tbl != netns_core_table)
  414. kfree(tbl);
  415. err_dup:
  416. return -ENOMEM;
  417. }
  418. static __net_exit void sysctl_core_net_exit(struct net *net)
  419. {
  420. struct ctl_table *tbl;
  421. tbl = net->core.sysctl_hdr->ctl_table_arg;
  422. unregister_net_sysctl_table(net->core.sysctl_hdr);
  423. BUG_ON(tbl == netns_core_table);
  424. kfree(tbl);
  425. }
  426. static __net_initdata struct pernet_operations sysctl_core_ops = {
  427. .init = sysctl_core_net_init,
  428. .exit = sysctl_core_net_exit,
  429. };
  430. static __init int sysctl_core_init(void)
  431. {
  432. register_net_sysctl(&init_net, "net/core", net_core_table);
  433. return register_pernet_subsys(&sysctl_core_ops);
  434. }
  435. fs_initcall(sysctl_core_init);