xref: /freebsd/sys/rpc/clnt_rc.c (revision 39beb93c3f8bdbf72a61fda42300b5ebed7390c8)
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/kernel.h>
34 #include <sys/limits.h>
35 #include <sys/lock.h>
36 #include <sys/malloc.h>
37 #include <sys/mbuf.h>
38 #include <sys/mutex.h>
39 #include <sys/pcpu.h>
40 #include <sys/proc.h>
41 #include <sys/socket.h>
42 #include <sys/socketvar.h>
43 #include <sys/time.h>
44 #include <sys/uio.h>
45 
46 #include <rpc/rpc.h>
47 #include <rpc/rpc_com.h>
48 
49 static enum clnt_stat clnt_reconnect_call(CLIENT *, struct rpc_callextra *,
50     rpcproc_t, struct mbuf *, struct mbuf **, struct timeval);
51 static void clnt_reconnect_geterr(CLIENT *, struct rpc_err *);
52 static bool_t clnt_reconnect_freeres(CLIENT *, xdrproc_t, void *);
53 static void clnt_reconnect_abort(CLIENT *);
54 static bool_t clnt_reconnect_control(CLIENT *, u_int, void *);
55 static void clnt_reconnect_close(CLIENT *);
56 static void clnt_reconnect_destroy(CLIENT *);
57 
58 static struct clnt_ops clnt_reconnect_ops = {
59 	.cl_call =	clnt_reconnect_call,
60 	.cl_abort =	clnt_reconnect_abort,
61 	.cl_geterr =	clnt_reconnect_geterr,
62 	.cl_freeres =	clnt_reconnect_freeres,
63 	.cl_close =	clnt_reconnect_close,
64 	.cl_destroy =	clnt_reconnect_destroy,
65 	.cl_control =	clnt_reconnect_control
66 };
67 
68 static int	fake_wchan;
69 
70 struct rc_data {
71 	struct mtx		rc_lock;
72 	struct sockaddr_storage	rc_addr; /* server address */
73 	struct netconfig*	rc_nconf; /* network type */
74 	rpcprog_t		rc_prog;  /* program number */
75 	rpcvers_t		rc_vers;  /* version number */
76 	size_t			rc_sendsz;
77 	size_t			rc_recvsz;
78 	struct timeval		rc_timeout;
79 	struct timeval		rc_retry;
80 	int			rc_retries;
81 	int			rc_privport;
82 	char			*rc_waitchan;
83 	int			rc_intr;
84 	int			rc_connecting;
85 	int			rc_closed;
86 	struct ucred		*rc_ucred;
87 	CLIENT*			rc_client; /* underlying RPC client */
88 	struct rpc_err		rc_err;
89 };
90 
91 CLIENT *
92 clnt_reconnect_create(
93 	struct netconfig *nconf,	/* network type */
94 	struct sockaddr *svcaddr,	/* servers address */
95 	rpcprog_t program,		/* program number */
96 	rpcvers_t version,		/* version number */
97 	size_t sendsz,			/* buffer recv size */
98 	size_t recvsz)			/* buffer send size */
99 {
100 	CLIENT *cl = NULL;		/* client handle */
101 	struct rc_data *rc = NULL;	/* private data */
102 
103 	if (svcaddr == NULL) {
104 		rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
105 		return (NULL);
106 	}
107 
108 	cl = mem_alloc(sizeof (CLIENT));
109 	rc = mem_alloc(sizeof (*rc));
110 	mtx_init(&rc->rc_lock, "rc->rc_lock", NULL, MTX_DEF);
111 	(void) memcpy(&rc->rc_addr, svcaddr, (size_t)svcaddr->sa_len);
112 	rc->rc_nconf = nconf;
113 	rc->rc_prog = program;
114 	rc->rc_vers = version;
115 	rc->rc_sendsz = sendsz;
116 	rc->rc_recvsz = recvsz;
117 	rc->rc_timeout.tv_sec = -1;
118 	rc->rc_timeout.tv_usec = -1;
119 	rc->rc_retry.tv_sec = 3;
120 	rc->rc_retry.tv_usec = 0;
121 	rc->rc_retries = INT_MAX;
122 	rc->rc_privport = FALSE;
123 	rc->rc_waitchan = "rpcrecv";
124 	rc->rc_intr = 0;
125 	rc->rc_connecting = FALSE;
126 	rc->rc_closed = FALSE;
127 	rc->rc_ucred = crdup(curthread->td_ucred);
128 	rc->rc_client = NULL;
129 
130 	cl->cl_refs = 1;
131 	cl->cl_ops = &clnt_reconnect_ops;
132 	cl->cl_private = (caddr_t)(void *)rc;
133 	cl->cl_auth = authnone_create();
134 	cl->cl_tp = NULL;
135 	cl->cl_netid = NULL;
136 	return (cl);
137 }
138 
139 static enum clnt_stat
140 clnt_reconnect_connect(CLIENT *cl)
141 {
142 	struct thread *td = curthread;
143 	struct rc_data *rc = (struct rc_data *)cl->cl_private;
144 	struct socket *so;
145 	enum clnt_stat stat;
146 	int error;
147 	int one = 1;
148 	struct ucred *oldcred;
149 
150 	mtx_lock(&rc->rc_lock);
151 again:
152 	if (rc->rc_closed) {
153 		mtx_unlock(&rc->rc_lock);
154 		return (RPC_CANTSEND);
155 	}
156 	if (rc->rc_connecting) {
157 		while (!rc->rc_closed && !rc->rc_client) {
158 			error = msleep(rc, &rc->rc_lock,
159 			    rc->rc_intr ? PCATCH : 0, "rpcrecon", 0);
160 			if (error) {
161 				mtx_unlock(&rc->rc_lock);
162 				return (RPC_INTR);
163 			}
164 		}
165 		/*
166 		 * If the other guy failed to connect, we might as
167 		 * well have another go.
168 		 */
169 		if (!rc->rc_client && !rc->rc_connecting)
170 			goto again;
171 		mtx_unlock(&rc->rc_lock);
172 		return (RPC_SUCCESS);
173 	} else {
174 		rc->rc_connecting = TRUE;
175 	}
176 	mtx_unlock(&rc->rc_lock);
177 
178 	so = __rpc_nconf2socket(rc->rc_nconf);
179 	if (!so) {
180 		stat = rpc_createerr.cf_stat = RPC_TLIERROR;
181 		rpc_createerr.cf_error.re_errno = 0;
182 		goto out;
183 	}
184 
185 	oldcred = td->td_ucred;
186 	td->td_ucred = rc->rc_ucred;
187 	if (rc->rc_privport)
188 		bindresvport(so, NULL);
189 
190 	if (rc->rc_nconf->nc_semantics == NC_TPI_CLTS)
191 		rc->rc_client = clnt_dg_create(so,
192 		    (struct sockaddr *) &rc->rc_addr, rc->rc_prog, rc->rc_vers,
193 		    rc->rc_sendsz, rc->rc_recvsz);
194 	else
195 		rc->rc_client = clnt_vc_create(so,
196 		    (struct sockaddr *) &rc->rc_addr, rc->rc_prog, rc->rc_vers,
197 		    rc->rc_sendsz, rc->rc_recvsz);
198 	td->td_ucred = oldcred;
199 
200 	if (!rc->rc_client) {
201 		soclose(so);
202 		rc->rc_err = rpc_createerr.cf_error;
203 		stat = rpc_createerr.cf_stat;
204 		goto out;
205 	}
206 
207 	CLNT_CONTROL(rc->rc_client, CLSET_FD_CLOSE, 0);
208 	CLNT_CONTROL(rc->rc_client, CLSET_CONNECT, &one);
209 	CLNT_CONTROL(rc->rc_client, CLSET_TIMEOUT, &rc->rc_timeout);
210 	CLNT_CONTROL(rc->rc_client, CLSET_RETRY_TIMEOUT, &rc->rc_retry);
211 	CLNT_CONTROL(rc->rc_client, CLSET_WAITCHAN, rc->rc_waitchan);
212 	CLNT_CONTROL(rc->rc_client, CLSET_INTERRUPTIBLE, &rc->rc_intr);
213 	stat = RPC_SUCCESS;
214 
215 out:
216 	mtx_lock(&rc->rc_lock);
217 	if (rc->rc_closed) {
218 		if (rc->rc_client) {
219 			CLNT_CLOSE(rc->rc_client);
220 			CLNT_RELEASE(rc->rc_client);
221 			rc->rc_client = NULL;
222 		}
223 	}
224 	rc->rc_connecting = FALSE;
225 	wakeup(rc);
226 	mtx_unlock(&rc->rc_lock);
227 
228 	return (stat);
229 }
230 
231 static enum clnt_stat
232 clnt_reconnect_call(
233 	CLIENT		*cl,		/* client handle */
234 	struct rpc_callextra *ext,	/* call metadata */
235 	rpcproc_t	proc,		/* procedure number */
236 	struct mbuf	*args,		/* pointer to args */
237 	struct mbuf	**resultsp,	/* pointer to results */
238 	struct timeval	utimeout)
239 {
240 	struct rc_data *rc = (struct rc_data *)cl->cl_private;
241 	CLIENT *client;
242 	enum clnt_stat stat;
243 	int tries;
244 
245 	tries = 0;
246 	do {
247 		if (rc->rc_closed) {
248 			return (RPC_CANTSEND);
249 		}
250 
251 		if (!rc->rc_client) {
252 			stat = clnt_reconnect_connect(cl);
253 			if (stat == RPC_SYSTEMERROR) {
254 				(void) tsleep(&fake_wchan, 0,
255 				    "rpccon", hz);
256 				tries++;
257 				if (tries >= rc->rc_retries)
258 					return (stat);
259 				continue;
260 			}
261 			if (stat != RPC_SUCCESS)
262 				return (stat);
263 		}
264 
265 		mtx_lock(&rc->rc_lock);
266 		if (!rc->rc_client) {
267 			mtx_unlock(&rc->rc_lock);
268 			stat = RPC_FAILED;
269 			continue;
270 		}
271 		CLNT_ACQUIRE(rc->rc_client);
272 		client = rc->rc_client;
273 		mtx_unlock(&rc->rc_lock);
274 		stat = CLNT_CALL_MBUF(client, ext, proc, args,
275 		    resultsp, utimeout);
276 
277 		if (stat != RPC_SUCCESS) {
278 			if (!ext)
279 				CLNT_GETERR(client, &rc->rc_err);
280 		}
281 
282 		CLNT_RELEASE(client);
283 		if (stat == RPC_TIMEDOUT) {
284 			/*
285 			 * Check for async send misfeature for NLM
286 			 * protocol.
287 			 */
288 			if ((rc->rc_timeout.tv_sec == 0
289 				&& rc->rc_timeout.tv_usec == 0)
290 			    || (rc->rc_timeout.tv_sec == -1
291 				&& utimeout.tv_sec == 0
292 				&& utimeout.tv_usec == 0)) {
293 				break;
294 			}
295 		}
296 
297 		if (stat == RPC_TIMEDOUT || stat == RPC_CANTSEND
298 		    || stat == RPC_CANTRECV) {
299 			tries++;
300 			if (tries >= rc->rc_retries)
301 				break;
302 
303 			if (ext && ext->rc_feedback)
304 				ext->rc_feedback(FEEDBACK_RECONNECT, proc,
305 				    ext->rc_feedback_arg);
306 
307 			mtx_lock(&rc->rc_lock);
308 			/*
309 			 * Make sure that someone else hasn't already
310 			 * reconnected.
311 			 */
312 			if (rc->rc_client == client) {
313 				CLNT_RELEASE(rc->rc_client);
314 				rc->rc_client = NULL;
315 			}
316 			mtx_unlock(&rc->rc_lock);
317 		} else {
318 			break;
319 		}
320 	} while (stat != RPC_SUCCESS);
321 
322 	KASSERT(stat != RPC_SUCCESS || *resultsp,
323 	    ("RPC_SUCCESS without reply"));
324 
325 	return (stat);
326 }
327 
328 static void
329 clnt_reconnect_geterr(CLIENT *cl, struct rpc_err *errp)
330 {
331 	struct rc_data *rc = (struct rc_data *)cl->cl_private;
332 
333 	*errp = rc->rc_err;
334 }
335 
336 static bool_t
337 clnt_reconnect_freeres(CLIENT *cl, xdrproc_t xdr_res, void *res_ptr)
338 {
339 	struct rc_data *rc = (struct rc_data *)cl->cl_private;
340 
341 	return (CLNT_FREERES(rc->rc_client, xdr_res, res_ptr));
342 }
343 
344 /*ARGSUSED*/
345 static void
346 clnt_reconnect_abort(CLIENT *h)
347 {
348 }
349 
350 static bool_t
351 clnt_reconnect_control(CLIENT *cl, u_int request, void *info)
352 {
353 	struct rc_data *rc = (struct rc_data *)cl->cl_private;
354 
355 	if (info == NULL) {
356 		return (FALSE);
357 	}
358 	switch (request) {
359 	case CLSET_TIMEOUT:
360 		rc->rc_timeout = *(struct timeval *)info;
361 		if (rc->rc_client)
362 			CLNT_CONTROL(rc->rc_client, request, info);
363 		break;
364 
365 	case CLGET_TIMEOUT:
366 		*(struct timeval *)info = rc->rc_timeout;
367 		break;
368 
369 	case CLSET_RETRY_TIMEOUT:
370 		rc->rc_retry = *(struct timeval *)info;
371 		if (rc->rc_client)
372 			CLNT_CONTROL(rc->rc_client, request, info);
373 		break;
374 
375 	case CLGET_RETRY_TIMEOUT:
376 		*(struct timeval *)info = rc->rc_retry;
377 		break;
378 
379 	case CLGET_VERS:
380 		*(uint32_t *)info = rc->rc_vers;
381 		break;
382 
383 	case CLSET_VERS:
384 		rc->rc_vers = *(uint32_t *) info;
385 		if (rc->rc_client)
386 			CLNT_CONTROL(rc->rc_client, CLSET_VERS, info);
387 		break;
388 
389 	case CLGET_PROG:
390 		*(uint32_t *)info = rc->rc_prog;
391 		break;
392 
393 	case CLSET_PROG:
394 		rc->rc_prog = *(uint32_t *) info;
395 		if (rc->rc_client)
396 			CLNT_CONTROL(rc->rc_client, request, info);
397 		break;
398 
399 	case CLSET_WAITCHAN:
400 		rc->rc_waitchan = (char *)info;
401 		if (rc->rc_client)
402 			CLNT_CONTROL(rc->rc_client, request, info);
403 		break;
404 
405 	case CLGET_WAITCHAN:
406 		*(const char **) info = rc->rc_waitchan;
407 		break;
408 
409 	case CLSET_INTERRUPTIBLE:
410 		rc->rc_intr = *(int *) info;
411 		if (rc->rc_client)
412 			CLNT_CONTROL(rc->rc_client, request, info);
413 		break;
414 
415 	case CLGET_INTERRUPTIBLE:
416 		*(int *) info = rc->rc_intr;
417 		break;
418 
419 	case CLSET_RETRIES:
420 		rc->rc_retries = *(int *) info;
421 		break;
422 
423 	case CLGET_RETRIES:
424 		*(int *) info = rc->rc_retries;
425 		break;
426 
427 	case CLSET_PRIVPORT:
428 		rc->rc_privport = *(int *) info;
429 		break;
430 
431 	case CLGET_PRIVPORT:
432 		*(int *) info = rc->rc_privport;
433 		break;
434 
435 	default:
436 		return (FALSE);
437 	}
438 
439 	return (TRUE);
440 }
441 
442 static void
443 clnt_reconnect_close(CLIENT *cl)
444 {
445 	struct rc_data *rc = (struct rc_data *)cl->cl_private;
446 	CLIENT *client;
447 
448 	mtx_lock(&rc->rc_lock);
449 
450 	if (rc->rc_closed) {
451 		mtx_unlock(&rc->rc_lock);
452 		return;
453 	}
454 
455 	rc->rc_closed = TRUE;
456 	client = rc->rc_client;
457 	rc->rc_client = NULL;
458 
459 	mtx_unlock(&rc->rc_lock);
460 
461 	if (client) {
462 		CLNT_CLOSE(client);
463 		CLNT_RELEASE(client);
464 	}
465 }
466 
467 static void
468 clnt_reconnect_destroy(CLIENT *cl)
469 {
470 	struct rc_data *rc = (struct rc_data *)cl->cl_private;
471 
472 	if (rc->rc_client)
473 		CLNT_DESTROY(rc->rc_client);
474 	crfree(rc->rc_ucred);
475 	mtx_destroy(&rc->rc_lock);
476 	mem_free(rc, sizeof(*rc));
477 	mem_free(cl, sizeof (CLIENT));
478 }
479