gateway_common.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright (C) 2009-2011 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. char sbit = (gw_srv_class & 0x80) >> 7;
  56. char dpart = (gw_srv_class & 0x78) >> 3;
  57. char 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. long *up, long *down)
  68. {
  69. int ret, multi = 1;
  70. char *slash_ptr, *tmp_ptr;
  71. slash_ptr = strchr(buff, '/');
  72. if (slash_ptr)
  73. *slash_ptr = 0;
  74. if (strlen(buff) > 4) {
  75. tmp_ptr = buff + strlen(buff) - 4;
  76. if (strnicmp(tmp_ptr, "mbit", 4) == 0)
  77. multi = 1024;
  78. if ((strnicmp(tmp_ptr, "kbit", 4) == 0) ||
  79. (multi > 1))
  80. *tmp_ptr = '\0';
  81. }
  82. ret = strict_strtoul(buff, 10, down);
  83. if (ret) {
  84. bat_err(net_dev,
  85. "Download speed of gateway mode invalid: %s\n",
  86. buff);
  87. return false;
  88. }
  89. *down *= multi;
  90. /* we also got some upload info */
  91. if (slash_ptr) {
  92. multi = 1;
  93. if (strlen(slash_ptr + 1) > 4) {
  94. tmp_ptr = slash_ptr + 1 - 4 + strlen(slash_ptr + 1);
  95. if (strnicmp(tmp_ptr, "mbit", 4) == 0)
  96. multi = 1024;
  97. if ((strnicmp(tmp_ptr, "kbit", 4) == 0) ||
  98. (multi > 1))
  99. *tmp_ptr = '\0';
  100. }
  101. ret = strict_strtoul(slash_ptr + 1, 10, up);
  102. if (ret) {
  103. bat_err(net_dev,
  104. "Upload speed of gateway mode invalid: "
  105. "%s\n", slash_ptr + 1);
  106. return false;
  107. }
  108. *up *= multi;
  109. }
  110. return true;
  111. }
  112. ssize_t gw_bandwidth_set(struct net_device *net_dev, char *buff, size_t count)
  113. {
  114. struct bat_priv *bat_priv = netdev_priv(net_dev);
  115. long gw_bandwidth_tmp = 0, up = 0, down = 0;
  116. bool ret;
  117. ret = parse_gw_bandwidth(net_dev, buff, &up, &down);
  118. if (!ret)
  119. goto end;
  120. if ((!down) || (down < 256))
  121. down = 2000;
  122. if (!up)
  123. up = down / 5;
  124. kbit_to_gw_bandwidth(down, up, &gw_bandwidth_tmp);
  125. /**
  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. gw_bandwidth_to_kbit((uint8_t)gw_bandwidth_tmp,
  131. (int *)&down, (int *)&up);
  132. gw_deselect(bat_priv);
  133. bat_info(net_dev, "Changing gateway bandwidth from: '%i' to: '%ld' "
  134. "(propagating: %ld%s/%ld%s)\n",
  135. atomic_read(&bat_priv->gw_bandwidth), gw_bandwidth_tmp,
  136. (down > 2048 ? down / 1024 : down),
  137. (down > 2048 ? "MBit" : "KBit"),
  138. (up > 2048 ? up / 1024 : up),
  139. (up > 2048 ? "MBit" : "KBit"));
  140. atomic_set(&bat_priv->gw_bandwidth, gw_bandwidth_tmp);
  141. end:
  142. return count;
  143. }