I'm writing software for bandwidth management in Linux. I use TC for bandwidth shaping, and it worked fine. I want to limit user bandwidth for a period of time. For example:

  • For user 1, 100MB in a week

What is the best way to do it?


The iptables quota module can be quite useful, too.


You can add an iptables rule for each IP and use it to count the traffic that passed through the rule. Just add 2 rules for each IP:

 iptables -A FORWARD -s <ip>
 iptables -A FORWARD -d <ip>

And then you can get the results with iptables -nv -L FOWARD that will return something like this:

# iptables -nvL FORWARD
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 ACCEPT     all  --  *      virbr0  0.0.0.0/0            192.168.122.0/24    state RELATED,ESTABLISHED

This one is zero but the bytes column will give you what you want I swear :)

After that all you need is a cron job to save those values, and to check if they are over the alloted bytes that week, then change the rules to block the traffic or use tc to throttle their bandwidth, or anything else.