Linux下雙網卡綁定七種模式

來源:互聯網
上載者:User

最近測試伺服器,接觸到的網卡比較多,一台伺服器上有2-7個網口,配置IP和網路參數時,如果配置不對,會有些問題,特別是涉及到環路方面。這篇文章對網卡的常用模式作了一個介紹,感覺可以宏觀理解多網卡的工作方式,解釋為什麼要設計成多網卡,但是沒有涉及如何進行有效配置的方法。

今天分享的是linux作業系統下雙網卡綁定有哪七種模式,分別是如何工作的。現在一般的企業都會使用雙網卡接入,這樣既能添加網路頻寬,同時又能做相應的冗餘,可以說是好處多多。而一般企業都會使用linux作業系統下內建的網卡繫結模式,當然現在網卡產商也會出一些針對windows作業系統網卡管理軟體來做網卡綁定(windows作業系統沒有網卡綁定功能 需要第三方支援)。進入正題,linux有七種網卡繫結模式:0. round robin,1.active-backup,2.load balancing (xor),
 3.fault-tolerance (broadcast), 4.lacp,  5.transmit load balancing, 6.adaptive load balancing。

第一種:bond0:round robin
標準:round-robin policy: Transmit packets in sequential order from the first available slave through the last. This mode provides load balancing and fault tolerance.

特點:(1)所有鏈路處於負載平衡狀態,輪詢方式往每條鏈路發送報文,基於per packet方式發送。服務上ping 一個相同地址:1.1.1.1 雙網卡的兩個網卡都有流量發出。負載到兩條鏈路上,說明是基於per packet方式 ,進行輪詢發送。(2)這模式的特點增加了頻寬,同時支援容錯能力,當有鏈路出問題,會把流量切換到正常的鏈路上。

實際綁定結果:
cat /proc/net/bonding/bond0
Ethernet Channel Bonding Driver: v3.6.0 (September 26, 2009)
Bonding Mode: load balancing (round-robin)  -----RR的模式
MII Status: up
MII Polling Interval (ms): 100
Up Delay (ms): 0
Down Delay (ms): 0
Slave Interface: eth0
MII Status: up
Link Failure Count: 0
Permanent HW addr: 74:ea:3a:6a:54:e3
Slave Interface: eth1
MII Status: up
Link Failure Count: 0

應用拓撲:交換器端需要配置彙總口,cisco叫port channel。拓撲圖如下:

第二種:bond1:active-backup
標準文檔定義:Active-backup policy: Only one slave in the bond is active. A different slave becomes active if, and only if, the active slave fails. The bond’s MAC address is externally visible on only one port (network adapter) to avoid confusing the switch. This
mode provides fault tolerance. The primary option affects the behavior of this mode.

模式的特點:一個連接埠處於主狀態 ,一個處於從狀態,所有流量都在主鏈路上處理,從不會有任何流量。當主連接埠down掉時,從連接埠接手主狀態。

實際綁定結果:
root@1:~# cat /proc/net/bonding/bond0
Ethernet Channel Bonding Driver: v3.6.0 (September 26, 2009)
Bonding Mode: fault-tolerance (active-backup) -----backup模式
Primary Slave: None
Currently Active Slave: eth0
MII Status: up
MII Polling Interval (ms): 100
Up Delay (ms): 0
Down Delay (ms): 0
Slave Interface: eth0
MII Status: up
Link Failure Count: 0
Permanent HW addr: 74:ea:3a:6a:54:e3
Slave Interface: eth1
MII Status: up
Link Failure Count: 0
Permanent HW addr: d8:5d:4c:71:f9:94

應用拓撲:這種模式接入不需要交換器端支援,隨便怎麼接入都行。

第三種:bond2:load balancing (xor)
標準文檔描述:XOR policy: Transmit based on [(source MAC address XOR'd with destination MAC address) modulo slave count]. This selects the same slave for each destination MAC address. This mode provides load balancing and fault tolerance.

特點:該模式將限定流量,以保證到達特定對端的流量總是從同一個介面上發出。既然目的地是通過MAC地址來決定的,因此該模式在“本地”網路設定下可以工作得很好。如果所有流量是通過單個路由器(比如 “網關”型網路設定,只有一個網關時,源和目標mac都固定了,那麼這個演算法算出的線路就一直是同一條,那麼這種模式就沒有多少意義了。),那該模式就不是最好的選擇。和balance-rr一樣,交換器連接埠需要能配置為“port channel”。這模式是通過源和目標mac做hash因子來做xor演算法來選路的。

實際綁定結果:
[root@localhost ~]# cat /proc/net/bonding/bond0
Ethernet Channel Bonding Driver: v3.0.3 (March 23, 2006)
Bonding Mode: load balancing (xor) ------配置為xor模式
Transmit Hash Policy: layer2 (0)
MII Status: up
MII Polling Interval (ms): 100
Up Delay (ms): 0
Down Delay (ms): 0
Slave Interface: eth1
MII Status: up
Link Failure Count: 0
Permanent HW addr: 00:d0:f8:40:f1:a0
Slave Interface: eth2
MII Status: up
Link Failure Count: 0
Permanent HW addr: 00:d0:f8:00:0c:0c

應用拓撲:同bond0一樣的應用程式模型。這個模式也需要交換器配置彙總口。

第四種:bond3:fault-tolerance (broadcast)
標準文檔定義:Broadcast policy: transmits everything on all slave interfaces. This mode provides fault tolerance.

特點:這種模式的特點是一個報文會複製兩份往bond下的兩個介面分別發送出去,當有對端交換器失效,我們感覺不到任何downtime,但此法過於浪費資源;不過這種模式有很好的容錯機制。此模式適用於金融行業,因為他們需要高可靠性的網路,不允許出現任何問題。

實際綁定結果:
root@ubuntu12:~/ram# cat /proc/net/bonding/bond0
Ethernet Channel Bonding Driver: v3.6.0 (September 26, 2009)
Bonding Mode: fault-tolerance (broadcast) ------- fault-tolerance 模式
MII Status: up
MII Polling Interval (ms): 100
Up Delay (ms): 0
Down Delay (ms): 0
Slave Interface: eth0
MII Status: up
Link Failure Count: 0
Permanent HW addr: 74:ea:3a:6a:54:e3
Slave Interface: eth1
MII Status: up
Link Failure Count: 0
Permanent HW addr: d8:5d:4c:71:f9:94

應用拓撲:如下:

這種模式適用於如下拓撲,兩個介面分別接入兩台交換器,並且屬於不同的vlan,當一邊的網路出現故障不會影響伺服器另一邊接入的網路正常工作。而且故障過程是0丟包。下面展示了這種模式下ping資訊:
64 bytes from 1.1.1.1: icmp_seq=901 ttl=64 time=0.205 ms
64 bytes from 1.1.1.1: icmp_seq=901 ttl=64 time=0.213 ms (DUP!) ---dup為重複報文
64 bytes from 1.1.1.1: icmp_seq=902 ttl=64 time=0.245 ms
64 bytes from 1.1.1.1: icmp_seq=902 ttl=64 time=0.254 ms (DUP!)
64 bytes from 1.1.1.1: icmp_seq=903 ttl=64 time=0.216 ms
64 bytes from 1.1.1.1: icmp_seq=903 ttl=64 time=0.226 ms (DUP!)
從這個ping資訊可以看到,這種模式的特點是,同一個報文伺服器會複製兩份分別往兩條線路發送,導致回複兩份重複報文,這種模式有浪費資源的嫌疑。

第五種:bond4:lacp

標準文檔定義:IEEE 802.3ad Dynamic link aggregation. Creates aggregation groups that share the same speed and duplex settings. Utilizes all slaves in the active aggregator according to the 802.3ad specification. Pre-requisites: 1. Ethtool support in the base drivers
for retrieving.the speed and duplex of each slave. 2. A switch that supports IEEE 802.3ad Dynamic link
aggregation. Most switches will require some type of configuration to enable 802.3ad mode.

特點:802.3ad模式是IEEE標準,因此所有實現了802.3ad的對端都可以很好的互操作。802.3ad 協議包括彙總的自動設定,因此只需要很少的對交換器的手動設定(要指出的是,只有某些裝置才能使用802.3ad)。802.3ad標準也要求幀按順序(一定程度上)傳遞,因此通常單個串連不會看到包的亂序。802.3ad也有些缺點:標準要求所有裝置在彙總操作時,要在同樣的速率和雙工模式,而且,和除了balance-rr模式外的其它bonding負載平衡模式一樣,任何串連都不能使用多於一個介面的頻寬。
此外,linux bonding的802.3ad實現通過對端來分發流量(通過MAC地址的XOR值),因此在“網關”型配置下,所有外出(Outgoing)流量將使用同一個裝置。進入(Incoming)的流量也可能在同一個裝置上終止,這依賴於對端802.3ad實現裡的均衡策略。在“本地”型配置下,路兩將通過 bond裡的裝置進行分發。

實際綁定結果:
root@:~# cat /proc/net/bonding/bond0
Ethernet Channel Bonding Driver: v3.6.0 (September 26, 2009)
Bonding Mode: IEEE 802.3ad Dynamic link aggregation
Transmit Hash Policy: layer2 (0)
MII Status: up
MII Polling Interval (ms): 100
Up Delay (ms): 0
Down Delay (ms): 0
802.3ad info
LACP rate: slow
Aggregator selection policy (ad_select): stable
Active Aggregator Info:
Aggregator ID: 1
Number of ports: 1
Actor Key: 9
Partner Key: 1
Partner Mac Address: 00:00:00:00:00:00
Slave Interface: eth0
MII Status: up
Link Failure Count: 0
Permanent HW addr: 74:ea:3a:6a:54:e3
Aggregator ID: 1
Slave Interface: eth1
MII Status: up
Link Failure Count: 0
Permanent HW addr: d8:5d:4c:71:f9:94
Aggregator ID: 2

應用拓撲:應用拓撲同bond0,和bond2一樣,不過這種模式除了配置port channel之外還要在port channel彙總口下開啟LACP功能,成功協商後,兩端可以正常通訊。否則不能使用。

交換器端配置:
interface AggregatePort 1 配置彙總口
interface GigabitEthernet 0/23
port-group 1 mode active 介面下開啟lacp 主動模式
interface GigabitEthernet 0/24
port-group 1 mode active

第六種:bond5: transmit load balancing

標準文檔定義:Adaptive transmit load balancing: channel bonding that does not require any special switch support. The outgoing traffic is distributed according to the current load (computed relative to the speed) on each slave. Incoming traffic is received by the
current slave. If the receiving slave fails, another slave takes over the MAC address of the failed receiving slave. Prerequisite: Ethtool support in the base drivers for retrieving the speed of each slave.

特點:balance-tlb模式通過對端均衡外出(outgoing)流量。既然它是根據MAC地址進行均衡,在“網關”型配置(如上文所述)下,該模式會通過單個裝置來發送所有流量,然而,在“本地”型網路設定下,該模式以相對智能的方式(不是balance-xor或802.3ad模式裡提及的XOR方式)來均衡多個本網對端,因此那些數字不幸的MAC地址(比如XOR得到同樣值)不會聚集到同一個介面上。
不像802.3ad,該模式的介面可以有不同的速率,而且不需要特別的交換器配置。不利的一面在於,該模式下所有進入的(incoming)流量會到達同一個介面;該模式要求slave介面的網路裝置驅動有某種ethtool支援;而且ARP監控不可用。

實際配置結果:
cat /proc/net/bonding/bond0
Ethernet Channel Bonding Driver: v3.0.3 (March 23, 2006)
Bonding Mode: transmit load balancing -----TLB模式
Primary Slave: None
Currently Active Slave: eth1
MII Status: up
MII Polling Interval (ms): 100
Up Delay (ms): 0
Down Delay (ms): 0
Slave Interface: eth1
MII Status: up
Link Failure Count: 0
Permanent HW addr: 00:d0:f8:40:f1:a0
Slave Interface: eth2
MII Status: up
Link Failure Count: 0
Permanent HW addr: 00:d0:f8:00:0c:0c

應用拓撲:這個模式下bond成員使用各自的mac,而不是上面幾種模式是使用bond0介面的mac。

如,裝置開始時會發送免費arp,以主連接埠eth1的mac為源,當用戶端收到這個arp時就會在arp緩衝中記錄下這個mac對的ip。而在這個模式下,伺服器每個連接埠在ping操作時,會根據演算法算出出口,地址不斷變化時他,這時會負載到不同連接埠。實驗中ping1.1.1.3時往eth2發送,源mac為00:D0:F8:00:0C:0C,ping1.1.1.4是往eth1發送,源mac為00:D0:F8:40:F1:A0,以此類推,所以從伺服器出去的流量負載到兩條線路,但是由於服務發arp時只用00:D0:F8:40:F1:A0,這樣用戶端緩衝記錄的是00:D0:F8:40:F1:A0對的ip,封裝時目標mac:00:D0:F8:40:F1:A0。這樣進入服務的流量都只往eth1(00:D0:F8:40:F1:A0)走。裝置會一直發入snap報文,eth1發送源為00d0.f840.f1a0的snap報文,eth2發送源為00d0.f800.0c0c的snap報文。這個snap報文mac和目標mac一樣都是網卡本地mac,源ip和目標ip也一樣,這個報文的作用是檢測線路是否正常的迴環報文。
註:可以通過修改bond0的mac地址來引導他發修改後的源mac的免費arp(MACADDR=00:D0:F8:00:0C:0C)

第七種:bond6:adaptive load balancing
特點:該模式包含了balance-tlb模式,同時加上針對IPV4流量的接收負載平衡(receive load balance, rlb),而且不需要任何switch(交換器)的支援。接收負載平衡是通過ARP協商實現的。bonding驅動截獲本機發送的ARP應答,並把源硬體地址改寫為bond中某個slave的唯一硬體地址,從而使得不同的對端使用不同的硬體地址進行通訊。所有連接埠都會收到對端的arp請求報文,回複arp回時,bond驅動模組會截獲所發的arp回複報文,根據演算法算到相應連接埠,這時會把arp回複報文的源mac,send源mac都改成相應連接埠mac。從抓包情況分析回複報文是第一個從連接埠1發,第二個從連接埠2發。以此類推。
(還有一個點:每個連接埠除發送本連接埠回複的報文,也同樣會發送其他連接埠回複的報文,mac還是其他連接埠的mac)這樣來自伺服器端的接收流量也會被均衡。
當本機發送ARP請求時,bonding驅動把對端的IP資訊從ARP包中複製並儲存下來。當ARP應答從對端到達時,bonding驅動把它的硬體地址提取出來,並發起一個ARP應答給bond中的某個slave(這個演算法和上面一樣,比如算到1口,就給發送arp請求,1回複時mac用1的mac)。使用ARP協商進行負載平衡的一個問題是:每次廣播 ARP請求時都會使用bond的硬體地址,因此對端學習到這個硬體地址後,接收流量將會全部流向當前的slave。這個問題通過給所有的對端發送更新(ARP應答)來解決,往所有連接埠發送應答,應答中包含他們獨一無二的硬體地址,從而導致流量重新分配。當新的slave加入到bond中時,或者某個未啟用的slave重新啟用時,接收流量也要重新分配。接收的負載被順序地分布(round
robin)在bond中最高速的slave上
當某個鏈路被重新接上,或者一個新的slave加入到bond中,接收流量在所有當前啟用的slave中全部重新分配,通過使用指定的MAC地址給每個 client發起ARP應答。下面介紹的updelay參數必須被設定為某個大於等於switch(交換器)轉寄延時的值,從而保證發往對端的ARP應答不會被switch(交換器)阻截。
必要條件:
條件1:ethtool支援擷取每個slave的速率;
條件2:底層驅動支援設定某個裝置的硬體地址,從而使得總是有個slave(curr_active_slave)使用bond的硬體地址,同時保證每個bond 中的slave都有一個唯一的硬體地址。如果curr_active_slave出故障,它的硬體地址將會被新選出來的 curr_active_slave接管。

實際配置結果:
root@:/tmp# cat /proc/net/bonding/bond0
Ethernet Channel Bonding Driver: v3.6.0 (September 26, 2009)
Bonding Mode: adaptive load balancing
Primary Slave: None
Currently Active Slave: eth0
MII Status: up
MII Polling Interval (ms): 100
Up Delay (ms): 0
Down Delay (ms): 0
Slave Interface: eth0
MII Status: up
Link Failure Count: 0
Permanent HW addr: 74:ea:3a:6a:54:e3
Slave Interface: eth1
MII Status: up
Link Failure Count: 0
Permanent HW addr: d8:5d:4c:71:f9:94

應用拓撲:

A是雙網卡綁定。
當B 發送一個arp請求到達A時,按正常情況A會回應一個arp回應報文,源mac為bond的mac,源就是bond的ip。但是這個模式下bonding驅動會截獲這個arp回應,把源mac改成bond狀態 下其中某一個網卡的mac:mac1,這樣B收到這個arp回應時就會在arp緩衝中記錄下ip:1.1.1.1對應的mac為mac1。這樣B的過來的流量都走MAC1.
當C 發送一個arp請求到達A時,按正常情況A會回應一個arp回應報文,源mac為bond的mac,源就是bond的ip。但是這個模式下bonding驅動會截獲這個arp回應,把源mac改成bond狀態 下其中某一個網卡的mac:mac2,這樣C收到這個arp回應時就會在arp緩衝中記錄下ip:1.1.1.1對應的mac為mac2。這樣C的過來的流量都走MAC2.
這樣就可以做到回來讓回來的流量也負載平衡。出方向均衡和MODE=5一致,不同地址會根據xor演算法算出不同出口,發不同出口發送相應的arp ,mac是對應網卡的mac。

轉載自:http://www.datacentersky.com/depth-analysis-of-linux-dual-nic-teaming-seven-models.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.