Netdev List
 help / color / mirror / Atom feed
From: Julian Anastasov <ja@ssi.bg>
To: "Zhenrong (Daniel) Gu" <zrgu@stu.xjtu.edu.cn>
Cc: lvs-devel@vger.kernel.org, netdev@vger.kernel.org,
	Xing Feng <xingfeng@stu.xjtu.edu.cn>,
	Peng Zhang <p-zhang@xjtu.edu.cn>
Subject: Re: [BUG] ipvs: new connection on a reused port goes to the previous real server during conn_tab resize
Date: Wed, 25 Nov 2026 07:04:57 +0200 (EET)	[thread overview]
Message-ID: <4da25158-cbab-1186-b948-e17b13262bc3@ssi.bg> (raw)
In-Reply-To: <awWHfO0EgdLoOxLi@fxxowodeMacBook-Pro.local>


	Hello,

On Tue, 24 Nov 2026, Zhenrong (Daniel) Gu wrote:

> Hello Julian,
> 
> On Fri, Nov 20, 2026 at 06:55:07AM +0200, Julian Anastasov wrote:
> > 	Appending a RFC version for testing. It is for the net
> > tree because it contains all recent fixes for IPVS.
> > 
> > [PATCH RFC net] ipvs: fix connection reuse with the resizable hash tables
> 
> 
> Thanks for the patch. We tested it on the net tree (af6737b90,
> 7.4.0-rc3) in a KVM guest with 8 vCPUs, using the reproducer from the
> report:
> 
>   mode      kernel    reused-port   misrouted     answered     failed
>                       connections   connections   > 100 ms
>   force     net         13289          413          333        35 reset
>   force     net+RFC     15634            0            0             0
>   grow      net         15584            7            5         1 reset
>   grow      net+RFC     15678            0            0             0
>   noresize  net          7841            0            0             0
>   noresize  net+RFC      7834            0            0             0
> 
> (force: 60 s, conn_lfactor written every 100 ms; grow: 60 s, all
> defaults; noresize: 30 s, conn_lfactor=8.)
> 
> With the patch no packet of a reused-port connection went to another
> real server than its SYN, and the client saw no delayed or failed
> exchanges.
> 
> To check for regressions we also ran, on both kernels:
> 
> - tools/testing/selftests/net/netfilter/ipvs.sh: PASS on both.
> - Rolling updates with drained and deleted real servers, and reused
>   ports with conn_reuse_mode=0, in a NAT setup with kube-proxy's
>   sysctls: same results on both kernels.
> - Master/backup sync with reused-port traffic and forced resizes: the
>   backup's entries for the reused ports matched the master's on 20 of
>   20 ports with the patch, 22 of 30 without.
> - Persistent service (-p 60) with reused ports and forced resizes:
>   every connection reached the same real server, on both kernels.
> 
> We did not construct the case of a late packet from the old real
> server (e.g. a retransmitted FIN) after its connection was marked
> reused.
> 
> Tested-by: Zhenrong (Daniel) Gu <zrgu@stu.xjtu.edu.cn>

	Thank you for the testing! Due to the Sashiko concerns [1]
I'll post new version later today:

[1]
https://sashiko.dev/#/patchset/20261124160226.103532-1-ja%40ssi.bg

	Here is the v2 that I'll post, in case you want to do
the main test again. The difference is that we do not mark
the connection as reused early because a retransmitted SYN
should hit the old connection while it is using confirmed
conntrack and is not expired yet (2nd SYN comes before the
expiration):

[PATCH RFC nf v2] ipvs: fix connection reuse with the resizable hash tables

Zhenrong (Daniel) Gu, Xing Feng and Peng Zhang report for
a problem with the reused connections while testing a passive
runtime checker for L4 load balancers.

When connection is reused, we create new connection and put it
first in the list. By this way all packets hit the new connection
leaving the reused one to expire. This order worked before the
the introduction of resizable connection hash table which
started to reverse the order of connections each time the
table is resized. If the table is resized while the reused
connection is still present, the next packets will not hit the
new connection.

To fix that we prefer not to rely on the order of connections,
now we will just mark the reused connection with a new flag
IP_VS_CONN_F_REUSED, so that it can be skipped when the list
is traversed on received packet. As the reused connection is
still hashed while waiting its timer to expire, it is visible
to the procedures that delete connections on memory pressure
or netns cleanup. Also, when the reused connections are now
marked, we can exclude them from the synchronization process.

Reported-by: "Zhenrong (Daniel) Gu" <zrgu@stu.xjtu.edu.cn>
Reported-by: Xing Feng <xingfeng@stu.xjtu.edu.cn>
Reported-by: Peng Zhang <p-zhang@xjtu.edu.cn>
Link: https://lore.kernel.org/all/avx0_13w3xGNdZP8@fxxowodeMacBook-Pro.local/
Link: https://sashiko.dev/#/patchset/20261124160226.103532-1-ja%40ssi.bg
Fixes: 2fa7cc9c7025 ("ipvs: switch to per-net connection table")
Tested-by: "Zhenrong (Daniel) Gu" <zrgu@stu.xjtu.edu.cn>
Signed-off-by: Julian Anastasov <ja@ssi.bg>
---
 Changes in v2:
 * Mark the connection as reused only when we create a replacement
   connection, not while dropping packets when old_ct is present.
 * Check for IP_VS_CONN_F_MASTER_NOSYNC for the controlling connections

 Link to v1:
 https://lore.kernel.org/all/20261124160226.103532-1-ja@ssi.bg/

 include/net/ip_vs.h             |  1 +
 include/uapi/linux/ip_vs.h      |  7 +++++++
 net/netfilter/ipvs/ip_vs_conn.c | 15 +++++++++++++++
 net/netfilter/ipvs/ip_vs_core.c | 15 ++++-----------
 net/netfilter/ipvs/ip_vs_sync.c | 12 ++++++------
 5 files changed, 33 insertions(+), 17 deletions(-)

diff --git a/include/net/ip_vs.h b/include/net/ip_vs.h
index 6ee782f2362c..100638ee3780 100644
--- a/include/net/ip_vs.h
+++ b/include/net/ip_vs.h
@@ -1717,6 +1717,7 @@ struct ip_vs_conn *ip_vs_conn_new(const struct ip_vs_conn_param *p, int dest_af,
 				  __be16 dport, unsigned int flags,
 				  struct ip_vs_dest *dest, __u32 fwmark);
 void ip_vs_conn_expire_now(struct ip_vs_conn *cp);
+void ip_vs_conn_mark_reused(struct ip_vs_conn *cp);
 
 const char *ip_vs_state_name(const struct ip_vs_conn *cp);
 
diff --git a/include/uapi/linux/ip_vs.h b/include/uapi/linux/ip_vs.h
index 2c37c6ac7525..b327e07e2c3f 100644
--- a/include/uapi/linux/ip_vs.h
+++ b/include/uapi/linux/ip_vs.h
@@ -112,6 +112,13 @@
 				IP_VS_CONN_F_NFCT | \
 				0)
 
+#define IP_VS_CONN_F_REUSED	(1 << 30)	/* replaced from new one */
+
+/* Connections with any of these flags are not synced */
+#define IP_VS_CONN_F_MASTER_NOSYNC	(IP_VS_CONN_F_ONE_PACKET | \
+					 IP_VS_CONN_F_REUSED | \
+					 0)
+
 #define IP_VS_SCHEDNAME_MAXLEN	16
 #define IP_VS_PENAME_MAXLEN	16
 #define IP_VS_IFNAME_MAXLEN	16
diff --git a/net/netfilter/ipvs/ip_vs_conn.c b/net/netfilter/ipvs/ip_vs_conn.c
index 98a3c9ec123f..f9f1665daa1f 100644
--- a/net/netfilter/ipvs/ip_vs_conn.c
+++ b/net/netfilter/ipvs/ip_vs_conn.c
@@ -405,6 +405,7 @@ __ip_vs_conn_in_get(const struct ip_vs_conn_param *p)
 						     &cp->vaddr) &&
 				    (!p->cport ^
 				     (!(cp->flags & IP_VS_CONN_F_NO_CPORT))) &&
+				    !(cp->flags & IP_VS_CONN_F_REUSED) &&
 				    p->protocol == cp->protocol) {
 					if (__ip_vs_conn_get(cp)) {
 						/* HIT */
@@ -592,6 +593,7 @@ struct ip_vs_conn *ip_vs_conn_out_get(const struct ip_vs_conn_param *p)
 				    ip_vs_addr_equal(p->af, p->caddr, saddr) &&
 				    (!p->vport ^
 				     (!(cp->flags & IP_VS_CONN_F_NO_CPORT))) &&
+				    !(cp->flags & IP_VS_CONN_F_REUSED) &&
 				    p->protocol == cp->protocol) {
 					if (__ip_vs_conn_get(cp))
 						goto out;
@@ -1468,6 +1470,19 @@ void ip_vs_conn_expire_now(struct ip_vs_conn *cp)
 		mod_timer_pending(&cp->timer, jiffies);
 }
 
+/* Mark connection as reused:
+ * - it is still hashed
+ * - but it is not hit by packets
+ * - it is replaced by new connection with same addresses and ports
+ * - it is not synced anymore
+ */
+void ip_vs_conn_mark_reused(struct ip_vs_conn *cp)
+{
+	spin_lock_bh(&cp->lock);
+	cp->flags &= ~IP_VS_CONN_F_NFCT;
+	cp->flags |= IP_VS_CONN_F_REUSED;
+	spin_unlock_bh(&cp->lock);
+}
 
 /*
  *	Create a new connection entry and hash it into the conn_tab
diff --git a/net/netfilter/ipvs/ip_vs_core.c b/net/netfilter/ipvs/ip_vs_core.c
index fe3ff0a10ddf..5354ecfe2607 100644
--- a/net/netfilter/ipvs/ip_vs_core.c
+++ b/net/netfilter/ipvs/ip_vs_core.c
@@ -2215,11 +2215,8 @@ ip_vs_in_hook(void *priv, struct sk_buff *skb, const struct nf_hook_state *state
 		}
 
 		if (resched) {
-			if (!old_ct) {
-				spin_lock_bh(&cp->lock);
-				cp->flags &= ~IP_VS_CONN_F_NFCT;
-				spin_unlock_bh(&cp->lock);
-			}
+			if (!old_ct)
+				ip_vs_conn_mark_reused(cp);
 			if (!atomic_read(&cp->n_control))
 				ip_vs_conn_expire_now(cp);
 			__ip_vs_conn_put(cp);
@@ -2235,12 +2232,8 @@ ip_vs_in_hook(void *priv, struct sk_buff *skb, const struct nf_hook_state *state
 		if (sysctl_expire_nodest_conn(ipvs)) {
 			bool old_ct = ip_vs_conn_uses_old_conntrack(cp, skb);
 
-			if (!old_ct) {
-				spin_lock_bh(&cp->lock);
-				cp->flags &= ~IP_VS_CONN_F_NFCT;
-				spin_unlock_bh(&cp->lock);
-			}
-
+			if (!old_ct)
+				ip_vs_conn_mark_reused(cp);
 			ip_vs_conn_expire_now(cp);
 			__ip_vs_conn_put(cp);
 			if (old_ct)
diff --git a/net/netfilter/ipvs/ip_vs_sync.c b/net/netfilter/ipvs/ip_vs_sync.c
index 7e562e745ab5..08ac716aad29 100644
--- a/net/netfilter/ipvs/ip_vs_sync.c
+++ b/net/netfilter/ipvs/ip_vs_sync.c
@@ -551,8 +551,7 @@ static void ip_vs_sync_conn_v0(struct netns_ipvs *ipvs, struct ip_vs_conn *cp,
 	if (unlikely(cp->af != AF_INET))
 		return;
 	flags = READ_ONCE(cp->flags);
-	/* Do not sync ONE PACKET */
-	if (flags & IP_VS_CONN_F_ONE_PACKET)
+	if (flags & IP_VS_CONN_F_MASTER_NOSYNC)
 		return;
 
 	if (!ip_vs_sync_conn_needed(ipvs, cp, pkts))
@@ -654,10 +653,10 @@ void ip_vs_sync_conn(struct netns_ipvs *ipvs, struct ip_vs_conn *cp, int pkts)
 		ip_vs_sync_conn_v0(ipvs, cp, pkts);
 		return;
 	}
-	/* Do not sync ONE PACKET */
-	if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
-		goto control;
+
 sloop:
+	if (cp->flags & IP_VS_CONN_F_MASTER_NOSYNC)
+		goto control;
 	if (!ip_vs_sync_conn_needed(ipvs, cp, pkts))
 		goto control;
 	flags = READ_ONCE(cp->flags);
@@ -895,6 +894,7 @@ static void ip_vs_proc_conn(struct netns_ipvs *ipvs, struct ip_vs_conn_param *pa
 		if (cp && ((cp->dport != dport) ||
 			   !ip_vs_addr_equal(cp->daf, &cp->daddr, daddr))) {
 			if (!(flags & IP_VS_CONN_F_INACTIVE)) {
+				ip_vs_conn_mark_reused(cp);
 				ip_vs_conn_expire_now(cp);
 				__ip_vs_conn_put(cp);
 				cp = NULL;
@@ -1003,7 +1003,7 @@ static bool ip_vs_sync_validate_flags(u32 flags)
 		if (flags & IP_VS_CONN_F_TEMPLATE)
 			return false;
 	}
-	if (flags & IP_VS_CONN_F_ONE_PACKET)
+	if (flags & IP_VS_CONN_F_MASTER_NOSYNC)
 		return false;
 	return true;
 }

Prerequisite-Message-Id: <20261103165746.39516-1-axel.mierczuk@1password.com>
-- 
2.56.1

Regards

--
Julian Anastasov <ja@ssi.bg>


      reply	other threads:[~2026-11-25  5:05 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17 15:38 [BUG] ipvs: new connection on a reused port goes to the previous real server during conn_tab resize Zhenrong (Daniel) Gu
2026-11-18 15:26 ` Julian Anastasov
2026-11-19 11:39   ` Zhenrong (Daniel) Gu
2026-11-20  4:55     ` Julian Anastasov
2026-11-24 12:47       ` Zhenrong (Daniel) Gu
2026-11-25  5:04         ` Julian Anastasov [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4da25158-cbab-1186-b948-e17b13262bc3@ssi.bg \
    --to=ja@ssi.bg \
    --cc=lvs-devel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=p-zhang@xjtu.edu.cn \
    --cc=xingfeng@stu.xjtu.edu.cn \
    --cc=zrgu@stu.xjtu.edu.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox