bitarray.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* Copyright (C) 2006-2016 B.A.T.M.A.N. contributors:
  2. *
  3. * Simon Wunderlich, Marek Lindner
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of version 2 of the GNU General Public
  7. * License as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "bitarray.h"
  18. #include "main.h"
  19. #include <linux/bitmap.h>
  20. #include "log.h"
  21. /* shift the packet array by n places. */
  22. static void batadv_bitmap_shift_left(unsigned long *seq_bits, s32 n)
  23. {
  24. if (n <= 0 || n >= BATADV_TQ_LOCAL_WINDOW_SIZE)
  25. return;
  26. bitmap_shift_left(seq_bits, seq_bits, n, BATADV_TQ_LOCAL_WINDOW_SIZE);
  27. }
  28. /**
  29. * batadv_bit_get_packet - receive and process one packet within the sequence
  30. * number window
  31. * @priv: the bat priv with all the soft interface information
  32. * @seq_bits: pointer to the sequence number receive packet
  33. * @seq_num_diff: difference between the current/received sequence number and
  34. * the last sequence number
  35. * @set_mark: whether this packet should be marked in seq_bits
  36. *
  37. * Return: true if the window was moved (either new or very old),
  38. * false if the window was not moved/shifted.
  39. */
  40. bool batadv_bit_get_packet(void *priv, unsigned long *seq_bits,
  41. s32 seq_num_diff, int set_mark)
  42. {
  43. struct batadv_priv *bat_priv = priv;
  44. /* sequence number is slightly older. We already got a sequence number
  45. * higher than this one, so we just mark it.
  46. */
  47. if (seq_num_diff <= 0 && seq_num_diff > -BATADV_TQ_LOCAL_WINDOW_SIZE) {
  48. if (set_mark)
  49. batadv_set_bit(seq_bits, -seq_num_diff);
  50. return false;
  51. }
  52. /* sequence number is slightly newer, so we shift the window and
  53. * set the mark if required
  54. */
  55. if (seq_num_diff > 0 && seq_num_diff < BATADV_TQ_LOCAL_WINDOW_SIZE) {
  56. batadv_bitmap_shift_left(seq_bits, seq_num_diff);
  57. if (set_mark)
  58. batadv_set_bit(seq_bits, 0);
  59. return true;
  60. }
  61. /* sequence number is much newer, probably missed a lot of packets */
  62. if (seq_num_diff >= BATADV_TQ_LOCAL_WINDOW_SIZE &&
  63. seq_num_diff < BATADV_EXPECTED_SEQNO_RANGE) {
  64. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  65. "We missed a lot of packets (%i) !\n",
  66. seq_num_diff - 1);
  67. bitmap_zero(seq_bits, BATADV_TQ_LOCAL_WINDOW_SIZE);
  68. if (set_mark)
  69. batadv_set_bit(seq_bits, 0);
  70. return true;
  71. }
  72. /* received a much older packet. The other host either restarted
  73. * or the old packet got delayed somewhere in the network. The
  74. * packet should be dropped without calling this function if the
  75. * seqno window is protected.
  76. *
  77. * seq_num_diff <= -BATADV_TQ_LOCAL_WINDOW_SIZE
  78. * or
  79. * seq_num_diff >= BATADV_EXPECTED_SEQNO_RANGE
  80. */
  81. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  82. "Other host probably restarted!\n");
  83. bitmap_zero(seq_bits, BATADV_TQ_LOCAL_WINDOW_SIZE);
  84. if (set_mark)
  85. batadv_set_bit(seq_bits, 0);
  86. return true;
  87. }