Saturday, June 11, 2011

Script of the Week: Tuning TCP Packets

In order to gain some practice building scripts from the ground up this exercise will provide basic resources to create a script that will monitor and manipulate tcp packets on the server.
1. Background for Tuning TCP Sockets
In an excellent book, “Performance Tuning for Linux Servers”, published by IBM, it documents one of the major issues with many Linux servers, that TCP sockets used for networking have not been optimized in the Linux kernel.  As a result networking performance on high usage servers fails to provide the needed access to the server.  These tuning features of the TCP sockets can have significant increases in speed.  Be sure to test before and after to verify these are doing what you expect.
The  tcp_max_syn_backlog sets the number of TCP SYN packets that the server will queue before they are dropped.  Here you can see the default, this can be increased to 30000.
cat /proc/sys/net/ipv4/tcp_max_syn_backlog
1024

The recommended increase is to 30,000.
echo 30000 > /proc/sys/net/ipv4/tcp_max_syn_backlog
With a web server you will see a lot of TCP connections in the TIME-WAIT state.  TIME_WAIT is when the socket is waiting after close to handle packets  still in the network.  This also should be increased.  Here is the default.
cat /proc/sys/net/ipv4/tcp_max_tw_buckets
180000

This should be updated to 2 million.
echo 2000000 > /proc/sys/net/ipv4/tcp_max_tw_buckets
The number of packets that can be queued should be increased from the default of 1000 to 50000.
cat /proc/sys/net/core/netdev_max_backlog
1000

These should be increased to 50,000.
echo 50000 > /proc/sys/net/core/netdev_max_backlog
2. Create a script that will implement these needed changes to the tcp sockets.  It should have these features:
* header with licensee information
* require root to be able to run script
* check command paths, if incorrect die
* send an email to your email address on completion
* automatically run the script at boot time
3. Debug the script
sh -x script_name

#!/bin/bash
#####################################################################
# The purpose of the script is to optimize tcp packets
#####################################################################
# Variables
ADMIN="admin_email"
CONLOG=/tmp/connections
#Feature Options
SENDMAIL=1
# Command Paths
MAIL=/bin/mail
ID=/usr/bin/id
# Make sure script runs with the EUID of root
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root" 1>&2
exit 1
fi
# Kill if script has problems and log
die(){
echo "$@"
exit 999
}
init(){
[ ! -x $MAIL ] && die "$MAIL command not found."
[ ! -x $ID ] && die "$ID command not found."
>$CONLOG
}
init
echo 30000 > /proc/sys/net/ipv4/tcp_max_syn_backlog
echo "tcp_max_syn_backlog increased to 30,000" > $CONLOG
echo 2000000 > /proc/sys/net/ipv4/tcp_max_tw_buckets
echo "tcp_max_tw_buckets increased to 2 million" >> $CONLOG
echo 50000 > /proc/sys/net/core/netdev_max_backlog
echo "netdev_max_backlog increased to 5000" >> $CONLOG
if [ $SENDMAIL -eq 1 ];
then
$MAIL -s "TCP PACKET MANAGEMENT @ $(hostname)" $ADMIN < $CONLOG
fi
rm -f /tmp/connections
exit 0


http://bashshell.net/script-of-the-week/script-of-the-week-tuning-tcp-packets/