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.
25 * $FreeBSD: head/sys/net80211/ieee80211_power.c 186302 2008-12-18 23:00:09Z sam $
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>
43 #include <net/route.h>
45 #include <netproto/802_11/ieee80211_var.h>
49 static void ieee80211_update_ps(struct ieee80211vap *, int);
50 static int ieee80211_set_tim(struct ieee80211_node *, int);
52 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;
76 ieee80211_power_latevattach(struct ieee80211vap *vap)
79 * Allocate these only if needed. Beware that we
80 * know adhoc mode doesn't support ATIM yet...
82 if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
83 vap->iv_tim_len = howmany(vap->iv_max_aid,8) * sizeof(uint8_t);
84 vap->iv_tim_bitmap = (uint8_t *) kmalloc(vap->iv_tim_len,
85 M_80211_POWER, M_INTWAIT | M_ZERO);
86 if (vap->iv_tim_bitmap == NULL) {
87 kprintf("%s: no memory for TIM bitmap!\n", __func__);
88 /* XXX good enough to keep from crashing? */
95 ieee80211_power_vdetach(struct ieee80211vap *vap)
97 if (vap->iv_tim_bitmap != NULL) {
98 kfree(vap->iv_tim_bitmap, M_80211_POWER);
99 vap->iv_tim_bitmap = NULL;
104 ieee80211_psq_init(struct ieee80211_psq *psq, const char *name)
106 memset(psq, 0, sizeof(psq));
107 psq->psq_maxlen = IEEE80211_PS_MAX_QUEUE;
111 ieee80211_psq_cleanup(struct ieee80211_psq *psq)
114 psq_drain(psq); /* XXX should not be needed? */
116 KASSERT(psq->psq_len == 0, ("%d frames on ps q", psq->psq_len));
121 * Return the highest priority frame in the ps queue.
124 ieee80211_node_psq_dequeue(struct ieee80211_node *ni, int *qlen)
126 struct ieee80211_psq *psq = &ni->ni_psq;
127 struct ieee80211_psq_head *qhead;
130 qhead = &psq->psq_head[0];
132 if ((m = qhead->head) != NULL) {
133 if ((qhead->head = m->m_nextpkt) == NULL)
135 KASSERT(qhead->len > 0, ("qhead len %d", qhead->len));
137 KASSERT(psq->psq_len > 0, ("psq len %d", psq->psq_len));
141 if (m == NULL && qhead == &psq->psq_head[0]) {
142 /* Algol-68 style for loop */
143 qhead = &psq->psq_head[1];
147 *qlen = psq->psq_len;
152 * Reclaim an mbuf from the ps q. If marked with M_ENCAP
153 * we assume there is a node reference that must be relcaimed.
156 psq_mfree(struct mbuf *m)
158 if (m->m_flags & M_ENCAP) {
159 struct ieee80211_node *ni = (void *) m->m_pkthdr.rcvif;
160 ieee80211_free_node(ni);
167 * Clear any frames queued in the power save queue.
168 * The number of frames that were present is returned.
171 psq_drain(struct ieee80211_psq *psq)
173 struct ieee80211_psq_head *qhead;
178 qhead = &psq->psq_head[0];
180 while ((m = qhead->head) != NULL) {
181 qhead->head = m->m_nextpkt;
186 if (qhead == &psq->psq_head[0]) { /* Algol-68 style for loop */
187 qhead = &psq->psq_head[1];
196 * Clear any frames queued in the power save queue.
197 * The number of frames that were present is returned.
200 ieee80211_node_psq_drain(struct ieee80211_node *ni)
202 return psq_drain(&ni->ni_psq);
206 * Age frames on the power save queue. The aging interval is
207 * 4 times the listen interval specified by the station. This
208 * number is factored into the age calculations when the frame
209 * is placed on the queue. We store ages as time differences
210 * so we can check and/or adjust only the head of the list.
211 * If a frame's age exceeds the threshold then discard it.
212 * The number of frames discarded is returned so the caller
213 * can check if it needs to adjust the tim.
216 ieee80211_node_psq_age(struct ieee80211_node *ni)
218 struct ieee80211_psq *psq = &ni->ni_psq;
221 if (psq->psq_len != 0) {
222 #ifdef IEEE80211_DEBUG
223 struct ieee80211vap *vap = ni->ni_vap;
225 struct ieee80211_psq_head *qhead;
228 qhead = &psq->psq_head[0];
230 while ((m = qhead->head) != NULL &&
231 M_AGE_GET(m) < IEEE80211_INACT_WAIT) {
232 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
233 "discard frame, age %u", M_AGE_GET(m));
234 if ((qhead->head = m->m_nextpkt) == NULL)
236 KASSERT(qhead->len > 0, ("qhead len %d", qhead->len));
238 KASSERT(psq->psq_len > 0, ("psq len %d", psq->psq_len));
243 if (qhead == &psq->psq_head[0]) { /* Algol-68 style for loop */
244 qhead = &psq->psq_head[1];
248 M_AGE_SUB(m, IEEE80211_INACT_WAIT);
250 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
251 "discard %u frames for age", discard);
252 IEEE80211_NODE_STAT_ADD(ni, ps_discard, discard);
258 * Handle a change in the PS station occupancy.
261 ieee80211_update_ps(struct ieee80211vap *vap, int nsta)
264 KASSERT(vap->iv_opmode == IEEE80211_M_HOSTAP ||
265 vap->iv_opmode == IEEE80211_M_IBSS,
266 ("operating mode %u", vap->iv_opmode));
270 * Indicate whether there are frames queued for a station in power-save mode.
273 ieee80211_set_tim(struct ieee80211_node *ni, int set)
275 struct ieee80211vap *vap = ni->ni_vap;
276 struct ieee80211com *ic = ni->ni_ic;
281 KASSERT(vap->iv_opmode == IEEE80211_M_HOSTAP ||
282 vap->iv_opmode == IEEE80211_M_IBSS,
283 ("operating mode %u", vap->iv_opmode));
285 aid = IEEE80211_AID(ni->ni_associd);
286 KASSERT(aid < vap->iv_max_aid,
287 ("bogus aid %u, max %u", aid, vap->iv_max_aid));
289 changed = (set != (isset(vap->iv_tim_bitmap, aid) != 0));
292 setbit(vap->iv_tim_bitmap, aid);
293 vap->iv_ps_pending++;
295 clrbit(vap->iv_tim_bitmap, aid);
296 vap->iv_ps_pending--;
298 /* NB: we know vap is in RUN state so no need to check */
299 vap->iv_update_beacon(vap, IEEE80211_BEACON_TIM);
306 * Save an outbound packet for a node in power-save sleep state.
307 * The new packet is placed on the node's saved queue, and the TIM
308 * is changed, if necessary.
311 ieee80211_pwrsave(struct ieee80211_node *ni, struct mbuf *m)
313 struct ieee80211_psq *psq = &ni->ni_psq;
314 struct ieee80211vap *vap = ni->ni_vap;
315 struct ieee80211com *ic = ni->ni_ic;
316 struct ieee80211_psq_head *qhead;
319 if (psq->psq_len >= psq->psq_maxlen) {
321 IEEE80211_NOTE(vap, IEEE80211_MSG_ANY, ni,
322 "pwr save q overflow, drops %d (size %d)",
323 psq->psq_drops, psq->psq_len);
324 #ifdef IEEE80211_DEBUG
325 if (ieee80211_msg_dumppkts(vap))
326 ieee80211_dump_pkt(ni->ni_ic, mtod(m, caddr_t),
333 * Tag the frame with it's expiry time and insert it in
334 * the appropriate queue. The aging interval is 4 times
335 * the listen interval specified by the station. Frames
336 * that sit around too long are reclaimed using this
339 /* TU -> secs. XXX handle overflow? */
340 age = IEEE80211_TU_TO_MS((ni->ni_intval * ic->ic_bintval) << 2) / 1000;
342 * Encapsulated frames go on the high priority queue,
343 * other stuff goes on the low priority queue. We use
344 * this to order frames returned out of the driver
345 * ahead of frames we collect in ieee80211_start.
347 if (m->m_flags & M_ENCAP)
348 qhead = &psq->psq_head[0];
350 qhead = &psq->psq_head[1];
351 if (qhead->tail == NULL) {
356 * Take care to adjust age when inserting the first
357 * frame of a queue and the other queue already has
358 * frames. We need to preserve the age difference
359 * relationship so ieee80211_node_psq_age works.
361 if (qhead == &psq->psq_head[1]) {
362 mh = psq->psq_head[0].head;
366 mh = psq->psq_head[1].head;
368 int nage = M_AGE_GET(mh) - age;
369 /* XXX is clamping to zero good 'nuf? */
370 M_AGE_SET(mh, nage < 0 ? 0 : nage);
374 qhead->tail->m_nextpkt = m;
375 age -= M_AGE_GET(qhead->head);
377 KASSERT(age >= 0, ("age %d", age));
382 qlen = ++(psq->psq_len);
384 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
385 "save frame with age %d, %u now queued", age, qlen);
387 if (qlen == 1 && vap->iv_set_tim != NULL)
388 vap->iv_set_tim(ni, 1);
394 * Move frames from the ps q to the vap's send queue
395 * and/or the driver's send queue; and kick the start
396 * method for each, as appropriate. Note we're careful
397 * to preserve packet ordering here.
400 pwrsave_flushq(struct ieee80211_node *ni)
402 struct ieee80211_psq *psq = &ni->ni_psq;
403 struct ieee80211vap *vap = ni->ni_vap;
404 struct ieee80211_psq_head *qhead;
405 struct ifnet *parent, *ifp;
407 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
408 "flush ps queue, %u packets queued", psq->psq_len);
410 qhead = &psq->psq_head[0]; /* 802.11 frames */
411 if (qhead->head != NULL) {
412 /* XXX could dispatch through vap and check M_ENCAP */
413 parent = vap->iv_ic->ic_ifp;
414 /* XXX need different driver interface */
415 /* XXX bypasses q max and OACTIVE */
416 IF_PREPEND_LIST(&parent->if_snd, qhead->head, qhead->tail,
418 qhead->head = qhead->tail = NULL;
423 qhead = &psq->psq_head[1]; /* 802.3 frames */
424 if (qhead->head != NULL) {
426 /* XXX need different driver interface */
427 /* XXX bypasses q max and OACTIVE */
428 IF_PREPEND_LIST(&ifp->if_snd, qhead->head, qhead->tail,
430 qhead->head = qhead->tail = NULL;
436 /* NB: do this outside the psq lock */
437 /* XXX packets might get reordered if parent is OACTIVE */
439 parent->if_start(parent);
445 * Handle station power-save state change.
448 ieee80211_node_pwrsave(struct ieee80211_node *ni, int enable)
450 struct ieee80211vap *vap = ni->ni_vap;
455 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) == 0) {
459 ni->ni_flags |= IEEE80211_NODE_PWR_MGT;
460 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
461 "power save mode on, %u sta's in ps mode", vap->iv_ps_sta);
464 vap->iv_update_ps(vap, vap->iv_ps_sta);
466 if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
470 ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
471 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
472 "power save mode off, %u sta's in ps mode", vap->iv_ps_sta);
474 /* NB: order here is intentional so TIM is clear before flush */
475 if (vap->iv_set_tim != NULL)
476 vap->iv_set_tim(ni, 0);
478 /* NB if no sta's in ps, driver should flush mc q */
479 vap->iv_update_ps(vap, vap->iv_ps_sta);
481 if (ni->ni_psq.psq_len != 0)
487 * Handle power-save state change in station mode.
490 ieee80211_sta_pwrsave(struct ieee80211vap *vap, int enable)
492 struct ieee80211_node *ni = vap->iv_bss;
494 if (!((enable != 0) ^ ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) != 0)))
497 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
498 "sta power save mode %s", enable ? "on" : "off");
500 ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
501 ieee80211_send_nulldata(ieee80211_ref_node(ni));
503 * Flush any queued frames; we can do this immediately
504 * because we know they'll be queued behind the null
505 * data frame we send the ap.
506 * XXX can we use a data frame to take us out of ps?
508 if (ni->ni_psq.psq_len != 0)
511 ni->ni_flags |= IEEE80211_NODE_PWR_MGT;
512 ieee80211_send_nulldata(ieee80211_ref_node(ni));