bitarray.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* Copyright (C) 2006-2015 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. /* shift the packet array by n places. */
  21. static void batadv_bitmap_shift_left(unsigned long *seq_bits, s32 n)
  22. {
  23. if (n <= 0 || n >= BATADV_TQ_LOCAL_WINDOW_SIZE)
  24. return;
  25. bitmap_shift_left(seq_bits, seq_bits, n, BATADV_TQ_LOCAL_WINDOW_SIZE);
  26. }
  27. /* receive and process one packet within the sequence number window.
  28. *
  29. * returns:
  30. * 1 if the window was moved (either new or very old)
  31. * 0 if the window was not moved/shifted.
  32. */
  33. int batadv_bit_get_packet(void *priv, unsigned long *seq_bits, s32 seq_num_diff,
  34. int set_mark)
  35. {
  36. struct batadv_priv *bat_priv = priv;
  37. /* sequence number is slightly older. We already got a sequence number
  38. * higher than this one, so we just mark it.
  39. */
  40. if (seq_num_diff <= 0 && seq_num_diff > -BATADV_TQ_LOCAL_WINDOW_SIZE) {
  41. if (set_mark)
  42. batadv_set_bit(seq_bits, -seq_num_diff);
  43. return 0;
  44. }
  45. /* sequence number is slightly newer, so we shift the window and
  46. * set the mark if required
  47. */
  48. if (seq_num_diff > 0 && seq_num_diff < BATADV_TQ_LOCAL_WINDOW_SIZE) {
  49. batadv_bitmap_shift_left(seq_bits, seq_num_diff);
  50. if (set_mark)
  51. batadv_set_bit(seq_bits, 0);
  52. return 1;
  53. }
  54. /* sequence number is much newer, probably missed a lot of packets */
  55. if (seq_num_diff >= BATADV_TQ_LOCAL_WINDOW_SIZE &&
  56. seq_num_diff < BATADV_EXPECTED_SEQNO_RANGE) {
  57. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  58. "We missed a lot of packets (%i) !\n",
  59. seq_num_diff - 1);
  60. bitmap_zero(seq_bits, BATADV_TQ_LOCAL_WINDOW_SIZE);
  61. if (set_mark)
  62. batadv_set_bit(seq_bits, 0);
  63. return 1;
  64. }
  65. /* received a much older packet. The other host either restarted
  66. * or the old packet got delayed somewhere in the network. The
  67. * packet should be dropped without calling this function if the
  68. * seqno window is protected.
  69. *
  70. * seq_num_diff <= -BATADV_TQ_LOCAL_WINDOW_SIZE
  71. * or
  72. * seq_num_diff >= BATADV_EXPECTED_SEQNO_RANGE
  73. */
  74. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  75. "Other host probably restarted!\n");
  76. bitmap_zero(seq_bits, BATADV_TQ_LOCAL_WINDOW_SIZE);
  77. if (set_mark)
  78. batadv_set_bit(seq_bits, 0);
  79. return 1;
  80. }