Skip to content

Commit 58d607d

Browse files
edumazetdavem330
authored andcommitted
tcp: provide skb->hash to synack packets
In commit b73c3d0 ("net: Save TX flow hash in sock and set in skbuf on xmit"), Tom provided a l4 hash to most outgoing TCP packets. We'd like to provide one as well for SYNACK packets, so that all packets of a given flow share same txhash, to later enable bonding driver to also use skb->hash to perform slave selection. Note that a SYNACK retransmit shuffles the tx hash, as Tom did in commit 265f94f ("net: Recompute sk_txhash on negative routing advice") for established sockets. This has nice effect making TCP flows resilient to some kind of black holes, even at connection establish phase. Signed-off-by: Eric Dumazet <edumazet@google.com> Cc: Tom Herbert <tom@herbertland.com> Cc: Mahesh Bandewar <maheshb@google.com> Acked-by: Tom Herbert <tom@herbertland.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent bbe8373 commit 58d607d

File tree

6 files changed

+14
-6
lines changed

6 files changed

+14
-6
lines changed

include/linux/tcp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ struct tcp_request_sock {
113113
struct inet_request_sock req;
114114
const struct tcp_request_sock_ops *af_specific;
115115
bool tfo_listener;
116+
u32 txhash;
116117
u32 rcv_isn;
117118
u32 snt_isn;
118119
u32 snt_synack; /* synack sent time */

include/net/sock.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1654,12 +1654,16 @@ static inline void sock_graft(struct sock *sk, struct socket *parent)
16541654
kuid_t sock_i_uid(struct sock *sk);
16551655
unsigned long sock_i_ino(struct sock *sk);
16561656

1657-
static inline void sk_set_txhash(struct sock *sk)
1657+
static inline u32 net_tx_rndhash(void)
16581658
{
1659-
sk->sk_txhash = prandom_u32();
1659+
u32 v = prandom_u32();
1660+
1661+
return v ?: 1;
1662+
}
16601663

1661-
if (unlikely(!sk->sk_txhash))
1662-
sk->sk_txhash = 1;
1664+
static inline void sk_set_txhash(struct sock *sk)
1665+
{
1666+
sk->sk_txhash = net_tx_rndhash();
16631667
}
16641668

16651669
static inline void sk_rethink_txhash(struct sock *sk)

net/ipv4/tcp_input.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6228,6 +6228,7 @@ int tcp_conn_request(struct request_sock_ops *rsk_ops,
62286228
}
62296229

62306230
tcp_rsk(req)->snt_isn = isn;
6231+
tcp_rsk(req)->txhash = net_tx_rndhash();
62316232
tcp_openreq_init_rwin(req, sk, dst);
62326233
fastopen = !want_cookie &&
62336234
tcp_try_fastopen(sk, skb, req, &foc, dst);

net/ipv4/tcp_ipv4.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1276,8 +1276,8 @@ struct sock *tcp_v4_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
12761276
newinet->mc_index = inet_iif(skb);
12771277
newinet->mc_ttl = ip_hdr(skb)->ttl;
12781278
newinet->rcv_tos = ip_hdr(skb)->tos;
1279+
newsk->sk_txhash = tcp_rsk(req)->txhash;
12791280
inet_csk(newsk)->icsk_ext_hdr_len = 0;
1280-
sk_set_txhash(newsk);
12811281
if (inet_opt)
12821282
inet_csk(newsk)->icsk_ext_hdr_len = inet_opt->opt.optlen;
12831283
newinet->inet_id = newtp->write_seq ^ jiffies;

net/ipv4/tcp_output.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2987,6 +2987,7 @@ struct sk_buff *tcp_make_synack(struct sock *sk, struct dst_entry *dst,
29872987
rcu_read_lock();
29882988
md5 = tcp_rsk(req)->af_specific->req_md5_lookup(sk, req_to_sk(req));
29892989
#endif
2990+
skb_set_hash(skb, tcp_rsk(req)->txhash, PKT_HASH_TYPE_L4);
29902991
tcp_header_size = tcp_synack_options(sk, req, mss, skb, &opts, md5,
29912992
foc) + sizeof(*th);
29922993

@@ -3505,6 +3506,7 @@ int tcp_rtx_synack(struct sock *sk, struct request_sock *req)
35053506
struct flowi fl;
35063507
int res;
35073508

3509+
tcp_rsk(req)->txhash = net_tx_rndhash();
35083510
res = af_ops->send_synack(sk, NULL, &fl, req, 0, NULL);
35093511
if (!res) {
35103512
TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_RETRANSSEGS);

net/ipv6/tcp_ipv6.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1090,7 +1090,7 @@ static struct sock *tcp_v6_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
10901090
newsk->sk_v6_rcv_saddr = ireq->ir_v6_loc_addr;
10911091
newsk->sk_bound_dev_if = ireq->ir_iif;
10921092

1093-
sk_set_txhash(newsk);
1093+
newsk->sk_txhash = tcp_rsk(req)->txhash;
10941094

10951095
/* Now IPv6 options...
10961096

0 commit comments

Comments
 (0)