Skip to main content

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

Comments

Post a Comment

On topic comments are welcome. Junk will be deleted.

Popular posts from this blog

PCB Mill

PCB Mill Kit My latest toy is a small PCB Mill, a CNC 3018 Pro, there are many available from Ali Express for the enormous sum of 285 Dirhams or so, which is about 70 Euro.  I thought that even if it didn't work at all, it would not be a big loss. Assembled CNC 3018 Kit It will help if you have a little previous workshop experience, but these machines are so simple and relatively slow moving, that any radio-geek can safely experiment. Carving With a V-bit in a Puddle of Oil Of course I can have boards made in China by Dirty PCBs , but what is the fun in that? The problem with making PCB antennas, is that you need to experiment to change the design 1 mm this way or that, to tune it just so and just such and having to wait 2 weeks for each experiment doesn't work.  A few hours playing with a router is more practical. It turned out to be a pretty nice little kit, made from aluminium and 1/4 inch Bakelite (paper reinforced phenol formaldehyde).  This Pre...

OpenEMS with Octave and SciLAB

I wanted to do some advanced RF antenna development work and needed an electromagnetic field solver that is a bit more up to date than NEC2 .  Commercial solvers from Matlab , Ansys and others are hideously expensive (in the order of $20,000 to $50,000) and do not fit in the wallet of a hobbyist or a small consulting company.  Recently, openEMS became available and it fills the niche with a capable free tool.  In general, openEMS is a solver - a Finite-Difference Time-Domain (FDTD) numerical engine.  You interact with it through Octave , which is almost identical to Matlab .  You can watch a good video by Thorsten Liebig here: https://www.youtube.com/watch?app=desktop&v=ThMLf0d5gaE   Getting it to work is a little painful, but it is free, so bear with it - then save a backup clone, or a zipped copy of the whole virtual machine directory and NEVER update it, to ensure that it keeps going and doesn't get broken by future updates, right when you are ...

Yagi Antenna for 900 MHz ISM Band

I like tinkering with wire antenna designs, since they are simple and cheap to make.  Mr Yagi invented his antenna about 100 years ago, but there are still some things left to learn about it. 900 MHz ISM Band Yagi The 900 MHz ISM band ranges from 902 to 928 MHz.  Covering the whole band with a single Yagi antenna is difficult, since they are inherently narrow band devices.  Consequently some tweaking is required and the result below is a desensitized design that can be built and replicated quite easily, but you need a network analyzer - "To Measure, is to Know!" A Yagi generally consists of a Reflector, Radiator and one or more Director elements, arranged on a boom.  For a small Yagi, a wooden ruler works a treat, since one can easily mark the position of the wires.  The wire elements are fastened to the bottom of the ruler with hot glue.  The wire elements are  made from straightened out jumbo size paper clips.  The balun, is tw...