Traffic shaping with OpenWRT

This post will explain how to do traffic shaping with openwrt. It’s using a script which you can easily customize to your own needs.

What is traffic shaping?

Traffic shaping is slowing down certain packets.

Why slowing packets down? I want to go as fast as possible!

On a typical network you have different types of traffic f.e.

  • web browsing
  • smtp traffic for sending/receiving emails
  • bittorrent
  • voip
  • ssh

Some types af traffic require a lot of bandwidth. Other types of traffic require a small amount of bandwidth but require a low latency. By shaping certain traffic you can create room for other traffic. Without shaping, your openwrt internet router would behave like a brainless fifo queue. With a correctly configured traffic shaper, your openwrt internet router will intelligently prioritize packets based on a number of criteria.

Why shaping traffic?

I started doing traffic shaping because I noticed problems with call quality using voip phones. I’m using Siemens Gigaset C610 IP. These call quality problems happened during whenever backups where sent to a cloud provider. While it’s important to take regular backups, I didn’t want those backups to have an influence on my voip traffic. At all times, the voip traffic needs a higher priority then backups.

Howto to start traffic shaping with OpenWRT?

In order to start traffic shaping with OpenWRT, you have to install some packages.

Install required packages

# opkg update
# opkg install tc kmod-sched

Install script

You can download my script here.
Please make sure to edit the variable on top of the file.
Note that I started ‘The ultimate traffic confitioner’ from lartc.org

Run script at boot time

Add this line to /etc/rc.local . Make sure you insert the line before the ‘exit 0’.

sh /etc/shape.sh

Future post…

In a future post I’ll show what I gained with traffic shaping with openwrt.
Traffic shaper benchmark

 

Update on 2013/05/03 : added kmod-sched to the list of packages to install
Update on 2013/05/08 : added link to benchmark post.

21 thoughts on “Traffic shaping with OpenWRT

    1. Tom

      When running this script I’m receiving the following errors: “File exists” for insmod sch_ingress & cls_u32 ( I’m guessing those aren’t needed in the script then), “RTNETLINK answers: No such file or directory”, “RTNETLINK answers: Invalid argument”, “‘burst’ requires ‘rate'” & “Illegal ‘police'”. This is via the latest trunk image for my WNDR3700 with the ‘tc’ module installed.

      Any idea why this script isn’t functioning as intended?

      Reply
      1. Thomas Elsen Post author

        You can ignore the warnings from insmod. You get these when the modules are already loaded.

        Regarding the other errors: I’ll install the latest trunk to test the script again and let you know.
        Best regards

        Reply
        1. Tom

          Reflashing to Backfire 10.03.1 solved a lot of issues, apparently the ingress/egress queuing disciplines were not compiled in the trunk image’s kernel. I get no insmod errors now, however the lines “$TC qdisc del dev $DEV root” and “$TC qdisc del dev $DEV ingress” replied with RTNETLINK errors, and it also seems the ingress portion of this script is non-functional. Furthermore, under rc.local, the line should be “sh /etc/shape.sh” correct?

          Any idea why downlink shaping isn’t occurring?

          Reply
          1. Tom

            Because VoIP uses the UDP protocol I changed “match ip tos from 0x88 0xff” to “match ip protocol 17 0xff” (/etc/protocals lists UDP as 17).

            Still no idea why downlink throttling is not occurring but uplink shaping is better than nothing.

          2. Thomas Elsen Post author

            Tom, the Siemens VOIP phones that I used are sending out SIP traffic with a TOS field with 0x88. That’s why I hard coded that value in my script. I don’t want to give priority to all UDP traffic on my network. Especially since I’m using OpenVPN to pull/push backups to/from servers on the internet. All that VPN traffic would be UDP as well.

          3. Thomas Elsen Post author

            I’ve added kmod-sched to the list of packages to install. That should fix the downlink throttling.

            Thanks for your feedback.

  1. Pingback: Traffic shaper benchmark | Thomas Elsen Security Blog

  2. Pieter-Bas

    Hi, thanks for the explanation. I’d like to do traffic shaping for exactly the same reason as you are.

    I’m connected by FTH and I have a synchrone bandwith. Can you just explain me the ratio settings. 1:10 or 1:20. Is this a ratio or what is it exactly?

    Then another question: with synchrone bandwith, can I use this script, or should I use something else? Since download shaping is important to me as well and in your script, download shaping is only policed using a tc filter containing a Token Bucket Filter.

    thanks in advance,
    Pieter-Bas

    Reply
    1. Pieter-Bas

      Okay, I found the answer to my 2nd question:
      “You do NOT have the same level of control over download traffic, i.e. traffic arriving at your router from the Internet. Here, you can only drop packets but not rearrange them.
      The dropping of TCP packets, causes the sending site to reduce it’s transmission rate, the dropping of UDP packets however, will only help to keep the buffer empty.”

      Source: http://wiki.openwrt.org/doc/howto/packet.scheduler/packet.scheduler

      Reply
  3. Mario

    Hi, thanks for sharing.

    A stupid question: if I just want to limit bandwidth usage I would only have to set variables UPLINK and DOWNLINK accordingly, right?

    I have a dedicated router that broadcasts an open separate guest network that I want to cap at specific speed limits.

    Reply
  4. Mario

    Of course, I would use the generic script ‘The ultimate traffic confitioner’ that you’ve mentioned, since I don’t want to prioritize any specific application…

    Reply
  5. Denis Tunjic

    Hi i just added the script this way…

    vi /etc/init.d/shape.sh

    pasted your script….

    then saved it

    then i enabled start at boot by

    /etc/init.d/shape.sh enable

    is this right….

    also i get an error cant find device eth1

    im using 12.09 Attitude Adj

    with a tplink wdr4300

    and help with this would be great…

    will this script work where i am uploading with torrents and when i want to stream youtube or music that it will stop the buffering…???

    Thanx Heaps

    Denis

    Reply
    1. Thomas Elsen Post author

      Hi Denis,

      First check what your external interface is. ‘ifconfig’ will list all the interfaces.

      If you have it, change all occurences of eth1 with your external interface in ‘/etc/init.d/shape.sh’.

      Best regards,
      Thomas

      Reply
  6. Hans Van Den Heuvel

    Hi I followed your guide , but when I started benchmarking nothing happened , when I tried to run the shape.sh file with terminal , terminal didnt found it .
    Can you help me pls

    Best regards,
    Hans

    Reply
  7. Ryan

    I’d like to tweak this to work differently depending on time of day, as my ISP offers unlimited bandwidth but throttles my connection for a four hour period every night to be able to offer this. Cron would work here, just running the script before and after the throttling period but it would work better if the script checked the time and then set the bandwidth figures based on that – How would you implement that?

    Reply
    1. Thomas Elsen Post author

      I’d right 2 scripts with different values and run them using cron at the beginnning of each period. In my opinion, it’s easier to let cron handle the timing instead of the script.

      Reply
  8. shani

    I am having trouble starting shape.sh
    I am using openwrt KAMIKAZE (8.09.2, r18961)

    here is log:
    root@OpenWrt:/#
    root@OpenWrt:/# [J
    root@OpenWrt:/# /etc/shape.sh start[J
    insmod: cannot insert ‘/lib/modules/2.6.26.8/sch_htb.ko’: invalid parameters (17): Invalid argument
    insmod: cannot insert ‘/lib/modules/2.6.26.8/sch_sfq.ko’: invalid parameters (17): Invalid argument
    insmod: cannot insert ‘/lib/modules/2.6.26.8/sch_ingress.ko’: invalid parameters (17): Invalid argument
    insmod: Loading module failed: No such file or directory
    insmod: cannot insert ‘/lib/modules/2.6.26.8/cls_u32.ko’: invalid parameters (17): Invalid argument
    root@OpenWrt:/#

    Reply

Leave a Reply

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