gateway_common.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* Copyright (C) 2009-2013 B.A.T.M.A.N. contributors:
  2. *
  3. * Marek Lindner
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of version 2 of the GNU General Public
  7. * License as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA
  18. */
  19. #include "main.h"
  20. #include "gateway_common.h"
  21. #include "gateway_client.h"
  22. /* calculates the gateway class from kbit */
  23. static void batadv_kbit_to_gw_bandwidth(int down, int up, long *gw_srv_class)
  24. {
  25. int mdown = 0, tdown, tup, difference;
  26. uint8_t sbit, part;
  27. *gw_srv_class = 0;
  28. difference = 0x0FFFFFFF;
  29. /* test all downspeeds */
  30. for (sbit = 0; sbit < 2; sbit++) {
  31. for (part = 0; part < 16; part++) {
  32. tdown = 32 * (sbit + 2) * (1 << part);
  33. if (abs(tdown - down) < difference) {
  34. *gw_srv_class = (sbit << 7) + (part << 3);
  35. difference = abs(tdown - down);
  36. mdown = tdown;
  37. }
  38. }
  39. }
  40. /* test all upspeeds */
  41. difference = 0x0FFFFFFF;
  42. for (part = 0; part < 8; part++) {
  43. tup = ((part + 1) * (mdown)) / 8;
  44. if (abs(tup - up) < difference) {
  45. *gw_srv_class = (*gw_srv_class & 0xF8) | part;
  46. difference = abs(tup - up);
  47. }
  48. }
  49. }
  50. /* returns the up and downspeeds in kbit, calculated from the class */
  51. void batadv_gw_bandwidth_to_kbit(uint8_t gw_srv_class, int *down, int *up)
  52. {
  53. int sbit = (gw_srv_class & 0x80) >> 7;
  54. int dpart = (gw_srv_class & 0x78) >> 3;
  55. int upart = (gw_srv_class & 0x07);
  56. if (!gw_srv_class) {
  57. *down = 0;
  58. *up = 0;
  59. return;
  60. }
  61. *down = 32 * (sbit + 2) * (1 << dpart);
  62. *up = ((upart + 1) * (*down)) / 8;
  63. }
  64. static bool batadv_parse_gw_bandwidth(struct net_device *net_dev, char *buff,
  65. int *up, int *down)
  66. {
  67. int ret, multi = 1;
  68. char *slash_ptr, *tmp_ptr;
  69. long ldown, lup;
  70. slash_ptr = strchr(buff, '/');
  71. if (slash_ptr)
  72. *slash_ptr = 0;
  73. if (strlen(buff) > 4) {
  74. tmp_ptr = buff + strlen(buff) - 4;
  75. if (strnicmp(tmp_ptr, "mbit", 4) == 0)
  76. multi = 1024;
  77. if ((strnicmp(tmp_ptr, "kbit", 4) == 0) ||
  78. (multi > 1))
  79. *tmp_ptr = '\0';
  80. }
  81. ret = kstrtol(buff, 10, &ldown);
  82. if (ret) {
  83. batadv_err(net_dev,
  84. "Download speed of gateway mode invalid: %s\n",
  85. buff);
  86. return false;
  87. }
  88. *down = ldown * multi;
  89. /* we also got some upload info */
  90. if (slash_ptr) {
  91. multi = 1;
  92. if (strlen(slash_ptr + 1) > 4) {
  93. tmp_ptr = slash_ptr + 1 - 4 + strlen(slash_ptr + 1);
  94. if (strnicmp(tmp_ptr, "mbit", 4) == 0)
  95. multi = 1024;
  96. if ((strnicmp(tmp_ptr, "kbit", 4) == 0) ||
  97. (multi > 1))
  98. *tmp_ptr = '\0';
  99. }
  100. ret = kstrtol(slash_ptr + 1, 10, &lup);
  101. if (ret) {
  102. batadv_err(net_dev,
  103. "Upload speed of gateway mode invalid: %s\n",
  104. slash_ptr + 1);
  105. return false;
  106. }
  107. *up = lup * multi;
  108. }
  109. return true;
  110. }
  111. ssize_t batadv_gw_bandwidth_set(struct net_device *net_dev, char *buff,
  112. size_t count)
  113. {
  114. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  115. long gw_bandwidth_tmp = 0;
  116. int up = 0, down = 0;
  117. bool ret;
  118. ret = batadv_parse_gw_bandwidth(net_dev, buff, &up, &down);
  119. if (!ret)
  120. goto end;
  121. if ((!down) || (down < 256))
  122. down = 2000;
  123. if (!up)
  124. up = down / 5;
  125. batadv_kbit_to_gw_bandwidth(down, up, &gw_bandwidth_tmp);
  126. /* the gw bandwidth we guessed above might not match the given
  127. * speeds, hence we need to calculate it back to show the number
  128. * that is going to be propagated
  129. */
  130. batadv_gw_bandwidth_to_kbit((uint8_t)gw_bandwidth_tmp, &down, &up);
  131. if (atomic_read(&bat_priv->gw_bandwidth) == gw_bandwidth_tmp)
  132. return count;
  133. batadv_gw_deselect(bat_priv);
  134. batadv_info(net_dev,
  135. "Changing gateway bandwidth from: '%i' to: '%ld' (propagating: %d%s/%d%s)\n",
  136. atomic_read(&bat_priv->gw_bandwidth), gw_bandwidth_tmp,
  137. (down > 2048 ? down / 1024 : down),
  138. (down > 2048 ? "MBit" : "KBit"),
  139. (up > 2048 ? up / 1024 : up),
  140. (up > 2048 ? "MBit" : "KBit"));
  141. atomic_set(&bat_priv->gw_bandwidth, gw_bandwidth_tmp);
  142. end:
  143. return count;
  144. }