#!/bin/sh

# Robust bufferbloat testing script - testing the effects of cubic, bbr,
# udp vs tcp, v4vsv6, TOS bits, and other things against a given ISP's network.
# Requirements: flent, irtt, netperf. https://flent.org - apt-get flent irtt
# Sometimes you need to compile netperf yourself from git with --enable-demo
# Flent packages available via most linux repositories, instructions for osx
# and other OSes on the website. Getting flent-gui running takes extra packages

# if you change this from run to run, changing some variable, you
# get easily comparable data in flent-gui. Useful things to vary are ecn on/off
# various settings for the policer or SQM, different cpe, wifi vs lte, etc, etc

#T=fiber-1gbit-sym # test run title with parameters under test
T=starlink-wifi2-cake-up # test run title with parameters under test

# CONG Must be in tcp_allowed_congestion_control on BOTH sides,
# and you must be root

CONG="bbr cubic cdg westwood dctcp" # congestion controls to test. 

# FLOWS="1 2 3 4 8 16 32 64 128 256 512 1024" # Number of flows to test
FLOWS="1 2"

# elide the -x for less metadata
# use a -4 or -6 if you wish to explicitly test ipv4 or ipv6. 

FOPS="--step-size=.05 --socket-stats -x"
NO6=1 # enable/disable ipv6
ODDTESTS=1 # Enable some more unusual tests, like rrul-voip

IF=enp0s25 # ethernet interface to capture on

# TCPDUMP="tcpdump -i $IF -s 128" # packet caps can heisenbug the tests
# KD="killall tcpdump; sleep 2"
# You don't need root to run this test suite if you aren't capturing

TCPDUMP=/bin/true # off by default
KD=/bin/true # off by default

# set this to a netperf server local or with mininmal hops to your network
H=dallas.starlink.taht.net # Preferably not this, but it's ok if you do. 

# Other servers around the world include de, london, singapore, dallas, atlanta
# Pick 4 at wildly varying baseline RTTs, these are good to a 1.2 gbit each.

H1=sydney.starlink.taht.net
H2=fremont.starlink.taht.net
H3=de.starlink.taht.net
H4=atlanta.starlink.taht.net

# you should't need to modify anything after this line

D=`date --utc +%F-%T`
PT=$T-`date --utc +%T`

# Misc interesting tests

if [ "$ODDTESTS" -eq "1" ]
then
$TCPDUMP -w $PT-mixed.cap &
sleep 2
flent $FOPS -t "$T" -H $H bursts_11e
flent $FOPS -t "$T" -H $H iterated_bidirectional
flent $FOPS -t "$T" -H $H tcp_2up_delay
flent $FOPS -t "$T" -H $H tcp_4up_squarewave
flent $FOPS -t "$T" -H $H rrul_cs8
flent $FOPS -t "$T" -H $H cubic_reno
flent $FOPS -t "$T" -H $H cubic_bbr
flent $FOPS -t "$T" -H $H -H $H1 -H $H2 -H $H3 -H $H4 cisco_5tcpup_2udpflood 

if [ "$NO6" -eq 0 ]
then
       flent $FOPS -t "$T" -H $H rrul46compete
       flent $FOPS -t "$T" -H $H rrul46
fi

# The http tests require MUCH more setup
# flent $FOPS -t "$T" -H $H http
# flent $FOPS -t "$T" -H $H http-1down
# flent $FOPS -t "$T" -H $H http-1up
# flent $FOPS -t "$T" -H $H http-rrul

$KD

fi

# P2 - Test RTT + Bandwidth Fairness worldwide

$TCPDUMP -w $PT-rtt_fair.cap &
sleep 2

for C in $CONG
do
    flent -l 30 $FOPS -t $T-$C-f=5 -H $H -H $H1 -H $H2 -H $H3 -H $H4 --test-parameter=cc_algo=$C,$C,$C,$C,$C rtt_fair_var
    flent -l 30 $FOPS -t $T-$C-f=5 -H $H -H $H1 -H $H2 -H $H3 -H $H4 --test-parameter=cc_algo=$C,$C,$C,$C,$C rtt_fair_var_down
    flent -l 30 $FOPS -t $T-$C-f=5 -H $H -H $H1 -H $H2 -H $H3 -H $H4 --test-parameter=cc_algo=$C,$C,$C,$C,$C rtt_fair_var_up
done

$KD

for C in $CONG
do
    $TCPDUMP -w $PT-$C-downloads.cap &
    for i in $FLOWS
    do
	flent -l 60 $FOPS --test-parameter=upload_streams=$i -t $T-f=$i -H $H --test-parameter=cc_algo=bbr --test-parameter=cc_alg=$C --test-parameter=cc=$C --test-parameter=CC=$C tcp_ndown
    done
    $KD
done

    
for C in $CONG
do
    $TCPDUMP -w $PT-$C-uploads.cap &
    sleep 2
    for i in $FLOWS
    do
	flent -l 60 $FOPS --test-parameter=download_streams=$i -t $T-$C-f=$i -H $H --test-parameter=cc_algo=$C --test-parameter=cc_alg=$C --test-parameter=cc=$C --test-parameter=CC=$C tcp_ndown
    done
done

$KD

for C in $CONG
do
    $TCPDUMP -w rrul-$C.cap &
    sleep 2
    flent -l 300 $FOPS -t $T-$C -H $H --test-parameter=cc_algo=$C --test-parameter=cc_alg=$C --test-parameter=cc=$C --test-parameter=CC=$C rrul
    flent -l 300 $FOPS -t $T-$C -H $H --test-parameter=cc_algo=$C --test-parameter=cc_alg=$C --test-parameter=cc=$C --test-parameter=CC=$C rrul_be
$KD
done
