I'd like to automatically shape bandwidth-hungry TCP connections corresponding to the already transmitted data.
A summary would be : "The first Mbit is transferred at full bandwidth, then it gradually drops to 1% of the bandwidth when it reaches 10Mbit, and stays there".
An example is better than 1k words :
b-w ^
| . .
100% | ******** .
| . * .
| . * .
| . * .
1% | . .********
+----------------------------> Data transfered.
1Mbit . . 10Mbit
I know that a full traffic shaping would be better, since it would allow bursting and use of leftover bandwidth, but the idea is specifically to automatically limit big data transfers without any further configuration.
How would I implement this on a linux host ?
Update: It isn't immediately obvious, but the data meter counts both ways (upload & download) as I purposefully didn't precise download or upload.
The HTB qdisc implements the concept of bursts which is a bit of what you want - it sends at full hardware rate up to the amount of data specified in the "burst" parameter. To get a gradual decrease you would need to nest HTB classes, and probably you would not want to do it too excessively as it greatly increases the complexity of the setup. But the Linux traffic shaping engine itself is stateless, it just acts upon packets, not connections. Using tc filters alone, you can only differentiate packets based on IP/TCP headers.
So if you need to classify differently based on connections, the most straightforward approach would probably be using the iptables "--connbytes" match and a packet mark (-j MARK target) to shove a connection's packets into the right queue (fast/slowdown)
See the wordy section on bandwidth management of the LARTC howto and / or the comprehensive "Open source bandwidth solutions" whitepaper for more insight.
Furthermore, if you need to shape upstream as well as downstream, take a look at the implementation of the virtual IMQ interface - it has been designed specifically for this purpose.
I was inspired by @the-wabbit solution to this, and managed to solve this. I blogged about this solution in my personal wiki: https://giki.wiki/@nubela/Software-Engineering/Per-Connection-Throttling
Here's my solution to this question with an actual shell script: