1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4# Kselftest framework requirement - SKIP code is 4.
5ksft_skip=4
6
7ALL_TESTS="loopback_test"
8NUM_NETIFS=2
9lib_dir=$(dirname "$0")
10source "$lib_dir"/../../../net/forwarding/tc_common.sh
11source "$lib_dir"/../../../net/forwarding/lib.sh
12
13h1_create()
14{
15	simple_if_init $h1 192.0.2.1/24
16	tc qdisc add dev $h1 clsact
17}
18
19h1_destroy()
20{
21	tc qdisc del dev $h1 clsact
22	simple_if_fini $h1 192.0.2.1/24
23}
24
25h2_create()
26{
27	simple_if_init $h2
28}
29
30h2_destroy()
31{
32	simple_if_fini $h2
33}
34
35loopback_test()
36{
37	RET=0
38
39	tc filter add dev $h1 ingress protocol arp pref 1 handle 101 flower \
40		skip_hw arp_op reply arp_tip 192.0.2.1 action drop
41
42	$MZ $h1 -c 1 -t arp -q
43
44	tc_check_packets "dev $h1 ingress" 101 1
45	check_fail $? "Matched on a filter without loopback setup"
46
47	ethtool -K $h1 loopback on
48	check_err $? "Failed to enable loopback"
49
50	setup_wait_dev $h1
51
52	$MZ $h1 -c 1 -t arp -q
53
54	tc_check_packets "dev $h1 ingress" 101 1
55	check_err $? "Did not match on filter with loopback"
56
57	ethtool -K $h1 loopback off
58	check_err $? "Failed to disable loopback"
59
60	$MZ $h1 -c 1 -t arp -q
61
62	tc_check_packets "dev $h1 ingress" 101 2
63	check_fail $? "Matched on a filter after loopback was removed"
64
65	tc filter del dev $h1 ingress protocol arp pref 1 handle 101 flower
66
67	log_test "loopback"
68}
69
70setup_prepare()
71{
72	h1=${NETIFS[p1]}
73	h2=${NETIFS[p2]}
74
75	vrf_prepare
76
77	h1_create
78	h2_create
79
80	if ethtool -k $h1 | grep loopback | grep -q fixed; then
81		log_test "SKIP: dev $h1 does not support loopback feature"
82		exit $ksft_skip
83	fi
84}
85
86cleanup()
87{
88	pre_cleanup
89
90	h2_destroy
91	h1_destroy
92
93	vrf_cleanup
94}
95
96trap cleanup EXIT
97
98setup_prepare
99setup_wait
100
101tests_run
102
103exit $EXIT_STATUS
104