From: Simon Wunderlich <sw@simonwunderlich.de>
To: davem@davemloft.net
Cc: netdev@vger.kernel.org, b.a.t.m.a.n@lists.open-mesh.org,
Markus Pargmann <mpa@pengutronix.de>,
Sven Eckelmann <sven@narfation.org>,
Marek Lindner <mareklindner@neomailbox.ch>,
Simon Wunderlich <sw@simonwunderlich.de>
Subject: [PATCH 13/19] batman-adv: iv_ogm, Reduce code duplication
Date: Fri, 12 Aug 2016 10:56:49 +0200 [thread overview]
Message-ID: <1470992215-11009-14-git-send-email-sw@simonwunderlich.de> (raw)
In-Reply-To: <1470992215-11009-1-git-send-email-sw@simonwunderlich.de>
From: Markus Pargmann <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 separate 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>
[sven@narfation.org: rebased on current version, initialize return variable
in batadv_iv_ogm_neigh_diff, add kerneldoc, convert to bool return type]
Signed-off-by: Sven Eckelmann <sven@narfation.org>
Signed-off-by: Marek Lindner <mareklindner@neomailbox.ch>
Signed-off-by: Simon Wunderlich <sw@simonwunderlich.de>
---
net/batman-adv/bat_iv_ogm.c | 77 +++++++++++++++++++++++++++------------------
1 file changed, 47 insertions(+), 30 deletions(-)
diff --git a/net/batman-adv/bat_iv_ogm.c b/net/batman-adv/bat_iv_ogm.c
index d04874f..57e0af9 100644
--- a/net/batman-adv/bat_iv_ogm.c
+++ b/net/batman-adv/bat_iv_ogm.c
@@ -2019,35 +2019,40 @@ static void batadv_iv_neigh_print(struct batadv_priv *bat_priv,
}
/**
- * batadv_iv_ogm_neigh_cmp - compare the metrics of two neighbors
+ * batadv_iv_ogm_neigh_diff - calculate tq difference 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
+ * @diff: pointer to integer receiving the calculated difference
*
- * Return: 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
+ * The content of *@diff is only valid when this function returns true.
+ * It is less, equal to or greater than 0 if the metric via neigh1 is lower,
+ * the same as or higher than the metric via neigh2
+ *
+ * Return: true when the difference could be calculated, false otherwise
*/
-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 bool 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;
u8 tq1, tq2;
- int diff;
+ bool ret = true;
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 = false;
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)
@@ -2055,6 +2060,32 @@ out:
if (neigh2_ifinfo)
batadv_neigh_ifinfo_put(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
+ *
+ * Return: 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)
+{
+ bool ret;
+ int diff;
+
+ ret = batadv_iv_ogm_neigh_diff(neigh1, if_outgoing1, neigh2,
+ if_outgoing2, &diff);
+ if (!ret)
+ return 0;
+
return diff;
}
@@ -2075,29 +2106,15 @@ batadv_iv_ogm_neigh_is_sob(struct batadv_neigh_node *neigh1,
struct batadv_neigh_node *neigh2,
struct batadv_hard_iface *if_outgoing2)
{
- struct batadv_neigh_ifinfo *neigh1_ifinfo, *neigh2_ifinfo;
- u8 tq1, tq2;
bool ret;
+ int diff;
- 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;
-
-out:
- if (neigh1_ifinfo)
- batadv_neigh_ifinfo_put(neigh1_ifinfo);
- if (neigh2_ifinfo)
- batadv_neigh_ifinfo_put(neigh2_ifinfo);
+ ret = batadv_iv_ogm_neigh_diff(neigh1, if_outgoing1, neigh2,
+ if_outgoing2, &diff);
+ if (!ret)
+ return false;
+ ret = diff > -BATADV_TQ_SIMILARITY_THRESHOLD;
return ret;
}
--
2.8.1
next prev parent reply other threads:[~2016-08-12 8:57 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-08-12 8:56 [PATCH 00/19] pull request for net-next: batman-adv 2016-08-12 Simon Wunderlich
2016-08-12 8:56 ` [PATCH 02/19] batman-adv: Document optional batadv_algo_ops Simon Wunderlich
2016-08-12 8:56 ` [PATCH 05/19] batman-adv: Modify mesh_iface outside sysfs context Simon Wunderlich
[not found] ` <1470992215-11009-1-git-send-email-sw-2YrNx6rUIHYiY0qSoAWiAoQuADTiUCJX@public.gmane.org>
2016-08-12 8:56 ` [PATCH 01/19] batman-adv: Start new development cycle Simon Wunderlich
2016-08-12 8:56 ` [PATCH 03/19] batman-adv: Define module rtnl link name Simon Wunderlich
2016-08-12 8:56 ` [PATCH 04/19] batman-adv: Use rtnl link in device creation example Simon Wunderlich
2016-08-12 8:56 ` [PATCH 06/19] batman-adv: Revert "postpone sysfs removal when unregistering" Simon Wunderlich
2016-08-12 8:56 ` [PATCH 07/19] batman-adv: Avoid sysfs name collision for netns moves Simon Wunderlich
2016-08-12 8:56 ` [PATCH 08/19] batman-adv: Remove unused primary_if and bat_priv variables Simon Wunderlich
2016-08-12 8:56 ` [PATCH 09/19] batman-adv: make the GW selection class algorithm specific Simon Wunderlich
2016-08-12 8:56 ` [PATCH 10/19] batman-adv: make GW election code protocol specific Simon Wunderlich
2016-08-12 8:56 ` [PATCH 11/19] batman-adv: B.A.T.M.A.N. V - implement GW selection logic Simon Wunderlich
2016-08-13 3:56 ` [PATCH 00/19] pull request for net-next: batman-adv 2016-08-12 David Miller
2016-08-12 8:56 ` [PATCH 12/19] batman-adv: disable sysfs knobs when GW-mode is not implemented Simon Wunderlich
2016-08-12 8:56 ` Simon Wunderlich [this message]
2016-08-12 8:56 ` [PATCH 14/19] batman-adv: fix boolreturn.cocci warnings Simon Wunderlich
2016-08-12 8:56 ` [PATCH 15/19] batman-adv: Introduce forward packet creation helper Simon Wunderlich
2016-08-12 8:56 ` [PATCH 16/19] batman-adv: use kmem_cache for translation table Simon Wunderlich
2016-08-12 8:56 ` [PATCH 17/19] batman-adv: Remove orig_node reference handling from send_skb_unicast Simon Wunderlich
2016-08-12 8:56 ` [PATCH 18/19] batman-adv: Use bitwise instead of arithmetic operator for flags Simon Wunderlich
2016-08-12 8:56 ` [PATCH 19/19] batman-adv: Fix consistency of update route messages Simon Wunderlich
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=1470992215-11009-14-git-send-email-sw@simonwunderlich.de \
--to=sw@simonwunderlich.de \
--cc=b.a.t.m.a.n@lists.open-mesh.org \
--cc=davem@davemloft.net \
--cc=mareklindner@neomailbox.ch \
--cc=mpa@pengutronix.de \
--cc=netdev@vger.kernel.org \
--cc=sven@narfation.org \
/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).