xref: /openbsd/sys/net80211/ieee80211_amrr.c (revision a6445c1d)
1 /*	$OpenBSD: ieee80211_amrr.c,v 1.7 2011/03/02 08:48:59 fgsch Exp $	*/
2 
3 /*-
4  * Copyright (c) 2006
5  *	Damien Bergamini <damien.bergamini@free.fr>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include <sys/param.h>
21 #include <sys/systm.h>
22 #include <sys/kernel.h>
23 #include <sys/socket.h>
24 
25 #include <net/if.h>
26 #include <net/if_media.h>
27 
28 #ifdef INET
29 #include <netinet/in.h>
30 #include <netinet/if_ether.h>
31 #endif
32 
33 #include <net80211/ieee80211_var.h>
34 #include <net80211/ieee80211_priv.h>
35 #include <net80211/ieee80211_amrr.h>
36 
37 #define is_success(amn)	\
38 	((amn)->amn_retrycnt < (amn)->amn_txcnt / 10)
39 #define is_failure(amn)	\
40 	((amn)->amn_retrycnt > (amn)->amn_txcnt / 3)
41 #define is_enough(amn)		\
42 	((amn)->amn_txcnt > 10)
43 #define is_min_rate(ni)		\
44 	((ni)->ni_txrate == 0)
45 #define is_max_rate(ni)		\
46 	((ni)->ni_txrate == (ni)->ni_rates.rs_nrates - 1)
47 #define increase_rate(ni)	\
48 	((ni)->ni_txrate++)
49 #define decrease_rate(ni)	\
50 	((ni)->ni_txrate--)
51 #define reset_cnt(amn)		\
52 	do { (amn)->amn_txcnt = (amn)->amn_retrycnt = 0; } while (0)
53 
54 void
55 ieee80211_amrr_node_init(const struct ieee80211_amrr *amrr,
56     struct ieee80211_amrr_node *amn)
57 {
58 	amn->amn_success = 0;
59 	amn->amn_recovery = 0;
60 	amn->amn_txcnt = amn->amn_retrycnt = 0;
61 	amn->amn_success_threshold = amrr->amrr_min_success_threshold;
62 }
63 
64 /*
65  * Update ni->ni_txrate.
66  */
67 void
68 ieee80211_amrr_choose(struct ieee80211_amrr *amrr, struct ieee80211_node *ni,
69     struct ieee80211_amrr_node *amn)
70 {
71 #define RV(rate)	((rate) & IEEE80211_RATE_VAL)
72 	int need_change = 0;
73 
74 	if (is_success(amn) && is_enough(amn)) {
75 		amn->amn_success++;
76 		if (amn->amn_success >= amn->amn_success_threshold &&
77 		    !is_max_rate(ni)) {
78 			amn->amn_recovery = 1;
79 			amn->amn_success = 0;
80 			increase_rate(ni);
81 			DPRINTF(("increase rate=%d,#tx=%d,#retries=%d\n",
82 			    RV(ni->ni_rates.rs_rates[ni->ni_txrate]),
83 			    amn->amn_txcnt, amn->amn_retrycnt));
84 			need_change = 1;
85 		} else {
86 			amn->amn_recovery = 0;
87 		}
88 	} else if (is_failure(amn)) {
89 		amn->amn_success = 0;
90 		if (!is_min_rate(ni)) {
91 			if (amn->amn_recovery) {
92 				amn->amn_success_threshold *= 2;
93 				if (amn->amn_success_threshold >
94 				    amrr->amrr_max_success_threshold)
95 					amn->amn_success_threshold =
96 					    amrr->amrr_max_success_threshold;
97 			} else {
98 				amn->amn_success_threshold =
99 				    amrr->amrr_min_success_threshold;
100 			}
101 			decrease_rate(ni);
102 			DPRINTF(("decrease rate=%d,#tx=%d,#retries=%d\n",
103 			    RV(ni->ni_rates.rs_rates[ni->ni_txrate]),
104 			    amn->amn_txcnt, amn->amn_retrycnt));
105 			need_change = 1;
106 		}
107 		amn->amn_recovery = 0;
108 	}
109 
110 	if (is_enough(amn) || need_change)
111 		reset_cnt(amn);
112 #undef RV
113 }
114