#!/bin/bash

set -xe

bond_mode=${1:-802.3ad}

# cleanup
nmcli connection delete bond0 veth0 veth1 || :
dhclient -r bond0 || :
killall dnsmasq || :
killall iperf3 || :
ip netns del ns1 || :
ip link del bond0 || :
ip link del bond1 || :
ip link del veth0 || :
ip link del veth1 || :
sleep 1

# create local interfaces
ip link add bond0 type bond mode "$bond_mode"
ip link add veth0 type veth peer veth0_p
ip link add veth1 type veth peer veth1_p

# create interfaces on the other side (other netns)
ip netns add ns1
ip -n ns1 link add bond1 type bond mode "$bond_mode"
ip link set veth0_p netns ns1
ip link set veth1_p netns ns1
ip -n ns1 link set veth0_p master bond1
ip -n ns1 link set veth1_p master bond1
ip -n ns1 link set bond1 up
ip -n ns1 addr add 192.168.199.2/24 dev bond1

# put local interfaces up
ip link set veth0 master bond0
ip link set veth1 master bond0
ip link set bond0 up
ip addr add 192.168.199.1/24 dev bond0
sleep 1

# check
ip netns exec ns1 iperf3 -s &
sleep 1
iperf3 -c 192.168.199.2 --udp --parallel 8 
