12e9cf5744be400bd3fab31039ff6d3904d64d9a
[openwrt/openwrt.git] / package / kernel / mac80211 / patches / subsys / 319-wifi-mac80211-mesh-fast-xmit-support.patch
1 From: Felix Fietkau <nbd@nbd.name>
2 Date: Sun, 26 Feb 2023 13:53:08 +0100
3 Subject: [PATCH] wifi: mac80211: mesh fast xmit support
4
5 Previously, fast xmit only worked on interface types where initially a
6 sta lookup is performed, and a cached header can be attached to the sta,
7 requiring only some fields to be updated at runtime.
8
9 This technique is not directly applicable for a mesh device type due
10 to the dynamic nature of the topology and protocol. There are more
11 addresses that need to be filled, and there is an extra header with a
12 dynamic length based on the addressing mode.
13
14 Change the code to cache entries contain a copy of the mesh subframe header +
15 bridge tunnel header, as well as an embedded struct ieee80211_fast_tx, which
16 contains the information for building the 802.11 header.
17
18 Add a mesh specific early fast xmit call, which looks up a cached entry and
19 adds only the mesh subframe header, before passing it over to the generic
20 fast xmit code.
21
22 To ensure the changes in network are reflected in these cached headers,
23 flush affected cached entries on path changes, as well as other conditions
24 that currently trigger a fast xmit check in other modes (key changes etc.)
25
26 This code is loosely based on a previous implementation by:
27 Sriram R <quic_srirrama@quicinc.com>
28
29 Signed-off-by: Ryder Lee <ryder.lee@mediatek.com>
30 Signed-off-by: Felix Fietkau <nbd@nbd.name>
31 ---
32
33 --- a/net/mac80211/ieee80211_i.h
34 +++ b/net/mac80211/ieee80211_i.h
35 @@ -37,6 +37,7 @@
36 extern const struct cfg80211_ops mac80211_config_ops;
37
38 struct ieee80211_local;
39 +struct ieee80211_mesh_fast_tx;
40
41 /* Maximum number of broadcast/multicast frames to buffer when some of the
42 * associated stations are using power saving. */
43 @@ -655,6 +656,19 @@ struct mesh_table {
44 atomic_t entries; /* Up to MAX_MESH_NEIGHBOURS */
45 };
46
47 +/**
48 + * struct mesh_tx_cache - mesh fast xmit header cache
49 + *
50 + * @rht: hash table containing struct ieee80211_mesh_fast_tx, using skb DA as key
51 + * @walk_head: linked list containing all ieee80211_mesh_fast_tx objects
52 + * @walk_lock: lock protecting walk_head and rht
53 + */
54 +struct mesh_tx_cache {
55 + struct rhashtable rht;
56 + struct hlist_head walk_head;
57 + spinlock_t walk_lock;
58 +};
59 +
60 struct ieee80211_if_mesh {
61 struct timer_list housekeeping_timer;
62 struct timer_list mesh_path_timer;
63 @@ -733,6 +747,7 @@ struct ieee80211_if_mesh {
64 struct mesh_table mpp_paths; /* Store paths for MPP&MAP */
65 int mesh_paths_generation;
66 int mpp_paths_generation;
67 + struct mesh_tx_cache tx_cache;
68 };
69
70 #ifdef CPTCFG_MAC80211_MESH
71 @@ -1998,6 +2013,11 @@ int ieee80211_tx_control_port(struct wip
72 int link_id, u64 *cookie);
73 int ieee80211_probe_mesh_link(struct wiphy *wiphy, struct net_device *dev,
74 const u8 *buf, size_t len);
75 +void __ieee80211_xmit_fast(struct ieee80211_sub_if_data *sdata,
76 + struct sta_info *sta,
77 + struct ieee80211_fast_tx *fast_tx,
78 + struct sk_buff *skb, bool ampdu,
79 + const u8 *da, const u8 *sa);
80
81 /* HT */
82 void ieee80211_apply_htcap_overrides(struct ieee80211_sub_if_data *sdata,
83 --- a/net/mac80211/mesh.c
84 +++ b/net/mac80211/mesh.c
85 @@ -10,6 +10,7 @@
86 #include <asm/unaligned.h>
87 #include "ieee80211_i.h"
88 #include "mesh.h"
89 +#include "wme.h"
90 #include "driver-ops.h"
91
92 static int mesh_allocated;
93 @@ -698,6 +699,95 @@ ieee80211_mesh_update_bss_params(struct
94 __le32_to_cpu(he_oper->he_oper_params);
95 }
96
97 +bool ieee80211_mesh_xmit_fast(struct ieee80211_sub_if_data *sdata,
98 + struct sk_buff *skb, u32 ctrl_flags)
99 +{
100 + struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
101 + struct ieee80211_mesh_fast_tx *entry;
102 + struct ieee80211s_hdr *meshhdr;
103 + u8 sa[ETH_ALEN] __aligned(2);
104 + struct tid_ampdu_tx *tid_tx;
105 + struct sta_info *sta;
106 + bool copy_sa = false;
107 + u16 ethertype;
108 + u8 tid;
109 +
110 + if (ctrl_flags & IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP)
111 + return false;
112 +
113 + if (ifmsh->mshcfg.dot11MeshNolearn)
114 + return false;
115 +
116 + /* Add support for these cases later */
117 + if (ifmsh->ps_peers_light_sleep || ifmsh->ps_peers_deep_sleep)
118 + return false;
119 +
120 + if (is_multicast_ether_addr(skb->data))
121 + return false;
122 +
123 + ethertype = (skb->data[12] << 8) | skb->data[13];
124 + if (ethertype < ETH_P_802_3_MIN)
125 + return false;
126 +
127 + if (skb->sk && skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS)
128 + return false;
129 +
130 + if (skb->ip_summed == CHECKSUM_PARTIAL) {
131 + skb_set_transport_header(skb, skb_checksum_start_offset(skb));
132 + if (skb_checksum_help(skb))
133 + return false;
134 + }
135 +
136 + entry = mesh_fast_tx_get(sdata, skb->data);
137 + if (!entry)
138 + return false;
139 +
140 + if (skb_headroom(skb) < entry->hdrlen + entry->fast_tx.hdr_len)
141 + return false;
142 +
143 + sta = rcu_dereference(entry->mpath->next_hop);
144 + if (!sta)
145 + return false;
146 +
147 + tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
148 + tid_tx = rcu_dereference(sta->ampdu_mlme.tid_tx[tid]);
149 + if (tid_tx) {
150 + if (!test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state))
151 + return false;
152 + if (tid_tx->timeout)
153 + tid_tx->last_tx = jiffies;
154 + }
155 +
156 + skb = skb_share_check(skb, GFP_ATOMIC);
157 + if (!skb)
158 + return true;
159 +
160 + skb_set_queue_mapping(skb, ieee80211_select_queue(sdata, sta, skb));
161 +
162 + meshhdr = (struct ieee80211s_hdr *)entry->hdr;
163 + if ((meshhdr->flags & MESH_FLAGS_AE) == MESH_FLAGS_AE_A5_A6) {
164 + /* preserve SA from eth header for 6-addr frames */
165 + ether_addr_copy(sa, skb->data + ETH_ALEN);
166 + copy_sa = true;
167 + }
168 +
169 + memcpy(skb_push(skb, entry->hdrlen - 2 * ETH_ALEN), entry->hdr,
170 + entry->hdrlen);
171 +
172 + meshhdr = (struct ieee80211s_hdr *)skb->data;
173 + put_unaligned_le32(atomic_inc_return(&sdata->u.mesh.mesh_seqnum),
174 + &meshhdr->seqnum);
175 + meshhdr->ttl = sdata->u.mesh.mshcfg.dot11MeshTTL;
176 + if (copy_sa)
177 + ether_addr_copy(meshhdr->eaddr2, sa);
178 +
179 + skb_push(skb, 2 * ETH_ALEN);
180 + __ieee80211_xmit_fast(sdata, sta, &entry->fast_tx, skb, tid_tx,
181 + entry->mpath->dst, sdata->vif.addr);
182 +
183 + return true;
184 +}
185 +
186 /**
187 * ieee80211_fill_mesh_addresses - fill addresses of a locally originated mesh frame
188 * @hdr: 802.11 frame header
189 @@ -780,6 +870,8 @@ static void ieee80211_mesh_housekeeping(
190 changed = mesh_accept_plinks_update(sdata);
191 ieee80211_mbss_info_change_notify(sdata, changed);
192
193 + mesh_fast_tx_gc(sdata);
194 +
195 mod_timer(&ifmsh->housekeeping_timer,
196 round_jiffies(jiffies +
197 IEEE80211_MESH_HOUSEKEEPING_INTERVAL));
198 --- a/net/mac80211/mesh.h
199 +++ b/net/mac80211/mesh.h
200 @@ -122,11 +122,41 @@ struct mesh_path {
201 u8 rann_snd_addr[ETH_ALEN];
202 u32 rann_metric;
203 unsigned long last_preq_to_root;
204 + unsigned long fast_tx_check;
205 bool is_root;
206 bool is_gate;
207 u32 path_change_count;
208 };
209
210 +#define MESH_FAST_TX_CACHE_MAX_SIZE 512
211 +#define MESH_FAST_TX_CACHE_THRESHOLD_SIZE 384
212 +#define MESH_FAST_TX_CACHE_TIMEOUT 8000 /* msecs */
213 +
214 +/**
215 + * struct ieee80211_mesh_fast_tx - cached mesh fast tx entry
216 + * @rhash: rhashtable pointer
217 + * @addr_key: The Ethernet DA which is the key for this entry
218 + * @fast_tx: base fast_tx data
219 + * @hdr: cached mesh and rfc1042 headers
220 + * @hdrlen: length of mesh + rfc1042
221 + * @walk_list: list containing all the fast tx entries
222 + * @mpath: mesh path corresponding to the Mesh DA
223 + * @mppath: MPP entry corresponding to this DA
224 + * @timestamp: Last used time of this entry
225 + */
226 +struct ieee80211_mesh_fast_tx {
227 + struct rhash_head rhash;
228 + u8 addr_key[ETH_ALEN] __aligned(2);
229 +
230 + struct ieee80211_fast_tx fast_tx;
231 + u8 hdr[sizeof(struct ieee80211s_hdr) + sizeof(rfc1042_header)];
232 + u16 hdrlen;
233 +
234 + struct mesh_path *mpath, *mppath;
235 + struct hlist_node walk_list;
236 + unsigned long timestamp;
237 +};
238 +
239 /* Recent multicast cache */
240 /* RMC_BUCKETS must be a power of 2, maximum 256 */
241 #define RMC_BUCKETS 256
242 @@ -298,6 +328,20 @@ void mesh_path_discard_frame(struct ieee
243 void mesh_path_tx_root_frame(struct ieee80211_sub_if_data *sdata);
244
245 bool mesh_action_is_path_sel(struct ieee80211_mgmt *mgmt);
246 +struct ieee80211_mesh_fast_tx *
247 +mesh_fast_tx_get(struct ieee80211_sub_if_data *sdata, const u8 *addr);
248 +bool ieee80211_mesh_xmit_fast(struct ieee80211_sub_if_data *sdata,
249 + struct sk_buff *skb, u32 ctrl_flags);
250 +void mesh_fast_tx_cache(struct ieee80211_sub_if_data *sdata,
251 + struct sk_buff *skb, struct mesh_path *mpath);
252 +void mesh_fast_tx_gc(struct ieee80211_sub_if_data *sdata);
253 +void mesh_fast_tx_flush_addr(struct ieee80211_sub_if_data *sdata,
254 + const u8 *addr);
255 +void mesh_fast_tx_flush_mpath(struct mesh_path *mpath);
256 +void mesh_fast_tx_flush_sta(struct ieee80211_sub_if_data *sdata,
257 + struct sta_info *sta);
258 +void mesh_path_refresh(struct ieee80211_sub_if_data *sdata,
259 + struct mesh_path *mpath, const u8 *addr);
260
261 #ifdef CPTCFG_MAC80211_MESH
262 static inline
263 --- a/net/mac80211/mesh_hwmp.c
264 +++ b/net/mac80211/mesh_hwmp.c
265 @@ -394,6 +394,7 @@ static u32 hwmp_route_info_get(struct ie
266 u32 orig_sn, orig_metric;
267 unsigned long orig_lifetime, exp_time;
268 u32 last_hop_metric, new_metric;
269 + bool flush_mpath = false;
270 bool process = true;
271 u8 hopcount;
272
273 @@ -491,8 +492,10 @@ static u32 hwmp_route_info_get(struct ie
274 }
275
276 if (fresh_info) {
277 - if (rcu_access_pointer(mpath->next_hop) != sta)
278 + if (rcu_access_pointer(mpath->next_hop) != sta) {
279 mpath->path_change_count++;
280 + flush_mpath = true;
281 + }
282 mesh_path_assign_nexthop(mpath, sta);
283 mpath->flags |= MESH_PATH_SN_VALID;
284 mpath->metric = new_metric;
285 @@ -502,6 +505,8 @@ static u32 hwmp_route_info_get(struct ie
286 mpath->hop_count = hopcount;
287 mesh_path_activate(mpath);
288 spin_unlock_bh(&mpath->state_lock);
289 + if (flush_mpath)
290 + mesh_fast_tx_flush_mpath(mpath);
291 ewma_mesh_fail_avg_init(&sta->mesh->fail_avg);
292 /* init it at a low value - 0 start is tricky */
293 ewma_mesh_fail_avg_add(&sta->mesh->fail_avg, 1);
294 @@ -539,8 +544,10 @@ static u32 hwmp_route_info_get(struct ie
295 }
296
297 if (fresh_info) {
298 - if (rcu_access_pointer(mpath->next_hop) != sta)
299 + if (rcu_access_pointer(mpath->next_hop) != sta) {
300 mpath->path_change_count++;
301 + flush_mpath = true;
302 + }
303 mesh_path_assign_nexthop(mpath, sta);
304 mpath->metric = last_hop_metric;
305 mpath->exp_time = time_after(mpath->exp_time, exp_time)
306 @@ -548,6 +555,8 @@ static u32 hwmp_route_info_get(struct ie
307 mpath->hop_count = 1;
308 mesh_path_activate(mpath);
309 spin_unlock_bh(&mpath->state_lock);
310 + if (flush_mpath)
311 + mesh_fast_tx_flush_mpath(mpath);
312 ewma_mesh_fail_avg_init(&sta->mesh->fail_avg);
313 /* init it at a low value - 0 start is tricky */
314 ewma_mesh_fail_avg_add(&sta->mesh->fail_avg, 1);
315 @@ -1215,6 +1224,20 @@ static int mesh_nexthop_lookup_nolearn(s
316 return 0;
317 }
318
319 +void mesh_path_refresh(struct ieee80211_sub_if_data *sdata,
320 + struct mesh_path *mpath, const u8 *addr)
321 +{
322 + if (mpath->flags & (MESH_PATH_REQ_QUEUED | MESH_PATH_FIXED |
323 + MESH_PATH_RESOLVING))
324 + return;
325 +
326 + if (time_after(jiffies,
327 + mpath->exp_time -
328 + msecs_to_jiffies(sdata->u.mesh.mshcfg.path_refresh_time)) &&
329 + (!addr || ether_addr_equal(sdata->vif.addr, addr)))
330 + mesh_queue_preq(mpath, PREQ_Q_F_START | PREQ_Q_F_REFRESH);
331 +}
332 +
333 /**
334 * mesh_nexthop_lookup - put the appropriate next hop on a mesh frame. Calling
335 * this function is considered "using" the associated mpath, so preempt a path
336 @@ -1242,19 +1265,15 @@ int mesh_nexthop_lookup(struct ieee80211
337 if (!mpath || !(mpath->flags & MESH_PATH_ACTIVE))
338 return -ENOENT;
339
340 - if (time_after(jiffies,
341 - mpath->exp_time -
342 - msecs_to_jiffies(sdata->u.mesh.mshcfg.path_refresh_time)) &&
343 - ether_addr_equal(sdata->vif.addr, hdr->addr4) &&
344 - !(mpath->flags & MESH_PATH_RESOLVING) &&
345 - !(mpath->flags & MESH_PATH_FIXED))
346 - mesh_queue_preq(mpath, PREQ_Q_F_START | PREQ_Q_F_REFRESH);
347 + mesh_path_refresh(sdata, mpath, hdr->addr4);
348
349 next_hop = rcu_dereference(mpath->next_hop);
350 if (next_hop) {
351 memcpy(hdr->addr1, next_hop->sta.addr, ETH_ALEN);
352 memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
353 ieee80211_mps_set_frame_flags(sdata, next_hop, hdr);
354 + if (ieee80211_hw_check(&sdata->local->hw, SUPPORT_FAST_XMIT))
355 + mesh_fast_tx_cache(sdata, skb, mpath);
356 return 0;
357 }
358
359 --- a/net/mac80211/mesh_pathtbl.c
360 +++ b/net/mac80211/mesh_pathtbl.c
361 @@ -14,6 +14,7 @@
362 #include "wme.h"
363 #include "ieee80211_i.h"
364 #include "mesh.h"
365 +#include <linux/rhashtable.h>
366
367 static void mesh_path_free_rcu(struct mesh_table *tbl, struct mesh_path *mpath);
368
369 @@ -32,6 +33,41 @@ static const struct rhashtable_params me
370 .hashfn = mesh_table_hash,
371 };
372
373 +static const struct rhashtable_params fast_tx_rht_params = {
374 + .nelem_hint = 10,
375 + .automatic_shrinking = true,
376 + .key_len = ETH_ALEN,
377 + .key_offset = offsetof(struct ieee80211_mesh_fast_tx, addr_key),
378 + .head_offset = offsetof(struct ieee80211_mesh_fast_tx, rhash),
379 + .hashfn = mesh_table_hash,
380 +};
381 +
382 +static void __mesh_fast_tx_entry_free(void *ptr, void *tblptr)
383 +{
384 + struct ieee80211_mesh_fast_tx *entry = ptr;
385 +
386 + kfree_rcu(entry, fast_tx.rcu_head);
387 +}
388 +
389 +static void mesh_fast_tx_deinit(struct ieee80211_sub_if_data *sdata)
390 +{
391 + struct mesh_tx_cache *cache;
392 +
393 + cache = &sdata->u.mesh.tx_cache;
394 + rhashtable_free_and_destroy(&cache->rht,
395 + __mesh_fast_tx_entry_free, NULL);
396 +}
397 +
398 +static void mesh_fast_tx_init(struct ieee80211_sub_if_data *sdata)
399 +{
400 + struct mesh_tx_cache *cache;
401 +
402 + cache = &sdata->u.mesh.tx_cache;
403 + rhashtable_init(&cache->rht, &fast_tx_rht_params);
404 + INIT_HLIST_HEAD(&cache->walk_head);
405 + spin_lock_init(&cache->walk_lock);
406 +}
407 +
408 static inline bool mpath_expired(struct mesh_path *mpath)
409 {
410 return (mpath->flags & MESH_PATH_ACTIVE) &&
411 @@ -381,6 +417,243 @@ struct mesh_path *mesh_path_new(struct i
412 return new_mpath;
413 }
414
415 +static void mesh_fast_tx_entry_free(struct mesh_tx_cache *cache,
416 + struct ieee80211_mesh_fast_tx *entry)
417 +{
418 + hlist_del_rcu(&entry->walk_list);
419 + rhashtable_remove_fast(&cache->rht, &entry->rhash, fast_tx_rht_params);
420 + kfree_rcu(entry, fast_tx.rcu_head);
421 +}
422 +
423 +struct ieee80211_mesh_fast_tx *
424 +mesh_fast_tx_get(struct ieee80211_sub_if_data *sdata, const u8 *addr)
425 +{
426 + struct ieee80211_mesh_fast_tx *entry;
427 + struct mesh_tx_cache *cache;
428 +
429 + cache = &sdata->u.mesh.tx_cache;
430 + entry = rhashtable_lookup(&cache->rht, addr, fast_tx_rht_params);
431 + if (!entry)
432 + return NULL;
433 +
434 + if (!(entry->mpath->flags & MESH_PATH_ACTIVE) ||
435 + mpath_expired(entry->mpath)) {
436 + spin_lock_bh(&cache->walk_lock);
437 + entry = rhashtable_lookup(&cache->rht, addr, fast_tx_rht_params);
438 + if (entry)
439 + mesh_fast_tx_entry_free(cache, entry);
440 + spin_unlock_bh(&cache->walk_lock);
441 + return NULL;
442 + }
443 +
444 + mesh_path_refresh(sdata, entry->mpath, NULL);
445 + if (entry->mppath)
446 + entry->mppath->exp_time = jiffies;
447 + entry->timestamp = jiffies;
448 +
449 + return entry;
450 +}
451 +
452 +void mesh_fast_tx_cache(struct ieee80211_sub_if_data *sdata,
453 + struct sk_buff *skb, struct mesh_path *mpath)
454 +{
455 + struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
456 + struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
457 + struct ieee80211_mesh_fast_tx *entry, *prev;
458 + struct ieee80211_mesh_fast_tx build = {};
459 + struct ieee80211s_hdr *meshhdr;
460 + struct mesh_tx_cache *cache;
461 + struct ieee80211_key *key;
462 + struct mesh_path *mppath;
463 + struct sta_info *sta;
464 + u8 *qc;
465 +
466 + if (sdata->noack_map ||
467 + !ieee80211_is_data_qos(hdr->frame_control))
468 + return;
469 +
470 + build.fast_tx.hdr_len = ieee80211_hdrlen(hdr->frame_control);
471 + meshhdr = (struct ieee80211s_hdr *)(skb->data + build.fast_tx.hdr_len);
472 + build.hdrlen = ieee80211_get_mesh_hdrlen(meshhdr);
473 +
474 + cache = &sdata->u.mesh.tx_cache;
475 + if (atomic_read(&cache->rht.nelems) >= MESH_FAST_TX_CACHE_MAX_SIZE)
476 + return;
477 +
478 + sta = rcu_dereference(mpath->next_hop);
479 + if (!sta)
480 + return;
481 +
482 + if ((meshhdr->flags & MESH_FLAGS_AE) == MESH_FLAGS_AE_A5_A6) {
483 + /* This is required to keep the mppath alive */
484 + mppath = mpp_path_lookup(sdata, meshhdr->eaddr1);
485 + if (!mppath)
486 + return;
487 + build.mppath = mppath;
488 + } else if (ieee80211_has_a4(hdr->frame_control)) {
489 + mppath = mpath;
490 + } else {
491 + return;
492 + }
493 +
494 + /* rate limit, in case fast xmit can't be enabled */
495 + if (mppath->fast_tx_check == jiffies)
496 + return;
497 +
498 + mppath->fast_tx_check = jiffies;
499 +
500 + /*
501 + * Same use of the sta lock as in ieee80211_check_fast_xmit, in order
502 + * to protect against concurrent sta key updates.
503 + */
504 + spin_lock_bh(&sta->lock);
505 + key = rcu_access_pointer(sta->ptk[sta->ptk_idx]);
506 + if (!key)
507 + key = rcu_access_pointer(sdata->default_unicast_key);
508 + build.fast_tx.key = key;
509 +
510 + if (key) {
511 + bool gen_iv, iv_spc;
512 +
513 + gen_iv = key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV;
514 + iv_spc = key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE;
515 +
516 + if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) ||
517 + (key->flags & KEY_FLAG_TAINTED))
518 + goto unlock_sta;
519 +
520 + switch (key->conf.cipher) {
521 + case WLAN_CIPHER_SUITE_CCMP:
522 + case WLAN_CIPHER_SUITE_CCMP_256:
523 + if (gen_iv)
524 + build.fast_tx.pn_offs = build.fast_tx.hdr_len;
525 + if (gen_iv || iv_spc)
526 + build.fast_tx.hdr_len += IEEE80211_CCMP_HDR_LEN;
527 + break;
528 + case WLAN_CIPHER_SUITE_GCMP:
529 + case WLAN_CIPHER_SUITE_GCMP_256:
530 + if (gen_iv)
531 + build.fast_tx.pn_offs = build.fast_tx.hdr_len;
532 + if (gen_iv || iv_spc)
533 + build.fast_tx.hdr_len += IEEE80211_GCMP_HDR_LEN;
534 + break;
535 + default:
536 + goto unlock_sta;
537 + }
538 + }
539 +
540 + memcpy(build.addr_key, mppath->dst, ETH_ALEN);
541 + build.timestamp = jiffies;
542 + build.fast_tx.band = info->band;
543 + build.fast_tx.da_offs = offsetof(struct ieee80211_hdr, addr3);
544 + build.fast_tx.sa_offs = offsetof(struct ieee80211_hdr, addr4);
545 + build.mpath = mpath;
546 + memcpy(build.hdr, meshhdr, build.hdrlen);
547 + memcpy(build.hdr + build.hdrlen, rfc1042_header, sizeof(rfc1042_header));
548 + build.hdrlen += sizeof(rfc1042_header);
549 + memcpy(build.fast_tx.hdr, hdr, build.fast_tx.hdr_len);
550 +
551 + hdr = (struct ieee80211_hdr *)build.fast_tx.hdr;
552 + if (build.fast_tx.key)
553 + hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
554 +
555 + qc = ieee80211_get_qos_ctl(hdr);
556 + qc[1] |= IEEE80211_QOS_CTL_MESH_CONTROL_PRESENT >> 8;
557 +
558 + entry = kmemdup(&build, sizeof(build), GFP_ATOMIC);
559 + if (!entry)
560 + goto unlock_sta;
561 +
562 + spin_lock(&cache->walk_lock);
563 + prev = rhashtable_lookup_get_insert_fast(&cache->rht,
564 + &entry->rhash,
565 + fast_tx_rht_params);
566 + if (unlikely(IS_ERR(prev))) {
567 + kfree(entry);
568 + goto unlock_cache;
569 + }
570 +
571 + /*
572 + * replace any previous entry in the hash table, in case we're
573 + * replacing it with a different type (e.g. mpath -> mpp)
574 + */
575 + if (unlikely(prev)) {
576 + rhashtable_replace_fast(&cache->rht, &prev->rhash,
577 + &entry->rhash, fast_tx_rht_params);
578 + hlist_del_rcu(&prev->walk_list);
579 + kfree_rcu(prev, fast_tx.rcu_head);
580 + }
581 +
582 + hlist_add_head(&entry->walk_list, &cache->walk_head);
583 +
584 +unlock_cache:
585 + spin_unlock(&cache->walk_lock);
586 +unlock_sta:
587 + spin_unlock_bh(&sta->lock);
588 +}
589 +
590 +void mesh_fast_tx_gc(struct ieee80211_sub_if_data *sdata)
591 +{
592 + unsigned long timeout = msecs_to_jiffies(MESH_FAST_TX_CACHE_TIMEOUT);
593 + struct mesh_tx_cache *cache;
594 + struct ieee80211_mesh_fast_tx *entry;
595 + struct hlist_node *n;
596 +
597 + cache = &sdata->u.mesh.tx_cache;
598 + if (atomic_read(&cache->rht.nelems) < MESH_FAST_TX_CACHE_THRESHOLD_SIZE)
599 + return;
600 +
601 + spin_lock_bh(&cache->walk_lock);
602 + hlist_for_each_entry_safe(entry, n, &cache->walk_head, walk_list)
603 + if (!time_is_after_jiffies(entry->timestamp + timeout))
604 + mesh_fast_tx_entry_free(cache, entry);
605 + spin_unlock_bh(&cache->walk_lock);
606 +}
607 +
608 +void mesh_fast_tx_flush_mpath(struct mesh_path *mpath)
609 +{
610 + struct ieee80211_sub_if_data *sdata = mpath->sdata;
611 + struct mesh_tx_cache *cache = &sdata->u.mesh.tx_cache;
612 + struct ieee80211_mesh_fast_tx *entry;
613 + struct hlist_node *n;
614 +
615 + cache = &sdata->u.mesh.tx_cache;
616 + spin_lock_bh(&cache->walk_lock);
617 + hlist_for_each_entry_safe(entry, n, &cache->walk_head, walk_list)
618 + if (entry->mpath == mpath)
619 + mesh_fast_tx_entry_free(cache, entry);
620 + spin_unlock_bh(&cache->walk_lock);
621 +}
622 +
623 +void mesh_fast_tx_flush_sta(struct ieee80211_sub_if_data *sdata,
624 + struct sta_info *sta)
625 +{
626 + struct mesh_tx_cache *cache = &sdata->u.mesh.tx_cache;
627 + struct ieee80211_mesh_fast_tx *entry;
628 + struct hlist_node *n;
629 +
630 + cache = &sdata->u.mesh.tx_cache;
631 + spin_lock_bh(&cache->walk_lock);
632 + hlist_for_each_entry_safe(entry, n, &cache->walk_head, walk_list)
633 + if (rcu_access_pointer(entry->mpath->next_hop) == sta)
634 + mesh_fast_tx_entry_free(cache, entry);
635 + spin_unlock_bh(&cache->walk_lock);
636 +}
637 +
638 +void mesh_fast_tx_flush_addr(struct ieee80211_sub_if_data *sdata,
639 + const u8 *addr)
640 +{
641 + struct mesh_tx_cache *cache = &sdata->u.mesh.tx_cache;
642 + struct ieee80211_mesh_fast_tx *entry;
643 +
644 + cache = &sdata->u.mesh.tx_cache;
645 + spin_lock_bh(&cache->walk_lock);
646 + entry = rhashtable_lookup(&cache->rht, addr, fast_tx_rht_params);
647 + if (entry)
648 + mesh_fast_tx_entry_free(cache, entry);
649 + spin_unlock_bh(&cache->walk_lock);
650 +}
651 +
652 /**
653 * mesh_path_add - allocate and add a new path to the mesh path table
654 * @dst: destination address of the path (ETH_ALEN length)
655 @@ -464,6 +737,8 @@ int mpp_path_add(struct ieee80211_sub_if
656
657 if (ret)
658 kfree(new_mpath);
659 + else
660 + mesh_fast_tx_flush_addr(sdata, dst);
661
662 sdata->u.mesh.mpp_paths_generation++;
663 return ret;
664 @@ -523,6 +798,10 @@ static void __mesh_path_del(struct mesh_
665 {
666 hlist_del_rcu(&mpath->walk_list);
667 rhashtable_remove_fast(&tbl->rhead, &mpath->rhash, mesh_rht_params);
668 + if (tbl == &mpath->sdata->u.mesh.mpp_paths)
669 + mesh_fast_tx_flush_addr(mpath->sdata, mpath->dst);
670 + else
671 + mesh_fast_tx_flush_mpath(mpath);
672 mesh_path_free_rcu(tbl, mpath);
673 }
674
675 @@ -747,6 +1026,7 @@ void mesh_path_fix_nexthop(struct mesh_p
676 mpath->exp_time = 0;
677 mpath->flags = MESH_PATH_FIXED | MESH_PATH_SN_VALID;
678 mesh_path_activate(mpath);
679 + mesh_fast_tx_flush_mpath(mpath);
680 spin_unlock_bh(&mpath->state_lock);
681 ewma_mesh_fail_avg_init(&next_hop->mesh->fail_avg);
682 /* init it at a low value - 0 start is tricky */
683 @@ -758,6 +1038,7 @@ void mesh_pathtbl_init(struct ieee80211_
684 {
685 mesh_table_init(&sdata->u.mesh.mesh_paths);
686 mesh_table_init(&sdata->u.mesh.mpp_paths);
687 + mesh_fast_tx_init(sdata);
688 }
689
690 static
691 @@ -785,6 +1066,7 @@ void mesh_path_expire(struct ieee80211_s
692
693 void mesh_pathtbl_unregister(struct ieee80211_sub_if_data *sdata)
694 {
695 + mesh_fast_tx_deinit(sdata);
696 mesh_table_free(&sdata->u.mesh.mesh_paths);
697 mesh_table_free(&sdata->u.mesh.mpp_paths);
698 }
699 --- a/net/mac80211/rx.c
700 +++ b/net/mac80211/rx.c
701 @@ -2791,6 +2791,7 @@ ieee80211_rx_mesh_data(struct ieee80211_
702 if (mesh_hdr->flags & MESH_FLAGS_AE) {
703 struct mesh_path *mppath;
704 char *proxied_addr;
705 + bool update = false;
706
707 if (multicast)
708 proxied_addr = mesh_hdr->eaddr1;
709 @@ -2806,11 +2807,18 @@ ieee80211_rx_mesh_data(struct ieee80211_
710 mpp_path_add(sdata, proxied_addr, eth->h_source);
711 } else {
712 spin_lock_bh(&mppath->state_lock);
713 - if (!ether_addr_equal(mppath->mpp, eth->h_source))
714 + if (!ether_addr_equal(mppath->mpp, eth->h_source)) {
715 memcpy(mppath->mpp, eth->h_source, ETH_ALEN);
716 + update = true;
717 + }
718 mppath->exp_time = jiffies;
719 spin_unlock_bh(&mppath->state_lock);
720 }
721 +
722 + /* flush fast xmit cache if the address path changed */
723 + if (update)
724 + mesh_fast_tx_flush_addr(sdata, proxied_addr);
725 +
726 rcu_read_unlock();
727 }
728
729 --- a/net/mac80211/tx.c
730 +++ b/net/mac80211/tx.c
731 @@ -3021,6 +3021,9 @@ void ieee80211_check_fast_xmit(struct st
732 if (!ieee80211_hw_check(&local->hw, SUPPORT_FAST_XMIT))
733 return;
734
735 + if (ieee80211_vif_is_mesh(&sdata->vif))
736 + mesh_fast_tx_flush_sta(sdata, sta);
737 +
738 /* Locking here protects both the pointer itself, and against concurrent
739 * invocations winning data access races to, e.g., the key pointer that
740 * is used.
741 @@ -3402,6 +3405,9 @@ static bool ieee80211_amsdu_aggregate(st
742 if (sdata->vif.offload_flags & IEEE80211_OFFLOAD_ENCAP_ENABLED)
743 return false;
744
745 + if (ieee80211_vif_is_mesh(&sdata->vif))
746 + return false;
747 +
748 if (skb_is_gso(skb))
749 return false;
750
751 @@ -3634,10 +3640,11 @@ free:
752 return NULL;
753 }
754
755 -static void __ieee80211_xmit_fast(struct ieee80211_sub_if_data *sdata,
756 - struct sta_info *sta,
757 - struct ieee80211_fast_tx *fast_tx,
758 - struct sk_buff *skb, u8 tid, bool ampdu)
759 +void __ieee80211_xmit_fast(struct ieee80211_sub_if_data *sdata,
760 + struct sta_info *sta,
761 + struct ieee80211_fast_tx *fast_tx,
762 + struct sk_buff *skb, bool ampdu,
763 + const u8 *da, const u8 *sa)
764 {
765 struct ieee80211_local *local = sdata->local;
766 struct ieee80211_hdr *hdr = (void *)fast_tx->hdr;
767 @@ -3646,7 +3653,6 @@ static void __ieee80211_xmit_fast(struct
768 ieee80211_tx_result r;
769 int hw_headroom = sdata->local->hw.extra_tx_headroom;
770 int extra_head = fast_tx->hdr_len - (ETH_HLEN - 2);
771 - struct ethhdr eth;
772
773 skb = skb_share_check(skb, GFP_ATOMIC);
774 if (unlikely(!skb))
775 @@ -3666,11 +3672,10 @@ static void __ieee80211_xmit_fast(struct
776 ENCRYPT_NO)))
777 goto free;
778
779 - memcpy(&eth, skb->data, ETH_HLEN - 2);
780 hdr = skb_push(skb, extra_head);
781 memcpy(skb->data, fast_tx->hdr, fast_tx->hdr_len);
782 - memcpy(skb->data + fast_tx->da_offs, eth.h_dest, ETH_ALEN);
783 - memcpy(skb->data + fast_tx->sa_offs, eth.h_source, ETH_ALEN);
784 + memcpy(skb->data + fast_tx->da_offs, da, ETH_ALEN);
785 + memcpy(skb->data + fast_tx->sa_offs, sa, ETH_ALEN);
786
787 info = IEEE80211_SKB_CB(skb);
788 memset(info, 0, sizeof(*info));
789 @@ -3689,7 +3694,8 @@ static void __ieee80211_xmit_fast(struct
790 #endif
791
792 if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
793 - tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
794 + u8 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
795 +
796 *ieee80211_get_qos_ctl(hdr) = tid;
797 }
798
799 @@ -3732,6 +3738,7 @@ static bool ieee80211_xmit_fast(struct i
800 struct ieee80211_hdr *hdr = (void *)fast_tx->hdr;
801 struct tid_ampdu_tx *tid_tx = NULL;
802 struct sk_buff *next;
803 + struct ethhdr eth;
804 u8 tid = IEEE80211_NUM_TIDS;
805
806 /* control port protocol needs a lot of special handling */
807 @@ -3757,6 +3764,8 @@ static bool ieee80211_xmit_fast(struct i
808 }
809 }
810
811 + memcpy(&eth, skb->data, ETH_HLEN - 2);
812 +
813 /* after this point (skb is modified) we cannot return false */
814 skb = ieee80211_tx_skb_fixup(skb, ieee80211_sdata_netdev_features(sdata));
815 if (!skb)
816 @@ -3764,7 +3773,8 @@ static bool ieee80211_xmit_fast(struct i
817
818 skb_list_walk_safe(skb, skb, next) {
819 skb_mark_not_on_list(skb);
820 - __ieee80211_xmit_fast(sdata, sta, fast_tx, skb, tid, tid_tx);
821 + __ieee80211_xmit_fast(sdata, sta, fast_tx, skb, tid_tx,
822 + eth.h_dest, eth.h_source);
823 }
824
825 return true;
826 @@ -4244,8 +4254,15 @@ void __ieee80211_subif_start_xmit(struct
827 return;
828 }
829
830 + sk_pacing_shift_update(skb->sk, sdata->local->hw.tx_sk_pacing_shift);
831 +
832 rcu_read_lock();
833
834 + if (ieee80211_vif_is_mesh(&sdata->vif) &&
835 + ieee80211_hw_check(&local->hw, SUPPORT_FAST_XMIT) &&
836 + ieee80211_mesh_xmit_fast(sdata, skb, ctrl_flags))
837 + goto out;
838 +
839 if (ieee80211_lookup_ra_sta(sdata, skb, &sta))
840 goto out_free;
841
842 @@ -4255,8 +4272,6 @@ void __ieee80211_subif_start_xmit(struct
843 skb_set_queue_mapping(skb, ieee80211_select_queue(sdata, sta, skb));
844 ieee80211_aggr_check(sdata, sta, skb);
845
846 - sk_pacing_shift_update(skb->sk, sdata->local->hw.tx_sk_pacing_shift);
847 -
848 if (sta) {
849 struct ieee80211_fast_tx *fast_tx;
850