Answers:
内核的流量整形层基本上是连接到网卡的数据包调度程序。因此,一种流量整形策略适用于一张网卡。
对于您的情况,您可以做的是创建一个IP地址和所连接带宽的列表,然后为每个IP地址创建:
@Zoredache给出的示例有效,但是我个人更喜欢使用Netfilter功能而不是TC来过滤数据包,并使用HTB而不是CBQ来进行交换算法。因此,您可以尝试执行以下操作(关联数组需要Bash 4):
#! /bin/bash
NETCARD=eth0
MAXBANDWIDTH=100000
# reinit
tc qdisc del dev $NETCARD root handle 1
tc qdisc add dev $NETCARD root handle 1: htb default 9999
# create the default class
tc class add dev $NETCARD parent 1:0 classid 1:9999 htb rate $(( $MAXBANDWIDTH ))kbit ceil $(( $MAXBANDWIDTH ))kbit burst 5k prio 9999
# control bandwidth per IP
declare -A ipctrl
# define list of IP and bandwidth (in kilo bits per seconds) below
ipctrl[192.168.1.1]="256"
ipctrl[192.168.1.2]="128"
ipctrl[192.168.1.3]="512"
ipctrl[192.168.1.4]="32"
mark=0
for ip in "${!ipctrl[@]}"
do
mark=$(( mark + 1 ))
bandwidth=${ipctrl[$ip]}
# traffic shaping rule
tc class add dev $NETCARD parent 1:0 classid 1:$mark htb rate $(( $bandwidth ))kbit ceil $(( $bandwidth ))kbit burst 5k prio $mark
# netfilter packet marking rule
iptables -t mangle -A INPUT -i $NETCARD -s $ip -j CONNMARK --set-mark $mark
# filter that bind the two
tc filter add dev $NETCARD parent 1:0 protocol ip prio $mark handle $mark fw flowid 1:$mark
echo "IP $ip is attached to mark $mark and limited to $bandwidth kbps"
done
#propagate netfilter marks on connections
iptables -t mangle -A POSTROUTING -j CONNMARK --restore-mark
-编辑:忘记默认类,并在脚本末尾传播标记。
诸如此类的事情对我来说将承包商的网络摄像头限制在有限的带宽范围内。有关详细信息,请参见手册中的tc。
#!/bin/bash
set -x
DEV=eth0
export DEV
tc qdisc del dev $DEV root
tc qdisc del dev $DEV root
tc qdisc add dev $DEV root handle 1: cbq avpkt 1000 bandwidth 100mbit
# setup a class to limit to 1500 kilobits/s
tc class add dev $DEV parent 1: classid 1:1 cbq rate 1500kbit \
allot 1500 prio 5 bounded isolated
# add traffic from 10.2.1.37 to that class
tc filter add dev $DEV parent 1: protocol ip prio 16 u32 \
match ip src 10.2.1.37 flowid 1:1