Blame view

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