3 # Uncomment to see generated bytecode
12 IPVETH0="192.168.254.1"
13 IPVETH1="192.168.254.2"
14 IPVETH1b="192.168.254.3"
16 IPVETH2="192.168.111.1"
17 IPVETH3="192.168.111.2"
19 IP_LOCAL="192.168.99.1"
21 TRACE_ROOT=/sys/kernel/debug/tracing
26 if [ ! -z "$2" ]; then
27 MAC=$(ip netns exec $2 ip link show $1 | grep ether | awk '{print $2}')
29 MAC=$(ip link show $1 | grep ether | awk '{print $2}')
32 echo "0x${MAC:10:2}${MAC:8:2}${MAC:6:2}${MAC:4:2}${MAC:2:2}${MAC:0:2}"
38 rm test_lwt_bpf.o 2> /dev/null
39 ip link del $VETH0 2> /dev/null
40 ip link del $VETH1 2> /dev/null
41 ip link del $VETH2 2> /dev/null
42 ip link del $VETH3 2> /dev/null
43 ip netns exec $NS1 killall netserver
44 ip netns delete $NS1 2> /dev/null
45 ip netns delete $NS2 2> /dev/null
49 function setup_one_veth {
51 ip link add $2 type veth peer name $3
53 ip addr add $4/24 dev $2
54 ip link set $3 netns $1
55 ip netns exec $1 ip link set dev $3 up
56 ip netns exec $1 ip addr add $5/24 dev $3
59 ip netns exec $1 ip addr add $6/32 dev $3
65 cat ${TRACE_ROOT}/trace | grep -v '^#'
69 function cleanup_routes {
70 ip route del ${IPVETH1}/32 dev $VETH0 2> /dev/null || true
71 ip route del table local local ${IP_LOCAL}/32 dev lo 2> /dev/null || true
74 function install_test {
76 cp /dev/null ${TRACE_ROOT}/trace
78 OPTS="encap bpf headroom 14 $1 obj test_lwt_bpf.o section $2 $VERBOSE"
80 if [ "$1" == "in" ]; then
81 ip route add table local local ${IP_LOCAL}/32 $OPTS dev lo
83 ip route add ${IPVETH1}/32 $OPTS dev $VETH0
87 function remove_prog {
88 if [ "$1" == "in" ]; then
89 ip route del table local local ${IP_LOCAL}/32 dev lo
91 ip route del ${IPVETH1}/32 dev $VETH0
95 function filter_trace {
96 # Add newline to allow starting EXPECT= variables on newline
98 echo "${NL}$*" | sed -e 's/^.*: : //g'
101 function expect_fail {
110 function match_trace {
115 GOT="$(filter_trace "$TRACE")"
117 [ "$GOT" != "$EXPECT" ] && {
118 expect_fail "$EXPECT" "$GOT"
125 function test_start {
127 echo "----------------------------------------------------------------"
128 echo "Starting test: $*"
129 echo "----------------------------------------------------------------"
139 function test_ctx_xmit {
140 test_start "test_ctx on lwt xmit"
141 install_test xmit test_ctx
142 ping -c 3 $IPVETH1 || {
143 failure "test_ctx xmit: packets are dropped"
145 match_trace "$(get_trace)" "
146 len 84 hash 0 protocol 8
147 cb 1234 ingress_ifindex 0 ifindex $DST_IFINDEX
148 len 84 hash 0 protocol 8
149 cb 1234 ingress_ifindex 0 ifindex $DST_IFINDEX
150 len 84 hash 0 protocol 8
151 cb 1234 ingress_ifindex 0 ifindex $DST_IFINDEX" || exit 1
155 function test_ctx_out {
156 test_start "test_ctx on lwt out"
157 install_test out test_ctx
158 ping -c 3 $IPVETH1 || {
159 failure "test_ctx out: packets are dropped"
161 match_trace "$(get_trace)" "
162 len 84 hash 0 protocol 0
163 cb 1234 ingress_ifindex 0 ifindex 0
164 len 84 hash 0 protocol 0
165 cb 1234 ingress_ifindex 0 ifindex 0
166 len 84 hash 0 protocol 0
167 cb 1234 ingress_ifindex 0 ifindex 0" || exit 1
171 function test_ctx_in {
172 test_start "test_ctx on lwt in"
173 install_test in test_ctx
174 ping -c 3 $IP_LOCAL || {
175 failure "test_ctx out: packets are dropped"
177 # We will both request & reply packets as the packets will
178 # be from $IP_LOCAL => $IP_LOCAL
179 match_trace "$(get_trace)" "
180 len 84 hash 0 protocol 8
181 cb 1234 ingress_ifindex 1 ifindex 1
182 len 84 hash 0 protocol 8
183 cb 1234 ingress_ifindex 1 ifindex 1
184 len 84 hash 0 protocol 8
185 cb 1234 ingress_ifindex 1 ifindex 1
186 len 84 hash 0 protocol 8
187 cb 1234 ingress_ifindex 1 ifindex 1
188 len 84 hash 0 protocol 8
189 cb 1234 ingress_ifindex 1 ifindex 1
190 len 84 hash 0 protocol 8
191 cb 1234 ingress_ifindex 1 ifindex 1" || exit 1
196 test_start "test_data on lwt $1"
197 install_test $1 test_data
198 ping -c 3 $IPVETH1 || {
199 failure "test_data ${1}: packets are dropped"
201 match_trace "$(get_trace)" "
202 src: 1fea8c0 dst: 2fea8c0
203 src: 1fea8c0 dst: 2fea8c0
204 src: 1fea8c0 dst: 2fea8c0" || exit 1
208 function test_data_in {
209 test_start "test_data on lwt in"
210 install_test in test_data
211 ping -c 3 $IP_LOCAL || {
212 failure "test_data in: packets are dropped"
214 # We will both request & reply packets as the packets will
215 # be from $IP_LOCAL => $IP_LOCAL
216 match_trace "$(get_trace)" "
217 src: 163a8c0 dst: 163a8c0
218 src: 163a8c0 dst: 163a8c0
219 src: 163a8c0 dst: 163a8c0
220 src: 163a8c0 dst: 163a8c0
221 src: 163a8c0 dst: 163a8c0
222 src: 163a8c0 dst: 163a8c0" || exit 1
227 test_start "test_cb on lwt $1"
228 install_test $1 test_cb
229 ping -c 3 $IPVETH1 || {
230 failure "test_cb ${1}: packets are dropped"
232 match_trace "$(get_trace)" "
238 cb3: 0 cb4: 0" || exit 1
242 function test_cb_in {
243 test_start "test_cb on lwt in"
244 install_test in test_cb
245 ping -c 3 $IP_LOCAL || {
246 failure "test_cb in: packets are dropped"
248 # We will both request & reply packets as the packets will
249 # be from $IP_LOCAL => $IP_LOCAL
250 match_trace "$(get_trace)" "
262 cb3: 0 cb4: 0" || exit 1
266 function test_drop_all {
267 test_start "test_drop_all on lwt $1"
268 install_test $1 drop_all
269 ping -c 3 $IPVETH1 && {
270 failure "test_drop_all ${1}: Unexpected success of ping"
272 match_trace "$(get_trace)" "
275 dropping with: 2" || exit 1
279 function test_drop_all_in {
280 test_start "test_drop_all on lwt in"
281 install_test in drop_all
282 ping -c 3 $IP_LOCAL && {
283 failure "test_drop_all in: Unexpected success of ping"
285 match_trace "$(get_trace)" "
288 dropping with: 2" || exit 1
292 function test_push_ll_and_redirect {
293 test_start "test_push_ll_and_redirect on lwt xmit"
294 install_test xmit push_ll_and_redirect
295 ping -c 3 $IPVETH1 || {
296 failure "Redirected packets appear to be dropped"
298 match_trace "$(get_trace)" "
299 redirected to $DST_IFINDEX
300 redirected to $DST_IFINDEX
301 redirected to $DST_IFINDEX" || exit 1
305 function test_no_l2_and_redirect {
306 test_start "test_no_l2_and_redirect on lwt xmit"
307 install_test xmit fill_garbage_and_redirect
308 ping -c 3 $IPVETH1 && {
309 failure "Unexpected success despite lack of L2 header"
311 match_trace "$(get_trace)" "
312 redirected to $DST_IFINDEX
313 redirected to $DST_IFINDEX
314 redirected to $DST_IFINDEX" || exit 1
318 function test_rewrite {
319 test_start "test_rewrite on lwt xmit"
320 install_test xmit test_rewrite
321 ping -c 3 $IPVETH1 || {
322 failure "Rewritten packets appear to be dropped"
324 match_trace "$(get_trace)" "
325 out: rewriting from 2fea8c0 to 3fea8c0
326 out: rewriting from 2fea8c0 to 3fea8c0
327 out: rewriting from 2fea8c0 to 3fea8c0" || exit 1
331 function test_fill_garbage {
332 test_start "test_fill_garbage on lwt xmit"
333 install_test xmit fill_garbage
334 ping -c 3 $IPVETH1 && {
335 failure "test_drop_all ${1}: Unexpected success of ping"
337 match_trace "$(get_trace)" "
338 Set initial 96 bytes of header to FF
339 Set initial 96 bytes of header to FF
340 Set initial 96 bytes of header to FF" || exit 1
344 function test_netperf_nop {
345 test_start "test_netperf_nop on lwt xmit"
346 install_test xmit nop
347 netperf -H $IPVETH1 -t TCP_STREAM || {
348 failure "packets appear to be dropped"
350 match_trace "$(get_trace)" ""|| exit 1
354 function test_netperf_redirect {
355 test_start "test_netperf_redirect on lwt xmit"
356 install_test xmit push_ll_and_redirect_silent
357 netperf -H $IPVETH1 -t TCP_STREAM || {
358 failure "Rewritten packets appear to be dropped"
360 match_trace "$(get_trace)" ""|| exit 1
365 setup_one_veth $NS1 $VETH0 $VETH1 $IPVETH0 $IPVETH1 $IPVETH1b
366 setup_one_veth $NS2 $VETH2 $VETH3 $IPVETH2 $IPVETH3
367 ip netns exec $NS1 netserver
368 echo 1 > ${TRACE_ROOT}/tracing_on
370 DST_MAC=$(lookup_mac $VETH1 $NS1)
371 SRC_MAC=$(lookup_mac $VETH0)
372 DST_IFINDEX=$(cat /sys/class/net/$VETH0/ifindex)
374 CLANG_OPTS="-O2 -target bpf -I ../include/"
375 CLANG_OPTS+=" -DSRC_MAC=$SRC_MAC -DDST_MAC=$DST_MAC -DDST_IFINDEX=$DST_IFINDEX"
376 clang $CLANG_OPTS -c test_lwt_bpf.c -o test_lwt_bpf.o
391 test_push_ll_and_redirect
392 test_no_l2_and_redirect
395 test_netperf_redirect
398 echo 0 > ${TRACE_ROOT}/tracing_on