用tc+iptables进行流量限速,本地流量限速成功,转发流量限速失败。
请问一下是限速脚本有问题,还是tc不支持转发流量的限速?
1、组网
LAN1-PC <--> (eth1)路由器(eth2) <--> LAN2-PC
LAN1-PC :192.168.1.2
路由器eth1:192.168.1.1
路由器eth2:192.168.2.1
LAN1-PC :192.168.2.2
路由器配置了路由转发,没有做NAT
2、实现目标
从LAN1经路由器转发到LAN2的流量限速为10M,最高20M
作为对比,从路由器本地发起到LAN2的流量也做限速,限速30M,最高40M
3、测试结果
用iperf从LAN1-PC发起到LAN2-PC的流量,没有实现限速,速率是网口的最大速率
用iperf从路由器本地发起到LAN2-PC的流量,限速成功,速率保持在接近40M的水平
4、附录:测试脚本
#!/bin/sh
IN_IFACE=eth1
OUT_IFACE=eth2
ETH2_IP=192.168.2.1/32
LAN1_IP=192.168.1.2/32
iptables -F -t mangle
iptables -X -t mangle
iptables -Z -t mangle
iptables -t mangle -A POSTROUTING -s $ETH2_IP -j MARK --set-mark 10
#iptables -t mangle -A FORWARD -s $LAN1_IP -j MARK --set-mark 20
#iptables -t mangle -A POSTROUTING -s $LAN1_IP -j MARK --set-mark 20
iptables -t mangle -A PREROUTING -s $LAN1_IP -j MARK --set-mark 20
tc qdisc add dev $OUT_IFACE root handle 2:0 htb default 1
tc class add dev $OUT_IFACE parent 2:0 classid 2:1 htb rate 50Mbit ceil 60Mbit prio 8
tc class add dev $OUT_IFACE parent 2:1 classid 2:2 htb rate 30Mbit ceil 40Mbit prio 6
tc class add dev $OUT_IFACE parent 2:1 classid 2:3 htb rate 10Mbit ceil 20Mbit prio 7
tc filter add dev $OUT_IFACE parent 2:0 protocol ip prio 1 handle 10 fw classid 2:2
tc filter add dev $OUT_IFACE parent 2:0 protocol ip prio 2 handle 20 fw classid 2:3