linux内核早先对于ipv4的fib支持Hash与trie两种数据组织方式,后续删除了hash,只留下了trie。
trie是一种最长前缀匹配算法,对于大量路由的情况下查找效率高于hash,但是这种算法会消耗更多的内存、算法更加复杂。
图1 原始数据
图2 trie
将图1中的数据用trie表示那么就可以得到图2的模型。那么是怎么画出这幅图的呢?实际上就是从根节点触发,往左子树走一步则增加一个0,往右子树走则增加1,如此循环下去,直到叶子节点。
Use new experimental LC-trie as FIB lookup algorithm. This improves lookup performance if you have a large number of routes.
LC-trie is a longest matching prefix lookup algorithm which performs better than FIB_HASH for large routing tables. But, it consumes more memory and is more complex.
LC-trie is described in:
IP-address lookup using LC-tries. Stefan Nilsson and Gunnar Karlsson IEEE Journal on Selected Areas in Communications, 17(6):1083-1092,- June 1999–
An experimental study of compression methods for dynamic tries- Stefan Nilsson and Matti Tikkanen. Algorithmica, 33(1):19-33, 2002.- http://www.csc.kth.se/~snilsson/software/dyntrie2/
首先看看linux代码中提供的帮助理解trie的说明
/* To understand this stuff, an understanding of keys and all their bits is
- necessary. Every node in the trie has a key associated with it, but not
- all of the bits in that key are significant.
- Consider a node ‘n’ and its parent ‘tp’.
- If n is a leaf, every bit in its key is significant. Its presence is
- necessitated by path compression, since during a tree traversal (when
- searching for a leaf - unless we are doing an insertion) we will completely
- ignore all skipped bits we encounter. Thus we need to verify, at the end of
- a potentially successful search, that we have indeed been walking the
- correct key path.
- Note that we can never “miss” the co