![Simon Wunderlich](/assets/img/avatar_default.png)
BATMAN and broadcast packets are tracked with a sequence number window of currently 64 entries to measure and avoid duplicates. Packets which have a sequence number smaller than the newest received packet minus 64 are not within this sequence number window anymore and are called "old packets" from now on. When old packets are received, the routing code assumes that the host of the originator has been restarted. This assumption however might be wrong as packets can also be delayed by NIC drivers, e.g. because of long queues or collision detection in dense WiFi? environments. This behaviour can be reproduced by doing a broadcast ping flood in a dense node environment. The effect is that the sequence number window is jumping forth and back, accepting and forwarding any packet (because packets are assumed to be "new") and causing loops. To overcome this problem, the sequence number handling has been reorganized. When an old packet is received, the window is reset back only once. Other old packets are dropped for (currently) 30 seconds to "protect" the new sequence number and avoid the hopping as described above. The reorganization brings some code cleanups (at least i hope you feel the same) and also fixes a bug in count_real_packets() which falsely updated the last_real_seqno for slightly older packets within the seqno window if they are no duplicates. This second version of the patch also fixes a problem where for seq_diff==64 bit_shift() reads from outside of the seqno window, and removes the loop for seq_diff == -64 which was present in the first patch. The third iteration also adds a window for the next expected sequence numbers. This minimizes sequence number flapping for packets with very big differences (e.g. 3 packets with seqno 0, 25000 and 50000 might still cause problems without this window). Signed-off-by: Simon Wunderlich <siwu@hrz.tu-chemnitz.de> Signed-off-by: Andrew Lunn <andrew@lunn.ch> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
205 lines
5.4 KiB
C
205 lines
5.4 KiB
C
/*
|
|
* Copyright (C) 2006-2010 B.A.T.M.A.N. contributors:
|
|
*
|
|
* Simon Wunderlich, Marek Lindner
|
|
*
|
|
* This program is free software; you can redistribute it and/or
|
|
* modify it under the terms of version 2 of the GNU General Public
|
|
* License as published by the Free Software Foundation.
|
|
*
|
|
* This program is distributed in the hope that it will be useful, but
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
* General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program; if not, write to the Free Software
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
* 02110-1301, USA
|
|
*
|
|
*/
|
|
|
|
#include "main.h"
|
|
#include "bitarray.h"
|
|
|
|
/* returns true if the corresponding bit in the given seq_bits indicates true
|
|
* and curr_seqno is within range of last_seqno */
|
|
uint8_t get_bit_status(TYPE_OF_WORD *seq_bits, uint16_t last_seqno,
|
|
uint16_t curr_seqno)
|
|
{
|
|
int16_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 (seq_bits[word_num] & 1 << word_offset)
|
|
return 1;
|
|
else
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
/* turn corresponding bit on, so we can remember that we got the packet */
|
|
void bit_mark(TYPE_OF_WORD *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;
|
|
|
|
seq_bits[word_num] |= 1 << word_offset; /* turn the position on */
|
|
}
|
|
|
|
/* shift the packet array by n places. */
|
|
void bit_shift(TYPE_OF_WORD *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
|
|
* it's position */
|
|
/* for our example that would be: word[0] = 9800 + 0076 =
|
|
* 9876 */
|
|
}
|
|
/* now for our last word, i==word_num, we only have the it's "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(TYPE_OF_WORD *seq_bits)
|
|
{
|
|
int i;
|
|
for (i = 0; i < NUM_WORDS; i++)
|
|
seq_bits[i] = 0;
|
|
}
|
|
|
|
|
|
/* receive and process one packet within the sequence number window.
|
|
*
|
|
* returns:
|
|
* 1 if the window was moved (either new or very old)
|
|
* 0 if the window was not moved/shifted.
|
|
*/
|
|
char bit_get_packet(TYPE_OF_WORD *seq_bits, int16_t seq_num_diff,
|
|
int8_t set_mark)
|
|
{
|
|
/* sequence number is slightly older. We already got a sequence number
|
|
* higher than this one, so we just mark it. */
|
|
|
|
if ((seq_num_diff <= 0) && (seq_num_diff > -TQ_LOCAL_WINDOW_SIZE)) {
|
|
if (set_mark)
|
|
bit_mark(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);
|
|
|
|
if (set_mark)
|
|
bit_mark(seq_bits, 0);
|
|
return 1;
|
|
}
|
|
|
|
/* sequence number is much newer, probably missed a lot of packets */
|
|
|
|
if ((seq_num_diff >= TQ_LOCAL_WINDOW_SIZE)
|
|
|| (seq_num_diff < EXPECTED_SEQNO_RANGE)) {
|
|
bat_dbg(DBG_BATMAN,
|
|
"We missed a lot of packets (%i) !\n",
|
|
seq_num_diff - 1);
|
|
bit_reset_window(seq_bits);
|
|
if (set_mark)
|
|
bit_mark(seq_bits, 0);
|
|
return 1;
|
|
}
|
|
|
|
/* received a much older packet. The other host either restarted
|
|
* or the old packet got delayed somewhere in the network. The
|
|
* packet should be dropped without calling this function if the
|
|
* seqno window is protected. */
|
|
|
|
if ((seq_num_diff <= -TQ_LOCAL_WINDOW_SIZE)
|
|
|| (seq_num_diff >= EXPECTED_SEQNO_RANGE)) {
|
|
|
|
bat_dbg(DBG_BATMAN,
|
|
"Other host probably restarted!\n");
|
|
|
|
bit_reset_window(seq_bits);
|
|
if (set_mark)
|
|
bit_mark(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. The inner loop uses the Kernighan algorithm, see
|
|
* http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetKernighan
|
|
*/
|
|
int bit_packet_count(TYPE_OF_WORD *seq_bits)
|
|
{
|
|
int i, hamming = 0;
|
|
TYPE_OF_WORD word;
|
|
|
|
for (i = 0; i < NUM_WORDS; i++) {
|
|
word = seq_bits[i];
|
|
|
|
while (word) {
|
|
word &= word-1;
|
|
hamming++;
|
|
}
|
|
}
|
|
return hamming;
|
|
}
|