ipvs: fix connection reuse with the resizable hash tables
Subsystems (4)
Julian Anastasov
maintainerPATCH 1/1 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.
--- include/net/ip_vs.h | 1 + include/uapi/linux/ip_vs.h | 7 +++++++ net/netfilter/ipvs/ip_vs_conn.c | 16 ++++++++++++++++ net/netfilter/ipvs/ip_vs_core.c | 13 ++----------- net/netfilter/ipvs/ip_vs_sync.c | 9 ++++----- 5 files changed, 30 insertions(+), 16 deletions(-)
include/net/ip_vs.h +1 -0
@@ -1714,6 +1714,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,bool old_ct);const char *ip_vs_state_name (const struct ip_vs_conn *cp);
include/uapi/linux/ip_vs.h +7 -0
@@ -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
net/netfilter/ipvs/ip_vs_conn.c +16 -0
@@ -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,20 @@ 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,bool old_ct) +{ +spin_lock_bh (&cp->lock); +if (!old_ct) + 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
net/netfilter/ipvs/ip_vs_core.c +2 -11
@@ -2209,11 +2209,7 @@ 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); - } +ip_vs_conn_mark_reused (cp, old_ct);if (!atomic_read (&cp->n_control))ip_vs_conn_expire_now (cp);__ip_vs_conn_put (cp);
@@ -2229,12 +2225,7 @@ ip_vs_in_hook(void *priv, struct sk_buff *skb, const struct nf_hook_state *stateif (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); - } - +ip_vs_conn_mark_reused (cp, old_ct);ip_vs_conn_expire_now (cp);__ip_vs_conn_put (cp);if (old_ct)
net/netfilter/ipvs/ip_vs_sync.c +4 -5
@@ -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,8 +653,7 @@ 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 ) +if (cp->flags &IP_VS_CONN_F_MASTER_NOSYNC ) goto control; sloop:if (!ip_vs_sync_conn_needed (ipvs, cp, pkts))
@@ -895,6 +893,7 @@ static void ip_vs_proc_conn(struct netns_ipvs *ipvs, struct ip_vs_conn_param *paif (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, false);ip_vs_conn_expire_now (cp);__ip_vs_conn_put (cp); cp =NULL ;
@@ -1003,7 +1002,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: <[email protected]>
Signature · 1 line
2.56.1
Julian Anastasov
maintainerHello,
Quoted 22 lines · Julian Anastasov
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.
Quoted 7 lines
Reported-by "Zhenrong (Daniel) Gu" <[email protected]>Reported-by Xing Feng <[email protected]>Reported-by Peng Zhang <[email protected]>Fixes 2fa7cc9c7025 ("ipvs: switch to per-net connection table")Signed-off-by Julian Anastasov <[email protected]>
Sashiko has some valid concerns that we can address in new
version:
https://sashiko.dev/#/patchset/20261124160226.103532-1-ja%40ssi.bg
As for the packet with sport 0 matching NO_CPORT connection,
this can be fixed separately.
pw-bot: changes-requested
Regards
--
Julian Anastasov <[email protected]>