clnt_dg.c revision 196503
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 196503 2009-08-24 10:09:30Z zec $");
41177633Sdfr
42177633Sdfr/*
43177633Sdfr * Implements a connectionless client side RPC.
44177633Sdfr */
45177633Sdfr
46177633Sdfr#include <sys/param.h>
47177633Sdfr#include <sys/systm.h>
48180025Sdfr#include <sys/kernel.h>
49177633Sdfr#include <sys/lock.h>
50177633Sdfr#include <sys/malloc.h>
51177633Sdfr#include <sys/mbuf.h>
52177633Sdfr#include <sys/mutex.h>
53177633Sdfr#include <sys/pcpu.h>
54177633Sdfr#include <sys/proc.h>
55177633Sdfr#include <sys/socket.h>
56177633Sdfr#include <sys/socketvar.h>
57177633Sdfr#include <sys/time.h>
58177633Sdfr#include <sys/uio.h>
59177633Sdfr
60196503Szec#include <net/vnet.h>
61196503Szec
62177633Sdfr#include <rpc/rpc.h>
63177685Sdfr#include <rpc/rpc_com.h>
64177633Sdfr
65177633Sdfr
66177633Sdfr#ifdef _FREEFALL_CONFIG
67177633Sdfr/*
68177633Sdfr * Disable RPC exponential back-off for FreeBSD.org systems.
69177633Sdfr */
70177633Sdfr#define	RPC_MAX_BACKOFF		1 /* second */
71177633Sdfr#else
72177633Sdfr#define	RPC_MAX_BACKOFF		30 /* seconds */
73177633Sdfr#endif
74177633Sdfr
75177633Sdfrstatic bool_t time_not_ok(struct timeval *);
76180025Sdfrstatic enum clnt_stat clnt_dg_call(CLIENT *, struct rpc_callextra *,
77184588Sdfr    rpcproc_t, struct mbuf *, struct mbuf **, struct timeval);
78177633Sdfrstatic void clnt_dg_geterr(CLIENT *, struct rpc_err *);
79177633Sdfrstatic bool_t clnt_dg_freeres(CLIENT *, xdrproc_t, void *);
80177633Sdfrstatic void clnt_dg_abort(CLIENT *);
81177633Sdfrstatic bool_t clnt_dg_control(CLIENT *, u_int, void *);
82184588Sdfrstatic void clnt_dg_close(CLIENT *);
83177633Sdfrstatic void clnt_dg_destroy(CLIENT *);
84193272Sjhbstatic int clnt_dg_soupcall(struct socket *so, void *arg, int waitflag);
85177633Sdfr
86177633Sdfrstatic struct clnt_ops clnt_dg_ops = {
87177633Sdfr	.cl_call =	clnt_dg_call,
88177633Sdfr	.cl_abort =	clnt_dg_abort,
89177633Sdfr	.cl_geterr =	clnt_dg_geterr,
90177633Sdfr	.cl_freeres =	clnt_dg_freeres,
91184588Sdfr	.cl_close =	clnt_dg_close,
92177633Sdfr	.cl_destroy =	clnt_dg_destroy,
93177633Sdfr	.cl_control =	clnt_dg_control
94177633Sdfr};
95177633Sdfr
96177633Sdfrstatic const char mem_err_clnt_dg[] = "clnt_dg_create: out of memory";
97177633Sdfr
98177633Sdfr/*
99180025Sdfr * A pending RPC request which awaits a reply. Requests which have
100180025Sdfr * received their reply will have cr_xid set to zero and cr_mrep to
101180025Sdfr * the mbuf chain of the reply.
102177633Sdfr */
103177633Sdfrstruct cu_request {
104177633Sdfr	TAILQ_ENTRY(cu_request) cr_link;
105180025Sdfr	CLIENT			*cr_client;	/* owner */
106177633Sdfr	uint32_t		cr_xid;		/* XID of request */
107177633Sdfr	struct mbuf		*cr_mrep;	/* reply received by upcall */
108177633Sdfr	int			cr_error;	/* any error from upcall */
109184588Sdfr	char			cr_verf[MAX_AUTH_BYTES]; /* reply verf */
110177633Sdfr};
111177633Sdfr
112177633SdfrTAILQ_HEAD(cu_request_list, cu_request);
113177633Sdfr
114177633Sdfr#define MCALL_MSG_SIZE 24
115177633Sdfr
116177633Sdfr/*
117193272Sjhb * This structure is pointed to by the socket buffer's sb_upcallarg
118177633Sdfr * member. It is separate from the client private data to facilitate
119177633Sdfr * multiple clients sharing the same socket. The cs_lock mutex is used
120177633Sdfr * to protect all fields of this structure, the socket's receive
121177633Sdfr * buffer SOCKBUF_LOCK is used to ensure that exactly one of these
122177633Sdfr * structures is installed on the socket.
123177633Sdfr */
124177633Sdfrstruct cu_socket {
125177633Sdfr	struct mtx		cs_lock;
126177633Sdfr	int			cs_refs;	/* Count of clients */
127177633Sdfr	struct cu_request_list	cs_pending;	/* Requests awaiting replies */
128193437Srmacklem	int			cs_upcallrefs;	/* Refcnt of upcalls in prog.*/
129177633Sdfr};
130177633Sdfr
131193437Srmacklemstatic void clnt_dg_upcallsdone(struct socket *, struct cu_socket *);
132193437Srmacklem
133177633Sdfr/*
134177633Sdfr * Private data kept per client handle
135177633Sdfr */
136177633Sdfrstruct cu_data {
137180025Sdfr	int			cu_threads;	/* # threads in clnt_vc_call */
138184588Sdfr	bool_t			cu_closing;	/* TRUE if we are closing */
139184588Sdfr	bool_t			cu_closed;	/* TRUE if we are closed */
140177633Sdfr	struct socket		*cu_socket;	/* connection socket */
141177633Sdfr	bool_t			cu_closeit;	/* opened by library */
142177633Sdfr	struct sockaddr_storage	cu_raddr;	/* remote address */
143177633Sdfr	int			cu_rlen;
144177633Sdfr	struct timeval		cu_wait;	/* retransmit interval */
145177633Sdfr	struct timeval		cu_total;	/* total time for the call */
146177633Sdfr	struct rpc_err		cu_error;
147177633Sdfr	uint32_t		cu_xid;
148177633Sdfr	char			cu_mcallc[MCALL_MSG_SIZE]; /* marshalled callmsg */
149177633Sdfr	size_t			cu_mcalllen;
150177633Sdfr	size_t			cu_sendsz;	/* send size */
151177633Sdfr	size_t			cu_recvsz;	/* recv size */
152177633Sdfr	int			cu_async;
153177633Sdfr	int			cu_connect;	/* Use connect(). */
154177633Sdfr	int			cu_connected;	/* Have done connect(). */
155177633Sdfr	const char		*cu_waitchan;
156177633Sdfr	int			cu_waitflag;
157184588Sdfr	int			cu_cwnd;	/* congestion window */
158184588Sdfr	int			cu_sent;	/* number of in-flight RPCs */
159184588Sdfr	bool_t			cu_cwnd_wait;
160177633Sdfr};
161177633Sdfr
162184588Sdfr#define CWNDSCALE	256
163184588Sdfr#define MAXCWND		(32 * CWNDSCALE)
164184588Sdfr
165177633Sdfr/*
166177633Sdfr * Connection less client creation returns with client handle parameters.
167177633Sdfr * Default options are set, which the user can change using clnt_control().
168177633Sdfr * fd should be open and bound.
169177633Sdfr * NB: The rpch->cl_auth is initialized to null authentication.
170177633Sdfr * 	Caller may wish to set this something more useful.
171177633Sdfr *
172177633Sdfr * sendsz and recvsz are the maximum allowable packet sizes that can be
173177633Sdfr * sent and received. Normally they are the same, but they can be
174177633Sdfr * changed to improve the program efficiency and buffer allocation.
175177633Sdfr * If they are 0, use the transport default.
176177633Sdfr *
177177633Sdfr * If svcaddr is NULL, returns NULL.
178177633Sdfr */
179177633SdfrCLIENT *
180177633Sdfrclnt_dg_create(
181177633Sdfr	struct socket *so,
182177633Sdfr	struct sockaddr *svcaddr,	/* servers address */
183177633Sdfr	rpcprog_t program,		/* program number */
184177633Sdfr	rpcvers_t version,		/* version number */
185177633Sdfr	size_t sendsz,			/* buffer recv size */
186177633Sdfr	size_t recvsz)			/* buffer send size */
187177633Sdfr{
188177633Sdfr	CLIENT *cl = NULL;		/* client handle */
189177633Sdfr	struct cu_data *cu = NULL;	/* private data */
190177633Sdfr	struct cu_socket *cs = NULL;
191193272Sjhb	struct sockbuf *sb;
192177633Sdfr	struct timeval now;
193177633Sdfr	struct rpc_msg call_msg;
194177633Sdfr	struct __rpc_sockinfo si;
195177633Sdfr	XDR xdrs;
196177633Sdfr
197177633Sdfr	if (svcaddr == NULL) {
198177633Sdfr		rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
199177633Sdfr		return (NULL);
200177633Sdfr	}
201177633Sdfr
202196503Szec	CURVNET_SET(so->so_vnet);
203177633Sdfr	if (!__rpc_socket2sockinfo(so, &si)) {
204177633Sdfr		rpc_createerr.cf_stat = RPC_TLIERROR;
205177633Sdfr		rpc_createerr.cf_error.re_errno = 0;
206196503Szec		CURVNET_RESTORE();
207177633Sdfr		return (NULL);
208177633Sdfr	}
209196503Szec	CURVNET_RESTORE();
210177633Sdfr
211177633Sdfr	/*
212177633Sdfr	 * Find the receive and the send size
213177633Sdfr	 */
214177633Sdfr	sendsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsz);
215177633Sdfr	recvsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsz);
216177633Sdfr	if ((sendsz == 0) || (recvsz == 0)) {
217177633Sdfr		rpc_createerr.cf_stat = RPC_TLIERROR; /* XXX */
218177633Sdfr		rpc_createerr.cf_error.re_errno = 0;
219177633Sdfr		return (NULL);
220177633Sdfr	}
221177633Sdfr
222177633Sdfr	cl = mem_alloc(sizeof (CLIENT));
223177633Sdfr
224177633Sdfr	/*
225177633Sdfr	 * Should be multiple of 4 for XDR.
226177633Sdfr	 */
227177633Sdfr	sendsz = ((sendsz + 3) / 4) * 4;
228177633Sdfr	recvsz = ((recvsz + 3) / 4) * 4;
229177633Sdfr	cu = mem_alloc(sizeof (*cu));
230180025Sdfr	cu->cu_threads = 0;
231180025Sdfr	cu->cu_closing = FALSE;
232184588Sdfr	cu->cu_closed = FALSE;
233177633Sdfr	(void) memcpy(&cu->cu_raddr, svcaddr, (size_t)svcaddr->sa_len);
234177633Sdfr	cu->cu_rlen = svcaddr->sa_len;
235177633Sdfr	/* Other values can also be set through clnt_control() */
236180025Sdfr	cu->cu_wait.tv_sec = 3;	/* heuristically chosen */
237177633Sdfr	cu->cu_wait.tv_usec = 0;
238177633Sdfr	cu->cu_total.tv_sec = -1;
239177633Sdfr	cu->cu_total.tv_usec = -1;
240177633Sdfr	cu->cu_sendsz = sendsz;
241177633Sdfr	cu->cu_recvsz = recvsz;
242177633Sdfr	cu->cu_async = FALSE;
243177633Sdfr	cu->cu_connect = FALSE;
244177633Sdfr	cu->cu_connected = FALSE;
245177633Sdfr	cu->cu_waitchan = "rpcrecv";
246177633Sdfr	cu->cu_waitflag = 0;
247184588Sdfr	cu->cu_cwnd = MAXCWND / 2;
248184588Sdfr	cu->cu_sent = 0;
249184588Sdfr	cu->cu_cwnd_wait = FALSE;
250177633Sdfr	(void) getmicrotime(&now);
251177633Sdfr	cu->cu_xid = __RPC_GETXID(&now);
252177633Sdfr	call_msg.rm_xid = cu->cu_xid;
253177633Sdfr	call_msg.rm_call.cb_prog = program;
254177633Sdfr	call_msg.rm_call.cb_vers = version;
255177633Sdfr	xdrmem_create(&xdrs, cu->cu_mcallc, MCALL_MSG_SIZE, XDR_ENCODE);
256177633Sdfr	if (! xdr_callhdr(&xdrs, &call_msg)) {
257177633Sdfr		rpc_createerr.cf_stat = RPC_CANTENCODEARGS;  /* XXX */
258177633Sdfr		rpc_createerr.cf_error.re_errno = 0;
259177633Sdfr		goto err2;
260177633Sdfr	}
261177633Sdfr	cu->cu_mcalllen = XDR_GETPOS(&xdrs);;
262177633Sdfr
263177633Sdfr	/*
264177633Sdfr	 * By default, closeit is always FALSE. It is users responsibility
265177633Sdfr	 * to do a close on it, else the user may use clnt_control
266177633Sdfr	 * to let clnt_destroy do it for him/her.
267177633Sdfr	 */
268177633Sdfr	cu->cu_closeit = FALSE;
269177633Sdfr	cu->cu_socket = so;
270180025Sdfr	soreserve(so, 256*1024, 256*1024);
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
431177633Sdfr	MGETHDR(mreq, M_WAIT, 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;
467184588Sdfr			errp->re_status = stat = RPC_CANTSEND;
468184588Sdfr			goto out;
469184588Sdfr		}
470184588Sdfr	}
471184588Sdfr	cu->cu_sent += CWNDSCALE;
472184588Sdfr
473180025Sdfr	TAILQ_INSERT_TAIL(&cs->cs_pending, cr, cr_link);
474177633Sdfr	mtx_unlock(&cs->cs_lock);
475177633Sdfr
476177633Sdfr	/*
477177633Sdfr	 * sosend consumes mreq.
478177633Sdfr	 */
479177633Sdfr	error = sosend(cu->cu_socket, sa, NULL, mreq, NULL, 0, curthread);
480177633Sdfr	mreq = NULL;
481177633Sdfr
482177633Sdfr	/*
483177633Sdfr	 * sub-optimal code appears here because we have
484177633Sdfr	 * some clock time to spare while the packets are in flight.
485177633Sdfr	 * (We assume that this is actually only executed once.)
486177633Sdfr	 */
487184588Sdfr	reply_msg.acpted_rply.ar_verf.oa_flavor = AUTH_NULL;
488184588Sdfr	reply_msg.acpted_rply.ar_verf.oa_base = cr->cr_verf;
489184588Sdfr	reply_msg.acpted_rply.ar_verf.oa_length = 0;
490184588Sdfr	reply_msg.acpted_rply.ar_results.where = NULL;
491184588Sdfr	reply_msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
492177633Sdfr
493177633Sdfr	mtx_lock(&cs->cs_lock);
494177633Sdfr	if (error) {
495180025Sdfr		TAILQ_REMOVE(&cs->cs_pending, cr, cr_link);
496184588Sdfr		errp->re_errno = error;
497184588Sdfr		errp->re_status = stat = RPC_CANTSEND;
498184588Sdfr		cu->cu_sent -= CWNDSCALE;
499184588Sdfr		if (cu->cu_cwnd_wait) {
500184588Sdfr			cu->cu_cwnd_wait = FALSE;
501184588Sdfr			wakeup(&cu->cu_cwnd_wait);
502184588Sdfr		}
503177633Sdfr		goto out;
504177633Sdfr	}
505177633Sdfr
506177633Sdfr	/*
507177633Sdfr	 * Check to see if we got an upcall while waiting for the
508180025Sdfr	 * lock.
509177633Sdfr	 */
510180025Sdfr	if (cr->cr_error) {
511180025Sdfr		TAILQ_REMOVE(&cs->cs_pending, cr, cr_link);
512184588Sdfr		errp->re_errno = cr->cr_error;
513184588Sdfr		errp->re_status = stat = RPC_CANTRECV;
514184588Sdfr		cu->cu_sent -= CWNDSCALE;
515184588Sdfr		if (cu->cu_cwnd_wait) {
516184588Sdfr			cu->cu_cwnd_wait = FALSE;
517184588Sdfr			wakeup(&cu->cu_cwnd_wait);
518184588Sdfr		}
519177633Sdfr		goto out;
520177633Sdfr	}
521180025Sdfr	if (cr->cr_mrep) {
522180025Sdfr		TAILQ_REMOVE(&cs->cs_pending, cr, cr_link);
523184588Sdfr		cu->cu_sent -= CWNDSCALE;
524184588Sdfr		if (cu->cu_cwnd_wait) {
525184588Sdfr			cu->cu_cwnd_wait = FALSE;
526184588Sdfr			wakeup(&cu->cu_cwnd_wait);
527184588Sdfr		}
528177633Sdfr		goto got_reply;
529177633Sdfr	}
530177633Sdfr
531177633Sdfr	/*
532177633Sdfr	 * Hack to provide rpc-based message passing
533177633Sdfr	 */
534180025Sdfr	if (timeout == 0) {
535180025Sdfr		TAILQ_REMOVE(&cs->cs_pending, cr, cr_link);
536184588Sdfr		errp->re_status = stat = RPC_TIMEDOUT;
537184588Sdfr		cu->cu_sent -= CWNDSCALE;
538184588Sdfr		if (cu->cu_cwnd_wait) {
539184588Sdfr			cu->cu_cwnd_wait = FALSE;
540184588Sdfr			wakeup(&cu->cu_cwnd_wait);
541184588Sdfr		}
542177633Sdfr		goto out;
543177633Sdfr	}
544177633Sdfr
545177633Sdfrget_reply:
546177633Sdfr	for (;;) {
547177633Sdfr		/* Decide how long to wait. */
548180025Sdfr		if (next_sendtime < timeout)
549177633Sdfr			tv = next_sendtime;
550180025Sdfr		else
551180025Sdfr			tv = timeout;
552180025Sdfr		tv -= time_waited;
553180025Sdfr
554180025Sdfr		if (tv > 0) {
555195245Srmacklem			if (cu->cu_closing || cu->cu_closed) {
556180025Sdfr				error = 0;
557195245Srmacklem				cr->cr_error = ESHUTDOWN;
558195245Srmacklem			} else {
559180025Sdfr				error = msleep(cr, &cs->cs_lock,
560180025Sdfr				    cu->cu_waitflag, cu->cu_waitchan, tv);
561195245Srmacklem			}
562177633Sdfr		} else {
563180025Sdfr			error = EWOULDBLOCK;
564177633Sdfr		}
565177633Sdfr
566180025Sdfr		TAILQ_REMOVE(&cs->cs_pending, cr, cr_link);
567184588Sdfr		cu->cu_sent -= CWNDSCALE;
568184588Sdfr		if (cu->cu_cwnd_wait) {
569184588Sdfr			cu->cu_cwnd_wait = FALSE;
570184588Sdfr			wakeup(&cu->cu_cwnd_wait);
571184588Sdfr		}
572177633Sdfr
573177633Sdfr		if (!error) {
574177633Sdfr			/*
575177633Sdfr			 * We were woken up by the upcall.  If the
576177633Sdfr			 * upcall had a receive error, report that,
577177633Sdfr			 * otherwise we have a reply.
578177633Sdfr			 */
579180025Sdfr			if (cr->cr_error) {
580184588Sdfr				errp->re_errno = cr->cr_error;
581184588Sdfr				errp->re_status = stat = RPC_CANTRECV;
582177633Sdfr				goto out;
583177633Sdfr			}
584184588Sdfr
585184588Sdfr			cu->cu_cwnd += (CWNDSCALE * CWNDSCALE
586184588Sdfr			    + cu->cu_cwnd / 2) / cu->cu_cwnd;
587184588Sdfr			if (cu->cu_cwnd > MAXCWND)
588184588Sdfr				cu->cu_cwnd = MAXCWND;
589184588Sdfr
590184588Sdfr			if (rt) {
591184588Sdfr				/*
592184588Sdfr				 * Add one to the time since a tick
593184588Sdfr				 * count of N means that the actual
594184588Sdfr				 * time taken was somewhere between N
595184588Sdfr				 * and N+1.
596184588Sdfr				 */
597184588Sdfr				rtt = ticks - starttime + 1;
598184588Sdfr
599184588Sdfr				/*
600184588Sdfr				 * Update our estimate of the round
601184588Sdfr				 * trip time using roughly the
602184588Sdfr				 * algorithm described in RFC
603184588Sdfr				 * 2988. Given an RTT sample R:
604184588Sdfr				 *
605184588Sdfr				 * RTTVAR = (1-beta) * RTTVAR + beta * |SRTT-R|
606184588Sdfr				 * SRTT = (1-alpha) * SRTT + alpha * R
607184588Sdfr				 *
608184588Sdfr				 * where alpha = 0.125 and beta = 0.25.
609184588Sdfr				 *
610184588Sdfr				 * The initial retransmit timeout is
611184588Sdfr				 * SRTT + 4*RTTVAR and doubles on each
612184588Sdfr				 * retransmision.
613184588Sdfr				 */
614184588Sdfr				if (rt->rt_srtt == 0) {
615184588Sdfr					rt->rt_srtt = rtt;
616184588Sdfr					rt->rt_deviate = rtt / 2;
617184588Sdfr				} else {
618184588Sdfr					int32_t error = rtt - rt->rt_srtt;
619184588Sdfr					rt->rt_srtt += error / 8;
620184588Sdfr					error = abs(error) - rt->rt_deviate;
621184588Sdfr					rt->rt_deviate += error / 4;
622184588Sdfr				}
623184588Sdfr				rt->rt_rtxcur = rt->rt_srtt + 4*rt->rt_deviate;
624184588Sdfr			}
625184588Sdfr
626177633Sdfr			break;
627177633Sdfr		}
628177633Sdfr
629177633Sdfr		/*
630177633Sdfr		 * The sleep returned an error so our request is still
631177633Sdfr		 * on the list. If we got EWOULDBLOCK, we may want to
632177633Sdfr		 * re-send the request.
633177633Sdfr		 */
634177633Sdfr		if (error != EWOULDBLOCK) {
635184588Sdfr			errp->re_errno = error;
636177633Sdfr			if (error == EINTR)
637184588Sdfr				errp->re_status = stat = RPC_INTR;
638177633Sdfr			else
639184588Sdfr				errp->re_status = stat = RPC_CANTRECV;
640177633Sdfr			goto out;
641177633Sdfr		}
642177633Sdfr
643180025Sdfr		time_waited = ticks - starttime;
644177633Sdfr
645177633Sdfr		/* Check for timeout. */
646180025Sdfr		if (time_waited > timeout) {
647184588Sdfr			errp->re_errno = EWOULDBLOCK;
648184588Sdfr			errp->re_status = stat = RPC_TIMEDOUT;
649177633Sdfr			goto out;
650177633Sdfr		}
651177633Sdfr
652177633Sdfr		/* Retransmit if necessary. */
653180025Sdfr		if (time_waited >= next_sendtime) {
654184588Sdfr			cu->cu_cwnd /= 2;
655184588Sdfr			if (cu->cu_cwnd < CWNDSCALE)
656184588Sdfr				cu->cu_cwnd = CWNDSCALE;
657180025Sdfr			if (ext && ext->rc_feedback) {
658180025Sdfr				mtx_unlock(&cs->cs_lock);
659180025Sdfr				if (retrans == 0)
660180025Sdfr					ext->rc_feedback(FEEDBACK_REXMIT1,
661180025Sdfr					    proc, ext->rc_feedback_arg);
662180025Sdfr				else
663180025Sdfr					ext->rc_feedback(FEEDBACK_REXMIT2,
664180025Sdfr					    proc, ext->rc_feedback_arg);
665180025Sdfr				mtx_lock(&cs->cs_lock);
666180025Sdfr			}
667184588Sdfr			if (cu->cu_closing || cu->cu_closed) {
668184588Sdfr				errp->re_errno = ESHUTDOWN;
669184588Sdfr				errp->re_status = stat = RPC_CANTRECV;
670180025Sdfr				goto out;
671180025Sdfr			}
672180025Sdfr			retrans++;
673177633Sdfr			/* update retransmit_time */
674180025Sdfr			if (retransmit_time < RPC_MAX_BACKOFF * hz)
675180025Sdfr				retransmit_time = 2 * retransmit_time;
676180025Sdfr			next_sendtime += retransmit_time;
677177633Sdfr			goto send_again;
678177633Sdfr		}
679180025Sdfr		TAILQ_INSERT_TAIL(&cs->cs_pending, cr, cr_link);
680177633Sdfr	}
681177633Sdfr
682177633Sdfrgot_reply:
683177633Sdfr	/*
684177633Sdfr	 * Now decode and validate the response. We need to drop the
685177633Sdfr	 * lock since xdr_replymsg may end up sleeping in malloc.
686177633Sdfr	 */
687177633Sdfr	mtx_unlock(&cs->cs_lock);
688177633Sdfr
689180025Sdfr	if (ext && ext->rc_feedback)
690180025Sdfr		ext->rc_feedback(FEEDBACK_OK, proc, ext->rc_feedback_arg);
691180025Sdfr
692180025Sdfr	xdrmbuf_create(&xdrs, cr->cr_mrep, XDR_DECODE);
693177633Sdfr	ok = xdr_replymsg(&xdrs, &reply_msg);
694180025Sdfr	cr->cr_mrep = NULL;
695177633Sdfr
696177633Sdfr	if (ok) {
697177633Sdfr		if ((reply_msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
698184588Sdfr		    (reply_msg.acpted_rply.ar_stat == SUCCESS))
699184588Sdfr			errp->re_status = stat = RPC_SUCCESS;
700177633Sdfr		else
701184588Sdfr			stat = _seterr_reply(&reply_msg, &(cu->cu_error));
702177633Sdfr
703184588Sdfr		if (errp->re_status == RPC_SUCCESS) {
704184588Sdfr			results = xdrmbuf_getall(&xdrs);
705184588Sdfr			if (! AUTH_VALIDATE(auth, xid,
706184588Sdfr				&reply_msg.acpted_rply.ar_verf,
707184588Sdfr				&results)) {
708184588Sdfr				errp->re_status = stat = RPC_AUTHERROR;
709184588Sdfr				errp->re_why = AUTH_INVALIDRESP;
710184588Sdfr				if (retrans &&
711184588Sdfr				    auth->ah_cred.oa_flavor == RPCSEC_GSS) {
712184588Sdfr					/*
713184588Sdfr					 * If we retransmitted, its
714184588Sdfr					 * possible that we will
715184588Sdfr					 * receive a reply for one of
716184588Sdfr					 * the earlier transmissions
717184588Sdfr					 * (which will use an older
718184588Sdfr					 * RPCSEC_GSS sequence
719184588Sdfr					 * number). In this case, just
720184588Sdfr					 * go back and listen for a
721184588Sdfr					 * new reply. We could keep a
722184588Sdfr					 * record of all the seq
723184588Sdfr					 * numbers we have transmitted
724184588Sdfr					 * so far so that we could
725184588Sdfr					 * accept a reply for any of
726184588Sdfr					 * them here.
727184588Sdfr					 */
728184588Sdfr					XDR_DESTROY(&xdrs);
729184588Sdfr					mtx_lock(&cs->cs_lock);
730184588Sdfr					TAILQ_INSERT_TAIL(&cs->cs_pending,
731184588Sdfr					    cr, cr_link);
732184588Sdfr					cr->cr_mrep = NULL;
733184588Sdfr					goto get_reply;
734184588Sdfr				}
735184588Sdfr			} else {
736184588Sdfr				*resultsp = results;
737177633Sdfr			}
738177633Sdfr		}		/* end successful completion */
739177633Sdfr		/*
740177633Sdfr		 * If unsuccesful AND error is an authentication error
741177633Sdfr		 * then refresh credentials and try again, else break
742177633Sdfr		 */
743184588Sdfr		else if (stat == RPC_AUTHERROR)
744177633Sdfr			/* maybe our credentials need to be refreshed ... */
745177633Sdfr			if (nrefreshes > 0 &&
746184588Sdfr			    AUTH_REFRESH(auth, &reply_msg)) {
747177633Sdfr				nrefreshes--;
748184588Sdfr				XDR_DESTROY(&xdrs);
749184588Sdfr				mtx_lock(&cs->cs_lock);
750177633Sdfr				goto call_again;
751177633Sdfr			}
752177633Sdfr		/* end of unsuccessful completion */
753177633Sdfr	}	/* end of valid reply message */
754177633Sdfr	else {
755184588Sdfr		errp->re_status = stat = RPC_CANTDECODERES;
756177633Sdfr
757177633Sdfr	}
758184588Sdfr	XDR_DESTROY(&xdrs);
759184588Sdfr	mtx_lock(&cs->cs_lock);
760177633Sdfrout:
761177633Sdfr	mtx_assert(&cs->cs_lock, MA_OWNED);
762177633Sdfr
763177633Sdfr	if (mreq)
764177633Sdfr		m_freem(mreq);
765180025Sdfr	if (cr->cr_mrep)
766180025Sdfr		m_freem(cr->cr_mrep);
767177633Sdfr
768180025Sdfr	cu->cu_threads--;
769180025Sdfr	if (cu->cu_closing)
770180025Sdfr		wakeup(cu);
771180025Sdfr
772177633Sdfr	mtx_unlock(&cs->cs_lock);
773180025Sdfr
774184588Sdfr	if (auth && stat != RPC_SUCCESS)
775184588Sdfr		AUTH_VALIDATE(auth, xid, NULL, NULL);
776184588Sdfr
777180025Sdfr	free(cr, M_RPC);
778180025Sdfr
779184588Sdfr	return (stat);
780177633Sdfr}
781177633Sdfr
782177633Sdfrstatic void
783177633Sdfrclnt_dg_geterr(CLIENT *cl, struct rpc_err *errp)
784177633Sdfr{
785177633Sdfr	struct cu_data *cu = (struct cu_data *)cl->cl_private;
786177633Sdfr
787177633Sdfr	*errp = cu->cu_error;
788177633Sdfr}
789177633Sdfr
790177633Sdfrstatic bool_t
791177633Sdfrclnt_dg_freeres(CLIENT *cl, xdrproc_t xdr_res, void *res_ptr)
792177633Sdfr{
793177633Sdfr	XDR xdrs;
794177633Sdfr	bool_t dummy;
795177633Sdfr
796177633Sdfr	xdrs.x_op = XDR_FREE;
797177633Sdfr	dummy = (*xdr_res)(&xdrs, res_ptr);
798177633Sdfr
799177633Sdfr	return (dummy);
800177633Sdfr}
801177633Sdfr
802177633Sdfr/*ARGSUSED*/
803177633Sdfrstatic void
804177633Sdfrclnt_dg_abort(CLIENT *h)
805177633Sdfr{
806177633Sdfr}
807177633Sdfr
808177633Sdfrstatic bool_t
809177633Sdfrclnt_dg_control(CLIENT *cl, u_int request, void *info)
810177633Sdfr{
811177633Sdfr	struct cu_data *cu = (struct cu_data *)cl->cl_private;
812193272Sjhb	struct cu_socket *cs;
813177633Sdfr	struct sockaddr *addr;
814177633Sdfr
815193272Sjhb	cs = cu->cu_socket->so_rcv.sb_upcallarg;
816177633Sdfr	mtx_lock(&cs->cs_lock);
817177633Sdfr
818177633Sdfr	switch (request) {
819177633Sdfr	case CLSET_FD_CLOSE:
820177633Sdfr		cu->cu_closeit = TRUE;
821177633Sdfr		mtx_unlock(&cs->cs_lock);
822177633Sdfr		return (TRUE);
823177633Sdfr	case CLSET_FD_NCLOSE:
824177633Sdfr		cu->cu_closeit = FALSE;
825177633Sdfr		mtx_unlock(&cs->cs_lock);
826177633Sdfr		return (TRUE);
827177633Sdfr	}
828177633Sdfr
829177633Sdfr	/* for other requests which use info */
830177633Sdfr	if (info == NULL) {
831177633Sdfr		mtx_unlock(&cs->cs_lock);
832177633Sdfr		return (FALSE);
833177633Sdfr	}
834177633Sdfr	switch (request) {
835177633Sdfr	case CLSET_TIMEOUT:
836177633Sdfr		if (time_not_ok((struct timeval *)info)) {
837177633Sdfr			mtx_unlock(&cs->cs_lock);
838177633Sdfr			return (FALSE);
839177633Sdfr		}
840177633Sdfr		cu->cu_total = *(struct timeval *)info;
841177633Sdfr		break;
842177633Sdfr	case CLGET_TIMEOUT:
843177633Sdfr		*(struct timeval *)info = cu->cu_total;
844177633Sdfr		break;
845177633Sdfr	case CLSET_RETRY_TIMEOUT:
846177633Sdfr		if (time_not_ok((struct timeval *)info)) {
847177633Sdfr			mtx_unlock(&cs->cs_lock);
848177633Sdfr			return (FALSE);
849177633Sdfr		}
850177633Sdfr		cu->cu_wait = *(struct timeval *)info;
851177633Sdfr		break;
852177633Sdfr	case CLGET_RETRY_TIMEOUT:
853177633Sdfr		*(struct timeval *)info = cu->cu_wait;
854177633Sdfr		break;
855177633Sdfr	case CLGET_SVC_ADDR:
856177633Sdfr		/*
857177633Sdfr		 * Slightly different semantics to userland - we use
858177633Sdfr		 * sockaddr instead of netbuf.
859177633Sdfr		 */
860177633Sdfr		memcpy(info, &cu->cu_raddr, cu->cu_raddr.ss_len);
861177633Sdfr		break;
862177633Sdfr	case CLSET_SVC_ADDR:		/* set to new address */
863177633Sdfr		addr = (struct sockaddr *)info;
864177633Sdfr		(void) memcpy(&cu->cu_raddr, addr, addr->sa_len);
865177633Sdfr		break;
866177633Sdfr	case CLGET_XID:
867177633Sdfr		*(uint32_t *)info = cu->cu_xid;
868177633Sdfr		break;
869177633Sdfr
870177633Sdfr	case CLSET_XID:
871177633Sdfr		/* This will set the xid of the NEXT call */
872177633Sdfr		/* decrement by 1 as clnt_dg_call() increments once */
873177633Sdfr		cu->cu_xid = *(uint32_t *)info - 1;
874177633Sdfr		break;
875177633Sdfr
876177633Sdfr	case CLGET_VERS:
877177633Sdfr		/*
878177633Sdfr		 * This RELIES on the information that, in the call body,
879177633Sdfr		 * the version number field is the fifth field from the
880177633Sdfr		 * begining of the RPC header. MUST be changed if the
881177633Sdfr		 * call_struct is changed
882177633Sdfr		 */
883177633Sdfr		*(uint32_t *)info =
884177633Sdfr		    ntohl(*(uint32_t *)(void *)(cu->cu_mcallc +
885177633Sdfr		    4 * BYTES_PER_XDR_UNIT));
886177633Sdfr		break;
887177633Sdfr
888177633Sdfr	case CLSET_VERS:
889177633Sdfr		*(uint32_t *)(void *)(cu->cu_mcallc + 4 * BYTES_PER_XDR_UNIT)
890177633Sdfr			= htonl(*(uint32_t *)info);
891177633Sdfr		break;
892177633Sdfr
893177633Sdfr	case CLGET_PROG:
894177633Sdfr		/*
895177633Sdfr		 * This RELIES on the information that, in the call body,
896177633Sdfr		 * the program number field is the fourth field from the
897177633Sdfr		 * begining of the RPC header. MUST be changed if the
898177633Sdfr		 * call_struct is changed
899177633Sdfr		 */
900177633Sdfr		*(uint32_t *)info =
901177633Sdfr		    ntohl(*(uint32_t *)(void *)(cu->cu_mcallc +
902177633Sdfr		    3 * BYTES_PER_XDR_UNIT));
903177633Sdfr		break;
904177633Sdfr
905177633Sdfr	case CLSET_PROG:
906177633Sdfr		*(uint32_t *)(void *)(cu->cu_mcallc + 3 * BYTES_PER_XDR_UNIT)
907177633Sdfr			= htonl(*(uint32_t *)info);
908177633Sdfr		break;
909177633Sdfr	case CLSET_ASYNC:
910177633Sdfr		cu->cu_async = *(int *)info;
911177633Sdfr		break;
912177633Sdfr	case CLSET_CONNECT:
913177633Sdfr		cu->cu_connect = *(int *)info;
914177633Sdfr		break;
915177633Sdfr	case CLSET_WAITCHAN:
916184588Sdfr		cu->cu_waitchan = (const char *)info;
917177633Sdfr		break;
918177633Sdfr	case CLGET_WAITCHAN:
919177633Sdfr		*(const char **) info = cu->cu_waitchan;
920177633Sdfr		break;
921177633Sdfr	case CLSET_INTERRUPTIBLE:
922177633Sdfr		if (*(int *) info)
923177633Sdfr			cu->cu_waitflag = PCATCH;
924177633Sdfr		else
925177633Sdfr			cu->cu_waitflag = 0;
926177633Sdfr		break;
927177633Sdfr	case CLGET_INTERRUPTIBLE:
928177633Sdfr		if (cu->cu_waitflag)
929177633Sdfr			*(int *) info = TRUE;
930177633Sdfr		else
931177633Sdfr			*(int *) info = FALSE;
932177633Sdfr		break;
933177633Sdfr	default:
934177633Sdfr		mtx_unlock(&cs->cs_lock);
935177633Sdfr		return (FALSE);
936177633Sdfr	}
937177633Sdfr	mtx_unlock(&cs->cs_lock);
938177633Sdfr	return (TRUE);
939177633Sdfr}
940177633Sdfr
941177633Sdfrstatic void
942184588Sdfrclnt_dg_close(CLIENT *cl)
943177633Sdfr{
944177633Sdfr	struct cu_data *cu = (struct cu_data *)cl->cl_private;
945193272Sjhb	struct cu_socket *cs;
946180025Sdfr	struct cu_request *cr;
947177633Sdfr
948193272Sjhb	cs = cu->cu_socket->so_rcv.sb_upcallarg;
949180025Sdfr	mtx_lock(&cs->cs_lock);
950177633Sdfr
951184588Sdfr	if (cu->cu_closed) {
952184588Sdfr		mtx_unlock(&cs->cs_lock);
953184588Sdfr		return;
954184588Sdfr	}
955184588Sdfr
956184588Sdfr	if (cu->cu_closing) {
957184588Sdfr		while (cu->cu_closing)
958184588Sdfr			msleep(cu, &cs->cs_lock, 0, "rpcclose", 0);
959184588Sdfr		KASSERT(cu->cu_closed, ("client should be closed"));
960184588Sdfr		mtx_unlock(&cs->cs_lock);
961184588Sdfr		return;
962184588Sdfr	}
963184588Sdfr
964180025Sdfr	/*
965180025Sdfr	 * Abort any pending requests and wait until everyone
966180025Sdfr	 * has finished with clnt_vc_call.
967180025Sdfr	 */
968180025Sdfr	cu->cu_closing = TRUE;
969180025Sdfr	TAILQ_FOREACH(cr, &cs->cs_pending, cr_link) {
970180025Sdfr		if (cr->cr_client == cl) {
971180025Sdfr			cr->cr_xid = 0;
972180025Sdfr			cr->cr_error = ESHUTDOWN;
973180025Sdfr			wakeup(cr);
974180025Sdfr		}
975180025Sdfr	}
976180025Sdfr
977180025Sdfr	while (cu->cu_threads)
978180025Sdfr		msleep(cu, &cs->cs_lock, 0, "rpcclose", 0);
979180025Sdfr
980184588Sdfr	cu->cu_closing = FALSE;
981184588Sdfr	cu->cu_closed = TRUE;
982184588Sdfr
983184588Sdfr	mtx_unlock(&cs->cs_lock);
984184588Sdfr	wakeup(cu);
985184588Sdfr}
986184588Sdfr
987184588Sdfrstatic void
988184588Sdfrclnt_dg_destroy(CLIENT *cl)
989184588Sdfr{
990184588Sdfr	struct cu_data *cu = (struct cu_data *)cl->cl_private;
991193272Sjhb	struct cu_socket *cs;
992184588Sdfr	struct socket *so = NULL;
993184588Sdfr	bool_t lastsocketref;
994184588Sdfr
995193272Sjhb	cs = cu->cu_socket->so_rcv.sb_upcallarg;
996184588Sdfr	clnt_dg_close(cl);
997184588Sdfr
998184588Sdfr	mtx_lock(&cs->cs_lock);
999184588Sdfr
1000177633Sdfr	cs->cs_refs--;
1001177633Sdfr	if (cs->cs_refs == 0) {
1002193437Srmacklem		mtx_unlock(&cs->cs_lock);
1003180025Sdfr		SOCKBUF_LOCK(&cu->cu_socket->so_rcv);
1004193272Sjhb		soupcall_clear(cu->cu_socket, SO_RCV);
1005193437Srmacklem		clnt_dg_upcallsdone(cu->cu_socket, cs);
1006177633Sdfr		SOCKBUF_UNLOCK(&cu->cu_socket->so_rcv);
1007193437Srmacklem		mtx_destroy(&cs->cs_lock);
1008177633Sdfr		mem_free(cs, sizeof(*cs));
1009177633Sdfr		lastsocketref = TRUE;
1010177633Sdfr	} else {
1011177633Sdfr		mtx_unlock(&cs->cs_lock);
1012177633Sdfr		lastsocketref = FALSE;
1013177633Sdfr	}
1014177633Sdfr
1015180025Sdfr	if (cu->cu_closeit && lastsocketref) {
1016177633Sdfr		so = cu->cu_socket;
1017177633Sdfr		cu->cu_socket = NULL;
1018177633Sdfr	}
1019177633Sdfr
1020177633Sdfr	if (so)
1021177633Sdfr		soclose(so);
1022177633Sdfr
1023177633Sdfr	if (cl->cl_netid && cl->cl_netid[0])
1024177633Sdfr		mem_free(cl->cl_netid, strlen(cl->cl_netid) +1);
1025177633Sdfr	if (cl->cl_tp && cl->cl_tp[0])
1026177633Sdfr		mem_free(cl->cl_tp, strlen(cl->cl_tp) +1);
1027177633Sdfr	mem_free(cu, sizeof (*cu));
1028177633Sdfr	mem_free(cl, sizeof (CLIENT));
1029177633Sdfr}
1030177633Sdfr
1031177633Sdfr/*
1032177633Sdfr * Make sure that the time is not garbage.  -1 value is allowed.
1033177633Sdfr */
1034177633Sdfrstatic bool_t
1035177633Sdfrtime_not_ok(struct timeval *t)
1036177633Sdfr{
1037177633Sdfr	return (t->tv_sec < -1 || t->tv_sec > 100000000 ||
1038177633Sdfr		t->tv_usec < -1 || t->tv_usec > 1000000);
1039177633Sdfr}
1040177633Sdfr
1041193272Sjhbint
1042177633Sdfrclnt_dg_soupcall(struct socket *so, void *arg, int waitflag)
1043177633Sdfr{
1044177633Sdfr	struct cu_socket *cs = (struct cu_socket *) arg;
1045177633Sdfr	struct uio uio;
1046177633Sdfr	struct mbuf *m;
1047177633Sdfr	struct mbuf *control;
1048177633Sdfr	struct cu_request *cr;
1049177633Sdfr	int error, rcvflag, foundreq;
1050177633Sdfr	uint32_t xid;
1051177633Sdfr
1052193437Srmacklem	cs->cs_upcallrefs++;
1053177633Sdfr	uio.uio_resid = 1000000000;
1054177633Sdfr	uio.uio_td = curthread;
1055177633Sdfr	do {
1056193272Sjhb		SOCKBUF_UNLOCK(&so->so_rcv);
1057177633Sdfr		m = NULL;
1058177633Sdfr		control = NULL;
1059177633Sdfr		rcvflag = MSG_DONTWAIT;
1060177633Sdfr		error = soreceive(so, NULL, &uio, &m, &control, &rcvflag);
1061177633Sdfr		if (control)
1062177633Sdfr			m_freem(control);
1063193272Sjhb		SOCKBUF_LOCK(&so->so_rcv);
1064177633Sdfr
1065177633Sdfr		if (error == EWOULDBLOCK)
1066177633Sdfr			break;
1067177633Sdfr
1068177633Sdfr		/*
1069177633Sdfr		 * If there was an error, wake up all pending
1070177633Sdfr		 * requests.
1071177633Sdfr		 */
1072177633Sdfr		if (error) {
1073177633Sdfr			mtx_lock(&cs->cs_lock);
1074177633Sdfr			TAILQ_FOREACH(cr, &cs->cs_pending, cr_link) {
1075180025Sdfr				cr->cr_xid = 0;
1076177633Sdfr				cr->cr_error = error;
1077177633Sdfr				wakeup(cr);
1078177633Sdfr			}
1079177633Sdfr			mtx_unlock(&cs->cs_lock);
1080177633Sdfr			break;
1081177633Sdfr		}
1082177633Sdfr
1083177633Sdfr		/*
1084177633Sdfr		 * The XID is in the first uint32_t of the reply.
1085177633Sdfr		 */
1086184588Sdfr		if (m->m_len < sizeof(xid))
1087184588Sdfr			m = m_pullup(m, sizeof(xid));
1088177633Sdfr		if (!m)
1089180025Sdfr			/*
1090180025Sdfr			 * Should never happen.
1091180025Sdfr			 */
1092180025Sdfr			continue;
1093180025Sdfr
1094177633Sdfr		xid = ntohl(*mtod(m, uint32_t *));
1095177633Sdfr
1096177633Sdfr		/*
1097177633Sdfr		 * Attempt to match this reply with a pending request.
1098177633Sdfr		 */
1099177633Sdfr		mtx_lock(&cs->cs_lock);
1100177633Sdfr		foundreq = 0;
1101177633Sdfr		TAILQ_FOREACH(cr, &cs->cs_pending, cr_link) {
1102177633Sdfr			if (cr->cr_xid == xid) {
1103177633Sdfr				/*
1104180025Sdfr				 * This one matches. We leave the
1105177633Sdfr				 * reply mbuf in cr->cr_mrep. Set the
1106180025Sdfr				 * XID to zero so that we will ignore
1107180025Sdfr				 * any duplicated replies that arrive
1108180025Sdfr				 * before clnt_dg_call removes it from
1109180025Sdfr				 * the queue.
1110177633Sdfr				 */
1111177633Sdfr				cr->cr_xid = 0;
1112177633Sdfr				cr->cr_mrep = m;
1113177633Sdfr				cr->cr_error = 0;
1114177633Sdfr				foundreq = 1;
1115177633Sdfr				wakeup(cr);
1116177633Sdfr				break;
1117177633Sdfr			}
1118177633Sdfr		}
1119177633Sdfr		mtx_unlock(&cs->cs_lock);
1120177633Sdfr
1121177633Sdfr		/*
1122177633Sdfr		 * If we didn't find the matching request, just drop
1123177633Sdfr		 * it - its probably a repeated reply.
1124177633Sdfr		 */
1125177633Sdfr		if (!foundreq)
1126177633Sdfr			m_freem(m);
1127177633Sdfr	} while (m);
1128193437Srmacklem	cs->cs_upcallrefs--;
1129193437Srmacklem	if (cs->cs_upcallrefs < 0)
1130193437Srmacklem		panic("rpcdg upcall refcnt");
1131193437Srmacklem	if (cs->cs_upcallrefs == 0)
1132193437Srmacklem		wakeup(&cs->cs_upcallrefs);
1133193272Sjhb	return (SU_OK);
1134177633Sdfr}
1135177633Sdfr
1136193437Srmacklem/*
1137193437Srmacklem * Wait for all upcalls in progress to complete.
1138193437Srmacklem */
1139193437Srmacklemstatic void
1140193437Srmacklemclnt_dg_upcallsdone(struct socket *so, struct cu_socket *cs)
1141193437Srmacklem{
1142193437Srmacklem
1143193437Srmacklem	SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1144193437Srmacklem
1145193437Srmacklem	while (cs->cs_upcallrefs > 0)
1146193437Srmacklem		(void) msleep(&cs->cs_upcallrefs, SOCKBUF_MTX(&so->so_rcv), 0,
1147193437Srmacklem		    "rpcdgup", 0);
1148193437Srmacklem}
1149