Print
Parent Category: Tutorials
Hits: 10253

In this tutorial i'll show you how setup Linux QoS in order to improve quality Asterisk VoIP calls.

First let's see how the default Linux prioritization looks:

root@Hercule:~# tc qdisc show dev eth0
qdisc pfifo_fast 0: root bands 3 priomap 1 2 2 2 1 2 0 0 1 1 1 1 1 1 1 1
root@Hercule:~#

As you can see the current policy is FIFO and it contains 3 bands named from 1 to 3.

Let's look at the priomap argument. It contains 16 numbers from 0 to 2.

The link between  band and priomap numbers is like this:

band 1 takes the priomap 0

band 2 takes the priomap 1

band 3 takes the priomap 2

Those 16 numbers from 0 to 2 meaning bits.

Below you have a Table with Linux TOS values :

TOS Bits Means Linux Priority Band
0x0 0 Normal 0 Best Effort 1
0x2 1 Minimize Monetary Cost 1 Filler 2
0x4 2 Maximize Reliability 0 Best Effort 2
0x6 3 mmc+mr 0 Best Effort 2
0x8 4 Maximize Throughput 2 Bulk 1
0xa 5 mmc+mt 2 Bulk 2
0xc 6 mr+mt 2 Bulk 0
0xe 7 mmc+mr+mt 2 Bulk 0
0x10 8 Minimize Delay 6 Interactive 1
0x12 9 mmc+md 6 Interactive 1
0x14 10 mr+md 6 Interactive 1
0x16 11 mmc+mr+md 6 Interactive 1
0x18 12 mt+md 4 Int. Bulk 1
0x1a 13 mmc+mt+md 4 Int. Bulk 1
0x1c 14 mr+mt+md 4 Int. Bulk 1
0x1e 15 mmc+mr+mt+md 4 Int. Bulk 1

 

Here you have the linux TOS:

# cat /etc/iproute2/rt_dsfield
0x00 default
0x10 lowdelay
0x08 throughput
0x04 reliability
# This value overlap with ECT, do not use it!
0x02 mincost
# These values seems do not want to die, Cisco likes them by a strange reason.
0x20 priority
0x40 immediate
0x60 flash
0x80 flash-override
0xa0 critical
0xc0 internet
0xe0 network
# Newer RFC2597 values
0x28 AF11
0x30 AF12
0x38 AF13
0x48 AF21
0x50 AF22
0x58 AF23
0x68 AF31
0x70 AF32
0x78 AF33
0x88 AF41
0x90 AF42
0x98 AF43

The "prio" qdisc now will dequeue packets from class 0 with the
highest priority. The higher the class numbers the lower the de-
queue priority.

If you would like to send packets with a TOS value of 0x0, 0x2, 0x4,
0x6, 0x8 to class 1 and packets with all other TOS values to class 0
you could change your priomap such (you have to state it when setting
up the "prio" qdisc if you want to have something different from the
default priomap):

1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0

Here is an example script that i use with Asterisk:

#!/bin/bash

# Flush any existing queues
for dev in $devices ; do
tc qdisc del dev $dev root 2> /dev/null > /dev/null

# Band 3 has priomap 2, band 2 has priomap 1 and band 1 has priomap 0
#tc qdisc add dev $dev root handle 1: prio priomap 1 2 2 2 1 2 0 0 1 1 1 1 1 1 1 1
tc qdisc add dev $dev root handle 1: prio priomap 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 0
# Put all in band 3:
#tc qdisc add dev $dev root handle 1: prio priomap 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
tc qdisc add dev $dev parent 1:1 handle 10: sfq limit 3000
tc qdisc add dev $dev parent 1:2 handle 20: sfq
tc qdisc add dev $dev parent 1:3 handle 30: sfq

#Adding traffic in band 1
#Currently VoIP TOS defined in /etc/asterisk/sip.conf:
#tos_sip=cs3                    ; Sets TOS for SIP packets. cs3=0x60
#tos_audio=ef                   ; Sets TOS for RTP audio packets. ef=0xb8
#;tos_video=af41                 ; Sets TOS for RTP video packets.
#VoIP audio packets with defined TOS mark:
tc filter add dev $dev protocol ip parent 1: prio 1 u32 match ip tos 0xb8 0xff flowid 1:1
#tc filter add dev $dev protocol ip parent 1: prio 1 u32 match ip dport 4569 0xffff flowid 1:1
#tc filter add dev $dev protocol ip parent 1: prio 1 u32 match ip sport 4569 0xffff flowid 1:1
tc filter add dev $dev protocol ip parent 1: prio 1 u32 match ip dport 16384 0xffff flowid 1:1
tc filter add dev $dev protocol ip parent 1: prio 1 u32 match ip sport 16384 0xffff flowid 1:1

#Adding traffic in band 2
#SIP packets with definied TOS mark :
tc filter add dev $dev protocol ip parent 1: prio 1 u32 match ip tos 0x60 0xff flowid 1:2
#tc filter add dev $dev protocol ip parent 1: prio 1 u32 match ip dport 5060 0xffff flowid 1:2
#tc filter add dev $dev protocol ip parent 1: prio 1 u32 match ip sport 5060 0xffff flowid 1:2
#Adding tos 0x10 (Minimize Delay) in band 2:
tc filter add dev $dev protocol ip parent 1: prio 1 u32 match ip tos 0x10 0xff flowid 1:2

Note:

We use Hosting and VPS Hosting, from: www.star-host.org

We like and trust them.

Good prices, high security.