diff options
Diffstat (limited to 'net/batman-adv/bitarray.c')
-rw-r--r-- | net/batman-adv/bitarray.c | 118 |
1 files changed, 9 insertions, 109 deletions
diff --git a/net/batman-adv/bitarray.c b/net/batman-adv/bitarray.c index 6d0aa216b232..07ae6e1b8aca 100644 --- a/net/batman-adv/bitarray.c +++ b/net/batman-adv/bitarray.c | |||
@@ -24,100 +24,13 @@ | |||
24 | 24 | ||
25 | #include <linux/bitops.h> | 25 | #include <linux/bitops.h> |
26 | 26 | ||
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 | |||
67 | /* shift the packet array by n places. */ | 27 | /* 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) |
69 | { | 29 | { |
70 | int32_t word_offset, word_num; | ||
71 | int32_t i; | ||
72 | |||
73 | if (n <= 0 || n >= TQ_LOCAL_WINDOW_SIZE) | 30 | if (n <= 0 || n >= TQ_LOCAL_WINDOW_SIZE) |
74 | return; | 31 | return; |
75 | 32 | ||
76 | word_offset = n % WORD_BIT_SIZE;/* shift how much inside each word */ | 33 | bitmap_shift_left(seq_bits, seq_bits, n, TQ_LOCAL_WINDOW_SIZE); |
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; | ||
121 | } | 34 | } |
122 | 35 | ||
123 | 36 | ||
@@ -137,7 +50,7 @@ int bit_get_packet(void *priv, unsigned long *seq_bits, | |||
137 | 50 | ||
138 | if ((seq_num_diff <= 0) && (seq_num_diff > -TQ_LOCAL_WINDOW_SIZE)) { | 51 | if ((seq_num_diff <= 0) && (seq_num_diff > -TQ_LOCAL_WINDOW_SIZE)) { |
139 | if (set_mark) | 52 | if (set_mark) |
140 | bit_mark(seq_bits, -seq_num_diff); | 53 | bat_set_bit(seq_bits, -seq_num_diff); |
141 | return 0; | 54 | return 0; |
142 | } | 55 | } |
143 | 56 | ||
@@ -145,10 +58,10 @@ int bit_get_packet(void *priv, unsigned long *seq_bits, | |||
145 | * set the mark if required */ | 58 | * set the mark if required */ |
146 | 59 | ||
147 | if ((seq_num_diff > 0) && (seq_num_diff < TQ_LOCAL_WINDOW_SIZE)) { | 60 | 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); |
149 | 62 | ||
150 | if (set_mark) | 63 | if (set_mark) |
151 | bit_mark(seq_bits, 0); | 64 | bat_set_bit(seq_bits, 0); |
152 | return 1; | 65 | return 1; |
153 | } | 66 | } |
154 | 67 | ||
@@ -159,9 +72,9 @@ int bit_get_packet(void *priv, unsigned long *seq_bits, | |||
159 | bat_dbg(DBG_BATMAN, bat_priv, | 72 | bat_dbg(DBG_BATMAN, bat_priv, |
160 | "We missed a lot of packets (%i) !\n", | 73 | "We missed a lot of packets (%i) !\n", |
161 | seq_num_diff - 1); | 74 | seq_num_diff - 1); |
162 | bit_reset_window(seq_bits); | 75 | bitmap_zero(seq_bits, TQ_LOCAL_WINDOW_SIZE); |
163 | if (set_mark) | 76 | if (set_mark) |
164 | bit_mark(seq_bits, 0); | 77 | bat_set_bit(seq_bits, 0); |
165 | return 1; | 78 | return 1; |
166 | } | 79 | } |
167 | 80 | ||
@@ -176,9 +89,9 @@ int bit_get_packet(void *priv, unsigned long *seq_bits, | |||
176 | bat_dbg(DBG_BATMAN, bat_priv, | 89 | bat_dbg(DBG_BATMAN, bat_priv, |
177 | "Other host probably restarted!\n"); | 90 | "Other host probably restarted!\n"); |
178 | 91 | ||
179 | bit_reset_window(seq_bits); | 92 | bitmap_zero(seq_bits, TQ_LOCAL_WINDOW_SIZE); |
180 | if (set_mark) | 93 | if (set_mark) |
181 | bit_mark(seq_bits, 0); | 94 | bat_set_bit(seq_bits, 0); |
182 | 95 | ||
183 | return 1; | 96 | return 1; |
184 | } | 97 | } |
@@ -186,16 +99,3 @@ int bit_get_packet(void *priv, unsigned long *seq_bits, | |||
186 | /* never reached */ | 99 | /* never reached */ |
187 | return 0; | 100 | return 0; |
188 | } | 101 | } |
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 | } | ||