Skip to contentSkip to conversation

net/udp: Add 4-tuple hash for connected socket

RFC PATCH Superseded Philo Lu 3 participants 8 messages Sep 13, 2024 – Sep 23, 2024

Subsystems (3)

v1 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Philo Lu

author
#0 lore raw

RFC PATCH 1/1 net/udp: Add 4-tuple hash for connected socket

This RFC patch introduces 4-tuple hash for connected udp sockets, to make udp lookup faster. It is a tentative proposal and any comment is welcome.

Currently, the udp_table has two hash table, the port hash and portaddr hash. But for UDP server, all sockets have the same local port and addr, so they are all on the same hash slot within a reuseport group. And the target sock is selected by scoring.

In some applications, the UDP server uses connect() for each incoming client, and then the socket (fd) is used exclusively by the client. In such scenarios, current scoring method can be ineffcient with a large number of connections, resulting in high softirq overhead.

To solve the problem, a 4-tuple hash list is added to udp_table, and is updated when calling connect(). Then __udp4_lib_lookup() firstly searches the 4-tuple hash list, and return directly if success. A new sockopt UDP_HASH4 is added to enable it. So the usage is:
1. socket()
2. bind()
3. setsockopt(UDP_HASH4)
4. connect()

AFAICT the patch (if useful) can be further improved by:
(a) Support disable with sockopt UDP_HASH4. Now it cannot be disabled once turned on until the socket closed.
(b) Better interact with hash2/reuseport. Now hash4 hardly affects other mechanisms, but maintaining sockets in both hash4 and hash2 lists seems unnecessary.
(c) Support early demux and ipv6.

---
 include/linux/udp.h      |   8 +++
 include/net/udp.h        |  17 +++++-
 include/uapi/linux/udp.h |   1 +
 net/ipv4/udp.c           | 127 +++++++++++++++++++++++++++++++++++++--
 net/ipv6/udp.c           |   2 +-
 5 files changed, 147 insertions(+), 8 deletions(-)
include/linux/udp.h +8 -0
index 3eb3f2b9a2a0..c7b28e52fc49 100644
@@ -42,6 +42,7 @@ enum {
 	UDP_FLAGS_ENCAP_ENABLED, /* This socket enabled encap */
 	UDP_FLAGS_UDPLITE_SEND_CC, /* set via udplite setsockopt */
 	UDP_FLAGS_UDPLITE_RECV_CC, /* set via udplite setsockopt */
+	UDP_FLAGS_HASH4_ENABLED, /* Use 4-tuple hash */
 };
 
 struct udp_sock {
@@ -56,6 +57,10 @@ struct udp_sock {
 	int		 pending;	/* Any pending frames ? */
 	__u8		 encap_type;	/* Is this an Encapsulation socket? */
 
+	/* For UDP 4-tuple hash */
+	__u16 udp_lrpa_hash;
+	struct hlist_node udp_lrpa_node;
+
 	/*
 	 * Following member retains the information to create a UDP header
 	 * when the socket is uncorked.
@@ -206,6 +211,9 @@ static inline void udp_allow_gso(struct sock *sk)
 #define udp_portaddr_for_each_entry_rcu(__sk, list) \
 	hlist_for_each_entry_rcu(__sk, list, __sk_common.skc_portaddr_node)
 
+#define udp_lrpa_for_each_entry_rcu(__up, list) \
+	hlist_for_each_entry_rcu(__up, list, udp_lrpa_node)
+
 #define IS_UDPLITE(__sk) (__sk->sk_protocol == IPPROTO_UDPLITE)
 
 #endif	/* _LINUX_UDP_H */
include/net/udp.h +16 -1
index 61222545ab1c..a05d79d35fbb 100644
@@ -67,12 +67,15 @@ struct udp_hslot {
  *
  *	@hash:	hash table, sockets are hashed on (local port)
  *	@hash2:	hash table, sockets are hashed on (local port, local address)
+ *	@hash4:	hash table, sockets are hashed on
+ *		(local port, local address, remote port, remote address)
  *	@mask:	number of slots in hash tables, minus 1
  *	@log:	log2(number of slots in hash table)
  */
 struct udp_table {
 	struct udp_hslot	*hash;
 	struct udp_hslot	*hash2;
+	struct udp_hslot	*hash4;
 	unsigned int		mask;
 	unsigned int		log;
 };
@@ -94,6 +97,17 @@ static inline struct udp_hslot *udp_hashslot2(struct udp_table *table,
 	return &table->hash2[hash & table->mask];
 }
 
+static inline struct udp_hslot *udp_hashslot4(struct udp_table *table,
+					      unsigned int hash)
+{
+	return &table->hash4[hash & table->mask];
+}
+
+static inline bool udp_hashed4(const struct sock *sk)
+{
+	return !hlist_unhashed(&udp_sk(sk)->udp_lrpa_node);
+}
+
 extern struct proto udp_prot;
 
 extern atomic_long_t udp_memory_allocated;
@@ -193,7 +207,7 @@ static inline int udp_lib_hash(struct sock *sk)
 }
 
 void udp_lib_unhash(struct sock *sk);
-void udp_lib_rehash(struct sock *sk, u16 new_hash);
+void udp_lib_rehash(struct sock *sk, u16 new_hash, u16 new_hash4);
 
 static inline void udp_lib_close(struct sock *sk, long timeout)
 {
@@ -286,6 +300,7 @@ int udp_rcv(struct sk_buff *skb);
 int udp_ioctl(struct sock *sk, int cmd, int *karg);
 int udp_init_sock(struct sock *sk);
 int udp_pre_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len);
+int udp_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len);
 int __udp_disconnect(struct sock *sk, int flags);
 int udp_disconnect(struct sock *sk, int flags);
 __poll_t udp_poll(struct file *file, struct socket *sock, poll_table *wait);
include/uapi/linux/udp.h +1 -0
index 1a0fe8b151fb..5b9ecbbec144 100644
@@ -34,6 +34,7 @@ struct udphdr {
 #define UDP_NO_CHECK6_RX 102	/* Disable accpeting checksum for UDP6 */
 #define UDP_SEGMENT	103	/* Set GSO segmentation size */
 #define UDP_GRO		104	/* This socket can receive UDP GRO packets */
+#define UDP_HASH4	105	/* Enable 4-tuple hash with connect() */
 
 /* UDP encapsulation types */
 #define UDP_ENCAP_ESPINUDP_NON_IKE	1 /* unused  draft-ietf-ipsec-nat-t-ike-00/01 */
net/ipv4/udp.c +121 -6

213 lines not on this page · raw message

net/ipv6/udp.c +1 -1
index 52dfbb2ff1a8..47659381222d 100644
@@ -111,7 +111,7 @@ void udp_v6_rehash(struct sock *sk)
 					  &sk->sk_v6_rcv_saddr,
 					  inet_sk(sk)->inet_num);
 
-	udp_lib_rehash(sk, new_hash);
+	udp_lib_rehash(sk, new_hash, 0); /* 4-tuple hash not implemented */
 }
 
 static int compute_score(struct sock *sk, const struct net *net,
Signature · 1 line
2.32.0.3.g01195cf9f
#1 lore raw
Quoted 32 lines · Philo Lu

This RFC patch introduces 4-tuple hash for connected udp sockets, to make udp lookup faster. It is a tentative proposal and any comment is welcome.

Currently, the udp_table has two hash table, the port hash and portaddr hash. But for UDP server, all sockets have the same local port and addr, so they are all on the same hash slot within a reuseport group. And the target sock is selected by scoring.

In some applications, the UDP server uses connect() for each incoming client, and then the socket (fd) is used exclusively by the client. In such scenarios, current scoring method can be ineffcient with a large number of connections, resulting in high softirq overhead.

To solve the problem, a 4-tuple hash list is added to udp_table, and is updated when calling connect(). Then __udp4_lib_lookup() firstly searches the 4-tuple hash list, and return directly if success. A new sockopt UDP_HASH4 is added to enable it. So the usage is:
1. socket()
2. bind()
3. setsockopt(UDP_HASH4)
4. connect()

AFAICT the patch (if useful) can be further improved by:
(a) Support disable with sockopt UDP_HASH4. Now it cannot be disabled once turned on until the socket closed.
(b) Better interact with hash2/reuseport. Now hash4 hardly affects other mechanisms, but maintaining sockets in both hash4 and hash2 lists seems unnecessary.
(c) Support early demux and ipv6.

Quoted 1 line

Adding a 4-tuple hash for UDP has been discussed in the past.

Main issue is that this is adding one cache line miss per incoming packet.

Most heavy duty UDP servers (DNS, QUIC), use non connected sockets, because having one million UDP sockets has huge kernel memory cost, not counting poor cache locality.

#2 lore raw
Quoted 33 lines · Eric Dumazet
Quoted 32 lines · Philo Lu

This RFC patch introduces 4-tuple hash for connected udp sockets, to make udp lookup faster. It is a tentative proposal and any comment is welcome.

Currently, the udp_table has two hash table, the port hash and portaddr hash. But for UDP server, all sockets have the same local port and addr, so they are all on the same hash slot within a reuseport group. And the target sock is selected by scoring.

In some applications, the UDP server uses connect() for each incoming client, and then the socket (fd) is used exclusively by the client. In such scenarios, current scoring method can be ineffcient with a large number of connections, resulting in high softirq overhead.

To solve the problem, a 4-tuple hash list is added to udp_table, and is updated when calling connect(). Then __udp4_lib_lookup() firstly searches the 4-tuple hash list, and return directly if success. A new sockopt UDP_HASH4 is added to enable it. So the usage is:
1. socket()
2. bind()
3. setsockopt(UDP_HASH4)
4. connect()

AFAICT the patch (if useful) can be further improved by:
(a) Support disable with sockopt UDP_HASH4. Now it cannot be disabled once turned on until the socket closed.
(b) Better interact with hash2/reuseport. Now hash4 hardly affects other mechanisms, but maintaining sockets in both hash4 and hash2 lists seems unnecessary.
(c) Support early demux and ipv6.

Quoted 3 lines
Quoted 1 line

Adding a 4-tuple hash for UDP has been discussed in the past.

Thanks for the information! we don't know the history.

Quoted 2 lines

Main issue is that this is adding one cache line miss per incoming packet.

What about adding something like refcnt in 'struct udp_hslot' ? if someone enabled uhash4 on the port, we increase the refcnt. Then we can check if that port have uhash4 enabled. If it's zero, we can just bypass the uhash4 lookup process and goto the current udp4_lib_lookup2().

That should avoid the cache line miss per incoming packet ?

Best regards,
Dust

#3 lore raw
Quoted 42 lines · Dust Li
Quoted 33 lines · Eric Dumazet
Quoted 32 lines · Philo Lu

This RFC patch introduces 4-tuple hash for connected udp sockets, to make udp lookup faster. It is a tentative proposal and any comment is welcome.

Currently, the udp_table has two hash table, the port hash and portaddr hash. But for UDP server, all sockets have the same local port and addr, so they are all on the same hash slot within a reuseport group. And the target sock is selected by scoring.

In some applications, the UDP server uses connect() for each incoming client, and then the socket (fd) is used exclusively by the client. In such scenarios, current scoring method can be ineffcient with a large number of connections, resulting in high softirq overhead.

To solve the problem, a 4-tuple hash list is added to udp_table, and is updated when calling connect(). Then __udp4_lib_lookup() firstly searches the 4-tuple hash list, and return directly if success. A new sockopt UDP_HASH4 is added to enable it. So the usage is:
1. socket()
2. bind()
3. setsockopt(UDP_HASH4)
4. connect()

AFAICT the patch (if useful) can be further improved by:
(a) Support disable with sockopt UDP_HASH4. Now it cannot be disabled once turned on until the socket closed.
(b) Better interact with hash2/reuseport. Now hash4 hardly affects other mechanisms, but maintaining sockets in both hash4 and hash2 lists seems unnecessary.
(c) Support early demux and ipv6.

Quoted 3 lines
Quoted 1 line

Adding a 4-tuple hash for UDP has been discussed in the past.

Thanks for the information! we don't know the history.

Quoted 1 line
Quoted 8 lines
Quoted 1 line

Main issue is that this is adding one cache line miss per incoming packet.

What about adding something like refcnt in 'struct udp_hslot' ? if someone enabled uhash4 on the port, we increase the refcnt. Then we can check if that port have uhash4 enabled. If it's zero, we can just bypass the uhash4 lookup process and goto the current udp4_lib_lookup2().

Reading anything (thus a refcnt) in 'struct udp_hslot' will need the same cache line miss.

Note that udp_hslot already has a 'count' field

#4 lore raw
Quoted 52 lines · Eric Dumazet
Quoted 42 lines · Dust Li
Quoted 33 lines · Eric Dumazet
Quoted 32 lines · Philo Lu

This RFC patch introduces 4-tuple hash for connected udp sockets, to make udp lookup faster. It is a tentative proposal and any comment is welcome.

Currently, the udp_table has two hash table, the port hash and portaddr hash. But for UDP server, all sockets have the same local port and addr, so they are all on the same hash slot within a reuseport group. And the target sock is selected by scoring.

In some applications, the UDP server uses connect() for each incoming client, and then the socket (fd) is used exclusively by the client. In such scenarios, current scoring method can be ineffcient with a large number of connections, resulting in high softirq overhead.

To solve the problem, a 4-tuple hash list is added to udp_table, and is updated when calling connect(). Then __udp4_lib_lookup() firstly searches the 4-tuple hash list, and return directly if success. A new sockopt UDP_HASH4 is added to enable it. So the usage is:
1. socket()
2. bind()
3. setsockopt(UDP_HASH4)
4. connect()

AFAICT the patch (if useful) can be further improved by:
(a) Support disable with sockopt UDP_HASH4. Now it cannot be disabled once turned on until the socket closed.
(b) Better interact with hash2/reuseport. Now hash4 hardly affects other mechanisms, but maintaining sockets in both hash4 and hash2 lists seems unnecessary.
(c) Support early demux and ipv6.

Quoted 3 lines
Quoted 1 line

Adding a 4-tuple hash for UDP has been discussed in the past.

Thanks for the information! we don't know the history.

Quoted 1 line
Quoted 8 lines
Quoted 1 line

Main issue is that this is adding one cache line miss per incoming packet.

What about adding something like refcnt in 'struct udp_hslot' ? if someone enabled uhash4 on the port, we increase the refcnt. Then we can check if that port have uhash4 enabled. If it's zero, we can just bypass the uhash4 lookup process and goto the current udp4_lib_lookup2().

Quoted 2 lines

Reading anything (thus a refcnt) in 'struct udp_hslot' will need the same cache line miss.

hslot2->head in 'struct udp_hslot' will be read right away in udp4_lib_lookup2() in any case, it's just a few instructions later(about 20). So I think cache miss should not be a problem in this case.

Quoted 2 lines

Note that udp_hslot already has a 'count' field

Yes, but that's for uhash/uhash2. I'm thinking of adding something to indicate that uhash4 was enabled on this port. So we can avoid the extra memory footprint on some cold memory. Maybe 'struct udp_hslot' is not a good place.

Best regards,
Dust

#5 lore raw
Quoted 54 lines · Dust Li
Quoted 52 lines · Eric Dumazet
Quoted 50 lines · Dust Li
Quoted 33 lines · Eric Dumazet
Quoted 32 lines · Philo Lu

This RFC patch introduces 4-tuple hash for connected udp sockets, to make udp lookup faster. It is a tentative proposal and any comment is welcome.

Currently, the udp_table has two hash table, the port hash and portaddr hash. But for UDP server, all sockets have the same local port and addr, so they are all on the same hash slot within a reuseport group. And the target sock is selected by scoring.

In some applications, the UDP server uses connect() for each incoming client, and then the socket (fd) is used exclusively by the client. In such scenarios, current scoring method can be ineffcient with a large number of connections, resulting in high softirq overhead.

To solve the problem, a 4-tuple hash list is added to udp_table, and is updated when calling connect(). Then __udp4_lib_lookup() firstly searches the 4-tuple hash list, and return directly if success. A new sockopt UDP_HASH4 is added to enable it. So the usage is:
1. socket()
2. bind()
3. setsockopt(UDP_HASH4)
4. connect()

AFAICT the patch (if useful) can be further improved by:
(a) Support disable with sockopt UDP_HASH4. Now it cannot be disabled once turned on until the socket closed.
(b) Better interact with hash2/reuseport. Now hash4 hardly affects other mechanisms, but maintaining sockets in both hash4 and hash2 lists seems unnecessary.
(c) Support early demux and ipv6.

Quoted 3 lines
Quoted 1 line

Adding a 4-tuple hash for UDP has been discussed in the past.

Thanks for the information! we don't know the history.

Quoted 2 lines

Main issue is that this is adding one cache line miss per incoming packet.

What about adding something like refcnt in 'struct udp_hslot' ? if someone enabled uhash4 on the port, we increase the refcnt. Then we can check if that port have uhash4 enabled. If it's zero, we can just bypass the uhash4 lookup process and goto the current udp4_lib_lookup2().

Quoted 7 lines
Quoted 2 lines

Reading anything (thus a refcnt) in 'struct udp_hslot' will need the same cache line miss.

hslot2->head in 'struct udp_hslot' will be read right away in udp4_lib_lookup2() in any case, it's just a few instructions later(about 20). So I think cache miss should not be a problem in this case.

I guess this could work.

author
#6 lore raw
In reply to Eric Dumazet

Hi Eric, sorry for the late response.

Quoted 33 lines · Eric Dumazet
Quoted 32 lines · Philo Lu

This RFC patch introduces 4-tuple hash for connected udp sockets, to make udp lookup faster. It is a tentative proposal and any comment is welcome.

Currently, the udp_table has two hash table, the port hash and portaddr hash. But for UDP server, all sockets have the same local port and addr, so they are all on the same hash slot within a reuseport group. And the target sock is selected by scoring.

In some applications, the UDP server uses connect() for each incoming client, and then the socket (fd) is used exclusively by the client. In such scenarios, current scoring method can be ineffcient with a large number of connections, resulting in high softirq overhead.

To solve the problem, a 4-tuple hash list is added to udp_table, and is updated when calling connect(). Then __udp4_lib_lookup() firstly searches the 4-tuple hash list, and return directly if success. A new sockopt UDP_HASH4 is added to enable it. So the usage is:
1. socket()
2. bind()
3. setsockopt(UDP_HASH4)
4. connect()

AFAICT the patch (if useful) can be further improved by:
(a) Support disable with sockopt UDP_HASH4. Now it cannot be disabled once turned on until the socket closed.
(b) Better interact with hash2/reuseport. Now hash4 hardly affects other mechanisms, but maintaining sockets in both hash4 and hash2 lists seems unnecessary.
(c) Support early demux and ipv6.

Quoted 6 lines
Quoted 1 line

Adding a 4-tuple hash for UDP has been discussed in the past.

Main issue is that this is adding one cache line miss per incoming packet.

Thanks to Dust's idea, we can create a new field for hslot2 (or create a new struct for hslot2), indicating whether there are connected sockets in this hslot (i.e., local port and local address), and run hash4 lookup only when it's true. Then there would be no cache line miss.

The detailed patch is attached below.

Quoted 3 lines

Most heavy duty UDP servers (DNS, QUIC), use non connected sockets, because having one million UDP sockets has huge kernel memory cost, not counting poor cache locality.

Some of our applications do use connected UDP sockets (~10,000 conns), and will get significant benefits from hash4. We use connect() to separate receiving sockets and listening ones, and then it's easier to manage them (just like TCP), especially during live-upgrading, such as nginx reload. Besides, I believe hash4 is harmless to those servers without connected sockets.

Suggestions are always welcome, and I'll keep improving this patch.

Thanks.

---
 include/net/udp.h |  3 +++
 net/ipv4/udp.c    | 17 ++++++++++++-----
 2 files changed, 15 insertions(+), 5 deletions(-)
include/net/udp.h +3 -0
index a05d79d35fbba..bec04c0e753d0 100644
@@ -54,11 +54,14 @@ struct udp_skb_cb {
  *
  *	@head:	head of list of sockets
  *	@count:	number of sockets in 'head' list
+ *	@hash4_cnt: number of sockets in 'hash4' table of the same (local port, local address),
+ *		    Only used by hash2.
  *	@lock:	spinlock protecting changes to head/count
  */
 struct udp_hslot {
 	struct hlist_head	head;
 	int			count;
+	u32			hash4_cnt;
 	spinlock_t		lock;
 } __attribute__((aligned(2 * sizeof(long))));
 
net/ipv4/udp.c +12 -5
index aac0251ff6fac..dfa8b3c091def 100644
@@ -511,14 +511,16 @@ struct sock *__udp4_lib_lookup(const struct net *net, __be32 saddr,
 	struct udp_hslot *hslot2;
 	struct sock *result, *sk;
 
-	result = udp4_lib_lookup4(net, saddr, sport, daddr, hnum, dif, sdif, udptable);
-	if (result)
-		return result;
-
 	hash2 = ipv4_portaddr_hash(net, daddr, hnum);
 	slot2 = hash2 & udptable->mask;
 	hslot2 = &udptable->hash2[slot2];
 ·
+	if (hslot2->hash4_cnt) {
+		result = udp4_lib_lookup4(net, saddr, sport, daddr, hnum, dif, sdif, udptable);
+		if (result)
+			return result;
+	}
+
 	/* Lookup connected or non-wildcard socket */
 	result = udp4_lib_lookup2(net, saddr, sport,
 				  daddr, hnum, dif, sdif,
@@ -1961,7 +1963,7 @@ EXPORT_SYMBOL(udp_pre_connect);
 /* call with sock lock */
 static void udp4_hash4(struct sock *sk)
 {
-	struct udp_hslot *hslot, *hslot4;
+	struct udp_hslot *hslot, *hslot2, *hslot4;
 	struct net *net = sock_net(sk);
 	struct udp_table *udptable;
 	unsigned int hash;
@@ -1975,6 +1977,7 @@ static void udp4_hash4(struct sock *sk)
 
 	udptable = net->ipv4.udp_table;
 	hslot = udp_hashslot(udptable, net, udp_sk(sk)->udp_port_hash);
+	hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash);
 	hslot4 = udp_hashslot4(udptable, hash);
 	udp_sk(sk)->udp_lrpa_hash = hash;
 
@@ -1985,6 +1988,7 @@ static void udp4_hash4(struct sock *sk)
 	spin_lock(&hslot4->lock);
 	hlist_add_head_rcu(&udp_sk(sk)->udp_lrpa_node, &hslot4->head);
 	hslot4->count++;
+	hslot2->hash4_cnt++;
 	spin_unlock(&hslot4->lock);
 
 	spin_unlock_bh(&hslot->lock);
@@ -2068,6 +2072,7 @@ void udp_lib_unhash(struct sock *sk)
 				spin_lock(&hslot4->lock);
 				hlist_del_init_rcu(&udp_sk(sk)->udp_lrpa_node);
 				hslot4->count--;
+				hslot2->hash4_cnt--;
 				spin_unlock(&hslot4->lock);
 			}
 		}
@@ -2119,11 +2124,13 @@ void udp_lib_rehash(struct sock *sk, u16 newhash, u16 newhash4)
 				spin_lock(&hslot4->lock);
 				hlist_del_init_rcu(&udp_sk(sk)->udp_lrpa_node);
 				hslot4->count--;
+				hslot2->hash4_cnt--;
 				spin_unlock(&hslot4->lock);
 
 				spin_lock(&nhslot4->lock);
 				hlist_add_head_rcu(&udp_sk(sk)->udp_lrpa_node, &nhslot4->head);
 				nhslot4->count++;
+				nhslot2->hash4_cnt++;
 				spin_unlock(&nhslot4->lock);
 			}
 
Signature · 1 line
Philo
#7 lore raw
Quoted 80 lines · Philo Lu

Hi Eric, sorry for the late response.

Quoted 33 lines · Eric Dumazet
Quoted 32 lines · Philo Lu

This RFC patch introduces 4-tuple hash for connected udp sockets, to make udp lookup faster. It is a tentative proposal and any comment is welcome.

Currently, the udp_table has two hash table, the port hash and portaddr hash. But for UDP server, all sockets have the same local port and addr, so they are all on the same hash slot within a reuseport group. And the target sock is selected by scoring.

In some applications, the UDP server uses connect() for each incoming client, and then the socket (fd) is used exclusively by the client. In such scenarios, current scoring method can be ineffcient with a large number of connections, resulting in high softirq overhead.

To solve the problem, a 4-tuple hash list is added to udp_table, and is updated when calling connect(). Then __udp4_lib_lookup() firstly searches the 4-tuple hash list, and return directly if success. A new sockopt UDP_HASH4 is added to enable it. So the usage is:
1. socket()
2. bind()
3. setsockopt(UDP_HASH4)
4. connect()

AFAICT the patch (if useful) can be further improved by:
(a) Support disable with sockopt UDP_HASH4. Now it cannot be disabled once turned on until the socket closed.
(b) Better interact with hash2/reuseport. Now hash4 hardly affects other mechanisms, but maintaining sockets in both hash4 and hash2 lists seems unnecessary.
(c) Support early demux and ipv6.

Quoted 6 lines
Quoted 1 line

Adding a 4-tuple hash for UDP has been discussed in the past.

Main issue is that this is adding one cache line miss per incoming packet.

Thanks to Dust's idea, we can create a new field for hslot2 (or create a new struct for hslot2), indicating whether there are connected sockets in this hslot (i.e., local port and local address), and run hash4 lookup only when it's true. Then there would be no cache line miss.

The detailed patch is attached below.

Quoted 3 lines

Most heavy duty UDP servers (DNS, QUIC), use non connected sockets, because having one million UDP sockets has huge kernel memory cost, not counting poor cache locality.

Some of our applications do use connected UDP sockets (~10,000 conns), and will get significant benefits from hash4. We use connect() to separate receiving sockets and listening ones, and then it's easier to manage them (just like TCP), especially during live-upgrading, such as nginx reload. Besides, I believe hash4 is harmless to those servers without connected sockets.

Suggestions are always welcome, and I'll keep improving this patch.

Thanks.

---
  include/net/udp.h |  3 +++
  net/ipv4/udp.c    | 17 ++++++++++++-----
  2 files changed, 15 insertions(+), 5 deletions(-)
include/net/udp.h +1 -0
index a05d79d35fbba..bec04c0e753d0 100644
@@ -54,11 +54,14 @@ struct udp_skb_cb {
   *
   *    @head:  head of list of sockets
   *    @count: number of sockets in 'head' list
+ *     @hash4_cnt: number of sockets in 'hash4' table of the same (local

include/net/udp.h struct udp_skb_cb { · line 67

Quoted 10 lines
include/net/udp.h +2 -0
 port, local address),
+ *                 Only used by hash2.
   *    @lock:  spinlock protecting changes to head/count
   */
  struct udp_hslot {
        struct hlist_head       head;
        int                     count;
+       u32                     hash4_cnt;
        spinlock_t              lock;
  } __attribute__((aligned(2 * sizeof(long))));

This would double the size of this structure (from 16 to 32 bytes)

Perhaps add another 'struct udp_hslot_main' for the relevant hash table, and keep the smaller 'struct udp_hslot' for the two others.

Current cumulative size of the hash tables is 2 MB.

Alternatively we could move the locks out of the structure, this is not used in the fast path.