xref: /freebsd/tests/sys/netinet6/lpm6.sh (revision dd41de95a84d979615a2ef11df6850622bf6184e)
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 ${lo_src}
41	# enable link-local IPv6
42	jexec ${jname}a ndp -i ${epair0}a -- -disabled
43	jexec ${jname}a ifconfig ${epair0}a up
44	jexec ${jname}a ndp -i ${epair1}a -- -disabled
45	jexec ${jname}a ifconfig ${epair1}a up
46	jexec ${jname}a ifconfig ${lo_src} up
47
48	vnet_mkjail ${jname}b ${epair0}b ${epair1}b ${lo_dst}
49	jexec ${jname}b ndp -i ${epair0}b -- -disabled
50	jexec ${jname}b ifconfig ${epair0}b up
51	jexec ${jname}b ndp -i ${epair1}b -- -disabled
52	jexec ${jname}b ifconfig ${epair1}b up
53	jexec ${jname}b ifconfig ${lo_dst} up
54
55	# wait for DAD to complete
56	while [ `jexec ${jname}b ifconfig | grep inet6 | grep -c tentative` != "0" ]; do
57		sleep 0.1
58	done
59	while [ `jexec ${jname}a ifconfig | grep inet6 | grep -c tentative` != "0" ]; do
60		sleep 0.1
61	done
62}
63
64
65atf_test_case "lpm6_test1_success" "cleanup"
66lpm6_test1_success_head()
67{
68
69	atf_set descr 'Test IPv6 LPM for the host routes'
70	atf_set require.user root
71}
72
73lpm6_test1_success_body()
74{
75
76	vnet_init
77
78	net_dst="2001:db8:"
79
80	jname="v6t-lpm6_test1_success"
81
82	epair0=$(vnet_mkepair)
83	epair1=$(vnet_mkepair)
84	lo_dst=$(vnet_mkloopback)
85
86	setup_networking ${jname} ${lo_dst} ${epair0} ${epair1}
87
88	jexec ${jname}b ifconfig ${lo_dst} inet6 ${net_dst}:2:0/128
89	jexec ${jname}b ifconfig ${lo_dst} inet6 ${net_dst}:2:1/128
90
91	# Add routes
92	# A -> towards B via epair0a LL
93	ll=`jexec ${jname}b ifconfig ${epair0}b inet6 | awk '$2~/^fe80:/{print$2}' | awk -F% '{print$1}'`
94	jexec ${jname}a route add -6 -host ${net_dst}:2:0  ${ll}%${epair0}a
95	# A -> towards B via epair1a LL
96	ll=`jexec ${jname}b ifconfig ${epair1}b inet6 | awk '$2~/^fe80:/{print$2}' | awk -F% '{print$1}'`
97	jexec ${jname}a route add -6 -host ${net_dst}:2:1 ${ll}%${epair1}a
98
99	count=20
100	valid_message="${count} packets transmitted, ${count} packets received"
101
102	# Check that ${net_dst}:2:0 goes via epair0
103	atf_check -o match:"${valid_message}" jexec ${jname}a ping -6 -f -nc${count} ${net_dst}:2:0
104	pkt_0=`jexec ${jname}a netstat -Wf link -I ${epair0}a | head | awk '$1!~/^Name/{print$8}'`
105	pkt_1=`jexec ${jname}a netstat -Wf link -I ${epair1}a | head | awk '$1!~/^Name/{print$8}'`
106	if [ ${pkt_0} -le ${count} ]; then
107		echo "LPM failure: 1: ${pkt_0} (should be ${count})  2: ${pkt_1}"
108		exit 1
109	fi
110
111	# Check that ${net_dst}:2:1 goes via epair1
112	atf_check -o match:"${valid_message}" jexec ${jname}a ping -6 -f -nc${count} ${net_dst}:2:1
113	pkt_0=`jexec ${jname}a netstat -Wf link -I ${epair0}a | head | awk '$1!~/^Name/{print$8}'`
114	pkt_1=`jexec ${jname}a netstat -Wf link -I ${epair1}a | head | awk '$1!~/^Name/{print$8}'`
115	if [ ${pkt_1} -le ${count} ]; then
116		echo "LPM failure: 1: ${pkt_0} 2: ${pkt_1} (should be ${count})"
117		exit 1
118	fi
119
120	echo "RAW BALANCING: 1: ${pkt_0} 2: ${pkt_1}"
121}
122
123lpm6_test1_success_cleanup()
124{
125	vnet_cleanup
126}
127
128atf_test_case "lpm6_test2_success" "cleanup"
129lpm6_test2_success_head()
130{
131
132	atf_set descr 'Test IPv6 LPM for /126 and /127'
133	atf_set require.user root
134}
135
136lpm6_test2_success_body()
137{
138
139	vnet_init
140
141	net_dst="2001:db8:"
142
143	jname="v6t-lpm6_test2_success"
144
145	epair0=$(vnet_mkepair)
146	epair1=$(vnet_mkepair)
147	lo_dst=$(vnet_mkloopback)
148
149	setup_networking ${jname} ${lo_dst} ${epair0} ${epair1}
150
151	jexec ${jname}b ifconfig ${lo_dst} inet6 ${net_dst}:2:0/128
152	jexec ${jname}b ifconfig ${lo_dst} inet6 ${net_dst}:2:2/128
153
154	# Add routes
155	# A -> towards B via epair0a LL
156	ll=`jexec ${jname}b ifconfig ${epair0}b inet6 | awk '$2~/^fe80:/{print$2}' | awk -F% '{print$1}'`
157	jexec ${jname}a route add -6 -net ${net_dst}:2:0/126  ${ll}%${epair0}a
158	# A -> towards B via epair1a LL
159	ll=`jexec ${jname}b ifconfig ${epair1}b inet6 | awk '$2~/^fe80:/{print$2}' | awk -F% '{print$1}'`
160	jexec ${jname}a route add -6 -net ${net_dst}:2:0/127 ${ll}%${epair1}a
161
162	count=20
163	valid_message="${count} packets transmitted, ${count} packets received"
164
165	# Check that ${net_dst}:2:0 goes via epair1
166	atf_check -o match:"${valid_message}" jexec ${jname}a ping -6 -f -nc${count} ${net_dst}:2:0
167	pkt_0=`jexec ${jname}a netstat -Wf link -I ${epair0}a | head | awk '$1!~/^Name/{print$8}'`
168	pkt_1=`jexec ${jname}a netstat -Wf link -I ${epair1}a | head | awk '$1!~/^Name/{print$8}'`
169	if [ ${pkt_1} -le ${count} ]; then
170		echo "LPM failure: 1: ${pkt_0} 2: ${pkt_1} (should be ${count})"
171		exit 1
172	fi
173
174	# Check that ${net_dst}:2:2 goes via epair0
175	atf_check -o match:"${valid_message}" jexec ${jname}a ping -6 -f -nc${count} ${net_dst}:2:2
176	pkt_0=`jexec ${jname}a netstat -Wf link -I ${epair0}a | head | awk '$1!~/^Name/{print$8}'`
177	pkt_1=`jexec ${jname}a netstat -Wf link -I ${epair1}a | head | awk '$1!~/^Name/{print$8}'`
178	if [ ${pkt_0} -le ${count} ]; then
179		echo "LPM failure: 1: ${pkt_0} (should be ${count})  2: ${pkt_1}"
180		exit 1
181	fi
182
183	echo "RAW BALANCING: 1: ${pkt_0} 2: ${pkt_1}"
184}
185
186lpm6_test2_success_cleanup()
187{
188	vnet_cleanup
189}
190
191atf_init_test_cases()
192{
193	atf_add_test_case "lpm6_test1_success"
194	atf_add_test_case "lpm6_test2_success"
195}
196
197# end
198
199
200