primary egress(outgoing traffic) queuing discipline on any device is known as the root qdisc.
HTB make sure that each class gets at least the minimum of the amount of service assigned to it when requested by that class. When a class requests less than the amount assigned, the remaining (excess) bandwidth is distributed to other classes which request service

# Create the root qdisc, here eth0 is internet link
tc qdisc add dev eth0 root handle 1: htb default 1000
The default 1000 means that any traffic that is not otherwise classified will be assigned to class 1:1000
handles are written X : Y where x is an integer identifying a qdisc(queuing discipline) and y is an integer identifying a class belonging to that qdisc. The handle for a qdisc must have zero for its y value and the handle for a class must have a non-zero value for its y value. The "1:" above is treated as "1:0".tc class add dev eth0 parent 1: classid 1:1 htb rate 2mbit ceil 2mbit burst 1k
here burst=1kilobyte (burst < mtu=1500 B) to avoid fragmentation# Define variables
### Define variables
INTERNET_INTERFACE="eth1" ### Interface connected to the internet
LOCAL_NETWORK_INTERFACE="eth0" ### Interface connected to the local network
GENERAL_RATE="5mbit" ### General rate for all machines except specific ones
SPECIFIC_MAC_PC="aa:bb:cc:11:22:33" ### Specific MAC address of the PC with 10Mbps rate
SPECIFIC_MAC_LAPTOP="dd:ee:ff:44:55:66" ### Specific MAC address of the laptop with 15Mbps rate
### Enable IP forwarding
echo 1 > /proc/sys/net/ipv4/ip_forward
### Clear existing tc configuration
tc qdisc del dev $INTERNET_INTERFACE root
tc qdisc del dev $LOCAL_NETWORK_INTERFACE root
tc qdisc del dev ifb0 root
### Create ifb device
modprobe ifb numifbs=1
ip link set dev ifb0 up
### Configure HTB qdisc on the internet interface (upload)
tc qdisc add dev $INTERNET_INTERFACE root handle 1: htb default 9999
### Create default class for general rate (upload)
tc class add dev $INTERNET_INTERFACE parent 1: classid 1:9999 htb rate $GENERAL_RATE ceil $GENERAL_RATE burst 5k
### Create class for PC with specific MAC address (10Mbps) (upload)
tc class add dev $INTERNET_INTERFACE parent 1: classid 1:11 htb rate 10mbit ceil 10mbit burst 5k
### Create class for laptop with specific MAC address (15Mbps) (upload)
tc class add dev $INTERNET_INTERFACE parent 1: classid 1:12 htb rate 15mbit ceil 15mbit burst 5k
### Create filters to match specific MAC addresses and assign them to their respective classes (upload)
tc filter add dev $INTERNET_INTERFACE parent 1:0 protocol ip prio 1 u32 match u32 0 0 flowid 1:11 match mac $SPECIFIC_MAC_PC flowid 1:11
tc filter add dev $INTERNET_INTERFACE parent 1:0 protocol ip prio 1 u32 match u32 0 0 flowid 1:12 match mac $SPECIFIC_MAC_LAPTOP flowid 1:12
### Configure HTB qdisc on the local network interface (download)
tc qdisc add dev $LOCAL_NETWORK_INTERFACE root handle 1: htb default 9999
### Create default class for general rate (download)
tc class add dev $LOCAL_NETWORK_INTERFACE parent 1: classid 1:9999 htb rate $GENERAL_RATE ceil $GENERAL_RATE burst 5k
### Create filter to match all traffic and assign it to the default class (download)
tc filter add dev $LOCAL_NETWORK_INTERFACE parent 1:0 protocol ip prio 1 u32 match u32 0 0 flowid 1:9999
### Redirect incoming traffic to ifb0 for shaping (download)
iptables -A FORWARD -i $INTERNET_INTERFACE -j ifb0
### Configure HTB qdisc on ifb0 (download)
tc qdisc add dev ifb0 root handle 1: htb default 9999
### Create default class for general rate on ifb0 (download)
tc class add dev ifb0 parent 1: classid 1:9999 htb rate $GENERAL_RATE ceil $GENERAL_RATE burst 5k
### Create filter to match all traffic on ifb0 and assign it to the default class (download)
tc filter add dev ifb0 parent 1:0 protocol ip prio 1 u32 match u32 0 0 flowid 1:9999
+-------+ +------+ +------+
|ingress| |egress| +---------+ |egress|
|qdisc +--->qdisc +--->netfilter+--->qdisc |
|eth1 | |ifb1 | +---------+ |eth1 |
+-------+ +------+ +------+INTERNET_INTERFACE="eth1" LOCAL_NETWORK_INTERFACE="eth0" iptables -F iptables -t nat -F # Enable IP forwarding echo 1 > /proc/sys/net/ipv4/ip_forward # Set up iptables rules to redirect traffic to tc classes iptables -t mangle -A PREROUTING -i $INTERNET_INTERFACE -j MARK --set-mark 1 iptables -t nat -A POSTROUTING -o $INTERNET_INTERFACE -j MASQUERADE iptables -A FORWARD -i $INTERNET_INTERFACE -o $LOCAL_NETWORK_INTERFACE -m mark --mark 1 -j ACCEPT iptables -A FORWARD -i $LOCAL_NETWORK_INTERFACE -o $INTERNET_INTERFACE -j ACCEPT
Reference: http://luxik.cdi.cz/~devik/qos/htb/manual/userg.htm ,
No comments:
Post a Comment