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_var.h>
42 #include <net/if_media.h>
43 #include <net/ethernet.h>
45 #include <net80211/ieee80211_var.h>
49 static void ieee80211_update_ps(struct ieee80211vap *, int);
50 static int ieee80211_set_tim(struct ieee80211_node *, int);
52 static MALLOC_DEFINE(M_80211_POWER, "80211power", "802.11 power save state");
55 ieee80211_power_attach(struct ieee80211com *ic)
60 ieee80211_power_detach(struct ieee80211com *ic)
65 ieee80211_power_vattach(struct ieee80211vap *vap)
67 if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
68 vap->iv_opmode == IEEE80211_M_IBSS) {
69 /* NB: driver should override */
70 vap->iv_update_ps = ieee80211_update_ps;
71 vap->iv_set_tim = ieee80211_set_tim;
73 vap->iv_node_ps = ieee80211_node_pwrsave;
74 vap->iv_sta_ps = ieee80211_sta_pwrsave;
78 ieee80211_power_latevattach(struct ieee80211vap *vap)
81 * Allocate these only if needed. Beware that we
82 * know adhoc mode doesn't support ATIM yet...
84 if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
85 vap->iv_tim_len = howmany(vap->iv_max_aid,8) * sizeof(uint8_t);
86 vap->iv_tim_bitmap = (uint8_t *) malloc(vap->iv_tim_len,
87 M_80211_POWER, M_NOWAIT | M_ZERO);
88 if (vap->iv_tim_bitmap == NULL) {
89 printf("%s: no memory for TIM bitmap!\n", __func__);
90 /* XXX good enough to keep from crashing? */
97 ieee80211_power_vdetach(struct ieee80211vap *vap)
99 if (vap->iv_tim_bitmap != NULL) {
100 free(vap->iv_tim_bitmap, M_80211_POWER);
101 vap->iv_tim_bitmap = NULL;
106 ieee80211_psq_init(struct ieee80211_psq *psq, const char *name)
108 memset(psq, 0, sizeof(*psq));
109 psq->psq_maxlen = IEEE80211_PS_MAX_QUEUE;
110 IEEE80211_PSQ_INIT(psq, name); /* OS-dependent setup */
114 ieee80211_psq_cleanup(struct ieee80211_psq *psq)
117 psq_drain(psq); /* XXX should not be needed? */
119 KASSERT(psq->psq_len == 0, ("%d frames on ps q", psq->psq_len));
121 IEEE80211_PSQ_DESTROY(psq); /* OS-dependent cleanup */
125 * Return the highest priority frame in the ps queue.
128 ieee80211_node_psq_dequeue(struct ieee80211_node *ni, int *qlen)
130 struct ieee80211_psq *psq = &ni->ni_psq;
131 struct ieee80211_psq_head *qhead;
134 IEEE80211_PSQ_LOCK(psq);
135 qhead = &psq->psq_head[0];
137 if ((m = qhead->head) != NULL) {
138 if ((qhead->head = m->m_nextpkt) == NULL)
140 KASSERT(qhead->len > 0, ("qhead len %d", qhead->len));
142 KASSERT(psq->psq_len > 0, ("psq len %d", psq->psq_len));
146 if (m == NULL && qhead == &psq->psq_head[0]) {
147 /* Algol-68 style for loop */
148 qhead = &psq->psq_head[1];
152 *qlen = psq->psq_len;
153 IEEE80211_PSQ_UNLOCK(psq);
158 * Reclaim an mbuf from the ps q. If marked with M_ENCAP
159 * we assume there is a node reference that must be relcaimed.
162 psq_mfree(struct mbuf *m)
164 if (m->m_flags & M_ENCAP) {
165 struct ieee80211_node *ni = (void *) m->m_pkthdr.rcvif;
166 ieee80211_free_node(ni);
173 * Clear any frames queued in the power save queue.
174 * The number of frames that were present is returned.
177 psq_drain(struct ieee80211_psq *psq)
179 struct ieee80211_psq_head *qhead;
183 IEEE80211_PSQ_LOCK(psq);
185 qhead = &psq->psq_head[0];
187 while ((m = qhead->head) != NULL) {
188 qhead->head = m->m_nextpkt;
193 if (qhead == &psq->psq_head[0]) { /* Algol-68 style for loop */
194 qhead = &psq->psq_head[1];
198 IEEE80211_PSQ_UNLOCK(psq);
204 * Clear any frames queued in the power save queue.
205 * The number of frames that were present is returned.
208 ieee80211_node_psq_drain(struct ieee80211_node *ni)
210 return psq_drain(&ni->ni_psq);
214 * Age frames on the power save queue. The aging interval is
215 * 4 times the listen interval specified by the station. This
216 * number is factored into the age calculations when the frame
217 * is placed on the queue. We store ages as time differences
218 * so we can check and/or adjust only the head of the list.
219 * If a frame's age exceeds the threshold then discard it.
220 * The number of frames discarded is returned so the caller
221 * can check if it needs to adjust the tim.
224 ieee80211_node_psq_age(struct ieee80211_node *ni)
226 struct ieee80211_psq *psq = &ni->ni_psq;
229 if (psq->psq_len != 0) {
230 #ifdef IEEE80211_DEBUG
231 struct ieee80211vap *vap = ni->ni_vap;
233 struct ieee80211_psq_head *qhead;
236 IEEE80211_PSQ_LOCK(psq);
237 qhead = &psq->psq_head[0];
239 while ((m = qhead->head) != NULL &&
240 M_AGE_GET(m) < IEEE80211_INACT_WAIT) {
241 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
242 "discard frame, age %u", M_AGE_GET(m));
243 if ((qhead->head = m->m_nextpkt) == NULL)
245 KASSERT(qhead->len > 0, ("qhead len %d", qhead->len));
247 KASSERT(psq->psq_len > 0, ("psq len %d", psq->psq_len));
252 if (qhead == &psq->psq_head[0]) { /* Algol-68 style for loop */
253 qhead = &psq->psq_head[1];
257 M_AGE_SUB(m, IEEE80211_INACT_WAIT);
258 IEEE80211_PSQ_UNLOCK(psq);
260 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
261 "discard %u frames for age", discard);
262 IEEE80211_NODE_STAT_ADD(ni, ps_discard, discard);
268 * Handle a change in the PS station occupancy.
271 ieee80211_update_ps(struct ieee80211vap *vap, int nsta)
274 KASSERT(vap->iv_opmode == IEEE80211_M_HOSTAP ||
275 vap->iv_opmode == IEEE80211_M_IBSS,
276 ("operating mode %u", vap->iv_opmode));
280 * Indicate whether there are frames queued for a station in power-save mode.
283 ieee80211_set_tim(struct ieee80211_node *ni, int set)
285 struct ieee80211vap *vap = ni->ni_vap;
286 struct ieee80211com *ic = ni->ni_ic;
290 KASSERT(vap->iv_opmode == IEEE80211_M_HOSTAP ||
291 vap->iv_opmode == IEEE80211_M_IBSS,
292 ("operating mode %u", vap->iv_opmode));
294 aid = IEEE80211_AID(ni->ni_associd);
295 KASSERT(aid < vap->iv_max_aid,
296 ("bogus aid %u, max %u", aid, vap->iv_max_aid));
299 changed = (set != (isset(vap->iv_tim_bitmap, aid) != 0));
302 setbit(vap->iv_tim_bitmap, aid);
303 vap->iv_ps_pending++;
305 clrbit(vap->iv_tim_bitmap, aid);
306 vap->iv_ps_pending--;
308 /* NB: we know vap is in RUN state so no need to check */
309 vap->iv_update_beacon(vap, IEEE80211_BEACON_TIM);
311 IEEE80211_UNLOCK(ic);
317 * Save an outbound packet for a node in power-save sleep state.
318 * The new packet is placed on the node's saved queue, and the TIM
319 * is changed, if necessary.
322 ieee80211_pwrsave(struct ieee80211_node *ni, struct mbuf *m)
324 struct ieee80211_psq *psq = &ni->ni_psq;
325 struct ieee80211vap *vap = ni->ni_vap;
326 struct ieee80211com *ic = ni->ni_ic;
327 struct ieee80211_psq_head *qhead;
330 IEEE80211_PSQ_LOCK(psq);
331 if (psq->psq_len >= psq->psq_maxlen) {
333 IEEE80211_PSQ_UNLOCK(psq);
334 IEEE80211_NOTE(vap, IEEE80211_MSG_ANY, ni,
335 "pwr save q overflow, drops %d (size %d)",
336 psq->psq_drops, psq->psq_len);
337 #ifdef IEEE80211_DEBUG
338 if (ieee80211_msg_dumppkts(vap))
339 ieee80211_dump_pkt(ni->ni_ic, mtod(m, caddr_t),
346 * Tag the frame with it's expiry time and insert it in
347 * the appropriate queue. The aging interval is 4 times
348 * the listen interval specified by the station. Frames
349 * that sit around too long are reclaimed using this
352 /* TU -> secs. XXX handle overflow? */
353 age = IEEE80211_TU_TO_MS((ni->ni_intval * ic->ic_bintval) << 2) / 1000;
355 * Encapsulated frames go on the high priority queue,
356 * other stuff goes on the low priority queue. We use
357 * this to order frames returned out of the driver
358 * ahead of frames we collect in ieee80211_start.
360 if (m->m_flags & M_ENCAP)
361 qhead = &psq->psq_head[0];
363 qhead = &psq->psq_head[1];
364 if (qhead->tail == NULL) {
369 * Take care to adjust age when inserting the first
370 * frame of a queue and the other queue already has
371 * frames. We need to preserve the age difference
372 * relationship so ieee80211_node_psq_age works.
374 if (qhead == &psq->psq_head[1]) {
375 mh = psq->psq_head[0].head;
379 mh = psq->psq_head[1].head;
381 int nage = M_AGE_GET(mh) - age;
382 /* XXX is clamping to zero good 'nuf? */
383 M_AGE_SET(mh, nage < 0 ? 0 : nage);
387 qhead->tail->m_nextpkt = m;
388 age -= M_AGE_GET(qhead->head);
390 KASSERT(age >= 0, ("age %d", age));
395 qlen = ++(psq->psq_len);
396 IEEE80211_PSQ_UNLOCK(psq);
398 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
399 "save frame with age %d, %u now queued", age, qlen);
401 if (qlen == 1 && vap->iv_set_tim != NULL)
402 vap->iv_set_tim(ni, 1);
408 * Move frames from the ps q to the vap's send queue
409 * and/or the driver's send queue; and kick the start
410 * method for each, as appropriate. Note we're careful
411 * to preserve packet ordering here.
414 pwrsave_flushq(struct ieee80211_node *ni)
416 struct ieee80211_psq *psq = &ni->ni_psq;
417 struct ieee80211com *ic = ni->ni_ic;
418 struct ieee80211vap *vap = ni->ni_vap;
419 struct ieee80211_psq_head *qhead;
420 struct ifnet *parent, *ifp;
421 struct mbuf *parent_q = NULL, *ifp_q = NULL;
424 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
425 "flush ps queue, %u packets queued", psq->psq_len);
427 IEEE80211_PSQ_LOCK(psq);
428 qhead = &psq->psq_head[0]; /* 802.11 frames */
429 if (qhead->head != NULL) {
430 /* XXX could dispatch through vap and check M_ENCAP */
431 parent = vap->iv_ic->ic_ifp;
432 /* XXX need different driver interface */
433 /* XXX bypasses q max and OACTIVE */
434 parent_q = qhead->head;
435 qhead->head = qhead->tail = NULL;
440 qhead = &psq->psq_head[1]; /* 802.3 frames */
441 if (qhead->head != NULL) {
443 /* XXX need different driver interface */
444 /* XXX bypasses q max and OACTIVE */
446 qhead->head = qhead->tail = NULL;
451 IEEE80211_PSQ_UNLOCK(psq);
453 /* NB: do this outside the psq lock */
454 /* XXX packets might get reordered if parent is OACTIVE */
455 /* parent frames, should be encapsulated */
456 if (parent != NULL) {
457 while (parent_q != NULL) {
459 parent_q = m->m_nextpkt;
461 /* must be encapsulated */
462 KASSERT((m->m_flags & M_ENCAP),
463 ("%s: parentq with non-M_ENCAP frame!\n",
466 * For encaped frames, we need to free the node
467 * reference upon failure.
469 if (ieee80211_parent_xmitpkt(ic, m) != 0)
470 ieee80211_free_node(ni);
474 /* VAP frames, aren't encapsulated */
476 while (ifp_q != NULL) {
478 ifp_q = m->m_nextpkt;
480 KASSERT((!(m->m_flags & M_ENCAP)),
481 ("%s: vapq with M_ENCAP frame!\n", __func__));
482 (void) ieee80211_vap_xmitpkt(vap, m);
488 * Handle station power-save state change.
491 ieee80211_node_pwrsave(struct ieee80211_node *ni, int enable)
493 struct ieee80211vap *vap = ni->ni_vap;
498 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) == 0) {
502 ni->ni_flags |= IEEE80211_NODE_PWR_MGT;
503 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
504 "power save mode on, %u sta's in ps mode", vap->iv_ps_sta);
507 vap->iv_update_ps(vap, vap->iv_ps_sta);
509 if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
513 ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
514 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
515 "power save mode off, %u sta's in ps mode", vap->iv_ps_sta);
517 /* NB: order here is intentional so TIM is clear before flush */
518 if (vap->iv_set_tim != NULL)
519 vap->iv_set_tim(ni, 0);
521 /* NB if no sta's in ps, driver should flush mc q */
522 vap->iv_update_ps(vap, vap->iv_ps_sta);
524 if (ni->ni_psq.psq_len != 0)
530 * Handle power-save state change in station mode.
533 ieee80211_sta_pwrsave(struct ieee80211vap *vap, int enable)
535 struct ieee80211_node *ni = vap->iv_bss;
537 if (!((enable != 0) ^ ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) != 0)))
540 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
541 "sta power save mode %s", enable ? "on" : "off");
543 ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
544 ieee80211_send_nulldata(ieee80211_ref_node(ni));
546 * Flush any queued frames; we can do this immediately
547 * because we know they'll be queued behind the null
548 * data frame we send the ap.
549 * XXX can we use a data frame to take us out of ps?
551 if (ni->ni_psq.psq_len != 0)
554 ni->ni_flags |= IEEE80211_NODE_PWR_MGT;
555 ieee80211_send_nulldata(ieee80211_ref_node(ni));