Skip to content

Commit 162bd64

Browse files
T-Xordex
authored andcommitted
batman-adv: ELP - creating neighbor structures
Initially developed by Linus during a 6 months trainee study period in Ascom (Switzerland) AG. Signed-off-by: Linus Luessing <linus.luessing@web.de> Signed-off-by: Marek Lindner <mareklindner@neomailbox.ch> Signed-off-by: Antonio Quartulli <antonio@open-mesh.com>
1 parent d6f94d9 commit 162bd64

File tree

5 files changed

+214
-1
lines changed

5 files changed

+214
-1
lines changed

net/batman-adv/bat_v.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <linux/init.h>
2323

2424
#include "bat_v_elp.h"
25+
#include "packet.h"
2526

2627
static int batadv_v_iface_enable(struct batadv_hard_iface *hard_iface)
2728
{
@@ -42,6 +43,12 @@ static void batadv_v_primary_iface_set(struct batadv_hard_iface *hard_iface)
4243
batadv_v_elp_primary_iface_set(hard_iface);
4344
}
4445

46+
static void
47+
batadv_v_hardif_neigh_init(struct batadv_hardif_neigh_node *hardif_neigh)
48+
{
49+
ewma_throughput_init(&hardif_neigh->bat_v.throughput);
50+
}
51+
4552
static void batadv_v_ogm_schedule(struct batadv_hard_iface *hard_iface)
4653
{
4754
}
@@ -56,6 +63,7 @@ static struct batadv_algo_ops batadv_batman_v __read_mostly = {
5663
.bat_iface_disable = batadv_v_iface_disable,
5764
.bat_iface_update_mac = batadv_v_iface_update_mac,
5865
.bat_primary_iface_set = batadv_v_primary_iface_set,
66+
.bat_hardif_neigh_init = batadv_v_hardif_neigh_init,
5967
.bat_ogm_emit = batadv_v_ogm_emit,
6068
.bat_ogm_schedule = batadv_v_ogm_schedule,
6169
};
@@ -70,5 +78,18 @@ static struct batadv_algo_ops batadv_batman_v __read_mostly = {
7078
*/
7179
int __init batadv_v_init(void)
7280
{
73-
return batadv_algo_register(&batadv_batman_v);
81+
int ret;
82+
83+
/* B.A.T.M.A.N. V echo location protocol packet */
84+
ret = batadv_recv_handler_register(BATADV_ELP,
85+
batadv_v_elp_packet_recv);
86+
if (ret < 0)
87+
return ret;
88+
89+
ret = batadv_algo_register(&batadv_batman_v);
90+
91+
if (ret < 0)
92+
batadv_recv_handler_unregister(BATADV_ELP);
93+
94+
return ret;
7495
}

net/batman-adv/bat_v_elp.c

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@
3838

3939
#include "bat_algo.h"
4040
#include "hard-interface.h"
41+
#include "hash.h"
42+
#include "originator.h"
4143
#include "packet.h"
44+
#include "routing.h"
4245
#include "send.h"
4346

4447
/**
@@ -191,3 +194,152 @@ void batadv_v_elp_primary_iface_set(struct batadv_hard_iface *primary_iface)
191194
}
192195
rcu_read_unlock();
193196
}
197+
198+
/**
199+
* batadv_v_ogm_orig_get - retrieve and possibly create an originator node
200+
* @bat_priv: the bat priv with all the soft interface information
201+
* @addr: the address of the originator
202+
*
203+
* Return: the orig_node corresponding to the specified address. If such object
204+
* does not exist it is allocated here. In case of allocation failure returns
205+
* NULL.
206+
*/
207+
static struct batadv_orig_node *
208+
batadv_v_ogm_orig_get(struct batadv_priv *bat_priv,
209+
const u8 *addr)
210+
{
211+
struct batadv_orig_node *orig_node;
212+
int hash_added;
213+
214+
orig_node = batadv_orig_hash_find(bat_priv, addr);
215+
if (orig_node)
216+
return orig_node;
217+
218+
orig_node = batadv_orig_node_new(bat_priv, addr);
219+
if (!orig_node)
220+
return NULL;
221+
222+
hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig,
223+
batadv_choose_orig, orig_node,
224+
&orig_node->hash_entry);
225+
if (hash_added != 0) {
226+
/* orig_node->refcounter is initialised to 2 by
227+
* batadv_orig_node_new()
228+
*/
229+
batadv_orig_node_put(orig_node);
230+
batadv_orig_node_put(orig_node);
231+
orig_node = NULL;
232+
}
233+
234+
return orig_node;
235+
}
236+
237+
/**
238+
* batadv_v_elp_neigh_update - update an ELP neighbour node
239+
* @bat_priv: the bat priv with all the soft interface information
240+
* @neigh_addr: the neighbour interface address
241+
* @if_incoming: the interface the packet was received through
242+
* @elp_packet: the received ELP packet
243+
*
244+
* Updates the ELP neighbour node state with the data received within the new
245+
* ELP packet.
246+
*/
247+
static void batadv_v_elp_neigh_update(struct batadv_priv *bat_priv,
248+
u8 *neigh_addr,
249+
struct batadv_hard_iface *if_incoming,
250+
struct batadv_elp_packet *elp_packet)
251+
252+
{
253+
struct batadv_neigh_node *neigh;
254+
struct batadv_orig_node *orig_neigh;
255+
struct batadv_hardif_neigh_node *hardif_neigh;
256+
s32 seqno_diff;
257+
s32 elp_latest_seqno;
258+
259+
orig_neigh = batadv_v_ogm_orig_get(bat_priv, elp_packet->orig);
260+
if (!orig_neigh)
261+
return;
262+
263+
neigh = batadv_neigh_node_new(orig_neigh, if_incoming, neigh_addr);
264+
if (!neigh)
265+
goto orig_free;
266+
267+
hardif_neigh = batadv_hardif_neigh_get(if_incoming, neigh_addr);
268+
if (!hardif_neigh)
269+
goto neigh_free;
270+
271+
elp_latest_seqno = hardif_neigh->bat_v.elp_latest_seqno;
272+
seqno_diff = ntohl(elp_packet->seqno) - elp_latest_seqno;
273+
274+
/* known or older sequence numbers are ignored. However always adopt
275+
* if the router seems to have been restarted.
276+
*/
277+
if (seqno_diff < 1 && seqno_diff > -BATADV_ELP_MAX_AGE)
278+
goto hardif_free;
279+
280+
neigh->last_seen = jiffies;
281+
hardif_neigh->last_seen = jiffies;
282+
hardif_neigh->bat_v.elp_latest_seqno = ntohl(elp_packet->seqno);
283+
hardif_neigh->bat_v.elp_interval = ntohl(elp_packet->elp_interval);
284+
285+
hardif_free:
286+
if (hardif_neigh)
287+
batadv_hardif_neigh_put(hardif_neigh);
288+
neigh_free:
289+
if (neigh)
290+
batadv_neigh_node_put(neigh);
291+
orig_free:
292+
if (orig_neigh)
293+
batadv_orig_node_put(orig_neigh);
294+
}
295+
296+
/**
297+
* batadv_v_elp_packet_recv - main ELP packet handler
298+
* @skb: the received packet
299+
* @if_incoming: the interface this packet was received through
300+
*
301+
* Return: NET_RX_SUCCESS and consumes the skb if the packet was peoperly
302+
* processed or NET_RX_DROP in case of failure.
303+
*/
304+
int batadv_v_elp_packet_recv(struct sk_buff *skb,
305+
struct batadv_hard_iface *if_incoming)
306+
{
307+
struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
308+
struct batadv_elp_packet *elp_packet;
309+
struct batadv_hard_iface *primary_if;
310+
struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb);
311+
bool ret;
312+
313+
ret = batadv_check_management_packet(skb, if_incoming, BATADV_ELP_HLEN);
314+
if (!ret)
315+
return NET_RX_DROP;
316+
317+
if (batadv_is_my_mac(bat_priv, ethhdr->h_source))
318+
return NET_RX_DROP;
319+
320+
/* did we receive a B.A.T.M.A.N. V ELP packet on an interface
321+
* that does not have B.A.T.M.A.N. V ELP enabled ?
322+
*/
323+
if (strcmp(bat_priv->bat_algo_ops->name, "BATMAN_V") != 0)
324+
return NET_RX_DROP;
325+
326+
elp_packet = (struct batadv_elp_packet *)skb->data;
327+
328+
batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
329+
"Received ELP packet from %pM seqno %u ORIG: %pM\n",
330+
ethhdr->h_source, ntohl(elp_packet->seqno),
331+
elp_packet->orig);
332+
333+
primary_if = batadv_primary_if_get_selected(bat_priv);
334+
if (!primary_if)
335+
goto out;
336+
337+
batadv_v_elp_neigh_update(bat_priv, ethhdr->h_source, if_incoming,
338+
elp_packet);
339+
340+
out:
341+
if (primary_if)
342+
batadv_hardif_put(primary_if);
343+
consume_skb(skb);
344+
return NET_RX_SUCCESS;
345+
}

net/batman-adv/bat_v_elp.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@
2020
#ifndef _NET_BATMAN_ADV_BAT_V_ELP_H_
2121
#define _NET_BATMAN_ADV_BAT_V_ELP_H_
2222

23+
struct sk_buff;
24+
2325
int batadv_v_elp_iface_enable(struct batadv_hard_iface *hard_iface);
2426
void batadv_v_elp_iface_disable(struct batadv_hard_iface *hard_iface);
2527
void batadv_v_elp_primary_iface_set(struct batadv_hard_iface *primary_iface);
28+
int batadv_v_elp_packet_recv(struct sk_buff *skb,
29+
struct batadv_hard_iface *if_incoming);
2630

2731
#endif /* _NET_BATMAN_ADV_BAT_V_ELP_H_ */

net/batman-adv/main.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@
6060
#define BATADV_TQ_LOCAL_BIDRECT_RECV_MINIMUM 1
6161
#define BATADV_TQ_TOTAL_BIDRECT_LIMIT 1
6262

63+
/* B.A.T.M.A.N. V */
64+
#define BATADV_ELP_MAX_AGE 64
65+
6366
/* number of OGMs sent with the last tt diff */
6467
#define BATADV_TT_OGM_APPEND_MAX 3
6568

net/batman-adv/types.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#error only "main.h" can be included directly
2323
#endif
2424

25+
#include <linux/average.h>
2526
#include <linux/bitops.h>
2627
#include <linux/compiler.h>
2728
#include <linux/if_ether.h>
@@ -364,12 +365,28 @@ struct batadv_gw_node {
364365
struct rcu_head rcu;
365366
};
366367

368+
DECLARE_EWMA(throughput, 1024, 8)
369+
370+
/**
371+
* struct batadv_hardif_neigh_node_bat_v - B.A.T.M.A.N. V private neighbor
372+
* information
373+
* @throughput: ewma link throughput towards this neighbor
374+
* @elp_interval: time interval between two ELP transmissions
375+
* @elp_latest_seqno: latest and best known ELP sequence number
376+
*/
377+
struct batadv_hardif_neigh_node_bat_v {
378+
struct ewma_throughput throughput;
379+
u32 elp_interval;
380+
u32 elp_latest_seqno;
381+
};
382+
367383
/**
368384
* struct batadv_hardif_neigh_node - unique neighbor per hard-interface
369385
* @list: list node for batadv_hard_iface::neigh_list
370386
* @addr: the MAC address of the neighboring interface
371387
* @if_incoming: pointer to incoming hard-interface
372388
* @last_seen: when last packet via this neighbor was received
389+
* @bat_v: B.A.T.M.A.N. V private data
373390
* @refcount: number of contexts the object is used
374391
* @rcu: struct used for freeing in a RCU-safe manner
375392
*/
@@ -378,6 +395,9 @@ struct batadv_hardif_neigh_node {
378395
u8 addr[ETH_ALEN];
379396
struct batadv_hard_iface *if_incoming;
380397
unsigned long last_seen;
398+
#ifdef CONFIG_BATMAN_ADV_BATMAN_V
399+
struct batadv_hardif_neigh_node_bat_v bat_v;
400+
#endif
381401
struct kref refcount;
382402
struct rcu_head rcu;
383403
};
@@ -424,11 +444,21 @@ struct batadv_neigh_ifinfo_bat_iv {
424444
u8 real_packet_count;
425445
};
426446

447+
/**
448+
* struct batadv_neigh_ifinfo_bat_v - neighbor information per outgoing
449+
* interface for B.A.T.M.A.N. V
450+
* @throughput: last throughput metric received from originator via this neigh
451+
*/
452+
struct batadv_neigh_ifinfo_bat_v {
453+
u32 throughput;
454+
};
455+
427456
/**
428457
* struct batadv_neigh_ifinfo - neighbor information per outgoing interface
429458
* @list: list node for batadv_neigh_node::ifinfo_list
430459
* @if_outgoing: pointer to outgoing hard-interface
431460
* @bat_iv: B.A.T.M.A.N. IV private structure
461+
* @bat_v: B.A.T.M.A.N. V private data
432462
* @last_ttl: last received ttl from this neigh node
433463
* @refcount: number of contexts the object is used
434464
* @rcu: struct used for freeing in a RCU-safe manner
@@ -437,6 +467,9 @@ struct batadv_neigh_ifinfo {
437467
struct hlist_node list;
438468
struct batadv_hard_iface *if_outgoing;
439469
struct batadv_neigh_ifinfo_bat_iv bat_iv;
470+
#ifdef CONFIG_BATMAN_ADV_BATMAN_V
471+
struct batadv_neigh_ifinfo_bat_v bat_v;
472+
#endif
440473
u8 last_ttl;
441474
struct kref refcount;
442475
struct rcu_head rcu;

0 commit comments

Comments
 (0)