1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4ALL_TESTS="match_cfm_opcode match_cfm_level match_cfm_level_and_opcode"
5NUM_NETIFS=2
6source tc_common.sh
7source lib.sh
8
9h1_create()
10{
11	simple_if_init $h1
12}
13
14h1_destroy()
15{
16	simple_if_fini $h1
17}
18
19h2_create()
20{
21	simple_if_init $h2
22	tc qdisc add dev $h2 clsact
23}
24
25h2_destroy()
26{
27	tc qdisc del dev $h2 clsact
28	simple_if_fini $h2
29}
30
31u8_to_hex()
32{
33	local u8=$1; shift
34
35	printf "%02x" $u8
36}
37
38generate_cfm_hdr()
39{
40	local mdl=$1; shift
41	local op=$1; shift
42	local flags=$1; shift
43	local tlv_offset=$1; shift
44
45	local cfm_hdr=$(:
46	               )"$(u8_to_hex $((mdl << 5))):"$( 	: MD level and Version
47	               )"$(u8_to_hex $op):"$(			: OpCode
48	               )"$(u8_to_hex $flags):"$(		: Flags
49	               )"$(u8_to_hex $tlv_offset)"$(		: TLV offset
50	               )
51
52	echo $cfm_hdr
53}
54
55match_cfm_opcode()
56{
57	local ethtype="89 02"; readonly ethtype
58	RET=0
59
60	tc filter add dev $h2 ingress protocol cfm pref 1 handle 101 \
61	   flower cfm op 47 action drop
62	tc filter add dev $h2 ingress protocol cfm pref 1 handle 102 \
63	   flower cfm op 43 action drop
64
65	pkt="$ethtype $(generate_cfm_hdr 7 47 0 32)"
66	$MZ $h1 -c 1 -p 64 -a $h1mac -b $h2mac "$pkt" -q
67	pkt="$ethtype $(generate_cfm_hdr 6 5 0 4)"
68	$MZ $h1 -c 1 -p 64 -a $h1mac -b $h2mac "$pkt" -q
69
70	tc_check_packets "dev $h2 ingress" 101 1
71	check_err $? "Did not match on correct opcode"
72
73	tc_check_packets "dev $h2 ingress" 102 0
74	check_err $? "Matched on the wrong opcode"
75
76	pkt="$ethtype $(generate_cfm_hdr 0 43 0 12)"
77	$MZ $h1 -c 1 -p 64 -a $h1mac -b $h2mac "$pkt" -q
78
79	tc_check_packets "dev $h2 ingress" 101 1
80	check_err $? "Matched on the wrong opcode"
81
82	tc_check_packets "dev $h2 ingress" 102 1
83	check_err $? "Did not match on correct opcode"
84
85	tc filter del dev $h2 ingress protocol cfm pref 1 handle 101 flower
86	tc filter del dev $h2 ingress protocol cfm pref 1 handle 102 flower
87
88	log_test "CFM opcode match test"
89}
90
91match_cfm_level()
92{
93	local ethtype="89 02"; readonly ethtype
94	RET=0
95
96	tc filter add dev $h2 ingress protocol cfm pref 1 handle 101 \
97	   flower cfm mdl 5 action drop
98	tc filter add dev $h2 ingress protocol cfm pref 1 handle 102 \
99	   flower cfm mdl 3 action drop
100	tc filter add dev $h2 ingress protocol cfm pref 1 handle 103 \
101	   flower cfm mdl 0 action drop
102
103	pkt="$ethtype $(generate_cfm_hdr 5 42 0 12)"
104	$MZ $h1 -c 1 -p 64 -a $h1mac -b $h2mac "$pkt" -q
105	pkt="$ethtype $(generate_cfm_hdr 6 1 0 70)"
106	$MZ $h1 -c 1 -p 64 -a $h1mac -b $h2mac "$pkt" -q
107	pkt="$ethtype $(generate_cfm_hdr 0 1 0 70)"
108	$MZ $h1 -c 1 -p 64 -a $h1mac -b $h2mac "$pkt" -q
109
110	tc_check_packets "dev $h2 ingress" 101 1
111	check_err $? "Did not match on correct level"
112
113	tc_check_packets "dev $h2 ingress" 102 0
114	check_err $? "Matched on the wrong level"
115
116	tc_check_packets "dev $h2 ingress" 103 1
117	check_err $? "Did not match on correct level"
118
119	pkt="$ethtype $(generate_cfm_hdr 3 0 0 4)"
120	$MZ $h1 -c 1 -p 64 -a $h1mac -b $h2mac "$pkt" -q
121
122	tc_check_packets "dev $h2 ingress" 101 1
123	check_err $? "Matched on the wrong level"
124
125	tc_check_packets "dev $h2 ingress" 102 1
126	check_err $? "Did not match on correct level"
127
128	tc_check_packets "dev $h2 ingress" 103 1
129	check_err $? "Matched on the wrong level"
130
131	tc filter del dev $h2 ingress protocol cfm pref 1 handle 101 flower
132	tc filter del dev $h2 ingress protocol cfm pref 1 handle 102 flower
133	tc filter del dev $h2 ingress protocol cfm pref 1 handle 103 flower
134
135	log_test "CFM level match test"
136}
137
138match_cfm_level_and_opcode()
139{
140	local ethtype="89 02"; readonly ethtype
141	RET=0
142
143	tc filter add dev $h2 ingress protocol cfm pref 1 handle 101 \
144	   flower cfm mdl 5 op 41 action drop
145	tc filter add dev $h2 ingress protocol cfm pref 1 handle 102 \
146	   flower cfm mdl 7 op 42 action drop
147
148	pkt="$ethtype $(generate_cfm_hdr 5 41 0 4)"
149	$MZ $h1 -c 1 -p 64 -a $h1mac -b $h2mac "$pkt" -q
150	pkt="$ethtype $(generate_cfm_hdr 7 3 0 4)"
151	$MZ $h1 -c 1 -p 64 -a $h1mac -b $h2mac "$pkt" -q
152	pkt="$ethtype $(generate_cfm_hdr 3 42 0 12)"
153	$MZ $h1 -c 1 -p 64 -a $h1mac -b $h2mac "$pkt" -q
154
155	tc_check_packets "dev $h2 ingress" 101 1
156	check_err $? "Did not match on correct level and opcode"
157
158	tc_check_packets "dev $h2 ingress" 102 0
159	check_err $? "Matched on the wrong level and opcode"
160
161	pkt="$ethtype $(generate_cfm_hdr 7 42 0 12)"
162	$MZ $h1 -c 1 -p 64 -a $h1mac -b $h2mac "$pkt" -q
163
164	tc_check_packets "dev $h2 ingress" 101 1
165	check_err $? "Matched on the wrong level and opcode"
166
167	tc_check_packets "dev $h2 ingress" 102 1
168	check_err $? "Did not match on correct level and opcode"
169
170	tc filter del dev $h2 ingress protocol cfm pref 1 handle 101 flower
171	tc filter del dev $h2 ingress protocol cfm pref 1 handle 102 flower
172
173	log_test "CFM opcode and level match test"
174}
175
176setup_prepare()
177{
178	h1=${NETIFS[p1]}
179	h2=${NETIFS[p2]}
180	h1mac=$(mac_get $h1)
181	h2mac=$(mac_get $h2)
182
183	vrf_prepare
184
185	h1_create
186	h2_create
187}
188
189cleanup()
190{
191	pre_cleanup
192
193	h2_destroy
194	h1_destroy
195
196	vrf_cleanup
197}
198
199trap cleanup EXIT
200
201setup_prepare
202setup_wait
203
204tests_run
205
206exit $EXIT_STATUS
207