2 * Copyright (c) 2002-2007 Sam Leffler, Errno Consulting
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
30 * IEEE 802.11 power save support.
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
36 #include <sys/socket.h>
39 #include <net/if_media.h>
40 #include <net/ethernet.h>
42 #include <net80211/ieee80211_var.h>
46 static void ieee80211_set_tim(struct ieee80211_node *ni, int set);
49 ieee80211_power_attach(struct ieee80211com *ic)
51 /* NB: driver should override */
52 ic->ic_set_tim = ieee80211_set_tim;
56 ieee80211_power_lateattach(struct ieee80211com *ic)
58 ic->ic_tim_len = howmany(ic->ic_max_aid,8) * sizeof(uint8_t);
59 MALLOC(ic->ic_tim_bitmap, uint8_t *, ic->ic_tim_len,
60 M_DEVBUF, M_NOWAIT | M_ZERO);
61 if (ic->ic_tim_bitmap == NULL) {
62 printf("%s: no memory for TIM bitmap!\n", __func__);
63 /* XXX good enough to keep from crashing? */
69 ieee80211_power_detach(struct ieee80211com *ic)
71 if (ic->ic_tim_bitmap != NULL) {
72 FREE(ic->ic_tim_bitmap, M_DEVBUF);
73 ic->ic_tim_bitmap = NULL;
78 * Clear any frames queued on a node's power save queue.
79 * The number of frames that were present is returned.
82 ieee80211_node_saveq_drain(struct ieee80211_node *ni)
86 IEEE80211_NODE_SAVEQ_LOCK(ni);
87 qlen = IEEE80211_NODE_SAVEQ_QLEN(ni);
88 _IF_DRAIN(&ni->ni_savedq);
89 IEEE80211_NODE_SAVEQ_UNLOCK(ni);
95 * Age frames on the power save queue. The aging interval is
96 * 4 times the listen interval specified by the station. This
97 * number is factored into the age calculations when the frame
98 * is placed on the queue. We store ages as time differences
99 * so we can check and/or adjust only the head of the list.
100 * If a frame's age exceeds the threshold then discard it.
101 * The number of frames discarded is returned so the caller
102 * can check if it needs to adjust the tim.
105 ieee80211_node_saveq_age(struct ieee80211_node *ni)
109 if (IEEE80211_NODE_SAVEQ_QLEN(ni) != 0) {
112 IEEE80211_NODE_SAVEQ_LOCK(ni);
113 while (IF_POLL(&ni->ni_savedq, m) != NULL &&
114 M_AGE_GET(m) < IEEE80211_INACT_WAIT) {
115 IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_POWER, "[%s] discard frame, age %u\n", ether_sprintf(ni->ni_macaddr), M_AGE_GET(m));/*XXX*/
116 _IEEE80211_NODE_SAVEQ_DEQUEUE_HEAD(ni, m);
121 M_AGE_SUB(m, IEEE80211_INACT_WAIT);
122 IEEE80211_NODE_SAVEQ_UNLOCK(ni);
124 IEEE80211_NOTE(ni->ni_ic, IEEE80211_MSG_POWER, ni,
125 "discard %u frames for age", discard);
126 IEEE80211_NODE_STAT_ADD(ni, ps_discard, discard);
132 * Indicate whether there are frames queued for a station in power-save mode.
135 ieee80211_set_tim(struct ieee80211_node *ni, int set)
137 struct ieee80211com *ic = ni->ni_ic;
140 KASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP ||
141 ic->ic_opmode == IEEE80211_M_IBSS,
142 ("operating mode %u", ic->ic_opmode));
144 aid = IEEE80211_AID(ni->ni_associd);
145 KASSERT(aid < ic->ic_max_aid,
146 ("bogus aid %u, max %u", aid, ic->ic_max_aid));
148 IEEE80211_BEACON_LOCK(ic);
149 if (set != (isset(ic->ic_tim_bitmap, aid) != 0)) {
151 setbit(ic->ic_tim_bitmap, aid);
154 clrbit(ic->ic_tim_bitmap, aid);
157 /* NB: we know ic is in RUN state so no need to check */
158 ic->ic_update_beacon(ic, IEEE80211_BEACON_TIM);
160 IEEE80211_BEACON_UNLOCK(ic);
164 * Save an outbound packet for a node in power-save sleep state.
165 * The new packet is placed on the node's saved queue, and the TIM
166 * is changed, if necessary.
169 ieee80211_pwrsave(struct ieee80211_node *ni, struct mbuf *m)
171 struct ieee80211com *ic = ni->ni_ic;
174 IEEE80211_NODE_SAVEQ_LOCK(ni);
175 if (_IF_QFULL(&ni->ni_savedq)) {
176 _IF_DROP(&ni->ni_savedq);
177 IEEE80211_NODE_SAVEQ_UNLOCK(ni);
178 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
179 "[%s] pwr save q overflow, drops %d (size %d)\n",
180 ether_sprintf(ni->ni_macaddr),
181 ni->ni_savedq.ifq_drops, IEEE80211_PS_MAX_QUEUE);
182 #ifdef IEEE80211_DEBUG
183 if (ieee80211_msg_dumppkts(ic))
184 ieee80211_dump_pkt(ic, mtod(m, caddr_t), m->m_len, -1, -1);
190 * Tag the frame with it's expiry time and insert
191 * it in the queue. The aging interval is 4 times
192 * the listen interval specified by the station.
193 * Frames that sit around too long are reclaimed
194 * using this information.
196 /* TU -> secs. XXX handle overflow? */
197 age = IEEE80211_TU_TO_MS((ni->ni_intval * ic->ic_bintval) << 2) / 1000;
198 _IEEE80211_NODE_SAVEQ_ENQUEUE(ni, m, qlen, age);
199 IEEE80211_NODE_SAVEQ_UNLOCK(ni);
201 IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
202 "[%s] save frame with age %d, %u now queued\n",
203 ether_sprintf(ni->ni_macaddr), age, qlen);
205 if (qlen == 1 && ic->ic_set_tim != NULL)
206 ic->ic_set_tim(ni, 1);
210 * Handle station power-save state change.
213 ieee80211_node_pwrsave(struct ieee80211_node *ni, int enable)
215 struct ieee80211com *ic = ni->ni_ic;
216 struct mbuf *m, *mhead, *mtail;
220 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) == 0)
222 ni->ni_flags |= IEEE80211_NODE_PWR_MGT;
223 IEEE80211_NOTE(ic, IEEE80211_MSG_POWER, ni,
224 "power save mode on, %u sta's in ps mode", ic->ic_ps_sta);
228 if (ni->ni_flags & IEEE80211_NODE_PWR_MGT)
230 ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
231 IEEE80211_NOTE(ic, IEEE80211_MSG_POWER, ni,
232 "power save mode off, %u sta's in ps mode", ic->ic_ps_sta);
233 /* XXX if no stations in ps mode, flush mc frames */
236 * Flush queued unicast frames.
238 if (IEEE80211_NODE_SAVEQ_QLEN(ni) == 0) {
239 if (ic->ic_set_tim != NULL)
240 ic->ic_set_tim(ni, 0); /* just in case */
243 IEEE80211_NOTE(ic, IEEE80211_MSG_POWER, ni,
244 "flush ps queue, %u packets queue", IEEE80211_NODE_SAVEQ_QLEN(ni));
246 * Unload the frames from the ps q but don't send them
247 * to the driver yet. We do this in two stages to minimize
248 * locking but also because there's no easy way to preserve
249 * ordering given the existing ifnet access mechanisms.
250 * XXX could be optimized
252 IEEE80211_NODE_SAVEQ_LOCK(ni);
253 mcount = IEEE80211_NODE_SAVEQ_QLEN(ni);
254 mhead = mtail = NULL;
256 _IEEE80211_NODE_SAVEQ_DEQUEUE_HEAD(ni, m);
263 mtail->m_nextpkt = m;
266 IEEE80211_NODE_SAVEQ_UNLOCK(ni);
268 /* XXX need different driver interface */
269 /* XXX bypasses q max */
270 IF_PREPEND_LIST(&ic->ic_ifp->if_snd, mhead, mtail, mcount);
272 if (ic->ic_set_tim != NULL)
273 ic->ic_set_tim(ni, 0);
277 * Handle power-save state change in station mode.
280 ieee80211_sta_pwrsave(struct ieee80211com *ic, int enable)
282 struct ieee80211_node *ni = ic->ic_bss;
285 if (!((enable != 0) ^ ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) != 0)))
288 IEEE80211_NOTE(ic, IEEE80211_MSG_POWER, ni,
289 "sta power save mode %s", enable ? "on" : "off");
291 ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
292 ieee80211_send_nulldata(ieee80211_ref_node(ni));
294 * Flush any queued frames; we can do this immediately
295 * because we know they'll be queued behind the null
296 * data frame we send the ap.
297 * XXX can we use a data frame to take us out of ps?
299 qlen = IEEE80211_NODE_SAVEQ_QLEN(ni);
301 IEEE80211_NOTE(ic, IEEE80211_MSG_POWER, ni,
302 "flush ps queue, %u packets queued", qlen);
306 IEEE80211_NODE_SAVEQ_LOCK(ni);
307 _IEEE80211_NODE_SAVEQ_DEQUEUE_HEAD(ni, m);
308 IEEE80211_NODE_SAVEQ_UNLOCK(ni);
311 /* XXX need different driver interface */
312 /* XXX bypasses q max */
313 IF_ENQUEUE(&ic->ic_ifp->if_snd, m);
317 ni->ni_flags |= IEEE80211_NODE_PWR_MGT;
318 ieee80211_send_nulldata(ieee80211_ref_node(ni));