Gentoo Linux以太网Bonding

    技术2022-05-20  42

        现在我们越来越接近主题了。先前的博文大多是关于OS的安装配置,基本上没什么技术含量,但却是为了下一步的工作做准备,同时也可以顺便整理一下自己的思路。我现在已经在酝酿下一个系列,但在这之前先来介绍一下《Gentoo Linux安装纪要之前传》博文中提到过的Bonding,也算是有个交代吧。

        先来看看相关系统的网络配置吧。测试机是Windows XP系统,在以太网卡上配置两个IP地址:“192.168.0.252/24、“192.168.1.252/24,默认网关是“192.168.0.254。配置“192.168.1.252这个地址是专门用于测试IP数据的,这样方便我们过滤其它工具(比如最常用的SecureCRT)产生的数据。系统中装有Wireshark可以用来捕获网络中的数据,以便观察一些网络实现的行为。

        Gentoo Linux的配置在之前的博文中已经介绍过了,这里专门把网络配置列出来:

    eth0      Link encap:Ethernet  HWaddr 00:e0:4c:3a:8f:47

              inet addr:192.168.0.253  Bcast:192.168.0.255  Mask:255.255.255.0

              inet6 addr: fe80::2e0:4cff:fe3a:8f47/64 Scope:Link

              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

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

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

              collisions:0 txqueuelen:1000

              RX bytes:15482 (15.1 KiB)  TX bytes:13681 (13.3 KiB)

              Interrupt:11 Base address:0x6000

     

    eth1      Link encap:Ethernet  HWaddr 00:e0:4c:83:07:9c

              inet6 addr: fe80::2e0:4cff:fe83:79c/64 Scope:Link

              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

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

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

              collisions:0 txqueuelen:1000

              RX bytes:0 (0.0 B)  TX bytes:238 (238.0 B)

              Interrupt:9 Base address:0xa000

     

    eth2      Link encap:Ethernet  HWaddr 00:e0:4c:e2:57:8d

              inet6 addr: fe80::2e0:4cff:fee2:578d/64 Scope:Link

              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

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

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

              collisions:0 txqueuelen:1000

              RX bytes:0 (0.0 B)  TX bytes:238 (238.0 B)

              Interrupt:5 Base address:0xe000

     

    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:0 errors:0 dropped:0 overruns:0 frame:0

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

              collisions:0 txqueuelen:0

              RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

        关于Bonding的安装配置,Gentoo官方网站上面这部分的内容写得很粗略,基本没什么借鉴价值。我在Gentoo维基网站找到一个相当详细的文档,有兴趣的话可以定位到这个地址:http://www.gentoo-wiki.info/HOWTO_Setup_Bonded_ethernet_adapters。我这里写的过程更具实践性,或许您可以从中受到一些启发吧。

        首先就是在内核中增加对“Bonding driver support”,这一步实际上我们在之前的安装里面已经完成了,目前的内核中直接就能支持Bonding了。接下来我们安装ifenslave

    localhost ~ # emerge net-misc/ifenslave

        创建名为net.bond0的链接,让它指向net.lo配置文件:

    localhost ~ # ln -sf /etc/init.d/net.lo /etc/init.d/net.bond0

        在所有的运行级里面启用net.bond0

    localhost ~ # rc-update add net.bond0 default

        配置文件“/etc/conf.d/net”的内容:

    preup() {

        # Adjusting the bonding mode / MII monitor

        # Possible modes are : 0, 1, 2, 3, 4, 5, 6,

        #     OR

        #   balance-rr, active-backup, balance-xor, broadcast,

        #   802.3ad, balance-tlb, balance-alb

     

        # MII monitor time interval typically: 100 milliseconds

     

        if [[ ${IFACE} == "bond0" ]] ; then

            BOND_MODE="balance-rr"

            BOND_MIIMON="100"

            echo ${BOND_MODE} >/sys/class/net/bond0/bonding/mode

            echo ${BOND_MIIMON}  >/sys/class/net/bond0/bonding/miimon

            einfo "Bonding mode is set to ${BOND_MODE} on ${IFACE}"

            einfo "MII monitor interval is set to ${BOND_MIIMON} ms on ${IFACE}"

        else

            einfo "Doing nothing on ${IFACE}"

        fi

        return 0

    }

     

    config_eth0=( "192.168.0.253/24" )

    routes_eth0=( "default via 192.168.0.254" )

    slaves_bond0="eth1 eth2"

    config_bond0=( "192.168.1.253/24" )

    routes_bond0=( "default via 192.168.0.254" )

        其中,BOND_MODE="balance-rr"表示采用Round-Robin模式封装、传输以太网帧。配置完成之后,我们重启Linux系统,并打开Wireshark捕获网络数据,应用“!(ip.host contains 192.168.0)”表达式过滤掉其中“192.168.0网段的数据。Linux系统重启过程中,我们可以捕获到下面的数据(为了看得更清楚一些,我做了一点处理):

    18 51.436858 00:e0:4c:83:07:9c  Broadcast ARP  Who has 192.168.1.253?  Tell 0.0.0.0

    19 52.436863 00:e0:4c:83:07:9c  Broadcast ARP  Who has 192.168.1.253?  Tell 0.0.0.0

        系统连续发送了2次免费ARP通告,用于检测网络中是否存在重复IP地址。我们可以注意到,所使用的MAC地址都是eth1的。然后我们登录到系统中查看网络配置,下面我把需要与之前配置对比的部分列出来:

    localhost ~ # ifconfig

    bond0     Link encap:Ethernet  HWaddr 00:e0:4c:83:07:9c

              inet addr:192.168.1.253  Bcast:192.168.1.255  Mask:255.255.255.0

              inet6 addr: fe80::2e0:4cff:fe83:79c/64 Scope:Link

              UP BROADCAST RUNNING MASTER MULTICAST  MTU:1500  Metric:1

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

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

              collisions:0 txqueuelen:0

              RX bytes:13140 (12.8 KiB)  TX bytes:558 (558.0 B)

     

    eth1      Link encap:Ethernet  HWaddr 00:e0:4c:83:07:9c

     

    eth2      Link encap:Ethernet  HWaddr 00:e0:4c:83:07:9c

        可以看出bond0接口的配置已经正常了,仔细检查之后发现:eth2接口采用了eth1接口的MAC地址。我们在Windows XP的运行“ping -n 1 192.168.1.253命令,捕获到以下网络数据:

    56 60.418753 00:e0:6f:08:53:9a Broadcast ARP  Who has 192.168.1.253?  Tell 192.168.1.252

    57 60.419705 00:e0:4c:83:07:9c 00:e0:6f:08:53:9a ARP  192.168.1.253 is at 00:e0:4c:83:07:9c

    58 60.419730 192.168.1.252  192.168.1.253  ICMP Echo (ping) request  (id=0x0300, seq(be/le)=256/1, ttl=128)

    59 60.419765 00:e0:4c:83:07:9c 00:e0:6f:08:53:9a ARP  192.168.1.253 is at 00:e0:4c:83:07:9c

    60 60.419945 192.168.1.253  192.168.1.252  ICMP Echo (ping) reply    (id=0x0300, seq(be/le)=256/1, ttl=64)

    64 65.435773 00:e0:4c:83:07:9c 00:e0:6f:08:53:9a ARP  Who has 192.168.1.252?  Tell 192.168.1.253

    65 65.435788 00:e0:6f:08:53:9a 00:e0:4c:83:07:9c ARP  192.168.1.252 is at 00:e0:6f:08:53:9a

        分别拔掉两个网卡所连接的网线,再重复上面的实验过程,结果基本一致。同样地在其它模式下得出的实验结果列一个表格,这样方便做比较:

     

    模式

    eth1

    eth2

    免费ARP

    负载

    均衡

    容错

     

    独立模式

    83:07:9c

    e2:57:8d

    2×2

    两个接口各自独立运行

    0.Round-robin

    83:07:9c

    83:07:9c

    2

    依次在两个接口轮流传输

    1.Active-backup

    83:07:9c

    83:07:9c

    2

    eth2接口作为备份,在eth1接口失效之后(100ms)启用

    2.XOR

    83:07:9c

    83:07:9c

    2

    根据源、目的地址异或计算的结果选择传输接口

    3.Broadcast

    83:07:9c

    83:07:9c

    2×2

    数据“同时”往eth1eth2接口上面重复发送

    4.IEEE 802.3ad

    83:07:9c

    83:07:9c

    2

    需要交换机支持LACP协议

    5.transmit load balancing

    83:07:9c

    e2:57:8d

    2

     

    e2:57:8d

    83:07:9c

    2

    拔掉eth1连接的网线

    6.Adaptive load balancing

    83:07:9c

    e2:57:8d

    2

     

    e2:57:8d

    83:07:9c

    2

    拔掉eth1连接的网线

     

        验进展到这一步,我们应该可以知道Bonding是一种把多个网卡组合使用的技术,通过底层驱动来实现负载均衡或者容错,或者二者兼而有之。上面那个表格有几点必须说明一下:

    独立模式是指所有的网卡不组成Bonding而独立运行,这是我们通常情况下使用网卡的状态;

    模式05通过修改eth2接口的MAC地址,使得2个网卡使用相同的地址来实现负载均衡或者容错;

    模式67刚开始时不修改网卡的MAC地址,但一旦eth1接口的链路出现问题,就修改eth2接口的MAC地址,让它接替eth1接口继续工作;如果之后eth2接口的链路又出现问题,就由下一网卡来接替工作,而如果这是最后一个网卡,那么就又轮到eth1接口了。


    最新回复(0)