clnt_dg.c revision 177633
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 177633 2008-03-26 15:23:12Z dfr $"); 41177633Sdfr 42177633Sdfr/* 43177633Sdfr * Implements a connectionless client side RPC. 44177633Sdfr */ 45177633Sdfr 46177633Sdfr#include <sys/param.h> 47177633Sdfr#include <sys/systm.h> 48177633Sdfr#include <sys/lock.h> 49177633Sdfr#include <sys/malloc.h> 50177633Sdfr#include <sys/mbuf.h> 51177633Sdfr#include <sys/mutex.h> 52177633Sdfr#include <sys/pcpu.h> 53177633Sdfr#include <sys/proc.h> 54177633Sdfr#include <sys/socket.h> 55177633Sdfr#include <sys/socketvar.h> 56177633Sdfr#include <sys/time.h> 57177633Sdfr#include <sys/uio.h> 58177633Sdfr 59177633Sdfr#include <rpc/rpc.h> 60177633Sdfr#include "rpc_com.h" 61177633Sdfr 62177633Sdfr 63177633Sdfr#ifdef _FREEFALL_CONFIG 64177633Sdfr/* 65177633Sdfr * Disable RPC exponential back-off for FreeBSD.org systems. 66177633Sdfr */ 67177633Sdfr#define RPC_MAX_BACKOFF 1 /* second */ 68177633Sdfr#else 69177633Sdfr#define RPC_MAX_BACKOFF 30 /* seconds */ 70177633Sdfr#endif 71177633Sdfr 72177633Sdfrstatic bool_t time_not_ok(struct timeval *); 73177633Sdfrstatic enum clnt_stat clnt_dg_call(CLIENT *, rpcproc_t, xdrproc_t, void *, 74177633Sdfr xdrproc_t, void *, struct timeval); 75177633Sdfrstatic void clnt_dg_geterr(CLIENT *, struct rpc_err *); 76177633Sdfrstatic bool_t clnt_dg_freeres(CLIENT *, xdrproc_t, void *); 77177633Sdfrstatic void clnt_dg_abort(CLIENT *); 78177633Sdfrstatic bool_t clnt_dg_control(CLIENT *, u_int, void *); 79177633Sdfrstatic void clnt_dg_destroy(CLIENT *); 80177633Sdfrstatic void clnt_dg_soupcall(struct socket *so, void *arg, int waitflag); 81177633Sdfr 82177633Sdfrstatic struct clnt_ops clnt_dg_ops = { 83177633Sdfr .cl_call = clnt_dg_call, 84177633Sdfr .cl_abort = clnt_dg_abort, 85177633Sdfr .cl_geterr = clnt_dg_geterr, 86177633Sdfr .cl_freeres = clnt_dg_freeres, 87177633Sdfr .cl_destroy = clnt_dg_destroy, 88177633Sdfr .cl_control = clnt_dg_control 89177633Sdfr}; 90177633Sdfr 91177633Sdfrstatic const char mem_err_clnt_dg[] = "clnt_dg_create: out of memory"; 92177633Sdfr 93177633Sdfr/* 94177633Sdfr * A pending RPC request which awaits a reply. 95177633Sdfr */ 96177633Sdfrstruct cu_request { 97177633Sdfr TAILQ_ENTRY(cu_request) cr_link; 98177633Sdfr uint32_t cr_xid; /* XID of request */ 99177633Sdfr struct mbuf *cr_mrep; /* reply received by upcall */ 100177633Sdfr int cr_error; /* any error from upcall */ 101177633Sdfr}; 102177633Sdfr 103177633SdfrTAILQ_HEAD(cu_request_list, cu_request); 104177633Sdfr 105177633Sdfr#define MCALL_MSG_SIZE 24 106177633Sdfr 107177633Sdfr/* 108177633Sdfr * This structure is pointed to by the socket's so_upcallarg 109177633Sdfr * member. It is separate from the client private data to facilitate 110177633Sdfr * multiple clients sharing the same socket. The cs_lock mutex is used 111177633Sdfr * to protect all fields of this structure, the socket's receive 112177633Sdfr * buffer SOCKBUF_LOCK is used to ensure that exactly one of these 113177633Sdfr * structures is installed on the socket. 114177633Sdfr */ 115177633Sdfrstruct cu_socket { 116177633Sdfr struct mtx cs_lock; 117177633Sdfr int cs_refs; /* Count of clients */ 118177633Sdfr struct cu_request_list cs_pending; /* Requests awaiting replies */ 119177633Sdfr 120177633Sdfr}; 121177633Sdfr 122177633Sdfr/* 123177633Sdfr * Private data kept per client handle 124177633Sdfr */ 125177633Sdfrstruct cu_data { 126177633Sdfr struct socket *cu_socket; /* connection socket */ 127177633Sdfr bool_t cu_closeit; /* opened by library */ 128177633Sdfr struct sockaddr_storage cu_raddr; /* remote address */ 129177633Sdfr int cu_rlen; 130177633Sdfr struct timeval cu_wait; /* retransmit interval */ 131177633Sdfr struct timeval cu_total; /* total time for the call */ 132177633Sdfr struct rpc_err cu_error; 133177633Sdfr uint32_t cu_xid; 134177633Sdfr char cu_mcallc[MCALL_MSG_SIZE]; /* marshalled callmsg */ 135177633Sdfr size_t cu_mcalllen; 136177633Sdfr size_t cu_sendsz; /* send size */ 137177633Sdfr size_t cu_recvsz; /* recv size */ 138177633Sdfr int cu_async; 139177633Sdfr int cu_connect; /* Use connect(). */ 140177633Sdfr int cu_connected; /* Have done connect(). */ 141177633Sdfr const char *cu_waitchan; 142177633Sdfr int cu_waitflag; 143177633Sdfr}; 144177633Sdfr 145177633Sdfr/* 146177633Sdfr * Connection less client creation returns with client handle parameters. 147177633Sdfr * Default options are set, which the user can change using clnt_control(). 148177633Sdfr * fd should be open and bound. 149177633Sdfr * NB: The rpch->cl_auth is initialized to null authentication. 150177633Sdfr * Caller may wish to set this something more useful. 151177633Sdfr * 152177633Sdfr * sendsz and recvsz are the maximum allowable packet sizes that can be 153177633Sdfr * sent and received. Normally they are the same, but they can be 154177633Sdfr * changed to improve the program efficiency and buffer allocation. 155177633Sdfr * If they are 0, use the transport default. 156177633Sdfr * 157177633Sdfr * If svcaddr is NULL, returns NULL. 158177633Sdfr */ 159177633SdfrCLIENT * 160177633Sdfrclnt_dg_create( 161177633Sdfr struct socket *so, 162177633Sdfr struct sockaddr *svcaddr, /* servers address */ 163177633Sdfr rpcprog_t program, /* program number */ 164177633Sdfr rpcvers_t version, /* version number */ 165177633Sdfr size_t sendsz, /* buffer recv size */ 166177633Sdfr size_t recvsz) /* buffer send size */ 167177633Sdfr{ 168177633Sdfr CLIENT *cl = NULL; /* client handle */ 169177633Sdfr struct cu_data *cu = NULL; /* private data */ 170177633Sdfr struct cu_socket *cs = NULL; 171177633Sdfr struct timeval now; 172177633Sdfr struct rpc_msg call_msg; 173177633Sdfr struct __rpc_sockinfo si; 174177633Sdfr XDR xdrs; 175177633Sdfr 176177633Sdfr if (svcaddr == NULL) { 177177633Sdfr rpc_createerr.cf_stat = RPC_UNKNOWNADDR; 178177633Sdfr return (NULL); 179177633Sdfr } 180177633Sdfr 181177633Sdfr if (!__rpc_socket2sockinfo(so, &si)) { 182177633Sdfr rpc_createerr.cf_stat = RPC_TLIERROR; 183177633Sdfr rpc_createerr.cf_error.re_errno = 0; 184177633Sdfr return (NULL); 185177633Sdfr } 186177633Sdfr 187177633Sdfr /* 188177633Sdfr * Find the receive and the send size 189177633Sdfr */ 190177633Sdfr sendsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsz); 191177633Sdfr recvsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsz); 192177633Sdfr if ((sendsz == 0) || (recvsz == 0)) { 193177633Sdfr rpc_createerr.cf_stat = RPC_TLIERROR; /* XXX */ 194177633Sdfr rpc_createerr.cf_error.re_errno = 0; 195177633Sdfr return (NULL); 196177633Sdfr } 197177633Sdfr 198177633Sdfr cl = mem_alloc(sizeof (CLIENT)); 199177633Sdfr 200177633Sdfr /* 201177633Sdfr * Should be multiple of 4 for XDR. 202177633Sdfr */ 203177633Sdfr sendsz = ((sendsz + 3) / 4) * 4; 204177633Sdfr recvsz = ((recvsz + 3) / 4) * 4; 205177633Sdfr cu = mem_alloc(sizeof (*cu)); 206177633Sdfr (void) memcpy(&cu->cu_raddr, svcaddr, (size_t)svcaddr->sa_len); 207177633Sdfr cu->cu_rlen = svcaddr->sa_len; 208177633Sdfr /* Other values can also be set through clnt_control() */ 209177633Sdfr cu->cu_wait.tv_sec = 15; /* heuristically chosen */ 210177633Sdfr cu->cu_wait.tv_usec = 0; 211177633Sdfr cu->cu_total.tv_sec = -1; 212177633Sdfr cu->cu_total.tv_usec = -1; 213177633Sdfr cu->cu_sendsz = sendsz; 214177633Sdfr cu->cu_recvsz = recvsz; 215177633Sdfr cu->cu_async = FALSE; 216177633Sdfr cu->cu_connect = FALSE; 217177633Sdfr cu->cu_connected = FALSE; 218177633Sdfr cu->cu_waitchan = "rpcrecv"; 219177633Sdfr cu->cu_waitflag = 0; 220177633Sdfr (void) getmicrotime(&now); 221177633Sdfr cu->cu_xid = __RPC_GETXID(&now); 222177633Sdfr call_msg.rm_xid = cu->cu_xid; 223177633Sdfr call_msg.rm_call.cb_prog = program; 224177633Sdfr call_msg.rm_call.cb_vers = version; 225177633Sdfr xdrmem_create(&xdrs, cu->cu_mcallc, MCALL_MSG_SIZE, XDR_ENCODE); 226177633Sdfr if (! xdr_callhdr(&xdrs, &call_msg)) { 227177633Sdfr rpc_createerr.cf_stat = RPC_CANTENCODEARGS; /* XXX */ 228177633Sdfr rpc_createerr.cf_error.re_errno = 0; 229177633Sdfr goto err2; 230177633Sdfr } 231177633Sdfr cu->cu_mcalllen = XDR_GETPOS(&xdrs);; 232177633Sdfr 233177633Sdfr /* 234177633Sdfr * By default, closeit is always FALSE. It is users responsibility 235177633Sdfr * to do a close on it, else the user may use clnt_control 236177633Sdfr * to let clnt_destroy do it for him/her. 237177633Sdfr */ 238177633Sdfr cu->cu_closeit = FALSE; 239177633Sdfr cu->cu_socket = so; 240177633Sdfr 241177633Sdfr SOCKBUF_LOCK(&so->so_rcv); 242177633Sdfrrecheck_socket: 243177633Sdfr if (so->so_upcall) { 244177633Sdfr if (so->so_upcall != clnt_dg_soupcall) { 245177633Sdfr SOCKBUF_UNLOCK(&so->so_rcv); 246177633Sdfr printf("clnt_dg_create(): socket already has an incompatible upcall\n"); 247177633Sdfr goto err2; 248177633Sdfr } 249177633Sdfr cs = (struct cu_socket *) so->so_upcallarg; 250177633Sdfr mtx_lock(&cs->cs_lock); 251177633Sdfr cs->cs_refs++; 252177633Sdfr mtx_unlock(&cs->cs_lock); 253177633Sdfr } else { 254177633Sdfr /* 255177633Sdfr * We are the first on this socket - allocate the 256177633Sdfr * structure and install it in the socket. 257177633Sdfr */ 258177633Sdfr SOCKBUF_UNLOCK(&cu->cu_socket->so_rcv); 259177633Sdfr cs = mem_alloc(sizeof(*cs)); 260177633Sdfr SOCKBUF_LOCK(&cu->cu_socket->so_rcv); 261177633Sdfr if (so->so_upcall) { 262177633Sdfr /* 263177633Sdfr * We have lost a race with some other client. 264177633Sdfr */ 265177633Sdfr mem_free(cs, sizeof(*cs)); 266177633Sdfr goto recheck_socket; 267177633Sdfr } 268177633Sdfr mtx_init(&cs->cs_lock, "cs->cs_lock", NULL, MTX_DEF); 269177633Sdfr cs->cs_refs = 1; 270177633Sdfr TAILQ_INIT(&cs->cs_pending); 271177633Sdfr so->so_upcallarg = cs; 272177633Sdfr so->so_upcall = clnt_dg_soupcall; 273177633Sdfr so->so_rcv.sb_flags |= SB_UPCALL; 274177633Sdfr } 275177633Sdfr SOCKBUF_UNLOCK(&so->so_rcv); 276177633Sdfr 277177633Sdfr cl->cl_ops = &clnt_dg_ops; 278177633Sdfr cl->cl_private = (caddr_t)(void *)cu; 279177633Sdfr cl->cl_auth = authnone_create(); 280177633Sdfr cl->cl_tp = NULL; 281177633Sdfr cl->cl_netid = NULL; 282177633Sdfr return (cl); 283177633Sdfrerr2: 284177633Sdfr if (cl) { 285177633Sdfr mem_free(cl, sizeof (CLIENT)); 286177633Sdfr if (cu) 287177633Sdfr mem_free(cu, sizeof (*cu)); 288177633Sdfr } 289177633Sdfr return (NULL); 290177633Sdfr} 291177633Sdfr 292177633Sdfrstatic enum clnt_stat 293177633Sdfrclnt_dg_call( 294177633Sdfr CLIENT *cl, /* client handle */ 295177633Sdfr rpcproc_t proc, /* procedure number */ 296177633Sdfr xdrproc_t xargs, /* xdr routine for args */ 297177633Sdfr void *argsp, /* pointer to args */ 298177633Sdfr xdrproc_t xresults, /* xdr routine for results */ 299177633Sdfr void *resultsp, /* pointer to results */ 300177633Sdfr struct timeval utimeout) /* seconds to wait before giving up */ 301177633Sdfr{ 302177633Sdfr struct cu_data *cu = (struct cu_data *)cl->cl_private; 303177633Sdfr struct cu_socket *cs = (struct cu_socket *) cu->cu_socket->so_upcallarg; 304177633Sdfr XDR xdrs; 305177633Sdfr struct rpc_msg reply_msg; 306177633Sdfr bool_t ok; 307177633Sdfr int nrefreshes = 2; /* number of times to refresh cred */ 308177633Sdfr struct timeval timeout; 309177633Sdfr struct timeval retransmit_time; 310177633Sdfr struct timeval next_sendtime, starttime, time_waited, tv; 311177633Sdfr struct sockaddr *sa; 312177633Sdfr socklen_t salen; 313177633Sdfr uint32_t xid; 314177633Sdfr struct mbuf *mreq = NULL; 315177633Sdfr struct cu_request cr; 316177633Sdfr int error; 317177633Sdfr 318177633Sdfr mtx_lock(&cs->cs_lock); 319177633Sdfr 320177633Sdfr cr.cr_mrep = NULL; 321177633Sdfr cr.cr_error = 0; 322177633Sdfr 323177633Sdfr if (cu->cu_total.tv_usec == -1) { 324177633Sdfr timeout = utimeout; /* use supplied timeout */ 325177633Sdfr } else { 326177633Sdfr timeout = cu->cu_total; /* use default timeout */ 327177633Sdfr } 328177633Sdfr 329177633Sdfr if (cu->cu_connect && !cu->cu_connected) { 330177633Sdfr mtx_unlock(&cs->cs_lock); 331177633Sdfr error = soconnect(cu->cu_socket, 332177633Sdfr (struct sockaddr *)&cu->cu_raddr, curthread); 333177633Sdfr mtx_lock(&cs->cs_lock); 334177633Sdfr if (error) { 335177633Sdfr cu->cu_error.re_errno = error; 336177633Sdfr cu->cu_error.re_status = RPC_CANTSEND; 337177633Sdfr goto out; 338177633Sdfr } 339177633Sdfr cu->cu_connected = 1; 340177633Sdfr } 341177633Sdfr if (cu->cu_connected) { 342177633Sdfr sa = NULL; 343177633Sdfr salen = 0; 344177633Sdfr } else { 345177633Sdfr sa = (struct sockaddr *)&cu->cu_raddr; 346177633Sdfr salen = cu->cu_rlen; 347177633Sdfr } 348177633Sdfr time_waited.tv_sec = 0; 349177633Sdfr time_waited.tv_usec = 0; 350177633Sdfr retransmit_time = next_sendtime = cu->cu_wait; 351177633Sdfr 352177633Sdfr getmicrotime(&starttime); 353177633Sdfr 354177633Sdfrcall_again: 355177633Sdfr mtx_assert(&cs->cs_lock, MA_OWNED); 356177633Sdfr 357177633Sdfr cu->cu_xid++; 358177633Sdfr xid = cu->cu_xid; 359177633Sdfr 360177633Sdfrsend_again: 361177633Sdfr mtx_unlock(&cs->cs_lock); 362177633Sdfr 363177633Sdfr MGETHDR(mreq, M_WAIT, MT_DATA); 364177633Sdfr MCLGET(mreq, M_WAIT); 365177633Sdfr mreq->m_len = 0; 366177633Sdfr m_append(mreq, cu->cu_mcalllen, cu->cu_mcallc); 367177633Sdfr 368177633Sdfr /* 369177633Sdfr * The XID is the first thing in the request. 370177633Sdfr */ 371177633Sdfr *mtod(mreq, uint32_t *) = htonl(xid); 372177633Sdfr 373177633Sdfr xdrmbuf_create(&xdrs, mreq, XDR_ENCODE); 374177633Sdfr 375177633Sdfr if (cu->cu_async == TRUE && xargs == NULL) 376177633Sdfr goto get_reply; 377177633Sdfr 378177633Sdfr if ((! XDR_PUTINT32(&xdrs, &proc)) || 379177633Sdfr (! AUTH_MARSHALL(cl->cl_auth, &xdrs)) || 380177633Sdfr (! (*xargs)(&xdrs, argsp))) { 381177633Sdfr cu->cu_error.re_status = RPC_CANTENCODEARGS; 382177633Sdfr mtx_lock(&cs->cs_lock); 383177633Sdfr goto out; 384177633Sdfr } 385177633Sdfr m_fixhdr(mreq); 386177633Sdfr 387177633Sdfr cr.cr_xid = xid; 388177633Sdfr mtx_lock(&cs->cs_lock); 389177633Sdfr TAILQ_INSERT_TAIL(&cs->cs_pending, &cr, cr_link); 390177633Sdfr mtx_unlock(&cs->cs_lock); 391177633Sdfr 392177633Sdfr /* 393177633Sdfr * sosend consumes mreq. 394177633Sdfr */ 395177633Sdfr error = sosend(cu->cu_socket, sa, NULL, mreq, NULL, 0, curthread); 396177633Sdfr mreq = NULL; 397177633Sdfr 398177633Sdfr /* 399177633Sdfr * sub-optimal code appears here because we have 400177633Sdfr * some clock time to spare while the packets are in flight. 401177633Sdfr * (We assume that this is actually only executed once.) 402177633Sdfr */ 403177633Sdfr reply_msg.acpted_rply.ar_verf = _null_auth; 404177633Sdfr reply_msg.acpted_rply.ar_results.where = resultsp; 405177633Sdfr reply_msg.acpted_rply.ar_results.proc = xresults; 406177633Sdfr 407177633Sdfr mtx_lock(&cs->cs_lock); 408177633Sdfr if (error) { 409177633Sdfr TAILQ_REMOVE(&cs->cs_pending, &cr, cr_link); 410177633Sdfr 411177633Sdfr cu->cu_error.re_errno = error; 412177633Sdfr cu->cu_error.re_status = RPC_CANTSEND; 413177633Sdfr goto out; 414177633Sdfr } 415177633Sdfr 416177633Sdfr /* 417177633Sdfr * Check to see if we got an upcall while waiting for the 418177633Sdfr * lock. In both these cases, the request has been removed 419177633Sdfr * from cs->cs_pending. 420177633Sdfr */ 421177633Sdfr if (cr.cr_error) { 422177633Sdfr cu->cu_error.re_errno = cr.cr_error; 423177633Sdfr cu->cu_error.re_status = RPC_CANTRECV; 424177633Sdfr goto out; 425177633Sdfr } 426177633Sdfr if (cr.cr_mrep) { 427177633Sdfr goto got_reply; 428177633Sdfr } 429177633Sdfr 430177633Sdfr /* 431177633Sdfr * Hack to provide rpc-based message passing 432177633Sdfr */ 433177633Sdfr if (timeout.tv_sec == 0 && timeout.tv_usec == 0) { 434177633Sdfr if (cr.cr_xid) 435177633Sdfr TAILQ_REMOVE(&cs->cs_pending, &cr, cr_link); 436177633Sdfr cu->cu_error.re_status = RPC_TIMEDOUT; 437177633Sdfr goto out; 438177633Sdfr } 439177633Sdfr 440177633Sdfrget_reply: 441177633Sdfr for (;;) { 442177633Sdfr /* Decide how long to wait. */ 443177633Sdfr if (timevalcmp(&next_sendtime, &timeout, <)) { 444177633Sdfr tv = next_sendtime; 445177633Sdfr } else { 446177633Sdfr tv = timeout; 447177633Sdfr } 448177633Sdfr timevalsub(&tv, &time_waited); 449177633Sdfr if (tv.tv_sec < 0 || tv.tv_usec < 0) 450177633Sdfr tv.tv_sec = tv.tv_usec = 0; 451177633Sdfr 452177633Sdfr error = msleep(&cr, &cs->cs_lock, cu->cu_waitflag, 453177633Sdfr cu->cu_waitchan, tvtohz(&tv)); 454177633Sdfr 455177633Sdfr if (!error) { 456177633Sdfr /* 457177633Sdfr * We were woken up by the upcall. If the 458177633Sdfr * upcall had a receive error, report that, 459177633Sdfr * otherwise we have a reply. 460177633Sdfr */ 461177633Sdfr if (cr.cr_error) { 462177633Sdfr cu->cu_error.re_errno = cr.cr_error; 463177633Sdfr cu->cu_error.re_status = RPC_CANTRECV; 464177633Sdfr goto out; 465177633Sdfr } 466177633Sdfr break; 467177633Sdfr } 468177633Sdfr 469177633Sdfr /* 470177633Sdfr * The sleep returned an error so our request is still 471177633Sdfr * on the list. If we got EWOULDBLOCK, we may want to 472177633Sdfr * re-send the request. 473177633Sdfr */ 474177633Sdfr if (error != EWOULDBLOCK) { 475177633Sdfr if (cr.cr_xid) 476177633Sdfr TAILQ_REMOVE(&cs->cs_pending, &cr, cr_link); 477177633Sdfr cu->cu_error.re_errno = error; 478177633Sdfr if (error == EINTR) 479177633Sdfr cu->cu_error.re_status = RPC_INTR; 480177633Sdfr else 481177633Sdfr cu->cu_error.re_status = RPC_CANTRECV; 482177633Sdfr goto out; 483177633Sdfr } 484177633Sdfr 485177633Sdfr getmicrotime(&tv); 486177633Sdfr time_waited = tv; 487177633Sdfr timevalsub(&time_waited, &starttime); 488177633Sdfr 489177633Sdfr /* Check for timeout. */ 490177633Sdfr if (timevalcmp(&time_waited, &timeout, >)) { 491177633Sdfr if (cr.cr_xid) 492177633Sdfr TAILQ_REMOVE(&cs->cs_pending, &cr, cr_link); 493177633Sdfr cu->cu_error.re_errno = EWOULDBLOCK; 494177633Sdfr cu->cu_error.re_status = RPC_TIMEDOUT; 495177633Sdfr goto out; 496177633Sdfr } 497177633Sdfr 498177633Sdfr /* Retransmit if necessary. */ 499177633Sdfr if (timevalcmp(&time_waited, &next_sendtime, >)) { 500177633Sdfr if (cr.cr_xid) 501177633Sdfr TAILQ_REMOVE(&cs->cs_pending, &cr, cr_link); 502177633Sdfr /* update retransmit_time */ 503177633Sdfr if (retransmit_time.tv_sec < RPC_MAX_BACKOFF) 504177633Sdfr timevaladd(&retransmit_time, &retransmit_time); 505177633Sdfr timevaladd(&next_sendtime, &retransmit_time); 506177633Sdfr goto send_again; 507177633Sdfr } 508177633Sdfr } 509177633Sdfr 510177633Sdfrgot_reply: 511177633Sdfr /* 512177633Sdfr * Now decode and validate the response. We need to drop the 513177633Sdfr * lock since xdr_replymsg may end up sleeping in malloc. 514177633Sdfr */ 515177633Sdfr mtx_unlock(&cs->cs_lock); 516177633Sdfr 517177633Sdfr xdrmbuf_create(&xdrs, cr.cr_mrep, XDR_DECODE); 518177633Sdfr ok = xdr_replymsg(&xdrs, &reply_msg); 519177633Sdfr XDR_DESTROY(&xdrs); 520177633Sdfr cr.cr_mrep = NULL; 521177633Sdfr 522177633Sdfr mtx_lock(&cs->cs_lock); 523177633Sdfr 524177633Sdfr if (ok) { 525177633Sdfr if ((reply_msg.rm_reply.rp_stat == MSG_ACCEPTED) && 526177633Sdfr (reply_msg.acpted_rply.ar_stat == SUCCESS)) 527177633Sdfr cu->cu_error.re_status = RPC_SUCCESS; 528177633Sdfr else 529177633Sdfr _seterr_reply(&reply_msg, &(cu->cu_error)); 530177633Sdfr 531177633Sdfr if (cu->cu_error.re_status == RPC_SUCCESS) { 532177633Sdfr if (! AUTH_VALIDATE(cl->cl_auth, 533177633Sdfr &reply_msg.acpted_rply.ar_verf)) { 534177633Sdfr cu->cu_error.re_status = RPC_AUTHERROR; 535177633Sdfr cu->cu_error.re_why = AUTH_INVALIDRESP; 536177633Sdfr } 537177633Sdfr if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) { 538177633Sdfr xdrs.x_op = XDR_FREE; 539177633Sdfr (void) xdr_opaque_auth(&xdrs, 540177633Sdfr &(reply_msg.acpted_rply.ar_verf)); 541177633Sdfr } 542177633Sdfr } /* end successful completion */ 543177633Sdfr /* 544177633Sdfr * If unsuccesful AND error is an authentication error 545177633Sdfr * then refresh credentials and try again, else break 546177633Sdfr */ 547177633Sdfr else if (cu->cu_error.re_status == RPC_AUTHERROR) 548177633Sdfr /* maybe our credentials need to be refreshed ... */ 549177633Sdfr if (nrefreshes > 0 && 550177633Sdfr AUTH_REFRESH(cl->cl_auth, &reply_msg)) { 551177633Sdfr nrefreshes--; 552177633Sdfr goto call_again; 553177633Sdfr } 554177633Sdfr /* end of unsuccessful completion */ 555177633Sdfr } /* end of valid reply message */ 556177633Sdfr else { 557177633Sdfr cu->cu_error.re_status = RPC_CANTDECODERES; 558177633Sdfr 559177633Sdfr } 560177633Sdfrout: 561177633Sdfr mtx_assert(&cs->cs_lock, MA_OWNED); 562177633Sdfr 563177633Sdfr if (mreq) 564177633Sdfr m_freem(mreq); 565177633Sdfr if (cr.cr_mrep) 566177633Sdfr m_freem(cr.cr_mrep); 567177633Sdfr 568177633Sdfr mtx_unlock(&cs->cs_lock); 569177633Sdfr return (cu->cu_error.re_status); 570177633Sdfr} 571177633Sdfr 572177633Sdfrstatic void 573177633Sdfrclnt_dg_geterr(CLIENT *cl, struct rpc_err *errp) 574177633Sdfr{ 575177633Sdfr struct cu_data *cu = (struct cu_data *)cl->cl_private; 576177633Sdfr 577177633Sdfr *errp = cu->cu_error; 578177633Sdfr} 579177633Sdfr 580177633Sdfrstatic bool_t 581177633Sdfrclnt_dg_freeres(CLIENT *cl, xdrproc_t xdr_res, void *res_ptr) 582177633Sdfr{ 583177633Sdfr XDR xdrs; 584177633Sdfr bool_t dummy; 585177633Sdfr 586177633Sdfr xdrs.x_op = XDR_FREE; 587177633Sdfr dummy = (*xdr_res)(&xdrs, res_ptr); 588177633Sdfr 589177633Sdfr return (dummy); 590177633Sdfr} 591177633Sdfr 592177633Sdfr/*ARGSUSED*/ 593177633Sdfrstatic void 594177633Sdfrclnt_dg_abort(CLIENT *h) 595177633Sdfr{ 596177633Sdfr} 597177633Sdfr 598177633Sdfrstatic bool_t 599177633Sdfrclnt_dg_control(CLIENT *cl, u_int request, void *info) 600177633Sdfr{ 601177633Sdfr struct cu_data *cu = (struct cu_data *)cl->cl_private; 602177633Sdfr struct cu_socket *cs = (struct cu_socket *) cu->cu_socket->so_upcallarg; 603177633Sdfr struct sockaddr *addr; 604177633Sdfr 605177633Sdfr mtx_lock(&cs->cs_lock); 606177633Sdfr 607177633Sdfr switch (request) { 608177633Sdfr case CLSET_FD_CLOSE: 609177633Sdfr cu->cu_closeit = TRUE; 610177633Sdfr mtx_unlock(&cs->cs_lock); 611177633Sdfr return (TRUE); 612177633Sdfr case CLSET_FD_NCLOSE: 613177633Sdfr cu->cu_closeit = FALSE; 614177633Sdfr mtx_unlock(&cs->cs_lock); 615177633Sdfr return (TRUE); 616177633Sdfr } 617177633Sdfr 618177633Sdfr /* for other requests which use info */ 619177633Sdfr if (info == NULL) { 620177633Sdfr mtx_unlock(&cs->cs_lock); 621177633Sdfr return (FALSE); 622177633Sdfr } 623177633Sdfr switch (request) { 624177633Sdfr case CLSET_TIMEOUT: 625177633Sdfr if (time_not_ok((struct timeval *)info)) { 626177633Sdfr mtx_unlock(&cs->cs_lock); 627177633Sdfr return (FALSE); 628177633Sdfr } 629177633Sdfr cu->cu_total = *(struct timeval *)info; 630177633Sdfr break; 631177633Sdfr case CLGET_TIMEOUT: 632177633Sdfr *(struct timeval *)info = cu->cu_total; 633177633Sdfr break; 634177633Sdfr case CLSET_RETRY_TIMEOUT: 635177633Sdfr if (time_not_ok((struct timeval *)info)) { 636177633Sdfr mtx_unlock(&cs->cs_lock); 637177633Sdfr return (FALSE); 638177633Sdfr } 639177633Sdfr cu->cu_wait = *(struct timeval *)info; 640177633Sdfr break; 641177633Sdfr case CLGET_RETRY_TIMEOUT: 642177633Sdfr *(struct timeval *)info = cu->cu_wait; 643177633Sdfr break; 644177633Sdfr case CLGET_SVC_ADDR: 645177633Sdfr /* 646177633Sdfr * Slightly different semantics to userland - we use 647177633Sdfr * sockaddr instead of netbuf. 648177633Sdfr */ 649177633Sdfr memcpy(info, &cu->cu_raddr, cu->cu_raddr.ss_len); 650177633Sdfr break; 651177633Sdfr case CLSET_SVC_ADDR: /* set to new address */ 652177633Sdfr addr = (struct sockaddr *)info; 653177633Sdfr (void) memcpy(&cu->cu_raddr, addr, addr->sa_len); 654177633Sdfr break; 655177633Sdfr case CLGET_XID: 656177633Sdfr *(uint32_t *)info = cu->cu_xid; 657177633Sdfr break; 658177633Sdfr 659177633Sdfr case CLSET_XID: 660177633Sdfr /* This will set the xid of the NEXT call */ 661177633Sdfr /* decrement by 1 as clnt_dg_call() increments once */ 662177633Sdfr cu->cu_xid = *(uint32_t *)info - 1; 663177633Sdfr break; 664177633Sdfr 665177633Sdfr case CLGET_VERS: 666177633Sdfr /* 667177633Sdfr * This RELIES on the information that, in the call body, 668177633Sdfr * the version number field is the fifth field from the 669177633Sdfr * begining of the RPC header. MUST be changed if the 670177633Sdfr * call_struct is changed 671177633Sdfr */ 672177633Sdfr *(uint32_t *)info = 673177633Sdfr ntohl(*(uint32_t *)(void *)(cu->cu_mcallc + 674177633Sdfr 4 * BYTES_PER_XDR_UNIT)); 675177633Sdfr break; 676177633Sdfr 677177633Sdfr case CLSET_VERS: 678177633Sdfr *(uint32_t *)(void *)(cu->cu_mcallc + 4 * BYTES_PER_XDR_UNIT) 679177633Sdfr = htonl(*(uint32_t *)info); 680177633Sdfr break; 681177633Sdfr 682177633Sdfr case CLGET_PROG: 683177633Sdfr /* 684177633Sdfr * This RELIES on the information that, in the call body, 685177633Sdfr * the program number field is the fourth field from the 686177633Sdfr * begining of the RPC header. MUST be changed if the 687177633Sdfr * call_struct is changed 688177633Sdfr */ 689177633Sdfr *(uint32_t *)info = 690177633Sdfr ntohl(*(uint32_t *)(void *)(cu->cu_mcallc + 691177633Sdfr 3 * BYTES_PER_XDR_UNIT)); 692177633Sdfr break; 693177633Sdfr 694177633Sdfr case CLSET_PROG: 695177633Sdfr *(uint32_t *)(void *)(cu->cu_mcallc + 3 * BYTES_PER_XDR_UNIT) 696177633Sdfr = htonl(*(uint32_t *)info); 697177633Sdfr break; 698177633Sdfr case CLSET_ASYNC: 699177633Sdfr cu->cu_async = *(int *)info; 700177633Sdfr break; 701177633Sdfr case CLSET_CONNECT: 702177633Sdfr cu->cu_connect = *(int *)info; 703177633Sdfr break; 704177633Sdfr case CLSET_WAITCHAN: 705177633Sdfr cu->cu_waitchan = *(const char **)info; 706177633Sdfr break; 707177633Sdfr case CLGET_WAITCHAN: 708177633Sdfr *(const char **) info = cu->cu_waitchan; 709177633Sdfr break; 710177633Sdfr case CLSET_INTERRUPTIBLE: 711177633Sdfr if (*(int *) info) 712177633Sdfr cu->cu_waitflag = PCATCH; 713177633Sdfr else 714177633Sdfr cu->cu_waitflag = 0; 715177633Sdfr break; 716177633Sdfr case CLGET_INTERRUPTIBLE: 717177633Sdfr if (cu->cu_waitflag) 718177633Sdfr *(int *) info = TRUE; 719177633Sdfr else 720177633Sdfr *(int *) info = FALSE; 721177633Sdfr break; 722177633Sdfr default: 723177633Sdfr mtx_unlock(&cs->cs_lock); 724177633Sdfr return (FALSE); 725177633Sdfr } 726177633Sdfr mtx_unlock(&cs->cs_lock); 727177633Sdfr return (TRUE); 728177633Sdfr} 729177633Sdfr 730177633Sdfrstatic void 731177633Sdfrclnt_dg_destroy(CLIENT *cl) 732177633Sdfr{ 733177633Sdfr struct cu_data *cu = (struct cu_data *)cl->cl_private; 734177633Sdfr struct cu_socket *cs = (struct cu_socket *) cu->cu_socket->so_upcallarg; 735177633Sdfr struct socket *so = NULL; 736177633Sdfr bool_t lastsocketref; 737177633Sdfr 738177633Sdfr SOCKBUF_LOCK(&cu->cu_socket->so_rcv); 739177633Sdfr 740177633Sdfr mtx_lock(&cs->cs_lock); 741177633Sdfr cs->cs_refs--; 742177633Sdfr if (cs->cs_refs == 0) { 743177633Sdfr cu->cu_socket->so_upcallarg = NULL; 744177633Sdfr cu->cu_socket->so_upcall = NULL; 745177633Sdfr cu->cu_socket->so_rcv.sb_flags &= ~SB_UPCALL; 746177633Sdfr mtx_destroy(&cs->cs_lock); 747177633Sdfr SOCKBUF_UNLOCK(&cu->cu_socket->so_rcv); 748177633Sdfr mem_free(cs, sizeof(*cs)); 749177633Sdfr lastsocketref = TRUE; 750177633Sdfr } else { 751177633Sdfr mtx_unlock(&cs->cs_lock); 752177633Sdfr SOCKBUF_UNLOCK(&cu->cu_socket->so_rcv); 753177633Sdfr lastsocketref = FALSE; 754177633Sdfr } 755177633Sdfr 756177633Sdfr if (cu->cu_closeit) { 757177633Sdfr KASSERT(lastsocketref, ("clnt_dg_destroy(): closing a socket " 758177633Sdfr "shared with other clients")); 759177633Sdfr so = cu->cu_socket; 760177633Sdfr cu->cu_socket = NULL; 761177633Sdfr } 762177633Sdfr 763177633Sdfr if (so) 764177633Sdfr soclose(so); 765177633Sdfr 766177633Sdfr if (cl->cl_netid && cl->cl_netid[0]) 767177633Sdfr mem_free(cl->cl_netid, strlen(cl->cl_netid) +1); 768177633Sdfr if (cl->cl_tp && cl->cl_tp[0]) 769177633Sdfr mem_free(cl->cl_tp, strlen(cl->cl_tp) +1); 770177633Sdfr mem_free(cu, sizeof (*cu)); 771177633Sdfr mem_free(cl, sizeof (CLIENT)); 772177633Sdfr} 773177633Sdfr 774177633Sdfr/* 775177633Sdfr * Make sure that the time is not garbage. -1 value is allowed. 776177633Sdfr */ 777177633Sdfrstatic bool_t 778177633Sdfrtime_not_ok(struct timeval *t) 779177633Sdfr{ 780177633Sdfr return (t->tv_sec < -1 || t->tv_sec > 100000000 || 781177633Sdfr t->tv_usec < -1 || t->tv_usec > 1000000); 782177633Sdfr} 783177633Sdfr 784177633Sdfrvoid 785177633Sdfrclnt_dg_soupcall(struct socket *so, void *arg, int waitflag) 786177633Sdfr{ 787177633Sdfr struct cu_socket *cs = (struct cu_socket *) arg; 788177633Sdfr struct uio uio; 789177633Sdfr struct mbuf *m; 790177633Sdfr struct mbuf *control; 791177633Sdfr struct cu_request *cr; 792177633Sdfr int error, rcvflag, foundreq; 793177633Sdfr uint32_t xid; 794177633Sdfr 795177633Sdfr uio.uio_resid = 1000000000; 796177633Sdfr uio.uio_td = curthread; 797177633Sdfr do { 798177633Sdfr m = NULL; 799177633Sdfr control = NULL; 800177633Sdfr rcvflag = MSG_DONTWAIT; 801177633Sdfr error = soreceive(so, NULL, &uio, &m, &control, &rcvflag); 802177633Sdfr if (control) 803177633Sdfr m_freem(control); 804177633Sdfr 805177633Sdfr if (error == EWOULDBLOCK) 806177633Sdfr break; 807177633Sdfr 808177633Sdfr /* 809177633Sdfr * If there was an error, wake up all pending 810177633Sdfr * requests. 811177633Sdfr */ 812177633Sdfr if (error) { 813177633Sdfr mtx_lock(&cs->cs_lock); 814177633Sdfr TAILQ_FOREACH(cr, &cs->cs_pending, cr_link) { 815177633Sdfr cr->cr_error = error; 816177633Sdfr wakeup(cr); 817177633Sdfr } 818177633Sdfr TAILQ_INIT(&cs->cs_pending); 819177633Sdfr mtx_unlock(&cs->cs_lock); 820177633Sdfr break; 821177633Sdfr } 822177633Sdfr 823177633Sdfr /* 824177633Sdfr * The XID is in the first uint32_t of the reply. 825177633Sdfr */ 826177633Sdfr m = m_pullup(m, sizeof(xid)); 827177633Sdfr if (!m) 828177633Sdfr break; 829177633Sdfr xid = ntohl(*mtod(m, uint32_t *)); 830177633Sdfr 831177633Sdfr /* 832177633Sdfr * Attempt to match this reply with a pending request. 833177633Sdfr */ 834177633Sdfr mtx_lock(&cs->cs_lock); 835177633Sdfr foundreq = 0; 836177633Sdfr TAILQ_FOREACH(cr, &cs->cs_pending, cr_link) { 837177633Sdfr if (cr->cr_xid == xid) { 838177633Sdfr /* 839177633Sdfr * This one matches. We snip it out of 840177633Sdfr * the pending list and leave the 841177633Sdfr * reply mbuf in cr->cr_mrep. Set the 842177633Sdfr * XID to zero so that clnt_dg_call 843177633Sdfr * can know not to repeat the 844177633Sdfr * TAILQ_REMOVE. 845177633Sdfr */ 846177633Sdfr TAILQ_REMOVE(&cs->cs_pending, cr, cr_link); 847177633Sdfr cr->cr_xid = 0; 848177633Sdfr cr->cr_mrep = m; 849177633Sdfr cr->cr_error = 0; 850177633Sdfr foundreq = 1; 851177633Sdfr wakeup(cr); 852177633Sdfr break; 853177633Sdfr } 854177633Sdfr } 855177633Sdfr mtx_unlock(&cs->cs_lock); 856177633Sdfr 857177633Sdfr /* 858177633Sdfr * If we didn't find the matching request, just drop 859177633Sdfr * it - its probably a repeated reply. 860177633Sdfr */ 861177633Sdfr if (!foundreq) 862177633Sdfr m_freem(m); 863177633Sdfr } while (m); 864177633Sdfr} 865177633Sdfr 866