xref: /freebsd/sys/rpc/clnt_rc.c (revision 721351876cd4d3a8a700f62d2061331fa951a488)
1 /*-
2  * Copyright (c) 2008 Isilon Inc http://www.isilon.com/
3  * Authors: Doug Rabson <dfr@rabson.org>
4  * Developed with Red Inc: Alfred Perlstein <alfred@freebsd.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/lock.h>
34 #include <sys/malloc.h>
35 #include <sys/mbuf.h>
36 #include <sys/mutex.h>
37 #include <sys/pcpu.h>
38 #include <sys/proc.h>
39 #include <sys/socket.h>
40 #include <sys/socketvar.h>
41 #include <sys/time.h>
42 #include <sys/uio.h>
43 
44 #include <rpc/rpc.h>
45 #include <rpc/rpc_com.h>
46 
47 static enum clnt_stat clnt_reconnect_call(CLIENT *, rpcproc_t,
48     xdrproc_t, void *, xdrproc_t, void *, struct timeval);
49 static void clnt_reconnect_geterr(CLIENT *, struct rpc_err *);
50 static bool_t clnt_reconnect_freeres(CLIENT *, xdrproc_t, void *);
51 static void clnt_reconnect_abort(CLIENT *);
52 static bool_t clnt_reconnect_control(CLIENT *, u_int, void *);
53 static void clnt_reconnect_destroy(CLIENT *);
54 
55 static struct clnt_ops clnt_reconnect_ops = {
56 	.cl_call =	clnt_reconnect_call,
57 	.cl_abort =	clnt_reconnect_abort,
58 	.cl_geterr =	clnt_reconnect_geterr,
59 	.cl_freeres =	clnt_reconnect_freeres,
60 	.cl_destroy =	clnt_reconnect_destroy,
61 	.cl_control =	clnt_reconnect_control
62 };
63 
64 struct rc_data {
65 	struct sockaddr_storage	rc_addr; /* server address */
66 	struct netconfig*	rc_nconf; /* network type */
67 	rpcprog_t		rc_prog;  /* program number */
68 	rpcvers_t		rc_vers;  /* version number */
69 	size_t			rc_sendsz;
70 	size_t			rc_recvsz;
71 	struct timeval		rc_timeout;
72 	struct timeval		rc_retry;
73 	const char		*rc_waitchan;
74 	int			rc_intr;
75 	CLIENT*			rc_client; /* underlying RPC client */
76 };
77 
78 CLIENT *
79 clnt_reconnect_create(
80 	struct netconfig *nconf,	/* network type */
81 	struct sockaddr *svcaddr,	/* servers address */
82 	rpcprog_t program,		/* program number */
83 	rpcvers_t version,		/* version number */
84 	size_t sendsz,			/* buffer recv size */
85 	size_t recvsz)			/* buffer send size */
86 {
87 	CLIENT *cl = NULL;		/* client handle */
88 	struct rc_data *rc = NULL;	/* private data */
89 
90 	if (svcaddr == NULL) {
91 		rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
92 		return (NULL);
93 	}
94 
95 	cl = mem_alloc(sizeof (CLIENT));
96 	rc = mem_alloc(sizeof (*rc));
97 	(void) memcpy(&rc->rc_addr, svcaddr, (size_t)svcaddr->sa_len);
98 	rc->rc_nconf = nconf;
99 	rc->rc_prog = program;
100 	rc->rc_vers = version;
101 	rc->rc_sendsz = sendsz;
102 	rc->rc_recvsz = recvsz;
103 	rc->rc_timeout.tv_sec = -1;
104 	rc->rc_timeout.tv_usec = -1;
105 	rc->rc_retry.tv_sec = 15;
106 	rc->rc_retry.tv_usec = 0;
107 	rc->rc_waitchan = "rpcrecv";
108 	rc->rc_intr = 0;
109 	rc->rc_client = NULL;
110 
111 	cl->cl_ops = &clnt_reconnect_ops;
112 	cl->cl_private = (caddr_t)(void *)rc;
113 	cl->cl_auth = authnone_create();
114 	cl->cl_tp = NULL;
115 	cl->cl_netid = NULL;
116 	return (cl);
117 }
118 
119 static enum clnt_stat
120 clnt_reconnect_connect(CLIENT *cl)
121 {
122 	struct rc_data *rc = (struct rc_data *)cl->cl_private;
123 	struct socket *so;
124 	int one = 1;
125 
126 	so = __rpc_nconf2socket(rc->rc_nconf);
127 	if (!so) {
128 		rpc_createerr.cf_stat = RPC_TLIERROR;
129 		rpc_createerr.cf_error.re_errno = 0;
130 		return (RPC_TLIERROR);
131 	}
132 
133 	if (rc->rc_nconf->nc_semantics == NC_TPI_CLTS)
134 		rc->rc_client = clnt_dg_create(so,
135 		    (struct sockaddr *) &rc->rc_addr, rc->rc_prog, rc->rc_vers,
136 		    rc->rc_sendsz, rc->rc_recvsz);
137 	else
138 		rc->rc_client = clnt_vc_create(so,
139 		    (struct sockaddr *) &rc->rc_addr, rc->rc_prog, rc->rc_vers,
140 		    rc->rc_sendsz, rc->rc_recvsz);
141 
142 	if (!rc->rc_client)
143 		return (rpc_createerr.cf_stat);
144 
145 	CLNT_CONTROL(rc->rc_client, CLSET_FD_CLOSE, 0);
146 	CLNT_CONTROL(rc->rc_client, CLSET_CONNECT, &one);
147 	CLNT_CONTROL(rc->rc_client, CLSET_TIMEOUT, &rc->rc_timeout);
148 	CLNT_CONTROL(rc->rc_client, CLSET_RETRY_TIMEOUT, &rc->rc_retry);
149 	CLNT_CONTROL(rc->rc_client, CLSET_WAITCHAN, &rc->rc_waitchan);
150 	CLNT_CONTROL(rc->rc_client, CLSET_INTERRUPTIBLE, &rc->rc_intr);
151 
152 	return (RPC_SUCCESS);
153 }
154 
155 static enum clnt_stat
156 clnt_reconnect_call(
157 	CLIENT	*cl,			/* client handle */
158 	rpcproc_t	proc,		/* procedure number */
159 	xdrproc_t	xargs,		/* xdr routine for args */
160 	void		*argsp,		/* pointer to args */
161 	xdrproc_t	xresults,	/* xdr routine for results */
162 	void		*resultsp,	/* pointer to results */
163 	struct timeval	utimeout)	/* seconds to wait before giving up */
164 {
165 	struct rc_data *rc = (struct rc_data *)cl->cl_private;
166 	enum clnt_stat stat;
167 
168 	do {
169 		if (!rc->rc_client) {
170 			stat = clnt_reconnect_connect(cl);
171 			if (stat != RPC_SUCCESS)
172 				return (stat);
173 		}
174 
175 		stat = CLNT_CALL(rc->rc_client, proc, xargs, argsp,
176 		    xresults, resultsp, utimeout);
177 
178 		if (stat == RPC_TIMEDOUT) {
179 			/*
180 			 * Check for async send misfeature for NLM
181 			 * protocol.
182 			 */
183 			if ((rc->rc_timeout.tv_sec == 0
184 				&& rc->rc_timeout.tv_usec == 0)
185 			    || (rc->rc_timeout.tv_sec == -1
186 				&& utimeout.tv_sec == 0
187 				&& utimeout.tv_usec == 0))
188 				break;
189 		}
190 
191 		if (stat == RPC_INTR)
192 			break;
193 
194 		if (stat != RPC_SUCCESS) {
195 			CLNT_DESTROY(rc->rc_client);
196 			rc->rc_client = NULL;
197 		}
198 	} while (stat != RPC_SUCCESS);
199 
200 	return (stat);
201 }
202 
203 static void
204 clnt_reconnect_geterr(CLIENT *cl, struct rpc_err *errp)
205 {
206 	struct rc_data *rc = (struct rc_data *)cl->cl_private;
207 
208 	if (rc->rc_client)
209 		CLNT_GETERR(rc->rc_client, errp);
210 	else
211 		memset(errp, 0, sizeof(*errp));
212 }
213 
214 static bool_t
215 clnt_reconnect_freeres(CLIENT *cl, xdrproc_t xdr_res, void *res_ptr)
216 {
217 	struct rc_data *rc = (struct rc_data *)cl->cl_private;
218 
219 	return (CLNT_FREERES(rc->rc_client, xdr_res, res_ptr));
220 }
221 
222 /*ARGSUSED*/
223 static void
224 clnt_reconnect_abort(CLIENT *h)
225 {
226 }
227 
228 static bool_t
229 clnt_reconnect_control(CLIENT *cl, u_int request, void *info)
230 {
231 	struct rc_data *rc = (struct rc_data *)cl->cl_private;
232 
233 	if (info == NULL) {
234 		return (FALSE);
235 	}
236 	switch (request) {
237 	case CLSET_TIMEOUT:
238 		rc->rc_timeout = *(struct timeval *)info;
239 		if (rc->rc_client)
240 			CLNT_CONTROL(rc->rc_client, request, info);
241 		break;
242 
243 	case CLGET_TIMEOUT:
244 		*(struct timeval *)info = rc->rc_timeout;
245 		break;
246 
247 	case CLSET_RETRY_TIMEOUT:
248 		rc->rc_retry = *(struct timeval *)info;
249 		if (rc->rc_client)
250 			CLNT_CONTROL(rc->rc_client, request, info);
251 		break;
252 
253 	case CLGET_RETRY_TIMEOUT:
254 		*(struct timeval *)info = rc->rc_retry;
255 		break;
256 
257 	case CLGET_VERS:
258 		*(uint32_t *)info = rc->rc_vers;
259 		break;
260 
261 	case CLSET_VERS:
262 		rc->rc_vers = *(uint32_t *) info;
263 		if (rc->rc_client)
264 			CLNT_CONTROL(rc->rc_client, CLSET_VERS, info);
265 		break;
266 
267 	case CLGET_PROG:
268 		*(uint32_t *)info = rc->rc_prog;
269 		break;
270 
271 	case CLSET_PROG:
272 		rc->rc_prog = *(uint32_t *) info;
273 		if (rc->rc_client)
274 			CLNT_CONTROL(rc->rc_client, request, info);
275 		break;
276 
277 	case CLSET_WAITCHAN:
278 		rc->rc_waitchan = *(const char **)info;
279 		if (rc->rc_client)
280 			CLNT_CONTROL(rc->rc_client, request, info);
281 		break;
282 
283 	case CLGET_WAITCHAN:
284 		*(const char **) info = rc->rc_waitchan;
285 		break;
286 
287 	case CLSET_INTERRUPTIBLE:
288 		rc->rc_intr = *(int *) info;
289 		if (rc->rc_client)
290 			CLNT_CONTROL(rc->rc_client, request, info);
291 		break;
292 
293 	case CLGET_INTERRUPTIBLE:
294 		*(int *) info = rc->rc_intr;
295 		break;
296 
297 	default:
298 		return (FALSE);
299 	}
300 
301 	return (TRUE);
302 }
303 
304 static void
305 clnt_reconnect_destroy(CLIENT *cl)
306 {
307 	struct rc_data *rc = (struct rc_data *)cl->cl_private;
308 
309 	if (rc->rc_client)
310 		CLNT_DESTROY(rc->rc_client);
311 	mem_free(rc, sizeof(*rc));
312 	mem_free(cl, sizeof (CLIENT));
313 }
314