1 /* Copyright (C) 2009-2014 B.A.T.M.A.N. contributors:
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.
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.
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/>.
19 #include "gateway_common.h"
20 #include "gateway_client.h"
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
30 * Returns false on parse error and true otherwise.
32 static bool batadv_parse_gw_bandwidth(struct net_device *net_dev, char *buff,
33 uint32_t *down, uint32_t *up)
35 enum batadv_bandwidth_units bw_unit_type = BATADV_BW_UNIT_KBIT;
36 char *slash_ptr, *tmp_ptr;
40 slash_ptr = strchr(buff, '/');
44 if (strlen(buff) > 4) {
45 tmp_ptr = buff + strlen(buff) - 4;
47 if (strnicmp(tmp_ptr, "mbit", 4) == 0)
48 bw_unit_type = BATADV_BW_UNIT_MBIT;
50 if ((strnicmp(tmp_ptr, "kbit", 4) == 0) ||
51 (bw_unit_type == BATADV_BW_UNIT_MBIT))
55 ret = kstrtol(buff, 10, &ldown);
58 "Download speed of gateway mode invalid: %s\n",
63 switch (bw_unit_type) {
64 case BATADV_BW_UNIT_MBIT:
67 case BATADV_BW_UNIT_KBIT:
73 /* we also got some upload info */
75 bw_unit_type = BATADV_BW_UNIT_KBIT;
77 if (strlen(slash_ptr + 1) > 4) {
78 tmp_ptr = slash_ptr + 1 - 4 + strlen(slash_ptr + 1);
80 if (strnicmp(tmp_ptr, "mbit", 4) == 0)
81 bw_unit_type = BATADV_BW_UNIT_MBIT;
83 if ((strnicmp(tmp_ptr, "kbit", 4) == 0) ||
84 (bw_unit_type == BATADV_BW_UNIT_MBIT))
88 ret = kstrtol(slash_ptr + 1, 10, &lup);
91 "Upload speed of gateway mode invalid: %s\n",
96 switch (bw_unit_type) {
97 case BATADV_BW_UNIT_MBIT:
100 case BATADV_BW_UNIT_KBIT:
111 * batadv_gw_tvlv_container_update - update the gw tvlv container after gateway
113 * @bat_priv: the bat priv with all the soft interface information
115 void batadv_gw_tvlv_container_update(struct batadv_priv *bat_priv)
117 struct batadv_tvlv_gateway_data gw;
121 gw_mode = atomic_read(&bat_priv->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);
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,
139 ssize_t batadv_gw_bandwidth_set(struct net_device *net_dev, char *buff,
142 struct batadv_priv *bat_priv = netdev_priv(net_dev);
143 uint32_t down_curr, up_curr, down_new = 0, up_new = 0;
146 down_curr = (unsigned int)atomic_read(&bat_priv->gw.bandwidth_down);
147 up_curr = (unsigned int)atomic_read(&bat_priv->gw.bandwidth_up);
149 ret = batadv_parse_gw_bandwidth(net_dev, buff, &down_new, &up_new);
157 up_new = down_new / 5;
162 if ((down_curr == down_new) && (up_curr == up_new))
165 batadv_gw_reselect(bat_priv);
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);
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);
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
187 static void batadv_gw_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
188 struct batadv_orig_node *orig,
191 uint16_t tvlv_value_len)
193 struct batadv_tvlv_gateway_data gateway, *gateway_ptr;
195 /* only fetch the tvlv value if the handler wasn't called via the
196 * CIFNOTFND flag and if there is data to fetch
198 if ((flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND) ||
199 (tvlv_value_len < sizeof(gateway))) {
200 gateway.bandwidth_down = 0;
201 gateway.bandwidth_up = 0;
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;
213 batadv_gw_node_update(bat_priv, orig, &gateway);
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);
223 * batadv_gw_init - initialise the gateway handling internals
224 * @bat_priv: the bat priv with all the soft interface information
226 void batadv_gw_init(struct batadv_priv *bat_priv)
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);
234 * batadv_gw_free - free the gateway handling internals
235 * @bat_priv: the bat priv with all the soft interface information
237 void batadv_gw_free(struct batadv_priv *bat_priv)
239 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_GW, 1);
240 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_GW, 1);