2 * Copyright (C) 2009-2010 B.A.T.M.A.N. contributors:
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.
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.
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
23 #include "gateway_common.h"
24 #include "gateway_client.h"
26 /* calculates the gateway class from kbit */
27 static void kbit_to_gw_bandwidth(int down
, int up
, long *gw_srv_class
)
29 int mdown
= 0, tdown
, tup
, difference
;
33 difference
= 0x0FFFFFFF;
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
);
40 if (abs(tdown
- down
) < difference
) {
41 *gw_srv_class
= (sbit
<< 7) + (part
<< 3);
42 difference
= abs(tdown
- down
);
48 /* test all upspeeds */
49 difference
= 0x0FFFFFFF;
51 for (part
= 0; part
< 8; part
++) {
52 tup
= ((part
+ 1) * (mdown
)) / 8;
54 if (abs(tup
- up
) < difference
) {
55 *gw_srv_class
= (*gw_srv_class
& 0xF8) | part
;
56 difference
= abs(tup
- up
);
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
)
64 char sbit
= (gw_srv_class
& 0x80) >> 7;
65 char dpart
= (gw_srv_class
& 0x78) >> 3;
66 char upart
= (gw_srv_class
& 0x07);
74 *down
= 32 * (sbit
+ 2) * (1 << dpart
);
75 *up
= ((upart
+ 1) * (*down
)) / 8;
78 static bool parse_gw_bandwidth(struct net_device
*net_dev
, char *buff
,
82 char *slash_ptr
, *tmp_ptr
;
84 slash_ptr
= strchr(buff
, '/');
88 if (strlen(buff
) > 4) {
89 tmp_ptr
= buff
+ strlen(buff
) - 4;
91 if (strnicmp(tmp_ptr
, "mbit", 4) == 0)
94 if ((strnicmp(tmp_ptr
, "kbit", 4) == 0) ||
99 ret
= strict_strtoul(buff
, 10, down
);
102 "Download speed of gateway mode invalid: %s\n",
109 /* we also got some upload info */
113 if (strlen(slash_ptr
+ 1) > 4) {
114 tmp_ptr
= slash_ptr
+ 1 - 4 + strlen(slash_ptr
+ 1);
116 if (strnicmp(tmp_ptr
, "mbit", 4) == 0)
119 if ((strnicmp(tmp_ptr
, "kbit", 4) == 0) ||
124 ret
= strict_strtoul(slash_ptr
+ 1, 10, up
);
127 "Upload speed of gateway mode invalid: "
128 "%s\n", slash_ptr
+ 1);
138 ssize_t
gw_bandwidth_set(struct net_device
*net_dev
, char *buff
, size_t count
)
140 struct bat_priv
*bat_priv
= netdev_priv(net_dev
);
141 long gw_bandwidth_tmp
= 0, up
= 0, down
= 0;
144 ret
= parse_gw_bandwidth(net_dev
, buff
, &up
, &down
);
148 if ((!down
) || (down
< 256))
154 kbit_to_gw_bandwidth(down
, up
, &gw_bandwidth_tmp
);
157 * the gw bandwidth we guessed above might not match the given
158 * speeds, hence we need to calculate it back to show the number
159 * that is going to be propagated
161 gw_bandwidth_to_kbit((uint8_t)gw_bandwidth_tmp
,
162 (int *)&down
, (int *)&up
);
164 gw_deselect(bat_priv
);
165 bat_info(net_dev
, "Changing gateway bandwidth from: '%i' to: '%ld' "
166 "(propagating: %ld%s/%ld%s)\n",
167 atomic_read(&bat_priv
->gw_bandwidth
), gw_bandwidth_tmp
,
168 (down
> 2048 ? down
/ 1024 : down
),
169 (down
> 2048 ? "MBit" : "KBit"),
170 (up
> 2048 ? up
/ 1024 : up
),
171 (up
> 2048 ? "MBit" : "KBit"));
173 atomic_set(&bat_priv
->gw_bandwidth
, gw_bandwidth_tmp
);