netlink: implement nla_get_in_addr and nla_get_in6_addr
[deliverable/linux.git] / net / ipv4 / tcp_metrics.c
1 #include <linux/rcupdate.h>
2 #include <linux/spinlock.h>
3 #include <linux/jiffies.h>
4 #include <linux/module.h>
5 #include <linux/cache.h>
6 #include <linux/slab.h>
7 #include <linux/init.h>
8 #include <linux/tcp.h>
9 #include <linux/hash.h>
10 #include <linux/tcp_metrics.h>
11 #include <linux/vmalloc.h>
12
13 #include <net/inet_connection_sock.h>
14 #include <net/net_namespace.h>
15 #include <net/request_sock.h>
16 #include <net/inetpeer.h>
17 #include <net/sock.h>
18 #include <net/ipv6.h>
19 #include <net/dst.h>
20 #include <net/tcp.h>
21 #include <net/genetlink.h>
22
23 int sysctl_tcp_nometrics_save __read_mostly;
24
25 static struct tcp_metrics_block *__tcp_get_metrics(const struct inetpeer_addr *saddr,
26 const struct inetpeer_addr *daddr,
27 struct net *net, unsigned int hash);
28
29 struct tcp_fastopen_metrics {
30 u16 mss;
31 u16 syn_loss:10; /* Recurring Fast Open SYN losses */
32 unsigned long last_syn_loss; /* Last Fast Open SYN loss */
33 struct tcp_fastopen_cookie cookie;
34 };
35
36 /* TCP_METRIC_MAX includes 2 extra fields for userspace compatibility
37 * Kernel only stores RTT and RTTVAR in usec resolution
38 */
39 #define TCP_METRIC_MAX_KERNEL (TCP_METRIC_MAX - 2)
40
41 struct tcp_metrics_block {
42 struct tcp_metrics_block __rcu *tcpm_next;
43 possible_net_t tcpm_net;
44 struct inetpeer_addr tcpm_saddr;
45 struct inetpeer_addr tcpm_daddr;
46 unsigned long tcpm_stamp;
47 u32 tcpm_ts;
48 u32 tcpm_ts_stamp;
49 u32 tcpm_lock;
50 u32 tcpm_vals[TCP_METRIC_MAX_KERNEL + 1];
51 struct tcp_fastopen_metrics tcpm_fastopen;
52
53 struct rcu_head rcu_head;
54 };
55
56 static inline struct net *tm_net(struct tcp_metrics_block *tm)
57 {
58 return read_pnet(&tm->tcpm_net);
59 }
60
61 static bool tcp_metric_locked(struct tcp_metrics_block *tm,
62 enum tcp_metric_index idx)
63 {
64 return tm->tcpm_lock & (1 << idx);
65 }
66
67 static u32 tcp_metric_get(struct tcp_metrics_block *tm,
68 enum tcp_metric_index idx)
69 {
70 return tm->tcpm_vals[idx];
71 }
72
73 static void tcp_metric_set(struct tcp_metrics_block *tm,
74 enum tcp_metric_index idx,
75 u32 val)
76 {
77 tm->tcpm_vals[idx] = val;
78 }
79
80 static bool addr_same(const struct inetpeer_addr *a,
81 const struct inetpeer_addr *b)
82 {
83 if (a->family != b->family)
84 return false;
85 if (a->family == AF_INET)
86 return a->addr.a4 == b->addr.a4;
87 return ipv6_addr_equal(&a->addr.in6, &b->addr.in6);
88 }
89
90 struct tcpm_hash_bucket {
91 struct tcp_metrics_block __rcu *chain;
92 };
93
94 static struct tcpm_hash_bucket *tcp_metrics_hash __read_mostly;
95 static unsigned int tcp_metrics_hash_log __read_mostly;
96
97 static DEFINE_SPINLOCK(tcp_metrics_lock);
98
99 static void tcpm_suck_dst(struct tcp_metrics_block *tm,
100 const struct dst_entry *dst,
101 bool fastopen_clear)
102 {
103 u32 msval;
104 u32 val;
105
106 tm->tcpm_stamp = jiffies;
107
108 val = 0;
109 if (dst_metric_locked(dst, RTAX_RTT))
110 val |= 1 << TCP_METRIC_RTT;
111 if (dst_metric_locked(dst, RTAX_RTTVAR))
112 val |= 1 << TCP_METRIC_RTTVAR;
113 if (dst_metric_locked(dst, RTAX_SSTHRESH))
114 val |= 1 << TCP_METRIC_SSTHRESH;
115 if (dst_metric_locked(dst, RTAX_CWND))
116 val |= 1 << TCP_METRIC_CWND;
117 if (dst_metric_locked(dst, RTAX_REORDERING))
118 val |= 1 << TCP_METRIC_REORDERING;
119 tm->tcpm_lock = val;
120
121 msval = dst_metric_raw(dst, RTAX_RTT);
122 tm->tcpm_vals[TCP_METRIC_RTT] = msval * USEC_PER_MSEC;
123
124 msval = dst_metric_raw(dst, RTAX_RTTVAR);
125 tm->tcpm_vals[TCP_METRIC_RTTVAR] = msval * USEC_PER_MSEC;
126 tm->tcpm_vals[TCP_METRIC_SSTHRESH] = dst_metric_raw(dst, RTAX_SSTHRESH);
127 tm->tcpm_vals[TCP_METRIC_CWND] = dst_metric_raw(dst, RTAX_CWND);
128 tm->tcpm_vals[TCP_METRIC_REORDERING] = dst_metric_raw(dst, RTAX_REORDERING);
129 tm->tcpm_ts = 0;
130 tm->tcpm_ts_stamp = 0;
131 if (fastopen_clear) {
132 tm->tcpm_fastopen.mss = 0;
133 tm->tcpm_fastopen.syn_loss = 0;
134 tm->tcpm_fastopen.cookie.len = 0;
135 }
136 }
137
138 #define TCP_METRICS_TIMEOUT (60 * 60 * HZ)
139
140 static void tcpm_check_stamp(struct tcp_metrics_block *tm, struct dst_entry *dst)
141 {
142 if (tm && unlikely(time_after(jiffies, tm->tcpm_stamp + TCP_METRICS_TIMEOUT)))
143 tcpm_suck_dst(tm, dst, false);
144 }
145
146 #define TCP_METRICS_RECLAIM_DEPTH 5
147 #define TCP_METRICS_RECLAIM_PTR (struct tcp_metrics_block *) 0x1UL
148
149 #define deref_locked(p) \
150 rcu_dereference_protected(p, lockdep_is_held(&tcp_metrics_lock))
151
152 static struct tcp_metrics_block *tcpm_new(struct dst_entry *dst,
153 struct inetpeer_addr *saddr,
154 struct inetpeer_addr *daddr,
155 unsigned int hash)
156 {
157 struct tcp_metrics_block *tm;
158 struct net *net;
159 bool reclaim = false;
160
161 spin_lock_bh(&tcp_metrics_lock);
162 net = dev_net(dst->dev);
163
164 /* While waiting for the spin-lock the cache might have been populated
165 * with this entry and so we have to check again.
166 */
167 tm = __tcp_get_metrics(saddr, daddr, net, hash);
168 if (tm == TCP_METRICS_RECLAIM_PTR) {
169 reclaim = true;
170 tm = NULL;
171 }
172 if (tm) {
173 tcpm_check_stamp(tm, dst);
174 goto out_unlock;
175 }
176
177 if (unlikely(reclaim)) {
178 struct tcp_metrics_block *oldest;
179
180 oldest = deref_locked(tcp_metrics_hash[hash].chain);
181 for (tm = deref_locked(oldest->tcpm_next); tm;
182 tm = deref_locked(tm->tcpm_next)) {
183 if (time_before(tm->tcpm_stamp, oldest->tcpm_stamp))
184 oldest = tm;
185 }
186 tm = oldest;
187 } else {
188 tm = kmalloc(sizeof(*tm), GFP_ATOMIC);
189 if (!tm)
190 goto out_unlock;
191 }
192 write_pnet(&tm->tcpm_net, net);
193 tm->tcpm_saddr = *saddr;
194 tm->tcpm_daddr = *daddr;
195
196 tcpm_suck_dst(tm, dst, true);
197
198 if (likely(!reclaim)) {
199 tm->tcpm_next = tcp_metrics_hash[hash].chain;
200 rcu_assign_pointer(tcp_metrics_hash[hash].chain, tm);
201 }
202
203 out_unlock:
204 spin_unlock_bh(&tcp_metrics_lock);
205 return tm;
206 }
207
208 static struct tcp_metrics_block *tcp_get_encode(struct tcp_metrics_block *tm, int depth)
209 {
210 if (tm)
211 return tm;
212 if (depth > TCP_METRICS_RECLAIM_DEPTH)
213 return TCP_METRICS_RECLAIM_PTR;
214 return NULL;
215 }
216
217 static struct tcp_metrics_block *__tcp_get_metrics(const struct inetpeer_addr *saddr,
218 const struct inetpeer_addr *daddr,
219 struct net *net, unsigned int hash)
220 {
221 struct tcp_metrics_block *tm;
222 int depth = 0;
223
224 for (tm = rcu_dereference(tcp_metrics_hash[hash].chain); tm;
225 tm = rcu_dereference(tm->tcpm_next)) {
226 if (addr_same(&tm->tcpm_saddr, saddr) &&
227 addr_same(&tm->tcpm_daddr, daddr) &&
228 net_eq(tm_net(tm), net))
229 break;
230 depth++;
231 }
232 return tcp_get_encode(tm, depth);
233 }
234
235 static struct tcp_metrics_block *__tcp_get_metrics_req(struct request_sock *req,
236 struct dst_entry *dst)
237 {
238 struct tcp_metrics_block *tm;
239 struct inetpeer_addr saddr, daddr;
240 unsigned int hash;
241 struct net *net;
242
243 saddr.family = req->rsk_ops->family;
244 daddr.family = req->rsk_ops->family;
245 switch (daddr.family) {
246 case AF_INET:
247 saddr.addr.a4 = inet_rsk(req)->ir_loc_addr;
248 daddr.addr.a4 = inet_rsk(req)->ir_rmt_addr;
249 hash = (__force unsigned int) daddr.addr.a4;
250 break;
251 #if IS_ENABLED(CONFIG_IPV6)
252 case AF_INET6:
253 saddr.addr.in6 = inet_rsk(req)->ir_v6_loc_addr;
254 daddr.addr.in6 = inet_rsk(req)->ir_v6_rmt_addr;
255 hash = ipv6_addr_hash(&inet_rsk(req)->ir_v6_rmt_addr);
256 break;
257 #endif
258 default:
259 return NULL;
260 }
261
262 net = dev_net(dst->dev);
263 hash ^= net_hash_mix(net);
264 hash = hash_32(hash, tcp_metrics_hash_log);
265
266 for (tm = rcu_dereference(tcp_metrics_hash[hash].chain); tm;
267 tm = rcu_dereference(tm->tcpm_next)) {
268 if (addr_same(&tm->tcpm_saddr, &saddr) &&
269 addr_same(&tm->tcpm_daddr, &daddr) &&
270 net_eq(tm_net(tm), net))
271 break;
272 }
273 tcpm_check_stamp(tm, dst);
274 return tm;
275 }
276
277 static struct tcp_metrics_block *__tcp_get_metrics_tw(struct inet_timewait_sock *tw)
278 {
279 struct tcp_metrics_block *tm;
280 struct inetpeer_addr saddr, daddr;
281 unsigned int hash;
282 struct net *net;
283
284 if (tw->tw_family == AF_INET) {
285 saddr.family = AF_INET;
286 saddr.addr.a4 = tw->tw_rcv_saddr;
287 daddr.family = AF_INET;
288 daddr.addr.a4 = tw->tw_daddr;
289 hash = (__force unsigned int) daddr.addr.a4;
290 }
291 #if IS_ENABLED(CONFIG_IPV6)
292 else if (tw->tw_family == AF_INET6) {
293 if (ipv6_addr_v4mapped(&tw->tw_v6_daddr)) {
294 saddr.family = AF_INET;
295 saddr.addr.a4 = tw->tw_rcv_saddr;
296 daddr.family = AF_INET;
297 daddr.addr.a4 = tw->tw_daddr;
298 hash = (__force unsigned int) daddr.addr.a4;
299 } else {
300 saddr.family = AF_INET6;
301 saddr.addr.in6 = tw->tw_v6_rcv_saddr;
302 daddr.family = AF_INET6;
303 daddr.addr.in6 = tw->tw_v6_daddr;
304 hash = ipv6_addr_hash(&tw->tw_v6_daddr);
305 }
306 }
307 #endif
308 else
309 return NULL;
310
311 net = twsk_net(tw);
312 hash ^= net_hash_mix(net);
313 hash = hash_32(hash, tcp_metrics_hash_log);
314
315 for (tm = rcu_dereference(tcp_metrics_hash[hash].chain); tm;
316 tm = rcu_dereference(tm->tcpm_next)) {
317 if (addr_same(&tm->tcpm_saddr, &saddr) &&
318 addr_same(&tm->tcpm_daddr, &daddr) &&
319 net_eq(tm_net(tm), net))
320 break;
321 }
322 return tm;
323 }
324
325 static struct tcp_metrics_block *tcp_get_metrics(struct sock *sk,
326 struct dst_entry *dst,
327 bool create)
328 {
329 struct tcp_metrics_block *tm;
330 struct inetpeer_addr saddr, daddr;
331 unsigned int hash;
332 struct net *net;
333
334 if (sk->sk_family == AF_INET) {
335 saddr.family = AF_INET;
336 saddr.addr.a4 = inet_sk(sk)->inet_saddr;
337 daddr.family = AF_INET;
338 daddr.addr.a4 = inet_sk(sk)->inet_daddr;
339 hash = (__force unsigned int) daddr.addr.a4;
340 }
341 #if IS_ENABLED(CONFIG_IPV6)
342 else if (sk->sk_family == AF_INET6) {
343 if (ipv6_addr_v4mapped(&sk->sk_v6_daddr)) {
344 saddr.family = AF_INET;
345 saddr.addr.a4 = inet_sk(sk)->inet_saddr;
346 daddr.family = AF_INET;
347 daddr.addr.a4 = inet_sk(sk)->inet_daddr;
348 hash = (__force unsigned int) daddr.addr.a4;
349 } else {
350 saddr.family = AF_INET6;
351 saddr.addr.in6 = sk->sk_v6_rcv_saddr;
352 daddr.family = AF_INET6;
353 daddr.addr.in6 = sk->sk_v6_daddr;
354 hash = ipv6_addr_hash(&sk->sk_v6_daddr);
355 }
356 }
357 #endif
358 else
359 return NULL;
360
361 net = dev_net(dst->dev);
362 hash ^= net_hash_mix(net);
363 hash = hash_32(hash, tcp_metrics_hash_log);
364
365 tm = __tcp_get_metrics(&saddr, &daddr, net, hash);
366 if (tm == TCP_METRICS_RECLAIM_PTR)
367 tm = NULL;
368 if (!tm && create)
369 tm = tcpm_new(dst, &saddr, &daddr, hash);
370 else
371 tcpm_check_stamp(tm, dst);
372
373 return tm;
374 }
375
376 /* Save metrics learned by this TCP session. This function is called
377 * only, when TCP finishes successfully i.e. when it enters TIME-WAIT
378 * or goes from LAST-ACK to CLOSE.
379 */
380 void tcp_update_metrics(struct sock *sk)
381 {
382 const struct inet_connection_sock *icsk = inet_csk(sk);
383 struct dst_entry *dst = __sk_dst_get(sk);
384 struct tcp_sock *tp = tcp_sk(sk);
385 struct tcp_metrics_block *tm;
386 unsigned long rtt;
387 u32 val;
388 int m;
389
390 if (sysctl_tcp_nometrics_save || !dst)
391 return;
392
393 if (dst->flags & DST_HOST)
394 dst_confirm(dst);
395
396 rcu_read_lock();
397 if (icsk->icsk_backoff || !tp->srtt_us) {
398 /* This session failed to estimate rtt. Why?
399 * Probably, no packets returned in time. Reset our
400 * results.
401 */
402 tm = tcp_get_metrics(sk, dst, false);
403 if (tm && !tcp_metric_locked(tm, TCP_METRIC_RTT))
404 tcp_metric_set(tm, TCP_METRIC_RTT, 0);
405 goto out_unlock;
406 } else
407 tm = tcp_get_metrics(sk, dst, true);
408
409 if (!tm)
410 goto out_unlock;
411
412 rtt = tcp_metric_get(tm, TCP_METRIC_RTT);
413 m = rtt - tp->srtt_us;
414
415 /* If newly calculated rtt larger than stored one, store new
416 * one. Otherwise, use EWMA. Remember, rtt overestimation is
417 * always better than underestimation.
418 */
419 if (!tcp_metric_locked(tm, TCP_METRIC_RTT)) {
420 if (m <= 0)
421 rtt = tp->srtt_us;
422 else
423 rtt -= (m >> 3);
424 tcp_metric_set(tm, TCP_METRIC_RTT, rtt);
425 }
426
427 if (!tcp_metric_locked(tm, TCP_METRIC_RTTVAR)) {
428 unsigned long var;
429
430 if (m < 0)
431 m = -m;
432
433 /* Scale deviation to rttvar fixed point */
434 m >>= 1;
435 if (m < tp->mdev_us)
436 m = tp->mdev_us;
437
438 var = tcp_metric_get(tm, TCP_METRIC_RTTVAR);
439 if (m >= var)
440 var = m;
441 else
442 var -= (var - m) >> 2;
443
444 tcp_metric_set(tm, TCP_METRIC_RTTVAR, var);
445 }
446
447 if (tcp_in_initial_slowstart(tp)) {
448 /* Slow start still did not finish. */
449 if (!tcp_metric_locked(tm, TCP_METRIC_SSTHRESH)) {
450 val = tcp_metric_get(tm, TCP_METRIC_SSTHRESH);
451 if (val && (tp->snd_cwnd >> 1) > val)
452 tcp_metric_set(tm, TCP_METRIC_SSTHRESH,
453 tp->snd_cwnd >> 1);
454 }
455 if (!tcp_metric_locked(tm, TCP_METRIC_CWND)) {
456 val = tcp_metric_get(tm, TCP_METRIC_CWND);
457 if (tp->snd_cwnd > val)
458 tcp_metric_set(tm, TCP_METRIC_CWND,
459 tp->snd_cwnd);
460 }
461 } else if (tp->snd_cwnd > tp->snd_ssthresh &&
462 icsk->icsk_ca_state == TCP_CA_Open) {
463 /* Cong. avoidance phase, cwnd is reliable. */
464 if (!tcp_metric_locked(tm, TCP_METRIC_SSTHRESH))
465 tcp_metric_set(tm, TCP_METRIC_SSTHRESH,
466 max(tp->snd_cwnd >> 1, tp->snd_ssthresh));
467 if (!tcp_metric_locked(tm, TCP_METRIC_CWND)) {
468 val = tcp_metric_get(tm, TCP_METRIC_CWND);
469 tcp_metric_set(tm, TCP_METRIC_CWND, (val + tp->snd_cwnd) >> 1);
470 }
471 } else {
472 /* Else slow start did not finish, cwnd is non-sense,
473 * ssthresh may be also invalid.
474 */
475 if (!tcp_metric_locked(tm, TCP_METRIC_CWND)) {
476 val = tcp_metric_get(tm, TCP_METRIC_CWND);
477 tcp_metric_set(tm, TCP_METRIC_CWND,
478 (val + tp->snd_ssthresh) >> 1);
479 }
480 if (!tcp_metric_locked(tm, TCP_METRIC_SSTHRESH)) {
481 val = tcp_metric_get(tm, TCP_METRIC_SSTHRESH);
482 if (val && tp->snd_ssthresh > val)
483 tcp_metric_set(tm, TCP_METRIC_SSTHRESH,
484 tp->snd_ssthresh);
485 }
486 if (!tcp_metric_locked(tm, TCP_METRIC_REORDERING)) {
487 val = tcp_metric_get(tm, TCP_METRIC_REORDERING);
488 if (val < tp->reordering &&
489 tp->reordering != sysctl_tcp_reordering)
490 tcp_metric_set(tm, TCP_METRIC_REORDERING,
491 tp->reordering);
492 }
493 }
494 tm->tcpm_stamp = jiffies;
495 out_unlock:
496 rcu_read_unlock();
497 }
498
499 /* Initialize metrics on socket. */
500
501 void tcp_init_metrics(struct sock *sk)
502 {
503 struct dst_entry *dst = __sk_dst_get(sk);
504 struct tcp_sock *tp = tcp_sk(sk);
505 struct tcp_metrics_block *tm;
506 u32 val, crtt = 0; /* cached RTT scaled by 8 */
507
508 if (dst == NULL)
509 goto reset;
510
511 dst_confirm(dst);
512
513 rcu_read_lock();
514 tm = tcp_get_metrics(sk, dst, true);
515 if (!tm) {
516 rcu_read_unlock();
517 goto reset;
518 }
519
520 if (tcp_metric_locked(tm, TCP_METRIC_CWND))
521 tp->snd_cwnd_clamp = tcp_metric_get(tm, TCP_METRIC_CWND);
522
523 val = tcp_metric_get(tm, TCP_METRIC_SSTHRESH);
524 if (val) {
525 tp->snd_ssthresh = val;
526 if (tp->snd_ssthresh > tp->snd_cwnd_clamp)
527 tp->snd_ssthresh = tp->snd_cwnd_clamp;
528 } else {
529 /* ssthresh may have been reduced unnecessarily during.
530 * 3WHS. Restore it back to its initial default.
531 */
532 tp->snd_ssthresh = TCP_INFINITE_SSTHRESH;
533 }
534 val = tcp_metric_get(tm, TCP_METRIC_REORDERING);
535 if (val && tp->reordering != val) {
536 tcp_disable_fack(tp);
537 tcp_disable_early_retrans(tp);
538 tp->reordering = val;
539 }
540
541 crtt = tcp_metric_get(tm, TCP_METRIC_RTT);
542 rcu_read_unlock();
543 reset:
544 /* The initial RTT measurement from the SYN/SYN-ACK is not ideal
545 * to seed the RTO for later data packets because SYN packets are
546 * small. Use the per-dst cached values to seed the RTO but keep
547 * the RTT estimator variables intact (e.g., srtt, mdev, rttvar).
548 * Later the RTO will be updated immediately upon obtaining the first
549 * data RTT sample (tcp_rtt_estimator()). Hence the cached RTT only
550 * influences the first RTO but not later RTT estimation.
551 *
552 * But if RTT is not available from the SYN (due to retransmits or
553 * syn cookies) or the cache, force a conservative 3secs timeout.
554 *
555 * A bit of theory. RTT is time passed after "normal" sized packet
556 * is sent until it is ACKed. In normal circumstances sending small
557 * packets force peer to delay ACKs and calculation is correct too.
558 * The algorithm is adaptive and, provided we follow specs, it
559 * NEVER underestimate RTT. BUT! If peer tries to make some clever
560 * tricks sort of "quick acks" for time long enough to decrease RTT
561 * to low value, and then abruptly stops to do it and starts to delay
562 * ACKs, wait for troubles.
563 */
564 if (crtt > tp->srtt_us) {
565 /* Set RTO like tcp_rtt_estimator(), but from cached RTT. */
566 crtt /= 8 * USEC_PER_MSEC;
567 inet_csk(sk)->icsk_rto = crtt + max(2 * crtt, tcp_rto_min(sk));
568 } else if (tp->srtt_us == 0) {
569 /* RFC6298: 5.7 We've failed to get a valid RTT sample from
570 * 3WHS. This is most likely due to retransmission,
571 * including spurious one. Reset the RTO back to 3secs
572 * from the more aggressive 1sec to avoid more spurious
573 * retransmission.
574 */
575 tp->rttvar_us = jiffies_to_usecs(TCP_TIMEOUT_FALLBACK);
576 tp->mdev_us = tp->mdev_max_us = tp->rttvar_us;
577
578 inet_csk(sk)->icsk_rto = TCP_TIMEOUT_FALLBACK;
579 }
580 /* Cut cwnd down to 1 per RFC5681 if SYN or SYN-ACK has been
581 * retransmitted. In light of RFC6298 more aggressive 1sec
582 * initRTO, we only reset cwnd when more than 1 SYN/SYN-ACK
583 * retransmission has occurred.
584 */
585 if (tp->total_retrans > 1)
586 tp->snd_cwnd = 1;
587 else
588 tp->snd_cwnd = tcp_init_cwnd(tp, dst);
589 tp->snd_cwnd_stamp = tcp_time_stamp;
590 }
591
592 bool tcp_peer_is_proven(struct request_sock *req, struct dst_entry *dst,
593 bool paws_check, bool timestamps)
594 {
595 struct tcp_metrics_block *tm;
596 bool ret;
597
598 if (!dst)
599 return false;
600
601 rcu_read_lock();
602 tm = __tcp_get_metrics_req(req, dst);
603 if (paws_check) {
604 if (tm &&
605 (u32)get_seconds() - tm->tcpm_ts_stamp < TCP_PAWS_MSL &&
606 ((s32)(tm->tcpm_ts - req->ts_recent) > TCP_PAWS_WINDOW ||
607 !timestamps))
608 ret = false;
609 else
610 ret = true;
611 } else {
612 if (tm && tcp_metric_get(tm, TCP_METRIC_RTT) && tm->tcpm_ts_stamp)
613 ret = true;
614 else
615 ret = false;
616 }
617 rcu_read_unlock();
618
619 return ret;
620 }
621 EXPORT_SYMBOL_GPL(tcp_peer_is_proven);
622
623 void tcp_fetch_timewait_stamp(struct sock *sk, struct dst_entry *dst)
624 {
625 struct tcp_metrics_block *tm;
626
627 rcu_read_lock();
628 tm = tcp_get_metrics(sk, dst, true);
629 if (tm) {
630 struct tcp_sock *tp = tcp_sk(sk);
631
632 if ((u32)get_seconds() - tm->tcpm_ts_stamp <= TCP_PAWS_MSL) {
633 tp->rx_opt.ts_recent_stamp = tm->tcpm_ts_stamp;
634 tp->rx_opt.ts_recent = tm->tcpm_ts;
635 }
636 }
637 rcu_read_unlock();
638 }
639 EXPORT_SYMBOL_GPL(tcp_fetch_timewait_stamp);
640
641 /* VJ's idea. Save last timestamp seen from this destination and hold
642 * it at least for normal timewait interval to use for duplicate
643 * segment detection in subsequent connections, before they enter
644 * synchronized state.
645 */
646 bool tcp_remember_stamp(struct sock *sk)
647 {
648 struct dst_entry *dst = __sk_dst_get(sk);
649 bool ret = false;
650
651 if (dst) {
652 struct tcp_metrics_block *tm;
653
654 rcu_read_lock();
655 tm = tcp_get_metrics(sk, dst, true);
656 if (tm) {
657 struct tcp_sock *tp = tcp_sk(sk);
658
659 if ((s32)(tm->tcpm_ts - tp->rx_opt.ts_recent) <= 0 ||
660 ((u32)get_seconds() - tm->tcpm_ts_stamp > TCP_PAWS_MSL &&
661 tm->tcpm_ts_stamp <= (u32)tp->rx_opt.ts_recent_stamp)) {
662 tm->tcpm_ts_stamp = (u32)tp->rx_opt.ts_recent_stamp;
663 tm->tcpm_ts = tp->rx_opt.ts_recent;
664 }
665 ret = true;
666 }
667 rcu_read_unlock();
668 }
669 return ret;
670 }
671
672 bool tcp_tw_remember_stamp(struct inet_timewait_sock *tw)
673 {
674 struct tcp_metrics_block *tm;
675 bool ret = false;
676
677 rcu_read_lock();
678 tm = __tcp_get_metrics_tw(tw);
679 if (tm) {
680 const struct tcp_timewait_sock *tcptw;
681 struct sock *sk = (struct sock *) tw;
682
683 tcptw = tcp_twsk(sk);
684 if ((s32)(tm->tcpm_ts - tcptw->tw_ts_recent) <= 0 ||
685 ((u32)get_seconds() - tm->tcpm_ts_stamp > TCP_PAWS_MSL &&
686 tm->tcpm_ts_stamp <= (u32)tcptw->tw_ts_recent_stamp)) {
687 tm->tcpm_ts_stamp = (u32)tcptw->tw_ts_recent_stamp;
688 tm->tcpm_ts = tcptw->tw_ts_recent;
689 }
690 ret = true;
691 }
692 rcu_read_unlock();
693
694 return ret;
695 }
696
697 static DEFINE_SEQLOCK(fastopen_seqlock);
698
699 void tcp_fastopen_cache_get(struct sock *sk, u16 *mss,
700 struct tcp_fastopen_cookie *cookie,
701 int *syn_loss, unsigned long *last_syn_loss)
702 {
703 struct tcp_metrics_block *tm;
704
705 rcu_read_lock();
706 tm = tcp_get_metrics(sk, __sk_dst_get(sk), false);
707 if (tm) {
708 struct tcp_fastopen_metrics *tfom = &tm->tcpm_fastopen;
709 unsigned int seq;
710
711 do {
712 seq = read_seqbegin(&fastopen_seqlock);
713 if (tfom->mss)
714 *mss = tfom->mss;
715 *cookie = tfom->cookie;
716 *syn_loss = tfom->syn_loss;
717 *last_syn_loss = *syn_loss ? tfom->last_syn_loss : 0;
718 } while (read_seqretry(&fastopen_seqlock, seq));
719 }
720 rcu_read_unlock();
721 }
722
723 void tcp_fastopen_cache_set(struct sock *sk, u16 mss,
724 struct tcp_fastopen_cookie *cookie, bool syn_lost)
725 {
726 struct dst_entry *dst = __sk_dst_get(sk);
727 struct tcp_metrics_block *tm;
728
729 if (!dst)
730 return;
731 rcu_read_lock();
732 tm = tcp_get_metrics(sk, dst, true);
733 if (tm) {
734 struct tcp_fastopen_metrics *tfom = &tm->tcpm_fastopen;
735
736 write_seqlock_bh(&fastopen_seqlock);
737 if (mss)
738 tfom->mss = mss;
739 if (cookie && cookie->len > 0)
740 tfom->cookie = *cookie;
741 if (syn_lost) {
742 ++tfom->syn_loss;
743 tfom->last_syn_loss = jiffies;
744 } else
745 tfom->syn_loss = 0;
746 write_sequnlock_bh(&fastopen_seqlock);
747 }
748 rcu_read_unlock();
749 }
750
751 static struct genl_family tcp_metrics_nl_family = {
752 .id = GENL_ID_GENERATE,
753 .hdrsize = 0,
754 .name = TCP_METRICS_GENL_NAME,
755 .version = TCP_METRICS_GENL_VERSION,
756 .maxattr = TCP_METRICS_ATTR_MAX,
757 .netnsok = true,
758 };
759
760 static struct nla_policy tcp_metrics_nl_policy[TCP_METRICS_ATTR_MAX + 1] = {
761 [TCP_METRICS_ATTR_ADDR_IPV4] = { .type = NLA_U32, },
762 [TCP_METRICS_ATTR_ADDR_IPV6] = { .type = NLA_BINARY,
763 .len = sizeof(struct in6_addr), },
764 /* Following attributes are not received for GET/DEL,
765 * we keep them for reference
766 */
767 #if 0
768 [TCP_METRICS_ATTR_AGE] = { .type = NLA_MSECS, },
769 [TCP_METRICS_ATTR_TW_TSVAL] = { .type = NLA_U32, },
770 [TCP_METRICS_ATTR_TW_TS_STAMP] = { .type = NLA_S32, },
771 [TCP_METRICS_ATTR_VALS] = { .type = NLA_NESTED, },
772 [TCP_METRICS_ATTR_FOPEN_MSS] = { .type = NLA_U16, },
773 [TCP_METRICS_ATTR_FOPEN_SYN_DROPS] = { .type = NLA_U16, },
774 [TCP_METRICS_ATTR_FOPEN_SYN_DROP_TS] = { .type = NLA_MSECS, },
775 [TCP_METRICS_ATTR_FOPEN_COOKIE] = { .type = NLA_BINARY,
776 .len = TCP_FASTOPEN_COOKIE_MAX, },
777 #endif
778 };
779
780 /* Add attributes, caller cancels its header on failure */
781 static int tcp_metrics_fill_info(struct sk_buff *msg,
782 struct tcp_metrics_block *tm)
783 {
784 struct nlattr *nest;
785 int i;
786
787 switch (tm->tcpm_daddr.family) {
788 case AF_INET:
789 if (nla_put_in_addr(msg, TCP_METRICS_ATTR_ADDR_IPV4,
790 tm->tcpm_daddr.addr.a4) < 0)
791 goto nla_put_failure;
792 if (nla_put_in_addr(msg, TCP_METRICS_ATTR_SADDR_IPV4,
793 tm->tcpm_saddr.addr.a4) < 0)
794 goto nla_put_failure;
795 break;
796 case AF_INET6:
797 if (nla_put_in6_addr(msg, TCP_METRICS_ATTR_ADDR_IPV6,
798 &tm->tcpm_daddr.addr.in6) < 0)
799 goto nla_put_failure;
800 if (nla_put_in6_addr(msg, TCP_METRICS_ATTR_SADDR_IPV6,
801 &tm->tcpm_saddr.addr.in6) < 0)
802 goto nla_put_failure;
803 break;
804 default:
805 return -EAFNOSUPPORT;
806 }
807
808 if (nla_put_msecs(msg, TCP_METRICS_ATTR_AGE,
809 jiffies - tm->tcpm_stamp) < 0)
810 goto nla_put_failure;
811 if (tm->tcpm_ts_stamp) {
812 if (nla_put_s32(msg, TCP_METRICS_ATTR_TW_TS_STAMP,
813 (s32) (get_seconds() - tm->tcpm_ts_stamp)) < 0)
814 goto nla_put_failure;
815 if (nla_put_u32(msg, TCP_METRICS_ATTR_TW_TSVAL,
816 tm->tcpm_ts) < 0)
817 goto nla_put_failure;
818 }
819
820 {
821 int n = 0;
822
823 nest = nla_nest_start(msg, TCP_METRICS_ATTR_VALS);
824 if (!nest)
825 goto nla_put_failure;
826 for (i = 0; i < TCP_METRIC_MAX_KERNEL + 1; i++) {
827 u32 val = tm->tcpm_vals[i];
828
829 if (!val)
830 continue;
831 if (i == TCP_METRIC_RTT) {
832 if (nla_put_u32(msg, TCP_METRIC_RTT_US + 1,
833 val) < 0)
834 goto nla_put_failure;
835 n++;
836 val = max(val / 1000, 1U);
837 }
838 if (i == TCP_METRIC_RTTVAR) {
839 if (nla_put_u32(msg, TCP_METRIC_RTTVAR_US + 1,
840 val) < 0)
841 goto nla_put_failure;
842 n++;
843 val = max(val / 1000, 1U);
844 }
845 if (nla_put_u32(msg, i + 1, val) < 0)
846 goto nla_put_failure;
847 n++;
848 }
849 if (n)
850 nla_nest_end(msg, nest);
851 else
852 nla_nest_cancel(msg, nest);
853 }
854
855 {
856 struct tcp_fastopen_metrics tfom_copy[1], *tfom;
857 unsigned int seq;
858
859 do {
860 seq = read_seqbegin(&fastopen_seqlock);
861 tfom_copy[0] = tm->tcpm_fastopen;
862 } while (read_seqretry(&fastopen_seqlock, seq));
863
864 tfom = tfom_copy;
865 if (tfom->mss &&
866 nla_put_u16(msg, TCP_METRICS_ATTR_FOPEN_MSS,
867 tfom->mss) < 0)
868 goto nla_put_failure;
869 if (tfom->syn_loss &&
870 (nla_put_u16(msg, TCP_METRICS_ATTR_FOPEN_SYN_DROPS,
871 tfom->syn_loss) < 0 ||
872 nla_put_msecs(msg, TCP_METRICS_ATTR_FOPEN_SYN_DROP_TS,
873 jiffies - tfom->last_syn_loss) < 0))
874 goto nla_put_failure;
875 if (tfom->cookie.len > 0 &&
876 nla_put(msg, TCP_METRICS_ATTR_FOPEN_COOKIE,
877 tfom->cookie.len, tfom->cookie.val) < 0)
878 goto nla_put_failure;
879 }
880
881 return 0;
882
883 nla_put_failure:
884 return -EMSGSIZE;
885 }
886
887 static int tcp_metrics_dump_info(struct sk_buff *skb,
888 struct netlink_callback *cb,
889 struct tcp_metrics_block *tm)
890 {
891 void *hdr;
892
893 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
894 &tcp_metrics_nl_family, NLM_F_MULTI,
895 TCP_METRICS_CMD_GET);
896 if (!hdr)
897 return -EMSGSIZE;
898
899 if (tcp_metrics_fill_info(skb, tm) < 0)
900 goto nla_put_failure;
901
902 genlmsg_end(skb, hdr);
903 return 0;
904
905 nla_put_failure:
906 genlmsg_cancel(skb, hdr);
907 return -EMSGSIZE;
908 }
909
910 static int tcp_metrics_nl_dump(struct sk_buff *skb,
911 struct netlink_callback *cb)
912 {
913 struct net *net = sock_net(skb->sk);
914 unsigned int max_rows = 1U << tcp_metrics_hash_log;
915 unsigned int row, s_row = cb->args[0];
916 int s_col = cb->args[1], col = s_col;
917
918 for (row = s_row; row < max_rows; row++, s_col = 0) {
919 struct tcp_metrics_block *tm;
920 struct tcpm_hash_bucket *hb = tcp_metrics_hash + row;
921
922 rcu_read_lock();
923 for (col = 0, tm = rcu_dereference(hb->chain); tm;
924 tm = rcu_dereference(tm->tcpm_next), col++) {
925 if (!net_eq(tm_net(tm), net))
926 continue;
927 if (col < s_col)
928 continue;
929 if (tcp_metrics_dump_info(skb, cb, tm) < 0) {
930 rcu_read_unlock();
931 goto done;
932 }
933 }
934 rcu_read_unlock();
935 }
936
937 done:
938 cb->args[0] = row;
939 cb->args[1] = col;
940 return skb->len;
941 }
942
943 static int __parse_nl_addr(struct genl_info *info, struct inetpeer_addr *addr,
944 unsigned int *hash, int optional, int v4, int v6)
945 {
946 struct nlattr *a;
947
948 a = info->attrs[v4];
949 if (a) {
950 addr->family = AF_INET;
951 addr->addr.a4 = nla_get_in_addr(a);
952 if (hash)
953 *hash = (__force unsigned int) addr->addr.a4;
954 return 0;
955 }
956 a = info->attrs[v6];
957 if (a) {
958 if (nla_len(a) != sizeof(struct in6_addr))
959 return -EINVAL;
960 addr->family = AF_INET6;
961 addr->addr.in6 = nla_get_in6_addr(a);
962 if (hash)
963 *hash = ipv6_addr_hash(&addr->addr.in6);
964 return 0;
965 }
966 return optional ? 1 : -EAFNOSUPPORT;
967 }
968
969 static int parse_nl_addr(struct genl_info *info, struct inetpeer_addr *addr,
970 unsigned int *hash, int optional)
971 {
972 return __parse_nl_addr(info, addr, hash, optional,
973 TCP_METRICS_ATTR_ADDR_IPV4,
974 TCP_METRICS_ATTR_ADDR_IPV6);
975 }
976
977 static int parse_nl_saddr(struct genl_info *info, struct inetpeer_addr *addr)
978 {
979 return __parse_nl_addr(info, addr, NULL, 0,
980 TCP_METRICS_ATTR_SADDR_IPV4,
981 TCP_METRICS_ATTR_SADDR_IPV6);
982 }
983
984 static int tcp_metrics_nl_cmd_get(struct sk_buff *skb, struct genl_info *info)
985 {
986 struct tcp_metrics_block *tm;
987 struct inetpeer_addr saddr, daddr;
988 unsigned int hash;
989 struct sk_buff *msg;
990 struct net *net = genl_info_net(info);
991 void *reply;
992 int ret;
993 bool src = true;
994
995 ret = parse_nl_addr(info, &daddr, &hash, 0);
996 if (ret < 0)
997 return ret;
998
999 ret = parse_nl_saddr(info, &saddr);
1000 if (ret < 0)
1001 src = false;
1002
1003 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1004 if (!msg)
1005 return -ENOMEM;
1006
1007 reply = genlmsg_put_reply(msg, info, &tcp_metrics_nl_family, 0,
1008 info->genlhdr->cmd);
1009 if (!reply)
1010 goto nla_put_failure;
1011
1012 hash ^= net_hash_mix(net);
1013 hash = hash_32(hash, tcp_metrics_hash_log);
1014 ret = -ESRCH;
1015 rcu_read_lock();
1016 for (tm = rcu_dereference(tcp_metrics_hash[hash].chain); tm;
1017 tm = rcu_dereference(tm->tcpm_next)) {
1018 if (addr_same(&tm->tcpm_daddr, &daddr) &&
1019 (!src || addr_same(&tm->tcpm_saddr, &saddr)) &&
1020 net_eq(tm_net(tm), net)) {
1021 ret = tcp_metrics_fill_info(msg, tm);
1022 break;
1023 }
1024 }
1025 rcu_read_unlock();
1026 if (ret < 0)
1027 goto out_free;
1028
1029 genlmsg_end(msg, reply);
1030 return genlmsg_reply(msg, info);
1031
1032 nla_put_failure:
1033 ret = -EMSGSIZE;
1034
1035 out_free:
1036 nlmsg_free(msg);
1037 return ret;
1038 }
1039
1040 static void tcp_metrics_flush_all(struct net *net)
1041 {
1042 unsigned int max_rows = 1U << tcp_metrics_hash_log;
1043 struct tcpm_hash_bucket *hb = tcp_metrics_hash;
1044 struct tcp_metrics_block *tm;
1045 unsigned int row;
1046
1047 for (row = 0; row < max_rows; row++, hb++) {
1048 struct tcp_metrics_block __rcu **pp;
1049 spin_lock_bh(&tcp_metrics_lock);
1050 pp = &hb->chain;
1051 for (tm = deref_locked(*pp); tm; tm = deref_locked(*pp)) {
1052 if (net_eq(tm_net(tm), net)) {
1053 *pp = tm->tcpm_next;
1054 kfree_rcu(tm, rcu_head);
1055 } else {
1056 pp = &tm->tcpm_next;
1057 }
1058 }
1059 spin_unlock_bh(&tcp_metrics_lock);
1060 }
1061 }
1062
1063 static int tcp_metrics_nl_cmd_del(struct sk_buff *skb, struct genl_info *info)
1064 {
1065 struct tcpm_hash_bucket *hb;
1066 struct tcp_metrics_block *tm;
1067 struct tcp_metrics_block __rcu **pp;
1068 struct inetpeer_addr saddr, daddr;
1069 unsigned int hash;
1070 struct net *net = genl_info_net(info);
1071 int ret;
1072 bool src = true, found = false;
1073
1074 ret = parse_nl_addr(info, &daddr, &hash, 1);
1075 if (ret < 0)
1076 return ret;
1077 if (ret > 0) {
1078 tcp_metrics_flush_all(net);
1079 return 0;
1080 }
1081 ret = parse_nl_saddr(info, &saddr);
1082 if (ret < 0)
1083 src = false;
1084
1085 hash ^= net_hash_mix(net);
1086 hash = hash_32(hash, tcp_metrics_hash_log);
1087 hb = tcp_metrics_hash + hash;
1088 pp = &hb->chain;
1089 spin_lock_bh(&tcp_metrics_lock);
1090 for (tm = deref_locked(*pp); tm; tm = deref_locked(*pp)) {
1091 if (addr_same(&tm->tcpm_daddr, &daddr) &&
1092 (!src || addr_same(&tm->tcpm_saddr, &saddr)) &&
1093 net_eq(tm_net(tm), net)) {
1094 *pp = tm->tcpm_next;
1095 kfree_rcu(tm, rcu_head);
1096 found = true;
1097 } else {
1098 pp = &tm->tcpm_next;
1099 }
1100 }
1101 spin_unlock_bh(&tcp_metrics_lock);
1102 if (!found)
1103 return -ESRCH;
1104 return 0;
1105 }
1106
1107 static const struct genl_ops tcp_metrics_nl_ops[] = {
1108 {
1109 .cmd = TCP_METRICS_CMD_GET,
1110 .doit = tcp_metrics_nl_cmd_get,
1111 .dumpit = tcp_metrics_nl_dump,
1112 .policy = tcp_metrics_nl_policy,
1113 },
1114 {
1115 .cmd = TCP_METRICS_CMD_DEL,
1116 .doit = tcp_metrics_nl_cmd_del,
1117 .policy = tcp_metrics_nl_policy,
1118 .flags = GENL_ADMIN_PERM,
1119 },
1120 };
1121
1122 static unsigned int tcpmhash_entries;
1123 static int __init set_tcpmhash_entries(char *str)
1124 {
1125 ssize_t ret;
1126
1127 if (!str)
1128 return 0;
1129
1130 ret = kstrtouint(str, 0, &tcpmhash_entries);
1131 if (ret)
1132 return 0;
1133
1134 return 1;
1135 }
1136 __setup("tcpmhash_entries=", set_tcpmhash_entries);
1137
1138 static int __net_init tcp_net_metrics_init(struct net *net)
1139 {
1140 size_t size;
1141 unsigned int slots;
1142
1143 if (!net_eq(net, &init_net))
1144 return 0;
1145
1146 slots = tcpmhash_entries;
1147 if (!slots) {
1148 if (totalram_pages >= 128 * 1024)
1149 slots = 16 * 1024;
1150 else
1151 slots = 8 * 1024;
1152 }
1153
1154 tcp_metrics_hash_log = order_base_2(slots);
1155 size = sizeof(struct tcpm_hash_bucket) << tcp_metrics_hash_log;
1156
1157 tcp_metrics_hash = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
1158 if (!tcp_metrics_hash)
1159 tcp_metrics_hash = vzalloc(size);
1160
1161 if (!tcp_metrics_hash)
1162 return -ENOMEM;
1163
1164 return 0;
1165 }
1166
1167 static void __net_exit tcp_net_metrics_exit(struct net *net)
1168 {
1169 tcp_metrics_flush_all(net);
1170 }
1171
1172 static __net_initdata struct pernet_operations tcp_net_metrics_ops = {
1173 .init = tcp_net_metrics_init,
1174 .exit = tcp_net_metrics_exit,
1175 };
1176
1177 void __init tcp_metrics_init(void)
1178 {
1179 int ret;
1180
1181 ret = register_pernet_subsys(&tcp_net_metrics_ops);
1182 if (ret < 0)
1183 panic("Could not allocate the tcp_metrics hash table\n");
1184
1185 ret = genl_register_family_with_ops(&tcp_metrics_nl_family,
1186 tcp_metrics_nl_ops);
1187 if (ret < 0)
1188 panic("Could not register tcp_metrics generic netlink\n");
1189 }
This page took 0.165773 seconds and 5 git commands to generate.