sch_cbq.c 47.6 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * net/sched/sch_cbq.c	Class-Based Queueing discipline.
 *
 *		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
 *		2 of the License, or (at your option) any later version.
 *
 * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
 *
 */

#include <linux/module.h>
14
#include <linux/slab.h>
L
Linus Torvalds 已提交
15 16 17 18 19
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/errno.h>
#include <linux/skbuff.h>
20
#include <net/netlink.h>
L
Linus Torvalds 已提交
21 22 23 24 25 26 27
#include <net/pkt_sched.h>


/*	Class-Based Queueing (CBQ) algorithm.
	=======================================

	Sources: [1] Sally Floyd and Van Jacobson, "Link-sharing and Resource
28
		 Management Models for Packet Networks",
L
Linus Torvalds 已提交
29 30
		 IEEE/ACM Transactions on Networking, Vol.3, No.4, 1995

31
		 [2] Sally Floyd, "Notes on CBQ and Guaranteed Service", 1995
L
Linus Torvalds 已提交
32

33
		 [3] Sally Floyd, "Notes on Class-Based Queueing: Setting
L
Linus Torvalds 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46
		 Parameters", 1996

		 [4] Sally Floyd and Michael Speer, "Experimental Results
		 for Class-Based Queueing", 1998, not published.

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

	Algorithm skeleton was taken from NS simulator cbq.cc.
	If someone wants to check this code against the LBL version,
	he should take into account that ONLY the skeleton was borrowed,
	the implementation is different. Particularly:

	--- The WRR algorithm is different. Our version looks more
47 48 49 50 51 52
	reasonable (I hope) and works when quanta are allowed to be
	less than MTU, which is always the case when real time classes
	have small rates. Note, that the statement of [3] is
	incomplete, delay may actually be estimated even if class
	per-round allotment is less than MTU. Namely, if per-round
	allotment is W*r_i, and r_1+...+r_k = r < 1
L
Linus Torvalds 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

	delay_i <= ([MTU/(W*r_i)]*W*r + W*r + k*MTU)/B

	In the worst case we have IntServ estimate with D = W*r+k*MTU
	and C = MTU*r. The proof (if correct at all) is trivial.


	--- It seems that cbq-2.0 is not very accurate. At least, I cannot
	interpret some places, which look like wrong translations
	from NS. Anyone is advised to find these differences
	and explain to me, why I am wrong 8).

	--- Linux has no EOI event, so that we cannot estimate true class
	idle time. Workaround is to consider the next dequeue event
	as sign that previous packet is finished. This is wrong because of
	internal device queueing, but on a permanently loaded link it is true.
	Moreover, combined with clock integrator, this scheme looks
	very close to an ideal solution.  */

struct cbq_sched_data;


E
Eric Dumazet 已提交
75
struct cbq_class {
76
	struct Qdisc_class_common common;
L
Linus Torvalds 已提交
77 78 79 80 81 82 83
	struct cbq_class	*next_alive;	/* next class with backlog in this priority band */

/* Parameters */
	unsigned char		priority;	/* class priority */
	unsigned char		priority2;	/* priority to be used after overlimit */
	unsigned char		ewma_log;	/* time constant for idle time calculation */
	unsigned char		ovl_strategy;
84
#ifdef CONFIG_NET_CLS_ACT
L
Linus Torvalds 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98
	unsigned char		police;
#endif

	u32			defmap;

	/* Link-sharing scheduler parameters */
	long			maxidle;	/* Class parameters: see below. */
	long			offtime;
	long			minidle;
	u32			avpkt;
	struct qdisc_rate_table	*R_tab;

	/* Overlimit strategy parameters */
	void			(*overlimit)(struct cbq_class *cl);
99
	psched_tdiff_t		penalty;
L
Linus Torvalds 已提交
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

	/* General scheduler (WRR) parameters */
	long			allot;
	long			quantum;	/* Allotment per WRR round */
	long			weight;		/* Relative allotment: see below */

	struct Qdisc		*qdisc;		/* Ptr to CBQ discipline */
	struct cbq_class	*split;		/* Ptr to split node */
	struct cbq_class	*share;		/* Ptr to LS parent in the class tree */
	struct cbq_class	*tparent;	/* Ptr to tree parent in the class tree */
	struct cbq_class	*borrow;	/* NULL if class is bandwidth limited;
						   parent otherwise */
	struct cbq_class	*sibling;	/* Sibling chain */
	struct cbq_class	*children;	/* Pointer to children chain */

	struct Qdisc		*q;		/* Elementary queueing discipline */


/* Variables */
	unsigned char		cpriority;	/* Effective priority */
	unsigned char		delayed;
	unsigned char		level;		/* level of the class in hierarchy:
						   0 for leaf classes, and maximal
						   level of children + 1 for nodes.
						 */

	psched_time_t		last;		/* Last end of service */
	psched_time_t		undertime;
	long			avgidle;
	long			deficit;	/* Saved deficit for WRR */
130
	psched_time_t		penalized;
131
	struct gnet_stats_basic_packed bstats;
L
Linus Torvalds 已提交
132
	struct gnet_stats_queue qstats;
133
	struct gnet_stats_rate_est64 rate_est;
L
Linus Torvalds 已提交
134 135 136 137 138 139 140
	struct tc_cbq_xstats	xstats;

	struct tcf_proto	*filter_list;

	int			refcnt;
	int			filters;

E
Eric Dumazet 已提交
141
	struct cbq_class	*defaults[TC_PRIO_MAX + 1];
L
Linus Torvalds 已提交
142 143
};

E
Eric Dumazet 已提交
144
struct cbq_sched_data {
145
	struct Qdisc_class_hash	clhash;			/* Hash table of all classes */
E
Eric Dumazet 已提交
146 147
	int			nclasses[TC_CBQ_MAXPRIO + 1];
	unsigned int		quanta[TC_CBQ_MAXPRIO + 1];
L
Linus Torvalds 已提交
148 149 150

	struct cbq_class	link;

E
Eric Dumazet 已提交
151 152
	unsigned int		activemask;
	struct cbq_class	*active[TC_CBQ_MAXPRIO + 1];	/* List of all classes
L
Linus Torvalds 已提交
153 154
								   with backlog */

155
#ifdef CONFIG_NET_CLS_ACT
L
Linus Torvalds 已提交
156 157 158 159 160 161 162
	struct cbq_class	*rx_class;
#endif
	struct cbq_class	*tx_class;
	struct cbq_class	*tx_borrowed;
	int			tx_len;
	psched_time_t		now;		/* Cached timestamp */
	psched_time_t		now_rt;		/* Cached real time */
E
Eric Dumazet 已提交
163
	unsigned int		pmask;
L
Linus Torvalds 已提交
164

165
	struct hrtimer		delay_timer;
166
	struct qdisc_watchdog	watchdog;	/* Watchdog timer,
L
Linus Torvalds 已提交
167 168 169
						   started when CBQ has
						   backlog, but cannot
						   transmit just now */
170
	psched_tdiff_t		wd_expires;
L
Linus Torvalds 已提交
171 172 173 174 175
	int			toplevel;
	u32			hgenerator;
};


E
Eric Dumazet 已提交
176
#define L2T(cl, len)	qdisc_l2t((cl)->R_tab, len)
L
Linus Torvalds 已提交
177

E
Eric Dumazet 已提交
178
static inline struct cbq_class *
L
Linus Torvalds 已提交
179 180
cbq_class_lookup(struct cbq_sched_data *q, u32 classid)
{
181
	struct Qdisc_class_common *clc;
L
Linus Torvalds 已提交
182

183 184 185 186
	clc = qdisc_class_find(&q->clhash, classid);
	if (clc == NULL)
		return NULL;
	return container_of(clc, struct cbq_class, common);
L
Linus Torvalds 已提交
187 188
}

189
#ifdef CONFIG_NET_CLS_ACT
L
Linus Torvalds 已提交
190 191 192 193

static struct cbq_class *
cbq_reclassify(struct sk_buff *skb, struct cbq_class *this)
{
E
Eric Dumazet 已提交
194
	struct cbq_class *cl;
L
Linus Torvalds 已提交
195

E
Eric Dumazet 已提交
196 197
	for (cl = this->tparent; cl; cl = cl->tparent) {
		struct cbq_class *new = cl->defaults[TC_PRIO_BESTEFFORT];
L
Linus Torvalds 已提交
198

E
Eric Dumazet 已提交
199 200 201
		if (new != NULL && new != this)
			return new;
	}
L
Linus Torvalds 已提交
202 203 204 205 206 207
	return NULL;
}

#endif

/* Classify packet. The procedure is pretty complicated, but
E
Eric Dumazet 已提交
208 209 210 211 212 213 214
 * it allows us to combine link sharing and priority scheduling
 * transparently.
 *
 * Namely, you can put link sharing rules (f.e. route based) at root of CBQ,
 * so that it resolves to split nodes. Then packets are classified
 * by logical priority, or a more specific classifier may be attached
 * to the split node.
L
Linus Torvalds 已提交
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
 */

static struct cbq_class *
cbq_classify(struct sk_buff *skb, struct Qdisc *sch, int *qerr)
{
	struct cbq_sched_data *q = qdisc_priv(sch);
	struct cbq_class *head = &q->link;
	struct cbq_class **defmap;
	struct cbq_class *cl = NULL;
	u32 prio = skb->priority;
	struct tcf_result res;

	/*
	 *  Step 1. If skb->priority points to one of our classes, use it.
	 */
E
Eric Dumazet 已提交
230
	if (TC_H_MAJ(prio ^ sch->handle) == 0 &&
L
Linus Torvalds 已提交
231 232 233
	    (cl = cbq_class_lookup(q, prio)) != NULL)
		return cl;

234
	*qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
L
Linus Torvalds 已提交
235 236 237 238 239 240 241
	for (;;) {
		int result = 0;
		defmap = head->defaults;

		/*
		 * Step 2+n. Apply classifier.
		 */
242 243
		if (!head->filter_list ||
		    (result = tc_classify_compat(skb, head->filter_list, &res)) < 0)
L
Linus Torvalds 已提交
244 245
			goto fallback;

E
Eric Dumazet 已提交
246 247
		cl = (void *)res.class;
		if (!cl) {
L
Linus Torvalds 已提交
248 249
			if (TC_H_MAJ(res.classid))
				cl = cbq_class_lookup(q, res.classid);
E
Eric Dumazet 已提交
250
			else if ((cl = defmap[res.classid & TC_PRIO_MAX]) == NULL)
L
Linus Torvalds 已提交
251 252
				cl = defmap[TC_PRIO_BESTEFFORT];

253
			if (cl == NULL)
L
Linus Torvalds 已提交
254 255
				goto fallback;
		}
256 257
		if (cl->level >= head->level)
			goto fallback;
L
Linus Torvalds 已提交
258 259 260
#ifdef CONFIG_NET_CLS_ACT
		switch (result) {
		case TC_ACT_QUEUED:
261
		case TC_ACT_STOLEN:
262
			*qerr = NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
L
Linus Torvalds 已提交
263 264
		case TC_ACT_SHOT:
			return NULL;
265 266
		case TC_ACT_RECLASSIFY:
			return cbq_reclassify(skb, cl);
L
Linus Torvalds 已提交
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
		}
#endif
		if (cl->level == 0)
			return cl;

		/*
		 * Step 3+n. If classifier selected a link sharing class,
		 *	   apply agency specific classifier.
		 *	   Repeat this procdure until we hit a leaf node.
		 */
		head = cl;
	}

fallback:
	cl = head;

	/*
	 * Step 4. No success...
	 */
	if (TC_H_MAJ(prio) == 0 &&
E
Eric Dumazet 已提交
287
	    !(cl = head->defaults[prio & TC_PRIO_MAX]) &&
L
Linus Torvalds 已提交
288 289 290 291 292 293 294
	    !(cl = head->defaults[TC_PRIO_BESTEFFORT]))
		return head;

	return cl;
}

/*
E
Eric Dumazet 已提交
295 296 297
 * A packet has just been enqueued on the empty class.
 * cbq_activate_class adds it to the tail of active class list
 * of its priority band.
L
Linus Torvalds 已提交
298 299
 */

E
Eric Dumazet 已提交
300
static inline void cbq_activate_class(struct cbq_class *cl)
L
Linus Torvalds 已提交
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
{
	struct cbq_sched_data *q = qdisc_priv(cl->qdisc);
	int prio = cl->cpriority;
	struct cbq_class *cl_tail;

	cl_tail = q->active[prio];
	q->active[prio] = cl;

	if (cl_tail != NULL) {
		cl->next_alive = cl_tail->next_alive;
		cl_tail->next_alive = cl;
	} else {
		cl->next_alive = cl;
		q->activemask |= (1<<prio);
	}
}

/*
E
Eric Dumazet 已提交
319 320 321
 * Unlink class from active chain.
 * Note that this same procedure is done directly in cbq_dequeue*
 * during round-robin procedure.
L
Linus Torvalds 已提交
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
 */

static void cbq_deactivate_class(struct cbq_class *this)
{
	struct cbq_sched_data *q = qdisc_priv(this->qdisc);
	int prio = this->cpriority;
	struct cbq_class *cl;
	struct cbq_class *cl_prev = q->active[prio];

	do {
		cl = cl_prev->next_alive;
		if (cl == this) {
			cl_prev->next_alive = cl->next_alive;
			cl->next_alive = NULL;

			if (cl == q->active[prio]) {
				q->active[prio] = cl_prev;
				if (cl == q->active[prio]) {
					q->active[prio] = NULL;
					q->activemask &= ~(1<<prio);
					return;
				}
			}
			return;
		}
	} while ((cl_prev = cl) != q->active[prio]);
}

static void
cbq_mark_toplevel(struct cbq_sched_data *q, struct cbq_class *cl)
{
	int toplevel = q->toplevel;

355
	if (toplevel > cl->level && !(qdisc_is_throttled(cl->q))) {
L
Linus Torvalds 已提交
356 357 358
		psched_time_t now;
		psched_tdiff_t incr;

359
		now = psched_get_time();
P
Patrick McHardy 已提交
360
		incr = now - q->now_rt;
361
		now = q->now + incr;
L
Linus Torvalds 已提交
362 363

		do {
P
Patrick McHardy 已提交
364
			if (cl->undertime < now) {
L
Linus Torvalds 已提交
365 366 367
				q->toplevel = cl->level;
				return;
			}
E
Eric Dumazet 已提交
368
		} while ((cl = cl->borrow) != NULL && toplevel > cl->level);
L
Linus Torvalds 已提交
369 370 371 372 373 374 375
	}
}

static int
cbq_enqueue(struct sk_buff *skb, struct Qdisc *sch)
{
	struct cbq_sched_data *q = qdisc_priv(sch);
376
	int uninitialized_var(ret);
L
Linus Torvalds 已提交
377 378
	struct cbq_class *cl = cbq_classify(skb, sch, &ret);

379
#ifdef CONFIG_NET_CLS_ACT
L
Linus Torvalds 已提交
380 381 382
	q->rx_class = cl;
#endif
	if (cl == NULL) {
383
		if (ret & __NET_XMIT_BYPASS)
L
Linus Torvalds 已提交
384 385 386 387 388
			sch->qstats.drops++;
		kfree_skb(skb);
		return ret;
	}

389
#ifdef CONFIG_NET_CLS_ACT
L
Linus Torvalds 已提交
390 391
	cl->q->__parent = sch;
#endif
392 393
	ret = qdisc_enqueue(skb, cl->q);
	if (ret == NET_XMIT_SUCCESS) {
L
Linus Torvalds 已提交
394 395 396 397 398 399 400
		sch->q.qlen++;
		cbq_mark_toplevel(q, cl);
		if (!cl->next_alive)
			cbq_activate_class(cl);
		return ret;
	}

401 402 403 404 405
	if (net_xmit_drop_count(ret)) {
		sch->qstats.drops++;
		cbq_mark_toplevel(q, cl);
		cl->qstats.drops++;
	}
L
Linus Torvalds 已提交
406 407 408 409 410 411 412 413 414 415
	return ret;
}

/* Overlimit actions */

/* TC_CBQ_OVL_CLASSIC: (default) penalize leaf class by adding offtime */

static void cbq_ovl_classic(struct cbq_class *cl)
{
	struct cbq_sched_data *q = qdisc_priv(cl->qdisc);
P
Patrick McHardy 已提交
416
	psched_tdiff_t delay = cl->undertime - q->now;
L
Linus Torvalds 已提交
417 418 419 420

	if (!cl->delayed) {
		delay += cl->offtime;

421
		/*
E
Eric Dumazet 已提交
422 423 424 425 426
		 * Class goes to sleep, so that it will have no
		 * chance to work avgidle. Let's forgive it 8)
		 *
		 * BTW cbq-2.0 has a crap in this
		 * place, apparently they forgot to shift it by cl->ewma_log.
L
Linus Torvalds 已提交
427 428 429 430 431 432 433
		 */
		if (cl->avgidle < 0)
			delay -= (-cl->avgidle) - ((-cl->avgidle) >> cl->ewma_log);
		if (cl->avgidle < cl->minidle)
			cl->avgidle = cl->minidle;
		if (delay <= 0)
			delay = 1;
434
		cl->undertime = q->now + delay;
L
Linus Torvalds 已提交
435 436 437 438 439 440 441 442

		cl->xstats.overactions++;
		cl->delayed = 1;
	}
	if (q->wd_expires == 0 || q->wd_expires > delay)
		q->wd_expires = delay;

	/* Dirty work! We must schedule wakeups based on
E
Eric Dumazet 已提交
443 444
	 * real available rate, rather than leaf rate,
	 * which may be tiny (even zero).
L
Linus Torvalds 已提交
445 446 447 448 449 450
	 */
	if (q->toplevel == TC_CBQ_MAXLEVEL) {
		struct cbq_class *b;
		psched_tdiff_t base_delay = q->wd_expires;

		for (b = cl->borrow; b; b = b->borrow) {
P
Patrick McHardy 已提交
451
			delay = b->undertime - q->now;
L
Linus Torvalds 已提交
452 453 454 455 456 457 458 459 460 461 462 463
			if (delay < base_delay) {
				if (delay <= 0)
					delay = 1;
				base_delay = delay;
			}
		}

		q->wd_expires = base_delay;
	}
}

/* TC_CBQ_OVL_RCLASSIC: penalize by offtime classes in hierarchy, when
E
Eric Dumazet 已提交
464
 * they go overlimit
L
Linus Torvalds 已提交
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
 */

static void cbq_ovl_rclassic(struct cbq_class *cl)
{
	struct cbq_sched_data *q = qdisc_priv(cl->qdisc);
	struct cbq_class *this = cl;

	do {
		if (cl->level > q->toplevel) {
			cl = NULL;
			break;
		}
	} while ((cl = cl->borrow) != NULL);

	if (cl == NULL)
		cl = this;
	cbq_ovl_classic(cl);
}

/* TC_CBQ_OVL_DELAY: delay until it will go to underlimit */

static void cbq_ovl_delay(struct cbq_class *cl)
{
	struct cbq_sched_data *q = qdisc_priv(cl->qdisc);
P
Patrick McHardy 已提交
489
	psched_tdiff_t delay = cl->undertime - q->now;
L
Linus Torvalds 已提交
490

491 492 493 494
	if (test_bit(__QDISC_STATE_DEACTIVATED,
		     &qdisc_root_sleeping(cl->qdisc)->state))
		return;

L
Linus Torvalds 已提交
495
	if (!cl->delayed) {
496 497
		psched_time_t sched = q->now;
		ktime_t expires;
L
Linus Torvalds 已提交
498 499 500 501 502 503

		delay += cl->offtime;
		if (cl->avgidle < 0)
			delay -= (-cl->avgidle) - ((-cl->avgidle) >> cl->ewma_log);
		if (cl->avgidle < cl->minidle)
			cl->avgidle = cl->minidle;
504
		cl->undertime = q->now + delay;
L
Linus Torvalds 已提交
505 506

		if (delay > 0) {
507
			sched += delay + cl->penalty;
L
Linus Torvalds 已提交
508 509 510
			cl->penalized = sched;
			cl->cpriority = TC_CBQ_MAXPRIO;
			q->pmask |= (1<<TC_CBQ_MAXPRIO);
511

E
Eric Dumazet 已提交
512
			expires = ns_to_ktime(PSCHED_TICKS2NS(sched));
513 514 515 516 517 518
			if (hrtimer_try_to_cancel(&q->delay_timer) &&
			    ktime_to_ns(ktime_sub(
					hrtimer_get_expires(&q->delay_timer),
					expires)) > 0)
				hrtimer_set_expires(&q->delay_timer, expires);
			hrtimer_restart(&q->delay_timer);
L
Linus Torvalds 已提交
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
			cl->delayed = 1;
			cl->xstats.overactions++;
			return;
		}
		delay = 1;
	}
	if (q->wd_expires == 0 || q->wd_expires > delay)
		q->wd_expires = delay;
}

/* TC_CBQ_OVL_LOWPRIO: penalize class by lowering its priority band */

static void cbq_ovl_lowprio(struct cbq_class *cl)
{
	struct cbq_sched_data *q = qdisc_priv(cl->qdisc);

535
	cl->penalized = q->now + cl->penalty;
L
Linus Torvalds 已提交
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555

	if (cl->cpriority != cl->priority2) {
		cl->cpriority = cl->priority2;
		q->pmask |= (1<<cl->cpriority);
		cl->xstats.overactions++;
	}
	cbq_ovl_classic(cl);
}

/* TC_CBQ_OVL_DROP: penalize class by dropping */

static void cbq_ovl_drop(struct cbq_class *cl)
{
	if (cl->q->ops->drop)
		if (cl->q->ops->drop(cl->q))
			cl->qdisc->q.qlen--;
	cl->xstats.overactions++;
	cbq_ovl_classic(cl);
}

556 557
static psched_tdiff_t cbq_undelay_prio(struct cbq_sched_data *q, int prio,
				       psched_time_t now)
L
Linus Torvalds 已提交
558 559 560
{
	struct cbq_class *cl;
	struct cbq_class *cl_prev = q->active[prio];
561
	psched_time_t sched = now;
L
Linus Torvalds 已提交
562 563

	if (cl_prev == NULL)
564
		return 0;
L
Linus Torvalds 已提交
565 566 567

	do {
		cl = cl_prev->next_alive;
568
		if (now - cl->penalized > 0) {
L
Linus Torvalds 已提交
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
			cl_prev->next_alive = cl->next_alive;
			cl->next_alive = NULL;
			cl->cpriority = cl->priority;
			cl->delayed = 0;
			cbq_activate_class(cl);

			if (cl == q->active[prio]) {
				q->active[prio] = cl_prev;
				if (cl == q->active[prio]) {
					q->active[prio] = NULL;
					return 0;
				}
			}

			cl = cl_prev->next_alive;
584
		} else if (sched - cl->penalized > 0)
L
Linus Torvalds 已提交
585 586 587
			sched = cl->penalized;
	} while ((cl_prev = cl) != q->active[prio]);

588
	return sched - now;
L
Linus Torvalds 已提交
589 590
}

591
static enum hrtimer_restart cbq_undelay(struct hrtimer *timer)
L
Linus Torvalds 已提交
592
{
593
	struct cbq_sched_data *q = container_of(timer, struct cbq_sched_data,
594
						delay_timer);
595 596 597
	struct Qdisc *sch = q->watchdog.qdisc;
	psched_time_t now;
	psched_tdiff_t delay = 0;
E
Eric Dumazet 已提交
598
	unsigned int pmask;
L
Linus Torvalds 已提交
599

600
	now = psched_get_time();
601

L
Linus Torvalds 已提交
602 603 604 605 606
	pmask = q->pmask;
	q->pmask = 0;

	while (pmask) {
		int prio = ffz(~pmask);
607
		psched_tdiff_t tmp;
L
Linus Torvalds 已提交
608 609 610

		pmask &= ~(1<<prio);

611
		tmp = cbq_undelay_prio(q, prio, now);
L
Linus Torvalds 已提交
612 613 614 615 616 617 618 619
		if (tmp > 0) {
			q->pmask |= 1<<prio;
			if (tmp < delay || delay == 0)
				delay = tmp;
		}
	}

	if (delay) {
620 621 622
		ktime_t time;

		time = ktime_set(0, 0);
623
		time = ktime_add_ns(time, PSCHED_TICKS2NS(now + delay));
624
		hrtimer_start(&q->delay_timer, time, HRTIMER_MODE_ABS);
L
Linus Torvalds 已提交
625 626
	}

627
	qdisc_unthrottled(sch);
628
	__netif_schedule(qdisc_root(sch));
629
	return HRTIMER_NORESTART;
L
Linus Torvalds 已提交
630 631
}

632
#ifdef CONFIG_NET_CLS_ACT
L
Linus Torvalds 已提交
633 634 635 636 637 638 639 640 641
static int cbq_reshape_fail(struct sk_buff *skb, struct Qdisc *child)
{
	struct Qdisc *sch = child->__parent;
	struct cbq_sched_data *q = qdisc_priv(sch);
	struct cbq_class *cl = q->rx_class;

	q->rx_class = NULL;

	if (cl && (cl = cbq_reclassify(skb, cl)) != NULL) {
642
		int ret;
L
Linus Torvalds 已提交
643 644 645 646 647 648

		cbq_mark_toplevel(q, cl);

		q->rx_class = cl;
		cl->q->__parent = sch;

649 650
		ret = qdisc_enqueue(skb, cl->q);
		if (ret == NET_XMIT_SUCCESS) {
L
Linus Torvalds 已提交
651 652 653 654 655
			sch->q.qlen++;
			if (!cl->next_alive)
				cbq_activate_class(cl);
			return 0;
		}
656 657
		if (net_xmit_drop_count(ret))
			sch->qstats.drops++;
L
Linus Torvalds 已提交
658 659 660 661 662 663 664 665
		return 0;
	}

	sch->qstats.drops++;
	return -1;
}
#endif

666
/*
E
Eric Dumazet 已提交
667 668 669 670 671 672 673
 * It is mission critical procedure.
 *
 * We "regenerate" toplevel cutoff, if transmitting class
 * has backlog and it is not regulated. It is not part of
 * original CBQ description, but looks more reasonable.
 * Probably, it is wrong. This question needs further investigation.
 */
L
Linus Torvalds 已提交
674

E
Eric Dumazet 已提交
675
static inline void
L
Linus Torvalds 已提交
676 677 678 679 680 681
cbq_update_toplevel(struct cbq_sched_data *q, struct cbq_class *cl,
		    struct cbq_class *borrowed)
{
	if (cl && q->toplevel >= borrowed->level) {
		if (cl->q->q.qlen > 1) {
			do {
682
				if (borrowed->undertime == PSCHED_PASTPERFECT) {
L
Linus Torvalds 已提交
683 684 685
					q->toplevel = borrowed->level;
					return;
				}
E
Eric Dumazet 已提交
686
			} while ((borrowed = borrowed->borrow) != NULL);
L
Linus Torvalds 已提交
687
		}
688
#if 0
L
Linus Torvalds 已提交
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
	/* It is not necessary now. Uncommenting it
	   will save CPU cycles, but decrease fairness.
	 */
		q->toplevel = TC_CBQ_MAXLEVEL;
#endif
	}
}

static void
cbq_update(struct cbq_sched_data *q)
{
	struct cbq_class *this = q->tx_class;
	struct cbq_class *cl = this;
	int len = q->tx_len;

	q->tx_class = NULL;

	for ( ; cl; cl = cl->share) {
		long avgidle = cl->avgidle;
		long idle;

		cl->bstats.packets++;
		cl->bstats.bytes += len;

		/*
E
Eric Dumazet 已提交
714 715 716 717
		 * (now - last) is total time between packet right edges.
		 * (last_pktlen/rate) is "virtual" busy time, so that
		 *
		 *	idle = (now - last) - last_pktlen/rate
L
Linus Torvalds 已提交
718 719
		 */

P
Patrick McHardy 已提交
720
		idle = q->now - cl->last;
L
Linus Torvalds 已提交
721 722 723 724 725 726
		if ((unsigned long)idle > 128*1024*1024) {
			avgidle = cl->maxidle;
		} else {
			idle -= L2T(cl, len);

		/* true_avgidle := (1-W)*true_avgidle + W*idle,
E
Eric Dumazet 已提交
727 728 729
		 * where W=2^{-ewma_log}. But cl->avgidle is scaled:
		 * cl->avgidle == true_avgidle/W,
		 * hence:
L
Linus Torvalds 已提交
730 731 732 733 734 735 736 737 738 739 740 741 742
		 */
			avgidle += idle - (avgidle>>cl->ewma_log);
		}

		if (avgidle <= 0) {
			/* Overlimit or at-limit */

			if (avgidle < cl->minidle)
				avgidle = cl->minidle;

			cl->avgidle = avgidle;

			/* Calculate expected time, when this class
E
Eric Dumazet 已提交
743 744 745 746 747 748
			 * will be allowed to send.
			 * It will occur, when:
			 * (1-W)*true_avgidle + W*delay = 0, i.e.
			 * idle = (1/W - 1)*(-true_avgidle)
			 * or
			 * idle = (1 - W)*(-cl->avgidle);
L
Linus Torvalds 已提交
749 750 751 752
			 */
			idle = (-avgidle) - ((-avgidle) >> cl->ewma_log);

			/*
E
Eric Dumazet 已提交
753 754 755 756 757 758
			 * That is not all.
			 * To maintain the rate allocated to the class,
			 * we add to undertime virtual clock,
			 * necessary to complete transmitted packet.
			 * (len/phys_bandwidth has been already passed
			 * to the moment of cbq_update)
L
Linus Torvalds 已提交
759 760 761 762 763
			 */

			idle -= L2T(&q->link, len);
			idle += L2T(cl, len);

764
			cl->undertime = q->now + idle;
L
Linus Torvalds 已提交
765 766 767
		} else {
			/* Underlimit */

768
			cl->undertime = PSCHED_PASTPERFECT;
L
Linus Torvalds 已提交
769 770 771 772 773 774 775 776 777 778 779
			if (avgidle > cl->maxidle)
				cl->avgidle = cl->maxidle;
			else
				cl->avgidle = avgidle;
		}
		cl->last = q->now;
	}

	cbq_update_toplevel(q, this, q->tx_borrowed);
}

E
Eric Dumazet 已提交
780
static inline struct cbq_class *
L
Linus Torvalds 已提交
781 782 783 784 785 786 787 788
cbq_under_limit(struct cbq_class *cl)
{
	struct cbq_sched_data *q = qdisc_priv(cl->qdisc);
	struct cbq_class *this_cl = cl;

	if (cl->tparent == NULL)
		return cl;

789
	if (cl->undertime == PSCHED_PASTPERFECT || q->now >= cl->undertime) {
L
Linus Torvalds 已提交
790 791 792 793 794 795
		cl->delayed = 0;
		return cl;
	}

	do {
		/* It is very suspicious place. Now overlimit
E
Eric Dumazet 已提交
796 797 798 799 800 801 802 803
		 * action is generated for not bounded classes
		 * only if link is completely congested.
		 * Though it is in agree with ancestor-only paradigm,
		 * it looks very stupid. Particularly,
		 * it means that this chunk of code will either
		 * never be called or result in strong amplification
		 * of burstiness. Dangerous, silly, and, however,
		 * no another solution exists.
L
Linus Torvalds 已提交
804
		 */
E
Eric Dumazet 已提交
805 806
		cl = cl->borrow;
		if (!cl) {
L
Linus Torvalds 已提交
807 808 809 810 811 812
			this_cl->qstats.overlimits++;
			this_cl->overlimit(this_cl);
			return NULL;
		}
		if (cl->level > q->toplevel)
			return NULL;
813
	} while (cl->undertime != PSCHED_PASTPERFECT && q->now < cl->undertime);
L
Linus Torvalds 已提交
814 815 816 817 818

	cl->delayed = 0;
	return cl;
}

E
Eric Dumazet 已提交
819
static inline struct sk_buff *
L
Linus Torvalds 已提交
820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
cbq_dequeue_prio(struct Qdisc *sch, int prio)
{
	struct cbq_sched_data *q = qdisc_priv(sch);
	struct cbq_class *cl_tail, *cl_prev, *cl;
	struct sk_buff *skb;
	int deficit;

	cl_tail = cl_prev = q->active[prio];
	cl = cl_prev->next_alive;

	do {
		deficit = 0;

		/* Start round */
		do {
			struct cbq_class *borrow = cl;

			if (cl->q->q.qlen &&
			    (borrow = cbq_under_limit(cl)) == NULL)
				goto skip_class;

			if (cl->deficit <= 0) {
				/* Class exhausted its allotment per
E
Eric Dumazet 已提交
843
				 * this round. Switch to the next one.
L
Linus Torvalds 已提交
844 845 846 847 848 849 850 851 852
				 */
				deficit = 1;
				cl->deficit += cl->quantum;
				goto next_class;
			}

			skb = cl->q->dequeue(cl->q);

			/* Class did not give us any skb :-(
E
Eric Dumazet 已提交
853 854
			 * It could occur even if cl->q->q.qlen != 0
			 * f.e. if cl->q == "tbf"
L
Linus Torvalds 已提交
855 856 857 858
			 */
			if (skb == NULL)
				goto skip_class;

859
			cl->deficit -= qdisc_pkt_len(skb);
L
Linus Torvalds 已提交
860 861 862 863 864 865 866
			q->tx_class = cl;
			q->tx_borrowed = borrow;
			if (borrow != cl) {
#ifndef CBQ_XSTATS_BORROWS_BYTES
				borrow->xstats.borrows++;
				cl->xstats.borrows++;
#else
867 868
				borrow->xstats.borrows += qdisc_pkt_len(skb);
				cl->xstats.borrows += qdisc_pkt_len(skb);
L
Linus Torvalds 已提交
869 870
#endif
			}
871
			q->tx_len = qdisc_pkt_len(skb);
L
Linus Torvalds 已提交
872 873 874 875 876 877 878 879 880 881 882

			if (cl->deficit <= 0) {
				q->active[prio] = cl;
				cl = cl->next_alive;
				cl->deficit += cl->quantum;
			}
			return skb;

skip_class:
			if (cl->q->q.qlen == 0 || prio != cl->cpriority) {
				/* Class is empty or penalized.
E
Eric Dumazet 已提交
883
				 * Unlink it from active chain.
L
Linus Torvalds 已提交
884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921
				 */
				cl_prev->next_alive = cl->next_alive;
				cl->next_alive = NULL;

				/* Did cl_tail point to it? */
				if (cl == cl_tail) {
					/* Repair it! */
					cl_tail = cl_prev;

					/* Was it the last class in this band? */
					if (cl == cl_tail) {
						/* Kill the band! */
						q->active[prio] = NULL;
						q->activemask &= ~(1<<prio);
						if (cl->q->q.qlen)
							cbq_activate_class(cl);
						return NULL;
					}

					q->active[prio] = cl_tail;
				}
				if (cl->q->q.qlen)
					cbq_activate_class(cl);

				cl = cl_prev;
			}

next_class:
			cl_prev = cl;
			cl = cl->next_alive;
		} while (cl_prev != cl_tail);
	} while (deficit);

	q->active[prio] = cl_prev;

	return NULL;
}

E
Eric Dumazet 已提交
922
static inline struct sk_buff *
L
Linus Torvalds 已提交
923 924 925 926
cbq_dequeue_1(struct Qdisc *sch)
{
	struct cbq_sched_data *q = qdisc_priv(sch);
	struct sk_buff *skb;
E
Eric Dumazet 已提交
927
	unsigned int activemask;
L
Linus Torvalds 已提交
928

E
Eric Dumazet 已提交
929
	activemask = q->activemask & 0xFF;
L
Linus Torvalds 已提交
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947
	while (activemask) {
		int prio = ffz(~activemask);
		activemask &= ~(1<<prio);
		skb = cbq_dequeue_prio(sch, prio);
		if (skb)
			return skb;
	}
	return NULL;
}

static struct sk_buff *
cbq_dequeue(struct Qdisc *sch)
{
	struct sk_buff *skb;
	struct cbq_sched_data *q = qdisc_priv(sch);
	psched_time_t now;
	psched_tdiff_t incr;

948
	now = psched_get_time();
P
Patrick McHardy 已提交
949
	incr = now - q->now_rt;
L
Linus Torvalds 已提交
950 951 952 953

	if (q->tx_class) {
		psched_tdiff_t incr2;
		/* Time integrator. We calculate EOS time
E
Eric Dumazet 已提交
954 955 956 957 958
		 * by adding expected packet transmission time.
		 * If real time is greater, we warp artificial clock,
		 * so that:
		 *
		 * cbq_time = max(real_time, work);
L
Linus Torvalds 已提交
959 960
		 */
		incr2 = L2T(&q->link, q->tx_len);
961
		q->now += incr2;
L
Linus Torvalds 已提交
962 963 964
		cbq_update(q);
		if ((incr -= incr2) < 0)
			incr = 0;
965 966 967 968
		q->now += incr;
	} else {
		if (now > q->now)
			q->now = now;
L
Linus Torvalds 已提交
969 970 971 972 973 974 975 976
	}
	q->now_rt = now;

	for (;;) {
		q->wd_expires = 0;

		skb = cbq_dequeue_1(sch);
		if (skb) {
977
			qdisc_bstats_update(sch, skb);
L
Linus Torvalds 已提交
978
			sch->q.qlen--;
979
			qdisc_unthrottled(sch);
L
Linus Torvalds 已提交
980 981 982 983
			return skb;
		}

		/* All the classes are overlimit.
E
Eric Dumazet 已提交
984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999
		 *
		 * It is possible, if:
		 *
		 * 1. Scheduler is empty.
		 * 2. Toplevel cutoff inhibited borrowing.
		 * 3. Root class is overlimit.
		 *
		 * Reset 2d and 3d conditions and retry.
		 *
		 * Note, that NS and cbq-2.0 are buggy, peeking
		 * an arbitrary class is appropriate for ancestor-only
		 * sharing, but not for toplevel algorithm.
		 *
		 * Our version is better, but slower, because it requires
		 * two passes, but it is unavoidable with top-level sharing.
		 */
L
Linus Torvalds 已提交
1000 1001

		if (q->toplevel == TC_CBQ_MAXLEVEL &&
1002
		    q->link.undertime == PSCHED_PASTPERFECT)
L
Linus Torvalds 已提交
1003 1004 1005
			break;

		q->toplevel = TC_CBQ_MAXLEVEL;
1006
		q->link.undertime = PSCHED_PASTPERFECT;
L
Linus Torvalds 已提交
1007 1008 1009
	}

	/* No packets in scheduler or nobody wants to give them to us :-(
E
Eric Dumazet 已提交
1010 1011
	 * Sigh... start watchdog timer in the last case.
	 */
L
Linus Torvalds 已提交
1012 1013 1014

	if (sch->q.qlen) {
		sch->qstats.overlimits++;
1015 1016
		if (q->wd_expires)
			qdisc_watchdog_schedule(&q->watchdog,
1017
						now + q->wd_expires);
L
Linus Torvalds 已提交
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
	}
	return NULL;
}

/* CBQ class maintanance routines */

static void cbq_adjust_levels(struct cbq_class *this)
{
	if (this == NULL)
		return;

	do {
		int level = 0;
		struct cbq_class *cl;

E
Eric Dumazet 已提交
1033 1034
		cl = this->children;
		if (cl) {
L
Linus Torvalds 已提交
1035 1036 1037 1038 1039
			do {
				if (cl->level > level)
					level = cl->level;
			} while ((cl = cl->sibling) != this->children);
		}
E
Eric Dumazet 已提交
1040
		this->level = level + 1;
L
Linus Torvalds 已提交
1041 1042 1043 1044 1045 1046
	} while ((this = this->tparent) != NULL);
}

static void cbq_normalize_quanta(struct cbq_sched_data *q, int prio)
{
	struct cbq_class *cl;
1047
	unsigned int h;
L
Linus Torvalds 已提交
1048 1049 1050 1051

	if (q->quanta[prio] == 0)
		return;

1052
	for (h = 0; h < q->clhash.hashsize; h++) {
1053
		hlist_for_each_entry(cl, &q->clhash.hash[h], common.hnode) {
L
Linus Torvalds 已提交
1054
			/* BUGGGG... Beware! This expression suffer of
E
Eric Dumazet 已提交
1055
			 * arithmetic overflows!
L
Linus Torvalds 已提交
1056 1057 1058 1059 1060
			 */
			if (cl->priority == prio) {
				cl->quantum = (cl->weight*cl->allot*q->nclasses[prio])/
					q->quanta[prio];
			}
1061
			if (cl->quantum <= 0 || cl->quantum>32*qdisc_dev(cl->qdisc)->mtu) {
E
Eric Dumazet 已提交
1062 1063
				pr_warning("CBQ: class %08x has bad quantum==%ld, repaired.\n",
					   cl->common.classid, cl->quantum);
1064
				cl->quantum = qdisc_dev(cl->qdisc)->mtu/2 + 1;
L
Linus Torvalds 已提交
1065 1066 1067 1068 1069 1070 1071 1072 1073
			}
		}
	}
}

static void cbq_sync_defmap(struct cbq_class *cl)
{
	struct cbq_sched_data *q = qdisc_priv(cl->qdisc);
	struct cbq_class *split = cl->split;
E
Eric Dumazet 已提交
1074
	unsigned int h;
L
Linus Torvalds 已提交
1075 1076 1077 1078 1079
	int i;

	if (split == NULL)
		return;

E
Eric Dumazet 已提交
1080 1081
	for (i = 0; i <= TC_PRIO_MAX; i++) {
		if (split->defaults[i] == cl && !(cl->defmap & (1<<i)))
L
Linus Torvalds 已提交
1082 1083 1084
			split->defaults[i] = NULL;
	}

E
Eric Dumazet 已提交
1085
	for (i = 0; i <= TC_PRIO_MAX; i++) {
L
Linus Torvalds 已提交
1086 1087 1088 1089 1090
		int level = split->level;

		if (split->defaults[i])
			continue;

1091
		for (h = 0; h < q->clhash.hashsize; h++) {
L
Linus Torvalds 已提交
1092 1093
			struct cbq_class *c;

1094
			hlist_for_each_entry(c, &q->clhash.hash[h],
1095
					     common.hnode) {
L
Linus Torvalds 已提交
1096
				if (c->split == split && c->level < level &&
E
Eric Dumazet 已提交
1097
				    c->defmap & (1<<i)) {
L
Linus Torvalds 已提交
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
					split->defaults[i] = c;
					level = c->level;
				}
			}
		}
	}
}

static void cbq_change_defmap(struct cbq_class *cl, u32 splitid, u32 def, u32 mask)
{
	struct cbq_class *split = NULL;

	if (splitid == 0) {
E
Eric Dumazet 已提交
1111 1112
		split = cl->split;
		if (!split)
L
Linus Torvalds 已提交
1113
			return;
1114
		splitid = split->common.classid;
L
Linus Torvalds 已提交
1115 1116
	}

1117
	if (split == NULL || split->common.classid != splitid) {
L
Linus Torvalds 已提交
1118
		for (split = cl->tparent; split; split = split->tparent)
1119
			if (split->common.classid == splitid)
L
Linus Torvalds 已提交
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
				break;
	}

	if (split == NULL)
		return;

	if (cl->split != split) {
		cl->defmap = 0;
		cbq_sync_defmap(cl);
		cl->split = split;
E
Eric Dumazet 已提交
1130
		cl->defmap = def & mask;
L
Linus Torvalds 已提交
1131
	} else
E
Eric Dumazet 已提交
1132
		cl->defmap = (cl->defmap & ~mask) | (def & mask);
L
Linus Torvalds 已提交
1133 1134 1135 1136 1137 1138 1139 1140 1141

	cbq_sync_defmap(cl);
}

static void cbq_unlink_class(struct cbq_class *this)
{
	struct cbq_class *cl, **clp;
	struct cbq_sched_data *q = qdisc_priv(this->qdisc);

1142
	qdisc_class_hash_remove(&q->clhash, &this->common);
L
Linus Torvalds 已提交
1143 1144

	if (this->tparent) {
E
Eric Dumazet 已提交
1145
		clp = &this->sibling;
L
Linus Torvalds 已提交
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
		cl = *clp;
		do {
			if (cl == this) {
				*clp = cl->sibling;
				break;
			}
			clp = &cl->sibling;
		} while ((cl = *clp) != this->sibling);

		if (this->tparent->children == this) {
			this->tparent->children = this->sibling;
			if (this->sibling == this)
				this->tparent->children = NULL;
		}
	} else {
1161
		WARN_ON(this->sibling != this);
L
Linus Torvalds 已提交
1162 1163 1164 1165 1166 1167 1168 1169 1170
	}
}

static void cbq_link_class(struct cbq_class *this)
{
	struct cbq_sched_data *q = qdisc_priv(this->qdisc);
	struct cbq_class *parent = this->tparent;

	this->sibling = this;
1171
	qdisc_class_hash_insert(&q->clhash, &this->common);
L
Linus Torvalds 已提交
1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183

	if (parent == NULL)
		return;

	if (parent->children == NULL) {
		parent->children = this;
	} else {
		this->sibling = parent->children->sibling;
		parent->children->sibling = this;
	}
}

E
Eric Dumazet 已提交
1184
static unsigned int cbq_drop(struct Qdisc *sch)
L
Linus Torvalds 已提交
1185 1186 1187 1188 1189 1190 1191
{
	struct cbq_sched_data *q = qdisc_priv(sch);
	struct cbq_class *cl, *cl_head;
	int prio;
	unsigned int len;

	for (prio = TC_CBQ_MAXPRIO; prio >= 0; prio--) {
E
Eric Dumazet 已提交
1192 1193
		cl_head = q->active[prio];
		if (!cl_head)
L
Linus Torvalds 已提交
1194 1195 1196 1197 1198 1199
			continue;

		cl = cl_head;
		do {
			if (cl->q->ops->drop && (len = cl->q->ops->drop(cl->q))) {
				sch->q.qlen--;
1200 1201
				if (!cl->q->q.qlen)
					cbq_deactivate_class(cl);
L
Linus Torvalds 已提交
1202 1203 1204 1205 1206 1207 1208 1209
				return len;
			}
		} while ((cl = cl->next_alive) != cl_head);
	}
	return 0;
}

static void
E
Eric Dumazet 已提交
1210
cbq_reset(struct Qdisc *sch)
L
Linus Torvalds 已提交
1211 1212 1213 1214
{
	struct cbq_sched_data *q = qdisc_priv(sch);
	struct cbq_class *cl;
	int prio;
E
Eric Dumazet 已提交
1215
	unsigned int h;
L
Linus Torvalds 已提交
1216 1217 1218 1219 1220

	q->activemask = 0;
	q->pmask = 0;
	q->tx_class = NULL;
	q->tx_borrowed = NULL;
1221
	qdisc_watchdog_cancel(&q->watchdog);
1222
	hrtimer_cancel(&q->delay_timer);
L
Linus Torvalds 已提交
1223
	q->toplevel = TC_CBQ_MAXLEVEL;
1224
	q->now = psched_get_time();
L
Linus Torvalds 已提交
1225 1226 1227 1228 1229
	q->now_rt = q->now;

	for (prio = 0; prio <= TC_CBQ_MAXPRIO; prio++)
		q->active[prio] = NULL;

1230
	for (h = 0; h < q->clhash.hashsize; h++) {
1231
		hlist_for_each_entry(cl, &q->clhash.hash[h], common.hnode) {
L
Linus Torvalds 已提交
1232 1233 1234
			qdisc_reset(cl->q);

			cl->next_alive = NULL;
1235
			cl->undertime = PSCHED_PASTPERFECT;
L
Linus Torvalds 已提交
1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246
			cl->avgidle = cl->maxidle;
			cl->deficit = cl->quantum;
			cl->cpriority = cl->priority;
		}
	}
	sch->q.qlen = 0;
}


static int cbq_set_lss(struct cbq_class *cl, struct tc_cbq_lssopt *lss)
{
E
Eric Dumazet 已提交
1247 1248 1249
	if (lss->change & TCF_CBQ_LSS_FLAGS) {
		cl->share = (lss->flags & TCF_CBQ_LSS_ISOLATED) ? NULL : cl->tparent;
		cl->borrow = (lss->flags & TCF_CBQ_LSS_BOUNDED) ? NULL : cl->tparent;
L
Linus Torvalds 已提交
1250
	}
E
Eric Dumazet 已提交
1251
	if (lss->change & TCF_CBQ_LSS_EWMA)
L
Linus Torvalds 已提交
1252
		cl->ewma_log = lss->ewma_log;
E
Eric Dumazet 已提交
1253
	if (lss->change & TCF_CBQ_LSS_AVPKT)
L
Linus Torvalds 已提交
1254
		cl->avpkt = lss->avpkt;
E
Eric Dumazet 已提交
1255
	if (lss->change & TCF_CBQ_LSS_MINIDLE)
L
Linus Torvalds 已提交
1256
		cl->minidle = -(long)lss->minidle;
E
Eric Dumazet 已提交
1257
	if (lss->change & TCF_CBQ_LSS_MAXIDLE) {
L
Linus Torvalds 已提交
1258 1259 1260
		cl->maxidle = lss->maxidle;
		cl->avgidle = lss->maxidle;
	}
E
Eric Dumazet 已提交
1261
	if (lss->change & TCF_CBQ_LSS_OFFTIME)
L
Linus Torvalds 已提交
1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288
		cl->offtime = lss->offtime;
	return 0;
}

static void cbq_rmprio(struct cbq_sched_data *q, struct cbq_class *cl)
{
	q->nclasses[cl->priority]--;
	q->quanta[cl->priority] -= cl->weight;
	cbq_normalize_quanta(q, cl->priority);
}

static void cbq_addprio(struct cbq_sched_data *q, struct cbq_class *cl)
{
	q->nclasses[cl->priority]++;
	q->quanta[cl->priority] += cl->weight;
	cbq_normalize_quanta(q, cl->priority);
}

static int cbq_set_wrr(struct cbq_class *cl, struct tc_cbq_wrropt *wrr)
{
	struct cbq_sched_data *q = qdisc_priv(cl->qdisc);

	if (wrr->allot)
		cl->allot = wrr->allot;
	if (wrr->weight)
		cl->weight = wrr->weight;
	if (wrr->priority) {
E
Eric Dumazet 已提交
1289
		cl->priority = wrr->priority - 1;
L
Linus Torvalds 已提交
1290 1291
		cl->cpriority = cl->priority;
		if (cl->priority >= cl->priority2)
E
Eric Dumazet 已提交
1292
			cl->priority2 = TC_CBQ_MAXPRIO - 1;
L
Linus Torvalds 已提交
1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308
	}

	cbq_addprio(q, cl);
	return 0;
}

static int cbq_set_overlimit(struct cbq_class *cl, struct tc_cbq_ovl *ovl)
{
	switch (ovl->strategy) {
	case TC_CBQ_OVL_CLASSIC:
		cl->overlimit = cbq_ovl_classic;
		break;
	case TC_CBQ_OVL_DELAY:
		cl->overlimit = cbq_ovl_delay;
		break;
	case TC_CBQ_OVL_LOWPRIO:
E
Eric Dumazet 已提交
1309 1310
		if (ovl->priority2 - 1 >= TC_CBQ_MAXPRIO ||
		    ovl->priority2 - 1 <= cl->priority)
L
Linus Torvalds 已提交
1311
			return -EINVAL;
E
Eric Dumazet 已提交
1312
		cl->priority2 = ovl->priority2 - 1;
L
Linus Torvalds 已提交
1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323
		cl->overlimit = cbq_ovl_lowprio;
		break;
	case TC_CBQ_OVL_DROP:
		cl->overlimit = cbq_ovl_drop;
		break;
	case TC_CBQ_OVL_RCLASSIC:
		cl->overlimit = cbq_ovl_rclassic;
		break;
	default:
		return -EINVAL;
	}
1324
	cl->penalty = ovl->penalty;
L
Linus Torvalds 已提交
1325 1326 1327
	return 0;
}

1328
#ifdef CONFIG_NET_CLS_ACT
L
Linus Torvalds 已提交
1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348
static int cbq_set_police(struct cbq_class *cl, struct tc_cbq_police *p)
{
	cl->police = p->police;

	if (cl->q->handle) {
		if (p->police == TC_POLICE_RECLASSIFY)
			cl->q->reshape_fail = cbq_reshape_fail;
		else
			cl->q->reshape_fail = NULL;
	}
	return 0;
}
#endif

static int cbq_set_fopt(struct cbq_class *cl, struct tc_cbq_fopt *fopt)
{
	cbq_change_defmap(cl, fopt->split, fopt->defmap, fopt->defchange);
	return 0;
}

1349 1350 1351 1352 1353 1354 1355 1356 1357 1358
static const struct nla_policy cbq_policy[TCA_CBQ_MAX + 1] = {
	[TCA_CBQ_LSSOPT]	= { .len = sizeof(struct tc_cbq_lssopt) },
	[TCA_CBQ_WRROPT]	= { .len = sizeof(struct tc_cbq_wrropt) },
	[TCA_CBQ_FOPT]		= { .len = sizeof(struct tc_cbq_fopt) },
	[TCA_CBQ_OVL_STRATEGY]	= { .len = sizeof(struct tc_cbq_ovl) },
	[TCA_CBQ_RATE]		= { .len = sizeof(struct tc_ratespec) },
	[TCA_CBQ_RTAB]		= { .type = NLA_BINARY, .len = TC_RTAB_SIZE },
	[TCA_CBQ_POLICE]	= { .len = sizeof(struct tc_cbq_police) },
};

1359
static int cbq_init(struct Qdisc *sch, struct nlattr *opt)
L
Linus Torvalds 已提交
1360 1361
{
	struct cbq_sched_data *q = qdisc_priv(sch);
1362
	struct nlattr *tb[TCA_CBQ_MAX + 1];
L
Linus Torvalds 已提交
1363
	struct tc_ratespec *r;
1364 1365
	int err;

1366
	err = nla_parse_nested(tb, TCA_CBQ_MAX, opt, cbq_policy);
1367 1368
	if (err < 0)
		return err;
L
Linus Torvalds 已提交
1369

1370
	if (tb[TCA_CBQ_RTAB] == NULL || tb[TCA_CBQ_RATE] == NULL)
L
Linus Torvalds 已提交
1371 1372
		return -EINVAL;

1373
	r = nla_data(tb[TCA_CBQ_RATE]);
L
Linus Torvalds 已提交
1374

1375
	if ((q->link.R_tab = qdisc_get_rtab(r, tb[TCA_CBQ_RTAB])) == NULL)
L
Linus Torvalds 已提交
1376 1377
		return -EINVAL;

1378 1379 1380 1381
	err = qdisc_class_hash_init(&q->clhash);
	if (err < 0)
		goto put_rtab;

L
Linus Torvalds 已提交
1382 1383
	q->link.refcnt = 1;
	q->link.sibling = &q->link;
1384
	q->link.common.classid = sch->handle;
L
Linus Torvalds 已提交
1385
	q->link.qdisc = sch;
1386 1387 1388
	q->link.q = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
				      sch->handle);
	if (!q->link.q)
L
Linus Torvalds 已提交
1389 1390
		q->link.q = &noop_qdisc;

E
Eric Dumazet 已提交
1391 1392 1393
	q->link.priority = TC_CBQ_MAXPRIO - 1;
	q->link.priority2 = TC_CBQ_MAXPRIO - 1;
	q->link.cpriority = TC_CBQ_MAXPRIO - 1;
L
Linus Torvalds 已提交
1394 1395
	q->link.ovl_strategy = TC_CBQ_OVL_CLASSIC;
	q->link.overlimit = cbq_ovl_classic;
1396
	q->link.allot = psched_mtu(qdisc_dev(sch));
L
Linus Torvalds 已提交
1397 1398 1399 1400 1401 1402 1403
	q->link.quantum = q->link.allot;
	q->link.weight = q->link.R_tab->rate.rate;

	q->link.ewma_log = TC_CBQ_DEF_EWMA;
	q->link.avpkt = q->link.allot/2;
	q->link.minidle = -0x7FFFFFFF;

1404
	qdisc_watchdog_init(&q->watchdog, sch);
1405
	hrtimer_init(&q->delay_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
L
Linus Torvalds 已提交
1406 1407
	q->delay_timer.function = cbq_undelay;
	q->toplevel = TC_CBQ_MAXLEVEL;
1408
	q->now = psched_get_time();
L
Linus Torvalds 已提交
1409 1410 1411 1412
	q->now_rt = q->now;

	cbq_link_class(&q->link);

1413 1414
	if (tb[TCA_CBQ_LSSOPT])
		cbq_set_lss(&q->link, nla_data(tb[TCA_CBQ_LSSOPT]));
L
Linus Torvalds 已提交
1415 1416 1417

	cbq_addprio(q, &q->link);
	return 0;
1418 1419 1420 1421

put_rtab:
	qdisc_put_rtab(q->link.R_tab);
	return err;
L
Linus Torvalds 已提交
1422 1423
}

E
Eric Dumazet 已提交
1424
static int cbq_dump_rate(struct sk_buff *skb, struct cbq_class *cl)
L
Linus Torvalds 已提交
1425
{
1426
	unsigned char *b = skb_tail_pointer(skb);
L
Linus Torvalds 已提交
1427

1428 1429
	if (nla_put(skb, TCA_CBQ_RATE, sizeof(cl->R_tab->rate), &cl->R_tab->rate))
		goto nla_put_failure;
L
Linus Torvalds 已提交
1430 1431
	return skb->len;

1432
nla_put_failure:
1433
	nlmsg_trim(skb, b);
L
Linus Torvalds 已提交
1434 1435 1436
	return -1;
}

E
Eric Dumazet 已提交
1437
static int cbq_dump_lss(struct sk_buff *skb, struct cbq_class *cl)
L
Linus Torvalds 已提交
1438
{
1439
	unsigned char *b = skb_tail_pointer(skb);
L
Linus Torvalds 已提交
1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453
	struct tc_cbq_lssopt opt;

	opt.flags = 0;
	if (cl->borrow == NULL)
		opt.flags |= TCF_CBQ_LSS_BOUNDED;
	if (cl->share == NULL)
		opt.flags |= TCF_CBQ_LSS_ISOLATED;
	opt.ewma_log = cl->ewma_log;
	opt.level = cl->level;
	opt.avpkt = cl->avpkt;
	opt.maxidle = cl->maxidle;
	opt.minidle = (u32)(-cl->minidle);
	opt.offtime = cl->offtime;
	opt.change = ~0;
1454 1455
	if (nla_put(skb, TCA_CBQ_LSSOPT, sizeof(opt), &opt))
		goto nla_put_failure;
L
Linus Torvalds 已提交
1456 1457
	return skb->len;

1458
nla_put_failure:
1459
	nlmsg_trim(skb, b);
L
Linus Torvalds 已提交
1460 1461 1462
	return -1;
}

E
Eric Dumazet 已提交
1463
static int cbq_dump_wrr(struct sk_buff *skb, struct cbq_class *cl)
L
Linus Torvalds 已提交
1464
{
1465
	unsigned char *b = skb_tail_pointer(skb);
L
Linus Torvalds 已提交
1466 1467 1468 1469
	struct tc_cbq_wrropt opt;

	opt.flags = 0;
	opt.allot = cl->allot;
E
Eric Dumazet 已提交
1470 1471
	opt.priority = cl->priority + 1;
	opt.cpriority = cl->cpriority + 1;
L
Linus Torvalds 已提交
1472
	opt.weight = cl->weight;
1473 1474
	if (nla_put(skb, TCA_CBQ_WRROPT, sizeof(opt), &opt))
		goto nla_put_failure;
L
Linus Torvalds 已提交
1475 1476
	return skb->len;

1477
nla_put_failure:
1478
	nlmsg_trim(skb, b);
L
Linus Torvalds 已提交
1479 1480 1481
	return -1;
}

E
Eric Dumazet 已提交
1482
static int cbq_dump_ovl(struct sk_buff *skb, struct cbq_class *cl)
L
Linus Torvalds 已提交
1483
{
1484
	unsigned char *b = skb_tail_pointer(skb);
L
Linus Torvalds 已提交
1485 1486 1487
	struct tc_cbq_ovl opt;

	opt.strategy = cl->ovl_strategy;
E
Eric Dumazet 已提交
1488
	opt.priority2 = cl->priority2 + 1;
1489
	opt.pad = 0;
1490
	opt.penalty = cl->penalty;
1491 1492
	if (nla_put(skb, TCA_CBQ_OVL_STRATEGY, sizeof(opt), &opt))
		goto nla_put_failure;
L
Linus Torvalds 已提交
1493 1494
	return skb->len;

1495
nla_put_failure:
1496
	nlmsg_trim(skb, b);
L
Linus Torvalds 已提交
1497 1498 1499
	return -1;
}

E
Eric Dumazet 已提交
1500
static int cbq_dump_fopt(struct sk_buff *skb, struct cbq_class *cl)
L
Linus Torvalds 已提交
1501
{
1502
	unsigned char *b = skb_tail_pointer(skb);
L
Linus Torvalds 已提交
1503 1504 1505
	struct tc_cbq_fopt opt;

	if (cl->split || cl->defmap) {
1506
		opt.split = cl->split ? cl->split->common.classid : 0;
L
Linus Torvalds 已提交
1507 1508
		opt.defmap = cl->defmap;
		opt.defchange = ~0;
1509 1510
		if (nla_put(skb, TCA_CBQ_FOPT, sizeof(opt), &opt))
			goto nla_put_failure;
L
Linus Torvalds 已提交
1511 1512 1513
	}
	return skb->len;

1514
nla_put_failure:
1515
	nlmsg_trim(skb, b);
L
Linus Torvalds 已提交
1516 1517 1518
	return -1;
}

1519
#ifdef CONFIG_NET_CLS_ACT
E
Eric Dumazet 已提交
1520
static int cbq_dump_police(struct sk_buff *skb, struct cbq_class *cl)
L
Linus Torvalds 已提交
1521
{
1522
	unsigned char *b = skb_tail_pointer(skb);
L
Linus Torvalds 已提交
1523 1524 1525 1526
	struct tc_cbq_police opt;

	if (cl->police) {
		opt.police = cl->police;
1527 1528
		opt.__res1 = 0;
		opt.__res2 = 0;
1529 1530
		if (nla_put(skb, TCA_CBQ_POLICE, sizeof(opt), &opt))
			goto nla_put_failure;
L
Linus Torvalds 已提交
1531 1532 1533
	}
	return skb->len;

1534
nla_put_failure:
1535
	nlmsg_trim(skb, b);
L
Linus Torvalds 已提交
1536 1537 1538 1539 1540 1541 1542 1543 1544 1545
	return -1;
}
#endif

static int cbq_dump_attr(struct sk_buff *skb, struct cbq_class *cl)
{
	if (cbq_dump_lss(skb, cl) < 0 ||
	    cbq_dump_rate(skb, cl) < 0 ||
	    cbq_dump_wrr(skb, cl) < 0 ||
	    cbq_dump_ovl(skb, cl) < 0 ||
1546
#ifdef CONFIG_NET_CLS_ACT
L
Linus Torvalds 已提交
1547 1548 1549 1550 1551 1552 1553 1554 1555 1556
	    cbq_dump_police(skb, cl) < 0 ||
#endif
	    cbq_dump_fopt(skb, cl) < 0)
		return -1;
	return 0;
}

static int cbq_dump(struct Qdisc *sch, struct sk_buff *skb)
{
	struct cbq_sched_data *q = qdisc_priv(sch);
1557
	struct nlattr *nest;
L
Linus Torvalds 已提交
1558

1559 1560 1561
	nest = nla_nest_start(skb, TCA_OPTIONS);
	if (nest == NULL)
		goto nla_put_failure;
L
Linus Torvalds 已提交
1562
	if (cbq_dump_attr(skb, &q->link) < 0)
1563
		goto nla_put_failure;
1564
	nla_nest_end(skb, nest);
L
Linus Torvalds 已提交
1565 1566
	return skb->len;

1567
nla_put_failure:
1568
	nla_nest_cancel(skb, nest);
L
Linus Torvalds 已提交
1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584
	return -1;
}

static int
cbq_dump_stats(struct Qdisc *sch, struct gnet_dump *d)
{
	struct cbq_sched_data *q = qdisc_priv(sch);

	q->link.xstats.avgidle = q->link.avgidle;
	return gnet_stats_copy_app(d, &q->link.xstats, sizeof(q->link.xstats));
}

static int
cbq_dump_class(struct Qdisc *sch, unsigned long arg,
	       struct sk_buff *skb, struct tcmsg *tcm)
{
E
Eric Dumazet 已提交
1585
	struct cbq_class *cl = (struct cbq_class *)arg;
1586
	struct nlattr *nest;
L
Linus Torvalds 已提交
1587 1588

	if (cl->tparent)
1589
		tcm->tcm_parent = cl->tparent->common.classid;
L
Linus Torvalds 已提交
1590 1591
	else
		tcm->tcm_parent = TC_H_ROOT;
1592
	tcm->tcm_handle = cl->common.classid;
L
Linus Torvalds 已提交
1593 1594
	tcm->tcm_info = cl->q->handle;

1595 1596 1597
	nest = nla_nest_start(skb, TCA_OPTIONS);
	if (nest == NULL)
		goto nla_put_failure;
L
Linus Torvalds 已提交
1598
	if (cbq_dump_attr(skb, cl) < 0)
1599
		goto nla_put_failure;
1600
	nla_nest_end(skb, nest);
L
Linus Torvalds 已提交
1601 1602
	return skb->len;

1603
nla_put_failure:
1604
	nla_nest_cancel(skb, nest);
L
Linus Torvalds 已提交
1605 1606 1607 1608 1609 1610 1611 1612
	return -1;
}

static int
cbq_dump_class_stats(struct Qdisc *sch, unsigned long arg,
	struct gnet_dump *d)
{
	struct cbq_sched_data *q = qdisc_priv(sch);
E
Eric Dumazet 已提交
1613
	struct cbq_class *cl = (struct cbq_class *)arg;
L
Linus Torvalds 已提交
1614 1615 1616 1617 1618

	cl->qstats.qlen = cl->q->q.qlen;
	cl->xstats.avgidle = cl->avgidle;
	cl->xstats.undertime = 0;

1619
	if (cl->undertime != PSCHED_PASTPERFECT)
P
Patrick McHardy 已提交
1620
		cl->xstats.undertime = cl->undertime - q->now;
L
Linus Torvalds 已提交
1621 1622

	if (gnet_stats_copy_basic(d, &cl->bstats) < 0 ||
1623
	    gnet_stats_copy_rate_est(d, &cl->bstats, &cl->rate_est) < 0 ||
L
Linus Torvalds 已提交
1624 1625 1626 1627 1628 1629 1630 1631 1632
	    gnet_stats_copy_queue(d, &cl->qstats) < 0)
		return -1;

	return gnet_stats_copy_app(d, &cl->xstats, sizeof(cl->xstats));
}

static int cbq_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
		     struct Qdisc **old)
{
E
Eric Dumazet 已提交
1633
	struct cbq_class *cl = (struct cbq_class *)arg;
L
Linus Torvalds 已提交
1634

1635
	if (new == NULL) {
1636
		new = qdisc_create_dflt(sch->dev_queue,
1637 1638 1639 1640
					&pfifo_qdisc_ops, cl->common.classid);
		if (new == NULL)
			return -ENOBUFS;
	} else {
1641
#ifdef CONFIG_NET_CLS_ACT
1642 1643
		if (cl->police == TC_POLICE_RECLASSIFY)
			new->reshape_fail = cbq_reshape_fail;
L
Linus Torvalds 已提交
1644 1645
#endif
	}
1646 1647 1648 1649 1650 1651 1652 1653
	sch_tree_lock(sch);
	*old = cl->q;
	cl->q = new;
	qdisc_tree_decrease_qlen(*old, (*old)->q.qlen);
	qdisc_reset(*old);
	sch_tree_unlock(sch);

	return 0;
L
Linus Torvalds 已提交
1654 1655
}

E
Eric Dumazet 已提交
1656
static struct Qdisc *cbq_leaf(struct Qdisc *sch, unsigned long arg)
L
Linus Torvalds 已提交
1657
{
E
Eric Dumazet 已提交
1658
	struct cbq_class *cl = (struct cbq_class *)arg;
L
Linus Torvalds 已提交
1659

1660
	return cl->q;
L
Linus Torvalds 已提交
1661 1662
}

1663 1664 1665 1666 1667 1668 1669 1670
static void cbq_qlen_notify(struct Qdisc *sch, unsigned long arg)
{
	struct cbq_class *cl = (struct cbq_class *)arg;

	if (cl->q->q.qlen == 0)
		cbq_deactivate_class(cl);
}

L
Linus Torvalds 已提交
1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686
static unsigned long cbq_get(struct Qdisc *sch, u32 classid)
{
	struct cbq_sched_data *q = qdisc_priv(sch);
	struct cbq_class *cl = cbq_class_lookup(q, classid);

	if (cl) {
		cl->refcnt++;
		return (unsigned long)cl;
	}
	return 0;
}

static void cbq_destroy_class(struct Qdisc *sch, struct cbq_class *cl)
{
	struct cbq_sched_data *q = qdisc_priv(sch);

1687
	WARN_ON(cl->filters);
L
Linus Torvalds 已提交
1688

1689
	tcf_destroy_chain(&cl->filter_list);
L
Linus Torvalds 已提交
1690 1691 1692 1693 1694 1695 1696
	qdisc_destroy(cl->q);
	qdisc_put_rtab(cl->R_tab);
	gen_kill_estimator(&cl->bstats, &cl->rate_est);
	if (cl != &q->link)
		kfree(cl);
}

E
Eric Dumazet 已提交
1697
static void cbq_destroy(struct Qdisc *sch)
L
Linus Torvalds 已提交
1698 1699
{
	struct cbq_sched_data *q = qdisc_priv(sch);
1700
	struct hlist_node *next;
L
Linus Torvalds 已提交
1701
	struct cbq_class *cl;
E
Eric Dumazet 已提交
1702
	unsigned int h;
L
Linus Torvalds 已提交
1703

1704
#ifdef CONFIG_NET_CLS_ACT
L
Linus Torvalds 已提交
1705 1706 1707 1708 1709 1710 1711
	q->rx_class = NULL;
#endif
	/*
	 * Filters must be destroyed first because we don't destroy the
	 * classes from root to leafs which means that filters can still
	 * be bound to classes which have been destroyed already. --TGR '04
	 */
1712
	for (h = 0; h < q->clhash.hashsize; h++) {
1713
		hlist_for_each_entry(cl, &q->clhash.hash[h], common.hnode)
1714
			tcf_destroy_chain(&cl->filter_list);
1715
	}
1716
	for (h = 0; h < q->clhash.hashsize; h++) {
1717
		hlist_for_each_entry_safe(cl, next, &q->clhash.hash[h],
1718
					  common.hnode)
L
Linus Torvalds 已提交
1719 1720
			cbq_destroy_class(sch, cl);
	}
1721
	qdisc_class_hash_destroy(&q->clhash);
L
Linus Torvalds 已提交
1722 1723 1724 1725
}

static void cbq_put(struct Qdisc *sch, unsigned long arg)
{
E
Eric Dumazet 已提交
1726
	struct cbq_class *cl = (struct cbq_class *)arg;
L
Linus Torvalds 已提交
1727 1728

	if (--cl->refcnt == 0) {
1729
#ifdef CONFIG_NET_CLS_ACT
1730
		spinlock_t *root_lock = qdisc_root_sleeping_lock(sch);
L
Linus Torvalds 已提交
1731 1732
		struct cbq_sched_data *q = qdisc_priv(sch);

1733
		spin_lock_bh(root_lock);
L
Linus Torvalds 已提交
1734 1735
		if (q->rx_class == cl)
			q->rx_class = NULL;
1736
		spin_unlock_bh(root_lock);
L
Linus Torvalds 已提交
1737 1738 1739 1740 1741 1742 1743
#endif

		cbq_destroy_class(sch, cl);
	}
}

static int
1744
cbq_change_class(struct Qdisc *sch, u32 classid, u32 parentid, struct nlattr **tca,
L
Linus Torvalds 已提交
1745 1746 1747 1748
		 unsigned long *arg)
{
	int err;
	struct cbq_sched_data *q = qdisc_priv(sch);
E
Eric Dumazet 已提交
1749
	struct cbq_class *cl = (struct cbq_class *)*arg;
1750 1751
	struct nlattr *opt = tca[TCA_OPTIONS];
	struct nlattr *tb[TCA_CBQ_MAX + 1];
L
Linus Torvalds 已提交
1752 1753 1754
	struct cbq_class *parent;
	struct qdisc_rate_table *rtab = NULL;

1755
	if (opt == NULL)
L
Linus Torvalds 已提交
1756 1757
		return -EINVAL;

1758
	err = nla_parse_nested(tb, TCA_CBQ_MAX, opt, cbq_policy);
1759 1760 1761
	if (err < 0)
		return err;

L
Linus Torvalds 已提交
1762 1763 1764
	if (cl) {
		/* Check parent */
		if (parentid) {
1765 1766
			if (cl->tparent &&
			    cl->tparent->common.classid != parentid)
L
Linus Torvalds 已提交
1767 1768 1769 1770 1771
				return -EINVAL;
			if (!cl->tparent && parentid != TC_H_ROOT)
				return -EINVAL;
		}

1772
		if (tb[TCA_CBQ_RATE]) {
1773 1774
			rtab = qdisc_get_rtab(nla_data(tb[TCA_CBQ_RATE]),
					      tb[TCA_CBQ_RTAB]);
L
Linus Torvalds 已提交
1775 1776 1777 1778
			if (rtab == NULL)
				return -EINVAL;
		}

1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789
		if (tca[TCA_RATE]) {
			err = gen_replace_estimator(&cl->bstats, &cl->rate_est,
						    qdisc_root_sleeping_lock(sch),
						    tca[TCA_RATE]);
			if (err) {
				if (rtab)
					qdisc_put_rtab(rtab);
				return err;
			}
		}

L
Linus Torvalds 已提交
1790 1791 1792 1793 1794 1795 1796
		/* Change class parameters */
		sch_tree_lock(sch);

		if (cl->next_alive != NULL)
			cbq_deactivate_class(cl);

		if (rtab) {
1797 1798
			qdisc_put_rtab(cl->R_tab);
			cl->R_tab = rtab;
L
Linus Torvalds 已提交
1799 1800
		}

1801 1802
		if (tb[TCA_CBQ_LSSOPT])
			cbq_set_lss(cl, nla_data(tb[TCA_CBQ_LSSOPT]));
L
Linus Torvalds 已提交
1803

1804
		if (tb[TCA_CBQ_WRROPT]) {
L
Linus Torvalds 已提交
1805
			cbq_rmprio(q, cl);
1806
			cbq_set_wrr(cl, nla_data(tb[TCA_CBQ_WRROPT]));
L
Linus Torvalds 已提交
1807 1808
		}

1809 1810
		if (tb[TCA_CBQ_OVL_STRATEGY])
			cbq_set_overlimit(cl, nla_data(tb[TCA_CBQ_OVL_STRATEGY]));
L
Linus Torvalds 已提交
1811

1812
#ifdef CONFIG_NET_CLS_ACT
1813 1814
		if (tb[TCA_CBQ_POLICE])
			cbq_set_police(cl, nla_data(tb[TCA_CBQ_POLICE]));
L
Linus Torvalds 已提交
1815 1816
#endif

1817 1818
		if (tb[TCA_CBQ_FOPT])
			cbq_set_fopt(cl, nla_data(tb[TCA_CBQ_FOPT]));
L
Linus Torvalds 已提交
1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830

		if (cl->q->q.qlen)
			cbq_activate_class(cl);

		sch_tree_unlock(sch);

		return 0;
	}

	if (parentid == TC_H_ROOT)
		return -EINVAL;

1831 1832
	if (tb[TCA_CBQ_WRROPT] == NULL || tb[TCA_CBQ_RATE] == NULL ||
	    tb[TCA_CBQ_LSSOPT] == NULL)
L
Linus Torvalds 已提交
1833 1834
		return -EINVAL;

1835
	rtab = qdisc_get_rtab(nla_data(tb[TCA_CBQ_RATE]), tb[TCA_CBQ_RTAB]);
L
Linus Torvalds 已提交
1836 1837 1838 1839 1840
	if (rtab == NULL)
		return -EINVAL;

	if (classid) {
		err = -EINVAL;
E
Eric Dumazet 已提交
1841 1842
		if (TC_H_MAJ(classid ^ sch->handle) ||
		    cbq_class_lookup(q, classid))
L
Linus Torvalds 已提交
1843 1844 1845
			goto failure;
	} else {
		int i;
E
Eric Dumazet 已提交
1846
		classid = TC_H_MAKE(sch->handle, 0x8000);
L
Linus Torvalds 已提交
1847

E
Eric Dumazet 已提交
1848
		for (i = 0; i < 0x8000; i++) {
L
Linus Torvalds 已提交
1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868
			if (++q->hgenerator >= 0x8000)
				q->hgenerator = 1;
			if (cbq_class_lookup(q, classid|q->hgenerator) == NULL)
				break;
		}
		err = -ENOSR;
		if (i >= 0x8000)
			goto failure;
		classid = classid|q->hgenerator;
	}

	parent = &q->link;
	if (parentid) {
		parent = cbq_class_lookup(q, parentid);
		err = -EINVAL;
		if (parent == NULL)
			goto failure;
	}

	err = -ENOBUFS;
1869
	cl = kzalloc(sizeof(*cl), GFP_KERNEL);
L
Linus Torvalds 已提交
1870 1871
	if (cl == NULL)
		goto failure;
1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882

	if (tca[TCA_RATE]) {
		err = gen_new_estimator(&cl->bstats, &cl->rate_est,
					qdisc_root_sleeping_lock(sch),
					tca[TCA_RATE]);
		if (err) {
			kfree(cl);
			goto failure;
		}
	}

L
Linus Torvalds 已提交
1883 1884 1885
	cl->R_tab = rtab;
	rtab = NULL;
	cl->refcnt = 1;
1886 1887
	cl->q = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops, classid);
	if (!cl->q)
L
Linus Torvalds 已提交
1888
		cl->q = &noop_qdisc;
1889
	cl->common.classid = classid;
L
Linus Torvalds 已提交
1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902
	cl->tparent = parent;
	cl->qdisc = sch;
	cl->allot = parent->allot;
	cl->quantum = cl->allot;
	cl->weight = cl->R_tab->rate.rate;

	sch_tree_lock(sch);
	cbq_link_class(cl);
	cl->borrow = cl->tparent;
	if (cl->tparent != &q->link)
		cl->share = cl->tparent;
	cbq_adjust_levels(parent);
	cl->minidle = -0x7FFFFFFF;
1903 1904
	cbq_set_lss(cl, nla_data(tb[TCA_CBQ_LSSOPT]));
	cbq_set_wrr(cl, nla_data(tb[TCA_CBQ_WRROPT]));
E
Eric Dumazet 已提交
1905
	if (cl->ewma_log == 0)
L
Linus Torvalds 已提交
1906
		cl->ewma_log = q->link.ewma_log;
E
Eric Dumazet 已提交
1907
	if (cl->maxidle == 0)
L
Linus Torvalds 已提交
1908
		cl->maxidle = q->link.maxidle;
E
Eric Dumazet 已提交
1909
	if (cl->avpkt == 0)
L
Linus Torvalds 已提交
1910 1911
		cl->avpkt = q->link.avpkt;
	cl->overlimit = cbq_ovl_classic;
1912 1913
	if (tb[TCA_CBQ_OVL_STRATEGY])
		cbq_set_overlimit(cl, nla_data(tb[TCA_CBQ_OVL_STRATEGY]));
1914
#ifdef CONFIG_NET_CLS_ACT
1915 1916
	if (tb[TCA_CBQ_POLICE])
		cbq_set_police(cl, nla_data(tb[TCA_CBQ_POLICE]));
L
Linus Torvalds 已提交
1917
#endif
1918 1919
	if (tb[TCA_CBQ_FOPT])
		cbq_set_fopt(cl, nla_data(tb[TCA_CBQ_FOPT]));
L
Linus Torvalds 已提交
1920 1921
	sch_tree_unlock(sch);

1922 1923
	qdisc_class_hash_grow(sch, &q->clhash);

L
Linus Torvalds 已提交
1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934
	*arg = (unsigned long)cl;
	return 0;

failure:
	qdisc_put_rtab(rtab);
	return err;
}

static int cbq_delete(struct Qdisc *sch, unsigned long arg)
{
	struct cbq_sched_data *q = qdisc_priv(sch);
E
Eric Dumazet 已提交
1935
	struct cbq_class *cl = (struct cbq_class *)arg;
1936
	unsigned int qlen;
L
Linus Torvalds 已提交
1937 1938 1939 1940 1941 1942

	if (cl->filters || cl->children || cl == &q->link)
		return -EBUSY;

	sch_tree_lock(sch);

1943 1944 1945 1946
	qlen = cl->q->q.qlen;
	qdisc_reset(cl->q);
	qdisc_tree_decrease_qlen(cl->q, qlen);

L
Linus Torvalds 已提交
1947 1948 1949 1950 1951 1952 1953 1954 1955
	if (cl->next_alive)
		cbq_deactivate_class(cl);

	if (q->tx_borrowed == cl)
		q->tx_borrowed = q->tx_class;
	if (q->tx_class == cl) {
		q->tx_class = NULL;
		q->tx_borrowed = NULL;
	}
1956
#ifdef CONFIG_NET_CLS_ACT
L
Linus Torvalds 已提交
1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968
	if (q->rx_class == cl)
		q->rx_class = NULL;
#endif

	cbq_unlink_class(cl);
	cbq_adjust_levels(cl->tparent);
	cl->defmap = 0;
	cbq_sync_defmap(cl);

	cbq_rmprio(q, cl);
	sch_tree_unlock(sch);

1969 1970 1971 1972 1973
	BUG_ON(--cl->refcnt == 0);
	/*
	 * This shouldn't happen: we "hold" one cops->get() when called
	 * from tc_ctl_tclass; the destroy method is done from cops->put().
	 */
L
Linus Torvalds 已提交
1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992

	return 0;
}

static struct tcf_proto **cbq_find_tcf(struct Qdisc *sch, unsigned long arg)
{
	struct cbq_sched_data *q = qdisc_priv(sch);
	struct cbq_class *cl = (struct cbq_class *)arg;

	if (cl == NULL)
		cl = &q->link;

	return &cl->filter_list;
}

static unsigned long cbq_bind_filter(struct Qdisc *sch, unsigned long parent,
				     u32 classid)
{
	struct cbq_sched_data *q = qdisc_priv(sch);
E
Eric Dumazet 已提交
1993
	struct cbq_class *p = (struct cbq_class *)parent;
L
Linus Torvalds 已提交
1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006
	struct cbq_class *cl = cbq_class_lookup(q, classid);

	if (cl) {
		if (p && p->level <= cl->level)
			return 0;
		cl->filters++;
		return (unsigned long)cl;
	}
	return 0;
}

static void cbq_unbind_filter(struct Qdisc *sch, unsigned long arg)
{
E
Eric Dumazet 已提交
2007
	struct cbq_class *cl = (struct cbq_class *)arg;
L
Linus Torvalds 已提交
2008 2009 2010 2011 2012 2013 2014

	cl->filters--;
}

static void cbq_walk(struct Qdisc *sch, struct qdisc_walker *arg)
{
	struct cbq_sched_data *q = qdisc_priv(sch);
2015
	struct cbq_class *cl;
E
Eric Dumazet 已提交
2016
	unsigned int h;
L
Linus Torvalds 已提交
2017 2018 2019 2020

	if (arg->stop)
		return;

2021
	for (h = 0; h < q->clhash.hashsize; h++) {
2022
		hlist_for_each_entry(cl, &q->clhash.hash[h], common.hnode) {
L
Linus Torvalds 已提交
2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035
			if (arg->count < arg->skip) {
				arg->count++;
				continue;
			}
			if (arg->fn(sch, (unsigned long)cl, arg) < 0) {
				arg->stop = 1;
				return;
			}
			arg->count++;
		}
	}
}

2036
static const struct Qdisc_class_ops cbq_class_ops = {
L
Linus Torvalds 已提交
2037 2038
	.graft		=	cbq_graft,
	.leaf		=	cbq_leaf,
2039
	.qlen_notify	=	cbq_qlen_notify,
L
Linus Torvalds 已提交
2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051
	.get		=	cbq_get,
	.put		=	cbq_put,
	.change		=	cbq_change_class,
	.delete		=	cbq_delete,
	.walk		=	cbq_walk,
	.tcf_chain	=	cbq_find_tcf,
	.bind_tcf	=	cbq_bind_filter,
	.unbind_tcf	=	cbq_unbind_filter,
	.dump		=	cbq_dump_class,
	.dump_stats	=	cbq_dump_class_stats,
};

2052
static struct Qdisc_ops cbq_qdisc_ops __read_mostly = {
L
Linus Torvalds 已提交
2053 2054 2055 2056 2057 2058
	.next		=	NULL,
	.cl_ops		=	&cbq_class_ops,
	.id		=	"cbq",
	.priv_size	=	sizeof(struct cbq_sched_data),
	.enqueue	=	cbq_enqueue,
	.dequeue	=	cbq_dequeue,
2059
	.peek		=	qdisc_peek_dequeued,
L
Linus Torvalds 已提交
2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073
	.drop		=	cbq_drop,
	.init		=	cbq_init,
	.reset		=	cbq_reset,
	.destroy	=	cbq_destroy,
	.change		=	NULL,
	.dump		=	cbq_dump,
	.dump_stats	=	cbq_dump_stats,
	.owner		=	THIS_MODULE,
};

static int __init cbq_module_init(void)
{
	return register_qdisc(&cbq_qdisc_ops);
}
2074
static void __exit cbq_module_exit(void)
L
Linus Torvalds 已提交
2075 2076 2077 2078 2079 2080
{
	unregister_qdisc(&cbq_qdisc_ops);
}
module_init(cbq_module_init)
module_exit(cbq_module_exit)
MODULE_LICENSE("GPL");