Asterisk, OpenVPN and QoS

Installing a VoIP system is nowadays an easy task, just install Asterisk, have a few SIP clients and you have an ‘instant’ telephone system. But your system will not be as reliable as the one offered by any telecom company. Why? Quality of Service, or for short, QoS.

Telecom companies use sophisticated hierarchies of systems to deliver the needed QoS. Backbones uses SDH systems, where one can guarantee the bandwidth and throughput for any kind of data. So if you specify that a voice packet should be delivered in 10ms, it will get delivered in that time span. Now when it comes to IP networks, you have no guarantee that your packet will be delivered in that time frame, which is a good thing when you’re downloading files, opening web pages, and so on. But when it comes to voice and video streaming, it’s a real mess. So you must create some QoS rules for your packets.

Asterisk has this real nice feature for aggregating multiple servers so that it works as a single phone network. The only problem is that this feature is not really secure, so as to mitigate that, one can always create VPN’s (Virtual Private Networks). But how does that impact your QoS solution? Well, depends on what kind, or how you configure your VPN, with OpenVPN it’s quite simple.

Just as a reminder, for the rest of the article when I say QoS, I really mean the QoS of the gateway of your network. The gateway is the one place that will enforce the needed quality of service (okay, on bigger networks you will have multiple routers which will need to be configured for QoS too).

Don’t get too excited with QoS, even though you did everything by the book that doesn’t mean that your ISP will use TOS field the same way you did. By that I mean, you won’t solve any problem with QoS if the problem is not on how you route packets to the internet. If you have full control of your link and all the router in between your networks, you’re a lucky guy!

The Network

We have some computers, servers, and IP phones on each network. The OpenVPN tunnel server doesn’t need to be the same as the gateway, as long as you export the correct ports to that server. Make sure you also add the correct gateway for the packets that should be tunneled (ie. packets for the network that originates on the network). On the image, the tunnel is represented by the red lines.

A sample network using Asterisk and OpenVPN


I don’t intend to give a full how-to on OpenVPN, just a basic configuration, with a highlight on how to get QoS for the tunneled packets. Besides that, configuring OpenVPN is really simple.

First you have to create your own Certificate Authority (CA). You can use something like tinyca or minica, or the command line version, described here. Remember that you will need one certificate per client. After that is just a matter of writing a really simple text file. Below are a sample configuration, known to work well integrating two Asterisk servers.


# OpenVPN server
# Listen to local ip address only

# Should be exported on the router
port 1194
proto udp
dev tun

# SSL/TLS CA and keys
ca ca.crt

# Diffie Hellman Parameters
dh dh1024.pem

# Server tunnel
ifconfig-pool-persist ipp.txt

push "route"
client-config-dir client-configs
keepalive 10 120

# Drop privileges
user nobody
group nogroup

# Persist

# Logs
verb 5
status /var/log/openvpn.log

# Fork to the background


The highlighted line is the one which will make the QoS work for the encrypted packets. If you think that passing the TOS (Type of service) is a security fault, don’t panic, just create another tunnel for passing your sensitive data – and that’s really easy to do with OpenVPN.

# OpenVPN client

# Interface for tunnel
# Protocol and Port
dev tun0
proto udp
port 1194

# SSL/TLS CA and keys
ca /etc/openvpn/certs/ca.crt
cert /etc/openvpn/certs/remote1.mynetwork.crt
key /etc/openvpn/keys/remote1.mynetwork.key

# Symmetric cipher
cipher BF-CBC

# Remote server to connect to. Can be domain name or IP address.

# Check if the tunnel went down and restart it. 
# 10 is the ping interval number and 120 is the timeout to restart.
keepalive 10 120

# This is need so we can apply QoS to the tunnel

# Drop privileges
user nobody
group nogroup

# Use a persistent key and tunnel interface.

# Log to file instead of syslog
log-append /var/log/openvpn.log
verb 4

# Fork to the background

If you can ping the remote server, using the internal IP address, then your tunnel is up and running.


I suppose that you already know how to configure an Asterisk server, if you don’t you can follow my guide (it’s a bit outdated, I might update it soon).

Getting IAX2 working is really simple too, so I won’t describe it. If you’re using FreePBX, you can follow this guide. Remember to use the internal IP’s from your network.

Make sure your asterisk installation is tagging the correct TOS for the packets. On my FreePBX install it already had the correct configuration set on /etc/asterisk/sip_general_additional.conf. Check your asterisk configuration for the following lines:


This tags your voice data as Expedited Forwarding, normal SIP packets get Class Selector 3 and video data gets Assured Forwarding, Class 4, with drop precedence 1. More on what all this means shortly.


Getting the right choice of tools for your specific QoS application is a hard problem. You can have some traffic shaping algorithms, congestion avoidance mechanisms and quite a few packet scheduling algorithms. I’m not an expert on how all these different types of algorithms work, or what is the best solution for your case. I’m just putting together some information that I think is relevant. One can always read all the RFC’s about QoS.

First things first, the mentioned TOS field is now called DSCP (Differentiated Services Code Point), it replaces the TOS field and is specified for IPv4 and IPv6 (for reference RFC2474 is the specification). It tries to maintain backward compatibility with the TOS field. Most networks use the following traffic classes:

  • Default PHB — which is typically best-effort traffic
  • Expedited Forwarding (EF) PHB — dedicated to low-loss, low-latency traffic
  • Assured Forwarding (AF) PHB — gives assurance of delivery under prescribed conditions
  • Class Selector PHBs — which maintain backward compatibility with the IP Precedence field.

That is what EF, CS3 and AF41 means, just a common way of signalling that your packet is important, or not that much. But just tagging your packets won’t get you far. For now, you’ve got your Asterisk correctly tagging the packets, and your tunnel to preserve them. Time to add the magic to classify and prioritise the packets!

Linux Traffic Control

Linux has the tc tool for configuring and setting up a QoS policy. With it you can configure different kinds of queueing disciplines and classes. This queues acts directly on net devices, so you have to configure it per device. In the example below we have an ADSL modem on ppp0 device.

TC allows you to configure classful and classless disciples, each one supporting different scheduling algorithms. We will use Hierarchy Token Bucket (HTB) for the classful packets (the ones that got tagged by Asterisk), and Stochastic Fairness Queueing (SFQ) for the classless packets. After getting your queues configured you have to inform iptables that it should use the queue, that’s basically setting up some CLASSIFY targets. You definitely can add some MARK rules to tag your packets, but we don’t need it, Asterisk is doing that job for us.

First we will configure what is the maximum bandwidth allowed, in this case we have an 1000kbps uplink that we want to add a QoS policy. The following table illustrates the QoS policy required for the network. As we are using an asymmetric connection, we will limit the upload bandwidth to 95% of the nominal speed.

Class Nominal rate Maximum rate Priority Packets
Real time 47.5kbps 95kbps 0 ICMP, SYN, RST, ACK
High 522.5kbps 950kbps 1 EF and CS3 packets
Regular 190kbps 950kbps 2 Regular traffic, HTTP, SSH, etc
Bulk 190kbps 950kbps 3  
QoS Policy

With the queues in place you just have to add the necessary iptable rules. The rules will classify the packets that have the DSCP tag using the same classes that you defined using tc. That’s it, your QoS is now in place. Just make sure you add and remove the rules according to the status of your link (in this case ppp0). The script bellow is called by /etc/ppp/ip-up.d and /etc/ppp/ip-down.d, with the start and stop targets respectively.

# !/bin/bash
# 20110916 - Leonardo Santos <leonardo at aligera dot com dot br>
# Initial version. It only uses the iptables target CLASSIFY.
# For the QoS to work, Asterisk has to tag the packets with the right DSCP.
# The OpenVPN tunnel must be passing along the DSCP field, and not blanking it out.

# uplink in kbps



do_iptables() {
        iptables -$1 POSTROUTING -t mangle -p icmp -j CLASSIFY --set-class 1:$CLASS_RT
        iptables -$1 POSTROUTING -t mangle -p tcp -m tcp --tcp-flags SYN,RST,ACK SYN -j CLASSIFY --set-class 1:$CLASS_RT
        iptables -$1 POSTROUTING -t mangle -p udp -m dscp --dscp-class cs3 -j CLASSIFY --set-class 1:$CLASS_HIGH
        iptables -$1 POSTROUTING -t mangle -p udp -m dscp --dscp-class ef -j CLASSIFY --set-class 1:$CLASS_HIGH
add_rules() {
        tc qdisc add dev $DEV root handle 1: htb default $CLASS_BULK
        tc class add dev $DEV parent 1: classid 1:1 htb rate ${CEIL}kbit ceil ${CEIL}kbit
        tc class add dev $DEV parent 1:1 classid 1:$CLASS_RT   htb rate $((1*$CEIL/20))kbit  ceil $(($CEIL/10))kbit prio 0
        tc class add dev $DEV parent 1:1 classid 1:$CLASS_HIGH htb rate $((11*$CEIL/20))kbit ceil ${CEIL}kbit       prio 1
        tc class add dev $DEV parent 1:1 classid 1:$CLASS_REG  htb rate $((4*$CEIL/20))kbit  ceil ${CEIL}kbit       prio 2
        tc class add dev $DEV parent 1:1 classid 1:$CLASS_BULK htb rate $((4*$CEIL/20))kbit  ceil ${CEIL}kbit       prio 3
        tc qdisc add dev $DEV parent 1:$CLASS_HIGH handle 120: sfq perturb 10
        tc qdisc add dev $DEV parent 1:$CLASS_BULK handle 130: sfq perturb 10
        do_iptables A
del_rules() {
        tc qdisc del dev $DEV root
        do_iptables D
show_status() {
        tc -s -d class show dev $DEV
        tc -s -d qdisc show dev $DEV
case $1 in
                echo "Usage: $0 {start|stop|restart|status}"
                exit 1

I would like to thank Leonardo Santos for putting the script together and letting me publish it, and for being a good friend.

Leave a Reply

Your email address will not be published. Required fields are marked *