Managing Network Bandwidth
Trickle

Photo by Thom Holmes on Unsplash
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.

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.
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).
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.
Buy Linux Magazine
Direct Download
Read full article as PDF:
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Find SysAdmin Jobs
News
-
LibreOffice 7.5 has Arrived and is Loaded with New Features and Improvements
The favorite office suite of the Linux community has a new release that includes some visual refreshing and new features across all modules.
-
The Next Major Release of Elementary OS Has Arrived
It's been over a year since the developers of elementary OS released version 6.1 (Jólnir) but they've finally made their latest release (Horus) available with a renewed focus on the user.
-
KDE Plasma 5.27 Beta Is Ready for Testing
The latest beta iteration of the KDE Plasma desktop is now available and includes some important additions and fixes.
-
Netrunner OS 23 Is Now Available
The latest version of this Linux distribution is now based on Debian Bullseye and is ready for installation and finally hits the KDE 5.20 branch of the desktop.
-
New Linux Distribution Built for Gamers
With a Gnome desktop that offers different layouts and a custom kernel, PikaOS is a great option for gamers of all types.
-
System76 Beefs Up Popular Pangolin Laptop
The darling of open-source-powered laptops and desktops will soon drop a new AMD Ryzen 7-powered version of their popular Pangolin laptop.
-
Nobara Project Is a Modified Version of Fedora with User-Friendly Fixes
If you're looking for a version of Fedora that includes third-party and proprietary packages, look no further than the Nobara Project.
-
Gnome 44 Now Has a Release Date
Gnome 44 will be officially released on March 22, 2023.
-
Nitrux 2.6 Available with Kernel 6.1 and a Major Change
The developers of Nitrux have officially released version 2.6 of their Linux distribution with plenty of new features to excite users.
-
Vanilla OS Initial Release Is Now Available
A stock GNOME experience with on-demand immutability finally sees its first production release.