mac80211: optimize skb resizing to avoid reallocation when using 802.11s + batman-adv
[openwrt/openwrt.git] / package / kernel / mac80211 / patches / subsys / 357-mac80211-optimize-skb-resizing.patch
1 From: Felix Fietkau <nbd@nbd.name>
2 Date: Sun, 17 Mar 2019 18:11:30 +0100
3 Subject: [PATCH] mac80211: optimize skb resizing
4
5 When forwarding unicast packets from ethernet to batman-adv over 802.11s
6 (with forwarding disabled), the typical required headroom to transmit
7 encrypted packets on mt76 is 32 (802.11) + 6 (802.11s) + 8 (CCMP) +
8 2 (padding) + 6 (LLC) + 18 (batman-adv) - 14 (old ethernet header) = 58 bytes.
9
10 On systems where NET_SKB_PAD is 64 this leads to a call to pskb_expand_head
11 for every packet, since mac80211 also tries to allocate 16 bytes status
12 headroom for radiotap headers.
13
14 This patch fixes these unnecessary reallocations by only requiring the extra
15 status headroom in ieee80211_tx_monitor()
16 If however a reallocation happens before that call, the status headroom gets
17 added there as well, in order to avoid double reallocation.
18
19 The patch also cleans up the code by moving the headroom calculation to
20 ieee80211_skb_resize.
21
22 Signed-off-by: Felix Fietkau <nbd@nbd.name>
23 ---
24
25 --- a/net/mac80211/ieee80211_i.h
26 +++ b/net/mac80211/ieee80211_i.h
27 @@ -1761,6 +1761,8 @@ void ieee80211_clear_fast_xmit(struct st
28 int ieee80211_tx_control_port(struct wiphy *wiphy, struct net_device *dev,
29 const u8 *buf, size_t len,
30 const u8 *dest, __be16 proto, bool unencrypted);
31 +int ieee80211_skb_resize(struct ieee80211_sub_if_data *sdata,
32 + struct sk_buff *skb, int hdrlen, int hdr_add);
33
34 /* HT */
35 void ieee80211_apply_htcap_overrides(struct ieee80211_sub_if_data *sdata,
36 --- a/net/mac80211/status.c
37 +++ b/net/mac80211/status.c
38 @@ -672,6 +672,11 @@ void ieee80211_tx_monitor(struct ieee802
39 }
40 }
41
42 + if (ieee80211_skb_resize(NULL, skb, 0, 0)) {
43 + dev_kfree_skb(skb);
44 + return;
45 + }
46 +
47 /* send frame to monitor interfaces now */
48 rtap_len = ieee80211_tx_radiotap_len(info);
49 if (WARN_ON_ONCE(skb_headroom(skb) < rtap_len)) {
50 --- a/net/mac80211/tx.c
51 +++ b/net/mac80211/tx.c
52 @@ -1914,37 +1914,53 @@ static bool ieee80211_tx(struct ieee8021
53 }
54
55 /* device xmit handlers */
56 -
57 -static int ieee80211_skb_resize(struct ieee80211_sub_if_data *sdata,
58 - struct sk_buff *skb,
59 - int head_need, bool may_encrypt)
60 +int ieee80211_skb_resize(struct ieee80211_sub_if_data *sdata,
61 + struct sk_buff *skb, int hdr_len, int hdr_extra)
62 {
63 struct ieee80211_local *local = sdata->local;
64 + struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
65 struct ieee80211_hdr *hdr;
66 - bool enc_tailroom;
67 - int tail_need = 0;
68 + int head_need, head_max;
69 + int tail_need, tail_max;
70 + bool enc_tailroom = false;
71
72 - hdr = (struct ieee80211_hdr *) skb->data;
73 - enc_tailroom = may_encrypt &&
74 - (sdata->crypto_tx_tailroom_needed_cnt ||
75 - ieee80211_is_mgmt(hdr->frame_control));
76 + if (sdata && !hdr_len &&
77 + !(info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT)) {
78 + hdr = (struct ieee80211_hdr *) skb->data;
79 + enc_tailroom = (sdata->crypto_tx_tailroom_needed_cnt ||
80 + ieee80211_is_mgmt(hdr->frame_control));
81 + hdr_len += sdata->encrypt_headroom;
82 + }
83
84 - if (enc_tailroom) {
85 - tail_need = IEEE80211_ENCRYPT_TAILROOM;
86 - tail_need -= skb_tailroom(skb);
87 - tail_need = max_t(int, tail_need, 0);
88 + head_need = head_max = hdr_len;
89 + tail_need = tail_max = 0;
90 + if (!sdata) {
91 + head_need = head_max = local->tx_headroom;
92 + } else {
93 + head_max += hdr_extra;
94 + head_max += max_t(int, local->tx_headroom,
95 + local->hw.extra_tx_headroom);
96 + head_need += local->hw.extra_tx_headroom;
97 +
98 + tail_max = IEEE80211_ENCRYPT_TAILROOM;
99 + if (enc_tailroom)
100 + tail_need = tail_max;
101 }
102
103 if (skb_cloned(skb) &&
104 (!ieee80211_hw_check(&local->hw, SUPPORTS_CLONED_SKBS) ||
105 !skb_clone_writable(skb, ETH_HLEN) || enc_tailroom))
106 I802_DEBUG_INC(local->tx_expand_skb_head_cloned);
107 - else if (head_need || tail_need)
108 + else if (head_need > skb_headroom(skb) ||
109 + tail_need > skb_tailroom(skb))
110 I802_DEBUG_INC(local->tx_expand_skb_head);
111 else
112 return 0;
113
114 - if (pskb_expand_head(skb, head_need, tail_need, GFP_ATOMIC)) {
115 + head_max = max_t(int, 0, head_max - skb_headroom(skb));
116 + tail_max = max_t(int, 0, tail_max - skb_tailroom(skb));
117 +
118 + if (pskb_expand_head(skb, head_max, tail_max, GFP_ATOMIC)) {
119 wiphy_debug(local->hw.wiphy,
120 "failed to reallocate TX buffer\n");
121 return -ENOMEM;
122 @@ -1960,18 +1976,8 @@ void ieee80211_xmit(struct ieee80211_sub
123 struct ieee80211_local *local = sdata->local;
124 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
125 struct ieee80211_hdr *hdr;
126 - int headroom;
127 - bool may_encrypt;
128 -
129 - may_encrypt = !(info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT);
130 -
131 - headroom = local->tx_headroom;
132 - if (may_encrypt)
133 - headroom += sdata->encrypt_headroom;
134 - headroom -= skb_headroom(skb);
135 - headroom = max_t(int, 0, headroom);
136
137 - if (ieee80211_skb_resize(sdata, skb, headroom, may_encrypt)) {
138 + if (ieee80211_skb_resize(sdata, skb, 0, 0)) {
139 ieee80211_free_txskb(&local->hw, skb);
140 return;
141 }
142 @@ -2740,30 +2746,14 @@ static struct sk_buff *ieee80211_build_h
143
144 skb_pull(skb, skip_header_bytes);
145 padsize = ieee80211_hdr_padsize(&local->hw, hdrlen);
146 - head_need = hdrlen + encaps_len + meshhdrlen - skb_headroom(skb);
147 + head_need = hdrlen + encaps_len + meshhdrlen;
148 head_need += padsize;
149
150 - /*
151 - * So we need to modify the skb header and hence need a copy of
152 - * that. The head_need variable above doesn't, so far, include
153 - * the needed header space that we don't need right away. If we
154 - * can, then we don't reallocate right now but only after the
155 - * frame arrives at the master device (if it does...)
156 - *
157 - * If we cannot, however, then we will reallocate to include all
158 - * the ever needed space. Also, if we need to reallocate it anyway,
159 - * make it big enough for everything we may ever need.
160 - */
161 -
162 - if (head_need > 0 || skb_cloned(skb)) {
163 - head_need += sdata->encrypt_headroom;
164 - head_need += local->tx_headroom;
165 - head_need = max_t(int, 0, head_need);
166 - if (ieee80211_skb_resize(sdata, skb, head_need, true)) {
167 - ieee80211_free_txskb(&local->hw, skb);
168 - skb = NULL;
169 - return ERR_PTR(-ENOMEM);
170 - }
171 + if (ieee80211_skb_resize(sdata, skb, head_need,
172 + sdata->encrypt_headroom)) {
173 + ieee80211_free_txskb(&local->hw, skb);
174 + skb = NULL;
175 + return ERR_PTR(-ENOMEM);
176 }
177
178 if (encaps_data)
179 @@ -3377,7 +3367,6 @@ static bool ieee80211_xmit_fast(struct i
180 struct ieee80211_local *local = sdata->local;
181 u16 ethertype = (skb->data[12] << 8) | skb->data[13];
182 int extra_head = fast_tx->hdr_len - (ETH_HLEN - 2);
183 - int hw_headroom = sdata->local->hw.extra_tx_headroom;
184 struct ethhdr eth;
185 struct ieee80211_tx_info *info;
186 struct ieee80211_hdr *hdr = (void *)fast_tx->hdr;
187 @@ -3429,10 +3418,7 @@ static bool ieee80211_xmit_fast(struct i
188 * as the may-encrypt argument for the resize to not account for
189 * more room than we already have in 'extra_head'
190 */
191 - if (unlikely(ieee80211_skb_resize(sdata, skb,
192 - max_t(int, extra_head + hw_headroom -
193 - skb_headroom(skb), 0),
194 - false))) {
195 + if (unlikely(ieee80211_skb_resize(sdata, skb, extra_head, 0))) {
196 kfree_skb(skb);
197 return true;
198 }