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 * $FreeBSD: head/sys/net80211/ieee80211_power.c 186302 2008-12-18 23:00:09Z sam $ 26 */ 27 28 /* 29 * IEEE 802.11 power save support. 30 */ 31 #include "opt_wlan.h" 32 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 #include <sys/kernel.h> 36 37 #include <sys/socket.h> 38 39 #include <net/if.h> 40 #include <net/if_media.h> 41 #include <net/ifq_var.h> 42 #include <net/ethernet.h> 43 #include <net/route.h> 44 45 #include <netproto/802_11/ieee80211_var.h> 46 47 #include <net/bpf.h> 48 49 static void ieee80211_update_ps(struct ieee80211vap *, int); 50 static int ieee80211_set_tim(struct ieee80211_node *, int); 51 52 MALLOC_DEFINE(M_80211_POWER, "80211power", "802.11 power save state"); 53 54 void 55 ieee80211_power_attach(struct ieee80211com *ic) 56 { 57 } 58 59 void 60 ieee80211_power_detach(struct ieee80211com *ic) 61 { 62 } 63 64 void 65 ieee80211_power_vattach(struct ieee80211vap *vap) 66 { 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; 72 } 73 vap->iv_node_ps = ieee80211_node_pwrsave; 74 vap->iv_sta_ps = ieee80211_sta_pwrsave; 75 } 76 77 void 78 ieee80211_power_latevattach(struct ieee80211vap *vap) 79 { 80 /* 81 * Allocate these only if needed. Beware that we 82 * know adhoc mode doesn't support ATIM yet... 83 */ 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 *) kmalloc(vap->iv_tim_len, 87 M_80211_POWER, M_INTWAIT | M_ZERO); 88 if (vap->iv_tim_bitmap == NULL) { 89 kprintf("%s: no memory for TIM bitmap!\n", __func__); 90 /* XXX good enough to keep from crashing? */ 91 vap->iv_tim_len = 0; 92 } 93 } 94 } 95 96 void 97 ieee80211_power_vdetach(struct ieee80211vap *vap) 98 { 99 if (vap->iv_tim_bitmap != NULL) { 100 kfree(vap->iv_tim_bitmap, M_80211_POWER); 101 vap->iv_tim_bitmap = NULL; 102 } 103 } 104 105 void 106 ieee80211_psq_init(struct ieee80211_psq *psq, const char *name) 107 { 108 memset(psq, 0, sizeof(*psq)); 109 psq->psq_maxlen = IEEE80211_PS_MAX_QUEUE; 110 } 111 112 void 113 ieee80211_psq_cleanup(struct ieee80211_psq *psq) 114 { 115 #if 0 116 psq_drain(psq); /* XXX should not be needed? */ 117 #else 118 KASSERT(psq->psq_len == 0, ("%d frames on ps q", psq->psq_len)); 119 #endif 120 } 121 122 /* 123 * Return the highest priority frame in the ps queue. 124 */ 125 struct mbuf * 126 ieee80211_node_psq_dequeue(struct ieee80211_node *ni, int *qlen) 127 { 128 struct ieee80211_psq *psq = &ni->ni_psq; 129 struct ieee80211_psq_head *qhead; 130 struct mbuf *m; 131 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 return m; 151 } 152 153 /* 154 * Reclaim an mbuf from the ps q. If marked with M_ENCAP 155 * we assume there is a node reference that must be relcaimed. 156 */ 157 static void 158 psq_mfree(struct mbuf *m) 159 { 160 if (m->m_flags & M_ENCAP) { 161 struct ieee80211_node *ni = (void *) m->m_pkthdr.rcvif; 162 ieee80211_free_node(ni); 163 } 164 m->m_nextpkt = NULL; 165 m_freem(m); 166 } 167 168 /* 169 * Clear any frames queued in the power save queue. 170 * The number of frames that were present is returned. 171 */ 172 static int 173 psq_drain(struct ieee80211_psq *psq) 174 { 175 struct ieee80211_psq_head *qhead; 176 struct mbuf *m; 177 int qlen; 178 179 qlen = psq->psq_len; 180 qhead = &psq->psq_head[0]; 181 again: 182 while ((m = qhead->head) != NULL) { 183 qhead->head = m->m_nextpkt; 184 psq_mfree(m); 185 } 186 qhead->tail = NULL; 187 qhead->len = 0; 188 if (qhead == &psq->psq_head[0]) { /* Algol-68 style for loop */ 189 qhead = &psq->psq_head[1]; 190 goto again; 191 } 192 psq->psq_len = 0; 193 194 return qlen; 195 } 196 197 /* 198 * Clear any frames queued in the power save queue. 199 * The number of frames that were present is returned. 200 */ 201 int 202 ieee80211_node_psq_drain(struct ieee80211_node *ni) 203 { 204 return psq_drain(&ni->ni_psq); 205 } 206 207 /* 208 * Age frames on the power save queue. The aging interval is 209 * 4 times the listen interval specified by the station. This 210 * number is factored into the age calculations when the frame 211 * is placed on the queue. We store ages as time differences 212 * so we can check and/or adjust only the head of the list. 213 * If a frame's age exceeds the threshold then discard it. 214 * The number of frames discarded is returned so the caller 215 * can check if it needs to adjust the tim. 216 */ 217 int 218 ieee80211_node_psq_age(struct ieee80211_node *ni) 219 { 220 struct ieee80211_psq *psq = &ni->ni_psq; 221 int discard = 0; 222 223 if (psq->psq_len != 0) { 224 #ifdef IEEE80211_DEBUG 225 struct ieee80211vap *vap = ni->ni_vap; 226 #endif 227 struct ieee80211_psq_head *qhead; 228 struct mbuf *m; 229 230 qhead = &psq->psq_head[0]; 231 again: 232 while ((m = qhead->head) != NULL && 233 M_AGE_GET(m) < IEEE80211_INACT_WAIT) { 234 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni, 235 "discard frame, age %u", M_AGE_GET(m)); 236 if ((qhead->head = m->m_nextpkt) == NULL) 237 qhead->tail = NULL; 238 KASSERT(qhead->len > 0, ("qhead len %d", qhead->len)); 239 qhead->len--; 240 KASSERT(psq->psq_len > 0, ("psq len %d", psq->psq_len)); 241 psq->psq_len--; 242 psq_mfree(m); 243 discard++; 244 } 245 if (qhead == &psq->psq_head[0]) { /* Algol-68 style for loop */ 246 qhead = &psq->psq_head[1]; 247 goto again; 248 } 249 if (m != NULL) 250 M_AGE_SUB(m, IEEE80211_INACT_WAIT); 251 252 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni, 253 "discard %u frames for age", discard); 254 IEEE80211_NODE_STAT_ADD(ni, ps_discard, discard); 255 } 256 return discard; 257 } 258 259 /* 260 * Handle a change in the PS station occupancy. 261 */ 262 static void 263 ieee80211_update_ps(struct ieee80211vap *vap, int nsta) 264 { 265 266 KASSERT(vap->iv_opmode == IEEE80211_M_HOSTAP || 267 vap->iv_opmode == IEEE80211_M_IBSS, 268 ("operating mode %u", vap->iv_opmode)); 269 } 270 271 /* 272 * Indicate whether there are frames queued for a station in power-save mode. 273 */ 274 static int 275 ieee80211_set_tim(struct ieee80211_node *ni, int set) 276 { 277 struct ieee80211vap *vap = ni->ni_vap; 278 uint16_t aid; 279 int changed; 280 281 KASSERT(vap->iv_opmode == IEEE80211_M_HOSTAP || 282 vap->iv_opmode == IEEE80211_M_IBSS, 283 ("operating mode %u", vap->iv_opmode)); 284 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)); 288 289 changed = (set != (isset(vap->iv_tim_bitmap, aid) != 0)); 290 if (changed) { 291 if (set) { 292 setbit(vap->iv_tim_bitmap, aid); 293 vap->iv_ps_pending++; 294 } else { 295 clrbit(vap->iv_tim_bitmap, aid); 296 vap->iv_ps_pending--; 297 } 298 /* NB: we know vap is in RUN state so no need to check */ 299 vap->iv_update_beacon(vap, IEEE80211_BEACON_TIM); 300 } 301 302 return changed; 303 } 304 305 /* 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. 309 */ 310 int 311 ieee80211_pwrsave(struct ieee80211_node *ni, struct mbuf *m) 312 { 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; 317 int qlen, age; 318 319 if (psq->psq_len >= psq->psq_maxlen) { 320 psq->psq_drops++; 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), 327 m->m_len, -1, -1); 328 #endif 329 psq_mfree(m); 330 return ENOSPC; 331 } 332 /* 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 337 * information. 338 */ 339 /* TU -> secs. XXX handle overflow? */ 340 age = IEEE80211_TU_TO_MS((ni->ni_intval * ic->ic_bintval) << 2) / 1000; 341 /* 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. 346 */ 347 if (m->m_flags & M_ENCAP) 348 qhead = &psq->psq_head[0]; 349 else 350 qhead = &psq->psq_head[1]; 351 if (qhead->tail == NULL) { 352 struct mbuf *mh; 353 354 qhead->head = m; 355 /* 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. 360 */ 361 if (qhead == &psq->psq_head[1]) { 362 mh = psq->psq_head[0].head; 363 if (mh != NULL) 364 age-= M_AGE_GET(mh); 365 } else { 366 mh = psq->psq_head[1].head; 367 if (mh != NULL) { 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); 371 } 372 } 373 } else { 374 qhead->tail->m_nextpkt = m; 375 age -= M_AGE_GET(qhead->head); 376 } 377 KASSERT(age >= 0, ("age %d", age)); 378 M_AGE_SET(m, age); 379 m->m_nextpkt = NULL; 380 qhead->tail = m; 381 qhead->len++; 382 qlen = ++(psq->psq_len); 383 384 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni, 385 "save frame with age %d, %u now queued", age, qlen); 386 387 if (qlen == 1 && vap->iv_set_tim != NULL) 388 vap->iv_set_tim(ni, 1); 389 390 return 0; 391 } 392 393 /* 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. 398 */ 399 static void 400 pwrsave_flushq(struct ieee80211_node *ni) 401 { 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; 406 struct ifaltq_subque *ifp_ifsq, *parent_ifsq; 407 408 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni, 409 "flush ps queue, %u packets queued", psq->psq_len); 410 411 qhead = &psq->psq_head[0]; /* 802.11 frames */ 412 if (qhead->head != NULL) { 413 const struct mbuf *m; 414 int bcnt = 0; 415 416 for (m = qhead->head; m != NULL; m = m->m_nextpkt) 417 bcnt += m->m_pkthdr.len; 418 419 /* XXX could dispatch through vap and check M_ENCAP */ 420 parent = vap->iv_ic->ic_ifp; 421 parent_ifsq = ifq_get_subq_default(&parent->if_snd); 422 423 /* XXX this breaks ALTQ's packet scheduler */ 424 ALTQ_SQ_LOCK(parent_ifsq); 425 /* XXX need different driver interface */ 426 /* XXX bypasses q max and OACTIVE */ 427 IF_PREPEND_LIST(parent_ifsq, qhead->head, qhead->tail, 428 qhead->len, bcnt); 429 ALTQ_SQ_UNLOCK(parent_ifsq); 430 431 qhead->head = qhead->tail = NULL; 432 qhead->len = 0; 433 } else { 434 parent = NULL; 435 parent_ifsq = NULL; 436 } 437 438 qhead = &psq->psq_head[1]; /* 802.3 frames */ 439 if (qhead->head != NULL) { 440 const struct mbuf *m; 441 int bcnt = 0; 442 443 for (m = qhead->head; m != NULL; m = m->m_nextpkt) 444 bcnt += m->m_pkthdr.len; 445 446 ifp = vap->iv_ifp; 447 ifp_ifsq = ifq_get_subq_default(&ifp->if_snd); 448 449 /* XXX this breaks ALTQ's packet scheduler */ 450 ALTQ_SQ_LOCK(ifp_ifsq); 451 /* XXX need different driver interface */ 452 /* XXX bypasses q max and OACTIVE */ 453 IF_PREPEND_LIST(ifp_ifsq, qhead->head, qhead->tail, 454 qhead->len, bcnt); 455 ALTQ_SQ_UNLOCK(ifp_ifsq); 456 457 qhead->head = qhead->tail = NULL; 458 qhead->len = 0; 459 } else { 460 ifp = NULL; 461 ifp_ifsq = NULL; 462 } 463 psq->psq_len = 0; 464 465 /* NB: do this outside the psq lock */ 466 /* XXX packets might get reordered if parent is OACTIVE */ 467 if (parent != NULL && parent_ifsq != NULL) 468 parent->if_start(parent, parent_ifsq); 469 if (ifp != NULL && ifp_ifsq != NULL) 470 ifp->if_start(ifp, ifp_ifsq); 471 } 472 473 /* 474 * Handle station power-save state change. 475 */ 476 void 477 ieee80211_node_pwrsave(struct ieee80211_node *ni, int enable) 478 { 479 struct ieee80211vap *vap = ni->ni_vap; 480 int update; 481 482 update = 0; 483 if (enable) { 484 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) == 0) { 485 vap->iv_ps_sta++; 486 update = 1; 487 } 488 ni->ni_flags |= IEEE80211_NODE_PWR_MGT; 489 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni, 490 "power save mode on, %u sta's in ps mode", vap->iv_ps_sta); 491 492 if (update) 493 vap->iv_update_ps(vap, vap->iv_ps_sta); 494 } else { 495 if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) { 496 vap->iv_ps_sta--; 497 update = 1; 498 } 499 ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT; 500 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni, 501 "power save mode off, %u sta's in ps mode", vap->iv_ps_sta); 502 503 /* NB: order here is intentional so TIM is clear before flush */ 504 if (vap->iv_set_tim != NULL) 505 vap->iv_set_tim(ni, 0); 506 if (update) { 507 /* NB if no sta's in ps, driver should flush mc q */ 508 vap->iv_update_ps(vap, vap->iv_ps_sta); 509 } 510 if (ni->ni_psq.psq_len != 0) 511 pwrsave_flushq(ni); 512 } 513 } 514 515 /* 516 * Handle power-save state change in station mode. 517 */ 518 void 519 ieee80211_sta_pwrsave(struct ieee80211vap *vap, int enable) 520 { 521 struct ieee80211_node *ni = vap->iv_bss; 522 523 if (!((enable != 0) ^ ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) != 0))) 524 return; 525 526 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni, 527 "sta power save mode %s", enable ? "on" : "off"); 528 if (!enable) { 529 ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT; 530 ieee80211_send_nulldata(ieee80211_ref_node(ni)); 531 /* 532 * Flush any queued frames; we can do this immediately 533 * because we know they'll be queued behind the null 534 * data frame we send the ap. 535 * XXX can we use a data frame to take us out of ps? 536 */ 537 if (ni->ni_psq.psq_len != 0) 538 pwrsave_flushq(ni); 539 } else { 540 ni->ni_flags |= IEEE80211_NODE_PWR_MGT; 541 ieee80211_send_nulldata(ieee80211_ref_node(ni)); 542 } 543 } 544 545 /* 546 * Handle being notified that we have data available for us in a TIM/ATIM. 547 * 548 * This may schedule a transition from _SLEEP -> _RUN if it's appropriate. 549 */ 550 void 551 ieee80211_sta_tim_notify(struct ieee80211vap *vap, int set) 552 { 553 /* 554 * Schedule the driver state change. It'll happen at some point soon. 555 * Since the hardware shouldn't know that we're running just yet 556 * (and thus tell the peer that we're awake before we actually wake 557 * up said hardware), we leave the actual node state transition 558 * up to the transition to RUN. 559 * 560 * XXX TODO: verify that the transition to RUN will wake up the 561 * BSS node! 562 */ 563 IEEE80211_DPRINTF(vap, IEEE80211_MSG_POWER, "%s: TIM=%d\n", __func__, set); 564 if (set == 1 && vap->iv_state == IEEE80211_S_SLEEP) { 565 ieee80211_new_state_locked(vap, IEEE80211_S_RUN, 0); 566 } 567 } 568 569 /* 570 * Timer check on whether the VAP has had any transmit activity. 571 * 572 * This may schedule a transition from _RUN -> _SLEEP if it's appropriate. 573 */ 574 void 575 ieee80211_sta_ps_timer_check(struct ieee80211vap *vap) 576 { 577 struct ieee80211com *ic = vap->iv_ic; 578 579 /* XXX lock assert */ 580 581 /* For no, only do this in STA mode */ 582 if (! (vap->iv_caps & IEEE80211_C_SWSLEEP)) 583 goto out; 584 585 if (vap->iv_opmode != IEEE80211_M_STA) 586 goto out; 587 588 /* If we're not at run state, bail */ 589 if (vap->iv_state != IEEE80211_S_RUN) 590 goto out; 591 592 IEEE80211_DPRINTF(vap, IEEE80211_MSG_POWER, 593 "%s: lastdata=%llu, ticks=%llu\n", 594 __func__, (unsigned long long) ic->ic_lastdata, 595 (unsigned long long) ticks); 596 597 /* If powersave is disabled on the VAP, don't bother */ 598 if (! (vap->iv_flags & IEEE80211_F_PMGTON)) 599 goto out; 600 601 /* If we've done any data within our idle interval, bail */ 602 /* XXX hard-coded to one second for now, ew! */ 603 if (time_after(ic->ic_lastdata + 500, ticks)) 604 goto out; 605 606 /* 607 * Signify we're going into power save and transition the 608 * node to powersave. 609 */ 610 if ((vap->iv_bss->ni_flags & IEEE80211_NODE_PWR_MGT) == 0) 611 vap->iv_sta_ps(vap, 1); 612 613 /* 614 * XXX The driver has to handle the fact that we're going 615 * to sleep but frames may still be transmitted; 616 * hopefully it and/or us will do the right thing and mark any 617 * transmitted frames with PWRMGT set to 1. 618 */ 619 ieee80211_new_state_locked(vap, IEEE80211_S_SLEEP, 0); 620 621 IEEE80211_DPRINTF(vap, IEEE80211_MSG_POWER, 622 "%s: time delta=%d msec\n", __func__, 623 (int) ticks_to_msecs(ticks - ic->ic_lastdata)); 624 625 out: 626 return; 627 } 628