2 * Copyright (c) 2002-2008 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.
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
38 #include <sys/socket.h>
41 #include <net/if_media.h>
42 #include <net/ethernet.h>
44 #include <net80211/ieee80211_var.h>
48 static void ieee80211_update_ps(struct ieee80211vap *, int);
49 static int ieee80211_set_tim(struct ieee80211_node *, int);
51 static MALLOC_DEFINE(M_80211_POWER, "80211power", "802.11 power save state");
54 ieee80211_power_attach(struct ieee80211com *ic)
59 ieee80211_power_detach(struct ieee80211com *ic)
64 ieee80211_power_vattach(struct ieee80211vap *vap)
66 if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
67 vap->iv_opmode == IEEE80211_M_IBSS) {
68 /* NB: driver should override */
69 vap->iv_update_ps = ieee80211_update_ps;
70 vap->iv_set_tim = ieee80211_set_tim;
72 vap->iv_node_ps = ieee80211_node_pwrsave;
73 vap->iv_sta_ps = ieee80211_sta_pwrsave;
77 ieee80211_power_latevattach(struct ieee80211vap *vap)
80 * Allocate these only if needed. Beware that we
81 * know adhoc mode doesn't support ATIM yet...
83 if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
84 vap->iv_tim_len = howmany(vap->iv_max_aid,8) * sizeof(uint8_t);
85 vap->iv_tim_bitmap = (uint8_t *) malloc(vap->iv_tim_len,
86 M_80211_POWER, M_NOWAIT | M_ZERO);
87 if (vap->iv_tim_bitmap == NULL) {
88 printf("%s: no memory for TIM bitmap!\n", __func__);
89 /* XXX good enough to keep from crashing? */
96 ieee80211_power_vdetach(struct ieee80211vap *vap)
98 if (vap->iv_tim_bitmap != NULL) {
99 free(vap->iv_tim_bitmap, M_80211_POWER);
100 vap->iv_tim_bitmap = NULL;
105 ieee80211_psq_init(struct ieee80211_psq *psq, const char *name)
107 memset(psq, 0, sizeof(*psq));
108 psq->psq_maxlen = IEEE80211_PS_MAX_QUEUE;
109 IEEE80211_PSQ_INIT(psq, name); /* OS-dependent setup */
113 ieee80211_psq_cleanup(struct ieee80211_psq *psq)
116 psq_drain(psq); /* XXX should not be needed? */
118 KASSERT(psq->psq_len == 0, ("%d frames on ps q", psq->psq_len));
120 IEEE80211_PSQ_DESTROY(psq); /* OS-dependent cleanup */
124 * Return the highest priority frame in the ps queue.
127 ieee80211_node_psq_dequeue(struct ieee80211_node *ni, int *qlen)
129 struct ieee80211_psq *psq = &ni->ni_psq;
130 struct ieee80211_psq_head *qhead;
133 IEEE80211_PSQ_LOCK(psq);
134 qhead = &psq->psq_head[0];
136 if ((m = qhead->head) != NULL) {
137 if ((qhead->head = m->m_nextpkt) == NULL)
139 KASSERT(qhead->len > 0, ("qhead len %d", qhead->len));
141 KASSERT(psq->psq_len > 0, ("psq len %d", psq->psq_len));
145 if (m == NULL && qhead == &psq->psq_head[0]) {
146 /* Algol-68 style for loop */
147 qhead = &psq->psq_head[1];
151 *qlen = psq->psq_len;
152 IEEE80211_PSQ_UNLOCK(psq);
157 * Reclaim an mbuf from the ps q. If marked with M_ENCAP
158 * we assume there is a node reference that must be relcaimed.
161 psq_mfree(struct mbuf *m)
163 if (m->m_flags & M_ENCAP) {
164 struct ieee80211_node *ni = (void *) m->m_pkthdr.rcvif;
165 ieee80211_free_node(ni);
172 * Clear any frames queued in the power save queue.
173 * The number of frames that were present is returned.
176 psq_drain(struct ieee80211_psq *psq)
178 struct ieee80211_psq_head *qhead;
182 IEEE80211_PSQ_LOCK(psq);
184 qhead = &psq->psq_head[0];
186 while ((m = qhead->head) != NULL) {
187 qhead->head = m->m_nextpkt;
192 if (qhead == &psq->psq_head[0]) { /* Algol-68 style for loop */
193 qhead = &psq->psq_head[1];
197 IEEE80211_PSQ_UNLOCK(psq);
203 * Clear any frames queued in the power save queue.
204 * The number of frames that were present is returned.
207 ieee80211_node_psq_drain(struct ieee80211_node *ni)
209 return psq_drain(&ni->ni_psq);
213 * Age frames on the power save queue. The aging interval is
214 * 4 times the listen interval specified by the station. This
215 * number is factored into the age calculations when the frame
216 * is placed on the queue. We store ages as time differences
217 * so we can check and/or adjust only the head of the list.
218 * If a frame's age exceeds the threshold then discard it.
219 * The number of frames discarded is returned so the caller
220 * can check if it needs to adjust the tim.
223 ieee80211_node_psq_age(struct ieee80211_node *ni)
225 struct ieee80211_psq *psq = &ni->ni_psq;
228 if (psq->psq_len != 0) {
229 #ifdef IEEE80211_DEBUG
230 struct ieee80211vap *vap = ni->ni_vap;
232 struct ieee80211_psq_head *qhead;
235 IEEE80211_PSQ_LOCK(psq);
236 qhead = &psq->psq_head[0];
238 while ((m = qhead->head) != NULL &&
239 M_AGE_GET(m) < IEEE80211_INACT_WAIT) {
240 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
241 "discard frame, age %u", M_AGE_GET(m));
242 if ((qhead->head = m->m_nextpkt) == NULL)
244 KASSERT(qhead->len > 0, ("qhead len %d", qhead->len));
246 KASSERT(psq->psq_len > 0, ("psq len %d", psq->psq_len));
251 if (qhead == &psq->psq_head[0]) { /* Algol-68 style for loop */
252 qhead = &psq->psq_head[1];
256 M_AGE_SUB(m, IEEE80211_INACT_WAIT);
257 IEEE80211_PSQ_UNLOCK(psq);
259 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
260 "discard %u frames for age", discard);
261 IEEE80211_NODE_STAT_ADD(ni, ps_discard, discard);
267 * Handle a change in the PS station occupancy.
270 ieee80211_update_ps(struct ieee80211vap *vap, int nsta)
273 KASSERT(vap->iv_opmode == IEEE80211_M_HOSTAP ||
274 vap->iv_opmode == IEEE80211_M_IBSS,
275 ("operating mode %u", vap->iv_opmode));
279 * Indicate whether there are frames queued for a station in power-save mode.
282 ieee80211_set_tim(struct ieee80211_node *ni, int set)
284 struct ieee80211vap *vap = ni->ni_vap;
285 struct ieee80211com *ic = ni->ni_ic;
289 KASSERT(vap->iv_opmode == IEEE80211_M_HOSTAP ||
290 vap->iv_opmode == IEEE80211_M_IBSS,
291 ("operating mode %u", vap->iv_opmode));
293 aid = IEEE80211_AID(ni->ni_associd);
294 KASSERT(aid < vap->iv_max_aid,
295 ("bogus aid %u, max %u", aid, vap->iv_max_aid));
298 changed = (set != (isset(vap->iv_tim_bitmap, aid) != 0));
301 setbit(vap->iv_tim_bitmap, aid);
302 vap->iv_ps_pending++;
304 clrbit(vap->iv_tim_bitmap, aid);
305 vap->iv_ps_pending--;
307 /* NB: we know vap is in RUN state so no need to check */
308 vap->iv_update_beacon(vap, IEEE80211_BEACON_TIM);
310 IEEE80211_UNLOCK(ic);
316 * Save an outbound packet for a node in power-save sleep state.
317 * The new packet is placed on the node's saved queue, and the TIM
318 * is changed, if necessary.
321 ieee80211_pwrsave(struct ieee80211_node *ni, struct mbuf *m)
323 struct ieee80211_psq *psq = &ni->ni_psq;
324 struct ieee80211vap *vap = ni->ni_vap;
325 struct ieee80211com *ic = ni->ni_ic;
326 struct ieee80211_psq_head *qhead;
329 IEEE80211_PSQ_LOCK(psq);
330 if (psq->psq_len >= psq->psq_maxlen) {
332 IEEE80211_PSQ_UNLOCK(psq);
333 IEEE80211_NOTE(vap, IEEE80211_MSG_ANY, ni,
334 "pwr save q overflow, drops %d (size %d)",
335 psq->psq_drops, psq->psq_len);
336 #ifdef IEEE80211_DEBUG
337 if (ieee80211_msg_dumppkts(vap))
338 ieee80211_dump_pkt(ni->ni_ic, mtod(m, caddr_t),
345 * Tag the frame with it's expiry time and insert it in
346 * the appropriate queue. The aging interval is 4 times
347 * the listen interval specified by the station. Frames
348 * that sit around too long are reclaimed using this
351 /* TU -> secs. XXX handle overflow? */
352 age = IEEE80211_TU_TO_MS((ni->ni_intval * ic->ic_bintval) << 2) / 1000;
354 * Encapsulated frames go on the high priority queue,
355 * other stuff goes on the low priority queue. We use
356 * this to order frames returned out of the driver
357 * ahead of frames we collect in ieee80211_start.
359 if (m->m_flags & M_ENCAP)
360 qhead = &psq->psq_head[0];
362 qhead = &psq->psq_head[1];
363 if (qhead->tail == NULL) {
368 * Take care to adjust age when inserting the first
369 * frame of a queue and the other queue already has
370 * frames. We need to preserve the age difference
371 * relationship so ieee80211_node_psq_age works.
373 if (qhead == &psq->psq_head[1]) {
374 mh = psq->psq_head[0].head;
378 mh = psq->psq_head[1].head;
380 int nage = M_AGE_GET(mh) - age;
381 /* XXX is clamping to zero good 'nuf? */
382 M_AGE_SET(mh, nage < 0 ? 0 : nage);
386 qhead->tail->m_nextpkt = m;
387 age -= M_AGE_GET(qhead->head);
389 KASSERT(age >= 0, ("age %d", age));
394 qlen = ++(psq->psq_len);
395 IEEE80211_PSQ_UNLOCK(psq);
397 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
398 "save frame with age %d, %u now queued", age, qlen);
400 if (qlen == 1 && vap->iv_set_tim != NULL)
401 vap->iv_set_tim(ni, 1);
407 * Move frames from the ps q to the vap's send queue
408 * and/or the driver's send queue; and kick the start
409 * method for each, as appropriate. Note we're careful
410 * to preserve packet ordering here.
413 pwrsave_flushq(struct ieee80211_node *ni)
415 struct ieee80211_psq *psq = &ni->ni_psq;
416 struct ieee80211com *ic = ni->ni_ic;
417 struct ieee80211vap *vap = ni->ni_vap;
418 struct ieee80211_psq_head *qhead;
419 struct ifnet *parent, *ifp;
420 struct mbuf *parent_q = NULL, *ifp_q = NULL;
423 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
424 "flush ps queue, %u packets queued", psq->psq_len);
426 IEEE80211_PSQ_LOCK(psq);
427 qhead = &psq->psq_head[0]; /* 802.11 frames */
428 if (qhead->head != NULL) {
429 /* XXX could dispatch through vap and check M_ENCAP */
430 parent = vap->iv_ic->ic_ifp;
431 /* XXX need different driver interface */
432 /* XXX bypasses q max and OACTIVE */
433 parent_q = qhead->head;
434 qhead->head = qhead->tail = NULL;
439 qhead = &psq->psq_head[1]; /* 802.3 frames */
440 if (qhead->head != NULL) {
442 /* XXX need different driver interface */
443 /* XXX bypasses q max and OACTIVE */
445 qhead->head = qhead->tail = NULL;
450 IEEE80211_PSQ_UNLOCK(psq);
452 /* NB: do this outside the psq lock */
453 /* XXX packets might get reordered if parent is OACTIVE */
454 /* parent frames, should be encapsulated */
455 if (parent != NULL) {
456 while (parent_q != NULL) {
458 parent_q = m->m_nextpkt;
460 /* must be encapsulated */
461 KASSERT((m->m_flags & M_ENCAP),
462 ("%s: parentq with non-M_ENCAP frame!\n",
465 * For encaped frames, we need to free the node
466 * reference upon failure.
468 if (ieee80211_parent_xmitpkt(ic, m) != 0)
469 ieee80211_free_node(ni);
473 /* VAP frames, aren't encapsulated */
475 while (ifp_q != NULL) {
477 ifp_q = m->m_nextpkt;
479 KASSERT((!(m->m_flags & M_ENCAP)),
480 ("%s: vapq with M_ENCAP frame!\n", __func__));
481 (void) ieee80211_vap_xmitpkt(vap, m);
487 * Handle station power-save state change.
490 ieee80211_node_pwrsave(struct ieee80211_node *ni, int enable)
492 struct ieee80211vap *vap = ni->ni_vap;
497 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) == 0) {
501 ni->ni_flags |= IEEE80211_NODE_PWR_MGT;
502 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
503 "power save mode on, %u sta's in ps mode", vap->iv_ps_sta);
506 vap->iv_update_ps(vap, vap->iv_ps_sta);
508 if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
512 ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
513 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
514 "power save mode off, %u sta's in ps mode", vap->iv_ps_sta);
516 /* NB: order here is intentional so TIM is clear before flush */
517 if (vap->iv_set_tim != NULL)
518 vap->iv_set_tim(ni, 0);
520 /* NB if no sta's in ps, driver should flush mc q */
521 vap->iv_update_ps(vap, vap->iv_ps_sta);
523 if (ni->ni_psq.psq_len != 0)
529 * Handle power-save state change in station mode.
532 ieee80211_sta_pwrsave(struct ieee80211vap *vap, int enable)
534 struct ieee80211_node *ni = vap->iv_bss;
536 if (!((enable != 0) ^ ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) != 0)))
539 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
540 "sta power save mode %s", enable ? "on" : "off");
542 ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
543 ieee80211_send_nulldata(ieee80211_ref_node(ni));
545 * Flush any queued frames; we can do this immediately
546 * because we know they'll be queued behind the null
547 * data frame we send the ap.
548 * XXX can we use a data frame to take us out of ps?
550 if (ni->ni_psq.psq_len != 0)
553 ni->ni_flags |= IEEE80211_NODE_PWR_MGT;
554 ieee80211_send_nulldata(ieee80211_ref_node(ni));