0001
0002
0003
0004
0005 export TESTNAME=test_flow_dissector
0006 unmount=0
0007
0008
0009 ksft_skip=4
0010
0011 msg="skip all tests:"
0012 if [ $UID != 0 ]; then
0013 echo $msg please run this as root >&2
0014 exit $ksft_skip
0015 fi
0016
0017
0018
0019
0020 if [[ -z $(ip netns identify $$) ]]; then
0021 err=0
0022 if bpftool="$(which bpftool)"; then
0023 echo "Testing global flow dissector..."
0024
0025 $bpftool prog loadall ./bpf_flow.o /sys/fs/bpf/flow \
0026 type flow_dissector
0027
0028 if ! unshare --net $bpftool prog attach pinned \
0029 /sys/fs/bpf/flow/_dissect flow_dissector; then
0030 echo "Unexpected unsuccessful attach in namespace" >&2
0031 err=1
0032 fi
0033
0034 $bpftool prog attach pinned /sys/fs/bpf/flow/_dissect \
0035 flow_dissector
0036
0037 if unshare --net $bpftool prog attach pinned \
0038 /sys/fs/bpf/flow/_dissect flow_dissector; then
0039 echo "Unexpected successful attach in namespace" >&2
0040 err=1
0041 fi
0042
0043 if ! $bpftool prog detach pinned \
0044 /sys/fs/bpf/flow/_dissect flow_dissector; then
0045 echo "Failed to detach flow dissector" >&2
0046 err=1
0047 fi
0048
0049 rm -rf /sys/fs/bpf/flow
0050 else
0051 echo "Skipping root flow dissector test, bpftool not found" >&2
0052 fi
0053
0054
0055 ../net/in_netns.sh "$0" "$@"
0056 err=$(( $err + $? ))
0057
0058 if (( $err == 0 )); then
0059 echo "selftests: $TESTNAME [PASS]";
0060 else
0061 echo "selftests: $TESTNAME [FAILED]";
0062 fi
0063
0064 exit $err
0065 fi
0066
0067
0068 exit_handler()
0069 {
0070 set +e
0071
0072
0073 tc filter del dev lo ingress pref 1337 2> /dev/null
0074 tc qdisc del dev lo ingress 2> /dev/null
0075 ./flow_dissector_load -d 2> /dev/null
0076 if [ $unmount -ne 0 ]; then
0077 umount bpffs 2> /dev/null
0078 fi
0079 }
0080
0081
0082
0083 set -e
0084
0085
0086 trap exit_handler 0 2 3 6 9
0087
0088
0089 if /bin/mount | grep /sys/fs/bpf > /dev/null; then
0090 echo "bpffs already mounted"
0091 else
0092 echo "bpffs not mounted. Mounting..."
0093 unmount=1
0094 /bin/mount bpffs /sys/fs/bpf -t bpf
0095 fi
0096
0097
0098 ./flow_dissector_load -p bpf_flow.o -s _dissect
0099
0100
0101 tc qdisc add dev lo ingress
0102 echo 0 > /proc/sys/net/ipv4/conf/default/rp_filter
0103 echo 0 > /proc/sys/net/ipv4/conf/all/rp_filter
0104 echo 0 > /proc/sys/net/ipv4/conf/lo/rp_filter
0105
0106 echo "Testing IPv4..."
0107
0108 tc filter add dev lo parent ffff: protocol ip pref 1337 flower ip_proto \
0109 udp src_port 9 action drop
0110
0111
0112 ./test_flow_dissector -i 4 -f 8
0113
0114 ./test_flow_dissector -i 4 -f 9 -F
0115
0116 ./test_flow_dissector -i 4 -f 10
0117
0118 echo "Testing IPIP..."
0119
0120 ./with_addr.sh ./with_tunnels.sh ./test_flow_dissector -o 4 -e bare -i 4 \
0121 -D 192.168.0.1 -S 1.1.1.1 -f 8
0122
0123 ./with_addr.sh ./with_tunnels.sh ./test_flow_dissector -o 4 -e bare -i 4 \
0124 -D 192.168.0.1 -S 1.1.1.1 -f 9 -F
0125
0126 ./with_addr.sh ./with_tunnels.sh ./test_flow_dissector -o 4 -e bare -i 4 \
0127 -D 192.168.0.1 -S 1.1.1.1 -f 10
0128
0129 echo "Testing IPv4 + GRE..."
0130
0131 ./with_addr.sh ./with_tunnels.sh ./test_flow_dissector -o 4 -e gre -i 4 \
0132 -D 192.168.0.1 -S 1.1.1.1 -f 8
0133
0134 ./with_addr.sh ./with_tunnels.sh ./test_flow_dissector -o 4 -e gre -i 4 \
0135 -D 192.168.0.1 -S 1.1.1.1 -f 9 -F
0136
0137 ./with_addr.sh ./with_tunnels.sh ./test_flow_dissector -o 4 -e gre -i 4 \
0138 -D 192.168.0.1 -S 1.1.1.1 -f 10
0139
0140 tc filter del dev lo ingress pref 1337
0141
0142 echo "Testing port range..."
0143
0144 tc filter add dev lo parent ffff: protocol ip pref 1337 flower ip_proto \
0145 udp src_port 8-10 action drop
0146
0147
0148 ./test_flow_dissector -i 4 -f 7
0149
0150 ./test_flow_dissector -i 4 -f 9 -F
0151
0152 ./test_flow_dissector -i 4 -f 11
0153
0154 tc filter del dev lo ingress pref 1337
0155
0156 echo "Testing IPv6..."
0157
0158 tc filter add dev lo parent ffff: protocol ipv6 pref 1337 flower ip_proto \
0159 udp src_port 9 action drop
0160
0161
0162 ./test_flow_dissector -i 6 -f 8
0163
0164 ./test_flow_dissector -i 6 -f 9 -F
0165
0166 ./test_flow_dissector -i 6 -f 10
0167
0168 exit 0