Como segmentar el ancho de banda de una red con HTB

Imagen de damage

Viendome en la curiosidad de probar HTB para segmentar el ancho de banda de mi red, he realizado este Script, el cual lo comparto con todos ustedes y espero sea de su utilidad y beneficio.

Vale recalcar que con este script pueden segmentar por IP simples, multiples, subredes o por toda la red, segmenta tanto el upload como el download.
Si tiene alguna sugerencia al mismo bienvenidos sean (realice el script en solo 2 noches osea que puede ser mejorado :cool: )

#!/bin/sh
#
# SCRIPT HTB USANDO FOR-NET-DO v0.1.1
# CREADO POR Damage el 14/12/06 8:00AM

OK="\033[1;0m [ \033[00;32mOK \033[1;0m]\033[0m"
echo -e "ANCHO DE BANDA: $OK"

### VARIABLES ###flow
WAN="0/0"
DEVWAN="eth0"
DEVLAN="eth1"
TC=/sbin/tc
IPTABLES="/sbin/iptables -t mangle"
HTB=/sbin/htb

### PUERTOS ###
PROXY_PORT="8080"
SMTP_PORT="25"
POP3_PORT="110"
FTP_PORT="20:21"

##########
# INICIO #
##########
### ---------------- FLUSH DE REGLAS ----------------- ###
$TC qdisc del dev $DEVWAN root handle 1: htb
$TC qdisc del dev $DEVLAN root handle 2: htb
$IPTABLES -F
$IPTABLES -X

### ---------------- CREANDO CLASE ROOT -------------- ###
$TC qdisc add dev $DEVWAN root handle 1: htb default 97 r2q 10
$TC qdisc add dev $DEVLAN root handle 2: htb default 98 r2q 10

### ----------------- CLASES PADRES ---------------------------------------- ###
$TC class add dev $DEVWAN parent 1: classid 1:100 htb rate 10Mbit ceil 10Mbit quantum 128000
$TC class add dev $DEVLAN parent 2: classid 2:200 htb rate 10Mbit ceil 10Mbit quantum 128000

#ICMP tienen prioridad (marcas por defecto)
$IPTABLES -A OUTPUT -p icmp -j MARK --set-mark 97
$IPTABLES -A FORWARD -p icmp -j MARK --set-mark 97
$IPTABLES -A POSTROUTING -p icmp -j MARK --set-mark 97
$IPTABLES -A PREROUTING -p icmp -j MARK --set-mark 97
$IPTABLES -A OUTPUT -p icmp -j MARK --set-mark 98
$IPTABLES -A FORWARD -p icmp -j MARK --set-mark 98
$IPTABLES -A POSTROUTING -p icmp -j MARK --set-mark 98
$IPTABLES -A PREROUTING -p icmp -j MARK --set-mark 98

### BW1 ANCHO DE BANDA PARA IP O SUBRED ESPECIFICA #####
RATE_LAN1="64kbit"
CEIL_LAN1="128kbit"
NETWORKS_BW1="10.10.10.2"
for NET in $NETWORKS_BW1; do
$IPTABLES -A PREROUTING -s $NET -d $WAN -i $DEVLAN -j MARK --set-mark 1
$IPTABLES -A FORWARD -s $WAN -d $NET -i $DEVWAN -o $DEVLAN -j MARK --set-mark 2
$IPTABLES -A OUTPUT -d $NET -p tcp --sport $PROXY_PORT -o $DEVLAN -j MARK --set-mark 2
$IPTABLES -A OUTPUT -d $NET -p tcp --sport $SMTP_PORT -o $DEVLAN -j MARK --set-mark 2
$IPTABLES -A OUTPUT -d $NET -p tcp --sport $POP3_PORT -o $DEVLAN -j MARK --set-mark 2
done
$TC class add dev $DEVWAN parent 1:100 classid 1:1 htb rate $RATE_LAN1 ceil $CEIL_LAN1 quantum 1500
$TC class add dev $DEVLAN parent 2:200 classid 2:2 htb rate $RATE_LAN1 ceil $CEIL_LAN1 quantum 1500
$TC filter add dev $DEVWAN parent 1:0 protocol ip prio 1 handle 1 fw classid 1:1
$TC filter add dev $DEVLAN parent 2:0 protocol ip prio 1 handle 2 fw classid 2:2

### BW2 ANCHO DE BANDA PARA IP O SUBRED ESPECIFICA #####
RATE_LAN2="64kbit"
CEIL_LAN2="64kbit"
NETWORKS_BW2="10.10.10.3"
for NET in $NETWORKS_BW2; do
$IPTABLES -A PREROUTING -s $NET -d $WAN -i $DEVLAN -j MARK --set-mark 3
$IPTABLES -A FORWARD -s $WAN -d $NET -i $DEVWAN -o $DEVLAN -j MARK --set-mark 4
$IPTABLES -A OUTPUT -d $NET -p tcp --sport $PROXY_PORT -o $DEVLAN -j MARK --set-mark 4
$IPTABLES -A OUTPUT -d $NET -p tcp --sport $SMTP_PORT -o $DEVLAN -j MARK --set-mark 4
$IPTABLES -A OUTPUT -d $NET -p tcp --sport $POP3_PORT -o $DEVLAN -j MARK --set-mark 4
done
$TC class add dev $DEVWAN parent 1:100 classid 1:3 htb rate $RATE_LAN2 ceil $CEIL_LAN2 quantum 1500
$TC class add dev $DEVLAN parent 2:200 classid 2:4 htb rate $RATE_LAN2 ceil $CEIL_LAN2 quantum 1500
$TC filter add dev $DEVWAN parent 1:0 protocol ip prio 1 handle 3 fw classid 1:3
$TC filter add dev $DEVLAN parent 2:0 protocol ip prio 1 handle 4 fw classid 2:4

#### ----------- LINEA DE CLASE Y FILTRO DE CLASE POR DEFECTO ------------ ####
$TC class add dev $DEVWAN parent 1:100 classid 1:97 htb rate 9Mbit ceil 9Mbit
$TC class add dev $DEVLAN parent 2:200 classid 2:98 htb rate 9Mbit ceil 9Mbit
$TC filter add dev $DEVWAN parent 1:0 protocol ip prio 1 handle 97 fw classid 1:97
$TC filter add dev $DEVLAN parent 2:0 protocol ip prio 1 handle 98 fw classid 2:98


Nota aclaratoria: El script no calcula el valor del quantum automaticamente :) (no soy mandrake el mago en bash asi que no pidan magias ni milagros jeje) por lo cual deben tener presente ésta formula:
quantum=rate*1024/8/r2q, con un quantum=1500 se soporta una ancho de banda de hasta 128Kbits si es mayor debes utilizar la formúla para calcularlo y declararlo en la clase respectiva.

Necesito dar un rate de 256Kbit y un ceil de 384Kbit, entonces aplicamos:
quantum=(256*1024)/8)/10)
quantum=3277, esto lo declaramos en la clase del grupo correspondiente por ejemplo:

$TC class add dev $DEVWAN parent 1:100 classid 1:5 htb rate 256kbit ceil 384kbit quantum 3277
$TC class add dev $DEVLAN parent 2:200 classid 2:6 htb rate 256kbit ceil 384kbit quantum 3277


Ejemplo:

Listo eso es todo por ahora, espero haber sido claro :?
Pueden adicionar tantos grupos como deseen solo deben modificar unos pocos parametros :)
VIVA el Software Libre y el Libre Conocimiento..........
*Corregido al 20/6/07@8:55AM

Comentarios

donde consigo este archivo

"""
Asi es debes bajar en htb.iniv0.8.5 (algo asi se llama) y copiarlo a /sbin con el nombre de htb, luego hacerlo ejecutable con chmod a+x.

"""
porfavor podrias decir el nombre exacto del archivo para buscarlo por la red????
o ya esta incluido en el cd de centos4????

Usa google y escibe en

Imagen de damage

Usa google y escibe en buscar htb.init y te dara como resultado de una la pagina de sourceforge.net, de ahi descargalo.

Keep The Fire Burning.....
Stryper 1988
http://counter.li.org/

El r2q puede ser entre 1,10

Imagen de damage

El r2q puede ser entre 1,10 o 100, eso depende del rate dado, despeja la formula del quantum en función del r2q y te daras una idea, todo depende del rate de la clase padre, pero 10 es mas que suficiente para casi todo el mundo al menos que menejes más de 10Mbits de BW.
Keep The Fire Burning.....
Stryper 1988
http://counter.li.org/

VOIP

Imagen de edleo

Hola a todos!
este script podria servir para darle prioridad a la voip dentro de una red, tanto de subida como de bajada?
Gracias por la ayuda de antemano

De ley mamey, pero mejor

Imagen de damage

De ley mamey, pero mejor dale una mirada a otro post que se te hara mas facil esta aqui mismo en los COMO es sobre HTB-GEN.
Saludos.
Keep The Fire Burning.....
Stryper 1988
http://counter.li.org/

Instalacion en centos 5

estoy tratando de mejorar este script para voip que después comentare , otras cositas mas en un centos 5 pero me presenta este error , me he bajado el script de htb.ini y lo he copiado a /sbin como htb con permisos de ejecución al correrlo me presenta este error:

ANCHO DE BANDA: [ OK ]
RTNETLINK answers: Invalid argument
RTNETLINK answers: Invalid argument
What is "quantum"?
Usage: ... qdisc add ... htb [default N] [r2q N]
default minor id of class to which unclassified packets are sent {0}
r2q DRR quantums are computed as rate in Bps/r2q {10}
debug string of 16 numbers each 0-3 {0}

... class add ... htb rate R1 [burst B1] [mpu B] [overhead O]
[prio P] [slot S] [pslot PS]
[ceil R2] [cburst B2] [mtu MTU] [quantum Q]
rate rate allocated to this class (class can still borrow)
burst max bytes burst which can be accumulated during idle period {computed}
mpu minimum packet size used in rate computations
overhead per-packet size overhead used in rate computations
ceil definite upper class rate (no borrows) {rate}
cburst burst but for ceil {computed}
mtu max packet size we create rate map for {1600}
prio priority of leaf; lower are served first {0}
quantum how much bytes to serve from leaf at once {use r2q}

TC HTB version 3.3
What is "quantum"?
Usage: ... qdisc add ... htb [default N] [r2q N]
default minor id of class to which unclassified packets are sent {0}
r2q DRR quantums are computed as rate in Bps/r2q {10}
debug string of 16 numbers each 0-3 {0}

... class add ... htb rate R1 [burst B1] [mpu B] [overhead O]
[prio P] [slot S] [pslot PS]
[ceil R2] [cburst B2] [mtu MTU] [quantum Q]
rate rate allocated to this class (class can still borrow)
burst max bytes burst which can be accumulated during idle period {computed}
mpu minimum packet size used in rate computations
overhead per-packet size overhead used in rate computations
ceil definite upper class rate (no borrows) {rate}
cburst burst but for ceil {computed}
mtu max packet size we create rate map for {1600}
prio priority of leaf; lower are served first {0}
quantum how much bytes to serve from leaf at once {use r2q}

TC HTB version 3.3
RTNETLINK answers: No such file or directory
RTNETLINK answers: No such file or directory
RTNETLINK answers: No such file or directory
RTNETLINK answers: No such file or directory
RTNETLINK answers: Invalid argument
We have an error talking to the kernel
RTNETLINK answers: Invalid argument
We have an error talking to the kernel
RTNETLINK answers: No such file or directory
RTNETLINK answers: No such file or directory
RTNETLINK answers: Invalid argument
We have an error talking to the kernel
RTNETLINK answers: Invalid argument
We have an error talking to the kernel
RTNETLINK answers: No such file or directory
RTNETLINK answers: No such file or directory
RTNETLINK answers: Invalid argument
We have an error talking to the kernel
RTNETLINK answers: Invalid argument
We have an error talking to the kernel

estoy usando el kernel
2.6.18-53.1.4.el5

ideas?

bye

Pues algo has hecho mal, te

Imagen de damage

Pues algo has hecho mal, te esta botando un erro de Quantum, deberias revisar para que sirver el quantum (leer), tratar de depurar el error es muy complejo eso es tarea tuya, este script en especial tengo meses que no lo topo y no lo he probado en Centos 5 ni con esa version de Kernel, no sabrìa si tiene algun bug. En dìas posteriores me dedicare a probarlo nuevamente.

Keep The Fire Burning.....
Stryper 1988

Páginas