2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/limits.h>
38 #include <sys/malloc.h>
40 #include <sys/mutex.h>
43 #include <sys/socket.h>
44 #include <sys/socketvar.h>
49 #include <rpc/rpc_com.h>
51 #include <rpc/rpcsec_tls.h>
53 static enum clnt_stat clnt_reconnect_call(CLIENT *, struct rpc_callextra *,
54 rpcproc_t, struct mbuf *, struct mbuf **, struct timeval);
55 static void clnt_reconnect_geterr(CLIENT *, struct rpc_err *);
56 static bool_t clnt_reconnect_freeres(CLIENT *, xdrproc_t, void *);
57 static void clnt_reconnect_abort(CLIENT *);
58 static bool_t clnt_reconnect_control(CLIENT *, u_int, void *);
59 static void clnt_reconnect_close(CLIENT *);
60 static void clnt_reconnect_destroy(CLIENT *);
62 static struct clnt_ops clnt_reconnect_ops = {
63 .cl_call = clnt_reconnect_call,
64 .cl_abort = clnt_reconnect_abort,
65 .cl_geterr = clnt_reconnect_geterr,
66 .cl_freeres = clnt_reconnect_freeres,
67 .cl_close = clnt_reconnect_close,
68 .cl_destroy = clnt_reconnect_destroy,
69 .cl_control = clnt_reconnect_control
72 static int fake_wchan;
75 clnt_reconnect_create(
76 struct netconfig *nconf, /* network type */
77 struct sockaddr *svcaddr, /* servers address */
78 rpcprog_t program, /* program number */
79 rpcvers_t version, /* version number */
80 size_t sendsz, /* buffer recv size */
81 size_t recvsz) /* buffer send size */
83 CLIENT *cl = NULL; /* client handle */
84 struct rc_data *rc = NULL; /* private data */
86 if (svcaddr == NULL) {
87 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
91 cl = mem_alloc(sizeof (CLIENT));
92 rc = mem_alloc(sizeof (*rc));
93 mtx_init(&rc->rc_lock, "rc->rc_lock", NULL, MTX_DEF);
94 (void) memcpy(&rc->rc_addr, svcaddr, (size_t)svcaddr->sa_len);
96 rc->rc_prog = program;
97 rc->rc_vers = version;
98 rc->rc_sendsz = sendsz;
99 rc->rc_recvsz = recvsz;
100 rc->rc_timeout.tv_sec = -1;
101 rc->rc_timeout.tv_usec = -1;
102 rc->rc_retry.tv_sec = 3;
103 rc->rc_retry.tv_usec = 0;
104 rc->rc_retries = INT_MAX;
105 rc->rc_privport = FALSE;
106 rc->rc_waitchan = "rpcrecv";
108 rc->rc_connecting = FALSE;
109 rc->rc_closed = FALSE;
110 rc->rc_ucred = crdup(curthread->td_ucred);
111 rc->rc_client = NULL;
115 cl->cl_ops = &clnt_reconnect_ops;
116 cl->cl_private = (caddr_t)(void *)rc;
117 cl->cl_auth = authnone_create();
123 static enum clnt_stat
124 clnt_reconnect_connect(CLIENT *cl)
126 struct thread *td = curthread;
127 struct rc_data *rc = (struct rc_data *)cl->cl_private;
132 struct ucred *oldcred;
133 CLIENT *newclient = NULL;
137 mtx_lock(&rc->rc_lock);
138 while (rc->rc_connecting) {
139 error = msleep(rc, &rc->rc_lock,
140 rc->rc_intr ? PCATCH : 0, "rpcrecon", 0);
142 mtx_unlock(&rc->rc_lock);
147 mtx_unlock(&rc->rc_lock);
148 return (RPC_CANTSEND);
151 mtx_unlock(&rc->rc_lock);
152 return (RPC_SUCCESS);
156 * My turn to attempt a connect. The rc_connecting variable
157 * serializes the following code sequence, so it is guaranteed
158 * that rc_client will still be NULL after it is re-locked below,
159 * since that is the only place it is set non-NULL.
161 rc->rc_connecting = TRUE;
162 mtx_unlock(&rc->rc_lock);
164 oldcred = td->td_ucred;
165 td->td_ucred = rc->rc_ucred;
166 so = __rpc_nconf2socket(rc->rc_nconf);
168 stat = rpc_createerr.cf_stat = RPC_TLIERROR;
169 rpc_createerr.cf_error.re_errno = 0;
170 td->td_ucred = oldcred;
175 bindresvport(so, NULL);
177 if (rc->rc_nconf->nc_semantics == NC_TPI_CLTS)
178 newclient = clnt_dg_create(so,
179 (struct sockaddr *) &rc->rc_addr, rc->rc_prog, rc->rc_vers,
180 rc->rc_sendsz, rc->rc_recvsz);
183 * I do not believe a timeout of less than 1sec would make
184 * sense here since short delays can occur when a server is
185 * temporarily overloaded.
187 if (rc->rc_timeout.tv_sec > 0 && rc->rc_timeout.tv_usec >= 0) {
188 error = so_setsockopt(so, SOL_SOCKET, SO_SNDTIMEO,
189 &rc->rc_timeout, sizeof(struct timeval));
191 stat = rpc_createerr.cf_stat = RPC_CANTSEND;
192 rpc_createerr.cf_error.re_errno = error;
193 td->td_ucred = oldcred;
197 newclient = clnt_vc_create(so,
198 (struct sockaddr *) &rc->rc_addr, rc->rc_prog, rc->rc_vers,
199 rc->rc_sendsz, rc->rc_recvsz, rc->rc_intr);
200 if (rc->rc_tls && newclient != NULL) {
201 stat = rpctls_connect(newclient, so, ssl, &reterr);
202 if (stat != RPC_SUCCESS || reterr != RPCTLSERR_OK) {
203 if (stat == RPC_SUCCESS)
205 stat = rpc_createerr.cf_stat = stat;
206 rpc_createerr.cf_error.re_errno = 0;
207 CLNT_CLOSE(newclient);
208 CLNT_RELEASE(newclient);
210 td->td_ucred = oldcred;
215 td->td_ucred = oldcred;
219 rc->rc_err = rpc_createerr.cf_error;
220 stat = rpc_createerr.cf_stat;
224 CLNT_CONTROL(newclient, CLSET_FD_CLOSE, 0);
225 CLNT_CONTROL(newclient, CLSET_CONNECT, &one);
226 CLNT_CONTROL(newclient, CLSET_TIMEOUT, &rc->rc_timeout);
227 CLNT_CONTROL(newclient, CLSET_RETRY_TIMEOUT, &rc->rc_retry);
228 CLNT_CONTROL(newclient, CLSET_WAITCHAN, rc->rc_waitchan);
229 CLNT_CONTROL(newclient, CLSET_INTERRUPTIBLE, &rc->rc_intr);
231 CLNT_CONTROL(newclient, CLSET_TLS, ssl);
232 if (rc->rc_backchannel != NULL)
233 CLNT_CONTROL(newclient, CLSET_BACKCHANNEL, rc->rc_backchannel);
237 mtx_lock(&rc->rc_lock);
238 KASSERT(rc->rc_client == NULL, ("rc_client not null"));
239 if (!rc->rc_closed) {
240 rc->rc_client = newclient;
243 rc->rc_connecting = FALSE;
245 mtx_unlock(&rc->rc_lock);
249 * It has been closed, so discard the new client.
250 * nb: clnt_[dg|vc]_close()/clnt_[dg|vc]_destroy() cannot
251 * be called with the rc_lock mutex held, since they may
252 * msleep() while holding a different mutex.
254 CLNT_CLOSE(newclient);
255 CLNT_RELEASE(newclient);
261 static enum clnt_stat
263 CLIENT *cl, /* client handle */
264 struct rpc_callextra *ext, /* call metadata */
265 rpcproc_t proc, /* procedure number */
266 struct mbuf *args, /* pointer to args */
267 struct mbuf **resultsp, /* pointer to results */
268 struct timeval utimeout)
270 struct rc_data *rc = (struct rc_data *)cl->cl_private;
277 mtx_lock(&rc->rc_lock);
279 mtx_unlock(&rc->rc_lock);
280 return (RPC_CANTSEND);
283 if (!rc->rc_client) {
284 mtx_unlock(&rc->rc_lock);
285 stat = clnt_reconnect_connect(cl);
286 if (stat == RPC_SYSTEMERROR) {
287 error = tsleep(&fake_wchan,
288 rc->rc_intr ? PCATCH : 0, "rpccon", hz);
289 if (error == EINTR || error == ERESTART)
292 if (tries >= rc->rc_retries)
296 if (stat != RPC_SUCCESS)
298 mtx_lock(&rc->rc_lock);
301 if (!rc->rc_client) {
302 mtx_unlock(&rc->rc_lock);
306 CLNT_ACQUIRE(rc->rc_client);
307 client = rc->rc_client;
308 mtx_unlock(&rc->rc_lock);
309 stat = CLNT_CALL_MBUF(client, ext, proc, args,
312 if (stat != RPC_SUCCESS) {
314 CLNT_GETERR(client, &rc->rc_err);
317 if (stat == RPC_TIMEDOUT) {
319 * Check for async send misfeature for NLM
322 if ((rc->rc_timeout.tv_sec == 0
323 && rc->rc_timeout.tv_usec == 0)
324 || (rc->rc_timeout.tv_sec == -1
325 && utimeout.tv_sec == 0
326 && utimeout.tv_usec == 0)) {
327 CLNT_RELEASE(client);
332 if (stat == RPC_TIMEDOUT || stat == RPC_CANTSEND
333 || stat == RPC_CANTRECV) {
335 if (tries >= rc->rc_retries) {
336 CLNT_RELEASE(client);
340 if (ext && ext->rc_feedback)
341 ext->rc_feedback(FEEDBACK_RECONNECT, proc,
342 ext->rc_feedback_arg);
344 mtx_lock(&rc->rc_lock);
346 * Make sure that someone else hasn't already
347 * reconnected by checking if rc_client has changed.
348 * If not, we are done with the client and must
349 * do CLNT_RELEASE(client) twice to dispose of it,
350 * because there is both an initial refcnt and one
351 * acquired by CLNT_ACQUIRE() above.
353 if (rc->rc_client == client) {
354 rc->rc_client = NULL;
355 mtx_unlock(&rc->rc_lock);
356 CLNT_RELEASE(client);
358 mtx_unlock(&rc->rc_lock);
360 CLNT_RELEASE(client);
362 CLNT_RELEASE(client);
365 } while (stat != RPC_SUCCESS);
367 KASSERT(stat != RPC_SUCCESS || *resultsp,
368 ("RPC_SUCCESS without reply"));
374 clnt_reconnect_geterr(CLIENT *cl, struct rpc_err *errp)
376 struct rc_data *rc = (struct rc_data *)cl->cl_private;
382 * Since this function requires that rc_client be valid, it can
383 * only be called when that is guaranteed to be the case.
386 clnt_reconnect_freeres(CLIENT *cl, xdrproc_t xdr_res, void *res_ptr)
388 struct rc_data *rc = (struct rc_data *)cl->cl_private;
390 return (CLNT_FREERES(rc->rc_client, xdr_res, res_ptr));
395 clnt_reconnect_abort(CLIENT *h)
400 * CLNT_CONTROL() on the client returned by clnt_reconnect_create() must
401 * always be called before CLNT_CALL_MBUF() by a single thread only.
404 clnt_reconnect_control(CLIENT *cl, u_int request, void *info)
406 struct rc_data *rc = (struct rc_data *)cl->cl_private;
414 rc->rc_timeout = *(struct timeval *)info;
416 CLNT_CONTROL(rc->rc_client, request, info);
420 *(struct timeval *)info = rc->rc_timeout;
423 case CLSET_RETRY_TIMEOUT:
424 rc->rc_retry = *(struct timeval *)info;
426 CLNT_CONTROL(rc->rc_client, request, info);
429 case CLGET_RETRY_TIMEOUT:
430 *(struct timeval *)info = rc->rc_retry;
434 *(uint32_t *)info = rc->rc_vers;
438 rc->rc_vers = *(uint32_t *) info;
440 CLNT_CONTROL(rc->rc_client, CLSET_VERS, info);
444 *(uint32_t *)info = rc->rc_prog;
448 rc->rc_prog = *(uint32_t *) info;
450 CLNT_CONTROL(rc->rc_client, request, info);
454 rc->rc_waitchan = (char *)info;
456 CLNT_CONTROL(rc->rc_client, request, info);
460 *(const char **) info = rc->rc_waitchan;
463 case CLSET_INTERRUPTIBLE:
464 rc->rc_intr = *(int *) info;
466 CLNT_CONTROL(rc->rc_client, request, info);
469 case CLGET_INTERRUPTIBLE:
470 *(int *) info = rc->rc_intr;
474 rc->rc_retries = *(int *) info;
478 *(int *) info = rc->rc_retries;
482 rc->rc_privport = *(int *) info;
486 *(int *) info = rc->rc_privport;
489 case CLSET_BACKCHANNEL:
490 xprt = (SVCXPRT *)info;
492 rc->rc_backchannel = info;
507 clnt_reconnect_close(CLIENT *cl)
509 struct rc_data *rc = (struct rc_data *)cl->cl_private;
512 mtx_lock(&rc->rc_lock);
515 mtx_unlock(&rc->rc_lock);
519 rc->rc_closed = TRUE;
520 client = rc->rc_client;
521 rc->rc_client = NULL;
523 mtx_unlock(&rc->rc_lock);
527 CLNT_RELEASE(client);
532 clnt_reconnect_destroy(CLIENT *cl)
534 struct rc_data *rc = (struct rc_data *)cl->cl_private;
538 CLNT_DESTROY(rc->rc_client);
539 if (rc->rc_backchannel) {
540 xprt = (SVCXPRT *)rc->rc_backchannel;
541 xprt_unregister(xprt);
544 crfree(rc->rc_ucred);
545 mtx_destroy(&rc->rc_lock);
546 mem_free(rc, sizeof(*rc));
547 mem_free(cl, sizeof (CLIENT));