352246d592147109f32310fc652316d1fe6b4740
[openwrt/openwrt.git] / package / kernel / mac80211 / patches / subsys / 351-mac80211-add-TX_NEEDS_ALIGNED4_SKBS-hw-flag.patch
1 From: Janusz Dziedzic <janusz.dziedzic@tieto.com>
2 Date: Sun, 10 Mar 2019 17:22:08 +0100
3 Subject: [PATCH] mac80211: add TX_NEEDS_ALIGNED4_SKBS hw flag
4
5 The driver should set this flag if the hardware requires tx skb data
6 (starting with the LLC header) to be aligned to 4 bytes.
7
8 Padding is added after ieee80211_hdr, before IV/LLC.
9
10 Before this patch, we have to do memmove(hdrlen) twice in the driver:
11 Once before we pass this to HW and once again in tx completion
12 (to fix up the skb for monitor mode).
13
14 With this patch we can skip this memmove() and thus reduce CPU cycles in
15 the data path.
16
17 Signed-off-by: Janusz Dziedzic <janusz.dziedzic@tieto.com>
18 Signed-off-by: Felix Fietkau <nbd@nbd.name>
19 ---
20
21 --- a/include/net/mac80211.h
22 +++ b/include/net/mac80211.h
23 @@ -2134,6 +2134,9 @@ struct ieee80211_txq {
24 * @IEEE80211_HW_TX_STATUS_NO_AMPDU_LEN: Driver does not report accurate A-MPDU
25 * length in tx status information
26 *
27 + * @IEEE80211_HW_TX_NEEDS_ALIGNED4_SKBS: Driver need aligned skbs to four-byte.
28 + * Padding will be added after ieee80211_hdr, before IV/LLC.
29 + *
30 * @NUM_IEEE80211_HW_FLAGS: number of hardware flags, used for sizing arrays
31 */
32 enum ieee80211_hw_flags {
33 @@ -2180,6 +2183,7 @@ enum ieee80211_hw_flags {
34 IEEE80211_HW_DEAUTH_NEED_MGD_TX_PREP,
35 IEEE80211_HW_DOESNT_SUPPORT_QOS_NDP,
36 IEEE80211_HW_TX_STATUS_NO_AMPDU_LEN,
37 + IEEE80211_HW_TX_NEEDS_ALIGNED4_SKBS,
38
39 /* keep last, obviously */
40 NUM_IEEE80211_HW_FLAGS
41 @@ -2462,6 +2466,40 @@ ieee80211_get_alt_retry_rate(const struc
42 void ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb);
43
44 /**
45 + * ieee80211_hdr_padsize - get size of padding between 802.11 header and LLC
46 + * @hw: the hardware
47 + * @hdrlen: 802.11 header length
48 + */
49 +static inline unsigned int
50 +ieee80211_hdr_padsize(struct ieee80211_hw *hw, unsigned int hdrlen)
51 +{
52 + /*
53 + * While hdrlen is already aligned to two-byte boundaries,
54 + * simple check with & 2 will return correct padsize.
55 + */
56 + if (ieee80211_hw_check(hw, TX_NEEDS_ALIGNED4_SKBS))
57 + return hdrlen & 2;
58 + return 0;
59 +}
60 +
61 +/**
62 + * ieee80211_padded_hdrlen - get padded 802.11 header size
63 + * @hw: the hardware
64 + * @fc: frame control field in little-endian format
65 + */
66 +static inline unsigned int
67 +ieee80211_padded_hdrlen(struct ieee80211_hw *hw, __le16 fc)
68 +{
69 + unsigned int hdrlen;
70 +
71 + hdrlen = ieee80211_hdrlen(fc);
72 + hdrlen += ieee80211_hdr_padsize(hw, hdrlen);
73 +
74 + return hdrlen;
75 +}
76 +
77 +
78 +/**
79 * DOC: Hardware crypto acceleration
80 *
81 * mac80211 is capable of taking advantage of many hardware
82 --- a/net/mac80211/iface.c
83 +++ b/net/mac80211/iface.c
84 @@ -1871,6 +1871,10 @@ int ieee80211_if_add(struct ieee80211_lo
85 + 8 /* rfc1042/bridge tunnel */
86 - ETH_HLEN /* ethernet hard_header_len */
87 + IEEE80211_ENCRYPT_HEADROOM;
88 +
89 + if (ieee80211_hw_check(&local->hw, TX_NEEDS_ALIGNED4_SKBS))
90 + ndev->needed_headroom += 2; /* padding */
91 +
92 ndev->needed_tailroom = IEEE80211_ENCRYPT_TAILROOM;
93
94 ret = dev_alloc_name(ndev, ndev->name);
95 --- a/net/mac80211/mesh_pathtbl.c
96 +++ b/net/mac80211/mesh_pathtbl.c
97 @@ -103,13 +103,15 @@ void mesh_path_assign_nexthop(struct mes
98 static void prepare_for_gate(struct sk_buff *skb, char *dst_addr,
99 struct mesh_path *gate_mpath)
100 {
101 + struct ieee80211_sub_if_data *sdata = gate_mpath->sdata;
102 + struct ieee80211_hw *hw = &sdata->local->hw;
103 struct ieee80211_hdr *hdr;
104 struct ieee80211s_hdr *mshdr;
105 int mesh_hdrlen, hdrlen;
106 char *next_hop;
107
108 hdr = (struct ieee80211_hdr *) skb->data;
109 - hdrlen = ieee80211_hdrlen(hdr->frame_control);
110 + hdrlen = ieee80211_padded_hdrlen(hw, hdr->frame_control);
111 mshdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
112
113 if (!(mshdr->flags & MESH_FLAGS_AE)) {
114 --- a/net/mac80211/rx.c
115 +++ b/net/mac80211/rx.c
116 @@ -2597,7 +2597,7 @@ ieee80211_rx_h_mesh_fwding(struct ieee80
117 struct ieee80211_local *local = rx->local;
118 struct ieee80211_sub_if_data *sdata = rx->sdata;
119 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
120 - u16 ac, q, hdrlen;
121 + u16 ac, q, hdrlen, padsize;
122 int tailroom = 0;
123
124 hdr = (struct ieee80211_hdr *) skb->data;
125 @@ -2688,7 +2688,9 @@ ieee80211_rx_h_mesh_fwding(struct ieee80
126 if (sdata->crypto_tx_tailroom_needed_cnt)
127 tailroom = IEEE80211_ENCRYPT_TAILROOM;
128
129 - fwd_skb = skb_copy_expand(skb, local->tx_headroom +
130 + padsize = ieee80211_hdr_padsize(&local->hw, hdrlen);
131 +
132 + fwd_skb = skb_copy_expand(skb, local->tx_headroom + padsize +
133 sdata->encrypt_headroom,
134 tailroom, GFP_ATOMIC);
135 if (!fwd_skb)
136 @@ -2720,6 +2722,12 @@ ieee80211_rx_h_mesh_fwding(struct ieee80
137 return RX_DROP_MONITOR;
138 }
139
140 + if (padsize) {
141 + skb_push(fwd_skb, padsize);
142 + memmove(fwd_skb->data, skb->data + padsize, hdrlen);
143 + memset(fwd_skb->data + hdrlen, 0, padsize);
144 + }
145 +
146 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_frames);
147 ieee80211_add_pending_skb(local, fwd_skb);
148 out:
149 --- a/net/mac80211/sta_info.h
150 +++ b/net/mac80211/sta_info.h
151 @@ -301,7 +301,7 @@ struct ieee80211_fast_tx {
152 u8 hdr_len;
153 u8 sa_offs, da_offs, pn_offs;
154 u8 band;
155 - u8 hdr[30 + 2 + IEEE80211_FAST_XMIT_MAX_IV +
156 + u8 hdr[30 + 2 + 2 + IEEE80211_FAST_XMIT_MAX_IV +
157 sizeof(rfc1042_header)] __aligned(2);
158
159 struct rcu_head rcu_head;
160 --- a/net/mac80211/status.c
161 +++ b/net/mac80211/status.c
162 @@ -515,6 +515,7 @@ static void ieee80211_report_used_skb(st
163 {
164 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
165 struct ieee80211_hdr *hdr = (void *)skb->data;
166 + struct ieee80211_hw *hw = &local->hw;
167 bool acked = info->flags & IEEE80211_TX_STAT_ACK;
168
169 if (dropped)
170 @@ -531,7 +532,7 @@ static void ieee80211_report_used_skb(st
171 skb->dev = NULL;
172 } else {
173 unsigned int hdr_size =
174 - ieee80211_hdrlen(hdr->frame_control);
175 + ieee80211_padded_hdrlen(hw, hdr->frame_control);
176
177 /* Check to see if packet is a TDLS teardown packet */
178 if (ieee80211_is_data(hdr->frame_control) &&
179 @@ -655,9 +656,22 @@ void ieee80211_tx_monitor(struct ieee802
180 struct sk_buff *skb2;
181 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
182 struct ieee80211_sub_if_data *sdata;
183 + struct ieee80211_hdr *hdr = (void *)skb->data;
184 struct net_device *prev_dev = NULL;
185 + unsigned int hdrlen, padsize;
186 int rtap_len;
187
188 + /* Remove padding if was added */
189 + if (ieee80211_hw_check(&local->hw, TX_NEEDS_ALIGNED4_SKBS)) {
190 + hdrlen = ieee80211_hdrlen(hdr->frame_control);
191 + padsize = ieee80211_hdr_padsize(&local->hw, hdrlen);
192 +
193 + if (padsize && skb->len > hdrlen + padsize) {
194 + memmove(skb->data + padsize, skb->data, hdrlen);
195 + skb_pull(skb, padsize);
196 + }
197 + }
198 +
199 /* send frame to monitor interfaces now */
200 rtap_len = ieee80211_tx_radiotap_len(info);
201 if (WARN_ON_ONCE(skb_headroom(skb) < rtap_len)) {
202 --- a/net/mac80211/tkip.c
203 +++ b/net/mac80211/tkip.c
204 @@ -201,10 +201,12 @@ void ieee80211_get_tkip_p2k(struct ieee8
205 {
206 struct ieee80211_key *key = (struct ieee80211_key *)
207 container_of(keyconf, struct ieee80211_key, conf);
208 + struct ieee80211_hw *hw = &key->local->hw;
209 const u8 *tk = &key->conf.key[NL80211_TKIP_DATA_OFFSET_ENCR_KEY];
210 struct tkip_ctx *ctx = &key->u.tkip.tx;
211 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
212 - const u8 *data = (u8 *)hdr + ieee80211_hdrlen(hdr->frame_control);
213 + const u8 *data = (u8 *)hdr + ieee80211_padded_hdrlen(hw,
214 + hdr->frame_control);
215 u32 iv32 = get_unaligned_le32(&data[4]);
216 u16 iv16 = data[2] | (data[0] << 8);
217
218 --- a/net/mac80211/tx.c
219 +++ b/net/mac80211/tx.c
220 @@ -1175,8 +1175,7 @@ ieee80211_tx_prepare(struct ieee80211_su
221 info->flags &= ~IEEE80211_TX_INTFL_NEED_TXPROCESSING;
222
223 hdr = (struct ieee80211_hdr *) skb->data;
224 -
225 - tx->hdrlen = ieee80211_hdrlen(hdr->frame_control);
226 + tx->hdrlen = ieee80211_padded_hdrlen(&local->hw, hdr->frame_control);
227
228 if (likely(sta)) {
229 if (!IS_ERR(sta))
230 @@ -2215,7 +2214,7 @@ netdev_tx_t ieee80211_monitor_start_xmit
231 goto fail;
232
233 hdr = (struct ieee80211_hdr *)(skb->data + len_rthdr);
234 - hdrlen = ieee80211_hdrlen(hdr->frame_control);
235 + hdrlen = ieee80211_padded_hdrlen(&local->hw, hdr->frame_control);
236
237 if (skb->len < len_rthdr + hdrlen)
238 goto fail;
239 @@ -2433,7 +2432,7 @@ static struct sk_buff *ieee80211_build_h
240 struct ieee80211_chanctx_conf *chanctx_conf;
241 struct ieee80211_sub_if_data *ap_sdata;
242 enum nl80211_band band;
243 - int ret;
244 + int padsize, ret;
245
246 if (IS_ERR(sta))
247 sta = NULL;
248 @@ -2732,7 +2731,9 @@ static struct sk_buff *ieee80211_build_h
249 }
250
251 skb_pull(skb, skip_header_bytes);
252 + padsize = ieee80211_hdr_padsize(&local->hw, hdrlen);
253 head_need = hdrlen + encaps_len + meshhdrlen - skb_headroom(skb);
254 + head_need += padsize;
255
256 /*
257 * So we need to modify the skb header and hence need a copy of
258 @@ -2765,6 +2766,9 @@ static struct sk_buff *ieee80211_build_h
259 memcpy(skb_push(skb, meshhdrlen), &mesh_hdr, meshhdrlen);
260 #endif
261
262 + if (padsize)
263 + memset(skb_push(skb, padsize), 0, padsize);
264 +
265 if (ieee80211_is_data_qos(fc)) {
266 __le16 *qos_control;
267
268 @@ -2940,6 +2944,8 @@ void ieee80211_check_fast_xmit(struct st
269 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
270 }
271
272 + build.hdr_len += ieee80211_hdr_padsize(&local->hw, build.hdr_len);
273 +
274 /* We store the key here so there's no point in using rcu_dereference()
275 * but that's fine because the code that changes the pointers will call
276 * this function after doing so. For a single CPU that would be enough,
277 @@ -3518,7 +3524,7 @@ begin:
278 tx.local = local;
279 tx.skb = skb;
280 tx.sdata = vif_to_sdata(info->control.vif);
281 - tx.hdrlen = ieee80211_hdrlen(hdr->frame_control);
282 + tx.hdrlen = ieee80211_padded_hdrlen(hw, hdr->frame_control);
283
284 if (txq->sta)
285 tx.sta = container_of(txq->sta, struct sta_info, sta);
286 @@ -3856,7 +3862,7 @@ ieee80211_build_data_template(struct iee
287 hdr = (void *)skb->data;
288 tx.sta = sta_info_get(sdata, hdr->addr1);
289 tx.skb = skb;
290 - tx.hdrlen = ieee80211_hdrlen(hdr->frame_control);
291 + tx.hdrlen = ieee80211_padded_hdrlen(&tx.local->hw, hdr->frame_control);
292
293 if (ieee80211_tx_h_select_key(&tx) != TX_CONTINUE) {
294 rcu_read_unlock();
295 --- a/net/mac80211/debugfs.c
296 +++ b/net/mac80211/debugfs.c
297 @@ -215,6 +215,7 @@ static const char *hw_flag_names[] = {
298 FLAG(DEAUTH_NEED_MGD_TX_PREP),
299 FLAG(DOESNT_SUPPORT_QOS_NDP),
300 FLAG(TX_STATUS_NO_AMPDU_LEN),
301 + FLAG(TX_NEEDS_ALIGNED4_SKBS),
302 #undef FLAG
303 };
304