Managing Network Bandwidth

Trickle

Photo by Thom Holmes on Unsplash

Photo by Thom Holmes on Unsplash

Author(s):

The trickle command-line utility helps you shape network traffic.

Special Thanks: This article was made possible by support from Linux Professional Institute

If you have ever been in a situation where one application ate all your bandwith, then you will appreciate the trickle bandwidth shaper application. With trickle, you can control an application's upload and download speeds so that no single application hogs all the bandwidth. For example, you can ensure that downloading from Firefox doesn’t interfere with downloading a file from FTP.

Shaping bandwidth traffic is one of the mundane but essential tasks of a system administrator, be it in a large corporate setting or a small office. trickle enables you to control the network traffic rate on a per-application basis, as opposed to per-user control (the most familiar type of bandwidth shaping in a client-server environment).

trickle is available in the official repositories of most RPM- or deb-based distributions. For RHEL-based rollouts, including CentOS servers, you’ll first have to install and enable the Extra Packages for Enterprise Linux (EPEL) repository before pulling in trickle.

trickle regulates speeds by delaying the data transferred over a socket. trickle provides an alternate version of the BSD socket API, causing socket calls to be handled now by trickle. Speed is limited by controlling the amount of the data written or read from a socket.

A word of warning, trickle has some limitations. It only works on TCP connections, so you can’t use it to regulate UDP stream connections (to learn more about dynamic linking and the technical details behind trickle, see the trickle technical paper). Additionally, trickle doesn’t work with all TCP connections either. Since trickle uses dynamic linking and loading, it can only work with applications that use dynamic libraries (specifically glibc). In other words, trickle will not work with statically linked applications or binaries with the SUID or SGID bits set, because it uses dynamic linking and loading to place itself between the shaped processes and their associated network socket -- trickle acts as a proxy between these two components.

In order to determine whether or not you can use trickle, use the ldd command (Figure 1), which lists the dynamic dependencies of all the shared libraries. Specifically, look for the presence of the GNU C library (glibc) in the list of dynamic dependencies for any given application, since this library defines the system calls that are invoked when communicating via sockets.

Figure 1: Use the which command to find the path of the application that you wish to check with ldd. For example, which apt will report back with /bin/ rpm.

For example, if you’re wondering whether trickle will work with the cURL downloader, use the following command:

$ ldd /usr/bin/curl | grep libc.so

This command should return a result resembling:

libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fc532022000)

You can similarly test for other network applications. As long as you get a result, trickle can regulate the speeds for those applications. If the command doesn’t print a result, it means that the binary it was run against does not use libc; thus trickle cannot be used as bandwidth shaper in that instance. As shown in the example, trickle can regulate bandwidth for both graphical and command-line apps and utilities. This means you can use it to throttle the Transmission BitTorrent downloader just as easily as the command-line based Apt and DNF package managers.

Using trickle

When using trickle, you can choose appropriate download and upload speeds to limit applications. You don’t have to do anything fancy – just use the -d and the -u switches to tell trickle the respective download and upload speeds for an application.

For instance, the command

trickle -d 1024 wget -c 
  https://archive.org/download/CharlieChaplin/Chaplin_512kb.mp4

will download the Charlie Chaplin movie (available as public domain from the Internet Archive) with the Wget downloader limiting the download speed to 1024Kbps. (I’ve used Wget here to illustrate trickle’s ability – you can use Wget’s --limit-rate option to regulate its download speed.) In the same vein, use the -u switch to regulate the upload speed.

If setting per-application speeds seems too much of a chore, you can set global speed caps with the trickled command, such as

trickled -d 2048 -u 1024

trickled runs trickle in the daemon mode, allowing you to define download and upload limits to be shared by all the applications run through trickle without the need for specifying limits each time. The trickled daemon monitors all connections; once you’ve set up trickled, you don’t need to bother setting speeds for Wget, FTP, browsers ,and everything else that uses TCP. So,

trickle apt upgrade

will now update the Ubuntu installation while respecting the limits set by the trickled.

You can similarly use trickled to regulate the bandwidth for a particular app instead of specifying it at every invocation. For instance, if you wish to limit the download speed available to the Wget downloader to 1Mbps, you can do it as follows:

trickled -d 1024 wget

Once set, whenever you call wget, such as

trickle wget -c somefile.iso

trickle will ensure the downloads don’t exceed the specified limit (1Mbps in this case). Note that when you fix speeds for a particular application, like the BitTorrent client, the allotted bandwidth gets split between all the files you download simultaneously. So if you’ve allocated 1Mbps upload speed to Transmission and have two seeding torrents, their combined upload speed will not exceed the specified limit.

Permanent Settings

If you think setting upload and download speeds is all that trickle does, you’re wrong. This nifty little tool lets you set per-application priority, and you can also define time- and length-smoothing parameters.

With time-smoothing, you can define the time interval for a particular application to transfer data. Large values will produce bursts in sending and receiving data, while smaller values ensure a smooth and continuous data transfer. Use the -t option to define the time-smoothing values in seconds. The default value is 5s. According to the trickled man page, a smoothing time of under 1s is ideal for interactive applications and anywhere from 1–10s is recommended for applications that transfer large amounts of data.

Like time smoothing, you can also define length smoothing with the -l command switch. Length smoothing is based on the length of the I/O operation. It’s usually used as a fallback option to time smoothing; if trickled is unable to meet the interval specified with time smoothing, it’ll instead try to meet the transfer size specified with length smoothing (the default value is 10KB).

As previously mentioned, defining global speeds (i.e., setting speed limits for all applications) is done with the trickled command. But what if you need to define custom time- and length-smoothing values for several applications? To achieve this ,you’ll need to specify the value in trickle’s configuration file, /etc/trickled.conf (Figure 2). In fact, when you run trickle, it checks to see if the trickled daemon is running and implements the parameters defined in the file for the application it is called to throttle. Remember that trickle will override the options defined in /etc/trickled.conf if you specify values with the trickle command.

Figure 2: trickle’s configuration file has a couple of example entries to help you create new ones.

Each section in the configuration file is demarcated by square brackets, and the format of the file is pretty straightforward:

[service]
Priority = <value>
Time-Smoothing = <value>
Length-Smoothing = <value>

The first important parameter in the trickled.conf file is the Priority parameter. Priorities are set relative to other services. An application with a lower value means it has a higher priority. Applications with a higher priority get more bandwidth than the others.

Here’s how an actual configuration file looks:

[ssh]
Priority = 1
Time-Smoothing = 0.1
Length-Smoothing = 1

[www]
Priority = 2
Time-Smoothing = 5
Length-Smoothing = 10

[ftp]
Priority = 5

In this example, traffic over SSH has the highest priority; to make sure that the remote sessions don’t have any unnecessary delays, both the time-smoothing and length-smoothing parameters are also set to the lowest values. Web browsing also has a high priority for a smooth experience. Finally, FTP gets the lowest priority, since I am in no particular hurry for my downloads to complete.

Linux offers other bandwidth shapers besides trickle, including wondershaper, which is useful for enforcing limits directly on the network interfaces (see the “Shape Network Adapters” box).

Shape Network Adapters

Another way of controlling network traffic is to limit bandwidth on individual network interface cards (NICs). wondershaper is a small Bash script that uses the tc command-line utility in the background to let you regulate the amount of data flowing through a particular NIC. As you can imagine, while you can use wondershaper on a machine with a single NIC, its real advantage is on a machine with multiple NICs.

Just like trickle, wondershaper too is available in the official repositories of mainstream distributions. To limit network traffic with wondershaper, specify the NIC on which you wish to restrict traffic with the download and upload speed in kilobits per second. For example,

wondershaper enp5s0 4096 1024 

will restrict the download speed on the first wired adapter to 4Mbps and the upload speed to 1Mbps. If you specify a NIC with the wondershaper command, it’ll return the status of traffic shaping on that NIC, such as wondershaper enp5s0. Use the clear option to remove all custom restrictions from a network interface:

wondershaper clear enp5s0

Unlike trickle, however, wondershaper requires superuser privileges, so you’ll either have to run the above commands with sudo or as the root user.

trickle packs quite a punch despite its minuscule size. You can use trickle in standalone or daemon mode, which gives you more control and flexibility over its operations (Figure 3). It does have its limitations, in that it can only handle TCP streams and apps that use dynamic libraries. However, trickle is still a wonderful utility for anyone looking to control network traffic on a per-application basis.

Figure 3: You can create custom launchers for Internet apps such as Firefox with a trickle throttled bandwidth.

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Traffic shaping with Trickle

    Is your Internet connection groaning under the load of too many simultaneous downloads? If so, try Trickle, a simple application that gives you more granular control over network traffic.

  • Charly's Column: Trickle

    If your data traffic suffers from congestion at times, don't worry. Now you can shoot down programs that are heavy on traffic to free up the inflow and outflow.

  • Cloud Backup

    Encrypted backup is easier than ever with MCrypt and the Amazon Web Service's S3cmd utility.

comments powered by Disqus