views:

765

answers:

3

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.

+2  A: 

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.

Jonathan Feinberg
+1 for python pointers.
whatnick
+4  A: 

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.

Nick Craig-Wood
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.
Andy Ross
A: 

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)

ezpz