xref: /onnv-gate/usr/src/uts/common/rpc/clnt_clts.c (revision 11380:73d454719071)
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
51676Sjpk  * Common Development and Distribution License (the "License").
61676Sjpk  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
228766Sdai.ngo@sun.com  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T
280Sstevel@tonic-gate  * All Rights Reserved
290Sstevel@tonic-gate  */
300Sstevel@tonic-gate 
310Sstevel@tonic-gate /*
320Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
330Sstevel@tonic-gate  * under license from the Regents of the University of California.
340Sstevel@tonic-gate  */
350Sstevel@tonic-gate 
360Sstevel@tonic-gate 
370Sstevel@tonic-gate /*
380Sstevel@tonic-gate  * Implements a kernel based, client side RPC.
390Sstevel@tonic-gate  */
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #include <sys/param.h>
420Sstevel@tonic-gate #include <sys/types.h>
430Sstevel@tonic-gate #include <sys/systm.h>
440Sstevel@tonic-gate #include <sys/sysmacros.h>
450Sstevel@tonic-gate #include <sys/stream.h>
460Sstevel@tonic-gate #include <sys/strsubr.h>
470Sstevel@tonic-gate #include <sys/ddi.h>
480Sstevel@tonic-gate #include <sys/tiuser.h>
490Sstevel@tonic-gate #include <sys/tihdr.h>
500Sstevel@tonic-gate #include <sys/t_kuser.h>
510Sstevel@tonic-gate #include <sys/errno.h>
520Sstevel@tonic-gate #include <sys/kmem.h>
530Sstevel@tonic-gate #include <sys/debug.h>
540Sstevel@tonic-gate #include <sys/kstat.h>
550Sstevel@tonic-gate #include <sys/t_lock.h>
560Sstevel@tonic-gate #include <sys/cmn_err.h>
570Sstevel@tonic-gate #include <sys/conf.h>
580Sstevel@tonic-gate #include <sys/disp.h>
590Sstevel@tonic-gate #include <sys/taskq.h>
600Sstevel@tonic-gate #include <sys/list.h>
610Sstevel@tonic-gate #include <sys/atomic.h>
620Sstevel@tonic-gate #include <sys/zone.h>
630Sstevel@tonic-gate #include <netinet/in.h>
640Sstevel@tonic-gate #include <rpc/types.h>
650Sstevel@tonic-gate #include <rpc/xdr.h>
660Sstevel@tonic-gate #include <rpc/auth.h>
670Sstevel@tonic-gate #include <rpc/clnt.h>
680Sstevel@tonic-gate #include <rpc/rpc_msg.h>
690Sstevel@tonic-gate 
708766Sdai.ngo@sun.com #include <sys/sdt.h>
718766Sdai.ngo@sun.com 
720Sstevel@tonic-gate static enum clnt_stat clnt_clts_kcallit(CLIENT *, rpcproc_t, xdrproc_t,
730Sstevel@tonic-gate 		    caddr_t, xdrproc_t, caddr_t, struct timeval);
740Sstevel@tonic-gate static void	clnt_clts_kabort(CLIENT *);
750Sstevel@tonic-gate static void	clnt_clts_kerror(CLIENT *, struct rpc_err *);
760Sstevel@tonic-gate static bool_t	clnt_clts_kfreeres(CLIENT *, xdrproc_t, caddr_t);
770Sstevel@tonic-gate static bool_t	clnt_clts_kcontrol(CLIENT *, int, char *);
780Sstevel@tonic-gate static void	clnt_clts_kdestroy(CLIENT *);
790Sstevel@tonic-gate static int	clnt_clts_ksettimers(CLIENT *, struct rpc_timers *,
800Sstevel@tonic-gate 		    struct rpc_timers *, int, void (*)(), caddr_t, uint32_t);
810Sstevel@tonic-gate 
820Sstevel@tonic-gate /*
830Sstevel@tonic-gate  * Operations vector for CLTS based RPC
840Sstevel@tonic-gate  */
850Sstevel@tonic-gate static struct clnt_ops clts_ops = {
860Sstevel@tonic-gate 	clnt_clts_kcallit,	/* do rpc call */
870Sstevel@tonic-gate 	clnt_clts_kabort,	/* abort call */
880Sstevel@tonic-gate 	clnt_clts_kerror,	/* return error status */
890Sstevel@tonic-gate 	clnt_clts_kfreeres,	/* free results */
900Sstevel@tonic-gate 	clnt_clts_kdestroy,	/* destroy rpc handle */
910Sstevel@tonic-gate 	clnt_clts_kcontrol,	/* the ioctl() of rpc */
920Sstevel@tonic-gate 	clnt_clts_ksettimers	/* set retry timers */
930Sstevel@tonic-gate };
940Sstevel@tonic-gate 
950Sstevel@tonic-gate /*
960Sstevel@tonic-gate  * Endpoint for CLTS (INET, INET6, loopback, etc.)
970Sstevel@tonic-gate  */
980Sstevel@tonic-gate typedef struct endpnt_type {
990Sstevel@tonic-gate 	struct endpnt_type *e_next;	/* pointer to next endpoint type */
1000Sstevel@tonic-gate 	list_t		e_pool;		/* list of available endpoints */
1018766Sdai.ngo@sun.com 	list_t		e_ilist;	/* list of idle endpoints */
1020Sstevel@tonic-gate 	struct endpnt	*e_pcurr;	/* pointer to current endpoint */
1030Sstevel@tonic-gate 	char		e_protofmly[KNC_STRSIZE];	/* protocol family */
1040Sstevel@tonic-gate 	dev_t		e_rdev;		/* device */
1050Sstevel@tonic-gate 	kmutex_t	e_plock;	/* pool lock */
1060Sstevel@tonic-gate 	kmutex_t	e_ilock;	/* idle list lock */
1070Sstevel@tonic-gate 	timeout_id_t	e_itimer;	/* timer to dispatch the taskq */
1080Sstevel@tonic-gate 	uint_t		e_cnt;		/* number of endpoints in the pool */
1090Sstevel@tonic-gate 	zoneid_t	e_zoneid;	/* zoneid of endpoint type */
1100Sstevel@tonic-gate 	kcondvar_t	e_async_cv;	/* cv for asynchronous reap threads */
1110Sstevel@tonic-gate 	uint_t		e_async_count;	/* count of asynchronous reap threads */
1120Sstevel@tonic-gate } endpnt_type_t;
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate typedef struct endpnt {
1150Sstevel@tonic-gate 	list_node_t	e_node;		/* link to the pool */
1160Sstevel@tonic-gate 	list_node_t	e_idle;		/* link to the idle list */
1170Sstevel@tonic-gate 	endpnt_type_t	*e_type;	/* back pointer to endpoint type */
1180Sstevel@tonic-gate 	TIUSER		*e_tiptr;	/* pointer to transport endpoint */
1190Sstevel@tonic-gate 	queue_t		*e_wq;		/* write queue */
1200Sstevel@tonic-gate 	uint_t		e_flags;	/* endpoint flags */
1210Sstevel@tonic-gate 	uint_t		e_ref;		/* ref count on endpoint */
1220Sstevel@tonic-gate 	kcondvar_t	e_cv;		/* condition variable */
1230Sstevel@tonic-gate 	kmutex_t	e_lock;		/* protects cv and flags */
1240Sstevel@tonic-gate 	time_t		e_itime;	/* time when rele'd */
1250Sstevel@tonic-gate } endpnt_t;
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate #define	ENDPNT_ESTABLISHED	0x1	/* endpoint is established */
1280Sstevel@tonic-gate #define	ENDPNT_WAITING		0x2	/* thread waiting for endpoint */
1290Sstevel@tonic-gate #define	ENDPNT_BOUND		0x4	/* endpoint is bound */
1300Sstevel@tonic-gate #define	ENDPNT_STALE		0x8	/* endpoint is dead */
1310Sstevel@tonic-gate #define	ENDPNT_ONIDLE		0x10	/* endpoint is on the idle list */
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate static krwlock_t	endpnt_type_lock; /* protects endpnt_type_list */
1340Sstevel@tonic-gate static endpnt_type_t	*endpnt_type_list = NULL; /* list of CLTS endpoints */
1350Sstevel@tonic-gate static struct kmem_cache	*endpnt_cache; /* cache of endpnt_t's */
1360Sstevel@tonic-gate static taskq_t			*endpnt_taskq; /* endpnt_t reaper thread */
1370Sstevel@tonic-gate static bool_t			taskq_created; /* flag for endpnt_taskq */
1380Sstevel@tonic-gate static kmutex_t			endpnt_taskq_lock; /* taskq lock */
1390Sstevel@tonic-gate static zone_key_t		endpnt_destructor_key;
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate #define	DEFAULT_ENDPOINT_REAP_INTERVAL 60 /* 1 minute */
1420Sstevel@tonic-gate #define	DEFAULT_INTERVAL_SHIFT 30 /* 30 seconds */
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate /*
1450Sstevel@tonic-gate  * Endpoint tunables
1460Sstevel@tonic-gate  */
1470Sstevel@tonic-gate static int	clnt_clts_max_endpoints = -1;
1480Sstevel@tonic-gate static int	clnt_clts_hash_size = DEFAULT_HASH_SIZE;
1490Sstevel@tonic-gate static time_t	clnt_clts_endpoint_reap_interval = -1;
1500Sstevel@tonic-gate static clock_t	clnt_clts_taskq_dispatch_interval;
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate /*
1530Sstevel@tonic-gate  * Response completion hash queue
1540Sstevel@tonic-gate  */
1550Sstevel@tonic-gate static call_table_t *clts_call_ht;
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate /*
1580Sstevel@tonic-gate  * Routines for the endpoint manager
1590Sstevel@tonic-gate  */
1600Sstevel@tonic-gate static struct endpnt_type *endpnt_type_create(struct knetconfig *);
1610Sstevel@tonic-gate static void endpnt_type_free(struct endpnt_type *);
1620Sstevel@tonic-gate static int check_endpnt(struct endpnt *, struct endpnt **);
163342Snd150628 static struct endpnt *endpnt_get(struct knetconfig *, int);
1640Sstevel@tonic-gate static void endpnt_rele(struct endpnt *);
1650Sstevel@tonic-gate static void endpnt_reap_settimer(endpnt_type_t *);
1660Sstevel@tonic-gate static void endpnt_reap(endpnt_type_t *);
1670Sstevel@tonic-gate static void endpnt_reap_dispatch(void *);
1680Sstevel@tonic-gate static void endpnt_reclaim(zoneid_t);
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate /*
1720Sstevel@tonic-gate  * Request dipatching function.
1730Sstevel@tonic-gate  */
1740Sstevel@tonic-gate static int clnt_clts_dispatch_send(queue_t *q, mblk_t *, struct netbuf *addr,
1758778SErik.Nordmark@Sun.COM 					calllist_t *, uint_t, cred_t *);
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate /*
1780Sstevel@tonic-gate  * The size of the preserialized RPC header information.
1790Sstevel@tonic-gate  */
1800Sstevel@tonic-gate #define	CKU_HDRSIZE	20
1810Sstevel@tonic-gate /*
1820Sstevel@tonic-gate  * The initial allocation size.  It is small to reduce space requirements.
1830Sstevel@tonic-gate  */
1840Sstevel@tonic-gate #define	CKU_INITSIZE	2048
1850Sstevel@tonic-gate /*
1860Sstevel@tonic-gate  * The size of additional allocations, if required.  It is larger to
1870Sstevel@tonic-gate  * reduce the number of actual allocations.
1880Sstevel@tonic-gate  */
1890Sstevel@tonic-gate #define	CKU_ALLOCSIZE	8192
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate /*
1920Sstevel@tonic-gate  * Private data per rpc handle.  This structure is allocated by
1930Sstevel@tonic-gate  * clnt_clts_kcreate, and freed by clnt_clts_kdestroy.
1940Sstevel@tonic-gate  */
1950Sstevel@tonic-gate struct cku_private {
1960Sstevel@tonic-gate 	CLIENT			 cku_client;	/* client handle */
1970Sstevel@tonic-gate 	int			 cku_retrys;	/* request retrys */
1980Sstevel@tonic-gate 	calllist_t		 cku_call;
1990Sstevel@tonic-gate 	struct endpnt		*cku_endpnt;	/* open end point */
2000Sstevel@tonic-gate 	struct knetconfig	 cku_config;
2010Sstevel@tonic-gate 	struct netbuf		 cku_addr;	/* remote address */
2020Sstevel@tonic-gate 	struct rpc_err		 cku_err;	/* error status */
2030Sstevel@tonic-gate 	XDR			 cku_outxdr;	/* xdr stream for output */
2040Sstevel@tonic-gate 	XDR			 cku_inxdr;	/* xdr stream for input */
2050Sstevel@tonic-gate 	char			 cku_rpchdr[CKU_HDRSIZE + 4]; /* rpc header */
2060Sstevel@tonic-gate 	struct cred		*cku_cred;	/* credentials */
2070Sstevel@tonic-gate 	struct rpc_timers	*cku_timers;	/* for estimating RTT */
2080Sstevel@tonic-gate 	struct rpc_timers	*cku_timeall;	/* for estimating RTT */
2090Sstevel@tonic-gate 	void			 (*cku_feedback)(int, int, caddr_t);
2100Sstevel@tonic-gate 						/* ptr to feedback rtn */
2110Sstevel@tonic-gate 	caddr_t			 cku_feedarg;	/* argument for feedback func */
2120Sstevel@tonic-gate 	uint32_t		 cku_xid;	/* current XID */
2130Sstevel@tonic-gate 	bool_t			 cku_bcast;	/* RPC broadcast hint */
214342Snd150628 	int			cku_useresvport; /* Use reserved port */
2150Sstevel@tonic-gate 	struct rpc_clts_client	*cku_stats;	/* counters for the zone */
2160Sstevel@tonic-gate };
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate static const struct rpc_clts_client {
2190Sstevel@tonic-gate 	kstat_named_t	rccalls;
2200Sstevel@tonic-gate 	kstat_named_t	rcbadcalls;
2210Sstevel@tonic-gate 	kstat_named_t	rcretrans;
2220Sstevel@tonic-gate 	kstat_named_t	rcbadxids;
2230Sstevel@tonic-gate 	kstat_named_t	rctimeouts;
2240Sstevel@tonic-gate 	kstat_named_t	rcnewcreds;
2250Sstevel@tonic-gate 	kstat_named_t	rcbadverfs;
2260Sstevel@tonic-gate 	kstat_named_t	rctimers;
2270Sstevel@tonic-gate 	kstat_named_t	rcnomem;
2280Sstevel@tonic-gate 	kstat_named_t	rccantsend;
2290Sstevel@tonic-gate } clts_rcstat_tmpl = {
2300Sstevel@tonic-gate 	{ "calls",	KSTAT_DATA_UINT64 },
2310Sstevel@tonic-gate 	{ "badcalls",	KSTAT_DATA_UINT64 },
2320Sstevel@tonic-gate 	{ "retrans",	KSTAT_DATA_UINT64 },
2330Sstevel@tonic-gate 	{ "badxids",	KSTAT_DATA_UINT64 },
2340Sstevel@tonic-gate 	{ "timeouts",	KSTAT_DATA_UINT64 },
2350Sstevel@tonic-gate 	{ "newcreds",	KSTAT_DATA_UINT64 },
2360Sstevel@tonic-gate 	{ "badverfs",	KSTAT_DATA_UINT64 },
2370Sstevel@tonic-gate 	{ "timers",	KSTAT_DATA_UINT64 },
2380Sstevel@tonic-gate 	{ "nomem",	KSTAT_DATA_UINT64 },
2390Sstevel@tonic-gate 	{ "cantsend",	KSTAT_DATA_UINT64 },
2400Sstevel@tonic-gate };
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate static uint_t clts_rcstat_ndata =
2430Sstevel@tonic-gate 	sizeof (clts_rcstat_tmpl) / sizeof (kstat_named_t);
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate #define	RCSTAT_INCR(s, x)			\
2460Sstevel@tonic-gate 	atomic_add_64(&(s)->x.value.ui64, 1)
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate #define	ptoh(p)		(&((p)->cku_client))
2490Sstevel@tonic-gate #define	htop(h)		((struct cku_private *)((h)->cl_private))
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate /*
2520Sstevel@tonic-gate  * Times to retry
2530Sstevel@tonic-gate  */
2540Sstevel@tonic-gate #define	SNDTRIES	4
2550Sstevel@tonic-gate #define	REFRESHES	2	/* authentication refreshes */
2560Sstevel@tonic-gate 
257681Srg137905 /*
258681Srg137905  * The following is used to determine the global default behavior for
259681Srg137905  * CLTS when binding to a local port.
260681Srg137905  *
261681Srg137905  * If the value is set to 1 the default will be to select a reserved
262681Srg137905  * (aka privileged) port, if the value is zero the default will be to
263681Srg137905  * use non-reserved ports.  Users of kRPC may override this by using
264681Srg137905  * CLNT_CONTROL() and CLSET_BINDRESVPORT.
265681Srg137905  */
266681Srg137905 static int clnt_clts_do_bindresvport = 1;
267681Srg137905 
2680Sstevel@tonic-gate #define	BINDRESVPORT_RETRIES 5
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate void
clnt_clts_stats_init(zoneid_t zoneid,struct rpc_clts_client ** statsp)2710Sstevel@tonic-gate clnt_clts_stats_init(zoneid_t zoneid, struct rpc_clts_client **statsp)
2720Sstevel@tonic-gate {
2730Sstevel@tonic-gate 	kstat_t *ksp;
2740Sstevel@tonic-gate 	kstat_named_t *knp;
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 	knp = rpcstat_zone_init_common(zoneid, "unix", "rpc_clts_client",
2770Sstevel@tonic-gate 	    (const kstat_named_t *)&clts_rcstat_tmpl,
2780Sstevel@tonic-gate 	    sizeof (clts_rcstat_tmpl));
2790Sstevel@tonic-gate 	/*
2800Sstevel@tonic-gate 	 * Backwards compatibility for old kstat clients
2810Sstevel@tonic-gate 	 */
2820Sstevel@tonic-gate 	ksp = kstat_create_zone("unix", 0, "rpc_client", "rpc",
2830Sstevel@tonic-gate 	    KSTAT_TYPE_NAMED, clts_rcstat_ndata,
2840Sstevel@tonic-gate 	    KSTAT_FLAG_VIRTUAL | KSTAT_FLAG_WRITABLE, zoneid);
2850Sstevel@tonic-gate 	if (ksp) {
2860Sstevel@tonic-gate 		ksp->ks_data = knp;
2870Sstevel@tonic-gate 		kstat_install(ksp);
2880Sstevel@tonic-gate 	}
2890Sstevel@tonic-gate 	*statsp = (struct rpc_clts_client *)knp;
2900Sstevel@tonic-gate }
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate void
clnt_clts_stats_fini(zoneid_t zoneid,struct rpc_clts_client ** statsp)2930Sstevel@tonic-gate clnt_clts_stats_fini(zoneid_t zoneid, struct rpc_clts_client **statsp)
2940Sstevel@tonic-gate {
2950Sstevel@tonic-gate 	rpcstat_zone_fini_common(zoneid, "unix", "rpc_clts_client");
2960Sstevel@tonic-gate 	kstat_delete_byname_zone("unix", 0, "rpc_client", zoneid);
2970Sstevel@tonic-gate 	kmem_free(*statsp, sizeof (clts_rcstat_tmpl));
2980Sstevel@tonic-gate }
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate /*
3010Sstevel@tonic-gate  * Create an rpc handle for a clts rpc connection.
3020Sstevel@tonic-gate  * Allocates space for the handle structure and the private data.
3030Sstevel@tonic-gate  */
3040Sstevel@tonic-gate /* ARGSUSED */
3050Sstevel@tonic-gate int
clnt_clts_kcreate(struct knetconfig * config,struct netbuf * addr,rpcprog_t pgm,rpcvers_t vers,int retrys,struct cred * cred,CLIENT ** cl)3060Sstevel@tonic-gate clnt_clts_kcreate(struct knetconfig *config, struct netbuf *addr,
3070Sstevel@tonic-gate 	rpcprog_t pgm, rpcvers_t vers, int retrys, struct cred *cred,
3080Sstevel@tonic-gate 	CLIENT **cl)
3090Sstevel@tonic-gate {
3100Sstevel@tonic-gate 	CLIENT *h;
3110Sstevel@tonic-gate 	struct cku_private *p;
3120Sstevel@tonic-gate 	struct rpc_msg call_msg;
3130Sstevel@tonic-gate 	int error;
3140Sstevel@tonic-gate 	int plen;
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate 	if (cl == NULL)
3170Sstevel@tonic-gate 		return (EINVAL);
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate 	*cl = NULL;
3200Sstevel@tonic-gate 	error = 0;
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 	p = kmem_zalloc(sizeof (*p), KM_SLEEP);
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 	h = ptoh(p);
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 	/* handle */
3270Sstevel@tonic-gate 	h->cl_ops = &clts_ops;
3280Sstevel@tonic-gate 	h->cl_private = (caddr_t)p;
3290Sstevel@tonic-gate 	h->cl_auth = authkern_create();
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 	/* call message, just used to pre-serialize below */
3320Sstevel@tonic-gate 	call_msg.rm_xid = 0;
3330Sstevel@tonic-gate 	call_msg.rm_direction = CALL;
3340Sstevel@tonic-gate 	call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
3350Sstevel@tonic-gate 	call_msg.rm_call.cb_prog = pgm;
3360Sstevel@tonic-gate 	call_msg.rm_call.cb_vers = vers;
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate 	/* private */
3390Sstevel@tonic-gate 	clnt_clts_kinit(h, addr, retrys, cred);
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 	xdrmem_create(&p->cku_outxdr, p->cku_rpchdr, CKU_HDRSIZE, XDR_ENCODE);
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	/* pre-serialize call message header */
3440Sstevel@tonic-gate 	if (!xdr_callhdr(&p->cku_outxdr, &call_msg)) {
3450Sstevel@tonic-gate 		error = EINVAL;		/* XXX */
3460Sstevel@tonic-gate 		goto bad;
3470Sstevel@tonic-gate 	}
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 	p->cku_config.knc_rdev = config->knc_rdev;
3500Sstevel@tonic-gate 	p->cku_config.knc_semantics = config->knc_semantics;
3510Sstevel@tonic-gate 	plen = strlen(config->knc_protofmly) + 1;
3520Sstevel@tonic-gate 	p->cku_config.knc_protofmly = kmem_alloc(plen, KM_SLEEP);
3530Sstevel@tonic-gate 	bcopy(config->knc_protofmly, p->cku_config.knc_protofmly, plen);
354342Snd150628 	p->cku_useresvport = -1; /* value is has not been set */
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 	cv_init(&p->cku_call.call_cv, NULL, CV_DEFAULT, NULL);
3570Sstevel@tonic-gate 	mutex_init(&p->cku_call.call_lock, NULL, MUTEX_DEFAULT, NULL);
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 	*cl = h;
3600Sstevel@tonic-gate 	return (0);
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate bad:
3630Sstevel@tonic-gate 	auth_destroy(h->cl_auth);
3640Sstevel@tonic-gate 	kmem_free(p->cku_addr.buf, addr->maxlen);
3650Sstevel@tonic-gate 	kmem_free(p, sizeof (struct cku_private));
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate 	return (error);
3680Sstevel@tonic-gate }
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate void
clnt_clts_kinit(CLIENT * h,struct netbuf * addr,int retrys,cred_t * cred)3710Sstevel@tonic-gate clnt_clts_kinit(CLIENT *h, struct netbuf *addr, int retrys, cred_t *cred)
3720Sstevel@tonic-gate {
3730Sstevel@tonic-gate 	/* LINTED pointer alignment */
3740Sstevel@tonic-gate 	struct cku_private *p = htop(h);
3750Sstevel@tonic-gate 	struct rpcstat *rsp;
3760Sstevel@tonic-gate 
377766Scarlsonj 	rsp = zone_getspecific(rpcstat_zone_key, rpc_zone());
3780Sstevel@tonic-gate 	ASSERT(rsp != NULL);
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 	p->cku_retrys = retrys;
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 	if (p->cku_addr.maxlen < addr->len) {
3830Sstevel@tonic-gate 		if (p->cku_addr.maxlen != 0 && p->cku_addr.buf != NULL)
3840Sstevel@tonic-gate 			kmem_free(p->cku_addr.buf, p->cku_addr.maxlen);
3850Sstevel@tonic-gate 
3860Sstevel@tonic-gate 		p->cku_addr.buf = kmem_zalloc(addr->maxlen, KM_SLEEP);
3870Sstevel@tonic-gate 		p->cku_addr.maxlen = addr->maxlen;
3880Sstevel@tonic-gate 	}
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 	p->cku_addr.len = addr->len;
3910Sstevel@tonic-gate 	bcopy(addr->buf, p->cku_addr.buf, addr->len);
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate 	p->cku_cred = cred;
3940Sstevel@tonic-gate 	p->cku_xid = 0;
3950Sstevel@tonic-gate 	p->cku_timers = NULL;
3960Sstevel@tonic-gate 	p->cku_timeall = NULL;
3970Sstevel@tonic-gate 	p->cku_feedback = NULL;
3980Sstevel@tonic-gate 	p->cku_bcast = FALSE;
3990Sstevel@tonic-gate 	p->cku_call.call_xid = 0;
4000Sstevel@tonic-gate 	p->cku_call.call_hash = 0;
4010Sstevel@tonic-gate 	p->cku_call.call_notified = FALSE;
4020Sstevel@tonic-gate 	p->cku_call.call_next = NULL;
4030Sstevel@tonic-gate 	p->cku_call.call_prev = NULL;
4040Sstevel@tonic-gate 	p->cku_call.call_reply = NULL;
4050Sstevel@tonic-gate 	p->cku_call.call_wq = NULL;
4060Sstevel@tonic-gate 	p->cku_stats = rsp->rpc_clts_client;
4070Sstevel@tonic-gate }
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate /*
4100Sstevel@tonic-gate  * set the timers.  Return current retransmission timeout.
4110Sstevel@tonic-gate  */
4120Sstevel@tonic-gate static int
clnt_clts_ksettimers(CLIENT * h,struct rpc_timers * t,struct rpc_timers * all,int minimum,void (* feedback)(int,int,caddr_t),caddr_t arg,uint32_t xid)4130Sstevel@tonic-gate clnt_clts_ksettimers(CLIENT *h, struct rpc_timers *t, struct rpc_timers *all,
4140Sstevel@tonic-gate 	int minimum, void (*feedback)(int, int, caddr_t), caddr_t arg,
4150Sstevel@tonic-gate 	uint32_t xid)
4160Sstevel@tonic-gate {
4170Sstevel@tonic-gate 	/* LINTED pointer alignment */
4180Sstevel@tonic-gate 	struct cku_private *p = htop(h);
4190Sstevel@tonic-gate 	int value;
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 	p->cku_feedback = feedback;
4220Sstevel@tonic-gate 	p->cku_feedarg = arg;
4230Sstevel@tonic-gate 	p->cku_timers = t;
4240Sstevel@tonic-gate 	p->cku_timeall = all;
4250Sstevel@tonic-gate 	if (xid)
4260Sstevel@tonic-gate 		p->cku_xid = xid;
4270Sstevel@tonic-gate 	value = all->rt_rtxcur;
4280Sstevel@tonic-gate 	value += t->rt_rtxcur;
4290Sstevel@tonic-gate 	if (value < minimum)
4300Sstevel@tonic-gate 		return (minimum);
4310Sstevel@tonic-gate 	RCSTAT_INCR(p->cku_stats, rctimers);
4320Sstevel@tonic-gate 	return (value);
4330Sstevel@tonic-gate }
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate /*
4360Sstevel@tonic-gate  * Time out back off function. tim is in HZ
4370Sstevel@tonic-gate  */
4380Sstevel@tonic-gate #define	MAXTIMO	(20 * hz)
4390Sstevel@tonic-gate #define	backoff(tim)	(((tim) < MAXTIMO) ? dobackoff(tim) : (tim))
4400Sstevel@tonic-gate #define	dobackoff(tim)	((((tim) << 1) > MAXTIMO) ? MAXTIMO : ((tim) << 1))
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate #define	RETRY_POLL_TIMO	30
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate /*
4450Sstevel@tonic-gate  * Call remote procedure.
4460Sstevel@tonic-gate  * Most of the work of rpc is done here.  We serialize what is left
4470Sstevel@tonic-gate  * of the header (some was pre-serialized in the handle), serialize
4480Sstevel@tonic-gate  * the arguments, and send it off.  We wait for a reply or a time out.
4490Sstevel@tonic-gate  * Timeout causes an immediate return, other packet problems may cause
4500Sstevel@tonic-gate  * a retry on the receive.  When a good packet is received we deserialize
4510Sstevel@tonic-gate  * it, and check verification.  A bad reply code will cause one retry
4520Sstevel@tonic-gate  * with full (longhand) credentials.
4530Sstevel@tonic-gate  */
4540Sstevel@tonic-gate enum clnt_stat
clnt_clts_kcallit_addr(CLIENT * h,rpcproc_t procnum,xdrproc_t xdr_args,caddr_t argsp,xdrproc_t xdr_results,caddr_t resultsp,struct timeval wait,struct netbuf * sin)4550Sstevel@tonic-gate clnt_clts_kcallit_addr(CLIENT *h, rpcproc_t procnum, xdrproc_t xdr_args,
4560Sstevel@tonic-gate 	caddr_t argsp, xdrproc_t xdr_results, caddr_t resultsp,
4570Sstevel@tonic-gate 	struct timeval wait, struct netbuf *sin)
4580Sstevel@tonic-gate {
4590Sstevel@tonic-gate 	/* LINTED pointer alignment */
4600Sstevel@tonic-gate 	struct cku_private *p = htop(h);
4610Sstevel@tonic-gate 	XDR *xdrs;
4620Sstevel@tonic-gate 	int stries = p->cku_retrys;
4630Sstevel@tonic-gate 	int refreshes = REFRESHES;	/* number of times to refresh cred */
4640Sstevel@tonic-gate 	int round_trip;			/* time the RPC */
4650Sstevel@tonic-gate 	int error;
4660Sstevel@tonic-gate 	mblk_t *mp;
4670Sstevel@tonic-gate 	mblk_t *mpdup;
4680Sstevel@tonic-gate 	mblk_t *resp = NULL;
4690Sstevel@tonic-gate 	mblk_t *tmp;
4700Sstevel@tonic-gate 	calllist_t *call = &p->cku_call;
4718766Sdai.ngo@sun.com 	clock_t ori_timout, timout;
4720Sstevel@tonic-gate 	bool_t interrupted;
4730Sstevel@tonic-gate 	enum clnt_stat status;
4740Sstevel@tonic-gate 	struct rpc_msg reply_msg;
4750Sstevel@tonic-gate 	enum clnt_stat re_status;
476342Snd150628 	endpnt_t *endpt;
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 	RCSTAT_INCR(p->cku_stats, rccalls);
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 	RPCLOG(2, "clnt_clts_kcallit_addr: wait.tv_sec: %ld\n", wait.tv_sec);
4810Sstevel@tonic-gate 	RPCLOG(2, "clnt_clts_kcallit_addr: wait.tv_usec: %ld\n", wait.tv_usec);
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate 	timout = TIMEVAL_TO_TICK(&wait);
4848766Sdai.ngo@sun.com 	ori_timout = timout;
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate 	if (p->cku_xid == 0) {
4870Sstevel@tonic-gate 		p->cku_xid = alloc_xid();
4880Sstevel@tonic-gate 		if (p->cku_endpnt != NULL)
4890Sstevel@tonic-gate 			endpnt_rele(p->cku_endpnt);
4900Sstevel@tonic-gate 		p->cku_endpnt = NULL;
4910Sstevel@tonic-gate 	}
4926403Sgt29601 	call->call_zoneid = rpc_zoneid();
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 	mpdup = NULL;
4950Sstevel@tonic-gate call_again:
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate 	if (mpdup == NULL) {
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 		while ((mp = allocb(CKU_INITSIZE, BPRI_LO)) == NULL) {
5000Sstevel@tonic-gate 			if (strwaitbuf(CKU_INITSIZE, BPRI_LO)) {
5010Sstevel@tonic-gate 				p->cku_err.re_status = RPC_SYSTEMERROR;
5020Sstevel@tonic-gate 				p->cku_err.re_errno = ENOSR;
5030Sstevel@tonic-gate 				goto done;
5040Sstevel@tonic-gate 			}
5050Sstevel@tonic-gate 		}
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 		xdrs = &p->cku_outxdr;
5080Sstevel@tonic-gate 		xdrmblk_init(xdrs, mp, XDR_ENCODE, CKU_ALLOCSIZE);
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate 		if (h->cl_auth->ah_cred.oa_flavor != RPCSEC_GSS) {
5110Sstevel@tonic-gate 			/*
5120Sstevel@tonic-gate 			 * Copy in the preserialized RPC header
5130Sstevel@tonic-gate 			 * information.
5140Sstevel@tonic-gate 			 */
5150Sstevel@tonic-gate 			bcopy(p->cku_rpchdr, mp->b_rptr, CKU_HDRSIZE);
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 			/*
5180Sstevel@tonic-gate 			 * transaction id is the 1st thing in the output
5190Sstevel@tonic-gate 			 * buffer.
5200Sstevel@tonic-gate 			 */
5210Sstevel@tonic-gate 			/* LINTED pointer alignment */
5220Sstevel@tonic-gate 			(*(uint32_t *)(mp->b_rptr)) = p->cku_xid;
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate 			/* Skip the preserialized stuff. */
5250Sstevel@tonic-gate 			XDR_SETPOS(xdrs, CKU_HDRSIZE);
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 			/* Serialize dynamic stuff into the output buffer. */
5280Sstevel@tonic-gate 			if ((!XDR_PUTINT32(xdrs, (int32_t *)&procnum)) ||
5290Sstevel@tonic-gate 			    (!AUTH_MARSHALL(h->cl_auth, xdrs, p->cku_cred)) ||
5300Sstevel@tonic-gate 			    (!(*xdr_args)(xdrs, argsp))) {
5310Sstevel@tonic-gate 				freemsg(mp);
5320Sstevel@tonic-gate 				p->cku_err.re_status = RPC_CANTENCODEARGS;
5330Sstevel@tonic-gate 				p->cku_err.re_errno = EIO;
5340Sstevel@tonic-gate 				goto done;
5350Sstevel@tonic-gate 			}
5360Sstevel@tonic-gate 		} else {
5370Sstevel@tonic-gate 			uint32_t *uproc = (uint32_t *)
5380Sstevel@tonic-gate 			    &p->cku_rpchdr[CKU_HDRSIZE];
5390Sstevel@tonic-gate 			IXDR_PUT_U_INT32(uproc, procnum);
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate 			(*(uint32_t *)(&p->cku_rpchdr[0])) = p->cku_xid;
5420Sstevel@tonic-gate 			XDR_SETPOS(xdrs, 0);
5430Sstevel@tonic-gate 
5440Sstevel@tonic-gate 			/* Serialize the procedure number and the arguments. */
5450Sstevel@tonic-gate 			if (!AUTH_WRAP(h->cl_auth, (caddr_t)p->cku_rpchdr,
5460Sstevel@tonic-gate 			    CKU_HDRSIZE+4, xdrs, xdr_args, argsp)) {
5470Sstevel@tonic-gate 				freemsg(mp);
5480Sstevel@tonic-gate 				p->cku_err.re_status = RPC_CANTENCODEARGS;
5490Sstevel@tonic-gate 				p->cku_err.re_errno = EIO;
5500Sstevel@tonic-gate 				goto done;
5510Sstevel@tonic-gate 			}
5520Sstevel@tonic-gate 		}
5530Sstevel@tonic-gate 	} else
5540Sstevel@tonic-gate 		mp = mpdup;
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate 	mpdup = dupmsg(mp);
5570Sstevel@tonic-gate 	if (mpdup == NULL) {
5580Sstevel@tonic-gate 		freemsg(mp);
5590Sstevel@tonic-gate 		p->cku_err.re_status = RPC_SYSTEMERROR;
5600Sstevel@tonic-gate 		p->cku_err.re_errno = ENOSR;
5610Sstevel@tonic-gate 		goto done;
5620Sstevel@tonic-gate 	}
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate 	/*
5650Sstevel@tonic-gate 	 * Grab an endpnt only if the endpoint is NULL.  We could be retrying
5660Sstevel@tonic-gate 	 * the request and in this case we want to go through the same
5670Sstevel@tonic-gate 	 * source port, so that the duplicate request cache may detect a
5680Sstevel@tonic-gate 	 * retry.
5690Sstevel@tonic-gate 	 */
570342Snd150628 
5710Sstevel@tonic-gate 	if (p->cku_endpnt == NULL)
572342Snd150628 		p->cku_endpnt = endpnt_get(&p->cku_config, p->cku_useresvport);
5730Sstevel@tonic-gate 
5740Sstevel@tonic-gate 	if (p->cku_endpnt == NULL) {
5750Sstevel@tonic-gate 		freemsg(mp);
5760Sstevel@tonic-gate 		p->cku_err.re_status = RPC_SYSTEMERROR;
5770Sstevel@tonic-gate 		p->cku_err.re_errno = ENOSR;
5780Sstevel@tonic-gate 		goto done;
5790Sstevel@tonic-gate 	}
5800Sstevel@tonic-gate 
58111066Srafael.vanoni@sun.com 	round_trip = ddi_get_lbolt();
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 	error = clnt_clts_dispatch_send(p->cku_endpnt->e_wq, mp,
5848778SErik.Nordmark@Sun.COM 	    &p->cku_addr, call, p->cku_xid, p->cku_cred);
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate 	if (error != 0) {
5870Sstevel@tonic-gate 		freemsg(mp);
5880Sstevel@tonic-gate 		p->cku_err.re_status = RPC_CANTSEND;
5890Sstevel@tonic-gate 		p->cku_err.re_errno = error;
5900Sstevel@tonic-gate 		RCSTAT_INCR(p->cku_stats, rccantsend);
5910Sstevel@tonic-gate 		goto done1;
5920Sstevel@tonic-gate 	}
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate 	RPCLOG(64, "clnt_clts_kcallit_addr: sent call for xid 0x%x\n",
5956403Sgt29601 	    p->cku_xid);
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate 	/*
5980Sstevel@tonic-gate 	 * There are two reasons for which we go back to to tryread.
5990Sstevel@tonic-gate 	 *
6000Sstevel@tonic-gate 	 * a) In case the status is RPC_PROCUNAVAIL and we sent out a
6010Sstevel@tonic-gate 	 *    broadcast we should not get any invalid messages with the
6020Sstevel@tonic-gate 	 *    RPC_PROCUNAVAIL error back. Some broken RPC implementations
6030Sstevel@tonic-gate 	 *    send them and for this we have to ignore them ( as we would
6040Sstevel@tonic-gate 	 *    have never received them ) and look for another message
6050Sstevel@tonic-gate 	 *    which might contain the valid response because we don't know
6060Sstevel@tonic-gate 	 *    how many broken implementations are in the network. So we are
6070Sstevel@tonic-gate 	 *    going to loop until
6080Sstevel@tonic-gate 	 *    - we received a valid response
6090Sstevel@tonic-gate 	 *    - we have processed all invalid responses and
6100Sstevel@tonic-gate 	 *	got a time out when we try to receive again a
6110Sstevel@tonic-gate 	 *	message.
6120Sstevel@tonic-gate 	 *
6130Sstevel@tonic-gate 	 * b) We will jump back to tryread also in case we failed
6140Sstevel@tonic-gate 	 *    within the AUTH_VALIDATE. In this case we should move
6150Sstevel@tonic-gate 	 *    on and loop until we received a valid response or we
6160Sstevel@tonic-gate 	 *    have processed all responses with broken authentication
6170Sstevel@tonic-gate 	 *    and we got a time out when we try to receive a message.
6180Sstevel@tonic-gate 	 */
6190Sstevel@tonic-gate tryread:
6200Sstevel@tonic-gate 	mutex_enter(&call->call_lock);
6210Sstevel@tonic-gate 	interrupted = FALSE;
6220Sstevel@tonic-gate 	if (call->call_notified == FALSE) {
6230Sstevel@tonic-gate 		klwp_t *lwp = ttolwp(curthread);
6240Sstevel@tonic-gate 		clock_t cv_wait_ret = 1; /* init to > 0 */
6250Sstevel@tonic-gate 		clock_t cv_timout = timout;
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 		if (lwp != NULL)
6280Sstevel@tonic-gate 			lwp->lwp_nostop++;
6290Sstevel@tonic-gate 
63011066Srafael.vanoni@sun.com 		cv_timout += ddi_get_lbolt();
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate 		if (h->cl_nosignal)
6330Sstevel@tonic-gate 			while ((cv_wait_ret =
6346403Sgt29601 			    cv_timedwait(&call->call_cv,
6356403Sgt29601 			    &call->call_lock, cv_timout)) > 0 &&
6366403Sgt29601 			    call->call_notified == FALSE)
6376403Sgt29601 				;
6380Sstevel@tonic-gate 		else
6390Sstevel@tonic-gate 			while ((cv_wait_ret =
6406403Sgt29601 			    cv_timedwait_sig(&call->call_cv,
6416403Sgt29601 			    &call->call_lock, cv_timout)) > 0 &&
6426403Sgt29601 			    call->call_notified == FALSE)
6436403Sgt29601 				;
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate 		if (cv_wait_ret == 0)
6460Sstevel@tonic-gate 			interrupted = TRUE;
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate 		if (lwp != NULL)
6490Sstevel@tonic-gate 			lwp->lwp_nostop--;
6500Sstevel@tonic-gate 	}
6510Sstevel@tonic-gate 	resp = call->call_reply;
6520Sstevel@tonic-gate 	call->call_reply = NULL;
6530Sstevel@tonic-gate 	status = call->call_status;
6540Sstevel@tonic-gate 	/*
6550Sstevel@tonic-gate 	 * We have to reset the call_notified here. In case we have
6560Sstevel@tonic-gate 	 * to do a retry ( e.g. in case we got a RPC_PROCUNAVAIL
6570Sstevel@tonic-gate 	 * error ) we need to set this to false to ensure that
6580Sstevel@tonic-gate 	 * we will wait for the next message. When the next message
6590Sstevel@tonic-gate 	 * is going to arrive the function clnt_clts_dispatch_notify
6600Sstevel@tonic-gate 	 * will set this to true again.
6610Sstevel@tonic-gate 	 */
6620Sstevel@tonic-gate 	call->call_notified = FALSE;
663*11380SMarcel.Telka@Sun.COM 	call->call_status = RPC_TIMEDOUT;
6640Sstevel@tonic-gate 	mutex_exit(&call->call_lock);
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate 	if (status == RPC_TIMEDOUT) {
6670Sstevel@tonic-gate 		if (interrupted) {
6680Sstevel@tonic-gate 			/*
6690Sstevel@tonic-gate 			 * We got interrupted, bail out
6700Sstevel@tonic-gate 			 */
6710Sstevel@tonic-gate 			p->cku_err.re_status = RPC_INTR;
6720Sstevel@tonic-gate 			p->cku_err.re_errno = EINTR;
6730Sstevel@tonic-gate 			goto done1;
6740Sstevel@tonic-gate 		} else {
6750Sstevel@tonic-gate 			RPCLOG(8, "clnt_clts_kcallit_addr: "
6766403Sgt29601 			    "request w/xid 0x%x timedout "
6776403Sgt29601 			    "waiting for reply\n", p->cku_xid);
6780Sstevel@tonic-gate #if 0 /* XXX not yet */
6790Sstevel@tonic-gate 			/*
6800Sstevel@tonic-gate 			 * Timeout may be due to a dead gateway. Send
6810Sstevel@tonic-gate 			 * an ioctl downstream advising deletion of
6820Sstevel@tonic-gate 			 * route when we reach the half-way point to
6830Sstevel@tonic-gate 			 * timing out.
6840Sstevel@tonic-gate 			 */
6850Sstevel@tonic-gate 			if (stries == p->cku_retrys/2) {
6860Sstevel@tonic-gate 				t_kadvise(p->cku_endpnt->e_tiptr,
6876403Sgt29601 				    (uchar_t *)p->cku_addr.buf,
6886403Sgt29601 				    p->cku_addr.len);
6890Sstevel@tonic-gate 			}
6900Sstevel@tonic-gate #endif /* not yet */
6910Sstevel@tonic-gate 			p->cku_err.re_status = RPC_TIMEDOUT;
6920Sstevel@tonic-gate 			p->cku_err.re_errno = ETIMEDOUT;
6930Sstevel@tonic-gate 			RCSTAT_INCR(p->cku_stats, rctimeouts);
6940Sstevel@tonic-gate 			goto done1;
6950Sstevel@tonic-gate 		}
6960Sstevel@tonic-gate 	}
6970Sstevel@tonic-gate 
698*11380SMarcel.Telka@Sun.COM 	ASSERT(resp != NULL);
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate 	/*
7010Sstevel@tonic-gate 	 * Prepare the message for further processing.  We need to remove
7020Sstevel@tonic-gate 	 * the datagram header and copy the source address if necessary.  No
7030Sstevel@tonic-gate 	 * need to verify the header since rpcmod took care of that.
7040Sstevel@tonic-gate 	 */
7050Sstevel@tonic-gate 	/*
7060Sstevel@tonic-gate 	 * Copy the source address if the caller has supplied a netbuf.
7070Sstevel@tonic-gate 	 */
7080Sstevel@tonic-gate 	if (sin != NULL) {
7090Sstevel@tonic-gate 		union T_primitives *pptr;
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate 		pptr = (union T_primitives *)resp->b_rptr;
7120Sstevel@tonic-gate 		bcopy(resp->b_rptr + pptr->unitdata_ind.SRC_offset, sin->buf,
7136403Sgt29601 		    pptr->unitdata_ind.SRC_length);
7140Sstevel@tonic-gate 		sin->len = pptr->unitdata_ind.SRC_length;
7150Sstevel@tonic-gate 	}
7160Sstevel@tonic-gate 
7170Sstevel@tonic-gate 	/*
7180Sstevel@tonic-gate 	 * Pop off the datagram header.
719*11380SMarcel.Telka@Sun.COM 	 * It was retained in rpcmodrput().
7200Sstevel@tonic-gate 	 */
721*11380SMarcel.Telka@Sun.COM 	tmp = resp;
722*11380SMarcel.Telka@Sun.COM 	resp = resp->b_cont;
723*11380SMarcel.Telka@Sun.COM 	tmp->b_cont = NULL;
724*11380SMarcel.Telka@Sun.COM 	freeb(tmp);
7250Sstevel@tonic-gate 
72611066Srafael.vanoni@sun.com 	round_trip = ddi_get_lbolt() - round_trip;
7270Sstevel@tonic-gate 	/*
7280Sstevel@tonic-gate 	 * Van Jacobson timer algorithm here, only if NOT a retransmission.
7290Sstevel@tonic-gate 	 */
7300Sstevel@tonic-gate 	if (p->cku_timers != NULL && stries == p->cku_retrys) {
7310Sstevel@tonic-gate 		int rt;
7320Sstevel@tonic-gate 
7330Sstevel@tonic-gate 		rt = round_trip;
7340Sstevel@tonic-gate 		rt -= (p->cku_timers->rt_srtt >> 3);
7350Sstevel@tonic-gate 		p->cku_timers->rt_srtt += rt;
7360Sstevel@tonic-gate 		if (rt < 0)
7370Sstevel@tonic-gate 			rt = - rt;
7380Sstevel@tonic-gate 		rt -= (p->cku_timers->rt_deviate >> 2);
7390Sstevel@tonic-gate 		p->cku_timers->rt_deviate += rt;
7400Sstevel@tonic-gate 		p->cku_timers->rt_rtxcur =
7410Sstevel@tonic-gate 		    (clock_t)((p->cku_timers->rt_srtt >> 2) +
7420Sstevel@tonic-gate 		    p->cku_timers->rt_deviate) >> 1;
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate 		rt = round_trip;
7450Sstevel@tonic-gate 		rt -= (p->cku_timeall->rt_srtt >> 3);
7460Sstevel@tonic-gate 		p->cku_timeall->rt_srtt += rt;
7470Sstevel@tonic-gate 		if (rt < 0)
7480Sstevel@tonic-gate 			rt = - rt;
7490Sstevel@tonic-gate 		rt -= (p->cku_timeall->rt_deviate >> 2);
7500Sstevel@tonic-gate 		p->cku_timeall->rt_deviate += rt;
7510Sstevel@tonic-gate 		p->cku_timeall->rt_rtxcur =
7520Sstevel@tonic-gate 		    (clock_t)((p->cku_timeall->rt_srtt >> 2) +
7530Sstevel@tonic-gate 		    p->cku_timeall->rt_deviate) >> 1;
7540Sstevel@tonic-gate 		if (p->cku_feedback != NULL) {
7550Sstevel@tonic-gate 			(*p->cku_feedback)(FEEDBACK_OK, procnum,
7560Sstevel@tonic-gate 			    p->cku_feedarg);
7570Sstevel@tonic-gate 		}
7580Sstevel@tonic-gate 	}
7590Sstevel@tonic-gate 
7600Sstevel@tonic-gate 	/*
7610Sstevel@tonic-gate 	 * Process reply
7620Sstevel@tonic-gate 	 */
7630Sstevel@tonic-gate 	xdrs = &(p->cku_inxdr);
7640Sstevel@tonic-gate 	xdrmblk_init(xdrs, resp, XDR_DECODE, 0);
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate 	reply_msg.rm_direction = REPLY;
7670Sstevel@tonic-gate 	reply_msg.rm_reply.rp_stat = MSG_ACCEPTED;
7680Sstevel@tonic-gate 	reply_msg.acpted_rply.ar_stat = SUCCESS;
7690Sstevel@tonic-gate 	reply_msg.acpted_rply.ar_verf = _null_auth;
7700Sstevel@tonic-gate 	/*
7710Sstevel@tonic-gate 	 *  xdr_results will be done in AUTH_UNWRAP.
7720Sstevel@tonic-gate 	 */
7730Sstevel@tonic-gate 	reply_msg.acpted_rply.ar_results.where = NULL;
7740Sstevel@tonic-gate 	reply_msg.acpted_rply.ar_results.proc = xdr_void;
7750Sstevel@tonic-gate 
7760Sstevel@tonic-gate 	/*
7770Sstevel@tonic-gate 	 * Decode and validate the response.
7780Sstevel@tonic-gate 	 */
7790Sstevel@tonic-gate 	if (!xdr_replymsg(xdrs, &reply_msg)) {
7800Sstevel@tonic-gate 		p->cku_err.re_status = RPC_CANTDECODERES;
7810Sstevel@tonic-gate 		p->cku_err.re_errno = EIO;
7820Sstevel@tonic-gate 		(void) xdr_rpc_free_verifier(xdrs, &reply_msg);
7830Sstevel@tonic-gate 		goto done1;
7840Sstevel@tonic-gate 	}
7850Sstevel@tonic-gate 
7860Sstevel@tonic-gate 	_seterr_reply(&reply_msg, &(p->cku_err));
7870Sstevel@tonic-gate 
7880Sstevel@tonic-gate 	re_status = p->cku_err.re_status;
7890Sstevel@tonic-gate 	if (re_status == RPC_SUCCESS) {
7900Sstevel@tonic-gate 		/*
7910Sstevel@tonic-gate 		 * Reply is good, check auth.
7920Sstevel@tonic-gate 		 */
7930Sstevel@tonic-gate 		if (!AUTH_VALIDATE(h->cl_auth,
7946403Sgt29601 		    &reply_msg.acpted_rply.ar_verf)) {
7950Sstevel@tonic-gate 			p->cku_err.re_status = RPC_AUTHERROR;
7960Sstevel@tonic-gate 			p->cku_err.re_why = AUTH_INVALIDRESP;
7970Sstevel@tonic-gate 			RCSTAT_INCR(p->cku_stats, rcbadverfs);
7980Sstevel@tonic-gate 			(void) xdr_rpc_free_verifier(xdrs, &reply_msg);
7990Sstevel@tonic-gate 			goto tryread;
8000Sstevel@tonic-gate 		}
8010Sstevel@tonic-gate 		if (!AUTH_UNWRAP(h->cl_auth, xdrs, xdr_results, resultsp)) {
8020Sstevel@tonic-gate 			p->cku_err.re_status = RPC_CANTDECODERES;
8030Sstevel@tonic-gate 			p->cku_err.re_errno = EIO;
8040Sstevel@tonic-gate 		}
8050Sstevel@tonic-gate 		(void) xdr_rpc_free_verifier(xdrs, &reply_msg);
8060Sstevel@tonic-gate 		goto done1;
8070Sstevel@tonic-gate 	}
8080Sstevel@tonic-gate 	/* set errno in case we can't recover */
8090Sstevel@tonic-gate 	if (re_status != RPC_VERSMISMATCH &&
8106403Sgt29601 	    re_status != RPC_AUTHERROR && re_status != RPC_PROGVERSMISMATCH)
8110Sstevel@tonic-gate 		p->cku_err.re_errno = EIO;
8120Sstevel@tonic-gate 	/*
8130Sstevel@tonic-gate 	 * Determine whether or not we're doing an RPC
8140Sstevel@tonic-gate 	 * broadcast. Some server implementations don't
8150Sstevel@tonic-gate 	 * follow RFC 1050, section 7.4.2 in that they
8160Sstevel@tonic-gate 	 * don't remain silent when they see a proc
8170Sstevel@tonic-gate 	 * they don't support. Therefore we keep trying
8180Sstevel@tonic-gate 	 * to receive on RPC_PROCUNAVAIL, hoping to get
8190Sstevel@tonic-gate 	 * a valid response from a compliant server.
8200Sstevel@tonic-gate 	 */
8210Sstevel@tonic-gate 	if (re_status == RPC_PROCUNAVAIL && p->cku_bcast) {
8220Sstevel@tonic-gate 		(void) xdr_rpc_free_verifier(xdrs, &reply_msg);
8230Sstevel@tonic-gate 		goto tryread;
8240Sstevel@tonic-gate 	}
8250Sstevel@tonic-gate 	if (re_status == RPC_AUTHERROR) {
826*11380SMarcel.Telka@Sun.COM 
827*11380SMarcel.Telka@Sun.COM 		(void) xdr_rpc_free_verifier(xdrs, &reply_msg);
828*11380SMarcel.Telka@Sun.COM 		call_table_remove(call);
829*11380SMarcel.Telka@Sun.COM 		if (call->call_reply != NULL) {
830*11380SMarcel.Telka@Sun.COM 			freemsg(call->call_reply);
831*11380SMarcel.Telka@Sun.COM 			call->call_reply = NULL;
832*11380SMarcel.Telka@Sun.COM 		}
833*11380SMarcel.Telka@Sun.COM 
8340Sstevel@tonic-gate 		/*
8350Sstevel@tonic-gate 		 * Maybe our credential need to be refreshed
8360Sstevel@tonic-gate 		 */
8370Sstevel@tonic-gate 		if (refreshes > 0 &&
8380Sstevel@tonic-gate 		    AUTH_REFRESH(h->cl_auth, &reply_msg, p->cku_cred)) {
8390Sstevel@tonic-gate 			/*
8400Sstevel@tonic-gate 			 * The credential is refreshed. Try the request again.
8410Sstevel@tonic-gate 			 * Even if stries == 0, we still retry as long as
8420Sstevel@tonic-gate 			 * refreshes > 0. This prevents a soft authentication
8430Sstevel@tonic-gate 			 * error turning into a hard one at an upper level.
8440Sstevel@tonic-gate 			 */
8450Sstevel@tonic-gate 			refreshes--;
8460Sstevel@tonic-gate 			RCSTAT_INCR(p->cku_stats, rcbadcalls);
8470Sstevel@tonic-gate 			RCSTAT_INCR(p->cku_stats, rcnewcreds);
8480Sstevel@tonic-gate 
8490Sstevel@tonic-gate 			freemsg(mpdup);
850*11380SMarcel.Telka@Sun.COM 			mpdup = NULL;
8510Sstevel@tonic-gate 			freemsg(resp);
852*11380SMarcel.Telka@Sun.COM 			resp = NULL;
8530Sstevel@tonic-gate 			goto call_again;
8540Sstevel@tonic-gate 		}
8550Sstevel@tonic-gate 		/*
8560Sstevel@tonic-gate 		 * We have used the client handle to do an AUTH_REFRESH
8570Sstevel@tonic-gate 		 * and the RPC status may be set to RPC_SUCCESS;
8580Sstevel@tonic-gate 		 * Let's make sure to set it to RPC_AUTHERROR.
8590Sstevel@tonic-gate 		 */
8600Sstevel@tonic-gate 		p->cku_err.re_status = RPC_CANTDECODERES;
8610Sstevel@tonic-gate 
8620Sstevel@tonic-gate 		/*
8630Sstevel@tonic-gate 		 * Map recoverable and unrecoverable
8640Sstevel@tonic-gate 		 * authentication errors to appropriate errno
8650Sstevel@tonic-gate 		 */
8660Sstevel@tonic-gate 		switch (p->cku_err.re_why) {
867342Snd150628 		case AUTH_TOOWEAK:
868342Snd150628 			/*
869342Snd150628 			 * Could be an nfsportmon failure, set
870342Snd150628 			 * useresvport and try again.
871342Snd150628 			 */
872342Snd150628 			if (p->cku_useresvport != 1) {
873342Snd150628 				p->cku_useresvport = 1;
874342Snd150628 
875*11380SMarcel.Telka@Sun.COM 				freemsg(mpdup);
876*11380SMarcel.Telka@Sun.COM 				mpdup = NULL;
877*11380SMarcel.Telka@Sun.COM 				freemsg(resp);
878*11380SMarcel.Telka@Sun.COM 				resp = NULL;
879342Snd150628 
880342Snd150628 				endpt = p->cku_endpnt;
881342Snd150628 				if (endpt->e_tiptr != NULL) {
882342Snd150628 					mutex_enter(&endpt->e_lock);
883342Snd150628 					endpt->e_flags &= ~ENDPNT_BOUND;
884342Snd150628 					(void) t_kclose(endpt->e_tiptr, 1);
885342Snd150628 					endpt->e_tiptr = NULL;
886342Snd150628 					mutex_exit(&endpt->e_lock);
887342Snd150628 
888342Snd150628 				}
889342Snd150628 
890342Snd150628 				p->cku_xid = alloc_xid();
891342Snd150628 				endpnt_rele(p->cku_endpnt);
892342Snd150628 				p->cku_endpnt = NULL;
893342Snd150628 				goto call_again;
894342Snd150628 			}
895342Snd150628 			/* FALLTHRU */
896342Snd150628 		case AUTH_BADCRED:
897342Snd150628 		case AUTH_BADVERF:
898342Snd150628 		case AUTH_INVALIDRESP:
899342Snd150628 		case AUTH_FAILED:
900342Snd150628 		case RPCSEC_GSS_NOCRED:
901342Snd150628 		case RPCSEC_GSS_FAILED:
902342Snd150628 			p->cku_err.re_errno = EACCES;
903342Snd150628 			break;
904342Snd150628 		case AUTH_REJECTEDCRED:
905342Snd150628 		case AUTH_REJECTEDVERF:
906342Snd150628 		default:
907342Snd150628 			p->cku_err.re_errno = EIO;
908342Snd150628 			break;
9090Sstevel@tonic-gate 		}
9100Sstevel@tonic-gate 		RPCLOG(1, "clnt_clts_kcallit : authentication failed "
9110Sstevel@tonic-gate 		    "with RPC_AUTHERROR of type %d\n",
9120Sstevel@tonic-gate 		    p->cku_err.re_why);
913*11380SMarcel.Telka@Sun.COM 		goto done;
9140Sstevel@tonic-gate 	}
9150Sstevel@tonic-gate 
9160Sstevel@tonic-gate 	(void) xdr_rpc_free_verifier(xdrs, &reply_msg);
9170Sstevel@tonic-gate 
9180Sstevel@tonic-gate done1:
9190Sstevel@tonic-gate 	call_table_remove(call);
9200Sstevel@tonic-gate 	if (call->call_reply != NULL) {
9210Sstevel@tonic-gate 		freemsg(call->call_reply);
9220Sstevel@tonic-gate 		call->call_reply = NULL;
9230Sstevel@tonic-gate 	}
9240Sstevel@tonic-gate 	RPCLOG(64, "clnt_clts_kcallit_addr: xid 0x%x taken off dispatch list",
9256403Sgt29601 	    p->cku_xid);
9260Sstevel@tonic-gate 
9270Sstevel@tonic-gate done:
9280Sstevel@tonic-gate 	if (resp != NULL) {
9290Sstevel@tonic-gate 		freemsg(resp);
9300Sstevel@tonic-gate 		resp = NULL;
9310Sstevel@tonic-gate 	}
9320Sstevel@tonic-gate 
9330Sstevel@tonic-gate 	if ((p->cku_err.re_status != RPC_SUCCESS) &&
9340Sstevel@tonic-gate 	    (p->cku_err.re_status != RPC_INTR) &&
9350Sstevel@tonic-gate 	    (p->cku_err.re_status != RPC_UDERROR) &&
9360Sstevel@tonic-gate 	    !IS_UNRECOVERABLE_RPC(p->cku_err.re_status)) {
9370Sstevel@tonic-gate 		if (p->cku_feedback != NULL && stries == p->cku_retrys) {
9380Sstevel@tonic-gate 			(*p->cku_feedback)(FEEDBACK_REXMIT1, procnum,
9390Sstevel@tonic-gate 			    p->cku_feedarg);
9400Sstevel@tonic-gate 		}
9410Sstevel@tonic-gate 
9420Sstevel@tonic-gate 		timout = backoff(timout);
9430Sstevel@tonic-gate 		if (p->cku_timeall != (struct rpc_timers *)0)
9440Sstevel@tonic-gate 			p->cku_timeall->rt_rtxcur = timout;
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate 		if (p->cku_err.re_status == RPC_SYSTEMERROR ||
9470Sstevel@tonic-gate 		    p->cku_err.re_status == RPC_CANTSEND) {
9480Sstevel@tonic-gate 			/*
9490Sstevel@tonic-gate 			 * Errors due to lack of resources, wait a bit
9500Sstevel@tonic-gate 			 * and try again.
9510Sstevel@tonic-gate 			 */
9520Sstevel@tonic-gate 			(void) delay(hz/10);
9530Sstevel@tonic-gate 		}
9540Sstevel@tonic-gate 		if (stries-- > 0) {
9550Sstevel@tonic-gate 			RCSTAT_INCR(p->cku_stats, rcretrans);
9560Sstevel@tonic-gate 			goto call_again;
9570Sstevel@tonic-gate 		}
9580Sstevel@tonic-gate 	}
9590Sstevel@tonic-gate 
9600Sstevel@tonic-gate 	if (mpdup != NULL)
9610Sstevel@tonic-gate 		freemsg(mpdup);
9620Sstevel@tonic-gate 
9630Sstevel@tonic-gate 	if (p->cku_err.re_status != RPC_SUCCESS) {
9640Sstevel@tonic-gate 		RCSTAT_INCR(p->cku_stats, rcbadcalls);
9650Sstevel@tonic-gate 	}
9660Sstevel@tonic-gate 
9670Sstevel@tonic-gate 	/*
9680Sstevel@tonic-gate 	 * Allow the endpoint to be held by the client handle in case this
9690Sstevel@tonic-gate 	 * RPC was not successful.  A retry may occur at a higher level and
9700Sstevel@tonic-gate 	 * in this case we may want to send the request over the same
9710Sstevel@tonic-gate 	 * source port.
9728766Sdai.ngo@sun.com 	 * Endpoint is also released for one-way RPC: no reply, nor retransmit
9738766Sdai.ngo@sun.com 	 * is expected.
9740Sstevel@tonic-gate 	 */
9758766Sdai.ngo@sun.com 	if ((p->cku_err.re_status == RPC_SUCCESS ||
9768766Sdai.ngo@sun.com 	    (p->cku_err.re_status == RPC_TIMEDOUT && ori_timout == 0)) &&
9778766Sdai.ngo@sun.com 	    p->cku_endpnt != NULL) {
9780Sstevel@tonic-gate 		endpnt_rele(p->cku_endpnt);
9790Sstevel@tonic-gate 		p->cku_endpnt = NULL;
9808766Sdai.ngo@sun.com 	} else {
9818766Sdai.ngo@sun.com 		DTRACE_PROBE2(clnt_clts_kcallit_done, int, p->cku_err.re_status,
9828766Sdai.ngo@sun.com 		    struct endpnt *, p->cku_endpnt);
9830Sstevel@tonic-gate 	}
9840Sstevel@tonic-gate 
9850Sstevel@tonic-gate 	return (p->cku_err.re_status);
9860Sstevel@tonic-gate }
9870Sstevel@tonic-gate 
9880Sstevel@tonic-gate static enum clnt_stat
clnt_clts_kcallit(CLIENT * h,rpcproc_t procnum,xdrproc_t xdr_args,caddr_t argsp,xdrproc_t xdr_results,caddr_t resultsp,struct timeval wait)9890Sstevel@tonic-gate clnt_clts_kcallit(CLIENT *h, rpcproc_t procnum, xdrproc_t xdr_args,
9900Sstevel@tonic-gate 	caddr_t argsp, xdrproc_t xdr_results, caddr_t resultsp,
9910Sstevel@tonic-gate 	struct timeval wait)
9920Sstevel@tonic-gate {
9930Sstevel@tonic-gate 	return (clnt_clts_kcallit_addr(h, procnum, xdr_args, argsp,
9946403Sgt29601 	    xdr_results, resultsp, wait, NULL));
9950Sstevel@tonic-gate }
9960Sstevel@tonic-gate 
9970Sstevel@tonic-gate /*
9980Sstevel@tonic-gate  * Return error info on this handle.
9990Sstevel@tonic-gate  */
10000Sstevel@tonic-gate static void
clnt_clts_kerror(CLIENT * h,struct rpc_err * err)10010Sstevel@tonic-gate clnt_clts_kerror(CLIENT *h, struct rpc_err *err)
10020Sstevel@tonic-gate {
10030Sstevel@tonic-gate 	/* LINTED pointer alignment */
10040Sstevel@tonic-gate 	struct cku_private *p = htop(h);
10050Sstevel@tonic-gate 
10060Sstevel@tonic-gate 	*err = p->cku_err;
10070Sstevel@tonic-gate }
10080Sstevel@tonic-gate 
10090Sstevel@tonic-gate static bool_t
clnt_clts_kfreeres(CLIENT * h,xdrproc_t xdr_res,caddr_t res_ptr)10100Sstevel@tonic-gate clnt_clts_kfreeres(CLIENT *h, xdrproc_t xdr_res, caddr_t res_ptr)
10110Sstevel@tonic-gate {
10120Sstevel@tonic-gate 	/* LINTED pointer alignment */
10130Sstevel@tonic-gate 	struct cku_private *p = htop(h);
10140Sstevel@tonic-gate 	XDR *xdrs;
10150Sstevel@tonic-gate 
10160Sstevel@tonic-gate 	xdrs = &(p->cku_outxdr);
10170Sstevel@tonic-gate 	xdrs->x_op = XDR_FREE;
10180Sstevel@tonic-gate 	return ((*xdr_res)(xdrs, res_ptr));
10190Sstevel@tonic-gate }
10200Sstevel@tonic-gate 
10210Sstevel@tonic-gate /*ARGSUSED*/
10220Sstevel@tonic-gate static void
clnt_clts_kabort(CLIENT * h)10230Sstevel@tonic-gate clnt_clts_kabort(CLIENT *h)
10240Sstevel@tonic-gate {
10250Sstevel@tonic-gate }
10260Sstevel@tonic-gate 
10270Sstevel@tonic-gate static bool_t
clnt_clts_kcontrol(CLIENT * h,int cmd,char * arg)10280Sstevel@tonic-gate clnt_clts_kcontrol(CLIENT *h, int cmd, char *arg)
10290Sstevel@tonic-gate {
10300Sstevel@tonic-gate 	/* LINTED pointer alignment */
10310Sstevel@tonic-gate 	struct cku_private *p = htop(h);
10320Sstevel@tonic-gate 
10330Sstevel@tonic-gate 	switch (cmd) {
10340Sstevel@tonic-gate 	case CLSET_XID:
10350Sstevel@tonic-gate 		p->cku_xid = *((uint32_t *)arg);
10360Sstevel@tonic-gate 		return (TRUE);
10370Sstevel@tonic-gate 
10380Sstevel@tonic-gate 	case CLGET_XID:
10390Sstevel@tonic-gate 		*((uint32_t *)arg) = p->cku_xid;
10400Sstevel@tonic-gate 		return (TRUE);
10410Sstevel@tonic-gate 
10420Sstevel@tonic-gate 	case CLSET_BCAST:
10430Sstevel@tonic-gate 		p->cku_bcast = *((uint32_t *)arg);
10440Sstevel@tonic-gate 		return (TRUE);
10450Sstevel@tonic-gate 
10460Sstevel@tonic-gate 	case CLGET_BCAST:
10470Sstevel@tonic-gate 		*((uint32_t *)arg) = p->cku_bcast;
10480Sstevel@tonic-gate 		return (TRUE);
1049342Snd150628 	case CLSET_BINDRESVPORT:
1050342Snd150628 		if (arg == NULL)
1051342Snd150628 			return (FALSE);
1052342Snd150628 
1053342Snd150628 		if (*(int *)arg != 1 && *(int *)arg != 0)
1054342Snd150628 			return (FALSE);
1055342Snd150628 
1056342Snd150628 		p->cku_useresvport = *(int *)arg;
1057342Snd150628 
1058342Snd150628 		return (TRUE);
1059342Snd150628 
1060342Snd150628 	case CLGET_BINDRESVPORT:
1061342Snd150628 		if (arg == NULL)
1062342Snd150628 			return (FALSE);
1063342Snd150628 
1064342Snd150628 		*(int *)arg = p->cku_useresvport;
1065342Snd150628 
1066342Snd150628 		return (TRUE);
10670Sstevel@tonic-gate 
10680Sstevel@tonic-gate 	default:
10690Sstevel@tonic-gate 		return (FALSE);
10700Sstevel@tonic-gate 	}
10710Sstevel@tonic-gate }
10720Sstevel@tonic-gate 
10730Sstevel@tonic-gate /*
10740Sstevel@tonic-gate  * Destroy rpc handle.
10750Sstevel@tonic-gate  * Frees the space used for output buffer, private data, and handle
10760Sstevel@tonic-gate  * structure, and the file pointer/TLI data on last reference.
10770Sstevel@tonic-gate  */
10780Sstevel@tonic-gate static void
clnt_clts_kdestroy(CLIENT * h)10790Sstevel@tonic-gate clnt_clts_kdestroy(CLIENT *h)
10800Sstevel@tonic-gate {
10810Sstevel@tonic-gate 	/* LINTED pointer alignment */
10820Sstevel@tonic-gate 	struct cku_private *p = htop(h);
10830Sstevel@tonic-gate 	calllist_t *call = &p->cku_call;
10840Sstevel@tonic-gate 
10850Sstevel@tonic-gate 	int plen;
10860Sstevel@tonic-gate 
10870Sstevel@tonic-gate 	RPCLOG(8, "clnt_clts_kdestroy h: %p\n", (void *)h);
10880Sstevel@tonic-gate 	RPCLOG(8, "clnt_clts_kdestroy h: xid=0x%x\n", p->cku_xid);
10890Sstevel@tonic-gate 
10900Sstevel@tonic-gate 	if (p->cku_endpnt != NULL)
10910Sstevel@tonic-gate 		endpnt_rele(p->cku_endpnt);
10920Sstevel@tonic-gate 
10930Sstevel@tonic-gate 	cv_destroy(&call->call_cv);
10940Sstevel@tonic-gate 	mutex_destroy(&call->call_lock);
10950Sstevel@tonic-gate 
10960Sstevel@tonic-gate 	plen = strlen(p->cku_config.knc_protofmly) + 1;
10970Sstevel@tonic-gate 	kmem_free(p->cku_config.knc_protofmly, plen);
10980Sstevel@tonic-gate 	kmem_free(p->cku_addr.buf, p->cku_addr.maxlen);
10990Sstevel@tonic-gate 	kmem_free(p, sizeof (*p));
11000Sstevel@tonic-gate }
11010Sstevel@tonic-gate 
11020Sstevel@tonic-gate /*
11030Sstevel@tonic-gate  * The connectionless (CLTS) kRPC endpoint management subsystem.
11040Sstevel@tonic-gate  *
11050Sstevel@tonic-gate  * Because endpoints are potentially shared among threads making RPC calls,
11060Sstevel@tonic-gate  * they are managed in a pool according to type (endpnt_type_t).  Each
11070Sstevel@tonic-gate  * endpnt_type_t points to a list of usable endpoints through the e_pool
11080Sstevel@tonic-gate  * field, which is of type list_t.  list_t is a doubly-linked list.
11090Sstevel@tonic-gate  * The number of endpoints in the pool is stored in the e_cnt field of
11100Sstevel@tonic-gate  * endpnt_type_t and the endpoints are reference counted using the e_ref field
11110Sstevel@tonic-gate  * in the endpnt_t structure.
11120Sstevel@tonic-gate  *
11130Sstevel@tonic-gate  * As an optimization, endpoints that have no references are also linked
11140Sstevel@tonic-gate  * to an idle list via e_ilist which is also of type list_t.  When a thread
11150Sstevel@tonic-gate  * calls endpnt_get() to obtain a transport endpoint, the idle list is first
11160Sstevel@tonic-gate  * consulted and if such an endpoint exists, it is removed from the idle list
11170Sstevel@tonic-gate  * and returned to the caller.
11180Sstevel@tonic-gate  *
11190Sstevel@tonic-gate  * If the idle list is empty, then a check is made to see if more endpoints
11200Sstevel@tonic-gate  * can be created.  If so, we proceed and create a new endpoint which is added
11210Sstevel@tonic-gate  * to the pool and returned to the caller.  If we have reached the limit and
11220Sstevel@tonic-gate  * cannot make a new endpoint then one is returned to the caller via round-
11230Sstevel@tonic-gate  * robin policy.
11240Sstevel@tonic-gate  *
11250Sstevel@tonic-gate  * When an endpoint is placed on the idle list by a thread calling
11260Sstevel@tonic-gate  * endpnt_rele(), it is timestamped and then a reaper taskq is scheduled to
11270Sstevel@tonic-gate  * be dispatched if one hasn't already been.  When the timer fires, the
11280Sstevel@tonic-gate  * taskq traverses the idle list and checks to see which endpoints are
11290Sstevel@tonic-gate  * eligible to be closed.  It determines this by checking if the timestamp
11300Sstevel@tonic-gate  * when the endpoint was released has exceeded the the threshold for how long
11310Sstevel@tonic-gate  * it should stay alive.
11320Sstevel@tonic-gate  *
11330Sstevel@tonic-gate  * endpnt_t structures remain persistent until the memory reclaim callback,
11340Sstevel@tonic-gate  * endpnt_reclaim(), is invoked.
11350Sstevel@tonic-gate  *
11360Sstevel@tonic-gate  * Here is an example of how the data structures would be laid out by the
11370Sstevel@tonic-gate  * subsystem:
11380Sstevel@tonic-gate  *
11390Sstevel@tonic-gate  *       endpnt_type_t
11400Sstevel@tonic-gate  *
11410Sstevel@tonic-gate  *	 loopback		                  inet
11420Sstevel@tonic-gate  *	 _______________	                  ______________
11430Sstevel@tonic-gate  *	| e_next        |----------------------->| e_next       |---->>
11440Sstevel@tonic-gate  *	| e_pool        |<---+                   | e_pool       |<----+
11450Sstevel@tonic-gate  *	| e_ilist       |<---+--+                | e_ilist      |<----+--+
11460Sstevel@tonic-gate  *   +->| e_pcurr       |----+--+--+	      +->| e_pcurr      |-----+--+--+
11470Sstevel@tonic-gate  *   |	| ...           |    |  |  |	      |	 | ...	        |     |  |  |
11480Sstevel@tonic-gate  *   |	| e_itimer (90) |    |  |  |	      |	 | e_itimer (0) |     |  |  |
11490Sstevel@tonic-gate  *   |	| e_cnt (1)     |    |  |  |	      |	 | e_cnt (3)    |     |  |  |
11500Sstevel@tonic-gate  *   |	+---------------+    |  |  |	      |	 +--------------+     |  |  |
11510Sstevel@tonic-gate  *   |			     |  |  |	      |			      |  |  |
11520Sstevel@tonic-gate  *   |   endpnt_t            |  |  |          |	                      |  |  |
11530Sstevel@tonic-gate  *   |	 ____________        |  |  |	      |	  ____________        |  |  |
11540Sstevel@tonic-gate  *   |	| e_node     |<------+  |  |	      |	 | e_node     |<------+  |  |
11550Sstevel@tonic-gate  *   |	| e_idle     |<---------+  |	      |	 | e_idle     |       |  |  |
11560Sstevel@tonic-gate  *   +--| e_type     |<------------+	      +--| e_type     |       |  |  |
11570Sstevel@tonic-gate  *	| e_tiptr    |                        |  | e_tiptr    |       |  |  |
11580Sstevel@tonic-gate  *      | ...	     |		              |	 | ...	      |       |  |  |
11590Sstevel@tonic-gate  *	| e_lock     |		              |	 | e_lock     |       |  |  |
11600Sstevel@tonic-gate  *	| ...        |		              |	 | ...	      |       |  |  |
11610Sstevel@tonic-gate  *      | e_ref (0)  |		              |	 | e_ref (2)  |       |  |  |
11620Sstevel@tonic-gate  *	| e_itime    |	                      |	 | e_itime    |       |  |  |
11630Sstevel@tonic-gate  *	+------------+		              |	 +------------+       |  |  |
11640Sstevel@tonic-gate  *					      |			      |  |  |
11650Sstevel@tonic-gate  *					      |			      |  |  |
11660Sstevel@tonic-gate  *					      |	  ____________        |  |  |
11670Sstevel@tonic-gate  *					      |	 | e_node     |<------+  |  |
11680Sstevel@tonic-gate  *					      |	 | e_idle     |<------+--+  |
11690Sstevel@tonic-gate  *					      +--| e_type     |       |     |
11700Sstevel@tonic-gate  *					      |	 | e_tiptr    |       |     |
11710Sstevel@tonic-gate  *					      |	 | ...	      |       |     |
11720Sstevel@tonic-gate  *					      |	 | e_lock     |       |     |
11730Sstevel@tonic-gate  *					      |	 | ...	      |       |     |
11740Sstevel@tonic-gate  *					      |	 | e_ref (0)  |       |     |
11750Sstevel@tonic-gate  *					      |	 | e_itime    |       |     |
11760Sstevel@tonic-gate  *					      |	 +------------+       |     |
11770Sstevel@tonic-gate  *					      |			      |     |
11780Sstevel@tonic-gate  *					      |			      |     |
11790Sstevel@tonic-gate  *					      |	  ____________        |     |
11800Sstevel@tonic-gate  *					      |	 | e_node     |<------+     |
11810Sstevel@tonic-gate  *					      |	 | e_idle     |             |
11820Sstevel@tonic-gate  *					      +--| e_type     |<------------+
11830Sstevel@tonic-gate  *						 | e_tiptr    |
11840Sstevel@tonic-gate  *						 | ...	      |
11850Sstevel@tonic-gate  *						 | e_lock     |
11860Sstevel@tonic-gate  *						 | ...	      |
11870Sstevel@tonic-gate  *						 | e_ref (1)  |
11880Sstevel@tonic-gate  *						 | e_itime    |
11890Sstevel@tonic-gate  *						 +------------+
11900Sstevel@tonic-gate  *
11910Sstevel@tonic-gate  * Endpoint locking strategy:
11920Sstevel@tonic-gate  *
11930Sstevel@tonic-gate  * The following functions manipulate lists which hold the endpoint and the
11940Sstevel@tonic-gate  * endpoints themselves:
11950Sstevel@tonic-gate  *
11960Sstevel@tonic-gate  * endpnt_get()/check_endpnt()/endpnt_rele()/endpnt_reap()/do_endpnt_reclaim()
11970Sstevel@tonic-gate  *
11980Sstevel@tonic-gate  * Lock description follows:
11990Sstevel@tonic-gate  *
12000Sstevel@tonic-gate  * endpnt_type_lock: Global reader/writer lock which protects accesses to the
12010Sstevel@tonic-gate  *		     endpnt_type_list.
12020Sstevel@tonic-gate  *
12030Sstevel@tonic-gate  * e_plock: Lock defined in the endpnt_type_t.  It is intended to
12040Sstevel@tonic-gate  *	    protect accesses to the pool of endopints (e_pool) for a given
12050Sstevel@tonic-gate  *	    endpnt_type_t.
12060Sstevel@tonic-gate  *
12070Sstevel@tonic-gate  * e_ilock: Lock defined in endpnt_type_t.  It is intended to protect accesses
12080Sstevel@tonic-gate  *	    to the idle list (e_ilist) of available endpoints for a given
12090Sstevel@tonic-gate  *	    endpnt_type_t.  It also protects access to the e_itimer, e_async_cv,
12100Sstevel@tonic-gate  *	    and e_async_count fields in endpnt_type_t.
12110Sstevel@tonic-gate  *
12120Sstevel@tonic-gate  * e_lock: Lock defined in the endpnt structure.  It is intended to protect
12130Sstevel@tonic-gate  *	   flags, cv, and ref count.
12140Sstevel@tonic-gate  *
12150Sstevel@tonic-gate  * The order goes as follows so as not to induce deadlock.
12160Sstevel@tonic-gate  *
12170Sstevel@tonic-gate  * endpnt_type_lock -> e_plock -> e_ilock -> e_lock
12180Sstevel@tonic-gate  *
12190Sstevel@tonic-gate  * Interaction with Zones and shutting down:
12200Sstevel@tonic-gate  *
12210Sstevel@tonic-gate  * endpnt_type_ts are uniquely identified by the (e_zoneid, e_rdev, e_protofmly)
12220Sstevel@tonic-gate  * tuple, which means that a zone may not reuse another zone's idle endpoints
12230Sstevel@tonic-gate  * without first doing a t_kclose().
12240Sstevel@tonic-gate  *
12250Sstevel@tonic-gate  * A zone's endpnt_type_ts are destroyed when a zone is shut down; e_async_cv
12260Sstevel@tonic-gate  * and e_async_count are used to keep track of the threads in endpnt_taskq
12270Sstevel@tonic-gate  * trying to reap endpnt_ts in the endpnt_type_t.
12280Sstevel@tonic-gate  */
12290Sstevel@tonic-gate 
12300Sstevel@tonic-gate /*
12310Sstevel@tonic-gate  * Allocate and initialize an endpnt_type_t
12320Sstevel@tonic-gate  */
12330Sstevel@tonic-gate static struct endpnt_type *
endpnt_type_create(struct knetconfig * config)12340Sstevel@tonic-gate endpnt_type_create(struct knetconfig *config)
12350Sstevel@tonic-gate {
12360Sstevel@tonic-gate 	struct endpnt_type	*etype;
12370Sstevel@tonic-gate 
12380Sstevel@tonic-gate 	/*
12390Sstevel@tonic-gate 	 * Allocate a new endpoint type to hang a list of
12400Sstevel@tonic-gate 	 * endpoints off of it.
12410Sstevel@tonic-gate 	 */
12420Sstevel@tonic-gate 	etype = kmem_alloc(sizeof (struct endpnt_type), KM_SLEEP);
12430Sstevel@tonic-gate 	etype->e_next = NULL;
12440Sstevel@tonic-gate 	etype->e_pcurr = NULL;
12450Sstevel@tonic-gate 	etype->e_itimer = 0;
12460Sstevel@tonic-gate 	etype->e_cnt = 0;
12470Sstevel@tonic-gate 
12480Sstevel@tonic-gate 	(void) strncpy(etype->e_protofmly, config->knc_protofmly, KNC_STRSIZE);
12490Sstevel@tonic-gate 	mutex_init(&etype->e_plock, NULL, MUTEX_DEFAULT, NULL);
12500Sstevel@tonic-gate 	mutex_init(&etype->e_ilock, NULL, MUTEX_DEFAULT, NULL);
12510Sstevel@tonic-gate 	etype->e_rdev = config->knc_rdev;
1252766Scarlsonj 	etype->e_zoneid = rpc_zoneid();
12530Sstevel@tonic-gate 	etype->e_async_count = 0;
12540Sstevel@tonic-gate 	cv_init(&etype->e_async_cv, NULL, CV_DEFAULT, NULL);
12550Sstevel@tonic-gate 
12560Sstevel@tonic-gate 	list_create(&etype->e_pool, sizeof (endpnt_t),
12576403Sgt29601 	    offsetof(endpnt_t, e_node));
12580Sstevel@tonic-gate 	list_create(&etype->e_ilist, sizeof (endpnt_t),
12596403Sgt29601 	    offsetof(endpnt_t, e_idle));
12600Sstevel@tonic-gate 
12610Sstevel@tonic-gate 	/*
12620Sstevel@tonic-gate 	 * Check to see if we need to create a taskq for endpoint
12630Sstevel@tonic-gate 	 * reaping
12640Sstevel@tonic-gate 	 */
12650Sstevel@tonic-gate 	mutex_enter(&endpnt_taskq_lock);
12660Sstevel@tonic-gate 	if (taskq_created == FALSE) {
12670Sstevel@tonic-gate 		taskq_created = TRUE;
12680Sstevel@tonic-gate 		mutex_exit(&endpnt_taskq_lock);
12690Sstevel@tonic-gate 		ASSERT(endpnt_taskq == NULL);
12700Sstevel@tonic-gate 		endpnt_taskq = taskq_create("clts_endpnt_taskq", 1,
12716403Sgt29601 		    minclsyspri, 200, INT_MAX, 0);
12720Sstevel@tonic-gate 	} else
12730Sstevel@tonic-gate 		mutex_exit(&endpnt_taskq_lock);
12740Sstevel@tonic-gate 
12750Sstevel@tonic-gate 	return (etype);
12760Sstevel@tonic-gate }
12770Sstevel@tonic-gate 
12780Sstevel@tonic-gate /*
12790Sstevel@tonic-gate  * Free an endpnt_type_t
12800Sstevel@tonic-gate  */
12810Sstevel@tonic-gate static void
endpnt_type_free(struct endpnt_type * etype)12820Sstevel@tonic-gate endpnt_type_free(struct endpnt_type *etype)
12830Sstevel@tonic-gate {
12840Sstevel@tonic-gate 	mutex_destroy(&etype->e_plock);
12850Sstevel@tonic-gate 	mutex_destroy(&etype->e_ilock);
12860Sstevel@tonic-gate 	list_destroy(&etype->e_pool);
12870Sstevel@tonic-gate 	list_destroy(&etype->e_ilist);
12880Sstevel@tonic-gate 	kmem_free(etype, sizeof (endpnt_type_t));
12890Sstevel@tonic-gate }
12900Sstevel@tonic-gate 
12910Sstevel@tonic-gate /*
12920Sstevel@tonic-gate  * Check the endpoint to ensure that it is suitable for use.
12930Sstevel@tonic-gate  *
12940Sstevel@tonic-gate  * Possible return values:
12950Sstevel@tonic-gate  *
12960Sstevel@tonic-gate  * return (1) - Endpoint is established, but needs to be re-opened.
12970Sstevel@tonic-gate  * return (0) && *newp == NULL - Endpoint is established, but unusable.
12980Sstevel@tonic-gate  * return (0) && *newp != NULL - Endpoint is established and usable.
12990Sstevel@tonic-gate  */
13000Sstevel@tonic-gate static int
check_endpnt(struct endpnt * endp,struct endpnt ** newp)13010Sstevel@tonic-gate check_endpnt(struct endpnt *endp, struct endpnt **newp)
13020Sstevel@tonic-gate {
13030Sstevel@tonic-gate 	*newp = endp;
13040Sstevel@tonic-gate 
13050Sstevel@tonic-gate 	mutex_enter(&endp->e_lock);
13060Sstevel@tonic-gate 	ASSERT(endp->e_ref >= 1);
13070Sstevel@tonic-gate 
13080Sstevel@tonic-gate 	/*
13090Sstevel@tonic-gate 	 * The first condition we check for is if the endpoint has been
13100Sstevel@tonic-gate 	 * allocated, but is unusable either because it has been closed or
13110Sstevel@tonic-gate 	 * has been marked stale.  Only *one* thread will be allowed to
13128766Sdai.ngo@sun.com 	 * execute the then clause.  This is enforced because the first thread
13130Sstevel@tonic-gate 	 * to check this condition will clear the flags, so that subsequent
13140Sstevel@tonic-gate 	 * thread(s) checking this endpoint will move on.
13150Sstevel@tonic-gate 	 */
13160Sstevel@tonic-gate 	if ((endp->e_flags & ENDPNT_ESTABLISHED) &&
13176403Sgt29601 	    (!(endp->e_flags & ENDPNT_BOUND) ||
13186403Sgt29601 	    (endp->e_flags & ENDPNT_STALE))) {
13190Sstevel@tonic-gate 		/*
13200Sstevel@tonic-gate 		 * Clear the flags here since they will be
13210Sstevel@tonic-gate 		 * set again by this thread.  They need to be
13220Sstevel@tonic-gate 		 * individually cleared because we want to maintain
13230Sstevel@tonic-gate 		 * the state for ENDPNT_ONIDLE.
13240Sstevel@tonic-gate 		 */
13250Sstevel@tonic-gate 		endp->e_flags &= ~(ENDPNT_ESTABLISHED |
13266403Sgt29601 		    ENDPNT_WAITING | ENDPNT_BOUND | ENDPNT_STALE);
13270Sstevel@tonic-gate 		mutex_exit(&endp->e_lock);
13280Sstevel@tonic-gate 		return (1);
13290Sstevel@tonic-gate 	}
13300Sstevel@tonic-gate 
13310Sstevel@tonic-gate 	/*
13320Sstevel@tonic-gate 	 * The second condition is meant for any thread that is waiting for
13330Sstevel@tonic-gate 	 * an endpoint to become established.  It will cv_wait() until
13340Sstevel@tonic-gate 	 * the condition for the endpoint has been changed to ENDPNT_BOUND or
13350Sstevel@tonic-gate 	 * ENDPNT_STALE.
13360Sstevel@tonic-gate 	 */
13370Sstevel@tonic-gate 	while (!(endp->e_flags & ENDPNT_BOUND) &&
13386403Sgt29601 	    !(endp->e_flags & ENDPNT_STALE)) {
13390Sstevel@tonic-gate 		endp->e_flags |= ENDPNT_WAITING;
13400Sstevel@tonic-gate 		cv_wait(&endp->e_cv, &endp->e_lock);
13410Sstevel@tonic-gate 	}
13420Sstevel@tonic-gate 
13430Sstevel@tonic-gate 	ASSERT(endp->e_flags & ENDPNT_ESTABLISHED);
13440Sstevel@tonic-gate 
13450Sstevel@tonic-gate 	/*
13460Sstevel@tonic-gate 	 * The last case we check for is if the endpoint has been marked stale.
13470Sstevel@tonic-gate 	 * If this is the case then set *newp to NULL and return, so that the
13480Sstevel@tonic-gate 	 * caller is notified of the error and can take appropriate action.
13490Sstevel@tonic-gate 	 */
13500Sstevel@tonic-gate 	if (endp->e_flags & ENDPNT_STALE) {
13510Sstevel@tonic-gate 		endp->e_ref--;
13520Sstevel@tonic-gate 		*newp = NULL;
13530Sstevel@tonic-gate 	}
13540Sstevel@tonic-gate 	mutex_exit(&endp->e_lock);
13550Sstevel@tonic-gate 	return (0);
13560Sstevel@tonic-gate }
13570Sstevel@tonic-gate 
13580Sstevel@tonic-gate #ifdef DEBUG
13590Sstevel@tonic-gate /*
13600Sstevel@tonic-gate  * Provide a fault injection setting to test error conditions.
13610Sstevel@tonic-gate  */
13620Sstevel@tonic-gate static int endpnt_get_return_null = 0;
13630Sstevel@tonic-gate #endif
13640Sstevel@tonic-gate 
13650Sstevel@tonic-gate /*
13660Sstevel@tonic-gate  * Returns a handle (struct endpnt *) to an open and bound endpoint
13670Sstevel@tonic-gate  * specified by the knetconfig passed in.  Returns NULL if no valid endpoint
13680Sstevel@tonic-gate  * can be obtained.
13690Sstevel@tonic-gate  */
13700Sstevel@tonic-gate static struct endpnt *
endpnt_get(struct knetconfig * config,int useresvport)1371342Snd150628 endpnt_get(struct knetconfig *config, int useresvport)
13720Sstevel@tonic-gate {
13730Sstevel@tonic-gate 	struct endpnt_type	*n_etype = NULL;
13740Sstevel@tonic-gate 	struct endpnt_type	*np = NULL;
13750Sstevel@tonic-gate 	struct endpnt		*new = NULL;
13760Sstevel@tonic-gate 	struct endpnt		*endp = NULL;
13770Sstevel@tonic-gate 	struct endpnt		*next = NULL;
13780Sstevel@tonic-gate 	TIUSER			*tiptr = NULL;
13790Sstevel@tonic-gate 	int			rtries = BINDRESVPORT_RETRIES;
13800Sstevel@tonic-gate 	int			i = 0;
13810Sstevel@tonic-gate 	int			error;
13820Sstevel@tonic-gate 	int			retval;
1383766Scarlsonj 	zoneid_t		zoneid = rpc_zoneid();
13841676Sjpk 	cred_t			*cr;
13850Sstevel@tonic-gate 
13860Sstevel@tonic-gate 	RPCLOG(1, "endpnt_get: protofmly %s, ", config->knc_protofmly);
13870Sstevel@tonic-gate 	RPCLOG(1, "rdev %ld\n", config->knc_rdev);
13880Sstevel@tonic-gate 
13890Sstevel@tonic-gate #ifdef DEBUG
13900Sstevel@tonic-gate 	/*
13910Sstevel@tonic-gate 	 * Inject fault if desired.  Pretend we have a stale endpoint
13920Sstevel@tonic-gate 	 * and return NULL.
13930Sstevel@tonic-gate 	 */
13940Sstevel@tonic-gate 	if (endpnt_get_return_null > 0) {
13950Sstevel@tonic-gate 		endpnt_get_return_null--;
13960Sstevel@tonic-gate 		return (NULL);
13970Sstevel@tonic-gate 	}
13980Sstevel@tonic-gate #endif
13990Sstevel@tonic-gate 	rw_enter(&endpnt_type_lock, RW_READER);
14000Sstevel@tonic-gate 
14010Sstevel@tonic-gate top:
14020Sstevel@tonic-gate 	for (np = endpnt_type_list; np != NULL; np = np->e_next)
14030Sstevel@tonic-gate 		if ((np->e_zoneid == zoneid) &&
14040Sstevel@tonic-gate 		    (np->e_rdev == config->knc_rdev) &&
14050Sstevel@tonic-gate 		    (strcmp(np->e_protofmly,
14066403Sgt29601 		    config->knc_protofmly) == 0))
14070Sstevel@tonic-gate 			break;
14080Sstevel@tonic-gate 
14090Sstevel@tonic-gate 	if (np == NULL && n_etype != NULL) {
14100Sstevel@tonic-gate 		ASSERT(rw_write_held(&endpnt_type_lock));
14110Sstevel@tonic-gate 
14120Sstevel@tonic-gate 		/*
14130Sstevel@tonic-gate 		 * Link the endpoint type onto the list
14140Sstevel@tonic-gate 		 */
14150Sstevel@tonic-gate 		n_etype->e_next = endpnt_type_list;
14160Sstevel@tonic-gate 		endpnt_type_list = n_etype;
14170Sstevel@tonic-gate 		np = n_etype;
14180Sstevel@tonic-gate 		n_etype = NULL;
14190Sstevel@tonic-gate 	}
14200Sstevel@tonic-gate 
14210Sstevel@tonic-gate 	if (np == NULL) {
14220Sstevel@tonic-gate 		/*
14230Sstevel@tonic-gate 		 * The logic here is that we were unable to find an
14240Sstevel@tonic-gate 		 * endpnt_type_t that matched our criteria, so we allocate a
14250Sstevel@tonic-gate 		 * new one.  Because kmem_alloc() needs to be called with
14260Sstevel@tonic-gate 		 * KM_SLEEP, we drop our locks so that we don't induce
14270Sstevel@tonic-gate 		 * deadlock.  After allocating and initializing the
14280Sstevel@tonic-gate 		 * endpnt_type_t, we reaquire the lock and go back to check
14290Sstevel@tonic-gate 		 * if this entry needs to be added to the list.  Since we do
14300Sstevel@tonic-gate 		 * some operations without any locking other threads may
14310Sstevel@tonic-gate 		 * have been looking for the same endpnt_type_t and gone
14320Sstevel@tonic-gate 		 * through this code path.  We check for this case and allow
14330Sstevel@tonic-gate 		 * one thread to link its endpnt_type_t to the list and the
14340Sstevel@tonic-gate 		 * other threads will simply free theirs.
14350Sstevel@tonic-gate 		 */
14360Sstevel@tonic-gate 		rw_exit(&endpnt_type_lock);
14370Sstevel@tonic-gate 		n_etype = endpnt_type_create(config);
14380Sstevel@tonic-gate 
14390Sstevel@tonic-gate 		/*
14400Sstevel@tonic-gate 		 * We need to reaquire the lock with RW_WRITER here so that
14410Sstevel@tonic-gate 		 * we can safely link the new endpoint type onto the list.
14420Sstevel@tonic-gate 		 */
14430Sstevel@tonic-gate 		rw_enter(&endpnt_type_lock, RW_WRITER);
14440Sstevel@tonic-gate 		goto top;
14450Sstevel@tonic-gate 	}
14460Sstevel@tonic-gate 
14470Sstevel@tonic-gate 	rw_exit(&endpnt_type_lock);
14480Sstevel@tonic-gate 	/*
14490Sstevel@tonic-gate 	 * If n_etype is not NULL, then another thread was able to
14500Sstevel@tonic-gate 	 * insert an endpnt_type_t of this type  onto the list before
14510Sstevel@tonic-gate 	 * we did.  Go ahead and free ours.
14520Sstevel@tonic-gate 	 */
14530Sstevel@tonic-gate 	if (n_etype != NULL)
14540Sstevel@tonic-gate 		endpnt_type_free(n_etype);
14550Sstevel@tonic-gate 
14560Sstevel@tonic-gate 	mutex_enter(&np->e_ilock);
14570Sstevel@tonic-gate 	/*
14580Sstevel@tonic-gate 	 * The algorithm to hand out endpoints is to first
14590Sstevel@tonic-gate 	 * give out those that are idle if such endpoints
14600Sstevel@tonic-gate 	 * exist.  Otherwise, create a new one if we haven't
14610Sstevel@tonic-gate 	 * reached the max threshold.  Finally, we give out
14620Sstevel@tonic-gate 	 * endpoints in a pseudo LRU fashion (round-robin).
14630Sstevel@tonic-gate 	 *
14640Sstevel@tonic-gate 	 * Note:  The idle list is merely a hint of those endpoints
14650Sstevel@tonic-gate 	 * that should be idle.  There exists a window after the
14660Sstevel@tonic-gate 	 * endpoint is released and before it is linked back onto the
14670Sstevel@tonic-gate 	 * idle list where a thread could get a reference to it and
14680Sstevel@tonic-gate 	 * use it.  This is okay, since the reference counts will
14690Sstevel@tonic-gate 	 * still be consistent.
14700Sstevel@tonic-gate 	 */
14710Sstevel@tonic-gate 	if ((endp = (endpnt_t *)list_head(&np->e_ilist)) != NULL) {
14720Sstevel@tonic-gate 		timeout_id_t t_id = 0;
14730Sstevel@tonic-gate 
14740Sstevel@tonic-gate 		mutex_enter(&endp->e_lock);
14750Sstevel@tonic-gate 		endp->e_ref++;
14760Sstevel@tonic-gate 		endp->e_itime = 0;
14770Sstevel@tonic-gate 		endp->e_flags &= ~ENDPNT_ONIDLE;
14780Sstevel@tonic-gate 		mutex_exit(&endp->e_lock);
14790Sstevel@tonic-gate 
14800Sstevel@tonic-gate 		/*
14810Sstevel@tonic-gate 		 * Pop the endpoint off the idle list and hand it off
14820Sstevel@tonic-gate 		 */
14830Sstevel@tonic-gate 		list_remove(&np->e_ilist, endp);
14840Sstevel@tonic-gate 
14850Sstevel@tonic-gate 		if (np->e_itimer != 0) {
14860Sstevel@tonic-gate 			t_id = np->e_itimer;
14870Sstevel@tonic-gate 			np->e_itimer = 0;
14880Sstevel@tonic-gate 		}
14890Sstevel@tonic-gate 		mutex_exit(&np->e_ilock);
14900Sstevel@tonic-gate 		/*
14910Sstevel@tonic-gate 		 * Reset the idle timer if it has been set
14920Sstevel@tonic-gate 		 */
14930Sstevel@tonic-gate 		if (t_id != (timeout_id_t)0)
14940Sstevel@tonic-gate 			(void) untimeout(t_id);
14950Sstevel@tonic-gate 
14960Sstevel@tonic-gate 		if (check_endpnt(endp, &new) == 0)
14970Sstevel@tonic-gate 			return (new);
14980Sstevel@tonic-gate 	} else if (np->e_cnt >= clnt_clts_max_endpoints) {
14990Sstevel@tonic-gate 		/*
15000Sstevel@tonic-gate 		 * There are no idle endpoints currently, so
15010Sstevel@tonic-gate 		 * create a new one if we have not reached the maximum or
15020Sstevel@tonic-gate 		 * hand one out in round-robin.
15030Sstevel@tonic-gate 		 */
15040Sstevel@tonic-gate 		mutex_exit(&np->e_ilock);
15050Sstevel@tonic-gate 		mutex_enter(&np->e_plock);
15060Sstevel@tonic-gate 		endp = np->e_pcurr;
15070Sstevel@tonic-gate 		mutex_enter(&endp->e_lock);
15080Sstevel@tonic-gate 		endp->e_ref++;
15090Sstevel@tonic-gate 		mutex_exit(&endp->e_lock);
15100Sstevel@tonic-gate 
15110Sstevel@tonic-gate 		ASSERT(endp != NULL);
15120Sstevel@tonic-gate 		/*
15130Sstevel@tonic-gate 		 * Advance the pointer to the next eligible endpoint, if
15140Sstevel@tonic-gate 		 * necessary.
15150Sstevel@tonic-gate 		 */
15160Sstevel@tonic-gate 		if (np->e_cnt > 1) {
15170Sstevel@tonic-gate 			next = (endpnt_t *)list_next(&np->e_pool, np->e_pcurr);
15180Sstevel@tonic-gate 			if (next == NULL)
15190Sstevel@tonic-gate 				next = (endpnt_t *)list_head(&np->e_pool);
15200Sstevel@tonic-gate 			np->e_pcurr = next;
15210Sstevel@tonic-gate 		}
15220Sstevel@tonic-gate 
15230Sstevel@tonic-gate 		mutex_exit(&np->e_plock);
15240Sstevel@tonic-gate 
15250Sstevel@tonic-gate 		/*
15260Sstevel@tonic-gate 		 * We need to check to see if this endpoint is bound or
15270Sstevel@tonic-gate 		 * not.  If it is in progress then just wait until
15280Sstevel@tonic-gate 		 * the set up is complete
15290Sstevel@tonic-gate 		 */
15300Sstevel@tonic-gate 		if (check_endpnt(endp, &new) == 0)
15310Sstevel@tonic-gate 			return (new);
15320Sstevel@tonic-gate 	} else {
15330Sstevel@tonic-gate 		mutex_exit(&np->e_ilock);
15340Sstevel@tonic-gate 		mutex_enter(&np->e_plock);
15350Sstevel@tonic-gate 
15360Sstevel@tonic-gate 		/*
15370Sstevel@tonic-gate 		 * Allocate a new endpoint to use.  If we can't allocate any
15380Sstevel@tonic-gate 		 * more memory then use one that is already established if any
15390Sstevel@tonic-gate 		 * such endpoints exist.
15400Sstevel@tonic-gate 		 */
15410Sstevel@tonic-gate 		new = kmem_cache_alloc(endpnt_cache, KM_NOSLEEP);
15420Sstevel@tonic-gate 		if (new == NULL) {
15430Sstevel@tonic-gate 			RPCLOG0(1, "endpnt_get: kmem_cache_alloc failed\n");
15440Sstevel@tonic-gate 			/*
15450Sstevel@tonic-gate 			 * Try to recover by using an existing endpoint.
15460Sstevel@tonic-gate 			 */
15470Sstevel@tonic-gate 			if (np->e_cnt <= 0) {
15480Sstevel@tonic-gate 				mutex_exit(&np->e_plock);
15490Sstevel@tonic-gate 				return (NULL);
15500Sstevel@tonic-gate 			}
15510Sstevel@tonic-gate 			endp = np->e_pcurr;
15520Sstevel@tonic-gate 			if ((next = list_next(&np->e_pool, np->e_pcurr)) !=
15536403Sgt29601 			    NULL)
15540Sstevel@tonic-gate 				np->e_pcurr = next;
15550Sstevel@tonic-gate 			ASSERT(endp != NULL);
15560Sstevel@tonic-gate 			mutex_enter(&endp->e_lock);
15570Sstevel@tonic-gate 			endp->e_ref++;
15580Sstevel@tonic-gate 			mutex_exit(&endp->e_lock);
15590Sstevel@tonic-gate 			mutex_exit(&np->e_plock);
15600Sstevel@tonic-gate 
15610Sstevel@tonic-gate 			if (check_endpnt(endp, &new) == 0)
15620Sstevel@tonic-gate 				return (new);
15630Sstevel@tonic-gate 		} else {
15640Sstevel@tonic-gate 			/*
15650Sstevel@tonic-gate 			 * Partially init an endpoint structure and put
15660Sstevel@tonic-gate 			 * it on the list, so that other interested threads
15670Sstevel@tonic-gate 			 * know that one is being created
15680Sstevel@tonic-gate 			 */
15690Sstevel@tonic-gate 			bzero(new, sizeof (struct endpnt));
15700Sstevel@tonic-gate 
15710Sstevel@tonic-gate 			cv_init(&new->e_cv, NULL, CV_DEFAULT, NULL);
15720Sstevel@tonic-gate 			mutex_init(&new->e_lock, NULL, MUTEX_DEFAULT, NULL);
15730Sstevel@tonic-gate 			new->e_ref = 1;
15740Sstevel@tonic-gate 			new->e_type = np;
15750Sstevel@tonic-gate 
15760Sstevel@tonic-gate 			/*
15770Sstevel@tonic-gate 			 * Link the endpoint into the pool.
15780Sstevel@tonic-gate 			 */
15790Sstevel@tonic-gate 			list_insert_head(&np->e_pool, new);
15800Sstevel@tonic-gate 			np->e_cnt++;
15810Sstevel@tonic-gate 			if (np->e_pcurr == NULL)
15820Sstevel@tonic-gate 				np->e_pcurr = new;
15830Sstevel@tonic-gate 			mutex_exit(&np->e_plock);
15840Sstevel@tonic-gate 		}
15850Sstevel@tonic-gate 	}
15860Sstevel@tonic-gate 
15870Sstevel@tonic-gate 	/*
15880Sstevel@tonic-gate 	 * The transport should be opened with sufficient privs
15890Sstevel@tonic-gate 	 */
15901676Sjpk 	cr = zone_kcred();
15910Sstevel@tonic-gate 	error = t_kopen(NULL, config->knc_rdev, FREAD|FWRITE|FNDELAY, &tiptr,
15921676Sjpk 	    cr);
15930Sstevel@tonic-gate 	if (error) {
15940Sstevel@tonic-gate 		RPCLOG(1, "endpnt_get: t_kopen: %d\n", error);
15950Sstevel@tonic-gate 		goto bad;
15960Sstevel@tonic-gate 	}
15970Sstevel@tonic-gate 
15980Sstevel@tonic-gate 	new->e_tiptr = tiptr;
15990Sstevel@tonic-gate 	rpc_poptimod(tiptr->fp->f_vnode);
16000Sstevel@tonic-gate 
16010Sstevel@tonic-gate 	/*
16020Sstevel@tonic-gate 	 * Allow the kernel to push the module on behalf of the user.
16030Sstevel@tonic-gate 	 */
16040Sstevel@tonic-gate 	error = strioctl(tiptr->fp->f_vnode, I_PUSH, (intptr_t)"rpcmod", 0,
16051676Sjpk 	    K_TO_K, cr, &retval);
16060Sstevel@tonic-gate 	if (error) {
16070Sstevel@tonic-gate 		RPCLOG(1, "endpnt_get: kstr_push on rpcmod failed %d\n", error);
16080Sstevel@tonic-gate 		goto bad;
16090Sstevel@tonic-gate 	}
16100Sstevel@tonic-gate 
16110Sstevel@tonic-gate 	error = strioctl(tiptr->fp->f_vnode, RPC_CLIENT, 0, 0, K_TO_K,
16121676Sjpk 	    cr, &retval);
16130Sstevel@tonic-gate 	if (error) {
16140Sstevel@tonic-gate 		RPCLOG(1, "endpnt_get: strioctl failed %d\n", error);
16150Sstevel@tonic-gate 		goto bad;
16160Sstevel@tonic-gate 	}
16170Sstevel@tonic-gate 
16180Sstevel@tonic-gate 	/*
16190Sstevel@tonic-gate 	 * Connectionless data flow should bypass the stream head.
16200Sstevel@tonic-gate 	 */
16210Sstevel@tonic-gate 	new->e_wq = tiptr->fp->f_vnode->v_stream->sd_wrq->q_next;
16220Sstevel@tonic-gate 
16230Sstevel@tonic-gate 	error = strioctl(tiptr->fp->f_vnode, I_PUSH, (intptr_t)"timod", 0,
16241676Sjpk 	    K_TO_K, cr, &retval);
16250Sstevel@tonic-gate 	if (error) {
16260Sstevel@tonic-gate 		RPCLOG(1, "endpnt_get: kstr_push on timod failed %d\n", error);
16270Sstevel@tonic-gate 		goto bad;
16280Sstevel@tonic-gate 	}
16290Sstevel@tonic-gate 
16300Sstevel@tonic-gate 	/*
16310Sstevel@tonic-gate 	 * Attempt to bind the endpoint.  If we fail then propogate
16320Sstevel@tonic-gate 	 * error back to calling subsystem, so that it can be handled
16330Sstevel@tonic-gate 	 * appropriately.
1634342Snd150628 	 * If the caller has not specified reserved port usage then
1635342Snd150628 	 * take the system default.
16360Sstevel@tonic-gate 	 */
1637342Snd150628 	if (useresvport == -1)
1638342Snd150628 		useresvport = clnt_clts_do_bindresvport;
1639342Snd150628 
1640342Snd150628 	if (useresvport &&
16410Sstevel@tonic-gate 	    (strcmp(config->knc_protofmly, NC_INET) == 0 ||
16426403Sgt29601 	    strcmp(config->knc_protofmly, NC_INET6) == 0)) {
16430Sstevel@tonic-gate 
16440Sstevel@tonic-gate 		while ((error =
16456403Sgt29601 		    bindresvport(new->e_tiptr, NULL, NULL, FALSE)) != 0) {
16460Sstevel@tonic-gate 			RPCLOG(1,
16476403Sgt29601 			    "endpnt_get: bindresvport error %d\n", error);
16480Sstevel@tonic-gate 			if (error != EPROTO) {
16490Sstevel@tonic-gate 				if (rtries-- <= 0)
16500Sstevel@tonic-gate 					goto bad;
16510Sstevel@tonic-gate 
16520Sstevel@tonic-gate 				delay(hz << i++);
16530Sstevel@tonic-gate 				continue;
16540Sstevel@tonic-gate 			}
16550Sstevel@tonic-gate 
16560Sstevel@tonic-gate 			(void) t_kclose(new->e_tiptr, 1);
16570Sstevel@tonic-gate 			/*
16580Sstevel@tonic-gate 			 * reopen with all privileges
16590Sstevel@tonic-gate 			 */
16600Sstevel@tonic-gate 			error = t_kopen(NULL, config->knc_rdev,
16611676Sjpk 			    FREAD|FWRITE|FNDELAY,
16621676Sjpk 			    &new->e_tiptr, cr);
16630Sstevel@tonic-gate 			if (error) {
16640Sstevel@tonic-gate 				RPCLOG(1, "endpnt_get: t_kopen: %d\n", error);
16650Sstevel@tonic-gate 					new->e_tiptr = NULL;
16660Sstevel@tonic-gate 					goto bad;
16670Sstevel@tonic-gate 			}
16680Sstevel@tonic-gate 		}
16690Sstevel@tonic-gate 	} else if ((error = t_kbind(new->e_tiptr, NULL, NULL)) != 0) {
16700Sstevel@tonic-gate 		RPCLOG(1, "endpnt_get: t_kbind failed: %d\n", error);
16710Sstevel@tonic-gate 		goto bad;
16720Sstevel@tonic-gate 	}
16730Sstevel@tonic-gate 
16740Sstevel@tonic-gate 	/*
16750Sstevel@tonic-gate 	 * Set the flags and notify and waiters that we have an established
16760Sstevel@tonic-gate 	 * endpoint.
16770Sstevel@tonic-gate 	 */
16780Sstevel@tonic-gate 	mutex_enter(&new->e_lock);
16790Sstevel@tonic-gate 	new->e_flags |= ENDPNT_ESTABLISHED;
16800Sstevel@tonic-gate 	new->e_flags |= ENDPNT_BOUND;
16810Sstevel@tonic-gate 	if (new->e_flags & ENDPNT_WAITING) {
16820Sstevel@tonic-gate 		cv_broadcast(&new->e_cv);
16830Sstevel@tonic-gate 		new->e_flags &= ~ENDPNT_WAITING;
16840Sstevel@tonic-gate 	}
16850Sstevel@tonic-gate 	mutex_exit(&new->e_lock);
16860Sstevel@tonic-gate 
16870Sstevel@tonic-gate 	return (new);
16880Sstevel@tonic-gate 
16890Sstevel@tonic-gate bad:
16900Sstevel@tonic-gate 	ASSERT(new != NULL);
16910Sstevel@tonic-gate 	/*
16920Sstevel@tonic-gate 	 * mark this endpoint as stale and notify any threads waiting
16930Sstevel@tonic-gate 	 * on this endpoint that it will be going away.
16940Sstevel@tonic-gate 	 */
16950Sstevel@tonic-gate 	mutex_enter(&new->e_lock);
16960Sstevel@tonic-gate 	if (new->e_ref > 0) {
16970Sstevel@tonic-gate 		new->e_flags |= ENDPNT_ESTABLISHED;
16980Sstevel@tonic-gate 		new->e_flags |= ENDPNT_STALE;
16990Sstevel@tonic-gate 		if (new->e_flags & ENDPNT_WAITING) {
17000Sstevel@tonic-gate 			cv_broadcast(&new->e_cv);
17010Sstevel@tonic-gate 			new->e_flags &= ~ENDPNT_WAITING;
17020Sstevel@tonic-gate 		}
17030Sstevel@tonic-gate 	}
17040Sstevel@tonic-gate 	new->e_ref--;
17050Sstevel@tonic-gate 	new->e_tiptr = NULL;
17060Sstevel@tonic-gate 	mutex_exit(&new->e_lock);
17070Sstevel@tonic-gate 
17080Sstevel@tonic-gate 	/*
17090Sstevel@tonic-gate 	 * If there was a transport endopoint opened, then close it.
17100Sstevel@tonic-gate 	 */
17110Sstevel@tonic-gate 	if (tiptr != NULL)
17120Sstevel@tonic-gate 		(void) t_kclose(tiptr, 1);
17130Sstevel@tonic-gate 
17140Sstevel@tonic-gate 	return (NULL);
17150Sstevel@tonic-gate }
17160Sstevel@tonic-gate 
17170Sstevel@tonic-gate /*
17180Sstevel@tonic-gate  * Release a referece to the endpoint
17190Sstevel@tonic-gate  */
17200Sstevel@tonic-gate static void
endpnt_rele(struct endpnt * sp)17210Sstevel@tonic-gate endpnt_rele(struct endpnt *sp)
17220Sstevel@tonic-gate {
17230Sstevel@tonic-gate 	mutex_enter(&sp->e_lock);
17240Sstevel@tonic-gate 	ASSERT(sp->e_ref > 0);
17250Sstevel@tonic-gate 	sp->e_ref--;
17260Sstevel@tonic-gate 	/*
17270Sstevel@tonic-gate 	 * If the ref count is zero, then start the idle timer and link
17280Sstevel@tonic-gate 	 * the endpoint onto the idle list.
17290Sstevel@tonic-gate 	 */
17300Sstevel@tonic-gate 	if (sp->e_ref == 0) {
17310Sstevel@tonic-gate 		sp->e_itime = gethrestime_sec();
17320Sstevel@tonic-gate 
17330Sstevel@tonic-gate 		/*
17340Sstevel@tonic-gate 		 * Check to see if the endpoint is already linked to the idle
17350Sstevel@tonic-gate 		 * list, so that we don't try to reinsert it.
17360Sstevel@tonic-gate 		 */
17370Sstevel@tonic-gate 		if (sp->e_flags & ENDPNT_ONIDLE) {
17380Sstevel@tonic-gate 			mutex_exit(&sp->e_lock);
17390Sstevel@tonic-gate 			mutex_enter(&sp->e_type->e_ilock);
17400Sstevel@tonic-gate 			endpnt_reap_settimer(sp->e_type);
17410Sstevel@tonic-gate 			mutex_exit(&sp->e_type->e_ilock);
17420Sstevel@tonic-gate 			return;
17430Sstevel@tonic-gate 		}
17440Sstevel@tonic-gate 
17450Sstevel@tonic-gate 		sp->e_flags |= ENDPNT_ONIDLE;
17460Sstevel@tonic-gate 		mutex_exit(&sp->e_lock);
17470Sstevel@tonic-gate 		mutex_enter(&sp->e_type->e_ilock);
17480Sstevel@tonic-gate 		list_insert_tail(&sp->e_type->e_ilist, sp);
17490Sstevel@tonic-gate 		endpnt_reap_settimer(sp->e_type);
17500Sstevel@tonic-gate 		mutex_exit(&sp->e_type->e_ilock);
17510Sstevel@tonic-gate 	} else
17520Sstevel@tonic-gate 		mutex_exit(&sp->e_lock);
17530Sstevel@tonic-gate }
17540Sstevel@tonic-gate 
17550Sstevel@tonic-gate static void
endpnt_reap_settimer(endpnt_type_t * etp)17560Sstevel@tonic-gate endpnt_reap_settimer(endpnt_type_t *etp)
17570Sstevel@tonic-gate {
17580Sstevel@tonic-gate 	if (etp->e_itimer == (timeout_id_t)0)
17590Sstevel@tonic-gate 		etp->e_itimer = timeout(endpnt_reap_dispatch, (void *)etp,
17606403Sgt29601 		    clnt_clts_taskq_dispatch_interval);
17610Sstevel@tonic-gate }
17620Sstevel@tonic-gate 
17630Sstevel@tonic-gate static void
endpnt_reap_dispatch(void * a)17640Sstevel@tonic-gate endpnt_reap_dispatch(void *a)
17650Sstevel@tonic-gate {
17660Sstevel@tonic-gate 	endpnt_type_t *etp = a;
17670Sstevel@tonic-gate 
17680Sstevel@tonic-gate 	/*
17690Sstevel@tonic-gate 	 * The idle timer has fired, so dispatch the taskq to close the
17700Sstevel@tonic-gate 	 * endpoint.
17710Sstevel@tonic-gate 	 */
17720Sstevel@tonic-gate 	if (taskq_dispatch(endpnt_taskq, (task_func_t *)endpnt_reap, etp,
17730Sstevel@tonic-gate 	    TQ_NOSLEEP) == NULL)
17740Sstevel@tonic-gate 		return;
17750Sstevel@tonic-gate 	mutex_enter(&etp->e_ilock);
17760Sstevel@tonic-gate 	etp->e_async_count++;
17770Sstevel@tonic-gate 	mutex_exit(&etp->e_ilock);
17780Sstevel@tonic-gate }
17790Sstevel@tonic-gate 
17800Sstevel@tonic-gate /*
17810Sstevel@tonic-gate  * Traverse the idle list and close those endpoints that have reached their
17820Sstevel@tonic-gate  * timeout interval.
17830Sstevel@tonic-gate  */
17840Sstevel@tonic-gate static void
endpnt_reap(endpnt_type_t * etp)17850Sstevel@tonic-gate endpnt_reap(endpnt_type_t *etp)
17860Sstevel@tonic-gate {
17870Sstevel@tonic-gate 	struct endpnt *e;
17880Sstevel@tonic-gate 	struct endpnt *next_node = NULL;
17890Sstevel@tonic-gate 
17900Sstevel@tonic-gate 	mutex_enter(&etp->e_ilock);
17910Sstevel@tonic-gate 	e = list_head(&etp->e_ilist);
17920Sstevel@tonic-gate 	while (e != NULL) {
17930Sstevel@tonic-gate 		next_node = list_next(&etp->e_ilist, e);
17940Sstevel@tonic-gate 
17950Sstevel@tonic-gate 		mutex_enter(&e->e_lock);
17960Sstevel@tonic-gate 		if (e->e_ref > 0) {
17970Sstevel@tonic-gate 			mutex_exit(&e->e_lock);
17980Sstevel@tonic-gate 			e = next_node;
17990Sstevel@tonic-gate 			continue;
18000Sstevel@tonic-gate 		}
18010Sstevel@tonic-gate 
18020Sstevel@tonic-gate 		ASSERT(e->e_ref == 0);
18030Sstevel@tonic-gate 		if (e->e_itime > 0 &&
18040Sstevel@tonic-gate 		    (e->e_itime + clnt_clts_endpoint_reap_interval) <
18050Sstevel@tonic-gate 		    gethrestime_sec()) {
18060Sstevel@tonic-gate 			e->e_flags &= ~ENDPNT_BOUND;
18070Sstevel@tonic-gate 			(void) t_kclose(e->e_tiptr, 1);
18080Sstevel@tonic-gate 			e->e_tiptr = NULL;
18090Sstevel@tonic-gate 			e->e_itime = 0;
18100Sstevel@tonic-gate 		}
18110Sstevel@tonic-gate 		mutex_exit(&e->e_lock);
18120Sstevel@tonic-gate 		e = next_node;
18130Sstevel@tonic-gate 	}
18140Sstevel@tonic-gate 	etp->e_itimer = 0;
18150Sstevel@tonic-gate 	if (--etp->e_async_count == 0)
18160Sstevel@tonic-gate 		cv_signal(&etp->e_async_cv);
18170Sstevel@tonic-gate 	mutex_exit(&etp->e_ilock);
18180Sstevel@tonic-gate }
18190Sstevel@tonic-gate 
18200Sstevel@tonic-gate static void
endpnt_reclaim(zoneid_t zoneid)18210Sstevel@tonic-gate endpnt_reclaim(zoneid_t zoneid)
18220Sstevel@tonic-gate {
18230Sstevel@tonic-gate 	struct endpnt_type *np;
18240Sstevel@tonic-gate 	struct endpnt *e;
18250Sstevel@tonic-gate 	struct endpnt *next_node = NULL;
18260Sstevel@tonic-gate 	list_t free_list;
18270Sstevel@tonic-gate 	int rcnt = 0;
18280Sstevel@tonic-gate 
18290Sstevel@tonic-gate 	list_create(&free_list, sizeof (endpnt_t), offsetof(endpnt_t, e_node));
18300Sstevel@tonic-gate 
18310Sstevel@tonic-gate 	RPCLOG0(1, "endpnt_reclaim: reclaim callback started\n");
18320Sstevel@tonic-gate 	rw_enter(&endpnt_type_lock, RW_READER);
18330Sstevel@tonic-gate 	for (np = endpnt_type_list; np != NULL; np = np->e_next) {
18340Sstevel@tonic-gate 		if (zoneid != ALL_ZONES && zoneid != np->e_zoneid)
18350Sstevel@tonic-gate 			continue;
18360Sstevel@tonic-gate 
18370Sstevel@tonic-gate 		mutex_enter(&np->e_plock);
18380Sstevel@tonic-gate 		RPCLOG(1, "endpnt_reclaim: protofmly %s, ",
18396403Sgt29601 		    np->e_protofmly);
18400Sstevel@tonic-gate 		RPCLOG(1, "rdev %ld\n", np->e_rdev);
18410Sstevel@tonic-gate 		RPCLOG(1, "endpnt_reclaim: found %d endpoint(s)\n",
18426403Sgt29601 		    np->e_cnt);
18430Sstevel@tonic-gate 
18440Sstevel@tonic-gate 		if (np->e_cnt == 0) {
18450Sstevel@tonic-gate 			mutex_exit(&np->e_plock);
18460Sstevel@tonic-gate 			continue;
18470Sstevel@tonic-gate 		}
18480Sstevel@tonic-gate 
18490Sstevel@tonic-gate 		/*
18500Sstevel@tonic-gate 		 * The nice thing about maintaining an idle list is that if
18510Sstevel@tonic-gate 		 * there are any endpoints to reclaim, they are going to be
18520Sstevel@tonic-gate 		 * on this list.  Just go through and reap the one's that
18530Sstevel@tonic-gate 		 * have ref counts of zero.
18540Sstevel@tonic-gate 		 */
18550Sstevel@tonic-gate 		mutex_enter(&np->e_ilock);
18560Sstevel@tonic-gate 		e = list_head(&np->e_ilist);
18570Sstevel@tonic-gate 		while (e != NULL) {
18580Sstevel@tonic-gate 			next_node = list_next(&np->e_ilist, e);
18590Sstevel@tonic-gate 			mutex_enter(&e->e_lock);
18600Sstevel@tonic-gate 			if (e->e_ref > 0) {
18610Sstevel@tonic-gate 				mutex_exit(&e->e_lock);
18620Sstevel@tonic-gate 				e = next_node;
18630Sstevel@tonic-gate 				continue;
18640Sstevel@tonic-gate 			}
18650Sstevel@tonic-gate 			ASSERT(e->e_ref == 0);
18660Sstevel@tonic-gate 			mutex_exit(&e->e_lock);
18670Sstevel@tonic-gate 
18680Sstevel@tonic-gate 			list_remove(&np->e_ilist, e);
18690Sstevel@tonic-gate 			list_remove(&np->e_pool, e);
18700Sstevel@tonic-gate 			list_insert_head(&free_list, e);
18710Sstevel@tonic-gate 
18720Sstevel@tonic-gate 			rcnt++;
18730Sstevel@tonic-gate 			np->e_cnt--;
18740Sstevel@tonic-gate 			e = next_node;
18750Sstevel@tonic-gate 		}
18760Sstevel@tonic-gate 		mutex_exit(&np->e_ilock);
18770Sstevel@tonic-gate 		/*
18780Sstevel@tonic-gate 		 * Reset the current pointer to be safe
18790Sstevel@tonic-gate 		 */
18800Sstevel@tonic-gate 		if ((e = (struct endpnt *)list_head(&np->e_pool)) != NULL)
18810Sstevel@tonic-gate 			np->e_pcurr = e;
18820Sstevel@tonic-gate 		else {
18830Sstevel@tonic-gate 			ASSERT(np->e_cnt == 0);
18840Sstevel@tonic-gate 			np->e_pcurr = NULL;
18850Sstevel@tonic-gate 		}
18860Sstevel@tonic-gate 
18870Sstevel@tonic-gate 		mutex_exit(&np->e_plock);
18880Sstevel@tonic-gate 	}
18890Sstevel@tonic-gate 	rw_exit(&endpnt_type_lock);
18900Sstevel@tonic-gate 
18910Sstevel@tonic-gate 	while ((e = list_head(&free_list)) != NULL) {
18920Sstevel@tonic-gate 		list_remove(&free_list, e);
18930Sstevel@tonic-gate 		if (e->e_tiptr != NULL)
18940Sstevel@tonic-gate 			(void) t_kclose(e->e_tiptr, 1);
18950Sstevel@tonic-gate 
18960Sstevel@tonic-gate 		cv_destroy(&e->e_cv);
18970Sstevel@tonic-gate 		mutex_destroy(&e->e_lock);
18980Sstevel@tonic-gate 		kmem_cache_free(endpnt_cache, e);
18990Sstevel@tonic-gate 	}
19000Sstevel@tonic-gate 	list_destroy(&free_list);
19010Sstevel@tonic-gate 	RPCLOG(1, "endpnt_reclaim: reclaimed %d endpoint(s)\n", rcnt);
19020Sstevel@tonic-gate }
19030Sstevel@tonic-gate 
19040Sstevel@tonic-gate /*
19050Sstevel@tonic-gate  * Endpoint reclaim zones destructor callback routine.
19060Sstevel@tonic-gate  *
19070Sstevel@tonic-gate  * After reclaiming any cached entries, we basically go through the endpnt_type
19080Sstevel@tonic-gate  * list, canceling outstanding timeouts and free'ing data structures.
19090Sstevel@tonic-gate  */
19100Sstevel@tonic-gate /* ARGSUSED */
19110Sstevel@tonic-gate static void
endpnt_destructor(zoneid_t zoneid,void * a)19120Sstevel@tonic-gate endpnt_destructor(zoneid_t zoneid, void *a)
19130Sstevel@tonic-gate {
19140Sstevel@tonic-gate 	struct endpnt_type **npp;
19150Sstevel@tonic-gate 	struct endpnt_type *np;
19160Sstevel@tonic-gate 	struct endpnt_type *free_list = NULL;
19170Sstevel@tonic-gate 	timeout_id_t t_id = 0;
19180Sstevel@tonic-gate 	extern void clcleanup_zone(zoneid_t);
19190Sstevel@tonic-gate 	extern void clcleanup4_zone(zoneid_t);
19200Sstevel@tonic-gate 
19210Sstevel@tonic-gate 	/* Make sure NFS client handles are released. */
19220Sstevel@tonic-gate 	clcleanup_zone(zoneid);
19230Sstevel@tonic-gate 	clcleanup4_zone(zoneid);
19240Sstevel@tonic-gate 
19250Sstevel@tonic-gate 	endpnt_reclaim(zoneid);
19260Sstevel@tonic-gate 	/*
19270Sstevel@tonic-gate 	 * We don't need to be holding on to any locks across the call to
19280Sstevel@tonic-gate 	 * endpnt_reclaim() and the code below; we know that no-one can
19290Sstevel@tonic-gate 	 * be holding open connections for this zone (all processes and kernel
19300Sstevel@tonic-gate 	 * threads are gone), so nothing could be adding anything to the list.
19310Sstevel@tonic-gate 	 */
19320Sstevel@tonic-gate 	rw_enter(&endpnt_type_lock, RW_WRITER);
19330Sstevel@tonic-gate 	npp = &endpnt_type_list;
19340Sstevel@tonic-gate 	while ((np = *npp) != NULL) {
19350Sstevel@tonic-gate 		if (np->e_zoneid != zoneid) {
19360Sstevel@tonic-gate 			npp = &np->e_next;
19370Sstevel@tonic-gate 			continue;
19380Sstevel@tonic-gate 		}
19390Sstevel@tonic-gate 		mutex_enter(&np->e_plock);
19400Sstevel@tonic-gate 		mutex_enter(&np->e_ilock);
19410Sstevel@tonic-gate 		if (np->e_itimer != 0) {
19420Sstevel@tonic-gate 			t_id = np->e_itimer;
19430Sstevel@tonic-gate 			np->e_itimer = 0;
19440Sstevel@tonic-gate 		}
19450Sstevel@tonic-gate 		ASSERT(np->e_cnt == 0);
19460Sstevel@tonic-gate 		ASSERT(list_head(&np->e_pool) == NULL);
19470Sstevel@tonic-gate 		ASSERT(list_head(&np->e_ilist) == NULL);
19480Sstevel@tonic-gate 
19490Sstevel@tonic-gate 		mutex_exit(&np->e_ilock);
19500Sstevel@tonic-gate 		mutex_exit(&np->e_plock);
19510Sstevel@tonic-gate 
19520Sstevel@tonic-gate 		/*
19530Sstevel@tonic-gate 		 * untimeout() any outstanding timers that have not yet fired.
19540Sstevel@tonic-gate 		 */
19550Sstevel@tonic-gate 		if (t_id != (timeout_id_t)0)
19560Sstevel@tonic-gate 			(void) untimeout(t_id);
19570Sstevel@tonic-gate 		*npp = np->e_next;
19580Sstevel@tonic-gate 		np->e_next = free_list;
19590Sstevel@tonic-gate 		free_list = np;
19600Sstevel@tonic-gate 	}
19610Sstevel@tonic-gate 	rw_exit(&endpnt_type_lock);
19620Sstevel@tonic-gate 
19630Sstevel@tonic-gate 	while (free_list != NULL) {
19640Sstevel@tonic-gate 		np = free_list;
19650Sstevel@tonic-gate 		free_list = free_list->e_next;
19660Sstevel@tonic-gate 		/*
19670Sstevel@tonic-gate 		 * Wait for threads in endpnt_taskq trying to reap endpnt_ts in
19680Sstevel@tonic-gate 		 * the endpnt_type_t.
19690Sstevel@tonic-gate 		 */
19700Sstevel@tonic-gate 		mutex_enter(&np->e_ilock);
19710Sstevel@tonic-gate 		while (np->e_async_count > 0)
19720Sstevel@tonic-gate 			cv_wait(&np->e_async_cv, &np->e_ilock);
19730Sstevel@tonic-gate 		cv_destroy(&np->e_async_cv);
19740Sstevel@tonic-gate 		mutex_destroy(&np->e_plock);
19750Sstevel@tonic-gate 		mutex_destroy(&np->e_ilock);
19760Sstevel@tonic-gate 		list_destroy(&np->e_pool);
19770Sstevel@tonic-gate 		list_destroy(&np->e_ilist);
19780Sstevel@tonic-gate 		kmem_free(np, sizeof (endpnt_type_t));
19790Sstevel@tonic-gate 	}
19800Sstevel@tonic-gate }
19810Sstevel@tonic-gate 
19820Sstevel@tonic-gate /*
19830Sstevel@tonic-gate  * Endpoint reclaim kmem callback routine.
19840Sstevel@tonic-gate  */
19850Sstevel@tonic-gate /* ARGSUSED */
19860Sstevel@tonic-gate static void
endpnt_repossess(void * a)19870Sstevel@tonic-gate endpnt_repossess(void *a)
19880Sstevel@tonic-gate {
19890Sstevel@tonic-gate 	/*
19900Sstevel@tonic-gate 	 * Reclaim idle endpnt's from all zones.
19910Sstevel@tonic-gate 	 */
19920Sstevel@tonic-gate 	if (endpnt_taskq != NULL)
19930Sstevel@tonic-gate 		(void) taskq_dispatch(endpnt_taskq,
19940Sstevel@tonic-gate 		    (task_func_t *)endpnt_reclaim, (void *)ALL_ZONES,
19950Sstevel@tonic-gate 		    TQ_NOSLEEP);
19960Sstevel@tonic-gate }
19970Sstevel@tonic-gate 
19980Sstevel@tonic-gate /*
19990Sstevel@tonic-gate  * RPC request dispatch routine.  Constructs a datagram message and wraps it
20000Sstevel@tonic-gate  * around the RPC request to pass downstream.
20010Sstevel@tonic-gate  */
20020Sstevel@tonic-gate static int
clnt_clts_dispatch_send(queue_t * q,mblk_t * mp,struct netbuf * addr,calllist_t * cp,uint_t xid,cred_t * cr)20030Sstevel@tonic-gate clnt_clts_dispatch_send(queue_t *q, mblk_t *mp, struct netbuf *addr,
20048778SErik.Nordmark@Sun.COM     calllist_t *cp, uint_t xid, cred_t *cr)
20050Sstevel@tonic-gate {
20060Sstevel@tonic-gate 	mblk_t *bp;
20070Sstevel@tonic-gate 	int msgsz;
20080Sstevel@tonic-gate 	struct T_unitdata_req *udreq;
20090Sstevel@tonic-gate 
20100Sstevel@tonic-gate 	/*
20110Sstevel@tonic-gate 	 * Set up the call record.
20120Sstevel@tonic-gate 	 */
20130Sstevel@tonic-gate 	cp->call_wq = q;
20140Sstevel@tonic-gate 	cp->call_xid = xid;
20150Sstevel@tonic-gate 	cp->call_status = RPC_TIMEDOUT;
20160Sstevel@tonic-gate 	cp->call_notified = FALSE;
20170Sstevel@tonic-gate 	RPCLOG(64,
20186403Sgt29601 	    "clnt_clts_dispatch_send: putting xid 0x%x on "
20196403Sgt29601 	    "dispatch list\n", xid);
20200Sstevel@tonic-gate 	cp->call_hash = call_hash(xid, clnt_clts_hash_size);
20210Sstevel@tonic-gate 	cp->call_bucket = &clts_call_ht[cp->call_hash];
20220Sstevel@tonic-gate 	call_table_enter(cp);
20230Sstevel@tonic-gate 
20240Sstevel@tonic-gate 	/*
20250Sstevel@tonic-gate 	 * Construct the datagram
20260Sstevel@tonic-gate 	 */
20270Sstevel@tonic-gate 	msgsz = (int)TUNITDATAREQSZ;
20288778SErik.Nordmark@Sun.COM 	/*
20298778SErik.Nordmark@Sun.COM 	 * Note: if the receiver uses SCM_UCRED/getpeerucred the pid will
20308778SErik.Nordmark@Sun.COM 	 * appear as -1.
20318778SErik.Nordmark@Sun.COM 	 */
20328778SErik.Nordmark@Sun.COM 	while (!(bp = allocb_cred(msgsz + addr->len, cr, NOPID))) {
20330Sstevel@tonic-gate 		if (strwaitbuf(msgsz + addr->len, BPRI_LO))
20340Sstevel@tonic-gate 			return (ENOSR);
20350Sstevel@tonic-gate 	}
20360Sstevel@tonic-gate 
20370Sstevel@tonic-gate 	udreq = (struct T_unitdata_req *)bp->b_wptr;
20380Sstevel@tonic-gate 	udreq->PRIM_type = T_UNITDATA_REQ;
20390Sstevel@tonic-gate 	udreq->DEST_length = addr->len;
20400Sstevel@tonic-gate 
20410Sstevel@tonic-gate 	if (addr->len) {
20420Sstevel@tonic-gate 		bcopy(addr->buf, bp->b_wptr + msgsz, addr->len);
20430Sstevel@tonic-gate 		udreq->DEST_offset = (t_scalar_t)msgsz;
20440Sstevel@tonic-gate 		msgsz += addr->len;
20450Sstevel@tonic-gate 	} else
20460Sstevel@tonic-gate 		udreq->DEST_offset = 0;
20470Sstevel@tonic-gate 	udreq->OPT_length = 0;
20480Sstevel@tonic-gate 	udreq->OPT_offset = 0;
20490Sstevel@tonic-gate 
20500Sstevel@tonic-gate 	bp->b_datap->db_type = M_PROTO;
20510Sstevel@tonic-gate 	bp->b_wptr += msgsz;
20520Sstevel@tonic-gate 
20530Sstevel@tonic-gate 	/*
20540Sstevel@tonic-gate 	 * Link the datagram header with the actual data
20550Sstevel@tonic-gate 	 */
20560Sstevel@tonic-gate 	linkb(bp, mp);
20570Sstevel@tonic-gate 
20580Sstevel@tonic-gate 	/*
20590Sstevel@tonic-gate 	 * Send downstream.
20600Sstevel@tonic-gate 	 */
20618205SSiddheshwar.Mahesh@Sun.COM 	if (canput(cp->call_wq)) {
20628205SSiddheshwar.Mahesh@Sun.COM 		put(cp->call_wq, bp);
20638205SSiddheshwar.Mahesh@Sun.COM 		return (0);
20648205SSiddheshwar.Mahesh@Sun.COM 	}
20650Sstevel@tonic-gate 
20668205SSiddheshwar.Mahesh@Sun.COM 	return (EIO);
20670Sstevel@tonic-gate }
20680Sstevel@tonic-gate 
20690Sstevel@tonic-gate /*
20700Sstevel@tonic-gate  * RPC response delivery routine.  Deliver the response to the waiting
20710Sstevel@tonic-gate  * thread by matching the xid.
20720Sstevel@tonic-gate  */
20730Sstevel@tonic-gate void
clnt_clts_dispatch_notify(mblk_t * mp,int resp_off,zoneid_t zoneid)20740Sstevel@tonic-gate clnt_clts_dispatch_notify(mblk_t *mp, int resp_off, zoneid_t zoneid)
20750Sstevel@tonic-gate {
20760Sstevel@tonic-gate 	calllist_t *e = NULL;
20770Sstevel@tonic-gate 	call_table_t *chtp;
20780Sstevel@tonic-gate 	uint32_t xid;
20790Sstevel@tonic-gate 	uint_t hash;
20800Sstevel@tonic-gate 	unsigned char *hdr_offset;
20810Sstevel@tonic-gate 	mblk_t *resp;
20820Sstevel@tonic-gate 
20830Sstevel@tonic-gate 	/*
20840Sstevel@tonic-gate 	 * If the RPC response is not contained in the same mblk as the
20850Sstevel@tonic-gate 	 * datagram header, then move to the next mblk.
20860Sstevel@tonic-gate 	 */
20870Sstevel@tonic-gate 	hdr_offset = mp->b_rptr;
20880Sstevel@tonic-gate 	resp = mp;
20890Sstevel@tonic-gate 	if ((mp->b_wptr - (mp->b_rptr + resp_off)) == 0)
20900Sstevel@tonic-gate 		resp = mp->b_cont;
20910Sstevel@tonic-gate 	else
20920Sstevel@tonic-gate 		resp->b_rptr += resp_off;
20930Sstevel@tonic-gate 
20940Sstevel@tonic-gate 	ASSERT(resp != NULL);
20950Sstevel@tonic-gate 
20960Sstevel@tonic-gate 	if ((IS_P2ALIGNED(resp->b_rptr, sizeof (uint32_t))) &&
20970Sstevel@tonic-gate 	    (resp->b_wptr - resp->b_rptr) >= sizeof (xid))
20980Sstevel@tonic-gate 		xid = *((uint32_t *)resp->b_rptr);
20990Sstevel@tonic-gate 	else {
21000Sstevel@tonic-gate 		int i = 0;
21010Sstevel@tonic-gate 		unsigned char *p = (unsigned char *)&xid;
21020Sstevel@tonic-gate 		unsigned char *rptr;
21030Sstevel@tonic-gate 		mblk_t *tmp = resp;
21040Sstevel@tonic-gate 
21050Sstevel@tonic-gate 		/*
21060Sstevel@tonic-gate 		 * Copy the xid, byte-by-byte into xid.
21070Sstevel@tonic-gate 		 */
21080Sstevel@tonic-gate 		while (tmp) {
21090Sstevel@tonic-gate 			rptr = tmp->b_rptr;
21100Sstevel@tonic-gate 			while (rptr < tmp->b_wptr) {
21110Sstevel@tonic-gate 				*p++ = *rptr++;
21120Sstevel@tonic-gate 				if (++i >= sizeof (xid))
21130Sstevel@tonic-gate 					goto done_xid_copy;
21140Sstevel@tonic-gate 			}
21150Sstevel@tonic-gate 			tmp = tmp->b_cont;
21160Sstevel@tonic-gate 		}
21170Sstevel@tonic-gate 
21180Sstevel@tonic-gate 		/*
21190Sstevel@tonic-gate 		 * If we got here, we ran out of mblk space before the
21200Sstevel@tonic-gate 		 * xid could be copied.
21210Sstevel@tonic-gate 		 */
21220Sstevel@tonic-gate 		ASSERT(tmp == NULL && i < sizeof (xid));
21230Sstevel@tonic-gate 
21240Sstevel@tonic-gate 		RPCLOG0(1,
21256403Sgt29601 		    "clnt_dispatch_notify(clts): message less than "
21266403Sgt29601 		    "size of xid\n");
21270Sstevel@tonic-gate 
21280Sstevel@tonic-gate 		freemsg(mp);
21290Sstevel@tonic-gate 		return;
21300Sstevel@tonic-gate 	}
21310Sstevel@tonic-gate 
21320Sstevel@tonic-gate done_xid_copy:
21330Sstevel@tonic-gate 
21340Sstevel@tonic-gate 	/*
21350Sstevel@tonic-gate 	 * Reset the read pointer back to the beginning of the protocol
21360Sstevel@tonic-gate 	 * header if we moved it.
21370Sstevel@tonic-gate 	 */
21380Sstevel@tonic-gate 	if (mp->b_rptr != hdr_offset)
21390Sstevel@tonic-gate 		mp->b_rptr = hdr_offset;
21400Sstevel@tonic-gate 
21410Sstevel@tonic-gate 	hash = call_hash(xid, clnt_clts_hash_size);
21420Sstevel@tonic-gate 	chtp = &clts_call_ht[hash];
21430Sstevel@tonic-gate 	/* call_table_find returns with the hash bucket locked */
21440Sstevel@tonic-gate 	call_table_find(chtp, xid, e);
21450Sstevel@tonic-gate 
21460Sstevel@tonic-gate 	if (e != NULL) {
21470Sstevel@tonic-gate 		mutex_enter(&e->call_lock);
21486403Sgt29601 
21496403Sgt29601 		/*
21506403Sgt29601 		 * verify that the reply is coming in on
21516403Sgt29601 		 * the same zone that it was sent from.
21526403Sgt29601 		 */
21536403Sgt29601 		if (e->call_zoneid != zoneid) {
21546403Sgt29601 			mutex_exit(&e->call_lock);
21556403Sgt29601 			mutex_exit(&chtp->ct_lock);
21569804SGerald.Thornbrugh@Sun.COM 			RPCLOG0(8, "clnt_dispatch_notify (clts): incorrect "
21579804SGerald.Thornbrugh@Sun.COM 			    "zoneid\n");
21586403Sgt29601 			freemsg(mp);
21596403Sgt29601 			return;
21606403Sgt29601 		}
21616403Sgt29601 
21620Sstevel@tonic-gate 		/*
21630Sstevel@tonic-gate 		 * found thread waiting for this reply.
21640Sstevel@tonic-gate 		 */
21650Sstevel@tonic-gate 		if (e->call_reply) {
21660Sstevel@tonic-gate 			RPCLOG(8,
21676403Sgt29601 			    "clnt_dispatch_notify (clts): discarding old "
21686403Sgt29601 			    "reply for xid 0x%x\n",
21696403Sgt29601 			    xid);
21700Sstevel@tonic-gate 			freemsg(e->call_reply);
21710Sstevel@tonic-gate 		}
21720Sstevel@tonic-gate 		e->call_notified = TRUE;
21730Sstevel@tonic-gate 		e->call_reply = mp;
21740Sstevel@tonic-gate 		e->call_status = RPC_SUCCESS;
21750Sstevel@tonic-gate 		cv_signal(&e->call_cv);
21760Sstevel@tonic-gate 		mutex_exit(&e->call_lock);
21770Sstevel@tonic-gate 		mutex_exit(&chtp->ct_lock);
21780Sstevel@tonic-gate 	} else {
21790Sstevel@tonic-gate 		zone_t *zone;
21800Sstevel@tonic-gate 		struct rpcstat *rpcstat;
21810Sstevel@tonic-gate 
21820Sstevel@tonic-gate 		mutex_exit(&chtp->ct_lock);
21830Sstevel@tonic-gate 		RPCLOG(8, "clnt_dispatch_notify (clts): no caller for reply "
21846403Sgt29601 		    "0x%x\n", xid);
21850Sstevel@tonic-gate 		freemsg(mp);
21860Sstevel@tonic-gate 		/*
21870Sstevel@tonic-gate 		 * This is unfortunate, but we need to lookup the zone so we
21880Sstevel@tonic-gate 		 * can increment its "rcbadxids" counter.
21890Sstevel@tonic-gate 		 */
21900Sstevel@tonic-gate 		zone = zone_find_by_id(zoneid);
21910Sstevel@tonic-gate 		if (zone == NULL) {
21920Sstevel@tonic-gate 			/*
21930Sstevel@tonic-gate 			 * The zone went away...
21940Sstevel@tonic-gate 			 */
21950Sstevel@tonic-gate 			return;
21960Sstevel@tonic-gate 		}
21970Sstevel@tonic-gate 		rpcstat = zone_getspecific(rpcstat_zone_key, zone);
21980Sstevel@tonic-gate 		if (zone_status_get(zone) >= ZONE_IS_SHUTTING_DOWN) {
21990Sstevel@tonic-gate 			/*
22000Sstevel@tonic-gate 			 * Not interested
22010Sstevel@tonic-gate 			 */
22020Sstevel@tonic-gate 			zone_rele(zone);
22030Sstevel@tonic-gate 			return;
22040Sstevel@tonic-gate 		}
22050Sstevel@tonic-gate 		RCSTAT_INCR(rpcstat->rpc_clts_client, rcbadxids);
22060Sstevel@tonic-gate 		zone_rele(zone);
22070Sstevel@tonic-gate 	}
22080Sstevel@tonic-gate }
22090Sstevel@tonic-gate 
22100Sstevel@tonic-gate /*
22110Sstevel@tonic-gate  * Init routine.  Called when rpcmod is loaded.
22120Sstevel@tonic-gate  */
22130Sstevel@tonic-gate void
clnt_clts_init(void)22140Sstevel@tonic-gate clnt_clts_init(void)
22150Sstevel@tonic-gate {
22160Sstevel@tonic-gate 	endpnt_cache = kmem_cache_create("clnt_clts_endpnt_cache",
22170Sstevel@tonic-gate 	    sizeof (struct endpnt), 0, NULL, NULL, endpnt_repossess, NULL,
22180Sstevel@tonic-gate 	    NULL, 0);
22190Sstevel@tonic-gate 
22200Sstevel@tonic-gate 	rw_init(&endpnt_type_lock, NULL, RW_DEFAULT, NULL);
22210Sstevel@tonic-gate 
22220Sstevel@tonic-gate 	/*
22230Sstevel@tonic-gate 	 * Perform simple bounds checking to make sure that the setting is
22240Sstevel@tonic-gate 	 * reasonable
22250Sstevel@tonic-gate 	 */
22260Sstevel@tonic-gate 	if (clnt_clts_max_endpoints <= 0) {
22270Sstevel@tonic-gate 		if (clnt_clts_do_bindresvport)
22280Sstevel@tonic-gate 			clnt_clts_max_endpoints = RESERVED_PORTSPACE;
22290Sstevel@tonic-gate 		else
22300Sstevel@tonic-gate 			clnt_clts_max_endpoints = NONRESERVED_PORTSPACE;
22310Sstevel@tonic-gate 	}
22320Sstevel@tonic-gate 
22330Sstevel@tonic-gate 	if (clnt_clts_do_bindresvport &&
22340Sstevel@tonic-gate 	    clnt_clts_max_endpoints > RESERVED_PORTSPACE)
22350Sstevel@tonic-gate 		clnt_clts_max_endpoints = RESERVED_PORTSPACE;
22360Sstevel@tonic-gate 	else if (clnt_clts_max_endpoints > NONRESERVED_PORTSPACE)
22370Sstevel@tonic-gate 		clnt_clts_max_endpoints = NONRESERVED_PORTSPACE;
22380Sstevel@tonic-gate 
22390Sstevel@tonic-gate 	if (clnt_clts_hash_size < DEFAULT_MIN_HASH_SIZE)
22400Sstevel@tonic-gate 		clnt_clts_hash_size = DEFAULT_MIN_HASH_SIZE;
22410Sstevel@tonic-gate 
22420Sstevel@tonic-gate 	/*
22430Sstevel@tonic-gate 	 * Defer creating the taskq until rpcmod gets pushed.  If we are
22440Sstevel@tonic-gate 	 * in diskless boot mode, rpcmod will get loaded early even before
22450Sstevel@tonic-gate 	 * thread_create() is available.
22460Sstevel@tonic-gate 	 */
22470Sstevel@tonic-gate 	endpnt_taskq = NULL;
22480Sstevel@tonic-gate 	taskq_created = FALSE;
22490Sstevel@tonic-gate 	mutex_init(&endpnt_taskq_lock, NULL, MUTEX_DEFAULT, NULL);
22500Sstevel@tonic-gate 
22510Sstevel@tonic-gate 	if (clnt_clts_endpoint_reap_interval < DEFAULT_ENDPOINT_REAP_INTERVAL)
22520Sstevel@tonic-gate 		clnt_clts_endpoint_reap_interval =
22536403Sgt29601 		    DEFAULT_ENDPOINT_REAP_INTERVAL;
22540Sstevel@tonic-gate 
22550Sstevel@tonic-gate 	/*
22560Sstevel@tonic-gate 	 * Dispatch the taskq at an interval which is offset from the
22570Sstevel@tonic-gate 	 * interval that the endpoints should be reaped.
22580Sstevel@tonic-gate 	 */
22590Sstevel@tonic-gate 	clnt_clts_taskq_dispatch_interval =
22606403Sgt29601 	    (clnt_clts_endpoint_reap_interval + DEFAULT_INTERVAL_SHIFT) * hz;
22610Sstevel@tonic-gate 
22620Sstevel@tonic-gate 	/*
22630Sstevel@tonic-gate 	 * Initialize the completion queue
22640Sstevel@tonic-gate 	 */
22650Sstevel@tonic-gate 	clts_call_ht = call_table_init(clnt_clts_hash_size);
22660Sstevel@tonic-gate 	/*
22670Sstevel@tonic-gate 	 * Initialize the zone destructor callback.
22680Sstevel@tonic-gate 	 */
22690Sstevel@tonic-gate 	zone_key_create(&endpnt_destructor_key, NULL, NULL, endpnt_destructor);
22700Sstevel@tonic-gate }
22710Sstevel@tonic-gate 
22720Sstevel@tonic-gate void
clnt_clts_fini(void)22730Sstevel@tonic-gate clnt_clts_fini(void)
22740Sstevel@tonic-gate {
22750Sstevel@tonic-gate 	(void) zone_key_delete(endpnt_destructor_key);
22760Sstevel@tonic-gate }
2277