Merge branch 'master' into net80211-update
[dragonfly.git] / sys / netproto / 802_11 / ieee80211_rssadapt.h
1 /* $DragonFly$ */
2 /*      $FreeBSD: head/sys/net80211/ieee80211_rssadapt.h 178354 2008-04-20 20:35:46Z sam $      */
3 /* $NetBSD: ieee80211_rssadapt.h,v 1.4 2005/02/26 22:45:09 perry Exp $ */
4 /*-
5  * Copyright (c) 2003, 2004 David Young.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or
8  * without modification, are permitted provided that the following
9  * conditions are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above
13  *    copyright notice, this list of conditions and the following
14  *    disclaimer in the documentation and/or other materials provided
15  *    with the distribution.
16  * 3. The name of David Young may not be used to endorse or promote
17  *    products derived from this software without specific prior
18  *    written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY David Young ``AS IS'' AND ANY
21  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL David
24  * Young BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
31  * OF SUCH DAMAGE.
32  */
33 #ifndef _NET80211_IEEE80211_RSSADAPT_H_
34 #define _NET80211_IEEE80211_RSSADAPT_H_
35
36 /* Data-rate adaptation loosely based on "Link Adaptation Strategy
37  * for IEEE 802.11 WLAN via Received Signal Strength Measurement"
38  * by Javier del Prado Pavon and Sunghyun Choi.
39  */
40
41 /* Buckets for frames 0-128 bytes long, 129-1024, 1025-maximum. */
42 #define IEEE80211_RSSADAPT_BKTS         3
43 #define IEEE80211_RSSADAPT_BKT0         128
44 #define IEEE80211_RSSADAPT_BKTPOWER     3       /* 2**_BKTPOWER */
45
46 struct ieee80211_rssadapt {
47         struct ieee80211vap *vap;
48         int     interval;                       /* update interval (ticks) */
49 };
50
51 struct ieee80211_rssadapt_node {
52         struct ieee80211_rssadapt *ra_rs;       /* backpointer */
53         struct ieee80211_rateset ra_rates;      /* negotiated rates */
54         int     ra_rix;                         /* current rate index */
55         int     ra_ticks;                       /* time of last update */
56         int     ra_last_raise;                  /* time of last rate raise */
57         int     ra_raise_interval;              /* rate raise time threshold */
58
59         /* Tx failures in this update interval */
60         uint32_t                ra_nfail;
61         /* Tx successes in this update interval */
62         uint32_t                ra_nok;
63         /* exponential average packets/second */
64         uint32_t                ra_pktrate;
65         /* RSSI threshold for each Tx rate */
66         uint16_t                ra_rate_thresh[IEEE80211_RSSADAPT_BKTS]
67                                               [IEEE80211_RATE_SIZE];
68 };
69
70 void    ieee80211_rssadapt_init(struct ieee80211_rssadapt *,
71             struct ieee80211vap *, int);
72 void    ieee80211_rssadapt_cleanup(struct ieee80211_rssadapt *);
73 void    ieee80211_rssadapt_setinterval(struct ieee80211_rssadapt *, int);
74 void    ieee80211_rssadapt_node_init(struct ieee80211_rssadapt *,
75             struct ieee80211_rssadapt_node *, struct ieee80211_node *);
76 int     ieee80211_rssadapt_choose(struct ieee80211_node *,
77             struct ieee80211_rssadapt_node *, u_int);
78
79 /* NB: these are public only for the inline below */
80 void    ieee80211_rssadapt_raise_rate(struct ieee80211_rssadapt_node *,
81             int pktlen, int rssi);
82 void    ieee80211_rssadapt_lower_rate(struct ieee80211_rssadapt_node *,
83             int pktlen, int rssi);
84
85 #define IEEE80211_RSSADAPT_SUCCESS      1
86 #define IEEE80211_RSSADAPT_FAILURE      0
87
88 static __inline void
89 ieee80211_rssadapt_tx_complete(struct ieee80211_rssadapt_node *ra,
90     int success, int pktlen, int rssi)
91 {
92         if (success) {
93                 ra->ra_nok++;
94                 if ((ra->ra_rix + 1) < ra->ra_rates.rs_nrates &&
95                     (ticks - ra->ra_last_raise) >= ra->ra_raise_interval)
96                         ieee80211_rssadapt_raise_rate(ra, pktlen, rssi);
97         } else {
98                 ra->ra_nfail++;
99                 ieee80211_rssadapt_lower_rate(ra, pktlen, rssi);
100         }
101 }
102 #endif /* _NET80211_IEEE80211_RSSADAPT_H_ */