Skip to main content

Bandwidth Shaper Script di openSUSE



# Tulisan ini digunakan sebagai bahan dasar saja untuk memahami
# Dibuat sudah lama sekali dan mungkin ada yang deprecated
# Use at your own risk

Seorang rekan menanyakan kenapa mengkonfigurasi openSuSE koq susah sekali. Katanya untuk ngejalanin script sederhana membuat default route aja musti ngebuat script yang njlimet. He…he..he….
Dia bilang kalau di RedHat dan turunannya seperti Fedora dan Centos khan ada rc.local, terus kalau di openSUSE padanannya apa?

OK. Sebetulnya masalah ini sudah pernah saya bahas diblog saya yang dulu dan dibeberapa email saya tapi tak mengapa saya ulang lagi di sini dan saya tambahkan beberapa hal yang saya anggap relevan karena kebetulan ada juga yang nanya tentang load balancing trafik internet ke dua gateway dan implementasi htb (hierarchical tocken bucket) untuk traffic shaping.

Jadi saya akan menjelaskan implementasi load balancing, traffic shaping dan rc.local di openSuSE sekaligus, mumpung lagi ada kesempatan nulis.


LOAD BALANCING TRAFIK INTERNET

Di tempat saya koneksi internet terhubung ke dua ISP, LC 128 kbps ke ISP-A dan ADSL ke ISP-B. Singkat cerita saya menggunakan sebuah server dengan 3 ethernet card

eth0 ip address 202.158.xx.yyy netmask 255.255.255.240 gw 202.158.xx.yyy
eth1 ip address 10.0.50.5 netmask 255.255.255.248 gw 10.0.50.1
eth2 ip address 192.168.117.171 netmask 255.255.255.0 gw 192.168.117.171

Untuk load balancing trafik ini saya mengacu pada dokumen LARTC (Linux Advanced Routing & Traffic Control) How To yang disusun oleh Bert Hubert (thanks Om Bert). Syarat untuk load balancing adalah sudah terinstallnya paket iproute2, yang sudah terinstall saat saya menginstall openSUSE 10.3.

Selanjutnya

# pertama hapus semua default route dari ip route
#
ip route del default
ip route del 10.0.50.0/29
ip route del 202.158.xx.zzz/28
ip route del 169.254.0.0/16

# tambah ip route yang masuk akal
#

ip route add 10.0.50.0/29 dev eth1 proto kernel scope link src 10.0.50.5
ip route add 202.158.xx.zzz/28 dev eth0 proto kernel scope link src 202.158.xx.xxx

# tambah juga load balancing default route (ip router anda)
# weight menandakan mana yang lebih anda pilih, 2 > 1
ip route add default scope global \
nexthop via 202.158.xx.yyy dev eth0 weight 1 \
nexthop via 10.0.50.1 dev eth1 weight 2

# tambah table policy routing
#
ip route add via 202.158.xx.yyy dev eth0 src 202.158.xx.xxx table ISP-A
ip route add via 10.0.50.1 dev eth1 src 10.0.50.5 table ISP-B

# ini musti ditest sometimes we need this
ip route add 192.168.117.0/24 dev eth4 table ISP-A
ip route add 10.0.50.0/29 dev eth1 table ISP-A
ip route add 127.0.0.0/8 dev lo table ISP-A
ip route add 192.168.117.0/24 dev eth4 table ISP-B
ip route add 202.158.xx.yyy/28 dev eth0 table ISP-B
ip route add 127.0.0.0/8 dev lo table ISP-B

# jangan lupa setup dua ip rule agar sistem menggunakan policy routing diatas
#
ip rule add from 202.158.xx.xxx table ISP-A
ip rule add from 10.0.50.5 table ISP-B

# setting IP masquerade
#
iptables -t nat -A POSTROUTING -s 192.168.117.0/24 -d 0/0 -o eth0 -j MASQUERADE
iptables -t nat -A POSTROUTING -s 192.168.117.0/24 -d 0/0 -o eth1 -j MASQUERADE

# set TOS field agar router gak bingung, supaya ssh dan ftp bisa jalan

iptables -t mangle -A PREROUTING -j TOS –set-tos 0x00
iptables -t mangle -A OUTPUT -j TOS –set-tos 0x00

perlu diingat bahwa balancing disini tidak sempurna, karena route based, dan routes di-cache. Jadi route ke site yang sering dikunjungi akan selalu melaui provider yang sama.
Contoh di server ini kalau saya traceroute www.detik.com akan selalu lewat ISP-A dan traceroute www.republika.co.id akan selalu melalui ISP-B.


TRAFFIC SHAPING

Tujuan traffic shaping di sini adalah (tentu saja anda bisa melakukan shaping dengan tujuan lain he..he…he..):
- menjaga low latency untuk interactive trafic, jangan sampai upload atau download mengganggu ssh.
- menjaga agar browsing berjalan pada reasonable speeds sementara melalukan up atau downloading.
- menjaga agar upload tidak mengganggu download dan sebaliknya.
- menandakan port atau host yang sering memakan traffic sebagai low priority.

Ada banyak sumber di intenet misalnya tulisan ini dan favorit saya adalah sekali lagi om Bert dengan dokumen LARTC. Jangan lupa baca pre-requisites untuk menjalankan HTB dan pastikan kernel anda mendukungnya.

Implementasi di tempat saya sederhana saja seperti di bawah:

untuk eth0 dan eth1:

DOWNLINK=96 # untuk eth0, untuk eth1 –> DOWNLINK=288
UPLINK=80 # untuk eth0, untuk eth1 –> UPLINK=20
DEV=eth0 # ganti dengan eth1 untuk eth1

# low priority OUTGOING traffic - you can leave this blank if you want
# low priority source netmasks
NOPRIOHOSTSRC=

# low priority destination netmasks
NOPRIOHOSTDST=

# low priority source ports
NOPRIOPORTSRC=

# low priority destination ports
NOPRIOPORTDST=

if [ "$1" = "status" ]
then
tc -s qdisc ls dev $DEV
tc -s class ls dev $DEV
exit
fi

# clean existing down- and uplink qdiscs, hide errors
tc qdisc del dev $DEV root 2> /dev/null > /dev/null
tc qdisc del dev $DEV ingress 2> /dev/null > /dev/null

if [ "$1" = "stop" ]
then
exit
fi

###### uplink

# install root HTB, point default traffic to 1:20:

tc qdisc add dev $DEV root handle 1: htb default 20

# shape everything at $UPLINK speed - this prevents huge queues in your
# DSL modem which destroy latency:

tc class add dev $DEV parent 1: classid 1:1 htb rate ${UPLINK}kbit burst 6k

# high prio class 1:10:

tc class add dev $DEV parent 1:1 classid 1:10 htb rate ${UPLINK}kbit \
burst 6k prio 1

# bulk & default class 1:20 - gets slightly less traffic,
# and a lower priority:

tc class add dev $DEV parent 1:1 classid 1:20 htb rate $[9*$UPLINK/10]kbit \
burst 6k prio 2

tc class add dev $DEV parent 1:1 classid 1:30 htb rate $[8*$UPLINK/10]kbit \
burst 6k prio 2

# all get Stochastic Fairness:
tc qdisc add dev $DEV parent 1:10 handle 10: sfq perturb 10
tc qdisc add dev $DEV parent 1:20 handle 20: sfq perturb 10
tc qdisc add dev $DEV parent 1:30 handle 30: sfq perturb 10

# TOS Minimum Delay (ssh, NOT scp) in 1:10:

tc filter add dev $DEV parent 1:0 protocol ip prio 10 u32 \
match ip tos 0x10 0xff flowid 1:10

# ICMP (ip protocol 1) in the interactive class 1:10 so we
# can do measurements & impress our friends:
tc filter add dev $DEV parent 1:0 protocol ip prio 10 u32 \
match ip protocol 1 0xff flowid 1:10

# To speed up downloads while an upload is going on, put ACK packets in
# the interactive class:

tc filter add dev $DEV parent 1: protocol ip prio 10 u32 \
match ip protocol 6 0xff \
match u8 0x05 0x0f at 0 \
match u16 0x0000 0xffc0 at 2 \
match u8 0x10 0xff at 33 \
flowid 1:10

# rest is ‘non-interactive’ ie ‘bulk’ and ends up in 1:20

# some traffic however suffers a worse fate
for a in $NOPRIOPORTDST
do
tc filter add dev $DEV parent 1: protocol ip prio 14 u32 \
match ip dport $a 0xffff flowid 1:30
done

for a in $NOPRIOPORTSRC
do
tc filter add dev $DEV parent 1: protocol ip prio 15 u32 \
match ip sport $a 0xffff flowid 1:30
done

for a in $NOPRIOHOSTSRC
do
tc filter add dev $DEV parent 1: protocol ip prio 16 u32 \
match ip src $a flowid 1:30
done

for a in $NOPRIOHOSTDST
do
tc filter add dev $DEV parent 1: protocol ip prio 17 u32 \
match ip dst $a flowid 1:30
done

# rest is ‘non-interactive’ ie ‘bulk’ and ends up in 1:20

tc filter add dev $DEV parent 1: protocol ip prio 18 u32 \
match ip dst 0.0.0.0/0 flowid 1:20

########## downlink #############
# slow downloads down to somewhat less than the real speed to prevent
# queuing at our ISP. Tune to see how high you can set it.
# ISPs tend to have *huge* queues to make sure big downloads are fast
#
# attach ingress policer:

tc qdisc add dev $DEV handle ffff: ingress

# filter *everything* to it (0.0.0.0/0), drop everything that’s
# coming in too fast:

tc filter add dev $DEV parent ffff: protocol ip prio 50 u32 match ip src \
0.0.0.0/0 police rate ${DOWNLINK}kbit burst 10k drop flowid :1

Script ini bekerja cukup baik pada ADSL tapi harus dicoba-coba sampai didapat nilai optimum untuk nilai di DOWNLINK dan UPLINK. Masalah umum ADSL adalah kecepatan upload yang jauh dibawah kecepatan download, dan karena sifat TCP/IP yang terus mengirim paket sampai akhirnya tidak ada tempat lagi untuk paket, biasanya modem akan hang. Dengan kecepatan dowload yang kencang biasanya user terus mendownload beberapa sites sekaligus sehingga akumulasi upload menjadi besar. Bila traffic upload ini mencapai modem ADSL maka modem akan hang.

Karena itu harus diatur agar traffic upload kita kontrol dan tidak mencapai modem ADSL hal ini dilakukan dengan menurunkan nilai UPLOAD sampai nilai optimum. Hal ini tercapai jika network latency mencapai nilai terendah dan network tidak putus. Lebih jauh silakan baca pada dokumen LARTC Om Bert di atas.


rc.local di openSUSE

Tidak ada rc.local di openSUSE (he..he…he…)
Kalau kita lihat di RedHat (dan cloningnya) rc.local dijalankan setelah semua service selesai dijalankan di run level 5. Ini gak ada padanannya di openSUSE.

User biasanya mengira boot.local di /etc/init.d adalah padanan dari rc.local. Ini adalah perkiraan yang salah karena boot.local akan jalan paling awal sebelum service-service yang lain dijalankan. Sehingga seringkali user membuat script iptables dan disisipkan pada boot.local kemudian komplain karena scriptnya tidak jalan. Ini terjadi karena script iptables dipanggil sebelum service network dikonfigurasi di run level 3 sehingga sudah pasti tidak akan berfungsi.

Di openSUSE kita harus mengetahui pada saat mana script kita harus jalan apa saja syarat yang dibutuhkan, walaupun umumnya akan jalan di run level 3 dan 5. Misalnya kita ingin menjalankan script load balancing di atas, maka sebelum script ini jalan service network harus sudah jalan dulu.

Untuk dasar dari script tersebut kita dapat menggunakan file /etc/init.d/skeleton sebagai dasar, walaupun tidak tertutup kemungkinan menggunakan script lain seperti yang akan saya contohkan.

Script untuk traffic shapper:
#!/bin/sh
#
#
# /etc/init.d/bwshaper_eth0
#
### BEGIN INIT INFO
# Provides: bwshaper_eth0
# Required-Start: $network
# Should-Start:
# Required-Stop:
# Should-Stop:
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Short-Description: Custom shapping using htb for eth0 to ISP-A
# Description: decreased the upload traffic on eth0 to ISP-A by doing queuing using htb,
# script by medwin@gmail.com
### END INIT INFO
#

test -s /etc/rc.status && . /etc/rc.status && rc_reset

case "$1" in
start )

## letakkan script and di sini

rc_status -v
;;
stop)
# ok kita test
;;

esac

rc_exit

# end of script

Script di atas hanya satu contoh sederhana saja. Perhatikan bagian:
### BEGIN INIT INFO
# Provides: bwshaper_eth0 —> ini nama service anda
# Required-Start: $network —> ini adalah service yang harus jalan sebelum script anda di jalankan
# Should-Start:
# Required-Stop:
# Should-Stop:
# Default-Start: 3 5 —> ini run level script anda bekerja
# Default-Stop: 0 1 2 6
# Short-Description: Custom shapping using htb for eth0 to ISP-A
# Description: decreased the upload traffic on eth0 to ISP-A by doing queuing using htb, script by medwin@gmail.com
### END INIT INFO
bagian ini akan dipelajari oleh insserv untuk menjalankan script anda pada run level berapa.
Copy script anda di /etc/init.d
Untuk memasukkan service anda ke service maka daftarkan dengan perintah

> insserv (nama service)

kemudian check dengan

> chkconfig –list

untuk mengetahui kalau service anda sudah masuk ke daftar service di run level tertentu.

Anda juga bisa berkreasi dengan membuat script service yang bisa dijalankan dan diberhentikan seperti misalnya dengan menyisipkan

case "$1" in

start)

echo -n "Starting bandwidth shaping on eth0: "
start
echo "done"
;;

stop)

echo -n "Stopping bandwidth shaping on eth0: "
stop
echo "done"
;;

restart)

echo -n "Restarting bandwidth shaping on eth0: "
restart
echo "done"
;;

status)

echo "Bandwidth shaping status for $IF:"
show
echo ""
;;

*)

pwd=$(pwd)
echo "Usage: tc.bash {start|stop|restart|status}"
;;

pada script anda. Kemudian melakukan symbolic link file tersebut ke /usr/sbin atau /sbin, misalnya dengan nama rcbwshaper_eth0

> ln-s /etc/init.d/bwshaper_eth0 /usr/sbin/rcbwshaper_eth0

sehingga anda bisa memanggilnya dengan perintah

> rcbwshaper_eth0 {start|stop|restart|status}

OK selamat mencoba.
Till then keep safe and stop global warming.

Comments

Popular posts from this blog

Prejudice and Privilege

Notes : It is not about Linux or other geeky stuff nor it is a political writing.  It was a day in the end of March 2007. I was just landed at the Franz Josef Strauss Munich Airport  around 10 AM in the morning. I had 5 days free time from my work in Astrium. At that time I was contracted by EADS Astrium  (now become Airbus Defense and Space) to work with them in Toulouse . I worked for one of their project. I flew from Toulouse where I worked to visit my brother family in Munich. Just after I picked up my luggage from the conveyor, three guys without uniform approaching me and asked me in English what i'm doing in Munich. I asked them if I did anything wrong. One of them told me that it was a random checked.     "Who are you guys? Sorry sir if it is a random check, why do you choose me instead of other?" I reply to their answer.  One of them said they're from the Munich immigration, and at the same time showing a gun behind his jacket. For my itinerary Munich was not

Asterisk 1.6.1 on openSUSE 11.1 (Part 2)

In this second part I will explain step-by-step configuration to use our appliance to build an Asterisk PABX server. Without further ado, here is the list: Install the Digium card on the PCI slot Install our appliance. You can also use any linux distribution, download asterisk from its website and install it. There are several softwares I forget when I made the appliance, it is not the mandatory (dependencies) but they are useful when we want to use asterisk optimally. They are: mpg123, sox, libmad, and festival. The easiest way to install it in openSUSE is using zypper. Check it first where they reside in repositories and add the repositories accordingly. mpg123 and sox are in the packman repositoriy, libmad in OBS (please check with webpin) and festival in oss. Then as root run: "zypper install mpg123 sox libmad0 festival". It is always useful to update your installation to update repository, to make sure that all the security update is up to date. Download the

In Search of Manohara

Notes: I always write note in my laptop and put it somewhere either on disk or cloud when I think there is an interesting experience. I found this 7 years old note when I try to clear some space in my disk, and before I wipe it out from my disk I think it is an interesting story to share with you. This happened in 2011, at that time I was working to build Linux based computer labs for 500 elementary and junior high schools in Yogyakarta Indonesia. No it is not about computer and other geeky stuff. So here it is.  Well, it is very out of topic everyone, you've already been warned 😁 During my busy weeks in implementing openSUSE Li-f-e for schools in Yogyakarta Indonesia (see the pictures below), me and one of my colleague use our free time in a Sunday afternoon to visit Borobudur , the biggest Buddhist temple in the world. One of the World Heritage preserve by Unesco. 85% of Indonesia population are Moslem and this make Indonesia the biggest Moslem country in the world. We