2 # SPDX-License-Identifier: GPL-2.0
4 # Uncomment to see generated bytecode
13 IPVETH0
="192.168.254.1"
14 IPVETH1
="192.168.254.2"
15 IPVETH1b
="192.168.254.3"
17 IPVETH2
="192.168.111.1"
18 IPVETH3
="192.168.111.2"
20 IP_LOCAL
="192.168.99.1"
22 PROG_SRC
="test_lwt_bpf.c"
23 BPF_PROG
="test_lwt_bpf.o"
24 TRACE_ROOT
=/sys
/kernel
/tracing
25 CONTEXT_INFO
=$
(cat ${TRACE_ROOT}/trace_options |
grep context
)
30 if [ ! -z "$2" ]; then
31 MAC
=$
(ip netns
exec $2 ip link show
$1 |
grep ether |
awk '{print $2}')
33 MAC
=$
(ip link show
$1 |
grep ether |
awk '{print $2}')
36 echo "0x${MAC:10:2}${MAC:8:2}${MAC:6:2}${MAC:4:2}${MAC:2:2}${MAC:0:2}"
42 rm $BPF_PROG 2> /dev
/null
43 ip link del
$VETH0 2> /dev
/null
44 ip link del
$VETH1 2> /dev
/null
45 ip link del
$VETH2 2> /dev
/null
46 ip link del
$VETH3 2> /dev
/null
47 ip netns
exec $NS1 killall netserver
48 ip netns delete
$NS1 2> /dev
/null
49 ip netns delete
$NS2 2> /dev
/null
53 function setup_one_veth
{
55 ip link add
$2 type veth peer name
$3
57 ip addr add
$4/24 dev
$2
58 ip link
set $3 netns
$1
59 ip netns
exec $1 ip link
set dev
$3 up
60 ip netns
exec $1 ip addr add
$5/24 dev
$3
63 ip netns
exec $1 ip addr add
$6/32 dev
$3
69 cat ${TRACE_ROOT}/trace |
grep -v '^#'
73 function cleanup_routes
{
74 ip route del
${IPVETH1}/32 dev
$VETH0 2> /dev
/null || true
75 ip route del table
local local ${IP_LOCAL}/32 dev lo
2> /dev
/null || true
78 function install_test
{
80 cp /dev
/null
${TRACE_ROOT}/trace
82 OPTS
="encap bpf headroom 14 $1 obj $BPF_PROG section $2 $VERBOSE"
84 if [ "$1" == "in" ]; then
85 ip route add table
local local ${IP_LOCAL}/32 $OPTS dev lo
87 ip route add
${IPVETH1}/32 $OPTS dev
$VETH0
91 function remove_prog
{
92 if [ "$1" == "in" ]; then
93 ip route del table
local local ${IP_LOCAL}/32 dev lo
95 ip route del
${IPVETH1}/32 dev
$VETH0
99 function filter_trace
{
100 # Add newline to allow starting EXPECT= variables on newline
102 echo "${NL}$*" |
sed -e 's/bpf_trace_printk: //g'
105 function expect_fail
{
114 function match_trace
{
119 GOT
="$(filter_trace "$TRACE")"
121 [ "$GOT" != "$EXPECT" ] && {
122 expect_fail
"$EXPECT" "$GOT"
129 function test_start
{
131 echo "----------------------------------------------------------------"
132 echo "Starting test: $*"
133 echo "----------------------------------------------------------------"
143 function test_ctx_xmit
{
144 test_start
"test_ctx on lwt xmit"
145 install_test xmit test_ctx
146 ping -c 3 $IPVETH1 ||
{
147 failure
"test_ctx xmit: packets are dropped"
149 match_trace
"$(get_trace)" "
150 len 84 hash 0 protocol 8
151 cb 1234 ingress_ifindex 0 ifindex $DST_IFINDEX
152 len 84 hash 0 protocol 8
153 cb 1234 ingress_ifindex 0 ifindex $DST_IFINDEX
154 len 84 hash 0 protocol 8
155 cb 1234 ingress_ifindex 0 ifindex $DST_IFINDEX" ||
exit 1
159 function test_ctx_out
{
160 test_start
"test_ctx on lwt out"
161 install_test out test_ctx
162 ping -c 3 $IPVETH1 ||
{
163 failure
"test_ctx out: packets are dropped"
165 match_trace
"$(get_trace)" "
166 len 84 hash 0 protocol 8
167 cb 1234 ingress_ifindex 0 ifindex 0
168 len 84 hash 0 protocol 8
169 cb 1234 ingress_ifindex 0 ifindex 0
170 len 84 hash 0 protocol 8
171 cb 1234 ingress_ifindex 0 ifindex 0" ||
exit 1
175 function test_ctx_in
{
176 test_start
"test_ctx on lwt in"
177 install_test
in test_ctx
178 ping -c 3 $IP_LOCAL ||
{
179 failure
"test_ctx out: packets are dropped"
181 # We will both request & reply packets as the packets will
182 # be from $IP_LOCAL => $IP_LOCAL
183 match_trace
"$(get_trace)" "
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
192 len 84 hash 0 protocol 8
193 cb 1234 ingress_ifindex 1 ifindex 1
194 len 84 hash 0 protocol 8
195 cb 1234 ingress_ifindex 1 ifindex 1" ||
exit 1
200 test_start
"test_data on lwt $1"
201 install_test
$1 test_data
202 ping -c 3 $IPVETH1 ||
{
203 failure
"test_data ${1}: packets are dropped"
205 match_trace
"$(get_trace)" "
206 src: 1fea8c0 dst: 2fea8c0
207 src: 1fea8c0 dst: 2fea8c0
208 src: 1fea8c0 dst: 2fea8c0" ||
exit 1
212 function test_data_in
{
213 test_start
"test_data on lwt in"
214 install_test
in test_data
215 ping -c 3 $IP_LOCAL ||
{
216 failure
"test_data in: packets are dropped"
218 # We will both request & reply packets as the packets will
219 # be from $IP_LOCAL => $IP_LOCAL
220 match_trace
"$(get_trace)" "
221 src: 163a8c0 dst: 163a8c0
222 src: 163a8c0 dst: 163a8c0
223 src: 163a8c0 dst: 163a8c0
224 src: 163a8c0 dst: 163a8c0
225 src: 163a8c0 dst: 163a8c0
226 src: 163a8c0 dst: 163a8c0" ||
exit 1
231 test_start
"test_cb on lwt $1"
232 install_test
$1 test_cb
233 ping -c 3 $IPVETH1 ||
{
234 failure
"test_cb ${1}: packets are dropped"
236 match_trace
"$(get_trace)" "
242 cb3: 0 cb4: 0" ||
exit 1
246 function test_cb_in
{
247 test_start
"test_cb on lwt in"
248 install_test
in test_cb
249 ping -c 3 $IP_LOCAL ||
{
250 failure
"test_cb in: packets are dropped"
252 # We will both request & reply packets as the packets will
253 # be from $IP_LOCAL => $IP_LOCAL
254 match_trace
"$(get_trace)" "
266 cb3: 0 cb4: 0" ||
exit 1
270 function test_drop_all
{
271 test_start
"test_drop_all on lwt $1"
272 install_test
$1 drop_all
273 ping -c 3 $IPVETH1 && {
274 failure
"test_drop_all ${1}: Unexpected success of ping"
276 match_trace
"$(get_trace)" "
279 dropping with: 2" ||
exit 1
283 function test_drop_all_in
{
284 test_start
"test_drop_all on lwt in"
285 install_test
in drop_all
286 ping -c 3 $IP_LOCAL && {
287 failure
"test_drop_all in: Unexpected success of ping"
289 match_trace
"$(get_trace)" "
292 dropping with: 2" ||
exit 1
296 function test_push_ll_and_redirect
{
297 test_start
"test_push_ll_and_redirect on lwt xmit"
298 install_test xmit push_ll_and_redirect
299 ping -c 3 $IPVETH1 ||
{
300 failure
"Redirected packets appear to be dropped"
302 match_trace
"$(get_trace)" "
303 redirected to $DST_IFINDEX
304 redirected to $DST_IFINDEX
305 redirected to $DST_IFINDEX" ||
exit 1
309 function test_no_l2_and_redirect
{
310 test_start
"test_no_l2_and_redirect on lwt xmit"
311 install_test xmit fill_garbage_and_redirect
312 ping -c 3 $IPVETH1 && {
313 failure
"Unexpected success despite lack of L2 header"
315 match_trace
"$(get_trace)" "
316 redirected to $DST_IFINDEX
317 redirected to $DST_IFINDEX
318 redirected to $DST_IFINDEX" ||
exit 1
322 function test_rewrite
{
323 test_start
"test_rewrite on lwt xmit"
324 install_test xmit test_rewrite
325 ping -c 3 $IPVETH1 ||
{
326 failure
"Rewritten packets appear to be dropped"
328 match_trace
"$(get_trace)" "
329 out: rewriting from 2fea8c0 to 3fea8c0
330 out: rewriting from 2fea8c0 to 3fea8c0
331 out: rewriting from 2fea8c0 to 3fea8c0" ||
exit 1
335 function test_fill_garbage
{
336 test_start
"test_fill_garbage on lwt xmit"
337 install_test xmit fill_garbage
338 ping -c 3 $IPVETH1 && {
339 failure
"test_drop_all ${1}: Unexpected success of ping"
341 match_trace
"$(get_trace)" "
342 Set initial 96 bytes of header to FF
343 Set initial 96 bytes of header to FF
344 Set initial 96 bytes of header to FF" ||
exit 1
348 function test_netperf_nop
{
349 test_start
"test_netperf_nop on lwt xmit"
350 install_test xmit nop
351 netperf
-H $IPVETH1 -t TCP_STREAM ||
{
352 failure
"packets appear to be dropped"
354 match_trace
"$(get_trace)" ""||
exit 1
358 function test_netperf_redirect
{
359 test_start
"test_netperf_redirect on lwt xmit"
360 install_test xmit push_ll_and_redirect_silent
361 netperf
-H $IPVETH1 -t TCP_STREAM ||
{
362 failure
"Rewritten packets appear to be dropped"
364 match_trace
"$(get_trace)" ""||
exit 1
369 setup_one_veth
$NS1 $VETH0 $VETH1 $IPVETH0 $IPVETH1 $IPVETH1b
370 setup_one_veth
$NS2 $VETH2 $VETH3 $IPVETH2 $IPVETH3
371 ip netns
exec $NS1 netserver
372 echo 1 > ${TRACE_ROOT}/tracing_on
373 echo nocontext-info
> ${TRACE_ROOT}/trace_options
375 DST_MAC
=$
(lookup_mac
$VETH1 $NS1)
376 SRC_MAC
=$
(lookup_mac
$VETH0)
377 DST_IFINDEX
=$
(cat /sys
/class
/net
/$VETH0/ifindex
)
379 CLANG_OPTS
="-O2 --target=bpf -I ../include/"
380 CLANG_OPTS
+=" -DSRC_MAC=$SRC_MAC -DDST_MAC=$DST_MAC -DDST_IFINDEX=$DST_IFINDEX"
381 clang
$CLANG_OPTS -c $PROG_SRC -o $BPF_PROG
396 test_push_ll_and_redirect
397 test_no_l2_and_redirect
400 test_netperf_redirect
403 echo 0 > ${TRACE_ROOT}/tracing_on
404 echo $CONTEXT_INFO > ${TRACE_ROOT}/trace_options