bitarray.c 2.8 KB

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