#!/bin/bash
# Script to turn on/off tso attribute for an interface
#   interface is set via ethtool, setting also stored in if-up script which 
#   will get executed if network or system restarted.

# $1 = interface name, $2 = on or off

myName=`basename $0`
LOG=/var/hsc/log/$myName.$$.log
IFUP_DIR=/etc/sysconfig/network/if-up.d
FNAME=$1-settso
    
echo "$(date +%F" "%T.%3N) updateInterfaceTSO invoked for interface=$1, setting tso=$2" > $LOG

if [ ! -d $IFUP_DIR ]; then
    echo "$(date +%F" "%T.%3N) IF-UP script directory ($IFUP_DIR) does not exists, unable to create IF-UP script file" >> $LOG
    echo "$(date +%F" "%T.%3N) exiting with return value $$" >> $LOG
    exit 1
fi
    
echo "$(date +%F" "%T.%3N) running ethtool to set tso value for interface" >>  $LOG
ethtool -K $1 tso $2
rc=$?
echo "$(date +%F" "%T.%3N) ethtool return code=$rc" >> $LOG

if [ $rc -eq 0 ]; then
    # verify if-up tso script file exists for interface
    if [ ! -e $IFUP_DIR/$1-settso ]; then
        echo "$(date +%F" "%T.%3N) using touch to create file $IFUP_DIR/$1-settso" >> $LOG
        touch $IFUP_DIR/$1-settso
        rc=$?
        if [ $rc -ne 0 ]; then
            echo "$(date +%F" "%T.%3N) creation of /etc/sysconfig/network/if-up.d/$1-settso failed, rc=$rc" >> $LOG
            exit $rc
        fi
        #set file permissions
        chmod 755 /etc/sysconfig/network/if-up.d/$1-settso
    fi

    # write script text to file
    echo "$(date +%F" "%T.%3N) writing ethtool command to if-up script file" >> $LOG
    echo "ethtool -K $1 tso $2" > /etc/sysconfig/network/if-up.d/$1-settso 
    rc=$?
    echo "$(date +%F" "%T.%3N) result from echo command=$rc" >> $LOG
   
fi

echo "$(date +%F" "%T.%3N) exiting updateInterfaceTSO script, rc=$rc" >> $LOG
exit $rc
