1 From: Felix Fietkau <nbd@nbd.name>
2 Date: Wed, 16 Dec 2020 21:34:03 +0100
3 Subject: [PATCH] mac80211: add rx decapsulation offload support
5 This allows drivers to pass 802.3 frames to mac80211, with some restrictions:
7 - the skb must be passed with a valid sta
8 - fast-rx needs to be active for the sta
9 - monitor mode needs to be disabled
11 mac80211 will tell the driver when it is safe to enable rx decap offload for
14 In order to implement support, a driver must:
16 - call ieee80211_hw_set(hw, SUPPORTS_RX_DECAP_OFFLOAD)
17 - implement ops->sta_set_decap_offload
18 - mark 802.3 frames with RX_FLAG_8023
20 If it doesn't want to enable offload for some vif types, it can mask out
21 IEEE80211_OFFLOAD_DECAP_ENABLED in vif->offload_flags from within the
22 .add_interface or .update_vif_offload driver ops
24 Signed-off-by: Felix Fietkau <nbd@nbd.name>
27 --- a/include/net/mac80211.h
28 +++ b/include/net/mac80211.h
29 @@ -1300,6 +1300,8 @@ ieee80211_tx_info_clear_status(struct ie
30 * the "0-length PSDU" field included there. The value for it is
31 * in &struct ieee80211_rx_status. Note that if this value isn't
32 * known the frame shouldn't be reported.
33 + * @RX_FLAG_8023: the frame has an 802.3 header (decap offload performed by
34 + * hardware or driver)
36 enum mac80211_rx_flags {
37 RX_FLAG_MMIC_ERROR = BIT(0),
38 @@ -1332,6 +1334,7 @@ enum mac80211_rx_flags {
39 RX_FLAG_RADIOTAP_HE_MU = BIT(27),
40 RX_FLAG_RADIOTAP_LSIG = BIT(28),
41 RX_FLAG_NO_PSDU = BIT(29),
42 + RX_FLAG_8023 = BIT(30),
46 @@ -1653,11 +1656,15 @@ enum ieee80211_vif_flags {
47 * The driver supports sending frames passed as 802.3 frames by mac80211.
48 * It must also support sending 802.11 packets for the same interface.
49 * @IEEE80211_OFFLOAD_ENCAP_4ADDR: support 4-address mode encapsulation offload
50 + * @IEEE80211_OFFLOAD_DECAP_ENABLED: rx encapsulation offload is enabled
51 + * The driver supports passing received 802.11 frames as 802.3 frames to
55 enum ieee80211_offload_flags {
56 IEEE80211_OFFLOAD_ENCAP_ENABLED = BIT(0),
57 IEEE80211_OFFLOAD_ENCAP_4ADDR = BIT(1),
58 + IEEE80211_OFFLOAD_DECAP_ENABLED = BIT(2),
62 @@ -2393,6 +2400,9 @@ struct ieee80211_txq {
63 * @IEEE80211_HW_SUPPORTS_TX_ENCAP_OFFLOAD: Hardware supports tx encapsulation
66 + * @IEEE80211_HW_SUPPORTS_RX_DECAP_OFFLOAD: Hardware supports rx decapsulation
69 * @NUM_IEEE80211_HW_FLAGS: number of hardware flags, used for sizing arrays
71 enum ieee80211_hw_flags {
72 @@ -2446,6 +2456,7 @@ enum ieee80211_hw_flags {
73 IEEE80211_HW_SUPPORTS_ONLY_HE_MULTI_BSSID,
74 IEEE80211_HW_AMPDU_KEYBORDER_SUPPORT,
75 IEEE80211_HW_SUPPORTS_TX_ENCAP_OFFLOAD,
76 + IEEE80211_HW_SUPPORTS_RX_DECAP_OFFLOAD,
78 /* keep last, obviously */
79 NUM_IEEE80211_HW_FLAGS
80 @@ -4202,6 +4213,9 @@ struct ieee80211_ops {
81 struct ieee80211_sta *sta, bool enabled);
82 int (*set_sar_specs)(struct ieee80211_hw *hw,
83 const struct cfg80211_sar_specs *sar);
84 + void (*sta_set_decap_offload)(struct ieee80211_hw *hw,
85 + struct ieee80211_vif *vif,
86 + struct ieee80211_sta *sta, bool enabled);
90 --- a/net/mac80211/debugfs.c
91 +++ b/net/mac80211/debugfs.c
92 @@ -405,6 +405,7 @@ static const char *hw_flag_names[] = {
93 FLAG(SUPPORTS_ONLY_HE_MULTI_BSSID),
94 FLAG(AMPDU_KEYBORDER_SUPPORT),
95 FLAG(SUPPORTS_TX_ENCAP_OFFLOAD),
96 + FLAG(SUPPORTS_RX_DECAP_OFFLOAD),
100 --- a/net/mac80211/debugfs_sta.c
101 +++ b/net/mac80211/debugfs_sta.c
102 @@ -79,6 +79,7 @@ static const char * const sta_flag_names
103 FLAG(MPSP_RECIPIENT),
105 FLAG(USES_ENCRYPTION),
106 + FLAG(DECAP_OFFLOAD),
110 --- a/net/mac80211/driver-ops.h
111 +++ b/net/mac80211/driver-ops.h
112 @@ -1413,4 +1413,20 @@ static inline void drv_sta_set_4addr(str
113 trace_drv_return_void(local);
116 +static inline void drv_sta_set_decap_offload(struct ieee80211_local *local,
117 + struct ieee80211_sub_if_data *sdata,
118 + struct ieee80211_sta *sta,
121 + sdata = get_bss_sdata(sdata);
122 + if (!check_sdata_in_driver(sdata))
125 + trace_drv_sta_set_decap_offload(local, sdata, sta, enabled);
126 + if (local->ops->sta_set_decap_offload)
127 + local->ops->sta_set_decap_offload(&local->hw, &sdata->vif, sta,
129 + trace_drv_return_void(local);
132 #endif /* __MAC80211_DRIVER_OPS */
133 --- a/net/mac80211/iface.c
134 +++ b/net/mac80211/iface.c
135 @@ -815,7 +815,7 @@ static const struct net_device_ops ieee8
139 -static bool ieee80211_iftype_supports_encap_offload(enum nl80211_iftype iftype)
140 +static bool ieee80211_iftype_supports_hdr_offload(enum nl80211_iftype iftype)
143 /* P2P GO and client are mapped to AP/STATION types */
144 @@ -835,7 +835,7 @@ static bool ieee80211_set_sdata_offload_
145 flags = sdata->vif.offload_flags;
147 if (ieee80211_hw_check(&local->hw, SUPPORTS_TX_ENCAP_OFFLOAD) &&
148 - ieee80211_iftype_supports_encap_offload(sdata->vif.type)) {
149 + ieee80211_iftype_supports_hdr_offload(sdata->vif.type)) {
150 flags |= IEEE80211_OFFLOAD_ENCAP_ENABLED;
152 if (!ieee80211_hw_check(&local->hw, SUPPORTS_TX_FRAG) &&
153 @@ -848,10 +848,21 @@ static bool ieee80211_set_sdata_offload_
154 flags &= ~IEEE80211_OFFLOAD_ENCAP_ENABLED;
157 + if (ieee80211_hw_check(&local->hw, SUPPORTS_RX_DECAP_OFFLOAD) &&
158 + ieee80211_iftype_supports_hdr_offload(sdata->vif.type)) {
159 + flags |= IEEE80211_OFFLOAD_DECAP_ENABLED;
161 + if (local->monitors)
162 + flags &= ~IEEE80211_OFFLOAD_DECAP_ENABLED;
164 + flags &= ~IEEE80211_OFFLOAD_DECAP_ENABLED;
167 if (sdata->vif.offload_flags == flags)
170 sdata->vif.offload_flags = flags;
171 + ieee80211_check_fast_rx_iface(sdata);
175 @@ -869,7 +880,7 @@ static void ieee80211_set_vif_encap_ops(
178 if (!ieee80211_hw_check(&local->hw, SUPPORTS_TX_ENCAP_OFFLOAD) ||
179 - !ieee80211_iftype_supports_encap_offload(bss->vif.type))
180 + !ieee80211_iftype_supports_hdr_offload(bss->vif.type))
183 enabled = bss->vif.offload_flags & IEEE80211_OFFLOAD_ENCAP_ENABLED;
184 --- a/net/mac80211/rx.c
185 +++ b/net/mac80211/rx.c
186 @@ -4099,7 +4099,9 @@ void ieee80211_check_fast_rx(struct sta_
187 .vif_type = sdata->vif.type,
188 .control_port_protocol = sdata->control_port_protocol,
189 }, *old, *new = NULL;
190 + bool set_offload = false;
194 /* use sparse to check that we don't return without updating */
195 __acquire(check_fast_rx);
196 @@ -4212,6 +4214,17 @@ void ieee80211_check_fast_rx(struct sta_
198 new = kmemdup(&fastrx, sizeof(fastrx), GFP_KERNEL);
200 + offload = assign &&
201 + (sdata->vif.offload_flags & IEEE80211_OFFLOAD_DECAP_ENABLED);
204 + set_offload = !test_and_set_sta_flag(sta, WLAN_STA_DECAP_OFFLOAD);
206 + set_offload = test_and_clear_sta_flag(sta, WLAN_STA_DECAP_OFFLOAD);
209 + drv_sta_set_decap_offload(local, sdata, &sta->sta, assign);
211 spin_lock_bh(&sta->lock);
212 old = rcu_dereference_protected(sta->fast_rx, true);
213 rcu_assign_pointer(sta->fast_rx, new);
214 @@ -4258,6 +4271,108 @@ void ieee80211_check_fast_rx_iface(struc
215 mutex_unlock(&local->sta_mtx);
218 +static void ieee80211_rx_8023(struct ieee80211_rx_data *rx,
219 + struct ieee80211_fast_rx *fast_rx,
222 + struct ieee80211_sta_rx_stats *stats;
223 + struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
224 + struct sta_info *sta = rx->sta;
225 + struct sk_buff *skb = rx->skb;
226 + void *sa = skb->data + ETH_ALEN;
227 + void *da = skb->data;
229 + stats = &sta->rx_stats;
230 + if (fast_rx->uses_rss)
231 + stats = this_cpu_ptr(sta->pcpu_rx_stats);
233 + /* statistics part of ieee80211_rx_h_sta_process() */
234 + if (!(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
235 + stats->last_signal = status->signal;
236 + if (!fast_rx->uses_rss)
237 + ewma_signal_add(&sta->rx_stats_avg.signal,
241 + if (status->chains) {
244 + stats->chains = status->chains;
245 + for (i = 0; i < ARRAY_SIZE(status->chain_signal); i++) {
246 + int signal = status->chain_signal[i];
248 + if (!(status->chains & BIT(i)))
251 + stats->chain_signal_last[i] = signal;
252 + if (!fast_rx->uses_rss)
253 + ewma_signal_add(&sta->rx_stats_avg.chain_signal[i],
257 + /* end of statistics */
259 + stats->last_rx = jiffies;
260 + stats->last_rate = sta_stats_encode_rate(status);
262 + stats->fragments++;
265 + skb->dev = fast_rx->dev;
267 + dev_sw_netstats_rx_add(fast_rx->dev, skb->len);
269 + /* The seqno index has the same property as needed
270 + * for the rx_msdu field, i.e. it is IEEE80211_NUM_TIDS
271 + * for non-QoS-data frames. Here we know it's a data
272 + * frame, so count MSDUs.
274 + u64_stats_update_begin(&stats->syncp);
275 + stats->msdu[rx->seqno_idx]++;
276 + stats->bytes += orig_len;
277 + u64_stats_update_end(&stats->syncp);
279 + if (fast_rx->internal_forward) {
280 + struct sk_buff *xmit_skb = NULL;
281 + if (is_multicast_ether_addr(da)) {
282 + xmit_skb = skb_copy(skb, GFP_ATOMIC);
283 + } else if (!ether_addr_equal(da, sa) &&
284 + sta_info_get(rx->sdata, da)) {
291 + * Send to wireless media and increase priority by 256
292 + * to keep the received priority instead of
293 + * reclassifying the frame (see cfg80211_classify8021d).
295 + xmit_skb->priority += 256;
296 + xmit_skb->protocol = htons(ETH_P_802_3);
297 + skb_reset_network_header(xmit_skb);
298 + skb_reset_mac_header(xmit_skb);
299 + dev_queue_xmit(xmit_skb);
306 + /* deliver to local stack */
307 + skb->protocol = eth_type_trans(skb, fast_rx->dev);
308 + memset(skb->cb, 0, sizeof(skb->cb));
310 +#if LINUX_VERSION_IS_GEQ(4,19,0)
311 + list_add_tail(&skb->list, rx->list);
313 + __skb_queue_tail(rx->list, skb);
316 + netif_receive_skb(skb);
320 static bool ieee80211_invoke_fast_rx(struct ieee80211_rx_data *rx,
321 struct ieee80211_fast_rx *fast_rx)
323 @@ -4278,9 +4393,6 @@ static bool ieee80211_invoke_fast_rx(str
324 } addrs __aligned(2);
325 struct ieee80211_sta_rx_stats *stats = &sta->rx_stats;
327 - if (fast_rx->uses_rss)
328 - stats = this_cpu_ptr(sta->pcpu_rx_stats);
330 /* for parallel-rx, we need to have DUP_VALIDATED, otherwise we write
331 * to a common data structure; drivers can implement that per queue
332 * but we don't have that information in mac80211
333 @@ -4354,32 +4466,6 @@ static bool ieee80211_invoke_fast_rx(str
334 pskb_trim(skb, skb->len - fast_rx->icv_len))
337 - /* statistics part of ieee80211_rx_h_sta_process() */
338 - if (!(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
339 - stats->last_signal = status->signal;
340 - if (!fast_rx->uses_rss)
341 - ewma_signal_add(&sta->rx_stats_avg.signal,
345 - if (status->chains) {
348 - stats->chains = status->chains;
349 - for (i = 0; i < ARRAY_SIZE(status->chain_signal); i++) {
350 - int signal = status->chain_signal[i];
352 - if (!(status->chains & BIT(i)))
355 - stats->chain_signal_last[i] = signal;
356 - if (!fast_rx->uses_rss)
357 - ewma_signal_add(&sta->rx_stats_avg.chain_signal[i],
361 - /* end of statistics */
363 if (rx->key && !ieee80211_has_protected(hdr->frame_control))
366 @@ -4391,12 +4477,6 @@ static bool ieee80211_invoke_fast_rx(str
370 - stats->last_rx = jiffies;
371 - stats->last_rate = sta_stats_encode_rate(status);
373 - stats->fragments++;
376 /* do the header conversion - first grab the addresses */
377 ether_addr_copy(addrs.da, skb->data + fast_rx->da_offs);
378 ether_addr_copy(addrs.sa, skb->data + fast_rx->sa_offs);
379 @@ -4405,62 +4485,14 @@ static bool ieee80211_invoke_fast_rx(str
380 /* push the addresses in front */
381 memcpy(skb_push(skb, sizeof(addrs)), &addrs, sizeof(addrs));
383 - skb->dev = fast_rx->dev;
385 - dev_sw_netstats_rx_add(fast_rx->dev, skb->len);
387 - /* The seqno index has the same property as needed
388 - * for the rx_msdu field, i.e. it is IEEE80211_NUM_TIDS
389 - * for non-QoS-data frames. Here we know it's a data
390 - * frame, so count MSDUs.
392 - u64_stats_update_begin(&stats->syncp);
393 - stats->msdu[rx->seqno_idx]++;
394 - stats->bytes += orig_len;
395 - u64_stats_update_end(&stats->syncp);
397 - if (fast_rx->internal_forward) {
398 - struct sk_buff *xmit_skb = NULL;
399 - if (is_multicast_ether_addr(addrs.da)) {
400 - xmit_skb = skb_copy(skb, GFP_ATOMIC);
401 - } else if (!ether_addr_equal(addrs.da, addrs.sa) &&
402 - sta_info_get(rx->sdata, addrs.da)) {
409 - * Send to wireless media and increase priority by 256
410 - * to keep the received priority instead of
411 - * reclassifying the frame (see cfg80211_classify8021d).
413 - xmit_skb->priority += 256;
414 - xmit_skb->protocol = htons(ETH_P_802_3);
415 - skb_reset_network_header(xmit_skb);
416 - skb_reset_mac_header(xmit_skb);
417 - dev_queue_xmit(xmit_skb);
424 - /* deliver to local stack */
425 - skb->protocol = eth_type_trans(skb, fast_rx->dev);
426 - memset(skb->cb, 0, sizeof(skb->cb));
428 -#if LINUX_VERSION_IS_GEQ(4,19,0)
429 - list_add_tail(&skb->list, rx->list);
431 - __skb_queue_tail(rx->list, skb);
434 - netif_receive_skb(skb);
435 + ieee80211_rx_8023(rx, fast_rx, orig_len);
440 + if (fast_rx->uses_rss)
441 + stats = this_cpu_ptr(sta->pcpu_rx_stats);
446 @@ -4514,6 +4546,47 @@ static bool ieee80211_prepare_and_rx_han
450 +static void __ieee80211_rx_handle_8023(struct ieee80211_hw *hw,
451 + struct ieee80211_sta *pubsta,
452 + struct sk_buff *skb,
453 +#if LINUX_VERSION_IS_GEQ(4,19,0)
454 + struct list_head *list)
456 + struct sk_buff_head *list)
459 + struct ieee80211_local *local = hw_to_local(hw);
460 + struct ieee80211_fast_rx *fast_rx;
461 + struct ieee80211_rx_data rx;
463 + memset(&rx, 0, sizeof(rx));
468 + I802_DEBUG_INC(local->dot11ReceivedFragmentCount);
470 + /* drop frame if too short for header */
471 + if (skb->len < sizeof(struct ethhdr))
477 + rx.sta = container_of(pubsta, struct sta_info, sta);
478 + rx.sdata = rx.sta->sdata;
480 + fast_rx = rcu_dereference(rx.sta->fast_rx);
484 + ieee80211_rx_8023(&rx, fast_rx, skb->len);
488 + dev_kfree_skb(skb);
492 * This is the actual Rx frames handler. as it belongs to Rx path it must
493 * be called with rcu_read_lock protection.
494 @@ -4753,13 +4826,17 @@ void ieee80211_rx_list(struct ieee80211_
495 * if it was previously present.
496 * Also, frames with less than 16 bytes are dropped.
498 - skb = ieee80211_rx_monitor(local, skb, rate);
499 + if (!(status->flag & RX_FLAG_8023))
500 + skb = ieee80211_rx_monitor(local, skb, rate);
502 ieee80211_tpt_led_trig_rx(local,
503 ((struct ieee80211_hdr *)skb->data)->frame_control,
506 - __ieee80211_rx_handle_packet(hw, pubsta, skb, list);
507 + if (status->flag & RX_FLAG_8023)
508 + __ieee80211_rx_handle_8023(hw, pubsta, skb, list);
510 + __ieee80211_rx_handle_packet(hw, pubsta, skb, list);
514 --- a/net/mac80211/sta_info.h
515 +++ b/net/mac80211/sta_info.h
517 * until pending frames are delivered
518 * @WLAN_STA_USES_ENCRYPTION: This station was configured for encryption,
519 * so drop all packets without a key later.
520 + * @WLAN_STA_DECAP_OFFLOAD: This station uses rx decap offload
522 * @NUM_WLAN_STA_FLAGS: number of defined flags
524 @@ -102,6 +103,7 @@ enum ieee80211_sta_info_flags {
525 WLAN_STA_MPSP_RECIPIENT,
527 WLAN_STA_USES_ENCRYPTION,
528 + WLAN_STA_DECAP_OFFLOAD,
532 --- a/net/mac80211/trace.h
533 +++ b/net/mac80211/trace.h
534 @@ -2761,7 +2761,7 @@ DEFINE_EVENT(local_sdata_addr_evt, drv_u
535 TP_ARGS(local, sdata)
538 -TRACE_EVENT(drv_sta_set_4addr,
539 +DECLARE_EVENT_CLASS(sta_flag_evt,
540 TP_PROTO(struct ieee80211_local *local,
541 struct ieee80211_sub_if_data *sdata,
542 struct ieee80211_sta *sta, bool enabled),
543 @@ -2788,6 +2788,22 @@ TRACE_EVENT(drv_sta_set_4addr,
547 +DEFINE_EVENT(sta_flag_evt, drv_sta_set_4addr,
548 + TP_PROTO(struct ieee80211_local *local,
549 + struct ieee80211_sub_if_data *sdata,
550 + struct ieee80211_sta *sta, bool enabled),
552 + TP_ARGS(local, sdata, sta, enabled)
555 +DEFINE_EVENT(sta_flag_evt, drv_sta_set_decap_offload,
556 + TP_PROTO(struct ieee80211_local *local,
557 + struct ieee80211_sub_if_data *sdata,
558 + struct ieee80211_sta *sta, bool enabled),
560 + TP_ARGS(local, sdata, sta, enabled)
563 #endif /* !__MAC80211_DRIVER_TRACE || TRACE_HEADER_MULTI_READ */
565 #undef TRACE_INCLUDE_PATH