Traffic shaping under Linux
Asked Answered
S

4

5

Where can I learn about controlling/interrogating the network interface under Linux? I'd like to get specific application upload/download speeds, and enforce a speed limit for a specific application.

I'd particularly like information that can help me write a traffic shaping application using Python.

Sardis answered 10/10, 2009 at 14:24 Comment(0)
U
6

You want the iproute2 suite, in which you use the tc command. tc commands look like

tc class add dev eth2 parent 1: classid 1:1 htb rate 100Mbit ceil 100Mbit quantum 1600

Here's an existing Python traffic-shaping application that uses iproute2.

Untoward answered 10/10, 2009 at 15:21 Comment(0)
S
6

It is actually quite hard shaping per application using the linux kernel tools, unless the application uses specific ip addresses and/or ports you can match on.

Assuming that is the case then you'll need to read up on iptables and in particular fwmarks. You'll also need to read up on tc. In combination those two tools can do what you want. The Linux Advanced Routing & Traffic Control is a good place to start.

Assuming your application doesn't use a predictable set of ports/ip addresses then you'll need to use a userspace shaper like Trickle. This inserts itself between the application and the kernel and shapes the traffic for that application in userspace.

I don't think there are any direct python bindings for any of those tools, but it would be simple to script them using python and just calling the executables directly.

Schedule answered 10/10, 2009 at 15:34 Comment(1)
iptables can match on uid/gid for locally-generated traffic. That makes a convenient way of doing work on specific applications, provided you have some control over the user/group setup of the deployment environment.Norite
N
0

Is there any reason you wish to use python? As mentioned, it will likely only hand-off to already developed tools for this purpose. However, if you look around, you can find things such as Click! modular router, XORP, and others that provide a drop-in for things you want to do - not to mention all the suggestions already provided (such as iptables and tc)

Nickell answered 10/10, 2009 at 18:49 Comment(1)
It could be for maintainability. In a python shop other users will get productive more quickly if there is a python wrapper.Peewit
M
0

Bandwidth limiting for specific application (google-chrome in this case):

trickle -d 600 -u 200 google-chrome

units are in kbits

Total bandwidth limiting for an ethernet device :

sudo wondershaper eth0 600 200

Units are in kbits again. Change eth0 to your interface name. It uses tc tbf and htb...

To master connectivity on linux, commands & programs you need to learn are :

ip
ifconfig
route
iptables
netstat
tc
wondershaper
trickle
iftop
iptraf-ng
wget 
curl
nslookup
dig

The document you need to use as a reference is :

https://www.lartc.org/howto/index.html

Cookbook has nice examples in this document.

An easy-to-read flowing blog page is :

https://www.cnblogs.com/zengkefu/p/5635100.html

Do not forget to have fun experimenting with them ;).

Malissamalissia answered 31/7, 2020 at 9:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.