Skip to contentSkip to conversation

batman-adv: Replace gw_reselect 64 bit div with shift

B.A.T.M.A.N. Applied Sven Eckelmann 3 participants 3 messages Jun 22, 2015 – Jul 10, 2015

lore
Share & tools
lore
#0 lore raw

The 64-bit gw_factor is divided by BATADV_TQ_LOCAL_WINDOW_SIZE ** 2 * 64. But the rest of the calculation has nothing to do with the tq window size end therefore the calculation is just (tmp_gw_factor / (64 ** 3)).

The problem with 64 bit div is that it doesn't work on systems without native 64 bit div support. It has to be emulated using do_div or div_u64. The change in f63c54bba31d ("batman-adv: Avoid u32 overflow during gateway select") only compiled on such systems because the compiler converted the div to a (tmp_gw_factor >> 18). Making this explicit avoids having build problems in the future when BATADV_TQ_LOCAL_WINDOW_SIZE is changed in such a way that (BATADV_TQ_LOCAL_WINDOW_SIZE ** 2 * 64) is not a power of two.

---
 gateway_client.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
gateway_client.c +1 -5
index 3f32357..0f8d06b 100644
@@ -134,14 +134,10 @@ batadv_gw_get_best_gw_node(struct batadv_priv *bat_priv)
 	struct batadv_neigh_ifinfo *router_ifinfo;
 	struct batadv_gw_node *gw_node, *curr_gw = NULL;
 	uint64_t max_gw_factor = 0, tmp_gw_factor = 0;
-	uint32_t gw_divisor;
 	uint8_t max_tq = 0;
 	uint8_t tq_avg;
 	struct batadv_orig_node *orig_node;
 
-	gw_divisor = BATADV_TQ_LOCAL_WINDOW_SIZE * BATADV_TQ_LOCAL_WINDOW_SIZE;
-	gw_divisor *= 64;
-
 	rcu_read_lock();
 	hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.list, list) {
 		if (gw_node->deleted)
@@ -167,7 +163,7 @@ batadv_gw_get_best_gw_node(struct batadv_priv *bat_priv)
 			tmp_gw_factor = tq_avg * tq_avg;
 			tmp_gw_factor *= gw_node->bandwidth_down;
 			tmp_gw_factor *= 100 * 100;
-			tmp_gw_factor /= gw_divisor;
+			tmp_gw_factor >>= 18;
 
 			if ((tmp_gw_factor > max_gw_factor) ||
 			    ((tmp_gw_factor == max_gw_factor) &&
Signature · 1 line
2.1.4
#1 lore raw

gateway_client.c batadv_gw_get_best_gw_node(struct batadv_priv *bat_priv) · line 143

Quoted 2 lines · Sven Eckelmann
gateway_client.c +0 -2
-	gw_divisor = BATADV_TQ_LOCAL_WINDOW_SIZE * BATADV_TQ_LOCAL_WINDOW_SIZE;
-	gw_divisor *= 64;

Marek can you comment on why this constant was used in this computation? To me it looks like we only needed a "good enough number" and not a real reference to the local window size.

The remaining change looks good to me.

If you merge this patch I'll then squash it with the uint64_t change that we already have and I will re-send the pull request to David.

Thanks,

Quoted 14 lines
gateway_client.c +1 -2
-
 	rcu_read_lock();
 	hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.list, list) {
 		if (gw_node->deleted)
@@ -167,7 +163,7 @@ batadv_gw_get_best_gw_node(struct batadv_priv *bat_priv)
 			tmp_gw_factor = tq_avg * tq_avg;
 			tmp_gw_factor *= gw_node->bandwidth_down;
 			tmp_gw_factor *= 100 * 100;
-			tmp_gw_factor /= gw_divisor;
+			tmp_gw_factor >>= 18;
 
 			if ((tmp_gw_factor > max_gw_factor) ||
 			    ((tmp_gw_factor == max_gw_factor) &&
Signature · 1 line
Antonio Quartulli
#2 lore raw
In reply to Sven Eckelmann
Quoted 12 lines · On Monday, June 22, 2015 09:13:23 Sven Eckelmann wrote:

The 64-bit gw_factor is divided by BATADV_TQ_LOCAL_WINDOW_SIZE ** 2 * 64. But the rest of the calculation has nothing to do with the tq window size end therefore the calculation is just (tmp_gw_factor / (64 ** 3)).

The problem with 64 bit div is that it doesn't work on systems without native 64 bit div support. It has to be emulated using do_div or div_u64. The change in f63c54bba31d ("batman-adv: Avoid u32 overflow during gateway select") only compiled on such systems because the compiler converted the div to a (tmp_gw_factor >> 18). Making this explicit avoids having build problems in the future when BATADV_TQ_LOCAL_WINDOW_SIZE is changed in such a way that (BATADV_TQ_LOCAL_WINDOW_SIZE ** 2 * 64) is not a power of two.

Quoted 4 lines
---
 gateway_client.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)

Applied in revision 013aab3.

Thanks,
Marek