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
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.
8 Padding is added after ieee80211_hdr, before IV/LLC.
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).
14 With this patch we can skip this memmove() and thus reduce CPU cycles in
17 Signed-off-by: Janusz Dziedzic <janusz.dziedzic@tieto.com>
18 Signed-off-by: Felix Fietkau <nbd@nbd.name>
21 --- a/include/net/mac80211.h
22 +++ b/include/net/mac80211.h
23 @@ -2140,6 +2140,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
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.
30 * @NUM_IEEE80211_HW_FLAGS: number of hardware flags, used for sizing arrays
32 enum ieee80211_hw_flags {
33 @@ -2186,6 +2189,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,
39 /* keep last, obviously */
40 NUM_IEEE80211_HW_FLAGS
41 @@ -2472,6 +2476,40 @@ ieee80211_get_alt_retry_rate(const struc
42 void ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb);
45 + * ieee80211_hdr_padsize - get size of padding between 802.11 header and LLC
47 + * @hdrlen: 802.11 header length
49 +static inline unsigned int
50 +ieee80211_hdr_padsize(struct ieee80211_hw *hw, unsigned int hdrlen)
53 + * While hdrlen is already aligned to two-byte boundaries,
54 + * simple check with & 2 will return correct padsize.
56 + if (ieee80211_hw_check(hw, TX_NEEDS_ALIGNED4_SKBS))
62 + * ieee80211_padded_hdrlen - get padded 802.11 header size
64 + * @fc: frame control field in little-endian format
66 +static inline unsigned int
67 +ieee80211_padded_hdrlen(struct ieee80211_hw *hw, __le16 fc)
69 + unsigned int hdrlen;
71 + hdrlen = ieee80211_hdrlen(fc);
72 + hdrlen += ieee80211_hdr_padsize(hw, hdrlen);
79 * DOC: Hardware crypto acceleration
81 * mac80211 is capable of taking advantage of many hardware
82 --- a/net/mac80211/iface.c
83 +++ b/net/mac80211/iface.c
84 @@ -1873,6 +1873,10 @@ int ieee80211_if_add(struct ieee80211_lo
85 + 8 /* rfc1042/bridge tunnel */
86 - ETH_HLEN /* ethernet hard_header_len */
87 + IEEE80211_ENCRYPT_HEADROOM;
89 + if (ieee80211_hw_check(&local->hw, TX_NEEDS_ALIGNED4_SKBS))
90 + ndev->needed_headroom += 2; /* padding */
92 ndev->needed_tailroom = IEEE80211_ENCRYPT_TAILROOM;
94 ret = dev_alloc_name(ndev, ndev->name);
95 --- a/net/mac80211/mesh_pathtbl.c
96 +++ b/net/mac80211/mesh_pathtbl.c
97 @@ -106,13 +106,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)
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;
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);
113 if (!(mshdr->flags & MESH_FLAGS_AE)) {
114 --- a/net/mac80211/rx.c
115 +++ b/net/mac80211/rx.c
116 @@ -2705,7 +2705,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;
121 + u16 ac, q, hdrlen, padsize;
124 hdr = (struct ieee80211_hdr *) skb->data;
125 @@ -2798,7 +2798,9 @@ ieee80211_rx_h_mesh_fwding(struct ieee80
126 if (sdata->crypto_tx_tailroom_needed_cnt)
127 tailroom = IEEE80211_ENCRYPT_TAILROOM;
129 - fwd_skb = skb_copy_expand(skb, local->tx_headroom +
130 + padsize = ieee80211_hdr_padsize(&local->hw, hdrlen);
132 + fwd_skb = skb_copy_expand(skb, local->tx_headroom + padsize +
133 sdata->encrypt_headroom,
134 tailroom, GFP_ATOMIC);
136 @@ -2830,6 +2832,12 @@ ieee80211_rx_h_mesh_fwding(struct ieee80
137 return RX_DROP_MONITOR;
141 + skb_push(fwd_skb, padsize);
142 + memmove(fwd_skb->data, skb->data + padsize, hdrlen);
143 + memset(fwd_skb->data + hdrlen, 0, padsize);
146 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_frames);
147 ieee80211_add_pending_skb(local, fwd_skb);
149 --- a/net/mac80211/sta_info.h
150 +++ b/net/mac80211/sta_info.h
151 @@ -312,7 +312,7 @@ struct ieee80211_fast_tx {
153 u8 sa_offs, da_offs, pn_offs;
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);
159 struct rcu_head rcu_head;
160 --- a/net/mac80211/status.c
161 +++ b/net/mac80211/status.c
162 @@ -514,6 +514,7 @@ static void ieee80211_report_used_skb(st
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;
170 @@ -530,7 +531,7 @@ static void ieee80211_report_used_skb(st
173 unsigned int hdr_size =
174 - ieee80211_hdrlen(hdr->frame_control);
175 + ieee80211_padded_hdrlen(hw, hdr->frame_control);
177 /* Check to see if packet is a TDLS teardown packet */
178 if (ieee80211_is_data(hdr->frame_control) &&
179 @@ -654,9 +655,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;
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);
193 + if (padsize && skb->len > hdrlen + padsize) {
194 + memmove(skb->data + padsize, skb->data, hdrlen);
195 + skb_pull(skb, padsize);
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
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);
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;
223 hdr = (struct ieee80211_hdr *) skb->data;
225 - tx->hdrlen = ieee80211_hdrlen(hdr->frame_control);
226 + tx->hdrlen = ieee80211_padded_hdrlen(&local->hw, hdr->frame_control);
230 @@ -2233,7 +2232,7 @@ netdev_tx_t ieee80211_monitor_start_xmit
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);
237 if (skb->len < len_rthdr + hdrlen)
239 @@ -2452,7 +2451,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;
248 @@ -2751,7 +2750,9 @@ static struct sk_buff *ieee80211_build_h
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;
257 * So we need to modify the skb header and hence need a copy of
258 @@ -2784,6 +2785,9 @@ static struct sk_buff *ieee80211_build_h
259 memcpy(skb_push(skb, meshhdrlen), &mesh_hdr, meshhdrlen);
263 + memset(skb_push(skb, padsize), 0, padsize);
265 if (ieee80211_is_data_qos(fc)) {
268 @@ -2960,6 +2964,8 @@ void ieee80211_check_fast_xmit(struct st
269 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
272 + build.hdr_len += ieee80211_hdr_padsize(&local->hw, build.hdr_len);
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 @@ -3536,7 +3542,7 @@ begin:
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);
285 tx.sta = container_of(txq->sta, struct sta_info, sta);
286 @@ -4040,7 +4046,7 @@ ieee80211_build_data_template(struct iee
287 hdr = (void *)skb->data;
288 tx.sta = sta_info_get(sdata, hdr->addr1);
290 - tx.hdrlen = ieee80211_hdrlen(hdr->frame_control);
291 + tx.hdrlen = ieee80211_padded_hdrlen(&tx.local->hw, hdr->frame_control);
293 if (ieee80211_tx_h_select_key(&tx) != TX_CONTINUE) {
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),