0001
0002
0003
0004
0005
0006
0007 NS1=lwt_ns1
0008 NS2=lwt_ns2
0009 VETH0=tst_lwt1a
0010 VETH1=tst_lwt1b
0011 VETH2=tst_lwt2a
0012 VETH3=tst_lwt2b
0013 IPVETH0="192.168.254.1"
0014 IPVETH1="192.168.254.2"
0015 IPVETH1b="192.168.254.3"
0016
0017 IPVETH2="192.168.111.1"
0018 IPVETH3="192.168.111.2"
0019
0020 IP_LOCAL="192.168.99.1"
0021
0022 TRACE_ROOT=/sys/kernel/debug/tracing
0023
0024 function lookup_mac()
0025 {
0026 set +x
0027 if [ ! -z "$2" ]; then
0028 MAC=$(ip netns exec $2 ip link show $1 | grep ether | awk '{print $2}')
0029 else
0030 MAC=$(ip link show $1 | grep ether | awk '{print $2}')
0031 fi
0032 MAC="${MAC//:/}"
0033 echo "0x${MAC:10:2}${MAC:8:2}${MAC:6:2}${MAC:4:2}${MAC:2:2}${MAC:0:2}"
0034 set -x
0035 }
0036
0037 function cleanup {
0038 set +ex
0039 rm test_lwt_bpf.o 2> /dev/null
0040 ip link del $VETH0 2> /dev/null
0041 ip link del $VETH1 2> /dev/null
0042 ip link del $VETH2 2> /dev/null
0043 ip link del $VETH3 2> /dev/null
0044 ip netns exec $NS1 killall netserver
0045 ip netns delete $NS1 2> /dev/null
0046 ip netns delete $NS2 2> /dev/null
0047 set -ex
0048 }
0049
0050 function setup_one_veth {
0051 ip netns add $1
0052 ip link add $2 type veth peer name $3
0053 ip link set dev $2 up
0054 ip addr add $4/24 dev $2
0055 ip link set $3 netns $1
0056 ip netns exec $1 ip link set dev $3 up
0057 ip netns exec $1 ip addr add $5/24 dev $3
0058
0059 if [ "$6" ]; then
0060 ip netns exec $1 ip addr add $6/32 dev $3
0061 fi
0062 }
0063
0064 function get_trace {
0065 set +x
0066 cat ${TRACE_ROOT}/trace | grep -v '^#'
0067 set -x
0068 }
0069
0070 function cleanup_routes {
0071 ip route del ${IPVETH1}/32 dev $VETH0 2> /dev/null || true
0072 ip route del table local local ${IP_LOCAL}/32 dev lo 2> /dev/null || true
0073 }
0074
0075 function install_test {
0076 cleanup_routes
0077 cp /dev/null ${TRACE_ROOT}/trace
0078
0079 OPTS="encap bpf headroom 14 $1 obj test_lwt_bpf.o section $2 $VERBOSE"
0080
0081 if [ "$1" == "in" ]; then
0082 ip route add table local local ${IP_LOCAL}/32 $OPTS dev lo
0083 else
0084 ip route add ${IPVETH1}/32 $OPTS dev $VETH0
0085 fi
0086 }
0087
0088 function remove_prog {
0089 if [ "$1" == "in" ]; then
0090 ip route del table local local ${IP_LOCAL}/32 dev lo
0091 else
0092 ip route del ${IPVETH1}/32 dev $VETH0
0093 fi
0094 }
0095
0096 function filter_trace {
0097
0098 NL=$'\n'
0099 echo "${NL}$*" | sed -e 's/^.*: : //g'
0100 }
0101
0102 function expect_fail {
0103 set +x
0104 echo "FAIL:"
0105 echo "Expected: $1"
0106 echo "Got: $2"
0107 set -x
0108 exit 1
0109 }
0110
0111 function match_trace {
0112 set +x
0113 RET=0
0114 TRACE=$1
0115 EXPECT=$2
0116 GOT="$(filter_trace "$TRACE")"
0117
0118 [ "$GOT" != "$EXPECT" ] && {
0119 expect_fail "$EXPECT" "$GOT"
0120 RET=1
0121 }
0122 set -x
0123 return $RET
0124 }
0125
0126 function test_start {
0127 set +x
0128 echo "----------------------------------------------------------------"
0129 echo "Starting test: $*"
0130 echo "----------------------------------------------------------------"
0131 set -x
0132 }
0133
0134 function failure {
0135 get_trace
0136 echo "FAIL: $*"
0137 exit 1
0138 }
0139
0140 function test_ctx_xmit {
0141 test_start "test_ctx on lwt xmit"
0142 install_test xmit test_ctx
0143 ping -c 3 $IPVETH1 || {
0144 failure "test_ctx xmit: packets are dropped"
0145 }
0146 match_trace "$(get_trace)" "
0147 len 84 hash 0 protocol 8
0148 cb 1234 ingress_ifindex 0 ifindex $DST_IFINDEX
0149 len 84 hash 0 protocol 8
0150 cb 1234 ingress_ifindex 0 ifindex $DST_IFINDEX
0151 len 84 hash 0 protocol 8
0152 cb 1234 ingress_ifindex 0 ifindex $DST_IFINDEX" || exit 1
0153 remove_prog xmit
0154 }
0155
0156 function test_ctx_out {
0157 test_start "test_ctx on lwt out"
0158 install_test out test_ctx
0159 ping -c 3 $IPVETH1 || {
0160 failure "test_ctx out: packets are dropped"
0161 }
0162 match_trace "$(get_trace)" "
0163 len 84 hash 0 protocol 0
0164 cb 1234 ingress_ifindex 0 ifindex 0
0165 len 84 hash 0 protocol 0
0166 cb 1234 ingress_ifindex 0 ifindex 0
0167 len 84 hash 0 protocol 0
0168 cb 1234 ingress_ifindex 0 ifindex 0" || exit 1
0169 remove_prog out
0170 }
0171
0172 function test_ctx_in {
0173 test_start "test_ctx on lwt in"
0174 install_test in test_ctx
0175 ping -c 3 $IP_LOCAL || {
0176 failure "test_ctx out: packets are dropped"
0177 }
0178
0179
0180 match_trace "$(get_trace)" "
0181 len 84 hash 0 protocol 8
0182 cb 1234 ingress_ifindex 1 ifindex 1
0183 len 84 hash 0 protocol 8
0184 cb 1234 ingress_ifindex 1 ifindex 1
0185 len 84 hash 0 protocol 8
0186 cb 1234 ingress_ifindex 1 ifindex 1
0187 len 84 hash 0 protocol 8
0188 cb 1234 ingress_ifindex 1 ifindex 1
0189 len 84 hash 0 protocol 8
0190 cb 1234 ingress_ifindex 1 ifindex 1
0191 len 84 hash 0 protocol 8
0192 cb 1234 ingress_ifindex 1 ifindex 1" || exit 1
0193 remove_prog in
0194 }
0195
0196 function test_data {
0197 test_start "test_data on lwt $1"
0198 install_test $1 test_data
0199 ping -c 3 $IPVETH1 || {
0200 failure "test_data ${1}: packets are dropped"
0201 }
0202 match_trace "$(get_trace)" "
0203 src: 1fea8c0 dst: 2fea8c0
0204 src: 1fea8c0 dst: 2fea8c0
0205 src: 1fea8c0 dst: 2fea8c0" || exit 1
0206 remove_prog $1
0207 }
0208
0209 function test_data_in {
0210 test_start "test_data on lwt in"
0211 install_test in test_data
0212 ping -c 3 $IP_LOCAL || {
0213 failure "test_data in: packets are dropped"
0214 }
0215
0216
0217 match_trace "$(get_trace)" "
0218 src: 163a8c0 dst: 163a8c0
0219 src: 163a8c0 dst: 163a8c0
0220 src: 163a8c0 dst: 163a8c0
0221 src: 163a8c0 dst: 163a8c0
0222 src: 163a8c0 dst: 163a8c0
0223 src: 163a8c0 dst: 163a8c0" || exit 1
0224 remove_prog in
0225 }
0226
0227 function test_cb {
0228 test_start "test_cb on lwt $1"
0229 install_test $1 test_cb
0230 ping -c 3 $IPVETH1 || {
0231 failure "test_cb ${1}: packets are dropped"
0232 }
0233 match_trace "$(get_trace)" "
0234 cb0: 0 cb1: 0 cb2: 0
0235 cb3: 0 cb4: 0
0236 cb0: 0 cb1: 0 cb2: 0
0237 cb3: 0 cb4: 0
0238 cb0: 0 cb1: 0 cb2: 0
0239 cb3: 0 cb4: 0" || exit 1
0240 remove_prog $1
0241 }
0242
0243 function test_cb_in {
0244 test_start "test_cb on lwt in"
0245 install_test in test_cb
0246 ping -c 3 $IP_LOCAL || {
0247 failure "test_cb in: packets are dropped"
0248 }
0249
0250
0251 match_trace "$(get_trace)" "
0252 cb0: 0 cb1: 0 cb2: 0
0253 cb3: 0 cb4: 0
0254 cb0: 0 cb1: 0 cb2: 0
0255 cb3: 0 cb4: 0
0256 cb0: 0 cb1: 0 cb2: 0
0257 cb3: 0 cb4: 0
0258 cb0: 0 cb1: 0 cb2: 0
0259 cb3: 0 cb4: 0
0260 cb0: 0 cb1: 0 cb2: 0
0261 cb3: 0 cb4: 0
0262 cb0: 0 cb1: 0 cb2: 0
0263 cb3: 0 cb4: 0" || exit 1
0264 remove_prog in
0265 }
0266
0267 function test_drop_all {
0268 test_start "test_drop_all on lwt $1"
0269 install_test $1 drop_all
0270 ping -c 3 $IPVETH1 && {
0271 failure "test_drop_all ${1}: Unexpected success of ping"
0272 }
0273 match_trace "$(get_trace)" "
0274 dropping with: 2
0275 dropping with: 2
0276 dropping with: 2" || exit 1
0277 remove_prog $1
0278 }
0279
0280 function test_drop_all_in {
0281 test_start "test_drop_all on lwt in"
0282 install_test in drop_all
0283 ping -c 3 $IP_LOCAL && {
0284 failure "test_drop_all in: Unexpected success of ping"
0285 }
0286 match_trace "$(get_trace)" "
0287 dropping with: 2
0288 dropping with: 2
0289 dropping with: 2" || exit 1
0290 remove_prog in
0291 }
0292
0293 function test_push_ll_and_redirect {
0294 test_start "test_push_ll_and_redirect on lwt xmit"
0295 install_test xmit push_ll_and_redirect
0296 ping -c 3 $IPVETH1 || {
0297 failure "Redirected packets appear to be dropped"
0298 }
0299 match_trace "$(get_trace)" "
0300 redirected to $DST_IFINDEX
0301 redirected to $DST_IFINDEX
0302 redirected to $DST_IFINDEX" || exit 1
0303 remove_prog xmit
0304 }
0305
0306 function test_no_l2_and_redirect {
0307 test_start "test_no_l2_and_redirect on lwt xmit"
0308 install_test xmit fill_garbage_and_redirect
0309 ping -c 3 $IPVETH1 && {
0310 failure "Unexpected success despite lack of L2 header"
0311 }
0312 match_trace "$(get_trace)" "
0313 redirected to $DST_IFINDEX
0314 redirected to $DST_IFINDEX
0315 redirected to $DST_IFINDEX" || exit 1
0316 remove_prog xmit
0317 }
0318
0319 function test_rewrite {
0320 test_start "test_rewrite on lwt xmit"
0321 install_test xmit test_rewrite
0322 ping -c 3 $IPVETH1 || {
0323 failure "Rewritten packets appear to be dropped"
0324 }
0325 match_trace "$(get_trace)" "
0326 out: rewriting from 2fea8c0 to 3fea8c0
0327 out: rewriting from 2fea8c0 to 3fea8c0
0328 out: rewriting from 2fea8c0 to 3fea8c0" || exit 1
0329 remove_prog out
0330 }
0331
0332 function test_fill_garbage {
0333 test_start "test_fill_garbage on lwt xmit"
0334 install_test xmit fill_garbage
0335 ping -c 3 $IPVETH1 && {
0336 failure "test_drop_all ${1}: Unexpected success of ping"
0337 }
0338 match_trace "$(get_trace)" "
0339 Set initial 96 bytes of header to FF
0340 Set initial 96 bytes of header to FF
0341 Set initial 96 bytes of header to FF" || exit 1
0342 remove_prog xmit
0343 }
0344
0345 function test_netperf_nop {
0346 test_start "test_netperf_nop on lwt xmit"
0347 install_test xmit nop
0348 netperf -H $IPVETH1 -t TCP_STREAM || {
0349 failure "packets appear to be dropped"
0350 }
0351 match_trace "$(get_trace)" ""|| exit 1
0352 remove_prog xmit
0353 }
0354
0355 function test_netperf_redirect {
0356 test_start "test_netperf_redirect on lwt xmit"
0357 install_test xmit push_ll_and_redirect_silent
0358 netperf -H $IPVETH1 -t TCP_STREAM || {
0359 failure "Rewritten packets appear to be dropped"
0360 }
0361 match_trace "$(get_trace)" ""|| exit 1
0362 remove_prog xmit
0363 }
0364
0365 cleanup
0366 setup_one_veth $NS1 $VETH0 $VETH1 $IPVETH0 $IPVETH1 $IPVETH1b
0367 setup_one_veth $NS2 $VETH2 $VETH3 $IPVETH2 $IPVETH3
0368 ip netns exec $NS1 netserver
0369 echo 1 > ${TRACE_ROOT}/tracing_on
0370
0371 DST_MAC=$(lookup_mac $VETH1 $NS1)
0372 SRC_MAC=$(lookup_mac $VETH0)
0373 DST_IFINDEX=$(cat /sys/class/net/$VETH0/ifindex)
0374
0375 CLANG_OPTS="-O2 -target bpf -I ../include/"
0376 CLANG_OPTS+=" -DSRC_MAC=$SRC_MAC -DDST_MAC=$DST_MAC -DDST_IFINDEX=$DST_IFINDEX"
0377 clang $CLANG_OPTS -c test_lwt_bpf.c -o test_lwt_bpf.o
0378
0379 test_ctx_xmit
0380 test_ctx_out
0381 test_ctx_in
0382 test_data "xmit"
0383 test_data "out"
0384 test_data_in
0385 test_cb "xmit"
0386 test_cb "out"
0387 test_cb_in
0388 test_drop_all "xmit"
0389 test_drop_all "out"
0390 test_drop_all_in
0391 test_rewrite
0392 test_push_ll_and_redirect
0393 test_no_l2_and_redirect
0394 test_fill_garbage
0395 test_netperf_nop
0396 test_netperf_redirect
0397
0398 cleanup
0399 echo 0 > ${TRACE_ROOT}/tracing_on
0400 exit 0