Linux Fib实现分析

本文探讨Linux内核中用于IPv4路由查找的FIB机制,主要关注LC-Trie算法。LC-Trie是一种最长前缀匹配算法,适用于大型路由表,虽然内存消耗大且更复杂。文章通过代码分析和示例解释了LC-Trie的工作原理,并提到了路由添加流程。
摘要由CSDN通过智能技术生成

linux内核早先对于ipv4的fib支持Hash与trie两种数据组织方式,后续删除了hash,只留下了trie。
trie是一种最长前缀匹配算法,对于大量路由的情况下查找效率高于hash,但是这种算法会消耗更多的内存、算法更加复杂。数据

图1 原始数据

trie
图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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值