Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Tom Herbert and committed by
David S. Miller
877d1f62 7a86d96e

+14 -41
-16
include/net/ip.h
··· 370 370 flow->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; 371 371 } 372 372 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 - 389 373 static inline __wsum inet_gro_compute_pseudo(struct sk_buff *skb, int proto) 390 374 { 391 375 const struct iphdr *iph = skb_gro_network_header(skb);
-19
include/net/ipv6.h
··· 707 707 } 708 708 709 709 #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 - 729 710 static inline __be32 ip6_make_flowlabel(struct net *net, struct sk_buff *skb, 730 711 __be32 flowlabel, bool autolabel) 731 712 {
+8
include/net/sock.h
··· 1687 1687 kuid_t sock_i_uid(struct sock *sk); 1688 1688 unsigned long sock_i_ino(struct sock *sk); 1689 1689 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 + 1690 1698 static inline struct dst_entry * 1691 1699 __sk_dst_get(struct sock *sk) 1692 1700 {
+1 -1
net/ipv4/datagram.c
··· 74 74 inet->inet_daddr = fl4->daddr; 75 75 inet->inet_dport = usin->sin_port; 76 76 sk->sk_state = TCP_ESTABLISHED; 77 - inet_set_txhash(sk); 77 + sk_set_txhash(sk); 78 78 inet->inet_id = jiffies; 79 79 80 80 sk_dst_set(sk, &rt->dst);
+2 -2
net/ipv4/tcp_ipv4.c
··· 222 222 if (err) 223 223 goto failure; 224 224 225 - inet_set_txhash(sk); 225 + sk_set_txhash(sk); 226 226 227 227 rt = ip_route_newports(fl4, rt, orig_sport, orig_dport, 228 228 inet->inet_sport, inet->inet_dport, sk); ··· 1277 1277 newinet->mc_ttl = ip_hdr(skb)->ttl; 1278 1278 newinet->rcv_tos = ip_hdr(skb)->tos; 1279 1279 inet_csk(newsk)->icsk_ext_hdr_len = 0; 1280 - inet_set_txhash(newsk); 1280 + sk_set_txhash(newsk); 1281 1281 if (inet_opt) 1282 1282 inet_csk(newsk)->icsk_ext_hdr_len = inet_opt->opt.optlen; 1283 1283 newinet->inet_id = newtp->write_seq ^ jiffies;
+1 -1
net/ipv6/datagram.c
··· 199 199 NULL); 200 200 201 201 sk->sk_state = TCP_ESTABLISHED; 202 - ip6_set_txhash(sk); 202 + sk_set_txhash(sk); 203 203 out: 204 204 fl6_sock_release(flowlabel); 205 205 return err;
+2 -2
net/ipv6/tcp_ipv6.c
··· 276 276 if (err) 277 277 goto late_failure; 278 278 279 - ip6_set_txhash(sk); 279 + sk_set_txhash(sk); 280 280 281 281 if (!tp->write_seq && likely(!tp->repair)) 282 282 tp->write_seq = secure_tcpv6_sequence_number(np->saddr.s6_addr32, ··· 1090 1090 newsk->sk_v6_rcv_saddr = ireq->ir_v6_loc_addr; 1091 1091 newsk->sk_bound_dev_if = ireq->ir_iif; 1092 1092 1093 - ip6_set_txhash(newsk); 1093 + sk_set_txhash(newsk); 1094 1094 1095 1095 /* Now IPv6 options... 1096 1096