xref: /minix3/tests/net/carp/t_basic.c (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc /*	$NetBSD: t_basic.c,v 1.5 2011/06/26 13:13:31 christos Exp $	*/
2*11be35a1SLionel Sambuc 
3*11be35a1SLionel Sambuc /*-
4*11be35a1SLionel Sambuc  * Copyright (c) 2010 The NetBSD Foundation, Inc.
5*11be35a1SLionel Sambuc  * All rights reserved.
6*11be35a1SLionel Sambuc  *
7*11be35a1SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
8*11be35a1SLionel Sambuc  * modification, are permitted provided that the following conditions
9*11be35a1SLionel Sambuc  * are met:
10*11be35a1SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
11*11be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
12*11be35a1SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
13*11be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
14*11be35a1SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
15*11be35a1SLionel Sambuc  *
16*11be35a1SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17*11be35a1SLionel Sambuc  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18*11be35a1SLionel Sambuc  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19*11be35a1SLionel Sambuc  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20*11be35a1SLionel Sambuc  * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21*11be35a1SLionel Sambuc  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*11be35a1SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23*11be35a1SLionel Sambuc  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*11be35a1SLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25*11be35a1SLionel Sambuc  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26*11be35a1SLionel Sambuc  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27*11be35a1SLionel Sambuc  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*11be35a1SLionel Sambuc  */
29*11be35a1SLionel Sambuc 
30*11be35a1SLionel Sambuc #include <sys/cdefs.h>
31*11be35a1SLionel Sambuc #ifndef lint
32*11be35a1SLionel Sambuc __RCSID("$NetBSD: t_basic.c,v 1.5 2011/06/26 13:13:31 christos Exp $");
33*11be35a1SLionel Sambuc #endif /* not lint */
34*11be35a1SLionel Sambuc 
35*11be35a1SLionel Sambuc #include <sys/types.h>
36*11be35a1SLionel Sambuc #include <sys/socket.h>
37*11be35a1SLionel Sambuc #include <sys/wait.h>
38*11be35a1SLionel Sambuc 
39*11be35a1SLionel Sambuc #include <netinet/in.h>
40*11be35a1SLionel Sambuc #include <netinet/in_systm.h>
41*11be35a1SLionel Sambuc #include <netinet/ip_carp.h>
42*11be35a1SLionel Sambuc 
43*11be35a1SLionel Sambuc #include <rump/rump.h>
44*11be35a1SLionel Sambuc #include <rump/rump_syscalls.h>
45*11be35a1SLionel Sambuc 
46*11be35a1SLionel Sambuc #include <atf-c.h>
47*11be35a1SLionel Sambuc #include <errno.h>
48*11be35a1SLionel Sambuc #include <stdio.h>
49*11be35a1SLionel Sambuc #include <stdlib.h>
50*11be35a1SLionel Sambuc #include <string.h>
51*11be35a1SLionel Sambuc #include <unistd.h>
52*11be35a1SLionel Sambuc #include <signal.h>
53*11be35a1SLionel Sambuc 
54*11be35a1SLionel Sambuc #include "../config/netconfig.c"
55*11be35a1SLionel Sambuc #include "../../h_macros.h"
56*11be35a1SLionel Sambuc 
57*11be35a1SLionel Sambuc static bool oknow = false;
58*11be35a1SLionel Sambuc 
59*11be35a1SLionel Sambuc static void
sighnd(int sig)60*11be35a1SLionel Sambuc sighnd(int sig)
61*11be35a1SLionel Sambuc {
62*11be35a1SLionel Sambuc 
63*11be35a1SLionel Sambuc 	ATF_REQUIRE_EQ(sig, SIGCHLD);
64*11be35a1SLionel Sambuc 	if (oknow)
65*11be35a1SLionel Sambuc 		return;
66*11be35a1SLionel Sambuc 
67*11be35a1SLionel Sambuc 	atf_tc_fail("child died unexpectedly");
68*11be35a1SLionel Sambuc }
69*11be35a1SLionel Sambuc 
70*11be35a1SLionel Sambuc ATF_TC(handover);
ATF_TC_HEAD(handover,tc)71*11be35a1SLionel Sambuc ATF_TC_HEAD(handover, tc)
72*11be35a1SLionel Sambuc {
73*11be35a1SLionel Sambuc 
74*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "check that carp handover works if "
75*11be35a1SLionel Sambuc 	    "the master dies");
76*11be35a1SLionel Sambuc }
77*11be35a1SLionel Sambuc 
78*11be35a1SLionel Sambuc #define THEBUS "buuuuuuus,etherbus"
79*11be35a1SLionel Sambuc 
80*11be35a1SLionel Sambuc static void
child(bool master)81*11be35a1SLionel Sambuc child(bool master)
82*11be35a1SLionel Sambuc {
83*11be35a1SLionel Sambuc 	char ifname[IFNAMSIZ];
84*11be35a1SLionel Sambuc 	struct carpreq cr;
85*11be35a1SLionel Sambuc 	struct ifreq ifr;
86*11be35a1SLionel Sambuc 	const char *carpif;
87*11be35a1SLionel Sambuc 	int s;
88*11be35a1SLionel Sambuc 
89*11be35a1SLionel Sambuc 	/* helps reading carp debug output */
90*11be35a1SLionel Sambuc 	if (master)
91*11be35a1SLionel Sambuc 		carpif = "carp0";
92*11be35a1SLionel Sambuc 	else
93*11be35a1SLionel Sambuc 		carpif = "carp1";
94*11be35a1SLionel Sambuc 
95*11be35a1SLionel Sambuc 	/*
96*11be35a1SLionel Sambuc 	 * Should use sysctl, bug debug is dabug.
97*11be35a1SLionel Sambuc 	 */
98*11be35a1SLionel Sambuc 	{
99*11be35a1SLionel Sambuc 	//extern int rumpns_carp_opts[]; /* XXX */
100*11be35a1SLionel Sambuc 	//rumpns_carp_opts[CARPCTL_LOG] = 1;
101*11be35a1SLionel Sambuc 	}
102*11be35a1SLionel Sambuc 
103*11be35a1SLionel Sambuc 
104*11be35a1SLionel Sambuc 	rump_init();
105*11be35a1SLionel Sambuc 
106*11be35a1SLionel Sambuc 	memset(&ifr, 0, sizeof(ifr));
107*11be35a1SLionel Sambuc 	strlcpy(ifr.ifr_name, carpif, sizeof(ifr.ifr_name));
108*11be35a1SLionel Sambuc 
109*11be35a1SLionel Sambuc 	RL(s = rump_sys_socket(PF_INET, SOCK_DGRAM, 0));
110*11be35a1SLionel Sambuc 	RL(rump_sys_ioctl(s, SIOCIFCREATE, &ifr));
111*11be35a1SLionel Sambuc 
112*11be35a1SLionel Sambuc 	netcfg_rump_makeshmif(THEBUS, ifname);
113*11be35a1SLionel Sambuc 
114*11be35a1SLionel Sambuc 	if (master)
115*11be35a1SLionel Sambuc 		netcfg_rump_if(ifname, "10.1.1.1", "255.255.255.0");
116*11be35a1SLionel Sambuc 	else
117*11be35a1SLionel Sambuc 		netcfg_rump_if(ifname, "10.1.1.2", "255.255.255.0");
118*11be35a1SLionel Sambuc 
119*11be35a1SLionel Sambuc 	/* configure the carp interface */
120*11be35a1SLionel Sambuc 	ifr.ifr_data = &cr;
121*11be35a1SLionel Sambuc 	RL(rump_sys_ioctl(s, SIOCGVH, &ifr));
122*11be35a1SLionel Sambuc 
123*11be35a1SLionel Sambuc 	strlcpy(cr.carpr_carpdev, ifname, sizeof(cr.carpr_carpdev));
124*11be35a1SLionel Sambuc 	cr.carpr_vhid = 175;
125*11be35a1SLionel Sambuc 	if (master)
126*11be35a1SLionel Sambuc 		cr.carpr_advskew = 0;
127*11be35a1SLionel Sambuc 	else
128*11be35a1SLionel Sambuc 		cr.carpr_advskew = 200;
129*11be35a1SLionel Sambuc 	cr.carpr_advbase = 1;
130*11be35a1SLionel Sambuc 	strcpy((char *)cr.carpr_key, "s3cret");
131*11be35a1SLionel Sambuc 
132*11be35a1SLionel Sambuc 	RL(rump_sys_ioctl(s, SIOCSVH, &ifr));
133*11be35a1SLionel Sambuc 	netcfg_rump_if(carpif, "10.1.1.100", "255.255.255.0");
134*11be35a1SLionel Sambuc 
135*11be35a1SLionel Sambuc 	/* tassa pause()en enka muuta voi */
136*11be35a1SLionel Sambuc 	pause();
137*11be35a1SLionel Sambuc }
138*11be35a1SLionel Sambuc 
ATF_TC_BODY(handover,tc)139*11be35a1SLionel Sambuc ATF_TC_BODY(handover, tc)
140*11be35a1SLionel Sambuc {
141*11be35a1SLionel Sambuc 	char ifname[IFNAMSIZ];
142*11be35a1SLionel Sambuc 	pid_t mpid, cpid;
143*11be35a1SLionel Sambuc 	int i, status;
144*11be35a1SLionel Sambuc 
145*11be35a1SLionel Sambuc 	signal(SIGCHLD, sighnd);
146*11be35a1SLionel Sambuc 
147*11be35a1SLionel Sambuc 	/* fork master */
148*11be35a1SLionel Sambuc 	switch (mpid = fork()) {
149*11be35a1SLionel Sambuc 	case -1:
150*11be35a1SLionel Sambuc 		atf_tc_fail_errno("fork failed");
151*11be35a1SLionel Sambuc 		/*NOTREACHED*/
152*11be35a1SLionel Sambuc 	case 0:
153*11be35a1SLionel Sambuc 		child(true);
154*11be35a1SLionel Sambuc 		/*NOTREACHED*/
155*11be35a1SLionel Sambuc 	default:
156*11be35a1SLionel Sambuc 		break;
157*11be35a1SLionel Sambuc 	}
158*11be35a1SLionel Sambuc 
159*11be35a1SLionel Sambuc 	usleep(500000);
160*11be35a1SLionel Sambuc 
161*11be35a1SLionel Sambuc 	/* fork backup */
162*11be35a1SLionel Sambuc 	switch (cpid = fork()) {
163*11be35a1SLionel Sambuc 	case -1:
164*11be35a1SLionel Sambuc 		kill(mpid, SIGKILL);
165*11be35a1SLionel Sambuc 		atf_tc_fail_errno("fork failed");
166*11be35a1SLionel Sambuc 		/*NOTREACHED*/
167*11be35a1SLionel Sambuc 	case 0:
168*11be35a1SLionel Sambuc 		child(false);
169*11be35a1SLionel Sambuc 		/*NOTREACHED*/
170*11be35a1SLionel Sambuc 	default:
171*11be35a1SLionel Sambuc 		break;
172*11be35a1SLionel Sambuc 	}
173*11be35a1SLionel Sambuc 
174*11be35a1SLionel Sambuc 	usleep(500000);
175*11be35a1SLionel Sambuc 
176*11be35a1SLionel Sambuc 	rump_init();
177*11be35a1SLionel Sambuc 	netcfg_rump_makeshmif(THEBUS, ifname);
178*11be35a1SLionel Sambuc 	netcfg_rump_if(ifname, "10.1.1.240", "255.255.255.0");
179*11be35a1SLionel Sambuc 
180*11be35a1SLionel Sambuc 	/* check that the primary addresses are up */
181*11be35a1SLionel Sambuc 	ATF_REQUIRE_EQ(netcfg_rump_pingtest("10.1.1.1", 1000), true);
182*11be35a1SLionel Sambuc 	ATF_REQUIRE_EQ(netcfg_rump_pingtest("10.1.1.2", 1000), true);
183*11be35a1SLionel Sambuc 
184*11be35a1SLionel Sambuc 	/* give carp a while to croak */
185*11be35a1SLionel Sambuc 	sleep(4);
186*11be35a1SLionel Sambuc 
187*11be35a1SLionel Sambuc 	/* check that the shared IP works */
188*11be35a1SLionel Sambuc 	ATF_REQUIRE_EQ(netcfg_rump_pingtest("10.1.1.100", 500), true);
189*11be35a1SLionel Sambuc 
190*11be35a1SLionel Sambuc 	/* KILLING SPREE */
191*11be35a1SLionel Sambuc 	oknow = true;
192*11be35a1SLionel Sambuc 	kill(mpid, SIGKILL);
193*11be35a1SLionel Sambuc 	wait(&status);
194*11be35a1SLionel Sambuc 	usleep(10000); /* just in case */
195*11be35a1SLionel Sambuc 	oknow = false;
196*11be35a1SLionel Sambuc 
197*11be35a1SLionel Sambuc 	/* check that primary is now dead */
198*11be35a1SLionel Sambuc 	ATF_REQUIRE_EQ(netcfg_rump_pingtest("10.1.1.1", 100), false);
199*11be35a1SLionel Sambuc 
200*11be35a1SLionel Sambuc 	/* do it in installments. carp will cluck meanwhile */
201*11be35a1SLionel Sambuc 	for (i = 0; i < 5; i++) {
202*11be35a1SLionel Sambuc 		if (netcfg_rump_pingtest("10.1.1.100", 1000) == true)
203*11be35a1SLionel Sambuc 			break;
204*11be35a1SLionel Sambuc 	}
205*11be35a1SLionel Sambuc 	if (i == 5)
206*11be35a1SLionel Sambuc 		atf_tc_fail("failed to failover");
207*11be35a1SLionel Sambuc 
208*11be35a1SLionel Sambuc 	/* to kill the child */
209*11be35a1SLionel Sambuc 	oknow = true;
210*11be35a1SLionel Sambuc 	kill(cpid, SIGKILL);
211*11be35a1SLionel Sambuc 
212*11be35a1SLionel Sambuc 	/* clean & done */
213*11be35a1SLionel Sambuc }
214*11be35a1SLionel Sambuc 
ATF_TP_ADD_TCS(tp)215*11be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
216*11be35a1SLionel Sambuc {
217*11be35a1SLionel Sambuc 
218*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, handover);
219*11be35a1SLionel Sambuc 
220*11be35a1SLionel Sambuc 	return atf_no_error();
221*11be35a1SLionel Sambuc }
222