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>
49 static enum clnt_stat clnt_reconnect_call(CLIENT *, struct rpc_callextra *,
50 rpcproc_t, struct mbuf *, struct mbuf **, struct timeval);
51 static void clnt_reconnect_geterr(CLIENT *, struct rpc_err *);
52 static bool_t clnt_reconnect_freeres(CLIENT *, xdrproc_t, void *);
53 static void clnt_reconnect_abort(CLIENT *);
54 static bool_t clnt_reconnect_control(CLIENT *, u_int, void *);
55 static void clnt_reconnect_close(CLIENT *);
56 static void clnt_reconnect_destroy(CLIENT *);
58 static struct clnt_ops clnt_reconnect_ops = {
59 .cl_call = clnt_reconnect_call,
60 .cl_abort = clnt_reconnect_abort,
61 .cl_geterr = clnt_reconnect_geterr,
62 .cl_freeres = clnt_reconnect_freeres,
63 .cl_close = clnt_reconnect_close,
64 .cl_destroy = clnt_reconnect_destroy,
65 .cl_control = clnt_reconnect_control
68 static int fake_wchan;
72 struct sockaddr_storage rc_addr; /* server address */
73 struct netconfig* rc_nconf; /* network type */
74 rpcprog_t rc_prog; /* program number */
75 rpcvers_t rc_vers; /* version number */
78 struct timeval rc_timeout;
79 struct timeval rc_retry;
86 struct ucred *rc_ucred;
87 CLIENT* rc_client; /* underlying RPC client */
88 struct rpc_err rc_err;
92 clnt_reconnect_create(
93 struct netconfig *nconf, /* network type */
94 struct sockaddr *svcaddr, /* servers address */
95 rpcprog_t program, /* program number */
96 rpcvers_t version, /* version number */
97 size_t sendsz, /* buffer recv size */
98 size_t recvsz) /* buffer send size */
100 CLIENT *cl = NULL; /* client handle */
101 struct rc_data *rc = NULL; /* private data */
103 if (svcaddr == NULL) {
104 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
108 cl = mem_alloc(sizeof (CLIENT));
109 rc = mem_alloc(sizeof (*rc));
110 mtx_init(&rc->rc_lock, "rc->rc_lock", NULL, MTX_DEF);
111 (void) memcpy(&rc->rc_addr, svcaddr, (size_t)svcaddr->sa_len);
112 rc->rc_nconf = nconf;
113 rc->rc_prog = program;
114 rc->rc_vers = version;
115 rc->rc_sendsz = sendsz;
116 rc->rc_recvsz = recvsz;
117 rc->rc_timeout.tv_sec = -1;
118 rc->rc_timeout.tv_usec = -1;
119 rc->rc_retry.tv_sec = 3;
120 rc->rc_retry.tv_usec = 0;
121 rc->rc_retries = INT_MAX;
122 rc->rc_privport = FALSE;
123 rc->rc_waitchan = "rpcrecv";
125 rc->rc_connecting = FALSE;
126 rc->rc_closed = FALSE;
127 rc->rc_ucred = crdup(curthread->td_ucred);
128 rc->rc_client = NULL;
131 cl->cl_ops = &clnt_reconnect_ops;
132 cl->cl_private = (caddr_t)(void *)rc;
133 cl->cl_auth = authnone_create();
139 static enum clnt_stat
140 clnt_reconnect_connect(CLIENT *cl)
142 struct thread *td = curthread;
143 struct rc_data *rc = (struct rc_data *)cl->cl_private;
148 struct ucred *oldcred;
149 CLIENT *newclient = NULL;
151 mtx_lock(&rc->rc_lock);
152 while (rc->rc_connecting) {
153 error = msleep(rc, &rc->rc_lock,
154 rc->rc_intr ? PCATCH : 0, "rpcrecon", 0);
156 mtx_unlock(&rc->rc_lock);
161 mtx_unlock(&rc->rc_lock);
162 return (RPC_CANTSEND);
165 mtx_unlock(&rc->rc_lock);
166 return (RPC_SUCCESS);
170 * My turn to attempt a connect. The rc_connecting variable
171 * serializes the following code sequence, so it is guaranteed
172 * that rc_client will still be NULL after it is re-locked below,
173 * since that is the only place it is set non-NULL.
175 rc->rc_connecting = TRUE;
176 mtx_unlock(&rc->rc_lock);
178 so = __rpc_nconf2socket(rc->rc_nconf);
180 stat = rpc_createerr.cf_stat = RPC_TLIERROR;
181 rpc_createerr.cf_error.re_errno = 0;
185 oldcred = td->td_ucred;
186 td->td_ucred = rc->rc_ucred;
188 bindresvport(so, NULL);
190 if (rc->rc_nconf->nc_semantics == NC_TPI_CLTS)
191 newclient = clnt_dg_create(so,
192 (struct sockaddr *) &rc->rc_addr, rc->rc_prog, rc->rc_vers,
193 rc->rc_sendsz, rc->rc_recvsz);
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);
198 td->td_ucred = oldcred;
202 rc->rc_err = rpc_createerr.cf_error;
203 stat = rpc_createerr.cf_stat;
207 CLNT_CONTROL(newclient, CLSET_FD_CLOSE, 0);
208 CLNT_CONTROL(newclient, CLSET_CONNECT, &one);
209 CLNT_CONTROL(newclient, CLSET_TIMEOUT, &rc->rc_timeout);
210 CLNT_CONTROL(newclient, CLSET_RETRY_TIMEOUT, &rc->rc_retry);
211 CLNT_CONTROL(newclient, CLSET_WAITCHAN, rc->rc_waitchan);
212 CLNT_CONTROL(newclient, CLSET_INTERRUPTIBLE, &rc->rc_intr);
216 mtx_lock(&rc->rc_lock);
217 KASSERT(rc->rc_client == NULL, ("rc_client not null"));
218 if (!rc->rc_closed) {
219 rc->rc_client = newclient;
222 rc->rc_connecting = FALSE;
224 mtx_unlock(&rc->rc_lock);
228 * It has been closed, so discard the new client.
229 * nb: clnt_[dg|vc]_close()/clnt_[dg|vc]_destroy() cannot
230 * be called with the rc_lock mutex held, since they may
231 * msleep() while holding a different mutex.
233 CLNT_CLOSE(newclient);
234 CLNT_RELEASE(newclient);
240 static enum clnt_stat
242 CLIENT *cl, /* client handle */
243 struct rpc_callextra *ext, /* call metadata */
244 rpcproc_t proc, /* procedure number */
245 struct mbuf *args, /* pointer to args */
246 struct mbuf **resultsp, /* pointer to results */
247 struct timeval utimeout)
249 struct rc_data *rc = (struct rc_data *)cl->cl_private;
256 mtx_lock(&rc->rc_lock);
258 mtx_unlock(&rc->rc_lock);
259 return (RPC_CANTSEND);
262 if (!rc->rc_client) {
263 mtx_unlock(&rc->rc_lock);
264 stat = clnt_reconnect_connect(cl);
265 if (stat == RPC_SYSTEMERROR) {
266 error = tsleep(&fake_wchan,
267 rc->rc_intr ? PCATCH | PBDRY : 0, "rpccon",
269 if (error == EINTR || error == ERESTART)
272 if (tries >= rc->rc_retries)
276 if (stat != RPC_SUCCESS)
278 mtx_lock(&rc->rc_lock);
281 if (!rc->rc_client) {
282 mtx_unlock(&rc->rc_lock);
286 CLNT_ACQUIRE(rc->rc_client);
287 client = rc->rc_client;
288 mtx_unlock(&rc->rc_lock);
289 stat = CLNT_CALL_MBUF(client, ext, proc, args,
292 if (stat != RPC_SUCCESS) {
294 CLNT_GETERR(client, &rc->rc_err);
297 if (stat == RPC_TIMEDOUT) {
299 * Check for async send misfeature for NLM
302 if ((rc->rc_timeout.tv_sec == 0
303 && rc->rc_timeout.tv_usec == 0)
304 || (rc->rc_timeout.tv_sec == -1
305 && utimeout.tv_sec == 0
306 && utimeout.tv_usec == 0)) {
307 CLNT_RELEASE(client);
312 if (stat == RPC_TIMEDOUT || stat == RPC_CANTSEND
313 || stat == RPC_CANTRECV) {
315 if (tries >= rc->rc_retries) {
316 CLNT_RELEASE(client);
320 if (ext && ext->rc_feedback)
321 ext->rc_feedback(FEEDBACK_RECONNECT, proc,
322 ext->rc_feedback_arg);
324 mtx_lock(&rc->rc_lock);
326 * Make sure that someone else hasn't already
327 * reconnected by checking if rc_client has changed.
328 * If not, we are done with the client and must
329 * do CLNT_RELEASE(client) twice to dispose of it,
330 * because there is both an initial refcnt and one
331 * acquired by CLNT_ACQUIRE() above.
333 if (rc->rc_client == client) {
334 rc->rc_client = NULL;
335 mtx_unlock(&rc->rc_lock);
336 CLNT_RELEASE(client);
338 mtx_unlock(&rc->rc_lock);
340 CLNT_RELEASE(client);
342 CLNT_RELEASE(client);
345 } while (stat != RPC_SUCCESS);
347 KASSERT(stat != RPC_SUCCESS || *resultsp,
348 ("RPC_SUCCESS without reply"));
354 clnt_reconnect_geterr(CLIENT *cl, struct rpc_err *errp)
356 struct rc_data *rc = (struct rc_data *)cl->cl_private;
362 * Since this function requires that rc_client be valid, it can
363 * only be called when that is guaranteed to be the case.
366 clnt_reconnect_freeres(CLIENT *cl, xdrproc_t xdr_res, void *res_ptr)
368 struct rc_data *rc = (struct rc_data *)cl->cl_private;
370 return (CLNT_FREERES(rc->rc_client, xdr_res, res_ptr));
375 clnt_reconnect_abort(CLIENT *h)
380 * CLNT_CONTROL() on the client returned by clnt_reconnect_create() must
381 * always be called before CLNT_CALL_MBUF() by a single thread only.
384 clnt_reconnect_control(CLIENT *cl, u_int request, void *info)
386 struct rc_data *rc = (struct rc_data *)cl->cl_private;
393 rc->rc_timeout = *(struct timeval *)info;
395 CLNT_CONTROL(rc->rc_client, request, info);
399 *(struct timeval *)info = rc->rc_timeout;
402 case CLSET_RETRY_TIMEOUT:
403 rc->rc_retry = *(struct timeval *)info;
405 CLNT_CONTROL(rc->rc_client, request, info);
408 case CLGET_RETRY_TIMEOUT:
409 *(struct timeval *)info = rc->rc_retry;
413 *(uint32_t *)info = rc->rc_vers;
417 rc->rc_vers = *(uint32_t *) info;
419 CLNT_CONTROL(rc->rc_client, CLSET_VERS, info);
423 *(uint32_t *)info = rc->rc_prog;
427 rc->rc_prog = *(uint32_t *) info;
429 CLNT_CONTROL(rc->rc_client, request, info);
433 rc->rc_waitchan = (char *)info;
435 CLNT_CONTROL(rc->rc_client, request, info);
439 *(const char **) info = rc->rc_waitchan;
442 case CLSET_INTERRUPTIBLE:
443 rc->rc_intr = *(int *) info;
445 CLNT_CONTROL(rc->rc_client, request, info);
448 case CLGET_INTERRUPTIBLE:
449 *(int *) info = rc->rc_intr;
453 rc->rc_retries = *(int *) info;
457 *(int *) info = rc->rc_retries;
461 rc->rc_privport = *(int *) info;
465 *(int *) info = rc->rc_privport;
476 clnt_reconnect_close(CLIENT *cl)
478 struct rc_data *rc = (struct rc_data *)cl->cl_private;
481 mtx_lock(&rc->rc_lock);
484 mtx_unlock(&rc->rc_lock);
488 rc->rc_closed = TRUE;
489 client = rc->rc_client;
490 rc->rc_client = NULL;
492 mtx_unlock(&rc->rc_lock);
496 CLNT_RELEASE(client);
501 clnt_reconnect_destroy(CLIENT *cl)
503 struct rc_data *rc = (struct rc_data *)cl->cl_private;
506 CLNT_DESTROY(rc->rc_client);
507 crfree(rc->rc_ucred);
508 mtx_destroy(&rc->rc_lock);
509 mem_free(rc, sizeof(*rc));
510 mem_free(cl, sizeof (CLIENT));