]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/batman-adv/bitarray.c
Merge branch 'fix/misc' into topic/misc
[karo-tx-linux.git] / drivers / staging / batman-adv / bitarray.c
1 /*
2  * Copyright (C) 2006-2010 B.A.T.M.A.N. contributors:
3  *
4  * Simon Wunderlich, Marek Lindner
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of version 2 of the GNU General Public
8  * License as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  * 02110-1301, USA
19  *
20  */
21
22 #include "main.h"
23 #include "bitarray.h"
24
25 /* returns true if the corresponding bit in the given seq_bits indicates true
26  * and curr_seqno is within range of last_seqno */
27 uint8_t get_bit_status(TYPE_OF_WORD *seq_bits, uint32_t last_seqno,
28                        uint32_t curr_seqno)
29 {
30         int32_t diff, word_offset, word_num;
31
32         diff = last_seqno - curr_seqno;
33         if (diff < 0 || diff >= TQ_LOCAL_WINDOW_SIZE) {
34                 return 0;
35         } else {
36                 /* which word */
37                 word_num = (last_seqno - curr_seqno) / WORD_BIT_SIZE;
38                 /* which position in the selected word */
39                 word_offset = (last_seqno - curr_seqno) % WORD_BIT_SIZE;
40
41                 if (seq_bits[word_num] & 1 << word_offset)
42                         return 1;
43                 else
44                         return 0;
45         }
46 }
47
48 /* turn corresponding bit on, so we can remember that we got the packet */
49 void bit_mark(TYPE_OF_WORD *seq_bits, int32_t n)
50 {
51         int32_t word_offset, word_num;
52
53         /* if too old, just drop it */
54         if (n < 0 || n >= TQ_LOCAL_WINDOW_SIZE)
55                 return;
56
57         /* which word */
58         word_num = n / WORD_BIT_SIZE;
59         /* which position in the selected word */
60         word_offset = n % WORD_BIT_SIZE;
61
62         seq_bits[word_num] |= 1 << word_offset; /* turn the position on */
63 }
64
65 /* shift the packet array by n places. */
66 static void bit_shift(TYPE_OF_WORD *seq_bits, int32_t n)
67 {
68         int32_t word_offset, word_num;
69         int32_t i;
70
71         if (n <= 0 || n >= TQ_LOCAL_WINDOW_SIZE)
72                 return;
73
74         word_offset = n % WORD_BIT_SIZE;/* shift how much inside each word */
75         word_num = n / WORD_BIT_SIZE;   /* shift over how much (full) words */
76
77         for (i = NUM_WORDS - 1; i > word_num; i--) {
78                 /* going from old to new, so we don't overwrite the data we copy
79                  * from.
80                  *
81                  * left is high, right is low: FEDC BA98 7654 3210
82                  *                                        ^^ ^^
83                  *                             vvvv
84                  * ^^^^ = from, vvvvv =to, we'd have word_num==1 and
85                  * word_offset==WORD_BIT_SIZE/2 ????? in this example.
86                  * (=24 bits)
87                  *
88                  * our desired output would be: 9876 5432 1000 0000
89                  * */
90
91                 seq_bits[i] =
92                         (seq_bits[i - word_num] << word_offset) +
93                         /* take the lower port from the left half, shift it left
94                          * to its final position */
95                         (seq_bits[i - word_num - 1] >>
96                          (WORD_BIT_SIZE-word_offset));
97                 /* and the upper part of the right half and shift it left to
98                  * it's position */
99                 /* for our example that would be: word[0] = 9800 + 0076 =
100                  * 9876 */
101         }
102         /* now for our last word, i==word_num, we only have the it's "left"
103          * half. that's the 1000 word in our example.*/
104
105         seq_bits[i] = (seq_bits[i - word_num] << word_offset);
106
107         /* pad the rest with 0, if there is anything */
108         i--;
109
110         for (; i >= 0; i--)
111                 seq_bits[i] = 0;
112 }
113
114 static void bit_reset_window(TYPE_OF_WORD *seq_bits)
115 {
116         int i;
117         for (i = 0; i < NUM_WORDS; i++)
118                 seq_bits[i] = 0;
119 }
120
121
122 /* receive and process one packet within the sequence number window.
123  *
124  * returns:
125  *  1 if the window was moved (either new or very old)
126  *  0 if the window was not moved/shifted.
127  */
128 char bit_get_packet(TYPE_OF_WORD *seq_bits, int32_t seq_num_diff,
129                     int8_t set_mark)
130 {
131         /* FIXME: each orig_node->batman_if will be attached to a softif */
132         struct bat_priv *bat_priv = netdev_priv(soft_device);
133
134         /* sequence number is slightly older. We already got a sequence number
135          * higher than this one, so we just mark it. */
136
137         if ((seq_num_diff <= 0) && (seq_num_diff > -TQ_LOCAL_WINDOW_SIZE)) {
138                 if (set_mark)
139                         bit_mark(seq_bits, -seq_num_diff);
140                 return 0;
141         }
142
143         /* sequence number is slightly newer, so we shift the window and
144          * set the mark if required */
145
146         if ((seq_num_diff > 0) && (seq_num_diff < TQ_LOCAL_WINDOW_SIZE)) {
147                 bit_shift(seq_bits, seq_num_diff);
148
149                 if (set_mark)
150                         bit_mark(seq_bits, 0);
151                 return 1;
152         }
153
154         /* sequence number is much newer, probably missed a lot of packets */
155
156         if ((seq_num_diff >= TQ_LOCAL_WINDOW_SIZE)
157                 || (seq_num_diff < EXPECTED_SEQNO_RANGE)) {
158                 bat_dbg(DBG_BATMAN, bat_priv,
159                         "We missed a lot of packets (%i) !\n",
160                         seq_num_diff - 1);
161                 bit_reset_window(seq_bits);
162                 if (set_mark)
163                         bit_mark(seq_bits, 0);
164                 return 1;
165         }
166
167         /* received a much older packet. The other host either restarted
168          * or the old packet got delayed somewhere in the network. The
169          * packet should be dropped without calling this function if the
170          * seqno window is protected. */
171
172         if ((seq_num_diff <= -TQ_LOCAL_WINDOW_SIZE)
173                 || (seq_num_diff >= EXPECTED_SEQNO_RANGE)) {
174
175                 bat_dbg(DBG_BATMAN, bat_priv,
176                         "Other host probably restarted!\n");
177
178                 bit_reset_window(seq_bits);
179                 if (set_mark)
180                         bit_mark(seq_bits, 0);
181
182                 return 1;
183         }
184
185         /* never reached */
186         return 0;
187 }
188
189 /* count the hamming weight, how many good packets did we receive? just count
190  * the 1's. The inner loop uses the Kernighan algorithm, see
191  * http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetKernighan
192  */
193 int bit_packet_count(TYPE_OF_WORD *seq_bits)
194 {
195         int i, hamming = 0;
196         TYPE_OF_WORD word;
197
198         for (i = 0; i < NUM_WORDS; i++) {
199                 word = seq_bits[i];
200
201                 while (word) {
202                         word &= word-1;
203                         hamming++;
204                 }
205         }
206         return hamming;
207 }