幾年前我曾寫過好幾篇關於 Linux 移除路由 cache 支援的文章,路由 cache 的退場源自一次對路由子系統的重構,具體原因不再重複。本文要介紹的是,這次重構對 Redirect 路由以及 neighbour 子系統帶來的影響。
事實上,直到最近三個月我才發現這些影響竟如此之大。工作細節不便詳述,這裡只是把關於開放原始碼 Linux 核心協議棧的一些實作知識做個彙整,方便日後查閱;若有誰也因而受益,則不勝榮幸。
IP 協議棧中,IP 發送由兩部分組成:
要成功送出一個封包,必須有對應的路由,這部分由 IP 協議規範的路由查找邏輯完成。對 Linux 而言,最終查找結果是一個 rtable 結構體,表示一個路由項,其內嵌的第一個欄位是 dst_entry,因此兩者可相互強制轉換,其中重要的欄位是 rt_gateway——它只是把封包往目的地送時的下一跳 IP,是 IP 逐跳轉送的核心。至此,IP 路由查找就結束了。
路由查找階段已知 rt_gateway,接著要往二層落實,這就是 IP neighbour 解析的工作。rt_gateway 就是 neighbour,現在需把它解析成硬體位址。所謂 neighbour,就是邏輯上與本機直連的所有網卡裝置;「邏輯上直連」意味著,對乙太網路而言,整條網路上的裝置都可能是本機鄰居,關鍵看誰被選為當前封包的下一跳;而對 POINTOPOINT 裝置而言,其鄰居只有唯一一個(即對端),唯一意味著不需要解析硬體位址!值得注意的是,若無視這個差異,將帶來巨大的效能損失,這點本文最後會說明。
為了描述方便,以下不再提 rtable,一律以 dst_entry 代表路由查找結果。文中程式碼並非實際的 Linux 協議棧程式碼,而是為表述方便抽象出的偽程式碼;這裡的 dst_entry 只是代表一個路由項,因為 dst_entry 表示與協議無關的部分,本文內容也與具體協議無關。
在 3.5 版之前,路由子系統存在一個路由 cache 雜湊表,快取了最近最常使用的一些 dst_entry(IPv4 即 rtable)路由項,對封包先以其 IP 位址元組查路由 cache,命中就直接取出 dst_entry,否則再查系統路由表。
到了 3.5 核心,路由 cache 消失了,其緣由已有他文描述。路由 cache 的移除對 neighbour 子系統產生了副作用,而這個副作用被證明是有益的。在詳述之前,先說另一個變化——Redirect 路由實作的改變。
所謂 Redirect 路由,肯定是對本機已存在路由項的 Redirect。早期核心中,重定向路由被保存在不同位置(如 inet_peer),意味著路由子系統與協議棧其他部分發生耦合。其實不管 Redirect 路由存在哪裡,最終它都得進入路由 cache 才能起作用;而當路由 cache 完全消失後,Redirect 路由的保存位置問題才暴露出來。為了「在路由子系統內部解決 Redirect 路由問題」,重構後的核心在路由表中為每個路由項保存一個 exception 雜湊表,一個路由項 Fib_info 類似於:
Fib_info {
Address nexhop;
Hash_list exception;
};
exception 表的表項類似:
Exception_entry {
Match_info info;
Address new_nexthop;
};
如此一來,收到 Redirect 路由時會初始化一個 Exception_entry 並插入相應的 exception 雜湊表;查詢路由時,若最終找到一個 Fib_info,在構建最終 dst_entry 前,會先用諸如來源 IP 之類的 Match_info 去查 exception 雜湊表,若找到匹配的 Exception_entry,就不再用 Fib_info 的 nexhop,而是用 Exception_entry 的 new_nexthop 來構建 dst_entry。
簡單介紹 Redirect 路由後,接下來的篇幅將全部用於說明路由與 neighbour 的關係。
以下是網上摘錄的、關於路由 cache 移除後對 neighbour 的影響:
Neighbours其實兩者本不該有關聯——路由子系統與 neighbour 子系統是處在上下不同層次的子系統,合理方式是透過路由項的 nexthop 值承上啟下,以一個唯一的 neighbour 查找介面關聯即可:
dst_entry = 路由表查找(或者路由cache查找,通過skb的destination作鍵值) nexthop = dst_entry.nexthop neigh = neighbour表查找(通過nexthop作為鍵值)
然而 Linux 協議棧的實作遠比這複雜,這一切得從 3.5 重構前說起。
重構前由於存在路由 cache,凡能在 cache 中找到 dst_entry 的 skb 就不必再查路由表。路由 cache 的假設是:絕大多數 skb 都不需要查路由表,理想情況下都能在 cache 命中。對 neighbour 而言,顯而易見的做法是把 neighbour 與 dst_entry 綁定,在 cache 找到 dst_entry 也就一起找到 neighbour。也就是說,路由 cache 不僅快取 dst_entry,還快取 neighbour。事實上,3.5 前的 dst_entry 就有一個 neighbour 欄位,從路由 cache 找到路由項後直接取出即可呼叫其 output 回呼函式。
可以推導出 dst_entry 與 neighbour 的綁定時機:查找路由表之後,即在路由 cache 未命中時,完成路由表查找後、把結果插入路由 cache 之前,執行一個 neighbour 綁定的邏輯。和路由 cache 一樣,neighbour 子系統也維護一張 neighbour 表,執行替換、更新、過期等狀態操作,這張表與路由 cache 表之間存在巨大耦合。整體邏輯如下:
func ip_output(skb): dst_entry = lookup_from_cache(skb.destination); if dst_entry == NULL then dst_entry = lookup_fib(skb.destination); nexthop = dst_entry.gateway?:skb.destination; neigh = lookup(neighbour_table, nexthop); if neigh == NULL then neigh = create(neighbour_table, nexthop); neighbour_add_timer(neigh); end dst_entry.neighbour = neigh; insert_into_route_cache(dst_entry); end neigh = dst_entry.neighbour; neigh.output(neigh, skb); endfunc ---->TO Layer2
試想兩個問題:如果 neighbour 定時器執行時某個 neighbour 過期了,可以刪除嗎?如果路由 cache 定時器執行時某條 cache 過期了,可以刪除嗎?能精確回答這兩題,就對兩個子系統的關係足夠了解了。先看第一題:若刪除 neighbour,而與它綁定的路由 cache 項還在,後續 skb 匹配到該 cache 項時便無法取出 neighbour;由於綁定只發生在 cache 未命中時,此時無法重新綁定,且路由項與 neighbour 是多對一關係,neighbour 無法反向引用 cache 項,透過 dst_entry.neighbour 引用一個已刪除的 neighbour 就是野指標,會引發 oops 最終導致核心 panic。因此答案顯而易見:即便 neighbour 過期也不能刪除,只能標記為無效,這可透過引用計數做到。再看第二題:路由 cache 過期可以刪除,但要記得遞減與它綁定的 neighbour 引用計數,若為 0 則刪除 neighbour。由此可見,路由 cache 與 neighbour 的耦合導致與 dst_entry 綁定的 neighbour 過期刪除只能從 cache 項發起。修改後的邏輯如下:
func ip_output(skb): dst_entry = lookup_from_cache(skb.destination); if dst_entry == NULL then dst_entry = lookup_fib(skb.destination); nexthop = dst_entry.gateway?:skb.destination; neigh = lookup(neighbour_table, nexthop); if neigh == NULL then neigh = create(neighbour_table, nexthop); neighbour_add_timer(neigh); end inc(neigh.refcnt); dst_entry.neighbour = neigh; insert_into_route_cache(dst_entry); end neigh = dst_entry.neighbour; # 如果是INVALID狀態的neigh,需要在output回呼中處理 neigh.output(neigh, skb); endfunc func neighbour_add_timer(neigh): inc(neigh.refcnt); neigh.timer.func = neighbour_timeout; timer_start(neigh.timer); endfunc func neighbour_timeout(neigh): cnt = dec(neigh.refcnt); if cnt == 0 then free_neigh(neigh); else neigh.status = INVALID; end endfunc func dst_entry_timeout(dst_entry): neigh = dst_entry.neighbour; cnt = dec(neigh.refcnt); if cnt == 0 then free_neigh(neigh); end free_dst(dst_entry); endfunc
最後看看這會帶來什麼問題。若 neighbour 表與路由 cache 表的 gc 參數不同步,比如 neighbour 過快到期而 cache 項到期很慢,就會有很多 neighbour 無法刪除,造成 neighbour 表爆滿;此時需強制回收路由 cache,這是 neighbour 子系統反饋到路由子系統的一個耦合,一切簡直太亂了:
func create(neighbour_table, nexthop): retry: neigh = alloc_neigh(nexthop); if neigh == NULL or neighbour_table.num > MAX then shrink_route_cache(); retry; end endfunc
關於路由 cache 的 gc 定時器與 neighbour 子系統的關係,有一篇寫得很好的文章《Tuning Linux IPv4 route cache》提到:
You may find documentation about those obsolete sysctl values:這一切在 3.5 核心之後發生了改變!!
經過重構,3.5 及此後的核心移除了對路由 cache 的支援,也就是說每個封包都得查路由表(暫不考慮在 socket 快取 dst_entry 的情形)。沒有路由 cache 就不必處理 cache 的過期與替換,整個路由子系統成了完全無狀態的系統,因此 dst_entry 再無需與 neighbour 綁定;既然每次都要重查路由表開銷也不大,每次少得多的 neighbour 表查詢開銷更可忽略,於是 dst_entry 移除了 neighbour 欄位,IP 發送邏輯如下:
func ip_output(skb): dst_entry = lookup_fib(skb.destination); nexthop = dst_entry.gateway?:skb.destination; neigh = lookup(neighbour_table, nexthop); if neigh == NULL then neigh = create(neighbour_table, nexthop); end neigh.output(skb); endfunc
路由項不再與 neighbour 關聯,neighbour 表就能獨立執行過期操作,因路由 cache gc 過慢而導致頻繁爆滿的情況也消失了。不仅如此,程式碼看起來也清爽多了。
很多講解 Linux neighbour 子系統的資料幾乎都在說 ARP,各種複雜的 ARP 操作、佇列、狀態機等,卻鮮少描述 ARP 之外的 neighbour。本文最後這個小節就補充一個例子。從問題開始:一個 NOARP 裝置(如 POINTOPOINT)發出的 skb,其 neighbour 是誰?
在廣播式乙太網路下,要把封包送到遠端需解析「下一跳」位址,每個封包都要經由一個 gateway 發出,它被抽象為一個同網段的 IP,需用 ARP 落實到確定的硬體位址。但對 pointopoint 裝置而言,與它對連的只有固定一個,沒有廣播或組播二層,也就沒有 gateway 概念,其下一跳就是目標 IP 本身。
依上述 ip_output,查 neighbour 表前用的鍵值是 nexthop;對 pointopoint 而言 nexthop 就是 skb 的目標位址,找不到便以此為鍵值建立。若使用 pointopoint 裝置發送的 skb 目標位址空間十分龐大,就會有海量 neighbour 同時被建立並插入 neighbour 表,這必然遭遇鎖的問題——它們的插入操作將全部自旋在 neighbour 表讀寫鎖的寫鎖上!neigh_create 邏輯如下:
struct neighbour *neigh_create(struct neigh_table *tbl, const void *pkey, struct net_device *dev) {
struct neighbour *n1, *rc, *n = neigh_alloc(tbl);
......
write_lock_bh(&tbl->lock); // 插入hash表
write_unlock_bh(&tbl->lock);
.......
}
在海量目標 IP 的 skb 通過 pointopoint 裝置發送時,這是一個完全避不開的瓶頸!然而核心沒這麼傻,它採用以下方式規避:
__be32 nexthop = ((struct rtable *)dst)->rt_gateway?:ip_hdr(skb)->daddr; if (dev->flags&(IFF_LOOPBACK|IFF_POINTOPOINT)) nexthop = 0;
這意味著只要發送的 pointopint 裝置相同、且偽二層(如 IPGRE)資訊相同,所有 skb 將使用同一個 neighbour,不管目標位址是否相同。在 IPIP Tunnel 情形下,由於這種裝置沒有任何二層資訊,所有通過 IPIP Tunnel 的 skb 將共用一個單一 neighbour,即便使用不同的 tunnel 裝置發送。
但在 3.5 重構之後,悲劇了!直接看 4.4 核心:
static inline __be32 rt_nexthop(const struct rtable *rt, __be32 daddr) {
if (rt->rt_gateway) return rt->rt_gateway;
return daddr;
}
static int ip_finish_output2(struct net *net, struct sock *sk, struct sk_buff *skb) {
......
nexthop = (__force u32) rt_nexthop(rt, ip_hdr(skb)->daddr);
neigh = __ipv4_neigh_lookup_noref(dev, nexthop);
if (unlikely(!neigh)) neigh = __neigh_create(&arp_tbl, &nexthop, dev, false);
if (!IS_ERR(neigh)) {
int res = dst_neigh_output(dst, neigh, skb);
return res;
}
......
}
可見 dev->flags&(IFF_LOOPBACK|IFF_POINTOPOINT) 這個判斷消失了!這意味著核心變傻了,上面分析的那種現象在 3.5 之後一定會發生。
遭遇這問題後,在看 3.5 之前的實作前,我的想法是初始化一個全域的 dummy neighbour,簡單地用 dev_queue_xmit 直接輸出:
static const struct neigh_ops dummy_direct_ops = {
.family = AF_INET,
.output = neigh_direct_output,
.connected_output = neigh_direct_output,
};
struct neighbour dummy_neigh;
void dummy_neigh_init() {
memset(&dummy_neigh, 0, sizeof(dummy_neigh));
dummy_neigh.nud_state = NUD_NOARP;
dummy_neigh.ops = &dummy_direct_ops;
dummy_neigh.output = neigh_direct_output;
dummy_neigh.hh.hh_len = 0;
}
static inline int ip_finish_output2(struct sk_buff *skb) {
......
nexthop = (__force u32) rt_nexthop(rt, ip_hdr(skb)->daddr);
if (dev->type == ARPHRD_TUNNEL) {
neigh = &dummy_neigh;
} else {
neigh = __ipv4_neigh_lookup_noref(dev, nexthop);
}
if (unlikely(!neigh)) neigh = __neigh_create(&arp_tbl, &nexthop, dev, false);
......
}
後來看了 3.5 之前的實作,發現了 if (dev->flags&(IFF_LOOPBACK|IFF_POINTOPOINT)) nexthop = 0;,於是決定採用它,程式碼更少也更優雅,於是產生了下面的 patch:
diff --git a/net/ipv4/ip_output.c b/net/ipv4/ip_output.c --- a/net/ipv4/ip_output.c +++ b/net/ipv4/ip_output.c @@ -202,6 +202,8 @@ static int ip_finish_output2(struct net *net, struct sock *sk, struct sk_buff *s rcu_read_lock_bh(); nexthop = (__force u32) rt_nexthop(rt, ip_hdr(skb)->daddr); + if (dev->flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) + nexthop = 0; neigh = __ipv4_neigh_lookup_noref(dev, nexthop); if (unlikely(!neigh)) neigh = __neigh_create(&arp_tbl, &nexthop, dev, false);