netfilter: ip6table_raw: fix table priority
[safe/jmp/linux-2.6] / net / core / gen_estimator.c
index daadbcc..493775f 100644 (file)
@@ -31,6 +31,7 @@
 #include <linux/skbuff.h>
 #include <linux/rtnetlink.h>
 #include <linux/init.h>
+#include <linux/rbtree.h>
 #include <net/sock.h>
 #include <net/gen_stats.h>
 
@@ -65,9 +66,9 @@
 
    NOTES.
 
-   * The stored value for avbps is scaled by 2^5, so that maximal
-     rate is ~1Gbit, avpps is scaled by 2^10.
-
+   * avbps is scaled by 2^5, avpps is scaled by 2^10.
+   * both values are reported as 32 bit unsigned values. bps can
+     overflow for fast links : max speed being 34360Mbit/sec
    * Minimal interval is HZ/4=250msec (it is the greatest common divisor
      for HZ=100 and HZ=1024 8)), maximal interval
      is (HZ*2^EST_MAX_INTERVAL)/4 = 8sec. Shorter intervals
 struct gen_estimator
 {
        struct list_head        list;
-       struct gnet_stats_basic *bstats;
+       struct gnet_stats_basic_packed  *bstats;
        struct gnet_stats_rate_est      *rate_est;
        spinlock_t              *stats_lock;
        int                     ewma_log;
        u64                     last_bytes;
+       u64                     avbps;
        u32                     last_packets;
        u32                     avpps;
-       u32                     avbps;
        struct rcu_head         e_rcu;
+       struct rb_node          node;
 };
 
 struct gen_estimator_head
@@ -102,6 +104,9 @@ static struct gen_estimator_head elist[EST_MAX_INTERVAL+1];
 /* Protects against NULL dereference */
 static DEFINE_RWLOCK(est_lock);
 
+/* Protects against soft lockup during large deletion */
+static struct rb_root est_root = RB_ROOT;
+
 static void est_timer(unsigned long arg)
 {
        int idx = (int)arg;
@@ -110,6 +115,7 @@ static void est_timer(unsigned long arg)
        rcu_read_lock();
        list_for_each_entry_rcu(e, &elist[idx].list, list) {
                u64 nbytes;
+               u64 brate;
                u32 npackets;
                u32 rate;
 
@@ -120,14 +126,14 @@ static void est_timer(unsigned long arg)
 
                nbytes = e->bstats->bytes;
                npackets = e->bstats->packets;
-               rate = (nbytes - e->last_bytes)<<(7 - idx);
+               brate = (nbytes - e->last_bytes)<<(7 - idx);
                e->last_bytes = nbytes;
-               e->avbps += ((long)rate - (long)e->avbps) >> e->ewma_log;
+               e->avbps += (brate >> e->ewma_log) - (e->avbps >> e->ewma_log);
                e->rate_est->bps = (e->avbps+0xF)>>5;
 
                rate = (npackets - e->last_packets)<<(12 - idx);
                e->last_packets = npackets;
-               e->avpps += ((long)rate - (long)e->avpps) >> e->ewma_log;
+               e->avpps += (rate >> e->ewma_log) - (e->avpps >> e->ewma_log);
                e->rate_est->pps = (e->avpps+0x1FF)>>10;
 skip:
                read_unlock(&est_lock);
@@ -135,10 +141,50 @@ skip:
        }
 
        if (!list_empty(&elist[idx].list))
-               mod_timer(&elist[idx].timer, jiffies + ((HZ<<idx)/4));
+               mod_timer(&elist[idx].timer, jiffies + ((HZ/4) << idx));
        rcu_read_unlock();
 }
 
+static void gen_add_node(struct gen_estimator *est)
+{
+       struct rb_node **p = &est_root.rb_node, *parent = NULL;
+
+       while (*p) {
+               struct gen_estimator *e;
+
+               parent = *p;
+               e = rb_entry(parent, struct gen_estimator, node);
+
+               if (est->bstats > e->bstats)
+                       p = &parent->rb_right;
+               else
+                       p = &parent->rb_left;
+       }
+       rb_link_node(&est->node, parent, p);
+       rb_insert_color(&est->node, &est_root);
+}
+
+static
+struct gen_estimator *gen_find_node(const struct gnet_stats_basic_packed *bstats,
+                                   const struct gnet_stats_rate_est *rate_est)
+{
+       struct rb_node *p = est_root.rb_node;
+
+       while (p) {
+               struct gen_estimator *e;
+
+               e = rb_entry(p, struct gen_estimator, node);
+
+               if (bstats > e->bstats)
+                       p = p->rb_right;
+               else if (bstats < e->bstats || rate_est != e->rate_est)
+                       p = p->rb_left;
+               else
+                       return e;
+       }
+       return NULL;
+}
+
 /**
  * gen_new_estimator - create a new rate estimator
  * @bstats: basic statistics
@@ -156,16 +202,16 @@ skip:
  *
  * NOTE: Called under rtnl_mutex
  */
-int gen_new_estimator(struct gnet_stats_basic *bstats,
+int gen_new_estimator(struct gnet_stats_basic_packed *bstats,
                      struct gnet_stats_rate_est *rate_est,
                      spinlock_t *stats_lock,
-                     struct rtattr *opt)
+                     struct nlattr *opt)
 {
        struct gen_estimator *est;
-       struct gnet_estimator *parm = RTA_DATA(opt);
+       struct gnet_estimator *parm = nla_data(opt);
        int idx;
 
-       if (RTA_PAYLOAD(opt) < sizeof(*parm))
+       if (nla_len(opt) < sizeof(*parm))
                return -EINVAL;
 
        if (parm->interval < -2 || parm->interval > 3)
@@ -191,11 +237,14 @@ int gen_new_estimator(struct gnet_stats_basic *bstats,
        }
 
        if (list_empty(&elist[idx].list))
-               mod_timer(&elist[idx].timer, jiffies + ((HZ<<idx)/4));
+               mod_timer(&elist[idx].timer, jiffies + ((HZ/4) << idx));
 
        list_add_rcu(&est->list, &elist[idx].list);
+       gen_add_node(est);
+
        return 0;
 }
+EXPORT_SYMBOL(gen_new_estimator);
 
 static void __gen_kill_estimator(struct rcu_head *head)
 {
@@ -209,39 +258,30 @@ static void __gen_kill_estimator(struct rcu_head *head)
  * @bstats: basic statistics
  * @rate_est: rate estimator statistics
  *
- * Removes the rate estimator specified by &bstats and &rate_est
- * and deletes the timer.
+ * Removes the rate estimator specified by &bstats and &rate_est.
  *
  * NOTE: Called under rtnl_mutex
  */
-void gen_kill_estimator(struct gnet_stats_basic *bstats,
-       struct gnet_stats_rate_est *rate_est)
+void gen_kill_estimator(struct gnet_stats_basic_packed *bstats,
+                       struct gnet_stats_rate_est *rate_est)
 {
-       int idx;
-       struct gen_estimator *e, *n;
-
-       for (idx=0; idx <= EST_MAX_INTERVAL; idx++) {
-
-               /* Skip non initialized indexes */
-               if (!elist[idx].timer.function)
-                       continue;
+       struct gen_estimator *e;
 
-               list_for_each_entry_safe(e, n, &elist[idx].list, list) {
-                       if (e->rate_est != rate_est || e->bstats != bstats)
-                               continue;
+       while ((e = gen_find_node(bstats, rate_est))) {
+               rb_erase(&e->node, &est_root);
 
-                       write_lock_bh(&est_lock);
-                       e->bstats = NULL;
-                       write_unlock_bh(&est_lock);
+               write_lock_bh(&est_lock);
+               e->bstats = NULL;
+               write_unlock_bh(&est_lock);
 
-                       list_del_rcu(&e->list);
-                       call_rcu(&e->e_rcu, __gen_kill_estimator);
-               }
+               list_del_rcu(&e->list);
+               call_rcu(&e->e_rcu, __gen_kill_estimator);
        }
 }
+EXPORT_SYMBOL(gen_kill_estimator);
 
 /**
- * gen_replace_estimator - replace rate estimator configruation
+ * gen_replace_estimator - replace rate estimator configuration
  * @bstats: basic statistics
  * @rate_est: rate estimator statistics
  * @stats_lock: statistics lock
@@ -252,16 +292,27 @@ void gen_kill_estimator(struct gnet_stats_basic *bstats,
  *
  * Returns 0 on success or a negative error code.
  */
-int
-gen_replace_estimator(struct gnet_stats_basic *bstats,
-       struct gnet_stats_rate_est *rate_est, spinlock_t *stats_lock,
-       struct rtattr *opt)
+int gen_replace_estimator(struct gnet_stats_basic_packed *bstats,
+                         struct gnet_stats_rate_est *rate_est,
+                         spinlock_t *stats_lock, struct nlattr *opt)
 {
-    gen_kill_estimator(bstats, rate_est);
-    return gen_new_estimator(bstats, rate_est, stats_lock, opt);
+       gen_kill_estimator(bstats, rate_est);
+       return gen_new_estimator(bstats, rate_est, stats_lock, opt);
 }
+EXPORT_SYMBOL(gen_replace_estimator);
 
+/**
+ * gen_estimator_active - test if estimator is currently in use
+ * @bstats: basic statistics
+ * @rate_est: rate estimator statistics
+ *
+ * Returns true if estimator is active, and false if not.
+ */
+bool gen_estimator_active(const struct gnet_stats_basic_packed *bstats,
+                         const struct gnet_stats_rate_est *rate_est)
+{
+       ASSERT_RTNL();
 
-EXPORT_SYMBOL(gen_kill_estimator);
-EXPORT_SYMBOL(gen_new_estimator);
-EXPORT_SYMBOL(gen_replace_estimator);
+       return gen_find_node(bstats, rate_est) != NULL;
+}
+EXPORT_SYMBOL(gen_estimator_active);