xref: /freebsd/tests/sys/netinet/carp.sh (revision 6ba2210ee039f2f12878c217bcf058e9c8b26b29)
1# $FreeBSD$
2#
3# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4#
5# Copyright (c) 2020 Kristof Provost <kp@FreeBSD.org>
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. $(atf_get_srcdir)/../common/vnet.subr
29
30is_master()
31{
32	jail=$1
33	itf=$2
34
35	jexec ${jail} ifconfig ${itf} | grep carp | grep MASTER
36}
37
38wait_for_carp()
39{
40	jail1=$1
41	itf1=$2
42	jail2=$3
43	itf2=$4
44
45	while [ -z "$(is_master ${jail1} ${itf1})" ] &&
46	    [ -z "$(is_master ${jail2} ${itf2})" ]; do
47		sleep 1
48	done
49}
50
51atf_test_case "basic_v4" "cleanup"
52basic_v4_head()
53{
54	atf_set descr 'Basic CARP test (IPv4)'
55	atf_set require.user root
56}
57
58basic_v4_body()
59{
60	if ! kldstat -q -m carp; then
61		atf_skip "This test requires carp"
62	fi
63
64	vnet_init
65	bridge=$(vnet_mkbridge)
66	epair_one=$(vnet_mkepair)
67	epair_two=$(vnet_mkepair)
68
69	vnet_mkjail carp_basic_v4_one ${bridge} ${epair_one}a ${epair_two}a
70	vnet_mkjail carp_basic_v4_two ${epair_one}b
71	vnet_mkjail carp_basic_v4_three ${epair_two}b
72
73	jexec carp_basic_v4_one ifconfig ${bridge} 192.0.2.4/29 up
74	jexec carp_basic_v4_one ifconfig ${bridge} addm ${epair_one}a \
75	    addm ${epair_two}a
76	jexec carp_basic_v4_one ifconfig ${epair_one}a up
77	jexec carp_basic_v4_one ifconfig ${epair_two}a up
78
79	jexec carp_basic_v4_two ifconfig ${epair_one}b 192.0.2.202/29 up
80	jexec carp_basic_v4_two ifconfig ${epair_one}b add vhid 1 192.0.2.1/29
81
82	jexec carp_basic_v4_three ifconfig ${epair_two}b 192.0.2.203/29 up
83	jexec carp_basic_v4_three ifconfig ${epair_two}b add vhid 1 \
84	    192.0.2.1/29
85
86	wait_for_carp carp_basic_v4_two ${epair_one}b \
87	    carp_basic_v4_three ${epair_two}b
88
89	atf_check -s exit:0 -o ignore jexec carp_basic_v4_one \
90	    ping -c 3 192.0.2.1
91}
92
93basic_v4_cleanup()
94{
95	vnet_cleanup
96}
97
98atf_test_case "basic_v6" "cleanup"
99basic_v6_head()
100{
101	atf_set descr 'Basic CARP test (IPv6)'
102	atf_set require.user root
103}
104
105basic_v6_body()
106{
107	if ! kldstat -q -m carp; then
108		atf_skip "This test requires carp"
109	fi
110
111	vnet_init
112	bridge=$(vnet_mkbridge)
113	epair_one=$(vnet_mkepair)
114	epair_two=$(vnet_mkepair)
115
116	vnet_mkjail carp_basic_v6_one ${bridge} ${epair_one}a ${epair_two}a
117	vnet_mkjail carp_basic_v6_two ${epair_one}b
118	vnet_mkjail carp_basic_v6_three ${epair_two}b
119
120	jexec carp_basic_v6_one ifconfig ${bridge} inet6 2001:db8::0:4/64 up \
121	    no_dad
122	jexec carp_basic_v6_one ifconfig ${bridge} addm ${epair_one}a \
123	    addm ${epair_two}a
124	jexec carp_basic_v6_one ifconfig ${epair_one}a up
125	jexec carp_basic_v6_one ifconfig ${epair_two}a up
126
127	jexec carp_basic_v6_two ifconfig ${epair_one}b inet6 \
128	    2001:db8::1:2/64 up no_dad
129	jexec carp_basic_v6_two ifconfig ${epair_one}b inet6 add vhid 1 \
130	    2001:db8::0:1/64
131
132	jexec carp_basic_v6_three ifconfig ${epair_two}b inet6 2001:db8::1:3/64 up no_dad
133	jexec carp_basic_v6_three ifconfig ${epair_two}b inet6 add vhid 1 \
134	    2001:db8::0:1/64
135
136	wait_for_carp carp_basic_v6_two ${epair_one}b \
137	    carp_basic_v6_three ${epair_two}b
138
139	atf_check -s exit:0 -o ignore jexec carp_basic_v6_one \
140	    ping -6 -c 3 2001:db8::0:1
141}
142
143basic_v6_cleanup()
144{
145	vnet_cleanup
146}
147
148atf_init_test_cases()
149{
150	atf_add_test_case "basic_v4"
151	atf_add_test_case "basic_v6"
152}
153