clnt_rc.c revision 194934
153642Sguido/*-
280482Sdarrenr * Copyright (c) 2008 Isilon Inc http://www.isilon.com/
353642Sguido * Authors: Doug Rabson <dfr@rabson.org>
480482Sdarrenr * Developed with Red Inc: Alfred Perlstein <alfred@freebsd.org>
553642Sguido *
653642Sguido * Redistribution and use in source and binary forms, with or without
763523Sdarrenr * modification, are permitted provided that the following conditions
857126Sguido * are met:
953642Sguido * 1. Redistributions of source code must retain the above copyright
1053642Sguido *    notice, this list of conditions and the following disclaimer.
1153642Sguido * 2. Redistributions in binary form must reproduce the above copyright
1253642Sguido *    notice, this list of conditions and the following disclaimer in the
1360854Sdarrenr *    documentation and/or other materials provided with the distribution.
1460854Sdarrenr *
1560854Sdarrenr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1660854Sdarrenr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1760854Sdarrenr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1860854Sdarrenr * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1980482Sdarrenr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2080482Sdarrenr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2180482Sdarrenr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2280482Sdarrenr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2380482Sdarrenr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2480482Sdarrenr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2553642Sguido * SUCH DAMAGE.
2653642Sguido */
2753642Sguido
2853642Sguido#include <sys/cdefs.h>
2953642Sguido__FBSDID("$FreeBSD: head/sys/rpc/clnt_rc.c 194934 2009-06-25 00:28:43Z rmacklem $");
3053642Sguido
3153642Sguido#include <sys/param.h>
3253642Sguido#include <sys/systm.h>
3353642Sguido#include <sys/kernel.h>
3453642Sguido#include <sys/limits.h>
3553642Sguido#include <sys/lock.h>
3653642Sguido#include <sys/malloc.h>
3753642Sguido#include <sys/mbuf.h>
3853642Sguido#include <sys/mutex.h>
3953642Sguido#include <sys/pcpu.h>
4053642Sguido#include <sys/proc.h>
4153642Sguido#include <sys/socket.h>
4253642Sguido#include <sys/socketvar.h>
4353642Sguido#include <sys/time.h>
4453642Sguido#include <sys/uio.h>
4553642Sguido
4653642Sguido#include <rpc/rpc.h>
4753642Sguido#include <rpc/rpc_com.h>
4853642Sguido
4953642Sguidostatic enum clnt_stat clnt_reconnect_call(CLIENT *, struct rpc_callextra *,
5053642Sguido    rpcproc_t, struct mbuf *, struct mbuf **, struct timeval);
5153642Sguidostatic void clnt_reconnect_geterr(CLIENT *, struct rpc_err *);
5253642Sguidostatic bool_t clnt_reconnect_freeres(CLIENT *, xdrproc_t, void *);
5353642Sguidostatic void clnt_reconnect_abort(CLIENT *);
5453642Sguidostatic bool_t clnt_reconnect_control(CLIENT *, u_int, void *);
5553642Sguidostatic void clnt_reconnect_close(CLIENT *);
5653642Sguidostatic void clnt_reconnect_destroy(CLIENT *);
5753642Sguido
5860854Sdarrenrstatic struct clnt_ops clnt_reconnect_ops = {
5960854Sdarrenr	.cl_call =	clnt_reconnect_call,
6060854Sdarrenr	.cl_abort =	clnt_reconnect_abort,
6153642Sguido	.cl_geterr =	clnt_reconnect_geterr,
6253642Sguido	.cl_freeres =	clnt_reconnect_freeres,
6353642Sguido	.cl_close =	clnt_reconnect_close,
6453642Sguido	.cl_destroy =	clnt_reconnect_destroy,
6560854Sdarrenr	.cl_control =	clnt_reconnect_control
6653642Sguido};
6760854Sdarrenr
6860854Sdarrenrstatic int	fake_wchan;
6953642Sguido
7060854Sdarrenrstruct rc_data {
7160854Sdarrenr	struct mtx		rc_lock;
7253642Sguido	struct sockaddr_storage	rc_addr; /* server address */
7353642Sguido	struct netconfig*	rc_nconf; /* network type */
7453642Sguido	rpcprog_t		rc_prog;  /* program number */
7553642Sguido	rpcvers_t		rc_vers;  /* version number */
7653642Sguido	size_t			rc_sendsz;
7753642Sguido	size_t			rc_recvsz;
7853642Sguido	struct timeval		rc_timeout;
7953642Sguido	struct timeval		rc_retry;
8053642Sguido	int			rc_retries;
8153642Sguido	int			rc_privport;
8253642Sguido	char			*rc_waitchan;
8353642Sguido	int			rc_intr;
8460854Sdarrenr	int			rc_connecting;
8560854Sdarrenr	int			rc_closed;
8660854Sdarrenr	struct ucred		*rc_ucred;
8760854Sdarrenr	CLIENT*			rc_client; /* underlying RPC client */
8853642Sguido	struct rpc_err		rc_err;
8953642Sguido};
9060854Sdarrenr
9160854SdarrenrCLIENT *
9260854Sdarrenrclnt_reconnect_create(
9360854Sdarrenr	struct netconfig *nconf,	/* network type */
9460854Sdarrenr	struct sockaddr *svcaddr,	/* servers address */
9560854Sdarrenr	rpcprog_t program,		/* program number */
9660854Sdarrenr	rpcvers_t version,		/* version number */
9760854Sdarrenr	size_t sendsz,			/* buffer recv size */
9860854Sdarrenr	size_t recvsz)			/* buffer send size */
9953642Sguido{
10053642Sguido	CLIENT *cl = NULL;		/* client handle */
10153642Sguido	struct rc_data *rc = NULL;	/* private data */
10253642Sguido
10353642Sguido	if (svcaddr == NULL) {
10453642Sguido		rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
10553642Sguido		return (NULL);
10660854Sdarrenr	}
10760854Sdarrenr
10853642Sguido	cl = mem_alloc(sizeof (CLIENT));
10953642Sguido	rc = mem_alloc(sizeof (*rc));
11053642Sguido	mtx_init(&rc->rc_lock, "rc->rc_lock", NULL, MTX_DEF);
11153642Sguido	(void) memcpy(&rc->rc_addr, svcaddr, (size_t)svcaddr->sa_len);
11253642Sguido	rc->rc_nconf = nconf;
11353642Sguido	rc->rc_prog = program;
11453642Sguido	rc->rc_vers = version;
11553642Sguido	rc->rc_sendsz = sendsz;
11653642Sguido	rc->rc_recvsz = recvsz;
11753642Sguido	rc->rc_timeout.tv_sec = -1;
11853642Sguido	rc->rc_timeout.tv_usec = -1;
11960854Sdarrenr	rc->rc_retry.tv_sec = 3;
12060854Sdarrenr	rc->rc_retry.tv_usec = 0;
12160854Sdarrenr	rc->rc_retries = INT_MAX;
12260854Sdarrenr	rc->rc_privport = FALSE;
12360854Sdarrenr	rc->rc_waitchan = "rpcrecv";
12453642Sguido	rc->rc_intr = 0;
12560854Sdarrenr	rc->rc_connecting = FALSE;
12660854Sdarrenr	rc->rc_closed = FALSE;
12760854Sdarrenr	rc->rc_ucred = crdup(curthread->td_ucred);
12853642Sguido	rc->rc_client = NULL;
12953642Sguido
13053642Sguido	cl->cl_refs = 1;
13160854Sdarrenr	cl->cl_ops = &clnt_reconnect_ops;
13260854Sdarrenr	cl->cl_private = (caddr_t)(void *)rc;
13353642Sguido	cl->cl_auth = authnone_create();
13453642Sguido	cl->cl_tp = NULL;
13553642Sguido	cl->cl_netid = NULL;
13653642Sguido	return (cl);
13753642Sguido}
13853642Sguido
13960854Sdarrenrstatic enum clnt_stat
14060854Sdarrenrclnt_reconnect_connect(CLIENT *cl)
14160854Sdarrenr{
14260854Sdarrenr	struct thread *td = curthread;
14353642Sguido	struct rc_data *rc = (struct rc_data *)cl->cl_private;
14453642Sguido	struct socket *so;
14553642Sguido	enum clnt_stat stat;
14653642Sguido	int error;
14753642Sguido	int one = 1;
14853642Sguido	struct ucred *oldcred;
14953642Sguido	CLIENT *newclient = NULL;
15053642Sguido
15153642Sguido	mtx_lock(&rc->rc_lock);
15260854Sdarrenr	while (rc->rc_connecting) {
15353642Sguido		error = msleep(rc, &rc->rc_lock,
15453642Sguido		    rc->rc_intr ? PCATCH : 0, "rpcrecon", 0);
15553642Sguido		if (error) {
15653642Sguido			mtx_unlock(&rc->rc_lock);
15753642Sguido			return (RPC_INTR);
15853642Sguido		}
15953642Sguido	}
16053642Sguido	if (rc->rc_closed) {
16153642Sguido		mtx_unlock(&rc->rc_lock);
16253642Sguido		return (RPC_CANTSEND);
16353642Sguido	}
16453642Sguido	if (rc->rc_client) {
16553642Sguido		mtx_unlock(&rc->rc_lock);
16653642Sguido		return (RPC_SUCCESS);
16753642Sguido	}
16853642Sguido
16953642Sguido	/*
17060854Sdarrenr	 * My turn to attempt a connect. The rc_connecting variable
17153642Sguido	 * serializes the following code sequence, so it is guaranteed
17253642Sguido	 * that rc_client will still be NULL after it is re-locked below,
17353642Sguido	 * since that is the only place it is set non-NULL.
17453642Sguido	 */
17553642Sguido	rc->rc_connecting = TRUE;
17653642Sguido	mtx_unlock(&rc->rc_lock);
17753642Sguido
17853642Sguido	so = __rpc_nconf2socket(rc->rc_nconf);
17967614Sdarrenr	if (!so) {
18053642Sguido		stat = rpc_createerr.cf_stat = RPC_TLIERROR;
18153642Sguido		rpc_createerr.cf_error.re_errno = 0;
18260854Sdarrenr		goto out;
18353642Sguido	}
18453642Sguido
18553642Sguido	oldcred = td->td_ucred;
18653642Sguido	td->td_ucred = rc->rc_ucred;
18757096Sguido	if (rc->rc_privport)
18853642Sguido		bindresvport(so, NULL);
18960854Sdarrenr
19053642Sguido	if (rc->rc_nconf->nc_semantics == NC_TPI_CLTS)
19153642Sguido		newclient = clnt_dg_create(so,
19253642Sguido		    (struct sockaddr *) &rc->rc_addr, rc->rc_prog, rc->rc_vers,
19353642Sguido		    rc->rc_sendsz, rc->rc_recvsz);
19453642Sguido	else
19553642Sguido		newclient = clnt_vc_create(so,
19653642Sguido		    (struct sockaddr *) &rc->rc_addr, rc->rc_prog, rc->rc_vers,
19753642Sguido		    rc->rc_sendsz, rc->rc_recvsz);
19853642Sguido	td->td_ucred = oldcred;
199
200	if (!newclient) {
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(newclient, CLSET_FD_CLOSE, 0);
208	CLNT_CONTROL(newclient, CLSET_CONNECT, &one);
209	CLNT_CONTROL(newclient, CLSET_TIMEOUT, &rc->rc_timeout);
210	CLNT_CONTROL(newclient, CLSET_RETRY_TIMEOUT, &rc->rc_retry);
211	CLNT_CONTROL(newclient, CLSET_WAITCHAN, rc->rc_waitchan);
212	CLNT_CONTROL(newclient, CLSET_INTERRUPTIBLE, &rc->rc_intr);
213	stat = RPC_SUCCESS;
214
215out:
216	mtx_lock(&rc->rc_lock);
217	KASSERT(rc->rc_client == NULL, ("rc_client not null"));
218	if (!rc->rc_closed) {
219		rc->rc_client = newclient;
220		newclient = NULL;
221	}
222	rc->rc_connecting = FALSE;
223	wakeup(rc);
224	mtx_unlock(&rc->rc_lock);
225
226	if (newclient) {
227		/*
228		 * It has been closed, so discard the new client.
229		 * nb: clnt_[dg|vc]_close()/clnt_[dg|vc]_destroy() cannot
230		 * be called with the rc_lock mutex held, since they may
231		 * msleep() while holding a different mutex.
232		 */
233		CLNT_CLOSE(newclient);
234		CLNT_RELEASE(newclient);
235	}
236
237	return (stat);
238}
239
240static enum clnt_stat
241clnt_reconnect_call(
242	CLIENT		*cl,		/* client handle */
243	struct rpc_callextra *ext,	/* call metadata */
244	rpcproc_t	proc,		/* procedure number */
245	struct mbuf	*args,		/* pointer to args */
246	struct mbuf	**resultsp,	/* pointer to results */
247	struct timeval	utimeout)
248{
249	struct rc_data *rc = (struct rc_data *)cl->cl_private;
250	CLIENT *client;
251	enum clnt_stat stat;
252	int tries, error;
253
254	tries = 0;
255	do {
256		mtx_lock(&rc->rc_lock);
257		if (rc->rc_closed) {
258			mtx_unlock(&rc->rc_lock);
259			return (RPC_CANTSEND);
260		}
261
262		if (!rc->rc_client) {
263			mtx_unlock(&rc->rc_lock);
264			stat = clnt_reconnect_connect(cl);
265			if (stat == RPC_SYSTEMERROR) {
266				error = tsleep(&fake_wchan,
267				    rc->rc_intr ? PCATCH : 0, "rpccon", hz);
268				if (error == EINTR || error == ERESTART)
269					return (RPC_INTR);
270				tries++;
271				if (tries >= rc->rc_retries)
272					return (stat);
273				continue;
274			}
275			if (stat != RPC_SUCCESS)
276				return (stat);
277			mtx_lock(&rc->rc_lock);
278		}
279
280		if (!rc->rc_client) {
281			mtx_unlock(&rc->rc_lock);
282			stat = RPC_FAILED;
283			continue;
284		}
285		CLNT_ACQUIRE(rc->rc_client);
286		client = rc->rc_client;
287		mtx_unlock(&rc->rc_lock);
288		stat = CLNT_CALL_MBUF(client, ext, proc, args,
289		    resultsp, utimeout);
290
291		if (stat != RPC_SUCCESS) {
292			if (!ext)
293				CLNT_GETERR(client, &rc->rc_err);
294		}
295
296		if (stat == RPC_TIMEDOUT) {
297			/*
298			 * Check for async send misfeature for NLM
299			 * protocol.
300			 */
301			if ((rc->rc_timeout.tv_sec == 0
302				&& rc->rc_timeout.tv_usec == 0)
303			    || (rc->rc_timeout.tv_sec == -1
304				&& utimeout.tv_sec == 0
305				&& utimeout.tv_usec == 0)) {
306				CLNT_RELEASE(client);
307				break;
308			}
309		}
310
311		if (stat == RPC_TIMEDOUT || stat == RPC_CANTSEND
312		    || stat == RPC_CANTRECV) {
313			tries++;
314			if (tries >= rc->rc_retries) {
315				CLNT_RELEASE(client);
316				break;
317			}
318
319			if (ext && ext->rc_feedback)
320				ext->rc_feedback(FEEDBACK_RECONNECT, proc,
321				    ext->rc_feedback_arg);
322
323			mtx_lock(&rc->rc_lock);
324			/*
325			 * Make sure that someone else hasn't already
326			 * reconnected by checking if rc_client has changed.
327			 * If not, we are done with the client and must
328			 * do CLNT_RELEASE(client) twice to dispose of it,
329			 * because there is both an initial refcnt and one
330			 * acquired by CLNT_ACQUIRE() above.
331			 */
332			if (rc->rc_client == client) {
333				rc->rc_client = NULL;
334				mtx_unlock(&rc->rc_lock);
335				CLNT_RELEASE(client);
336			} else {
337				mtx_unlock(&rc->rc_lock);
338			}
339			CLNT_RELEASE(client);
340		} else {
341			CLNT_RELEASE(client);
342			break;
343		}
344	} while (stat != RPC_SUCCESS);
345
346	KASSERT(stat != RPC_SUCCESS || *resultsp,
347	    ("RPC_SUCCESS without reply"));
348
349	return (stat);
350}
351
352static void
353clnt_reconnect_geterr(CLIENT *cl, struct rpc_err *errp)
354{
355	struct rc_data *rc = (struct rc_data *)cl->cl_private;
356
357	*errp = rc->rc_err;
358}
359
360/*
361 * Since this function requires that rc_client be valid, it can
362 * only be called when that is guaranteed to be the case.
363 */
364static bool_t
365clnt_reconnect_freeres(CLIENT *cl, xdrproc_t xdr_res, void *res_ptr)
366{
367	struct rc_data *rc = (struct rc_data *)cl->cl_private;
368
369	return (CLNT_FREERES(rc->rc_client, xdr_res, res_ptr));
370}
371
372/*ARGSUSED*/
373static void
374clnt_reconnect_abort(CLIENT *h)
375{
376}
377
378/*
379 * CLNT_CONTROL() on the client returned by clnt_reconnect_create() must
380 * always be called before CLNT_CALL_MBUF() by a single thread only.
381 */
382static bool_t
383clnt_reconnect_control(CLIENT *cl, u_int request, void *info)
384{
385	struct rc_data *rc = (struct rc_data *)cl->cl_private;
386
387	if (info == NULL) {
388		return (FALSE);
389	}
390	switch (request) {
391	case CLSET_TIMEOUT:
392		rc->rc_timeout = *(struct timeval *)info;
393		if (rc->rc_client)
394			CLNT_CONTROL(rc->rc_client, request, info);
395		break;
396
397	case CLGET_TIMEOUT:
398		*(struct timeval *)info = rc->rc_timeout;
399		break;
400
401	case CLSET_RETRY_TIMEOUT:
402		rc->rc_retry = *(struct timeval *)info;
403		if (rc->rc_client)
404			CLNT_CONTROL(rc->rc_client, request, info);
405		break;
406
407	case CLGET_RETRY_TIMEOUT:
408		*(struct timeval *)info = rc->rc_retry;
409		break;
410
411	case CLGET_VERS:
412		*(uint32_t *)info = rc->rc_vers;
413		break;
414
415	case CLSET_VERS:
416		rc->rc_vers = *(uint32_t *) info;
417		if (rc->rc_client)
418			CLNT_CONTROL(rc->rc_client, CLSET_VERS, info);
419		break;
420
421	case CLGET_PROG:
422		*(uint32_t *)info = rc->rc_prog;
423		break;
424
425	case CLSET_PROG:
426		rc->rc_prog = *(uint32_t *) info;
427		if (rc->rc_client)
428			CLNT_CONTROL(rc->rc_client, request, info);
429		break;
430
431	case CLSET_WAITCHAN:
432		rc->rc_waitchan = (char *)info;
433		if (rc->rc_client)
434			CLNT_CONTROL(rc->rc_client, request, info);
435		break;
436
437	case CLGET_WAITCHAN:
438		*(const char **) info = rc->rc_waitchan;
439		break;
440
441	case CLSET_INTERRUPTIBLE:
442		rc->rc_intr = *(int *) info;
443		if (rc->rc_client)
444			CLNT_CONTROL(rc->rc_client, request, info);
445		break;
446
447	case CLGET_INTERRUPTIBLE:
448		*(int *) info = rc->rc_intr;
449		break;
450
451	case CLSET_RETRIES:
452		rc->rc_retries = *(int *) info;
453		break;
454
455	case CLGET_RETRIES:
456		*(int *) info = rc->rc_retries;
457		break;
458
459	case CLSET_PRIVPORT:
460		rc->rc_privport = *(int *) info;
461		break;
462
463	case CLGET_PRIVPORT:
464		*(int *) info = rc->rc_privport;
465		break;
466
467	default:
468		return (FALSE);
469	}
470
471	return (TRUE);
472}
473
474static void
475clnt_reconnect_close(CLIENT *cl)
476{
477	struct rc_data *rc = (struct rc_data *)cl->cl_private;
478	CLIENT *client;
479
480	mtx_lock(&rc->rc_lock);
481
482	if (rc->rc_closed) {
483		mtx_unlock(&rc->rc_lock);
484		return;
485	}
486
487	rc->rc_closed = TRUE;
488	client = rc->rc_client;
489	rc->rc_client = NULL;
490
491	mtx_unlock(&rc->rc_lock);
492
493	if (client) {
494		CLNT_CLOSE(client);
495		CLNT_RELEASE(client);
496	}
497}
498
499static void
500clnt_reconnect_destroy(CLIENT *cl)
501{
502	struct rc_data *rc = (struct rc_data *)cl->cl_private;
503
504	if (rc->rc_client)
505		CLNT_DESTROY(rc->rc_client);
506	crfree(rc->rc_ucred);
507	mtx_destroy(&rc->rc_lock);
508	mem_free(rc, sizeof(*rc));
509	mem_free(cl, sizeof (CLIENT));
510}
511