net/ipv4/route.c
author: elvis
linux 在設定 route 有兩個機制,一個是 fib,一個是 dynamic 產生的 routing
fib 是利用 route (man 8 route) 指定來靜態 route table
而 net/ipv4/route.c 所做的則是動能產生 routing hash,以加快 route decision 的速度
範例:
- 靜態 route table
# route -FKernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.1.3 * 255.255.255.255 UH 0 0 0 eth0 140.123.103.156 * 255.255.255.255 UH 0 0 0 eth1 192.168.1.0 * 255.255.255.0 U 0 0 0 eth0 140.123.0.0 * 255.255.0.0 U 0 0 0 eth1 127.0.0.0 * 255.0.0.0 U 0 0 0 lo default CSIE-C5505 0.0.0.0 UG 0 0 0 eth1
- 動態 route table
# route -CKernel IP routing cache Source Destination Gateway Flags Metric Ref Use Iface infomedia.cs.cc 140.123.103.255 140.123.103.255 ri 0 6 0 eth1 CSIE-C5505 not-a-legal-add not-a-legal-add ibl 0 796989 0 lo fish.cs.ccu.edu 140.123.103.255 140.123.103.255 ri 0 2 0 eth1 didi.cs.ccu.edu 140.123.103.255 140.123.103.255 ri 0 31 0 eth1 lena.cs.ccu.edu 140.123.103.255 140.123.103.255 ri 0 2438 0 eth1 filter.cs.ccu.e 140.123.103.255 140.123.103.255 ri 0 265 0 eth1 CSIE-C5505 ALL-SYSTEMS.MCA ALL-SYSTEMS.MCA ml 0 7273 0 lo vivi.cs.ccu.edu 140.123.103.255 140.123.103.255 ri 0 7 0 eth1 gigi.cs.ccu.edu 140.123.103.255 140.123.103.255 ri 0 3 0 eth1 scoap.cs.ccu.ed 140.123.103.255 140.123.103.255 ri 0 12 0 eth1 bist.cs.ccu.edu 140.123.103.255 140.123.103.255 ri 0 86 0 eth1 macgyver.cs.ccu 140.123.103.255 140.123.103.255 ri 0 11 0 eth1
- RFC 1122 有關 IP 層 routing 的說明 這裡
一個 skb 要決定行走的路徑(input) 時 會呼叫如 ip_route_input(skb,...); 來決定他的 rtable 流程大概是 1.ip_route_input 查看是否有存在的 rtable 2.呼叫 ip_route_input_slow 產生 rtable 3.ip_route_input_slow 呼叫 fib_lookup 4.fib_lookup 從 fib_rules 找出 fib_rule 5.fib_lookup 呼叫 fib_get_table 來取得 fib_table 6.fib_lookup 使用 fib_table 呼叫 tb->tb_lookup 取得 fib_result 7.fib_lookup 將 fib_rule 設定給 fib_result 8.ip_route_input_slow 利用 rt_intern_hash 設定 rtable 9.返回 ip_route_input 此時 skb 已有設定好的 rtable還有 input/output 方法 10.返回原呼叫者 11.原呼叫者呼叫 dst->input() 處理table
/* IPv4 routing cache flags */
#define RTCF_DEAD RTNH_F_DEAD
#define RTCF_ONLINK RTNH_F_ONLINK
/* Obsolete flag. About to be deleted */
#define RTCF_NOPMTUDISC RTM_F_NOPMTUDISC
#define RTCF_NOTIFY 0x00010000
#define RTCF_DIRECTDST 0x00020000
#define RTCF_REDIRECTED 0x00040000
#define RTCF_TPROXY 0x00080000
#define RTCF_FAST 0x00200000
#define RTCF_MASQ 0x00400000
#define RTCF_SNAT 0x00800000
#define RTCF_DOREDIRECT 0x01000000
#define RTCF_DIRECTSRC 0x04000000
#define RTCF_DNAT 0x08000000
#define RTCF_BROADCAST 0x10000000
#define RTCF_MULTICAST 0x20000000
#define RTCF_REJECT 0x40000000
#define RTCF_LOCAL 0x80000000
#define RTCF_NAT (RTCF_DNAT|RTCF_SNAT)
routing type
enum
{
RTN_UNSPEC,
RTN_UNICAST, /* Gateway or direct route */
RTN_LOCAL, /* Accept locally */
RTN_BROADCAST, /* Accept locally as broadcast,
send as broadcast */
RTN_ANYCAST, /* Accept locally as broadcast,
but send as unicast */
RTN_MULTICAST, /* Multicast route */
RTN_BLACKHOLE, /* Drop */
RTN_UNREACHABLE, /* Destination is unreachable */
RTN_PROHIBIT, /* Administratively prohibited */
RTN_THROW, /* Not in this table */
RTN_NAT, /* Translate this address */
RTN_XRESOLVE, /* Use external resolver */
};
scope 的值
enum rt_scope_t
{
RT_SCOPE_UNIVERSE=0,
/* User defined values */
RT_SCOPE_SITE=200,
RT_SCOPE_LINK=253,
RT_SCOPE_HOST=254,
RT_SCOPE_NOWHERE=255
};
●用來查詢 fib table 的鍵值 struct rt_key @include/net/route.h { __u32 dst; 目的 ip 位址 __u32 src; 來源 ip 位址 int iif; input interface int oif; output interface __u8 tos; type of service __u8 scope; Unknown }; ●route table 最主要的結構 一些判斷位址的 macro #define LOOPBACK(x) (((x) & htonl(0xff000000)) == htonl(0x7f000000)) 127.0.0.0/255.0.0.0 loopback #define MULTICAST(x) (((x) & htonl(0xf0000000)) == htonl(0xe0000000)) class D 1110 開頭 #define BADCLASS(x) (((x) & htonl(0xf0000000)) == htonl(0xf0000000)) class E 保留的區段,不應該有人用到 11110 開頭 #define ZERONET(x) (((x) & htonl(0xff000000)) == htonl(0x00000000)) class A 且 netid 為 0000000 (七個 0) #define LOCAL_MCAST(x) (((x) & htonl(0xFFFFFF00)) == htonl(0xE0000000)) class D multicast ip 且 只有最小的 8bit struct rtable @include/net/route.h { union { struct dst_entry dst; struct rtable *rt_next; } u; unsigned rt_flags; RTCF_MULTICAST 之類的 flag unsigned rt_type; RTN_UNICAST 之類的型態,用來表示此 route rule 是哪種型態 __u32 rt_dst; /* Path destination */ __u32 rt_src; /* Path source */ int rt_iif; incoming interface /* Info on neighbour */ __u32 rt_gateway; /* Cache lookup keys */ struct rt_key key; /* Miscellaneous cached information */ __u32 rt_spec_dst; /* RFC1122 specific destination */ inet_peer 的結構是個 avl tree 用來紀錄各個 peer 的資訊 詳細 implement 在 inetpeer.h 與 inetpeer.c struct inet_peer *peer; /* long-li

本文主要探讨Linux内核中net/ipv4/route.c文件的内容,包括静态和动态路由表的展示,路由处理流程,以及关键的数据结构和函数详解。介绍了路由初始化、路由流程图、路由表的维护,如rt_hash_table的使用,以及路由缓存的管理等核心概念。
最低0.47元/天 解锁文章

被折叠的 条评论
为什么被折叠?



