Penguin
Diff: TrafficShaping
EditPageHistoryDiffInfoLikePages

Differences between version 15 and predecessor to the previous major change of TrafficShaping.

Other diffs: Previous Revision, Previous Author, or view the Annotated Edit History

Newer page: version 15 Last edited on Friday, October 29, 2004 5:53:02 pm by JohnMcPherson Revert
Older page: version 12 Last edited on Friday, October 29, 2004 2:37:22 pm by PatrickGrant Revert
@@ -1,5 +1,20 @@
-This is my first attempt and writing something up about traffic shaping. I don't really understand much how this works , but I'm going to document a bit in the hope that people can improve it.  
+This describes traffic shaping under linux . This uses the __tc__ (traffic  
+control) program from the iproute package.  
+  
+Make sure you have the correct kernel support for "QoS". In your  
+LinuxKernel .config file, you will probably need support for the  
+following:  
+# CONFIG_NET_SCHED  
+# CONFIG_NET_SCH_CBQ (support for the cbq class, used in this script)  
+# CONFIG_NET_SCH_HTB (htb class , not used in this script)  
+# CONFIG_NET_SCH_PRIO (prio class, used in this script)  
+# CONFIG_NET_SCH_SFQ Stochastic Fairness Queueing qdisc, not used  
+# CONFIG_NET_SCH_TBF Token Bucket Filter qdisc, not used in this script  
+# CONFIG_NET_CLS_U32 used for matching in filters?  
+  
+  
+----  
  
 This is a script I use to throttle one machine down to half our ADSL rate. This machine is used for downloading large files (for example .iso's of the latest LinuxDistribution), but we don't want it impacting the rest of our machines. This example was stolen from the Advanced Router HOWTO, and cleaned up a bit by me. 
  
 You run this script on your gateway rate limiting data to your internal client machine(s). 
@@ -37,24 +52,27 @@
  # we're using the "cbq" discipline, and we're saying that the average packet size is 1000 bytes 
  # (probably completely wrong :) Once again this is just a parameter to make it more accurate. 
  $TC qdisc add dev $DEV root handle 1: cbq avpkt 1000 bandwidth $LINERATE 
  
- # Create a class, also(?) a cbq, rate limited to $THROTTLERATE. allot I Think is how much  
- # data they get before they are rate limited . This must be at least the [MTU] (since you can't  
- # send partial packets you must be able to send at least one entire packet).  
- # I don't know what "prio" is about .  
- # bounded means that it cannot exceed this rate, if this was left off, I think it means that 
+ # Create a child class, also a cbq, rate limited to $THROTTLERATE. "__ allot__" is how much data the  
+ # parent class gets from each child class in turn . This must be at least the [MTU] (since you  
+ # can't send partial packets you must be able to send at least one entire packet).  
+ # "__ prio__ " is used when there is more than 1 child class, and both have data in their queue .  
+ # "__ bounded__" means that it cannot exceed this rate, if this was left off, I think it means that 
  # when the link is saturated that this can't use more than $THROTTLERATE and everyone else can 
  # share the rest. An example use of this might be to set $THROTTLERATE to 0 and remove bounded 
  # meaning that everything else can use the link in preference. I think. 
- # Isolated I'm not sure about, I think it means it doesn't interact with any other rules. 
+ # "__isolated__" I'm not sure about, I think it means it doesn't interact with any other rules. 
  $TC class add dev $DEV parent 1: classid 1:1 cbq rate $THROTTLERATE \ 
  allot 1500 prio 5 bounded isolated 
  
- # Add a filter to go into this class
+ # Add a filter to the parent to redirect traffic into the the children classes.  
+ # (in this case, only a single child) . Traffic that doesn't fall into the child class  
+ # will not be rate limited (depending on the parent class's settings, I guess)  
+ #  
  # This uses the "u32" filter which matches based on header fields in the IP packet. 
- # If you want to match on multiple rules you can use "match ip dest $IP src $IP" etc. I don't  
- # know how you do not. I think if you want to do anything interesting with TC you probably want 
+ # If you want to match on multiple rules you can use "match ip dest $IP src $IP" etc.  
+ # I think if you want to do anything interesting with TC you probably want 
  # to use fwmark from iptables(8). 
  $TC filter add dev $DEV parent 1: protocol ip prio 16 u32 \ 
  match ip dst $IP flowid 1:1 
  
@@ -79,38 +97,39 @@
 I use "dst $IP" for 'traffic destined to $IP', if you want traffic *from* an IP use 'src $IP' instead. 
  
 ---- 
  
-I made some adjustments to the above script to split up the ADSL upload in my flat network. This was to ensure that no one person can whore the upload, thus making everything laggy. (I found the wondershaper disappointing) 
+I made some adjustments to the above script to split up the ADSL upload in my flat network. This was to ensure that no one person can whore the upload, which makes everything laggy for everyone else . (I found the wondershaper disappointing) 
  
 Notes: 
 This will not work with masquerading 
+The network connection can still get easily saturated  
  
-#!/bin/sh  
-# List of IPs to have upload throttled  
-IPS=`seq 114 117 | awk '{print "1.2.3."$1}'` 
+ #!/bin/sh  
+ # List of IPs to have upload throttled  
+ IPS=`seq 114 117 | awk '{print "1.2.3."$1}'` 
  
-LINERATE=2mbit  
-THROTTLERATE=14kbps 
+ LINERATE=2mbit  
+ THROTTLERATE=14kbps 
  
-tc qdisc del dev ppp0 root 2>/dev/null 
+ tc qdisc del dev ppp0 root 2>/dev/null 
  
-tc qdisc add dev ppp0 root handle 1: cbq avpkt 1000 bandwidth $LINERATE 
+ tc qdisc add dev ppp0 root handle 1: cbq avpkt 1000 bandwidth $LINERATE 
  
-for IP in $IPS;  
-do  
- echo throttling $IP 
+ for IP in $IPS;  
+ do  
+ echo throttling $IP 
  
- LASTTHING=`echo $IP | cut -d . -f 4` 
+ LASTTHING=`echo $IP | cut -d . -f 4` 
  
- tc class add dev ppp0 parent 1: classid 1:$LASTTHING cbq rate \  
- $THROTTLERATE allot 1500 prio 5 bounded isolated 
+ tc class add dev ppp0 parent 1: classid 1:$LASTTHING cbq rate \  
+ $THROTTLERATE allot 1500 prio 5 bounded isolated 
  
- tc filter add dev ppp0 parent 1: protocol ip prio 16 u32 \  
- match ip src $IP flowid 1:$LASTTHING 
+ tc filter add dev ppp0 parent 1: protocol ip prio 16 u32 \  
+ match ip src $IP flowid 1:$LASTTHING 
  
- tc qdisc add dev ppp0 parent 1:$LASTTHING sfq perturb 10  
-done; 
+ tc qdisc add dev ppp0 parent 1:$LASTTHING sfq perturb 10  
+ done; 
  
 ---- 
 After a bit of fiddling I've managed to get TrafficShaping working on a per protocol (read port) basis