summaryrefslogtreecommitdiff
path: root/wcn6740/qcwcn/wifi_hal/tcp_params_update.cpp
blob: c13b1b0396fc591988f2771ce59db5b2d38b20a7 (plain)
1
2
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/* Copyright (c) 2018-2019, The Linux Foundation. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *  * Neither the name of The Linux Foundation nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "sync.h"

#include <utils/Log.h>

#include "wifi_hal.h"
#include "common.h"
#include "cpp_bindings.h"
#include "vendor_definitions.h"
#include <netlink/genl/genl.h>
#include <string.h>
#include <net/if.h>
#include "tcp_params_update.h"

TCPParamCommand::TCPParamCommand(wifi_handle handle, int id,
					u32 vendor_id, u32 subcmd)
	: WifiVendorCommand(handle, id, vendor_id, subcmd)
{
	if (registerVendorHandler(vendor_id, subcmd)) {
		/* Error case should not happen print log */
		ALOGE("%s: Unable to register Vendor Handler Vendor Id=0x%x subcmd=%u",
						__FUNCTION__, vendor_id, subcmd);
	}
	memset(def_tcp_limit_output_bytes, 0, SIZE_TCP_PARAM);
	memset(def_tcp_adv_win_scale, 0, SIZE_TCP_PARAM);
	def_tcp_limit_output_bytes_valid = false;
	def_tcp_adv_win_scale_valid = false;
}

TCPParamCommand::~TCPParamCommand()
{
	unregisterVendorHandler(OUI_QCA, QCA_NL80211_VENDOR_SUBCMD_THROUGHPUT_CHANGE_EVENT);
}

TCPParamCommand *TCPParamCommand::instance(wifi_handle handle, wifi_request_id id)
{
	TCPParamCommand* mTCPParamCommandInstance;

	mTCPParamCommandInstance = new TCPParamCommand(handle, id, OUI_QCA,
						QCA_NL80211_VENDOR_SUBCMD_THROUGHPUT_CHANGE_EVENT);
	return mTCPParamCommandInstance;
}

/* This function will be the main handler for incoming event.
 * Call the appropriate callback handler after parsing the vendor data.
 */
int TCPParamCommand::handleEvent(WifiEvent &event)
{
	wifi_error ret = WIFI_SUCCESS;
	WifiVendorCommand::handleEvent(event);

	u8 tpDirection, tpLevel;
	u32 tcpLimitOutputBytes;
	u8 tcpLimitOutputBytesFlag = 0;
	s8 tcpAdvWinScale;
	u8 tcpAdvWinScaleFlag = 0;
	u32 tcpDelackSeg;
	u8 tcpDelackSegFlag = 0;
	char value_to_str[100];
	int ret_val = 0;

	/* Parse the vendordata and get the attribute */
	switch(mSubcmd) {
	case QCA_NL80211_VENDOR_SUBCMD_THROUGHPUT_CHANGE_EVENT:
	{
		struct nlattr *tb[QCA_WLAN_VENDOR_ATTR_THROUGHPUT_CHANGE_MAX + 1];

		nla_parse(tb, QCA_WLAN_VENDOR_ATTR_THROUGHPUT_CHANGE_MAX,
					(struct nlattr *)mVendorData, mDataLen, NULL);

		if (!tb[QCA_WLAN_VENDOR_ATTR_THROUGHPUT_CHANGE_DIRECTION] ||
		    !tb[QCA_WLAN_VENDOR_ATTR_THROUGHPUT_CHANGE_THROUGHPUT_LEVEL]) {
			ALOGE("Invalid event, didn't receive mandatory attributes");
			return WIFI_ERROR_INVALID_ARGS;
		}
		tpDirection = nla_get_u8(tb[QCA_WLAN_VENDOR_ATTR_THROUGHPUT_CHANGE_DIRECTION]);
		tpLevel = nla_get_u8(tb[QCA_WLAN_VENDOR_ATTR_THROUGHPUT_CHANGE_THROUGHPUT_LEVEL]);

		if (tb[QCA_WLAN_VENDOR_ATTR_THROUGHPUT_CHANGE_TCP_LIMIT_OUTPUT_BYTES]) {
			tcpLimitOutputBytes = nla_get_u32(tb[
			QCA_WLAN_VENDOR_ATTR_THROUGHPUT_CHANGE_TCP_LIMIT_OUTPUT_BYTES]);
			tcpLimitOutputBytesFlag = 1;
		}

		if (tb[QCA_WLAN_VENDOR_ATTR_THROUGHPUT_CHANGE_TCP_ADV_WIN_SCALE]) {
			tcpAdvWinScale = *(s8 *)nla_data(tb[
			QCA_WLAN_VENDOR_ATTR_THROUGHPUT_CHANGE_TCP_ADV_WIN_SCALE]);
			tcpAdvWinScaleFlag = 1;
		}

		if (tb[QCA_WLAN_VENDOR_ATTR_THROUGHPUT_CHANGE_TCP_DELACK_SEG]) {
			tcpDelackSeg = nla_get_u32(tb[
			QCA_WLAN_VENDOR_ATTR_THROUGHPUT_CHANGE_TCP_DELACK_SEG]);
			tcpDelackSegFlag = 1;
		}
		if (tpDirection == TP_CHANGE_RX) {
			switch(tpLevel) {
			case QCA_WLAN_THROUGHPUT_LEVEL_LOW:
			{
				if (def_tcp_adv_win_scale_valid)
					wlan_service_set_tcp_adv_win_scale(def_tcp_adv_win_scale);
				wlan_service_set_tcp_use_userconfig("0");
			}
			break;
			case QCA_WLAN_THROUGHPUT_LEVEL_MEDIUM:
			case QCA_WLAN_THROUGHPUT_LEVEL_HIGH:
			{
				if (tcpAdvWinScaleFlag) {
					ret_val = snprintf(value_to_str, sizeof(value_to_str), "%d",
											tcpAdvWinScale);
					if (ret_val < 0 || ret_val >= (int)sizeof(value_to_str)) {
						ALOGE("Error in converting value to string: %d", ret_val);
						ret = WIFI_ERROR_UNKNOWN;
						break;
					}
					wlan_service_set_tcp_adv_win_scale(value_to_str);
				}
				if (tcpDelackSegFlag && wlan_service_set_tcp_use_userconfig("1") == 0) {
					ret_val = snprintf(value_to_str, sizeof(value_to_str), "%d",
											tcpDelackSeg);
					if (ret_val < 0 || ret_val >= (int)sizeof(value_to_str)) {
						ALOGE("Error in converting value to string: %d", ret_val);
						ret = WIFI_ERROR_UNKNOWN;
						break;
					}
					wlan_service_set_tcp_delack_seg(value_to_str);
				 }
			}
			break;
			default:
			{
				/* Error case should not happen print log */
				ALOGE("%s: Invalid throughput level value", __FUNCTION__);
				return WIFI_ERROR_INVALID_ARGS;
			}
			}
		} else if (tpDirection == TP_CHANGE_TX) {
			switch(tpLevel) {
			case QCA_WLAN_THROUGHPUT_LEVEL_LOW:
			{
				if (def_tcp_limit_output_bytes_valid)
					wlan_service_set_tcp_limit_output_bytes(
									def_tcp_limit_output_bytes);
			}
			break;
			case QCA_WLAN_THROUGHPUT_LEVEL_MEDIUM:
			case QCA_WLAN_THROUGHPUT_LEVEL_HIGH:
			{
				if (tcpLimitOutputBytesFlag) {
					ret_val = snprintf(value_to_str, sizeof(value_to_str), "%d",
											tcpLimitOutputBytes);
					if (ret_val < 0 || ret_val >= (int)sizeof(value_to_str)) {
						ALOGE("Error in converting value to string: %d", ret_val);
						ret = WIFI_ERROR_UNKNOWN;
						break;
					}
					wlan_service_set_tcp_limit_output_bytes(value_to_str);
				}
			}
			break;
			default:
			{
				/* Error case should not happen print log */
				ALOGE("%s: Invalid throughput level value", __FUNCTION__);
				return WIFI_ERROR_INVALID_ARGS;
			}
			}
		} else {
			/* Error case should not happen print log */
			ALOGE("%s: Invalid throughput change direction", __FUNCTION__);
			return ret;
		}
	}
	break;
	default:
		/* Error case should not happen print log */
		ALOGE("%s: Wrong subcmd received %d", __FUNCTION__, mSubcmd);
	}

	return ret;
}

wifi_error wifi_init_tcp_param_change_event_handler(wifi_interface_handle iface)
{
	wifi_handle wifiHandle = getWifiHandle(iface);
	TCPParamCommand *tcpParamCommand;

	if (wifiHandle == NULL) {
		ALOGE("%s: Interface Handle is invalid", __func__);
		return WIFI_ERROR_UNKNOWN;
	}

	hal_info *info = getHalInfo(wifiHandle);
	if (!info)
		return WIFI_ERROR_UNKNOWN;

	tcpParamCommand = TCPParamCommand::instance(wifiHandle, 0);
	if (tcpParamCommand == NULL) {
		ALOGE("%s: Error TcpParamCommand NULL", __FUNCTION__);
		return WIFI_ERROR_OUT_OF_MEMORY;
	}

	info->tcp_param_handler = (tcp_param_cmd_handler *)malloc(sizeof(tcp_param_cmd_handler));
	if (info->tcp_param_handler == NULL) {
		ALOGE("%s: Allocation of tcp handler failed",__FUNCTION__);
		delete tcpParamCommand;
		return WIFI_ERROR_OUT_OF_MEMORY;
	}
	info->tcp_param_handler->tcpParamCommand = tcpParamCommand;

	if (wlan_service_read_sys_param("/proc/sys/net/ipv4/tcp_limit_output_bytes",
					tcpParamCommand->def_tcp_limit_output_bytes,
					SIZE_TCP_PARAM) == 0) {
		tcpParamCommand->def_tcp_limit_output_bytes_valid = true;
	}

	if (wlan_service_read_sys_param("/proc/sys/net/ipv4/tcp_adv_win_scale",
					tcpParamCommand->def_tcp_adv_win_scale,
					SIZE_TCP_PARAM) == 0) {
		tcpParamCommand->def_tcp_adv_win_scale_valid = true;
	}

	return WIFI_SUCCESS;
}

void cleanupTCPParamCommand(hal_info *info) {

	TCPParamCommand *tcpParamCommand;

	if (info == NULL || info->tcp_param_handler == NULL)
		return;

	tcpParamCommand = info->tcp_param_handler->tcpParamCommand;

	if (tcpParamCommand) {
		if (tcpParamCommand->def_tcp_limit_output_bytes_valid)
			wlan_service_update_sys_param("/proc/sys/net/ipv4/tcp_limit_output_bytes",
						      tcpParamCommand->def_tcp_limit_output_bytes);
		wlan_service_update_sys_param("/proc/sys/net/ipv4/tcp_use_userconfig", "0");
		if (tcpParamCommand->def_tcp_adv_win_scale_valid)
			wlan_service_update_sys_param("/proc/sys/net/ipv4/tcp_adv_win_scale",
						      tcpParamCommand->def_tcp_adv_win_scale);
		delete tcpParamCommand;
	}

	free(info->tcp_param_handler);

	return;
}

/**
 * wlan_service_update_sys_param()
 * @path: path on the file system to be modified
 * @str:  value to be written to the path
 *
 * Generic function to update a system parameter
 * Return: WIFI_SUCCESS code if read is successful
 *         Eror code if read is failure
 */
wifi_error wlan_service_update_sys_param(const char *path, const char *str)
{
	int ret;
	FILE *fp;
	fp = fopen(path, "w");

	if (fp == NULL) {
		ALOGE("%s: unable to open %s", __FUNCTION__, path);
		return WIFI_ERROR_UNKNOWN;
	}

	ALOGD("%s: %s %s", __FUNCTION__,  path, str);

	ret = fputs(str, fp);
	fclose(fp);

	if (ret < 0) {
		ALOGE("%s: failed to write %s to %s with err %d", __FUNCTION__, str, path, ret);
		return mapKernelErrortoWifiHalError(ret);
	}

	return WIFI_SUCCESS;
}

/**
 * wlan_service_read_sys_param()
 * @path: path on the file system to be read
 * @str:  value read from the path
 *
 * Generic function to read a system parameter
 * Return: WIFI_SUCCESS code if read is successful
 *         Eror code if read is failure
 */
wifi_error wlan_service_read_sys_param(const char *path, char *str, size_t max_size)
{
	size_t ret_len;
	FILE *fp = fopen(path, "r");

	if (fp == NULL) {
		ALOGE("%s: unable to open %s", __FUNCTION__, path);
		return WIFI_ERROR_UNKNOWN;
	}

	ret_len = fread(str, 1, max_size, fp);
	fclose(fp);

	if (ret_len == 0 || ret_len == max_size) {
		ALOGE("Faild to read %s, ret_len = %zu", path, ret_len);
		return WIFI_ERROR_UNKNOWN;
	}

	ALOGD("%s: %s %s", __FUNCTION__,  path, str);
	return WIFI_SUCCESS;
}

int TCPParamCommand::wlan_service_set_tcp_adv_win_scale(char *str)
{
	return wlan_service_update_sys_param(
		"/proc/sys/net/ipv4/tcp_adv_win_scale", str);
}

int TCPParamCommand::wlan_service_set_tcp_use_userconfig(const char *str)
{
	return wlan_service_update_sys_param(
		"/proc/sys/net/ipv4/tcp_use_userconfig", str);
}

int TCPParamCommand::wlan_service_set_tcp_delack_seg(char *str)
{
	return wlan_service_update_sys_param(
		"/proc/sys/net/ipv4/tcp_delack_seg", str);
}

int TCPParamCommand::wlan_service_set_tcp_limit_output_bytes(char *str)
{
	return wlan_service_update_sys_param (
		"/proc/sys/net/ipv4/tcp_limit_output_bytes", str);
}