Skip to content

Commit 0079d2c

Browse files
ecsvAntonio Quartulli
authored andcommitted
batman-adv: Replace bitarray operations with bitmap
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>
1 parent c1faead commit 0079d2c

6 files changed

Lines changed: 41 additions & 130 deletions

File tree

‎net/batman-adv/bat_iv_ogm.c‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -850,9 +850,9 @@ static int bat_iv_ogm_update_seqnos(const struct ethhdr *ethhdr,
850850
hlist_for_each_entry_rcu(tmp_neigh_node, node,
851851
&orig_node->neigh_list, list) {
852852

853-
is_duplicate |= get_bit_status(tmp_neigh_node->real_bits,
854-
orig_node->last_real_seqno,
855-
batman_ogm_packet->seqno);
853+
is_duplicate |= bat_test_bit(tmp_neigh_node->real_bits,
854+
orig_node->last_real_seqno,
855+
batman_ogm_packet->seqno);
856856

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

868868
tmp_neigh_node->real_packet_count =
869-
bit_packet_count(tmp_neigh_node->real_bits);
869+
bitmap_weight(tmp_neigh_node->real_bits,
870+
TQ_LOCAL_WINDOW_SIZE);
870871
}
871872
rcu_read_unlock();
872873

@@ -998,11 +999,11 @@ static void bat_iv_ogm_process(const struct ethhdr *ethhdr,
998999

9991000
spin_lock_bh(&orig_neigh_node->ogm_cnt_lock);
10001001
word = &(orig_neigh_node->bcast_own[offset]);
1001-
bit_mark(word,
1002-
if_incoming_seqno -
1002+
bat_set_bit(word,
1003+
if_incoming_seqno -
10031004
batman_ogm_packet->seqno - 2);
10041005
orig_neigh_node->bcast_own_sum[if_incoming->if_num] =
1005-
bit_packet_count(word);
1006+
bitmap_weight(word, TQ_LOCAL_WINDOW_SIZE);
10061007
spin_unlock_bh(&orig_neigh_node->ogm_cnt_lock);
10071008
}
10081009

‎net/batman-adv/bitarray.c‎

Lines changed: 9 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -24,100 +24,13 @@
2424

2525
#include <linux/bitops.h>
2626

27-
/* returns true if the corresponding bit in the given seq_bits indicates true
28-
* and curr_seqno is within range of last_seqno */
29-
int get_bit_status(const unsigned long *seq_bits, uint32_t last_seqno,
30-
uint32_t curr_seqno)
31-
{
32-
int32_t diff, word_offset, word_num;
33-
34-
diff = last_seqno - curr_seqno;
35-
if (diff < 0 || diff >= TQ_LOCAL_WINDOW_SIZE) {
36-
return 0;
37-
} else {
38-
/* which word */
39-
word_num = (last_seqno - curr_seqno) / WORD_BIT_SIZE;
40-
/* which position in the selected word */
41-
word_offset = (last_seqno - curr_seqno) % WORD_BIT_SIZE;
42-
43-
if (test_bit(word_offset, &seq_bits[word_num]))
44-
return 1;
45-
else
46-
return 0;
47-
}
48-
}
49-
50-
/* turn corresponding bit on, so we can remember that we got the packet */
51-
void bit_mark(unsigned long *seq_bits, int32_t n)
52-
{
53-
int32_t word_offset, word_num;
54-
55-
/* if too old, just drop it */
56-
if (n < 0 || n >= TQ_LOCAL_WINDOW_SIZE)
57-
return;
58-
59-
/* which word */
60-
word_num = n / WORD_BIT_SIZE;
61-
/* which position in the selected word */
62-
word_offset = n % WORD_BIT_SIZE;
63-
64-
set_bit(word_offset, &seq_bits[word_num]); /* turn the position on */
65-
}
66-
6727
/* shift the packet array by n places. */
68-
static void bit_shift(unsigned long *seq_bits, int32_t n)
28+
static void bat_bitmap_shift_left(unsigned long *seq_bits, int32_t n)
6929
{
70-
int32_t word_offset, word_num;
71-
int32_t i;
72-
7330
if (n <= 0 || n >= TQ_LOCAL_WINDOW_SIZE)
7431
return;
7532

76-
word_offset = n % WORD_BIT_SIZE;/* shift how much inside each word */
77-
word_num = n / WORD_BIT_SIZE; /* shift over how much (full) words */
78-
79-
for (i = NUM_WORDS - 1; i > word_num; i--) {
80-
/* going from old to new, so we don't overwrite the data we copy
81-
* from.
82-
*
83-
* left is high, right is low: FEDC BA98 7654 3210
84-
* ^^ ^^
85-
* vvvv
86-
* ^^^^ = from, vvvvv =to, we'd have word_num==1 and
87-
* word_offset==WORD_BIT_SIZE/2 ????? in this example.
88-
* (=24 bits)
89-
*
90-
* our desired output would be: 9876 5432 1000 0000
91-
* */
92-
93-
seq_bits[i] =
94-
(seq_bits[i - word_num] << word_offset) +
95-
/* take the lower port from the left half, shift it left
96-
* to its final position */
97-
(seq_bits[i - word_num - 1] >>
98-
(WORD_BIT_SIZE-word_offset));
99-
/* and the upper part of the right half and shift it left to
100-
* its position */
101-
/* for our example that would be: word[0] = 9800 + 0076 =
102-
* 9876 */
103-
}
104-
/* now for our last word, i==word_num, we only have its "left" half.
105-
* that's the 1000 word in our example.*/
106-
107-
seq_bits[i] = (seq_bits[i - word_num] << word_offset);
108-
109-
/* pad the rest with 0, if there is anything */
110-
i--;
111-
112-
for (; i >= 0; i--)
113-
seq_bits[i] = 0;
114-
}
115-
116-
static void bit_reset_window(unsigned long *seq_bits)
117-
{
118-
int i;
119-
for (i = 0; i < NUM_WORDS; i++)
120-
seq_bits[i] = 0;
33+
bitmap_shift_left(seq_bits, seq_bits, n, TQ_LOCAL_WINDOW_SIZE);
12134
}
12235

12336

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

13851
if ((seq_num_diff <= 0) && (seq_num_diff > -TQ_LOCAL_WINDOW_SIZE)) {
13952
if (set_mark)
140-
bit_mark(seq_bits, -seq_num_diff);
53+
bat_set_bit(seq_bits, -seq_num_diff);
14154
return 0;
14255
}
14356

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

14760
if ((seq_num_diff > 0) && (seq_num_diff < TQ_LOCAL_WINDOW_SIZE)) {
148-
bit_shift(seq_bits, seq_num_diff);
61+
bat_bitmap_shift_left(seq_bits, seq_num_diff);
14962

15063
if (set_mark)
151-
bit_mark(seq_bits, 0);
64+
bat_set_bit(seq_bits, 0);
15265
return 1;
15366
}
15467

@@ -159,9 +72,9 @@ int bit_get_packet(void *priv, unsigned long *seq_bits,
15972
bat_dbg(DBG_BATMAN, bat_priv,
16073
"We missed a lot of packets (%i) !\n",
16174
seq_num_diff - 1);
162-
bit_reset_window(seq_bits);
75+
bitmap_zero(seq_bits, TQ_LOCAL_WINDOW_SIZE);
16376
if (set_mark)
164-
bit_mark(seq_bits, 0);
77+
bat_set_bit(seq_bits, 0);
16578
return 1;
16679
}
16780

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

179-
bit_reset_window(seq_bits);
92+
bitmap_zero(seq_bits, TQ_LOCAL_WINDOW_SIZE);
18093
if (set_mark)
181-
bit_mark(seq_bits, 0);
94+
bat_set_bit(seq_bits, 0);
18295

18396
return 1;
18497
}
18598

18699
/* never reached */
187100
return 0;
188101
}
189-
190-
/* count the hamming weight, how many good packets did we receive? just count
191-
* the 1's.
192-
*/
193-
int bit_packet_count(const unsigned long *seq_bits)
194-
{
195-
int i, hamming = 0;
196-
197-
for (i = 0; i < NUM_WORDS; i++)
198-
hamming += hweight_long(seq_bits[i]);
199-
200-
return hamming;
201-
}

‎net/batman-adv/bitarray.h‎

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,33 @@
2222
#ifndef _NET_BATMAN_ADV_BITARRAY_H_
2323
#define _NET_BATMAN_ADV_BITARRAY_H_
2424

25-
#define WORD_BIT_SIZE (sizeof(unsigned long) * 8)
26-
2725
/* returns true if the corresponding bit in the given seq_bits indicates true
2826
* and curr_seqno is within range of last_seqno */
29-
int get_bit_status(const unsigned long *seq_bits, uint32_t last_seqno,
30-
uint32_t curr_seqno);
27+
static inline int bat_test_bit(const unsigned long *seq_bits,
28+
uint32_t last_seqno, uint32_t curr_seqno)
29+
{
30+
int32_t diff;
31+
32+
diff = last_seqno - curr_seqno;
33+
if (diff < 0 || diff >= TQ_LOCAL_WINDOW_SIZE)
34+
return 0;
35+
else
36+
return test_bit(diff, seq_bits);
37+
}
3138

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

46+
set_bit(n, seq_bits); /* turn the position on */
47+
}
3548

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

41-
/* count the hamming weight, how many good packets did we receive? */
42-
int bit_packet_count(const unsigned long *seq_bits);
43-
4454
#endif /* _NET_BATMAN_ADV_BITARRAY_H_ */

‎net/batman-adv/main.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565

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

68-
#define NUM_WORDS (TQ_LOCAL_WINDOW_SIZE / WORD_BIT_SIZE)
68+
#define NUM_WORDS BITS_TO_LONGS(TQ_LOCAL_WINDOW_SIZE)
6969

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

‎net/batman-adv/routing.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ void slide_own_bcast_window(struct hard_iface *hard_iface)
5252

5353
bit_get_packet(bat_priv, word, 1, 0);
5454
orig_node->bcast_own_sum[hard_iface->if_num] =
55-
bit_packet_count(word);
55+
bitmap_weight(word, TQ_LOCAL_WINDOW_SIZE);
5656
spin_unlock_bh(&orig_node->ogm_cnt_lock);
5757
}
5858
rcu_read_unlock();
@@ -1047,8 +1047,8 @@ int recv_bcast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
10471047
spin_lock_bh(&orig_node->bcast_seqno_lock);
10481048

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

10541054
seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;

‎net/batman-adv/types.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ struct orig_node {
9090
bool tt_poss_change;
9191
uint32_t last_real_seqno;
9292
uint8_t last_ttl;
93-
unsigned long bcast_bits[NUM_WORDS];
93+
DECLARE_BITMAP(bcast_bits, TQ_LOCAL_WINDOW_SIZE);
9494
uint32_t last_bcast_seqno;
9595
struct hlist_head neigh_list;
9696
struct list_head frag_list;
@@ -132,7 +132,7 @@ struct neigh_node {
132132
uint8_t last_ttl;
133133
struct list_head bonding_list;
134134
unsigned long last_valid;
135-
unsigned long real_bits[NUM_WORDS];
135+
DECLARE_BITMAP(real_bits, TQ_LOCAL_WINDOW_SIZE);
136136
atomic_t refcount;
137137
struct rcu_head rcu;
138138
struct orig_node *orig_node;

0 commit comments

Comments
 (0)