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: releng/10.3/sys/rpc/clnt_dg.c 261046 2014-01-22 23:45:27Z mav $"); 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: 316177633Sdfr if (cl) { 317177633Sdfr mem_free(cl, sizeof (CLIENT)); 318177633Sdfr if (cu) 319177633Sdfr mem_free(cu, sizeof (*cu)); 320177633Sdfr } 321177633Sdfr return (NULL); 322177633Sdfr} 323177633Sdfr 324177633Sdfrstatic enum clnt_stat 325177633Sdfrclnt_dg_call( 326180025Sdfr CLIENT *cl, /* client handle */ 327180025Sdfr struct rpc_callextra *ext, /* call metadata */ 328177633Sdfr rpcproc_t proc, /* procedure number */ 329184588Sdfr struct mbuf *args, /* pointer to args */ 330184588Sdfr struct mbuf **resultsp, /* pointer to results */ 331177633Sdfr struct timeval utimeout) /* seconds to wait before giving up */ 332177633Sdfr{ 333177633Sdfr struct cu_data *cu = (struct cu_data *)cl->cl_private; 334193272Sjhb struct cu_socket *cs; 335184588Sdfr struct rpc_timers *rt; 336180025Sdfr AUTH *auth; 337184588Sdfr struct rpc_err *errp; 338184588Sdfr enum clnt_stat stat; 339177633Sdfr XDR xdrs; 340177633Sdfr struct rpc_msg reply_msg; 341177633Sdfr bool_t ok; 342180025Sdfr int retrans; /* number of re-transmits so far */ 343177633Sdfr int nrefreshes = 2; /* number of times to refresh cred */ 344180025Sdfr struct timeval *tvp; 345180025Sdfr int timeout; 346180025Sdfr int retransmit_time; 347184588Sdfr int next_sendtime, starttime, rtt, time_waited, tv = 0; 348177633Sdfr struct sockaddr *sa; 349177633Sdfr socklen_t salen; 350184588Sdfr uint32_t xid = 0; 351184588Sdfr struct mbuf *mreq = NULL, *results; 352180025Sdfr struct cu_request *cr; 353177633Sdfr int error; 354177633Sdfr 355193272Sjhb cs = cu->cu_socket->so_rcv.sb_upcallarg; 356180025Sdfr cr = malloc(sizeof(struct cu_request), M_RPC, M_WAITOK); 357180025Sdfr 358177633Sdfr mtx_lock(&cs->cs_lock); 359177633Sdfr 360184588Sdfr if (cu->cu_closing || cu->cu_closed) { 361180025Sdfr mtx_unlock(&cs->cs_lock); 362180025Sdfr free(cr, M_RPC); 363180025Sdfr return (RPC_CANTSEND); 364180025Sdfr } 365180025Sdfr cu->cu_threads++; 366177633Sdfr 367184588Sdfr if (ext) { 368180025Sdfr auth = ext->rc_auth; 369184588Sdfr errp = &ext->rc_err; 370184588Sdfr } else { 371180025Sdfr auth = cl->cl_auth; 372184588Sdfr errp = &cu->cu_error; 373184588Sdfr } 374180025Sdfr 375180025Sdfr cr->cr_client = cl; 376180025Sdfr cr->cr_mrep = NULL; 377180025Sdfr cr->cr_error = 0; 378180025Sdfr 379177633Sdfr if (cu->cu_total.tv_usec == -1) { 380180025Sdfr tvp = &utimeout; /* use supplied timeout */ 381177633Sdfr } else { 382180025Sdfr tvp = &cu->cu_total; /* use default timeout */ 383177633Sdfr } 384180025Sdfr if (tvp->tv_sec || tvp->tv_usec) 385180025Sdfr timeout = tvtohz(tvp); 386180025Sdfr else 387180025Sdfr timeout = 0; 388177633Sdfr 389177633Sdfr if (cu->cu_connect && !cu->cu_connected) { 390177633Sdfr mtx_unlock(&cs->cs_lock); 391177633Sdfr error = soconnect(cu->cu_socket, 392177633Sdfr (struct sockaddr *)&cu->cu_raddr, curthread); 393177633Sdfr mtx_lock(&cs->cs_lock); 394177633Sdfr if (error) { 395184588Sdfr errp->re_errno = error; 396184588Sdfr errp->re_status = stat = RPC_CANTSEND; 397177633Sdfr goto out; 398177633Sdfr } 399177633Sdfr cu->cu_connected = 1; 400177633Sdfr } 401177633Sdfr if (cu->cu_connected) { 402177633Sdfr sa = NULL; 403177633Sdfr salen = 0; 404177633Sdfr } else { 405177633Sdfr sa = (struct sockaddr *)&cu->cu_raddr; 406177633Sdfr salen = cu->cu_rlen; 407177633Sdfr } 408180025Sdfr time_waited = 0; 409180025Sdfr retrans = 0; 410184588Sdfr if (ext && ext->rc_timers) { 411184588Sdfr rt = ext->rc_timers; 412184588Sdfr if (!rt->rt_rtxcur) 413184588Sdfr rt->rt_rtxcur = tvtohz(&cu->cu_wait); 414184588Sdfr retransmit_time = next_sendtime = rt->rt_rtxcur; 415184588Sdfr } else { 416184588Sdfr rt = NULL; 417184588Sdfr retransmit_time = next_sendtime = tvtohz(&cu->cu_wait); 418184588Sdfr } 419177633Sdfr 420180025Sdfr starttime = ticks; 421177633Sdfr 422177633Sdfrcall_again: 423177633Sdfr mtx_assert(&cs->cs_lock, MA_OWNED); 424177633Sdfr 425177633Sdfr cu->cu_xid++; 426177633Sdfr xid = cu->cu_xid; 427177633Sdfr 428177633Sdfrsend_again: 429177633Sdfr mtx_unlock(&cs->cs_lock); 430177633Sdfr 431248195Sglebius mreq = m_gethdr(M_WAITOK, MT_DATA); 432184588Sdfr KASSERT(cu->cu_mcalllen <= MHLEN, ("RPC header too big")); 433184588Sdfr bcopy(cu->cu_mcallc, mreq->m_data, cu->cu_mcalllen); 434184588Sdfr mreq->m_len = cu->cu_mcalllen; 435177633Sdfr 436177633Sdfr /* 437177633Sdfr * The XID is the first thing in the request. 438177633Sdfr */ 439177633Sdfr *mtod(mreq, uint32_t *) = htonl(xid); 440177633Sdfr 441177633Sdfr xdrmbuf_create(&xdrs, mreq, XDR_ENCODE); 442177633Sdfr 443184588Sdfr if (cu->cu_async == TRUE && args == NULL) 444177633Sdfr goto get_reply; 445177633Sdfr 446177633Sdfr if ((! XDR_PUTINT32(&xdrs, &proc)) || 447184588Sdfr (! AUTH_MARSHALL(auth, xid, &xdrs, 448184588Sdfr m_copym(args, 0, M_COPYALL, M_WAITOK)))) { 449184588Sdfr errp->re_status = stat = RPC_CANTENCODEARGS; 450177633Sdfr mtx_lock(&cs->cs_lock); 451177633Sdfr goto out; 452177633Sdfr } 453184588Sdfr mreq->m_pkthdr.len = m_length(mreq, NULL); 454177633Sdfr 455180025Sdfr cr->cr_xid = xid; 456177633Sdfr mtx_lock(&cs->cs_lock); 457184588Sdfr 458184588Sdfr /* 459184588Sdfr * Try to get a place in the congestion window. 460184588Sdfr */ 461184588Sdfr while (cu->cu_sent >= cu->cu_cwnd) { 462184588Sdfr cu->cu_cwnd_wait = TRUE; 463184588Sdfr error = msleep(&cu->cu_cwnd_wait, &cs->cs_lock, 464184588Sdfr cu->cu_waitflag, "rpccwnd", 0); 465184588Sdfr if (error) { 466184588Sdfr errp->re_errno = error; 467225234Sart if (error == EINTR || error == ERESTART) 468225234Sart errp->re_status = stat = RPC_INTR; 469225234Sart else 470225234Sart errp->re_status = stat = RPC_CANTSEND; 471184588Sdfr goto out; 472184588Sdfr } 473184588Sdfr } 474184588Sdfr cu->cu_sent += CWNDSCALE; 475184588Sdfr 476180025Sdfr TAILQ_INSERT_TAIL(&cs->cs_pending, cr, cr_link); 477177633Sdfr mtx_unlock(&cs->cs_lock); 478177633Sdfr 479177633Sdfr /* 480177633Sdfr * sosend consumes mreq. 481177633Sdfr */ 482177633Sdfr error = sosend(cu->cu_socket, sa, NULL, mreq, NULL, 0, curthread); 483177633Sdfr mreq = NULL; 484177633Sdfr 485177633Sdfr /* 486177633Sdfr * sub-optimal code appears here because we have 487177633Sdfr * some clock time to spare while the packets are in flight. 488177633Sdfr * (We assume that this is actually only executed once.) 489177633Sdfr */ 490184588Sdfr reply_msg.acpted_rply.ar_verf.oa_flavor = AUTH_NULL; 491184588Sdfr reply_msg.acpted_rply.ar_verf.oa_base = cr->cr_verf; 492184588Sdfr reply_msg.acpted_rply.ar_verf.oa_length = 0; 493184588Sdfr reply_msg.acpted_rply.ar_results.where = NULL; 494184588Sdfr reply_msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void; 495177633Sdfr 496177633Sdfr mtx_lock(&cs->cs_lock); 497177633Sdfr if (error) { 498180025Sdfr TAILQ_REMOVE(&cs->cs_pending, cr, cr_link); 499184588Sdfr errp->re_errno = error; 500184588Sdfr errp->re_status = stat = RPC_CANTSEND; 501184588Sdfr cu->cu_sent -= CWNDSCALE; 502184588Sdfr if (cu->cu_cwnd_wait) { 503184588Sdfr cu->cu_cwnd_wait = FALSE; 504184588Sdfr wakeup(&cu->cu_cwnd_wait); 505184588Sdfr } 506177633Sdfr goto out; 507177633Sdfr } 508177633Sdfr 509177633Sdfr /* 510177633Sdfr * Check to see if we got an upcall while waiting for the 511180025Sdfr * lock. 512177633Sdfr */ 513180025Sdfr if (cr->cr_error) { 514180025Sdfr TAILQ_REMOVE(&cs->cs_pending, cr, cr_link); 515184588Sdfr errp->re_errno = cr->cr_error; 516184588Sdfr errp->re_status = stat = RPC_CANTRECV; 517184588Sdfr cu->cu_sent -= CWNDSCALE; 518184588Sdfr if (cu->cu_cwnd_wait) { 519184588Sdfr cu->cu_cwnd_wait = FALSE; 520184588Sdfr wakeup(&cu->cu_cwnd_wait); 521184588Sdfr } 522177633Sdfr goto out; 523177633Sdfr } 524180025Sdfr if (cr->cr_mrep) { 525180025Sdfr TAILQ_REMOVE(&cs->cs_pending, cr, cr_link); 526184588Sdfr cu->cu_sent -= CWNDSCALE; 527184588Sdfr if (cu->cu_cwnd_wait) { 528184588Sdfr cu->cu_cwnd_wait = FALSE; 529184588Sdfr wakeup(&cu->cu_cwnd_wait); 530184588Sdfr } 531177633Sdfr goto got_reply; 532177633Sdfr } 533177633Sdfr 534177633Sdfr /* 535177633Sdfr * Hack to provide rpc-based message passing 536177633Sdfr */ 537180025Sdfr if (timeout == 0) { 538180025Sdfr TAILQ_REMOVE(&cs->cs_pending, cr, cr_link); 539184588Sdfr errp->re_status = stat = RPC_TIMEDOUT; 540184588Sdfr cu->cu_sent -= CWNDSCALE; 541184588Sdfr if (cu->cu_cwnd_wait) { 542184588Sdfr cu->cu_cwnd_wait = FALSE; 543184588Sdfr wakeup(&cu->cu_cwnd_wait); 544184588Sdfr } 545177633Sdfr goto out; 546177633Sdfr } 547177633Sdfr 548177633Sdfrget_reply: 549177633Sdfr for (;;) { 550177633Sdfr /* Decide how long to wait. */ 551180025Sdfr if (next_sendtime < timeout) 552177633Sdfr tv = next_sendtime; 553180025Sdfr else 554180025Sdfr tv = timeout; 555180025Sdfr tv -= time_waited; 556180025Sdfr 557180025Sdfr if (tv > 0) { 558195245Srmacklem if (cu->cu_closing || cu->cu_closed) { 559180025Sdfr error = 0; 560195245Srmacklem cr->cr_error = ESHUTDOWN; 561195245Srmacklem } else { 562180025Sdfr error = msleep(cr, &cs->cs_lock, 563180025Sdfr cu->cu_waitflag, cu->cu_waitchan, tv); 564195245Srmacklem } 565177633Sdfr } else { 566180025Sdfr error = EWOULDBLOCK; 567177633Sdfr } 568177633Sdfr 569180025Sdfr TAILQ_REMOVE(&cs->cs_pending, cr, cr_link); 570184588Sdfr cu->cu_sent -= CWNDSCALE; 571184588Sdfr if (cu->cu_cwnd_wait) { 572184588Sdfr cu->cu_cwnd_wait = FALSE; 573184588Sdfr wakeup(&cu->cu_cwnd_wait); 574184588Sdfr } 575177633Sdfr 576177633Sdfr if (!error) { 577177633Sdfr /* 578177633Sdfr * We were woken up by the upcall. If the 579177633Sdfr * upcall had a receive error, report that, 580177633Sdfr * otherwise we have a reply. 581177633Sdfr */ 582180025Sdfr if (cr->cr_error) { 583184588Sdfr errp->re_errno = cr->cr_error; 584184588Sdfr errp->re_status = stat = RPC_CANTRECV; 585177633Sdfr goto out; 586177633Sdfr } 587184588Sdfr 588184588Sdfr cu->cu_cwnd += (CWNDSCALE * CWNDSCALE 589184588Sdfr + cu->cu_cwnd / 2) / cu->cu_cwnd; 590184588Sdfr if (cu->cu_cwnd > MAXCWND) 591184588Sdfr cu->cu_cwnd = MAXCWND; 592184588Sdfr 593184588Sdfr if (rt) { 594184588Sdfr /* 595184588Sdfr * Add one to the time since a tick 596184588Sdfr * count of N means that the actual 597184588Sdfr * time taken was somewhere between N 598184588Sdfr * and N+1. 599184588Sdfr */ 600184588Sdfr rtt = ticks - starttime + 1; 601184588Sdfr 602184588Sdfr /* 603184588Sdfr * Update our estimate of the round 604184588Sdfr * trip time using roughly the 605184588Sdfr * algorithm described in RFC 606184588Sdfr * 2988. Given an RTT sample R: 607184588Sdfr * 608184588Sdfr * RTTVAR = (1-beta) * RTTVAR + beta * |SRTT-R| 609184588Sdfr * SRTT = (1-alpha) * SRTT + alpha * R 610184588Sdfr * 611184588Sdfr * where alpha = 0.125 and beta = 0.25. 612184588Sdfr * 613184588Sdfr * The initial retransmit timeout is 614184588Sdfr * SRTT + 4*RTTVAR and doubles on each 615184588Sdfr * retransmision. 616184588Sdfr */ 617184588Sdfr if (rt->rt_srtt == 0) { 618184588Sdfr rt->rt_srtt = rtt; 619184588Sdfr rt->rt_deviate = rtt / 2; 620184588Sdfr } else { 621184588Sdfr int32_t error = rtt - rt->rt_srtt; 622184588Sdfr rt->rt_srtt += error / 8; 623184588Sdfr error = abs(error) - rt->rt_deviate; 624184588Sdfr rt->rt_deviate += error / 4; 625184588Sdfr } 626184588Sdfr rt->rt_rtxcur = rt->rt_srtt + 4*rt->rt_deviate; 627184588Sdfr } 628184588Sdfr 629177633Sdfr break; 630177633Sdfr } 631177633Sdfr 632177633Sdfr /* 633177633Sdfr * The sleep returned an error so our request is still 634177633Sdfr * on the list. If we got EWOULDBLOCK, we may want to 635177633Sdfr * re-send the request. 636177633Sdfr */ 637177633Sdfr if (error != EWOULDBLOCK) { 638184588Sdfr errp->re_errno = error; 639225234Sart if (error == EINTR || error == ERESTART) 640184588Sdfr errp->re_status = stat = RPC_INTR; 641177633Sdfr else 642184588Sdfr errp->re_status = stat = RPC_CANTRECV; 643177633Sdfr goto out; 644177633Sdfr } 645177633Sdfr 646180025Sdfr time_waited = ticks - starttime; 647177633Sdfr 648177633Sdfr /* Check for timeout. */ 649180025Sdfr if (time_waited > timeout) { 650184588Sdfr errp->re_errno = EWOULDBLOCK; 651184588Sdfr errp->re_status = stat = RPC_TIMEDOUT; 652177633Sdfr goto out; 653177633Sdfr } 654177633Sdfr 655177633Sdfr /* Retransmit if necessary. */ 656180025Sdfr if (time_waited >= next_sendtime) { 657184588Sdfr cu->cu_cwnd /= 2; 658184588Sdfr if (cu->cu_cwnd < CWNDSCALE) 659184588Sdfr cu->cu_cwnd = CWNDSCALE; 660180025Sdfr if (ext && ext->rc_feedback) { 661180025Sdfr mtx_unlock(&cs->cs_lock); 662180025Sdfr if (retrans == 0) 663180025Sdfr ext->rc_feedback(FEEDBACK_REXMIT1, 664180025Sdfr proc, ext->rc_feedback_arg); 665180025Sdfr else 666180025Sdfr ext->rc_feedback(FEEDBACK_REXMIT2, 667180025Sdfr proc, ext->rc_feedback_arg); 668180025Sdfr mtx_lock(&cs->cs_lock); 669180025Sdfr } 670184588Sdfr if (cu->cu_closing || cu->cu_closed) { 671184588Sdfr errp->re_errno = ESHUTDOWN; 672184588Sdfr errp->re_status = stat = RPC_CANTRECV; 673180025Sdfr goto out; 674180025Sdfr } 675180025Sdfr retrans++; 676177633Sdfr /* update retransmit_time */ 677180025Sdfr if (retransmit_time < RPC_MAX_BACKOFF * hz) 678180025Sdfr retransmit_time = 2 * retransmit_time; 679180025Sdfr next_sendtime += retransmit_time; 680177633Sdfr goto send_again; 681177633Sdfr } 682255284Srmacklem cu->cu_sent += CWNDSCALE; 683180025Sdfr TAILQ_INSERT_TAIL(&cs->cs_pending, cr, cr_link); 684177633Sdfr } 685177633Sdfr 686177633Sdfrgot_reply: 687177633Sdfr /* 688177633Sdfr * Now decode and validate the response. We need to drop the 689177633Sdfr * lock since xdr_replymsg may end up sleeping in malloc. 690177633Sdfr */ 691177633Sdfr mtx_unlock(&cs->cs_lock); 692177633Sdfr 693180025Sdfr if (ext && ext->rc_feedback) 694180025Sdfr ext->rc_feedback(FEEDBACK_OK, proc, ext->rc_feedback_arg); 695180025Sdfr 696180025Sdfr xdrmbuf_create(&xdrs, cr->cr_mrep, XDR_DECODE); 697177633Sdfr ok = xdr_replymsg(&xdrs, &reply_msg); 698180025Sdfr cr->cr_mrep = NULL; 699177633Sdfr 700177633Sdfr if (ok) { 701177633Sdfr if ((reply_msg.rm_reply.rp_stat == MSG_ACCEPTED) && 702184588Sdfr (reply_msg.acpted_rply.ar_stat == SUCCESS)) 703184588Sdfr errp->re_status = stat = RPC_SUCCESS; 704177633Sdfr else 705184588Sdfr stat = _seterr_reply(&reply_msg, &(cu->cu_error)); 706177633Sdfr 707184588Sdfr if (errp->re_status == RPC_SUCCESS) { 708184588Sdfr results = xdrmbuf_getall(&xdrs); 709184588Sdfr if (! AUTH_VALIDATE(auth, xid, 710184588Sdfr &reply_msg.acpted_rply.ar_verf, 711184588Sdfr &results)) { 712184588Sdfr errp->re_status = stat = RPC_AUTHERROR; 713184588Sdfr errp->re_why = AUTH_INVALIDRESP; 714184588Sdfr if (retrans && 715184588Sdfr auth->ah_cred.oa_flavor == RPCSEC_GSS) { 716184588Sdfr /* 717184588Sdfr * If we retransmitted, its 718184588Sdfr * possible that we will 719184588Sdfr * receive a reply for one of 720184588Sdfr * the earlier transmissions 721184588Sdfr * (which will use an older 722184588Sdfr * RPCSEC_GSS sequence 723184588Sdfr * number). In this case, just 724184588Sdfr * go back and listen for a 725184588Sdfr * new reply. We could keep a 726184588Sdfr * record of all the seq 727184588Sdfr * numbers we have transmitted 728184588Sdfr * so far so that we could 729184588Sdfr * accept a reply for any of 730184588Sdfr * them here. 731184588Sdfr */ 732184588Sdfr XDR_DESTROY(&xdrs); 733184588Sdfr mtx_lock(&cs->cs_lock); 734255284Srmacklem cu->cu_sent += CWNDSCALE; 735184588Sdfr TAILQ_INSERT_TAIL(&cs->cs_pending, 736184588Sdfr cr, cr_link); 737184588Sdfr cr->cr_mrep = NULL; 738184588Sdfr goto get_reply; 739184588Sdfr } 740184588Sdfr } else { 741184588Sdfr *resultsp = results; 742177633Sdfr } 743177633Sdfr } /* end successful completion */ 744177633Sdfr /* 745177633Sdfr * If unsuccesful AND error is an authentication error 746177633Sdfr * then refresh credentials and try again, else break 747177633Sdfr */ 748184588Sdfr else if (stat == RPC_AUTHERROR) 749177633Sdfr /* maybe our credentials need to be refreshed ... */ 750177633Sdfr if (nrefreshes > 0 && 751184588Sdfr AUTH_REFRESH(auth, &reply_msg)) { 752177633Sdfr nrefreshes--; 753184588Sdfr XDR_DESTROY(&xdrs); 754184588Sdfr mtx_lock(&cs->cs_lock); 755177633Sdfr goto call_again; 756177633Sdfr } 757177633Sdfr /* end of unsuccessful completion */ 758177633Sdfr } /* end of valid reply message */ 759177633Sdfr else { 760184588Sdfr errp->re_status = stat = RPC_CANTDECODERES; 761177633Sdfr 762177633Sdfr } 763184588Sdfr XDR_DESTROY(&xdrs); 764184588Sdfr mtx_lock(&cs->cs_lock); 765177633Sdfrout: 766177633Sdfr mtx_assert(&cs->cs_lock, MA_OWNED); 767177633Sdfr 768177633Sdfr if (mreq) 769177633Sdfr m_freem(mreq); 770180025Sdfr if (cr->cr_mrep) 771180025Sdfr m_freem(cr->cr_mrep); 772177633Sdfr 773180025Sdfr cu->cu_threads--; 774180025Sdfr if (cu->cu_closing) 775180025Sdfr wakeup(cu); 776180025Sdfr 777177633Sdfr mtx_unlock(&cs->cs_lock); 778180025Sdfr 779184588Sdfr if (auth && stat != RPC_SUCCESS) 780184588Sdfr AUTH_VALIDATE(auth, xid, NULL, NULL); 781184588Sdfr 782180025Sdfr free(cr, M_RPC); 783180025Sdfr 784184588Sdfr return (stat); 785177633Sdfr} 786177633Sdfr 787177633Sdfrstatic void 788177633Sdfrclnt_dg_geterr(CLIENT *cl, struct rpc_err *errp) 789177633Sdfr{ 790177633Sdfr struct cu_data *cu = (struct cu_data *)cl->cl_private; 791177633Sdfr 792177633Sdfr *errp = cu->cu_error; 793177633Sdfr} 794177633Sdfr 795177633Sdfrstatic bool_t 796177633Sdfrclnt_dg_freeres(CLIENT *cl, xdrproc_t xdr_res, void *res_ptr) 797177633Sdfr{ 798177633Sdfr XDR xdrs; 799177633Sdfr bool_t dummy; 800177633Sdfr 801177633Sdfr xdrs.x_op = XDR_FREE; 802177633Sdfr dummy = (*xdr_res)(&xdrs, res_ptr); 803177633Sdfr 804177633Sdfr return (dummy); 805177633Sdfr} 806177633Sdfr 807177633Sdfr/*ARGSUSED*/ 808177633Sdfrstatic void 809177633Sdfrclnt_dg_abort(CLIENT *h) 810177633Sdfr{ 811177633Sdfr} 812177633Sdfr 813177633Sdfrstatic bool_t 814177633Sdfrclnt_dg_control(CLIENT *cl, u_int request, void *info) 815177633Sdfr{ 816177633Sdfr struct cu_data *cu = (struct cu_data *)cl->cl_private; 817193272Sjhb struct cu_socket *cs; 818177633Sdfr struct sockaddr *addr; 819177633Sdfr 820193272Sjhb cs = cu->cu_socket->so_rcv.sb_upcallarg; 821177633Sdfr mtx_lock(&cs->cs_lock); 822177633Sdfr 823177633Sdfr switch (request) { 824177633Sdfr case CLSET_FD_CLOSE: 825177633Sdfr cu->cu_closeit = TRUE; 826177633Sdfr mtx_unlock(&cs->cs_lock); 827177633Sdfr return (TRUE); 828177633Sdfr case CLSET_FD_NCLOSE: 829177633Sdfr cu->cu_closeit = FALSE; 830177633Sdfr mtx_unlock(&cs->cs_lock); 831177633Sdfr return (TRUE); 832177633Sdfr } 833177633Sdfr 834177633Sdfr /* for other requests which use info */ 835177633Sdfr if (info == NULL) { 836177633Sdfr mtx_unlock(&cs->cs_lock); 837177633Sdfr return (FALSE); 838177633Sdfr } 839177633Sdfr switch (request) { 840177633Sdfr case CLSET_TIMEOUT: 841177633Sdfr if (time_not_ok((struct timeval *)info)) { 842177633Sdfr mtx_unlock(&cs->cs_lock); 843177633Sdfr return (FALSE); 844177633Sdfr } 845177633Sdfr cu->cu_total = *(struct timeval *)info; 846177633Sdfr break; 847177633Sdfr case CLGET_TIMEOUT: 848177633Sdfr *(struct timeval *)info = cu->cu_total; 849177633Sdfr break; 850177633Sdfr case CLSET_RETRY_TIMEOUT: 851177633Sdfr if (time_not_ok((struct timeval *)info)) { 852177633Sdfr mtx_unlock(&cs->cs_lock); 853177633Sdfr return (FALSE); 854177633Sdfr } 855177633Sdfr cu->cu_wait = *(struct timeval *)info; 856177633Sdfr break; 857177633Sdfr case CLGET_RETRY_TIMEOUT: 858177633Sdfr *(struct timeval *)info = cu->cu_wait; 859177633Sdfr break; 860177633Sdfr case CLGET_SVC_ADDR: 861177633Sdfr /* 862177633Sdfr * Slightly different semantics to userland - we use 863177633Sdfr * sockaddr instead of netbuf. 864177633Sdfr */ 865177633Sdfr memcpy(info, &cu->cu_raddr, cu->cu_raddr.ss_len); 866177633Sdfr break; 867177633Sdfr case CLSET_SVC_ADDR: /* set to new address */ 868177633Sdfr addr = (struct sockaddr *)info; 869177633Sdfr (void) memcpy(&cu->cu_raddr, addr, addr->sa_len); 870177633Sdfr break; 871177633Sdfr case CLGET_XID: 872177633Sdfr *(uint32_t *)info = cu->cu_xid; 873177633Sdfr break; 874177633Sdfr 875177633Sdfr case CLSET_XID: 876177633Sdfr /* This will set the xid of the NEXT call */ 877177633Sdfr /* decrement by 1 as clnt_dg_call() increments once */ 878177633Sdfr cu->cu_xid = *(uint32_t *)info - 1; 879177633Sdfr break; 880177633Sdfr 881177633Sdfr case CLGET_VERS: 882177633Sdfr /* 883177633Sdfr * This RELIES on the information that, in the call body, 884177633Sdfr * the version number field is the fifth field from the 885177633Sdfr * begining of the RPC header. MUST be changed if the 886177633Sdfr * call_struct is changed 887177633Sdfr */ 888177633Sdfr *(uint32_t *)info = 889177633Sdfr ntohl(*(uint32_t *)(void *)(cu->cu_mcallc + 890177633Sdfr 4 * BYTES_PER_XDR_UNIT)); 891177633Sdfr break; 892177633Sdfr 893177633Sdfr case CLSET_VERS: 894177633Sdfr *(uint32_t *)(void *)(cu->cu_mcallc + 4 * BYTES_PER_XDR_UNIT) 895177633Sdfr = htonl(*(uint32_t *)info); 896177633Sdfr break; 897177633Sdfr 898177633Sdfr case CLGET_PROG: 899177633Sdfr /* 900177633Sdfr * This RELIES on the information that, in the call body, 901177633Sdfr * the program number field is the fourth field from the 902177633Sdfr * begining of the RPC header. MUST be changed if the 903177633Sdfr * call_struct is changed 904177633Sdfr */ 905177633Sdfr *(uint32_t *)info = 906177633Sdfr ntohl(*(uint32_t *)(void *)(cu->cu_mcallc + 907177633Sdfr 3 * BYTES_PER_XDR_UNIT)); 908177633Sdfr break; 909177633Sdfr 910177633Sdfr case CLSET_PROG: 911177633Sdfr *(uint32_t *)(void *)(cu->cu_mcallc + 3 * BYTES_PER_XDR_UNIT) 912177633Sdfr = htonl(*(uint32_t *)info); 913177633Sdfr break; 914177633Sdfr case CLSET_ASYNC: 915177633Sdfr cu->cu_async = *(int *)info; 916177633Sdfr break; 917177633Sdfr case CLSET_CONNECT: 918177633Sdfr cu->cu_connect = *(int *)info; 919177633Sdfr break; 920177633Sdfr case CLSET_WAITCHAN: 921184588Sdfr cu->cu_waitchan = (const char *)info; 922177633Sdfr break; 923177633Sdfr case CLGET_WAITCHAN: 924177633Sdfr *(const char **) info = cu->cu_waitchan; 925177633Sdfr break; 926177633Sdfr case CLSET_INTERRUPTIBLE: 927177633Sdfr if (*(int *) info) 928177633Sdfr cu->cu_waitflag = PCATCH; 929177633Sdfr else 930177633Sdfr cu->cu_waitflag = 0; 931177633Sdfr break; 932177633Sdfr case CLGET_INTERRUPTIBLE: 933177633Sdfr if (cu->cu_waitflag) 934177633Sdfr *(int *) info = TRUE; 935177633Sdfr else 936177633Sdfr *(int *) info = FALSE; 937177633Sdfr break; 938177633Sdfr default: 939177633Sdfr mtx_unlock(&cs->cs_lock); 940177633Sdfr return (FALSE); 941177633Sdfr } 942177633Sdfr mtx_unlock(&cs->cs_lock); 943177633Sdfr return (TRUE); 944177633Sdfr} 945177633Sdfr 946177633Sdfrstatic void 947184588Sdfrclnt_dg_close(CLIENT *cl) 948177633Sdfr{ 949177633Sdfr struct cu_data *cu = (struct cu_data *)cl->cl_private; 950193272Sjhb struct cu_socket *cs; 951180025Sdfr struct cu_request *cr; 952177633Sdfr 953193272Sjhb cs = cu->cu_socket->so_rcv.sb_upcallarg; 954180025Sdfr mtx_lock(&cs->cs_lock); 955177633Sdfr 956184588Sdfr if (cu->cu_closed) { 957184588Sdfr mtx_unlock(&cs->cs_lock); 958184588Sdfr return; 959184588Sdfr } 960184588Sdfr 961184588Sdfr if (cu->cu_closing) { 962184588Sdfr while (cu->cu_closing) 963184588Sdfr msleep(cu, &cs->cs_lock, 0, "rpcclose", 0); 964184588Sdfr KASSERT(cu->cu_closed, ("client should be closed")); 965184588Sdfr mtx_unlock(&cs->cs_lock); 966184588Sdfr return; 967184588Sdfr } 968184588Sdfr 969180025Sdfr /* 970180025Sdfr * Abort any pending requests and wait until everyone 971180025Sdfr * has finished with clnt_vc_call. 972180025Sdfr */ 973180025Sdfr cu->cu_closing = TRUE; 974180025Sdfr TAILQ_FOREACH(cr, &cs->cs_pending, cr_link) { 975180025Sdfr if (cr->cr_client == cl) { 976180025Sdfr cr->cr_xid = 0; 977180025Sdfr cr->cr_error = ESHUTDOWN; 978180025Sdfr wakeup(cr); 979180025Sdfr } 980180025Sdfr } 981180025Sdfr 982180025Sdfr while (cu->cu_threads) 983180025Sdfr msleep(cu, &cs->cs_lock, 0, "rpcclose", 0); 984180025Sdfr 985184588Sdfr cu->cu_closing = FALSE; 986184588Sdfr cu->cu_closed = TRUE; 987184588Sdfr 988184588Sdfr mtx_unlock(&cs->cs_lock); 989184588Sdfr wakeup(cu); 990184588Sdfr} 991184588Sdfr 992184588Sdfrstatic void 993184588Sdfrclnt_dg_destroy(CLIENT *cl) 994184588Sdfr{ 995184588Sdfr struct cu_data *cu = (struct cu_data *)cl->cl_private; 996193272Sjhb struct cu_socket *cs; 997184588Sdfr struct socket *so = NULL; 998184588Sdfr bool_t lastsocketref; 999184588Sdfr 1000193272Sjhb cs = cu->cu_socket->so_rcv.sb_upcallarg; 1001184588Sdfr clnt_dg_close(cl); 1002184588Sdfr 1003227059Srmacklem SOCKBUF_LOCK(&cu->cu_socket->so_rcv); 1004184588Sdfr mtx_lock(&cs->cs_lock); 1005184588Sdfr 1006177633Sdfr cs->cs_refs--; 1007177633Sdfr if (cs->cs_refs == 0) { 1008193437Srmacklem mtx_unlock(&cs->cs_lock); 1009193272Sjhb soupcall_clear(cu->cu_socket, SO_RCV); 1010193437Srmacklem clnt_dg_upcallsdone(cu->cu_socket, cs); 1011177633Sdfr SOCKBUF_UNLOCK(&cu->cu_socket->so_rcv); 1012193437Srmacklem mtx_destroy(&cs->cs_lock); 1013177633Sdfr mem_free(cs, sizeof(*cs)); 1014177633Sdfr lastsocketref = TRUE; 1015177633Sdfr } else { 1016177633Sdfr mtx_unlock(&cs->cs_lock); 1017227059Srmacklem SOCKBUF_UNLOCK(&cu->cu_socket->so_rcv); 1018177633Sdfr lastsocketref = FALSE; 1019177633Sdfr } 1020177633Sdfr 1021180025Sdfr if (cu->cu_closeit && lastsocketref) { 1022177633Sdfr so = cu->cu_socket; 1023177633Sdfr cu->cu_socket = NULL; 1024177633Sdfr } 1025177633Sdfr 1026177633Sdfr if (so) 1027177633Sdfr soclose(so); 1028177633Sdfr 1029177633Sdfr if (cl->cl_netid && cl->cl_netid[0]) 1030177633Sdfr mem_free(cl->cl_netid, strlen(cl->cl_netid) +1); 1031177633Sdfr if (cl->cl_tp && cl->cl_tp[0]) 1032177633Sdfr mem_free(cl->cl_tp, strlen(cl->cl_tp) +1); 1033177633Sdfr mem_free(cu, sizeof (*cu)); 1034177633Sdfr mem_free(cl, sizeof (CLIENT)); 1035177633Sdfr} 1036177633Sdfr 1037177633Sdfr/* 1038177633Sdfr * Make sure that the time is not garbage. -1 value is allowed. 1039177633Sdfr */ 1040177633Sdfrstatic bool_t 1041177633Sdfrtime_not_ok(struct timeval *t) 1042177633Sdfr{ 1043177633Sdfr return (t->tv_sec < -1 || t->tv_sec > 100000000 || 1044177633Sdfr t->tv_usec < -1 || t->tv_usec > 1000000); 1045177633Sdfr} 1046177633Sdfr 1047193272Sjhbint 1048177633Sdfrclnt_dg_soupcall(struct socket *so, void *arg, int waitflag) 1049177633Sdfr{ 1050177633Sdfr struct cu_socket *cs = (struct cu_socket *) arg; 1051177633Sdfr struct uio uio; 1052177633Sdfr struct mbuf *m; 1053177633Sdfr struct mbuf *control; 1054177633Sdfr struct cu_request *cr; 1055177633Sdfr int error, rcvflag, foundreq; 1056177633Sdfr uint32_t xid; 1057177633Sdfr 1058193437Srmacklem cs->cs_upcallrefs++; 1059177633Sdfr uio.uio_resid = 1000000000; 1060177633Sdfr uio.uio_td = curthread; 1061177633Sdfr do { 1062193272Sjhb SOCKBUF_UNLOCK(&so->so_rcv); 1063177633Sdfr m = NULL; 1064177633Sdfr control = NULL; 1065177633Sdfr rcvflag = MSG_DONTWAIT; 1066177633Sdfr error = soreceive(so, NULL, &uio, &m, &control, &rcvflag); 1067177633Sdfr if (control) 1068177633Sdfr m_freem(control); 1069193272Sjhb SOCKBUF_LOCK(&so->so_rcv); 1070177633Sdfr 1071177633Sdfr if (error == EWOULDBLOCK) 1072177633Sdfr break; 1073177633Sdfr 1074177633Sdfr /* 1075177633Sdfr * If there was an error, wake up all pending 1076177633Sdfr * requests. 1077177633Sdfr */ 1078177633Sdfr if (error) { 1079177633Sdfr mtx_lock(&cs->cs_lock); 1080177633Sdfr TAILQ_FOREACH(cr, &cs->cs_pending, cr_link) { 1081180025Sdfr cr->cr_xid = 0; 1082177633Sdfr cr->cr_error = error; 1083177633Sdfr wakeup(cr); 1084177633Sdfr } 1085177633Sdfr mtx_unlock(&cs->cs_lock); 1086177633Sdfr break; 1087177633Sdfr } 1088177633Sdfr 1089177633Sdfr /* 1090177633Sdfr * The XID is in the first uint32_t of the reply. 1091177633Sdfr */ 1092220585Srmacklem if (m->m_len < sizeof(xid) && m_length(m, NULL) < sizeof(xid)) { 1093180025Sdfr /* 1094180025Sdfr * Should never happen. 1095180025Sdfr */ 1096220585Srmacklem m_freem(m); 1097180025Sdfr continue; 1098220585Srmacklem } 1099180025Sdfr 1100217242Srmacklem m_copydata(m, 0, sizeof(xid), (char *)&xid); 1101217242Srmacklem xid = ntohl(xid); 1102177633Sdfr 1103177633Sdfr /* 1104177633Sdfr * Attempt to match this reply with a pending request. 1105177633Sdfr */ 1106177633Sdfr mtx_lock(&cs->cs_lock); 1107177633Sdfr foundreq = 0; 1108177633Sdfr TAILQ_FOREACH(cr, &cs->cs_pending, cr_link) { 1109177633Sdfr if (cr->cr_xid == xid) { 1110177633Sdfr /* 1111180025Sdfr * This one matches. We leave the 1112177633Sdfr * reply mbuf in cr->cr_mrep. Set the 1113180025Sdfr * XID to zero so that we will ignore 1114180025Sdfr * any duplicated replies that arrive 1115180025Sdfr * before clnt_dg_call removes it from 1116180025Sdfr * the queue. 1117177633Sdfr */ 1118177633Sdfr cr->cr_xid = 0; 1119177633Sdfr cr->cr_mrep = m; 1120177633Sdfr cr->cr_error = 0; 1121177633Sdfr foundreq = 1; 1122177633Sdfr wakeup(cr); 1123177633Sdfr break; 1124177633Sdfr } 1125177633Sdfr } 1126177633Sdfr mtx_unlock(&cs->cs_lock); 1127177633Sdfr 1128177633Sdfr /* 1129177633Sdfr * If we didn't find the matching request, just drop 1130177633Sdfr * it - its probably a repeated reply. 1131177633Sdfr */ 1132177633Sdfr if (!foundreq) 1133177633Sdfr m_freem(m); 1134177633Sdfr } while (m); 1135193437Srmacklem cs->cs_upcallrefs--; 1136193437Srmacklem if (cs->cs_upcallrefs < 0) 1137193437Srmacklem panic("rpcdg upcall refcnt"); 1138193437Srmacklem if (cs->cs_upcallrefs == 0) 1139193437Srmacklem wakeup(&cs->cs_upcallrefs); 1140193272Sjhb return (SU_OK); 1141177633Sdfr} 1142177633Sdfr 1143193437Srmacklem/* 1144193437Srmacklem * Wait for all upcalls in progress to complete. 1145193437Srmacklem */ 1146193437Srmacklemstatic void 1147193437Srmacklemclnt_dg_upcallsdone(struct socket *so, struct cu_socket *cs) 1148193437Srmacklem{ 1149193437Srmacklem 1150193437Srmacklem SOCKBUF_LOCK_ASSERT(&so->so_rcv); 1151193437Srmacklem 1152193437Srmacklem while (cs->cs_upcallrefs > 0) 1153193437Srmacklem (void) msleep(&cs->cs_upcallrefs, SOCKBUF_MTX(&so->so_rcv), 0, 1154193437Srmacklem "rpcdgup", 0); 1155193437Srmacklem} 1156