David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 1 | #include <linux/rcupdate.h> |
| 2 | #include <linux/spinlock.h> |
| 3 | #include <linux/jiffies.h> |
David S. Miller | ab92bb2 | 2012-07-09 16:19:30 -0700 | [diff] [blame] | 4 | #include <linux/module.h> |
David S. Miller | 4aabd8e | 2012-07-09 16:07:30 -0700 | [diff] [blame] | 5 | #include <linux/cache.h> |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 6 | #include <linux/slab.h> |
| 7 | #include <linux/init.h> |
David S. Miller | 4aabd8e | 2012-07-09 16:07:30 -0700 | [diff] [blame] | 8 | #include <linux/tcp.h> |
Eric Dumazet | 5815d5e | 2012-07-19 23:02:34 +0000 | [diff] [blame] | 9 | #include <linux/hash.h> |
Julian Anastasov | d23ff70 | 2012-09-04 11:03:15 +0000 | [diff] [blame] | 10 | #include <linux/tcp_metrics.h> |
Eric Dumazet | 976a702 | 2012-11-16 05:31:53 +0000 | [diff] [blame] | 11 | #include <linux/vmalloc.h> |
David S. Miller | 4aabd8e | 2012-07-09 16:07:30 -0700 | [diff] [blame] | 12 | |
| 13 | #include <net/inet_connection_sock.h> |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 14 | #include <net/net_namespace.h> |
David S. Miller | ab92bb2 | 2012-07-09 16:19:30 -0700 | [diff] [blame] | 15 | #include <net/request_sock.h> |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 16 | #include <net/inetpeer.h> |
David S. Miller | 4aabd8e | 2012-07-09 16:07:30 -0700 | [diff] [blame] | 17 | #include <net/sock.h> |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 18 | #include <net/ipv6.h> |
David S. Miller | 4aabd8e | 2012-07-09 16:07:30 -0700 | [diff] [blame] | 19 | #include <net/dst.h> |
| 20 | #include <net/tcp.h> |
Julian Anastasov | d23ff70 | 2012-09-04 11:03:15 +0000 | [diff] [blame] | 21 | #include <net/genetlink.h> |
David S. Miller | 4aabd8e | 2012-07-09 16:07:30 -0700 | [diff] [blame] | 22 | |
| 23 | int sysctl_tcp_nometrics_save __read_mostly; |
| 24 | |
David S. Miller | 4180442 | 2014-01-18 00:55:41 -0800 | [diff] [blame] | 25 | static struct tcp_metrics_block *__tcp_get_metrics(const struct inetpeer_addr *saddr, |
| 26 | const struct inetpeer_addr *daddr, |
Christoph Paasch | 77f99ad | 2014-01-16 20:01:21 +0100 | [diff] [blame] | 27 | struct net *net, unsigned int hash); |
| 28 | |
Yuchung Cheng | 1fe4c48 | 2012-07-19 06:43:06 +0000 | [diff] [blame] | 29 | struct tcp_fastopen_metrics { |
| 30 | u16 mss; |
Daniel Lee | 2646c83 | 2015-04-06 14:37:27 -0700 | [diff] [blame] | 31 | u16 syn_loss:10, /* Recurring Fast Open SYN losses */ |
| 32 | try_exp:2; /* Request w/ exp. option (once) */ |
Yuchung Cheng | aab4874 | 2012-07-19 06:43:10 +0000 | [diff] [blame] | 33 | unsigned long last_syn_loss; /* Last Fast Open SYN loss */ |
Yuchung Cheng | 1fe4c48 | 2012-07-19 06:43:06 +0000 | [diff] [blame] | 34 | struct tcp_fastopen_cookie cookie; |
| 35 | }; |
| 36 | |
Eric Dumazet | 740b0f1 | 2014-02-26 14:02:48 -0800 | [diff] [blame] | 37 | /* TCP_METRIC_MAX includes 2 extra fields for userspace compatibility |
| 38 | * Kernel only stores RTT and RTTVAR in usec resolution |
| 39 | */ |
| 40 | #define TCP_METRIC_MAX_KERNEL (TCP_METRIC_MAX - 2) |
| 41 | |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 42 | struct tcp_metrics_block { |
| 43 | struct tcp_metrics_block __rcu *tcpm_next; |
Eric W. Biederman | 849e8a0 | 2015-03-13 00:05:52 -0500 | [diff] [blame] | 44 | possible_net_t tcpm_net; |
Christoph Paasch | a544302 | 2014-01-08 16:05:56 +0100 | [diff] [blame] | 45 | struct inetpeer_addr tcpm_saddr; |
Christoph Paasch | 324fd55 | 2014-01-08 16:05:55 +0100 | [diff] [blame] | 46 | struct inetpeer_addr tcpm_daddr; |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 47 | unsigned long tcpm_stamp; |
| 48 | u32 tcpm_lock; |
Eric Dumazet | 740b0f1 | 2014-02-26 14:02:48 -0800 | [diff] [blame] | 49 | u32 tcpm_vals[TCP_METRIC_MAX_KERNEL + 1]; |
Yuchung Cheng | 1fe4c48 | 2012-07-19 06:43:06 +0000 | [diff] [blame] | 50 | struct tcp_fastopen_metrics tcpm_fastopen; |
Julian Anastasov | d23ff70 | 2012-09-04 11:03:15 +0000 | [diff] [blame] | 51 | |
| 52 | struct rcu_head rcu_head; |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 53 | }; |
| 54 | |
Eric W. Biederman | 849e8a0 | 2015-03-13 00:05:52 -0500 | [diff] [blame] | 55 | static inline struct net *tm_net(struct tcp_metrics_block *tm) |
| 56 | { |
| 57 | return read_pnet(&tm->tcpm_net); |
| 58 | } |
| 59 | |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 60 | static bool tcp_metric_locked(struct tcp_metrics_block *tm, |
| 61 | enum tcp_metric_index idx) |
| 62 | { |
| 63 | return tm->tcpm_lock & (1 << idx); |
| 64 | } |
| 65 | |
| 66 | static u32 tcp_metric_get(struct tcp_metrics_block *tm, |
| 67 | enum tcp_metric_index idx) |
| 68 | { |
| 69 | return tm->tcpm_vals[idx]; |
| 70 | } |
| 71 | |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 72 | static void tcp_metric_set(struct tcp_metrics_block *tm, |
| 73 | enum tcp_metric_index idx, |
| 74 | u32 val) |
| 75 | { |
| 76 | tm->tcpm_vals[idx] = val; |
| 77 | } |
| 78 | |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 79 | static bool addr_same(const struct inetpeer_addr *a, |
| 80 | const struct inetpeer_addr *b) |
| 81 | { |
David Ahern | d39d14f | 2015-08-27 16:07:01 -0700 | [diff] [blame] | 82 | return inetpeer_addr_cmp(a, b) == 0; |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | struct tcpm_hash_bucket { |
| 86 | struct tcp_metrics_block __rcu *chain; |
| 87 | }; |
| 88 | |
Eric W. Biederman | 098a697 | 2015-03-13 00:07:44 -0500 | [diff] [blame] | 89 | static struct tcpm_hash_bucket *tcp_metrics_hash __read_mostly; |
| 90 | static unsigned int tcp_metrics_hash_log __read_mostly; |
| 91 | |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 92 | static DEFINE_SPINLOCK(tcp_metrics_lock); |
| 93 | |
Eric Dumazet | 740b0f1 | 2014-02-26 14:02:48 -0800 | [diff] [blame] | 94 | static void tcpm_suck_dst(struct tcp_metrics_block *tm, |
| 95 | const struct dst_entry *dst, |
Eric Dumazet | efeaa55 | 2013-05-03 19:12:45 +0000 | [diff] [blame] | 96 | bool fastopen_clear) |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 97 | { |
Eric Dumazet | 740b0f1 | 2014-02-26 14:02:48 -0800 | [diff] [blame] | 98 | u32 msval; |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 99 | u32 val; |
| 100 | |
Julian Anastasov | 9a0a950 | 2012-07-23 10:46:38 +0300 | [diff] [blame] | 101 | tm->tcpm_stamp = jiffies; |
| 102 | |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 103 | val = 0; |
| 104 | if (dst_metric_locked(dst, RTAX_RTT)) |
| 105 | val |= 1 << TCP_METRIC_RTT; |
| 106 | if (dst_metric_locked(dst, RTAX_RTTVAR)) |
| 107 | val |= 1 << TCP_METRIC_RTTVAR; |
| 108 | if (dst_metric_locked(dst, RTAX_SSTHRESH)) |
| 109 | val |= 1 << TCP_METRIC_SSTHRESH; |
| 110 | if (dst_metric_locked(dst, RTAX_CWND)) |
| 111 | val |= 1 << TCP_METRIC_CWND; |
| 112 | if (dst_metric_locked(dst, RTAX_REORDERING)) |
| 113 | val |= 1 << TCP_METRIC_REORDERING; |
| 114 | tm->tcpm_lock = val; |
| 115 | |
Eric Dumazet | 740b0f1 | 2014-02-26 14:02:48 -0800 | [diff] [blame] | 116 | msval = dst_metric_raw(dst, RTAX_RTT); |
| 117 | tm->tcpm_vals[TCP_METRIC_RTT] = msval * USEC_PER_MSEC; |
| 118 | |
| 119 | msval = dst_metric_raw(dst, RTAX_RTTVAR); |
| 120 | tm->tcpm_vals[TCP_METRIC_RTTVAR] = msval * USEC_PER_MSEC; |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 121 | tm->tcpm_vals[TCP_METRIC_SSTHRESH] = dst_metric_raw(dst, RTAX_SSTHRESH); |
| 122 | tm->tcpm_vals[TCP_METRIC_CWND] = dst_metric_raw(dst, RTAX_CWND); |
| 123 | tm->tcpm_vals[TCP_METRIC_REORDERING] = dst_metric_raw(dst, RTAX_REORDERING); |
Eric Dumazet | efeaa55 | 2013-05-03 19:12:45 +0000 | [diff] [blame] | 124 | if (fastopen_clear) { |
| 125 | tm->tcpm_fastopen.mss = 0; |
| 126 | tm->tcpm_fastopen.syn_loss = 0; |
Daniel Lee | 2646c83 | 2015-04-06 14:37:27 -0700 | [diff] [blame] | 127 | tm->tcpm_fastopen.try_exp = 0; |
| 128 | tm->tcpm_fastopen.cookie.exp = false; |
Eric Dumazet | efeaa55 | 2013-05-03 19:12:45 +0000 | [diff] [blame] | 129 | tm->tcpm_fastopen.cookie.len = 0; |
| 130 | } |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Christoph Paasch | 77f99ad | 2014-01-16 20:01:21 +0100 | [diff] [blame] | 133 | #define TCP_METRICS_TIMEOUT (60 * 60 * HZ) |
| 134 | |
| 135 | static void tcpm_check_stamp(struct tcp_metrics_block *tm, struct dst_entry *dst) |
| 136 | { |
| 137 | if (tm && unlikely(time_after(jiffies, tm->tcpm_stamp + TCP_METRICS_TIMEOUT))) |
| 138 | tcpm_suck_dst(tm, dst, false); |
| 139 | } |
| 140 | |
| 141 | #define TCP_METRICS_RECLAIM_DEPTH 5 |
| 142 | #define TCP_METRICS_RECLAIM_PTR (struct tcp_metrics_block *) 0x1UL |
| 143 | |
Eric Dumazet | 9f1ab18 | 2015-03-16 07:14:34 -0700 | [diff] [blame] | 144 | #define deref_locked(p) \ |
| 145 | rcu_dereference_protected(p, lockdep_is_held(&tcp_metrics_lock)) |
| 146 | |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 147 | static struct tcp_metrics_block *tcpm_new(struct dst_entry *dst, |
Christoph Paasch | a544302 | 2014-01-08 16:05:56 +0100 | [diff] [blame] | 148 | struct inetpeer_addr *saddr, |
Christoph Paasch | 324fd55 | 2014-01-08 16:05:55 +0100 | [diff] [blame] | 149 | struct inetpeer_addr *daddr, |
Christoph Paasch | 77f99ad | 2014-01-16 20:01:21 +0100 | [diff] [blame] | 150 | unsigned int hash) |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 151 | { |
| 152 | struct tcp_metrics_block *tm; |
| 153 | struct net *net; |
Christoph Paasch | 77f99ad | 2014-01-16 20:01:21 +0100 | [diff] [blame] | 154 | bool reclaim = false; |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 155 | |
| 156 | spin_lock_bh(&tcp_metrics_lock); |
| 157 | net = dev_net(dst->dev); |
Christoph Paasch | 77f99ad | 2014-01-16 20:01:21 +0100 | [diff] [blame] | 158 | |
| 159 | /* While waiting for the spin-lock the cache might have been populated |
| 160 | * with this entry and so we have to check again. |
| 161 | */ |
David S. Miller | 4180442 | 2014-01-18 00:55:41 -0800 | [diff] [blame] | 162 | tm = __tcp_get_metrics(saddr, daddr, net, hash); |
Christoph Paasch | 77f99ad | 2014-01-16 20:01:21 +0100 | [diff] [blame] | 163 | if (tm == TCP_METRICS_RECLAIM_PTR) { |
| 164 | reclaim = true; |
| 165 | tm = NULL; |
| 166 | } |
| 167 | if (tm) { |
| 168 | tcpm_check_stamp(tm, dst); |
| 169 | goto out_unlock; |
| 170 | } |
| 171 | |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 172 | if (unlikely(reclaim)) { |
| 173 | struct tcp_metrics_block *oldest; |
| 174 | |
Eric Dumazet | 9f1ab18 | 2015-03-16 07:14:34 -0700 | [diff] [blame] | 175 | oldest = deref_locked(tcp_metrics_hash[hash].chain); |
| 176 | for (tm = deref_locked(oldest->tcpm_next); tm; |
| 177 | tm = deref_locked(tm->tcpm_next)) { |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 178 | if (time_before(tm->tcpm_stamp, oldest->tcpm_stamp)) |
| 179 | oldest = tm; |
| 180 | } |
| 181 | tm = oldest; |
| 182 | } else { |
| 183 | tm = kmalloc(sizeof(*tm), GFP_ATOMIC); |
| 184 | if (!tm) |
| 185 | goto out_unlock; |
| 186 | } |
Eric W. Biederman | 849e8a0 | 2015-03-13 00:05:52 -0500 | [diff] [blame] | 187 | write_pnet(&tm->tcpm_net, net); |
Christoph Paasch | a544302 | 2014-01-08 16:05:56 +0100 | [diff] [blame] | 188 | tm->tcpm_saddr = *saddr; |
Christoph Paasch | 324fd55 | 2014-01-08 16:05:55 +0100 | [diff] [blame] | 189 | tm->tcpm_daddr = *daddr; |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 190 | |
Eric Dumazet | efeaa55 | 2013-05-03 19:12:45 +0000 | [diff] [blame] | 191 | tcpm_suck_dst(tm, dst, true); |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 192 | |
| 193 | if (likely(!reclaim)) { |
Eric W. Biederman | 098a697 | 2015-03-13 00:07:44 -0500 | [diff] [blame] | 194 | tm->tcpm_next = tcp_metrics_hash[hash].chain; |
| 195 | rcu_assign_pointer(tcp_metrics_hash[hash].chain, tm); |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | out_unlock: |
| 199 | spin_unlock_bh(&tcp_metrics_lock); |
| 200 | return tm; |
| 201 | } |
| 202 | |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 203 | static struct tcp_metrics_block *tcp_get_encode(struct tcp_metrics_block *tm, int depth) |
| 204 | { |
| 205 | if (tm) |
| 206 | return tm; |
| 207 | if (depth > TCP_METRICS_RECLAIM_DEPTH) |
| 208 | return TCP_METRICS_RECLAIM_PTR; |
| 209 | return NULL; |
| 210 | } |
| 211 | |
Christoph Paasch | a544302 | 2014-01-08 16:05:56 +0100 | [diff] [blame] | 212 | static struct tcp_metrics_block *__tcp_get_metrics(const struct inetpeer_addr *saddr, |
| 213 | const struct inetpeer_addr *daddr, |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 214 | struct net *net, unsigned int hash) |
| 215 | { |
| 216 | struct tcp_metrics_block *tm; |
| 217 | int depth = 0; |
| 218 | |
Eric W. Biederman | 098a697 | 2015-03-13 00:07:44 -0500 | [diff] [blame] | 219 | for (tm = rcu_dereference(tcp_metrics_hash[hash].chain); tm; |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 220 | tm = rcu_dereference(tm->tcpm_next)) { |
Christoph Paasch | a544302 | 2014-01-08 16:05:56 +0100 | [diff] [blame] | 221 | if (addr_same(&tm->tcpm_saddr, saddr) && |
Eric W. Biederman | 849e8a0 | 2015-03-13 00:05:52 -0500 | [diff] [blame] | 222 | addr_same(&tm->tcpm_daddr, daddr) && |
| 223 | net_eq(tm_net(tm), net)) |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 224 | break; |
| 225 | depth++; |
| 226 | } |
| 227 | return tcp_get_encode(tm, depth); |
| 228 | } |
| 229 | |
| 230 | static struct tcp_metrics_block *__tcp_get_metrics_req(struct request_sock *req, |
| 231 | struct dst_entry *dst) |
| 232 | { |
| 233 | struct tcp_metrics_block *tm; |
Christoph Paasch | a544302 | 2014-01-08 16:05:56 +0100 | [diff] [blame] | 234 | struct inetpeer_addr saddr, daddr; |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 235 | unsigned int hash; |
| 236 | struct net *net; |
| 237 | |
Christoph Paasch | a544302 | 2014-01-08 16:05:56 +0100 | [diff] [blame] | 238 | saddr.family = req->rsk_ops->family; |
Christoph Paasch | 324fd55 | 2014-01-08 16:05:55 +0100 | [diff] [blame] | 239 | daddr.family = req->rsk_ops->family; |
| 240 | switch (daddr.family) { |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 241 | case AF_INET: |
David Ahern | 3abef28 | 2015-08-27 16:07:00 -0700 | [diff] [blame] | 242 | inetpeer_set_addr_v4(&saddr, inet_rsk(req)->ir_loc_addr); |
| 243 | inetpeer_set_addr_v4(&daddr, inet_rsk(req)->ir_rmt_addr); |
David Ahern | 72afa35 | 2015-08-27 16:06:59 -0700 | [diff] [blame] | 244 | hash = ipv4_addr_hash(inet_rsk(req)->ir_rmt_addr); |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 245 | break; |
Eric Dumazet | 634fb979 | 2013-10-09 15:21:29 -0700 | [diff] [blame] | 246 | #if IS_ENABLED(CONFIG_IPV6) |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 247 | case AF_INET6: |
David Ahern | 3abef28 | 2015-08-27 16:07:00 -0700 | [diff] [blame] | 248 | inetpeer_set_addr_v6(&saddr, &inet_rsk(req)->ir_v6_loc_addr); |
| 249 | inetpeer_set_addr_v6(&daddr, &inet_rsk(req)->ir_v6_rmt_addr); |
Eric Dumazet | 634fb979 | 2013-10-09 15:21:29 -0700 | [diff] [blame] | 250 | hash = ipv6_addr_hash(&inet_rsk(req)->ir_v6_rmt_addr); |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 251 | break; |
Eric Dumazet | 634fb979 | 2013-10-09 15:21:29 -0700 | [diff] [blame] | 252 | #endif |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 253 | default: |
| 254 | return NULL; |
| 255 | } |
| 256 | |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 257 | net = dev_net(dst->dev); |
Eric W. Biederman | 3e5da62 | 2015-03-13 00:05:24 -0500 | [diff] [blame] | 258 | hash ^= net_hash_mix(net); |
Eric W. Biederman | 098a697 | 2015-03-13 00:07:44 -0500 | [diff] [blame] | 259 | hash = hash_32(hash, tcp_metrics_hash_log); |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 260 | |
Eric W. Biederman | 098a697 | 2015-03-13 00:07:44 -0500 | [diff] [blame] | 261 | for (tm = rcu_dereference(tcp_metrics_hash[hash].chain); tm; |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 262 | tm = rcu_dereference(tm->tcpm_next)) { |
Christoph Paasch | a544302 | 2014-01-08 16:05:56 +0100 | [diff] [blame] | 263 | if (addr_same(&tm->tcpm_saddr, &saddr) && |
Eric W. Biederman | 849e8a0 | 2015-03-13 00:05:52 -0500 | [diff] [blame] | 264 | addr_same(&tm->tcpm_daddr, &daddr) && |
| 265 | net_eq(tm_net(tm), net)) |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 266 | break; |
| 267 | } |
| 268 | tcpm_check_stamp(tm, dst); |
| 269 | return tm; |
| 270 | } |
| 271 | |
| 272 | static struct tcp_metrics_block *tcp_get_metrics(struct sock *sk, |
| 273 | struct dst_entry *dst, |
| 274 | bool create) |
| 275 | { |
| 276 | struct tcp_metrics_block *tm; |
Christoph Paasch | a544302 | 2014-01-08 16:05:56 +0100 | [diff] [blame] | 277 | struct inetpeer_addr saddr, daddr; |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 278 | unsigned int hash; |
| 279 | struct net *net; |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 280 | |
Christoph Paasch | 3ad88cf | 2014-01-22 13:58:44 +0100 | [diff] [blame] | 281 | if (sk->sk_family == AF_INET) { |
David Ahern | 3abef28 | 2015-08-27 16:07:00 -0700 | [diff] [blame] | 282 | inetpeer_set_addr_v4(&saddr, inet_sk(sk)->inet_saddr); |
| 283 | inetpeer_set_addr_v4(&daddr, inet_sk(sk)->inet_daddr); |
David Ahern | 72afa35 | 2015-08-27 16:06:59 -0700 | [diff] [blame] | 284 | hash = ipv4_addr_hash(inet_sk(sk)->inet_daddr); |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 285 | } |
Christoph Paasch | 3ad88cf | 2014-01-22 13:58:44 +0100 | [diff] [blame] | 286 | #if IS_ENABLED(CONFIG_IPV6) |
| 287 | else if (sk->sk_family == AF_INET6) { |
| 288 | if (ipv6_addr_v4mapped(&sk->sk_v6_daddr)) { |
David Ahern | 3abef28 | 2015-08-27 16:07:00 -0700 | [diff] [blame] | 289 | inetpeer_set_addr_v4(&saddr, inet_sk(sk)->inet_saddr); |
| 290 | inetpeer_set_addr_v4(&daddr, inet_sk(sk)->inet_daddr); |
David Ahern | 72afa35 | 2015-08-27 16:06:59 -0700 | [diff] [blame] | 291 | hash = ipv4_addr_hash(inet_sk(sk)->inet_daddr); |
Christoph Paasch | 3ad88cf | 2014-01-22 13:58:44 +0100 | [diff] [blame] | 292 | } else { |
David Ahern | 3abef28 | 2015-08-27 16:07:00 -0700 | [diff] [blame] | 293 | inetpeer_set_addr_v6(&saddr, &sk->sk_v6_rcv_saddr); |
| 294 | inetpeer_set_addr_v6(&daddr, &sk->sk_v6_daddr); |
Christoph Paasch | 3ad88cf | 2014-01-22 13:58:44 +0100 | [diff] [blame] | 295 | hash = ipv6_addr_hash(&sk->sk_v6_daddr); |
| 296 | } |
| 297 | } |
| 298 | #endif |
| 299 | else |
| 300 | return NULL; |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 301 | |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 302 | net = dev_net(dst->dev); |
Eric W. Biederman | 3e5da62 | 2015-03-13 00:05:24 -0500 | [diff] [blame] | 303 | hash ^= net_hash_mix(net); |
Eric W. Biederman | 098a697 | 2015-03-13 00:07:44 -0500 | [diff] [blame] | 304 | hash = hash_32(hash, tcp_metrics_hash_log); |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 305 | |
Christoph Paasch | a544302 | 2014-01-08 16:05:56 +0100 | [diff] [blame] | 306 | tm = __tcp_get_metrics(&saddr, &daddr, net, hash); |
Christoph Paasch | 77f99ad | 2014-01-16 20:01:21 +0100 | [diff] [blame] | 307 | if (tm == TCP_METRICS_RECLAIM_PTR) |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 308 | tm = NULL; |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 309 | if (!tm && create) |
David S. Miller | 4180442 | 2014-01-18 00:55:41 -0800 | [diff] [blame] | 310 | tm = tcpm_new(dst, &saddr, &daddr, hash); |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 311 | else |
| 312 | tcpm_check_stamp(tm, dst); |
| 313 | |
| 314 | return tm; |
| 315 | } |
| 316 | |
David S. Miller | 4aabd8e | 2012-07-09 16:07:30 -0700 | [diff] [blame] | 317 | /* Save metrics learned by this TCP session. This function is called |
| 318 | * only, when TCP finishes successfully i.e. when it enters TIME-WAIT |
| 319 | * or goes from LAST-ACK to CLOSE. |
| 320 | */ |
| 321 | void tcp_update_metrics(struct sock *sk) |
| 322 | { |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 323 | const struct inet_connection_sock *icsk = inet_csk(sk); |
David S. Miller | 4aabd8e | 2012-07-09 16:07:30 -0700 | [diff] [blame] | 324 | struct dst_entry *dst = __sk_dst_get(sk); |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 325 | struct tcp_sock *tp = tcp_sk(sk); |
Nikolay Borisov | 1043e25 | 2016-02-03 09:46:52 +0200 | [diff] [blame] | 326 | struct net *net = sock_net(sk); |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 327 | struct tcp_metrics_block *tm; |
| 328 | unsigned long rtt; |
| 329 | u32 val; |
| 330 | int m; |
David S. Miller | 4aabd8e | 2012-07-09 16:07:30 -0700 | [diff] [blame] | 331 | |
Julian Anastasov | c3a2e83 | 2017-02-06 23:14:14 +0200 | [diff] [blame] | 332 | sk_dst_confirm(sk); |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 333 | if (sysctl_tcp_nometrics_save || !dst) |
David S. Miller | 4aabd8e | 2012-07-09 16:07:30 -0700 | [diff] [blame] | 334 | return; |
| 335 | |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 336 | rcu_read_lock(); |
Eric Dumazet | 740b0f1 | 2014-02-26 14:02:48 -0800 | [diff] [blame] | 337 | if (icsk->icsk_backoff || !tp->srtt_us) { |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 338 | /* This session failed to estimate rtt. Why? |
| 339 | * Probably, no packets returned in time. Reset our |
| 340 | * results. |
David S. Miller | 4aabd8e | 2012-07-09 16:07:30 -0700 | [diff] [blame] | 341 | */ |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 342 | tm = tcp_get_metrics(sk, dst, false); |
| 343 | if (tm && !tcp_metric_locked(tm, TCP_METRIC_RTT)) |
| 344 | tcp_metric_set(tm, TCP_METRIC_RTT, 0); |
| 345 | goto out_unlock; |
| 346 | } else |
| 347 | tm = tcp_get_metrics(sk, dst, true); |
| 348 | |
| 349 | if (!tm) |
| 350 | goto out_unlock; |
| 351 | |
Eric Dumazet | 740b0f1 | 2014-02-26 14:02:48 -0800 | [diff] [blame] | 352 | rtt = tcp_metric_get(tm, TCP_METRIC_RTT); |
| 353 | m = rtt - tp->srtt_us; |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 354 | |
| 355 | /* If newly calculated rtt larger than stored one, store new |
| 356 | * one. Otherwise, use EWMA. Remember, rtt overestimation is |
| 357 | * always better than underestimation. |
| 358 | */ |
| 359 | if (!tcp_metric_locked(tm, TCP_METRIC_RTT)) { |
| 360 | if (m <= 0) |
Eric Dumazet | 740b0f1 | 2014-02-26 14:02:48 -0800 | [diff] [blame] | 361 | rtt = tp->srtt_us; |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 362 | else |
| 363 | rtt -= (m >> 3); |
Eric Dumazet | 740b0f1 | 2014-02-26 14:02:48 -0800 | [diff] [blame] | 364 | tcp_metric_set(tm, TCP_METRIC_RTT, rtt); |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | if (!tcp_metric_locked(tm, TCP_METRIC_RTTVAR)) { |
| 368 | unsigned long var; |
| 369 | |
| 370 | if (m < 0) |
| 371 | m = -m; |
| 372 | |
| 373 | /* Scale deviation to rttvar fixed point */ |
| 374 | m >>= 1; |
Eric Dumazet | 740b0f1 | 2014-02-26 14:02:48 -0800 | [diff] [blame] | 375 | if (m < tp->mdev_us) |
| 376 | m = tp->mdev_us; |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 377 | |
Eric Dumazet | 740b0f1 | 2014-02-26 14:02:48 -0800 | [diff] [blame] | 378 | var = tcp_metric_get(tm, TCP_METRIC_RTTVAR); |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 379 | if (m >= var) |
| 380 | var = m; |
| 381 | else |
| 382 | var -= (var - m) >> 2; |
| 383 | |
Eric Dumazet | 740b0f1 | 2014-02-26 14:02:48 -0800 | [diff] [blame] | 384 | tcp_metric_set(tm, TCP_METRIC_RTTVAR, var); |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | if (tcp_in_initial_slowstart(tp)) { |
| 388 | /* Slow start still did not finish. */ |
| 389 | if (!tcp_metric_locked(tm, TCP_METRIC_SSTHRESH)) { |
| 390 | val = tcp_metric_get(tm, TCP_METRIC_SSTHRESH); |
| 391 | if (val && (tp->snd_cwnd >> 1) > val) |
| 392 | tcp_metric_set(tm, TCP_METRIC_SSTHRESH, |
| 393 | tp->snd_cwnd >> 1); |
David S. Miller | 4aabd8e | 2012-07-09 16:07:30 -0700 | [diff] [blame] | 394 | } |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 395 | if (!tcp_metric_locked(tm, TCP_METRIC_CWND)) { |
| 396 | val = tcp_metric_get(tm, TCP_METRIC_CWND); |
| 397 | if (tp->snd_cwnd > val) |
| 398 | tcp_metric_set(tm, TCP_METRIC_CWND, |
| 399 | tp->snd_cwnd); |
David S. Miller | 4aabd8e | 2012-07-09 16:07:30 -0700 | [diff] [blame] | 400 | } |
Yuchung Cheng | 071d508 | 2015-07-09 13:16:29 -0700 | [diff] [blame] | 401 | } else if (!tcp_in_slow_start(tp) && |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 402 | icsk->icsk_ca_state == TCP_CA_Open) { |
| 403 | /* Cong. avoidance phase, cwnd is reliable. */ |
| 404 | if (!tcp_metric_locked(tm, TCP_METRIC_SSTHRESH)) |
| 405 | tcp_metric_set(tm, TCP_METRIC_SSTHRESH, |
| 406 | max(tp->snd_cwnd >> 1, tp->snd_ssthresh)); |
| 407 | if (!tcp_metric_locked(tm, TCP_METRIC_CWND)) { |
| 408 | val = tcp_metric_get(tm, TCP_METRIC_CWND); |
Alexander Duyck | 2100844 | 2012-07-11 17:18:04 -0700 | [diff] [blame] | 409 | tcp_metric_set(tm, TCP_METRIC_CWND, (val + tp->snd_cwnd) >> 1); |
David S. Miller | 4aabd8e | 2012-07-09 16:07:30 -0700 | [diff] [blame] | 410 | } |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 411 | } else { |
| 412 | /* Else slow start did not finish, cwnd is non-sense, |
| 413 | * ssthresh may be also invalid. |
| 414 | */ |
| 415 | if (!tcp_metric_locked(tm, TCP_METRIC_CWND)) { |
| 416 | val = tcp_metric_get(tm, TCP_METRIC_CWND); |
| 417 | tcp_metric_set(tm, TCP_METRIC_CWND, |
| 418 | (val + tp->snd_ssthresh) >> 1); |
| 419 | } |
| 420 | if (!tcp_metric_locked(tm, TCP_METRIC_SSTHRESH)) { |
| 421 | val = tcp_metric_get(tm, TCP_METRIC_SSTHRESH); |
| 422 | if (val && tp->snd_ssthresh > val) |
| 423 | tcp_metric_set(tm, TCP_METRIC_SSTHRESH, |
| 424 | tp->snd_ssthresh); |
| 425 | } |
| 426 | if (!tcp_metric_locked(tm, TCP_METRIC_REORDERING)) { |
| 427 | val = tcp_metric_get(tm, TCP_METRIC_REORDERING); |
| 428 | if (val < tp->reordering && |
Nikolay Borisov | 1043e25 | 2016-02-03 09:46:52 +0200 | [diff] [blame] | 429 | tp->reordering != net->ipv4.sysctl_tcp_reordering) |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 430 | tcp_metric_set(tm, TCP_METRIC_REORDERING, |
| 431 | tp->reordering); |
David S. Miller | 4aabd8e | 2012-07-09 16:07:30 -0700 | [diff] [blame] | 432 | } |
| 433 | } |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 434 | tm->tcpm_stamp = jiffies; |
| 435 | out_unlock: |
| 436 | rcu_read_unlock(); |
David S. Miller | 4aabd8e | 2012-07-09 16:07:30 -0700 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | /* Initialize metrics on socket. */ |
| 440 | |
| 441 | void tcp_init_metrics(struct sock *sk) |
| 442 | { |
David S. Miller | 4aabd8e | 2012-07-09 16:07:30 -0700 | [diff] [blame] | 443 | struct dst_entry *dst = __sk_dst_get(sk); |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 444 | struct tcp_sock *tp = tcp_sk(sk); |
| 445 | struct tcp_metrics_block *tm; |
Yuchung Cheng | 1b7fdd2 | 2013-08-30 08:35:53 -0700 | [diff] [blame] | 446 | u32 val, crtt = 0; /* cached RTT scaled by 8 */ |
David S. Miller | 4aabd8e | 2012-07-09 16:07:30 -0700 | [diff] [blame] | 447 | |
Julian Anastasov | c3a2e83 | 2017-02-06 23:14:14 +0200 | [diff] [blame] | 448 | sk_dst_confirm(sk); |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 449 | if (!dst) |
David S. Miller | 4aabd8e | 2012-07-09 16:07:30 -0700 | [diff] [blame] | 450 | goto reset; |
| 451 | |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 452 | rcu_read_lock(); |
| 453 | tm = tcp_get_metrics(sk, dst, true); |
| 454 | if (!tm) { |
| 455 | rcu_read_unlock(); |
| 456 | goto reset; |
| 457 | } |
| 458 | |
| 459 | if (tcp_metric_locked(tm, TCP_METRIC_CWND)) |
| 460 | tp->snd_cwnd_clamp = tcp_metric_get(tm, TCP_METRIC_CWND); |
| 461 | |
| 462 | val = tcp_metric_get(tm, TCP_METRIC_SSTHRESH); |
| 463 | if (val) { |
| 464 | tp->snd_ssthresh = val; |
David S. Miller | 4aabd8e | 2012-07-09 16:07:30 -0700 | [diff] [blame] | 465 | if (tp->snd_ssthresh > tp->snd_cwnd_clamp) |
| 466 | tp->snd_ssthresh = tp->snd_cwnd_clamp; |
| 467 | } else { |
| 468 | /* ssthresh may have been reduced unnecessarily during. |
| 469 | * 3WHS. Restore it back to its initial default. |
| 470 | */ |
| 471 | tp->snd_ssthresh = TCP_INFINITE_SSTHRESH; |
| 472 | } |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 473 | val = tcp_metric_get(tm, TCP_METRIC_REORDERING); |
| 474 | if (val && tp->reordering != val) { |
David S. Miller | 4aabd8e | 2012-07-09 16:07:30 -0700 | [diff] [blame] | 475 | tcp_disable_fack(tp); |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 476 | tp->reordering = val; |
David S. Miller | 4aabd8e | 2012-07-09 16:07:30 -0700 | [diff] [blame] | 477 | } |
| 478 | |
Eric Dumazet | 740b0f1 | 2014-02-26 14:02:48 -0800 | [diff] [blame] | 479 | crtt = tcp_metric_get(tm, TCP_METRIC_RTT); |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 480 | rcu_read_unlock(); |
David S. Miller | 4aabd8e | 2012-07-09 16:07:30 -0700 | [diff] [blame] | 481 | reset: |
Yuchung Cheng | 52f20e6 | 2013-09-03 14:14:35 -0700 | [diff] [blame] | 482 | /* The initial RTT measurement from the SYN/SYN-ACK is not ideal |
| 483 | * to seed the RTO for later data packets because SYN packets are |
| 484 | * small. Use the per-dst cached values to seed the RTO but keep |
| 485 | * the RTT estimator variables intact (e.g., srtt, mdev, rttvar). |
| 486 | * Later the RTO will be updated immediately upon obtaining the first |
| 487 | * data RTT sample (tcp_rtt_estimator()). Hence the cached RTT only |
| 488 | * influences the first RTO but not later RTT estimation. |
| 489 | * |
| 490 | * But if RTT is not available from the SYN (due to retransmits or |
| 491 | * syn cookies) or the cache, force a conservative 3secs timeout. |
| 492 | * |
| 493 | * A bit of theory. RTT is time passed after "normal" sized packet |
| 494 | * is sent until it is ACKed. In normal circumstances sending small |
| 495 | * packets force peer to delay ACKs and calculation is correct too. |
| 496 | * The algorithm is adaptive and, provided we follow specs, it |
| 497 | * NEVER underestimate RTT. BUT! If peer tries to make some clever |
| 498 | * tricks sort of "quick acks" for time long enough to decrease RTT |
| 499 | * to low value, and then abruptly stops to do it and starts to delay |
| 500 | * ACKs, wait for troubles. |
| 501 | */ |
Eric Dumazet | 740b0f1 | 2014-02-26 14:02:48 -0800 | [diff] [blame] | 502 | if (crtt > tp->srtt_us) { |
Neal Cardwell | 269aa75 | 2013-09-16 21:44:20 -0400 | [diff] [blame] | 503 | /* Set RTO like tcp_rtt_estimator(), but from cached RTT. */ |
Konstantin Khlebnikov | 9bdfb3b | 2016-02-21 10:12:39 +0300 | [diff] [blame] | 504 | crtt /= 8 * USEC_PER_SEC / HZ; |
Neal Cardwell | 269aa75 | 2013-09-16 21:44:20 -0400 | [diff] [blame] | 505 | inet_csk(sk)->icsk_rto = crtt + max(2 * crtt, tcp_rto_min(sk)); |
Eric Dumazet | 740b0f1 | 2014-02-26 14:02:48 -0800 | [diff] [blame] | 506 | } else if (tp->srtt_us == 0) { |
David S. Miller | 4aabd8e | 2012-07-09 16:07:30 -0700 | [diff] [blame] | 507 | /* RFC6298: 5.7 We've failed to get a valid RTT sample from |
| 508 | * 3WHS. This is most likely due to retransmission, |
| 509 | * including spurious one. Reset the RTO back to 3secs |
| 510 | * from the more aggressive 1sec to avoid more spurious |
| 511 | * retransmission. |
| 512 | */ |
Eric Dumazet | 740b0f1 | 2014-02-26 14:02:48 -0800 | [diff] [blame] | 513 | tp->rttvar_us = jiffies_to_usecs(TCP_TIMEOUT_FALLBACK); |
| 514 | tp->mdev_us = tp->mdev_max_us = tp->rttvar_us; |
| 515 | |
David S. Miller | 4aabd8e | 2012-07-09 16:07:30 -0700 | [diff] [blame] | 516 | inet_csk(sk)->icsk_rto = TCP_TIMEOUT_FALLBACK; |
| 517 | } |
| 518 | /* Cut cwnd down to 1 per RFC5681 if SYN or SYN-ACK has been |
| 519 | * retransmitted. In light of RFC6298 more aggressive 1sec |
| 520 | * initRTO, we only reset cwnd when more than 1 SYN/SYN-ACK |
| 521 | * retransmission has occurred. |
| 522 | */ |
| 523 | if (tp->total_retrans > 1) |
| 524 | tp->snd_cwnd = 1; |
| 525 | else |
| 526 | tp->snd_cwnd = tcp_init_cwnd(tp, dst); |
Eric Dumazet | c2203cf | 2017-05-16 14:00:04 -0700 | [diff] [blame^] | 527 | tp->snd_cwnd_stamp = tcp_jiffies32; |
David S. Miller | 4aabd8e | 2012-07-09 16:07:30 -0700 | [diff] [blame] | 528 | } |
David S. Miller | ab92bb2 | 2012-07-09 16:19:30 -0700 | [diff] [blame] | 529 | |
Soheil Hassas Yeganeh | d82bae1 | 2017-03-15 16:30:45 -0400 | [diff] [blame] | 530 | bool tcp_peer_is_proven(struct request_sock *req, struct dst_entry *dst) |
David S. Miller | ab92bb2 | 2012-07-09 16:19:30 -0700 | [diff] [blame] | 531 | { |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 532 | struct tcp_metrics_block *tm; |
| 533 | bool ret; |
| 534 | |
David S. Miller | ab92bb2 | 2012-07-09 16:19:30 -0700 | [diff] [blame] | 535 | if (!dst) |
| 536 | return false; |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 537 | |
| 538 | rcu_read_lock(); |
| 539 | tm = __tcp_get_metrics_req(req, dst); |
Soheil Hassas Yeganeh | d82bae1 | 2017-03-15 16:30:45 -0400 | [diff] [blame] | 540 | if (tm && tcp_metric_get(tm, TCP_METRIC_RTT)) |
David S. Miller | 81166dd | 2012-07-10 03:14:24 -0700 | [diff] [blame] | 541 | ret = true; |
Soheil Hassas Yeganeh | d82bae1 | 2017-03-15 16:30:45 -0400 | [diff] [blame] | 542 | else |
| 543 | ret = false; |
David S. Miller | 81166dd | 2012-07-10 03:14:24 -0700 | [diff] [blame] | 544 | rcu_read_unlock(); |
| 545 | |
| 546 | return ret; |
| 547 | } |
| 548 | |
Yuchung Cheng | 1fe4c48 | 2012-07-19 06:43:06 +0000 | [diff] [blame] | 549 | static DEFINE_SEQLOCK(fastopen_seqlock); |
| 550 | |
| 551 | void tcp_fastopen_cache_get(struct sock *sk, u16 *mss, |
Yuchung Cheng | aab4874 | 2012-07-19 06:43:10 +0000 | [diff] [blame] | 552 | struct tcp_fastopen_cookie *cookie, |
| 553 | int *syn_loss, unsigned long *last_syn_loss) |
Yuchung Cheng | 1fe4c48 | 2012-07-19 06:43:06 +0000 | [diff] [blame] | 554 | { |
| 555 | struct tcp_metrics_block *tm; |
| 556 | |
| 557 | rcu_read_lock(); |
| 558 | tm = tcp_get_metrics(sk, __sk_dst_get(sk), false); |
| 559 | if (tm) { |
| 560 | struct tcp_fastopen_metrics *tfom = &tm->tcpm_fastopen; |
| 561 | unsigned int seq; |
| 562 | |
| 563 | do { |
| 564 | seq = read_seqbegin(&fastopen_seqlock); |
| 565 | if (tfom->mss) |
| 566 | *mss = tfom->mss; |
| 567 | *cookie = tfom->cookie; |
Daniel Lee | 2646c83 | 2015-04-06 14:37:27 -0700 | [diff] [blame] | 568 | if (cookie->len <= 0 && tfom->try_exp == 1) |
| 569 | cookie->exp = true; |
Yuchung Cheng | aab4874 | 2012-07-19 06:43:10 +0000 | [diff] [blame] | 570 | *syn_loss = tfom->syn_loss; |
| 571 | *last_syn_loss = *syn_loss ? tfom->last_syn_loss : 0; |
Yuchung Cheng | 1fe4c48 | 2012-07-19 06:43:06 +0000 | [diff] [blame] | 572 | } while (read_seqretry(&fastopen_seqlock, seq)); |
| 573 | } |
| 574 | rcu_read_unlock(); |
| 575 | } |
| 576 | |
Yuchung Cheng | 1fe4c48 | 2012-07-19 06:43:06 +0000 | [diff] [blame] | 577 | void tcp_fastopen_cache_set(struct sock *sk, u16 mss, |
Daniel Lee | 2646c83 | 2015-04-06 14:37:27 -0700 | [diff] [blame] | 578 | struct tcp_fastopen_cookie *cookie, bool syn_lost, |
| 579 | u16 try_exp) |
Yuchung Cheng | 1fe4c48 | 2012-07-19 06:43:06 +0000 | [diff] [blame] | 580 | { |
Eric Dumazet | dccf76c | 2013-11-13 15:00:46 -0800 | [diff] [blame] | 581 | struct dst_entry *dst = __sk_dst_get(sk); |
Yuchung Cheng | 1fe4c48 | 2012-07-19 06:43:06 +0000 | [diff] [blame] | 582 | struct tcp_metrics_block *tm; |
| 583 | |
Eric Dumazet | dccf76c | 2013-11-13 15:00:46 -0800 | [diff] [blame] | 584 | if (!dst) |
| 585 | return; |
Yuchung Cheng | 1fe4c48 | 2012-07-19 06:43:06 +0000 | [diff] [blame] | 586 | rcu_read_lock(); |
Eric Dumazet | dccf76c | 2013-11-13 15:00:46 -0800 | [diff] [blame] | 587 | tm = tcp_get_metrics(sk, dst, true); |
Yuchung Cheng | 1fe4c48 | 2012-07-19 06:43:06 +0000 | [diff] [blame] | 588 | if (tm) { |
| 589 | struct tcp_fastopen_metrics *tfom = &tm->tcpm_fastopen; |
| 590 | |
| 591 | write_seqlock_bh(&fastopen_seqlock); |
Yuchung Cheng | c968601 | 2013-10-29 10:09:05 -0700 | [diff] [blame] | 592 | if (mss) |
| 593 | tfom->mss = mss; |
| 594 | if (cookie && cookie->len > 0) |
Yuchung Cheng | 1fe4c48 | 2012-07-19 06:43:06 +0000 | [diff] [blame] | 595 | tfom->cookie = *cookie; |
Daniel Lee | 2646c83 | 2015-04-06 14:37:27 -0700 | [diff] [blame] | 596 | else if (try_exp > tfom->try_exp && |
| 597 | tfom->cookie.len <= 0 && !tfom->cookie.exp) |
| 598 | tfom->try_exp = try_exp; |
Yuchung Cheng | aab4874 | 2012-07-19 06:43:10 +0000 | [diff] [blame] | 599 | if (syn_lost) { |
| 600 | ++tfom->syn_loss; |
| 601 | tfom->last_syn_loss = jiffies; |
| 602 | } else |
| 603 | tfom->syn_loss = 0; |
Yuchung Cheng | 1fe4c48 | 2012-07-19 06:43:06 +0000 | [diff] [blame] | 604 | write_sequnlock_bh(&fastopen_seqlock); |
| 605 | } |
| 606 | rcu_read_unlock(); |
| 607 | } |
| 608 | |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 609 | static struct genl_family tcp_metrics_nl_family; |
Julian Anastasov | d23ff70 | 2012-09-04 11:03:15 +0000 | [diff] [blame] | 610 | |
stephen hemminger | 4f70c96 | 2016-08-31 15:21:37 -0700 | [diff] [blame] | 611 | static const struct nla_policy tcp_metrics_nl_policy[TCP_METRICS_ATTR_MAX + 1] = { |
Julian Anastasov | d23ff70 | 2012-09-04 11:03:15 +0000 | [diff] [blame] | 612 | [TCP_METRICS_ATTR_ADDR_IPV4] = { .type = NLA_U32, }, |
| 613 | [TCP_METRICS_ATTR_ADDR_IPV6] = { .type = NLA_BINARY, |
| 614 | .len = sizeof(struct in6_addr), }, |
| 615 | /* Following attributes are not received for GET/DEL, |
| 616 | * we keep them for reference |
| 617 | */ |
| 618 | #if 0 |
| 619 | [TCP_METRICS_ATTR_AGE] = { .type = NLA_MSECS, }, |
| 620 | [TCP_METRICS_ATTR_TW_TSVAL] = { .type = NLA_U32, }, |
| 621 | [TCP_METRICS_ATTR_TW_TS_STAMP] = { .type = NLA_S32, }, |
| 622 | [TCP_METRICS_ATTR_VALS] = { .type = NLA_NESTED, }, |
| 623 | [TCP_METRICS_ATTR_FOPEN_MSS] = { .type = NLA_U16, }, |
| 624 | [TCP_METRICS_ATTR_FOPEN_SYN_DROPS] = { .type = NLA_U16, }, |
| 625 | [TCP_METRICS_ATTR_FOPEN_SYN_DROP_TS] = { .type = NLA_MSECS, }, |
| 626 | [TCP_METRICS_ATTR_FOPEN_COOKIE] = { .type = NLA_BINARY, |
| 627 | .len = TCP_FASTOPEN_COOKIE_MAX, }, |
| 628 | #endif |
| 629 | }; |
| 630 | |
| 631 | /* Add attributes, caller cancels its header on failure */ |
| 632 | static int tcp_metrics_fill_info(struct sk_buff *msg, |
| 633 | struct tcp_metrics_block *tm) |
| 634 | { |
| 635 | struct nlattr *nest; |
| 636 | int i; |
| 637 | |
Christoph Paasch | 324fd55 | 2014-01-08 16:05:55 +0100 | [diff] [blame] | 638 | switch (tm->tcpm_daddr.family) { |
Julian Anastasov | d23ff70 | 2012-09-04 11:03:15 +0000 | [diff] [blame] | 639 | case AF_INET: |
Jiri Benc | 930345e | 2015-03-29 16:59:25 +0200 | [diff] [blame] | 640 | if (nla_put_in_addr(msg, TCP_METRICS_ATTR_ADDR_IPV4, |
David Ahern | 3abef28 | 2015-08-27 16:07:00 -0700 | [diff] [blame] | 641 | inetpeer_get_addr_v4(&tm->tcpm_daddr)) < 0) |
Julian Anastasov | d23ff70 | 2012-09-04 11:03:15 +0000 | [diff] [blame] | 642 | goto nla_put_failure; |
Jiri Benc | 930345e | 2015-03-29 16:59:25 +0200 | [diff] [blame] | 643 | if (nla_put_in_addr(msg, TCP_METRICS_ATTR_SADDR_IPV4, |
David Ahern | 3abef28 | 2015-08-27 16:07:00 -0700 | [diff] [blame] | 644 | inetpeer_get_addr_v4(&tm->tcpm_saddr)) < 0) |
Christoph Paasch | 8a59359 | 2014-01-08 16:05:57 +0100 | [diff] [blame] | 645 | goto nla_put_failure; |
Julian Anastasov | d23ff70 | 2012-09-04 11:03:15 +0000 | [diff] [blame] | 646 | break; |
| 647 | case AF_INET6: |
Jiri Benc | 930345e | 2015-03-29 16:59:25 +0200 | [diff] [blame] | 648 | if (nla_put_in6_addr(msg, TCP_METRICS_ATTR_ADDR_IPV6, |
David Ahern | 3abef28 | 2015-08-27 16:07:00 -0700 | [diff] [blame] | 649 | inetpeer_get_addr_v6(&tm->tcpm_daddr)) < 0) |
Julian Anastasov | d23ff70 | 2012-09-04 11:03:15 +0000 | [diff] [blame] | 650 | goto nla_put_failure; |
Jiri Benc | 930345e | 2015-03-29 16:59:25 +0200 | [diff] [blame] | 651 | if (nla_put_in6_addr(msg, TCP_METRICS_ATTR_SADDR_IPV6, |
David Ahern | 3abef28 | 2015-08-27 16:07:00 -0700 | [diff] [blame] | 652 | inetpeer_get_addr_v6(&tm->tcpm_saddr)) < 0) |
Christoph Paasch | 8a59359 | 2014-01-08 16:05:57 +0100 | [diff] [blame] | 653 | goto nla_put_failure; |
Julian Anastasov | d23ff70 | 2012-09-04 11:03:15 +0000 | [diff] [blame] | 654 | break; |
| 655 | default: |
| 656 | return -EAFNOSUPPORT; |
| 657 | } |
| 658 | |
| 659 | if (nla_put_msecs(msg, TCP_METRICS_ATTR_AGE, |
Nicolas Dichtel | 2175d87 | 2016-04-22 17:31:21 +0200 | [diff] [blame] | 660 | jiffies - tm->tcpm_stamp, |
| 661 | TCP_METRICS_ATTR_PAD) < 0) |
Julian Anastasov | d23ff70 | 2012-09-04 11:03:15 +0000 | [diff] [blame] | 662 | goto nla_put_failure; |
Julian Anastasov | d23ff70 | 2012-09-04 11:03:15 +0000 | [diff] [blame] | 663 | |
| 664 | { |
| 665 | int n = 0; |
| 666 | |
| 667 | nest = nla_nest_start(msg, TCP_METRICS_ATTR_VALS); |
| 668 | if (!nest) |
| 669 | goto nla_put_failure; |
Eric Dumazet | 740b0f1 | 2014-02-26 14:02:48 -0800 | [diff] [blame] | 670 | for (i = 0; i < TCP_METRIC_MAX_KERNEL + 1; i++) { |
| 671 | u32 val = tm->tcpm_vals[i]; |
| 672 | |
| 673 | if (!val) |
Julian Anastasov | d23ff70 | 2012-09-04 11:03:15 +0000 | [diff] [blame] | 674 | continue; |
Eric Dumazet | 740b0f1 | 2014-02-26 14:02:48 -0800 | [diff] [blame] | 675 | if (i == TCP_METRIC_RTT) { |
| 676 | if (nla_put_u32(msg, TCP_METRIC_RTT_US + 1, |
| 677 | val) < 0) |
| 678 | goto nla_put_failure; |
| 679 | n++; |
| 680 | val = max(val / 1000, 1U); |
| 681 | } |
| 682 | if (i == TCP_METRIC_RTTVAR) { |
| 683 | if (nla_put_u32(msg, TCP_METRIC_RTTVAR_US + 1, |
| 684 | val) < 0) |
| 685 | goto nla_put_failure; |
| 686 | n++; |
| 687 | val = max(val / 1000, 1U); |
| 688 | } |
| 689 | if (nla_put_u32(msg, i + 1, val) < 0) |
Julian Anastasov | d23ff70 | 2012-09-04 11:03:15 +0000 | [diff] [blame] | 690 | goto nla_put_failure; |
| 691 | n++; |
| 692 | } |
| 693 | if (n) |
| 694 | nla_nest_end(msg, nest); |
| 695 | else |
| 696 | nla_nest_cancel(msg, nest); |
| 697 | } |
| 698 | |
| 699 | { |
| 700 | struct tcp_fastopen_metrics tfom_copy[1], *tfom; |
| 701 | unsigned int seq; |
| 702 | |
| 703 | do { |
| 704 | seq = read_seqbegin(&fastopen_seqlock); |
| 705 | tfom_copy[0] = tm->tcpm_fastopen; |
| 706 | } while (read_seqretry(&fastopen_seqlock, seq)); |
| 707 | |
| 708 | tfom = tfom_copy; |
| 709 | if (tfom->mss && |
| 710 | nla_put_u16(msg, TCP_METRICS_ATTR_FOPEN_MSS, |
| 711 | tfom->mss) < 0) |
| 712 | goto nla_put_failure; |
| 713 | if (tfom->syn_loss && |
| 714 | (nla_put_u16(msg, TCP_METRICS_ATTR_FOPEN_SYN_DROPS, |
| 715 | tfom->syn_loss) < 0 || |
| 716 | nla_put_msecs(msg, TCP_METRICS_ATTR_FOPEN_SYN_DROP_TS, |
Nicolas Dichtel | 2175d87 | 2016-04-22 17:31:21 +0200 | [diff] [blame] | 717 | jiffies - tfom->last_syn_loss, |
| 718 | TCP_METRICS_ATTR_PAD) < 0)) |
Julian Anastasov | d23ff70 | 2012-09-04 11:03:15 +0000 | [diff] [blame] | 719 | goto nla_put_failure; |
| 720 | if (tfom->cookie.len > 0 && |
| 721 | nla_put(msg, TCP_METRICS_ATTR_FOPEN_COOKIE, |
| 722 | tfom->cookie.len, tfom->cookie.val) < 0) |
| 723 | goto nla_put_failure; |
| 724 | } |
| 725 | |
| 726 | return 0; |
| 727 | |
| 728 | nla_put_failure: |
| 729 | return -EMSGSIZE; |
| 730 | } |
| 731 | |
| 732 | static int tcp_metrics_dump_info(struct sk_buff *skb, |
| 733 | struct netlink_callback *cb, |
| 734 | struct tcp_metrics_block *tm) |
| 735 | { |
| 736 | void *hdr; |
| 737 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 738 | hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, |
Julian Anastasov | d23ff70 | 2012-09-04 11:03:15 +0000 | [diff] [blame] | 739 | &tcp_metrics_nl_family, NLM_F_MULTI, |
| 740 | TCP_METRICS_CMD_GET); |
| 741 | if (!hdr) |
| 742 | return -EMSGSIZE; |
| 743 | |
| 744 | if (tcp_metrics_fill_info(skb, tm) < 0) |
| 745 | goto nla_put_failure; |
| 746 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 747 | genlmsg_end(skb, hdr); |
| 748 | return 0; |
Julian Anastasov | d23ff70 | 2012-09-04 11:03:15 +0000 | [diff] [blame] | 749 | |
| 750 | nla_put_failure: |
| 751 | genlmsg_cancel(skb, hdr); |
| 752 | return -EMSGSIZE; |
| 753 | } |
| 754 | |
| 755 | static int tcp_metrics_nl_dump(struct sk_buff *skb, |
| 756 | struct netlink_callback *cb) |
| 757 | { |
| 758 | struct net *net = sock_net(skb->sk); |
Eric W. Biederman | 098a697 | 2015-03-13 00:07:44 -0500 | [diff] [blame] | 759 | unsigned int max_rows = 1U << tcp_metrics_hash_log; |
Julian Anastasov | d23ff70 | 2012-09-04 11:03:15 +0000 | [diff] [blame] | 760 | unsigned int row, s_row = cb->args[0]; |
| 761 | int s_col = cb->args[1], col = s_col; |
| 762 | |
| 763 | for (row = s_row; row < max_rows; row++, s_col = 0) { |
| 764 | struct tcp_metrics_block *tm; |
Eric W. Biederman | 098a697 | 2015-03-13 00:07:44 -0500 | [diff] [blame] | 765 | struct tcpm_hash_bucket *hb = tcp_metrics_hash + row; |
Julian Anastasov | d23ff70 | 2012-09-04 11:03:15 +0000 | [diff] [blame] | 766 | |
| 767 | rcu_read_lock(); |
| 768 | for (col = 0, tm = rcu_dereference(hb->chain); tm; |
| 769 | tm = rcu_dereference(tm->tcpm_next), col++) { |
Eric W. Biederman | 849e8a0 | 2015-03-13 00:05:52 -0500 | [diff] [blame] | 770 | if (!net_eq(tm_net(tm), net)) |
| 771 | continue; |
Julian Anastasov | d23ff70 | 2012-09-04 11:03:15 +0000 | [diff] [blame] | 772 | if (col < s_col) |
| 773 | continue; |
| 774 | if (tcp_metrics_dump_info(skb, cb, tm) < 0) { |
| 775 | rcu_read_unlock(); |
| 776 | goto done; |
| 777 | } |
| 778 | } |
| 779 | rcu_read_unlock(); |
| 780 | } |
| 781 | |
| 782 | done: |
| 783 | cb->args[0] = row; |
| 784 | cb->args[1] = col; |
| 785 | return skb->len; |
| 786 | } |
| 787 | |
Christoph Paasch | 3e7013d | 2014-01-08 16:05:59 +0100 | [diff] [blame] | 788 | static int __parse_nl_addr(struct genl_info *info, struct inetpeer_addr *addr, |
| 789 | unsigned int *hash, int optional, int v4, int v6) |
Julian Anastasov | d23ff70 | 2012-09-04 11:03:15 +0000 | [diff] [blame] | 790 | { |
| 791 | struct nlattr *a; |
| 792 | |
Christoph Paasch | 3e7013d | 2014-01-08 16:05:59 +0100 | [diff] [blame] | 793 | a = info->attrs[v4]; |
Julian Anastasov | d23ff70 | 2012-09-04 11:03:15 +0000 | [diff] [blame] | 794 | if (a) { |
David Ahern | 3abef28 | 2015-08-27 16:07:00 -0700 | [diff] [blame] | 795 | inetpeer_set_addr_v4(addr, nla_get_in_addr(a)); |
Christoph Paasch | 3e7013d | 2014-01-08 16:05:59 +0100 | [diff] [blame] | 796 | if (hash) |
David Ahern | 3abef28 | 2015-08-27 16:07:00 -0700 | [diff] [blame] | 797 | *hash = ipv4_addr_hash(inetpeer_get_addr_v4(addr)); |
Julian Anastasov | d23ff70 | 2012-09-04 11:03:15 +0000 | [diff] [blame] | 798 | return 0; |
| 799 | } |
Christoph Paasch | 3e7013d | 2014-01-08 16:05:59 +0100 | [diff] [blame] | 800 | a = info->attrs[v6]; |
Julian Anastasov | d23ff70 | 2012-09-04 11:03:15 +0000 | [diff] [blame] | 801 | if (a) { |
David Ahern | 3abef28 | 2015-08-27 16:07:00 -0700 | [diff] [blame] | 802 | struct in6_addr in6; |
| 803 | |
Julian Anastasov | 2c42a3f | 2012-10-30 12:03:09 +0000 | [diff] [blame] | 804 | if (nla_len(a) != sizeof(struct in6_addr)) |
Julian Anastasov | d23ff70 | 2012-09-04 11:03:15 +0000 | [diff] [blame] | 805 | return -EINVAL; |
David Ahern | 3abef28 | 2015-08-27 16:07:00 -0700 | [diff] [blame] | 806 | in6 = nla_get_in6_addr(a); |
| 807 | inetpeer_set_addr_v6(addr, &in6); |
Christoph Paasch | 3e7013d | 2014-01-08 16:05:59 +0100 | [diff] [blame] | 808 | if (hash) |
David Ahern | 3abef28 | 2015-08-27 16:07:00 -0700 | [diff] [blame] | 809 | *hash = ipv6_addr_hash(inetpeer_get_addr_v6(addr)); |
Julian Anastasov | d23ff70 | 2012-09-04 11:03:15 +0000 | [diff] [blame] | 810 | return 0; |
| 811 | } |
| 812 | return optional ? 1 : -EAFNOSUPPORT; |
| 813 | } |
| 814 | |
Christoph Paasch | 3e7013d | 2014-01-08 16:05:59 +0100 | [diff] [blame] | 815 | static int parse_nl_addr(struct genl_info *info, struct inetpeer_addr *addr, |
| 816 | unsigned int *hash, int optional) |
| 817 | { |
| 818 | return __parse_nl_addr(info, addr, hash, optional, |
| 819 | TCP_METRICS_ATTR_ADDR_IPV4, |
| 820 | TCP_METRICS_ATTR_ADDR_IPV6); |
| 821 | } |
| 822 | |
| 823 | static int parse_nl_saddr(struct genl_info *info, struct inetpeer_addr *addr) |
| 824 | { |
| 825 | return __parse_nl_addr(info, addr, NULL, 0, |
| 826 | TCP_METRICS_ATTR_SADDR_IPV4, |
| 827 | TCP_METRICS_ATTR_SADDR_IPV6); |
| 828 | } |
| 829 | |
Julian Anastasov | d23ff70 | 2012-09-04 11:03:15 +0000 | [diff] [blame] | 830 | static int tcp_metrics_nl_cmd_get(struct sk_buff *skb, struct genl_info *info) |
| 831 | { |
| 832 | struct tcp_metrics_block *tm; |
Christoph Paasch | 3e7013d | 2014-01-08 16:05:59 +0100 | [diff] [blame] | 833 | struct inetpeer_addr saddr, daddr; |
Julian Anastasov | d23ff70 | 2012-09-04 11:03:15 +0000 | [diff] [blame] | 834 | unsigned int hash; |
| 835 | struct sk_buff *msg; |
| 836 | struct net *net = genl_info_net(info); |
| 837 | void *reply; |
| 838 | int ret; |
Christoph Paasch | 3e7013d | 2014-01-08 16:05:59 +0100 | [diff] [blame] | 839 | bool src = true; |
Julian Anastasov | d23ff70 | 2012-09-04 11:03:15 +0000 | [diff] [blame] | 840 | |
Christoph Paasch | 324fd55 | 2014-01-08 16:05:55 +0100 | [diff] [blame] | 841 | ret = parse_nl_addr(info, &daddr, &hash, 0); |
Julian Anastasov | d23ff70 | 2012-09-04 11:03:15 +0000 | [diff] [blame] | 842 | if (ret < 0) |
| 843 | return ret; |
| 844 | |
Christoph Paasch | 3e7013d | 2014-01-08 16:05:59 +0100 | [diff] [blame] | 845 | ret = parse_nl_saddr(info, &saddr); |
| 846 | if (ret < 0) |
| 847 | src = false; |
| 848 | |
Julian Anastasov | d23ff70 | 2012-09-04 11:03:15 +0000 | [diff] [blame] | 849 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 850 | if (!msg) |
| 851 | return -ENOMEM; |
| 852 | |
| 853 | reply = genlmsg_put_reply(msg, info, &tcp_metrics_nl_family, 0, |
| 854 | info->genlhdr->cmd); |
| 855 | if (!reply) |
| 856 | goto nla_put_failure; |
| 857 | |
Eric W. Biederman | 3e5da62 | 2015-03-13 00:05:24 -0500 | [diff] [blame] | 858 | hash ^= net_hash_mix(net); |
Eric W. Biederman | 098a697 | 2015-03-13 00:07:44 -0500 | [diff] [blame] | 859 | hash = hash_32(hash, tcp_metrics_hash_log); |
Julian Anastasov | d23ff70 | 2012-09-04 11:03:15 +0000 | [diff] [blame] | 860 | ret = -ESRCH; |
| 861 | rcu_read_lock(); |
Eric W. Biederman | 098a697 | 2015-03-13 00:07:44 -0500 | [diff] [blame] | 862 | for (tm = rcu_dereference(tcp_metrics_hash[hash].chain); tm; |
Julian Anastasov | d23ff70 | 2012-09-04 11:03:15 +0000 | [diff] [blame] | 863 | tm = rcu_dereference(tm->tcpm_next)) { |
Christoph Paasch | 3e7013d | 2014-01-08 16:05:59 +0100 | [diff] [blame] | 864 | if (addr_same(&tm->tcpm_daddr, &daddr) && |
Eric W. Biederman | 849e8a0 | 2015-03-13 00:05:52 -0500 | [diff] [blame] | 865 | (!src || addr_same(&tm->tcpm_saddr, &saddr)) && |
| 866 | net_eq(tm_net(tm), net)) { |
Julian Anastasov | d23ff70 | 2012-09-04 11:03:15 +0000 | [diff] [blame] | 867 | ret = tcp_metrics_fill_info(msg, tm); |
| 868 | break; |
| 869 | } |
| 870 | } |
| 871 | rcu_read_unlock(); |
| 872 | if (ret < 0) |
| 873 | goto out_free; |
| 874 | |
| 875 | genlmsg_end(msg, reply); |
| 876 | return genlmsg_reply(msg, info); |
| 877 | |
| 878 | nla_put_failure: |
| 879 | ret = -EMSGSIZE; |
| 880 | |
| 881 | out_free: |
| 882 | nlmsg_free(msg); |
| 883 | return ret; |
| 884 | } |
| 885 | |
Eric W. Biederman | 8a4bff7 | 2015-03-13 00:06:43 -0500 | [diff] [blame] | 886 | static void tcp_metrics_flush_all(struct net *net) |
Julian Anastasov | d23ff70 | 2012-09-04 11:03:15 +0000 | [diff] [blame] | 887 | { |
Eric W. Biederman | 098a697 | 2015-03-13 00:07:44 -0500 | [diff] [blame] | 888 | unsigned int max_rows = 1U << tcp_metrics_hash_log; |
| 889 | struct tcpm_hash_bucket *hb = tcp_metrics_hash; |
Julian Anastasov | d23ff70 | 2012-09-04 11:03:15 +0000 | [diff] [blame] | 890 | struct tcp_metrics_block *tm; |
| 891 | unsigned int row; |
| 892 | |
| 893 | for (row = 0; row < max_rows; row++, hb++) { |
Eric W. Biederman | 04f721c | 2015-03-13 00:07:10 -0500 | [diff] [blame] | 894 | struct tcp_metrics_block __rcu **pp; |
Julian Anastasov | d23ff70 | 2012-09-04 11:03:15 +0000 | [diff] [blame] | 895 | spin_lock_bh(&tcp_metrics_lock); |
Eric W. Biederman | 04f721c | 2015-03-13 00:07:10 -0500 | [diff] [blame] | 896 | pp = &hb->chain; |
Eric Dumazet | 9f1ab18 | 2015-03-16 07:14:34 -0700 | [diff] [blame] | 897 | for (tm = deref_locked(*pp); tm; tm = deref_locked(*pp)) { |
Eric W. Biederman | 04f721c | 2015-03-13 00:07:10 -0500 | [diff] [blame] | 898 | if (net_eq(tm_net(tm), net)) { |
| 899 | *pp = tm->tcpm_next; |
| 900 | kfree_rcu(tm, rcu_head); |
| 901 | } else { |
| 902 | pp = &tm->tcpm_next; |
| 903 | } |
Julian Anastasov | d23ff70 | 2012-09-04 11:03:15 +0000 | [diff] [blame] | 904 | } |
Eric W. Biederman | 04f721c | 2015-03-13 00:07:10 -0500 | [diff] [blame] | 905 | spin_unlock_bh(&tcp_metrics_lock); |
Julian Anastasov | d23ff70 | 2012-09-04 11:03:15 +0000 | [diff] [blame] | 906 | } |
Julian Anastasov | d23ff70 | 2012-09-04 11:03:15 +0000 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | static int tcp_metrics_nl_cmd_del(struct sk_buff *skb, struct genl_info *info) |
| 910 | { |
| 911 | struct tcpm_hash_bucket *hb; |
Christoph Paasch | 00ca9c5 | 2014-01-21 13:30:26 +0100 | [diff] [blame] | 912 | struct tcp_metrics_block *tm; |
Julian Anastasov | d23ff70 | 2012-09-04 11:03:15 +0000 | [diff] [blame] | 913 | struct tcp_metrics_block __rcu **pp; |
Christoph Paasch | 3e7013d | 2014-01-08 16:05:59 +0100 | [diff] [blame] | 914 | struct inetpeer_addr saddr, daddr; |
Julian Anastasov | d23ff70 | 2012-09-04 11:03:15 +0000 | [diff] [blame] | 915 | unsigned int hash; |
| 916 | struct net *net = genl_info_net(info); |
| 917 | int ret; |
Christoph Paasch | 00ca9c5 | 2014-01-21 13:30:26 +0100 | [diff] [blame] | 918 | bool src = true, found = false; |
Julian Anastasov | d23ff70 | 2012-09-04 11:03:15 +0000 | [diff] [blame] | 919 | |
Christoph Paasch | 324fd55 | 2014-01-08 16:05:55 +0100 | [diff] [blame] | 920 | ret = parse_nl_addr(info, &daddr, &hash, 1); |
Julian Anastasov | d23ff70 | 2012-09-04 11:03:15 +0000 | [diff] [blame] | 921 | if (ret < 0) |
| 922 | return ret; |
Eric W. Biederman | 8a4bff7 | 2015-03-13 00:06:43 -0500 | [diff] [blame] | 923 | if (ret > 0) { |
| 924 | tcp_metrics_flush_all(net); |
| 925 | return 0; |
| 926 | } |
Christoph Paasch | 3e7013d | 2014-01-08 16:05:59 +0100 | [diff] [blame] | 927 | ret = parse_nl_saddr(info, &saddr); |
| 928 | if (ret < 0) |
| 929 | src = false; |
Julian Anastasov | d23ff70 | 2012-09-04 11:03:15 +0000 | [diff] [blame] | 930 | |
Eric W. Biederman | 3e5da62 | 2015-03-13 00:05:24 -0500 | [diff] [blame] | 931 | hash ^= net_hash_mix(net); |
Eric W. Biederman | 098a697 | 2015-03-13 00:07:44 -0500 | [diff] [blame] | 932 | hash = hash_32(hash, tcp_metrics_hash_log); |
| 933 | hb = tcp_metrics_hash + hash; |
Julian Anastasov | d23ff70 | 2012-09-04 11:03:15 +0000 | [diff] [blame] | 934 | pp = &hb->chain; |
| 935 | spin_lock_bh(&tcp_metrics_lock); |
Eric Dumazet | 9f1ab18 | 2015-03-16 07:14:34 -0700 | [diff] [blame] | 936 | for (tm = deref_locked(*pp); tm; tm = deref_locked(*pp)) { |
Christoph Paasch | 3e7013d | 2014-01-08 16:05:59 +0100 | [diff] [blame] | 937 | if (addr_same(&tm->tcpm_daddr, &daddr) && |
Eric W. Biederman | 849e8a0 | 2015-03-13 00:05:52 -0500 | [diff] [blame] | 938 | (!src || addr_same(&tm->tcpm_saddr, &saddr)) && |
| 939 | net_eq(tm_net(tm), net)) { |
Julian Anastasov | d23ff70 | 2012-09-04 11:03:15 +0000 | [diff] [blame] | 940 | *pp = tm->tcpm_next; |
Christoph Paasch | 00ca9c5 | 2014-01-21 13:30:26 +0100 | [diff] [blame] | 941 | kfree_rcu(tm, rcu_head); |
| 942 | found = true; |
Christoph Paasch | bbf852b | 2014-01-08 16:05:58 +0100 | [diff] [blame] | 943 | } else { |
| 944 | pp = &tm->tcpm_next; |
Julian Anastasov | d23ff70 | 2012-09-04 11:03:15 +0000 | [diff] [blame] | 945 | } |
| 946 | } |
| 947 | spin_unlock_bh(&tcp_metrics_lock); |
Christoph Paasch | 00ca9c5 | 2014-01-21 13:30:26 +0100 | [diff] [blame] | 948 | if (!found) |
Julian Anastasov | d23ff70 | 2012-09-04 11:03:15 +0000 | [diff] [blame] | 949 | return -ESRCH; |
Julian Anastasov | d23ff70 | 2012-09-04 11:03:15 +0000 | [diff] [blame] | 950 | return 0; |
| 951 | } |
| 952 | |
Johannes Berg | 4534de8 | 2013-11-14 17:14:46 +0100 | [diff] [blame] | 953 | static const struct genl_ops tcp_metrics_nl_ops[] = { |
Julian Anastasov | d23ff70 | 2012-09-04 11:03:15 +0000 | [diff] [blame] | 954 | { |
| 955 | .cmd = TCP_METRICS_CMD_GET, |
| 956 | .doit = tcp_metrics_nl_cmd_get, |
| 957 | .dumpit = tcp_metrics_nl_dump, |
| 958 | .policy = tcp_metrics_nl_policy, |
Julian Anastasov | d23ff70 | 2012-09-04 11:03:15 +0000 | [diff] [blame] | 959 | }, |
| 960 | { |
| 961 | .cmd = TCP_METRICS_CMD_DEL, |
| 962 | .doit = tcp_metrics_nl_cmd_del, |
| 963 | .policy = tcp_metrics_nl_policy, |
| 964 | .flags = GENL_ADMIN_PERM, |
| 965 | }, |
| 966 | }; |
| 967 | |
Johannes Berg | 56989f6 | 2016-10-24 14:40:05 +0200 | [diff] [blame] | 968 | static struct genl_family tcp_metrics_nl_family __ro_after_init = { |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 969 | .hdrsize = 0, |
| 970 | .name = TCP_METRICS_GENL_NAME, |
| 971 | .version = TCP_METRICS_GENL_VERSION, |
| 972 | .maxattr = TCP_METRICS_ATTR_MAX, |
| 973 | .netnsok = true, |
| 974 | .module = THIS_MODULE, |
| 975 | .ops = tcp_metrics_nl_ops, |
| 976 | .n_ops = ARRAY_SIZE(tcp_metrics_nl_ops), |
| 977 | }; |
| 978 | |
Eric Dumazet | 5815d5e | 2012-07-19 23:02:34 +0000 | [diff] [blame] | 979 | static unsigned int tcpmhash_entries; |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 980 | static int __init set_tcpmhash_entries(char *str) |
| 981 | { |
| 982 | ssize_t ret; |
| 983 | |
| 984 | if (!str) |
| 985 | return 0; |
| 986 | |
Eric Dumazet | 5815d5e | 2012-07-19 23:02:34 +0000 | [diff] [blame] | 987 | ret = kstrtouint(str, 0, &tcpmhash_entries); |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 988 | if (ret) |
| 989 | return 0; |
| 990 | |
| 991 | return 1; |
| 992 | } |
| 993 | __setup("tcpmhash_entries=", set_tcpmhash_entries); |
| 994 | |
| 995 | static int __net_init tcp_net_metrics_init(struct net *net) |
| 996 | { |
Eric Dumazet | 5815d5e | 2012-07-19 23:02:34 +0000 | [diff] [blame] | 997 | size_t size; |
| 998 | unsigned int slots; |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 999 | |
Eric W. Biederman | 098a697 | 2015-03-13 00:07:44 -0500 | [diff] [blame] | 1000 | if (!net_eq(net, &init_net)) |
| 1001 | return 0; |
| 1002 | |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 1003 | slots = tcpmhash_entries; |
| 1004 | if (!slots) { |
| 1005 | if (totalram_pages >= 128 * 1024) |
| 1006 | slots = 16 * 1024; |
| 1007 | else |
| 1008 | slots = 8 * 1024; |
| 1009 | } |
| 1010 | |
Eric W. Biederman | 098a697 | 2015-03-13 00:07:44 -0500 | [diff] [blame] | 1011 | tcp_metrics_hash_log = order_base_2(slots); |
| 1012 | size = sizeof(struct tcpm_hash_bucket) << tcp_metrics_hash_log; |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 1013 | |
Michal Hocko | 752ade6 | 2017-05-08 15:57:27 -0700 | [diff] [blame] | 1014 | tcp_metrics_hash = kvzalloc(size, GFP_KERNEL); |
Eric W. Biederman | 098a697 | 2015-03-13 00:07:44 -0500 | [diff] [blame] | 1015 | if (!tcp_metrics_hash) |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 1016 | return -ENOMEM; |
| 1017 | |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 1018 | return 0; |
| 1019 | } |
| 1020 | |
| 1021 | static void __net_exit tcp_net_metrics_exit(struct net *net) |
| 1022 | { |
Eric W. Biederman | 098a697 | 2015-03-13 00:07:44 -0500 | [diff] [blame] | 1023 | tcp_metrics_flush_all(net); |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | static __net_initdata struct pernet_operations tcp_net_metrics_ops = { |
| 1027 | .init = tcp_net_metrics_init, |
| 1028 | .exit = tcp_net_metrics_exit, |
| 1029 | }; |
| 1030 | |
| 1031 | void __init tcp_metrics_init(void) |
| 1032 | { |
Julian Anastasov | d23ff70 | 2012-09-04 11:03:15 +0000 | [diff] [blame] | 1033 | int ret; |
| 1034 | |
| 1035 | ret = register_pernet_subsys(&tcp_net_metrics_ops); |
| 1036 | if (ret < 0) |
Eric W. Biederman | 6493517 | 2015-03-13 00:04:51 -0500 | [diff] [blame] | 1037 | panic("Could not allocate the tcp_metrics hash table\n"); |
| 1038 | |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 1039 | ret = genl_register_family(&tcp_metrics_nl_family); |
Julian Anastasov | d23ff70 | 2012-09-04 11:03:15 +0000 | [diff] [blame] | 1040 | if (ret < 0) |
Eric W. Biederman | 6493517 | 2015-03-13 00:04:51 -0500 | [diff] [blame] | 1041 | panic("Could not register tcp_metrics generic netlink\n"); |
David S. Miller | 51c5d0c | 2012-07-10 00:49:14 -0700 | [diff] [blame] | 1042 | } |