]>
Commit | Line | Data |
---|---|---|
51c5d0c4 DM |
1 | #include <linux/rcupdate.h> |
2 | #include <linux/spinlock.h> | |
3 | #include <linux/jiffies.h> | |
4 | #include <linux/bootmem.h> | |
ab92bb2f | 5 | #include <linux/module.h> |
4aabd8ef | 6 | #include <linux/cache.h> |
51c5d0c4 DM |
7 | #include <linux/slab.h> |
8 | #include <linux/init.h> | |
4aabd8ef DM |
9 | #include <linux/tcp.h> |
10 | ||
11 | #include <net/inet_connection_sock.h> | |
51c5d0c4 | 12 | #include <net/net_namespace.h> |
ab92bb2f | 13 | #include <net/request_sock.h> |
51c5d0c4 | 14 | #include <net/inetpeer.h> |
4aabd8ef | 15 | #include <net/sock.h> |
51c5d0c4 | 16 | #include <net/ipv6.h> |
4aabd8ef DM |
17 | #include <net/dst.h> |
18 | #include <net/tcp.h> | |
19 | ||
20 | int sysctl_tcp_nometrics_save __read_mostly; | |
21 | ||
51c5d0c4 DM |
22 | enum 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 | ||
33 | struct tcp_metrics_block { | |
34 | struct tcp_metrics_block __rcu *tcpm_next; | |
35 | struct inetpeer_addr tcpm_addr; | |
36 | unsigned long tcpm_stamp; | |
81166dd6 DM |
37 | u32 tcpm_ts; |
38 | u32 tcpm_ts_stamp; | |
51c5d0c4 DM |
39 | u32 tcpm_lock; |
40 | u32 tcpm_vals[TCP_METRIC_MAX]; | |
41 | }; | |
42 | ||
43 | static 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 | ||
49 | static u32 tcp_metric_get(struct tcp_metrics_block *tm, | |
50 | enum tcp_metric_index idx) | |
51 | { | |
52 | return tm->tcpm_vals[idx]; | |
53 | } | |
54 | ||
55 | static 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 | ||
61 | static 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 | ||
68 | static 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 | ||
75 | static 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 | ||
91 | struct tcpm_hash_bucket { | |
92 | struct tcp_metrics_block __rcu *chain; | |
93 | }; | |
94 | ||
95 | static DEFINE_SPINLOCK(tcp_metrics_lock); | |
96 | ||
97 | static 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); | |
81166dd6 DM |
119 | tm->tcpm_ts = 0; |
120 | tm->tcpm_ts_stamp = 0; | |
51c5d0c4 DM |
121 | } |
122 | ||
123 | static 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 | ||
158 | out_unlock: | |
159 | spin_unlock_bh(&tcp_metrics_lock); | |
160 | return tm; | |
161 | } | |
162 | ||
163 | #define TCP_METRICS_TIMEOUT (60 * 60 * HZ) | |
164 | ||
165 | static 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 | ||
174 | static 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 | ||
183 | static 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 | ||
198 | static 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; | |
ddbe5032 | 214 | hash = ipv6_addr_hash(&inet6_rsk(req)->rmt_addr); |
51c5d0c4 DM |
215 | 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 | ||
81166dd6 DM |
234 | static 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; | |
ddbe5032 | 251 | hash = ipv6_addr_hash(&tw6->tw_v6_daddr); |
81166dd6 DM |
252 | 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 | ||
51c5d0c4 DM |
270 | static 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; | |
ddbe5032 | 288 | hash = ipv6_addr_hash(&inet6_sk(sk)->daddr); |
51c5d0c4 DM |
289 | 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 | ||
4aabd8ef DM |
313 | /* 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 | */ | |
317 | void tcp_update_metrics(struct sock *sk) | |
318 | { | |
51c5d0c4 | 319 | const struct inet_connection_sock *icsk = inet_csk(sk); |
4aabd8ef | 320 | struct dst_entry *dst = __sk_dst_get(sk); |
51c5d0c4 DM |
321 | struct tcp_sock *tp = tcp_sk(sk); |
322 | struct tcp_metrics_block *tm; | |
323 | unsigned long rtt; | |
324 | u32 val; | |
325 | int m; | |
4aabd8ef | 326 | |
51c5d0c4 | 327 | if (sysctl_tcp_nometrics_save || !dst) |
4aabd8ef DM |
328 | return; |
329 | ||
51c5d0c4 | 330 | if (dst->flags & DST_HOST) |
4aabd8ef DM |
331 | dst_confirm(dst); |
332 | ||
51c5d0c4 DM |
333 | 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. | |
338 | */ | |
339 | 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); | |
4aabd8ef | 345 | |
51c5d0c4 DM |
346 | if (!tm) |
347 | goto out_unlock; | |
4aabd8ef | 348 | |
51c5d0c4 DM |
349 | rtt = tcp_metric_get_jiffies(tm, TCP_METRIC_RTT); |
350 | m = rtt - tp->srtt; | |
4aabd8ef | 351 | |
51c5d0c4 DM |
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 | } | |
4aabd8ef | 363 | |
51c5d0c4 DM |
364 | if (!tcp_metric_locked(tm, TCP_METRIC_RTTVAR)) { |
365 | unsigned long var; | |
4aabd8ef | 366 | |
51c5d0c4 DM |
367 | if (m < 0) |
368 | m = -m; | |
4aabd8ef | 369 | |
51c5d0c4 DM |
370 | /* Scale deviation to rttvar fixed point */ |
371 | m >>= 1; | |
372 | if (m < tp->mdev) | |
373 | m = tp->mdev; | |
4aabd8ef | 374 | |
51c5d0c4 DM |
375 | var = tcp_metric_get_jiffies(tm, TCP_METRIC_RTTVAR); |
376 | if (m >= var) | |
377 | var = m; | |
378 | else | |
379 | var -= (var - m) >> 2; | |
4aabd8ef | 380 | |
51c5d0c4 DM |
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); | |
391 | } | |
392 | 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); | |
397 | } | |
398 | } 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); | |
2100844c | 406 | tcp_metric_set(tm, TCP_METRIC_CWND, (val + tp->snd_cwnd) >> 1); |
51c5d0c4 DM |
407 | } |
408 | } 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 && | |
4aabd8ef | 426 | tp->reordering != sysctl_tcp_reordering) |
51c5d0c4 DM |
427 | tcp_metric_set(tm, TCP_METRIC_REORDERING, |
428 | tp->reordering); | |
4aabd8ef DM |
429 | } |
430 | } | |
51c5d0c4 DM |
431 | tm->tcpm_stamp = jiffies; |
432 | out_unlock: | |
433 | rcu_read_unlock(); | |
4aabd8ef DM |
434 | } |
435 | ||
436 | /* Initialize metrics on socket. */ | |
437 | ||
438 | void tcp_init_metrics(struct sock *sk) | |
439 | { | |
4aabd8ef | 440 | struct dst_entry *dst = __sk_dst_get(sk); |
51c5d0c4 DM |
441 | struct tcp_sock *tp = tcp_sk(sk); |
442 | struct tcp_metrics_block *tm; | |
443 | u32 val; | |
4aabd8ef DM |
444 | |
445 | if (dst == NULL) | |
446 | goto reset; | |
447 | ||
448 | dst_confirm(dst); | |
449 | ||
51c5d0c4 DM |
450 | 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; | |
4aabd8ef DM |
463 | 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 | } | |
51c5d0c4 DM |
471 | val = tcp_metric_get(tm, TCP_METRIC_REORDERING); |
472 | if (val && tp->reordering != val) { | |
4aabd8ef DM |
473 | tcp_disable_fack(tp); |
474 | tcp_disable_early_retrans(tp); | |
51c5d0c4 | 475 | tp->reordering = val; |
4aabd8ef DM |
476 | } |
477 | ||
51c5d0c4 DM |
478 | val = tcp_metric_get(tm, TCP_METRIC_RTT); |
479 | if (val == 0 || tp->srtt == 0) { | |
480 | rcu_read_unlock(); | |
4aabd8ef | 481 | goto reset; |
51c5d0c4 | 482 | } |
4aabd8ef DM |
483 | /* 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 | */ | |
51c5d0c4 DM |
497 | val = msecs_to_jiffies(val); |
498 | if (val > tp->srtt) { | |
499 | tp->srtt = val; | |
4aabd8ef DM |
500 | tp->rtt_seq = tp->snd_nxt; |
501 | } | |
51c5d0c4 DM |
502 | val = tcp_metric_get_jiffies(tm, TCP_METRIC_RTTVAR); |
503 | if (val > tp->mdev) { | |
504 | tp->mdev = val; | |
4aabd8ef DM |
505 | tp->mdev_max = tp->rttvar = max(tp->mdev, tcp_rto_min(sk)); |
506 | } | |
51c5d0c4 DM |
507 | rcu_read_unlock(); |
508 | ||
4aabd8ef DM |
509 | tcp_set_rto(sk); |
510 | reset: | |
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 | } | |
ab92bb2f | 532 | |
81166dd6 | 533 | bool tcp_peer_is_proven(struct request_sock *req, struct dst_entry *dst, bool paws_check) |
ab92bb2f | 534 | { |
51c5d0c4 DM |
535 | struct tcp_metrics_block *tm; |
536 | bool ret; | |
537 | ||
ab92bb2f DM |
538 | if (!dst) |
539 | return false; | |
51c5d0c4 DM |
540 | |
541 | rcu_read_lock(); | |
542 | tm = __tcp_get_metrics_req(req, dst); | |
81166dd6 DM |
543 | 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 | } | |
51c5d0c4 DM |
556 | rcu_read_unlock(); |
557 | ||
558 | return ret; | |
ab92bb2f DM |
559 | } |
560 | EXPORT_SYMBOL_GPL(tcp_peer_is_proven); | |
51c5d0c4 | 561 | |
81166dd6 DM |
562 | void 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 | } | |
578 | EXPORT_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 | */ | |
585 | bool 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 | ||
611 | bool 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 | ||
51c5d0c4 DM |
636 | static unsigned long tcpmhash_entries; |
637 | static 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 | ||
652 | static 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 | ||
675 | static void __net_exit tcp_net_metrics_exit(struct net *net) | |
676 | { | |
677 | kfree(net->ipv4.tcp_metrics_hash); | |
678 | } | |
679 | ||
680 | static __net_initdata struct pernet_operations tcp_net_metrics_ops = { | |
681 | .init = tcp_net_metrics_init, | |
682 | .exit = tcp_net_metrics_exit, | |
683 | }; | |
684 | ||
685 | void __init tcp_metrics_init(void) | |
686 | { | |
687 | register_pernet_subsys(&tcp_net_metrics_ops); | |
688 | } |