1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4# This test is for checking the [no]localbypass VXLAN device option. The test
5# configures two VXLAN devices in the same network namespace and a tc filter on
6# the loopback device that drops encapsulated packets. The test sends packets
7# from the first VXLAN device and verifies that by default these packets are
8# received by the second VXLAN device. The test then enables the nolocalbypass
9# option and verifies that packets are no longer received by the second VXLAN
10# device.
11
12ret=0
13# Kselftest framework requirement - SKIP code is 4.
14ksft_skip=4
15
16TESTS="
17	nolocalbypass
18"
19VERBOSE=0
20PAUSE_ON_FAIL=no
21PAUSE=no
22
23################################################################################
24# Utilities
25
26log_test()
27{
28	local rc=$1
29	local expected=$2
30	local msg="$3"
31
32	if [ ${rc} -eq ${expected} ]; then
33		printf "TEST: %-60s  [ OK ]\n" "${msg}"
34		nsuccess=$((nsuccess+1))
35	else
36		ret=1
37		nfail=$((nfail+1))
38		printf "TEST: %-60s  [FAIL]\n" "${msg}"
39		if [ "$VERBOSE" = "1" ]; then
40			echo "    rc=$rc, expected $expected"
41		fi
42
43		if [ "${PAUSE_ON_FAIL}" = "yes" ]; then
44		echo
45			echo "hit enter to continue, 'q' to quit"
46			read a
47			[ "$a" = "q" ] && exit 1
48		fi
49	fi
50
51	if [ "${PAUSE}" = "yes" ]; then
52		echo
53		echo "hit enter to continue, 'q' to quit"
54		read a
55		[ "$a" = "q" ] && exit 1
56	fi
57
58	[ "$VERBOSE" = "1" ] && echo
59}
60
61run_cmd()
62{
63	local cmd="$1"
64	local out
65	local stderr="2>/dev/null"
66
67	if [ "$VERBOSE" = "1" ]; then
68		printf "COMMAND: $cmd\n"
69		stderr=
70	fi
71
72	out=$(eval $cmd $stderr)
73	rc=$?
74	if [ "$VERBOSE" = "1" -a -n "$out" ]; then
75		echo "    $out"
76	fi
77
78	return $rc
79}
80
81tc_check_packets()
82{
83	local ns=$1; shift
84	local id=$1; shift
85	local handle=$1; shift
86	local count=$1; shift
87	local pkts
88
89	sleep 0.1
90	pkts=$(tc -n $ns -j -s filter show $id \
91		| jq ".[] | select(.options.handle == $handle) | \
92		.options.actions[0].stats.packets")
93	[[ $pkts == $count ]]
94}
95
96################################################################################
97# Setup
98
99setup()
100{
101	ip netns add ns1
102
103	ip -n ns1 link set dev lo up
104	ip -n ns1 address add 192.0.2.1/32 dev lo
105	ip -n ns1 address add 198.51.100.1/32 dev lo
106
107	ip -n ns1 link add name vx0 up type vxlan id 100 local 198.51.100.1 \
108		dstport 4789 nolearning
109	ip -n ns1 link add name vx1 up type vxlan id 100 dstport 4790
110}
111
112cleanup()
113{
114	ip netns del ns1 &> /dev/null
115}
116
117################################################################################
118# Tests
119
120nolocalbypass()
121{
122	local smac=00:01:02:03:04:05
123	local dmac=00:0a:0b:0c:0d:0e
124
125	run_cmd "bridge -n ns1 fdb add $dmac dev vx0 self static dst 192.0.2.1 port 4790"
126
127	run_cmd "tc -n ns1 qdisc add dev vx1 clsact"
128	run_cmd "tc -n ns1 filter add dev vx1 ingress pref 1 handle 101 proto all flower src_mac $smac dst_mac $dmac action pass"
129
130	run_cmd "tc -n ns1 qdisc add dev lo clsact"
131	run_cmd "tc -n ns1 filter add dev lo ingress pref 1 handle 101 proto ip flower ip_proto udp dst_port 4790 action drop"
132
133	run_cmd "ip -n ns1 -d -j link show dev vx0 | jq -e '.[][\"linkinfo\"][\"info_data\"][\"localbypass\"] == true'"
134	log_test $? 0 "localbypass enabled"
135
136	run_cmd "ip netns exec ns1 mausezahn vx0 -a $smac -b $dmac -c 1 -p 100 -q"
137
138	tc_check_packets "ns1" "dev vx1 ingress" 101 1
139	log_test $? 0 "Packet received by local VXLAN device - localbypass"
140
141	run_cmd "ip -n ns1 link set dev vx0 type vxlan nolocalbypass"
142
143	run_cmd "ip -n ns1 -d -j link show dev vx0 | jq -e '.[][\"linkinfo\"][\"info_data\"][\"localbypass\"] == false'"
144	log_test $? 0 "localbypass disabled"
145
146	run_cmd "ip netns exec ns1 mausezahn vx0 -a $smac -b $dmac -c 1 -p 100 -q"
147
148	tc_check_packets "ns1" "dev vx1 ingress" 101 1
149	log_test $? 0 "Packet not received by local VXLAN device - nolocalbypass"
150
151	run_cmd "ip -n ns1 link set dev vx0 type vxlan localbypass"
152
153	run_cmd "ip -n ns1 -d -j link show dev vx0 | jq -e '.[][\"linkinfo\"][\"info_data\"][\"localbypass\"] == true'"
154	log_test $? 0 "localbypass enabled"
155
156	run_cmd "ip netns exec ns1 mausezahn vx0 -a $smac -b $dmac -c 1 -p 100 -q"
157
158	tc_check_packets "ns1" "dev vx1 ingress" 101 2
159	log_test $? 0 "Packet received by local VXLAN device - localbypass"
160}
161
162################################################################################
163# Usage
164
165usage()
166{
167	cat <<EOF
168usage: ${0##*/} OPTS
169
170        -t <test>   Test(s) to run (default: all)
171                    (options: $TESTS)
172        -p          Pause on fail
173        -P          Pause after each test before cleanup
174        -v          Verbose mode (show commands and output)
175EOF
176}
177
178################################################################################
179# Main
180
181trap cleanup EXIT
182
183while getopts ":t:pPvh" opt; do
184	case $opt in
185		t) TESTS=$OPTARG ;;
186		p) PAUSE_ON_FAIL=yes;;
187		P) PAUSE=yes;;
188		v) VERBOSE=$(($VERBOSE + 1));;
189		h) usage; exit 0;;
190		*) usage; exit 1;;
191	esac
192done
193
194# Make sure we don't pause twice.
195[ "${PAUSE}" = "yes" ] && PAUSE_ON_FAIL=no
196
197if [ "$(id -u)" -ne 0 ];then
198	echo "SKIP: Need root privileges"
199	exit $ksft_skip;
200fi
201
202if [ ! -x "$(command -v ip)" ]; then
203	echo "SKIP: Could not run test without ip tool"
204	exit $ksft_skip
205fi
206
207if [ ! -x "$(command -v bridge)" ]; then
208	echo "SKIP: Could not run test without bridge tool"
209	exit $ksft_skip
210fi
211
212if [ ! -x "$(command -v mausezahn)" ]; then
213	echo "SKIP: Could not run test without mausezahn tool"
214	exit $ksft_skip
215fi
216
217if [ ! -x "$(command -v jq)" ]; then
218	echo "SKIP: Could not run test without jq tool"
219	exit $ksft_skip
220fi
221
222ip link help vxlan 2>&1 | grep -q "localbypass"
223if [ $? -ne 0 ]; then
224	echo "SKIP: iproute2 ip too old, missing VXLAN nolocalbypass support"
225	exit $ksft_skip
226fi
227
228cleanup
229
230for t in $TESTS
231do
232	setup; $t; cleanup;
233done
234
235if [ "$TESTS" != "none" ]; then
236	printf "\nTests passed: %3d\n" ${nsuccess}
237	printf "Tests failed: %3d\n"   ${nfail}
238fi
239
240exit $ret
241