歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> 深度分析Linux下雙網卡綁定七種模式

深度分析Linux下雙網卡綁定七種模式

日期:2017/3/1 15:43:09   编辑:關於Linux
深度分析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。
Copyright © Linux教程網 All Rights Reserved