Skip to content

Commit 877d1f6

Browse files
tomratbertdavem330
authored andcommitted
net: Set sk_txhash from a random number
This patch creates sk_set_txhash and eliminates protocol specific inet_set_txhash and ip6_set_txhash. sk_set_txhash simply sets a random number instead of performing flow dissection. sk_set_txash is also allowed to be called multiple times for the same socket, we'll need this when redoing the hash for negative routing advice. Signed-off-by: Tom Herbert <tom@herbertland.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 7a86d96 commit 877d1f6

File tree

7 files changed

+14
-41
lines changed

7 files changed

+14
-41
lines changed

include/net/ip.h

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -370,22 +370,6 @@ static inline void iph_to_flow_copy_v4addrs(struct flow_keys *flow,
370370
flow->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
371371
}
372372

373-
static inline void inet_set_txhash(struct sock *sk)
374-
{
375-
struct inet_sock *inet = inet_sk(sk);
376-
struct flow_keys keys;
377-
378-
memset(&keys, 0, sizeof(keys));
379-
380-
keys.addrs.v4addrs.src = inet->inet_saddr;
381-
keys.addrs.v4addrs.dst = inet->inet_daddr;
382-
keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
383-
keys.ports.src = inet->inet_sport;
384-
keys.ports.dst = inet->inet_dport;
385-
386-
sk->sk_txhash = flow_hash_from_keys(&keys);
387-
}
388-
389373
static inline __wsum inet_gro_compute_pseudo(struct sk_buff *skb, int proto)
390374
{
391375
const struct iphdr *iph = skb_gro_network_header(skb);

include/net/ipv6.h

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -707,25 +707,6 @@ static inline void iph_to_flow_copy_v6addrs(struct flow_keys *flow,
707707
}
708708

709709
#if IS_ENABLED(CONFIG_IPV6)
710-
static inline void ip6_set_txhash(struct sock *sk)
711-
{
712-
struct inet_sock *inet = inet_sk(sk);
713-
struct ipv6_pinfo *np = inet6_sk(sk);
714-
struct flow_keys keys;
715-
716-
memset(&keys, 0, sizeof(keys));
717-
718-
memcpy(&keys.addrs.v6addrs.src, &np->saddr,
719-
sizeof(keys.addrs.v6addrs.src));
720-
memcpy(&keys.addrs.v6addrs.dst, &sk->sk_v6_daddr,
721-
sizeof(keys.addrs.v6addrs.dst));
722-
keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
723-
keys.ports.src = inet->inet_sport;
724-
keys.ports.dst = inet->inet_dport;
725-
726-
sk->sk_txhash = flow_hash_from_keys(&keys);
727-
}
728-
729710
static inline __be32 ip6_make_flowlabel(struct net *net, struct sk_buff *skb,
730711
__be32 flowlabel, bool autolabel)
731712
{

include/net/sock.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1687,6 +1687,14 @@ static inline void sock_graft(struct sock *sk, struct socket *parent)
16871687
kuid_t sock_i_uid(struct sock *sk);
16881688
unsigned long sock_i_ino(struct sock *sk);
16891689

1690+
static inline void sk_set_txhash(struct sock *sk)
1691+
{
1692+
sk->sk_txhash = prandom_u32();
1693+
1694+
if (unlikely(!sk->sk_txhash))
1695+
sk->sk_txhash = 1;
1696+
}
1697+
16901698
static inline struct dst_entry *
16911699
__sk_dst_get(struct sock *sk)
16921700
{

net/ipv4/datagram.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ int __ip4_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len
7474
inet->inet_daddr = fl4->daddr;
7575
inet->inet_dport = usin->sin_port;
7676
sk->sk_state = TCP_ESTABLISHED;
77-
inet_set_txhash(sk);
77+
sk_set_txhash(sk);
7878
inet->inet_id = jiffies;
7979

8080
sk_dst_set(sk, &rt->dst);

net/ipv4/tcp_ipv4.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
222222
if (err)
223223
goto failure;
224224

225-
inet_set_txhash(sk);
225+
sk_set_txhash(sk);
226226

227227
rt = ip_route_newports(fl4, rt, orig_sport, orig_dport,
228228
inet->inet_sport, inet->inet_dport, sk);
@@ -1277,7 +1277,7 @@ struct sock *tcp_v4_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
12771277
newinet->mc_ttl = ip_hdr(skb)->ttl;
12781278
newinet->rcv_tos = ip_hdr(skb)->tos;
12791279
inet_csk(newsk)->icsk_ext_hdr_len = 0;
1280-
inet_set_txhash(newsk);
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/ipv6/datagram.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ static int __ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int a
199199
NULL);
200200

201201
sk->sk_state = TCP_ESTABLISHED;
202-
ip6_set_txhash(sk);
202+
sk_set_txhash(sk);
203203
out:
204204
fl6_sock_release(flowlabel);
205205
return err;

net/ipv6/tcp_ipv6.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ static int tcp_v6_connect(struct sock *sk, struct sockaddr *uaddr,
276276
if (err)
277277
goto late_failure;
278278

279-
ip6_set_txhash(sk);
279+
sk_set_txhash(sk);
280280

281281
if (!tp->write_seq && likely(!tp->repair))
282282
tp->write_seq = secure_tcpv6_sequence_number(np->saddr.s6_addr32,
@@ -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-
ip6_set_txhash(newsk);
1093+
sk_set_txhash(newsk);
10941094

10951095
/* Now IPv6 options...
10961096

0 commit comments

Comments
 (0)