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/limits.h>
35 #include <sys/malloc.h>
37 #include <sys/mutex.h>
40 #include <sys/socket.h>
41 #include <sys/socketvar.h>
46 #include <rpc/rpc_com.h>
48 static enum clnt_stat clnt_reconnect_call(CLIENT *, struct rpc_callextra *,
49 rpcproc_t, xdrproc_t, void *, xdrproc_t, void *, struct timeval);
50 static void clnt_reconnect_geterr(CLIENT *, struct rpc_err *);
51 static bool_t clnt_reconnect_freeres(CLIENT *, xdrproc_t, void *);
52 static void clnt_reconnect_abort(CLIENT *);
53 static bool_t clnt_reconnect_control(CLIENT *, u_int, void *);
54 static void clnt_reconnect_destroy(CLIENT *);
56 static struct clnt_ops clnt_reconnect_ops = {
57 .cl_call = clnt_reconnect_call,
58 .cl_abort = clnt_reconnect_abort,
59 .cl_geterr = clnt_reconnect_geterr,
60 .cl_freeres = clnt_reconnect_freeres,
61 .cl_destroy = clnt_reconnect_destroy,
62 .cl_control = clnt_reconnect_control
67 struct sockaddr_storage rc_addr; /* server address */
68 struct netconfig* rc_nconf; /* network type */
69 rpcprog_t rc_prog; /* program number */
70 rpcvers_t rc_vers; /* version number */
73 struct timeval rc_timeout;
74 struct timeval rc_retry;
76 const char *rc_waitchan;
79 CLIENT* rc_client; /* underlying RPC client */
83 clnt_reconnect_create(
84 struct netconfig *nconf, /* network type */
85 struct sockaddr *svcaddr, /* servers address */
86 rpcprog_t program, /* program number */
87 rpcvers_t version, /* version number */
88 size_t sendsz, /* buffer recv size */
89 size_t recvsz) /* buffer send size */
91 CLIENT *cl = NULL; /* client handle */
92 struct rc_data *rc = NULL; /* private data */
94 if (svcaddr == NULL) {
95 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
99 cl = mem_alloc(sizeof (CLIENT));
100 rc = mem_alloc(sizeof (*rc));
101 mtx_init(&rc->rc_lock, "rc->rc_lock", NULL, MTX_DEF);
102 (void) memcpy(&rc->rc_addr, svcaddr, (size_t)svcaddr->sa_len);
103 rc->rc_nconf = nconf;
104 rc->rc_prog = program;
105 rc->rc_vers = version;
106 rc->rc_sendsz = sendsz;
107 rc->rc_recvsz = recvsz;
108 rc->rc_timeout.tv_sec = -1;
109 rc->rc_timeout.tv_usec = -1;
110 rc->rc_retry.tv_sec = 3;
111 rc->rc_retry.tv_usec = 0;
112 rc->rc_retries = INT_MAX;
113 rc->rc_waitchan = "rpcrecv";
115 rc->rc_connecting = FALSE;
116 rc->rc_client = NULL;
119 cl->cl_ops = &clnt_reconnect_ops;
120 cl->cl_private = (caddr_t)(void *)rc;
121 cl->cl_auth = authnone_create();
127 static enum clnt_stat
128 clnt_reconnect_connect(CLIENT *cl)
130 struct rc_data *rc = (struct rc_data *)cl->cl_private;
136 mtx_lock(&rc->rc_lock);
138 if (rc->rc_connecting) {
139 while (!rc->rc_client) {
140 error = msleep(rc, &rc->rc_lock,
141 rc->rc_intr ? PCATCH : 0, "rpcrecon", 0);
143 mtx_unlock(&rc->rc_lock);
148 * If the other guy failed to connect, we might as
149 * well have another go.
151 if (!rc->rc_client && !rc->rc_connecting)
153 mtx_unlock(&rc->rc_lock);
154 return (RPC_SUCCESS);
156 rc->rc_connecting = TRUE;
158 mtx_unlock(&rc->rc_lock);
160 so = __rpc_nconf2socket(rc->rc_nconf);
162 stat = rpc_createerr.cf_stat = RPC_TLIERROR;
163 rpc_createerr.cf_error.re_errno = 0;
167 if (rc->rc_nconf->nc_semantics == NC_TPI_CLTS)
168 rc->rc_client = clnt_dg_create(so,
169 (struct sockaddr *) &rc->rc_addr, rc->rc_prog, rc->rc_vers,
170 rc->rc_sendsz, rc->rc_recvsz);
172 rc->rc_client = clnt_vc_create(so,
173 (struct sockaddr *) &rc->rc_addr, rc->rc_prog, rc->rc_vers,
174 rc->rc_sendsz, rc->rc_recvsz);
176 if (!rc->rc_client) {
177 stat = rpc_createerr.cf_stat;
181 CLNT_CONTROL(rc->rc_client, CLSET_FD_CLOSE, 0);
182 CLNT_CONTROL(rc->rc_client, CLSET_CONNECT, &one);
183 CLNT_CONTROL(rc->rc_client, CLSET_TIMEOUT, &rc->rc_timeout);
184 CLNT_CONTROL(rc->rc_client, CLSET_RETRY_TIMEOUT, &rc->rc_retry);
185 CLNT_CONTROL(rc->rc_client, CLSET_WAITCHAN, &rc->rc_waitchan);
186 CLNT_CONTROL(rc->rc_client, CLSET_INTERRUPTIBLE, &rc->rc_intr);
190 mtx_lock(&rc->rc_lock);
191 rc->rc_connecting = FALSE;
193 mtx_unlock(&rc->rc_lock);
198 static enum clnt_stat
200 CLIENT *cl, /* client handle */
201 struct rpc_callextra *ext, /* call metadata */
202 rpcproc_t proc, /* procedure number */
203 xdrproc_t xargs, /* xdr routine for args */
204 void *argsp, /* pointer to args */
205 xdrproc_t xresults, /* xdr routine for results */
206 void *resultsp, /* pointer to results */
207 struct timeval utimeout) /* seconds to wait before giving up */
209 struct rc_data *rc = (struct rc_data *)cl->cl_private;
216 if (!rc->rc_client) {
217 stat = clnt_reconnect_connect(cl);
218 if (stat != RPC_SUCCESS)
222 mtx_lock(&rc->rc_lock);
223 CLNT_ACQUIRE(rc->rc_client);
224 client = rc->rc_client;
225 mtx_unlock(&rc->rc_lock);
226 stat = CLNT_CALL_EXT(client, ext, proc, xargs, argsp,
227 xresults, resultsp, utimeout);
229 CLNT_RELEASE(client);
230 if (stat == RPC_TIMEDOUT) {
232 * Check for async send misfeature for NLM
235 if ((rc->rc_timeout.tv_sec == 0
236 && rc->rc_timeout.tv_usec == 0)
237 || (rc->rc_timeout.tv_sec == -1
238 && utimeout.tv_sec == 0
239 && utimeout.tv_usec == 0)) {
244 if (stat == RPC_INTR)
247 if (stat != RPC_SUCCESS) {
249 if (tries >= rc->rc_retries)
252 if (ext && ext->rc_feedback)
253 ext->rc_feedback(FEEDBACK_RECONNECT, proc,
254 ext->rc_feedback_arg);
256 mtx_lock(&rc->rc_lock);
258 * Make sure that someone else hasn't already
261 if (rc->rc_client == client) {
262 CLNT_RELEASE(rc->rc_client);
263 rc->rc_client = NULL;
265 mtx_unlock(&rc->rc_lock);
267 } while (stat != RPC_SUCCESS);
273 clnt_reconnect_geterr(CLIENT *cl, struct rpc_err *errp)
275 struct rc_data *rc = (struct rc_data *)cl->cl_private;
278 CLNT_GETERR(rc->rc_client, errp);
280 memset(errp, 0, sizeof(*errp));
284 clnt_reconnect_freeres(CLIENT *cl, xdrproc_t xdr_res, void *res_ptr)
286 struct rc_data *rc = (struct rc_data *)cl->cl_private;
288 return (CLNT_FREERES(rc->rc_client, xdr_res, res_ptr));
293 clnt_reconnect_abort(CLIENT *h)
298 clnt_reconnect_control(CLIENT *cl, u_int request, void *info)
300 struct rc_data *rc = (struct rc_data *)cl->cl_private;
307 rc->rc_timeout = *(struct timeval *)info;
309 CLNT_CONTROL(rc->rc_client, request, info);
313 *(struct timeval *)info = rc->rc_timeout;
316 case CLSET_RETRY_TIMEOUT:
317 rc->rc_retry = *(struct timeval *)info;
319 CLNT_CONTROL(rc->rc_client, request, info);
322 case CLGET_RETRY_TIMEOUT:
323 *(struct timeval *)info = rc->rc_retry;
327 *(uint32_t *)info = rc->rc_vers;
331 rc->rc_vers = *(uint32_t *) info;
333 CLNT_CONTROL(rc->rc_client, CLSET_VERS, info);
337 *(uint32_t *)info = rc->rc_prog;
341 rc->rc_prog = *(uint32_t *) info;
343 CLNT_CONTROL(rc->rc_client, request, info);
347 rc->rc_waitchan = *(const char **)info;
349 CLNT_CONTROL(rc->rc_client, request, info);
353 *(const char **) info = rc->rc_waitchan;
356 case CLSET_INTERRUPTIBLE:
357 rc->rc_intr = *(int *) info;
359 CLNT_CONTROL(rc->rc_client, request, info);
362 case CLGET_INTERRUPTIBLE:
363 *(int *) info = rc->rc_intr;
367 rc->rc_retries = *(int *) info;
371 *(int *) info = rc->rc_retries;
382 clnt_reconnect_destroy(CLIENT *cl)
384 struct rc_data *rc = (struct rc_data *)cl->cl_private;
387 CLNT_DESTROY(rc->rc_client);
388 mtx_destroy(&rc->rc_lock);
389 mem_free(rc, sizeof(*rc));
390 mem_free(cl, sizeof (CLIENT));