Skip to content

Commit 81166dd

Browse files
committed
tcp: Move timestamps from inetpeer to metrics cache.
With help from Lin Ming. Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 94334d5 commit 81166dd

9 files changed

Lines changed: 149 additions & 127 deletions

File tree

‎include/net/inetpeer.h‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,13 @@ struct inet_peer {
4646
};
4747
/*
4848
* Once inet_peer is queued for deletion (refcnt == -1), following fields
49-
* are not available: rid, ip_id_count, tcp_ts, tcp_ts_stamp
49+
* are not available: rid, ip_id_count
5050
* We can share memory with rcu_head to help keep inet_peer small.
5151
*/
5252
union {
5353
struct {
5454
atomic_t rid; /* Frag reception counter */
5555
atomic_t ip_id_count; /* IP ID for the next packet */
56-
__u32 tcp_ts;
57-
__u32 tcp_ts_stamp;
5856
};
5957
struct rcu_head rcu;
6058
struct inet_peer *gc_next;

‎include/net/tcp.h‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,10 @@ extern void tcp_clear_retrans(struct tcp_sock *tp);
390390
extern void tcp_update_metrics(struct sock *sk);
391391
extern void tcp_init_metrics(struct sock *sk);
392392
extern void tcp_metrics_init(void);
393-
extern bool tcp_peer_is_proven(struct request_sock *req, struct dst_entry *dst);
393+
extern bool tcp_peer_is_proven(struct request_sock *req, struct dst_entry *dst, bool paws_check);
394+
extern bool tcp_remember_stamp(struct sock *sk);
395+
extern bool tcp_tw_remember_stamp(struct inet_timewait_sock *tw);
396+
extern void tcp_fetch_timewait_stamp(struct sock *sk, struct dst_entry *dst);
394397
extern void tcp_disable_fack(struct tcp_sock *tp);
395398
extern void tcp_close(struct sock *sk, long timeout);
396399
extern void tcp_init_sock(struct sock *sk);

‎net/ipv4/inetpeer.c‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,6 @@ struct inet_peer *inet_getpeer(struct inet_peer_base *base,
508508
(daddr->family == AF_INET) ?
509509
secure_ip_id(daddr->addr.a4) :
510510
secure_ipv6_id(daddr->addr.a6));
511-
p->tcp_ts_stamp = 0;
512511
p->metrics[RTAX_LOCK-1] = INETPEER_METRICS_NEW;
513512
p->rate_tokens = 0;
514513
p->rate_last = 0;

‎net/ipv4/route.c‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2846,7 +2846,7 @@ static int rt_fill_info(struct net *net,
28462846
struct rtmsg *r;
28472847
struct nlmsghdr *nlh;
28482848
unsigned long expires = 0;
2849-
u32 id = 0, ts = 0, tsage = 0, error;
2849+
u32 id = 0, error;
28502850

28512851
nlh = nlmsg_put(skb, pid, seq, event, sizeof(*r), flags);
28522852
if (nlh == NULL)
@@ -2903,10 +2903,6 @@ static int rt_fill_info(struct net *net,
29032903
const struct inet_peer *peer = rt_peer_ptr(rt);
29042904
inet_peer_refcheck(peer);
29052905
id = atomic_read(&peer->ip_id_count) & 0xffff;
2906-
if (peer->tcp_ts_stamp) {
2907-
ts = peer->tcp_ts;
2908-
tsage = get_seconds() - peer->tcp_ts_stamp;
2909-
}
29102906
expires = ACCESS_ONCE(peer->pmtu_expires);
29112907
if (expires) {
29122908
if (time_before(jiffies, expires))
@@ -2942,7 +2938,7 @@ static int rt_fill_info(struct net *net,
29422938
goto nla_put_failure;
29432939
}
29442940

2945-
if (rtnl_put_cacheinfo(skb, &rt->dst, id, ts, tsage,
2941+
if (rtnl_put_cacheinfo(skb, &rt->dst, id, 0, 0,
29462942
expires, error) < 0)
29472943
goto nla_put_failure;
29482944

‎net/ipv4/tcp_ipv4.c‎

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -209,22 +209,8 @@ int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
209209
}
210210

211211
if (tcp_death_row.sysctl_tw_recycle &&
212-
!tp->rx_opt.ts_recent_stamp && fl4->daddr == daddr) {
213-
struct inet_peer *peer = rt_get_peer(rt, fl4->daddr);
214-
/*
215-
* VJ's idea. We save last timestamp seen from
216-
* the destination in peer table, when entering state
217-
* TIME-WAIT * and initialize rx_opt.ts_recent from it,
218-
* when trying new connection.
219-
*/
220-
if (peer) {
221-
inet_peer_refcheck(peer);
222-
if ((u32)get_seconds() - peer->tcp_ts_stamp <= TCP_PAWS_MSL) {
223-
tp->rx_opt.ts_recent_stamp = peer->tcp_ts_stamp;
224-
tp->rx_opt.ts_recent = peer->tcp_ts;
225-
}
226-
}
227-
}
212+
!tp->rx_opt.ts_recent_stamp && fl4->daddr == daddr)
213+
tcp_fetch_timewait_stamp(sk, &rt->dst);
228214

229215
inet->inet_dport = usin->sin_port;
230216
inet->inet_daddr = daddr;
@@ -1375,7 +1361,6 @@ int tcp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
13751361
isn = cookie_v4_init_sequence(sk, skb, &req->mss);
13761362
req->cookie_ts = tmp_opt.tstamp_ok;
13771363
} else if (!isn) {
1378-
struct inet_peer *peer = NULL;
13791364
struct flowi4 fl4;
13801365

13811366
/* VJ's idea. We save last timestamp seen
@@ -1390,12 +1375,8 @@ int tcp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
13901375
if (tmp_opt.saw_tstamp &&
13911376
tcp_death_row.sysctl_tw_recycle &&
13921377
(dst = inet_csk_route_req(sk, &fl4, req, want_cookie)) != NULL &&
1393-
fl4.daddr == saddr &&
1394-
(peer = rt_get_peer((struct rtable *)dst, fl4.daddr)) != NULL) {
1395-
inet_peer_refcheck(peer);
1396-
if ((u32)get_seconds() - peer->tcp_ts_stamp < TCP_PAWS_MSL &&
1397-
(s32)(peer->tcp_ts - req->ts_recent) >
1398-
TCP_PAWS_WINDOW) {
1378+
fl4.daddr == saddr) {
1379+
if (!tcp_peer_is_proven(req, dst, true)) {
13991380
NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_PAWSPASSIVEREJECTED);
14001381
goto drop_and_release;
14011382
}
@@ -1404,8 +1385,7 @@ int tcp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
14041385
else if (!sysctl_tcp_syncookies &&
14051386
(sysctl_max_syn_backlog - inet_csk_reqsk_queue_len(sk) <
14061387
(sysctl_max_syn_backlog >> 2)) &&
1407-
(!peer || !peer->tcp_ts_stamp) &&
1408-
!tcp_peer_is_proven(req, dst)) {
1388+
!tcp_peer_is_proven(req, dst, false)) {
14091389
/* Without syncookies last quarter of
14101390
* backlog is filled with destinations,
14111391
* proven to be alive.

‎net/ipv4/tcp_metrics.c‎

Lines changed: 131 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ struct tcp_metrics_block {
3434
struct tcp_metrics_block __rcu *tcpm_next;
3535
struct inetpeer_addr tcpm_addr;
3636
unsigned long tcpm_stamp;
37+
u32 tcpm_ts;
38+
u32 tcpm_ts_stamp;
3739
u32 tcpm_lock;
3840
u32 tcpm_vals[TCP_METRIC_MAX];
3941
};
@@ -114,6 +116,8 @@ static void tcpm_suck_dst(struct tcp_metrics_block *tm, struct dst_entry *dst)
114116
tm->tcpm_vals[TCP_METRIC_SSTHRESH] = dst_metric_raw(dst, RTAX_SSTHRESH);
115117
tm->tcpm_vals[TCP_METRIC_CWND] = dst_metric_raw(dst, RTAX_CWND);
116118
tm->tcpm_vals[TCP_METRIC_REORDERING] = dst_metric_raw(dst, RTAX_REORDERING);
119+
tm->tcpm_ts = 0;
120+
tm->tcpm_ts_stamp = 0;
117121
}
118122

119123
static struct tcp_metrics_block *tcpm_new(struct dst_entry *dst,
@@ -230,6 +234,45 @@ static struct tcp_metrics_block *__tcp_get_metrics_req(struct request_sock *req,
230234
return tm;
231235
}
232236

237+
static struct tcp_metrics_block *__tcp_get_metrics_tw(struct inet_timewait_sock *tw)
238+
{
239+
struct inet6_timewait_sock *tw6;
240+
struct tcp_metrics_block *tm;
241+
struct inetpeer_addr addr;
242+
unsigned int hash;
243+
struct net *net;
244+
245+
addr.family = tw->tw_family;
246+
switch (addr.family) {
247+
case AF_INET:
248+
addr.addr.a4 = tw->tw_daddr;
249+
hash = (__force unsigned int) addr.addr.a4;
250+
break;
251+
case AF_INET6:
252+
tw6 = inet6_twsk((struct sock *)tw);
253+
*(struct in6_addr *)addr.addr.a6 = tw6->tw_v6_daddr;
254+
hash = ((__force unsigned int) addr.addr.a6[0] ^
255+
(__force unsigned int) addr.addr.a6[1] ^
256+
(__force unsigned int) addr.addr.a6[2] ^
257+
(__force unsigned int) addr.addr.a6[3]);
258+
break;
259+
default:
260+
return NULL;
261+
}
262+
263+
hash ^= (hash >> 24) ^ (hash >> 16) ^ (hash >> 8);
264+
265+
net = twsk_net(tw);
266+
hash &= net->ipv4.tcp_metrics_hash_mask;
267+
268+
for (tm = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain); tm;
269+
tm = rcu_dereference(tm->tcpm_next)) {
270+
if (addr_same(&tm->tcpm_addr, &addr))
271+
break;
272+
}
273+
return tm;
274+
}
275+
233276
static struct tcp_metrics_block *tcp_get_metrics(struct sock *sk,
234277
struct dst_entry *dst,
235278
bool create)
@@ -496,7 +539,7 @@ void tcp_init_metrics(struct sock *sk)
496539
tp->snd_cwnd_stamp = tcp_time_stamp;
497540
}
498541

499-
bool tcp_peer_is_proven(struct request_sock *req, struct dst_entry *dst)
542+
bool tcp_peer_is_proven(struct request_sock *req, struct dst_entry *dst, bool paws_check)
500543
{
501544
struct tcp_metrics_block *tm;
502545
bool ret;
@@ -506,16 +549,99 @@ bool tcp_peer_is_proven(struct request_sock *req, struct dst_entry *dst)
506549

507550
rcu_read_lock();
508551
tm = __tcp_get_metrics_req(req, dst);
509-
if (tm && tcp_metric_get(tm, TCP_METRIC_RTT))
510-
ret = true;
511-
else
512-
ret = false;
552+
if (paws_check) {
553+
if (tm &&
554+
(u32)get_seconds() - tm->tcpm_ts_stamp < TCP_PAWS_MSL &&
555+
(s32)(tm->tcpm_ts - req->ts_recent) > TCP_PAWS_WINDOW)
556+
ret = false;
557+
else
558+
ret = true;
559+
} else {
560+
if (tm && tcp_metric_get(tm, TCP_METRIC_RTT) && tm->tcpm_ts_stamp)
561+
ret = true;
562+
else
563+
ret = false;
564+
}
513565
rcu_read_unlock();
514566

515567
return ret;
516568
}
517569
EXPORT_SYMBOL_GPL(tcp_peer_is_proven);
518570

571+
void tcp_fetch_timewait_stamp(struct sock *sk, struct dst_entry *dst)
572+
{
573+
struct tcp_metrics_block *tm;
574+
575+
rcu_read_lock();
576+
tm = tcp_get_metrics(sk, dst, true);
577+
if (tm) {
578+
struct tcp_sock *tp = tcp_sk(sk);
579+
580+
if ((u32)get_seconds() - tm->tcpm_ts_stamp <= TCP_PAWS_MSL) {
581+
tp->rx_opt.ts_recent_stamp = tm->tcpm_ts_stamp;
582+
tp->rx_opt.ts_recent = tm->tcpm_ts;
583+
}
584+
}
585+
rcu_read_unlock();
586+
}
587+
EXPORT_SYMBOL_GPL(tcp_fetch_timewait_stamp);
588+
589+
/* VJ's idea. Save last timestamp seen from this destination and hold
590+
* it at least for normal timewait interval to use for duplicate
591+
* segment detection in subsequent connections, before they enter
592+
* synchronized state.
593+
*/
594+
bool tcp_remember_stamp(struct sock *sk)
595+
{
596+
struct dst_entry *dst = __sk_dst_get(sk);
597+
bool ret = false;
598+
599+
if (dst) {
600+
struct tcp_metrics_block *tm;
601+
602+
rcu_read_lock();
603+
tm = tcp_get_metrics(sk, dst, true);
604+
if (tm) {
605+
struct tcp_sock *tp = tcp_sk(sk);
606+
607+
if ((s32)(tm->tcpm_ts - tp->rx_opt.ts_recent) <= 0 ||
608+
((u32)get_seconds() - tm->tcpm_ts_stamp > TCP_PAWS_MSL &&
609+
tm->tcpm_ts_stamp <= (u32)tp->rx_opt.ts_recent_stamp)) {
610+
tm->tcpm_ts_stamp = (u32)tp->rx_opt.ts_recent_stamp;
611+
tm->tcpm_ts = tp->rx_opt.ts_recent;
612+
}
613+
ret = true;
614+
}
615+
rcu_read_unlock();
616+
}
617+
return ret;
618+
}
619+
620+
bool tcp_tw_remember_stamp(struct inet_timewait_sock *tw)
621+
{
622+
struct tcp_metrics_block *tm;
623+
bool ret = false;
624+
625+
rcu_read_lock();
626+
tm = __tcp_get_metrics_tw(tw);
627+
if (tw) {
628+
const struct tcp_timewait_sock *tcptw;
629+
struct sock *sk = (struct sock *) tw;
630+
631+
tcptw = tcp_twsk(sk);
632+
if ((s32)(tm->tcpm_ts - tcptw->tw_ts_recent) <= 0 ||
633+
((u32)get_seconds() - tm->tcpm_ts_stamp > TCP_PAWS_MSL &&
634+
tm->tcpm_ts_stamp <= (u32)tcptw->tw_ts_recent_stamp)) {
635+
tm->tcpm_ts_stamp = (u32)tcptw->tw_ts_recent_stamp;
636+
tm->tcpm_ts = tcptw->tw_ts_recent;
637+
}
638+
ret = true;
639+
}
640+
rcu_read_unlock();
641+
642+
return ret;
643+
}
644+
519645
static unsigned long tcpmhash_entries;
520646
static int __init set_tcpmhash_entries(char *str)
521647
{

‎net/ipv4/tcp_minisocks.c‎

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -49,52 +49,6 @@ struct inet_timewait_death_row tcp_death_row = {
4949
};
5050
EXPORT_SYMBOL_GPL(tcp_death_row);
5151

52-
/* VJ's idea. Save last timestamp seen from this destination
53-
* and hold it at least for normal timewait interval to use for duplicate
54-
* segment detection in subsequent connections, before they enter synchronized
55-
* state.
56-
*/
57-
58-
static bool tcp_remember_stamp(struct sock *sk)
59-
{
60-
const struct inet_connection_sock *icsk = inet_csk(sk);
61-
struct tcp_sock *tp = tcp_sk(sk);
62-
struct inet_peer *peer;
63-
64-
peer = icsk->icsk_af_ops->get_peer(sk);
65-
if (peer) {
66-
if ((s32)(peer->tcp_ts - tp->rx_opt.ts_recent) <= 0 ||
67-
((u32)get_seconds() - peer->tcp_ts_stamp > TCP_PAWS_MSL &&
68-
peer->tcp_ts_stamp <= (u32)tp->rx_opt.ts_recent_stamp)) {
69-
peer->tcp_ts_stamp = (u32)tp->rx_opt.ts_recent_stamp;
70-
peer->tcp_ts = tp->rx_opt.ts_recent;
71-
}
72-
return true;
73-
}
74-
75-
return false;
76-
}
77-
78-
static bool tcp_tw_remember_stamp(struct inet_timewait_sock *tw)
79-
{
80-
const struct tcp_timewait_sock *tcptw;
81-
struct sock *sk = (struct sock *) tw;
82-
struct inet_peer *peer;
83-
84-
tcptw = tcp_twsk(sk);
85-
peer = tcptw->tw_peer;
86-
if (peer) {
87-
if ((s32)(peer->tcp_ts - tcptw->tw_ts_recent) <= 0 ||
88-
((u32)get_seconds() - peer->tcp_ts_stamp > TCP_PAWS_MSL &&
89-
peer->tcp_ts_stamp <= (u32)tcptw->tw_ts_recent_stamp)) {
90-
peer->tcp_ts_stamp = (u32)tcptw->tw_ts_recent_stamp;
91-
peer->tcp_ts = tcptw->tw_ts_recent;
92-
}
93-
return true;
94-
}
95-
return false;
96-
}
97-
9852
static bool tcp_in_window(u32 seq, u32 end_seq, u32 s_win, u32 e_win)
9953
{
10054
if (seq == s_win)

‎net/ipv6/route.c‎

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2348,13 +2348,11 @@ static int rt6_fill_node(struct net *net,
23482348
int iif, int type, u32 pid, u32 seq,
23492349
int prefix, int nowait, unsigned int flags)
23502350
{
2351-
const struct inet_peer *peer;
23522351
struct rtmsg *rtm;
23532352
struct nlmsghdr *nlh;
23542353
long expires;
23552354
u32 table;
23562355
struct neighbour *n;
2357-
u32 ts, tsage;
23582356

23592357
if (prefix) { /* user wants prefix routes only */
23602358
if (!(rt->rt6i_flags & RTF_PREFIX_RT)) {
@@ -2473,16 +2471,7 @@ static int rt6_fill_node(struct net *net,
24732471
else
24742472
expires = INT_MAX;
24752473

2476-
peer = NULL;
2477-
if (rt6_has_peer(rt))
2478-
peer = rt6_peer_ptr(rt);
2479-
ts = tsage = 0;
2480-
if (peer && peer->tcp_ts_stamp) {
2481-
ts = peer->tcp_ts;
2482-
tsage = get_seconds() - peer->tcp_ts_stamp;
2483-
}
2484-
2485-
if (rtnl_put_cacheinfo(skb, &rt->dst, 0, ts, tsage,
2474+
if (rtnl_put_cacheinfo(skb, &rt->dst, 0, 0, 0,
24862475
expires, rt->dst.error) < 0)
24872476
goto nla_put_failure;
24882477

0 commit comments

Comments
 (0)