Linux 平台上之 Multipath Routing 應用


撰稿/整理: Rex Tsai <chihchun@kalug.linux.org.tw>
HTML 版本: http://kalug.linux.org.tw/sections.php?op=viewarticle&artid=4 TEXT 版本: [Tips] Linux 平台上之 Multipath Routing 應用. $Date: 2001/10/22 09:25:41 $ 警告: ***************************************************************************** 您因使用或不能使用本文之教學而產生之任何軟體、硬體損害(包括但不限 於直接或間接個人損害、營業利潤之喪失、業務中斷、營業資訊之遺失、或 任何其他金錢損失),概不負任何損害賠償責任。 The author has taken care in the preparation of this paper, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein. Linux is a registered trademark of Linus Torvalds. Other product and company names mentioned herein may be the trademarks of their respective owners. ***************************************************************************** 撰稿/整理: Rex Tsai <chihchun@kalug.linux.org.tw> 此文件應有許多技術上得問題. 請熟悉的朋友來信指教. [Introduction] 許多中小企業或是 Soho 甚至家庭, 會在家中裝有一條以上寬頻/寬帶線路. 這篇 Tips 將介紹如何在 Linux 上, 好好的應用這些資源. 在你使用兩家不同的 ISP 的時候, destination address-based load balancing 將會是最好的解決方案. 你的封包可以即時透過不同的 ISP 線路 走出去. 當然這裡所提的是從內部到外部, 如果你的專線提供不同的網路服 務提供給網路上得人存取, 那麼接下來所說明的方式不適合您. 此篇文章只能提供內部網路對外的 load sharing. 以下文章以 GNU/Debian, Linux kernel 2.4.12, iproute-20001007-1 為 環境, 所有動作請在主機前面執行. 假設你有兩條以上的網際網路數據線路 (xDSL, ISDN, Cable, whatever..), 想充分使用這些線路, 概觀來看有三種方式 1. Multiplexing 2. Packet-wise load balancing 3. Destination address-based load balancing, 或稱 Equal-Cost MultiPath Routing (ECMP) 稍微解釋一下三種方式的不同 1. Multiplexing Multiplexing, 這個方式提供某些路由器提供 offer-load balancing 或叫 做 load sharing. 這個方式可以讓路由器將流量分給不同的外流 ports. 但是會造成每個 port 的傳送上約有 30 % 的 overhead. 此外, 每家廠商的 實作都是獨一的, 因次你會被鎖在特定的解決方案上. 類似的技術是 "bonding" 或是 "multi-link". 這裡所提得 Bonding 是一 個標準, 是由 Bandwidth on Demand Interoperability Group (一個大 概有四十個製造商的協會)所提. 已經提交給 American National Standards Institute TR41.4 group. 這個通常的在於兩條數據線路都是接在同一個 點(ISP)上的時候, 因此如果是兩條不同 ISP 所提供的線路, 那麼就沒辦法 達到這個目的. 當然, 如果你的 ISP 不提供此項技術服務, 那麼也是沒轍. 舉個例子, 像是 stick multiple ISDN channels. 將幾個慢速的線路合成為 快速的一條. 像是 ISDN H.221 規格即用到 inverse multiplexing. 但通常 用於視訊傳輸而不是電子資料. 2. Packet-wise load balancing: 這個在你可以得到所有的 ISP 協助的時候是可行的, 如果兩家 ISP 都願意 協助將其不同的路由器皆設定到同一個 IP 位址. 那麼便可以這麼作. 在這個方式中, 你會用到像是 sch_teql (the TEQL scheduler) 來創造一個 virtual device 將你的封包分散在不同的網路介面上. 一般來講, 如果你是使用兩家不同 ISP , 那麼你不可以使用這個解決方案. 但, 你 "可以" 將所有的網路封包透過 IPIP or CIPE (Crypto IP Encapsu- lation) 的方式來解決這個問題. 看看 Linux Kernel 中的 IP: tunneling. 3. Equal-Cost MultiPath: 在 Linux 核心中叫做 equal cost multipath (CONFIG_IP_ROUTE_MULTIPATH) 比較正確的說法應該是 "destination address-based load balancing". 一 般 Linux 想要為某個 IP 位址找到路由, 會因為效能的問題去查驗暫存(cache) 中的資料, 如果目標 IP 並沒有在暫存空間中, 那麼他便會去查 routing table 來決定該 IP 位址的路由, 並將該路由放進 cache 中. 一般來講核心中的路由功能只能為某個封包決定唯一的方向. 如果使用 ECMP 並 有機會讓某個 package pattern 具有好幾種不同的方向, 可以讓某個符合路由條 件的封包透過 "equal" cost 或是自訂的權重來選擇該走的路由. [Howto] 如果你有數台電腦想使用外部網路, 一條 ADSL 不夠你使用(例如某人抓檔太兇) 那麼這是你正在找的解決方案. 你所能做的作好的方式就是每個 connection 可以 以 "non-deterministic fashion" 的方式選擇路由, 將 connection 分散到不同的 Router 上, 注意: 這裡所說的不是 packets, 這樣 TCP/IP session 將無法連續. 但是這整個路由程序有兩個部份四個問題要解決. 1a. How to get your packets to the outside world, 1b. How the outside world replies to you, 2a. How the outside world sends packets to you, 2b. How you reply to the outside world. Multipath 可以解決 1a 出去的問題. 一般的路由設定可以解決 1b 與 2a. 而 2b 則必須使用 policy routing (multiple tables)才能解決, Multiple Tables 可以 讓你加入以封包來源位址為依據來決定路由. 以下的範例, 在核心中, 務必加入編譯以下選項 必備: CONFIG_NETLINK=y 這個選項是 Kernel/User netlink socket CONFIG_RTNETLINK=y Routing messages CONFIG_INET=y TCP/IP networking CONFIG_IP_ADVANCED_ROUTER=y IP: advanced router CONFIG_IP_MULTIPLE_TABLES=y IP: policy routing CONFIG_IP_ROUTE_MULTIPATH=y IP: equal cost multipath 選用: CONFIG_IP_ROUTE_LARGE_TABLES=y 一般來講 IP: large routing tables 也會勾選, 一方面 routing zones 可以大於 64 筆, 這些資料存在 hash 資料結構中, 也可以 加速 "the routing process". 而 iproute 套件也是必須的軟體. 這個軟體的安裝方式與位置請洽詢提供 您所使用套件之廠商/組織. (Red Hat, Debian, Mandrake, SuSE, etc...) 重頭戲來了, 這裡假設你有三塊網路卡, 分別給內部網路與兩家 ISP. eth0 是內部網路, eth1 與 eth2 是其他兩家 ISP 線路. eth0 是內部網路, 範圍是 10.0.0.0/255.255.255.0 eth1 其中一家 ISP, IP 是 1.1.1.1, 閘道器(gateway)是 1.1.1.253 eth2 另外一家 ISP, IP 是 2.2.2.2, 閘道器(gateway)是 2.2.2.253 # 列出所有的 rule ip rule list # table 後的 "10" 是 table identifer, 為數字. # 註: 可用英文代稱取代請看 /etc/iproute2/rt_tables # # table 10 是給在 gateway 後面的內部網路使用, 10.0.0.x 是 LAN 使用的 IP. # # pref 後面指定的 "10" 是 priority. 為 policy routing database 搜尋的次序 ip rule add pref 10 to 10.0.0.0/24 table 10 ip route add 10.0.0.0/24 table 10 dev eth1 # table 20 給 ISP #1, IP 1.1.1.1, gateway 1.1.1.253 # pref 後面指定的 "20" 是 priority. 為 policy routing database 搜尋的次序 ip rule add pref 20 from 1.1.1.1 table 20 ip route add default table 20 via 1.1.1.253 # table 30 is for ISP #2, IP 2.2.2.2, gateway 2.2.2.253 ip rule add pref 20 from 2.2.2.2 table 30 ip route add default table 30 via 2.2.2.253 # 列出所有的 rule ip rule list # 列出 table 10 的 rule ip route list table 10 # 列出 table 20 的 rule ip route list table 20 # If your ISP's have servers that authenticate by originating IP address, # (e.g. SMTP or NNTP servers) you will want to explicitly list them here. # 這裡是靜態的 routing table 設定. 如果你的 ISP 有提供某些網路服務, 必須該 # ISP 的 IP 才能使用, 那麼你會想將它設定在這裡 # (e.g. Proxy, SMTP or NNTP Server) ip route add 1.1.1.0/24 dev eth1 ip route add 2.2.2.0/24 dev eth2 # 如果上面所有的 routing table 都沒有吻合, 那麼封包會走 default route # 這裡使用 "ECMP" 來選擇上游路由器. # "ip route repleace" 是用來取代原本的 default routi. ip route replace default nexthop via 1.1.1.253 dev eth1 \ nexthop via 2.2.253 dev eth2 # 如果你想加上權重, 是這樣使用的. 請依據你的線路網路頻寬 # 頻寬越大, 請把 weight 加大. #ip route replace default nexthop via 1.1.1.253 dev eth1 weight 1 \ # nexthop via 2.2.253 dev eth2 weight 3 # Make it all happen. IMPORTANT! The above commands do NOT # flush the route cache! ip route flush cache <-- 結束 --> Linux 上得 ECMP implementation 的實作有一個特色, 當你的上游網路介面 使用 ARP (e.g. 使用傳統 IP-over-Ethernet)時, 如果其中一個網路介面陣 亡時, Linux kernel 會自動把該介面 "shutdown", 並停止那些需要經過該 介面的 "nexthops". 但是要附註一提的是, 如果你使用的是 "網路型" ADSL 服務, 你會取得一個 ADSL 路由器, 你可能會架設一個 NAT 伺服器與 ADSL 路由器放在同一個 Ethernet Hub 上, 如果這個時候 ADSL 斷線, 但是 Hub 並未斷線, Linux 會認為該網路卡仍在 on-line 狀態. 除非是該 hub 或是網 路卡故障, kernel 才會 shutdown 該網路介面. 因此如果你需要確實的 "failover", 請動手寫一個小 script, 定時用 ping 或稱 "icmp echo request" 來詢問 ISP 端的閘道器是否正常的運作, 並用 "ip route replace" 來置換那些確定可通的路由. 這裡就請您自行處理了. Enjoy it, Rex. [Reference] [1] Linux-Net mailing list. http://www.uwsg.iu.edu/hypermail/linux/net/ 原文 http://www.uwsg.iu.edu/hypermail/linux/net/0107.3/0028.html [2] Alexey N. Kuznetsov, April 14, 1999, IP Command Reference [Futrher reading] [1] Jack Coates , Load-Balancing on LRP HOWTO [2] Netherlabs BV (bert hubert ), Gregory Maxwell , Remco van Mook , Martijn van Oosterhout , Paul B Schroeder , Jasper Spaans , howto@ds9a.nl, Linux 2.4 Advanced Routing Howto [3] Horacio J. Pe?a, horape@compendium.com.ar, 05/Apr/2000 Policy based routing MICRO-HOWTO, http://www.compendium.com.ar/policy-routing.txt [Keywords] Linux, Routeing, 路由, ADSL, 寬頻, 寬帶, 頻寬, 合併, 分流, 共用 [Acknowledgements] Zygo Blaxell (zblaxell@furryterror.org) 在 Linux-net 的說明 Digital Sesame, Inc - http://www.d11e.com 提供環境測試. -- Best Regards. Rex Tsai <chihchun_at_kalug.linux.org.tw> --