^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* Copyright (C) 2006-2020 B.A.T.M.A.N. contributors:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Simon Wunderlich, Marek Lindner
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #ifndef _NET_BATMAN_ADV_BITARRAY_H_
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #define _NET_BATMAN_ADV_BITARRAY_H_
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include "main.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/bitops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/compiler.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/stddef.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * batadv_test_bit() - check if bit is set in the current window
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * @seq_bits: pointer to the sequence number receive packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * @last_seqno: latest sequence number in seq_bits
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * @curr_seqno: sequence number to test for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * Return: true if the corresponding bit in the given seq_bits indicates true
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * and curr_seqno is within range of last_seqno. Otherwise returns false.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) static inline bool batadv_test_bit(const unsigned long *seq_bits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) u32 last_seqno, u32 curr_seqno)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) s32 diff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) diff = last_seqno - curr_seqno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) if (diff < 0 || diff >= BATADV_TQ_LOCAL_WINDOW_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) return test_bit(diff, seq_bits) != 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * batadv_set_bit() - Turn corresponding bit on, so we can remember that we got
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * the packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * @seq_bits: bitmap of the packet receive window
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * @n: relative sequence number of newly received packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) static inline void batadv_set_bit(unsigned long *seq_bits, s32 n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) /* if too old, just drop it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) if (n < 0 || n >= BATADV_TQ_LOCAL_WINDOW_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) set_bit(n, seq_bits); /* turn the position on */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) bool batadv_bit_get_packet(void *priv, unsigned long *seq_bits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) s32 seq_num_diff, int set_mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #endif /* _NET_BATMAN_ADV_BITARRAY_H_ */