Monday, February 15, 2016

Network Emulator

In my experience, it doesn't help telling developers that a radio data link is inherently slow and unreliable and that it gets worse with increasing distance.  They will always design for the best case - 1 meter of copper wire in a lab - and then be all upset when it doesn't work so well in reality.

The solution is to make them a configurable network emulator from an old laptop PC (with a USB ethernet adaptor for a second port), put it between two of their machines and then stand back at a safe distance from any nerf guns or rubber band rifles and watch the wheels fall off the software.

This network torture tool uses netem and bridge-utils to create a transparent bridge between two ethernet ports.  This cruel script is prettied up with Zenity, so that one can use sliders to vary the delay and packet loss.

Either make the network utilities SUID root, or run the script as root.

#! /bin/bash
# Network Emulator
# Version 0.1, Copyright GPL, Feb 2016, Herman Oosthuysen
# Depends upon: zenity, ebtables, bridge-utils, netem
# SUID root: systemctl, killall, ethtool, ifconfig, brctl, iptables, ebtables, tc

# Configuration
export PORT0="eth0"
export PORT1="eth1"
export BR0="br0"
export SPEED="10"
export DUPLEX="full"
export IP0="0.0.0.0"
export IP1="192.168.1.1"
export MSK="255.255.255.0"
export DELAY="0"
export LOSS="0"
export RETURN="0"

zenity --question \
  --width=350 \
  --title="Network Emulator" \
  --text="Continue?"
 
if [ "$?" == "1" ]; then
  echo "Cancel"
  exit 0
fi


# First of all, disable NetworkManager and dhclient, 
# to prevent arguments over control of the ports.
systemctl stop NetworkManager
systemctl disable NetworkManager
killall dhclient
 
echo "Create a transparent bridge $BR0"
brctl addbr $BR0
brctl stp $BR0 off
brctl addif $BR0 $PORT0
brctl addif $BR0 $PORT1

echo "Full duplex, $SPEED bps"
ifconfig $PORT0 up
ethtool $PORT0
ethtool -s $PORT0 speed $SPEED duplex $DUPLEX autoneg off

ifconfig $PORT1 up
ethtool $PORT1
ethtool -s $PORT1 speed $SPEED duplex $DUPLEX autoneg off

echo "Enable IP4 forwarding"
ifconfig $PORT0 $IP0 promisc up
ifconfig $PORT1 $IP0 promisc up
echo "1" > /proc/sys/net/ipv4/ip_forward

# Give the bridge a pingable address
echo "Bridge IP = $IP1, Netmask = $MSK"
ifconfig $BR0 $IP1 netmask $MSK up

echo "Open iptables and ebtables to allow everything, INPUT, OUTPUT and FORWARD" 
iptables -F
iptables -P INPUT ACCEPT
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT

ebtables -F
ebtables -P INPUT ACCEPT
ebtables -P OUTPUT ACCEPT
ebtables -P FORWARD ACCEPT


# Create an initial rule
tc qdisc add dev $BR0 root netem delay $DELAYms 10ms 25%

(
while TRUE; do
  # Update the progress bar
  echo "#DELAY=$DELAY LOSS=$LOSS"
 
  # Get the packet delay in ms
  DELAY=$(zenity --scale \
    --text="Packet Delay milliseconds" \
    --value="0" \
    --min-value="0" \
    --max-value="100" \
    --step="1")
   
  # tc qdisc change dev eth0 root netem delay 100ms 10ms 25%
  tc qdisc change dev $BR0 root netem delay $DELAYms 10ms 25%
 
  # Update the progress bar
  echo "#DELAY=$DELAY LOSS=$LOSS"
 
  # Get the packet loss in 1/x%
  LOSS=$(zenity --scale \
    --text="Packet Loss Fraction %" \
    --value="0" \
    --min-value="0" \
    --max-value="100" \
    --step="1")
   
  LOSS=1/$LOSS
  # tc qdisc change dev eth0 root netem loss 0.3% 25%
  tc qdisc change dev $BR0 root netem loss $LOSS% 25%
 
  # Update the progress bar
  echo "#DELAY=$DELAY LOSS=$LOSS"
 
  zenity --question \
    --width=350 \
    --title="Network Emulator" \
    --text="Continue?"
   
  if [ "$?" == "1" ]; then
    echo "Cancel"
    echo "100"
    exit 0
  fi
done
) | zenity --progress \
  --width=350 \
  --title="Network Emulator" \
  --text="Running..." \
  --no-cancel \
  --auto-close \
  --pulsate

echo "Done!"
exit 0


La voila!

Herman

No comments:

Post a Comment

On topic comments are welcome. Junk will be deleted.