Linux网卡bonding

一、binding技术

bonding(绑定)是一种linux系统下的网卡绑定技术,可以把服务器上n个物理网卡在系统内部抽象(绑定)成一个逻辑上的网卡,能够提升网络吞吐量、实现网络冗余、负载等功能,有很多优势。

二、常用模式详解

1、模式0 balance-rr


默认模式

  • 优点:有高可用性(容错)和负载均衡的功能,需要交换机配置,每块网卡轮询发包,流量分发比较均匀。
  • 缺点:一个连接或者会话的数据包从不同的接口发出的话,中途再经过不同的链路,在客户端很有可能会出现数据包无序到达的问题,而无序到达的数据包需要重新要求被发送,这样网络的吞吐量就会下降
交换机配置静态聚合口
Switch1# configure terminal 进入全局配置模式 
Switch1(config)# interface eth-0-1 进入接口配置模式 
Switch1(config-if)# no shutdown  端口 up 
Switch1(config-if)# static-channel-group 1 添加接口到 channel group 1 
Switch1(config-if)# exit  退出接口配置模式 
Switch1(config)# interface eth-0-2 进入接口配置模式 
Switch1(config-if)# static-channel-group 1 添加接口到 channel group 1 
Switch1(config-if)# no shutdown  端口 up 
Switch1(config-if)# exit  退出接口配置模式 
### Switch1(config-if)# no static-channel-group 从聚合口移除
### Switch1# show channel-group summary 验证静态聚合口
### Switch1# show interface agg 1 
配置过程,网上有很多例子。

查看bond状态的命令

  • ifconfig #ifconfig命令可以查看设备的bond信息,bond口和两个成员口MAC地址相同,成员口的地址失效,两块网卡共用bond0设备的一个IP地址
  • cat /proc/net/bonding/bond0
  • cat /sys/class/net/bond0/bonding/mode
#cat /proc/net/bonding/bond0接口状态
[root@nas network-scripts]# cat /proc/net/bonding/bond0
Ethernet Channel Bonding Driver: v3.7.1 (April 27, 2011)

Bonding Mode: load balancing (round-robin)
MII Status: up
MII Polling Interval (ms): 0
Up Delay (ms): 0
Down Delay (ms): 0

Slave Interface: p7p1
MII Status: up
Speed: 10000 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 00:b3:42:03:b3:f0
Slave queue ID: 0

Slave Interface: p7p2
MII Status: up
Speed: 10000 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 00:b3:42:03:b3:f1
Slave queue ID: 0
#cat ifcfg-bond0
[root@nas network-scripts]# cat ifcfg-bond0
#Modify#
DEVICE=bond0
TYPE=Bond
BONDING_MASTER=yes
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=no
NAME=bond0
ONBOOT=yes
BONDING_OPTS="miimon=100  mode=balance-rr
IPADDR="192.168.11.151"
PREFIX=24
SLAVER="p7p1,p7p2"
  • bond默认只能做网口mii检测不做arp链路检测,也可以使用ARP做链路检测,但是arp比较消耗资源
  • 负载均衡模式下,slave口在操作系统上看是两个独立的Mac地址

2、模式1 active-backup 主备

  • 特点:一个网口处于主状态,一个处于从状态,所有流量都在主链路上处理,从状态不会出现任何流量。当主端口down掉时,从端口接手主状态。此模式优点是可以提供高网络连接的可用性,但是资源利用率较低,只有一个接口处于工作状态。
  • 此时绑定的MAC地址只在一个外部端口上可见。交换机配置的是捆绑的话将不能工作,因为交换机往两块网卡发包,有一半包是丢弃的
此模式不需要交换机配置
#cat /proc/net/bonding/bond0接口状态。
[root@nas network-scripts]# cat /proc/net/bonding/bond0
Ethernet Channel Bonding Driver: v3.7.1 (April 27, 2011)

Bonding Mode: fault-tolerance (active-backup)
Primary Slave: None
Currently Active Slave: p7p2  #p7p2为主设备
MII Status: up
MII Polling Interval (ms): 100
Up Delay (ms): 0
Down Delay (ms): 0

Slave Interface: p7p2
MII Status: up
Speed: 10000 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 00:b3:42:03:b3:f1
Slave queue ID: 0

Slave Interface: p7p1
MII Status: up
Speed: 10000 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 00:b3:42:03:b3:f0
Slave queue ID: 0
#cat ifcfg-bond0
[root@nas network-scripts]# cat ifcfg-bond0
#Modify#
DEVICE=bond0
TYPE=Bond
BONDING_MASTER=yes
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=no
NAME=bond0
ONBOOT=yes
BONDING_OPTS="miimon=100  mode=active-backup
"
IPADDR="192.168.11.151"
PREFIX=24
SLAVER="p7p1,p7p2"

3、模式4 802.3ad lacp IEEE 802.3ad动态链路聚合

  • 特点:创建一个聚合组,它们共享同样的速率和双工设定。根据802.3ad规范将多个slave工作在同一个激活的聚合体下。
    外出流量的slave选举是基于传输hash策略,该策略可以通过xmit_hash_policy选项从缺省的XOR策略改变到其他策略。
交换机配置动态聚合
Switch# configure terminal 
Enter configuration commands, one per line.  End with CNTL/Z.
Switch(config)# interface range eth-0-3 - 4
Switch(config-if-range)# channel-group 1 mode active 
Switch(config-if-range)# exit
Switch(config)# exit
Switch#  show channel-group summary  #查看信息
port-channel load-balance hash-arithmetic: xor
port-channel load-balance hash-field-select:
        macda macsa ipsa ipda 

Flags:  s - suspend           T - standby
        D - down/admin down   B - in Bundle
        R - Layer3            S - Layer2
        w - wait              U - in use

Mode:   SLB  - static load balance
        DLB  - dynamic load balance
        SHLB - self-healing load balance
        RR   - round robin load balance

Aggregator Name  Mode      Protocol     Ports
----------------+---------+------------+-----------------------------------------------
agg1(SD)         SLB       LACP         eth-0-3(s)      eth-0-4(s)
cat /proc/net/bonding/bond0
[root@nas network-scripts]# cat /proc/net/bonding/bond0
Ethernet Channel Bonding Driver: v3.7.1 (April 27, 2011)

Bonding Mode: IEEE 802.3ad Dynamic link aggregation
Transmit Hash Policy: layer3+4 (1)  ##hash策略
MII Status: up
MII Polling Interval (ms): 100
Up Delay (ms): 0
Down Delay (ms): 0

802.3ad info
LACP rate: slow
Min links: 0
Aggregator selection policy (ad_select): stable
System priority: 65535
System MAC address: 00:b3:42:03:b3:f0
Active Aggregator Info:
    Aggregator ID: 1
    Number of ports: 2
    Actor Key: 13
    Partner Key: 1
    Partner Mac Address: 00:1e:08:0f:43:c3

Slave Interface: p7p1
MII Status: up
Speed: 10000 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 00:b3:42:03:b3:f0
Slave queue ID: 0
Aggregator ID: 1
Actor Churn State: none
Partner Churn State: none
Actor Churned Count: 0
Partner Churned Count: 0
details actor lacp pdu:
    system priority: 65535
    system mac address: 00:b3:42:03:b3:f0
    port key: 13
    port priority: 255
    port number: 1
    port state: 61
details partner lacp pdu:
    system priority: 2000
    system mac address: 00:1e:08:0f:43:c3
    oper key: 1
    port priority: 32768
    port number: 3
    port state: 61

Slave Interface: p7p2
MII Status: up
Speed: 10000 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 00:b3:42:03:b3:f1
Slave queue ID: 0
Aggregator ID: 1
Actor Churn State: none
Partner Churn State: none
Actor Churned Count: 0
Partner Churned Count: 0
details actor lacp pdu:
    system priority: 65535
    system mac address: 00:b3:42:03:b3:f0
    port key: 13
    port priority: 255
    port number: 2
    port state: 61
details partner lacp pdu:
    system priority: 2000
    system mac address: 00:1e:08:0f:43:c3
    oper key: 1
    port priority: 32768
    port number: 4
    port state: 61

此模式基于hash算法,随机选择网口,不同的hash策略选择的网口可能不相同。

hash算法(xmit_hash_policy)
数值 策略 算法
0 layer2 默认值, hash = source MAC XOR destination MAC XOR packet type ID hash mod slave count
1 layer3+4 hash = source MAC XOR destination MAC XOR packet type ID hash = hash XOR source IP XOR destination IP hash = hash XOR (hash RSHIFT 16) hash = hash XOR (hash RSHIFT 8) hash mod slave count
2 layer2+3 hash = source port, destination port (as in the header) hash = hash XOR source IP XOR destination IP hash = hash XOR (hash RSHIFT 16) hash = hash XOR (hash RSHIFT 8) hash modulo slave count.
hash策略中异或方式 详细的解释可以参考下方的官方文档
bond的源码地址
https://github.com/torvalds/linux/commit/b5f862180d7011d9575d0499fa37f0f25b423b12
bond的官方文档
https://www.kernel.org/doc/Documentation/networking/bonding.txt
针对802.3ad中的hash算法layer3+4的解释
https://www.llcblog.cn/2020/01/10/how-bonding-lacp-xmit-hash-work/
参考文章
https://blog.csdn.net/sxy2475/article/details/77425931?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.nonecase
关于bond的参数详解及部分源码解释:
https://blog.csdn.net/sinat_20184565/article/details/104510601

你可能感兴趣的:(Linux网卡bonding)