xref: /freebsd/sys/net80211/ieee80211_power.c (revision 641a6cfb86023499caafe26a4d821a0b885cf00b)
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
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.
13  *
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.
24  */
25 
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
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>
36 #include <sys/kernel.h>
37 
38 #include <sys/socket.h>
39 
40 #include <net/if.h>
41 #include <net/if_media.h>
42 #include <net/ethernet.h>
43 
44 #include <net80211/ieee80211_var.h>
45 
46 #include <net/bpf.h>
47 
48 static void ieee80211_update_ps(struct ieee80211vap *, int);
49 static int ieee80211_set_tim(struct ieee80211_node *, int);
50 
51 static MALLOC_DEFINE(M_80211_POWER, "80211power", "802.11 power save state");
52 
53 void
54 ieee80211_power_attach(struct ieee80211com *ic)
55 {
56 }
57 
58 void
59 ieee80211_power_detach(struct ieee80211com *ic)
60 {
61 }
62 
63 void
64 ieee80211_power_vattach(struct ieee80211vap *vap)
65 {
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;
71 	}
72 }
73 
74 void
75 ieee80211_power_latevattach(struct ieee80211vap *vap)
76 {
77 	/*
78 	 * Allocate these only if needed.  Beware that we
79 	 * know adhoc mode doesn't support ATIM yet...
80 	 */
81 	if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
82 		vap->iv_tim_len = howmany(vap->iv_max_aid,8) * sizeof(uint8_t);
83 		vap->iv_tim_bitmap = (uint8_t *) malloc(vap->iv_tim_len,
84 			M_80211_POWER, M_NOWAIT | M_ZERO);
85 		if (vap->iv_tim_bitmap == NULL) {
86 			printf("%s: no memory for TIM bitmap!\n", __func__);
87 			/* XXX good enough to keep from crashing? */
88 			vap->iv_tim_len = 0;
89 		}
90 	}
91 }
92 
93 void
94 ieee80211_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 
102 void
103 ieee80211_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 
110 void
111 ieee80211_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 
121 /*
122  * Return the highest priority frame in the ps queue.
123  */
124 struct mbuf *
125 ieee80211_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];
133 again:
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  */
158 static void
159 psq_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.
171  * The number of frames that were present is returned.
172  */
173 static int
174 psq_drain(struct ieee80211_psq *psq)
175 {
176 	struct ieee80211_psq_head *qhead;
177 	struct mbuf *m;
178 	int qlen;
179 
180 	IEEE80211_PSQ_LOCK(psq);
181 	qlen = psq->psq_len;
182 	qhead = &psq->psq_head[0];
183 again:
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);
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  */
204 int
205 ieee80211_node_psq_drain(struct ieee80211_node *ni)
206 {
207 	return psq_drain(&ni->ni_psq);
208 }
209 
210 /*
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  */
220 int
221 ieee80211_node_psq_age(struct ieee80211_node *ni)
222 {
223 	struct ieee80211_psq *psq = &ni->ni_psq;
224 	int discard = 0;
225 
226 	if (psq->psq_len != 0) {
227 #ifdef IEEE80211_DEBUG
228 		struct ieee80211vap *vap = ni->ni_vap;
229 #endif
230 		struct ieee80211_psq_head *qhead;
231 		struct mbuf *m;
232 
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) {
238 			IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
239 			     "discard frame, age %u", M_AGE_GET(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);
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 		}
253 		if (m != NULL)
254 			M_AGE_SUB(m, IEEE80211_INACT_WAIT);
255 		IEEE80211_PSQ_UNLOCK(psq);
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 
264 /*
265  * Handle a change in the PS station occupancy.
266  */
267 static void
268 ieee80211_update_ps(struct ieee80211vap *vap, int nsta)
269 {
270 
271 	KASSERT(vap->iv_opmode == IEEE80211_M_HOSTAP ||
272 		vap->iv_opmode == IEEE80211_M_IBSS,
273 		("operating mode %u", vap->iv_opmode));
274 }
275 
276 /*
277  * Indicate whether there are frames queued for a station in power-save mode.
278  */
279 static int
280 ieee80211_set_tim(struct ieee80211_node *ni, int set)
281 {
282 	struct ieee80211vap *vap = ni->ni_vap;
283 	struct ieee80211com *ic = ni->ni_ic;
284 	uint16_t aid;
285 	int changed;
286 
287 	KASSERT(vap->iv_opmode == IEEE80211_M_HOSTAP ||
288 		vap->iv_opmode == IEEE80211_M_IBSS,
289 		("operating mode %u", vap->iv_opmode));
290 
291 	aid = IEEE80211_AID(ni->ni_associd);
292 	KASSERT(aid < vap->iv_max_aid,
293 		("bogus aid %u, max %u", aid, vap->iv_max_aid));
294 
295 	IEEE80211_LOCK(ic);
296 	changed = (set != (isset(vap->iv_tim_bitmap, aid) != 0));
297 	if (changed) {
298 		if (set) {
299 			setbit(vap->iv_tim_bitmap, aid);
300 			vap->iv_ps_pending++;
301 		} else {
302 			clrbit(vap->iv_tim_bitmap, aid);
303 			vap->iv_ps_pending--;
304 		}
305 		/* NB: we know vap is in RUN state so no need to check */
306 		vap->iv_update_beacon(vap, IEEE80211_BEACON_TIM);
307 	}
308 	IEEE80211_UNLOCK(ic);
309 
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  */
318 int
319 ieee80211_pwrsave(struct ieee80211_node *ni, struct mbuf *m)
320 {
321 	struct ieee80211_psq *psq = &ni->ni_psq;
322 	struct ieee80211vap *vap = ni->ni_vap;
323 	struct ieee80211com *ic = ni->ni_ic;
324 	struct ieee80211_psq_head *qhead;
325 	int qlen, age;
326 
327 	IEEE80211_PSQ_LOCK(psq);
328 	if (psq->psq_len >= psq->psq_maxlen) {
329 		psq->psq_drops++;
330 		IEEE80211_PSQ_UNLOCK(psq);
331 		IEEE80211_NOTE(vap, IEEE80211_MSG_ANY, ni,
332 		    "pwr save q overflow, drops %d (size %d)",
333 		    psq->psq_drops, psq->psq_len);
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
339 		psq_mfree(m);
340 		return ENOSPC;
341 	}
342 	/*
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.
348 	 */
349 	/* TU -> secs.  XXX handle overflow? */
350 	age = IEEE80211_TU_TO_MS((ni->ni_intval * ic->ic_bintval) << 2) / 1000;
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;
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 
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;
402 }
403 
404 /*
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.
409  */
410 static void
411 pwrsave_flushq(struct ieee80211_node *ni)
412 {
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;
417 
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;
426 		/* XXX need different driver interface */
427 		/* XXX bypasses q max and OACTIVE */
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)
454 		if_start(ifp);
455 }
456 
457 /*
458  * Handle station power-save state change.
459  */
460 void
461 ieee80211_node_pwrsave(struct ieee80211_node *ni, int enable)
462 {
463 	struct ieee80211vap *vap = ni->ni_vap;
464 	int update;
465 
466 	update = 0;
467 	if (enable) {
468 		if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) == 0) {
469 			vap->iv_ps_sta++;
470 			update = 1;
471 		}
472 		ni->ni_flags |= IEEE80211_NODE_PWR_MGT;
473 		IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
474 		    "power save mode on, %u sta's in ps mode", vap->iv_ps_sta);
475 
476 		if (update)
477 			vap->iv_update_ps(vap, vap->iv_ps_sta);
478 	} else {
479 		if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
480 			vap->iv_ps_sta--;
481 			update = 1;
482 		}
483 		ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
484 		IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
485 		    "power save mode off, %u sta's in ps mode", vap->iv_ps_sta);
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 		}
494 		if (ni->ni_psq.psq_len != 0)
495 			pwrsave_flushq(ni);
496 	}
497 }
498 
499 /*
500  * Handle power-save state change in station mode.
501  */
502 void
503 ieee80211_sta_pwrsave(struct ieee80211vap *vap, int enable)
504 {
505 	struct ieee80211_node *ni = vap->iv_bss;
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 		 */
521 		if (ni->ni_psq.psq_len != 0)
522 			pwrsave_flushq(ni);
523 	} else {
524 		ni->ni_flags |= IEEE80211_NODE_PWR_MGT;
525 		ieee80211_send_nulldata(ieee80211_ref_node(ni));
526 	}
527 }
528