Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * net/sched/sch_gred.c Generic Random Early Detection queue. |
| 3 | * |
| 4 | * |
| 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. |
| 9 | * |
| 10 | * Authors: J Hadi Salim (hadi@cyberus.ca) 1998-2002 |
| 11 | * |
| 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 |
| 15 | * from Ren Liu |
| 16 | * - More error checks |
| 17 | * |
| 18 | * |
| 19 | * |
| 20 | * For all the glorious comments look at Alexey's sch_red.c |
| 21 | */ |
| 22 | |
| 23 | #include <linux/config.h> |
| 24 | #include <linux/module.h> |
| 25 | #include <asm/uaccess.h> |
| 26 | #include <asm/system.h> |
| 27 | #include <linux/bitops.h> |
| 28 | #include <linux/types.h> |
| 29 | #include <linux/kernel.h> |
| 30 | #include <linux/sched.h> |
| 31 | #include <linux/string.h> |
| 32 | #include <linux/mm.h> |
| 33 | #include <linux/socket.h> |
| 34 | #include <linux/sockios.h> |
| 35 | #include <linux/in.h> |
| 36 | #include <linux/errno.h> |
| 37 | #include <linux/interrupt.h> |
| 38 | #include <linux/if_ether.h> |
| 39 | #include <linux/inet.h> |
| 40 | #include <linux/netdevice.h> |
| 41 | #include <linux/etherdevice.h> |
| 42 | #include <linux/notifier.h> |
| 43 | #include <net/ip.h> |
| 44 | #include <net/route.h> |
| 45 | #include <linux/skbuff.h> |
| 46 | #include <net/sock.h> |
| 47 | #include <net/pkt_sched.h> |
Thomas Graf | 22b3342 | 2005-11-05 21:14:16 +0100 | [diff] [blame] | 48 | #include <net/red.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
| 50 | #if 1 /* control */ |
| 51 | #define DPRINTK(format,args...) printk(KERN_DEBUG format,##args) |
| 52 | #else |
| 53 | #define DPRINTK(format,args...) |
| 54 | #endif |
| 55 | |
| 56 | #if 0 /* data */ |
| 57 | #define D2PRINTK(format,args...) printk(KERN_DEBUG format,##args) |
| 58 | #else |
| 59 | #define D2PRINTK(format,args...) |
| 60 | #endif |
| 61 | |
Thomas Graf | f62d6b9 | 2005-11-05 21:14:15 +0100 | [diff] [blame] | 62 | #define GRED_DEF_PRIO (MAX_DPs / 2) |
Thomas Graf | 716a1b4 | 2005-11-05 21:14:20 +0100 | [diff] [blame^] | 63 | #define GRED_VQ_MASK (MAX_DPs - 1) |
Thomas Graf | f62d6b9 | 2005-11-05 21:14:15 +0100 | [diff] [blame] | 64 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | struct gred_sched_data; |
| 66 | struct gred_sched; |
| 67 | |
| 68 | struct gred_sched_data |
| 69 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | u32 limit; /* HARD maximal queue length */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | u32 DP; /* the drop pramaters */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | u32 bytesin; /* bytes seen on virtualQ so far*/ |
| 73 | u32 packetsin; /* packets seen on virtualQ so far*/ |
| 74 | u32 backlog; /* bytes on the virtualQ */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | u8 prio; /* the prio of this vq */ |
| 76 | |
Thomas Graf | 22b3342 | 2005-11-05 21:14:16 +0100 | [diff] [blame] | 77 | struct red_parms parms; |
| 78 | struct red_stats stats; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | }; |
| 80 | |
Thomas Graf | dea3f62 | 2005-11-05 21:14:09 +0100 | [diff] [blame] | 81 | enum { |
| 82 | GRED_WRED_MODE = 1, |
Thomas Graf | d6fd4e9 | 2005-11-05 21:14:10 +0100 | [diff] [blame] | 83 | GRED_RIO_MODE, |
Thomas Graf | dea3f62 | 2005-11-05 21:14:09 +0100 | [diff] [blame] | 84 | }; |
| 85 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | struct gred_sched |
| 87 | { |
| 88 | struct gred_sched_data *tab[MAX_DPs]; |
Thomas Graf | dea3f62 | 2005-11-05 21:14:09 +0100 | [diff] [blame] | 89 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | u32 DPs; |
| 91 | u32 def; |
| 92 | u8 initd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | }; |
| 94 | |
Thomas Graf | dea3f62 | 2005-11-05 21:14:09 +0100 | [diff] [blame] | 95 | static inline int gred_wred_mode(struct gred_sched *table) |
| 96 | { |
| 97 | return test_bit(GRED_WRED_MODE, &table->flags); |
| 98 | } |
| 99 | |
| 100 | static inline void gred_enable_wred_mode(struct gred_sched *table) |
| 101 | { |
| 102 | __set_bit(GRED_WRED_MODE, &table->flags); |
| 103 | } |
| 104 | |
| 105 | static inline void gred_disable_wred_mode(struct gred_sched *table) |
| 106 | { |
| 107 | __clear_bit(GRED_WRED_MODE, &table->flags); |
| 108 | } |
| 109 | |
Thomas Graf | d6fd4e9 | 2005-11-05 21:14:10 +0100 | [diff] [blame] | 110 | static inline int gred_rio_mode(struct gred_sched *table) |
| 111 | { |
| 112 | return test_bit(GRED_RIO_MODE, &table->flags); |
| 113 | } |
| 114 | |
| 115 | static inline void gred_enable_rio_mode(struct gred_sched *table) |
| 116 | { |
| 117 | __set_bit(GRED_RIO_MODE, &table->flags); |
| 118 | } |
| 119 | |
| 120 | static inline void gred_disable_rio_mode(struct gred_sched *table) |
| 121 | { |
| 122 | __clear_bit(GRED_RIO_MODE, &table->flags); |
| 123 | } |
| 124 | |
Thomas Graf | dea3f62 | 2005-11-05 21:14:09 +0100 | [diff] [blame] | 125 | static inline int gred_wred_mode_check(struct Qdisc *sch) |
| 126 | { |
| 127 | struct gred_sched *table = qdisc_priv(sch); |
| 128 | int i; |
| 129 | |
| 130 | /* Really ugly O(n^2) but shouldn't be necessary too frequent. */ |
| 131 | for (i = 0; i < table->DPs; i++) { |
| 132 | struct gred_sched_data *q = table->tab[i]; |
| 133 | int n; |
| 134 | |
| 135 | if (q == NULL) |
| 136 | continue; |
| 137 | |
| 138 | for (n = 0; n < table->DPs; n++) |
| 139 | if (table->tab[n] && table->tab[n] != q && |
| 140 | table->tab[n]->prio == q->prio) |
| 141 | return 1; |
| 142 | } |
| 143 | |
| 144 | return 0; |
| 145 | } |
| 146 | |
Thomas Graf | 22b3342 | 2005-11-05 21:14:16 +0100 | [diff] [blame] | 147 | static inline unsigned int gred_backlog(struct gred_sched *table, |
| 148 | struct gred_sched_data *q, |
| 149 | struct Qdisc *sch) |
| 150 | { |
| 151 | if (gred_wred_mode(table)) |
| 152 | return sch->qstats.backlog; |
| 153 | else |
| 154 | return q->backlog; |
| 155 | } |
| 156 | |
Thomas Graf | 716a1b4 | 2005-11-05 21:14:20 +0100 | [diff] [blame^] | 157 | static inline u16 tc_index_to_dp(struct sk_buff *skb) |
| 158 | { |
| 159 | return skb->tc_index & GRED_VQ_MASK; |
| 160 | } |
| 161 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | static int |
| 163 | gred_enqueue(struct sk_buff *skb, struct Qdisc* sch) |
| 164 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | struct gred_sched_data *q=NULL; |
| 166 | struct gred_sched *t= qdisc_priv(sch); |
Thomas Graf | 22b3342 | 2005-11-05 21:14:16 +0100 | [diff] [blame] | 167 | unsigned long qavg = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | int i=0; |
Thomas Graf | 716a1b4 | 2005-11-05 21:14:20 +0100 | [diff] [blame^] | 169 | u16 dp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | |
| 171 | if (!t->initd && skb_queue_len(&sch->q) < (sch->dev->tx_queue_len ? : 1)) { |
| 172 | D2PRINTK("NO GRED Queues setup yet! Enqueued anyway\n"); |
| 173 | goto do_enqueue; |
| 174 | } |
| 175 | |
Thomas Graf | 716a1b4 | 2005-11-05 21:14:20 +0100 | [diff] [blame^] | 176 | dp = tc_index_to_dp(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | |
Thomas Graf | 716a1b4 | 2005-11-05 21:14:20 +0100 | [diff] [blame^] | 178 | if (dp >= t->DPs || (q = t->tab[dp]) == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | printk("GRED: setting to default (%d)\n ",t->def); |
| 180 | if (!(q=t->tab[t->def])) { |
| 181 | DPRINTK("GRED: setting to default FAILED! dropping!! " |
| 182 | "(%d)\n ", t->def); |
| 183 | goto drop; |
| 184 | } |
| 185 | /* fix tc_index? --could be controvesial but needed for |
| 186 | requeueing */ |
Thomas Graf | 716a1b4 | 2005-11-05 21:14:20 +0100 | [diff] [blame^] | 187 | skb->tc_index=(skb->tc_index & ~GRED_VQ_MASK) | t->def; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | D2PRINTK("gred_enqueue virtualQ 0x%x classid %x backlog %d " |
| 191 | "general backlog %d\n",skb->tc_index&0xf,sch->handle,q->backlog, |
| 192 | sch->qstats.backlog); |
| 193 | /* sum up all the qaves of prios <= to ours to get the new qave*/ |
Thomas Graf | d6fd4e9 | 2005-11-05 21:14:10 +0100 | [diff] [blame] | 194 | if (!gred_wred_mode(t) && gred_rio_mode(t)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | for (i=0;i<t->DPs;i++) { |
| 196 | if ((!t->tab[i]) || (i==q->DP)) |
| 197 | continue; |
| 198 | |
Thomas Graf | 22b3342 | 2005-11-05 21:14:16 +0100 | [diff] [blame] | 199 | if (t->tab[i]->prio < q->prio && |
| 200 | !red_is_idling(&t->tab[i]->parms)) |
| 201 | qavg +=t->tab[i]->parms.qavg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | } |
| 205 | |
| 206 | q->packetsin++; |
| 207 | q->bytesin+=skb->len; |
| 208 | |
Thomas Graf | dea3f62 | 2005-11-05 21:14:09 +0100 | [diff] [blame] | 209 | if (gred_wred_mode(t)) { |
Thomas Graf | 22b3342 | 2005-11-05 21:14:16 +0100 | [diff] [blame] | 210 | qavg = 0; |
| 211 | q->parms.qavg = t->tab[t->def]->parms.qavg; |
| 212 | q->parms.qidlestart = t->tab[t->def]->parms.qidlestart; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Thomas Graf | 22b3342 | 2005-11-05 21:14:16 +0100 | [diff] [blame] | 215 | q->parms.qavg = red_calc_qavg(&q->parms, gred_backlog(t, q, sch)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | |
Thomas Graf | 22b3342 | 2005-11-05 21:14:16 +0100 | [diff] [blame] | 217 | if (red_is_idling(&q->parms)) |
| 218 | red_end_of_idle_period(&q->parms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | |
Thomas Graf | dea3f62 | 2005-11-05 21:14:09 +0100 | [diff] [blame] | 220 | if (gred_wred_mode(t)) |
Thomas Graf | 22b3342 | 2005-11-05 21:14:16 +0100 | [diff] [blame] | 221 | t->tab[t->def]->parms.qavg = q->parms.qavg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | |
Thomas Graf | 22b3342 | 2005-11-05 21:14:16 +0100 | [diff] [blame] | 223 | switch (red_action(&q->parms, q->parms.qavg + qavg)) { |
| 224 | case RED_DONT_MARK: |
| 225 | break; |
| 226 | |
| 227 | case RED_PROB_MARK: |
| 228 | sch->qstats.overlimits++; |
| 229 | q->stats.prob_drop++; |
Thomas Graf | c3b553c | 2005-11-05 21:14:18 +0100 | [diff] [blame] | 230 | goto congestion_drop; |
Thomas Graf | 22b3342 | 2005-11-05 21:14:16 +0100 | [diff] [blame] | 231 | |
| 232 | case RED_HARD_MARK: |
| 233 | sch->qstats.overlimits++; |
| 234 | q->stats.forced_drop++; |
Thomas Graf | c3b553c | 2005-11-05 21:14:18 +0100 | [diff] [blame] | 235 | goto congestion_drop; |
Thomas Graf | 22b3342 | 2005-11-05 21:14:16 +0100 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | if (q->backlog + skb->len <= q->limit) { |
| 239 | q->backlog += skb->len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | do_enqueue: |
Thomas Graf | edf7a7b | 2005-11-05 21:14:19 +0100 | [diff] [blame] | 241 | return qdisc_enqueue_tail(skb, sch); |
Thomas Graf | 22b3342 | 2005-11-05 21:14:16 +0100 | [diff] [blame] | 242 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | |
Thomas Graf | 22b3342 | 2005-11-05 21:14:16 +0100 | [diff] [blame] | 244 | q->stats.pdrop++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | drop: |
Thomas Graf | edf7a7b | 2005-11-05 21:14:19 +0100 | [diff] [blame] | 246 | return qdisc_drop(skb, sch); |
Thomas Graf | c3b553c | 2005-11-05 21:14:18 +0100 | [diff] [blame] | 247 | |
| 248 | congestion_drop: |
Thomas Graf | edf7a7b | 2005-11-05 21:14:19 +0100 | [diff] [blame] | 249 | qdisc_drop(skb, sch); |
Thomas Graf | c3b553c | 2005-11-05 21:14:18 +0100 | [diff] [blame] | 250 | return NET_XMIT_CN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | static int |
| 254 | gred_requeue(struct sk_buff *skb, struct Qdisc* sch) |
| 255 | { |
Thomas Graf | 716a1b4 | 2005-11-05 21:14:20 +0100 | [diff] [blame^] | 256 | struct gred_sched *t = qdisc_priv(sch); |
| 257 | struct gred_sched_data *q = t->tab[tc_index_to_dp(skb)]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | /* error checking here -- probably unnecessary */ |
Thomas Graf | 22b3342 | 2005-11-05 21:14:16 +0100 | [diff] [blame] | 259 | |
| 260 | if (red_is_idling(&q->parms)) |
| 261 | red_end_of_idle_period(&q->parms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | q->backlog += skb->len; |
Thomas Graf | edf7a7b | 2005-11-05 21:14:19 +0100 | [diff] [blame] | 264 | return qdisc_requeue(skb, sch); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | static struct sk_buff * |
| 268 | gred_dequeue(struct Qdisc* sch) |
| 269 | { |
| 270 | struct sk_buff *skb; |
| 271 | struct gred_sched_data *q; |
| 272 | struct gred_sched *t= qdisc_priv(sch); |
| 273 | |
Thomas Graf | edf7a7b | 2005-11-05 21:14:19 +0100 | [diff] [blame] | 274 | skb = qdisc_dequeue_head(sch); |
| 275 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | if (skb) { |
Thomas Graf | 716a1b4 | 2005-11-05 21:14:20 +0100 | [diff] [blame^] | 277 | q = t->tab[tc_index_to_dp(skb)]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | if (q) { |
| 279 | q->backlog -= skb->len; |
Thomas Graf | dea3f62 | 2005-11-05 21:14:09 +0100 | [diff] [blame] | 280 | if (!q->backlog && !gred_wred_mode(t)) |
Thomas Graf | 22b3342 | 2005-11-05 21:14:16 +0100 | [diff] [blame] | 281 | red_start_of_idle_period(&q->parms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | } else { |
Thomas Graf | 716a1b4 | 2005-11-05 21:14:20 +0100 | [diff] [blame^] | 283 | D2PRINTK("gred_dequeue: skb has bad tcindex %x\n", |
| 284 | tc_index_to_dp(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | } |
| 286 | return skb; |
| 287 | } |
| 288 | |
Thomas Graf | dea3f62 | 2005-11-05 21:14:09 +0100 | [diff] [blame] | 289 | if (gred_wred_mode(t)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | q= t->tab[t->def]; |
| 291 | if (!q) |
| 292 | D2PRINTK("no default VQ set: Results will be " |
| 293 | "screwed up\n"); |
| 294 | else |
Thomas Graf | 22b3342 | 2005-11-05 21:14:16 +0100 | [diff] [blame] | 295 | red_start_of_idle_period(&q->parms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | return NULL; |
| 299 | } |
| 300 | |
| 301 | static unsigned int gred_drop(struct Qdisc* sch) |
| 302 | { |
| 303 | struct sk_buff *skb; |
| 304 | |
| 305 | struct gred_sched_data *q; |
| 306 | struct gred_sched *t= qdisc_priv(sch); |
| 307 | |
Thomas Graf | edf7a7b | 2005-11-05 21:14:19 +0100 | [diff] [blame] | 308 | skb = qdisc_dequeue_tail(sch); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | if (skb) { |
| 310 | unsigned int len = skb->len; |
Thomas Graf | 716a1b4 | 2005-11-05 21:14:20 +0100 | [diff] [blame^] | 311 | q = t->tab[tc_index_to_dp(skb)]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | if (q) { |
| 313 | q->backlog -= len; |
Thomas Graf | 22b3342 | 2005-11-05 21:14:16 +0100 | [diff] [blame] | 314 | q->stats.other++; |
Thomas Graf | dea3f62 | 2005-11-05 21:14:09 +0100 | [diff] [blame] | 315 | if (!q->backlog && !gred_wred_mode(t)) |
Thomas Graf | 22b3342 | 2005-11-05 21:14:16 +0100 | [diff] [blame] | 316 | red_start_of_idle_period(&q->parms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | } else { |
Thomas Graf | 716a1b4 | 2005-11-05 21:14:20 +0100 | [diff] [blame^] | 318 | D2PRINTK("gred_dequeue: skb has bad tcindex %x\n", |
| 319 | tc_index_to_dp(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | } |
| 321 | |
Thomas Graf | edf7a7b | 2005-11-05 21:14:19 +0100 | [diff] [blame] | 322 | qdisc_drop(skb, sch); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | return len; |
| 324 | } |
| 325 | |
| 326 | q=t->tab[t->def]; |
| 327 | if (!q) { |
| 328 | D2PRINTK("no default VQ set: Results might be screwed up\n"); |
| 329 | return 0; |
| 330 | } |
| 331 | |
Thomas Graf | 22b3342 | 2005-11-05 21:14:16 +0100 | [diff] [blame] | 332 | red_start_of_idle_period(&q->parms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | return 0; |
| 334 | |
| 335 | } |
| 336 | |
| 337 | static void gred_reset(struct Qdisc* sch) |
| 338 | { |
| 339 | int i; |
| 340 | struct gred_sched_data *q; |
| 341 | struct gred_sched *t= qdisc_priv(sch); |
| 342 | |
Thomas Graf | edf7a7b | 2005-11-05 21:14:19 +0100 | [diff] [blame] | 343 | qdisc_reset_queue(sch); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | |
| 345 | for (i=0;i<t->DPs;i++) { |
| 346 | q= t->tab[i]; |
| 347 | if (!q) |
| 348 | continue; |
Thomas Graf | 22b3342 | 2005-11-05 21:14:16 +0100 | [diff] [blame] | 349 | red_restart(&q->parms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | q->backlog = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | } |
| 352 | } |
| 353 | |
Thomas Graf | 6639607 | 2005-11-05 21:14:13 +0100 | [diff] [blame] | 354 | static inline void gred_destroy_vq(struct gred_sched_data *q) |
| 355 | { |
| 356 | kfree(q); |
| 357 | } |
| 358 | |
| 359 | static inline int gred_change_table_def(struct Qdisc *sch, struct rtattr *dps) |
| 360 | { |
| 361 | struct gred_sched *table = qdisc_priv(sch); |
| 362 | struct tc_gred_sopt *sopt; |
| 363 | int i; |
| 364 | |
| 365 | if (dps == NULL || RTA_PAYLOAD(dps) < sizeof(*sopt)) |
| 366 | return -EINVAL; |
| 367 | |
| 368 | sopt = RTA_DATA(dps); |
| 369 | |
| 370 | if (sopt->DPs > MAX_DPs || sopt->DPs == 0 || sopt->def_DP >= sopt->DPs) |
| 371 | return -EINVAL; |
| 372 | |
| 373 | sch_tree_lock(sch); |
| 374 | table->DPs = sopt->DPs; |
| 375 | table->def = sopt->def_DP; |
| 376 | |
| 377 | /* |
| 378 | * Every entry point to GRED is synchronized with the above code |
| 379 | * and the DP is checked against DPs, i.e. shadowed VQs can no |
| 380 | * longer be found so we can unlock right here. |
| 381 | */ |
| 382 | sch_tree_unlock(sch); |
| 383 | |
| 384 | if (sopt->grio) { |
| 385 | gred_enable_rio_mode(table); |
| 386 | gred_disable_wred_mode(table); |
| 387 | if (gred_wred_mode_check(sch)) |
| 388 | gred_enable_wred_mode(table); |
| 389 | } else { |
| 390 | gred_disable_rio_mode(table); |
| 391 | gred_disable_wred_mode(table); |
| 392 | } |
| 393 | |
| 394 | for (i = table->DPs; i < MAX_DPs; i++) { |
| 395 | if (table->tab[i]) { |
| 396 | printk(KERN_WARNING "GRED: Warning: Destroying " |
| 397 | "shadowed VQ 0x%x\n", i); |
| 398 | gred_destroy_vq(table->tab[i]); |
| 399 | table->tab[i] = NULL; |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | table->initd = 0; |
| 404 | |
| 405 | return 0; |
| 406 | } |
| 407 | |
Thomas Graf | f62d6b9 | 2005-11-05 21:14:15 +0100 | [diff] [blame] | 408 | static inline int gred_change_vq(struct Qdisc *sch, int dp, |
| 409 | struct tc_gred_qopt *ctl, int prio, u8 *stab) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | { |
| 411 | struct gred_sched *table = qdisc_priv(sch); |
| 412 | struct gred_sched_data *q; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | |
Thomas Graf | f62d6b9 | 2005-11-05 21:14:15 +0100 | [diff] [blame] | 414 | if (table->tab[dp] == NULL) { |
| 415 | table->tab[dp] = kmalloc(sizeof(*q), GFP_KERNEL); |
| 416 | if (table->tab[dp] == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | return -ENOMEM; |
Thomas Graf | f62d6b9 | 2005-11-05 21:14:15 +0100 | [diff] [blame] | 418 | memset(table->tab[dp], 0, sizeof(*q)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | } |
| 420 | |
Thomas Graf | f62d6b9 | 2005-11-05 21:14:15 +0100 | [diff] [blame] | 421 | q = table->tab[dp]; |
| 422 | q->DP = dp; |
| 423 | q->prio = prio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | q->limit = ctl->limit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | |
Thomas Graf | 22b3342 | 2005-11-05 21:14:16 +0100 | [diff] [blame] | 426 | if (q->backlog == 0) |
| 427 | red_end_of_idle_period(&q->parms); |
| 428 | |
| 429 | red_set_parms(&q->parms, |
| 430 | ctl->qth_min, ctl->qth_max, ctl->Wlog, ctl->Plog, |
| 431 | ctl->Scell_log, stab); |
| 432 | |
Thomas Graf | f62d6b9 | 2005-11-05 21:14:15 +0100 | [diff] [blame] | 433 | return 0; |
| 434 | } |
| 435 | |
| 436 | static int gred_change(struct Qdisc *sch, struct rtattr *opt) |
| 437 | { |
| 438 | struct gred_sched *table = qdisc_priv(sch); |
| 439 | struct tc_gred_qopt *ctl; |
| 440 | struct rtattr *tb[TCA_GRED_MAX]; |
| 441 | int err = -EINVAL, prio = GRED_DEF_PRIO; |
| 442 | u8 *stab; |
| 443 | |
| 444 | if (opt == NULL || rtattr_parse_nested(tb, TCA_GRED_MAX, opt)) |
| 445 | return -EINVAL; |
| 446 | |
| 447 | if (tb[TCA_GRED_PARMS-1] == NULL && tb[TCA_GRED_STAB-1] == NULL) |
| 448 | return gred_change_table_def(sch, opt); |
| 449 | |
| 450 | if (tb[TCA_GRED_PARMS-1] == NULL || |
| 451 | RTA_PAYLOAD(tb[TCA_GRED_PARMS-1]) < sizeof(*ctl) || |
| 452 | tb[TCA_GRED_STAB-1] == NULL || |
| 453 | RTA_PAYLOAD(tb[TCA_GRED_STAB-1]) < 256) |
| 454 | return -EINVAL; |
| 455 | |
| 456 | ctl = RTA_DATA(tb[TCA_GRED_PARMS-1]); |
| 457 | stab = RTA_DATA(tb[TCA_GRED_STAB-1]); |
| 458 | |
| 459 | if (ctl->DP >= table->DPs) |
| 460 | goto errout; |
| 461 | |
| 462 | if (gred_rio_mode(table)) { |
| 463 | if (ctl->prio == 0) { |
| 464 | int def_prio = GRED_DEF_PRIO; |
| 465 | |
| 466 | if (table->tab[table->def]) |
| 467 | def_prio = table->tab[table->def]->prio; |
| 468 | |
| 469 | printk(KERN_DEBUG "GRED: DP %u does not have a prio " |
| 470 | "setting default to %d\n", ctl->DP, def_prio); |
| 471 | |
| 472 | prio = def_prio; |
| 473 | } else |
| 474 | prio = ctl->prio; |
| 475 | } |
| 476 | |
| 477 | sch_tree_lock(sch); |
| 478 | |
| 479 | err = gred_change_vq(sch, ctl->DP, ctl, prio, stab); |
| 480 | if (err < 0) |
| 481 | goto errout_locked; |
| 482 | |
| 483 | if (table->tab[table->def] == NULL) { |
| 484 | if (gred_rio_mode(table)) |
| 485 | prio = table->tab[ctl->DP]->prio; |
| 486 | |
| 487 | err = gred_change_vq(sch, table->def, ctl, prio, stab); |
| 488 | if (err < 0) |
| 489 | goto errout_locked; |
| 490 | } |
| 491 | |
| 492 | table->initd = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | |
Thomas Graf | d6fd4e9 | 2005-11-05 21:14:10 +0100 | [diff] [blame] | 494 | if (gred_rio_mode(table)) { |
Thomas Graf | dea3f62 | 2005-11-05 21:14:09 +0100 | [diff] [blame] | 495 | gred_disable_wred_mode(table); |
| 496 | if (gred_wred_mode_check(sch)) |
| 497 | gred_enable_wred_mode(table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | } |
| 499 | |
Thomas Graf | f62d6b9 | 2005-11-05 21:14:15 +0100 | [diff] [blame] | 500 | err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | |
Thomas Graf | f62d6b9 | 2005-11-05 21:14:15 +0100 | [diff] [blame] | 502 | errout_locked: |
| 503 | sch_tree_unlock(sch); |
| 504 | errout: |
| 505 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | static int gred_init(struct Qdisc *sch, struct rtattr *opt) |
| 509 | { |
Thomas Graf | 6639607 | 2005-11-05 21:14:13 +0100 | [diff] [blame] | 510 | struct rtattr *tb[TCA_GRED_MAX]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | |
Thomas Graf | 6639607 | 2005-11-05 21:14:13 +0100 | [diff] [blame] | 512 | if (opt == NULL || rtattr_parse_nested(tb, TCA_GRED_MAX, opt)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | return -EINVAL; |
| 514 | |
Thomas Graf | 6639607 | 2005-11-05 21:14:13 +0100 | [diff] [blame] | 515 | if (tb[TCA_GRED_PARMS-1] || tb[TCA_GRED_STAB-1]) |
| 516 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | |
Thomas Graf | 6639607 | 2005-11-05 21:14:13 +0100 | [diff] [blame] | 518 | return gred_change_table_def(sch, tb[TCA_GRED_DPS-1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | static int gred_dump(struct Qdisc *sch, struct sk_buff *skb) |
| 522 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | struct gred_sched *table = qdisc_priv(sch); |
Thomas Graf | 05f1cc0 | 2005-11-05 21:14:11 +0100 | [diff] [blame] | 524 | struct rtattr *parms, *opts = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | int i; |
Thomas Graf | e063682 | 2005-11-05 21:14:12 +0100 | [diff] [blame] | 526 | struct tc_gred_sopt sopt = { |
| 527 | .DPs = table->DPs, |
| 528 | .def_DP = table->def, |
| 529 | .grio = gred_rio_mode(table), |
| 530 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | |
Thomas Graf | 05f1cc0 | 2005-11-05 21:14:11 +0100 | [diff] [blame] | 532 | opts = RTA_NEST(skb, TCA_OPTIONS); |
Thomas Graf | e063682 | 2005-11-05 21:14:12 +0100 | [diff] [blame] | 533 | RTA_PUT(skb, TCA_GRED_DPS, sizeof(sopt), &sopt); |
Thomas Graf | 05f1cc0 | 2005-11-05 21:14:11 +0100 | [diff] [blame] | 534 | parms = RTA_NEST(skb, TCA_GRED_PARMS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | |
Thomas Graf | 05f1cc0 | 2005-11-05 21:14:11 +0100 | [diff] [blame] | 536 | for (i = 0; i < MAX_DPs; i++) { |
| 537 | struct gred_sched_data *q = table->tab[i]; |
| 538 | struct tc_gred_qopt opt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | |
Thomas Graf | 05f1cc0 | 2005-11-05 21:14:11 +0100 | [diff] [blame] | 540 | memset(&opt, 0, sizeof(opt)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | |
| 542 | if (!q) { |
| 543 | /* hack -- fix at some point with proper message |
| 544 | This is how we indicate to tc that there is no VQ |
| 545 | at this DP */ |
| 546 | |
Thomas Graf | 05f1cc0 | 2005-11-05 21:14:11 +0100 | [diff] [blame] | 547 | opt.DP = MAX_DPs + i; |
| 548 | goto append_opt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | } |
| 550 | |
Thomas Graf | 05f1cc0 | 2005-11-05 21:14:11 +0100 | [diff] [blame] | 551 | opt.limit = q->limit; |
| 552 | opt.DP = q->DP; |
| 553 | opt.backlog = q->backlog; |
| 554 | opt.prio = q->prio; |
Thomas Graf | 22b3342 | 2005-11-05 21:14:16 +0100 | [diff] [blame] | 555 | opt.qth_min = q->parms.qth_min >> q->parms.Wlog; |
| 556 | opt.qth_max = q->parms.qth_max >> q->parms.Wlog; |
| 557 | opt.Wlog = q->parms.Wlog; |
| 558 | opt.Plog = q->parms.Plog; |
| 559 | opt.Scell_log = q->parms.Scell_log; |
| 560 | opt.other = q->stats.other; |
| 561 | opt.early = q->stats.prob_drop; |
| 562 | opt.forced = q->stats.forced_drop; |
| 563 | opt.pdrop = q->stats.pdrop; |
Thomas Graf | 05f1cc0 | 2005-11-05 21:14:11 +0100 | [diff] [blame] | 564 | opt.packets = q->packetsin; |
| 565 | opt.bytesin = q->bytesin; |
| 566 | |
Thomas Graf | 22b3342 | 2005-11-05 21:14:16 +0100 | [diff] [blame] | 567 | if (gred_wred_mode(table)) { |
| 568 | q->parms.qidlestart = |
| 569 | table->tab[table->def]->parms.qidlestart; |
| 570 | q->parms.qavg = table->tab[table->def]->parms.qavg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | |
Thomas Graf | 22b3342 | 2005-11-05 21:14:16 +0100 | [diff] [blame] | 573 | opt.qave = red_calc_qavg(&q->parms, q->parms.qavg); |
| 574 | |
Thomas Graf | 05f1cc0 | 2005-11-05 21:14:11 +0100 | [diff] [blame] | 575 | append_opt: |
| 576 | RTA_APPEND(skb, sizeof(opt), &opt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | } |
| 578 | |
Thomas Graf | 05f1cc0 | 2005-11-05 21:14:11 +0100 | [diff] [blame] | 579 | RTA_NEST_END(skb, parms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | |
Thomas Graf | 05f1cc0 | 2005-11-05 21:14:11 +0100 | [diff] [blame] | 581 | return RTA_NEST_END(skb, opts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | |
| 583 | rtattr_failure: |
Thomas Graf | 05f1cc0 | 2005-11-05 21:14:11 +0100 | [diff] [blame] | 584 | return RTA_NEST_CANCEL(skb, opts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | static void gred_destroy(struct Qdisc *sch) |
| 588 | { |
| 589 | struct gred_sched *table = qdisc_priv(sch); |
| 590 | int i; |
| 591 | |
| 592 | for (i = 0;i < table->DPs; i++) { |
| 593 | if (table->tab[i]) |
Thomas Graf | 6639607 | 2005-11-05 21:14:13 +0100 | [diff] [blame] | 594 | gred_destroy_vq(table->tab[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | } |
| 596 | } |
| 597 | |
| 598 | static struct Qdisc_ops gred_qdisc_ops = { |
| 599 | .next = NULL, |
| 600 | .cl_ops = NULL, |
| 601 | .id = "gred", |
| 602 | .priv_size = sizeof(struct gred_sched), |
| 603 | .enqueue = gred_enqueue, |
| 604 | .dequeue = gred_dequeue, |
| 605 | .requeue = gred_requeue, |
| 606 | .drop = gred_drop, |
| 607 | .init = gred_init, |
| 608 | .reset = gred_reset, |
| 609 | .destroy = gred_destroy, |
| 610 | .change = gred_change, |
| 611 | .dump = gred_dump, |
| 612 | .owner = THIS_MODULE, |
| 613 | }; |
| 614 | |
| 615 | static int __init gred_module_init(void) |
| 616 | { |
| 617 | return register_qdisc(&gred_qdisc_ops); |
| 618 | } |
| 619 | static void __exit gred_module_exit(void) |
| 620 | { |
| 621 | unregister_qdisc(&gred_qdisc_ops); |
| 622 | } |
| 623 | module_init(gred_module_init) |
| 624 | module_exit(gred_module_exit) |
| 625 | MODULE_LICENSE("GPL"); |