Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

Loading...
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
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
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
/*
 * 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 <asm/uaccess.h>
#include <asm/system.h>
#include <asm/bitops.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/string.h>
#include <linux/mm.h>
#include <linux/socket.h>
#include <linux/sockios.h>
#include <linux/in.h>
#include <linux/errno.h>
#include <linux/interrupt.h>
#include <linux/if_ether.h>
#include <linux/inet.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/notifier.h>
#include <linux/module.h>
#include <net/ip.h>
#include <net/route.h>
#include <linux/skbuff.h>
#include <net/sock.h>
#include <net/pkt_sched.h>

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

	Sources: [1] Sally Floyd and Van Jacobson, "Link-sharing and Resource
	         Management Models for Packet Networks",
		 IEEE/ACM Transactions on Networking, Vol.3, No.4, 1995

	         [2] Sally Floyd, "Notes on CBQ and Guaranted Service", 1995

	         [3] Sally Floyd, "Notes on Class-Based Queueing: Setting
		 Parameters", 1996

	Algorithm skeleton is taken from from NS simulator cbq.cc.

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

	Differences from NS version.

	--- WRR algorith is different. Our version looks more reasonable :-)
	and fair when quanta are allowed to be less than MTU.

	--- cl->aveidle is REALLY limited from below by cl->minidle.
	Seems, it was bug in NS.

	--- Purely lexical change: "depth" -> "level", "maxdepth" -> "toplevel".
	When depth increases we expect, that the thing becomes lower, does not it? :-)
	Besides that, "depth" word is semantically overloaded ---
	"token bucket depth", "sfq depth"... Besides that, the algorithm
	was called "top-LEVEL sharing".

	PROBLEM.

	--- Linux has no EOI event at the moment, so that we cannot
	estimate true class idle time. Three workarounds are possible,
	all of them have drawbacks:

	1. (as now) Consider the next dequeue event as sign that
	previous packet is finished. It is wrong because of ping-pong
	buffers, but on permanently loaded link it is true.
	2. (NS approach) Use as link busy time estimate skb->leb/"physical
	bandwidth". Even more wrong f.e. on ethernet real busy time much
	higher because of collisions.
	3. (seems, the most clever) Split net bh to two parts:
	NETRX_BH (for received packets) and preserve NET_BH for transmitter.
	It will not require driver changes (NETRX_BH flag will be set
	in netif_rx), but will allow to trace EOIs more precisely
	and will save useless checks in net_bh. Besides that we will
	have to eliminate random calling hard_start_xmit with dev->tbusy flag
	(done) and to drop failure_q --- i.e. if !dev->tbusy hard_start_xmit
	MUST succeed; failed packets will be dropped on the floor.
*/

#define CBQ_TOPLEVEL_SHARING
/* #define CBQ_NO_TRICKERY */

#define CBQ_CLASSIFIER(skb, q) ((q)->fallback_class)

struct cbq_class
{
/* Parameters */
	int			priority;	/* priority */
#ifdef CBQ_TOPLEVEL_SHARING
	int			level;		/* level of the class in hierarchy:
						   0 for leaf classes, and maximal
						   level of childrens + 1 for nodes.
						 */
#endif

	long			maxidle;	/* Class paramters: see below. */
	long			minidle;
	int			filter_log;
#ifndef CBQ_NO_TRICKERY
	long			extradelay;
#endif

	long			quantum;	/* Allotment per WRR round */
	long			rquantum;	/* Relative allotment: see below */

	int			cell_log;
	unsigned long		L_tab[256];

	struct Qdisc		*qdisc;		/* ptr to CBQ discipline */
	struct cbq_class	*root;		/* Ptr to root class;
						   root can be not unique.
						 */
	struct cbq_class	*parent;	/* Ptr to parent in the class tree */
	struct cbq_class	*borrow;	/* NULL if class is bandwidth limited;
						   parent otherwise */

	struct Qdisc		*q;		/* Elementary queueing discipline */
	struct cbq_class	*next;		/* next class in this priority band */

	struct cbq_class	*next_alive;	/* next class with backlog in this priority band */

/* Variables */
	psched_time_t		last;
	psched_time_t		undertime;
	long			avgidle;
	long			deficit;	/* Saved deficit for WRR */
	char			awake;		/* Class is in alive list */

#if 0
	void			(*overlimit)(struct cbq_class *cl);
#endif
};

#define L2T(cl,len)	((cl)->L_tab[(len)>>(cl)->cell_log])

struct cbq_sched_data
{
	struct cbq_class	*classes[CBQ_MAXPRIO];	/* List of all classes */
	int			nclasses[CBQ_MAXPRIO];
	unsigned		quanta[CBQ_MAXPRIO];
	unsigned		mtu;
	int			cell_log;
	unsigned long		L_tab[256];
	struct cbq_class	*fallback_class;

	unsigned		activemask;
	struct cbq_class	*active[CBQ_MAXPRIO];	/* List of all classes
							   with backlog */
	struct cbq_class	*last_sent;
	int			last_sent_len;

	psched_time_t		now;		/* Cached timestamp */

	struct timer_list	wd_timer;	/* Wathchdog timer, that
						   started when CBQ has
						   backlog, but cannot
						   transmit just now */
	unsigned long		wd_expires;
#ifdef CBQ_TOPLEVEL_SHARING
	struct cbq_class	*borrowed;
	int			toplevel;
#endif
};

/*
   WRR quanta
   ----------

   cl->quantum is number added to class allotment on every round.
   cl->rquantum is "relative" quantum.

   For real-time classes:

   cl->quantum = (cl->rquantum*q->nclasses[prio]*q->mtu)/q->quanta[prio]

   where q->quanta[prio] is sum of all rquanta for given priority.
   cl->rquantum can be identified with absolute rate of the class
   in arbitrary units (f.e. bytes/sec)

   In this case, delay introduced by round-robin was estimated by
   Sally Floyd [2] as:

   D = q->nclasses*q->mtu/(bandwidth/2)

   Note, that D does not depend on class rate (it is very bad),
   but not much worse than Gallager-Parekh estimate for CSZ
   C/R = q->mtu/rate, when real-time classes have close rates.

   For not real-time classes this folmula is not necessary,
   so that cl->quantum can be set to any reasonable not zero value.
   Apparently, it should be proportional to class rate, if the
   rate is not zero.
*/

/*
   maxidle, minidle, extradelay
   ----------------------------

   CBQ estimator calculates smoothed class idle time cl->aveidle,
   considering class as virtual interface with corresponding bandwidth.
   When cl->aveidle wants to be less than zero, class is overlimit.
   When it is positive, class is underlimit.

   * maxidle bounds aveidle from above.
     It controls maximal length of burst in this class after
     long period of idle time. Burstness of active class
     is controlled by filter constant cl->filter_log,
     but this number is related to burst length only indirectly.

   * minidle is a negative number, normally set to zero.
     Setting it to not zero value allows avgidle to drop
     below zero, effectively penalizing class, when it is overlimit.
     When the class load will decrease, it will take a time to
     raise negative avgidle to put the class at limit.
     It should be set to zero for leaf classes.

   * extradelay is penalty in delay, when a class goes overlimit.
     I believe this parameter is useless and confusing.
     Setting it to not zero forces class to accumulate
     its "idleness" for extradelay and then send BURST of packets
     until going to overlimit again. Non-sense.

   For details see [1] and [3].

   Really, minidle and extradelay are irrelevant to real scheduling
   task. As I understand, SF&VJ introduced them to experiment
   with CBQ simulator in attempts to fix erratic behaviour
   of ancestor-only (and, partially, top-level) algorithm.

   WARNING.

   User passes them measured in usecs, but cl->minidle,
   cl->maxidle and cl->aveidle are scaled with cl->filter_log
   in the text of the scheduler.
*/

/*
   A packet has just been enqueued on the empty class.
   cbq_wakeup_class adds it to the tail of active class list
   of its priority band.
 */

static __inline__ void cbq_wakeup_class(struct cbq_class *cl)
{
	struct cbq_sched_data *q = (struct cbq_sched_data*)cl->qdisc->data;
	int prio = cl->priority;
	struct cbq_class *cl_tail;

	cl->awake = 1;

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

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

static int
cbq_enqueue(struct sk_buff *skb, struct Qdisc *sch)
{
	struct cbq_sched_data *q = (struct cbq_sched_data *)sch->data;
	struct cbq_class *cl = CBQ_CLASSIFIER(skb, q);

	if (cl->q->enqueue(skb, cl->q) == 1) {
		sch->q.qlen++;

#ifdef CBQ_TOPLEVEL_SHARING
		if (q->toplevel > 0) {
			psched_time_t now;
			PSCHED_GET_TIME(now);
			if (PSCHED_TLESS(cl->undertime, now))
				q->toplevel = 0;
			else if (q->toplevel > 1 && cl->borrow &&
				 PSCHED_TLESS(cl->borrow->undertime, now))
				q->toplevel = 1;
		}
#endif
		if (!cl->awake)
			cbq_wakeup_class(cl);
		return 1;
	}
	return 0;
}

static __inline__ void cbq_delay(struct cbq_sched_data *q, struct cbq_class *cl)
{
	long delay;

	delay = PSCHED_TDIFF(cl->undertime, q->now);
	if (q->wd_expires == 0 || q->wd_expires - delay > 0)
		q->wd_expires = delay;
}

static void cbq_watchdog(unsigned long arg)
{
	struct Qdisc *sch = (struct Qdisc*)arg;
	struct cbq_sched_data *q = (struct cbq_sched_data*)sch->data;

	q->wd_timer.expires = 0;
	q->wd_timer.function = NULL;
	qdisc_wakeup(sch->dev);
}

static __inline__ void
cbq_update(struct cbq_sched_data *q)
{
	struct cbq_class *cl;

	for (cl = q->last_sent; cl; cl = cl->parent) {
		long avgidle = cl->avgidle;
		long idle;

		/*
		   (now - last) is total time between packet right edges.
		   (last_pktlen/rate) is "virtual" busy time, so that

		         idle = (now - last) - last_pktlen/rate
		 */

		idle = PSCHED_TDIFF(q->now, cl->last)
			- L2T(cl, q->last_sent_len);

		/* true_avgidle := (1-W)*true_avgidle + W*idle,
		   where W=2^{-filter_log}. But cl->avgidle is scaled:
		   cl->avgidle == true_avgidle/W,
		   hence:
		 */
		avgidle += idle - (avgidle>>cl->filter_log);

		if (avgidle <= 0) {
			/* Overlimit or at-limit */
#ifdef CBQ_NO_TRICKERY
			avgidle = 0;
#else
			if (avgidle < cl->minidle)
				avgidle = cl->minidle;
#endif

			/* This line was missing in NS. */
			cl->avgidle = avgidle;

			/* Calculate expected time, when this class
			   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);

			   That is not all.
			   We want to set undertime to the moment, when
			   the class is allowed to start next transmission i.e.
			   (undertime + next_pktlen/phys_bandwidth)
			   - now - next_pktlen/rate = idle
			   or
			   undertime = now + idle + next_pktlen/rate
			   - next_pktlen/phys_bandwidth

			   We do not know next packet length, but can
			   estimate it with average packet length
			   or current packet_length.
			 */

			idle = (-avgidle) - ((-avgidle) >> cl->filter_log);
			idle += L2T(q, q->last_sent_len);
			idle -= L2T(cl, q->last_sent_len);
			PSCHED_TADD2(q->now, idle, cl->undertime);
#ifndef CBQ_NO_TRICKERY
			/* Do not forget extra delay :-) */
			PSCHED_TADD(cl->undertime, cl->extradelay);
#endif
		} else {
			/* Underlimit */

			PSCHED_SET_PASTPERFECT(cl->undertime);
			if (avgidle > cl->maxidle)
				cl->avgidle = cl->maxidle;
			else
				cl->avgidle = avgidle;
		}
		cl->last = q->now;
	}

#ifdef CBQ_TOPLEVEL_SHARING
	cl = q->last_sent;

	if (q->borrowed && q->toplevel >= q->borrowed->level) {
		if (cl->q->q.qlen <= 1 || PSCHED_TLESS(q->now, q->borrowed->undertime))
			q->toplevel = CBQ_MAXLEVEL;
		else if (q->borrowed != cl)
			q->toplevel = q->borrowed->level;
	}
#endif

	q->last_sent = NULL;
}

static __inline__ int
cbq_under_limit(struct cbq_class *cl)
{
	struct cbq_sched_data *q = (struct cbq_sched_data*)cl->qdisc->data;
	struct cbq_class *this_cl = cl;

	if (PSCHED_IS_PASTPERFECT(cl->undertime) || cl->parent == NULL)
		return 1;

	if (PSCHED_TLESS(cl->undertime, q->now)) {
		q->borrowed = cl;
		return 1;
	}

	while (!PSCHED_IS_PASTPERFECT(cl->undertime) &&
	       PSCHED_TLESS(q->now, cl->undertime)) {
		cl = cl->borrow;
		if (cl == NULL
#ifdef CBQ_TOPLEVEL_SHARING
		    || cl->level > q->toplevel
#endif
		    ) {
#if 0
			this_cl->overlimit(this_cl);
#else
			cbq_delay(q, this_cl);
#endif
			return 0;
		}
	}
	q->borrowed = cl;
	return 1;
}

static __inline__ struct sk_buff *
cbq_dequeue_prio(struct Qdisc *sch, int prio, int fallback)
{
	struct cbq_sched_data *q = (struct cbq_sched_data *)sch->data;
	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 {
			/* Class is empty */
			if (cl->q->q.qlen == 0) 
				goto skip_class;
			
			if (fallback) {
				/* Fallback pass: all classes are overlimit;
				   we send from the first class that is allowed
				   to borrow.
				 */

				if (cl->borrow == NULL)
					goto skip_class;
			} else {
				/* Normal pass: check that class is under limit */
				if (!cbq_under_limit(cl))
					goto skip_class;
			}

			if (cl->deficit <= 0) {
				/* Class exhausted its allotment per this
				   round.
				 */
				deficit = 1;
				goto next_class;
			}

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

			/* Class did not give us any skb :-(
			   It could occur if cl->q == "tbf"
			 */
			if (skb == NULL)
				goto skip_class;

			cl->deficit -= skb->len;
			q->last_sent = cl;
			q->last_sent_len = skb->len;

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

skip_class:
			cl->deficit = 0;

			if (cl->q->q.qlen == 0) {
				/* Class is empty, declare it dead */
				cl_prev->next_alive = cl->next_alive;
				cl->awake = 0;

				/* 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);
						return NULL;
					}
				}
			}

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

	q->active[prio] = cl_prev;
	
	return NULL;
}

static __inline__ struct sk_buff *
cbq_dequeue_1(struct Qdisc *sch, int fallback)
{
	struct cbq_sched_data *q = (struct cbq_sched_data *)sch->data;
	struct sk_buff *skb;
	unsigned activemask;

	activemask = q->activemask;
	while (activemask) {
		int prio = ffz(~activemask);
		activemask &= ~(1<<prio);
		skb = cbq_dequeue_prio(sch, prio, fallback);
		if (skb)
			return skb;
	}
	return NULL;
}

static struct sk_buff *
cbq_dequeue(struct Qdisc *sch)
{
	struct sk_buff *skb;
	struct cbq_sched_data *q = (struct cbq_sched_data *)sch->data;

	PSCHED_GET_TIME(q->now);

	if (q->last_sent)
		cbq_update(q);

	q->wd_expires = 0;

	skb = cbq_dequeue_1(sch, 0);
	if (skb)
		return skb;

	/* All the classes are overlimit.
	   Search for overlimit class, which is allowed to borrow
	   and use it as fallback case.
	 */

#ifdef CBQ_TOPLEVEL_SHARING
	q->toplevel = CBQ_MAXLEVEL;
#endif

	skb = cbq_dequeue_1(sch, 1);
	if (skb)
		return skb;

	/* No packets in scheduler or nobody wants to give them to us :-(
	   Sigh... start watchdog timer in the last case. */

	if (sch->q.qlen && q->wd_expires) {
		if (q->wd_timer.function)
			del_timer(&q->wd_timer);
		q->wd_timer.function = cbq_watchdog;
		q->wd_timer.expires = jiffies + PSCHED_US2JIFFIE(q->wd_expires);
		add_timer(&q->wd_timer);
	}
	return NULL;
}

/* CBQ class maintanance routines */

static void cbq_adjust_levels(struct cbq_class *this)
{
	struct cbq_class *cl;

	for (cl = this->parent; cl; cl = cl->parent) {
		if (cl->level > this->level)
			return;
		cl->level = this->level + 1;
		this = cl;
	}
}

static void cbq_normalize_quanta(struct cbq_sched_data *q, int prio)
{
	struct cbq_class *cl;

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

	for (cl = q->classes[prio]; cl; cl = cl->next) {
		if (cl->rquantum)
			cl->quantum = (cl->rquantum*q->mtu*q->nclasses[prio])/
				q->quanta[prio];
	}
}

static __inline__ int cbq_unlink_class(struct cbq_class *this)
{
	struct cbq_class *cl, **clp;
	struct cbq_sched_data *q = (struct cbq_sched_data*)this->qdisc->data;

	for (clp = &q->classes[this->priority]; (cl = *clp) != NULL;
	     clp = &cl->next) {
		if (cl == this) {
			*clp = cl->next;
			return 0;
		}
	}
	return -ENOENT;
}

static int cbq_prune(struct cbq_class *this)
{
	struct cbq_class *cl;
	int prio = this->priority;
	struct cbq_sched_data *q = (struct cbq_sched_data*)this->qdisc->data;

	qdisc_reset(this->q);

	if (cbq_unlink_class(this))
		return -ENOENT;

	if (this->awake) {
		struct cbq_class *cl_prev = q->active[prio];
		do {
			cl = cl_prev->next_alive;
			if (cl == this) {
				cl_prev->next_alive = cl->next_alive;

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

				cl = cl->next_alive;
				cl->deficit += cl->quantum;
				break;
			}
		} while ((cl_prev = cl) != q->active[prio]);
	}

	--q->nclasses[prio];
	if (this->rquantum) {
		q->quanta[prio] -= this->rquantum;
		cbq_normalize_quanta(q, prio);
	}

	if (q->fallback_class == this)
		q->fallback_class = NULL;

	this->parent = NULL;
	this->borrow = NULL;
	this->root = this;
	this->qdisc = NULL;
	return 0;
}

static int cbq_graft(struct cbq_class *this, struct cbq_class *parent)
{
	struct cbq_class *cl, **clp;
	int prio = this->priority;
	struct cbq_sched_data *q = (struct cbq_sched_data*)this->qdisc->data;

	qdisc_reset(this->q);


	for (clp = &q->classes[prio]; (cl = *clp) != NULL; clp = &cl->next) {
		if (cl == this)
			return -EBUSY;
	}

	cl->next = NULL;
	*clp = cl;
	
	cl->parent = parent;
	cl->borrow = parent;
	cl->root = parent ? parent->root : cl;

	++q->nclasses[prio];
	if (this->rquantum) {
		q->quanta[prio] += this->rquantum;
		cbq_normalize_quanta(q, prio);
	}
	
	cbq_adjust_levels(this);

	return 0;
}


static void
cbq_reset(struct Qdisc* sch)
{
	struct cbq_sched_data *q = (struct cbq_sched_data *)sch->data;
	struct cbq_class *cl;
	int prio;

	q->activemask = 0;
	q->last_sent = NULL;
	if (q->wd_timer.function) {
		del_timer(&q->wd_timer);
		q->wd_timer.expires = 0;
		q->wd_timer.function = NULL;
	}
#ifdef CBQ_TOPLEVEL_SHARING
	q->toplevel = CBQ_MAXLEVEL;
#endif

	for (prio = 0; prio < CBQ_MAXPRIO; prio++) {
		q->active[prio] = NULL;
		
		for (cl = q->classes[prio]; cl; cl = cl->next) {
			qdisc_reset(cl->q);

			cl->next_alive = NULL;
			PSCHED_SET_PASTPERFECT(cl->undertime);
			cl->avgidle = 0;
			cl->deficit = 0;
			cl->awake = 0;
		}
	}
}

static void
cbq_destroy(struct Qdisc* sch)
{
	struct cbq_sched_data *q = (struct cbq_sched_data *)sch->data;
	struct cbq_class *cl, **clp;
	int prio;

	for (prio = 0; prio < CBQ_MAXPRIO; prio++) {
		struct cbq_class *cl_head = q->classes[prio];
		
		for (clp = &cl_head; (cl=*clp) != NULL; clp = &cl->next) {
			qdisc_destroy(cl->q);
			kfree(cl);
		}
	}
}

static int cbq_control(struct Qdisc *sch, void *arg)
{
	struct cbq_sched_data *q;

	q = (struct cbq_sched_data *)sch->data;

	/* Do attachment here. It is the last thing to do. */

	return -EINVAL;
}

static int cbq_init(struct Qdisc *sch, void *arg)
{
	struct cbq_sched_data *q;
	struct cbqctl *ctl = (struct cbqctl*)arg;

	q = (struct cbq_sched_data *)sch->data;
	init_timer(&q->wd_timer);
	q->wd_timer.data = (unsigned long)sch;
#ifdef CBQ_TOPLEVEL_SHARING
	q->toplevel = CBQ_MAXLEVEL;
#endif

	return 0;
}


struct Qdisc_ops cbq_ops =
{
	NULL,
	"cbq",
	0,
	sizeof(struct cbq_sched_data),
	cbq_enqueue,
	cbq_dequeue,
	cbq_reset,
	cbq_destroy,
	cbq_init,
	cbq_control,
};

#ifdef MODULE
int init_module(void)
{
	int err;

	/* Load once and never free it. */
	MOD_INC_USE_COUNT;

	err = register_qdisc(&cbq_ops);
	if (err)
		MOD_DEC_USE_COUNT;
	return err;
}

void cleanup_module(void) 
{
}
#endif