1177633Sdfr/* $NetBSD: clnt_dg.c,v 1.4 2000/07/14 08:40:41 fvdl Exp $ */ 2177633Sdfr 3261046Smav/*- 4261046Smav * Copyright (c) 2009, Sun Microsystems, Inc. 5261046Smav * All rights reserved. 6261046Smav * 7261046Smav * Redistribution and use in source and binary forms, with or without 8261046Smav * modification, are permitted provided that the following conditions are met: 9261046Smav * - Redistributions of source code must retain the above copyright notice, 10261046Smav * this list of conditions and the following disclaimer. 11261046Smav * - Redistributions in binary form must reproduce the above copyright notice, 12261046Smav * this list of conditions and the following disclaimer in the documentation 13261046Smav * and/or other materials provided with the distribution. 14261046Smav * - Neither the name of Sun Microsystems, Inc. nor the names of its 15261046Smav * contributors may be used to endorse or promote products derived 16261046Smav * from this software without specific prior written permission. 17177633Sdfr * 18261046Smav * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19261046Smav * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20261046Smav * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21261046Smav * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22261046Smav * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23261046Smav * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24261046Smav * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25261046Smav * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26261046Smav * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27261046Smav * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28261046Smav * POSSIBILITY OF SUCH DAMAGE. 29177633Sdfr */ 30177633Sdfr/* 31177633Sdfr * Copyright (c) 1986-1991 by Sun Microsystems Inc. 32177633Sdfr */ 33177633Sdfr 34177633Sdfr#if defined(LIBC_SCCS) && !defined(lint) 35177633Sdfr#ident "@(#)clnt_dg.c 1.23 94/04/22 SMI" 36177633Sdfrstatic char sccsid[] = "@(#)clnt_dg.c 1.19 89/03/16 Copyr 1988 Sun Micro"; 37177633Sdfr#endif 38177633Sdfr#include <sys/cdefs.h> 39177633Sdfr__FBSDID("$FreeBSD$"); 40177633Sdfr 41177633Sdfr/* 42177633Sdfr * Implements a connectionless client side RPC. 43177633Sdfr */ 44177633Sdfr 45177633Sdfr#include <sys/param.h> 46177633Sdfr#include <sys/systm.h> 47180025Sdfr#include <sys/kernel.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 59196503Szec#include <net/vnet.h> 60196503Szec 61177633Sdfr#include <rpc/rpc.h> 62177685Sdfr#include <rpc/rpc_com.h> 63177633Sdfr 64177633Sdfr 65177633Sdfr#ifdef _FREEFALL_CONFIG 66177633Sdfr/* 67177633Sdfr * Disable RPC exponential back-off for FreeBSD.org systems. 68177633Sdfr */ 69177633Sdfr#define RPC_MAX_BACKOFF 1 /* second */ 70177633Sdfr#else 71177633Sdfr#define RPC_MAX_BACKOFF 30 /* seconds */ 72177633Sdfr#endif 73177633Sdfr 74177633Sdfrstatic bool_t time_not_ok(struct timeval *); 75180025Sdfrstatic enum clnt_stat clnt_dg_call(CLIENT *, struct rpc_callextra *, 76184588Sdfr rpcproc_t, struct mbuf *, struct mbuf **, struct timeval); 77177633Sdfrstatic void clnt_dg_geterr(CLIENT *, struct rpc_err *); 78177633Sdfrstatic bool_t clnt_dg_freeres(CLIENT *, xdrproc_t, void *); 79177633Sdfrstatic void clnt_dg_abort(CLIENT *); 80177633Sdfrstatic bool_t clnt_dg_control(CLIENT *, u_int, void *); 81184588Sdfrstatic void clnt_dg_close(CLIENT *); 82177633Sdfrstatic void clnt_dg_destroy(CLIENT *); 83193272Sjhbstatic int clnt_dg_soupcall(struct socket *so, void *arg, int waitflag); 84177633Sdfr 85177633Sdfrstatic struct clnt_ops clnt_dg_ops = { 86177633Sdfr .cl_call = clnt_dg_call, 87177633Sdfr .cl_abort = clnt_dg_abort, 88177633Sdfr .cl_geterr = clnt_dg_geterr, 89177633Sdfr .cl_freeres = clnt_dg_freeres, 90184588Sdfr .cl_close = clnt_dg_close, 91177633Sdfr .cl_destroy = clnt_dg_destroy, 92177633Sdfr .cl_control = clnt_dg_control 93177633Sdfr}; 94177633Sdfr 95177633Sdfr/* 96180025Sdfr * A pending RPC request which awaits a reply. Requests which have 97180025Sdfr * received their reply will have cr_xid set to zero and cr_mrep to 98180025Sdfr * the mbuf chain of the reply. 99177633Sdfr */ 100177633Sdfrstruct cu_request { 101177633Sdfr TAILQ_ENTRY(cu_request) cr_link; 102180025Sdfr CLIENT *cr_client; /* owner */ 103177633Sdfr uint32_t cr_xid; /* XID of request */ 104177633Sdfr struct mbuf *cr_mrep; /* reply received by upcall */ 105177633Sdfr int cr_error; /* any error from upcall */ 106184588Sdfr char cr_verf[MAX_AUTH_BYTES]; /* reply verf */ 107177633Sdfr}; 108177633Sdfr 109177633SdfrTAILQ_HEAD(cu_request_list, cu_request); 110177633Sdfr 111177633Sdfr#define MCALL_MSG_SIZE 24 112177633Sdfr 113177633Sdfr/* 114193272Sjhb * This structure is pointed to by the socket buffer's sb_upcallarg 115177633Sdfr * member. It is separate from the client private data to facilitate 116177633Sdfr * multiple clients sharing the same socket. The cs_lock mutex is used 117177633Sdfr * to protect all fields of this structure, the socket's receive 118177633Sdfr * buffer SOCKBUF_LOCK is used to ensure that exactly one of these 119177633Sdfr * structures is installed on the socket. 120177633Sdfr */ 121177633Sdfrstruct cu_socket { 122177633Sdfr struct mtx cs_lock; 123177633Sdfr int cs_refs; /* Count of clients */ 124177633Sdfr struct cu_request_list cs_pending; /* Requests awaiting replies */ 125193437Srmacklem int cs_upcallrefs; /* Refcnt of upcalls in prog.*/ 126177633Sdfr}; 127177633Sdfr 128193437Srmacklemstatic void clnt_dg_upcallsdone(struct socket *, struct cu_socket *); 129193437Srmacklem 130177633Sdfr/* 131177633Sdfr * Private data kept per client handle 132177633Sdfr */ 133177633Sdfrstruct cu_data { 134180025Sdfr int cu_threads; /* # threads in clnt_vc_call */ 135184588Sdfr bool_t cu_closing; /* TRUE if we are closing */ 136184588Sdfr bool_t cu_closed; /* TRUE if we are closed */ 137177633Sdfr struct socket *cu_socket; /* connection socket */ 138177633Sdfr bool_t cu_closeit; /* opened by library */ 139177633Sdfr struct sockaddr_storage cu_raddr; /* remote address */ 140177633Sdfr int cu_rlen; 141177633Sdfr struct timeval cu_wait; /* retransmit interval */ 142177633Sdfr struct timeval cu_total; /* total time for the call */ 143177633Sdfr struct rpc_err cu_error; 144177633Sdfr uint32_t cu_xid; 145177633Sdfr char cu_mcallc[MCALL_MSG_SIZE]; /* marshalled callmsg */ 146177633Sdfr size_t cu_mcalllen; 147177633Sdfr size_t cu_sendsz; /* send size */ 148177633Sdfr size_t cu_recvsz; /* recv size */ 149177633Sdfr int cu_async; 150177633Sdfr int cu_connect; /* Use connect(). */ 151177633Sdfr int cu_connected; /* Have done connect(). */ 152177633Sdfr const char *cu_waitchan; 153177633Sdfr int cu_waitflag; 154184588Sdfr int cu_cwnd; /* congestion window */ 155184588Sdfr int cu_sent; /* number of in-flight RPCs */ 156184588Sdfr bool_t cu_cwnd_wait; 157177633Sdfr}; 158177633Sdfr 159184588Sdfr#define CWNDSCALE 256 160184588Sdfr#define MAXCWND (32 * CWNDSCALE) 161184588Sdfr 162177633Sdfr/* 163177633Sdfr * Connection less client creation returns with client handle parameters. 164177633Sdfr * Default options are set, which the user can change using clnt_control(). 165177633Sdfr * fd should be open and bound. 166177633Sdfr * NB: The rpch->cl_auth is initialized to null authentication. 167177633Sdfr * Caller may wish to set this something more useful. 168177633Sdfr * 169177633Sdfr * sendsz and recvsz are the maximum allowable packet sizes that can be 170177633Sdfr * sent and received. Normally they are the same, but they can be 171177633Sdfr * changed to improve the program efficiency and buffer allocation. 172177633Sdfr * If they are 0, use the transport default. 173177633Sdfr * 174177633Sdfr * If svcaddr is NULL, returns NULL. 175177633Sdfr */ 176177633SdfrCLIENT * 177177633Sdfrclnt_dg_create( 178177633Sdfr struct socket *so, 179177633Sdfr struct sockaddr *svcaddr, /* servers address */ 180177633Sdfr rpcprog_t program, /* program number */ 181177633Sdfr rpcvers_t version, /* version number */ 182177633Sdfr size_t sendsz, /* buffer recv size */ 183177633Sdfr size_t recvsz) /* buffer send size */ 184177633Sdfr{ 185177633Sdfr CLIENT *cl = NULL; /* client handle */ 186177633Sdfr struct cu_data *cu = NULL; /* private data */ 187177633Sdfr struct cu_socket *cs = NULL; 188193272Sjhb struct sockbuf *sb; 189177633Sdfr struct timeval now; 190177633Sdfr struct rpc_msg call_msg; 191177633Sdfr struct __rpc_sockinfo si; 192177633Sdfr XDR xdrs; 193213756Srmacklem int error; 194177633Sdfr 195177633Sdfr if (svcaddr == NULL) { 196177633Sdfr rpc_createerr.cf_stat = RPC_UNKNOWNADDR; 197177633Sdfr return (NULL); 198177633Sdfr } 199177633Sdfr 200177633Sdfr if (!__rpc_socket2sockinfo(so, &si)) { 201177633Sdfr rpc_createerr.cf_stat = RPC_TLIERROR; 202177633Sdfr rpc_createerr.cf_error.re_errno = 0; 203177633Sdfr return (NULL); 204177633Sdfr } 205177633Sdfr 206177633Sdfr /* 207177633Sdfr * Find the receive and the send size 208177633Sdfr */ 209177633Sdfr sendsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsz); 210177633Sdfr recvsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsz); 211177633Sdfr if ((sendsz == 0) || (recvsz == 0)) { 212177633Sdfr rpc_createerr.cf_stat = RPC_TLIERROR; /* XXX */ 213177633Sdfr rpc_createerr.cf_error.re_errno = 0; 214177633Sdfr return (NULL); 215177633Sdfr } 216177633Sdfr 217177633Sdfr cl = mem_alloc(sizeof (CLIENT)); 218177633Sdfr 219177633Sdfr /* 220177633Sdfr * Should be multiple of 4 for XDR. 221177633Sdfr */ 222177633Sdfr sendsz = ((sendsz + 3) / 4) * 4; 223177633Sdfr recvsz = ((recvsz + 3) / 4) * 4; 224177633Sdfr cu = mem_alloc(sizeof (*cu)); 225180025Sdfr cu->cu_threads = 0; 226180025Sdfr cu->cu_closing = FALSE; 227184588Sdfr cu->cu_closed = FALSE; 228177633Sdfr (void) memcpy(&cu->cu_raddr, svcaddr, (size_t)svcaddr->sa_len); 229177633Sdfr cu->cu_rlen = svcaddr->sa_len; 230177633Sdfr /* Other values can also be set through clnt_control() */ 231180025Sdfr cu->cu_wait.tv_sec = 3; /* heuristically chosen */ 232177633Sdfr cu->cu_wait.tv_usec = 0; 233177633Sdfr cu->cu_total.tv_sec = -1; 234177633Sdfr cu->cu_total.tv_usec = -1; 235177633Sdfr cu->cu_sendsz = sendsz; 236177633Sdfr cu->cu_recvsz = recvsz; 237177633Sdfr cu->cu_async = FALSE; 238177633Sdfr cu->cu_connect = FALSE; 239177633Sdfr cu->cu_connected = FALSE; 240177633Sdfr cu->cu_waitchan = "rpcrecv"; 241177633Sdfr cu->cu_waitflag = 0; 242184588Sdfr cu->cu_cwnd = MAXCWND / 2; 243184588Sdfr cu->cu_sent = 0; 244184588Sdfr cu->cu_cwnd_wait = FALSE; 245177633Sdfr (void) getmicrotime(&now); 246177633Sdfr cu->cu_xid = __RPC_GETXID(&now); 247177633Sdfr call_msg.rm_xid = cu->cu_xid; 248177633Sdfr call_msg.rm_call.cb_prog = program; 249177633Sdfr call_msg.rm_call.cb_vers = version; 250177633Sdfr xdrmem_create(&xdrs, cu->cu_mcallc, MCALL_MSG_SIZE, XDR_ENCODE); 251177633Sdfr if (! xdr_callhdr(&xdrs, &call_msg)) { 252177633Sdfr rpc_createerr.cf_stat = RPC_CANTENCODEARGS; /* XXX */ 253177633Sdfr rpc_createerr.cf_error.re_errno = 0; 254177633Sdfr goto err2; 255177633Sdfr } 256201758Smbr cu->cu_mcalllen = XDR_GETPOS(&xdrs); 257177633Sdfr 258177633Sdfr /* 259177633Sdfr * By default, closeit is always FALSE. It is users responsibility 260177633Sdfr * to do a close on it, else the user may use clnt_control 261177633Sdfr * to let clnt_destroy do it for him/her. 262177633Sdfr */ 263177633Sdfr cu->cu_closeit = FALSE; 264177633Sdfr cu->cu_socket = so; 265213756Srmacklem error = soreserve(so, (u_long)sendsz, (u_long)recvsz); 266213756Srmacklem if (error != 0) { 267213756Srmacklem rpc_createerr.cf_stat = RPC_FAILED; 268213756Srmacklem rpc_createerr.cf_error.re_errno = error; 269213756Srmacklem goto err2; 270213756Srmacklem } 271177633Sdfr 272193272Sjhb sb = &so->so_rcv; 273177633Sdfr SOCKBUF_LOCK(&so->so_rcv); 274177633Sdfrrecheck_socket: 275193272Sjhb if (sb->sb_upcall) { 276193272Sjhb if (sb->sb_upcall != clnt_dg_soupcall) { 277177633Sdfr SOCKBUF_UNLOCK(&so->so_rcv); 278177633Sdfr printf("clnt_dg_create(): socket already has an incompatible upcall\n"); 279177633Sdfr goto err2; 280177633Sdfr } 281193272Sjhb cs = (struct cu_socket *) sb->sb_upcallarg; 282177633Sdfr mtx_lock(&cs->cs_lock); 283177633Sdfr cs->cs_refs++; 284177633Sdfr mtx_unlock(&cs->cs_lock); 285177633Sdfr } else { 286177633Sdfr /* 287177633Sdfr * We are the first on this socket - allocate the 288177633Sdfr * structure and install it in the socket. 289177633Sdfr */ 290193272Sjhb SOCKBUF_UNLOCK(&so->so_rcv); 291177633Sdfr cs = mem_alloc(sizeof(*cs)); 292193272Sjhb SOCKBUF_LOCK(&so->so_rcv); 293193272Sjhb if (sb->sb_upcall) { 294177633Sdfr /* 295177633Sdfr * We have lost a race with some other client. 296177633Sdfr */ 297177633Sdfr mem_free(cs, sizeof(*cs)); 298177633Sdfr goto recheck_socket; 299177633Sdfr } 300177633Sdfr mtx_init(&cs->cs_lock, "cs->cs_lock", NULL, MTX_DEF); 301177633Sdfr cs->cs_refs = 1; 302193437Srmacklem cs->cs_upcallrefs = 0; 303177633Sdfr TAILQ_INIT(&cs->cs_pending); 304193272Sjhb soupcall_set(so, SO_RCV, clnt_dg_soupcall, cs); 305177633Sdfr } 306177633Sdfr SOCKBUF_UNLOCK(&so->so_rcv); 307177633Sdfr 308180025Sdfr cl->cl_refs = 1; 309177633Sdfr cl->cl_ops = &clnt_dg_ops; 310177633Sdfr cl->cl_private = (caddr_t)(void *)cu; 311177633Sdfr cl->cl_auth = authnone_create(); 312177633Sdfr cl->cl_tp = NULL; 313177633Sdfr cl->cl_netid = NULL; 314177633Sdfr return (cl); 315177633Sdfrerr2: 316303692Sngie mem_free(cl, sizeof (CLIENT)); 317303692Sngie mem_free(cu, sizeof (*cu)); 318303692Sngie 319177633Sdfr return (NULL); 320177633Sdfr} 321177633Sdfr 322177633Sdfrstatic enum clnt_stat 323177633Sdfrclnt_dg_call( 324180025Sdfr CLIENT *cl, /* client handle */ 325180025Sdfr struct rpc_callextra *ext, /* call metadata */ 326177633Sdfr rpcproc_t proc, /* procedure number */ 327184588Sdfr struct mbuf *args, /* pointer to args */ 328184588Sdfr struct mbuf **resultsp, /* pointer to results */ 329177633Sdfr struct timeval utimeout) /* seconds to wait before giving up */ 330177633Sdfr{ 331177633Sdfr struct cu_data *cu = (struct cu_data *)cl->cl_private; 332193272Sjhb struct cu_socket *cs; 333184588Sdfr struct rpc_timers *rt; 334180025Sdfr AUTH *auth; 335184588Sdfr struct rpc_err *errp; 336184588Sdfr enum clnt_stat stat; 337177633Sdfr XDR xdrs; 338177633Sdfr struct rpc_msg reply_msg; 339177633Sdfr bool_t ok; 340180025Sdfr int retrans; /* number of re-transmits so far */ 341177633Sdfr int nrefreshes = 2; /* number of times to refresh cred */ 342180025Sdfr struct timeval *tvp; 343180025Sdfr int timeout; 344180025Sdfr int retransmit_time; 345184588Sdfr int next_sendtime, starttime, rtt, time_waited, tv = 0; 346177633Sdfr struct sockaddr *sa; 347177633Sdfr socklen_t salen; 348184588Sdfr uint32_t xid = 0; 349184588Sdfr struct mbuf *mreq = NULL, *results; 350180025Sdfr struct cu_request *cr; 351177633Sdfr int error; 352177633Sdfr 353193272Sjhb cs = cu->cu_socket->so_rcv.sb_upcallarg; 354180025Sdfr cr = malloc(sizeof(struct cu_request), M_RPC, M_WAITOK); 355180025Sdfr 356177633Sdfr mtx_lock(&cs->cs_lock); 357177633Sdfr 358184588Sdfr if (cu->cu_closing || cu->cu_closed) { 359180025Sdfr mtx_unlock(&cs->cs_lock); 360180025Sdfr free(cr, M_RPC); 361180025Sdfr return (RPC_CANTSEND); 362180025Sdfr } 363180025Sdfr cu->cu_threads++; 364177633Sdfr 365184588Sdfr if (ext) { 366180025Sdfr auth = ext->rc_auth; 367184588Sdfr errp = &ext->rc_err; 368184588Sdfr } else { 369180025Sdfr auth = cl->cl_auth; 370184588Sdfr errp = &cu->cu_error; 371184588Sdfr } 372180025Sdfr 373180025Sdfr cr->cr_client = cl; 374180025Sdfr cr->cr_mrep = NULL; 375180025Sdfr cr->cr_error = 0; 376180025Sdfr 377177633Sdfr if (cu->cu_total.tv_usec == -1) { 378180025Sdfr tvp = &utimeout; /* use supplied timeout */ 379177633Sdfr } else { 380180025Sdfr tvp = &cu->cu_total; /* use default timeout */ 381177633Sdfr } 382180025Sdfr if (tvp->tv_sec || tvp->tv_usec) 383180025Sdfr timeout = tvtohz(tvp); 384180025Sdfr else 385180025Sdfr timeout = 0; 386177633Sdfr 387177633Sdfr if (cu->cu_connect && !cu->cu_connected) { 388177633Sdfr mtx_unlock(&cs->cs_lock); 389177633Sdfr error = soconnect(cu->cu_socket, 390177633Sdfr (struct sockaddr *)&cu->cu_raddr, curthread); 391177633Sdfr mtx_lock(&cs->cs_lock); 392177633Sdfr if (error) { 393184588Sdfr errp->re_errno = error; 394184588Sdfr errp->re_status = stat = RPC_CANTSEND; 395177633Sdfr goto out; 396177633Sdfr } 397177633Sdfr cu->cu_connected = 1; 398177633Sdfr } 399177633Sdfr if (cu->cu_connected) { 400177633Sdfr sa = NULL; 401177633Sdfr salen = 0; 402177633Sdfr } else { 403177633Sdfr sa = (struct sockaddr *)&cu->cu_raddr; 404177633Sdfr salen = cu->cu_rlen; 405177633Sdfr } 406180025Sdfr time_waited = 0; 407180025Sdfr retrans = 0; 408184588Sdfr if (ext && ext->rc_timers) { 409184588Sdfr rt = ext->rc_timers; 410184588Sdfr if (!rt->rt_rtxcur) 411184588Sdfr rt->rt_rtxcur = tvtohz(&cu->cu_wait); 412184588Sdfr retransmit_time = next_sendtime = rt->rt_rtxcur; 413184588Sdfr } else { 414184588Sdfr rt = NULL; 415184588Sdfr retransmit_time = next_sendtime = tvtohz(&cu->cu_wait); 416184588Sdfr } 417177633Sdfr 418180025Sdfr starttime = ticks; 419177633Sdfr 420177633Sdfrcall_again: 421177633Sdfr mtx_assert(&cs->cs_lock, MA_OWNED); 422177633Sdfr 423177633Sdfr cu->cu_xid++; 424177633Sdfr xid = cu->cu_xid; 425177633Sdfr 426177633Sdfrsend_again: 427177633Sdfr mtx_unlock(&cs->cs_lock); 428177633Sdfr 429248195Sglebius mreq = m_gethdr(M_WAITOK, MT_DATA); 430184588Sdfr KASSERT(cu->cu_mcalllen <= MHLEN, ("RPC header too big")); 431184588Sdfr bcopy(cu->cu_mcallc, mreq->m_data, cu->cu_mcalllen); 432184588Sdfr mreq->m_len = cu->cu_mcalllen; 433177633Sdfr 434177633Sdfr /* 435177633Sdfr * The XID is the first thing in the request. 436177633Sdfr */ 437177633Sdfr *mtod(mreq, uint32_t *) = htonl(xid); 438177633Sdfr 439177633Sdfr xdrmbuf_create(&xdrs, mreq, XDR_ENCODE); 440177633Sdfr 441184588Sdfr if (cu->cu_async == TRUE && args == NULL) 442177633Sdfr goto get_reply; 443177633Sdfr 444177633Sdfr if ((! XDR_PUTINT32(&xdrs, &proc)) || 445184588Sdfr (! AUTH_MARSHALL(auth, xid, &xdrs, 446184588Sdfr m_copym(args, 0, M_COPYALL, M_WAITOK)))) { 447184588Sdfr errp->re_status = stat = RPC_CANTENCODEARGS; 448177633Sdfr mtx_lock(&cs->cs_lock); 449177633Sdfr goto out; 450177633Sdfr } 451184588Sdfr mreq->m_pkthdr.len = m_length(mreq, NULL); 452177633Sdfr 453180025Sdfr cr->cr_xid = xid; 454177633Sdfr mtx_lock(&cs->cs_lock); 455184588Sdfr 456184588Sdfr /* 457184588Sdfr * Try to get a place in the congestion window. 458184588Sdfr */ 459184588Sdfr while (cu->cu_sent >= cu->cu_cwnd) { 460184588Sdfr cu->cu_cwnd_wait = TRUE; 461184588Sdfr error = msleep(&cu->cu_cwnd_wait, &cs->cs_lock, 462184588Sdfr cu->cu_waitflag, "rpccwnd", 0); 463184588Sdfr if (error) { 464184588Sdfr errp->re_errno = error; 465225234Sart if (error == EINTR || error == ERESTART) 466225234Sart errp->re_status = stat = RPC_INTR; 467225234Sart else 468225234Sart errp->re_status = stat = RPC_CANTSEND; 469184588Sdfr goto out; 470184588Sdfr } 471184588Sdfr } 472184588Sdfr cu->cu_sent += CWNDSCALE; 473184588Sdfr 474180025Sdfr TAILQ_INSERT_TAIL(&cs->cs_pending, cr, cr_link); 475177633Sdfr mtx_unlock(&cs->cs_lock); 476177633Sdfr 477177633Sdfr /* 478177633Sdfr * sosend consumes mreq. 479177633Sdfr */ 480177633Sdfr error = sosend(cu->cu_socket, sa, NULL, mreq, NULL, 0, curthread); 481177633Sdfr mreq = NULL; 482177633Sdfr 483177633Sdfr /* 484177633Sdfr * sub-optimal code appears here because we have 485177633Sdfr * some clock time to spare while the packets are in flight. 486177633Sdfr * (We assume that this is actually only executed once.) 487177633Sdfr */ 488184588Sdfr reply_msg.acpted_rply.ar_verf.oa_flavor = AUTH_NULL; 489184588Sdfr reply_msg.acpted_rply.ar_verf.oa_base = cr->cr_verf; 490184588Sdfr reply_msg.acpted_rply.ar_verf.oa_length = 0; 491184588Sdfr reply_msg.acpted_rply.ar_results.where = NULL; 492184588Sdfr reply_msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void; 493177633Sdfr 494177633Sdfr mtx_lock(&cs->cs_lock); 495177633Sdfr if (error) { 496180025Sdfr TAILQ_REMOVE(&cs->cs_pending, cr, cr_link); 497184588Sdfr errp->re_errno = error; 498184588Sdfr errp->re_status = stat = RPC_CANTSEND; 499184588Sdfr cu->cu_sent -= CWNDSCALE; 500184588Sdfr if (cu->cu_cwnd_wait) { 501184588Sdfr cu->cu_cwnd_wait = FALSE; 502184588Sdfr wakeup(&cu->cu_cwnd_wait); 503184588Sdfr } 504177633Sdfr goto out; 505177633Sdfr } 506177633Sdfr 507177633Sdfr /* 508177633Sdfr * Check to see if we got an upcall while waiting for the 509180025Sdfr * lock. 510177633Sdfr */ 511180025Sdfr if (cr->cr_error) { 512180025Sdfr TAILQ_REMOVE(&cs->cs_pending, cr, cr_link); 513184588Sdfr errp->re_errno = cr->cr_error; 514184588Sdfr errp->re_status = stat = RPC_CANTRECV; 515184588Sdfr cu->cu_sent -= CWNDSCALE; 516184588Sdfr if (cu->cu_cwnd_wait) { 517184588Sdfr cu->cu_cwnd_wait = FALSE; 518184588Sdfr wakeup(&cu->cu_cwnd_wait); 519184588Sdfr } 520177633Sdfr goto out; 521177633Sdfr } 522180025Sdfr if (cr->cr_mrep) { 523180025Sdfr TAILQ_REMOVE(&cs->cs_pending, cr, cr_link); 524184588Sdfr cu->cu_sent -= CWNDSCALE; 525184588Sdfr if (cu->cu_cwnd_wait) { 526184588Sdfr cu->cu_cwnd_wait = FALSE; 527184588Sdfr wakeup(&cu->cu_cwnd_wait); 528184588Sdfr } 529177633Sdfr goto got_reply; 530177633Sdfr } 531177633Sdfr 532177633Sdfr /* 533177633Sdfr * Hack to provide rpc-based message passing 534177633Sdfr */ 535180025Sdfr if (timeout == 0) { 536180025Sdfr TAILQ_REMOVE(&cs->cs_pending, cr, cr_link); 537184588Sdfr errp->re_status = stat = RPC_TIMEDOUT; 538184588Sdfr cu->cu_sent -= CWNDSCALE; 539184588Sdfr if (cu->cu_cwnd_wait) { 540184588Sdfr cu->cu_cwnd_wait = FALSE; 541184588Sdfr wakeup(&cu->cu_cwnd_wait); 542184588Sdfr } 543177633Sdfr goto out; 544177633Sdfr } 545177633Sdfr 546177633Sdfrget_reply: 547177633Sdfr for (;;) { 548177633Sdfr /* Decide how long to wait. */ 549180025Sdfr if (next_sendtime < timeout) 550177633Sdfr tv = next_sendtime; 551180025Sdfr else 552180025Sdfr tv = timeout; 553180025Sdfr tv -= time_waited; 554180025Sdfr 555180025Sdfr if (tv > 0) { 556195245Srmacklem if (cu->cu_closing || cu->cu_closed) { 557180025Sdfr error = 0; 558195245Srmacklem cr->cr_error = ESHUTDOWN; 559195245Srmacklem } else { 560180025Sdfr error = msleep(cr, &cs->cs_lock, 561180025Sdfr cu->cu_waitflag, cu->cu_waitchan, tv); 562195245Srmacklem } 563177633Sdfr } else { 564180025Sdfr error = EWOULDBLOCK; 565177633Sdfr } 566177633Sdfr 567180025Sdfr TAILQ_REMOVE(&cs->cs_pending, cr, cr_link); 568184588Sdfr cu->cu_sent -= CWNDSCALE; 569184588Sdfr if (cu->cu_cwnd_wait) { 570184588Sdfr cu->cu_cwnd_wait = FALSE; 571184588Sdfr wakeup(&cu->cu_cwnd_wait); 572184588Sdfr } 573177633Sdfr 574177633Sdfr if (!error) { 575177633Sdfr /* 576177633Sdfr * We were woken up by the upcall. If the 577177633Sdfr * upcall had a receive error, report that, 578177633Sdfr * otherwise we have a reply. 579177633Sdfr */ 580180025Sdfr if (cr->cr_error) { 581184588Sdfr errp->re_errno = cr->cr_error; 582184588Sdfr errp->re_status = stat = RPC_CANTRECV; 583177633Sdfr goto out; 584177633Sdfr } 585184588Sdfr 586184588Sdfr cu->cu_cwnd += (CWNDSCALE * CWNDSCALE 587184588Sdfr + cu->cu_cwnd / 2) / cu->cu_cwnd; 588184588Sdfr if (cu->cu_cwnd > MAXCWND) 589184588Sdfr cu->cu_cwnd = MAXCWND; 590184588Sdfr 591184588Sdfr if (rt) { 592184588Sdfr /* 593184588Sdfr * Add one to the time since a tick 594184588Sdfr * count of N means that the actual 595184588Sdfr * time taken was somewhere between N 596184588Sdfr * and N+1. 597184588Sdfr */ 598184588Sdfr rtt = ticks - starttime + 1; 599184588Sdfr 600184588Sdfr /* 601184588Sdfr * Update our estimate of the round 602184588Sdfr * trip time using roughly the 603184588Sdfr * algorithm described in RFC 604184588Sdfr * 2988. Given an RTT sample R: 605184588Sdfr * 606184588Sdfr * RTTVAR = (1-beta) * RTTVAR + beta * |SRTT-R| 607184588Sdfr * SRTT = (1-alpha) * SRTT + alpha * R 608184588Sdfr * 609184588Sdfr * where alpha = 0.125 and beta = 0.25. 610184588Sdfr * 611184588Sdfr * The initial retransmit timeout is 612184588Sdfr * SRTT + 4*RTTVAR and doubles on each 613184588Sdfr * retransmision. 614184588Sdfr */ 615184588Sdfr if (rt->rt_srtt == 0) { 616184588Sdfr rt->rt_srtt = rtt; 617184588Sdfr rt->rt_deviate = rtt / 2; 618184588Sdfr } else { 619184588Sdfr int32_t error = rtt - rt->rt_srtt; 620184588Sdfr rt->rt_srtt += error / 8; 621184588Sdfr error = abs(error) - rt->rt_deviate; 622184588Sdfr rt->rt_deviate += error / 4; 623184588Sdfr } 624184588Sdfr rt->rt_rtxcur = rt->rt_srtt + 4*rt->rt_deviate; 625184588Sdfr } 626184588Sdfr 627177633Sdfr break; 628177633Sdfr } 629177633Sdfr 630177633Sdfr /* 631177633Sdfr * The sleep returned an error so our request is still 632177633Sdfr * on the list. If we got EWOULDBLOCK, we may want to 633177633Sdfr * re-send the request. 634177633Sdfr */ 635177633Sdfr if (error != EWOULDBLOCK) { 636184588Sdfr errp->re_errno = error; 637225234Sart if (error == EINTR || error == ERESTART) 638184588Sdfr errp->re_status = stat = RPC_INTR; 639177633Sdfr else 640184588Sdfr errp->re_status = stat = RPC_CANTRECV; 641177633Sdfr goto out; 642177633Sdfr } 643177633Sdfr 644180025Sdfr time_waited = ticks - starttime; 645177633Sdfr 646177633Sdfr /* Check for timeout. */ 647180025Sdfr if (time_waited > timeout) { 648184588Sdfr errp->re_errno = EWOULDBLOCK; 649184588Sdfr errp->re_status = stat = RPC_TIMEDOUT; 650177633Sdfr goto out; 651177633Sdfr } 652177633Sdfr 653177633Sdfr /* Retransmit if necessary. */ 654180025Sdfr if (time_waited >= next_sendtime) { 655184588Sdfr cu->cu_cwnd /= 2; 656184588Sdfr if (cu->cu_cwnd < CWNDSCALE) 657184588Sdfr cu->cu_cwnd = CWNDSCALE; 658180025Sdfr if (ext && ext->rc_feedback) { 659180025Sdfr mtx_unlock(&cs->cs_lock); 660180025Sdfr if (retrans == 0) 661180025Sdfr ext->rc_feedback(FEEDBACK_REXMIT1, 662180025Sdfr proc, ext->rc_feedback_arg); 663180025Sdfr else 664180025Sdfr ext->rc_feedback(FEEDBACK_REXMIT2, 665180025Sdfr proc, ext->rc_feedback_arg); 666180025Sdfr mtx_lock(&cs->cs_lock); 667180025Sdfr } 668184588Sdfr if (cu->cu_closing || cu->cu_closed) { 669184588Sdfr errp->re_errno = ESHUTDOWN; 670184588Sdfr errp->re_status = stat = RPC_CANTRECV; 671180025Sdfr goto out; 672180025Sdfr } 673180025Sdfr retrans++; 674177633Sdfr /* update retransmit_time */ 675180025Sdfr if (retransmit_time < RPC_MAX_BACKOFF * hz) 676180025Sdfr retransmit_time = 2 * retransmit_time; 677180025Sdfr next_sendtime += retransmit_time; 678177633Sdfr goto send_again; 679177633Sdfr } 680255284Srmacklem cu->cu_sent += CWNDSCALE; 681180025Sdfr TAILQ_INSERT_TAIL(&cs->cs_pending, cr, cr_link); 682177633Sdfr } 683177633Sdfr 684177633Sdfrgot_reply: 685177633Sdfr /* 686177633Sdfr * Now decode and validate the response. We need to drop the 687177633Sdfr * lock since xdr_replymsg may end up sleeping in malloc. 688177633Sdfr */ 689177633Sdfr mtx_unlock(&cs->cs_lock); 690177633Sdfr 691180025Sdfr if (ext && ext->rc_feedback) 692180025Sdfr ext->rc_feedback(FEEDBACK_OK, proc, ext->rc_feedback_arg); 693180025Sdfr 694180025Sdfr xdrmbuf_create(&xdrs, cr->cr_mrep, XDR_DECODE); 695177633Sdfr ok = xdr_replymsg(&xdrs, &reply_msg); 696180025Sdfr cr->cr_mrep = NULL; 697177633Sdfr 698177633Sdfr if (ok) { 699177633Sdfr if ((reply_msg.rm_reply.rp_stat == MSG_ACCEPTED) && 700184588Sdfr (reply_msg.acpted_rply.ar_stat == SUCCESS)) 701184588Sdfr errp->re_status = stat = RPC_SUCCESS; 702177633Sdfr else 703184588Sdfr stat = _seterr_reply(&reply_msg, &(cu->cu_error)); 704177633Sdfr 705184588Sdfr if (errp->re_status == RPC_SUCCESS) { 706184588Sdfr results = xdrmbuf_getall(&xdrs); 707184588Sdfr if (! AUTH_VALIDATE(auth, xid, 708184588Sdfr &reply_msg.acpted_rply.ar_verf, 709184588Sdfr &results)) { 710184588Sdfr errp->re_status = stat = RPC_AUTHERROR; 711184588Sdfr errp->re_why = AUTH_INVALIDRESP; 712184588Sdfr if (retrans && 713184588Sdfr auth->ah_cred.oa_flavor == RPCSEC_GSS) { 714184588Sdfr /* 715184588Sdfr * If we retransmitted, its 716184588Sdfr * possible that we will 717184588Sdfr * receive a reply for one of 718184588Sdfr * the earlier transmissions 719184588Sdfr * (which will use an older 720184588Sdfr * RPCSEC_GSS sequence 721184588Sdfr * number). In this case, just 722184588Sdfr * go back and listen for a 723184588Sdfr * new reply. We could keep a 724184588Sdfr * record of all the seq 725184588Sdfr * numbers we have transmitted 726184588Sdfr * so far so that we could 727184588Sdfr * accept a reply for any of 728184588Sdfr * them here. 729184588Sdfr */ 730184588Sdfr XDR_DESTROY(&xdrs); 731184588Sdfr mtx_lock(&cs->cs_lock); 732255284Srmacklem cu->cu_sent += CWNDSCALE; 733184588Sdfr TAILQ_INSERT_TAIL(&cs->cs_pending, 734184588Sdfr cr, cr_link); 735184588Sdfr cr->cr_mrep = NULL; 736184588Sdfr goto get_reply; 737184588Sdfr } 738184588Sdfr } else { 739184588Sdfr *resultsp = results; 740177633Sdfr } 741177633Sdfr } /* end successful completion */ 742177633Sdfr /* 743177633Sdfr * If unsuccesful AND error is an authentication error 744177633Sdfr * then refresh credentials and try again, else break 745177633Sdfr */ 746184588Sdfr else if (stat == RPC_AUTHERROR) 747177633Sdfr /* maybe our credentials need to be refreshed ... */ 748177633Sdfr if (nrefreshes > 0 && 749184588Sdfr AUTH_REFRESH(auth, &reply_msg)) { 750177633Sdfr nrefreshes--; 751184588Sdfr XDR_DESTROY(&xdrs); 752184588Sdfr mtx_lock(&cs->cs_lock); 753177633Sdfr goto call_again; 754177633Sdfr } 755177633Sdfr /* end of unsuccessful completion */ 756177633Sdfr } /* end of valid reply message */ 757177633Sdfr else { 758184588Sdfr errp->re_status = stat = RPC_CANTDECODERES; 759177633Sdfr 760177633Sdfr } 761184588Sdfr XDR_DESTROY(&xdrs); 762184588Sdfr mtx_lock(&cs->cs_lock); 763177633Sdfrout: 764177633Sdfr mtx_assert(&cs->cs_lock, MA_OWNED); 765177633Sdfr 766177633Sdfr if (mreq) 767177633Sdfr m_freem(mreq); 768180025Sdfr if (cr->cr_mrep) 769180025Sdfr m_freem(cr->cr_mrep); 770177633Sdfr 771180025Sdfr cu->cu_threads--; 772180025Sdfr if (cu->cu_closing) 773180025Sdfr wakeup(cu); 774180025Sdfr 775177633Sdfr mtx_unlock(&cs->cs_lock); 776180025Sdfr 777184588Sdfr if (auth && stat != RPC_SUCCESS) 778184588Sdfr AUTH_VALIDATE(auth, xid, NULL, NULL); 779184588Sdfr 780180025Sdfr free(cr, M_RPC); 781180025Sdfr 782184588Sdfr return (stat); 783177633Sdfr} 784177633Sdfr 785177633Sdfrstatic void 786177633Sdfrclnt_dg_geterr(CLIENT *cl, struct rpc_err *errp) 787177633Sdfr{ 788177633Sdfr struct cu_data *cu = (struct cu_data *)cl->cl_private; 789177633Sdfr 790177633Sdfr *errp = cu->cu_error; 791177633Sdfr} 792177633Sdfr 793177633Sdfrstatic bool_t 794177633Sdfrclnt_dg_freeres(CLIENT *cl, xdrproc_t xdr_res, void *res_ptr) 795177633Sdfr{ 796177633Sdfr XDR xdrs; 797177633Sdfr bool_t dummy; 798177633Sdfr 799177633Sdfr xdrs.x_op = XDR_FREE; 800177633Sdfr dummy = (*xdr_res)(&xdrs, res_ptr); 801177633Sdfr 802177633Sdfr return (dummy); 803177633Sdfr} 804177633Sdfr 805177633Sdfr/*ARGSUSED*/ 806177633Sdfrstatic void 807177633Sdfrclnt_dg_abort(CLIENT *h) 808177633Sdfr{ 809177633Sdfr} 810177633Sdfr 811177633Sdfrstatic bool_t 812177633Sdfrclnt_dg_control(CLIENT *cl, u_int request, void *info) 813177633Sdfr{ 814177633Sdfr struct cu_data *cu = (struct cu_data *)cl->cl_private; 815193272Sjhb struct cu_socket *cs; 816177633Sdfr struct sockaddr *addr; 817177633Sdfr 818193272Sjhb cs = cu->cu_socket->so_rcv.sb_upcallarg; 819177633Sdfr mtx_lock(&cs->cs_lock); 820177633Sdfr 821177633Sdfr switch (request) { 822177633Sdfr case CLSET_FD_CLOSE: 823177633Sdfr cu->cu_closeit = TRUE; 824177633Sdfr mtx_unlock(&cs->cs_lock); 825177633Sdfr return (TRUE); 826177633Sdfr case CLSET_FD_NCLOSE: 827177633Sdfr cu->cu_closeit = FALSE; 828177633Sdfr mtx_unlock(&cs->cs_lock); 829177633Sdfr return (TRUE); 830177633Sdfr } 831177633Sdfr 832177633Sdfr /* for other requests which use info */ 833177633Sdfr if (info == NULL) { 834177633Sdfr mtx_unlock(&cs->cs_lock); 835177633Sdfr return (FALSE); 836177633Sdfr } 837177633Sdfr switch (request) { 838177633Sdfr case CLSET_TIMEOUT: 839177633Sdfr if (time_not_ok((struct timeval *)info)) { 840177633Sdfr mtx_unlock(&cs->cs_lock); 841177633Sdfr return (FALSE); 842177633Sdfr } 843177633Sdfr cu->cu_total = *(struct timeval *)info; 844177633Sdfr break; 845177633Sdfr case CLGET_TIMEOUT: 846177633Sdfr *(struct timeval *)info = cu->cu_total; 847177633Sdfr break; 848177633Sdfr case CLSET_RETRY_TIMEOUT: 849177633Sdfr if (time_not_ok((struct timeval *)info)) { 850177633Sdfr mtx_unlock(&cs->cs_lock); 851177633Sdfr return (FALSE); 852177633Sdfr } 853177633Sdfr cu->cu_wait = *(struct timeval *)info; 854177633Sdfr break; 855177633Sdfr case CLGET_RETRY_TIMEOUT: 856177633Sdfr *(struct timeval *)info = cu->cu_wait; 857177633Sdfr break; 858177633Sdfr case CLGET_SVC_ADDR: 859177633Sdfr /* 860177633Sdfr * Slightly different semantics to userland - we use 861177633Sdfr * sockaddr instead of netbuf. 862177633Sdfr */ 863177633Sdfr memcpy(info, &cu->cu_raddr, cu->cu_raddr.ss_len); 864177633Sdfr break; 865177633Sdfr case CLSET_SVC_ADDR: /* set to new address */ 866177633Sdfr addr = (struct sockaddr *)info; 867177633Sdfr (void) memcpy(&cu->cu_raddr, addr, addr->sa_len); 868177633Sdfr break; 869177633Sdfr case CLGET_XID: 870177633Sdfr *(uint32_t *)info = cu->cu_xid; 871177633Sdfr break; 872177633Sdfr 873177633Sdfr case CLSET_XID: 874177633Sdfr /* This will set the xid of the NEXT call */ 875177633Sdfr /* decrement by 1 as clnt_dg_call() increments once */ 876177633Sdfr cu->cu_xid = *(uint32_t *)info - 1; 877177633Sdfr break; 878177633Sdfr 879177633Sdfr case CLGET_VERS: 880177633Sdfr /* 881177633Sdfr * This RELIES on the information that, in the call body, 882177633Sdfr * the version number field is the fifth field from the 883177633Sdfr * begining of the RPC header. MUST be changed if the 884177633Sdfr * call_struct is changed 885177633Sdfr */ 886177633Sdfr *(uint32_t *)info = 887177633Sdfr ntohl(*(uint32_t *)(void *)(cu->cu_mcallc + 888177633Sdfr 4 * BYTES_PER_XDR_UNIT)); 889177633Sdfr break; 890177633Sdfr 891177633Sdfr case CLSET_VERS: 892177633Sdfr *(uint32_t *)(void *)(cu->cu_mcallc + 4 * BYTES_PER_XDR_UNIT) 893177633Sdfr = htonl(*(uint32_t *)info); 894177633Sdfr break; 895177633Sdfr 896177633Sdfr case CLGET_PROG: 897177633Sdfr /* 898177633Sdfr * This RELIES on the information that, in the call body, 899177633Sdfr * the program number field is the fourth field from the 900177633Sdfr * begining of the RPC header. MUST be changed if the 901177633Sdfr * call_struct is changed 902177633Sdfr */ 903177633Sdfr *(uint32_t *)info = 904177633Sdfr ntohl(*(uint32_t *)(void *)(cu->cu_mcallc + 905177633Sdfr 3 * BYTES_PER_XDR_UNIT)); 906177633Sdfr break; 907177633Sdfr 908177633Sdfr case CLSET_PROG: 909177633Sdfr *(uint32_t *)(void *)(cu->cu_mcallc + 3 * BYTES_PER_XDR_UNIT) 910177633Sdfr = htonl(*(uint32_t *)info); 911177633Sdfr break; 912177633Sdfr case CLSET_ASYNC: 913177633Sdfr cu->cu_async = *(int *)info; 914177633Sdfr break; 915177633Sdfr case CLSET_CONNECT: 916177633Sdfr cu->cu_connect = *(int *)info; 917177633Sdfr break; 918177633Sdfr case CLSET_WAITCHAN: 919184588Sdfr cu->cu_waitchan = (const char *)info; 920177633Sdfr break; 921177633Sdfr case CLGET_WAITCHAN: 922177633Sdfr *(const char **) info = cu->cu_waitchan; 923177633Sdfr break; 924177633Sdfr case CLSET_INTERRUPTIBLE: 925177633Sdfr if (*(int *) info) 926177633Sdfr cu->cu_waitflag = PCATCH; 927177633Sdfr else 928177633Sdfr cu->cu_waitflag = 0; 929177633Sdfr break; 930177633Sdfr case CLGET_INTERRUPTIBLE: 931177633Sdfr if (cu->cu_waitflag) 932177633Sdfr *(int *) info = TRUE; 933177633Sdfr else 934177633Sdfr *(int *) info = FALSE; 935177633Sdfr break; 936177633Sdfr default: 937177633Sdfr mtx_unlock(&cs->cs_lock); 938177633Sdfr return (FALSE); 939177633Sdfr } 940177633Sdfr mtx_unlock(&cs->cs_lock); 941177633Sdfr return (TRUE); 942177633Sdfr} 943177633Sdfr 944177633Sdfrstatic void 945184588Sdfrclnt_dg_close(CLIENT *cl) 946177633Sdfr{ 947177633Sdfr struct cu_data *cu = (struct cu_data *)cl->cl_private; 948193272Sjhb struct cu_socket *cs; 949180025Sdfr struct cu_request *cr; 950177633Sdfr 951193272Sjhb cs = cu->cu_socket->so_rcv.sb_upcallarg; 952180025Sdfr mtx_lock(&cs->cs_lock); 953177633Sdfr 954184588Sdfr if (cu->cu_closed) { 955184588Sdfr mtx_unlock(&cs->cs_lock); 956184588Sdfr return; 957184588Sdfr } 958184588Sdfr 959184588Sdfr if (cu->cu_closing) { 960184588Sdfr while (cu->cu_closing) 961184588Sdfr msleep(cu, &cs->cs_lock, 0, "rpcclose", 0); 962184588Sdfr KASSERT(cu->cu_closed, ("client should be closed")); 963184588Sdfr mtx_unlock(&cs->cs_lock); 964184588Sdfr return; 965184588Sdfr } 966184588Sdfr 967180025Sdfr /* 968180025Sdfr * Abort any pending requests and wait until everyone 969180025Sdfr * has finished with clnt_vc_call. 970180025Sdfr */ 971180025Sdfr cu->cu_closing = TRUE; 972180025Sdfr TAILQ_FOREACH(cr, &cs->cs_pending, cr_link) { 973180025Sdfr if (cr->cr_client == cl) { 974180025Sdfr cr->cr_xid = 0; 975180025Sdfr cr->cr_error = ESHUTDOWN; 976180025Sdfr wakeup(cr); 977180025Sdfr } 978180025Sdfr } 979180025Sdfr 980180025Sdfr while (cu->cu_threads) 981180025Sdfr msleep(cu, &cs->cs_lock, 0, "rpcclose", 0); 982180025Sdfr 983184588Sdfr cu->cu_closing = FALSE; 984184588Sdfr cu->cu_closed = TRUE; 985184588Sdfr 986184588Sdfr mtx_unlock(&cs->cs_lock); 987184588Sdfr wakeup(cu); 988184588Sdfr} 989184588Sdfr 990184588Sdfrstatic void 991184588Sdfrclnt_dg_destroy(CLIENT *cl) 992184588Sdfr{ 993184588Sdfr struct cu_data *cu = (struct cu_data *)cl->cl_private; 994193272Sjhb struct cu_socket *cs; 995184588Sdfr struct socket *so = NULL; 996184588Sdfr bool_t lastsocketref; 997184588Sdfr 998193272Sjhb cs = cu->cu_socket->so_rcv.sb_upcallarg; 999184588Sdfr clnt_dg_close(cl); 1000184588Sdfr 1001227059Srmacklem SOCKBUF_LOCK(&cu->cu_socket->so_rcv); 1002184588Sdfr mtx_lock(&cs->cs_lock); 1003184588Sdfr 1004177633Sdfr cs->cs_refs--; 1005177633Sdfr if (cs->cs_refs == 0) { 1006193437Srmacklem mtx_unlock(&cs->cs_lock); 1007193272Sjhb soupcall_clear(cu->cu_socket, SO_RCV); 1008193437Srmacklem clnt_dg_upcallsdone(cu->cu_socket, cs); 1009177633Sdfr SOCKBUF_UNLOCK(&cu->cu_socket->so_rcv); 1010193437Srmacklem mtx_destroy(&cs->cs_lock); 1011177633Sdfr mem_free(cs, sizeof(*cs)); 1012177633Sdfr lastsocketref = TRUE; 1013177633Sdfr } else { 1014177633Sdfr mtx_unlock(&cs->cs_lock); 1015227059Srmacklem SOCKBUF_UNLOCK(&cu->cu_socket->so_rcv); 1016177633Sdfr lastsocketref = FALSE; 1017177633Sdfr } 1018177633Sdfr 1019180025Sdfr if (cu->cu_closeit && lastsocketref) { 1020177633Sdfr so = cu->cu_socket; 1021177633Sdfr cu->cu_socket = NULL; 1022177633Sdfr } 1023177633Sdfr 1024177633Sdfr if (so) 1025177633Sdfr soclose(so); 1026177633Sdfr 1027177633Sdfr if (cl->cl_netid && cl->cl_netid[0]) 1028177633Sdfr mem_free(cl->cl_netid, strlen(cl->cl_netid) +1); 1029177633Sdfr if (cl->cl_tp && cl->cl_tp[0]) 1030177633Sdfr mem_free(cl->cl_tp, strlen(cl->cl_tp) +1); 1031177633Sdfr mem_free(cu, sizeof (*cu)); 1032177633Sdfr mem_free(cl, sizeof (CLIENT)); 1033177633Sdfr} 1034177633Sdfr 1035177633Sdfr/* 1036177633Sdfr * Make sure that the time is not garbage. -1 value is allowed. 1037177633Sdfr */ 1038177633Sdfrstatic bool_t 1039177633Sdfrtime_not_ok(struct timeval *t) 1040177633Sdfr{ 1041177633Sdfr return (t->tv_sec < -1 || t->tv_sec > 100000000 || 1042177633Sdfr t->tv_usec < -1 || t->tv_usec > 1000000); 1043177633Sdfr} 1044177633Sdfr 1045193272Sjhbint 1046177633Sdfrclnt_dg_soupcall(struct socket *so, void *arg, int waitflag) 1047177633Sdfr{ 1048177633Sdfr struct cu_socket *cs = (struct cu_socket *) arg; 1049177633Sdfr struct uio uio; 1050177633Sdfr struct mbuf *m; 1051177633Sdfr struct mbuf *control; 1052177633Sdfr struct cu_request *cr; 1053177633Sdfr int error, rcvflag, foundreq; 1054177633Sdfr uint32_t xid; 1055177633Sdfr 1056193437Srmacklem cs->cs_upcallrefs++; 1057177633Sdfr uio.uio_resid = 1000000000; 1058177633Sdfr uio.uio_td = curthread; 1059177633Sdfr do { 1060193272Sjhb SOCKBUF_UNLOCK(&so->so_rcv); 1061177633Sdfr m = NULL; 1062177633Sdfr control = NULL; 1063177633Sdfr rcvflag = MSG_DONTWAIT; 1064177633Sdfr error = soreceive(so, NULL, &uio, &m, &control, &rcvflag); 1065177633Sdfr if (control) 1066177633Sdfr m_freem(control); 1067193272Sjhb SOCKBUF_LOCK(&so->so_rcv); 1068177633Sdfr 1069177633Sdfr if (error == EWOULDBLOCK) 1070177633Sdfr break; 1071177633Sdfr 1072177633Sdfr /* 1073177633Sdfr * If there was an error, wake up all pending 1074177633Sdfr * requests. 1075177633Sdfr */ 1076177633Sdfr if (error) { 1077177633Sdfr mtx_lock(&cs->cs_lock); 1078177633Sdfr TAILQ_FOREACH(cr, &cs->cs_pending, cr_link) { 1079180025Sdfr cr->cr_xid = 0; 1080177633Sdfr cr->cr_error = error; 1081177633Sdfr wakeup(cr); 1082177633Sdfr } 1083177633Sdfr mtx_unlock(&cs->cs_lock); 1084177633Sdfr break; 1085177633Sdfr } 1086177633Sdfr 1087177633Sdfr /* 1088177633Sdfr * The XID is in the first uint32_t of the reply. 1089177633Sdfr */ 1090220585Srmacklem if (m->m_len < sizeof(xid) && m_length(m, NULL) < sizeof(xid)) { 1091180025Sdfr /* 1092180025Sdfr * Should never happen. 1093180025Sdfr */ 1094220585Srmacklem m_freem(m); 1095180025Sdfr continue; 1096220585Srmacklem } 1097180025Sdfr 1098217242Srmacklem m_copydata(m, 0, sizeof(xid), (char *)&xid); 1099217242Srmacklem xid = ntohl(xid); 1100177633Sdfr 1101177633Sdfr /* 1102177633Sdfr * Attempt to match this reply with a pending request. 1103177633Sdfr */ 1104177633Sdfr mtx_lock(&cs->cs_lock); 1105177633Sdfr foundreq = 0; 1106177633Sdfr TAILQ_FOREACH(cr, &cs->cs_pending, cr_link) { 1107177633Sdfr if (cr->cr_xid == xid) { 1108177633Sdfr /* 1109180025Sdfr * This one matches. We leave the 1110177633Sdfr * reply mbuf in cr->cr_mrep. Set the 1111180025Sdfr * XID to zero so that we will ignore 1112180025Sdfr * any duplicated replies that arrive 1113180025Sdfr * before clnt_dg_call removes it from 1114180025Sdfr * the queue. 1115177633Sdfr */ 1116177633Sdfr cr->cr_xid = 0; 1117177633Sdfr cr->cr_mrep = m; 1118177633Sdfr cr->cr_error = 0; 1119177633Sdfr foundreq = 1; 1120177633Sdfr wakeup(cr); 1121177633Sdfr break; 1122177633Sdfr } 1123177633Sdfr } 1124177633Sdfr mtx_unlock(&cs->cs_lock); 1125177633Sdfr 1126177633Sdfr /* 1127177633Sdfr * If we didn't find the matching request, just drop 1128177633Sdfr * it - its probably a repeated reply. 1129177633Sdfr */ 1130177633Sdfr if (!foundreq) 1131177633Sdfr m_freem(m); 1132177633Sdfr } while (m); 1133193437Srmacklem cs->cs_upcallrefs--; 1134193437Srmacklem if (cs->cs_upcallrefs < 0) 1135193437Srmacklem panic("rpcdg upcall refcnt"); 1136193437Srmacklem if (cs->cs_upcallrefs == 0) 1137193437Srmacklem wakeup(&cs->cs_upcallrefs); 1138193272Sjhb return (SU_OK); 1139177633Sdfr} 1140177633Sdfr 1141193437Srmacklem/* 1142193437Srmacklem * Wait for all upcalls in progress to complete. 1143193437Srmacklem */ 1144193437Srmacklemstatic void 1145193437Srmacklemclnt_dg_upcallsdone(struct socket *so, struct cu_socket *cs) 1146193437Srmacklem{ 1147193437Srmacklem 1148193437Srmacklem SOCKBUF_LOCK_ASSERT(&so->so_rcv); 1149193437Srmacklem 1150193437Srmacklem while (cs->cs_upcallrefs > 0) 1151193437Srmacklem (void) msleep(&cs->cs_upcallrefs, SOCKBUF_MTX(&so->so_rcv), 0, 1152193437Srmacklem "rpcdgup", 0); 1153193437Srmacklem} 1154