/*1* Copyright (C) 2006-2011 B.A.T.M.A.N. contributors:2*3* Simon Wunderlich, Marek Lindner4*5* This program is free software; you can redistribute it and/or6* modify it under the terms of version 2 of the GNU General Public7* License as published by the Free Software Foundation.8*9* This program is distributed in the hope that it will be useful, but10* WITHOUT ANY WARRANTY; without even the implied warranty of11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU12* General Public License for more details.13*14* You should have received a copy of the GNU General Public License15* along with this program; if not, write to the Free Software16* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA17* 02110-1301, USA18*19*/2021#include "main.h"22#include "bitarray.h"2324#include <linux/bitops.h>2526/* returns true if the corresponding bit in the given seq_bits indicates true27* and curr_seqno is within range of last_seqno */28uint8_t get_bit_status(unsigned long *seq_bits, uint32_t last_seqno,29uint32_t curr_seqno)30{31int32_t diff, word_offset, word_num;3233diff = last_seqno - curr_seqno;34if (diff < 0 || diff >= TQ_LOCAL_WINDOW_SIZE) {35return 0;36} else {37/* which word */38word_num = (last_seqno - curr_seqno) / WORD_BIT_SIZE;39/* which position in the selected word */40word_offset = (last_seqno - curr_seqno) % WORD_BIT_SIZE;4142if (test_bit(word_offset, &seq_bits[word_num]))43return 1;44else45return 0;46}47}4849/* turn corresponding bit on, so we can remember that we got the packet */50void bit_mark(unsigned long *seq_bits, int32_t n)51{52int32_t word_offset, word_num;5354/* if too old, just drop it */55if (n < 0 || n >= TQ_LOCAL_WINDOW_SIZE)56return;5758/* which word */59word_num = n / WORD_BIT_SIZE;60/* which position in the selected word */61word_offset = n % WORD_BIT_SIZE;6263set_bit(word_offset, &seq_bits[word_num]); /* turn the position on */64}6566/* shift the packet array by n places. */67static void bit_shift(unsigned long *seq_bits, int32_t n)68{69int32_t word_offset, word_num;70int32_t i;7172if (n <= 0 || n >= TQ_LOCAL_WINDOW_SIZE)73return;7475word_offset = n % WORD_BIT_SIZE;/* shift how much inside each word */76word_num = n / WORD_BIT_SIZE; /* shift over how much (full) words */7778for (i = NUM_WORDS - 1; i > word_num; i--) {79/* going from old to new, so we don't overwrite the data we copy80* from.81*82* left is high, right is low: FEDC BA98 7654 321083* ^^ ^^84* vvvv85* ^^^^ = from, vvvvv =to, we'd have word_num==1 and86* word_offset==WORD_BIT_SIZE/2 ????? in this example.87* (=24 bits)88*89* our desired output would be: 9876 5432 1000 000090* */9192seq_bits[i] =93(seq_bits[i - word_num] << word_offset) +94/* take the lower port from the left half, shift it left95* to its final position */96(seq_bits[i - word_num - 1] >>97(WORD_BIT_SIZE-word_offset));98/* and the upper part of the right half and shift it left to99* it's position */100/* for our example that would be: word[0] = 9800 + 0076 =101* 9876 */102}103/* now for our last word, i==word_num, we only have the it's "left"104* half. that's the 1000 word in our example.*/105106seq_bits[i] = (seq_bits[i - word_num] << word_offset);107108/* pad the rest with 0, if there is anything */109i--;110111for (; i >= 0; i--)112seq_bits[i] = 0;113}114115static void bit_reset_window(unsigned long *seq_bits)116{117int i;118for (i = 0; i < NUM_WORDS; i++)119seq_bits[i] = 0;120}121122123/* receive and process one packet within the sequence number window.124*125* returns:126* 1 if the window was moved (either new or very old)127* 0 if the window was not moved/shifted.128*/129char bit_get_packet(void *priv, unsigned long *seq_bits,130int32_t seq_num_diff, int8_t set_mark)131{132struct bat_priv *bat_priv = (struct bat_priv *)priv;133134/* sequence number is slightly older. We already got a sequence number135* higher than this one, so we just mark it. */136137if ((seq_num_diff <= 0) && (seq_num_diff > -TQ_LOCAL_WINDOW_SIZE)) {138if (set_mark)139bit_mark(seq_bits, -seq_num_diff);140return 0;141}142143/* sequence number is slightly newer, so we shift the window and144* set the mark if required */145146if ((seq_num_diff > 0) && (seq_num_diff < TQ_LOCAL_WINDOW_SIZE)) {147bit_shift(seq_bits, seq_num_diff);148149if (set_mark)150bit_mark(seq_bits, 0);151return 1;152}153154/* sequence number is much newer, probably missed a lot of packets */155156if ((seq_num_diff >= TQ_LOCAL_WINDOW_SIZE)157|| (seq_num_diff < EXPECTED_SEQNO_RANGE)) {158bat_dbg(DBG_BATMAN, bat_priv,159"We missed a lot of packets (%i) !\n",160seq_num_diff - 1);161bit_reset_window(seq_bits);162if (set_mark)163bit_mark(seq_bits, 0);164return 1;165}166167/* received a much older packet. The other host either restarted168* or the old packet got delayed somewhere in the network. The169* packet should be dropped without calling this function if the170* seqno window is protected. */171172if ((seq_num_diff <= -TQ_LOCAL_WINDOW_SIZE)173|| (seq_num_diff >= EXPECTED_SEQNO_RANGE)) {174175bat_dbg(DBG_BATMAN, bat_priv,176"Other host probably restarted!\n");177178bit_reset_window(seq_bits);179if (set_mark)180bit_mark(seq_bits, 0);181182return 1;183}184185/* never reached */186return 0;187}188189/* count the hamming weight, how many good packets did we receive? just count190* the 1's.191*/192int bit_packet_count(unsigned long *seq_bits)193{194int i, hamming = 0;195196for (i = 0; i < NUM_WORDS; i++)197hamming += hweight_long(seq_bits[i]);198199return hamming;200}201202203