From e06b5b197144e425168ab8e75cfd5f48ead01d83 Mon Sep 17 00:00:00 2001 From: teastep Date: Wed, 10 May 2006 16:21:31 +0000 Subject: [PATCH] Detect MTU for tc rather than specify in tcdevices file git-svn-id: https://shorewall.svn.sourceforge.net/svnroot/shorewall/trunk@3900 fbd18981-670d-0410-9b5c-8dc0c1a9a2bb --- Shorewall/changelog.txt | 2 +- Shorewall/functions | 70 +++++++++++++++++++++++++++----------- Shorewall/releasenotes.txt | 6 ++-- Shorewall/tcdevices | 5 +-- 4 files changed, 55 insertions(+), 28 deletions(-) diff --git a/Shorewall/changelog.txt b/Shorewall/changelog.txt index 203e7571d..f79cb79b8 100644 --- a/Shorewall/changelog.txt +++ b/Shorewall/changelog.txt @@ -4,7 +4,7 @@ Changes in 3.2.0 Beta 7 2) Restore traffic control to 'refresh'. -3) Add MTU column to /etc/shorewall/tcdevices. +3) Detect MTU for entries in /etc/shorewall/tcdevices. Changes in 3.2.0 Beta 6 diff --git a/Shorewall/functions b/Shorewall/functions index fdc418b31..d313fd842 100644 --- a/Shorewall/functions +++ b/Shorewall/functions @@ -978,6 +978,17 @@ find_gateway() { done } +# +# Find the value 'mtu' in the passed arguments then echo the next value +# + +find_mtu() { + while [ $# -gt 1 ]; do + [ "x$1" = xmtu ] && echo $2 && return + shift + done +} + # # Find the value 'peer' in the passed arguments then echo the next value up to # "/" @@ -1499,6 +1510,20 @@ verify_mark() # $1 = value to test verify_mark2 $1 || fatal_error "Invalid Mark or Mask value: $1" } +# +# Detect a device's MTU +# +get_device_mtu() # $1 = device +{ + local output=$(ip link ls dev $1 2> /dev/null) + + if [ -n "$output" ]; then + echo $(find_mtu $output) + else + echo 1500 + fi +} + # # Arne Bernin's 'tc4shorewall' # @@ -1534,15 +1559,8 @@ setup_traffic_shaping() } calculate_quantum() { - local rate - rate=$1 - rate=$(rate_to_kbit $rate) - rate=$(expr $rate \* 128 / $r2q ) - if [ $rate -lt $mtu ] ; then - echo $mtu - else - echo $rate - fi + local rate=$(rate_to_kbit $1) + echo $(( $rate * ( 128 / $r2q ) )) } # get given outbandwidth for device @@ -1655,12 +1673,14 @@ setup_traffic_shaping() save_command qt tc qdisc del dev $device ingress fi - [ x${mtu:--} = x- ] && mtu=1500 - - eval ${dev}_mtu=$mtu - run_tc qdisc add dev $device root handle $devnum: htb default 1$defmark - run_tc class add dev $device parent $devnum: classid $devnum:1 htb rate $outband mtu $mtu + + if [ $COMMAND = compile ]; then + run_tc "class add dev $device parent $devnum: classid $devnum:1 htb rate $outband mtu \$(get_device_mtu $device)" + else + run_tc class add dev $device parent $devnum: classid $devnum:1 htb rate $outband mtu $(get_device_mtu $device) + fi + run_tc qdisc add dev $device handle ffff: ingress run_tc filter add dev $device parent ffff: protocol ip prio 50 u32 match ip src 0.0.0.0/0 police rate ${inband} burst 10k drop flowid :1 eval ${dev}_devnum=$devnum @@ -1668,7 +1688,7 @@ setup_traffic_shaping() } add_tc_class() { - local full classid tospair tosmask + local full classid tospair tosmask quantum full=$(get_outband_for_dev $device) full=$(rate_to_kbit $full) @@ -1692,7 +1712,6 @@ setup_traffic_shaping() dev=$(chain_base $device) eval devnum=\$${dev}_devnum - eval mtu=\$${dev}_mtu # # Convert HEX/OCTAL mark representation to decimal # @@ -1702,7 +1721,18 @@ setup_traffic_shaping() [ -n "$devnum" ] || fatal_error "Device $device not defined in $devfile" - run_tc class add dev $device parent $devnum:1 classid $classid htb rate $rate ceil $ceil prio $prio mtu $mtu quantum $(calculate_quantum $rate) + quantum=$(calculate_quantum $rate) + + if [ $COMMAND = compile ]; then + save_command "mtu=\$(get_device_mtu $device)" + save_command "[ \$mtu -gt $quantum ] && quantum=\$mtu || quantum=$quantum" + run_tc "class add dev $device parent $devnum:1 classid $classid htb rate $rate ceil $ceil prio $prio mtu \$mtu quantum \$quantum" + else + mtu= $(get_device_mtu $device) + [ $mtu -gt $quantum ] && quantum=$mtu + run_tc class add dev $device parent $devnum:1 classid $classid htb rate $rate ceil $ceil prio $prio mtu $mtu quantum $quantum + fi + run_tc qdisc add dev $device parent $classid handle 1$mark: sfq perturb 10 # add filters if [ -n "$CLASSIFY_TARGET" ]; then @@ -1741,9 +1771,9 @@ setup_traffic_shaping() [ $COMMAND = compile ] && save_progress_message "Setting up Traffic Control..." progress_message2 "$DOING $devfile..." - while read device inband outband mtu; do - expandv device inband outband mtu - tcdev="$device $inband $outband $mtu" + while read device inband outband; do + expandv device inband outband + tcdev="$device $inband $outband" add_root_tc progress_message_and_save " TC Device $tcdev defined." done < $TMP_DIR/tcdevices diff --git a/Shorewall/releasenotes.txt b/Shorewall/releasenotes.txt index 6d6cc60f0..a1dfa0554 100644 --- a/Shorewall/releasenotes.txt +++ b/Shorewall/releasenotes.txt @@ -36,9 +36,9 @@ Other changes in 3.2.0 Beta 7 1) 'shorewall refresh' once again refreshes the tcrules and traffic shaping. -2) An MTU column has been added to /etc/shorewall/tcdevices that allows - specification of the MTU to use for traffic-shaping. If not specified or - if specified as '-' then 1500 is assumed. +2) Shorewall will now attempt to detect the MTU of devices listed in + /etc/shorewall/tcdevices and will use the detected MTU in setting + up traffic shaping. Migration Considerations: diff --git a/Shorewall/tcdevices b/Shorewall/tcdevices index 4ac60981a..59b22bfe7 100644 --- a/Shorewall/tcdevices +++ b/Shorewall/tcdevices @@ -51,9 +51,6 @@ # speed, and make sure there is NO space between the # number and the unit. # -# MTU The device's MTU. If not specified, a value of 1500 -# is assumed. -# # Example 1: Suppose you are using PPP over Ethernet (DSL) # and ppp0 is the interface for this. The # device has an outgoing bandwidth of 500kbit and an @@ -62,5 +59,5 @@ # # ############################################################################### -#INTERFACE IN-BANDWITH OUT-BANDWIDTH MTU +#INTERFACE IN-BANDWITH OUT-BANDWIDTH #LAST LINE -- ADD YOUR ENTRIES BEFORE THIS ONE -- DO NOT REMOVE