xref: /onnv-gate/usr/src/uts/common/rpc/mt_rpcinit.c (revision 6403:3ba77d5f184a)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52951Selowe  * Common Development and Distribution License (the "License").
62951Selowe  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*6403Sgt29601  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
270Sstevel@tonic-gate /*	  All Rights Reserved  	*/
280Sstevel@tonic-gate 
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 */
310Sstevel@tonic-gate 
320Sstevel@tonic-gate /*
330Sstevel@tonic-gate  * Define and initialize MT client/server data.
340Sstevel@tonic-gate  */
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #include	<sys/types.h>
370Sstevel@tonic-gate #include	<sys/t_lock.h>
380Sstevel@tonic-gate #include	<sys/kstat.h>
390Sstevel@tonic-gate #include	<sys/systm.h>
400Sstevel@tonic-gate #include	<sys/zone.h>
410Sstevel@tonic-gate 
420Sstevel@tonic-gate #include	<rpc/types.h>
430Sstevel@tonic-gate #include	<rpc/auth.h>
440Sstevel@tonic-gate #include	<rpc/clnt.h>
450Sstevel@tonic-gate 
460Sstevel@tonic-gate kmutex_t xid_lock;		/* XID allocation */
470Sstevel@tonic-gate kmutex_t clnt_pending_lock;	/* for list of pending calls awaiting replies */
480Sstevel@tonic-gate kmutex_t clnt_max_msg_lock;	/* updating max message sanity check for cots */
490Sstevel@tonic-gate 
500Sstevel@tonic-gate zone_key_t	rpcstat_zone_key;
510Sstevel@tonic-gate 
520Sstevel@tonic-gate /*
530Sstevel@tonic-gate  * rpcstat_zone_[init|fini]_common() ends up being nearly identical to
540Sstevel@tonic-gate  * nfsstat_zone_[init|fini]_common().  Due to them necessarily being in
550Sstevel@tonic-gate  * different modules, however, we end up needing to duplicate the code.
560Sstevel@tonic-gate  */
570Sstevel@tonic-gate kstat_named_t *
rpcstat_zone_init_common(zoneid_t zoneid,const char * module,const char * name,const kstat_named_t * template,size_t template_size)580Sstevel@tonic-gate rpcstat_zone_init_common(zoneid_t zoneid, const char *module, const char *name,
590Sstevel@tonic-gate     const kstat_named_t *template, size_t template_size)
600Sstevel@tonic-gate {
610Sstevel@tonic-gate 	kstat_t *ksp;
620Sstevel@tonic-gate 	kstat_named_t *ks_data;
630Sstevel@tonic-gate 
640Sstevel@tonic-gate 
650Sstevel@tonic-gate /*
660Sstevel@tonic-gate  * PSARC 2001/697 Contract Private Interface
670Sstevel@tonic-gate  * rpc_clts_client
680Sstevel@tonic-gate  * rpc_cots_client
690Sstevel@tonic-gate  * Changes must be reviewed by Solaris File Sharing
700Sstevel@tonic-gate  * Changes must be communicated to contract-2001-697@sun.com
710Sstevel@tonic-gate  *
720Sstevel@tonic-gate  */
730Sstevel@tonic-gate 	ks_data = kmem_alloc(template_size, KM_SLEEP);
740Sstevel@tonic-gate 	bcopy(template, ks_data, template_size);
752951Selowe 	if ((ksp = kstat_create_zone(module, 0, name, "rpc",
760Sstevel@tonic-gate 	    KSTAT_TYPE_NAMED, template_size / sizeof (kstat_named_t),
770Sstevel@tonic-gate 	    KSTAT_FLAG_VIRTUAL | KSTAT_FLAG_WRITABLE, zoneid)) != NULL) {
780Sstevel@tonic-gate 		ksp->ks_data = ks_data;
790Sstevel@tonic-gate 		kstat_install(ksp);
800Sstevel@tonic-gate 	}
810Sstevel@tonic-gate 	return (ks_data);
820Sstevel@tonic-gate }
830Sstevel@tonic-gate 
840Sstevel@tonic-gate void
rpcstat_zone_fini_common(zoneid_t zoneid,const char * module,const char * name)850Sstevel@tonic-gate rpcstat_zone_fini_common(zoneid_t zoneid, const char *module, const char *name)
860Sstevel@tonic-gate {
872951Selowe 	kstat_delete_byname_zone(module, 0, name, zoneid);
880Sstevel@tonic-gate }
890Sstevel@tonic-gate 
900Sstevel@tonic-gate static void *
mt_kstat_zone_init(zoneid_t zoneid)910Sstevel@tonic-gate mt_kstat_zone_init(zoneid_t zoneid)
920Sstevel@tonic-gate {
930Sstevel@tonic-gate 	struct rpcstat *rpcstat;
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 	rpcstat = kmem_alloc(sizeof (*rpcstat), KM_SLEEP);
960Sstevel@tonic-gate 
970Sstevel@tonic-gate 	clnt_clts_stats_init(zoneid, &rpcstat->rpc_clts_client);
980Sstevel@tonic-gate 	svc_clts_stats_init(zoneid, &rpcstat->rpc_clts_server);
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	clnt_cots_stats_init(zoneid, &rpcstat->rpc_cots_client);
1010Sstevel@tonic-gate 	svc_cots_stats_init(zoneid, &rpcstat->rpc_cots_server);
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 	return (rpcstat);
1040Sstevel@tonic-gate }
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate /*
1070Sstevel@tonic-gate  * Deletes the previously allocated "rpc" kstats
1080Sstevel@tonic-gate  */
1090Sstevel@tonic-gate static void
mt_kstat_zone_fini(zoneid_t zoneid,void * data)1100Sstevel@tonic-gate mt_kstat_zone_fini(zoneid_t zoneid, void *data)
1110Sstevel@tonic-gate {
1120Sstevel@tonic-gate 	struct rpcstat *rpcstat = data;
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	clnt_cots_stats_fini(zoneid, &rpcstat->rpc_cots_client);
1150Sstevel@tonic-gate 	svc_cots_stats_fini(zoneid, &rpcstat->rpc_cots_server);
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 	clnt_clts_stats_fini(zoneid, &rpcstat->rpc_clts_client);
1180Sstevel@tonic-gate 	svc_clts_stats_fini(zoneid, &rpcstat->rpc_clts_server);
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	kmem_free(rpcstat, sizeof (*rpcstat));
1210Sstevel@tonic-gate }
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate void
mt_kstat_init(void)1240Sstevel@tonic-gate mt_kstat_init(void)
1250Sstevel@tonic-gate {
1260Sstevel@tonic-gate 	zone_key_create(&rpcstat_zone_key, mt_kstat_zone_init, NULL,
1270Sstevel@tonic-gate 	    mt_kstat_zone_fini);
1280Sstevel@tonic-gate }
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate void
mt_kstat_fini(void)1310Sstevel@tonic-gate mt_kstat_fini(void)
1320Sstevel@tonic-gate {
1330Sstevel@tonic-gate 	(void) zone_key_delete(rpcstat_zone_key);
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate static bool_t	clnt_xid_initialized = FALSE;
1370Sstevel@tonic-gate static uint32_t clnt_xid = 0;	/* transaction id used by all clients */
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate uint32_t
alloc_xid(void)1400Sstevel@tonic-gate alloc_xid(void)
1410Sstevel@tonic-gate {
1420Sstevel@tonic-gate 	uint32_t  xid;
1430Sstevel@tonic-gate 	timestruc_t now;
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	/*
1460Sstevel@tonic-gate 	 * Do a one time initialzation to better utilize the number
1470Sstevel@tonic-gate 	 * space.
1480Sstevel@tonic-gate 	 */
1490Sstevel@tonic-gate 	mutex_enter(&xid_lock);
1500Sstevel@tonic-gate 	if (clnt_xid_initialized == FALSE) {
1510Sstevel@tonic-gate 		clnt_xid_initialized = TRUE;
1520Sstevel@tonic-gate 		gethrestime(&now);
1530Sstevel@tonic-gate 		clnt_xid = (uint32_t)((now.tv_sec << 20) |
1540Sstevel@tonic-gate 		    (now.tv_nsec >> 10));
1550Sstevel@tonic-gate 	}
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 	xid = clnt_xid++;
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	/*
1600Sstevel@tonic-gate 	 * Don't return a zero xid.  This could happen if the initialization
1610Sstevel@tonic-gate 	 * happens to return zero or if clnt_xid wraps.
1620Sstevel@tonic-gate 	 */
1630Sstevel@tonic-gate 	if (xid == 0)
1640Sstevel@tonic-gate 		xid = clnt_xid++;
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 	mutex_exit(&xid_lock);
1670Sstevel@tonic-gate 	return (xid);
1680Sstevel@tonic-gate }
169766Scarlsonj 
170766Scarlsonj /*
171766Scarlsonj  * These functions are temporary and designed for the upgrade-workaround only.
172766Scarlsonj  * They cannot be used for general zone-crossing RPC client support, and will
173766Scarlsonj  * be removed shortly.
174*6403Sgt29601  *
175*6403Sgt29601  * Currently these functions route all nfs global clients to the global zone.
176*6403Sgt29601  * When this upgrade-workaround is removed these function should return the
177*6403Sgt29601  * correct zone or their calls should be changed (rpc_zone() to curproc->p_zone
178*6403Sgt29601  * and rpc_zoneid() to getzoneid()).
179766Scarlsonj  */
180766Scarlsonj struct zone *
rpc_zone(void)181766Scarlsonj rpc_zone(void)
182766Scarlsonj {
183766Scarlsonj 	return (nfs_global_client_only != 0 ? global_zone : curproc->p_zone);
184766Scarlsonj }
185766Scarlsonj 
186766Scarlsonj zoneid_t
rpc_zoneid(void)187766Scarlsonj rpc_zoneid(void)
188766Scarlsonj {
189766Scarlsonj 	return (nfs_global_client_only != 0 ? GLOBAL_ZONEID : getzoneid());
190766Scarlsonj }
191