sysctl_net_core.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 <net/ip.h>
  17. #include <net/sock.h>
  18. #include <net/net_ratelimit.h>
  19. #ifdef CONFIG_RPS
  20. static int rps_sock_flow_sysctl(ctl_table *table, int write,
  21. void __user *buffer, size_t *lenp, loff_t *ppos)
  22. {
  23. unsigned int orig_size, size;
  24. int ret, i;
  25. ctl_table tmp = {
  26. .data = &size,
  27. .maxlen = sizeof(size),
  28. .mode = table->mode
  29. };
  30. struct rps_sock_flow_table *orig_sock_table, *sock_table;
  31. static DEFINE_MUTEX(sock_flow_mutex);
  32. mutex_lock(&sock_flow_mutex);
  33. orig_sock_table = rcu_dereference_protected(rps_sock_flow_table,
  34. lockdep_is_held(&sock_flow_mutex));
  35. size = orig_size = orig_sock_table ? orig_sock_table->mask + 1 : 0;
  36. ret = proc_dointvec(&tmp, write, buffer, lenp, ppos);
  37. if (write) {
  38. if (size) {
  39. if (size > 1<<30) {
  40. /* Enforce limit to prevent overflow */
  41. mutex_unlock(&sock_flow_mutex);
  42. return -EINVAL;
  43. }
  44. size = roundup_pow_of_two(size);
  45. if (size != orig_size) {
  46. sock_table =
  47. vmalloc(RPS_SOCK_FLOW_TABLE_SIZE(size));
  48. if (!sock_table) {
  49. mutex_unlock(&sock_flow_mutex);
  50. return -ENOMEM;
  51. }
  52. sock_table->mask = size - 1;
  53. } else
  54. sock_table = orig_sock_table;
  55. for (i = 0; i < size; i++)
  56. sock_table->ents[i] = RPS_NO_CPU;
  57. } else
  58. sock_table = NULL;
  59. if (sock_table != orig_sock_table) {
  60. rcu_assign_pointer(rps_sock_flow_table, sock_table);
  61. synchronize_rcu();
  62. vfree(orig_sock_table);
  63. }
  64. }
  65. mutex_unlock(&sock_flow_mutex);
  66. return ret;
  67. }
  68. #endif /* CONFIG_RPS */
  69. static struct ctl_table net_core_table[] = {
  70. #ifdef CONFIG_NET
  71. {
  72. .procname = "wmem_max",
  73. .data = &sysctl_wmem_max,
  74. .maxlen = sizeof(int),
  75. .mode = 0644,
  76. .proc_handler = proc_dointvec
  77. },
  78. {
  79. .procname = "rmem_max",
  80. .data = &sysctl_rmem_max,
  81. .maxlen = sizeof(int),
  82. .mode = 0644,
  83. .proc_handler = proc_dointvec
  84. },
  85. {
  86. .procname = "wmem_default",
  87. .data = &sysctl_wmem_default,
  88. .maxlen = sizeof(int),
  89. .mode = 0644,
  90. .proc_handler = proc_dointvec
  91. },
  92. {
  93. .procname = "rmem_default",
  94. .data = &sysctl_rmem_default,
  95. .maxlen = sizeof(int),
  96. .mode = 0644,
  97. .proc_handler = proc_dointvec
  98. },
  99. {
  100. .procname = "dev_weight",
  101. .data = &weight_p,
  102. .maxlen = sizeof(int),
  103. .mode = 0644,
  104. .proc_handler = proc_dointvec
  105. },
  106. {
  107. .procname = "netdev_max_backlog",
  108. .data = &netdev_max_backlog,
  109. .maxlen = sizeof(int),
  110. .mode = 0644,
  111. .proc_handler = proc_dointvec
  112. },
  113. #ifdef CONFIG_BPF_JIT
  114. {
  115. .procname = "bpf_jit_enable",
  116. .data = &bpf_jit_enable,
  117. .maxlen = sizeof(int),
  118. .mode = 0644,
  119. .proc_handler = proc_dointvec
  120. },
  121. #endif
  122. {
  123. .procname = "netdev_tstamp_prequeue",
  124. .data = &netdev_tstamp_prequeue,
  125. .maxlen = sizeof(int),
  126. .mode = 0644,
  127. .proc_handler = proc_dointvec
  128. },
  129. {
  130. .procname = "message_cost",
  131. .data = &net_ratelimit_state.interval,
  132. .maxlen = sizeof(int),
  133. .mode = 0644,
  134. .proc_handler = proc_dointvec_jiffies,
  135. },
  136. {
  137. .procname = "message_burst",
  138. .data = &net_ratelimit_state.burst,
  139. .maxlen = sizeof(int),
  140. .mode = 0644,
  141. .proc_handler = proc_dointvec,
  142. },
  143. {
  144. .procname = "optmem_max",
  145. .data = &sysctl_optmem_max,
  146. .maxlen = sizeof(int),
  147. .mode = 0644,
  148. .proc_handler = proc_dointvec
  149. },
  150. #ifdef CONFIG_RPS
  151. {
  152. .procname = "rps_sock_flow_entries",
  153. .maxlen = sizeof(int),
  154. .mode = 0644,
  155. .proc_handler = rps_sock_flow_sysctl
  156. },
  157. #endif
  158. #endif /* CONFIG_NET */
  159. {
  160. .procname = "netdev_budget",
  161. .data = &netdev_budget,
  162. .maxlen = sizeof(int),
  163. .mode = 0644,
  164. .proc_handler = proc_dointvec
  165. },
  166. {
  167. .procname = "warnings",
  168. .data = &net_msg_warn,
  169. .maxlen = sizeof(int),
  170. .mode = 0644,
  171. .proc_handler = proc_dointvec
  172. },
  173. { }
  174. };
  175. static struct ctl_table netns_core_table[] = {
  176. {
  177. .procname = "somaxconn",
  178. .data = &init_net.core.sysctl_somaxconn,
  179. .maxlen = sizeof(int),
  180. .mode = 0644,
  181. .proc_handler = proc_dointvec
  182. },
  183. { }
  184. };
  185. __net_initdata struct ctl_path net_core_path[] = {
  186. { .procname = "net", },
  187. { .procname = "core", },
  188. { },
  189. };
  190. static __net_init int sysctl_core_net_init(struct net *net)
  191. {
  192. struct ctl_table *tbl;
  193. net->core.sysctl_somaxconn = SOMAXCONN;
  194. tbl = netns_core_table;
  195. if (!net_eq(net, &init_net)) {
  196. tbl = kmemdup(tbl, sizeof(netns_core_table), GFP_KERNEL);
  197. if (tbl == NULL)
  198. goto err_dup;
  199. tbl[0].data = &net->core.sysctl_somaxconn;
  200. }
  201. net->core.sysctl_hdr = register_net_sysctl_table(net,
  202. net_core_path, tbl);
  203. if (net->core.sysctl_hdr == NULL)
  204. goto err_reg;
  205. return 0;
  206. err_reg:
  207. if (tbl != netns_core_table)
  208. kfree(tbl);
  209. err_dup:
  210. return -ENOMEM;
  211. }
  212. static __net_exit void sysctl_core_net_exit(struct net *net)
  213. {
  214. struct ctl_table *tbl;
  215. tbl = net->core.sysctl_hdr->ctl_table_arg;
  216. unregister_net_sysctl_table(net->core.sysctl_hdr);
  217. BUG_ON(tbl == netns_core_table);
  218. kfree(tbl);
  219. }
  220. static __net_initdata struct pernet_operations sysctl_core_ops = {
  221. .init = sysctl_core_net_init,
  222. .exit = sysctl_core_net_exit,
  223. };
  224. static __init int sysctl_core_init(void)
  225. {
  226. static struct ctl_table empty[1];
  227. register_sysctl_paths(net_core_path, empty);
  228. register_net_sysctl_rotable(net_core_path, net_core_table);
  229. return register_pernet_subsys(&sysctl_core_ops);
  230. }
  231. fs_initcall(sysctl_core_init);