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#include <rpc/krpc.h>
49
50static enum clnt_stat clnt_reconnect_call(CLIENT *, struct rpc_callextra *,
51    rpcproc_t, struct mbuf *, struct mbuf **, struct timeval);
52static void clnt_reconnect_geterr(CLIENT *, struct rpc_err *);
53static bool_t clnt_reconnect_freeres(CLIENT *, xdrproc_t, void *);
54static void clnt_reconnect_abort(CLIENT *);
55static bool_t clnt_reconnect_control(CLIENT *, u_int, void *);
56static void clnt_reconnect_close(CLIENT *);
57static void clnt_reconnect_destroy(CLIENT *);
58
59static struct clnt_ops clnt_reconnect_ops = {
60	.cl_call =	clnt_reconnect_call,
61	.cl_abort =	clnt_reconnect_abort,
62	.cl_geterr =	clnt_reconnect_geterr,
63	.cl_freeres =	clnt_reconnect_freeres,
64	.cl_close =	clnt_reconnect_close,
65	.cl_destroy =	clnt_reconnect_destroy,
66	.cl_control =	clnt_reconnect_control
67};
68
69static int	fake_wchan;
70
71CLIENT *
72clnt_reconnect_create(
73	struct netconfig *nconf,	/* network type */
74	struct sockaddr *svcaddr,	/* servers address */
75	rpcprog_t program,		/* program number */
76	rpcvers_t version,		/* version number */
77	size_t sendsz,			/* buffer recv size */
78	size_t recvsz)			/* buffer send size */
79{
80	CLIENT *cl = NULL;		/* client handle */
81	struct rc_data *rc = NULL;	/* private data */
82
83	if (svcaddr == NULL) {
84		rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
85		return (NULL);
86	}
87
88	cl = mem_alloc(sizeof (CLIENT));
89	rc = mem_alloc(sizeof (*rc));
90	mtx_init(&rc->rc_lock, "rc->rc_lock", NULL, MTX_DEF);
91	(void) memcpy(&rc->rc_addr, svcaddr, (size_t)svcaddr->sa_len);
92	rc->rc_nconf = nconf;
93	rc->rc_prog = program;
94	rc->rc_vers = version;
95	rc->rc_sendsz = sendsz;
96	rc->rc_recvsz = recvsz;
97	rc->rc_timeout.tv_sec = -1;
98	rc->rc_timeout.tv_usec = -1;
99	rc->rc_retry.tv_sec = 3;
100	rc->rc_retry.tv_usec = 0;
101	rc->rc_retries = INT_MAX;
102	rc->rc_privport = FALSE;
103	rc->rc_waitchan = "rpcrecv";
104	rc->rc_intr = 0;
105	rc->rc_connecting = FALSE;
106	rc->rc_closed = FALSE;
107	rc->rc_ucred = crdup(curthread->td_ucred);
108	rc->rc_client = NULL;
109
110	cl->cl_refs = 1;
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
119static enum clnt_stat
120clnt_reconnect_connect(CLIENT *cl)
121{
122	struct thread *td = curthread;
123	struct rc_data *rc = (struct rc_data *)cl->cl_private;
124	struct socket *so;
125	enum clnt_stat stat;
126	int error;
127	int one = 1;
128	struct ucred *oldcred;
129	CLIENT *newclient = NULL;
130
131	mtx_lock(&rc->rc_lock);
132	while (rc->rc_connecting) {
133		error = msleep(rc, &rc->rc_lock,
134		    rc->rc_intr ? PCATCH : 0, "rpcrecon", 0);
135		if (error) {
136			mtx_unlock(&rc->rc_lock);
137			return (RPC_INTR);
138		}
139	}
140	if (rc->rc_closed) {
141		mtx_unlock(&rc->rc_lock);
142		return (RPC_CANTSEND);
143	}
144	if (rc->rc_client) {
145		mtx_unlock(&rc->rc_lock);
146		return (RPC_SUCCESS);
147	}
148
149	/*
150	 * My turn to attempt a connect. The rc_connecting variable
151	 * serializes the following code sequence, so it is guaranteed
152	 * that rc_client will still be NULL after it is re-locked below,
153	 * since that is the only place it is set non-NULL.
154	 */
155	rc->rc_connecting = TRUE;
156	mtx_unlock(&rc->rc_lock);
157
158	oldcred = td->td_ucred;
159	td->td_ucred = rc->rc_ucred;
160	so = __rpc_nconf2socket(rc->rc_nconf);
161	if (!so) {
162		stat = rpc_createerr.cf_stat = RPC_TLIERROR;
163		rpc_createerr.cf_error.re_errno = 0;
164		td->td_ucred = oldcred;
165		goto out;
166	}
167
168	if (rc->rc_privport)
169		bindresvport(so, NULL);
170
171	if (rc->rc_nconf->nc_semantics == NC_TPI_CLTS)
172		newclient = clnt_dg_create(so,
173		    (struct sockaddr *) &rc->rc_addr, rc->rc_prog, rc->rc_vers,
174		    rc->rc_sendsz, rc->rc_recvsz);
175	else
176		newclient = clnt_vc_create(so,
177		    (struct sockaddr *) &rc->rc_addr, rc->rc_prog, rc->rc_vers,
178		    rc->rc_sendsz, rc->rc_recvsz, rc->rc_intr);
179	td->td_ucred = oldcred;
180
181	if (!newclient) {
182		soclose(so);
183		rc->rc_err = rpc_createerr.cf_error;
184		stat = rpc_createerr.cf_stat;
185		goto out;
186	}
187
188	CLNT_CONTROL(newclient, CLSET_FD_CLOSE, 0);
189	CLNT_CONTROL(newclient, CLSET_CONNECT, &one);
190	CLNT_CONTROL(newclient, CLSET_TIMEOUT, &rc->rc_timeout);
191	CLNT_CONTROL(newclient, CLSET_RETRY_TIMEOUT, &rc->rc_retry);
192	CLNT_CONTROL(newclient, CLSET_WAITCHAN, rc->rc_waitchan);
193	CLNT_CONTROL(newclient, CLSET_INTERRUPTIBLE, &rc->rc_intr);
194	if (rc->rc_backchannel != NULL)
195		CLNT_CONTROL(newclient, CLSET_BACKCHANNEL, rc->rc_backchannel);
196	stat = RPC_SUCCESS;
197
198out:
199	mtx_lock(&rc->rc_lock);
200	KASSERT(rc->rc_client == NULL, ("rc_client not null"));
201	if (!rc->rc_closed) {
202		rc->rc_client = newclient;
203		newclient = NULL;
204	}
205	rc->rc_connecting = FALSE;
206	wakeup(rc);
207	mtx_unlock(&rc->rc_lock);
208
209	if (newclient) {
210		/*
211		 * It has been closed, so discard the new client.
212		 * nb: clnt_[dg|vc]_close()/clnt_[dg|vc]_destroy() cannot
213		 * be called with the rc_lock mutex held, since they may
214		 * msleep() while holding a different mutex.
215		 */
216		CLNT_CLOSE(newclient);
217		CLNT_RELEASE(newclient);
218	}
219
220	return (stat);
221}
222
223static enum clnt_stat
224clnt_reconnect_call(
225	CLIENT		*cl,		/* client handle */
226	struct rpc_callextra *ext,	/* call metadata */
227	rpcproc_t	proc,		/* procedure number */
228	struct mbuf	*args,		/* pointer to args */
229	struct mbuf	**resultsp,	/* pointer to results */
230	struct timeval	utimeout)
231{
232	struct rc_data *rc = (struct rc_data *)cl->cl_private;
233	CLIENT *client;
234	enum clnt_stat stat;
235	int tries, error;
236
237	tries = 0;
238	do {
239		mtx_lock(&rc->rc_lock);
240		if (rc->rc_closed) {
241			mtx_unlock(&rc->rc_lock);
242			return (RPC_CANTSEND);
243		}
244
245		if (!rc->rc_client) {
246			mtx_unlock(&rc->rc_lock);
247			stat = clnt_reconnect_connect(cl);
248			if (stat == RPC_SYSTEMERROR) {
249				error = tsleep(&fake_wchan,
250				    rc->rc_intr ? PCATCH | PBDRY : 0, "rpccon",
251				    hz);
252				if (error == EINTR || error == ERESTART)
253					return (RPC_INTR);
254				tries++;
255				if (tries >= rc->rc_retries)
256					return (stat);
257				continue;
258			}
259			if (stat != RPC_SUCCESS)
260				return (stat);
261			mtx_lock(&rc->rc_lock);
262		}
263
264		if (!rc->rc_client) {
265			mtx_unlock(&rc->rc_lock);
266			stat = RPC_FAILED;
267			continue;
268		}
269		CLNT_ACQUIRE(rc->rc_client);
270		client = rc->rc_client;
271		mtx_unlock(&rc->rc_lock);
272		stat = CLNT_CALL_MBUF(client, ext, proc, args,
273		    resultsp, utimeout);
274
275		if (stat != RPC_SUCCESS) {
276			if (!ext)
277				CLNT_GETERR(client, &rc->rc_err);
278		}
279
280		if (stat == RPC_TIMEDOUT) {
281			/*
282			 * Check for async send misfeature for NLM
283			 * protocol.
284			 */
285			if ((rc->rc_timeout.tv_sec == 0
286				&& rc->rc_timeout.tv_usec == 0)
287			    || (rc->rc_timeout.tv_sec == -1
288				&& utimeout.tv_sec == 0
289				&& utimeout.tv_usec == 0)) {
290				CLNT_RELEASE(client);
291				break;
292			}
293		}
294
295		if (stat == RPC_TIMEDOUT || stat == RPC_CANTSEND
296		    || stat == RPC_CANTRECV) {
297			tries++;
298			if (tries >= rc->rc_retries) {
299				CLNT_RELEASE(client);
300				break;
301			}
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 by checking if rc_client has changed.
311			 * If not, we are done with the client and must
312			 * do CLNT_RELEASE(client) twice to dispose of it,
313			 * because there is both an initial refcnt and one
314			 * acquired by CLNT_ACQUIRE() above.
315			 */
316			if (rc->rc_client == client) {
317				rc->rc_client = NULL;
318				mtx_unlock(&rc->rc_lock);
319				CLNT_RELEASE(client);
320			} else {
321				mtx_unlock(&rc->rc_lock);
322			}
323			CLNT_RELEASE(client);
324		} else {
325			CLNT_RELEASE(client);
326			break;
327		}
328	} while (stat != RPC_SUCCESS);
329
330	KASSERT(stat != RPC_SUCCESS || *resultsp,
331	    ("RPC_SUCCESS without reply"));
332
333	return (stat);
334}
335
336static void
337clnt_reconnect_geterr(CLIENT *cl, struct rpc_err *errp)
338{
339	struct rc_data *rc = (struct rc_data *)cl->cl_private;
340
341	*errp = rc->rc_err;
342}
343
344/*
345 * Since this function requires that rc_client be valid, it can
346 * only be called when that is guaranteed to be the case.
347 */
348static bool_t
349clnt_reconnect_freeres(CLIENT *cl, xdrproc_t xdr_res, void *res_ptr)
350{
351	struct rc_data *rc = (struct rc_data *)cl->cl_private;
352
353	return (CLNT_FREERES(rc->rc_client, xdr_res, res_ptr));
354}
355
356/*ARGSUSED*/
357static void
358clnt_reconnect_abort(CLIENT *h)
359{
360}
361
362/*
363 * CLNT_CONTROL() on the client returned by clnt_reconnect_create() must
364 * always be called before CLNT_CALL_MBUF() by a single thread only.
365 */
366static bool_t
367clnt_reconnect_control(CLIENT *cl, u_int request, void *info)
368{
369	struct rc_data *rc = (struct rc_data *)cl->cl_private;
370	SVCXPRT *xprt;
371
372	if (info == NULL) {
373		return (FALSE);
374	}
375	switch (request) {
376	case CLSET_TIMEOUT:
377		rc->rc_timeout = *(struct timeval *)info;
378		if (rc->rc_client)
379			CLNT_CONTROL(rc->rc_client, request, info);
380		break;
381
382	case CLGET_TIMEOUT:
383		*(struct timeval *)info = rc->rc_timeout;
384		break;
385
386	case CLSET_RETRY_TIMEOUT:
387		rc->rc_retry = *(struct timeval *)info;
388		if (rc->rc_client)
389			CLNT_CONTROL(rc->rc_client, request, info);
390		break;
391
392	case CLGET_RETRY_TIMEOUT:
393		*(struct timeval *)info = rc->rc_retry;
394		break;
395
396	case CLGET_VERS:
397		*(uint32_t *)info = rc->rc_vers;
398		break;
399
400	case CLSET_VERS:
401		rc->rc_vers = *(uint32_t *) info;
402		if (rc->rc_client)
403			CLNT_CONTROL(rc->rc_client, CLSET_VERS, info);
404		break;
405
406	case CLGET_PROG:
407		*(uint32_t *)info = rc->rc_prog;
408		break;
409
410	case CLSET_PROG:
411		rc->rc_prog = *(uint32_t *) info;
412		if (rc->rc_client)
413			CLNT_CONTROL(rc->rc_client, request, info);
414		break;
415
416	case CLSET_WAITCHAN:
417		rc->rc_waitchan = (char *)info;
418		if (rc->rc_client)
419			CLNT_CONTROL(rc->rc_client, request, info);
420		break;
421
422	case CLGET_WAITCHAN:
423		*(const char **) info = rc->rc_waitchan;
424		break;
425
426	case CLSET_INTERRUPTIBLE:
427		rc->rc_intr = *(int *) info;
428		if (rc->rc_client)
429			CLNT_CONTROL(rc->rc_client, request, info);
430		break;
431
432	case CLGET_INTERRUPTIBLE:
433		*(int *) info = rc->rc_intr;
434		break;
435
436	case CLSET_RETRIES:
437		rc->rc_retries = *(int *) info;
438		break;
439
440	case CLGET_RETRIES:
441		*(int *) info = rc->rc_retries;
442		break;
443
444	case CLSET_PRIVPORT:
445		rc->rc_privport = *(int *) info;
446		break;
447
448	case CLGET_PRIVPORT:
449		*(int *) info = rc->rc_privport;
450		break;
451
452	case CLSET_BACKCHANNEL:
453		xprt = (SVCXPRT *)info;
454		SVC_ACQUIRE(xprt);
455		xprt_register(xprt);
456		rc->rc_backchannel = info;
457		break;
458
459	default:
460		return (FALSE);
461	}
462
463	return (TRUE);
464}
465
466static void
467clnt_reconnect_close(CLIENT *cl)
468{
469	struct rc_data *rc = (struct rc_data *)cl->cl_private;
470	CLIENT *client;
471
472	mtx_lock(&rc->rc_lock);
473
474	if (rc->rc_closed) {
475		mtx_unlock(&rc->rc_lock);
476		return;
477	}
478
479	rc->rc_closed = TRUE;
480	client = rc->rc_client;
481	rc->rc_client = NULL;
482
483	mtx_unlock(&rc->rc_lock);
484
485	if (client) {
486		CLNT_CLOSE(client);
487		CLNT_RELEASE(client);
488	}
489}
490
491static void
492clnt_reconnect_destroy(CLIENT *cl)
493{
494	struct rc_data *rc = (struct rc_data *)cl->cl_private;
495	SVCXPRT *xprt;
496
497	if (rc->rc_client)
498		CLNT_DESTROY(rc->rc_client);
499	if (rc->rc_backchannel) {
500		xprt = (SVCXPRT *)rc->rc_backchannel;
501		xprt_unregister(xprt);
502		SVC_RELEASE(xprt);
503	}
504	crfree(rc->rc_ucred);
505	mtx_destroy(&rc->rc_lock);
506	mem_free(rc, sizeof(*rc));
507	mem_free(cl, sizeof (CLIENT));
508}
509