1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2008 Isilon Inc http://www.isilon.com/
5 * Authors: Doug Rabson <dfr@rabson.org>
6 * Developed with Red Inc: Alfred Perlstein <alfred@freebsd.org>
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#include <sys/cdefs.h>
31__FBSDID("$FreeBSD$");
32
33#include <sys/param.h>
34#include <sys/systm.h>
35#include <sys/kernel.h>
36#include <sys/limits.h>
37#include <sys/lock.h>
38#include <sys/malloc.h>
39#include <sys/mbuf.h>
40#include <sys/mutex.h>
41#include <sys/pcpu.h>
42#include <sys/proc.h>
43#include <sys/socket.h>
44#include <sys/socketvar.h>
45#include <sys/time.h>
46#include <sys/uio.h>
47
48#include <rpc/rpc.h>
49#include <rpc/rpc_com.h>
50#include <rpc/krpc.h>
51
52static enum clnt_stat clnt_reconnect_call(CLIENT *, struct rpc_callextra *,
53    rpcproc_t, struct mbuf *, struct mbuf **, struct timeval);
54static void clnt_reconnect_geterr(CLIENT *, struct rpc_err *);
55static bool_t clnt_reconnect_freeres(CLIENT *, xdrproc_t, void *);
56static void clnt_reconnect_abort(CLIENT *);
57static bool_t clnt_reconnect_control(CLIENT *, u_int, void *);
58static void clnt_reconnect_close(CLIENT *);
59static void clnt_reconnect_destroy(CLIENT *);
60
61static struct clnt_ops clnt_reconnect_ops = {
62	.cl_call =	clnt_reconnect_call,
63	.cl_abort =	clnt_reconnect_abort,
64	.cl_geterr =	clnt_reconnect_geterr,
65	.cl_freeres =	clnt_reconnect_freeres,
66	.cl_close =	clnt_reconnect_close,
67	.cl_destroy =	clnt_reconnect_destroy,
68	.cl_control =	clnt_reconnect_control
69};
70
71static int	fake_wchan;
72
73CLIENT *
74clnt_reconnect_create(
75	struct netconfig *nconf,	/* network type */
76	struct sockaddr *svcaddr,	/* servers address */
77	rpcprog_t program,		/* program number */
78	rpcvers_t version,		/* version number */
79	size_t sendsz,			/* buffer recv size */
80	size_t recvsz)			/* buffer send size */
81{
82	CLIENT *cl = NULL;		/* client handle */
83	struct rc_data *rc = NULL;	/* private data */
84
85	if (svcaddr == NULL) {
86		rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
87		return (NULL);
88	}
89
90	cl = mem_alloc(sizeof (CLIENT));
91	rc = mem_alloc(sizeof (*rc));
92	mtx_init(&rc->rc_lock, "rc->rc_lock", NULL, MTX_DEF);
93	(void) memcpy(&rc->rc_addr, svcaddr, (size_t)svcaddr->sa_len);
94	rc->rc_nconf = nconf;
95	rc->rc_prog = program;
96	rc->rc_vers = version;
97	rc->rc_sendsz = sendsz;
98	rc->rc_recvsz = recvsz;
99	rc->rc_timeout.tv_sec = -1;
100	rc->rc_timeout.tv_usec = -1;
101	rc->rc_retry.tv_sec = 3;
102	rc->rc_retry.tv_usec = 0;
103	rc->rc_retries = INT_MAX;
104	rc->rc_privport = FALSE;
105	rc->rc_waitchan = "rpcrecv";
106	rc->rc_intr = 0;
107	rc->rc_connecting = FALSE;
108	rc->rc_closed = FALSE;
109	rc->rc_ucred = crdup(curthread->td_ucred);
110	rc->rc_client = NULL;
111	rc->rc_reconcall = NULL;
112	rc->rc_reconarg = NULL;
113
114	cl->cl_refs = 1;
115	cl->cl_ops = &clnt_reconnect_ops;
116	cl->cl_private = (caddr_t)(void *)rc;
117	cl->cl_auth = authnone_create();
118	cl->cl_tp = NULL;
119	cl->cl_netid = NULL;
120	return (cl);
121}
122
123static enum clnt_stat
124clnt_reconnect_connect(CLIENT *cl)
125{
126	struct thread *td = curthread;
127	struct rc_data *rc = (struct rc_data *)cl->cl_private;
128	struct socket *so;
129	enum clnt_stat stat;
130	int error;
131	int one = 1;
132	struct ucred *oldcred;
133	CLIENT *newclient = NULL;
134
135	mtx_lock(&rc->rc_lock);
136	while (rc->rc_connecting) {
137		error = msleep(rc, &rc->rc_lock,
138		    rc->rc_intr ? PCATCH : 0, "rpcrecon", 0);
139		if (error) {
140			mtx_unlock(&rc->rc_lock);
141			return (RPC_INTR);
142		}
143	}
144	if (rc->rc_closed) {
145		mtx_unlock(&rc->rc_lock);
146		return (RPC_CANTSEND);
147	}
148	if (rc->rc_client) {
149		mtx_unlock(&rc->rc_lock);
150		return (RPC_SUCCESS);
151	}
152
153	/*
154	 * My turn to attempt a connect. The rc_connecting variable
155	 * serializes the following code sequence, so it is guaranteed
156	 * that rc_client will still be NULL after it is re-locked below,
157	 * since that is the only place it is set non-NULL.
158	 */
159	rc->rc_connecting = TRUE;
160	mtx_unlock(&rc->rc_lock);
161
162	oldcred = td->td_ucred;
163	td->td_ucred = rc->rc_ucred;
164	so = __rpc_nconf2socket(rc->rc_nconf);
165	if (!so) {
166		stat = rpc_createerr.cf_stat = RPC_TLIERROR;
167		rpc_createerr.cf_error.re_errno = 0;
168		td->td_ucred = oldcred;
169		goto out;
170	}
171
172	if (rc->rc_privport)
173		bindresvport(so, NULL);
174
175	if (rc->rc_nconf->nc_semantics == NC_TPI_CLTS)
176		newclient = clnt_dg_create(so,
177		    (struct sockaddr *) &rc->rc_addr, rc->rc_prog, rc->rc_vers,
178		    rc->rc_sendsz, rc->rc_recvsz);
179	else {
180		/*
181		 * I do not believe a timeout of less than 1sec would make
182		 * sense here since short delays can occur when a server is
183		 * temporarily overloaded.
184		 */
185		if (rc->rc_timeout.tv_sec > 0 && rc->rc_timeout.tv_usec >= 0) {
186			error = so_setsockopt(so, SOL_SOCKET, SO_SNDTIMEO,
187			    &rc->rc_timeout, sizeof(struct timeval));
188			if (error != 0) {
189				stat = rpc_createerr.cf_stat = RPC_CANTSEND;
190				rpc_createerr.cf_error.re_errno = error;
191				td->td_ucred = oldcred;
192				goto out;
193			}
194		}
195		newclient = clnt_vc_create(so,
196		    (struct sockaddr *) &rc->rc_addr, rc->rc_prog, rc->rc_vers,
197		    rc->rc_sendsz, rc->rc_recvsz, rc->rc_intr);
198		if (newclient != NULL && rc->rc_reconcall != NULL)
199			(*rc->rc_reconcall)(newclient, rc->rc_reconarg,
200			    rc->rc_ucred);
201	}
202	td->td_ucred = oldcred;
203
204	if (!newclient) {
205		soclose(so);
206		rc->rc_err = rpc_createerr.cf_error;
207		stat = rpc_createerr.cf_stat;
208		goto out;
209	}
210
211	CLNT_CONTROL(newclient, CLSET_FD_CLOSE, 0);
212	CLNT_CONTROL(newclient, CLSET_CONNECT, &one);
213	CLNT_CONTROL(newclient, CLSET_TIMEOUT, &rc->rc_timeout);
214	CLNT_CONTROL(newclient, CLSET_RETRY_TIMEOUT, &rc->rc_retry);
215	CLNT_CONTROL(newclient, CLSET_WAITCHAN, rc->rc_waitchan);
216	CLNT_CONTROL(newclient, CLSET_INTERRUPTIBLE, &rc->rc_intr);
217	if (rc->rc_backchannel != NULL)
218		CLNT_CONTROL(newclient, CLSET_BACKCHANNEL, rc->rc_backchannel);
219	stat = RPC_SUCCESS;
220
221out:
222	mtx_lock(&rc->rc_lock);
223	KASSERT(rc->rc_client == NULL, ("rc_client not null"));
224	if (!rc->rc_closed) {
225		rc->rc_client = newclient;
226		newclient = NULL;
227	}
228	rc->rc_connecting = FALSE;
229	wakeup(rc);
230	mtx_unlock(&rc->rc_lock);
231
232	if (newclient) {
233		/*
234		 * It has been closed, so discard the new client.
235		 * nb: clnt_[dg|vc]_close()/clnt_[dg|vc]_destroy() cannot
236		 * be called with the rc_lock mutex held, since they may
237		 * msleep() while holding a different mutex.
238		 */
239		CLNT_CLOSE(newclient);
240		CLNT_RELEASE(newclient);
241	}
242
243	return (stat);
244}
245
246static enum clnt_stat
247clnt_reconnect_call(
248	CLIENT		*cl,		/* client handle */
249	struct rpc_callextra *ext,	/* call metadata */
250	rpcproc_t	proc,		/* procedure number */
251	struct mbuf	*args,		/* pointer to args */
252	struct mbuf	**resultsp,	/* pointer to results */
253	struct timeval	utimeout)
254{
255	struct rc_data *rc = (struct rc_data *)cl->cl_private;
256	CLIENT *client;
257	enum clnt_stat stat;
258	int tries, error;
259
260	tries = 0;
261	do {
262		mtx_lock(&rc->rc_lock);
263		if (rc->rc_closed) {
264			mtx_unlock(&rc->rc_lock);
265			return (RPC_CANTSEND);
266		}
267
268		if (!rc->rc_client) {
269			mtx_unlock(&rc->rc_lock);
270			stat = clnt_reconnect_connect(cl);
271			if (stat == RPC_SYSTEMERROR) {
272				error = tsleep(&fake_wchan,
273				    rc->rc_intr ? PCATCH : 0, "rpccon", hz);
274				if (error == EINTR || error == ERESTART)
275					return (RPC_INTR);
276				tries++;
277				if (tries >= rc->rc_retries)
278					return (stat);
279				continue;
280			}
281			if (stat != RPC_SUCCESS)
282				return (stat);
283			mtx_lock(&rc->rc_lock);
284		}
285
286		if (!rc->rc_client) {
287			mtx_unlock(&rc->rc_lock);
288			stat = RPC_FAILED;
289			continue;
290		}
291		CLNT_ACQUIRE(rc->rc_client);
292		client = rc->rc_client;
293		mtx_unlock(&rc->rc_lock);
294		stat = CLNT_CALL_MBUF(client, ext, proc, args,
295		    resultsp, utimeout);
296
297		if (stat != RPC_SUCCESS) {
298			if (!ext)
299				CLNT_GETERR(client, &rc->rc_err);
300		}
301
302		if (stat == RPC_TIMEDOUT) {
303			/*
304			 * Check for async send misfeature for NLM
305			 * protocol.
306			 */
307			if ((rc->rc_timeout.tv_sec == 0
308				&& rc->rc_timeout.tv_usec == 0)
309			    || (rc->rc_timeout.tv_sec == -1
310				&& utimeout.tv_sec == 0
311				&& utimeout.tv_usec == 0)) {
312				CLNT_RELEASE(client);
313				break;
314			}
315		}
316
317		if (stat == RPC_TIMEDOUT || stat == RPC_CANTSEND
318		    || stat == RPC_CANTRECV) {
319			tries++;
320			if (tries >= rc->rc_retries) {
321				CLNT_RELEASE(client);
322				break;
323			}
324
325			if (ext && ext->rc_feedback)
326				ext->rc_feedback(FEEDBACK_RECONNECT, proc,
327				    ext->rc_feedback_arg);
328
329			mtx_lock(&rc->rc_lock);
330			/*
331			 * Make sure that someone else hasn't already
332			 * reconnected by checking if rc_client has changed.
333			 * If not, we are done with the client and must
334			 * do CLNT_RELEASE(client) twice to dispose of it,
335			 * because there is both an initial refcnt and one
336			 * acquired by CLNT_ACQUIRE() above.
337			 */
338			if (rc->rc_client == client) {
339				rc->rc_client = NULL;
340				mtx_unlock(&rc->rc_lock);
341				CLNT_RELEASE(client);
342			} else {
343				mtx_unlock(&rc->rc_lock);
344			}
345			CLNT_RELEASE(client);
346		} else {
347			CLNT_RELEASE(client);
348			break;
349		}
350	} while (stat != RPC_SUCCESS);
351
352	KASSERT(stat != RPC_SUCCESS || *resultsp,
353	    ("RPC_SUCCESS without reply"));
354
355	return (stat);
356}
357
358static void
359clnt_reconnect_geterr(CLIENT *cl, struct rpc_err *errp)
360{
361	struct rc_data *rc = (struct rc_data *)cl->cl_private;
362
363	*errp = rc->rc_err;
364}
365
366/*
367 * Since this function requires that rc_client be valid, it can
368 * only be called when that is guaranteed to be the case.
369 */
370static bool_t
371clnt_reconnect_freeres(CLIENT *cl, xdrproc_t xdr_res, void *res_ptr)
372{
373	struct rc_data *rc = (struct rc_data *)cl->cl_private;
374
375	return (CLNT_FREERES(rc->rc_client, xdr_res, res_ptr));
376}
377
378/*ARGSUSED*/
379static void
380clnt_reconnect_abort(CLIENT *h)
381{
382}
383
384/*
385 * CLNT_CONTROL() on the client returned by clnt_reconnect_create() must
386 * always be called before CLNT_CALL_MBUF() by a single thread only.
387 */
388static bool_t
389clnt_reconnect_control(CLIENT *cl, u_int request, void *info)
390{
391	struct rc_data *rc = (struct rc_data *)cl->cl_private;
392	SVCXPRT *xprt;
393	struct rpc_reconupcall *upcp;
394
395	if (info == NULL) {
396		return (FALSE);
397	}
398	switch (request) {
399	case CLSET_TIMEOUT:
400		rc->rc_timeout = *(struct timeval *)info;
401		if (rc->rc_client)
402			CLNT_CONTROL(rc->rc_client, request, info);
403		break;
404
405	case CLGET_TIMEOUT:
406		*(struct timeval *)info = rc->rc_timeout;
407		break;
408
409	case CLSET_RETRY_TIMEOUT:
410		rc->rc_retry = *(struct timeval *)info;
411		if (rc->rc_client)
412			CLNT_CONTROL(rc->rc_client, request, info);
413		break;
414
415	case CLGET_RETRY_TIMEOUT:
416		*(struct timeval *)info = rc->rc_retry;
417		break;
418
419	case CLGET_VERS:
420		*(uint32_t *)info = rc->rc_vers;
421		break;
422
423	case CLSET_VERS:
424		rc->rc_vers = *(uint32_t *) info;
425		if (rc->rc_client)
426			CLNT_CONTROL(rc->rc_client, CLSET_VERS, info);
427		break;
428
429	case CLGET_PROG:
430		*(uint32_t *)info = rc->rc_prog;
431		break;
432
433	case CLSET_PROG:
434		rc->rc_prog = *(uint32_t *) info;
435		if (rc->rc_client)
436			CLNT_CONTROL(rc->rc_client, request, info);
437		break;
438
439	case CLSET_WAITCHAN:
440		rc->rc_waitchan = (char *)info;
441		if (rc->rc_client)
442			CLNT_CONTROL(rc->rc_client, request, info);
443		break;
444
445	case CLGET_WAITCHAN:
446		*(const char **) info = rc->rc_waitchan;
447		break;
448
449	case CLSET_INTERRUPTIBLE:
450		rc->rc_intr = *(int *) info;
451		if (rc->rc_client)
452			CLNT_CONTROL(rc->rc_client, request, info);
453		break;
454
455	case CLGET_INTERRUPTIBLE:
456		*(int *) info = rc->rc_intr;
457		break;
458
459	case CLSET_RETRIES:
460		rc->rc_retries = *(int *) info;
461		break;
462
463	case CLGET_RETRIES:
464		*(int *) info = rc->rc_retries;
465		break;
466
467	case CLSET_PRIVPORT:
468		rc->rc_privport = *(int *) info;
469		break;
470
471	case CLGET_PRIVPORT:
472		*(int *) info = rc->rc_privport;
473		break;
474
475	case CLSET_BACKCHANNEL:
476		xprt = (SVCXPRT *)info;
477		xprt_register(xprt);
478		rc->rc_backchannel = info;
479		break;
480
481	case CLSET_RECONUPCALL:
482		upcp = (struct rpc_reconupcall *)info;
483		rc->rc_reconcall = upcp->call;
484		rc->rc_reconarg = upcp->arg;
485		break;
486
487	default:
488		return (FALSE);
489	}
490
491	return (TRUE);
492}
493
494static void
495clnt_reconnect_close(CLIENT *cl)
496{
497	struct rc_data *rc = (struct rc_data *)cl->cl_private;
498	CLIENT *client;
499
500	mtx_lock(&rc->rc_lock);
501
502	if (rc->rc_closed) {
503		mtx_unlock(&rc->rc_lock);
504		return;
505	}
506
507	rc->rc_closed = TRUE;
508	client = rc->rc_client;
509	rc->rc_client = NULL;
510
511	mtx_unlock(&rc->rc_lock);
512
513	if (client) {
514		CLNT_CLOSE(client);
515		CLNT_RELEASE(client);
516	}
517}
518
519static void
520clnt_reconnect_destroy(CLIENT *cl)
521{
522	struct rc_data *rc = (struct rc_data *)cl->cl_private;
523	SVCXPRT *xprt;
524
525	if (rc->rc_client)
526		CLNT_DESTROY(rc->rc_client);
527	if (rc->rc_backchannel) {
528		xprt = (SVCXPRT *)rc->rc_backchannel;
529		KASSERT(xprt->xp_socket == NULL,
530		    ("clnt_reconnect_destroy: xp_socket not NULL"));
531		xprt_unregister(xprt);
532		SVC_RELEASE(xprt);
533	}
534	crfree(rc->rc_ucred);
535	mtx_destroy(&rc->rc_lock);
536	mem_free(rc->rc_reconarg, 0);
537	mem_free(rc, sizeof(*rc));
538	mem_free(cl, sizeof (CLIENT));
539}
540