Jakub Kicinski | 025efa0 | 2021-09-01 08:55:01 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # SPDX-License-Identifier: GPL-2.0 |
| 3 | |
| 4 | # This test is for checking GRE GSO. |
| 5 | |
| 6 | ret=0 |
| 7 | # Kselftest framework requirement - SKIP code is 4. |
| 8 | ksft_skip=4 |
| 9 | |
| 10 | # all tests in this script. Can be overridden with -t option |
| 11 | TESTS="gre_gso" |
| 12 | |
| 13 | VERBOSE=0 |
| 14 | PAUSE_ON_FAIL=no |
| 15 | PAUSE=no |
| 16 | IP="ip -netns ns1" |
| 17 | NS_EXEC="ip netns exec ns1" |
| 18 | TMPFILE=`mktemp` |
| 19 | PID= |
| 20 | |
| 21 | log_test() |
| 22 | { |
| 23 | local rc=$1 |
| 24 | local expected=$2 |
| 25 | local msg="$3" |
| 26 | |
| 27 | if [ ${rc} -eq ${expected} ]; then |
| 28 | printf " TEST: %-60s [ OK ]\n" "${msg}" |
| 29 | nsuccess=$((nsuccess+1)) |
| 30 | else |
| 31 | ret=1 |
| 32 | nfail=$((nfail+1)) |
| 33 | printf " TEST: %-60s [FAIL]\n" "${msg}" |
| 34 | if [ "${PAUSE_ON_FAIL}" = "yes" ]; then |
| 35 | echo |
| 36 | echo "hit enter to continue, 'q' to quit" |
| 37 | read a |
| 38 | [ "$a" = "q" ] && exit 1 |
| 39 | fi |
| 40 | fi |
| 41 | |
| 42 | if [ "${PAUSE}" = "yes" ]; then |
| 43 | echo |
| 44 | echo "hit enter to continue, 'q' to quit" |
| 45 | read a |
| 46 | [ "$a" = "q" ] && exit 1 |
| 47 | fi |
| 48 | } |
| 49 | |
| 50 | setup() |
| 51 | { |
| 52 | set -e |
| 53 | ip netns add ns1 |
| 54 | ip netns set ns1 auto |
| 55 | $IP link set dev lo up |
| 56 | |
| 57 | ip link add veth0 type veth peer name veth1 |
| 58 | ip link set veth0 up |
| 59 | ip link set veth1 netns ns1 |
| 60 | $IP link set veth1 name veth0 |
| 61 | $IP link set veth0 up |
| 62 | |
| 63 | dd if=/dev/urandom of=$TMPFILE bs=1024 count=2048 &>/dev/null |
| 64 | set +e |
| 65 | } |
| 66 | |
| 67 | cleanup() |
| 68 | { |
| 69 | rm -rf $TMPFILE |
| 70 | [ -n "$PID" ] && kill $PID |
| 71 | ip link del dev gre1 &> /dev/null |
| 72 | ip link del dev veth0 &> /dev/null |
| 73 | ip netns del ns1 |
| 74 | } |
| 75 | |
| 76 | get_linklocal() |
| 77 | { |
| 78 | local dev=$1 |
| 79 | local ns=$2 |
| 80 | local addr |
| 81 | |
| 82 | [ -n "$ns" ] && ns="-netns $ns" |
| 83 | |
| 84 | addr=$(ip -6 -br $ns addr show dev ${dev} | \ |
| 85 | awk '{ |
| 86 | for (i = 3; i <= NF; ++i) { |
| 87 | if ($i ~ /^fe80/) |
| 88 | print $i |
| 89 | } |
| 90 | }' |
| 91 | ) |
| 92 | addr=${addr/\/*} |
| 93 | |
| 94 | [ -z "$addr" ] && return 1 |
| 95 | |
| 96 | echo $addr |
| 97 | |
| 98 | return 0 |
| 99 | } |
| 100 | |
| 101 | gre_create_tun() |
| 102 | { |
| 103 | local a1=$1 |
| 104 | local a2=$2 |
| 105 | local mode |
| 106 | |
| 107 | [[ $a1 =~ ^[0-9.]*$ ]] && mode=gre || mode=ip6gre |
| 108 | |
| 109 | ip tunnel add gre1 mode $mode local $a1 remote $a2 dev veth0 |
| 110 | ip link set gre1 up |
| 111 | $IP tunnel add gre1 mode $mode local $a2 remote $a1 dev veth0 |
| 112 | $IP link set gre1 up |
| 113 | } |
| 114 | |
| 115 | gre_gst_test_checks() |
| 116 | { |
| 117 | local name=$1 |
| 118 | local addr=$2 |
Andrea Righi | a985442 | 2021-11-04 11:46:13 +0100 | [diff] [blame] | 119 | local proto=$3 |
Jakub Kicinski | 025efa0 | 2021-09-01 08:55:01 -0700 | [diff] [blame] | 120 | |
Jakub Kicinski | 0cda7d4 | 2021-11-11 08:29:29 -0800 | [diff] [blame] | 121 | [ "$proto" == 6 ] && addr="[$addr]" |
| 122 | |
| 123 | $NS_EXEC socat - tcp${proto}-listen:$port,reuseaddr,fork >/dev/null & |
Jakub Kicinski | 025efa0 | 2021-09-01 08:55:01 -0700 | [diff] [blame] | 124 | PID=$! |
| 125 | while ! $NS_EXEC ss -ltn | grep -q $port; do ((i++)); sleep 0.01; done |
| 126 | |
Jakub Kicinski | 0cda7d4 | 2021-11-11 08:29:29 -0800 | [diff] [blame] | 127 | cat $TMPFILE | timeout 1 socat -u STDIN TCP:$addr:$port |
Jakub Kicinski | 025efa0 | 2021-09-01 08:55:01 -0700 | [diff] [blame] | 128 | log_test $? 0 "$name - copy file w/ TSO" |
| 129 | |
| 130 | ethtool -K veth0 tso off |
| 131 | |
Jakub Kicinski | 0cda7d4 | 2021-11-11 08:29:29 -0800 | [diff] [blame] | 132 | cat $TMPFILE | timeout 1 socat -u STDIN TCP:$addr:$port |
Jakub Kicinski | 025efa0 | 2021-09-01 08:55:01 -0700 | [diff] [blame] | 133 | log_test $? 0 "$name - copy file w/ GSO" |
| 134 | |
| 135 | ethtool -K veth0 tso on |
| 136 | |
| 137 | kill $PID |
| 138 | PID= |
| 139 | } |
| 140 | |
| 141 | gre6_gso_test() |
| 142 | { |
| 143 | local port=7777 |
| 144 | |
| 145 | setup |
| 146 | |
| 147 | a1=$(get_linklocal veth0) |
| 148 | a2=$(get_linklocal veth0 ns1) |
| 149 | |
| 150 | gre_create_tun $a1 $a2 |
| 151 | |
| 152 | ip addr add 172.16.2.1/24 dev gre1 |
| 153 | $IP addr add 172.16.2.2/24 dev gre1 |
| 154 | |
| 155 | ip -6 addr add 2001:db8:1::1/64 dev gre1 nodad |
| 156 | $IP -6 addr add 2001:db8:1::2/64 dev gre1 nodad |
| 157 | |
| 158 | sleep 2 |
| 159 | |
Jakub Kicinski | 0cda7d4 | 2021-11-11 08:29:29 -0800 | [diff] [blame] | 160 | gre_gst_test_checks GREv6/v4 172.16.2.2 4 |
| 161 | gre_gst_test_checks GREv6/v6 2001:db8:1::2 6 |
Jakub Kicinski | 025efa0 | 2021-09-01 08:55:01 -0700 | [diff] [blame] | 162 | |
| 163 | cleanup |
| 164 | } |
| 165 | |
| 166 | gre_gso_test() |
| 167 | { |
| 168 | gre6_gso_test |
| 169 | } |
| 170 | |
| 171 | ################################################################################ |
| 172 | # usage |
| 173 | |
| 174 | usage() |
| 175 | { |
| 176 | cat <<EOF |
| 177 | usage: ${0##*/} OPTS |
| 178 | |
| 179 | -t <test> Test(s) to run (default: all) |
| 180 | (options: $TESTS) |
| 181 | -p Pause on fail |
| 182 | -P Pause after each test before cleanup |
| 183 | -v verbose mode (show commands and output) |
| 184 | EOF |
| 185 | } |
| 186 | |
| 187 | ################################################################################ |
| 188 | # main |
| 189 | |
| 190 | while getopts :t:pPhv o |
| 191 | do |
| 192 | case $o in |
| 193 | t) TESTS=$OPTARG;; |
| 194 | p) PAUSE_ON_FAIL=yes;; |
| 195 | P) PAUSE=yes;; |
| 196 | v) VERBOSE=$(($VERBOSE + 1));; |
| 197 | h) usage; exit 0;; |
| 198 | *) usage; exit 1;; |
| 199 | esac |
| 200 | done |
| 201 | |
| 202 | PEER_CMD="ip netns exec ${PEER_NS}" |
| 203 | |
| 204 | # make sure we don't pause twice |
| 205 | [ "${PAUSE}" = "yes" ] && PAUSE_ON_FAIL=no |
| 206 | |
| 207 | if [ "$(id -u)" -ne 0 ];then |
| 208 | echo "SKIP: Need root privileges" |
| 209 | exit $ksft_skip; |
| 210 | fi |
| 211 | |
| 212 | if [ ! -x "$(command -v ip)" ]; then |
| 213 | echo "SKIP: Could not run test without ip tool" |
| 214 | exit $ksft_skip |
| 215 | fi |
| 216 | |
Jakub Kicinski | 0cda7d4 | 2021-11-11 08:29:29 -0800 | [diff] [blame] | 217 | if [ ! -x "$(command -v socat)" ]; then |
| 218 | echo "SKIP: Could not run test without socat tool" |
Jakub Kicinski | 025efa0 | 2021-09-01 08:55:01 -0700 | [diff] [blame] | 219 | exit $ksft_skip |
| 220 | fi |
| 221 | |
| 222 | # start clean |
| 223 | cleanup &> /dev/null |
| 224 | |
| 225 | for t in $TESTS |
| 226 | do |
| 227 | case $t in |
| 228 | gre_gso) gre_gso_test;; |
| 229 | |
| 230 | help) echo "Test names: $TESTS"; exit 0;; |
| 231 | esac |
| 232 | done |
| 233 | |
| 234 | if [ "$TESTS" != "none" ]; then |
| 235 | printf "\nTests passed: %3d\n" ${nsuccess} |
| 236 | printf "Tests failed: %3d\n" ${nfail} |
| 237 | fi |
| 238 | |
| 239 | exit $ret |