Skip to content

Commit 3111bee

Browse files
T-Xsimonwunderlich
authored andcommitted
batman-adv: Simple (re)broadcast avoidance
With this patch, (re)broadcasting on a specific interfaces is avoided: * No neighbor: There is no need to broadcast on an interface if there is no node behind it. * Single neighbor is source: If there is just one neighbor on an interface and if this neighbor is the one we actually got this broadcast packet from, then we do not need to echo it back. * Single neighbor is originator: If there is just one neighbor on an interface and if this neighbor is the originator of this broadcast packet, then we do not need to echo it back. Goodies for BATMAN V: ("Upgrade your BATMAN IV network to V now to get these for free!") Thanks to the split of OGMv1 into two packet types, OGMv2 and ELP that is, we can now apply the same optimizations stated above to OGMv2 packets, too. Furthermore, with BATMAN V, rebroadcasts can be reduced in certain multi interface cases, too, where BATMAN IV cannot. This is thanks to the removal of the "secondary interface originator" concept in BATMAN V. Signed-off-by: Linus Lüssing <linus.luessing@c0d3.blue> Signed-off-by: Sven Eckelmann <sven@narfation.org> Signed-off-by: Simon Wunderlich <sw@simonwunderlich.de>
1 parent cbebd36 commit 3111bee

9 files changed

Lines changed: 193 additions & 8 deletions

File tree

‎net/batman-adv/bat_v_ogm.c‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ static void batadv_v_ogm_send(struct work_struct *work)
140140
unsigned char *ogm_buff, *pkt_buff;
141141
int ogm_buff_len;
142142
u16 tvlv_len = 0;
143+
int ret;
143144

144145
bat_v = container_of(work, struct batadv_priv_bat_v, ogm_wq.work);
145146
bat_priv = container_of(bat_v, struct batadv_priv, bat_v);
@@ -182,6 +183,31 @@ static void batadv_v_ogm_send(struct work_struct *work)
182183
if (!kref_get_unless_zero(&hard_iface->refcount))
183184
continue;
184185

186+
ret = batadv_hardif_no_broadcast(hard_iface, NULL, NULL);
187+
if (ret) {
188+
char *type;
189+
190+
switch (ret) {
191+
case BATADV_HARDIF_BCAST_NORECIPIENT:
192+
type = "no neighbor";
193+
break;
194+
case BATADV_HARDIF_BCAST_DUPFWD:
195+
type = "single neighbor is source";
196+
break;
197+
case BATADV_HARDIF_BCAST_DUPORIG:
198+
type = "single neighbor is originator";
199+
break;
200+
default:
201+
type = "unknown";
202+
}
203+
204+
batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "OGM2 from ourselve on %s surpressed: %s\n",
205+
hard_iface->net_dev->name, type);
206+
207+
batadv_hardif_put(hard_iface);
208+
continue;
209+
}
210+
185211
batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
186212
"Sending own OGM2 packet (originator %pM, seqno %u, throughput %u, TTL %d) on interface %s [%pM]\n",
187213
ogm_packet->orig, ntohl(ogm_packet->seqno),
@@ -651,6 +677,7 @@ static void batadv_v_ogm_process(const struct sk_buff *skb, int ogm_offset,
651677
struct batadv_hard_iface *hard_iface;
652678
struct batadv_ogm2_packet *ogm_packet;
653679
u32 ogm_throughput, link_throughput, path_throughput;
680+
int ret;
654681

655682
ethhdr = eth_hdr(skb);
656683
ogm_packet = (struct batadv_ogm2_packet *)(skb->data + ogm_offset);
@@ -716,6 +743,35 @@ static void batadv_v_ogm_process(const struct sk_buff *skb, int ogm_offset,
716743
if (!kref_get_unless_zero(&hard_iface->refcount))
717744
continue;
718745

746+
ret = batadv_hardif_no_broadcast(hard_iface,
747+
ogm_packet->orig,
748+
hardif_neigh->orig);
749+
750+
if (ret) {
751+
char *type;
752+
753+
switch (ret) {
754+
case BATADV_HARDIF_BCAST_NORECIPIENT:
755+
type = "no neighbor";
756+
break;
757+
case BATADV_HARDIF_BCAST_DUPFWD:
758+
type = "single neighbor is source";
759+
break;
760+
case BATADV_HARDIF_BCAST_DUPORIG:
761+
type = "single neighbor is originator";
762+
break;
763+
default:
764+
type = "unknown";
765+
}
766+
767+
batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "OGM2 packet from %pM on %s surpressed: %s\n",
768+
ogm_packet->orig, hard_iface->net_dev->name,
769+
type);
770+
771+
batadv_hardif_put(hard_iface);
772+
continue;
773+
}
774+
719775
batadv_v_ogm_process_per_outif(bat_priv, ethhdr, ogm_packet,
720776
orig_node, neigh_node,
721777
if_incoming, hard_iface);

‎net/batman-adv/hard-interface.c‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,58 @@ bool batadv_is_wifi_netdev(struct net_device *net_device)
228228
return false;
229229
}
230230

231+
/**
232+
* batadv_hardif_no_broadcast - check whether (re)broadcast is necessary
233+
* @if_outgoing: the outgoing interface checked and considered for (re)broadcast
234+
* @orig_addr: the originator of this packet
235+
* @orig_neigh: originator address of the forwarder we just got the packet from
236+
* (NULL if we originated)
237+
*
238+
* Checks whether a packet needs to be (re)broadcasted on the given interface.
239+
*
240+
* Return:
241+
* BATADV_HARDIF_BCAST_NORECIPIENT: No neighbor on interface
242+
* BATADV_HARDIF_BCAST_DUPFWD: Just one neighbor, but it is the forwarder
243+
* BATADV_HARDIF_BCAST_DUPORIG: Just one neighbor, but it is the originator
244+
* BATADV_HARDIF_BCAST_OK: Several neighbors, must broadcast
245+
*/
246+
int batadv_hardif_no_broadcast(struct batadv_hard_iface *if_outgoing,
247+
u8 *orig_addr, u8 *orig_neigh)
248+
{
249+
struct batadv_hardif_neigh_node *hardif_neigh;
250+
struct hlist_node *first;
251+
int ret = BATADV_HARDIF_BCAST_OK;
252+
253+
rcu_read_lock();
254+
255+
/* 0 neighbors -> no (re)broadcast */
256+
first = rcu_dereference(hlist_first_rcu(&if_outgoing->neigh_list));
257+
if (!first) {
258+
ret = BATADV_HARDIF_BCAST_NORECIPIENT;
259+
goto out;
260+
}
261+
262+
/* >1 neighbors -> (re)brodcast */
263+
if (rcu_dereference(hlist_next_rcu(first)))
264+
goto out;
265+
266+
hardif_neigh = hlist_entry(first, struct batadv_hardif_neigh_node,
267+
list);
268+
269+
/* 1 neighbor, is the originator -> no rebroadcast */
270+
if (orig_addr && batadv_compare_eth(hardif_neigh->orig, orig_addr)) {
271+
ret = BATADV_HARDIF_BCAST_DUPORIG;
272+
/* 1 neighbor, is the one we received from -> no rebroadcast */
273+
} else if (orig_neigh &&
274+
batadv_compare_eth(hardif_neigh->orig, orig_neigh)) {
275+
ret = BATADV_HARDIF_BCAST_DUPFWD;
276+
}
277+
278+
out:
279+
rcu_read_unlock();
280+
return ret;
281+
}
282+
231283
static struct batadv_hard_iface *
232284
batadv_hardif_get_active(const struct net_device *soft_iface)
233285
{

‎net/batman-adv/hard-interface.h‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,20 @@ enum batadv_hard_if_state {
3939
BATADV_IF_I_WANT_YOU,
4040
};
4141

42+
/**
43+
* enum batadv_hard_if_bcast - broadcast avoidance options
44+
* @BATADV_HARDIF_BCAST_OK: Do broadcast on according hard interface
45+
* @BATADV_HARDIF_BCAST_NORECIPIENT: Broadcast not needed, there is no recipient
46+
* @BATADV_HARDIF_BCAST_DUPFWD: There is just the neighbor we got it from
47+
* @BATADV_HARDIF_BCAST_DUPORIG: There is just the originator
48+
*/
49+
enum batadv_hard_if_bcast {
50+
BATADV_HARDIF_BCAST_OK = 0,
51+
BATADV_HARDIF_BCAST_NORECIPIENT,
52+
BATADV_HARDIF_BCAST_DUPFWD,
53+
BATADV_HARDIF_BCAST_DUPORIG,
54+
};
55+
4256
/**
4357
* enum batadv_hard_if_cleanup - Cleanup modi for soft_iface after slave removal
4458
* @BATADV_IF_CLEANUP_KEEP: Don't automatically delete soft-interface
@@ -63,6 +77,8 @@ void batadv_hardif_remove_interfaces(void);
6377
int batadv_hardif_min_mtu(struct net_device *soft_iface);
6478
void batadv_update_min_mtu(struct net_device *soft_iface);
6579
void batadv_hardif_release(struct kref *ref);
80+
int batadv_hardif_no_broadcast(struct batadv_hard_iface *if_outgoing,
81+
u8 *orig_addr, u8 *orig_neigh);
6682

6783
/**
6884
* batadv_hardif_put - decrement the hard interface refcounter and possibly

‎net/batman-adv/originator.c‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -512,12 +512,14 @@ batadv_neigh_node_get(const struct batadv_orig_node *orig_node,
512512
* batadv_hardif_neigh_create - create a hardif neighbour node
513513
* @hard_iface: the interface this neighbour is connected to
514514
* @neigh_addr: the interface address of the neighbour to retrieve
515+
* @orig_node: originator object representing the neighbour
515516
*
516517
* Return: the hardif neighbour node if found or created or NULL otherwise.
517518
*/
518519
static struct batadv_hardif_neigh_node *
519520
batadv_hardif_neigh_create(struct batadv_hard_iface *hard_iface,
520-
const u8 *neigh_addr)
521+
const u8 *neigh_addr,
522+
struct batadv_orig_node *orig_node)
521523
{
522524
struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
523525
struct batadv_hardif_neigh_node *hardif_neigh;
@@ -536,6 +538,7 @@ batadv_hardif_neigh_create(struct batadv_hard_iface *hard_iface,
536538
kref_get(&hard_iface->refcount);
537539
INIT_HLIST_NODE(&hardif_neigh->list);
538540
ether_addr_copy(hardif_neigh->addr, neigh_addr);
541+
ether_addr_copy(hardif_neigh->orig, orig_node->orig);
539542
hardif_neigh->if_incoming = hard_iface;
540543
hardif_neigh->last_seen = jiffies;
541544

@@ -556,12 +559,14 @@ batadv_hardif_neigh_create(struct batadv_hard_iface *hard_iface,
556559
* node
557560
* @hard_iface: the interface this neighbour is connected to
558561
* @neigh_addr: the interface address of the neighbour to retrieve
562+
* @orig_node: originator object representing the neighbour
559563
*
560564
* Return: the hardif neighbour node if found or created or NULL otherwise.
561565
*/
562566
static struct batadv_hardif_neigh_node *
563567
batadv_hardif_neigh_get_or_create(struct batadv_hard_iface *hard_iface,
564-
const u8 *neigh_addr)
568+
const u8 *neigh_addr,
569+
struct batadv_orig_node *orig_node)
565570
{
566571
struct batadv_hardif_neigh_node *hardif_neigh;
567572

@@ -570,7 +575,7 @@ batadv_hardif_neigh_get_or_create(struct batadv_hard_iface *hard_iface,
570575
if (hardif_neigh)
571576
return hardif_neigh;
572577

573-
return batadv_hardif_neigh_create(hard_iface, neigh_addr);
578+
return batadv_hardif_neigh_create(hard_iface, neigh_addr, orig_node);
574579
}
575580

576581
/**
@@ -630,7 +635,7 @@ batadv_neigh_node_create(struct batadv_orig_node *orig_node,
630635
goto out;
631636

632637
hardif_neigh = batadv_hardif_neigh_get_or_create(hard_iface,
633-
neigh_addr);
638+
neigh_addr, orig_node);
634639
if (!hardif_neigh)
635640
goto out;
636641

‎net/batman-adv/routing.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1170,7 +1170,7 @@ int batadv_recv_bcast_packet(struct sk_buff *skb,
11701170
batadv_skb_set_priority(skb, sizeof(struct batadv_bcast_packet));
11711171

11721172
/* rebroadcast packet */
1173-
batadv_add_bcast_packet_to_list(bat_priv, skb, 1);
1173+
batadv_add_bcast_packet_to_list(bat_priv, skb, 1, false);
11741174

11751175
/* don't hand the broadcast up if it is from an originator
11761176
* from the same backbone.

‎net/batman-adv/send.c‎

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,7 @@ _batadv_add_bcast_packet_to_list(struct batadv_priv *bat_priv,
549549
* @bat_priv: the bat priv with all the soft interface information
550550
* @skb: broadcast packet to add
551551
* @delay: number of jiffies to wait before sending
552+
* @own_packet: true if it is a self-generated broadcast packet
552553
*
553554
* add a broadcast packet to the queue and setup timers. broadcast packets
554555
* are sent multiple times to increase probability for being received.
@@ -560,7 +561,8 @@ _batadv_add_bcast_packet_to_list(struct batadv_priv *bat_priv,
560561
*/
561562
int batadv_add_bcast_packet_to_list(struct batadv_priv *bat_priv,
562563
const struct sk_buff *skb,
563-
unsigned long delay)
564+
unsigned long delay,
565+
bool own_packet)
564566
{
565567
struct batadv_hard_iface *primary_if;
566568
struct batadv_forw_packet *forw_packet;
@@ -587,6 +589,7 @@ int batadv_add_bcast_packet_to_list(struct batadv_priv *bat_priv,
587589
bcast_packet->ttl--;
588590

589591
forw_packet->skb = newskb;
592+
forw_packet->own = own_packet;
590593

591594
INIT_DELAYED_WORK(&forw_packet->delayed_work,
592595
batadv_send_outstanding_bcast_packet);
@@ -603,11 +606,16 @@ int batadv_add_bcast_packet_to_list(struct batadv_priv *bat_priv,
603606
static void batadv_send_outstanding_bcast_packet(struct work_struct *work)
604607
{
605608
struct batadv_hard_iface *hard_iface;
609+
struct batadv_hardif_neigh_node *neigh_node;
606610
struct delayed_work *delayed_work;
607611
struct batadv_forw_packet *forw_packet;
612+
struct batadv_bcast_packet *bcast_packet;
608613
struct sk_buff *skb1;
609614
struct net_device *soft_iface;
610615
struct batadv_priv *bat_priv;
616+
u8 *neigh_addr;
617+
u8 *orig_neigh;
618+
int ret = 0;
611619

612620
delayed_work = to_delayed_work(work);
613621
forw_packet = container_of(delayed_work, struct batadv_forw_packet,
@@ -625,6 +633,8 @@ static void batadv_send_outstanding_bcast_packet(struct work_struct *work)
625633
if (batadv_dat_drop_broadcast_packet(bat_priv, forw_packet))
626634
goto out;
627635

636+
bcast_packet = (struct batadv_bcast_packet *)forw_packet->skb->data;
637+
628638
/* rebroadcast packet */
629639
rcu_read_lock();
630640
list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
@@ -634,6 +644,49 @@ static void batadv_send_outstanding_bcast_packet(struct work_struct *work)
634644
if (forw_packet->num_packets >= hard_iface->num_bcasts)
635645
continue;
636646

647+
if (forw_packet->own) {
648+
neigh_node = NULL;
649+
} else {
650+
neigh_addr = eth_hdr(forw_packet->skb)->h_source;
651+
neigh_node = batadv_hardif_neigh_get(hard_iface,
652+
neigh_addr);
653+
}
654+
655+
orig_neigh = neigh_node ? neigh_node->orig : NULL;
656+
657+
ret = batadv_hardif_no_broadcast(hard_iface, bcast_packet->orig,
658+
orig_neigh);
659+
660+
if (ret) {
661+
char *type;
662+
663+
switch (ret) {
664+
case BATADV_HARDIF_BCAST_NORECIPIENT:
665+
type = "no neighbor";
666+
break;
667+
case BATADV_HARDIF_BCAST_DUPFWD:
668+
type = "single neighbor is source";
669+
break;
670+
case BATADV_HARDIF_BCAST_DUPORIG:
671+
type = "single neighbor is originator";
672+
break;
673+
default:
674+
type = "unknown";
675+
}
676+
677+
batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "BCAST packet from orig %pM on %s surpressed: %s\n",
678+
bcast_packet->orig,
679+
hard_iface->net_dev->name, type);
680+
681+
if (neigh_node)
682+
batadv_hardif_neigh_put(neigh_node);
683+
684+
continue;
685+
}
686+
687+
if (neigh_node)
688+
batadv_hardif_neigh_put(neigh_node);
689+
637690
if (!kref_get_unless_zero(&hard_iface->refcount))
638691
continue;
639692

‎net/batman-adv/send.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ int batadv_send_unicast_skb(struct sk_buff *skb,
4646
struct batadv_neigh_node *neigh_node);
4747
int batadv_add_bcast_packet_to_list(struct batadv_priv *bat_priv,
4848
const struct sk_buff *skb,
49-
unsigned long delay);
49+
unsigned long delay,
50+
bool own_packet);
5051
void
5152
batadv_purge_outstanding_packets(struct batadv_priv *bat_priv,
5253
const struct batadv_hard_iface *hard_iface);

‎net/batman-adv/soft-interface.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ static int batadv_interface_tx(struct sk_buff *skb,
357357
seqno = atomic_inc_return(&bat_priv->bcast_seqno);
358358
bcast_packet->seqno = htonl(seqno);
359359

360-
batadv_add_bcast_packet_to_list(bat_priv, skb, brd_delay);
360+
batadv_add_bcast_packet_to_list(bat_priv, skb, brd_delay, true);
361361

362362
/* a copy is stored in the bcast list, therefore removing
363363
* the original skb.

‎net/batman-adv/types.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,7 @@ struct batadv_hardif_neigh_node_bat_v {
408408
* struct batadv_hardif_neigh_node - unique neighbor per hard-interface
409409
* @list: list node for batadv_hard_iface::neigh_list
410410
* @addr: the MAC address of the neighboring interface
411+
* @orig: the address of the originator this neighbor node belongs to
411412
* @if_incoming: pointer to incoming hard-interface
412413
* @last_seen: when last packet via this neighbor was received
413414
* @bat_v: B.A.T.M.A.N. V private data
@@ -417,6 +418,7 @@ struct batadv_hardif_neigh_node_bat_v {
417418
struct batadv_hardif_neigh_node {
418419
struct hlist_node list;
419420
u8 addr[ETH_ALEN];
421+
u8 orig[ETH_ALEN];
420422
struct batadv_hard_iface *if_incoming;
421423
unsigned long last_seen;
422424
#ifdef CONFIG_BATMAN_ADV_BATMAN_V

0 commit comments

Comments
 (0)