2 * net/sched/sch_gred.c Generic Random Early Detection queue.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version
8 * 2 of the License, or (at your option) any later version.
10 * Authors: J Hadi Salim (hadi@cyberus.ca) 1998-2002
12 * 991129: - Bug fix with grio mode
13 * - a better sing. AvgQ mode with Grio(WRED)
14 * - A finer grained VQ dequeue based on sugestion
18 * For all the glorious comments look at include/net/red.h
21 #include <linux/config.h>
22 #include <linux/module.h>
23 #include <linux/types.h>
24 #include <linux/kernel.h>
25 #include <linux/netdevice.h>
26 #include <linux/skbuff.h>
27 #include <net/pkt_sched.h>
30 #define GRED_DEF_PRIO (MAX_DPs / 2)
31 #define GRED_VQ_MASK (MAX_DPs - 1)
33 struct gred_sched_data
;
36 struct gred_sched_data
38 u32 limit
; /* HARD maximal queue length */
39 u32 DP
; /* the drop pramaters */
40 u32 bytesin
; /* bytes seen on virtualQ so far*/
41 u32 packetsin
; /* packets seen on virtualQ so far*/
42 u32 backlog
; /* bytes on the virtualQ */
43 u8 prio
; /* the prio of this vq */
45 struct red_parms parms
;
46 struct red_stats stats
;
56 struct gred_sched_data
*tab
[MAX_DPs
];
61 struct red_parms wred_set
;
64 static inline int gred_wred_mode(struct gred_sched
*table
)
66 return test_bit(GRED_WRED_MODE
, &table
->flags
);
69 static inline void gred_enable_wred_mode(struct gred_sched
*table
)
71 __set_bit(GRED_WRED_MODE
, &table
->flags
);
74 static inline void gred_disable_wred_mode(struct gred_sched
*table
)
76 __clear_bit(GRED_WRED_MODE
, &table
->flags
);
79 static inline int gred_rio_mode(struct gred_sched
*table
)
81 return test_bit(GRED_RIO_MODE
, &table
->flags
);
84 static inline void gred_enable_rio_mode(struct gred_sched
*table
)
86 __set_bit(GRED_RIO_MODE
, &table
->flags
);
89 static inline void gred_disable_rio_mode(struct gred_sched
*table
)
91 __clear_bit(GRED_RIO_MODE
, &table
->flags
);
94 static inline int gred_wred_mode_check(struct Qdisc
*sch
)
96 struct gred_sched
*table
= qdisc_priv(sch
);
99 /* Really ugly O(n^2) but shouldn't be necessary too frequent. */
100 for (i
= 0; i
< table
->DPs
; i
++) {
101 struct gred_sched_data
*q
= table
->tab
[i
];
107 for (n
= 0; n
< table
->DPs
; n
++)
108 if (table
->tab
[n
] && table
->tab
[n
] != q
&&
109 table
->tab
[n
]->prio
== q
->prio
)
116 static inline unsigned int gred_backlog(struct gred_sched
*table
,
117 struct gred_sched_data
*q
,
120 if (gred_wred_mode(table
))
121 return sch
->qstats
.backlog
;
126 static inline u16
tc_index_to_dp(struct sk_buff
*skb
)
128 return skb
->tc_index
& GRED_VQ_MASK
;
131 static inline void gred_load_wred_set(struct gred_sched
*table
,
132 struct gred_sched_data
*q
)
134 q
->parms
.qavg
= table
->wred_set
.qavg
;
135 q
->parms
.qidlestart
= table
->wred_set
.qidlestart
;
138 static inline void gred_store_wred_set(struct gred_sched
*table
,
139 struct gred_sched_data
*q
)
141 table
->wred_set
.qavg
= q
->parms
.qavg
;
144 static inline int gred_use_ecn(struct gred_sched
*t
)
146 return t
->red_flags
& TC_RED_ECN
;
149 static inline int gred_use_harddrop(struct gred_sched
*t
)
151 return t
->red_flags
& TC_RED_HARDDROP
;
154 static int gred_enqueue(struct sk_buff
*skb
, struct Qdisc
* sch
)
156 struct gred_sched_data
*q
=NULL
;
157 struct gred_sched
*t
= qdisc_priv(sch
);
158 unsigned long qavg
= 0;
159 u16 dp
= tc_index_to_dp(skb
);
161 if (dp
>= t
->DPs
|| (q
= t
->tab
[dp
]) == NULL
) {
164 if ((q
= t
->tab
[dp
]) == NULL
) {
165 /* Pass through packets not assigned to a DP
166 * if no default DP has been configured. This
167 * allows for DP flows to be left untouched.
169 if (skb_queue_len(&sch
->q
) < sch
->dev
->tx_queue_len
)
170 return qdisc_enqueue_tail(skb
, sch
);
175 /* fix tc_index? --could be controvesial but needed for
177 skb
->tc_index
= (skb
->tc_index
& ~GRED_VQ_MASK
) | dp
;
180 /* sum up all the qaves of prios <= to ours to get the new qave */
181 if (!gred_wred_mode(t
) && gred_rio_mode(t
)) {
184 for (i
= 0; i
< t
->DPs
; i
++) {
185 if (t
->tab
[i
] && t
->tab
[i
]->prio
< q
->prio
&&
186 !red_is_idling(&t
->tab
[i
]->parms
))
187 qavg
+=t
->tab
[i
]->parms
.qavg
;
193 q
->bytesin
+= skb
->len
;
195 if (gred_wred_mode(t
))
196 gred_load_wred_set(t
, q
);
198 q
->parms
.qavg
= red_calc_qavg(&q
->parms
, gred_backlog(t
, q
, sch
));
200 if (red_is_idling(&q
->parms
))
201 red_end_of_idle_period(&q
->parms
);
203 if (gred_wred_mode(t
))
204 gred_store_wred_set(t
, q
);
206 switch (red_action(&q
->parms
, q
->parms
.qavg
+ qavg
)) {
211 sch
->qstats
.overlimits
++;
212 if (!gred_use_ecn(t
) || !INET_ECN_set_ce(skb
)) {
213 q
->stats
.prob_drop
++;
214 goto congestion_drop
;
217 q
->stats
.prob_mark
++;
221 sch
->qstats
.overlimits
++;
222 if (gred_use_harddrop(t
) || !gred_use_ecn(t
) ||
223 !INET_ECN_set_ce(skb
)) {
224 q
->stats
.forced_drop
++;
225 goto congestion_drop
;
227 q
->stats
.forced_mark
++;
231 if (q
->backlog
+ skb
->len
<= q
->limit
) {
232 q
->backlog
+= skb
->len
;
233 return qdisc_enqueue_tail(skb
, sch
);
238 return qdisc_drop(skb
, sch
);
241 qdisc_drop(skb
, sch
);
245 static int gred_requeue(struct sk_buff
*skb
, struct Qdisc
* sch
)
247 struct gred_sched
*t
= qdisc_priv(sch
);
248 struct gred_sched_data
*q
;
249 u16 dp
= tc_index_to_dp(skb
);
251 if (dp
>= t
->DPs
|| (q
= t
->tab
[dp
]) == NULL
) {
253 printk(KERN_WARNING
"GRED: Unable to relocate VQ 0x%x "
254 "for requeue, screwing up backlog.\n",
255 tc_index_to_dp(skb
));
257 if (red_is_idling(&q
->parms
))
258 red_end_of_idle_period(&q
->parms
);
259 q
->backlog
+= skb
->len
;
262 return qdisc_requeue(skb
, sch
);
265 static struct sk_buff
*gred_dequeue(struct Qdisc
* sch
)
268 struct gred_sched
*t
= qdisc_priv(sch
);
270 skb
= qdisc_dequeue_head(sch
);
273 struct gred_sched_data
*q
;
274 u16 dp
= tc_index_to_dp(skb
);
276 if (dp
>= t
->DPs
|| (q
= t
->tab
[dp
]) == NULL
) {
278 printk(KERN_WARNING
"GRED: Unable to relocate "
279 "VQ 0x%x after dequeue, screwing up "
280 "backlog.\n", tc_index_to_dp(skb
));
282 q
->backlog
-= skb
->len
;
284 if (!q
->backlog
&& !gred_wred_mode(t
))
285 red_start_of_idle_period(&q
->parms
);
291 if (gred_wred_mode(t
) && !red_is_idling(&t
->wred_set
))
292 red_start_of_idle_period(&t
->wred_set
);
297 static unsigned int gred_drop(struct Qdisc
* sch
)
300 struct gred_sched
*t
= qdisc_priv(sch
);
302 skb
= qdisc_dequeue_tail(sch
);
304 unsigned int len
= skb
->len
;
305 struct gred_sched_data
*q
;
306 u16 dp
= tc_index_to_dp(skb
);
308 if (dp
>= t
->DPs
|| (q
= t
->tab
[dp
]) == NULL
) {
310 printk(KERN_WARNING
"GRED: Unable to relocate "
311 "VQ 0x%x while dropping, screwing up "
312 "backlog.\n", tc_index_to_dp(skb
));
317 if (!q
->backlog
&& !gred_wred_mode(t
))
318 red_start_of_idle_period(&q
->parms
);
321 qdisc_drop(skb
, sch
);
325 if (gred_wred_mode(t
) && !red_is_idling(&t
->wred_set
))
326 red_start_of_idle_period(&t
->wred_set
);
332 static void gred_reset(struct Qdisc
* sch
)
335 struct gred_sched
*t
= qdisc_priv(sch
);
337 qdisc_reset_queue(sch
);
339 for (i
= 0; i
< t
->DPs
; i
++) {
340 struct gred_sched_data
*q
= t
->tab
[i
];
345 red_restart(&q
->parms
);
350 static inline void gred_destroy_vq(struct gred_sched_data
*q
)
355 static inline int gred_change_table_def(struct Qdisc
*sch
, struct rtattr
*dps
)
357 struct gred_sched
*table
= qdisc_priv(sch
);
358 struct tc_gred_sopt
*sopt
;
361 if (dps
== NULL
|| RTA_PAYLOAD(dps
) < sizeof(*sopt
))
364 sopt
= RTA_DATA(dps
);
366 if (sopt
->DPs
> MAX_DPs
|| sopt
->DPs
== 0 || sopt
->def_DP
>= sopt
->DPs
)
370 table
->DPs
= sopt
->DPs
;
371 table
->def
= sopt
->def_DP
;
372 table
->red_flags
= sopt
->flags
;
375 * Every entry point to GRED is synchronized with the above code
376 * and the DP is checked against DPs, i.e. shadowed VQs can no
377 * longer be found so we can unlock right here.
379 sch_tree_unlock(sch
);
382 gred_enable_rio_mode(table
);
383 gred_disable_wred_mode(table
);
384 if (gred_wred_mode_check(sch
))
385 gred_enable_wred_mode(table
);
387 gred_disable_rio_mode(table
);
388 gred_disable_wred_mode(table
);
391 for (i
= table
->DPs
; i
< MAX_DPs
; i
++) {
393 printk(KERN_WARNING
"GRED: Warning: Destroying "
394 "shadowed VQ 0x%x\n", i
);
395 gred_destroy_vq(table
->tab
[i
]);
396 table
->tab
[i
] = NULL
;
403 static inline int gred_change_vq(struct Qdisc
*sch
, int dp
,
404 struct tc_gred_qopt
*ctl
, int prio
, u8
*stab
)
406 struct gred_sched
*table
= qdisc_priv(sch
);
407 struct gred_sched_data
*q
;
409 if (table
->tab
[dp
] == NULL
) {
410 table
->tab
[dp
] = kmalloc(sizeof(*q
), GFP_KERNEL
);
411 if (table
->tab
[dp
] == NULL
)
413 memset(table
->tab
[dp
], 0, sizeof(*q
));
419 q
->limit
= ctl
->limit
;
422 red_end_of_idle_period(&q
->parms
);
424 red_set_parms(&q
->parms
,
425 ctl
->qth_min
, ctl
->qth_max
, ctl
->Wlog
, ctl
->Plog
,
426 ctl
->Scell_log
, stab
);
431 static int gred_change(struct Qdisc
*sch
, struct rtattr
*opt
)
433 struct gred_sched
*table
= qdisc_priv(sch
);
434 struct tc_gred_qopt
*ctl
;
435 struct rtattr
*tb
[TCA_GRED_MAX
];
436 int err
= -EINVAL
, prio
= GRED_DEF_PRIO
;
439 if (opt
== NULL
|| rtattr_parse_nested(tb
, TCA_GRED_MAX
, opt
))
442 if (tb
[TCA_GRED_PARMS
-1] == NULL
&& tb
[TCA_GRED_STAB
-1] == NULL
)
443 return gred_change_table_def(sch
, opt
);
445 if (tb
[TCA_GRED_PARMS
-1] == NULL
||
446 RTA_PAYLOAD(tb
[TCA_GRED_PARMS
-1]) < sizeof(*ctl
) ||
447 tb
[TCA_GRED_STAB
-1] == NULL
||
448 RTA_PAYLOAD(tb
[TCA_GRED_STAB
-1]) < 256)
451 ctl
= RTA_DATA(tb
[TCA_GRED_PARMS
-1]);
452 stab
= RTA_DATA(tb
[TCA_GRED_STAB
-1]);
454 if (ctl
->DP
>= table
->DPs
)
457 if (gred_rio_mode(table
)) {
458 if (ctl
->prio
== 0) {
459 int def_prio
= GRED_DEF_PRIO
;
461 if (table
->tab
[table
->def
])
462 def_prio
= table
->tab
[table
->def
]->prio
;
464 printk(KERN_DEBUG
"GRED: DP %u does not have a prio "
465 "setting default to %d\n", ctl
->DP
, def_prio
);
474 err
= gred_change_vq(sch
, ctl
->DP
, ctl
, prio
, stab
);
478 if (gred_rio_mode(table
)) {
479 gred_disable_wred_mode(table
);
480 if (gred_wred_mode_check(sch
))
481 gred_enable_wred_mode(table
);
487 sch_tree_unlock(sch
);
492 static int gred_init(struct Qdisc
*sch
, struct rtattr
*opt
)
494 struct rtattr
*tb
[TCA_GRED_MAX
];
496 if (opt
== NULL
|| rtattr_parse_nested(tb
, TCA_GRED_MAX
, opt
))
499 if (tb
[TCA_GRED_PARMS
-1] || tb
[TCA_GRED_STAB
-1])
502 return gred_change_table_def(sch
, tb
[TCA_GRED_DPS
-1]);
505 static int gred_dump(struct Qdisc
*sch
, struct sk_buff
*skb
)
507 struct gred_sched
*table
= qdisc_priv(sch
);
508 struct rtattr
*parms
, *opts
= NULL
;
510 struct tc_gred_sopt sopt
= {
512 .def_DP
= table
->def
,
513 .grio
= gred_rio_mode(table
),
514 .flags
= table
->red_flags
,
517 opts
= RTA_NEST(skb
, TCA_OPTIONS
);
518 RTA_PUT(skb
, TCA_GRED_DPS
, sizeof(sopt
), &sopt
);
519 parms
= RTA_NEST(skb
, TCA_GRED_PARMS
);
521 for (i
= 0; i
< MAX_DPs
; i
++) {
522 struct gred_sched_data
*q
= table
->tab
[i
];
523 struct tc_gred_qopt opt
;
525 memset(&opt
, 0, sizeof(opt
));
528 /* hack -- fix at some point with proper message
529 This is how we indicate to tc that there is no VQ
532 opt
.DP
= MAX_DPs
+ i
;
536 opt
.limit
= q
->limit
;
538 opt
.backlog
= q
->backlog
;
540 opt
.qth_min
= q
->parms
.qth_min
>> q
->parms
.Wlog
;
541 opt
.qth_max
= q
->parms
.qth_max
>> q
->parms
.Wlog
;
542 opt
.Wlog
= q
->parms
.Wlog
;
543 opt
.Plog
= q
->parms
.Plog
;
544 opt
.Scell_log
= q
->parms
.Scell_log
;
545 opt
.other
= q
->stats
.other
;
546 opt
.early
= q
->stats
.prob_drop
;
547 opt
.forced
= q
->stats
.forced_drop
;
548 opt
.pdrop
= q
->stats
.pdrop
;
549 opt
.packets
= q
->packetsin
;
550 opt
.bytesin
= q
->bytesin
;
552 if (gred_wred_mode(table
)) {
553 q
->parms
.qidlestart
=
554 table
->tab
[table
->def
]->parms
.qidlestart
;
555 q
->parms
.qavg
= table
->tab
[table
->def
]->parms
.qavg
;
558 opt
.qave
= red_calc_qavg(&q
->parms
, q
->parms
.qavg
);
561 RTA_APPEND(skb
, sizeof(opt
), &opt
);
564 RTA_NEST_END(skb
, parms
);
566 return RTA_NEST_END(skb
, opts
);
569 return RTA_NEST_CANCEL(skb
, opts
);
572 static void gred_destroy(struct Qdisc
*sch
)
574 struct gred_sched
*table
= qdisc_priv(sch
);
577 for (i
= 0; i
< table
->DPs
; i
++) {
579 gred_destroy_vq(table
->tab
[i
]);
583 static struct Qdisc_ops gred_qdisc_ops
= {
585 .priv_size
= sizeof(struct gred_sched
),
586 .enqueue
= gred_enqueue
,
587 .dequeue
= gred_dequeue
,
588 .requeue
= gred_requeue
,
592 .destroy
= gred_destroy
,
593 .change
= gred_change
,
595 .owner
= THIS_MODULE
,
598 static int __init
gred_module_init(void)
600 return register_qdisc(&gred_qdisc_ops
);
603 static void __exit
gred_module_exit(void)
605 unregister_qdisc(&gred_qdisc_ops
);
608 module_init(gred_module_init
)
609 module_exit(gred_module_exit
)
611 MODULE_LICENSE("GPL");