madwifi/net80211/ieee80211_power.c

392 lines
11 KiB
C
Raw Normal View History

/*-
* Copyright (c) 2001 Atsushi Onoe
* Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* Alternatively, this software may be distributed under the terms of the
* GNU General Public License ("GPL") version 2 as published by the Free
* Software Foundation.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id$
*/
#ifndef EXPORT_SYMTAB
#define EXPORT_SYMTAB
#endif
/*
* IEEE 802.11 power save support.
*/
#ifndef AUTOCONF_INCLUDED
#include <linux/config.h>
#endif
#include <linux/version.h>
#include <linux/module.h>
#include <linux/skbuff.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include "if_media.h"
#include <net80211/ieee80211_var.h>
#include <net80211/ieee80211_proto.h>
#ifndef NETDEV_TX_OK
#define NETDEV_TX_OK 0
#define NETDEV_TX_BUSY 1
#endif
static void ieee80211_set_tim(struct ieee80211_node *ni, int set);
void
ieee80211_power_attach(struct ieee80211com *ic)
{
}
void
ieee80211_power_detach(struct ieee80211com *ic)
{
}
void
ieee80211_power_vattach(struct ieee80211vap *vap)
{
if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
vap->iv_opmode == IEEE80211_M_IBSS) {
/* NB: driver should override */
vap->iv_set_tim = ieee80211_set_tim;
}
}
void
ieee80211_power_latevattach(struct ieee80211vap *vap)
{
/*
* Allocate these only if needed. Beware that we
* know adhoc mode doesn't support ATIM yet...
*/
if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
vap->iv_tim_len = howmany(vap->iv_max_aid, 8) * sizeof(u_int8_t);
MALLOC(vap->iv_tim_bitmap, u_int8_t *, vap->iv_tim_len,
M_DEVBUF, M_NOWAIT | M_ZERO);
if (vap->iv_tim_bitmap == NULL) {
printf("%s: no memory for TIM bitmap!\n", __func__);
/* XXX good enough to keep from crashing? */
vap->iv_tim_len = 0;
}
}
}
void
ieee80211_power_vdetach(struct ieee80211vap *vap)
{
if (vap->iv_tim_bitmap != NULL) {
FREE(vap->iv_tim_bitmap, M_DEVBUF);
vap->iv_tim_bitmap = NULL;
}
}
/*
* Clear any frames queued on a node's power save queue.
* The number of frames that were present is returned.
*/
int
ieee80211_node_saveq_drain(struct ieee80211_node *ni)
{
struct sk_buff *skb;
int qlen;
IEEE80211_NODE_SAVEQ_LOCK_IRQ(ni);
qlen = IEEE80211_NODE_SAVEQ_QLEN(ni);
while ((skb = __skb_dequeue(&ni->ni_savedq)) != NULL) {
This patch augments the current reference counting code with: * Counters for total outstanding instances for each resource type (skb, ath_node and ath_buf) * One pair of acquisition/release functions per resource type in unlocked and one in locked * Adds some more _debug versions of functions in the call chain that acquire/release resources so that the original func/line in the driver as well as the func/line that affected the resource use can be shown in the trace. Intermediate stack frames aren't necessary to trace the leaks. * Changes naming convention for "lock-required" functions to suffix _locked for the versions that expect locking, to be consistent with some other places in the code. * Consolidate debug messages to the helper functions that actually affect the reference count or acquire/release a resource * Additional sanity checks and leak detection (esp for detecting node ref leaks through skb) * skb references are nulled out by the new sbk unref/free function. I've tested these changes extensively and found lots of cases where we didn't get enough node references when cloning skbuff, and where the kernel drops packets due to performance issues and leaks our node references. With these changes and the tracing enabled I have verified that: * TX BUF: tx buffers always go down to zero when the tx queue is done, and you can watch tx queue usage ratio go up and down again as the driver is working. There are no leaks here at the moment, although there *are* some in the madwifi-dfs branch during CAC at the moment. * skbuff leaks in all the common flows are fixed. We were leaking node references in a lot of places where kernel was dropping skb's due to congestion and we were failing to increment node references when cloning skbuffs. These are now detected, as are skbuffs that are reaped by the kernel while still holding a node reference. * the ath_node count works correctly and on an idle system we get about 5 references per station table node, with 2 node instances per VAP. One for the bss and one for the node in the station table, I believe. The ath_node count goes up and down but always lands back at the stable number based on the vaps you have configured and the number of actual stations in the station table. The point here is that it's pretty constant what you will see over time, despite excessive node creation/release in our code during input (esp input_all). Thank god for the slab allocator. git-svn-id: http://madwifi-project.org/svn/madwifi/trunk@2902 0192ed92-7a03-0410-a25b-9323aeb14dbd
2007-11-21 23:14:11 +03:00
if (SKB_CB(skb)->ni != NULL)
ieee80211_unref_node(&SKB_CB(skb)->ni);
ieee80211_dev_kfree_skb(&skb);
}
IEEE80211_NODE_SAVEQ_UNLOCK_IRQ(ni);
return qlen;
}
/*
* Age frames on the power save queue. The aging interval is
* 4 times the listen interval specified by the station. This
* number is factored into the age calculations when the frame
* is placed on the queue. We store ages as time differences
* so we can check and/or adjust only the head of the list.
* If a frame's age exceeds the threshold then discard it.
* The number of frames discarded is returned so the caller
* can check if it needs to adjust the tim.
*/
int
ieee80211_node_saveq_age(struct ieee80211_node *ni)
{
int discard = 0;
/* XXX racey but good 'nuf? */
if (IEEE80211_NODE_SAVEQ_QLEN(ni) != 0) {
#ifdef IEEE80211_DEBUG
struct ieee80211vap *vap = ni->ni_vap;
#endif
struct sk_buff *skb;
IEEE80211_NODE_SAVEQ_LOCK_IRQ(ni);
while ((skb = skb_peek(&ni->ni_savedq)) != NULL &&
M_AGE_GET(skb) < IEEE80211_INACT_WAIT) {
IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
"discard frame, age %u", M_AGE_GET(skb));
skb = __skb_dequeue(&ni->ni_savedq);
if (SKB_CB(skb)->ni != NULL)
This patch augments the current reference counting code with: * Counters for total outstanding instances for each resource type (skb, ath_node and ath_buf) * One pair of acquisition/release functions per resource type in unlocked and one in locked * Adds some more _debug versions of functions in the call chain that acquire/release resources so that the original func/line in the driver as well as the func/line that affected the resource use can be shown in the trace. Intermediate stack frames aren't necessary to trace the leaks. * Changes naming convention for "lock-required" functions to suffix _locked for the versions that expect locking, to be consistent with some other places in the code. * Consolidate debug messages to the helper functions that actually affect the reference count or acquire/release a resource * Additional sanity checks and leak detection (esp for detecting node ref leaks through skb) * skb references are nulled out by the new sbk unref/free function. I've tested these changes extensively and found lots of cases where we didn't get enough node references when cloning skbuff, and where the kernel drops packets due to performance issues and leaks our node references. With these changes and the tracing enabled I have verified that: * TX BUF: tx buffers always go down to zero when the tx queue is done, and you can watch tx queue usage ratio go up and down again as the driver is working. There are no leaks here at the moment, although there *are* some in the madwifi-dfs branch during CAC at the moment. * skbuff leaks in all the common flows are fixed. We were leaking node references in a lot of places where kernel was dropping skb's due to congestion and we were failing to increment node references when cloning skbuffs. These are now detected, as are skbuffs that are reaped by the kernel while still holding a node reference. * the ath_node count works correctly and on an idle system we get about 5 references per station table node, with 2 node instances per VAP. One for the bss and one for the node in the station table, I believe. The ath_node count goes up and down but always lands back at the stable number based on the vaps you have configured and the number of actual stations in the station table. The point here is that it's pretty constant what you will see over time, despite excessive node creation/release in our code during input (esp input_all). Thank god for the slab allocator. git-svn-id: http://madwifi-project.org/svn/madwifi/trunk@2902 0192ed92-7a03-0410-a25b-9323aeb14dbd
2007-11-21 23:14:11 +03:00
ieee80211_unref_node(&SKB_CB(skb)->ni);
ieee80211_dev_kfree_skb(&skb);
discard++;
}
if (skb != NULL)
M_AGE_SUB(skb, IEEE80211_INACT_WAIT);
IEEE80211_NODE_SAVEQ_UNLOCK_IRQ(ni);
IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
"discard %u frames for age", discard);
IEEE80211_NODE_STAT_ADD(ni, ps_discard, discard);
}
return discard;
}
/*
* Indicate whether there are frames queued for a station in power-save mode.
*/
static void
ieee80211_set_tim(struct ieee80211_node *ni, int set)
{
struct ieee80211vap *vap = ni->ni_vap;
u_int16_t aid;
KASSERT(vap->iv_opmode == IEEE80211_M_HOSTAP ||
vap->iv_opmode == IEEE80211_M_IBSS,
("operating mode %u", vap->iv_opmode));
aid = IEEE80211_AID(ni->ni_associd);
KASSERT(aid < vap->iv_max_aid,
("bogus aid %u, max %u", aid, vap->iv_max_aid));
IEEE80211_LOCK_IRQ(ni->ni_ic);
if (set != (isset(vap->iv_tim_bitmap, aid) != 0)) {
if (set) {
setbit(vap->iv_tim_bitmap, aid);
vap->iv_ps_pending++;
} else {
clrbit(vap->iv_tim_bitmap, aid);
vap->iv_ps_pending--;
}
vap->iv_flags |= IEEE80211_F_TIMUPDATE;
}
IEEE80211_UNLOCK_IRQ(ni->ni_ic);
}
/*
* Save an outbound packet for a node in power-save sleep state.
* The new packet is placed on the node's saved queue, and the TIM
* is changed, if necessary.
* It must return either NETDEV_TX_OK or NETDEV_TX_BUSY
*/
int
ieee80211_pwrsave(struct sk_buff *skb)
{
struct ieee80211_node *ni = SKB_CB(skb)->ni;
struct ieee80211vap *vap = ni->ni_vap;
struct ieee80211com *ic = ni->ni_ic;
struct sk_buff *tail;
int qlen, age;
IEEE80211_NODE_SAVEQ_LOCK_IRQ(ni);
if (IEEE80211_NODE_SAVEQ_QLEN(ni) >= IEEE80211_PS_MAX_QUEUE) {
IEEE80211_NODE_STAT(ni, psq_drops);
IEEE80211_NODE_SAVEQ_UNLOCK_IRQ_EARLY(ni);
IEEE80211_NOTE(vap, IEEE80211_MSG_ANY, ni,
"pwr save q overflow, drops %d (size %d)",
ni->ni_stats.ns_psq_drops, IEEE80211_PS_MAX_QUEUE);
#ifdef IEEE80211_DEBUG
if (ieee80211_msg_dumppkts(vap))
ieee80211_dump_pkt(ni->ni_ic, skb->data, skb->len, -1, -1);
#endif
if (SKB_CB(skb)->ni != NULL)
ieee80211_unref_node(&SKB_CB(skb)->ni);
return NETDEV_TX_BUSY;
}
/*
* Tag the frame with its expiry time and insert
* it in the queue. The aging interval is 4 times
* the listen interval specified by the station.
* Frames that sit around too long are reclaimed
* using this information.
*/
/* XXX handle overflow? */
age = ((ni->ni_intval * ic->ic_lintval) << 2) / 1024; /* TU -> secs */
tail = skb_peek_tail(&ni->ni_savedq);
if (tail != NULL) {
age -= M_AGE_GET(tail);
__skb_append(tail, skb, &ni->ni_savedq);
} else
__skb_queue_head(&ni->ni_savedq, skb);
M_AGE_SET(skb, age);
qlen = IEEE80211_NODE_SAVEQ_QLEN(ni);
IEEE80211_NODE_SAVEQ_UNLOCK_IRQ(ni);
IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
"save frame, %u now queued", qlen);
if (qlen == 1 && vap->iv_set_tim != NULL)
vap->iv_set_tim(ni, 1);
return NETDEV_TX_OK;
}
/*
* Handle power-save state change in ap/ibss mode.
*/
void
ieee80211_node_pwrsave(struct ieee80211_node *ni, int enable)
{
struct ieee80211vap *vap = ni->ni_vap;
KASSERT(vap->iv_opmode == IEEE80211_M_HOSTAP ||
vap->iv_opmode == IEEE80211_M_IBSS,
("unexpected operating mode %u", vap->iv_opmode));
if (enable) {
if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) == 0)
vap->iv_ps_sta++;
ni->ni_flags |= IEEE80211_NODE_PWR_MGT;
IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
"power save mode on, %u STAs in PS mode",
vap->iv_ps_sta);
} else {
if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT))
vap->iv_ps_sta--;
ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
"power save mode off, %u STAs in PS mode",
vap->iv_ps_sta);
/* XXX if no stations in ps mode, flush mc frames */
/*
* Flush queued unicast frames.
*/
if (IEEE80211_NODE_SAVEQ_QLEN(ni) == 0) {
if (vap->iv_set_tim != NULL)
vap->iv_set_tim(ni, 0); /* just in case */
return;
}
IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
"flush ps queue, %u packets queued",
IEEE80211_NODE_SAVEQ_QLEN(ni));
for (;;) {
struct sk_buff *skb;
int qlen;
IEEE80211_NODE_SAVEQ_LOCK_IRQ(ni);
IEEE80211_NODE_SAVEQ_DEQUEUE(ni, skb, qlen);
IEEE80211_NODE_SAVEQ_UNLOCK_IRQ(ni);
if (skb == NULL)
break;
/*
* If this is the last packet, turn off the TIM bit.
*
* Set the M_PWR_SAV bit on skb to allow encap to test for
* adding MORE_DATA bit to wh.
*
* The 802.11 MAC Spec says we should only set MORE_DATA for
* unicast packets when the STA is in PS mode (7.1.3.1.8);
* which it isn't.
*/
#if 0
M_PWR_SAV_SET(skb);
#endif
#ifdef ATH_SUPERG_XR
/*
* if it is a XR vap, send the data to associated normal net
* device. XR vap has a net device which is not registered with
* OS.
*/
if (vap->iv_xrvap && vap->iv_flags & IEEE80211_F_XR)
skb->dev = vap->iv_xrvap->iv_dev;
else
skb->dev = vap->iv_dev; /* XXX? unnecessary */
#endif
ieee80211_parent_queue_xmit(skb);
}
vap->iv_set_tim(ni, 0);
}
}
/*
* Handle power-save state change in station mode.
*/
void
ieee80211_sta_pwrsave(struct ieee80211vap *vap, int enable)
{
struct ieee80211_node *ni = vap->iv_bss;
int qlen;
if (!!enable == !!IEEE80211_VAP_IS_SLEEPING(vap)) /* Bool. normalise */
return;
IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
"STA power save mode %s", enable ? "on" : "off");
if (!enable) {
IEEE80211_VAP_WAKEUP(vap);
ieee80211_send_nulldata(ieee80211_ref_node(ni));
/*
* Flush any queued frames; we can do this immediately
* because we know they'll be queued behind the null
* data frame we send the ap.
* XXX: Can we use a data frame to take us out of PS mode?
*/
qlen = IEEE80211_NODE_SAVEQ_QLEN(ni);
if (qlen != 0) {
IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
"flush PS queue, %u packets queued", qlen);
for (;;) {
struct sk_buff *skb;
IEEE80211_NODE_SAVEQ_LOCK_IRQ(ni);
skb = __skb_dequeue(&ni->ni_savedq);
IEEE80211_NODE_SAVEQ_UNLOCK_IRQ(ni);
if (skb == NULL)
break;
ieee80211_parent_queue_xmit(skb);
}
}
} else {
IEEE80211_VAP_GOTOSLEEP(vap);
ieee80211_send_nulldata(ieee80211_ref_node(ni));
}
}