xref: /onnv-gate/usr/src/uts/common/rpc/svc_gen.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  *  Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  *  Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28*0Sstevel@tonic-gate /*	  All Rights Reserved	*/
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate /*
31*0Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
32*0Sstevel@tonic-gate  * under license from the Regents of the University of California.
33*0Sstevel@tonic-gate  */
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate #include <sys/types.h>
38*0Sstevel@tonic-gate #include <sys/sysmacros.h>
39*0Sstevel@tonic-gate #include <sys/param.h>
40*0Sstevel@tonic-gate #include <sys/cmn_err.h>
41*0Sstevel@tonic-gate #include <sys/debug.h>
42*0Sstevel@tonic-gate #include <rpc/types.h>
43*0Sstevel@tonic-gate #include <netinet/in.h>
44*0Sstevel@tonic-gate #include <rpc/auth.h>
45*0Sstevel@tonic-gate #include <rpc/clnt.h>
46*0Sstevel@tonic-gate #include <sys/tiuser.h>
47*0Sstevel@tonic-gate #include <sys/t_kuser.h>
48*0Sstevel@tonic-gate #include <rpc/svc.h>
49*0Sstevel@tonic-gate #include <sys/file.h>
50*0Sstevel@tonic-gate #include <sys/user.h>
51*0Sstevel@tonic-gate #include <sys/stream.h>
52*0Sstevel@tonic-gate #include <sys/strsubr.h>
53*0Sstevel@tonic-gate #include <sys/stropts.h>
54*0Sstevel@tonic-gate #include <sys/tihdr.h>
55*0Sstevel@tonic-gate #include <sys/timod.h>
56*0Sstevel@tonic-gate #include <sys/sunddi.h>
57*0Sstevel@tonic-gate #include <sys/fcntl.h>
58*0Sstevel@tonic-gate #include <sys/errno.h>
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate /*
61*0Sstevel@tonic-gate  * Create server-side kernel RPC `master' transport handle
62*0Sstevel@tonic-gate  *
63*0Sstevel@tonic-gate  * This is public interface for creation of a server RPC transport handle
64*0Sstevel@tonic-gate  * for a given file descriptor. This function is called from nfs_svc()
65*0Sstevel@tonic-gate  * and lm_svc().
66*0Sstevel@tonic-gate  *
67*0Sstevel@tonic-gate  * PSARC 2003/523 Contract Private Interface
68*0Sstevel@tonic-gate  * svc_tli_kcreate
69*0Sstevel@tonic-gate  * Changes must be reviewed by Solaris File Sharing
70*0Sstevel@tonic-gate  * Changes must be communicated to contract-2003-523@sun.com
71*0Sstevel@tonic-gate  *
72*0Sstevel@tonic-gate  * Arguments:
73*0Sstevel@tonic-gate  * - fp		 - connection end point
74*0Sstevel@tonic-gate  * - max_msgsize - max receive size
75*0Sstevel@tonic-gate  * - netid	 - netid
76*0Sstevel@tonic-gate  * - addrmask	 - address mask
77*0Sstevel@tonic-gate  * - nxprt       - filled with outgoing transport handle
78*0Sstevel@tonic-gate  * - sct	 - callout table to be registered with this transport handle
79*0Sstevel@tonic-gate  * - closeproc	 - optional pointer to a closeproc for this transport or NULL
80*0Sstevel@tonic-gate  * - id	         - RPC pool id (currently only NFS_SVCPOOL_ID or LM_SVCPOOL_ID)
81*0Sstevel@tonic-gate  * - hotstream	 - very MT-hot flag (TRUE for NFS, FALSE for Lock Manager)
82*0Sstevel@tonic-gate  *
83*0Sstevel@tonic-gate  * Description:
84*0Sstevel@tonic-gate  * - make sure rpcmod is on the stream
85*0Sstevel@tonic-gate  * - call T_INFO_REQ to get the transport service type info
86*0Sstevel@tonic-gate  * - call transport-type specific `create' routine (svc_clts_kcreate(),
87*0Sstevel@tonic-gate  *   svc_cots_kcreate()) to create and initialize transport for the stream
88*0Sstevel@tonic-gate  * - call svc_xprt_register() to register the transport handle into the
89*0Sstevel@tonic-gate  *   service thread pool
90*0Sstevel@tonic-gate  * - initialize transport-type independent fields (synchronization objects,
91*0Sstevel@tonic-gate  *   thread counts, callout table, closeproc)
92*0Sstevel@tonic-gate  * - optionally, for CLTS transports tell streams framework that the
93*0Sstevel@tonic-gate  *   stream can be MT-hot
94*0Sstevel@tonic-gate  * - call transport-type specific `start' function to tell rpcmod that
95*0Sstevel@tonic-gate  *   the transport is ready to receive.
96*0Sstevel@tonic-gate  */
97*0Sstevel@tonic-gate int
svc_tli_kcreate(struct file * fp,uint_t max_msgsize,char * netid,struct netbuf * addrmask,SVCMASTERXPRT ** nxprt,SVC_CALLOUT_TABLE * sct,void (* closeproc)(const SVCMASTERXPRT *),int id,bool_t hotstream)98*0Sstevel@tonic-gate svc_tli_kcreate(
99*0Sstevel@tonic-gate 	struct file	*fp,		/* connection end point */
100*0Sstevel@tonic-gate 	uint_t		max_msgsize,	/* max receive size */
101*0Sstevel@tonic-gate 	char		*netid,
102*0Sstevel@tonic-gate 	struct netbuf	*addrmask,
103*0Sstevel@tonic-gate 	SVCMASTERXPRT	**nxprt,
104*0Sstevel@tonic-gate 	SVC_CALLOUT_TABLE *sct,
105*0Sstevel@tonic-gate 	void		(*closeproc)(const SVCMASTERXPRT *),
106*0Sstevel@tonic-gate 	int		id,		/* thread pool  */
107*0Sstevel@tonic-gate 	bool_t		hotstream)
108*0Sstevel@tonic-gate {
109*0Sstevel@tonic-gate 	queue_t		*wq;
110*0Sstevel@tonic-gate 	SVCMASTERXPRT	*xprt = NULL;	/* service handle */
111*0Sstevel@tonic-gate 	int		retval;
112*0Sstevel@tonic-gate 	struct strioctl strioc;
113*0Sstevel@tonic-gate 	struct T_info_ack tinfo;
114*0Sstevel@tonic-gate 	int		error;
115*0Sstevel@tonic-gate 	void		**vp;
116*0Sstevel@tonic-gate 	major_t		udpmaj;
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate 	RPCLOG(16, "svc_tli_kcreate: on file %p\n", (void *)fp);
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate 	if (fp == NULL || nxprt == NULL)
121*0Sstevel@tonic-gate 		return (EINVAL);
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate 	if (fp->f_vnode->v_stream == NULL)
124*0Sstevel@tonic-gate 		return (ENOSTR);
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate 	/*
127*0Sstevel@tonic-gate 	 * Make sure that an RPC interface module is on the stream.
128*0Sstevel@tonic-gate 	 */
129*0Sstevel@tonic-gate 	wq = fp->f_vnode->v_stream->sd_wrq;
130*0Sstevel@tonic-gate 	while ((wq = wq->q_next) != NULL) {
131*0Sstevel@tonic-gate 		if (strcmp(wq->q_qinfo->qi_minfo->mi_idname, "rpcmod") == 0)
132*0Sstevel@tonic-gate 			break;
133*0Sstevel@tonic-gate 	}
134*0Sstevel@tonic-gate 	if (!wq) {
135*0Sstevel@tonic-gate 		RPCLOG0(1, "svc_tli_kcreate: no RPC module on stream\n");
136*0Sstevel@tonic-gate 		return (EINVAL);
137*0Sstevel@tonic-gate 	}
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate 	/*
140*0Sstevel@tonic-gate 	 * Find out what type of transport this is.
141*0Sstevel@tonic-gate 	 */
142*0Sstevel@tonic-gate 	strioc.ic_cmd = TI_GETINFO;
143*0Sstevel@tonic-gate 	strioc.ic_timout = -1;
144*0Sstevel@tonic-gate 	strioc.ic_len = sizeof (tinfo);
145*0Sstevel@tonic-gate 	strioc.ic_dp = (char *)&tinfo;
146*0Sstevel@tonic-gate 	tinfo.PRIM_type = T_INFO_REQ;
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 	error = strioctl(fp->f_vnode, I_STR, (intptr_t)&strioc, 0, K_TO_K,
149*0Sstevel@tonic-gate 	    CRED(), &retval);
150*0Sstevel@tonic-gate 	if (error || retval) {
151*0Sstevel@tonic-gate 		RPCLOG(1, "svc_tli_kcreate: getinfo ioctl: %d\n", error);
152*0Sstevel@tonic-gate 		return (error);
153*0Sstevel@tonic-gate 	}
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate 	/*
156*0Sstevel@tonic-gate 	 * Call transport-type specific `create' function.
157*0Sstevel@tonic-gate 	 * It will allocate transport structure.
158*0Sstevel@tonic-gate 	 */
159*0Sstevel@tonic-gate 	switch (tinfo.SERV_type) {
160*0Sstevel@tonic-gate 	case T_CLTS:
161*0Sstevel@tonic-gate 		error = svc_clts_kcreate(fp, max_msgsize, &tinfo, &xprt);
162*0Sstevel@tonic-gate 		break;
163*0Sstevel@tonic-gate 	case T_COTS:
164*0Sstevel@tonic-gate 	case T_COTS_ORD:
165*0Sstevel@tonic-gate 		error = svc_cots_kcreate(fp, max_msgsize, &tinfo, &xprt);
166*0Sstevel@tonic-gate 		break;
167*0Sstevel@tonic-gate 	default:
168*0Sstevel@tonic-gate 		RPCLOG(1, "svc_tli_kcreate: Bad service type %d\n",
169*0Sstevel@tonic-gate 		    tinfo.SERV_type);
170*0Sstevel@tonic-gate 		error = EINVAL;
171*0Sstevel@tonic-gate 	}
172*0Sstevel@tonic-gate 	if (error)
173*0Sstevel@tonic-gate 		return (error);
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate 	/*
176*0Sstevel@tonic-gate 	 * Initialize transport-type independent fields.
177*0Sstevel@tonic-gate 	 */
178*0Sstevel@tonic-gate 	xprt->xp_req_head = (mblk_t *)0;
179*0Sstevel@tonic-gate 	xprt->xp_req_tail = (mblk_t *)0;
180*0Sstevel@tonic-gate 	mutex_init(&xprt->xp_req_lock, NULL, MUTEX_DEFAULT, NULL);
181*0Sstevel@tonic-gate 	mutex_init(&xprt->xp_thread_lock, NULL, MUTEX_DEFAULT, NULL);
182*0Sstevel@tonic-gate 	xprt->xp_type = tinfo.SERV_type;
183*0Sstevel@tonic-gate 	xprt->xp_threads = 0;
184*0Sstevel@tonic-gate 	xprt->xp_detached_threads = 0;
185*0Sstevel@tonic-gate 	xprt->xp_fp = fp;
186*0Sstevel@tonic-gate 	xprt->xp_wq = wq;
187*0Sstevel@tonic-gate 	xprt->xp_closeproc = closeproc;
188*0Sstevel@tonic-gate 	xprt->xp_sct = sct;
189*0Sstevel@tonic-gate 	xprt->xp_netid = NULL;
190*0Sstevel@tonic-gate 	if (netid != NULL) {
191*0Sstevel@tonic-gate 		xprt->xp_netid = kmem_alloc(strlen(netid) + 1, KM_SLEEP);
192*0Sstevel@tonic-gate 		(void) strcpy(xprt->xp_netid, netid);
193*0Sstevel@tonic-gate 	}
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate 	xprt->xp_addrmask.len = 0;
196*0Sstevel@tonic-gate 	xprt->xp_addrmask.maxlen = 0;
197*0Sstevel@tonic-gate 	xprt->xp_addrmask.buf = NULL;
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate 	if (addrmask != NULL) {
200*0Sstevel@tonic-gate 		xprt->xp_addrmask = *addrmask;
201*0Sstevel@tonic-gate 	}
202*0Sstevel@tonic-gate 
203*0Sstevel@tonic-gate 	/*
204*0Sstevel@tonic-gate 	 * Register this transport handle after all fields have been
205*0Sstevel@tonic-gate 	 * initialized. The registration can fail only if we try to register
206*0Sstevel@tonic-gate 	 * with a non-existent pool (ENOENT) or a closing pool (EBUSY).
207*0Sstevel@tonic-gate 	 */
208*0Sstevel@tonic-gate 	if (error = svc_xprt_register(xprt, id)) {
209*0Sstevel@tonic-gate 		/* if there was an addrmask, caller will delete it */
210*0Sstevel@tonic-gate 		xprt->xp_addrmask.maxlen = 0;
211*0Sstevel@tonic-gate 		SVC_DESTROY(xprt);
212*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "svc_tli_kcreate: xprt_register failed");
213*0Sstevel@tonic-gate 
214*0Sstevel@tonic-gate 		return (error);
215*0Sstevel@tonic-gate 	}
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate 	/*
218*0Sstevel@tonic-gate 	 * Set the private RPC cell in the module's data.
219*0Sstevel@tonic-gate 	 */
220*0Sstevel@tonic-gate 	vp = (void **)wq->q_ptr;
221*0Sstevel@tonic-gate 	vp[0] = xprt;
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate 	/*
224*0Sstevel@tonic-gate 	 * Inform the streams framework that the stream may be very MT hot.
225*0Sstevel@tonic-gate 	 */
226*0Sstevel@tonic-gate 	if (hotstream && tinfo.SERV_type == T_CLTS) {
227*0Sstevel@tonic-gate 		udpmaj = ddi_name_to_major("udp");
228*0Sstevel@tonic-gate 		if (udpmaj != (major_t)-1 &&
229*0Sstevel@tonic-gate 			getmajor(fp->f_vnode->v_rdev) == udpmaj)
230*0Sstevel@tonic-gate 			create_putlocks(wq, 1);
231*0Sstevel@tonic-gate 	}
232*0Sstevel@tonic-gate 
233*0Sstevel@tonic-gate 	*nxprt = xprt;
234*0Sstevel@tonic-gate 
235*0Sstevel@tonic-gate 	/*
236*0Sstevel@tonic-gate 	 * Tell rpcmod that the transport is fully initialized and
237*0Sstevel@tonic-gate 	 * ready to process requests.
238*0Sstevel@tonic-gate 	 */
239*0Sstevel@tonic-gate 	SVC_START(xprt);
240*0Sstevel@tonic-gate 
241*0Sstevel@tonic-gate 	return (0);
242*0Sstevel@tonic-gate }
243