xref: /freebsd/sys/net80211/ieee80211_ratectl.h (revision af23369a6deaaeb612ab266eb88b8bb8d560c322)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2010 Rui Paulo <rpaulo@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * 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 copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  * $FreeBSD$
28  */
29 
30 #ifndef _NET80211_IEEE80211_RATECTL_H_
31 #define _NET80211_IEEE80211_RATECTL_H_
32 
33 enum ieee80211_ratealgs {
34 	IEEE80211_RATECTL_AMRR		= 0,
35 	IEEE80211_RATECTL_RSSADAPT	= 1,
36 	IEEE80211_RATECTL_ONOE		= 2,
37 	IEEE80211_RATECTL_SAMPLE	= 3,
38 	IEEE80211_RATECTL_NONE		= 4,
39 	IEEE80211_RATECTL_MAX
40 };
41 
42 /* used fields for tx_complete() events */
43 #define IEEE80211_RATECTL_STATUS_PKTLEN		0x00000001
44 #define IEEE80211_RATECTL_STATUS_FINAL_RATE	0x00000002
45 #define IEEE80211_RATECTL_STATUS_SHORT_RETRY	0x00000004
46 #define IEEE80211_RATECTL_STATUS_LONG_RETRY	0x00000008
47 #define IEEE80211_RATECTL_STATUS_RSSI		0x00000010
48 
49 /* failure reason */
50 enum ieee80211_ratectl_tx_fail_reason {
51 	IEEE80211_RATECTL_TX_SUCCESS		= 0,
52 	IEEE80211_RATECTL_TX_FAIL_SHORT		= 1,	/* too many RTS retries */
53 	IEEE80211_RATECTL_TX_FAIL_LONG		= 2,	/* too many retries */
54 	IEEE80211_RATECTL_TX_FAIL_EXPIRED	= 3,	/* lifetime expired */
55 	IEEE80211_RATECTL_TX_FAIL_UNSPECIFIED	= 4,	/* another reason */
56 };
57 #define IEEE80211_RATECTL_TX_FAIL_MAX	\
58 	(IEEE80211_RATECTL_TX_FAIL_UNSPECIFIED + 1)
59 
60 struct ieee80211_ratectl_tx_status {
61 	uint32_t	flags;		/* mark used fields */
62 	enum ieee80211_ratectl_tx_fail_reason status;	/* Tx status */
63 
64 	int		pktlen;		/* frame length */
65 	int		final_rate;	/* transmission rate */
66 	uint_fast8_t	short_retries;	/* RTS/CTS retries */
67 	uint_fast8_t	long_retries;	/* ACK retries */
68 	int8_t		rssi;		/* ACK RSSI */
69 
70 	uint8_t		spare[15];	/* for future use */
71 };
72 
73 /* used fields for tx_update() events */
74 #define IEEE80211_RATECTL_TX_STATS_NODE		0x00000001
75 #define IEEE80211_RATECTL_TX_STATS_RETRIES	0x00000002
76 
77 struct ieee80211_ratectl_tx_stats {
78 	uint32_t	flags;		/* mark used fields */
79 
80 	struct ieee80211_node *ni;	/* receiver */
81 	int		nframes;	/* transmitted frames */
82 	int		nsuccess;	/* ACKed frames */
83 	int		nretries;	/* number of retries */
84 };
85 
86 struct ieee80211_ratectl {
87 	const char *ir_name;
88 	int	(*ir_attach)(const struct ieee80211vap *);
89 	void	(*ir_detach)(const struct ieee80211vap *);
90 	void	(*ir_init)(struct ieee80211vap *);
91 	void	(*ir_deinit)(struct ieee80211vap *);
92 	void	(*ir_node_init)(struct ieee80211_node *);
93 	void	(*ir_node_deinit)(struct ieee80211_node *);
94 	int	(*ir_rate)(struct ieee80211_node *, void *, uint32_t);
95 	void	(*ir_tx_complete)(const struct ieee80211_node *,
96 	    			  const struct ieee80211_ratectl_tx_status *);
97 	void	(*ir_tx_update)(struct ieee80211vap *,
98 				struct ieee80211_ratectl_tx_stats *);
99 	void	(*ir_setinterval)(const struct ieee80211vap *, int);
100 	void	(*ir_node_stats)(struct ieee80211_node *ni, struct sbuf *s);
101 };
102 
103 void	ieee80211_ratectl_register(int, const struct ieee80211_ratectl *);
104 void	ieee80211_ratectl_unregister(int);
105 void	ieee80211_ratectl_init(struct ieee80211vap *);
106 void	ieee80211_ratectl_set(struct ieee80211vap *, int);
107 
108 MALLOC_DECLARE(M_80211_RATECTL);
109 
110 static __inline void
111 ieee80211_ratectl_deinit(struct ieee80211vap *vap)
112 {
113 	vap->iv_rate->ir_deinit(vap);
114 }
115 
116 static __inline void
117 ieee80211_ratectl_node_init(struct ieee80211_node *ni)
118 {
119 	const struct ieee80211vap *vap = ni->ni_vap;
120 
121 	vap->iv_rate->ir_node_init(ni);
122 }
123 
124 static __inline void
125 ieee80211_ratectl_node_deinit(struct ieee80211_node *ni)
126 {
127 	const struct ieee80211vap *vap = ni->ni_vap;
128 
129 	vap->iv_rate->ir_node_deinit(ni);
130 }
131 
132 static int __inline
133 ieee80211_ratectl_rate(struct ieee80211_node *ni, void *arg, uint32_t iarg)
134 {
135 	const struct ieee80211vap *vap = ni->ni_vap;
136 
137 	return vap->iv_rate->ir_rate(ni, arg, iarg);
138 }
139 
140 static __inline void
141 ieee80211_ratectl_tx_complete(const struct ieee80211_node *ni,
142     const struct ieee80211_ratectl_tx_status *status)
143 {
144 	const struct ieee80211vap *vap = ni->ni_vap;
145 
146 	vap->iv_rate->ir_tx_complete(ni, status);
147 }
148 
149 static __inline void
150 ieee80211_ratectl_tx_update(struct ieee80211vap *vap,
151     struct ieee80211_ratectl_tx_stats *stats)
152 {
153 	if (vap->iv_rate->ir_tx_update == NULL)
154 		return;
155 	vap->iv_rate->ir_tx_update(vap, stats);
156 }
157 
158 static __inline void
159 ieee80211_ratectl_setinterval(const struct ieee80211vap *vap, int msecs)
160 {
161 	if (vap->iv_rate->ir_setinterval == NULL)
162 		return;
163 	vap->iv_rate->ir_setinterval(vap, msecs);
164 }
165 
166 static __inline void
167 ieee80211_ratectl_node_stats(struct ieee80211_node *ni, struct sbuf *s)
168 {
169 	const struct ieee80211vap *vap = ni->ni_vap;
170 
171 	if (vap->iv_rate->ir_node_stats == NULL)
172 		return;
173 	vap->iv_rate->ir_node_stats(ni, s);
174 }
175 
176 #endif	/* _NET80211_IEEE80211_RATECTL_H_ */
177