How to throttle bandwidth on a Linux network interface?

57

15

Is there a way to constrain the bandwidth on a NIC in linux? I'd like to be able to simulate an arbitrarily slow connection.

Mark Borgerding

Posted 2008-08-28T02:27:19.920

Reputation: 848

Answers

32

The netem kernel module, controlled by iproute.

You need to compile Netem with the kernel:

Networking -->
   Networking Options -->
     QoS and/or fair queuing -->
        Network emulator

Once the netem module loaded, iproute's tc allows you things like:

tc qdisc add dev  tap0 root netem delay 50ms loss 50% 

(50 % packet loss, extra delay of 50 ms)

bortzmeyer

Posted 2008-08-28T02:27:19.920

Reputation: 1 083

Can this differentiate different ports or different applications or different type of traffic... etc. – CMCDragonkai – 2014-07-21T12:31:09.997

Is there a quick way to clear the delay and loss, or I need to set it to 0 ? – mirkobrankovic – 2017-03-14T09:01:20.240

1Quick note: the kernel module is sch_netem. Generally there's no need to load it manually, it will be loaded automatically as needed. – sleske – 2010-12-29T00:41:05.413

Here's working link http://www.linuxfoundation.org/collaborate/workgroups/networking/netem

– Lee Chee Kiam – 2012-04-19T02:43:22.313

Yes, this is a very good module for these things, indeed. I can't believe noone answered this until now... – None – 2009-04-28T09:37:47.113

I use this for testing servers locally (on localhost) as it's been standard on all distros for years. To add 20ms delay "tc qdisc add dev lo root handle 1:0 netem delay 20msec", and to remove it "tc qdisc del dev lo root" – pixelbeat – 2009-08-30T23:16:56.583

My version of netem (Ubuntu 13.10) also has the option to limit the bandwidth using the rate option: tc qdisc add dev lo root handle 1:0 netem delay 10ms rate 1mbit limit 1000. Note that for some devices such as lo (for accessing localhost), you need to set the queue length as well: ifconfig lo txqueuelen 1000. See http://serverfault.com/a/394949/76090

– z0r – 2013-11-20T02:58:05.890

27

Client side, right?

trickle should do what you want. If you're running Ubuntu (or Debian, I think), you can install it with sudo apt-get install trickle, and then run it. trickle -s -d 10 -u 10 firefox (or so, I haven't used it in a while) would run firefox, limiting its download and upload speed to 10 kilobytes a second.

Bernard

Posted 2008-08-28T02:27:19.920

Reputation: 575

Thanks for this tip. For some reason trickle wouldn't work for me with firefox but it worked fine with google-chrome. (Ubuntu 9.10 64bit) – Tom – 2010-02-18T18:52:19.083

6trickle works by intercepting calls to link in standard library functions for TCP transfers - if an app has these functions statically linked in at compile time then this can not work. – David Spillett – 2010-08-09T13:34:07.450

16

I found a way to avoid my box from eating all the bandwidth available on network using a tool called 'wondershaper' in ubuntu. I hope it exist in other linux destros too. I can limit the bandwidth what my linux home pc can consume using following command.

sudo wondershaper eth0 1000 200

This limits the download limit to 1000 kilobits and uplaod to 200 kilobits. Refer to the man pages of wondershaper for more information/options.

harry

Posted 2008-08-28T02:27:19.920

Reputation: 541

To reset, use: sudo wondershaper clear eth0 – Léo Lam – 2015-01-18T18:33:05.627

@boatcoder Well it is in kilobit, so it should be 8192 kilobit to get your 1Mb/s. Still no 28096, but definitely not 1024 either ;-) – Stan Smulders – 2017-01-06T11:11:26.940

1This is not quite what the OP is looking for (it's about conserving bandwith, not about purposefully degrading the connection), but still good to know. – sleske – 2010-12-20T09:41:15.620

1BTW: It's package wondershaper in Debian. – sleske – 2010-12-20T09:41:32.110

This rocked b/c we were having problems at the office with bandwidth saturation, there was a backing going that I couldn't interrupt, and I was able to fix this in two simple lines: # apt-get install wondershaper then # wondershaper eth0 9999999 500 thanks again! – orange80 – 2011-05-13T20:59:06.443

4wondershaper appears to have some math problems though. To get a 1Mb/s up/down path, we ended up using 28096 instead of 1024. No idea as to why this happened though. – boatcoder – 2012-05-14T20:19:42.427

8

NIST make a network simulator called NistNet.

http://snad.ncsl.nist.gov/nistnet/ (link seems dead)

http://cs.ecs.baylor.edu/~donahoo/tools/nistnet/

NistNet lets you make a router that emulates a comms link of your choosing.

For simplest operation you have it on a box with two NIC's between two networks.

For example, I had an application that had to work over a satellite link.

Data could go over the a 2Mbps link with BIG latency. Control had to go over a 128kbps link, with the same latency.

NistNet helped to get it all working on 128kbps.

Using NistNet I could simulate not only the bandwidth, but also the latency, and it will also drop packets for you, simulating a busy or unreliable link.

If you want to simulate an abitrary connection, don't just constrain the bandwidth, wind the latency up too.

Don't forget to fragment packets either. You can set nice small MTU's on the phony link.

IIRC adding packet jitter will let you find out if you can cope with out-of order packets.

Tim Williscroft

Posted 2008-08-28T02:27:19.920

Reputation: 675

Link works for me. – tripleee – 2012-10-05T03:49:22.800

NistNet is not updated in a long time, is it? IMO, the best way by far is the netem module, which already exists in all recent kernels. – None – 2009-04-28T09:35:27.120

You may be able to simulate a machine with 2 NICs using a VM. – Scott – 2009-04-28T14:50:02.510

link doesn't work – hasen – 2009-08-30T20:18:24.273

4

I've personally used wondershaper in the past for this, though it was written for an opposite use case - making the most of your ADSL connection.

I'll have to try the others mentioned here though.

Thomas Vander Stichele

Posted 2008-08-28T02:27:19.920

Reputation: 201

3

You can use locally installed squid proxy to limit bandwidth for example to test a web page on slow link:

  1. Install squid proxy from your distribution — on my Fedora it was as easy as yum install squid.

  2. Add the following to /etc/squid/squid.conf:

    delay_pools 1
    delay_class 1 1
    delay_access 1 allow all
    delay_parameters 1 16000/16000

    This will limit bandwidth to about 128 kbps (16000 Bps).

  3. Start squid: /etc/init.d/squid start

  4. Configure your browser to use proxy server localhost port 3128.

Tometzky

Posted 2008-08-28T02:27:19.920

Reputation: 464

3

No one has yet mentioned ip_relay, named "iprelay" in the Debian repositories. It works as a TCP proxy, rather than intercepting calls the way trickle does, and will work with any application that can use a proxy (e.g. a web browser) or accept a user-defined destination port (telnet, ssh, ftp, curl, et cetera).

It's a little more difficult to set up, but often works in situations where trickle won't.

CodeGnome

Posted 2008-08-28T02:27:19.920

Reputation: 1 841

1This is exactly for what I am looking! Huge thanks. I'm glad I scrolled down and kept reading. – Jay R. Wren – 2016-07-15T13:57:33.460