[PATCH 04/12] Minor code cleanups in filtering code.

Sergey Popovich popovich_sergei at mail.ru
Mon Sep 30 20:27:31 CEST 2013


  * Remove duplicate f_eval_asn() declaration: already in filter/filter.h

  * Move pm_format() from filter/filter.c to nest/a-path.c where other
    pm_* functions reside and also similar *_set_format() family already
    resides in nest/a-set.c.

  * Move tree_compare() from filter/filter.c to filter/tree.c and make it
    static as it only used in qsort(). Also remove definition from
filter/filter.h.

  * Inline fprefix_get_bounds() from filter/filter.c in trie_add_prefix() at
    filter/filter.h as there is only one place where this code used.

  * Move tree_node_print() and tree_print() from filter/filter.c to
filter/tree.c
    Make tree_node_print() static. Add global comment to tree_print()
and declare
    it in filter.h. This is similar to trie_print() from filter/trie.c

  * Make printing of zero prefix in trie_print() at filter/trie.c to print
    AFI specific value.
---
 filter/filter.c |   93
-------------------------------------------------------
 filter/filter.h |   19 +++++++++---
 filter/tree.c   |   41 ++++++++++++++++++++++++
 filter/trie.c   |    2 +-
 nest/a-path.c   |   38 +++++++++++++++++++++++
 nest/attrs.h    |    1 +
 6 files changed, 95 insertions(+), 99 deletions(-)

diff --git a/filter/filter.c b/filter/filter.c
index c089d94..7e91c71 100644
--- a/filter/filter.c
+++ b/filter/filter.c
@@ -58,46 +58,6 @@ adata_empty(struct linpool *pool, int l)
   return res;
 }
 -u32 f_eval_asn(struct f_inst *expr);
-
-static void
-pm_format(struct f_path_mask *p, byte *buf, unsigned int size)
-{
-  byte *end = buf + size - 16;
-
-  while (p)
-    {
-      if (buf > end)
-	{
-	  strcpy(buf, " ...");
-	  return;
-	}
-
-      switch(p->kind)
-	{
-	case PM_ASN:
-	  buf += bsprintf(buf, " %u", p->val);
-	  break;
-
-	case PM_QUESTION:
-	  buf += bsprintf(buf, " ?");
-	  break;
-
-	case PM_ASTERISK:
-	  buf += bsprintf(buf, " *");
-	  break;
-
-	case PM_ASN_EXPR:
-	  buf += bsprintf(buf, " %u", f_eval_asn((struct f_inst *) p->val));
-	  break;
-	}
-
-      p = p->next;
-    }
-
-  *buf = 0;
-}
-
 static inline int
 int_cmp(int i1, int i2)
 {
@@ -291,30 +251,6 @@ val_same(struct f_val v1, struct f_val v2)
   }
 }
 -int -tree_compare(const void *p1, const void *p2)
-{
-  return val_compare((* (struct f_tree **) p1)->from, (* (struct f_tree
**) p2)->from);
-}
-
-void
-fprefix_get_bounds(struct f_prefix *px, int *l, int *h)
-{
-  *l = *h = px->len & LEN_MASK;
-
-  if (px->len & LEN_MINUS)
-    *l = 0;
-
-  else if (px->len & LEN_PLUS)
-    *h = MAX_PREFIX_LENGTH;
-
-  else if (px->len & LEN_RANGE)
-    {
-      *l = 0xff & (px->len >> 16);
-      *h = 0xff & (px->len >> 8);
-    }
-}
-
 /*
  * val_simple_in_range - check if @v1 ~ @v2 for everything except sets
  */ @@ -535,35 +471,6 @@ val_in_range(struct f_val v1, struct f_val v2)
   return CMP_ERROR;
 }
 -static void
-tree_node_print(struct f_tree *t, char **sep)
-{
-  if (t == NULL)
-    return;
-
-  tree_node_print(t->left, sep);
-
-  logn(*sep);
-  val_print(t->from);
-  if (val_compare(t->from, t->to) != 0)
-    {
-      logn( ".." );
-      val_print(t->to);
-    }
-  *sep = ", ";
-
-  tree_node_print(t->right, sep);
-}
-
-static void
-tree_print(struct f_tree *t)
-{
-  char *sep = "";
-  logn( "[" );
-  tree_node_print(t, &sep);
-  logn( "] " );
-}
-
 /*
  * val_print - format filter value
  */
diff --git a/filter/filter.h b/filter/filter.h
index a3de530..9e60ef8 100644
--- a/filter/filter.h
+++ b/filter/filter.h
@@ -78,6 +78,7 @@ struct f_inst *f_generate_roa_check(struct symbol
*sym, struct f_inst *prefix, s
 struct f_tree *build_tree(struct f_tree *);
 struct f_tree *find_tree(struct f_tree *t, struct f_val val);
 int same_tree(struct f_tree *t1, struct f_tree *t2);
+void tree_print(struct f_tree *t);
  struct f_trie *f_new_trie(linpool *lp);
 void trie_add_prefix(struct f_trie *t, ip_addr px, int plen, int l, int h);
@@ -85,13 +86,23 @@ int trie_match_prefix(struct f_trie *t, ip_addr px,
int plen);
 int trie_same(struct f_trie *t1, struct f_trie *t2);
 void trie_print(struct f_trie *t);
 -void fprefix_get_bounds(struct f_prefix *px, int *l, int *h);
-
 static inline void
 trie_add_fprefix(struct f_trie *t, struct f_prefix *px)
 {
   int l, h;
-  fprefix_get_bounds(px, &l, &h);
+
+  l = h = px->len & LEN_MASK;
+
+  if (px->len & LEN_MINUS)
+    l = 0;
+  else if (px->len & LEN_PLUS)
+    h = MAX_PREFIX_LENGTH;
+  else if (px->len & LEN_RANGE)
+    {
+      l = 0xff & (px->len >> 16);
+      h = 0xff & (px->len >> 8);
+    }
+
   trie_add_prefix(t, px->ip, px->len & LEN_MASK, l, h);
 }
 @@ -117,8 +128,6 @@ int i_same(struct f_inst *f1, struct f_inst *f2);
  int val_compare(struct f_val v1, struct f_val v2);
 int val_same(struct f_val v1, struct f_val v2);
-int tree_compare(const void *p1, const void *p2);
-
 void val_print(struct f_val v);
  #define F_NOP 0
diff --git a/filter/tree.c b/filter/tree.c
index f6ab75b..4690f85 100644
--- a/filter/tree.c
+++ b/filter/tree.c
@@ -53,6 +53,12 @@ build_tree_rec(struct f_tree **buf, int l, int h)
   return n;
 }
 +static int
+tree_compare(const void *p1, const void *p2)
+{
+  return val_compare((* (struct f_tree **) p1)->from,
+                     (* (struct f_tree **) p2)->from);
+}
  /**
  * build_tree
@@ -132,3 +138,38 @@ same_tree(struct f_tree *t1, struct f_tree *t2)
     return 0;
   return 1;
 }
+
+static void
+tree_node_print(struct f_tree *t, char **sep)
+{
+  if (t == NULL)
+    return;
+
+  tree_node_print(t->left, sep);
+
+  logn(*sep);
+  val_print(t->from);
+  if (val_compare(t->from, t->to) != 0)
+    {
+      logn( ".." );
+      val_print(t->to);
+    }
+  *sep = ", ";
+
+  tree_node_print(t->right, sep);
+}
+
+/**
+ * tree_print
+ * @t: tree to be printed
+ *
+ * Prints the tree to the log buffer.
+ */
+void
+tree_print(struct f_tree *t)
+{
+  char *sep = "";
+  logn( "[" );
+  tree_node_print(t, &sep);
+  logn( "] " );
+}
diff --git a/filter/trie.c b/filter/trie.c
index 581332c..f42afb8 100644
--- a/filter/trie.c
+++ b/filter/trie.c
@@ -293,7 +293,7 @@ trie_print(struct f_trie *t)
   logn("[");
   if (t->zero)
     {
-      logn("0.0.0.0/0");
+      logn("%I/%d", IPA_NONE, 0);
       sep = ", ";
     }
   trie_node_print(&t->root, &sep);
diff --git a/nest/a-path.c b/nest/a-path.c
index 1679886..cf129c1 100644
--- a/nest/a-path.c
+++ b/nest/a-path.c
@@ -434,6 +434,44 @@ pm_mark(struct pm_pos *pos, int i, int plen, int
*nl, int *nh)
     *nh = j;
 }
 +void
+pm_format(struct f_path_mask *p, byte *buf, unsigned int size)
+{
+  byte *end = buf + size - 16;
+
+  while (p)
+    {
+      if (buf > end)
+	{
+	  strcpy(buf, " ...");
+	  return;
+	}
+
+      switch(p->kind)
+	{
+	case PM_ASN:
+	  buf += bsprintf(buf, " %u", p->val);
+	  break;
+
+	case PM_QUESTION:
+	  buf += bsprintf(buf, " ?");
+	  break;
+
+	case PM_ASTERISK:
+	  buf += bsprintf(buf, " *");
+	  break;
+
+	case PM_ASN_EXPR:
+	  buf += bsprintf(buf, " %u", f_eval_asn((struct f_inst *) p->val));
+	  break;
+	}
+
+      p = p->next;
+    }
+
+  *buf = 0;
+}
+
 /* AS path matching is nontrivial. Because AS path can
  * contain sets, it is not a plain wildcard matching. A set   * in an
AS path is interpreted as it might represent any
diff --git a/nest/attrs.h b/nest/attrs.h
index 38639ae..9468355 100644
--- a/nest/attrs.h
+++ b/nest/attrs.h
@@ -56,6 +56,7 @@ struct f_path_mask {
 };
  int as_path_match(struct adata *path, struct f_path_mask *mask);
+void pm_format(struct f_path_mask *p, byte *buf, unsigned int size);
  /* a-set.c */
 -- 1.7.10.4



More information about the Bird-users mailing list