10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*
280Sstevel@tonic-gate  * Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T
290Sstevel@tonic-gate  *		All Rights Reserved
300Sstevel@tonic-gate  */
310Sstevel@tonic-gate 
320Sstevel@tonic-gate /*
330Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
340Sstevel@tonic-gate  * under license from the Regents of the University of California.
350Sstevel@tonic-gate  */
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
380Sstevel@tonic-gate 
390Sstevel@tonic-gate /*
400Sstevel@tonic-gate  * Implements a kernel based, client side RPC over Connection Oriented
410Sstevel@tonic-gate  * Transports (COTS).
420Sstevel@tonic-gate  */
430Sstevel@tonic-gate 
440Sstevel@tonic-gate /*
450Sstevel@tonic-gate  * Much of this file has been re-written to let NFS work better over slow
460Sstevel@tonic-gate  * transports. A description follows.
470Sstevel@tonic-gate  *
480Sstevel@tonic-gate  * One of the annoying things about kRPC/COTS is that it will temporarily
490Sstevel@tonic-gate  * create more than one connection between a client and server. This
500Sstevel@tonic-gate  * happens because when a connection is made, the end-points entry in the
510Sstevel@tonic-gate  * linked list of connections (headed by cm_hd), is removed so that other
520Sstevel@tonic-gate  * threads don't mess with it. Went ahead and bit the bullet by keeping
530Sstevel@tonic-gate  * the endpoint on the connection list and introducing state bits,
540Sstevel@tonic-gate  * condition variables etc. to the connection entry data structure (struct
550Sstevel@tonic-gate  * cm_xprt).
560Sstevel@tonic-gate  *
570Sstevel@tonic-gate  * Here is a summary of the changes to cm-xprt:
580Sstevel@tonic-gate  *
590Sstevel@tonic-gate  *	x_ctime is the timestamp of when the endpoint was last
600Sstevel@tonic-gate  *	connected or disconnected. If an end-point is ever disconnected
610Sstevel@tonic-gate  *	or re-connected, then any outstanding RPC request is presumed
620Sstevel@tonic-gate  *	lost, telling clnt_cots_kcallit that it needs to re-send the
630Sstevel@tonic-gate  *	request, not just wait for the original request's reply to
640Sstevel@tonic-gate  *	arrive.
650Sstevel@tonic-gate  *
660Sstevel@tonic-gate  *	x_thread flag which tells us if a thread is doing a connection attempt.
670Sstevel@tonic-gate  *
680Sstevel@tonic-gate  *	x_waitdis flag which tells us we are waiting a disconnect ACK.
690Sstevel@tonic-gate  *
700Sstevel@tonic-gate  *	x_needdis flag which tells us we need to send a T_DISCONN_REQ
710Sstevel@tonic-gate  *	to kill the connection.
720Sstevel@tonic-gate  *
730Sstevel@tonic-gate  *	x_needrel flag which tells us we need to send a T_ORDREL_REQ to
740Sstevel@tonic-gate  *	gracefully close the connection.
750Sstevel@tonic-gate  *
760Sstevel@tonic-gate  *	#defined bitmasks for the all the b_* bits so that more
770Sstevel@tonic-gate  *	efficient (and at times less clumsy) masks can be used to
780Sstevel@tonic-gate  *	manipulated state in cases where multiple bits have to
790Sstevel@tonic-gate  *	set/cleared/checked in the same critical section.
800Sstevel@tonic-gate  *
810Sstevel@tonic-gate  *	x_conn_cv and x_dis-_cv are new condition variables to let
820Sstevel@tonic-gate  *	threads knows when the connection attempt is done, and to let
830Sstevel@tonic-gate  *	the connecting thread know when the disconnect handshake is
840Sstevel@tonic-gate  *	done.
850Sstevel@tonic-gate  *
860Sstevel@tonic-gate  * Added the CONN_HOLD() macro so that all reference holds have the same
870Sstevel@tonic-gate  * look and feel.
880Sstevel@tonic-gate  *
890Sstevel@tonic-gate  * In the private (cku_private) portion of the client handle,
900Sstevel@tonic-gate  *
910Sstevel@tonic-gate  *	cku_flags replaces the cku_sent a boolean. cku_flags keeps
920Sstevel@tonic-gate  *	track of whether a request as been sent, and whether the
930Sstevel@tonic-gate  *	client's handles call record is on the dispatch list (so that
940Sstevel@tonic-gate  *	the reply can be matched by XID to the right client handle).
950Sstevel@tonic-gate  *	The idea of CKU_ONQUEUE is that we can exit clnt_cots_kcallit()
960Sstevel@tonic-gate  *	and still have the response find the right client handle so
970Sstevel@tonic-gate  *	that the retry of CLNT_CALL() gets the result. Testing, found
980Sstevel@tonic-gate  *	situations where if the timeout was increased, performance
990Sstevel@tonic-gate  *	degraded. This was due to us hitting a window where the thread
1000Sstevel@tonic-gate  *	was back in rfscall() (probably printing server not responding)
1010Sstevel@tonic-gate  *	while the response came back but no place to put it.
1020Sstevel@tonic-gate  *
1030Sstevel@tonic-gate  *	cku_ctime is just a cache of x_ctime. If they match,
1040Sstevel@tonic-gate  *	clnt_cots_kcallit() won't to send a retry (unless the maximum
1050Sstevel@tonic-gate  *	receive count limit as been reached). If the don't match, then
1060Sstevel@tonic-gate  *	we assume the request has been lost, and a retry of the request
1070Sstevel@tonic-gate  *	is needed.
1080Sstevel@tonic-gate  *
1090Sstevel@tonic-gate  *	cku_recv_attempts counts the number of receive count attempts
1100Sstevel@tonic-gate  *	after one try is sent on the wire.
1110Sstevel@tonic-gate  *
1120Sstevel@tonic-gate  * Added the clnt_delay() routine so that interruptible and
1130Sstevel@tonic-gate  * noninterruptible delays are possible.
1140Sstevel@tonic-gate  *
1150Sstevel@tonic-gate  * CLNT_MIN_TIMEOUT has been bumped to 10 seconds from 3. This is used to
1160Sstevel@tonic-gate  * control how long the client delays before returned after getting
1170Sstevel@tonic-gate  * ECONNREFUSED. At 3 seconds, 8 client threads per mount really does bash
1180Sstevel@tonic-gate  * a server that may be booting and not yet started nfsd.
1190Sstevel@tonic-gate  *
1200Sstevel@tonic-gate  * CLNT_MAXRECV_WITHOUT_RETRY is a new macro (value of 3) (with a tunable)
1210Sstevel@tonic-gate  * Why don't we just wait forever (receive an infinite # of times)?
1220Sstevel@tonic-gate  * Because the server may have rebooted. More insidious is that some
1230Sstevel@tonic-gate  * servers (ours) will drop NFS/TCP requests in some cases. This is bad,
1240Sstevel@tonic-gate  * but it is a reality.
1250Sstevel@tonic-gate  *
1260Sstevel@tonic-gate  * The case of a server doing orderly release really messes up the
1270Sstevel@tonic-gate  * client's recovery, especially if the server's TCP implementation is
1280Sstevel@tonic-gate  * buggy.  It was found was that the kRPC/COTS client was breaking some
1290Sstevel@tonic-gate  * TPI rules, such as not waiting for the acknowledgement of a
1300Sstevel@tonic-gate  * T_DISCON_REQ (hence the added case statements T_ERROR_ACK, T_OK_ACK and
1310Sstevel@tonic-gate  * T_DISCON_REQ in clnt_dispatch_notifyall()).
1320Sstevel@tonic-gate  *
1330Sstevel@tonic-gate  * One of things that we've seen is that a kRPC TCP endpoint goes into
1340Sstevel@tonic-gate  * TIMEWAIT and a thus a reconnect takes a long time to satisfy because
1350Sstevel@tonic-gate  * that the TIMEWAIT state takes a while to finish.  If a server sends a
1360Sstevel@tonic-gate  * T_ORDREL_IND, there is little point in an RPC client doing a
1370Sstevel@tonic-gate  * T_ORDREL_REQ, because the RPC request isn't going to make it (the
1380Sstevel@tonic-gate  * server is saying that it won't accept any more data). So kRPC was
1390Sstevel@tonic-gate  * changed to send a T_DISCON_REQ when we get a T_ORDREL_IND. So now the
1400Sstevel@tonic-gate  * connection skips the TIMEWAIT state and goes straight to a bound state
1410Sstevel@tonic-gate  * that kRPC can quickly switch to connected.
1420Sstevel@tonic-gate  *
1430Sstevel@tonic-gate  * Code that issues TPI request must use waitforack() to wait for the
1440Sstevel@tonic-gate  * corresponding ack (assuming there is one) in any future modifications.
1450Sstevel@tonic-gate  * This works around problems that may be introduced by breaking TPI rules
1460Sstevel@tonic-gate  * (by submitting new calls before earlier requests have been acked) in the
1470Sstevel@tonic-gate  * case of a signal or other early return.  waitforack() depends on
1480Sstevel@tonic-gate  * clnt_dispatch_notifyconn() to issue the wakeup when the ack
1490Sstevel@tonic-gate  * arrives, so adding new TPI calls may require corresponding changes
1500Sstevel@tonic-gate  * to clnt_dispatch_notifyconn(). Presently, the timeout period is based on
1510Sstevel@tonic-gate  * CLNT_MIN_TIMEOUT which is 10 seconds. If you modify this value, be sure
1520Sstevel@tonic-gate  * not to set it too low or TPI ACKS will be lost.
1530Sstevel@tonic-gate  */
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate #include <sys/param.h>
1560Sstevel@tonic-gate #include <sys/types.h>
1570Sstevel@tonic-gate #include <sys/user.h>
1580Sstevel@tonic-gate #include <sys/systm.h>
1590Sstevel@tonic-gate #include <sys/sysmacros.h>
1600Sstevel@tonic-gate #include <sys/proc.h>
1610Sstevel@tonic-gate #include <sys/socket.h>
1620Sstevel@tonic-gate #include <sys/file.h>
1630Sstevel@tonic-gate #include <sys/stream.h>
1640Sstevel@tonic-gate #include <sys/strsubr.h>
1650Sstevel@tonic-gate #include <sys/stropts.h>
1660Sstevel@tonic-gate #include <sys/strsun.h>
1670Sstevel@tonic-gate #include <sys/timod.h>
1680Sstevel@tonic-gate #include <sys/tiuser.h>
1690Sstevel@tonic-gate #include <sys/tihdr.h>
1700Sstevel@tonic-gate #include <sys/t_kuser.h>
1710Sstevel@tonic-gate #include <sys/fcntl.h>
1720Sstevel@tonic-gate #include <sys/errno.h>
1730Sstevel@tonic-gate #include <sys/kmem.h>
1740Sstevel@tonic-gate #include <sys/debug.h>
1750Sstevel@tonic-gate #include <sys/systm.h>
1760Sstevel@tonic-gate #include <sys/kstat.h>
1770Sstevel@tonic-gate #include <sys/t_lock.h>
1780Sstevel@tonic-gate #include <sys/ddi.h>
1790Sstevel@tonic-gate #include <sys/cmn_err.h>
1800Sstevel@tonic-gate #include <sys/time.h>
1810Sstevel@tonic-gate #include <sys/isa_defs.h>
1820Sstevel@tonic-gate #include <sys/callb.h>
1830Sstevel@tonic-gate #include <sys/sunddi.h>
1840Sstevel@tonic-gate #include <sys/atomic.h>
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate #include <netinet/in.h>
1870Sstevel@tonic-gate #include <netinet/tcp.h>
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate #include <rpc/types.h>
1900Sstevel@tonic-gate #include <rpc/xdr.h>
1910Sstevel@tonic-gate #include <rpc/auth.h>
1920Sstevel@tonic-gate #include <rpc/clnt.h>
1930Sstevel@tonic-gate #include <rpc/rpc_msg.h>
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate #define	COTS_DEFAULT_ALLOCSIZE	2048
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate #define	WIRE_HDR_SIZE	20	/* serialized call header, sans proc number */
1980Sstevel@tonic-gate #define	MSG_OFFSET	128	/* offset of call into the mblk */
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate const char *kinet_ntop6(uchar_t *, char *, size_t);
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate static int	clnt_cots_ksettimers(CLIENT *, struct rpc_timers *,
2030Sstevel@tonic-gate     struct rpc_timers *, int, void(*)(int, int, caddr_t), caddr_t, uint32_t);
2040Sstevel@tonic-gate static enum clnt_stat	clnt_cots_kcallit(CLIENT *, rpcproc_t, xdrproc_t,
2050Sstevel@tonic-gate     caddr_t, xdrproc_t, caddr_t, struct timeval);
2060Sstevel@tonic-gate static void	clnt_cots_kabort(CLIENT *);
2070Sstevel@tonic-gate static void	clnt_cots_kerror(CLIENT *, struct rpc_err *);
2080Sstevel@tonic-gate static bool_t	clnt_cots_kfreeres(CLIENT *, xdrproc_t, caddr_t);
2090Sstevel@tonic-gate static void	clnt_cots_kdestroy(CLIENT *);
2100Sstevel@tonic-gate static bool_t	clnt_cots_kcontrol(CLIENT *, int, char *);
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate /* List of transports managed by the connection manager. */
2140Sstevel@tonic-gate struct cm_xprt {
2150Sstevel@tonic-gate 	TIUSER		*x_tiptr;	/* transport handle */
2160Sstevel@tonic-gate 	queue_t		*x_wq;		/* send queue */
2170Sstevel@tonic-gate 	clock_t		x_time;		/* last time we handed this xprt out */
2180Sstevel@tonic-gate 	clock_t		x_ctime;	/* time we went to CONNECTED */
2190Sstevel@tonic-gate 	int		x_tidu_size;    /* TIDU size of this transport */
2200Sstevel@tonic-gate 	union {
2210Sstevel@tonic-gate 	    struct {
2220Sstevel@tonic-gate 		unsigned int
2230Sstevel@tonic-gate #ifdef	_BIT_FIELDS_HTOL
2240Sstevel@tonic-gate 		b_closing:	1,	/* we've sent a ord rel on this conn */
2250Sstevel@tonic-gate 		b_dead:		1,	/* transport is closed or disconn */
2260Sstevel@tonic-gate 		b_doomed:	1,	/* too many conns, let this go idle */
2270Sstevel@tonic-gate 		b_connected:	1,	/* this connection is connected */
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 		b_ordrel:	1,	/* do an orderly release? */
2300Sstevel@tonic-gate 		b_thread:	1,	/* thread doing connect */
2310Sstevel@tonic-gate 		b_waitdis:	1,	/* waiting for disconnect ACK */
2320Sstevel@tonic-gate 		b_needdis:	1,	/* need T_DISCON_REQ */
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 		b_needrel:	1,	/* need T_ORDREL_REQ */
2350Sstevel@tonic-gate 		b_early_disc:	1,	/* got a T_ORDREL_IND or T_DISCON_IND */
2360Sstevel@tonic-gate 					/* disconnect during connect */
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 		b_pad:		22;
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate #endif
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate #ifdef	_BIT_FIELDS_LTOH
2430Sstevel@tonic-gate 		b_pad:		22,
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 		b_early_disc:	1,	/* got a T_ORDREL_IND or T_DISCON_IND */
2460Sstevel@tonic-gate 					/* disconnect during connect */
2470Sstevel@tonic-gate 		b_needrel:	1,	/* need T_ORDREL_REQ */
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 		b_needdis:	1,	/* need T_DISCON_REQ */
2500Sstevel@tonic-gate 		b_waitdis:	1,	/* waiting for disconnect ACK */
2510Sstevel@tonic-gate 		b_thread:	1,	/* thread doing connect */
2520Sstevel@tonic-gate 		b_ordrel:	1,	/* do an orderly release? */
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 		b_connected:	1,	/* this connection is connected */
2550Sstevel@tonic-gate 		b_doomed:	1,	/* too many conns, let this go idle */
2560Sstevel@tonic-gate 		b_dead:		1,	/* transport is closed or disconn */
2570Sstevel@tonic-gate 		b_closing:	1;	/* we've sent a ord rel on this conn */
2580Sstevel@tonic-gate #endif
2590Sstevel@tonic-gate 	    } bit;	    unsigned int word;
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate #define	x_closing	x_state.bit.b_closing
2620Sstevel@tonic-gate #define	x_dead		x_state.bit.b_dead
2630Sstevel@tonic-gate #define	x_doomed	x_state.bit.b_doomed
2640Sstevel@tonic-gate #define	x_connected	x_state.bit.b_connected
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate #define	x_ordrel	x_state.bit.b_ordrel
2670Sstevel@tonic-gate #define	x_thread	x_state.bit.b_thread
2680Sstevel@tonic-gate #define	x_waitdis	x_state.bit.b_waitdis
2690Sstevel@tonic-gate #define	x_needdis	x_state.bit.b_needdis
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate #define	x_needrel	x_state.bit.b_needrel
2720Sstevel@tonic-gate #define	x_early_disc    x_state.bit.b_early_disc
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate #define	x_state_flags	x_state.word
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate #define	X_CLOSING	0x80000000
2770Sstevel@tonic-gate #define	X_DEAD		0x40000000
2780Sstevel@tonic-gate #define	X_DOOMED	0x20000000
2790Sstevel@tonic-gate #define	X_CONNECTED	0x10000000
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate #define	X_ORDREL	0x08000000
2820Sstevel@tonic-gate #define	X_THREAD	0x04000000
2830Sstevel@tonic-gate #define	X_WAITDIS	0x02000000
2840Sstevel@tonic-gate #define	X_NEEDDIS	0x01000000
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate #define	X_NEEDREL	0x00800000
2870Sstevel@tonic-gate #define	X_EARLYDISC	0x00400000
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate #define	X_BADSTATES	(X_CLOSING | X_DEAD | X_DOOMED)
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 	}		x_state;
2920Sstevel@tonic-gate 	int		x_ref;		/* number of users of this xprt */
2930Sstevel@tonic-gate 	int		x_family;	/* address family of transport */
2940Sstevel@tonic-gate 	dev_t		x_rdev;		/* device number of transport */
2950Sstevel@tonic-gate 	struct cm_xprt	*x_next;
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 	struct netbuf	x_server;	/* destination address */
2980Sstevel@tonic-gate 	struct netbuf	x_src;		/* src address (for retries) */
2990Sstevel@tonic-gate 	kmutex_t	x_lock;		/* lock on this entry */
3000Sstevel@tonic-gate 	kcondvar_t	x_cv;		/* to signal when can be closed */
3010Sstevel@tonic-gate 	kcondvar_t	x_conn_cv;	/* to signal when connection attempt */
3020Sstevel@tonic-gate 					/* is complete */
3030Sstevel@tonic-gate 	kstat_t		*x_ksp;
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate 	kcondvar_t	x_dis_cv;	/* to signal when disconnect attempt */
3060Sstevel@tonic-gate 					/* is complete */
3070Sstevel@tonic-gate 	zoneid_t	x_zoneid;	/* zone this xprt belongs to */
3080Sstevel@tonic-gate };
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate typedef struct cm_kstat_xprt {
3110Sstevel@tonic-gate 	kstat_named_t	x_wq;
3120Sstevel@tonic-gate 	kstat_named_t	x_server;
3130Sstevel@tonic-gate 	kstat_named_t	x_family;
3140Sstevel@tonic-gate 	kstat_named_t	x_rdev;
3150Sstevel@tonic-gate 	kstat_named_t	x_time;
3160Sstevel@tonic-gate 	kstat_named_t	x_state;
3170Sstevel@tonic-gate 	kstat_named_t	x_ref;
3180Sstevel@tonic-gate 	kstat_named_t	x_port;
3190Sstevel@tonic-gate } cm_kstat_xprt_t;
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate static cm_kstat_xprt_t cm_kstat_template = {
3220Sstevel@tonic-gate 	{ "write_queue", KSTAT_DATA_UINT32 },
3230Sstevel@tonic-gate 	{ "server",	KSTAT_DATA_STRING },
3240Sstevel@tonic-gate 	{ "addr_family", KSTAT_DATA_UINT32 },
3250Sstevel@tonic-gate 	{ "device",	KSTAT_DATA_UINT32 },
3260Sstevel@tonic-gate 	{ "time_stamp",	KSTAT_DATA_UINT32 },
3270Sstevel@tonic-gate 	{ "status",	KSTAT_DATA_UINT32 },
3280Sstevel@tonic-gate 	{ "ref_count",	KSTAT_DATA_INT32 },
3290Sstevel@tonic-gate 	{ "port",	KSTAT_DATA_UINT32 },
3300Sstevel@tonic-gate };
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate /*
3330Sstevel@tonic-gate  * The inverse of this is connmgr_release().
3340Sstevel@tonic-gate  */
3350Sstevel@tonic-gate #define	CONN_HOLD(Cm_entry)	{\
3360Sstevel@tonic-gate 	mutex_enter(&(Cm_entry)->x_lock);	\
3370Sstevel@tonic-gate 	(Cm_entry)->x_ref++;	\
3380Sstevel@tonic-gate 	mutex_exit(&(Cm_entry)->x_lock);	\
3390Sstevel@tonic-gate }
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate /*
3430Sstevel@tonic-gate  * Private data per rpc handle.  This structure is allocated by
3440Sstevel@tonic-gate  * clnt_cots_kcreate, and freed by clnt_cots_kdestroy.
3450Sstevel@tonic-gate  */
3460Sstevel@tonic-gate typedef struct cku_private_s {
3470Sstevel@tonic-gate 	CLIENT			cku_client;	/* client handle */
3480Sstevel@tonic-gate 	calllist_t		cku_call;	/* for dispatching calls */
3490Sstevel@tonic-gate 	struct rpc_err		cku_err;	/* error status */
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 	struct netbuf		cku_srcaddr;	/* source address for retries */
3520Sstevel@tonic-gate 	int			cku_addrfmly;  /* for binding port */
3530Sstevel@tonic-gate 	struct netbuf		cku_addr;	/* remote address */
3540Sstevel@tonic-gate 	dev_t			cku_device;	/* device to use */
3550Sstevel@tonic-gate 	uint_t			cku_flags;
3560Sstevel@tonic-gate #define	CKU_ONQUEUE		0x1
3570Sstevel@tonic-gate #define	CKU_SENT		0x2
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 	bool_t			cku_progress;	/* for CLSET_PROGRESS */
3600Sstevel@tonic-gate 	uint32_t		cku_xid;	/* current XID */
3610Sstevel@tonic-gate 	clock_t			cku_ctime;	/* time stamp of when */
3620Sstevel@tonic-gate 						/* connection was created */
3630Sstevel@tonic-gate 	uint_t			cku_recv_attempts;
3640Sstevel@tonic-gate 	XDR			cku_outxdr;	/* xdr routine for output */
3650Sstevel@tonic-gate 	XDR			cku_inxdr;	/* xdr routine for input */
3660Sstevel@tonic-gate 	char			cku_rpchdr[WIRE_HDR_SIZE + 4];
3670Sstevel@tonic-gate 						/* pre-serialized rpc header */
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate 	uint_t			cku_outbuflen;	/* default output mblk length */
3700Sstevel@tonic-gate 	struct cred		*cku_cred;	/* credentials */
3710Sstevel@tonic-gate 	bool_t			cku_nodelayonerr;
3720Sstevel@tonic-gate 						/* for CLSET_NODELAYONERR */
3730Sstevel@tonic-gate 	int			cku_useresvport; /* Use reserved port */
3740Sstevel@tonic-gate 	struct rpc_cots_client	*cku_stats;	/* stats for zone */
3750Sstevel@tonic-gate } cku_private_t;
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate static struct cm_xprt *connmgr_wrapconnect(struct cm_xprt *,
3780Sstevel@tonic-gate 	const struct timeval *, struct netbuf *, int, struct netbuf *,
3790Sstevel@tonic-gate 	struct rpc_err *, bool_t, bool_t);
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate static bool_t	connmgr_connect(struct cm_xprt *, queue_t *, struct netbuf *,
3820Sstevel@tonic-gate 				int, calllist_t *, int *, bool_t reconnect,
3830Sstevel@tonic-gate 				const struct timeval *, bool_t);
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate static bool_t	connmgr_setopt(queue_t *, int, int, calllist_t *);
3860Sstevel@tonic-gate static void	connmgr_sndrel(struct cm_xprt *);
3870Sstevel@tonic-gate static void	connmgr_snddis(struct cm_xprt *);
3880Sstevel@tonic-gate static void	connmgr_close(struct cm_xprt *);
3890Sstevel@tonic-gate static void	connmgr_release(struct cm_xprt *);
3900Sstevel@tonic-gate static struct cm_xprt *connmgr_wrapget(struct netbuf *, const struct timeval *,
3910Sstevel@tonic-gate 	cku_private_t *);
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate static struct cm_xprt *connmgr_get(struct netbuf *, const struct timeval *,
3940Sstevel@tonic-gate 	struct netbuf *, int, struct netbuf *, struct rpc_err *, dev_t,
3950Sstevel@tonic-gate 	bool_t, int);
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate static void connmgr_cancelconn(struct cm_xprt *);
3980Sstevel@tonic-gate static enum clnt_stat connmgr_cwait(struct cm_xprt *, const struct timeval *,
3990Sstevel@tonic-gate 	bool_t);
4000Sstevel@tonic-gate static void connmgr_dis_and_wait(struct cm_xprt *);
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate static void	clnt_dispatch_send(queue_t *, mblk_t *, calllist_t *, uint_t,
4030Sstevel@tonic-gate 					uint_t);
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate static int clnt_delay(clock_t, bool_t);
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate static int waitforack(calllist_t *, t_scalar_t, const struct timeval *, bool_t);
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate /*
4100Sstevel@tonic-gate  * Operations vector for TCP/IP based RPC
4110Sstevel@tonic-gate  */
4120Sstevel@tonic-gate static struct clnt_ops tcp_ops = {
4130Sstevel@tonic-gate 	clnt_cots_kcallit,	/* do rpc call */
4140Sstevel@tonic-gate 	clnt_cots_kabort,	/* abort call */
4150Sstevel@tonic-gate 	clnt_cots_kerror,	/* return error status */
4160Sstevel@tonic-gate 	clnt_cots_kfreeres,	/* free results */
4170Sstevel@tonic-gate 	clnt_cots_kdestroy,	/* destroy rpc handle */
4180Sstevel@tonic-gate 	clnt_cots_kcontrol,	/* the ioctl() of rpc */
4190Sstevel@tonic-gate 	clnt_cots_ksettimers,	/* set retry timers */
4200Sstevel@tonic-gate };
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate static int rpc_kstat_instance = 0;  /* keeps the current instance */
4230Sstevel@tonic-gate 				/* number for the next kstat_create */
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate static struct cm_xprt *cm_hd = NULL;
4260Sstevel@tonic-gate static kmutex_t connmgr_lock;	/* for connection mngr's list of transports */
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate extern kmutex_t clnt_max_msg_lock;
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate static calllist_t *clnt_pending = NULL;
4310Sstevel@tonic-gate extern kmutex_t clnt_pending_lock;
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate static int clnt_cots_hash_size = DEFAULT_HASH_SIZE;
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate static call_table_t *cots_call_ht;
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate static const struct rpc_cots_client {
4380Sstevel@tonic-gate 	kstat_named_t	rccalls;
4390Sstevel@tonic-gate 	kstat_named_t	rcbadcalls;
4400Sstevel@tonic-gate 	kstat_named_t	rcbadxids;
4410Sstevel@tonic-gate 	kstat_named_t	rctimeouts;
4420Sstevel@tonic-gate 	kstat_named_t	rcnewcreds;
4430Sstevel@tonic-gate 	kstat_named_t	rcbadverfs;
4440Sstevel@tonic-gate 	kstat_named_t	rctimers;
4450Sstevel@tonic-gate 	kstat_named_t	rccantconn;
4460Sstevel@tonic-gate 	kstat_named_t	rcnomem;
4470Sstevel@tonic-gate 	kstat_named_t	rcintrs;
4480Sstevel@tonic-gate } cots_rcstat_tmpl = {
4490Sstevel@tonic-gate 	{ "calls",	KSTAT_DATA_UINT64 },
4500Sstevel@tonic-gate 	{ "badcalls",	KSTAT_DATA_UINT64 },
4510Sstevel@tonic-gate 	{ "badxids",	KSTAT_DATA_UINT64 },
4520Sstevel@tonic-gate 	{ "timeouts",	KSTAT_DATA_UINT64 },
4530Sstevel@tonic-gate 	{ "newcreds",	KSTAT_DATA_UINT64 },
4540Sstevel@tonic-gate 	{ "badverfs",	KSTAT_DATA_UINT64 },
4550Sstevel@tonic-gate 	{ "timers",	KSTAT_DATA_UINT64 },
4560Sstevel@tonic-gate 	{ "cantconn",	KSTAT_DATA_UINT64 },
4570Sstevel@tonic-gate 	{ "nomem",	KSTAT_DATA_UINT64 },
4580Sstevel@tonic-gate 	{ "interrupts", KSTAT_DATA_UINT64 }
4590Sstevel@tonic-gate };
4600Sstevel@tonic-gate 
4610Sstevel@tonic-gate #define	COTSRCSTAT_INCR(p, x)	\
4620Sstevel@tonic-gate 	atomic_add_64(&(p)->x.value.ui64, 1)
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate #define	CLNT_MAX_CONNS	1	/* concurrent connections between clnt/srvr */
4650Sstevel@tonic-gate static int clnt_max_conns = CLNT_MAX_CONNS;
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate #define	CLNT_MIN_TIMEOUT	10	/* seconds to wait after we get a */
4680Sstevel@tonic-gate 					/* connection reset */
4690Sstevel@tonic-gate #define	CLNT_MIN_CONNTIMEOUT	5	/* seconds to wait for a connection */
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate static int clnt_cots_min_tout = CLNT_MIN_TIMEOUT;
4730Sstevel@tonic-gate static int clnt_cots_min_conntout = CLNT_MIN_CONNTIMEOUT;
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate /*
4760Sstevel@tonic-gate  * Limit the number of times we will attempt to receive a reply without
4770Sstevel@tonic-gate  * re-sending a response.
4780Sstevel@tonic-gate  */
4790Sstevel@tonic-gate #define	CLNT_MAXRECV_WITHOUT_RETRY	3
4800Sstevel@tonic-gate static uint_t clnt_cots_maxrecv	= CLNT_MAXRECV_WITHOUT_RETRY;
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate uint_t *clnt_max_msg_sizep;
4830Sstevel@tonic-gate void (*clnt_stop_idle)(queue_t *wq);
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate #define	ptoh(p)		(&((p)->cku_client))
4860Sstevel@tonic-gate #define	htop(h)		((cku_private_t *)((h)->cl_private))
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate /*
4890Sstevel@tonic-gate  * Times to retry
4900Sstevel@tonic-gate  */
4910Sstevel@tonic-gate #define	REFRESHES	2	/* authentication refreshes */
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate static int clnt_cots_do_bindresvport = 1; /* bind to reserved port */
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate static zone_key_t zone_cots_key;
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate /*
4980Sstevel@tonic-gate  * We need to do this after all kernel threads in the zone have exited.
4990Sstevel@tonic-gate  */
5000Sstevel@tonic-gate /* ARGSUSED */
5010Sstevel@tonic-gate static void
5020Sstevel@tonic-gate clnt_zone_destroy(zoneid_t zoneid, void *unused)
5030Sstevel@tonic-gate {
5040Sstevel@tonic-gate 	struct cm_xprt **cmp;
5050Sstevel@tonic-gate 	struct cm_xprt *cm_entry;
5060Sstevel@tonic-gate 	struct cm_xprt *freelist = NULL;
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate 	mutex_enter(&connmgr_lock);
5090Sstevel@tonic-gate 	cmp = &cm_hd;
5100Sstevel@tonic-gate 	while ((cm_entry = *cmp) != NULL) {
5110Sstevel@tonic-gate 		if (cm_entry->x_zoneid == zoneid) {
5120Sstevel@tonic-gate 			*cmp = cm_entry->x_next;
5130Sstevel@tonic-gate 			cm_entry->x_next = freelist;
5140Sstevel@tonic-gate 			freelist = cm_entry;
5150Sstevel@tonic-gate 		} else {
5160Sstevel@tonic-gate 			cmp = &cm_entry->x_next;
5170Sstevel@tonic-gate 		}
5180Sstevel@tonic-gate 	}
5190Sstevel@tonic-gate 	mutex_exit(&connmgr_lock);
5200Sstevel@tonic-gate 	while ((cm_entry = freelist) != NULL) {
5210Sstevel@tonic-gate 		freelist = cm_entry->x_next;
5220Sstevel@tonic-gate 		connmgr_close(cm_entry);
5230Sstevel@tonic-gate 	}
5240Sstevel@tonic-gate }
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate int
5270Sstevel@tonic-gate clnt_cots_kcreate(dev_t dev, struct netbuf *addr, int family, rpcprog_t prog,
5280Sstevel@tonic-gate 	rpcvers_t vers, uint_t max_msgsize, cred_t *cred, CLIENT **ncl)
5290Sstevel@tonic-gate {
5300Sstevel@tonic-gate 	CLIENT *h;
5310Sstevel@tonic-gate 	cku_private_t *p;
5320Sstevel@tonic-gate 	struct rpc_msg call_msg;
5330Sstevel@tonic-gate 	struct rpcstat *rpcstat;
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate 	RPCLOG(8, "clnt_cots_kcreate: prog %u\n", prog);
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate 	rpcstat = zone_getspecific(rpcstat_zone_key, curproc->p_zone);
5380Sstevel@tonic-gate 	ASSERT(rpcstat != NULL);
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate 	/* Allocate and intialize the client handle. */
5410Sstevel@tonic-gate 	p = kmem_zalloc(sizeof (*p), KM_SLEEP);
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate 	h = ptoh(p);
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate 	h->cl_private = (caddr_t)p;
5460Sstevel@tonic-gate 	h->cl_auth = authkern_create();
5470Sstevel@tonic-gate 	h->cl_ops = &tcp_ops;
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate 	cv_init(&p->cku_call.call_cv, NULL, CV_DEFAULT, NULL);
5500Sstevel@tonic-gate 	mutex_init(&p->cku_call.call_lock, NULL, MUTEX_DEFAULT, NULL);
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate 	/*
5530Sstevel@tonic-gate 	 * If the current sanity check size in rpcmod is smaller
5540Sstevel@tonic-gate 	 * than the size needed, then increase the sanity check.
5550Sstevel@tonic-gate 	 */
5560Sstevel@tonic-gate 	if (max_msgsize != 0 && clnt_max_msg_sizep != NULL &&
5570Sstevel@tonic-gate 	    max_msgsize > *clnt_max_msg_sizep) {
5580Sstevel@tonic-gate 		mutex_enter(&clnt_max_msg_lock);
5590Sstevel@tonic-gate 		if (max_msgsize > *clnt_max_msg_sizep)
5600Sstevel@tonic-gate 			*clnt_max_msg_sizep = max_msgsize;
5610Sstevel@tonic-gate 		mutex_exit(&clnt_max_msg_lock);
5620Sstevel@tonic-gate 	}
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate 	p->cku_outbuflen = COTS_DEFAULT_ALLOCSIZE;
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate 	/* Preserialize the call message header */
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate 	call_msg.rm_xid = 0;
5690Sstevel@tonic-gate 	call_msg.rm_direction = CALL;
5700Sstevel@tonic-gate 	call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
5710Sstevel@tonic-gate 	call_msg.rm_call.cb_prog = prog;
5720Sstevel@tonic-gate 	call_msg.rm_call.cb_vers = vers;
5730Sstevel@tonic-gate 
5740Sstevel@tonic-gate 	xdrmem_create(&p->cku_outxdr, p->cku_rpchdr, WIRE_HDR_SIZE, XDR_ENCODE);
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 	if (!xdr_callhdr(&p->cku_outxdr, &call_msg)) {
5770Sstevel@tonic-gate 		RPCLOG0(1, "clnt_cots_kcreate - Fatal header serialization "
5780Sstevel@tonic-gate 		    "error\n");
5790Sstevel@tonic-gate 		auth_destroy(h->cl_auth);
5800Sstevel@tonic-gate 		kmem_free(p, sizeof (cku_private_t));
5810Sstevel@tonic-gate 		RPCLOG0(1, "clnt_cots_kcreate: create failed error EINVAL\n");
5820Sstevel@tonic-gate 		return (EINVAL);		/* XXX */
5830Sstevel@tonic-gate 	}
5840Sstevel@tonic-gate 
5850Sstevel@tonic-gate 	/*
5860Sstevel@tonic-gate 	 * The zalloc initialized the fields below.
5870Sstevel@tonic-gate 	 * p->cku_xid = 0;
5880Sstevel@tonic-gate 	 * p->cku_flags = 0;
5890Sstevel@tonic-gate 	 * p->cku_srcaddr.len = 0;
5900Sstevel@tonic-gate 	 * p->cku_srcaddr.maxlen = 0;
5910Sstevel@tonic-gate 	 */
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate 	p->cku_cred = cred;
5940Sstevel@tonic-gate 	p->cku_device = dev;
5950Sstevel@tonic-gate 	p->cku_addrfmly = family;
5960Sstevel@tonic-gate 	p->cku_addr.buf = kmem_zalloc(addr->maxlen, KM_SLEEP);
5970Sstevel@tonic-gate 	p->cku_addr.maxlen = addr->maxlen;
5980Sstevel@tonic-gate 	p->cku_addr.len = addr->len;
5990Sstevel@tonic-gate 	bcopy(addr->buf, p->cku_addr.buf, addr->len);
6000Sstevel@tonic-gate 	p->cku_stats = rpcstat->rpc_cots_client;
6010Sstevel@tonic-gate 	p->cku_useresvport = -1; /* value is has not been set */
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 	*ncl = h;
6040Sstevel@tonic-gate 	return (0);
6050Sstevel@tonic-gate }
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate /*ARGSUSED*/
6080Sstevel@tonic-gate static void
6090Sstevel@tonic-gate clnt_cots_kabort(CLIENT *h)
6100Sstevel@tonic-gate {
6110Sstevel@tonic-gate }
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate /*
6140Sstevel@tonic-gate  * Return error info on this handle.
6150Sstevel@tonic-gate  */
6160Sstevel@tonic-gate static void
6170Sstevel@tonic-gate clnt_cots_kerror(CLIENT *h, struct rpc_err *err)
6180Sstevel@tonic-gate {
6190Sstevel@tonic-gate 	/* LINTED pointer alignment */
6200Sstevel@tonic-gate 	cku_private_t *p = htop(h);
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate 	*err = p->cku_err;
6230Sstevel@tonic-gate }
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate static bool_t
6260Sstevel@tonic-gate clnt_cots_kfreeres(CLIENT *h, xdrproc_t xdr_res, caddr_t res_ptr)
6270Sstevel@tonic-gate {
6280Sstevel@tonic-gate 	/* LINTED pointer alignment */
6290Sstevel@tonic-gate 	cku_private_t *p = htop(h);
6300Sstevel@tonic-gate 	XDR *xdrs;
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate 	xdrs = &(p->cku_outxdr);
6330Sstevel@tonic-gate 	xdrs->x_op = XDR_FREE;
6340Sstevel@tonic-gate 	return ((*xdr_res)(xdrs, res_ptr));
6350Sstevel@tonic-gate }
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate static bool_t
6380Sstevel@tonic-gate clnt_cots_kcontrol(CLIENT *h, int cmd, char *arg)
6390Sstevel@tonic-gate {
6400Sstevel@tonic-gate 	cku_private_t *p = htop(h);
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 	switch (cmd) {
6430Sstevel@tonic-gate 	case CLSET_PROGRESS:
6440Sstevel@tonic-gate 		p->cku_progress = TRUE;
6450Sstevel@tonic-gate 		return (TRUE);
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate 	case CLSET_XID:
6480Sstevel@tonic-gate 		if (arg == NULL)
6490Sstevel@tonic-gate 			return (FALSE);
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate 		p->cku_xid = *((uint32_t *)arg);
6520Sstevel@tonic-gate 		return (TRUE);
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate 	case CLGET_XID:
6550Sstevel@tonic-gate 		if (arg == NULL)
6560Sstevel@tonic-gate 			return (FALSE);
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate 		*((uint32_t *)arg) = p->cku_xid;
6590Sstevel@tonic-gate 		return (TRUE);
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 	case CLSET_NODELAYONERR:
6620Sstevel@tonic-gate 		if (arg == NULL)
6630Sstevel@tonic-gate 			return (FALSE);
6640Sstevel@tonic-gate 
6650Sstevel@tonic-gate 		if (*((bool_t *)arg) == TRUE) {
6660Sstevel@tonic-gate 			p->cku_nodelayonerr = TRUE;
6670Sstevel@tonic-gate 			return (TRUE);
6680Sstevel@tonic-gate 		}
6690Sstevel@tonic-gate 		if (*((bool_t *)arg) == FALSE) {
6700Sstevel@tonic-gate 			p->cku_nodelayonerr = FALSE;
6710Sstevel@tonic-gate 			return (TRUE);
6720Sstevel@tonic-gate 		}
6730Sstevel@tonic-gate 		return (FALSE);
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate 	case CLGET_NODELAYONERR:
6760Sstevel@tonic-gate 		if (arg == NULL)
6770Sstevel@tonic-gate 			return (FALSE);
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 		*((bool_t *)arg) = p->cku_nodelayonerr;
6800Sstevel@tonic-gate 		return (TRUE);
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate 	case CLSET_BINDRESVPORT:
6830Sstevel@tonic-gate 		if (arg == NULL)
6840Sstevel@tonic-gate 			return (FALSE);
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate 		if (*(int *)arg != 1 && *(int *)arg != 0)
6870Sstevel@tonic-gate 			return (FALSE);
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate 		p->cku_useresvport = *(int *)arg;
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate 		return (TRUE);
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate 	case CLGET_BINDRESVPORT:
6940Sstevel@tonic-gate 		if (arg == NULL)
6950Sstevel@tonic-gate 			return (FALSE);
6960Sstevel@tonic-gate 
6970Sstevel@tonic-gate 		*(int *)arg = p->cku_useresvport;
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 		return (TRUE);
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate 	default:
7020Sstevel@tonic-gate 		return (FALSE);
7030Sstevel@tonic-gate 	}
7040Sstevel@tonic-gate }
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate /*
7070Sstevel@tonic-gate  * Destroy rpc handle.  Frees the space used for output buffer,
7080Sstevel@tonic-gate  * private data, and handle structure.
7090Sstevel@tonic-gate  */
7100Sstevel@tonic-gate static void
7110Sstevel@tonic-gate clnt_cots_kdestroy(CLIENT *h)
7120Sstevel@tonic-gate {
7130Sstevel@tonic-gate 	/* LINTED pointer alignment */
7140Sstevel@tonic-gate 	cku_private_t *p = htop(h);
7150Sstevel@tonic-gate 	calllist_t *call = &p->cku_call;
7160Sstevel@tonic-gate 
7170Sstevel@tonic-gate 	RPCLOG(8, "clnt_cots_kdestroy h: %p\n", (void *)h);
7180Sstevel@tonic-gate 	RPCLOG(8, "clnt_cots_kdestroy h: xid=0x%x\n", p->cku_xid);
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate 	if (p->cku_flags & CKU_ONQUEUE) {
7210Sstevel@tonic-gate 		RPCLOG(64, "clnt_cots_kdestroy h: removing call for xid 0x%x "
7220Sstevel@tonic-gate 		    "from dispatch list\n", p->cku_xid);
7230Sstevel@tonic-gate 		call_table_remove(call);
7240Sstevel@tonic-gate 	}
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate 	if (call->call_reply)
7270Sstevel@tonic-gate 		freemsg(call->call_reply);
7280Sstevel@tonic-gate 	cv_destroy(&call->call_cv);
7290Sstevel@tonic-gate 	mutex_destroy(&call->call_lock);
7300Sstevel@tonic-gate 
7310Sstevel@tonic-gate 	kmem_free(p->cku_srcaddr.buf, p->cku_srcaddr.maxlen);
7320Sstevel@tonic-gate 	kmem_free(p->cku_addr.buf, p->cku_addr.maxlen);
7330Sstevel@tonic-gate 	kmem_free(p, sizeof (*p));
7340Sstevel@tonic-gate }
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate static int clnt_cots_pulls;
7370Sstevel@tonic-gate #define	RM_HDR_SIZE	4	/* record mark header size */
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate /*
7400Sstevel@tonic-gate  * Call remote procedure.
7410Sstevel@tonic-gate  */
7420Sstevel@tonic-gate static enum clnt_stat
7430Sstevel@tonic-gate clnt_cots_kcallit(CLIENT *h, rpcproc_t procnum, xdrproc_t xdr_args,
7440Sstevel@tonic-gate     caddr_t argsp, xdrproc_t xdr_results, caddr_t resultsp, struct timeval wait)
7450Sstevel@tonic-gate {
7460Sstevel@tonic-gate 	/* LINTED pointer alignment */
7470Sstevel@tonic-gate 	cku_private_t *p = htop(h);
7480Sstevel@tonic-gate 	calllist_t *call = &p->cku_call;
7490Sstevel@tonic-gate 	XDR *xdrs;
7500Sstevel@tonic-gate 	struct rpc_msg reply_msg;
7510Sstevel@tonic-gate 	mblk_t *mp;
7520Sstevel@tonic-gate #ifdef	RPCDEBUG
7530Sstevel@tonic-gate 	clock_t time_sent;
7540Sstevel@tonic-gate #endif
7550Sstevel@tonic-gate 	struct netbuf *retryaddr;
7560Sstevel@tonic-gate 	struct cm_xprt *cm_entry = NULL;
7570Sstevel@tonic-gate 	queue_t *wq;
7580Sstevel@tonic-gate 	int len;
7590Sstevel@tonic-gate 	int mpsize;
7600Sstevel@tonic-gate 	int refreshes = REFRESHES;
7610Sstevel@tonic-gate 	int interrupted;
7620Sstevel@tonic-gate 	int tidu_size;
7630Sstevel@tonic-gate 	enum clnt_stat status;
7640Sstevel@tonic-gate 	struct timeval cwait;
7650Sstevel@tonic-gate 	bool_t delay_first = FALSE;
7660Sstevel@tonic-gate 	clock_t ticks;
7670Sstevel@tonic-gate 
7680Sstevel@tonic-gate 	RPCLOG(2, "clnt_cots_kcallit, procnum %u\n", procnum);
7690Sstevel@tonic-gate 	COTSRCSTAT_INCR(p->cku_stats, rccalls);
7700Sstevel@tonic-gate 
7710Sstevel@tonic-gate 	RPCLOG(2, "clnt_cots_kcallit: wait.tv_sec: %ld\n", wait.tv_sec);
7720Sstevel@tonic-gate 	RPCLOG(2, "clnt_cots_kcallit: wait.tv_usec: %ld\n", wait.tv_usec);
7730Sstevel@tonic-gate 
7740Sstevel@tonic-gate 	/*
7750Sstevel@tonic-gate 	 * Bug ID 1240234:
7760Sstevel@tonic-gate 	 * Look out for zero length timeouts. We don't want to
7770Sstevel@tonic-gate 	 * wait zero seconds for a connection to be established.
7780Sstevel@tonic-gate 	 */
7790Sstevel@tonic-gate 	if (wait.tv_sec < clnt_cots_min_conntout) {
7800Sstevel@tonic-gate 		cwait.tv_sec = clnt_cots_min_conntout;
7810Sstevel@tonic-gate 		cwait.tv_usec = 0;
7820Sstevel@tonic-gate 		RPCLOG(8, "clnt_cots_kcallit: wait.tv_sec (%ld) too low,",
7830Sstevel@tonic-gate 		    wait.tv_sec);
7840Sstevel@tonic-gate 		RPCLOG(8, " setting to: %d\n", clnt_cots_min_conntout);
7850Sstevel@tonic-gate 	} else {
7860Sstevel@tonic-gate 		cwait = wait;
7870Sstevel@tonic-gate 	}
7880Sstevel@tonic-gate 
7890Sstevel@tonic-gate call_again:
7900Sstevel@tonic-gate 	if (cm_entry) {
7910Sstevel@tonic-gate 		connmgr_release(cm_entry);
7920Sstevel@tonic-gate 		cm_entry = NULL;
7930Sstevel@tonic-gate 	}
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate 	mp = NULL;
7960Sstevel@tonic-gate 
7970Sstevel@tonic-gate 	/*
7980Sstevel@tonic-gate 	 * If the call is not a retry, allocate a new xid and cache it
7990Sstevel@tonic-gate 	 * for future retries.
8000Sstevel@tonic-gate 	 * Bug ID 1246045:
8010Sstevel@tonic-gate 	 * Treat call as a retry for purposes of binding the source
8020Sstevel@tonic-gate 	 * port only if we actually attempted to send anything on
8030Sstevel@tonic-gate 	 * the previous call.
8040Sstevel@tonic-gate 	 */
8050Sstevel@tonic-gate 	if (p->cku_xid == 0) {
8060Sstevel@tonic-gate 		p->cku_xid = alloc_xid();
8070Sstevel@tonic-gate 		/*
8080Sstevel@tonic-gate 		 * We need to ASSERT here that our xid != 0 because this
8090Sstevel@tonic-gate 		 * determines whether or not our call record gets placed on
8100Sstevel@tonic-gate 		 * the hash table or the linked list.  By design, we mandate
8110Sstevel@tonic-gate 		 * that RPC calls over cots must have xid's != 0, so we can
8120Sstevel@tonic-gate 		 * ensure proper management of the hash table.
8130Sstevel@tonic-gate 		 */
8140Sstevel@tonic-gate 		ASSERT(p->cku_xid != 0);
8150Sstevel@tonic-gate 
8160Sstevel@tonic-gate 		retryaddr = NULL;
8170Sstevel@tonic-gate 		p->cku_flags &= ~CKU_SENT;
8180Sstevel@tonic-gate 
8190Sstevel@tonic-gate 		if (p->cku_flags & CKU_ONQUEUE) {
8200Sstevel@tonic-gate 			RPCLOG(8, "clnt_cots_kcallit: new call, dequeuing old"
8210Sstevel@tonic-gate 			    " one (%p)\n", (void *)call);
8220Sstevel@tonic-gate 			call_table_remove(call);
8230Sstevel@tonic-gate 			p->cku_flags &= ~CKU_ONQUEUE;
8240Sstevel@tonic-gate 			RPCLOG(64, "clnt_cots_kcallit: removing call from "
8250Sstevel@tonic-gate 			    "dispatch list because xid was zero (now 0x%x)\n",
8260Sstevel@tonic-gate 			    p->cku_xid);
8270Sstevel@tonic-gate 		}
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate 		if (call->call_reply != NULL) {
8300Sstevel@tonic-gate 			freemsg(call->call_reply);
8310Sstevel@tonic-gate 			call->call_reply = NULL;
8320Sstevel@tonic-gate 		}
8330Sstevel@tonic-gate 	} else if (p->cku_srcaddr.buf == NULL || p->cku_srcaddr.len == 0) {
8340Sstevel@tonic-gate 		retryaddr = NULL;
8350Sstevel@tonic-gate 
8360Sstevel@tonic-gate 	} else if (p->cku_flags & CKU_SENT) {
8370Sstevel@tonic-gate 		retryaddr = &p->cku_srcaddr;
8380Sstevel@tonic-gate 
8390Sstevel@tonic-gate 	} else {
8400Sstevel@tonic-gate 		/*
8410Sstevel@tonic-gate 		 * Bug ID 1246045: Nothing was sent, so set retryaddr to
8420Sstevel@tonic-gate 		 * NULL and let connmgr_get() bind to any source port it
8430Sstevel@tonic-gate 		 * can get.
8440Sstevel@tonic-gate 		 */
8450Sstevel@tonic-gate 		retryaddr = NULL;
8460Sstevel@tonic-gate 	}
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate 	RPCLOG(64, "clnt_cots_kcallit: xid = 0x%x", p->cku_xid);
8490Sstevel@tonic-gate 	RPCLOG(64, " flags = 0x%x\n", p->cku_flags);
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate 	p->cku_err.re_status = RPC_TIMEDOUT;
8520Sstevel@tonic-gate 	p->cku_err.re_errno = p->cku_err.re_terrno = 0;
8530Sstevel@tonic-gate 
8540Sstevel@tonic-gate 	cm_entry = connmgr_wrapget(retryaddr, &cwait, p);
8550Sstevel@tonic-gate 
8560Sstevel@tonic-gate 	if (cm_entry == NULL) {
8570Sstevel@tonic-gate 		RPCLOG(1, "clnt_cots_kcallit: can't connect status %s\n",
8580Sstevel@tonic-gate 		    clnt_sperrno(p->cku_err.re_status));
8590Sstevel@tonic-gate 
8600Sstevel@tonic-gate 		/*
8610Sstevel@tonic-gate 		 * The reasons why we fail to create a connection are
8620Sstevel@tonic-gate 		 * varied. In most cases we don't want the caller to
8630Sstevel@tonic-gate 		 * immediately retry. This could have one or more
8640Sstevel@tonic-gate 		 * bad effects. This includes flooding the net with
8650Sstevel@tonic-gate 		 * connect requests to ports with no listener; a hard
8660Sstevel@tonic-gate 		 * kernel loop due to all the "reserved" TCP ports being
8670Sstevel@tonic-gate 		 * in use.
8680Sstevel@tonic-gate 		 */
8690Sstevel@tonic-gate 		delay_first = TRUE;
8700Sstevel@tonic-gate 
8710Sstevel@tonic-gate 		/*
8720Sstevel@tonic-gate 		 * Even if we end up returning EINTR, we still count a
8730Sstevel@tonic-gate 		 * a "can't connect", because the connection manager
8740Sstevel@tonic-gate 		 * might have been committed to waiting for or timing out on
8750Sstevel@tonic-gate 		 * a connection.
8760Sstevel@tonic-gate 		 */
8770Sstevel@tonic-gate 		COTSRCSTAT_INCR(p->cku_stats, rccantconn);
8780Sstevel@tonic-gate 		switch (p->cku_err.re_status) {
8790Sstevel@tonic-gate 		case RPC_INTR:
8800Sstevel@tonic-gate 			p->cku_err.re_errno = EINTR;
8810Sstevel@tonic-gate 
8820Sstevel@tonic-gate 			/*
8830Sstevel@tonic-gate 			 * No need to delay because a UNIX signal(2)
8840Sstevel@tonic-gate 			 * interrupted us. The caller likely won't
8850Sstevel@tonic-gate 			 * retry the CLNT_CALL() and even if it does,
8860Sstevel@tonic-gate 			 * we assume the caller knows what it is doing.
8870Sstevel@tonic-gate 			 */
8880Sstevel@tonic-gate 			delay_first = FALSE;
8890Sstevel@tonic-gate 			break;
8900Sstevel@tonic-gate 
8910Sstevel@tonic-gate 		case RPC_TIMEDOUT:
8920Sstevel@tonic-gate 			p->cku_err.re_errno = ETIMEDOUT;
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate 			/*
8950Sstevel@tonic-gate 			 * No need to delay because timed out already
8960Sstevel@tonic-gate 			 * on the connection request and assume that the
8970Sstevel@tonic-gate 			 * transport time out is longer than our minimum
8980Sstevel@tonic-gate 			 * timeout, or least not too much smaller.
8990Sstevel@tonic-gate 			 */
9000Sstevel@tonic-gate 			delay_first = FALSE;
9010Sstevel@tonic-gate 			break;
9020Sstevel@tonic-gate 
9030Sstevel@tonic-gate 		case RPC_SYSTEMERROR:
9040Sstevel@tonic-gate 		case RPC_TLIERROR:
9050Sstevel@tonic-gate 			/*
9060Sstevel@tonic-gate 			 * We want to delay here because a transient
9070Sstevel@tonic-gate 			 * system error has a better chance of going away
9080Sstevel@tonic-gate 			 * if we delay a bit. If it's not transient, then
9090Sstevel@tonic-gate 			 * we don't want end up in a hard kernel loop
9100Sstevel@tonic-gate 			 * due to retries.
9110Sstevel@tonic-gate 			 */
9120Sstevel@tonic-gate 			ASSERT(p->cku_err.re_errno != 0);
9130Sstevel@tonic-gate 			break;
9140Sstevel@tonic-gate 
9150Sstevel@tonic-gate 
9160Sstevel@tonic-gate 		case RPC_CANTCONNECT:
9170Sstevel@tonic-gate 			/*
9180Sstevel@tonic-gate 			 * RPC_CANTCONNECT is set on T_ERROR_ACK which
9190Sstevel@tonic-gate 			 * implies some error down in the TCP layer or
9200Sstevel@tonic-gate 			 * below. If cku_nodelayonerror is set then we
9210Sstevel@tonic-gate 			 * assume the caller knows not to try too hard.
9220Sstevel@tonic-gate 			 */
9230Sstevel@tonic-gate 			RPCLOG0(8, "clnt_cots_kcallit: connection failed,");
9240Sstevel@tonic-gate 			RPCLOG0(8, " re_status=RPC_CANTCONNECT,");
9250Sstevel@tonic-gate 			RPCLOG(8, " re_errno=%d,", p->cku_err.re_errno);
9260Sstevel@tonic-gate 			RPCLOG(8, " cku_nodelayonerr=%d", p->cku_nodelayonerr);
9270Sstevel@tonic-gate 			if (p->cku_nodelayonerr == TRUE)
9280Sstevel@tonic-gate 				delay_first = FALSE;
9290Sstevel@tonic-gate 
9300Sstevel@tonic-gate 			p->cku_err.re_errno = EIO;
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate 			break;
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate 		case RPC_XPRTFAILED:
9350Sstevel@tonic-gate 			/*
9360Sstevel@tonic-gate 			 * We want to delay here because we likely
9370Sstevel@tonic-gate 			 * got a refused connection.
9380Sstevel@tonic-gate 			 */
9390Sstevel@tonic-gate 			if (p->cku_err.re_errno != 0)
9400Sstevel@tonic-gate 				break;
9410Sstevel@tonic-gate 
9420Sstevel@tonic-gate 			/* fall thru */
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate 		default:
9450Sstevel@tonic-gate 			/*
9460Sstevel@tonic-gate 			 * We delay here because it is better to err
9470Sstevel@tonic-gate 			 * on the side of caution. If we got here then
9480Sstevel@tonic-gate 			 * status could have been RPC_SUCCESS, but we
9490Sstevel@tonic-gate 			 * know that we did not get a connection, so
9500Sstevel@tonic-gate 			 * force the rpc status to RPC_CANTCONNECT.
9510Sstevel@tonic-gate 			 */
9520Sstevel@tonic-gate 			p->cku_err.re_status = RPC_CANTCONNECT;
9530Sstevel@tonic-gate 			p->cku_err.re_errno = EIO;
9540Sstevel@tonic-gate 			break;
9550Sstevel@tonic-gate 		}
9560Sstevel@tonic-gate 		if (delay_first == TRUE)
9570Sstevel@tonic-gate 			ticks = clnt_cots_min_tout * drv_usectohz(1000000);
9580Sstevel@tonic-gate 		goto cots_done;
9590Sstevel@tonic-gate 	}
9600Sstevel@tonic-gate 
9610Sstevel@tonic-gate 	/*
9620Sstevel@tonic-gate 	 * If we've never sent any request on this connection (send count
9630Sstevel@tonic-gate 	 * is zero, or the connection has been reset), cache the
9640Sstevel@tonic-gate 	 * the connection's create time and send a request (possibly a retry)
9650Sstevel@tonic-gate 	 */
9660Sstevel@tonic-gate 	if ((p->cku_flags & CKU_SENT) == 0 ||
9670Sstevel@tonic-gate 	    p->cku_ctime != cm_entry->x_ctime) {
9680Sstevel@tonic-gate 		p->cku_ctime = cm_entry->x_ctime;
9690Sstevel@tonic-gate 
9700Sstevel@tonic-gate 	} else if ((p->cku_flags & CKU_SENT) && (p->cku_flags & CKU_ONQUEUE) &&
9710Sstevel@tonic-gate 	    (call->call_reply != NULL ||
9720Sstevel@tonic-gate 	    p->cku_recv_attempts < clnt_cots_maxrecv)) {
9730Sstevel@tonic-gate 
9740Sstevel@tonic-gate 		/*
9750Sstevel@tonic-gate 		 * If we've sent a request and our call is on the dispatch
9760Sstevel@tonic-gate 		 * queue and we haven't made too many receive attempts, then
9770Sstevel@tonic-gate 		 * don't re-send, just receive.
9780Sstevel@tonic-gate 		 */
9790Sstevel@tonic-gate 		p->cku_recv_attempts++;
9800Sstevel@tonic-gate 		goto read_again;
9810Sstevel@tonic-gate 	}
9820Sstevel@tonic-gate 
9830Sstevel@tonic-gate 	/*
9840Sstevel@tonic-gate 	 * Now we create the RPC request in a STREAMS message.  We have to do
9850Sstevel@tonic-gate 	 * this after the call to connmgr_get so that we have the correct
9860Sstevel@tonic-gate 	 * TIDU size for the transport.
9870Sstevel@tonic-gate 	 */
9880Sstevel@tonic-gate 	tidu_size = cm_entry->x_tidu_size;
9890Sstevel@tonic-gate 	len = MSG_OFFSET + MAX(tidu_size, RM_HDR_SIZE + WIRE_HDR_SIZE);
9900Sstevel@tonic-gate 
9910Sstevel@tonic-gate 	while ((mp = allocb(len, BPRI_MED)) == NULL) {
9920Sstevel@tonic-gate 		if (strwaitbuf(len, BPRI_MED)) {
9930Sstevel@tonic-gate 			p->cku_err.re_status = RPC_SYSTEMERROR;
9940Sstevel@tonic-gate 			p->cku_err.re_errno = ENOSR;
9950Sstevel@tonic-gate 			COTSRCSTAT_INCR(p->cku_stats, rcnomem);
9960Sstevel@tonic-gate 			goto cots_done;
9970Sstevel@tonic-gate 		}
9980Sstevel@tonic-gate 	}
9990Sstevel@tonic-gate 	xdrs = &p->cku_outxdr;
10000Sstevel@tonic-gate 	xdrmblk_init(xdrs, mp, XDR_ENCODE, tidu_size);
10010Sstevel@tonic-gate 	mpsize = MBLKSIZE(mp);
10020Sstevel@tonic-gate 	ASSERT(mpsize >= len);
10030Sstevel@tonic-gate 	ASSERT(mp->b_rptr == mp->b_datap->db_base);
10040Sstevel@tonic-gate 
10050Sstevel@tonic-gate 	/*
10060Sstevel@tonic-gate 	 * If the size of mblk is not appreciably larger than what we
10070Sstevel@tonic-gate 	 * asked, then resize the mblk to exactly len bytes. The reason for
10080Sstevel@tonic-gate 	 * this: suppose len is 1600 bytes, the tidu is 1460 bytes
10090Sstevel@tonic-gate 	 * (from TCP over ethernet), and the arguments to the RPC require
10100Sstevel@tonic-gate 	 * 2800 bytes. Ideally we want the protocol to render two
10110Sstevel@tonic-gate 	 * ~1400 byte segments over the wire. However if allocb() gives us a 2k
10120Sstevel@tonic-gate 	 * mblk, and we allocate a second mblk for the remainder, the protocol
10130Sstevel@tonic-gate 	 * module may generate 3 segments over the wire:
10140Sstevel@tonic-gate 	 * 1460 bytes for the first, 448 (2048 - 1600) for the second, and
10150Sstevel@tonic-gate 	 * 892 for the third. If we "waste" 448 bytes in the first mblk,
10160Sstevel@tonic-gate 	 * the XDR encoding will generate two ~1400 byte mblks, and the
10170Sstevel@tonic-gate 	 * protocol module is more likely to produce properly sized segments.
10180Sstevel@tonic-gate 	 */
10190Sstevel@tonic-gate 	if ((mpsize >> 1) <= len)
10200Sstevel@tonic-gate 		mp->b_rptr += (mpsize - len);
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate 	/*
10230Sstevel@tonic-gate 	 * Adjust b_rptr to reserve space for the non-data protocol headers
10240Sstevel@tonic-gate 	 * any downstream modules might like to add, and for the
10250Sstevel@tonic-gate 	 * record marking header.
10260Sstevel@tonic-gate 	 */
10270Sstevel@tonic-gate 	mp->b_rptr += (MSG_OFFSET + RM_HDR_SIZE);
10280Sstevel@tonic-gate 
10290Sstevel@tonic-gate 	if (h->cl_auth->ah_cred.oa_flavor != RPCSEC_GSS) {
10300Sstevel@tonic-gate 		/* Copy in the preserialized RPC header information. */
10310Sstevel@tonic-gate 		bcopy(p->cku_rpchdr, mp->b_rptr, WIRE_HDR_SIZE);
10320Sstevel@tonic-gate 
10330Sstevel@tonic-gate 		/* Use XDR_SETPOS() to set the b_wptr to past the RPC header. */
10340Sstevel@tonic-gate 		XDR_SETPOS(xdrs, (uint_t)(mp->b_rptr - mp->b_datap->db_base +
10350Sstevel@tonic-gate 		    WIRE_HDR_SIZE));
10360Sstevel@tonic-gate 
10370Sstevel@tonic-gate 		ASSERT((mp->b_wptr - mp->b_rptr) == WIRE_HDR_SIZE);
10380Sstevel@tonic-gate 
10390Sstevel@tonic-gate 		/* Serialize the procedure number and the arguments. */
10400Sstevel@tonic-gate 		if ((!XDR_PUTINT32(xdrs, (int32_t *)&procnum)) ||
10410Sstevel@tonic-gate 		    (!AUTH_MARSHALL(h->cl_auth, xdrs, p->cku_cred)) ||
10420Sstevel@tonic-gate 		    (!(*xdr_args)(xdrs, argsp))) {
10430Sstevel@tonic-gate 			p->cku_err.re_status = RPC_CANTENCODEARGS;
10440Sstevel@tonic-gate 			p->cku_err.re_errno = EIO;
10450Sstevel@tonic-gate 			goto cots_done;
10460Sstevel@tonic-gate 		}
10470Sstevel@tonic-gate 
10480Sstevel@tonic-gate 		(*(uint32_t *)(mp->b_rptr)) = p->cku_xid;
10490Sstevel@tonic-gate 	} else {
10500Sstevel@tonic-gate 		uint32_t *uproc = (uint32_t *)&p->cku_rpchdr[WIRE_HDR_SIZE];
10510Sstevel@tonic-gate 		IXDR_PUT_U_INT32(uproc, procnum);
10520Sstevel@tonic-gate 
10530Sstevel@tonic-gate 		(*(uint32_t *)(&p->cku_rpchdr[0])) = p->cku_xid;
10540Sstevel@tonic-gate 
10550Sstevel@tonic-gate 		/* Use XDR_SETPOS() to set the b_wptr. */
10560Sstevel@tonic-gate 		XDR_SETPOS(xdrs, (uint_t)(mp->b_rptr - mp->b_datap->db_base));
10570Sstevel@tonic-gate 
10580Sstevel@tonic-gate 		/* Serialize the procedure number and the arguments. */
10590Sstevel@tonic-gate 		if (!AUTH_WRAP(h->cl_auth, p->cku_rpchdr, WIRE_HDR_SIZE+4,
10600Sstevel@tonic-gate 		    xdrs, xdr_args, argsp)) {
10610Sstevel@tonic-gate 			p->cku_err.re_status = RPC_CANTENCODEARGS;
10620Sstevel@tonic-gate 			p->cku_err.re_errno = EIO;
10630Sstevel@tonic-gate 			goto cots_done;
10640Sstevel@tonic-gate 		}
10650Sstevel@tonic-gate 	}
10660Sstevel@tonic-gate 
10670Sstevel@tonic-gate 	RPCLOG(2, "clnt_cots_kcallit: connected, sending call, tidu_size %d\n",
10680Sstevel@tonic-gate 	    tidu_size);
10690Sstevel@tonic-gate 
10700Sstevel@tonic-gate 	wq = cm_entry->x_wq;
10710Sstevel@tonic-gate 	clnt_dispatch_send(wq, mp, call, p->cku_xid,
10720Sstevel@tonic-gate 				(p->cku_flags & CKU_ONQUEUE));
10730Sstevel@tonic-gate 
10740Sstevel@tonic-gate 	RPCLOG(64, "clnt_cots_kcallit: sent call for xid 0x%x\n",
10750Sstevel@tonic-gate 		(uint_t)p->cku_xid);
10760Sstevel@tonic-gate 	p->cku_flags = (CKU_ONQUEUE|CKU_SENT);
10770Sstevel@tonic-gate 	p->cku_recv_attempts = 1;
10780Sstevel@tonic-gate 
10790Sstevel@tonic-gate #ifdef	RPCDEBUG
10800Sstevel@tonic-gate 	time_sent = lbolt;
10810Sstevel@tonic-gate #endif
10820Sstevel@tonic-gate 
10830Sstevel@tonic-gate 	/*
10840Sstevel@tonic-gate 	 * Wait for a reply or a timeout.  If there is no error or timeout,
10850Sstevel@tonic-gate 	 * (both indicated by call_status), call->call_reply will contain
10860Sstevel@tonic-gate 	 * the RPC reply message.
10870Sstevel@tonic-gate 	 */
10880Sstevel@tonic-gate read_again:
10890Sstevel@tonic-gate 	mutex_enter(&call->call_lock);
10900Sstevel@tonic-gate 	interrupted = 0;
10910Sstevel@tonic-gate 	if (call->call_status == RPC_TIMEDOUT) {
10920Sstevel@tonic-gate 		/*
10930Sstevel@tonic-gate 		 * Indicate that the lwp is not to be stopped while waiting
10940Sstevel@tonic-gate 		 * for this network traffic.  This is to avoid deadlock while
10950Sstevel@tonic-gate 		 * debugging a process via /proc and also to avoid recursive
10960Sstevel@tonic-gate 		 * mutex_enter()s due to NFS page faults while stopping
10970Sstevel@tonic-gate 		 * (NFS holds locks when it calls here).
10980Sstevel@tonic-gate 		 */
10990Sstevel@tonic-gate 		clock_t cv_wait_ret;
11000Sstevel@tonic-gate 		clock_t timout;
11010Sstevel@tonic-gate 		clock_t oldlbolt;
11020Sstevel@tonic-gate 
11030Sstevel@tonic-gate 		klwp_t *lwp = ttolwp(curthread);
11040Sstevel@tonic-gate 
11050Sstevel@tonic-gate 		if (lwp != NULL)
11060Sstevel@tonic-gate 			lwp->lwp_nostop++;
11070Sstevel@tonic-gate 
11080Sstevel@tonic-gate 		oldlbolt = lbolt;
11090Sstevel@tonic-gate 		timout = wait.tv_sec * drv_usectohz(1000000) +
11100Sstevel@tonic-gate 		    drv_usectohz(wait.tv_usec) + oldlbolt;
11110Sstevel@tonic-gate 		/*
11120Sstevel@tonic-gate 		 * Iterate until the call_status is changed to something
11130Sstevel@tonic-gate 		 * other that RPC_TIMEDOUT, or if cv_timedwait_sig() returns
11140Sstevel@tonic-gate 		 * something <=0 zero. The latter means that we timed
11150Sstevel@tonic-gate 		 * out.
11160Sstevel@tonic-gate 		 */
11170Sstevel@tonic-gate 		if (h->cl_nosignal)
11180Sstevel@tonic-gate 			while ((cv_wait_ret = cv_timedwait(&call->call_cv,
11190Sstevel@tonic-gate 			    &call->call_lock, timout)) > 0 &&
11200Sstevel@tonic-gate 			    call->call_status == RPC_TIMEDOUT);
11210Sstevel@tonic-gate 		else
11220Sstevel@tonic-gate 			while ((cv_wait_ret = cv_timedwait_sig(
11230Sstevel@tonic-gate 			    &call->call_cv,
11240Sstevel@tonic-gate 			    &call->call_lock, timout)) > 0 &&
11250Sstevel@tonic-gate 			    call->call_status == RPC_TIMEDOUT);
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate 		switch (cv_wait_ret) {
11280Sstevel@tonic-gate 		case 0:
11290Sstevel@tonic-gate 			/*
11300Sstevel@tonic-gate 			 * If we got out of the above loop with
11310Sstevel@tonic-gate 			 * cv_timedwait_sig() returning 0, then we were
11320Sstevel@tonic-gate 			 * interrupted regardless what call_status is.
11330Sstevel@tonic-gate 			 */
11340Sstevel@tonic-gate 			interrupted = 1;
11350Sstevel@tonic-gate 			break;
11360Sstevel@tonic-gate 		case -1:
11370Sstevel@tonic-gate 			/* cv_timedwait_sig() timed out */
11380Sstevel@tonic-gate 			break;
11390Sstevel@tonic-gate 		default:
11400Sstevel@tonic-gate 
11410Sstevel@tonic-gate 			/*
11420Sstevel@tonic-gate 			 * We were cv_signaled(). If we didn't
11430Sstevel@tonic-gate 			 * get a successful call_status and returned
11440Sstevel@tonic-gate 			 * before time expired, delay up to clnt_cots_min_tout
11450Sstevel@tonic-gate 			 * seconds so that the caller doesn't immediately
11460Sstevel@tonic-gate 			 * try to call us again and thus force the
11470Sstevel@tonic-gate 			 * same condition that got us here (such
11480Sstevel@tonic-gate 			 * as a RPC_XPRTFAILED due to the server not
11490Sstevel@tonic-gate 			 * listening on the end-point.
11500Sstevel@tonic-gate 			 */
11510Sstevel@tonic-gate 			if (call->call_status != RPC_SUCCESS) {
11520Sstevel@tonic-gate 				clock_t curlbolt;
11530Sstevel@tonic-gate 				clock_t diff;
11540Sstevel@tonic-gate 
11550Sstevel@tonic-gate 				curlbolt = ddi_get_lbolt();
11560Sstevel@tonic-gate 				ticks = clnt_cots_min_tout *
11570Sstevel@tonic-gate 				    drv_usectohz(1000000);
11580Sstevel@tonic-gate 				diff = curlbolt - oldlbolt;
11590Sstevel@tonic-gate 				if (diff < ticks) {
11600Sstevel@tonic-gate 					delay_first = TRUE;
11610Sstevel@tonic-gate 					if (diff > 0)
11620Sstevel@tonic-gate 						ticks -= diff;
11630Sstevel@tonic-gate 				}
11640Sstevel@tonic-gate 			}
11650Sstevel@tonic-gate 			break;
11660Sstevel@tonic-gate 		}
11670Sstevel@tonic-gate 
11680Sstevel@tonic-gate 		if (lwp != NULL)
11690Sstevel@tonic-gate 			lwp->lwp_nostop--;
11700Sstevel@tonic-gate 	}
11710Sstevel@tonic-gate 	/*
11720Sstevel@tonic-gate 	 * Get the reply message, if any.  This will be freed at the end
11730Sstevel@tonic-gate 	 * whether or not an error occurred.
11740Sstevel@tonic-gate 	 */
11750Sstevel@tonic-gate 	mp = call->call_reply;
11760Sstevel@tonic-gate 	call->call_reply = NULL;
11770Sstevel@tonic-gate 
11780Sstevel@tonic-gate 	/*
11790Sstevel@tonic-gate 	 * call_err is the error info when the call is on dispatch queue.
11800Sstevel@tonic-gate 	 * cku_err is the error info returned to the caller.
11810Sstevel@tonic-gate 	 * Sync cku_err with call_err for local message processing.
11820Sstevel@tonic-gate 	 */
11830Sstevel@tonic-gate 
11840Sstevel@tonic-gate 	status = call->call_status;
11850Sstevel@tonic-gate 	p->cku_err = call->call_err;
11860Sstevel@tonic-gate 	mutex_exit(&call->call_lock);
11870Sstevel@tonic-gate 
11880Sstevel@tonic-gate 	if (status != RPC_SUCCESS) {
11890Sstevel@tonic-gate 		switch (status) {
11900Sstevel@tonic-gate 		case RPC_TIMEDOUT:
11910Sstevel@tonic-gate 			if (interrupted) {
11920Sstevel@tonic-gate 				COTSRCSTAT_INCR(p->cku_stats, rcintrs);
11930Sstevel@tonic-gate 				p->cku_err.re_status = RPC_INTR;
11940Sstevel@tonic-gate 				p->cku_err.re_errno = EINTR;
11950Sstevel@tonic-gate 				RPCLOG(1, "clnt_cots_kcallit: xid 0x%x",
11960Sstevel@tonic-gate 				    p->cku_xid);
11970Sstevel@tonic-gate 				RPCLOG(1, "signal interrupted at %ld", lbolt);
11980Sstevel@tonic-gate 				RPCLOG(1, ", was sent at %ld\n", time_sent);
11990Sstevel@tonic-gate 			} else {
12000Sstevel@tonic-gate 				COTSRCSTAT_INCR(p->cku_stats, rctimeouts);
12010Sstevel@tonic-gate 				p->cku_err.re_errno = ETIMEDOUT;
12020Sstevel@tonic-gate 				RPCLOG(1, "clnt_cots_kcallit: timed out at %ld",
12030Sstevel@tonic-gate 				    lbolt);
12040Sstevel@tonic-gate 				RPCLOG(1, ", was sent at %ld\n", time_sent);
12050Sstevel@tonic-gate 			}
12060Sstevel@tonic-gate 			break;
12070Sstevel@tonic-gate 
12080Sstevel@tonic-gate 		case RPC_XPRTFAILED:
12090Sstevel@tonic-gate 			if (p->cku_err.re_errno == 0)
12100Sstevel@tonic-gate 				p->cku_err.re_errno = EIO;
12110Sstevel@tonic-gate 
12120Sstevel@tonic-gate 			RPCLOG(1, "clnt_cots_kcallit: transport failed: %d\n",
12130Sstevel@tonic-gate 			    p->cku_err.re_errno);
12140Sstevel@tonic-gate 			break;
12150Sstevel@tonic-gate 
12160Sstevel@tonic-gate 		case RPC_SYSTEMERROR:
12170Sstevel@tonic-gate 			ASSERT(p->cku_err.re_errno);
12180Sstevel@tonic-gate 			RPCLOG(1, "clnt_cots_kcallit: system error: %d\n",
12190Sstevel@tonic-gate 			    p->cku_err.re_errno);
12200Sstevel@tonic-gate 			break;
12210Sstevel@tonic-gate 
12220Sstevel@tonic-gate 		default:
12230Sstevel@tonic-gate 			p->cku_err.re_status = RPC_SYSTEMERROR;
12240Sstevel@tonic-gate 			p->cku_err.re_errno = EIO;
12250Sstevel@tonic-gate 			RPCLOG(1, "clnt_cots_kcallit: error: %s\n",
12260Sstevel@tonic-gate 			    clnt_sperrno(status));
12270Sstevel@tonic-gate 			break;
12280Sstevel@tonic-gate 		}
12290Sstevel@tonic-gate 		if (p->cku_err.re_status != RPC_TIMEDOUT) {
12300Sstevel@tonic-gate 
12310Sstevel@tonic-gate 			if (p->cku_flags & CKU_ONQUEUE) {
12320Sstevel@tonic-gate 				call_table_remove(call);
12330Sstevel@tonic-gate 				p->cku_flags &= ~CKU_ONQUEUE;
12340Sstevel@tonic-gate 			}
12350Sstevel@tonic-gate 
12360Sstevel@tonic-gate 			RPCLOG(64, "clnt_cots_kcallit: non TIMEOUT so xid 0x%x "
12370Sstevel@tonic-gate 			    "taken off dispatch list\n", p->cku_xid);
12380Sstevel@tonic-gate 			if (call->call_reply) {
12390Sstevel@tonic-gate 				freemsg(call->call_reply);
12400Sstevel@tonic-gate 				call->call_reply = NULL;
12410Sstevel@tonic-gate 			}
12420Sstevel@tonic-gate 		} else if (wait.tv_sec != 0) {
12430Sstevel@tonic-gate 			/*
12440Sstevel@tonic-gate 			 * We've sent the request over TCP and so we have
12450Sstevel@tonic-gate 			 * every reason to believe it will get
12460Sstevel@tonic-gate 			 * delivered. In which case returning a timeout is not
12470Sstevel@tonic-gate 			 * appropriate.
12480Sstevel@tonic-gate 			 */
12490Sstevel@tonic-gate 			if (p->cku_progress == TRUE &&
12500Sstevel@tonic-gate 			    p->cku_recv_attempts < clnt_cots_maxrecv) {
12510Sstevel@tonic-gate 				p->cku_err.re_status = RPC_INPROGRESS;
12520Sstevel@tonic-gate 			}
12530Sstevel@tonic-gate 		}
12540Sstevel@tonic-gate 		goto cots_done;
12550Sstevel@tonic-gate 	}
12560Sstevel@tonic-gate 
12570Sstevel@tonic-gate 	xdrs = &p->cku_inxdr;
12580Sstevel@tonic-gate 	xdrmblk_init(xdrs, mp, XDR_DECODE, 0);
12590Sstevel@tonic-gate 
12600Sstevel@tonic-gate 	reply_msg.rm_direction = REPLY;
12610Sstevel@tonic-gate 	reply_msg.rm_reply.rp_stat = MSG_ACCEPTED;
12620Sstevel@tonic-gate 	reply_msg.acpted_rply.ar_stat = SUCCESS;
12630Sstevel@tonic-gate 
12640Sstevel@tonic-gate 	reply_msg.acpted_rply.ar_verf = _null_auth;
12650Sstevel@tonic-gate 	/*
12660Sstevel@tonic-gate 	 *  xdr_results will be done in AUTH_UNWRAP.
12670Sstevel@tonic-gate 	 */
12680Sstevel@tonic-gate 	reply_msg.acpted_rply.ar_results.where = NULL;
12690Sstevel@tonic-gate 	reply_msg.acpted_rply.ar_results.proc = xdr_void;
12700Sstevel@tonic-gate 
12710Sstevel@tonic-gate 	if (xdr_replymsg(xdrs, &reply_msg)) {
12720Sstevel@tonic-gate 		enum clnt_stat re_status;
12730Sstevel@tonic-gate 
12740Sstevel@tonic-gate 		_seterr_reply(&reply_msg, &p->cku_err);
12750Sstevel@tonic-gate 
12760Sstevel@tonic-gate 		re_status = p->cku_err.re_status;
12770Sstevel@tonic-gate 		if (re_status == RPC_SUCCESS) {
12780Sstevel@tonic-gate 			/*
12790Sstevel@tonic-gate 			 * Reply is good, check auth.
12800Sstevel@tonic-gate 			 */
12810Sstevel@tonic-gate 			if (!AUTH_VALIDATE(h->cl_auth,
12820Sstevel@tonic-gate 			    &reply_msg.acpted_rply.ar_verf)) {
12830Sstevel@tonic-gate 				COTSRCSTAT_INCR(p->cku_stats, rcbadverfs);
12840Sstevel@tonic-gate 				RPCLOG0(1, "clnt_cots_kcallit: validation "
12850Sstevel@tonic-gate 				    "failure\n");
12860Sstevel@tonic-gate 				freemsg(mp);
12870Sstevel@tonic-gate 				(void) xdr_rpc_free_verifier(xdrs, &reply_msg);
12880Sstevel@tonic-gate 				mutex_enter(&call->call_lock);
12890Sstevel@tonic-gate 				if (call->call_reply == NULL)
12900Sstevel@tonic-gate 					call->call_status = RPC_TIMEDOUT;
12910Sstevel@tonic-gate 				mutex_exit(&call->call_lock);
12920Sstevel@tonic-gate 				goto read_again;
12930Sstevel@tonic-gate 			} else if (!AUTH_UNWRAP(h->cl_auth, xdrs,
12940Sstevel@tonic-gate 			    xdr_results, resultsp)) {
12950Sstevel@tonic-gate 				RPCLOG0(1, "clnt_cots_kcallit: validation "
12960Sstevel@tonic-gate 				    "failure (unwrap)\n");
12970Sstevel@tonic-gate 				p->cku_err.re_status = RPC_CANTDECODERES;
12980Sstevel@tonic-gate 				p->cku_err.re_errno = EIO;
12990Sstevel@tonic-gate 			}
13000Sstevel@tonic-gate 		} else {
13010Sstevel@tonic-gate 			/* set errno in case we can't recover */
13020Sstevel@tonic-gate 			if (re_status != RPC_VERSMISMATCH &&
13030Sstevel@tonic-gate 			    re_status != RPC_AUTHERROR &&
13040Sstevel@tonic-gate 			    re_status != RPC_PROGVERSMISMATCH)
13050Sstevel@tonic-gate 				p->cku_err.re_errno = EIO;
13060Sstevel@tonic-gate 
13070Sstevel@tonic-gate 			if (re_status == RPC_AUTHERROR) {
13080Sstevel@tonic-gate 				/*
13090Sstevel@tonic-gate 				 * Maybe our credential need to be
13100Sstevel@tonic-gate 				 * refreshed
13110Sstevel@tonic-gate 				 */
13120Sstevel@tonic-gate 			    if ((refreshes > 0) &&
13130Sstevel@tonic-gate 				AUTH_REFRESH(h->cl_auth, &reply_msg,
13140Sstevel@tonic-gate 						p->cku_cred)) {
13150Sstevel@tonic-gate 				refreshes--;
13160Sstevel@tonic-gate 				(void) xdr_rpc_free_verifier(xdrs, &reply_msg);
13170Sstevel@tonic-gate 				freemsg(mp);
13180Sstevel@tonic-gate 				mp = NULL;
13190Sstevel@tonic-gate 
13200Sstevel@tonic-gate 				if (p->cku_flags & CKU_ONQUEUE) {
13210Sstevel@tonic-gate 					call_table_remove(call);
13220Sstevel@tonic-gate 					p->cku_flags &= ~CKU_ONQUEUE;
13230Sstevel@tonic-gate 				}
13240Sstevel@tonic-gate 
13250Sstevel@tonic-gate 				RPCLOG(64, "clnt_cots_kcallit: AUTH_ERROR, so "
13260Sstevel@tonic-gate 				    "xid 0x%x taken off dispatch list\n",
13270Sstevel@tonic-gate 				    p->cku_xid);
13280Sstevel@tonic-gate 				if (call->call_reply) {
13290Sstevel@tonic-gate 					freemsg(call->call_reply);
13300Sstevel@tonic-gate 					call->call_reply = NULL;
13310Sstevel@tonic-gate 				}
13320Sstevel@tonic-gate 				COTSRCSTAT_INCR(p->cku_stats, rcbadcalls);
13330Sstevel@tonic-gate 				COTSRCSTAT_INCR(p->cku_stats, rcnewcreds);
13340Sstevel@tonic-gate 				goto call_again;
13350Sstevel@tonic-gate 			    } else {
13360Sstevel@tonic-gate 				/*
13370Sstevel@tonic-gate 				 * We have used the client handle to
13380Sstevel@tonic-gate 				 * do an AUTH_REFRESH and the RPC status may
13390Sstevel@tonic-gate 				 * be set to RPC_SUCCESS; Let's make sure to
13400Sstevel@tonic-gate 				 * set it to RPC_AUTHERROR.
13410Sstevel@tonic-gate 				 */
13420Sstevel@tonic-gate 				p->cku_err.re_status = RPC_AUTHERROR;
13430Sstevel@tonic-gate 				/*
13440Sstevel@tonic-gate 				 * Map recoverable and unrecoverable
13450Sstevel@tonic-gate 				 * authentication errors to appropriate errno
13460Sstevel@tonic-gate 				 */
13470Sstevel@tonic-gate 				switch (p->cku_err.re_why) {
13480Sstevel@tonic-gate 				case AUTH_BADCRED:
13490Sstevel@tonic-gate 				case AUTH_BADVERF:
13500Sstevel@tonic-gate 				case AUTH_INVALIDRESP:
13510Sstevel@tonic-gate 				case AUTH_TOOWEAK:
13520Sstevel@tonic-gate 				case AUTH_FAILED:
13530Sstevel@tonic-gate 				case RPCSEC_GSS_NOCRED:
13540Sstevel@tonic-gate 				case RPCSEC_GSS_FAILED:
13550Sstevel@tonic-gate 						p->cku_err.re_errno = EACCES;
13560Sstevel@tonic-gate 						break;
13570Sstevel@tonic-gate 				case AUTH_REJECTEDCRED:
13580Sstevel@tonic-gate 				case AUTH_REJECTEDVERF:
13590Sstevel@tonic-gate 				default:	p->cku_err.re_errno = EIO;
13600Sstevel@tonic-gate 						break;
13610Sstevel@tonic-gate 				}
13620Sstevel@tonic-gate 				RPCLOG(1, "clnt_cots_kcallit : authentication"
13630Sstevel@tonic-gate 				    " failed with RPC_AUTHERROR of type %d\n",
13640Sstevel@tonic-gate 				    (int)p->cku_err.re_why);
13650Sstevel@tonic-gate 			    }
13660Sstevel@tonic-gate 			}
13670Sstevel@tonic-gate 		}
13680Sstevel@tonic-gate 	} else {
13690Sstevel@tonic-gate 		/* reply didn't decode properly. */
13700Sstevel@tonic-gate 		p->cku_err.re_status = RPC_CANTDECODERES;
13710Sstevel@tonic-gate 		p->cku_err.re_errno = EIO;
13720Sstevel@tonic-gate 		RPCLOG0(1, "clnt_cots_kcallit: decode failure\n");
13730Sstevel@tonic-gate 	}
13740Sstevel@tonic-gate 
13750Sstevel@tonic-gate 	(void) xdr_rpc_free_verifier(xdrs, &reply_msg);
13760Sstevel@tonic-gate 
13770Sstevel@tonic-gate 	if (p->cku_flags & CKU_ONQUEUE) {
13780Sstevel@tonic-gate 		call_table_remove(call);
13790Sstevel@tonic-gate 		p->cku_flags &= ~CKU_ONQUEUE;
13800Sstevel@tonic-gate 	}
13810Sstevel@tonic-gate 
13820Sstevel@tonic-gate 	RPCLOG(64, "clnt_cots_kcallit: xid 0x%x taken off dispatch list",
13830Sstevel@tonic-gate 	    p->cku_xid);
13840Sstevel@tonic-gate 	RPCLOG(64, " status is %s\n", clnt_sperrno(p->cku_err.re_status));
13850Sstevel@tonic-gate cots_done:
13860Sstevel@tonic-gate 	if (cm_entry)
13870Sstevel@tonic-gate 		connmgr_release(cm_entry);
13880Sstevel@tonic-gate 
13890Sstevel@tonic-gate 	if (mp != NULL)
13900Sstevel@tonic-gate 		freemsg(mp);
13910Sstevel@tonic-gate 	if ((p->cku_flags & CKU_ONQUEUE) == 0 && call->call_reply) {
13920Sstevel@tonic-gate 		freemsg(call->call_reply);
13930Sstevel@tonic-gate 		call->call_reply = NULL;
13940Sstevel@tonic-gate 	}
13950Sstevel@tonic-gate 	if (p->cku_err.re_status != RPC_SUCCESS) {
13960Sstevel@tonic-gate 		RPCLOG0(1, "clnt_cots_kcallit: tail-end failure\n");
13970Sstevel@tonic-gate 		COTSRCSTAT_INCR(p->cku_stats, rcbadcalls);
13980Sstevel@tonic-gate 	}
13990Sstevel@tonic-gate 
14000Sstevel@tonic-gate 	/*
14010Sstevel@tonic-gate 	 * No point in delaying if the zone is going away.
14020Sstevel@tonic-gate 	 */
14030Sstevel@tonic-gate 	if (delay_first == TRUE &&
14040Sstevel@tonic-gate 	    !(zone_status_get(curproc->p_zone) >= ZONE_IS_SHUTTING_DOWN)) {
14050Sstevel@tonic-gate 		if (clnt_delay(ticks, h->cl_nosignal) == EINTR) {
14060Sstevel@tonic-gate 			p->cku_err.re_errno = EINTR;
14070Sstevel@tonic-gate 			p->cku_err.re_status = RPC_INTR;
14080Sstevel@tonic-gate 		}
14090Sstevel@tonic-gate 	}
14100Sstevel@tonic-gate 	return (p->cku_err.re_status);
14110Sstevel@tonic-gate }
14120Sstevel@tonic-gate 
14130Sstevel@tonic-gate /*
14140Sstevel@tonic-gate  * Kinit routine for cots.  This sets up the correct operations in
14150Sstevel@tonic-gate  * the client handle, as the handle may have previously been a clts
14160Sstevel@tonic-gate  * handle, and clears the xid field so there is no way a new call
14170Sstevel@tonic-gate  * could be mistaken for a retry.  It also sets in the handle the
14180Sstevel@tonic-gate  * information that is passed at create/kinit time but needed at
14190Sstevel@tonic-gate  * call time, as cots creates the transport at call time - device,
14200Sstevel@tonic-gate  * address of the server, protocol family.
14210Sstevel@tonic-gate  */
14220Sstevel@tonic-gate void
14230Sstevel@tonic-gate clnt_cots_kinit(CLIENT *h, dev_t dev, int family, struct netbuf *addr,
14240Sstevel@tonic-gate 	int max_msgsize, cred_t *cred)
14250Sstevel@tonic-gate {
14260Sstevel@tonic-gate 	/* LINTED pointer alignment */
14270Sstevel@tonic-gate 	cku_private_t *p = htop(h);
14280Sstevel@tonic-gate 	calllist_t *call = &p->cku_call;
14290Sstevel@tonic-gate 
14300Sstevel@tonic-gate 	h->cl_ops = &tcp_ops;
14310Sstevel@tonic-gate 	if (p->cku_flags & CKU_ONQUEUE) {
14320Sstevel@tonic-gate 		call_table_remove(call);
14330Sstevel@tonic-gate 		p->cku_flags &= ~CKU_ONQUEUE;
14340Sstevel@tonic-gate 		RPCLOG(64, "clnt_cots_kinit: removing call for xid 0x%x from"
14350Sstevel@tonic-gate 		    " dispatch list\n", p->cku_xid);
14360Sstevel@tonic-gate 	}
14370Sstevel@tonic-gate 
14380Sstevel@tonic-gate 	if (call->call_reply != NULL) {
14390Sstevel@tonic-gate 		freemsg(call->call_reply);
14400Sstevel@tonic-gate 		call->call_reply = NULL;
14410Sstevel@tonic-gate 	}
14420Sstevel@tonic-gate 
14430Sstevel@tonic-gate 	call->call_bucket = NULL;
14440Sstevel@tonic-gate 	call->call_hash = 0;
14450Sstevel@tonic-gate 
14460Sstevel@tonic-gate 	/*
14470Sstevel@tonic-gate 	 * We don't clear cku_flags here, because clnt_cots_kcallit()
14480Sstevel@tonic-gate 	 * takes care of handling the cku_flags reset.
14490Sstevel@tonic-gate 	 */
14500Sstevel@tonic-gate 	p->cku_xid = 0;
14510Sstevel@tonic-gate 	p->cku_device = dev;
14520Sstevel@tonic-gate 	p->cku_addrfmly = family;
14530Sstevel@tonic-gate 	p->cku_cred = cred;
14540Sstevel@tonic-gate 
14550Sstevel@tonic-gate 	if (p->cku_addr.maxlen < addr->len) {
14560Sstevel@tonic-gate 		if (p->cku_addr.maxlen != 0 && p->cku_addr.buf != NULL)
14570Sstevel@tonic-gate 			kmem_free(p->cku_addr.buf, p->cku_addr.maxlen);
14580Sstevel@tonic-gate 		p->cku_addr.buf = kmem_zalloc(addr->maxlen, KM_SLEEP);
14590Sstevel@tonic-gate 		p->cku_addr.maxlen = addr->maxlen;
14600Sstevel@tonic-gate 	}
14610Sstevel@tonic-gate 
14620Sstevel@tonic-gate 	p->cku_addr.len = addr->len;
14630Sstevel@tonic-gate 	bcopy(addr->buf, p->cku_addr.buf, addr->len);
14640Sstevel@tonic-gate 
14650Sstevel@tonic-gate 	/*
14660Sstevel@tonic-gate 	 * If the current sanity check size in rpcmod is smaller
14670Sstevel@tonic-gate 	 * than the size needed, then increase the sanity check.
14680Sstevel@tonic-gate 	 */
14690Sstevel@tonic-gate 	if (max_msgsize != 0 && clnt_max_msg_sizep != NULL &&
14700Sstevel@tonic-gate 	    max_msgsize > *clnt_max_msg_sizep) {
14710Sstevel@tonic-gate 		mutex_enter(&clnt_max_msg_lock);
14720Sstevel@tonic-gate 		if (max_msgsize > *clnt_max_msg_sizep)
14730Sstevel@tonic-gate 			*clnt_max_msg_sizep = max_msgsize;
14740Sstevel@tonic-gate 		mutex_exit(&clnt_max_msg_lock);
14750Sstevel@tonic-gate 	}
14760Sstevel@tonic-gate }
14770Sstevel@tonic-gate 
14780Sstevel@tonic-gate /*
14790Sstevel@tonic-gate  * ksettimers is a no-op for cots, with the exception of setting the xid.
14800Sstevel@tonic-gate  */
14810Sstevel@tonic-gate /* ARGSUSED */
14820Sstevel@tonic-gate static int
14830Sstevel@tonic-gate clnt_cots_ksettimers(CLIENT *h, struct rpc_timers *t, struct rpc_timers *all,
14840Sstevel@tonic-gate 	int minimum, void (*feedback)(int, int, caddr_t), caddr_t arg,
14850Sstevel@tonic-gate 	uint32_t xid)
14860Sstevel@tonic-gate {
14870Sstevel@tonic-gate 	/* LINTED pointer alignment */
14880Sstevel@tonic-gate 	cku_private_t *p = htop(h);
14890Sstevel@tonic-gate 
14900Sstevel@tonic-gate 	if (xid)
14910Sstevel@tonic-gate 		p->cku_xid = xid;
14920Sstevel@tonic-gate 	COTSRCSTAT_INCR(p->cku_stats, rctimers);
14930Sstevel@tonic-gate 	return (0);
14940Sstevel@tonic-gate }
14950Sstevel@tonic-gate 
14960Sstevel@tonic-gate extern void rpc_poptimod(struct vnode *);
14970Sstevel@tonic-gate extern int kstr_push(struct vnode *, char *);
14980Sstevel@tonic-gate 
14990Sstevel@tonic-gate int
15000Sstevel@tonic-gate conn_kstat_update(kstat_t *ksp, int rw)
15010Sstevel@tonic-gate {
15020Sstevel@tonic-gate 	struct cm_xprt *cm_entry;
15030Sstevel@tonic-gate 	struct cm_kstat_xprt *cm_ksp_data;
15040Sstevel@tonic-gate 	uchar_t *b;
15050Sstevel@tonic-gate 	char *fbuf;
15060Sstevel@tonic-gate 
15070Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
15080Sstevel@tonic-gate 		return (EACCES);
15090Sstevel@tonic-gate 	if (ksp == NULL || ksp->ks_private == NULL)
15100Sstevel@tonic-gate 		return (EIO);
15110Sstevel@tonic-gate 	cm_entry  = (struct cm_xprt *)ksp->ks_private;
15120Sstevel@tonic-gate 	cm_ksp_data = (struct cm_kstat_xprt *)ksp->ks_data;
15130Sstevel@tonic-gate 
15140Sstevel@tonic-gate 	cm_ksp_data->x_wq.value.ui32 = (uint32_t)(uintptr_t)cm_entry->x_wq;
15150Sstevel@tonic-gate 	cm_ksp_data->x_family.value.ui32 = cm_entry->x_family;
15160Sstevel@tonic-gate 	cm_ksp_data->x_rdev.value.ui32 = (uint32_t)cm_entry->x_rdev;
15170Sstevel@tonic-gate 	cm_ksp_data->x_time.value.ui32 = cm_entry->x_time;
15180Sstevel@tonic-gate 	cm_ksp_data->x_ref.value.ui32 = cm_entry->x_ref;
15190Sstevel@tonic-gate 	cm_ksp_data->x_state.value.ui32 = cm_entry->x_state_flags;
15200Sstevel@tonic-gate 
15210Sstevel@tonic-gate 	if (cm_entry->x_server.buf) {
15220Sstevel@tonic-gate 		fbuf = cm_ksp_data->x_server.value.string.addr.ptr;
15230Sstevel@tonic-gate 		if (cm_entry->x_family == AF_INET &&
15240Sstevel@tonic-gate 		    cm_entry->x_server.len ==
15250Sstevel@tonic-gate 		    sizeof (struct sockaddr_in)) {
15260Sstevel@tonic-gate 			struct sockaddr_in  *sa;
15270Sstevel@tonic-gate 			sa = (struct sockaddr_in *)
15280Sstevel@tonic-gate 				cm_entry->x_server.buf;
15290Sstevel@tonic-gate 			b = (uchar_t *)&sa->sin_addr;
15300Sstevel@tonic-gate 			(void) sprintf(fbuf,
15310Sstevel@tonic-gate 			    "%03d.%03d.%03d.%03d", b[0] & 0xFF, b[1] & 0xFF,
15320Sstevel@tonic-gate 			    b[2] & 0xFF, b[3] & 0xFF);
15330Sstevel@tonic-gate 			cm_ksp_data->x_port.value.ui32 =
15340Sstevel@tonic-gate 				(uint32_t)sa->sin_port;
15350Sstevel@tonic-gate 		} else if (cm_entry->x_family == AF_INET6 &&
15360Sstevel@tonic-gate 				cm_entry->x_server.len >=
15370Sstevel@tonic-gate 				sizeof (struct sockaddr_in6)) {
15380Sstevel@tonic-gate 			/* extract server IP address & port */
15390Sstevel@tonic-gate 			struct sockaddr_in6 *sin6;
15400Sstevel@tonic-gate 			sin6 = (struct sockaddr_in6 *)cm_entry->x_server.buf;
15410Sstevel@tonic-gate 			(void) kinet_ntop6((uchar_t *)&sin6->sin6_addr, fbuf,
15420Sstevel@tonic-gate 				INET6_ADDRSTRLEN);
15430Sstevel@tonic-gate 			cm_ksp_data->x_port.value.ui32 = sin6->sin6_port;
15440Sstevel@tonic-gate 		} else {
15450Sstevel@tonic-gate 			struct sockaddr_in  *sa;
15460Sstevel@tonic-gate 
15470Sstevel@tonic-gate 			sa = (struct sockaddr_in *)cm_entry->x_server.buf;
15480Sstevel@tonic-gate 			b = (uchar_t *)&sa->sin_addr;
15490Sstevel@tonic-gate 			(void) sprintf(fbuf,
15500Sstevel@tonic-gate 			    "%03d.%03d.%03d.%03d", b[0] & 0xFF, b[1] & 0xFF,
15510Sstevel@tonic-gate 			    b[2] & 0xFF, b[3] & 0xFF);
15520Sstevel@tonic-gate 		}
15530Sstevel@tonic-gate 		KSTAT_NAMED_STR_BUFLEN(&cm_ksp_data->x_server) =
15540Sstevel@tonic-gate 			strlen(fbuf) + 1;
15550Sstevel@tonic-gate 	}
15560Sstevel@tonic-gate 
15570Sstevel@tonic-gate 	return (0);
15580Sstevel@tonic-gate }
15590Sstevel@tonic-gate 
15600Sstevel@tonic-gate 
15610Sstevel@tonic-gate /*
15620Sstevel@tonic-gate  * We want a version of delay which is interruptible by a UNIX signal
15630Sstevel@tonic-gate  * Return EINTR if an interrupt occured.
15640Sstevel@tonic-gate  */
15650Sstevel@tonic-gate static int
15660Sstevel@tonic-gate clnt_delay(clock_t ticks, bool_t nosignal)
15670Sstevel@tonic-gate {
15680Sstevel@tonic-gate 	if (nosignal == TRUE) {
15690Sstevel@tonic-gate 		delay(ticks);
15700Sstevel@tonic-gate 		return (0);
15710Sstevel@tonic-gate 	}
15720Sstevel@tonic-gate 	return (delay_sig(ticks));
15730Sstevel@tonic-gate }
15740Sstevel@tonic-gate 
15750Sstevel@tonic-gate /*
15760Sstevel@tonic-gate  * Wait for a connection until a timeout, or until we are
15770Sstevel@tonic-gate  * signalled that there has been a connection state change.
15780Sstevel@tonic-gate  */
15790Sstevel@tonic-gate static enum clnt_stat
15800Sstevel@tonic-gate connmgr_cwait(struct cm_xprt *cm_entry, const struct timeval *waitp,
15810Sstevel@tonic-gate 	bool_t nosignal)
15820Sstevel@tonic-gate {
15830Sstevel@tonic-gate 	bool_t interrupted;
15840Sstevel@tonic-gate 	clock_t timout, cv_stat;
15850Sstevel@tonic-gate 	enum clnt_stat clstat;
15860Sstevel@tonic-gate 	unsigned int old_state;
15870Sstevel@tonic-gate 
15880Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&connmgr_lock));
15890Sstevel@tonic-gate 	/*
15900Sstevel@tonic-gate 	 * We wait for the transport connection to be made, or an
15910Sstevel@tonic-gate 	 * indication that it could not be made.
15920Sstevel@tonic-gate 	 */
15930Sstevel@tonic-gate 	clstat = RPC_TIMEDOUT;
15940Sstevel@tonic-gate 	interrupted = FALSE;
15950Sstevel@tonic-gate 
15960Sstevel@tonic-gate 	old_state = cm_entry->x_state_flags;
15970Sstevel@tonic-gate 	/*
15980Sstevel@tonic-gate 	 * Now loop until cv_timedwait{_sig} returns because of
15990Sstevel@tonic-gate 	 * a signal(0) or timeout(-1) or cv_signal(>0). But it may be
16000Sstevel@tonic-gate 	 * cv_signalled for various other reasons too. So loop
16010Sstevel@tonic-gate 	 * until there is a state change on the connection.
16020Sstevel@tonic-gate 	 */
16030Sstevel@tonic-gate 
16040Sstevel@tonic-gate 	timout = waitp->tv_sec * drv_usectohz(1000000) +
16050Sstevel@tonic-gate 	    drv_usectohz(waitp->tv_usec) + lbolt;
16060Sstevel@tonic-gate 
16070Sstevel@tonic-gate 	if (nosignal) {
16080Sstevel@tonic-gate 		while ((cv_stat = cv_timedwait(&cm_entry->x_conn_cv,
16090Sstevel@tonic-gate 		    &connmgr_lock, timout)) > 0 &&
16100Sstevel@tonic-gate 		    cm_entry->x_state_flags == old_state)
16110Sstevel@tonic-gate 			;
16120Sstevel@tonic-gate 	} else {
16130Sstevel@tonic-gate 		while ((cv_stat = cv_timedwait_sig(&cm_entry->x_conn_cv,
16140Sstevel@tonic-gate 		    &connmgr_lock, timout)) > 0 &&
16150Sstevel@tonic-gate 		    cm_entry->x_state_flags == old_state)
16160Sstevel@tonic-gate 			;
16170Sstevel@tonic-gate 
16180Sstevel@tonic-gate 		if (cv_stat == 0) /* got intr signal? */
16190Sstevel@tonic-gate 			interrupted = TRUE;
16200Sstevel@tonic-gate 	}
16210Sstevel@tonic-gate 
16220Sstevel@tonic-gate 	if ((cm_entry->x_state_flags & (X_BADSTATES|X_CONNECTED)) ==
16230Sstevel@tonic-gate 	    X_CONNECTED) {
16240Sstevel@tonic-gate 		clstat = RPC_SUCCESS;
16250Sstevel@tonic-gate 	} else {
16260Sstevel@tonic-gate 		if (interrupted == TRUE)
16270Sstevel@tonic-gate 			clstat = RPC_INTR;
16280Sstevel@tonic-gate 		RPCLOG(1, "connmgr_cwait: can't connect, error: %s\n",
16290Sstevel@tonic-gate 		    clnt_sperrno(clstat));
16300Sstevel@tonic-gate 	}
16310Sstevel@tonic-gate 
16320Sstevel@tonic-gate 	return (clstat);
16330Sstevel@tonic-gate }
16340Sstevel@tonic-gate 
16350Sstevel@tonic-gate /*
16360Sstevel@tonic-gate  * Primary interface for how RPC grabs a connection.
16370Sstevel@tonic-gate  */
16380Sstevel@tonic-gate static struct cm_xprt *
16390Sstevel@tonic-gate connmgr_wrapget(
16400Sstevel@tonic-gate 	struct netbuf *retryaddr,
16410Sstevel@tonic-gate 	const struct timeval *waitp,
16420Sstevel@tonic-gate 	cku_private_t *p)
16430Sstevel@tonic-gate {
16440Sstevel@tonic-gate 	struct cm_xprt *cm_entry;
16450Sstevel@tonic-gate 
16460Sstevel@tonic-gate 	cm_entry = connmgr_get(retryaddr, waitp, &p->cku_addr, p->cku_addrfmly,
16470Sstevel@tonic-gate 	    &p->cku_srcaddr, &p->cku_err, p->cku_device,
16480Sstevel@tonic-gate 	    p->cku_client.cl_nosignal, p->cku_useresvport);
16490Sstevel@tonic-gate 
16500Sstevel@tonic-gate 	if (cm_entry == NULL) {
16510Sstevel@tonic-gate 		/*
16520Sstevel@tonic-gate 		 * Re-map the call status to RPC_INTR if the err code is
16530Sstevel@tonic-gate 		 * EINTR. This can happen if calls status is RPC_TLIERROR.
16540Sstevel@tonic-gate 		 * However, don't re-map if signalling has been turned off.
16550Sstevel@tonic-gate 		 * XXX Really need to create a separate thread whenever
16560Sstevel@tonic-gate 		 * there isn't an existing connection.
16570Sstevel@tonic-gate 		 */
16580Sstevel@tonic-gate 		if (p->cku_err.re_errno == EINTR) {
16590Sstevel@tonic-gate 			if (p->cku_client.cl_nosignal == TRUE)
16600Sstevel@tonic-gate 				p->cku_err.re_errno = EIO;
16610Sstevel@tonic-gate 			else
16620Sstevel@tonic-gate 				p->cku_err.re_status = RPC_INTR;
16630Sstevel@tonic-gate 		}
16640Sstevel@tonic-gate 	}
16650Sstevel@tonic-gate 
16660Sstevel@tonic-gate 	return (cm_entry);
16670Sstevel@tonic-gate }
16680Sstevel@tonic-gate 
16690Sstevel@tonic-gate /*
16700Sstevel@tonic-gate  * Obtains a transport to the server specified in addr.  If a suitable transport
16710Sstevel@tonic-gate  * does not already exist in the list of cached transports, a new connection
16720Sstevel@tonic-gate  * is created, connected, and added to the list. The connection is for sending
16730Sstevel@tonic-gate  * only - the reply message may come back on another transport connection.
16740Sstevel@tonic-gate  */
16750Sstevel@tonic-gate static struct cm_xprt *
16760Sstevel@tonic-gate connmgr_get(
16770Sstevel@tonic-gate 	struct netbuf	*retryaddr,
16780Sstevel@tonic-gate 	const struct timeval	*waitp,	/* changed to a ptr to converse stack */
16790Sstevel@tonic-gate 	struct netbuf	*destaddr,
16800Sstevel@tonic-gate 	int		addrfmly,
16810Sstevel@tonic-gate 	struct netbuf	*srcaddr,
16820Sstevel@tonic-gate 	struct rpc_err	*rpcerr,
16830Sstevel@tonic-gate 	dev_t		device,
16840Sstevel@tonic-gate 	bool_t		nosignal,
16850Sstevel@tonic-gate 	int		useresvport)
16860Sstevel@tonic-gate {
16870Sstevel@tonic-gate 	struct cm_xprt *cm_entry;
16880Sstevel@tonic-gate 	struct cm_xprt *lru_entry;
16890Sstevel@tonic-gate 	struct cm_xprt **cmp;
16900Sstevel@tonic-gate 	queue_t *wq;
16910Sstevel@tonic-gate 	TIUSER *tiptr;
16920Sstevel@tonic-gate 	int i;
16930Sstevel@tonic-gate 	int retval;
16940Sstevel@tonic-gate 	clock_t prev_time;
16950Sstevel@tonic-gate 	int tidu_size;
16960Sstevel@tonic-gate 	bool_t	connected;
16970Sstevel@tonic-gate 	zoneid_t zoneid = getzoneid();
16980Sstevel@tonic-gate 
16990Sstevel@tonic-gate 	/*
17000Sstevel@tonic-gate 	 * If the call is not a retry, look for a transport entry that
17010Sstevel@tonic-gate 	 * goes to the server of interest.
17020Sstevel@tonic-gate 	 */
17030Sstevel@tonic-gate 	mutex_enter(&connmgr_lock);
17040Sstevel@tonic-gate 
17050Sstevel@tonic-gate 	if (retryaddr == NULL) {
17060Sstevel@tonic-gate use_new_conn:
17070Sstevel@tonic-gate 		i = 0;
17080Sstevel@tonic-gate 		cm_entry = lru_entry = NULL;
17090Sstevel@tonic-gate 		prev_time = lbolt;
17100Sstevel@tonic-gate 
17110Sstevel@tonic-gate 		cmp = &cm_hd;
17120Sstevel@tonic-gate 		while ((cm_entry = *cmp) != NULL) {
17130Sstevel@tonic-gate 			ASSERT(cm_entry != cm_entry->x_next);
17140Sstevel@tonic-gate 			/*
17150Sstevel@tonic-gate 			 * Garbage collect conections that are marked
17160Sstevel@tonic-gate 			 * for needs disconnect.
17170Sstevel@tonic-gate 			 */
17180Sstevel@tonic-gate 			if (cm_entry->x_needdis) {
1719*154Sshepler 				CONN_HOLD(cm_entry);
17200Sstevel@tonic-gate 				connmgr_dis_and_wait(cm_entry);
1721*154Sshepler 				connmgr_release(cm_entry);
17220Sstevel@tonic-gate 				/*
17230Sstevel@tonic-gate 				 * connmgr_lock could have been
17240Sstevel@tonic-gate 				 * dropped for the disconnect
17250Sstevel@tonic-gate 				 * processing so start over.
17260Sstevel@tonic-gate 				 */
17270Sstevel@tonic-gate 				goto use_new_conn;
17280Sstevel@tonic-gate 			}
17290Sstevel@tonic-gate 
17300Sstevel@tonic-gate 			/*
17310Sstevel@tonic-gate 			 * Garbage collect the dead connections that have
17320Sstevel@tonic-gate 			 * no threads working on them.
17330Sstevel@tonic-gate 			 */
17340Sstevel@tonic-gate 			if ((cm_entry->x_state_flags & (X_DEAD|X_THREAD)) ==
17350Sstevel@tonic-gate 			    X_DEAD) {
17360Sstevel@tonic-gate 				*cmp = cm_entry->x_next;
17370Sstevel@tonic-gate 				mutex_exit(&connmgr_lock);
17380Sstevel@tonic-gate 				connmgr_close(cm_entry);
17390Sstevel@tonic-gate 				mutex_enter(&connmgr_lock);
17400Sstevel@tonic-gate 				goto use_new_conn;
17410Sstevel@tonic-gate 			}
17420Sstevel@tonic-gate 
17430Sstevel@tonic-gate 
17440Sstevel@tonic-gate 			if ((cm_entry->x_state_flags & X_BADSTATES) == 0 &&
17450Sstevel@tonic-gate 			    cm_entry->x_zoneid == zoneid &&
17460Sstevel@tonic-gate 			    cm_entry->x_rdev == device &&
17470Sstevel@tonic-gate 			    destaddr->len == cm_entry->x_server.len &&
17480Sstevel@tonic-gate 			    bcmp(destaddr->buf, cm_entry->x_server.buf,
17490Sstevel@tonic-gate 			    destaddr->len) == 0) {
17500Sstevel@tonic-gate 				/*
17510Sstevel@tonic-gate 				 * If the matching entry isn't connected,
17520Sstevel@tonic-gate 				 * attempt to reconnect it.
17530Sstevel@tonic-gate 				 */
17540Sstevel@tonic-gate 				if (cm_entry->x_connected == FALSE) {
17550Sstevel@tonic-gate 					/*
17560Sstevel@tonic-gate 					 * We don't go through trying
17570Sstevel@tonic-gate 					 * to find the least recently
17580Sstevel@tonic-gate 					 * used connected because
17590Sstevel@tonic-gate 					 * connmgr_reconnect() briefly
17600Sstevel@tonic-gate 					 * dropped the connmgr_lock,
17610Sstevel@tonic-gate 					 * allowing a window for our
17620Sstevel@tonic-gate 					 * accounting to be messed up.
17630Sstevel@tonic-gate 					 * In any case, a re-connected
17640Sstevel@tonic-gate 					 * connection is as good as
17650Sstevel@tonic-gate 					 * a LRU connection.
17660Sstevel@tonic-gate 					 */
17670Sstevel@tonic-gate 					return (connmgr_wrapconnect(cm_entry,
17680Sstevel@tonic-gate 					    waitp, destaddr, addrfmly, srcaddr,
17690Sstevel@tonic-gate 					    rpcerr, TRUE, nosignal));
17700Sstevel@tonic-gate 				}
17710Sstevel@tonic-gate 				i++;
17720Sstevel@tonic-gate 				if (cm_entry->x_time - prev_time <= 0 ||
17730Sstevel@tonic-gate 				    lru_entry == NULL) {
17740Sstevel@tonic-gate 					prev_time = cm_entry->x_time;
17750Sstevel@tonic-gate 					lru_entry = cm_entry;
17760Sstevel@tonic-gate 				}
17770Sstevel@tonic-gate 			}
17780Sstevel@tonic-gate 			cmp = &cm_entry->x_next;
17790Sstevel@tonic-gate 		}
17800Sstevel@tonic-gate 
17810Sstevel@tonic-gate 		if (i > clnt_max_conns) {
17820Sstevel@tonic-gate 			RPCLOG(8, "connmgr_get: too many conns, dooming entry"
17830Sstevel@tonic-gate 			    " %p\n", (void *)lru_entry->x_tiptr);
17840Sstevel@tonic-gate 			lru_entry->x_doomed = TRUE;
17850Sstevel@tonic-gate 			goto use_new_conn;
17860Sstevel@tonic-gate 		}
17870Sstevel@tonic-gate 
17880Sstevel@tonic-gate 		/*
17890Sstevel@tonic-gate 		 * If we are at the maximum number of connections to
17900Sstevel@tonic-gate 		 * the server, hand back the least recently used one.
17910Sstevel@tonic-gate 		 */
17920Sstevel@tonic-gate 		if (i == clnt_max_conns) {
17930Sstevel@tonic-gate 			/*
17940Sstevel@tonic-gate 			 * Copy into the handle the source address of
17950Sstevel@tonic-gate 			 * the connection, which we will use in case of
17960Sstevel@tonic-gate 			 * a later retry.
17970Sstevel@tonic-gate 			 */
17980Sstevel@tonic-gate 			if (srcaddr->len != lru_entry->x_src.len) {
17990Sstevel@tonic-gate 				if (srcaddr->len > 0)
18000Sstevel@tonic-gate 					kmem_free(srcaddr->buf,
18010Sstevel@tonic-gate 					    srcaddr->maxlen);
18020Sstevel@tonic-gate 				srcaddr->buf = kmem_zalloc(
18030Sstevel@tonic-gate 				    lru_entry->x_src.len, KM_SLEEP);
18040Sstevel@tonic-gate 				srcaddr->maxlen = srcaddr->len =
18050Sstevel@tonic-gate 				    lru_entry->x_src.len;
18060Sstevel@tonic-gate 			}
18070Sstevel@tonic-gate 			bcopy(lru_entry->x_src.buf, srcaddr->buf, srcaddr->len);
18080Sstevel@tonic-gate 			RPCLOG(2, "connmgr_get: call going out on %p\n",
18090Sstevel@tonic-gate 			    (void *)lru_entry);
18100Sstevel@tonic-gate 			lru_entry->x_time = lbolt;
18110Sstevel@tonic-gate 			CONN_HOLD(lru_entry);
18120Sstevel@tonic-gate 			mutex_exit(&connmgr_lock);
18130Sstevel@tonic-gate 			return (lru_entry);
18140Sstevel@tonic-gate 		}
18150Sstevel@tonic-gate 
18160Sstevel@tonic-gate 	} else {
18170Sstevel@tonic-gate 		/*
18180Sstevel@tonic-gate 		 * This is the retry case (retryaddr != NULL).  Retries must
18190Sstevel@tonic-gate 		 * be sent on the same source port as the original call.
18200Sstevel@tonic-gate 		 */
18210Sstevel@tonic-gate 
18220Sstevel@tonic-gate 		/*
18230Sstevel@tonic-gate 		 * Walk the list looking for a connection with a source address
18240Sstevel@tonic-gate 		 * that matches the retry address.
18250Sstevel@tonic-gate 		 */
18260Sstevel@tonic-gate 		cmp = &cm_hd;
18270Sstevel@tonic-gate 		while ((cm_entry = *cmp) != NULL) {
18280Sstevel@tonic-gate 			ASSERT(cm_entry != cm_entry->x_next);
18290Sstevel@tonic-gate 			if (zoneid != cm_entry->x_zoneid ||
18300Sstevel@tonic-gate 			    device != cm_entry->x_rdev ||
18310Sstevel@tonic-gate 			    retryaddr->len != cm_entry->x_src.len ||
18320Sstevel@tonic-gate 			    bcmp(retryaddr->buf, cm_entry->x_src.buf,
18330Sstevel@tonic-gate 				    retryaddr->len) != 0) {
18340Sstevel@tonic-gate 				cmp = &cm_entry->x_next;
18350Sstevel@tonic-gate 				continue;
18360Sstevel@tonic-gate 			}
18370Sstevel@tonic-gate 
18380Sstevel@tonic-gate 			/*
18390Sstevel@tonic-gate 			 * Sanity check: if the connection with our source
18400Sstevel@tonic-gate 			 * port is going to some other server, something went
18410Sstevel@tonic-gate 			 * wrong, as we never delete connections (i.e. release
18420Sstevel@tonic-gate 			 * ports) unless they have been idle.  In this case,
18430Sstevel@tonic-gate 			 * it is probably better to send the call out using
18440Sstevel@tonic-gate 			 * a new source address than to fail it altogether,
18450Sstevel@tonic-gate 			 * since that port may never be released.
18460Sstevel@tonic-gate 			 */
18470Sstevel@tonic-gate 			if (destaddr->len != cm_entry->x_server.len ||
18480Sstevel@tonic-gate 				bcmp(destaddr->buf, cm_entry->x_server.buf,
18490Sstevel@tonic-gate 					destaddr->len) != 0) {
18500Sstevel@tonic-gate 				RPCLOG(1, "connmgr_get: tiptr %p"
18510Sstevel@tonic-gate 				    " is going to a different server"
18520Sstevel@tonic-gate 				    " with the port that belongs"
18530Sstevel@tonic-gate 				    " to us!\n", (void *)cm_entry->x_tiptr);
18540Sstevel@tonic-gate 				retryaddr = NULL;
18550Sstevel@tonic-gate 				goto use_new_conn;
18560Sstevel@tonic-gate 			}
18570Sstevel@tonic-gate 
18580Sstevel@tonic-gate 			/*
18590Sstevel@tonic-gate 			 * If the connection of interest is not connected and we
18600Sstevel@tonic-gate 			 * can't reconnect it, then the server is probably
18610Sstevel@tonic-gate 			 * still down.  Return NULL to the caller and let it
18620Sstevel@tonic-gate 			 * retry later if it wants to.  We have a delay so the
18630Sstevel@tonic-gate 			 * machine doesn't go into a tight retry loop.  If the
18640Sstevel@tonic-gate 			 * entry was already connected, or the reconnected was
18650Sstevel@tonic-gate 			 * successful, return this entry.
18660Sstevel@tonic-gate 			 */
18670Sstevel@tonic-gate 			if (cm_entry->x_connected == FALSE) {
18680Sstevel@tonic-gate 				return (connmgr_wrapconnect(cm_entry,
18690Sstevel@tonic-gate 				    waitp, destaddr, addrfmly, NULL,
18700Sstevel@tonic-gate 				    rpcerr, TRUE, nosignal));
18710Sstevel@tonic-gate 			} else {
18720Sstevel@tonic-gate 				CONN_HOLD(cm_entry);
18730Sstevel@tonic-gate 
18740Sstevel@tonic-gate 				cm_entry->x_time = lbolt;
18750Sstevel@tonic-gate 				mutex_exit(&connmgr_lock);
18760Sstevel@tonic-gate 				RPCLOG(2, "connmgr_get: found old "
18770Sstevel@tonic-gate 				    "transport %p for retry\n",
18780Sstevel@tonic-gate 				    (void *)cm_entry);
18790Sstevel@tonic-gate 				return (cm_entry);
18800Sstevel@tonic-gate 			}
18810Sstevel@tonic-gate 		}
18820Sstevel@tonic-gate 
18830Sstevel@tonic-gate 		/*
18840Sstevel@tonic-gate 		 * We cannot find an entry in the list for this retry.
18850Sstevel@tonic-gate 		 * Either the entry has been removed temporarily to be
18860Sstevel@tonic-gate 		 * reconnected by another thread, or the original call
18870Sstevel@tonic-gate 		 * got a port but never got connected,
18880Sstevel@tonic-gate 		 * and hence the transport never got put in the
18890Sstevel@tonic-gate 		 * list.  Fall through to the "create new connection" code -
18900Sstevel@tonic-gate 		 * the former case will fail there trying to rebind the port,
18910Sstevel@tonic-gate 		 * and the later case (and any other pathological cases) will
18920Sstevel@tonic-gate 		 * rebind and reconnect and not hang the client machine.
18930Sstevel@tonic-gate 		 */
18940Sstevel@tonic-gate 		RPCLOG0(8, "connmgr_get: no entry in list for retry\n");
18950Sstevel@tonic-gate 	}
18960Sstevel@tonic-gate 	/*
18970Sstevel@tonic-gate 	 * Set up a transport entry in the connection manager's list.
18980Sstevel@tonic-gate 	 */
18990Sstevel@tonic-gate 	cm_entry = (struct cm_xprt *)
19000Sstevel@tonic-gate 	    kmem_zalloc(sizeof (struct cm_xprt), KM_SLEEP);
19010Sstevel@tonic-gate 
19020Sstevel@tonic-gate 	cm_entry->x_server.buf = kmem_zalloc(destaddr->len, KM_SLEEP);
19030Sstevel@tonic-gate 	bcopy(destaddr->buf, cm_entry->x_server.buf, destaddr->len);
19040Sstevel@tonic-gate 	cm_entry->x_server.len = cm_entry->x_server.maxlen = destaddr->len;
19050Sstevel@tonic-gate 
19060Sstevel@tonic-gate 	cm_entry->x_state_flags = X_THREAD;
19070Sstevel@tonic-gate 	cm_entry->x_ref = 1;
19080Sstevel@tonic-gate 	cm_entry->x_family = addrfmly;
19090Sstevel@tonic-gate 	cm_entry->x_rdev = device;
19100Sstevel@tonic-gate 	cm_entry->x_zoneid = zoneid;
19110Sstevel@tonic-gate 	mutex_init(&cm_entry->x_lock, NULL, MUTEX_DEFAULT, NULL);
19120Sstevel@tonic-gate 	cv_init(&cm_entry->x_cv, NULL, CV_DEFAULT, NULL);
19130Sstevel@tonic-gate 	cv_init(&cm_entry->x_conn_cv, NULL, CV_DEFAULT, NULL);
19140Sstevel@tonic-gate 	cv_init(&cm_entry->x_dis_cv, NULL, CV_DEFAULT, NULL);
19150Sstevel@tonic-gate 
19160Sstevel@tonic-gate 	/*
19170Sstevel@tonic-gate 	 * Note that we add this partially initialized entry to the
19180Sstevel@tonic-gate 	 * connection list. This is so that we don't have connections to
19190Sstevel@tonic-gate 	 * the same server.
19200Sstevel@tonic-gate 	 *
19210Sstevel@tonic-gate 	 * Note that x_src is not initialized at this point. This is because
19220Sstevel@tonic-gate 	 * retryaddr might be NULL in which case x_src is whatever
19230Sstevel@tonic-gate 	 * t_kbind/bindresvport gives us. If another thread wants a
19240Sstevel@tonic-gate 	 * connection to the same server, seemingly we have an issue, but we
19250Sstevel@tonic-gate 	 * don't. If the other thread comes in with retryaddr == NULL, then it
19260Sstevel@tonic-gate 	 * will never look at x_src, and it will end up waiting in
19270Sstevel@tonic-gate 	 * connmgr_cwait() for the first thread to finish the connection
19280Sstevel@tonic-gate 	 * attempt. If the other thread comes in with retryaddr != NULL, then
19290Sstevel@tonic-gate 	 * that means there was a request sent on a connection, in which case
19300Sstevel@tonic-gate 	 * the the connection should already exist. Thus the first thread
19310Sstevel@tonic-gate 	 * never gets here ... it finds the connection it its server in the
19320Sstevel@tonic-gate 	 * connection list.
19330Sstevel@tonic-gate 	 *
19340Sstevel@tonic-gate 	 * But even if theory is wrong, in the retryaddr != NULL case, the 2nd
19350Sstevel@tonic-gate 	 * thread will skip us because x_src.len == 0.
19360Sstevel@tonic-gate 	 */
19370Sstevel@tonic-gate 	cm_entry->x_next = cm_hd;
19380Sstevel@tonic-gate 	cm_hd = cm_entry;
19390Sstevel@tonic-gate 	mutex_exit(&connmgr_lock);
19400Sstevel@tonic-gate 
19410Sstevel@tonic-gate 	/*
19420Sstevel@tonic-gate 	 * Either we didn't find an entry to the server of interest, or we
19430Sstevel@tonic-gate 	 * don't have the maximum number of connections to that server -
19440Sstevel@tonic-gate 	 * create a new connection.
19450Sstevel@tonic-gate 	 */
19460Sstevel@tonic-gate 	RPCLOG0(8, "connmgr_get: creating new connection\n");
19470Sstevel@tonic-gate 	rpcerr->re_status = RPC_TLIERROR;
19480Sstevel@tonic-gate 
19490Sstevel@tonic-gate 	i = t_kopen(NULL, device, FREAD|FWRITE|FNDELAY, &tiptr, kcred);
19500Sstevel@tonic-gate 	if (i) {
19510Sstevel@tonic-gate 		RPCLOG(1, "connmgr_get: can't open cots device, error %d\n", i);
19520Sstevel@tonic-gate 		rpcerr->re_errno = i;
19530Sstevel@tonic-gate 		connmgr_cancelconn(cm_entry);
19540Sstevel@tonic-gate 		return (NULL);
19550Sstevel@tonic-gate 	}
19560Sstevel@tonic-gate 	rpc_poptimod(tiptr->fp->f_vnode);
19570Sstevel@tonic-gate 
19580Sstevel@tonic-gate 	if (i = strioctl(tiptr->fp->f_vnode, I_PUSH, (intptr_t)"rpcmod", 0,
19590Sstevel@tonic-gate 			K_TO_K, kcred, &retval)) {
19600Sstevel@tonic-gate 		RPCLOG(1, "connmgr_get: can't push cots module, %d\n", i);
19610Sstevel@tonic-gate 		(void) t_kclose(tiptr, 1);
19620Sstevel@tonic-gate 		rpcerr->re_errno = i;
19630Sstevel@tonic-gate 		connmgr_cancelconn(cm_entry);
19640Sstevel@tonic-gate 		return (NULL);
19650Sstevel@tonic-gate 	}
19660Sstevel@tonic-gate 
19670Sstevel@tonic-gate 	if (i = strioctl(tiptr->fp->f_vnode, RPC_CLIENT, 0, 0, K_TO_K,
19680Sstevel@tonic-gate 		kcred, &retval)) {
19690Sstevel@tonic-gate 		RPCLOG(1, "connmgr_get: can't set client status with cots "
19700Sstevel@tonic-gate 		    "module, %d\n", i);
19710Sstevel@tonic-gate 		(void) t_kclose(tiptr, 1);
19720Sstevel@tonic-gate 		rpcerr->re_errno = i;
19730Sstevel@tonic-gate 		connmgr_cancelconn(cm_entry);
19740Sstevel@tonic-gate 		return (NULL);
19750Sstevel@tonic-gate 	}
19760Sstevel@tonic-gate 
19770Sstevel@tonic-gate 	mutex_enter(&connmgr_lock);
19780Sstevel@tonic-gate 
19790Sstevel@tonic-gate 	wq = tiptr->fp->f_vnode->v_stream->sd_wrq->q_next;
19800Sstevel@tonic-gate 	cm_entry->x_wq = wq;
19810Sstevel@tonic-gate 
19820Sstevel@tonic-gate 	mutex_exit(&connmgr_lock);
19830Sstevel@tonic-gate 
19840Sstevel@tonic-gate 	if (i = strioctl(tiptr->fp->f_vnode, I_PUSH, (intptr_t)"timod", 0,
19850Sstevel@tonic-gate 			K_TO_K, kcred, &retval)) {
19860Sstevel@tonic-gate 		RPCLOG(1, "connmgr_get: can't push timod, %d\n", i);
19870Sstevel@tonic-gate 		(void) t_kclose(tiptr, 1);
19880Sstevel@tonic-gate 		rpcerr->re_errno = i;
19890Sstevel@tonic-gate 		connmgr_cancelconn(cm_entry);
19900Sstevel@tonic-gate 		return (NULL);
19910Sstevel@tonic-gate 	}
19920Sstevel@tonic-gate 
19930Sstevel@tonic-gate 	/*
19940Sstevel@tonic-gate 	 * If the caller has not specified reserved port usage then
19950Sstevel@tonic-gate 	 * take the system default.
19960Sstevel@tonic-gate 	 */
19970Sstevel@tonic-gate 	if (useresvport == -1)
19980Sstevel@tonic-gate 		useresvport = clnt_cots_do_bindresvport;
19990Sstevel@tonic-gate 
20000Sstevel@tonic-gate 	if ((useresvport || retryaddr != NULL) &&
20010Sstevel@tonic-gate 	    (addrfmly == AF_INET || addrfmly == AF_INET6)) {
20020Sstevel@tonic-gate 		bool_t alloc_src = FALSE;
20030Sstevel@tonic-gate 
20040Sstevel@tonic-gate 		if (srcaddr->len != destaddr->len) {
20050Sstevel@tonic-gate 			kmem_free(srcaddr->buf, srcaddr->maxlen);
20060Sstevel@tonic-gate 			srcaddr->buf = kmem_zalloc(destaddr->len, KM_SLEEP);
20070Sstevel@tonic-gate 			srcaddr->maxlen = destaddr->len;
20080Sstevel@tonic-gate 			srcaddr->len = destaddr->len;
20090Sstevel@tonic-gate 			alloc_src = TRUE;
20100Sstevel@tonic-gate 		}
20110Sstevel@tonic-gate 
20120Sstevel@tonic-gate 		if ((i = bindresvport(tiptr, retryaddr, srcaddr, TRUE)) != 0) {
20130Sstevel@tonic-gate 			(void) t_kclose(tiptr, 1);
20140Sstevel@tonic-gate 			RPCLOG(1, "connmgr_get: couldn't bind, retryaddr: "
20150Sstevel@tonic-gate 				"%p\n", (void *)retryaddr);
20160Sstevel@tonic-gate 
20170Sstevel@tonic-gate 			/*
20180Sstevel@tonic-gate 			 * 1225408: If we allocated a source address, then it
20190Sstevel@tonic-gate 			 * is either garbage or all zeroes. In that case
20200Sstevel@tonic-gate 			 * we need to clear srcaddr.
20210Sstevel@tonic-gate 			 */
20220Sstevel@tonic-gate 			if (alloc_src == TRUE) {
20230Sstevel@tonic-gate 				kmem_free(srcaddr->buf, srcaddr->maxlen);
20240Sstevel@tonic-gate 				srcaddr->maxlen = srcaddr->len = 0;
20250Sstevel@tonic-gate 				srcaddr->buf = NULL;
20260Sstevel@tonic-gate 			}
20270Sstevel@tonic-gate 			rpcerr->re_errno = i;
20280Sstevel@tonic-gate 			connmgr_cancelconn(cm_entry);
20290Sstevel@tonic-gate 			return (NULL);
20300Sstevel@tonic-gate 		}
20310Sstevel@tonic-gate 	} else {
20320Sstevel@tonic-gate 		if ((i = t_kbind(tiptr, NULL, NULL)) != 0) {
20330Sstevel@tonic-gate 			RPCLOG(1, "clnt_cots_kcreate: t_kbind: %d\n", i);
20340Sstevel@tonic-gate 			(void) t_kclose(tiptr, 1);
20350Sstevel@tonic-gate 			rpcerr->re_errno = i;
20360Sstevel@tonic-gate 			connmgr_cancelconn(cm_entry);
20370Sstevel@tonic-gate 			return (NULL);
20380Sstevel@tonic-gate 		}
20390Sstevel@tonic-gate 	}
20400Sstevel@tonic-gate 
20410Sstevel@tonic-gate 	{
20420Sstevel@tonic-gate 		/*
20430Sstevel@tonic-gate 		 * Keep the kernel stack lean. Don't move this call
20440Sstevel@tonic-gate 		 * declaration to the top of this function because a
20450Sstevel@tonic-gate 		 * call is declared in connmgr_wrapconnect()
20460Sstevel@tonic-gate 		 */
20470Sstevel@tonic-gate 		calllist_t call;
20480Sstevel@tonic-gate 
20490Sstevel@tonic-gate 		bzero(&call, sizeof (call));
20500Sstevel@tonic-gate 		cv_init(&call.call_cv, NULL, CV_DEFAULT, NULL);
20510Sstevel@tonic-gate 
20520Sstevel@tonic-gate 		/*
20530Sstevel@tonic-gate 		 * This is a bound end-point so don't close it's stream.
20540Sstevel@tonic-gate 		 */
20550Sstevel@tonic-gate 		connected = connmgr_connect(cm_entry, wq, destaddr, addrfmly,
20560Sstevel@tonic-gate 						&call, &tidu_size, FALSE, waitp,
20570Sstevel@tonic-gate 						nosignal);
20580Sstevel@tonic-gate 		*rpcerr = call.call_err;
20590Sstevel@tonic-gate 		cv_destroy(&call.call_cv);
20600Sstevel@tonic-gate 
20610Sstevel@tonic-gate 	}
20620Sstevel@tonic-gate 
20630Sstevel@tonic-gate 	mutex_enter(&connmgr_lock);
20640Sstevel@tonic-gate 
20650Sstevel@tonic-gate 	/*
20660Sstevel@tonic-gate 	 * Set up a transport entry in the connection manager's list.
20670Sstevel@tonic-gate 	 */
20680Sstevel@tonic-gate 	cm_entry->x_src.buf = kmem_zalloc(srcaddr->len, KM_SLEEP);
20690Sstevel@tonic-gate 	bcopy(srcaddr->buf, cm_entry->x_src.buf, srcaddr->len);
20700Sstevel@tonic-gate 	cm_entry->x_src.len = cm_entry->x_src.maxlen = srcaddr->len;
20710Sstevel@tonic-gate 
20720Sstevel@tonic-gate 	cm_entry->x_tiptr = tiptr;
20730Sstevel@tonic-gate 	cm_entry->x_time = lbolt;
20740Sstevel@tonic-gate 
20750Sstevel@tonic-gate 	if (tiptr->tp_info.servtype == T_COTS_ORD)
20760Sstevel@tonic-gate 		cm_entry->x_ordrel = TRUE;
20770Sstevel@tonic-gate 	else
20780Sstevel@tonic-gate 		cm_entry->x_ordrel = FALSE;
20790Sstevel@tonic-gate 
20800Sstevel@tonic-gate 	cm_entry->x_tidu_size = tidu_size;
20810Sstevel@tonic-gate 
20820Sstevel@tonic-gate 	if (cm_entry->x_early_disc)
20830Sstevel@tonic-gate 		cm_entry->x_connected = FALSE;
20840Sstevel@tonic-gate 	else
20850Sstevel@tonic-gate 		cm_entry->x_connected = connected;
20860Sstevel@tonic-gate 
20870Sstevel@tonic-gate 	/*
20880Sstevel@tonic-gate 	 * There could be a discrepancy here such that
20890Sstevel@tonic-gate 	 * x_early_disc is TRUE yet connected is TRUE as well
20900Sstevel@tonic-gate 	 * and the connection is actually connected. In that case
20910Sstevel@tonic-gate 	 * lets be conservative and declare the connection as not
20920Sstevel@tonic-gate 	 * connected.
20930Sstevel@tonic-gate 	 */
20940Sstevel@tonic-gate 	cm_entry->x_early_disc = FALSE;
20950Sstevel@tonic-gate 	cm_entry->x_needdis = (cm_entry->x_connected == FALSE);
20960Sstevel@tonic-gate 	cm_entry->x_ctime = lbolt;
20970Sstevel@tonic-gate 
20980Sstevel@tonic-gate 	/*
20990Sstevel@tonic-gate 	 * Notify any threads waiting that the connection attempt is done.
21000Sstevel@tonic-gate 	 */
21010Sstevel@tonic-gate 	cm_entry->x_thread = FALSE;
21020Sstevel@tonic-gate 	cv_broadcast(&cm_entry->x_conn_cv);
21030Sstevel@tonic-gate 
21040Sstevel@tonic-gate 	mutex_exit(&connmgr_lock);
21050Sstevel@tonic-gate 
21060Sstevel@tonic-gate 	if (cm_entry->x_connected == FALSE) {
21070Sstevel@tonic-gate 		connmgr_release(cm_entry);
21080Sstevel@tonic-gate 		return (NULL);
21090Sstevel@tonic-gate 	}
21100Sstevel@tonic-gate 	return (cm_entry);
21110Sstevel@tonic-gate }
21120Sstevel@tonic-gate 
21130Sstevel@tonic-gate /*
21140Sstevel@tonic-gate  * Keep the cm_xprt entry on the connecton list when making a connection. This
21150Sstevel@tonic-gate  * is to prevent multiple connections to a slow server from appearing.
21160Sstevel@tonic-gate  * We use the bit field x_thread to tell if a thread is doing a connection
21170Sstevel@tonic-gate  * which keeps other interested threads from messing with connection.
21180Sstevel@tonic-gate  * Those other threads just wait if x_thread is set.
21190Sstevel@tonic-gate  *
21200Sstevel@tonic-gate  * If x_thread is not set, then we do the actual work of connecting via
21210Sstevel@tonic-gate  * connmgr_connect().
21220Sstevel@tonic-gate  *
21230Sstevel@tonic-gate  * mutex convention: called with connmgr_lock held, returns with it released.
21240Sstevel@tonic-gate  */
21250Sstevel@tonic-gate static struct cm_xprt *
21260Sstevel@tonic-gate connmgr_wrapconnect(
21270Sstevel@tonic-gate 	struct cm_xprt	*cm_entry,
21280Sstevel@tonic-gate 	const struct timeval	*waitp,
21290Sstevel@tonic-gate 	struct netbuf	*destaddr,
21300Sstevel@tonic-gate 	int		addrfmly,
21310Sstevel@tonic-gate 	struct netbuf	*srcaddr,
21320Sstevel@tonic-gate 	struct rpc_err	*rpcerr,
21330Sstevel@tonic-gate 	bool_t		reconnect,
21340Sstevel@tonic-gate 	bool_t		nosignal)
21350Sstevel@tonic-gate {
21360Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&connmgr_lock));
21370Sstevel@tonic-gate 	/*
21380Sstevel@tonic-gate 	 * Hold this entry as we are about to drop connmgr_lock.
21390Sstevel@tonic-gate 	 */
21400Sstevel@tonic-gate 	CONN_HOLD(cm_entry);
21410Sstevel@tonic-gate 
21420Sstevel@tonic-gate 	/*
21430Sstevel@tonic-gate 	 * If there is a thread already making a connection for us, then
21440Sstevel@tonic-gate 	 * wait for it to complete the connection.
21450Sstevel@tonic-gate 	 */
21460Sstevel@tonic-gate 	if (cm_entry->x_thread == TRUE) {
21470Sstevel@tonic-gate 		rpcerr->re_status = connmgr_cwait(cm_entry, waitp, nosignal);
21480Sstevel@tonic-gate 
21490Sstevel@tonic-gate 		if (rpcerr->re_status != RPC_SUCCESS) {
21500Sstevel@tonic-gate 			mutex_exit(&connmgr_lock);
21510Sstevel@tonic-gate 			connmgr_release(cm_entry);
21520Sstevel@tonic-gate 			return (NULL);
21530Sstevel@tonic-gate 		}
21540Sstevel@tonic-gate 	} else {
21550Sstevel@tonic-gate 		bool_t connected;
21560Sstevel@tonic-gate 		calllist_t call;
21570Sstevel@tonic-gate 
21580Sstevel@tonic-gate 		cm_entry->x_thread = TRUE;
21590Sstevel@tonic-gate 
21600Sstevel@tonic-gate 		while (cm_entry->x_needrel == TRUE) {
21610Sstevel@tonic-gate 			cm_entry->x_needrel = FALSE;
21620Sstevel@tonic-gate 
21630Sstevel@tonic-gate 			connmgr_sndrel(cm_entry);
21640Sstevel@tonic-gate 			delay(drv_usectohz(1000000));
21650Sstevel@tonic-gate 
21660Sstevel@tonic-gate 			mutex_enter(&connmgr_lock);
21670Sstevel@tonic-gate 		}
21680Sstevel@tonic-gate 
21690Sstevel@tonic-gate 		/*
21700Sstevel@tonic-gate 		 * If we need to send a T_DISCON_REQ, send one.
21710Sstevel@tonic-gate 		 */
21720Sstevel@tonic-gate 		connmgr_dis_and_wait(cm_entry);
21730Sstevel@tonic-gate 
21740Sstevel@tonic-gate 		mutex_exit(&connmgr_lock);
21750Sstevel@tonic-gate 
21760Sstevel@tonic-gate 		bzero(&call, sizeof (call));
21770Sstevel@tonic-gate 		cv_init(&call.call_cv, NULL, CV_DEFAULT, NULL);
21780Sstevel@tonic-gate 
21790Sstevel@tonic-gate 		connected = connmgr_connect(cm_entry, cm_entry->x_wq,
21800Sstevel@tonic-gate 					    destaddr, addrfmly, &call,
21810Sstevel@tonic-gate 					    &cm_entry->x_tidu_size,
21820Sstevel@tonic-gate 					    reconnect, waitp, nosignal);
21830Sstevel@tonic-gate 
21840Sstevel@tonic-gate 		*rpcerr = call.call_err;
21850Sstevel@tonic-gate 		cv_destroy(&call.call_cv);
21860Sstevel@tonic-gate 
21870Sstevel@tonic-gate 		mutex_enter(&connmgr_lock);
21880Sstevel@tonic-gate 
21890Sstevel@tonic-gate 
21900Sstevel@tonic-gate 		if (cm_entry->x_early_disc)
21910Sstevel@tonic-gate 			cm_entry->x_connected = FALSE;
21920Sstevel@tonic-gate 		else
21930Sstevel@tonic-gate 			cm_entry->x_connected = connected;
21940Sstevel@tonic-gate 
21950Sstevel@tonic-gate 		/*
21960Sstevel@tonic-gate 		 * There could be a discrepancy here such that
21970Sstevel@tonic-gate 		 * x_early_disc is TRUE yet connected is TRUE as well
21980Sstevel@tonic-gate 		 * and the connection is actually connected. In that case
21990Sstevel@tonic-gate 		 * lets be conservative and declare the connection as not
22000Sstevel@tonic-gate 		 * connected.
22010Sstevel@tonic-gate 		 */
22020Sstevel@tonic-gate 
22030Sstevel@tonic-gate 		cm_entry->x_early_disc = FALSE;
22040Sstevel@tonic-gate 		cm_entry->x_needdis = (cm_entry->x_connected == FALSE);
22050Sstevel@tonic-gate 
22060Sstevel@tonic-gate 
22070Sstevel@tonic-gate 		/*
22080Sstevel@tonic-gate 		 * connmgr_connect() may have given up before the connection
22090Sstevel@tonic-gate 		 * actually timed out. So ensure that before the next
22100Sstevel@tonic-gate 		 * connection attempt we do a disconnect.
22110Sstevel@tonic-gate 		 */
22120Sstevel@tonic-gate 		cm_entry->x_ctime = lbolt;
22130Sstevel@tonic-gate 		cm_entry->x_thread = FALSE;
22140Sstevel@tonic-gate 
22150Sstevel@tonic-gate 		cv_broadcast(&cm_entry->x_conn_cv);
22160Sstevel@tonic-gate 
22170Sstevel@tonic-gate 		if (cm_entry->x_connected == FALSE) {
22180Sstevel@tonic-gate 			mutex_exit(&connmgr_lock);
22190Sstevel@tonic-gate 			connmgr_release(cm_entry);
22200Sstevel@tonic-gate 			return (NULL);
22210Sstevel@tonic-gate 		}
22220Sstevel@tonic-gate 	}
22230Sstevel@tonic-gate 
22240Sstevel@tonic-gate 	if (srcaddr != NULL) {
22250Sstevel@tonic-gate 		/*
22260Sstevel@tonic-gate 		 * Copy into the handle the
22270Sstevel@tonic-gate 		 * source address of the
22280Sstevel@tonic-gate 		 * connection, which we will use
22290Sstevel@tonic-gate 		 * in case of a later retry.
22300Sstevel@tonic-gate 		 */
22310Sstevel@tonic-gate 		if (srcaddr->len != cm_entry->x_src.len) {
22320Sstevel@tonic-gate 			if (srcaddr->maxlen > 0)
22330Sstevel@tonic-gate 				kmem_free(srcaddr->buf, srcaddr->maxlen);
22340Sstevel@tonic-gate 			srcaddr->buf = kmem_zalloc(cm_entry->x_src.len,
22350Sstevel@tonic-gate 			    KM_SLEEP);
22360Sstevel@tonic-gate 			srcaddr->maxlen = srcaddr->len =
22370Sstevel@tonic-gate 			    cm_entry->x_src.len;
22380Sstevel@tonic-gate 		}
22390Sstevel@tonic-gate 		bcopy(cm_entry->x_src.buf, srcaddr->buf, srcaddr->len);
22400Sstevel@tonic-gate 	}
22410Sstevel@tonic-gate 	cm_entry->x_time = lbolt;
22420Sstevel@tonic-gate 	mutex_exit(&connmgr_lock);
22430Sstevel@tonic-gate 	return (cm_entry);
22440Sstevel@tonic-gate }
22450Sstevel@tonic-gate 
22460Sstevel@tonic-gate /*
22470Sstevel@tonic-gate  * If we need to send a T_DISCON_REQ, send one.
22480Sstevel@tonic-gate  */
22490Sstevel@tonic-gate static void
22500Sstevel@tonic-gate connmgr_dis_and_wait(struct cm_xprt *cm_entry)
22510Sstevel@tonic-gate {
22520Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&connmgr_lock));
22530Sstevel@tonic-gate 	for (;;) {
22540Sstevel@tonic-gate 		while (cm_entry->x_needdis == TRUE) {
22550Sstevel@tonic-gate 			RPCLOG(8, "connmgr_dis_and_wait: need "
22560Sstevel@tonic-gate 				"T_DISCON_REQ for connection 0x%p\n",
22570Sstevel@tonic-gate 				(void *)cm_entry);
22580Sstevel@tonic-gate 			cm_entry->x_needdis = FALSE;
22590Sstevel@tonic-gate 			cm_entry->x_waitdis = TRUE;
22600Sstevel@tonic-gate 
22610Sstevel@tonic-gate 			connmgr_snddis(cm_entry);
22620Sstevel@tonic-gate 
22630Sstevel@tonic-gate 			mutex_enter(&connmgr_lock);
22640Sstevel@tonic-gate 		}
22650Sstevel@tonic-gate 
22660Sstevel@tonic-gate 		if (cm_entry->x_waitdis == TRUE) {
22670Sstevel@tonic-gate 			clock_t curlbolt;
22680Sstevel@tonic-gate 			clock_t timout;
22690Sstevel@tonic-gate 
22700Sstevel@tonic-gate 			RPCLOG(8, "connmgr_dis_and_wait waiting for "
22710Sstevel@tonic-gate 				"T_DISCON_REQ's ACK for connection %p\n",
22720Sstevel@tonic-gate 				(void *)cm_entry);
22730Sstevel@tonic-gate 			curlbolt = ddi_get_lbolt();
22740Sstevel@tonic-gate 
22750Sstevel@tonic-gate 			timout = clnt_cots_min_conntout *
22760Sstevel@tonic-gate 				drv_usectohz(1000000) + curlbolt;
22770Sstevel@tonic-gate 
22780Sstevel@tonic-gate 			/*
22790Sstevel@tonic-gate 			 * The TPI spec says that the T_DISCON_REQ
22800Sstevel@tonic-gate 			 * will get acknowledged, but in practice
22810Sstevel@tonic-gate 			 * the ACK may never get sent. So don't
22820Sstevel@tonic-gate 			 * block forever.
22830Sstevel@tonic-gate 			 */
22840Sstevel@tonic-gate 			(void) cv_timedwait(&cm_entry->x_dis_cv,
22850Sstevel@tonic-gate 					    &connmgr_lock, timout);
22860Sstevel@tonic-gate 		}
22870Sstevel@tonic-gate 		/*
22880Sstevel@tonic-gate 		 * If we got the ACK, break. If we didn't,
22890Sstevel@tonic-gate 		 * then send another T_DISCON_REQ.
22900Sstevel@tonic-gate 		 */
22910Sstevel@tonic-gate 		if (cm_entry->x_waitdis == FALSE) {
22920Sstevel@tonic-gate 			break;
22930Sstevel@tonic-gate 		} else {
22940Sstevel@tonic-gate 			RPCLOG(8, "connmgr_dis_and_wait: did"
22950Sstevel@tonic-gate 				"not get T_DISCON_REQ's ACK for "
22960Sstevel@tonic-gate 				"connection  %p\n", (void *)cm_entry);
22970Sstevel@tonic-gate 			cm_entry->x_needdis = TRUE;
22980Sstevel@tonic-gate 		}
22990Sstevel@tonic-gate 	}
23000Sstevel@tonic-gate }
23010Sstevel@tonic-gate 
23020Sstevel@tonic-gate static void
23030Sstevel@tonic-gate connmgr_cancelconn(struct cm_xprt *cm_entry)
23040Sstevel@tonic-gate {
23050Sstevel@tonic-gate 	/*
23060Sstevel@tonic-gate 	 * Mark the connection table entry as dead; the next thread that
23070Sstevel@tonic-gate 	 * goes through connmgr_release() will notice this and deal with it.
23080Sstevel@tonic-gate 	 */
23090Sstevel@tonic-gate 	mutex_enter(&connmgr_lock);
23100Sstevel@tonic-gate 	cm_entry->x_dead = TRUE;
23110Sstevel@tonic-gate 
23120Sstevel@tonic-gate 	/*
23130Sstevel@tonic-gate 	 * Notify any threads waiting for the connection that it isn't
23140Sstevel@tonic-gate 	 * going to happen.
23150Sstevel@tonic-gate 	 */
23160Sstevel@tonic-gate 	cm_entry->x_thread = FALSE;
23170Sstevel@tonic-gate 	cv_broadcast(&cm_entry->x_conn_cv);
23180Sstevel@tonic-gate 	mutex_exit(&connmgr_lock);
23190Sstevel@tonic-gate 
23200Sstevel@tonic-gate 	connmgr_release(cm_entry);
23210Sstevel@tonic-gate }
23220Sstevel@tonic-gate 
23230Sstevel@tonic-gate static void
23240Sstevel@tonic-gate connmgr_close(struct cm_xprt *cm_entry)
23250Sstevel@tonic-gate {
23260Sstevel@tonic-gate 	mutex_enter(&cm_entry->x_lock);
23270Sstevel@tonic-gate 	while (cm_entry->x_ref != 0) {
23280Sstevel@tonic-gate 		/*
23290Sstevel@tonic-gate 		 * Must be a noninterruptible wait.
23300Sstevel@tonic-gate 		 */
23310Sstevel@tonic-gate 		cv_wait(&cm_entry->x_cv, &cm_entry->x_lock);
23320Sstevel@tonic-gate 	}
23330Sstevel@tonic-gate 
23340Sstevel@tonic-gate 	if (cm_entry->x_tiptr != NULL)
23350Sstevel@tonic-gate 		(void) t_kclose(cm_entry->x_tiptr, 1);
23360Sstevel@tonic-gate 
23370Sstevel@tonic-gate 	mutex_exit(&cm_entry->x_lock);
23380Sstevel@tonic-gate 	if (cm_entry->x_ksp != NULL) {
23390Sstevel@tonic-gate 		mutex_enter(&connmgr_lock);
23400Sstevel@tonic-gate 		cm_entry->x_ksp->ks_private = NULL;
23410Sstevel@tonic-gate 		mutex_exit(&connmgr_lock);
23420Sstevel@tonic-gate 
23430Sstevel@tonic-gate 		/*
23440Sstevel@tonic-gate 		 * Must free the buffer we allocated for the
23450Sstevel@tonic-gate 		 * server address in the update function
23460Sstevel@tonic-gate 		 */
23470Sstevel@tonic-gate 		if (((struct cm_kstat_xprt *)(cm_entry->x_ksp->ks_data))->
23480Sstevel@tonic-gate 		    x_server.value.string.addr.ptr != NULL)
23490Sstevel@tonic-gate 			kmem_free(((struct cm_kstat_xprt *)(cm_entry->x_ksp->
23500Sstevel@tonic-gate 			    ks_data))->x_server.value.string.addr.ptr,
23510Sstevel@tonic-gate 				    INET6_ADDRSTRLEN);
23520Sstevel@tonic-gate 		kmem_free(cm_entry->x_ksp->ks_data,
23530Sstevel@tonic-gate 			    cm_entry->x_ksp->ks_data_size);
23540Sstevel@tonic-gate 		kstat_delete(cm_entry->x_ksp);
23550Sstevel@tonic-gate 	}
23560Sstevel@tonic-gate 
23570Sstevel@tonic-gate 	mutex_destroy(&cm_entry->x_lock);
23580Sstevel@tonic-gate 	cv_destroy(&cm_entry->x_cv);
23590Sstevel@tonic-gate 	cv_destroy(&cm_entry->x_conn_cv);
23600Sstevel@tonic-gate 	cv_destroy(&cm_entry->x_dis_cv);
23610Sstevel@tonic-gate 
23620Sstevel@tonic-gate 	if (cm_entry->x_server.buf != NULL)
23630Sstevel@tonic-gate 		kmem_free(cm_entry->x_server.buf, cm_entry->x_server.maxlen);
23640Sstevel@tonic-gate 	if (cm_entry->x_src.buf != NULL)
23650Sstevel@tonic-gate 		kmem_free(cm_entry->x_src.buf, cm_entry->x_src.maxlen);
23660Sstevel@tonic-gate 	kmem_free(cm_entry, sizeof (struct cm_xprt));
23670Sstevel@tonic-gate }
23680Sstevel@tonic-gate 
23690Sstevel@tonic-gate /*
23700Sstevel@tonic-gate  * Called by KRPC after sending the call message to release the connection
23710Sstevel@tonic-gate  * it was using.
23720Sstevel@tonic-gate  */
23730Sstevel@tonic-gate static void
23740Sstevel@tonic-gate connmgr_release(struct cm_xprt *cm_entry)
23750Sstevel@tonic-gate {
23760Sstevel@tonic-gate 	mutex_enter(&cm_entry->x_lock);
23770Sstevel@tonic-gate 	cm_entry->x_ref--;
23780Sstevel@tonic-gate 	if (cm_entry->x_ref == 0)
23790Sstevel@tonic-gate 		cv_signal(&cm_entry->x_cv);
23800Sstevel@tonic-gate 	mutex_exit(&cm_entry->x_lock);
23810Sstevel@tonic-gate }
23820Sstevel@tonic-gate 
23830Sstevel@tonic-gate /*
23840Sstevel@tonic-gate  * Given an open stream, connect to the remote.  Returns true if connected,
23850Sstevel@tonic-gate  * false otherwise.
23860Sstevel@tonic-gate  */
23870Sstevel@tonic-gate static bool_t
23880Sstevel@tonic-gate connmgr_connect(
23890Sstevel@tonic-gate 	struct cm_xprt		*cm_entry,
23900Sstevel@tonic-gate 	queue_t			*wq,
23910Sstevel@tonic-gate 	struct netbuf		*addr,
23920Sstevel@tonic-gate 	int			addrfmly,
23930Sstevel@tonic-gate 	calllist_t 		*e,
23940Sstevel@tonic-gate 	int 			*tidu_ptr,
23950Sstevel@tonic-gate 	bool_t 			reconnect,
23960Sstevel@tonic-gate 	const struct timeval 	*waitp,
23970Sstevel@tonic-gate 	bool_t 			nosignal)
23980Sstevel@tonic-gate {
23990Sstevel@tonic-gate 	mblk_t *mp;
24000Sstevel@tonic-gate 	struct T_conn_req *tcr;
24010Sstevel@tonic-gate 	struct T_info_ack *tinfo;
24020Sstevel@tonic-gate 	int interrupted, error;
24030Sstevel@tonic-gate 	int tidu_size, kstat_instance;
24040Sstevel@tonic-gate 
24050Sstevel@tonic-gate 	/* if it's a reconnect, flush any lingering data messages */
24060Sstevel@tonic-gate 	if (reconnect)
24070Sstevel@tonic-gate 		(void) putctl1(wq, M_FLUSH, FLUSHRW);
24080Sstevel@tonic-gate 
24090Sstevel@tonic-gate 	mp = allocb(sizeof (*tcr) + addr->len, BPRI_LO);
24100Sstevel@tonic-gate 	if (mp == NULL) {
24110Sstevel@tonic-gate 		/*
24120Sstevel@tonic-gate 		 * This is unfortunate, but we need to look up the stats for
24130Sstevel@tonic-gate 		 * this zone to increment the "memory allocation failed"
24140Sstevel@tonic-gate 		 * counter.  curproc->p_zone is safe since we're initiating a
24150Sstevel@tonic-gate 		 * connection and not in some strange streams context.
24160Sstevel@tonic-gate 		 */
24170Sstevel@tonic-gate 		struct rpcstat *rpcstat;
24180Sstevel@tonic-gate 
24190Sstevel@tonic-gate 		rpcstat = zone_getspecific(rpcstat_zone_key, curproc->p_zone);
24200Sstevel@tonic-gate 		ASSERT(rpcstat != NULL);
24210Sstevel@tonic-gate 
24220Sstevel@tonic-gate 		RPCLOG0(1, "connmgr_connect: cannot alloc mp for "
24230Sstevel@tonic-gate 		    "sending conn request\n");
24240Sstevel@tonic-gate 		COTSRCSTAT_INCR(rpcstat->rpc_cots_client, rcnomem);
24250Sstevel@tonic-gate 		e->call_status = RPC_SYSTEMERROR;
24260Sstevel@tonic-gate 		e->call_reason = ENOSR;
24270Sstevel@tonic-gate 		return (FALSE);
24280Sstevel@tonic-gate 	}
24290Sstevel@tonic-gate 
24300Sstevel@tonic-gate 	mp->b_datap->db_type = M_PROTO;
24310Sstevel@tonic-gate 	tcr = (struct T_conn_req *)mp->b_rptr;
24320Sstevel@tonic-gate 	bzero(tcr, sizeof (*tcr));
24330Sstevel@tonic-gate 	tcr->PRIM_type = T_CONN_REQ;
24340Sstevel@tonic-gate 	tcr->DEST_length = addr->len;
24350Sstevel@tonic-gate 	tcr->DEST_offset = sizeof (struct T_conn_req);
24360Sstevel@tonic-gate 	mp->b_wptr = mp->b_rptr + sizeof (*tcr);
24370Sstevel@tonic-gate 
24380Sstevel@tonic-gate 	bcopy(addr->buf, mp->b_wptr, tcr->DEST_length);
24390Sstevel@tonic-gate 	mp->b_wptr += tcr->DEST_length;
24400Sstevel@tonic-gate 
24410Sstevel@tonic-gate 	RPCLOG(8, "connmgr_connect: sending conn request on queue "
24420Sstevel@tonic-gate 	    "%p", (void *)wq);
24430Sstevel@tonic-gate 	RPCLOG(8, " call %p\n", (void *)wq);
24440Sstevel@tonic-gate 	/*
24450Sstevel@tonic-gate 	 * We use the entry in the handle that is normally used for
24460Sstevel@tonic-gate 	 * waiting for RPC replies to wait for the connection accept.
24470Sstevel@tonic-gate 	 */
24480Sstevel@tonic-gate 	clnt_dispatch_send(wq, mp, e, 0, 0);
24490Sstevel@tonic-gate 
24500Sstevel@tonic-gate 	mutex_enter(&clnt_pending_lock);
24510Sstevel@tonic-gate 
24520Sstevel@tonic-gate 	/*
24530Sstevel@tonic-gate 	 * We wait for the transport connection to be made, or an
24540Sstevel@tonic-gate 	 * indication that it could not be made.
24550Sstevel@tonic-gate 	 */
24560Sstevel@tonic-gate 	interrupted = 0;
24570Sstevel@tonic-gate 
24580Sstevel@tonic-gate 	/*
24590Sstevel@tonic-gate 	 * waitforack should have been called with T_OK_ACK, but the
24600Sstevel@tonic-gate 	 * present implementation needs to be passed T_INFO_ACK to
24610Sstevel@tonic-gate 	 * work correctly.
24620Sstevel@tonic-gate 	 */
24630Sstevel@tonic-gate 	error = waitforack(e, T_INFO_ACK, waitp, nosignal);
24640Sstevel@tonic-gate 	if (error == EINTR)
24650Sstevel@tonic-gate 		interrupted = 1;
24660Sstevel@tonic-gate 	if (zone_status_get(curproc->p_zone) >= ZONE_IS_EMPTY) {
24670Sstevel@tonic-gate 		/*
24680Sstevel@tonic-gate 		 * No time to lose; we essentially have been signaled to
24690Sstevel@tonic-gate 		 * quit.
24700Sstevel@tonic-gate 		 */
24710Sstevel@tonic-gate 		interrupted = 1;
24720Sstevel@tonic-gate 	}
24730Sstevel@tonic-gate #ifdef RPCDEBUG
24740Sstevel@tonic-gate 	if (error == ETIME)
24750Sstevel@tonic-gate 		RPCLOG0(8, "connmgr_connect: giving up "
24760Sstevel@tonic-gate 		    "on connection attempt; "
24770Sstevel@tonic-gate 		    "clnt_dispatch notifyconn "
24780Sstevel@tonic-gate 		    "diagnostic 'no one waiting for "
24790Sstevel@tonic-gate 		    "connection' should not be "
24800Sstevel@tonic-gate 		    "unexpected\n");
24810Sstevel@tonic-gate #endif
24820Sstevel@tonic-gate 	if (e->call_prev)
24830Sstevel@tonic-gate 		e->call_prev->call_next = e->call_next;
24840Sstevel@tonic-gate 	else
24850Sstevel@tonic-gate 		clnt_pending = e->call_next;
24860Sstevel@tonic-gate 	if (e->call_next)
24870Sstevel@tonic-gate 		e->call_next->call_prev = e->call_prev;
24880Sstevel@tonic-gate 	mutex_exit(&clnt_pending_lock);
24890Sstevel@tonic-gate 
24900Sstevel@tonic-gate 	if (e->call_status != RPC_SUCCESS || error != 0) {
24910Sstevel@tonic-gate 		if (interrupted)
24920Sstevel@tonic-gate 			e->call_status = RPC_INTR;
24930Sstevel@tonic-gate 		else if (error == ETIME)
24940Sstevel@tonic-gate 			e->call_status = RPC_TIMEDOUT;
24950Sstevel@tonic-gate 		else if (error == EPROTO)
24960Sstevel@tonic-gate 			e->call_status = RPC_SYSTEMERROR;
24970Sstevel@tonic-gate 
24980Sstevel@tonic-gate 		RPCLOG(8, "connmgr_connect: can't connect, status: "
24990Sstevel@tonic-gate 		    "%s\n", clnt_sperrno(e->call_status));
25000Sstevel@tonic-gate 
25010Sstevel@tonic-gate 		if (e->call_reply) {
25020Sstevel@tonic-gate 			freemsg(e->call_reply);
25030Sstevel@tonic-gate 			e->call_reply = NULL;
25040Sstevel@tonic-gate 		}
25050Sstevel@tonic-gate 
25060Sstevel@tonic-gate 		return (FALSE);
25070Sstevel@tonic-gate 	}
25080Sstevel@tonic-gate 	/*
25090Sstevel@tonic-gate 	 * The result of the "connection accept" is a T_info_ack
25100Sstevel@tonic-gate 	 * in the call_reply field.
25110Sstevel@tonic-gate 	 */
25120Sstevel@tonic-gate 	ASSERT(e->call_reply != NULL);
25130Sstevel@tonic-gate 	mp = e->call_reply;
25140Sstevel@tonic-gate 	e->call_reply = NULL;
25150Sstevel@tonic-gate 	tinfo = (struct T_info_ack *)mp->b_rptr;
25160Sstevel@tonic-gate 
25170Sstevel@tonic-gate 	tidu_size = tinfo->TIDU_size;
25180Sstevel@tonic-gate 	tidu_size -= (tidu_size % BYTES_PER_XDR_UNIT);
25190Sstevel@tonic-gate 	if (tidu_size > COTS_DEFAULT_ALLOCSIZE || (tidu_size <= 0))
25200Sstevel@tonic-gate 		tidu_size = COTS_DEFAULT_ALLOCSIZE;
25210Sstevel@tonic-gate 	*tidu_ptr = tidu_size;
25220Sstevel@tonic-gate 
25230Sstevel@tonic-gate 	freemsg(mp);
25240Sstevel@tonic-gate 
25250Sstevel@tonic-gate 	/*
25260Sstevel@tonic-gate 	 * Set up the pertinent options.  NODELAY is so the transport doesn't
25270Sstevel@tonic-gate 	 * buffer up RPC messages on either end.  This may not be valid for
25280Sstevel@tonic-gate 	 * all transports.  Failure to set this option is not cause to
25290Sstevel@tonic-gate 	 * bail out so we return success anyway.  Note that lack of NODELAY
25300Sstevel@tonic-gate 	 * or some other way to flush the message on both ends will cause
25310Sstevel@tonic-gate 	 * lots of retries and terrible performance.
25320Sstevel@tonic-gate 	 */
25330Sstevel@tonic-gate 	if (addrfmly == AF_INET || addrfmly == AF_INET6) {
25340Sstevel@tonic-gate 		(void) connmgr_setopt(wq, IPPROTO_TCP, TCP_NODELAY, e);
25350Sstevel@tonic-gate 		if (e->call_status == RPC_XPRTFAILED)
25360Sstevel@tonic-gate 			return (FALSE);
25370Sstevel@tonic-gate 	}
25380Sstevel@tonic-gate 
25390Sstevel@tonic-gate 	/*
25400Sstevel@tonic-gate 	 * Since we have a connection, we now need to figure out if
25410Sstevel@tonic-gate 	 * we need to create a kstat. If x_ksp is not NULL then we
25420Sstevel@tonic-gate 	 * are reusing a connection and so we do not need to create
25430Sstevel@tonic-gate 	 * another kstat -- lets just return.
25440Sstevel@tonic-gate 	 */
25450Sstevel@tonic-gate 	if (cm_entry->x_ksp != NULL)
25460Sstevel@tonic-gate 		return (TRUE);
25470Sstevel@tonic-gate 
25480Sstevel@tonic-gate 	/*
25490Sstevel@tonic-gate 	 * We need to increment rpc_kstat_instance atomically to prevent
25500Sstevel@tonic-gate 	 * two kstats being created with the same instance.
25510Sstevel@tonic-gate 	 */
25520Sstevel@tonic-gate 	kstat_instance = atomic_add_32_nv((uint32_t *)&rpc_kstat_instance, 1);
25530Sstevel@tonic-gate 
25540Sstevel@tonic-gate 	if ((cm_entry->x_ksp = kstat_create_zone("unix", kstat_instance,
25550Sstevel@tonic-gate 	    "rpc_cots_connections", "rpc", KSTAT_TYPE_NAMED,
25560Sstevel@tonic-gate 	    (uint_t)(sizeof (cm_kstat_xprt_t) / sizeof (kstat_named_t)),
25570Sstevel@tonic-gate 	    KSTAT_FLAG_VIRTUAL, cm_entry->x_zoneid)) == NULL) {
25580Sstevel@tonic-gate 		return (TRUE);
25590Sstevel@tonic-gate 	    }
25600Sstevel@tonic-gate 
25610Sstevel@tonic-gate 	cm_entry->x_ksp->ks_lock = &connmgr_lock;
25620Sstevel@tonic-gate 	cm_entry->x_ksp->ks_private = cm_entry;
25630Sstevel@tonic-gate 	cm_entry->x_ksp->ks_data_size = ((INET6_ADDRSTRLEN * sizeof (char))
25640Sstevel@tonic-gate 					    + sizeof (cm_kstat_template));
25650Sstevel@tonic-gate 	cm_entry->x_ksp->ks_data = kmem_alloc(cm_entry->x_ksp->ks_data_size,
25660Sstevel@tonic-gate 					    KM_SLEEP);
25670Sstevel@tonic-gate 	bcopy(&cm_kstat_template, cm_entry->x_ksp->ks_data,
25680Sstevel@tonic-gate 	    cm_entry->x_ksp->ks_data_size);
25690Sstevel@tonic-gate 	((struct cm_kstat_xprt *)(cm_entry->x_ksp->ks_data))->
25700Sstevel@tonic-gate 		    x_server.value.string.addr.ptr =
25710Sstevel@tonic-gate 		    kmem_alloc(INET6_ADDRSTRLEN, KM_SLEEP);
25720Sstevel@tonic-gate 
25730Sstevel@tonic-gate 	cm_entry->x_ksp->ks_update = conn_kstat_update;
25740Sstevel@tonic-gate 	kstat_install(cm_entry->x_ksp);
25750Sstevel@tonic-gate 	return (TRUE);
25760Sstevel@tonic-gate }
25770Sstevel@tonic-gate 
25780Sstevel@tonic-gate /*
25790Sstevel@tonic-gate  * Called by connmgr_connect to set an option on the new stream.
25800Sstevel@tonic-gate  */
25810Sstevel@tonic-gate static bool_t
25820Sstevel@tonic-gate connmgr_setopt(queue_t *wq, int level, int name, calllist_t *e)
25830Sstevel@tonic-gate {
25840Sstevel@tonic-gate 	mblk_t *mp;
25850Sstevel@tonic-gate 	struct opthdr *opt;
25860Sstevel@tonic-gate 	struct T_optmgmt_req *tor;
25870Sstevel@tonic-gate 	struct timeval waitp;
25880Sstevel@tonic-gate 	int error;
25890Sstevel@tonic-gate 
25900Sstevel@tonic-gate 	mp = allocb(sizeof (struct T_optmgmt_req) + sizeof (struct opthdr) +
25910Sstevel@tonic-gate 	    sizeof (int), BPRI_LO);
25920Sstevel@tonic-gate 	if (mp == NULL) {
25930Sstevel@tonic-gate 		RPCLOG0(1, "connmgr_setopt: cannot alloc mp for option "
25940Sstevel@tonic-gate 		    "request\n");
25950Sstevel@tonic-gate 		return (FALSE);
25960Sstevel@tonic-gate 	}
25970Sstevel@tonic-gate 
25980Sstevel@tonic-gate 	mp->b_datap->db_type = M_PROTO;
25990Sstevel@tonic-gate 	tor = (struct T_optmgmt_req *)(mp->b_rptr);
26000Sstevel@tonic-gate 	tor->PRIM_type = T_SVR4_OPTMGMT_REQ;
26010Sstevel@tonic-gate 	tor->MGMT_flags = T_NEGOTIATE;
26020Sstevel@tonic-gate 	tor->OPT_length = sizeof (struct opthdr) + sizeof (int);
26030Sstevel@tonic-gate 	tor->OPT_offset = sizeof (struct T_optmgmt_req);
26040Sstevel@tonic-gate 
26050Sstevel@tonic-gate 	opt = (struct opthdr *)(mp->b_rptr + sizeof (struct T_optmgmt_req));
26060Sstevel@tonic-gate 	opt->level = level;
26070Sstevel@tonic-gate 	opt->name = name;
26080Sstevel@tonic-gate 	opt->len = sizeof (int);
26090Sstevel@tonic-gate 	*(int *)((char *)opt + sizeof (*opt)) = 1;
26100Sstevel@tonic-gate 	mp->b_wptr += sizeof (struct T_optmgmt_req) + sizeof (struct opthdr) +
26110Sstevel@tonic-gate 	    sizeof (int);
26120Sstevel@tonic-gate 
26130Sstevel@tonic-gate 	/*
26140Sstevel@tonic-gate 	 * We will use this connection regardless
26150Sstevel@tonic-gate 	 * of whether or not the option is settable.
26160Sstevel@tonic-gate 	 */
26170Sstevel@tonic-gate 	clnt_dispatch_send(wq, mp, e, 0, 0);
26180Sstevel@tonic-gate 	mutex_enter(&clnt_pending_lock);
26190Sstevel@tonic-gate 
26200Sstevel@tonic-gate 	waitp.tv_sec = clnt_cots_min_conntout;
26210Sstevel@tonic-gate 	waitp.tv_usec = 0;
26220Sstevel@tonic-gate 	error = waitforack(e, T_OPTMGMT_ACK, &waitp, 1);
26230Sstevel@tonic-gate 
26240Sstevel@tonic-gate 	if (e->call_prev)
26250Sstevel@tonic-gate 		e->call_prev->call_next = e->call_next;
26260Sstevel@tonic-gate 	else
26270Sstevel@tonic-gate 		clnt_pending = e->call_next;
26280Sstevel@tonic-gate 	if (e->call_next)
26290Sstevel@tonic-gate 		e->call_next->call_prev = e->call_prev;
26300Sstevel@tonic-gate 	mutex_exit(&clnt_pending_lock);
26310Sstevel@tonic-gate 
26320Sstevel@tonic-gate 	if (e->call_reply != NULL) {
26330Sstevel@tonic-gate 		freemsg(e->call_reply);
26340Sstevel@tonic-gate 		e->call_reply = NULL;
26350Sstevel@tonic-gate 	}
26360Sstevel@tonic-gate 
26370Sstevel@tonic-gate 	if (e->call_status != RPC_SUCCESS || error != 0) {
26380Sstevel@tonic-gate 		RPCLOG(1, "connmgr_setopt: can't set option: %d\n", name);
26390Sstevel@tonic-gate 		return (FALSE);
26400Sstevel@tonic-gate 	}
26410Sstevel@tonic-gate 	RPCLOG(8, "connmgr_setopt: successfully set option: %d\n", name);
26420Sstevel@tonic-gate 	return (TRUE);
26430Sstevel@tonic-gate }
26440Sstevel@tonic-gate 
26450Sstevel@tonic-gate #ifdef	DEBUG
26460Sstevel@tonic-gate 
26470Sstevel@tonic-gate /*
26480Sstevel@tonic-gate  * This is a knob to let us force code coverage in allocation failure
26490Sstevel@tonic-gate  * case.
26500Sstevel@tonic-gate  */
26510Sstevel@tonic-gate static int	connmgr_failsnd;
26520Sstevel@tonic-gate #define	CONN_SND_ALLOC(Size, Pri)	\
26530Sstevel@tonic-gate 	((connmgr_failsnd-- > 0) ? NULL : allocb(Size, Pri))
26540Sstevel@tonic-gate 
26550Sstevel@tonic-gate #else
26560Sstevel@tonic-gate 
26570Sstevel@tonic-gate #define	CONN_SND_ALLOC(Size, Pri)	allocb(Size, Pri)
26580Sstevel@tonic-gate 
26590Sstevel@tonic-gate #endif
26600Sstevel@tonic-gate 
26610Sstevel@tonic-gate /*
26620Sstevel@tonic-gate  * Sends an orderly release on the specified queue.
26630Sstevel@tonic-gate  * Entered with connmgr_lock. Exited without connmgr_lock
26640Sstevel@tonic-gate  */
26650Sstevel@tonic-gate static void
26660Sstevel@tonic-gate connmgr_sndrel(struct cm_xprt *cm_entry)
26670Sstevel@tonic-gate {
26680Sstevel@tonic-gate 	struct T_ordrel_req *torr;
26690Sstevel@tonic-gate 	mblk_t *mp;
26700Sstevel@tonic-gate 	queue_t *q = cm_entry->x_wq;
26710Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&connmgr_lock));
26720Sstevel@tonic-gate 	mp = CONN_SND_ALLOC(sizeof (struct T_ordrel_req), BPRI_LO);
26730Sstevel@tonic-gate 	if (mp == NULL) {
26740Sstevel@tonic-gate 		cm_entry->x_needrel = TRUE;
26750Sstevel@tonic-gate 		mutex_exit(&connmgr_lock);
26760Sstevel@tonic-gate 		RPCLOG(1, "connmgr_sndrel: cannot alloc mp for sending ordrel "
26770Sstevel@tonic-gate 			"to queue %p\n", (void *)q);
26780Sstevel@tonic-gate 		return;
26790Sstevel@tonic-gate 	}
26800Sstevel@tonic-gate 	mutex_exit(&connmgr_lock);
26810Sstevel@tonic-gate 
26820Sstevel@tonic-gate 	mp->b_datap->db_type = M_PROTO;
26830Sstevel@tonic-gate 	torr = (struct T_ordrel_req *)(mp->b_rptr);
26840Sstevel@tonic-gate 	torr->PRIM_type = T_ORDREL_REQ;
26850Sstevel@tonic-gate 	mp->b_wptr = mp->b_rptr + sizeof (struct T_ordrel_req);
26860Sstevel@tonic-gate 
26870Sstevel@tonic-gate 	RPCLOG(8, "connmgr_sndrel: sending ordrel to queue %p\n", (void *)q);
26880Sstevel@tonic-gate 	put(q, mp);
26890Sstevel@tonic-gate }
26900Sstevel@tonic-gate 
26910Sstevel@tonic-gate /*
26920Sstevel@tonic-gate  * Sends an disconnect on the specified queue.
26930Sstevel@tonic-gate  * Entered with connmgr_lock. Exited without connmgr_lock
26940Sstevel@tonic-gate  */
26950Sstevel@tonic-gate static void
26960Sstevel@tonic-gate connmgr_snddis(struct cm_xprt *cm_entry)
26970Sstevel@tonic-gate {
26980Sstevel@tonic-gate 	struct T_discon_req *tdis;
26990Sstevel@tonic-gate 	mblk_t *mp;
27000Sstevel@tonic-gate 	queue_t *q = cm_entry->x_wq;
27010Sstevel@tonic-gate 
27020Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&connmgr_lock));
27030Sstevel@tonic-gate 	mp = CONN_SND_ALLOC(sizeof (*tdis), BPRI_LO);
27040Sstevel@tonic-gate 	if (mp == NULL) {
27050Sstevel@tonic-gate 		cm_entry->x_needdis = TRUE;
27060Sstevel@tonic-gate 		mutex_exit(&connmgr_lock);
27070Sstevel@tonic-gate 		RPCLOG(1, "connmgr_snddis: cannot alloc mp for sending discon "
27080Sstevel@tonic-gate 		    "to queue %p\n", (void *)q);
27090Sstevel@tonic-gate 		return;
27100Sstevel@tonic-gate 	}
27110Sstevel@tonic-gate 	mutex_exit(&connmgr_lock);
27120Sstevel@tonic-gate 
27130Sstevel@tonic-gate 	mp->b_datap->db_type = M_PROTO;
27140Sstevel@tonic-gate 	tdis = (struct T_discon_req *)mp->b_rptr;
27150Sstevel@tonic-gate 	tdis->PRIM_type = T_DISCON_REQ;
27160Sstevel@tonic-gate 	mp->b_wptr = mp->b_rptr + sizeof (*tdis);
27170Sstevel@tonic-gate 
27180Sstevel@tonic-gate 	RPCLOG(8, "connmgr_snddis: sending discon to queue %p\n", (void *)q);
27190Sstevel@tonic-gate 	put(q, mp);
27200Sstevel@tonic-gate }
27210Sstevel@tonic-gate 
27220Sstevel@tonic-gate /*
27230Sstevel@tonic-gate  * Sets up the entry for receiving replies, and calls rpcmod's write put proc
27240Sstevel@tonic-gate  * (through put) to send the call.
27250Sstevel@tonic-gate  */
27260Sstevel@tonic-gate static void
27270Sstevel@tonic-gate clnt_dispatch_send(queue_t *q, mblk_t *mp, calllist_t *e, uint_t xid,
27280Sstevel@tonic-gate 			uint_t queue_flag)
27290Sstevel@tonic-gate {
27300Sstevel@tonic-gate 	ASSERT(e != NULL);
27310Sstevel@tonic-gate 
27320Sstevel@tonic-gate 	e->call_status = RPC_TIMEDOUT;	/* optimistic, eh? */
27330Sstevel@tonic-gate 	e->call_reason = 0;
27340Sstevel@tonic-gate 	e->call_wq = q;
27350Sstevel@tonic-gate 	e->call_xid = xid;
27360Sstevel@tonic-gate 	e->call_notified = FALSE;
27370Sstevel@tonic-gate 
27380Sstevel@tonic-gate 	/*
27390Sstevel@tonic-gate 	 * If queue_flag is set then the calllist_t is already on the hash
27400Sstevel@tonic-gate 	 * queue.  In this case just send the message and return.
27410Sstevel@tonic-gate 	 */
27420Sstevel@tonic-gate 	if (queue_flag) {
27430Sstevel@tonic-gate 		put(q, mp);
27440Sstevel@tonic-gate 		return;
27450Sstevel@tonic-gate 	}
27460Sstevel@tonic-gate 
27470Sstevel@tonic-gate 	/*
27480Sstevel@tonic-gate 	 * Set up calls for RPC requests (with XID != 0) on the hash
27490Sstevel@tonic-gate 	 * queue for fast lookups and place other calls (i.e.
27500Sstevel@tonic-gate 	 * connection management) on the linked list.
27510Sstevel@tonic-gate 	 */
27520Sstevel@tonic-gate 	if (xid != 0) {
27530Sstevel@tonic-gate 		RPCLOG(64, "clnt_dispatch_send: putting xid 0x%x on "
27540Sstevel@tonic-gate 			"dispatch list\n", xid);
27550Sstevel@tonic-gate 		e->call_hash = call_hash(xid, clnt_cots_hash_size);
27560Sstevel@tonic-gate 		e->call_bucket = &cots_call_ht[e->call_hash];
27570Sstevel@tonic-gate 		call_table_enter(e);
27580Sstevel@tonic-gate 	} else {
27590Sstevel@tonic-gate 		mutex_enter(&clnt_pending_lock);
27600Sstevel@tonic-gate 		if (clnt_pending)
27610Sstevel@tonic-gate 			clnt_pending->call_prev = e;
27620Sstevel@tonic-gate 		e->call_next = clnt_pending;
27630Sstevel@tonic-gate 		e->call_prev = NULL;
27640Sstevel@tonic-gate 		clnt_pending = e;
27650Sstevel@tonic-gate 		mutex_exit(&clnt_pending_lock);
27660Sstevel@tonic-gate 	}
27670Sstevel@tonic-gate 
27680Sstevel@tonic-gate 	put(q, mp);
27690Sstevel@tonic-gate }
27700Sstevel@tonic-gate 
27710Sstevel@tonic-gate /*
27720Sstevel@tonic-gate  * Called by rpcmod to notify a client with a clnt_pending call that its reply
27730Sstevel@tonic-gate  * has arrived.  If we can't find a client waiting for this reply, we log
27740Sstevel@tonic-gate  * the error and return.
27750Sstevel@tonic-gate  */
27760Sstevel@tonic-gate bool_t
27770Sstevel@tonic-gate clnt_dispatch_notify(mblk_t *mp, zoneid_t zoneid)
27780Sstevel@tonic-gate {
27790Sstevel@tonic-gate 	calllist_t *e = NULL;
27800Sstevel@tonic-gate 	call_table_t *chtp;
27810Sstevel@tonic-gate 	uint32_t xid;
27820Sstevel@tonic-gate 	uint_t hash;
27830Sstevel@tonic-gate 
27840Sstevel@tonic-gate 	if ((IS_P2ALIGNED(mp->b_rptr, sizeof (uint32_t))) &&
27850Sstevel@tonic-gate 	    (mp->b_wptr - mp->b_rptr) >= sizeof (xid))
27860Sstevel@tonic-gate 		xid = *((uint32_t *)mp->b_rptr);
27870Sstevel@tonic-gate 	else {
27880Sstevel@tonic-gate 		int i = 0;
27890Sstevel@tonic-gate 		unsigned char *p = (unsigned char *)&xid;
27900Sstevel@tonic-gate 		unsigned char *rptr;
27910Sstevel@tonic-gate 		mblk_t *tmp = mp;
27920Sstevel@tonic-gate 
27930Sstevel@tonic-gate 		/*
27940Sstevel@tonic-gate 		 * Copy the xid, byte-by-byte into xid.
27950Sstevel@tonic-gate 		 */
27960Sstevel@tonic-gate 		while (tmp) {
27970Sstevel@tonic-gate 			rptr = tmp->b_rptr;
27980Sstevel@tonic-gate 			while (rptr < tmp->b_wptr) {
27990Sstevel@tonic-gate 				*p++ = *rptr++;
28000Sstevel@tonic-gate 				if (++i >= sizeof (xid))
28010Sstevel@tonic-gate 					goto done_xid_copy;
28020Sstevel@tonic-gate 			}
28030Sstevel@tonic-gate 			tmp = tmp->b_cont;
28040Sstevel@tonic-gate 		}
28050Sstevel@tonic-gate 
28060Sstevel@tonic-gate 		/*
28070Sstevel@tonic-gate 		 * If we got here, we ran out of mblk space before the
28080Sstevel@tonic-gate 		 * xid could be copied.
28090Sstevel@tonic-gate 		 */
28100Sstevel@tonic-gate 		ASSERT(tmp == NULL && i < sizeof (xid));
28110Sstevel@tonic-gate 
28120Sstevel@tonic-gate 		RPCLOG0(1,
28130Sstevel@tonic-gate 		    "clnt_dispatch_notify: message less than size of xid\n");
28140Sstevel@tonic-gate 		return (FALSE);
28150Sstevel@tonic-gate 
28160Sstevel@tonic-gate 	}
28170Sstevel@tonic-gate done_xid_copy:
28180Sstevel@tonic-gate 
28190Sstevel@tonic-gate 	hash = call_hash(xid, clnt_cots_hash_size);
28200Sstevel@tonic-gate 	chtp = &cots_call_ht[hash];
28210Sstevel@tonic-gate 	/* call_table_find returns with the hash bucket locked */
28220Sstevel@tonic-gate 	call_table_find(chtp, xid, e);
28230Sstevel@tonic-gate 
28240Sstevel@tonic-gate 	if (e != NULL) {
28250Sstevel@tonic-gate 		/*
28260Sstevel@tonic-gate 		 * Found thread waiting for this reply
28270Sstevel@tonic-gate 		 */
28280Sstevel@tonic-gate 		mutex_enter(&e->call_lock);
28290Sstevel@tonic-gate 		if (e->call_reply)
28300Sstevel@tonic-gate 			/*
28310Sstevel@tonic-gate 			 * This can happen under the following scenario:
28320Sstevel@tonic-gate 			 * clnt_cots_kcallit() times out on the response,
28330Sstevel@tonic-gate 			 * rfscall() repeats the CLNT_CALL() with
28340Sstevel@tonic-gate 			 * the same xid, clnt_cots_kcallit() sends the retry,
28350Sstevel@tonic-gate 			 * thereby putting the clnt handle on the pending list,
28360Sstevel@tonic-gate 			 * the first response arrives, signalling the thread
28370Sstevel@tonic-gate 			 * in clnt_cots_kcallit(). Before that thread is
28380Sstevel@tonic-gate 			 * dispatched, the second response arrives as well,
28390Sstevel@tonic-gate 			 * and clnt_dispatch_notify still finds the handle on
28400Sstevel@tonic-gate 			 * the pending list, with call_reply set. So free the
28410Sstevel@tonic-gate 			 * old reply now.
28420Sstevel@tonic-gate 			 *
28430Sstevel@tonic-gate 			 * It is also possible for a response intended for
28440Sstevel@tonic-gate 			 * an RPC call with a different xid to reside here.
28450Sstevel@tonic-gate 			 * This can happen if the thread that owned this
28460Sstevel@tonic-gate 			 * client handle prior to the current owner bailed
28470Sstevel@tonic-gate 			 * out and left its call record on the dispatch
28480Sstevel@tonic-gate 			 * queue.  A window exists where the response can
28490Sstevel@tonic-gate 			 * arrive before the current owner dispatches its
28500Sstevel@tonic-gate 			 * RPC call.
28510Sstevel@tonic-gate 			 *
28520Sstevel@tonic-gate 			 * In any case, this is the very last point where we
28530Sstevel@tonic-gate 			 * can safely check the call_reply field before
28540Sstevel@tonic-gate 			 * placing the new response there.
28550Sstevel@tonic-gate 			 */
28560Sstevel@tonic-gate 			freemsg(e->call_reply);
28570Sstevel@tonic-gate 		e->call_reply = mp;
28580Sstevel@tonic-gate 		e->call_status = RPC_SUCCESS;
28590Sstevel@tonic-gate 		e->call_notified = TRUE;
28600Sstevel@tonic-gate 		cv_signal(&e->call_cv);
28610Sstevel@tonic-gate 		mutex_exit(&e->call_lock);
28620Sstevel@tonic-gate 		mutex_exit(&chtp->ct_lock);
28630Sstevel@tonic-gate 		return (TRUE);
28640Sstevel@tonic-gate 	} else {
28650Sstevel@tonic-gate 		zone_t *zone;
28660Sstevel@tonic-gate 		struct rpcstat *rpcstat;
28670Sstevel@tonic-gate 
28680Sstevel@tonic-gate 		mutex_exit(&chtp->ct_lock);
28690Sstevel@tonic-gate 		RPCLOG(65, "clnt_dispatch_notify: no caller for reply 0x%x\n",
28700Sstevel@tonic-gate 		    xid);
28710Sstevel@tonic-gate 		/*
28720Sstevel@tonic-gate 		 * This is unfortunate, but we need to lookup the zone so we
28730Sstevel@tonic-gate 		 * can increment its "rcbadxids" counter.
28740Sstevel@tonic-gate 		 */
28750Sstevel@tonic-gate 		zone = zone_find_by_id(zoneid);
28760Sstevel@tonic-gate 		if (zone == NULL) {
28770Sstevel@tonic-gate 			/*
28780Sstevel@tonic-gate 			 * The zone went away...
28790Sstevel@tonic-gate 			 */
28800Sstevel@tonic-gate 			return (FALSE);
28810Sstevel@tonic-gate 		}
28820Sstevel@tonic-gate 		rpcstat = zone_getspecific(rpcstat_zone_key, zone);
28830Sstevel@tonic-gate 		if (zone_status_get(zone) >= ZONE_IS_SHUTTING_DOWN) {
28840Sstevel@tonic-gate 			/*
28850Sstevel@tonic-gate 			 * Not interested
28860Sstevel@tonic-gate 			 */
28870Sstevel@tonic-gate 			zone_rele(zone);
28880Sstevel@tonic-gate 			return (FALSE);
28890Sstevel@tonic-gate 		}
28900Sstevel@tonic-gate 		COTSRCSTAT_INCR(rpcstat->rpc_cots_client, rcbadxids);
28910Sstevel@tonic-gate 		zone_rele(zone);
28920Sstevel@tonic-gate 	}
28930Sstevel@tonic-gate 	return (FALSE);
28940Sstevel@tonic-gate }
28950Sstevel@tonic-gate 
28960Sstevel@tonic-gate /*
28970Sstevel@tonic-gate  * Called by rpcmod when a non-data indication arrives.  The ones in which we
28980Sstevel@tonic-gate  * are interested are connection indications and options acks.  We dispatch
28990Sstevel@tonic-gate  * based on the queue the indication came in on.  If we are not interested in
29000Sstevel@tonic-gate  * what came in, we return false to rpcmod, who will then pass it upstream.
29010Sstevel@tonic-gate  */
29020Sstevel@tonic-gate bool_t
29030Sstevel@tonic-gate clnt_dispatch_notifyconn(queue_t *q, mblk_t *mp)
29040Sstevel@tonic-gate {
29050Sstevel@tonic-gate 	calllist_t *e;
29060Sstevel@tonic-gate 	int type;
29070Sstevel@tonic-gate 
29080Sstevel@tonic-gate 	ASSERT((q->q_flag & QREADR) == 0);
29090Sstevel@tonic-gate 
29100Sstevel@tonic-gate 	type = ((union T_primitives *)mp->b_rptr)->type;
29110Sstevel@tonic-gate 	RPCLOG(8, "clnt_dispatch_notifyconn: prim type: [%s]\n",
29120Sstevel@tonic-gate 	    rpc_tpiprim2name(type));
29130Sstevel@tonic-gate 	mutex_enter(&clnt_pending_lock);
29140Sstevel@tonic-gate 	for (e = clnt_pending; /* NO CONDITION */; e = e->call_next) {
29150Sstevel@tonic-gate 		if (e == NULL) {
29160Sstevel@tonic-gate 			mutex_exit(&clnt_pending_lock);
29170Sstevel@tonic-gate 			RPCLOG(1, "clnt_dispatch_notifyconn: no one waiting "
29180Sstevel@tonic-gate 			    "for connection on queue 0x%p\n", (void *)q);
29190Sstevel@tonic-gate 			return (FALSE);
29200Sstevel@tonic-gate 		}
29210Sstevel@tonic-gate 		if (e->call_wq == q)
29220Sstevel@tonic-gate 			break;
29230Sstevel@tonic-gate 	}
29240Sstevel@tonic-gate 
29250Sstevel@tonic-gate 	switch (type) {
29260Sstevel@tonic-gate 	case T_CONN_CON:
29270Sstevel@tonic-gate 		/*
29280Sstevel@tonic-gate 		 * The transport is now connected, send a T_INFO_REQ to get
29290Sstevel@tonic-gate 		 * the tidu size.
29300Sstevel@tonic-gate 		 */
29310Sstevel@tonic-gate 		mutex_exit(&clnt_pending_lock);
29320Sstevel@tonic-gate 		ASSERT(mp->b_datap->db_lim - mp->b_datap->db_base >=
29330Sstevel@tonic-gate 			sizeof (struct T_info_req));
29340Sstevel@tonic-gate 		mp->b_rptr = mp->b_datap->db_base;
29350Sstevel@tonic-gate 		((union T_primitives *)mp->b_rptr)->type = T_INFO_REQ;
29360Sstevel@tonic-gate 		mp->b_wptr = mp->b_rptr + sizeof (struct T_info_req);
29370Sstevel@tonic-gate 		mp->b_datap->db_type = M_PCPROTO;
29380Sstevel@tonic-gate 		put(q, mp);
29390Sstevel@tonic-gate 		return (TRUE);
29400Sstevel@tonic-gate 	case T_INFO_ACK:
29410Sstevel@tonic-gate 	case T_OPTMGMT_ACK:
29420Sstevel@tonic-gate 		e->call_status = RPC_SUCCESS;
29430Sstevel@tonic-gate 		e->call_reply = mp;
29440Sstevel@tonic-gate 		e->call_notified = TRUE;
29450Sstevel@tonic-gate 		cv_signal(&e->call_cv);
29460Sstevel@tonic-gate 		break;
29470Sstevel@tonic-gate 	case T_ERROR_ACK:
29480Sstevel@tonic-gate 		e->call_status = RPC_CANTCONNECT;
29490Sstevel@tonic-gate 		e->call_reply = mp;
29500Sstevel@tonic-gate 		e->call_notified = TRUE;
29510Sstevel@tonic-gate 		cv_signal(&e->call_cv);
29520Sstevel@tonic-gate 		break;
29530Sstevel@tonic-gate 	case T_OK_ACK:
29540Sstevel@tonic-gate 		/*
29550Sstevel@tonic-gate 		 * Great, but we are really waiting for a T_CONN_CON
29560Sstevel@tonic-gate 		 */
29570Sstevel@tonic-gate 		freemsg(mp);
29580Sstevel@tonic-gate 		break;
29590Sstevel@tonic-gate 	default:
29600Sstevel@tonic-gate 		mutex_exit(&clnt_pending_lock);
29610Sstevel@tonic-gate 		RPCLOG(1, "clnt_dispatch_notifyconn: bad type %d\n", type);
29620Sstevel@tonic-gate 		return (FALSE);
29630Sstevel@tonic-gate 	}
29640Sstevel@tonic-gate 
29650Sstevel@tonic-gate 	mutex_exit(&clnt_pending_lock);
29660Sstevel@tonic-gate 	return (TRUE);
29670Sstevel@tonic-gate }
29680Sstevel@tonic-gate 
29690Sstevel@tonic-gate /*
29700Sstevel@tonic-gate  * Called by rpcmod when the transport is (or should be) going away.  Informs
29710Sstevel@tonic-gate  * all callers waiting for replies and marks the entry in the connection
29720Sstevel@tonic-gate  * manager's list as unconnected, and either closing (close handshake in
29730Sstevel@tonic-gate  * progress) or dead.
29740Sstevel@tonic-gate  */
29750Sstevel@tonic-gate void
29760Sstevel@tonic-gate clnt_dispatch_notifyall(queue_t *q, int32_t msg_type, int32_t reason)
29770Sstevel@tonic-gate {
29780Sstevel@tonic-gate 	calllist_t *e;
29790Sstevel@tonic-gate 	call_table_t *ctp;
29800Sstevel@tonic-gate 	struct cm_xprt *cm_entry;
29810Sstevel@tonic-gate 	int have_connmgr_lock;
29820Sstevel@tonic-gate 	int i;
29830Sstevel@tonic-gate 
29840Sstevel@tonic-gate 	ASSERT((q->q_flag & QREADR) == 0);
29850Sstevel@tonic-gate 
29860Sstevel@tonic-gate 	RPCLOG(1, "clnt_dispatch_notifyall on queue %p", (void *)q);
29870Sstevel@tonic-gate 	RPCLOG(1, " received a notifcation prim type [%s]",
29880Sstevel@tonic-gate 	    rpc_tpiprim2name(msg_type));
29890Sstevel@tonic-gate 	RPCLOG(1, " and reason %d\n", reason);
29900Sstevel@tonic-gate 
29910Sstevel@tonic-gate 	/*
29920Sstevel@tonic-gate 	 * Find the transport entry in the connection manager's list, close
29930Sstevel@tonic-gate 	 * the transport and delete the entry.  In the case where rpcmod's
29940Sstevel@tonic-gate 	 * idle timer goes off, it sends us a T_ORDREL_REQ, indicating we
29950Sstevel@tonic-gate 	 * should gracefully close the connection.
29960Sstevel@tonic-gate 	 */
29970Sstevel@tonic-gate 	have_connmgr_lock = 1;
29980Sstevel@tonic-gate 	mutex_enter(&connmgr_lock);
29990Sstevel@tonic-gate 	for (cm_entry = cm_hd; cm_entry; cm_entry = cm_entry->x_next) {
30000Sstevel@tonic-gate 		ASSERT(cm_entry != cm_entry->x_next);
30010Sstevel@tonic-gate 		if (cm_entry->x_wq == q) {
30020Sstevel@tonic-gate 			ASSERT(MUTEX_HELD(&connmgr_lock));
30030Sstevel@tonic-gate 			ASSERT(have_connmgr_lock == 1);
30040Sstevel@tonic-gate 			switch (msg_type) {
30050Sstevel@tonic-gate 			case T_ORDREL_REQ:
30060Sstevel@tonic-gate 
30070Sstevel@tonic-gate 				if (cm_entry->x_dead) {
30080Sstevel@tonic-gate 					RPCLOG(1, "idle timeout on dead "
30090Sstevel@tonic-gate 					    "connection: %p\n",
30100Sstevel@tonic-gate 					    (void *)cm_entry);
30110Sstevel@tonic-gate 					if (clnt_stop_idle != NULL)
30120Sstevel@tonic-gate 						(*clnt_stop_idle)(q);
30130Sstevel@tonic-gate 					break;
30140Sstevel@tonic-gate 				}
30150Sstevel@tonic-gate 
30160Sstevel@tonic-gate 				/*
30170Sstevel@tonic-gate 				 * Only mark the connection as dead if it is
30180Sstevel@tonic-gate 				 * connected and idle.
30190Sstevel@tonic-gate 				 * An unconnected connection has probably
30200Sstevel@tonic-gate 				 * gone idle because the server is down,
30210Sstevel@tonic-gate 				 * and when it comes back up there will be
30220Sstevel@tonic-gate 				 * retries that need to use that connection.
30230Sstevel@tonic-gate 				 */
30240Sstevel@tonic-gate 				if (cm_entry->x_connected ||
30250Sstevel@tonic-gate 				    cm_entry->x_doomed) {
30260Sstevel@tonic-gate 				    if (cm_entry->x_ordrel) {
30270Sstevel@tonic-gate 					if (cm_entry->x_closing == TRUE) {
30280Sstevel@tonic-gate 					/*
30290Sstevel@tonic-gate 					 * The connection is obviously
30300Sstevel@tonic-gate 					 * wedged due to a bug or problem
30310Sstevel@tonic-gate 					 * with the transport. Mark it
30320Sstevel@tonic-gate 					 * as dead. Otherwise we can leak
30330Sstevel@tonic-gate 					 * connections.
30340Sstevel@tonic-gate 					 */
30350Sstevel@tonic-gate 					    cm_entry->x_dead = TRUE;
30360Sstevel@tonic-gate 					    mutex_exit(&connmgr_lock);
30370Sstevel@tonic-gate 					    have_connmgr_lock = 0;
30380Sstevel@tonic-gate 					    if (clnt_stop_idle != NULL)
30390Sstevel@tonic-gate 						(*clnt_stop_idle)(q);
30400Sstevel@tonic-gate 					    break;
30410Sstevel@tonic-gate 					}
30420Sstevel@tonic-gate 					cm_entry->x_closing = TRUE;
30430Sstevel@tonic-gate 					connmgr_sndrel(cm_entry);
30440Sstevel@tonic-gate 					have_connmgr_lock = 0;
30450Sstevel@tonic-gate 				    } else {
30460Sstevel@tonic-gate 					cm_entry->x_dead = TRUE;
30470Sstevel@tonic-gate 					mutex_exit(&connmgr_lock);
30480Sstevel@tonic-gate 					have_connmgr_lock = 0;
30490Sstevel@tonic-gate 					if (clnt_stop_idle != NULL)
30500Sstevel@tonic-gate 						(*clnt_stop_idle)(q);
30510Sstevel@tonic-gate 				    }
30520Sstevel@tonic-gate 				} else {
30530Sstevel@tonic-gate 					/*
30540Sstevel@tonic-gate 					 * We don't mark the connection
30550Sstevel@tonic-gate 					 * as dead, but we turn off the
30560Sstevel@tonic-gate 					 * idle timer.
30570Sstevel@tonic-gate 					 */
30580Sstevel@tonic-gate 					mutex_exit(&connmgr_lock);
30590Sstevel@tonic-gate 					have_connmgr_lock = 0;
30600Sstevel@tonic-gate 					if (clnt_stop_idle != NULL)
30610Sstevel@tonic-gate 						(*clnt_stop_idle)(q);
30620Sstevel@tonic-gate 					RPCLOG(1, "clnt_dispatch_notifyall:"
30630Sstevel@tonic-gate 					    " ignoring timeout from rpcmod"
30640Sstevel@tonic-gate 					    " (q %p) because we are not "
30650Sstevel@tonic-gate 					    " connected\n", (void *)q);
30660Sstevel@tonic-gate 				}
30670Sstevel@tonic-gate 				break;
30680Sstevel@tonic-gate 			case T_ORDREL_IND:
30690Sstevel@tonic-gate 				/*
30700Sstevel@tonic-gate 				 * If this entry is marked closing, then we are
30710Sstevel@tonic-gate 				 * completing a close handshake, and the
30720Sstevel@tonic-gate 				 * connection is dead.  Otherwise, the server is
30730Sstevel@tonic-gate 				 * trying to close. Since the server will not
30740Sstevel@tonic-gate 				 * be sending any more RPC replies, we abort
30750Sstevel@tonic-gate 				 * the connection, including flushing
30760Sstevel@tonic-gate 				 * any RPC requests that are in-transit.
30770Sstevel@tonic-gate 				 */
30780Sstevel@tonic-gate 				if (cm_entry->x_closing) {
30790Sstevel@tonic-gate 					cm_entry->x_dead = TRUE;
30800Sstevel@tonic-gate 					mutex_exit(&connmgr_lock);
30810Sstevel@tonic-gate 					have_connmgr_lock = 0;
30820Sstevel@tonic-gate 					if (clnt_stop_idle != NULL)
30830Sstevel@tonic-gate 						(*clnt_stop_idle)(q);
30840Sstevel@tonic-gate 				} else {
30850Sstevel@tonic-gate 					/*
30860Sstevel@tonic-gate 					 * if we're getting a disconnect
30870Sstevel@tonic-gate 					 * before we've finished our
30880Sstevel@tonic-gate 					 * connect attempt, mark it for
30890Sstevel@tonic-gate 					 * later processing
30900Sstevel@tonic-gate 					 */
30910Sstevel@tonic-gate 					if (cm_entry->x_thread)
30920Sstevel@tonic-gate 						cm_entry->x_early_disc = TRUE;
30930Sstevel@tonic-gate 					else
30940Sstevel@tonic-gate 						cm_entry->x_connected = FALSE;
30950Sstevel@tonic-gate 					cm_entry->x_waitdis = TRUE;
30960Sstevel@tonic-gate 					connmgr_snddis(cm_entry);
30970Sstevel@tonic-gate 					have_connmgr_lock = 0;
30980Sstevel@tonic-gate 				}
30990Sstevel@tonic-gate 				break;
31000Sstevel@tonic-gate 
31010Sstevel@tonic-gate 			case T_ERROR_ACK:
31020Sstevel@tonic-gate 			case T_OK_ACK:
31030Sstevel@tonic-gate 				cm_entry->x_waitdis = FALSE;
31040Sstevel@tonic-gate 				cv_signal(&cm_entry->x_dis_cv);
31050Sstevel@tonic-gate 				mutex_exit(&connmgr_lock);
31060Sstevel@tonic-gate 				return;
31070Sstevel@tonic-gate 
31080Sstevel@tonic-gate 			case T_DISCON_REQ:
31090Sstevel@tonic-gate 				if (cm_entry->x_thread)
31100Sstevel@tonic-gate 					cm_entry->x_early_disc = TRUE;
31110Sstevel@tonic-gate 				else
31120Sstevel@tonic-gate 					cm_entry->x_connected = FALSE;
31130Sstevel@tonic-gate 				cm_entry->x_waitdis = TRUE;
31140Sstevel@tonic-gate 
31150Sstevel@tonic-gate 				connmgr_snddis(cm_entry);
31160Sstevel@tonic-gate 				have_connmgr_lock = 0;
31170Sstevel@tonic-gate 				break;
31180Sstevel@tonic-gate 
31190Sstevel@tonic-gate 			case T_DISCON_IND:
31200Sstevel@tonic-gate 			default:
31210Sstevel@tonic-gate 				/*
31220Sstevel@tonic-gate 				 * if we're getting a disconnect before
31230Sstevel@tonic-gate 				 * we've finished our connect attempt,
31240Sstevel@tonic-gate 				 * mark it for later processing
31250Sstevel@tonic-gate 				 */
31260Sstevel@tonic-gate 				if (cm_entry->x_closing) {
31270Sstevel@tonic-gate 					cm_entry->x_dead = TRUE;
31280Sstevel@tonic-gate 					mutex_exit(&connmgr_lock);
31290Sstevel@tonic-gate 					have_connmgr_lock = 0;
31300Sstevel@tonic-gate 					if (clnt_stop_idle != NULL)
31310Sstevel@tonic-gate 						(*clnt_stop_idle)(q);
31320Sstevel@tonic-gate 				} else {
31330Sstevel@tonic-gate 					if (cm_entry->x_thread) {
31340Sstevel@tonic-gate 						cm_entry->x_early_disc = TRUE;
31350Sstevel@tonic-gate 					} else {
31360Sstevel@tonic-gate 						cm_entry->x_dead = TRUE;
31370Sstevel@tonic-gate 						cm_entry->x_connected = FALSE;
31380Sstevel@tonic-gate 					}
31390Sstevel@tonic-gate 				}
31400Sstevel@tonic-gate 				break;
31410Sstevel@tonic-gate 			}
31420Sstevel@tonic-gate 			break;
31430Sstevel@tonic-gate 		}
31440Sstevel@tonic-gate 	}
31450Sstevel@tonic-gate 
31460Sstevel@tonic-gate 	if (have_connmgr_lock)
31470Sstevel@tonic-gate 		mutex_exit(&connmgr_lock);
31480Sstevel@tonic-gate 
31490Sstevel@tonic-gate 	if (msg_type == T_ERROR_ACK || msg_type == T_OK_ACK) {
31500Sstevel@tonic-gate 		RPCLOG(1, "clnt_dispatch_notifyall: (wq %p) could not find "
31510Sstevel@tonic-gate 		    "connmgr entry for discon ack\n", (void *)q);
31520Sstevel@tonic-gate 		return;
31530Sstevel@tonic-gate 	}
31540Sstevel@tonic-gate 
31550Sstevel@tonic-gate 	/*
31560Sstevel@tonic-gate 	 * Then kick all the clnt_pending calls out of their wait.  There
31570Sstevel@tonic-gate 	 * should be no clnt_pending calls in the case of rpcmod's idle
31580Sstevel@tonic-gate 	 * timer firing.
31590Sstevel@tonic-gate 	 */
31600Sstevel@tonic-gate 	for (i = 0; i < clnt_cots_hash_size; i++) {
31610Sstevel@tonic-gate 		ctp = &cots_call_ht[i];
31620Sstevel@tonic-gate 		mutex_enter(&ctp->ct_lock);
31630Sstevel@tonic-gate 		for (e = ctp->ct_call_next;
31640Sstevel@tonic-gate 			e != (calllist_t *)ctp;
31650Sstevel@tonic-gate 			e = e->call_next) {
31660Sstevel@tonic-gate 			if (e->call_wq == q && e->call_notified == FALSE) {
31670Sstevel@tonic-gate 				RPCLOG(1,
31680Sstevel@tonic-gate 				"clnt_dispatch_notifyall for queue %p ",
31690Sstevel@tonic-gate 					(void *)q);
31700Sstevel@tonic-gate 				RPCLOG(1, "aborting clnt_pending call %p\n",
31710Sstevel@tonic-gate 					(void *)e);
31720Sstevel@tonic-gate 
31730Sstevel@tonic-gate 				if (msg_type == T_DISCON_IND)
31740Sstevel@tonic-gate 					e->call_reason = reason;
31750Sstevel@tonic-gate 				e->call_notified = TRUE;
31760Sstevel@tonic-gate 				e->call_status = RPC_XPRTFAILED;
31770Sstevel@tonic-gate 				cv_signal(&e->call_cv);
31780Sstevel@tonic-gate 			}
31790Sstevel@tonic-gate 		}
31800Sstevel@tonic-gate 		mutex_exit(&ctp->ct_lock);
31810Sstevel@tonic-gate 	}
31820Sstevel@tonic-gate 
31830Sstevel@tonic-gate 	mutex_enter(&clnt_pending_lock);
31840Sstevel@tonic-gate 	for (e = clnt_pending; e; e = e->call_next) {
31850Sstevel@tonic-gate 		/*
31860Sstevel@tonic-gate 		 * Only signal those RPC handles that haven't been
31870Sstevel@tonic-gate 		 * signalled yet. Otherwise we can get a bogus call_reason.
31880Sstevel@tonic-gate 		 * This can happen if thread A is making a call over a
31890Sstevel@tonic-gate 		 * connection. If the server is killed, it will cause
31900Sstevel@tonic-gate 		 * reset, and reason will default to EIO as a result of
31910Sstevel@tonic-gate 		 * a T_ORDREL_IND. Thread B then attempts to recreate
31920Sstevel@tonic-gate 		 * the connection but gets a T_DISCON_IND. If we set the
31930Sstevel@tonic-gate 		 * call_reason code for all threads, then if thread A
31940Sstevel@tonic-gate 		 * hasn't been dispatched yet, it will get the wrong
31950Sstevel@tonic-gate 		 * reason. The bogus call_reason can make it harder to
31960Sstevel@tonic-gate 		 * discriminate between calls that fail because the
31970Sstevel@tonic-gate 		 * connection attempt failed versus those where the call
31980Sstevel@tonic-gate 		 * may have been executed on the server.
31990Sstevel@tonic-gate 		 */
32000Sstevel@tonic-gate 		if (e->call_wq == q && e->call_notified == FALSE) {
32010Sstevel@tonic-gate 			RPCLOG(1, "clnt_dispatch_notifyall for queue %p ",
32020Sstevel@tonic-gate 			    (void *)q);
32030Sstevel@tonic-gate 			RPCLOG(1, " aborting clnt_pending call %p\n",
32040Sstevel@tonic-gate 			    (void *)e);
32050Sstevel@tonic-gate 
32060Sstevel@tonic-gate 			if (msg_type == T_DISCON_IND)
32070Sstevel@tonic-gate 				e->call_reason = reason;
32080Sstevel@tonic-gate 			e->call_notified = TRUE;
32090Sstevel@tonic-gate 			/*
32100Sstevel@tonic-gate 			 * Let the caller timeout, else he will retry
32110Sstevel@tonic-gate 			 * immediately.
32120Sstevel@tonic-gate 			 */
32130Sstevel@tonic-gate 			e->call_status = RPC_XPRTFAILED;
32140Sstevel@tonic-gate 
32150Sstevel@tonic-gate 			/*
32160Sstevel@tonic-gate 			 * We used to just signal those threads
32170Sstevel@tonic-gate 			 * waiting for a connection, (call_xid = 0).
32180Sstevel@tonic-gate 			 * That meant that threads waiting for a response
32190Sstevel@tonic-gate 			 * waited till their timeout expired. This
32200Sstevel@tonic-gate 			 * could be a long time if they've specified a
32210Sstevel@tonic-gate 			 * maximum timeout. (2^31 - 1). So we
32220Sstevel@tonic-gate 			 * Signal all threads now.
32230Sstevel@tonic-gate 			 */
32240Sstevel@tonic-gate 			cv_signal(&e->call_cv);
32250Sstevel@tonic-gate 		}
32260Sstevel@tonic-gate 	}
32270Sstevel@tonic-gate 	mutex_exit(&clnt_pending_lock);
32280Sstevel@tonic-gate }
32290Sstevel@tonic-gate 
32300Sstevel@tonic-gate 
32310Sstevel@tonic-gate /*ARGSUSED*/
32320Sstevel@tonic-gate /*
32330Sstevel@tonic-gate  * after resuming a system that's been suspended for longer than the
32340Sstevel@tonic-gate  * NFS server's idle timeout (svc_idle_timeout for Solaris 2), rfscall()
32350Sstevel@tonic-gate  * generates "NFS server X not responding" and "NFS server X ok" messages;
32360Sstevel@tonic-gate  * here we reset inet connections to cause a re-connect and avoid those
32370Sstevel@tonic-gate  * NFS messages.  see 4045054
32380Sstevel@tonic-gate  */
32390Sstevel@tonic-gate boolean_t
32400Sstevel@tonic-gate connmgr_cpr_reset(void *arg, int code)
32410Sstevel@tonic-gate {
32420Sstevel@tonic-gate 	struct cm_xprt *cxp;
32430Sstevel@tonic-gate 
32440Sstevel@tonic-gate 	if (code == CB_CODE_CPR_CHKPT)
32450Sstevel@tonic-gate 		return (B_TRUE);
32460Sstevel@tonic-gate 
32470Sstevel@tonic-gate 	if (mutex_tryenter(&connmgr_lock) == 0)
32480Sstevel@tonic-gate 		return (B_FALSE);
32490Sstevel@tonic-gate 	for (cxp = cm_hd; cxp; cxp = cxp->x_next) {
32500Sstevel@tonic-gate 		if ((cxp->x_family == AF_INET || cxp->x_family == AF_INET6) &&
32510Sstevel@tonic-gate 			cxp->x_connected == TRUE) {
32520Sstevel@tonic-gate 			if (cxp->x_thread)
32530Sstevel@tonic-gate 				cxp->x_early_disc = TRUE;
32540Sstevel@tonic-gate 			else
32550Sstevel@tonic-gate 				cxp->x_connected = FALSE;
32560Sstevel@tonic-gate 			cxp->x_needdis = TRUE;
32570Sstevel@tonic-gate 		}
32580Sstevel@tonic-gate 	}
32590Sstevel@tonic-gate 	mutex_exit(&connmgr_lock);
32600Sstevel@tonic-gate 	return (B_TRUE);
32610Sstevel@tonic-gate }
32620Sstevel@tonic-gate 
32630Sstevel@tonic-gate void
32640Sstevel@tonic-gate clnt_cots_stats_init(zoneid_t zoneid, struct rpc_cots_client **statsp)
32650Sstevel@tonic-gate {
32660Sstevel@tonic-gate 
32670Sstevel@tonic-gate 	*statsp = (struct rpc_cots_client *)rpcstat_zone_init_common(zoneid,
32680Sstevel@tonic-gate 	    "unix", "rpc_cots_client", (const kstat_named_t *)&cots_rcstat_tmpl,
32690Sstevel@tonic-gate 	    sizeof (cots_rcstat_tmpl));
32700Sstevel@tonic-gate }
32710Sstevel@tonic-gate 
32720Sstevel@tonic-gate void
32730Sstevel@tonic-gate clnt_cots_stats_fini(zoneid_t zoneid, struct rpc_cots_client **statsp)
32740Sstevel@tonic-gate {
32750Sstevel@tonic-gate 	rpcstat_zone_fini_common(zoneid, "unix", "rpc_cots_client");
32760Sstevel@tonic-gate 	kmem_free(*statsp, sizeof (cots_rcstat_tmpl));
32770Sstevel@tonic-gate }
32780Sstevel@tonic-gate 
32790Sstevel@tonic-gate void
32800Sstevel@tonic-gate clnt_cots_init(void)
32810Sstevel@tonic-gate {
32820Sstevel@tonic-gate 	mutex_init(&connmgr_lock, NULL, MUTEX_DEFAULT, NULL);
32830Sstevel@tonic-gate 	mutex_init(&clnt_pending_lock, NULL, MUTEX_DEFAULT, NULL);
32840Sstevel@tonic-gate 
32850Sstevel@tonic-gate 	if (clnt_cots_hash_size < DEFAULT_MIN_HASH_SIZE)
32860Sstevel@tonic-gate 		clnt_cots_hash_size = DEFAULT_MIN_HASH_SIZE;
32870Sstevel@tonic-gate 
32880Sstevel@tonic-gate 	cots_call_ht = call_table_init(clnt_cots_hash_size);
32890Sstevel@tonic-gate 	zone_key_create(&zone_cots_key, NULL, NULL, clnt_zone_destroy);
32900Sstevel@tonic-gate }
32910Sstevel@tonic-gate 
32920Sstevel@tonic-gate void
32930Sstevel@tonic-gate clnt_cots_fini(void)
32940Sstevel@tonic-gate {
32950Sstevel@tonic-gate 	(void) zone_key_delete(zone_cots_key);
32960Sstevel@tonic-gate }
32970Sstevel@tonic-gate 
32980Sstevel@tonic-gate /*
32990Sstevel@tonic-gate  * Wait for TPI ack, returns success only if expected ack is received
33000Sstevel@tonic-gate  * within timeout period.
33010Sstevel@tonic-gate  */
33020Sstevel@tonic-gate 
33030Sstevel@tonic-gate static int
33040Sstevel@tonic-gate waitforack(calllist_t *e, t_scalar_t ack_prim, const struct timeval *waitp,
33050Sstevel@tonic-gate     bool_t nosignal)
33060Sstevel@tonic-gate {
33070Sstevel@tonic-gate 	union T_primitives *tpr;
33080Sstevel@tonic-gate 	clock_t timout;
33090Sstevel@tonic-gate 	int cv_stat = 1;
33100Sstevel@tonic-gate 
33110Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&clnt_pending_lock));
33120Sstevel@tonic-gate 	while (e->call_reply == NULL) {
33130Sstevel@tonic-gate 		if (waitp != NULL) {
33140Sstevel@tonic-gate 			timout = waitp->tv_sec * drv_usectohz(MICROSEC) +
33150Sstevel@tonic-gate 			    drv_usectohz(waitp->tv_usec) + lbolt;
33160Sstevel@tonic-gate 			if (nosignal)
33170Sstevel@tonic-gate 				cv_stat = cv_timedwait(&e->call_cv,
33180Sstevel@tonic-gate 				    &clnt_pending_lock, timout);
33190Sstevel@tonic-gate 			else
33200Sstevel@tonic-gate 				cv_stat = cv_timedwait_sig(&e->call_cv,
33210Sstevel@tonic-gate 				    &clnt_pending_lock, timout);
33220Sstevel@tonic-gate 		} else {
33230Sstevel@tonic-gate 			if (nosignal)
33240Sstevel@tonic-gate 				cv_wait(&e->call_cv, &clnt_pending_lock);
33250Sstevel@tonic-gate 			else
33260Sstevel@tonic-gate 				cv_stat = cv_wait_sig(&e->call_cv,
33270Sstevel@tonic-gate 				    &clnt_pending_lock);
33280Sstevel@tonic-gate 		}
33290Sstevel@tonic-gate 		if (cv_stat == -1)
33300Sstevel@tonic-gate 			return (ETIME);
33310Sstevel@tonic-gate 		if (cv_stat == 0)
33320Sstevel@tonic-gate 			return (EINTR);
33330Sstevel@tonic-gate 	}
33340Sstevel@tonic-gate 	tpr = (union T_primitives *)e->call_reply->b_rptr;
33350Sstevel@tonic-gate 	if (tpr->type == ack_prim)
33360Sstevel@tonic-gate 		return (0); /* Success */
33370Sstevel@tonic-gate 
33380Sstevel@tonic-gate 	if (tpr->type == T_ERROR_ACK) {
33390Sstevel@tonic-gate 		if (tpr->error_ack.TLI_error == TSYSERR)
33400Sstevel@tonic-gate 			return (tpr->error_ack.UNIX_error);
33410Sstevel@tonic-gate 		else
33420Sstevel@tonic-gate 			return (t_tlitosyserr(tpr->error_ack.TLI_error));
33430Sstevel@tonic-gate 	}
33440Sstevel@tonic-gate 
33450Sstevel@tonic-gate 	return (EPROTO); /* unknown or unexpected primitive */
33460Sstevel@tonic-gate }
3347