Implement ipv6 udp hash4 like that in ipv4. The major difference is that
the hash value should be calculated with udp6_ehashfn(). Besides,
ipv4-mapped ipv6 address is handled before hash() and rehash().
Core procedures of hash/unhash/rehash are same as ipv4, and udpv4 and
udpv6 share the same udptable, so some functions in ipv4 hash4 can also
be shared.
Signed-off-by: Philo Lu <lulie@linux.alibaba.com>
Signed-off-by: Cambda Zhu <cambda@linux.alibaba.com>
Signed-off-by: Fred Chen <fred.cc@alibaba-inc.com>
Signed-off-by: Yubing Qiu <yubing.qiuyubing@alibaba-inc.com>
---
net/ipv6/udp.c | 96 ++++++++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 94 insertions(+), 2 deletions(-)
diff --git a/net/ipv6/udp.c b/net/ipv6/udp.c
index 1ea99d704e31..64f13f258fca 100644
--- a/net/ipv6/udp.c
+++ b/net/ipv6/udp.c
@@ -110,8 +110,17 @@ void udp_v6_rehash(struct sock *sk)
u16 new_hash = ipv6_portaddr_hash(sock_net(sk),
&sk->sk_v6_rcv_saddr,
inet_sk(sk)->inet_num);
+ u16 new_hash4;
- udp_lib_rehash(sk, new_hash, 0); /* 4-tuple hash not implemented */
+ if (ipv6_addr_v4mapped(&sk->sk_v6_rcv_saddr)) {
+ new_hash4 = udp_ehashfn(sock_net(sk), sk->sk_rcv_saddr, sk->sk_num,
+ sk->sk_daddr, sk->sk_dport);
+ } else {
+ new_hash4 = udp6_ehashfn(sock_net(sk), &sk->sk_v6_rcv_saddr, sk->sk_num,
+ &sk->sk_v6_daddr, sk->sk_dport);
+ }
+
+ udp_lib_rehash(sk, new_hash, new_hash4);
}
static int compute_score(struct sock *sk, const struct net *net,
@@ -216,6 +225,71 @@ static struct sock *udp6_lib_lookup2(const struct net *net,
return result;
}
+#if IS_ENABLED(CONFIG_BASE_SMALL)
+static struct sock *udp6_lib_lookup4(const struct net *net,
+ const struct in6_addr *saddr, __be16 sport,
+ const struct in6_addr *daddr, unsigned int hnum,
+ int dif, int sdif, struct udp_table *udptable)
+{
+ return NULL;
+}
+
+static void udp6_hash4(struct sock *sk)
+{
+}
+#else /* !CONFIG_BASE_SMALL */
+static struct sock *udp6_lib_lookup4(const struct net *net,
+ const struct in6_addr *saddr, __be16 sport,
+ const struct in6_addr *daddr, unsigned int hnum,
+ int dif, int sdif, struct udp_table *udptable)
+{
+ const __portpair ports = INET_COMBINED_PORTS(sport, hnum);
+ const struct hlist_nulls_node *node;
+ struct udp_hslot *hslot4;
+ unsigned int hash4, slot;
+ struct udp_sock *up;
+ struct sock *sk;
+
+ hash4 = udp6_ehashfn(net, daddr, hnum, saddr, sport);
+ slot = hash4 & udptable->mask;
+ hslot4 = &udptable->hash4[slot];
+
+begin:
+ udp_lrpa_for_each_entry_rcu(up, node, &hslot4->nulls_head) {
+ sk = (struct sock *)up;
+ if (inet6_match(net, sk, saddr, daddr, ports, dif, sdif))
+ return sk;
+ }
+
+ /* if the nulls value we got at the end of this lookup is not the expected one, we must
+ * restart lookup. We probably met an item that was moved to another chain due to rehash.
+ */
+ if (get_nulls_value(node) != slot)
+ goto begin;
+
+ return NULL;
+}
+
+static void udp6_hash4(struct sock *sk)
+{
+ struct net *net = sock_net(sk);
+ unsigned int hash;
+
+ if (ipv6_addr_v4mapped(&sk->sk_v6_rcv_saddr)) {
+ udp4_hash4(sk);
+ return;
+ }
+
+ if (sk_unhashed(sk) || ipv6_addr_any(&sk->sk_v6_rcv_saddr))
+ return;
+
+ hash = udp6_ehashfn(net, &sk->sk_v6_rcv_saddr, sk->sk_num,
+ &sk->sk_v6_daddr, sk->sk_dport);
+
+ udp_lib_hash4(sk, hash);
+}
+#endif /* CONFIG_BASE_SMALL */
+
/* rcu_read_lock() must be held */
struct sock *__udp6_lib_lookup(const struct net *net,
const struct in6_addr *saddr, __be16 sport,
@@ -231,6 +305,12 @@ struct sock *__udp6_lib_lookup(const struct net *net,
hash2 = ipv6_portaddr_hash(net, daddr, hnum);
hslot2 = udp_hashslot2(udptable, hash2);
+ if (udp_has_hash4(hslot2)) {
+ result = udp6_lib_lookup4(net, saddr, sport, daddr, hnum, dif, sdif, udptable);
+ if (result) /* udp6_lib_lookup4 return sk or NULL */
+ return result;
+ }
+
/* Lookup connected or non-wildcard sockets */
result = udp6_lib_lookup2(net, saddr, sport,
daddr, hnum, dif, sdif,
@@ -1166,6 +1246,18 @@ static int udpv6_pre_connect(struct sock *sk, struct sockaddr *uaddr,
return BPF_CGROUP_RUN_PROG_INET6_CONNECT_LOCK(sk, uaddr, &addr_len);
}
+static int udpv6_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
+{
+ int res;
+
+ lock_sock(sk);
+ res = __ip6_datagram_connect(sk, uaddr, addr_len);
+ if (!res)
+ udp6_hash4(sk);
+ release_sock(sk);
+ return res;
+}
+
/**
* udp6_hwcsum_outgoing - handle outgoing HW checksumming
* @sk: socket we are sending on
@@ -1761,7 +1853,7 @@ struct proto udpv6_prot = {
.owner = THIS_MODULE,
.close = udp_lib_close,
.pre_connect = udpv6_pre_connect,
- .connect = ip6_datagram_connect,
+ .connect = udpv6_connect,
.disconnect = udp_disconnect,
.ioctl = udp_ioctl,
.init = udpv6_init_sock,
--
2.32.0.3.g01195cf9f
Hi Philo, kernel test robot noticed the following build errors: [auto build test ERROR on net-next/main] url: https://github.com/intel-lab-lkp/linux/commits/Philo-Lu/net-udp-Add-a-new-struct-for-hash2-slot/20241031-204729 base: net-next/main patch link: https://lore.kernel.org/r/20241031124550.20227-5-lulie%40linux.alibaba.com patch subject: [PATCH v6 net-next 4/4] ipv6/udp: Add 4-tuple hash for connected socket config: i386-randconfig-141-20241101 (https://download.01.org/0day-ci/archive/20241102/202411020025.8DrAkT2l-lkp@intel.com/config) compiler: gcc-12 (Debian 12.2.0-14) 12.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241102/202411020025.8DrAkT2l-lkp@intel.com/reproduce) If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags | Reported-by: kernel test robot <lkp@intel.com> | Closes: https://lore.kernel.org/oe-kbuild-all/202411020025.8DrAkT2l-lkp@intel.com/ All errors (new ones prefixed by >>): net/ipv6/udp.c: In function 'udp_v6_rehash': >> net/ipv6/udp.c:116:29: error: implicit declaration of function 'udp_ehashfn'; did you mean 'udp6_ehashfn'? [-Werror=implicit-function-declaration] 116 | new_hash4 = udp_ehashfn(sock_net(sk), sk->sk_rcv_saddr, sk->sk_num, | ^~~~~~~~~~~ | udp6_ehashfn cc1: some warnings being treated as errors vim +116 net/ipv6/udp.c 107 108 void udp_v6_rehash(struct sock *sk) 109 { 110 u16 new_hash = ipv6_portaddr_hash(sock_net(sk), 111 &sk->sk_v6_rcv_saddr, 112 inet_sk(sk)->inet_num); 113 u16 new_hash4; 114 115 if (ipv6_addr_v4mapped(&sk->sk_v6_rcv_saddr)) { > 116 new_hash4 = udp_ehashfn(sock_net(sk), sk->sk_rcv_saddr, sk->sk_num, 117 sk->sk_daddr, sk->sk_dport); 118 } else { 119 new_hash4 = udp6_ehashfn(sock_net(sk), &sk->sk_v6_rcv_saddr, sk->sk_num, 120 &sk->sk_v6_daddr, sk->sk_dport); 121 } 122 123 udp_lib_rehash(sk, new_hash, new_hash4); 124 } 125 -- 0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
On 2024/10/31 20:45, Philo Lu wrote: > Implement ipv6 udp hash4 like that in ipv4. The major difference is that > the hash value should be calculated with udp6_ehashfn(). Besides, > ipv4-mapped ipv6 address is handled before hash() and rehash(). > > Core procedures of hash/unhash/rehash are same as ipv4, and udpv4 and > udpv6 share the same udptable, so some functions in ipv4 hash4 can also > be shared. > > Signed-off-by: Philo Lu <lulie@linux.alibaba.com> > Signed-off-by: Cambda Zhu <cambda@linux.alibaba.com> > Signed-off-by: Fred Chen <fred.cc@alibaba-inc.com> > Signed-off-by: Yubing Qiu <yubing.qiuyubing@alibaba-inc.com> > --- > net/ipv6/udp.c | 96 ++++++++++++++++++++++++++++++++++++++++++++++++-- > 1 file changed, 94 insertions(+), 2 deletions(-) > > diff --git a/net/ipv6/udp.c b/net/ipv6/udp.c > index 1ea99d704e31..64f13f258fca 100644 > --- a/net/ipv6/udp.c > +++ b/net/ipv6/udp.c > @@ -110,8 +110,17 @@ void udp_v6_rehash(struct sock *sk) > u16 new_hash = ipv6_portaddr_hash(sock_net(sk), > &sk->sk_v6_rcv_saddr, > inet_sk(sk)->inet_num); > + u16 new_hash4; > > - udp_lib_rehash(sk, new_hash, 0); /* 4-tuple hash not implemented */ > + if (ipv6_addr_v4mapped(&sk->sk_v6_rcv_saddr)) { > + new_hash4 = udp_ehashfn(sock_net(sk), sk->sk_rcv_saddr, sk->sk_num, > + sk->sk_daddr, sk->sk_dport); Just found udp_ehashfn() used here results in an build error of undefined function. I think the `ipv6_addr_v4mapped(&sk->sk_v6_rcv_saddr)` branch can be moved into udp_lib_rehash() to fix the issue. So in the worst case, we need to calculate the newhash4 twice in ipv6 hash4 rehash, one in udp_v6_rehash() and the other in udp_lib_rehash(). > + } else { > + new_hash4 = udp6_ehashfn(sock_net(sk), &sk->sk_v6_rcv_saddr, sk->sk_num, > + &sk->sk_v6_daddr, sk->sk_dport); > + } > + > + udp_lib_rehash(sk, new_hash, new_hash4); > } > -- Philo
From: Philo Lu <lulie@linux.alibaba.com> Date: Fri, 1 Nov 2024 19:40:19 +0800 > On 2024/10/31 20:45, Philo Lu wrote: > > Implement ipv6 udp hash4 like that in ipv4. The major difference is that > > the hash value should be calculated with udp6_ehashfn(). Besides, > > ipv4-mapped ipv6 address is handled before hash() and rehash(). > > > > Core procedures of hash/unhash/rehash are same as ipv4, and udpv4 and > > udpv6 share the same udptable, so some functions in ipv4 hash4 can also > > be shared. > > > > Signed-off-by: Philo Lu <lulie@linux.alibaba.com> > > Signed-off-by: Cambda Zhu <cambda@linux.alibaba.com> > > Signed-off-by: Fred Chen <fred.cc@alibaba-inc.com> > > Signed-off-by: Yubing Qiu <yubing.qiuyubing@alibaba-inc.com> > > --- > > net/ipv6/udp.c | 96 ++++++++++++++++++++++++++++++++++++++++++++++++-- > > 1 file changed, 94 insertions(+), 2 deletions(-) > > > > diff --git a/net/ipv6/udp.c b/net/ipv6/udp.c > > index 1ea99d704e31..64f13f258fca 100644 > > --- a/net/ipv6/udp.c > > +++ b/net/ipv6/udp.c > > @@ -110,8 +110,17 @@ void udp_v6_rehash(struct sock *sk) > > u16 new_hash = ipv6_portaddr_hash(sock_net(sk), > > &sk->sk_v6_rcv_saddr, > > inet_sk(sk)->inet_num); > > + u16 new_hash4; > > > > - udp_lib_rehash(sk, new_hash, 0); /* 4-tuple hash not implemented */ > > + if (ipv6_addr_v4mapped(&sk->sk_v6_rcv_saddr)) { > > + new_hash4 = udp_ehashfn(sock_net(sk), sk->sk_rcv_saddr, sk->sk_num, > > + sk->sk_daddr, sk->sk_dport); > > Just found udp_ehashfn() used here results in an build error of > undefined function. > > I think the `ipv6_addr_v4mapped(&sk->sk_v6_rcv_saddr)` branch can be > moved into udp_lib_rehash() to fix the issue. So in the worst case, we > need to calculate the newhash4 twice in ipv6 hash4 rehash, one in > udp_v6_rehash() and the other in udp_lib_rehash(). You can simply export udp_ehashfn().
© 2016 - 2024 Red Hat, Inc.