bitarray.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright (C) 2006-2012 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. #include "main.h"
  22. #include "bitarray.h"
  23. #include <linux/bitops.h>
  24. /* returns true if the corresponding bit in the given seq_bits indicates true
  25. * and curr_seqno is within range of last_seqno */
  26. int get_bit_status(const unsigned long *seq_bits, uint32_t last_seqno,
  27. uint32_t curr_seqno)
  28. {
  29. int32_t diff, word_offset, word_num;
  30. diff = last_seqno - curr_seqno;
  31. if (diff < 0 || diff >= TQ_LOCAL_WINDOW_SIZE) {
  32. return 0;
  33. } else {
  34. /* which word */
  35. word_num = (last_seqno - curr_seqno) / WORD_BIT_SIZE;
  36. /* which position in the selected word */
  37. word_offset = (last_seqno - curr_seqno) % WORD_BIT_SIZE;
  38. if (test_bit(word_offset, &seq_bits[word_num]))
  39. return 1;
  40. else
  41. return 0;
  42. }
  43. }
  44. /* turn corresponding bit on, so we can remember that we got the packet */
  45. void bit_mark(unsigned long *seq_bits, int32_t n)
  46. {
  47. int32_t word_offset, word_num;
  48. /* if too old, just drop it */
  49. if (n < 0 || n >= TQ_LOCAL_WINDOW_SIZE)
  50. return;
  51. /* which word */
  52. word_num = n / WORD_BIT_SIZE;
  53. /* which position in the selected word */
  54. word_offset = n % WORD_BIT_SIZE;
  55. set_bit(word_offset, &seq_bits[word_num]); /* turn the position on */
  56. }
  57. /* shift the packet array by n places. */
  58. static void bit_shift(unsigned long *seq_bits, int32_t n)
  59. {
  60. int32_t word_offset, word_num;
  61. int32_t i;
  62. if (n <= 0 || n >= TQ_LOCAL_WINDOW_SIZE)
  63. return;
  64. word_offset = n % WORD_BIT_SIZE;/* shift how much inside each word */
  65. word_num = n / WORD_BIT_SIZE; /* shift over how much (full) words */
  66. for (i = NUM_WORDS - 1; i > word_num; i--) {
  67. /* going from old to new, so we don't overwrite the data we copy
  68. * from.
  69. *
  70. * left is high, right is low: FEDC BA98 7654 3210
  71. * ^^ ^^
  72. * vvvv
  73. * ^^^^ = from, vvvvv =to, we'd have word_num==1 and
  74. * word_offset==WORD_BIT_SIZE/2 ????? in this example.
  75. * (=24 bits)
  76. *
  77. * our desired output would be: 9876 5432 1000 0000
  78. * */
  79. seq_bits[i] =
  80. (seq_bits[i - word_num] << word_offset) +
  81. /* take the lower port from the left half, shift it left
  82. * to its final position */
  83. (seq_bits[i - word_num - 1] >>
  84. (WORD_BIT_SIZE-word_offset));
  85. /* and the upper part of the right half and shift it left to
  86. * its position */
  87. /* for our example that would be: word[0] = 9800 + 0076 =
  88. * 9876 */
  89. }
  90. /* now for our last word, i==word_num, we only have its "left" half.
  91. * that's the 1000 word in our example.*/
  92. seq_bits[i] = (seq_bits[i - word_num] << word_offset);
  93. /* pad the rest with 0, if there is anything */
  94. i--;
  95. for (; i >= 0; i--)
  96. seq_bits[i] = 0;
  97. }
  98. static void bit_reset_window(unsigned long *seq_bits)
  99. {
  100. int i;
  101. for (i = 0; i < NUM_WORDS; i++)
  102. seq_bits[i] = 0;
  103. }
  104. /* receive and process one packet within the sequence number window.
  105. *
  106. * returns:
  107. * 1 if the window was moved (either new or very old)
  108. * 0 if the window was not moved/shifted.
  109. */
  110. int bit_get_packet(void *priv, unsigned long *seq_bits,
  111. int32_t seq_num_diff, int set_mark)
  112. {
  113. struct bat_priv *bat_priv = priv;
  114. /* sequence number is slightly older. We already got a sequence number
  115. * higher than this one, so we just mark it. */
  116. if ((seq_num_diff <= 0) && (seq_num_diff > -TQ_LOCAL_WINDOW_SIZE)) {
  117. if (set_mark)
  118. bit_mark(seq_bits, -seq_num_diff);
  119. return 0;
  120. }
  121. /* sequence number is slightly newer, so we shift the window and
  122. * set the mark if required */
  123. if ((seq_num_diff > 0) && (seq_num_diff < TQ_LOCAL_WINDOW_SIZE)) {
  124. bit_shift(seq_bits, seq_num_diff);
  125. if (set_mark)
  126. bit_mark(seq_bits, 0);
  127. return 1;
  128. }
  129. /* sequence number is much newer, probably missed a lot of packets */
  130. if ((seq_num_diff >= TQ_LOCAL_WINDOW_SIZE) &&
  131. (seq_num_diff < EXPECTED_SEQNO_RANGE)) {
  132. bat_dbg(DBG_BATMAN, bat_priv,
  133. "We missed a lot of packets (%i) !\n",
  134. seq_num_diff - 1);
  135. bit_reset_window(seq_bits);
  136. if (set_mark)
  137. bit_mark(seq_bits, 0);
  138. return 1;
  139. }
  140. /* received a much older packet. The other host either restarted
  141. * or the old packet got delayed somewhere in the network. The
  142. * packet should be dropped without calling this function if the
  143. * seqno window is protected. */
  144. if ((seq_num_diff <= -TQ_LOCAL_WINDOW_SIZE) ||
  145. (seq_num_diff >= EXPECTED_SEQNO_RANGE)) {
  146. bat_dbg(DBG_BATMAN, bat_priv,
  147. "Other host probably restarted!\n");
  148. bit_reset_window(seq_bits);
  149. if (set_mark)
  150. bit_mark(seq_bits, 0);
  151. return 1;
  152. }
  153. /* never reached */
  154. return 0;
  155. }
  156. /* count the hamming weight, how many good packets did we receive? just count
  157. * the 1's.
  158. */
  159. int bit_packet_count(const unsigned long *seq_bits)
  160. {
  161. int i, hamming = 0;
  162. for (i = 0; i < NUM_WORDS; i++)
  163. hamming += hweight_long(seq_bits[i]);
  164. return hamming;
  165. }