rc80211_pid_algo.c 14.6 KB
Newer Older
1 2 3 4
/*
 * Copyright 2002-2005, Instant802 Networks, Inc.
 * Copyright 2005, Devicescape Software, Inc.
 * Copyright 2007, Mattias Nissler <mattias.nissler@gmx.de>
5
 * Copyright 2007-2008, Stefano Brivio <stefano.brivio@polimi.it>
6 7 8 9 10 11 12 13 14
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/netdevice.h>
#include <linux/types.h>
#include <linux/skbuff.h>
15
#include <linux/debugfs.h>
16
#include <net/mac80211.h>
J
Johannes Berg 已提交
17
#include "rate.h"
18
#include "mesh.h"
M
Mattias Nissler 已提交
19 20
#include "rc80211_pid.h"

21 22 23 24 25 26 27

/* This is an implementation of a TX rate control algorithm that uses a PID
 * controller. Given a target failed frames rate, the controller decides about
 * TX rate changes to meet the target failed frames rate.
 *
 * The controller basically computes the following:
 *
28
 * adj = CP * err + CI * err_avg + CD * (err - last_err) * (1 + sharpening)
29 30 31 32 33 34
 *
 * where
 * 	adj	adjustment value that is used to switch TX rate (see below)
 * 	err	current error: target vs. current failed frames percentage
 * 	last_err	last error
 * 	err_avg	average (i.e. poor man's integral) of recent errors
35 36 37
 *	sharpening	non-zero when fast response is needed (i.e. right after
 *			association or no frames sent for a long time), heading
 * 			to zero over time
38 39 40 41 42 43 44 45 46 47
 * 	CP	Proportional coefficient
 * 	CI	Integral coefficient
 * 	CD	Derivative coefficient
 *
 * CP, CI, CD are subject to careful tuning.
 *
 * The integral component uses a exponential moving average approach instead of
 * an actual sliding window. The advantage is that we don't need to keep an
 * array of the last N error values and computation is easier.
 *
48 49 50 51 52 53 54 55 56 57 58 59
 * Once we have the adj value, we map it to a rate by means of a learning
 * algorithm. This algorithm keeps the state of the percentual failed frames
 * difference between rates. The behaviour of the lowest available rate is kept
 * as a reference value, and every time we switch between two rates, we compute
 * the difference between the failed frames each rate exhibited. By doing so,
 * we compare behaviours which different rates exhibited in adjacent timeslices,
 * thus the comparison is minimally affected by external conditions. This
 * difference gets propagated to the whole set of measurements, so that the
 * reference is always the same. Periodically, we normalize this set so that
 * recent events weigh the most. By comparing the adj value with this set, we
 * avoid pejorative switches to lower rates and allow for switches to higher
 * rates if they behaved well.
60 61 62 63 64 65 66
 *
 * Note that for the computations we use a fixed-point representation to avoid
 * floating point arithmetic. Hence, all values are shifted left by
 * RC_PID_ARITH_SHIFT.
 */


67 68 69 70
/* Adjust the rate while ensuring that we won't switch to a lower rate if it
 * exhibited a worse failed frames behaviour and we'll choose the highest rate
 * whose failed frames behaviour is not worse than the one of the original rate
 * target. While at it, check that the new rate is valid. */
71 72 73
static void rate_control_pid_adjust_rate(struct ieee80211_supported_band *sband,
					 struct ieee80211_sta *sta,
					 struct rc_pid_sta_info *spinfo, int adj,
74
					 struct rc_pid_rateinfo *rinfo)
75
{
76
	int cur_sorted, new_sorted, probe, tmp, n_bitrates, band;
77
	int cur = spinfo->txrate_idx;
78

79 80
	band = sband->band;
	n_bitrates = sband->n_bitrates;
81

82 83 84
	/* Map passed arguments to sorted values. */
	cur_sorted = rinfo[cur].rev_index;
	new_sorted = cur_sorted + adj;
85

86 87 88 89 90 91 92
	/* Check limits. */
	if (new_sorted < 0)
		new_sorted = rinfo[0].rev_index;
	else if (new_sorted >= n_bitrates)
		new_sorted = rinfo[n_bitrates - 1].rev_index;

	tmp = new_sorted;
93

94 95 96 97 98 99 100 101 102 103 104 105
	if (adj < 0) {
		/* Ensure that the rate decrease isn't disadvantageous. */
		for (probe = cur_sorted; probe >= new_sorted; probe--)
			if (rinfo[probe].diff <= rinfo[cur_sorted].diff &&
			    rate_supported(sta, band, rinfo[probe].index))
				tmp = probe;
	} else {
		/* Look for rate increase with zero (or below) cost. */
		for (probe = new_sorted + 1; probe < n_bitrates; probe++)
			if (rinfo[probe].diff <= rinfo[new_sorted].diff &&
			    rate_supported(sta, band, rinfo[probe].index))
				tmp = probe;
106
	}
M
Mattias Nissler 已提交
107

108 109 110
	/* Fit the rate found to the nearest supported rate. */
	do {
		if (rate_supported(sta, band, rinfo[tmp].index)) {
111
			spinfo->txrate_idx = rinfo[tmp].index;
112 113 114 115 116 117 118 119
			break;
		}
		if (adj < 0)
			tmp--;
		else
			tmp++;
	} while (tmp < n_bitrates && tmp >= 0);

M
Mattias Nissler 已提交
120
#ifdef CONFIG_MAC80211_DEBUGFS
121 122 123
	rate_control_pid_event_rate_change(&spinfo->events,
		spinfo->txrate_idx,
		sband->bitrates[spinfo->txrate_idx].bitrate);
M
Mattias Nissler 已提交
124
#endif
125 126
}

127
/* Normalize the failed frames per-rate differences. */
128
static void rate_control_pid_normalize(struct rc_pid_info *pinfo, int l)
129
{
130 131
	int i, norm_offset = pinfo->norm_offset;
	struct rc_pid_rateinfo *r = pinfo->rinfo;
132

133 134 135 136
	if (r[0].diff > norm_offset)
		r[0].diff -= norm_offset;
	else if (r[0].diff < -norm_offset)
		r[0].diff += norm_offset;
137
	for (i = 0; i < l - 1; i++)
138 139
		if (r[i + 1].diff > r[i].diff + norm_offset)
			r[i + 1].diff -= norm_offset;
140
		else if (r[i + 1].diff <= r[i].diff)
141
			r[i + 1].diff += norm_offset;
142 143
}

144
static void rate_control_pid_sample(struct rc_pid_info *pinfo,
145 146 147
				    struct ieee80211_supported_band *sband,
				    struct ieee80211_sta *sta,
				    struct rc_pid_sta_info *spinfo)
148
{
149
	struct rc_pid_rateinfo *rinfo = pinfo->rinfo;
150 151
	u32 pf;
	s32 err_avg;
152 153 154
	u32 err_prop;
	u32 err_int;
	u32 err_der;
155
	int adj, i, j, tmp;
156
	unsigned long period;
157

158 159
	/* In case nothing happened during the previous control interval, turn
	 * the sharpening factor on. */
160 161 162 163 164
	period = (HZ * pinfo->sampling_period + 500) / 1000;
	if (!period)
		period = 1;
	if (jiffies - spinfo->last_sample > 2 * period)
		spinfo->sharp_cnt = pinfo->sharpen_duration;
165

166 167
	spinfo->last_sample = jiffies;

168
	/* This should never happen, but in case, we assume the old sample is
169
	 * still a good measurement and copy it. */
170
	if (unlikely(spinfo->tx_num_xmit == 0))
171
		pf = spinfo->last_pf;
172
	else
173
		pf = spinfo->tx_num_failed * 100 / spinfo->tx_num_xmit;
174

175 176 177
	spinfo->tx_num_xmit = 0;
	spinfo->tx_num_failed = 0;

178
	/* If we just switched rate, update the rate behaviour info. */
179
	if (pinfo->oldrate != spinfo->txrate_idx) {
180 181

		i = rinfo[pinfo->oldrate].rev_index;
182
		j = rinfo[spinfo->txrate_idx].rev_index;
183 184 185 186 187

		tmp = (pf - spinfo->last_pf);
		tmp = RC_PID_DO_ARITH_RIGHT_SHIFT(tmp, RC_PID_ARITH_SHIFT);

		rinfo[j].diff = rinfo[i].diff + tmp;
188
		pinfo->oldrate = spinfo->txrate_idx;
189
	}
190
	rate_control_pid_normalize(pinfo, sband->n_bitrates);
191

192
	/* Compute the proportional, integral and derivative errors. */
193
	err_prop = (pinfo->target << RC_PID_ARITH_SHIFT) - pf;
194

195
	err_avg = spinfo->err_avg_sc >> pinfo->smoothing_shift;
196
	spinfo->err_avg_sc = spinfo->err_avg_sc - err_avg + err_prop;
197
	err_int = spinfo->err_avg_sc >> pinfo->smoothing_shift;
198

199 200
	err_der = (pf - spinfo->last_pf) *
		  (1 + pinfo->sharpen_factor * spinfo->sharp_cnt);
201
	spinfo->last_pf = pf;
202 203
	if (spinfo->sharp_cnt)
			spinfo->sharp_cnt--;
204

M
Mattias Nissler 已提交
205 206 207 208 209
#ifdef CONFIG_MAC80211_DEBUGFS
	rate_control_pid_event_pf_sample(&spinfo->events, pf, err_prop, err_int,
					 err_der);
#endif

210 211 212
	/* Compute the controller output. */
	adj = (err_prop * pinfo->coeff_p + err_int * pinfo->coeff_i
	      + err_der * pinfo->coeff_d);
213
	adj = RC_PID_DO_ARITH_RIGHT_SHIFT(adj, 2 * RC_PID_ARITH_SHIFT);
214 215 216

	/* Change rate. */
	if (adj)
217
		rate_control_pid_adjust_rate(sband, sta, spinfo, adj, rinfo);
218 219
}

220 221
static void rate_control_pid_tx_status(void *priv, struct ieee80211_supported_band *sband,
				       struct ieee80211_sta *sta, void *priv_sta,
222
				       struct sk_buff *skb)
223 224
{
	struct rc_pid_info *pinfo = priv;
225
	struct rc_pid_sta_info *spinfo = priv_sta;
226
	unsigned long period;
227
	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
228

229 230
	if (!spinfo)
		return;
231

232 233
	/* Ignore all frames that were sent with a different rate than the rate
	 * we currently advise mac80211 to use. */
234
	if (info->status.rates[0].idx != spinfo->txrate_idx)
235
		return;
236 237 238

	spinfo->tx_num_xmit++;

M
Mattias Nissler 已提交
239
#ifdef CONFIG_MAC80211_DEBUGFS
240
	rate_control_pid_event_tx_status(&spinfo->events, info);
M
Mattias Nissler 已提交
241 242
#endif

243 244 245
	/* We count frames that totally failed to be transmitted as two bad
	 * frames, those that made it out but had some retries as one good and
	 * one bad frame. */
246
	if (!(info->flags & IEEE80211_TX_STAT_ACK)) {
247 248
		spinfo->tx_num_failed += 2;
		spinfo->tx_num_xmit++;
249
	} else if (info->status.rates[0].count > 1) {
250 251 252 253 254
		spinfo->tx_num_failed++;
		spinfo->tx_num_xmit++;
	}

	/* Update PID controller state. */
255 256 257 258
	period = (HZ * pinfo->sampling_period + 500) / 1000;
	if (!period)
		period = 1;
	if (time_after(jiffies, spinfo->last_sample + period))
259
		rate_control_pid_sample(pinfo, sband, sta, spinfo);
260 261
}

262
static void
263 264 265
rate_control_pid_get_rate(void *priv, struct ieee80211_sta *sta,
			  void *priv_sta,
			  struct ieee80211_tx_rate_control *txrc)
266
{
267 268 269
	struct sk_buff *skb = txrc->skb;
	struct ieee80211_supported_band *sband = txrc->sband;
	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
270
	struct rc_pid_sta_info *spinfo = priv_sta;
271 272
	int rateidx;

273 274 275 276 277 278 279
	if (txrc->rts)
		info->control.rates[0].count =
			txrc->hw->conf.long_frame_max_tx_count;
	else
		info->control.rates[0].count =
			txrc->hw->conf.short_frame_max_tx_count;

280
	/* Send management frames and NO_ACK data using lowest rate. */
281
	if (rate_control_send_low(sta, priv_sta, txrc))
282 283
		return;

284
	rateidx = spinfo->txrate_idx;
285

286 287
	if (rateidx >= sband->n_bitrates)
		rateidx = sband->n_bitrates - 1;
288

289
	info->control.rates[0].idx = rateidx;
M
Mattias Nissler 已提交
290 291

#ifdef CONFIG_MAC80211_DEBUGFS
292
	rate_control_pid_event_tx_rate(&spinfo->events,
293
		rateidx, sband->bitrates[rateidx].bitrate);
M
Mattias Nissler 已提交
294
#endif
295 296
}

297 298 299
static void
rate_control_pid_rate_init(void *priv, struct ieee80211_supported_band *sband,
			   struct ieee80211_sta *sta, void *priv_sta)
300
{
301
	struct rc_pid_sta_info *spinfo = priv_sta;
302 303 304 305
	struct rc_pid_info *pinfo = priv;
	struct rc_pid_rateinfo *rinfo = pinfo->rinfo;
	int i, j, tmp;
	bool s;
306

307 308 309 310
	/* TODO: This routine should consider using RSSI from previous packets
	 * as we need to have IEEE 802.1X auth succeed immediately after assoc..
	 * Until that method is implemented, we will use the lowest supported
	 * rate as a workaround. */
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
	/* Sort the rates. This is optimized for the most common case (i.e.
	 * almost-sorted CCK+OFDM rates). Kind of bubble-sort with reversed
	 * mapping too. */
	for (i = 0; i < sband->n_bitrates; i++) {
		rinfo[i].index = i;
		rinfo[i].rev_index = i;
		if (RC_PID_FAST_START)
			rinfo[i].diff = 0;
		else
			rinfo[i].diff = i * pinfo->norm_offset;
	}
	for (i = 1; i < sband->n_bitrates; i++) {
		s = 0;
		for (j = 0; j < sband->n_bitrates - i; j++)
			if (unlikely(sband->bitrates[rinfo[j].index].bitrate >
				     sband->bitrates[rinfo[j + 1].index].bitrate)) {
				tmp = rinfo[j].index;
				rinfo[j].index = rinfo[j + 1].index;
				rinfo[j + 1].index = tmp;
				rinfo[rinfo[j].index].rev_index = j;
				rinfo[rinfo[j + 1].index].rev_index = j + 1;
				s = 1;
			}
		if (!s)
			break;
	}

339
	spinfo->txrate_idx = rate_lowest_index(sband, sta);
340 341
}

342 343
static void *rate_control_pid_alloc(struct ieee80211_hw *hw,
				    struct dentry *debugfsdir)
344 345
{
	struct rc_pid_info *pinfo;
346
	struct rc_pid_rateinfo *rinfo;
347
	struct ieee80211_supported_band *sband;
348
	int i, max_rates = 0;
349 350 351
#ifdef CONFIG_MAC80211_DEBUGFS
	struct rc_pid_debugfs_entries *de;
#endif
352 353

	pinfo = kmalloc(sizeof(*pinfo), GFP_ATOMIC);
354 355 356
	if (!pinfo)
		return NULL;

357 358
	for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
		sband = hw->wiphy->bands[i];
359
		if (sband && sband->n_bitrates > max_rates)
360 361 362 363
			max_rates = sband->n_bitrates;
	}

	rinfo = kmalloc(sizeof(*rinfo) * max_rates, GFP_ATOMIC);
364 365 366 367 368
	if (!rinfo) {
		kfree(pinfo);
		return NULL;
	}

369 370 371 372 373 374 375 376 377 378 379 380
	pinfo->target = RC_PID_TARGET_PF;
	pinfo->sampling_period = RC_PID_INTERVAL;
	pinfo->coeff_p = RC_PID_COEFF_P;
	pinfo->coeff_i = RC_PID_COEFF_I;
	pinfo->coeff_d = RC_PID_COEFF_D;
	pinfo->smoothing_shift = RC_PID_SMOOTHING_SHIFT;
	pinfo->sharpen_factor = RC_PID_SHARPENING_FACTOR;
	pinfo->sharpen_duration = RC_PID_SHARPENING_DURATION;
	pinfo->norm_offset = RC_PID_NORM_OFFSET;
	pinfo->rinfo = rinfo;
	pinfo->oldrate = 0;

381 382 383
#ifdef CONFIG_MAC80211_DEBUGFS
	de = &pinfo->dentries;
	de->target = debugfs_create_u32("target_pf", S_IRUSR | S_IWUSR,
384
					debugfsdir, &pinfo->target);
385
	de->sampling_period = debugfs_create_u32("sampling_period",
386
						 S_IRUSR | S_IWUSR, debugfsdir,
387 388
						 &pinfo->sampling_period);
	de->coeff_p = debugfs_create_u32("coeff_p", S_IRUSR | S_IWUSR,
389
					 debugfsdir, (u32 *)&pinfo->coeff_p);
390
	de->coeff_i = debugfs_create_u32("coeff_i", S_IRUSR | S_IWUSR,
391
					 debugfsdir, (u32 *)&pinfo->coeff_i);
392
	de->coeff_d = debugfs_create_u32("coeff_d", S_IRUSR | S_IWUSR,
393
					 debugfsdir, (u32 *)&pinfo->coeff_d);
394
	de->smoothing_shift = debugfs_create_u32("smoothing_shift",
395
						 S_IRUSR | S_IWUSR, debugfsdir,
396 397
						 &pinfo->smoothing_shift);
	de->sharpen_factor = debugfs_create_u32("sharpen_factor",
398
					       S_IRUSR | S_IWUSR, debugfsdir,
399 400
					       &pinfo->sharpen_factor);
	de->sharpen_duration = debugfs_create_u32("sharpen_duration",
401
						  S_IRUSR | S_IWUSR, debugfsdir,
402 403
						  &pinfo->sharpen_duration);
	de->norm_offset = debugfs_create_u32("norm_offset",
404
					     S_IRUSR | S_IWUSR, debugfsdir,
405 406 407
					     &pinfo->norm_offset);
#endif

408 409 410 411 412 413
	return pinfo;
}

static void rate_control_pid_free(void *priv)
{
	struct rc_pid_info *pinfo = priv;
414 415 416 417 418 419 420 421 422 423 424 425 426 427
#ifdef CONFIG_MAC80211_DEBUGFS
	struct rc_pid_debugfs_entries *de = &pinfo->dentries;

	debugfs_remove(de->norm_offset);
	debugfs_remove(de->sharpen_duration);
	debugfs_remove(de->sharpen_factor);
	debugfs_remove(de->smoothing_shift);
	debugfs_remove(de->coeff_d);
	debugfs_remove(de->coeff_i);
	debugfs_remove(de->coeff_p);
	debugfs_remove(de->sampling_period);
	debugfs_remove(de->target);
#endif

428
	kfree(pinfo->rinfo);
429 430 431
	kfree(pinfo);
}

432 433
static void *rate_control_pid_alloc_sta(void *priv, struct ieee80211_sta *sta,
					gfp_t gfp)
434 435 436 437
{
	struct rc_pid_sta_info *spinfo;

	spinfo = kzalloc(sizeof(*spinfo), gfp);
M
Mattias Nissler 已提交
438 439 440
	if (spinfo == NULL)
		return NULL;

441 442
	spinfo->last_sample = jiffies;

M
Mattias Nissler 已提交
443 444 445 446
#ifdef CONFIG_MAC80211_DEBUGFS
	spin_lock_init(&spinfo->events.lock);
	init_waitqueue_head(&spinfo->events.waitqueue);
#endif
447 448 449 450

	return spinfo;
}

451 452
static void rate_control_pid_free_sta(void *priv, struct ieee80211_sta *sta,
				      void *priv_sta)
453
{
454
	kfree(priv_sta);
455 456
}

457
static struct rate_control_ops mac80211_rcpid = {
458 459 460 461 462 463 464 465
	.name = "pid",
	.tx_status = rate_control_pid_tx_status,
	.get_rate = rate_control_pid_get_rate,
	.rate_init = rate_control_pid_rate_init,
	.alloc = rate_control_pid_alloc,
	.free = rate_control_pid_free,
	.alloc_sta = rate_control_pid_alloc_sta,
	.free_sta = rate_control_pid_free_sta,
M
Mattias Nissler 已提交
466 467 468 469
#ifdef CONFIG_MAC80211_DEBUGFS
	.add_sta_debugfs = rate_control_pid_add_sta_debugfs,
	.remove_sta_debugfs = rate_control_pid_remove_sta_debugfs,
#endif
470
};
471 472 473 474 475 476

int __init rc80211_pid_init(void)
{
	return ieee80211_rate_control_register(&mac80211_rcpid);
}

477
void rc80211_pid_exit(void)
478 479 480
{
	ieee80211_rate_control_unregister(&mac80211_rcpid);
}