xref: /illumos-gate/usr/src/uts/common/rpc/mt_rpcinit.c (revision 2d6eb4a5e0a47d30189497241345dc5466bb68ab)
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
5d624471bSelowe  * Common Development and Distribution License (the "License").
6d624471bSelowe  * 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 /*
22*8ffff9fdSgt29601  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * Define and initialize MT client/server data.
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include	<sys/types.h>
357c478bd9Sstevel@tonic-gate #include	<sys/t_lock.h>
367c478bd9Sstevel@tonic-gate #include	<sys/kstat.h>
377c478bd9Sstevel@tonic-gate #include	<sys/systm.h>
387c478bd9Sstevel@tonic-gate #include	<sys/zone.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #include	<rpc/types.h>
417c478bd9Sstevel@tonic-gate #include	<rpc/auth.h>
427c478bd9Sstevel@tonic-gate #include	<rpc/clnt.h>
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate kmutex_t xid_lock;		/* XID allocation */
457c478bd9Sstevel@tonic-gate kmutex_t clnt_pending_lock;	/* for list of pending calls awaiting replies */
467c478bd9Sstevel@tonic-gate kmutex_t clnt_max_msg_lock;	/* updating max message sanity check for cots */
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate zone_key_t	rpcstat_zone_key;
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate /*
517c478bd9Sstevel@tonic-gate  * rpcstat_zone_[init|fini]_common() ends up being nearly identical to
527c478bd9Sstevel@tonic-gate  * nfsstat_zone_[init|fini]_common().  Due to them necessarily being in
537c478bd9Sstevel@tonic-gate  * different modules, however, we end up needing to duplicate the code.
547c478bd9Sstevel@tonic-gate  */
557c478bd9Sstevel@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)567c478bd9Sstevel@tonic-gate rpcstat_zone_init_common(zoneid_t zoneid, const char *module, const char *name,
577c478bd9Sstevel@tonic-gate     const kstat_named_t *template, size_t template_size)
587c478bd9Sstevel@tonic-gate {
597c478bd9Sstevel@tonic-gate 	kstat_t *ksp;
607c478bd9Sstevel@tonic-gate 	kstat_named_t *ks_data;
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate /*
647c478bd9Sstevel@tonic-gate  * PSARC 2001/697 Contract Private Interface
657c478bd9Sstevel@tonic-gate  * rpc_clts_client
667c478bd9Sstevel@tonic-gate  * rpc_cots_client
677c478bd9Sstevel@tonic-gate  * Changes must be reviewed by Solaris File Sharing
687c478bd9Sstevel@tonic-gate  * Changes must be communicated to contract-2001-697@sun.com
697c478bd9Sstevel@tonic-gate  *
707c478bd9Sstevel@tonic-gate  */
717c478bd9Sstevel@tonic-gate 	ks_data = kmem_alloc(template_size, KM_SLEEP);
727c478bd9Sstevel@tonic-gate 	bcopy(template, ks_data, template_size);
73d624471bSelowe 	if ((ksp = kstat_create_zone(module, 0, name, "rpc",
747c478bd9Sstevel@tonic-gate 	    KSTAT_TYPE_NAMED, template_size / sizeof (kstat_named_t),
757c478bd9Sstevel@tonic-gate 	    KSTAT_FLAG_VIRTUAL | KSTAT_FLAG_WRITABLE, zoneid)) != NULL) {
767c478bd9Sstevel@tonic-gate 		ksp->ks_data = ks_data;
777c478bd9Sstevel@tonic-gate 		kstat_install(ksp);
787c478bd9Sstevel@tonic-gate 	}
797c478bd9Sstevel@tonic-gate 	return (ks_data);
807c478bd9Sstevel@tonic-gate }
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate void
rpcstat_zone_fini_common(zoneid_t zoneid,const char * module,const char * name)837c478bd9Sstevel@tonic-gate rpcstat_zone_fini_common(zoneid_t zoneid, const char *module, const char *name)
847c478bd9Sstevel@tonic-gate {
85d624471bSelowe 	kstat_delete_byname_zone(module, 0, name, zoneid);
867c478bd9Sstevel@tonic-gate }
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate static void *
mt_kstat_zone_init(zoneid_t zoneid)897c478bd9Sstevel@tonic-gate mt_kstat_zone_init(zoneid_t zoneid)
907c478bd9Sstevel@tonic-gate {
917c478bd9Sstevel@tonic-gate 	struct rpcstat *rpcstat;
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	rpcstat = kmem_alloc(sizeof (*rpcstat), KM_SLEEP);
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	clnt_clts_stats_init(zoneid, &rpcstat->rpc_clts_client);
967c478bd9Sstevel@tonic-gate 	svc_clts_stats_init(zoneid, &rpcstat->rpc_clts_server);
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	clnt_cots_stats_init(zoneid, &rpcstat->rpc_cots_client);
997c478bd9Sstevel@tonic-gate 	svc_cots_stats_init(zoneid, &rpcstat->rpc_cots_server);
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	return (rpcstat);
1027c478bd9Sstevel@tonic-gate }
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate /*
1057c478bd9Sstevel@tonic-gate  * Deletes the previously allocated "rpc" kstats
1067c478bd9Sstevel@tonic-gate  */
1077c478bd9Sstevel@tonic-gate static void
mt_kstat_zone_fini(zoneid_t zoneid,void * data)1087c478bd9Sstevel@tonic-gate mt_kstat_zone_fini(zoneid_t zoneid, void *data)
1097c478bd9Sstevel@tonic-gate {
1107c478bd9Sstevel@tonic-gate 	struct rpcstat *rpcstat = data;
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	clnt_cots_stats_fini(zoneid, &rpcstat->rpc_cots_client);
1137c478bd9Sstevel@tonic-gate 	svc_cots_stats_fini(zoneid, &rpcstat->rpc_cots_server);
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	clnt_clts_stats_fini(zoneid, &rpcstat->rpc_clts_client);
1167c478bd9Sstevel@tonic-gate 	svc_clts_stats_fini(zoneid, &rpcstat->rpc_clts_server);
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	kmem_free(rpcstat, sizeof (*rpcstat));
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate void
mt_kstat_init(void)1227c478bd9Sstevel@tonic-gate mt_kstat_init(void)
1237c478bd9Sstevel@tonic-gate {
1247c478bd9Sstevel@tonic-gate 	zone_key_create(&rpcstat_zone_key, mt_kstat_zone_init, NULL,
1257c478bd9Sstevel@tonic-gate 	    mt_kstat_zone_fini);
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate void
mt_kstat_fini(void)1297c478bd9Sstevel@tonic-gate mt_kstat_fini(void)
1307c478bd9Sstevel@tonic-gate {
1317c478bd9Sstevel@tonic-gate 	(void) zone_key_delete(rpcstat_zone_key);
1327c478bd9Sstevel@tonic-gate }
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate static bool_t	clnt_xid_initialized = FALSE;
1357c478bd9Sstevel@tonic-gate static uint32_t clnt_xid = 0;	/* transaction id used by all clients */
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate uint32_t
alloc_xid(void)1387c478bd9Sstevel@tonic-gate alloc_xid(void)
1397c478bd9Sstevel@tonic-gate {
1407c478bd9Sstevel@tonic-gate 	uint32_t  xid;
1417c478bd9Sstevel@tonic-gate 	timestruc_t now;
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	/*
1447c478bd9Sstevel@tonic-gate 	 * Do a one time initialzation to better utilize the number
1457c478bd9Sstevel@tonic-gate 	 * space.
1467c478bd9Sstevel@tonic-gate 	 */
1477c478bd9Sstevel@tonic-gate 	mutex_enter(&xid_lock);
1487c478bd9Sstevel@tonic-gate 	if (clnt_xid_initialized == FALSE) {
1497c478bd9Sstevel@tonic-gate 		clnt_xid_initialized = TRUE;
1507c478bd9Sstevel@tonic-gate 		gethrestime(&now);
1517c478bd9Sstevel@tonic-gate 		clnt_xid = (uint32_t)((now.tv_sec << 20) |
1527c478bd9Sstevel@tonic-gate 		    (now.tv_nsec >> 10));
1537c478bd9Sstevel@tonic-gate 	}
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	xid = clnt_xid++;
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	/*
1587c478bd9Sstevel@tonic-gate 	 * Don't return a zero xid.  This could happen if the initialization
1597c478bd9Sstevel@tonic-gate 	 * happens to return zero or if clnt_xid wraps.
1607c478bd9Sstevel@tonic-gate 	 */
1617c478bd9Sstevel@tonic-gate 	if (xid == 0)
1627c478bd9Sstevel@tonic-gate 		xid = clnt_xid++;
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	mutex_exit(&xid_lock);
1657c478bd9Sstevel@tonic-gate 	return (xid);
1667c478bd9Sstevel@tonic-gate }
167108322fbScarlsonj 
168108322fbScarlsonj /*
169108322fbScarlsonj  * These functions are temporary and designed for the upgrade-workaround only.
170108322fbScarlsonj  * They cannot be used for general zone-crossing RPC client support, and will
171108322fbScarlsonj  * be removed shortly.
172*8ffff9fdSgt29601  *
173*8ffff9fdSgt29601  * Currently these functions route all nfs global clients to the global zone.
174*8ffff9fdSgt29601  * When this upgrade-workaround is removed these function should return the
175*8ffff9fdSgt29601  * correct zone or their calls should be changed (rpc_zone() to curproc->p_zone
176*8ffff9fdSgt29601  * and rpc_zoneid() to getzoneid()).
177108322fbScarlsonj  */
178108322fbScarlsonj struct zone *
rpc_zone(void)179108322fbScarlsonj rpc_zone(void)
180108322fbScarlsonj {
181108322fbScarlsonj 	return (nfs_global_client_only != 0 ? global_zone : curproc->p_zone);
182108322fbScarlsonj }
183108322fbScarlsonj 
184108322fbScarlsonj zoneid_t
rpc_zoneid(void)185108322fbScarlsonj rpc_zoneid(void)
186108322fbScarlsonj {
187108322fbScarlsonj 	return (nfs_global_client_only != 0 ? GLOBAL_ZONEID : getzoneid());
188108322fbScarlsonj }
189