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 |
| 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 |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #include "main.h" |
| 21 | #include "gateway_common.h" |
| 22 | #include "gateway_client.h" |
| 23 | |
| 24 | /* calculates the gateway class from kbit */ |
Sven Eckelmann | 8e714a5 | 2012-05-12 18:33:56 +0200 | [diff] [blame] | 25 | static void batadv_kbit_to_gw_bandwidth(int down, int up, long *gw_srv_class) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 26 | { |
| 27 | int mdown = 0, tdown, tup, difference; |
| 28 | uint8_t sbit, part; |
| 29 | |
| 30 | *gw_srv_class = 0; |
| 31 | difference = 0x0FFFFFFF; |
| 32 | |
| 33 | /* test all downspeeds */ |
| 34 | for (sbit = 0; sbit < 2; sbit++) { |
| 35 | for (part = 0; part < 16; part++) { |
| 36 | tdown = 32 * (sbit + 2) * (1 << part); |
| 37 | |
| 38 | if (abs(tdown - down) < difference) { |
| 39 | *gw_srv_class = (sbit << 7) + (part << 3); |
| 40 | difference = abs(tdown - down); |
| 41 | mdown = tdown; |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | /* test all upspeeds */ |
| 47 | difference = 0x0FFFFFFF; |
| 48 | |
| 49 | for (part = 0; part < 8; part++) { |
| 50 | tup = ((part + 1) * (mdown)) / 8; |
| 51 | |
| 52 | if (abs(tup - up) < difference) { |
| 53 | *gw_srv_class = (*gw_srv_class & 0xF8) | part; |
| 54 | difference = abs(tup - up); |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /* returns the up and downspeeds in kbit, calculated from the class */ |
Sven Eckelmann | 84d5e5e | 2012-05-12 02:09:30 +0200 | [diff] [blame] | 60 | void batadv_gw_bandwidth_to_kbit(uint8_t gw_srv_class, int *down, int *up) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 61 | { |
Sven Eckelmann | b4e1705 | 2011-06-15 09:41:37 +0200 | [diff] [blame] | 62 | int sbit = (gw_srv_class & 0x80) >> 7; |
| 63 | int dpart = (gw_srv_class & 0x78) >> 3; |
| 64 | int upart = (gw_srv_class & 0x07); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 65 | |
| 66 | if (!gw_srv_class) { |
| 67 | *down = 0; |
| 68 | *up = 0; |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | *down = 32 * (sbit + 2) * (1 << dpart); |
| 73 | *up = ((upart + 1) * (*down)) / 8; |
| 74 | } |
| 75 | |
Sven Eckelmann | 8e714a5 | 2012-05-12 18:33:56 +0200 | [diff] [blame] | 76 | static bool batadv_parse_gw_bandwidth(struct net_device *net_dev, char *buff, |
| 77 | int *up, int *down) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 78 | { |
| 79 | int ret, multi = 1; |
| 80 | char *slash_ptr, *tmp_ptr; |
Sven Eckelmann | 37a4065 | 2011-05-14 23:14:51 +0200 | [diff] [blame] | 81 | long ldown, lup; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 82 | |
| 83 | slash_ptr = strchr(buff, '/'); |
| 84 | if (slash_ptr) |
| 85 | *slash_ptr = 0; |
| 86 | |
| 87 | if (strlen(buff) > 4) { |
| 88 | tmp_ptr = buff + strlen(buff) - 4; |
| 89 | |
| 90 | if (strnicmp(tmp_ptr, "mbit", 4) == 0) |
| 91 | multi = 1024; |
| 92 | |
| 93 | if ((strnicmp(tmp_ptr, "kbit", 4) == 0) || |
Sven Eckelmann | 7c64fd9 | 2012-02-28 10:55:36 +0100 | [diff] [blame] | 94 | (multi > 1)) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 95 | *tmp_ptr = '\0'; |
| 96 | } |
| 97 | |
Sven Eckelmann | 25a92b1 | 2011-09-30 13:32:01 +0200 | [diff] [blame] | 98 | ret = kstrtol(buff, 10, &ldown); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 99 | if (ret) { |
Sven Eckelmann | 3e34819 | 2012-05-16 20:23:22 +0200 | [diff] [blame] | 100 | batadv_err(net_dev, |
| 101 | "Download speed of gateway mode invalid: %s\n", |
| 102 | buff); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 103 | return false; |
| 104 | } |
| 105 | |
Sven Eckelmann | 37a4065 | 2011-05-14 23:14:51 +0200 | [diff] [blame] | 106 | *down = ldown * multi; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 107 | |
| 108 | /* we also got some upload info */ |
| 109 | if (slash_ptr) { |
| 110 | multi = 1; |
| 111 | |
| 112 | if (strlen(slash_ptr + 1) > 4) { |
| 113 | tmp_ptr = slash_ptr + 1 - 4 + strlen(slash_ptr + 1); |
| 114 | |
| 115 | if (strnicmp(tmp_ptr, "mbit", 4) == 0) |
| 116 | multi = 1024; |
| 117 | |
| 118 | if ((strnicmp(tmp_ptr, "kbit", 4) == 0) || |
Sven Eckelmann | 7c64fd9 | 2012-02-28 10:55:36 +0100 | [diff] [blame] | 119 | (multi > 1)) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 120 | *tmp_ptr = '\0'; |
| 121 | } |
| 122 | |
Sven Eckelmann | 25a92b1 | 2011-09-30 13:32:01 +0200 | [diff] [blame] | 123 | ret = kstrtol(slash_ptr + 1, 10, &lup); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 124 | if (ret) { |
Sven Eckelmann | 3e34819 | 2012-05-16 20:23:22 +0200 | [diff] [blame] | 125 | batadv_err(net_dev, |
| 126 | "Upload speed of gateway mode invalid: %s\n", |
| 127 | slash_ptr + 1); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 128 | return false; |
| 129 | } |
| 130 | |
Sven Eckelmann | 37a4065 | 2011-05-14 23:14:51 +0200 | [diff] [blame] | 131 | *up = lup * multi; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | return true; |
| 135 | } |
| 136 | |
Sven Eckelmann | 84d5e5e | 2012-05-12 02:09:30 +0200 | [diff] [blame] | 137 | ssize_t batadv_gw_bandwidth_set(struct net_device *net_dev, char *buff, |
| 138 | size_t count) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 139 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 140 | struct batadv_priv *bat_priv = netdev_priv(net_dev); |
Sven Eckelmann | 37a4065 | 2011-05-14 23:14:51 +0200 | [diff] [blame] | 141 | long gw_bandwidth_tmp = 0; |
| 142 | int up = 0, down = 0; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 143 | bool ret; |
| 144 | |
Sven Eckelmann | 8e714a5 | 2012-05-12 18:33:56 +0200 | [diff] [blame] | 145 | ret = batadv_parse_gw_bandwidth(net_dev, buff, &up, &down); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 146 | if (!ret) |
| 147 | goto end; |
| 148 | |
| 149 | if ((!down) || (down < 256)) |
| 150 | down = 2000; |
| 151 | |
| 152 | if (!up) |
| 153 | up = down / 5; |
| 154 | |
Sven Eckelmann | 8e714a5 | 2012-05-12 18:33:56 +0200 | [diff] [blame] | 155 | batadv_kbit_to_gw_bandwidth(down, up, &gw_bandwidth_tmp); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 156 | |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 157 | /* the gw bandwidth we guessed above might not match the given |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 158 | * speeds, hence we need to calculate it back to show the number |
| 159 | * that is going to be propagated |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 160 | */ |
Sven Eckelmann | 84d5e5e | 2012-05-12 02:09:30 +0200 | [diff] [blame] | 161 | batadv_gw_bandwidth_to_kbit((uint8_t)gw_bandwidth_tmp, &down, &up); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 162 | |
Marek Lindner | dafe94b | 2012-05-11 16:10:50 +0800 | [diff] [blame] | 163 | if (atomic_read(&bat_priv->gw_bandwidth) == gw_bandwidth_tmp) |
| 164 | return count; |
| 165 | |
Sven Eckelmann | 7cf06bc | 2012-05-12 02:09:29 +0200 | [diff] [blame] | 166 | batadv_gw_deselect(bat_priv); |
Sven Eckelmann | 3e34819 | 2012-05-16 20:23:22 +0200 | [diff] [blame] | 167 | batadv_info(net_dev, |
| 168 | "Changing gateway bandwidth from: '%i' to: '%ld' (propagating: %d%s/%d%s)\n", |
| 169 | atomic_read(&bat_priv->gw_bandwidth), gw_bandwidth_tmp, |
| 170 | (down > 2048 ? down / 1024 : down), |
| 171 | (down > 2048 ? "MBit" : "KBit"), |
| 172 | (up > 2048 ? up / 1024 : up), |
| 173 | (up > 2048 ? "MBit" : "KBit")); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 174 | |
| 175 | atomic_set(&bat_priv->gw_bandwidth, gw_bandwidth_tmp); |
| 176 | |
| 177 | end: |
| 178 | return count; |
| 179 | } |