#!/bin/bash
# Net configuration tweaking for GigeVision cameras.
# Adjustable configuration parameters.

maxCameras=1
imageSize=$((8192*4096))

# Figure out a decent (supported) MTU here.
# (Genie camera max is 9152 - so start at 9216 and work downwards by 128).
networkLists=(eth0)
mtuLists=(1500)
listNumber=${#mtuLists[@]}

# Check for ifconfig (deprecated)
# (use iproute2 tools if ifconfig not present).
IPCONFIG=$(which ifconfig)
if [[ $IPCONFIG ]] ; then
   # Find availabe net interfaces (with ifconfig)
   availableNetif=`$IPCONFIG -s | sed 1d | awk "(\\$1 != \"lo\") { printf \\$1 \" \"}"`
else
   # Find availabe ipv4 net interfaces (with ip link show  - from iproute)
   IPTOOL=$(which ip)
   if [[ $IPTOOL ]] ; then
      availableNetif=`$IPTOOL -4 link show | cut -d" " -f2 | awk "(\\$1 != \"lo:\") { printf \\$1 \" \"}" | sed "s/[:,]//g"`
   else
      echo " "
      echo "Neither ifconfig (from net-tools) or ip (from iproute2) are installed"
      echo "Install one of these to properly configure network devices"
      exit 1
   fi
fi


# Check if root previlige
if [ `id -u` -ne 0 ]; then
    echo "Please run this program as root."
    exit 1
fi

SetNetMtu()
{
    # Figure out a decent (supported) MTU here.
    # (Genie camera max is 9152 - so start at 9216 and work downwards by 128).
    # netifMtu=9216
    netifMtuDec=128
    status=0
    while [ $status -eq 0 ] 
    do
        if [[ $IPCONFIG ]] ; then
            result="$($IPCONFIG $netifName down mtu $netifMtu up 2>&1)"
        else
            result="$($IPTOOL link set mtu $netifMtu dev $netifName 2>&1)"
        fi
        if [ "$result" != "" ] ; then
            ((netifMtu-=netifMtuDec))
        else
            status=1
        fi
    done

    # Make sure that 1500 is the minimum mtu.
    if [ $netifMtu -lt 1500 ] ; then
        netifMtu=1500
    fi

    echo "mtu setting for $netifName is now $netifMtu"
    # Adjust the MTU for jumbo frames (9152 maximum)
    if [[ $IPCONFIG ]] ; then
        $IPCONFIG $netifName mtu $netifMtu
    else
        $IPTOOL link set mtu $netifMtu dev $netifName
    fi
}

SetNetConfig()
{
    #Adjust the minimum receive buffer allocation
    sysctl -w net.ipv4.udp_rmem_min=12288

    #Adjust the network device backlog here
    backlogmax=4096
    backlog=$(cat /proc/sys/net/core/netdev_max_backlog)
    if [ "$backlog" != "" ] ; then
        if [ $backlog -lt $backlogmax ] ; then
            newbacklog=$backlogmax
        else
            newbacklog=$backlog
        fi
    else
        newbacklog=$backlogmax
    fi 
    sysctl -w net.core.netdev_max_backlog=$newbacklog

    # Adjust the queue length for UDP packets
    qlength=$(( 2*maxCameras*(imageSize / netifMtu) ))
    sysctl -w net.unix.max_dgram_qlen=$qlength

    # Adjust the default (and maximum) memory for receiving network packets
    defrmem=$((imageSize))
    maxrmem=$((2*imageSize))
    rmemdefault=$(cat /proc/sys/net/core/rmem_default)
    rmemmax=$(cat /proc/sys/net/core/rmem_max)
    if [ $rmemmax -lt $maxrmem ] ; then
        sysctl -w net.core.rmem_max=$maxrmem
    fi
    if [ $rmemdefault -lt $defrmem ] ; then
        sysctl -w net.core.rmem_default=$defrmem
    fi
}

# Use "ethtool" to adjust the setting of the network device drivers
# to optimize the rx_ring for maximum throughput. We need this to
# receive image data packets from the cameras. (Sending to the camera
# is not as critical)
# ETHTOOL="/usr/sbin/ethtool -g $(netif_name)"
SetEthTool()
{
	ETHTOOL=$(which ethtool)
	if [[ $ETHTOOL ]] ; then
    	if [ -x $ETHTOOL ] ; then
        	RX_JUMBO=`$ETHTOOL -g $netifName  | awk "\\$2 == \"Jumbo:\" { print \\$3 }" -`
        	RX_JUMBOMAX=`echo $RX_JUMBO | awk """ { print \\$1 }" -`
        	RX_JUMBOCUR=`echo $RX_JUMBO | awk """ { print \\$2 }" -`
        	if [ "$RX_JUMBOMAX" != "$RX_JUMBOCUR" ] ; then
            	if [ "$RX_JUMBOMAX" != "" ] ; then
                	$ETHTOOL -G $netifName rx-jumbo $RX_JUMBOMAX
            	fi
        	fi
        	RX_VALUE=`$ETHTOOL -g $netifName  | awk "\\$1 == \"RX:\" { print \\$2 }" -`
        	RX_MAXIMUM=`echo $RX_VALUE | awk """ { print \\$1 }" -`
        	RX_CURRENT=`echo $RX_VALUE | awk """ { print \\$2 }" -`
        	if [ "$RX_MAXIMUM" != "$RX_CURRENT" ] ; then
            	if [ "$RX_MAXIMUM" != "" ] ; then
                	$ETHTOOL -G $netifName rx $RX_MAXIMUM
            	fi
        	fi
        	# Turn off PAUSE on rx operations
        	$ETHTOOL -A $netifName rx off
    	fi
	else
    	echo "*** Unable to adjust NIC driver settings ***"
    	echo "*** ethtool utility not found  ***"
	fi
}

# Network mtu config.
for ((i =0; i < $listNumber;i++));
do
    netifName=${networkLists[i]}
    netifMtu=${mtuLists[i]}
	echo ${networkLists[i]}
	echo ${mtuLists[i]}

	present=`echo $availableNetif | grep $netifName`
    if [ "$present" == "" ] ; then 
        echo "Error: net if $netifName : Not Present"
	else
		SetNetMtu
		SetEthTool
    fi
done

SetNetConfig

echo "Run completed!"
exit 0





