Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 8 | #include <linux/mm.h> |
| 9 | #include <linux/sysctl.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/module.h> |
Arnaldo Carvalho de Melo | 2038073 | 2005-08-16 02:18:02 -0300 | [diff] [blame] | 11 | #include <linux/socket.h> |
Pavel Emelyanov | a37ae40 | 2007-10-23 21:13:53 -0700 | [diff] [blame] | 12 | #include <linux/netdevice.h> |
Ingo Molnar | 3fff4c4 | 2009-09-22 16:18:09 +0200 | [diff] [blame] | 13 | #include <linux/ratelimit.h> |
Tom Herbert | fec5e65 | 2010-04-16 16:01:27 -0700 | [diff] [blame] | 14 | #include <linux/vmalloc.h> |
Pavel Emelyanov | 33eb9cf | 2007-12-05 01:37:34 -0800 | [diff] [blame] | 15 | #include <linux/init.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 16 | #include <linux/slab.h> |
Shan Wei | 7426a56 | 2012-04-18 18:05:46 +0000 | [diff] [blame] | 17 | #include <linux/kmemleak.h> |
Ingo Molnar | 3fff4c4 | 2009-09-22 16:18:09 +0200 | [diff] [blame] | 18 | |
Hannes Eder | 63d819c | 2009-02-25 10:32:14 +0000 | [diff] [blame] | 19 | #include <net/ip.h> |
Arnaldo Carvalho de Melo | 2038073 | 2005-08-16 02:18:02 -0300 | [diff] [blame] | 20 | #include <net/sock.h> |
David S. Miller | c5c177b | 2011-05-27 13:41:33 -0400 | [diff] [blame] | 21 | #include <net/net_ratelimit.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
Tom Herbert | fec5e65 | 2010-04-16 16:01:27 -0700 | [diff] [blame] | 23 | #ifdef CONFIG_RPS |
| 24 | static int rps_sock_flow_sysctl(ctl_table *table, int write, |
| 25 | void __user *buffer, size_t *lenp, loff_t *ppos) |
| 26 | { |
| 27 | unsigned int orig_size, size; |
| 28 | int ret, i; |
| 29 | ctl_table tmp = { |
| 30 | .data = &size, |
| 31 | .maxlen = sizeof(size), |
| 32 | .mode = table->mode |
| 33 | }; |
| 34 | struct rps_sock_flow_table *orig_sock_table, *sock_table; |
| 35 | static DEFINE_MUTEX(sock_flow_mutex); |
| 36 | |
| 37 | mutex_lock(&sock_flow_mutex); |
| 38 | |
Eric Dumazet | 6e3f7fa | 2010-10-25 03:02:02 +0000 | [diff] [blame] | 39 | orig_sock_table = rcu_dereference_protected(rps_sock_flow_table, |
| 40 | lockdep_is_held(&sock_flow_mutex)); |
Tom Herbert | fec5e65 | 2010-04-16 16:01:27 -0700 | [diff] [blame] | 41 | size = orig_size = orig_sock_table ? orig_sock_table->mask + 1 : 0; |
| 42 | |
| 43 | ret = proc_dointvec(&tmp, write, buffer, lenp, ppos); |
| 44 | |
| 45 | if (write) { |
| 46 | if (size) { |
| 47 | if (size > 1<<30) { |
| 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 | |
| 61 | sock_table->mask = size - 1; |
| 62 | } else |
| 63 | sock_table = orig_sock_table; |
| 64 | |
| 65 | for (i = 0; i < size; i++) |
| 66 | sock_table->ents[i] = RPS_NO_CPU; |
| 67 | } else |
| 68 | sock_table = NULL; |
| 69 | |
| 70 | if (sock_table != orig_sock_table) { |
| 71 | rcu_assign_pointer(rps_sock_flow_table, sock_table); |
Eric Dumazet | adc9300 | 2011-11-17 03:13:26 +0000 | [diff] [blame] | 72 | if (sock_table) |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 73 | static_key_slow_inc(&rps_needed); |
Eric Dumazet | adc9300 | 2011-11-17 03:13:26 +0000 | [diff] [blame] | 74 | if (orig_sock_table) { |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 75 | static_key_slow_dec(&rps_needed); |
Eric Dumazet | adc9300 | 2011-11-17 03:13:26 +0000 | [diff] [blame] | 76 | synchronize_rcu(); |
| 77 | vfree(orig_sock_table); |
| 78 | } |
Tom Herbert | fec5e65 | 2010-04-16 16:01:27 -0700 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
| 82 | mutex_unlock(&sock_flow_mutex); |
| 83 | |
| 84 | return ret; |
| 85 | } |
| 86 | #endif /* CONFIG_RPS */ |
| 87 | |
Pavel Emelyanov | 33eb9cf | 2007-12-05 01:37:34 -0800 | [diff] [blame] | 88 | static struct ctl_table net_core_table[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | #ifdef CONFIG_NET |
| 90 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | .procname = "wmem_max", |
| 92 | .data = &sysctl_wmem_max, |
| 93 | .maxlen = sizeof(int), |
| 94 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 95 | .proc_handler = proc_dointvec |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | }, |
| 97 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | .procname = "rmem_max", |
| 99 | .data = &sysctl_rmem_max, |
| 100 | .maxlen = sizeof(int), |
| 101 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 102 | .proc_handler = proc_dointvec |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | }, |
| 104 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | .procname = "wmem_default", |
| 106 | .data = &sysctl_wmem_default, |
| 107 | .maxlen = sizeof(int), |
| 108 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 109 | .proc_handler = proc_dointvec |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | }, |
| 111 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | .procname = "rmem_default", |
| 113 | .data = &sysctl_rmem_default, |
| 114 | .maxlen = sizeof(int), |
| 115 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 116 | .proc_handler = proc_dointvec |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | }, |
| 118 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | .procname = "dev_weight", |
| 120 | .data = &weight_p, |
| 121 | .maxlen = sizeof(int), |
| 122 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 123 | .proc_handler = proc_dointvec |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | }, |
| 125 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | .procname = "netdev_max_backlog", |
| 127 | .data = &netdev_max_backlog, |
| 128 | .maxlen = sizeof(int), |
| 129 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 130 | .proc_handler = proc_dointvec |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | }, |
Eric Dumazet | 0a14842 | 2011-04-20 09:27:32 +0000 | [diff] [blame] | 132 | #ifdef CONFIG_BPF_JIT |
| 133 | { |
| 134 | .procname = "bpf_jit_enable", |
| 135 | .data = &bpf_jit_enable, |
| 136 | .maxlen = sizeof(int), |
| 137 | .mode = 0644, |
| 138 | .proc_handler = proc_dointvec |
| 139 | }, |
| 140 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | { |
Eric Dumazet | 3b098e2 | 2010-05-15 23:57:10 -0700 | [diff] [blame] | 142 | .procname = "netdev_tstamp_prequeue", |
| 143 | .data = &netdev_tstamp_prequeue, |
| 144 | .maxlen = sizeof(int), |
| 145 | .mode = 0644, |
| 146 | .proc_handler = proc_dointvec |
| 147 | }, |
| 148 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | .procname = "message_cost", |
Dave Young | 717115e | 2008-07-25 01:45:58 -0700 | [diff] [blame] | 150 | .data = &net_ratelimit_state.interval, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | .maxlen = sizeof(int), |
| 152 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 153 | .proc_handler = proc_dointvec_jiffies, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | }, |
| 155 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | .procname = "message_burst", |
Dave Young | 717115e | 2008-07-25 01:45:58 -0700 | [diff] [blame] | 157 | .data = &net_ratelimit_state.burst, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | .maxlen = sizeof(int), |
| 159 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 160 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | }, |
| 162 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | .procname = "optmem_max", |
| 164 | .data = &sysctl_optmem_max, |
| 165 | .maxlen = sizeof(int), |
| 166 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 167 | .proc_handler = proc_dointvec |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | }, |
Tom Herbert | fec5e65 | 2010-04-16 16:01:27 -0700 | [diff] [blame] | 169 | #ifdef CONFIG_RPS |
| 170 | { |
| 171 | .procname = "rps_sock_flow_entries", |
| 172 | .maxlen = sizeof(int), |
| 173 | .mode = 0644, |
| 174 | .proc_handler = rps_sock_flow_sysctl |
| 175 | }, |
| 176 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | #endif /* CONFIG_NET */ |
| 178 | { |
Stephen Hemminger | 51b0bde | 2005-06-23 20:14:40 -0700 | [diff] [blame] | 179 | .procname = "netdev_budget", |
| 180 | .data = &netdev_budget, |
| 181 | .maxlen = sizeof(int), |
| 182 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 183 | .proc_handler = proc_dointvec |
Stephen Hemminger | 51b0bde | 2005-06-23 20:14:40 -0700 | [diff] [blame] | 184 | }, |
Stephen Hemminger | a2a316f | 2007-03-08 20:41:08 -0800 | [diff] [blame] | 185 | { |
Stephen Hemminger | a2a316f | 2007-03-08 20:41:08 -0800 | [diff] [blame] | 186 | .procname = "warnings", |
| 187 | .data = &net_msg_warn, |
| 188 | .maxlen = sizeof(int), |
| 189 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 190 | .proc_handler = proc_dointvec |
Stephen Hemminger | a2a316f | 2007-03-08 20:41:08 -0800 | [diff] [blame] | 191 | }, |
Eric W. Biederman | f8572d8 | 2009-11-05 13:32:03 -0800 | [diff] [blame] | 192 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | }; |
Pavel Emelyanov | 33eb9cf | 2007-12-05 01:37:34 -0800 | [diff] [blame] | 194 | |
Pavel Emelyanov | d5a4502 | 2008-05-19 13:49:52 -0700 | [diff] [blame] | 195 | static struct ctl_table netns_core_table[] = { |
| 196 | { |
Pavel Emelyanov | d5a4502 | 2008-05-19 13:49:52 -0700 | [diff] [blame] | 197 | .procname = "somaxconn", |
| 198 | .data = &init_net.core.sysctl_somaxconn, |
| 199 | .maxlen = sizeof(int), |
| 200 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 201 | .proc_handler = proc_dointvec |
Pavel Emelyanov | d5a4502 | 2008-05-19 13:49:52 -0700 | [diff] [blame] | 202 | }, |
Eric W. Biederman | f8572d8 | 2009-11-05 13:32:03 -0800 | [diff] [blame] | 203 | { } |
Pavel Emelyanov | d5a4502 | 2008-05-19 13:49:52 -0700 | [diff] [blame] | 204 | }; |
| 205 | |
Pavel Emelyanov | 024626e | 2007-12-08 00:09:24 -0800 | [diff] [blame] | 206 | static __net_init int sysctl_core_net_init(struct net *net) |
| 207 | { |
Pavel Emelyanov | d5a4502 | 2008-05-19 13:49:52 -0700 | [diff] [blame] | 208 | struct ctl_table *tbl; |
Pavel Emelyanov | 024626e | 2007-12-08 00:09:24 -0800 | [diff] [blame] | 209 | |
Pavel Emelyanov | 8efa6e9 | 2008-03-31 19:41:14 -0700 | [diff] [blame] | 210 | net->core.sysctl_somaxconn = SOMAXCONN; |
Pavel Emelyanov | b8e1f9b | 2007-12-08 00:12:33 -0800 | [diff] [blame] | 211 | |
Pavel Emelyanov | d5a4502 | 2008-05-19 13:49:52 -0700 | [diff] [blame] | 212 | tbl = netns_core_table; |
Octavian Purdila | 09ad9bc | 2009-11-25 15:14:13 -0800 | [diff] [blame] | 213 | if (!net_eq(net, &init_net)) { |
Pavel Emelyanov | d5a4502 | 2008-05-19 13:49:52 -0700 | [diff] [blame] | 214 | tbl = kmemdup(tbl, sizeof(netns_core_table), GFP_KERNEL); |
Pavel Emelyanov | 024626e | 2007-12-08 00:09:24 -0800 | [diff] [blame] | 215 | if (tbl == NULL) |
| 216 | goto err_dup; |
| 217 | |
Pavel Emelyanov | d5a4502 | 2008-05-19 13:49:52 -0700 | [diff] [blame] | 218 | tbl[0].data = &net->core.sysctl_somaxconn; |
Pavel Emelyanov | 024626e | 2007-12-08 00:09:24 -0800 | [diff] [blame] | 219 | } |
| 220 | |
Eric W. Biederman | ec8f23c | 2012-04-19 13:44:49 +0000 | [diff] [blame] | 221 | net->core.sysctl_hdr = register_net_sysctl(net, "net/core", tbl); |
Pavel Emelyanov | 8efa6e9 | 2008-03-31 19:41:14 -0700 | [diff] [blame] | 222 | if (net->core.sysctl_hdr == NULL) |
Pavel Emelyanov | 024626e | 2007-12-08 00:09:24 -0800 | [diff] [blame] | 223 | goto err_reg; |
| 224 | |
| 225 | return 0; |
| 226 | |
| 227 | err_reg: |
Pavel Emelyanov | d5a4502 | 2008-05-19 13:49:52 -0700 | [diff] [blame] | 228 | if (tbl != netns_core_table) |
Pavel Emelyanov | 024626e | 2007-12-08 00:09:24 -0800 | [diff] [blame] | 229 | kfree(tbl); |
| 230 | err_dup: |
| 231 | return -ENOMEM; |
| 232 | } |
| 233 | |
| 234 | static __net_exit void sysctl_core_net_exit(struct net *net) |
| 235 | { |
| 236 | struct ctl_table *tbl; |
| 237 | |
Pavel Emelyanov | 8efa6e9 | 2008-03-31 19:41:14 -0700 | [diff] [blame] | 238 | tbl = net->core.sysctl_hdr->ctl_table_arg; |
| 239 | unregister_net_sysctl_table(net->core.sysctl_hdr); |
Pavel Emelyanov | d5a4502 | 2008-05-19 13:49:52 -0700 | [diff] [blame] | 240 | BUG_ON(tbl == netns_core_table); |
Pavel Emelyanov | 024626e | 2007-12-08 00:09:24 -0800 | [diff] [blame] | 241 | kfree(tbl); |
| 242 | } |
| 243 | |
| 244 | static __net_initdata struct pernet_operations sysctl_core_ops = { |
| 245 | .init = sysctl_core_net_init, |
| 246 | .exit = sysctl_core_net_exit, |
| 247 | }; |
| 248 | |
Pavel Emelyanov | 33eb9cf | 2007-12-05 01:37:34 -0800 | [diff] [blame] | 249 | static __init int sysctl_core_init(void) |
| 250 | { |
Eric W. Biederman | 4344475 | 2012-04-19 13:22:55 +0000 | [diff] [blame] | 251 | register_net_sysctl(&init_net, "net/core", net_core_table); |
Pavel Emelyanov | 024626e | 2007-12-08 00:09:24 -0800 | [diff] [blame] | 252 | return register_pernet_subsys(&sysctl_core_ops); |
Pavel Emelyanov | 33eb9cf | 2007-12-05 01:37:34 -0800 | [diff] [blame] | 253 | } |
| 254 | |
Alexey Dobriyan | b27aead | 2008-11-25 18:00:48 -0800 | [diff] [blame] | 255 | fs_initcall(sysctl_core_init); |