Commit d737ccbe authored by Sven Eckelmann's avatar Sven Eckelmann Committed by Antonio Quartulli

batman-adv: Add function to convert string to batadv throughput

The code to convert the throughput information from a string to the
batman-adv internal (100Kibit/s) representation is duplicated in
batadv_parse_gw_bandwidth. Move this functionality to its own function
batadv_parse_throughput to reduce the code complexity.
Signed-off-by: default avatarSven Eckelmann <sven@open-mesh.com>
Signed-off-by: default avatarMarek Lindner <mareklindner@neomailbox.ch>
Signed-off-by: default avatarAntonio Quartulli <a@unstable.cc>
parent 9e728e84
...@@ -31,27 +31,23 @@ ...@@ -31,27 +31,23 @@
#include "packet.h" #include "packet.h"
/** /**
* batadv_parse_gw_bandwidth - parse supplied string buffer to extract download * batadv_parse_throughput - parse supplied string buffer to extract throughput
* and upload bandwidth information * information
* @net_dev: the soft interface net device * @net_dev: the soft interface net device
* @buff: string buffer to parse * @buff: string buffer to parse
* @down: pointer holding the returned download bandwidth information * @description: text shown when throughput string cannot be parsed
* @up: pointer holding the returned upload bandwidth information * @throughput: pointer holding the returned throughput information
* *
* Returns false on parse error and true otherwise. * Returns false on parse error and true otherwise.
*/ */
static bool batadv_parse_gw_bandwidth(struct net_device *net_dev, char *buff, static bool batadv_parse_throughput(struct net_device *net_dev, char *buff,
u32 *down, u32 *up) const char *description, u32 *throughput)
{ {
enum batadv_bandwidth_units bw_unit_type = BATADV_BW_UNIT_KBIT; enum batadv_bandwidth_units bw_unit_type = BATADV_BW_UNIT_KBIT;
char *slash_ptr, *tmp_ptr; u64 lthroughput;
u64 ldown, lup; char *tmp_ptr;
int ret; int ret;
slash_ptr = strchr(buff, '/');
if (slash_ptr)
*slash_ptr = 0;
if (strlen(buff) > 4) { if (strlen(buff) > 4) {
tmp_ptr = buff + strlen(buff) - 4; tmp_ptr = buff + strlen(buff) - 4;
...@@ -63,92 +59,77 @@ static bool batadv_parse_gw_bandwidth(struct net_device *net_dev, char *buff, ...@@ -63,92 +59,77 @@ static bool batadv_parse_gw_bandwidth(struct net_device *net_dev, char *buff,
*tmp_ptr = '\0'; *tmp_ptr = '\0';
} }
ret = kstrtou64(buff, 10, &ldown); ret = kstrtou64(buff, 10, &lthroughput);
if (ret) { if (ret) {
batadv_err(net_dev, batadv_err(net_dev,
"Download speed of gateway mode invalid: %s\n", "Invalid throughput speed for %s: %s\n",
buff); description, buff);
return false; return false;
} }
switch (bw_unit_type) { switch (bw_unit_type) {
case BATADV_BW_UNIT_MBIT: case BATADV_BW_UNIT_MBIT:
/* prevent overflow */ /* prevent overflow */
if (U64_MAX / 10 < ldown) { if (U64_MAX / 10 < lthroughput) {
batadv_err(net_dev, batadv_err(net_dev,
"Download speed of gateway mode too large: %s\n", "Throughput speed for %s too large: %s\n",
buff); description, buff);
return false; return false;
} }
ldown *= 10; lthroughput *= 10;
break; break;
case BATADV_BW_UNIT_KBIT: case BATADV_BW_UNIT_KBIT:
default: default:
ldown = div_u64(ldown, 100); lthroughput = div_u64(lthroughput, 100);
break; break;
} }
if (U32_MAX < ldown) { if (lthroughput > U32_MAX) {
batadv_err(net_dev, batadv_err(net_dev,
"Download speed of gateway mode too large: %s\n", "Throughput speed for %s too large: %s\n",
buff); description, buff);
return false; return false;
} }
*down = ldown; *throughput = lthroughput;
/* we also got some upload info */ return true;
if (slash_ptr) { }
bw_unit_type = BATADV_BW_UNIT_KBIT;
if (strlen(slash_ptr + 1) > 4) {
tmp_ptr = slash_ptr + 1 - 4 + strlen(slash_ptr + 1);
if (strncasecmp(tmp_ptr, "mbit", 4) == 0)
bw_unit_type = BATADV_BW_UNIT_MBIT;
if ((strncasecmp(tmp_ptr, "kbit", 4) == 0) || /**
(bw_unit_type == BATADV_BW_UNIT_MBIT)) * batadv_parse_gw_bandwidth - parse supplied string buffer to extract download
*tmp_ptr = '\0'; * and upload bandwidth information
} * @net_dev: the soft interface net device
* @buff: string buffer to parse
* @down: pointer holding the returned download bandwidth information
* @up: pointer holding the returned upload bandwidth information
*
* Return: false on parse error and true otherwise.
*/
static bool batadv_parse_gw_bandwidth(struct net_device *net_dev, char *buff,
u32 *down, u32 *up)
{
char *slash_ptr;
bool ret;
ret = kstrtou64(slash_ptr + 1, 10, &lup); slash_ptr = strchr(buff, '/');
if (ret) { if (slash_ptr)
batadv_err(net_dev, *slash_ptr = 0;
"Upload speed of gateway mode invalid: %s\n",
slash_ptr + 1);
return false;
}
switch (bw_unit_type) { ret = batadv_parse_throughput(net_dev, buff, "download gateway speed",
case BATADV_BW_UNIT_MBIT: down);
/* prevent overflow */ if (!ret)
if (U64_MAX / 10 < lup) {
batadv_err(net_dev,
"Upload speed of gateway mode too large: %s\n",
slash_ptr + 1);
return false; return false;
}
lup *= 10; /* we also got some upload info */
break; if (slash_ptr) {
case BATADV_BW_UNIT_KBIT: ret = batadv_parse_throughput(net_dev, slash_ptr + 1,
default: "upload gateway speed", up);
lup = div_u64(lup, 100); if (!ret)
break;
}
if (U32_MAX < lup) {
batadv_err(net_dev,
"Upload speed of gateway mode too large: %s\n",
slash_ptr + 1);
return false; return false;
} }
*up = lup;
}
return true; return true;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment