1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include "main.h"
23#include "bitarray.h"
24
25#include <linux/bitops.h>
26
27
28
29int 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
39 word_num = (last_seqno - curr_seqno) / WORD_BIT_SIZE;
40
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
51void bit_mark(unsigned long *seq_bits, int32_t n)
52{
53 int32_t word_offset, word_num;
54
55
56 if (n < 0 || n >= TQ_LOCAL_WINDOW_SIZE)
57 return;
58
59
60 word_num = n / WORD_BIT_SIZE;
61
62 word_offset = n % WORD_BIT_SIZE;
63
64 set_bit(word_offset, &seq_bits[word_num]);
65}
66
67
68static void bit_shift(unsigned long *seq_bits, int32_t n)
69{
70 int32_t word_offset, word_num;
71 int32_t i;
72
73 if (n <= 0 || n >= TQ_LOCAL_WINDOW_SIZE)
74 return;
75
76 word_offset = n % WORD_BIT_SIZE;
77 word_num = n / WORD_BIT_SIZE;
78
79 for (i = NUM_WORDS - 1; i > word_num; i--) {
80
81
82
83
84
85
86
87
88
89
90
91
92
93 seq_bits[i] =
94 (seq_bits[i - word_num] << word_offset) +
95
96
97 (seq_bits[i - word_num - 1] >>
98 (WORD_BIT_SIZE-word_offset));
99
100
101
102
103 }
104
105
106
107 seq_bits[i] = (seq_bits[i - word_num] << word_offset);
108
109
110 i--;
111
112 for (; i >= 0; i--)
113 seq_bits[i] = 0;
114}
115
116static 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}
122
123
124
125
126
127
128
129
130int bit_get_packet(void *priv, unsigned long *seq_bits,
131 int32_t seq_num_diff, int set_mark)
132{
133 struct bat_priv *bat_priv = priv;
134
135
136
137
138 if ((seq_num_diff <= 0) && (seq_num_diff > -TQ_LOCAL_WINDOW_SIZE)) {
139 if (set_mark)
140 bit_mark(seq_bits, -seq_num_diff);
141 return 0;
142 }
143
144
145
146
147 if ((seq_num_diff > 0) && (seq_num_diff < TQ_LOCAL_WINDOW_SIZE)) {
148 bit_shift(seq_bits, seq_num_diff);
149
150 if (set_mark)
151 bit_mark(seq_bits, 0);
152 return 1;
153 }
154
155
156
157 if ((seq_num_diff >= TQ_LOCAL_WINDOW_SIZE)
158 && (seq_num_diff < EXPECTED_SEQNO_RANGE)) {
159 bat_dbg(DBG_BATMAN, bat_priv,
160 "We missed a lot of packets (%i) !\n",
161 seq_num_diff - 1);
162 bit_reset_window(seq_bits);
163 if (set_mark)
164 bit_mark(seq_bits, 0);
165 return 1;
166 }
167
168
169
170
171
172
173 if ((seq_num_diff <= -TQ_LOCAL_WINDOW_SIZE)
174 || (seq_num_diff >= EXPECTED_SEQNO_RANGE)) {
175
176 bat_dbg(DBG_BATMAN, bat_priv,
177 "Other host probably restarted!\n");
178
179 bit_reset_window(seq_bits);
180 if (set_mark)
181 bit_mark(seq_bits, 0);
182
183 return 1;
184 }
185
186
187 return 0;
188}
189
190
191
192
193int 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}
202