forked from cyyself/wg-bench
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenwrt-benchmark.sh
More file actions
executable file
·141 lines (115 loc) · 3.66 KB
/
openwrt-benchmark.sh
File metadata and controls
executable file
·141 lines (115 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/bin/sh
#set -x
[ -s /etc/os-release ] && . /etc/os-release
NETNS="wg-bench"
NIC="wg-bench"
HOST_VETH_IP="11.0.0.1"
NS_VETH_IP="11.0.0.2"
HOST_WG_IP="169.254.200.1"
NS_WG_IP="169.254.200.2"
HOST_PORT="11001"
NS_PORT="11002"
DEVICE=$(cat /tmp/sysinfo/model 2>/dev/null || echo "Unknown Device")
VERSION=$(grep OPENWRT_RELEASE /etc/os-release | cut -d'"' -f2 | cut -d' ' -f1,2)
KERNEL_VERSION=$(uname -r)
if [ "$ID_LIKE" != "lede openwrt" ]; then
echo "This is not OpenWrt. Exit"
exit 1
fi
if command -v opkg >/dev/null 2>&1; then
PKG_MANAGER="opkg"
INSTALL_CMD="opkg install"
LIST_INSTALLED_CMD="opkg list-installed"
elif command -v apk >/dev/null 2>&1; then
PKG_MANAGER="apk"
INSTALL_CMD="apk add"
LIST_INSTALLED_CMD="apk list -I"
fi
printf "\033[32;1m\nPackages:\033[0m\n"
PACKAGES="wireguard-tools iperf3 ip-full kmod-veth psmisc"
UPDATE_NEEDED=false
PACKAGES_TO_INSTALL=""
for package in $PACKAGES; do
if $LIST_INSTALLED_CMD | grep -q "$package"; then
echo "$package already installed"
else
echo "$package needs to be installed"
PACKAGES_TO_INSTALL="$PACKAGES_TO_INSTALL $package"
UPDATE_NEEDED=true
fi
done
if [ "$UPDATE_NEEDED" = true ]; then
if [ "$PKG_MANAGER" = "opkg" ]; then
echo "Updating package lists..."
opkg update
fi
echo "Installing packages: $PACKAGES_TO_INSTALL"
for package in $PACKAGES_TO_INSTALL; do
$INSTALL_CMD "$package"
done
fi
printf "\033[32;1m\nRouter details:\033[0m\n"
ubus call system board
setup() {
# Create netns
ip netns add ${NETNS}
# Create veth
ip link add ${NIC} type veth peer name ${NIC}-ns
ip link set ${NIC}-ns netns ${NETNS}
ip addr add ${HOST_VETH_IP}/30 dev ${NIC}
ip netns exec ${NETNS} ip addr add ${NS_VETH_IP}/30 dev ${NIC}-ns
ip link set ${NIC} up
ip netns exec ${NETNS} ip link set ${NIC}-ns up
# Setup WG for host
wg genkey > host-privkey 2> /dev/null
wg genkey > ns-privkey 2> /dev/null
ip link add ${NIC}-wg type wireguard
wg set ${NIC}-wg listen-port ${HOST_PORT} private-key host-privkey
ip addr add ${HOST_WG_IP}/32 dev ${NIC}-wg peer ${NS_WG_IP}
wg set ${NIC}-wg peer $(wg pubkey < ns-privkey) allowed-ips ${NS_WG_IP}/32 endpoint ${NS_VETH_IP}:${NS_PORT}
ip link set ${NIC}-wg up
# Set up WG for netns
ip netns exec ${NETNS} ip link add ${NIC}-wg type wireguard
ip netns exec ${NETNS} wg set ${NIC}-wg listen-port ${NS_PORT} private-key $PWD/ns-privkey
ip netns exec ${NETNS} ip addr add ${NS_WG_IP}/32 dev ${NIC}-wg peer ${HOST_WG_IP}
ip netns exec ${NETNS} wg set ${NIC}-wg peer $(wg pubkey < $PWD/host-privkey) allowed-ips ${HOST_WG_IP}/32 endpoint ${HOST_VETH_IP}:${HOST_PORT}
ip netns exec ${NETNS} ip link set ${NIC}-wg up
rm host-privkey
rm ns-privkey
}
bench() {
ip netns exec ${NETNS} iperf3 -s -D -p 4242
sleep 2
iperf3 -c ${NS_WG_IP} $@ -p 4242 | tee /tmp/iperf3_output
ip netns exec ${NETNS} fuser -k 4242/tcp
}
clean() {
# Delete netns
ip netns del ${NETNS}
# Delete veth
ip link del ${NIC}
# Delete WG
ip link del ${NIC}-wg
}
table() {
if [ -f /tmp/iperf3_output ]; then
speed=$(grep "sender" /tmp/iperf3_output | tail -1 | awk '{for(i=1;i<=NF;i++) if($i ~ /[MGK]bits\/sec/) print $(i-1) " " $i}')
if [ -z "$speed" ]; then
speed="N/A"
fi
rm -f /tmp/iperf3_output
else
speed="N/A"
fi
echo
echo "Markdown Table Row:"
printf "| %-32s | %-32s | %-14s | %s |\n" \
"$DEVICE / ENTER CPU MODEL" \
"$VERSION / $KERNEL_VERSION" \
"$speed" \
""
}
setup
bench
clean
table