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 /*
38*0Sstevel@tonic-gate  * svc_cots.c
39*0Sstevel@tonic-gate  * Server side for connection-oriented RPC in the kernel.
40*0Sstevel@tonic-gate  *
41*0Sstevel@tonic-gate  */
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate #include <sys/param.h>
44*0Sstevel@tonic-gate #include <sys/types.h>
45*0Sstevel@tonic-gate #include <sys/sysmacros.h>
46*0Sstevel@tonic-gate #include <sys/file.h>
47*0Sstevel@tonic-gate #include <sys/stream.h>
48*0Sstevel@tonic-gate #include <sys/strsubr.h>
49*0Sstevel@tonic-gate #include <sys/strsun.h>
50*0Sstevel@tonic-gate #include <sys/stropts.h>
51*0Sstevel@tonic-gate #include <sys/tiuser.h>
52*0Sstevel@tonic-gate #include <sys/timod.h>
53*0Sstevel@tonic-gate #include <sys/tihdr.h>
54*0Sstevel@tonic-gate #include <sys/fcntl.h>
55*0Sstevel@tonic-gate #include <sys/errno.h>
56*0Sstevel@tonic-gate #include <sys/kmem.h>
57*0Sstevel@tonic-gate #include <sys/systm.h>
58*0Sstevel@tonic-gate #include <sys/debug.h>
59*0Sstevel@tonic-gate #include <sys/cmn_err.h>
60*0Sstevel@tonic-gate #include <sys/kstat.h>
61*0Sstevel@tonic-gate #include <sys/vtrace.h>
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate #include <rpc/types.h>
64*0Sstevel@tonic-gate #include <rpc/xdr.h>
65*0Sstevel@tonic-gate #include <rpc/auth.h>
66*0Sstevel@tonic-gate #include <rpc/rpc_msg.h>
67*0Sstevel@tonic-gate #include <rpc/svc.h>
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate #define	COTS_MAX_ALLOCSIZE	2048
70*0Sstevel@tonic-gate #define	MSG_OFFSET		128	/* offset of call into the mblk */
71*0Sstevel@tonic-gate #define	RM_HDR_SIZE		4	/* record mark header size */
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate /*
74*0Sstevel@tonic-gate  * Routines exported through ops vector.
75*0Sstevel@tonic-gate  */
76*0Sstevel@tonic-gate static bool_t		svc_cots_krecv(SVCXPRT *, mblk_t *, struct rpc_msg *);
77*0Sstevel@tonic-gate static bool_t		svc_cots_ksend(SVCXPRT *, struct rpc_msg *);
78*0Sstevel@tonic-gate static bool_t		svc_cots_kgetargs(SVCXPRT *, xdrproc_t, caddr_t);
79*0Sstevel@tonic-gate static bool_t		svc_cots_kfreeargs(SVCXPRT *, xdrproc_t, caddr_t);
80*0Sstevel@tonic-gate static void		svc_cots_kdestroy(SVCMASTERXPRT *);
81*0Sstevel@tonic-gate static int		svc_cots_kdup(struct svc_req *, caddr_t, int,
82*0Sstevel@tonic-gate 				struct dupreq **, bool_t *);
83*0Sstevel@tonic-gate static void		svc_cots_kdupdone(struct dupreq *, caddr_t,
84*0Sstevel@tonic-gate 				void (*)(), int, int);
85*0Sstevel@tonic-gate static int32_t		*svc_cots_kgetres(SVCXPRT *, int);
86*0Sstevel@tonic-gate static void		svc_cots_kfreeres(SVCXPRT *);
87*0Sstevel@tonic-gate static void		svc_cots_kclone_destroy(SVCXPRT *);
88*0Sstevel@tonic-gate static void		svc_cots_kstart(SVCMASTERXPRT *);
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate /*
91*0Sstevel@tonic-gate  * Server transport operations vector.
92*0Sstevel@tonic-gate  */
93*0Sstevel@tonic-gate struct svc_ops svc_cots_op = {
94*0Sstevel@tonic-gate 	svc_cots_krecv,		/* Get requests */
95*0Sstevel@tonic-gate 	svc_cots_kgetargs,	/* Deserialize arguments */
96*0Sstevel@tonic-gate 	svc_cots_ksend,		/* Send reply */
97*0Sstevel@tonic-gate 	svc_cots_kfreeargs,	/* Free argument data space */
98*0Sstevel@tonic-gate 	svc_cots_kdestroy,	/* Destroy transport handle */
99*0Sstevel@tonic-gate 	svc_cots_kdup,		/* Check entry in dup req cache */
100*0Sstevel@tonic-gate 	svc_cots_kdupdone,	/* Mark entry in dup req cache as done */
101*0Sstevel@tonic-gate 	svc_cots_kgetres,	/* Get pointer to response buffer */
102*0Sstevel@tonic-gate 	svc_cots_kfreeres,	/* Destroy pre-serialized response header */
103*0Sstevel@tonic-gate 	svc_cots_kclone_destroy, /* Destroy a clone xprt */
104*0Sstevel@tonic-gate 	svc_cots_kstart		/* Tell `ready-to-receive' to rpcmod */
105*0Sstevel@tonic-gate };
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate /*
108*0Sstevel@tonic-gate  * Master transport private data.
109*0Sstevel@tonic-gate  * Kept in xprt->xp_p2.
110*0Sstevel@tonic-gate  */
111*0Sstevel@tonic-gate struct cots_master_data {
112*0Sstevel@tonic-gate 	char	*cmd_src_addr;	/* client's address */
113*0Sstevel@tonic-gate 	int	cmd_xprt_started; /* flag for clone routine to call */
114*0Sstevel@tonic-gate 				/* rpcmod's start routine. */
115*0Sstevel@tonic-gate 	struct rpc_cots_server *cmd_stats;	/* stats for zone */
116*0Sstevel@tonic-gate };
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate /*
119*0Sstevel@tonic-gate  * Transport private data.
120*0Sstevel@tonic-gate  * Kept in clone_xprt->xp_p2buf.
121*0Sstevel@tonic-gate  */
122*0Sstevel@tonic-gate typedef struct cots_data {
123*0Sstevel@tonic-gate 	mblk_t	*cd_mp;		/* pre-allocated reply message */
124*0Sstevel@tonic-gate 	mblk_t	*cd_req_mp;	/* request message */
125*0Sstevel@tonic-gate } cots_data_t;
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate /*
128*0Sstevel@tonic-gate  * Server statistics
129*0Sstevel@tonic-gate  * NOTE: This structure type is duplicated in the NFS fast path.
130*0Sstevel@tonic-gate  */
131*0Sstevel@tonic-gate static const struct rpc_cots_server {
132*0Sstevel@tonic-gate 	kstat_named_t	rscalls;
133*0Sstevel@tonic-gate 	kstat_named_t	rsbadcalls;
134*0Sstevel@tonic-gate 	kstat_named_t	rsnullrecv;
135*0Sstevel@tonic-gate 	kstat_named_t	rsbadlen;
136*0Sstevel@tonic-gate 	kstat_named_t	rsxdrcall;
137*0Sstevel@tonic-gate 	kstat_named_t	rsdupchecks;
138*0Sstevel@tonic-gate 	kstat_named_t	rsdupreqs;
139*0Sstevel@tonic-gate } cots_rsstat_tmpl = {
140*0Sstevel@tonic-gate 	{ "calls",	KSTAT_DATA_UINT64 },
141*0Sstevel@tonic-gate 	{ "badcalls",	KSTAT_DATA_UINT64 },
142*0Sstevel@tonic-gate 	{ "nullrecv",	KSTAT_DATA_UINT64 },
143*0Sstevel@tonic-gate 	{ "badlen",	KSTAT_DATA_UINT64 },
144*0Sstevel@tonic-gate 	{ "xdrcall",	KSTAT_DATA_UINT64 },
145*0Sstevel@tonic-gate 	{ "dupchecks",	KSTAT_DATA_UINT64 },
146*0Sstevel@tonic-gate 	{ "dupreqs",	KSTAT_DATA_UINT64 }
147*0Sstevel@tonic-gate };
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate #define	CLONE2STATS(clone_xprt)	\
150*0Sstevel@tonic-gate 	((struct cots_master_data *)(clone_xprt)->xp_master->xp_p2)->cmd_stats
151*0Sstevel@tonic-gate #define	RSSTAT_INCR(s, x)	\
152*0Sstevel@tonic-gate 	atomic_add_64(&(s)->x.value.ui64, 1)
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate /*
155*0Sstevel@tonic-gate  * Pointer to a transport specific `ready to receive' function in rpcmod
156*0Sstevel@tonic-gate  * (set from rpcmod).
157*0Sstevel@tonic-gate  */
158*0Sstevel@tonic-gate void    (*mir_start)(queue_t *);
159*0Sstevel@tonic-gate uint_t	*svc_max_msg_sizep;
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate /*
162*0Sstevel@tonic-gate  * the address size of the underlying transport can sometimes be
163*0Sstevel@tonic-gate  * unknown (tinfo->ADDR_size == -1).  For this case, it is
164*0Sstevel@tonic-gate  * necessary to figure out what the size is so the correct amount
165*0Sstevel@tonic-gate  * of data is allocated.  This is an itterative process:
166*0Sstevel@tonic-gate  *	1. take a good guess (use T_MINADDRSIZE)
167*0Sstevel@tonic-gate  *	2. try it.
168*0Sstevel@tonic-gate  *	3. if it works then everything is ok
169*0Sstevel@tonic-gate  *	4. if the error is ENAMETOLONG, double the guess
170*0Sstevel@tonic-gate  *	5. go back to step 2.
171*0Sstevel@tonic-gate  */
172*0Sstevel@tonic-gate #define	T_UNKNOWNADDRSIZE	(-1)
173*0Sstevel@tonic-gate #define	T_MINADDRSIZE	32
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate /*
176*0Sstevel@tonic-gate  * Create a transport record.
177*0Sstevel@tonic-gate  * The transport record, output buffer, and private data structure
178*0Sstevel@tonic-gate  * are allocated.  The output buffer is serialized into using xdrmem.
179*0Sstevel@tonic-gate  * There is one transport record per user process which implements a
180*0Sstevel@tonic-gate  * set of services.
181*0Sstevel@tonic-gate  */
182*0Sstevel@tonic-gate static kmutex_t cots_kcreate_lock;
183*0Sstevel@tonic-gate 
184*0Sstevel@tonic-gate int
185*0Sstevel@tonic-gate svc_cots_kcreate(file_t *fp, uint_t max_msgsize, struct T_info_ack *tinfo,
186*0Sstevel@tonic-gate     SVCMASTERXPRT **nxprt)
187*0Sstevel@tonic-gate {
188*0Sstevel@tonic-gate 	struct cots_master_data *cmd;
189*0Sstevel@tonic-gate 	int err;
190*0Sstevel@tonic-gate 	int retval;
191*0Sstevel@tonic-gate 	SVCMASTERXPRT *xprt;
192*0Sstevel@tonic-gate 	int addr_size;
193*0Sstevel@tonic-gate 	struct rpcstat *rpcstat;
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate 	if (nxprt == NULL)
196*0Sstevel@tonic-gate 		return (EINVAL);
197*0Sstevel@tonic-gate 
198*0Sstevel@tonic-gate 	rpcstat = zone_getspecific(rpcstat_zone_key, curproc->p_zone);
199*0Sstevel@tonic-gate 	ASSERT(rpcstat != NULL);
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate 	addr_size = tinfo->ADDR_size;
202*0Sstevel@tonic-gate 	if (addr_size == T_UNKNOWNADDRSIZE) {
203*0Sstevel@tonic-gate 	    addr_size = T_MINADDRSIZE;
204*0Sstevel@tonic-gate 	}
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate allocate_space:
207*0Sstevel@tonic-gate 
208*0Sstevel@tonic-gate 	xprt = kmem_zalloc(sizeof (SVCMASTERXPRT), KM_SLEEP);
209*0Sstevel@tonic-gate 	cmd = kmem_zalloc(sizeof (*cmd) + addr_size, KM_SLEEP);
210*0Sstevel@tonic-gate 
211*0Sstevel@tonic-gate 	/* cd_src_addr is set to the end of cots_data_t struct */
212*0Sstevel@tonic-gate 	cmd->cmd_src_addr = (char *)&cmd[1];
213*0Sstevel@tonic-gate 
214*0Sstevel@tonic-gate 	if ((tinfo->TIDU_size > COTS_MAX_ALLOCSIZE) ||
215*0Sstevel@tonic-gate 	    (tinfo->TIDU_size <= 0))
216*0Sstevel@tonic-gate 		xprt->xp_msg_size = COTS_MAX_ALLOCSIZE;
217*0Sstevel@tonic-gate 	else {
218*0Sstevel@tonic-gate 		xprt->xp_msg_size = tinfo->TIDU_size -
219*0Sstevel@tonic-gate 			(tinfo->TIDU_size % BYTES_PER_XDR_UNIT);
220*0Sstevel@tonic-gate 	}
221*0Sstevel@tonic-gate 
222*0Sstevel@tonic-gate 	xprt->xp_ops = &svc_cots_op;
223*0Sstevel@tonic-gate 	xprt->xp_p2 = (caddr_t)cmd;
224*0Sstevel@tonic-gate 	cmd->cmd_xprt_started = 0;
225*0Sstevel@tonic-gate 	cmd->cmd_stats = rpcstat->rpc_cots_server;
226*0Sstevel@tonic-gate 
227*0Sstevel@tonic-gate 	xprt->xp_rtaddr.maxlen = addr_size;
228*0Sstevel@tonic-gate 	xprt->xp_rtaddr.len = 0;
229*0Sstevel@tonic-gate 	xprt->xp_rtaddr.buf = cmd->cmd_src_addr;
230*0Sstevel@tonic-gate 
231*0Sstevel@tonic-gate 	/*
232*0Sstevel@tonic-gate 	 * Get the address of the client for duplicate request
233*0Sstevel@tonic-gate 	 * cache processing. Note that the TI_GETPEERNAME ioctl should
234*0Sstevel@tonic-gate 	 * be replaced with a T_ADDR_REQ/T_ADDR_ACK handshake when
235*0Sstevel@tonic-gate 	 * TCP supports these standard TPI primitives.
236*0Sstevel@tonic-gate 	 */
237*0Sstevel@tonic-gate 	retval = 0;
238*0Sstevel@tonic-gate 	err = strioctl(fp->f_vnode, TI_GETPEERNAME,
239*0Sstevel@tonic-gate 	    (intptr_t)&xprt->xp_rtaddr, 0, K_TO_K, CRED(), &retval);
240*0Sstevel@tonic-gate 	if (err) {
241*0Sstevel@tonic-gate 		kmem_free(xprt, sizeof (SVCMASTERXPRT));
242*0Sstevel@tonic-gate 		kmem_free(cmd, sizeof (*cmd) + addr_size);
243*0Sstevel@tonic-gate 		if ((err == ENAMETOOLONG) &&
244*0Sstevel@tonic-gate 			(tinfo->ADDR_size == T_UNKNOWNADDRSIZE)) {
245*0Sstevel@tonic-gate 			addr_size *= 2;
246*0Sstevel@tonic-gate 			goto allocate_space;
247*0Sstevel@tonic-gate 		}
248*0Sstevel@tonic-gate 		return (err);
249*0Sstevel@tonic-gate 	}
250*0Sstevel@tonic-gate 
251*0Sstevel@tonic-gate 	/*
252*0Sstevel@tonic-gate 	 * If the current sanity check size in rpcmod is smaller
253*0Sstevel@tonic-gate 	 * than the size needed for this xprt, then increase
254*0Sstevel@tonic-gate 	 * the sanity check.
255*0Sstevel@tonic-gate 	 */
256*0Sstevel@tonic-gate 	if (max_msgsize != 0 && svc_max_msg_sizep &&
257*0Sstevel@tonic-gate 	    max_msgsize > *svc_max_msg_sizep) {
258*0Sstevel@tonic-gate 
259*0Sstevel@tonic-gate 		/* This check needs a lock */
260*0Sstevel@tonic-gate 		mutex_enter(&cots_kcreate_lock);
261*0Sstevel@tonic-gate 		if (svc_max_msg_sizep && max_msgsize > *svc_max_msg_sizep)
262*0Sstevel@tonic-gate 			*svc_max_msg_sizep = max_msgsize;
263*0Sstevel@tonic-gate 		mutex_exit(&cots_kcreate_lock);
264*0Sstevel@tonic-gate 	}
265*0Sstevel@tonic-gate 
266*0Sstevel@tonic-gate 	*nxprt = xprt;
267*0Sstevel@tonic-gate 	return (0);
268*0Sstevel@tonic-gate }
269*0Sstevel@tonic-gate 
270*0Sstevel@tonic-gate /*
271*0Sstevel@tonic-gate  * Destroy a master transport record.
272*0Sstevel@tonic-gate  * Frees the space allocated for a transport record.
273*0Sstevel@tonic-gate  */
274*0Sstevel@tonic-gate static void
275*0Sstevel@tonic-gate svc_cots_kdestroy(SVCMASTERXPRT *xprt)
276*0Sstevel@tonic-gate {
277*0Sstevel@tonic-gate 	struct cots_master_data *cmd = (struct cots_master_data *)xprt->xp_p2;
278*0Sstevel@tonic-gate 
279*0Sstevel@tonic-gate 	ASSERT(cmd);
280*0Sstevel@tonic-gate 
281*0Sstevel@tonic-gate 	if (xprt->xp_netid)
282*0Sstevel@tonic-gate 		kmem_free(xprt->xp_netid, strlen(xprt->xp_netid) + 1);
283*0Sstevel@tonic-gate 	if (xprt->xp_addrmask.maxlen)
284*0Sstevel@tonic-gate 		kmem_free(xprt->xp_addrmask.buf, xprt->xp_addrmask.maxlen);
285*0Sstevel@tonic-gate 
286*0Sstevel@tonic-gate 	mutex_destroy(&xprt->xp_req_lock);
287*0Sstevel@tonic-gate 	mutex_destroy(&xprt->xp_thread_lock);
288*0Sstevel@tonic-gate 
289*0Sstevel@tonic-gate 	kmem_free(cmd, sizeof (*cmd) + xprt->xp_rtaddr.maxlen);
290*0Sstevel@tonic-gate 	kmem_free(xprt, sizeof (SVCMASTERXPRT));
291*0Sstevel@tonic-gate }
292*0Sstevel@tonic-gate 
293*0Sstevel@tonic-gate /*
294*0Sstevel@tonic-gate  * svc_tli_kcreate() calls this function at the end to tell
295*0Sstevel@tonic-gate  * rpcmod that the transport is ready to receive requests.
296*0Sstevel@tonic-gate  */
297*0Sstevel@tonic-gate static void
298*0Sstevel@tonic-gate svc_cots_kstart(SVCMASTERXPRT *xprt)
299*0Sstevel@tonic-gate {
300*0Sstevel@tonic-gate 	struct cots_master_data *cmd = (struct cots_master_data *)xprt->xp_p2;
301*0Sstevel@tonic-gate 
302*0Sstevel@tonic-gate 	if (cmd->cmd_xprt_started == 0) {
303*0Sstevel@tonic-gate 		/*
304*0Sstevel@tonic-gate 		 * Acquire the xp_req_lock in order to use xp_wq
305*0Sstevel@tonic-gate 		 * safely (we don't want to qenable a queue that has
306*0Sstevel@tonic-gate 		 * already been closed).
307*0Sstevel@tonic-gate 		 */
308*0Sstevel@tonic-gate 		mutex_enter(&xprt->xp_req_lock);
309*0Sstevel@tonic-gate 		if (cmd->cmd_xprt_started == 0 &&
310*0Sstevel@tonic-gate 			xprt->xp_wq != NULL) {
311*0Sstevel@tonic-gate 			(*mir_start)(xprt->xp_wq);
312*0Sstevel@tonic-gate 			cmd->cmd_xprt_started = 1;
313*0Sstevel@tonic-gate 		}
314*0Sstevel@tonic-gate 		mutex_exit(&xprt->xp_req_lock);
315*0Sstevel@tonic-gate 	}
316*0Sstevel@tonic-gate }
317*0Sstevel@tonic-gate 
318*0Sstevel@tonic-gate /*
319*0Sstevel@tonic-gate  * Transport-type specific part of svc_xprt_cleanup().
320*0Sstevel@tonic-gate  */
321*0Sstevel@tonic-gate static void
322*0Sstevel@tonic-gate svc_cots_kclone_destroy(SVCXPRT *clone_xprt)
323*0Sstevel@tonic-gate {
324*0Sstevel@tonic-gate 	cots_data_t *cd = (cots_data_t *)clone_xprt->xp_p2buf;
325*0Sstevel@tonic-gate 
326*0Sstevel@tonic-gate 	if (cd->cd_req_mp) {
327*0Sstevel@tonic-gate 		freemsg(cd->cd_req_mp);
328*0Sstevel@tonic-gate 		cd->cd_req_mp = (mblk_t *)0;
329*0Sstevel@tonic-gate 	}
330*0Sstevel@tonic-gate 	ASSERT(cd->cd_mp == NULL);
331*0Sstevel@tonic-gate }
332*0Sstevel@tonic-gate 
333*0Sstevel@tonic-gate /*
334*0Sstevel@tonic-gate  * Receive rpc requests.
335*0Sstevel@tonic-gate  * Checks if the message is intact, and deserializes the call packet.
336*0Sstevel@tonic-gate  */
337*0Sstevel@tonic-gate static bool_t
338*0Sstevel@tonic-gate svc_cots_krecv(SVCXPRT *clone_xprt, mblk_t *mp, struct rpc_msg *msg)
339*0Sstevel@tonic-gate {
340*0Sstevel@tonic-gate 	cots_data_t *cd = (cots_data_t *)clone_xprt->xp_p2buf;
341*0Sstevel@tonic-gate 	XDR *xdrs = &clone_xprt->xp_xdrin;
342*0Sstevel@tonic-gate 	struct rpc_cots_server *stats = CLONE2STATS(clone_xprt);
343*0Sstevel@tonic-gate 
344*0Sstevel@tonic-gate 	TRACE_0(TR_FAC_KRPC, TR_SVC_COTS_KRECV_START,
345*0Sstevel@tonic-gate 	    "svc_cots_krecv_start:");
346*0Sstevel@tonic-gate 	RPCLOG(4, "svc_cots_krecv_start clone_xprt = %p:\n",
347*0Sstevel@tonic-gate 	    (void *)clone_xprt);
348*0Sstevel@tonic-gate 
349*0Sstevel@tonic-gate 	RSSTAT_INCR(stats, rscalls);
350*0Sstevel@tonic-gate 
351*0Sstevel@tonic-gate 	if (mp->b_datap->db_type != M_DATA) {
352*0Sstevel@tonic-gate 		RPCLOG(16, "svc_cots_krecv bad db_type %d\n",
353*0Sstevel@tonic-gate 		    mp->b_datap->db_type);
354*0Sstevel@tonic-gate 		goto bad;
355*0Sstevel@tonic-gate 	}
356*0Sstevel@tonic-gate 
357*0Sstevel@tonic-gate 	xdrmblk_init(xdrs, mp, XDR_DECODE, 0);
358*0Sstevel@tonic-gate 
359*0Sstevel@tonic-gate 	TRACE_0(TR_FAC_KRPC, TR_XDR_CALLMSG_START,
360*0Sstevel@tonic-gate 	    "xdr_callmsg_start:");
361*0Sstevel@tonic-gate 	RPCLOG0(4, "xdr_callmsg_start:\n");
362*0Sstevel@tonic-gate 	if (!xdr_callmsg(xdrs, msg)) {
363*0Sstevel@tonic-gate 		TRACE_1(TR_FAC_KRPC, TR_XDR_CALLMSG_END,
364*0Sstevel@tonic-gate 		    "xdr_callmsg_end:(%S)", "bad");
365*0Sstevel@tonic-gate 		RPCLOG0(1, "svc_cots_krecv xdr_callmsg failure\n");
366*0Sstevel@tonic-gate 		RSSTAT_INCR(stats, rsxdrcall);
367*0Sstevel@tonic-gate 		goto bad;
368*0Sstevel@tonic-gate 	}
369*0Sstevel@tonic-gate 	TRACE_1(TR_FAC_KRPC, TR_XDR_CALLMSG_END,
370*0Sstevel@tonic-gate 	    "xdr_callmsg_end:(%S)", "good");
371*0Sstevel@tonic-gate 
372*0Sstevel@tonic-gate 	clone_xprt->xp_xid = msg->rm_xid;
373*0Sstevel@tonic-gate 	cd->cd_req_mp = mp;
374*0Sstevel@tonic-gate 
375*0Sstevel@tonic-gate 	TRACE_1(TR_FAC_KRPC, TR_SVC_COTS_KRECV_END,
376*0Sstevel@tonic-gate 	    "svc_cots_krecv_end:(%S)", "good");
377*0Sstevel@tonic-gate 	RPCLOG0(4, "svc_cots_krecv_end:good\n");
378*0Sstevel@tonic-gate 	return (TRUE);
379*0Sstevel@tonic-gate 
380*0Sstevel@tonic-gate bad:
381*0Sstevel@tonic-gate 	if (mp)
382*0Sstevel@tonic-gate 		freemsg(mp);
383*0Sstevel@tonic-gate 
384*0Sstevel@tonic-gate 	RSSTAT_INCR(stats, rsbadcalls);
385*0Sstevel@tonic-gate 	TRACE_1(TR_FAC_KRPC, TR_SVC_COTS_KRECV_END,
386*0Sstevel@tonic-gate 	    "svc_cots_krecv_end:(%S)", "bad");
387*0Sstevel@tonic-gate 	return (FALSE);
388*0Sstevel@tonic-gate }
389*0Sstevel@tonic-gate 
390*0Sstevel@tonic-gate /*
391*0Sstevel@tonic-gate  * Send rpc reply.
392*0Sstevel@tonic-gate  */
393*0Sstevel@tonic-gate static bool_t
394*0Sstevel@tonic-gate svc_cots_ksend(SVCXPRT *clone_xprt, struct rpc_msg *msg)
395*0Sstevel@tonic-gate {
396*0Sstevel@tonic-gate 	/* LINTED pointer alignment */
397*0Sstevel@tonic-gate 	cots_data_t *cd = (cots_data_t *)clone_xprt->xp_p2buf;
398*0Sstevel@tonic-gate 	XDR *xdrs = &(clone_xprt->xp_xdrout);
399*0Sstevel@tonic-gate 	int retval = FALSE;
400*0Sstevel@tonic-gate 	mblk_t *mp;
401*0Sstevel@tonic-gate 	xdrproc_t xdr_results;
402*0Sstevel@tonic-gate 	caddr_t xdr_location;
403*0Sstevel@tonic-gate 	bool_t has_args;
404*0Sstevel@tonic-gate 
405*0Sstevel@tonic-gate 	TRACE_0(TR_FAC_KRPC, TR_SVC_COTS_KSEND_START,
406*0Sstevel@tonic-gate 	    "svc_cots_ksend_start:");
407*0Sstevel@tonic-gate 
408*0Sstevel@tonic-gate 	/*
409*0Sstevel@tonic-gate 	 * If there is a result procedure specified in the reply message,
410*0Sstevel@tonic-gate 	 * it will be processed in the xdr_replymsg and SVCAUTH_WRAP.
411*0Sstevel@tonic-gate 	 * We need to make sure it won't be processed twice, so we null
412*0Sstevel@tonic-gate 	 * it for xdr_replymsg here.
413*0Sstevel@tonic-gate 	 */
414*0Sstevel@tonic-gate 	has_args = FALSE;
415*0Sstevel@tonic-gate 	if (msg->rm_reply.rp_stat == MSG_ACCEPTED &&
416*0Sstevel@tonic-gate 	    msg->rm_reply.rp_acpt.ar_stat == SUCCESS) {
417*0Sstevel@tonic-gate 		if ((xdr_results = msg->acpted_rply.ar_results.proc) != NULL) {
418*0Sstevel@tonic-gate 			has_args = TRUE;
419*0Sstevel@tonic-gate 			xdr_location = msg->acpted_rply.ar_results.where;
420*0Sstevel@tonic-gate 			msg->acpted_rply.ar_results.proc = xdr_void;
421*0Sstevel@tonic-gate 			msg->acpted_rply.ar_results.where = NULL;
422*0Sstevel@tonic-gate 		}
423*0Sstevel@tonic-gate 	}
424*0Sstevel@tonic-gate 
425*0Sstevel@tonic-gate 	mp = cd->cd_mp;
426*0Sstevel@tonic-gate 	if (mp) {
427*0Sstevel@tonic-gate 		/*
428*0Sstevel@tonic-gate 		 * The program above pre-allocated an mblk and put
429*0Sstevel@tonic-gate 		 * the data in place.
430*0Sstevel@tonic-gate 		 */
431*0Sstevel@tonic-gate 		cd->cd_mp = (mblk_t *)NULL;
432*0Sstevel@tonic-gate 		if (!(xdr_replymsg_body(xdrs, msg) &&
433*0Sstevel@tonic-gate 		    (!has_args || SVCAUTH_WRAP(&clone_xprt->xp_auth, xdrs,
434*0Sstevel@tonic-gate 		    xdr_results, xdr_location)))) {
435*0Sstevel@tonic-gate 			RPCLOG0(1, "svc_cots_ksend: "
436*0Sstevel@tonic-gate 			    "xdr_replymsg_body/SVCAUTH_WRAP failed\n");
437*0Sstevel@tonic-gate 			freemsg(mp);
438*0Sstevel@tonic-gate 			goto out;
439*0Sstevel@tonic-gate 		}
440*0Sstevel@tonic-gate 	} else {
441*0Sstevel@tonic-gate 		int	len;
442*0Sstevel@tonic-gate 		int	mpsize;
443*0Sstevel@tonic-gate 
444*0Sstevel@tonic-gate 		/*
445*0Sstevel@tonic-gate 		 * Leave space for protocol headers.
446*0Sstevel@tonic-gate 		 */
447*0Sstevel@tonic-gate 		len = MSG_OFFSET + clone_xprt->xp_msg_size;
448*0Sstevel@tonic-gate 
449*0Sstevel@tonic-gate 		/*
450*0Sstevel@tonic-gate 		 * Allocate an initial mblk for the response data.
451*0Sstevel@tonic-gate 		 */
452*0Sstevel@tonic-gate 		while (!(mp = allocb(len, BPRI_LO))) {
453*0Sstevel@tonic-gate 			RPCLOG0(16, "svc_cots_ksend: allocb failed failed\n");
454*0Sstevel@tonic-gate 			if (strwaitbuf(len, BPRI_LO)) {
455*0Sstevel@tonic-gate 				TRACE_1(TR_FAC_KRPC, TR_SVC_COTS_KSEND_END,
456*0Sstevel@tonic-gate 				    "svc_cots_ksend_end:(%S)", "strwaitbuf");
457*0Sstevel@tonic-gate 				RPCLOG0(1,
458*0Sstevel@tonic-gate 				    "svc_cots_ksend: strwaitbuf failed\n");
459*0Sstevel@tonic-gate 				goto out;
460*0Sstevel@tonic-gate 			}
461*0Sstevel@tonic-gate 		}
462*0Sstevel@tonic-gate 
463*0Sstevel@tonic-gate 		/*
464*0Sstevel@tonic-gate 		 * Initialize the XDR decode stream.  Additional mblks
465*0Sstevel@tonic-gate 		 * will be allocated if necessary.  They will be TIDU
466*0Sstevel@tonic-gate 		 * sized.
467*0Sstevel@tonic-gate 		 */
468*0Sstevel@tonic-gate 		xdrmblk_init(xdrs, mp, XDR_ENCODE, clone_xprt->xp_msg_size);
469*0Sstevel@tonic-gate 		mpsize = MBLKSIZE(mp);
470*0Sstevel@tonic-gate 		ASSERT(mpsize >= len);
471*0Sstevel@tonic-gate 		ASSERT(mp->b_rptr == mp->b_datap->db_base);
472*0Sstevel@tonic-gate 
473*0Sstevel@tonic-gate 		/*
474*0Sstevel@tonic-gate 		 * If the size of mblk is not appreciably larger than what we
475*0Sstevel@tonic-gate 		 * asked, then resize the mblk to exactly len bytes. Reason for
476*0Sstevel@tonic-gate 		 * this: suppose len is 1600 bytes, the tidu is 1460 bytes
477*0Sstevel@tonic-gate 		 * (from TCP over ethernet), and the arguments to RPC require
478*0Sstevel@tonic-gate 		 * 2800 bytes. Ideally we want the protocol to render two
479*0Sstevel@tonic-gate 		 * ~1400 byte segments over the wire. If allocb() gives us a 2k
480*0Sstevel@tonic-gate 		 * mblk, and we allocate a second mblk for the rest, the
481*0Sstevel@tonic-gate 		 * protocol module may generate 3 segments over the wire:
482*0Sstevel@tonic-gate 		 * 1460 bytes for the first, 448 (2048 - 1600) for the 2nd, and
483*0Sstevel@tonic-gate 		 * 892 for the 3rd. If we "waste" 448 bytes in the first mblk,
484*0Sstevel@tonic-gate 		 * the XDR encoding will generate two ~1400 byte mblks, and the
485*0Sstevel@tonic-gate 		 * protocol module is more likely to produce properly sized
486*0Sstevel@tonic-gate 		 * segments.
487*0Sstevel@tonic-gate 		 */
488*0Sstevel@tonic-gate 		if ((mpsize >> 1) <= len) {
489*0Sstevel@tonic-gate 			mp->b_rptr += (mpsize - len);
490*0Sstevel@tonic-gate 		}
491*0Sstevel@tonic-gate 
492*0Sstevel@tonic-gate 		/*
493*0Sstevel@tonic-gate 		 * Adjust b_rptr to reserve space for the non-data protocol
494*0Sstevel@tonic-gate 		 * headers that any downstream modules might like to add, and
495*0Sstevel@tonic-gate 		 * for the record marking header.
496*0Sstevel@tonic-gate 		 */
497*0Sstevel@tonic-gate 		mp->b_rptr += (MSG_OFFSET + RM_HDR_SIZE);
498*0Sstevel@tonic-gate 
499*0Sstevel@tonic-gate 		XDR_SETPOS(xdrs, (uint_t)(mp->b_rptr - mp->b_datap->db_base));
500*0Sstevel@tonic-gate 		ASSERT(mp->b_wptr == mp->b_rptr);
501*0Sstevel@tonic-gate 
502*0Sstevel@tonic-gate 		msg->rm_xid = clone_xprt->xp_xid;
503*0Sstevel@tonic-gate 
504*0Sstevel@tonic-gate 		TRACE_0(TR_FAC_KRPC, TR_XDR_REPLYMSG_START,
505*0Sstevel@tonic-gate 		    "xdr_replymsg_start:");
506*0Sstevel@tonic-gate 		if (!(xdr_replymsg(xdrs, msg) &&
507*0Sstevel@tonic-gate 		    (!has_args || SVCAUTH_WRAP(&clone_xprt->xp_auth, xdrs,
508*0Sstevel@tonic-gate 		    xdr_results, xdr_location)))) {
509*0Sstevel@tonic-gate 			TRACE_1(TR_FAC_KRPC, TR_XDR_REPLYMSG_END,
510*0Sstevel@tonic-gate 			    "xdr_replymsg_end:(%S)", "bad");
511*0Sstevel@tonic-gate 			freemsg(mp);
512*0Sstevel@tonic-gate 			RPCLOG0(1, "svc_cots_ksend: xdr_replymsg/SVCAUTH_WRAP "
513*0Sstevel@tonic-gate 				"failed\n");
514*0Sstevel@tonic-gate 			goto out;
515*0Sstevel@tonic-gate 		}
516*0Sstevel@tonic-gate 		TRACE_1(TR_FAC_KRPC, TR_XDR_REPLYMSG_END,
517*0Sstevel@tonic-gate 		    "xdr_replymsg_end:(%S)", "good");
518*0Sstevel@tonic-gate 	}
519*0Sstevel@tonic-gate 
520*0Sstevel@tonic-gate 	put(clone_xprt->xp_wq, mp);
521*0Sstevel@tonic-gate 	retval = TRUE;
522*0Sstevel@tonic-gate 
523*0Sstevel@tonic-gate out:
524*0Sstevel@tonic-gate 	/*
525*0Sstevel@tonic-gate 	 * This is completely disgusting.  If public is set it is
526*0Sstevel@tonic-gate 	 * a pointer to a structure whose first field is the address
527*0Sstevel@tonic-gate 	 * of the function to free that structure and any related
528*0Sstevel@tonic-gate 	 * stuff.  (see rrokfree in nfs_xdr.c).
529*0Sstevel@tonic-gate 	 */
530*0Sstevel@tonic-gate 	if (xdrs->x_public) {
531*0Sstevel@tonic-gate 		/* LINTED pointer alignment */
532*0Sstevel@tonic-gate 		(**((int (**)())xdrs->x_public))(xdrs->x_public);
533*0Sstevel@tonic-gate 	}
534*0Sstevel@tonic-gate 
535*0Sstevel@tonic-gate 	TRACE_1(TR_FAC_KRPC, TR_SVC_COTS_KSEND_END,
536*0Sstevel@tonic-gate 	    "svc_cots_ksend_end:(%S)", "done");
537*0Sstevel@tonic-gate 	return (retval);
538*0Sstevel@tonic-gate }
539*0Sstevel@tonic-gate 
540*0Sstevel@tonic-gate /*
541*0Sstevel@tonic-gate  * Deserialize arguments.
542*0Sstevel@tonic-gate  */
543*0Sstevel@tonic-gate static bool_t
544*0Sstevel@tonic-gate svc_cots_kgetargs(SVCXPRT *clone_xprt, xdrproc_t xdr_args,
545*0Sstevel@tonic-gate     caddr_t args_ptr)
546*0Sstevel@tonic-gate {
547*0Sstevel@tonic-gate 	return (SVCAUTH_UNWRAP(&clone_xprt->xp_auth, &clone_xprt->xp_xdrin,
548*0Sstevel@tonic-gate 	    xdr_args, args_ptr));
549*0Sstevel@tonic-gate }
550*0Sstevel@tonic-gate 
551*0Sstevel@tonic-gate static bool_t
552*0Sstevel@tonic-gate svc_cots_kfreeargs(SVCXPRT *clone_xprt, xdrproc_t xdr_args,
553*0Sstevel@tonic-gate     caddr_t args_ptr)
554*0Sstevel@tonic-gate {
555*0Sstevel@tonic-gate 	cots_data_t *cd = (cots_data_t *)clone_xprt->xp_p2buf;
556*0Sstevel@tonic-gate 	mblk_t *mp;
557*0Sstevel@tonic-gate 	bool_t retval;
558*0Sstevel@tonic-gate 
559*0Sstevel@tonic-gate 	/*
560*0Sstevel@tonic-gate 	 * It is important to call the XDR routine before
561*0Sstevel@tonic-gate 	 * freeing the request mblk.  Structures in the
562*0Sstevel@tonic-gate 	 * XDR data may point into the mblk and require that
563*0Sstevel@tonic-gate 	 * the memory be intact during the free routine.
564*0Sstevel@tonic-gate 	 */
565*0Sstevel@tonic-gate 	if (args_ptr) {
566*0Sstevel@tonic-gate 		/* LINTED pointer alignment */
567*0Sstevel@tonic-gate 		XDR	*xdrs = &clone_xprt->xp_xdrin;
568*0Sstevel@tonic-gate 
569*0Sstevel@tonic-gate 		xdrs->x_op = XDR_FREE;
570*0Sstevel@tonic-gate 		retval = (*xdr_args)(xdrs, args_ptr);
571*0Sstevel@tonic-gate 	} else
572*0Sstevel@tonic-gate 		retval = TRUE;
573*0Sstevel@tonic-gate 
574*0Sstevel@tonic-gate 	if ((mp = cd->cd_req_mp) != NULL) {
575*0Sstevel@tonic-gate 		cd->cd_req_mp = (mblk_t *)0;
576*0Sstevel@tonic-gate 		freemsg(mp);
577*0Sstevel@tonic-gate 	}
578*0Sstevel@tonic-gate 
579*0Sstevel@tonic-gate 	return (retval);
580*0Sstevel@tonic-gate }
581*0Sstevel@tonic-gate 
582*0Sstevel@tonic-gate static int32_t *
583*0Sstevel@tonic-gate svc_cots_kgetres(SVCXPRT *clone_xprt, int size)
584*0Sstevel@tonic-gate {
585*0Sstevel@tonic-gate 	/* LINTED pointer alignment */
586*0Sstevel@tonic-gate 	cots_data_t *cd = (cots_data_t *)clone_xprt->xp_p2buf;
587*0Sstevel@tonic-gate 	XDR *xdrs = &clone_xprt->xp_xdrout;
588*0Sstevel@tonic-gate 	mblk_t *mp;
589*0Sstevel@tonic-gate 	int32_t *buf;
590*0Sstevel@tonic-gate 	struct rpc_msg rply;
591*0Sstevel@tonic-gate 	int len;
592*0Sstevel@tonic-gate 	int mpsize;
593*0Sstevel@tonic-gate 
594*0Sstevel@tonic-gate 	/*
595*0Sstevel@tonic-gate 	 * Leave space for protocol headers.
596*0Sstevel@tonic-gate 	 */
597*0Sstevel@tonic-gate 	len = MSG_OFFSET + clone_xprt->xp_msg_size;
598*0Sstevel@tonic-gate 
599*0Sstevel@tonic-gate 	/*
600*0Sstevel@tonic-gate 	 * Allocate an initial mblk for the response data.
601*0Sstevel@tonic-gate 	 */
602*0Sstevel@tonic-gate 	while ((mp = allocb(len, BPRI_LO)) == NULL) {
603*0Sstevel@tonic-gate 		if (strwaitbuf(len, BPRI_LO))
604*0Sstevel@tonic-gate 			return (FALSE);
605*0Sstevel@tonic-gate 	}
606*0Sstevel@tonic-gate 
607*0Sstevel@tonic-gate 	/*
608*0Sstevel@tonic-gate 	 * Initialize the XDR decode stream.  Additional mblks
609*0Sstevel@tonic-gate 	 * will be allocated if necessary.  They will be TIDU
610*0Sstevel@tonic-gate 	 * sized.
611*0Sstevel@tonic-gate 	 */
612*0Sstevel@tonic-gate 	xdrmblk_init(xdrs, mp, XDR_ENCODE, clone_xprt->xp_msg_size);
613*0Sstevel@tonic-gate 	mpsize = MBLKSIZE(mp);
614*0Sstevel@tonic-gate 	ASSERT(mpsize >= len);
615*0Sstevel@tonic-gate 	ASSERT(mp->b_rptr == mp->b_datap->db_base);
616*0Sstevel@tonic-gate 
617*0Sstevel@tonic-gate 	/*
618*0Sstevel@tonic-gate 	 * If the size of mblk is not appreciably larger than what we
619*0Sstevel@tonic-gate 	 * asked, then resize the mblk to exactly len bytes. Reason for
620*0Sstevel@tonic-gate 	 * this: suppose len is 1600 bytes, the tidu is 1460 bytes
621*0Sstevel@tonic-gate 	 * (from TCP over ethernet), and the arguments to RPC require
622*0Sstevel@tonic-gate 	 * 2800 bytes. Ideally we want the protocol to render two
623*0Sstevel@tonic-gate 	 * ~1400 byte segments over the wire. If allocb() gives us a 2k
624*0Sstevel@tonic-gate 	 * mblk, and we allocate a second mblk for the rest, the
625*0Sstevel@tonic-gate 	 * protocol module may generate 3 segments over the wire:
626*0Sstevel@tonic-gate 	 * 1460 bytes for the first, 448 (2048 - 1600) for the 2nd, and
627*0Sstevel@tonic-gate 	 * 892 for the 3rd. If we "waste" 448 bytes in the first mblk,
628*0Sstevel@tonic-gate 	 * the XDR encoding will generate two ~1400 byte mblks, and the
629*0Sstevel@tonic-gate 	 * protocol module is more likely to produce properly sized
630*0Sstevel@tonic-gate 	 * segments.
631*0Sstevel@tonic-gate 	 */
632*0Sstevel@tonic-gate 	if ((mpsize >> 1) <= len) {
633*0Sstevel@tonic-gate 		mp->b_rptr += (mpsize - len);
634*0Sstevel@tonic-gate 	}
635*0Sstevel@tonic-gate 
636*0Sstevel@tonic-gate 	/*
637*0Sstevel@tonic-gate 	 * Adjust b_rptr to reserve space for the non-data protocol
638*0Sstevel@tonic-gate 	 * headers that any downstream modules might like to add, and
639*0Sstevel@tonic-gate 	 * for the record marking header.
640*0Sstevel@tonic-gate 	 */
641*0Sstevel@tonic-gate 	mp->b_rptr += (MSG_OFFSET + RM_HDR_SIZE);
642*0Sstevel@tonic-gate 
643*0Sstevel@tonic-gate 	XDR_SETPOS(xdrs, (uint_t)(mp->b_rptr - mp->b_datap->db_base));
644*0Sstevel@tonic-gate 	ASSERT(mp->b_wptr == mp->b_rptr);
645*0Sstevel@tonic-gate 
646*0Sstevel@tonic-gate 	/*
647*0Sstevel@tonic-gate 	 * Assume a successful RPC since most of them are.
648*0Sstevel@tonic-gate 	 */
649*0Sstevel@tonic-gate 	rply.rm_xid = clone_xprt->xp_xid;
650*0Sstevel@tonic-gate 	rply.rm_direction = REPLY;
651*0Sstevel@tonic-gate 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
652*0Sstevel@tonic-gate 	rply.acpted_rply.ar_verf = clone_xprt->xp_verf;
653*0Sstevel@tonic-gate 	rply.acpted_rply.ar_stat = SUCCESS;
654*0Sstevel@tonic-gate 
655*0Sstevel@tonic-gate 	if (!xdr_replymsg_hdr(xdrs, &rply)) {
656*0Sstevel@tonic-gate 		freeb(mp);
657*0Sstevel@tonic-gate 		return (NULL);
658*0Sstevel@tonic-gate 	}
659*0Sstevel@tonic-gate 
660*0Sstevel@tonic-gate 
661*0Sstevel@tonic-gate 	buf = XDR_INLINE(xdrs, size);
662*0Sstevel@tonic-gate 	if (buf == NULL) {
663*0Sstevel@tonic-gate 		ASSERT(cd->cd_mp == NULL);
664*0Sstevel@tonic-gate 		freemsg(mp);
665*0Sstevel@tonic-gate 	} else {
666*0Sstevel@tonic-gate 		cd->cd_mp = mp;
667*0Sstevel@tonic-gate 	}
668*0Sstevel@tonic-gate 	return (buf);
669*0Sstevel@tonic-gate }
670*0Sstevel@tonic-gate 
671*0Sstevel@tonic-gate static void
672*0Sstevel@tonic-gate svc_cots_kfreeres(SVCXPRT *clone_xprt)
673*0Sstevel@tonic-gate {
674*0Sstevel@tonic-gate 	cots_data_t *cd;
675*0Sstevel@tonic-gate 	mblk_t *mp;
676*0Sstevel@tonic-gate 
677*0Sstevel@tonic-gate 	cd = (cots_data_t *)clone_xprt->xp_p2buf;
678*0Sstevel@tonic-gate 	if ((mp = cd->cd_mp) != NULL) {
679*0Sstevel@tonic-gate 		cd->cd_mp = (mblk_t *)NULL;
680*0Sstevel@tonic-gate 		freemsg(mp);
681*0Sstevel@tonic-gate 	}
682*0Sstevel@tonic-gate }
683*0Sstevel@tonic-gate 
684*0Sstevel@tonic-gate /*
685*0Sstevel@tonic-gate  * the dup cacheing routines below provide a cache of non-failure
686*0Sstevel@tonic-gate  * transaction id's.  rpc service routines can use this to detect
687*0Sstevel@tonic-gate  * retransmissions and re-send a non-failure response.
688*0Sstevel@tonic-gate  */
689*0Sstevel@tonic-gate 
690*0Sstevel@tonic-gate /*
691*0Sstevel@tonic-gate  * MAXDUPREQS is the number of cached items.  It should be adjusted
692*0Sstevel@tonic-gate  * to the service load so that there is likely to be a response entry
693*0Sstevel@tonic-gate  * when the first retransmission comes in.
694*0Sstevel@tonic-gate  */
695*0Sstevel@tonic-gate #define	MAXDUPREQS	1024
696*0Sstevel@tonic-gate 
697*0Sstevel@tonic-gate /*
698*0Sstevel@tonic-gate  * This should be appropriately scaled to MAXDUPREQS.
699*0Sstevel@tonic-gate  */
700*0Sstevel@tonic-gate #define	DRHASHSZ	257
701*0Sstevel@tonic-gate 
702*0Sstevel@tonic-gate #if ((DRHASHSZ & (DRHASHSZ - 1)) == 0)
703*0Sstevel@tonic-gate #define	XIDHASH(xid)	((xid) & (DRHASHSZ - 1))
704*0Sstevel@tonic-gate #else
705*0Sstevel@tonic-gate #define	XIDHASH(xid)	((xid) % DRHASHSZ)
706*0Sstevel@tonic-gate #endif
707*0Sstevel@tonic-gate #define	DRHASH(dr)	XIDHASH((dr)->dr_xid)
708*0Sstevel@tonic-gate #define	REQTOXID(req)	((req)->rq_xprt->xp_xid)
709*0Sstevel@tonic-gate 
710*0Sstevel@tonic-gate static int	cotsndupreqs = 0;
711*0Sstevel@tonic-gate static int	cotsmaxdupreqs = MAXDUPREQS;
712*0Sstevel@tonic-gate static kmutex_t cotsdupreq_lock;
713*0Sstevel@tonic-gate static struct dupreq *cotsdrhashtbl[DRHASHSZ];
714*0Sstevel@tonic-gate static int	cotsdrhashstat[DRHASHSZ];
715*0Sstevel@tonic-gate 
716*0Sstevel@tonic-gate static void unhash(struct dupreq *);
717*0Sstevel@tonic-gate 
718*0Sstevel@tonic-gate /*
719*0Sstevel@tonic-gate  * cotsdrmru points to the head of a circular linked list in lru order.
720*0Sstevel@tonic-gate  * cotsdrmru->dr_next == drlru
721*0Sstevel@tonic-gate  */
722*0Sstevel@tonic-gate struct dupreq *cotsdrmru;
723*0Sstevel@tonic-gate 
724*0Sstevel@tonic-gate /*
725*0Sstevel@tonic-gate  * PSARC 2003/523 Contract Private Interface
726*0Sstevel@tonic-gate  * svc_cots_kdup
727*0Sstevel@tonic-gate  * Changes must be reviewed by Solaris File Sharing
728*0Sstevel@tonic-gate  * Changes must be communicated to contract-2003-523@sun.com
729*0Sstevel@tonic-gate  *
730*0Sstevel@tonic-gate  * svc_cots_kdup searches the request cache and returns 0 if the
731*0Sstevel@tonic-gate  * request is not found in the cache.  If it is found, then it
732*0Sstevel@tonic-gate  * returns the state of the request (in progress or done) and
733*0Sstevel@tonic-gate  * the status or attributes that were part of the original reply.
734*0Sstevel@tonic-gate  *
735*0Sstevel@tonic-gate  * If DUP_DONE (there is a duplicate) svc_cots_kdup copies over the
736*0Sstevel@tonic-gate  * value of the response. In that case, also return in *dupcachedp
737*0Sstevel@tonic-gate  * whether the response free routine is cached in the dupreq - in which case
738*0Sstevel@tonic-gate  * the caller should not be freeing it, because it will be done later
739*0Sstevel@tonic-gate  * in the svc_cots_kdup code when the dupreq is reused.
740*0Sstevel@tonic-gate  */
741*0Sstevel@tonic-gate static int
742*0Sstevel@tonic-gate svc_cots_kdup(struct svc_req *req, caddr_t res, int size, struct dupreq **drpp,
743*0Sstevel@tonic-gate 	bool_t *dupcachedp)
744*0Sstevel@tonic-gate {
745*0Sstevel@tonic-gate 	struct rpc_cots_server *stats = CLONE2STATS(req->rq_xprt);
746*0Sstevel@tonic-gate 	struct dupreq *dr;
747*0Sstevel@tonic-gate 	uint32_t xid;
748*0Sstevel@tonic-gate 	uint32_t drhash;
749*0Sstevel@tonic-gate 	int status;
750*0Sstevel@tonic-gate 
751*0Sstevel@tonic-gate 	xid = REQTOXID(req);
752*0Sstevel@tonic-gate 	mutex_enter(&cotsdupreq_lock);
753*0Sstevel@tonic-gate 	RSSTAT_INCR(stats, rsdupchecks);
754*0Sstevel@tonic-gate 	/*
755*0Sstevel@tonic-gate 	 * Check to see whether an entry already exists in the cache.
756*0Sstevel@tonic-gate 	 */
757*0Sstevel@tonic-gate 	dr = cotsdrhashtbl[XIDHASH(xid)];
758*0Sstevel@tonic-gate 	while (dr != NULL) {
759*0Sstevel@tonic-gate 		if (dr->dr_xid == xid &&
760*0Sstevel@tonic-gate 		    dr->dr_proc == req->rq_proc &&
761*0Sstevel@tonic-gate 		    dr->dr_prog == req->rq_prog &&
762*0Sstevel@tonic-gate 		    dr->dr_vers == req->rq_vers &&
763*0Sstevel@tonic-gate 		    dr->dr_addr.len == req->rq_xprt->xp_rtaddr.len &&
764*0Sstevel@tonic-gate 		    bcmp((caddr_t)dr->dr_addr.buf,
765*0Sstevel@tonic-gate 		    (caddr_t)req->rq_xprt->xp_rtaddr.buf,
766*0Sstevel@tonic-gate 		    dr->dr_addr.len) == 0) {
767*0Sstevel@tonic-gate 			status = dr->dr_status;
768*0Sstevel@tonic-gate 			if (status == DUP_DONE) {
769*0Sstevel@tonic-gate 				bcopy(dr->dr_resp.buf, res, size);
770*0Sstevel@tonic-gate 				if (dupcachedp != NULL)
771*0Sstevel@tonic-gate 					*dupcachedp = (dr->dr_resfree != NULL);
772*0Sstevel@tonic-gate 				TRACE_0(TR_FAC_KRPC, TR_SVC_COTS_KDUP_DONE,
773*0Sstevel@tonic-gate 				    "svc_cots_kdup: DUP_DONE");
774*0Sstevel@tonic-gate 			} else {
775*0Sstevel@tonic-gate 				dr->dr_status = DUP_INPROGRESS;
776*0Sstevel@tonic-gate 				*drpp = dr;
777*0Sstevel@tonic-gate 				TRACE_0(TR_FAC_KRPC,
778*0Sstevel@tonic-gate 				    TR_SVC_COTS_KDUP_INPROGRESS,
779*0Sstevel@tonic-gate 				    "svc_cots_kdup: DUP_INPROGRESS");
780*0Sstevel@tonic-gate 			}
781*0Sstevel@tonic-gate 			RSSTAT_INCR(stats, rsdupreqs);
782*0Sstevel@tonic-gate 			mutex_exit(&cotsdupreq_lock);
783*0Sstevel@tonic-gate 			return (status);
784*0Sstevel@tonic-gate 		}
785*0Sstevel@tonic-gate 		dr = dr->dr_chain;
786*0Sstevel@tonic-gate 	}
787*0Sstevel@tonic-gate 
788*0Sstevel@tonic-gate 	/*
789*0Sstevel@tonic-gate 	 * There wasn't an entry, either allocate a new one or recycle
790*0Sstevel@tonic-gate 	 * an old one.
791*0Sstevel@tonic-gate 	 */
792*0Sstevel@tonic-gate 	if (cotsndupreqs < cotsmaxdupreqs) {
793*0Sstevel@tonic-gate 		dr = kmem_alloc(sizeof (*dr), KM_NOSLEEP);
794*0Sstevel@tonic-gate 		if (dr == NULL) {
795*0Sstevel@tonic-gate 			mutex_exit(&cotsdupreq_lock);
796*0Sstevel@tonic-gate 			return (DUP_ERROR);
797*0Sstevel@tonic-gate 		}
798*0Sstevel@tonic-gate 		dr->dr_resp.buf = NULL;
799*0Sstevel@tonic-gate 		dr->dr_resp.maxlen = 0;
800*0Sstevel@tonic-gate 		dr->dr_addr.buf = NULL;
801*0Sstevel@tonic-gate 		dr->dr_addr.maxlen = 0;
802*0Sstevel@tonic-gate 		if (cotsdrmru) {
803*0Sstevel@tonic-gate 			dr->dr_next = cotsdrmru->dr_next;
804*0Sstevel@tonic-gate 			cotsdrmru->dr_next = dr;
805*0Sstevel@tonic-gate 		} else {
806*0Sstevel@tonic-gate 			dr->dr_next = dr;
807*0Sstevel@tonic-gate 		}
808*0Sstevel@tonic-gate 		cotsndupreqs++;
809*0Sstevel@tonic-gate 	} else {
810*0Sstevel@tonic-gate 		dr = cotsdrmru->dr_next;
811*0Sstevel@tonic-gate 		while (dr->dr_status == DUP_INPROGRESS) {
812*0Sstevel@tonic-gate 			dr = dr->dr_next;
813*0Sstevel@tonic-gate 			if (dr == cotsdrmru->dr_next) {
814*0Sstevel@tonic-gate 				cmn_err(CE_WARN, "svc_cots_kdup no slots free");
815*0Sstevel@tonic-gate 				mutex_exit(&cotsdupreq_lock);
816*0Sstevel@tonic-gate 				return (DUP_ERROR);
817*0Sstevel@tonic-gate 			}
818*0Sstevel@tonic-gate 		}
819*0Sstevel@tonic-gate 		unhash(dr);
820*0Sstevel@tonic-gate 		if (dr->dr_resfree) {
821*0Sstevel@tonic-gate 			(*dr->dr_resfree)(dr->dr_resp.buf);
822*0Sstevel@tonic-gate 		}
823*0Sstevel@tonic-gate 	}
824*0Sstevel@tonic-gate 	dr->dr_resfree = NULL;
825*0Sstevel@tonic-gate 	cotsdrmru = dr;
826*0Sstevel@tonic-gate 
827*0Sstevel@tonic-gate 	dr->dr_xid = REQTOXID(req);
828*0Sstevel@tonic-gate 	dr->dr_prog = req->rq_prog;
829*0Sstevel@tonic-gate 	dr->dr_vers = req->rq_vers;
830*0Sstevel@tonic-gate 	dr->dr_proc = req->rq_proc;
831*0Sstevel@tonic-gate 	if (dr->dr_addr.maxlen < req->rq_xprt->xp_rtaddr.len) {
832*0Sstevel@tonic-gate 		if (dr->dr_addr.buf != NULL)
833*0Sstevel@tonic-gate 			kmem_free(dr->dr_addr.buf, dr->dr_addr.maxlen);
834*0Sstevel@tonic-gate 		dr->dr_addr.maxlen = req->rq_xprt->xp_rtaddr.len;
835*0Sstevel@tonic-gate 		dr->dr_addr.buf = kmem_alloc(dr->dr_addr.maxlen, KM_NOSLEEP);
836*0Sstevel@tonic-gate 		if (dr->dr_addr.buf == NULL) {
837*0Sstevel@tonic-gate 			dr->dr_addr.maxlen = 0;
838*0Sstevel@tonic-gate 			dr->dr_status = DUP_DROP;
839*0Sstevel@tonic-gate 			mutex_exit(&cotsdupreq_lock);
840*0Sstevel@tonic-gate 			return (DUP_ERROR);
841*0Sstevel@tonic-gate 		}
842*0Sstevel@tonic-gate 	}
843*0Sstevel@tonic-gate 	dr->dr_addr.len = req->rq_xprt->xp_rtaddr.len;
844*0Sstevel@tonic-gate 	bcopy(req->rq_xprt->xp_rtaddr.buf, dr->dr_addr.buf, dr->dr_addr.len);
845*0Sstevel@tonic-gate 	if (dr->dr_resp.maxlen < size) {
846*0Sstevel@tonic-gate 		if (dr->dr_resp.buf != NULL)
847*0Sstevel@tonic-gate 			kmem_free(dr->dr_resp.buf, dr->dr_resp.maxlen);
848*0Sstevel@tonic-gate 		dr->dr_resp.maxlen = (unsigned int)size;
849*0Sstevel@tonic-gate 		dr->dr_resp.buf = kmem_alloc(size, KM_NOSLEEP);
850*0Sstevel@tonic-gate 		if (dr->dr_resp.buf == NULL) {
851*0Sstevel@tonic-gate 			dr->dr_resp.maxlen = 0;
852*0Sstevel@tonic-gate 			dr->dr_status = DUP_DROP;
853*0Sstevel@tonic-gate 			mutex_exit(&cotsdupreq_lock);
854*0Sstevel@tonic-gate 			return (DUP_ERROR);
855*0Sstevel@tonic-gate 		}
856*0Sstevel@tonic-gate 	}
857*0Sstevel@tonic-gate 	dr->dr_status = DUP_INPROGRESS;
858*0Sstevel@tonic-gate 
859*0Sstevel@tonic-gate 	drhash = (uint32_t)DRHASH(dr);
860*0Sstevel@tonic-gate 	dr->dr_chain = cotsdrhashtbl[drhash];
861*0Sstevel@tonic-gate 	cotsdrhashtbl[drhash] = dr;
862*0Sstevel@tonic-gate 	cotsdrhashstat[drhash]++;
863*0Sstevel@tonic-gate 	mutex_exit(&cotsdupreq_lock);
864*0Sstevel@tonic-gate 	*drpp = dr;
865*0Sstevel@tonic-gate 	return (DUP_NEW);
866*0Sstevel@tonic-gate }
867*0Sstevel@tonic-gate 
868*0Sstevel@tonic-gate /*
869*0Sstevel@tonic-gate  * PSARC 2003/523 Contract Private Interface
870*0Sstevel@tonic-gate  * svc_cots_kdupdone
871*0Sstevel@tonic-gate  * Changes must be reviewed by Solaris File Sharing
872*0Sstevel@tonic-gate  * Changes must be communicated to contract-2003-523@sun.com
873*0Sstevel@tonic-gate  *
874*0Sstevel@tonic-gate  * svc_cots_kdupdone marks the request done (DUP_DONE or DUP_DROP)
875*0Sstevel@tonic-gate  * and stores the response.
876*0Sstevel@tonic-gate  */
877*0Sstevel@tonic-gate static void
878*0Sstevel@tonic-gate svc_cots_kdupdone(struct dupreq *dr, caddr_t res, void (*dis_resfree)(),
879*0Sstevel@tonic-gate 	int size, int status)
880*0Sstevel@tonic-gate {
881*0Sstevel@tonic-gate 	ASSERT(dr->dr_resfree == NULL);
882*0Sstevel@tonic-gate 	if (status == DUP_DONE) {
883*0Sstevel@tonic-gate 		bcopy(res, dr->dr_resp.buf, size);
884*0Sstevel@tonic-gate 		dr->dr_resfree = dis_resfree;
885*0Sstevel@tonic-gate 	}
886*0Sstevel@tonic-gate 	dr->dr_status = status;
887*0Sstevel@tonic-gate }
888*0Sstevel@tonic-gate 
889*0Sstevel@tonic-gate /*
890*0Sstevel@tonic-gate  * This routine expects that the mutex, cotsdupreq_lock, is already held.
891*0Sstevel@tonic-gate  */
892*0Sstevel@tonic-gate static void
893*0Sstevel@tonic-gate unhash(struct dupreq *dr)
894*0Sstevel@tonic-gate {
895*0Sstevel@tonic-gate 	struct dupreq *drt;
896*0Sstevel@tonic-gate 	struct dupreq *drtprev = NULL;
897*0Sstevel@tonic-gate 	uint32_t drhash;
898*0Sstevel@tonic-gate 
899*0Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cotsdupreq_lock));
900*0Sstevel@tonic-gate 
901*0Sstevel@tonic-gate 	drhash = (uint32_t)DRHASH(dr);
902*0Sstevel@tonic-gate 	drt = cotsdrhashtbl[drhash];
903*0Sstevel@tonic-gate 	while (drt != NULL) {
904*0Sstevel@tonic-gate 		if (drt == dr) {
905*0Sstevel@tonic-gate 			cotsdrhashstat[drhash]--;
906*0Sstevel@tonic-gate 			if (drtprev == NULL) {
907*0Sstevel@tonic-gate 				cotsdrhashtbl[drhash] = drt->dr_chain;
908*0Sstevel@tonic-gate 			} else {
909*0Sstevel@tonic-gate 				drtprev->dr_chain = drt->dr_chain;
910*0Sstevel@tonic-gate 			}
911*0Sstevel@tonic-gate 			return;
912*0Sstevel@tonic-gate 		}
913*0Sstevel@tonic-gate 		drtprev = drt;
914*0Sstevel@tonic-gate 		drt = drt->dr_chain;
915*0Sstevel@tonic-gate 	}
916*0Sstevel@tonic-gate }
917*0Sstevel@tonic-gate 
918*0Sstevel@tonic-gate void
919*0Sstevel@tonic-gate svc_cots_stats_init(zoneid_t zoneid, struct rpc_cots_server **statsp)
920*0Sstevel@tonic-gate {
921*0Sstevel@tonic-gate 	*statsp = (struct rpc_cots_server *)rpcstat_zone_init_common(zoneid,
922*0Sstevel@tonic-gate 	    "unix", "rpc_cots_server", (const kstat_named_t *)&cots_rsstat_tmpl,
923*0Sstevel@tonic-gate 	    sizeof (cots_rsstat_tmpl));
924*0Sstevel@tonic-gate }
925*0Sstevel@tonic-gate 
926*0Sstevel@tonic-gate void
927*0Sstevel@tonic-gate svc_cots_stats_fini(zoneid_t zoneid, struct rpc_cots_server **statsp)
928*0Sstevel@tonic-gate {
929*0Sstevel@tonic-gate 	rpcstat_zone_fini_common(zoneid, "unix", "rpc_cots_server");
930*0Sstevel@tonic-gate 	kmem_free(*statsp, sizeof (cots_rsstat_tmpl));
931*0Sstevel@tonic-gate }
932*0Sstevel@tonic-gate 
933*0Sstevel@tonic-gate void
934*0Sstevel@tonic-gate svc_cots_init(void)
935*0Sstevel@tonic-gate {
936*0Sstevel@tonic-gate 	/*
937*0Sstevel@tonic-gate 	 * Check to make sure that the cots private data will fit into
938*0Sstevel@tonic-gate 	 * the stack buffer allocated by svc_run.  The ASSERT is a safety
939*0Sstevel@tonic-gate 	 * net if the cots_data_t structure ever changes.
940*0Sstevel@tonic-gate 	 */
941*0Sstevel@tonic-gate 	/*CONSTANTCONDITION*/
942*0Sstevel@tonic-gate 	ASSERT(sizeof (cots_data_t) <= SVC_P2LEN);
943*0Sstevel@tonic-gate 
944*0Sstevel@tonic-gate 	mutex_init(&cots_kcreate_lock, NULL, MUTEX_DEFAULT, NULL);
945*0Sstevel@tonic-gate 	mutex_init(&cotsdupreq_lock, NULL, MUTEX_DEFAULT, NULL);
946*0Sstevel@tonic-gate }
947