使用的配置环境:openwrt的x86系统;每个接口都是独立的网口,局域网中,拥有唯一的mac地址;
1.配置多wan口;
(1).在/etc/config/network中,配置对应网口的信息,这里我将eth1配置成wan1了;注意:metric值一定要与wan的不同;
(2).在/etc/config/firewall中,配置wan1的防火墙规则,将wan1加入到option name wan对应的接口列表中,如下式:
(3).重启network和firewall;
/etc/init.d/mwan3 restart
/etc/init.d/network restart
2.使用mwan3,实现多wan时的负载均衡;
(1), 在mwan3的配置文件中/etc/config/mwan3配置wan1的信息;
配置如下:
注意:(因为本人是在界面上配置的,所以部分配置信息在文件在的位置在后面)
(1).配置成负载均衡时,wan1的metric值和weight值要与wan口的完全相同;
(2).做备份时,metric值weight值大的,作为备用线路;
注:此处policy可以配置多条的,每个接口对应的member也可以多个,当配置特殊路由时,将相应的配置写在balanced之前,如:按80端口只走wan1等,相应的配置可以查资料;
(2).配置完成后,重启mwan3;
/etc/init.d/mwan3 restart
(3).结果
以上配置完成后,监听eth0 ,eth1端口,查看包的情况;本人,eth0对应wan,10M的宽带;eth1对应的wan1,20M的宽带,下载速度,能达到30M,截图如下;
注意:本人试过使用
相同网关的线路,不能实现带宽叠加;
3.mwan3实现负载均衡的原理;
(1).关于策略路由:
root@Lepton:/#
ip rule s
0: from all lookup 128
1: from all lookup local
1001: from all iif eth0 lookup main
1002: from all iif eth1 lookup main
2001: from all fwmark 0x100/0xff00 lookup 1
2002: from all fwmark 0x200/0xff00 lookup 2
2254: from all fwmark 0xfe00/0xff00 unreachable
32766: from all lookup main
32767: from all lookup default
root@Lepton:/#
(2).路由表如下:
root@Lepton:/#
ip route
default via 192.168.1.2 dev eth0 proto static src 192.168.1.103
default via 172.16.8.1 dev eth1 proto static src 172.16.8.121 metric 1
172.16.8.0/24 dev
eth1 proto static scope link metric 1
172.16.8.1 dev
eth1 proto static scope link src 172.16.8.121 metric 1
172.16.9.0/24 dev
br-lan proto kernel scope link src 172.16.9.2
192.168.1.0/24 dev
eth0 proto kernel scope link src 192.168.1.103
192.168.1.2 dev
eth0 proto static scope link src 192.168.1.103
root@Lepton:/#
(3).关于防火墙规则:
上图为规则在防火墙的对应关系,mwan3_policy_balanced中,使用
mode random probability 模块,使得wan上有50%的包,打上(0x100/0xff00);剩下的包,打上(0x200/0xff00);
而源地址不为wan网关的包,只要匹配到(0x100/0xff00)就走mwan3_iface_wan;(这里就是为什么对相同网关,无法做到带宽叠加的原因),剩下的同理,匹配(0x200/0xff00 )走mwan3_iface_wan1.
root@Lepton:/# iptables -t mangle -L
Chain PREROUTING (policy ACCEPT)
target prot opt source destination
mwan3_hook all -- anywhere anywhere
fwmark all -- anywhere anywhere
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
mssfix all -- anywhere anywhere
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
mwan3_hook all -- anywhere anywhere
mwan3_output_hook all -- anywhere anywhere
Chain POSTROUTING (policy ACCEPT)
target prot opt source destination
Chain fwmark (1 references)
target prot opt source destination
Chain mssfix (1 references)
target prot opt source destination
TCPMSS tcp -- anywhere anywhere tcp flags:SYN,RST/SYN /* wan (mtu_fix) */ TCPMSS clamp to PMTU
TCPMSS tcp -- anywhere anywhere tcp flags:SYN,RST/SYN /* wan (mtu_fix) */ TCPMSS clamp to PMTU
Chain mwan3_connected (1 references)
target prot opt source destination
MARK all -- anywhere 127.0.0.0/8 MARK or 0xff00
MARK all -- anywhere base-address.mcast.net/3 MARK or 0xff00
MARK all -- anywhere 172.16.8.0/24 MARK or 0xff00
MARK all -- anywhere 172.16.8.1 MARK or 0xff00
MARK all -- anywhere 172.16.9.0/24 MARK or 0xff00
MARK all -- anywhere 192.168.1.0/24 MARK or 0xff00
MARK all -- anywhere 192.168.1.2 MARK or 0xff00
MARK all -- anywhere 127.0.0.0 MARK or 0xff00
MARK all -- anywhere 127.0.0.0/8 MARK or 0xff00
MARK all -- anywhere localhost MARK or 0xff00
MARK all -- anywhere 127.255.255.255 MARK or 0xff00
MARK all -- anywhere 172.16.8.0 MARK or 0xff00
MARK all -- anywhere 172.16.8.121 MARK or 0xff00
MARK all -- anywhere 172.16.8.255 MARK or 0xff00
MARK all -- anywhere 172.16.9.0 MARK or 0xff00
MARK all -- anywhere Lepton.lan MARK or 0xff00
MARK all -- anywhere 172.16.9.255 MARK or 0xff00
MARK all -- anywhere 192.168.1.0 MARK or 0xff00
MARK all -- anywhere 192.168.1.103 MARK or 0xff00
MARK all -- anywhere 192.168.1.255 MARK or 0xff00
Chain mwan3_hook (2 references)
target prot opt source destination
CONNMARK all -- anywhere anywhere CONNMARK restore mask 0xff00
mwan3_ifaces all -- anywhere anywhere mark match 0x0/0xff00
mwan3_rules all -- anywhere anywhere mark match 0x0/0xff00
CONNMARK all -- anywhere anywhere CONNMARK save mask 0xff00
mwan3_connected all -- anywhere anywhere
Chain mwan3_iface_wan (1 references)
target prot opt source destination
MARK all -- 192.168.1.2 anywhere mark match 0x0/0xff00 /* wan */ MARK or 0xff00
MARK all -- 192.168.1.0/24 anywhere mark match 0x0/0xff00 /* wan */ MARK or 0xff00
MARK all -- anywhere anywhere mark match 0x0/0xff00 /* wan */ MARK xset 0x100/0xff00
Chain mwan3_iface_wan1 (1 references)
target prot opt source destination
MARK all -- 172.16.8.1 anywhere mark match 0x0/0xff00 /* wan1 */ MARK or 0xff00
MARK all -- 172.16.8.0/24 anywhere mark match 0x0/0xff00 /* wan1 */ MARK or 0xff00
MARK all -- anywhere anywhere mark match 0x0/0xff00 /* wan1 */ MARK xset 0x200/0xff00
Chain mwan3_ifaces (1 references)
target prot opt source destination
mwan3_iface_wan all -- anywhere anywhere mark match 0x0/0xff00
mwan3_iface_wan1 all -- anywhere anywhere mark match 0x0/0xff00
Chain mwan3_output_hook (1 references)
target prot opt source destination
mwan3_track_wan icmp -- anywhere anywhere icmp echo-request length 32
mwan3_track_wan1 icmp -- anywhere anywhere icmp echo-request length 32
Chain mwan3_policy_balanced (1 references)
target prot opt source destination
MARK all -- anywhere anywhere mark match 0x0/0xff00 statistic mode random probability 0.50000000000 /* wan1 1 2 */ MARK xset 0x200/0xff00
MARK all -- anywhere anywhere mark match 0x0/0xff00 /* wan 1 1 */ MARK xset 0x100/0xff00
Chain mwan3_rules (1 references)
target prot opt source destination
mwan3_policy_balanced all -- anywhere anywhere mark match 0x0/0xff00 /* default_rule */
Chain mwan3_track_wan (1 references)
target prot opt source destination
MARK all -- anywhere resolver2.opendns.com MARK or 0xff00
MARK all -- anywhere resolver1.opendns.com MARK or 0xff00
MARK all -- anywhere google-public-dns-a.google.com MARK or 0xff00
MARK all -- anywhere google-public-dns-b.google.com MARK or 0xff00
MARK all -- anywhere public1.114dns.com MARK or 0xff00
Chain mwan3_track_wan1 (1 references)
target prot opt source destination
MARK all -- anywhere resolver2.opendns.com MARK or 0xff00
MARK all -- anywhere google-public-dns-a.google.com MARK or 0xff00
MARK all -- anywhere public1.114dns.com MARK or 0xff00
root@Lepton:/#