From b172b819b78ea8440de8acb1d04a2649dbfcac02 Mon Sep 17 00:00:00 2001
From: Julian Anastasov <ja@ssi.bg>
Date: Mon, 29 Sep 2003 11:57:25 -0700
Subject: [PATCH] [IPVS]: Use list_for_each_entry_continue in some schedulers.

---
 net/ipv4/ipvs/ip_vs_lblc.c  |  9 +++++----
 net/ipv4/ipvs/ip_vs_lblcr.c |  9 +++++----
 net/ipv4/ipvs/ip_vs_lc.c    | 21 ++++-----------------
 net/ipv4/ipvs/ip_vs_sed.c   |  9 +++++----
 net/ipv4/ipvs/ip_vs_wlc.c   |  9 +++++----
 5 files changed, 24 insertions(+), 33 deletions(-)

diff --git a/net/ipv4/ipvs/ip_vs_lblc.c b/net/ipv4/ipvs/ip_vs_lblc.c
index 1fcc770b2a4a..2a592b9e0500 100644
--- a/net/ipv4/ipvs/ip_vs_lblc.c
+++ b/net/ipv4/ipvs/ip_vs_lblc.c
@@ -458,10 +458,11 @@ __ip_vs_wlc_schedule(struct ip_vs_service *svc, struct iphdr *iph)
 	 * The server with weight=0 is quiesced and will not receive any
 	 * new connection.
 	 */
-	list_for_each_entry(least, &svc->destinations, n_list) {
-		if (least->flags & IP_VS_DEST_F_OVERLOAD)
+	list_for_each_entry(dest, &svc->destinations, n_list) {
+		if (dest->flags & IP_VS_DEST_F_OVERLOAD)
 			continue;
-		if (atomic_read(&least->weight) > 0) {
+		if (atomic_read(&dest->weight) > 0) {
+			least = dest;
 			loh = atomic_read(&least->activeconns) * 50
 				+ atomic_read(&least->inactconns);
 			goto nextstage;
@@ -473,7 +474,7 @@ __ip_vs_wlc_schedule(struct ip_vs_service *svc, struct iphdr *iph)
 	 *    Find the destination with the least load.
 	 */
   nextstage:
-	list_for_each_entry(dest, &svc->destinations, n_list) {
+	list_for_each_entry_continue(dest, &svc->destinations, n_list) {
 		if (dest->flags & IP_VS_DEST_F_OVERLOAD)
 			continue;
 
diff --git a/net/ipv4/ipvs/ip_vs_lblcr.c b/net/ipv4/ipvs/ip_vs_lblcr.c
index f09cd84e75b0..dd6ff8f3440e 100644
--- a/net/ipv4/ipvs/ip_vs_lblcr.c
+++ b/net/ipv4/ipvs/ip_vs_lblcr.c
@@ -711,11 +711,12 @@ __ip_vs_wlc_schedule(struct ip_vs_service *svc, struct iphdr *iph)
 	 * The server with weight=0 is quiesced and will not receive any
 	 * new connection.
 	 */
-	list_for_each_entry(least, &svc->destinations, n_list) {
-		if (least->flags & IP_VS_DEST_F_OVERLOAD)
+	list_for_each_entry(dest, &svc->destinations, n_list) {
+		if (dest->flags & IP_VS_DEST_F_OVERLOAD)
 			continue;
 
-		if (atomic_read(&least->weight) > 0) {
+		if (atomic_read(&dest->weight) > 0) {
+			least = dest;
 			loh = atomic_read(&least->activeconns) * 50
 				+ atomic_read(&least->inactconns);
 			goto nextstage;
@@ -727,7 +728,7 @@ __ip_vs_wlc_schedule(struct ip_vs_service *svc, struct iphdr *iph)
 	 *    Find the destination with the least load.
 	 */
   nextstage:
-	list_for_each_entry(dest, &svc->destinations, n_list) {
+	list_for_each_entry_continue(dest, &svc->destinations, n_list) {
 		if (dest->flags & IP_VS_DEST_F_OVERLOAD)
 			continue;
 
diff --git a/net/ipv4/ipvs/ip_vs_lc.c b/net/ipv4/ipvs/ip_vs_lc.c
index f56088aca250..7f903553d7c4 100644
--- a/net/ipv4/ipvs/ip_vs_lc.c
+++ b/net/ipv4/ipvs/ip_vs_lc.c
@@ -65,8 +65,8 @@ ip_vs_lc_dest_overhead(struct ip_vs_dest *dest)
 static struct ip_vs_dest *
 ip_vs_lc_schedule(struct ip_vs_service *svc, struct iphdr *iph)
 {
-	struct ip_vs_dest *dest, *least;
-	unsigned int loh, doh;
+	struct ip_vs_dest *dest, *least = NULL;
+	unsigned int loh = 0, doh;
 
 	IP_VS_DBG(6, "ip_vs_lc_schedule(): Scheduling...\n");
 
@@ -79,31 +79,18 @@ ip_vs_lc_schedule(struct ip_vs_service *svc, struct iphdr *iph)
 	 * served, but no new connection is assigned to the server.
 	 */
 
-	list_for_each_entry(least, &svc->destinations, n_list) {
-		if (least->flags & IP_VS_DEST_F_OVERLOAD)
-			continue;
-		if (atomic_read(&least->weight) > 0) {
-			loh = ip_vs_lc_dest_overhead(least);
-			goto nextstage;
-		}
-	}
-	return NULL;
-
-	/*
-	 *    Find the destination with the least load.
-	 */
-  nextstage:
 	list_for_each_entry(dest, &svc->destinations, n_list) {
 		if ((dest->flags & IP_VS_DEST_F_OVERLOAD) ||
 		    atomic_read(&dest->weight) == 0)
 			continue;
 		doh = ip_vs_lc_dest_overhead(dest);
-		if (doh < loh) {
+		if (!least || doh < loh) {
 			least = dest;
 			loh = doh;
 		}
 	}
 
+	if (least)
 	IP_VS_DBG(6, "LC: server %u.%u.%u.%u:%u activeconns %d inactconns %d\n",
 		  NIPQUAD(least->addr), ntohs(least->port),
 		  atomic_read(&least->activeconns),
diff --git a/net/ipv4/ipvs/ip_vs_sed.c b/net/ipv4/ipvs/ip_vs_sed.c
index a57b455a422b..a8d6791b1ee5 100644
--- a/net/ipv4/ipvs/ip_vs_sed.c
+++ b/net/ipv4/ipvs/ip_vs_sed.c
@@ -103,9 +103,10 @@ ip_vs_sed_schedule(struct ip_vs_service *svc, struct iphdr *iph)
 	 * new connections.
 	 */
 
-	list_for_each_entry(least, &svc->destinations, n_list) {
-		if (!(least->flags & IP_VS_DEST_F_OVERLOAD) &&
-		    atomic_read(&least->weight) > 0) {
+	list_for_each_entry(dest, &svc->destinations, n_list) {
+		if (!(dest->flags & IP_VS_DEST_F_OVERLOAD) &&
+		    atomic_read(&dest->weight) > 0) {
+			least = dest;
 			loh = ip_vs_sed_dest_overhead(least);
 			goto nextstage;
 		}
@@ -116,7 +117,7 @@ ip_vs_sed_schedule(struct ip_vs_service *svc, struct iphdr *iph)
 	 *    Find the destination with the least load.
 	 */
   nextstage:
-	list_for_each_entry(dest, &svc->destinations, n_list) {
+	list_for_each_entry_continue(dest, &svc->destinations, n_list) {
 		if (dest->flags & IP_VS_DEST_F_OVERLOAD)
 			continue;
 		doh = ip_vs_sed_dest_overhead(dest);
diff --git a/net/ipv4/ipvs/ip_vs_wlc.c b/net/ipv4/ipvs/ip_vs_wlc.c
index 1a0a46d44896..ec7a374d6e27 100644
--- a/net/ipv4/ipvs/ip_vs_wlc.c
+++ b/net/ipv4/ipvs/ip_vs_wlc.c
@@ -91,9 +91,10 @@ ip_vs_wlc_schedule(struct ip_vs_service *svc, struct iphdr *iph)
 	 * new connections.
 	 */
 
-	list_for_each_entry(least, &svc->destinations, n_list) {
-		if (!(least->flags & IP_VS_DEST_F_OVERLOAD) &&
-		    atomic_read(&least->weight) > 0) {
+	list_for_each_entry(dest, &svc->destinations, n_list) {
+		if (!(dest->flags & IP_VS_DEST_F_OVERLOAD) &&
+		    atomic_read(&dest->weight) > 0) {
+			least = dest;
 			loh = ip_vs_wlc_dest_overhead(least);
 			goto nextstage;
 		}
@@ -104,7 +105,7 @@ ip_vs_wlc_schedule(struct ip_vs_service *svc, struct iphdr *iph)
 	 *    Find the destination with the least load.
 	 */
   nextstage:
-	list_for_each_entry(dest, &svc->destinations, n_list) {
+	list_for_each_entry_continue(dest, &svc->destinations, n_list) {
 		if (dest->flags & IP_VS_DEST_F_OVERLOAD)
 			continue;
 		doh = ip_vs_wlc_dest_overhead(dest);
-- 
2.30.9