1 /* Copyright (C) 2009-2015 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/>.
18 #include "gateway_common.h"
21 #include <linux/atomic.h>
22 #include <linux/errno.h>
23 #include <linux/byteorder/generic.h>
24 #include <linux/kernel.h>
25 #include <linux/math64.h>
26 #include <linux/netdevice.h>
27 #include <linux/stddef.h>
28 #include <linux/string.h>
30 #include "gateway_client.h"
34 * batadv_parse_throughput - parse supplied string buffer to extract throughput
36 * @net_dev: the soft interface net device
37 * @buff: string buffer to parse
38 * @description: text shown when throughput string cannot be parsed
39 * @throughput: pointer holding the returned throughput information
41 * Returns false on parse error and true otherwise.
43 static bool batadv_parse_throughput(struct net_device
*net_dev
, char *buff
,
44 const char *description
, u32
*throughput
)
46 enum batadv_bandwidth_units bw_unit_type
= BATADV_BW_UNIT_KBIT
;
51 if (strlen(buff
) > 4) {
52 tmp_ptr
= buff
+ strlen(buff
) - 4;
54 if (strncasecmp(tmp_ptr
, "mbit", 4) == 0)
55 bw_unit_type
= BATADV_BW_UNIT_MBIT
;
57 if ((strncasecmp(tmp_ptr
, "kbit", 4) == 0) ||
58 (bw_unit_type
== BATADV_BW_UNIT_MBIT
))
62 ret
= kstrtou64(buff
, 10, <hroughput
);
65 "Invalid throughput speed for %s: %s\n",
70 switch (bw_unit_type
) {
71 case BATADV_BW_UNIT_MBIT
:
72 /* prevent overflow */
73 if (U64_MAX
/ 10 < lthroughput
) {
75 "Throughput speed for %s too large: %s\n",
82 case BATADV_BW_UNIT_KBIT
:
84 lthroughput
= div_u64(lthroughput
, 100);
88 if (lthroughput
> U32_MAX
) {
90 "Throughput speed for %s too large: %s\n",
95 *throughput
= lthroughput
;
101 * batadv_parse_gw_bandwidth - parse supplied string buffer to extract download
102 * and upload bandwidth information
103 * @net_dev: the soft interface net device
104 * @buff: string buffer to parse
105 * @down: pointer holding the returned download bandwidth information
106 * @up: pointer holding the returned upload bandwidth information
108 * Return: false on parse error and true otherwise.
110 static bool batadv_parse_gw_bandwidth(struct net_device
*net_dev
, char *buff
,
116 slash_ptr
= strchr(buff
, '/');
120 ret
= batadv_parse_throughput(net_dev
, buff
, "download gateway speed",
125 /* we also got some upload info */
127 ret
= batadv_parse_throughput(net_dev
, slash_ptr
+ 1,
128 "upload gateway speed", up
);
137 * batadv_gw_tvlv_container_update - update the gw tvlv container after gateway
139 * @bat_priv: the bat priv with all the soft interface information
141 void batadv_gw_tvlv_container_update(struct batadv_priv
*bat_priv
)
143 struct batadv_tvlv_gateway_data gw
;
147 gw_mode
= atomic_read(&bat_priv
->gw_mode
);
150 case BATADV_GW_MODE_OFF
:
151 case BATADV_GW_MODE_CLIENT
:
152 batadv_tvlv_container_unregister(bat_priv
, BATADV_TVLV_GW
, 1);
154 case BATADV_GW_MODE_SERVER
:
155 down
= atomic_read(&bat_priv
->gw
.bandwidth_down
);
156 up
= atomic_read(&bat_priv
->gw
.bandwidth_up
);
157 gw
.bandwidth_down
= htonl(down
);
158 gw
.bandwidth_up
= htonl(up
);
159 batadv_tvlv_container_register(bat_priv
, BATADV_TVLV_GW
, 1,
165 ssize_t
batadv_gw_bandwidth_set(struct net_device
*net_dev
, char *buff
,
168 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
175 down_curr
= (unsigned int)atomic_read(&bat_priv
->gw
.bandwidth_down
);
176 up_curr
= (unsigned int)atomic_read(&bat_priv
->gw
.bandwidth_up
);
178 ret
= batadv_parse_gw_bandwidth(net_dev
, buff
, &down_new
, &up_new
);
186 up_new
= down_new
/ 5;
191 if ((down_curr
== down_new
) && (up_curr
== up_new
))
194 batadv_gw_reselect(bat_priv
);
196 "Changing gateway bandwidth from: '%u.%u/%u.%u MBit' to: '%u.%u/%u.%u MBit'\n",
197 down_curr
/ 10, down_curr
% 10, up_curr
/ 10, up_curr
% 10,
198 down_new
/ 10, down_new
% 10, up_new
/ 10, up_new
% 10);
200 atomic_set(&bat_priv
->gw
.bandwidth_down
, down_new
);
201 atomic_set(&bat_priv
->gw
.bandwidth_up
, up_new
);
202 batadv_gw_tvlv_container_update(bat_priv
);
208 * batadv_gw_tvlv_ogm_handler_v1 - process incoming gateway tvlv container
209 * @bat_priv: the bat priv with all the soft interface information
210 * @orig: the orig_node of the ogm
211 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
212 * @tvlv_value: tvlv buffer containing the gateway data
213 * @tvlv_value_len: tvlv buffer length
215 static void batadv_gw_tvlv_ogm_handler_v1(struct batadv_priv
*bat_priv
,
216 struct batadv_orig_node
*orig
,
218 void *tvlv_value
, u16 tvlv_value_len
)
220 struct batadv_tvlv_gateway_data gateway
, *gateway_ptr
;
222 /* only fetch the tvlv value if the handler wasn't called via the
223 * CIFNOTFND flag and if there is data to fetch
225 if ((flags
& BATADV_TVLV_HANDLER_OGM_CIFNOTFND
) ||
226 (tvlv_value_len
< sizeof(gateway
))) {
227 gateway
.bandwidth_down
= 0;
228 gateway
.bandwidth_up
= 0;
230 gateway_ptr
= tvlv_value
;
231 gateway
.bandwidth_down
= gateway_ptr
->bandwidth_down
;
232 gateway
.bandwidth_up
= gateway_ptr
->bandwidth_up
;
233 if ((gateway
.bandwidth_down
== 0) ||
234 (gateway
.bandwidth_up
== 0)) {
235 gateway
.bandwidth_down
= 0;
236 gateway
.bandwidth_up
= 0;
240 batadv_gw_node_update(bat_priv
, orig
, &gateway
);
242 /* restart gateway selection if fast or late switching was enabled */
243 if ((gateway
.bandwidth_down
!= 0) &&
244 (atomic_read(&bat_priv
->gw_mode
) == BATADV_GW_MODE_CLIENT
) &&
245 (atomic_read(&bat_priv
->gw_sel_class
) > 2))
246 batadv_gw_check_election(bat_priv
, orig
);
250 * batadv_gw_init - initialise the gateway handling internals
251 * @bat_priv: the bat priv with all the soft interface information
253 void batadv_gw_init(struct batadv_priv
*bat_priv
)
255 batadv_tvlv_handler_register(bat_priv
, batadv_gw_tvlv_ogm_handler_v1
,
256 NULL
, BATADV_TVLV_GW
, 1,
257 BATADV_TVLV_HANDLER_OGM_CIFNOTFND
);
261 * batadv_gw_free - free the gateway handling internals
262 * @bat_priv: the bat priv with all the soft interface information
264 void batadv_gw_free(struct batadv_priv
*bat_priv
)
266 batadv_tvlv_container_unregister(bat_priv
, BATADV_TVLV_GW
, 1);
267 batadv_tvlv_handler_unregister(bat_priv
, BATADV_TVLV_GW
, 1);