ieee80211_amrr.c 3.2 KB

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