clnt_dg.c revision 180025
1177633Sdfr/* $NetBSD: clnt_dg.c,v 1.4 2000/07/14 08:40:41 fvdl Exp $ */ 2177633Sdfr 3177633Sdfr/* 4177633Sdfr * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 5177633Sdfr * unrestricted use provided that this legend is included on all tape 6177633Sdfr * media and as a part of the software program in whole or part. Users 7177633Sdfr * may copy or modify Sun RPC without charge, but are not authorized 8177633Sdfr * to license or distribute it to anyone else except as part of a product or 9177633Sdfr * program developed by the user. 10177633Sdfr * 11177633Sdfr * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 12177633Sdfr * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 13177633Sdfr * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 14177633Sdfr * 15177633Sdfr * Sun RPC is provided with no support and without any obligation on the 16177633Sdfr * part of Sun Microsystems, Inc. to assist in its use, correction, 17177633Sdfr * modification or enhancement. 18177633Sdfr * 19177633Sdfr * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 20177633Sdfr * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 21177633Sdfr * OR ANY PART THEREOF. 22177633Sdfr * 23177633Sdfr * In no event will Sun Microsystems, Inc. be liable for any lost revenue 24177633Sdfr * or profits or other special, indirect and consequential damages, even if 25177633Sdfr * Sun has been advised of the possibility of such damages. 26177633Sdfr * 27177633Sdfr * Sun Microsystems, Inc. 28177633Sdfr * 2550 Garcia Avenue 29177633Sdfr * Mountain View, California 94043 30177633Sdfr */ 31177633Sdfr/* 32177633Sdfr * Copyright (c) 1986-1991 by Sun Microsystems Inc. 33177633Sdfr */ 34177633Sdfr 35177633Sdfr#if defined(LIBC_SCCS) && !defined(lint) 36177633Sdfr#ident "@(#)clnt_dg.c 1.23 94/04/22 SMI" 37177633Sdfrstatic char sccsid[] = "@(#)clnt_dg.c 1.19 89/03/16 Copyr 1988 Sun Micro"; 38177633Sdfr#endif 39177633Sdfr#include <sys/cdefs.h> 40177633Sdfr__FBSDID("$FreeBSD: head/sys/rpc/clnt_dg.c 180025 2008-06-26 10:21:54Z dfr $"); 41177633Sdfr 42177633Sdfr/* 43177633Sdfr * Implements a connectionless client side RPC. 44177633Sdfr */ 45177633Sdfr 46177633Sdfr#include <sys/param.h> 47177633Sdfr#include <sys/systm.h> 48180025Sdfr#include <sys/kernel.h> 49177633Sdfr#include <sys/lock.h> 50177633Sdfr#include <sys/malloc.h> 51177633Sdfr#include <sys/mbuf.h> 52177633Sdfr#include <sys/mutex.h> 53177633Sdfr#include <sys/pcpu.h> 54177633Sdfr#include <sys/proc.h> 55177633Sdfr#include <sys/socket.h> 56177633Sdfr#include <sys/socketvar.h> 57177633Sdfr#include <sys/time.h> 58177633Sdfr#include <sys/uio.h> 59177633Sdfr 60177633Sdfr#include <rpc/rpc.h> 61177685Sdfr#include <rpc/rpc_com.h> 62177633Sdfr 63177633Sdfr 64177633Sdfr#ifdef _FREEFALL_CONFIG 65177633Sdfr/* 66177633Sdfr * Disable RPC exponential back-off for FreeBSD.org systems. 67177633Sdfr */ 68177633Sdfr#define RPC_MAX_BACKOFF 1 /* second */ 69177633Sdfr#else 70177633Sdfr#define RPC_MAX_BACKOFF 30 /* seconds */ 71177633Sdfr#endif 72177633Sdfr 73177633Sdfrstatic bool_t time_not_ok(struct timeval *); 74180025Sdfrstatic enum clnt_stat clnt_dg_call(CLIENT *, struct rpc_callextra *, 75180025Sdfr rpcproc_t, xdrproc_t, void *, xdrproc_t, void *, struct timeval); 76177633Sdfrstatic void clnt_dg_geterr(CLIENT *, struct rpc_err *); 77177633Sdfrstatic bool_t clnt_dg_freeres(CLIENT *, xdrproc_t, void *); 78177633Sdfrstatic void clnt_dg_abort(CLIENT *); 79177633Sdfrstatic bool_t clnt_dg_control(CLIENT *, u_int, void *); 80177633Sdfrstatic void clnt_dg_destroy(CLIENT *); 81177633Sdfrstatic void clnt_dg_soupcall(struct socket *so, void *arg, int waitflag); 82177633Sdfr 83177633Sdfrstatic struct clnt_ops clnt_dg_ops = { 84177633Sdfr .cl_call = clnt_dg_call, 85177633Sdfr .cl_abort = clnt_dg_abort, 86177633Sdfr .cl_geterr = clnt_dg_geterr, 87177633Sdfr .cl_freeres = clnt_dg_freeres, 88177633Sdfr .cl_destroy = clnt_dg_destroy, 89177633Sdfr .cl_control = clnt_dg_control 90177633Sdfr}; 91177633Sdfr 92177633Sdfrstatic const char mem_err_clnt_dg[] = "clnt_dg_create: out of memory"; 93177633Sdfr 94177633Sdfr/* 95180025Sdfr * A pending RPC request which awaits a reply. Requests which have 96180025Sdfr * received their reply will have cr_xid set to zero and cr_mrep to 97180025Sdfr * the mbuf chain of the reply. 98177633Sdfr */ 99177633Sdfrstruct cu_request { 100177633Sdfr TAILQ_ENTRY(cu_request) cr_link; 101180025Sdfr CLIENT *cr_client; /* owner */ 102177633Sdfr uint32_t cr_xid; /* XID of request */ 103177633Sdfr struct mbuf *cr_mrep; /* reply received by upcall */ 104177633Sdfr int cr_error; /* any error from upcall */ 105177633Sdfr}; 106177633Sdfr 107177633SdfrTAILQ_HEAD(cu_request_list, cu_request); 108177633Sdfr 109177633Sdfr#define MCALL_MSG_SIZE 24 110177633Sdfr 111177633Sdfr/* 112177633Sdfr * This structure is pointed to by the socket's so_upcallarg 113177633Sdfr * member. It is separate from the client private data to facilitate 114177633Sdfr * multiple clients sharing the same socket. The cs_lock mutex is used 115177633Sdfr * to protect all fields of this structure, the socket's receive 116177633Sdfr * buffer SOCKBUF_LOCK is used to ensure that exactly one of these 117177633Sdfr * structures is installed on the socket. 118177633Sdfr */ 119177633Sdfrstruct cu_socket { 120177633Sdfr struct mtx cs_lock; 121177633Sdfr int cs_refs; /* Count of clients */ 122177633Sdfr struct cu_request_list cs_pending; /* Requests awaiting replies */ 123177633Sdfr 124177633Sdfr}; 125177633Sdfr 126177633Sdfr/* 127177633Sdfr * Private data kept per client handle 128177633Sdfr */ 129177633Sdfrstruct cu_data { 130180025Sdfr int cu_threads; /* # threads in clnt_vc_call */ 131180025Sdfr bool_t cu_closing; /* TRUE if we are destroying */ 132177633Sdfr struct socket *cu_socket; /* connection socket */ 133177633Sdfr bool_t cu_closeit; /* opened by library */ 134177633Sdfr struct sockaddr_storage cu_raddr; /* remote address */ 135177633Sdfr int cu_rlen; 136177633Sdfr struct timeval cu_wait; /* retransmit interval */ 137177633Sdfr struct timeval cu_total; /* total time for the call */ 138177633Sdfr struct rpc_err cu_error; 139177633Sdfr uint32_t cu_xid; 140177633Sdfr char cu_mcallc[MCALL_MSG_SIZE]; /* marshalled callmsg */ 141177633Sdfr size_t cu_mcalllen; 142177633Sdfr size_t cu_sendsz; /* send size */ 143177633Sdfr size_t cu_recvsz; /* recv size */ 144177633Sdfr int cu_async; 145177633Sdfr int cu_connect; /* Use connect(). */ 146177633Sdfr int cu_connected; /* Have done connect(). */ 147177633Sdfr const char *cu_waitchan; 148177633Sdfr int cu_waitflag; 149177633Sdfr}; 150177633Sdfr 151177633Sdfr/* 152177633Sdfr * Connection less client creation returns with client handle parameters. 153177633Sdfr * Default options are set, which the user can change using clnt_control(). 154177633Sdfr * fd should be open and bound. 155177633Sdfr * NB: The rpch->cl_auth is initialized to null authentication. 156177633Sdfr * Caller may wish to set this something more useful. 157177633Sdfr * 158177633Sdfr * sendsz and recvsz are the maximum allowable packet sizes that can be 159177633Sdfr * sent and received. Normally they are the same, but they can be 160177633Sdfr * changed to improve the program efficiency and buffer allocation. 161177633Sdfr * If they are 0, use the transport default. 162177633Sdfr * 163177633Sdfr * If svcaddr is NULL, returns NULL. 164177633Sdfr */ 165177633SdfrCLIENT * 166177633Sdfrclnt_dg_create( 167177633Sdfr struct socket *so, 168177633Sdfr struct sockaddr *svcaddr, /* servers address */ 169177633Sdfr rpcprog_t program, /* program number */ 170177633Sdfr rpcvers_t version, /* version number */ 171177633Sdfr size_t sendsz, /* buffer recv size */ 172177633Sdfr size_t recvsz) /* buffer send size */ 173177633Sdfr{ 174177633Sdfr CLIENT *cl = NULL; /* client handle */ 175177633Sdfr struct cu_data *cu = NULL; /* private data */ 176177633Sdfr struct cu_socket *cs = NULL; 177177633Sdfr struct timeval now; 178177633Sdfr struct rpc_msg call_msg; 179177633Sdfr struct __rpc_sockinfo si; 180177633Sdfr XDR xdrs; 181177633Sdfr 182177633Sdfr if (svcaddr == NULL) { 183177633Sdfr rpc_createerr.cf_stat = RPC_UNKNOWNADDR; 184177633Sdfr return (NULL); 185177633Sdfr } 186177633Sdfr 187177633Sdfr if (!__rpc_socket2sockinfo(so, &si)) { 188177633Sdfr rpc_createerr.cf_stat = RPC_TLIERROR; 189177633Sdfr rpc_createerr.cf_error.re_errno = 0; 190177633Sdfr return (NULL); 191177633Sdfr } 192177633Sdfr 193177633Sdfr /* 194177633Sdfr * Find the receive and the send size 195177633Sdfr */ 196177633Sdfr sendsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsz); 197177633Sdfr recvsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsz); 198177633Sdfr if ((sendsz == 0) || (recvsz == 0)) { 199177633Sdfr rpc_createerr.cf_stat = RPC_TLIERROR; /* XXX */ 200177633Sdfr rpc_createerr.cf_error.re_errno = 0; 201177633Sdfr return (NULL); 202177633Sdfr } 203177633Sdfr 204177633Sdfr cl = mem_alloc(sizeof (CLIENT)); 205177633Sdfr 206177633Sdfr /* 207177633Sdfr * Should be multiple of 4 for XDR. 208177633Sdfr */ 209177633Sdfr sendsz = ((sendsz + 3) / 4) * 4; 210177633Sdfr recvsz = ((recvsz + 3) / 4) * 4; 211177633Sdfr cu = mem_alloc(sizeof (*cu)); 212180025Sdfr cu->cu_threads = 0; 213180025Sdfr cu->cu_closing = FALSE; 214177633Sdfr (void) memcpy(&cu->cu_raddr, svcaddr, (size_t)svcaddr->sa_len); 215177633Sdfr cu->cu_rlen = svcaddr->sa_len; 216177633Sdfr /* Other values can also be set through clnt_control() */ 217180025Sdfr cu->cu_wait.tv_sec = 3; /* heuristically chosen */ 218177633Sdfr cu->cu_wait.tv_usec = 0; 219177633Sdfr cu->cu_total.tv_sec = -1; 220177633Sdfr cu->cu_total.tv_usec = -1; 221177633Sdfr cu->cu_sendsz = sendsz; 222177633Sdfr cu->cu_recvsz = recvsz; 223177633Sdfr cu->cu_async = FALSE; 224177633Sdfr cu->cu_connect = FALSE; 225177633Sdfr cu->cu_connected = FALSE; 226177633Sdfr cu->cu_waitchan = "rpcrecv"; 227177633Sdfr cu->cu_waitflag = 0; 228177633Sdfr (void) getmicrotime(&now); 229177633Sdfr cu->cu_xid = __RPC_GETXID(&now); 230177633Sdfr call_msg.rm_xid = cu->cu_xid; 231177633Sdfr call_msg.rm_call.cb_prog = program; 232177633Sdfr call_msg.rm_call.cb_vers = version; 233177633Sdfr xdrmem_create(&xdrs, cu->cu_mcallc, MCALL_MSG_SIZE, XDR_ENCODE); 234177633Sdfr if (! xdr_callhdr(&xdrs, &call_msg)) { 235177633Sdfr rpc_createerr.cf_stat = RPC_CANTENCODEARGS; /* XXX */ 236177633Sdfr rpc_createerr.cf_error.re_errno = 0; 237177633Sdfr goto err2; 238177633Sdfr } 239177633Sdfr cu->cu_mcalllen = XDR_GETPOS(&xdrs);; 240177633Sdfr 241177633Sdfr /* 242177633Sdfr * By default, closeit is always FALSE. It is users responsibility 243177633Sdfr * to do a close on it, else the user may use clnt_control 244177633Sdfr * to let clnt_destroy do it for him/her. 245177633Sdfr */ 246177633Sdfr cu->cu_closeit = FALSE; 247177633Sdfr cu->cu_socket = so; 248180025Sdfr soreserve(so, 256*1024, 256*1024); 249177633Sdfr 250177633Sdfr SOCKBUF_LOCK(&so->so_rcv); 251177633Sdfrrecheck_socket: 252177633Sdfr if (so->so_upcall) { 253177633Sdfr if (so->so_upcall != clnt_dg_soupcall) { 254177633Sdfr SOCKBUF_UNLOCK(&so->so_rcv); 255177633Sdfr printf("clnt_dg_create(): socket already has an incompatible upcall\n"); 256177633Sdfr goto err2; 257177633Sdfr } 258177633Sdfr cs = (struct cu_socket *) so->so_upcallarg; 259177633Sdfr mtx_lock(&cs->cs_lock); 260177633Sdfr cs->cs_refs++; 261177633Sdfr mtx_unlock(&cs->cs_lock); 262177633Sdfr } else { 263177633Sdfr /* 264177633Sdfr * We are the first on this socket - allocate the 265177633Sdfr * structure and install it in the socket. 266177633Sdfr */ 267177633Sdfr SOCKBUF_UNLOCK(&cu->cu_socket->so_rcv); 268177633Sdfr cs = mem_alloc(sizeof(*cs)); 269177633Sdfr SOCKBUF_LOCK(&cu->cu_socket->so_rcv); 270177633Sdfr if (so->so_upcall) { 271177633Sdfr /* 272177633Sdfr * We have lost a race with some other client. 273177633Sdfr */ 274177633Sdfr mem_free(cs, sizeof(*cs)); 275177633Sdfr goto recheck_socket; 276177633Sdfr } 277177633Sdfr mtx_init(&cs->cs_lock, "cs->cs_lock", NULL, MTX_DEF); 278177633Sdfr cs->cs_refs = 1; 279177633Sdfr TAILQ_INIT(&cs->cs_pending); 280177633Sdfr so->so_upcallarg = cs; 281177633Sdfr so->so_upcall = clnt_dg_soupcall; 282177633Sdfr so->so_rcv.sb_flags |= SB_UPCALL; 283177633Sdfr } 284177633Sdfr SOCKBUF_UNLOCK(&so->so_rcv); 285177633Sdfr 286180025Sdfr cl->cl_refs = 1; 287177633Sdfr cl->cl_ops = &clnt_dg_ops; 288177633Sdfr cl->cl_private = (caddr_t)(void *)cu; 289177633Sdfr cl->cl_auth = authnone_create(); 290177633Sdfr cl->cl_tp = NULL; 291177633Sdfr cl->cl_netid = NULL; 292177633Sdfr return (cl); 293177633Sdfrerr2: 294177633Sdfr if (cl) { 295177633Sdfr mem_free(cl, sizeof (CLIENT)); 296177633Sdfr if (cu) 297177633Sdfr mem_free(cu, sizeof (*cu)); 298177633Sdfr } 299177633Sdfr return (NULL); 300177633Sdfr} 301177633Sdfr 302177633Sdfrstatic enum clnt_stat 303177633Sdfrclnt_dg_call( 304180025Sdfr CLIENT *cl, /* client handle */ 305180025Sdfr struct rpc_callextra *ext, /* call metadata */ 306177633Sdfr rpcproc_t proc, /* procedure number */ 307177633Sdfr xdrproc_t xargs, /* xdr routine for args */ 308177633Sdfr void *argsp, /* pointer to args */ 309177633Sdfr xdrproc_t xresults, /* xdr routine for results */ 310177633Sdfr void *resultsp, /* pointer to results */ 311177633Sdfr struct timeval utimeout) /* seconds to wait before giving up */ 312177633Sdfr{ 313177633Sdfr struct cu_data *cu = (struct cu_data *)cl->cl_private; 314177633Sdfr struct cu_socket *cs = (struct cu_socket *) cu->cu_socket->so_upcallarg; 315180025Sdfr AUTH *auth; 316177633Sdfr XDR xdrs; 317177633Sdfr struct rpc_msg reply_msg; 318177633Sdfr bool_t ok; 319180025Sdfr int retrans; /* number of re-transmits so far */ 320177633Sdfr int nrefreshes = 2; /* number of times to refresh cred */ 321180025Sdfr struct timeval *tvp; 322180025Sdfr int timeout; 323180025Sdfr int retransmit_time; 324180025Sdfr int next_sendtime, starttime, time_waited, tv; 325177633Sdfr struct sockaddr *sa; 326177633Sdfr socklen_t salen; 327177633Sdfr uint32_t xid; 328177633Sdfr struct mbuf *mreq = NULL; 329180025Sdfr struct cu_request *cr; 330177633Sdfr int error; 331177633Sdfr 332180025Sdfr cr = malloc(sizeof(struct cu_request), M_RPC, M_WAITOK); 333180025Sdfr 334177633Sdfr mtx_lock(&cs->cs_lock); 335177633Sdfr 336180025Sdfr if (cu->cu_closing) { 337180025Sdfr mtx_unlock(&cs->cs_lock); 338180025Sdfr free(cr, M_RPC); 339180025Sdfr return (RPC_CANTSEND); 340180025Sdfr } 341180025Sdfr cu->cu_threads++; 342177633Sdfr 343180025Sdfr if (ext) 344180025Sdfr auth = ext->rc_auth; 345180025Sdfr else 346180025Sdfr auth = cl->cl_auth; 347180025Sdfr 348180025Sdfr cr->cr_client = cl; 349180025Sdfr cr->cr_mrep = NULL; 350180025Sdfr cr->cr_error = 0; 351180025Sdfr 352177633Sdfr if (cu->cu_total.tv_usec == -1) { 353180025Sdfr tvp = &utimeout; /* use supplied timeout */ 354177633Sdfr } else { 355180025Sdfr tvp = &cu->cu_total; /* use default timeout */ 356177633Sdfr } 357180025Sdfr if (tvp->tv_sec || tvp->tv_usec) 358180025Sdfr timeout = tvtohz(tvp); 359180025Sdfr else 360180025Sdfr timeout = 0; 361177633Sdfr 362177633Sdfr if (cu->cu_connect && !cu->cu_connected) { 363177633Sdfr mtx_unlock(&cs->cs_lock); 364177633Sdfr error = soconnect(cu->cu_socket, 365177633Sdfr (struct sockaddr *)&cu->cu_raddr, curthread); 366177633Sdfr mtx_lock(&cs->cs_lock); 367177633Sdfr if (error) { 368177633Sdfr cu->cu_error.re_errno = error; 369177633Sdfr cu->cu_error.re_status = RPC_CANTSEND; 370177633Sdfr goto out; 371177633Sdfr } 372177633Sdfr cu->cu_connected = 1; 373177633Sdfr } 374177633Sdfr if (cu->cu_connected) { 375177633Sdfr sa = NULL; 376177633Sdfr salen = 0; 377177633Sdfr } else { 378177633Sdfr sa = (struct sockaddr *)&cu->cu_raddr; 379177633Sdfr salen = cu->cu_rlen; 380177633Sdfr } 381180025Sdfr time_waited = 0; 382180025Sdfr retrans = 0; 383180025Sdfr retransmit_time = next_sendtime = tvtohz(&cu->cu_wait); 384177633Sdfr 385180025Sdfr starttime = ticks; 386177633Sdfr 387177633Sdfrcall_again: 388177633Sdfr mtx_assert(&cs->cs_lock, MA_OWNED); 389177633Sdfr 390177633Sdfr cu->cu_xid++; 391177633Sdfr xid = cu->cu_xid; 392177633Sdfr 393177633Sdfrsend_again: 394177633Sdfr mtx_unlock(&cs->cs_lock); 395177633Sdfr 396177633Sdfr MGETHDR(mreq, M_WAIT, MT_DATA); 397177633Sdfr MCLGET(mreq, M_WAIT); 398177633Sdfr mreq->m_len = 0; 399177633Sdfr m_append(mreq, cu->cu_mcalllen, cu->cu_mcallc); 400177633Sdfr 401177633Sdfr /* 402177633Sdfr * The XID is the first thing in the request. 403177633Sdfr */ 404177633Sdfr *mtod(mreq, uint32_t *) = htonl(xid); 405177633Sdfr 406177633Sdfr xdrmbuf_create(&xdrs, mreq, XDR_ENCODE); 407177633Sdfr 408177633Sdfr if (cu->cu_async == TRUE && xargs == NULL) 409177633Sdfr goto get_reply; 410177633Sdfr 411177633Sdfr if ((! XDR_PUTINT32(&xdrs, &proc)) || 412180025Sdfr (! AUTH_MARSHALL(auth, &xdrs)) || 413177633Sdfr (! (*xargs)(&xdrs, argsp))) { 414177633Sdfr cu->cu_error.re_status = RPC_CANTENCODEARGS; 415177633Sdfr mtx_lock(&cs->cs_lock); 416177633Sdfr goto out; 417177633Sdfr } 418177633Sdfr m_fixhdr(mreq); 419177633Sdfr 420180025Sdfr cr->cr_xid = xid; 421177633Sdfr mtx_lock(&cs->cs_lock); 422180025Sdfr TAILQ_INSERT_TAIL(&cs->cs_pending, cr, cr_link); 423177633Sdfr mtx_unlock(&cs->cs_lock); 424177633Sdfr 425177633Sdfr /* 426177633Sdfr * sosend consumes mreq. 427177633Sdfr */ 428177633Sdfr error = sosend(cu->cu_socket, sa, NULL, mreq, NULL, 0, curthread); 429177633Sdfr mreq = NULL; 430177633Sdfr 431177633Sdfr /* 432177633Sdfr * sub-optimal code appears here because we have 433177633Sdfr * some clock time to spare while the packets are in flight. 434177633Sdfr * (We assume that this is actually only executed once.) 435177633Sdfr */ 436177633Sdfr reply_msg.acpted_rply.ar_verf = _null_auth; 437177633Sdfr reply_msg.acpted_rply.ar_results.where = resultsp; 438177633Sdfr reply_msg.acpted_rply.ar_results.proc = xresults; 439177633Sdfr 440177633Sdfr mtx_lock(&cs->cs_lock); 441177633Sdfr if (error) { 442180025Sdfr TAILQ_REMOVE(&cs->cs_pending, cr, cr_link); 443177633Sdfr cu->cu_error.re_errno = error; 444177633Sdfr cu->cu_error.re_status = RPC_CANTSEND; 445177633Sdfr goto out; 446177633Sdfr } 447177633Sdfr 448177633Sdfr /* 449177633Sdfr * Check to see if we got an upcall while waiting for the 450180025Sdfr * lock. 451177633Sdfr */ 452180025Sdfr if (cr->cr_error) { 453180025Sdfr TAILQ_REMOVE(&cs->cs_pending, cr, cr_link); 454180025Sdfr cu->cu_error.re_errno = cr->cr_error; 455177633Sdfr cu->cu_error.re_status = RPC_CANTRECV; 456177633Sdfr goto out; 457177633Sdfr } 458180025Sdfr if (cr->cr_mrep) { 459180025Sdfr TAILQ_REMOVE(&cs->cs_pending, cr, cr_link); 460177633Sdfr goto got_reply; 461177633Sdfr } 462177633Sdfr 463177633Sdfr /* 464177633Sdfr * Hack to provide rpc-based message passing 465177633Sdfr */ 466180025Sdfr if (timeout == 0) { 467180025Sdfr TAILQ_REMOVE(&cs->cs_pending, cr, cr_link); 468177633Sdfr cu->cu_error.re_status = RPC_TIMEDOUT; 469177633Sdfr goto out; 470177633Sdfr } 471177633Sdfr 472177633Sdfrget_reply: 473177633Sdfr for (;;) { 474177633Sdfr /* Decide how long to wait. */ 475180025Sdfr if (next_sendtime < timeout) 476177633Sdfr tv = next_sendtime; 477180025Sdfr else 478180025Sdfr tv = timeout; 479180025Sdfr tv -= time_waited; 480180025Sdfr 481180025Sdfr if (tv > 0) { 482180025Sdfr if (cu->cu_closing) 483180025Sdfr error = 0; 484180025Sdfr else 485180025Sdfr error = msleep(cr, &cs->cs_lock, 486180025Sdfr cu->cu_waitflag, cu->cu_waitchan, tv); 487177633Sdfr } else { 488180025Sdfr error = EWOULDBLOCK; 489177633Sdfr } 490177633Sdfr 491180025Sdfr TAILQ_REMOVE(&cs->cs_pending, cr, cr_link); 492177633Sdfr 493177633Sdfr if (!error) { 494177633Sdfr /* 495177633Sdfr * We were woken up by the upcall. If the 496177633Sdfr * upcall had a receive error, report that, 497177633Sdfr * otherwise we have a reply. 498177633Sdfr */ 499180025Sdfr if (cr->cr_error) { 500180025Sdfr cu->cu_error.re_errno = cr->cr_error; 501177633Sdfr cu->cu_error.re_status = RPC_CANTRECV; 502177633Sdfr goto out; 503177633Sdfr } 504177633Sdfr break; 505177633Sdfr } 506177633Sdfr 507177633Sdfr /* 508177633Sdfr * The sleep returned an error so our request is still 509177633Sdfr * on the list. If we got EWOULDBLOCK, we may want to 510177633Sdfr * re-send the request. 511177633Sdfr */ 512177633Sdfr if (error != EWOULDBLOCK) { 513177633Sdfr cu->cu_error.re_errno = error; 514177633Sdfr if (error == EINTR) 515177633Sdfr cu->cu_error.re_status = RPC_INTR; 516177633Sdfr else 517177633Sdfr cu->cu_error.re_status = RPC_CANTRECV; 518177633Sdfr goto out; 519177633Sdfr } 520177633Sdfr 521180025Sdfr time_waited = ticks - starttime; 522177633Sdfr 523177633Sdfr /* Check for timeout. */ 524180025Sdfr if (time_waited > timeout) { 525177633Sdfr cu->cu_error.re_errno = EWOULDBLOCK; 526177633Sdfr cu->cu_error.re_status = RPC_TIMEDOUT; 527177633Sdfr goto out; 528177633Sdfr } 529177633Sdfr 530177633Sdfr /* Retransmit if necessary. */ 531180025Sdfr if (time_waited >= next_sendtime) { 532180025Sdfr if (ext && ext->rc_feedback) { 533180025Sdfr mtx_unlock(&cs->cs_lock); 534180025Sdfr if (retrans == 0) 535180025Sdfr ext->rc_feedback(FEEDBACK_REXMIT1, 536180025Sdfr proc, ext->rc_feedback_arg); 537180025Sdfr else 538180025Sdfr ext->rc_feedback(FEEDBACK_REXMIT2, 539180025Sdfr proc, ext->rc_feedback_arg); 540180025Sdfr mtx_lock(&cs->cs_lock); 541180025Sdfr } 542180025Sdfr if (cu->cu_closing) { 543180025Sdfr cu->cu_error.re_errno = ESHUTDOWN; 544180025Sdfr cu->cu_error.re_status = RPC_CANTRECV; 545180025Sdfr goto out; 546180025Sdfr } 547180025Sdfr retrans++; 548177633Sdfr /* update retransmit_time */ 549180025Sdfr if (retransmit_time < RPC_MAX_BACKOFF * hz) 550180025Sdfr retransmit_time = 2 * retransmit_time; 551180025Sdfr next_sendtime += retransmit_time; 552177633Sdfr goto send_again; 553177633Sdfr } 554180025Sdfr TAILQ_INSERT_TAIL(&cs->cs_pending, cr, cr_link); 555177633Sdfr } 556177633Sdfr 557177633Sdfrgot_reply: 558177633Sdfr /* 559177633Sdfr * Now decode and validate the response. We need to drop the 560177633Sdfr * lock since xdr_replymsg may end up sleeping in malloc. 561177633Sdfr */ 562177633Sdfr mtx_unlock(&cs->cs_lock); 563177633Sdfr 564180025Sdfr if (ext && ext->rc_feedback) 565180025Sdfr ext->rc_feedback(FEEDBACK_OK, proc, ext->rc_feedback_arg); 566180025Sdfr 567180025Sdfr xdrmbuf_create(&xdrs, cr->cr_mrep, XDR_DECODE); 568177633Sdfr ok = xdr_replymsg(&xdrs, &reply_msg); 569177633Sdfr XDR_DESTROY(&xdrs); 570180025Sdfr cr->cr_mrep = NULL; 571177633Sdfr 572177633Sdfr mtx_lock(&cs->cs_lock); 573177633Sdfr 574177633Sdfr if (ok) { 575177633Sdfr if ((reply_msg.rm_reply.rp_stat == MSG_ACCEPTED) && 576177633Sdfr (reply_msg.acpted_rply.ar_stat == SUCCESS)) 577177633Sdfr cu->cu_error.re_status = RPC_SUCCESS; 578177633Sdfr else 579177633Sdfr _seterr_reply(&reply_msg, &(cu->cu_error)); 580177633Sdfr 581177633Sdfr if (cu->cu_error.re_status == RPC_SUCCESS) { 582177633Sdfr if (! AUTH_VALIDATE(cl->cl_auth, 583177633Sdfr &reply_msg.acpted_rply.ar_verf)) { 584177633Sdfr cu->cu_error.re_status = RPC_AUTHERROR; 585177633Sdfr cu->cu_error.re_why = AUTH_INVALIDRESP; 586177633Sdfr } 587177633Sdfr if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) { 588177633Sdfr xdrs.x_op = XDR_FREE; 589177633Sdfr (void) xdr_opaque_auth(&xdrs, 590177633Sdfr &(reply_msg.acpted_rply.ar_verf)); 591177633Sdfr } 592177633Sdfr } /* end successful completion */ 593177633Sdfr /* 594177633Sdfr * If unsuccesful AND error is an authentication error 595177633Sdfr * then refresh credentials and try again, else break 596177633Sdfr */ 597177633Sdfr else if (cu->cu_error.re_status == RPC_AUTHERROR) 598177633Sdfr /* maybe our credentials need to be refreshed ... */ 599177633Sdfr if (nrefreshes > 0 && 600177633Sdfr AUTH_REFRESH(cl->cl_auth, &reply_msg)) { 601177633Sdfr nrefreshes--; 602177633Sdfr goto call_again; 603177633Sdfr } 604177633Sdfr /* end of unsuccessful completion */ 605177633Sdfr } /* end of valid reply message */ 606177633Sdfr else { 607177633Sdfr cu->cu_error.re_status = RPC_CANTDECODERES; 608177633Sdfr 609177633Sdfr } 610177633Sdfrout: 611177633Sdfr mtx_assert(&cs->cs_lock, MA_OWNED); 612177633Sdfr 613177633Sdfr if (mreq) 614177633Sdfr m_freem(mreq); 615180025Sdfr if (cr->cr_mrep) 616180025Sdfr m_freem(cr->cr_mrep); 617177633Sdfr 618180025Sdfr cu->cu_threads--; 619180025Sdfr if (cu->cu_closing) 620180025Sdfr wakeup(cu); 621180025Sdfr 622177633Sdfr mtx_unlock(&cs->cs_lock); 623180025Sdfr 624180025Sdfr free(cr, M_RPC); 625180025Sdfr 626177633Sdfr return (cu->cu_error.re_status); 627177633Sdfr} 628177633Sdfr 629177633Sdfrstatic void 630177633Sdfrclnt_dg_geterr(CLIENT *cl, struct rpc_err *errp) 631177633Sdfr{ 632177633Sdfr struct cu_data *cu = (struct cu_data *)cl->cl_private; 633177633Sdfr 634177633Sdfr *errp = cu->cu_error; 635177633Sdfr} 636177633Sdfr 637177633Sdfrstatic bool_t 638177633Sdfrclnt_dg_freeres(CLIENT *cl, xdrproc_t xdr_res, void *res_ptr) 639177633Sdfr{ 640177633Sdfr XDR xdrs; 641177633Sdfr bool_t dummy; 642177633Sdfr 643177633Sdfr xdrs.x_op = XDR_FREE; 644177633Sdfr dummy = (*xdr_res)(&xdrs, res_ptr); 645177633Sdfr 646177633Sdfr return (dummy); 647177633Sdfr} 648177633Sdfr 649177633Sdfr/*ARGSUSED*/ 650177633Sdfrstatic void 651177633Sdfrclnt_dg_abort(CLIENT *h) 652177633Sdfr{ 653177633Sdfr} 654177633Sdfr 655177633Sdfrstatic bool_t 656177633Sdfrclnt_dg_control(CLIENT *cl, u_int request, void *info) 657177633Sdfr{ 658177633Sdfr struct cu_data *cu = (struct cu_data *)cl->cl_private; 659177633Sdfr struct cu_socket *cs = (struct cu_socket *) cu->cu_socket->so_upcallarg; 660177633Sdfr struct sockaddr *addr; 661177633Sdfr 662177633Sdfr mtx_lock(&cs->cs_lock); 663177633Sdfr 664177633Sdfr switch (request) { 665177633Sdfr case CLSET_FD_CLOSE: 666177633Sdfr cu->cu_closeit = TRUE; 667177633Sdfr mtx_unlock(&cs->cs_lock); 668177633Sdfr return (TRUE); 669177633Sdfr case CLSET_FD_NCLOSE: 670177633Sdfr cu->cu_closeit = FALSE; 671177633Sdfr mtx_unlock(&cs->cs_lock); 672177633Sdfr return (TRUE); 673177633Sdfr } 674177633Sdfr 675177633Sdfr /* for other requests which use info */ 676177633Sdfr if (info == NULL) { 677177633Sdfr mtx_unlock(&cs->cs_lock); 678177633Sdfr return (FALSE); 679177633Sdfr } 680177633Sdfr switch (request) { 681177633Sdfr case CLSET_TIMEOUT: 682177633Sdfr if (time_not_ok((struct timeval *)info)) { 683177633Sdfr mtx_unlock(&cs->cs_lock); 684177633Sdfr return (FALSE); 685177633Sdfr } 686177633Sdfr cu->cu_total = *(struct timeval *)info; 687177633Sdfr break; 688177633Sdfr case CLGET_TIMEOUT: 689177633Sdfr *(struct timeval *)info = cu->cu_total; 690177633Sdfr break; 691177633Sdfr case CLSET_RETRY_TIMEOUT: 692177633Sdfr if (time_not_ok((struct timeval *)info)) { 693177633Sdfr mtx_unlock(&cs->cs_lock); 694177633Sdfr return (FALSE); 695177633Sdfr } 696177633Sdfr cu->cu_wait = *(struct timeval *)info; 697177633Sdfr break; 698177633Sdfr case CLGET_RETRY_TIMEOUT: 699177633Sdfr *(struct timeval *)info = cu->cu_wait; 700177633Sdfr break; 701177633Sdfr case CLGET_SVC_ADDR: 702177633Sdfr /* 703177633Sdfr * Slightly different semantics to userland - we use 704177633Sdfr * sockaddr instead of netbuf. 705177633Sdfr */ 706177633Sdfr memcpy(info, &cu->cu_raddr, cu->cu_raddr.ss_len); 707177633Sdfr break; 708177633Sdfr case CLSET_SVC_ADDR: /* set to new address */ 709177633Sdfr addr = (struct sockaddr *)info; 710177633Sdfr (void) memcpy(&cu->cu_raddr, addr, addr->sa_len); 711177633Sdfr break; 712177633Sdfr case CLGET_XID: 713177633Sdfr *(uint32_t *)info = cu->cu_xid; 714177633Sdfr break; 715177633Sdfr 716177633Sdfr case CLSET_XID: 717177633Sdfr /* This will set the xid of the NEXT call */ 718177633Sdfr /* decrement by 1 as clnt_dg_call() increments once */ 719177633Sdfr cu->cu_xid = *(uint32_t *)info - 1; 720177633Sdfr break; 721177633Sdfr 722177633Sdfr case CLGET_VERS: 723177633Sdfr /* 724177633Sdfr * This RELIES on the information that, in the call body, 725177633Sdfr * the version number field is the fifth field from the 726177633Sdfr * begining of the RPC header. MUST be changed if the 727177633Sdfr * call_struct is changed 728177633Sdfr */ 729177633Sdfr *(uint32_t *)info = 730177633Sdfr ntohl(*(uint32_t *)(void *)(cu->cu_mcallc + 731177633Sdfr 4 * BYTES_PER_XDR_UNIT)); 732177633Sdfr break; 733177633Sdfr 734177633Sdfr case CLSET_VERS: 735177633Sdfr *(uint32_t *)(void *)(cu->cu_mcallc + 4 * BYTES_PER_XDR_UNIT) 736177633Sdfr = htonl(*(uint32_t *)info); 737177633Sdfr break; 738177633Sdfr 739177633Sdfr case CLGET_PROG: 740177633Sdfr /* 741177633Sdfr * This RELIES on the information that, in the call body, 742177633Sdfr * the program number field is the fourth field from the 743177633Sdfr * begining of the RPC header. MUST be changed if the 744177633Sdfr * call_struct is changed 745177633Sdfr */ 746177633Sdfr *(uint32_t *)info = 747177633Sdfr ntohl(*(uint32_t *)(void *)(cu->cu_mcallc + 748177633Sdfr 3 * BYTES_PER_XDR_UNIT)); 749177633Sdfr break; 750177633Sdfr 751177633Sdfr case CLSET_PROG: 752177633Sdfr *(uint32_t *)(void *)(cu->cu_mcallc + 3 * BYTES_PER_XDR_UNIT) 753177633Sdfr = htonl(*(uint32_t *)info); 754177633Sdfr break; 755177633Sdfr case CLSET_ASYNC: 756177633Sdfr cu->cu_async = *(int *)info; 757177633Sdfr break; 758177633Sdfr case CLSET_CONNECT: 759177633Sdfr cu->cu_connect = *(int *)info; 760177633Sdfr break; 761177633Sdfr case CLSET_WAITCHAN: 762177633Sdfr cu->cu_waitchan = *(const char **)info; 763177633Sdfr break; 764177633Sdfr case CLGET_WAITCHAN: 765177633Sdfr *(const char **) info = cu->cu_waitchan; 766177633Sdfr break; 767177633Sdfr case CLSET_INTERRUPTIBLE: 768177633Sdfr if (*(int *) info) 769177633Sdfr cu->cu_waitflag = PCATCH; 770177633Sdfr else 771177633Sdfr cu->cu_waitflag = 0; 772177633Sdfr break; 773177633Sdfr case CLGET_INTERRUPTIBLE: 774177633Sdfr if (cu->cu_waitflag) 775177633Sdfr *(int *) info = TRUE; 776177633Sdfr else 777177633Sdfr *(int *) info = FALSE; 778177633Sdfr break; 779177633Sdfr default: 780177633Sdfr mtx_unlock(&cs->cs_lock); 781177633Sdfr return (FALSE); 782177633Sdfr } 783177633Sdfr mtx_unlock(&cs->cs_lock); 784177633Sdfr return (TRUE); 785177633Sdfr} 786177633Sdfr 787177633Sdfrstatic void 788177633Sdfrclnt_dg_destroy(CLIENT *cl) 789177633Sdfr{ 790177633Sdfr struct cu_data *cu = (struct cu_data *)cl->cl_private; 791177633Sdfr struct cu_socket *cs = (struct cu_socket *) cu->cu_socket->so_upcallarg; 792180025Sdfr struct cu_request *cr; 793177633Sdfr struct socket *so = NULL; 794177633Sdfr bool_t lastsocketref; 795177633Sdfr 796180025Sdfr mtx_lock(&cs->cs_lock); 797177633Sdfr 798180025Sdfr /* 799180025Sdfr * Abort any pending requests and wait until everyone 800180025Sdfr * has finished with clnt_vc_call. 801180025Sdfr */ 802180025Sdfr cu->cu_closing = TRUE; 803180025Sdfr TAILQ_FOREACH(cr, &cs->cs_pending, cr_link) { 804180025Sdfr if (cr->cr_client == cl) { 805180025Sdfr cr->cr_xid = 0; 806180025Sdfr cr->cr_error = ESHUTDOWN; 807180025Sdfr wakeup(cr); 808180025Sdfr } 809180025Sdfr } 810180025Sdfr 811180025Sdfr while (cu->cu_threads) 812180025Sdfr msleep(cu, &cs->cs_lock, 0, "rpcclose", 0); 813180025Sdfr 814177633Sdfr cs->cs_refs--; 815177633Sdfr if (cs->cs_refs == 0) { 816180025Sdfr mtx_destroy(&cs->cs_lock); 817180025Sdfr SOCKBUF_LOCK(&cu->cu_socket->so_rcv); 818177633Sdfr cu->cu_socket->so_upcallarg = NULL; 819177633Sdfr cu->cu_socket->so_upcall = NULL; 820177633Sdfr cu->cu_socket->so_rcv.sb_flags &= ~SB_UPCALL; 821177633Sdfr SOCKBUF_UNLOCK(&cu->cu_socket->so_rcv); 822177633Sdfr mem_free(cs, sizeof(*cs)); 823177633Sdfr lastsocketref = TRUE; 824177633Sdfr } else { 825177633Sdfr mtx_unlock(&cs->cs_lock); 826177633Sdfr lastsocketref = FALSE; 827177633Sdfr } 828177633Sdfr 829180025Sdfr if (cu->cu_closeit && lastsocketref) { 830177633Sdfr so = cu->cu_socket; 831177633Sdfr cu->cu_socket = NULL; 832177633Sdfr } 833177633Sdfr 834177633Sdfr if (so) 835177633Sdfr soclose(so); 836177633Sdfr 837177633Sdfr if (cl->cl_netid && cl->cl_netid[0]) 838177633Sdfr mem_free(cl->cl_netid, strlen(cl->cl_netid) +1); 839177633Sdfr if (cl->cl_tp && cl->cl_tp[0]) 840177633Sdfr mem_free(cl->cl_tp, strlen(cl->cl_tp) +1); 841177633Sdfr mem_free(cu, sizeof (*cu)); 842177633Sdfr mem_free(cl, sizeof (CLIENT)); 843177633Sdfr} 844177633Sdfr 845177633Sdfr/* 846177633Sdfr * Make sure that the time is not garbage. -1 value is allowed. 847177633Sdfr */ 848177633Sdfrstatic bool_t 849177633Sdfrtime_not_ok(struct timeval *t) 850177633Sdfr{ 851177633Sdfr return (t->tv_sec < -1 || t->tv_sec > 100000000 || 852177633Sdfr t->tv_usec < -1 || t->tv_usec > 1000000); 853177633Sdfr} 854177633Sdfr 855177633Sdfrvoid 856177633Sdfrclnt_dg_soupcall(struct socket *so, void *arg, int waitflag) 857177633Sdfr{ 858177633Sdfr struct cu_socket *cs = (struct cu_socket *) arg; 859177633Sdfr struct uio uio; 860177633Sdfr struct mbuf *m; 861177633Sdfr struct mbuf *control; 862177633Sdfr struct cu_request *cr; 863177633Sdfr int error, rcvflag, foundreq; 864177633Sdfr uint32_t xid; 865177633Sdfr 866177633Sdfr uio.uio_resid = 1000000000; 867177633Sdfr uio.uio_td = curthread; 868177633Sdfr do { 869177633Sdfr m = NULL; 870177633Sdfr control = NULL; 871177633Sdfr rcvflag = MSG_DONTWAIT; 872177633Sdfr error = soreceive(so, NULL, &uio, &m, &control, &rcvflag); 873177633Sdfr if (control) 874177633Sdfr m_freem(control); 875177633Sdfr 876177633Sdfr if (error == EWOULDBLOCK) 877177633Sdfr break; 878177633Sdfr 879177633Sdfr /* 880177633Sdfr * If there was an error, wake up all pending 881177633Sdfr * requests. 882177633Sdfr */ 883177633Sdfr if (error) { 884177633Sdfr mtx_lock(&cs->cs_lock); 885177633Sdfr TAILQ_FOREACH(cr, &cs->cs_pending, cr_link) { 886180025Sdfr cr->cr_xid = 0; 887177633Sdfr cr->cr_error = error; 888177633Sdfr wakeup(cr); 889177633Sdfr } 890177633Sdfr mtx_unlock(&cs->cs_lock); 891177633Sdfr break; 892177633Sdfr } 893177633Sdfr 894177633Sdfr /* 895177633Sdfr * The XID is in the first uint32_t of the reply. 896177633Sdfr */ 897177633Sdfr m = m_pullup(m, sizeof(xid)); 898177633Sdfr if (!m) 899180025Sdfr /* 900180025Sdfr * Should never happen. 901180025Sdfr */ 902180025Sdfr continue; 903180025Sdfr 904177633Sdfr xid = ntohl(*mtod(m, uint32_t *)); 905177633Sdfr 906177633Sdfr /* 907177633Sdfr * Attempt to match this reply with a pending request. 908177633Sdfr */ 909177633Sdfr mtx_lock(&cs->cs_lock); 910177633Sdfr foundreq = 0; 911177633Sdfr TAILQ_FOREACH(cr, &cs->cs_pending, cr_link) { 912177633Sdfr if (cr->cr_xid == xid) { 913177633Sdfr /* 914180025Sdfr * This one matches. We leave the 915177633Sdfr * reply mbuf in cr->cr_mrep. Set the 916180025Sdfr * XID to zero so that we will ignore 917180025Sdfr * any duplicated replies that arrive 918180025Sdfr * before clnt_dg_call removes it from 919180025Sdfr * the queue. 920177633Sdfr */ 921177633Sdfr cr->cr_xid = 0; 922177633Sdfr cr->cr_mrep = m; 923177633Sdfr cr->cr_error = 0; 924177633Sdfr foundreq = 1; 925177633Sdfr wakeup(cr); 926177633Sdfr break; 927177633Sdfr } 928177633Sdfr } 929177633Sdfr mtx_unlock(&cs->cs_lock); 930177633Sdfr 931177633Sdfr /* 932177633Sdfr * If we didn't find the matching request, just drop 933177633Sdfr * it - its probably a repeated reply. 934177633Sdfr */ 935177633Sdfr if (!foundreq) 936177633Sdfr m_freem(m); 937177633Sdfr } while (m); 938177633Sdfr} 939177633Sdfr 940