Antonio Quartulli | 0b87393 | 2013-01-04 03:05:31 +0100 | [diff] [blame] | 1 | /* Copyright (C) 2009-2013 B.A.T.M.A.N. contributors: |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 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 |
Antonio Quartulli | ebf38fb | 2013-11-03 20:40:48 +0100 | [diff] [blame^] | 15 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | #include "main.h" |
| 19 | #include "gateway_common.h" |
| 20 | #include "gateway_client.h" |
| 21 | |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 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 | */ |
Sven Eckelmann | 8e714a5 | 2012-05-12 18:33:56 +0200 | [diff] [blame] | 32 | static bool batadv_parse_gw_bandwidth(struct net_device *net_dev, char *buff, |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 33 | uint32_t *down, uint32_t *up) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 34 | { |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 35 | enum batadv_bandwidth_units bw_unit_type = BATADV_BW_UNIT_KBIT; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 36 | char *slash_ptr, *tmp_ptr; |
Sven Eckelmann | 37a4065 | 2011-05-14 23:14:51 +0200 | [diff] [blame] | 37 | long ldown, lup; |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 38 | int ret; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 39 | |
| 40 | slash_ptr = strchr(buff, '/'); |
| 41 | if (slash_ptr) |
| 42 | *slash_ptr = 0; |
| 43 | |
| 44 | if (strlen(buff) > 4) { |
| 45 | tmp_ptr = buff + strlen(buff) - 4; |
| 46 | |
| 47 | if (strnicmp(tmp_ptr, "mbit", 4) == 0) |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 48 | bw_unit_type = BATADV_BW_UNIT_MBIT; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 49 | |
| 50 | if ((strnicmp(tmp_ptr, "kbit", 4) == 0) || |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 51 | (bw_unit_type == BATADV_BW_UNIT_MBIT)) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 52 | *tmp_ptr = '\0'; |
| 53 | } |
| 54 | |
Sven Eckelmann | 25a92b1 | 2011-09-30 13:32:01 +0200 | [diff] [blame] | 55 | ret = kstrtol(buff, 10, &ldown); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 56 | if (ret) { |
Sven Eckelmann | 3e34819 | 2012-05-16 20:23:22 +0200 | [diff] [blame] | 57 | batadv_err(net_dev, |
| 58 | "Download speed of gateway mode invalid: %s\n", |
| 59 | buff); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 60 | return false; |
| 61 | } |
| 62 | |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 63 | switch (bw_unit_type) { |
| 64 | case BATADV_BW_UNIT_MBIT: |
| 65 | *down = ldown * 10; |
| 66 | break; |
| 67 | case BATADV_BW_UNIT_KBIT: |
| 68 | default: |
| 69 | *down = ldown / 100; |
| 70 | break; |
| 71 | } |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 72 | |
| 73 | /* we also got some upload info */ |
| 74 | if (slash_ptr) { |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 75 | bw_unit_type = BATADV_BW_UNIT_KBIT; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 76 | |
| 77 | if (strlen(slash_ptr + 1) > 4) { |
| 78 | tmp_ptr = slash_ptr + 1 - 4 + strlen(slash_ptr + 1); |
| 79 | |
| 80 | if (strnicmp(tmp_ptr, "mbit", 4) == 0) |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 81 | bw_unit_type = BATADV_BW_UNIT_MBIT; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 82 | |
| 83 | if ((strnicmp(tmp_ptr, "kbit", 4) == 0) || |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 84 | (bw_unit_type == BATADV_BW_UNIT_MBIT)) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 85 | *tmp_ptr = '\0'; |
| 86 | } |
| 87 | |
Sven Eckelmann | 25a92b1 | 2011-09-30 13:32:01 +0200 | [diff] [blame] | 88 | ret = kstrtol(slash_ptr + 1, 10, &lup); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 89 | if (ret) { |
Sven Eckelmann | 3e34819 | 2012-05-16 20:23:22 +0200 | [diff] [blame] | 90 | batadv_err(net_dev, |
| 91 | "Upload speed of gateway mode invalid: %s\n", |
| 92 | slash_ptr + 1); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 93 | return false; |
| 94 | } |
| 95 | |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 96 | switch (bw_unit_type) { |
| 97 | case BATADV_BW_UNIT_MBIT: |
| 98 | *up = lup * 10; |
| 99 | break; |
| 100 | case BATADV_BW_UNIT_KBIT: |
| 101 | default: |
| 102 | *up = lup / 100; |
| 103 | break; |
| 104 | } |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | return true; |
| 108 | } |
| 109 | |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 110 | /** |
| 111 | * batadv_gw_tvlv_container_update - update the gw tvlv container after gateway |
| 112 | * setting change |
| 113 | * @bat_priv: the bat priv with all the soft interface information |
| 114 | */ |
| 115 | void batadv_gw_tvlv_container_update(struct batadv_priv *bat_priv) |
| 116 | { |
| 117 | struct batadv_tvlv_gateway_data gw; |
| 118 | uint32_t down, up; |
| 119 | char gw_mode; |
| 120 | |
| 121 | gw_mode = atomic_read(&bat_priv->gw_mode); |
| 122 | |
| 123 | switch (gw_mode) { |
| 124 | case BATADV_GW_MODE_OFF: |
| 125 | case BATADV_GW_MODE_CLIENT: |
| 126 | batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_GW, 1); |
| 127 | break; |
| 128 | case BATADV_GW_MODE_SERVER: |
| 129 | down = atomic_read(&bat_priv->gw.bandwidth_down); |
| 130 | up = atomic_read(&bat_priv->gw.bandwidth_up); |
| 131 | gw.bandwidth_down = htonl(down); |
| 132 | gw.bandwidth_up = htonl(up); |
| 133 | batadv_tvlv_container_register(bat_priv, BATADV_TVLV_GW, 1, |
| 134 | &gw, sizeof(gw)); |
| 135 | break; |
| 136 | } |
| 137 | } |
| 138 | |
Sven Eckelmann | 84d5e5e | 2012-05-12 02:09:30 +0200 | [diff] [blame] | 139 | ssize_t batadv_gw_bandwidth_set(struct net_device *net_dev, char *buff, |
| 140 | size_t count) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 141 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 142 | struct batadv_priv *bat_priv = netdev_priv(net_dev); |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 143 | uint32_t down_curr, up_curr, down_new = 0, up_new = 0; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 144 | bool ret; |
| 145 | |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 146 | down_curr = (unsigned int)atomic_read(&bat_priv->gw.bandwidth_down); |
| 147 | up_curr = (unsigned int)atomic_read(&bat_priv->gw.bandwidth_up); |
| 148 | |
| 149 | ret = batadv_parse_gw_bandwidth(net_dev, buff, &down_new, &up_new); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 150 | if (!ret) |
| 151 | goto end; |
| 152 | |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 153 | if (!down_new) |
| 154 | down_new = 1; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 155 | |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 156 | if (!up_new) |
| 157 | up_new = down_new / 5; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 158 | |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 159 | if (!up_new) |
| 160 | up_new = 1; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 161 | |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 162 | if ((down_curr == down_new) && (up_curr == up_new)) |
Marek Lindner | dafe94b | 2012-05-11 16:10:50 +0800 | [diff] [blame] | 163 | return count; |
| 164 | |
Sven Eckelmann | 7cf06bc | 2012-05-12 02:09:29 +0200 | [diff] [blame] | 165 | batadv_gw_deselect(bat_priv); |
Sven Eckelmann | 3e34819 | 2012-05-16 20:23:22 +0200 | [diff] [blame] | 166 | batadv_info(net_dev, |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 167 | "Changing gateway bandwidth from: '%u.%u/%u.%u MBit' to: '%u.%u/%u.%u MBit'\n", |
| 168 | down_curr / 10, down_curr % 10, up_curr / 10, up_curr % 10, |
| 169 | down_new / 10, down_new % 10, up_new / 10, up_new % 10); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 170 | |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 171 | atomic_set(&bat_priv->gw.bandwidth_down, down_new); |
| 172 | atomic_set(&bat_priv->gw.bandwidth_up, up_new); |
| 173 | batadv_gw_tvlv_container_update(bat_priv); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 174 | |
| 175 | end: |
| 176 | return count; |
| 177 | } |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 178 | |
| 179 | /** |
| 180 | * batadv_gw_tvlv_ogm_handler_v1 - process incoming gateway tvlv container |
| 181 | * @bat_priv: the bat priv with all the soft interface information |
| 182 | * @orig: the orig_node of the ogm |
| 183 | * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags) |
| 184 | * @tvlv_value: tvlv buffer containing the gateway data |
| 185 | * @tvlv_value_len: tvlv buffer length |
| 186 | */ |
| 187 | static void batadv_gw_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv, |
| 188 | struct batadv_orig_node *orig, |
| 189 | uint8_t flags, |
| 190 | void *tvlv_value, |
| 191 | uint16_t tvlv_value_len) |
| 192 | { |
| 193 | struct batadv_tvlv_gateway_data gateway, *gateway_ptr; |
| 194 | |
| 195 | /* only fetch the tvlv value if the handler wasn't called via the |
| 196 | * CIFNOTFND flag and if there is data to fetch |
| 197 | */ |
| 198 | if ((flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND) || |
| 199 | (tvlv_value_len < sizeof(gateway))) { |
| 200 | gateway.bandwidth_down = 0; |
| 201 | gateway.bandwidth_up = 0; |
| 202 | } else { |
| 203 | gateway_ptr = tvlv_value; |
| 204 | gateway.bandwidth_down = gateway_ptr->bandwidth_down; |
| 205 | gateway.bandwidth_up = gateway_ptr->bandwidth_up; |
| 206 | if ((gateway.bandwidth_down == 0) || |
| 207 | (gateway.bandwidth_up == 0)) { |
| 208 | gateway.bandwidth_down = 0; |
| 209 | gateway.bandwidth_up = 0; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | batadv_gw_node_update(bat_priv, orig, &gateway); |
| 214 | |
| 215 | /* restart gateway selection if fast or late switching was enabled */ |
| 216 | if ((gateway.bandwidth_down != 0) && |
| 217 | (atomic_read(&bat_priv->gw_mode) == BATADV_GW_MODE_CLIENT) && |
| 218 | (atomic_read(&bat_priv->gw_sel_class) > 2)) |
| 219 | batadv_gw_check_election(bat_priv, orig); |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * batadv_gw_init - initialise the gateway handling internals |
| 224 | * @bat_priv: the bat priv with all the soft interface information |
| 225 | */ |
| 226 | void batadv_gw_init(struct batadv_priv *bat_priv) |
| 227 | { |
| 228 | batadv_tvlv_handler_register(bat_priv, batadv_gw_tvlv_ogm_handler_v1, |
| 229 | NULL, BATADV_TVLV_GW, 1, |
| 230 | BATADV_TVLV_HANDLER_OGM_CIFNOTFND); |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * batadv_gw_free - free the gateway handling internals |
| 235 | * @bat_priv: the bat priv with all the soft interface information |
| 236 | */ |
| 237 | void batadv_gw_free(struct batadv_priv *bat_priv) |
| 238 | { |
| 239 | batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_GW, 1); |
| 240 | batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_GW, 1); |
| 241 | } |