$Date: 2001/10/22 09:25:41 $
警告:
*****************************************************************************
您因使用或不能使用本文之教學而產生之任何軟件、硬件損害(包括但不限
于直接或間接個人損害、營業(yè)利潤之喪失、業(yè)務中斷、營業(yè)信息之遺失、或
任何其它金錢損失),概不負任何損害賠償責任。
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
此文件應有許多技術上得問題. 請熟悉的朋友來信指教.
[Introduction]
許多中小企業(yè)或是 Soho 甚至家庭, 會在家中裝有一條以上寬頻/寬帶線路.
這篇 Tips 將介紹如何在 Linux 上, 好好的應用這些資源.
在你使用兩家不同的 ISP 的時候, destination address-based load
balancing 將會是最好的解決方案. 你的封包可以實時透過不同的 ISP 線路
走出去. 當然這里所提的是從內部到外部, 假如你的專線提供不同的網(wǎng)絡服
務提供給網(wǎng)絡上得人存取, 那么接下來所說明的方式不適合您.
此篇文章只能提供內部網(wǎng)絡對外的 load sharing.
以下文章以 GNU/Debian, Linux kernel 2.4.12, iproute-20001007-1 為
環(huán)境, 所有動作請在主機前面執(zhí)行.
假設你有兩條以上的網(wǎng)際網(wǎng)絡數(shù)據(jù)線路 (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 (一個大
概有四十個制造商的協(xié)會)所提. 已經(jīng)提交給 American National Standards
Institute TR41.4 group. 這個通常的在于兩條數(shù)據(jù)線路都是接在同一個
點(ISP)上的時候, 因此假如是兩條不同 ISP 所提供的線路, 那么就沒辦法
達到這個目的. 當然, 假如你的 ISP 不提供此項技術服務, 那么也是沒轍.
舉個例子, 像是 stick multiple ISDN channels. 將幾個慢速的線路合成為
快速的一條. 像是 ISDN H.221 規(guī)格即用到 inverse multiplexing. 但通常
用于視訊傳輸而不是電子資料.
2. Packet-wise load balancing:
這個在你可以得到所有的 ISP 協(xié)助的時候是可行的,
假如兩家 ISP 都愿意
協(xié)助將其不同的路由器皆設定到同一個 IP 地址. 那么便可以這么作.
在這個方式中, 你會用到像是 sch_teql (the TEQL scheduler) 來創(chuàng)造一個
virtual device 將你的封包分散在不同的網(wǎng)絡接口上.
一般來講, 假如你是使用兩家不同 ISP , 那么你不可以使用這個解決方案.
但, 你 "可以" 將所有的網(wǎng)絡封包透過 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]
假如你有數(shù)臺計算機想使用外部網(wǎng)絡, 一條 ADSL 不夠你使用(例如某人抓檔太兇)
那么這是你正在找的解決方案. 你所能做的作好的方式就是每個 connection 可以
以 "non-deterministic fashion" 的方式選擇路由, 將 connection 分散到不同的
Router 上, 注重: 這里所說的不是 packets, 這樣 TCP/IP session 將無法連續(xù).
但是這整個路由程序有兩個部份四個問題要解決.
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 可以
讓你加入以封包來源地址為依據(jù)來決定路由.
以下的范例, 在核心中, 務必加入編譯以下選項
必備:
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 筆, 這些數(shù)據(jù)存在 hash 數(shù)據(jù)結構中, 也可以
加速 "the routing process".
而 iproute 套件也是必須的軟件. 這個軟件的安裝方式與位置請洽詢提供
您所使用套件之廠商/組織. (Red Hat, Debian, Mandrake, SuSE, etc...)
重頭戲來了, 這里假設你有三塊網(wǎng)絡卡, 分別給內部網(wǎng)絡與兩家 ISP. eth0
是內部網(wǎng)絡, eth1 與 eth2 是其它兩家 ISP 線路.
eth0 是內部網(wǎng)絡, 范圍是 10.0.0.0/255.255.255.0
eth1 其中一家 ISP, IP 是 1.1.1.1, 網(wǎng)關器(gateway)是 1.1.1.253
eth2 另外一家 ISP, IP 是 2.2.2.2, 網(wǎng)關器(gateway)是 2.2.2.253
# 列出所有的 rule
ip rule list
# table 后的 "10" 是 table identifer, 為數(shù)字.
# 注: 可用英文代稱取代請看 /etc/iproute2/rt_tables
#
# table 10 是給在 gateway 后面的內部網(wǎng)絡使用, 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.
# 這里是靜態(tài)的 routing table 設定. 假如你的 ISP 有提供某些網(wǎng)絡服務, 必須該
# 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
# 假如上面所有的 rout