ipv4: tcp: set unicast_sock uc_ttl to -1
[linux-3.10.git] / net / sched / act_police.c
index a73e3e6..a9de232 100644 (file)
 #include <linux/skbuff.h>
 #include <linux/rtnetlink.h>
 #include <linux/init.h>
+#include <linux/slab.h>
 #include <net/act_api.h>
 #include <net/netlink.h>
 
-#define L2T(p,L)   qdisc_l2t((p)->tcfp_R_tab, L)
-#define L2T_P(p,L) qdisc_l2t((p)->tcfp_P_tab, L)
+#define L2T(p, L)   qdisc_l2t((p)->tcfp_R_tab, L)
+#define L2T_P(p, L) qdisc_l2t((p)->tcfp_P_tab, L)
 
 #define POL_TAB_MASK     15
 static struct tcf_common *tcf_police_ht[POL_TAB_MASK + 1];
@@ -36,8 +37,7 @@ static struct tcf_hashinfo police_hash_info = {
 };
 
 /* old policer structure from before tc actions */
-struct tc_police_compat
-{
+struct tc_police_compat {
        u32                     index;
        int                     action;
        u32                     limit;
@@ -54,7 +54,7 @@ static int tcf_act_police_walker(struct sk_buff *skb, struct netlink_callback *c
 {
        struct tcf_common *p;
        int err = 0, index = -1, i = 0, s_i = 0, n_i = 0;
-       struct rtattr *r;
+       struct nlattr *nest;
 
        read_lock_bh(&police_lock);
 
@@ -69,18 +69,19 @@ static int tcf_act_police_walker(struct sk_buff *skb, struct netlink_callback *c
                                continue;
                        a->priv = p;
                        a->order = index;
-                       r = (struct rtattr *)skb_tail_pointer(skb);
-                       RTA_PUT(skb, a->order, 0, NULL);
+                       nest = nla_nest_start(skb, a->order);
+                       if (nest == NULL)
+                               goto nla_put_failure;
                        if (type == RTM_DELACTION)
                                err = tcf_action_dump_1(skb, a, 0, 1);
                        else
                                err = tcf_action_dump_1(skb, a, 0, 0);
                        if (err < 0) {
                                index--;
-                               nlmsg_trim(skb, r);
+                               nla_nest_cancel(skb, nest);
                                goto done;
                        }
-                       r->rta_len = skb_tail_pointer(skb) - (u8 *)r;
+                       nla_nest_end(skb, nest);
                        n_i++;
                }
        }
@@ -90,8 +91,8 @@ done:
                cb->args[0] += n_i;
        return n_i;
 
-rtattr_failure:
-       nlmsg_trim(skb, r);
+nla_put_failure:
+       nla_nest_cancel(skb, nest);
        goto done;
 }
 
@@ -111,40 +112,48 @@ static void tcf_police_destroy(struct tcf_police *p)
                                qdisc_put_rtab(p->tcfp_R_tab);
                        if (p->tcfp_P_tab)
                                qdisc_put_rtab(p->tcfp_P_tab);
-                       kfree(p);
+                       /*
+                        * gen_estimator est_timer() might access p->tcf_lock
+                        * or bstats, wait a RCU grace period before freeing p
+                        */
+                       kfree_rcu(p, tcf_rcu);
                        return;
                }
        }
-       BUG_TRAP(0);
+       WARN_ON(1);
 }
 
-static int tcf_act_police_locate(struct rtattr *rta, struct rtattr *est,
+static const struct nla_policy police_policy[TCA_POLICE_MAX + 1] = {
+       [TCA_POLICE_RATE]       = { .len = TC_RTAB_SIZE },
+       [TCA_POLICE_PEAKRATE]   = { .len = TC_RTAB_SIZE },
+       [TCA_POLICE_AVRATE]     = { .type = NLA_U32 },
+       [TCA_POLICE_RESULT]     = { .type = NLA_U32 },
+};
+
+static int tcf_act_police_locate(struct nlattr *nla, struct nlattr *est,
                                 struct tc_action *a, int ovr, int bind)
 {
-       unsigned h;
+       unsigned int h;
        int ret = 0, err;
-       struct rtattr *tb[TCA_POLICE_MAX];
+       struct nlattr *tb[TCA_POLICE_MAX + 1];
        struct tc_police *parm;
        struct tcf_police *police;
        struct qdisc_rate_table *R_tab = NULL, *P_tab = NULL;
        int size;
 
-       if (rta == NULL || rtattr_parse_nested(tb, TCA_POLICE_MAX, rta) < 0)
+       if (nla == NULL)
                return -EINVAL;
 
-       if (tb[TCA_POLICE_TBF-1] == NULL)
-               return -EINVAL;
-       size = RTA_PAYLOAD(tb[TCA_POLICE_TBF-1]);
-       if (size != sizeof(*parm) && size != sizeof(struct tc_police_compat))
-               return -EINVAL;
-       parm = RTA_DATA(tb[TCA_POLICE_TBF-1]);
+       err = nla_parse_nested(tb, TCA_POLICE_MAX, nla, police_policy);
+       if (err < 0)
+               return err;
 
-       if (tb[TCA_POLICE_RESULT-1] != NULL &&
-           RTA_PAYLOAD(tb[TCA_POLICE_RESULT-1]) != sizeof(u32))
+       if (tb[TCA_POLICE_TBF] == NULL)
                return -EINVAL;
-       if (tb[TCA_POLICE_RESULT-1] != NULL &&
-           RTA_PAYLOAD(tb[TCA_POLICE_RESULT-1]) != sizeof(u32))
+       size = nla_len(tb[TCA_POLICE_TBF]);
+       if (size != sizeof(*parm) && size != sizeof(struct tc_police_compat))
                return -EINVAL;
+       parm = nla_data(tb[TCA_POLICE_TBF]);
 
        if (parm->index) {
                struct tcf_common *pc;
@@ -174,20 +183,34 @@ static int tcf_act_police_locate(struct rtattr *rta, struct rtattr *est,
 override:
        if (parm->rate.rate) {
                err = -ENOMEM;
-               R_tab = qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE-1]);
+               R_tab = qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE]);
                if (R_tab == NULL)
                        goto failure;
+
                if (parm->peakrate.rate) {
                        P_tab = qdisc_get_rtab(&parm->peakrate,
-                                              tb[TCA_POLICE_PEAKRATE-1]);
-                       if (P_tab == NULL) {
-                               qdisc_put_rtab(R_tab);
+                                              tb[TCA_POLICE_PEAKRATE]);
+                       if (P_tab == NULL)
                                goto failure;
-                       }
                }
        }
-       /* No failure allowed after this point */
+
        spin_lock_bh(&police->tcf_lock);
+       if (est) {
+               err = gen_replace_estimator(&police->tcf_bstats,
+                                           &police->tcf_rate_est,
+                                           &police->tcf_lock, est);
+               if (err)
+                       goto failure_unlock;
+       } else if (tb[TCA_POLICE_AVRATE] &&
+                  (ret == ACT_P_CREATED ||
+                   !gen_estimator_active(&police->tcf_bstats,
+                                         &police->tcf_rate_est))) {
+               err = -EINVAL;
+               goto failure_unlock;
+       }
+
+       /* No failure allowed after this point */
        if (R_tab != NULL) {
                qdisc_put_rtab(police->tcfp_R_tab);
                police->tcfp_R_tab = R_tab;
@@ -197,8 +220,8 @@ override:
                police->tcfp_P_tab = P_tab;
        }
 
-       if (tb[TCA_POLICE_RESULT-1])
-               police->tcfp_result = *(u32*)RTA_DATA(tb[TCA_POLICE_RESULT-1]);
+       if (tb[TCA_POLICE_RESULT])
+               police->tcfp_result = nla_get_u32(tb[TCA_POLICE_RESULT]);
        police->tcfp_toks = police->tcfp_burst = parm->burst;
        police->tcfp_mtu = parm->mtu;
        if (police->tcfp_mtu == 0) {
@@ -210,13 +233,8 @@ override:
                police->tcfp_ptoks = L2T_P(police, police->tcfp_mtu);
        police->tcf_action = parm->action;
 
-       if (tb[TCA_POLICE_AVRATE-1])
-               police->tcfp_ewma_rate =
-                       *(u32*)RTA_DATA(tb[TCA_POLICE_AVRATE-1]);
-       if (est)
-               gen_replace_estimator(&police->tcf_bstats,
-                                     &police->tcf_rate_est,
-                                     &police->tcf_lock, est);
+       if (tb[TCA_POLICE_AVRATE])
+               police->tcfp_ewma_rate = nla_get_u32(tb[TCA_POLICE_AVRATE]);
 
        spin_unlock_bh(&police->tcf_lock);
        if (ret != ACT_P_CREATED)
@@ -234,7 +252,13 @@ override:
        a->priv = police;
        return ret;
 
+failure_unlock:
+       spin_unlock_bh(&police->tcf_lock);
 failure:
+       if (P_tab)
+               qdisc_put_rtab(P_tab);
+       if (R_tab)
+               qdisc_put_rtab(R_tab);
        if (ret == ACT_P_CREATED)
                kfree(police);
        return err;
@@ -258,7 +282,7 @@ static int tcf_act_police_cleanup(struct tc_action *a, int bind)
        return ret;
 }
 
-static int tcf_act_police(struct sk_buff *skb, struct tc_action *a,
+static int tcf_act_police(struct sk_buff *skb, const struct tc_action *a,
                          struct tcf_result *res)
 {
        struct tcf_police *police = a->priv;
@@ -268,17 +292,18 @@ static int tcf_act_police(struct sk_buff *skb, struct tc_action *a,
 
        spin_lock(&police->tcf_lock);
 
-       police->tcf_bstats.bytes += skb->len;
-       police->tcf_bstats.packets++;
+       bstats_update(&police->tcf_bstats, skb);
 
        if (police->tcfp_ewma_rate &&
            police->tcf_rate_est.bps >= police->tcfp_ewma_rate) {
                police->tcf_qstats.overlimits++;
+               if (police->tcf_action == TC_ACT_SHOT)
+                       police->tcf_qstats.drops++;
                spin_unlock(&police->tcf_lock);
                return police->tcf_action;
        }
 
-       if (skb->len <= police->tcfp_mtu) {
+       if (qdisc_pkt_len(skb) <= police->tcfp_mtu) {
                if (police->tcfp_R_tab == NULL) {
                        spin_unlock(&police->tcf_lock);
                        return police->tcfp_result;
@@ -291,12 +316,12 @@ static int tcf_act_police(struct sk_buff *skb, struct tc_action *a,
                        ptoks = toks + police->tcfp_ptoks;
                        if (ptoks > (long)L2T_P(police, police->tcfp_mtu))
                                ptoks = (long)L2T_P(police, police->tcfp_mtu);
-                       ptoks -= L2T_P(police, skb->len);
+                       ptoks -= L2T_P(police, qdisc_pkt_len(skb));
                }
                toks += police->tcfp_toks;
                if (toks > (long)police->tcfp_burst)
                        toks = police->tcfp_burst;
-               toks -= L2T(police, skb->len);
+               toks -= L2T(police, qdisc_pkt_len(skb));
                if ((toks|ptoks) >= 0) {
                        police->tcfp_t_c = now;
                        police->tcfp_toks = toks;
@@ -307,6 +332,8 @@ static int tcf_act_police(struct sk_buff *skb, struct tc_action *a,
        }
 
        police->tcf_qstats.overlimits++;
+       if (police->tcf_action == TC_ACT_SHOT)
+               police->tcf_qstats.drops++;
        spin_unlock(&police->tcf_lock);
        return police->tcf_action;
 }
@@ -316,31 +343,30 @@ tcf_act_police_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
 {
        unsigned char *b = skb_tail_pointer(skb);
        struct tcf_police *police = a->priv;
-       struct tc_police opt;
-
-       opt.index = police->tcf_index;
-       opt.action = police->tcf_action;
-       opt.mtu = police->tcfp_mtu;
-       opt.burst = police->tcfp_burst;
-       opt.refcnt = police->tcf_refcnt - ref;
-       opt.bindcnt = police->tcf_bindcnt - bind;
+       struct tc_police opt = {
+               .index = police->tcf_index,
+               .action = police->tcf_action,
+               .mtu = police->tcfp_mtu,
+               .burst = police->tcfp_burst,
+               .refcnt = police->tcf_refcnt - ref,
+               .bindcnt = police->tcf_bindcnt - bind,
+       };
+
        if (police->tcfp_R_tab)
                opt.rate = police->tcfp_R_tab->rate;
-       else
-               memset(&opt.rate, 0, sizeof(opt.rate));
        if (police->tcfp_P_tab)
                opt.peakrate = police->tcfp_P_tab->rate;
-       else
-               memset(&opt.peakrate, 0, sizeof(opt.peakrate));
-       RTA_PUT(skb, TCA_POLICE_TBF, sizeof(opt), &opt);
-       if (police->tcfp_result)
-               RTA_PUT(skb, TCA_POLICE_RESULT, sizeof(int),
-                       &police->tcfp_result);
-       if (police->tcfp_ewma_rate)
-               RTA_PUT(skb, TCA_POLICE_AVRATE, 4, &police->tcfp_ewma_rate);
+       if (nla_put(skb, TCA_POLICE_TBF, sizeof(opt), &opt))
+               goto nla_put_failure;
+       if (police->tcfp_result &&
+           nla_put_u32(skb, TCA_POLICE_RESULT, police->tcfp_result))
+               goto nla_put_failure;
+       if (police->tcfp_ewma_rate &&
+           nla_put_u32(skb, TCA_POLICE_AVRATE, police->tcfp_ewma_rate))
+               goto nla_put_failure;
        return skb->len;
 
-rtattr_failure:
+nla_put_failure:
        nlmsg_trim(skb, b);
        return -1;
 }