rc80211_pid_algo.c 14.9 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, Stefano Brivio <stefano.brivio@polimi.it>
6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * 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>

#include <net/mac80211.h>
#include "ieee80211_rate.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 71 72 73 74 75 76
/* Shift the adjustment so 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 adjustment is within the ranges. Then,
 * provide the new rate index. */
static int rate_control_pid_shift_adjust(struct rc_pid_rateinfo *r,
					 int adj, int cur, int l)
{
	int i, j, k, tmp;

77 78
	j = r[cur].rev_index;
	i = j + adj;
79

80 81 82 83
	if (i < 0)
		return r[0].index;
	if (i >= l - 1)
		return r[l - 1].index;
84

85
	tmp = i;
86 87

	if (adj < 0) {
88 89 90 91 92 93 94
		for (k = j; k >= i; k--)
			if (r[k].diff <= r[j].diff)
				tmp = k;
	} else {
		for (k = i + 1; k + i < l; k++)
			if (r[k].diff <= r[i].diff)
				tmp = k;
95
	}
96 97

	return r[tmp].index;
98
}
99 100

static void rate_control_pid_adjust_rate(struct ieee80211_local *local,
101 102
					 struct sta_info *sta, int adj,
					 struct rc_pid_rateinfo *rinfo)
103 104 105
{
	struct ieee80211_sub_if_data *sdata;
	struct ieee80211_hw_mode *mode;
106
	int newidx;
107 108 109 110 111 112 113 114 115 116 117 118
	int maxrate;
	int back = (adj > 0) ? 1 : -1;

	sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
	if (sdata->bss && sdata->bss->force_unicast_rateidx > -1) {
		/* forced unicast rate - do not change STA rate */
		return;
	}

	mode = local->oper_hw_mode;
	maxrate = sdata->bss ? sdata->bss->max_ratectrl_rateidx : -1;

119 120
	newidx = rate_control_pid_shift_adjust(rinfo, adj, sta->txrate,
					       mode->num_rates);
121 122 123 124 125 126 127 128 129 130

	while (newidx != sta->txrate) {
		if (rate_supported(sta, mode, newidx) &&
		    (maxrate < 0 || newidx <= maxrate)) {
			sta->txrate = newidx;
			break;
		}

		newidx += back;
	}
M
Mattias Nissler 已提交
131 132 133 134 135 136

#ifdef CONFIG_MAC80211_DEBUGFS
	rate_control_pid_event_rate_change(
		&((struct rc_pid_sta_info *)sta->rate_ctrl_priv)->events,
		newidx, mode->rates[newidx].rate);
#endif
137 138
}

139
/* Normalize the failed frames per-rate differences. */
140
static void rate_control_pid_normalize(struct rc_pid_info *pinfo, int l)
141
{
142 143
	int i, norm_offset = pinfo->norm_offset;
	struct rc_pid_rateinfo *r = pinfo->rinfo;
144

145 146 147 148
	if (r[0].diff > norm_offset)
		r[0].diff -= norm_offset;
	else if (r[0].diff < -norm_offset)
		r[0].diff += norm_offset;
149
	for (i = 0; i < l - 1; i++)
150 151
		if (r[i + 1].diff > r[i].diff + norm_offset)
			r[i + 1].diff -= norm_offset;
152
		else if (r[i + 1].diff <= r[i].diff)
153
			r[i + 1].diff += norm_offset;
154 155
}

156 157 158 159 160
static void rate_control_pid_sample(struct rc_pid_info *pinfo,
				    struct ieee80211_local *local,
				    struct sta_info *sta)
{
	struct rc_pid_sta_info *spinfo = sta->rate_ctrl_priv;
161 162
	struct rc_pid_rateinfo *rinfo = pinfo->rinfo;
	struct ieee80211_hw_mode *mode;
163 164
	u32 pf;
	s32 err_avg;
165 166 167
	u32 err_prop;
	u32 err_int;
	u32 err_der;
168
	int adj, i, j, tmp;
169
	unsigned long period;
170

171
	mode = local->oper_hw_mode;
172
	spinfo = sta->rate_ctrl_priv;
173 174 175

	/* In case nothing happened during the previous control interval, turn
	 * the sharpening factor on. */
176 177 178 179 180
	period = (HZ * pinfo->sampling_period + 500) / 1000;
	if (!period)
		period = 1;
	if (jiffies - spinfo->last_sample > 2 * period)
		spinfo->sharp_cnt = pinfo->sharpen_duration;
181

182 183
	spinfo->last_sample = jiffies;

184
	/* This should never happen, but in case, we assume the old sample is
185
	 * still a good measurement and copy it. */
186
	if (unlikely(spinfo->tx_num_xmit == 0))
187 188 189 190 191 192
		pf = spinfo->last_pf;
	else {
		pf = spinfo->tx_num_failed * 100 / spinfo->tx_num_xmit;
		pf <<= RC_PID_ARITH_SHIFT;
	}

193 194 195
	spinfo->tx_num_xmit = 0;
	spinfo->tx_num_failed = 0;

196 197 198 199 200 201 202 203 204 205 206 207
	/* If we just switched rate, update the rate behaviour info. */
	if (pinfo->oldrate != sta->txrate) {

		i = rinfo[pinfo->oldrate].rev_index;
		j = rinfo[sta->txrate].rev_index;

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

		rinfo[j].diff = rinfo[i].diff + tmp;
		pinfo->oldrate = sta->txrate;
	}
208
	rate_control_pid_normalize(pinfo, mode->num_rates);
209

210
	/* Compute the proportional, integral and derivative errors. */
211
	err_prop = (pinfo->target << RC_PID_ARITH_SHIFT) - pf;
212

213
	err_avg = spinfo->err_avg_sc >> pinfo->smoothing_shift;
214
	spinfo->err_avg_sc = spinfo->err_avg_sc - err_avg + err_prop;
215
	err_int = spinfo->err_avg_sc >> pinfo->smoothing_shift;
216

217 218
	err_der = (pf - spinfo->last_pf) *
		  (1 + pinfo->sharpen_factor * spinfo->sharp_cnt);
219
	spinfo->last_pf = pf;
220 221
	if (spinfo->sharp_cnt)
			spinfo->sharp_cnt--;
222

M
Mattias Nissler 已提交
223 224 225 226 227
#ifdef CONFIG_MAC80211_DEBUGFS
	rate_control_pid_event_pf_sample(&spinfo->events, pf, err_prop, err_int,
					 err_der);
#endif

228 229 230
	/* Compute the controller output. */
	adj = (err_prop * pinfo->coeff_p + err_int * pinfo->coeff_i
	      + err_der * pinfo->coeff_d);
231
	adj = RC_PID_DO_ARITH_RIGHT_SHIFT(adj, 2 * RC_PID_ARITH_SHIFT);
232 233 234

	/* Change rate. */
	if (adj)
235
		rate_control_pid_adjust_rate(local, sta, adj, rinfo);
236 237 238 239 240 241 242 243 244 245 246
}

static void rate_control_pid_tx_status(void *priv, struct net_device *dev,
				       struct sk_buff *skb,
				       struct ieee80211_tx_status *status)
{
	struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
	struct rc_pid_info *pinfo = priv;
	struct sta_info *sta;
	struct rc_pid_sta_info *spinfo;
247
	unsigned long period;
248 249 250 251 252 253 254 255 256

	sta = sta_info_get(local, hdr->addr1);

	if (!sta)
		return;

	/* Ignore all frames that were sent with a different rate than the rate
	 * we currently advise mac80211 to use. */
	if (status->control.rate != &local->oper_hw_mode->rates[sta->txrate])
257
		goto ignore;
258 259 260 261

	spinfo = sta->rate_ctrl_priv;
	spinfo->tx_num_xmit++;

M
Mattias Nissler 已提交
262 263 264 265
#ifdef CONFIG_MAC80211_DEBUGFS
	rate_control_pid_event_tx_status(&spinfo->events, status);
#endif

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
	/* 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. */
	if (status->excessive_retries) {
		spinfo->tx_num_failed += 2;
		spinfo->tx_num_xmit++;
	} else if (status->retry_count) {
		spinfo->tx_num_failed++;
		spinfo->tx_num_xmit++;
	}

	if (status->excessive_retries) {
		sta->tx_retry_failed++;
		sta->tx_num_consecutive_failures++;
		sta->tx_num_mpdu_fail++;
	} else {
		sta->last_ack_rssi[0] = sta->last_ack_rssi[1];
		sta->last_ack_rssi[1] = sta->last_ack_rssi[2];
		sta->last_ack_rssi[2] = status->ack_signal;
		sta->tx_num_consecutive_failures = 0;
		sta->tx_num_mpdu_ok++;
	}
	sta->tx_retry_count += status->retry_count;
	sta->tx_num_mpdu_fail += status->retry_count;

	/* Update PID controller state. */
292 293 294 295
	period = (HZ * pinfo->sampling_period + 500) / 1000;
	if (!period)
		period = 1;
	if (time_after(jiffies, spinfo->last_sample + period))
296 297
		rate_control_pid_sample(pinfo, local, sta);

298
ignore:
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
	sta_info_put(sta);
}

static void rate_control_pid_get_rate(void *priv, struct net_device *dev,
				      struct ieee80211_hw_mode *mode,
				      struct sk_buff *skb,
				      struct rate_selection *sel)
{
	struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
	struct sta_info *sta;
	int rateidx;

	sta = sta_info_get(local, hdr->addr1);

	if (!sta) {
		sel->rate = rate_lowest(local, mode, NULL);
		sta_info_put(sta);
		return;
	}

	rateidx = sta->txrate;

	if (rateidx >= mode->num_rates)
		rateidx = mode->num_rates - 1;

	sta_info_put(sta);

	sel->rate = &mode->rates[rateidx];
M
Mattias Nissler 已提交
328 329 330 331 332 333

#ifdef CONFIG_MAC80211_DEBUGFS
	rate_control_pid_event_tx_rate(
		&((struct rc_pid_sta_info *) sta->rate_ctrl_priv)->events,
		rateidx, mode->rates[rateidx].rate);
#endif
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
}

static void rate_control_pid_rate_init(void *priv, void *priv_sta,
					  struct ieee80211_local *local,
					  struct sta_info *sta)
{
	/* 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. */
	sta->txrate = rate_lowest_index(local, local->oper_hw_mode, sta);
}

static void *rate_control_pid_alloc(struct ieee80211_local *local)
{
	struct rc_pid_info *pinfo;
350 351 352 353
	struct rc_pid_rateinfo *rinfo;
	struct ieee80211_hw_mode *mode;
	int i, j, tmp;
	bool s;
354 355 356
#ifdef CONFIG_MAC80211_DEBUGFS
	struct rc_pid_debugfs_entries *de;
#endif
357 358

	pinfo = kmalloc(sizeof(*pinfo), GFP_ATOMIC);
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
	if (!pinfo)
		return NULL;

	/* We can safely assume that oper_hw_mode won't change unless we get
	 * reinitialized. */
	mode = local->oper_hw_mode;
	rinfo = kmalloc(sizeof(*rinfo) * mode->num_rates, GFP_ATOMIC);
	if (!rinfo) {
		kfree(pinfo);
		return NULL;
	}

	/* 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 < mode->num_rates; i++) {
		rinfo[i].index = i;
		rinfo[i].rev_index = i;
377
		if (pinfo->fast_start)
378 379
			rinfo[i].diff = 0;
		else
380
			rinfo[i].diff = i * pinfo->norm_offset;
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
	}
	for (i = 1; i < mode->num_rates; i++) {
		s = 0;
		for (j = 0; j < mode->num_rates - i; j++)
			if (unlikely(mode->rates[rinfo[j].index].rate >
				     mode->rates[rinfo[j + 1].index].rate)) {
				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;
	}
397 398

	pinfo->target = RC_PID_TARGET_PF;
399
	pinfo->sampling_period = RC_PID_INTERVAL;
400 401 402
	pinfo->coeff_p = RC_PID_COEFF_P;
	pinfo->coeff_i = RC_PID_COEFF_I;
	pinfo->coeff_d = RC_PID_COEFF_D;
403 404 405 406 407
	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->fast_start = RC_PID_FAST_START;
408 409
	pinfo->rinfo = rinfo;
	pinfo->oldrate = 0;
410

411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
#ifdef CONFIG_MAC80211_DEBUGFS
	de = &pinfo->dentries;
	de->dir = debugfs_create_dir("rc80211_pid",
				     local->hw.wiphy->debugfsdir);
	de->target = debugfs_create_u32("target_pf", S_IRUSR | S_IWUSR,
					de->dir, &pinfo->target);
	de->sampling_period = debugfs_create_u32("sampling_period",
						 S_IRUSR | S_IWUSR, de->dir,
						 &pinfo->sampling_period);
	de->coeff_p = debugfs_create_u32("coeff_p", S_IRUSR | S_IWUSR,
					 de->dir, &pinfo->coeff_p);
	de->coeff_i = debugfs_create_u32("coeff_i", S_IRUSR | S_IWUSR,
					 de->dir, &pinfo->coeff_i);
	de->coeff_d = debugfs_create_u32("coeff_d", S_IRUSR | S_IWUSR,
					 de->dir, &pinfo->coeff_d);
	de->smoothing_shift = debugfs_create_u32("smoothing_shift",
						 S_IRUSR | S_IWUSR, de->dir,
						 &pinfo->smoothing_shift);
	de->sharpen_factor = debugfs_create_u32("sharpen_factor",
					       S_IRUSR | S_IWUSR, de->dir,
					       &pinfo->sharpen_factor);
	de->sharpen_duration = debugfs_create_u32("sharpen_duration",
						  S_IRUSR | S_IWUSR, de->dir,
						  &pinfo->sharpen_duration);
	de->norm_offset = debugfs_create_u32("norm_offset",
					     S_IRUSR | S_IWUSR, de->dir,
					     &pinfo->norm_offset);
	de->fast_start = debugfs_create_bool("fast_start",
					     S_IRUSR | S_IWUSR, de->dir,
					     &pinfo->fast_start);
#endif

443 444 445 446 447 448
	return pinfo;
}

static void rate_control_pid_free(void *priv)
{
	struct rc_pid_info *pinfo = priv;
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
#ifdef CONFIG_MAC80211_DEBUGFS
	struct rc_pid_debugfs_entries *de = &pinfo->dentries;

	debugfs_remove(de->fast_start);
	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);
	debugfs_remove(de->dir);
#endif

465
	kfree(pinfo->rinfo);
466 467 468 469 470 471 472 473 474 475 476 477
	kfree(pinfo);
}

static void rate_control_pid_clear(void *priv)
{
}

static void *rate_control_pid_alloc_sta(void *priv, gfp_t gfp)
{
	struct rc_pid_sta_info *spinfo;

	spinfo = kzalloc(sizeof(*spinfo), gfp);
M
Mattias Nissler 已提交
478 479 480 481 482 483 484
	if (spinfo == NULL)
		return NULL;

#ifdef CONFIG_MAC80211_DEBUGFS
	spin_lock_init(&spinfo->events.lock);
	init_waitqueue_head(&spinfo->events.waitqueue);
#endif
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504

	return spinfo;
}

static void rate_control_pid_free_sta(void *priv, void *priv_sta)
{
	struct rc_pid_sta_info *spinfo = priv_sta;
	kfree(spinfo);
}

struct rate_control_ops mac80211_rcpid = {
	.name = "pid",
	.tx_status = rate_control_pid_tx_status,
	.get_rate = rate_control_pid_get_rate,
	.rate_init = rate_control_pid_rate_init,
	.clear = rate_control_pid_clear,
	.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 已提交
505 506 507 508
#ifdef CONFIG_MAC80211_DEBUGFS
	.add_sta_debugfs = rate_control_pid_add_sta_debugfs,
	.remove_sta_debugfs = rate_control_pid_remove_sta_debugfs,
#endif
509
};