基于私有目标IP地址的Linux收入过滤

基于私有目标IP地址的Linux收入过滤

我正在尝试在我的 OpenWRT 盒子中设置一个 QoS 脚本,以便将来自互联网的流量分为低优先级和高优先级。对流量进行分类的标准是其在我家庭网络中的目标 IP 地址,即私有 IP 地址。具体来说,如果流量被发送到 IP=192.168.1.22,则应使用低优先级,否则应使用高优先级。

为了实现上述功能,我将到达 eth1 接口的所有 Internet 流量重定向到 IFB 设备,并在该设备上实施流量整形。但是,我遇到的问题是所有流量都流向高优先级类别。为了进行过滤,我使用 iptables 对传入流量设置标记,我猜这就是我犯错的地方。我将过滤规则插入“mangle”表中,我尝试了 PREROUTING、FORWARD 和 POSTROUTING 链,但这些链中的任何一个都不起作用。

任何帮助都将受到赞赏。

此致

丹尼尔

我在此附上我正在使用的脚本:

# Variable definition
ETH=eth1
IFB=ifb1
IP_LP="192.168.1.22/32"
DL_RATE="900kbit"
HP_RATE="890kbit"
LP_RATE="10kbit"
TC="tc"
IPTABLES="iptables"

# Loading the required modules
insmod ifb
insmod sch_htb
insmod sch_ingress
insmod ipt_IMQ
insmod act_mirred
insmod act_connmark
insmod cls_u32
insmod cls_fw
insmod em_u32

# Bringing up the $IFB interface, and redirecting all the ingress traffic arriving to the $ETH interface to it
$TC qdisc del dev $ETH ingress
$TC qdisc add dev $ETH ingress
ifconfig $IFB up 
$TC filter add dev $ETH parent ffff: protocol ip prio 1 u32 match u32 0 0 flowid 1:1 action mirred egress redirect dev $IFB

# Adding the HTB scheduler to the ingress interface
$TC qdisc add dev $IFB root handle 1: htb default 11

# add main rate limit classes
$TC class add dev $IFB parent 1: classid 1:1 htb rate $DL_RATE

# add leaf classes: set the maximum bandwidth that each priority class can get, and the maximum borrowing they can do
$TC class add dev $IFB parent 1:1 classid 1:10 htb rate $LP_RATE ceil $DL_RATE
$TC class add dev $IFB parent 1:1 classid 1:11 htb rate $HP_RATE ceil $DL_RATE

# filter traffic into classes by fwmark
$TC filter add dev $IFB parent 1:0 prio 0 protocol ip handle 10 fw flowid 1:10  # packets with MARK 10 go to classid 1:10
$TC filter add dev $IFB parent 1:0 prio 0 protocol ip handle 11 fw flowid 1:11  # packets with MARK 11 go to classid 1:11

# add MYSHAPER-IN chain to the mangle table in iptables
$IPTABLES -t mangle -N MYSHAPER-IN                      # create a user defined chain in the mangle table
$IPTABLES -t mangle -I PREROUTING -i $ETH -j MYSHAPER-IN            # insert a rule in the PREROUTING chain to jump to our chain

# add fwmark entries to classify different types of traffic - Set fwmark according to the priority.
$IPTABLES -t mangle -A MYSHAPER-IN -d $IP_LP -j MARK --set-mark 10              # rule to mark packets addressed to the low prio host 
$IPTABLES -t mangle -A MYSHAPER-IN -m mark --mark 0 -j MARK --set-mark 11       # rule to mark any unmarked packets as high prio

在这里你可以看到传入流量如何成功重定向到 IFB 接口,但所有流量都转到高优先级类(当然,当我进行此统计时,我让低优先级主机接收数据):

:~# tc -s class show dev ifb1
class htb 1:11 parent 1:1 prio 0 rate 890000bit ceil 900000bit burst 1599b cburst 1599b 
 Sent 71763116 bytes 58364 pkt (dropped 7296, overlimits 0 requeues 0) 
 rate 893208bit 84pps backlog 0b 31p requeues 0 
 lended: 57510 borrowed: 823 giants: 0
 tokens: -50586 ctokens: -189649

class htb 1:10 parent 1:1 prio 0 rate 10000bit ceil 900000bit burst 1600b cburst 1599b 
 Sent 0 bytes 0 pkt (dropped 0, overlimits 0 requeues 0) 
 rate 0bit 0pps backlog 0b 0p requeues 0 
 lended: 0 borrowed: 0 giants: 0
 tokens: 20000000 ctokens: 222218

class htb 1:1 root rate 900000bit ceil 900000bit burst 1599b cburst 1599b 
 Sent 71720770 bytes 58333 pkt (dropped 0, overlimits 0 requeues 0) 
 rate 891776bit 84pps backlog 0b 0p requeues 0 
 lended: 823 borrowed: 0 giants: 0
 tokens: -189649 ctokens: -189649

答案1

我终于解决了这个问题,所以我把它记录在这里,以防它对其他人有用。诀窍是使用 IMQ 设备而不是 IFB 设备。然后使用 iptables 进行分类并使用 IMQ 就没有问题了。

此致

丹尼尔

这是工作脚本:

#!/bin/sh

# This script classifies all the Internet traffic addressed to a given IP in our LAN (IP_LP) as low priority. 
# All the rest of traffic is classified as high priority. In the absence of high prio the low prio can grab all the bandwidth. 
# However, when there is high prio traffic the low prio one is limited to 10Kbos. 

INTERNET="eth1"
IMQ="imq1"
IP_LP="192.168.1.22/32"
DL_RATE="900kbit"
HP_RATE="890kbit"
LP_RATE="10kbit"
TC="tc"
IPTABLES="iptables"
IFCONFIG="ifconfig"

# Loading the required modules
insmod ifb
insmod sch_htb
insmod sch_ingress
insmod ipt_IMQ
insmod act_mirred
insmod act_connmark
insmod cls_u32
insmod cls_fw
insmod em_u32

# Bringing up the IMQ device
$IFCONFIG $IMQ up

# Adding the HTB scheduler to the ingress interface
$TC qdisc add dev $IMQ root handle 1: htb default 11

# add main rate limit classes
$TC class add dev $IMQ parent 1: classid 1:1 htb rate $DL_RATE

# add leaf classes: set the maximum bandwidth that each priority class can get, and the maximum borrowing they can do
$TC class add dev $IMQ parent 1:1 classid 1:10 htb rate $LP_RATE ceil $DL_RATE
$TC class add dev $IMQ parent 1:1 classid 1:11 htb rate $HP_RATE ceil $DL_RATE

# Filtering packets according to destination IP address
$TC filter add dev $IMQ parent 1: protocol ip prio 1 u32 match ip dst $IP_LP flowid 1:10

# Sending packets after SNAT has been done into the IMQ device
$IPTABLES -t mangle -A FORWARD -i $INTERNET -j IMQ --todev 1

相关内容