QOS com Iptables

Publicado por Júlio César Mauro 24/10/2005

[ Hits: 24.452 ]

Download qos.sh




Este script tem a funcao de limitar a banda do usuario em 1/4 da banda
especificada na variavel RATEUP. Caso a banda total nao esteja sendo utilizado,
o usuario que estiver utilizando o sistema, pode utilizar a banda total

  



Esconder código-fonte

#!/bin/sh

#chkconfig: 2345 11 89

# Autor: Julio Cesar Mauro <julio.mauro<at>gmail.com

# Data: 23/10/2004

# Versao: 1.0

# Descricao: Este script tem a funcao de limitar a banda do usuario em 1/4 da banda 

#         especificada na variavel RATEUP. Caso a banda total nao esteja sendo utilizado, 

#        o usuario que estiver utilizando o sistema, pode utilizar a banda total.



. /etc/rc.d/init.d/functions



. /etc/sysconfig/network



[ ${NETWORKING} = "no" ] && exit 0





DEV=eth0

RATEUP=256

RATEDN=700

IPT="/sbin/iptables"

TC="/sbin/tc"

HOSTS="172.16.24.0/24"





case "$1" in

start)



################################################################################################

# Add HTB root qdisc

   $TC  qdisc add dev $DEV root handle 1: htb default 26 2> /dev/null > /dev/null



   $IPT -A FORWARD -t mangle -p udp --sport 1024:49151 -j TOS --set-tos Maximize-Throughput

   $IPT -A FORWARD -t mangle -p udp --dport 1024:49151 -j TOS --set-tos Maximize-Throughput

   $IPT -A FORWARD -t mangle -p tcp --sport 1:65535 -j TOS --set-tos Minimize-Delay

   $IPT -A FORWARD -t mangle -p tcp --dport 1:65535 -j TOS --set-tos Minimize-Delay



# Adiciona root qdisc

   $TC qdisc add dev $DEV root handle 1: htb default 50 2> /dev/null > /dev/null

        $TC class add dev $DEV parent 1: classid 1:1 htb rate ${RATEUP}kbit 2> /dev/null > /dev/null



# Classes # 

   $TC class add dev $DEV parent 1:1 classid 1:20 htb rate $[$RATEUP/4]kbit ceil ${RATEUP}kbit prio 0 2> /dev/null > /dev/null



# Sqf #

   $TC qdisc add dev $DEV parent 1:20 handle 20: sfq perturb 10 2> /dev/null > /dev/null



# Filtros #

   $TC filter add dev $DEV parent 1:0 prio 0 protocol ip handle 20 fw flowid 1:20 2> /dev/null > /dev/null

   $TC filter add dev $DEV parent 1:0 protocol ip prio 0 u32 match ip dst $HOSTS classid 1:20 2> /dev/null > /dev/null

 

# Marca pacotes #

   $IPT -A FORWARD -t mangle

   $IPT -A PREROUTING -t mangle -d 172.16.24.0/24 -j MARK --set-mark 1

        $IPT -t nat -I POSTROUTING -s 172.16.24.0/24 -d 0/0 -o $DEV -j MASQUERADE

   $IPT -A FORWARD -t mangle -p tcp --dport 1:65535 -j MARK --set-mark 23

   $IPT -A FORWARD -t mangle -p tcp --sport 1:65535 -j MARK --set-mark 23

   $IPT -A FORWARD -t mangle -p icmp -j MARK --set-mark 20

   $IPT -A FORWARD -t mangle -p udp -j MARK --set-mark 21

   $IPT -A FORWARD -t mangle -m mark --mark 0 -j MARK --set-mark 26



   echo "Adicionado Regras de QoS:" ; 

   ;;



#################################################################################################

stop)

   $TC  qdisc del dev $DEV root 2> /dev/null > /dev/null



   $IPT -D FORWARD -t mangle -p udp --sport 1024:49151 -j TOS --set-tos Maximize-Throughput

   $IPT -D FORWARD -t mangle -p udp --dport 1024:49151 -j TOS --set-tos Maximize-Throughput

   $IPT -D FORWARD -t mangle -p tcp --sport 1:65535 -j TOS --set-tos Minimize-Delay

   $IPT -D FORWARD -t mangle -p tcp --dport 1:65535 -j TOS --set-tos Minimize-Delay

   $IPT -D FORWARD -t mangle

   $IPT -D FORWARD -t mangle -p tcp --dport 1:65535 -j MARK --set-mark 23

   $IPT -D FORWARD -t mangle -p tcp --sport 1:65535 -j MARK --set-mark 23

   $IPT -D FORWARD -t mangle -p icmp -j MARK --set-mark 20

   $IPT -D FORWARD -t mangle -p udp -j MARK --set-mark 21

   $IPT -D FORWARD -t mangle -m mark --mark 0 -j MARK --set-mark 26

   

   echo "Removendo Regras de QoS:" ;

   ;;



   



#################################################################################################

status)



        echo "[qdisc]"

        $TC  -s qdisc show dev $DEV

        echo "[class]"

        $TC  -s class show dev $DEV

        echo "[filter]"

        $TC  -s filter show dev $DEV

        exit

   ;;

*)

   echo "Usage: qos {start|stop|restart|status}"

    exit 1

esac



# EOF #

##################################################################################################


Scripts recomendados

Script de backup para fita DAT

consistir variável numérica ou alfa-numérica

Registar automáticamente o dns de uma maquina no DNS via DHCP

Piano em Shell + Yad

Atualização automática do Slackware


  

Comentários
[1] Comentário enviado por lucas peregrino em 05/07/2011 - 13:27h

Amigo to vendo seu script aqui voce poderia me da uma ajuda pois queria sensura so alguns ip da maquina para ter o nivel de velocidade menor de download e upload tentei modifica seu poste mais nao deu muito certo nao


Contribuir com comentário




Patrocínio

Site hospedado pelo provedor RedeHost.
Linux banner

Destaques

Artigos

Dicas

Tópicos

Top 10 do mês

Scripts