What I Learned Today
iptables dropping traffic

Here is a simple rule to drop traffic from a source using linux iptables

sudo iptables -A INPUT -s 192.168.247.1 -j DROP

to remove the rule at a later time, run:

sudo iptables -D INPUT -s 192.168.247.1 -j DROP

Emulate network latency using tc

Use this code:

PATH=$PATH:/sbin
lat=$1  ## desired bandwidth in megabits per sec
destip=$2  ## destination host to limit traffic to

if [[ -z $lat ]]
then
        lat=100
fi

if [[ -z $destip ]]
then
        echo "using default IP: 192.168.245.21"
        destip=192.168.245.21
fi

echo "note this may not work for VMs since it uses eth instead of peth0"

tc qdisc del dev eth0 root

tc qdisc add dev eth0 root handle 1: prio
tc qdisc add dev eth0 parent 1:3 handle 30: netem \
     delay ${lat}ms 
## add to end of previous line for distribution: 10ms distribution normal

## I think the line below is for bandwidth
#tc qdisc add dev eth0 parent 30:1 tbf rate 20kbit buffer 1600 limit 3000

tc filter add dev eth0 protocol ip parent 1:0 prio 3 u32 \
     match ip dst $destip/32 flowid 1:3

(Source: linuxfoundation.org)

Bind Address in Use error

I was getting this error when trying to start tomcat:

java.net.BindException: Address already in use:8080
LifecycleException:  Protocol handler initialization failed: java.net.BindException: Address already in use:8080

To fix this, I followed these instructions:

  1. Find the zombie process holding the port open: netstat -anp | grep 8080
  2. use kill -9 to stop it
Using TC to limit bandwidth

This code will limit the bandwidth between the source host and the destination IP set.



bw=$1  ## desired bandwidth in megabits per sec
destip=$2  ## destination host to limit traffic to
tc qdisc del dev peth0 root
tc qdisc add dev peth0 root handle 1: htb
tc class add dev peth0 parent 1: classid 1:1 htb rate ${bw}mbit ceil ${bw}mbit
tc class add dev peth0 parent 1:1 classid 1:11 htb rate ${bw}mbit ceil ${bw}mbit
tc filter add dev peth0 protocol ip parent 1:0 prio 1 u32 match ip dst $destip flowid 1:11

Set IP address in CentOS

To set a static IP address (ie for a new VM from stacklet.com)

nano /etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE=eth0
BOOTPROTO=none
ONBOOT=yes
TYPE=Ethernet
IPADDR=192.168.246.30
NETMASK=255.255.255.0
GATEWAY=192.168.246.254

Then: ifdown eth0; ifup eth0

Or, to run from the command line:

ifconfig eth0 192.168.99.14 netmask 255.255.255.0 up

To configure name server:

cat > /etc/resolv.conf
search local cs.umass.edu
nameserver 128.119.240.1