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