Re: Rate limiting with "tc"?
- From: Felix Tiede <f.tiede@xxxxxx>
- Date: Wed, 24 Sep 2008 22:47:22 +0200
Ravenpi wrote:
Hi, all. My company has a reasonably large pipe to the Internet;
however, certain large transfers (e.g., our hourly database
replication with our offsite server) spike the usage. I'd like to
limit our traffic, both inbound and outbound, such that no single user
can use more than 50% of our bandwidth. I've poked around, and it
seems that the best way to accomplish this is by way of the "tc"
command... unfortunately, its documentation is, ummmm... "in depth."
While I'm willing to wade through it, and play with trial-and-error,
this is one of those times where I just want that one magic command,
and then I'll move on. So any help would be greatly appreciated.
First:
Outbound policing is possible, but inbound is not.
Now onto tc:
tc works together with something to classify packets. There's a number of
tools to do so, some with more features, others simpler to maintain. I for
myself use iptables for this task. To do so, you need the MARK target
available for iptables, because it's a mark applied to each packet used to
classify and thus assign a packet to a specific rate-limited queue.
Also I'm using the HTB (Hierarchical Token Bucket) to classify packets.
That's based on historic reasons: I've did it with htb years ago and never
had any reason or will to change it.
Now onto the usage:
1. You need a qdisc for your nic.
tc qdisc add dev <NIC> root handle <x>:0 htb default <n>
NIC is the network device to work on, x is the master number of your chains,
n is the default chain if packets can not be assigned by other means.
2. Now you need a master chain:
tc class add dev <NIC> parent <x>:0 classid <x>:1 htb \
rate <NORM>kbit ceil <MAX>kbit
NIC and x are always the same as used for 'tc qdisc' above, NORM and MAX are
the normal and maximum rates (in kbit/s) available for this nic on the
outbound direction. both might be the same, but NORM should not be greater
than MAX. This means that normally not more than NORM kbit/s should be
sent, but if traffic exceeds this rate, there's more bandwith available.
3.a You need a default chain:
tc class add dev <NIC> parent <x>:1 classid <x>:<n> htb \
rate <(NORM-T)>kbit ceil <MAX>kbit prio 2
(NORM-T) is the normal traffic bandwith without traffic handled by specific
chains. By specifying 'ceil <MAX>kbit' the hard limit is anyway the maximum
bandwith you allow. Thus, if default traffic exceeds the normal rate and no
other traffic is using bandwith, default traffic can use up to MAX kbit/s.
There's no need to explicitly put all packets into this chain, they are put
into this chain by default due to 'default <n>' from step 1.
3.b Now you need chains with limited bandwith and priorities:
tc class add dev <NIC> parent <x>:1 classid <x>:<y> htb \
rate 10kbit ceil <MAX>kbit prio 0
This is a chain for very low traffic but with maximum priority. I use it for
ACK packets speeding up my downloads even if there's lot of other upstream
traffic. But it's also limited in bandwith. y is a unique number to name
the chain.
3.c To use it you need an iptables rule:
iptables -t mangle -A OUTPUT -p tcp \
-m length --length :64 -j MARK --set-mark <z>
This needs the length match for iptables. You can set the mark (z) to
whatever you like; I prefer the number of the chain to make it easier to
see which iptables rule(s) belong to which chain.
3.d Now you need to tell the system to put packets with a specific mark to a
chain:
tc filter add dev <NIC> parent <x>:0 prio 0 protocol ip \
handle <z> fw flowid <x>:<y>
That's it. Now packets up to the size of 64byte are prioritized first but
may normally not use more than 10kbit/s of bandwith.
Repeat steps 3.b-d for all types of traffic you may need to prioritize.
HTH,
Felix
.
- Follow-Ups:
- Re: Rate limiting with "tc"?
- From: Ravenpi
- Re: Rate limiting with "tc"?
- References:
- Rate limiting with "tc"?
- From: Ravenpi
- Rate limiting with "tc"?
- Prev by Date: Re: Recommendation for 1000BASE-T NIC
- Next by Date: Re: Recommendation for 1000BASE-T NIC
- Previous by thread: Re: Rate limiting with "tc"?
- Next by thread: Re: Rate limiting with "tc"?
- Index(es):
Relevant Pages
|