Shaiper или распиливаем траффик

Материал из support.qbpro.ru

описываем правила для пакетов в iptables:

#!/bin/bash
iptables -t mangle -A PREROUTING -p icmp -j MARK --set-mark 0x1
iptables -t mangle -A PREROUTING -p icmp -j RETURN 
iptables -t mangle -A PREROUTING -m tos --tos Minimize-Delay -j MARK --set-mark 0x1
iptables -t mangle -A PREROUTING -m tos --tos Minimize-Delay -j RETURN
iptables -t mangle -A PREROUTING -m tos --tos Minimize-Cost -j MARK --set-mark 0x5
iptables -t mangle -A PREROUTING -m tos --tos Minimize-Cost -j RETURN
iptables -t mangle -A PREROUTING -m tos --tos Maximize-Throughput -j MARK --set-mark 0x6
iptables -t mangle -A PREROUTING -m tos --tos Maximize-Throughput -j RETURN
### приоритетные пакеты по траффику - по портам
iptables -t mangle -A PREROUTING -p tcp -m multiport --sports 22,80,53,1194,5061 -j MARK --set-mark 0x1
iptables -t mangle -A PREROUTING -p tcp -m multiport --sports 22,80,53,1194,5061 -j RETURN
### для пакетов, с которых начинается TCP-соединение, т.е. SYN-пакетов
iptables -t mangle -I PREROUTING -p tcp -m tcp --tcp-flags SYN,RST,ACK SYN -j MARK --set-mark 0x1
iptables -t mangle -I PREROUTING -p tcp -m tcp --tcp-flags SYN,RST,ACK SYN -j RETURN
### После того, как в цепочку PREROUTING, таблицы mangle, будут внесены все правила, закончим ее правилом:
iptables -t mangle -A PREROUTING -j MARK --set-mark 0x6

теперь распределяем трафик в соответствии с классами..

#!/bin/bash

## сброс правил

DEV1='ppp0'
DEV0='eth0'
DEV2='eth1'
DEV3='eth2'
DEV4='eth3'
tc q d dev $DEV0 root handle 1: htb
tc q d dev $DEV1 root handle 1: htb
tc q d dev $DEV2 root handle 1: htb
tc q d dev $DEV3 root handle 1: htb
tc q d dev $DEV4 root handle 1: htb

### присвойте переменной CEIL величину, составляющую 75% от общей пропускной способности для исходящего трафика
CEIL='5'
tc qdisc add dev ppp0 root handle 1: htb default 15
tc class add dev ppp0 parent 1: classid 1:1 htb rate ${CEIL}mbit ceil ${CEIL}mbit
tc class add dev ppp0 parent 1:1 classid 1:10 htb rate 3mbit ceil 5mbit prio 0
tc class add dev ppp0 parent 1:1 classid 1:11 htb rate 3mbit ceil ${CEIL}mbit prio 1
tc class add dev ppp0 parent 1:1 classid 1:12 htb rate 2mbit ceil ${CEIL}mbit prio 2
tc class add dev ppp0 parent 1:1 classid 1:13 htb rate 2mbit ceil ${CEIL}mbit prio 2
tc class add dev ppp0 parent 1:1 classid 1:14 htb rate 1mbit ceil ${CEIL}mbit prio 3
tc class add dev ppp0 parent 1:1 classid 1:15 htb rate 1mbit ceil ${CEIL}mbit prio 3
tc qdisc add dev ppp0 parent 1:12 handle 120: sfq perturb 10
tc qdisc add dev ppp0 parent 1:13 handle 130: sfq perturb 10
tc qdisc add dev ppp0 parent 1:14 handle 140: sfq perturb 10
tc qdisc add dev ppp0 parent 1:15 handle 150: sfq perturb 10
### фильтры, которые будут выполнять классификацию пакетов, основываясь на метках iptables
tc filter add dev ppp0 parent 1:0 protocol ip prio 1 handle 1 fw classid 1:10
tc filter add dev ppp0 parent 1:0 protocol ip prio 2 handle 2 fw classid 1:11
tc filter add dev ppp0 parent 1:0 protocol ip prio 3 handle 3 fw classid 1:12
tc filter add dev ppp0 parent 1:0 protocol ip prio 4 handle 4 fw classid 1:13
tc filter add dev ppp0 parent 1:0 protocol ip prio 5 handle 5 fw classid 1:14
tc filter add dev ppp0 parent 1:0 protocol ip prio 6 handle 6 fw classid 1:15 
###

.. все распределение формируется на внешнем интерфейсе.. посмотреть можно командой:

tc -s class show dev ppp0

ДОПОЛНИТЕЛЬНО: