blob: 0507b56b6d4b2721ebce42d2a89a6638350c3b74 [file] [log] [blame]
David S. Miller51c5d0c42012-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. Miller51c5d0c42012-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. Miller51c5d0c42012-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. Miller51c5d0c42012-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. Miller51c5d0c42012-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
David S. Miller41804422014-01-18 00:55:41 -080023static struct tcp_metrics_block *__tcp_get_metrics(const struct inetpeer_addr *saddr,
24 const struct inetpeer_addr *daddr,
Christoph Paasch77f99ad2014-01-16 20:01:21 +010025 struct net *net, unsigned int hash);
26
Yuchung Cheng1fe4c482012-07-19 06:43:06 +000027struct tcp_fastopen_metrics {
28 u16 mss;
Daniel Lee2646c832015-04-06 14:37:27 -070029 u16 syn_loss:10, /* Recurring Fast Open SYN losses */
30 try_exp:2; /* Request w/ exp. option (once) */
Yuchung Chengaab48742012-07-19 06:43:10 +000031 unsigned long last_syn_loss; /* Last Fast Open SYN loss */
Yuchung Cheng1fe4c482012-07-19 06:43:06 +000032 struct tcp_fastopen_cookie cookie;
33};
34
Eric Dumazet740b0f12014-02-26 14:02:48 -080035/* TCP_METRIC_MAX includes 2 extra fields for userspace compatibility
36 * Kernel only stores RTT and RTTVAR in usec resolution
37 */
38#define TCP_METRIC_MAX_KERNEL (TCP_METRIC_MAX - 2)
39
David S. Miller51c5d0c42012-07-10 00:49:14 -070040struct tcp_metrics_block {
41 struct tcp_metrics_block __rcu *tcpm_next;
Eric W. Biederman849e8a02015-03-13 00:05:52 -050042 possible_net_t tcpm_net;
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. Miller51c5d0c42012-07-10 00:49:14 -070045 unsigned long tcpm_stamp;
46 u32 tcpm_lock;
Eric Dumazet740b0f12014-02-26 14:02:48 -080047 u32 tcpm_vals[TCP_METRIC_MAX_KERNEL + 1];
Yuchung Cheng1fe4c482012-07-19 06:43:06 +000048 struct tcp_fastopen_metrics tcpm_fastopen;
Julian Anastasovd23ff702012-09-04 11:03:15 +000049
50 struct rcu_head rcu_head;
David S. Miller51c5d0c42012-07-10 00:49:14 -070051};
52
Eric W. Biederman849e8a02015-03-13 00:05:52 -050053static inline struct net *tm_net(struct tcp_metrics_block *tm)
54{
55 return read_pnet(&tm->tcpm_net);
56}
57
David S. Miller51c5d0c42012-07-10 00:49:14 -070058static bool tcp_metric_locked(struct tcp_metrics_block *tm,
59 enum tcp_metric_index idx)
60{
61 return tm->tcpm_lock & (1 << idx);
62}
63
64static u32 tcp_metric_get(struct tcp_metrics_block *tm,
65 enum tcp_metric_index idx)
66{
67 return tm->tcpm_vals[idx];
68}
69
David S. Miller51c5d0c42012-07-10 00:49:14 -070070static void tcp_metric_set(struct tcp_metrics_block *tm,
71 enum tcp_metric_index idx,
72 u32 val)
73{
74 tm->tcpm_vals[idx] = val;
75}
76
David S. Miller51c5d0c42012-07-10 00:49:14 -070077static bool addr_same(const struct inetpeer_addr *a,
78 const struct inetpeer_addr *b)
79{
David Ahernd39d14f2015-08-27 16:07:01 -070080 return inetpeer_addr_cmp(a, b) == 0;
David S. Miller51c5d0c42012-07-10 00:49:14 -070081}
82
83struct tcpm_hash_bucket {
84 struct tcp_metrics_block __rcu *chain;
85};
86
Eric W. Biederman098a6972015-03-13 00:07:44 -050087static struct tcpm_hash_bucket *tcp_metrics_hash __read_mostly;
88static unsigned int tcp_metrics_hash_log __read_mostly;
89
David S. Miller51c5d0c42012-07-10 00:49:14 -070090static DEFINE_SPINLOCK(tcp_metrics_lock);
91
Eric Dumazet740b0f12014-02-26 14:02:48 -080092static void tcpm_suck_dst(struct tcp_metrics_block *tm,
93 const struct dst_entry *dst,
Eric Dumazetefeaa552013-05-03 19:12:45 +000094 bool fastopen_clear)
David S. Miller51c5d0c42012-07-10 00:49:14 -070095{
Eric Dumazet740b0f12014-02-26 14:02:48 -080096 u32 msval;
David S. Miller51c5d0c42012-07-10 00:49:14 -070097 u32 val;
98
Julian Anastasov9a0a9502012-07-23 10:46:38 +030099 tm->tcpm_stamp = jiffies;
100
David S. Miller51c5d0c42012-07-10 00:49:14 -0700101 val = 0;
102 if (dst_metric_locked(dst, RTAX_RTT))
103 val |= 1 << TCP_METRIC_RTT;
104 if (dst_metric_locked(dst, RTAX_RTTVAR))
105 val |= 1 << TCP_METRIC_RTTVAR;
106 if (dst_metric_locked(dst, RTAX_SSTHRESH))
107 val |= 1 << TCP_METRIC_SSTHRESH;
108 if (dst_metric_locked(dst, RTAX_CWND))
109 val |= 1 << TCP_METRIC_CWND;
110 if (dst_metric_locked(dst, RTAX_REORDERING))
111 val |= 1 << TCP_METRIC_REORDERING;
112 tm->tcpm_lock = val;
113
Eric Dumazet740b0f12014-02-26 14:02:48 -0800114 msval = dst_metric_raw(dst, RTAX_RTT);
115 tm->tcpm_vals[TCP_METRIC_RTT] = msval * USEC_PER_MSEC;
116
117 msval = dst_metric_raw(dst, RTAX_RTTVAR);
118 tm->tcpm_vals[TCP_METRIC_RTTVAR] = msval * USEC_PER_MSEC;
David S. Miller51c5d0c42012-07-10 00:49:14 -0700119 tm->tcpm_vals[TCP_METRIC_SSTHRESH] = dst_metric_raw(dst, RTAX_SSTHRESH);
120 tm->tcpm_vals[TCP_METRIC_CWND] = dst_metric_raw(dst, RTAX_CWND);
121 tm->tcpm_vals[TCP_METRIC_REORDERING] = dst_metric_raw(dst, RTAX_REORDERING);
Eric Dumazetefeaa552013-05-03 19:12:45 +0000122 if (fastopen_clear) {
123 tm->tcpm_fastopen.mss = 0;
124 tm->tcpm_fastopen.syn_loss = 0;
Daniel Lee2646c832015-04-06 14:37:27 -0700125 tm->tcpm_fastopen.try_exp = 0;
126 tm->tcpm_fastopen.cookie.exp = false;
Eric Dumazetefeaa552013-05-03 19:12:45 +0000127 tm->tcpm_fastopen.cookie.len = 0;
128 }
David S. Miller51c5d0c42012-07-10 00:49:14 -0700129}
130
Christoph Paasch77f99ad2014-01-16 20:01:21 +0100131#define TCP_METRICS_TIMEOUT (60 * 60 * HZ)
132
133static void tcpm_check_stamp(struct tcp_metrics_block *tm, struct dst_entry *dst)
134{
135 if (tm && unlikely(time_after(jiffies, tm->tcpm_stamp + TCP_METRICS_TIMEOUT)))
136 tcpm_suck_dst(tm, dst, false);
137}
138
139#define TCP_METRICS_RECLAIM_DEPTH 5
140#define TCP_METRICS_RECLAIM_PTR (struct tcp_metrics_block *) 0x1UL
141
Eric Dumazet9f1ab182015-03-16 07:14:34 -0700142#define deref_locked(p) \
143 rcu_dereference_protected(p, lockdep_is_held(&tcp_metrics_lock))
144
David S. Miller51c5d0c42012-07-10 00:49:14 -0700145static struct tcp_metrics_block *tcpm_new(struct dst_entry *dst,
Christoph Paascha5443022014-01-08 16:05:56 +0100146 struct inetpeer_addr *saddr,
Christoph Paasch324fd552014-01-08 16:05:55 +0100147 struct inetpeer_addr *daddr,
Christoph Paasch77f99ad2014-01-16 20:01:21 +0100148 unsigned int hash)
David S. Miller51c5d0c42012-07-10 00:49:14 -0700149{
150 struct tcp_metrics_block *tm;
151 struct net *net;
Christoph Paasch77f99ad2014-01-16 20:01:21 +0100152 bool reclaim = false;
David S. Miller51c5d0c42012-07-10 00:49:14 -0700153
154 spin_lock_bh(&tcp_metrics_lock);
155 net = dev_net(dst->dev);
Christoph Paasch77f99ad2014-01-16 20:01:21 +0100156
157 /* While waiting for the spin-lock the cache might have been populated
158 * with this entry and so we have to check again.
159 */
David S. Miller41804422014-01-18 00:55:41 -0800160 tm = __tcp_get_metrics(saddr, daddr, net, hash);
Christoph Paasch77f99ad2014-01-16 20:01:21 +0100161 if (tm == TCP_METRICS_RECLAIM_PTR) {
162 reclaim = true;
163 tm = NULL;
164 }
165 if (tm) {
166 tcpm_check_stamp(tm, dst);
167 goto out_unlock;
168 }
169
David S. Miller51c5d0c42012-07-10 00:49:14 -0700170 if (unlikely(reclaim)) {
171 struct tcp_metrics_block *oldest;
172
Eric Dumazet9f1ab182015-03-16 07:14:34 -0700173 oldest = deref_locked(tcp_metrics_hash[hash].chain);
174 for (tm = deref_locked(oldest->tcpm_next); tm;
175 tm = deref_locked(tm->tcpm_next)) {
David S. Miller51c5d0c42012-07-10 00:49:14 -0700176 if (time_before(tm->tcpm_stamp, oldest->tcpm_stamp))
177 oldest = tm;
178 }
179 tm = oldest;
180 } else {
181 tm = kmalloc(sizeof(*tm), GFP_ATOMIC);
182 if (!tm)
183 goto out_unlock;
184 }
Eric W. Biederman849e8a02015-03-13 00:05:52 -0500185 write_pnet(&tm->tcpm_net, net);
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. Miller51c5d0c42012-07-10 00:49:14 -0700188
Eric Dumazetefeaa552013-05-03 19:12:45 +0000189 tcpm_suck_dst(tm, dst, true);
David S. Miller51c5d0c42012-07-10 00:49:14 -0700190
191 if (likely(!reclaim)) {
Eric W. Biederman098a6972015-03-13 00:07:44 -0500192 tm->tcpm_next = tcp_metrics_hash[hash].chain;
193 rcu_assign_pointer(tcp_metrics_hash[hash].chain, tm);
David S. Miller51c5d0c42012-07-10 00:49:14 -0700194 }
195
196out_unlock:
197 spin_unlock_bh(&tcp_metrics_lock);
198 return tm;
199}
200
David S. Miller51c5d0c42012-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. Miller51c5d0c42012-07-10 00:49:14 -0700212 struct net *net, unsigned int hash)
213{
214 struct tcp_metrics_block *tm;
215 int depth = 0;
216
Eric W. Biederman098a6972015-03-13 00:07:44 -0500217 for (tm = rcu_dereference(tcp_metrics_hash[hash].chain); tm;
David S. Miller51c5d0c42012-07-10 00:49:14 -0700218 tm = rcu_dereference(tm->tcpm_next)) {
Christoph Paascha5443022014-01-08 16:05:56 +0100219 if (addr_same(&tm->tcpm_saddr, saddr) &&
Eric W. Biederman849e8a02015-03-13 00:05:52 -0500220 addr_same(&tm->tcpm_daddr, daddr) &&
221 net_eq(tm_net(tm), net))
David S. Miller51c5d0c42012-07-10 00:49:14 -0700222 break;
223 depth++;
224 }
225 return tcp_get_encode(tm, depth);
226}
227
228static struct tcp_metrics_block *__tcp_get_metrics_req(struct request_sock *req,
229 struct dst_entry *dst)
230{
231 struct tcp_metrics_block *tm;
Christoph Paascha5443022014-01-08 16:05:56 +0100232 struct inetpeer_addr saddr, daddr;
David S. Miller51c5d0c42012-07-10 00:49:14 -0700233 unsigned int hash;
234 struct net *net;
235
Christoph Paascha5443022014-01-08 16:05:56 +0100236 saddr.family = req->rsk_ops->family;
Christoph Paasch324fd552014-01-08 16:05:55 +0100237 daddr.family = req->rsk_ops->family;
238 switch (daddr.family) {
David S. Miller51c5d0c42012-07-10 00:49:14 -0700239 case AF_INET:
David Ahern3abef282015-08-27 16:07:00 -0700240 inetpeer_set_addr_v4(&saddr, inet_rsk(req)->ir_loc_addr);
241 inetpeer_set_addr_v4(&daddr, inet_rsk(req)->ir_rmt_addr);
David Ahern72afa352015-08-27 16:06:59 -0700242 hash = ipv4_addr_hash(inet_rsk(req)->ir_rmt_addr);
David S. Miller51c5d0c42012-07-10 00:49:14 -0700243 break;
Eric Dumazet634fb9792013-10-09 15:21:29 -0700244#if IS_ENABLED(CONFIG_IPV6)
David S. Miller51c5d0c42012-07-10 00:49:14 -0700245 case AF_INET6:
David Ahern3abef282015-08-27 16:07:00 -0700246 inetpeer_set_addr_v6(&saddr, &inet_rsk(req)->ir_v6_loc_addr);
247 inetpeer_set_addr_v6(&daddr, &inet_rsk(req)->ir_v6_rmt_addr);
Eric Dumazet634fb9792013-10-09 15:21:29 -0700248 hash = ipv6_addr_hash(&inet_rsk(req)->ir_v6_rmt_addr);
David S. Miller51c5d0c42012-07-10 00:49:14 -0700249 break;
Eric Dumazet634fb9792013-10-09 15:21:29 -0700250#endif
David S. Miller51c5d0c42012-07-10 00:49:14 -0700251 default:
252 return NULL;
253 }
254
David S. Miller51c5d0c42012-07-10 00:49:14 -0700255 net = dev_net(dst->dev);
Eric W. Biederman3e5da622015-03-13 00:05:24 -0500256 hash ^= net_hash_mix(net);
Eric W. Biederman098a6972015-03-13 00:07:44 -0500257 hash = hash_32(hash, tcp_metrics_hash_log);
David S. Miller51c5d0c42012-07-10 00:49:14 -0700258
Eric W. Biederman098a6972015-03-13 00:07:44 -0500259 for (tm = rcu_dereference(tcp_metrics_hash[hash].chain); tm;
David S. Miller51c5d0c42012-07-10 00:49:14 -0700260 tm = rcu_dereference(tm->tcpm_next)) {
Christoph Paascha5443022014-01-08 16:05:56 +0100261 if (addr_same(&tm->tcpm_saddr, &saddr) &&
Eric W. Biederman849e8a02015-03-13 00:05:52 -0500262 addr_same(&tm->tcpm_daddr, &daddr) &&
263 net_eq(tm_net(tm), net))
David S. Miller51c5d0c42012-07-10 00:49:14 -0700264 break;
265 }
266 tcpm_check_stamp(tm, dst);
267 return tm;
268}
269
270static struct tcp_metrics_block *tcp_get_metrics(struct sock *sk,
271 struct dst_entry *dst,
272 bool create)
273{
274 struct tcp_metrics_block *tm;
Christoph Paascha5443022014-01-08 16:05:56 +0100275 struct inetpeer_addr saddr, daddr;
David S. Miller51c5d0c42012-07-10 00:49:14 -0700276 unsigned int hash;
277 struct net *net;
David S. Miller51c5d0c42012-07-10 00:49:14 -0700278
Christoph Paasch3ad88cf2014-01-22 13:58:44 +0100279 if (sk->sk_family == AF_INET) {
David Ahern3abef282015-08-27 16:07:00 -0700280 inetpeer_set_addr_v4(&saddr, inet_sk(sk)->inet_saddr);
281 inetpeer_set_addr_v4(&daddr, inet_sk(sk)->inet_daddr);
David Ahern72afa352015-08-27 16:06:59 -0700282 hash = ipv4_addr_hash(inet_sk(sk)->inet_daddr);
David S. Miller51c5d0c42012-07-10 00:49:14 -0700283 }
Christoph Paasch3ad88cf2014-01-22 13:58:44 +0100284#if IS_ENABLED(CONFIG_IPV6)
285 else if (sk->sk_family == AF_INET6) {
286 if (ipv6_addr_v4mapped(&sk->sk_v6_daddr)) {
David Ahern3abef282015-08-27 16:07:00 -0700287 inetpeer_set_addr_v4(&saddr, inet_sk(sk)->inet_saddr);
288 inetpeer_set_addr_v4(&daddr, inet_sk(sk)->inet_daddr);
David Ahern72afa352015-08-27 16:06:59 -0700289 hash = ipv4_addr_hash(inet_sk(sk)->inet_daddr);
Christoph Paasch3ad88cf2014-01-22 13:58:44 +0100290 } else {
David Ahern3abef282015-08-27 16:07:00 -0700291 inetpeer_set_addr_v6(&saddr, &sk->sk_v6_rcv_saddr);
292 inetpeer_set_addr_v6(&daddr, &sk->sk_v6_daddr);
Christoph Paasch3ad88cf2014-01-22 13:58:44 +0100293 hash = ipv6_addr_hash(&sk->sk_v6_daddr);
294 }
295 }
296#endif
297 else
298 return NULL;
David S. Miller51c5d0c42012-07-10 00:49:14 -0700299
David S. Miller51c5d0c42012-07-10 00:49:14 -0700300 net = dev_net(dst->dev);
Eric W. Biederman3e5da622015-03-13 00:05:24 -0500301 hash ^= net_hash_mix(net);
Eric W. Biederman098a6972015-03-13 00:07:44 -0500302 hash = hash_32(hash, tcp_metrics_hash_log);
David S. Miller51c5d0c42012-07-10 00:49:14 -0700303
Christoph Paascha5443022014-01-08 16:05:56 +0100304 tm = __tcp_get_metrics(&saddr, &daddr, net, hash);
Christoph Paasch77f99ad2014-01-16 20:01:21 +0100305 if (tm == TCP_METRICS_RECLAIM_PTR)
David S. Miller51c5d0c42012-07-10 00:49:14 -0700306 tm = NULL;
David S. Miller51c5d0c42012-07-10 00:49:14 -0700307 if (!tm && create)
David S. Miller41804422014-01-18 00:55:41 -0800308 tm = tcpm_new(dst, &saddr, &daddr, hash);
David S. Miller51c5d0c42012-07-10 00:49:14 -0700309 else
310 tcpm_check_stamp(tm, dst);
311
312 return tm;
313}
314
David S. Miller4aabd8e2012-07-09 16:07:30 -0700315/* Save metrics learned by this TCP session. This function is called
316 * only, when TCP finishes successfully i.e. when it enters TIME-WAIT
317 * or goes from LAST-ACK to CLOSE.
318 */
319void tcp_update_metrics(struct sock *sk)
320{
David S. Miller51c5d0c42012-07-10 00:49:14 -0700321 const struct inet_connection_sock *icsk = inet_csk(sk);
David S. Miller4aabd8e2012-07-09 16:07:30 -0700322 struct dst_entry *dst = __sk_dst_get(sk);
David S. Miller51c5d0c42012-07-10 00:49:14 -0700323 struct tcp_sock *tp = tcp_sk(sk);
Nikolay Borisov1043e252016-02-03 09:46:52 +0200324 struct net *net = sock_net(sk);
David S. Miller51c5d0c42012-07-10 00:49:14 -0700325 struct tcp_metrics_block *tm;
326 unsigned long rtt;
327 u32 val;
328 int m;
David S. Miller4aabd8e2012-07-09 16:07:30 -0700329
Julian Anastasovc3a2e832017-02-06 23:14:14 +0200330 sk_dst_confirm(sk);
Eric Dumazetec36e412017-10-27 07:47:21 -0700331 if (net->ipv4.sysctl_tcp_nometrics_save || !dst)
David S. Miller4aabd8e2012-07-09 16:07:30 -0700332 return;
333
David S. Miller51c5d0c42012-07-10 00:49:14 -0700334 rcu_read_lock();
Eric Dumazet740b0f12014-02-26 14:02:48 -0800335 if (icsk->icsk_backoff || !tp->srtt_us) {
David S. Miller51c5d0c42012-07-10 00:49:14 -0700336 /* This session failed to estimate rtt. Why?
337 * Probably, no packets returned in time. Reset our
338 * results.
David S. Miller4aabd8e2012-07-09 16:07:30 -0700339 */
David S. Miller51c5d0c42012-07-10 00:49:14 -0700340 tm = tcp_get_metrics(sk, dst, false);
341 if (tm && !tcp_metric_locked(tm, TCP_METRIC_RTT))
342 tcp_metric_set(tm, TCP_METRIC_RTT, 0);
343 goto out_unlock;
344 } else
345 tm = tcp_get_metrics(sk, dst, true);
346
347 if (!tm)
348 goto out_unlock;
349
Eric Dumazet740b0f12014-02-26 14:02:48 -0800350 rtt = tcp_metric_get(tm, TCP_METRIC_RTT);
351 m = rtt - tp->srtt_us;
David S. Miller51c5d0c42012-07-10 00:49:14 -0700352
353 /* If newly calculated rtt larger than stored one, store new
354 * one. Otherwise, use EWMA. Remember, rtt overestimation is
355 * always better than underestimation.
356 */
357 if (!tcp_metric_locked(tm, TCP_METRIC_RTT)) {
358 if (m <= 0)
Eric Dumazet740b0f12014-02-26 14:02:48 -0800359 rtt = tp->srtt_us;
David S. Miller51c5d0c42012-07-10 00:49:14 -0700360 else
361 rtt -= (m >> 3);
Eric Dumazet740b0f12014-02-26 14:02:48 -0800362 tcp_metric_set(tm, TCP_METRIC_RTT, rtt);
David S. Miller51c5d0c42012-07-10 00:49:14 -0700363 }
364
365 if (!tcp_metric_locked(tm, TCP_METRIC_RTTVAR)) {
366 unsigned long var;
367
368 if (m < 0)
369 m = -m;
370
371 /* Scale deviation to rttvar fixed point */
372 m >>= 1;
Eric Dumazet740b0f12014-02-26 14:02:48 -0800373 if (m < tp->mdev_us)
374 m = tp->mdev_us;
David S. Miller51c5d0c42012-07-10 00:49:14 -0700375
Eric Dumazet740b0f12014-02-26 14:02:48 -0800376 var = tcp_metric_get(tm, TCP_METRIC_RTTVAR);
David S. Miller51c5d0c42012-07-10 00:49:14 -0700377 if (m >= var)
378 var = m;
379 else
380 var -= (var - m) >> 2;
381
Eric Dumazet740b0f12014-02-26 14:02:48 -0800382 tcp_metric_set(tm, TCP_METRIC_RTTVAR, var);
David S. Miller51c5d0c42012-07-10 00:49:14 -0700383 }
384
385 if (tcp_in_initial_slowstart(tp)) {
386 /* Slow start still did not finish. */
387 if (!tcp_metric_locked(tm, TCP_METRIC_SSTHRESH)) {
388 val = tcp_metric_get(tm, TCP_METRIC_SSTHRESH);
389 if (val && (tp->snd_cwnd >> 1) > val)
390 tcp_metric_set(tm, TCP_METRIC_SSTHRESH,
391 tp->snd_cwnd >> 1);
David S. Miller4aabd8e2012-07-09 16:07:30 -0700392 }
David S. Miller51c5d0c42012-07-10 00:49:14 -0700393 if (!tcp_metric_locked(tm, TCP_METRIC_CWND)) {
394 val = tcp_metric_get(tm, TCP_METRIC_CWND);
395 if (tp->snd_cwnd > val)
396 tcp_metric_set(tm, TCP_METRIC_CWND,
397 tp->snd_cwnd);
David S. Miller4aabd8e2012-07-09 16:07:30 -0700398 }
Yuchung Cheng071d5082015-07-09 13:16:29 -0700399 } else if (!tcp_in_slow_start(tp) &&
David S. Miller51c5d0c42012-07-10 00:49:14 -0700400 icsk->icsk_ca_state == TCP_CA_Open) {
401 /* Cong. avoidance phase, cwnd is reliable. */
402 if (!tcp_metric_locked(tm, TCP_METRIC_SSTHRESH))
403 tcp_metric_set(tm, TCP_METRIC_SSTHRESH,
404 max(tp->snd_cwnd >> 1, tp->snd_ssthresh));
405 if (!tcp_metric_locked(tm, TCP_METRIC_CWND)) {
406 val = tcp_metric_get(tm, TCP_METRIC_CWND);
Alexander Duyck21008442012-07-11 17:18:04 -0700407 tcp_metric_set(tm, TCP_METRIC_CWND, (val + tp->snd_cwnd) >> 1);
David S. Miller4aabd8e2012-07-09 16:07:30 -0700408 }
David S. Miller51c5d0c42012-07-10 00:49:14 -0700409 } else {
410 /* Else slow start did not finish, cwnd is non-sense,
411 * ssthresh may be also invalid.
412 */
413 if (!tcp_metric_locked(tm, TCP_METRIC_CWND)) {
414 val = tcp_metric_get(tm, TCP_METRIC_CWND);
415 tcp_metric_set(tm, TCP_METRIC_CWND,
416 (val + tp->snd_ssthresh) >> 1);
417 }
418 if (!tcp_metric_locked(tm, TCP_METRIC_SSTHRESH)) {
419 val = tcp_metric_get(tm, TCP_METRIC_SSTHRESH);
420 if (val && tp->snd_ssthresh > val)
421 tcp_metric_set(tm, TCP_METRIC_SSTHRESH,
422 tp->snd_ssthresh);
423 }
424 if (!tcp_metric_locked(tm, TCP_METRIC_REORDERING)) {
425 val = tcp_metric_get(tm, TCP_METRIC_REORDERING);
426 if (val < tp->reordering &&
Nikolay Borisov1043e252016-02-03 09:46:52 +0200427 tp->reordering != net->ipv4.sysctl_tcp_reordering)
David S. Miller51c5d0c42012-07-10 00:49:14 -0700428 tcp_metric_set(tm, TCP_METRIC_REORDERING,
429 tp->reordering);
David S. Miller4aabd8e2012-07-09 16:07:30 -0700430 }
431 }
David S. Miller51c5d0c42012-07-10 00:49:14 -0700432 tm->tcpm_stamp = jiffies;
433out_unlock:
434 rcu_read_unlock();
David S. Miller4aabd8e2012-07-09 16:07:30 -0700435}
436
437/* Initialize metrics on socket. */
438
439void tcp_init_metrics(struct sock *sk)
440{
David S. Miller4aabd8e2012-07-09 16:07:30 -0700441 struct dst_entry *dst = __sk_dst_get(sk);
David S. Miller51c5d0c42012-07-10 00:49:14 -0700442 struct tcp_sock *tp = tcp_sk(sk);
443 struct tcp_metrics_block *tm;
Yuchung Cheng1b7fdd22013-08-30 08:35:53 -0700444 u32 val, crtt = 0; /* cached RTT scaled by 8 */
David S. Miller4aabd8e2012-07-09 16:07:30 -0700445
Julian Anastasovc3a2e832017-02-06 23:14:14 +0200446 sk_dst_confirm(sk);
Ian Morris51456b22015-04-03 09:17:26 +0100447 if (!dst)
David S. Miller4aabd8e2012-07-09 16:07:30 -0700448 goto reset;
449
David S. Miller51c5d0c42012-07-10 00:49:14 -0700450 rcu_read_lock();
451 tm = tcp_get_metrics(sk, dst, true);
452 if (!tm) {
453 rcu_read_unlock();
454 goto reset;
455 }
456
457 if (tcp_metric_locked(tm, TCP_METRIC_CWND))
458 tp->snd_cwnd_clamp = tcp_metric_get(tm, TCP_METRIC_CWND);
459
460 val = tcp_metric_get(tm, TCP_METRIC_SSTHRESH);
461 if (val) {
462 tp->snd_ssthresh = val;
David S. Miller4aabd8e2012-07-09 16:07:30 -0700463 if (tp->snd_ssthresh > tp->snd_cwnd_clamp)
464 tp->snd_ssthresh = tp->snd_cwnd_clamp;
465 } else {
466 /* ssthresh may have been reduced unnecessarily during.
467 * 3WHS. Restore it back to its initial default.
468 */
469 tp->snd_ssthresh = TCP_INFINITE_SSTHRESH;
470 }
David S. Miller51c5d0c42012-07-10 00:49:14 -0700471 val = tcp_metric_get(tm, TCP_METRIC_REORDERING);
472 if (val && tp->reordering != val) {
David S. Miller4aabd8e2012-07-09 16:07:30 -0700473 tcp_disable_fack(tp);
David S. Miller51c5d0c42012-07-10 00:49:14 -0700474 tp->reordering = val;
David S. Miller4aabd8e2012-07-09 16:07:30 -0700475 }
476
Eric Dumazet740b0f12014-02-26 14:02:48 -0800477 crtt = tcp_metric_get(tm, TCP_METRIC_RTT);
David S. Miller51c5d0c42012-07-10 00:49:14 -0700478 rcu_read_unlock();
David S. Miller4aabd8e2012-07-09 16:07:30 -0700479reset:
Yuchung Cheng52f20e62013-09-03 14:14:35 -0700480 /* The initial RTT measurement from the SYN/SYN-ACK is not ideal
481 * to seed the RTO for later data packets because SYN packets are
482 * small. Use the per-dst cached values to seed the RTO but keep
483 * the RTT estimator variables intact (e.g., srtt, mdev, rttvar).
484 * Later the RTO will be updated immediately upon obtaining the first
485 * data RTT sample (tcp_rtt_estimator()). Hence the cached RTT only
486 * influences the first RTO but not later RTT estimation.
487 *
488 * But if RTT is not available from the SYN (due to retransmits or
489 * syn cookies) or the cache, force a conservative 3secs timeout.
490 *
491 * A bit of theory. RTT is time passed after "normal" sized packet
492 * is sent until it is ACKed. In normal circumstances sending small
493 * packets force peer to delay ACKs and calculation is correct too.
494 * The algorithm is adaptive and, provided we follow specs, it
495 * NEVER underestimate RTT. BUT! If peer tries to make some clever
496 * tricks sort of "quick acks" for time long enough to decrease RTT
497 * to low value, and then abruptly stops to do it and starts to delay
498 * ACKs, wait for troubles.
499 */
Eric Dumazet740b0f12014-02-26 14:02:48 -0800500 if (crtt > tp->srtt_us) {
Neal Cardwell269aa752013-09-16 21:44:20 -0400501 /* Set RTO like tcp_rtt_estimator(), but from cached RTT. */
Konstantin Khlebnikov9bdfb3b2016-02-21 10:12:39 +0300502 crtt /= 8 * USEC_PER_SEC / HZ;
Neal Cardwell269aa752013-09-16 21:44:20 -0400503 inet_csk(sk)->icsk_rto = crtt + max(2 * crtt, tcp_rto_min(sk));
Eric Dumazet740b0f12014-02-26 14:02:48 -0800504 } else if (tp->srtt_us == 0) {
David S. Miller4aabd8e2012-07-09 16:07:30 -0700505 /* RFC6298: 5.7 We've failed to get a valid RTT sample from
506 * 3WHS. This is most likely due to retransmission,
507 * including spurious one. Reset the RTO back to 3secs
508 * from the more aggressive 1sec to avoid more spurious
509 * retransmission.
510 */
Eric Dumazet740b0f12014-02-26 14:02:48 -0800511 tp->rttvar_us = jiffies_to_usecs(TCP_TIMEOUT_FALLBACK);
512 tp->mdev_us = tp->mdev_max_us = tp->rttvar_us;
513
David S. Miller4aabd8e2012-07-09 16:07:30 -0700514 inet_csk(sk)->icsk_rto = TCP_TIMEOUT_FALLBACK;
515 }
516 /* Cut cwnd down to 1 per RFC5681 if SYN or SYN-ACK has been
517 * retransmitted. In light of RFC6298 more aggressive 1sec
518 * initRTO, we only reset cwnd when more than 1 SYN/SYN-ACK
519 * retransmission has occurred.
520 */
521 if (tp->total_retrans > 1)
522 tp->snd_cwnd = 1;
523 else
524 tp->snd_cwnd = tcp_init_cwnd(tp, dst);
Eric Dumazetc2203cf2017-05-16 14:00:04 -0700525 tp->snd_cwnd_stamp = tcp_jiffies32;
David S. Miller4aabd8e2012-07-09 16:07:30 -0700526}
David S. Millerab92bb22012-07-09 16:19:30 -0700527
Soheil Hassas Yeganehd82bae12017-03-15 16:30:45 -0400528bool tcp_peer_is_proven(struct request_sock *req, struct dst_entry *dst)
David S. Millerab92bb22012-07-09 16:19:30 -0700529{
David S. Miller51c5d0c42012-07-10 00:49:14 -0700530 struct tcp_metrics_block *tm;
531 bool ret;
532
David S. Millerab92bb22012-07-09 16:19:30 -0700533 if (!dst)
534 return false;
David S. Miller51c5d0c42012-07-10 00:49:14 -0700535
536 rcu_read_lock();
537 tm = __tcp_get_metrics_req(req, dst);
Soheil Hassas Yeganehd82bae12017-03-15 16:30:45 -0400538 if (tm && tcp_metric_get(tm, TCP_METRIC_RTT))
David S. Miller81166dd2012-07-10 03:14:24 -0700539 ret = true;
Soheil Hassas Yeganehd82bae12017-03-15 16:30:45 -0400540 else
541 ret = false;
David S. Miller81166dd2012-07-10 03:14:24 -0700542 rcu_read_unlock();
543
544 return ret;
545}
546
Yuchung Cheng1fe4c482012-07-19 06:43:06 +0000547static DEFINE_SEQLOCK(fastopen_seqlock);
548
549void tcp_fastopen_cache_get(struct sock *sk, u16 *mss,
Yuchung Chengaab48742012-07-19 06:43:10 +0000550 struct tcp_fastopen_cookie *cookie,
551 int *syn_loss, unsigned long *last_syn_loss)
Yuchung Cheng1fe4c482012-07-19 06:43:06 +0000552{
553 struct tcp_metrics_block *tm;
554
555 rcu_read_lock();
556 tm = tcp_get_metrics(sk, __sk_dst_get(sk), false);
557 if (tm) {
558 struct tcp_fastopen_metrics *tfom = &tm->tcpm_fastopen;
559 unsigned int seq;
560
561 do {
562 seq = read_seqbegin(&fastopen_seqlock);
563 if (tfom->mss)
564 *mss = tfom->mss;
565 *cookie = tfom->cookie;
Daniel Lee2646c832015-04-06 14:37:27 -0700566 if (cookie->len <= 0 && tfom->try_exp == 1)
567 cookie->exp = true;
Yuchung Chengaab48742012-07-19 06:43:10 +0000568 *syn_loss = tfom->syn_loss;
569 *last_syn_loss = *syn_loss ? tfom->last_syn_loss : 0;
Yuchung Cheng1fe4c482012-07-19 06:43:06 +0000570 } while (read_seqretry(&fastopen_seqlock, seq));
571 }
572 rcu_read_unlock();
573}
574
Yuchung Cheng1fe4c482012-07-19 06:43:06 +0000575void tcp_fastopen_cache_set(struct sock *sk, u16 mss,
Daniel Lee2646c832015-04-06 14:37:27 -0700576 struct tcp_fastopen_cookie *cookie, bool syn_lost,
577 u16 try_exp)
Yuchung Cheng1fe4c482012-07-19 06:43:06 +0000578{
Eric Dumazetdccf76c2013-11-13 15:00:46 -0800579 struct dst_entry *dst = __sk_dst_get(sk);
Yuchung Cheng1fe4c482012-07-19 06:43:06 +0000580 struct tcp_metrics_block *tm;
581
Eric Dumazetdccf76c2013-11-13 15:00:46 -0800582 if (!dst)
583 return;
Yuchung Cheng1fe4c482012-07-19 06:43:06 +0000584 rcu_read_lock();
Eric Dumazetdccf76c2013-11-13 15:00:46 -0800585 tm = tcp_get_metrics(sk, dst, true);
Yuchung Cheng1fe4c482012-07-19 06:43:06 +0000586 if (tm) {
587 struct tcp_fastopen_metrics *tfom = &tm->tcpm_fastopen;
588
589 write_seqlock_bh(&fastopen_seqlock);
Yuchung Chengc9686012013-10-29 10:09:05 -0700590 if (mss)
591 tfom->mss = mss;
592 if (cookie && cookie->len > 0)
Yuchung Cheng1fe4c482012-07-19 06:43:06 +0000593 tfom->cookie = *cookie;
Daniel Lee2646c832015-04-06 14:37:27 -0700594 else if (try_exp > tfom->try_exp &&
595 tfom->cookie.len <= 0 && !tfom->cookie.exp)
596 tfom->try_exp = try_exp;
Yuchung Chengaab48742012-07-19 06:43:10 +0000597 if (syn_lost) {
598 ++tfom->syn_loss;
599 tfom->last_syn_loss = jiffies;
600 } else
601 tfom->syn_loss = 0;
Yuchung Cheng1fe4c482012-07-19 06:43:06 +0000602 write_sequnlock_bh(&fastopen_seqlock);
603 }
604 rcu_read_unlock();
605}
606
Johannes Berg489111e2016-10-24 14:40:03 +0200607static struct genl_family tcp_metrics_nl_family;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000608
stephen hemminger4f70c962016-08-31 15:21:37 -0700609static const struct nla_policy tcp_metrics_nl_policy[TCP_METRICS_ATTR_MAX + 1] = {
Julian Anastasovd23ff702012-09-04 11:03:15 +0000610 [TCP_METRICS_ATTR_ADDR_IPV4] = { .type = NLA_U32, },
611 [TCP_METRICS_ATTR_ADDR_IPV6] = { .type = NLA_BINARY,
612 .len = sizeof(struct in6_addr), },
613 /* Following attributes are not received for GET/DEL,
614 * we keep them for reference
615 */
616#if 0
617 [TCP_METRICS_ATTR_AGE] = { .type = NLA_MSECS, },
618 [TCP_METRICS_ATTR_TW_TSVAL] = { .type = NLA_U32, },
619 [TCP_METRICS_ATTR_TW_TS_STAMP] = { .type = NLA_S32, },
620 [TCP_METRICS_ATTR_VALS] = { .type = NLA_NESTED, },
621 [TCP_METRICS_ATTR_FOPEN_MSS] = { .type = NLA_U16, },
622 [TCP_METRICS_ATTR_FOPEN_SYN_DROPS] = { .type = NLA_U16, },
623 [TCP_METRICS_ATTR_FOPEN_SYN_DROP_TS] = { .type = NLA_MSECS, },
624 [TCP_METRICS_ATTR_FOPEN_COOKIE] = { .type = NLA_BINARY,
625 .len = TCP_FASTOPEN_COOKIE_MAX, },
626#endif
627};
628
629/* Add attributes, caller cancels its header on failure */
630static int tcp_metrics_fill_info(struct sk_buff *msg,
631 struct tcp_metrics_block *tm)
632{
633 struct nlattr *nest;
634 int i;
635
Christoph Paasch324fd552014-01-08 16:05:55 +0100636 switch (tm->tcpm_daddr.family) {
Julian Anastasovd23ff702012-09-04 11:03:15 +0000637 case AF_INET:
Jiri Benc930345e2015-03-29 16:59:25 +0200638 if (nla_put_in_addr(msg, TCP_METRICS_ATTR_ADDR_IPV4,
David Ahern3abef282015-08-27 16:07:00 -0700639 inetpeer_get_addr_v4(&tm->tcpm_daddr)) < 0)
Julian Anastasovd23ff702012-09-04 11:03:15 +0000640 goto nla_put_failure;
Jiri Benc930345e2015-03-29 16:59:25 +0200641 if (nla_put_in_addr(msg, TCP_METRICS_ATTR_SADDR_IPV4,
David Ahern3abef282015-08-27 16:07:00 -0700642 inetpeer_get_addr_v4(&tm->tcpm_saddr)) < 0)
Christoph Paasch8a59359c2014-01-08 16:05:57 +0100643 goto nla_put_failure;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000644 break;
645 case AF_INET6:
Jiri Benc930345e2015-03-29 16:59:25 +0200646 if (nla_put_in6_addr(msg, TCP_METRICS_ATTR_ADDR_IPV6,
David Ahern3abef282015-08-27 16:07:00 -0700647 inetpeer_get_addr_v6(&tm->tcpm_daddr)) < 0)
Julian Anastasovd23ff702012-09-04 11:03:15 +0000648 goto nla_put_failure;
Jiri Benc930345e2015-03-29 16:59:25 +0200649 if (nla_put_in6_addr(msg, TCP_METRICS_ATTR_SADDR_IPV6,
David Ahern3abef282015-08-27 16:07:00 -0700650 inetpeer_get_addr_v6(&tm->tcpm_saddr)) < 0)
Christoph Paasch8a59359c2014-01-08 16:05:57 +0100651 goto nla_put_failure;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000652 break;
653 default:
654 return -EAFNOSUPPORT;
655 }
656
657 if (nla_put_msecs(msg, TCP_METRICS_ATTR_AGE,
Nicolas Dichtel2175d872016-04-22 17:31:21 +0200658 jiffies - tm->tcpm_stamp,
659 TCP_METRICS_ATTR_PAD) < 0)
Julian Anastasovd23ff702012-09-04 11:03:15 +0000660 goto nla_put_failure;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000661
662 {
663 int n = 0;
664
665 nest = nla_nest_start(msg, TCP_METRICS_ATTR_VALS);
666 if (!nest)
667 goto nla_put_failure;
Eric Dumazet740b0f12014-02-26 14:02:48 -0800668 for (i = 0; i < TCP_METRIC_MAX_KERNEL + 1; i++) {
669 u32 val = tm->tcpm_vals[i];
670
671 if (!val)
Julian Anastasovd23ff702012-09-04 11:03:15 +0000672 continue;
Eric Dumazet740b0f12014-02-26 14:02:48 -0800673 if (i == TCP_METRIC_RTT) {
674 if (nla_put_u32(msg, TCP_METRIC_RTT_US + 1,
675 val) < 0)
676 goto nla_put_failure;
677 n++;
678 val = max(val / 1000, 1U);
679 }
680 if (i == TCP_METRIC_RTTVAR) {
681 if (nla_put_u32(msg, TCP_METRIC_RTTVAR_US + 1,
682 val) < 0)
683 goto nla_put_failure;
684 n++;
685 val = max(val / 1000, 1U);
686 }
687 if (nla_put_u32(msg, i + 1, val) < 0)
Julian Anastasovd23ff702012-09-04 11:03:15 +0000688 goto nla_put_failure;
689 n++;
690 }
691 if (n)
692 nla_nest_end(msg, nest);
693 else
694 nla_nest_cancel(msg, nest);
695 }
696
697 {
698 struct tcp_fastopen_metrics tfom_copy[1], *tfom;
699 unsigned int seq;
700
701 do {
702 seq = read_seqbegin(&fastopen_seqlock);
703 tfom_copy[0] = tm->tcpm_fastopen;
704 } while (read_seqretry(&fastopen_seqlock, seq));
705
706 tfom = tfom_copy;
707 if (tfom->mss &&
708 nla_put_u16(msg, TCP_METRICS_ATTR_FOPEN_MSS,
709 tfom->mss) < 0)
710 goto nla_put_failure;
711 if (tfom->syn_loss &&
712 (nla_put_u16(msg, TCP_METRICS_ATTR_FOPEN_SYN_DROPS,
713 tfom->syn_loss) < 0 ||
714 nla_put_msecs(msg, TCP_METRICS_ATTR_FOPEN_SYN_DROP_TS,
Nicolas Dichtel2175d872016-04-22 17:31:21 +0200715 jiffies - tfom->last_syn_loss,
716 TCP_METRICS_ATTR_PAD) < 0))
Julian Anastasovd23ff702012-09-04 11:03:15 +0000717 goto nla_put_failure;
718 if (tfom->cookie.len > 0 &&
719 nla_put(msg, TCP_METRICS_ATTR_FOPEN_COOKIE,
720 tfom->cookie.len, tfom->cookie.val) < 0)
721 goto nla_put_failure;
722 }
723
724 return 0;
725
726nla_put_failure:
727 return -EMSGSIZE;
728}
729
730static int tcp_metrics_dump_info(struct sk_buff *skb,
731 struct netlink_callback *cb,
732 struct tcp_metrics_block *tm)
733{
734 void *hdr;
735
Eric W. Biederman15e47302012-09-07 20:12:54 +0000736 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Julian Anastasovd23ff702012-09-04 11:03:15 +0000737 &tcp_metrics_nl_family, NLM_F_MULTI,
738 TCP_METRICS_CMD_GET);
739 if (!hdr)
740 return -EMSGSIZE;
741
742 if (tcp_metrics_fill_info(skb, tm) < 0)
743 goto nla_put_failure;
744
Johannes Berg053c0952015-01-16 22:09:00 +0100745 genlmsg_end(skb, hdr);
746 return 0;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000747
748nla_put_failure:
749 genlmsg_cancel(skb, hdr);
750 return -EMSGSIZE;
751}
752
753static int tcp_metrics_nl_dump(struct sk_buff *skb,
754 struct netlink_callback *cb)
755{
756 struct net *net = sock_net(skb->sk);
Eric W. Biederman098a6972015-03-13 00:07:44 -0500757 unsigned int max_rows = 1U << tcp_metrics_hash_log;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000758 unsigned int row, s_row = cb->args[0];
759 int s_col = cb->args[1], col = s_col;
760
761 for (row = s_row; row < max_rows; row++, s_col = 0) {
762 struct tcp_metrics_block *tm;
Eric W. Biederman098a6972015-03-13 00:07:44 -0500763 struct tcpm_hash_bucket *hb = tcp_metrics_hash + row;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000764
765 rcu_read_lock();
766 for (col = 0, tm = rcu_dereference(hb->chain); tm;
767 tm = rcu_dereference(tm->tcpm_next), col++) {
Eric W. Biederman849e8a02015-03-13 00:05:52 -0500768 if (!net_eq(tm_net(tm), net))
769 continue;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000770 if (col < s_col)
771 continue;
772 if (tcp_metrics_dump_info(skb, cb, tm) < 0) {
773 rcu_read_unlock();
774 goto done;
775 }
776 }
777 rcu_read_unlock();
778 }
779
780done:
781 cb->args[0] = row;
782 cb->args[1] = col;
783 return skb->len;
784}
785
Christoph Paasch3e7013d2014-01-08 16:05:59 +0100786static int __parse_nl_addr(struct genl_info *info, struct inetpeer_addr *addr,
787 unsigned int *hash, int optional, int v4, int v6)
Julian Anastasovd23ff702012-09-04 11:03:15 +0000788{
789 struct nlattr *a;
790
Christoph Paasch3e7013d2014-01-08 16:05:59 +0100791 a = info->attrs[v4];
Julian Anastasovd23ff702012-09-04 11:03:15 +0000792 if (a) {
David Ahern3abef282015-08-27 16:07:00 -0700793 inetpeer_set_addr_v4(addr, nla_get_in_addr(a));
Christoph Paasch3e7013d2014-01-08 16:05:59 +0100794 if (hash)
David Ahern3abef282015-08-27 16:07:00 -0700795 *hash = ipv4_addr_hash(inetpeer_get_addr_v4(addr));
Julian Anastasovd23ff702012-09-04 11:03:15 +0000796 return 0;
797 }
Christoph Paasch3e7013d2014-01-08 16:05:59 +0100798 a = info->attrs[v6];
Julian Anastasovd23ff702012-09-04 11:03:15 +0000799 if (a) {
David Ahern3abef282015-08-27 16:07:00 -0700800 struct in6_addr in6;
801
Julian Anastasov2c42a3f2012-10-30 12:03:09 +0000802 if (nla_len(a) != sizeof(struct in6_addr))
Julian Anastasovd23ff702012-09-04 11:03:15 +0000803 return -EINVAL;
David Ahern3abef282015-08-27 16:07:00 -0700804 in6 = nla_get_in6_addr(a);
805 inetpeer_set_addr_v6(addr, &in6);
Christoph Paasch3e7013d2014-01-08 16:05:59 +0100806 if (hash)
David Ahern3abef282015-08-27 16:07:00 -0700807 *hash = ipv6_addr_hash(inetpeer_get_addr_v6(addr));
Julian Anastasovd23ff702012-09-04 11:03:15 +0000808 return 0;
809 }
810 return optional ? 1 : -EAFNOSUPPORT;
811}
812
Christoph Paasch3e7013d2014-01-08 16:05:59 +0100813static int parse_nl_addr(struct genl_info *info, struct inetpeer_addr *addr,
814 unsigned int *hash, int optional)
815{
816 return __parse_nl_addr(info, addr, hash, optional,
817 TCP_METRICS_ATTR_ADDR_IPV4,
818 TCP_METRICS_ATTR_ADDR_IPV6);
819}
820
821static int parse_nl_saddr(struct genl_info *info, struct inetpeer_addr *addr)
822{
823 return __parse_nl_addr(info, addr, NULL, 0,
824 TCP_METRICS_ATTR_SADDR_IPV4,
825 TCP_METRICS_ATTR_SADDR_IPV6);
826}
827
Julian Anastasovd23ff702012-09-04 11:03:15 +0000828static int tcp_metrics_nl_cmd_get(struct sk_buff *skb, struct genl_info *info)
829{
830 struct tcp_metrics_block *tm;
Christoph Paasch3e7013d2014-01-08 16:05:59 +0100831 struct inetpeer_addr saddr, daddr;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000832 unsigned int hash;
833 struct sk_buff *msg;
834 struct net *net = genl_info_net(info);
835 void *reply;
836 int ret;
Christoph Paasch3e7013d2014-01-08 16:05:59 +0100837 bool src = true;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000838
Christoph Paasch324fd552014-01-08 16:05:55 +0100839 ret = parse_nl_addr(info, &daddr, &hash, 0);
Julian Anastasovd23ff702012-09-04 11:03:15 +0000840 if (ret < 0)
841 return ret;
842
Christoph Paasch3e7013d2014-01-08 16:05:59 +0100843 ret = parse_nl_saddr(info, &saddr);
844 if (ret < 0)
845 src = false;
846
Julian Anastasovd23ff702012-09-04 11:03:15 +0000847 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
848 if (!msg)
849 return -ENOMEM;
850
851 reply = genlmsg_put_reply(msg, info, &tcp_metrics_nl_family, 0,
852 info->genlhdr->cmd);
853 if (!reply)
854 goto nla_put_failure;
855
Eric W. Biederman3e5da622015-03-13 00:05:24 -0500856 hash ^= net_hash_mix(net);
Eric W. Biederman098a6972015-03-13 00:07:44 -0500857 hash = hash_32(hash, tcp_metrics_hash_log);
Julian Anastasovd23ff702012-09-04 11:03:15 +0000858 ret = -ESRCH;
859 rcu_read_lock();
Eric W. Biederman098a6972015-03-13 00:07:44 -0500860 for (tm = rcu_dereference(tcp_metrics_hash[hash].chain); tm;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000861 tm = rcu_dereference(tm->tcpm_next)) {
Christoph Paasch3e7013d2014-01-08 16:05:59 +0100862 if (addr_same(&tm->tcpm_daddr, &daddr) &&
Eric W. Biederman849e8a02015-03-13 00:05:52 -0500863 (!src || addr_same(&tm->tcpm_saddr, &saddr)) &&
864 net_eq(tm_net(tm), net)) {
Julian Anastasovd23ff702012-09-04 11:03:15 +0000865 ret = tcp_metrics_fill_info(msg, tm);
866 break;
867 }
868 }
869 rcu_read_unlock();
870 if (ret < 0)
871 goto out_free;
872
873 genlmsg_end(msg, reply);
874 return genlmsg_reply(msg, info);
875
876nla_put_failure:
877 ret = -EMSGSIZE;
878
879out_free:
880 nlmsg_free(msg);
881 return ret;
882}
883
Eric W. Biederman8a4bff72015-03-13 00:06:43 -0500884static void tcp_metrics_flush_all(struct net *net)
Julian Anastasovd23ff702012-09-04 11:03:15 +0000885{
Eric W. Biederman098a6972015-03-13 00:07:44 -0500886 unsigned int max_rows = 1U << tcp_metrics_hash_log;
887 struct tcpm_hash_bucket *hb = tcp_metrics_hash;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000888 struct tcp_metrics_block *tm;
889 unsigned int row;
890
891 for (row = 0; row < max_rows; row++, hb++) {
Eric W. Biederman04f721c2015-03-13 00:07:10 -0500892 struct tcp_metrics_block __rcu **pp;
Eric Dumazet789e6dd2017-09-19 16:27:07 -0700893 bool match;
894
Julian Anastasovd23ff702012-09-04 11:03:15 +0000895 spin_lock_bh(&tcp_metrics_lock);
Eric W. Biederman04f721c2015-03-13 00:07:10 -0500896 pp = &hb->chain;
Eric Dumazet9f1ab182015-03-16 07:14:34 -0700897 for (tm = deref_locked(*pp); tm; tm = deref_locked(*pp)) {
Eric Dumazet789e6dd2017-09-19 16:27:07 -0700898 match = net ? net_eq(tm_net(tm), net) :
899 !atomic_read(&tm_net(tm)->count);
900 if (match) {
Eric W. Biederman04f721c2015-03-13 00:07:10 -0500901 *pp = tm->tcpm_next;
902 kfree_rcu(tm, rcu_head);
903 } else {
904 pp = &tm->tcpm_next;
905 }
Julian Anastasovd23ff702012-09-04 11:03:15 +0000906 }
Eric W. Biederman04f721c2015-03-13 00:07:10 -0500907 spin_unlock_bh(&tcp_metrics_lock);
Julian Anastasovd23ff702012-09-04 11:03:15 +0000908 }
Julian Anastasovd23ff702012-09-04 11:03:15 +0000909}
910
911static int tcp_metrics_nl_cmd_del(struct sk_buff *skb, struct genl_info *info)
912{
913 struct tcpm_hash_bucket *hb;
Christoph Paasch00ca9c52014-01-21 13:30:26 +0100914 struct tcp_metrics_block *tm;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000915 struct tcp_metrics_block __rcu **pp;
Christoph Paasch3e7013d2014-01-08 16:05:59 +0100916 struct inetpeer_addr saddr, daddr;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000917 unsigned int hash;
918 struct net *net = genl_info_net(info);
919 int ret;
Christoph Paasch00ca9c52014-01-21 13:30:26 +0100920 bool src = true, found = false;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000921
Christoph Paasch324fd552014-01-08 16:05:55 +0100922 ret = parse_nl_addr(info, &daddr, &hash, 1);
Julian Anastasovd23ff702012-09-04 11:03:15 +0000923 if (ret < 0)
924 return ret;
Eric W. Biederman8a4bff72015-03-13 00:06:43 -0500925 if (ret > 0) {
926 tcp_metrics_flush_all(net);
927 return 0;
928 }
Christoph Paasch3e7013d2014-01-08 16:05:59 +0100929 ret = parse_nl_saddr(info, &saddr);
930 if (ret < 0)
931 src = false;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000932
Eric W. Biederman3e5da622015-03-13 00:05:24 -0500933 hash ^= net_hash_mix(net);
Eric W. Biederman098a6972015-03-13 00:07:44 -0500934 hash = hash_32(hash, tcp_metrics_hash_log);
935 hb = tcp_metrics_hash + hash;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000936 pp = &hb->chain;
937 spin_lock_bh(&tcp_metrics_lock);
Eric Dumazet9f1ab182015-03-16 07:14:34 -0700938 for (tm = deref_locked(*pp); tm; tm = deref_locked(*pp)) {
Christoph Paasch3e7013d2014-01-08 16:05:59 +0100939 if (addr_same(&tm->tcpm_daddr, &daddr) &&
Eric W. Biederman849e8a02015-03-13 00:05:52 -0500940 (!src || addr_same(&tm->tcpm_saddr, &saddr)) &&
941 net_eq(tm_net(tm), net)) {
Julian Anastasovd23ff702012-09-04 11:03:15 +0000942 *pp = tm->tcpm_next;
Christoph Paasch00ca9c52014-01-21 13:30:26 +0100943 kfree_rcu(tm, rcu_head);
944 found = true;
Christoph Paaschbbf852b2014-01-08 16:05:58 +0100945 } else {
946 pp = &tm->tcpm_next;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000947 }
948 }
949 spin_unlock_bh(&tcp_metrics_lock);
Christoph Paasch00ca9c52014-01-21 13:30:26 +0100950 if (!found)
Julian Anastasovd23ff702012-09-04 11:03:15 +0000951 return -ESRCH;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000952 return 0;
953}
954
Johannes Berg4534de82013-11-14 17:14:46 +0100955static const struct genl_ops tcp_metrics_nl_ops[] = {
Julian Anastasovd23ff702012-09-04 11:03:15 +0000956 {
957 .cmd = TCP_METRICS_CMD_GET,
958 .doit = tcp_metrics_nl_cmd_get,
959 .dumpit = tcp_metrics_nl_dump,
960 .policy = tcp_metrics_nl_policy,
Julian Anastasovd23ff702012-09-04 11:03:15 +0000961 },
962 {
963 .cmd = TCP_METRICS_CMD_DEL,
964 .doit = tcp_metrics_nl_cmd_del,
965 .policy = tcp_metrics_nl_policy,
966 .flags = GENL_ADMIN_PERM,
967 },
968};
969
Johannes Berg56989f62016-10-24 14:40:05 +0200970static struct genl_family tcp_metrics_nl_family __ro_after_init = {
Johannes Berg489111e2016-10-24 14:40:03 +0200971 .hdrsize = 0,
972 .name = TCP_METRICS_GENL_NAME,
973 .version = TCP_METRICS_GENL_VERSION,
974 .maxattr = TCP_METRICS_ATTR_MAX,
975 .netnsok = true,
976 .module = THIS_MODULE,
977 .ops = tcp_metrics_nl_ops,
978 .n_ops = ARRAY_SIZE(tcp_metrics_nl_ops),
979};
980
Eric Dumazet5815d5e2012-07-19 23:02:34 +0000981static unsigned int tcpmhash_entries;
David S. Miller51c5d0c42012-07-10 00:49:14 -0700982static int __init set_tcpmhash_entries(char *str)
983{
984 ssize_t ret;
985
986 if (!str)
987 return 0;
988
Eric Dumazet5815d5e2012-07-19 23:02:34 +0000989 ret = kstrtouint(str, 0, &tcpmhash_entries);
David S. Miller51c5d0c42012-07-10 00:49:14 -0700990 if (ret)
991 return 0;
992
993 return 1;
994}
995__setup("tcpmhash_entries=", set_tcpmhash_entries);
996
997static int __net_init tcp_net_metrics_init(struct net *net)
998{
Eric Dumazet5815d5e2012-07-19 23:02:34 +0000999 size_t size;
1000 unsigned int slots;
David S. Miller51c5d0c42012-07-10 00:49:14 -07001001
Eric W. Biederman098a6972015-03-13 00:07:44 -05001002 if (!net_eq(net, &init_net))
1003 return 0;
1004
David S. Miller51c5d0c42012-07-10 00:49:14 -07001005 slots = tcpmhash_entries;
1006 if (!slots) {
1007 if (totalram_pages >= 128 * 1024)
1008 slots = 16 * 1024;
1009 else
1010 slots = 8 * 1024;
1011 }
1012
Eric W. Biederman098a6972015-03-13 00:07:44 -05001013 tcp_metrics_hash_log = order_base_2(slots);
1014 size = sizeof(struct tcpm_hash_bucket) << tcp_metrics_hash_log;
David S. Miller51c5d0c42012-07-10 00:49:14 -07001015
Michal Hocko752ade62017-05-08 15:57:27 -07001016 tcp_metrics_hash = kvzalloc(size, GFP_KERNEL);
Eric W. Biederman098a6972015-03-13 00:07:44 -05001017 if (!tcp_metrics_hash)
David S. Miller51c5d0c42012-07-10 00:49:14 -07001018 return -ENOMEM;
1019
David S. Miller51c5d0c42012-07-10 00:49:14 -07001020 return 0;
1021}
1022
Eric Dumazet789e6dd2017-09-19 16:27:07 -07001023static void __net_exit tcp_net_metrics_exit_batch(struct list_head *net_exit_list)
David S. Miller51c5d0c42012-07-10 00:49:14 -07001024{
Eric Dumazet789e6dd2017-09-19 16:27:07 -07001025 tcp_metrics_flush_all(NULL);
David S. Miller51c5d0c42012-07-10 00:49:14 -07001026}
1027
1028static __net_initdata struct pernet_operations tcp_net_metrics_ops = {
Eric Dumazet789e6dd2017-09-19 16:27:07 -07001029 .init = tcp_net_metrics_init,
1030 .exit_batch = tcp_net_metrics_exit_batch,
David S. Miller51c5d0c42012-07-10 00:49:14 -07001031};
1032
1033void __init tcp_metrics_init(void)
1034{
Julian Anastasovd23ff702012-09-04 11:03:15 +00001035 int ret;
1036
1037 ret = register_pernet_subsys(&tcp_net_metrics_ops);
1038 if (ret < 0)
Eric W. Biederman64935172015-03-13 00:04:51 -05001039 panic("Could not allocate the tcp_metrics hash table\n");
1040
Johannes Berg489111e2016-10-24 14:40:03 +02001041 ret = genl_register_family(&tcp_metrics_nl_family);
Julian Anastasovd23ff702012-09-04 11:03:15 +00001042 if (ret < 0)
Eric W. Biederman64935172015-03-13 00:04:51 -05001043 panic("Could not register tcp_metrics generic netlink\n");
David S. Miller51c5d0c42012-07-10 00:49:14 -07001044}