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