2025-12-10 00:01:32 +08:00

169 lines
4.8 KiB
Bash

#!/bin/bash
# Net configuration tweaking for GigeVision cameras.
# Adjustable configuration parameters.
maxCameras=1
imageSize=$((8192*4096))
netifMtu=1500
# 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
netifMtu=9216
netifMtuDec=128
status=0
while [ $status -eq 0 ]
do
if [[ $IPCONFIG ]] ; then
result="$($IPCONFIG $netif_name down mtu $netifMtu up 2>&1)"
else
result="$($IPTOOL link set mtu $netifMtu dev $netif_name 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 $netif_name is now $netifMtu"
# Adjust the MTU for jumbo frames (9152 maximum)
if [[ $IPCONFIG ]] ; then
$IPCONFIG $netif_name mtu $netifMtu
else
$IPTOOL link set mtu $netifMtu dev $netif_name
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 $netif_name | 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 $netif_name rx-jumbo $RX_JUMBOMAX
fi
fi
RX_VALUE=`$ETHTOOL -g $netif_name | 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 $netif_name rx $RX_MAXIMUM
fi
fi
# Turn off PAUSE on rx operations
$ETHTOOL -A $netif_name rx off
fi
else
echo "*** Unable to adjust NIC driver settings ***"
echo "*** ethtool utility not found ***"
fi
}
# Check the net interface number.
if [ $# -eq 0 ] ; then
echo "Usage : $0 <netif_name>"
echo " e.g. $0 eth0"
echo " Available net if names : $availableNetif"
exit 1
else
netif_name=$1
present=`echo $availableNetif | grep $netif_name`
if [ "$present" == "" ] ; then
echo "Error: net if $1 : Not Present"
echo " Available net if names : $availableNetif"
exit 1
fi
fi
SetNetMtu
SetNetConfig
SetEthTool
echo "Run completed!"
exit 0