Clone this wiki locally
tcplim is a program to limit TCP connections bandwidth in userspace for Linux.
It is easy to set up, you don't need "tc" commands (but need iptables with REDIRECT target)
- limit bandwidth of individual connections or in total (or both (not implemented fully)).
- set up priorities (as for now - only from console after the connection has already established)
- enumerate connection, their total sent/received bytes, current speed
- terminate tcplim-controlled connections from console, change limits for their speed (including temporarity disabling them).
How to setup
- Compile tcplim file
- Create tcplim user account
- Start tcplim from tcplim user account:
tcplim@host:~ tcplim 0.0.0.0 1236 REDIRECT REDIRECT 100000 100000 0 0 50
iptables -t nat -A OUTPUT -p tcp -m owner ! --uid-owner tcplim -j REDIRECT --to-ports 1236
- All connections that are redirected to tcplim gets limited to 100k per second upload and 100k down per second (in total).
How it works
- You try to connect somewhere, for example to 18.104.22.168:80
- Due to iptables rule, you actually connect to 127.0.0.1:1236, i.e. to tcplim.
- tcplim receives connection, gets SO_ORIGINAL_DST address (22.214.171.124:80) and connects there
- (iptables does not redirect that tcplim's connection because of
! --uid-owner tcplim)
- tcplim passes buffers between that two connections (one to you, other to 126.96.36.199:80), limiting speed.
tcplim uses fixed number of command line options:
- Address to listen.
0.0.0.0to listen all interfaces.
- Port to listen. You should also specify it in
- Address to connect. Use
REDIRECTif you use it with
- Port to connect. Use
REDIRECTif you use it with
- Total upload limit (bytes per second). 0 to disable.
- Total download limit (bytes per second). 0 to disable.
- Per-stream upload limit (bytes per second). 0 to disable.
- Per-stream download limit (bytes per second). 0 to disable.
- Time interval, in milliseconds. Too much == jitter in bandwidth. Too low == higher overhead + too small packets.
l- enumerate connections (with limits, total uploaded/downloaded bytes, current speed, priority level).
source_ip:source_port -> dest_ip:dest_port [source_fd->dest_fd] uploaded_bytes:downloaded_bytes limit=upload_bps_limit:download_bps_limit nice=upload_prio:download_prio rate=upload_rate:download_rate
k 6- kill connection (specifying source or dest fd)
i 7 10000- limit fd 7 to 10000 bytes per second.
o 10000 20000- set total upload limit to 10kbps and total download limit to 20kbps
n 7 9- set nice level. Default is 10. Less == more priority. Unconstrained fully loaded high prio connection == everything else starving.
q- quit. All connections gets interrupted.
t 500- edit Time Interval parameter (the last command line option)
d 10000 20000- Set default limits for new connections (upload download)
r- Apply default limits to all connections
You should be careful not to loop tcplim into itself.
iptables should not redirect tcplim's own connections. Rules are added to OUTPUT table of "nat" table. It is recommended to run tcplim from a separate user account (and set up iptables to ignore this account when redirection connections).
You can also use tcplim without redirection. You should just specify destination address, and tcplim will always connect to the same location. It is useful if you want to put tcplim behind, for example, SOCKS proxy or ssh-forwarded port.
Also you can add rule to PREROUTING table and limit other's connections (if you route something).