sysctl_net_core.c 5.2 KB

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