Skip to content

Commit

Permalink
mac80211: Add airtime accounting and scheduling to TXQs
Browse files Browse the repository at this point in the history
This adds airtime accounting and scheduling to the mac80211 TXQ
scheduler. A new callback, ieee80211_sta_register_airtime(), is added
that drivers can call to report airtime usage for stations.

When airtime information is present, mac80211 will schedule TXQs
(through ieee80211_next_txq()) in a way that enforces airtime fairness
between active stations. This scheduling works the same way as the ath9k
in-driver airtime fairness scheduling. If no airtime usage is reported
by the driver, the scheduler will default to round-robin scheduling.

For drivers that don't control TXQ scheduling in software, a new API
function, ieee80211_txq_may_transmit(), is added which the driver can use
to check if the TXQ is eligible for transmission, or should be throttled to
enforce fairness. Calls to this function must also be enclosed in
ieee80211_txq_schedule_{start,end}() calls to ensure proper locking.

The API ieee80211_txq_may_transmit() also ensures that TXQ list will be
aligned aginst driver's own round-robin scheduler list. i.e it rotates
the TXQ list till it makes the requested node becomes the first entry
in TXQ list. Thus both the TXQ list and driver's list are in sync.

Co-developed-by: Rajkumar Manoharan <[email protected]>
Signed-off-by: Louie Lu <[email protected]>
[added debugfs write op to reset airtime counter]
Signed-off-by: Toke Høiland-Jørgensen <[email protected]>
Signed-off-by: Rajkumar Manoharan <[email protected]>
Signed-off-by: Johannes Berg <[email protected]>
  • Loading branch information
tohojo authored and jmberg-intel committed Jan 19, 2019
1 parent 3664705 commit b4809e9
Show file tree
Hide file tree
Showing 10 changed files with 282 additions and 10 deletions.
59 changes: 59 additions & 0 deletions include/net/mac80211.h
Original file line number Diff line number Diff line change
Expand Up @@ -2360,6 +2360,9 @@ enum ieee80211_hw_flags {
* @tx_sk_pacing_shift: Pacing shift to set on TCP sockets when frames from
* them are encountered. The default should typically not be changed,
* unless the driver has good reasons for needing more buffers.
*
* @weight_multipler: Driver specific airtime weight multiplier used while
* refilling deficit of each TXQ.
*/
struct ieee80211_hw {
struct ieee80211_conf conf;
Expand Down Expand Up @@ -2396,6 +2399,7 @@ struct ieee80211_hw {
const struct ieee80211_cipher_scheme *cipher_schemes;
u8 max_nan_de_entries;
u8 tx_sk_pacing_shift;
u8 weight_multiplier;
};

static inline bool _ieee80211_hw_check(struct ieee80211_hw *hw,
Expand Down Expand Up @@ -5407,6 +5411,34 @@ void ieee80211_sta_eosp(struct ieee80211_sta *pubsta);
*/
void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid);

/**
* ieee80211_sta_register_airtime - register airtime usage for a sta/tid
*
* Register airtime usage for a given sta on a given tid. The driver can call
* this function to notify mac80211 that a station used a certain amount of
* airtime. This information will be used by the TXQ scheduler to schedule
* stations in a way that ensures airtime fairness.
*
* The reported airtime should as a minimum include all time that is spent
* transmitting to the remote station, including overhead and padding, but not
* including time spent waiting for a TXOP. If the time is not reported by the
* hardware it can in some cases be calculated from the rate and known frame
* composition. When possible, the time should include any failed transmission
* attempts.
*
* The driver can either call this function synchronously for every packet or
* aggregate, or asynchronously as airtime usage information becomes available.
* TX and RX airtime can be reported together, or separately by setting one of
* them to 0.
*
* @pubsta: the station
* @tid: the TID to register airtime for
* @tx_airtime: airtime used during TX (in usec)
* @rx_airtime: airtime used during RX (in usec)
*/
void ieee80211_sta_register_airtime(struct ieee80211_sta *pubsta, u8 tid,
u32 tx_airtime, u32 rx_airtime);

/**
* ieee80211_iter_keys - iterate keys programmed into the device
* @hw: pointer obtained from ieee80211_alloc_hw()
Expand Down Expand Up @@ -6173,6 +6205,33 @@ void ieee80211_txq_schedule_start(struct ieee80211_hw *hw, u8 ac)
void ieee80211_txq_schedule_end(struct ieee80211_hw *hw, u8 ac)
__releases(txq_lock);

/**
* ieee80211_txq_may_transmit - check whether TXQ is allowed to transmit
*
* This function is used to check whether given txq is allowed to transmit by
* the airtime scheduler, and can be used by drivers to access the airtime
* fairness accounting without going using the scheduling order enfored by
* next_txq().
*
* Returns %true if the airtime scheduler thinks the TXQ should be allowed to
* transmit, and %false if it should be throttled. This function can also have
* the side effect of rotating the TXQ in the scheduler rotation, which will
* eventually bring the deficit to positive and allow the station to transmit
* again.
*
* The API ieee80211_txq_may_transmit() also ensures that TXQ list will be
* aligned aginst driver's own round-robin scheduler list. i.e it rotates
* the TXQ list till it makes the requested node becomes the first entry
* in TXQ list. Thus both the TXQ list and driver's list are in sync. If this
* function returns %true, the driver is expected to schedule packets
* for transmission, and then return the TXQ through ieee80211_return_txq().
*
* @hw: pointer as obtained from ieee80211_alloc_hw()
* @txq: pointer obtained from station or virtual interface
*/
bool ieee80211_txq_may_transmit(struct ieee80211_hw *hw,
struct ieee80211_txq *txq);

/**
* ieee80211_txq_get_depth - get pending frame/byte count of given txq
*
Expand Down
3 changes: 3 additions & 0 deletions net/mac80211/cfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -1447,6 +1447,9 @@ static int sta_apply_parameters(struct ieee80211_local *local,
if (ieee80211_vif_is_mesh(&sdata->vif))
sta_apply_mesh_params(local, sta, params);

if (params->airtime_weight)
sta->airtime_weight = params->airtime_weight;

/* set the STA state after all sta info from usermode has been set */
if (test_sta_flag(sta, WLAN_STA_TDLS_PEER) ||
set & BIT(NL80211_STA_FLAG_ASSOCIATED)) {
Expand Down
3 changes: 3 additions & 0 deletions net/mac80211/debugfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,9 @@ void debugfs_hw_add(struct ieee80211_local *local)
if (local->ops->wake_tx_queue)
DEBUGFS_ADD_MODE(aqm, 0600);

debugfs_create_u16("airtime_flags", 0600,
phyd, &local->airtime_flags);

statsd = debugfs_create_dir("statistics", phyd);

/* if the dir failed, don't put all the other things into the root! */
Expand Down
68 changes: 65 additions & 3 deletions net/mac80211/debugfs_sta.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,9 @@ static ssize_t sta_aqm_read(struct file *file, char __user *userbuf,
txqi->tin.tx_bytes,
txqi->tin.tx_packets,
txqi->flags,
txqi->flags & (1<<IEEE80211_TXQ_STOP) ? "STOP" : "RUN",
txqi->flags & (1<<IEEE80211_TXQ_AMPDU) ? " AMPDU" : "",
txqi->flags & (1<<IEEE80211_TXQ_NO_AMSDU) ? " NO-AMSDU" : "");
test_bit(IEEE80211_TXQ_STOP, &txqi->flags) ? "STOP" : "RUN",
test_bit(IEEE80211_TXQ_AMPDU, &txqi->flags) ? " AMPDU" : "",
test_bit(IEEE80211_TXQ_NO_AMSDU, &txqi->flags) ? " NO-AMSDU" : "");
}

rcu_read_unlock();
Expand All @@ -195,6 +195,64 @@ static ssize_t sta_aqm_read(struct file *file, char __user *userbuf,
}
STA_OPS(aqm);

static ssize_t sta_airtime_read(struct file *file, char __user *userbuf,
size_t count, loff_t *ppos)
{
struct sta_info *sta = file->private_data;
struct ieee80211_local *local = sta->sdata->local;
size_t bufsz = 200;
char *buf = kzalloc(bufsz, GFP_KERNEL), *p = buf;
u64 rx_airtime = 0, tx_airtime = 0;
s64 deficit[IEEE80211_NUM_ACS];
ssize_t rv;
int ac;

if (!buf)
return -ENOMEM;

for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
spin_lock_bh(&local->active_txq_lock[ac]);
rx_airtime += sta->airtime[ac].rx_airtime;
tx_airtime += sta->airtime[ac].tx_airtime;
deficit[ac] = sta->airtime[ac].deficit;
spin_unlock_bh(&local->active_txq_lock[ac]);
}

p += scnprintf(p, bufsz + buf - p,
"RX: %llu us\nTX: %llu us\nWeight: %u\n"
"Deficit: VO: %lld us VI: %lld us BE: %lld us BK: %lld us\n",
rx_airtime,
tx_airtime,
sta->airtime_weight,
deficit[0],
deficit[1],
deficit[2],
deficit[3]);

rv = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
kfree(buf);
return rv;
}

static ssize_t sta_airtime_write(struct file *file, const char __user *userbuf,
size_t count, loff_t *ppos)
{
struct sta_info *sta = file->private_data;
struct ieee80211_local *local = sta->sdata->local;
int ac;

for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
spin_lock_bh(&local->active_txq_lock[ac]);
sta->airtime[ac].rx_airtime = 0;
sta->airtime[ac].tx_airtime = 0;
sta->airtime[ac].deficit = sta->airtime_weight;
spin_unlock_bh(&local->active_txq_lock[ac]);
}

return count;
}
STA_OPS_RW(airtime);

static ssize_t sta_agg_status_read(struct file *file, char __user *userbuf,
size_t count, loff_t *ppos)
{
Expand Down Expand Up @@ -906,6 +964,10 @@ void ieee80211_sta_debugfs_add(struct sta_info *sta)
if (local->ops->wake_tx_queue)
DEBUGFS_ADD(aqm);

if (wiphy_ext_feature_isset(local->hw.wiphy,
NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
DEBUGFS_ADD(airtime);

if (sizeof(sta->driver_buffered_tids) == sizeof(u32))
debugfs_create_x32("driver_buffered_tids", 0400,
sta->debugfs_dir,
Expand Down
2 changes: 2 additions & 0 deletions net/mac80211/ieee80211_i.h
Original file line number Diff line number Diff line change
Expand Up @@ -1138,6 +1138,8 @@ struct ieee80211_local {
struct list_head active_txqs[IEEE80211_NUM_ACS];
u16 schedule_round[IEEE80211_NUM_ACS];

u16 airtime_flags;

const struct ieee80211_ops *ops;

/*
Expand Down
4 changes: 4 additions & 0 deletions net/mac80211/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,7 @@ struct ieee80211_hw *ieee80211_alloc_hw_nm(size_t priv_data_len,
INIT_LIST_HEAD(&local->active_txqs[i]);
spin_lock_init(&local->active_txq_lock[i]);
}
local->airtime_flags = AIRTIME_USE_TX | AIRTIME_USE_RX;

INIT_LIST_HEAD(&local->chanctx_list);
mutex_init(&local->chanctx_mtx);
Expand Down Expand Up @@ -1153,6 +1154,9 @@ int ieee80211_register_hw(struct ieee80211_hw *hw)
if (!local->hw.max_nan_de_entries)
local->hw.max_nan_de_entries = IEEE80211_MAX_NAN_INSTANCE_ID;

if (!local->hw.weight_multiplier)
local->hw.weight_multiplier = 1;

result = ieee80211_wep_init(local);
if (result < 0)
wiphy_debug(local->hw.wiphy, "Failed to initialize wep: %d\n",
Expand Down
44 changes: 41 additions & 3 deletions net/mac80211/sta_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ static void __cleanup_single_sta(struct sta_info *sta)
struct tid_ampdu_tx *tid_tx;
struct ieee80211_sub_if_data *sdata = sta->sdata;
struct ieee80211_local *local = sdata->local;
struct fq *fq = &local->fq;
struct ps_data *ps;

if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
Expand Down Expand Up @@ -120,9 +119,7 @@ static void __cleanup_single_sta(struct sta_info *sta)

txqi = to_txq_info(sta->sta.txq[i]);

spin_lock_bh(&fq->lock);
ieee80211_txq_purge(local, txqi);
spin_unlock_bh(&fq->lock);
}
}

Expand Down Expand Up @@ -387,9 +384,12 @@ struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
if (sta_prepare_rate_control(local, sta, gfp))
goto free_txq;

sta->airtime_weight = IEEE80211_DEFAULT_AIRTIME_WEIGHT;

for (i = 0; i < IEEE80211_NUM_ACS; i++) {
skb_queue_head_init(&sta->ps_tx_buf[i]);
skb_queue_head_init(&sta->tx_filtered[i]);
sta->airtime[i].deficit = sta->airtime_weight;
}

for (i = 0; i < IEEE80211_NUM_TIDS; i++)
Expand Down Expand Up @@ -1826,6 +1826,27 @@ void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
}
EXPORT_SYMBOL(ieee80211_sta_set_buffered);

void ieee80211_sta_register_airtime(struct ieee80211_sta *pubsta, u8 tid,
u32 tx_airtime, u32 rx_airtime)
{
struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
struct ieee80211_local *local = sta->sdata->local;
u8 ac = ieee80211_ac_from_tid(tid);
u32 airtime = 0;

if (sta->local->airtime_flags & AIRTIME_USE_TX)
airtime += tx_airtime;
if (sta->local->airtime_flags & AIRTIME_USE_RX)
airtime += rx_airtime;

spin_lock_bh(&local->active_txq_lock[ac]);
sta->airtime[ac].tx_airtime += tx_airtime;
sta->airtime[ac].rx_airtime += rx_airtime;
sta->airtime[ac].deficit -= airtime;
spin_unlock_bh(&local->active_txq_lock[ac]);
}
EXPORT_SYMBOL(ieee80211_sta_register_airtime);

int sta_info_move_state(struct sta_info *sta,
enum ieee80211_sta_state new_state)
{
Expand Down Expand Up @@ -2188,6 +2209,23 @@ void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo,
sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
}

if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_DURATION))) {
for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
sinfo->rx_duration += sta->airtime[ac].rx_airtime;
sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
}

if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_DURATION))) {
for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
sinfo->tx_duration += sta->airtime[ac].tx_airtime;
sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
}

if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT))) {
sinfo->airtime_weight = sta->airtime_weight;
sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT);
}

sinfo->rx_dropped_misc = sta->rx_stats.dropped;
if (sta->pcpu_rx_stats) {
for_each_possible_cpu(cpu) {
Expand Down
13 changes: 13 additions & 0 deletions net/mac80211/sta_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,16 @@ enum ieee80211_agg_stop_reason {
AGG_STOP_DESTROY_STA,
};

/* Debugfs flags to enable/disable use of RX/TX airtime in scheduler */
#define AIRTIME_USE_TX BIT(0)
#define AIRTIME_USE_RX BIT(1)

struct airtime_info {
u64 rx_airtime;
u64 tx_airtime;
s64 deficit;
};

struct sta_info;

/**
Expand Down Expand Up @@ -565,6 +575,9 @@ struct sta_info {
} tx_stats;
u16 tid_seq[IEEE80211_QOS_CTL_TID_MASK + 1];

struct airtime_info airtime[IEEE80211_NUM_ACS];
u16 airtime_weight;

/*
* Aggregation information, locked with lock.
*/
Expand Down
6 changes: 6 additions & 0 deletions net/mac80211/status.c
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,12 @@ static void __ieee80211_tx_status(struct ieee80211_hw *hw,
ieee80211_sta_tx_notify(sta->sdata, (void *) skb->data,
acked, info->status.tx_time);

if (info->status.tx_time &&
wiphy_ext_feature_isset(local->hw.wiphy,
NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
ieee80211_sta_register_airtime(&sta->sta, tid,
info->status.tx_time, 0);

if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
if (info->flags & IEEE80211_TX_STAT_ACK) {
if (sta->status_stats.lost_packets)
Expand Down
Loading

0 comments on commit b4809e9

Please sign in to comment.