xref: /titanic_44/usr/src/uts/common/rpc/clnt_clts.c (revision 1a5e258f5471356ca102c7176637cdce45bac147)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
545916cd2Sjpk  * Common Development and Distribution License (the "License").
645916cd2Sjpk  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
2251e44b2bSDai Ngo  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T
287c478bd9Sstevel@tonic-gate  * All Rights Reserved
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
337c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of California.
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate /*
387c478bd9Sstevel@tonic-gate  * Implements a kernel based, client side RPC.
397c478bd9Sstevel@tonic-gate  */
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #include <sys/param.h>
427c478bd9Sstevel@tonic-gate #include <sys/types.h>
437c478bd9Sstevel@tonic-gate #include <sys/systm.h>
447c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
457c478bd9Sstevel@tonic-gate #include <sys/stream.h>
467c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
477c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
487c478bd9Sstevel@tonic-gate #include <sys/tiuser.h>
497c478bd9Sstevel@tonic-gate #include <sys/tihdr.h>
507c478bd9Sstevel@tonic-gate #include <sys/t_kuser.h>
517c478bd9Sstevel@tonic-gate #include <sys/errno.h>
527c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
537c478bd9Sstevel@tonic-gate #include <sys/debug.h>
547c478bd9Sstevel@tonic-gate #include <sys/kstat.h>
557c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
567c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
577c478bd9Sstevel@tonic-gate #include <sys/conf.h>
587c478bd9Sstevel@tonic-gate #include <sys/disp.h>
597c478bd9Sstevel@tonic-gate #include <sys/taskq.h>
607c478bd9Sstevel@tonic-gate #include <sys/list.h>
617c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
627c478bd9Sstevel@tonic-gate #include <sys/zone.h>
637c478bd9Sstevel@tonic-gate #include <netinet/in.h>
647c478bd9Sstevel@tonic-gate #include <rpc/types.h>
657c478bd9Sstevel@tonic-gate #include <rpc/xdr.h>
667c478bd9Sstevel@tonic-gate #include <rpc/auth.h>
677c478bd9Sstevel@tonic-gate #include <rpc/clnt.h>
687c478bd9Sstevel@tonic-gate #include <rpc/rpc_msg.h>
697c478bd9Sstevel@tonic-gate 
7051e44b2bSDai Ngo #include <sys/sdt.h>
7151e44b2bSDai Ngo 
727c478bd9Sstevel@tonic-gate static enum clnt_stat clnt_clts_kcallit(CLIENT *, rpcproc_t, xdrproc_t,
737c478bd9Sstevel@tonic-gate 		    caddr_t, xdrproc_t, caddr_t, struct timeval);
747c478bd9Sstevel@tonic-gate static void	clnt_clts_kabort(CLIENT *);
757c478bd9Sstevel@tonic-gate static void	clnt_clts_kerror(CLIENT *, struct rpc_err *);
767c478bd9Sstevel@tonic-gate static bool_t	clnt_clts_kfreeres(CLIENT *, xdrproc_t, caddr_t);
777c478bd9Sstevel@tonic-gate static bool_t	clnt_clts_kcontrol(CLIENT *, int, char *);
787c478bd9Sstevel@tonic-gate static void	clnt_clts_kdestroy(CLIENT *);
797c478bd9Sstevel@tonic-gate static int	clnt_clts_ksettimers(CLIENT *, struct rpc_timers *,
807c478bd9Sstevel@tonic-gate 		    struct rpc_timers *, int, void (*)(), caddr_t, uint32_t);
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate /*
837c478bd9Sstevel@tonic-gate  * Operations vector for CLTS based RPC
847c478bd9Sstevel@tonic-gate  */
857c478bd9Sstevel@tonic-gate static struct clnt_ops clts_ops = {
867c478bd9Sstevel@tonic-gate 	clnt_clts_kcallit,	/* do rpc call */
877c478bd9Sstevel@tonic-gate 	clnt_clts_kabort,	/* abort call */
887c478bd9Sstevel@tonic-gate 	clnt_clts_kerror,	/* return error status */
897c478bd9Sstevel@tonic-gate 	clnt_clts_kfreeres,	/* free results */
907c478bd9Sstevel@tonic-gate 	clnt_clts_kdestroy,	/* destroy rpc handle */
917c478bd9Sstevel@tonic-gate 	clnt_clts_kcontrol,	/* the ioctl() of rpc */
927c478bd9Sstevel@tonic-gate 	clnt_clts_ksettimers	/* set retry timers */
937c478bd9Sstevel@tonic-gate };
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate /*
967c478bd9Sstevel@tonic-gate  * Endpoint for CLTS (INET, INET6, loopback, etc.)
977c478bd9Sstevel@tonic-gate  */
987c478bd9Sstevel@tonic-gate typedef struct endpnt_type {
997c478bd9Sstevel@tonic-gate 	struct endpnt_type *e_next;	/* pointer to next endpoint type */
1007c478bd9Sstevel@tonic-gate 	list_t		e_pool;		/* list of available endpoints */
10151e44b2bSDai Ngo 	list_t		e_ilist;	/* list of idle endpoints */
1027c478bd9Sstevel@tonic-gate 	struct endpnt	*e_pcurr;	/* pointer to current endpoint */
1037c478bd9Sstevel@tonic-gate 	char		e_protofmly[KNC_STRSIZE];	/* protocol family */
1047c478bd9Sstevel@tonic-gate 	dev_t		e_rdev;		/* device */
1057c478bd9Sstevel@tonic-gate 	kmutex_t	e_plock;	/* pool lock */
1067c478bd9Sstevel@tonic-gate 	kmutex_t	e_ilock;	/* idle list lock */
1077c478bd9Sstevel@tonic-gate 	timeout_id_t	e_itimer;	/* timer to dispatch the taskq */
1087c478bd9Sstevel@tonic-gate 	uint_t		e_cnt;		/* number of endpoints in the pool */
1097c478bd9Sstevel@tonic-gate 	zoneid_t	e_zoneid;	/* zoneid of endpoint type */
1107c478bd9Sstevel@tonic-gate 	kcondvar_t	e_async_cv;	/* cv for asynchronous reap threads */
1117c478bd9Sstevel@tonic-gate 	uint_t		e_async_count;	/* count of asynchronous reap threads */
1127c478bd9Sstevel@tonic-gate } endpnt_type_t;
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate typedef struct endpnt {
1157c478bd9Sstevel@tonic-gate 	list_node_t	e_node;		/* link to the pool */
1167c478bd9Sstevel@tonic-gate 	list_node_t	e_idle;		/* link to the idle list */
1177c478bd9Sstevel@tonic-gate 	endpnt_type_t	*e_type;	/* back pointer to endpoint type */
1187c478bd9Sstevel@tonic-gate 	TIUSER		*e_tiptr;	/* pointer to transport endpoint */
1197c478bd9Sstevel@tonic-gate 	queue_t		*e_wq;		/* write queue */
1207c478bd9Sstevel@tonic-gate 	uint_t		e_flags;	/* endpoint flags */
1217c478bd9Sstevel@tonic-gate 	uint_t		e_ref;		/* ref count on endpoint */
1227c478bd9Sstevel@tonic-gate 	kcondvar_t	e_cv;		/* condition variable */
1237c478bd9Sstevel@tonic-gate 	kmutex_t	e_lock;		/* protects cv and flags */
1247c478bd9Sstevel@tonic-gate 	time_t		e_itime;	/* time when rele'd */
1257c478bd9Sstevel@tonic-gate } endpnt_t;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate #define	ENDPNT_ESTABLISHED	0x1	/* endpoint is established */
1287c478bd9Sstevel@tonic-gate #define	ENDPNT_WAITING		0x2	/* thread waiting for endpoint */
1297c478bd9Sstevel@tonic-gate #define	ENDPNT_BOUND		0x4	/* endpoint is bound */
1307c478bd9Sstevel@tonic-gate #define	ENDPNT_STALE		0x8	/* endpoint is dead */
1317c478bd9Sstevel@tonic-gate #define	ENDPNT_ONIDLE		0x10	/* endpoint is on the idle list */
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate static krwlock_t	endpnt_type_lock; /* protects endpnt_type_list */
1347c478bd9Sstevel@tonic-gate static endpnt_type_t	*endpnt_type_list = NULL; /* list of CLTS endpoints */
1357c478bd9Sstevel@tonic-gate static struct kmem_cache	*endpnt_cache; /* cache of endpnt_t's */
1367c478bd9Sstevel@tonic-gate static taskq_t			*endpnt_taskq; /* endpnt_t reaper thread */
1377c478bd9Sstevel@tonic-gate static bool_t			taskq_created; /* flag for endpnt_taskq */
1387c478bd9Sstevel@tonic-gate static kmutex_t			endpnt_taskq_lock; /* taskq lock */
1397c478bd9Sstevel@tonic-gate static zone_key_t		endpnt_destructor_key;
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate #define	DEFAULT_ENDPOINT_REAP_INTERVAL 60 /* 1 minute */
1427c478bd9Sstevel@tonic-gate #define	DEFAULT_INTERVAL_SHIFT 30 /* 30 seconds */
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate /*
1457c478bd9Sstevel@tonic-gate  * Endpoint tunables
1467c478bd9Sstevel@tonic-gate  */
1477c478bd9Sstevel@tonic-gate static int	clnt_clts_max_endpoints = -1;
1487c478bd9Sstevel@tonic-gate static int	clnt_clts_hash_size = DEFAULT_HASH_SIZE;
1497c478bd9Sstevel@tonic-gate static time_t	clnt_clts_endpoint_reap_interval = -1;
1507c478bd9Sstevel@tonic-gate static clock_t	clnt_clts_taskq_dispatch_interval;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate /*
1537c478bd9Sstevel@tonic-gate  * Response completion hash queue
1547c478bd9Sstevel@tonic-gate  */
1557c478bd9Sstevel@tonic-gate static call_table_t *clts_call_ht;
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate /*
1587c478bd9Sstevel@tonic-gate  * Routines for the endpoint manager
1597c478bd9Sstevel@tonic-gate  */
1607c478bd9Sstevel@tonic-gate static struct endpnt_type *endpnt_type_create(struct knetconfig *);
1617c478bd9Sstevel@tonic-gate static void endpnt_type_free(struct endpnt_type *);
1627c478bd9Sstevel@tonic-gate static int check_endpnt(struct endpnt *, struct endpnt **);
16378598ee3Snd150628 static struct endpnt *endpnt_get(struct knetconfig *, int);
1647c478bd9Sstevel@tonic-gate static void endpnt_rele(struct endpnt *);
1657c478bd9Sstevel@tonic-gate static void endpnt_reap_settimer(endpnt_type_t *);
1667c478bd9Sstevel@tonic-gate static void endpnt_reap(endpnt_type_t *);
1677c478bd9Sstevel@tonic-gate static void endpnt_reap_dispatch(void *);
1687c478bd9Sstevel@tonic-gate static void endpnt_reclaim(zoneid_t);
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate /*
1727c478bd9Sstevel@tonic-gate  * Request dipatching function.
1737c478bd9Sstevel@tonic-gate  */
1747c478bd9Sstevel@tonic-gate static int clnt_clts_dispatch_send(queue_t *q, mblk_t *, struct netbuf *addr,
175de8c4a14SErik Nordmark 					calllist_t *, uint_t, cred_t *);
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate /*
1787c478bd9Sstevel@tonic-gate  * The size of the preserialized RPC header information.
1797c478bd9Sstevel@tonic-gate  */
1807c478bd9Sstevel@tonic-gate #define	CKU_HDRSIZE	20
1817c478bd9Sstevel@tonic-gate /*
1827c478bd9Sstevel@tonic-gate  * The initial allocation size.  It is small to reduce space requirements.
1837c478bd9Sstevel@tonic-gate  */
1847c478bd9Sstevel@tonic-gate #define	CKU_INITSIZE	2048
1857c478bd9Sstevel@tonic-gate /*
1867c478bd9Sstevel@tonic-gate  * The size of additional allocations, if required.  It is larger to
1877c478bd9Sstevel@tonic-gate  * reduce the number of actual allocations.
1887c478bd9Sstevel@tonic-gate  */
1897c478bd9Sstevel@tonic-gate #define	CKU_ALLOCSIZE	8192
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate /*
1927c478bd9Sstevel@tonic-gate  * Private data per rpc handle.  This structure is allocated by
1937c478bd9Sstevel@tonic-gate  * clnt_clts_kcreate, and freed by clnt_clts_kdestroy.
1947c478bd9Sstevel@tonic-gate  */
1957c478bd9Sstevel@tonic-gate struct cku_private {
1967c478bd9Sstevel@tonic-gate 	CLIENT			 cku_client;	/* client handle */
1977c478bd9Sstevel@tonic-gate 	int			 cku_retrys;	/* request retrys */
1987c478bd9Sstevel@tonic-gate 	calllist_t		 cku_call;
1997c478bd9Sstevel@tonic-gate 	struct endpnt		*cku_endpnt;	/* open end point */
2007c478bd9Sstevel@tonic-gate 	struct knetconfig	 cku_config;
2017c478bd9Sstevel@tonic-gate 	struct netbuf		 cku_addr;	/* remote address */
2027c478bd9Sstevel@tonic-gate 	struct rpc_err		 cku_err;	/* error status */
2037c478bd9Sstevel@tonic-gate 	XDR			 cku_outxdr;	/* xdr stream for output */
2047c478bd9Sstevel@tonic-gate 	XDR			 cku_inxdr;	/* xdr stream for input */
2057c478bd9Sstevel@tonic-gate 	char			 cku_rpchdr[CKU_HDRSIZE + 4]; /* rpc header */
2067c478bd9Sstevel@tonic-gate 	struct cred		*cku_cred;	/* credentials */
2077c478bd9Sstevel@tonic-gate 	struct rpc_timers	*cku_timers;	/* for estimating RTT */
2087c478bd9Sstevel@tonic-gate 	struct rpc_timers	*cku_timeall;	/* for estimating RTT */
2097c478bd9Sstevel@tonic-gate 	void			 (*cku_feedback)(int, int, caddr_t);
2107c478bd9Sstevel@tonic-gate 						/* ptr to feedback rtn */
2117c478bd9Sstevel@tonic-gate 	caddr_t			 cku_feedarg;	/* argument for feedback func */
2127c478bd9Sstevel@tonic-gate 	uint32_t		 cku_xid;	/* current XID */
2137c478bd9Sstevel@tonic-gate 	bool_t			 cku_bcast;	/* RPC broadcast hint */
21478598ee3Snd150628 	int			cku_useresvport; /* Use reserved port */
2157c478bd9Sstevel@tonic-gate 	struct rpc_clts_client	*cku_stats;	/* counters for the zone */
2167c478bd9Sstevel@tonic-gate };
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate static const struct rpc_clts_client {
2197c478bd9Sstevel@tonic-gate 	kstat_named_t	rccalls;
2207c478bd9Sstevel@tonic-gate 	kstat_named_t	rcbadcalls;
2217c478bd9Sstevel@tonic-gate 	kstat_named_t	rcretrans;
2227c478bd9Sstevel@tonic-gate 	kstat_named_t	rcbadxids;
2237c478bd9Sstevel@tonic-gate 	kstat_named_t	rctimeouts;
2247c478bd9Sstevel@tonic-gate 	kstat_named_t	rcnewcreds;
2257c478bd9Sstevel@tonic-gate 	kstat_named_t	rcbadverfs;
2267c478bd9Sstevel@tonic-gate 	kstat_named_t	rctimers;
2277c478bd9Sstevel@tonic-gate 	kstat_named_t	rcnomem;
2287c478bd9Sstevel@tonic-gate 	kstat_named_t	rccantsend;
2297c478bd9Sstevel@tonic-gate } clts_rcstat_tmpl = {
2307c478bd9Sstevel@tonic-gate 	{ "calls",	KSTAT_DATA_UINT64 },
2317c478bd9Sstevel@tonic-gate 	{ "badcalls",	KSTAT_DATA_UINT64 },
2327c478bd9Sstevel@tonic-gate 	{ "retrans",	KSTAT_DATA_UINT64 },
2337c478bd9Sstevel@tonic-gate 	{ "badxids",	KSTAT_DATA_UINT64 },
2347c478bd9Sstevel@tonic-gate 	{ "timeouts",	KSTAT_DATA_UINT64 },
2357c478bd9Sstevel@tonic-gate 	{ "newcreds",	KSTAT_DATA_UINT64 },
2367c478bd9Sstevel@tonic-gate 	{ "badverfs",	KSTAT_DATA_UINT64 },
2377c478bd9Sstevel@tonic-gate 	{ "timers",	KSTAT_DATA_UINT64 },
2387c478bd9Sstevel@tonic-gate 	{ "nomem",	KSTAT_DATA_UINT64 },
2397c478bd9Sstevel@tonic-gate 	{ "cantsend",	KSTAT_DATA_UINT64 },
2407c478bd9Sstevel@tonic-gate };
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate static uint_t clts_rcstat_ndata =
2437c478bd9Sstevel@tonic-gate 	sizeof (clts_rcstat_tmpl) / sizeof (kstat_named_t);
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate #define	RCSTAT_INCR(s, x)			\
246*1a5e258fSJosef 'Jeff' Sipek 	atomic_inc_64(&(s)->x.value.ui64)
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate #define	ptoh(p)		(&((p)->cku_client))
2497c478bd9Sstevel@tonic-gate #define	htop(h)		((struct cku_private *)((h)->cl_private))
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate /*
2527c478bd9Sstevel@tonic-gate  * Times to retry
2537c478bd9Sstevel@tonic-gate  */
2547c478bd9Sstevel@tonic-gate #define	SNDTRIES	4
2557c478bd9Sstevel@tonic-gate #define	REFRESHES	2	/* authentication refreshes */
2567c478bd9Sstevel@tonic-gate 
2575bd9f8f1Srg137905 /*
2585bd9f8f1Srg137905  * The following is used to determine the global default behavior for
2595bd9f8f1Srg137905  * CLTS when binding to a local port.
2605bd9f8f1Srg137905  *
2615bd9f8f1Srg137905  * If the value is set to 1 the default will be to select a reserved
2625bd9f8f1Srg137905  * (aka privileged) port, if the value is zero the default will be to
2635bd9f8f1Srg137905  * use non-reserved ports.  Users of kRPC may override this by using
2645bd9f8f1Srg137905  * CLNT_CONTROL() and CLSET_BINDRESVPORT.
2655bd9f8f1Srg137905  */
2665bd9f8f1Srg137905 static int clnt_clts_do_bindresvport = 1;
2675bd9f8f1Srg137905 
2687c478bd9Sstevel@tonic-gate #define	BINDRESVPORT_RETRIES 5
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate void
clnt_clts_stats_init(zoneid_t zoneid,struct rpc_clts_client ** statsp)2717c478bd9Sstevel@tonic-gate clnt_clts_stats_init(zoneid_t zoneid, struct rpc_clts_client **statsp)
2727c478bd9Sstevel@tonic-gate {
2737c478bd9Sstevel@tonic-gate 	kstat_t *ksp;
2747c478bd9Sstevel@tonic-gate 	kstat_named_t *knp;
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	knp = rpcstat_zone_init_common(zoneid, "unix", "rpc_clts_client",
2777c478bd9Sstevel@tonic-gate 	    (const kstat_named_t *)&clts_rcstat_tmpl,
2787c478bd9Sstevel@tonic-gate 	    sizeof (clts_rcstat_tmpl));
2797c478bd9Sstevel@tonic-gate 	/*
2807c478bd9Sstevel@tonic-gate 	 * Backwards compatibility for old kstat clients
2817c478bd9Sstevel@tonic-gate 	 */
2827c478bd9Sstevel@tonic-gate 	ksp = kstat_create_zone("unix", 0, "rpc_client", "rpc",
2837c478bd9Sstevel@tonic-gate 	    KSTAT_TYPE_NAMED, clts_rcstat_ndata,
2847c478bd9Sstevel@tonic-gate 	    KSTAT_FLAG_VIRTUAL | KSTAT_FLAG_WRITABLE, zoneid);
2857c478bd9Sstevel@tonic-gate 	if (ksp) {
2867c478bd9Sstevel@tonic-gate 		ksp->ks_data = knp;
2877c478bd9Sstevel@tonic-gate 		kstat_install(ksp);
2887c478bd9Sstevel@tonic-gate 	}
2897c478bd9Sstevel@tonic-gate 	*statsp = (struct rpc_clts_client *)knp;
2907c478bd9Sstevel@tonic-gate }
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate void
clnt_clts_stats_fini(zoneid_t zoneid,struct rpc_clts_client ** statsp)2937c478bd9Sstevel@tonic-gate clnt_clts_stats_fini(zoneid_t zoneid, struct rpc_clts_client **statsp)
2947c478bd9Sstevel@tonic-gate {
2957c478bd9Sstevel@tonic-gate 	rpcstat_zone_fini_common(zoneid, "unix", "rpc_clts_client");
2967c478bd9Sstevel@tonic-gate 	kstat_delete_byname_zone("unix", 0, "rpc_client", zoneid);
2977c478bd9Sstevel@tonic-gate 	kmem_free(*statsp, sizeof (clts_rcstat_tmpl));
2987c478bd9Sstevel@tonic-gate }
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate /*
3017c478bd9Sstevel@tonic-gate  * Create an rpc handle for a clts rpc connection.
3027c478bd9Sstevel@tonic-gate  * Allocates space for the handle structure and the private data.
3037c478bd9Sstevel@tonic-gate  */
3047c478bd9Sstevel@tonic-gate /* ARGSUSED */
3057c478bd9Sstevel@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)3067c478bd9Sstevel@tonic-gate clnt_clts_kcreate(struct knetconfig *config, struct netbuf *addr,
3077c478bd9Sstevel@tonic-gate 	rpcprog_t pgm, rpcvers_t vers, int retrys, struct cred *cred,
3087c478bd9Sstevel@tonic-gate 	CLIENT **cl)
3097c478bd9Sstevel@tonic-gate {
3107c478bd9Sstevel@tonic-gate 	CLIENT *h;
3117c478bd9Sstevel@tonic-gate 	struct cku_private *p;
3127c478bd9Sstevel@tonic-gate 	struct rpc_msg call_msg;
3137c478bd9Sstevel@tonic-gate 	int error;
3147c478bd9Sstevel@tonic-gate 	int plen;
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 	if (cl == NULL)
3177c478bd9Sstevel@tonic-gate 		return (EINVAL);
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	*cl = NULL;
3207c478bd9Sstevel@tonic-gate 	error = 0;
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	p = kmem_zalloc(sizeof (*p), KM_SLEEP);
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	h = ptoh(p);
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	/* handle */
3277c478bd9Sstevel@tonic-gate 	h->cl_ops = &clts_ops;
3287c478bd9Sstevel@tonic-gate 	h->cl_private = (caddr_t)p;
3297c478bd9Sstevel@tonic-gate 	h->cl_auth = authkern_create();
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	/* call message, just used to pre-serialize below */
3327c478bd9Sstevel@tonic-gate 	call_msg.rm_xid = 0;
3337c478bd9Sstevel@tonic-gate 	call_msg.rm_direction = CALL;
3347c478bd9Sstevel@tonic-gate 	call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
3357c478bd9Sstevel@tonic-gate 	call_msg.rm_call.cb_prog = pgm;
3367c478bd9Sstevel@tonic-gate 	call_msg.rm_call.cb_vers = vers;
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 	/* private */
3397c478bd9Sstevel@tonic-gate 	clnt_clts_kinit(h, addr, retrys, cred);
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	xdrmem_create(&p->cku_outxdr, p->cku_rpchdr, CKU_HDRSIZE, XDR_ENCODE);
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	/* pre-serialize call message header */
3447c478bd9Sstevel@tonic-gate 	if (!xdr_callhdr(&p->cku_outxdr, &call_msg)) {
3457c478bd9Sstevel@tonic-gate 		error = EINVAL;		/* XXX */
3467c478bd9Sstevel@tonic-gate 		goto bad;
3477c478bd9Sstevel@tonic-gate 	}
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	p->cku_config.knc_rdev = config->knc_rdev;
3507c478bd9Sstevel@tonic-gate 	p->cku_config.knc_semantics = config->knc_semantics;
3517c478bd9Sstevel@tonic-gate 	plen = strlen(config->knc_protofmly) + 1;
3527c478bd9Sstevel@tonic-gate 	p->cku_config.knc_protofmly = kmem_alloc(plen, KM_SLEEP);
3537c478bd9Sstevel@tonic-gate 	bcopy(config->knc_protofmly, p->cku_config.knc_protofmly, plen);
35478598ee3Snd150628 	p->cku_useresvport = -1; /* value is has not been set */
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	cv_init(&p->cku_call.call_cv, NULL, CV_DEFAULT, NULL);
3577c478bd9Sstevel@tonic-gate 	mutex_init(&p->cku_call.call_lock, NULL, MUTEX_DEFAULT, NULL);
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 	*cl = h;
3607c478bd9Sstevel@tonic-gate 	return (0);
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate bad:
3637c478bd9Sstevel@tonic-gate 	auth_destroy(h->cl_auth);
3647c478bd9Sstevel@tonic-gate 	kmem_free(p->cku_addr.buf, addr->maxlen);
3657c478bd9Sstevel@tonic-gate 	kmem_free(p, sizeof (struct cku_private));
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	return (error);
3687c478bd9Sstevel@tonic-gate }
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate void
clnt_clts_kinit(CLIENT * h,struct netbuf * addr,int retrys,cred_t * cred)3717c478bd9Sstevel@tonic-gate clnt_clts_kinit(CLIENT *h, struct netbuf *addr, int retrys, cred_t *cred)
3727c478bd9Sstevel@tonic-gate {
3737c478bd9Sstevel@tonic-gate 	/* LINTED pointer alignment */
3747c478bd9Sstevel@tonic-gate 	struct cku_private *p = htop(h);
3757c478bd9Sstevel@tonic-gate 	struct rpcstat *rsp;
3767c478bd9Sstevel@tonic-gate 
377108322fbScarlsonj 	rsp = zone_getspecific(rpcstat_zone_key, rpc_zone());
3787c478bd9Sstevel@tonic-gate 	ASSERT(rsp != NULL);
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	p->cku_retrys = retrys;
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	if (p->cku_addr.maxlen < addr->len) {
3837c478bd9Sstevel@tonic-gate 		if (p->cku_addr.maxlen != 0 && p->cku_addr.buf != NULL)
3847c478bd9Sstevel@tonic-gate 			kmem_free(p->cku_addr.buf, p->cku_addr.maxlen);
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 		p->cku_addr.buf = kmem_zalloc(addr->maxlen, KM_SLEEP);
3877c478bd9Sstevel@tonic-gate 		p->cku_addr.maxlen = addr->maxlen;
3887c478bd9Sstevel@tonic-gate 	}
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 	p->cku_addr.len = addr->len;
3917c478bd9Sstevel@tonic-gate 	bcopy(addr->buf, p->cku_addr.buf, addr->len);
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 	p->cku_cred = cred;
3947c478bd9Sstevel@tonic-gate 	p->cku_xid = 0;
3957c478bd9Sstevel@tonic-gate 	p->cku_timers = NULL;
3967c478bd9Sstevel@tonic-gate 	p->cku_timeall = NULL;
3977c478bd9Sstevel@tonic-gate 	p->cku_feedback = NULL;
3987c478bd9Sstevel@tonic-gate 	p->cku_bcast = FALSE;
3997c478bd9Sstevel@tonic-gate 	p->cku_call.call_xid = 0;
4007c478bd9Sstevel@tonic-gate 	p->cku_call.call_hash = 0;
4017c478bd9Sstevel@tonic-gate 	p->cku_call.call_notified = FALSE;
4027c478bd9Sstevel@tonic-gate 	p->cku_call.call_next = NULL;
4037c478bd9Sstevel@tonic-gate 	p->cku_call.call_prev = NULL;
4047c478bd9Sstevel@tonic-gate 	p->cku_call.call_reply = NULL;
4057c478bd9Sstevel@tonic-gate 	p->cku_call.call_wq = NULL;
4067c478bd9Sstevel@tonic-gate 	p->cku_stats = rsp->rpc_clts_client;
4077c478bd9Sstevel@tonic-gate }
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate /*
4107c478bd9Sstevel@tonic-gate  * set the timers.  Return current retransmission timeout.
4117c478bd9Sstevel@tonic-gate  */
4127c478bd9Sstevel@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)4137c478bd9Sstevel@tonic-gate clnt_clts_ksettimers(CLIENT *h, struct rpc_timers *t, struct rpc_timers *all,
4147c478bd9Sstevel@tonic-gate 	int minimum, void (*feedback)(int, int, caddr_t), caddr_t arg,
4157c478bd9Sstevel@tonic-gate 	uint32_t xid)
4167c478bd9Sstevel@tonic-gate {
4177c478bd9Sstevel@tonic-gate 	/* LINTED pointer alignment */
4187c478bd9Sstevel@tonic-gate 	struct cku_private *p = htop(h);
4197c478bd9Sstevel@tonic-gate 	int value;
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 	p->cku_feedback = feedback;
4227c478bd9Sstevel@tonic-gate 	p->cku_feedarg = arg;
4237c478bd9Sstevel@tonic-gate 	p->cku_timers = t;
4247c478bd9Sstevel@tonic-gate 	p->cku_timeall = all;
4257c478bd9Sstevel@tonic-gate 	if (xid)
4267c478bd9Sstevel@tonic-gate 		p->cku_xid = xid;
4277c478bd9Sstevel@tonic-gate 	value = all->rt_rtxcur;
4287c478bd9Sstevel@tonic-gate 	value += t->rt_rtxcur;
4297c478bd9Sstevel@tonic-gate 	if (value < minimum)
4307c478bd9Sstevel@tonic-gate 		return (minimum);
4317c478bd9Sstevel@tonic-gate 	RCSTAT_INCR(p->cku_stats, rctimers);
4327c478bd9Sstevel@tonic-gate 	return (value);
4337c478bd9Sstevel@tonic-gate }
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate /*
4367c478bd9Sstevel@tonic-gate  * Time out back off function. tim is in HZ
4377c478bd9Sstevel@tonic-gate  */
4387c478bd9Sstevel@tonic-gate #define	MAXTIMO	(20 * hz)
4397c478bd9Sstevel@tonic-gate #define	backoff(tim)	(((tim) < MAXTIMO) ? dobackoff(tim) : (tim))
4407c478bd9Sstevel@tonic-gate #define	dobackoff(tim)	((((tim) << 1) > MAXTIMO) ? MAXTIMO : ((tim) << 1))
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate #define	RETRY_POLL_TIMO	30
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate /*
4457c478bd9Sstevel@tonic-gate  * Call remote procedure.
4467c478bd9Sstevel@tonic-gate  * Most of the work of rpc is done here.  We serialize what is left
4477c478bd9Sstevel@tonic-gate  * of the header (some was pre-serialized in the handle), serialize
4487c478bd9Sstevel@tonic-gate  * the arguments, and send it off.  We wait for a reply or a time out.
4497c478bd9Sstevel@tonic-gate  * Timeout causes an immediate return, other packet problems may cause
4507c478bd9Sstevel@tonic-gate  * a retry on the receive.  When a good packet is received we deserialize
4517c478bd9Sstevel@tonic-gate  * it, and check verification.  A bad reply code will cause one retry
4527c478bd9Sstevel@tonic-gate  * with full (longhand) credentials.
4537c478bd9Sstevel@tonic-gate  */
4547c478bd9Sstevel@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)4557c478bd9Sstevel@tonic-gate clnt_clts_kcallit_addr(CLIENT *h, rpcproc_t procnum, xdrproc_t xdr_args,
4567c478bd9Sstevel@tonic-gate 	caddr_t argsp, xdrproc_t xdr_results, caddr_t resultsp,
4577c478bd9Sstevel@tonic-gate 	struct timeval wait, struct netbuf *sin)
4587c478bd9Sstevel@tonic-gate {
4597c478bd9Sstevel@tonic-gate 	/* LINTED pointer alignment */
4607c478bd9Sstevel@tonic-gate 	struct cku_private *p = htop(h);
4617c478bd9Sstevel@tonic-gate 	XDR *xdrs;
4627c478bd9Sstevel@tonic-gate 	int stries = p->cku_retrys;
4637c478bd9Sstevel@tonic-gate 	int refreshes = REFRESHES;	/* number of times to refresh cred */
4647c478bd9Sstevel@tonic-gate 	int round_trip;			/* time the RPC */
4657c478bd9Sstevel@tonic-gate 	int error;
4667c478bd9Sstevel@tonic-gate 	mblk_t *mp;
4677c478bd9Sstevel@tonic-gate 	mblk_t *mpdup;
4687c478bd9Sstevel@tonic-gate 	mblk_t *resp = NULL;
4697c478bd9Sstevel@tonic-gate 	mblk_t *tmp;
4707c478bd9Sstevel@tonic-gate 	calllist_t *call = &p->cku_call;
47151e44b2bSDai Ngo 	clock_t ori_timout, timout;
4727c478bd9Sstevel@tonic-gate 	bool_t interrupted;
4737c478bd9Sstevel@tonic-gate 	enum clnt_stat status;
4747c478bd9Sstevel@tonic-gate 	struct rpc_msg reply_msg;
4757c478bd9Sstevel@tonic-gate 	enum clnt_stat re_status;
47678598ee3Snd150628 	endpnt_t *endpt;
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 	RCSTAT_INCR(p->cku_stats, rccalls);
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	RPCLOG(2, "clnt_clts_kcallit_addr: wait.tv_sec: %ld\n", wait.tv_sec);
4817c478bd9Sstevel@tonic-gate 	RPCLOG(2, "clnt_clts_kcallit_addr: wait.tv_usec: %ld\n", wait.tv_usec);
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	timout = TIMEVAL_TO_TICK(&wait);
48451e44b2bSDai Ngo 	ori_timout = timout;
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 	if (p->cku_xid == 0) {
4877c478bd9Sstevel@tonic-gate 		p->cku_xid = alloc_xid();
4887c478bd9Sstevel@tonic-gate 		if (p->cku_endpnt != NULL)
4897c478bd9Sstevel@tonic-gate 			endpnt_rele(p->cku_endpnt);
4907c478bd9Sstevel@tonic-gate 		p->cku_endpnt = NULL;
4917c478bd9Sstevel@tonic-gate 	}
4928ffff9fdSgt29601 	call->call_zoneid = rpc_zoneid();
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 	mpdup = NULL;
4957c478bd9Sstevel@tonic-gate call_again:
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	if (mpdup == NULL) {
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 		while ((mp = allocb(CKU_INITSIZE, BPRI_LO)) == NULL) {
5007c478bd9Sstevel@tonic-gate 			if (strwaitbuf(CKU_INITSIZE, BPRI_LO)) {
5017c478bd9Sstevel@tonic-gate 				p->cku_err.re_status = RPC_SYSTEMERROR;
5027c478bd9Sstevel@tonic-gate 				p->cku_err.re_errno = ENOSR;
5037c478bd9Sstevel@tonic-gate 				goto done;
5047c478bd9Sstevel@tonic-gate 			}
5057c478bd9Sstevel@tonic-gate 		}
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 		xdrs = &p->cku_outxdr;
5087c478bd9Sstevel@tonic-gate 		xdrmblk_init(xdrs, mp, XDR_ENCODE, CKU_ALLOCSIZE);
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 		if (h->cl_auth->ah_cred.oa_flavor != RPCSEC_GSS) {
5117c478bd9Sstevel@tonic-gate 			/*
5127c478bd9Sstevel@tonic-gate 			 * Copy in the preserialized RPC header
5137c478bd9Sstevel@tonic-gate 			 * information.
5147c478bd9Sstevel@tonic-gate 			 */
5157c478bd9Sstevel@tonic-gate 			bcopy(p->cku_rpchdr, mp->b_rptr, CKU_HDRSIZE);
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 			/*
5187c478bd9Sstevel@tonic-gate 			 * transaction id is the 1st thing in the output
5197c478bd9Sstevel@tonic-gate 			 * buffer.
5207c478bd9Sstevel@tonic-gate 			 */
5217c478bd9Sstevel@tonic-gate 			/* LINTED pointer alignment */
5227c478bd9Sstevel@tonic-gate 			(*(uint32_t *)(mp->b_rptr)) = p->cku_xid;
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 			/* Skip the preserialized stuff. */
5257c478bd9Sstevel@tonic-gate 			XDR_SETPOS(xdrs, CKU_HDRSIZE);
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 			/* Serialize dynamic stuff into the output buffer. */
5287c478bd9Sstevel@tonic-gate 			if ((!XDR_PUTINT32(xdrs, (int32_t *)&procnum)) ||
5297c478bd9Sstevel@tonic-gate 			    (!AUTH_MARSHALL(h->cl_auth, xdrs, p->cku_cred)) ||
5307c478bd9Sstevel@tonic-gate 			    (!(*xdr_args)(xdrs, argsp))) {
5317c478bd9Sstevel@tonic-gate 				freemsg(mp);
5327c478bd9Sstevel@tonic-gate 				p->cku_err.re_status = RPC_CANTENCODEARGS;
5337c478bd9Sstevel@tonic-gate 				p->cku_err.re_errno = EIO;
5347c478bd9Sstevel@tonic-gate 				goto done;
5357c478bd9Sstevel@tonic-gate 			}
5367c478bd9Sstevel@tonic-gate 		} else {
5377c478bd9Sstevel@tonic-gate 			uint32_t *uproc = (uint32_t *)
5387c478bd9Sstevel@tonic-gate 			    &p->cku_rpchdr[CKU_HDRSIZE];
5397c478bd9Sstevel@tonic-gate 			IXDR_PUT_U_INT32(uproc, procnum);
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 			(*(uint32_t *)(&p->cku_rpchdr[0])) = p->cku_xid;
5427c478bd9Sstevel@tonic-gate 			XDR_SETPOS(xdrs, 0);
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 			/* Serialize the procedure number and the arguments. */
5457c478bd9Sstevel@tonic-gate 			if (!AUTH_WRAP(h->cl_auth, (caddr_t)p->cku_rpchdr,
5467c478bd9Sstevel@tonic-gate 			    CKU_HDRSIZE+4, xdrs, xdr_args, argsp)) {
5477c478bd9Sstevel@tonic-gate 				freemsg(mp);
5487c478bd9Sstevel@tonic-gate 				p->cku_err.re_status = RPC_CANTENCODEARGS;
5497c478bd9Sstevel@tonic-gate 				p->cku_err.re_errno = EIO;
5507c478bd9Sstevel@tonic-gate 				goto done;
5517c478bd9Sstevel@tonic-gate 			}
5527c478bd9Sstevel@tonic-gate 		}
5537c478bd9Sstevel@tonic-gate 	} else
5547c478bd9Sstevel@tonic-gate 		mp = mpdup;
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 	mpdup = dupmsg(mp);
5577c478bd9Sstevel@tonic-gate 	if (mpdup == NULL) {
5587c478bd9Sstevel@tonic-gate 		freemsg(mp);
5597c478bd9Sstevel@tonic-gate 		p->cku_err.re_status = RPC_SYSTEMERROR;
5607c478bd9Sstevel@tonic-gate 		p->cku_err.re_errno = ENOSR;
5617c478bd9Sstevel@tonic-gate 		goto done;
5627c478bd9Sstevel@tonic-gate 	}
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 	/*
5657c478bd9Sstevel@tonic-gate 	 * Grab an endpnt only if the endpoint is NULL.  We could be retrying
5667c478bd9Sstevel@tonic-gate 	 * the request and in this case we want to go through the same
5677c478bd9Sstevel@tonic-gate 	 * source port, so that the duplicate request cache may detect a
5687c478bd9Sstevel@tonic-gate 	 * retry.
5697c478bd9Sstevel@tonic-gate 	 */
57078598ee3Snd150628 
5717c478bd9Sstevel@tonic-gate 	if (p->cku_endpnt == NULL)
57278598ee3Snd150628 		p->cku_endpnt = endpnt_get(&p->cku_config, p->cku_useresvport);
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 	if (p->cku_endpnt == NULL) {
5757c478bd9Sstevel@tonic-gate 		freemsg(mp);
5767c478bd9Sstevel@tonic-gate 		p->cku_err.re_status = RPC_SYSTEMERROR;
5777c478bd9Sstevel@tonic-gate 		p->cku_err.re_errno = ENOSR;
5787c478bd9Sstevel@tonic-gate 		goto done;
5797c478bd9Sstevel@tonic-gate 	}
5807c478bd9Sstevel@tonic-gate 
581d3d50737SRafael Vanoni 	round_trip = ddi_get_lbolt();
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 	error = clnt_clts_dispatch_send(p->cku_endpnt->e_wq, mp,
584de8c4a14SErik Nordmark 	    &p->cku_addr, call, p->cku_xid, p->cku_cred);
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	if (error != 0) {
5877c478bd9Sstevel@tonic-gate 		freemsg(mp);
5887c478bd9Sstevel@tonic-gate 		p->cku_err.re_status = RPC_CANTSEND;
5897c478bd9Sstevel@tonic-gate 		p->cku_err.re_errno = error;
5907c478bd9Sstevel@tonic-gate 		RCSTAT_INCR(p->cku_stats, rccantsend);
5917c478bd9Sstevel@tonic-gate 		goto done1;
5927c478bd9Sstevel@tonic-gate 	}
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 	RPCLOG(64, "clnt_clts_kcallit_addr: sent call for xid 0x%x\n",
5957c478bd9Sstevel@tonic-gate 	    p->cku_xid);
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 	/*
5987c478bd9Sstevel@tonic-gate 	 * There are two reasons for which we go back to to tryread.
5997c478bd9Sstevel@tonic-gate 	 *
6007c478bd9Sstevel@tonic-gate 	 * a) In case the status is RPC_PROCUNAVAIL and we sent out a
6017c478bd9Sstevel@tonic-gate 	 *    broadcast we should not get any invalid messages with the
6027c478bd9Sstevel@tonic-gate 	 *    RPC_PROCUNAVAIL error back. Some broken RPC implementations
6037c478bd9Sstevel@tonic-gate 	 *    send them and for this we have to ignore them ( as we would
6047c478bd9Sstevel@tonic-gate 	 *    have never received them ) and look for another message
6057c478bd9Sstevel@tonic-gate 	 *    which might contain the valid response because we don't know
6067c478bd9Sstevel@tonic-gate 	 *    how many broken implementations are in the network. So we are
6077c478bd9Sstevel@tonic-gate 	 *    going to loop until
6087c478bd9Sstevel@tonic-gate 	 *    - we received a valid response
6097c478bd9Sstevel@tonic-gate 	 *    - we have processed all invalid responses and
6107c478bd9Sstevel@tonic-gate 	 *	got a time out when we try to receive again a
6117c478bd9Sstevel@tonic-gate 	 *	message.
6127c478bd9Sstevel@tonic-gate 	 *
6137c478bd9Sstevel@tonic-gate 	 * b) We will jump back to tryread also in case we failed
6147c478bd9Sstevel@tonic-gate 	 *    within the AUTH_VALIDATE. In this case we should move
6157c478bd9Sstevel@tonic-gate 	 *    on and loop until we received a valid response or we
6167c478bd9Sstevel@tonic-gate 	 *    have processed all responses with broken authentication
6177c478bd9Sstevel@tonic-gate 	 *    and we got a time out when we try to receive a message.
6187c478bd9Sstevel@tonic-gate 	 */
6197c478bd9Sstevel@tonic-gate tryread:
6207c478bd9Sstevel@tonic-gate 	mutex_enter(&call->call_lock);
6217c478bd9Sstevel@tonic-gate 	interrupted = FALSE;
6227c478bd9Sstevel@tonic-gate 	if (call->call_notified == FALSE) {
6237c478bd9Sstevel@tonic-gate 		klwp_t *lwp = ttolwp(curthread);
6247c478bd9Sstevel@tonic-gate 		clock_t cv_wait_ret = 1; /* init to > 0 */
6257c478bd9Sstevel@tonic-gate 		clock_t cv_timout = timout;
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 		if (lwp != NULL)
6287c478bd9Sstevel@tonic-gate 			lwp->lwp_nostop++;
6297c478bd9Sstevel@tonic-gate 
630d3d50737SRafael Vanoni 		cv_timout += ddi_get_lbolt();
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate 		if (h->cl_nosignal)
6337c478bd9Sstevel@tonic-gate 			while ((cv_wait_ret =
6347c478bd9Sstevel@tonic-gate 			    cv_timedwait(&call->call_cv,
6357c478bd9Sstevel@tonic-gate 			    &call->call_lock, cv_timout)) > 0 &&
6368ffff9fdSgt29601 			    call->call_notified == FALSE)
6378ffff9fdSgt29601 				;
6387c478bd9Sstevel@tonic-gate 		else
6397c478bd9Sstevel@tonic-gate 			while ((cv_wait_ret =
6407c478bd9Sstevel@tonic-gate 			    cv_timedwait_sig(&call->call_cv,
6417c478bd9Sstevel@tonic-gate 			    &call->call_lock, cv_timout)) > 0 &&
6428ffff9fdSgt29601 			    call->call_notified == FALSE)
6438ffff9fdSgt29601 				;
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 		if (cv_wait_ret == 0)
6467c478bd9Sstevel@tonic-gate 			interrupted = TRUE;
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 		if (lwp != NULL)
6497c478bd9Sstevel@tonic-gate 			lwp->lwp_nostop--;
6507c478bd9Sstevel@tonic-gate 	}
6517c478bd9Sstevel@tonic-gate 	resp = call->call_reply;
6527c478bd9Sstevel@tonic-gate 	call->call_reply = NULL;
6537c478bd9Sstevel@tonic-gate 	status = call->call_status;
6547c478bd9Sstevel@tonic-gate 	/*
6557c478bd9Sstevel@tonic-gate 	 * We have to reset the call_notified here. In case we have
6567c478bd9Sstevel@tonic-gate 	 * to do a retry ( e.g. in case we got a RPC_PROCUNAVAIL
6577c478bd9Sstevel@tonic-gate 	 * error ) we need to set this to false to ensure that
6587c478bd9Sstevel@tonic-gate 	 * we will wait for the next message. When the next message
6597c478bd9Sstevel@tonic-gate 	 * is going to arrive the function clnt_clts_dispatch_notify
6607c478bd9Sstevel@tonic-gate 	 * will set this to true again.
6617c478bd9Sstevel@tonic-gate 	 */
6627c478bd9Sstevel@tonic-gate 	call->call_notified = FALSE;
66328a15eaaSMarcel Telka 	call->call_status = RPC_TIMEDOUT;
6647c478bd9Sstevel@tonic-gate 	mutex_exit(&call->call_lock);
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 	if (status == RPC_TIMEDOUT) {
6677c478bd9Sstevel@tonic-gate 		if (interrupted) {
6687c478bd9Sstevel@tonic-gate 			/*
6697c478bd9Sstevel@tonic-gate 			 * We got interrupted, bail out
6707c478bd9Sstevel@tonic-gate 			 */
6717c478bd9Sstevel@tonic-gate 			p->cku_err.re_status = RPC_INTR;
6727c478bd9Sstevel@tonic-gate 			p->cku_err.re_errno = EINTR;
6737c478bd9Sstevel@tonic-gate 			goto done1;
6747c478bd9Sstevel@tonic-gate 		} else {
6757c478bd9Sstevel@tonic-gate 			RPCLOG(8, "clnt_clts_kcallit_addr: "
6767c478bd9Sstevel@tonic-gate 			    "request w/xid 0x%x timedout "
6777c478bd9Sstevel@tonic-gate 			    "waiting for reply\n", p->cku_xid);
6787c478bd9Sstevel@tonic-gate #if 0 /* XXX not yet */
6797c478bd9Sstevel@tonic-gate 			/*
6807c478bd9Sstevel@tonic-gate 			 * Timeout may be due to a dead gateway. Send
6817c478bd9Sstevel@tonic-gate 			 * an ioctl downstream advising deletion of
6827c478bd9Sstevel@tonic-gate 			 * route when we reach the half-way point to
6837c478bd9Sstevel@tonic-gate 			 * timing out.
6847c478bd9Sstevel@tonic-gate 			 */
6857c478bd9Sstevel@tonic-gate 			if (stries == p->cku_retrys/2) {
6867c478bd9Sstevel@tonic-gate 				t_kadvise(p->cku_endpnt->e_tiptr,
6877c478bd9Sstevel@tonic-gate 				    (uchar_t *)p->cku_addr.buf,
6887c478bd9Sstevel@tonic-gate 				    p->cku_addr.len);
6897c478bd9Sstevel@tonic-gate 			}
6907c478bd9Sstevel@tonic-gate #endif /* not yet */
6917c478bd9Sstevel@tonic-gate 			p->cku_err.re_status = RPC_TIMEDOUT;
6927c478bd9Sstevel@tonic-gate 			p->cku_err.re_errno = ETIMEDOUT;
6937c478bd9Sstevel@tonic-gate 			RCSTAT_INCR(p->cku_stats, rctimeouts);
6947c478bd9Sstevel@tonic-gate 			goto done1;
6957c478bd9Sstevel@tonic-gate 		}
6967c478bd9Sstevel@tonic-gate 	}
6977c478bd9Sstevel@tonic-gate 
69828a15eaaSMarcel Telka 	ASSERT(resp != NULL);
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 	/*
7017c478bd9Sstevel@tonic-gate 	 * Prepare the message for further processing.  We need to remove
7027c478bd9Sstevel@tonic-gate 	 * the datagram header and copy the source address if necessary.  No
7037c478bd9Sstevel@tonic-gate 	 * need to verify the header since rpcmod took care of that.
7047c478bd9Sstevel@tonic-gate 	 */
7057c478bd9Sstevel@tonic-gate 	/*
7067c478bd9Sstevel@tonic-gate 	 * Copy the source address if the caller has supplied a netbuf.
7077c478bd9Sstevel@tonic-gate 	 */
7087c478bd9Sstevel@tonic-gate 	if (sin != NULL) {
7097c478bd9Sstevel@tonic-gate 		union T_primitives *pptr;
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate 		pptr = (union T_primitives *)resp->b_rptr;
7127c478bd9Sstevel@tonic-gate 		bcopy(resp->b_rptr + pptr->unitdata_ind.SRC_offset, sin->buf,
7137c478bd9Sstevel@tonic-gate 		    pptr->unitdata_ind.SRC_length);
7147c478bd9Sstevel@tonic-gate 		sin->len = pptr->unitdata_ind.SRC_length;
7157c478bd9Sstevel@tonic-gate 	}
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate 	/*
7187c478bd9Sstevel@tonic-gate 	 * Pop off the datagram header.
71928a15eaaSMarcel Telka 	 * It was retained in rpcmodrput().
7207c478bd9Sstevel@tonic-gate 	 */
7217c478bd9Sstevel@tonic-gate 	tmp = resp;
7227c478bd9Sstevel@tonic-gate 	resp = resp->b_cont;
7237c478bd9Sstevel@tonic-gate 	tmp->b_cont = NULL;
7247c478bd9Sstevel@tonic-gate 	freeb(tmp);
7257c478bd9Sstevel@tonic-gate 
726d3d50737SRafael Vanoni 	round_trip = ddi_get_lbolt() - round_trip;
7277c478bd9Sstevel@tonic-gate 	/*
7287c478bd9Sstevel@tonic-gate 	 * Van Jacobson timer algorithm here, only if NOT a retransmission.
7297c478bd9Sstevel@tonic-gate 	 */
7307c478bd9Sstevel@tonic-gate 	if (p->cku_timers != NULL && stries == p->cku_retrys) {
7317c478bd9Sstevel@tonic-gate 		int rt;
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 		rt = round_trip;
7347c478bd9Sstevel@tonic-gate 		rt -= (p->cku_timers->rt_srtt >> 3);
7357c478bd9Sstevel@tonic-gate 		p->cku_timers->rt_srtt += rt;
7367c478bd9Sstevel@tonic-gate 		if (rt < 0)
7377c478bd9Sstevel@tonic-gate 			rt = - rt;
7387c478bd9Sstevel@tonic-gate 		rt -= (p->cku_timers->rt_deviate >> 2);
7397c478bd9Sstevel@tonic-gate 		p->cku_timers->rt_deviate += rt;
7407c478bd9Sstevel@tonic-gate 		p->cku_timers->rt_rtxcur =
7417c478bd9Sstevel@tonic-gate 		    (clock_t)((p->cku_timers->rt_srtt >> 2) +
7427c478bd9Sstevel@tonic-gate 		    p->cku_timers->rt_deviate) >> 1;
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 		rt = round_trip;
7457c478bd9Sstevel@tonic-gate 		rt -= (p->cku_timeall->rt_srtt >> 3);
7467c478bd9Sstevel@tonic-gate 		p->cku_timeall->rt_srtt += rt;
7477c478bd9Sstevel@tonic-gate 		if (rt < 0)
7487c478bd9Sstevel@tonic-gate 			rt = - rt;
7497c478bd9Sstevel@tonic-gate 		rt -= (p->cku_timeall->rt_deviate >> 2);
7507c478bd9Sstevel@tonic-gate 		p->cku_timeall->rt_deviate += rt;
7517c478bd9Sstevel@tonic-gate 		p->cku_timeall->rt_rtxcur =
7527c478bd9Sstevel@tonic-gate 		    (clock_t)((p->cku_timeall->rt_srtt >> 2) +
7537c478bd9Sstevel@tonic-gate 		    p->cku_timeall->rt_deviate) >> 1;
7547c478bd9Sstevel@tonic-gate 		if (p->cku_feedback != NULL) {
7557c478bd9Sstevel@tonic-gate 			(*p->cku_feedback)(FEEDBACK_OK, procnum,
7567c478bd9Sstevel@tonic-gate 			    p->cku_feedarg);
7577c478bd9Sstevel@tonic-gate 		}
7587c478bd9Sstevel@tonic-gate 	}
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 	/*
7617c478bd9Sstevel@tonic-gate 	 * Process reply
7627c478bd9Sstevel@tonic-gate 	 */
7637c478bd9Sstevel@tonic-gate 	xdrs = &(p->cku_inxdr);
7647c478bd9Sstevel@tonic-gate 	xdrmblk_init(xdrs, resp, XDR_DECODE, 0);
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 	reply_msg.rm_direction = REPLY;
7677c478bd9Sstevel@tonic-gate 	reply_msg.rm_reply.rp_stat = MSG_ACCEPTED;
7687c478bd9Sstevel@tonic-gate 	reply_msg.acpted_rply.ar_stat = SUCCESS;
7697c478bd9Sstevel@tonic-gate 	reply_msg.acpted_rply.ar_verf = _null_auth;
7707c478bd9Sstevel@tonic-gate 	/*
7717c478bd9Sstevel@tonic-gate 	 *  xdr_results will be done in AUTH_UNWRAP.
7727c478bd9Sstevel@tonic-gate 	 */
7737c478bd9Sstevel@tonic-gate 	reply_msg.acpted_rply.ar_results.where = NULL;
7747c478bd9Sstevel@tonic-gate 	reply_msg.acpted_rply.ar_results.proc = xdr_void;
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 	/*
7777c478bd9Sstevel@tonic-gate 	 * Decode and validate the response.
7787c478bd9Sstevel@tonic-gate 	 */
7797c478bd9Sstevel@tonic-gate 	if (!xdr_replymsg(xdrs, &reply_msg)) {
7807c478bd9Sstevel@tonic-gate 		p->cku_err.re_status = RPC_CANTDECODERES;
7817c478bd9Sstevel@tonic-gate 		p->cku_err.re_errno = EIO;
7827c478bd9Sstevel@tonic-gate 		(void) xdr_rpc_free_verifier(xdrs, &reply_msg);
7837c478bd9Sstevel@tonic-gate 		goto done1;
7847c478bd9Sstevel@tonic-gate 	}
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate 	_seterr_reply(&reply_msg, &(p->cku_err));
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate 	re_status = p->cku_err.re_status;
7897c478bd9Sstevel@tonic-gate 	if (re_status == RPC_SUCCESS) {
7907c478bd9Sstevel@tonic-gate 		/*
7917c478bd9Sstevel@tonic-gate 		 * Reply is good, check auth.
7927c478bd9Sstevel@tonic-gate 		 */
7937c478bd9Sstevel@tonic-gate 		if (!AUTH_VALIDATE(h->cl_auth,
7947c478bd9Sstevel@tonic-gate 		    &reply_msg.acpted_rply.ar_verf)) {
7957c478bd9Sstevel@tonic-gate 			p->cku_err.re_status = RPC_AUTHERROR;
7967c478bd9Sstevel@tonic-gate 			p->cku_err.re_why = AUTH_INVALIDRESP;
7977c478bd9Sstevel@tonic-gate 			RCSTAT_INCR(p->cku_stats, rcbadverfs);
7987c478bd9Sstevel@tonic-gate 			(void) xdr_rpc_free_verifier(xdrs, &reply_msg);
7997c478bd9Sstevel@tonic-gate 			goto tryread;
8007c478bd9Sstevel@tonic-gate 		}
8017c478bd9Sstevel@tonic-gate 		if (!AUTH_UNWRAP(h->cl_auth, xdrs, xdr_results, resultsp)) {
8027c478bd9Sstevel@tonic-gate 			p->cku_err.re_status = RPC_CANTDECODERES;
8037c478bd9Sstevel@tonic-gate 			p->cku_err.re_errno = EIO;
8047c478bd9Sstevel@tonic-gate 		}
8057c478bd9Sstevel@tonic-gate 		(void) xdr_rpc_free_verifier(xdrs, &reply_msg);
8067c478bd9Sstevel@tonic-gate 		goto done1;
8077c478bd9Sstevel@tonic-gate 	}
8087c478bd9Sstevel@tonic-gate 	/* set errno in case we can't recover */
8097c478bd9Sstevel@tonic-gate 	if (re_status != RPC_VERSMISMATCH &&
8108ffff9fdSgt29601 	    re_status != RPC_AUTHERROR && re_status != RPC_PROGVERSMISMATCH)
8117c478bd9Sstevel@tonic-gate 		p->cku_err.re_errno = EIO;
8127c478bd9Sstevel@tonic-gate 	/*
8137c478bd9Sstevel@tonic-gate 	 * Determine whether or not we're doing an RPC
8147c478bd9Sstevel@tonic-gate 	 * broadcast. Some server implementations don't
8157c478bd9Sstevel@tonic-gate 	 * follow RFC 1050, section 7.4.2 in that they
8167c478bd9Sstevel@tonic-gate 	 * don't remain silent when they see a proc
8177c478bd9Sstevel@tonic-gate 	 * they don't support. Therefore we keep trying
8187c478bd9Sstevel@tonic-gate 	 * to receive on RPC_PROCUNAVAIL, hoping to get
8197c478bd9Sstevel@tonic-gate 	 * a valid response from a compliant server.
8207c478bd9Sstevel@tonic-gate 	 */
8217c478bd9Sstevel@tonic-gate 	if (re_status == RPC_PROCUNAVAIL && p->cku_bcast) {
8227c478bd9Sstevel@tonic-gate 		(void) xdr_rpc_free_verifier(xdrs, &reply_msg);
8237c478bd9Sstevel@tonic-gate 		goto tryread;
8247c478bd9Sstevel@tonic-gate 	}
8257c478bd9Sstevel@tonic-gate 	if (re_status == RPC_AUTHERROR) {
82628a15eaaSMarcel Telka 
82728a15eaaSMarcel Telka 		(void) xdr_rpc_free_verifier(xdrs, &reply_msg);
82828a15eaaSMarcel Telka 		call_table_remove(call);
82928a15eaaSMarcel Telka 		if (call->call_reply != NULL) {
83028a15eaaSMarcel Telka 			freemsg(call->call_reply);
83128a15eaaSMarcel Telka 			call->call_reply = NULL;
83228a15eaaSMarcel Telka 		}
83328a15eaaSMarcel Telka 
8347c478bd9Sstevel@tonic-gate 		/*
8357c478bd9Sstevel@tonic-gate 		 * Maybe our credential need to be refreshed
8367c478bd9Sstevel@tonic-gate 		 */
8377c478bd9Sstevel@tonic-gate 		if (refreshes > 0 &&
8387c478bd9Sstevel@tonic-gate 		    AUTH_REFRESH(h->cl_auth, &reply_msg, p->cku_cred)) {
8397c478bd9Sstevel@tonic-gate 			/*
8407c478bd9Sstevel@tonic-gate 			 * The credential is refreshed. Try the request again.
8417c478bd9Sstevel@tonic-gate 			 * Even if stries == 0, we still retry as long as
8427c478bd9Sstevel@tonic-gate 			 * refreshes > 0. This prevents a soft authentication
8437c478bd9Sstevel@tonic-gate 			 * error turning into a hard one at an upper level.
8447c478bd9Sstevel@tonic-gate 			 */
8457c478bd9Sstevel@tonic-gate 			refreshes--;
8467c478bd9Sstevel@tonic-gate 			RCSTAT_INCR(p->cku_stats, rcbadcalls);
8477c478bd9Sstevel@tonic-gate 			RCSTAT_INCR(p->cku_stats, rcnewcreds);
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate 			freemsg(mpdup);
8507c478bd9Sstevel@tonic-gate 			mpdup = NULL;
85128a15eaaSMarcel Telka 			freemsg(resp);
85228a15eaaSMarcel Telka 			resp = NULL;
8537c478bd9Sstevel@tonic-gate 			goto call_again;
8547c478bd9Sstevel@tonic-gate 		}
8557c478bd9Sstevel@tonic-gate 		/*
8567c478bd9Sstevel@tonic-gate 		 * We have used the client handle to do an AUTH_REFRESH
8577c478bd9Sstevel@tonic-gate 		 * and the RPC status may be set to RPC_SUCCESS;
8587c478bd9Sstevel@tonic-gate 		 * Let's make sure to set it to RPC_AUTHERROR.
8597c478bd9Sstevel@tonic-gate 		 */
8607c478bd9Sstevel@tonic-gate 		p->cku_err.re_status = RPC_CANTDECODERES;
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate 		/*
8637c478bd9Sstevel@tonic-gate 		 * Map recoverable and unrecoverable
8647c478bd9Sstevel@tonic-gate 		 * authentication errors to appropriate errno
8657c478bd9Sstevel@tonic-gate 		 */
8667c478bd9Sstevel@tonic-gate 		switch (p->cku_err.re_why) {
86778598ee3Snd150628 		case AUTH_TOOWEAK:
86878598ee3Snd150628 			/*
86978598ee3Snd150628 			 * Could be an nfsportmon failure, set
87078598ee3Snd150628 			 * useresvport and try again.
87178598ee3Snd150628 			 */
87278598ee3Snd150628 			if (p->cku_useresvport != 1) {
87378598ee3Snd150628 				p->cku_useresvport = 1;
87428a15eaaSMarcel Telka 
87578598ee3Snd150628 				freemsg(mpdup);
87678598ee3Snd150628 				mpdup = NULL;
87728a15eaaSMarcel Telka 				freemsg(resp);
87828a15eaaSMarcel Telka 				resp = NULL;
87928a15eaaSMarcel Telka 
88078598ee3Snd150628 				endpt = p->cku_endpnt;
88178598ee3Snd150628 				if (endpt->e_tiptr != NULL) {
88278598ee3Snd150628 					mutex_enter(&endpt->e_lock);
88378598ee3Snd150628 					endpt->e_flags &= ~ENDPNT_BOUND;
88478598ee3Snd150628 					(void) t_kclose(endpt->e_tiptr, 1);
88578598ee3Snd150628 					endpt->e_tiptr = NULL;
88678598ee3Snd150628 					mutex_exit(&endpt->e_lock);
88778598ee3Snd150628 
88878598ee3Snd150628 				}
88978598ee3Snd150628 
89078598ee3Snd150628 				p->cku_xid = alloc_xid();
89178598ee3Snd150628 				endpnt_rele(p->cku_endpnt);
89278598ee3Snd150628 				p->cku_endpnt = NULL;
89378598ee3Snd150628 				goto call_again;
89478598ee3Snd150628 			}
89578598ee3Snd150628 			/* FALLTHRU */
8967c478bd9Sstevel@tonic-gate 		case AUTH_BADCRED:
8977c478bd9Sstevel@tonic-gate 		case AUTH_BADVERF:
8987c478bd9Sstevel@tonic-gate 		case AUTH_INVALIDRESP:
8997c478bd9Sstevel@tonic-gate 		case AUTH_FAILED:
9007c478bd9Sstevel@tonic-gate 		case RPCSEC_GSS_NOCRED:
9017c478bd9Sstevel@tonic-gate 		case RPCSEC_GSS_FAILED:
9027c478bd9Sstevel@tonic-gate 			p->cku_err.re_errno = EACCES;
9037c478bd9Sstevel@tonic-gate 			break;
9047c478bd9Sstevel@tonic-gate 		case AUTH_REJECTEDCRED:
9057c478bd9Sstevel@tonic-gate 		case AUTH_REJECTEDVERF:
9067c478bd9Sstevel@tonic-gate 		default:
9077c478bd9Sstevel@tonic-gate 			p->cku_err.re_errno = EIO;
9087c478bd9Sstevel@tonic-gate 			break;
9097c478bd9Sstevel@tonic-gate 		}
9107c478bd9Sstevel@tonic-gate 		RPCLOG(1, "clnt_clts_kcallit : authentication failed "
9117c478bd9Sstevel@tonic-gate 		    "with RPC_AUTHERROR of type %d\n",
9127c478bd9Sstevel@tonic-gate 		    p->cku_err.re_why);
91328a15eaaSMarcel Telka 		goto done;
9147c478bd9Sstevel@tonic-gate 	}
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate 	(void) xdr_rpc_free_verifier(xdrs, &reply_msg);
9177c478bd9Sstevel@tonic-gate 
9187c478bd9Sstevel@tonic-gate done1:
9197c478bd9Sstevel@tonic-gate 	call_table_remove(call);
9207c478bd9Sstevel@tonic-gate 	if (call->call_reply != NULL) {
9217c478bd9Sstevel@tonic-gate 		freemsg(call->call_reply);
9227c478bd9Sstevel@tonic-gate 		call->call_reply = NULL;
9237c478bd9Sstevel@tonic-gate 	}
9247c478bd9Sstevel@tonic-gate 	RPCLOG(64, "clnt_clts_kcallit_addr: xid 0x%x taken off dispatch list",
9257c478bd9Sstevel@tonic-gate 	    p->cku_xid);
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate done:
9287c478bd9Sstevel@tonic-gate 	if (resp != NULL) {
9297c478bd9Sstevel@tonic-gate 		freemsg(resp);
9307c478bd9Sstevel@tonic-gate 		resp = NULL;
9317c478bd9Sstevel@tonic-gate 	}
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate 	if ((p->cku_err.re_status != RPC_SUCCESS) &&
9347c478bd9Sstevel@tonic-gate 	    (p->cku_err.re_status != RPC_INTR) &&
9357c478bd9Sstevel@tonic-gate 	    (p->cku_err.re_status != RPC_UDERROR) &&
9367c478bd9Sstevel@tonic-gate 	    !IS_UNRECOVERABLE_RPC(p->cku_err.re_status)) {
9377c478bd9Sstevel@tonic-gate 		if (p->cku_feedback != NULL && stries == p->cku_retrys) {
9387c478bd9Sstevel@tonic-gate 			(*p->cku_feedback)(FEEDBACK_REXMIT1, procnum,
9397c478bd9Sstevel@tonic-gate 			    p->cku_feedarg);
9407c478bd9Sstevel@tonic-gate 		}
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate 		timout = backoff(timout);
9437c478bd9Sstevel@tonic-gate 		if (p->cku_timeall != (struct rpc_timers *)0)
9447c478bd9Sstevel@tonic-gate 			p->cku_timeall->rt_rtxcur = timout;
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate 		if (p->cku_err.re_status == RPC_SYSTEMERROR ||
9477c478bd9Sstevel@tonic-gate 		    p->cku_err.re_status == RPC_CANTSEND) {
9487c478bd9Sstevel@tonic-gate 			/*
9497c478bd9Sstevel@tonic-gate 			 * Errors due to lack of resources, wait a bit
9507c478bd9Sstevel@tonic-gate 			 * and try again.
9517c478bd9Sstevel@tonic-gate 			 */
9527c478bd9Sstevel@tonic-gate 			(void) delay(hz/10);
9537c478bd9Sstevel@tonic-gate 		}
9547c478bd9Sstevel@tonic-gate 		if (stries-- > 0) {
9557c478bd9Sstevel@tonic-gate 			RCSTAT_INCR(p->cku_stats, rcretrans);
9567c478bd9Sstevel@tonic-gate 			goto call_again;
9577c478bd9Sstevel@tonic-gate 		}
9587c478bd9Sstevel@tonic-gate 	}
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 	if (mpdup != NULL)
9617c478bd9Sstevel@tonic-gate 		freemsg(mpdup);
9627c478bd9Sstevel@tonic-gate 
9637c478bd9Sstevel@tonic-gate 	if (p->cku_err.re_status != RPC_SUCCESS) {
9647c478bd9Sstevel@tonic-gate 		RCSTAT_INCR(p->cku_stats, rcbadcalls);
9657c478bd9Sstevel@tonic-gate 	}
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate 	/*
9687c478bd9Sstevel@tonic-gate 	 * Allow the endpoint to be held by the client handle in case this
9697c478bd9Sstevel@tonic-gate 	 * RPC was not successful.  A retry may occur at a higher level and
9707c478bd9Sstevel@tonic-gate 	 * in this case we may want to send the request over the same
9717c478bd9Sstevel@tonic-gate 	 * source port.
97251e44b2bSDai Ngo 	 * Endpoint is also released for one-way RPC: no reply, nor retransmit
97351e44b2bSDai Ngo 	 * is expected.
9747c478bd9Sstevel@tonic-gate 	 */
97551e44b2bSDai Ngo 	if ((p->cku_err.re_status == RPC_SUCCESS ||
97651e44b2bSDai Ngo 	    (p->cku_err.re_status == RPC_TIMEDOUT && ori_timout == 0)) &&
97751e44b2bSDai Ngo 	    p->cku_endpnt != NULL) {
9787c478bd9Sstevel@tonic-gate 		endpnt_rele(p->cku_endpnt);
9797c478bd9Sstevel@tonic-gate 		p->cku_endpnt = NULL;
98051e44b2bSDai Ngo 	} else {
98151e44b2bSDai Ngo 		DTRACE_PROBE2(clnt_clts_kcallit_done, int, p->cku_err.re_status,
98251e44b2bSDai Ngo 		    struct endpnt *, p->cku_endpnt);
9837c478bd9Sstevel@tonic-gate 	}
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate 	return (p->cku_err.re_status);
9867c478bd9Sstevel@tonic-gate }
9877c478bd9Sstevel@tonic-gate 
9887c478bd9Sstevel@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)9897c478bd9Sstevel@tonic-gate clnt_clts_kcallit(CLIENT *h, rpcproc_t procnum, xdrproc_t xdr_args,
9907c478bd9Sstevel@tonic-gate 	caddr_t argsp, xdrproc_t xdr_results, caddr_t resultsp,
9917c478bd9Sstevel@tonic-gate 	struct timeval wait)
9927c478bd9Sstevel@tonic-gate {
9937c478bd9Sstevel@tonic-gate 	return (clnt_clts_kcallit_addr(h, procnum, xdr_args, argsp,
9947c478bd9Sstevel@tonic-gate 	    xdr_results, resultsp, wait, NULL));
9957c478bd9Sstevel@tonic-gate }
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate /*
9987c478bd9Sstevel@tonic-gate  * Return error info on this handle.
9997c478bd9Sstevel@tonic-gate  */
10007c478bd9Sstevel@tonic-gate static void
clnt_clts_kerror(CLIENT * h,struct rpc_err * err)10017c478bd9Sstevel@tonic-gate clnt_clts_kerror(CLIENT *h, struct rpc_err *err)
10027c478bd9Sstevel@tonic-gate {
10037c478bd9Sstevel@tonic-gate 	/* LINTED pointer alignment */
10047c478bd9Sstevel@tonic-gate 	struct cku_private *p = htop(h);
10057c478bd9Sstevel@tonic-gate 
10067c478bd9Sstevel@tonic-gate 	*err = p->cku_err;
10077c478bd9Sstevel@tonic-gate }
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate static bool_t
clnt_clts_kfreeres(CLIENT * h,xdrproc_t xdr_res,caddr_t res_ptr)10107c478bd9Sstevel@tonic-gate clnt_clts_kfreeres(CLIENT *h, xdrproc_t xdr_res, caddr_t res_ptr)
10117c478bd9Sstevel@tonic-gate {
10127c478bd9Sstevel@tonic-gate 	/* LINTED pointer alignment */
10137c478bd9Sstevel@tonic-gate 	struct cku_private *p = htop(h);
10147c478bd9Sstevel@tonic-gate 	XDR *xdrs;
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate 	xdrs = &(p->cku_outxdr);
10177c478bd9Sstevel@tonic-gate 	xdrs->x_op = XDR_FREE;
10187c478bd9Sstevel@tonic-gate 	return ((*xdr_res)(xdrs, res_ptr));
10197c478bd9Sstevel@tonic-gate }
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate /*ARGSUSED*/
10227c478bd9Sstevel@tonic-gate static void
clnt_clts_kabort(CLIENT * h)10237c478bd9Sstevel@tonic-gate clnt_clts_kabort(CLIENT *h)
10247c478bd9Sstevel@tonic-gate {
10257c478bd9Sstevel@tonic-gate }
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate static bool_t
clnt_clts_kcontrol(CLIENT * h,int cmd,char * arg)10287c478bd9Sstevel@tonic-gate clnt_clts_kcontrol(CLIENT *h, int cmd, char *arg)
10297c478bd9Sstevel@tonic-gate {
10307c478bd9Sstevel@tonic-gate 	/* LINTED pointer alignment */
10317c478bd9Sstevel@tonic-gate 	struct cku_private *p = htop(h);
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@tonic-gate 	switch (cmd) {
10347c478bd9Sstevel@tonic-gate 	case CLSET_XID:
10357c478bd9Sstevel@tonic-gate 		p->cku_xid = *((uint32_t *)arg);
10367c478bd9Sstevel@tonic-gate 		return (TRUE);
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate 	case CLGET_XID:
10397c478bd9Sstevel@tonic-gate 		*((uint32_t *)arg) = p->cku_xid;
10407c478bd9Sstevel@tonic-gate 		return (TRUE);
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate 	case CLSET_BCAST:
10437c478bd9Sstevel@tonic-gate 		p->cku_bcast = *((uint32_t *)arg);
10447c478bd9Sstevel@tonic-gate 		return (TRUE);
10457c478bd9Sstevel@tonic-gate 
10467c478bd9Sstevel@tonic-gate 	case CLGET_BCAST:
10477c478bd9Sstevel@tonic-gate 		*((uint32_t *)arg) = p->cku_bcast;
10487c478bd9Sstevel@tonic-gate 		return (TRUE);
104978598ee3Snd150628 	case CLSET_BINDRESVPORT:
105078598ee3Snd150628 		if (arg == NULL)
105178598ee3Snd150628 			return (FALSE);
105278598ee3Snd150628 
105378598ee3Snd150628 		if (*(int *)arg != 1 && *(int *)arg != 0)
105478598ee3Snd150628 			return (FALSE);
105578598ee3Snd150628 
105678598ee3Snd150628 		p->cku_useresvport = *(int *)arg;
105778598ee3Snd150628 
105878598ee3Snd150628 		return (TRUE);
105978598ee3Snd150628 
106078598ee3Snd150628 	case CLGET_BINDRESVPORT:
106178598ee3Snd150628 		if (arg == NULL)
106278598ee3Snd150628 			return (FALSE);
106378598ee3Snd150628 
106478598ee3Snd150628 		*(int *)arg = p->cku_useresvport;
106578598ee3Snd150628 
106678598ee3Snd150628 		return (TRUE);
10677c478bd9Sstevel@tonic-gate 
10687c478bd9Sstevel@tonic-gate 	default:
10697c478bd9Sstevel@tonic-gate 		return (FALSE);
10707c478bd9Sstevel@tonic-gate 	}
10717c478bd9Sstevel@tonic-gate }
10727c478bd9Sstevel@tonic-gate 
10737c478bd9Sstevel@tonic-gate /*
10747c478bd9Sstevel@tonic-gate  * Destroy rpc handle.
10757c478bd9Sstevel@tonic-gate  * Frees the space used for output buffer, private data, and handle
10767c478bd9Sstevel@tonic-gate  * structure, and the file pointer/TLI data on last reference.
10777c478bd9Sstevel@tonic-gate  */
10787c478bd9Sstevel@tonic-gate static void
clnt_clts_kdestroy(CLIENT * h)10797c478bd9Sstevel@tonic-gate clnt_clts_kdestroy(CLIENT *h)
10807c478bd9Sstevel@tonic-gate {
10817c478bd9Sstevel@tonic-gate 	/* LINTED pointer alignment */
10827c478bd9Sstevel@tonic-gate 	struct cku_private *p = htop(h);
10837c478bd9Sstevel@tonic-gate 	calllist_t *call = &p->cku_call;
10847c478bd9Sstevel@tonic-gate 
10857c478bd9Sstevel@tonic-gate 	int plen;
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate 	RPCLOG(8, "clnt_clts_kdestroy h: %p\n", (void *)h);
10887c478bd9Sstevel@tonic-gate 	RPCLOG(8, "clnt_clts_kdestroy h: xid=0x%x\n", p->cku_xid);
10897c478bd9Sstevel@tonic-gate 
10907c478bd9Sstevel@tonic-gate 	if (p->cku_endpnt != NULL)
10917c478bd9Sstevel@tonic-gate 		endpnt_rele(p->cku_endpnt);
10927c478bd9Sstevel@tonic-gate 
10937c478bd9Sstevel@tonic-gate 	cv_destroy(&call->call_cv);
10947c478bd9Sstevel@tonic-gate 	mutex_destroy(&call->call_lock);
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate 	plen = strlen(p->cku_config.knc_protofmly) + 1;
10977c478bd9Sstevel@tonic-gate 	kmem_free(p->cku_config.knc_protofmly, plen);
10987c478bd9Sstevel@tonic-gate 	kmem_free(p->cku_addr.buf, p->cku_addr.maxlen);
10997c478bd9Sstevel@tonic-gate 	kmem_free(p, sizeof (*p));
11007c478bd9Sstevel@tonic-gate }
11017c478bd9Sstevel@tonic-gate 
11027c478bd9Sstevel@tonic-gate /*
11037c478bd9Sstevel@tonic-gate  * The connectionless (CLTS) kRPC endpoint management subsystem.
11047c478bd9Sstevel@tonic-gate  *
11057c478bd9Sstevel@tonic-gate  * Because endpoints are potentially shared among threads making RPC calls,
11067c478bd9Sstevel@tonic-gate  * they are managed in a pool according to type (endpnt_type_t).  Each
11077c478bd9Sstevel@tonic-gate  * endpnt_type_t points to a list of usable endpoints through the e_pool
11087c478bd9Sstevel@tonic-gate  * field, which is of type list_t.  list_t is a doubly-linked list.
11097c478bd9Sstevel@tonic-gate  * The number of endpoints in the pool is stored in the e_cnt field of
11107c478bd9Sstevel@tonic-gate  * endpnt_type_t and the endpoints are reference counted using the e_ref field
11117c478bd9Sstevel@tonic-gate  * in the endpnt_t structure.
11127c478bd9Sstevel@tonic-gate  *
11137c478bd9Sstevel@tonic-gate  * As an optimization, endpoints that have no references are also linked
11147c478bd9Sstevel@tonic-gate  * to an idle list via e_ilist which is also of type list_t.  When a thread
11157c478bd9Sstevel@tonic-gate  * calls endpnt_get() to obtain a transport endpoint, the idle list is first
11167c478bd9Sstevel@tonic-gate  * consulted and if such an endpoint exists, it is removed from the idle list
11177c478bd9Sstevel@tonic-gate  * and returned to the caller.
11187c478bd9Sstevel@tonic-gate  *
11197c478bd9Sstevel@tonic-gate  * If the idle list is empty, then a check is made to see if more endpoints
11207c478bd9Sstevel@tonic-gate  * can be created.  If so, we proceed and create a new endpoint which is added
11217c478bd9Sstevel@tonic-gate  * to the pool and returned to the caller.  If we have reached the limit and
11227c478bd9Sstevel@tonic-gate  * cannot make a new endpoint then one is returned to the caller via round-
11237c478bd9Sstevel@tonic-gate  * robin policy.
11247c478bd9Sstevel@tonic-gate  *
11257c478bd9Sstevel@tonic-gate  * When an endpoint is placed on the idle list by a thread calling
11267c478bd9Sstevel@tonic-gate  * endpnt_rele(), it is timestamped and then a reaper taskq is scheduled to
11277c478bd9Sstevel@tonic-gate  * be dispatched if one hasn't already been.  When the timer fires, the
11287c478bd9Sstevel@tonic-gate  * taskq traverses the idle list and checks to see which endpoints are
11297c478bd9Sstevel@tonic-gate  * eligible to be closed.  It determines this by checking if the timestamp
11307c478bd9Sstevel@tonic-gate  * when the endpoint was released has exceeded the the threshold for how long
11317c478bd9Sstevel@tonic-gate  * it should stay alive.
11327c478bd9Sstevel@tonic-gate  *
11337c478bd9Sstevel@tonic-gate  * endpnt_t structures remain persistent until the memory reclaim callback,
11347c478bd9Sstevel@tonic-gate  * endpnt_reclaim(), is invoked.
11357c478bd9Sstevel@tonic-gate  *
11367c478bd9Sstevel@tonic-gate  * Here is an example of how the data structures would be laid out by the
11377c478bd9Sstevel@tonic-gate  * subsystem:
11387c478bd9Sstevel@tonic-gate  *
11397c478bd9Sstevel@tonic-gate  *       endpnt_type_t
11407c478bd9Sstevel@tonic-gate  *
11417c478bd9Sstevel@tonic-gate  *	 loopback		                  inet
11427c478bd9Sstevel@tonic-gate  *	 _______________	                  ______________
11437c478bd9Sstevel@tonic-gate  *	| e_next        |----------------------->| e_next       |---->>
11447c478bd9Sstevel@tonic-gate  *	| e_pool        |<---+                   | e_pool       |<----+
11457c478bd9Sstevel@tonic-gate  *	| e_ilist       |<---+--+                | e_ilist      |<----+--+
11467c478bd9Sstevel@tonic-gate  *   +->| e_pcurr       |----+--+--+	      +->| e_pcurr      |-----+--+--+
11477c478bd9Sstevel@tonic-gate  *   |	| ...           |    |  |  |	      |	 | ...	        |     |  |  |
11487c478bd9Sstevel@tonic-gate  *   |	| e_itimer (90) |    |  |  |	      |	 | e_itimer (0) |     |  |  |
11497c478bd9Sstevel@tonic-gate  *   |	| e_cnt (1)     |    |  |  |	      |	 | e_cnt (3)    |     |  |  |
11507c478bd9Sstevel@tonic-gate  *   |	+---------------+    |  |  |	      |	 +--------------+     |  |  |
11517c478bd9Sstevel@tonic-gate  *   |			     |  |  |	      |			      |  |  |
11527c478bd9Sstevel@tonic-gate  *   |   endpnt_t            |  |  |          |	                      |  |  |
11537c478bd9Sstevel@tonic-gate  *   |	 ____________        |  |  |	      |	  ____________        |  |  |
11547c478bd9Sstevel@tonic-gate  *   |	| e_node     |<------+  |  |	      |	 | e_node     |<------+  |  |
11557c478bd9Sstevel@tonic-gate  *   |	| e_idle     |<---------+  |	      |	 | e_idle     |       |  |  |
11567c478bd9Sstevel@tonic-gate  *   +--| e_type     |<------------+	      +--| e_type     |       |  |  |
11577c478bd9Sstevel@tonic-gate  *	| e_tiptr    |                        |  | e_tiptr    |       |  |  |
11587c478bd9Sstevel@tonic-gate  *      | ...	     |		              |	 | ...	      |       |  |  |
11597c478bd9Sstevel@tonic-gate  *	| e_lock     |		              |	 | e_lock     |       |  |  |
11607c478bd9Sstevel@tonic-gate  *	| ...        |		              |	 | ...	      |       |  |  |
11617c478bd9Sstevel@tonic-gate  *      | e_ref (0)  |		              |	 | e_ref (2)  |       |  |  |
11627c478bd9Sstevel@tonic-gate  *	| e_itime    |	                      |	 | e_itime    |       |  |  |
11637c478bd9Sstevel@tonic-gate  *	+------------+		              |	 +------------+       |  |  |
11647c478bd9Sstevel@tonic-gate  *					      |			      |  |  |
11657c478bd9Sstevel@tonic-gate  *					      |			      |  |  |
11667c478bd9Sstevel@tonic-gate  *					      |	  ____________        |  |  |
11677c478bd9Sstevel@tonic-gate  *					      |	 | e_node     |<------+  |  |
11687c478bd9Sstevel@tonic-gate  *					      |	 | e_idle     |<------+--+  |
11697c478bd9Sstevel@tonic-gate  *					      +--| e_type     |       |     |
11707c478bd9Sstevel@tonic-gate  *					      |	 | e_tiptr    |       |     |
11717c478bd9Sstevel@tonic-gate  *					      |	 | ...	      |       |     |
11727c478bd9Sstevel@tonic-gate  *					      |	 | e_lock     |       |     |
11737c478bd9Sstevel@tonic-gate  *					      |	 | ...	      |       |     |
11747c478bd9Sstevel@tonic-gate  *					      |	 | e_ref (0)  |       |     |
11757c478bd9Sstevel@tonic-gate  *					      |	 | e_itime    |       |     |
11767c478bd9Sstevel@tonic-gate  *					      |	 +------------+       |     |
11777c478bd9Sstevel@tonic-gate  *					      |			      |     |
11787c478bd9Sstevel@tonic-gate  *					      |			      |     |
11797c478bd9Sstevel@tonic-gate  *					      |	  ____________        |     |
11807c478bd9Sstevel@tonic-gate  *					      |	 | e_node     |<------+     |
11817c478bd9Sstevel@tonic-gate  *					      |	 | e_idle     |             |
11827c478bd9Sstevel@tonic-gate  *					      +--| e_type     |<------------+
11837c478bd9Sstevel@tonic-gate  *						 | e_tiptr    |
11847c478bd9Sstevel@tonic-gate  *						 | ...	      |
11857c478bd9Sstevel@tonic-gate  *						 | e_lock     |
11867c478bd9Sstevel@tonic-gate  *						 | ...	      |
11877c478bd9Sstevel@tonic-gate  *						 | e_ref (1)  |
11887c478bd9Sstevel@tonic-gate  *						 | e_itime    |
11897c478bd9Sstevel@tonic-gate  *						 +------------+
11907c478bd9Sstevel@tonic-gate  *
11917c478bd9Sstevel@tonic-gate  * Endpoint locking strategy:
11927c478bd9Sstevel@tonic-gate  *
11937c478bd9Sstevel@tonic-gate  * The following functions manipulate lists which hold the endpoint and the
11947c478bd9Sstevel@tonic-gate  * endpoints themselves:
11957c478bd9Sstevel@tonic-gate  *
11967c478bd9Sstevel@tonic-gate  * endpnt_get()/check_endpnt()/endpnt_rele()/endpnt_reap()/do_endpnt_reclaim()
11977c478bd9Sstevel@tonic-gate  *
11987c478bd9Sstevel@tonic-gate  * Lock description follows:
11997c478bd9Sstevel@tonic-gate  *
12007c478bd9Sstevel@tonic-gate  * endpnt_type_lock: Global reader/writer lock which protects accesses to the
12017c478bd9Sstevel@tonic-gate  *		     endpnt_type_list.
12027c478bd9Sstevel@tonic-gate  *
12037c478bd9Sstevel@tonic-gate  * e_plock: Lock defined in the endpnt_type_t.  It is intended to
12047c478bd9Sstevel@tonic-gate  *	    protect accesses to the pool of endopints (e_pool) for a given
12057c478bd9Sstevel@tonic-gate  *	    endpnt_type_t.
12067c478bd9Sstevel@tonic-gate  *
12077c478bd9Sstevel@tonic-gate  * e_ilock: Lock defined in endpnt_type_t.  It is intended to protect accesses
12087c478bd9Sstevel@tonic-gate  *	    to the idle list (e_ilist) of available endpoints for a given
12097c478bd9Sstevel@tonic-gate  *	    endpnt_type_t.  It also protects access to the e_itimer, e_async_cv,
12107c478bd9Sstevel@tonic-gate  *	    and e_async_count fields in endpnt_type_t.
12117c478bd9Sstevel@tonic-gate  *
12127c478bd9Sstevel@tonic-gate  * e_lock: Lock defined in the endpnt structure.  It is intended to protect
12137c478bd9Sstevel@tonic-gate  *	   flags, cv, and ref count.
12147c478bd9Sstevel@tonic-gate  *
12157c478bd9Sstevel@tonic-gate  * The order goes as follows so as not to induce deadlock.
12167c478bd9Sstevel@tonic-gate  *
12177c478bd9Sstevel@tonic-gate  * endpnt_type_lock -> e_plock -> e_ilock -> e_lock
12187c478bd9Sstevel@tonic-gate  *
12197c478bd9Sstevel@tonic-gate  * Interaction with Zones and shutting down:
12207c478bd9Sstevel@tonic-gate  *
12217c478bd9Sstevel@tonic-gate  * endpnt_type_ts are uniquely identified by the (e_zoneid, e_rdev, e_protofmly)
12227c478bd9Sstevel@tonic-gate  * tuple, which means that a zone may not reuse another zone's idle endpoints
12237c478bd9Sstevel@tonic-gate  * without first doing a t_kclose().
12247c478bd9Sstevel@tonic-gate  *
12257c478bd9Sstevel@tonic-gate  * A zone's endpnt_type_ts are destroyed when a zone is shut down; e_async_cv
12267c478bd9Sstevel@tonic-gate  * and e_async_count are used to keep track of the threads in endpnt_taskq
12277c478bd9Sstevel@tonic-gate  * trying to reap endpnt_ts in the endpnt_type_t.
12287c478bd9Sstevel@tonic-gate  */
12297c478bd9Sstevel@tonic-gate 
12307c478bd9Sstevel@tonic-gate /*
12317c478bd9Sstevel@tonic-gate  * Allocate and initialize an endpnt_type_t
12327c478bd9Sstevel@tonic-gate  */
12337c478bd9Sstevel@tonic-gate static struct endpnt_type *
endpnt_type_create(struct knetconfig * config)12347c478bd9Sstevel@tonic-gate endpnt_type_create(struct knetconfig *config)
12357c478bd9Sstevel@tonic-gate {
12367c478bd9Sstevel@tonic-gate 	struct endpnt_type	*etype;
12377c478bd9Sstevel@tonic-gate 
12387c478bd9Sstevel@tonic-gate 	/*
12397c478bd9Sstevel@tonic-gate 	 * Allocate a new endpoint type to hang a list of
12407c478bd9Sstevel@tonic-gate 	 * endpoints off of it.
12417c478bd9Sstevel@tonic-gate 	 */
12427c478bd9Sstevel@tonic-gate 	etype = kmem_alloc(sizeof (struct endpnt_type), KM_SLEEP);
12437c478bd9Sstevel@tonic-gate 	etype->e_next = NULL;
12447c478bd9Sstevel@tonic-gate 	etype->e_pcurr = NULL;
12457c478bd9Sstevel@tonic-gate 	etype->e_itimer = 0;
12467c478bd9Sstevel@tonic-gate 	etype->e_cnt = 0;
12477c478bd9Sstevel@tonic-gate 
12487c478bd9Sstevel@tonic-gate 	(void) strncpy(etype->e_protofmly, config->knc_protofmly, KNC_STRSIZE);
12497c478bd9Sstevel@tonic-gate 	mutex_init(&etype->e_plock, NULL, MUTEX_DEFAULT, NULL);
12507c478bd9Sstevel@tonic-gate 	mutex_init(&etype->e_ilock, NULL, MUTEX_DEFAULT, NULL);
12517c478bd9Sstevel@tonic-gate 	etype->e_rdev = config->knc_rdev;
1252108322fbScarlsonj 	etype->e_zoneid = rpc_zoneid();
12537c478bd9Sstevel@tonic-gate 	etype->e_async_count = 0;
12547c478bd9Sstevel@tonic-gate 	cv_init(&etype->e_async_cv, NULL, CV_DEFAULT, NULL);
12557c478bd9Sstevel@tonic-gate 
12567c478bd9Sstevel@tonic-gate 	list_create(&etype->e_pool, sizeof (endpnt_t),
12577c478bd9Sstevel@tonic-gate 	    offsetof(endpnt_t, e_node));
12587c478bd9Sstevel@tonic-gate 	list_create(&etype->e_ilist, sizeof (endpnt_t),
12597c478bd9Sstevel@tonic-gate 	    offsetof(endpnt_t, e_idle));
12607c478bd9Sstevel@tonic-gate 
12617c478bd9Sstevel@tonic-gate 	/*
12627c478bd9Sstevel@tonic-gate 	 * Check to see if we need to create a taskq for endpoint
12637c478bd9Sstevel@tonic-gate 	 * reaping
12647c478bd9Sstevel@tonic-gate 	 */
12657c478bd9Sstevel@tonic-gate 	mutex_enter(&endpnt_taskq_lock);
12667c478bd9Sstevel@tonic-gate 	if (taskq_created == FALSE) {
12677c478bd9Sstevel@tonic-gate 		taskq_created = TRUE;
12687c478bd9Sstevel@tonic-gate 		mutex_exit(&endpnt_taskq_lock);
12697c478bd9Sstevel@tonic-gate 		ASSERT(endpnt_taskq == NULL);
12707c478bd9Sstevel@tonic-gate 		endpnt_taskq = taskq_create("clts_endpnt_taskq", 1,
12717c478bd9Sstevel@tonic-gate 		    minclsyspri, 200, INT_MAX, 0);
12727c478bd9Sstevel@tonic-gate 	} else
12737c478bd9Sstevel@tonic-gate 		mutex_exit(&endpnt_taskq_lock);
12747c478bd9Sstevel@tonic-gate 
12757c478bd9Sstevel@tonic-gate 	return (etype);
12767c478bd9Sstevel@tonic-gate }
12777c478bd9Sstevel@tonic-gate 
12787c478bd9Sstevel@tonic-gate /*
12797c478bd9Sstevel@tonic-gate  * Free an endpnt_type_t
12807c478bd9Sstevel@tonic-gate  */
12817c478bd9Sstevel@tonic-gate static void
endpnt_type_free(struct endpnt_type * etype)12827c478bd9Sstevel@tonic-gate endpnt_type_free(struct endpnt_type *etype)
12837c478bd9Sstevel@tonic-gate {
12847c478bd9Sstevel@tonic-gate 	mutex_destroy(&etype->e_plock);
12857c478bd9Sstevel@tonic-gate 	mutex_destroy(&etype->e_ilock);
12867c478bd9Sstevel@tonic-gate 	list_destroy(&etype->e_pool);
12877c478bd9Sstevel@tonic-gate 	list_destroy(&etype->e_ilist);
12887c478bd9Sstevel@tonic-gate 	kmem_free(etype, sizeof (endpnt_type_t));
12897c478bd9Sstevel@tonic-gate }
12907c478bd9Sstevel@tonic-gate 
12917c478bd9Sstevel@tonic-gate /*
12927c478bd9Sstevel@tonic-gate  * Check the endpoint to ensure that it is suitable for use.
12937c478bd9Sstevel@tonic-gate  *
12947c478bd9Sstevel@tonic-gate  * Possible return values:
12957c478bd9Sstevel@tonic-gate  *
12967c478bd9Sstevel@tonic-gate  * return (1) - Endpoint is established, but needs to be re-opened.
12977c478bd9Sstevel@tonic-gate  * return (0) && *newp == NULL - Endpoint is established, but unusable.
12987c478bd9Sstevel@tonic-gate  * return (0) && *newp != NULL - Endpoint is established and usable.
12997c478bd9Sstevel@tonic-gate  */
13007c478bd9Sstevel@tonic-gate static int
check_endpnt(struct endpnt * endp,struct endpnt ** newp)13017c478bd9Sstevel@tonic-gate check_endpnt(struct endpnt *endp, struct endpnt **newp)
13027c478bd9Sstevel@tonic-gate {
13037c478bd9Sstevel@tonic-gate 	*newp = endp;
13047c478bd9Sstevel@tonic-gate 
13057c478bd9Sstevel@tonic-gate 	mutex_enter(&endp->e_lock);
13067c478bd9Sstevel@tonic-gate 	ASSERT(endp->e_ref >= 1);
13077c478bd9Sstevel@tonic-gate 
13087c478bd9Sstevel@tonic-gate 	/*
13097c478bd9Sstevel@tonic-gate 	 * The first condition we check for is if the endpoint has been
13107c478bd9Sstevel@tonic-gate 	 * allocated, but is unusable either because it has been closed or
13117c478bd9Sstevel@tonic-gate 	 * has been marked stale.  Only *one* thread will be allowed to
131251e44b2bSDai Ngo 	 * execute the then clause.  This is enforced because the first thread
13137c478bd9Sstevel@tonic-gate 	 * to check this condition will clear the flags, so that subsequent
13147c478bd9Sstevel@tonic-gate 	 * thread(s) checking this endpoint will move on.
13157c478bd9Sstevel@tonic-gate 	 */
13167c478bd9Sstevel@tonic-gate 	if ((endp->e_flags & ENDPNT_ESTABLISHED) &&
13177c478bd9Sstevel@tonic-gate 	    (!(endp->e_flags & ENDPNT_BOUND) ||
13187c478bd9Sstevel@tonic-gate 	    (endp->e_flags & ENDPNT_STALE))) {
13197c478bd9Sstevel@tonic-gate 		/*
13207c478bd9Sstevel@tonic-gate 		 * Clear the flags here since they will be
13217c478bd9Sstevel@tonic-gate 		 * set again by this thread.  They need to be
13227c478bd9Sstevel@tonic-gate 		 * individually cleared because we want to maintain
13237c478bd9Sstevel@tonic-gate 		 * the state for ENDPNT_ONIDLE.
13247c478bd9Sstevel@tonic-gate 		 */
13257c478bd9Sstevel@tonic-gate 		endp->e_flags &= ~(ENDPNT_ESTABLISHED |
13267c478bd9Sstevel@tonic-gate 		    ENDPNT_WAITING | ENDPNT_BOUND | ENDPNT_STALE);
13277c478bd9Sstevel@tonic-gate 		mutex_exit(&endp->e_lock);
13287c478bd9Sstevel@tonic-gate 		return (1);
13297c478bd9Sstevel@tonic-gate 	}
13307c478bd9Sstevel@tonic-gate 
13317c478bd9Sstevel@tonic-gate 	/*
13327c478bd9Sstevel@tonic-gate 	 * The second condition is meant for any thread that is waiting for
13337c478bd9Sstevel@tonic-gate 	 * an endpoint to become established.  It will cv_wait() until
13347c478bd9Sstevel@tonic-gate 	 * the condition for the endpoint has been changed to ENDPNT_BOUND or
13357c478bd9Sstevel@tonic-gate 	 * ENDPNT_STALE.
13367c478bd9Sstevel@tonic-gate 	 */
13377c478bd9Sstevel@tonic-gate 	while (!(endp->e_flags & ENDPNT_BOUND) &&
13387c478bd9Sstevel@tonic-gate 	    !(endp->e_flags & ENDPNT_STALE)) {
13397c478bd9Sstevel@tonic-gate 		endp->e_flags |= ENDPNT_WAITING;
13407c478bd9Sstevel@tonic-gate 		cv_wait(&endp->e_cv, &endp->e_lock);
13417c478bd9Sstevel@tonic-gate 	}
13427c478bd9Sstevel@tonic-gate 
13437c478bd9Sstevel@tonic-gate 	ASSERT(endp->e_flags & ENDPNT_ESTABLISHED);
13447c478bd9Sstevel@tonic-gate 
13457c478bd9Sstevel@tonic-gate 	/*
13467c478bd9Sstevel@tonic-gate 	 * The last case we check for is if the endpoint has been marked stale.
13477c478bd9Sstevel@tonic-gate 	 * If this is the case then set *newp to NULL and return, so that the
13487c478bd9Sstevel@tonic-gate 	 * caller is notified of the error and can take appropriate action.
13497c478bd9Sstevel@tonic-gate 	 */
13507c478bd9Sstevel@tonic-gate 	if (endp->e_flags & ENDPNT_STALE) {
13517c478bd9Sstevel@tonic-gate 		endp->e_ref--;
13527c478bd9Sstevel@tonic-gate 		*newp = NULL;
13537c478bd9Sstevel@tonic-gate 	}
13547c478bd9Sstevel@tonic-gate 	mutex_exit(&endp->e_lock);
13557c478bd9Sstevel@tonic-gate 	return (0);
13567c478bd9Sstevel@tonic-gate }
13577c478bd9Sstevel@tonic-gate 
13587c478bd9Sstevel@tonic-gate #ifdef DEBUG
13597c478bd9Sstevel@tonic-gate /*
13607c478bd9Sstevel@tonic-gate  * Provide a fault injection setting to test error conditions.
13617c478bd9Sstevel@tonic-gate  */
13627c478bd9Sstevel@tonic-gate static int endpnt_get_return_null = 0;
13637c478bd9Sstevel@tonic-gate #endif
13647c478bd9Sstevel@tonic-gate 
13657c478bd9Sstevel@tonic-gate /*
13667c478bd9Sstevel@tonic-gate  * Returns a handle (struct endpnt *) to an open and bound endpoint
13677c478bd9Sstevel@tonic-gate  * specified by the knetconfig passed in.  Returns NULL if no valid endpoint
13687c478bd9Sstevel@tonic-gate  * can be obtained.
13697c478bd9Sstevel@tonic-gate  */
13707c478bd9Sstevel@tonic-gate static struct endpnt *
endpnt_get(struct knetconfig * config,int useresvport)137178598ee3Snd150628 endpnt_get(struct knetconfig *config, int useresvport)
13727c478bd9Sstevel@tonic-gate {
13737c478bd9Sstevel@tonic-gate 	struct endpnt_type	*n_etype = NULL;
13747c478bd9Sstevel@tonic-gate 	struct endpnt_type	*np = NULL;
13757c478bd9Sstevel@tonic-gate 	struct endpnt		*new = NULL;
13767c478bd9Sstevel@tonic-gate 	struct endpnt		*endp = NULL;
13777c478bd9Sstevel@tonic-gate 	struct endpnt		*next = NULL;
13787c478bd9Sstevel@tonic-gate 	TIUSER			*tiptr = NULL;
13797c478bd9Sstevel@tonic-gate 	int			rtries = BINDRESVPORT_RETRIES;
13807c478bd9Sstevel@tonic-gate 	int			i = 0;
13817c478bd9Sstevel@tonic-gate 	int			error;
13827c478bd9Sstevel@tonic-gate 	int			retval;
1383108322fbScarlsonj 	zoneid_t		zoneid = rpc_zoneid();
138445916cd2Sjpk 	cred_t			*cr;
13857c478bd9Sstevel@tonic-gate 
13867c478bd9Sstevel@tonic-gate 	RPCLOG(1, "endpnt_get: protofmly %s, ", config->knc_protofmly);
13877c478bd9Sstevel@tonic-gate 	RPCLOG(1, "rdev %ld\n", config->knc_rdev);
13887c478bd9Sstevel@tonic-gate 
13897c478bd9Sstevel@tonic-gate #ifdef DEBUG
13907c478bd9Sstevel@tonic-gate 	/*
13917c478bd9Sstevel@tonic-gate 	 * Inject fault if desired.  Pretend we have a stale endpoint
13927c478bd9Sstevel@tonic-gate 	 * and return NULL.
13937c478bd9Sstevel@tonic-gate 	 */
13947c478bd9Sstevel@tonic-gate 	if (endpnt_get_return_null > 0) {
13957c478bd9Sstevel@tonic-gate 		endpnt_get_return_null--;
13967c478bd9Sstevel@tonic-gate 		return (NULL);
13977c478bd9Sstevel@tonic-gate 	}
13987c478bd9Sstevel@tonic-gate #endif
13997c478bd9Sstevel@tonic-gate 	rw_enter(&endpnt_type_lock, RW_READER);
14007c478bd9Sstevel@tonic-gate 
14017c478bd9Sstevel@tonic-gate top:
14027c478bd9Sstevel@tonic-gate 	for (np = endpnt_type_list; np != NULL; np = np->e_next)
14037c478bd9Sstevel@tonic-gate 		if ((np->e_zoneid == zoneid) &&
14047c478bd9Sstevel@tonic-gate 		    (np->e_rdev == config->knc_rdev) &&
14057c478bd9Sstevel@tonic-gate 		    (strcmp(np->e_protofmly,
14067c478bd9Sstevel@tonic-gate 		    config->knc_protofmly) == 0))
14077c478bd9Sstevel@tonic-gate 			break;
14087c478bd9Sstevel@tonic-gate 
14097c478bd9Sstevel@tonic-gate 	if (np == NULL && n_etype != NULL) {
14107c478bd9Sstevel@tonic-gate 		ASSERT(rw_write_held(&endpnt_type_lock));
14117c478bd9Sstevel@tonic-gate 
14127c478bd9Sstevel@tonic-gate 		/*
14137c478bd9Sstevel@tonic-gate 		 * Link the endpoint type onto the list
14147c478bd9Sstevel@tonic-gate 		 */
14157c478bd9Sstevel@tonic-gate 		n_etype->e_next = endpnt_type_list;
14167c478bd9Sstevel@tonic-gate 		endpnt_type_list = n_etype;
14177c478bd9Sstevel@tonic-gate 		np = n_etype;
14187c478bd9Sstevel@tonic-gate 		n_etype = NULL;
14197c478bd9Sstevel@tonic-gate 	}
14207c478bd9Sstevel@tonic-gate 
14217c478bd9Sstevel@tonic-gate 	if (np == NULL) {
14227c478bd9Sstevel@tonic-gate 		/*
14237c478bd9Sstevel@tonic-gate 		 * The logic here is that we were unable to find an
14247c478bd9Sstevel@tonic-gate 		 * endpnt_type_t that matched our criteria, so we allocate a
14257c478bd9Sstevel@tonic-gate 		 * new one.  Because kmem_alloc() needs to be called with
14267c478bd9Sstevel@tonic-gate 		 * KM_SLEEP, we drop our locks so that we don't induce
14277c478bd9Sstevel@tonic-gate 		 * deadlock.  After allocating and initializing the
14287c478bd9Sstevel@tonic-gate 		 * endpnt_type_t, we reaquire the lock and go back to check
14297c478bd9Sstevel@tonic-gate 		 * if this entry needs to be added to the list.  Since we do
14307c478bd9Sstevel@tonic-gate 		 * some operations without any locking other threads may
14317c478bd9Sstevel@tonic-gate 		 * have been looking for the same endpnt_type_t and gone
14327c478bd9Sstevel@tonic-gate 		 * through this code path.  We check for this case and allow
14337c478bd9Sstevel@tonic-gate 		 * one thread to link its endpnt_type_t to the list and the
14347c478bd9Sstevel@tonic-gate 		 * other threads will simply free theirs.
14357c478bd9Sstevel@tonic-gate 		 */
14367c478bd9Sstevel@tonic-gate 		rw_exit(&endpnt_type_lock);
14377c478bd9Sstevel@tonic-gate 		n_etype = endpnt_type_create(config);
14387c478bd9Sstevel@tonic-gate 
14397c478bd9Sstevel@tonic-gate 		/*
14407c478bd9Sstevel@tonic-gate 		 * We need to reaquire the lock with RW_WRITER here so that
14417c478bd9Sstevel@tonic-gate 		 * we can safely link the new endpoint type onto the list.
14427c478bd9Sstevel@tonic-gate 		 */
14437c478bd9Sstevel@tonic-gate 		rw_enter(&endpnt_type_lock, RW_WRITER);
14447c478bd9Sstevel@tonic-gate 		goto top;
14457c478bd9Sstevel@tonic-gate 	}
14467c478bd9Sstevel@tonic-gate 
14477c478bd9Sstevel@tonic-gate 	rw_exit(&endpnt_type_lock);
14487c478bd9Sstevel@tonic-gate 	/*
14497c478bd9Sstevel@tonic-gate 	 * If n_etype is not NULL, then another thread was able to
14507c478bd9Sstevel@tonic-gate 	 * insert an endpnt_type_t of this type  onto the list before
14517c478bd9Sstevel@tonic-gate 	 * we did.  Go ahead and free ours.
14527c478bd9Sstevel@tonic-gate 	 */
14537c478bd9Sstevel@tonic-gate 	if (n_etype != NULL)
14547c478bd9Sstevel@tonic-gate 		endpnt_type_free(n_etype);
14557c478bd9Sstevel@tonic-gate 
14567c478bd9Sstevel@tonic-gate 	mutex_enter(&np->e_ilock);
14577c478bd9Sstevel@tonic-gate 	/*
14587c478bd9Sstevel@tonic-gate 	 * The algorithm to hand out endpoints is to first
14597c478bd9Sstevel@tonic-gate 	 * give out those that are idle if such endpoints
14607c478bd9Sstevel@tonic-gate 	 * exist.  Otherwise, create a new one if we haven't
14617c478bd9Sstevel@tonic-gate 	 * reached the max threshold.  Finally, we give out
14627c478bd9Sstevel@tonic-gate 	 * endpoints in a pseudo LRU fashion (round-robin).
14637c478bd9Sstevel@tonic-gate 	 *
14647c478bd9Sstevel@tonic-gate 	 * Note:  The idle list is merely a hint of those endpoints
14657c478bd9Sstevel@tonic-gate 	 * that should be idle.  There exists a window after the
14667c478bd9Sstevel@tonic-gate 	 * endpoint is released and before it is linked back onto the
14677c478bd9Sstevel@tonic-gate 	 * idle list where a thread could get a reference to it and
14687c478bd9Sstevel@tonic-gate 	 * use it.  This is okay, since the reference counts will
14697c478bd9Sstevel@tonic-gate 	 * still be consistent.
14707c478bd9Sstevel@tonic-gate 	 */
14717c478bd9Sstevel@tonic-gate 	if ((endp = (endpnt_t *)list_head(&np->e_ilist)) != NULL) {
14727c478bd9Sstevel@tonic-gate 		timeout_id_t t_id = 0;
14737c478bd9Sstevel@tonic-gate 
14747c478bd9Sstevel@tonic-gate 		mutex_enter(&endp->e_lock);
14757c478bd9Sstevel@tonic-gate 		endp->e_ref++;
14767c478bd9Sstevel@tonic-gate 		endp->e_itime = 0;
14777c478bd9Sstevel@tonic-gate 		endp->e_flags &= ~ENDPNT_ONIDLE;
14787c478bd9Sstevel@tonic-gate 		mutex_exit(&endp->e_lock);
14797c478bd9Sstevel@tonic-gate 
14807c478bd9Sstevel@tonic-gate 		/*
14817c478bd9Sstevel@tonic-gate 		 * Pop the endpoint off the idle list and hand it off
14827c478bd9Sstevel@tonic-gate 		 */
14837c478bd9Sstevel@tonic-gate 		list_remove(&np->e_ilist, endp);
14847c478bd9Sstevel@tonic-gate 
14857c478bd9Sstevel@tonic-gate 		if (np->e_itimer != 0) {
14867c478bd9Sstevel@tonic-gate 			t_id = np->e_itimer;
14877c478bd9Sstevel@tonic-gate 			np->e_itimer = 0;
14887c478bd9Sstevel@tonic-gate 		}
14897c478bd9Sstevel@tonic-gate 		mutex_exit(&np->e_ilock);
14907c478bd9Sstevel@tonic-gate 		/*
14917c478bd9Sstevel@tonic-gate 		 * Reset the idle timer if it has been set
14927c478bd9Sstevel@tonic-gate 		 */
14937c478bd9Sstevel@tonic-gate 		if (t_id != (timeout_id_t)0)
14947c478bd9Sstevel@tonic-gate 			(void) untimeout(t_id);
14957c478bd9Sstevel@tonic-gate 
14967c478bd9Sstevel@tonic-gate 		if (check_endpnt(endp, &new) == 0)
14977c478bd9Sstevel@tonic-gate 			return (new);
14987c478bd9Sstevel@tonic-gate 	} else if (np->e_cnt >= clnt_clts_max_endpoints) {
14997c478bd9Sstevel@tonic-gate 		/*
15007c478bd9Sstevel@tonic-gate 		 * There are no idle endpoints currently, so
15017c478bd9Sstevel@tonic-gate 		 * create a new one if we have not reached the maximum or
15027c478bd9Sstevel@tonic-gate 		 * hand one out in round-robin.
15037c478bd9Sstevel@tonic-gate 		 */
15047c478bd9Sstevel@tonic-gate 		mutex_exit(&np->e_ilock);
15057c478bd9Sstevel@tonic-gate 		mutex_enter(&np->e_plock);
15067c478bd9Sstevel@tonic-gate 		endp = np->e_pcurr;
15077c478bd9Sstevel@tonic-gate 		mutex_enter(&endp->e_lock);
15087c478bd9Sstevel@tonic-gate 		endp->e_ref++;
15097c478bd9Sstevel@tonic-gate 		mutex_exit(&endp->e_lock);
15107c478bd9Sstevel@tonic-gate 
15117c478bd9Sstevel@tonic-gate 		ASSERT(endp != NULL);
15127c478bd9Sstevel@tonic-gate 		/*
15137c478bd9Sstevel@tonic-gate 		 * Advance the pointer to the next eligible endpoint, if
15147c478bd9Sstevel@tonic-gate 		 * necessary.
15157c478bd9Sstevel@tonic-gate 		 */
15167c478bd9Sstevel@tonic-gate 		if (np->e_cnt > 1) {
15177c478bd9Sstevel@tonic-gate 			next = (endpnt_t *)list_next(&np->e_pool, np->e_pcurr);
15187c478bd9Sstevel@tonic-gate 			if (next == NULL)
15197c478bd9Sstevel@tonic-gate 				next = (endpnt_t *)list_head(&np->e_pool);
15207c478bd9Sstevel@tonic-gate 			np->e_pcurr = next;
15217c478bd9Sstevel@tonic-gate 		}
15227c478bd9Sstevel@tonic-gate 
15237c478bd9Sstevel@tonic-gate 		mutex_exit(&np->e_plock);
15247c478bd9Sstevel@tonic-gate 
15257c478bd9Sstevel@tonic-gate 		/*
15267c478bd9Sstevel@tonic-gate 		 * We need to check to see if this endpoint is bound or
15277c478bd9Sstevel@tonic-gate 		 * not.  If it is in progress then just wait until
15287c478bd9Sstevel@tonic-gate 		 * the set up is complete
15297c478bd9Sstevel@tonic-gate 		 */
15307c478bd9Sstevel@tonic-gate 		if (check_endpnt(endp, &new) == 0)
15317c478bd9Sstevel@tonic-gate 			return (new);
15327c478bd9Sstevel@tonic-gate 	} else {
15337c478bd9Sstevel@tonic-gate 		mutex_exit(&np->e_ilock);
15347c478bd9Sstevel@tonic-gate 		mutex_enter(&np->e_plock);
15357c478bd9Sstevel@tonic-gate 
15367c478bd9Sstevel@tonic-gate 		/*
15377c478bd9Sstevel@tonic-gate 		 * Allocate a new endpoint to use.  If we can't allocate any
15387c478bd9Sstevel@tonic-gate 		 * more memory then use one that is already established if any
15397c478bd9Sstevel@tonic-gate 		 * such endpoints exist.
15407c478bd9Sstevel@tonic-gate 		 */
15417c478bd9Sstevel@tonic-gate 		new = kmem_cache_alloc(endpnt_cache, KM_NOSLEEP);
15427c478bd9Sstevel@tonic-gate 		if (new == NULL) {
15437c478bd9Sstevel@tonic-gate 			RPCLOG0(1, "endpnt_get: kmem_cache_alloc failed\n");
15447c478bd9Sstevel@tonic-gate 			/*
15457c478bd9Sstevel@tonic-gate 			 * Try to recover by using an existing endpoint.
15467c478bd9Sstevel@tonic-gate 			 */
15477c478bd9Sstevel@tonic-gate 			if (np->e_cnt <= 0) {
15487c478bd9Sstevel@tonic-gate 				mutex_exit(&np->e_plock);
15497c478bd9Sstevel@tonic-gate 				return (NULL);
15507c478bd9Sstevel@tonic-gate 			}
15517c478bd9Sstevel@tonic-gate 			endp = np->e_pcurr;
15527c478bd9Sstevel@tonic-gate 			if ((next = list_next(&np->e_pool, np->e_pcurr)) !=
15537c478bd9Sstevel@tonic-gate 			    NULL)
15547c478bd9Sstevel@tonic-gate 				np->e_pcurr = next;
15557c478bd9Sstevel@tonic-gate 			ASSERT(endp != NULL);
15567c478bd9Sstevel@tonic-gate 			mutex_enter(&endp->e_lock);
15577c478bd9Sstevel@tonic-gate 			endp->e_ref++;
15587c478bd9Sstevel@tonic-gate 			mutex_exit(&endp->e_lock);
15597c478bd9Sstevel@tonic-gate 			mutex_exit(&np->e_plock);
15607c478bd9Sstevel@tonic-gate 
15617c478bd9Sstevel@tonic-gate 			if (check_endpnt(endp, &new) == 0)
15627c478bd9Sstevel@tonic-gate 				return (new);
15637c478bd9Sstevel@tonic-gate 		} else {
15647c478bd9Sstevel@tonic-gate 			/*
15657c478bd9Sstevel@tonic-gate 			 * Partially init an endpoint structure and put
15667c478bd9Sstevel@tonic-gate 			 * it on the list, so that other interested threads
15677c478bd9Sstevel@tonic-gate 			 * know that one is being created
15687c478bd9Sstevel@tonic-gate 			 */
15697c478bd9Sstevel@tonic-gate 			bzero(new, sizeof (struct endpnt));
15707c478bd9Sstevel@tonic-gate 
15717c478bd9Sstevel@tonic-gate 			cv_init(&new->e_cv, NULL, CV_DEFAULT, NULL);
15727c478bd9Sstevel@tonic-gate 			mutex_init(&new->e_lock, NULL, MUTEX_DEFAULT, NULL);
15737c478bd9Sstevel@tonic-gate 			new->e_ref = 1;
15747c478bd9Sstevel@tonic-gate 			new->e_type = np;
15757c478bd9Sstevel@tonic-gate 
15767c478bd9Sstevel@tonic-gate 			/*
15777c478bd9Sstevel@tonic-gate 			 * Link the endpoint into the pool.
15787c478bd9Sstevel@tonic-gate 			 */
15797c478bd9Sstevel@tonic-gate 			list_insert_head(&np->e_pool, new);
15807c478bd9Sstevel@tonic-gate 			np->e_cnt++;
15817c478bd9Sstevel@tonic-gate 			if (np->e_pcurr == NULL)
15827c478bd9Sstevel@tonic-gate 				np->e_pcurr = new;
15837c478bd9Sstevel@tonic-gate 			mutex_exit(&np->e_plock);
15847c478bd9Sstevel@tonic-gate 		}
15857c478bd9Sstevel@tonic-gate 	}
15867c478bd9Sstevel@tonic-gate 
15877c478bd9Sstevel@tonic-gate 	/*
15887c478bd9Sstevel@tonic-gate 	 * The transport should be opened with sufficient privs
15897c478bd9Sstevel@tonic-gate 	 */
159045916cd2Sjpk 	cr = zone_kcred();
15917c478bd9Sstevel@tonic-gate 	error = t_kopen(NULL, config->knc_rdev, FREAD|FWRITE|FNDELAY, &tiptr,
159245916cd2Sjpk 	    cr);
15937c478bd9Sstevel@tonic-gate 	if (error) {
15947c478bd9Sstevel@tonic-gate 		RPCLOG(1, "endpnt_get: t_kopen: %d\n", error);
15957c478bd9Sstevel@tonic-gate 		goto bad;
15967c478bd9Sstevel@tonic-gate 	}
15977c478bd9Sstevel@tonic-gate 
15987c478bd9Sstevel@tonic-gate 	new->e_tiptr = tiptr;
15997c478bd9Sstevel@tonic-gate 	rpc_poptimod(tiptr->fp->f_vnode);
16007c478bd9Sstevel@tonic-gate 
16017c478bd9Sstevel@tonic-gate 	/*
16027c478bd9Sstevel@tonic-gate 	 * Allow the kernel to push the module on behalf of the user.
16037c478bd9Sstevel@tonic-gate 	 */
16047c478bd9Sstevel@tonic-gate 	error = strioctl(tiptr->fp->f_vnode, I_PUSH, (intptr_t)"rpcmod", 0,
160545916cd2Sjpk 	    K_TO_K, cr, &retval);
16067c478bd9Sstevel@tonic-gate 	if (error) {
16077c478bd9Sstevel@tonic-gate 		RPCLOG(1, "endpnt_get: kstr_push on rpcmod failed %d\n", error);
16087c478bd9Sstevel@tonic-gate 		goto bad;
16097c478bd9Sstevel@tonic-gate 	}
16107c478bd9Sstevel@tonic-gate 
16117c478bd9Sstevel@tonic-gate 	error = strioctl(tiptr->fp->f_vnode, RPC_CLIENT, 0, 0, K_TO_K,
161245916cd2Sjpk 	    cr, &retval);
16137c478bd9Sstevel@tonic-gate 	if (error) {
16147c478bd9Sstevel@tonic-gate 		RPCLOG(1, "endpnt_get: strioctl failed %d\n", error);
16157c478bd9Sstevel@tonic-gate 		goto bad;
16167c478bd9Sstevel@tonic-gate 	}
16177c478bd9Sstevel@tonic-gate 
16187c478bd9Sstevel@tonic-gate 	/*
16197c478bd9Sstevel@tonic-gate 	 * Connectionless data flow should bypass the stream head.
16207c478bd9Sstevel@tonic-gate 	 */
16217c478bd9Sstevel@tonic-gate 	new->e_wq = tiptr->fp->f_vnode->v_stream->sd_wrq->q_next;
16227c478bd9Sstevel@tonic-gate 
16237c478bd9Sstevel@tonic-gate 	error = strioctl(tiptr->fp->f_vnode, I_PUSH, (intptr_t)"timod", 0,
162445916cd2Sjpk 	    K_TO_K, cr, &retval);
16257c478bd9Sstevel@tonic-gate 	if (error) {
16267c478bd9Sstevel@tonic-gate 		RPCLOG(1, "endpnt_get: kstr_push on timod failed %d\n", error);
16277c478bd9Sstevel@tonic-gate 		goto bad;
16287c478bd9Sstevel@tonic-gate 	}
16297c478bd9Sstevel@tonic-gate 
16307c478bd9Sstevel@tonic-gate 	/*
16317c478bd9Sstevel@tonic-gate 	 * Attempt to bind the endpoint.  If we fail then propogate
16327c478bd9Sstevel@tonic-gate 	 * error back to calling subsystem, so that it can be handled
16337c478bd9Sstevel@tonic-gate 	 * appropriately.
163478598ee3Snd150628 	 * If the caller has not specified reserved port usage then
163578598ee3Snd150628 	 * take the system default.
16367c478bd9Sstevel@tonic-gate 	 */
163778598ee3Snd150628 	if (useresvport == -1)
163878598ee3Snd150628 		useresvport = clnt_clts_do_bindresvport;
163978598ee3Snd150628 
164078598ee3Snd150628 	if (useresvport &&
16417c478bd9Sstevel@tonic-gate 	    (strcmp(config->knc_protofmly, NC_INET) == 0 ||
16427c478bd9Sstevel@tonic-gate 	    strcmp(config->knc_protofmly, NC_INET6) == 0)) {
16437c478bd9Sstevel@tonic-gate 
16447c478bd9Sstevel@tonic-gate 		while ((error =
16457c478bd9Sstevel@tonic-gate 		    bindresvport(new->e_tiptr, NULL, NULL, FALSE)) != 0) {
16467c478bd9Sstevel@tonic-gate 			RPCLOG(1,
16478ffff9fdSgt29601 			    "endpnt_get: bindresvport error %d\n", error);
16487c478bd9Sstevel@tonic-gate 			if (error != EPROTO) {
16497c478bd9Sstevel@tonic-gate 				if (rtries-- <= 0)
16507c478bd9Sstevel@tonic-gate 					goto bad;
16517c478bd9Sstevel@tonic-gate 
16527c478bd9Sstevel@tonic-gate 				delay(hz << i++);
16537c478bd9Sstevel@tonic-gate 				continue;
16547c478bd9Sstevel@tonic-gate 			}
16557c478bd9Sstevel@tonic-gate 
16567c478bd9Sstevel@tonic-gate 			(void) t_kclose(new->e_tiptr, 1);
16577c478bd9Sstevel@tonic-gate 			/*
16587c478bd9Sstevel@tonic-gate 			 * reopen with all privileges
16597c478bd9Sstevel@tonic-gate 			 */
16607c478bd9Sstevel@tonic-gate 			error = t_kopen(NULL, config->knc_rdev,
16617c478bd9Sstevel@tonic-gate 			    FREAD|FWRITE|FNDELAY,
166245916cd2Sjpk 			    &new->e_tiptr, cr);
16637c478bd9Sstevel@tonic-gate 			if (error) {
16647c478bd9Sstevel@tonic-gate 				RPCLOG(1, "endpnt_get: t_kopen: %d\n", error);
16657c478bd9Sstevel@tonic-gate 					new->e_tiptr = NULL;
16667c478bd9Sstevel@tonic-gate 					goto bad;
16677c478bd9Sstevel@tonic-gate 			}
16687c478bd9Sstevel@tonic-gate 		}
16697c478bd9Sstevel@tonic-gate 	} else if ((error = t_kbind(new->e_tiptr, NULL, NULL)) != 0) {
16707c478bd9Sstevel@tonic-gate 		RPCLOG(1, "endpnt_get: t_kbind failed: %d\n", error);
16717c478bd9Sstevel@tonic-gate 		goto bad;
16727c478bd9Sstevel@tonic-gate 	}
16737c478bd9Sstevel@tonic-gate 
16747c478bd9Sstevel@tonic-gate 	/*
16757c478bd9Sstevel@tonic-gate 	 * Set the flags and notify and waiters that we have an established
16767c478bd9Sstevel@tonic-gate 	 * endpoint.
16777c478bd9Sstevel@tonic-gate 	 */
16787c478bd9Sstevel@tonic-gate 	mutex_enter(&new->e_lock);
16797c478bd9Sstevel@tonic-gate 	new->e_flags |= ENDPNT_ESTABLISHED;
16807c478bd9Sstevel@tonic-gate 	new->e_flags |= ENDPNT_BOUND;
16817c478bd9Sstevel@tonic-gate 	if (new->e_flags & ENDPNT_WAITING) {
16827c478bd9Sstevel@tonic-gate 		cv_broadcast(&new->e_cv);
16837c478bd9Sstevel@tonic-gate 		new->e_flags &= ~ENDPNT_WAITING;
16847c478bd9Sstevel@tonic-gate 	}
16857c478bd9Sstevel@tonic-gate 	mutex_exit(&new->e_lock);
16867c478bd9Sstevel@tonic-gate 
16877c478bd9Sstevel@tonic-gate 	return (new);
16887c478bd9Sstevel@tonic-gate 
16897c478bd9Sstevel@tonic-gate bad:
16907c478bd9Sstevel@tonic-gate 	ASSERT(new != NULL);
16917c478bd9Sstevel@tonic-gate 	/*
16927c478bd9Sstevel@tonic-gate 	 * mark this endpoint as stale and notify any threads waiting
16937c478bd9Sstevel@tonic-gate 	 * on this endpoint that it will be going away.
16947c478bd9Sstevel@tonic-gate 	 */
16957c478bd9Sstevel@tonic-gate 	mutex_enter(&new->e_lock);
16967c478bd9Sstevel@tonic-gate 	if (new->e_ref > 0) {
16977c478bd9Sstevel@tonic-gate 		new->e_flags |= ENDPNT_ESTABLISHED;
16987c478bd9Sstevel@tonic-gate 		new->e_flags |= ENDPNT_STALE;
16997c478bd9Sstevel@tonic-gate 		if (new->e_flags & ENDPNT_WAITING) {
17007c478bd9Sstevel@tonic-gate 			cv_broadcast(&new->e_cv);
17017c478bd9Sstevel@tonic-gate 			new->e_flags &= ~ENDPNT_WAITING;
17027c478bd9Sstevel@tonic-gate 		}
17037c478bd9Sstevel@tonic-gate 	}
17047c478bd9Sstevel@tonic-gate 	new->e_ref--;
17057c478bd9Sstevel@tonic-gate 	new->e_tiptr = NULL;
17067c478bd9Sstevel@tonic-gate 	mutex_exit(&new->e_lock);
17077c478bd9Sstevel@tonic-gate 
17087c478bd9Sstevel@tonic-gate 	/*
17097c478bd9Sstevel@tonic-gate 	 * If there was a transport endopoint opened, then close it.
17107c478bd9Sstevel@tonic-gate 	 */
17117c478bd9Sstevel@tonic-gate 	if (tiptr != NULL)
17127c478bd9Sstevel@tonic-gate 		(void) t_kclose(tiptr, 1);
17137c478bd9Sstevel@tonic-gate 
17147c478bd9Sstevel@tonic-gate 	return (NULL);
17157c478bd9Sstevel@tonic-gate }
17167c478bd9Sstevel@tonic-gate 
17177c478bd9Sstevel@tonic-gate /*
17187c478bd9Sstevel@tonic-gate  * Release a referece to the endpoint
17197c478bd9Sstevel@tonic-gate  */
17207c478bd9Sstevel@tonic-gate static void
endpnt_rele(struct endpnt * sp)17217c478bd9Sstevel@tonic-gate endpnt_rele(struct endpnt *sp)
17227c478bd9Sstevel@tonic-gate {
17237c478bd9Sstevel@tonic-gate 	mutex_enter(&sp->e_lock);
17247c478bd9Sstevel@tonic-gate 	ASSERT(sp->e_ref > 0);
17257c478bd9Sstevel@tonic-gate 	sp->e_ref--;
17267c478bd9Sstevel@tonic-gate 	/*
17277c478bd9Sstevel@tonic-gate 	 * If the ref count is zero, then start the idle timer and link
17287c478bd9Sstevel@tonic-gate 	 * the endpoint onto the idle list.
17297c478bd9Sstevel@tonic-gate 	 */
17307c478bd9Sstevel@tonic-gate 	if (sp->e_ref == 0) {
17317c478bd9Sstevel@tonic-gate 		sp->e_itime = gethrestime_sec();
17327c478bd9Sstevel@tonic-gate 
17337c478bd9Sstevel@tonic-gate 		/*
17347c478bd9Sstevel@tonic-gate 		 * Check to see if the endpoint is already linked to the idle
17357c478bd9Sstevel@tonic-gate 		 * list, so that we don't try to reinsert it.
17367c478bd9Sstevel@tonic-gate 		 */
17377c478bd9Sstevel@tonic-gate 		if (sp->e_flags & ENDPNT_ONIDLE) {
17387c478bd9Sstevel@tonic-gate 			mutex_exit(&sp->e_lock);
17397c478bd9Sstevel@tonic-gate 			mutex_enter(&sp->e_type->e_ilock);
17407c478bd9Sstevel@tonic-gate 			endpnt_reap_settimer(sp->e_type);
17417c478bd9Sstevel@tonic-gate 			mutex_exit(&sp->e_type->e_ilock);
17427c478bd9Sstevel@tonic-gate 			return;
17437c478bd9Sstevel@tonic-gate 		}
17447c478bd9Sstevel@tonic-gate 
17457c478bd9Sstevel@tonic-gate 		sp->e_flags |= ENDPNT_ONIDLE;
17467c478bd9Sstevel@tonic-gate 		mutex_exit(&sp->e_lock);
17477c478bd9Sstevel@tonic-gate 		mutex_enter(&sp->e_type->e_ilock);
17487c478bd9Sstevel@tonic-gate 		list_insert_tail(&sp->e_type->e_ilist, sp);
17497c478bd9Sstevel@tonic-gate 		endpnt_reap_settimer(sp->e_type);
17507c478bd9Sstevel@tonic-gate 		mutex_exit(&sp->e_type->e_ilock);
17517c478bd9Sstevel@tonic-gate 	} else
17527c478bd9Sstevel@tonic-gate 		mutex_exit(&sp->e_lock);
17537c478bd9Sstevel@tonic-gate }
17547c478bd9Sstevel@tonic-gate 
17557c478bd9Sstevel@tonic-gate static void
endpnt_reap_settimer(endpnt_type_t * etp)17567c478bd9Sstevel@tonic-gate endpnt_reap_settimer(endpnt_type_t *etp)
17577c478bd9Sstevel@tonic-gate {
17587c478bd9Sstevel@tonic-gate 	if (etp->e_itimer == (timeout_id_t)0)
17597c478bd9Sstevel@tonic-gate 		etp->e_itimer = timeout(endpnt_reap_dispatch, (void *)etp,
17607c478bd9Sstevel@tonic-gate 		    clnt_clts_taskq_dispatch_interval);
17617c478bd9Sstevel@tonic-gate }
17627c478bd9Sstevel@tonic-gate 
17637c478bd9Sstevel@tonic-gate static void
endpnt_reap_dispatch(void * a)17647c478bd9Sstevel@tonic-gate endpnt_reap_dispatch(void *a)
17657c478bd9Sstevel@tonic-gate {
17667c478bd9Sstevel@tonic-gate 	endpnt_type_t *etp = a;
17677c478bd9Sstevel@tonic-gate 
17687c478bd9Sstevel@tonic-gate 	/*
17697c478bd9Sstevel@tonic-gate 	 * The idle timer has fired, so dispatch the taskq to close the
17707c478bd9Sstevel@tonic-gate 	 * endpoint.
17717c478bd9Sstevel@tonic-gate 	 */
17727c478bd9Sstevel@tonic-gate 	if (taskq_dispatch(endpnt_taskq, (task_func_t *)endpnt_reap, etp,
17737c478bd9Sstevel@tonic-gate 	    TQ_NOSLEEP) == NULL)
17747c478bd9Sstevel@tonic-gate 		return;
17757c478bd9Sstevel@tonic-gate 	mutex_enter(&etp->e_ilock);
17767c478bd9Sstevel@tonic-gate 	etp->e_async_count++;
17777c478bd9Sstevel@tonic-gate 	mutex_exit(&etp->e_ilock);
17787c478bd9Sstevel@tonic-gate }
17797c478bd9Sstevel@tonic-gate 
17807c478bd9Sstevel@tonic-gate /*
17817c478bd9Sstevel@tonic-gate  * Traverse the idle list and close those endpoints that have reached their
17827c478bd9Sstevel@tonic-gate  * timeout interval.
17837c478bd9Sstevel@tonic-gate  */
17847c478bd9Sstevel@tonic-gate static void
endpnt_reap(endpnt_type_t * etp)17857c478bd9Sstevel@tonic-gate endpnt_reap(endpnt_type_t *etp)
17867c478bd9Sstevel@tonic-gate {
17877c478bd9Sstevel@tonic-gate 	struct endpnt *e;
17887c478bd9Sstevel@tonic-gate 	struct endpnt *next_node = NULL;
17897c478bd9Sstevel@tonic-gate 
17907c478bd9Sstevel@tonic-gate 	mutex_enter(&etp->e_ilock);
17917c478bd9Sstevel@tonic-gate 	e = list_head(&etp->e_ilist);
17927c478bd9Sstevel@tonic-gate 	while (e != NULL) {
17937c478bd9Sstevel@tonic-gate 		next_node = list_next(&etp->e_ilist, e);
17947c478bd9Sstevel@tonic-gate 
17957c478bd9Sstevel@tonic-gate 		mutex_enter(&e->e_lock);
17967c478bd9Sstevel@tonic-gate 		if (e->e_ref > 0) {
17977c478bd9Sstevel@tonic-gate 			mutex_exit(&e->e_lock);
17987c478bd9Sstevel@tonic-gate 			e = next_node;
17997c478bd9Sstevel@tonic-gate 			continue;
18007c478bd9Sstevel@tonic-gate 		}
18017c478bd9Sstevel@tonic-gate 
18027c478bd9Sstevel@tonic-gate 		ASSERT(e->e_ref == 0);
18037c478bd9Sstevel@tonic-gate 		if (e->e_itime > 0 &&
18047c478bd9Sstevel@tonic-gate 		    (e->e_itime + clnt_clts_endpoint_reap_interval) <
18057c478bd9Sstevel@tonic-gate 		    gethrestime_sec()) {
18067c478bd9Sstevel@tonic-gate 			e->e_flags &= ~ENDPNT_BOUND;
18077c478bd9Sstevel@tonic-gate 			(void) t_kclose(e->e_tiptr, 1);
18087c478bd9Sstevel@tonic-gate 			e->e_tiptr = NULL;
18097c478bd9Sstevel@tonic-gate 			e->e_itime = 0;
18107c478bd9Sstevel@tonic-gate 		}
18117c478bd9Sstevel@tonic-gate 		mutex_exit(&e->e_lock);
18127c478bd9Sstevel@tonic-gate 		e = next_node;
18137c478bd9Sstevel@tonic-gate 	}
18147c478bd9Sstevel@tonic-gate 	etp->e_itimer = 0;
18157c478bd9Sstevel@tonic-gate 	if (--etp->e_async_count == 0)
18167c478bd9Sstevel@tonic-gate 		cv_signal(&etp->e_async_cv);
18177c478bd9Sstevel@tonic-gate 	mutex_exit(&etp->e_ilock);
18187c478bd9Sstevel@tonic-gate }
18197c478bd9Sstevel@tonic-gate 
18207c478bd9Sstevel@tonic-gate static void
endpnt_reclaim(zoneid_t zoneid)18217c478bd9Sstevel@tonic-gate endpnt_reclaim(zoneid_t zoneid)
18227c478bd9Sstevel@tonic-gate {
18237c478bd9Sstevel@tonic-gate 	struct endpnt_type *np;
18247c478bd9Sstevel@tonic-gate 	struct endpnt *e;
18257c478bd9Sstevel@tonic-gate 	struct endpnt *next_node = NULL;
18267c478bd9Sstevel@tonic-gate 	list_t free_list;
18277c478bd9Sstevel@tonic-gate 	int rcnt = 0;
18287c478bd9Sstevel@tonic-gate 
18297c478bd9Sstevel@tonic-gate 	list_create(&free_list, sizeof (endpnt_t), offsetof(endpnt_t, e_node));
18307c478bd9Sstevel@tonic-gate 
18317c478bd9Sstevel@tonic-gate 	RPCLOG0(1, "endpnt_reclaim: reclaim callback started\n");
18327c478bd9Sstevel@tonic-gate 	rw_enter(&endpnt_type_lock, RW_READER);
18337c478bd9Sstevel@tonic-gate 	for (np = endpnt_type_list; np != NULL; np = np->e_next) {
18347c478bd9Sstevel@tonic-gate 		if (zoneid != ALL_ZONES && zoneid != np->e_zoneid)
18357c478bd9Sstevel@tonic-gate 			continue;
18367c478bd9Sstevel@tonic-gate 
18377c478bd9Sstevel@tonic-gate 		mutex_enter(&np->e_plock);
18387c478bd9Sstevel@tonic-gate 		RPCLOG(1, "endpnt_reclaim: protofmly %s, ",
18397c478bd9Sstevel@tonic-gate 		    np->e_protofmly);
18407c478bd9Sstevel@tonic-gate 		RPCLOG(1, "rdev %ld\n", np->e_rdev);
18417c478bd9Sstevel@tonic-gate 		RPCLOG(1, "endpnt_reclaim: found %d endpoint(s)\n",
18427c478bd9Sstevel@tonic-gate 		    np->e_cnt);
18437c478bd9Sstevel@tonic-gate 
18447c478bd9Sstevel@tonic-gate 		if (np->e_cnt == 0) {
18457c478bd9Sstevel@tonic-gate 			mutex_exit(&np->e_plock);
18467c478bd9Sstevel@tonic-gate 			continue;
18477c478bd9Sstevel@tonic-gate 		}
18487c478bd9Sstevel@tonic-gate 
18497c478bd9Sstevel@tonic-gate 		/*
18507c478bd9Sstevel@tonic-gate 		 * The nice thing about maintaining an idle list is that if
18517c478bd9Sstevel@tonic-gate 		 * there are any endpoints to reclaim, they are going to be
18527c478bd9Sstevel@tonic-gate 		 * on this list.  Just go through and reap the one's that
18537c478bd9Sstevel@tonic-gate 		 * have ref counts of zero.
18547c478bd9Sstevel@tonic-gate 		 */
18557c478bd9Sstevel@tonic-gate 		mutex_enter(&np->e_ilock);
18567c478bd9Sstevel@tonic-gate 		e = list_head(&np->e_ilist);
18577c478bd9Sstevel@tonic-gate 		while (e != NULL) {
18587c478bd9Sstevel@tonic-gate 			next_node = list_next(&np->e_ilist, e);
18597c478bd9Sstevel@tonic-gate 			mutex_enter(&e->e_lock);
18607c478bd9Sstevel@tonic-gate 			if (e->e_ref > 0) {
18617c478bd9Sstevel@tonic-gate 				mutex_exit(&e->e_lock);
18627c478bd9Sstevel@tonic-gate 				e = next_node;
18637c478bd9Sstevel@tonic-gate 				continue;
18647c478bd9Sstevel@tonic-gate 			}
18657c478bd9Sstevel@tonic-gate 			ASSERT(e->e_ref == 0);
18667c478bd9Sstevel@tonic-gate 			mutex_exit(&e->e_lock);
18677c478bd9Sstevel@tonic-gate 
18687c478bd9Sstevel@tonic-gate 			list_remove(&np->e_ilist, e);
18697c478bd9Sstevel@tonic-gate 			list_remove(&np->e_pool, e);
18707c478bd9Sstevel@tonic-gate 			list_insert_head(&free_list, e);
18717c478bd9Sstevel@tonic-gate 
18727c478bd9Sstevel@tonic-gate 			rcnt++;
18737c478bd9Sstevel@tonic-gate 			np->e_cnt--;
18747c478bd9Sstevel@tonic-gate 			e = next_node;
18757c478bd9Sstevel@tonic-gate 		}
18767c478bd9Sstevel@tonic-gate 		mutex_exit(&np->e_ilock);
18777c478bd9Sstevel@tonic-gate 		/*
18787c478bd9Sstevel@tonic-gate 		 * Reset the current pointer to be safe
18797c478bd9Sstevel@tonic-gate 		 */
18807c478bd9Sstevel@tonic-gate 		if ((e = (struct endpnt *)list_head(&np->e_pool)) != NULL)
18817c478bd9Sstevel@tonic-gate 			np->e_pcurr = e;
18827c478bd9Sstevel@tonic-gate 		else {
18837c478bd9Sstevel@tonic-gate 			ASSERT(np->e_cnt == 0);
18847c478bd9Sstevel@tonic-gate 			np->e_pcurr = NULL;
18857c478bd9Sstevel@tonic-gate 		}
18867c478bd9Sstevel@tonic-gate 
18877c478bd9Sstevel@tonic-gate 		mutex_exit(&np->e_plock);
18887c478bd9Sstevel@tonic-gate 	}
18897c478bd9Sstevel@tonic-gate 	rw_exit(&endpnt_type_lock);
18907c478bd9Sstevel@tonic-gate 
18917c478bd9Sstevel@tonic-gate 	while ((e = list_head(&free_list)) != NULL) {
18927c478bd9Sstevel@tonic-gate 		list_remove(&free_list, e);
18937c478bd9Sstevel@tonic-gate 		if (e->e_tiptr != NULL)
18947c478bd9Sstevel@tonic-gate 			(void) t_kclose(e->e_tiptr, 1);
18957c478bd9Sstevel@tonic-gate 
18967c478bd9Sstevel@tonic-gate 		cv_destroy(&e->e_cv);
18977c478bd9Sstevel@tonic-gate 		mutex_destroy(&e->e_lock);
18987c478bd9Sstevel@tonic-gate 		kmem_cache_free(endpnt_cache, e);
18997c478bd9Sstevel@tonic-gate 	}
19007c478bd9Sstevel@tonic-gate 	list_destroy(&free_list);
19017c478bd9Sstevel@tonic-gate 	RPCLOG(1, "endpnt_reclaim: reclaimed %d endpoint(s)\n", rcnt);
19027c478bd9Sstevel@tonic-gate }
19037c478bd9Sstevel@tonic-gate 
19047c478bd9Sstevel@tonic-gate /*
19057c478bd9Sstevel@tonic-gate  * Endpoint reclaim zones destructor callback routine.
19067c478bd9Sstevel@tonic-gate  *
19077c478bd9Sstevel@tonic-gate  * After reclaiming any cached entries, we basically go through the endpnt_type
19087c478bd9Sstevel@tonic-gate  * list, canceling outstanding timeouts and free'ing data structures.
19097c478bd9Sstevel@tonic-gate  */
19107c478bd9Sstevel@tonic-gate /* ARGSUSED */
19117c478bd9Sstevel@tonic-gate static void
endpnt_destructor(zoneid_t zoneid,void * a)19127c478bd9Sstevel@tonic-gate endpnt_destructor(zoneid_t zoneid, void *a)
19137c478bd9Sstevel@tonic-gate {
19147c478bd9Sstevel@tonic-gate 	struct endpnt_type **npp;
19157c478bd9Sstevel@tonic-gate 	struct endpnt_type *np;
19167c478bd9Sstevel@tonic-gate 	struct endpnt_type *free_list = NULL;
19177c478bd9Sstevel@tonic-gate 	timeout_id_t t_id = 0;
19187c478bd9Sstevel@tonic-gate 	extern void clcleanup_zone(zoneid_t);
19197c478bd9Sstevel@tonic-gate 	extern void clcleanup4_zone(zoneid_t);
19207c478bd9Sstevel@tonic-gate 
19217c478bd9Sstevel@tonic-gate 	/* Make sure NFS client handles are released. */
19227c478bd9Sstevel@tonic-gate 	clcleanup_zone(zoneid);
19237c478bd9Sstevel@tonic-gate 	clcleanup4_zone(zoneid);
19247c478bd9Sstevel@tonic-gate 
19257c478bd9Sstevel@tonic-gate 	endpnt_reclaim(zoneid);
19267c478bd9Sstevel@tonic-gate 	/*
19277c478bd9Sstevel@tonic-gate 	 * We don't need to be holding on to any locks across the call to
19287c478bd9Sstevel@tonic-gate 	 * endpnt_reclaim() and the code below; we know that no-one can
19297c478bd9Sstevel@tonic-gate 	 * be holding open connections for this zone (all processes and kernel
19307c478bd9Sstevel@tonic-gate 	 * threads are gone), so nothing could be adding anything to the list.
19317c478bd9Sstevel@tonic-gate 	 */
19327c478bd9Sstevel@tonic-gate 	rw_enter(&endpnt_type_lock, RW_WRITER);
19337c478bd9Sstevel@tonic-gate 	npp = &endpnt_type_list;
19347c478bd9Sstevel@tonic-gate 	while ((np = *npp) != NULL) {
19357c478bd9Sstevel@tonic-gate 		if (np->e_zoneid != zoneid) {
19367c478bd9Sstevel@tonic-gate 			npp = &np->e_next;
19377c478bd9Sstevel@tonic-gate 			continue;
19387c478bd9Sstevel@tonic-gate 		}
19397c478bd9Sstevel@tonic-gate 		mutex_enter(&np->e_plock);
19407c478bd9Sstevel@tonic-gate 		mutex_enter(&np->e_ilock);
19417c478bd9Sstevel@tonic-gate 		if (np->e_itimer != 0) {
19427c478bd9Sstevel@tonic-gate 			t_id = np->e_itimer;
19437c478bd9Sstevel@tonic-gate 			np->e_itimer = 0;
19447c478bd9Sstevel@tonic-gate 		}
19457c478bd9Sstevel@tonic-gate 		ASSERT(np->e_cnt == 0);
19467c478bd9Sstevel@tonic-gate 		ASSERT(list_head(&np->e_pool) == NULL);
19477c478bd9Sstevel@tonic-gate 		ASSERT(list_head(&np->e_ilist) == NULL);
19487c478bd9Sstevel@tonic-gate 
19497c478bd9Sstevel@tonic-gate 		mutex_exit(&np->e_ilock);
19507c478bd9Sstevel@tonic-gate 		mutex_exit(&np->e_plock);
19517c478bd9Sstevel@tonic-gate 
19527c478bd9Sstevel@tonic-gate 		/*
19537c478bd9Sstevel@tonic-gate 		 * untimeout() any outstanding timers that have not yet fired.
19547c478bd9Sstevel@tonic-gate 		 */
19557c478bd9Sstevel@tonic-gate 		if (t_id != (timeout_id_t)0)
19567c478bd9Sstevel@tonic-gate 			(void) untimeout(t_id);
19577c478bd9Sstevel@tonic-gate 		*npp = np->e_next;
19587c478bd9Sstevel@tonic-gate 		np->e_next = free_list;
19597c478bd9Sstevel@tonic-gate 		free_list = np;
19607c478bd9Sstevel@tonic-gate 	}
19617c478bd9Sstevel@tonic-gate 	rw_exit(&endpnt_type_lock);
19627c478bd9Sstevel@tonic-gate 
19637c478bd9Sstevel@tonic-gate 	while (free_list != NULL) {
19647c478bd9Sstevel@tonic-gate 		np = free_list;
19657c478bd9Sstevel@tonic-gate 		free_list = free_list->e_next;
19667c478bd9Sstevel@tonic-gate 		/*
19677c478bd9Sstevel@tonic-gate 		 * Wait for threads in endpnt_taskq trying to reap endpnt_ts in
19687c478bd9Sstevel@tonic-gate 		 * the endpnt_type_t.
19697c478bd9Sstevel@tonic-gate 		 */
19707c478bd9Sstevel@tonic-gate 		mutex_enter(&np->e_ilock);
19717c478bd9Sstevel@tonic-gate 		while (np->e_async_count > 0)
19727c478bd9Sstevel@tonic-gate 			cv_wait(&np->e_async_cv, &np->e_ilock);
19737c478bd9Sstevel@tonic-gate 		cv_destroy(&np->e_async_cv);
19747c478bd9Sstevel@tonic-gate 		mutex_destroy(&np->e_plock);
19757c478bd9Sstevel@tonic-gate 		mutex_destroy(&np->e_ilock);
19767c478bd9Sstevel@tonic-gate 		list_destroy(&np->e_pool);
19777c478bd9Sstevel@tonic-gate 		list_destroy(&np->e_ilist);
19787c478bd9Sstevel@tonic-gate 		kmem_free(np, sizeof (endpnt_type_t));
19797c478bd9Sstevel@tonic-gate 	}
19807c478bd9Sstevel@tonic-gate }
19817c478bd9Sstevel@tonic-gate 
19827c478bd9Sstevel@tonic-gate /*
19837c478bd9Sstevel@tonic-gate  * Endpoint reclaim kmem callback routine.
19847c478bd9Sstevel@tonic-gate  */
19857c478bd9Sstevel@tonic-gate /* ARGSUSED */
19867c478bd9Sstevel@tonic-gate static void
endpnt_repossess(void * a)19877c478bd9Sstevel@tonic-gate endpnt_repossess(void *a)
19887c478bd9Sstevel@tonic-gate {
19897c478bd9Sstevel@tonic-gate 	/*
19907c478bd9Sstevel@tonic-gate 	 * Reclaim idle endpnt's from all zones.
19917c478bd9Sstevel@tonic-gate 	 */
19927c478bd9Sstevel@tonic-gate 	if (endpnt_taskq != NULL)
19937c478bd9Sstevel@tonic-gate 		(void) taskq_dispatch(endpnt_taskq,
19947c478bd9Sstevel@tonic-gate 		    (task_func_t *)endpnt_reclaim, (void *)ALL_ZONES,
19957c478bd9Sstevel@tonic-gate 		    TQ_NOSLEEP);
19967c478bd9Sstevel@tonic-gate }
19977c478bd9Sstevel@tonic-gate 
19987c478bd9Sstevel@tonic-gate /*
19997c478bd9Sstevel@tonic-gate  * RPC request dispatch routine.  Constructs a datagram message and wraps it
20007c478bd9Sstevel@tonic-gate  * around the RPC request to pass downstream.
20017c478bd9Sstevel@tonic-gate  */
20027c478bd9Sstevel@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)20037c478bd9Sstevel@tonic-gate clnt_clts_dispatch_send(queue_t *q, mblk_t *mp, struct netbuf *addr,
2004de8c4a14SErik Nordmark     calllist_t *cp, uint_t xid, cred_t *cr)
20057c478bd9Sstevel@tonic-gate {
20067c478bd9Sstevel@tonic-gate 	mblk_t *bp;
20077c478bd9Sstevel@tonic-gate 	int msgsz;
20087c478bd9Sstevel@tonic-gate 	struct T_unitdata_req *udreq;
20097c478bd9Sstevel@tonic-gate 
20107c478bd9Sstevel@tonic-gate 	/*
20117c478bd9Sstevel@tonic-gate 	 * Set up the call record.
20127c478bd9Sstevel@tonic-gate 	 */
20137c478bd9Sstevel@tonic-gate 	cp->call_wq = q;
20147c478bd9Sstevel@tonic-gate 	cp->call_xid = xid;
20157c478bd9Sstevel@tonic-gate 	cp->call_status = RPC_TIMEDOUT;
20167c478bd9Sstevel@tonic-gate 	cp->call_notified = FALSE;
20177c478bd9Sstevel@tonic-gate 	RPCLOG(64,
20187c478bd9Sstevel@tonic-gate 	    "clnt_clts_dispatch_send: putting xid 0x%x on "
20197c478bd9Sstevel@tonic-gate 	    "dispatch list\n", xid);
20207c478bd9Sstevel@tonic-gate 	cp->call_hash = call_hash(xid, clnt_clts_hash_size);
20217c478bd9Sstevel@tonic-gate 	cp->call_bucket = &clts_call_ht[cp->call_hash];
20227c478bd9Sstevel@tonic-gate 	call_table_enter(cp);
20237c478bd9Sstevel@tonic-gate 
20247c478bd9Sstevel@tonic-gate 	/*
20257c478bd9Sstevel@tonic-gate 	 * Construct the datagram
20267c478bd9Sstevel@tonic-gate 	 */
20277c478bd9Sstevel@tonic-gate 	msgsz = (int)TUNITDATAREQSZ;
2028de8c4a14SErik Nordmark 	/*
2029de8c4a14SErik Nordmark 	 * Note: if the receiver uses SCM_UCRED/getpeerucred the pid will
2030de8c4a14SErik Nordmark 	 * appear as -1.
2031de8c4a14SErik Nordmark 	 */
2032de8c4a14SErik Nordmark 	while (!(bp = allocb_cred(msgsz + addr->len, cr, NOPID))) {
20337c478bd9Sstevel@tonic-gate 		if (strwaitbuf(msgsz + addr->len, BPRI_LO))
20347c478bd9Sstevel@tonic-gate 			return (ENOSR);
20357c478bd9Sstevel@tonic-gate 	}
20367c478bd9Sstevel@tonic-gate 
20377c478bd9Sstevel@tonic-gate 	udreq = (struct T_unitdata_req *)bp->b_wptr;
20387c478bd9Sstevel@tonic-gate 	udreq->PRIM_type = T_UNITDATA_REQ;
20397c478bd9Sstevel@tonic-gate 	udreq->DEST_length = addr->len;
20407c478bd9Sstevel@tonic-gate 
20417c478bd9Sstevel@tonic-gate 	if (addr->len) {
20427c478bd9Sstevel@tonic-gate 		bcopy(addr->buf, bp->b_wptr + msgsz, addr->len);
20437c478bd9Sstevel@tonic-gate 		udreq->DEST_offset = (t_scalar_t)msgsz;
20447c478bd9Sstevel@tonic-gate 		msgsz += addr->len;
20457c478bd9Sstevel@tonic-gate 	} else
20467c478bd9Sstevel@tonic-gate 		udreq->DEST_offset = 0;
20477c478bd9Sstevel@tonic-gate 	udreq->OPT_length = 0;
20487c478bd9Sstevel@tonic-gate 	udreq->OPT_offset = 0;
20497c478bd9Sstevel@tonic-gate 
20507c478bd9Sstevel@tonic-gate 	bp->b_datap->db_type = M_PROTO;
20517c478bd9Sstevel@tonic-gate 	bp->b_wptr += msgsz;
20527c478bd9Sstevel@tonic-gate 
20537c478bd9Sstevel@tonic-gate 	/*
20547c478bd9Sstevel@tonic-gate 	 * Link the datagram header with the actual data
20557c478bd9Sstevel@tonic-gate 	 */
20567c478bd9Sstevel@tonic-gate 	linkb(bp, mp);
20577c478bd9Sstevel@tonic-gate 
20587c478bd9Sstevel@tonic-gate 	/*
20597c478bd9Sstevel@tonic-gate 	 * Send downstream.
20607c478bd9Sstevel@tonic-gate 	 */
2061125a8fd9SSiddheshwar Mahesh 	if (canput(cp->call_wq)) {
20627c478bd9Sstevel@tonic-gate 		put(cp->call_wq, bp);
20637c478bd9Sstevel@tonic-gate 		return (0);
20647c478bd9Sstevel@tonic-gate 	}
20657c478bd9Sstevel@tonic-gate 
2066125a8fd9SSiddheshwar Mahesh 	return (EIO);
2067125a8fd9SSiddheshwar Mahesh }
2068125a8fd9SSiddheshwar Mahesh 
20697c478bd9Sstevel@tonic-gate /*
20707c478bd9Sstevel@tonic-gate  * RPC response delivery routine.  Deliver the response to the waiting
20717c478bd9Sstevel@tonic-gate  * thread by matching the xid.
20727c478bd9Sstevel@tonic-gate  */
20737c478bd9Sstevel@tonic-gate void
clnt_clts_dispatch_notify(mblk_t * mp,int resp_off,zoneid_t zoneid)20747c478bd9Sstevel@tonic-gate clnt_clts_dispatch_notify(mblk_t *mp, int resp_off, zoneid_t zoneid)
20757c478bd9Sstevel@tonic-gate {
20767c478bd9Sstevel@tonic-gate 	calllist_t *e = NULL;
20777c478bd9Sstevel@tonic-gate 	call_table_t *chtp;
20787c478bd9Sstevel@tonic-gate 	uint32_t xid;
20797c478bd9Sstevel@tonic-gate 	uint_t hash;
20807c478bd9Sstevel@tonic-gate 	unsigned char *hdr_offset;
20817c478bd9Sstevel@tonic-gate 	mblk_t *resp;
20827c478bd9Sstevel@tonic-gate 
20837c478bd9Sstevel@tonic-gate 	/*
20847c478bd9Sstevel@tonic-gate 	 * If the RPC response is not contained in the same mblk as the
20857c478bd9Sstevel@tonic-gate 	 * datagram header, then move to the next mblk.
20867c478bd9Sstevel@tonic-gate 	 */
20877c478bd9Sstevel@tonic-gate 	hdr_offset = mp->b_rptr;
20887c478bd9Sstevel@tonic-gate 	resp = mp;
20897c478bd9Sstevel@tonic-gate 	if ((mp->b_wptr - (mp->b_rptr + resp_off)) == 0)
20907c478bd9Sstevel@tonic-gate 		resp = mp->b_cont;
20917c478bd9Sstevel@tonic-gate 	else
20927c478bd9Sstevel@tonic-gate 		resp->b_rptr += resp_off;
20937c478bd9Sstevel@tonic-gate 
20947c478bd9Sstevel@tonic-gate 	ASSERT(resp != NULL);
20957c478bd9Sstevel@tonic-gate 
20967c478bd9Sstevel@tonic-gate 	if ((IS_P2ALIGNED(resp->b_rptr, sizeof (uint32_t))) &&
20977c478bd9Sstevel@tonic-gate 	    (resp->b_wptr - resp->b_rptr) >= sizeof (xid))
20987c478bd9Sstevel@tonic-gate 		xid = *((uint32_t *)resp->b_rptr);
20997c478bd9Sstevel@tonic-gate 	else {
21007c478bd9Sstevel@tonic-gate 		int i = 0;
21017c478bd9Sstevel@tonic-gate 		unsigned char *p = (unsigned char *)&xid;
21027c478bd9Sstevel@tonic-gate 		unsigned char *rptr;
21037c478bd9Sstevel@tonic-gate 		mblk_t *tmp = resp;
21047c478bd9Sstevel@tonic-gate 
21057c478bd9Sstevel@tonic-gate 		/*
21067c478bd9Sstevel@tonic-gate 		 * Copy the xid, byte-by-byte into xid.
21077c478bd9Sstevel@tonic-gate 		 */
21087c478bd9Sstevel@tonic-gate 		while (tmp) {
21097c478bd9Sstevel@tonic-gate 			rptr = tmp->b_rptr;
21107c478bd9Sstevel@tonic-gate 			while (rptr < tmp->b_wptr) {
21117c478bd9Sstevel@tonic-gate 				*p++ = *rptr++;
21127c478bd9Sstevel@tonic-gate 				if (++i >= sizeof (xid))
21137c478bd9Sstevel@tonic-gate 					goto done_xid_copy;
21147c478bd9Sstevel@tonic-gate 			}
21157c478bd9Sstevel@tonic-gate 			tmp = tmp->b_cont;
21167c478bd9Sstevel@tonic-gate 		}
21177c478bd9Sstevel@tonic-gate 
21187c478bd9Sstevel@tonic-gate 		/*
21197c478bd9Sstevel@tonic-gate 		 * If we got here, we ran out of mblk space before the
21207c478bd9Sstevel@tonic-gate 		 * xid could be copied.
21217c478bd9Sstevel@tonic-gate 		 */
21227c478bd9Sstevel@tonic-gate 		ASSERT(tmp == NULL && i < sizeof (xid));
21237c478bd9Sstevel@tonic-gate 
21247c478bd9Sstevel@tonic-gate 		RPCLOG0(1,
21257c478bd9Sstevel@tonic-gate 		    "clnt_dispatch_notify(clts): message less than "
21267c478bd9Sstevel@tonic-gate 		    "size of xid\n");
21277c478bd9Sstevel@tonic-gate 
21287c478bd9Sstevel@tonic-gate 		freemsg(mp);
21297c478bd9Sstevel@tonic-gate 		return;
21307c478bd9Sstevel@tonic-gate 	}
21317c478bd9Sstevel@tonic-gate 
21327c478bd9Sstevel@tonic-gate done_xid_copy:
21337c478bd9Sstevel@tonic-gate 
21347c478bd9Sstevel@tonic-gate 	/*
21357c478bd9Sstevel@tonic-gate 	 * Reset the read pointer back to the beginning of the protocol
21367c478bd9Sstevel@tonic-gate 	 * header if we moved it.
21377c478bd9Sstevel@tonic-gate 	 */
21387c478bd9Sstevel@tonic-gate 	if (mp->b_rptr != hdr_offset)
21397c478bd9Sstevel@tonic-gate 		mp->b_rptr = hdr_offset;
21407c478bd9Sstevel@tonic-gate 
21417c478bd9Sstevel@tonic-gate 	hash = call_hash(xid, clnt_clts_hash_size);
21427c478bd9Sstevel@tonic-gate 	chtp = &clts_call_ht[hash];
21437c478bd9Sstevel@tonic-gate 	/* call_table_find returns with the hash bucket locked */
21447c478bd9Sstevel@tonic-gate 	call_table_find(chtp, xid, e);
21457c478bd9Sstevel@tonic-gate 
21467c478bd9Sstevel@tonic-gate 	if (e != NULL) {
21477c478bd9Sstevel@tonic-gate 		mutex_enter(&e->call_lock);
21488ffff9fdSgt29601 
21498ffff9fdSgt29601 		/*
21508ffff9fdSgt29601 		 * verify that the reply is coming in on
21518ffff9fdSgt29601 		 * the same zone that it was sent from.
21528ffff9fdSgt29601 		 */
21538ffff9fdSgt29601 		if (e->call_zoneid != zoneid) {
21548ffff9fdSgt29601 			mutex_exit(&e->call_lock);
21558ffff9fdSgt29601 			mutex_exit(&chtp->ct_lock);
21568c3630f0SGerald Thornbrugh 			RPCLOG0(8, "clnt_dispatch_notify (clts): incorrect "
21578c3630f0SGerald Thornbrugh 			    "zoneid\n");
21588ffff9fdSgt29601 			freemsg(mp);
21598ffff9fdSgt29601 			return;
21608ffff9fdSgt29601 		}
21618ffff9fdSgt29601 
21627c478bd9Sstevel@tonic-gate 		/*
21637c478bd9Sstevel@tonic-gate 		 * found thread waiting for this reply.
21647c478bd9Sstevel@tonic-gate 		 */
21657c478bd9Sstevel@tonic-gate 		if (e->call_reply) {
21667c478bd9Sstevel@tonic-gate 			RPCLOG(8,
21677c478bd9Sstevel@tonic-gate 			    "clnt_dispatch_notify (clts): discarding old "
21687c478bd9Sstevel@tonic-gate 			    "reply for xid 0x%x\n",
21697c478bd9Sstevel@tonic-gate 			    xid);
21707c478bd9Sstevel@tonic-gate 			freemsg(e->call_reply);
21717c478bd9Sstevel@tonic-gate 		}
21727c478bd9Sstevel@tonic-gate 		e->call_notified = TRUE;
21737c478bd9Sstevel@tonic-gate 		e->call_reply = mp;
21747c478bd9Sstevel@tonic-gate 		e->call_status = RPC_SUCCESS;
21757c478bd9Sstevel@tonic-gate 		cv_signal(&e->call_cv);
21767c478bd9Sstevel@tonic-gate 		mutex_exit(&e->call_lock);
21777c478bd9Sstevel@tonic-gate 		mutex_exit(&chtp->ct_lock);
21787c478bd9Sstevel@tonic-gate 	} else {
21797c478bd9Sstevel@tonic-gate 		zone_t *zone;
21807c478bd9Sstevel@tonic-gate 		struct rpcstat *rpcstat;
21817c478bd9Sstevel@tonic-gate 
21827c478bd9Sstevel@tonic-gate 		mutex_exit(&chtp->ct_lock);
21837c478bd9Sstevel@tonic-gate 		RPCLOG(8, "clnt_dispatch_notify (clts): no caller for reply "
21847c478bd9Sstevel@tonic-gate 		    "0x%x\n", xid);
21857c478bd9Sstevel@tonic-gate 		freemsg(mp);
21867c478bd9Sstevel@tonic-gate 		/*
21877c478bd9Sstevel@tonic-gate 		 * This is unfortunate, but we need to lookup the zone so we
21887c478bd9Sstevel@tonic-gate 		 * can increment its "rcbadxids" counter.
21897c478bd9Sstevel@tonic-gate 		 */
21907c478bd9Sstevel@tonic-gate 		zone = zone_find_by_id(zoneid);
21917c478bd9Sstevel@tonic-gate 		if (zone == NULL) {
21927c478bd9Sstevel@tonic-gate 			/*
21937c478bd9Sstevel@tonic-gate 			 * The zone went away...
21947c478bd9Sstevel@tonic-gate 			 */
21957c478bd9Sstevel@tonic-gate 			return;
21967c478bd9Sstevel@tonic-gate 		}
21977c478bd9Sstevel@tonic-gate 		rpcstat = zone_getspecific(rpcstat_zone_key, zone);
21987c478bd9Sstevel@tonic-gate 		if (zone_status_get(zone) >= ZONE_IS_SHUTTING_DOWN) {
21997c478bd9Sstevel@tonic-gate 			/*
22007c478bd9Sstevel@tonic-gate 			 * Not interested
22017c478bd9Sstevel@tonic-gate 			 */
22027c478bd9Sstevel@tonic-gate 			zone_rele(zone);
22037c478bd9Sstevel@tonic-gate 			return;
22047c478bd9Sstevel@tonic-gate 		}
22057c478bd9Sstevel@tonic-gate 		RCSTAT_INCR(rpcstat->rpc_clts_client, rcbadxids);
22067c478bd9Sstevel@tonic-gate 		zone_rele(zone);
22077c478bd9Sstevel@tonic-gate 	}
22087c478bd9Sstevel@tonic-gate }
22097c478bd9Sstevel@tonic-gate 
22107c478bd9Sstevel@tonic-gate /*
22117c478bd9Sstevel@tonic-gate  * Init routine.  Called when rpcmod is loaded.
22127c478bd9Sstevel@tonic-gate  */
22137c478bd9Sstevel@tonic-gate void
clnt_clts_init(void)22147c478bd9Sstevel@tonic-gate clnt_clts_init(void)
22157c478bd9Sstevel@tonic-gate {
22167c478bd9Sstevel@tonic-gate 	endpnt_cache = kmem_cache_create("clnt_clts_endpnt_cache",
22177c478bd9Sstevel@tonic-gate 	    sizeof (struct endpnt), 0, NULL, NULL, endpnt_repossess, NULL,
22187c478bd9Sstevel@tonic-gate 	    NULL, 0);
22197c478bd9Sstevel@tonic-gate 
22207c478bd9Sstevel@tonic-gate 	rw_init(&endpnt_type_lock, NULL, RW_DEFAULT, NULL);
22217c478bd9Sstevel@tonic-gate 
22227c478bd9Sstevel@tonic-gate 	/*
22237c478bd9Sstevel@tonic-gate 	 * Perform simple bounds checking to make sure that the setting is
22247c478bd9Sstevel@tonic-gate 	 * reasonable
22257c478bd9Sstevel@tonic-gate 	 */
22267c478bd9Sstevel@tonic-gate 	if (clnt_clts_max_endpoints <= 0) {
22277c478bd9Sstevel@tonic-gate 		if (clnt_clts_do_bindresvport)
22287c478bd9Sstevel@tonic-gate 			clnt_clts_max_endpoints = RESERVED_PORTSPACE;
22297c478bd9Sstevel@tonic-gate 		else
22307c478bd9Sstevel@tonic-gate 			clnt_clts_max_endpoints = NONRESERVED_PORTSPACE;
22317c478bd9Sstevel@tonic-gate 	}
22327c478bd9Sstevel@tonic-gate 
22337c478bd9Sstevel@tonic-gate 	if (clnt_clts_do_bindresvport &&
22347c478bd9Sstevel@tonic-gate 	    clnt_clts_max_endpoints > RESERVED_PORTSPACE)
22357c478bd9Sstevel@tonic-gate 		clnt_clts_max_endpoints = RESERVED_PORTSPACE;
22367c478bd9Sstevel@tonic-gate 	else if (clnt_clts_max_endpoints > NONRESERVED_PORTSPACE)
22377c478bd9Sstevel@tonic-gate 		clnt_clts_max_endpoints = NONRESERVED_PORTSPACE;
22387c478bd9Sstevel@tonic-gate 
22397c478bd9Sstevel@tonic-gate 	if (clnt_clts_hash_size < DEFAULT_MIN_HASH_SIZE)
22407c478bd9Sstevel@tonic-gate 		clnt_clts_hash_size = DEFAULT_MIN_HASH_SIZE;
22417c478bd9Sstevel@tonic-gate 
22427c478bd9Sstevel@tonic-gate 	/*
22437c478bd9Sstevel@tonic-gate 	 * Defer creating the taskq until rpcmod gets pushed.  If we are
22447c478bd9Sstevel@tonic-gate 	 * in diskless boot mode, rpcmod will get loaded early even before
22457c478bd9Sstevel@tonic-gate 	 * thread_create() is available.
22467c478bd9Sstevel@tonic-gate 	 */
22477c478bd9Sstevel@tonic-gate 	endpnt_taskq = NULL;
22487c478bd9Sstevel@tonic-gate 	taskq_created = FALSE;
22497c478bd9Sstevel@tonic-gate 	mutex_init(&endpnt_taskq_lock, NULL, MUTEX_DEFAULT, NULL);
22507c478bd9Sstevel@tonic-gate 
22517c478bd9Sstevel@tonic-gate 	if (clnt_clts_endpoint_reap_interval < DEFAULT_ENDPOINT_REAP_INTERVAL)
22527c478bd9Sstevel@tonic-gate 		clnt_clts_endpoint_reap_interval =
22537c478bd9Sstevel@tonic-gate 		    DEFAULT_ENDPOINT_REAP_INTERVAL;
22547c478bd9Sstevel@tonic-gate 
22557c478bd9Sstevel@tonic-gate 	/*
22567c478bd9Sstevel@tonic-gate 	 * Dispatch the taskq at an interval which is offset from the
22577c478bd9Sstevel@tonic-gate 	 * interval that the endpoints should be reaped.
22587c478bd9Sstevel@tonic-gate 	 */
22597c478bd9Sstevel@tonic-gate 	clnt_clts_taskq_dispatch_interval =
22608ffff9fdSgt29601 	    (clnt_clts_endpoint_reap_interval + DEFAULT_INTERVAL_SHIFT) * hz;
22617c478bd9Sstevel@tonic-gate 
22627c478bd9Sstevel@tonic-gate 	/*
22637c478bd9Sstevel@tonic-gate 	 * Initialize the completion queue
22647c478bd9Sstevel@tonic-gate 	 */
22657c478bd9Sstevel@tonic-gate 	clts_call_ht = call_table_init(clnt_clts_hash_size);
22667c478bd9Sstevel@tonic-gate 	/*
22677c478bd9Sstevel@tonic-gate 	 * Initialize the zone destructor callback.
22687c478bd9Sstevel@tonic-gate 	 */
22697c478bd9Sstevel@tonic-gate 	zone_key_create(&endpnt_destructor_key, NULL, NULL, endpnt_destructor);
22707c478bd9Sstevel@tonic-gate }
22717c478bd9Sstevel@tonic-gate 
22727c478bd9Sstevel@tonic-gate void
clnt_clts_fini(void)22737c478bd9Sstevel@tonic-gate clnt_clts_fini(void)
22747c478bd9Sstevel@tonic-gate {
22757c478bd9Sstevel@tonic-gate 	(void) zone_key_delete(endpnt_destructor_key);
22767c478bd9Sstevel@tonic-gate }
2277