blob: fbb42f44501e59bf8814eb9224c3b5671c59293e [file] [log] [blame]
David S. Miller51c5d0c2012-07-10 00:49:14 -07001#include <linux/rcupdate.h>
2#include <linux/spinlock.h>
3#include <linux/jiffies.h>
David S. Millerab92bb22012-07-09 16:19:30 -07004#include <linux/module.h>
David S. Miller4aabd8e2012-07-09 16:07:30 -07005#include <linux/cache.h>
David S. Miller51c5d0c2012-07-10 00:49:14 -07006#include <linux/slab.h>
7#include <linux/init.h>
David S. Miller4aabd8e2012-07-09 16:07:30 -07008#include <linux/tcp.h>
Eric Dumazet5815d5e2012-07-19 23:02:34 +00009#include <linux/hash.h>
Julian Anastasovd23ff702012-09-04 11:03:15 +000010#include <linux/tcp_metrics.h>
Eric Dumazet976a7022012-11-16 05:31:53 +000011#include <linux/vmalloc.h>
David S. Miller4aabd8e2012-07-09 16:07:30 -070012
13#include <net/inet_connection_sock.h>
David S. Miller51c5d0c2012-07-10 00:49:14 -070014#include <net/net_namespace.h>
David S. Millerab92bb22012-07-09 16:19:30 -070015#include <net/request_sock.h>
David S. Miller51c5d0c2012-07-10 00:49:14 -070016#include <net/inetpeer.h>
David S. Miller4aabd8e2012-07-09 16:07:30 -070017#include <net/sock.h>
David S. Miller51c5d0c2012-07-10 00:49:14 -070018#include <net/ipv6.h>
David S. Miller4aabd8e2012-07-09 16:07:30 -070019#include <net/dst.h>
20#include <net/tcp.h>
Julian Anastasovd23ff702012-09-04 11:03:15 +000021#include <net/genetlink.h>
David S. Miller4aabd8e2012-07-09 16:07:30 -070022
23int sysctl_tcp_nometrics_save __read_mostly;
24
David S. Miller41804422014-01-18 00:55:41 -080025static struct tcp_metrics_block *__tcp_get_metrics(const struct inetpeer_addr *saddr,
26 const struct inetpeer_addr *daddr,
Christoph Paasch77f99ad2014-01-16 20:01:21 +010027 struct net *net, unsigned int hash);
28
Yuchung Cheng1fe4c482012-07-19 06:43:06 +000029struct tcp_fastopen_metrics {
30 u16 mss;
Yuchung Chengaab48742012-07-19 06:43:10 +000031 u16 syn_loss:10; /* Recurring Fast Open SYN losses */
32 unsigned long last_syn_loss; /* Last Fast Open SYN loss */
Yuchung Cheng1fe4c482012-07-19 06:43:06 +000033 struct tcp_fastopen_cookie cookie;
34};
35
Eric Dumazet740b0f12014-02-26 14:02:48 -080036/* 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. Miller51c5d0c2012-07-10 00:49:14 -070041struct tcp_metrics_block {
42 struct tcp_metrics_block __rcu *tcpm_next;
Christoph Paascha5443022014-01-08 16:05:56 +010043 struct inetpeer_addr tcpm_saddr;
Christoph Paasch324fd552014-01-08 16:05:55 +010044 struct inetpeer_addr tcpm_daddr;
David S. Miller51c5d0c2012-07-10 00:49:14 -070045 unsigned long tcpm_stamp;
David S. Miller81166dd2012-07-10 03:14:24 -070046 u32 tcpm_ts;
47 u32 tcpm_ts_stamp;
David S. Miller51c5d0c2012-07-10 00:49:14 -070048 u32 tcpm_lock;
Eric Dumazet740b0f12014-02-26 14:02:48 -080049 u32 tcpm_vals[TCP_METRIC_MAX_KERNEL + 1];
Yuchung Cheng1fe4c482012-07-19 06:43:06 +000050 struct tcp_fastopen_metrics tcpm_fastopen;
Julian Anastasovd23ff702012-09-04 11:03:15 +000051
52 struct rcu_head rcu_head;
David S. Miller51c5d0c2012-07-10 00:49:14 -070053};
54
55static bool tcp_metric_locked(struct tcp_metrics_block *tm,
56 enum tcp_metric_index idx)
57{
58 return tm->tcpm_lock & (1 << idx);
59}
60
61static u32 tcp_metric_get(struct tcp_metrics_block *tm,
62 enum tcp_metric_index idx)
63{
64 return tm->tcpm_vals[idx];
65}
66
David S. Miller51c5d0c2012-07-10 00:49:14 -070067static void tcp_metric_set(struct tcp_metrics_block *tm,
68 enum tcp_metric_index idx,
69 u32 val)
70{
71 tm->tcpm_vals[idx] = val;
72}
73
David S. Miller51c5d0c2012-07-10 00:49:14 -070074static bool addr_same(const struct inetpeer_addr *a,
75 const struct inetpeer_addr *b)
76{
77 const struct in6_addr *a6, *b6;
78
79 if (a->family != b->family)
80 return false;
81 if (a->family == AF_INET)
82 return a->addr.a4 == b->addr.a4;
83
84 a6 = (const struct in6_addr *) &a->addr.a6[0];
85 b6 = (const struct in6_addr *) &b->addr.a6[0];
86
87 return ipv6_addr_equal(a6, b6);
88}
89
90struct tcpm_hash_bucket {
91 struct tcp_metrics_block __rcu *chain;
92};
93
94static DEFINE_SPINLOCK(tcp_metrics_lock);
95
Eric Dumazet740b0f12014-02-26 14:02:48 -080096static void tcpm_suck_dst(struct tcp_metrics_block *tm,
97 const struct dst_entry *dst,
Eric Dumazetefeaa552013-05-03 19:12:45 +000098 bool fastopen_clear)
David S. Miller51c5d0c2012-07-10 00:49:14 -070099{
Eric Dumazet740b0f12014-02-26 14:02:48 -0800100 u32 msval;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700101 u32 val;
102
Julian Anastasov9a0a9502012-07-23 10:46:38 +0300103 tm->tcpm_stamp = jiffies;
104
David S. Miller51c5d0c2012-07-10 00:49:14 -0700105 val = 0;
106 if (dst_metric_locked(dst, RTAX_RTT))
107 val |= 1 << TCP_METRIC_RTT;
108 if (dst_metric_locked(dst, RTAX_RTTVAR))
109 val |= 1 << TCP_METRIC_RTTVAR;
110 if (dst_metric_locked(dst, RTAX_SSTHRESH))
111 val |= 1 << TCP_METRIC_SSTHRESH;
112 if (dst_metric_locked(dst, RTAX_CWND))
113 val |= 1 << TCP_METRIC_CWND;
114 if (dst_metric_locked(dst, RTAX_REORDERING))
115 val |= 1 << TCP_METRIC_REORDERING;
116 tm->tcpm_lock = val;
117
Eric Dumazet740b0f12014-02-26 14:02:48 -0800118 msval = dst_metric_raw(dst, RTAX_RTT);
119 tm->tcpm_vals[TCP_METRIC_RTT] = msval * USEC_PER_MSEC;
120
121 msval = dst_metric_raw(dst, RTAX_RTTVAR);
122 tm->tcpm_vals[TCP_METRIC_RTTVAR] = msval * USEC_PER_MSEC;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700123 tm->tcpm_vals[TCP_METRIC_SSTHRESH] = dst_metric_raw(dst, RTAX_SSTHRESH);
124 tm->tcpm_vals[TCP_METRIC_CWND] = dst_metric_raw(dst, RTAX_CWND);
125 tm->tcpm_vals[TCP_METRIC_REORDERING] = dst_metric_raw(dst, RTAX_REORDERING);
David S. Miller81166dd2012-07-10 03:14:24 -0700126 tm->tcpm_ts = 0;
127 tm->tcpm_ts_stamp = 0;
Eric Dumazetefeaa552013-05-03 19:12:45 +0000128 if (fastopen_clear) {
129 tm->tcpm_fastopen.mss = 0;
130 tm->tcpm_fastopen.syn_loss = 0;
131 tm->tcpm_fastopen.cookie.len = 0;
132 }
David S. Miller51c5d0c2012-07-10 00:49:14 -0700133}
134
Christoph Paasch77f99ad2014-01-16 20:01:21 +0100135#define TCP_METRICS_TIMEOUT (60 * 60 * HZ)
136
137static void tcpm_check_stamp(struct tcp_metrics_block *tm, struct dst_entry *dst)
138{
139 if (tm && unlikely(time_after(jiffies, tm->tcpm_stamp + TCP_METRICS_TIMEOUT)))
140 tcpm_suck_dst(tm, dst, false);
141}
142
143#define TCP_METRICS_RECLAIM_DEPTH 5
144#define TCP_METRICS_RECLAIM_PTR (struct tcp_metrics_block *) 0x1UL
145
David S. Miller51c5d0c2012-07-10 00:49:14 -0700146static struct tcp_metrics_block *tcpm_new(struct dst_entry *dst,
Christoph Paascha5443022014-01-08 16:05:56 +0100147 struct inetpeer_addr *saddr,
Christoph Paasch324fd552014-01-08 16:05:55 +0100148 struct inetpeer_addr *daddr,
Christoph Paasch77f99ad2014-01-16 20:01:21 +0100149 unsigned int hash)
David S. Miller51c5d0c2012-07-10 00:49:14 -0700150{
151 struct tcp_metrics_block *tm;
152 struct net *net;
Christoph Paasch77f99ad2014-01-16 20:01:21 +0100153 bool reclaim = false;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700154
155 spin_lock_bh(&tcp_metrics_lock);
156 net = dev_net(dst->dev);
Christoph Paasch77f99ad2014-01-16 20:01:21 +0100157
158 /* While waiting for the spin-lock the cache might have been populated
159 * with this entry and so we have to check again.
160 */
David S. Miller41804422014-01-18 00:55:41 -0800161 tm = __tcp_get_metrics(saddr, daddr, net, hash);
Christoph Paasch77f99ad2014-01-16 20:01:21 +0100162 if (tm == TCP_METRICS_RECLAIM_PTR) {
163 reclaim = true;
164 tm = NULL;
165 }
166 if (tm) {
167 tcpm_check_stamp(tm, dst);
168 goto out_unlock;
169 }
170
David S. Miller51c5d0c2012-07-10 00:49:14 -0700171 if (unlikely(reclaim)) {
172 struct tcp_metrics_block *oldest;
173
174 oldest = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain);
175 for (tm = rcu_dereference(oldest->tcpm_next); tm;
176 tm = rcu_dereference(tm->tcpm_next)) {
177 if (time_before(tm->tcpm_stamp, oldest->tcpm_stamp))
178 oldest = tm;
179 }
180 tm = oldest;
181 } else {
182 tm = kmalloc(sizeof(*tm), GFP_ATOMIC);
183 if (!tm)
184 goto out_unlock;
185 }
Christoph Paascha5443022014-01-08 16:05:56 +0100186 tm->tcpm_saddr = *saddr;
Christoph Paasch324fd552014-01-08 16:05:55 +0100187 tm->tcpm_daddr = *daddr;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700188
Eric Dumazetefeaa552013-05-03 19:12:45 +0000189 tcpm_suck_dst(tm, dst, true);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700190
191 if (likely(!reclaim)) {
192 tm->tcpm_next = net->ipv4.tcp_metrics_hash[hash].chain;
193 rcu_assign_pointer(net->ipv4.tcp_metrics_hash[hash].chain, tm);
194 }
195
196out_unlock:
197 spin_unlock_bh(&tcp_metrics_lock);
198 return tm;
199}
200
David S. Miller51c5d0c2012-07-10 00:49:14 -0700201static struct tcp_metrics_block *tcp_get_encode(struct tcp_metrics_block *tm, int depth)
202{
203 if (tm)
204 return tm;
205 if (depth > TCP_METRICS_RECLAIM_DEPTH)
206 return TCP_METRICS_RECLAIM_PTR;
207 return NULL;
208}
209
Christoph Paascha5443022014-01-08 16:05:56 +0100210static struct tcp_metrics_block *__tcp_get_metrics(const struct inetpeer_addr *saddr,
211 const struct inetpeer_addr *daddr,
David S. Miller51c5d0c2012-07-10 00:49:14 -0700212 struct net *net, unsigned int hash)
213{
214 struct tcp_metrics_block *tm;
215 int depth = 0;
216
217 for (tm = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain); tm;
218 tm = rcu_dereference(tm->tcpm_next)) {
Christoph Paascha5443022014-01-08 16:05:56 +0100219 if (addr_same(&tm->tcpm_saddr, saddr) &&
220 addr_same(&tm->tcpm_daddr, daddr))
David S. Miller51c5d0c2012-07-10 00:49:14 -0700221 break;
222 depth++;
223 }
224 return tcp_get_encode(tm, depth);
225}
226
227static struct tcp_metrics_block *__tcp_get_metrics_req(struct request_sock *req,
228 struct dst_entry *dst)
229{
230 struct tcp_metrics_block *tm;
Christoph Paascha5443022014-01-08 16:05:56 +0100231 struct inetpeer_addr saddr, daddr;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700232 unsigned int hash;
233 struct net *net;
234
Christoph Paascha5443022014-01-08 16:05:56 +0100235 saddr.family = req->rsk_ops->family;
Christoph Paasch324fd552014-01-08 16:05:55 +0100236 daddr.family = req->rsk_ops->family;
237 switch (daddr.family) {
David S. Miller51c5d0c2012-07-10 00:49:14 -0700238 case AF_INET:
Christoph Paascha5443022014-01-08 16:05:56 +0100239 saddr.addr.a4 = inet_rsk(req)->ir_loc_addr;
Christoph Paasch324fd552014-01-08 16:05:55 +0100240 daddr.addr.a4 = inet_rsk(req)->ir_rmt_addr;
241 hash = (__force unsigned int) daddr.addr.a4;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700242 break;
Eric Dumazet634fb9792013-10-09 15:21:29 -0700243#if IS_ENABLED(CONFIG_IPV6)
David S. Miller51c5d0c2012-07-10 00:49:14 -0700244 case AF_INET6:
Christoph Paascha5443022014-01-08 16:05:56 +0100245 *(struct in6_addr *)saddr.addr.a6 = inet_rsk(req)->ir_v6_loc_addr;
Christoph Paasch324fd552014-01-08 16:05:55 +0100246 *(struct in6_addr *)daddr.addr.a6 = inet_rsk(req)->ir_v6_rmt_addr;
Eric Dumazet634fb9792013-10-09 15:21:29 -0700247 hash = ipv6_addr_hash(&inet_rsk(req)->ir_v6_rmt_addr);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700248 break;
Eric Dumazet634fb9792013-10-09 15:21:29 -0700249#endif
David S. Miller51c5d0c2012-07-10 00:49:14 -0700250 default:
251 return NULL;
252 }
253
David S. Miller51c5d0c2012-07-10 00:49:14 -0700254 net = dev_net(dst->dev);
Eric W. Biederman3e5da622015-03-13 00:05:24 -0500255 hash ^= net_hash_mix(net);
Eric Dumazet5815d5e2012-07-19 23:02:34 +0000256 hash = hash_32(hash, net->ipv4.tcp_metrics_hash_log);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700257
258 for (tm = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain); tm;
259 tm = rcu_dereference(tm->tcpm_next)) {
Christoph Paascha5443022014-01-08 16:05:56 +0100260 if (addr_same(&tm->tcpm_saddr, &saddr) &&
261 addr_same(&tm->tcpm_daddr, &daddr))
David S. Miller51c5d0c2012-07-10 00:49:14 -0700262 break;
263 }
264 tcpm_check_stamp(tm, dst);
265 return tm;
266}
267
David S. Miller81166dd2012-07-10 03:14:24 -0700268static struct tcp_metrics_block *__tcp_get_metrics_tw(struct inet_timewait_sock *tw)
269{
David S. Miller81166dd2012-07-10 03:14:24 -0700270 struct tcp_metrics_block *tm;
Christoph Paascha5443022014-01-08 16:05:56 +0100271 struct inetpeer_addr saddr, daddr;
David S. Miller81166dd2012-07-10 03:14:24 -0700272 unsigned int hash;
273 struct net *net;
274
Christoph Paasch3ad88cf2014-01-22 13:58:44 +0100275 if (tw->tw_family == AF_INET) {
276 saddr.family = AF_INET;
Christoph Paascha5443022014-01-08 16:05:56 +0100277 saddr.addr.a4 = tw->tw_rcv_saddr;
Christoph Paasch3ad88cf2014-01-22 13:58:44 +0100278 daddr.family = AF_INET;
Christoph Paasch324fd552014-01-08 16:05:55 +0100279 daddr.addr.a4 = tw->tw_daddr;
280 hash = (__force unsigned int) daddr.addr.a4;
David S. Miller81166dd2012-07-10 03:14:24 -0700281 }
Christoph Paasch3ad88cf2014-01-22 13:58:44 +0100282#if IS_ENABLED(CONFIG_IPV6)
283 else if (tw->tw_family == AF_INET6) {
284 if (ipv6_addr_v4mapped(&tw->tw_v6_daddr)) {
285 saddr.family = AF_INET;
286 saddr.addr.a4 = tw->tw_rcv_saddr;
287 daddr.family = AF_INET;
288 daddr.addr.a4 = tw->tw_daddr;
289 hash = (__force unsigned int) daddr.addr.a4;
290 } else {
291 saddr.family = AF_INET6;
292 *(struct in6_addr *)saddr.addr.a6 = tw->tw_v6_rcv_saddr;
293 daddr.family = AF_INET6;
294 *(struct in6_addr *)daddr.addr.a6 = tw->tw_v6_daddr;
295 hash = ipv6_addr_hash(&tw->tw_v6_daddr);
296 }
297 }
298#endif
299 else
300 return NULL;
David S. Miller81166dd2012-07-10 03:14:24 -0700301
David S. Miller81166dd2012-07-10 03:14:24 -0700302 net = twsk_net(tw);
Eric W. Biederman3e5da622015-03-13 00:05:24 -0500303 hash ^= net_hash_mix(net);
Eric Dumazet5815d5e2012-07-19 23:02:34 +0000304 hash = hash_32(hash, net->ipv4.tcp_metrics_hash_log);
David S. Miller81166dd2012-07-10 03:14:24 -0700305
306 for (tm = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain); tm;
307 tm = rcu_dereference(tm->tcpm_next)) {
Christoph Paascha5443022014-01-08 16:05:56 +0100308 if (addr_same(&tm->tcpm_saddr, &saddr) &&
309 addr_same(&tm->tcpm_daddr, &daddr))
David S. Miller81166dd2012-07-10 03:14:24 -0700310 break;
311 }
312 return tm;
313}
314
David S. Miller51c5d0c2012-07-10 00:49:14 -0700315static struct tcp_metrics_block *tcp_get_metrics(struct sock *sk,
316 struct dst_entry *dst,
317 bool create)
318{
319 struct tcp_metrics_block *tm;
Christoph Paascha5443022014-01-08 16:05:56 +0100320 struct inetpeer_addr saddr, daddr;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700321 unsigned int hash;
322 struct net *net;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700323
Christoph Paasch3ad88cf2014-01-22 13:58:44 +0100324 if (sk->sk_family == AF_INET) {
325 saddr.family = AF_INET;
Christoph Paascha5443022014-01-08 16:05:56 +0100326 saddr.addr.a4 = inet_sk(sk)->inet_saddr;
Christoph Paasch3ad88cf2014-01-22 13:58:44 +0100327 daddr.family = AF_INET;
Christoph Paasch324fd552014-01-08 16:05:55 +0100328 daddr.addr.a4 = inet_sk(sk)->inet_daddr;
329 hash = (__force unsigned int) daddr.addr.a4;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700330 }
Christoph Paasch3ad88cf2014-01-22 13:58:44 +0100331#if IS_ENABLED(CONFIG_IPV6)
332 else if (sk->sk_family == AF_INET6) {
333 if (ipv6_addr_v4mapped(&sk->sk_v6_daddr)) {
334 saddr.family = AF_INET;
335 saddr.addr.a4 = inet_sk(sk)->inet_saddr;
336 daddr.family = AF_INET;
337 daddr.addr.a4 = inet_sk(sk)->inet_daddr;
338 hash = (__force unsigned int) daddr.addr.a4;
339 } else {
340 saddr.family = AF_INET6;
341 *(struct in6_addr *)saddr.addr.a6 = sk->sk_v6_rcv_saddr;
342 daddr.family = AF_INET6;
343 *(struct in6_addr *)daddr.addr.a6 = sk->sk_v6_daddr;
344 hash = ipv6_addr_hash(&sk->sk_v6_daddr);
345 }
346 }
347#endif
348 else
349 return NULL;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700350
David S. Miller51c5d0c2012-07-10 00:49:14 -0700351 net = dev_net(dst->dev);
Eric W. Biederman3e5da622015-03-13 00:05:24 -0500352 hash ^= net_hash_mix(net);
Eric Dumazet5815d5e2012-07-19 23:02:34 +0000353 hash = hash_32(hash, net->ipv4.tcp_metrics_hash_log);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700354
Christoph Paascha5443022014-01-08 16:05:56 +0100355 tm = __tcp_get_metrics(&saddr, &daddr, net, hash);
Christoph Paasch77f99ad2014-01-16 20:01:21 +0100356 if (tm == TCP_METRICS_RECLAIM_PTR)
David S. Miller51c5d0c2012-07-10 00:49:14 -0700357 tm = NULL;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700358 if (!tm && create)
David S. Miller41804422014-01-18 00:55:41 -0800359 tm = tcpm_new(dst, &saddr, &daddr, hash);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700360 else
361 tcpm_check_stamp(tm, dst);
362
363 return tm;
364}
365
David S. Miller4aabd8e2012-07-09 16:07:30 -0700366/* Save metrics learned by this TCP session. This function is called
367 * only, when TCP finishes successfully i.e. when it enters TIME-WAIT
368 * or goes from LAST-ACK to CLOSE.
369 */
370void tcp_update_metrics(struct sock *sk)
371{
David S. Miller51c5d0c2012-07-10 00:49:14 -0700372 const struct inet_connection_sock *icsk = inet_csk(sk);
David S. Miller4aabd8e2012-07-09 16:07:30 -0700373 struct dst_entry *dst = __sk_dst_get(sk);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700374 struct tcp_sock *tp = tcp_sk(sk);
375 struct tcp_metrics_block *tm;
376 unsigned long rtt;
377 u32 val;
378 int m;
David S. Miller4aabd8e2012-07-09 16:07:30 -0700379
David S. Miller51c5d0c2012-07-10 00:49:14 -0700380 if (sysctl_tcp_nometrics_save || !dst)
David S. Miller4aabd8e2012-07-09 16:07:30 -0700381 return;
382
David S. Miller51c5d0c2012-07-10 00:49:14 -0700383 if (dst->flags & DST_HOST)
David S. Miller4aabd8e2012-07-09 16:07:30 -0700384 dst_confirm(dst);
385
David S. Miller51c5d0c2012-07-10 00:49:14 -0700386 rcu_read_lock();
Eric Dumazet740b0f12014-02-26 14:02:48 -0800387 if (icsk->icsk_backoff || !tp->srtt_us) {
David S. Miller51c5d0c2012-07-10 00:49:14 -0700388 /* This session failed to estimate rtt. Why?
389 * Probably, no packets returned in time. Reset our
390 * results.
David S. Miller4aabd8e2012-07-09 16:07:30 -0700391 */
David S. Miller51c5d0c2012-07-10 00:49:14 -0700392 tm = tcp_get_metrics(sk, dst, false);
393 if (tm && !tcp_metric_locked(tm, TCP_METRIC_RTT))
394 tcp_metric_set(tm, TCP_METRIC_RTT, 0);
395 goto out_unlock;
396 } else
397 tm = tcp_get_metrics(sk, dst, true);
398
399 if (!tm)
400 goto out_unlock;
401
Eric Dumazet740b0f12014-02-26 14:02:48 -0800402 rtt = tcp_metric_get(tm, TCP_METRIC_RTT);
403 m = rtt - tp->srtt_us;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700404
405 /* If newly calculated rtt larger than stored one, store new
406 * one. Otherwise, use EWMA. Remember, rtt overestimation is
407 * always better than underestimation.
408 */
409 if (!tcp_metric_locked(tm, TCP_METRIC_RTT)) {
410 if (m <= 0)
Eric Dumazet740b0f12014-02-26 14:02:48 -0800411 rtt = tp->srtt_us;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700412 else
413 rtt -= (m >> 3);
Eric Dumazet740b0f12014-02-26 14:02:48 -0800414 tcp_metric_set(tm, TCP_METRIC_RTT, rtt);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700415 }
416
417 if (!tcp_metric_locked(tm, TCP_METRIC_RTTVAR)) {
418 unsigned long var;
419
420 if (m < 0)
421 m = -m;
422
423 /* Scale deviation to rttvar fixed point */
424 m >>= 1;
Eric Dumazet740b0f12014-02-26 14:02:48 -0800425 if (m < tp->mdev_us)
426 m = tp->mdev_us;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700427
Eric Dumazet740b0f12014-02-26 14:02:48 -0800428 var = tcp_metric_get(tm, TCP_METRIC_RTTVAR);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700429 if (m >= var)
430 var = m;
431 else
432 var -= (var - m) >> 2;
433
Eric Dumazet740b0f12014-02-26 14:02:48 -0800434 tcp_metric_set(tm, TCP_METRIC_RTTVAR, var);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700435 }
436
437 if (tcp_in_initial_slowstart(tp)) {
438 /* Slow start still did not finish. */
439 if (!tcp_metric_locked(tm, TCP_METRIC_SSTHRESH)) {
440 val = tcp_metric_get(tm, TCP_METRIC_SSTHRESH);
441 if (val && (tp->snd_cwnd >> 1) > val)
442 tcp_metric_set(tm, TCP_METRIC_SSTHRESH,
443 tp->snd_cwnd >> 1);
David S. Miller4aabd8e2012-07-09 16:07:30 -0700444 }
David S. Miller51c5d0c2012-07-10 00:49:14 -0700445 if (!tcp_metric_locked(tm, TCP_METRIC_CWND)) {
446 val = tcp_metric_get(tm, TCP_METRIC_CWND);
447 if (tp->snd_cwnd > val)
448 tcp_metric_set(tm, TCP_METRIC_CWND,
449 tp->snd_cwnd);
David S. Miller4aabd8e2012-07-09 16:07:30 -0700450 }
David S. Miller51c5d0c2012-07-10 00:49:14 -0700451 } else if (tp->snd_cwnd > tp->snd_ssthresh &&
452 icsk->icsk_ca_state == TCP_CA_Open) {
453 /* Cong. avoidance phase, cwnd is reliable. */
454 if (!tcp_metric_locked(tm, TCP_METRIC_SSTHRESH))
455 tcp_metric_set(tm, TCP_METRIC_SSTHRESH,
456 max(tp->snd_cwnd >> 1, tp->snd_ssthresh));
457 if (!tcp_metric_locked(tm, TCP_METRIC_CWND)) {
458 val = tcp_metric_get(tm, TCP_METRIC_CWND);
Alexander Duyck21008442012-07-11 17:18:04 -0700459 tcp_metric_set(tm, TCP_METRIC_CWND, (val + tp->snd_cwnd) >> 1);
David S. Miller4aabd8e2012-07-09 16:07:30 -0700460 }
David S. Miller51c5d0c2012-07-10 00:49:14 -0700461 } else {
462 /* Else slow start did not finish, cwnd is non-sense,
463 * ssthresh may be also invalid.
464 */
465 if (!tcp_metric_locked(tm, TCP_METRIC_CWND)) {
466 val = tcp_metric_get(tm, TCP_METRIC_CWND);
467 tcp_metric_set(tm, TCP_METRIC_CWND,
468 (val + tp->snd_ssthresh) >> 1);
469 }
470 if (!tcp_metric_locked(tm, TCP_METRIC_SSTHRESH)) {
471 val = tcp_metric_get(tm, TCP_METRIC_SSTHRESH);
472 if (val && tp->snd_ssthresh > val)
473 tcp_metric_set(tm, TCP_METRIC_SSTHRESH,
474 tp->snd_ssthresh);
475 }
476 if (!tcp_metric_locked(tm, TCP_METRIC_REORDERING)) {
477 val = tcp_metric_get(tm, TCP_METRIC_REORDERING);
478 if (val < tp->reordering &&
David S. Miller4aabd8e2012-07-09 16:07:30 -0700479 tp->reordering != sysctl_tcp_reordering)
David S. Miller51c5d0c2012-07-10 00:49:14 -0700480 tcp_metric_set(tm, TCP_METRIC_REORDERING,
481 tp->reordering);
David S. Miller4aabd8e2012-07-09 16:07:30 -0700482 }
483 }
David S. Miller51c5d0c2012-07-10 00:49:14 -0700484 tm->tcpm_stamp = jiffies;
485out_unlock:
486 rcu_read_unlock();
David S. Miller4aabd8e2012-07-09 16:07:30 -0700487}
488
489/* Initialize metrics on socket. */
490
491void tcp_init_metrics(struct sock *sk)
492{
David S. Miller4aabd8e2012-07-09 16:07:30 -0700493 struct dst_entry *dst = __sk_dst_get(sk);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700494 struct tcp_sock *tp = tcp_sk(sk);
495 struct tcp_metrics_block *tm;
Yuchung Cheng1b7fdd22013-08-30 08:35:53 -0700496 u32 val, crtt = 0; /* cached RTT scaled by 8 */
David S. Miller4aabd8e2012-07-09 16:07:30 -0700497
498 if (dst == NULL)
499 goto reset;
500
501 dst_confirm(dst);
502
David S. Miller51c5d0c2012-07-10 00:49:14 -0700503 rcu_read_lock();
504 tm = tcp_get_metrics(sk, dst, true);
505 if (!tm) {
506 rcu_read_unlock();
507 goto reset;
508 }
509
510 if (tcp_metric_locked(tm, TCP_METRIC_CWND))
511 tp->snd_cwnd_clamp = tcp_metric_get(tm, TCP_METRIC_CWND);
512
513 val = tcp_metric_get(tm, TCP_METRIC_SSTHRESH);
514 if (val) {
515 tp->snd_ssthresh = val;
David S. Miller4aabd8e2012-07-09 16:07:30 -0700516 if (tp->snd_ssthresh > tp->snd_cwnd_clamp)
517 tp->snd_ssthresh = tp->snd_cwnd_clamp;
518 } else {
519 /* ssthresh may have been reduced unnecessarily during.
520 * 3WHS. Restore it back to its initial default.
521 */
522 tp->snd_ssthresh = TCP_INFINITE_SSTHRESH;
523 }
David S. Miller51c5d0c2012-07-10 00:49:14 -0700524 val = tcp_metric_get(tm, TCP_METRIC_REORDERING);
525 if (val && tp->reordering != val) {
David S. Miller4aabd8e2012-07-09 16:07:30 -0700526 tcp_disable_fack(tp);
527 tcp_disable_early_retrans(tp);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700528 tp->reordering = val;
David S. Miller4aabd8e2012-07-09 16:07:30 -0700529 }
530
Eric Dumazet740b0f12014-02-26 14:02:48 -0800531 crtt = tcp_metric_get(tm, TCP_METRIC_RTT);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700532 rcu_read_unlock();
David S. Miller4aabd8e2012-07-09 16:07:30 -0700533reset:
Yuchung Cheng52f20e62013-09-03 14:14:35 -0700534 /* The initial RTT measurement from the SYN/SYN-ACK is not ideal
535 * to seed the RTO for later data packets because SYN packets are
536 * small. Use the per-dst cached values to seed the RTO but keep
537 * the RTT estimator variables intact (e.g., srtt, mdev, rttvar).
538 * Later the RTO will be updated immediately upon obtaining the first
539 * data RTT sample (tcp_rtt_estimator()). Hence the cached RTT only
540 * influences the first RTO but not later RTT estimation.
541 *
542 * But if RTT is not available from the SYN (due to retransmits or
543 * syn cookies) or the cache, force a conservative 3secs timeout.
544 *
545 * A bit of theory. RTT is time passed after "normal" sized packet
546 * is sent until it is ACKed. In normal circumstances sending small
547 * packets force peer to delay ACKs and calculation is correct too.
548 * The algorithm is adaptive and, provided we follow specs, it
549 * NEVER underestimate RTT. BUT! If peer tries to make some clever
550 * tricks sort of "quick acks" for time long enough to decrease RTT
551 * to low value, and then abruptly stops to do it and starts to delay
552 * ACKs, wait for troubles.
553 */
Eric Dumazet740b0f12014-02-26 14:02:48 -0800554 if (crtt > tp->srtt_us) {
Neal Cardwell269aa752013-09-16 21:44:20 -0400555 /* Set RTO like tcp_rtt_estimator(), but from cached RTT. */
Eric Dumazet740b0f12014-02-26 14:02:48 -0800556 crtt /= 8 * USEC_PER_MSEC;
Neal Cardwell269aa752013-09-16 21:44:20 -0400557 inet_csk(sk)->icsk_rto = crtt + max(2 * crtt, tcp_rto_min(sk));
Eric Dumazet740b0f12014-02-26 14:02:48 -0800558 } else if (tp->srtt_us == 0) {
David S. Miller4aabd8e2012-07-09 16:07:30 -0700559 /* RFC6298: 5.7 We've failed to get a valid RTT sample from
560 * 3WHS. This is most likely due to retransmission,
561 * including spurious one. Reset the RTO back to 3secs
562 * from the more aggressive 1sec to avoid more spurious
563 * retransmission.
564 */
Eric Dumazet740b0f12014-02-26 14:02:48 -0800565 tp->rttvar_us = jiffies_to_usecs(TCP_TIMEOUT_FALLBACK);
566 tp->mdev_us = tp->mdev_max_us = tp->rttvar_us;
567
David S. Miller4aabd8e2012-07-09 16:07:30 -0700568 inet_csk(sk)->icsk_rto = TCP_TIMEOUT_FALLBACK;
569 }
570 /* Cut cwnd down to 1 per RFC5681 if SYN or SYN-ACK has been
571 * retransmitted. In light of RFC6298 more aggressive 1sec
572 * initRTO, we only reset cwnd when more than 1 SYN/SYN-ACK
573 * retransmission has occurred.
574 */
575 if (tp->total_retrans > 1)
576 tp->snd_cwnd = 1;
577 else
578 tp->snd_cwnd = tcp_init_cwnd(tp, dst);
579 tp->snd_cwnd_stamp = tcp_time_stamp;
580}
David S. Millerab92bb22012-07-09 16:19:30 -0700581
Hannes Frederic Sowaa26552a2014-08-14 22:06:12 +0200582bool tcp_peer_is_proven(struct request_sock *req, struct dst_entry *dst,
583 bool paws_check, bool timestamps)
David S. Millerab92bb22012-07-09 16:19:30 -0700584{
David S. Miller51c5d0c2012-07-10 00:49:14 -0700585 struct tcp_metrics_block *tm;
586 bool ret;
587
David S. Millerab92bb22012-07-09 16:19:30 -0700588 if (!dst)
589 return false;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700590
591 rcu_read_lock();
592 tm = __tcp_get_metrics_req(req, dst);
David S. Miller81166dd2012-07-10 03:14:24 -0700593 if (paws_check) {
594 if (tm &&
595 (u32)get_seconds() - tm->tcpm_ts_stamp < TCP_PAWS_MSL &&
Hannes Frederic Sowaa26552a2014-08-14 22:06:12 +0200596 ((s32)(tm->tcpm_ts - req->ts_recent) > TCP_PAWS_WINDOW ||
597 !timestamps))
David S. Miller81166dd2012-07-10 03:14:24 -0700598 ret = false;
599 else
600 ret = true;
601 } else {
602 if (tm && tcp_metric_get(tm, TCP_METRIC_RTT) && tm->tcpm_ts_stamp)
603 ret = true;
604 else
605 ret = false;
606 }
David S. Miller51c5d0c2012-07-10 00:49:14 -0700607 rcu_read_unlock();
608
609 return ret;
David S. Millerab92bb22012-07-09 16:19:30 -0700610}
611EXPORT_SYMBOL_GPL(tcp_peer_is_proven);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700612
David S. Miller81166dd2012-07-10 03:14:24 -0700613void tcp_fetch_timewait_stamp(struct sock *sk, struct dst_entry *dst)
614{
615 struct tcp_metrics_block *tm;
616
617 rcu_read_lock();
618 tm = tcp_get_metrics(sk, dst, true);
619 if (tm) {
620 struct tcp_sock *tp = tcp_sk(sk);
621
622 if ((u32)get_seconds() - tm->tcpm_ts_stamp <= TCP_PAWS_MSL) {
623 tp->rx_opt.ts_recent_stamp = tm->tcpm_ts_stamp;
624 tp->rx_opt.ts_recent = tm->tcpm_ts;
625 }
626 }
627 rcu_read_unlock();
628}
629EXPORT_SYMBOL_GPL(tcp_fetch_timewait_stamp);
630
631/* VJ's idea. Save last timestamp seen from this destination and hold
632 * it at least for normal timewait interval to use for duplicate
633 * segment detection in subsequent connections, before they enter
634 * synchronized state.
635 */
636bool tcp_remember_stamp(struct sock *sk)
637{
638 struct dst_entry *dst = __sk_dst_get(sk);
639 bool ret = false;
640
641 if (dst) {
642 struct tcp_metrics_block *tm;
643
644 rcu_read_lock();
645 tm = tcp_get_metrics(sk, dst, true);
646 if (tm) {
647 struct tcp_sock *tp = tcp_sk(sk);
648
649 if ((s32)(tm->tcpm_ts - tp->rx_opt.ts_recent) <= 0 ||
650 ((u32)get_seconds() - tm->tcpm_ts_stamp > TCP_PAWS_MSL &&
651 tm->tcpm_ts_stamp <= (u32)tp->rx_opt.ts_recent_stamp)) {
652 tm->tcpm_ts_stamp = (u32)tp->rx_opt.ts_recent_stamp;
653 tm->tcpm_ts = tp->rx_opt.ts_recent;
654 }
655 ret = true;
656 }
657 rcu_read_unlock();
658 }
659 return ret;
660}
661
662bool tcp_tw_remember_stamp(struct inet_timewait_sock *tw)
663{
664 struct tcp_metrics_block *tm;
665 bool ret = false;
666
667 rcu_read_lock();
668 tm = __tcp_get_metrics_tw(tw);
Julian Anastasov9a0a9502012-07-23 10:46:38 +0300669 if (tm) {
David S. Miller81166dd2012-07-10 03:14:24 -0700670 const struct tcp_timewait_sock *tcptw;
671 struct sock *sk = (struct sock *) tw;
672
673 tcptw = tcp_twsk(sk);
674 if ((s32)(tm->tcpm_ts - tcptw->tw_ts_recent) <= 0 ||
675 ((u32)get_seconds() - tm->tcpm_ts_stamp > TCP_PAWS_MSL &&
676 tm->tcpm_ts_stamp <= (u32)tcptw->tw_ts_recent_stamp)) {
677 tm->tcpm_ts_stamp = (u32)tcptw->tw_ts_recent_stamp;
678 tm->tcpm_ts = tcptw->tw_ts_recent;
679 }
680 ret = true;
681 }
682 rcu_read_unlock();
683
684 return ret;
685}
686
Yuchung Cheng1fe4c482012-07-19 06:43:06 +0000687static DEFINE_SEQLOCK(fastopen_seqlock);
688
689void tcp_fastopen_cache_get(struct sock *sk, u16 *mss,
Yuchung Chengaab48742012-07-19 06:43:10 +0000690 struct tcp_fastopen_cookie *cookie,
691 int *syn_loss, unsigned long *last_syn_loss)
Yuchung Cheng1fe4c482012-07-19 06:43:06 +0000692{
693 struct tcp_metrics_block *tm;
694
695 rcu_read_lock();
696 tm = tcp_get_metrics(sk, __sk_dst_get(sk), false);
697 if (tm) {
698 struct tcp_fastopen_metrics *tfom = &tm->tcpm_fastopen;
699 unsigned int seq;
700
701 do {
702 seq = read_seqbegin(&fastopen_seqlock);
703 if (tfom->mss)
704 *mss = tfom->mss;
705 *cookie = tfom->cookie;
Yuchung Chengaab48742012-07-19 06:43:10 +0000706 *syn_loss = tfom->syn_loss;
707 *last_syn_loss = *syn_loss ? tfom->last_syn_loss : 0;
Yuchung Cheng1fe4c482012-07-19 06:43:06 +0000708 } while (read_seqretry(&fastopen_seqlock, seq));
709 }
710 rcu_read_unlock();
711}
712
Yuchung Cheng1fe4c482012-07-19 06:43:06 +0000713void tcp_fastopen_cache_set(struct sock *sk, u16 mss,
Yuchung Chengaab48742012-07-19 06:43:10 +0000714 struct tcp_fastopen_cookie *cookie, bool syn_lost)
Yuchung Cheng1fe4c482012-07-19 06:43:06 +0000715{
Eric Dumazetdccf76c2013-11-13 15:00:46 -0800716 struct dst_entry *dst = __sk_dst_get(sk);
Yuchung Cheng1fe4c482012-07-19 06:43:06 +0000717 struct tcp_metrics_block *tm;
718
Eric Dumazetdccf76c2013-11-13 15:00:46 -0800719 if (!dst)
720 return;
Yuchung Cheng1fe4c482012-07-19 06:43:06 +0000721 rcu_read_lock();
Eric Dumazetdccf76c2013-11-13 15:00:46 -0800722 tm = tcp_get_metrics(sk, dst, true);
Yuchung Cheng1fe4c482012-07-19 06:43:06 +0000723 if (tm) {
724 struct tcp_fastopen_metrics *tfom = &tm->tcpm_fastopen;
725
726 write_seqlock_bh(&fastopen_seqlock);
Yuchung Chengc9686012013-10-29 10:09:05 -0700727 if (mss)
728 tfom->mss = mss;
729 if (cookie && cookie->len > 0)
Yuchung Cheng1fe4c482012-07-19 06:43:06 +0000730 tfom->cookie = *cookie;
Yuchung Chengaab48742012-07-19 06:43:10 +0000731 if (syn_lost) {
732 ++tfom->syn_loss;
733 tfom->last_syn_loss = jiffies;
734 } else
735 tfom->syn_loss = 0;
Yuchung Cheng1fe4c482012-07-19 06:43:06 +0000736 write_sequnlock_bh(&fastopen_seqlock);
737 }
738 rcu_read_unlock();
739}
740
Julian Anastasovd23ff702012-09-04 11:03:15 +0000741static struct genl_family tcp_metrics_nl_family = {
742 .id = GENL_ID_GENERATE,
743 .hdrsize = 0,
744 .name = TCP_METRICS_GENL_NAME,
745 .version = TCP_METRICS_GENL_VERSION,
746 .maxattr = TCP_METRICS_ATTR_MAX,
747 .netnsok = true,
748};
749
750static struct nla_policy tcp_metrics_nl_policy[TCP_METRICS_ATTR_MAX + 1] = {
751 [TCP_METRICS_ATTR_ADDR_IPV4] = { .type = NLA_U32, },
752 [TCP_METRICS_ATTR_ADDR_IPV6] = { .type = NLA_BINARY,
753 .len = sizeof(struct in6_addr), },
754 /* Following attributes are not received for GET/DEL,
755 * we keep them for reference
756 */
757#if 0
758 [TCP_METRICS_ATTR_AGE] = { .type = NLA_MSECS, },
759 [TCP_METRICS_ATTR_TW_TSVAL] = { .type = NLA_U32, },
760 [TCP_METRICS_ATTR_TW_TS_STAMP] = { .type = NLA_S32, },
761 [TCP_METRICS_ATTR_VALS] = { .type = NLA_NESTED, },
762 [TCP_METRICS_ATTR_FOPEN_MSS] = { .type = NLA_U16, },
763 [TCP_METRICS_ATTR_FOPEN_SYN_DROPS] = { .type = NLA_U16, },
764 [TCP_METRICS_ATTR_FOPEN_SYN_DROP_TS] = { .type = NLA_MSECS, },
765 [TCP_METRICS_ATTR_FOPEN_COOKIE] = { .type = NLA_BINARY,
766 .len = TCP_FASTOPEN_COOKIE_MAX, },
767#endif
768};
769
770/* Add attributes, caller cancels its header on failure */
771static int tcp_metrics_fill_info(struct sk_buff *msg,
772 struct tcp_metrics_block *tm)
773{
774 struct nlattr *nest;
775 int i;
776
Christoph Paasch324fd552014-01-08 16:05:55 +0100777 switch (tm->tcpm_daddr.family) {
Julian Anastasovd23ff702012-09-04 11:03:15 +0000778 case AF_INET:
779 if (nla_put_be32(msg, TCP_METRICS_ATTR_ADDR_IPV4,
Christoph Paasch324fd552014-01-08 16:05:55 +0100780 tm->tcpm_daddr.addr.a4) < 0)
Julian Anastasovd23ff702012-09-04 11:03:15 +0000781 goto nla_put_failure;
Christoph Paasch8a59359c2014-01-08 16:05:57 +0100782 if (nla_put_be32(msg, TCP_METRICS_ATTR_SADDR_IPV4,
783 tm->tcpm_saddr.addr.a4) < 0)
784 goto nla_put_failure;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000785 break;
786 case AF_INET6:
787 if (nla_put(msg, TCP_METRICS_ATTR_ADDR_IPV6, 16,
Christoph Paasch324fd552014-01-08 16:05:55 +0100788 tm->tcpm_daddr.addr.a6) < 0)
Julian Anastasovd23ff702012-09-04 11:03:15 +0000789 goto nla_put_failure;
Christoph Paasch8a59359c2014-01-08 16:05:57 +0100790 if (nla_put(msg, TCP_METRICS_ATTR_SADDR_IPV6, 16,
791 tm->tcpm_saddr.addr.a6) < 0)
792 goto nla_put_failure;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000793 break;
794 default:
795 return -EAFNOSUPPORT;
796 }
797
798 if (nla_put_msecs(msg, TCP_METRICS_ATTR_AGE,
799 jiffies - tm->tcpm_stamp) < 0)
800 goto nla_put_failure;
801 if (tm->tcpm_ts_stamp) {
802 if (nla_put_s32(msg, TCP_METRICS_ATTR_TW_TS_STAMP,
803 (s32) (get_seconds() - tm->tcpm_ts_stamp)) < 0)
804 goto nla_put_failure;
805 if (nla_put_u32(msg, TCP_METRICS_ATTR_TW_TSVAL,
806 tm->tcpm_ts) < 0)
807 goto nla_put_failure;
808 }
809
810 {
811 int n = 0;
812
813 nest = nla_nest_start(msg, TCP_METRICS_ATTR_VALS);
814 if (!nest)
815 goto nla_put_failure;
Eric Dumazet740b0f12014-02-26 14:02:48 -0800816 for (i = 0; i < TCP_METRIC_MAX_KERNEL + 1; i++) {
817 u32 val = tm->tcpm_vals[i];
818
819 if (!val)
Julian Anastasovd23ff702012-09-04 11:03:15 +0000820 continue;
Eric Dumazet740b0f12014-02-26 14:02:48 -0800821 if (i == TCP_METRIC_RTT) {
822 if (nla_put_u32(msg, TCP_METRIC_RTT_US + 1,
823 val) < 0)
824 goto nla_put_failure;
825 n++;
826 val = max(val / 1000, 1U);
827 }
828 if (i == TCP_METRIC_RTTVAR) {
829 if (nla_put_u32(msg, TCP_METRIC_RTTVAR_US + 1,
830 val) < 0)
831 goto nla_put_failure;
832 n++;
833 val = max(val / 1000, 1U);
834 }
835 if (nla_put_u32(msg, i + 1, val) < 0)
Julian Anastasovd23ff702012-09-04 11:03:15 +0000836 goto nla_put_failure;
837 n++;
838 }
839 if (n)
840 nla_nest_end(msg, nest);
841 else
842 nla_nest_cancel(msg, nest);
843 }
844
845 {
846 struct tcp_fastopen_metrics tfom_copy[1], *tfom;
847 unsigned int seq;
848
849 do {
850 seq = read_seqbegin(&fastopen_seqlock);
851 tfom_copy[0] = tm->tcpm_fastopen;
852 } while (read_seqretry(&fastopen_seqlock, seq));
853
854 tfom = tfom_copy;
855 if (tfom->mss &&
856 nla_put_u16(msg, TCP_METRICS_ATTR_FOPEN_MSS,
857 tfom->mss) < 0)
858 goto nla_put_failure;
859 if (tfom->syn_loss &&
860 (nla_put_u16(msg, TCP_METRICS_ATTR_FOPEN_SYN_DROPS,
861 tfom->syn_loss) < 0 ||
862 nla_put_msecs(msg, TCP_METRICS_ATTR_FOPEN_SYN_DROP_TS,
863 jiffies - tfom->last_syn_loss) < 0))
864 goto nla_put_failure;
865 if (tfom->cookie.len > 0 &&
866 nla_put(msg, TCP_METRICS_ATTR_FOPEN_COOKIE,
867 tfom->cookie.len, tfom->cookie.val) < 0)
868 goto nla_put_failure;
869 }
870
871 return 0;
872
873nla_put_failure:
874 return -EMSGSIZE;
875}
876
877static int tcp_metrics_dump_info(struct sk_buff *skb,
878 struct netlink_callback *cb,
879 struct tcp_metrics_block *tm)
880{
881 void *hdr;
882
Eric W. Biederman15e47302012-09-07 20:12:54 +0000883 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Julian Anastasovd23ff702012-09-04 11:03:15 +0000884 &tcp_metrics_nl_family, NLM_F_MULTI,
885 TCP_METRICS_CMD_GET);
886 if (!hdr)
887 return -EMSGSIZE;
888
889 if (tcp_metrics_fill_info(skb, tm) < 0)
890 goto nla_put_failure;
891
Johannes Berg053c0952015-01-16 22:09:00 +0100892 genlmsg_end(skb, hdr);
893 return 0;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000894
895nla_put_failure:
896 genlmsg_cancel(skb, hdr);
897 return -EMSGSIZE;
898}
899
900static int tcp_metrics_nl_dump(struct sk_buff *skb,
901 struct netlink_callback *cb)
902{
903 struct net *net = sock_net(skb->sk);
904 unsigned int max_rows = 1U << net->ipv4.tcp_metrics_hash_log;
905 unsigned int row, s_row = cb->args[0];
906 int s_col = cb->args[1], col = s_col;
907
908 for (row = s_row; row < max_rows; row++, s_col = 0) {
909 struct tcp_metrics_block *tm;
910 struct tcpm_hash_bucket *hb = net->ipv4.tcp_metrics_hash + row;
911
912 rcu_read_lock();
913 for (col = 0, tm = rcu_dereference(hb->chain); tm;
914 tm = rcu_dereference(tm->tcpm_next), col++) {
915 if (col < s_col)
916 continue;
917 if (tcp_metrics_dump_info(skb, cb, tm) < 0) {
918 rcu_read_unlock();
919 goto done;
920 }
921 }
922 rcu_read_unlock();
923 }
924
925done:
926 cb->args[0] = row;
927 cb->args[1] = col;
928 return skb->len;
929}
930
Christoph Paasch3e7013d2014-01-08 16:05:59 +0100931static int __parse_nl_addr(struct genl_info *info, struct inetpeer_addr *addr,
932 unsigned int *hash, int optional, int v4, int v6)
Julian Anastasovd23ff702012-09-04 11:03:15 +0000933{
934 struct nlattr *a;
935
Christoph Paasch3e7013d2014-01-08 16:05:59 +0100936 a = info->attrs[v4];
Julian Anastasovd23ff702012-09-04 11:03:15 +0000937 if (a) {
938 addr->family = AF_INET;
939 addr->addr.a4 = nla_get_be32(a);
Christoph Paasch3e7013d2014-01-08 16:05:59 +0100940 if (hash)
941 *hash = (__force unsigned int) addr->addr.a4;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000942 return 0;
943 }
Christoph Paasch3e7013d2014-01-08 16:05:59 +0100944 a = info->attrs[v6];
Julian Anastasovd23ff702012-09-04 11:03:15 +0000945 if (a) {
Julian Anastasov2c42a3f2012-10-30 12:03:09 +0000946 if (nla_len(a) != sizeof(struct in6_addr))
Julian Anastasovd23ff702012-09-04 11:03:15 +0000947 return -EINVAL;
948 addr->family = AF_INET6;
949 memcpy(addr->addr.a6, nla_data(a), sizeof(addr->addr.a6));
Christoph Paasch3e7013d2014-01-08 16:05:59 +0100950 if (hash)
951 *hash = ipv6_addr_hash((struct in6_addr *) addr->addr.a6);
Julian Anastasovd23ff702012-09-04 11:03:15 +0000952 return 0;
953 }
954 return optional ? 1 : -EAFNOSUPPORT;
955}
956
Christoph Paasch3e7013d2014-01-08 16:05:59 +0100957static int parse_nl_addr(struct genl_info *info, struct inetpeer_addr *addr,
958 unsigned int *hash, int optional)
959{
960 return __parse_nl_addr(info, addr, hash, optional,
961 TCP_METRICS_ATTR_ADDR_IPV4,
962 TCP_METRICS_ATTR_ADDR_IPV6);
963}
964
965static int parse_nl_saddr(struct genl_info *info, struct inetpeer_addr *addr)
966{
967 return __parse_nl_addr(info, addr, NULL, 0,
968 TCP_METRICS_ATTR_SADDR_IPV4,
969 TCP_METRICS_ATTR_SADDR_IPV6);
970}
971
Julian Anastasovd23ff702012-09-04 11:03:15 +0000972static int tcp_metrics_nl_cmd_get(struct sk_buff *skb, struct genl_info *info)
973{
974 struct tcp_metrics_block *tm;
Christoph Paasch3e7013d2014-01-08 16:05:59 +0100975 struct inetpeer_addr saddr, daddr;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000976 unsigned int hash;
977 struct sk_buff *msg;
978 struct net *net = genl_info_net(info);
979 void *reply;
980 int ret;
Christoph Paasch3e7013d2014-01-08 16:05:59 +0100981 bool src = true;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000982
Christoph Paasch324fd552014-01-08 16:05:55 +0100983 ret = parse_nl_addr(info, &daddr, &hash, 0);
Julian Anastasovd23ff702012-09-04 11:03:15 +0000984 if (ret < 0)
985 return ret;
986
Christoph Paasch3e7013d2014-01-08 16:05:59 +0100987 ret = parse_nl_saddr(info, &saddr);
988 if (ret < 0)
989 src = false;
990
Julian Anastasovd23ff702012-09-04 11:03:15 +0000991 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
992 if (!msg)
993 return -ENOMEM;
994
995 reply = genlmsg_put_reply(msg, info, &tcp_metrics_nl_family, 0,
996 info->genlhdr->cmd);
997 if (!reply)
998 goto nla_put_failure;
999
Eric W. Biederman3e5da622015-03-13 00:05:24 -05001000 hash ^= net_hash_mix(net);
Julian Anastasovd23ff702012-09-04 11:03:15 +00001001 hash = hash_32(hash, net->ipv4.tcp_metrics_hash_log);
1002 ret = -ESRCH;
1003 rcu_read_lock();
1004 for (tm = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain); tm;
1005 tm = rcu_dereference(tm->tcpm_next)) {
Christoph Paasch3e7013d2014-01-08 16:05:59 +01001006 if (addr_same(&tm->tcpm_daddr, &daddr) &&
1007 (!src || addr_same(&tm->tcpm_saddr, &saddr))) {
Julian Anastasovd23ff702012-09-04 11:03:15 +00001008 ret = tcp_metrics_fill_info(msg, tm);
1009 break;
1010 }
1011 }
1012 rcu_read_unlock();
1013 if (ret < 0)
1014 goto out_free;
1015
1016 genlmsg_end(msg, reply);
1017 return genlmsg_reply(msg, info);
1018
1019nla_put_failure:
1020 ret = -EMSGSIZE;
1021
1022out_free:
1023 nlmsg_free(msg);
1024 return ret;
1025}
1026
1027#define deref_locked_genl(p) \
1028 rcu_dereference_protected(p, lockdep_genl_is_held() && \
1029 lockdep_is_held(&tcp_metrics_lock))
1030
1031#define deref_genl(p) rcu_dereference_protected(p, lockdep_genl_is_held())
1032
1033static int tcp_metrics_flush_all(struct net *net)
1034{
1035 unsigned int max_rows = 1U << net->ipv4.tcp_metrics_hash_log;
1036 struct tcpm_hash_bucket *hb = net->ipv4.tcp_metrics_hash;
1037 struct tcp_metrics_block *tm;
1038 unsigned int row;
1039
1040 for (row = 0; row < max_rows; row++, hb++) {
1041 spin_lock_bh(&tcp_metrics_lock);
1042 tm = deref_locked_genl(hb->chain);
1043 if (tm)
1044 hb->chain = NULL;
1045 spin_unlock_bh(&tcp_metrics_lock);
1046 while (tm) {
1047 struct tcp_metrics_block *next;
1048
1049 next = deref_genl(tm->tcpm_next);
1050 kfree_rcu(tm, rcu_head);
1051 tm = next;
1052 }
1053 }
1054 return 0;
1055}
1056
1057static int tcp_metrics_nl_cmd_del(struct sk_buff *skb, struct genl_info *info)
1058{
1059 struct tcpm_hash_bucket *hb;
Christoph Paasch00ca9c52014-01-21 13:30:26 +01001060 struct tcp_metrics_block *tm;
Julian Anastasovd23ff702012-09-04 11:03:15 +00001061 struct tcp_metrics_block __rcu **pp;
Christoph Paasch3e7013d2014-01-08 16:05:59 +01001062 struct inetpeer_addr saddr, daddr;
Julian Anastasovd23ff702012-09-04 11:03:15 +00001063 unsigned int hash;
1064 struct net *net = genl_info_net(info);
1065 int ret;
Christoph Paasch00ca9c52014-01-21 13:30:26 +01001066 bool src = true, found = false;
Julian Anastasovd23ff702012-09-04 11:03:15 +00001067
Christoph Paasch324fd552014-01-08 16:05:55 +01001068 ret = parse_nl_addr(info, &daddr, &hash, 1);
Julian Anastasovd23ff702012-09-04 11:03:15 +00001069 if (ret < 0)
1070 return ret;
1071 if (ret > 0)
1072 return tcp_metrics_flush_all(net);
Christoph Paasch3e7013d2014-01-08 16:05:59 +01001073 ret = parse_nl_saddr(info, &saddr);
1074 if (ret < 0)
1075 src = false;
Julian Anastasovd23ff702012-09-04 11:03:15 +00001076
Eric W. Biederman3e5da622015-03-13 00:05:24 -05001077 hash ^= net_hash_mix(net);
Julian Anastasovd23ff702012-09-04 11:03:15 +00001078 hash = hash_32(hash, net->ipv4.tcp_metrics_hash_log);
1079 hb = net->ipv4.tcp_metrics_hash + hash;
1080 pp = &hb->chain;
1081 spin_lock_bh(&tcp_metrics_lock);
Christoph Paaschbbf852b2014-01-08 16:05:58 +01001082 for (tm = deref_locked_genl(*pp); tm; tm = deref_locked_genl(*pp)) {
Christoph Paasch3e7013d2014-01-08 16:05:59 +01001083 if (addr_same(&tm->tcpm_daddr, &daddr) &&
1084 (!src || addr_same(&tm->tcpm_saddr, &saddr))) {
Julian Anastasovd23ff702012-09-04 11:03:15 +00001085 *pp = tm->tcpm_next;
Christoph Paasch00ca9c52014-01-21 13:30:26 +01001086 kfree_rcu(tm, rcu_head);
1087 found = true;
Christoph Paaschbbf852b2014-01-08 16:05:58 +01001088 } else {
1089 pp = &tm->tcpm_next;
Julian Anastasovd23ff702012-09-04 11:03:15 +00001090 }
1091 }
1092 spin_unlock_bh(&tcp_metrics_lock);
Christoph Paasch00ca9c52014-01-21 13:30:26 +01001093 if (!found)
Julian Anastasovd23ff702012-09-04 11:03:15 +00001094 return -ESRCH;
Julian Anastasovd23ff702012-09-04 11:03:15 +00001095 return 0;
1096}
1097
Johannes Berg4534de82013-11-14 17:14:46 +01001098static const struct genl_ops tcp_metrics_nl_ops[] = {
Julian Anastasovd23ff702012-09-04 11:03:15 +00001099 {
1100 .cmd = TCP_METRICS_CMD_GET,
1101 .doit = tcp_metrics_nl_cmd_get,
1102 .dumpit = tcp_metrics_nl_dump,
1103 .policy = tcp_metrics_nl_policy,
Julian Anastasovd23ff702012-09-04 11:03:15 +00001104 },
1105 {
1106 .cmd = TCP_METRICS_CMD_DEL,
1107 .doit = tcp_metrics_nl_cmd_del,
1108 .policy = tcp_metrics_nl_policy,
1109 .flags = GENL_ADMIN_PERM,
1110 },
1111};
1112
Eric Dumazet5815d5e2012-07-19 23:02:34 +00001113static unsigned int tcpmhash_entries;
David S. Miller51c5d0c2012-07-10 00:49:14 -07001114static int __init set_tcpmhash_entries(char *str)
1115{
1116 ssize_t ret;
1117
1118 if (!str)
1119 return 0;
1120
Eric Dumazet5815d5e2012-07-19 23:02:34 +00001121 ret = kstrtouint(str, 0, &tcpmhash_entries);
David S. Miller51c5d0c2012-07-10 00:49:14 -07001122 if (ret)
1123 return 0;
1124
1125 return 1;
1126}
1127__setup("tcpmhash_entries=", set_tcpmhash_entries);
1128
1129static int __net_init tcp_net_metrics_init(struct net *net)
1130{
Eric Dumazet5815d5e2012-07-19 23:02:34 +00001131 size_t size;
1132 unsigned int slots;
David S. Miller51c5d0c2012-07-10 00:49:14 -07001133
1134 slots = tcpmhash_entries;
1135 if (!slots) {
1136 if (totalram_pages >= 128 * 1024)
1137 slots = 16 * 1024;
1138 else
1139 slots = 8 * 1024;
1140 }
1141
Eric Dumazet5815d5e2012-07-19 23:02:34 +00001142 net->ipv4.tcp_metrics_hash_log = order_base_2(slots);
1143 size = sizeof(struct tcpm_hash_bucket) << net->ipv4.tcp_metrics_hash_log;
David S. Miller51c5d0c2012-07-10 00:49:14 -07001144
Eric Dumazet976a7022012-11-16 05:31:53 +00001145 net->ipv4.tcp_metrics_hash = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
1146 if (!net->ipv4.tcp_metrics_hash)
1147 net->ipv4.tcp_metrics_hash = vzalloc(size);
1148
David S. Miller51c5d0c2012-07-10 00:49:14 -07001149 if (!net->ipv4.tcp_metrics_hash)
1150 return -ENOMEM;
1151
David S. Miller51c5d0c2012-07-10 00:49:14 -07001152 return 0;
1153}
1154
1155static void __net_exit tcp_net_metrics_exit(struct net *net)
1156{
Eric Dumazet36471012012-08-09 11:19:13 +02001157 unsigned int i;
1158
1159 for (i = 0; i < (1U << net->ipv4.tcp_metrics_hash_log) ; i++) {
1160 struct tcp_metrics_block *tm, *next;
1161
1162 tm = rcu_dereference_protected(net->ipv4.tcp_metrics_hash[i].chain, 1);
1163 while (tm) {
1164 next = rcu_dereference_protected(tm->tcpm_next, 1);
1165 kfree(tm);
1166 tm = next;
1167 }
1168 }
WANG Cong4cb28972014-06-02 15:55:22 -07001169 kvfree(net->ipv4.tcp_metrics_hash);
David S. Miller51c5d0c2012-07-10 00:49:14 -07001170}
1171
1172static __net_initdata struct pernet_operations tcp_net_metrics_ops = {
1173 .init = tcp_net_metrics_init,
1174 .exit = tcp_net_metrics_exit,
1175};
1176
1177void __init tcp_metrics_init(void)
1178{
Julian Anastasovd23ff702012-09-04 11:03:15 +00001179 int ret;
1180
1181 ret = register_pernet_subsys(&tcp_net_metrics_ops);
1182 if (ret < 0)
Eric W. Biederman64935172015-03-13 00:04:51 -05001183 panic("Could not allocate the tcp_metrics hash table\n");
1184
Julian Anastasovd23ff702012-09-04 11:03:15 +00001185 ret = genl_register_family_with_ops(&tcp_metrics_nl_family,
Johannes Bergc53ed742013-11-19 15:19:31 +01001186 tcp_metrics_nl_ops);
Julian Anastasovd23ff702012-09-04 11:03:15 +00001187 if (ret < 0)
Eric W. Biederman64935172015-03-13 00:04:51 -05001188 panic("Could not register tcp_metrics generic netlink\n");
David S. Miller51c5d0c2012-07-10 00:49:14 -07001189}