clnt_rc.c revision 178112
1157059Ssam/*- 2157059Ssam * Copyright (c) 2008 Isilon Inc http://www.isilon.com/ 3157059Ssam * Authors: Doug Rabson <dfr@rabson.org> 4157059Ssam * Developed with Red Inc: Alfred Perlstein <alfred@freebsd.org> 5157059Ssam * 6157059Ssam * Redistribution and use in source and binary forms, with or without 7157059Ssam * modification, are permitted provided that the following conditions 8157059Ssam * are met: 9157059Ssam * 1. Redistributions of source code must retain the above copyright 10157059Ssam * notice, this list of conditions and the following disclaimer. 11157059Ssam * 2. Redistributions in binary form must reproduce the above copyright 12157059Ssam * notice, this list of conditions and the following disclaimer in the 13157059Ssam * documentation and/or other materials provided with the distribution. 14157059Ssam * 15157059Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16157059Ssam * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17157059Ssam * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18157059Ssam * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19157059Ssam * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20157059Ssam * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21157059Ssam * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22157059Ssam * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23157059Ssam * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24157059Ssam * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25157059Ssam * SUCH DAMAGE. 26157059Ssam */ 27157059Ssam 28157059Ssam#include <sys/cdefs.h> 29157059Ssam__FBSDID("$FreeBSD: head/sys/rpc/clnt_rc.c 178112 2008-04-11 10:34:59Z dfr $"); 30157059Ssam 31157059Ssam#include <sys/param.h> 32157059Ssam#include <sys/systm.h> 33157059Ssam#include <sys/lock.h> 34157059Ssam#include <sys/malloc.h> 35157059Ssam#include <sys/mbuf.h> 36157059Ssam#include <sys/mutex.h> 37157059Ssam#include <sys/pcpu.h> 38157059Ssam#include <sys/proc.h> 39157059Ssam#include <sys/socket.h> 40157059Ssam#include <sys/socketvar.h> 41157059Ssam#include <sys/time.h> 42157059Ssam#include <sys/uio.h> 43157059Ssam 44157059Ssam#include <rpc/rpc.h> 45157059Ssam#include <rpc/rpc_com.h> 46157059Ssam 47157059Ssamstatic enum clnt_stat clnt_reconnect_call(CLIENT *, rpcproc_t, 48157059Ssam xdrproc_t, void *, xdrproc_t, void *, struct timeval); 49157059Ssamstatic void clnt_reconnect_geterr(CLIENT *, struct rpc_err *); 50157059Ssamstatic bool_t clnt_reconnect_freeres(CLIENT *, xdrproc_t, void *); 51157059Ssamstatic void clnt_reconnect_abort(CLIENT *); 52157059Ssamstatic bool_t clnt_reconnect_control(CLIENT *, u_int, void *); 53157059Ssamstatic void clnt_reconnect_destroy(CLIENT *); 54157059Ssam 55157059Ssamstatic struct clnt_ops clnt_reconnect_ops = { 56157059Ssam .cl_call = clnt_reconnect_call, 57157059Ssam .cl_abort = clnt_reconnect_abort, 58157059Ssam .cl_geterr = clnt_reconnect_geterr, 59157059Ssam .cl_freeres = clnt_reconnect_freeres, 60157059Ssam .cl_destroy = clnt_reconnect_destroy, 61267992Shselasky .cl_control = clnt_reconnect_control 62267992Shselasky}; 63157059Ssam 64157059Ssamstruct rc_data { 65157059Ssam struct sockaddr_storage rc_addr; /* server address */ 66157059Ssam struct netconfig* rc_nconf; /* network type */ 67157059Ssam rpcprog_t rc_prog; /* program number */ 68157059Ssam rpcvers_t rc_vers; /* version number */ 69157059Ssam size_t rc_sendsz; 70157059Ssam size_t rc_recvsz; 71157059Ssam struct timeval rc_timeout; 72157059Ssam struct timeval rc_retry; 73157059Ssam const char *rc_waitchan; 74157059Ssam int rc_intr; 75157059Ssam CLIENT* rc_client; /* underlying RPC client */ 76157059Ssam}; 77157059Ssam 78157059SsamCLIENT * 79283291Sjkimclnt_reconnect_create( 80157059Ssam struct netconfig *nconf, /* network type */ 81157059Ssam struct sockaddr *svcaddr, /* servers address */ 82157059Ssam rpcprog_t program, /* program number */ 83157059Ssam rpcvers_t version, /* version number */ 84158960Sphk size_t sendsz, /* buffer recv size */ 85158960Sphk size_t recvsz) /* buffer send size */ 86158960Sphk{ 87158960Sphk CLIENT *cl = NULL; /* client handle */ 88158960Sphk struct rc_data *rc = NULL; /* private data */ 89228631Savg 90228631Savg if (svcaddr == NULL) { 91228631Savg rpc_createerr.cf_stat = RPC_UNKNOWNADDR; 92228631Savg return (NULL); 93228631Savg } 94228631Savg 95228631Savg cl = mem_alloc(sizeof (CLIENT)); 96228631Savg rc = mem_alloc(sizeof (*rc)); 97228631Savg (void) memcpy(&rc->rc_addr, svcaddr, (size_t)svcaddr->sa_len); 98228631Savg rc->rc_nconf = nconf; 99157059Ssam rc->rc_prog = program; 100158960Sphk rc->rc_vers = version; 101157059Ssam rc->rc_sendsz = sendsz; 102157059Ssam rc->rc_recvsz = recvsz; 103157059Ssam rc->rc_timeout.tv_sec = -1; 104157059Ssam rc->rc_timeout.tv_usec = -1; 105157059Ssam rc->rc_retry.tv_sec = 15; 106157059Ssam rc->rc_retry.tv_usec = 0; 107157059Ssam rc->rc_waitchan = "rpcrecv"; 108157059Ssam rc->rc_intr = 0; 109157059Ssam rc->rc_client = NULL; 110157059Ssam 111157059Ssam cl->cl_ops = &clnt_reconnect_ops; 112157059Ssam cl->cl_private = (caddr_t)(void *)rc; 113157059Ssam cl->cl_auth = authnone_create(); 114157059Ssam cl->cl_tp = NULL; 115157059Ssam cl->cl_netid = NULL; 116157059Ssam return (cl); 117157059Ssam} 118157059Ssam 119157059Ssamstatic enum clnt_stat 120157059Ssamclnt_reconnect_connect(CLIENT *cl) 121157059Ssam{ 122157059Ssam struct rc_data *rc = (struct rc_data *)cl->cl_private; 123157059Ssam struct socket *so; 124157059Ssam int one = 1; 125157059Ssam 126157059Ssam so = __rpc_nconf2socket(rc->rc_nconf); 127157059Ssam if (!so) { 128157059Ssam rpc_createerr.cf_stat = RPC_TLIERROR; 129157059Ssam rpc_createerr.cf_error.re_errno = 0; 130157059Ssam return (RPC_TLIERROR); 131157059Ssam } 132157059Ssam 133157059Ssam if (rc->rc_nconf->nc_semantics == NC_TPI_CLTS) 134157059Ssam rc->rc_client = clnt_dg_create(so, 135157059Ssam (struct sockaddr *) &rc->rc_addr, rc->rc_prog, rc->rc_vers, 136157059Ssam rc->rc_sendsz, rc->rc_recvsz); 137157059Ssam else 138253604Savg rc->rc_client = clnt_vc_create(so, 139157059Ssam (struct sockaddr *) &rc->rc_addr, rc->rc_prog, rc->rc_vers, 140157059Ssam rc->rc_sendsz, rc->rc_recvsz); 141157059Ssam 142157059Ssam if (!rc->rc_client) 143157059Ssam return (rpc_createerr.cf_stat); 144157059Ssam 145157059Ssam CLNT_CONTROL(rc->rc_client, CLSET_FD_CLOSE, 0); 146157059Ssam CLNT_CONTROL(rc->rc_client, CLSET_CONNECT, &one); 147157059Ssam CLNT_CONTROL(rc->rc_client, CLSET_TIMEOUT, &rc->rc_timeout); 148157059Ssam CLNT_CONTROL(rc->rc_client, CLSET_RETRY_TIMEOUT, &rc->rc_retry); 149157059Ssam CLNT_CONTROL(rc->rc_client, CLSET_WAITCHAN, &rc->rc_waitchan); 150157059Ssam CLNT_CONTROL(rc->rc_client, CLSET_INTERRUPTIBLE, &rc->rc_intr); 151157059Ssam 152157059Ssam return (RPC_SUCCESS); 153157059Ssam} 154157059Ssam 155157059Ssamstatic enum clnt_stat 156157059Ssamclnt_reconnect_call( 157157059Ssam CLIENT *cl, /* client handle */ 158157059Ssam rpcproc_t proc, /* procedure number */ 159157059Ssam xdrproc_t xargs, /* xdr routine for args */ 160157059Ssam void *argsp, /* pointer to args */ 161157059Ssam xdrproc_t xresults, /* xdr routine for results */ 162157059Ssam void *resultsp, /* pointer to results */ 163157059Ssam struct timeval utimeout) /* seconds to wait before giving up */ 164157059Ssam{ 165157059Ssam struct rc_data *rc = (struct rc_data *)cl->cl_private; 166157059Ssam enum clnt_stat stat; 167157059Ssam 168157059Ssam do { 169157059Ssam if (!rc->rc_client) { 170157059Ssam stat = clnt_reconnect_connect(cl); 171158960Sphk if (stat != RPC_SUCCESS) 172157059Ssam return (stat); 173157059Ssam } 174157059Ssam 175157059Ssam stat = CLNT_CALL(rc->rc_client, proc, xargs, argsp, 176157059Ssam xresults, resultsp, utimeout); 177157059Ssam 178157059Ssam if (stat == RPC_TIMEDOUT) { 179157059Ssam /* 180157059Ssam * Check for async send misfeature for NLM 181157059Ssam * protocol. 182157059Ssam */ 183157059Ssam if ((rc->rc_timeout.tv_sec == 0 184157059Ssam && rc->rc_timeout.tv_usec == 0) 185 || (rc->rc_timeout.tv_sec == -1 186 && utimeout.tv_sec == 0 187 && utimeout.tv_usec == 0)) 188 break; 189 } 190 191 if (stat == RPC_INTR) 192 break; 193 194 if (stat != RPC_SUCCESS) { 195 CLNT_DESTROY(rc->rc_client); 196 rc->rc_client = NULL; 197 } 198 } while (stat != RPC_SUCCESS); 199 200 return (stat); 201} 202 203static void 204clnt_reconnect_geterr(CLIENT *cl, struct rpc_err *errp) 205{ 206 struct rc_data *rc = (struct rc_data *)cl->cl_private; 207 208 if (rc->rc_client) 209 CLNT_GETERR(rc->rc_client, errp); 210 else 211 memset(errp, 0, sizeof(*errp)); 212} 213 214static bool_t 215clnt_reconnect_freeres(CLIENT *cl, xdrproc_t xdr_res, void *res_ptr) 216{ 217 struct rc_data *rc = (struct rc_data *)cl->cl_private; 218 219 return (CLNT_FREERES(rc->rc_client, xdr_res, res_ptr)); 220} 221 222/*ARGSUSED*/ 223static void 224clnt_reconnect_abort(CLIENT *h) 225{ 226} 227 228static bool_t 229clnt_reconnect_control(CLIENT *cl, u_int request, void *info) 230{ 231 struct rc_data *rc = (struct rc_data *)cl->cl_private; 232 233 if (info == NULL) { 234 return (FALSE); 235 } 236 switch (request) { 237 case CLSET_TIMEOUT: 238 rc->rc_timeout = *(struct timeval *)info; 239 if (rc->rc_client) 240 CLNT_CONTROL(rc->rc_client, request, info); 241 break; 242 243 case CLGET_TIMEOUT: 244 *(struct timeval *)info = rc->rc_timeout; 245 break; 246 247 case CLSET_RETRY_TIMEOUT: 248 rc->rc_retry = *(struct timeval *)info; 249 if (rc->rc_client) 250 CLNT_CONTROL(rc->rc_client, request, info); 251 break; 252 253 case CLGET_RETRY_TIMEOUT: 254 *(struct timeval *)info = rc->rc_retry; 255 break; 256 257 case CLGET_VERS: 258 *(uint32_t *)info = rc->rc_vers; 259 break; 260 261 case CLSET_VERS: 262 rc->rc_vers = *(uint32_t *) info; 263 if (rc->rc_client) 264 CLNT_CONTROL(rc->rc_client, CLSET_VERS, info); 265 break; 266 267 case CLGET_PROG: 268 *(uint32_t *)info = rc->rc_prog; 269 break; 270 271 case CLSET_PROG: 272 rc->rc_prog = *(uint32_t *) info; 273 if (rc->rc_client) 274 CLNT_CONTROL(rc->rc_client, request, info); 275 break; 276 277 case CLSET_WAITCHAN: 278 rc->rc_waitchan = *(const char **)info; 279 if (rc->rc_client) 280 CLNT_CONTROL(rc->rc_client, request, info); 281 break; 282 283 case CLGET_WAITCHAN: 284 *(const char **) info = rc->rc_waitchan; 285 break; 286 287 case CLSET_INTERRUPTIBLE: 288 rc->rc_intr = *(int *) info; 289 if (rc->rc_client) 290 CLNT_CONTROL(rc->rc_client, request, info); 291 break; 292 293 case CLGET_INTERRUPTIBLE: 294 *(int *) info = rc->rc_intr; 295 break; 296 297 default: 298 return (FALSE); 299 } 300 301 return (TRUE); 302} 303 304static void 305clnt_reconnect_destroy(CLIENT *cl) 306{ 307 struct rc_data *rc = (struct rc_data *)cl->cl_private; 308 309 if (rc->rc_client) 310 CLNT_DESTROY(rc->rc_client); 311 mem_free(rc, sizeof(*rc)); 312 mem_free(cl, sizeof (CLIENT)); 313} 314