21c13f7c8fc992e9065976f878d6e407d848ebae
[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 <net/addrconf.h>
55
56 #if IS_ENABLED(CONFIG_IPV6)
57 static inline bool
58 br_multicast_has_router_adjacent(struct net_device *dev, int proto)
59 {
60 struct list_head bridge_mcast_list = LIST_HEAD_INIT(bridge_mcast_list);
61 struct br_ip_list *br_ip_entry, *tmp;
62 int ret;
63
64 if (proto != ETH_P_IPV6)
65 return true;
66
67 ret = br_multicast_list_adjacent(dev, &bridge_mcast_list);
68 if (ret < 0)
69 return true;
70
71 ret = false;
72
73 list_for_each_entry_safe(br_ip_entry, tmp, &bridge_mcast_list, list) {
74 if (br_ip_entry->addr.proto == htons(ETH_P_IPV6) &&
75 ipv6_addr_is_ll_all_routers(&br_ip_entry->addr.dst.ip6))
76 ret = true;
77
78 list_del(&br_ip_entry->list);
79 kfree(br_ip_entry);
80 }
81
82 return ret;
83 }
84 #else
85 static inline bool
86 br_multicast_has_router_adjacent(struct net_device *dev, int proto)
87 {
88 return true;
89 }
90 #endif
91
92 #endif /* LINUX_VERSION_IS_LESS(5, 14, 0) */
93
94 /* <DECLARE_EWMA> */
95
96 #include <linux/version.h>
97 #include_next <linux/average.h>
98
99 #include <linux/bug.h>
100
101 #ifdef DECLARE_EWMA
102 #undef DECLARE_EWMA
103 #endif /* DECLARE_EWMA */
104
105 /*
106 * Exponentially weighted moving average (EWMA)
107 *
108 * This implements a fixed-precision EWMA algorithm, with both the
109 * precision and fall-off coefficient determined at compile-time
110 * and built into the generated helper funtions.
111 *
112 * The first argument to the macro is the name that will be used
113 * for the struct and helper functions.
114 *
115 * The second argument, the precision, expresses how many bits are
116 * used for the fractional part of the fixed-precision values.
117 *
118 * The third argument, the weight reciprocal, determines how the
119 * new values will be weighed vs. the old state, new values will
120 * get weight 1/weight_rcp and old values 1-1/weight_rcp. Note
121 * that this parameter must be a power of two for efficiency.
122 */
123
124 #define DECLARE_EWMA(name, _precision, _weight_rcp) \
125 struct ewma_##name { \
126 unsigned long internal; \
127 }; \
128 static inline void ewma_##name##_init(struct ewma_##name *e) \
129 { \
130 BUILD_BUG_ON(!__builtin_constant_p(_precision)); \
131 BUILD_BUG_ON(!__builtin_constant_p(_weight_rcp)); \
132 /* \
133 * Even if you want to feed it just 0/1 you should have \
134 * some bits for the non-fractional part... \
135 */ \
136 BUILD_BUG_ON((_precision) > 30); \
137 BUILD_BUG_ON_NOT_POWER_OF_2(_weight_rcp); \
138 e->internal = 0; \
139 } \
140 static inline unsigned long \
141 ewma_##name##_read(struct ewma_##name *e) \
142 { \
143 BUILD_BUG_ON(!__builtin_constant_p(_precision)); \
144 BUILD_BUG_ON(!__builtin_constant_p(_weight_rcp)); \
145 BUILD_BUG_ON((_precision) > 30); \
146 BUILD_BUG_ON_NOT_POWER_OF_2(_weight_rcp); \
147 return e->internal >> (_precision); \
148 } \
149 static inline void ewma_##name##_add(struct ewma_##name *e, \
150 unsigned long val) \
151 { \
152 unsigned long internal = READ_ONCE(e->internal); \
153 unsigned long weight_rcp = ilog2(_weight_rcp); \
154 unsigned long precision = _precision; \
155 \
156 BUILD_BUG_ON(!__builtin_constant_p(_precision)); \
157 BUILD_BUG_ON(!__builtin_constant_p(_weight_rcp)); \
158 BUILD_BUG_ON((_precision) > 30); \
159 BUILD_BUG_ON_NOT_POWER_OF_2(_weight_rcp); \
160 \
161 WRITE_ONCE(e->internal, internal ? \
162 (((internal << weight_rcp) - internal) + \
163 (val << precision)) >> weight_rcp : \
164 (val << precision)); \
165 }
166
167 /* </DECLARE_EWMA> */