bitarray.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (C) B.A.T.M.A.N. contributors:
  3. *
  4. * Simon Wunderlich, Marek Lindner
  5. */
  6. #include "bitarray.h"
  7. #include "main.h"
  8. #include <linux/bitmap.h>
  9. #include "log.h"
  10. /* shift the packet array by n places. */
  11. static void batadv_bitmap_shift_left(unsigned long *seq_bits, s32 n)
  12. {
  13. if (n <= 0 || n >= BATADV_TQ_LOCAL_WINDOW_SIZE)
  14. return;
  15. bitmap_shift_left(seq_bits, seq_bits, n, BATADV_TQ_LOCAL_WINDOW_SIZE);
  16. }
  17. /**
  18. * batadv_bit_get_packet() - receive and process one packet within the sequence
  19. * number window
  20. * @priv: the bat priv with all the mesh interface information
  21. * @seq_bits: pointer to the sequence number receive packet
  22. * @seq_num_diff: difference between the current/received sequence number and
  23. * the last sequence number
  24. * @set_mark: whether this packet should be marked in seq_bits
  25. *
  26. * Return: true if the window was moved (either new or very old),
  27. * false if the window was not moved/shifted.
  28. */
  29. bool batadv_bit_get_packet(void *priv, unsigned long *seq_bits,
  30. s32 seq_num_diff, int set_mark)
  31. {
  32. struct batadv_priv *bat_priv = priv;
  33. /* sequence number is slightly older. We already got a sequence number
  34. * higher than this one, so we just mark it.
  35. */
  36. if (seq_num_diff <= 0 && seq_num_diff > -BATADV_TQ_LOCAL_WINDOW_SIZE) {
  37. if (set_mark)
  38. batadv_set_bit(seq_bits, -seq_num_diff);
  39. return false;
  40. }
  41. /* sequence number is slightly newer, so we shift the window and
  42. * set the mark if required
  43. */
  44. if (seq_num_diff > 0 && seq_num_diff < BATADV_TQ_LOCAL_WINDOW_SIZE) {
  45. batadv_bitmap_shift_left(seq_bits, seq_num_diff);
  46. if (set_mark)
  47. batadv_set_bit(seq_bits, 0);
  48. return true;
  49. }
  50. /* sequence number is much newer, probably missed a lot of packets */
  51. if (seq_num_diff >= BATADV_TQ_LOCAL_WINDOW_SIZE &&
  52. seq_num_diff < BATADV_EXPECTED_SEQNO_RANGE) {
  53. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  54. "We missed a lot of packets (%i) !\n",
  55. seq_num_diff - 1);
  56. bitmap_zero(seq_bits, BATADV_TQ_LOCAL_WINDOW_SIZE);
  57. if (set_mark)
  58. batadv_set_bit(seq_bits, 0);
  59. return true;
  60. }
  61. /* received a much older packet. The other host either restarted
  62. * or the old packet got delayed somewhere in the network. The
  63. * packet should be dropped without calling this function if the
  64. * seqno window is protected.
  65. *
  66. * seq_num_diff <= -BATADV_TQ_LOCAL_WINDOW_SIZE
  67. * or
  68. * seq_num_diff >= BATADV_EXPECTED_SEQNO_RANGE
  69. */
  70. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  71. "Other host probably restarted!\n");
  72. bitmap_zero(seq_bits, BATADV_TQ_LOCAL_WINDOW_SIZE);
  73. if (set_mark)
  74. batadv_set_bit(seq_bits, 0);
  75. return true;
  76. }