1 /* Please avoid adding hacks here - instead add it to mac80211/backports.git */
3 #undef CONFIG_MODULE_STRIPPED
5 #include <linux/version.h> /* LINUX_VERSION_CODE */
6 #include <linux/types.h>
8 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 15, 0)
10 #include <linux/netdevice.h>
12 #define netdev_master_upper_dev_link(dev, upper_dev, upper_priv, upper_info, extack) ({\
13 BUILD_BUG_ON(extack != NULL); \
14 netdev_master_upper_dev_link(dev, upper_dev, upper_priv, upper_info); \
17 #endif /* < KERNEL_VERSION(4, 15, 0) */
20 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 16, 0)
23 #define sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER))
26 #endif /* < KERNEL_VERSION(4, 16, 0) */
29 #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 1, 0)
31 #include_next <linux/igmp.h>
32 #include_next <net/addrconf.h>
34 static inline int batadv_ipv6_mc_check_mld1(struct sk_buff
*skb
)
36 return ipv6_mc_check_mld(skb
, NULL
);
39 static inline int batadv_ipv6_mc_check_mld2(struct sk_buff
*skb
,
40 struct sk_buff
**skb_trimmed
)
42 return ipv6_mc_check_mld(skb
, skb_trimmed
);
45 #define ipv6_mc_check_mld_get(_1, _2, ipv6_mc_check_mld_name, ...) ipv6_mc_check_mld_name
46 #define ipv6_mc_check_mld(...) \
47 ipv6_mc_check_mld_get(__VA_ARGS__, batadv_ipv6_mc_check_mld2, batadv_ipv6_mc_check_mld1)(__VA_ARGS__)
49 static inline int batadv_ip_mc_check_igmp1(struct sk_buff
*skb
)
51 return ip_mc_check_igmp(skb
, NULL
);
54 static inline int batadv_ip_mc_check_igmp2(struct sk_buff
*skb
,
55 struct sk_buff
**skb_trimmed
)
57 return ip_mc_check_igmp(skb
, skb_trimmed
);
60 #define ip_mc_check_igmp_get(_1, _2, ip_mc_check_igmp_name, ...) ip_mc_check_igmp_name
61 #define ip_mc_check_igmp(...) \
62 ip_mc_check_igmp_get(__VA_ARGS__, batadv_ip_mc_check_igmp2, batadv_ip_mc_check_igmp1)(__VA_ARGS__)
64 #endif /* < KERNEL_VERSION(5, 1, 0) */
67 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 15, 0)
69 #define batadv_softif_slave_add(__dev, __slave_dev, __extack) \
70 batadv_softif_slave_add(__dev, __slave_dev)
72 #endif /* < KERNEL_VERSION(4, 15, 0) */
75 #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 0, 0)
77 static inline int batadv_access_ok(int type
, const void __user
*p
,
80 return access_ok(type
, p
, size
);
87 #define access_ok_get(_1, _2, _3 , access_ok_name, ...) access_ok_name
88 #define access_ok(...) \
89 access_ok_get(__VA_ARGS__, access_ok3, access_ok2)(__VA_ARGS__)
91 #define access_ok2(addr, size) batadv_access_ok(VERIFY_WRITE, (addr), (size))
92 #define access_ok3(type, addr, size) batadv_access_ok((type), (addr), (size))
94 #endif /* < KERNEL_VERSION(5, 0, 0) */
96 #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 4, 0)
99 #if __GNUC__ > 7 && !defined(__CHECKER__)
100 # define fallthrough __attribute__((__fallthrough__))
102 # define fallthrough do {} while (0) /* fallthrough */
106 #endif /* < KERNEL_VERSION(5, 4, 0) */
110 #include <linux/version.h>
111 #include_next <linux/average.h>
113 #include <linux/bug.h>
117 #endif /* DECLARE_EWMA */
120 * Exponentially weighted moving average (EWMA)
122 * This implements a fixed-precision EWMA algorithm, with both the
123 * precision and fall-off coefficient determined at compile-time
124 * and built into the generated helper funtions.
126 * The first argument to the macro is the name that will be used
127 * for the struct and helper functions.
129 * The second argument, the precision, expresses how many bits are
130 * used for the fractional part of the fixed-precision values.
132 * The third argument, the weight reciprocal, determines how the
133 * new values will be weighed vs. the old state, new values will
134 * get weight 1/weight_rcp and old values 1-1/weight_rcp. Note
135 * that this parameter must be a power of two for efficiency.
138 #define DECLARE_EWMA(name, _precision, _weight_rcp) \
139 struct ewma_##name { \
140 unsigned long internal; \
142 static inline void ewma_##name##_init(struct ewma_##name *e) \
144 BUILD_BUG_ON(!__builtin_constant_p(_precision)); \
145 BUILD_BUG_ON(!__builtin_constant_p(_weight_rcp)); \
147 * Even if you want to feed it just 0/1 you should have \
148 * some bits for the non-fractional part... \
150 BUILD_BUG_ON((_precision) > 30); \
151 BUILD_BUG_ON_NOT_POWER_OF_2(_weight_rcp); \
154 static inline unsigned long \
155 ewma_##name##_read(struct ewma_##name *e) \
157 BUILD_BUG_ON(!__builtin_constant_p(_precision)); \
158 BUILD_BUG_ON(!__builtin_constant_p(_weight_rcp)); \
159 BUILD_BUG_ON((_precision) > 30); \
160 BUILD_BUG_ON_NOT_POWER_OF_2(_weight_rcp); \
161 return e->internal >> (_precision); \
163 static inline void ewma_##name##_add(struct ewma_##name *e, \
166 unsigned long internal = READ_ONCE(e->internal); \
167 unsigned long weight_rcp = ilog2(_weight_rcp); \
168 unsigned long precision = _precision; \
170 BUILD_BUG_ON(!__builtin_constant_p(_precision)); \
171 BUILD_BUG_ON(!__builtin_constant_p(_weight_rcp)); \
172 BUILD_BUG_ON((_precision) > 30); \
173 BUILD_BUG_ON_NOT_POWER_OF_2(_weight_rcp); \
175 WRITE_ONCE(e->internal, internal ? \
176 (((internal << weight_rcp) - internal) + \
177 (val << precision)) >> weight_rcp : \
178 (val << precision)); \
181 /* </DECLARE_EWMA> */