blob: 1a115b66579289fd7bb364d1f43ec31adacaf04c [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>
4#include <linux/bootmem.h>
David S. Millerab92bb22012-07-09 16:19:30 -07005#include <linux/module.h>
David S. Miller4aabd8e2012-07-09 16:07:30 -07006#include <linux/cache.h>
David S. Miller51c5d0c2012-07-10 00:49:14 -07007#include <linux/slab.h>
8#include <linux/init.h>
David S. Miller4aabd8e2012-07-09 16:07:30 -07009#include <linux/tcp.h>
10
11#include <net/inet_connection_sock.h>
David S. Miller51c5d0c2012-07-10 00:49:14 -070012#include <net/net_namespace.h>
David S. Millerab92bb22012-07-09 16:19:30 -070013#include <net/request_sock.h>
David S. Miller51c5d0c2012-07-10 00:49:14 -070014#include <net/inetpeer.h>
David S. Miller4aabd8e2012-07-09 16:07:30 -070015#include <net/sock.h>
David S. Miller51c5d0c2012-07-10 00:49:14 -070016#include <net/ipv6.h>
David S. Miller4aabd8e2012-07-09 16:07:30 -070017#include <net/dst.h>
18#include <net/tcp.h>
19
20int sysctl_tcp_nometrics_save __read_mostly;
21
David S. Miller51c5d0c2012-07-10 00:49:14 -070022enum tcp_metric_index {
23 TCP_METRIC_RTT,
24 TCP_METRIC_RTTVAR,
25 TCP_METRIC_SSTHRESH,
26 TCP_METRIC_CWND,
27 TCP_METRIC_REORDERING,
28
29 /* Always last. */
30 TCP_METRIC_MAX,
31};
32
33struct tcp_metrics_block {
34 struct tcp_metrics_block __rcu *tcpm_next;
35 struct inetpeer_addr tcpm_addr;
36 unsigned long tcpm_stamp;
David S. Miller81166dd2012-07-10 03:14:24 -070037 u32 tcpm_ts;
38 u32 tcpm_ts_stamp;
David S. Miller51c5d0c2012-07-10 00:49:14 -070039 u32 tcpm_lock;
40 u32 tcpm_vals[TCP_METRIC_MAX];
41};
42
43static bool tcp_metric_locked(struct tcp_metrics_block *tm,
44 enum tcp_metric_index idx)
45{
46 return tm->tcpm_lock & (1 << idx);
47}
48
49static u32 tcp_metric_get(struct tcp_metrics_block *tm,
50 enum tcp_metric_index idx)
51{
52 return tm->tcpm_vals[idx];
53}
54
55static u32 tcp_metric_get_jiffies(struct tcp_metrics_block *tm,
56 enum tcp_metric_index idx)
57{
58 return msecs_to_jiffies(tm->tcpm_vals[idx]);
59}
60
61static void tcp_metric_set(struct tcp_metrics_block *tm,
62 enum tcp_metric_index idx,
63 u32 val)
64{
65 tm->tcpm_vals[idx] = val;
66}
67
68static void tcp_metric_set_msecs(struct tcp_metrics_block *tm,
69 enum tcp_metric_index idx,
70 u32 val)
71{
72 tm->tcpm_vals[idx] = jiffies_to_msecs(val);
73}
74
75static bool addr_same(const struct inetpeer_addr *a,
76 const struct inetpeer_addr *b)
77{
78 const struct in6_addr *a6, *b6;
79
80 if (a->family != b->family)
81 return false;
82 if (a->family == AF_INET)
83 return a->addr.a4 == b->addr.a4;
84
85 a6 = (const struct in6_addr *) &a->addr.a6[0];
86 b6 = (const struct in6_addr *) &b->addr.a6[0];
87
88 return ipv6_addr_equal(a6, b6);
89}
90
91struct tcpm_hash_bucket {
92 struct tcp_metrics_block __rcu *chain;
93};
94
95static DEFINE_SPINLOCK(tcp_metrics_lock);
96
97static void tcpm_suck_dst(struct tcp_metrics_block *tm, struct dst_entry *dst)
98{
99 u32 val;
100
101 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
114 tm->tcpm_vals[TCP_METRIC_RTT] = dst_metric_raw(dst, RTAX_RTT);
115 tm->tcpm_vals[TCP_METRIC_RTTVAR] = dst_metric_raw(dst, RTAX_RTTVAR);
116 tm->tcpm_vals[TCP_METRIC_SSTHRESH] = dst_metric_raw(dst, RTAX_SSTHRESH);
117 tm->tcpm_vals[TCP_METRIC_CWND] = dst_metric_raw(dst, RTAX_CWND);
118 tm->tcpm_vals[TCP_METRIC_REORDERING] = dst_metric_raw(dst, RTAX_REORDERING);
David S. Miller81166dd2012-07-10 03:14:24 -0700119 tm->tcpm_ts = 0;
120 tm->tcpm_ts_stamp = 0;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700121}
122
123static struct tcp_metrics_block *tcpm_new(struct dst_entry *dst,
124 struct inetpeer_addr *addr,
125 unsigned int hash,
126 bool reclaim)
127{
128 struct tcp_metrics_block *tm;
129 struct net *net;
130
131 spin_lock_bh(&tcp_metrics_lock);
132 net = dev_net(dst->dev);
133 if (unlikely(reclaim)) {
134 struct tcp_metrics_block *oldest;
135
136 oldest = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain);
137 for (tm = rcu_dereference(oldest->tcpm_next); tm;
138 tm = rcu_dereference(tm->tcpm_next)) {
139 if (time_before(tm->tcpm_stamp, oldest->tcpm_stamp))
140 oldest = tm;
141 }
142 tm = oldest;
143 } else {
144 tm = kmalloc(sizeof(*tm), GFP_ATOMIC);
145 if (!tm)
146 goto out_unlock;
147 }
148 tm->tcpm_addr = *addr;
149 tm->tcpm_stamp = jiffies;
150
151 tcpm_suck_dst(tm, dst);
152
153 if (likely(!reclaim)) {
154 tm->tcpm_next = net->ipv4.tcp_metrics_hash[hash].chain;
155 rcu_assign_pointer(net->ipv4.tcp_metrics_hash[hash].chain, tm);
156 }
157
158out_unlock:
159 spin_unlock_bh(&tcp_metrics_lock);
160 return tm;
161}
162
163#define TCP_METRICS_TIMEOUT (60 * 60 * HZ)
164
165static void tcpm_check_stamp(struct tcp_metrics_block *tm, struct dst_entry *dst)
166{
167 if (tm && unlikely(time_after(jiffies, tm->tcpm_stamp + TCP_METRICS_TIMEOUT)))
168 tcpm_suck_dst(tm, dst);
169}
170
171#define TCP_METRICS_RECLAIM_DEPTH 5
172#define TCP_METRICS_RECLAIM_PTR (struct tcp_metrics_block *) 0x1UL
173
174static struct tcp_metrics_block *tcp_get_encode(struct tcp_metrics_block *tm, int depth)
175{
176 if (tm)
177 return tm;
178 if (depth > TCP_METRICS_RECLAIM_DEPTH)
179 return TCP_METRICS_RECLAIM_PTR;
180 return NULL;
181}
182
183static struct tcp_metrics_block *__tcp_get_metrics(const struct inetpeer_addr *addr,
184 struct net *net, unsigned int hash)
185{
186 struct tcp_metrics_block *tm;
187 int depth = 0;
188
189 for (tm = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain); tm;
190 tm = rcu_dereference(tm->tcpm_next)) {
191 if (addr_same(&tm->tcpm_addr, addr))
192 break;
193 depth++;
194 }
195 return tcp_get_encode(tm, depth);
196}
197
198static struct tcp_metrics_block *__tcp_get_metrics_req(struct request_sock *req,
199 struct dst_entry *dst)
200{
201 struct tcp_metrics_block *tm;
202 struct inetpeer_addr addr;
203 unsigned int hash;
204 struct net *net;
205
206 addr.family = req->rsk_ops->family;
207 switch (addr.family) {
208 case AF_INET:
209 addr.addr.a4 = inet_rsk(req)->rmt_addr;
210 hash = (__force unsigned int) addr.addr.a4;
211 break;
212 case AF_INET6:
213 *(struct in6_addr *)addr.addr.a6 = inet6_rsk(req)->rmt_addr;
Eric Dumazetddbe5032012-07-18 08:11:12 +0000214 hash = ipv6_addr_hash(&inet6_rsk(req)->rmt_addr);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700215 break;
216 default:
217 return NULL;
218 }
219
220 hash ^= (hash >> 24) ^ (hash >> 16) ^ (hash >> 8);
221
222 net = dev_net(dst->dev);
223 hash &= net->ipv4.tcp_metrics_hash_mask;
224
225 for (tm = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain); tm;
226 tm = rcu_dereference(tm->tcpm_next)) {
227 if (addr_same(&tm->tcpm_addr, &addr))
228 break;
229 }
230 tcpm_check_stamp(tm, dst);
231 return tm;
232}
233
David S. Miller81166dd2012-07-10 03:14:24 -0700234static struct tcp_metrics_block *__tcp_get_metrics_tw(struct inet_timewait_sock *tw)
235{
236 struct inet6_timewait_sock *tw6;
237 struct tcp_metrics_block *tm;
238 struct inetpeer_addr addr;
239 unsigned int hash;
240 struct net *net;
241
242 addr.family = tw->tw_family;
243 switch (addr.family) {
244 case AF_INET:
245 addr.addr.a4 = tw->tw_daddr;
246 hash = (__force unsigned int) addr.addr.a4;
247 break;
248 case AF_INET6:
249 tw6 = inet6_twsk((struct sock *)tw);
250 *(struct in6_addr *)addr.addr.a6 = tw6->tw_v6_daddr;
Eric Dumazetddbe5032012-07-18 08:11:12 +0000251 hash = ipv6_addr_hash(&tw6->tw_v6_daddr);
David S. Miller81166dd2012-07-10 03:14:24 -0700252 break;
253 default:
254 return NULL;
255 }
256
257 hash ^= (hash >> 24) ^ (hash >> 16) ^ (hash >> 8);
258
259 net = twsk_net(tw);
260 hash &= net->ipv4.tcp_metrics_hash_mask;
261
262 for (tm = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain); tm;
263 tm = rcu_dereference(tm->tcpm_next)) {
264 if (addr_same(&tm->tcpm_addr, &addr))
265 break;
266 }
267 return tm;
268}
269
David S. Miller51c5d0c2012-07-10 00:49:14 -0700270static 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;
275 struct inetpeer_addr addr;
276 unsigned int hash;
277 struct net *net;
278 bool reclaim;
279
280 addr.family = sk->sk_family;
281 switch (addr.family) {
282 case AF_INET:
283 addr.addr.a4 = inet_sk(sk)->inet_daddr;
284 hash = (__force unsigned int) addr.addr.a4;
285 break;
286 case AF_INET6:
287 *(struct in6_addr *)addr.addr.a6 = inet6_sk(sk)->daddr;
Eric Dumazetddbe5032012-07-18 08:11:12 +0000288 hash = ipv6_addr_hash(&inet6_sk(sk)->daddr);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700289 break;
290 default:
291 return NULL;
292 }
293
294 hash ^= (hash >> 24) ^ (hash >> 16) ^ (hash >> 8);
295
296 net = dev_net(dst->dev);
297 hash &= net->ipv4.tcp_metrics_hash_mask;
298
299 tm = __tcp_get_metrics(&addr, net, hash);
300 reclaim = false;
301 if (tm == TCP_METRICS_RECLAIM_PTR) {
302 reclaim = true;
303 tm = NULL;
304 }
305 if (!tm && create)
306 tm = tcpm_new(dst, &addr, hash, reclaim);
307 else
308 tcpm_check_stamp(tm, dst);
309
310 return tm;
311}
312
David S. Miller4aabd8e2012-07-09 16:07:30 -0700313/* Save metrics learned by this TCP session. This function is called
314 * only, when TCP finishes successfully i.e. when it enters TIME-WAIT
315 * or goes from LAST-ACK to CLOSE.
316 */
317void tcp_update_metrics(struct sock *sk)
318{
David S. Miller51c5d0c2012-07-10 00:49:14 -0700319 const struct inet_connection_sock *icsk = inet_csk(sk);
David S. Miller4aabd8e2012-07-09 16:07:30 -0700320 struct dst_entry *dst = __sk_dst_get(sk);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700321 struct tcp_sock *tp = tcp_sk(sk);
322 struct tcp_metrics_block *tm;
323 unsigned long rtt;
324 u32 val;
325 int m;
David S. Miller4aabd8e2012-07-09 16:07:30 -0700326
David S. Miller51c5d0c2012-07-10 00:49:14 -0700327 if (sysctl_tcp_nometrics_save || !dst)
David S. Miller4aabd8e2012-07-09 16:07:30 -0700328 return;
329
David S. Miller51c5d0c2012-07-10 00:49:14 -0700330 if (dst->flags & DST_HOST)
David S. Miller4aabd8e2012-07-09 16:07:30 -0700331 dst_confirm(dst);
332
David S. Miller51c5d0c2012-07-10 00:49:14 -0700333 rcu_read_lock();
334 if (icsk->icsk_backoff || !tp->srtt) {
335 /* This session failed to estimate rtt. Why?
336 * Probably, no packets returned in time. Reset our
337 * results.
David S. Miller4aabd8e2012-07-09 16:07:30 -0700338 */
David S. Miller51c5d0c2012-07-10 00:49:14 -0700339 tm = tcp_get_metrics(sk, dst, false);
340 if (tm && !tcp_metric_locked(tm, TCP_METRIC_RTT))
341 tcp_metric_set(tm, TCP_METRIC_RTT, 0);
342 goto out_unlock;
343 } else
344 tm = tcp_get_metrics(sk, dst, true);
345
346 if (!tm)
347 goto out_unlock;
348
349 rtt = tcp_metric_get_jiffies(tm, TCP_METRIC_RTT);
350 m = rtt - tp->srtt;
351
352 /* If newly calculated rtt larger than stored one, store new
353 * one. Otherwise, use EWMA. Remember, rtt overestimation is
354 * always better than underestimation.
355 */
356 if (!tcp_metric_locked(tm, TCP_METRIC_RTT)) {
357 if (m <= 0)
358 rtt = tp->srtt;
359 else
360 rtt -= (m >> 3);
361 tcp_metric_set_msecs(tm, TCP_METRIC_RTT, rtt);
362 }
363
364 if (!tcp_metric_locked(tm, TCP_METRIC_RTTVAR)) {
365 unsigned long var;
366
367 if (m < 0)
368 m = -m;
369
370 /* Scale deviation to rttvar fixed point */
371 m >>= 1;
372 if (m < tp->mdev)
373 m = tp->mdev;
374
375 var = tcp_metric_get_jiffies(tm, TCP_METRIC_RTTVAR);
376 if (m >= var)
377 var = m;
378 else
379 var -= (var - m) >> 2;
380
381 tcp_metric_set_msecs(tm, TCP_METRIC_RTTVAR, var);
382 }
383
384 if (tcp_in_initial_slowstart(tp)) {
385 /* Slow start still did not finish. */
386 if (!tcp_metric_locked(tm, TCP_METRIC_SSTHRESH)) {
387 val = tcp_metric_get(tm, TCP_METRIC_SSTHRESH);
388 if (val && (tp->snd_cwnd >> 1) > val)
389 tcp_metric_set(tm, TCP_METRIC_SSTHRESH,
390 tp->snd_cwnd >> 1);
David S. Miller4aabd8e2012-07-09 16:07:30 -0700391 }
David S. Miller51c5d0c2012-07-10 00:49:14 -0700392 if (!tcp_metric_locked(tm, TCP_METRIC_CWND)) {
393 val = tcp_metric_get(tm, TCP_METRIC_CWND);
394 if (tp->snd_cwnd > val)
395 tcp_metric_set(tm, TCP_METRIC_CWND,
396 tp->snd_cwnd);
David S. Miller4aabd8e2012-07-09 16:07:30 -0700397 }
David S. Miller51c5d0c2012-07-10 00:49:14 -0700398 } else if (tp->snd_cwnd > tp->snd_ssthresh &&
399 icsk->icsk_ca_state == TCP_CA_Open) {
400 /* Cong. avoidance phase, cwnd is reliable. */
401 if (!tcp_metric_locked(tm, TCP_METRIC_SSTHRESH))
402 tcp_metric_set(tm, TCP_METRIC_SSTHRESH,
403 max(tp->snd_cwnd >> 1, tp->snd_ssthresh));
404 if (!tcp_metric_locked(tm, TCP_METRIC_CWND)) {
405 val = tcp_metric_get(tm, TCP_METRIC_CWND);
Alexander Duyck21008442012-07-11 17:18:04 -0700406 tcp_metric_set(tm, TCP_METRIC_CWND, (val + tp->snd_cwnd) >> 1);
David S. Miller4aabd8e2012-07-09 16:07:30 -0700407 }
David S. Miller51c5d0c2012-07-10 00:49:14 -0700408 } else {
409 /* Else slow start did not finish, cwnd is non-sense,
410 * ssthresh may be also invalid.
411 */
412 if (!tcp_metric_locked(tm, TCP_METRIC_CWND)) {
413 val = tcp_metric_get(tm, TCP_METRIC_CWND);
414 tcp_metric_set(tm, TCP_METRIC_CWND,
415 (val + tp->snd_ssthresh) >> 1);
416 }
417 if (!tcp_metric_locked(tm, TCP_METRIC_SSTHRESH)) {
418 val = tcp_metric_get(tm, TCP_METRIC_SSTHRESH);
419 if (val && tp->snd_ssthresh > val)
420 tcp_metric_set(tm, TCP_METRIC_SSTHRESH,
421 tp->snd_ssthresh);
422 }
423 if (!tcp_metric_locked(tm, TCP_METRIC_REORDERING)) {
424 val = tcp_metric_get(tm, TCP_METRIC_REORDERING);
425 if (val < tp->reordering &&
David S. Miller4aabd8e2012-07-09 16:07:30 -0700426 tp->reordering != sysctl_tcp_reordering)
David S. Miller51c5d0c2012-07-10 00:49:14 -0700427 tcp_metric_set(tm, TCP_METRIC_REORDERING,
428 tp->reordering);
David S. Miller4aabd8e2012-07-09 16:07:30 -0700429 }
430 }
David S. Miller51c5d0c2012-07-10 00:49:14 -0700431 tm->tcpm_stamp = jiffies;
432out_unlock:
433 rcu_read_unlock();
David S. Miller4aabd8e2012-07-09 16:07:30 -0700434}
435
436/* Initialize metrics on socket. */
437
438void tcp_init_metrics(struct sock *sk)
439{
David S. Miller4aabd8e2012-07-09 16:07:30 -0700440 struct dst_entry *dst = __sk_dst_get(sk);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700441 struct tcp_sock *tp = tcp_sk(sk);
442 struct tcp_metrics_block *tm;
443 u32 val;
David S. Miller4aabd8e2012-07-09 16:07:30 -0700444
445 if (dst == NULL)
446 goto reset;
447
448 dst_confirm(dst);
449
David S. Miller51c5d0c2012-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. Miller51c5d0c2012-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);
474 tcp_disable_early_retrans(tp);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700475 tp->reordering = val;
David S. Miller4aabd8e2012-07-09 16:07:30 -0700476 }
477
David S. Miller51c5d0c2012-07-10 00:49:14 -0700478 val = tcp_metric_get(tm, TCP_METRIC_RTT);
479 if (val == 0 || tp->srtt == 0) {
480 rcu_read_unlock();
David S. Miller4aabd8e2012-07-09 16:07:30 -0700481 goto reset;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700482 }
David S. Miller4aabd8e2012-07-09 16:07:30 -0700483 /* Initial rtt is determined from SYN,SYN-ACK.
484 * The segment is small and rtt may appear much
485 * less than real one. Use per-dst memory
486 * to make it more realistic.
487 *
488 * A bit of theory. RTT is time passed after "normal" sized packet
489 * is sent until it is ACKed. In normal circumstances sending small
490 * packets force peer to delay ACKs and calculation is correct too.
491 * The algorithm is adaptive and, provided we follow specs, it
492 * NEVER underestimate RTT. BUT! If peer tries to make some clever
493 * tricks sort of "quick acks" for time long enough to decrease RTT
494 * to low value, and then abruptly stops to do it and starts to delay
495 * ACKs, wait for troubles.
496 */
David S. Miller51c5d0c2012-07-10 00:49:14 -0700497 val = msecs_to_jiffies(val);
498 if (val > tp->srtt) {
499 tp->srtt = val;
David S. Miller4aabd8e2012-07-09 16:07:30 -0700500 tp->rtt_seq = tp->snd_nxt;
501 }
David S. Miller51c5d0c2012-07-10 00:49:14 -0700502 val = tcp_metric_get_jiffies(tm, TCP_METRIC_RTTVAR);
503 if (val > tp->mdev) {
504 tp->mdev = val;
David S. Miller4aabd8e2012-07-09 16:07:30 -0700505 tp->mdev_max = tp->rttvar = max(tp->mdev, tcp_rto_min(sk));
506 }
David S. Miller51c5d0c2012-07-10 00:49:14 -0700507 rcu_read_unlock();
508
David S. Miller4aabd8e2012-07-09 16:07:30 -0700509 tcp_set_rto(sk);
510reset:
511 if (tp->srtt == 0) {
512 /* RFC6298: 5.7 We've failed to get a valid RTT sample from
513 * 3WHS. This is most likely due to retransmission,
514 * including spurious one. Reset the RTO back to 3secs
515 * from the more aggressive 1sec to avoid more spurious
516 * retransmission.
517 */
518 tp->mdev = tp->mdev_max = tp->rttvar = TCP_TIMEOUT_FALLBACK;
519 inet_csk(sk)->icsk_rto = TCP_TIMEOUT_FALLBACK;
520 }
521 /* Cut cwnd down to 1 per RFC5681 if SYN or SYN-ACK has been
522 * retransmitted. In light of RFC6298 more aggressive 1sec
523 * initRTO, we only reset cwnd when more than 1 SYN/SYN-ACK
524 * retransmission has occurred.
525 */
526 if (tp->total_retrans > 1)
527 tp->snd_cwnd = 1;
528 else
529 tp->snd_cwnd = tcp_init_cwnd(tp, dst);
530 tp->snd_cwnd_stamp = tcp_time_stamp;
531}
David S. Millerab92bb22012-07-09 16:19:30 -0700532
David S. Miller81166dd2012-07-10 03:14:24 -0700533bool tcp_peer_is_proven(struct request_sock *req, struct dst_entry *dst, bool paws_check)
David S. Millerab92bb22012-07-09 16:19:30 -0700534{
David S. Miller51c5d0c2012-07-10 00:49:14 -0700535 struct tcp_metrics_block *tm;
536 bool ret;
537
David S. Millerab92bb22012-07-09 16:19:30 -0700538 if (!dst)
539 return false;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700540
541 rcu_read_lock();
542 tm = __tcp_get_metrics_req(req, dst);
David S. Miller81166dd2012-07-10 03:14:24 -0700543 if (paws_check) {
544 if (tm &&
545 (u32)get_seconds() - tm->tcpm_ts_stamp < TCP_PAWS_MSL &&
546 (s32)(tm->tcpm_ts - req->ts_recent) > TCP_PAWS_WINDOW)
547 ret = false;
548 else
549 ret = true;
550 } else {
551 if (tm && tcp_metric_get(tm, TCP_METRIC_RTT) && tm->tcpm_ts_stamp)
552 ret = true;
553 else
554 ret = false;
555 }
David S. Miller51c5d0c2012-07-10 00:49:14 -0700556 rcu_read_unlock();
557
558 return ret;
David S. Millerab92bb22012-07-09 16:19:30 -0700559}
560EXPORT_SYMBOL_GPL(tcp_peer_is_proven);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700561
David S. Miller81166dd2012-07-10 03:14:24 -0700562void tcp_fetch_timewait_stamp(struct sock *sk, struct dst_entry *dst)
563{
564 struct tcp_metrics_block *tm;
565
566 rcu_read_lock();
567 tm = tcp_get_metrics(sk, dst, true);
568 if (tm) {
569 struct tcp_sock *tp = tcp_sk(sk);
570
571 if ((u32)get_seconds() - tm->tcpm_ts_stamp <= TCP_PAWS_MSL) {
572 tp->rx_opt.ts_recent_stamp = tm->tcpm_ts_stamp;
573 tp->rx_opt.ts_recent = tm->tcpm_ts;
574 }
575 }
576 rcu_read_unlock();
577}
578EXPORT_SYMBOL_GPL(tcp_fetch_timewait_stamp);
579
580/* VJ's idea. Save last timestamp seen from this destination and hold
581 * it at least for normal timewait interval to use for duplicate
582 * segment detection in subsequent connections, before they enter
583 * synchronized state.
584 */
585bool tcp_remember_stamp(struct sock *sk)
586{
587 struct dst_entry *dst = __sk_dst_get(sk);
588 bool ret = false;
589
590 if (dst) {
591 struct tcp_metrics_block *tm;
592
593 rcu_read_lock();
594 tm = tcp_get_metrics(sk, dst, true);
595 if (tm) {
596 struct tcp_sock *tp = tcp_sk(sk);
597
598 if ((s32)(tm->tcpm_ts - tp->rx_opt.ts_recent) <= 0 ||
599 ((u32)get_seconds() - tm->tcpm_ts_stamp > TCP_PAWS_MSL &&
600 tm->tcpm_ts_stamp <= (u32)tp->rx_opt.ts_recent_stamp)) {
601 tm->tcpm_ts_stamp = (u32)tp->rx_opt.ts_recent_stamp;
602 tm->tcpm_ts = tp->rx_opt.ts_recent;
603 }
604 ret = true;
605 }
606 rcu_read_unlock();
607 }
608 return ret;
609}
610
611bool tcp_tw_remember_stamp(struct inet_timewait_sock *tw)
612{
613 struct tcp_metrics_block *tm;
614 bool ret = false;
615
616 rcu_read_lock();
617 tm = __tcp_get_metrics_tw(tw);
618 if (tw) {
619 const struct tcp_timewait_sock *tcptw;
620 struct sock *sk = (struct sock *) tw;
621
622 tcptw = tcp_twsk(sk);
623 if ((s32)(tm->tcpm_ts - tcptw->tw_ts_recent) <= 0 ||
624 ((u32)get_seconds() - tm->tcpm_ts_stamp > TCP_PAWS_MSL &&
625 tm->tcpm_ts_stamp <= (u32)tcptw->tw_ts_recent_stamp)) {
626 tm->tcpm_ts_stamp = (u32)tcptw->tw_ts_recent_stamp;
627 tm->tcpm_ts = tcptw->tw_ts_recent;
628 }
629 ret = true;
630 }
631 rcu_read_unlock();
632
633 return ret;
634}
635
David S. Miller51c5d0c2012-07-10 00:49:14 -0700636static unsigned long tcpmhash_entries;
637static int __init set_tcpmhash_entries(char *str)
638{
639 ssize_t ret;
640
641 if (!str)
642 return 0;
643
644 ret = kstrtoul(str, 0, &tcpmhash_entries);
645 if (ret)
646 return 0;
647
648 return 1;
649}
650__setup("tcpmhash_entries=", set_tcpmhash_entries);
651
652static int __net_init tcp_net_metrics_init(struct net *net)
653{
654 int slots, size;
655
656 slots = tcpmhash_entries;
657 if (!slots) {
658 if (totalram_pages >= 128 * 1024)
659 slots = 16 * 1024;
660 else
661 slots = 8 * 1024;
662 }
663
664 size = slots * sizeof(struct tcpm_hash_bucket);
665
666 net->ipv4.tcp_metrics_hash = kzalloc(size, GFP_KERNEL);
667 if (!net->ipv4.tcp_metrics_hash)
668 return -ENOMEM;
669
670 net->ipv4.tcp_metrics_hash_mask = (slots - 1);
671
672 return 0;
673}
674
675static void __net_exit tcp_net_metrics_exit(struct net *net)
676{
677 kfree(net->ipv4.tcp_metrics_hash);
678}
679
680static __net_initdata struct pernet_operations tcp_net_metrics_ops = {
681 .init = tcp_net_metrics_init,
682 .exit = tcp_net_metrics_exit,
683};
684
685void __init tcp_metrics_init(void)
686{
687 register_pernet_subsys(&tcp_net_metrics_ops);
688}