All of lore.kernel.org
 help / color / mirror / Atom feed
From: Stephen Hemminger <stephen@networkplumber.org>
To: dsahern@gmail.com
Cc: netdev@vger.kernel.org,
	Stephen Hemminger <sthemmin@microsoft.com>,
	Stephen Hemminger <stephen@networkplumber.org>
Subject: [RFC iproute2-next 12/16] iproute: make flush a separate function
Date: Thu,  1 Feb 2018 17:19:42 -0800	[thread overview]
Message-ID: <20180202011946.21929-13-sthemmin@microsoft.com> (raw)
In-Reply-To: <20180202011946.21929-1-sthemmin@microsoft.com>

Minor refactoring to make function size smaller and reduce
depth of nesting.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 ip/iproute.c | 121 +++++++++++++++++++++++++++++++----------------------------
 1 file changed, 64 insertions(+), 57 deletions(-)

diff --git a/ip/iproute.c b/ip/iproute.c
index 55b2f51e3987..650e3e8b00ee 100644
--- a/ip/iproute.c
+++ b/ip/iproute.c
@@ -1472,6 +1472,68 @@ static int save_route_prep(void)
 	return 0;
 }
 
+static int iproute_flush(int do_ipv6, rtnl_filter_t filter_fn)
+{
+	time_t start = time(0);
+	char flushb[4096-512];
+	int round = 0;
+	int ret;
+
+	if (filter.cloned) {
+		if (do_ipv6 != AF_INET6) {
+			iproute_flush_cache();
+			if (show_stats)
+				printf("*** IPv4 routing cache is flushed.\n");
+		}
+		if (do_ipv6 == AF_INET)
+			return 0;
+	}
+
+	filter.flushb = flushb;
+	filter.flushp = 0;
+	filter.flushe = sizeof(flushb);
+
+	for (;;) {
+		if (rtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE) < 0) {
+			perror("Cannot send dump request");
+			return -2;
+		}
+		filter.flushed = 0;
+		if (rtnl_dump_filter(&rth, filter_fn, stdout) < 0) {
+			fprintf(stderr, "Flush terminated\n");
+			return -2;
+		}
+		if (filter.flushed == 0) {
+			if (show_stats) {
+				if (round == 0 &&
+				    (!filter.cloned || do_ipv6 == AF_INET6))
+					printf("Nothing to flush.\n");
+				else
+					printf("*** Flush is complete after %d round%s ***\n",
+					       round, round > 1 ? "s" : "");
+			}
+			fflush(stdout);
+			return 0;
+		}
+		round++;
+		ret = flush_update();
+		if (ret < 0)
+			return ret;
+
+		if (time(0) - start > 30) {
+			printf("\n*** Flush not completed after %ld seconds, %d entries remain ***\n",
+			       (long)(time(0) - start), filter.flushed);
+			return -1;
+		}
+
+		if (show_stats) {
+			printf("\n*** Round %d, deleting %d entries ***\n",
+			       round, filter.flushed);
+			fflush(stdout);
+		}
+	}
+}
+
 static int iproute_list_flush_or_save(int argc, char **argv, int action)
 {
 	int do_ipv6 = preferred_family;
@@ -1479,7 +1541,6 @@ static int iproute_list_flush_or_save(int argc, char **argv, int action)
 	char *od = NULL;
 	unsigned int mark = 0;
 	rtnl_filter_t filter_fn;
-	int ret;
 
 	if (action == IPROUTE_SAVE) {
 		if (save_route_prep())
@@ -1679,62 +1740,8 @@ static int iproute_list_flush_or_save(int argc, char **argv, int action)
 	}
 	filter.mark = mark;
 
-	if (action == IPROUTE_FLUSH) {
-		int round = 0;
-		char flushb[4096-512];
-		time_t start = time(0);
-
-		if (filter.cloned) {
-			if (do_ipv6 != AF_INET6) {
-				iproute_flush_cache();
-				if (show_stats)
-					printf("*** IPv4 routing cache is flushed.\n");
-			}
-			if (do_ipv6 == AF_INET)
-				return 0;
-		}
-
-		filter.flushb = flushb;
-		filter.flushp = 0;
-		filter.flushe = sizeof(flushb);
-
-		for (;;) {
-			if (rtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE) < 0) {
-				perror("Cannot send dump request");
-				return -2;
-			}
-			filter.flushed = 0;
-			if (rtnl_dump_filter(&rth, filter_fn, stdout) < 0) {
-				fprintf(stderr, "Flush terminated\n");
-				return -2;
-			}
-			if (filter.flushed == 0) {
-				if (show_stats) {
-					if (round == 0 && (!filter.cloned || do_ipv6 == AF_INET6))
-						printf("Nothing to flush.\n");
-					else
-						printf("*** Flush is complete after %d round%s ***\n", round, round > 1?"s":"");
-				}
-				fflush(stdout);
-				return 0;
-			}
-			round++;
-			ret = flush_update();
-			if (ret < 0)
-				return ret;
-
-			if (time(0) - start > 30) {
-				printf("\n*** Flush not completed after %ld seconds, %d entries remain ***\n",
-				       (long)(time(0) - start), filter.flushed);
-				return -1;
-			}
-
-			if (show_stats) {
-				printf("\n*** Round %d, deleting %d entries ***\n", round, filter.flushed);
-				fflush(stdout);
-			}
-		}
-	}
+	if (action == IPROUTE_FLUSH)
+		return iproute_flush(do_ipv6, filter_fn);
 
 	if (!filter.cloned) {
 		if (rtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE) < 0) {
-- 
2.15.1

  parent reply	other threads:[~2018-02-02  1:20 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-02  1:19 [RFC iproute2-next 00/16] iproute printing enhancements Stephen Hemminger
2018-02-02  1:19 ` [RFC iproute2-next 01/16] iproute: refactor printing flags Stephen Hemminger
2018-02-02  1:19 ` [RFC iproute2-next 02/16] iproute: make printing icmpv6 a function Stephen Hemminger
2018-02-02  1:19 ` [RFC iproute2-next 03/16] iproute: make printing IPv4 cache flags " Stephen Hemminger
2018-02-02  1:19 ` [RFC iproute2-next 04/16] iproute: refactor cacheinfo printing Stephen Hemminger
2018-02-02  1:19 ` [RFC iproute2-next 05/16] iproute: refactor metrics print Stephen Hemminger
2018-02-02  1:19 ` [RFC iproute2-next 06/16] iproute: refactor printing flow info Stephen Hemminger
2018-02-02  1:19 ` [RFC iproute2-next 07/16] iproute2: refactor newdst, gateway and via printing Stephen Hemminger
2018-02-02  1:19 ` [RFC iproute2-next 08/16] iproute: refactor multipath print Stephen Hemminger
2018-02-02  1:19 ` [RFC iproute2-next 09/16] iproute: refactor printing of interface Stephen Hemminger
2018-02-02  1:19 ` [RFC iproute2-next 10/16] iproute: whitespace fixes Stephen Hemminger
2018-02-02  1:19 ` [RFC iproute2-next 11/16] iproute: don't do assignment in condition Stephen Hemminger
2018-02-02  1:19 ` Stephen Hemminger [this message]
2018-02-02  1:19 ` [RFC iproute2-next 13/16] iproute: implement JSON and color output Stephen Hemminger
2018-02-02  1:19 ` [RFC iproute2-next 14/16] json: make pretty printing optional Stephen Hemminger
2018-02-02  1:19 ` [RFC iproute2-next 15/16] man: add documentation for json and pretty flags Stephen Hemminger
2018-02-02  1:19 ` [RFC iproute2-next 16/16] json: fix newline at end of array Stephen Hemminger
2018-02-02  2:59 ` [RFC iproute2-next 00/16] iproute printing enhancements David Ahern
2018-02-02  3:08 ` David Ahern
2018-02-02 22:24   ` Stephen Hemminger

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=20180202011946.21929-13-sthemmin@microsoft.com \
    --to=stephen@networkplumber.org \
    --cc=dsahern@gmail.com \
    --cc=netdev@vger.kernel.org \
    --cc=sthemmin@microsoft.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.