Commit 91b9a277 authored by Olof Johansson's avatar Olof Johansson Committed by David S. Miller

[IPV4]: FIB Trie cleanups.

Below is a patch that cleans up some of this, supposedly without
changing any behaviour:

* Whitespace cleanups
* Introduce DBG()
* BUG_ON() instead of if () { BUG(); }
* Remove some of the deep nesting to make the code flow more
  comprehensible
* Some mask operations were simplified
Signed-off-by: default avatarOlof Johansson <olof@lixom.net>
Signed-off-by: default avatarRobert Olsson <robert.olsson@its.uu.se>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 7663f188
...@@ -89,27 +89,27 @@ typedef unsigned int t_key; ...@@ -89,27 +89,27 @@ typedef unsigned int t_key;
#define T_TNODE 0 #define T_TNODE 0
#define T_LEAF 1 #define T_LEAF 1
#define NODE_TYPE_MASK 0x1UL #define NODE_TYPE_MASK 0x1UL
#define NODE_PARENT(_node) \ #define NODE_PARENT(node) \
((struct tnode *)((_node)->_parent & ~NODE_TYPE_MASK)) ((struct tnode *)((node)->parent & ~NODE_TYPE_MASK))
#define NODE_SET_PARENT(_node, _ptr) \ #define NODE_SET_PARENT(node, ptr) \
((_node)->_parent = (((unsigned long)(_ptr)) | \ ((node)->parent = (((unsigned long)(ptr)) | \
((_node)->_parent & NODE_TYPE_MASK))) ((node)->parent & NODE_TYPE_MASK)))
#define NODE_INIT_PARENT(_node, _type) \ #define NODE_INIT_PARENT(node, type) \
((_node)->_parent = (_type)) ((node)->parent = (type))
#define NODE_TYPE(_node) \ #define NODE_TYPE(node) \
((_node)->_parent & NODE_TYPE_MASK) ((node)->parent & NODE_TYPE_MASK)
#define IS_TNODE(n) (!(n->_parent & T_LEAF)) #define IS_TNODE(n) (!(n->parent & T_LEAF))
#define IS_LEAF(n) (n->_parent & T_LEAF) #define IS_LEAF(n) (n->parent & T_LEAF)
struct node { struct node {
t_key key; t_key key;
unsigned long _parent; unsigned long parent;
}; };
struct leaf { struct leaf {
t_key key; t_key key;
unsigned long _parent; unsigned long parent;
struct hlist_head list; struct hlist_head list;
}; };
...@@ -120,13 +120,13 @@ struct leaf_info { ...@@ -120,13 +120,13 @@ struct leaf_info {
}; };
struct tnode { struct tnode {
t_key key; t_key key;
unsigned long _parent; unsigned long parent;
unsigned short pos:5; /* 2log(KEYLENGTH) bits needed */ unsigned short pos:5; /* 2log(KEYLENGTH) bits needed */
unsigned short bits:5; /* 2log(KEYLENGTH) bits needed */ unsigned short bits:5; /* 2log(KEYLENGTH) bits needed */
unsigned short full_children; /* KEYLENGTH bits needed */ unsigned short full_children; /* KEYLENGTH bits needed */
unsigned short empty_children; /* KEYLENGTH bits needed */ unsigned short empty_children; /* KEYLENGTH bits needed */
struct node *child[0]; struct node *child[0];
}; };
#ifdef CONFIG_IP_FIB_TRIE_STATS #ifdef CONFIG_IP_FIB_TRIE_STATS
...@@ -150,16 +150,18 @@ struct trie_stat { ...@@ -150,16 +150,18 @@ struct trie_stat {
}; };
struct trie { struct trie {
struct node *trie; struct node *trie;
#ifdef CONFIG_IP_FIB_TRIE_STATS #ifdef CONFIG_IP_FIB_TRIE_STATS
struct trie_use_stats stats; struct trie_use_stats stats;
#endif #endif
int size; int size;
unsigned int revision; unsigned int revision;
}; };
static int trie_debug = 0; static int trie_debug = 0;
#define DBG(x...) do { if (trie_debug) printk(x); } while (0)
static int tnode_full(struct tnode *tn, struct node *n); static int tnode_full(struct tnode *tn, struct node *n);
static void put_child(struct trie *t, struct tnode *tn, int i, struct node *n); static void put_child(struct trie *t, struct tnode *tn, int i, struct node *n);
static void tnode_put_child_reorg(struct tnode *tn, int i, struct node *n, int wasfull); static void tnode_put_child_reorg(struct tnode *tn, int i, struct node *n, int wasfull);
...@@ -171,56 +173,31 @@ static void tnode_free(struct tnode *tn); ...@@ -171,56 +173,31 @@ static void tnode_free(struct tnode *tn);
static void trie_dump_seq(struct seq_file *seq, struct trie *t); static void trie_dump_seq(struct seq_file *seq, struct trie *t);
extern struct fib_alias *fib_find_alias(struct list_head *fah, u8 tos, u32 prio); extern struct fib_alias *fib_find_alias(struct list_head *fah, u8 tos, u32 prio);
extern int fib_detect_death(struct fib_info *fi, int order, extern int fib_detect_death(struct fib_info *fi, int order,
struct fib_info **last_resort, int *last_idx, int *dflt); struct fib_info **last_resort, int *last_idx, int *dflt);
extern void rtmsg_fib(int event, u32 key, struct fib_alias *fa, int z, int tb_id, extern void rtmsg_fib(int event, u32 key, struct fib_alias *fa, int z, int tb_id,
struct nlmsghdr *n, struct netlink_skb_parms *req); struct nlmsghdr *n, struct netlink_skb_parms *req);
static kmem_cache_t *fn_alias_kmem; static kmem_cache_t *fn_alias_kmem;
static struct trie *trie_local = NULL, *trie_main = NULL; static struct trie *trie_local = NULL, *trie_main = NULL;
static void trie_bug(char *err)
{
printk("Trie Bug: %s\n", err);
BUG();
}
static inline struct node *tnode_get_child(struct tnode *tn, int i) static inline struct node *tnode_get_child(struct tnode *tn, int i)
{ {
if (i >= 1<<tn->bits) BUG_ON(i >= 1 << tn->bits);
trie_bug("tnode_get_child");
return tn->child[i]; return tn->child[i];
} }
static inline int tnode_child_length(struct tnode *tn) static inline int tnode_child_length(struct tnode *tn)
{ {
return 1<<tn->bits; return 1 << tn->bits;
} }
/*
_________________________________________________________________
| i | i | i | i | i | i | i | N | N | N | S | S | S | S | S | C |
----------------------------------------------------------------
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
_________________________________________________________________
| C | C | C | u | u | u | u | u | u | u | u | u | u | u | u | u |
-----------------------------------------------------------------
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
tp->pos = 7
tp->bits = 3
n->pos = 15
n->bits=4
KEYLENGTH=32
*/
static inline t_key tkey_extract_bits(t_key a, int offset, int bits) static inline t_key tkey_extract_bits(t_key a, int offset, int bits)
{ {
if (offset < KEYLENGTH) if (offset < KEYLENGTH)
return ((t_key)(a << offset)) >> (KEYLENGTH - bits); return ((t_key)(a << offset)) >> (KEYLENGTH - bits);
else else
return 0; return 0;
} }
...@@ -233,8 +210,8 @@ static inline int tkey_sub_equals(t_key a, int offset, int bits, t_key b) ...@@ -233,8 +210,8 @@ static inline int tkey_sub_equals(t_key a, int offset, int bits, t_key b)
{ {
if (bits == 0 || offset >= KEYLENGTH) if (bits == 0 || offset >= KEYLENGTH)
return 1; return 1;
bits = bits > KEYLENGTH ? KEYLENGTH : bits; bits = bits > KEYLENGTH ? KEYLENGTH : bits;
return ((a ^ b) << offset) >> (KEYLENGTH - bits) == 0; return ((a ^ b) << offset) >> (KEYLENGTH - bits) == 0;
} }
static inline int tkey_mismatch(t_key a, int offset, t_key b) static inline int tkey_mismatch(t_key a, int offset, t_key b)
...@@ -249,7 +226,7 @@ static inline int tkey_mismatch(t_key a, int offset, t_key b) ...@@ -249,7 +226,7 @@ static inline int tkey_mismatch(t_key a, int offset, t_key b)
return i; return i;
} }
/* Candiate for fib_semantics */ /* Candidate for fib_semantics */
static void fn_free_alias(struct fib_alias *fa) static void fn_free_alias(struct fib_alias *fa)
{ {
...@@ -295,7 +272,7 @@ static void fn_free_alias(struct fib_alias *fa) ...@@ -295,7 +272,7 @@ static void fn_free_alias(struct fib_alias *fa)
tp->pos = 7 tp->pos = 7
tp->bits = 3 tp->bits = 3
n->pos = 15 n->pos = 15
n->bits=4 n->bits = 4
First, let's just ignore the bits that come before the parent tp, that is First, let's just ignore the bits that come before the parent tp, that is
the bits from 0 to (tp->pos-1). They are *known* but at this point we do the bits from 0 to (tp->pos-1). They are *known* but at this point we do
...@@ -343,10 +320,13 @@ static struct leaf *leaf_new(void) ...@@ -343,10 +320,13 @@ static struct leaf *leaf_new(void)
static struct leaf_info *leaf_info_new(int plen) static struct leaf_info *leaf_info_new(int plen)
{ {
struct leaf_info *li = kmalloc(sizeof(struct leaf_info), GFP_KERNEL); struct leaf_info *li = kmalloc(sizeof(struct leaf_info), GFP_KERNEL);
if (li) {
li->plen = plen; if (!li)
INIT_LIST_HEAD(&li->falh); return NULL;
}
li->plen = plen;
INIT_LIST_HEAD(&li->falh);
return li; return li;
} }
...@@ -373,7 +353,7 @@ static struct tnode *tnode_alloc(unsigned int size) ...@@ -373,7 +353,7 @@ static struct tnode *tnode_alloc(unsigned int size)
static void __tnode_free(struct tnode *tn) static void __tnode_free(struct tnode *tn)
{ {
unsigned int size = sizeof(struct tnode) + unsigned int size = sizeof(struct tnode) +
(1<<tn->bits) * sizeof(struct node *); (1 << tn->bits) * sizeof(struct node *);
if (size <= PAGE_SIZE) if (size <= PAGE_SIZE)
kfree(tn); kfree(tn);
...@@ -387,7 +367,7 @@ static struct tnode* tnode_new(t_key key, int pos, int bits) ...@@ -387,7 +367,7 @@ static struct tnode* tnode_new(t_key key, int pos, int bits)
int sz = sizeof(struct tnode) + nchildren * sizeof(struct node *); int sz = sizeof(struct tnode) + nchildren * sizeof(struct node *);
struct tnode *tn = tnode_alloc(sz); struct tnode *tn = tnode_alloc(sz);
if (tn) { if (tn) {
memset(tn, 0, sz); memset(tn, 0, sz);
NODE_INIT_PARENT(tn, T_TNODE); NODE_INIT_PARENT(tn, T_TNODE);
tn->pos = pos; tn->pos = pos;
...@@ -397,29 +377,21 @@ static struct tnode* tnode_new(t_key key, int pos, int bits) ...@@ -397,29 +377,21 @@ static struct tnode* tnode_new(t_key key, int pos, int bits)
tn->empty_children = 1<<bits; tn->empty_children = 1<<bits;
} }
if (trie_debug > 0) DBG("AT %p s=%u %u\n", tn, (unsigned int) sizeof(struct tnode),
printk("AT %p s=%u %u\n", tn, (unsigned int) sizeof(struct tnode), (unsigned int) (sizeof(struct node) * 1<<bits));
(unsigned int) (sizeof(struct node) * 1<<bits));
return tn; return tn;
} }
static void tnode_free(struct tnode *tn) static void tnode_free(struct tnode *tn)
{ {
if (!tn) { BUG_ON(!tn);
trie_bug("tnode_free\n");
}
if (IS_LEAF(tn)) { if (IS_LEAF(tn)) {
free_leaf((struct leaf *)tn); free_leaf((struct leaf *)tn);
if (trie_debug > 0 ) DBG("FL %p \n", tn);
printk("FL %p \n", tn); } else {
}
else if (IS_TNODE(tn)) {
__tnode_free(tn); __tnode_free(tn);
if (trie_debug > 0 ) DBG("FT %p \n", tn);
printk("FT %p \n", tn);
}
else {
trie_bug("tnode_free\n");
} }
} }
...@@ -453,7 +425,7 @@ static void tnode_put_child_reorg(struct tnode *tn, int i, struct node *n, int w ...@@ -453,7 +425,7 @@ static void tnode_put_child_reorg(struct tnode *tn, int i, struct node *n, int w
if (i >= 1<<tn->bits) { if (i >= 1<<tn->bits) {
printk("bits=%d, i=%d\n", tn->bits, i); printk("bits=%d, i=%d\n", tn->bits, i);
trie_bug("tnode_put_child_reorg bits"); BUG();
} }
write_lock_bh(&fib_lock); write_lock_bh(&fib_lock);
chi = tn->child[i]; chi = tn->child[i];
...@@ -465,15 +437,15 @@ static void tnode_put_child_reorg(struct tnode *tn, int i, struct node *n, int w ...@@ -465,15 +437,15 @@ static void tnode_put_child_reorg(struct tnode *tn, int i, struct node *n, int w
tn->empty_children--; tn->empty_children--;
/* update fullChildren */ /* update fullChildren */
if (wasfull == -1) if (wasfull == -1)
wasfull = tnode_full(tn, chi); wasfull = tnode_full(tn, chi);
isfull = tnode_full(tn, n); isfull = tnode_full(tn, n);
if (wasfull && !isfull) if (wasfull && !isfull)
tn->full_children--; tn->full_children--;
else if (!wasfull && isfull) else if (!wasfull && isfull)
tn->full_children++; tn->full_children++;
if (n) if (n)
NODE_SET_PARENT(n, tn); NODE_SET_PARENT(n, tn);
...@@ -489,9 +461,8 @@ static struct node *resize(struct trie *t, struct tnode *tn) ...@@ -489,9 +461,8 @@ static struct node *resize(struct trie *t, struct tnode *tn)
if (!tn) if (!tn)
return NULL; return NULL;
if (trie_debug) DBG("In tnode_resize %p inflate_threshold=%d threshold=%d\n",
printk("In tnode_resize %p inflate_threshold=%d threshold=%d\n", tn, inflate_threshold, halve_threshold);
tn, inflate_threshold, halve_threshold);
/* No children */ /* No children */
if (tn->empty_children == tnode_child_length(tn)) { if (tn->empty_children == tnode_child_length(tn)) {
...@@ -501,20 +472,21 @@ static struct node *resize(struct trie *t, struct tnode *tn) ...@@ -501,20 +472,21 @@ static struct node *resize(struct trie *t, struct tnode *tn)
/* One child */ /* One child */
if (tn->empty_children == tnode_child_length(tn) - 1) if (tn->empty_children == tnode_child_length(tn) - 1)
for (i = 0; i < tnode_child_length(tn); i++) { for (i = 0; i < tnode_child_length(tn); i++) {
struct node *n;
write_lock_bh(&fib_lock); write_lock_bh(&fib_lock);
if (tn->child[i] != NULL) { n = tn->child[i];
if (!n) {
/* compress one level */
struct node *n = tn->child[i];
if (n)
NODE_INIT_PARENT(n, NODE_TYPE(n));
write_unlock_bh(&fib_lock); write_unlock_bh(&fib_lock);
tnode_free(tn); continue;
return n;
} }
/* compress one level */
NODE_INIT_PARENT(n, NODE_TYPE(n));
write_unlock_bh(&fib_lock); write_unlock_bh(&fib_lock);
tnode_free(tn);
return n;
} }
/* /*
* Double as long as the resulting node has a number of * Double as long as the resulting node has a number of
...@@ -566,16 +538,16 @@ static struct node *resize(struct trie *t, struct tnode *tn) ...@@ -566,16 +538,16 @@ static struct node *resize(struct trie *t, struct tnode *tn)
* *
* expand not_to_be_doubled and to_be_doubled, and shorten: * expand not_to_be_doubled and to_be_doubled, and shorten:
* 100 * (tnode_child_length(tn) - tn->empty_children + * 100 * (tnode_child_length(tn) - tn->empty_children +
* tn->full_children ) >= inflate_threshold * new_child_length * tn->full_children) >= inflate_threshold * new_child_length
* *
* expand new_child_length: * expand new_child_length:
* 100 * (tnode_child_length(tn) - tn->empty_children + * 100 * (tnode_child_length(tn) - tn->empty_children +
* tn->full_children ) >= * tn->full_children) >=
* inflate_threshold * tnode_child_length(tn) * 2 * inflate_threshold * tnode_child_length(tn) * 2
* *
* shorten again: * shorten again:
* 50 * (tn->full_children + tnode_child_length(tn) - * 50 * (tn->full_children + tnode_child_length(tn) -
* tn->empty_children ) >= inflate_threshold * * tn->empty_children) >= inflate_threshold *
* tnode_child_length(tn) * tnode_child_length(tn)
* *
*/ */
...@@ -624,20 +596,23 @@ static struct node *resize(struct trie *t, struct tnode *tn) ...@@ -624,20 +596,23 @@ static struct node *resize(struct trie *t, struct tnode *tn)
if (tn->empty_children == tnode_child_length(tn) - 1) if (tn->empty_children == tnode_child_length(tn) - 1)
for (i = 0; i < tnode_child_length(tn); i++) { for (i = 0; i < tnode_child_length(tn); i++) {
struct node *n;
write_lock_bh(&fib_lock);
if (tn->child[i] != NULL) {
/* compress one level */
struct node *n = tn->child[i];
if (n) write_lock_bh(&fib_lock);
NODE_INIT_PARENT(n, NODE_TYPE(n));
n = tn->child[i];
if (!n) {
write_unlock_bh(&fib_lock); write_unlock_bh(&fib_lock);
tnode_free(tn); continue;
return n;
} }
/* compress one level */
NODE_INIT_PARENT(n, NODE_TYPE(n));
write_unlock_bh(&fib_lock); write_unlock_bh(&fib_lock);
tnode_free(tn);
return n;
} }
return (struct node *) tn; return (struct node *) tn;
...@@ -650,8 +625,7 @@ static struct tnode *inflate(struct trie *t, struct tnode *tn, int *err) ...@@ -650,8 +625,7 @@ static struct tnode *inflate(struct trie *t, struct tnode *tn, int *err)
int olen = tnode_child_length(tn); int olen = tnode_child_length(tn);
int i; int i;
if (trie_debug) DBG("In inflate\n");
printk("In inflate\n");
tn = tnode_new(oldtnode->key, oldtnode->pos, oldtnode->bits + 1); tn = tnode_new(oldtnode->key, oldtnode->pos, oldtnode->bits + 1);
...@@ -666,8 +640,8 @@ static struct tnode *inflate(struct trie *t, struct tnode *tn, int *err) ...@@ -666,8 +640,8 @@ static struct tnode *inflate(struct trie *t, struct tnode *tn, int *err)
* fails. In case of failure we return the oldnode and inflate * fails. In case of failure we return the oldnode and inflate
* of tnode is ignored. * of tnode is ignored.
*/ */
for(i = 0; i < olen; i++) { for (i = 0; i < olen; i++) {
struct tnode *inode = (struct tnode *) tnode_get_child(oldtnode, i); struct tnode *inode = (struct tnode *) tnode_get_child(oldtnode, i);
if (inode && if (inode &&
...@@ -675,7 +649,6 @@ static struct tnode *inflate(struct trie *t, struct tnode *tn, int *err) ...@@ -675,7 +649,6 @@ static struct tnode *inflate(struct trie *t, struct tnode *tn, int *err)
inode->pos == oldtnode->pos + oldtnode->bits && inode->pos == oldtnode->pos + oldtnode->bits &&
inode->bits > 1) { inode->bits > 1) {
struct tnode *left, *right; struct tnode *left, *right;
t_key m = TKEY_GET_MASK(inode->pos, 1); t_key m = TKEY_GET_MASK(inode->pos, 1);
left = tnode_new(inode->key&(~m), inode->pos + 1, left = tnode_new(inode->key&(~m), inode->pos + 1,
...@@ -685,7 +658,7 @@ static struct tnode *inflate(struct trie *t, struct tnode *tn, int *err) ...@@ -685,7 +658,7 @@ static struct tnode *inflate(struct trie *t, struct tnode *tn, int *err)
*err = -ENOMEM; *err = -ENOMEM;
break; break;
} }
right = tnode_new(inode->key|m, inode->pos + 1, right = tnode_new(inode->key|m, inode->pos + 1,
inode->bits - 1); inode->bits - 1);
...@@ -703,18 +676,20 @@ static struct tnode *inflate(struct trie *t, struct tnode *tn, int *err) ...@@ -703,18 +676,20 @@ static struct tnode *inflate(struct trie *t, struct tnode *tn, int *err)
int size = tnode_child_length(tn); int size = tnode_child_length(tn);
int j; int j;
for(j = 0; j < size; j++) for (j = 0; j < size; j++)
if (tn->child[j]) if (tn->child[j])
tnode_free((struct tnode *)tn->child[j]); tnode_free((struct tnode *)tn->child[j]);
tnode_free(tn); tnode_free(tn);
*err = -ENOMEM; *err = -ENOMEM;
return oldtnode; return oldtnode;
} }
for(i = 0; i < olen; i++) { for (i = 0; i < olen; i++) {
struct node *node = tnode_get_child(oldtnode, i); struct node *node = tnode_get_child(oldtnode, i);
struct tnode *left, *right;
int size, j;
/* An empty child */ /* An empty child */
if (node == NULL) if (node == NULL)
...@@ -740,56 +715,51 @@ static struct tnode *inflate(struct trie *t, struct tnode *tn, int *err) ...@@ -740,56 +715,51 @@ static struct tnode *inflate(struct trie *t, struct tnode *tn, int *err)
put_child(t, tn, 2*i+1, inode->child[1]); put_child(t, tn, 2*i+1, inode->child[1]);
tnode_free(inode); tnode_free(inode);
continue;
} }
/* An internal node with more than two children */ /* An internal node with more than two children */
else {
struct tnode *left, *right; /* We will replace this node 'inode' with two new
int size, j; * ones, 'left' and 'right', each with half of the
* original children. The two new nodes will have
/* We will replace this node 'inode' with two new * a position one bit further down the key and this
* ones, 'left' and 'right', each with half of the * means that the "significant" part of their keys
* original children. The two new nodes will have * (see the discussion near the top of this file)
* a position one bit further down the key and this * will differ by one bit, which will be "0" in
* means that the "significant" part of their keys * left's key and "1" in right's key. Since we are
* (see the discussion near the top of this file) * moving the key position by one step, the bit that
* will differ by one bit, which will be "0" in * we are moving away from - the bit at position
* left's key and "1" in right's key. Since we are * (inode->pos) - is the one that will differ between
* moving the key position by one step, the bit that * left and right. So... we synthesize that bit in the
* we are moving away from - the bit at position * two new keys.
* (inode->pos) - is the one that will differ between * The mask 'm' below will be a single "one" bit at
* left and right. So... we synthesize that bit in the * the position (inode->pos)
* two new keys. */
* The mask 'm' below will be a single "one" bit at
* the position (inode->pos)
*/
/* Use the old key, but set the new significant
* bit to zero.
*/
left = (struct tnode *) tnode_get_child(tn, 2*i); /* Use the old key, but set the new significant
put_child(t, tn, 2*i, NULL); * bit to zero.
*/
if (!left) left = (struct tnode *) tnode_get_child(tn, 2*i);
BUG(); put_child(t, tn, 2*i, NULL);
right = (struct tnode *) tnode_get_child(tn, 2*i+1); BUG_ON(!left);
put_child(t, tn, 2*i+1, NULL);
if (!right) right = (struct tnode *) tnode_get_child(tn, 2*i+1);
BUG(); put_child(t, tn, 2*i+1, NULL);
size = tnode_child_length(left); BUG_ON(!right);
for(j = 0; j < size; j++) {
put_child(t, left, j, inode->child[j]);
put_child(t, right, j, inode->child[j + size]);
}
put_child(t, tn, 2*i, resize(t, left));
put_child(t, tn, 2*i+1, resize(t, right));
tnode_free(inode); size = tnode_child_length(left);
for (j = 0; j < size; j++) {
put_child(t, left, j, inode->child[j]);
put_child(t, right, j, inode->child[j + size]);
} }
put_child(t, tn, 2*i, resize(t, left));
put_child(t, tn, 2*i+1, resize(t, right));
tnode_free(inode);
} }
tnode_free(oldtnode); tnode_free(oldtnode);
return tn; return tn;
...@@ -802,7 +772,7 @@ static struct tnode *halve(struct trie *t, struct tnode *tn, int *err) ...@@ -802,7 +772,7 @@ static struct tnode *halve(struct trie *t, struct tnode *tn, int *err)
int i; int i;
int olen = tnode_child_length(tn); int olen = tnode_child_length(tn);
if (trie_debug) printk("In halve\n"); DBG("In halve\n");
tn = tnode_new(oldtnode->key, oldtnode->pos, oldtnode->bits - 1); tn = tnode_new(oldtnode->key, oldtnode->pos, oldtnode->bits - 1);
...@@ -818,7 +788,7 @@ static struct tnode *halve(struct trie *t, struct tnode *tn, int *err) ...@@ -818,7 +788,7 @@ static struct tnode *halve(struct trie *t, struct tnode *tn, int *err)
* of tnode is ignored. * of tnode is ignored.
*/ */
for(i = 0; i < olen; i += 2) { for (i = 0; i < olen; i += 2) {
left = tnode_get_child(oldtnode, i); left = tnode_get_child(oldtnode, i);
right = tnode_get_child(oldtnode, i+1); right = tnode_get_child(oldtnode, i+1);
...@@ -839,17 +809,19 @@ static struct tnode *halve(struct trie *t, struct tnode *tn, int *err) ...@@ -839,17 +809,19 @@ static struct tnode *halve(struct trie *t, struct tnode *tn, int *err)
int size = tnode_child_length(tn); int size = tnode_child_length(tn);
int j; int j;
for(j = 0; j < size; j++) for (j = 0; j < size; j++)
if (tn->child[j]) if (tn->child[j])
tnode_free((struct tnode *)tn->child[j]); tnode_free((struct tnode *)tn->child[j]);
tnode_free(tn); tnode_free(tn);
*err = -ENOMEM; *err = -ENOMEM;
return oldtnode; return oldtnode;
} }
for(i = 0; i < olen; i += 2) { for (i = 0; i < olen; i += 2) {
struct tnode *newBinNode;
left = tnode_get_child(oldtnode, i); left = tnode_get_child(oldtnode, i);
right = tnode_get_child(oldtnode, i+1); right = tnode_get_child(oldtnode, i+1);
...@@ -858,38 +830,39 @@ static struct tnode *halve(struct trie *t, struct tnode *tn, int *err) ...@@ -858,38 +830,39 @@ static struct tnode *halve(struct trie *t, struct tnode *tn, int *err)
if (right == NULL) /* Both are empty */ if (right == NULL) /* Both are empty */
continue; continue;
put_child(t, tn, i/2, right); put_child(t, tn, i/2, right);
} else if (right == NULL) continue;
}
if (right == NULL) {
put_child(t, tn, i/2, left); put_child(t, tn, i/2, left);
continue;
}
/* Two nonempty children */ /* Two nonempty children */
else { newBinNode = (struct tnode *) tnode_get_child(tn, i/2);
struct tnode *newBinNode = put_child(t, tn, i/2, NULL);
(struct tnode *) tnode_get_child(tn, i/2);
put_child(t, tn, i/2, NULL);
if (!newBinNode) BUG_ON(!newBinNode);
BUG();
put_child(t, newBinNode, 0, left); put_child(t, newBinNode, 0, left);
put_child(t, newBinNode, 1, right); put_child(t, newBinNode, 1, right);
put_child(t, tn, i/2, resize(t, newBinNode)); put_child(t, tn, i/2, resize(t, newBinNode));
}
} }
tnode_free(oldtnode); tnode_free(oldtnode);
return tn; return tn;
} }
static void *trie_init(struct trie *t) static void trie_init(struct trie *t)
{ {
if (t) { if (!t)
t->size = 0; return;
t->trie = NULL;
t->revision = 0; t->size = 0;
t->trie = NULL;
t->revision = 0;
#ifdef CONFIG_IP_FIB_TRIE_STATS #ifdef CONFIG_IP_FIB_TRIE_STATS
memset(&t->stats, 0, sizeof(struct trie_use_stats)); memset(&t->stats, 0, sizeof(struct trie_use_stats));
#endif #endif
}
return t;
} }
static struct leaf_info *find_leaf_info(struct hlist_head *head, int plen) static struct leaf_info *find_leaf_info(struct hlist_head *head, int plen)
...@@ -897,39 +870,37 @@ static struct leaf_info *find_leaf_info(struct hlist_head *head, int plen) ...@@ -897,39 +870,37 @@ static struct leaf_info *find_leaf_info(struct hlist_head *head, int plen)
struct hlist_node *node; struct hlist_node *node;
struct leaf_info *li; struct leaf_info *li;
hlist_for_each_entry(li, node, head, hlist) { hlist_for_each_entry(li, node, head, hlist)
if (li->plen == plen) if (li->plen == plen)
return li; return li;
}
return NULL; return NULL;
} }
static inline struct list_head * get_fa_head(struct leaf *l, int plen) static inline struct list_head * get_fa_head(struct leaf *l, int plen)
{ {
struct list_head *fa_head = NULL;
struct leaf_info *li = find_leaf_info(&l->list, plen); struct leaf_info *li = find_leaf_info(&l->list, plen);
if (li) if (!li)
fa_head = &li->falh; return NULL;
return fa_head; return &li->falh;
} }
static void insert_leaf_info(struct hlist_head *head, struct leaf_info *new) static void insert_leaf_info(struct hlist_head *head, struct leaf_info *new)
{ {
struct leaf_info *li = NULL, *last = NULL; struct leaf_info *li = NULL, *last = NULL;
struct hlist_node *node, *tmp; struct hlist_node *node;
write_lock_bh(&fib_lock); write_lock_bh(&fib_lock);
if (hlist_empty(head)) if (hlist_empty(head)) {
hlist_add_head(&new->hlist, head); hlist_add_head(&new->hlist, head);
else { } else {
hlist_for_each_entry_safe(li, node, tmp, head, hlist) { hlist_for_each_entry(li, node, head, hlist) {
if (new->plen > li->plen) if (new->plen > li->plen)
break; break;
last = li; last = li;
} }
if (last) if (last)
...@@ -952,49 +923,47 @@ fib_find_node(struct trie *t, u32 key) ...@@ -952,49 +923,47 @@ fib_find_node(struct trie *t, u32 key)
while (n != NULL && NODE_TYPE(n) == T_TNODE) { while (n != NULL && NODE_TYPE(n) == T_TNODE) {
tn = (struct tnode *) n; tn = (struct tnode *) n;
check_tnode(tn); check_tnode(tn);
if (tkey_sub_equals(tn->key, pos, tn->pos-pos, key)) { if (tkey_sub_equals(tn->key, pos, tn->pos-pos, key)) {
pos=tn->pos + tn->bits; pos = tn->pos + tn->bits;
n = tnode_get_child(tn, tkey_extract_bits(key, tn->pos, tn->bits)); n = tnode_get_child(tn, tkey_extract_bits(key, tn->pos, tn->bits));
} } else
else
break; break;
} }
/* Case we have found a leaf. Compare prefixes */ /* Case we have found a leaf. Compare prefixes */
if (n != NULL && IS_LEAF(n) && tkey_equals(key, n->key)) { if (n != NULL && IS_LEAF(n) && tkey_equals(key, n->key))
struct leaf *l = (struct leaf *) n; return (struct leaf *)n;
return l;
}
return NULL; return NULL;
} }
static struct node *trie_rebalance(struct trie *t, struct tnode *tn) static struct node *trie_rebalance(struct trie *t, struct tnode *tn)
{ {
int i = 0; int i;
int wasfull; int wasfull;
t_key cindex, key; t_key cindex, key;
struct tnode *tp = NULL; struct tnode *tp = NULL;
if (!tn) BUG_ON(!tn);
BUG();
key = tn->key; key = tn->key;
i = 0; i = 0;
while (tn != NULL && NODE_PARENT(tn) != NULL) { while (tn != NULL && NODE_PARENT(tn) != NULL) {
if (i > 10) { if (i > 10) {
printk("Rebalance tn=%p \n", tn); printk("Rebalance tn=%p \n", tn);
if (tn) printk("tn->parent=%p \n", NODE_PARENT(tn)); if (tn)
printk("tn->parent=%p \n", NODE_PARENT(tn));
printk("Rebalance tp=%p \n", tp); printk("Rebalance tp=%p \n", tp);
if (tp) printk("tp->parent=%p \n", NODE_PARENT(tp)); if (tp)
printk("tp->parent=%p \n", NODE_PARENT(tp));
} }
if (i > 12) BUG(); BUG_ON(i > 12); /* Why is this a bug? -ojn */
i++; i++;
tp = NODE_PARENT(tn); tp = NODE_PARENT(tn);
...@@ -1002,7 +971,7 @@ static struct node *trie_rebalance(struct trie *t, struct tnode *tn) ...@@ -1002,7 +971,7 @@ static struct node *trie_rebalance(struct trie *t, struct tnode *tn)
wasfull = tnode_full(tp, tnode_get_child(tp, cindex)); wasfull = tnode_full(tp, tnode_get_child(tp, cindex));
tn = (struct tnode *) resize (t, (struct tnode *)tn); tn = (struct tnode *) resize (t, (struct tnode *)tn);
tnode_put_child_reorg((struct tnode *)tp, cindex,(struct node*)tn, wasfull); tnode_put_child_reorg((struct tnode *)tp, cindex,(struct node*)tn, wasfull);
if (!NODE_PARENT(tn)) if (!NODE_PARENT(tn))
break; break;
...@@ -1050,20 +1019,19 @@ fib_insert_node(struct trie *t, int *err, u32 key, int plen) ...@@ -1050,20 +1019,19 @@ fib_insert_node(struct trie *t, int *err, u32 key, int plen)
while (n != NULL && NODE_TYPE(n) == T_TNODE) { while (n != NULL && NODE_TYPE(n) == T_TNODE) {
tn = (struct tnode *) n; tn = (struct tnode *) n;
check_tnode(tn); check_tnode(tn);
if (tkey_sub_equals(tn->key, pos, tn->pos-pos, key)) { if (tkey_sub_equals(tn->key, pos, tn->pos-pos, key)) {
tp = tn; tp = tn;
pos=tn->pos + tn->bits; pos = tn->pos + tn->bits;
n = tnode_get_child(tn, tkey_extract_bits(key, tn->pos, tn->bits)); n = tnode_get_child(tn, tkey_extract_bits(key, tn->pos, tn->bits));
if (n && NODE_PARENT(n) != tn) { if (n && NODE_PARENT(n) != tn) {
printk("BUG tn=%p, n->parent=%p\n", tn, NODE_PARENT(n)); printk("BUG tn=%p, n->parent=%p\n", tn, NODE_PARENT(n));
BUG(); BUG();
} }
} } else
else
break; break;
} }
...@@ -1073,17 +1041,15 @@ fib_insert_node(struct trie *t, int *err, u32 key, int plen) ...@@ -1073,17 +1041,15 @@ fib_insert_node(struct trie *t, int *err, u32 key, int plen)
* tp is n's (parent) ----> NULL or TNODE * tp is n's (parent) ----> NULL or TNODE
*/ */
if (tp && IS_LEAF(tp)) BUG_ON(tp && IS_LEAF(tp));
BUG();
/* Case 1: n is a leaf. Compare prefixes */ /* Case 1: n is a leaf. Compare prefixes */
if (n != NULL && IS_LEAF(n) && tkey_equals(key, n->key)) { if (n != NULL && IS_LEAF(n) && tkey_equals(key, n->key)) {
struct leaf *l = ( struct leaf *) n; struct leaf *l = (struct leaf *) n;
li = leaf_info_new(plen); li = leaf_info_new(plen);
if (!li) { if (!li) {
*err = -ENOMEM; *err = -ENOMEM;
goto err; goto err;
...@@ -1113,35 +1079,31 @@ fib_insert_node(struct trie *t, int *err, u32 key, int plen) ...@@ -1113,35 +1079,31 @@ fib_insert_node(struct trie *t, int *err, u32 key, int plen)
fa_head = &li->falh; fa_head = &li->falh;
insert_leaf_info(&l->list, li); insert_leaf_info(&l->list, li);
/* Case 2: n is NULL, and will just insert a new leaf */
if (t->trie && n == NULL) { if (t->trie && n == NULL) {
/* Case 2: n is NULL, and will just insert a new leaf */
NODE_SET_PARENT(l, tp); NODE_SET_PARENT(l, tp);
if (!tp)
BUG();
else { BUG_ON(!tp);
cindex = tkey_extract_bits(key, tp->pos, tp->bits);
put_child(t, (struct tnode *)tp, cindex, (struct node *)l); cindex = tkey_extract_bits(key, tp->pos, tp->bits);
} put_child(t, (struct tnode *)tp, cindex, (struct node *)l);
} } else {
/* Case 3: n is a LEAF or a TNODE and the key doesn't match. */ /* Case 3: n is a LEAF or a TNODE and the key doesn't match. */
else {
/* /*
* Add a new tnode here * Add a new tnode here
* first tnode need some special handling * first tnode need some special handling
*/ */
if (tp) if (tp)
pos=tp->pos+tp->bits; pos = tp->pos+tp->bits;
else else
pos=0; pos = 0;
if (n) { if (n) {
newpos = tkey_mismatch(key, pos, n->key); newpos = tkey_mismatch(key, pos, n->key);
tn = tnode_new(n->key, newpos, 1); tn = tnode_new(n->key, newpos, 1);
} } else {
else {
newpos = 0; newpos = 0;
tn = tnode_new(key, newpos, 1); /* First tnode */ tn = tnode_new(key, newpos, 1); /* First tnode */
} }
...@@ -1151,32 +1113,32 @@ fib_insert_node(struct trie *t, int *err, u32 key, int plen) ...@@ -1151,32 +1113,32 @@ fib_insert_node(struct trie *t, int *err, u32 key, int plen)
tnode_free((struct tnode *) l); tnode_free((struct tnode *) l);
*err = -ENOMEM; *err = -ENOMEM;
goto err; goto err;
} }
NODE_SET_PARENT(tn, tp); NODE_SET_PARENT(tn, tp);
missbit=tkey_extract_bits(key, newpos, 1); missbit = tkey_extract_bits(key, newpos, 1);
put_child(t, tn, missbit, (struct node *)l); put_child(t, tn, missbit, (struct node *)l);
put_child(t, tn, 1-missbit, n); put_child(t, tn, 1-missbit, n);
if (tp) { if (tp) {
cindex = tkey_extract_bits(key, tp->pos, tp->bits); cindex = tkey_extract_bits(key, tp->pos, tp->bits);
put_child(t, (struct tnode *)tp, cindex, (struct node *)tn); put_child(t, (struct tnode *)tp, cindex, (struct node *)tn);
} } else {
else {
t->trie = (struct node*) tn; /* First tnode */ t->trie = (struct node*) tn; /* First tnode */
tp = tn; tp = tn;
} }
} }
if (tp && tp->pos+tp->bits > 32) {
if (tp && tp->pos + tp->bits > 32)
printk("ERROR tp=%p pos=%d, bits=%d, key=%0x plen=%d\n", printk("ERROR tp=%p pos=%d, bits=%d, key=%0x plen=%d\n",
tp, tp->pos, tp->bits, key, plen); tp, tp->pos, tp->bits, key, plen);
}
/* Rebalance the trie */ /* Rebalance the trie */
t->trie = trie_rebalance(t, tp); t->trie = trie_rebalance(t, tp);
done: done:
t->revision++; t->revision++;
err:; err:
return fa_head; return fa_head;
} }
...@@ -1204,17 +1166,18 @@ fn_trie_insert(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta, ...@@ -1204,17 +1166,18 @@ fn_trie_insert(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta,
key = ntohl(key); key = ntohl(key);
if (trie_debug) DBG("Insert table=%d %08x/%d\n", tb->tb_id, key, plen);
printk("Insert table=%d %08x/%d\n", tb->tb_id, key, plen);
mask = ntohl( inet_make_mask(plen) ); mask = ntohl(inet_make_mask(plen));
if (key & ~mask) if (key & ~mask)
return -EINVAL; return -EINVAL;
key = key & mask; key = key & mask;
if ((fi = fib_create_info(r, rta, nlhdr, &err)) == NULL) fi = fib_create_info(r, rta, nlhdr, &err);
if (!fi)
goto err; goto err;
l = fib_find_node(t, key); l = fib_find_node(t, key);
...@@ -1236,8 +1199,7 @@ fn_trie_insert(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta, ...@@ -1236,8 +1199,7 @@ fn_trie_insert(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta,
* and we need to allocate a new one of those as well. * and we need to allocate a new one of those as well.
*/ */
if (fa && if (fa && fa->fa_info->fib_priority == fi->fib_priority) {
fa->fa_info->fib_priority == fi->fib_priority) {
struct fib_alias *fa_orig; struct fib_alias *fa_orig;
err = -EEXIST; err = -EEXIST;
...@@ -1261,9 +1223,9 @@ fn_trie_insert(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta, ...@@ -1261,9 +1223,9 @@ fn_trie_insert(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta,
fib_release_info(fi_drop); fib_release_info(fi_drop);
if (state & FA_S_ACCESSED) if (state & FA_S_ACCESSED)
rt_cache_flush(-1); rt_cache_flush(-1);
goto succeeded; goto succeeded;
} }
/* Error if we find a perfect match which /* Error if we find a perfect match which
* uses the same scope, type, and nexthop * uses the same scope, type, and nexthop
...@@ -1285,7 +1247,7 @@ fn_trie_insert(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta, ...@@ -1285,7 +1247,7 @@ fn_trie_insert(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta,
fa = fa_orig; fa = fa_orig;
} }
err = -ENOENT; err = -ENOENT;
if (!(nlhdr->nlmsg_flags&NLM_F_CREATE)) if (!(nlhdr->nlmsg_flags & NLM_F_CREATE))
goto out; goto out;
err = -ENOBUFS; err = -ENOBUFS;
...@@ -1298,9 +1260,6 @@ fn_trie_insert(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta, ...@@ -1298,9 +1260,6 @@ fn_trie_insert(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta,
new_fa->fa_type = type; new_fa->fa_type = type;
new_fa->fa_scope = r->rtm_scope; new_fa->fa_scope = r->rtm_scope;
new_fa->fa_state = 0; new_fa->fa_state = 0;
#if 0
new_fa->dst = NULL;
#endif
/* /*
* Insert new entry to the list. * Insert new entry to the list.
*/ */
...@@ -1314,8 +1273,7 @@ fn_trie_insert(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta, ...@@ -1314,8 +1273,7 @@ fn_trie_insert(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta,
write_lock_bh(&fib_lock); write_lock_bh(&fib_lock);
list_add_tail(&new_fa->fa_list, list_add_tail(&new_fa->fa_list, (fa ? &fa->fa_list : fa_head));
(fa ? &fa->fa_list : fa_head));
write_unlock_bh(&fib_lock); write_unlock_bh(&fib_lock);
...@@ -1328,7 +1286,7 @@ out_free_new_fa: ...@@ -1328,7 +1286,7 @@ out_free_new_fa:
kmem_cache_free(fn_alias_kmem, new_fa); kmem_cache_free(fn_alias_kmem, new_fa);
out: out:
fib_release_info(fi); fib_release_info(fi);
err:; err:
return err; return err;
} }
...@@ -1342,7 +1300,6 @@ static inline int check_leaf(struct trie *t, struct leaf *l, t_key key, int *pl ...@@ -1342,7 +1300,6 @@ static inline int check_leaf(struct trie *t, struct leaf *l, t_key key, int *pl
struct hlist_node *node; struct hlist_node *node;
hlist_for_each_entry(li, node, hhead, hlist) { hlist_for_each_entry(li, node, hhead, hlist) {
i = li->plen; i = li->plen;
mask = ntohl(inet_make_mask(i)); mask = ntohl(inet_make_mask(i));
if (l->key != (key & mask)) if (l->key != (key & mask))
...@@ -1370,13 +1327,18 @@ fn_trie_lookup(struct fib_table *tb, const struct flowi *flp, struct fib_result ...@@ -1370,13 +1327,18 @@ fn_trie_lookup(struct fib_table *tb, const struct flowi *flp, struct fib_result
struct node *n; struct node *n;
struct tnode *pn; struct tnode *pn;
int pos, bits; int pos, bits;
t_key key=ntohl(flp->fl4_dst); t_key key = ntohl(flp->fl4_dst);
int chopped_off; int chopped_off;
t_key cindex = 0; t_key cindex = 0;
int current_prefix_length = KEYLENGTH; int current_prefix_length = KEYLENGTH;
struct tnode *cn;
t_key node_prefix, key_prefix, pref_mismatch;
int mp;
n = t->trie; n = t->trie;
read_lock(&fib_lock); read_lock(&fib_lock);
if (!n) if (!n)
goto failed; goto failed;
...@@ -1393,8 +1355,7 @@ fn_trie_lookup(struct fib_table *tb, const struct flowi *flp, struct fib_result ...@@ -1393,8 +1355,7 @@ fn_trie_lookup(struct fib_table *tb, const struct flowi *flp, struct fib_result
pn = (struct tnode *) n; pn = (struct tnode *) n;
chopped_off = 0; chopped_off = 0;
while (pn) { while (pn) {
pos = pn->pos; pos = pn->pos;
bits = pn->bits; bits = pn->bits;
...@@ -1410,130 +1371,129 @@ fn_trie_lookup(struct fib_table *tb, const struct flowi *flp, struct fib_result ...@@ -1410,130 +1371,129 @@ fn_trie_lookup(struct fib_table *tb, const struct flowi *flp, struct fib_result
goto backtrace; goto backtrace;
} }
if (IS_TNODE(n)) { if (IS_LEAF(n)) {
if ((ret = check_leaf(t, (struct leaf *)n, key, &plen, flp, res)) <= 0)
goto found;
else
goto backtrace;
}
#define HL_OPTIMIZE #define HL_OPTIMIZE
#ifdef HL_OPTIMIZE #ifdef HL_OPTIMIZE
struct tnode *cn = (struct tnode *)n; cn = (struct tnode *)n;
t_key node_prefix, key_prefix, pref_mismatch;
int mp;
/* /*
* It's a tnode, and we can do some extra checks here if we * It's a tnode, and we can do some extra checks here if we
* like, to avoid descending into a dead-end branch. * like, to avoid descending into a dead-end branch.
* This tnode is in the parent's child array at index * This tnode is in the parent's child array at index
* key[p_pos..p_pos+p_bits] but potentially with some bits * key[p_pos..p_pos+p_bits] but potentially with some bits
* chopped off, so in reality the index may be just a * chopped off, so in reality the index may be just a
* subprefix, padded with zero at the end. * subprefix, padded with zero at the end.
* We can also take a look at any skipped bits in this * We can also take a look at any skipped bits in this
* tnode - everything up to p_pos is supposed to be ok, * tnode - everything up to p_pos is supposed to be ok,
* and the non-chopped bits of the index (se previous * and the non-chopped bits of the index (se previous
* paragraph) are also guaranteed ok, but the rest is * paragraph) are also guaranteed ok, but the rest is
* considered unknown. * considered unknown.
* *
* The skipped bits are key[pos+bits..cn->pos]. * The skipped bits are key[pos+bits..cn->pos].
*/ */
/* If current_prefix_length < pos+bits, we are already doing
* actual prefix matching, which means everything from
* pos+(bits-chopped_off) onward must be zero along some
* branch of this subtree - otherwise there is *no* valid
* prefix present. Here we can only check the skipped
* bits. Remember, since we have already indexed into the
* parent's child array, we know that the bits we chopped of
* *are* zero.
*/
/* NOTA BENE: CHECKING ONLY SKIPPED BITS FOR THE NEW NODE HERE */ /* If current_prefix_length < pos+bits, we are already doing
* actual prefix matching, which means everything from
if (current_prefix_length < pos+bits) { * pos+(bits-chopped_off) onward must be zero along some
if (tkey_extract_bits(cn->key, current_prefix_length, * branch of this subtree - otherwise there is *no* valid
cn->pos - current_prefix_length) != 0 || * prefix present. Here we can only check the skipped
!(cn->child[0])) * bits. Remember, since we have already indexed into the
goto backtrace; * parent's child array, we know that the bits we chopped of
} * *are* zero.
*/
/* /* NOTA BENE: CHECKING ONLY SKIPPED BITS FOR THE NEW NODE HERE */
* If chopped_off=0, the index is fully validated and we
* only need to look at the skipped bits for this, the new,
* tnode. What we actually want to do is to find out if
* these skipped bits match our key perfectly, or if we will
* have to count on finding a matching prefix further down,
* because if we do, we would like to have some way of
* verifying the existence of such a prefix at this point.
*/
/* The only thing we can do at this point is to verify that if (current_prefix_length < pos+bits) {
* any such matching prefix can indeed be a prefix to our if (tkey_extract_bits(cn->key, current_prefix_length,
* key, and if the bits in the node we are inspecting that cn->pos - current_prefix_length) != 0 ||
* do not match our key are not ZERO, this cannot be true. !(cn->child[0]))
* Thus, find out where there is a mismatch (before cn->pos) goto backtrace;
* and verify that all the mismatching bits are zero in the }
* new tnode's key.
*/
/* Note: We aren't very concerned about the piece of the key /*
* that precede pn->pos+pn->bits, since these have already been * If chopped_off=0, the index is fully validated and we
* checked. The bits after cn->pos aren't checked since these are * only need to look at the skipped bits for this, the new,
* by definition "unknown" at this point. Thus, what we want to * tnode. What we actually want to do is to find out if
* see is if we are about to enter the "prefix matching" state, * these skipped bits match our key perfectly, or if we will
* and in that case verify that the skipped bits that will prevail * have to count on finding a matching prefix further down,
* throughout this subtree are zero, as they have to be if we are * because if we do, we would like to have some way of
* to find a matching prefix. * verifying the existence of such a prefix at this point.
*/ */
node_prefix = MASK_PFX(cn->key, cn->pos); /* The only thing we can do at this point is to verify that
key_prefix = MASK_PFX(key, cn->pos); * any such matching prefix can indeed be a prefix to our
pref_mismatch = key_prefix^node_prefix; * key, and if the bits in the node we are inspecting that
mp = 0; * do not match our key are not ZERO, this cannot be true.
* Thus, find out where there is a mismatch (before cn->pos)
* and verify that all the mismatching bits are zero in the
* new tnode's key.
*/
/* In short: If skipped bits in this node do not match the search /* Note: We aren't very concerned about the piece of the key
* key, enter the "prefix matching" state.directly. * that precede pn->pos+pn->bits, since these have already been
*/ * checked. The bits after cn->pos aren't checked since these are
if (pref_mismatch) { * by definition "unknown" at this point. Thus, what we want to
while (!(pref_mismatch & (1<<(KEYLENGTH-1)))) { * see is if we are about to enter the "prefix matching" state,
mp++; * and in that case verify that the skipped bits that will prevail
pref_mismatch = pref_mismatch <<1; * throughout this subtree are zero, as they have to be if we are
} * to find a matching prefix.
key_prefix = tkey_extract_bits(cn->key, mp, cn->pos-mp); */
if (key_prefix != 0) node_prefix = MASK_PFX(cn->key, cn->pos);
goto backtrace; key_prefix = MASK_PFX(key, cn->pos);
pref_mismatch = key_prefix^node_prefix;
if (current_prefix_length >= cn->pos) mp = 0;
current_prefix_length=mp;
} /* In short: If skipped bits in this node do not match the search
#endif * key, enter the "prefix matching" state.directly.
pn = (struct tnode *)n; /* Descend */ */
chopped_off = 0; if (pref_mismatch) {
continue; while (!(pref_mismatch & (1<<(KEYLENGTH-1)))) {
mp++;
pref_mismatch = pref_mismatch <<1;
}
key_prefix = tkey_extract_bits(cn->key, mp, cn->pos-mp);
if (key_prefix != 0)
goto backtrace;
if (current_prefix_length >= cn->pos)
current_prefix_length = mp;
} }
if (IS_LEAF(n)) { #endif
if ((ret = check_leaf(t, (struct leaf *)n, key, &plen, flp, res)) <= 0) pn = (struct tnode *)n; /* Descend */
goto found; chopped_off = 0;
} continue;
backtrace: backtrace:
chopped_off++; chopped_off++;
/* As zero don't change the child key (cindex) */ /* As zero don't change the child key (cindex) */
while ((chopped_off <= pn->bits) && !(cindex & (1<<(chopped_off-1)))) { while ((chopped_off <= pn->bits) && !(cindex & (1<<(chopped_off-1))))
chopped_off++; chopped_off++;
}
/* Decrease current_... with bits chopped off */ /* Decrease current_... with bits chopped off */
if (current_prefix_length > pn->pos + pn->bits - chopped_off) if (current_prefix_length > pn->pos + pn->bits - chopped_off)
current_prefix_length = pn->pos + pn->bits - chopped_off; current_prefix_length = pn->pos + pn->bits - chopped_off;
/* /*
* Either we do the actual chop off according or if we have * Either we do the actual chop off according or if we have
* chopped off all bits in this tnode walk up to our parent. * chopped off all bits in this tnode walk up to our parent.
*/ */
if (chopped_off <= pn->bits) if (chopped_off <= pn->bits) {
cindex &= ~(1 << (chopped_off-1)); cindex &= ~(1 << (chopped_off-1));
else { } else {
if (NODE_PARENT(pn) == NULL) if (NODE_PARENT(pn) == NULL)
goto failed; goto failed;
/* Get Child's index */ /* Get Child's index */
cindex = tkey_extract_bits(pn->key, NODE_PARENT(pn)->pos, NODE_PARENT(pn)->bits); cindex = tkey_extract_bits(pn->key, NODE_PARENT(pn)->pos, NODE_PARENT(pn)->bits);
pn = NODE_PARENT(pn); pn = NODE_PARENT(pn);
...@@ -1559,24 +1519,23 @@ static int trie_leaf_remove(struct trie *t, t_key key) ...@@ -1559,24 +1519,23 @@ static int trie_leaf_remove(struct trie *t, t_key key)
struct node *n = t->trie; struct node *n = t->trie;
struct leaf *l; struct leaf *l;
if (trie_debug) DBG("entering trie_leaf_remove(%p)\n", n);
printk("entering trie_leaf_remove(%p)\n", n);
/* Note that in the case skipped bits, those bits are *not* checked! /* Note that in the case skipped bits, those bits are *not* checked!
* When we finish this, we will have NULL or a T_LEAF, and the * When we finish this, we will have NULL or a T_LEAF, and the
* T_LEAF may or may not match our key. * T_LEAF may or may not match our key.
*/ */
while (n != NULL && IS_TNODE(n)) { while (n != NULL && IS_TNODE(n)) {
struct tnode *tn = (struct tnode *) n; struct tnode *tn = (struct tnode *) n;
check_tnode(tn); check_tnode(tn);
n = tnode_get_child(tn ,tkey_extract_bits(key, tn->pos, tn->bits)); n = tnode_get_child(tn ,tkey_extract_bits(key, tn->pos, tn->bits));
if (n && NODE_PARENT(n) != tn) { if (n && NODE_PARENT(n) != tn) {
printk("BUG tn=%p, n->parent=%p\n", tn, NODE_PARENT(n)); printk("BUG tn=%p, n->parent=%p\n", tn, NODE_PARENT(n));
BUG(); BUG();
} }
} }
l = (struct leaf *) n; l = (struct leaf *) n;
if (!n || !tkey_equals(l->key, key)) if (!n || !tkey_equals(l->key, key))
...@@ -1597,8 +1556,7 @@ static int trie_leaf_remove(struct trie *t, t_key key) ...@@ -1597,8 +1556,7 @@ static int trie_leaf_remove(struct trie *t, t_key key)
cindex = tkey_extract_bits(key, tp->pos, tp->bits); cindex = tkey_extract_bits(key, tp->pos, tp->bits);
put_child(t, (struct tnode *)tp, cindex, NULL); put_child(t, (struct tnode *)tp, cindex, NULL);
t->trie = trie_rebalance(t, tp); t->trie = trie_rebalance(t, tp);
} } else
else
t->trie = NULL; t->trie = NULL;
return 1; return 1;
...@@ -1606,7 +1564,7 @@ static int trie_leaf_remove(struct trie *t, t_key key) ...@@ -1606,7 +1564,7 @@ static int trie_leaf_remove(struct trie *t, t_key key)
static int static int
fn_trie_delete(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta, fn_trie_delete(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta,
struct nlmsghdr *nlhdr, struct netlink_skb_parms *req) struct nlmsghdr *nlhdr, struct netlink_skb_parms *req)
{ {
struct trie *t = (struct trie *) tb->tb_data; struct trie *t = (struct trie *) tb->tb_data;
u32 key, mask; u32 key, mask;
...@@ -1615,6 +1573,9 @@ fn_trie_delete(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta, ...@@ -1615,6 +1573,9 @@ fn_trie_delete(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta,
struct fib_alias *fa, *fa_to_delete; struct fib_alias *fa, *fa_to_delete;
struct list_head *fa_head; struct list_head *fa_head;
struct leaf *l; struct leaf *l;
int kill_li = 0;
struct leaf_info *li;
if (plen > 32) if (plen > 32)
return -EINVAL; return -EINVAL;
...@@ -1624,7 +1585,7 @@ fn_trie_delete(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta, ...@@ -1624,7 +1585,7 @@ fn_trie_delete(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta,
memcpy(&key, rta->rta_dst, 4); memcpy(&key, rta->rta_dst, 4);
key = ntohl(key); key = ntohl(key);
mask = ntohl( inet_make_mask(plen) ); mask = ntohl(inet_make_mask(plen));
if (key & ~mask) if (key & ~mask)
return -EINVAL; return -EINVAL;
...@@ -1641,8 +1602,7 @@ fn_trie_delete(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta, ...@@ -1641,8 +1602,7 @@ fn_trie_delete(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta,
if (!fa) if (!fa)
return -ESRCH; return -ESRCH;
if (trie_debug) DBG("Deleting %08x/%d tos=%d t=%p\n", key, plen, tos, t);
printk("Deleting %08x/%d tos=%d t=%p\n", key, plen, tos, t);
fa_to_delete = NULL; fa_to_delete = NULL;
fa_head = fa->fa_list.prev; fa_head = fa->fa_list.prev;
...@@ -1664,39 +1624,36 @@ fn_trie_delete(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta, ...@@ -1664,39 +1624,36 @@ fn_trie_delete(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta,
} }
} }
if (fa_to_delete) { if (!fa_to_delete)
int kill_li = 0; return -ESRCH;
struct leaf_info *li;
fa = fa_to_delete; fa = fa_to_delete;
rtmsg_fib(RTM_DELROUTE, htonl(key), fa, plen, tb->tb_id, nlhdr, req); rtmsg_fib(RTM_DELROUTE, htonl(key), fa, plen, tb->tb_id, nlhdr, req);
l = fib_find_node(t, key);
li = find_leaf_info(&l->list, plen);
l = fib_find_node(t, key); write_lock_bh(&fib_lock);
li = find_leaf_info(&l->list, plen);
write_lock_bh(&fib_lock); list_del(&fa->fa_list);
list_del(&fa->fa_list); if (list_empty(fa_head)) {
hlist_del(&li->hlist);
kill_li = 1;
}
write_unlock_bh(&fib_lock);
if (list_empty(fa_head)) { if (kill_li)
hlist_del(&li->hlist); free_leaf_info(li);
kill_li = 1;
}
write_unlock_bh(&fib_lock);
if (kill_li)
free_leaf_info(li);
if (hlist_empty(&l->list)) if (hlist_empty(&l->list))
trie_leaf_remove(t, key); trie_leaf_remove(t, key);
if (fa->fa_state & FA_S_ACCESSED) if (fa->fa_state & FA_S_ACCESSED)
rt_cache_flush(-1); rt_cache_flush(-1);
fn_free_alias(fa); fn_free_alias(fa);
return 0; return 0;
}
return -ESRCH;
} }
static int trie_flush_list(struct trie *t, struct list_head *head) static int trie_flush_list(struct trie *t, struct list_head *head)
...@@ -1706,9 +1663,8 @@ static int trie_flush_list(struct trie *t, struct list_head *head) ...@@ -1706,9 +1663,8 @@ static int trie_flush_list(struct trie *t, struct list_head *head)
list_for_each_entry_safe(fa, fa_node, head, fa_list) { list_for_each_entry_safe(fa, fa_node, head, fa_list) {
struct fib_info *fi = fa->fa_info; struct fib_info *fi = fa->fa_info;
if (fi && (fi->fib_flags&RTNH_F_DEAD)) {
if (fi && (fi->fib_flags&RTNH_F_DEAD)) {
write_lock_bh(&fib_lock); write_lock_bh(&fib_lock);
list_del(&fa->fa_list); list_del(&fa->fa_list);
write_unlock_bh(&fib_lock); write_unlock_bh(&fib_lock);
...@@ -1728,11 +1684,9 @@ static int trie_flush_leaf(struct trie *t, struct leaf *l) ...@@ -1728,11 +1684,9 @@ static int trie_flush_leaf(struct trie *t, struct leaf *l)
struct leaf_info *li = NULL; struct leaf_info *li = NULL;
hlist_for_each_entry_safe(li, node, tmp, lih, hlist) { hlist_for_each_entry_safe(li, node, tmp, lih, hlist) {
found += trie_flush_list(t, &li->falh); found += trie_flush_list(t, &li->falh);
if (list_empty(&li->falh)) { if (list_empty(&li->falh)) {
write_lock_bh(&fib_lock); write_lock_bh(&fib_lock);
hlist_del(&li->hlist); hlist_del(&li->hlist);
write_unlock_bh(&fib_lock); write_unlock_bh(&fib_lock);
...@@ -1757,8 +1711,7 @@ static struct leaf *nextleaf(struct trie *t, struct leaf *thisleaf) ...@@ -1757,8 +1711,7 @@ static struct leaf *nextleaf(struct trie *t, struct leaf *thisleaf)
return (struct leaf *) t->trie; return (struct leaf *) t->trie;
p = (struct tnode*) t->trie; /* Start */ p = (struct tnode*) t->trie; /* Start */
} } else
else
p = (struct tnode *) NODE_PARENT(c); p = (struct tnode *) NODE_PARENT(c);
while (p) { while (p) {
...@@ -1771,29 +1724,28 @@ static struct leaf *nextleaf(struct trie *t, struct leaf *thisleaf) ...@@ -1771,29 +1724,28 @@ static struct leaf *nextleaf(struct trie *t, struct leaf *thisleaf)
pos = 0; pos = 0;
last = 1 << p->bits; last = 1 << p->bits;
for(idx = pos; idx < last ; idx++) { for (idx = pos; idx < last ; idx++) {
if (p->child[idx]) { if (!p->child[idx])
continue;
/* Decend if tnode */
/* Decend if tnode */
while (IS_TNODE(p->child[idx])) { while (IS_TNODE(p->child[idx])) {
p = (struct tnode*) p->child[idx]; p = (struct tnode*) p->child[idx];
idx = 0; idx = 0;
/* Rightmost non-NULL branch */ /* Rightmost non-NULL branch */
if (p && IS_TNODE(p)) if (p && IS_TNODE(p))
while (p->child[idx] == NULL && idx < (1 << p->bits)) idx++; while (p->child[idx] == NULL && idx < (1 << p->bits)) idx++;
/* Done with this tnode? */ /* Done with this tnode? */
if (idx >= (1 << p->bits) || p->child[idx] == NULL ) if (idx >= (1 << p->bits) || p->child[idx] == NULL)
goto up; goto up;
}
return (struct leaf*) p->child[idx];
} }
return (struct leaf*) p->child[idx];
} }
up: up:
/* No more children go up one step */ /* No more children go up one step */
c = (struct node*) p; c = (struct node *) p;
p = (struct tnode *) NODE_PARENT(p); p = (struct tnode *) NODE_PARENT(p);
} }
return NULL; /* Ready. Root of trie */ return NULL; /* Ready. Root of trie */
...@@ -1807,7 +1759,7 @@ static int fn_trie_flush(struct fib_table *tb) ...@@ -1807,7 +1759,7 @@ static int fn_trie_flush(struct fib_table *tb)
t->revision++; t->revision++;
for (h=0; (l = nextleaf(t, l)) != NULL; h++) { for (h = 0; (l = nextleaf(t, l)) != NULL; h++) {
found += trie_flush_leaf(t, l); found += trie_flush_leaf(t, l);
if (ll && hlist_empty(&ll->list)) if (ll && hlist_empty(&ll->list))
...@@ -1818,12 +1770,11 @@ static int fn_trie_flush(struct fib_table *tb) ...@@ -1818,12 +1770,11 @@ static int fn_trie_flush(struct fib_table *tb)
if (ll && hlist_empty(&ll->list)) if (ll && hlist_empty(&ll->list))
trie_leaf_remove(t, ll->key); trie_leaf_remove(t, ll->key);
if (trie_debug) DBG("trie_flush found=%d\n", found);
printk("trie_flush found=%d\n", found);
return found; return found;
} }
static int trie_last_dflt=-1; static int trie_last_dflt = -1;
static void static void
fn_trie_select_default(struct fib_table *tb, const struct flowi *flp, struct fib_result *res) fn_trie_select_default(struct fib_table *tb, const struct flowi *flp, struct fib_result *res)
...@@ -1855,18 +1806,18 @@ fn_trie_select_default(struct fib_table *tb, const struct flowi *flp, struct fib ...@@ -1855,18 +1806,18 @@ fn_trie_select_default(struct fib_table *tb, const struct flowi *flp, struct fib
list_for_each_entry(fa, fa_head, fa_list) { list_for_each_entry(fa, fa_head, fa_list) {
struct fib_info *next_fi = fa->fa_info; struct fib_info *next_fi = fa->fa_info;
if (fa->fa_scope != res->scope || if (fa->fa_scope != res->scope ||
fa->fa_type != RTN_UNICAST) fa->fa_type != RTN_UNICAST)
continue; continue;
if (next_fi->fib_priority > res->fi->fib_priority) if (next_fi->fib_priority > res->fi->fib_priority)
break; break;
if (!next_fi->fib_nh[0].nh_gw || if (!next_fi->fib_nh[0].nh_gw ||
next_fi->fib_nh[0].nh_scope != RT_SCOPE_LINK) next_fi->fib_nh[0].nh_scope != RT_SCOPE_LINK)
continue; continue;
fa->fa_state |= FA_S_ACCESSED; fa->fa_state |= FA_S_ACCESSED;
if (fi == NULL) { if (fi == NULL) {
if (next_fi != res->fi) if (next_fi != res->fi)
break; break;
...@@ -1913,9 +1864,9 @@ static int fn_trie_dump_fa(t_key key, int plen, struct list_head *fah, struct fi ...@@ -1913,9 +1864,9 @@ static int fn_trie_dump_fa(t_key key, int plen, struct list_head *fah, struct fi
int i, s_i; int i, s_i;
struct fib_alias *fa; struct fib_alias *fa;
u32 xkey=htonl(key); u32 xkey = htonl(key);
s_i=cb->args[3]; s_i = cb->args[3];
i = 0; i = 0;
list_for_each_entry(fa, fah, fa_list) { list_for_each_entry(fa, fah, fa_list) {
...@@ -1946,10 +1897,10 @@ static int fn_trie_dump_fa(t_key key, int plen, struct list_head *fah, struct fi ...@@ -1946,10 +1897,10 @@ static int fn_trie_dump_fa(t_key key, int plen, struct list_head *fah, struct fi
fa->fa_info, 0) < 0) { fa->fa_info, 0) < 0) {
cb->args[3] = i; cb->args[3] = i;
return -1; return -1;
} }
i++; i++;
} }
cb->args[3]=i; cb->args[3] = i;
return skb->len; return skb->len;
} }
...@@ -1959,10 +1910,10 @@ static int fn_trie_dump_plen(struct trie *t, int plen, struct fib_table *tb, str ...@@ -1959,10 +1910,10 @@ static int fn_trie_dump_plen(struct trie *t, int plen, struct fib_table *tb, str
int h, s_h; int h, s_h;
struct list_head *fa_head; struct list_head *fa_head;
struct leaf *l = NULL; struct leaf *l = NULL;
s_h=cb->args[2];
for (h=0; (l = nextleaf(t, l)) != NULL; h++) { s_h = cb->args[2];
for (h = 0; (l = nextleaf(t, l)) != NULL; h++) {
if (h < s_h) if (h < s_h)
continue; continue;
if (h > s_h) if (h > s_h)
...@@ -1970,7 +1921,7 @@ static int fn_trie_dump_plen(struct trie *t, int plen, struct fib_table *tb, str ...@@ -1970,7 +1921,7 @@ static int fn_trie_dump_plen(struct trie *t, int plen, struct fib_table *tb, str
sizeof(cb->args) - 3*sizeof(cb->args[0])); sizeof(cb->args) - 3*sizeof(cb->args[0]));
fa_head = get_fa_head(l, plen); fa_head = get_fa_head(l, plen);
if (!fa_head) if (!fa_head)
continue; continue;
...@@ -1978,11 +1929,11 @@ static int fn_trie_dump_plen(struct trie *t, int plen, struct fib_table *tb, str ...@@ -1978,11 +1929,11 @@ static int fn_trie_dump_plen(struct trie *t, int plen, struct fib_table *tb, str
continue; continue;
if (fn_trie_dump_fa(l->key, plen, fa_head, tb, skb, cb)<0) { if (fn_trie_dump_fa(l->key, plen, fa_head, tb, skb, cb)<0) {
cb->args[2]=h; cb->args[2] = h;
return -1; return -1;
} }
} }
cb->args[2]=h; cb->args[2] = h;
return skb->len; return skb->len;
} }
...@@ -1994,13 +1945,12 @@ static int fn_trie_dump(struct fib_table *tb, struct sk_buff *skb, struct netlin ...@@ -1994,13 +1945,12 @@ static int fn_trie_dump(struct fib_table *tb, struct sk_buff *skb, struct netlin
s_m = cb->args[1]; s_m = cb->args[1];
read_lock(&fib_lock); read_lock(&fib_lock);
for (m=0; m<=32; m++) { for (m = 0; m <= 32; m++) {
if (m < s_m) if (m < s_m)
continue; continue;
if (m > s_m) if (m > s_m)
memset(&cb->args[2], 0, memset(&cb->args[2], 0,
sizeof(cb->args) - 2*sizeof(cb->args[0])); sizeof(cb->args) - 2*sizeof(cb->args[0]));
if (fn_trie_dump_plen(t, 32-m, tb, skb, cb)<0) { if (fn_trie_dump_plen(t, 32-m, tb, skb, cb)<0) {
cb->args[1] = m; cb->args[1] = m;
...@@ -2010,7 +1960,7 @@ static int fn_trie_dump(struct fib_table *tb, struct sk_buff *skb, struct netlin ...@@ -2010,7 +1960,7 @@ static int fn_trie_dump(struct fib_table *tb, struct sk_buff *skb, struct netlin
read_unlock(&fib_lock); read_unlock(&fib_lock);
cb->args[1] = m; cb->args[1] = m;
return skb->len; return skb->len;
out: out:
read_unlock(&fib_lock); read_unlock(&fib_lock);
return -1; return -1;
} }
...@@ -2051,9 +2001,9 @@ struct fib_table * __init fib_hash_init(int id) ...@@ -2051,9 +2001,9 @@ struct fib_table * __init fib_hash_init(int id)
trie_init(t); trie_init(t);
if (id == RT_TABLE_LOCAL) if (id == RT_TABLE_LOCAL)
trie_local = t; trie_local = t;
else if (id == RT_TABLE_MAIN) else if (id == RT_TABLE_MAIN)
trie_main = t; trie_main = t;
if (id == RT_TABLE_LOCAL) if (id == RT_TABLE_LOCAL)
printk("IPv4 FIB: Using LC-trie version %s\n", VERSION); printk("IPv4 FIB: Using LC-trie version %s\n", VERSION);
...@@ -2065,7 +2015,8 @@ struct fib_table * __init fib_hash_init(int id) ...@@ -2065,7 +2015,8 @@ struct fib_table * __init fib_hash_init(int id)
static void putspace_seq(struct seq_file *seq, int n) static void putspace_seq(struct seq_file *seq, int n)
{ {
while (n--) seq_printf(seq, " "); while (n--)
seq_printf(seq, " ");
} }
static void printbin_seq(struct seq_file *seq, unsigned int v, int bits) static void printbin_seq(struct seq_file *seq, unsigned int v, int bits)
...@@ -2086,29 +2037,22 @@ static void printnode_seq(struct seq_file *seq, int indent, struct node *n, ...@@ -2086,29 +2037,22 @@ static void printnode_seq(struct seq_file *seq, int indent, struct node *n,
seq_printf(seq, "%d/", cindex); seq_printf(seq, "%d/", cindex);
printbin_seq(seq, cindex, bits); printbin_seq(seq, cindex, bits);
seq_printf(seq, ": "); seq_printf(seq, ": ");
} } else
else
seq_printf(seq, "<root>: "); seq_printf(seq, "<root>: ");
seq_printf(seq, "%s:%p ", IS_LEAF(n)?"Leaf":"Internal node", n); seq_printf(seq, "%s:%p ", IS_LEAF(n)?"Leaf":"Internal node", n);
if (IS_LEAF(n))
seq_printf(seq, "key=%d.%d.%d.%d\n",
n->key >> 24, (n->key >> 16) % 256, (n->key >> 8) % 256, n->key % 256);
else {
int plen = ((struct tnode *)n)->pos;
t_key prf=MASK_PFX(n->key, plen);
seq_printf(seq, "key=%d.%d.%d.%d/%d\n",
prf >> 24, (prf >> 16) % 256, (prf >> 8) % 256, prf % 256, plen);
}
if (IS_LEAF(n)) { if (IS_LEAF(n)) {
struct leaf *l=(struct leaf *)n; struct leaf *l = (struct leaf *)n;
struct fib_alias *fa; struct fib_alias *fa;
int i; int i;
for (i=32; i>=0; i--)
if (find_leaf_info(&l->list, i)) { seq_printf(seq, "key=%d.%d.%d.%d\n",
n->key >> 24, (n->key >> 16) % 256, (n->key >> 8) % 256, n->key % 256);
for (i = 32; i >= 0; i--)
if (find_leaf_info(&l->list, i)) {
struct list_head *fa_head = get_fa_head(l, i); struct list_head *fa_head = get_fa_head(l, i);
if (!fa_head) if (!fa_head)
continue; continue;
...@@ -2118,17 +2062,16 @@ static void printnode_seq(struct seq_file *seq, int indent, struct node *n, ...@@ -2118,17 +2062,16 @@ static void printnode_seq(struct seq_file *seq, int indent, struct node *n,
putspace_seq(seq, indent+2); putspace_seq(seq, indent+2);
seq_printf(seq, "{/%d...dumping}\n", i); seq_printf(seq, "{/%d...dumping}\n", i);
list_for_each_entry(fa, fa_head, fa_list) { list_for_each_entry(fa, fa_head, fa_list) {
putspace_seq(seq, indent+2); putspace_seq(seq, indent+2);
if (fa->fa_info->fib_nh == NULL) {
seq_printf(seq, "Error _fib_nh=NULL\n");
continue;
}
if (fa->fa_info == NULL) { if (fa->fa_info == NULL) {
seq_printf(seq, "Error fa_info=NULL\n"); seq_printf(seq, "Error fa_info=NULL\n");
continue; continue;
} }
if (fa->fa_info->fib_nh == NULL) {
seq_printf(seq, "Error _fib_nh=NULL\n");
continue;
}
seq_printf(seq, "{type=%d scope=%d TOS=%d}\n", seq_printf(seq, "{type=%d scope=%d TOS=%d}\n",
fa->fa_type, fa->fa_type,
...@@ -2136,11 +2079,16 @@ static void printnode_seq(struct seq_file *seq, int indent, struct node *n, ...@@ -2136,11 +2079,16 @@ static void printnode_seq(struct seq_file *seq, int indent, struct node *n,
fa->fa_tos); fa->fa_tos);
} }
} }
} } else {
else if (IS_TNODE(n)) {
struct tnode *tn = (struct tnode *)n; struct tnode *tn = (struct tnode *)n;
int plen = ((struct tnode *)n)->pos;
t_key prf = MASK_PFX(n->key, plen);
seq_printf(seq, "key=%d.%d.%d.%d/%d\n",
prf >> 24, (prf >> 16) % 256, (prf >> 8) % 256, prf % 256, plen);
putspace_seq(seq, indent); seq_printf(seq, "| "); putspace_seq(seq, indent); seq_printf(seq, "| ");
seq_printf(seq, "{key prefix=%08x/", tn->key&TKEY_GET_MASK(0, tn->pos)); seq_printf(seq, "{key prefix=%08x/", tn->key & TKEY_GET_MASK(0, tn->pos));
printbin_seq(seq, tkey_extract_bits(tn->key, 0, tn->pos), tn->pos); printbin_seq(seq, tkey_extract_bits(tn->key, 0, tn->pos), tn->pos);
seq_printf(seq, "}\n"); seq_printf(seq, "}\n");
putspace_seq(seq, indent); seq_printf(seq, "| "); putspace_seq(seq, indent); seq_printf(seq, "| ");
...@@ -2155,100 +2103,103 @@ static void printnode_seq(struct seq_file *seq, int indent, struct node *n, ...@@ -2155,100 +2103,103 @@ static void printnode_seq(struct seq_file *seq, int indent, struct node *n,
static void trie_dump_seq(struct seq_file *seq, struct trie *t) static void trie_dump_seq(struct seq_file *seq, struct trie *t)
{ {
struct node *n = t->trie; struct node *n = t->trie;
int cindex=0; int cindex = 0;
int indent=1; int indent = 1;
int pend=0; int pend = 0;
int depth = 0; int depth = 0;
struct tnode *tn;
read_lock(&fib_lock); read_lock(&fib_lock);
seq_printf(seq, "------ trie_dump of t=%p ------\n", t); seq_printf(seq, "------ trie_dump of t=%p ------\n", t);
if (n) {
printnode_seq(seq, indent, n, pend, cindex, 0);
if (IS_TNODE(n)) {
struct tnode *tn = (struct tnode *)n;
pend = tn->pos+tn->bits;
putspace_seq(seq, indent); seq_printf(seq, "\\--\n");
indent += 3;
depth++;
while (tn && cindex < (1 << tn->bits)) {
if (tn->child[cindex]) {
/* Got a child */
printnode_seq(seq, indent, tn->child[cindex], pend, cindex, tn->bits);
if (IS_LEAF(tn->child[cindex])) {
cindex++;
}
else {
/*
* New tnode. Decend one level
*/
depth++;
n = tn->child[cindex];
tn = (struct tnode *)n;
pend = tn->pos+tn->bits;
putspace_seq(seq, indent); seq_printf(seq, "\\--\n");
indent+=3;
cindex=0;
}
}
else
cindex++;
if (!n) {
seq_printf(seq, "------ trie is empty\n");
read_unlock(&fib_lock);
return;
}
printnode_seq(seq, indent, n, pend, cindex, 0);
if (!IS_TNODE(n)) {
read_unlock(&fib_lock);
return;
}
tn = (struct tnode *)n;
pend = tn->pos+tn->bits;
putspace_seq(seq, indent); seq_printf(seq, "\\--\n");
indent += 3;
depth++;
while (tn && cindex < (1 << tn->bits)) {
if (tn->child[cindex]) {
/* Got a child */
printnode_seq(seq, indent, tn->child[cindex], pend, cindex, tn->bits);
if (IS_LEAF(tn->child[cindex])) {
cindex++;
} else {
/* /*
* Test if we are done * New tnode. Decend one level
*/ */
while (cindex >= (1 << tn->bits)) {
/* depth++;
* Move upwards and test for root tn = (struct tnode *)tn->child[cindex];
* pop off all traversed nodes pend = tn->pos + tn->bits;
*/ putspace_seq(seq, indent); seq_printf(seq, "\\--\n");
indent += 3;
if (NODE_PARENT(tn) == NULL) { cindex = 0;
tn = NULL;
n = NULL;
break;
}
else {
cindex = tkey_extract_bits(tn->key, NODE_PARENT(tn)->pos, NODE_PARENT(tn)->bits);
tn = NODE_PARENT(tn);
cindex++;
n = (struct node *)tn;
pend = tn->pos+tn->bits;
indent-=3;
depth--;
}
}
} }
} else
cindex++;
/*
* Test if we are done
*/
while (cindex >= (1 << tn->bits)) {
/*
* Move upwards and test for root
* pop off all traversed nodes
*/
if (NODE_PARENT(tn) == NULL) {
tn = NULL;
break;
}
cindex = tkey_extract_bits(tn->key, NODE_PARENT(tn)->pos, NODE_PARENT(tn)->bits);
cindex++;
tn = NODE_PARENT(tn);
pend = tn->pos + tn->bits;
indent -= 3;
depth--;
} }
else n = NULL;
} }
else seq_printf(seq, "------ trie is empty\n");
read_unlock(&fib_lock); read_unlock(&fib_lock);
} }
static struct trie_stat *trie_stat_new(void) static struct trie_stat *trie_stat_new(void)
{ {
struct trie_stat *s = kmalloc(sizeof(struct trie_stat), GFP_KERNEL); struct trie_stat *s;
int i; int i;
if (s) { s = kmalloc(sizeof(struct trie_stat), GFP_KERNEL);
s->totdepth = 0; if (!s)
s->maxdepth = 0; return NULL;
s->tnodes = 0;
s->leaves = 0; s->totdepth = 0;
s->nullpointers = 0; s->maxdepth = 0;
s->tnodes = 0;
for(i=0; i< MAX_CHILDS; i++) s->leaves = 0;
s->nodesizes[i] = 0; s->nullpointers = 0;
}
for (i = 0; i < MAX_CHILDS; i++)
s->nodesizes[i] = 0;
return s; return s;
} }
...@@ -2257,91 +2208,81 @@ static struct trie_stat *trie_collect_stats(struct trie *t) ...@@ -2257,91 +2208,81 @@ static struct trie_stat *trie_collect_stats(struct trie *t)
struct node *n = t->trie; struct node *n = t->trie;
struct trie_stat *s = trie_stat_new(); struct trie_stat *s = trie_stat_new();
int cindex = 0; int cindex = 0;
int indent = 1;
int pend = 0; int pend = 0;
int depth = 0; int depth = 0;
read_lock(&fib_lock); if (!s)
return NULL;
if (!n)
return s;
if (s) { read_lock(&fib_lock);
if (n) {
if (IS_TNODE(n)) {
struct tnode *tn = (struct tnode *)n;
pend = tn->pos+tn->bits;
indent += 3;
s->nodesizes[tn->bits]++;
depth++;
while (tn && cindex < (1 << tn->bits)) { if (IS_TNODE(n)) {
if (tn->child[cindex]) { struct tnode *tn = (struct tnode *)n;
/* Got a child */ pend = tn->pos+tn->bits;
s->nodesizes[tn->bits]++;
if (IS_LEAF(tn->child[cindex])) { depth++;
cindex++;
while (tn && cindex < (1 << tn->bits)) {
/* stats */ if (tn->child[cindex]) {
if (depth > s->maxdepth) /* Got a child */
s->maxdepth = depth;
s->totdepth += depth;
s->leaves++;
}
else {
/*
* New tnode. Decend one level
*/
s->tnodes++;
s->nodesizes[tn->bits]++;
depth++;
n = tn->child[cindex];
tn = (struct tnode *)n;
pend = tn->pos+tn->bits;
indent += 3;
cindex = 0;
}
}
else {
cindex++;
s->nullpointers++;
}
if (IS_LEAF(tn->child[cindex])) {
cindex++;
/* stats */
if (depth > s->maxdepth)
s->maxdepth = depth;
s->totdepth += depth;
s->leaves++;
} else {
/* /*
* Test if we are done * New tnode. Decend one level
*/ */
while (cindex >= (1 << tn->bits)) { s->tnodes++;
s->nodesizes[tn->bits]++;
/* depth++;
* Move upwards and test for root
* pop off all traversed nodes n = tn->child[cindex];
*/ tn = (struct tnode *)n;
pend = tn->pos+tn->bits;
if (NODE_PARENT(tn) == NULL) { cindex = 0;
tn = NULL;
n = NULL;
break;
}
else {
cindex = tkey_extract_bits(tn->key, NODE_PARENT(tn)->pos, NODE_PARENT(tn)->bits);
tn = NODE_PARENT(tn);
cindex++;
n = (struct node *)tn;
pend = tn->pos+tn->bits;
indent -= 3;
depth--;
}
}
} }
} else {
cindex++;
s->nullpointers++;
} }
else n = NULL;
/*
* Test if we are done
*/
while (cindex >= (1 << tn->bits)) {
/*
* Move upwards and test for root
* pop off all traversed nodes
*/
if (NODE_PARENT(tn) == NULL) {
tn = NULL;
n = NULL;
break;
}
cindex = tkey_extract_bits(tn->key, NODE_PARENT(tn)->pos, NODE_PARENT(tn)->bits);
tn = NODE_PARENT(tn);
cindex++;
n = (struct node *)tn;
pend = tn->pos+tn->bits;
depth--;
}
} }
} }
read_unlock(&fib_lock); read_unlock(&fib_lock);
return s; return s;
} }
...@@ -2359,17 +2300,22 @@ static struct fib_alias *fib_triestat_get_next(struct seq_file *seq) ...@@ -2359,17 +2300,22 @@ static struct fib_alias *fib_triestat_get_next(struct seq_file *seq)
static void *fib_triestat_seq_start(struct seq_file *seq, loff_t *pos) static void *fib_triestat_seq_start(struct seq_file *seq, loff_t *pos)
{ {
void *v = NULL; if (!ip_fib_main_table)
return NULL;
if (ip_fib_main_table) if (*pos)
v = *pos ? fib_triestat_get_next(seq) : SEQ_START_TOKEN; return fib_triestat_get_next(seq);
return v; else
return SEQ_START_TOKEN;
} }
static void *fib_triestat_seq_next(struct seq_file *seq, void *v, loff_t *pos) static void *fib_triestat_seq_next(struct seq_file *seq, void *v, loff_t *pos)
{ {
++*pos; ++*pos;
return v == SEQ_START_TOKEN ? fib_triestat_get_first(seq) : fib_triestat_get_next(seq); if (v == SEQ_START_TOKEN)
return fib_triestat_get_first(seq);
else
return fib_triestat_get_next(seq);
} }
static void fib_triestat_seq_stop(struct seq_file *seq, void *v) static void fib_triestat_seq_stop(struct seq_file *seq, void *v)
...@@ -2388,22 +2334,22 @@ static void collect_and_show(struct trie *t, struct seq_file *seq) ...@@ -2388,22 +2334,22 @@ static void collect_and_show(struct trie *t, struct seq_file *seq)
{ {
int bytes = 0; /* How many bytes are used, a ref is 4 bytes */ int bytes = 0; /* How many bytes are used, a ref is 4 bytes */
int i, max, pointers; int i, max, pointers;
struct trie_stat *stat; struct trie_stat *stat;
int avdepth; int avdepth;
stat = trie_collect_stats(t); stat = trie_collect_stats(t);
bytes=0; bytes = 0;
seq_printf(seq, "trie=%p\n", t); seq_printf(seq, "trie=%p\n", t);
if (stat) { if (stat) {
if (stat->leaves) if (stat->leaves)
avdepth=stat->totdepth*100 / stat->leaves; avdepth = stat->totdepth*100 / stat->leaves;
else else
avdepth=0; avdepth = 0;
seq_printf(seq, "Aver depth: %d.%02d\n", avdepth / 100, avdepth % 100 ); seq_printf(seq, "Aver depth: %d.%02d\n", avdepth / 100, avdepth % 100);
seq_printf(seq, "Max depth: %4d\n", stat->maxdepth); seq_printf(seq, "Max depth: %4d\n", stat->maxdepth);
seq_printf(seq, "Leaves: %d\n", stat->leaves); seq_printf(seq, "Leaves: %d\n", stat->leaves);
bytes += sizeof(struct leaf) * stat->leaves; bytes += sizeof(struct leaf) * stat->leaves;
seq_printf(seq, "Internal nodes: %d\n", stat->tnodes); seq_printf(seq, "Internal nodes: %d\n", stat->tnodes);
...@@ -2455,11 +2401,9 @@ static int fib_triestat_seq_show(struct seq_file *seq, void *v) ...@@ -2455,11 +2401,9 @@ static int fib_triestat_seq_show(struct seq_file *seq, void *v)
if (trie_main) if (trie_main)
collect_and_show(trie_main, seq); collect_and_show(trie_main, seq);
} } else {
else { snprintf(bf, sizeof(bf), "*\t%08X\t%08X", 200, 400);
snprintf(bf, sizeof(bf),
"*\t%08X\t%08X", 200, 400);
seq_printf(seq, "%-127s\n", bf); seq_printf(seq, "%-127s\n", bf);
} }
return 0; return 0;
...@@ -2520,22 +2464,27 @@ static struct fib_alias *fib_trie_get_next(struct seq_file *seq) ...@@ -2520,22 +2464,27 @@ static struct fib_alias *fib_trie_get_next(struct seq_file *seq)
static void *fib_trie_seq_start(struct seq_file *seq, loff_t *pos) static void *fib_trie_seq_start(struct seq_file *seq, loff_t *pos)
{ {
void *v = NULL; if (!ip_fib_main_table)
return NULL;
if (ip_fib_main_table) if (*pos)
v = *pos ? fib_trie_get_next(seq) : SEQ_START_TOKEN; return fib_trie_get_next(seq);
return v; else
return SEQ_START_TOKEN;
} }
static void *fib_trie_seq_next(struct seq_file *seq, void *v, loff_t *pos) static void *fib_trie_seq_next(struct seq_file *seq, void *v, loff_t *pos)
{ {
++*pos; ++*pos;
return v == SEQ_START_TOKEN ? fib_trie_get_first(seq) : fib_trie_get_next(seq); if (v == SEQ_START_TOKEN)
return fib_trie_get_first(seq);
else
return fib_trie_get_next(seq);
} }
static void fib_trie_seq_stop(struct seq_file *seq, void *v) static void fib_trie_seq_stop(struct seq_file *seq, void *v)
{ {
} }
/* /*
...@@ -2555,9 +2504,7 @@ static int fib_trie_seq_show(struct seq_file *seq, void *v) ...@@ -2555,9 +2504,7 @@ static int fib_trie_seq_show(struct seq_file *seq, void *v)
if (trie_main) if (trie_main)
trie_dump_seq(seq, trie_main); trie_dump_seq(seq, trie_main);
} } else {
else {
snprintf(bf, sizeof(bf), snprintf(bf, sizeof(bf),
"*\t%08X\t%08X", 200, 400); "*\t%08X\t%08X", 200, 400);
seq_printf(seq, "%-127s\n", bf); seq_printf(seq, "%-127s\n", bf);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment