Implementing QoS for SIP and RTP packets sent by Asterisk

Asterisk is often made to share its Internet connectivity with other devices, and by default Linux routes traffic First-In/First-Out (FIFO). When other devices on the network utilize bandwidth this increases latency and jitter on the voice traffic, affecting call quality.
Implementing QoS by setting TOS flags on the voice packets and prioritizing these packets over regular data on the router significantly reduces this latency and jitter.
  1. Firstly, Asterisk must be configured to set the correct TOS flags on SIP and RTP packets. Add the following lines to the [general] section of sip.conf and reload Asterisk:
    # nano /etc/asterisk/sip.conf

    [general]
    tos_sip=cs3
    tos_audio=ef
    

    # /etc/init.d/asterisk reload
  2. Next, on the router we must use the iproute2 tc tool to configure the Linux kernel to queue packets based on their TOS flags, and to prioritize the packets we've had Asterisk flag for us. Replace eth1 with your external network interface if necessary:
    # tc qdisc add dev eth1 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 eth1 parent 1:1 handle 10: sfq limit 3000
    # tc qdisc add dev eth1 parent 1:2 handle 20: sfq
    # tc qdisc add dev eth1 parent 1:3 handle 30: sfq
    # tc filter add dev eth1 protocol ip parent 1: prio 1 u32 match ip tos 0x60 0xfc flowid 1:1
    # tc filter add dev eth1 protocol ip parent 1: prio 1 u32 match ip tos 0xb8 0xfc flowid 1:1
    
  3. To view statistics, run the following command:
    # tc -s qdisc ls dev eth1
Voice traffic leaving the local network will now be prioritized. However, TOS flags must also be set on packets sent by each hardphone and any other switches or routers must be configured to route these packets correctly in order to prevent internal network traffic impacting call quality.
These settings are not persistent, i.e. they need to be loaded at each boot. I added the tc commands to a bash script, made it executable, and placed it in /etc/network/if-pre-up.d/.

Comments