Skip to contentSkip to conversation

ipvs: make ip_vs_svc_table and ip_vs_svc_fwm_table per netns

PATCH Superseded Dust Li 2 participants 4 messages Jul 23, 2023 – Jul 24, 2023

Subsystems (5)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Dust Li

author
#0 lore raw

PATCH v2 1/1 ipvs: make ip_vs_svc_table and ip_vs_svc_fwm_table per netns

From: Jiejian Wu <[email protected]>

Current ipvs uses one global mutex "__ip_vs_mutex" to keep the global "ip_vs_svc_table" and "ip_vs_svc_fwm_table" safe. But when there are tens of thousands of services from different netns in the table, it takes a long time to look up the table, for example, using "ipvsadm -ln" from different netns simultaneously.

We make "ip_vs_svc_table" and "ip_vs_svc_fwm_table" per netns, and we add "service_mutex" per netns to keep these two tables safe instead of the global "__ip_vs_mutex" in current version. To this end, looking up services from different netns simultaneously will not get stuck, shortening the time consumption in large-scale deployment. It can be reproduced using the simple scripts below.

init.sh: #!/bin/bash
for((i=1;i<=4;i++));do
ip netns add ns$i
ip netns exec ns$i ip link set dev lo up
ip netns exec ns$i sh add-services.sh
done

add-services.sh: #!/bin/bash
for((i=0;i<30000;i++)); do

        ipvsadm -A  -t 10.10.10.10:$((80+$i)) -s rr

done

runtest.sh: #!/bin/bash for((i=1;i<4;i++));do
ip netns exec ns$i ipvsadm -ln > /dev/null &
done
ip netns exec ns4 ipvsadm -ln > /dev/null

Run "sh init.sh" to initiate the network environment. Then run "time ./runtest.sh" to evaluate the time consumption. Our testbed is a 4-core Intel Xeon ECS. The result of the original version is around 8 seconds, while the result of the modified version is only 0.8 seconds.

---
v2:
* remove global __ip_vs_mutex in ip_vs_est.c
* remove ip_vs_ prefix for svc_table and svc_fwm_table
* remove redundant "svc->ipvs == ipvs" checks

---
 include/net/ip_vs.h            |  12 +++
 net/netfilter/ipvs/ip_vs_ctl.c | 163 ++++++++++++++-------------------
 net/netfilter/ipvs/ip_vs_est.c |  18 ++--
 3 files changed, 92 insertions(+), 101 deletions(-)
include/net/ip_vs.h +12 -0
index ff406ef4fd4aa..8c94cc420708b 100644
@@ -33,6 +33,12 @@
 
 #define IP_VS_HDR_INVERSE	1
 #define IP_VS_HDR_ICMP		2
+/*
+ *	Hash table: for virtual service lookups
+ */
+#define IP_VS_SVC_TAB_BITS 8
+#define IP_VS_SVC_TAB_SIZE (1 << IP_VS_SVC_TAB_BITS)
+#define IP_VS_SVC_TAB_MASK (IP_VS_SVC_TAB_SIZE - 1)
 
 /* Generic access of ipvs struct */
 static inline struct netns_ipvs *net_ipvs(struct net* net)
@@ -1041,6 +1047,12 @@ struct netns_ipvs {
 	 */
 	unsigned int		mixed_address_family_dests;
 	unsigned int		hooks_afmask;	/* &1=AF_INET, &2=AF_INET6 */
+
+	struct mutex service_mutex;
+	/* the service table hashed by <protocol, addr, port> */
+	struct hlist_head svc_table[IP_VS_SVC_TAB_SIZE];
+	/* the service table hashed by fwmark */
+	struct hlist_head svc_fwm_table[IP_VS_SVC_TAB_SIZE];
 };
 
 #define DEFAULT_SYNC_THRESHOLD	3
net/netfilter/ipvs/ip_vs_ctl.c +71 -92

422 lines not on this page · raw message

net/netfilter/ipvs/ip_vs_est.c +9 -9
index c5970ba416aea..323098dc8be68 100644
@@ -601,7 +601,7 @@ static void ip_vs_est_drain_temp_list(struct netns_ipvs *ipvs)
 	while (1) {
 		int max = 16;
 
-		mutex_lock(&__ip_vs_mutex);
+		mutex_lock(&ipvs->service_mutex);
 
 		while (max-- > 0) {
 			est = hlist_entry_safe(ipvs->est_temp_list.first,
@@ -621,12 +621,12 @@ static void ip_vs_est_drain_temp_list(struct netns_ipvs *ipvs)
 			}
 			goto unlock;
 		}
-		mutex_unlock(&__ip_vs_mutex);
+		mutex_unlock(&ipvs->service_mutex);
 		cond_resched();
 	}
 
 unlock:
-	mutex_unlock(&__ip_vs_mutex);
+	mutex_unlock(&ipvs->service_mutex);
 }
 
 /* Calculate limits for all kthreads */
@@ -646,9 +646,9 @@ static int ip_vs_est_calc_limits(struct netns_ipvs *ipvs, int *chain_max)
 	u64 val;
 
 	INIT_HLIST_HEAD(&chain);
-	mutex_lock(&__ip_vs_mutex);
+	mutex_lock(&ipvs->service_mutex);
 	kd = ipvs->est_kt_arr[0];
-	mutex_unlock(&__ip_vs_mutex);
+	mutex_unlock(&ipvs->service_mutex);
 	s = kd ? kd->calc_stats : NULL;
 	if (!s)
 		goto out;
@@ -747,7 +747,7 @@ static void ip_vs_est_calc_phase(struct netns_ipvs *ipvs)
 	if (!ip_vs_est_calc_limits(ipvs, &chain_max))
 		return;
 
-	mutex_lock(&__ip_vs_mutex);
+	mutex_lock(&ipvs->service_mutex);
 
 	/* Stop all other tasks, so that we can immediately move the
 	 * estimators to est_temp_list without RCU grace period
@@ -814,9 +814,9 @@ static void ip_vs_est_calc_phase(struct netns_ipvs *ipvs)
 		/* Give chance estimators to be added (to est_temp_list)
 		 * and deleted (releasing kthread contexts)
 		 */
-		mutex_unlock(&__ip_vs_mutex);
+		mutex_unlock(&ipvs->service_mutex);
 		cond_resched();
-		mutex_lock(&__ip_vs_mutex);
+		mutex_lock(&ipvs->service_mutex);
 
 		/* Current kt released ? */
 		if (id >= ipvs->est_kt_count)
@@ -892,7 +892,7 @@ static void ip_vs_est_calc_phase(struct netns_ipvs *ipvs)
 	mutex_unlock(&ipvs->est_mutex);
 
 unlock:
-	mutex_unlock(&__ip_vs_mutex);
+	mutex_unlock(&ipvs->service_mutex);
 }
 
 void ip_vs_zero_estimator(struct ip_vs_stats *stats)
Signature · 1 line
2.19.1.6.gb485710b
maintainer
#1 lore raw

Hello,

Quoted 33 lines · Dust Li

From: Jiejian Wu <[email protected]>

Current ipvs uses one global mutex "__ip_vs_mutex" to keep the global "ip_vs_svc_table" and "ip_vs_svc_fwm_table" safe. But when there are tens of thousands of services from different netns in the table, it takes a long time to look up the table, for example, using "ipvsadm -ln" from different netns simultaneously.

We make "ip_vs_svc_table" and "ip_vs_svc_fwm_table" per netns, and we add "service_mutex" per netns to keep these two tables safe instead of the global "__ip_vs_mutex" in current version. To this end, looking up services from different netns simultaneously will not get stuck, shortening the time consumption in large-scale deployment. It can be reproduced using the simple scripts below.

init.sh: #!/bin/bash
for((i=1;i<=4;i++));do
ip netns add ns$i
ip netns exec ns$i ip link set dev lo up
ip netns exec ns$i sh add-services.sh
done

add-services.sh: #!/bin/bash
for((i=0;i<30000;i++)); do

        ipvsadm -A  -t 10.10.10.10:$((80+$i)) -s rr

done

runtest.sh: #!/bin/bash for((i=1;i<4;i++));do
ip netns exec ns$i ipvsadm -ln > /dev/null &
done
ip netns exec ns4 ipvsadm -ln > /dev/null

Quoted 8 lines

Run "sh init.sh" to initiate the network environment. Then run "time ./runtest.sh" to evaluate the time consumption. Our testbed is a 4-core Intel Xeon ECS. The result of the original version is around 8 seconds, while the result of the modified version is only 0.8 seconds.

Changes look good to me, thanks! But checkpatch is reporting
for some cosmetic changes that you have to do in v3:

scripts/checkpatch.pl --strict /tmp/file.patch

Regards

--
Julian Anastasov <[email protected]>

author
#2 lore raw
Quoted 43 lines · Julian Anastasov

Hello,

Quoted 38 lines · Dust Li

From: Jiejian Wu <[email protected]>

Current ipvs uses one global mutex "__ip_vs_mutex" to keep the global "ip_vs_svc_table" and "ip_vs_svc_fwm_table" safe. But when there are tens of thousands of services from different netns in the table, it takes a long time to look up the table, for example, using "ipvsadm -ln" from different netns simultaneously.

We make "ip_vs_svc_table" and "ip_vs_svc_fwm_table" per netns, and we add "service_mutex" per netns to keep these two tables safe instead of the global "__ip_vs_mutex" in current version. To this end, looking up services from different netns simultaneously will not get stuck, shortening the time consumption in large-scale deployment. It can be reproduced using the simple scripts below.

init.sh: #!/bin/bash
for((i=1;i<=4;i++));do
ip netns add ns$i
ip netns exec ns$i ip link set dev lo up
ip netns exec ns$i sh add-services.sh
done

add-services.sh: #!/bin/bash
for((i=0;i<30000;i++)); do

        ipvsadm -A  -t 10.10.10.10:$((80+$i)) -s rr

done

runtest.sh: #!/bin/bash for((i=1;i<4;i++));do
ip netns exec ns$i ipvsadm -ln > /dev/null &
done
ip netns exec ns4 ipvsadm -ln > /dev/null

Run "sh init.sh" to initiate the network environment. Then run "time ./runtest.sh" to evaluate the time consumption. Our testbed is a 4-core Intel Xeon ECS. The result of the original version is around 8 seconds, while the result of the modified version is only 0.8 seconds.

Quoted 8 lines
Quoted 3 lines

Changes look good to me, thanks! But checkpatch is reporting
for some cosmetic changes that you have to do in v3:

scripts/checkpatch.pl --strict /tmp/file.patch

Oh, sorry for that! I ignored the CHECKs checkpatch reported, my checkpatch shows:

$./scripts/checkpatch.pl --strict 0001-ipvs-make-ip_vs_svc_table-and-ip_vs_svc_fwm_table-pe.patch
CHECK: Prefer using the BIT macro
#69: FILE: include/net/ip_vs.h:40:
+#define IP_VS_SVC_TAB_SIZE (1 << IP_VS_SVC_TAB_BITS)

We just moved this line from ip_vs_ctl.c to ip_vs.h, so we ignored the BIT macro. Do you think we should change it using BIT macro ?

CHECK: struct mutex definition without comment
#79: FILE: include/net/ip_vs.h:1051:
+ struct mutex service_mutex;

I think we can add comment for it.
But rethinking a bit on the service_mutex in ip_vs_est.c, I'm a wondering why we are using the service_mutex in estimation ? Is est_mutex enough for the protecting in ip_vs_est.c ?

CHECK: Logical continuations should be on the previous line #161: FILE: net/netfilter/ipvs/ip_vs_ctl.c:410:

                       && (svc->port == vport)

+ && (svc->protocol == protocol)) {

This is just the removal of '(svc->ipvs == ipvs)' and kept it as it is. So haven't change according to checkpatch. If you prefer, I can modify it to make checkpatch happy.

CHECK: Alignment should match open parenthesis #233: FILE: net/netfilter/ipvs/ip_vs_ctl.c:1767:
+ list_for_each_entry(dest, &svc->destinations,
+ n_list) {

We missed this, will change.

CHECK: Alignment should match open parenthesis #246: FILE: net/netfilter/ipvs/ip_vs_ctl.c:1774:
+ list_for_each_entry(dest, &svc->destinations,
+ n_list) {

Same above.

total: 0 errors, 0 warnings, 5 checks, 506 lines checked

Quoted 5 lines

Regards

--
Julian Anastasov <[email protected]>

maintainer
#3 lore raw

Hello,

Quoted 11 lines · Dust Li
Quoted 5 lines · Julian Anastasov

Changes look good to me, thanks! But checkpatch is reporting
for some cosmetic changes that you have to do in v3:

scripts/checkpatch.pl --strict /tmp/file.patch

Oh, sorry for that! I ignored the CHECKs checkpatch reported, my checkpatch shows:

Quoted 7 lines
   $./scripts/checkpatch.pl --strict 0001-ipvs-make-ip_vs_svc_table-and-ip_vs_svc_fwm_table-pe.patch
   CHECK: Prefer using the BIT macro
   #69: FILE: include/net/ip_vs.h:40:
   +#define IP_VS_SVC_TAB_SIZE (1 << IP_VS_SVC_TAB_BITS)

We just moved this line from ip_vs_ctl.c to ip_vs.h, so we ignored the BIT macro. Do you think we should change it using BIT macro ?

Yes, lets fix all these complains.

Quoted 8 lines

CHECK: struct mutex definition without comment
#79: FILE: include/net/ip_vs.h:1051:
+ struct mutex service_mutex;

I think we can add comment for it.
But rethinking a bit on the service_mutex in ip_vs_est.c, I'm a wondering why we are using the service_mutex in estimation ? Is est_mutex enough for the protecting in ip_vs_est.c ?

Yes, the estimation kthreads are synchronized only
with reconfiguration: ip_vs_start_estimator() and ip_vs_stop_estimator() are called under service_mutex. And the estimation data is already per-net, there is no global data.

Quoted 8 lines

CHECK: Logical continuations should be on the previous line #161: FILE: net/netfilter/ipvs/ip_vs_ctl.c:410:

                       && (svc->port == vport)

+ && (svc->protocol == protocol)) {

This is just the removal of '(svc->ipvs == ipvs)' and kept it as it is. So haven't change according to checkpatch. If you prefer, I can modify it to make checkpatch happy.

	Yes, lets move all '&&' in this 'if' block and also remove the 

parens:

	if (svc->af == af && ip_vs_addr_equal(af, &svc->addr, vaddr) &&
	    svc->port == vport && svc->protocol == protocol) {

Regards

--
Julian Anastasov <[email protected]>