#!/bin/sh
# DF-0271 race trigger: hammer add/remove of tap0 from bridge0 while injecting
# frames destined to the bridge MAC.  Run as root.
set +e
cd "$(dirname "$0")"

BRIDGE=bridge0
TAP=tap0
# bridge MAC (populated by run.sh before calling this)

# bring tap up and add to bridge
ifconfig $TAP up
ifconfig $BRIDGE addm $TAP 2>/dev/null
ifconfig $BRIDGE up

BRMAC=$(ifconfig $BRIDGE | grep 'ether ' | awk '{print $2}')
echo "[*] bridge $BRIDGE mac=$BRMAC tap=$TAP"

# background injector (rapid frame injection)
./bridge_race /dev/$TAP $BRMAC 200000 &
INJ=$!

# race: rapidly delete + re-add the member while packets flow
i=0
while kill -0 $INJ 2>/dev/null; do
	ifconfig $BRIDGE deletem $TAP 2>/dev/null
	ifconfig $TAP up 2>/dev/null
	ifconfig $BRIDGE addm $TAP 2>/dev/null
	i=$((i+1))
done
echo "[*] ran $i delete/re-add cycles"
wait $INJ 2>/dev/null
echo "RACE_DONE"
