gateway_common.c 4.1 KB

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