Controlling bandwidth with Wondershaper

Get in Shape


Traffic shaping helps you optimize bandwidth and keep the data flowing. Wondershaper makes this extremely complex technology available to normal users.

By Christoph Langner

Andres Rodriguez, 123RF

When multiple users rely on the same Internet connection, large-scale uploads can seriously affect the download speed. Just imagine somebody at work uploading their whole holiday snapshot collection or running a peer-to-peer client like eDonkey or BitTorrent. All this uploaing spoils the fun of surfing the web for other users. Wondershaper lets you shape the network traffic for a more agreeable allocation of bandwidth.

Many Internet service packages offer upload rates that are much slower than the corresponding download rate. ADSL lines, for instance, often provide an upload data rate 10 to 20 times slower than the download rate. In many environments, this asymmetry of data rates isn't a problem because most people download much more than they upload. But if the upload channel is maxed out for an extended period of time, the download channel will suffer, too.

To understand why this should be so, it is important to understand how the Transmission Control Protocol (TCP) works. TCP requires the receiver of a data packet to confirm that the packet has arrived in good shape, and the source will not send the next data packet until this confirmation has arrived. If the upload channel is overloaded, the SYN/ACK packet will just sit at the back of the queue of data packets waiting to be transmitted. This prevents the source from sending more data packets until the SYN/ACK packet has been transmitted and received at the source end.

Traffic shaping [1] makes it possible to manipulate the sending order for data packets. Most Linux distributions have the necessary tools in the form of a userspace program called Tc (which is short for "traffic control"). Unfortunately, Tc is so complex that you have to write a script that divides the network traffic into classes and sets individual priorities. Wondershaper [2] substantially reduces the complexity of traffic shaping.

The Wondershaper script assigns network traffic to classes, preferring data packets from interactive applications such as VoIP or Internet TV. By prioritizing traffic according to class, Wondershaper avoids traffic delays that users would perceive as interference to voice communications or video images. If the line is busy, Wondershaper puts packets that can wait to be transmitted - such as FTP-based data transfers - at the back of the queue. Network engineers refer to this as "quality of service," or QoS.

How It Works

ISPs tend to optimize their services for fast download, and they use large upload and download queues to improve reliability and reduce packet loss. According to the Wondershaper documentation, "These large queues ... damage interactivity. A keystroke must first travel the upstream queue, which may be seconds long, and go to your remote host. It is then displayed, which leads to a packet coming back, which must then traverse the downstream queue, located at your ISP, before it appears on your screen."

The key to shaping and prioritizing network traffic is to get some control over the upload and download queues, but because these queues reside on the ISP's network, they are almost always out of reach to users. Wondershaper eliminates the problem of an inaccessible upload queue by operating just under the available data transfer rate. If the actual flow of packets stays below the rate provided by the ISP, a queue does not build up on the ISP's equipment, and the queue essentially moves from the ISP router to your own router, where you have a greater degree of control.

In the Readme file available with the Wondershaper script, developer Bert Hubert writes that the task of managing download speed "... is slightly trickier, as we can't really influence how fast the Internet ships us data. We can, however, drop packets that are coming in too fast, which causes TCP/IP to slow down to just the rate we want. Because we don't want to drop traffic unnecessarily, we configure a burst size that we allow at a higher speed. Now, once we have done this, we have eliminated the downstream queue totally (except for short bursts) ... ."

With these techniques, Wondershaper is able to:

Of course, Wondershaper is a script that operates on a Linux system. The most common scenario is to deploy Wondershaper on a Linux box that is operating as a router/firewall connecting a small network with an Internet service line. Also, you could use Wondershaper on a device like the Linksys WRT54GL [3] with an alternative firmware system such as OpenWrt [4] or DD-WRT [5].

Getting Started

To get started with Wondershaper, download the package from the project homepage [2] and unpack it to a directory of your choice. Wondershaper is a script, so you do not need to install.

Before you start configuring, you should check the ping response times to see if Wondershaper's settings are having any effect. The test only returns meaningful results when you reach the maximum amount of upload traffic; to provoke this state, you could upload a large file to your own web space or send an email message with a large attachment. While doing so, ping your gateway and monitor the response times (Listing 1). The gateway IP address and the network interface you use to reach the Internet are easily ascertained with the route -n command.

Listing 1: route and ping
01 $ route -n
02 Kernel IP Routing Table
03 Target         Router   Genmask         Flags [...] Iface
04 88.66.0.1    0.0.0.0  255.255.255.255 UH    [...] ppp0
05 192.168.0.0  0.0.0.0  255.255.255.0   U     [...] eth0
06 169.254.0.0  0.0.0.0  255.255.0.0     U     [...] eth0
07 0.0.0.0      0.0.0.0  0.0.0.0         U     [...] ppp0
08
09 $ ping 88.66.0.1
10 PING 88.66.0.1 (88.66.0.1) 56(84) bytes of data.
11 64 bytes from 88.66.0.1: icmp_seq=1 ttl=255 time=594 ms
12 [...]

Although response times of 10 milliseconds (msec) are typical if the line is free, the time will grow to more than 500msec if the upload channel is busy. Web pages will take longer to update, and VoIP is impossible. When you enable Wondershaper, make sure you use the right network device (ppp0, eth0, ...). In our case, this is ppp0. The script is generically launched with the following command line:

$ sudo wondershaper interface_name download_rate upload_rate

The transfer rates are specified in kilobits per second (Kbps), as in wondershaper ppp0 15000 900 for a 16Mbps line. For a 2Mpbs connection, set it to 2000 - and you should always leave a safety margin of a couple of percent to the theoretical maximum value, which also applies to the upload bandwidth - then run the ping test again. If ping times do not improve, reduce the upload value again until response times are acceptable. In our lab, the settings started working at a setting of 15000 (Kbps) and an upload bandwidth of 600 (Kbps) (Listing 2). At these settings, we could surf the web despite stressing the upload channel with a large upload.

Listing 2: Finding the Balance
01 $ sudo wondershaper ppp0 15000 900
02 $ ping 88.66.0.1
03 PING 88.66.0.1 (88.66.0.1) 56(84) bytes of data.
04 64 bytes from 88.66.0.1: icmp_seq=2 ttl=255 time=520 ms
05 [...]
06 $ sudo wondershaper ppp0 15000 600
07 $ ping 88.66.0.1
08 PING 88.66.0.1 (88.66.0.1) 56(84) bytes of data.
09 64 bytes from 88.66.0.1: icmp_seq=1 ttl=255 time=7.25 ms
10 [...]
Options

Wondershaper provides a number of optional settings to help you fine tune traffic flow:

  • NOPRIOHOSTSRC - defines hosts or netmasks on your network that should have low priorities.
  • NOPRIOHOSTDST - defines hosts or netmasks on the Internet that should have low priorities.
  • NOPRIOPORTSRC - defines source ports on your network that should have low priorities.
  • NOPRIOPORTDST - defines destination ports on the Internet that should have low priorities.

See the documentation at the Wondershaper website for more on using these options to tune up traffic on your network.

Autostart

To inform Wondershaper that it is to launch automatically when a DSL connection is established, you need to add the following command line to your /etc/ppp/ip-up.local file:

# Set Wondershaper
wondershaper ppp0 15000 600

Alternatively, you can modify the /etc/network/interfaces file, where you would use the up and down commands to launch Wondershaper. In Listing 3, the script manipulates the bandwidth for the eth0 network interface. See the Readme file and the man page for more useful tips.

Listing 3: /etc/network/interfaces
01 # The primary network interface
02 auto eth0
03 iface eth0 inet dhcp
04 up /sbin/wondershaper eth0 15000 600
05 down /sbin/wondershaper clear eth0

Checking for Effectiveness

To test to see whether or not your bandwidth controls are effective, run a speed test, such as that offered by the Speedtest.net [6] site. In our lab scenario, downloads were more or less impossible before the traffic shaper was switched on; despite a 16Mbps line, the download speed dropped to 0.28Mbps. After starting Wondershaper, the value rocketed to 7.50Mbps (Figure 1). Even when the line was free, the download speed improved slightly at the cost of a slightly lower upload speed.

Figure 1: Wondershaper helps the normal download experience survive an upload event.

Conclusions

Although you can't expect Wondershaper to work miracles and free up an overloaded upload channel, it is effective at preventing uploads from affecting download speeds. This ability makes Wondershaper an extremely useful addition to any environment where multiple users share an Internet connection.

INFO
[1] Traffic shaping: http://en.wikipedia.org/wiki/Traffic_shaping
[2] Wondershaper: http://lartc.org/wondershaper/
[3] Linksys WRT54GL: http://www.linksysbycisco.com/US/en/products/WRT54GL
[4] OpenWrt: http://openwrt.org
[5] DD-WRT: http://www.dd-wrt.com/dd-wrtv3/index.php
[6] Speed test: http://speedtest.net
THE AUTHOR

Christoph Langner works in test management for PTV in Karlsruhe, Germany. He has been an active member of the open source software scene for many years. You can read his GNU/Linux-related blog (in German) at http://linuxundich.de.