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