Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

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

[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
Loading
Loading
Loading
Loading
+592 −645
Original line number Diff line number Diff line
@@ -89,27 +89,27 @@ typedef unsigned int t_key;
#define T_TNODE 0
#define T_LEAF  1
#define NODE_TYPE_MASK	0x1UL
#define NODE_PARENT(_node) \
	((struct tnode *)((_node)->_parent & ~NODE_TYPE_MASK))
#define NODE_SET_PARENT(_node, _ptr) \
	((_node)->_parent = (((unsigned long)(_ptr)) | \
                     ((_node)->_parent & NODE_TYPE_MASK)))
#define NODE_INIT_PARENT(_node, _type) \
	((_node)->_parent = (_type))
#define NODE_TYPE(_node) \
	((_node)->_parent & NODE_TYPE_MASK)

#define IS_TNODE(n) (!(n->_parent & T_LEAF))
#define IS_LEAF(n) (n->_parent & T_LEAF)
#define NODE_PARENT(node) \
	((struct tnode *)((node)->parent & ~NODE_TYPE_MASK))
#define NODE_SET_PARENT(node, ptr) \
	((node)->parent = (((unsigned long)(ptr)) | \
                     ((node)->parent & NODE_TYPE_MASK)))
#define NODE_INIT_PARENT(node, type) \
	((node)->parent = (type))
#define NODE_TYPE(node) \
	((node)->parent & NODE_TYPE_MASK)

#define IS_TNODE(n) (!(n->parent & T_LEAF))
#define IS_LEAF(n) (n->parent & T_LEAF)

struct node {
	t_key key;
	unsigned long _parent;
	unsigned long parent;
};

struct leaf {
	t_key key;
	unsigned long _parent;
	unsigned long parent;
	struct hlist_head list;
};

@@ -121,7 +121,7 @@ struct leaf_info {

struct tnode {
	t_key key;
	unsigned long _parent;
	unsigned long parent;
	unsigned short pos:5;		/* 2log(KEYLENGTH) bits needed */
	unsigned short bits:5;		/* 2log(KEYLENGTH) bits needed */
	unsigned short full_children;	/* KEYLENGTH bits needed */
@@ -160,6 +160,8 @@ struct trie {

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 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);
@@ -179,16 +181,9 @@ extern void rtmsg_fib(int event, u32 key, struct fib_alias *fa, int z, int tb_id
static kmem_cache_t *fn_alias_kmem;
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)
{
        if (i >= 1<<tn->bits)
                trie_bug("tnode_get_child");
	BUG_ON(i >= 1 << tn->bits);

	return tn->child[i];
}
@@ -198,24 +193,6 @@ static inline int tnode_child_length(struct tnode *tn)
	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)
{
	if (offset < KEYLENGTH)
@@ -249,7 +226,7 @@ static inline int tkey_mismatch(t_key a, int offset, t_key b)
	return i;
}

/* Candiate for fib_semantics */
/* Candidate for fib_semantics */

static void fn_free_alias(struct fib_alias *fa)
{
@@ -343,10 +320,13 @@ static struct leaf *leaf_new(void)
static struct leaf_info *leaf_info_new(int plen)
{
	struct leaf_info *li = kmalloc(sizeof(struct leaf_info),  GFP_KERNEL);
	if (li) {

	if (!li)
		return NULL;

	li->plen = plen;
	INIT_LIST_HEAD(&li->falh);
	}

	return li;
}

@@ -397,29 +377,21 @@ static struct tnode* tnode_new(t_key key, int pos, int bits)
		tn->empty_children = 1<<bits;
	}

	if (trie_debug > 0)
		printk("AT %p s=%u %u\n", tn, (unsigned int) sizeof(struct tnode),
	DBG("AT %p s=%u %u\n", tn, (unsigned int) sizeof(struct tnode),
	       (unsigned int) (sizeof(struct node) * 1<<bits));
	return tn;
}

static void tnode_free(struct tnode *tn)
{
	if (!tn) {
		trie_bug("tnode_free\n");
	}
	BUG_ON(!tn);

	if (IS_LEAF(tn)) {
		free_leaf((struct leaf *)tn);
		if (trie_debug > 0 )
			printk("FL %p \n", tn);
	}
	else if (IS_TNODE(tn)) {
		DBG("FL %p \n", tn);
	} else {
		__tnode_free(tn);
		if (trie_debug > 0 )
			printk("FT %p \n", tn);
	}
	else {
		trie_bug("tnode_free\n");
		DBG("FT %p \n", tn);
	}
}

@@ -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) {
		printk("bits=%d, i=%d\n", tn->bits, i);
		trie_bug("tnode_put_child_reorg bits");
		BUG();
	}
	write_lock_bh(&fib_lock);
	chi = tn->child[i];
@@ -471,9 +443,9 @@ static void tnode_put_child_reorg(struct tnode *tn, int i, struct node *n, int w
	isfull = tnode_full(tn, n);
	if (wasfull && !isfull)
		tn->full_children--;

	else if (!wasfull && isfull)
		tn->full_children++;

	if (n)
		NODE_SET_PARENT(n, tn);

@@ -489,8 +461,7 @@ static struct node *resize(struct trie *t, struct tnode *tn)
 	if (!tn)
		return NULL;

	if (trie_debug)
		printk("In tnode_resize %p inflate_threshold=%d threshold=%d\n",
	DBG("In tnode_resize %p inflate_threshold=%d threshold=%d\n",
	      tn, inflate_threshold, halve_threshold);

	/* No children */
@@ -501,21 +472,22 @@ static struct node *resize(struct trie *t, struct tnode *tn)
	/* One child */
	if (tn->empty_children == tnode_child_length(tn) - 1)
		for (i = 0; i < tnode_child_length(tn); i++) {
			struct node *n;

			write_lock_bh(&fib_lock);
			if (tn->child[i] != NULL) {
			n = tn->child[i];
			if (!n) {
				write_unlock_bh(&fib_lock);
				continue;
			}

			/* compress one level */
				struct node *n = tn->child[i];
				if (n)
			NODE_INIT_PARENT(n, NODE_TYPE(n));

			write_unlock_bh(&fib_lock);
			tnode_free(tn);
			return n;
		}
			write_unlock_bh(&fib_lock);
		}
	/*
	 * Double as long as the resulting node has a number of
	 * nonempty nodes that are above the threshold.
@@ -624,21 +596,24 @@ static struct node *resize(struct trie *t, struct tnode *tn)

	if (tn->empty_children == tnode_child_length(tn) - 1)
		for (i = 0; i < tnode_child_length(tn); i++) {
			struct node *n;

			write_lock_bh(&fib_lock);
			if (tn->child[i] != NULL) {

			n = tn->child[i];
			if (!n) {
				write_unlock_bh(&fib_lock);
				continue;
			}

			/* compress one level */
				struct node *n = tn->child[i];

				if (n)
			NODE_INIT_PARENT(n, NODE_TYPE(n));

			write_unlock_bh(&fib_lock);
			tnode_free(tn);
			return n;
		}
			write_unlock_bh(&fib_lock);
		}

	return (struct node *) tn;
}
@@ -650,8 +625,7 @@ static struct tnode *inflate(struct trie *t, struct tnode *tn, int *err)
	int olen = tnode_child_length(tn);
	int i;

  	if (trie_debug)
		printk("In inflate\n");
	DBG("In inflate\n");

	tn = tnode_new(oldtnode->key, oldtnode->pos, oldtnode->bits + 1);

@@ -675,7 +649,6 @@ static struct tnode *inflate(struct trie *t, struct tnode *tn, int *err)
		    inode->pos == oldtnode->pos + oldtnode->bits &&
		    inode->bits > 1) {
			struct tnode *left, *right;

			t_key m = TKEY_GET_MASK(inode->pos, 1);

			left = tnode_new(inode->key&(~m), inode->pos + 1,
@@ -715,6 +688,8 @@ static struct tnode *inflate(struct trie *t, struct tnode *tn, int *err)

	for (i = 0; i < olen; i++) {
		struct node *node = tnode_get_child(oldtnode, i);
		struct tnode *left, *right;
		int size, j;

		/* An empty child */
		if (node == NULL)
@@ -740,12 +715,10 @@ static struct tnode *inflate(struct trie *t, struct tnode *tn, int *err)
			put_child(t, tn, 2*i+1, inode->child[1]);

			tnode_free(inode);
			continue;
		}

		/* An internal node with more than two children */
		else {
			struct tnode *left, *right;
			int size, j;

		/* We will replace this node 'inode' with two new
		 * ones, 'left' and 'right', each with half of the
@@ -771,14 +744,12 @@ static struct tnode *inflate(struct trie *t, struct tnode *tn, int *err)
		left = (struct tnode *) tnode_get_child(tn, 2*i);
		put_child(t, tn, 2*i, NULL);

			if (!left)
				BUG();
		BUG_ON(!left);

		right = (struct tnode *) tnode_get_child(tn, 2*i+1);
		put_child(t, tn, 2*i+1, NULL);

			if (!right)
				BUG();
		BUG_ON(!right);

		size = tnode_child_length(left);
		for (j = 0; j < size; j++) {
@@ -790,7 +761,6 @@ static struct tnode *inflate(struct trie *t, struct tnode *tn, int *err)

		tnode_free(inode);
	}
	}
	tnode_free(oldtnode);
	return tn;
}
@@ -802,7 +772,7 @@ static struct tnode *halve(struct trie *t, struct tnode *tn, int *err)
	int i;
	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);

@@ -850,6 +820,8 @@ static struct tnode *halve(struct trie *t, struct tnode *tn, int *err)
	}

	for (i = 0; i < olen; i += 2) {
		struct tnode *newBinNode;

		left = tnode_get_child(oldtnode, i);
		right = tnode_get_child(oldtnode, i+1);

@@ -858,30 +830,33 @@ static struct tnode *halve(struct trie *t, struct tnode *tn, int *err)
			if (right == NULL)    /* Both are empty */
				continue;
			put_child(t, tn, i/2, right);
		} else if (right == NULL)
			continue;
		} 

		if (right == NULL) {
			put_child(t, tn, i/2, left);
			continue;
		}

		/* Two nonempty children */
		else {
			struct tnode *newBinNode =
				(struct tnode *) tnode_get_child(tn, i/2);
		newBinNode = (struct tnode *) tnode_get_child(tn, i/2);
		put_child(t, tn, i/2, NULL);

			if (!newBinNode)
				BUG();
		BUG_ON(!newBinNode);

		put_child(t, newBinNode, 0, left);
		put_child(t, newBinNode, 1, right);
		put_child(t, tn, i/2, resize(t, newBinNode));
	}
	}
	tnode_free(oldtnode);
	return tn;
}

static void *trie_init(struct trie *t)
static void trie_init(struct trie *t)
{
	if (t) {
	if (!t)
		return;

	t->size = 0;
	t->trie = NULL;
	t->revision = 0;
@@ -889,44 +864,40 @@ static void *trie_init(struct trie *t)
	memset(&t->stats, 0, sizeof(struct trie_use_stats));
#endif
}
	return t;
}

static struct leaf_info *find_leaf_info(struct hlist_head *head, int plen)
{
	struct hlist_node *node;
	struct leaf_info *li;

	hlist_for_each_entry(li, node, head, hlist) {
	hlist_for_each_entry(li, node, head, hlist)
		if (li->plen == plen)
			return li;
	}

	return NULL;
}

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);

	if (li)
		fa_head = &li->falh;
	if (!li)
		return NULL;

	return fa_head;
	return &li->falh;
}

static void insert_leaf_info(struct hlist_head *head, struct leaf_info *new)
{
	struct leaf_info *li = NULL, *last = NULL;
	struct hlist_node *node, *tmp;
	struct hlist_node *node;

	write_lock_bh(&fib_lock);

	if (hlist_empty(head))
	if (hlist_empty(head)) {
		hlist_add_head(&new->hlist, head);
	else {
		hlist_for_each_entry_safe(li, node, tmp, head, hlist) {
		
	} else {
		hlist_for_each_entry(li, node, head, hlist) {
			if (new->plen > li->plen)
				break;

@@ -958,43 +929,41 @@ fib_find_node(struct trie *t, u32 key)
		if (tkey_sub_equals(tn->key, pos, tn->pos-pos, key)) {
			pos = tn->pos + tn->bits;
			n = tnode_get_child(tn, tkey_extract_bits(key, tn->pos, tn->bits));
		}
		else
		} else
			break;
	}
	/* Case we have found a leaf. Compare prefixes */

	if (n != NULL && IS_LEAF(n) && tkey_equals(key, n->key)) {
		struct leaf *l = (struct leaf *) n;
		return l;
	}
	if (n != NULL && IS_LEAF(n) && tkey_equals(key, n->key))
		return (struct leaf *)n;

	return NULL;
}

static struct node *trie_rebalance(struct trie *t, struct tnode *tn)
{
	int i = 0;
	int i;
	int wasfull;
	t_key cindex, key;
	struct tnode *tp = NULL;

	if (!tn)
		BUG();
	BUG_ON(!tn);

	key = tn->key;
	i = 0;

	while (tn != NULL && NODE_PARENT(tn) != NULL) {

		if (i > 10) {
			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);
			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++;

		tp = NODE_PARENT(tn);
@@ -1062,8 +1031,7 @@ fib_insert_node(struct trie *t, int *err, u32 key, int plen)
				printk("BUG tn=%p, n->parent=%p\n", tn, NODE_PARENT(n));
				BUG();
			}
		}
		else
		} else
			break;
	}

@@ -1073,9 +1041,7 @@ fib_insert_node(struct trie *t, int *err, u32 key, int plen)
	 * tp is n's (parent) ----> NULL or TNODE
	 */

	if (tp && IS_LEAF(tp))
		BUG();

	BUG_ON(tp && IS_LEAF(tp));

	/* Case 1: n is a leaf. Compare prefixes */

@@ -1113,21 +1079,17 @@ fib_insert_node(struct trie *t, int *err, u32 key, int plen)
	fa_head = &li->falh;
	insert_leaf_info(&l->list, li);

	/* Case 2: n is NULL, and will just insert a new leaf */
	if (t->trie && n == NULL) {
		/* Case 2: n is NULL, and will just insert a new leaf */

		NODE_SET_PARENT(l, tp);

		if (!tp)
			BUG();
		BUG_ON(!tp);

		else {
		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. */
	else {
		/*
		 *  Add a new tnode here
		 *  first tnode need some special handling
@@ -1137,11 +1099,11 @@ fib_insert_node(struct trie *t, int *err, u32 key, int plen)
			pos = tp->pos+tp->bits;
		else
			pos = 0;

		if (n) {
			newpos = tkey_mismatch(key, pos, n->key);
			tn = tnode_new(n->key, newpos, 1);
		}
		else {
		} else {
			newpos = 0;
			tn = tnode_new(key, newpos, 1); /* First tnode */
		}
@@ -1162,21 +1124,21 @@ fib_insert_node(struct trie *t, int *err, u32 key, int plen)
		if (tp) {
			cindex = tkey_extract_bits(key, tp->pos, tp->bits);
			put_child(t, (struct tnode *)tp, cindex, (struct node *)tn);
		}
		else {
		} else {
			t->trie = (struct node*) tn; /* First tnode */
			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",
		       tp, tp->pos, tp->bits, key, plen);
	}

	/* Rebalance the trie */
	t->trie = trie_rebalance(t, tp);
done:
	t->revision++;
err:;
err:
	return fa_head;
}

@@ -1204,8 +1166,7 @@ fn_trie_insert(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta,

	key = ntohl(key);

	if (trie_debug)
		printk("Insert table=%d %08x/%d\n", tb->tb_id, key, plen);
	DBG("Insert table=%d %08x/%d\n", tb->tb_id, key, plen);

	mask = ntohl(inet_make_mask(plen));

@@ -1214,7 +1175,9 @@ fn_trie_insert(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta,

	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;

	l = fib_find_node(t, key);
@@ -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.
	 */

	if (fa &&
	    fa->fa_info->fib_priority == fi->fib_priority) {
	if (fa && fa->fa_info->fib_priority == fi->fib_priority) {
		struct fib_alias *fa_orig;

		err = -EEXIST;
@@ -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_scope = r->rtm_scope;
	new_fa->fa_state = 0;
#if 0
	new_fa->dst = NULL;
#endif
	/*
	 * Insert new entry to the list.
	 */
@@ -1314,8 +1273,7 @@ fn_trie_insert(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta,

	write_lock_bh(&fib_lock);

	list_add_tail(&new_fa->fa_list,
		 (fa ? &fa->fa_list : fa_head));
	list_add_tail(&new_fa->fa_list, (fa ? &fa->fa_list : fa_head));

	write_unlock_bh(&fib_lock);

@@ -1328,7 +1286,7 @@ fn_trie_insert(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta,
	kmem_cache_free(fn_alias_kmem, new_fa);
out:
	fib_release_info(fi);
err:;
err:
	return err;
}

@@ -1342,7 +1300,6 @@ static inline int check_leaf(struct trie *t, struct leaf *l, t_key key, int *pl
	struct hlist_node *node;

	hlist_for_each_entry(li, node, hhead, hlist) {

		i = li->plen;
		mask = ntohl(inet_make_mask(i));
		if (l->key != (key & mask))
@@ -1374,9 +1331,14 @@ fn_trie_lookup(struct fib_table *tb, const struct flowi *flp, struct fib_result
	int chopped_off;
	t_key cindex = 0;
	int current_prefix_length = KEYLENGTH;
	struct tnode *cn;
	t_key node_prefix, key_prefix, pref_mismatch;
	int mp;

	n = t->trie;

	read_lock(&fib_lock);

	if (!n)
		goto failed;

@@ -1394,7 +1356,6 @@ fn_trie_lookup(struct fib_table *tb, const struct flowi *flp, struct fib_result
	chopped_off = 0;

	while (pn) {

		pos = pn->pos;
		bits = pn->bits;

@@ -1410,12 +1371,16 @@ fn_trie_lookup(struct fib_table *tb, const struct flowi *flp, struct fib_result
			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
#ifdef HL_OPTIMIZE
			struct tnode *cn = (struct tnode *)n;
			t_key node_prefix, key_prefix, pref_mismatch;
			int mp;
		cn = (struct tnode *)n;

		/*
		 * It's a tnode, and we can do some extra checks here if we
@@ -1506,18 +1471,13 @@ fn_trie_lookup(struct fib_table *tb, const struct flowi *flp, struct fib_result
		pn = (struct tnode *)n; /* Descend */
		chopped_off = 0;
		continue;
		}
		if (IS_LEAF(n)) {
			if ((ret = check_leaf(t, (struct leaf *)n, key, &plen, flp, res)) <= 0)
				goto found;
	       }

backtrace:
		chopped_off++;

		/* 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++;
		}

		/* Decrease current_... with bits chopped off */
		if (current_prefix_length > pn->pos + pn->bits - chopped_off)
@@ -1528,9 +1488,9 @@ fn_trie_lookup(struct fib_table *tb, const struct flowi *flp, struct fib_result
		 * 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));
		else {
		} else {
			if (NODE_PARENT(pn) == NULL)
				goto failed;

@@ -1559,8 +1519,7 @@ static int trie_leaf_remove(struct trie *t, t_key key)
	struct node *n = t->trie;
	struct leaf *l;

	if (trie_debug)
		printk("entering trie_leaf_remove(%p)\n", n);
	DBG("entering trie_leaf_remove(%p)\n", n);

	/* 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
@@ -1597,8 +1556,7 @@ static int trie_leaf_remove(struct trie *t, t_key key)
		cindex = tkey_extract_bits(key, tp->pos, tp->bits);
		put_child(t, (struct tnode *)tp, cindex, NULL);
		t->trie = trie_rebalance(t, tp);
	}
	else
	} else
		t->trie = NULL;

	return 1;
@@ -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 list_head *fa_head;
	struct leaf *l;
	int kill_li = 0;
	struct leaf_info *li;


	if (plen > 32)
		return -EINVAL;
@@ -1641,8 +1602,7 @@ fn_trie_delete(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta,
	if (!fa)
		return -ESRCH;

	if (trie_debug)
		printk("Deleting %08x/%d tos=%d t=%p\n", key, plen, tos, t);
	DBG("Deleting %08x/%d tos=%d t=%p\n", key, plen, tos, t);

	fa_to_delete = NULL;
	fa_head = fa->fa_list.prev;
@@ -1664,9 +1624,8 @@ fn_trie_delete(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta,
		}
	}

	if (fa_to_delete) {
		int kill_li = 0;
		struct leaf_info *li;
	if (!fa_to_delete)
		return -ESRCH;

	fa = fa_to_delete;
	rtmsg_fib(RTM_DELROUTE, htonl(key), fa, plen, tb->tb_id, nlhdr, req);
@@ -1696,8 +1655,6 @@ fn_trie_delete(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta,
	fn_free_alias(fa);
	return 0;
}
	return -ESRCH;
}

static int trie_flush_list(struct trie *t, struct list_head *head)
{
@@ -1708,7 +1665,6 @@ static int trie_flush_list(struct trie *t, struct list_head *head)
		struct fib_info *fi = fa->fa_info;

		if (fi && (fi->fib_flags&RTNH_F_DEAD)) {

 			write_lock_bh(&fib_lock);
			list_del(&fa->fa_list);
			write_unlock_bh(&fib_lock);
@@ -1728,11 +1684,9 @@ static int trie_flush_leaf(struct trie *t, struct leaf *l)
	struct leaf_info *li = NULL;

	hlist_for_each_entry_safe(li, node, tmp, lih, hlist) {
		
		found += trie_flush_list(t, &li->falh);

		if (list_empty(&li->falh)) {

 			write_lock_bh(&fib_lock);
			hlist_del(&li->hlist);
			write_unlock_bh(&fib_lock);
@@ -1757,8 +1711,7 @@ static struct leaf *nextleaf(struct trie *t, struct leaf *thisleaf)
			return (struct leaf *) t->trie;

		p = (struct tnode*) t->trie;  /* Start */
	}
	else
	} else
		p = (struct tnode *) NODE_PARENT(c);

	while (p) {
@@ -1772,10 +1725,10 @@ static struct leaf *nextleaf(struct trie *t, struct leaf *thisleaf)

		last = 1 << p->bits;
		for (idx = pos; idx < last ; idx++) {
			if (p->child[idx]) {
			if (!p->child[idx])
				continue;

			/* Decend if tnode */

			while (IS_TNODE(p->child[idx])) {
				p = (struct tnode*) p->child[idx];
				idx = 0;
@@ -1790,7 +1743,6 @@ static struct leaf *nextleaf(struct trie *t, struct leaf *thisleaf)
			}
			return (struct leaf*) p->child[idx];
		}
		}
up:
		/* No more children go up one step  */
		c = (struct node *) p;
@@ -1818,8 +1770,7 @@ static int fn_trie_flush(struct fib_table *tb)
	if (ll && hlist_empty(&ll->list))
		trie_leaf_remove(t, ll->key);

	if (trie_debug)
		printk("trie_flush found=%d\n", found);
	DBG("trie_flush found=%d\n", found);
	return found;
}

@@ -1959,10 +1910,10 @@ static int fn_trie_dump_plen(struct trie *t, int plen, struct fib_table *tb, str
	int h, s_h;
	struct list_head *fa_head;
	struct leaf *l = NULL;

	s_h = cb->args[2];

	for (h = 0; (l = nextleaf(t, l)) != NULL; h++) {

		if (h < s_h)
			continue;
		if (h > s_h)
@@ -1995,7 +1946,6 @@ static int fn_trie_dump(struct fib_table *tb, struct sk_buff *skb, struct netlin

	read_lock(&fib_lock);
	for (m = 0; m <= 32; m++) {

		if (m < s_m)
			continue;
		if (m > s_m)
@@ -2065,7 +2015,8 @@ struct fib_table * __init fib_hash_init(int id)

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)
@@ -2086,27 +2037,20 @@ static void printnode_seq(struct seq_file *seq, int indent, struct node *n,
		seq_printf(seq, "%d/", cindex);
		printbin_seq(seq, cindex, bits);
		seq_printf(seq, ": ");
	}
	else
	} else
		seq_printf(seq, "<root>: ");
	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)) {
		struct leaf *l = (struct leaf *)n;
		struct fib_alias *fa;
		int 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);

				if (!fa_head)
@@ -2118,17 +2062,16 @@ static void printnode_seq(struct seq_file *seq, int indent, struct node *n,
				putspace_seq(seq, indent+2);
				seq_printf(seq, "{/%d...dumping}\n", i);


				list_for_each_entry(fa, fa_head, fa_list) {
					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) {
						seq_printf(seq, "Error fa_info=NULL\n");
						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",
					      fa->fa_type,
@@ -2136,9 +2079,14 @@ static void printnode_seq(struct seq_file *seq, int indent, struct node *n,
					      fa->fa_tos);
				}
			}
	}
	else if (IS_TNODE(n)) {
	} else {
		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, "|    ");
		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);
@@ -2159,14 +2107,27 @@ static void trie_dump_seq(struct seq_file *seq, struct trie *t)
	int indent = 1;
	int pend = 0;
	int depth = 0;
	struct tnode *tn;

  	read_lock(&fib_lock);

	seq_printf(seq, "------ trie_dump of t=%p ------\n", t);
	if (n) {

	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)) {
			struct tnode *tn = (struct tnode *)n;

	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;
@@ -2174,29 +2135,24 @@ static void trie_dump_seq(struct seq_file *seq, struct trie *t)

	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 {
			} else {
				/*
				 * New tnode. Decend one level
				 */

				depth++;
						n = tn->child[cindex];
						tn = (struct tnode *)n;
				tn = (struct tnode *)tn->child[cindex];
				pend = tn->pos + tn->bits;
				putspace_seq(seq, indent); seq_printf(seq, "\\--\n");
				indent += 3;
				cindex = 0;
			}
				}
				else
		} else
			cindex++;

		/*
@@ -2204,7 +2160,6 @@ static void trie_dump_seq(struct seq_file *seq, struct trie *t)
		 */

		while (cindex >= (1 << tn->bits)) {

			/*
			 * Move upwards and test for root
			 * pop off all traversed  nodes
@@ -2212,34 +2167,30 @@ static void trie_dump_seq(struct seq_file *seq, struct trie *t)

			if (NODE_PARENT(tn) == NULL) {
				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;
			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);
}

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;

	if (s) {
	s = kmalloc(sizeof(struct trie_stat), GFP_KERNEL);
	if (!s)
		return NULL;

	s->totdepth = 0;
	s->maxdepth = 0;
	s->tnodes = 0;
@@ -2248,7 +2199,7 @@ static struct trie_stat *trie_stat_new(void)

	for (i = 0; i < MAX_CHILDS; i++)
		s->nodesizes[i] = 0;
	}

	return s;
}

@@ -2257,18 +2208,19 @@ static struct trie_stat *trie_collect_stats(struct trie *t)
	struct node *n = t->trie;
	struct trie_stat *s = trie_stat_new();
	int cindex = 0;
	int indent = 1;
	int pend = 0;
	int depth = 0;

	if (!s)
		return NULL;
	if (!n)
		return s;

	read_lock(&fib_lock);

	if (s) {
		if (n) {
	if (IS_TNODE(n)) {
		struct tnode *tn = (struct tnode *)n;
		pend = tn->pos+tn->bits;
				indent += 3;
		s->nodesizes[tn->bits]++;
		depth++;

@@ -2284,9 +2236,7 @@ static struct trie_stat *trie_collect_stats(struct trie *t)
						s->maxdepth = depth;
					s->totdepth += depth;
					s->leaves++;
						}
				
						else {
				} else {
					/*
					 * New tnode. Decend one level
					 */
@@ -2299,11 +2249,9 @@ static struct trie_stat *trie_collect_stats(struct trie *t)
					tn = (struct tnode *)n;
					pend = tn->pos+tn->bits;

							indent += 3;
					cindex = 0;
				}
					}
					else {
			} else {
				cindex++;
				s->nullpointers++;
			}
@@ -2313,33 +2261,26 @@ static struct trie_stat *trie_collect_stats(struct trie *t)
			 */

			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;
				}
						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 n = NULL;
		}
	}

	read_unlock(&fib_lock);
	return s;
@@ -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)
{
	void *v = NULL;
	if (!ip_fib_main_table)
		return NULL;

	if (ip_fib_main_table)
		v = *pos ? fib_triestat_get_next(seq) : SEQ_START_TOKEN;
	return v;
	if (*pos)
		return fib_triestat_get_next(seq);
	else
		return SEQ_START_TOKEN;
}

static void *fib_triestat_seq_next(struct seq_file *seq, void *v, loff_t *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)
@@ -2455,10 +2401,8 @@ static int fib_triestat_seq_show(struct seq_file *seq, void *v)

		if (trie_main)
			collect_and_show(trie_main, seq);
	}
	else {
		snprintf(bf, sizeof(bf),
			 "*\t%08X\t%08X", 200, 400);
	} else {
		snprintf(bf, sizeof(bf), "*\t%08X\t%08X", 200, 400);

		seq_printf(seq, "%-127s\n", bf);
	}
@@ -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)
{
	void *v = NULL;
	if (!ip_fib_main_table)
		return NULL;

	if (ip_fib_main_table)
		v = *pos ? fib_trie_get_next(seq) : SEQ_START_TOKEN;
	return v;
	if (*pos)
		return fib_trie_get_next(seq);
	else
		return SEQ_START_TOKEN;
}

static void *fib_trie_seq_next(struct seq_file *seq, void *v, loff_t *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)
{

}

/*
@@ -2555,9 +2504,7 @@ static int fib_trie_seq_show(struct seq_file *seq, void *v)

		if (trie_main)
			trie_dump_seq(seq, trie_main);
	}

	else {
	} else {
		snprintf(bf, sizeof(bf),
			 "*\t%08X\t%08X", 200, 400);
		seq_printf(seq, "%-127s\n", bf);