Skip to content

Commit 2265c14

Browse files
Antonio Quartulliecsv
authored andcommitted
batman-adv: gateway election code refactoring
The gateway election mechanism has been a little revised. Now the gw_election is trigered by an atomic_t flag (gw_reselect) which is set to 1 in case of election needed, avoding to set curr_gw to NULL. Signed-off-by: Antonio Quartulli <ordex@autistici.org> Signed-off-by: Marek Lindner <lindner_marek@yahoo.de> Signed-off-by: Sven Eckelmann <sven@narfation.org>
1 parent c6bda68 commit 2265c14

3 files changed

Lines changed: 82 additions & 56 deletions

File tree

‎net/batman-adv/gateway_client.c‎

Lines changed: 80 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
*/
2121

2222
#include "main.h"
23+
#include "bat_sysfs.h"
2324
#include "gateway_client.h"
2425
#include "gateway_common.h"
2526
#include "hard-interface.h"
@@ -97,40 +98,19 @@ static void gw_select(struct bat_priv *bat_priv, struct gw_node *new_gw_node)
9798

9899
void gw_deselect(struct bat_priv *bat_priv)
99100
{
100-
gw_select(bat_priv, NULL);
101+
atomic_set(&bat_priv->gw_reselect, 1);
101102
}
102103

103-
void gw_election(struct bat_priv *bat_priv)
104+
static struct gw_node *gw_get_best_gw_node(struct bat_priv *bat_priv)
104105
{
105-
struct hlist_node *node;
106-
struct gw_node *gw_node, *curr_gw = NULL, *curr_gw_tmp = NULL;
107106
struct neigh_node *router;
108-
uint8_t max_tq = 0;
107+
struct hlist_node *node;
108+
struct gw_node *gw_node, *curr_gw = NULL;
109109
uint32_t max_gw_factor = 0, tmp_gw_factor = 0;
110+
uint8_t max_tq = 0;
110111
int down, up;
111112

112-
/**
113-
* The batman daemon checks here if we already passed a full originator
114-
* cycle in order to make sure we don't choose the first gateway we
115-
* hear about. This check is based on the daemon's uptime which we
116-
* don't have.
117-
**/
118-
if (atomic_read(&bat_priv->gw_mode) != GW_MODE_CLIENT)
119-
return;
120-
121-
curr_gw = gw_get_selected_gw_node(bat_priv);
122-
if (curr_gw)
123-
goto out;
124-
125113
rcu_read_lock();
126-
if (hlist_empty(&bat_priv->gw_list)) {
127-
bat_dbg(DBG_BATMAN, bat_priv,
128-
"Removing selected gateway - "
129-
"no gateway in range\n");
130-
gw_deselect(bat_priv);
131-
goto unlock;
132-
}
133-
134114
hlist_for_each_entry_rcu(gw_node, node, &bat_priv->gw_list, list) {
135115
if (gw_node->deleted)
136116
continue;
@@ -139,6 +119,9 @@ void gw_election(struct bat_priv *bat_priv)
139119
if (!router)
140120
continue;
141121

122+
if (!atomic_inc_not_zero(&gw_node->refcount))
123+
goto next;
124+
142125
switch (atomic_read(&bat_priv->gw_sel_class)) {
143126
case 1: /* fast connection */
144127
gw_bandwidth_to_kbit(gw_node->orig_node->gw_flags,
@@ -151,8 +134,12 @@ void gw_election(struct bat_priv *bat_priv)
151134

152135
if ((tmp_gw_factor > max_gw_factor) ||
153136
((tmp_gw_factor == max_gw_factor) &&
154-
(router->tq_avg > max_tq)))
155-
curr_gw_tmp = gw_node;
137+
(router->tq_avg > max_tq))) {
138+
if (curr_gw)
139+
gw_node_free_ref(curr_gw);
140+
curr_gw = gw_node;
141+
atomic_inc(&curr_gw->refcount);
142+
}
156143
break;
157144

158145
default: /**
@@ -163,8 +150,12 @@ void gw_election(struct bat_priv *bat_priv)
163150
* soon as a better gateway appears which has
164151
* $routing_class more tq points)
165152
**/
166-
if (router->tq_avg > max_tq)
167-
curr_gw_tmp = gw_node;
153+
if (router->tq_avg > max_tq) {
154+
if (curr_gw)
155+
gw_node_free_ref(curr_gw);
156+
curr_gw = gw_node;
157+
atomic_inc(&curr_gw->refcount);
158+
}
168159
break;
169160
}
170161

@@ -174,42 +165,75 @@ void gw_election(struct bat_priv *bat_priv)
174165
if (tmp_gw_factor > max_gw_factor)
175166
max_gw_factor = tmp_gw_factor;
176167

168+
gw_node_free_ref(gw_node);
169+
170+
next:
177171
neigh_node_free_ref(router);
178172
}
173+
rcu_read_unlock();
179174

180-
if (curr_gw != curr_gw_tmp) {
181-
router = orig_node_get_router(curr_gw_tmp->orig_node);
182-
if (!router)
183-
goto unlock;
175+
return curr_gw;
176+
}
184177

185-
if ((curr_gw) && (!curr_gw_tmp))
186-
bat_dbg(DBG_BATMAN, bat_priv,
187-
"Removing selected gateway - "
188-
"no gateway in range\n");
189-
else if ((!curr_gw) && (curr_gw_tmp))
190-
bat_dbg(DBG_BATMAN, bat_priv,
191-
"Adding route to gateway %pM "
192-
"(gw_flags: %i, tq: %i)\n",
193-
curr_gw_tmp->orig_node->orig,
194-
curr_gw_tmp->orig_node->gw_flags,
195-
router->tq_avg);
196-
else
197-
bat_dbg(DBG_BATMAN, bat_priv,
198-
"Changing route to gateway %pM "
199-
"(gw_flags: %i, tq: %i)\n",
200-
curr_gw_tmp->orig_node->orig,
201-
curr_gw_tmp->orig_node->gw_flags,
202-
router->tq_avg);
178+
void gw_election(struct bat_priv *bat_priv)
179+
{
180+
struct gw_node *curr_gw = NULL, *next_gw = NULL;
181+
struct neigh_node *router = NULL;
203182

204-
neigh_node_free_ref(router);
205-
gw_select(bat_priv, curr_gw_tmp);
183+
/**
184+
* The batman daemon checks here if we already passed a full originator
185+
* cycle in order to make sure we don't choose the first gateway we
186+
* hear about. This check is based on the daemon's uptime which we
187+
* don't have.
188+
**/
189+
if (atomic_read(&bat_priv->gw_mode) != GW_MODE_CLIENT)
190+
goto out;
191+
192+
if (!atomic_dec_not_zero(&bat_priv->gw_reselect))
193+
goto out;
194+
195+
curr_gw = gw_get_selected_gw_node(bat_priv);
196+
197+
next_gw = gw_get_best_gw_node(bat_priv);
198+
199+
if (curr_gw == next_gw)
200+
goto out;
201+
202+
if (next_gw) {
203+
router = orig_node_get_router(next_gw->orig_node);
204+
if (!router) {
205+
gw_deselect(bat_priv);
206+
goto out;
207+
}
206208
}
207209

208-
unlock:
209-
rcu_read_unlock();
210+
if ((curr_gw) && (!next_gw)) {
211+
bat_dbg(DBG_BATMAN, bat_priv,
212+
"Removing selected gateway - no gateway in range\n");
213+
} else if ((!curr_gw) && (next_gw)) {
214+
bat_dbg(DBG_BATMAN, bat_priv,
215+
"Adding route to gateway %pM (gw_flags: %i, tq: %i)\n",
216+
next_gw->orig_node->orig,
217+
next_gw->orig_node->gw_flags,
218+
router->tq_avg);
219+
} else {
220+
bat_dbg(DBG_BATMAN, bat_priv,
221+
"Changing route to gateway %pM "
222+
"(gw_flags: %i, tq: %i)\n",
223+
next_gw->orig_node->orig,
224+
next_gw->orig_node->gw_flags,
225+
router->tq_avg);
226+
}
227+
228+
gw_select(bat_priv, next_gw);
229+
210230
out:
211231
if (curr_gw)
212232
gw_node_free_ref(curr_gw);
233+
if (next_gw)
234+
gw_node_free_ref(next_gw);
235+
if (router)
236+
neigh_node_free_ref(router);
213237
}
214238

215239
void gw_check_election(struct bat_priv *bat_priv, struct orig_node *orig_node)

‎net/batman-adv/main.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ int mesh_init(struct net_device *soft_iface)
113113
if (vis_init(bat_priv) < 1)
114114
goto err;
115115

116+
atomic_set(&bat_priv->gw_reselect, 0);
116117
atomic_set(&bat_priv->mesh_state, MESH_ACTIVE);
117118
goto end;
118119

‎net/batman-adv/types.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ struct bat_priv {
201201
struct delayed_work orig_work;
202202
struct delayed_work vis_work;
203203
struct gw_node __rcu *curr_gw; /* rcu protected pointer */
204+
atomic_t gw_reselect;
204205
struct hard_iface __rcu *primary_if; /* rcu protected pointer */
205206
struct vis_info *my_vis_info;
206207
};

0 commit comments

Comments
 (0)