Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Traffic shaping under Linux

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.

like image 484
Seif Sallam Avatar asked Oct 10 '09 14:10

Seif Sallam


People also ask

What is Linux traffic control?

tc (traffic control) is the user-space system administration utility program used to configure the Linux kernel packet scheduler. Tc is usually packaged as part of the iproute2 package.

What is Qdisc in Linux?

qdisc. Simply put, a qdisc is a scheduler (Section 3.2). Every output interface needs a scheduler of some kind, and the default scheduler is a FIFO. Other qdiscs available under Linux will rearrange the packets entering the scheduler's queue in accordance with that scheduler's rules.

What does a traffic shaper do?

A traffic shaper works by delaying metered traffic such that each packet complies with the relevant traffic contract. Metering may be implemented with, for example, the leaky bucket or token bucket algorithms (the former typically in ATM and the latter in IP networks).

How is traffic shaping Ubiquiti?

To enable the traffic shaping feature, go to the Network tab and make sure the Configuration Mode is set to Advanced. Then scroll down and click the checkbox to enable Traffic Shaping.


2 Answers

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.

like image 124
Jonathan Feinberg Avatar answered Sep 16 '22 22:09

Jonathan Feinberg


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.

like image 27
Nick Craig-Wood Avatar answered Sep 18 '22 22:09

Nick Craig-Wood