Skip to content

Commit

Permalink
Add scripts for testing and development (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
erthalion authored Aug 2, 2024
1 parent 6766339 commit 5f620d2
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 0 deletions.
81 changes: 81 additions & 0 deletions scripts/network/prepare-tap.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/usr/bin/env bash
set -eou pipefail

# This script helps to prepare an environment for developing berserker network
# workload. It has the following preparatory steps:
# * Create and start up a new tun device for berserker to use
# * Optionally prepare iptables for the device to be visible
#
# The last step is optional, because iptables configuration could be different
# between development environments. Meaning it's not guaranteed that this part of
# the script is suitable for every case.

stop() { echo "$*" 1>&2 ; exit 1; }

which ip &>/dev/null || stop "Don't have the ip tool"
which whoami &>/dev/null || stop "Don't have the whoami tool"
which iptables &>/dev/null || stop "Don't have the iptables tool"
which sysctl &>/dev/null || stop "Don't have the sysctl tool"

ADDRESS="192.168.0.1/16"
NAME="tun0"
USER="$(whoami)"
CONFIGURE_IPTABLE="false"
CONFIGURE_TUNTAP_IF_EXISTS="false"

while getopts ":a:t:u:io" opt; do
case $opt in
a) ADDRESS="${OPTARG}"
;;
t) NAME="${OPTARG}"
;;
u) USER="${OPTARG}"
;;
i) CONFIGURE_IPTABLE="true"
;;
o) CONFIGURE_TUNTAP_IF_EXISTS="true"
;;
\?) echo "Invalid option -$OPTARG" >&2
exit 1
;;
esac
done

echo "Verifying if device ${NAME} is already created..."
if ip tuntap | grep "${NAME}" &> /dev/null;
then
echo "The devince ${NAME} already exists!"
if [[ "${CONFIGURE_TUNTAP_IF_EXISTS}" != "true" ]]
then
exit 1;
fi
fi

echo "Creating tun device ${NAME} for user ${USER}..."
ip tuntap add name "${NAME}" mode tun user "${USER}"
ip link set "${NAME}" up

echo "Assigning address ${ADDRESS} to device ${NAME}..."
ip addr add "${ADDRESS}" dev "${NAME}"

if [[ "${CONFIGURE_IPTABLE}" == "true" ]];
then
echo "Enabling ip forward..."
sysctl net.ipv4.ip_forward=1

echo "Preparing iptable..."
iptables -t nat -A POSTROUTING -s "${ADDRESS}" -j MASQUERADE
iptables -A FORWARD -i "${NAME}" -s "${ADDRESS}" -j ACCEPT
iptables -A FORWARD -o "${NAME}" -d "${ADDRESS}" -j ACCEPT

RULE_NR=$(iptables -t filter -L INPUT --line-numbers |\
grep "REJECT all" |\
awk '{print $1}')

# Excempt tun device from potentiall reject all rule
if [[ $RULE_NR == "" ]]; then
iptables -I INPUT -i "${NAME}" -s "${ADDRESS}" -j ACCEPT
else
iptables -I INPUT $((RULE_NR - 1)) -i "${NAME}" -s "${ADDRESS}" -j ACCEPT
fi
fi
76 changes: 76 additions & 0 deletions tests/workers/network/smoke.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/usr/bin/env bash
set -eou pipefail

stop() { echo "$*" 1>&2 ; exit 1; }

which bpftrace &>/dev/null || stop "Don't have bpftrace"
which bpftool &>/dev/null || stop "Don't have bpftool"
which berserker &>/dev/null || stop "Don't have berserker"

if [ ! -d "tests/workers/network/" ]; then
echo "Can't find test directory. Smoke tests have to be run from the project root directory"
fi

echo "Cleanup..."
rm -f /tmp/server.log
rm -f /tmp/client.log
rm -f /tmp/tcpaccept.log
# in case if it's still running from a previous run
pkill berserker || true

echo "Starting bpftrace..."
bpftrace tests/workers/network/sys_accept.bt &> /tmp/tcpaccept.log &
export BPFTRACE_PID=$!

# let bpftrace attach probes
attempts=0

while ! bpftool prog | grep -q bpftrace ;
do
if [[ "$attempts" -gt 20 ]]; then
echo "Can't find bpftool after ${attempts} attempts."
exit 1
fi;

attempts=$((attempts+1))
echo "Wait for bpftrace";
sleep 0.5;
done

echo "Starting the server..."
berserker tests/workers/network/workload.server.toml &> /tmp/server.log &
export SERVER_PID=$!

echo "Starting the client..."
berserker tests/workers/network/workload.client.toml &> /tmp/client.log &
export CLIENT_PID=$!

# let it do some work
sleep 5;

echo "Stopping..."
kill "${CLIENT_PID}" || { echo 'Can't stop the client ; exit 1; }
kill "${SERVER_PID}" || { echo 'Can't stop the server ; exit 1; }
kill "${BPFTRACE_PID}" || { echo 'Can't stop the bpftrace ; exit 1; }

echo "Verifying the results..."
ENDPOINTS=$(cat /tmp/tcpaccept.log |\
grep hit |\
awk '{print $4 " " $5}' |\
sort | uniq -c | wc -l)

if (( $ENDPOINTS > 0 )); then
echo "PASS"

rm -f /tmp/server.log
rm -f /tmp/client.log
rm -f /tmp/tcpaccept.log

exit 0;
else
echo "FAIL"
cat /tmp/server.log
cat /tmp/client.log
cat /tmp/tcpaccept.log
exit 1;
fi;
4 changes: 4 additions & 0 deletions tests/workers/network/sys_accept.bt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
k:__sys_accept* /comm == "berserker"/
{
printf("hit\n");
}
12 changes: 12 additions & 0 deletions tests/workers/network/workload.client.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
restart_interval = 10
per_core = false
workers = 1

[workload]
type = "network"
server = false
address = [192, 168, 0, 1]
target_port = 8081
arrival_rate = 1000
departure_rate = 1
nconnections = 10
12 changes: 12 additions & 0 deletions tests/workers/network/workload.server.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
restart_interval = 10
per_core = false
workers = 1

[workload]
type = "network"
server = true
address = [192, 168, 0, 1]
target_port = 8081
arrival_rate = 0.1
departure_rate = 0.1
nconnections = 10

0 comments on commit 5f620d2

Please sign in to comment.