Skip to content

Commit

Permalink
batman-adv: Replace bitarray operations with bitmap
Browse files Browse the repository at this point in the history
bitarray.c consists mostly of functionality that is already available as part
of the standard kernel API. batman-adv could use architecture optimized code
and reduce the binary size by switching to the standard functions.

Signed-off-by: Sven Eckelmann <sven@narfation.org>
Signed-off-by: Antonio Quartulli <ordex@autistici.org>
  • Loading branch information
ecsv authored and Antonio Quartulli committed Apr 11, 2012
1 parent c1faead commit 0079d2c
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 130 deletions.
15 changes: 8 additions & 7 deletions net/batman-adv/bat_iv_ogm.c
Original file line number Diff line number Diff line change
Expand Up @@ -850,9 +850,9 @@ static int bat_iv_ogm_update_seqnos(const struct ethhdr *ethhdr,
hlist_for_each_entry_rcu(tmp_neigh_node, node,
&orig_node->neigh_list, list) {

is_duplicate |= get_bit_status(tmp_neigh_node->real_bits,
orig_node->last_real_seqno,
batman_ogm_packet->seqno);
is_duplicate |= bat_test_bit(tmp_neigh_node->real_bits,
orig_node->last_real_seqno,
batman_ogm_packet->seqno);

if (compare_eth(tmp_neigh_node->addr, ethhdr->h_source) &&
(tmp_neigh_node->if_incoming == if_incoming))
Expand All @@ -866,7 +866,8 @@ static int bat_iv_ogm_update_seqnos(const struct ethhdr *ethhdr,
seq_diff, set_mark);

tmp_neigh_node->real_packet_count =
bit_packet_count(tmp_neigh_node->real_bits);
bitmap_weight(tmp_neigh_node->real_bits,
TQ_LOCAL_WINDOW_SIZE);
}
rcu_read_unlock();

Expand Down Expand Up @@ -998,11 +999,11 @@ static void bat_iv_ogm_process(const struct ethhdr *ethhdr,

spin_lock_bh(&orig_neigh_node->ogm_cnt_lock);
word = &(orig_neigh_node->bcast_own[offset]);
bit_mark(word,
if_incoming_seqno -
bat_set_bit(word,
if_incoming_seqno -
batman_ogm_packet->seqno - 2);
orig_neigh_node->bcast_own_sum[if_incoming->if_num] =
bit_packet_count(word);
bitmap_weight(word, TQ_LOCAL_WINDOW_SIZE);
spin_unlock_bh(&orig_neigh_node->ogm_cnt_lock);
}

Expand Down
118 changes: 9 additions & 109 deletions net/batman-adv/bitarray.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,100 +24,13 @@

#include <linux/bitops.h>

/* returns true if the corresponding bit in the given seq_bits indicates true
* and curr_seqno is within range of last_seqno */
int get_bit_status(const unsigned long *seq_bits, uint32_t last_seqno,
uint32_t curr_seqno)
{
int32_t diff, word_offset, word_num;

diff = last_seqno - curr_seqno;
if (diff < 0 || diff >= TQ_LOCAL_WINDOW_SIZE) {
return 0;
} else {
/* which word */
word_num = (last_seqno - curr_seqno) / WORD_BIT_SIZE;
/* which position in the selected word */
word_offset = (last_seqno - curr_seqno) % WORD_BIT_SIZE;

if (test_bit(word_offset, &seq_bits[word_num]))
return 1;
else
return 0;
}
}

/* turn corresponding bit on, so we can remember that we got the packet */
void bit_mark(unsigned long *seq_bits, int32_t n)
{
int32_t word_offset, word_num;

/* if too old, just drop it */
if (n < 0 || n >= TQ_LOCAL_WINDOW_SIZE)
return;

/* which word */
word_num = n / WORD_BIT_SIZE;
/* which position in the selected word */
word_offset = n % WORD_BIT_SIZE;

set_bit(word_offset, &seq_bits[word_num]); /* turn the position on */
}

/* shift the packet array by n places. */
static void bit_shift(unsigned long *seq_bits, int32_t n)
static void bat_bitmap_shift_left(unsigned long *seq_bits, int32_t n)
{
int32_t word_offset, word_num;
int32_t i;

if (n <= 0 || n >= TQ_LOCAL_WINDOW_SIZE)
return;

word_offset = n % WORD_BIT_SIZE;/* shift how much inside each word */
word_num = n / WORD_BIT_SIZE; /* shift over how much (full) words */

for (i = NUM_WORDS - 1; i > word_num; i--) {
/* going from old to new, so we don't overwrite the data we copy
* from.
*
* left is high, right is low: FEDC BA98 7654 3210
* ^^ ^^
* vvvv
* ^^^^ = from, vvvvv =to, we'd have word_num==1 and
* word_offset==WORD_BIT_SIZE/2 ????? in this example.
* (=24 bits)
*
* our desired output would be: 9876 5432 1000 0000
* */

seq_bits[i] =
(seq_bits[i - word_num] << word_offset) +
/* take the lower port from the left half, shift it left
* to its final position */
(seq_bits[i - word_num - 1] >>
(WORD_BIT_SIZE-word_offset));
/* and the upper part of the right half and shift it left to
* its position */
/* for our example that would be: word[0] = 9800 + 0076 =
* 9876 */
}
/* now for our last word, i==word_num, we only have its "left" half.
* that's the 1000 word in our example.*/

seq_bits[i] = (seq_bits[i - word_num] << word_offset);

/* pad the rest with 0, if there is anything */
i--;

for (; i >= 0; i--)
seq_bits[i] = 0;
}

static void bit_reset_window(unsigned long *seq_bits)
{
int i;
for (i = 0; i < NUM_WORDS; i++)
seq_bits[i] = 0;
bitmap_shift_left(seq_bits, seq_bits, n, TQ_LOCAL_WINDOW_SIZE);
}


Expand All @@ -137,18 +50,18 @@ int bit_get_packet(void *priv, unsigned long *seq_bits,

if ((seq_num_diff <= 0) && (seq_num_diff > -TQ_LOCAL_WINDOW_SIZE)) {
if (set_mark)
bit_mark(seq_bits, -seq_num_diff);
bat_set_bit(seq_bits, -seq_num_diff);
return 0;
}

/* sequence number is slightly newer, so we shift the window and
* set the mark if required */

if ((seq_num_diff > 0) && (seq_num_diff < TQ_LOCAL_WINDOW_SIZE)) {
bit_shift(seq_bits, seq_num_diff);
bat_bitmap_shift_left(seq_bits, seq_num_diff);

if (set_mark)
bit_mark(seq_bits, 0);
bat_set_bit(seq_bits, 0);
return 1;
}

Expand All @@ -159,9 +72,9 @@ int bit_get_packet(void *priv, unsigned long *seq_bits,
bat_dbg(DBG_BATMAN, bat_priv,
"We missed a lot of packets (%i) !\n",
seq_num_diff - 1);
bit_reset_window(seq_bits);
bitmap_zero(seq_bits, TQ_LOCAL_WINDOW_SIZE);
if (set_mark)
bit_mark(seq_bits, 0);
bat_set_bit(seq_bits, 0);
return 1;
}

Expand All @@ -176,26 +89,13 @@ int bit_get_packet(void *priv, unsigned long *seq_bits,
bat_dbg(DBG_BATMAN, bat_priv,
"Other host probably restarted!\n");

bit_reset_window(seq_bits);
bitmap_zero(seq_bits, TQ_LOCAL_WINDOW_SIZE);
if (set_mark)
bit_mark(seq_bits, 0);
bat_set_bit(seq_bits, 0);

return 1;
}

/* never reached */
return 0;
}

/* count the hamming weight, how many good packets did we receive? just count
* the 1's.
*/
int bit_packet_count(const unsigned long *seq_bits)
{
int i, hamming = 0;

for (i = 0; i < NUM_WORDS; i++)
hamming += hweight_long(seq_bits[i]);

return hamming;
}
26 changes: 18 additions & 8 deletions net/batman-adv/bitarray.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,33 @@
#ifndef _NET_BATMAN_ADV_BITARRAY_H_
#define _NET_BATMAN_ADV_BITARRAY_H_

#define WORD_BIT_SIZE (sizeof(unsigned long) * 8)

/* returns true if the corresponding bit in the given seq_bits indicates true
* and curr_seqno is within range of last_seqno */
int get_bit_status(const unsigned long *seq_bits, uint32_t last_seqno,
uint32_t curr_seqno);
static inline int bat_test_bit(const unsigned long *seq_bits,
uint32_t last_seqno, uint32_t curr_seqno)
{
int32_t diff;

diff = last_seqno - curr_seqno;
if (diff < 0 || diff >= TQ_LOCAL_WINDOW_SIZE)
return 0;
else
return test_bit(diff, seq_bits);
}

/* turn corresponding bit on, so we can remember that we got the packet */
void bit_mark(unsigned long *seq_bits, int32_t n);
static inline void bat_set_bit(unsigned long *seq_bits, int32_t n)
{
/* if too old, just drop it */
if (n < 0 || n >= TQ_LOCAL_WINDOW_SIZE)
return;

set_bit(n, seq_bits); /* turn the position on */
}

/* receive and process one packet, returns 1 if received seq_num is considered
* new, 0 if old */
int bit_get_packet(void *priv, unsigned long *seq_bits,
int32_t seq_num_diff, int set_mark);

/* count the hamming weight, how many good packets did we receive? */
int bit_packet_count(const unsigned long *seq_bits);

#endif /* _NET_BATMAN_ADV_BITARRAY_H_ */
2 changes: 1 addition & 1 deletion net/batman-adv/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@

#define NULL_IFINDEX 0 /* dummy ifindex used to avoid iface checks */

#define NUM_WORDS (TQ_LOCAL_WINDOW_SIZE / WORD_BIT_SIZE)
#define NUM_WORDS BITS_TO_LONGS(TQ_LOCAL_WINDOW_SIZE)

#define LOG_BUF_LEN 8192 /* has to be a power of 2 */

Expand Down
6 changes: 3 additions & 3 deletions net/batman-adv/routing.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void slide_own_bcast_window(struct hard_iface *hard_iface)

bit_get_packet(bat_priv, word, 1, 0);
orig_node->bcast_own_sum[hard_iface->if_num] =
bit_packet_count(word);
bitmap_weight(word, TQ_LOCAL_WINDOW_SIZE);
spin_unlock_bh(&orig_node->ogm_cnt_lock);
}
rcu_read_unlock();
Expand Down Expand Up @@ -1047,8 +1047,8 @@ int recv_bcast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
spin_lock_bh(&orig_node->bcast_seqno_lock);

/* check whether the packet is a duplicate */
if (get_bit_status(orig_node->bcast_bits, orig_node->last_bcast_seqno,
ntohl(bcast_packet->seqno)))
if (bat_test_bit(orig_node->bcast_bits, orig_node->last_bcast_seqno,
ntohl(bcast_packet->seqno)))
goto spin_unlock;

seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
Expand Down
4 changes: 2 additions & 2 deletions net/batman-adv/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ struct orig_node {
bool tt_poss_change;
uint32_t last_real_seqno;
uint8_t last_ttl;
unsigned long bcast_bits[NUM_WORDS];
DECLARE_BITMAP(bcast_bits, TQ_LOCAL_WINDOW_SIZE);
uint32_t last_bcast_seqno;
struct hlist_head neigh_list;
struct list_head frag_list;
Expand Down Expand Up @@ -132,7 +132,7 @@ struct neigh_node {
uint8_t last_ttl;
struct list_head bonding_list;
unsigned long last_valid;
unsigned long real_bits[NUM_WORDS];
DECLARE_BITMAP(real_bits, TQ_LOCAL_WINDOW_SIZE);
atomic_t refcount;
struct rcu_head rcu;
struct orig_node *orig_node;
Expand Down

0 comments on commit 0079d2c

Please sign in to comment.