mac80211: add an intermediate software queueing implementation
[openwrt.git] / package / kernel / mac80211 / patches / 321-mac80211-add-an-intermediate-software-queue-implemen.patch
1 From: Felix Fietkau <nbd@openwrt.org>
2 Date: Tue, 18 Nov 2014 23:58:51 +0100
3 Subject: [PATCH] mac80211: add an intermediate software queue implementation
4
5 This allows drivers to request per-vif and per-sta-tid queues from which
6 they can pull frames. This makes it easier to keep the hardware queues
7 short, and to improve fairness between clients and vifs.
8
9 The task of scheduling packet transmission is left up to the driver -
10 queueing is controlled by mac80211. Drivers can only dequeue packets by
11 calling ieee80211_tx_dequeue. This makes it possible to add active queue
12 management later without changing drivers using this code.
13
14 This can also be used as a starting point to implement A-MSDU
15 aggregation in a way that does not add artificially induced latency.
16
17 Signed-off-by: Felix Fietkau <nbd@openwrt.org>
18 ---
19
20 --- a/include/net/mac80211.h
21 +++ b/include/net/mac80211.h
22 @@ -1192,6 +1192,8 @@ struct ieee80211_vif {
23         u8 cab_queue;
24         u8 hw_queue[IEEE80211_NUM_ACS];
25  
26 +       struct ieee80211_txq *txq;
27 +
28         struct ieee80211_chanctx_conf __rcu *chanctx_conf;
29  
30         u32 driver_flags;
31 @@ -1448,6 +1450,8 @@ struct ieee80211_sta {
32         bool tdls;
33         bool tdls_initiator;
34  
35 +       struct ieee80211_txq *txq[IEEE80211_NUM_TIDS];
36 +
37         /* must be last */
38         u8 drv_priv[0] __aligned(sizeof(void *));
39  };
40 @@ -1476,6 +1480,27 @@ struct ieee80211_tx_control {
41  };
42  
43  /**
44 + * struct ieee80211_txq - Software intermediate tx queue
45 + *
46 + * @vif: &struct ieee80211_vif pointer from the add_interface callback.
47 + * @sta: station table entry, may be NULL for per-vif queue
48 + * @tid: the TID for this queue (unset for per-vif queue)
49 + * @ac: the AC for this queue
50 + *
51 + * The driver can obtain packets from this queue by calling
52 + * ieee80211_tx_dequeue().
53 + */
54 +struct ieee80211_txq {
55 +       struct ieee80211_vif *vif;
56 +       struct ieee80211_sta *sta;
57 +       u8 tid;
58 +       u8 ac;
59 +
60 +       /* must be last */
61 +       u8 drv_priv[0] __aligned(sizeof(void *));
62 +};
63 +
64 +/**
65   * enum ieee80211_hw_flags - hardware flags
66   *
67   * These flags are used to indicate hardware capabilities to
68 @@ -1698,6 +1723,8 @@ enum ieee80211_hw_flags {
69   *     within &struct ieee80211_sta.
70   * @chanctx_data_size: size (in bytes) of the drv_priv data area
71   *     within &struct ieee80211_chanctx_conf.
72 + * @txq_data_size: size (in bytes) of the drv_priv data area
73 + *     within @struct ieee80211_txq.
74   *
75   * @max_rates: maximum number of alternate rate retry stages the hw
76   *     can handle.
77 @@ -1746,6 +1773,9 @@ enum ieee80211_hw_flags {
78   * @n_cipher_schemes: a size of an array of cipher schemes definitions.
79   * @cipher_schemes: a pointer to an array of cipher scheme definitions
80   *     supported by HW.
81 + *
82 + * @txq_ac_max_pending: maximum number of frames per AC pending in all txq
83 + *     entries for a vif.
84   */
85  struct ieee80211_hw {
86         struct ieee80211_conf conf;
87 @@ -1758,6 +1788,7 @@ struct ieee80211_hw {
88         int vif_data_size;
89         int sta_data_size;
90         int chanctx_data_size;
91 +       int txq_data_size;
92         u16 queues;
93         u16 max_listen_interval;
94         s8 max_signal;
95 @@ -1774,6 +1805,7 @@ struct ieee80211_hw {
96         u8 uapsd_max_sp_len;
97         u8 n_cipher_schemes;
98         const struct ieee80211_cipher_scheme *cipher_schemes;
99 +       int txq_ac_max_pending;
100  };
101  
102  /**
103 @@ -2881,6 +2913,8 @@ enum ieee80211_reconfig_type {
104   *
105   * @get_txpower: get current maximum tx power (in dBm) based on configuration
106   *     and hardware limits.
107 + *
108 + * @wake_tx_queue: Called when new packets have been added to the queue.
109   */
110  struct ieee80211_ops {
111         void (*tx)(struct ieee80211_hw *hw,
112 @@ -3095,6 +3129,9 @@ struct ieee80211_ops {
113         u32 (*get_expected_throughput)(struct ieee80211_sta *sta);
114         int (*get_txpower)(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
115                            int *dbm);
116 +
117 +       void (*wake_tx_queue)(struct ieee80211_hw *hw,
118 +                             struct ieee80211_txq *txq);
119  };
120  
121  /**
122 @@ -5038,4 +5075,17 @@ void ieee80211_tdls_oper_request(struct 
123   */
124  size_t ieee80211_ie_split(const u8 *ies, size_t ielen,
125                           const u8 *ids, int n_ids, size_t offset);
126 +
127 +/**
128 + * ieee80211_tx_dequeue - dequeue a packet from a software tx queue
129 + *
130 + * @hw: pointer as obtained from ieee80211_alloc_hw()
131 + * @txq: pointer obtained from .add_tx_queue() call
132 + *
133 + * Returns 0 if successful, -EAGAIN if no frame was available.
134 + */
135 +int ieee80211_tx_dequeue(struct ieee80211_hw *hw, struct ieee80211_txq *txq,
136 +                        struct sk_buff **skb);
137 +
138 +
139  #endif /* MAC80211_H */
140 --- a/net/mac80211/driver-ops.h
141 +++ b/net/mac80211/driver-ops.h
142 @@ -1311,4 +1311,21 @@ static inline int drv_get_txpower(struct
143         return ret;
144  }
145  
146 +static inline void drv_wake_tx_queue(struct ieee80211_local *local,
147 +                                    struct txq_info *txq)
148 +{
149 +       struct ieee80211_sub_if_data *sdata = vif_to_sdata(txq->txq.vif);
150 +
151 +       if (!check_sdata_in_driver(sdata))
152 +               return;
153 +
154 +       if (txq->txq.sta)
155 +               trace_drv_wake_sta_tx_queue(local, sdata, txq->txq.sta,
156 +                                           txq->txq.tid);
157 +       else
158 +               trace_drv_wake_vif_tx_queue(local, sdata);
159 +
160 +       local->ops->wake_tx_queue(&local->hw, &txq->txq);
161 +}
162 +
163  #endif /* __MAC80211_DRIVER_OPS */
164 --- a/net/mac80211/ieee80211_i.h
165 +++ b/net/mac80211/ieee80211_i.h
166 @@ -793,6 +793,13 @@ struct mac80211_qos_map {
167         struct rcu_head rcu_head;
168  };
169  
170 +struct txq_info {
171 +       struct sk_buff_head queue;
172 +
173 +       /* keep last! */
174 +       struct ieee80211_txq txq;
175 +};
176 +
177  struct ieee80211_sub_if_data {
178         struct list_head list;
179  
180 @@ -837,6 +844,8 @@ struct ieee80211_sub_if_data {
181         bool control_port_no_encrypt;
182         int encrypt_headroom;
183  
184 +       struct txq_info *txq;
185 +       atomic_t txq_len[IEEE80211_NUM_ACS];
186         struct ieee80211_tx_queue_params tx_conf[IEEE80211_NUM_ACS];
187         struct mac80211_qos_map __rcu *qos_map;
188  
189 @@ -1868,6 +1877,11 @@ void ieee80211_add_pending_skbs(struct i
190                                 struct sk_buff_head *skbs);
191  void ieee80211_flush_queues(struct ieee80211_local *local,
192                             struct ieee80211_sub_if_data *sdata);
193 +void ieee80211_init_tx_queue(struct ieee80211_sub_if_data *sdata,
194 +                            struct sta_info *sta,
195 +                            struct txq_info *txq, int tid);
196 +void ieee80211_flush_tx_queue(struct ieee80211_local *local,
197 +                             struct ieee80211_txq *txq);
198  
199  void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
200                          u16 transaction, u16 auth_alg, u16 status,
201 --- a/net/mac80211/iface.c
202 +++ b/net/mac80211/iface.c
203 @@ -967,6 +967,9 @@ static void ieee80211_do_stop(struct iee
204         }
205         spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
206  
207 +       if (sdata->vif.txq)
208 +               ieee80211_flush_tx_queue(local, sdata->vif.txq);
209 +
210         if (local->open_count == 0)
211                 ieee80211_clear_tx_pending(local);
212  
213 @@ -1773,6 +1776,13 @@ int ieee80211_if_add(struct ieee80211_lo
214         /* setup type-dependent data */
215         ieee80211_setup_sdata(sdata, type);
216  
217 +       if (local->ops->wake_tx_queue) {
218 +               sdata->txq = kzalloc(sizeof(*sdata->txq) +
219 +                                    local->hw.txq_data_size, GFP_KERNEL);
220 +               if (sdata->txq)
221 +                       ieee80211_init_tx_queue(sdata, NULL, sdata->txq, 0);
222 +       }
223 +
224         if (ndev) {
225                 if (params) {
226                         ndev->ieee80211_ptr->use_4addr = params->use_4addr;
227 --- a/net/mac80211/main.c
228 +++ b/net/mac80211/main.c
229 @@ -1004,6 +1004,9 @@ int ieee80211_register_hw(struct ieee802
230  
231         local->dynamic_ps_forced_timeout = -1;
232  
233 +       if (!local->hw.txq_ac_max_pending)
234 +               local->hw.txq_ac_max_pending = 64;
235 +
236         result = ieee80211_wep_init(local);
237         if (result < 0)
238                 wiphy_debug(local->hw.wiphy, "Failed to initialize wep: %d\n",
239 --- a/net/mac80211/sta_info.c
240 +++ b/net/mac80211/sta_info.c
241 @@ -119,6 +119,11 @@ static void __cleanup_single_sta(struct 
242                 sta_info_recalc_tim(sta);
243         }
244  
245 +       if (sta->txq) {
246 +               for (i = 0; i < IEEE80211_NUM_TIDS; i++)
247 +                       ieee80211_flush_tx_queue(local, sta->sta.txq[i]);
248 +       }
249 +
250         for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
251                 local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
252                 ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
253 @@ -241,6 +246,8 @@ void sta_info_free(struct ieee80211_loca
254                 kfree(sta->tx_lat);
255         }
256  
257 +       kfree(sta->txq);
258 +
259         sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
260  
261         kfree(rcu_dereference_raw(sta->sta.rates));
262 @@ -294,12 +301,13 @@ struct sta_info *sta_info_alloc(struct i
263                                 const u8 *addr, gfp_t gfp)
264  {
265         struct ieee80211_local *local = sdata->local;
266 +       struct ieee80211_hw *hw = &local->hw;
267         struct sta_info *sta;
268         struct timespec uptime;
269         struct ieee80211_tx_latency_bin_ranges *tx_latency;
270         int i;
271  
272 -       sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp);
273 +       sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
274         if (!sta)
275                 return NULL;
276  
277 @@ -357,6 +365,20 @@ struct sta_info *sta_info_alloc(struct i
278         for (i = 0; i < ARRAY_SIZE(sta->chain_signal_avg); i++)
279                 ewma_init(&sta->chain_signal_avg[i], 1024, 8);
280  
281 +       if (local->ops->wake_tx_queue) {
282 +               int size = sizeof(struct txq_info) +
283 +                          ALIGN(hw->txq_data_size, sizeof(void *));
284 +
285 +               sta->txq = kcalloc(IEEE80211_NUM_TIDS, size, gfp);
286 +               if (!sta->txq)
287 +                       goto free;
288 +
289 +               for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
290 +                       struct txq_info *txq = sta->txq + i * size;
291 +                       ieee80211_init_tx_queue(sdata, sta, txq, i);
292 +               }
293 +       }
294 +
295         if (sta_prepare_rate_control(local, sta, gfp))
296                 goto free;
297  
298 @@ -380,7 +402,7 @@ struct sta_info *sta_info_alloc(struct i
299         if (sdata->vif.type == NL80211_IFTYPE_AP ||
300             sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
301                 struct ieee80211_supported_band *sband =
302 -                       local->hw.wiphy->bands[ieee80211_get_sdata_band(sdata)];
303 +                       hw->wiphy->bands[ieee80211_get_sdata_band(sdata)];
304                 u8 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
305                                 IEEE80211_HT_CAP_SM_PS_SHIFT;
306                 /*
307 --- a/net/mac80211/sta_info.h
308 +++ b/net/mac80211/sta_info.h
309 @@ -371,6 +371,7 @@ struct sta_info {
310         struct sk_buff_head ps_tx_buf[IEEE80211_NUM_ACS];
311         struct sk_buff_head tx_filtered[IEEE80211_NUM_ACS];
312         unsigned long driver_buffered_tids;
313 +       void *txq;
314  
315         /* Updated from RX path only, no locking requirements */
316         unsigned long rx_packets;
317 --- a/net/mac80211/trace.h
318 +++ b/net/mac80211/trace.h
319 @@ -2201,6 +2201,40 @@ TRACE_EVENT(drv_get_txpower,
320         )
321  );
322  
323 +DEFINE_EVENT(local_sdata_evt, drv_wake_vif_tx_queue,
324 +       TP_PROTO(struct ieee80211_local *local,
325 +                struct ieee80211_sub_if_data *sdata),
326 +       TP_ARGS(local, sdata)
327 +);
328 +
329 +TRACE_EVENT(drv_wake_sta_tx_queue,
330 +       TP_PROTO(struct ieee80211_local *local,
331 +                struct ieee80211_sub_if_data *sdata,
332 +                struct ieee80211_sta *sta,
333 +                u8 tid),
334 +
335 +       TP_ARGS(local, sdata, sta, tid),
336 +
337 +       TP_STRUCT__entry(
338 +               LOCAL_ENTRY
339 +               VIF_ENTRY
340 +               STA_ENTRY
341 +               __field(u8, tid)
342 +       ),
343 +
344 +       TP_fast_assign(
345 +               LOCAL_ASSIGN;
346 +               VIF_ASSIGN;
347 +               STA_ASSIGN;
348 +               __entry->tid = tid;
349 +       ),
350 +
351 +       TP_printk(
352 +               LOCAL_PR_FMT  VIF_PR_FMT  STA_PR_FMT " tid: 0x%x",
353 +               LOCAL_PR_ARG, VIF_PR_ARG, STA_PR_ARG, __entry->tid
354 +       )
355 +);
356 +
357  
358  #ifdef CPTCFG_MAC80211_MESSAGE_TRACING
359  #undef TRACE_SYSTEM
360 --- a/net/mac80211/tx.c
361 +++ b/net/mac80211/tx.c
362 @@ -1198,13 +1198,75 @@ ieee80211_tx_prepare(struct ieee80211_su
363         return TX_CONTINUE;
364  }
365  
366 +static void ieee80211_drv_tx(struct ieee80211_local *local,
367 +                            struct ieee80211_vif *vif,
368 +                            struct ieee80211_sta *pubsta,
369 +                            struct sk_buff *skb)
370 +{
371 +       struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
372 +       struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
373 +       struct ieee80211_tx_control control = {
374 +               .sta = pubsta
375 +       };
376 +       struct ieee80211_txq *pubtxq = NULL;
377 +       struct txq_info *txq;
378 +       u8 ac;
379 +
380 +       if (ieee80211_is_mgmt(hdr->frame_control) ||
381 +           ieee80211_is_ctl(hdr->frame_control))
382 +               goto tx_normal;
383 +
384 +       if (pubsta) {
385 +               u8 tid = skb->priority & IEEE80211_QOS_CTL_TID_MASK;
386 +               pubtxq = pubsta->txq[tid];
387 +       } else {
388 +               pubtxq = vif->txq;
389 +       }
390 +
391 +       if (!pubtxq)
392 +               goto tx_normal;
393 +
394 +       ac = pubtxq->ac;
395 +       txq = container_of(pubtxq, struct txq_info, txq);
396 +       atomic_inc(&sdata->txq_len[ac]);
397 +       if (atomic_read(&sdata->txq_len[ac]) >= local->hw.txq_ac_max_pending)
398 +               netif_stop_subqueue(sdata->dev, ac);
399 +
400 +       skb_queue_tail(&txq->queue, skb);
401 +       drv_wake_tx_queue(local, txq);
402 +
403 +       return;
404 +
405 +tx_normal:
406 +       drv_tx(local, &control, skb);
407 +}
408 +
409 +int ieee80211_tx_dequeue(struct ieee80211_hw *hw, struct ieee80211_txq *pubtxq,
410 +                        struct sk_buff **dest)
411 +{
412 +       struct ieee80211_local *local = hw_to_local(hw);
413 +       struct ieee80211_sub_if_data *sdata = vif_to_sdata(pubtxq->vif);
414 +       struct txq_info *txq = container_of(pubtxq, struct txq_info, txq);
415 +       u8 ac = pubtxq->ac;
416 +
417 +       *dest = skb_dequeue(&txq->queue);
418 +       if (!*dest)
419 +               return -EAGAIN;
420 +
421 +       atomic_dec(&sdata->txq_len[ac]);
422 +       if (__netif_subqueue_stopped(sdata->dev, ac))
423 +               ieee80211_propagate_queue_wake(local, sdata->vif.hw_queue[ac]);
424 +
425 +       return 0;
426 +}
427 +EXPORT_SYMBOL(ieee80211_tx_dequeue);
428 +
429  static bool ieee80211_tx_frags(struct ieee80211_local *local,
430                                struct ieee80211_vif *vif,
431                                struct ieee80211_sta *sta,
432                                struct sk_buff_head *skbs,
433                                bool txpending)
434  {
435 -       struct ieee80211_tx_control control;
436         struct sk_buff *skb, *tmp;
437         unsigned long flags;
438  
439 @@ -1262,10 +1324,9 @@ static bool ieee80211_tx_frags(struct ie
440                 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
441  
442                 info->control.vif = vif;
443 -               control.sta = sta;
444  
445                 __skb_unlink(skb, skbs);
446 -               drv_tx(local, &control, skb);
447 +               ieee80211_drv_tx(local, vif, sta, skb);
448         }
449  
450         return true;
451 --- a/net/mac80211/util.c
452 +++ b/net/mac80211/util.c
453 @@ -308,6 +308,11 @@ void ieee80211_propagate_queue_wake(stru
454                 for (ac = 0; ac < n_acs; ac++) {
455                         int ac_queue = sdata->vif.hw_queue[ac];
456  
457 +                       if (local->ops->wake_tx_queue &&
458 +                           (atomic_read(&sdata->txq_len[ac]) >
459 +                            local->hw.txq_ac_max_pending))
460 +                               continue;
461 +
462                         if (ac_queue == queue ||
463                             (sdata->vif.cab_queue == queue &&
464                              local->queue_stop_reasons[ac_queue] == 0 &&
465 @@ -3182,3 +3187,33 @@ u8 *ieee80211_add_wmm_info_ie(u8 *buf, u
466  
467         return buf;
468  }
469 +
470 +void ieee80211_init_tx_queue(struct ieee80211_sub_if_data *sdata,
471 +                            struct sta_info *sta,
472 +                            struct txq_info *txq, int tid)
473 +{
474 +       skb_queue_head_init(&txq->queue);
475 +       txq->txq.vif = &sdata->vif;
476 +
477 +       if (sta) {
478 +               txq->txq.sta = &sta->sta;
479 +               sta->sta.txq[tid] = &txq->txq;
480 +               txq->txq.ac = ieee802_1d_to_ac[tid & 7];
481 +       } else {
482 +               sdata->vif.txq = &txq->txq;
483 +               txq->txq.ac = IEEE80211_AC_BE;
484 +       }
485 +}
486 +
487 +void ieee80211_flush_tx_queue(struct ieee80211_local *local,
488 +                             struct ieee80211_txq *pubtxq)
489 +{
490 +       struct txq_info *txq = container_of(pubtxq, struct txq_info, txq);
491 +       struct ieee80211_sub_if_data *sdata = vif_to_sdata(pubtxq->vif);
492 +       struct sk_buff *skb;
493 +
494 +       while ((skb = skb_dequeue(&txq->queue)) != NULL) {
495 +               atomic_dec(&sdata->txq_len[pubtxq->ac]);
496 +               ieee80211_free_txskb(&local->hw, skb);
497 +       }
498 +}