gateway_common.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /* Copyright (C) 2009-2014 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, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "main.h"
  18. #include "gateway_common.h"
  19. #include "gateway_client.h"
  20. /**
  21. * batadv_parse_gw_bandwidth - parse supplied string buffer to extract download
  22. * and upload bandwidth information
  23. * @net_dev: the soft interface net device
  24. * @buff: string buffer to parse
  25. * @down: pointer holding the returned download bandwidth information
  26. * @up: pointer holding the returned upload bandwidth information
  27. *
  28. * Returns false on parse error and true otherwise.
  29. */
  30. static bool batadv_parse_gw_bandwidth(struct net_device *net_dev, char *buff,
  31. uint32_t *down, uint32_t *up)
  32. {
  33. enum batadv_bandwidth_units bw_unit_type = BATADV_BW_UNIT_KBIT;
  34. char *slash_ptr, *tmp_ptr;
  35. long ldown, lup;
  36. int ret;
  37. slash_ptr = strchr(buff, '/');
  38. if (slash_ptr)
  39. *slash_ptr = 0;
  40. if (strlen(buff) > 4) {
  41. tmp_ptr = buff + strlen(buff) - 4;
  42. if (strnicmp(tmp_ptr, "mbit", 4) == 0)
  43. bw_unit_type = BATADV_BW_UNIT_MBIT;
  44. if ((strnicmp(tmp_ptr, "kbit", 4) == 0) ||
  45. (bw_unit_type == BATADV_BW_UNIT_MBIT))
  46. *tmp_ptr = '\0';
  47. }
  48. ret = kstrtol(buff, 10, &ldown);
  49. if (ret) {
  50. batadv_err(net_dev,
  51. "Download speed of gateway mode invalid: %s\n",
  52. buff);
  53. return false;
  54. }
  55. switch (bw_unit_type) {
  56. case BATADV_BW_UNIT_MBIT:
  57. *down = ldown * 10;
  58. break;
  59. case BATADV_BW_UNIT_KBIT:
  60. default:
  61. *down = ldown / 100;
  62. break;
  63. }
  64. /* we also got some upload info */
  65. if (slash_ptr) {
  66. bw_unit_type = BATADV_BW_UNIT_KBIT;
  67. if (strlen(slash_ptr + 1) > 4) {
  68. tmp_ptr = slash_ptr + 1 - 4 + strlen(slash_ptr + 1);
  69. if (strnicmp(tmp_ptr, "mbit", 4) == 0)
  70. bw_unit_type = BATADV_BW_UNIT_MBIT;
  71. if ((strnicmp(tmp_ptr, "kbit", 4) == 0) ||
  72. (bw_unit_type == BATADV_BW_UNIT_MBIT))
  73. *tmp_ptr = '\0';
  74. }
  75. ret = kstrtol(slash_ptr + 1, 10, &lup);
  76. if (ret) {
  77. batadv_err(net_dev,
  78. "Upload speed of gateway mode invalid: %s\n",
  79. slash_ptr + 1);
  80. return false;
  81. }
  82. switch (bw_unit_type) {
  83. case BATADV_BW_UNIT_MBIT:
  84. *up = lup * 10;
  85. break;
  86. case BATADV_BW_UNIT_KBIT:
  87. default:
  88. *up = lup / 100;
  89. break;
  90. }
  91. }
  92. return true;
  93. }
  94. /**
  95. * batadv_gw_tvlv_container_update - update the gw tvlv container after gateway
  96. * setting change
  97. * @bat_priv: the bat priv with all the soft interface information
  98. */
  99. void batadv_gw_tvlv_container_update(struct batadv_priv *bat_priv)
  100. {
  101. struct batadv_tvlv_gateway_data gw;
  102. uint32_t down, up;
  103. char gw_mode;
  104. gw_mode = atomic_read(&bat_priv->gw_mode);
  105. switch (gw_mode) {
  106. case BATADV_GW_MODE_OFF:
  107. case BATADV_GW_MODE_CLIENT:
  108. batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_GW, 1);
  109. break;
  110. case BATADV_GW_MODE_SERVER:
  111. down = atomic_read(&bat_priv->gw.bandwidth_down);
  112. up = atomic_read(&bat_priv->gw.bandwidth_up);
  113. gw.bandwidth_down = htonl(down);
  114. gw.bandwidth_up = htonl(up);
  115. batadv_tvlv_container_register(bat_priv, BATADV_TVLV_GW, 1,
  116. &gw, sizeof(gw));
  117. break;
  118. }
  119. }
  120. ssize_t batadv_gw_bandwidth_set(struct net_device *net_dev, char *buff,
  121. size_t count)
  122. {
  123. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  124. uint32_t down_curr, up_curr, down_new = 0, up_new = 0;
  125. bool ret;
  126. down_curr = (unsigned int)atomic_read(&bat_priv->gw.bandwidth_down);
  127. up_curr = (unsigned int)atomic_read(&bat_priv->gw.bandwidth_up);
  128. ret = batadv_parse_gw_bandwidth(net_dev, buff, &down_new, &up_new);
  129. if (!ret)
  130. goto end;
  131. if (!down_new)
  132. down_new = 1;
  133. if (!up_new)
  134. up_new = down_new / 5;
  135. if (!up_new)
  136. up_new = 1;
  137. if ((down_curr == down_new) && (up_curr == up_new))
  138. return count;
  139. batadv_gw_reselect(bat_priv);
  140. batadv_info(net_dev,
  141. "Changing gateway bandwidth from: '%u.%u/%u.%u MBit' to: '%u.%u/%u.%u MBit'\n",
  142. down_curr / 10, down_curr % 10, up_curr / 10, up_curr % 10,
  143. down_new / 10, down_new % 10, up_new / 10, up_new % 10);
  144. atomic_set(&bat_priv->gw.bandwidth_down, down_new);
  145. atomic_set(&bat_priv->gw.bandwidth_up, up_new);
  146. batadv_gw_tvlv_container_update(bat_priv);
  147. end:
  148. return count;
  149. }
  150. /**
  151. * batadv_gw_tvlv_ogm_handler_v1 - process incoming gateway tvlv container
  152. * @bat_priv: the bat priv with all the soft interface information
  153. * @orig: the orig_node of the ogm
  154. * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
  155. * @tvlv_value: tvlv buffer containing the gateway data
  156. * @tvlv_value_len: tvlv buffer length
  157. */
  158. static void batadv_gw_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
  159. struct batadv_orig_node *orig,
  160. uint8_t flags,
  161. void *tvlv_value,
  162. uint16_t tvlv_value_len)
  163. {
  164. struct batadv_tvlv_gateway_data gateway, *gateway_ptr;
  165. /* only fetch the tvlv value if the handler wasn't called via the
  166. * CIFNOTFND flag and if there is data to fetch
  167. */
  168. if ((flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND) ||
  169. (tvlv_value_len < sizeof(gateway))) {
  170. gateway.bandwidth_down = 0;
  171. gateway.bandwidth_up = 0;
  172. } else {
  173. gateway_ptr = tvlv_value;
  174. gateway.bandwidth_down = gateway_ptr->bandwidth_down;
  175. gateway.bandwidth_up = gateway_ptr->bandwidth_up;
  176. if ((gateway.bandwidth_down == 0) ||
  177. (gateway.bandwidth_up == 0)) {
  178. gateway.bandwidth_down = 0;
  179. gateway.bandwidth_up = 0;
  180. }
  181. }
  182. batadv_gw_node_update(bat_priv, orig, &gateway);
  183. /* restart gateway selection if fast or late switching was enabled */
  184. if ((gateway.bandwidth_down != 0) &&
  185. (atomic_read(&bat_priv->gw_mode) == BATADV_GW_MODE_CLIENT) &&
  186. (atomic_read(&bat_priv->gw_sel_class) > 2))
  187. batadv_gw_check_election(bat_priv, orig);
  188. }
  189. /**
  190. * batadv_gw_init - initialise the gateway handling internals
  191. * @bat_priv: the bat priv with all the soft interface information
  192. */
  193. void batadv_gw_init(struct batadv_priv *bat_priv)
  194. {
  195. batadv_tvlv_handler_register(bat_priv, batadv_gw_tvlv_ogm_handler_v1,
  196. NULL, BATADV_TVLV_GW, 1,
  197. BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
  198. }
  199. /**
  200. * batadv_gw_free - free the gateway handling internals
  201. * @bat_priv: the bat priv with all the soft interface information
  202. */
  203. void batadv_gw_free(struct batadv_priv *bat_priv)
  204. {
  205. batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_GW, 1);
  206. batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_GW, 1);
  207. }