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