1 /*-
2 * Copyright (c) 2002-2007 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: releng/7.4/sys/net80211/ieee80211_power.c 187739 2009-01-26 20:24:04Z dwhite $");
28
29 /*
30 * IEEE 802.11 power save support.
31 */
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35
36 #include <sys/socket.h>
37
38 #include <net/if.h>
39 #include <net/if_media.h>
40 #include <net/ethernet.h>
41
42 #include <net80211/ieee80211_var.h>
43
44 #include <net/bpf.h>
45
46 static void ieee80211_set_tim(struct ieee80211_node *ni, int set);
47
48 void
49 ieee80211_power_attach(struct ieee80211com *ic)
50 {
51 /* NB: driver should override */
52 ic->ic_set_tim = ieee80211_set_tim;
53 }
54
55 void
56 ieee80211_power_lateattach(struct ieee80211com *ic)
57 {
58 ic->ic_tim_len = howmany(ic->ic_max_aid,8) * sizeof(uint8_t);
59 MALLOC(ic->ic_tim_bitmap, uint8_t *, ic->ic_tim_len,
60 M_DEVBUF, M_NOWAIT | M_ZERO);
61 if (ic->ic_tim_bitmap == NULL) {
62 printf("%s: no memory for TIM bitmap!\n", __func__);
63 /* XXX good enough to keep from crashing? */
64 ic->ic_tim_len = 0;
65 }
66 }
67
68 void
69 ieee80211_power_detach(struct ieee80211com *ic)
70 {
71 if (ic->ic_tim_bitmap != NULL) {
72 FREE(ic->ic_tim_bitmap, M_DEVBUF);
73 ic->ic_tim_bitmap = NULL;
74 }
75 }
76
77 /*
78 * Clear any frames queued on a node's power save queue.
79 * The number of frames that were present is returned.
80 */
81 int
82 ieee80211_node_saveq_drain(struct ieee80211_node *ni)
83 {
84 int qlen;
85
86 IEEE80211_NODE_SAVEQ_LOCK(ni);
87 qlen = IEEE80211_NODE_SAVEQ_QLEN(ni);
88 _IF_DRAIN(&ni->ni_savedq);
89 IEEE80211_NODE_SAVEQ_UNLOCK(ni);
90
91 return qlen;
92 }
93
94 /*
95 * Age frames on the power save queue. The aging interval is
96 * 4 times the listen interval specified by the station. This
97 * number is factored into the age calculations when the frame
98 * is placed on the queue. We store ages as time differences
99 * so we can check and/or adjust only the head of the list.
100 * If a frame's age exceeds the threshold then discard it.
101 * The number of frames discarded is returned so the caller
102 * can check if it needs to adjust the tim.
103 */
104 int
105 ieee80211_node_saveq_age(struct ieee80211_node *ni)
106 {
107 int discard = 0;
108
109 if (IEEE80211_NODE_SAVEQ_QLEN(ni) != 0) {
110 struct mbuf *m;
111
112 IEEE80211_NODE_SAVEQ_LOCK(ni);
113 while (IF_POLL(&ni->ni_savedq, m) != NULL &&
114 M_AGE_GET(m) < IEEE80211_INACT_WAIT) {
115 IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_POWER, "[%s] discard frame, age %u\n", ether_sprintf(ni->ni_macaddr), M_AGE_GET(m));/*XXX*/
116 _IEEE80211_NODE_SAVEQ_DEQUEUE_HEAD(ni, m);
117 m_freem(m);
118 discard++;
119 }
120 if (m != NULL)
121 M_AGE_SUB(m, IEEE80211_INACT_WAIT);
122 IEEE80211_NODE_SAVEQ_UNLOCK(ni);
123
124 IEEE80211_NOTE(ni->ni_ic, IEEE80211_MSG_POWER, ni,
125 "discard %u frames for age", discard);
126 IEEE80211_NODE_STAT_ADD(ni, ps_discard, discard);
127 }
128 return discard;
129 }
130
131 /*
132 * Indicate whether there are frames queued for a station in power-save mode.
133 */
134 static void
135 ieee80211_set_tim(struct ieee80211_node *ni, int set)
136 {
137 struct ieee80211com *ic = ni->ni_ic;
138 uint16_t aid;
139
140 KASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP ||
141 ic->ic_opmode == IEEE80211_M_IBSS,
142 ("operating mode %u", ic->ic_opmode));
143
144 aid = IEEE80211_AID(ni->ni_associd);
145 KASSERT(aid < ic->ic_max_aid,
146 ("bogus aid %u, max %u", aid, ic->ic_max_aid));
147
148 IEEE80211_BEACON_LOCK(ic);
149 if (set != (isset(ic->ic_tim_bitmap, aid) != 0)) {
150 if (set) {
151 setbit(ic->ic_tim_bitmap, aid);
152 ic->ic_ps_pending++;
153 } else {
154 clrbit(ic->ic_tim_bitmap, aid);
155 ic->ic_ps_pending--;
156 }
157 /* NB: we know ic is in RUN state so no need to check */
158 ic->ic_update_beacon(ic, IEEE80211_BEACON_TIM);
159 }
160 IEEE80211_BEACON_UNLOCK(ic);
161 }
162
163 /*
164 * Save an outbound packet for a node in power-save sleep state.
165 * The new packet is placed on the node's saved queue, and the TIM
166 * is changed, if necessary.
167 */
168 void
169 ieee80211_pwrsave(struct ieee80211_node *ni, struct mbuf *m)
170 {
171 struct ieee80211com *ic = ni->ni_ic;
172 int qlen, age;
173
174 IEEE80211_NODE_SAVEQ_LOCK(ni);
175 if (_IF_QFULL(&ni->ni_savedq)) {
176 _IF_DROP(&ni->ni_savedq);
177 IEEE80211_NODE_SAVEQ_UNLOCK(ni);
178 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
179 "[%s] pwr save q overflow, drops %d (size %d)\n",
180 ether_sprintf(ni->ni_macaddr),
181 ni->ni_savedq.ifq_drops, IEEE80211_PS_MAX_QUEUE);
182 #ifdef IEEE80211_DEBUG
183 if (ieee80211_msg_dumppkts(ic))
184 ieee80211_dump_pkt(ic, mtod(m, caddr_t), m->m_len, -1, -1);
185 #endif
186 m_freem(m);
187 return;
188 }
189 /*
190 * Tag the frame with it's expiry time and insert
191 * it in the queue. The aging interval is 4 times
192 * the listen interval specified by the station.
193 * Frames that sit around too long are reclaimed
194 * using this information.
195 */
196 /* TU -> secs. XXX handle overflow? */
197 age = IEEE80211_TU_TO_MS((ni->ni_intval * ic->ic_bintval) << 2) / 1000;
198 _IEEE80211_NODE_SAVEQ_ENQUEUE(ni, m, qlen, age);
199 IEEE80211_NODE_SAVEQ_UNLOCK(ni);
200
201 IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
202 "[%s] save frame with age %d, %u now queued\n",
203 ether_sprintf(ni->ni_macaddr), age, qlen);
204
205 if (qlen == 1 && ic->ic_set_tim != NULL)
206 ic->ic_set_tim(ni, 1);
207 }
208
209 /*
210 * Handle station power-save state change.
211 */
212 void
213 ieee80211_node_pwrsave(struct ieee80211_node *ni, int enable)
214 {
215 struct ieee80211com *ic = ni->ni_ic;
216 struct mbuf *m, *mhead, *mtail;
217 int mcount;
218
219 if (enable) {
220 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) == 0)
221 ic->ic_ps_sta++;
222 ni->ni_flags |= IEEE80211_NODE_PWR_MGT;
223 IEEE80211_NOTE(ic, IEEE80211_MSG_POWER, ni,
224 "power save mode on, %u sta's in ps mode", ic->ic_ps_sta);
225 return;
226 }
227
228 if (ni->ni_flags & IEEE80211_NODE_PWR_MGT)
229 ic->ic_ps_sta--;
230 ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
231 IEEE80211_NOTE(ic, IEEE80211_MSG_POWER, ni,
232 "power save mode off, %u sta's in ps mode", ic->ic_ps_sta);
233 /* XXX if no stations in ps mode, flush mc frames */
234
235 /*
236 * Flush queued unicast frames.
237 */
238 if (IEEE80211_NODE_SAVEQ_QLEN(ni) == 0) {
239 if (ic->ic_set_tim != NULL)
240 ic->ic_set_tim(ni, 0); /* just in case */
241 return;
242 }
243 IEEE80211_NOTE(ic, IEEE80211_MSG_POWER, ni,
244 "flush ps queue, %u packets queue", IEEE80211_NODE_SAVEQ_QLEN(ni));
245 /*
246 * Unload the frames from the ps q but don't send them
247 * to the driver yet. We do this in two stages to minimize
248 * locking but also because there's no easy way to preserve
249 * ordering given the existing ifnet access mechanisms.
250 * XXX could be optimized
251 */
252 IEEE80211_NODE_SAVEQ_LOCK(ni);
253 mcount = IEEE80211_NODE_SAVEQ_QLEN(ni);
254 mhead = mtail = NULL;
255 for (;;) {
256 _IEEE80211_NODE_SAVEQ_DEQUEUE_HEAD(ni, m);
257 if (m == NULL)
258 break;
259 if (mhead == NULL) {
260 mhead = m;
261 m->m_nextpkt = NULL;
262 } else
263 mtail->m_nextpkt = m;
264 mtail = m;
265 }
266 IEEE80211_NODE_SAVEQ_UNLOCK(ni);
267 if (mhead != NULL) {
268 /* XXX need different driver interface */
269 /* XXX bypasses q max */
270 IF_PREPEND_LIST(&ic->ic_ifp->if_snd, mhead, mtail, mcount);
271 }
272 if (ic->ic_set_tim != NULL)
273 ic->ic_set_tim(ni, 0);
274 }
275
276 /*
277 * Handle power-save state change in station mode.
278 */
279 void
280 ieee80211_sta_pwrsave(struct ieee80211com *ic, int enable)
281 {
282 struct ieee80211_node *ni = ic->ic_bss;
283 int qlen;
284
285 if (!((enable != 0) ^ ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) != 0)))
286 return;
287
288 IEEE80211_NOTE(ic, IEEE80211_MSG_POWER, ni,
289 "sta power save mode %s", enable ? "on" : "off");
290 if (!enable) {
291 ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
292 ieee80211_send_nulldata(ieee80211_ref_node(ni));
293 /*
294 * Flush any queued frames; we can do this immediately
295 * because we know they'll be queued behind the null
296 * data frame we send the ap.
297 * XXX can we use a data frame to take us out of ps?
298 */
299 qlen = IEEE80211_NODE_SAVEQ_QLEN(ni);
300 if (qlen != 0) {
301 IEEE80211_NOTE(ic, IEEE80211_MSG_POWER, ni,
302 "flush ps queue, %u packets queued", qlen);
303 for (;;) {
304 struct mbuf *m;
305
306 IEEE80211_NODE_SAVEQ_LOCK(ni);
307 _IEEE80211_NODE_SAVEQ_DEQUEUE_HEAD(ni, m);
308 IEEE80211_NODE_SAVEQ_UNLOCK(ni);
309 if (m == NULL)
310 break;
311 /* XXX need different driver interface */
312 /* XXX bypasses q max */
313 IF_ENQUEUE(&ic->ic_ifp->if_snd, m);
314 }
315 }
316 } else {
317 ni->ni_flags |= IEEE80211_NODE_PWR_MGT;
318 ieee80211_send_nulldata(ieee80211_ref_node(ni));
319 }
320 }
Cache object: 2b31fce449be1fba37821a281b1f58a7
|