Merge pull request #1038 from ecsv/batadv-2023.3
[feed/routing.git] / batman-adv / src / compat-hacks.h
1 /* Please avoid adding hacks here - instead add it to mac80211/backports.git */
2
3 #undef CONFIG_MODULE_STRIPPED
4
5 #include <linux/version.h> /* LINUX_VERSION_CODE */
6 #include <linux/types.h>
7
8 #if LINUX_VERSION_IS_LESS(6, 0, 0)
9
10 #define __vstring(item, fmt, ap) __dynamic_array(char, item, 256)
11 #define __assign_vstr(dst, fmt, va) \
12 WARN_ON_ONCE(vsnprintf(__get_dynamic_array(dst), 256, fmt, *va) >= 256)
13
14 #endif /* LINUX_VERSION_IS_LESS(6, 0, 0) */
15
16 #if LINUX_VERSION_IS_LESS(6, 2, 0)
17
18 #include <linux/random.h>
19
20 #define genl_split_ops genl_ops
21
22 static inline u32 batadv_get_random_u32_below(u32 ep_ro)
23 {
24 return prandom_u32_max(ep_ro);
25 }
26
27 #define get_random_u32_below batadv_get_random_u32_below
28
29 #endif /* LINUX_VERSION_IS_LESS(6, 2, 0) */
30
31 #if LINUX_VERSION_IS_LESS(6, 4, 0) && \
32 !(LINUX_VERSION_IS_GEQ(5, 10, 205) && LINUX_VERSION_IS_LESS(5, 11, 0)) && \
33 !(LINUX_VERSION_IS_GEQ(5, 15, 144) && LINUX_VERSION_IS_LESS(5, 16, 0)) && \
34 !(LINUX_VERSION_IS_GEQ(6, 1, 69) && LINUX_VERSION_IS_LESS(6, 2, 0))
35
36 #include <linux/if_vlan.h>
37
38 /* Prefer this version in TX path, instead of
39 * skb_reset_mac_header() + vlan_eth_hdr()
40 */
41 static inline struct vlan_ethhdr *skb_vlan_eth_hdr(const struct sk_buff *skb)
42 {
43 return (struct vlan_ethhdr *)skb->data;
44 }
45
46 #endif /* LINUX_VERSION_IS_LESS(6, 4, 0) */
47
48 /* <DECLARE_EWMA> */
49
50 #include <linux/version.h>
51 #include_next <linux/average.h>
52
53 #include <linux/bug.h>
54
55 #ifdef DECLARE_EWMA
56 #undef DECLARE_EWMA
57 #endif /* DECLARE_EWMA */
58
59 /*
60 * Exponentially weighted moving average (EWMA)
61 *
62 * This implements a fixed-precision EWMA algorithm, with both the
63 * precision and fall-off coefficient determined at compile-time
64 * and built into the generated helper funtions.
65 *
66 * The first argument to the macro is the name that will be used
67 * for the struct and helper functions.
68 *
69 * The second argument, the precision, expresses how many bits are
70 * used for the fractional part of the fixed-precision values.
71 *
72 * The third argument, the weight reciprocal, determines how the
73 * new values will be weighed vs. the old state, new values will
74 * get weight 1/weight_rcp and old values 1-1/weight_rcp. Note
75 * that this parameter must be a power of two for efficiency.
76 */
77
78 #define DECLARE_EWMA(name, _precision, _weight_rcp) \
79 struct ewma_##name { \
80 unsigned long internal; \
81 }; \
82 static inline void ewma_##name##_init(struct ewma_##name *e) \
83 { \
84 BUILD_BUG_ON(!__builtin_constant_p(_precision)); \
85 BUILD_BUG_ON(!__builtin_constant_p(_weight_rcp)); \
86 /* \
87 * Even if you want to feed it just 0/1 you should have \
88 * some bits for the non-fractional part... \
89 */ \
90 BUILD_BUG_ON((_precision) > 30); \
91 BUILD_BUG_ON_NOT_POWER_OF_2(_weight_rcp); \
92 e->internal = 0; \
93 } \
94 static inline unsigned long \
95 ewma_##name##_read(struct ewma_##name *e) \
96 { \
97 BUILD_BUG_ON(!__builtin_constant_p(_precision)); \
98 BUILD_BUG_ON(!__builtin_constant_p(_weight_rcp)); \
99 BUILD_BUG_ON((_precision) > 30); \
100 BUILD_BUG_ON_NOT_POWER_OF_2(_weight_rcp); \
101 return e->internal >> (_precision); \
102 } \
103 static inline void ewma_##name##_add(struct ewma_##name *e, \
104 unsigned long val) \
105 { \
106 unsigned long internal = READ_ONCE(e->internal); \
107 unsigned long weight_rcp = ilog2(_weight_rcp); \
108 unsigned long precision = _precision; \
109 \
110 BUILD_BUG_ON(!__builtin_constant_p(_precision)); \
111 BUILD_BUG_ON(!__builtin_constant_p(_weight_rcp)); \
112 BUILD_BUG_ON((_precision) > 30); \
113 BUILD_BUG_ON_NOT_POWER_OF_2(_weight_rcp); \
114 \
115 WRITE_ONCE(e->internal, internal ? \
116 (((internal << weight_rcp) - internal) + \
117 (val << precision)) >> weight_rcp : \
118 (val << precision)); \
119 }
120
121 /* </DECLARE_EWMA> */