trickel.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <!DOCTYPE html>
  2. <div class="full article">
  3. <h2>
  4. Managing Network Bandwidth </h2>
  5. <div class="full-content">
  6. <div class="social_bookmarks">
  7. <div style="display: inline; padding-top: 5px;">
  8. </div>
  9. </div>
  10. <h1> Trickle</h1>
  11. <div class="leadimage">
  12. <div class="author">
  13. Author(s): <span class="author" itemprop="author">Mayank Sharma</span>
  14. </div>
  15. <div class="article_intro">
  16. <p>The trickle command-line utility helps you shape network traffic.</p>
  17. </div>
  18. <div class="article_body"><p><i>Special Thanks: This article was made possible by support from&nbsp;<a href="https://www.lpi.org/our-certifications/lpic-1-overview" target="_blank">Linux Professional Institute</a></i></p><p>If you have ever been in a situation where one application ate all your bandwith, then you will appreciate the <i>trickle</i> bandwidth shaper application. With <i>trickle, </i>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.</p><p>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. <i>trickle</i> 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).</p><p><i>trickle</i> 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 <i>trickle</i>.</p><p><i>trickle</i> regulates speeds by delaying the data transferred over a socket. <i>trickle</i> provides an alternate version of the BSD socket API, causing socket calls to be handled now by <i>trickle</i>. Speed is limited by controlling the amount of the data written or read from a socket.</p><p>A word of warning, <i>trickle</i> 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 <i>trickle</i>, see the <a href="https://monkey.org/~marius/trickle/trickle.pdf" target="_blank"><i>trickle technical paper</i></a>). Additionally, <i>trickle</i> doesn’t work with all TCP connections either. Since <i>trickle</i> uses dynamic linking and loading, it can only work with applications that use dynamic libraries (specifically <i>glibc)</i>. In other words, <i>trickle</i> 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 -- <i>trickle</i> acts as a proxy between these two components.</p><p>In order to determine whether or not you can use <i>trickle</i>, use the <i>ldd</i> command (Figure 1), which lists the dynamic dependencies of all the shared libraries. Specifically, look for the presence of the GNU C library (<i>glibc)</i> in the list of dynamic dependencies for any given application, since this library defines the system calls that are invoked when communicating via sockets.</p>
  19. <div class="object-center">
  20. <div class="imagecenter">
  21. <img src="/var/linux_magazin/storage/images/media/linux-magazine-eng-us/images/trickle_f14/745467-1-eng-US/Trickle_F1_reference.png" alt="" />
  22. <div style="width: 600px;">
  23. 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.
  24. </div>
  25. </div>
  26. </div><p>For example, if you’re wondering whether <i>trickle</i> will work with the cURL downloader, use the following command:</p> <pre>$ ldd /usr/bin/curl | grep libc.so</pre><p>This command should return a result resembling:</p> <pre>libc.so.6 =&gt; /lib/x86_64-linux-gnu/libc.so.6 (0x00007fc532022000)</pre><p>You can similarly test for other network applications. As long as you get a result, <i>trickle</i> 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 <i>libc</i>; thus <i>trickle</i> cannot be used as bandwidth shaper in that instance. As shown in the example, <i>trickle</i> 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.</p><a name="eztoc0_0_1" id="eztoc0_0_1"></a><h4>Using trickle</h4><p>When using <i>trickle</i>, you can choose appropriate download and upload speeds to limit applications. You don’t have to do anything fancy – just use the <i>-d</i> and the <i>-u</i> switches to tell <i>trickle</i> the respective download and upload speeds for an application.</p><p>For instance, the command</p> <pre>trickle -d 1024 wget -c 
  27.   https://archive.org/download/CharlieChaplin/Chaplin_512kb.mp4</pre><p>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 <i>trickle</i>’s ability – you can use Wget’s <i>--limit-rate</i> option to regulate its download speed.) In the same vein, use the <i>-u</i> switch to regulate the upload speed.</p><p>If setting per-application speeds seems too much of a chore, you can set global speed caps with the <i>trickled</i> command, such as</p> <pre>trickled -d 2048 -u 1024</pre><p><i>trickled</i> runs <i>trickle</i> in the daemon mode, allowing you to define download and upload limits to be shared by all the applications run through <i>trickle</i> without the need for specifying limits each time. The <i>trickled</i> daemon monitors all connections; once you’ve set up <i>trickled</i>, you don’t need to bother setting speeds for Wget, FTP, browsers ,and everything else that uses TCP. So,</p> <pre>trickle apt upgrade</pre><p>will now update the Ubuntu installation while respecting the limits set by the <i>trickled</i>.</p><p>You can similarly use <i>trickled</i> 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:</p> <pre>trickled -d 1024 wget</pre><p>Once set, whenever you call <i>wget</i>, such as</p> <pre>trickle wget -c somefile.iso</pre><p><i>trickle</i> 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.</p><a name="eztoc0_0_2" id="eztoc0_0_2"></a><h4>Permanent Settings</h4><p>If you think setting upload and download speeds is all that <i>trickle</i> does, you’re wrong. This nifty little tool lets you set per-application priority, and you can also define time- and length-smoothing parameters.</p><p>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 <i>-t </i>option to define the time-smoothing values in seconds. The default value is 5s. According to the t<i>rickled</i> 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.</p><p>Like time smoothing, you can also define length smoothing with the <i>-l</i> 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 <i>trickled</i> 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).</p><p>As previously mentioned, defining global speeds (i.e., setting speed limits for all applications) is done with the <i>trickled</i> 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 <i>trickle</i>’s configuration file, <i>/etc/trickled.conf</i> (Figure 2). In fact, when you run <i>trickle</i>, it checks to see if the <i>trickled</i> daemon is running and implements the parameters defined in the file for the application it is called to throttle. Remember that <i>trickle</i> will override the options defined in <i>/etc/trickled.conf</i> if you specify values with the <i>trickle</i> command.</p>
  28. <div class="object-center">
  29. <div class="imagecenter">
  30. <img src="/var/linux_magazin/storage/images/media/linux-magazine-eng-us/images/trickle_f2/745470-1-eng-US/Trickle_F2_reference.png" alt="" />
  31. <div style="width: 600px;">
  32. Figure 2: trickle’s configuration file has a couple of example entries to help you create new ones.
  33. </div>
  34. </div>
  35. </div><p>Each section in the configuration file is demarcated by square brackets, and the format of the file is pretty straightforward:</p> <pre>[service]
  36. Priority = &lt;value&gt;
  37. Time-Smoothing = &lt;value&gt;
  38. Length-Smoothing = &lt;value&gt;</pre><p>The first important parameter in the <i>trickled.conf</i> file is the <i>Priority</i> 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.</p><p>Here’s how an actual configuration file looks:</p> <pre>[ssh]
  39. Priority = 1
  40. Time-Smoothing = 0.1
  41. Length-Smoothing = 1
  42. [www]
  43. Priority = 2
  44. Time-Smoothing = 5
  45. Length-Smoothing = 10
  46. [ftp]
  47. Priority = 5</pre><p>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.</p><p>Linux offers other bandwidth shapers besides <i>trickle</i><i>,</i> including <i>wondershaper, </i>which is useful for enforcing limits directly on the network interfaces (see the “Shape Network Adapters” box).</p><div class="box textbox" id="article_xturning_back_the_clock">
  48. <div class="boxtitle">
  49. <a name="eztoc0_0_3" id="eztoc0_0_3"></a><h4>Shape Network Adapters</h4>
  50. </div>
  51. <p>Another way of controlling network traffic is to limit bandwidth on individual network interface cards (NICs). <i>wondershaper</i> is a small Bash script that uses the <i>tc</i> 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 <i>wondershaper</i> on a machine with a single NIC, its real advantage is on a machine with multiple NICs.</p><p>Just like <i>trickle</i>, <i>wondershaper</i> too is available in the official repositories of mainstream distributions. To limit network traffic with <i>wondershaper</i>, specify the NIC on which you wish to restrict traffic with the download and upload speed in kilobits per second. For example,</p> <pre>wondershaper enp5s0 4096 1024 </pre><p>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 <i>wondershaper</i> command, it’ll return the status of traffic shaping on that NIC, such as <i>wondershaper enp5s0</i>. Use the <i>clear</i> option to remove all custom restrictions from a network interface:</p> <pre>wondershaper clear enp5s0</pre><p>Unlike <i>trickle</i>, however, <i>wondershaper</i> requires superuser privileges, so you’ll either have to run the above commands with <i>sudo</i> or as the root user.</p>
  52. <div class="clear"></div>
  53. </div><p><i>trickle</i> packs quite a punch despite its minuscule size. You can use <i>trickle</i> 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, <i>trickle</i> is still a wonderful utility for anyone looking to control network traffic on a per-application basis.</p>
  54. <div class="object-center">
  55. <div class="imagecenter">
  56. <img src="/var/linux_magazin/storage/images/media/linux-magazine-eng-us/images/trickle_f3/745473-1-eng-US/Trickle_F3_reference.png" alt="" />
  57. <div style="width: 600px;">
  58. Figure 3: You can create custom launchers for Internet apps such as Firefox with a trickle throttled bandwidth.
  59. </div>
  60. </div>
  61. </div></div>
  62. <div id="articlebox">
  63. <div class="articlebox row">
  64. </body>
  65. </html>