You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rc80211.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /*
  2. * Simple 802.11 rate-control algorithm for gPXE.
  3. *
  4. * Copyright (c) 2009 Joshua Oreman <oremanj@rwcr.net>.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of the
  9. * License, or any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. FILE_LICENCE ( GPL2_OR_LATER );
  21. #include <stdlib.h>
  22. #include <gpxe/net80211.h>
  23. /**
  24. * @file
  25. *
  26. * Simple 802.11 rate-control algorithm
  27. */
  28. /** @page rc80211 Rate control philosophy
  29. *
  30. * We want to maximize our transmission speed, to the extent that we
  31. * can do that without dropping undue numbers of packets. We also
  32. * don't want to take up very much code space, so our algorithm has to
  33. * be pretty simple
  34. *
  35. * When we receive a packet, we know what rate it was transmitted at,
  36. * and whether it had to be retransmitted to get to us.
  37. *
  38. * When we send a packet, we hear back how many times it had to be
  39. * retried to get through, and whether it got through at all.
  40. *
  41. * Indications of TX success are more reliable than RX success, but RX
  42. * information helps us know where to start.
  43. *
  44. * To handle all of this, we keep for each rate and each direction (TX
  45. * and RX separately) some state information for the most recent
  46. * packets on that rate and the number of packets for which we have
  47. * information. The state is a 32-bit unsigned integer in which two
  48. * bits represent a packet: 11 if it went through well, 10 if it went
  49. * through with one retry, 01 if it went through with more than one
  50. * retry, or 00 if it didn't go through at all. We define the
  51. * "goodness" for a particular (rate, direction) combination as the
  52. * sum of all the 2-bit fields, times 33, divided by the number of
  53. * 2-bit fields containing valid information (16 except when we're
  54. * starting out). The number produced is between 0 and 99; we use -1
  55. * for rates with less than 4 RX packets or 1 TX, as an indicator that
  56. * we do not have enough information to rely on them.
  57. *
  58. * In deciding which rates are best, we find the weighted average of
  59. * TX and RX goodness, where the weighting is by number of packets
  60. * with data and TX packets are worth 4 times as much as RX packets.
  61. * The weighted average is called "net goodness" and is also a number
  62. * between 0 and 99. If 3 consecutive packets fail transmission
  63. * outright, we automatically ratchet down the rate; otherwise, we
  64. * switch to the best rate whenever the current rate's goodness falls
  65. * below some threshold, and try increasing our rate when the goodness
  66. * is very high.
  67. *
  68. * This system is optimized for gPXE's style of usage. Because normal
  69. * operation always involves receiving something, we'll make our way
  70. * to the best rate pretty quickly. We tend to follow the lead of the
  71. * sending AP in choosing rates, but we won't use rates for long that
  72. * don't work well for us in transmission. We assume gPXE won't be
  73. * running for long enough that rate patterns will change much, so we
  74. * don't have to keep time counters or the like. And if this doesn't
  75. * work well in practice there are many ways it could be tweaked.
  76. *
  77. * To avoid staying at 1Mbps for a long time, we don't track any
  78. * transmitted packets until we've set our rate based on received
  79. * packets.
  80. */
  81. /** Two-bit packet status indicator for a packet with no retries */
  82. #define RC_PKT_OK 0x3
  83. /** Two-bit packet status indicator for a packet with one retry */
  84. #define RC_PKT_RETRIED_ONCE 0x2
  85. /** Two-bit packet status indicator for a TX packet with multiple retries
  86. *
  87. * It is not possible to tell whether an RX packet had one or multiple
  88. * retries; we rely instead on the fact that failed RX packets won't
  89. * get to us at all, so if we receive a lot of RX packets on a certain
  90. * rate it must be pretty good.
  91. */
  92. #define RC_PKT_RETRIED_MULTI 0x1
  93. /** Two-bit packet status indicator for a TX packet that was never ACKed
  94. *
  95. * It is not possible to tell whether an RX packet was setn if it
  96. * didn't get through to us, but if we don't see one we won't increase
  97. * the goodness for its rate. This asymmetry is part of why TX packets
  98. * are weighted much more heavily than RX.
  99. */
  100. #define RC_PKT_FAILED 0x0
  101. /** Number of times to weight TX packets more heavily than RX packets */
  102. #define RC_TX_FACTOR 4
  103. /** Number of consecutive failed TX packets that cause an automatic rate drop */
  104. #define RC_TX_EMERG_FAIL 3
  105. /** Minimum net goodness below which we will search for a better rate */
  106. #define RC_GOODNESS_MIN 85
  107. /** Maximum net goodness above which we will try to increase our rate */
  108. #define RC_GOODNESS_MAX 95
  109. /** Minimum (num RX + @c RC_TX_FACTOR * num TX) to use a certain rate */
  110. #define RC_UNCERTAINTY_THRESH 4
  111. /** TX direction */
  112. #define TX 0
  113. /** RX direction */
  114. #define RX 1
  115. /** A rate control context */
  116. struct rc80211_ctx
  117. {
  118. /** Goodness state for each rate, TX and RX */
  119. u32 goodness[2][NET80211_MAX_RATES];
  120. /** Number of packets recorded for each rate */
  121. u8 count[2][NET80211_MAX_RATES];
  122. /** Indication of whether we've set the device rate yet */
  123. int started;
  124. /** Counter of all packets sent and received */
  125. int packets;
  126. };
  127. /**
  128. * Initialize rate-control algorithm
  129. *
  130. * @v dev 802.11 device
  131. * @ret ctx Rate-control context, to be stored in @c dev->rctl
  132. */
  133. struct rc80211_ctx * rc80211_init ( struct net80211_device *dev __unused )
  134. {
  135. struct rc80211_ctx *ret = zalloc ( sizeof ( *ret ) );
  136. return ret;
  137. }
  138. /**
  139. * Calculate net goodness for a certain rate
  140. *
  141. * @v ctx Rate-control context
  142. * @v rate_idx Index of rate to calculate net goodness for
  143. */
  144. static int rc80211_calc_net_goodness ( struct rc80211_ctx *ctx,
  145. int rate_idx )
  146. {
  147. int sum[2], num[2], dir, pkt;
  148. for ( dir = 0; dir < 2; dir++ ) {
  149. u32 good = ctx->goodness[dir][rate_idx];
  150. num[dir] = ctx->count[dir][rate_idx];
  151. sum[dir] = 0;
  152. for ( pkt = 0; pkt < num[dir]; pkt++ )
  153. sum[dir] += ( good >> ( 2 * pkt ) ) & 0x3;
  154. }
  155. if ( ( num[TX] * RC_TX_FACTOR + num[RX] ) < RC_UNCERTAINTY_THRESH )
  156. return -1;
  157. return ( 33 * ( sum[TX] * RC_TX_FACTOR + sum[RX] ) /
  158. ( num[TX] * RC_TX_FACTOR + num[RX] ) );
  159. }
  160. /**
  161. * Determine the best rate to switch to and return it
  162. *
  163. * @v dev 802.11 device
  164. * @ret rate_idx Index of the best rate to switch to
  165. */
  166. static int rc80211_pick_best ( struct net80211_device *dev )
  167. {
  168. struct rc80211_ctx *ctx = dev->rctl;
  169. int best_net_good = 0, best_rate = -1, i;
  170. for ( i = 0; i < dev->nr_rates; i++ ) {
  171. int net_good = rc80211_calc_net_goodness ( ctx, i );
  172. if ( net_good > best_net_good ||
  173. ( best_net_good > RC_GOODNESS_MIN &&
  174. net_good > RC_GOODNESS_MIN ) ) {
  175. best_net_good = net_good;
  176. best_rate = i;
  177. }
  178. }
  179. if ( best_rate >= 0 ) {
  180. int old_good = rc80211_calc_net_goodness ( ctx, dev->rate );
  181. if ( old_good != best_net_good )
  182. DBGC ( ctx, "802.11 RC %p switching from goodness "
  183. "%d to %d\n", ctx, old_good, best_net_good );
  184. ctx->started = 1;
  185. return best_rate;
  186. }
  187. return dev->rate;
  188. }
  189. /**
  190. * Set 802.11 device rate
  191. *
  192. * @v dev 802.11 device
  193. * @v rate_idx Index of rate to switch to
  194. *
  195. * This is a thin wrapper around net80211_set_rate_idx to insert a
  196. * debugging message where appropriate.
  197. */
  198. static inline void rc80211_set_rate ( struct net80211_device *dev,
  199. int rate_idx )
  200. {
  201. DBGC ( dev->rctl, "802.11 RC %p changing rate %d->%d Mbps\n", dev->rctl,
  202. dev->rates[dev->rate] / 10, dev->rates[rate_idx] / 10 );
  203. net80211_set_rate_idx ( dev, rate_idx );
  204. }
  205. /**
  206. * Check rate-control state and change rate if necessary
  207. *
  208. * @v dev 802.11 device
  209. */
  210. static void rc80211_maybe_set_new ( struct net80211_device *dev )
  211. {
  212. struct rc80211_ctx *ctx = dev->rctl;
  213. int net_good;
  214. net_good = rc80211_calc_net_goodness ( ctx, dev->rate );
  215. if ( ! ctx->started ) {
  216. rc80211_set_rate ( dev, rc80211_pick_best ( dev ) );
  217. return;
  218. }
  219. if ( net_good < 0 ) /* insufficient data */
  220. return;
  221. if ( net_good > RC_GOODNESS_MAX && dev->rate + 1 < dev->nr_rates ) {
  222. int higher = rc80211_calc_net_goodness ( ctx, dev->rate + 1 );
  223. if ( higher > net_good || higher < 0 )
  224. rc80211_set_rate ( dev, dev->rate + 1 );
  225. else
  226. rc80211_set_rate ( dev, rc80211_pick_best ( dev ) );
  227. }
  228. if ( net_good < RC_GOODNESS_MIN ) {
  229. rc80211_set_rate ( dev, rc80211_pick_best ( dev ) );
  230. }
  231. }
  232. /**
  233. * Update rate-control state
  234. *
  235. * @v dev 802.11 device
  236. * @v direction One of the direction constants TX or RX
  237. * @v rate_idx Index of rate at which packet was sent or received
  238. * @v retries Number of times packet was retried before success
  239. * @v failed If nonzero, the packet failed to get through
  240. */
  241. static void rc80211_update ( struct net80211_device *dev, int direction,
  242. int rate_idx, int retries, int failed )
  243. {
  244. struct rc80211_ctx *ctx = dev->rctl;
  245. u32 goodness = ctx->goodness[direction][rate_idx];
  246. if ( ctx->count[direction][rate_idx] < 16 )
  247. ctx->count[direction][rate_idx]++;
  248. goodness <<= 2;
  249. if ( failed )
  250. goodness |= RC_PKT_FAILED;
  251. else if ( retries > 1 )
  252. goodness |= RC_PKT_RETRIED_MULTI;
  253. else if ( retries )
  254. goodness |= RC_PKT_RETRIED_ONCE;
  255. else
  256. goodness |= RC_PKT_OK;
  257. ctx->goodness[direction][rate_idx] = goodness;
  258. ctx->packets++;
  259. rc80211_maybe_set_new ( dev );
  260. }
  261. /**
  262. * Update rate-control state for transmitted packet
  263. *
  264. * @v dev 802.11 device
  265. * @v retries Number of times packet was transmitted before success
  266. * @v rc Return status code for transmission
  267. */
  268. void rc80211_update_tx ( struct net80211_device *dev, int retries, int rc )
  269. {
  270. struct rc80211_ctx *ctx = dev->rctl;
  271. if ( ! ctx->started )
  272. return;
  273. rc80211_update ( dev, TX, dev->rate, retries, rc );
  274. /* Check if the last RC_TX_EMERG_FAIL packets have all failed */
  275. if ( ! ( ctx->goodness[TX][dev->rate] &
  276. ( ( 1 << ( 2 * RC_TX_EMERG_FAIL ) ) - 1 ) ) ) {
  277. if ( dev->rate == 0 )
  278. DBGC ( dev->rctl, "802.11 RC %p saw %d consecutive "
  279. "failed TX, but cannot lower rate any further\n",
  280. dev->rctl, RC_TX_EMERG_FAIL );
  281. else {
  282. DBGC ( dev->rctl, "802.11 RC %p lowering rate (%d->%d "
  283. "Mbps) due to %d consecutive TX failures\n",
  284. dev->rctl, dev->rates[dev->rate] / 10,
  285. dev->rates[dev->rate - 1] / 10,
  286. RC_TX_EMERG_FAIL );
  287. rc80211_set_rate ( dev, dev->rate - 1 );
  288. }
  289. }
  290. }
  291. /**
  292. * Update rate-control state for received packet
  293. *
  294. * @v dev 802.11 device
  295. * @v retry Whether the received packet had been retransmitted
  296. * @v rate Rate at which packet was received, in 100 kbps units
  297. */
  298. void rc80211_update_rx ( struct net80211_device *dev, int retry, u16 rate )
  299. {
  300. int ridx;
  301. for ( ridx = 0; ridx < dev->nr_rates && dev->rates[ridx] != rate;
  302. ridx++ )
  303. ;
  304. if ( ridx >= dev->nr_rates )
  305. return; /* couldn't find the rate */
  306. rc80211_update ( dev, RX, ridx, retry, 0 );
  307. }
  308. /**
  309. * Free rate-control context
  310. *
  311. * @v ctx Rate-control context
  312. */
  313. void rc80211_free ( struct rc80211_ctx *ctx )
  314. {
  315. free ( ctx );
  316. }