2019-03-23 02:32:49 +08:00
|
|
|
#!/bin/bash
|
|
|
|
# SPDX-License-Identifier: GPL-2.0
|
|
|
|
#
|
|
|
|
# In-place tunneling
|
|
|
|
|
|
|
|
# must match the port that the bpf program filters on
|
|
|
|
readonly port=8000
|
|
|
|
|
|
|
|
readonly ns_prefix="ns-$$-"
|
|
|
|
readonly ns1="${ns_prefix}1"
|
|
|
|
readonly ns2="${ns_prefix}2"
|
|
|
|
|
|
|
|
readonly ns1_v4=192.168.1.1
|
|
|
|
readonly ns2_v4=192.168.1.2
|
2019-03-23 02:32:51 +08:00
|
|
|
readonly ns1_v6=fd::1
|
|
|
|
readonly ns2_v6=fd::2
|
|
|
|
|
2019-03-23 02:32:49 +08:00
|
|
|
|
|
|
|
setup() {
|
|
|
|
ip netns add "${ns1}"
|
|
|
|
ip netns add "${ns2}"
|
|
|
|
|
|
|
|
ip link add dev veth1 mtu 1500 netns "${ns1}" type veth \
|
|
|
|
peer name veth2 mtu 1500 netns "${ns2}"
|
|
|
|
|
|
|
|
ip -netns "${ns1}" link set veth1 up
|
|
|
|
ip -netns "${ns2}" link set veth2 up
|
|
|
|
|
|
|
|
ip -netns "${ns1}" -4 addr add "${ns1_v4}/24" dev veth1
|
|
|
|
ip -netns "${ns2}" -4 addr add "${ns2_v4}/24" dev veth2
|
2019-03-23 02:32:51 +08:00
|
|
|
ip -netns "${ns1}" -6 addr add "${ns1_v6}/64" dev veth1 nodad
|
|
|
|
ip -netns "${ns2}" -6 addr add "${ns2_v6}/64" dev veth2 nodad
|
2019-03-23 02:32:49 +08:00
|
|
|
|
|
|
|
sleep 1
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup() {
|
|
|
|
ip netns del "${ns2}"
|
|
|
|
ip netns del "${ns1}"
|
|
|
|
}
|
|
|
|
|
|
|
|
server_listen() {
|
2019-03-23 02:32:51 +08:00
|
|
|
ip netns exec "${ns2}" nc "${netcat_opt}" -l -p "${port}" &
|
2019-03-23 02:32:49 +08:00
|
|
|
sleep 0.2
|
|
|
|
}
|
|
|
|
|
|
|
|
client_connect() {
|
2019-03-23 02:32:51 +08:00
|
|
|
ip netns exec "${ns1}" nc "${netcat_opt}" -z -w 1 "${addr2}" "${port}"
|
2019-03-23 02:32:49 +08:00
|
|
|
echo $?
|
|
|
|
}
|
|
|
|
|
|
|
|
set -e
|
2019-03-23 02:32:51 +08:00
|
|
|
|
|
|
|
# no arguments: automated test, run all
|
|
|
|
if [[ "$#" -eq "0" ]]; then
|
|
|
|
echo "ipip"
|
2019-03-23 02:32:52 +08:00
|
|
|
$0 ipv4 ipip
|
2019-03-23 02:32:51 +08:00
|
|
|
|
|
|
|
echo "ip6ip6"
|
2019-03-23 02:32:52 +08:00
|
|
|
$0 ipv6 ip6tnl
|
|
|
|
|
|
|
|
echo "ip gre"
|
|
|
|
$0 ipv4 gre
|
|
|
|
|
|
|
|
echo "ip6 gre"
|
|
|
|
$0 ipv6 ip6gre
|
2019-03-23 02:32:51 +08:00
|
|
|
|
|
|
|
echo "OK. All tests passed"
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2019-03-23 02:32:52 +08:00
|
|
|
if [[ "$#" -ne "2" ]]; then
|
2019-03-23 02:32:51 +08:00
|
|
|
echo "Usage: $0"
|
2019-03-23 02:32:52 +08:00
|
|
|
echo " or: $0 <ipv4|ipv6> <tuntype>"
|
2019-03-23 02:32:51 +08:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
case "$1" in
|
|
|
|
"ipv4")
|
2019-03-23 02:32:52 +08:00
|
|
|
readonly tuntype=$2
|
2019-03-23 02:32:51 +08:00
|
|
|
readonly addr1="${ns1_v4}"
|
|
|
|
readonly addr2="${ns2_v4}"
|
|
|
|
readonly netcat_opt=-4
|
|
|
|
;;
|
|
|
|
"ipv6")
|
2019-03-23 02:32:52 +08:00
|
|
|
readonly tuntype=$2
|
2019-03-23 02:32:51 +08:00
|
|
|
readonly addr1="${ns1_v6}"
|
|
|
|
readonly addr2="${ns2_v6}"
|
|
|
|
readonly netcat_opt=-6
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "unknown arg: $1"
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
echo "encap ${addr1} to ${addr2}, type ${tuntype}"
|
|
|
|
|
2019-03-23 02:32:49 +08:00
|
|
|
trap cleanup EXIT
|
|
|
|
|
|
|
|
setup
|
|
|
|
|
|
|
|
# basic communication works
|
|
|
|
echo "test basic connectivity"
|
|
|
|
server_listen
|
|
|
|
client_connect
|
|
|
|
|
|
|
|
# clientside, insert bpf program to encap all TCP to port ${port}
|
|
|
|
# client can no longer connect
|
|
|
|
ip netns exec "${ns1}" tc qdisc add dev veth1 clsact
|
|
|
|
ip netns exec "${ns1}" tc filter add dev veth1 egress \
|
2019-03-23 02:32:52 +08:00
|
|
|
bpf direct-action object-file ./test_tc_tunnel.o \
|
|
|
|
section "encap_${tuntype}"
|
2019-03-23 02:32:49 +08:00
|
|
|
echo "test bpf encap without decap (expect failure)"
|
|
|
|
server_listen
|
|
|
|
! client_connect
|
|
|
|
|
|
|
|
# serverside, insert decap module
|
|
|
|
# server is still running
|
|
|
|
# client can connect again
|
2019-03-23 02:32:51 +08:00
|
|
|
ip netns exec "${ns2}" ip link add dev testtun0 type "${tuntype}" \
|
|
|
|
remote "${addr1}" local "${addr2}"
|
2019-03-23 02:32:49 +08:00
|
|
|
ip netns exec "${ns2}" ip link set dev testtun0 up
|
|
|
|
echo "test bpf encap with tunnel device decap"
|
|
|
|
client_connect
|
|
|
|
|
2019-03-23 02:32:50 +08:00
|
|
|
# serverside, use BPF for decap
|
|
|
|
ip netns exec "${ns2}" ip link del dev testtun0
|
|
|
|
ip netns exec "${ns2}" tc qdisc add dev veth2 clsact
|
|
|
|
ip netns exec "${ns2}" tc filter add dev veth2 ingress \
|
|
|
|
bpf direct-action object-file ./test_tc_tunnel.o section decap
|
|
|
|
server_listen
|
|
|
|
echo "test bpf encap with bpf decap"
|
|
|
|
client_connect
|
|
|
|
|
2019-03-23 02:32:49 +08:00
|
|
|
echo OK
|