ddd86710c38b0d3a6b85783739fb327e474548ab
[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(5, 10, 0)
9
10 #include <linux/if_bridge.h>
11
12 struct batadv_br_ip {
13 union {
14 __be32 ip4;
15 #if IS_ENABLED(CONFIG_IPV6)
16 struct in6_addr ip6;
17 #endif
18 } dst;
19 __be16 proto;
20 __u16 vid;
21 };
22
23 struct batadv_br_ip_list {
24 struct list_head list;
25 struct batadv_br_ip addr;
26 };
27
28 #if 0
29 /* "static" dropped to force compiler to evaluate it as part of multicast.c
30 * might need to be added again and then called in some kind of dummy
31 * compat.c in case this header is included in multiple files.
32 */
33 inline void __batadv_br_ip_list_check(void)
34 {
35 BUILD_BUG_ON(sizeof(struct batadv_br_ip_list) != sizeof(struct br_ip_list));
36 BUILD_BUG_ON(offsetof(struct batadv_br_ip_list, list) != offsetof(struct br_ip_list, list));
37 BUILD_BUG_ON(offsetof(struct batadv_br_ip_list, addr) != offsetof(struct br_ip_list, addr));
38
39 BUILD_BUG_ON(sizeof(struct batadv_br_ip) != sizeof(struct br_ip));
40 BUILD_BUG_ON(offsetof(struct batadv_br_ip, dst.ip4) != offsetof(struct br_ip, u.ip4));
41 BUILD_BUG_ON(offsetof(struct batadv_br_ip, dst.ip6) != offsetof(struct br_ip, u.ip6));
42 BUILD_BUG_ON(offsetof(struct batadv_br_ip, proto) != offsetof(struct br_ip, proto));
43 BUILD_BUG_ON(offsetof(struct batadv_br_ip, vid) != offsetof(struct br_ip, vid));
44 }
45 #endif
46
47 #define br_ip batadv_br_ip
48 #define br_ip_list batadv_br_ip_list
49
50 #endif /* LINUX_VERSION_IS_LESS(5, 10, 0) */
51
52 #if LINUX_VERSION_IS_LESS(5, 14, 0)
53
54 #include <linux/if_bridge.h>
55 #include <net/addrconf.h>
56
57 #if IS_ENABLED(CONFIG_IPV6)
58 static inline bool
59 br_multicast_has_router_adjacent(struct net_device *dev, int proto)
60 {
61 struct list_head bridge_mcast_list = LIST_HEAD_INIT(bridge_mcast_list);
62 struct br_ip_list *br_ip_entry, *tmp;
63 int ret;
64
65 if (proto != ETH_P_IPV6)
66 return true;
67
68 ret = br_multicast_list_adjacent(dev, &bridge_mcast_list);
69 if (ret < 0)
70 return true;
71
72 ret = false;
73
74 list_for_each_entry_safe(br_ip_entry, tmp, &bridge_mcast_list, list) {
75 if (br_ip_entry->addr.proto == htons(ETH_P_IPV6) &&
76 ipv6_addr_is_ll_all_routers(&br_ip_entry->addr.dst.ip6))
77 ret = true;
78
79 list_del(&br_ip_entry->list);
80 kfree(br_ip_entry);
81 }
82
83 return ret;
84 }
85 #else
86 static inline bool
87 br_multicast_has_router_adjacent(struct net_device *dev, int proto)
88 {
89 return true;
90 }
91 #endif
92
93 #endif /* LINUX_VERSION_IS_LESS(5, 14, 0) */
94
95 #if LINUX_VERSION_IS_LESS(5, 15, 0)
96
97 static inline void batadv_dev_put(struct net_device *dev)
98 {
99 if (!dev)
100 return;
101
102 dev_put(dev);
103 }
104 #define dev_put batadv_dev_put
105
106 static inline void batadv_dev_hold(struct net_device *dev)
107 {
108 if (!dev)
109 return;
110
111 dev_hold(dev);
112 }
113 #define dev_hold batadv_dev_hold
114
115 static inline void batadv_eth_hw_addr_set(struct net_device *dev,
116 const u8 *addr)
117 {
118 ether_addr_copy(dev->dev_addr, addr);
119 }
120 #define eth_hw_addr_set batadv_eth_hw_addr_set
121
122 #endif /* LINUX_VERSION_IS_LESS(5, 15, 0) */
123
124 /* <DECLARE_EWMA> */
125
126 #include <linux/version.h>
127 #include_next <linux/average.h>
128
129 #include <linux/bug.h>
130
131 #ifdef DECLARE_EWMA
132 #undef DECLARE_EWMA
133 #endif /* DECLARE_EWMA */
134
135 /*
136 * Exponentially weighted moving average (EWMA)
137 *
138 * This implements a fixed-precision EWMA algorithm, with both the
139 * precision and fall-off coefficient determined at compile-time
140 * and built into the generated helper funtions.
141 *
142 * The first argument to the macro is the name that will be used
143 * for the struct and helper functions.
144 *
145 * The second argument, the precision, expresses how many bits are
146 * used for the fractional part of the fixed-precision values.
147 *
148 * The third argument, the weight reciprocal, determines how the
149 * new values will be weighed vs. the old state, new values will
150 * get weight 1/weight_rcp and old values 1-1/weight_rcp. Note
151 * that this parameter must be a power of two for efficiency.
152 */
153
154 #define DECLARE_EWMA(name, _precision, _weight_rcp) \
155 struct ewma_##name { \
156 unsigned long internal; \
157 }; \
158 static inline void ewma_##name##_init(struct ewma_##name *e) \
159 { \
160 BUILD_BUG_ON(!__builtin_constant_p(_precision)); \
161 BUILD_BUG_ON(!__builtin_constant_p(_weight_rcp)); \
162 /* \
163 * Even if you want to feed it just 0/1 you should have \
164 * some bits for the non-fractional part... \
165 */ \
166 BUILD_BUG_ON((_precision) > 30); \
167 BUILD_BUG_ON_NOT_POWER_OF_2(_weight_rcp); \
168 e->internal = 0; \
169 } \
170 static inline unsigned long \
171 ewma_##name##_read(struct ewma_##name *e) \
172 { \
173 BUILD_BUG_ON(!__builtin_constant_p(_precision)); \
174 BUILD_BUG_ON(!__builtin_constant_p(_weight_rcp)); \
175 BUILD_BUG_ON((_precision) > 30); \
176 BUILD_BUG_ON_NOT_POWER_OF_2(_weight_rcp); \
177 return e->internal >> (_precision); \
178 } \
179 static inline void ewma_##name##_add(struct ewma_##name *e, \
180 unsigned long val) \
181 { \
182 unsigned long internal = READ_ONCE(e->internal); \
183 unsigned long weight_rcp = ilog2(_weight_rcp); \
184 unsigned long precision = _precision; \
185 \
186 BUILD_BUG_ON(!__builtin_constant_p(_precision)); \
187 BUILD_BUG_ON(!__builtin_constant_p(_weight_rcp)); \
188 BUILD_BUG_ON((_precision) > 30); \
189 BUILD_BUG_ON_NOT_POWER_OF_2(_weight_rcp); \
190 \
191 WRITE_ONCE(e->internal, internal ? \
192 (((internal << weight_rcp) - internal) + \
193 (val << precision)) >> weight_rcp : \
194 (val << precision)); \
195 }
196
197 /* </DECLARE_EWMA> */