#!/bin/sh
# rl0 functional + stress test on the QEMU-emulated rtl8139 (if_rl.c pass-2 audit)
# Exercises: rl_init/rl_stop (SIOCSIFFLAGS), rl_start -> rl_encap (m_defrag,
# bus_dmamap_load, TX regs), chip TX DMA, rl_intr, rl_rxeof (m_devget both
# branches incl. small + 1400B frames), rl_txeof, rl_tick/rl_stat_timer
# (callout_reset/callout_stop discipline), rl_setmulti, rl_detach/rl_attach
# (kldunload/kldload), and the if_timer watchdog arming paths.
set -x
date
ifconfig rl0 10.0.2.16 netmask 255.255.255.0 up
ifconfig rl0
route add -host 10.0.2.3 -iface rl0
# 1) small-frame TX/RX through the emulated 8139 (UDP DNS to slirp's virtual DNS)
nslookup -timeout=5 dragonflybsd.org 10.0.2.3
# 2) larger frames: EDNS0 buf 1400 -> 1400+ byte UDP out rl0 and back
dig +time=5 +tries=1 +bufsize=1400 @10.0.2.3 example.com >/dev/null 2>&1 || \
  drill -b 1400 @10.0.2.3 example.com >/dev/null 2>&1 || true
# 3) medium flood: 300 DNS round trips
i=0
while [ $i -lt 300 ]; do
  nslookup -timeout=2 localhost 10.0.2.3 >/dev/null 2>&1
  i=$((i+1))
done
echo "DNS_FLOOD_DONE rc=$?"
ifconfig rl0
netstat -I rl0 -b
# 4) down/up stress: 300 cycles (rl_stop/rl_init + callout discipline)
j=0
while [ $j -lt 300 ]; do
  ifconfig rl0 down
  ifconfig rl0 up
  j=$((j+1))
done
echo "DOWNUP_DONE"
# 5) let ticks fire while steady (5s at 1Hz tick)
sleep 5
ifconfig rl0
netstat -I rl0 -b
# 6) sanity: traffic still flows after the stress
nslookup -timeout=5 dragonflybsd.org 10.0.2.3
echo "FINAL_SANITY rc=$?"
dmesg | tail -15
date
echo ALL_DONE
