Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1 | /* |
Sven Eckelmann | 64afe35 | 2011-01-27 10:38:15 +0100 | [diff] [blame] | 2 | * Copyright (C) 2009-2011 B.A.T.M.A.N. contributors: |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 3 | * |
| 4 | * Marek Lindner |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of version 2 of the GNU General Public |
| 8 | * License as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 18 | * 02110-1301, USA |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include "main.h" |
| 23 | #include "gateway_common.h" |
| 24 | #include "gateway_client.h" |
| 25 | |
| 26 | /* calculates the gateway class from kbit */ |
| 27 | static void kbit_to_gw_bandwidth(int down, int up, long *gw_srv_class) |
| 28 | { |
| 29 | int mdown = 0, tdown, tup, difference; |
| 30 | uint8_t sbit, part; |
| 31 | |
| 32 | *gw_srv_class = 0; |
| 33 | difference = 0x0FFFFFFF; |
| 34 | |
| 35 | /* test all downspeeds */ |
| 36 | for (sbit = 0; sbit < 2; sbit++) { |
| 37 | for (part = 0; part < 16; part++) { |
| 38 | tdown = 32 * (sbit + 2) * (1 << part); |
| 39 | |
| 40 | if (abs(tdown - down) < difference) { |
| 41 | *gw_srv_class = (sbit << 7) + (part << 3); |
| 42 | difference = abs(tdown - down); |
| 43 | mdown = tdown; |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | /* test all upspeeds */ |
| 49 | difference = 0x0FFFFFFF; |
| 50 | |
| 51 | for (part = 0; part < 8; part++) { |
| 52 | tup = ((part + 1) * (mdown)) / 8; |
| 53 | |
| 54 | if (abs(tup - up) < difference) { |
| 55 | *gw_srv_class = (*gw_srv_class & 0xF8) | part; |
| 56 | difference = abs(tup - up); |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /* returns the up and downspeeds in kbit, calculated from the class */ |
| 62 | void gw_bandwidth_to_kbit(uint8_t gw_srv_class, int *down, int *up) |
| 63 | { |
Sven Eckelmann | b4e1705 | 2011-06-15 09:41:37 +0200 | [diff] [blame] | 64 | int sbit = (gw_srv_class & 0x80) >> 7; |
| 65 | int dpart = (gw_srv_class & 0x78) >> 3; |
| 66 | int upart = (gw_srv_class & 0x07); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 67 | |
| 68 | if (!gw_srv_class) { |
| 69 | *down = 0; |
| 70 | *up = 0; |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | *down = 32 * (sbit + 2) * (1 << dpart); |
| 75 | *up = ((upart + 1) * (*down)) / 8; |
| 76 | } |
| 77 | |
| 78 | static bool parse_gw_bandwidth(struct net_device *net_dev, char *buff, |
Sven Eckelmann | 37a4065 | 2011-05-14 23:14:51 +0200 | [diff] [blame] | 79 | int *up, int *down) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 80 | { |
| 81 | int ret, multi = 1; |
| 82 | char *slash_ptr, *tmp_ptr; |
Sven Eckelmann | 37a4065 | 2011-05-14 23:14:51 +0200 | [diff] [blame] | 83 | long ldown, lup; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 84 | |
| 85 | slash_ptr = strchr(buff, '/'); |
| 86 | if (slash_ptr) |
| 87 | *slash_ptr = 0; |
| 88 | |
| 89 | if (strlen(buff) > 4) { |
| 90 | tmp_ptr = buff + strlen(buff) - 4; |
| 91 | |
| 92 | if (strnicmp(tmp_ptr, "mbit", 4) == 0) |
| 93 | multi = 1024; |
| 94 | |
| 95 | if ((strnicmp(tmp_ptr, "kbit", 4) == 0) || |
| 96 | (multi > 1)) |
| 97 | *tmp_ptr = '\0'; |
| 98 | } |
| 99 | |
Sven Eckelmann | 25a92b1 | 2011-09-30 13:32:01 +0200 | [diff] [blame^] | 100 | ret = kstrtol(buff, 10, &ldown); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 101 | if (ret) { |
| 102 | bat_err(net_dev, |
| 103 | "Download speed of gateway mode invalid: %s\n", |
| 104 | buff); |
| 105 | return false; |
| 106 | } |
| 107 | |
Sven Eckelmann | 37a4065 | 2011-05-14 23:14:51 +0200 | [diff] [blame] | 108 | *down = ldown * multi; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 109 | |
| 110 | /* we also got some upload info */ |
| 111 | if (slash_ptr) { |
| 112 | multi = 1; |
| 113 | |
| 114 | if (strlen(slash_ptr + 1) > 4) { |
| 115 | tmp_ptr = slash_ptr + 1 - 4 + strlen(slash_ptr + 1); |
| 116 | |
| 117 | if (strnicmp(tmp_ptr, "mbit", 4) == 0) |
| 118 | multi = 1024; |
| 119 | |
| 120 | if ((strnicmp(tmp_ptr, "kbit", 4) == 0) || |
| 121 | (multi > 1)) |
| 122 | *tmp_ptr = '\0'; |
| 123 | } |
| 124 | |
Sven Eckelmann | 25a92b1 | 2011-09-30 13:32:01 +0200 | [diff] [blame^] | 125 | ret = kstrtol(slash_ptr + 1, 10, &lup); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 126 | if (ret) { |
| 127 | bat_err(net_dev, |
| 128 | "Upload speed of gateway mode invalid: " |
| 129 | "%s\n", slash_ptr + 1); |
| 130 | return false; |
| 131 | } |
| 132 | |
Sven Eckelmann | 37a4065 | 2011-05-14 23:14:51 +0200 | [diff] [blame] | 133 | *up = lup * multi; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | return true; |
| 137 | } |
| 138 | |
| 139 | ssize_t gw_bandwidth_set(struct net_device *net_dev, char *buff, size_t count) |
| 140 | { |
| 141 | struct bat_priv *bat_priv = netdev_priv(net_dev); |
Sven Eckelmann | 37a4065 | 2011-05-14 23:14:51 +0200 | [diff] [blame] | 142 | long gw_bandwidth_tmp = 0; |
| 143 | int up = 0, down = 0; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 144 | bool ret; |
| 145 | |
| 146 | ret = parse_gw_bandwidth(net_dev, buff, &up, &down); |
| 147 | if (!ret) |
| 148 | goto end; |
| 149 | |
| 150 | if ((!down) || (down < 256)) |
| 151 | down = 2000; |
| 152 | |
| 153 | if (!up) |
| 154 | up = down / 5; |
| 155 | |
| 156 | kbit_to_gw_bandwidth(down, up, &gw_bandwidth_tmp); |
| 157 | |
| 158 | /** |
| 159 | * the gw bandwidth we guessed above might not match the given |
| 160 | * speeds, hence we need to calculate it back to show the number |
| 161 | * that is going to be propagated |
| 162 | **/ |
Sven Eckelmann | 37a4065 | 2011-05-14 23:14:51 +0200 | [diff] [blame] | 163 | gw_bandwidth_to_kbit((uint8_t)gw_bandwidth_tmp, &down, &up); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 164 | |
| 165 | gw_deselect(bat_priv); |
| 166 | bat_info(net_dev, "Changing gateway bandwidth from: '%i' to: '%ld' " |
Sven Eckelmann | 37a4065 | 2011-05-14 23:14:51 +0200 | [diff] [blame] | 167 | "(propagating: %d%s/%d%s)\n", |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 168 | atomic_read(&bat_priv->gw_bandwidth), gw_bandwidth_tmp, |
| 169 | (down > 2048 ? down / 1024 : down), |
| 170 | (down > 2048 ? "MBit" : "KBit"), |
| 171 | (up > 2048 ? up / 1024 : up), |
| 172 | (up > 2048 ? "MBit" : "KBit")); |
| 173 | |
| 174 | atomic_set(&bat_priv->gw_bandwidth, gw_bandwidth_tmp); |
| 175 | |
| 176 | end: |
| 177 | return count; |
| 178 | } |