天天看點

Linux bonding研究及實作

Linux Bonding

本文介紹Linux(具體發行版本是CentOS5.6)下的雙卡綁定的原理及實作。

Linux雙網卡綁定實作是使用兩塊網卡虛拟成為一塊網卡裝置,這簡單來說,就是兩塊網卡具有相同的IP位址而并行連結聚合成一個邏輯鍊路工作。這項技術在Sun和Cisco中分别被稱為Trunking和Etherchannel技術,在Linux的2.4.x及其以後的核心則稱為Bonding技術。

bonding的前提條件:網卡應該具備自己獨立的BIOS晶片,并且各網卡晶片組型号相同。同一台伺服器上的網卡毫無疑問是是符合這個條件的。

Bonding原理

bonding的原理:在正常情況下,網卡隻接收目的硬體位址(MAC Address)是自身MAC的以太網幀,過濾别的資料幀,以減輕驅動程式的負擔;但是網卡也支援另外一種被稱為混雜promisc的模式,可以接收網絡上所有的幀,bonding就運作在這種模式下,而且修改了驅動程式中的mac位址,将兩塊網卡的MAC位址改成相同,可以接收特定MAC的資料幀。然後把相應的資料幀傳送給bond驅動程式處理。

為友善了解bonding的配置及實作,順便闡述一下Linux的網絡接口及其配置檔案。在 Linux 中,所有的網絡通訊都發生在軟體接口與實體網絡裝置之間。與網絡接口配置相關的檔案,以及控制網絡接口狀态的腳本檔案,全都位于 /etc/sysconfig/netwrok-scripts/ 目錄下。網絡接口配置檔案用于控制系統中的軟體網絡接口,并通過這些接口實作對網絡裝置的控制。當系統啟動時,系統通過這些接口配置檔案決定啟動哪些接口,以及如何對這些接口進行配置。接口配置檔案的名稱通常類似于 ifcfg-<name>,其中 <name> 與配置檔案所控制的裝置的名稱相關。 在所有的網絡接口中,最常用的就是以太網接口ifcfg-eth0,它是系統中第一塊網卡的配置檔案。雖然在不同的系統之間,檔案的類型和數量并不一定相同,但各種的網絡裝置都需要用到這些檔案保證裝置的正常運作。bonding技術,就是通過配置檔案建立綁定接口,進而實作多個實體網卡綁定到一個虛拟網卡。

測試環境

本次作業環境中,使用的是CentOS5.6,其系統資訊如下:

[root@support ~]# cat /etc/issue

CentOS release 5.6 (Final)

Kernel \r on an \m

[root@support ~]# uname -a

Linux support 2.6.18-238.9.1.el5 #1 SMP Tue Apr 12 18:10:13 EDT 2011 x86_64 x86_64 x86_64 GNU/Linux

可以通過modinfo bonding指令檢視Linux是否支援bonding,據資料介紹,RHEL4已預設支援了,RHEL5、CentOS5毫無疑問也是支援的。系統有類似下面的資訊輸出,就說明已支援了。

[root@support ~]# modinfo bonding

filename:       /lib/modules/2.6.18-238.9.1.el5/kernel/drivers/net/bonding/bonding.ko

description:    Ethernet Channel Bonding Driver, v3.4.0-1

version:        3.4.0-1

license:        GPL

srcversion:     358EAAF5610876F44387AEF

depends:        ipv6

vermagic:       2.6.18-238.9.1.el5 SMP mod_unload gcc-4.1

parm:           max_bonds:Max number of bonded devices (int)

…… ……

parm:           debug:Print debug messages; 0 for off (default), 1 for on (int)

module_sig:     883f3504da4d65c2ddc58dbbe9356811249c0a0993d9c1c5b9055a7e4f2b4d86e86ebb2c5fbdbf09e2c41b93ca13b6b809914bc6a201c3cf694855143

Bonding配置

通過在 /etc/sysconfig/network-scripts/ 目錄下建立 ifcfg-bond<N> 配置檔案,就能夠建立綁定接口。檔案名稱中的 <N> 為接口的編号,如第一個通道綁定接口的名稱一般為 ifcfg-bond0。ifcfg-bond<N> 檔案的内容與以太網接口的配置檔案(如 ifcfg-eth0)基本相同,隻是 DEVICE 選項的值,應該為 bond<N>。

Bonding接口建立以後,被綁定的網卡必須在他們的設定檔案裡面添加MASTER和SLAVE兩個參數。每個bonding接口的設定基本相同,具體配置如下:

1、建立/etc/sysconfig/network-scripts/ifcfg-bond0檔案,輸入以下配置資訊:

[root@support ~]# cat /etc/sysconfig/network-scripts/ifcfg-bond0

DEVICE=bond0

BOOTPROTO=static

IPADDR=10.8.0.231

NETMASK=255.255.0.0

NETWORK=10.8.0.0

BROADCAST=10.8.255.255

ONBOOT=yes

USERCTL=NO

BONDING_OPTS="mode=0 miimon=100"

說明:這裡使用了BONDING_OPTS選項,則不需要再使用 /etc/modprobe.conf 配置檔案對綁定裝置進行配置。參數mode=0,指負載均衡模式,詳見下文。miimon是用來進行鍊路監測的,其原理是檢測網上的鍊路狀态,一般将miimon值設為100,表示系統每100ms監測一次鍊路連接配接狀态,如果有一條線路不通就轉入另一條線路。

2、修改 /etc/sysconfig/network-scripts/ifcfg-eth0網卡屬性

[root@support ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE=eth0

BOOTPROTO=none

HWADDR=00:30:48:56:DA:72

MASTER=bond0

SLAVE=yes

3、修改 /etc/sysconfig/network-scripts/ifcfg-eth1網卡屬性

[root@support ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth1

DEVICE=eth1

HWADDR=00:30:48:56:DA:73

HOTPLUG=no

DHCP_HOSTNAME=support

說明:修改單個網卡配置,主要是去掉IP 位址、子網路遮罩等資訊。同時添加MASTER及SLAVE兩項參數。

MASTER=<bond-interface>:<bond-interface> 的值是以太網卡連接配接到的通道綁定接口的名稱,這個指令與 SLAVE 指令配合使用。 

SLAVE=<yes|no>:yes - 表示此裝置可以由 MASTER 指令中配置的通道綁定接口進行控制。 no - 表示此裝置不能由 MASTER 指令中配置的通道綁定接口進行控制。

4、編輯 /etc/modules.conf 檔案,添加:alias bond0 bonding,以使系統在啟動時加載bonding子產品,對外虛拟網絡接口裝置為 bond0

[root@support ~]# cat /etc/modprobe.conf

alias bond0 bonding

alias eth0 e1000

alias eth1 e1000

alias scsi_hostadapter ata_piix

alias scsi_hostadapter1 usb-storage

注:在linux6下測試時,發現沒有modprobe.conf這個檔案,需要進入modprobe.d檔案夾建立bond0.conf,再将“alias bond0 bonding”添加到裡面

[root@ara ~]# cat /etc/modprobe.d/bond0.conf 

5、重新開機網絡服務,以使用bond生效

[root@support ~]# service network restart

在網絡設定可以看到bond0與eth0、eth1之間的主從關系

[root@support ~]# ifconfig

bond0     Link encap:Ethernet  HWaddr 00:30:48:56:DA:72  

          inet addr:10.8.0.231  Bcast:10.8.255.255  Mask:255.255.0.0

          inet6 addr: fe80::230:48ff:fe56:da72/64 Scope:Link

          UP BROADCAST RUNNING MASTER MULTICAST  MTU:1500  Metric:1

          RX packets:19015 errors:0 dropped:0 overruns:0 frame:0

          TX packets:4714 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:0 

          RX bytes:2557139 (2.4 MiB)  TX bytes:469585 (458.5 KiB)

eth0      Link encap:Ethernet  HWaddr 00:30:48:56:DA:72  

          UP BROADCAST RUNNING SLAVE MULTICAST  MTU:1500  Metric:1

          RX packets:10592 errors:0 dropped:0 overruns:0 frame:0

          TX packets:2011 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:100 

          RX bytes:1295667 (1.2 MiB)  TX bytes:209014 (204.1 KiB)

eth1      Link encap:Ethernet  HWaddr 00:30:48:56:DA:72  

          RX packets:8423 errors:0 dropped:0 overruns:0 frame:0

          TX packets:2705 errors:0 dropped:0 overruns:0 carrier:0

          RX bytes:1261472 (1.2 MiB)  TX bytes:260887 (254.7 KiB)

lo        Link encap:Local Loopback  

          inet addr:127.0.0.1  Mask:255.0.0.0

          inet6 addr: ::1/128 Scope:Host

          UP LOOPBACK RUNNING  MTU:16436  Metric:1

          RX packets:1178 errors:0 dropped:0 overruns:0 frame:0

          TX packets:1178 errors:0 dropped:0 overruns:0 carrier:0

          RX bytes:2054820 (1.9 MiB)  TX bytes:2054820 (1.9 MiB)

至此,完成了bonding配置,系統已經在網卡的負載均衡模式下運作,随意拔掉一個網卡口的網線,網線依然正常。

Bonding工作模式

bonding的模式一共有7種,常用的為0、1兩種:

round-robin(balance-rr)  0  網卡的負載均衡模式

active-backup  1 網卡的容錯模式

balance-xor  2 需要交換機支援

broadcast  3 廣播模式

ieee802.3ad  4 動态鍊路聚合模式,需要交換機支援

mode-tlb  5 自适應模式

mode-alb  6 網卡虛拟化方式

mode=0表示load balancing(round-robin)為負載均衡方式,兩塊網卡都工作。在這種模式下,能在提供帶寬的負載均衡的同時提供失效保護。

mode=1表示fault-tolerance(active-backup)提供備援功能,工作方式是主備的工作方式,也就是說預設情況下隻有一塊網卡工作,另一塊做備份。bonding定義了網卡的4個鍊路狀态:正常狀态(BOND_LINK_UP)、網卡出現故障(BOND_LINK_FAIL)、失效狀态(BOND_LINK_DOWN)及網上恢複狀态(BOND_LINK_BACK)。mii的作用就是依次檢查網卡鍊路狀态是否處于這些狀态,然後通過标記某個變量來說明目前是否需要切換slave網卡。在這種模式下,兩塊網卡有一塊是不工作的,同時,bond虛裝置的MAC位址均一緻,是以這張備用網卡不會被外界察覺,交換機也不存在向該端口發包的情況。當bond的mii檢測到目前的active裝置失效了以後,bonding會迅速将另外一塊網卡設定為首選slave裝置。

在以上模式中,虛拟網卡的MAC位址始終是第一個slave網卡的MAC。由于外界學習到的伺服器MAC位址始終是不變的,在網絡上确定了IP和MAC的唯一對應關系,保證了上層業務傳輸的邏輯一緻性,是以鍊路的狀态不會受很大的影響。

Bonding工作情況

1、工作在模式0時

通過檢視bond0的工作狀态查詢能詳細的掌握bonding的工作情況,正常狀态下的/proc/net/bonding/bond0檔案:

[root@support ~]# cat /proc/net/bonding/bond0 

Ethernet Channel Bonding Driver: v3.4.0-1 (October 7, 2008)

Bonding Mode: load balancing (round-robin)

MII Status: up

MII Polling Interval (ms): 100

Up Delay (ms): 0

Down Delay (ms): 0

Slave Interface: eth0

Speed: 100 Mbps

Duplex: full

Link Failure Count: 1

Permanent HW addr: 00:30:48:56:da:72

Slave Interface: eth1

Link Failure Count: 2

Permanent HW addr: 00:30:48:56:da:73

從win7 ping,通過拔插網線檢視結果,不存在丢包

通過日志檢視網卡工作情況

[root@support ~]# tail -f /var/log/messages

May 29 08:01:41 support kernel: e1000: eth0 NIC Link is Up 100 Mbps Full Duplex, Flow Control: RX/TX

May 29 08:01:41 support kernel: bonding: bond0: link status definitely up for interface eth0.

May 29 08:01:48 support kernel: e1000: eth1 NIC Link is Down

May 29 08:01:48 support kernel: bonding: bond0: link status definitely down for interface eth1, disabling it

May 29 08:02:18 support kernel: e1000: eth1 NIC Link is Up 100 Mbps Full Duplex, Flow Control: RX/TX

May 29 08:02:18 support kernel: bonding: bond0: link status definitely up for interface eth1.

May 29 08:08:49 support kernel: e1000: eth0 NIC Link is Down

May 29 08:08:49 support kernel: bonding: bond0: link status definitely down for interface eth0, disabling it

May 29 08:08:56 support kernel: e1000: eth0 NIC Link is Up 100 Mbps Full Duplex, Flow Control: RX/TX

May 29 08:08:56 support kernel: bonding: bond0: link status definitely up for interface eth0.

2、工作在模式1時

正常狀态下的/proc/net/bonding/bond0檔案:

Bonding Mode: fault-tolerance (active-backup)

Primary Slave: None

Currently Active Slave: eth0

Link Failure Count: 0

從win7 ping,通過拔插網線檢視結果,發現在拔網線時,偶爾會存在丢掉一個包的情況。

May 29 08:25:24 support kernel: e1000: eth0 NIC Link is Down

May 29 08:25:24 support kernel: bonding: bond0: link status definitely down for interface eth0, disabling it

May 29 08:25:24 support kernel: bonding: bond0: making interface eth1 the new active one.

May 29 08:25:32 support kernel: e1000: eth0 NIC Link is Up 100 Mbps Full Duplex, Flow Control: RX/TX

May 29 08:25:32 support kernel: bonding: bond0: link status definitely up for interface eth0.

May 29 08:27:02 support kernel: e1000: eth1 NIC Link is Down

May 29 08:27:02 support kernel: bonding: bond0: link status definitely down for interface eth1, disabling it

May 29 08:27:02 support kernel: bonding: bond0: making interface eth0 the new active one.

May 29 08:27:48 support kernel: e1000: eth1 NIC Link is Up 100 Mbps Full Duplex, Flow Control: RX/TX

May 29 08:27:48 support kernel: bonding: bond0: link status definitely up for interface eth1.

小結

在測試過程中,發現丢包情況與裝置有關,老式的網新易得伺服器上做測試時,兩個網卡無論是接到同一個二層交換機,還是分别接到兩個二層交換機,在拔插網線,偶爾會出現丢掉一個包的情況。但是在戴爾R710上,當使用模式1時,丢包比較多,甚至在被其他計算機ping時,會出現ping不同的情況。

另外,模式0隻是實作了簡單的負載均衡及備援備份。如果要實作高效的負載均衡,提高網絡流量,需設定成模式4,而這需要交換機的支援,牽涉到交換機的鍊路聚合(LACP)功能。有待日後再研究。

本文轉自Sunshyfangtian 51CTO部落格,原文連結:http://blog.51cto.com/sunshyfangtian/577762,如需轉載請自行聯系原作者

繼續閱讀