batman-adv: Drop support for kernel < 5.15
[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
33 #include <linux/if_vlan.h>
34
35 /* Prefer this version in TX path, instead of
36 * skb_reset_mac_header() + vlan_eth_hdr()
37 */
38 static inline struct vlan_ethhdr *skb_vlan_eth_hdr(const struct sk_buff *skb)
39 {
40 return (struct vlan_ethhdr *)skb->data;
41 }
42
43 #endif /* LINUX_VERSION_IS_LESS(6, 4, 0) */
44
45 /* <DECLARE_EWMA> */
46
47 #include <linux/version.h>
48 #include_next <linux/average.h>
49
50 #include <linux/bug.h>
51
52 #ifdef DECLARE_EWMA
53 #undef DECLARE_EWMA
54 #endif /* DECLARE_EWMA */
55
56 /*
57 * Exponentially weighted moving average (EWMA)
58 *
59 * This implements a fixed-precision EWMA algorithm, with both the
60 * precision and fall-off coefficient determined at compile-time
61 * and built into the generated helper funtions.
62 *
63 * The first argument to the macro is the name that will be used
64 * for the struct and helper functions.
65 *
66 * The second argument, the precision, expresses how many bits are
67 * used for the fractional part of the fixed-precision values.
68 *
69 * The third argument, the weight reciprocal, determines how the
70 * new values will be weighed vs. the old state, new values will
71 * get weight 1/weight_rcp and old values 1-1/weight_rcp. Note
72 * that this parameter must be a power of two for efficiency.
73 */
74
75 #define DECLARE_EWMA(name, _precision, _weight_rcp) \
76 struct ewma_##name { \
77 unsigned long internal; \
78 }; \
79 static inline void ewma_##name##_init(struct ewma_##name *e) \
80 { \
81 BUILD_BUG_ON(!__builtin_constant_p(_precision)); \
82 BUILD_BUG_ON(!__builtin_constant_p(_weight_rcp)); \
83 /* \
84 * Even if you want to feed it just 0/1 you should have \
85 * some bits for the non-fractional part... \
86 */ \
87 BUILD_BUG_ON((_precision) > 30); \
88 BUILD_BUG_ON_NOT_POWER_OF_2(_weight_rcp); \
89 e->internal = 0; \
90 } \
91 static inline unsigned long \
92 ewma_##name##_read(struct ewma_##name *e) \
93 { \
94 BUILD_BUG_ON(!__builtin_constant_p(_precision)); \
95 BUILD_BUG_ON(!__builtin_constant_p(_weight_rcp)); \
96 BUILD_BUG_ON((_precision) > 30); \
97 BUILD_BUG_ON_NOT_POWER_OF_2(_weight_rcp); \
98 return e->internal >> (_precision); \
99 } \
100 static inline void ewma_##name##_add(struct ewma_##name *e, \
101 unsigned long val) \
102 { \
103 unsigned long internal = READ_ONCE(e->internal); \
104 unsigned long weight_rcp = ilog2(_weight_rcp); \
105 unsigned long precision = _precision; \
106 \
107 BUILD_BUG_ON(!__builtin_constant_p(_precision)); \
108 BUILD_BUG_ON(!__builtin_constant_p(_weight_rcp)); \
109 BUILD_BUG_ON((_precision) > 30); \
110 BUILD_BUG_ON_NOT_POWER_OF_2(_weight_rcp); \
111 \
112 WRITE_ONCE(e->internal, internal ? \
113 (((internal << weight_rcp) - internal) + \
114 (val << precision)) >> weight_rcp : \
115 (val << precision)); \
116 }
117
118 /* </DECLARE_EWMA> */