blob: 78ecc4a017128adf389c33418c937134f111e39d [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;
Eric W. Biederman849e8a02015-03-13 00:05:52 -050043 possible_net_t tcpm_net;
Christoph Paascha5443022014-01-08 16:05:56 +010044 struct inetpeer_addr tcpm_saddr;
Christoph Paasch324fd552014-01-08 16:05:55 +010045 struct inetpeer_addr tcpm_daddr;
David S. Miller51c5d0c2012-07-10 00:49:14 -070046 unsigned long tcpm_stamp;
David S. Miller81166dd2012-07-10 03:14:24 -070047 u32 tcpm_ts;
48 u32 tcpm_ts_stamp;
David S. Miller51c5d0c2012-07-10 00:49:14 -070049 u32 tcpm_lock;
Eric Dumazet740b0f12014-02-26 14:02:48 -080050 u32 tcpm_vals[TCP_METRIC_MAX_KERNEL + 1];
Yuchung Cheng1fe4c482012-07-19 06:43:06 +000051 struct tcp_fastopen_metrics tcpm_fastopen;
Julian Anastasovd23ff702012-09-04 11:03:15 +000052
53 struct rcu_head rcu_head;
David S. Miller51c5d0c2012-07-10 00:49:14 -070054};
55
Eric W. Biederman849e8a02015-03-13 00:05:52 -050056static inline struct net *tm_net(struct tcp_metrics_block *tm)
57{
58 return read_pnet(&tm->tcpm_net);
59}
60
David S. Miller51c5d0c2012-07-10 00:49:14 -070061static 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
67static 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. Miller51c5d0c2012-07-10 00:49:14 -070073static 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. Miller51c5d0c2012-07-10 00:49:14 -070080static bool addr_same(const struct inetpeer_addr *a,
81 const struct inetpeer_addr *b)
82{
David S. Miller51c5d0c2012-07-10 00:49:14 -070083 if (a->family != b->family)
84 return false;
85 if (a->family == AF_INET)
86 return a->addr.a4 == b->addr.a4;
Jiri Benc8f55db42015-03-29 16:59:23 +020087 return ipv6_addr_equal(&a->addr.in6, &b->addr.in6);
David S. Miller51c5d0c2012-07-10 00:49:14 -070088}
89
90struct tcpm_hash_bucket {
91 struct tcp_metrics_block __rcu *chain;
92};
93
Eric W. Biederman098a6972015-03-13 00:07:44 -050094static struct tcpm_hash_bucket *tcp_metrics_hash __read_mostly;
95static unsigned int tcp_metrics_hash_log __read_mostly;
96
David S. Miller51c5d0c2012-07-10 00:49:14 -070097static DEFINE_SPINLOCK(tcp_metrics_lock);
98
Eric Dumazet740b0f12014-02-26 14:02:48 -080099static void tcpm_suck_dst(struct tcp_metrics_block *tm,
100 const struct dst_entry *dst,
Eric Dumazetefeaa552013-05-03 19:12:45 +0000101 bool fastopen_clear)
David S. Miller51c5d0c2012-07-10 00:49:14 -0700102{
Eric Dumazet740b0f12014-02-26 14:02:48 -0800103 u32 msval;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700104 u32 val;
105
Julian Anastasov9a0a9502012-07-23 10:46:38 +0300106 tm->tcpm_stamp = jiffies;
107
David S. Miller51c5d0c2012-07-10 00:49:14 -0700108 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 Dumazet740b0f12014-02-26 14:02:48 -0800121 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. Miller51c5d0c2012-07-10 00:49:14 -0700126 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. Miller81166dd2012-07-10 03:14:24 -0700129 tm->tcpm_ts = 0;
130 tm->tcpm_ts_stamp = 0;
Eric Dumazetefeaa552013-05-03 19:12:45 +0000131 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. Miller51c5d0c2012-07-10 00:49:14 -0700136}
137
Christoph Paasch77f99ad2014-01-16 20:01:21 +0100138#define TCP_METRICS_TIMEOUT (60 * 60 * HZ)
139
140static 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 Dumazet9f1ab182015-03-16 07:14:34 -0700149#define deref_locked(p) \
150 rcu_dereference_protected(p, lockdep_is_held(&tcp_metrics_lock))
151
David S. Miller51c5d0c2012-07-10 00:49:14 -0700152static struct tcp_metrics_block *tcpm_new(struct dst_entry *dst,
Christoph Paascha5443022014-01-08 16:05:56 +0100153 struct inetpeer_addr *saddr,
Christoph Paasch324fd552014-01-08 16:05:55 +0100154 struct inetpeer_addr *daddr,
Christoph Paasch77f99ad2014-01-16 20:01:21 +0100155 unsigned int hash)
David S. Miller51c5d0c2012-07-10 00:49:14 -0700156{
157 struct tcp_metrics_block *tm;
158 struct net *net;
Christoph Paasch77f99ad2014-01-16 20:01:21 +0100159 bool reclaim = false;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700160
161 spin_lock_bh(&tcp_metrics_lock);
162 net = dev_net(dst->dev);
Christoph Paasch77f99ad2014-01-16 20:01:21 +0100163
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. Miller41804422014-01-18 00:55:41 -0800167 tm = __tcp_get_metrics(saddr, daddr, net, hash);
Christoph Paasch77f99ad2014-01-16 20:01:21 +0100168 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. Miller51c5d0c2012-07-10 00:49:14 -0700177 if (unlikely(reclaim)) {
178 struct tcp_metrics_block *oldest;
179
Eric Dumazet9f1ab182015-03-16 07:14:34 -0700180 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. Miller51c5d0c2012-07-10 00:49:14 -0700183 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. Biederman849e8a02015-03-13 00:05:52 -0500192 write_pnet(&tm->tcpm_net, net);
Christoph Paascha5443022014-01-08 16:05:56 +0100193 tm->tcpm_saddr = *saddr;
Christoph Paasch324fd552014-01-08 16:05:55 +0100194 tm->tcpm_daddr = *daddr;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700195
Eric Dumazetefeaa552013-05-03 19:12:45 +0000196 tcpm_suck_dst(tm, dst, true);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700197
198 if (likely(!reclaim)) {
Eric W. Biederman098a6972015-03-13 00:07:44 -0500199 tm->tcpm_next = tcp_metrics_hash[hash].chain;
200 rcu_assign_pointer(tcp_metrics_hash[hash].chain, tm);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700201 }
202
203out_unlock:
204 spin_unlock_bh(&tcp_metrics_lock);
205 return tm;
206}
207
David S. Miller51c5d0c2012-07-10 00:49:14 -0700208static 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 Paascha5443022014-01-08 16:05:56 +0100217static struct tcp_metrics_block *__tcp_get_metrics(const struct inetpeer_addr *saddr,
218 const struct inetpeer_addr *daddr,
David S. Miller51c5d0c2012-07-10 00:49:14 -0700219 struct net *net, unsigned int hash)
220{
221 struct tcp_metrics_block *tm;
222 int depth = 0;
223
Eric W. Biederman098a6972015-03-13 00:07:44 -0500224 for (tm = rcu_dereference(tcp_metrics_hash[hash].chain); tm;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700225 tm = rcu_dereference(tm->tcpm_next)) {
Christoph Paascha5443022014-01-08 16:05:56 +0100226 if (addr_same(&tm->tcpm_saddr, saddr) &&
Eric W. Biederman849e8a02015-03-13 00:05:52 -0500227 addr_same(&tm->tcpm_daddr, daddr) &&
228 net_eq(tm_net(tm), net))
David S. Miller51c5d0c2012-07-10 00:49:14 -0700229 break;
230 depth++;
231 }
232 return tcp_get_encode(tm, depth);
233}
234
235static 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 Paascha5443022014-01-08 16:05:56 +0100239 struct inetpeer_addr saddr, daddr;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700240 unsigned int hash;
241 struct net *net;
242
Christoph Paascha5443022014-01-08 16:05:56 +0100243 saddr.family = req->rsk_ops->family;
Christoph Paasch324fd552014-01-08 16:05:55 +0100244 daddr.family = req->rsk_ops->family;
245 switch (daddr.family) {
David S. Miller51c5d0c2012-07-10 00:49:14 -0700246 case AF_INET:
Christoph Paascha5443022014-01-08 16:05:56 +0100247 saddr.addr.a4 = inet_rsk(req)->ir_loc_addr;
Christoph Paasch324fd552014-01-08 16:05:55 +0100248 daddr.addr.a4 = inet_rsk(req)->ir_rmt_addr;
249 hash = (__force unsigned int) daddr.addr.a4;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700250 break;
Eric Dumazet634fb9792013-10-09 15:21:29 -0700251#if IS_ENABLED(CONFIG_IPV6)
David S. Miller51c5d0c2012-07-10 00:49:14 -0700252 case AF_INET6:
Jiri Benc8f55db42015-03-29 16:59:23 +0200253 saddr.addr.in6 = inet_rsk(req)->ir_v6_loc_addr;
254 daddr.addr.in6 = inet_rsk(req)->ir_v6_rmt_addr;
Eric Dumazet634fb9792013-10-09 15:21:29 -0700255 hash = ipv6_addr_hash(&inet_rsk(req)->ir_v6_rmt_addr);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700256 break;
Eric Dumazet634fb9792013-10-09 15:21:29 -0700257#endif
David S. Miller51c5d0c2012-07-10 00:49:14 -0700258 default:
259 return NULL;
260 }
261
David S. Miller51c5d0c2012-07-10 00:49:14 -0700262 net = dev_net(dst->dev);
Eric W. Biederman3e5da622015-03-13 00:05:24 -0500263 hash ^= net_hash_mix(net);
Eric W. Biederman098a6972015-03-13 00:07:44 -0500264 hash = hash_32(hash, tcp_metrics_hash_log);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700265
Eric W. Biederman098a6972015-03-13 00:07:44 -0500266 for (tm = rcu_dereference(tcp_metrics_hash[hash].chain); tm;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700267 tm = rcu_dereference(tm->tcpm_next)) {
Christoph Paascha5443022014-01-08 16:05:56 +0100268 if (addr_same(&tm->tcpm_saddr, &saddr) &&
Eric W. Biederman849e8a02015-03-13 00:05:52 -0500269 addr_same(&tm->tcpm_daddr, &daddr) &&
270 net_eq(tm_net(tm), net))
David S. Miller51c5d0c2012-07-10 00:49:14 -0700271 break;
272 }
273 tcpm_check_stamp(tm, dst);
274 return tm;
275}
276
David S. Miller81166dd2012-07-10 03:14:24 -0700277static struct tcp_metrics_block *__tcp_get_metrics_tw(struct inet_timewait_sock *tw)
278{
David S. Miller81166dd2012-07-10 03:14:24 -0700279 struct tcp_metrics_block *tm;
Christoph Paascha5443022014-01-08 16:05:56 +0100280 struct inetpeer_addr saddr, daddr;
David S. Miller81166dd2012-07-10 03:14:24 -0700281 unsigned int hash;
282 struct net *net;
283
Christoph Paasch3ad88cf2014-01-22 13:58:44 +0100284 if (tw->tw_family == AF_INET) {
285 saddr.family = AF_INET;
Christoph Paascha5443022014-01-08 16:05:56 +0100286 saddr.addr.a4 = tw->tw_rcv_saddr;
Christoph Paasch3ad88cf2014-01-22 13:58:44 +0100287 daddr.family = AF_INET;
Christoph Paasch324fd552014-01-08 16:05:55 +0100288 daddr.addr.a4 = tw->tw_daddr;
289 hash = (__force unsigned int) daddr.addr.a4;
David S. Miller81166dd2012-07-10 03:14:24 -0700290 }
Christoph Paasch3ad88cf2014-01-22 13:58:44 +0100291#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 Benc8f55db42015-03-29 16:59:23 +0200301 saddr.addr.in6 = tw->tw_v6_rcv_saddr;
Christoph Paasch3ad88cf2014-01-22 13:58:44 +0100302 daddr.family = AF_INET6;
Jiri Benc8f55db42015-03-29 16:59:23 +0200303 daddr.addr.in6 = tw->tw_v6_daddr;
Christoph Paasch3ad88cf2014-01-22 13:58:44 +0100304 hash = ipv6_addr_hash(&tw->tw_v6_daddr);
305 }
306 }
307#endif
308 else
309 return NULL;
David S. Miller81166dd2012-07-10 03:14:24 -0700310
David S. Miller81166dd2012-07-10 03:14:24 -0700311 net = twsk_net(tw);
Eric W. Biederman3e5da622015-03-13 00:05:24 -0500312 hash ^= net_hash_mix(net);
Eric W. Biederman098a6972015-03-13 00:07:44 -0500313 hash = hash_32(hash, tcp_metrics_hash_log);
David S. Miller81166dd2012-07-10 03:14:24 -0700314
Eric W. Biederman098a6972015-03-13 00:07:44 -0500315 for (tm = rcu_dereference(tcp_metrics_hash[hash].chain); tm;
David S. Miller81166dd2012-07-10 03:14:24 -0700316 tm = rcu_dereference(tm->tcpm_next)) {
Christoph Paascha5443022014-01-08 16:05:56 +0100317 if (addr_same(&tm->tcpm_saddr, &saddr) &&
Eric W. Biederman849e8a02015-03-13 00:05:52 -0500318 addr_same(&tm->tcpm_daddr, &daddr) &&
319 net_eq(tm_net(tm), net))
David S. Miller81166dd2012-07-10 03:14:24 -0700320 break;
321 }
322 return tm;
323}
324
David S. Miller51c5d0c2012-07-10 00:49:14 -0700325static 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 Paascha5443022014-01-08 16:05:56 +0100330 struct inetpeer_addr saddr, daddr;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700331 unsigned int hash;
332 struct net *net;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700333
Christoph Paasch3ad88cf2014-01-22 13:58:44 +0100334 if (sk->sk_family == AF_INET) {
335 saddr.family = AF_INET;
Christoph Paascha5443022014-01-08 16:05:56 +0100336 saddr.addr.a4 = inet_sk(sk)->inet_saddr;
Christoph Paasch3ad88cf2014-01-22 13:58:44 +0100337 daddr.family = AF_INET;
Christoph Paasch324fd552014-01-08 16:05:55 +0100338 daddr.addr.a4 = inet_sk(sk)->inet_daddr;
339 hash = (__force unsigned int) daddr.addr.a4;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700340 }
Christoph Paasch3ad88cf2014-01-22 13:58:44 +0100341#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 Benc8f55db42015-03-29 16:59:23 +0200351 saddr.addr.in6 = sk->sk_v6_rcv_saddr;
Christoph Paasch3ad88cf2014-01-22 13:58:44 +0100352 daddr.family = AF_INET6;
Jiri Benc8f55db42015-03-29 16:59:23 +0200353 daddr.addr.in6 = sk->sk_v6_daddr;
Christoph Paasch3ad88cf2014-01-22 13:58:44 +0100354 hash = ipv6_addr_hash(&sk->sk_v6_daddr);
355 }
356 }
357#endif
358 else
359 return NULL;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700360
David S. Miller51c5d0c2012-07-10 00:49:14 -0700361 net = dev_net(dst->dev);
Eric W. Biederman3e5da622015-03-13 00:05:24 -0500362 hash ^= net_hash_mix(net);
Eric W. Biederman098a6972015-03-13 00:07:44 -0500363 hash = hash_32(hash, tcp_metrics_hash_log);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700364
Christoph Paascha5443022014-01-08 16:05:56 +0100365 tm = __tcp_get_metrics(&saddr, &daddr, net, hash);
Christoph Paasch77f99ad2014-01-16 20:01:21 +0100366 if (tm == TCP_METRICS_RECLAIM_PTR)
David S. Miller51c5d0c2012-07-10 00:49:14 -0700367 tm = NULL;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700368 if (!tm && create)
David S. Miller41804422014-01-18 00:55:41 -0800369 tm = tcpm_new(dst, &saddr, &daddr, hash);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700370 else
371 tcpm_check_stamp(tm, dst);
372
373 return tm;
374}
375
David S. Miller4aabd8e2012-07-09 16:07:30 -0700376/* 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 */
380void tcp_update_metrics(struct sock *sk)
381{
David S. Miller51c5d0c2012-07-10 00:49:14 -0700382 const struct inet_connection_sock *icsk = inet_csk(sk);
David S. Miller4aabd8e2012-07-09 16:07:30 -0700383 struct dst_entry *dst = __sk_dst_get(sk);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700384 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. Miller4aabd8e2012-07-09 16:07:30 -0700389
David S. Miller51c5d0c2012-07-10 00:49:14 -0700390 if (sysctl_tcp_nometrics_save || !dst)
David S. Miller4aabd8e2012-07-09 16:07:30 -0700391 return;
392
David S. Miller51c5d0c2012-07-10 00:49:14 -0700393 if (dst->flags & DST_HOST)
David S. Miller4aabd8e2012-07-09 16:07:30 -0700394 dst_confirm(dst);
395
David S. Miller51c5d0c2012-07-10 00:49:14 -0700396 rcu_read_lock();
Eric Dumazet740b0f12014-02-26 14:02:48 -0800397 if (icsk->icsk_backoff || !tp->srtt_us) {
David S. Miller51c5d0c2012-07-10 00:49:14 -0700398 /* This session failed to estimate rtt. Why?
399 * Probably, no packets returned in time. Reset our
400 * results.
David S. Miller4aabd8e2012-07-09 16:07:30 -0700401 */
David S. Miller51c5d0c2012-07-10 00:49:14 -0700402 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 Dumazet740b0f12014-02-26 14:02:48 -0800412 rtt = tcp_metric_get(tm, TCP_METRIC_RTT);
413 m = rtt - tp->srtt_us;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700414
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 Dumazet740b0f12014-02-26 14:02:48 -0800421 rtt = tp->srtt_us;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700422 else
423 rtt -= (m >> 3);
Eric Dumazet740b0f12014-02-26 14:02:48 -0800424 tcp_metric_set(tm, TCP_METRIC_RTT, rtt);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700425 }
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 Dumazet740b0f12014-02-26 14:02:48 -0800435 if (m < tp->mdev_us)
436 m = tp->mdev_us;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700437
Eric Dumazet740b0f12014-02-26 14:02:48 -0800438 var = tcp_metric_get(tm, TCP_METRIC_RTTVAR);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700439 if (m >= var)
440 var = m;
441 else
442 var -= (var - m) >> 2;
443
Eric Dumazet740b0f12014-02-26 14:02:48 -0800444 tcp_metric_set(tm, TCP_METRIC_RTTVAR, var);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700445 }
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. Miller4aabd8e2012-07-09 16:07:30 -0700454 }
David S. Miller51c5d0c2012-07-10 00:49:14 -0700455 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. Miller4aabd8e2012-07-09 16:07:30 -0700460 }
David S. Miller51c5d0c2012-07-10 00:49:14 -0700461 } 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 Duyck21008442012-07-11 17:18:04 -0700469 tcp_metric_set(tm, TCP_METRIC_CWND, (val + tp->snd_cwnd) >> 1);
David S. Miller4aabd8e2012-07-09 16:07:30 -0700470 }
David S. Miller51c5d0c2012-07-10 00:49:14 -0700471 } 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. Miller4aabd8e2012-07-09 16:07:30 -0700489 tp->reordering != sysctl_tcp_reordering)
David S. Miller51c5d0c2012-07-10 00:49:14 -0700490 tcp_metric_set(tm, TCP_METRIC_REORDERING,
491 tp->reordering);
David S. Miller4aabd8e2012-07-09 16:07:30 -0700492 }
493 }
David S. Miller51c5d0c2012-07-10 00:49:14 -0700494 tm->tcpm_stamp = jiffies;
495out_unlock:
496 rcu_read_unlock();
David S. Miller4aabd8e2012-07-09 16:07:30 -0700497}
498
499/* Initialize metrics on socket. */
500
501void tcp_init_metrics(struct sock *sk)
502{
David S. Miller4aabd8e2012-07-09 16:07:30 -0700503 struct dst_entry *dst = __sk_dst_get(sk);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700504 struct tcp_sock *tp = tcp_sk(sk);
505 struct tcp_metrics_block *tm;
Yuchung Cheng1b7fdd22013-08-30 08:35:53 -0700506 u32 val, crtt = 0; /* cached RTT scaled by 8 */
David S. Miller4aabd8e2012-07-09 16:07:30 -0700507
Ian Morris51456b22015-04-03 09:17:26 +0100508 if (!dst)
David S. Miller4aabd8e2012-07-09 16:07:30 -0700509 goto reset;
510
511 dst_confirm(dst);
512
David S. Miller51c5d0c2012-07-10 00:49:14 -0700513 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. Miller4aabd8e2012-07-09 16:07:30 -0700526 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. Miller51c5d0c2012-07-10 00:49:14 -0700534 val = tcp_metric_get(tm, TCP_METRIC_REORDERING);
535 if (val && tp->reordering != val) {
David S. Miller4aabd8e2012-07-09 16:07:30 -0700536 tcp_disable_fack(tp);
537 tcp_disable_early_retrans(tp);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700538 tp->reordering = val;
David S. Miller4aabd8e2012-07-09 16:07:30 -0700539 }
540
Eric Dumazet740b0f12014-02-26 14:02:48 -0800541 crtt = tcp_metric_get(tm, TCP_METRIC_RTT);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700542 rcu_read_unlock();
David S. Miller4aabd8e2012-07-09 16:07:30 -0700543reset:
Yuchung Cheng52f20e62013-09-03 14:14:35 -0700544 /* 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 Dumazet740b0f12014-02-26 14:02:48 -0800564 if (crtt > tp->srtt_us) {
Neal Cardwell269aa752013-09-16 21:44:20 -0400565 /* Set RTO like tcp_rtt_estimator(), but from cached RTT. */
Eric Dumazet740b0f12014-02-26 14:02:48 -0800566 crtt /= 8 * USEC_PER_MSEC;
Neal Cardwell269aa752013-09-16 21:44:20 -0400567 inet_csk(sk)->icsk_rto = crtt + max(2 * crtt, tcp_rto_min(sk));
Eric Dumazet740b0f12014-02-26 14:02:48 -0800568 } else if (tp->srtt_us == 0) {
David S. Miller4aabd8e2012-07-09 16:07:30 -0700569 /* 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 Dumazet740b0f12014-02-26 14:02:48 -0800575 tp->rttvar_us = jiffies_to_usecs(TCP_TIMEOUT_FALLBACK);
576 tp->mdev_us = tp->mdev_max_us = tp->rttvar_us;
577
David S. Miller4aabd8e2012-07-09 16:07:30 -0700578 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. Millerab92bb22012-07-09 16:19:30 -0700591
Hannes Frederic Sowaa26552a2014-08-14 22:06:12 +0200592bool tcp_peer_is_proven(struct request_sock *req, struct dst_entry *dst,
593 bool paws_check, bool timestamps)
David S. Millerab92bb22012-07-09 16:19:30 -0700594{
David S. Miller51c5d0c2012-07-10 00:49:14 -0700595 struct tcp_metrics_block *tm;
596 bool ret;
597
David S. Millerab92bb22012-07-09 16:19:30 -0700598 if (!dst)
599 return false;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700600
601 rcu_read_lock();
602 tm = __tcp_get_metrics_req(req, dst);
David S. Miller81166dd2012-07-10 03:14:24 -0700603 if (paws_check) {
604 if (tm &&
605 (u32)get_seconds() - tm->tcpm_ts_stamp < TCP_PAWS_MSL &&
Hannes Frederic Sowaa26552a2014-08-14 22:06:12 +0200606 ((s32)(tm->tcpm_ts - req->ts_recent) > TCP_PAWS_WINDOW ||
607 !timestamps))
David S. Miller81166dd2012-07-10 03:14:24 -0700608 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. Miller51c5d0c2012-07-10 00:49:14 -0700617 rcu_read_unlock();
618
619 return ret;
David S. Millerab92bb22012-07-09 16:19:30 -0700620}
621EXPORT_SYMBOL_GPL(tcp_peer_is_proven);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700622
David S. Miller81166dd2012-07-10 03:14:24 -0700623void 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}
639EXPORT_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 */
646bool 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
672bool 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 Anastasov9a0a9502012-07-23 10:46:38 +0300679 if (tm) {
David S. Miller81166dd2012-07-10 03:14:24 -0700680 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 Cheng1fe4c482012-07-19 06:43:06 +0000697static DEFINE_SEQLOCK(fastopen_seqlock);
698
699void tcp_fastopen_cache_get(struct sock *sk, u16 *mss,
Yuchung Chengaab48742012-07-19 06:43:10 +0000700 struct tcp_fastopen_cookie *cookie,
701 int *syn_loss, unsigned long *last_syn_loss)
Yuchung Cheng1fe4c482012-07-19 06:43:06 +0000702{
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 Chengaab48742012-07-19 06:43:10 +0000716 *syn_loss = tfom->syn_loss;
717 *last_syn_loss = *syn_loss ? tfom->last_syn_loss : 0;
Yuchung Cheng1fe4c482012-07-19 06:43:06 +0000718 } while (read_seqretry(&fastopen_seqlock, seq));
719 }
720 rcu_read_unlock();
721}
722
Yuchung Cheng1fe4c482012-07-19 06:43:06 +0000723void tcp_fastopen_cache_set(struct sock *sk, u16 mss,
Yuchung Chengaab48742012-07-19 06:43:10 +0000724 struct tcp_fastopen_cookie *cookie, bool syn_lost)
Yuchung Cheng1fe4c482012-07-19 06:43:06 +0000725{
Eric Dumazetdccf76c2013-11-13 15:00:46 -0800726 struct dst_entry *dst = __sk_dst_get(sk);
Yuchung Cheng1fe4c482012-07-19 06:43:06 +0000727 struct tcp_metrics_block *tm;
728
Eric Dumazetdccf76c2013-11-13 15:00:46 -0800729 if (!dst)
730 return;
Yuchung Cheng1fe4c482012-07-19 06:43:06 +0000731 rcu_read_lock();
Eric Dumazetdccf76c2013-11-13 15:00:46 -0800732 tm = tcp_get_metrics(sk, dst, true);
Yuchung Cheng1fe4c482012-07-19 06:43:06 +0000733 if (tm) {
734 struct tcp_fastopen_metrics *tfom = &tm->tcpm_fastopen;
735
736 write_seqlock_bh(&fastopen_seqlock);
Yuchung Chengc9686012013-10-29 10:09:05 -0700737 if (mss)
738 tfom->mss = mss;
739 if (cookie && cookie->len > 0)
Yuchung Cheng1fe4c482012-07-19 06:43:06 +0000740 tfom->cookie = *cookie;
Yuchung Chengaab48742012-07-19 06:43:10 +0000741 if (syn_lost) {
742 ++tfom->syn_loss;
743 tfom->last_syn_loss = jiffies;
744 } else
745 tfom->syn_loss = 0;
Yuchung Cheng1fe4c482012-07-19 06:43:06 +0000746 write_sequnlock_bh(&fastopen_seqlock);
747 }
748 rcu_read_unlock();
749}
750
Julian Anastasovd23ff702012-09-04 11:03:15 +0000751static 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
760static 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 */
781static 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 Paasch324fd552014-01-08 16:05:55 +0100787 switch (tm->tcpm_daddr.family) {
Julian Anastasovd23ff702012-09-04 11:03:15 +0000788 case AF_INET:
Jiri Benc930345e2015-03-29 16:59:25 +0200789 if (nla_put_in_addr(msg, TCP_METRICS_ATTR_ADDR_IPV4,
790 tm->tcpm_daddr.addr.a4) < 0)
Julian Anastasovd23ff702012-09-04 11:03:15 +0000791 goto nla_put_failure;
Jiri Benc930345e2015-03-29 16:59:25 +0200792 if (nla_put_in_addr(msg, TCP_METRICS_ATTR_SADDR_IPV4,
793 tm->tcpm_saddr.addr.a4) < 0)
Christoph Paasch8a593592014-01-08 16:05:57 +0100794 goto nla_put_failure;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000795 break;
796 case AF_INET6:
Jiri Benc930345e2015-03-29 16:59:25 +0200797 if (nla_put_in6_addr(msg, TCP_METRICS_ATTR_ADDR_IPV6,
798 &tm->tcpm_daddr.addr.in6) < 0)
Julian Anastasovd23ff702012-09-04 11:03:15 +0000799 goto nla_put_failure;
Jiri Benc930345e2015-03-29 16:59:25 +0200800 if (nla_put_in6_addr(msg, TCP_METRICS_ATTR_SADDR_IPV6,
801 &tm->tcpm_saddr.addr.in6) < 0)
Christoph Paasch8a593592014-01-08 16:05:57 +0100802 goto nla_put_failure;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000803 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 Dumazet740b0f12014-02-26 14:02:48 -0800826 for (i = 0; i < TCP_METRIC_MAX_KERNEL + 1; i++) {
827 u32 val = tm->tcpm_vals[i];
828
829 if (!val)
Julian Anastasovd23ff702012-09-04 11:03:15 +0000830 continue;
Eric Dumazet740b0f12014-02-26 14:02:48 -0800831 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 Anastasovd23ff702012-09-04 11:03:15 +0000846 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
883nla_put_failure:
884 return -EMSGSIZE;
885}
886
887static 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. Biederman15e47302012-09-07 20:12:54 +0000893 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Julian Anastasovd23ff702012-09-04 11:03:15 +0000894 &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 Berg053c0952015-01-16 22:09:00 +0100902 genlmsg_end(skb, hdr);
903 return 0;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000904
905nla_put_failure:
906 genlmsg_cancel(skb, hdr);
907 return -EMSGSIZE;
908}
909
910static 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. Biederman098a6972015-03-13 00:07:44 -0500914 unsigned int max_rows = 1U << tcp_metrics_hash_log;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000915 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. Biederman098a6972015-03-13 00:07:44 -0500920 struct tcpm_hash_bucket *hb = tcp_metrics_hash + row;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000921
922 rcu_read_lock();
923 for (col = 0, tm = rcu_dereference(hb->chain); tm;
924 tm = rcu_dereference(tm->tcpm_next), col++) {
Eric W. Biederman849e8a02015-03-13 00:05:52 -0500925 if (!net_eq(tm_net(tm), net))
926 continue;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000927 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
937done:
938 cb->args[0] = row;
939 cb->args[1] = col;
940 return skb->len;
941}
942
Christoph Paasch3e7013d2014-01-08 16:05:59 +0100943static int __parse_nl_addr(struct genl_info *info, struct inetpeer_addr *addr,
944 unsigned int *hash, int optional, int v4, int v6)
Julian Anastasovd23ff702012-09-04 11:03:15 +0000945{
946 struct nlattr *a;
947
Christoph Paasch3e7013d2014-01-08 16:05:59 +0100948 a = info->attrs[v4];
Julian Anastasovd23ff702012-09-04 11:03:15 +0000949 if (a) {
950 addr->family = AF_INET;
Jiri Benc67b61f62015-03-29 16:59:26 +0200951 addr->addr.a4 = nla_get_in_addr(a);
Christoph Paasch3e7013d2014-01-08 16:05:59 +0100952 if (hash)
953 *hash = (__force unsigned int) addr->addr.a4;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000954 return 0;
955 }
Christoph Paasch3e7013d2014-01-08 16:05:59 +0100956 a = info->attrs[v6];
Julian Anastasovd23ff702012-09-04 11:03:15 +0000957 if (a) {
Julian Anastasov2c42a3f2012-10-30 12:03:09 +0000958 if (nla_len(a) != sizeof(struct in6_addr))
Julian Anastasovd23ff702012-09-04 11:03:15 +0000959 return -EINVAL;
960 addr->family = AF_INET6;
Jiri Benc67b61f62015-03-29 16:59:26 +0200961 addr->addr.in6 = nla_get_in6_addr(a);
Christoph Paasch3e7013d2014-01-08 16:05:59 +0100962 if (hash)
Jiri Benc8f55db42015-03-29 16:59:23 +0200963 *hash = ipv6_addr_hash(&addr->addr.in6);
Julian Anastasovd23ff702012-09-04 11:03:15 +0000964 return 0;
965 }
966 return optional ? 1 : -EAFNOSUPPORT;
967}
968
Christoph Paasch3e7013d2014-01-08 16:05:59 +0100969static 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
977static 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 Anastasovd23ff702012-09-04 11:03:15 +0000984static int tcp_metrics_nl_cmd_get(struct sk_buff *skb, struct genl_info *info)
985{
986 struct tcp_metrics_block *tm;
Christoph Paasch3e7013d2014-01-08 16:05:59 +0100987 struct inetpeer_addr saddr, daddr;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000988 unsigned int hash;
989 struct sk_buff *msg;
990 struct net *net = genl_info_net(info);
991 void *reply;
992 int ret;
Christoph Paasch3e7013d2014-01-08 16:05:59 +0100993 bool src = true;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000994
Christoph Paasch324fd552014-01-08 16:05:55 +0100995 ret = parse_nl_addr(info, &daddr, &hash, 0);
Julian Anastasovd23ff702012-09-04 11:03:15 +0000996 if (ret < 0)
997 return ret;
998
Christoph Paasch3e7013d2014-01-08 16:05:59 +0100999 ret = parse_nl_saddr(info, &saddr);
1000 if (ret < 0)
1001 src = false;
1002
Julian Anastasovd23ff702012-09-04 11:03:15 +00001003 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. Biederman3e5da622015-03-13 00:05:24 -05001012 hash ^= net_hash_mix(net);
Eric W. Biederman098a6972015-03-13 00:07:44 -05001013 hash = hash_32(hash, tcp_metrics_hash_log);
Julian Anastasovd23ff702012-09-04 11:03:15 +00001014 ret = -ESRCH;
1015 rcu_read_lock();
Eric W. Biederman098a6972015-03-13 00:07:44 -05001016 for (tm = rcu_dereference(tcp_metrics_hash[hash].chain); tm;
Julian Anastasovd23ff702012-09-04 11:03:15 +00001017 tm = rcu_dereference(tm->tcpm_next)) {
Christoph Paasch3e7013d2014-01-08 16:05:59 +01001018 if (addr_same(&tm->tcpm_daddr, &daddr) &&
Eric W. Biederman849e8a02015-03-13 00:05:52 -05001019 (!src || addr_same(&tm->tcpm_saddr, &saddr)) &&
1020 net_eq(tm_net(tm), net)) {
Julian Anastasovd23ff702012-09-04 11:03:15 +00001021 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
1032nla_put_failure:
1033 ret = -EMSGSIZE;
1034
1035out_free:
1036 nlmsg_free(msg);
1037 return ret;
1038}
1039
Eric W. Biederman8a4bff72015-03-13 00:06:43 -05001040static void tcp_metrics_flush_all(struct net *net)
Julian Anastasovd23ff702012-09-04 11:03:15 +00001041{
Eric W. Biederman098a6972015-03-13 00:07:44 -05001042 unsigned int max_rows = 1U << tcp_metrics_hash_log;
1043 struct tcpm_hash_bucket *hb = tcp_metrics_hash;
Julian Anastasovd23ff702012-09-04 11:03:15 +00001044 struct tcp_metrics_block *tm;
1045 unsigned int row;
1046
1047 for (row = 0; row < max_rows; row++, hb++) {
Eric W. Biederman04f721c2015-03-13 00:07:10 -05001048 struct tcp_metrics_block __rcu **pp;
Julian Anastasovd23ff702012-09-04 11:03:15 +00001049 spin_lock_bh(&tcp_metrics_lock);
Eric W. Biederman04f721c2015-03-13 00:07:10 -05001050 pp = &hb->chain;
Eric Dumazet9f1ab182015-03-16 07:14:34 -07001051 for (tm = deref_locked(*pp); tm; tm = deref_locked(*pp)) {
Eric W. Biederman04f721c2015-03-13 00:07:10 -05001052 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 Anastasovd23ff702012-09-04 11:03:15 +00001058 }
Eric W. Biederman04f721c2015-03-13 00:07:10 -05001059 spin_unlock_bh(&tcp_metrics_lock);
Julian Anastasovd23ff702012-09-04 11:03:15 +00001060 }
Julian Anastasovd23ff702012-09-04 11:03:15 +00001061}
1062
1063static int tcp_metrics_nl_cmd_del(struct sk_buff *skb, struct genl_info *info)
1064{
1065 struct tcpm_hash_bucket *hb;
Christoph Paasch00ca9c52014-01-21 13:30:26 +01001066 struct tcp_metrics_block *tm;
Julian Anastasovd23ff702012-09-04 11:03:15 +00001067 struct tcp_metrics_block __rcu **pp;
Christoph Paasch3e7013d2014-01-08 16:05:59 +01001068 struct inetpeer_addr saddr, daddr;
Julian Anastasovd23ff702012-09-04 11:03:15 +00001069 unsigned int hash;
1070 struct net *net = genl_info_net(info);
1071 int ret;
Christoph Paasch00ca9c52014-01-21 13:30:26 +01001072 bool src = true, found = false;
Julian Anastasovd23ff702012-09-04 11:03:15 +00001073
Christoph Paasch324fd552014-01-08 16:05:55 +01001074 ret = parse_nl_addr(info, &daddr, &hash, 1);
Julian Anastasovd23ff702012-09-04 11:03:15 +00001075 if (ret < 0)
1076 return ret;
Eric W. Biederman8a4bff72015-03-13 00:06:43 -05001077 if (ret > 0) {
1078 tcp_metrics_flush_all(net);
1079 return 0;
1080 }
Christoph Paasch3e7013d2014-01-08 16:05:59 +01001081 ret = parse_nl_saddr(info, &saddr);
1082 if (ret < 0)
1083 src = false;
Julian Anastasovd23ff702012-09-04 11:03:15 +00001084
Eric W. Biederman3e5da622015-03-13 00:05:24 -05001085 hash ^= net_hash_mix(net);
Eric W. Biederman098a6972015-03-13 00:07:44 -05001086 hash = hash_32(hash, tcp_metrics_hash_log);
1087 hb = tcp_metrics_hash + hash;
Julian Anastasovd23ff702012-09-04 11:03:15 +00001088 pp = &hb->chain;
1089 spin_lock_bh(&tcp_metrics_lock);
Eric Dumazet9f1ab182015-03-16 07:14:34 -07001090 for (tm = deref_locked(*pp); tm; tm = deref_locked(*pp)) {
Christoph Paasch3e7013d2014-01-08 16:05:59 +01001091 if (addr_same(&tm->tcpm_daddr, &daddr) &&
Eric W. Biederman849e8a02015-03-13 00:05:52 -05001092 (!src || addr_same(&tm->tcpm_saddr, &saddr)) &&
1093 net_eq(tm_net(tm), net)) {
Julian Anastasovd23ff702012-09-04 11:03:15 +00001094 *pp = tm->tcpm_next;
Christoph Paasch00ca9c52014-01-21 13:30:26 +01001095 kfree_rcu(tm, rcu_head);
1096 found = true;
Christoph Paaschbbf852b2014-01-08 16:05:58 +01001097 } else {
1098 pp = &tm->tcpm_next;
Julian Anastasovd23ff702012-09-04 11:03:15 +00001099 }
1100 }
1101 spin_unlock_bh(&tcp_metrics_lock);
Christoph Paasch00ca9c52014-01-21 13:30:26 +01001102 if (!found)
Julian Anastasovd23ff702012-09-04 11:03:15 +00001103 return -ESRCH;
Julian Anastasovd23ff702012-09-04 11:03:15 +00001104 return 0;
1105}
1106
Johannes Berg4534de82013-11-14 17:14:46 +01001107static const struct genl_ops tcp_metrics_nl_ops[] = {
Julian Anastasovd23ff702012-09-04 11:03:15 +00001108 {
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 Anastasovd23ff702012-09-04 11:03:15 +00001113 },
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 Dumazet5815d5e2012-07-19 23:02:34 +00001122static unsigned int tcpmhash_entries;
David S. Miller51c5d0c2012-07-10 00:49:14 -07001123static int __init set_tcpmhash_entries(char *str)
1124{
1125 ssize_t ret;
1126
1127 if (!str)
1128 return 0;
1129
Eric Dumazet5815d5e2012-07-19 23:02:34 +00001130 ret = kstrtouint(str, 0, &tcpmhash_entries);
David S. Miller51c5d0c2012-07-10 00:49:14 -07001131 if (ret)
1132 return 0;
1133
1134 return 1;
1135}
1136__setup("tcpmhash_entries=", set_tcpmhash_entries);
1137
1138static int __net_init tcp_net_metrics_init(struct net *net)
1139{
Eric Dumazet5815d5e2012-07-19 23:02:34 +00001140 size_t size;
1141 unsigned int slots;
David S. Miller51c5d0c2012-07-10 00:49:14 -07001142
Eric W. Biederman098a6972015-03-13 00:07:44 -05001143 if (!net_eq(net, &init_net))
1144 return 0;
1145
David S. Miller51c5d0c2012-07-10 00:49:14 -07001146 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. Biederman098a6972015-03-13 00:07:44 -05001154 tcp_metrics_hash_log = order_base_2(slots);
1155 size = sizeof(struct tcpm_hash_bucket) << tcp_metrics_hash_log;
David S. Miller51c5d0c2012-07-10 00:49:14 -07001156
Eric W. Biederman098a6972015-03-13 00:07:44 -05001157 tcp_metrics_hash = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
1158 if (!tcp_metrics_hash)
1159 tcp_metrics_hash = vzalloc(size);
Eric Dumazet976a7022012-11-16 05:31:53 +00001160
Eric W. Biederman098a6972015-03-13 00:07:44 -05001161 if (!tcp_metrics_hash)
David S. Miller51c5d0c2012-07-10 00:49:14 -07001162 return -ENOMEM;
1163
David S. Miller51c5d0c2012-07-10 00:49:14 -07001164 return 0;
1165}
1166
1167static void __net_exit tcp_net_metrics_exit(struct net *net)
1168{
Eric W. Biederman098a6972015-03-13 00:07:44 -05001169 tcp_metrics_flush_all(net);
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}