How to throttle bandwidth on a Linux network interface?

58,120

Solution 1

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)

Solution 2

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.

Solution 3

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.

Solution 4

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.

Solution 5

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.

Share:
58,120

Related videos on Youtube

Mark Borgerding
Author by

Mark Borgerding

linux dsp geek

Updated on September 17, 2022

Comments

  • Mark Borgerding
    Mark Borgerding over 1 year

    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.

  • Admin
    Admin about 15 years
    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.
  • Wayne
    Wayne about 15 years
    Yes, this is a very good module for these things, indeed. I can't believe noone answered this until now...
  • Scott
    Scott about 15 years
    You may be able to simulate a machine with 2 NICs using a VM.
  • hasen
    hasen over 14 years
    link doesn't work
  • mrcl
    mrcl over 14 years
    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"
  • Tom
    Tom about 14 years
    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)
  • David Spillett
    David Spillett almost 14 years
    trickle 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.
  • sleske
    sleske over 13 years
    BTW: It's package wondershaper in Debian.
  • sleske
    sleske over 13 years
    Quick note: the kernel module is sch_netem. Generally there's no need to load it manually, it will be loaded automatically as needed.
  • orange80
    orange80 about 13 years
    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!
  • Lee Chee Kiam
    Lee Chee Kiam about 12 years
  • Momshad Dinuri Alvee
    Momshad Dinuri Alvee about 12 years
    wondershaper 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.
  • tripleee
    tripleee over 11 years
    Link works for me.
  • z0r
    z0r over 10 years
    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 serverfault.com/a/394949/76090
  • totti
    totti over 10 years
  • CMCDragonkai
    CMCDragonkai almost 10 years
    Can this differentiate different ports or different applications or different type of traffic... etc.
  • Léo Lam
    Léo Lam over 9 years
    To reset, use: sudo wondershaper clear eth0
  • Jay R. Wren
    Jay R. Wren almost 8 years
    This is exactly for what I am looking! Huge thanks. I'm glad I scrolled down and kept reading.
  • Stan Smulders
    Stan Smulders over 7 years
    @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 ;-)
  • mirkobrankovic
    mirkobrankovic about 7 years
    Is there a quick way to clear the delay and loss, or I need to set it to 0 ?