xref: /freebsd/tests/sys/netinet/lpm.sh (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
1*768c98a7SAlexander V. Chernikov#!/usr/bin/env atf-sh
2*768c98a7SAlexander V. Chernikov#-
3*768c98a7SAlexander V. Chernikov# SPDX-License-Identifier: BSD-2-Clause
4*768c98a7SAlexander V. Chernikov#
5*768c98a7SAlexander V. Chernikov# Copyright (c) 2020 Alexander V. Chernikov
6*768c98a7SAlexander V. Chernikov#
7*768c98a7SAlexander V. Chernikov# Redistribution and use in source and binary forms, with or without
8*768c98a7SAlexander V. Chernikov# modification, are permitted provided that the following conditions
9*768c98a7SAlexander V. Chernikov# are met:
10*768c98a7SAlexander V. Chernikov# 1. Redistributions of source code must retain the above copyright
11*768c98a7SAlexander V. Chernikov#    notice, this list of conditions and the following disclaimer.
12*768c98a7SAlexander V. Chernikov# 2. Redistributions in binary form must reproduce the above copyright
13*768c98a7SAlexander V. Chernikov#    notice, this list of conditions and the following disclaimer in the
14*768c98a7SAlexander V. Chernikov#    documentation and/or other materials provided with the distribution.
15*768c98a7SAlexander V. Chernikov#
16*768c98a7SAlexander V. Chernikov# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17*768c98a7SAlexander V. Chernikov# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*768c98a7SAlexander V. Chernikov# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*768c98a7SAlexander V. Chernikov# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20*768c98a7SAlexander V. Chernikov# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*768c98a7SAlexander V. Chernikov# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22*768c98a7SAlexander V. Chernikov# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23*768c98a7SAlexander V. Chernikov# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*768c98a7SAlexander V. Chernikov# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*768c98a7SAlexander V. Chernikov# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*768c98a7SAlexander V. Chernikov# SUCH DAMAGE.
27*768c98a7SAlexander V. Chernikov#
28*768c98a7SAlexander V. Chernikov#
29*768c98a7SAlexander V. Chernikov
30*768c98a7SAlexander V. Chernikov. $(atf_get_srcdir)/../common/vnet.subr
31*768c98a7SAlexander V. Chernikov
32*768c98a7SAlexander V. Chernikovsetup_networking()
33*768c98a7SAlexander V. Chernikov{
34*768c98a7SAlexander V. Chernikov	jname="$1"
35*768c98a7SAlexander V. Chernikov	lo_dst="$2"
36*768c98a7SAlexander V. Chernikov	epair0="$3"
37*768c98a7SAlexander V. Chernikov	epair1="$4"
38*768c98a7SAlexander V. Chernikov
39*768c98a7SAlexander V. Chernikov	vnet_mkjail ${jname}a ${epair0}a ${epair1}a
40*768c98a7SAlexander V. Chernikov	# Setup transit IPv4 networks
41*768c98a7SAlexander V. Chernikov	jexec ${jname}a ifconfig ${epair0}a up
42*768c98a7SAlexander V. Chernikov	jexec ${jname}a ifconfig ${epair0}a inet 203.0.113.1/30
43*768c98a7SAlexander V. Chernikov	jexec ${jname}a ifconfig ${epair1}a up
44*768c98a7SAlexander V. Chernikov	jexec ${jname}a ifconfig ${epair1}a inet 203.0.113.5/30
45*768c98a7SAlexander V. Chernikov
46*768c98a7SAlexander V. Chernikov	vnet_mkjail ${jname}b ${epair0}b ${epair1}b ${lo_dst}
47*768c98a7SAlexander V. Chernikov	jexec ${jname}b ifconfig ${epair0}b up
48*768c98a7SAlexander V. Chernikov	jexec ${jname}b ifconfig ${epair0}b inet 203.0.113.2/30
49*768c98a7SAlexander V. Chernikov	jexec ${jname}b ifconfig ${epair1}b up
50*768c98a7SAlexander V. Chernikov	jexec ${jname}b ifconfig ${epair1}b inet 203.0.113.6/30
51*768c98a7SAlexander V. Chernikov	jexec ${jname}b ifconfig ${lo_dst} up
52*768c98a7SAlexander V. Chernikov
53*768c98a7SAlexander V. Chernikov}
54*768c98a7SAlexander V. Chernikov
55*768c98a7SAlexander V. Chernikovatf_test_case "lpm_test1_success" "cleanup"
56*768c98a7SAlexander V. Chernikovlpm_test1_success_head()
57*768c98a7SAlexander V. Chernikov{
58*768c98a7SAlexander V. Chernikov
59*768c98a7SAlexander V. Chernikov	atf_set descr 'Test IPv4 LPM for /30 and /31'
60*768c98a7SAlexander V. Chernikov	atf_set require.user root
61*768c98a7SAlexander V. Chernikov}
62*768c98a7SAlexander V. Chernikov
63*768c98a7SAlexander V. Chernikovlpm_test1_success_body()
64*768c98a7SAlexander V. Chernikov{
65*768c98a7SAlexander V. Chernikov
66*768c98a7SAlexander V. Chernikov	vnet_init
67*768c98a7SAlexander V. Chernikov
68*768c98a7SAlexander V. Chernikov	jname="v4t-lpm_test1_success"
69*768c98a7SAlexander V. Chernikov
70*768c98a7SAlexander V. Chernikov	lo_dst=$(vnet_mkloopback)
71*768c98a7SAlexander V. Chernikov	epair0=$(vnet_mkepair)
72*768c98a7SAlexander V. Chernikov	epair1=$(vnet_mkepair)
73*768c98a7SAlexander V. Chernikov
74*768c98a7SAlexander V. Chernikov	setup_networking ${jname} ${lo_dst} ${epair0} ${epair1}
75*768c98a7SAlexander V. Chernikov
76*768c98a7SAlexander V. Chernikov	jexec ${jname}b ifconfig ${lo_dst} inet 198.51.100.0/32
77*768c98a7SAlexander V. Chernikov	jexec ${jname}b ifconfig ${lo_dst} alias 198.51.100.2/32
78*768c98a7SAlexander V. Chernikov
79*768c98a7SAlexander V. Chernikov	# Add routes
80*768c98a7SAlexander V. Chernikov	# A -> towards B via epair0a
81*768c98a7SAlexander V. Chernikov	jexec ${jname}a route add -4 -net 198.51.100.0/30 203.0.113.2
82*768c98a7SAlexander V. Chernikov	# A -> towards B via epair1a
83*768c98a7SAlexander V. Chernikov	jexec ${jname}a route add -4 -net 198.51.100.0/31 203.0.113.6
84*768c98a7SAlexander V. Chernikov
85*768c98a7SAlexander V. Chernikov	count=20
86*768c98a7SAlexander V. Chernikov	valid_message="${count} packets transmitted, ${count} packets received"
87*768c98a7SAlexander V. Chernikov
88*768c98a7SAlexander V. Chernikov	# Check that 198.51.100.0 goes via epair1
89*768c98a7SAlexander V. Chernikov	atf_check -o match:"${valid_message}" jexec ${jname}a ping -f -nc${count} 198.51.100.0
90*768c98a7SAlexander V. Chernikov	pkt_0=`jexec ${jname}a netstat -Wf link -I ${epair0}a | head | awk '$1!~/^Name/{print$8}'`
91*768c98a7SAlexander V. Chernikov	pkt_1=`jexec ${jname}a netstat -Wf link -I ${epair1}a | head | awk '$1!~/^Name/{print$8}'`
92*768c98a7SAlexander V. Chernikov	if [ ${pkt_1} -le ${count} ]; then
93*768c98a7SAlexander V. Chernikov		echo "LPM failure: 1: ${pkt_0} 2: ${pkt_1} (should be ${count})"
94*768c98a7SAlexander V. Chernikov		exit 1
95*768c98a7SAlexander V. Chernikov	fi
96*768c98a7SAlexander V. Chernikov
97*768c98a7SAlexander V. Chernikov	# Check that 198.51.100.2 goes via epair0
98*768c98a7SAlexander V. Chernikov	atf_check -o match:"${valid_message}" jexec ${jname}a ping -f -nc${count} 198.51.100.2
99*768c98a7SAlexander V. Chernikov	pkt_0=`jexec ${jname}a netstat -Wf link -I ${epair0}a | head | awk '$1!~/^Name/{print$8}'`
100*768c98a7SAlexander V. Chernikov	pkt_1=`jexec ${jname}a netstat -Wf link -I ${epair1}a | head | awk '$1!~/^Name/{print$8}'`
101*768c98a7SAlexander V. Chernikov	if [ ${pkt_0} -le ${count} ]; then
102*768c98a7SAlexander V. Chernikov		echo "LPM failure: 1: ${pkt_0} (should be ${count})  2: ${pkt_1}"
103*768c98a7SAlexander V. Chernikov		exit 1
104*768c98a7SAlexander V. Chernikov	fi
105*768c98a7SAlexander V. Chernikov
106*768c98a7SAlexander V. Chernikov	echo "RAW BALANCING: 1: ${pkt_0} 2: ${pkt_1}"
107*768c98a7SAlexander V. Chernikov}
108*768c98a7SAlexander V. Chernikov
109*768c98a7SAlexander V. Chernikovlpm_test1_success_cleanup()
110*768c98a7SAlexander V. Chernikov{
111*768c98a7SAlexander V. Chernikov	vnet_cleanup
112*768c98a7SAlexander V. Chernikov}
113*768c98a7SAlexander V. Chernikov
114*768c98a7SAlexander V. Chernikovatf_test_case "lpm_test2_success" "cleanup"
115*768c98a7SAlexander V. Chernikovlpm_test2_success_head()
116*768c98a7SAlexander V. Chernikov{
117*768c98a7SAlexander V. Chernikov
118*768c98a7SAlexander V. Chernikov	atf_set descr 'Test IPv4 LPM for the host routes'
119*768c98a7SAlexander V. Chernikov	atf_set require.user root
120*768c98a7SAlexander V. Chernikov}
121*768c98a7SAlexander V. Chernikov
122*768c98a7SAlexander V. Chernikovlpm_test2_success_body()
123*768c98a7SAlexander V. Chernikov{
124*768c98a7SAlexander V. Chernikov
125*768c98a7SAlexander V. Chernikov	vnet_init
126*768c98a7SAlexander V. Chernikov
127*768c98a7SAlexander V. Chernikov	jname="v4t-lpm_test2_success"
128*768c98a7SAlexander V. Chernikov
129*768c98a7SAlexander V. Chernikov	lo_dst=$(vnet_mkloopback)
130*768c98a7SAlexander V. Chernikov	epair0=$(vnet_mkepair)
131*768c98a7SAlexander V. Chernikov	epair1=$(vnet_mkepair)
132*768c98a7SAlexander V. Chernikov
133*768c98a7SAlexander V. Chernikov	setup_networking ${jname} ${lo_dst} ${epair0} ${epair1}
134*768c98a7SAlexander V. Chernikov
135*768c98a7SAlexander V. Chernikov	jexec ${jname}b ifconfig ${lo_dst} inet 198.51.100.0/32
136*768c98a7SAlexander V. Chernikov	jexec ${jname}b ifconfig ${lo_dst} alias 198.51.100.1/32
137*768c98a7SAlexander V. Chernikov
138*768c98a7SAlexander V. Chernikov	# Add routes
139*768c98a7SAlexander V. Chernikov	# A -> towards B via epair0a
140*768c98a7SAlexander V. Chernikov	jexec ${jname}a route add -4 -host 198.51.100.0 203.0.113.2
141*768c98a7SAlexander V. Chernikov	# A -> towards B via epair1a
142*768c98a7SAlexander V. Chernikov	jexec ${jname}a route add -4 -host 198.51.100.1 203.0.113.6
143*768c98a7SAlexander V. Chernikov
144*768c98a7SAlexander V. Chernikov	count=20
145*768c98a7SAlexander V. Chernikov	valid_message="${count} packets transmitted, ${count} packets received"
146*768c98a7SAlexander V. Chernikov
147*768c98a7SAlexander V. Chernikov	# Check that 198.51.100.0 goes via epair0
148*768c98a7SAlexander V. Chernikov	atf_check -o match:"${valid_message}" jexec ${jname}a ping -f -nc${count} 198.51.100.0
149*768c98a7SAlexander V. Chernikov	pkt_0=`jexec ${jname}a netstat -Wf link -I ${epair0}a | head | awk '$1!~/^Name/{print$8}'`
150*768c98a7SAlexander V. Chernikov	pkt_1=`jexec ${jname}a netstat -Wf link -I ${epair1}a | head | awk '$1!~/^Name/{print$8}'`
151*768c98a7SAlexander V. Chernikov	if [ ${pkt_0} -le ${count} ]; then
152*768c98a7SAlexander V. Chernikov		echo "LPM failure: 1: ${pkt_0} (should be ${count})  2: ${pkt_1}"
153*768c98a7SAlexander V. Chernikov		exit 1
154*768c98a7SAlexander V. Chernikov	fi
155*768c98a7SAlexander V. Chernikov
156*768c98a7SAlexander V. Chernikov	# Check that 198.51.100.1 goes via epair1
157*768c98a7SAlexander V. Chernikov	atf_check -o match:"${valid_message}" jexec ${jname}a ping -f -nc${count} 198.51.100.1
158*768c98a7SAlexander V. Chernikov	pkt_0=`jexec ${jname}a netstat -Wf link -I ${epair0}a | head | awk '$1!~/^Name/{print$8}'`
159*768c98a7SAlexander V. Chernikov	pkt_1=`jexec ${jname}a netstat -Wf link -I ${epair1}a | head | awk '$1!~/^Name/{print$8}'`
160*768c98a7SAlexander V. Chernikov	if [ ${pkt_1} -le ${count} ]; then
161*768c98a7SAlexander V. Chernikov		echo "LPM failure: 1: ${pkt_0} 2: ${pkt_1} (should be ${count})"
162*768c98a7SAlexander V. Chernikov		exit 1
163*768c98a7SAlexander V. Chernikov	fi
164*768c98a7SAlexander V. Chernikov
165*768c98a7SAlexander V. Chernikov	echo "RAW BALANCING: 1: ${pkt_0} 2: ${pkt_1}"
166*768c98a7SAlexander V. Chernikov}
167*768c98a7SAlexander V. Chernikov
168*768c98a7SAlexander V. Chernikovlpm_test2_success_cleanup()
169*768c98a7SAlexander V. Chernikov{
170*768c98a7SAlexander V. Chernikov	vnet_cleanup
171*768c98a7SAlexander V. Chernikov}
172*768c98a7SAlexander V. Chernikov
173*768c98a7SAlexander V. Chernikovatf_init_test_cases()
174*768c98a7SAlexander V. Chernikov{
175*768c98a7SAlexander V. Chernikov	atf_add_test_case "lpm_test1_success"
176*768c98a7SAlexander V. Chernikov	atf_add_test_case "lpm_test2_success"
177*768c98a7SAlexander V. Chernikov}
178*768c98a7SAlexander V. Chernikov
179