Commit 57b12502 authored by Markus Pargmann's avatar Markus Pargmann Committed by Simon Wunderlich

batman-adv: iv_ogm, Reduce code duplication

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: default avatarMarkus 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: default avatarSven Eckelmann <sven@narfation.org>
Signed-off-by: default avatarMarek Lindner <mareklindner@neomailbox.ch>
Signed-off-by: default avatarSimon Wunderlich <sw@simonwunderlich.de>
parent a8d8d1de
...@@ -2019,35 +2019,40 @@ static void batadv_iv_neigh_print(struct batadv_priv *bat_priv, ...@@ -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 * @neigh1: the first neighbor object of the comparison
* @if_outgoing1: outgoing interface for the first neighbor * @if_outgoing1: outgoing interface for the first neighbor
* @neigh2: the second neighbor object of the comparison * @neigh2: the second neighbor object of the comparison
* @if_outgoing2: outgoing interface for the second neighbor * @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 * The content of *@diff is only valid when this function returns true.
* lower, the same as or higher than the metric via neigh2 * 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, static bool batadv_iv_ogm_neigh_diff(struct batadv_neigh_node *neigh1,
struct batadv_hard_iface *if_outgoing1, struct batadv_hard_iface *if_outgoing1,
struct batadv_neigh_node *neigh2, struct batadv_neigh_node *neigh2,
struct batadv_hard_iface *if_outgoing2) struct batadv_hard_iface *if_outgoing2,
int *diff)
{ {
struct batadv_neigh_ifinfo *neigh1_ifinfo, *neigh2_ifinfo; struct batadv_neigh_ifinfo *neigh1_ifinfo, *neigh2_ifinfo;
u8 tq1, tq2; u8 tq1, tq2;
int diff; bool ret = true;
neigh1_ifinfo = batadv_neigh_ifinfo_get(neigh1, if_outgoing1); neigh1_ifinfo = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
neigh2_ifinfo = batadv_neigh_ifinfo_get(neigh2, if_outgoing2); neigh2_ifinfo = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
if (!neigh1_ifinfo || !neigh2_ifinfo) { if (!neigh1_ifinfo || !neigh2_ifinfo) {
diff = 0; ret = false;
goto out; goto out;
} }
tq1 = neigh1_ifinfo->bat_iv.tq_avg; tq1 = neigh1_ifinfo->bat_iv.tq_avg;
tq2 = neigh2_ifinfo->bat_iv.tq_avg; tq2 = neigh2_ifinfo->bat_iv.tq_avg;
diff = tq1 - tq2; *diff = (int)tq1 - (int)tq2;
out: out:
if (neigh1_ifinfo) if (neigh1_ifinfo)
...@@ -2055,6 +2060,32 @@ static int batadv_iv_ogm_neigh_cmp(struct batadv_neigh_node *neigh1, ...@@ -2055,6 +2060,32 @@ static int batadv_iv_ogm_neigh_cmp(struct batadv_neigh_node *neigh1,
if (neigh2_ifinfo) if (neigh2_ifinfo)
batadv_neigh_ifinfo_put(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; return diff;
} }
...@@ -2075,29 +2106,15 @@ batadv_iv_ogm_neigh_is_sob(struct batadv_neigh_node *neigh1, ...@@ -2075,29 +2106,15 @@ batadv_iv_ogm_neigh_is_sob(struct batadv_neigh_node *neigh1,
struct batadv_neigh_node *neigh2, struct batadv_neigh_node *neigh2,
struct batadv_hard_iface *if_outgoing2) struct batadv_hard_iface *if_outgoing2)
{ {
struct batadv_neigh_ifinfo *neigh1_ifinfo, *neigh2_ifinfo;
u8 tq1, tq2;
bool ret; bool ret;
int diff;
neigh1_ifinfo = batadv_neigh_ifinfo_get(neigh1, if_outgoing1); ret = batadv_iv_ogm_neigh_diff(neigh1, if_outgoing1, neigh2,
neigh2_ifinfo = batadv_neigh_ifinfo_get(neigh2, if_outgoing2); if_outgoing2, &diff);
if (!ret)
/* we can't say that the metric is better */ return false;
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 = diff > -BATADV_TQ_SIMILARITY_THRESHOLD;
return ret; return ret;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment