Skip to contentSkip to conversation

mac80211: add an intermediate software queue implementation

PATCH Superseded Felix Fietkau 2 participants 4 messages Mar 17, 2015 mac80211

v3 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 mac80211: add an intermediate software queue implementation

This allows drivers to request per-vif and per-sta-tid queues from which they can pull frames. This makes it easier to keep the hardware queues short, and to improve fairness between clients and vifs.

The task of scheduling packet transmission is left up to the driver - queueing is controlled by mac80211. Drivers can only dequeue packets by calling ieee80211_tx_dequeue. This makes it possible to add active queue management later without changing drivers using this code.

This can also be used as a starting point to implement A-MSDU aggregation in a way that does not add artificially induced latency.

---
 include/net/mac80211.h     | 77 ++++++++++++++++++++++++++++++++++++++++++
 net/mac80211/driver-ops.h  | 17 ++++++++++
 net/mac80211/ieee80211_i.h | 14 ++++++++
 net/mac80211/iface.c       | 26 +++++++++++++++
 net/mac80211/main.c        |  3 ++
 net/mac80211/rx.c          | 15 +++++++++
 net/mac80211/sta_info.c    | 83 +++++++++++++++++++++++++++++++++++++++++-----
 net/mac80211/sta_info.h    |  3 ++
 net/mac80211/trace.h       | 34 +++++++++++++++++++
 net/mac80211/tx.c          | 72 ++++++++++++++++++++++++++++++++++++++--
 net/mac80211/util.c        | 38 +++++++++++++++++++++
 11 files changed, 370 insertions(+), 12 deletions(-)
include/net/mac80211.h +77 -0
index a1db2ea..724e133 100644
@@ -84,6 +84,33 @@
  *
  */
 
+/**
+ * DOC: mac80211 software tx queueing
+ *
+ * mac80211 provides an optional intermediate queueing implementation designed
+ * to allow the driver to keep hardware queues short and provide some fairness
+ * between different stations/interfaces.
+ * In this model, the driver pulls data frames from the mac80211 queue instead
+ * of letting mac80211 push them via drv_tx(). Management frames are still sent
+ * via drv_tx().
+ *
+ * Intermediate queues (struct ieee80211_txq) are kept per-sta per-tid, with a
+ * single per-vif queue for multicast data frames.
+ *
+ * The driver is expected to initialize its private per-queue data for stations
+ * and interfaces in the .add_interface and .sta_add ops.
+ *
+ * The driver can not access the queue directly. To dequeue a frame, it calls
+ * ieee80211_tx_dequeue(). Whenever mac80211 adds a new frame to a queue, it
+ * calls the .wake_tx_queue driver op.
+ *
+ * For AP powersave TIM handling, the driver only needs to indicate if it has
+ * buffered packets in the driver specific data structures. For frames buffered
+ * in the ieee80211_txq struct, mac80211 sets TIM and the driver is expected
+ * to dequeue and transmit packets on demand via the .release_buffered_frames
+ * op.
+ */
+
 struct device;
 
 /**
@@ -1257,6 +1284,8 @@ struct ieee80211_vif {
 	u8 cab_queue;
 	u8 hw_queue[IEEE80211_NUM_ACS];
 
+	struct ieee80211_txq *txq;
+
 	struct ieee80211_chanctx_conf __rcu *chanctx_conf;
 
 	u32 driver_flags;
@@ -1519,6 +1548,8 @@ struct ieee80211_sta {
 	bool tdls_initiator;
 	bool mfp;
 
+	struct ieee80211_txq *txq[IEEE80211_NUM_TIDS];
+
 	/* must be last */
 	u8 drv_priv[0] __aligned(sizeof(void *));
 };
@@ -1547,6 +1578,27 @@ struct ieee80211_tx_control {
 };
 
 /**
+ * struct ieee80211_txq - Software intermediate tx queue
+ *
+ * @vif: &struct ieee80211_vif pointer from the add_interface callback.
+ * @sta: station table entry, may be NULL for per-vif queue
+ * @tid: the TID for this queue (unset for per-vif queue)
+ * @ac: the AC for this queue
+ *
+ * The driver can obtain packets from this queue by calling
+ * ieee80211_tx_dequeue().
+ */
+struct ieee80211_txq {
+	struct ieee80211_vif *vif;
+	struct ieee80211_sta *sta;
+	u8 tid;
+	u8 ac;
+
+	/* must be last */
+	u8 drv_priv[0] __aligned(sizeof(void *));
+};
+
+/**
  * enum ieee80211_hw_flags - hardware flags
  *
  * These flags are used to indicate hardware capabilities to
@@ -1770,6 +1822,8 @@ enum ieee80211_hw_flags {
  *	within &struct ieee80211_sta.
  * @chanctx_data_size: size (in bytes) of the drv_priv data area
  *	within &struct ieee80211_chanctx_conf.
+ * @txq_data_size: size (in bytes) of the drv_priv data area
+ *	within @struct ieee80211_txq.
  *
  * @max_rates: maximum number of alternate rate retry stages the hw
  *	can handle.
@@ -1818,6 +1872,9 @@ enum ieee80211_hw_flags {
  * @n_cipher_schemes: a size of an array of cipher schemes definitions.
  * @cipher_schemes: a pointer to an array of cipher scheme definitions
  *	supported by HW.
+ *
+ * @txq_ac_max_pending: maximum number of frames per AC pending in all txq
+ *	entries for a vif.
  */
 struct ieee80211_hw {
 	struct ieee80211_conf conf;
@@ -1830,6 +1887,7 @@ struct ieee80211_hw {
 	int vif_data_size;
 	int sta_data_size;
 	int chanctx_data_size;
+	int txq_data_size;
 	u16 queues;
 	u16 max_listen_interval;
 	s8 max_signal;
@@ -1846,6 +1904,7 @@ struct ieee80211_hw {
 	u8 uapsd_max_sp_len;
 	u8 n_cipher_schemes;
 	const struct ieee80211_cipher_scheme *cipher_schemes;
+	int txq_ac_max_pending;
 };
 
 /**
@@ -3007,6 +3066,8 @@ enum ieee80211_reconfig_type {
  *	response template is provided, together with the location of the
  *	switch-timing IE within the template. The skb can only be used within
  *	the function call.
+ *
+ * @wake_tx_queue: Called when new packets have been added to the queue.
  */
 struct ieee80211_ops {
 	void (*tx)(struct ieee80211_hw *hw,
@@ -3238,6 +3299,9 @@ struct ieee80211_ops {
 	void (*tdls_recv_channel_switch)(struct ieee80211_hw *hw,
 					 struct ieee80211_vif *vif,
 					 struct ieee80211_tdls_ch_sw_params *params);
+
+	void (*wake_tx_queue)(struct ieee80211_hw *hw,
+			      struct ieee80211_txq *txq);
 };
 
 /**
@@ -5259,4 +5323,17 @@ void ieee80211_unreserve_tid(struct ieee80211_sta *sta, u8 tid);
  */
 size_t ieee80211_ie_split(const u8 *ies, size_t ielen,
 			  const u8 *ids, int n_ids, size_t offset);
+
+/**
+ * ieee80211_tx_dequeue - dequeue a packet from a software tx queue
+ *
+ * @hw: pointer as obtained from ieee80211_alloc_hw()
+ * @txq: pointer obtained from .add_tx_queue() call
+ *
+ * Returns the skb if successful, ERR_PTR(-EAGAIN) if no frame was available.
+ */
+struct sk_buff *ieee80211_tx_dequeue(struct ieee80211_hw *hw,
+				     struct ieee80211_txq *txq);
+
+
 #endif /* MAC80211_H */
net/mac80211/driver-ops.h +17 -0
index fdeda17..418f739 100644
@@ -1367,4 +1367,21 @@ drv_tdls_recv_channel_switch(struct ieee80211_local *local,
 	trace_drv_return_void(local);
 }
 
+static inline void drv_wake_tx_queue(struct ieee80211_local *local,
+				     struct txq_info *txq)
+{
+	struct ieee80211_sub_if_data *sdata = vif_to_sdata(txq->txq.vif);
+
+	if (!check_sdata_in_driver(sdata))
+		return;
+
+	if (txq->txq.sta)
+		trace_drv_wake_sta_tx_queue(local, sdata, txq->txq.sta,
+					    txq->txq.tid);
+	else
+		trace_drv_wake_vif_tx_queue(local, sdata);
+
+	local->ops->wake_tx_queue(&local->hw, &txq->txq);
+}
+
 #endif /* __MAC80211_DRIVER_OPS */
net/mac80211/ieee80211_i.h +14 -0
index 486a790..b4dc4f1 100644
@@ -809,6 +809,13 @@ struct mac80211_qos_map {
 	struct rcu_head rcu_head;
 };
 
+struct txq_info {
+	struct sk_buff_head queue;
+
+	/* keep last! */
+	struct ieee80211_txq txq;
+};
+
 struct ieee80211_sub_if_data {
 	struct list_head list;
 
@@ -853,6 +860,7 @@ struct ieee80211_sub_if_data {
 	bool control_port_no_encrypt;
 	int encrypt_headroom;
 
+	atomic_t txqs_len[IEEE80211_NUM_ACS];
 	struct ieee80211_tx_queue_params tx_conf[IEEE80211_NUM_ACS];
 	struct mac80211_qos_map __rcu *qos_map;
 
@@ -1902,6 +1910,12 @@ static inline bool ieee80211_can_run_worker(struct ieee80211_local *local)
 	return true;
 }
 
+void ieee80211_init_tx_queue(struct ieee80211_sub_if_data *sdata,
+			     struct sta_info *sta,
+			     struct txq_info *txq, int tid);
+void ieee80211_flush_tx_queue(struct ieee80211_local *local,
+			      struct ieee80211_txq *txq);
+
 void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
 			 u16 transaction, u16 auth_alg, u16 status,
 			 const u8 *extra, size_t extra_len, const u8 *bssid,
net/mac80211/iface.c +26 -0
index b49812f..d29e0f7 100644
@@ -969,6 +969,9 @@ static void ieee80211_do_stop(struct ieee80211_sub_if_data *sdata,
 	}
 	spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
 
+	if (sdata->vif.txq)
+		ieee80211_flush_tx_queue(local, sdata->vif.txq);
+
 	if (local->open_count == 0)
 		ieee80211_clear_tx_pending(local);
 
@@ -1753,6 +1756,15 @@ int ieee80211_if_add(struct ieee80211_local *local, const char *name,
 	ieee80211_setup_sdata(sdata, type);
 
 	if (ndev) {
+		struct txq_info *txqi = NULL;
+
+		if (local->ops->wake_tx_queue) {
+			txqi = kzalloc(sizeof(*txqi) +
+				      local->hw.txq_data_size, GFP_KERNEL);
+			if (txqi)
+				ieee80211_init_tx_queue(sdata, NULL, txqi, 0);
+		}
+
 		if (params) {
 			ndev->ieee80211_ptr->use_4addr = params->use_4addr;
 			if (type == NL80211_IFTYPE_STATION)
@@ -1765,6 +1777,7 @@ int ieee80211_if_add(struct ieee80211_local *local, const char *name,
 
 		ret = register_netdevice(ndev);
 		if (ret) {
+			kfree(txqi);
 			free_netdev(ndev);
 			return ret;
 		}
@@ -1782,6 +1795,7 @@ int ieee80211_if_add(struct ieee80211_local *local, const char *name,
 
 void ieee80211_if_remove(struct ieee80211_sub_if_data *sdata)
 {
+	struct txq_info *txqi;
 	ASSERT_RTNL();
 
 	mutex_lock(&sdata->local->iflist_mtx);
@@ -1790,6 +1804,11 @@ void ieee80211_if_remove(struct ieee80211_sub_if_data *sdata)
 
 	synchronize_rcu();
 
+	if (sdata->vif.txq) {
+		txqi = container_of(sdata->vif.txq, struct txq_info, txq);
+		kfree(txqi);
+	}
+
 	if (sdata->dev) {
 		unregister_netdevice(sdata->dev);
 	} else {
@@ -1813,6 +1832,7 @@ void ieee80211_sdata_stop(struct ieee80211_sub_if_data *sdata)
 void ieee80211_remove_interfaces(struct ieee80211_local *local)
 {
 	struct ieee80211_sub_if_data *sdata, *tmp;
+	struct txq_info *txqi;
 	LIST_HEAD(unreg_list);
 	LIST_HEAD(wdev_list);
 
@@ -1831,6 +1851,12 @@ void ieee80211_remove_interfaces(struct ieee80211_local *local)
 	list_for_each_entry_safe(sdata, tmp, &local->interfaces, list) {
 		list_del(&sdata->list);
 
+		if (sdata->vif.txq) {
+			txqi = container_of(sdata->vif.txq, struct txq_info,
+					   txq);
+			kfree(txqi);
+		}
+
 		if (sdata->dev)
 			unregister_netdevice_queue(sdata->dev, &unreg_list);
 		else
net/mac80211/main.c +3 -0
index 2f51e6d..df94567 100644
@@ -1035,6 +1035,9 @@ int ieee80211_register_hw(struct ieee80211_hw *hw)
 
 	local->dynamic_ps_forced_timeout = -1;
 
+	if (!local->hw.txq_ac_max_pending)
+		local->hw.txq_ac_max_pending = 64;
+
 	result = ieee80211_wep_init(local);
 	if (result < 0)
 		wiphy_debug(local->hw.wiphy, "Failed to initialize wep: %d\n",
net/mac80211/rx.c +15 -0
index 944bdc0..dfba093 100644
@@ -1184,6 +1184,7 @@ static void sta_ps_start(struct sta_info *sta)
 	struct ieee80211_sub_if_data *sdata = sta->sdata;
 	struct ieee80211_local *local = sdata->local;
 	struct ps_data *ps;
+	int tid;
 
 	if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
 	    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
@@ -1197,6 +1198,20 @@ static void sta_ps_start(struct sta_info *sta)
 		drv_sta_notify(local, sdata, STA_NOTIFY_SLEEP, &sta->sta);
 	ps_dbg(sdata, "STA %pM aid %d enters power save mode\n",
 	       sta->sta.addr, sta->sta.aid);
+
+	if (!sta->txqi)
+		return;
+
+	for (tid = 0; tid < IEEE80211_NUM_TIDS; tid++) {
+		struct txq_info *txqi;
+
+		txqi = container_of(sta->sta.txq[tid], struct txq_info, txq);
+
+		if (!skb_queue_len(&txqi->queue))
+			set_bit(tid, &sta->txq_buffered_tids);
+		else
+			clear_bit(tid, &sta->txq_buffered_tids);
+	}
 }
 
 static void sta_ps_end(struct sta_info *sta)
net/mac80211/sta_info.c +74 -9

173 lines not on this page · raw message

net/mac80211/sta_info.h +3 -0
index 248f56e..3c12997 100644
@@ -274,6 +274,7 @@ struct sta_ampdu_mlme {
  *	entered power saving state, these are also delivered to
  *	the station when it leaves powersave or polls for frames
  * @driver_buffered_tids: bitmap of TIDs the driver has data buffered on
+ * @txq_buffered_tids: bitmap of TIDs that mac80211 has txq data buffered on
  * @rx_packets: Number of MSDUs received from this STA
  * @rx_bytes: Number of bytes received from this STA
  * @last_rx: time (in jiffies) when last frame was received from this STA
@@ -368,6 +369,8 @@ struct sta_info {
 	struct sk_buff_head ps_tx_buf[IEEE80211_NUM_ACS];
 	struct sk_buff_head tx_filtered[IEEE80211_NUM_ACS];
 	unsigned long driver_buffered_tids;
+	unsigned long txq_buffered_tids;
+	struct txq_info *txqi;
 
 	/* Updated from RX path only, no locking requirements */
 	unsigned long rx_packets;
net/mac80211/trace.h +34 -0
index 263a956..5eb5553 100644
@@ -2312,6 +2312,40 @@ TRACE_EVENT(drv_tdls_recv_channel_switch,
 	)
 );
 
+DEFINE_EVENT(local_sdata_evt, drv_wake_vif_tx_queue,
+	TP_PROTO(struct ieee80211_local *local,
+		 struct ieee80211_sub_if_data *sdata),
+	TP_ARGS(local, sdata)
+);
+
+TRACE_EVENT(drv_wake_sta_tx_queue,
+	TP_PROTO(struct ieee80211_local *local,
+		 struct ieee80211_sub_if_data *sdata,
+		 struct ieee80211_sta *sta,
+		 u8 tid),
+
+	TP_ARGS(local, sdata, sta, tid),
+
+	TP_STRUCT__entry(
+		LOCAL_ENTRY
+		VIF_ENTRY
+		STA_ENTRY
+		__field(u8, tid)
+	),
+
+	TP_fast_assign(
+		LOCAL_ASSIGN;
+		VIF_ASSIGN;
+		STA_ASSIGN;
+		__entry->tid = tid;
+	),
+
+	TP_printk(
+		LOCAL_PR_FMT  VIF_PR_FMT  STA_PR_FMT " tid: 0x%x",
+		LOCAL_PR_ARG, VIF_PR_ARG, STA_PR_ARG, __entry->tid
+	)
+);
+
 #ifdef CONFIG_MAC80211_MESSAGE_TRACING
 #undef TRACE_SYSTEM
 #define TRACE_SYSTEM mac80211_msg
net/mac80211/tx.c +69 -3

92 lines not on this page · raw message

net/mac80211/util.c +38 -0
index 37d85d3..45cb02e 100644
@@ -308,6 +308,11 @@ void ieee80211_propagate_queue_wake(struct ieee80211_local *local, int queue)
 		for (ac = 0; ac < n_acs; ac++) {
 			int ac_queue = sdata->vif.hw_queue[ac];
 
+			if (local->ops->wake_tx_queue &&
+			    (atomic_read(&sdata->txqs_len[ac]) >
+			     local->hw.txq_ac_max_pending))
+				continue;
+
 			if (ac_queue == queue ||
 			    (sdata->vif.cab_queue == queue &&
 			     local->queue_stop_reasons[ac_queue] == 0 &&
@@ -3307,3 +3312,36 @@ u8 *ieee80211_add_wmm_info_ie(u8 *buf, u8 qosinfo)
 
 	return buf;
 }
+
+void ieee80211_init_tx_queue(struct ieee80211_sub_if_data *sdata,
+			     struct sta_info *sta,
+			     struct txq_info *txqi, int tid)
+{
+	skb_queue_head_init(&txqi->queue);
+	txqi->txq.vif = &sdata->vif;
+
+	if (sta) {
+		txqi->txq.sta = &sta->sta;
+		sta->sta.txq[tid] = &txqi->txq;
+		txqi->txq.ac = ieee802_1d_to_ac[tid & 7];
+	} else {
+		sdata->vif.txq = &txqi->txq;
+		txqi->txq.ac = IEEE80211_AC_BE;
+	}
+}
+
+void ieee80211_flush_tx_queue(struct ieee80211_local *local,
+			      struct ieee80211_txq *txq)
+{
+	struct txq_info *txqi = container_of(txq, struct txq_info, txq);
+	struct ieee80211_sub_if_data *sdata = vif_to_sdata(txq->vif);
+	struct sk_buff *skb;
+	int n = 0;
+
+	while ((skb = skb_dequeue(&txqi->queue)) != NULL) {
+		n++;
+		ieee80211_free_txskb(&local->hw, skb);
+	}
+
+	atomic_sub(n, &sdata->txqs_len[txq->ac]);
+}
Signature · 1 line
2.2.2
maintainer
#1 lore raw
Quoted 9 lines · Felix Fietkau

+/**
+ * DOC: mac80211 software tx queueing
+ *
+ * mac80211 provides an optional intermediate queueing implementation designed
+ * to allow the driver to keep hardware queues short and provide some fairness
+ * between different stations/interfaces.
+ * In this model, the driver pulls data frames from the mac80211 queue instead
+ * of letting mac80211 push them via drv_tx(). Management frames are still sent
+ * via drv_tx().

IIRC there were some other frames - perhaps it should read "Some frames (e.g. management) are still sent via drv_tx()."?

Also, I wonder if this is really correct, since bufferable management frames are probably *not* desired to go through that?

Quoted 2 lines

+ * For AP powersave TIM handling, the driver only needs to indicate if it has
+ * buffered packets in the driver specific data structures.

Maybe you should say how to indicate it.

Quoted 5 lines

For frames buffered
+ * in the ieee80211_txq struct, mac80211 sets TIM and the driver is expected
+ * to dequeue and transmit packets on demand via the .release_buffered_frames
+ * op.
+ */

"via" reads odd here to me, perhaps say "to dequeue and transmit packets on demand as requested by the .release_buffered_frames op"?

include/net/mac80211.h struct ieee80211_vif { · line 1287

Quoted 5 lines
include/net/mac80211.h +1 -0
@@ -1257,6 +1284,8 @@ struct ieee80211_vif {
 	u8 cab_queue;
 	u8 hw_queue[IEEE80211_NUM_ACS];
 
+	struct ieee80211_txq *txq;

This is just one txq, the mcast one? Perhaps that should be cab_txq then?

Or is it multiple, then perhaps it should be "txqs"?

include/net/mac80211.h struct ieee80211_sta { · line 1551

Quoted 1 line
include/net/mac80211.h +1 -0
+	struct ieee80211_txq *txq[IEEE80211_NUM_TIDS];

I wonder if there's a way to make this a single pointer here only? But I guess with variable-sized driver data this would be really difficult.

include/net/mac80211.h struct ieee80211_tx_control { · line 1585

Quoted 6 lines
include/net/mac80211.h +5 -0
 /**
+ * struct ieee80211_txq - Software intermediate tx queue
+ *
+ * @vif: &struct ieee80211_vif pointer from the add_interface callback.
+ * @sta: station table entry, may be NULL for per-vif queue
+ * @tid: the TID for this queue (unset for per-vif queue)

"unset" is probably not really true (and it's 0) - "unused" might be better?

include/net/mac80211.h enum ieee80211_hw_flags { · line 1877

Quoted 7 lines
include/net/mac80211.h +3 -0
@@ -1818,6 +1872,9 @@ enum ieee80211_hw_flags {
  * @n_cipher_schemes: a size of an array of cipher schemes definitions.
  * @cipher_schemes: a pointer to an array of cipher scheme definitions
  *	supported by HW.
+ *
+ * @txq_ac_max_pending: maximum number of frames per AC pending in all txq
+ *	entries for a vif.

I think you should give some guidance on how to best set this value, like max aggregation size or something? I'm not really sure :)

net/mac80211/driver-ops.h drv_tdls_recv_channel_switch(struct ieee80211_local *local, · line 1382

Quoted 13 lines
net/mac80211/driver-ops.h +13 -0
+static inline void drv_wake_tx_queue(struct ieee80211_local *local,
+				     struct txq_info *txq)
+{
+	struct ieee80211_sub_if_data *sdata = vif_to_sdata(txq->txq.vif);
+
+	if (!check_sdata_in_driver(sdata))
+		return;
+
+	if (txq->txq.sta)
+		trace_drv_wake_sta_tx_queue(local, sdata, txq->txq.sta,
+					    txq->txq.tid);
+	else
+		trace_drv_wake_vif_tx_queue(local, sdata);

Having separate tracepoints seems a bit strange - can't we unify them and have any necessary if inside the fast_assign() part of the macro? That way we stub out everything if tracing isn't enabled.

net/mac80211/iface.c int ieee80211_if_add(struct ieee80211_local *local, const char *name, · line 1765

Quoted 8 lines
net/mac80211/iface.c +7 -0
 	if (ndev) {
+		struct txq_info *txqi = NULL;
+
+		if (local->ops->wake_tx_queue) {
+			txqi = kzalloc(sizeof(*txqi) +
+				      local->hw.txq_data_size, GFP_KERNEL);
+			if (txqi)
+				ieee80211_init_tx_queue(sdata, NULL, txqi, 0);

Might be worth wrapping that into a single alloc_and_init() function? Or really just alloc() since the init could be implied?

net/mac80211/iface.c void ieee80211_if_remove(struct ieee80211_sub_if_data *sdata) · line 1808

Quoted 12 lines

void ieee80211_if_remove(struct ieee80211_sub_if_data *sdata) {

net/mac80211/iface.c +3 -0
+	struct txq_info *txqi;
 	ASSERT_RTNL();
 
 	mutex_lock(&sdata->local->iflist_mtx);
@@ -1790,6 +1804,11 @@ void ieee80211_if_remove(struct ieee80211_sub_if_data *sdata)
 
 	synchronize_rcu();
 
+	if (sdata->vif.txq) {
+		txqi = container_of(sdata->vif.txq, struct txq_info, txq);

you could move the variable here

net/mac80211/iface.c void ieee80211_remove_interfaces(struct ieee80211_local *local) · line 1856

Quoted 7 lines
net/mac80211/iface.c +3 -0
@@ -1831,6 +1851,12 @@ void ieee80211_remove_interfaces(struct ieee80211_local *local)
 	list_for_each_entry_safe(sdata, tmp, &local->interfaces, list) {
 		list_del(&sdata->list);
 
+		if (sdata->vif.txq) {
+			txqi = container_of(sdata->vif.txq, struct txq_info,
+					   txq);

similar here

net/mac80211/rx.c static void sta_ps_start(struct sta_info *sta) · line 1208

Quoted 12 lines
net/mac80211/rx.c +8 -0
@@ -1197,6 +1198,20 @@ static void sta_ps_start(struct sta_info *sta)
 		drv_sta_notify(local, sdata, STA_NOTIFY_SLEEP, &sta->sta);
 	ps_dbg(sdata, "STA %pM aid %d enters power save mode\n",
 	       sta->sta.addr, sta->sta.aid);
+
+	if (!sta->txqi)
+		return;
+
+	for (tid = 0; tid < IEEE80211_NUM_TIDS; tid++) {
+		struct txq_info *txqi;
+
+		txqi = container_of(sta->sta.txq[tid], struct txq_info, txq);

Perhaps you should also have an inline for all the container_of() since you do that a lot.

Quoted 8 lines
net/mac80211/sta_info.c +5 -3
-	if (sta_prepare_rate_control(local, sta, gfp)) {
-		kfree(sta);
-		return NULL;
+	if (local->ops->wake_tx_queue) {
+		void *txq_data;
+		int size = sizeof(struct txq_info) +
+			   ALIGN(hw->txq_data_size, sizeof(void *));
+

net/mac80211/sta_info.c struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata, · line 345

Quoted 10 lines
net/mac80211/sta_info.c +9 -0
+		txq_data = kcalloc(IEEE80211_NUM_TIDS, size, gfp);
+		if (!txq_data)
+			goto free;
+
+		sta->txqi = txq_data;
+		for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
+			struct txq_info *txq = txq_data + i * size;
+			ieee80211_init_tx_queue(sdata, sta, txq, i);
+		}
 	}

Oh, so you do have a combined allocation scheme? I guess I need to look at this in more detail.

net/mac80211/sta_info.c struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata, · line 348

Quoted 2 lines
net/mac80211/sta_info.c +2 -0
+	if (sta_prepare_rate_control(local, sta, gfp))
+		goto free_txq;

Does it even have to come before rate control?

Quoted 12 lines
net/mac80211/sta_info.c +4 -0
@@ -1090,10 +1119,25 @@ void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
 
 	BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
 	sta->driver_buffered_tids = 0;
+	sta->txq_buffered_tids = 0;
 
 	if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
 		drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
 
+	if (sta->txqi) {
+		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
+			struct txq_info *txqi;

net/mac80211/sta_info.c void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta) · line 1139

Quoted 10 lines
net/mac80211/sta_info.c +10 -0
+
+			txqi = container_of(sta->sta.txq[i], struct txq_info,
+					    txq);
+
+			if (!skb_queue_len(&txqi->queue))
+				continue;
+
+			drv_wake_tx_queue(local, txqi);
+		}
+	}

This could be an interesting race. If you wake the queue, and then the station goes to sleep again before the driver had a chance to pull frames, what happens? Should the driver be responsible for this? But you don't have "unwake_tx_queue", so maybe you should not return any frame from the dequeue in such a case?

net/mac80211/sta_info.c ieee80211_sta_ps_deliver_response(struct sta_info *sta, · line 1496

Quoted 5 lines
net/mac80211/sta_info.c +1 -0
@@ -1447,6 +1493,8 @@ ieee80211_sta_ps_deliver_response(struct sta_info *sta,
 
 		sta_info_recalc_tim(sta);
 	} else {
+		unsigned long tids = sta->txq_buffered_tids & driver_release_tids;

I'm not sure I understand this. Are you treating txq_buffered_tids as driver-buffered?

Quoted 17 lines
net/mac80211/sta_info.c +8 -1
 		/*
 		 * We need to release a frame that is buffered somewhere in the
 		 * driver ... it'll have to handle that.
@@ -1466,8 +1514,25 @@ ieee80211_sta_ps_deliver_response(struct sta_info *sta,
 		 * that the TID(s) became empty before returning here from the
 		 * release function.
 		 * Either way, however, when the driver tells us that the TID(s)
-		 * became empty we'll do the TIM recalculation.
+		 * became empty or we find that a txq became empty, we'll do the
+		 * TIM recalculation.
 		 */
+
+		if (!sta->txqi)
+			return;
+
+		for (tid = 0; tid < IEEE80211_NUM_TIDS; tid++) {
+			struct txq_info *txqi;

net/mac80211/sta_info.c ieee80211_sta_ps_deliver_response(struct sta_info *sta, · line 1535

Quoted 10 lines
net/mac80211/sta_info.c +10 -0
+
+			txqi = container_of(sta->sta.txq[tid], struct txq_info,
+					    txq);
+
+			if (!(tids & BIT(tid)) || skb_queue_len(&txqi->queue))
+				continue;
+
+			sta_info_recalc_tim(sta);
+			break;
+		}

[left as context in case you need it to explain]

net/mac80211/sta_info.h struct sta_info { · line 373

Quoted 6 lines
net/mac80211/sta_info.h +2 -0
@@ -368,6 +369,8 @@ struct sta_info {
 	struct sk_buff_head ps_tx_buf[IEEE80211_NUM_ACS];
 	struct sk_buff_head tx_filtered[IEEE80211_NUM_ACS];
 	unsigned long driver_buffered_tids;
+	unsigned long txq_buffered_tids;
+	struct txq_info *txqi;

Hm, so, internally you allocate one big thing and externally to the driver you have a per-TID array.

Why not just remove this pointer, and keep only the per-TID array? You'd have to do a bit more container_of() but I don't think that's a big deal? Plus you can't really use this anyway since you can't index it, i.e. you cannot say sta->txqi[t] since the size doesn't match up.

Quoted 11 lines
net/mac80211/tx.c +11 -0
+static void ieee80211_drv_tx(struct ieee80211_local *local,
+			     struct ieee80211_vif *vif,
+			     struct ieee80211_sta *pubsta,
+			     struct sk_buff *skb)
+{
+	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
+	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
+	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
+	struct ieee80211_tx_control control = {
+		.sta = pubsta
+	};

net/mac80211/tx.c ieee80211_tx_prepare(struct ieee80211_sub_if_data *sdata, · line 1242

Quoted 10 lines
net/mac80211/tx.c +10 -0
+	struct ieee80211_txq *txq = NULL;
+	struct txq_info *txqi;
+	u8 ac;
+
+	if (info->control.flags & IEEE80211_TX_CTRL_PS_RESPONSE)
+		goto tx_normal;
+
+	if (ieee80211_is_mgmt(hdr->frame_control) ||
+	    ieee80211_is_ctl(hdr->frame_control))
+		goto tx_normal;

Doesn't this become awkward with powersave handling for bufferable mgmt frames? They'd be stored on the per-station non-txq queues, but then the wakeup handling needs to see which ones to take first? Having these on the txqs might make that part easier?

OTOH, I guess it would make building A-MPDUs or even A-MSDUs far more complicated, so I guess it's a reasonable tradeoff.

net/mac80211/tx.c ieee80211_tx_prepare(struct ieee80211_sub_if_data *sdata, · line 1280

Quoted 12 lines
net/mac80211/tx.c +12 -0
+struct sk_buff *ieee80211_tx_dequeue(struct ieee80211_hw *hw,
+				     struct ieee80211_txq *txq)
+{
+	struct ieee80211_local *local = hw_to_local(hw);
+	struct ieee80211_sub_if_data *sdata = vif_to_sdata(txq->vif);
+	struct txq_info *txqi = container_of(txq, struct txq_info, txq);
+	struct sk_buff *skb;
+	u8 ac = txq->ac;
+
+	skb = skb_dequeue(&txqi->queue);
+	if (!skb)
+		return ERR_PTR(-EAGAIN);

why not just return NULL?

net/mac80211/tx.c ieee80211_tx_prepare(struct ieee80211_sub_if_data *sdata, · line 1284

Quoted 3 lines
net/mac80211/tx.c +3 -0
+	atomic_dec(&sdata->txqs_len[ac]);
+	if (__netif_subqueue_stopped(sdata->dev, ac))
+		ieee80211_propagate_queue_wake(local, sdata->vif.hw_queue[ac]);

Do you really want to do that unconditionally? There could be a lot of frames on the queue still, or even on other station queues?

net/mac80211/util.c u8 *ieee80211_add_wmm_info_ie(u8 *buf, u8 qosinfo) · line 3326

Quoted 4 lines
net/mac80211/util.c +4 -0
+	if (sta) {
+		txqi->txq.sta = &sta->sta;
+		sta->sta.txq[tid] = &txqi->txq;
+		txqi->txq.ac = ieee802_1d_to_ac[tid & 7];

I think you should probably restrict this to 8 TIDs anyway... nobody uses 16 TIDs, and the mapping for the higher 8 TIDs is dynamic so this is always wrong. Using just 8 instead of 16 will also save a lot of memory I guess.

If you want TSPECs, then you probably want the WMM ones, which also only use the lower 8 TIDs. Only if you really wanted the 802.11 QoS TSPEC you might need the higher 8 TIDs ...

Anyway - overall I think this looks pretty good. What we discussed in Ottawa was that we should perhaps forego the whole qdisc and netdev queue start/stop and just do something like the qdisc would do in the layer of these queues, although then we'd have to first convert every driver or make this layer mandatory in some other way. That's something we should explore here I think.

johannes

#2 lore raw
Quoted 9 lines · Johannes Berg

include/net/mac80211.h struct ieee80211_vif { · line 1287

Quoted 5 lines · Felix Fietkau
include/net/mac80211.h +1 -0
@@ -1257,6 +1284,8 @@ struct ieee80211_vif {
 	u8 cab_queue;
 	u8 hw_queue[IEEE80211_NUM_ACS];
 
+	struct ieee80211_txq *txq;

This is just one txq, the mcast one? Perhaps that should be cab_txq then?

That would be misleading, since this queue is only used when CAB isn't (i.e. no sta in PS).

Quoted 6 lines

Or is it multiple, then perhaps it should be "txqs"?

include/net/mac80211.h struct ieee80211_sta { · line 1551

Quoted 1 line
include/net/mac80211.h +1 -0
+	struct ieee80211_txq *txq[IEEE80211_NUM_TIDS];

I wonder if there's a way to make this a single pointer here only? But I guess with variable-sized driver data this would be really difficult.

Maybe a potential optimization for later - I'd like to keep it simple for now...

Quoted 10 lines

include/net/mac80211.h enum ieee80211_hw_flags { · line 1877

Quoted 7 lines
include/net/mac80211.h +3 -0
@@ -1818,6 +1872,9 @@ enum ieee80211_hw_flags {
  * @n_cipher_schemes: a size of an array of cipher schemes definitions.
  * @cipher_schemes: a pointer to an array of cipher scheme definitions
  *	supported by HW.
+ *
+ * @txq_ac_max_pending: maximum number of frames per AC pending in all txq
+ *	entries for a vif.

I think you should give some guidance on how to best set this value, like max aggregation size or something? I'm not really sure :)

I don't have any guidance for tuning this in the driver just yet. For devices with software aggregation, it should just be left at the default value.

Quoted 4 lines

net/mac80211/sta_info.c struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata, · line 348

Quoted 2 lines
net/mac80211/sta_info.c +2 -0
+	if (sta_prepare_rate_control(local, sta, gfp))
+		goto free_txq;

Does it even have to come before rate control?

It doesn't have to, but I figured cleanup is simpler that way.

Quoted 20 lines
Quoted 20 lines
net/mac80211/sta_info.c +12 -0
@@ -1090,10 +1119,25 @@ void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
 
 	BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
 	sta->driver_buffered_tids = 0;
+	sta->txq_buffered_tids = 0;
 
 	if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
 		drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
 
+	if (sta->txqi) {
+		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
+			struct txq_info *txqi;
+
+			txqi = container_of(sta->sta.txq[i], struct txq_info,
+					    txq);
+
+			if (!skb_queue_len(&txqi->queue))
+				continue;
+
+			drv_wake_tx_queue(local, txqi);
Quoted 8 lines

Review void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta) · line 1139

Quoted 2 lines
(quoted diff) +2 -0
+		}
+	}

This could be an interesting race. If you wake the queue, and then the station goes to sleep again before the driver had a chance to pull frames, what happens? Should the driver be responsible for this? But you don't have "unwake_tx_queue", so maybe you should not return any frame from the dequeue in such a case?

The driver is responsible for making sure that any queue of a sleeping sta is not scheduled.

Quoted 8 lines

net/mac80211/sta_info.c ieee80211_sta_ps_deliver_response(struct sta_info *sta, · line 1496

Quoted 5 lines
net/mac80211/sta_info.c +1 -0
@@ -1447,6 +1493,8 @@ ieee80211_sta_ps_deliver_response(struct sta_info *sta,
 
 		sta_info_recalc_tim(sta);
 	} else {
+		unsigned long tids = sta->txq_buffered_tids & driver_release_tids;

I'm not sure I understand this. Are you treating txq_buffered_tids as driver-buffered?

Yes. As explained in the DOC section, PS delivery of txq buffered frames goes through drv_release_buffered_frames. Maybe I could change the wording a bit to make it more clear.

Quoted 10 lines

net/mac80211/sta_info.h struct sta_info { · line 373

Quoted 6 lines
net/mac80211/sta_info.h +2 -0
@@ -368,6 +369,8 @@ struct sta_info {
 	struct sk_buff_head ps_tx_buf[IEEE80211_NUM_ACS];
 	struct sk_buff_head tx_filtered[IEEE80211_NUM_ACS];
 	unsigned long driver_buffered_tids;
+	unsigned long txq_buffered_tids;
+	struct txq_info *txqi;

Hm, so, internally you allocate one big thing and externally to the driver you have a per-TID array.

Quoted 4 lines

Why not just remove this pointer, and keep only the per-TID array? You'd have to do a bit more container_of() but I don't think that's a big deal? Plus you can't really use this anyway since you can't index it, i.e. you cannot say sta->txqi[t] since the size doesn't match up.

Will do

Quoted 18 lines
Quoted 18 lines
net/mac80211/tx.c +18 -0
+static void ieee80211_drv_tx(struct ieee80211_local *local,
+			     struct ieee80211_vif *vif,
+			     struct ieee80211_sta *pubsta,
+			     struct sk_buff *skb)
+{
+	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
+	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
+	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
+	struct ieee80211_tx_control control = {
+		.sta = pubsta
+	};
+	struct ieee80211_txq *txq = NULL;
+	struct txq_info *txqi;
+	u8 ac;
+
+	if (info->control.flags & IEEE80211_TX_CTRL_PS_RESPONSE)
+		goto tx_normal;
+
Quoted 8 lines

net/mac80211/tx.c ieee80211_tx_prepare(struct ieee80211_sub_if_data *sdata, · line 1242

Quoted 3 lines
net/mac80211/tx.c +3 -0
+	if (ieee80211_is_mgmt(hdr->frame_control) ||
+	    ieee80211_is_ctl(hdr->frame_control))
+		goto tx_normal;

Doesn't this become awkward with powersave handling for bufferable mgmt frames? They'd be stored on the per-station non-txq queues, but then the wakeup handling needs to see which ones to take first? Having these on the txqs might make that part easier?

I guess I'll change it to throw bufferable mgmt frames in the txq as well.

Quoted 17 lines

OTOH, I guess it would make building A-MPDUs or even A-MSDUs far more complicated, so I guess it's a reasonable tradeoff.

net/mac80211/tx.c ieee80211_tx_prepare(struct ieee80211_sub_if_data *sdata, · line 1280

Quoted 12 lines
net/mac80211/tx.c +12 -0
+struct sk_buff *ieee80211_tx_dequeue(struct ieee80211_hw *hw,
+				     struct ieee80211_txq *txq)
+{
+	struct ieee80211_local *local = hw_to_local(hw);
+	struct ieee80211_sub_if_data *sdata = vif_to_sdata(txq->vif);
+	struct txq_info *txqi = container_of(txq, struct txq_info, txq);
+	struct sk_buff *skb;
+	u8 ac = txq->ac;
+
+	skb = skb_dequeue(&txqi->queue);
+	if (!skb)
+		return ERR_PTR(-EAGAIN);

why not just return NULL?

I guess initially I wanted to be able to return other error codes as well, but now I'm not sure I need that anymore. I'll change it to NULL.

Quoted 6 lines

net/mac80211/tx.c ieee80211_tx_prepare(struct ieee80211_sub_if_data *sdata, · line 1284

Quoted 3 lines
net/mac80211/tx.c +3 -0
+	atomic_dec(&sdata->txqs_len[ac]);
+	if (__netif_subqueue_stopped(sdata->dev, ac))
+		ieee80211_propagate_queue_wake(local, sdata->vif.hw_queue[ac]);

Do you really want to do that unconditionally? There could be a lot of frames on the queue still, or even on other station queues?

Unconditionally? ieee80211_propagate_queue_wake checks the per-sdata txq limit.

Quoted 10 lines

net/mac80211/util.c u8 *ieee80211_add_wmm_info_ie(u8 *buf, u8 qosinfo) · line 3326

Quoted 4 lines
net/mac80211/util.c +4 -0
+	if (sta) {
+		txqi->txq.sta = &sta->sta;
+		sta->sta.txq[tid] = &txqi->txq;
+		txqi->txq.ac = ieee802_1d_to_ac[tid & 7];

I think you should probably restrict this to 8 TIDs anyway... nobody uses 16 TIDs, and the mapping for the higher 8 TIDs is dynamic so this is always wrong. Using just 8 instead of 16 will also save a lot of memory I guess.

Quoted 3 lines

If you want TSPECs, then you probably want the WMM ones, which also only use the lower 8 TIDs. Only if you really wanted the 802.11 QoS TSPEC you might need the higher 8 TIDs ...

Right, limiting it to 8 makes sense.

Quoted 6 lines

Anyway - overall I think this looks pretty good. What we discussed in Ottawa was that we should perhaps forego the whole qdisc and netdev queue start/stop and just do something like the qdisc would do in the layer of these queues, although then we'd have to first convert every driver or make this layer mandatory in some other way. That's something we should explore here I think.

I agree. Figuring out what tx queue scheduling should look like for devices with firmware based aggregation is going to be interesting though...

- Felix

maintainer
#3 lore raw
Quoted 1 line · Felix Fietkau
Quoted 1 line
Quoted 1 line
(quoted diff) +0 -0
@@ -1090,10 +1119,25 @@ void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)

[...]

Quoted 11 lines
Quoted 9 lines

net/mac80211/sta_info.c void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta) · line 1139

Quoted 3 lines
net/mac80211/sta_info.c +3 -0
+			drv_wake_tx_queue(local, txqi);
+		}
+	}

This could be an interesting race. If you wake the queue, and then the station goes to sleep again before the driver had a chance to pull frames, what happens? Should the driver be responsible for this? But you don't have "unwake_tx_queue", so maybe you should not return any frame from the dequeue in such a case?

The driver is responsible for making sure that any queue of a sleeping sta is not scheduled.

Right but you're asking it to wake up the queue here, and if it goes to sleep again quickly the driver might not have realized soon enough?

I guess the device would filter the frames though so it's probably not a big deal, but could be interesting.

Quoted 10 lines
Quoted 9 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+	struct txq_info *txqi;

Hm, so, internally you allocate one big thing and externally to the driver you have a per-TID array.

Why not just remove this pointer, and keep only the per-TID array? You'd have to do a bit more container_of() but I don't think that's a big deal? Plus you can't really use this anyway since you can't index it, i.e. you cannot say sta->txqi[t] since the size doesn't match up.

Will do

Actually freeing it might be interesting then?

OTOH, you could just allocate each one separately as well, there's little to be gained from doing a bigger allocation for all of them.

Quoted 8 lines
Quoted 4 lines

Doesn't this become awkward with powersave handling for bufferable mgmt frames? They'd be stored on the per-station non-txq queues, but then the wakeup handling needs to see which ones to take first? Having these on the txqs might make that part easier?

I guess I'll change it to throw bufferable mgmt frames in the txq as well.

Quoted 2 lines

OTOH, I guess it would make building A-MPDUs or even A-MSDUs far more complicated, so I guess it's a reasonable tradeoff.

Yeah I'm not so sure it's a great idea though since it's certainly easier to do other things like A-MSDU/A-MPDU with only (QoS-)data frames here.

Since, as you mentioned on IRC, you're also thinking about a sort of fastpath I think then we should restrict that to only data frames and not worry about any mgmt frames for such a fastpath.

Quoted 8 lines
Quoted 6 lines

net/mac80211/tx.c ieee80211_tx_prepare(struct ieee80211_sub_if_data *sdata, · line 1284

Quoted 3 lines
net/mac80211/tx.c +3 -0
+	atomic_dec(&sdata->txqs_len[ac]);
+	if (__netif_subqueue_stopped(sdata->dev, ac))
+		ieee80211_propagate_queue_wake(local, sdata->vif.hw_queue[ac]);

Do you really want to do that unconditionally? There could be a lot of frames on the queue still, or even on other station queues?

Unconditionally? ieee80211_propagate_queue_wake checks the per-sdata txq limit.

Oh, right, I missed that part.

Quoted 9 lines
Quoted 6 lines

Anyway - overall I think this looks pretty good. What we discussed in Ottawa was that we should perhaps forego the whole qdisc and netdev queue start/stop and just do something like the qdisc would do in the layer of these queues, although then we'd have to first convert every driver or make this layer mandatory in some other way. That's something we should explore here I think.

I agree. Figuring out what tx queue scheduling should look like for devices with firmware based aggregation is going to be interesting though...

Yeah, though we're possibly also going to be moving in the direction of having hardware queues managed closer to the stations, so that the firmware could do better fairness between stations.

johannes