Merge branch 'master' of master.kernel.org:/pub/scm/linux/kernel/git/torvalds/linux-2.6
[pandora-kernel.git] / net / sched / sch_netem.c
index 9e5e87e..c5ea40c 100644 (file)
@@ -180,7 +180,7 @@ static int netem_enqueue(struct sk_buff *skb, struct Qdisc *sch)
         * skb will be queued.
         */
        if (count > 1 && (skb2 = skb_clone(skb, GFP_ATOMIC)) != NULL) {
-               struct Qdisc *rootq = sch->dev->qdisc;
+               struct Qdisc *rootq = qdisc_root(sch);
                u32 dupsave = q->duplicate; /* prevent duplicating a dup... */
                q->duplicate = 0;
 
@@ -310,37 +310,16 @@ static void netem_reset(struct Qdisc *sch)
        qdisc_watchdog_cancel(&q->watchdog);
 }
 
-/* Pass size change message down to embedded FIFO */
-static int set_fifo_limit(struct Qdisc *q, int limit)
-{
-       struct rtattr *rta;
-       int ret = -ENOMEM;
-
-       /* Hack to avoid sending change message to non-FIFO */
-       if (strncmp(q->ops->id + 1, "fifo", 4) != 0)
-               return 0;
-
-       rta = kmalloc(RTA_LENGTH(sizeof(struct tc_fifo_qopt)), GFP_KERNEL);
-       if (rta) {
-               rta->rta_type = RTM_NEWQDISC;
-               rta->rta_len = RTA_LENGTH(sizeof(struct tc_fifo_qopt));
-               ((struct tc_fifo_qopt *)RTA_DATA(rta))->limit = limit;
-
-               ret = q->ops->change(q, rta);
-               kfree(rta);
-       }
-       return ret;
-}
-
 /*
  * Distribution data is a variable size payload containing
  * signed 16 bit values.
  */
-static int get_dist_table(struct Qdisc *sch, const struct rtattr *attr)
+static int get_dist_table(struct Qdisc *sch, const struct nlattr *attr)
 {
        struct netem_sched_data *q = qdisc_priv(sch);
-       unsigned long n = RTA_PAYLOAD(attr)/sizeof(__s16);
-       const __s16 *data = RTA_DATA(attr);
+       unsigned long n = nla_len(attr)/sizeof(__s16);
+       const __s16 *data = nla_data(attr);
+       spinlock_t *root_lock;
        struct disttable *d;
        int i;
 
@@ -355,21 +334,20 @@ static int get_dist_table(struct Qdisc *sch, const struct rtattr *attr)
        for (i = 0; i < n; i++)
                d->table[i] = data[i];
 
-       spin_lock_bh(&sch->dev->queue_lock);
+       root_lock = qdisc_root_lock(sch);
+
+       spin_lock_bh(root_lock);
        d = xchg(&q->delay_dist, d);
-       spin_unlock_bh(&sch->dev->queue_lock);
+       spin_unlock_bh(root_lock);
 
        kfree(d);
        return 0;
 }
 
-static int get_correlation(struct Qdisc *sch, const struct rtattr *attr)
+static int get_correlation(struct Qdisc *sch, const struct nlattr *attr)
 {
        struct netem_sched_data *q = qdisc_priv(sch);
-       const struct tc_netem_corr *c = RTA_DATA(attr);
-
-       if (RTA_PAYLOAD(attr) != sizeof(*c))
-               return -EINVAL;
+       const struct tc_netem_corr *c = nla_data(attr);
 
        init_crandom(&q->delay_cor, c->delay_corr);
        init_crandom(&q->loss_cor, c->loss_corr);
@@ -377,44 +355,49 @@ static int get_correlation(struct Qdisc *sch, const struct rtattr *attr)
        return 0;
 }
 
-static int get_reorder(struct Qdisc *sch, const struct rtattr *attr)
+static int get_reorder(struct Qdisc *sch, const struct nlattr *attr)
 {
        struct netem_sched_data *q = qdisc_priv(sch);
-       const struct tc_netem_reorder *r = RTA_DATA(attr);
-
-       if (RTA_PAYLOAD(attr) != sizeof(*r))
-               return -EINVAL;
+       const struct tc_netem_reorder *r = nla_data(attr);
 
        q->reorder = r->probability;
        init_crandom(&q->reorder_cor, r->correlation);
        return 0;
 }
 
-static int get_corrupt(struct Qdisc *sch, const struct rtattr *attr)
+static int get_corrupt(struct Qdisc *sch, const struct nlattr *attr)
 {
        struct netem_sched_data *q = qdisc_priv(sch);
-       const struct tc_netem_corrupt *r = RTA_DATA(attr);
-
-       if (RTA_PAYLOAD(attr) != sizeof(*r))
-               return -EINVAL;
+       const struct tc_netem_corrupt *r = nla_data(attr);
 
        q->corrupt = r->probability;
        init_crandom(&q->corrupt_cor, r->correlation);
        return 0;
 }
 
+static const struct nla_policy netem_policy[TCA_NETEM_MAX + 1] = {
+       [TCA_NETEM_CORR]        = { .len = sizeof(struct tc_netem_corr) },
+       [TCA_NETEM_REORDER]     = { .len = sizeof(struct tc_netem_reorder) },
+       [TCA_NETEM_CORRUPT]     = { .len = sizeof(struct tc_netem_corrupt) },
+};
+
 /* Parse netlink message to set options */
-static int netem_change(struct Qdisc *sch, struct rtattr *opt)
+static int netem_change(struct Qdisc *sch, struct nlattr *opt)
 {
        struct netem_sched_data *q = qdisc_priv(sch);
+       struct nlattr *tb[TCA_NETEM_MAX + 1];
        struct tc_netem_qopt *qopt;
        int ret;
 
-       if (opt == NULL || RTA_PAYLOAD(opt) < sizeof(*qopt))
+       if (opt == NULL)
                return -EINVAL;
 
-       qopt = RTA_DATA(opt);
-       ret = set_fifo_limit(q->qdisc, qopt->limit);
+       ret = nla_parse_nested_compat(tb, TCA_NETEM_MAX, opt, netem_policy,
+                                     qopt, sizeof(*qopt));
+       if (ret < 0)
+               return ret;
+
+       ret = fifo_set_limit(q->qdisc, qopt->limit);
        if (ret) {
                pr_debug("netem: can't set fifo limit\n");
                return ret;
@@ -434,39 +417,28 @@ static int netem_change(struct Qdisc *sch, struct rtattr *opt)
        if (q->gap)
                q->reorder = ~0;
 
-       /* Handle nested options after initial queue options.
-        * Should have put all options in nested format but too late now.
-        */
-       if (RTA_PAYLOAD(opt) > sizeof(*qopt)) {
-               struct rtattr *tb[TCA_NETEM_MAX];
-               if (rtattr_parse(tb, TCA_NETEM_MAX,
-                                RTA_DATA(opt) + sizeof(*qopt),
-                                RTA_PAYLOAD(opt) - sizeof(*qopt)))
-                       return -EINVAL;
-
-               if (tb[TCA_NETEM_CORR-1]) {
-                       ret = get_correlation(sch, tb[TCA_NETEM_CORR-1]);
-                       if (ret)
-                               return ret;
-               }
+       if (tb[TCA_NETEM_CORR]) {
+               ret = get_correlation(sch, tb[TCA_NETEM_CORR]);
+               if (ret)
+                       return ret;
+       }
 
-               if (tb[TCA_NETEM_DELAY_DIST-1]) {
-                       ret = get_dist_table(sch, tb[TCA_NETEM_DELAY_DIST-1]);
-                       if (ret)
-                               return ret;
-               }
+       if (tb[TCA_NETEM_DELAY_DIST]) {
+               ret = get_dist_table(sch, tb[TCA_NETEM_DELAY_DIST]);
+               if (ret)
+                       return ret;
+       }
 
-               if (tb[TCA_NETEM_REORDER-1]) {
-                       ret = get_reorder(sch, tb[TCA_NETEM_REORDER-1]);
-                       if (ret)
-                               return ret;
-               }
+       if (tb[TCA_NETEM_REORDER]) {
+               ret = get_reorder(sch, tb[TCA_NETEM_REORDER]);
+               if (ret)
+                       return ret;
+       }
 
-               if (tb[TCA_NETEM_CORRUPT-1]) {
-                       ret = get_corrupt(sch, tb[TCA_NETEM_CORRUPT-1]);
-                       if (ret)
-                               return ret;
-               }
+       if (tb[TCA_NETEM_CORRUPT]) {
+               ret = get_corrupt(sch, tb[TCA_NETEM_CORRUPT]);
+               if (ret)
+                       return ret;
        }
 
        return 0;
@@ -515,18 +487,18 @@ static int tfifo_enqueue(struct sk_buff *nskb, struct Qdisc *sch)
        return qdisc_reshape_fail(nskb, sch);
 }
 
-static int tfifo_init(struct Qdisc *sch, struct rtattr *opt)
+static int tfifo_init(struct Qdisc *sch, struct nlattr *opt)
 {
        struct fifo_sched_data *q = qdisc_priv(sch);
 
        if (opt) {
-               struct tc_fifo_qopt *ctl = RTA_DATA(opt);
-               if (RTA_PAYLOAD(opt) < sizeof(*ctl))
+               struct tc_fifo_qopt *ctl = nla_data(opt);
+               if (nla_len(opt) < sizeof(*ctl))
                        return -EINVAL;
 
                q->limit = ctl->limit;
        } else
-               q->limit = max_t(u32, sch->dev->tx_queue_len, 1);
+               q->limit = max_t(u32, qdisc_dev(sch)->tx_queue_len, 1);
 
        q->oldest = PSCHED_PASTPERFECT;
        return 0;
@@ -537,14 +509,14 @@ static int tfifo_dump(struct Qdisc *sch, struct sk_buff *skb)
        struct fifo_sched_data *q = qdisc_priv(sch);
        struct tc_fifo_qopt opt = { .limit = q->limit };
 
-       RTA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
+       NLA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
        return skb->len;
 
-rtattr_failure:
+nla_put_failure:
        return -1;
 }
 
-static struct Qdisc_ops tfifo_qdisc_ops = {
+static struct Qdisc_ops tfifo_qdisc_ops __read_mostly = {
        .id             =       "tfifo",
        .priv_size      =       sizeof(struct fifo_sched_data),
        .enqueue        =       tfifo_enqueue,
@@ -557,7 +529,7 @@ static struct Qdisc_ops tfifo_qdisc_ops = {
        .dump           =       tfifo_dump,
 };
 
-static int netem_init(struct Qdisc *sch, struct rtattr *opt)
+static int netem_init(struct Qdisc *sch, struct nlattr *opt)
 {
        struct netem_sched_data *q = qdisc_priv(sch);
        int ret;
@@ -567,7 +539,8 @@ static int netem_init(struct Qdisc *sch, struct rtattr *opt)
 
        qdisc_watchdog_init(&q->watchdog, sch);
 
-       q->qdisc = qdisc_create_dflt(sch->dev, &tfifo_qdisc_ops,
+       q->qdisc = qdisc_create_dflt(qdisc_dev(sch), sch->dev_queue,
+                                    &tfifo_qdisc_ops,
                                     TC_H_MAKE(sch->handle, 1));
        if (!q->qdisc) {
                pr_debug("netem: qdisc create failed\n");
@@ -595,7 +568,7 @@ static int netem_dump(struct Qdisc *sch, struct sk_buff *skb)
 {
        const struct netem_sched_data *q = qdisc_priv(sch);
        unsigned char *b = skb_tail_pointer(skb);
-       struct rtattr *rta = (struct rtattr *) b;
+       struct nlattr *nla = (struct nlattr *) b;
        struct tc_netem_qopt qopt;
        struct tc_netem_corr cor;
        struct tc_netem_reorder reorder;
@@ -607,26 +580,26 @@ static int netem_dump(struct Qdisc *sch, struct sk_buff *skb)
        qopt.loss = q->loss;
        qopt.gap = q->gap;
        qopt.duplicate = q->duplicate;
-       RTA_PUT(skb, TCA_OPTIONS, sizeof(qopt), &qopt);
+       NLA_PUT(skb, TCA_OPTIONS, sizeof(qopt), &qopt);
 
        cor.delay_corr = q->delay_cor.rho;
        cor.loss_corr = q->loss_cor.rho;
        cor.dup_corr = q->dup_cor.rho;
-       RTA_PUT(skb, TCA_NETEM_CORR, sizeof(cor), &cor);
+       NLA_PUT(skb, TCA_NETEM_CORR, sizeof(cor), &cor);
 
        reorder.probability = q->reorder;
        reorder.correlation = q->reorder_cor.rho;
-       RTA_PUT(skb, TCA_NETEM_REORDER, sizeof(reorder), &reorder);
+       NLA_PUT(skb, TCA_NETEM_REORDER, sizeof(reorder), &reorder);
 
        corrupt.probability = q->corrupt;
        corrupt.correlation = q->corrupt_cor.rho;
-       RTA_PUT(skb, TCA_NETEM_CORRUPT, sizeof(corrupt), &corrupt);
+       NLA_PUT(skb, TCA_NETEM_CORRUPT, sizeof(corrupt), &corrupt);
 
-       rta->rta_len = skb_tail_pointer(skb) - b;
+       nla->nla_len = skb_tail_pointer(skb) - b;
 
        return skb->len;
 
-rtattr_failure:
+nla_put_failure:
        nlmsg_trim(skb, b);
        return -1;
 }
@@ -678,7 +651,7 @@ static void netem_put(struct Qdisc *sch, unsigned long arg)
 }
 
 static int netem_change_class(struct Qdisc *sch, u32 classid, u32 parentid,
-                           struct rtattr **tca, unsigned long *arg)
+                           struct nlattr **tca, unsigned long *arg)
 {
        return -ENOSYS;
 }
@@ -705,7 +678,7 @@ static struct tcf_proto **netem_find_tcf(struct Qdisc *sch, unsigned long cl)
        return NULL;
 }
 
-static struct Qdisc_class_ops netem_class_ops = {
+static const struct Qdisc_class_ops netem_class_ops = {
        .graft          =       netem_graft,
        .leaf           =       netem_leaf,
        .get            =       netem_get,
@@ -717,7 +690,7 @@ static struct Qdisc_class_ops netem_class_ops = {
        .dump           =       netem_dump_class,
 };
 
-static struct Qdisc_ops netem_qdisc_ops = {
+static struct Qdisc_ops netem_qdisc_ops __read_mostly = {
        .id             =       "netem",
        .cl_ops         =       &netem_class_ops,
        .priv_size      =       sizeof(struct netem_sched_data),