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>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/limits.h>
36 #include <sys/malloc.h>
38 #include <sys/mutex.h>
41 #include <sys/socket.h>
42 #include <sys/socketvar.h>
47 #include <rpc/rpc_com.h>
50 static enum clnt_stat clnt_reconnect_call(CLIENT *, struct rpc_callextra *,
51 rpcproc_t, struct mbuf *, struct mbuf **, struct timeval);
52 static void clnt_reconnect_geterr(CLIENT *, struct rpc_err *);
53 static bool_t clnt_reconnect_freeres(CLIENT *, xdrproc_t, void *);
54 static void clnt_reconnect_abort(CLIENT *);
55 static bool_t clnt_reconnect_control(CLIENT *, u_int, void *);
56 static void clnt_reconnect_close(CLIENT *);
57 static void clnt_reconnect_destroy(CLIENT *);
59 static 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
69 static int fake_wchan;
72 clnt_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 */
80 CLIENT *cl = NULL; /* client handle */
81 struct rc_data *rc = NULL; /* private data */
83 if (svcaddr == NULL) {
84 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
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);
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";
105 rc->rc_connecting = FALSE;
106 rc->rc_closed = FALSE;
107 rc->rc_ucred = crdup(curthread->td_ucred);
108 rc->rc_client = NULL;
111 cl->cl_ops = &clnt_reconnect_ops;
112 cl->cl_private = (caddr_t)(void *)rc;
113 cl->cl_auth = authnone_create();
119 static enum clnt_stat
120 clnt_reconnect_connect(CLIENT *cl)
122 struct thread *td = curthread;
123 struct rc_data *rc = (struct rc_data *)cl->cl_private;
128 struct ucred *oldcred;
129 CLIENT *newclient = NULL;
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);
136 mtx_unlock(&rc->rc_lock);
141 mtx_unlock(&rc->rc_lock);
142 return (RPC_CANTSEND);
145 mtx_unlock(&rc->rc_lock);
146 return (RPC_SUCCESS);
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.
155 rc->rc_connecting = TRUE;
156 mtx_unlock(&rc->rc_lock);
158 oldcred = td->td_ucred;
159 td->td_ucred = rc->rc_ucred;
160 so = __rpc_nconf2socket(rc->rc_nconf);
162 stat = rpc_createerr.cf_stat = RPC_TLIERROR;
163 rpc_createerr.cf_error.re_errno = 0;
164 td->td_ucred = oldcred;
169 bindresvport(so, NULL);
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);
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;
183 rc->rc_err = rpc_createerr.cf_error;
184 stat = rpc_createerr.cf_stat;
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);
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;
205 rc->rc_connecting = FALSE;
207 mtx_unlock(&rc->rc_lock);
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.
216 CLNT_CLOSE(newclient);
217 CLNT_RELEASE(newclient);
223 static enum clnt_stat
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)
232 struct rc_data *rc = (struct rc_data *)cl->cl_private;
239 mtx_lock(&rc->rc_lock);
241 mtx_unlock(&rc->rc_lock);
242 return (RPC_CANTSEND);
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 : 0, "rpccon", hz);
251 if (error == EINTR || error == ERESTART)
254 if (tries >= rc->rc_retries)
258 if (stat != RPC_SUCCESS)
260 mtx_lock(&rc->rc_lock);
263 if (!rc->rc_client) {
264 mtx_unlock(&rc->rc_lock);
268 CLNT_ACQUIRE(rc->rc_client);
269 client = rc->rc_client;
270 mtx_unlock(&rc->rc_lock);
271 stat = CLNT_CALL_MBUF(client, ext, proc, args,
274 if (stat != RPC_SUCCESS) {
276 CLNT_GETERR(client, &rc->rc_err);
279 if (stat == RPC_TIMEDOUT) {
281 * Check for async send misfeature for NLM
284 if ((rc->rc_timeout.tv_sec == 0
285 && rc->rc_timeout.tv_usec == 0)
286 || (rc->rc_timeout.tv_sec == -1
287 && utimeout.tv_sec == 0
288 && utimeout.tv_usec == 0)) {
289 CLNT_RELEASE(client);
294 if (stat == RPC_TIMEDOUT || stat == RPC_CANTSEND
295 || stat == RPC_CANTRECV) {
297 if (tries >= rc->rc_retries) {
298 CLNT_RELEASE(client);
302 if (ext && ext->rc_feedback)
303 ext->rc_feedback(FEEDBACK_RECONNECT, proc,
304 ext->rc_feedback_arg);
306 mtx_lock(&rc->rc_lock);
308 * Make sure that someone else hasn't already
309 * reconnected by checking if rc_client has changed.
310 * If not, we are done with the client and must
311 * do CLNT_RELEASE(client) twice to dispose of it,
312 * because there is both an initial refcnt and one
313 * acquired by CLNT_ACQUIRE() above.
315 if (rc->rc_client == client) {
316 rc->rc_client = NULL;
317 mtx_unlock(&rc->rc_lock);
318 CLNT_RELEASE(client);
320 mtx_unlock(&rc->rc_lock);
322 CLNT_RELEASE(client);
324 CLNT_RELEASE(client);
327 } while (stat != RPC_SUCCESS);
329 KASSERT(stat != RPC_SUCCESS || *resultsp,
330 ("RPC_SUCCESS without reply"));
336 clnt_reconnect_geterr(CLIENT *cl, struct rpc_err *errp)
338 struct rc_data *rc = (struct rc_data *)cl->cl_private;
344 * Since this function requires that rc_client be valid, it can
345 * only be called when that is guaranteed to be the case.
348 clnt_reconnect_freeres(CLIENT *cl, xdrproc_t xdr_res, void *res_ptr)
350 struct rc_data *rc = (struct rc_data *)cl->cl_private;
352 return (CLNT_FREERES(rc->rc_client, xdr_res, res_ptr));
357 clnt_reconnect_abort(CLIENT *h)
362 * CLNT_CONTROL() on the client returned by clnt_reconnect_create() must
363 * always be called before CLNT_CALL_MBUF() by a single thread only.
366 clnt_reconnect_control(CLIENT *cl, u_int request, void *info)
368 struct rc_data *rc = (struct rc_data *)cl->cl_private;
376 rc->rc_timeout = *(struct timeval *)info;
378 CLNT_CONTROL(rc->rc_client, request, info);
382 *(struct timeval *)info = rc->rc_timeout;
385 case CLSET_RETRY_TIMEOUT:
386 rc->rc_retry = *(struct timeval *)info;
388 CLNT_CONTROL(rc->rc_client, request, info);
391 case CLGET_RETRY_TIMEOUT:
392 *(struct timeval *)info = rc->rc_retry;
396 *(uint32_t *)info = rc->rc_vers;
400 rc->rc_vers = *(uint32_t *) info;
402 CLNT_CONTROL(rc->rc_client, CLSET_VERS, info);
406 *(uint32_t *)info = rc->rc_prog;
410 rc->rc_prog = *(uint32_t *) info;
412 CLNT_CONTROL(rc->rc_client, request, info);
416 rc->rc_waitchan = (char *)info;
418 CLNT_CONTROL(rc->rc_client, request, info);
422 *(const char **) info = rc->rc_waitchan;
425 case CLSET_INTERRUPTIBLE:
426 rc->rc_intr = *(int *) info;
428 CLNT_CONTROL(rc->rc_client, request, info);
431 case CLGET_INTERRUPTIBLE:
432 *(int *) info = rc->rc_intr;
436 rc->rc_retries = *(int *) info;
440 *(int *) info = rc->rc_retries;
444 rc->rc_privport = *(int *) info;
448 *(int *) info = rc->rc_privport;
451 case CLSET_BACKCHANNEL:
452 xprt = (SVCXPRT *)info;
454 rc->rc_backchannel = info;
465 clnt_reconnect_close(CLIENT *cl)
467 struct rc_data *rc = (struct rc_data *)cl->cl_private;
470 mtx_lock(&rc->rc_lock);
473 mtx_unlock(&rc->rc_lock);
477 rc->rc_closed = TRUE;
478 client = rc->rc_client;
479 rc->rc_client = NULL;
481 mtx_unlock(&rc->rc_lock);
485 CLNT_RELEASE(client);
490 clnt_reconnect_destroy(CLIENT *cl)
492 struct rc_data *rc = (struct rc_data *)cl->cl_private;
496 CLNT_DESTROY(rc->rc_client);
497 if (rc->rc_backchannel) {
498 xprt = (SVCXPRT *)rc->rc_backchannel;
499 xprt_unregister(xprt);
502 crfree(rc->rc_ucred);
503 mtx_destroy(&rc->rc_lock);
504 mem_free(rc, sizeof(*rc));
505 mem_free(cl, sizeof (CLIENT));