pkt_sched: gen_estimator: Dont report fake rate estimators
[safe/jmp/linux-2.6] / net / sched / sch_gred.c
index 897e6df..40408d5 100644 (file)
  *                      from Ren Liu
  *                    - More error checks
  *
- *
- *
- *  For all the glorious comments look at Alexey's sch_red.c
+ *  For all the glorious comments look at include/net/red.h
  */
 
-#include <linux/config.h>
 #include <linux/module.h>
-#include <asm/uaccess.h>
-#include <asm/system.h>
-#include <linux/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 <net/ip.h>
-#include <net/route.h>
 #include <linux/skbuff.h>
-#include <net/sock.h>
 #include <net/pkt_sched.h>
 #include <net/red.h>
 
-#if 1 /* control */
-#define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
-#else
-#define DPRINTK(format,args...)
-#endif
-
-#if 0 /* data */
-#define D2PRINTK(format,args...) printk(KERN_DEBUG format,##args)
-#else
-#define D2PRINTK(format,args...)
-#endif
-
 #define GRED_DEF_PRIO (MAX_DPs / 2)
 #define GRED_VQ_MASK (MAX_DPs - 1)
 
@@ -72,7 +38,7 @@ struct gred_sched_data
        u32             bytesin;        /* bytes seen on virtualQ so far*/
        u32             packetsin;      /* packets seen on virtualQ so far*/
        u32             backlog;        /* bytes on the virtualQ */
-       u8              prio;        /* the prio of this vq */
+       u8              prio;           /* the prio of this vq */
 
        struct red_parms parms;
        struct red_stats stats;
@@ -87,8 +53,9 @@ struct gred_sched
 {
        struct gred_sched_data *tab[MAX_DPs];
        unsigned long   flags;
-       u32             DPs;   
-       u32             def; 
+       u32             red_flags;
+       u32             DPs;
+       u32             def;
        struct red_parms wred_set;
 };
 
@@ -172,13 +139,21 @@ static inline void gred_store_wred_set(struct gred_sched *table,
        table->wred_set.qavg = q->parms.qavg;
 }
 
-static int
-gred_enqueue(struct sk_buff *skb, struct Qdisc* sch)
+static inline int gred_use_ecn(struct gred_sched *t)
+{
+       return t->red_flags & TC_RED_ECN;
+}
+
+static inline int gred_use_harddrop(struct gred_sched *t)
+{
+       return t->red_flags & TC_RED_HARDDROP;
+}
+
+static int gred_enqueue(struct sk_buff *skb, struct Qdisc* sch)
 {
        struct gred_sched_data *q=NULL;
        struct gred_sched *t= qdisc_priv(sch);
        unsigned long qavg = 0;
-       int i=0;
        u16 dp = tc_index_to_dp(skb);
 
        if (dp >= t->DPs  || (q = t->tab[dp]) == NULL) {
@@ -189,7 +164,7 @@ gred_enqueue(struct sk_buff *skb, struct Qdisc* sch)
                         * if no default DP has been configured. This
                         * allows for DP flows to be left untouched.
                         */
-                       if (skb_queue_len(&sch->q) < sch->dev->tx_queue_len)
+                       if (skb_queue_len(&sch->q) < qdisc_dev(sch)->tx_queue_len)
                                return qdisc_enqueue_tail(skb, sch);
                        else
                                goto drop;
@@ -200,26 +175,23 @@ gred_enqueue(struct sk_buff *skb, struct Qdisc* sch)
                skb->tc_index = (skb->tc_index & ~GRED_VQ_MASK) | dp;
        }
 
-       /* sum up all the qaves of prios <= to ours to get the new qave*/
+       /* sum up all the qaves of prios <= to ours to get the new qave */
        if (!gred_wred_mode(t) && gred_rio_mode(t)) {
-               for (i=0;i<t->DPs;i++) {
-                       if ((!t->tab[i]) || (i==q->DP)) 
-                               continue; 
-                               
-                       if (t->tab[i]->prio < q->prio &&
+               int i;
+
+               for (i = 0; i < t->DPs; i++) {
+                       if (t->tab[i] && t->tab[i]->prio < q->prio &&
                            !red_is_idling(&t->tab[i]->parms))
                                qavg +=t->tab[i]->parms.qavg;
                }
-                       
+
        }
 
        q->packetsin++;
-       q->bytesin+=skb->len;
+       q->bytesin += qdisc_pkt_len(skb);
 
-       if (gred_wred_mode(t)) {
-               qavg = 0;
+       if (gred_wred_mode(t))
                gred_load_wred_set(t, q);
-       }
 
        q->parms.qavg = red_calc_qavg(&q->parms, gred_backlog(t, q, sch));
 
@@ -235,17 +207,27 @@ gred_enqueue(struct sk_buff *skb, struct Qdisc* sch)
 
                case RED_PROB_MARK:
                        sch->qstats.overlimits++;
-                       q->stats.prob_drop++;
-                       goto congestion_drop;
+                       if (!gred_use_ecn(t) || !INET_ECN_set_ce(skb)) {
+                               q->stats.prob_drop++;
+                               goto congestion_drop;
+                       }
+
+                       q->stats.prob_mark++;
+                       break;
 
                case RED_HARD_MARK:
                        sch->qstats.overlimits++;
-                       q->stats.forced_drop++;
-                       goto congestion_drop;
+                       if (gred_use_harddrop(t) || !gred_use_ecn(t) ||
+                           !INET_ECN_set_ce(skb)) {
+                               q->stats.forced_drop++;
+                               goto congestion_drop;
+                       }
+                       q->stats.forced_mark++;
+                       break;
        }
 
-       if (q->backlog + skb->len <= q->limit) {
-               q->backlog += skb->len;
+       if (q->backlog + qdisc_pkt_len(skb) <= q->limit) {
+               q->backlog += qdisc_pkt_len(skb);
                return qdisc_enqueue_tail(skb, sch);
        }
 
@@ -258,37 +240,15 @@ congestion_drop:
        return NET_XMIT_CN;
 }
 
-static int
-gred_requeue(struct sk_buff *skb, struct Qdisc* sch)
-{
-       struct gred_sched *t = qdisc_priv(sch);
-       struct gred_sched_data *q;
-       u16 dp = tc_index_to_dp(skb);
-
-       if (dp >= t->DPs || (q = t->tab[dp]) == NULL) {
-               if (net_ratelimit())
-                       printk(KERN_WARNING "GRED: Unable to relocate VQ 0x%x "
-                              "for requeue, screwing up backlog.\n",
-                              tc_index_to_dp(skb));
-       } else {
-               if (red_is_idling(&q->parms))
-                       red_end_of_idle_period(&q->parms);
-               q->backlog += skb->len;
-       }
-
-       return qdisc_requeue(skb, sch);
-}
-
-static struct sk_buff *
-gred_dequeue(struct Qdisc* sch)
+static struct sk_buff *gred_dequeue(struct Qdisc* sch)
 {
        struct sk_buff *skb;
-       struct gred_sched_data *q;
-       struct gred_sched *t= qdisc_priv(sch);
+       struct gred_sched *t = qdisc_priv(sch);
 
        skb = qdisc_dequeue_head(sch);
 
        if (skb) {
+               struct gred_sched_data *q;
                u16 dp = tc_index_to_dp(skb);
 
                if (dp >= t->DPs || (q = t->tab[dp]) == NULL) {
@@ -297,7 +257,7 @@ gred_dequeue(struct Qdisc* sch)
                                       "VQ 0x%x after dequeue, screwing up "
                                       "backlog.\n", tc_index_to_dp(skb));
                } else {
-                       q->backlog -= skb->len;
+                       q->backlog -= qdisc_pkt_len(skb);
 
                        if (!q->backlog && !gred_wred_mode(t))
                                red_start_of_idle_period(&q->parms);
@@ -306,7 +266,7 @@ gred_dequeue(struct Qdisc* sch)
                return skb;
        }
 
-       if (gred_wred_mode(t))
+       if (gred_wred_mode(t) && !red_is_idling(&t->wred_set))
                red_start_of_idle_period(&t->wred_set);
 
        return NULL;
@@ -315,13 +275,12 @@ gred_dequeue(struct Qdisc* sch)
 static unsigned int gred_drop(struct Qdisc* sch)
 {
        struct sk_buff *skb;
-
-       struct gred_sched_data *q;
-       struct gred_sched *t= qdisc_priv(sch);
+       struct gred_sched *t = qdisc_priv(sch);
 
        skb = qdisc_dequeue_tail(sch);
        if (skb) {
-               unsigned int len = skb->len;
+               unsigned int len = qdisc_pkt_len(skb);
+               struct gred_sched_data *q;
                u16 dp = tc_index_to_dp(skb);
 
                if (dp >= t->DPs || (q = t->tab[dp]) == NULL) {
@@ -341,7 +300,7 @@ static unsigned int gred_drop(struct Qdisc* sch)
                return len;
        }
 
-       if (gred_wred_mode(t))
+       if (gred_wred_mode(t) && !red_is_idling(&t->wred_set))
                red_start_of_idle_period(&t->wred_set);
 
        return 0;
@@ -351,15 +310,16 @@ static unsigned int gred_drop(struct Qdisc* sch)
 static void gred_reset(struct Qdisc* sch)
 {
        int i;
-       struct gred_sched_data *q;
-       struct gred_sched *t= qdisc_priv(sch);
+       struct gred_sched *t = qdisc_priv(sch);
 
        qdisc_reset_queue(sch);
 
-        for (i=0;i<t->DPs;i++) {
-               q= t->tab[i];
-               if (!q) 
-                       continue; 
+       for (i = 0; i < t->DPs; i++) {
+               struct gred_sched_data *q = t->tab[i];
+
+               if (!q)
+                       continue;
+
                red_restart(&q->parms);
                q->backlog = 0;
        }
@@ -370,16 +330,16 @@ static inline void gred_destroy_vq(struct gred_sched_data *q)
        kfree(q);
 }
 
-static inline int gred_change_table_def(struct Qdisc *sch, struct rtattr *dps)
+static inline int gred_change_table_def(struct Qdisc *sch, struct nlattr *dps)
 {
        struct gred_sched *table = qdisc_priv(sch);
        struct tc_gred_sopt *sopt;
        int i;
 
-       if (dps == NULL || RTA_PAYLOAD(dps) < sizeof(*sopt))
+       if (dps == NULL)
                return -EINVAL;
 
-       sopt = RTA_DATA(dps);
+       sopt = nla_data(dps);
 
        if (sopt->DPs > MAX_DPs || sopt->DPs == 0 || sopt->def_DP >= sopt->DPs)
                return -EINVAL;
@@ -387,6 +347,7 @@ static inline int gred_change_table_def(struct Qdisc *sch, struct rtattr *dps)
        sch_tree_lock(sch);
        table->DPs = sopt->DPs;
        table->def = sopt->def_DP;
+       table->red_flags = sopt->flags;
 
        /*
         * Every entry point to GRED is synchronized with the above code
@@ -411,7 +372,7 @@ static inline int gred_change_table_def(struct Qdisc *sch, struct rtattr *dps)
                               "shadowed VQ 0x%x\n", i);
                        gred_destroy_vq(table->tab[i]);
                        table->tab[i] = NULL;
-               }
+               }
        }
 
        return 0;
@@ -424,10 +385,9 @@ static inline int gred_change_vq(struct Qdisc *sch, int dp,
        struct gred_sched_data *q;
 
        if (table->tab[dp] == NULL) {
-               table->tab[dp] = kmalloc(sizeof(*q), GFP_KERNEL);
+               table->tab[dp] = kzalloc(sizeof(*q), GFP_KERNEL);
                if (table->tab[dp] == NULL)
                        return -ENOMEM;
-               memset(table->tab[dp], 0, sizeof(*q));
        }
 
        q = table->tab[dp];
@@ -445,28 +405,37 @@ static inline int gred_change_vq(struct Qdisc *sch, int dp,
        return 0;
 }
 
-static int gred_change(struct Qdisc *sch, struct rtattr *opt)
+static const struct nla_policy gred_policy[TCA_GRED_MAX + 1] = {
+       [TCA_GRED_PARMS]        = { .len = sizeof(struct tc_gred_qopt) },
+       [TCA_GRED_STAB]         = { .len = 256 },
+       [TCA_GRED_DPS]          = { .len = sizeof(struct tc_gred_sopt) },
+};
+
+static int gred_change(struct Qdisc *sch, struct nlattr *opt)
 {
        struct gred_sched *table = qdisc_priv(sch);
        struct tc_gred_qopt *ctl;
-       struct rtattr *tb[TCA_GRED_MAX];
-       int err = -EINVAL, prio = GRED_DEF_PRIO;
+       struct nlattr *tb[TCA_GRED_MAX + 1];
+       int err, prio = GRED_DEF_PRIO;
        u8 *stab;
 
-       if (opt == NULL || rtattr_parse_nested(tb, TCA_GRED_MAX, opt))
+       if (opt == NULL)
                return -EINVAL;
 
-       if (tb[TCA_GRED_PARMS-1] == NULL && tb[TCA_GRED_STAB-1] == NULL)
+       err = nla_parse_nested(tb, TCA_GRED_MAX, opt, gred_policy);
+       if (err < 0)
+               return err;
+
+       if (tb[TCA_GRED_PARMS] == NULL && tb[TCA_GRED_STAB] == NULL)
                return gred_change_table_def(sch, opt);
 
-       if (tb[TCA_GRED_PARMS-1] == NULL ||
-           RTA_PAYLOAD(tb[TCA_GRED_PARMS-1]) < sizeof(*ctl) ||
-           tb[TCA_GRED_STAB-1] == NULL ||
-           RTA_PAYLOAD(tb[TCA_GRED_STAB-1]) < 256)
+       if (tb[TCA_GRED_PARMS] == NULL ||
+           tb[TCA_GRED_STAB] == NULL)
                return -EINVAL;
 
-       ctl = RTA_DATA(tb[TCA_GRED_PARMS-1]);
-       stab = RTA_DATA(tb[TCA_GRED_STAB-1]);
+       err = -EINVAL;
+       ctl = nla_data(tb[TCA_GRED_PARMS]);
+       stab = nla_data(tb[TCA_GRED_STAB]);
 
        if (ctl->DP >= table->DPs)
                goto errout;
@@ -506,33 +475,43 @@ errout:
        return err;
 }
 
-static int gred_init(struct Qdisc *sch, struct rtattr *opt)
+static int gred_init(struct Qdisc *sch, struct nlattr *opt)
 {
-       struct rtattr *tb[TCA_GRED_MAX];
+       struct nlattr *tb[TCA_GRED_MAX + 1];
+       int err;
 
-       if (opt == NULL || rtattr_parse_nested(tb, TCA_GRED_MAX, opt))
+       if (opt == NULL)
                return -EINVAL;
 
-       if (tb[TCA_GRED_PARMS-1] || tb[TCA_GRED_STAB-1])
+       err = nla_parse_nested(tb, TCA_GRED_MAX, opt, gred_policy);
+       if (err < 0)
+               return err;
+
+       if (tb[TCA_GRED_PARMS] || tb[TCA_GRED_STAB])
                return -EINVAL;
 
-       return gred_change_table_def(sch, tb[TCA_GRED_DPS-1]);
+       return gred_change_table_def(sch, tb[TCA_GRED_DPS]);
 }
 
 static int gred_dump(struct Qdisc *sch, struct sk_buff *skb)
 {
        struct gred_sched *table = qdisc_priv(sch);
-       struct rtattr *parms, *opts = NULL;
+       struct nlattr *parms, *opts = NULL;
        int i;
        struct tc_gred_sopt sopt = {
                .DPs    = table->DPs,
                .def_DP = table->def,
                .grio   = gred_rio_mode(table),
+               .flags  = table->red_flags,
        };
 
-       opts = RTA_NEST(skb, TCA_OPTIONS);
-       RTA_PUT(skb, TCA_GRED_DPS, sizeof(sopt), &sopt);
-       parms = RTA_NEST(skb, TCA_GRED_PARMS);
+       opts = nla_nest_start(skb, TCA_OPTIONS);
+       if (opts == NULL)
+               goto nla_put_failure;
+       NLA_PUT(skb, TCA_GRED_DPS, sizeof(sopt), &sopt);
+       parms = nla_nest_start(skb, TCA_GRED_PARMS);
+       if (parms == NULL)
+               goto nla_put_failure;
 
        for (i = 0; i < MAX_DPs; i++) {
                struct gred_sched_data *q = table->tab[i];
@@ -574,15 +553,17 @@ static int gred_dump(struct Qdisc *sch, struct sk_buff *skb)
                opt.qave = red_calc_qavg(&q->parms, q->parms.qavg);
 
 append_opt:
-               RTA_APPEND(skb, sizeof(opt), &opt);
+               if (nla_append(skb, sizeof(opt), &opt) < 0)
+                       goto nla_put_failure;
        }
 
-       RTA_NEST_END(skb, parms);
+       nla_nest_end(skb, parms);
 
-       return RTA_NEST_END(skb, opts);
+       return nla_nest_end(skb, opts);
 
-rtattr_failure:
-       return RTA_NEST_CANCEL(skb, opts);
+nla_put_failure:
+       nla_nest_cancel(skb, opts);
+       return -EMSGSIZE;
 }
 
 static void gred_destroy(struct Qdisc *sch)
@@ -590,20 +571,18 @@ static void gred_destroy(struct Qdisc *sch)
        struct gred_sched *table = qdisc_priv(sch);
        int i;
 
-       for (i = 0;i < table->DPs; i++) {
+       for (i = 0; i < table->DPs; i++) {
                if (table->tab[i])
                        gred_destroy_vq(table->tab[i]);
        }
 }
 
-static struct Qdisc_ops gred_qdisc_ops = {
-       .next           =       NULL,
-       .cl_ops         =       NULL,
+static struct Qdisc_ops gred_qdisc_ops __read_mostly = {
        .id             =       "gred",
        .priv_size      =       sizeof(struct gred_sched),
        .enqueue        =       gred_enqueue,
        .dequeue        =       gred_dequeue,
-       .requeue        =       gred_requeue,
+       .peek           =       qdisc_peek_head,
        .drop           =       gred_drop,
        .init           =       gred_init,
        .reset          =       gred_reset,
@@ -617,10 +596,13 @@ static int __init gred_module_init(void)
 {
        return register_qdisc(&gred_qdisc_ops);
 }
-static void __exit gred_module_exit(void) 
+
+static void __exit gred_module_exit(void)
 {
        unregister_qdisc(&gred_qdisc_ops);
 }
+
 module_init(gred_module_init)
 module_exit(gred_module_exit)
+
 MODULE_LICENSE("GPL");