bitarray.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. /* shift the packet array by n places. */
  25. static void bat_bitmap_shift_left(unsigned long *seq_bits, int32_t n)
  26. {
  27. if (n <= 0 || n >= TQ_LOCAL_WINDOW_SIZE)
  28. return;
  29. bitmap_shift_left(seq_bits, seq_bits, n, TQ_LOCAL_WINDOW_SIZE);
  30. }
  31. /* receive and process one packet within the sequence number window.
  32. *
  33. * returns:
  34. * 1 if the window was moved (either new or very old)
  35. * 0 if the window was not moved/shifted.
  36. */
  37. int bit_get_packet(void *priv, unsigned long *seq_bits,
  38. int32_t seq_num_diff, int set_mark)
  39. {
  40. struct bat_priv *bat_priv = priv;
  41. /* sequence number is slightly older. We already got a sequence number
  42. * higher than this one, so we just mark it. */
  43. if ((seq_num_diff <= 0) && (seq_num_diff > -TQ_LOCAL_WINDOW_SIZE)) {
  44. if (set_mark)
  45. bat_set_bit(seq_bits, -seq_num_diff);
  46. return 0;
  47. }
  48. /* sequence number is slightly newer, so we shift the window and
  49. * set the mark if required */
  50. if ((seq_num_diff > 0) && (seq_num_diff < TQ_LOCAL_WINDOW_SIZE)) {
  51. bat_bitmap_shift_left(seq_bits, seq_num_diff);
  52. if (set_mark)
  53. bat_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 >= TQ_LOCAL_WINDOW_SIZE) &&
  58. (seq_num_diff < EXPECTED_SEQNO_RANGE)) {
  59. bat_dbg(DBG_BATMAN, bat_priv,
  60. "We missed a lot of packets (%i) !\n",
  61. seq_num_diff - 1);
  62. bitmap_zero(seq_bits, TQ_LOCAL_WINDOW_SIZE);
  63. if (set_mark)
  64. bat_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. if ((seq_num_diff <= -TQ_LOCAL_WINDOW_SIZE) ||
  72. (seq_num_diff >= EXPECTED_SEQNO_RANGE)) {
  73. bat_dbg(DBG_BATMAN, bat_priv,
  74. "Other host probably restarted!\n");
  75. bitmap_zero(seq_bits, TQ_LOCAL_WINDOW_SIZE);
  76. if (set_mark)
  77. bat_set_bit(seq_bits, 0);
  78. return 1;
  79. }
  80. /* never reached */
  81. return 0;
  82. }