--- X:/qos_gargoyle.ORIG	Tue Jun 04 09:56:53 2013
+++ X:/qos_gargoyle	Tue Jun 04 10:04:09 2013
@@ -301,6 +301,11 @@
 	insmod cls_fw >&- 2>&-
 	insmod cls_flow >&- 2>&-
 	insmod sch_hfsc >&- 2>&-
+	
+	# I'm using target but it's not mandatory. I got this number by doing "traceping" and it is a close match to qosmon (42ms) so qosmon COULD be used to feed back this information
+	AVERAGE_RTT=40
+	# a little over 2 packets just to be safe
+	CODEL_bytequeuelimit="3000"
 	insmod sch_sfq >&- 2>&-
 
 	#Set the atm parameters if pppoe is being used.
@@ -308,7 +313,7 @@
 	if [ "$wan_proto" = "pppoe" ] ; then
 		#On my PPPoE WAN link I see the following overhead
 		#MAC Header=14 & PPPoE=8 plus I read that an addition ATM=8 is added by the modem"
-		overhead="stab linklayer atm overhead 30 mtu 1492 tsize 128 "
+		overhead="stab linklayer atm overhead 38 mtu 1492 tsize 128 "
 	#else
 		#Even for the ethernet cases I do not think the qdisc sees the MAC Header
 		#but for now leave this out.
@@ -357,6 +362,10 @@
 			eval $uclass_name=\"\$classdef\"  #"#comment quote here so formatting in editor isn't FUBAR
 		done
 
+		for I in /sys/class/net/$qos_interface/queues/tx-*/byte_queue_limits/limit_max
+		do
+			echo $CODEL_bytequeuelimit > $I
+		done
 
 		# Attach egress queuing discipline to QoS interface, now with temperary default
 		$echo_on
@@ -422,8 +431,10 @@
 			
 			#Add the leaf class
 			tc class add dev $qos_interface parent 1:1 classid 1:$next_class_index hfsc ls m2 ${m2}Mbit $ll_str $ul_str
+
 			#Add the qdisc to the leaf class, assuming average packet at 500 bytes.
-			tc qdisc add dev $qos_interface parent 1:$next_class_index handle $next_class_index:1 sfq headdrop limit $(($tbw/500)) $sfq_depth divisor 256  
+			tc qdisc add dev $qos_interface parent 1:$next_class_index handle $next_class_index:1 fq_codel limit $(($tbw/500)) flows 256 quantum 500 target $((${AVERAGE_RTT}*1000)) ecn
+
 			#Add a filter to the root class to direct packets to this leaf class according to the conntrack mark
 			tc filter add dev $qos_interface parent 1:0 protocol ip handle $next_classid fw flowid 1:$next_class_index
 			#Add a filter to the leaf class to define flows as being the source IP address.
@@ -485,6 +497,12 @@
 	fi
 
 	if [ $total_download_bandwidth -ge 0 ] ; then
+
+		for I in /sys/class/net/imq0/queues/tx-*/byte_queue_limits/limit_max
+		do
+			echo ${CODEL_bytequeuelimit} > $I
+		done
+	
 		# Set up the InterMediate Queuing device (IMQ)
 		ip link set imq0 up
 
@@ -571,8 +589,9 @@
 
 			$echo_on
 			tc class add dev imq0 parent 1:1 classid 1:$next_class_index hfsc $rt_str $ll_str $ul_str
+			
 			#Assume average download packet size is 1000 bytes.
-			tc qdisc add dev imq0 parent 1:$next_class_index handle $next_class_index:1 sfq headdrop limit $(($tbw/1000)) $sfq_depth divisor 256  
+			tc qdisc add dev imq0 parent 1:$next_class_index handle $next_class_index:1 fq_codel limit $(($tbw/1000)) flows 256 quantum 1000 ecn	
 			tc filter add dev imq0 parent 1:0 prio $next_class_index protocol ip handle $next_classid fw flowid 1:$next_class_index
 			tc filter add dev imq0 parent $next_class_index: handle 1 flow divisor 256 map key dst and 0xff
 			$echo_off
