b.a.t.m.a.n.lists.open-mesh.org archive mirror
 help / color / mirror / Atom feed
From: Markus Pargmann <mpa@pengutronix.de>
To: Marek Lindner <mareklindner@neomailbox.ch>,
	Simon Wunderlich <sw@simonwunderlich.de>,
	Antonio Quartulli <antonio@meshcoding.com>
Cc: b.a.t.m.a.n@lists.open-mesh.org, Sven Eckelmann <sven@narfation.org>
Subject: [B.A.T.M.A.N.] [PATCH v2 03/26] batman-adv: iv_ogm, Reduce code duplication
Date: Fri, 26 Dec 2014 12:41:20 +0100	[thread overview]
Message-ID: <1419594103-10928-4-git-send-email-mpa@pengutronix.de> (raw)
In-Reply-To: <1419594103-10928-1-git-send-email-mpa@pengutronix.de>

The difference between tq1 and tq2 are calculated the same way in two
separate functions.

This patch moves the common code to a seperate function
'batadv_iv_ogm_neigh_diff' which handles everything necessary. The other
two functions can then handle errors and use the difference directly.

Signed-off-by: Markus Pargmann <mpa@pengutronix.de>
---
 bat_iv_ogm.c | 80 +++++++++++++++++++++++++++++++-----------------------------
 1 file changed, 41 insertions(+), 39 deletions(-)

diff --git a/bat_iv_ogm.c b/bat_iv_ogm.c
index 2d064a71613f..1458ecfa66b8 100644
--- a/bat_iv_ogm.c
+++ b/bat_iv_ogm.c
@@ -1858,36 +1858,27 @@ next:
 		seq_puts(seq, "No batman nodes in range ...\n");
 }
 
-/**
- * batadv_iv_ogm_neigh_cmp - compare the metrics of two neighbors
- * @neigh1: the first neighbor object of the comparison
- * @if_outgoing1: outgoing interface for the first neighbor
- * @neigh2: the second neighbor object of the comparison
- * @if_outgoing2: outgoing interface for the second neighbor
- *
- * Returns a value less, equal to or greater than 0 if the metric via neigh1 is
- * lower, the same as or higher than the metric via neigh2
- */
-static int batadv_iv_ogm_neigh_cmp(struct batadv_neigh_node *neigh1,
-				   struct batadv_hard_iface *if_outgoing1,
-				   struct batadv_neigh_node *neigh2,
-				   struct batadv_hard_iface *if_outgoing2)
+static int batadv_iv_ogm_neigh_diff(struct batadv_neigh_node *neigh1,
+				    struct batadv_hard_iface *if_outgoing1,
+				    struct batadv_neigh_node *neigh2,
+				    struct batadv_hard_iface *if_outgoing2,
+				    int *diff)
 {
 	struct batadv_neigh_ifinfo *neigh1_ifinfo, *neigh2_ifinfo;
 	uint8_t tq1, tq2;
-	int diff;
+	int ret;
 
 	neigh1_ifinfo = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
 	neigh2_ifinfo = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
 
 	if (!neigh1_ifinfo || !neigh2_ifinfo) {
-		diff = 0;
+		ret = -EINVAL;
 		goto out;
 	}
 
 	tq1 = neigh1_ifinfo->bat_iv.tq_avg;
 	tq2 = neigh2_ifinfo->bat_iv.tq_avg;
-	diff = tq1 - tq2;
+	*diff = (int)tq1 - (int)tq2;
 
 out:
 	if (neigh1_ifinfo)
@@ -1895,6 +1886,32 @@ out:
 	if (neigh2_ifinfo)
 		batadv_neigh_ifinfo_free_ref(neigh2_ifinfo);
 
+	return ret;
+}
+
+/**
+ * batadv_iv_ogm_neigh_cmp - compare the metrics of two neighbors
+ * @neigh1: the first neighbor object of the comparison
+ * @if_outgoing1: outgoing interface for the first neighbor
+ * @neigh2: the second neighbor object of the comparison
+ * @if_outgoing2: outgoing interface for the second neighbor
+ *
+ * Returns a value less, equal to or greater than 0 if the metric via neigh1 is
+ * lower, the same as or higher than the metric via neigh2
+ */
+static int batadv_iv_ogm_neigh_cmp(struct batadv_neigh_node *neigh1,
+				   struct batadv_hard_iface *if_outgoing1,
+				   struct batadv_neigh_node *neigh2,
+				   struct batadv_hard_iface *if_outgoing2)
+{
+	int ret;
+	int diff;
+
+	ret = batadv_iv_ogm_neigh_diff(neigh1, if_outgoing1, neigh2,
+				       if_outgoing2, &diff);
+	if (ret)
+		return 0;
+
 	return diff;
 }
 
@@ -1915,30 +1932,15 @@ batadv_iv_ogm_neigh_is_eob(struct batadv_neigh_node *neigh1,
 			   struct batadv_neigh_node *neigh2,
 			   struct batadv_hard_iface *if_outgoing2)
 {
-	struct batadv_neigh_ifinfo *neigh1_ifinfo, *neigh2_ifinfo;
-	uint8_t tq1, tq2;
-	bool ret;
-
-	neigh1_ifinfo = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
-	neigh2_ifinfo = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
-
-	/* we can't say that the metric is better */
-	if (!neigh1_ifinfo || !neigh2_ifinfo) {
-		ret = false;
-		goto out;
-	}
-
-	tq1 = neigh1_ifinfo->bat_iv.tq_avg;
-	tq2 = neigh2_ifinfo->bat_iv.tq_avg;
-	ret = (tq1 - tq2) > -BATADV_TQ_SIMILARITY_THRESHOLD;
+	int ret;
+	int diff;
 
-out:
-	if (neigh1_ifinfo)
-		batadv_neigh_ifinfo_free_ref(neigh1_ifinfo);
-	if (neigh2_ifinfo)
-		batadv_neigh_ifinfo_free_ref(neigh2_ifinfo);
+	ret = batadv_iv_ogm_neigh_diff(neigh1, if_outgoing1, neigh2,
+				       if_outgoing2, &diff);
+	if (ret)
+		return false;
 
-	return ret;
+	return diff > -BATADV_TQ_SIMILARITY_THRESHOLD;
 }
 
 static struct batadv_algo_ops batadv_batman_iv __read_mostly = {
-- 
2.1.3


  parent reply	other threads:[~2014-12-26 11:41 UTC|newest]

Thread overview: 67+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-12-26 11:41 [B.A.T.M.A.N.] [PATCH v2 00/26] batman-adv: Cleanups Markus Pargmann
2014-12-26 11:41 ` [B.A.T.M.A.N.] [PATCH v2 01/26] batman-adv: debugfs, avoid compiling for !DEBUG_FS Markus Pargmann
2015-01-11 10:32   ` Marek Lindner
2014-12-26 11:41 ` [B.A.T.M.A.N.] [PATCH v2 02/26] batman-adv: Separate logging header Markus Pargmann
2014-12-28  2:33   ` Marek Lindner
2014-12-28 11:08     ` Markus Pargmann
2014-12-26 11:41 ` Markus Pargmann [this message]
2015-01-11 10:38   ` [B.A.T.M.A.N.] [PATCH v2 03/26] batman-adv: iv_ogm, Reduce code duplication Marek Lindner
2014-12-26 11:41 ` [B.A.T.M.A.N.] [PATCH v2 04/26] batman-adv: iv_ogm, divide and round for ring buffer avg Markus Pargmann
2015-01-11 12:32   ` Marek Lindner
2015-01-11 12:42     ` Sven Eckelmann
2015-01-12 15:56   ` Simon Wunderlich
2014-12-26 11:41 ` [B.A.T.M.A.N.] [PATCH v2 05/26] batman-adv: init, Add some error handling Markus Pargmann
2015-01-11 12:38   ` Marek Lindner
2015-01-14 15:24     ` Markus Pargmann
2014-12-26 11:41 ` [B.A.T.M.A.N.] [PATCH v2 06/26] batman-adv: tvlv realloc, move error handling into if block Markus Pargmann
2015-01-11 12:40   ` Marek Lindner
2014-12-26 11:41 ` [B.A.T.M.A.N.] [PATCH v2 07/26] batman-adv: split tvlv into a seperate file Markus Pargmann
2014-12-26 11:41 ` [B.A.T.M.A.N.] [PATCH v2 08/26] batman-adv: Makefile, Sort alphabetically Markus Pargmann
2015-01-11 12:44   ` Marek Lindner
2014-12-26 11:41 ` [B.A.T.M.A.N.] [PATCH v2 09/26] batman-adv: iv_ogm_iface_enable, direct return values Markus Pargmann
2015-01-11 12:46   ` Marek Lindner
2014-12-26 11:41 ` [B.A.T.M.A.N.] [PATCH v2 10/26] batman-adv: iv_ogm_aggr_packet, bool return value Markus Pargmann
2015-01-11 12:48   ` Marek Lindner
2014-12-26 11:41 ` [B.A.T.M.A.N.] [PATCH v2 11/26] batman-adv: iv_ogm_send_to_if, declare char* as const Markus Pargmann
2015-02-18  9:14   ` Marek Lindner
2014-12-26 11:41 ` [B.A.T.M.A.N.] [PATCH v2 12/26] batman-adv: iv_ogm_can_aggregate, code readability Markus Pargmann
2015-02-19 16:20   ` Marek Lindner
2014-12-26 11:41 ` [B.A.T.M.A.N.] [PATCH v2 13/26] batman-adv: iv_ogm_orig_update, remove unnecessary brackets Markus Pargmann
2015-02-20 11:53   ` Marek Lindner
2014-12-26 11:41 ` [B.A.T.M.A.N.] [PATCH v2 14/26] batman-adv: iv_ogm_aggregate_new, simplify error handling Markus Pargmann
2015-02-23 16:50   ` Marek Lindner
2014-12-26 11:41 ` [B.A.T.M.A.N.] [PATCH v2 15/26] batman-adv: iv_ogm_queue_add, Simplify expressions Markus Pargmann
2015-02-24 21:20   ` Marek Lindner
2014-12-26 11:41 ` [B.A.T.M.A.N.] [PATCH v2 16/26] batman-adv: iv_ogm_orig_update, style, add missin brackets Markus Pargmann
2015-02-27 18:42   ` Marek Lindner
2014-12-26 11:41 ` [B.A.T.M.A.N.] [PATCH v2 17/26] batman-adv: iv_ogm, Fix dup_status comment Markus Pargmann
2015-02-27 18:43   ` Marek Lindner
2014-12-26 11:41 ` [B.A.T.M.A.N.] [PATCH v2 18/26] batman-adv: iv_ogm, fix coding style Markus Pargmann
2015-02-28 15:59   ` Marek Lindner
2014-12-26 11:41 ` [B.A.T.M.A.N.] [PATCH v2 19/26] batman-adv: iv_ogm, fix comment function name Markus Pargmann
2015-03-01  9:00   ` Marek Lindner
2014-12-26 11:41 ` [B.A.T.M.A.N.] [PATCH v2 20/26] batman-adv: types, Fix comment on bcast_own Markus Pargmann
2015-03-09  0:28   ` Marek Lindner
2014-12-26 11:41 ` [B.A.T.M.A.N.] [PATCH v2 21/26] batman-adv: main, Convert is_my_mac() to bool Markus Pargmann
2015-03-11  9:45   ` Marek Lindner
2014-12-26 11:41 ` [B.A.T.M.A.N.] [PATCH v2 22/26] batman-adv: main, batadv_compare_eth return bool Markus Pargmann
2015-03-11  9:48   ` Marek Lindner
2014-12-26 11:41 ` [B.A.T.M.A.N.] [PATCH v2 23/26] batman-adv: Remove unnecessary ret variable Markus Pargmann
2015-03-13  6:04   ` Marek Lindner
2014-12-26 11:41 ` [B.A.T.M.A.N.] [PATCH v2 24/26] batman-adv: Remove unnecessary ret variable in algo_register Markus Pargmann
2015-03-13  6:06   ` Marek Lindner
2014-12-26 11:41 ` [B.A.T.M.A.N.] [PATCH v2 25/26] batman-adv: packet.h, add some missing includes Markus Pargmann
2014-12-27 14:30   ` Antonio Quartulli
2014-12-27 17:03     ` Markus Pargmann
2014-12-31 17:55       ` Antonio Quartulli
2015-01-14 16:27         ` Markus Pargmann
2014-12-28  2:35   ` Marek Lindner
2014-12-28 11:11     ` Markus Pargmann
2015-03-21 22:36   ` Sven Eckelmann
2015-03-24 11:10     ` Markus Pargmann
2014-12-26 11:41 ` [B.A.T.M.A.N.] [PATCH v2 26/26] batman-adv: types.h, add missing include Markus Pargmann
2014-12-28  2:35   ` Marek Lindner
2015-03-22  0:52 ` [B.A.T.M.A.N.] [PATCH v2 00/26] batman-adv: Cleanups Sven Eckelmann
2015-03-22  1:34   ` Sven Eckelmann
2015-03-24 11:41     ` Markus Pargmann
2015-03-24 11:47       ` Sven Eckelmann

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=1419594103-10928-4-git-send-email-mpa@pengutronix.de \
    --to=mpa@pengutronix.de \
    --cc=antonio@meshcoding.com \
    --cc=b.a.t.m.a.n@lists.open-mesh.org \
    --cc=mareklindner@neomailbox.ch \
    --cc=sven@narfation.org \
    --cc=sw@simonwunderlich.de \
    /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;
as well as URLs for NNTP newsgroup(s).