ipv4: tcp: set unicast_sock uc_ttl to -1
[linux-3.10.git] / net / sched / act_police.c
index 32c3f9d..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;
@@ -112,11 +112,15 @@ 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 const struct nla_policy police_policy[TCA_POLICE_MAX + 1] = {
@@ -129,7 +133,7 @@ static const struct nla_policy police_policy[TCA_POLICE_MAX + 1] = {
 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 nlattr *tb[TCA_POLICE_MAX + 1];
        struct tc_police *parm;
@@ -182,17 +186,31 @@ override:
                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]);
-                       if (P_tab == NULL) {
-                               qdisc_put_rtab(R_tab);
+                       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;
@@ -217,10 +235,6 @@ override:
 
        if (tb[TCA_POLICE_AVRATE])
                police->tcfp_ewma_rate = nla_get_u32(tb[TCA_POLICE_AVRATE]);
-       if (est)
-               gen_replace_estimator(&police->tcf_bstats,
-                                     &police->tcf_rate_est,
-                                     &police->tcf_lock, est);
 
        spin_unlock_bh(&police->tcf_lock);
        if (ret != ACT_P_CREATED)
@@ -238,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;
@@ -262,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;
@@ -272,12 +292,13 @@ static int tcf_act_police(struct sk_buff *skb, struct tc_action *a,
 
        spin_lock(&police->tcf_lock);
 
-       police->tcf_bstats.bytes += qdisc_pkt_len(skb);
-       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;
        }
@@ -311,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;
 }
@@ -320,27 +343,27 @@ 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));
-       NLA_PUT(skb, TCA_POLICE_TBF, sizeof(opt), &opt);
-       if (police->tcfp_result)
-               NLA_PUT_U32(skb, TCA_POLICE_RESULT, police->tcfp_result);
-       if (police->tcfp_ewma_rate)
-               NLA_PUT_U32(skb, TCA_POLICE_AVRATE, 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;
 
 nla_put_failure: