Deleted Added
full compact
ieee80211_power.c (184210) ieee80211_power.c (184288)
1/*-
2 * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 10 unchanged lines hidden (view full) ---

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.
24 */
25
26#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 10 unchanged lines hidden (view full) ---

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.
24 */
25
26#include <sys/cdefs.h>
27__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_power.c 184210 2008-10-23 19:57:13Z des $");
27__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_power.c 184288 2008-10-26 01:04:46Z sam $");
28
29/*
30 * IEEE 802.11 power save support.
31 */
32#include "opt_wlan.h"
33
34#include <sys/param.h>
35#include <sys/systm.h>

--- 58 unchanged lines hidden (view full) ---

94ieee80211_power_vdetach(struct ieee80211vap *vap)
95{
96 if (vap->iv_tim_bitmap != NULL) {
97 FREE(vap->iv_tim_bitmap, M_80211_POWER);
98 vap->iv_tim_bitmap = NULL;
99 }
100}
101
28
29/*
30 * IEEE 802.11 power save support.
31 */
32#include "opt_wlan.h"
33
34#include <sys/param.h>
35#include <sys/systm.h>

--- 58 unchanged lines hidden (view full) ---

94ieee80211_power_vdetach(struct ieee80211vap *vap)
95{
96 if (vap->iv_tim_bitmap != NULL) {
97 FREE(vap->iv_tim_bitmap, M_80211_POWER);
98 vap->iv_tim_bitmap = NULL;
99 }
100}
101
102void
103ieee80211_psq_init(struct ieee80211_psq *psq, const char *name)
104{
105 memset(psq, 0, sizeof(psq));
106 psq->psq_maxlen = IEEE80211_PS_MAX_QUEUE;
107 IEEE80211_PSQ_INIT(psq, name); /* OS-dependent setup */
108}
109
110void
111ieee80211_psq_cleanup(struct ieee80211_psq *psq)
112{
113#if 0
114 psq_drain(psq); /* XXX should not be needed? */
115#else
116 KASSERT(psq->psq_len == 0, ("%d frames on ps q", psq->psq_len));
117#endif
118 IEEE80211_PSQ_DESTROY(psq); /* OS-dependent cleanup */
119}
120
102/*
121/*
103 * Clear any frames queued on a node's power save queue.
122 * Return the highest priority frame in the ps queue.
123 */
124struct mbuf *
125ieee80211_node_psq_dequeue(struct ieee80211_node *ni, int *qlen)
126{
127 struct ieee80211_psq *psq = &ni->ni_psq;
128 struct ieee80211_psq_head *qhead;
129 struct mbuf *m;
130
131 IEEE80211_PSQ_LOCK(psq);
132 qhead = &psq->psq_head[0];
133again:
134 if ((m = qhead->head) != NULL) {
135 if ((qhead->head = m->m_nextpkt) == NULL)
136 qhead->tail = NULL;
137 KASSERT(qhead->len > 0, ("qhead len %d", qhead->len));
138 qhead->len--;
139 KASSERT(psq->psq_len > 0, ("psq len %d", psq->psq_len));
140 psq->psq_len--;
141 m->m_nextpkt = NULL;
142 }
143 if (m == NULL && qhead == &psq->psq_head[0]) {
144 /* Algol-68 style for loop */
145 qhead = &psq->psq_head[1];
146 goto again;
147 }
148 if (qlen != NULL)
149 *qlen = psq->psq_len;
150 IEEE80211_PSQ_UNLOCK(psq);
151 return m;
152}
153
154/*
155 * Reclaim an mbuf from the ps q. If marked with M_ENCAP
156 * we assume there is a node reference that must be relcaimed.
157 */
158static void
159psq_mfree(struct mbuf *m)
160{
161 if (m->m_flags & M_ENCAP) {
162 struct ieee80211_node *ni = (void *) m->m_pkthdr.rcvif;
163 ieee80211_free_node(ni);
164 }
165 m->m_nextpkt = NULL;
166 m_freem(m);
167}
168
169/*
170 * Clear any frames queued in the power save queue.
104 * The number of frames that were present is returned.
105 */
171 * The number of frames that were present is returned.
172 */
106int
107ieee80211_node_saveq_drain(struct ieee80211_node *ni)
173static int
174psq_drain(struct ieee80211_psq *psq)
108{
175{
176 struct ieee80211_psq_head *qhead;
177 struct mbuf *m;
109 int qlen;
110
178 int qlen;
179
111 IEEE80211_NODE_SAVEQ_LOCK(ni);
112 qlen = IEEE80211_NODE_SAVEQ_QLEN(ni);
113 _IF_DRAIN(&ni->ni_savedq);
114 IEEE80211_NODE_SAVEQ_UNLOCK(ni);
180 IEEE80211_PSQ_LOCK(psq);
181 qlen = psq->psq_len;
182 qhead = &psq->psq_head[0];
183again:
184 while ((m = qhead->head) != NULL) {
185 qhead->head = m->m_nextpkt;
186 psq_mfree(m);
187 }
188 qhead->tail = NULL;
189 qhead->len = 0;
190 if (qhead == &psq->psq_head[0]) { /* Algol-68 style for loop */
191 qhead = &psq->psq_head[1];
192 goto again;
193 }
194 psq->psq_len = 0;
195 IEEE80211_PSQ_UNLOCK(psq);
115
116 return qlen;
117}
118
119/*
196
197 return qlen;
198}
199
200/*
201 * Clear any frames queued in the power save queue.
202 * The number of frames that were present is returned.
203 */
204int
205ieee80211_node_psq_drain(struct ieee80211_node *ni)
206{
207 return psq_drain(&ni->ni_psq);
208}
209
210/*
120 * Age frames on the power save queue. The aging interval is
121 * 4 times the listen interval specified by the station. This
122 * number is factored into the age calculations when the frame
123 * is placed on the queue. We store ages as time differences
124 * so we can check and/or adjust only the head of the list.
125 * If a frame's age exceeds the threshold then discard it.
126 * The number of frames discarded is returned so the caller
127 * can check if it needs to adjust the tim.
128 */
129int
211 * Age frames on the power save queue. The aging interval is
212 * 4 times the listen interval specified by the station. This
213 * number is factored into the age calculations when the frame
214 * is placed on the queue. We store ages as time differences
215 * so we can check and/or adjust only the head of the list.
216 * If a frame's age exceeds the threshold then discard it.
217 * The number of frames discarded is returned so the caller
218 * can check if it needs to adjust the tim.
219 */
220int
130ieee80211_node_saveq_age(struct ieee80211_node *ni)
221ieee80211_node_psq_age(struct ieee80211_node *ni)
131{
222{
223 struct ieee80211_psq *psq = &ni->ni_psq;
132 int discard = 0;
133
224 int discard = 0;
225
134 if (IEEE80211_NODE_SAVEQ_QLEN(ni) != 0) {
226 if (psq->psq_len != 0) {
135#ifdef IEEE80211_DEBUG
136 struct ieee80211vap *vap = ni->ni_vap;
137#endif
227#ifdef IEEE80211_DEBUG
228 struct ieee80211vap *vap = ni->ni_vap;
229#endif
230 struct ieee80211_psq_head *qhead;
138 struct mbuf *m;
139
231 struct mbuf *m;
232
140 IEEE80211_NODE_SAVEQ_LOCK(ni);
141 while (IF_POLL(&ni->ni_savedq, m) != NULL &&
142 M_AGE_GET(m) < IEEE80211_INACT_WAIT) {
233 IEEE80211_PSQ_LOCK(psq);
234 qhead = &psq->psq_head[0];
235 again:
236 while ((m = qhead->head) != NULL &&
237 M_AGE_GET(m) < IEEE80211_INACT_WAIT) {
143 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
144 "discard frame, age %u", M_AGE_GET(m));
238 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
239 "discard frame, age %u", M_AGE_GET(m));
145 _IEEE80211_NODE_SAVEQ_DEQUEUE_HEAD(ni, m);
146 m_freem(m);
240 if ((qhead->head = m->m_nextpkt) == NULL)
241 qhead->tail = NULL;
242 KASSERT(qhead->len > 0, ("qhead len %d", qhead->len));
243 qhead->len--;
244 KASSERT(psq->psq_len > 0, ("psq len %d", psq->psq_len));
245 psq->psq_len--;
246 psq_mfree(m);
147 discard++;
148 }
247 discard++;
248 }
249 if (qhead == &psq->psq_head[0]) { /* Algol-68 style for loop */
250 qhead = &psq->psq_head[1];
251 goto again;
252 }
149 if (m != NULL)
150 M_AGE_SUB(m, IEEE80211_INACT_WAIT);
253 if (m != NULL)
254 M_AGE_SUB(m, IEEE80211_INACT_WAIT);
151 IEEE80211_NODE_SAVEQ_UNLOCK(ni);
255 IEEE80211_PSQ_UNLOCK(psq);
152
153 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
154 "discard %u frames for age", discard);
155 IEEE80211_NODE_STAT_ADD(ni, ps_discard, discard);
156 }
157 return discard;
158}
159

--- 46 unchanged lines hidden (view full) ---

206 return changed;
207}
208
209/*
210 * Save an outbound packet for a node in power-save sleep state.
211 * The new packet is placed on the node's saved queue, and the TIM
212 * is changed, if necessary.
213 */
256
257 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
258 "discard %u frames for age", discard);
259 IEEE80211_NODE_STAT_ADD(ni, ps_discard, discard);
260 }
261 return discard;
262}
263

--- 46 unchanged lines hidden (view full) ---

310 return changed;
311}
312
313/*
314 * Save an outbound packet for a node in power-save sleep state.
315 * The new packet is placed on the node's saved queue, and the TIM
316 * is changed, if necessary.
317 */
214void
318int
215ieee80211_pwrsave(struct ieee80211_node *ni, struct mbuf *m)
216{
319ieee80211_pwrsave(struct ieee80211_node *ni, struct mbuf *m)
320{
321 struct ieee80211_psq *psq = &ni->ni_psq;
217 struct ieee80211vap *vap = ni->ni_vap;
218 struct ieee80211com *ic = ni->ni_ic;
322 struct ieee80211vap *vap = ni->ni_vap;
323 struct ieee80211com *ic = ni->ni_ic;
324 struct ieee80211_psq_head *qhead;
219 int qlen, age;
220
325 int qlen, age;
326
221 IEEE80211_NODE_SAVEQ_LOCK(ni);
222 if (_IF_QFULL(&ni->ni_savedq)) {
223 _IF_DROP(&ni->ni_savedq);
224 IEEE80211_NODE_SAVEQ_UNLOCK(ni);
327 IEEE80211_PSQ_LOCK(psq);
328 if (psq->psq_len >= psq->psq_maxlen) {
329 psq->psq_drops++;
330 IEEE80211_PSQ_UNLOCK(psq);
225 IEEE80211_NOTE(vap, IEEE80211_MSG_ANY, ni,
226 "pwr save q overflow, drops %d (size %d)",
331 IEEE80211_NOTE(vap, IEEE80211_MSG_ANY, ni,
332 "pwr save q overflow, drops %d (size %d)",
227 ni->ni_savedq.ifq_drops, IEEE80211_PS_MAX_QUEUE);
333 psq->psq_drops, psq->psq_len);
228#ifdef IEEE80211_DEBUG
229 if (ieee80211_msg_dumppkts(vap))
230 ieee80211_dump_pkt(ni->ni_ic, mtod(m, caddr_t),
231 m->m_len, -1, -1);
232#endif
334#ifdef IEEE80211_DEBUG
335 if (ieee80211_msg_dumppkts(vap))
336 ieee80211_dump_pkt(ni->ni_ic, mtod(m, caddr_t),
337 m->m_len, -1, -1);
338#endif
233 m_freem(m);
234 return;
339 psq_mfree(m);
340 return ENOSPC;
235 }
236 /*
341 }
342 /*
237 * Tag the frame with it's expiry time and insert
238 * it in the queue. The aging interval is 4 times
239 * the listen interval specified by the station.
240 * Frames that sit around too long are reclaimed
241 * using this information.
343 * Tag the frame with it's expiry time and insert it in
344 * the appropriate queue. The aging interval is 4 times
345 * the listen interval specified by the station. Frames
346 * that sit around too long are reclaimed using this
347 * information.
242 */
243 /* TU -> secs. XXX handle overflow? */
244 age = IEEE80211_TU_TO_MS((ni->ni_intval * ic->ic_bintval) << 2) / 1000;
348 */
349 /* TU -> secs. XXX handle overflow? */
350 age = IEEE80211_TU_TO_MS((ni->ni_intval * ic->ic_bintval) << 2) / 1000;
245 _IEEE80211_NODE_SAVEQ_ENQUEUE(ni, m, qlen, age);
246 IEEE80211_NODE_SAVEQ_UNLOCK(ni);
351 /*
352 * Encapsulated frames go on the high priority queue,
353 * other stuff goes on the low priority queue. We use
354 * this to order frames returned out of the driver
355 * ahead of frames we collect in ieee80211_start.
356 */
357 if (m->m_flags & M_ENCAP)
358 qhead = &psq->psq_head[0];
359 else
360 qhead = &psq->psq_head[1];
361 if (qhead->tail == NULL) {
362 struct mbuf *mh;
247
363
364 qhead->head = m;
365 /*
366 * Take care to adjust age when inserting the first
367 * frame of a queue and the other queue already has
368 * frames. We need to preserve the age difference
369 * relationship so ieee80211_node_psq_age works.
370 */
371 if (qhead == &psq->psq_head[1]) {
372 mh = psq->psq_head[0].head;
373 if (mh != NULL)
374 age-= M_AGE_GET(mh);
375 } else {
376 mh = psq->psq_head[1].head;
377 if (mh != NULL) {
378 int nage = M_AGE_GET(mh) - age;
379 /* XXX is clamping to zero good 'nuf? */
380 M_AGE_SET(mh, nage < 0 ? 0 : nage);
381 }
382 }
383 } else {
384 qhead->tail->m_nextpkt = m;
385 age -= M_AGE_GET(qhead->head);
386 }
387 KASSERT(age >= 0, ("age %d", age));
388 M_AGE_SET(m, age);
389 m->m_nextpkt = NULL;
390 qhead->tail = m;
391 qhead->len++;
392 qlen = ++(psq->psq_len);
393 IEEE80211_PSQ_UNLOCK(psq);
394
248 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
249 "save frame with age %d, %u now queued", age, qlen);
250
251 if (qlen == 1 && vap->iv_set_tim != NULL)
252 vap->iv_set_tim(ni, 1);
395 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
396 "save frame with age %d, %u now queued", age, qlen);
397
398 if (qlen == 1 && vap->iv_set_tim != NULL)
399 vap->iv_set_tim(ni, 1);
400
401 return 0;
253}
254
255/*
402}
403
404/*
256 * Unload the frames from the ps q but don't send them
257 * to the driver yet. We do this in two stages to minimize
258 * locking but also because there's no easy way to preserve
259 * ordering given the existing ifnet access mechanisms.
260 * XXX could be optimized
405 * Move frames from the ps q to the vap's send queue
406 * and/or the driver's send queue; and kick the start
407 * method for each, as appropriate. Note we're careful
408 * to preserve packet ordering here.
261 */
262static void
263pwrsave_flushq(struct ieee80211_node *ni)
264{
409 */
410static void
411pwrsave_flushq(struct ieee80211_node *ni)
412{
265 struct mbuf *m, *mhead, *mtail;
266 int mcount;
413 struct ieee80211_psq *psq = &ni->ni_psq;
414 struct ieee80211vap *vap = ni->ni_vap;
415 struct ieee80211_psq_head *qhead;
416 struct ifnet *parent, *ifp;
267
417
268 IEEE80211_NODE_SAVEQ_LOCK(ni);
269 mcount = IEEE80211_NODE_SAVEQ_QLEN(ni);
270 mhead = mtail = NULL;
271 for (;;) {
272 _IEEE80211_NODE_SAVEQ_DEQUEUE_HEAD(ni, m);
273 if (m == NULL)
274 break;
275 if (mhead == NULL) {
276 mhead = m;
277 m->m_nextpkt = NULL;
278 } else
279 mtail->m_nextpkt = m;
280 mtail = m;
281 }
282 IEEE80211_NODE_SAVEQ_UNLOCK(ni);
283 if (mhead != NULL) {
418 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
419 "flush ps queue, %u packets queued", psq->psq_len);
420
421 IEEE80211_PSQ_LOCK(psq);
422 qhead = &psq->psq_head[0]; /* 802.11 frames */
423 if (qhead->head != NULL) {
424 /* XXX could dispatch through vap and check M_ENCAP */
425 parent = vap->iv_ic->ic_ifp;
284 /* XXX need different driver interface */
285 /* XXX bypasses q max and OACTIVE */
426 /* XXX need different driver interface */
427 /* XXX bypasses q max and OACTIVE */
286 struct ifnet *ifp = ni->ni_vap->iv_ifp;
287 IF_PREPEND_LIST(&ifp->if_snd, mhead, mtail, mcount);
428 IF_PREPEND_LIST(&parent->if_snd, qhead->head, qhead->tail,
429 qhead->len);
430 qhead->head = qhead->tail = NULL;
431 qhead->len = 0;
432 } else
433 parent = NULL;
434
435 qhead = &psq->psq_head[1]; /* 802.3 frames */
436 if (qhead->head != NULL) {
437 ifp = vap->iv_ifp;
438 /* XXX need different driver interface */
439 /* XXX bypasses q max and OACTIVE */
440 IF_PREPEND_LIST(&ifp->if_snd, qhead->head, qhead->tail,
441 qhead->len);
442 qhead->head = qhead->tail = NULL;
443 qhead->len = 0;
444 } else
445 ifp = NULL;
446 psq->psq_len = 0;
447 IEEE80211_PSQ_UNLOCK(psq);
448
449 /* NB: do this outside the psq lock */
450 /* XXX packets might get reordered if parent is OACTIVE */
451 if (parent != NULL)
452 if_start(parent);
453 if (ifp != NULL)
288 if_start(ifp);
454 if_start(ifp);
289 }
290}
291
292/*
293 * Handle station power-save state change.
294 */
295void
296ieee80211_node_pwrsave(struct ieee80211_node *ni, int enable)
297{

--- 23 unchanged lines hidden (view full) ---

321
322 /* NB: order here is intentional so TIM is clear before flush */
323 if (vap->iv_set_tim != NULL)
324 vap->iv_set_tim(ni, 0);
325 if (update) {
326 /* NB if no sta's in ps, driver should flush mc q */
327 vap->iv_update_ps(vap, vap->iv_ps_sta);
328 }
455}
456
457/*
458 * Handle station power-save state change.
459 */
460void
461ieee80211_node_pwrsave(struct ieee80211_node *ni, int enable)
462{

--- 23 unchanged lines hidden (view full) ---

486
487 /* NB: order here is intentional so TIM is clear before flush */
488 if (vap->iv_set_tim != NULL)
489 vap->iv_set_tim(ni, 0);
490 if (update) {
491 /* NB if no sta's in ps, driver should flush mc q */
492 vap->iv_update_ps(vap, vap->iv_ps_sta);
493 }
329 pwrsave_flushq(ni);
494 if (ni->ni_psq.psq_len != 0)
495 pwrsave_flushq(ni);
330 }
331}
332
333/*
334 * Handle power-save state change in station mode.
335 */
336void
337ieee80211_sta_pwrsave(struct ieee80211vap *vap, int enable)
338{
339 struct ieee80211_node *ni = vap->iv_bss;
496 }
497}
498
499/*
500 * Handle power-save state change in station mode.
501 */
502void
503ieee80211_sta_pwrsave(struct ieee80211vap *vap, int enable)
504{
505 struct ieee80211_node *ni = vap->iv_bss;
340 int qlen;
341
342 if (!((enable != 0) ^ ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) != 0)))
343 return;
344
345 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
346 "sta power save mode %s", enable ? "on" : "off");
347 if (!enable) {
348 ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
349 ieee80211_send_nulldata(ieee80211_ref_node(ni));
350 /*
351 * Flush any queued frames; we can do this immediately
352 * because we know they'll be queued behind the null
353 * data frame we send the ap.
354 * XXX can we use a data frame to take us out of ps?
355 */
506
507 if (!((enable != 0) ^ ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) != 0)))
508 return;
509
510 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
511 "sta power save mode %s", enable ? "on" : "off");
512 if (!enable) {
513 ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
514 ieee80211_send_nulldata(ieee80211_ref_node(ni));
515 /*
516 * Flush any queued frames; we can do this immediately
517 * because we know they'll be queued behind the null
518 * data frame we send the ap.
519 * XXX can we use a data frame to take us out of ps?
520 */
356 qlen = IEEE80211_NODE_SAVEQ_QLEN(ni);
357 if (qlen != 0) {
358 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
359 "flush ps queue, %u packets queued", qlen);
521 if (ni->ni_psq.psq_len != 0)
360 pwrsave_flushq(ni);
522 pwrsave_flushq(ni);
361 }
362 } else {
363 ni->ni_flags |= IEEE80211_NODE_PWR_MGT;
364 ieee80211_send_nulldata(ieee80211_ref_node(ni));
365 }
366}
523 } else {
524 ni->ni_flags |= IEEE80211_NODE_PWR_MGT;
525 ieee80211_send_nulldata(ieee80211_ref_node(ni));
526 }
527}