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;
113 rc->rc_tlscertname = NULL;
116 cl->cl_ops = &clnt_reconnect_ops;
117 cl->cl_private = (caddr_t)(void *)rc;
118 cl->cl_auth = authnone_create();
124 static enum clnt_stat
125 clnt_reconnect_connect(CLIENT *cl)
127 struct thread *td = curthread;
128 struct rc_data *rc = (struct rc_data *)cl->cl_private;
133 struct ucred *oldcred;
134 CLIENT *newclient = NULL;
138 mtx_lock(&rc->rc_lock);
139 while (rc->rc_connecting) {
140 error = msleep(rc, &rc->rc_lock,
141 rc->rc_intr ? PCATCH : 0, "rpcrecon", 0);
143 mtx_unlock(&rc->rc_lock);
148 mtx_unlock(&rc->rc_lock);
149 return (RPC_CANTSEND);
152 mtx_unlock(&rc->rc_lock);
153 return (RPC_SUCCESS);
157 * My turn to attempt a connect. The rc_connecting variable
158 * serializes the following code sequence, so it is guaranteed
159 * that rc_client will still be NULL after it is re-locked below,
160 * since that is the only place it is set non-NULL.
162 rc->rc_connecting = TRUE;
163 mtx_unlock(&rc->rc_lock);
165 oldcred = td->td_ucred;
166 td->td_ucred = rc->rc_ucred;
167 so = __rpc_nconf2socket(rc->rc_nconf);
169 stat = rpc_createerr.cf_stat = RPC_TLIERROR;
170 rpc_createerr.cf_error.re_errno = 0;
171 td->td_ucred = oldcred;
176 bindresvport(so, NULL);
178 if (rc->rc_nconf->nc_semantics == NC_TPI_CLTS)
179 newclient = clnt_dg_create(so,
180 (struct sockaddr *) &rc->rc_addr, rc->rc_prog, rc->rc_vers,
181 rc->rc_sendsz, rc->rc_recvsz);
184 * I do not believe a timeout of less than 1sec would make
185 * sense here since short delays can occur when a server is
186 * temporarily overloaded.
188 if (rc->rc_timeout.tv_sec > 0 && rc->rc_timeout.tv_usec >= 0) {
189 error = so_setsockopt(so, SOL_SOCKET, SO_SNDTIMEO,
190 &rc->rc_timeout, sizeof(struct timeval));
192 stat = rpc_createerr.cf_stat = RPC_CANTSEND;
193 rpc_createerr.cf_error.re_errno = error;
194 td->td_ucred = oldcred;
198 newclient = clnt_vc_create(so,
199 (struct sockaddr *) &rc->rc_addr, rc->rc_prog, rc->rc_vers,
200 rc->rc_sendsz, rc->rc_recvsz, rc->rc_intr);
201 if (rc->rc_tls && newclient != NULL) {
202 stat = rpctls_connect(newclient, rc->rc_tlscertname, so,
204 if (stat != RPC_SUCCESS || reterr != RPCTLSERR_OK) {
205 if (stat == RPC_SUCCESS)
207 stat = rpc_createerr.cf_stat = stat;
208 rpc_createerr.cf_error.re_errno = 0;
209 CLNT_CLOSE(newclient);
210 CLNT_RELEASE(newclient);
212 td->td_ucred = oldcred;
217 td->td_ucred = oldcred;
221 rc->rc_err = rpc_createerr.cf_error;
222 stat = rpc_createerr.cf_stat;
226 CLNT_CONTROL(newclient, CLSET_FD_CLOSE, 0);
227 CLNT_CONTROL(newclient, CLSET_CONNECT, &one);
228 CLNT_CONTROL(newclient, CLSET_TIMEOUT, &rc->rc_timeout);
229 CLNT_CONTROL(newclient, CLSET_RETRY_TIMEOUT, &rc->rc_retry);
230 CLNT_CONTROL(newclient, CLSET_WAITCHAN, rc->rc_waitchan);
231 CLNT_CONTROL(newclient, CLSET_INTERRUPTIBLE, &rc->rc_intr);
233 CLNT_CONTROL(newclient, CLSET_TLS, ssl);
234 if (rc->rc_backchannel != NULL)
235 CLNT_CONTROL(newclient, CLSET_BACKCHANNEL, rc->rc_backchannel);
239 mtx_lock(&rc->rc_lock);
240 KASSERT(rc->rc_client == NULL, ("rc_client not null"));
241 if (!rc->rc_closed) {
242 rc->rc_client = newclient;
245 rc->rc_connecting = FALSE;
247 mtx_unlock(&rc->rc_lock);
251 * It has been closed, so discard the new client.
252 * nb: clnt_[dg|vc]_close()/clnt_[dg|vc]_destroy() cannot
253 * be called with the rc_lock mutex held, since they may
254 * msleep() while holding a different mutex.
256 CLNT_CLOSE(newclient);
257 CLNT_RELEASE(newclient);
263 static enum clnt_stat
265 CLIENT *cl, /* client handle */
266 struct rpc_callextra *ext, /* call metadata */
267 rpcproc_t proc, /* procedure number */
268 struct mbuf *args, /* pointer to args */
269 struct mbuf **resultsp, /* pointer to results */
270 struct timeval utimeout)
272 struct rc_data *rc = (struct rc_data *)cl->cl_private;
279 mtx_lock(&rc->rc_lock);
281 mtx_unlock(&rc->rc_lock);
282 return (RPC_CANTSEND);
285 if (!rc->rc_client) {
286 mtx_unlock(&rc->rc_lock);
287 stat = clnt_reconnect_connect(cl);
288 if (stat == RPC_SYSTEMERROR) {
289 error = tsleep(&fake_wchan,
290 rc->rc_intr ? PCATCH : 0, "rpccon", hz);
291 if (error == EINTR || error == ERESTART)
294 if (tries >= rc->rc_retries)
298 if (stat != RPC_SUCCESS)
300 mtx_lock(&rc->rc_lock);
303 if (!rc->rc_client) {
304 mtx_unlock(&rc->rc_lock);
308 CLNT_ACQUIRE(rc->rc_client);
309 client = rc->rc_client;
310 mtx_unlock(&rc->rc_lock);
311 stat = CLNT_CALL_MBUF(client, ext, proc, args,
314 if (stat != RPC_SUCCESS) {
316 CLNT_GETERR(client, &rc->rc_err);
319 if (stat == RPC_TIMEDOUT) {
321 * Check for async send misfeature for NLM
324 if ((rc->rc_timeout.tv_sec == 0
325 && rc->rc_timeout.tv_usec == 0)
326 || (rc->rc_timeout.tv_sec == -1
327 && utimeout.tv_sec == 0
328 && utimeout.tv_usec == 0)) {
329 CLNT_RELEASE(client);
334 if (stat == RPC_TIMEDOUT || stat == RPC_CANTSEND
335 || stat == RPC_CANTRECV) {
337 if (tries >= rc->rc_retries) {
338 CLNT_RELEASE(client);
342 if (ext && ext->rc_feedback)
343 ext->rc_feedback(FEEDBACK_RECONNECT, proc,
344 ext->rc_feedback_arg);
346 mtx_lock(&rc->rc_lock);
348 * Make sure that someone else hasn't already
349 * reconnected by checking if rc_client has changed.
350 * If not, we are done with the client and must
351 * do CLNT_RELEASE(client) twice to dispose of it,
352 * because there is both an initial refcnt and one
353 * acquired by CLNT_ACQUIRE() above.
355 if (rc->rc_client == client) {
356 rc->rc_client = NULL;
357 mtx_unlock(&rc->rc_lock);
358 CLNT_RELEASE(client);
360 mtx_unlock(&rc->rc_lock);
362 CLNT_RELEASE(client);
364 CLNT_RELEASE(client);
367 } while (stat != RPC_SUCCESS);
369 KASSERT(stat != RPC_SUCCESS || *resultsp,
370 ("RPC_SUCCESS without reply"));
376 clnt_reconnect_geterr(CLIENT *cl, struct rpc_err *errp)
378 struct rc_data *rc = (struct rc_data *)cl->cl_private;
384 * Since this function requires that rc_client be valid, it can
385 * only be called when that is guaranteed to be the case.
388 clnt_reconnect_freeres(CLIENT *cl, xdrproc_t xdr_res, void *res_ptr)
390 struct rc_data *rc = (struct rc_data *)cl->cl_private;
392 return (CLNT_FREERES(rc->rc_client, xdr_res, res_ptr));
397 clnt_reconnect_abort(CLIENT *h)
402 * CLNT_CONTROL() on the client returned by clnt_reconnect_create() must
403 * always be called before CLNT_CALL_MBUF() by a single thread only.
406 clnt_reconnect_control(CLIENT *cl, u_int request, void *info)
408 struct rc_data *rc = (struct rc_data *)cl->cl_private;
417 rc->rc_timeout = *(struct timeval *)info;
419 CLNT_CONTROL(rc->rc_client, request, info);
423 *(struct timeval *)info = rc->rc_timeout;
426 case CLSET_RETRY_TIMEOUT:
427 rc->rc_retry = *(struct timeval *)info;
429 CLNT_CONTROL(rc->rc_client, request, info);
432 case CLGET_RETRY_TIMEOUT:
433 *(struct timeval *)info = rc->rc_retry;
437 *(uint32_t *)info = rc->rc_vers;
441 rc->rc_vers = *(uint32_t *) info;
443 CLNT_CONTROL(rc->rc_client, CLSET_VERS, info);
447 *(uint32_t *)info = rc->rc_prog;
451 rc->rc_prog = *(uint32_t *) info;
453 CLNT_CONTROL(rc->rc_client, request, info);
457 rc->rc_waitchan = (char *)info;
459 CLNT_CONTROL(rc->rc_client, request, info);
463 *(const char **) info = rc->rc_waitchan;
466 case CLSET_INTERRUPTIBLE:
467 rc->rc_intr = *(int *) info;
469 CLNT_CONTROL(rc->rc_client, request, info);
472 case CLGET_INTERRUPTIBLE:
473 *(int *) info = rc->rc_intr;
477 rc->rc_retries = *(int *) info;
481 *(int *) info = rc->rc_retries;
485 rc->rc_privport = *(int *) info;
489 *(int *) info = rc->rc_privport;
492 case CLSET_BACKCHANNEL:
493 xprt = (SVCXPRT *)info;
495 rc->rc_backchannel = info;
502 case CLSET_TLSCERTNAME:
503 slen = strlen(info) + 1;
505 * tlscertname with "key.pem" appended to it forms a file
506 * name. As such, the maximum allowable strlen(info) is
507 * NAME_MAX - 7. However, "slen" includes the nul termination
508 * byte so it can be up to NAME_MAX - 6.
510 if (slen <= 1 || slen > NAME_MAX - 6)
512 rc->rc_tlscertname = mem_alloc(slen);
513 strlcpy(rc->rc_tlscertname, info, slen);
524 clnt_reconnect_close(CLIENT *cl)
526 struct rc_data *rc = (struct rc_data *)cl->cl_private;
529 mtx_lock(&rc->rc_lock);
532 mtx_unlock(&rc->rc_lock);
536 rc->rc_closed = TRUE;
537 client = rc->rc_client;
538 rc->rc_client = NULL;
540 mtx_unlock(&rc->rc_lock);
544 CLNT_RELEASE(client);
549 clnt_reconnect_destroy(CLIENT *cl)
551 struct rc_data *rc = (struct rc_data *)cl->cl_private;
555 CLNT_DESTROY(rc->rc_client);
556 if (rc->rc_backchannel) {
557 xprt = (SVCXPRT *)rc->rc_backchannel;
558 xprt_unregister(xprt);
561 crfree(rc->rc_ucred);
562 mtx_destroy(&rc->rc_lock);
563 mem_free(rc->rc_tlscertname, 0); /* 0 ok, since arg. ignored. */
564 mem_free(rc, sizeof(*rc));
565 mem_free(cl, sizeof (CLIENT));