sysctl_net_core.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. static int one = 1;
  21. #ifdef CONFIG_RPS
  22. static int rps_sock_flow_sysctl(ctl_table *table, int write,
  23. void __user *buffer, size_t *lenp, loff_t *ppos)
  24. {
  25. unsigned int orig_size, size;
  26. int ret, i;
  27. ctl_table tmp = {
  28. .data = &size,
  29. .maxlen = sizeof(size),
  30. .mode = table->mode
  31. };
  32. struct rps_sock_flow_table *orig_sock_table, *sock_table;
  33. static DEFINE_MUTEX(sock_flow_mutex);
  34. mutex_lock(&sock_flow_mutex);
  35. orig_sock_table = rcu_dereference_protected(rps_sock_flow_table,
  36. lockdep_is_held(&sock_flow_mutex));
  37. size = orig_size = orig_sock_table ? orig_sock_table->mask + 1 : 0;
  38. ret = proc_dointvec(&tmp, write, buffer, lenp, ppos);
  39. if (write) {
  40. if (size) {
  41. if (size > 1<<30) {
  42. /* Enforce limit to prevent overflow */
  43. mutex_unlock(&sock_flow_mutex);
  44. return -EINVAL;
  45. }
  46. size = roundup_pow_of_two(size);
  47. if (size != orig_size) {
  48. sock_table =
  49. vmalloc(RPS_SOCK_FLOW_TABLE_SIZE(size));
  50. if (!sock_table) {
  51. mutex_unlock(&sock_flow_mutex);
  52. return -ENOMEM;
  53. }
  54. sock_table->mask = size - 1;
  55. } else
  56. sock_table = orig_sock_table;
  57. for (i = 0; i < size; i++)
  58. sock_table->ents[i] = RPS_NO_CPU;
  59. } else
  60. sock_table = NULL;
  61. if (sock_table != orig_sock_table) {
  62. rcu_assign_pointer(rps_sock_flow_table, sock_table);
  63. if (sock_table)
  64. static_key_slow_inc(&rps_needed);
  65. if (orig_sock_table) {
  66. static_key_slow_dec(&rps_needed);
  67. synchronize_rcu();
  68. vfree(orig_sock_table);
  69. }
  70. }
  71. }
  72. mutex_unlock(&sock_flow_mutex);
  73. return ret;
  74. }
  75. #endif /* CONFIG_RPS */
  76. #ifdef CONFIG_NET_FLOW_LIMIT
  77. static DEFINE_MUTEX(flow_limit_update_mutex);
  78. static int flow_limit_cpu_sysctl(ctl_table *table, int write,
  79. void __user *buffer, size_t *lenp,
  80. loff_t *ppos)
  81. {
  82. struct sd_flow_limit *cur;
  83. struct softnet_data *sd;
  84. cpumask_var_t mask;
  85. int i, len, ret = 0;
  86. if (!alloc_cpumask_var(&mask, GFP_KERNEL))
  87. return -ENOMEM;
  88. if (write) {
  89. ret = cpumask_parse_user(buffer, *lenp, mask);
  90. if (ret)
  91. goto done;
  92. mutex_lock(&flow_limit_update_mutex);
  93. len = sizeof(*cur) + netdev_flow_limit_table_len;
  94. for_each_possible_cpu(i) {
  95. sd = &per_cpu(softnet_data, i);
  96. cur = rcu_dereference_protected(sd->flow_limit,
  97. lockdep_is_held(&flow_limit_update_mutex));
  98. if (cur && !cpumask_test_cpu(i, mask)) {
  99. RCU_INIT_POINTER(sd->flow_limit, NULL);
  100. synchronize_rcu();
  101. kfree(cur);
  102. } else if (!cur && cpumask_test_cpu(i, mask)) {
  103. cur = kzalloc(len, GFP_KERNEL);
  104. if (!cur) {
  105. /* not unwinding previous changes */
  106. ret = -ENOMEM;
  107. goto write_unlock;
  108. }
  109. cur->num_buckets = netdev_flow_limit_table_len;
  110. rcu_assign_pointer(sd->flow_limit, cur);
  111. }
  112. }
  113. write_unlock:
  114. mutex_unlock(&flow_limit_update_mutex);
  115. } else {
  116. if (*ppos || !*lenp) {
  117. *lenp = 0;
  118. goto done;
  119. }
  120. cpumask_clear(mask);
  121. rcu_read_lock();
  122. for_each_possible_cpu(i) {
  123. sd = &per_cpu(softnet_data, i);
  124. if (rcu_dereference(sd->flow_limit))
  125. cpumask_set_cpu(i, mask);
  126. }
  127. rcu_read_unlock();
  128. len = cpumask_scnprintf(buffer, *lenp, mask);
  129. *lenp = len + 1;
  130. *ppos += len + 1;
  131. }
  132. done:
  133. free_cpumask_var(mask);
  134. return ret;
  135. }
  136. static int flow_limit_table_len_sysctl(ctl_table *table, int write,
  137. void __user *buffer, size_t *lenp,
  138. loff_t *ppos)
  139. {
  140. unsigned int old, *ptr;
  141. int ret;
  142. mutex_lock(&flow_limit_update_mutex);
  143. ptr = table->data;
  144. old = *ptr;
  145. ret = proc_dointvec(table, write, buffer, lenp, ppos);
  146. if (!ret && write && !is_power_of_2(*ptr)) {
  147. *ptr = old;
  148. ret = -EINVAL;
  149. }
  150. mutex_unlock(&flow_limit_update_mutex);
  151. return ret;
  152. }
  153. #endif /* CONFIG_NET_FLOW_LIMIT */
  154. static struct ctl_table net_core_table[] = {
  155. #ifdef CONFIG_NET
  156. {
  157. .procname = "wmem_max",
  158. .data = &sysctl_wmem_max,
  159. .maxlen = sizeof(int),
  160. .mode = 0644,
  161. .proc_handler = proc_dointvec_minmax,
  162. .extra1 = &one,
  163. },
  164. {
  165. .procname = "rmem_max",
  166. .data = &sysctl_rmem_max,
  167. .maxlen = sizeof(int),
  168. .mode = 0644,
  169. .proc_handler = proc_dointvec_minmax,
  170. .extra1 = &one,
  171. },
  172. {
  173. .procname = "wmem_default",
  174. .data = &sysctl_wmem_default,
  175. .maxlen = sizeof(int),
  176. .mode = 0644,
  177. .proc_handler = proc_dointvec_minmax,
  178. .extra1 = &one,
  179. },
  180. {
  181. .procname = "rmem_default",
  182. .data = &sysctl_rmem_default,
  183. .maxlen = sizeof(int),
  184. .mode = 0644,
  185. .proc_handler = proc_dointvec_minmax,
  186. .extra1 = &one,
  187. },
  188. {
  189. .procname = "dev_weight",
  190. .data = &weight_p,
  191. .maxlen = sizeof(int),
  192. .mode = 0644,
  193. .proc_handler = proc_dointvec
  194. },
  195. {
  196. .procname = "netdev_max_backlog",
  197. .data = &netdev_max_backlog,
  198. .maxlen = sizeof(int),
  199. .mode = 0644,
  200. .proc_handler = proc_dointvec
  201. },
  202. #ifdef CONFIG_BPF_JIT
  203. {
  204. .procname = "bpf_jit_enable",
  205. .data = &bpf_jit_enable,
  206. .maxlen = sizeof(int),
  207. .mode = 0644,
  208. .proc_handler = proc_dointvec
  209. },
  210. #endif
  211. {
  212. .procname = "netdev_tstamp_prequeue",
  213. .data = &netdev_tstamp_prequeue,
  214. .maxlen = sizeof(int),
  215. .mode = 0644,
  216. .proc_handler = proc_dointvec
  217. },
  218. {
  219. .procname = "message_cost",
  220. .data = &net_ratelimit_state.interval,
  221. .maxlen = sizeof(int),
  222. .mode = 0644,
  223. .proc_handler = proc_dointvec_jiffies,
  224. },
  225. {
  226. .procname = "message_burst",
  227. .data = &net_ratelimit_state.burst,
  228. .maxlen = sizeof(int),
  229. .mode = 0644,
  230. .proc_handler = proc_dointvec,
  231. },
  232. {
  233. .procname = "optmem_max",
  234. .data = &sysctl_optmem_max,
  235. .maxlen = sizeof(int),
  236. .mode = 0644,
  237. .proc_handler = proc_dointvec
  238. },
  239. #ifdef CONFIG_RPS
  240. {
  241. .procname = "rps_sock_flow_entries",
  242. .maxlen = sizeof(int),
  243. .mode = 0644,
  244. .proc_handler = rps_sock_flow_sysctl
  245. },
  246. #endif
  247. #ifdef CONFIG_NET_FLOW_LIMIT
  248. {
  249. .procname = "flow_limit_cpu_bitmap",
  250. .mode = 0644,
  251. .proc_handler = flow_limit_cpu_sysctl
  252. },
  253. {
  254. .procname = "flow_limit_table_len",
  255. .data = &netdev_flow_limit_table_len,
  256. .maxlen = sizeof(int),
  257. .mode = 0644,
  258. .proc_handler = flow_limit_table_len_sysctl
  259. },
  260. #endif /* CONFIG_NET_FLOW_LIMIT */
  261. #endif /* CONFIG_NET */
  262. {
  263. .procname = "netdev_budget",
  264. .data = &netdev_budget,
  265. .maxlen = sizeof(int),
  266. .mode = 0644,
  267. .proc_handler = proc_dointvec
  268. },
  269. {
  270. .procname = "warnings",
  271. .data = &net_msg_warn,
  272. .maxlen = sizeof(int),
  273. .mode = 0644,
  274. .proc_handler = proc_dointvec
  275. },
  276. { }
  277. };
  278. static struct ctl_table netns_core_table[] = {
  279. {
  280. .procname = "somaxconn",
  281. .data = &init_net.core.sysctl_somaxconn,
  282. .maxlen = sizeof(int),
  283. .mode = 0644,
  284. .proc_handler = proc_dointvec
  285. },
  286. { }
  287. };
  288. static __net_init int sysctl_core_net_init(struct net *net)
  289. {
  290. struct ctl_table *tbl;
  291. net->core.sysctl_somaxconn = SOMAXCONN;
  292. tbl = netns_core_table;
  293. if (!net_eq(net, &init_net)) {
  294. tbl = kmemdup(tbl, sizeof(netns_core_table), GFP_KERNEL);
  295. if (tbl == NULL)
  296. goto err_dup;
  297. tbl[0].data = &net->core.sysctl_somaxconn;
  298. /* Don't export any sysctls to unprivileged users */
  299. if (net->user_ns != &init_user_ns) {
  300. tbl[0].procname = NULL;
  301. }
  302. }
  303. net->core.sysctl_hdr = register_net_sysctl(net, "net/core", tbl);
  304. if (net->core.sysctl_hdr == NULL)
  305. goto err_reg;
  306. return 0;
  307. err_reg:
  308. if (tbl != netns_core_table)
  309. kfree(tbl);
  310. err_dup:
  311. return -ENOMEM;
  312. }
  313. static __net_exit void sysctl_core_net_exit(struct net *net)
  314. {
  315. struct ctl_table *tbl;
  316. tbl = net->core.sysctl_hdr->ctl_table_arg;
  317. unregister_net_sysctl_table(net->core.sysctl_hdr);
  318. BUG_ON(tbl == netns_core_table);
  319. kfree(tbl);
  320. }
  321. static __net_initdata struct pernet_operations sysctl_core_ops = {
  322. .init = sysctl_core_net_init,
  323. .exit = sysctl_core_net_exit,
  324. };
  325. static __init int sysctl_core_init(void)
  326. {
  327. register_net_sysctl(&init_net, "net/core", net_core_table);
  328. return register_pernet_subsys(&sysctl_core_ops);
  329. }
  330. fs_initcall(sysctl_core_init);