gateway_common.c 6.7 KB

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