blob: 102b2c90bb807d3a88d31b59324baf72cf901cdf [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;
Daniel Lee2646c832015-04-06 14:37:27 -070031 u16 syn_loss:10, /* Recurring Fast Open SYN losses */
32 try_exp:2; /* Request w/ exp. option (once) */
Yuchung Chengaab48742012-07-19 06:43:10 +000033 unsigned long last_syn_loss; /* Last Fast Open SYN loss */
Yuchung Cheng1fe4c482012-07-19 06:43:06 +000034 struct tcp_fastopen_cookie cookie;
35};
36
Eric Dumazet740b0f12014-02-26 14:02:48 -080037/* TCP_METRIC_MAX includes 2 extra fields for userspace compatibility
38 * Kernel only stores RTT and RTTVAR in usec resolution
39 */
40#define TCP_METRIC_MAX_KERNEL (TCP_METRIC_MAX - 2)
41
David S. Miller51c5d0c2012-07-10 00:49:14 -070042struct tcp_metrics_block {
43 struct tcp_metrics_block __rcu *tcpm_next;
Eric W. Biederman849e8a02015-03-13 00:05:52 -050044 possible_net_t tcpm_net;
Christoph Paascha5443022014-01-08 16:05:56 +010045 struct inetpeer_addr tcpm_saddr;
Christoph Paasch324fd552014-01-08 16:05:55 +010046 struct inetpeer_addr tcpm_daddr;
David S. Miller51c5d0c2012-07-10 00:49:14 -070047 unsigned long tcpm_stamp;
48 u32 tcpm_lock;
Eric Dumazet740b0f12014-02-26 14:02:48 -080049 u32 tcpm_vals[TCP_METRIC_MAX_KERNEL + 1];
Yuchung Cheng1fe4c482012-07-19 06:43:06 +000050 struct tcp_fastopen_metrics tcpm_fastopen;
Julian Anastasovd23ff702012-09-04 11:03:15 +000051
52 struct rcu_head rcu_head;
David S. Miller51c5d0c2012-07-10 00:49:14 -070053};
54
Eric W. Biederman849e8a02015-03-13 00:05:52 -050055static inline struct net *tm_net(struct tcp_metrics_block *tm)
56{
57 return read_pnet(&tm->tcpm_net);
58}
59
David S. Miller51c5d0c2012-07-10 00:49:14 -070060static bool tcp_metric_locked(struct tcp_metrics_block *tm,
61 enum tcp_metric_index idx)
62{
63 return tm->tcpm_lock & (1 << idx);
64}
65
66static u32 tcp_metric_get(struct tcp_metrics_block *tm,
67 enum tcp_metric_index idx)
68{
69 return tm->tcpm_vals[idx];
70}
71
David S. Miller51c5d0c2012-07-10 00:49:14 -070072static void tcp_metric_set(struct tcp_metrics_block *tm,
73 enum tcp_metric_index idx,
74 u32 val)
75{
76 tm->tcpm_vals[idx] = val;
77}
78
David S. Miller51c5d0c2012-07-10 00:49:14 -070079static bool addr_same(const struct inetpeer_addr *a,
80 const struct inetpeer_addr *b)
81{
David Ahernd39d14f2015-08-27 16:07:01 -070082 return inetpeer_addr_cmp(a, b) == 0;
David S. Miller51c5d0c2012-07-10 00:49:14 -070083}
84
85struct tcpm_hash_bucket {
86 struct tcp_metrics_block __rcu *chain;
87};
88
Eric W. Biederman098a6972015-03-13 00:07:44 -050089static struct tcpm_hash_bucket *tcp_metrics_hash __read_mostly;
90static unsigned int tcp_metrics_hash_log __read_mostly;
91
David S. Miller51c5d0c2012-07-10 00:49:14 -070092static DEFINE_SPINLOCK(tcp_metrics_lock);
93
Eric Dumazet740b0f12014-02-26 14:02:48 -080094static void tcpm_suck_dst(struct tcp_metrics_block *tm,
95 const struct dst_entry *dst,
Eric Dumazetefeaa552013-05-03 19:12:45 +000096 bool fastopen_clear)
David S. Miller51c5d0c2012-07-10 00:49:14 -070097{
Eric Dumazet740b0f12014-02-26 14:02:48 -080098 u32 msval;
David S. Miller51c5d0c2012-07-10 00:49:14 -070099 u32 val;
100
Julian Anastasov9a0a9502012-07-23 10:46:38 +0300101 tm->tcpm_stamp = jiffies;
102
David S. Miller51c5d0c2012-07-10 00:49:14 -0700103 val = 0;
104 if (dst_metric_locked(dst, RTAX_RTT))
105 val |= 1 << TCP_METRIC_RTT;
106 if (dst_metric_locked(dst, RTAX_RTTVAR))
107 val |= 1 << TCP_METRIC_RTTVAR;
108 if (dst_metric_locked(dst, RTAX_SSTHRESH))
109 val |= 1 << TCP_METRIC_SSTHRESH;
110 if (dst_metric_locked(dst, RTAX_CWND))
111 val |= 1 << TCP_METRIC_CWND;
112 if (dst_metric_locked(dst, RTAX_REORDERING))
113 val |= 1 << TCP_METRIC_REORDERING;
114 tm->tcpm_lock = val;
115
Eric Dumazet740b0f12014-02-26 14:02:48 -0800116 msval = dst_metric_raw(dst, RTAX_RTT);
117 tm->tcpm_vals[TCP_METRIC_RTT] = msval * USEC_PER_MSEC;
118
119 msval = dst_metric_raw(dst, RTAX_RTTVAR);
120 tm->tcpm_vals[TCP_METRIC_RTTVAR] = msval * USEC_PER_MSEC;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700121 tm->tcpm_vals[TCP_METRIC_SSTHRESH] = dst_metric_raw(dst, RTAX_SSTHRESH);
122 tm->tcpm_vals[TCP_METRIC_CWND] = dst_metric_raw(dst, RTAX_CWND);
123 tm->tcpm_vals[TCP_METRIC_REORDERING] = dst_metric_raw(dst, RTAX_REORDERING);
Eric Dumazetefeaa552013-05-03 19:12:45 +0000124 if (fastopen_clear) {
125 tm->tcpm_fastopen.mss = 0;
126 tm->tcpm_fastopen.syn_loss = 0;
Daniel Lee2646c832015-04-06 14:37:27 -0700127 tm->tcpm_fastopen.try_exp = 0;
128 tm->tcpm_fastopen.cookie.exp = false;
Eric Dumazetefeaa552013-05-03 19:12:45 +0000129 tm->tcpm_fastopen.cookie.len = 0;
130 }
David S. Miller51c5d0c2012-07-10 00:49:14 -0700131}
132
Christoph Paasch77f99ad2014-01-16 20:01:21 +0100133#define TCP_METRICS_TIMEOUT (60 * 60 * HZ)
134
135static void tcpm_check_stamp(struct tcp_metrics_block *tm, struct dst_entry *dst)
136{
137 if (tm && unlikely(time_after(jiffies, tm->tcpm_stamp + TCP_METRICS_TIMEOUT)))
138 tcpm_suck_dst(tm, dst, false);
139}
140
141#define TCP_METRICS_RECLAIM_DEPTH 5
142#define TCP_METRICS_RECLAIM_PTR (struct tcp_metrics_block *) 0x1UL
143
Eric Dumazet9f1ab182015-03-16 07:14:34 -0700144#define deref_locked(p) \
145 rcu_dereference_protected(p, lockdep_is_held(&tcp_metrics_lock))
146
David S. Miller51c5d0c2012-07-10 00:49:14 -0700147static struct tcp_metrics_block *tcpm_new(struct dst_entry *dst,
Christoph Paascha5443022014-01-08 16:05:56 +0100148 struct inetpeer_addr *saddr,
Christoph Paasch324fd552014-01-08 16:05:55 +0100149 struct inetpeer_addr *daddr,
Christoph Paasch77f99ad2014-01-16 20:01:21 +0100150 unsigned int hash)
David S. Miller51c5d0c2012-07-10 00:49:14 -0700151{
152 struct tcp_metrics_block *tm;
153 struct net *net;
Christoph Paasch77f99ad2014-01-16 20:01:21 +0100154 bool reclaim = false;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700155
156 spin_lock_bh(&tcp_metrics_lock);
157 net = dev_net(dst->dev);
Christoph Paasch77f99ad2014-01-16 20:01:21 +0100158
159 /* While waiting for the spin-lock the cache might have been populated
160 * with this entry and so we have to check again.
161 */
David S. Miller41804422014-01-18 00:55:41 -0800162 tm = __tcp_get_metrics(saddr, daddr, net, hash);
Christoph Paasch77f99ad2014-01-16 20:01:21 +0100163 if (tm == TCP_METRICS_RECLAIM_PTR) {
164 reclaim = true;
165 tm = NULL;
166 }
167 if (tm) {
168 tcpm_check_stamp(tm, dst);
169 goto out_unlock;
170 }
171
David S. Miller51c5d0c2012-07-10 00:49:14 -0700172 if (unlikely(reclaim)) {
173 struct tcp_metrics_block *oldest;
174
Eric Dumazet9f1ab182015-03-16 07:14:34 -0700175 oldest = deref_locked(tcp_metrics_hash[hash].chain);
176 for (tm = deref_locked(oldest->tcpm_next); tm;
177 tm = deref_locked(tm->tcpm_next)) {
David S. Miller51c5d0c2012-07-10 00:49:14 -0700178 if (time_before(tm->tcpm_stamp, oldest->tcpm_stamp))
179 oldest = tm;
180 }
181 tm = oldest;
182 } else {
183 tm = kmalloc(sizeof(*tm), GFP_ATOMIC);
184 if (!tm)
185 goto out_unlock;
186 }
Eric W. Biederman849e8a02015-03-13 00:05:52 -0500187 write_pnet(&tm->tcpm_net, net);
Christoph Paascha5443022014-01-08 16:05:56 +0100188 tm->tcpm_saddr = *saddr;
Christoph Paasch324fd552014-01-08 16:05:55 +0100189 tm->tcpm_daddr = *daddr;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700190
Eric Dumazetefeaa552013-05-03 19:12:45 +0000191 tcpm_suck_dst(tm, dst, true);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700192
193 if (likely(!reclaim)) {
Eric W. Biederman098a6972015-03-13 00:07:44 -0500194 tm->tcpm_next = tcp_metrics_hash[hash].chain;
195 rcu_assign_pointer(tcp_metrics_hash[hash].chain, tm);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700196 }
197
198out_unlock:
199 spin_unlock_bh(&tcp_metrics_lock);
200 return tm;
201}
202
David S. Miller51c5d0c2012-07-10 00:49:14 -0700203static struct tcp_metrics_block *tcp_get_encode(struct tcp_metrics_block *tm, int depth)
204{
205 if (tm)
206 return tm;
207 if (depth > TCP_METRICS_RECLAIM_DEPTH)
208 return TCP_METRICS_RECLAIM_PTR;
209 return NULL;
210}
211
Christoph Paascha5443022014-01-08 16:05:56 +0100212static struct tcp_metrics_block *__tcp_get_metrics(const struct inetpeer_addr *saddr,
213 const struct inetpeer_addr *daddr,
David S. Miller51c5d0c2012-07-10 00:49:14 -0700214 struct net *net, unsigned int hash)
215{
216 struct tcp_metrics_block *tm;
217 int depth = 0;
218
Eric W. Biederman098a6972015-03-13 00:07:44 -0500219 for (tm = rcu_dereference(tcp_metrics_hash[hash].chain); tm;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700220 tm = rcu_dereference(tm->tcpm_next)) {
Christoph Paascha5443022014-01-08 16:05:56 +0100221 if (addr_same(&tm->tcpm_saddr, saddr) &&
Eric W. Biederman849e8a02015-03-13 00:05:52 -0500222 addr_same(&tm->tcpm_daddr, daddr) &&
223 net_eq(tm_net(tm), net))
David S. Miller51c5d0c2012-07-10 00:49:14 -0700224 break;
225 depth++;
226 }
227 return tcp_get_encode(tm, depth);
228}
229
230static struct tcp_metrics_block *__tcp_get_metrics_req(struct request_sock *req,
231 struct dst_entry *dst)
232{
233 struct tcp_metrics_block *tm;
Christoph Paascha5443022014-01-08 16:05:56 +0100234 struct inetpeer_addr saddr, daddr;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700235 unsigned int hash;
236 struct net *net;
237
Christoph Paascha5443022014-01-08 16:05:56 +0100238 saddr.family = req->rsk_ops->family;
Christoph Paasch324fd552014-01-08 16:05:55 +0100239 daddr.family = req->rsk_ops->family;
240 switch (daddr.family) {
David S. Miller51c5d0c2012-07-10 00:49:14 -0700241 case AF_INET:
David Ahern3abef282015-08-27 16:07:00 -0700242 inetpeer_set_addr_v4(&saddr, inet_rsk(req)->ir_loc_addr);
243 inetpeer_set_addr_v4(&daddr, inet_rsk(req)->ir_rmt_addr);
David Ahern72afa352015-08-27 16:06:59 -0700244 hash = ipv4_addr_hash(inet_rsk(req)->ir_rmt_addr);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700245 break;
Eric Dumazet634fb9792013-10-09 15:21:29 -0700246#if IS_ENABLED(CONFIG_IPV6)
David S. Miller51c5d0c2012-07-10 00:49:14 -0700247 case AF_INET6:
David Ahern3abef282015-08-27 16:07:00 -0700248 inetpeer_set_addr_v6(&saddr, &inet_rsk(req)->ir_v6_loc_addr);
249 inetpeer_set_addr_v6(&daddr, &inet_rsk(req)->ir_v6_rmt_addr);
Eric Dumazet634fb9792013-10-09 15:21:29 -0700250 hash = ipv6_addr_hash(&inet_rsk(req)->ir_v6_rmt_addr);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700251 break;
Eric Dumazet634fb9792013-10-09 15:21:29 -0700252#endif
David S. Miller51c5d0c2012-07-10 00:49:14 -0700253 default:
254 return NULL;
255 }
256
David S. Miller51c5d0c2012-07-10 00:49:14 -0700257 net = dev_net(dst->dev);
Eric W. Biederman3e5da622015-03-13 00:05:24 -0500258 hash ^= net_hash_mix(net);
Eric W. Biederman098a6972015-03-13 00:07:44 -0500259 hash = hash_32(hash, tcp_metrics_hash_log);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700260
Eric W. Biederman098a6972015-03-13 00:07:44 -0500261 for (tm = rcu_dereference(tcp_metrics_hash[hash].chain); tm;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700262 tm = rcu_dereference(tm->tcpm_next)) {
Christoph Paascha5443022014-01-08 16:05:56 +0100263 if (addr_same(&tm->tcpm_saddr, &saddr) &&
Eric W. Biederman849e8a02015-03-13 00:05:52 -0500264 addr_same(&tm->tcpm_daddr, &daddr) &&
265 net_eq(tm_net(tm), net))
David S. Miller51c5d0c2012-07-10 00:49:14 -0700266 break;
267 }
268 tcpm_check_stamp(tm, dst);
269 return tm;
270}
271
272static struct tcp_metrics_block *tcp_get_metrics(struct sock *sk,
273 struct dst_entry *dst,
274 bool create)
275{
276 struct tcp_metrics_block *tm;
Christoph Paascha5443022014-01-08 16:05:56 +0100277 struct inetpeer_addr saddr, daddr;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700278 unsigned int hash;
279 struct net *net;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700280
Christoph Paasch3ad88cf2014-01-22 13:58:44 +0100281 if (sk->sk_family == AF_INET) {
David Ahern3abef282015-08-27 16:07:00 -0700282 inetpeer_set_addr_v4(&saddr, inet_sk(sk)->inet_saddr);
283 inetpeer_set_addr_v4(&daddr, inet_sk(sk)->inet_daddr);
David Ahern72afa352015-08-27 16:06:59 -0700284 hash = ipv4_addr_hash(inet_sk(sk)->inet_daddr);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700285 }
Christoph Paasch3ad88cf2014-01-22 13:58:44 +0100286#if IS_ENABLED(CONFIG_IPV6)
287 else if (sk->sk_family == AF_INET6) {
288 if (ipv6_addr_v4mapped(&sk->sk_v6_daddr)) {
David Ahern3abef282015-08-27 16:07:00 -0700289 inetpeer_set_addr_v4(&saddr, inet_sk(sk)->inet_saddr);
290 inetpeer_set_addr_v4(&daddr, inet_sk(sk)->inet_daddr);
David Ahern72afa352015-08-27 16:06:59 -0700291 hash = ipv4_addr_hash(inet_sk(sk)->inet_daddr);
Christoph Paasch3ad88cf2014-01-22 13:58:44 +0100292 } else {
David Ahern3abef282015-08-27 16:07:00 -0700293 inetpeer_set_addr_v6(&saddr, &sk->sk_v6_rcv_saddr);
294 inetpeer_set_addr_v6(&daddr, &sk->sk_v6_daddr);
Christoph Paasch3ad88cf2014-01-22 13:58:44 +0100295 hash = ipv6_addr_hash(&sk->sk_v6_daddr);
296 }
297 }
298#endif
299 else
300 return NULL;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700301
David S. Miller51c5d0c2012-07-10 00:49:14 -0700302 net = dev_net(dst->dev);
Eric W. Biederman3e5da622015-03-13 00:05:24 -0500303 hash ^= net_hash_mix(net);
Eric W. Biederman098a6972015-03-13 00:07:44 -0500304 hash = hash_32(hash, tcp_metrics_hash_log);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700305
Christoph Paascha5443022014-01-08 16:05:56 +0100306 tm = __tcp_get_metrics(&saddr, &daddr, net, hash);
Christoph Paasch77f99ad2014-01-16 20:01:21 +0100307 if (tm == TCP_METRICS_RECLAIM_PTR)
David S. Miller51c5d0c2012-07-10 00:49:14 -0700308 tm = NULL;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700309 if (!tm && create)
David S. Miller41804422014-01-18 00:55:41 -0800310 tm = tcpm_new(dst, &saddr, &daddr, hash);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700311 else
312 tcpm_check_stamp(tm, dst);
313
314 return tm;
315}
316
David S. Miller4aabd8e2012-07-09 16:07:30 -0700317/* Save metrics learned by this TCP session. This function is called
318 * only, when TCP finishes successfully i.e. when it enters TIME-WAIT
319 * or goes from LAST-ACK to CLOSE.
320 */
321void tcp_update_metrics(struct sock *sk)
322{
David S. Miller51c5d0c2012-07-10 00:49:14 -0700323 const struct inet_connection_sock *icsk = inet_csk(sk);
David S. Miller4aabd8e2012-07-09 16:07:30 -0700324 struct dst_entry *dst = __sk_dst_get(sk);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700325 struct tcp_sock *tp = tcp_sk(sk);
Nikolay Borisov1043e252016-02-03 09:46:52 +0200326 struct net *net = sock_net(sk);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700327 struct tcp_metrics_block *tm;
328 unsigned long rtt;
329 u32 val;
330 int m;
David S. Miller4aabd8e2012-07-09 16:07:30 -0700331
Julian Anastasovc3a2e832017-02-06 23:14:14 +0200332 sk_dst_confirm(sk);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700333 if (sysctl_tcp_nometrics_save || !dst)
David S. Miller4aabd8e2012-07-09 16:07:30 -0700334 return;
335
David S. Miller51c5d0c2012-07-10 00:49:14 -0700336 rcu_read_lock();
Eric Dumazet740b0f12014-02-26 14:02:48 -0800337 if (icsk->icsk_backoff || !tp->srtt_us) {
David S. Miller51c5d0c2012-07-10 00:49:14 -0700338 /* This session failed to estimate rtt. Why?
339 * Probably, no packets returned in time. Reset our
340 * results.
David S. Miller4aabd8e2012-07-09 16:07:30 -0700341 */
David S. Miller51c5d0c2012-07-10 00:49:14 -0700342 tm = tcp_get_metrics(sk, dst, false);
343 if (tm && !tcp_metric_locked(tm, TCP_METRIC_RTT))
344 tcp_metric_set(tm, TCP_METRIC_RTT, 0);
345 goto out_unlock;
346 } else
347 tm = tcp_get_metrics(sk, dst, true);
348
349 if (!tm)
350 goto out_unlock;
351
Eric Dumazet740b0f12014-02-26 14:02:48 -0800352 rtt = tcp_metric_get(tm, TCP_METRIC_RTT);
353 m = rtt - tp->srtt_us;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700354
355 /* If newly calculated rtt larger than stored one, store new
356 * one. Otherwise, use EWMA. Remember, rtt overestimation is
357 * always better than underestimation.
358 */
359 if (!tcp_metric_locked(tm, TCP_METRIC_RTT)) {
360 if (m <= 0)
Eric Dumazet740b0f12014-02-26 14:02:48 -0800361 rtt = tp->srtt_us;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700362 else
363 rtt -= (m >> 3);
Eric Dumazet740b0f12014-02-26 14:02:48 -0800364 tcp_metric_set(tm, TCP_METRIC_RTT, rtt);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700365 }
366
367 if (!tcp_metric_locked(tm, TCP_METRIC_RTTVAR)) {
368 unsigned long var;
369
370 if (m < 0)
371 m = -m;
372
373 /* Scale deviation to rttvar fixed point */
374 m >>= 1;
Eric Dumazet740b0f12014-02-26 14:02:48 -0800375 if (m < tp->mdev_us)
376 m = tp->mdev_us;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700377
Eric Dumazet740b0f12014-02-26 14:02:48 -0800378 var = tcp_metric_get(tm, TCP_METRIC_RTTVAR);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700379 if (m >= var)
380 var = m;
381 else
382 var -= (var - m) >> 2;
383
Eric Dumazet740b0f12014-02-26 14:02:48 -0800384 tcp_metric_set(tm, TCP_METRIC_RTTVAR, var);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700385 }
386
387 if (tcp_in_initial_slowstart(tp)) {
388 /* Slow start still did not finish. */
389 if (!tcp_metric_locked(tm, TCP_METRIC_SSTHRESH)) {
390 val = tcp_metric_get(tm, TCP_METRIC_SSTHRESH);
391 if (val && (tp->snd_cwnd >> 1) > val)
392 tcp_metric_set(tm, TCP_METRIC_SSTHRESH,
393 tp->snd_cwnd >> 1);
David S. Miller4aabd8e2012-07-09 16:07:30 -0700394 }
David S. Miller51c5d0c2012-07-10 00:49:14 -0700395 if (!tcp_metric_locked(tm, TCP_METRIC_CWND)) {
396 val = tcp_metric_get(tm, TCP_METRIC_CWND);
397 if (tp->snd_cwnd > val)
398 tcp_metric_set(tm, TCP_METRIC_CWND,
399 tp->snd_cwnd);
David S. Miller4aabd8e2012-07-09 16:07:30 -0700400 }
Yuchung Cheng071d5082015-07-09 13:16:29 -0700401 } else if (!tcp_in_slow_start(tp) &&
David S. Miller51c5d0c2012-07-10 00:49:14 -0700402 icsk->icsk_ca_state == TCP_CA_Open) {
403 /* Cong. avoidance phase, cwnd is reliable. */
404 if (!tcp_metric_locked(tm, TCP_METRIC_SSTHRESH))
405 tcp_metric_set(tm, TCP_METRIC_SSTHRESH,
406 max(tp->snd_cwnd >> 1, tp->snd_ssthresh));
407 if (!tcp_metric_locked(tm, TCP_METRIC_CWND)) {
408 val = tcp_metric_get(tm, TCP_METRIC_CWND);
Alexander Duyck21008442012-07-11 17:18:04 -0700409 tcp_metric_set(tm, TCP_METRIC_CWND, (val + tp->snd_cwnd) >> 1);
David S. Miller4aabd8e2012-07-09 16:07:30 -0700410 }
David S. Miller51c5d0c2012-07-10 00:49:14 -0700411 } else {
412 /* Else slow start did not finish, cwnd is non-sense,
413 * ssthresh may be also invalid.
414 */
415 if (!tcp_metric_locked(tm, TCP_METRIC_CWND)) {
416 val = tcp_metric_get(tm, TCP_METRIC_CWND);
417 tcp_metric_set(tm, TCP_METRIC_CWND,
418 (val + tp->snd_ssthresh) >> 1);
419 }
420 if (!tcp_metric_locked(tm, TCP_METRIC_SSTHRESH)) {
421 val = tcp_metric_get(tm, TCP_METRIC_SSTHRESH);
422 if (val && tp->snd_ssthresh > val)
423 tcp_metric_set(tm, TCP_METRIC_SSTHRESH,
424 tp->snd_ssthresh);
425 }
426 if (!tcp_metric_locked(tm, TCP_METRIC_REORDERING)) {
427 val = tcp_metric_get(tm, TCP_METRIC_REORDERING);
428 if (val < tp->reordering &&
Nikolay Borisov1043e252016-02-03 09:46:52 +0200429 tp->reordering != net->ipv4.sysctl_tcp_reordering)
David S. Miller51c5d0c2012-07-10 00:49:14 -0700430 tcp_metric_set(tm, TCP_METRIC_REORDERING,
431 tp->reordering);
David S. Miller4aabd8e2012-07-09 16:07:30 -0700432 }
433 }
David S. Miller51c5d0c2012-07-10 00:49:14 -0700434 tm->tcpm_stamp = jiffies;
435out_unlock:
436 rcu_read_unlock();
David S. Miller4aabd8e2012-07-09 16:07:30 -0700437}
438
439/* Initialize metrics on socket. */
440
441void tcp_init_metrics(struct sock *sk)
442{
David S. Miller4aabd8e2012-07-09 16:07:30 -0700443 struct dst_entry *dst = __sk_dst_get(sk);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700444 struct tcp_sock *tp = tcp_sk(sk);
445 struct tcp_metrics_block *tm;
Yuchung Cheng1b7fdd22013-08-30 08:35:53 -0700446 u32 val, crtt = 0; /* cached RTT scaled by 8 */
David S. Miller4aabd8e2012-07-09 16:07:30 -0700447
Julian Anastasovc3a2e832017-02-06 23:14:14 +0200448 sk_dst_confirm(sk);
Ian Morris51456b22015-04-03 09:17:26 +0100449 if (!dst)
David S. Miller4aabd8e2012-07-09 16:07:30 -0700450 goto reset;
451
David S. Miller51c5d0c2012-07-10 00:49:14 -0700452 rcu_read_lock();
453 tm = tcp_get_metrics(sk, dst, true);
454 if (!tm) {
455 rcu_read_unlock();
456 goto reset;
457 }
458
459 if (tcp_metric_locked(tm, TCP_METRIC_CWND))
460 tp->snd_cwnd_clamp = tcp_metric_get(tm, TCP_METRIC_CWND);
461
462 val = tcp_metric_get(tm, TCP_METRIC_SSTHRESH);
463 if (val) {
464 tp->snd_ssthresh = val;
David S. Miller4aabd8e2012-07-09 16:07:30 -0700465 if (tp->snd_ssthresh > tp->snd_cwnd_clamp)
466 tp->snd_ssthresh = tp->snd_cwnd_clamp;
467 } else {
468 /* ssthresh may have been reduced unnecessarily during.
469 * 3WHS. Restore it back to its initial default.
470 */
471 tp->snd_ssthresh = TCP_INFINITE_SSTHRESH;
472 }
David S. Miller51c5d0c2012-07-10 00:49:14 -0700473 val = tcp_metric_get(tm, TCP_METRIC_REORDERING);
474 if (val && tp->reordering != val) {
David S. Miller4aabd8e2012-07-09 16:07:30 -0700475 tcp_disable_fack(tp);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700476 tp->reordering = val;
David S. Miller4aabd8e2012-07-09 16:07:30 -0700477 }
478
Eric Dumazet740b0f12014-02-26 14:02:48 -0800479 crtt = tcp_metric_get(tm, TCP_METRIC_RTT);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700480 rcu_read_unlock();
David S. Miller4aabd8e2012-07-09 16:07:30 -0700481reset:
Yuchung Cheng52f20e62013-09-03 14:14:35 -0700482 /* The initial RTT measurement from the SYN/SYN-ACK is not ideal
483 * to seed the RTO for later data packets because SYN packets are
484 * small. Use the per-dst cached values to seed the RTO but keep
485 * the RTT estimator variables intact (e.g., srtt, mdev, rttvar).
486 * Later the RTO will be updated immediately upon obtaining the first
487 * data RTT sample (tcp_rtt_estimator()). Hence the cached RTT only
488 * influences the first RTO but not later RTT estimation.
489 *
490 * But if RTT is not available from the SYN (due to retransmits or
491 * syn cookies) or the cache, force a conservative 3secs timeout.
492 *
493 * A bit of theory. RTT is time passed after "normal" sized packet
494 * is sent until it is ACKed. In normal circumstances sending small
495 * packets force peer to delay ACKs and calculation is correct too.
496 * The algorithm is adaptive and, provided we follow specs, it
497 * NEVER underestimate RTT. BUT! If peer tries to make some clever
498 * tricks sort of "quick acks" for time long enough to decrease RTT
499 * to low value, and then abruptly stops to do it and starts to delay
500 * ACKs, wait for troubles.
501 */
Eric Dumazet740b0f12014-02-26 14:02:48 -0800502 if (crtt > tp->srtt_us) {
Neal Cardwell269aa752013-09-16 21:44:20 -0400503 /* Set RTO like tcp_rtt_estimator(), but from cached RTT. */
Konstantin Khlebnikov9bdfb3b2016-02-21 10:12:39 +0300504 crtt /= 8 * USEC_PER_SEC / HZ;
Neal Cardwell269aa752013-09-16 21:44:20 -0400505 inet_csk(sk)->icsk_rto = crtt + max(2 * crtt, tcp_rto_min(sk));
Eric Dumazet740b0f12014-02-26 14:02:48 -0800506 } else if (tp->srtt_us == 0) {
David S. Miller4aabd8e2012-07-09 16:07:30 -0700507 /* RFC6298: 5.7 We've failed to get a valid RTT sample from
508 * 3WHS. This is most likely due to retransmission,
509 * including spurious one. Reset the RTO back to 3secs
510 * from the more aggressive 1sec to avoid more spurious
511 * retransmission.
512 */
Eric Dumazet740b0f12014-02-26 14:02:48 -0800513 tp->rttvar_us = jiffies_to_usecs(TCP_TIMEOUT_FALLBACK);
514 tp->mdev_us = tp->mdev_max_us = tp->rttvar_us;
515
David S. Miller4aabd8e2012-07-09 16:07:30 -0700516 inet_csk(sk)->icsk_rto = TCP_TIMEOUT_FALLBACK;
517 }
518 /* Cut cwnd down to 1 per RFC5681 if SYN or SYN-ACK has been
519 * retransmitted. In light of RFC6298 more aggressive 1sec
520 * initRTO, we only reset cwnd when more than 1 SYN/SYN-ACK
521 * retransmission has occurred.
522 */
523 if (tp->total_retrans > 1)
524 tp->snd_cwnd = 1;
525 else
526 tp->snd_cwnd = tcp_init_cwnd(tp, dst);
Eric Dumazetc2203cf2017-05-16 14:00:04 -0700527 tp->snd_cwnd_stamp = tcp_jiffies32;
David S. Miller4aabd8e2012-07-09 16:07:30 -0700528}
David S. Millerab92bb22012-07-09 16:19:30 -0700529
Soheil Hassas Yeganehd82bae12017-03-15 16:30:45 -0400530bool tcp_peer_is_proven(struct request_sock *req, struct dst_entry *dst)
David S. Millerab92bb22012-07-09 16:19:30 -0700531{
David S. Miller51c5d0c2012-07-10 00:49:14 -0700532 struct tcp_metrics_block *tm;
533 bool ret;
534
David S. Millerab92bb22012-07-09 16:19:30 -0700535 if (!dst)
536 return false;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700537
538 rcu_read_lock();
539 tm = __tcp_get_metrics_req(req, dst);
Soheil Hassas Yeganehd82bae12017-03-15 16:30:45 -0400540 if (tm && tcp_metric_get(tm, TCP_METRIC_RTT))
David S. Miller81166dd2012-07-10 03:14:24 -0700541 ret = true;
Soheil Hassas Yeganehd82bae12017-03-15 16:30:45 -0400542 else
543 ret = false;
David S. Miller81166dd2012-07-10 03:14:24 -0700544 rcu_read_unlock();
545
546 return ret;
547}
548
Yuchung Cheng1fe4c482012-07-19 06:43:06 +0000549static DEFINE_SEQLOCK(fastopen_seqlock);
550
551void tcp_fastopen_cache_get(struct sock *sk, u16 *mss,
Yuchung Chengaab48742012-07-19 06:43:10 +0000552 struct tcp_fastopen_cookie *cookie,
553 int *syn_loss, unsigned long *last_syn_loss)
Yuchung Cheng1fe4c482012-07-19 06:43:06 +0000554{
555 struct tcp_metrics_block *tm;
556
557 rcu_read_lock();
558 tm = tcp_get_metrics(sk, __sk_dst_get(sk), false);
559 if (tm) {
560 struct tcp_fastopen_metrics *tfom = &tm->tcpm_fastopen;
561 unsigned int seq;
562
563 do {
564 seq = read_seqbegin(&fastopen_seqlock);
565 if (tfom->mss)
566 *mss = tfom->mss;
567 *cookie = tfom->cookie;
Daniel Lee2646c832015-04-06 14:37:27 -0700568 if (cookie->len <= 0 && tfom->try_exp == 1)
569 cookie->exp = true;
Yuchung Chengaab48742012-07-19 06:43:10 +0000570 *syn_loss = tfom->syn_loss;
571 *last_syn_loss = *syn_loss ? tfom->last_syn_loss : 0;
Yuchung Cheng1fe4c482012-07-19 06:43:06 +0000572 } while (read_seqretry(&fastopen_seqlock, seq));
573 }
574 rcu_read_unlock();
575}
576
Yuchung Cheng1fe4c482012-07-19 06:43:06 +0000577void tcp_fastopen_cache_set(struct sock *sk, u16 mss,
Daniel Lee2646c832015-04-06 14:37:27 -0700578 struct tcp_fastopen_cookie *cookie, bool syn_lost,
579 u16 try_exp)
Yuchung Cheng1fe4c482012-07-19 06:43:06 +0000580{
Eric Dumazetdccf76c2013-11-13 15:00:46 -0800581 struct dst_entry *dst = __sk_dst_get(sk);
Yuchung Cheng1fe4c482012-07-19 06:43:06 +0000582 struct tcp_metrics_block *tm;
583
Eric Dumazetdccf76c2013-11-13 15:00:46 -0800584 if (!dst)
585 return;
Yuchung Cheng1fe4c482012-07-19 06:43:06 +0000586 rcu_read_lock();
Eric Dumazetdccf76c2013-11-13 15:00:46 -0800587 tm = tcp_get_metrics(sk, dst, true);
Yuchung Cheng1fe4c482012-07-19 06:43:06 +0000588 if (tm) {
589 struct tcp_fastopen_metrics *tfom = &tm->tcpm_fastopen;
590
591 write_seqlock_bh(&fastopen_seqlock);
Yuchung Chengc9686012013-10-29 10:09:05 -0700592 if (mss)
593 tfom->mss = mss;
594 if (cookie && cookie->len > 0)
Yuchung Cheng1fe4c482012-07-19 06:43:06 +0000595 tfom->cookie = *cookie;
Daniel Lee2646c832015-04-06 14:37:27 -0700596 else if (try_exp > tfom->try_exp &&
597 tfom->cookie.len <= 0 && !tfom->cookie.exp)
598 tfom->try_exp = try_exp;
Yuchung Chengaab48742012-07-19 06:43:10 +0000599 if (syn_lost) {
600 ++tfom->syn_loss;
601 tfom->last_syn_loss = jiffies;
602 } else
603 tfom->syn_loss = 0;
Yuchung Cheng1fe4c482012-07-19 06:43:06 +0000604 write_sequnlock_bh(&fastopen_seqlock);
605 }
606 rcu_read_unlock();
607}
608
Johannes Berg489111e2016-10-24 14:40:03 +0200609static struct genl_family tcp_metrics_nl_family;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000610
stephen hemminger4f70c962016-08-31 15:21:37 -0700611static const struct nla_policy tcp_metrics_nl_policy[TCP_METRICS_ATTR_MAX + 1] = {
Julian Anastasovd23ff702012-09-04 11:03:15 +0000612 [TCP_METRICS_ATTR_ADDR_IPV4] = { .type = NLA_U32, },
613 [TCP_METRICS_ATTR_ADDR_IPV6] = { .type = NLA_BINARY,
614 .len = sizeof(struct in6_addr), },
615 /* Following attributes are not received for GET/DEL,
616 * we keep them for reference
617 */
618#if 0
619 [TCP_METRICS_ATTR_AGE] = { .type = NLA_MSECS, },
620 [TCP_METRICS_ATTR_TW_TSVAL] = { .type = NLA_U32, },
621 [TCP_METRICS_ATTR_TW_TS_STAMP] = { .type = NLA_S32, },
622 [TCP_METRICS_ATTR_VALS] = { .type = NLA_NESTED, },
623 [TCP_METRICS_ATTR_FOPEN_MSS] = { .type = NLA_U16, },
624 [TCP_METRICS_ATTR_FOPEN_SYN_DROPS] = { .type = NLA_U16, },
625 [TCP_METRICS_ATTR_FOPEN_SYN_DROP_TS] = { .type = NLA_MSECS, },
626 [TCP_METRICS_ATTR_FOPEN_COOKIE] = { .type = NLA_BINARY,
627 .len = TCP_FASTOPEN_COOKIE_MAX, },
628#endif
629};
630
631/* Add attributes, caller cancels its header on failure */
632static int tcp_metrics_fill_info(struct sk_buff *msg,
633 struct tcp_metrics_block *tm)
634{
635 struct nlattr *nest;
636 int i;
637
Christoph Paasch324fd552014-01-08 16:05:55 +0100638 switch (tm->tcpm_daddr.family) {
Julian Anastasovd23ff702012-09-04 11:03:15 +0000639 case AF_INET:
Jiri Benc930345e2015-03-29 16:59:25 +0200640 if (nla_put_in_addr(msg, TCP_METRICS_ATTR_ADDR_IPV4,
David Ahern3abef282015-08-27 16:07:00 -0700641 inetpeer_get_addr_v4(&tm->tcpm_daddr)) < 0)
Julian Anastasovd23ff702012-09-04 11:03:15 +0000642 goto nla_put_failure;
Jiri Benc930345e2015-03-29 16:59:25 +0200643 if (nla_put_in_addr(msg, TCP_METRICS_ATTR_SADDR_IPV4,
David Ahern3abef282015-08-27 16:07:00 -0700644 inetpeer_get_addr_v4(&tm->tcpm_saddr)) < 0)
Christoph Paasch8a593592014-01-08 16:05:57 +0100645 goto nla_put_failure;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000646 break;
647 case AF_INET6:
Jiri Benc930345e2015-03-29 16:59:25 +0200648 if (nla_put_in6_addr(msg, TCP_METRICS_ATTR_ADDR_IPV6,
David Ahern3abef282015-08-27 16:07:00 -0700649 inetpeer_get_addr_v6(&tm->tcpm_daddr)) < 0)
Julian Anastasovd23ff702012-09-04 11:03:15 +0000650 goto nla_put_failure;
Jiri Benc930345e2015-03-29 16:59:25 +0200651 if (nla_put_in6_addr(msg, TCP_METRICS_ATTR_SADDR_IPV6,
David Ahern3abef282015-08-27 16:07:00 -0700652 inetpeer_get_addr_v6(&tm->tcpm_saddr)) < 0)
Christoph Paasch8a593592014-01-08 16:05:57 +0100653 goto nla_put_failure;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000654 break;
655 default:
656 return -EAFNOSUPPORT;
657 }
658
659 if (nla_put_msecs(msg, TCP_METRICS_ATTR_AGE,
Nicolas Dichtel2175d872016-04-22 17:31:21 +0200660 jiffies - tm->tcpm_stamp,
661 TCP_METRICS_ATTR_PAD) < 0)
Julian Anastasovd23ff702012-09-04 11:03:15 +0000662 goto nla_put_failure;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000663
664 {
665 int n = 0;
666
667 nest = nla_nest_start(msg, TCP_METRICS_ATTR_VALS);
668 if (!nest)
669 goto nla_put_failure;
Eric Dumazet740b0f12014-02-26 14:02:48 -0800670 for (i = 0; i < TCP_METRIC_MAX_KERNEL + 1; i++) {
671 u32 val = tm->tcpm_vals[i];
672
673 if (!val)
Julian Anastasovd23ff702012-09-04 11:03:15 +0000674 continue;
Eric Dumazet740b0f12014-02-26 14:02:48 -0800675 if (i == TCP_METRIC_RTT) {
676 if (nla_put_u32(msg, TCP_METRIC_RTT_US + 1,
677 val) < 0)
678 goto nla_put_failure;
679 n++;
680 val = max(val / 1000, 1U);
681 }
682 if (i == TCP_METRIC_RTTVAR) {
683 if (nla_put_u32(msg, TCP_METRIC_RTTVAR_US + 1,
684 val) < 0)
685 goto nla_put_failure;
686 n++;
687 val = max(val / 1000, 1U);
688 }
689 if (nla_put_u32(msg, i + 1, val) < 0)
Julian Anastasovd23ff702012-09-04 11:03:15 +0000690 goto nla_put_failure;
691 n++;
692 }
693 if (n)
694 nla_nest_end(msg, nest);
695 else
696 nla_nest_cancel(msg, nest);
697 }
698
699 {
700 struct tcp_fastopen_metrics tfom_copy[1], *tfom;
701 unsigned int seq;
702
703 do {
704 seq = read_seqbegin(&fastopen_seqlock);
705 tfom_copy[0] = tm->tcpm_fastopen;
706 } while (read_seqretry(&fastopen_seqlock, seq));
707
708 tfom = tfom_copy;
709 if (tfom->mss &&
710 nla_put_u16(msg, TCP_METRICS_ATTR_FOPEN_MSS,
711 tfom->mss) < 0)
712 goto nla_put_failure;
713 if (tfom->syn_loss &&
714 (nla_put_u16(msg, TCP_METRICS_ATTR_FOPEN_SYN_DROPS,
715 tfom->syn_loss) < 0 ||
716 nla_put_msecs(msg, TCP_METRICS_ATTR_FOPEN_SYN_DROP_TS,
Nicolas Dichtel2175d872016-04-22 17:31:21 +0200717 jiffies - tfom->last_syn_loss,
718 TCP_METRICS_ATTR_PAD) < 0))
Julian Anastasovd23ff702012-09-04 11:03:15 +0000719 goto nla_put_failure;
720 if (tfom->cookie.len > 0 &&
721 nla_put(msg, TCP_METRICS_ATTR_FOPEN_COOKIE,
722 tfom->cookie.len, tfom->cookie.val) < 0)
723 goto nla_put_failure;
724 }
725
726 return 0;
727
728nla_put_failure:
729 return -EMSGSIZE;
730}
731
732static int tcp_metrics_dump_info(struct sk_buff *skb,
733 struct netlink_callback *cb,
734 struct tcp_metrics_block *tm)
735{
736 void *hdr;
737
Eric W. Biederman15e47302012-09-07 20:12:54 +0000738 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Julian Anastasovd23ff702012-09-04 11:03:15 +0000739 &tcp_metrics_nl_family, NLM_F_MULTI,
740 TCP_METRICS_CMD_GET);
741 if (!hdr)
742 return -EMSGSIZE;
743
744 if (tcp_metrics_fill_info(skb, tm) < 0)
745 goto nla_put_failure;
746
Johannes Berg053c0952015-01-16 22:09:00 +0100747 genlmsg_end(skb, hdr);
748 return 0;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000749
750nla_put_failure:
751 genlmsg_cancel(skb, hdr);
752 return -EMSGSIZE;
753}
754
755static int tcp_metrics_nl_dump(struct sk_buff *skb,
756 struct netlink_callback *cb)
757{
758 struct net *net = sock_net(skb->sk);
Eric W. Biederman098a6972015-03-13 00:07:44 -0500759 unsigned int max_rows = 1U << tcp_metrics_hash_log;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000760 unsigned int row, s_row = cb->args[0];
761 int s_col = cb->args[1], col = s_col;
762
763 for (row = s_row; row < max_rows; row++, s_col = 0) {
764 struct tcp_metrics_block *tm;
Eric W. Biederman098a6972015-03-13 00:07:44 -0500765 struct tcpm_hash_bucket *hb = tcp_metrics_hash + row;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000766
767 rcu_read_lock();
768 for (col = 0, tm = rcu_dereference(hb->chain); tm;
769 tm = rcu_dereference(tm->tcpm_next), col++) {
Eric W. Biederman849e8a02015-03-13 00:05:52 -0500770 if (!net_eq(tm_net(tm), net))
771 continue;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000772 if (col < s_col)
773 continue;
774 if (tcp_metrics_dump_info(skb, cb, tm) < 0) {
775 rcu_read_unlock();
776 goto done;
777 }
778 }
779 rcu_read_unlock();
780 }
781
782done:
783 cb->args[0] = row;
784 cb->args[1] = col;
785 return skb->len;
786}
787
Christoph Paasch3e7013d2014-01-08 16:05:59 +0100788static int __parse_nl_addr(struct genl_info *info, struct inetpeer_addr *addr,
789 unsigned int *hash, int optional, int v4, int v6)
Julian Anastasovd23ff702012-09-04 11:03:15 +0000790{
791 struct nlattr *a;
792
Christoph Paasch3e7013d2014-01-08 16:05:59 +0100793 a = info->attrs[v4];
Julian Anastasovd23ff702012-09-04 11:03:15 +0000794 if (a) {
David Ahern3abef282015-08-27 16:07:00 -0700795 inetpeer_set_addr_v4(addr, nla_get_in_addr(a));
Christoph Paasch3e7013d2014-01-08 16:05:59 +0100796 if (hash)
David Ahern3abef282015-08-27 16:07:00 -0700797 *hash = ipv4_addr_hash(inetpeer_get_addr_v4(addr));
Julian Anastasovd23ff702012-09-04 11:03:15 +0000798 return 0;
799 }
Christoph Paasch3e7013d2014-01-08 16:05:59 +0100800 a = info->attrs[v6];
Julian Anastasovd23ff702012-09-04 11:03:15 +0000801 if (a) {
David Ahern3abef282015-08-27 16:07:00 -0700802 struct in6_addr in6;
803
Julian Anastasov2c42a3f2012-10-30 12:03:09 +0000804 if (nla_len(a) != sizeof(struct in6_addr))
Julian Anastasovd23ff702012-09-04 11:03:15 +0000805 return -EINVAL;
David Ahern3abef282015-08-27 16:07:00 -0700806 in6 = nla_get_in6_addr(a);
807 inetpeer_set_addr_v6(addr, &in6);
Christoph Paasch3e7013d2014-01-08 16:05:59 +0100808 if (hash)
David Ahern3abef282015-08-27 16:07:00 -0700809 *hash = ipv6_addr_hash(inetpeer_get_addr_v6(addr));
Julian Anastasovd23ff702012-09-04 11:03:15 +0000810 return 0;
811 }
812 return optional ? 1 : -EAFNOSUPPORT;
813}
814
Christoph Paasch3e7013d2014-01-08 16:05:59 +0100815static int parse_nl_addr(struct genl_info *info, struct inetpeer_addr *addr,
816 unsigned int *hash, int optional)
817{
818 return __parse_nl_addr(info, addr, hash, optional,
819 TCP_METRICS_ATTR_ADDR_IPV4,
820 TCP_METRICS_ATTR_ADDR_IPV6);
821}
822
823static int parse_nl_saddr(struct genl_info *info, struct inetpeer_addr *addr)
824{
825 return __parse_nl_addr(info, addr, NULL, 0,
826 TCP_METRICS_ATTR_SADDR_IPV4,
827 TCP_METRICS_ATTR_SADDR_IPV6);
828}
829
Julian Anastasovd23ff702012-09-04 11:03:15 +0000830static int tcp_metrics_nl_cmd_get(struct sk_buff *skb, struct genl_info *info)
831{
832 struct tcp_metrics_block *tm;
Christoph Paasch3e7013d2014-01-08 16:05:59 +0100833 struct inetpeer_addr saddr, daddr;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000834 unsigned int hash;
835 struct sk_buff *msg;
836 struct net *net = genl_info_net(info);
837 void *reply;
838 int ret;
Christoph Paasch3e7013d2014-01-08 16:05:59 +0100839 bool src = true;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000840
Christoph Paasch324fd552014-01-08 16:05:55 +0100841 ret = parse_nl_addr(info, &daddr, &hash, 0);
Julian Anastasovd23ff702012-09-04 11:03:15 +0000842 if (ret < 0)
843 return ret;
844
Christoph Paasch3e7013d2014-01-08 16:05:59 +0100845 ret = parse_nl_saddr(info, &saddr);
846 if (ret < 0)
847 src = false;
848
Julian Anastasovd23ff702012-09-04 11:03:15 +0000849 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
850 if (!msg)
851 return -ENOMEM;
852
853 reply = genlmsg_put_reply(msg, info, &tcp_metrics_nl_family, 0,
854 info->genlhdr->cmd);
855 if (!reply)
856 goto nla_put_failure;
857
Eric W. Biederman3e5da622015-03-13 00:05:24 -0500858 hash ^= net_hash_mix(net);
Eric W. Biederman098a6972015-03-13 00:07:44 -0500859 hash = hash_32(hash, tcp_metrics_hash_log);
Julian Anastasovd23ff702012-09-04 11:03:15 +0000860 ret = -ESRCH;
861 rcu_read_lock();
Eric W. Biederman098a6972015-03-13 00:07:44 -0500862 for (tm = rcu_dereference(tcp_metrics_hash[hash].chain); tm;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000863 tm = rcu_dereference(tm->tcpm_next)) {
Christoph Paasch3e7013d2014-01-08 16:05:59 +0100864 if (addr_same(&tm->tcpm_daddr, &daddr) &&
Eric W. Biederman849e8a02015-03-13 00:05:52 -0500865 (!src || addr_same(&tm->tcpm_saddr, &saddr)) &&
866 net_eq(tm_net(tm), net)) {
Julian Anastasovd23ff702012-09-04 11:03:15 +0000867 ret = tcp_metrics_fill_info(msg, tm);
868 break;
869 }
870 }
871 rcu_read_unlock();
872 if (ret < 0)
873 goto out_free;
874
875 genlmsg_end(msg, reply);
876 return genlmsg_reply(msg, info);
877
878nla_put_failure:
879 ret = -EMSGSIZE;
880
881out_free:
882 nlmsg_free(msg);
883 return ret;
884}
885
Eric W. Biederman8a4bff72015-03-13 00:06:43 -0500886static void tcp_metrics_flush_all(struct net *net)
Julian Anastasovd23ff702012-09-04 11:03:15 +0000887{
Eric W. Biederman098a6972015-03-13 00:07:44 -0500888 unsigned int max_rows = 1U << tcp_metrics_hash_log;
889 struct tcpm_hash_bucket *hb = tcp_metrics_hash;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000890 struct tcp_metrics_block *tm;
891 unsigned int row;
892
893 for (row = 0; row < max_rows; row++, hb++) {
Eric W. Biederman04f721c2015-03-13 00:07:10 -0500894 struct tcp_metrics_block __rcu **pp;
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 W. Biederman04f721c2015-03-13 00:07:10 -0500898 if (net_eq(tm_net(tm), net)) {
899 *pp = tm->tcpm_next;
900 kfree_rcu(tm, rcu_head);
901 } else {
902 pp = &tm->tcpm_next;
903 }
Julian Anastasovd23ff702012-09-04 11:03:15 +0000904 }
Eric W. Biederman04f721c2015-03-13 00:07:10 -0500905 spin_unlock_bh(&tcp_metrics_lock);
Julian Anastasovd23ff702012-09-04 11:03:15 +0000906 }
Julian Anastasovd23ff702012-09-04 11:03:15 +0000907}
908
909static int tcp_metrics_nl_cmd_del(struct sk_buff *skb, struct genl_info *info)
910{
911 struct tcpm_hash_bucket *hb;
Christoph Paasch00ca9c52014-01-21 13:30:26 +0100912 struct tcp_metrics_block *tm;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000913 struct tcp_metrics_block __rcu **pp;
Christoph Paasch3e7013d2014-01-08 16:05:59 +0100914 struct inetpeer_addr saddr, daddr;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000915 unsigned int hash;
916 struct net *net = genl_info_net(info);
917 int ret;
Christoph Paasch00ca9c52014-01-21 13:30:26 +0100918 bool src = true, found = false;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000919
Christoph Paasch324fd552014-01-08 16:05:55 +0100920 ret = parse_nl_addr(info, &daddr, &hash, 1);
Julian Anastasovd23ff702012-09-04 11:03:15 +0000921 if (ret < 0)
922 return ret;
Eric W. Biederman8a4bff72015-03-13 00:06:43 -0500923 if (ret > 0) {
924 tcp_metrics_flush_all(net);
925 return 0;
926 }
Christoph Paasch3e7013d2014-01-08 16:05:59 +0100927 ret = parse_nl_saddr(info, &saddr);
928 if (ret < 0)
929 src = false;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000930
Eric W. Biederman3e5da622015-03-13 00:05:24 -0500931 hash ^= net_hash_mix(net);
Eric W. Biederman098a6972015-03-13 00:07:44 -0500932 hash = hash_32(hash, tcp_metrics_hash_log);
933 hb = tcp_metrics_hash + hash;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000934 pp = &hb->chain;
935 spin_lock_bh(&tcp_metrics_lock);
Eric Dumazet9f1ab182015-03-16 07:14:34 -0700936 for (tm = deref_locked(*pp); tm; tm = deref_locked(*pp)) {
Christoph Paasch3e7013d2014-01-08 16:05:59 +0100937 if (addr_same(&tm->tcpm_daddr, &daddr) &&
Eric W. Biederman849e8a02015-03-13 00:05:52 -0500938 (!src || addr_same(&tm->tcpm_saddr, &saddr)) &&
939 net_eq(tm_net(tm), net)) {
Julian Anastasovd23ff702012-09-04 11:03:15 +0000940 *pp = tm->tcpm_next;
Christoph Paasch00ca9c52014-01-21 13:30:26 +0100941 kfree_rcu(tm, rcu_head);
942 found = true;
Christoph Paaschbbf852b2014-01-08 16:05:58 +0100943 } else {
944 pp = &tm->tcpm_next;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000945 }
946 }
947 spin_unlock_bh(&tcp_metrics_lock);
Christoph Paasch00ca9c52014-01-21 13:30:26 +0100948 if (!found)
Julian Anastasovd23ff702012-09-04 11:03:15 +0000949 return -ESRCH;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000950 return 0;
951}
952
Johannes Berg4534de82013-11-14 17:14:46 +0100953static const struct genl_ops tcp_metrics_nl_ops[] = {
Julian Anastasovd23ff702012-09-04 11:03:15 +0000954 {
955 .cmd = TCP_METRICS_CMD_GET,
956 .doit = tcp_metrics_nl_cmd_get,
957 .dumpit = tcp_metrics_nl_dump,
958 .policy = tcp_metrics_nl_policy,
Julian Anastasovd23ff702012-09-04 11:03:15 +0000959 },
960 {
961 .cmd = TCP_METRICS_CMD_DEL,
962 .doit = tcp_metrics_nl_cmd_del,
963 .policy = tcp_metrics_nl_policy,
964 .flags = GENL_ADMIN_PERM,
965 },
966};
967
Johannes Berg56989f62016-10-24 14:40:05 +0200968static struct genl_family tcp_metrics_nl_family __ro_after_init = {
Johannes Berg489111e2016-10-24 14:40:03 +0200969 .hdrsize = 0,
970 .name = TCP_METRICS_GENL_NAME,
971 .version = TCP_METRICS_GENL_VERSION,
972 .maxattr = TCP_METRICS_ATTR_MAX,
973 .netnsok = true,
974 .module = THIS_MODULE,
975 .ops = tcp_metrics_nl_ops,
976 .n_ops = ARRAY_SIZE(tcp_metrics_nl_ops),
977};
978
Eric Dumazet5815d5e2012-07-19 23:02:34 +0000979static unsigned int tcpmhash_entries;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700980static int __init set_tcpmhash_entries(char *str)
981{
982 ssize_t ret;
983
984 if (!str)
985 return 0;
986
Eric Dumazet5815d5e2012-07-19 23:02:34 +0000987 ret = kstrtouint(str, 0, &tcpmhash_entries);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700988 if (ret)
989 return 0;
990
991 return 1;
992}
993__setup("tcpmhash_entries=", set_tcpmhash_entries);
994
995static int __net_init tcp_net_metrics_init(struct net *net)
996{
Eric Dumazet5815d5e2012-07-19 23:02:34 +0000997 size_t size;
998 unsigned int slots;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700999
Eric W. Biederman098a6972015-03-13 00:07:44 -05001000 if (!net_eq(net, &init_net))
1001 return 0;
1002
David S. Miller51c5d0c2012-07-10 00:49:14 -07001003 slots = tcpmhash_entries;
1004 if (!slots) {
1005 if (totalram_pages >= 128 * 1024)
1006 slots = 16 * 1024;
1007 else
1008 slots = 8 * 1024;
1009 }
1010
Eric W. Biederman098a6972015-03-13 00:07:44 -05001011 tcp_metrics_hash_log = order_base_2(slots);
1012 size = sizeof(struct tcpm_hash_bucket) << tcp_metrics_hash_log;
David S. Miller51c5d0c2012-07-10 00:49:14 -07001013
Michal Hocko752ade62017-05-08 15:57:27 -07001014 tcp_metrics_hash = kvzalloc(size, GFP_KERNEL);
Eric W. Biederman098a6972015-03-13 00:07:44 -05001015 if (!tcp_metrics_hash)
David S. Miller51c5d0c2012-07-10 00:49:14 -07001016 return -ENOMEM;
1017
David S. Miller51c5d0c2012-07-10 00:49:14 -07001018 return 0;
1019}
1020
1021static void __net_exit tcp_net_metrics_exit(struct net *net)
1022{
Eric W. Biederman098a6972015-03-13 00:07:44 -05001023 tcp_metrics_flush_all(net);
David S. Miller51c5d0c2012-07-10 00:49:14 -07001024}
1025
1026static __net_initdata struct pernet_operations tcp_net_metrics_ops = {
1027 .init = tcp_net_metrics_init,
1028 .exit = tcp_net_metrics_exit,
1029};
1030
1031void __init tcp_metrics_init(void)
1032{
Julian Anastasovd23ff702012-09-04 11:03:15 +00001033 int ret;
1034
1035 ret = register_pernet_subsys(&tcp_net_metrics_ops);
1036 if (ret < 0)
Eric W. Biederman64935172015-03-13 00:04:51 -05001037 panic("Could not allocate the tcp_metrics hash table\n");
1038
Johannes Berg489111e2016-10-24 14:40:03 +02001039 ret = genl_register_family(&tcp_metrics_nl_family);
Julian Anastasovd23ff702012-09-04 11:03:15 +00001040 if (ret < 0)
Eric W. Biederman64935172015-03-13 00:04:51 -05001041 panic("Could not register tcp_metrics generic netlink\n");
David S. Miller51c5d0c2012-07-10 00:49:14 -07001042}