sch_netem.c 17.4 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6
/*
 * net/sched/sch_netem.c	Network emulator
 *
 * 		This program is free software; you can redistribute it and/or
 * 		modify it under the terms of the GNU General Public License
 * 		as published by the Free Software Foundation; either version
7
 * 		2 of the License.
Linus Torvalds's avatar
Linus Torvalds committed
8 9
 *
 *  		Many of the algorithms and ideas for this came from
10
 *		NIST Net which is not copyrighted.
Linus Torvalds's avatar
Linus Torvalds committed
11 12 13 14 15 16 17 18 19 20 21 22
 *
 * Authors:	Stephen Hemminger <shemminger@osdl.org>
 *		Catalin(ux aka Dino) BOIE <catab at umbrella dot ro>
 */

#include <linux/module.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/skbuff.h>
#include <linux/rtnetlink.h>

23
#include <net/netlink.h>
Linus Torvalds's avatar
Linus Torvalds committed
24 25
#include <net/pkt_sched.h>

26
#define VERSION "1.2"
27

Linus Torvalds's avatar
Linus Torvalds committed
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
/*	Network Emulation Queuing algorithm.
	====================================

	Sources: [1] Mark Carson, Darrin Santay, "NIST Net - A Linux-based
		 Network Emulation Tool
		 [2] Luigi Rizzo, DummyNet for FreeBSD

	 ----------------------------------------------------------------

	 This started out as a simple way to delay outgoing packets to
	 test TCP but has grown to include most of the functionality
	 of a full blown network emulator like NISTnet. It can delay
	 packets and add random jitter (and correlation). The random
	 distribution can be loaded from a table as well to provide
	 normal, Pareto, or experimental curves. Packet loss,
	 duplication, and reordering can also be emulated.

	 This qdisc does not do classification that can be handled in
	 layering other disciplines.  It does not need to do bandwidth
	 control either since that can be handled by using token
	 bucket or other rate control.

	 The simulator is limited by the Linux timer resolution
	 and will create packet bursts on the HZ boundary (1ms).
*/

struct netem_sched_data {
	struct Qdisc	*qdisc;
56
	struct qdisc_watchdog watchdog;
Linus Torvalds's avatar
Linus Torvalds committed
57

58 59 60
	psched_tdiff_t latency;
	psched_tdiff_t jitter;

Linus Torvalds's avatar
Linus Torvalds committed
61 62 63 64 65
	u32 loss;
	u32 limit;
	u32 counter;
	u32 gap;
	u32 duplicate;
66
	u32 reorder;
67
	u32 corrupt;
Linus Torvalds's avatar
Linus Torvalds committed
68 69

	struct crndstate {
70 71
		u32 last;
		u32 rho;
72
	} delay_cor, loss_cor, dup_cor, reorder_cor, corrupt_cor;
Linus Torvalds's avatar
Linus Torvalds committed
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

	struct disttable {
		u32  size;
		s16 table[0];
	} *delay_dist;
};

/* Time stamp put into socket buffer control block */
struct netem_skb_cb {
	psched_time_t	time_to_send;
};

/* init_crandom - initialize correlated random number generator
 * Use entropy source for initial seed.
 */
static void init_crandom(struct crndstate *state, unsigned long rho)
{
	state->rho = rho;
	state->last = net_random();
}

/* get_crandom - correlated random number generator
 * Next number depends on last value.
 * rho is scaled to avoid floating point.
 */
98
static u32 get_crandom(struct crndstate *state)
Linus Torvalds's avatar
Linus Torvalds committed
99 100 101 102
{
	u64 value, rho;
	unsigned long answer;

Stephen Hemminger's avatar
Stephen Hemminger committed
103
	if (state->rho == 0)	/* no correlation */
Linus Torvalds's avatar
Linus Torvalds committed
104 105 106 107 108 109 110 111 112 113 114 115 116
		return net_random();

	value = net_random();
	rho = (u64)state->rho + 1;
	answer = (value * ((1ull<<32) - rho) + state->last * rho) >> 32;
	state->last = answer;
	return answer;
}

/* tabledist - return a pseudo-randomly distributed value with mean mu and
 * std deviation sigma.  Uses table lookup to approximate the desired
 * distribution, and a uniformly-distributed pseudo-random source.
 */
117 118 119
static psched_tdiff_t tabledist(psched_tdiff_t mu, psched_tdiff_t sigma,
				struct crndstate *state,
				const struct disttable *dist)
Linus Torvalds's avatar
Linus Torvalds committed
120
{
121 122 123
	psched_tdiff_t x;
	long t;
	u32 rnd;
Linus Torvalds's avatar
Linus Torvalds committed
124 125 126 127 128 129 130

	if (sigma == 0)
		return mu;

	rnd = get_crandom(state);

	/* default uniform distribution */
131
	if (dist == NULL)
Linus Torvalds's avatar
Linus Torvalds committed
132 133 134 135 136 137 138 139 140 141 142 143
		return (rnd % (2*sigma)) - sigma + mu;

	t = dist->table[rnd % dist->size];
	x = (sigma % NETEM_DIST_SCALE) * t;
	if (x >= 0)
		x += NETEM_DIST_SCALE/2;
	else
		x -= NETEM_DIST_SCALE/2;

	return  x / NETEM_DIST_SCALE + (sigma / NETEM_DIST_SCALE) * t + mu;
}

144 145 146 147 148 149
/*
 * Insert one skb into qdisc.
 * Note: parent depends on return value to account for queue length.
 * 	NET_XMIT_DROP: queue length didn't change.
 *      NET_XMIT_SUCCESS: one skb was queued.
 */
Linus Torvalds's avatar
Linus Torvalds committed
150 151 152
static int netem_enqueue(struct sk_buff *skb, struct Qdisc *sch)
{
	struct netem_sched_data *q = qdisc_priv(sch);
153 154
	/* We don't fill cb now as skb_unshare() may invalidate it */
	struct netem_skb_cb *cb;
155
	struct sk_buff *skb2;
Linus Torvalds's avatar
Linus Torvalds committed
156
	int ret;
157
	int count = 1;
Linus Torvalds's avatar
Linus Torvalds committed
158

159
	pr_debug("netem_enqueue skb=%p\n", skb);
Linus Torvalds's avatar
Linus Torvalds committed
160

161 162 163 164
	/* Random duplication */
	if (q->duplicate && q->duplicate >= get_crandom(&q->dup_cor))
		++count;

Linus Torvalds's avatar
Linus Torvalds committed
165
	/* Random packet drop 0 => none, ~0 => all */
166 167 168 169
	if (q->loss && q->loss >= get_crandom(&q->loss_cor))
		--count;

	if (count == 0) {
Linus Torvalds's avatar
Linus Torvalds committed
170 171
		sch->qstats.drops++;
		kfree_skb(skb);
172
		return NET_XMIT_BYPASS;
Linus Torvalds's avatar
Linus Torvalds committed
173 174
	}

175 176
	skb_orphan(skb);

177 178 179 180 181 182 183 184 185 186 187 188
	/*
	 * If we need to duplicate packet, then re-insert at top of the
	 * qdisc tree, since parent queuer expects that only one
	 * skb will be queued.
	 */
	if (count > 1 && (skb2 = skb_clone(skb, GFP_ATOMIC)) != NULL) {
		struct Qdisc *rootq = sch->dev->qdisc;
		u32 dupsave = q->duplicate; /* prevent duplicating a dup... */
		q->duplicate = 0;

		rootq->enqueue(skb2, rootq);
		q->duplicate = dupsave;
Linus Torvalds's avatar
Linus Torvalds committed
189 190
	}

191 192 193 194 195 196 197 198
	/*
	 * Randomized packet corruption.
	 * Make copy if needed since we are modifying
	 * If packet is going to be hardware checksummed, then
	 * do it now in software before we mangle it.
	 */
	if (q->corrupt && q->corrupt >= get_crandom(&q->corrupt_cor)) {
		if (!(skb = skb_unshare(skb, GFP_ATOMIC))
199 200
		    || (skb->ip_summed == CHECKSUM_PARTIAL
			&& skb_checksum_help(skb))) {
201 202 203 204 205 206 207
			sch->qstats.drops++;
			return NET_XMIT_DROP;
		}

		skb->data[net_random() % skb_headlen(skb)] ^= 1<<(net_random() % 8);
	}

208
	cb = (struct netem_skb_cb *)skb->cb;
209 210 211
	if (q->gap == 0 		/* not doing reordering */
	    || q->counter < q->gap 	/* inside last reordering gap */
	    || q->reorder < get_crandom(&q->reorder_cor)) {
212
		psched_time_t now;
Stephen Hemminger's avatar
Stephen Hemminger committed
213 214 215 216 217
		psched_tdiff_t delay;

		delay = tabledist(q->latency, q->jitter,
				  &q->delay_cor, q->delay_dist);

218
		now = psched_get_time();
219
		cb->time_to_send = now + delay;
Linus Torvalds's avatar
Linus Torvalds committed
220 221 222
		++q->counter;
		ret = q->qdisc->enqueue(skb, q->qdisc);
	} else {
223
		/*
224 225 226
		 * Do re-ordering by putting one out of N packets at the front
		 * of the queue.
		 */
227
		cb->time_to_send = psched_get_time();
228
		q->counter = 0;
229
		ret = q->qdisc->ops->requeue(skb, q->qdisc);
Linus Torvalds's avatar
Linus Torvalds committed
230 231 232 233 234 235 236 237 238
	}

	if (likely(ret == NET_XMIT_SUCCESS)) {
		sch->q.qlen++;
		sch->bstats.bytes += skb->len;
		sch->bstats.packets++;
	} else
		sch->qstats.drops++;

239
	pr_debug("netem: enqueue ret %d\n", ret);
Linus Torvalds's avatar
Linus Torvalds committed
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
	return ret;
}

/* Requeue packets but don't change time stamp */
static int netem_requeue(struct sk_buff *skb, struct Qdisc *sch)
{
	struct netem_sched_data *q = qdisc_priv(sch);
	int ret;

	if ((ret = q->qdisc->ops->requeue(skb, q->qdisc)) == 0) {
		sch->q.qlen++;
		sch->qstats.requeues++;
	}

	return ret;
}

static unsigned int netem_drop(struct Qdisc* sch)
{
	struct netem_sched_data *q = qdisc_priv(sch);
260
	unsigned int len = 0;
Linus Torvalds's avatar
Linus Torvalds committed
261

262
	if (q->qdisc->ops->drop && (len = q->qdisc->ops->drop(q->qdisc)) != 0) {
Linus Torvalds's avatar
Linus Torvalds committed
263 264 265 266 267 268 269 270 271 272 273
		sch->q.qlen--;
		sch->qstats.drops++;
	}
	return len;
}

static struct sk_buff *netem_dequeue(struct Qdisc *sch)
{
	struct netem_sched_data *q = qdisc_priv(sch);
	struct sk_buff *skb;

274 275 276 277
	smp_mb();
	if (sch->flags & TCQ_F_THROTTLED)
		return NULL;

Linus Torvalds's avatar
Linus Torvalds committed
278
	skb = q->qdisc->dequeue(q->qdisc);
279
	if (skb) {
280 281
		const struct netem_skb_cb *cb
			= (const struct netem_skb_cb *)skb->cb;
282
		psched_time_t now = psched_get_time();
283 284

		/* if more time remaining? */
285
		if (cb->time_to_send <= now) {
286 287 288
			pr_debug("netem_dequeue: return skb=%p\n", skb);
			sch->q.qlen--;
			return skb;
Stephen Hemminger's avatar
Stephen Hemminger committed
289
		}
290 291 292 293 294 295 296 297 298

		if (unlikely(q->qdisc->ops->requeue(skb, q->qdisc) != NET_XMIT_SUCCESS)) {
			qdisc_tree_decrease_qlen(q->qdisc, 1);
			sch->qstats.drops++;
			printk(KERN_ERR "netem: %s could not requeue\n",
			       q->qdisc->ops->id);
		}

		qdisc_watchdog_schedule(&q->watchdog, cb->time_to_send);
299 300 301
	}

	return NULL;
Linus Torvalds's avatar
Linus Torvalds committed
302 303 304 305 306 307 308 309
}

static void netem_reset(struct Qdisc *sch)
{
	struct netem_sched_data *q = qdisc_priv(sch);

	qdisc_reset(q->qdisc);
	sch->q.qlen = 0;
310
	qdisc_watchdog_cancel(&q->watchdog);
Linus Torvalds's avatar
Linus Torvalds committed
311 312
}

313
/* Pass size change message down to embedded FIFO */
Linus Torvalds's avatar
Linus Torvalds committed
314 315
static int set_fifo_limit(struct Qdisc *q, int limit)
{
316
	struct nlattr *nla;
Linus Torvalds's avatar
Linus Torvalds committed
317 318
	int ret = -ENOMEM;

319 320 321 322
	/* Hack to avoid sending change message to non-FIFO */
	if (strncmp(q->ops->id + 1, "fifo", 4) != 0)
		return 0;

323 324 325 326 327
	nla = kmalloc(nla_attr_size(sizeof(struct tc_fifo_qopt)), GFP_KERNEL);
	if (nla) {
		nla->nla_type = RTM_NEWQDISC;
		nla->nla_len = nla_attr_size(sizeof(struct tc_fifo_qopt));
		((struct tc_fifo_qopt *)nla_data(nla))->limit = limit;
328

329 330
		ret = q->ops->change(q, nla);
		kfree(nla);
Linus Torvalds's avatar
Linus Torvalds committed
331 332 333 334 335 336 337 338
	}
	return ret;
}

/*
 * Distribution data is a variable size payload containing
 * signed 16 bit values.
 */
339
static int get_dist_table(struct Qdisc *sch, const struct nlattr *attr)
Linus Torvalds's avatar
Linus Torvalds committed
340 341
{
	struct netem_sched_data *q = qdisc_priv(sch);
342 343
	unsigned long n = nla_len(attr)/sizeof(__s16);
	const __s16 *data = nla_data(attr);
Linus Torvalds's avatar
Linus Torvalds committed
344 345 346 347 348 349 350 351 352 353 354 355 356
	struct disttable *d;
	int i;

	if (n > 65536)
		return -EINVAL;

	d = kmalloc(sizeof(*d) + n*sizeof(d->table[0]), GFP_KERNEL);
	if (!d)
		return -ENOMEM;

	d->size = n;
	for (i = 0; i < n; i++)
		d->table[i] = data[i];
357

Linus Torvalds's avatar
Linus Torvalds committed
358 359 360 361 362 363 364 365
	spin_lock_bh(&sch->dev->queue_lock);
	d = xchg(&q->delay_dist, d);
	spin_unlock_bh(&sch->dev->queue_lock);

	kfree(d);
	return 0;
}

366
static int get_correlation(struct Qdisc *sch, const struct nlattr *attr)
Linus Torvalds's avatar
Linus Torvalds committed
367 368
{
	struct netem_sched_data *q = qdisc_priv(sch);
369
	const struct tc_netem_corr *c = nla_data(attr);
Linus Torvalds's avatar
Linus Torvalds committed
370

371
	if (nla_len(attr) != sizeof(*c))
Linus Torvalds's avatar
Linus Torvalds committed
372 373 374 375 376 377 378 379
		return -EINVAL;

	init_crandom(&q->delay_cor, c->delay_corr);
	init_crandom(&q->loss_cor, c->loss_corr);
	init_crandom(&q->dup_cor, c->dup_corr);
	return 0;
}

380
static int get_reorder(struct Qdisc *sch, const struct nlattr *attr)
381 382
{
	struct netem_sched_data *q = qdisc_priv(sch);
383
	const struct tc_netem_reorder *r = nla_data(attr);
384

385
	if (nla_len(attr) != sizeof(*r))
386 387 388 389 390 391 392
		return -EINVAL;

	q->reorder = r->probability;
	init_crandom(&q->reorder_cor, r->correlation);
	return 0;
}

393
static int get_corrupt(struct Qdisc *sch, const struct nlattr *attr)
394 395
{
	struct netem_sched_data *q = qdisc_priv(sch);
396
	const struct tc_netem_corrupt *r = nla_data(attr);
397

398
	if (nla_len(attr) != sizeof(*r))
399 400 401 402 403 404 405 406
		return -EINVAL;

	q->corrupt = r->probability;
	init_crandom(&q->corrupt_cor, r->correlation);
	return 0;
}

/* Parse netlink message to set options */
407
static int netem_change(struct Qdisc *sch, struct nlattr *opt)
Linus Torvalds's avatar
Linus Torvalds committed
408 409 410 411
{
	struct netem_sched_data *q = qdisc_priv(sch);
	struct tc_netem_qopt *qopt;
	int ret;
412

413
	if (opt == NULL || nla_len(opt) < sizeof(*qopt))
Linus Torvalds's avatar
Linus Torvalds committed
414 415
		return -EINVAL;

416
	qopt = nla_data(opt);
Linus Torvalds's avatar
Linus Torvalds committed
417 418 419 420 421
	ret = set_fifo_limit(q->qdisc, qopt->limit);
	if (ret) {
		pr_debug("netem: can't set fifo limit\n");
		return ret;
	}
422

Linus Torvalds's avatar
Linus Torvalds committed
423 424 425 426
	q->latency = qopt->latency;
	q->jitter = qopt->jitter;
	q->limit = qopt->limit;
	q->gap = qopt->gap;
427
	q->counter = 0;
Linus Torvalds's avatar
Linus Torvalds committed
428 429 430
	q->loss = qopt->loss;
	q->duplicate = qopt->duplicate;

Stephen Hemminger's avatar
Stephen Hemminger committed
431 432
	/* for compatibility with earlier versions.
	 * if gap is set, need to assume 100% probability
433
	 */
434 435
	if (q->gap)
		q->reorder = ~0;
436

Linus Torvalds's avatar
Linus Torvalds committed
437 438
	/* Handle nested options after initial queue options.
	 * Should have put all options in nested format but too late now.
439
	 */
440 441 442 443 444
	if (nla_len(opt) > sizeof(*qopt)) {
		struct nlattr *tb[TCA_NETEM_MAX + 1];
		if (nla_parse(tb, TCA_NETEM_MAX,
			      nla_data(opt) + sizeof(*qopt),
			      nla_len(opt) - sizeof(*qopt), NULL))
Linus Torvalds's avatar
Linus Torvalds committed
445 446
			return -EINVAL;

447 448
		if (tb[TCA_NETEM_CORR]) {
			ret = get_correlation(sch, tb[TCA_NETEM_CORR]);
Linus Torvalds's avatar
Linus Torvalds committed
449 450 451 452
			if (ret)
				return ret;
		}

453 454
		if (tb[TCA_NETEM_DELAY_DIST]) {
			ret = get_dist_table(sch, tb[TCA_NETEM_DELAY_DIST]);
Linus Torvalds's avatar
Linus Torvalds committed
455 456 457
			if (ret)
				return ret;
		}
458

459 460
		if (tb[TCA_NETEM_REORDER]) {
			ret = get_reorder(sch, tb[TCA_NETEM_REORDER]);
461 462 463
			if (ret)
				return ret;
		}
Linus Torvalds's avatar
Linus Torvalds committed
464

465 466
		if (tb[TCA_NETEM_CORRUPT]) {
			ret = get_corrupt(sch, tb[TCA_NETEM_CORRUPT]);
467 468 469 470
			if (ret)
				return ret;
		}
	}
Linus Torvalds's avatar
Linus Torvalds committed
471 472 473 474

	return 0;
}

475 476 477 478 479 480
/*
 * Special case version of FIFO queue for use by netem.
 * It queues in order based on timestamps in skb's
 */
struct fifo_sched_data {
	u32 limit;
Stephen Hemminger's avatar
Stephen Hemminger committed
481
	psched_time_t oldest;
482 483 484 485 486 487
};

static int tfifo_enqueue(struct sk_buff *nskb, struct Qdisc *sch)
{
	struct fifo_sched_data *q = qdisc_priv(sch);
	struct sk_buff_head *list = &sch->q;
Stephen Hemminger's avatar
Stephen Hemminger committed
488
	psched_time_t tnext = ((struct netem_skb_cb *)nskb->cb)->time_to_send;
489 490 491
	struct sk_buff *skb;

	if (likely(skb_queue_len(list) < q->limit)) {
Stephen Hemminger's avatar
Stephen Hemminger committed
492
		/* Optimize for add at tail */
493
		if (likely(skb_queue_empty(list) || tnext >= q->oldest)) {
Stephen Hemminger's avatar
Stephen Hemminger committed
494 495 496 497
			q->oldest = tnext;
			return qdisc_enqueue_tail(nskb, sch);
		}

498 499 500 501
		skb_queue_reverse_walk(list, skb) {
			const struct netem_skb_cb *cb
				= (const struct netem_skb_cb *)skb->cb;

502
			if (tnext >= cb->time_to_send)
503 504 505 506 507 508 509 510 511 512 513 514
				break;
		}

		__skb_queue_after(list, skb, nskb);

		sch->qstats.backlog += nskb->len;
		sch->bstats.bytes += nskb->len;
		sch->bstats.packets++;

		return NET_XMIT_SUCCESS;
	}

Stephen Hemminger's avatar
Stephen Hemminger committed
515
	return qdisc_reshape_fail(nskb, sch);
516 517
}

518
static int tfifo_init(struct Qdisc *sch, struct nlattr *opt)
519 520 521 522
{
	struct fifo_sched_data *q = qdisc_priv(sch);

	if (opt) {
523 524
		struct tc_fifo_qopt *ctl = nla_data(opt);
		if (nla_len(opt) < sizeof(*ctl))
525 526 527 528 529 530
			return -EINVAL;

		q->limit = ctl->limit;
	} else
		q->limit = max_t(u32, sch->dev->tx_queue_len, 1);

531
	q->oldest = PSCHED_PASTPERFECT;
532 533 534 535 536 537 538 539
	return 0;
}

static int tfifo_dump(struct Qdisc *sch, struct sk_buff *skb)
{
	struct fifo_sched_data *q = qdisc_priv(sch);
	struct tc_fifo_qopt opt = { .limit = q->limit };

540
	NLA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
541 542
	return skb->len;

543
nla_put_failure:
544 545 546
	return -1;
}

547
static struct Qdisc_ops tfifo_qdisc_ops __read_mostly = {
548 549 550 551 552 553 554 555 556 557 558 559
	.id		=	"tfifo",
	.priv_size	=	sizeof(struct fifo_sched_data),
	.enqueue	=	tfifo_enqueue,
	.dequeue	=	qdisc_dequeue_head,
	.requeue	=	qdisc_requeue,
	.drop		=	qdisc_queue_drop,
	.init		=	tfifo_init,
	.reset		=	qdisc_reset_queue,
	.change		=	tfifo_init,
	.dump		=	tfifo_dump,
};

560
static int netem_init(struct Qdisc *sch, struct nlattr *opt)
Linus Torvalds's avatar
Linus Torvalds committed
561 562 563 564 565 566 567
{
	struct netem_sched_data *q = qdisc_priv(sch);
	int ret;

	if (!opt)
		return -EINVAL;

568
	qdisc_watchdog_init(&q->watchdog, sch);
Linus Torvalds's avatar
Linus Torvalds committed
569

570 571
	q->qdisc = qdisc_create_dflt(sch->dev, &tfifo_qdisc_ops,
				     TC_H_MAKE(sch->handle, 1));
Linus Torvalds's avatar
Linus Torvalds committed
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
	if (!q->qdisc) {
		pr_debug("netem: qdisc create failed\n");
		return -ENOMEM;
	}

	ret = netem_change(sch, opt);
	if (ret) {
		pr_debug("netem: change failed\n");
		qdisc_destroy(q->qdisc);
	}
	return ret;
}

static void netem_destroy(struct Qdisc *sch)
{
	struct netem_sched_data *q = qdisc_priv(sch);

589
	qdisc_watchdog_cancel(&q->watchdog);
Linus Torvalds's avatar
Linus Torvalds committed
590 591 592 593 594 595 596
	qdisc_destroy(q->qdisc);
	kfree(q->delay_dist);
}

static int netem_dump(struct Qdisc *sch, struct sk_buff *skb)
{
	const struct netem_sched_data *q = qdisc_priv(sch);
597
	unsigned char *b = skb_tail_pointer(skb);
598
	struct nlattr *nla = (struct nlattr *) b;
Linus Torvalds's avatar
Linus Torvalds committed
599 600
	struct tc_netem_qopt qopt;
	struct tc_netem_corr cor;
601
	struct tc_netem_reorder reorder;
602
	struct tc_netem_corrupt corrupt;
Linus Torvalds's avatar
Linus Torvalds committed
603 604 605 606 607 608 609

	qopt.latency = q->latency;
	qopt.jitter = q->jitter;
	qopt.limit = q->limit;
	qopt.loss = q->loss;
	qopt.gap = q->gap;
	qopt.duplicate = q->duplicate;
610
	NLA_PUT(skb, TCA_OPTIONS, sizeof(qopt), &qopt);
Linus Torvalds's avatar
Linus Torvalds committed
611 612 613 614

	cor.delay_corr = q->delay_cor.rho;
	cor.loss_corr = q->loss_cor.rho;
	cor.dup_corr = q->dup_cor.rho;
615
	NLA_PUT(skb, TCA_NETEM_CORR, sizeof(cor), &cor);
616 617 618

	reorder.probability = q->reorder;
	reorder.correlation = q->reorder_cor.rho;
619
	NLA_PUT(skb, TCA_NETEM_REORDER, sizeof(reorder), &reorder);
620

621 622
	corrupt.probability = q->corrupt;
	corrupt.correlation = q->corrupt_cor.rho;
623
	NLA_PUT(skb, TCA_NETEM_CORRUPT, sizeof(corrupt), &corrupt);
624

625
	nla->nla_len = skb_tail_pointer(skb) - b;
Linus Torvalds's avatar
Linus Torvalds committed
626 627 628

	return skb->len;

629
nla_put_failure:
630
	nlmsg_trim(skb, b);
Linus Torvalds's avatar
Linus Torvalds committed
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
	return -1;
}

static int netem_dump_class(struct Qdisc *sch, unsigned long cl,
			  struct sk_buff *skb, struct tcmsg *tcm)
{
	struct netem_sched_data *q = qdisc_priv(sch);

	if (cl != 1) 	/* only one class */
		return -ENOENT;

	tcm->tcm_handle |= TC_H_MIN(1);
	tcm->tcm_info = q->qdisc->handle;

	return 0;
}

static int netem_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
		     struct Qdisc **old)
{
	struct netem_sched_data *q = qdisc_priv(sch);

	if (new == NULL)
		new = &noop_qdisc;

	sch_tree_lock(sch);
	*old = xchg(&q->qdisc, new);
658
	qdisc_tree_decrease_qlen(*old, (*old)->q.qlen);
Linus Torvalds's avatar
Linus Torvalds committed
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
	qdisc_reset(*old);
	sch_tree_unlock(sch);

	return 0;
}

static struct Qdisc *netem_leaf(struct Qdisc *sch, unsigned long arg)
{
	struct netem_sched_data *q = qdisc_priv(sch);
	return q->qdisc;
}

static unsigned long netem_get(struct Qdisc *sch, u32 classid)
{
	return 1;
}

static void netem_put(struct Qdisc *sch, unsigned long arg)
{
}

680
static int netem_change_class(struct Qdisc *sch, u32 classid, u32 parentid,
681
			    struct nlattr **tca, unsigned long *arg)
Linus Torvalds's avatar
Linus Torvalds committed
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
{
	return -ENOSYS;
}

static int netem_delete(struct Qdisc *sch, unsigned long arg)
{
	return -ENOSYS;
}

static void netem_walk(struct Qdisc *sch, struct qdisc_walker *walker)
{
	if (!walker->stop) {
		if (walker->count >= walker->skip)
			if (walker->fn(sch, 1, walker) < 0) {
				walker->stop = 1;
				return;
			}
		walker->count++;
	}
}

static struct tcf_proto **netem_find_tcf(struct Qdisc *sch, unsigned long cl)
{
	return NULL;
}

708
static const struct Qdisc_class_ops netem_class_ops = {
Linus Torvalds's avatar
Linus Torvalds committed
709 710 711 712 713 714 715 716 717 718 719
	.graft		=	netem_graft,
	.leaf		=	netem_leaf,
	.get		=	netem_get,
	.put		=	netem_put,
	.change		=	netem_change_class,
	.delete		=	netem_delete,
	.walk		=	netem_walk,
	.tcf_chain	=	netem_find_tcf,
	.dump		=	netem_dump_class,
};

720
static struct Qdisc_ops netem_qdisc_ops __read_mostly = {
Linus Torvalds's avatar
Linus Torvalds committed
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
	.id		=	"netem",
	.cl_ops		=	&netem_class_ops,
	.priv_size	=	sizeof(struct netem_sched_data),
	.enqueue	=	netem_enqueue,
	.dequeue	=	netem_dequeue,
	.requeue	=	netem_requeue,
	.drop		=	netem_drop,
	.init		=	netem_init,
	.reset		=	netem_reset,
	.destroy	=	netem_destroy,
	.change		=	netem_change,
	.dump		=	netem_dump,
	.owner		=	THIS_MODULE,
};


static int __init netem_module_init(void)
{
739
	pr_info("netem: version " VERSION "\n");
Linus Torvalds's avatar
Linus Torvalds committed
740 741 742 743 744 745 746 747 748
	return register_qdisc(&netem_qdisc_ops);
}
static void __exit netem_module_exit(void)
{
	unregister_qdisc(&netem_qdisc_ops);
}
module_init(netem_module_init)
module_exit(netem_module_exit)
MODULE_LICENSE("GPL");