xref: /titanic_52/usr/src/uts/common/rpc/svc_clts.c (revision 03831d35f7499c87d51205817c93e9a8d42c4bae)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  *  Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  *  Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  * Portions of this source code were derived from Berkeley 4.3 BSD
32  * under license from the Regents of the University of California.
33  */
34 
35 #pragma ident	"%Z%%M%	%I%	%E% SMI"
36 
37 /*
38  * svc_clts.c
39  * Server side for RPC in the kernel.
40  *
41  */
42 
43 #include <sys/param.h>
44 #include <sys/types.h>
45 #include <sys/sysmacros.h>
46 #include <sys/file.h>
47 #include <sys/stream.h>
48 #include <sys/strsubr.h>
49 #include <sys/tihdr.h>
50 #include <sys/tiuser.h>
51 #include <sys/t_kuser.h>
52 #include <sys/fcntl.h>
53 #include <sys/errno.h>
54 #include <sys/kmem.h>
55 #include <sys/systm.h>
56 #include <sys/cmn_err.h>
57 #include <sys/kstat.h>
58 #include <sys/vtrace.h>
59 #include <sys/debug.h>
60 
61 #include <rpc/types.h>
62 #include <rpc/xdr.h>
63 #include <rpc/auth.h>
64 #include <rpc/clnt.h>
65 #include <rpc/rpc_msg.h>
66 #include <rpc/svc.h>
67 
68 /*
69  * Routines exported through ops vector.
70  */
71 static bool_t		svc_clts_krecv(SVCXPRT *, mblk_t *, struct rpc_msg *);
72 static bool_t		svc_clts_ksend(SVCXPRT *, struct rpc_msg *);
73 static bool_t		svc_clts_kgetargs(SVCXPRT *, xdrproc_t, caddr_t);
74 static bool_t		svc_clts_kfreeargs(SVCXPRT *, xdrproc_t, caddr_t);
75 static void		svc_clts_kdestroy(SVCMASTERXPRT *);
76 static int		svc_clts_kdup(struct svc_req *, caddr_t, int,
77 				struct dupreq **, bool_t *);
78 static void		svc_clts_kdupdone(struct dupreq *, caddr_t,
79 				void (*)(), int, int);
80 static int32_t		*svc_clts_kgetres(SVCXPRT *, int);
81 static void		svc_clts_kclone_destroy(SVCXPRT *);
82 static void		svc_clts_kfreeres(SVCXPRT *);
83 static void		svc_clts_kstart(SVCMASTERXPRT *);
84 
85 /*
86  * Server transport operations vector.
87  */
88 struct svc_ops svc_clts_op = {
89 	svc_clts_krecv,		/* Get requests */
90 	svc_clts_kgetargs,	/* Deserialize arguments */
91 	svc_clts_ksend,		/* Send reply */
92 	svc_clts_kfreeargs,	/* Free argument data space */
93 	svc_clts_kdestroy,	/* Destroy transport handle */
94 	svc_clts_kdup,		/* Check entry in dup req cache */
95 	svc_clts_kdupdone,	/* Mark entry in dup req cache as done */
96 	svc_clts_kgetres,	/* Get pointer to response buffer */
97 	svc_clts_kfreeres,	/* Destroy pre-serialized response header */
98 	svc_clts_kclone_destroy, /* Destroy a clone xprt */
99 	svc_clts_kstart		/* Tell `ready-to-receive' to rpcmod */
100 };
101 
102 /*
103  * Transport private data.
104  * Kept in xprt->xp_p2buf.
105  */
106 struct udp_data {
107 	mblk_t	*ud_resp;			/* buffer for response */
108 	mblk_t	*ud_inmp;			/* mblk chain of request */
109 };
110 
111 #define	UD_MAXSIZE	8800
112 #define	UD_INITSIZE	2048
113 
114 /*
115  * Connectionless server statistics
116  */
117 static const struct rpc_clts_server {
118 	kstat_named_t	rscalls;
119 	kstat_named_t	rsbadcalls;
120 	kstat_named_t	rsnullrecv;
121 	kstat_named_t	rsbadlen;
122 	kstat_named_t	rsxdrcall;
123 	kstat_named_t	rsdupchecks;
124 	kstat_named_t	rsdupreqs;
125 } clts_rsstat_tmpl = {
126 	{ "calls",	KSTAT_DATA_UINT64 },
127 	{ "badcalls",	KSTAT_DATA_UINT64 },
128 	{ "nullrecv",	KSTAT_DATA_UINT64 },
129 	{ "badlen",	KSTAT_DATA_UINT64 },
130 	{ "xdrcall",	KSTAT_DATA_UINT64 },
131 	{ "dupchecks",	KSTAT_DATA_UINT64 },
132 	{ "dupreqs",	KSTAT_DATA_UINT64 }
133 };
134 
135 static uint_t clts_rsstat_ndata =
136 	sizeof (clts_rsstat_tmpl) / sizeof (kstat_named_t);
137 
138 #define	CLONE2STATS(clone_xprt)	\
139 	(struct rpc_clts_server *)(clone_xprt)->xp_master->xp_p2
140 
141 #define	RSSTAT_INCR(stats, x)	\
142 	atomic_add_64(&(stats)->x.value.ui64, 1)
143 
144 /*
145  * Create a transport record.
146  * The transport record, output buffer, and private data structure
147  * are allocated.  The output buffer is serialized into using xdrmem.
148  * There is one transport record per user process which implements a
149  * set of services.
150  */
151 /* ARGSUSED */
152 int
153 svc_clts_kcreate(file_t *fp, uint_t sendsz, struct T_info_ack *tinfo,
154     SVCMASTERXPRT **nxprt)
155 {
156 	SVCMASTERXPRT *xprt;
157 	struct rpcstat *rpcstat;
158 
159 	if (nxprt == NULL)
160 		return (EINVAL);
161 
162 	rpcstat = zone_getspecific(rpcstat_zone_key, curproc->p_zone);
163 	ASSERT(rpcstat != NULL);
164 
165 	xprt = kmem_zalloc(sizeof (*xprt), KM_SLEEP);
166 	xprt->xp_p2 = (caddr_t)rpcstat->rpc_clts_server;
167 	xprt->xp_ops = &svc_clts_op;
168 	xprt->xp_msg_size = tinfo->TSDU_size;
169 
170 	xprt->xp_rtaddr.buf = NULL;
171 	xprt->xp_rtaddr.maxlen = tinfo->ADDR_size;
172 	xprt->xp_rtaddr.len = 0;
173 
174 	*nxprt = xprt;
175 
176 	return (0);
177 }
178 
179 /*
180  * Destroy a transport record.
181  * Frees the space allocated for a transport record.
182  */
183 static void
184 svc_clts_kdestroy(SVCMASTERXPRT *xprt)
185 {
186 	if (xprt->xp_netid)
187 		kmem_free(xprt->xp_netid, strlen(xprt->xp_netid) + 1);
188 	if (xprt->xp_addrmask.maxlen)
189 		kmem_free(xprt->xp_addrmask.buf, xprt->xp_addrmask.maxlen);
190 
191 	mutex_destroy(&xprt->xp_req_lock);
192 	mutex_destroy(&xprt->xp_thread_lock);
193 
194 	kmem_free(xprt, sizeof (SVCMASTERXPRT));
195 }
196 
197 /*
198  * Transport-type specific part of svc_xprt_cleanup().
199  * Frees the message buffer space allocated for a clone of a transport record
200  */
201 static void
202 svc_clts_kclone_destroy(SVCXPRT *clone_xprt)
203 {
204 	/* LINTED pointer alignment */
205 	struct udp_data *ud = (struct udp_data *)clone_xprt->xp_p2buf;
206 
207 	if (ud->ud_resp) {
208 		/*
209 		 * There should not be any left over results buffer.
210 		 */
211 		ASSERT(ud->ud_resp->b_cont == NULL);
212 
213 		/*
214 		 * Free the T_UNITDATA_{REQ/IND} that svc_clts_krecv
215 		 * saved.
216 		 */
217 		freeb(ud->ud_resp);
218 	}
219 	if (ud->ud_inmp)
220 		freemsg(ud->ud_inmp);
221 }
222 
223 /*
224  * svc_tli_kcreate() calls this function at the end to tell
225  * rpcmod that the transport is ready to receive requests.
226  */
227 /* ARGSUSED */
228 static void
229 svc_clts_kstart(SVCMASTERXPRT *xprt)
230 {
231 }
232 
233 /*
234  * Receive rpc requests.
235  * Pulls a request in off the socket, checks if the packet is intact,
236  * and deserializes the call packet.
237  */
238 static bool_t
239 svc_clts_krecv(SVCXPRT *clone_xprt, mblk_t *mp, struct rpc_msg *msg)
240 {
241 	/* LINTED pointer alignment */
242 	struct udp_data *ud = (struct udp_data *)clone_xprt->xp_p2buf;
243 	XDR *xdrs = &clone_xprt->xp_xdrin;
244 	struct rpc_clts_server *stats = CLONE2STATS(clone_xprt);
245 	union T_primitives *pptr;
246 	int hdrsz;
247 
248 	TRACE_0(TR_FAC_KRPC, TR_SVC_CLTS_KRECV_START,
249 	    "svc_clts_krecv_start:");
250 
251 	RSSTAT_INCR(stats, rscalls);
252 
253 	/*
254 	 * The incoming request should start with an M_PROTO message.
255 	 */
256 	if (mp->b_datap->db_type != M_PROTO) {
257 		goto bad;
258 	}
259 
260 	/*
261 	 * The incoming request should be an T_UNITDTA_IND.  There
262 	 * might be other messages coming up the stream, but we can
263 	 * ignore them.
264 	 */
265 	pptr = (union T_primitives *)mp->b_rptr;
266 	if (pptr->type != T_UNITDATA_IND) {
267 		goto bad;
268 	}
269 	/*
270 	 * Do some checking to make sure that the header at least looks okay.
271 	 */
272 	hdrsz = (int)(mp->b_wptr - mp->b_rptr);
273 	if (hdrsz < TUNITDATAINDSZ ||
274 	    hdrsz < (pptr->unitdata_ind.OPT_offset +
275 		    pptr->unitdata_ind.OPT_length) ||
276 	    hdrsz < (pptr->unitdata_ind.SRC_offset +
277 		    pptr->unitdata_ind.SRC_length)) {
278 		goto bad;
279 	}
280 
281 	/*
282 	 * Make sure that the transport provided a usable address.
283 	 */
284 	if (pptr->unitdata_ind.SRC_length <= 0) {
285 		goto bad;
286 	}
287 	/*
288 	 * Point the remote transport address in the service_transport
289 	 * handle at the address in the request.
290 	 */
291 	clone_xprt->xp_rtaddr.buf = (char *)mp->b_rptr +
292 	    pptr->unitdata_ind.SRC_offset;
293 	clone_xprt->xp_rtaddr.len = pptr->unitdata_ind.SRC_length;
294 
295 	/*
296 	 * Save the first mblk which contains the T_unidata_ind in
297 	 * ud_resp.  It will be used to generate the T_unitdata_req
298 	 * during the reply.
299 	 */
300 	if (ud->ud_resp) {
301 		if (ud->ud_resp->b_cont != NULL) {
302 			cmn_err(CE_WARN, "svc_clts_krecv: ud_resp %p, "
303 			    "b_cont %p", (void *)ud->ud_resp,
304 			    (void *)ud->ud_resp->b_cont);
305 		}
306 		freeb(ud->ud_resp);
307 	}
308 	ud->ud_resp = mp;
309 	mp = mp->b_cont;
310 	ud->ud_resp->b_cont = NULL;
311 
312 	xdrmblk_init(xdrs, mp, XDR_DECODE, 0);
313 
314 	TRACE_0(TR_FAC_KRPC, TR_XDR_CALLMSG_START,
315 	    "xdr_callmsg_start:");
316 	if (! xdr_callmsg(xdrs, msg)) {
317 		TRACE_1(TR_FAC_KRPC, TR_XDR_CALLMSG_END,
318 		    "xdr_callmsg_end:(%S)", "bad");
319 		RSSTAT_INCR(stats, rsxdrcall);
320 		goto bad;
321 	}
322 	TRACE_1(TR_FAC_KRPC, TR_XDR_CALLMSG_END,
323 	    "xdr_callmsg_end:(%S)", "good");
324 
325 	clone_xprt->xp_xid = msg->rm_xid;
326 	ud->ud_inmp = mp;
327 
328 	TRACE_1(TR_FAC_KRPC, TR_SVC_CLTS_KRECV_END,
329 	    "svc_clts_krecv_end:(%S)", "good");
330 	return (TRUE);
331 
332 bad:
333 	if (mp)
334 		freemsg(mp);
335 	if (ud->ud_resp) {
336 		/*
337 		 * There should not be any left over results buffer.
338 		 */
339 		ASSERT(ud->ud_resp->b_cont == NULL);
340 		freeb(ud->ud_resp);
341 		ud->ud_resp = NULL;
342 	}
343 
344 	RSSTAT_INCR(stats, rsbadcalls);
345 	TRACE_1(TR_FAC_KRPC, TR_SVC_CLTS_KRECV_END,
346 	    "svc_clts_krecv_end:(%S)", "bad");
347 	return (FALSE);
348 }
349 
350 /*
351  * Send rpc reply.
352  * Serialize the reply packet into the output buffer then
353  * call t_ksndudata to send it.
354  */
355 static bool_t
356 svc_clts_ksend(SVCXPRT *clone_xprt, struct rpc_msg *msg)
357 {
358 	/* LINTED pointer alignment */
359 	struct udp_data *ud = (struct udp_data *)clone_xprt->xp_p2buf;
360 	XDR *xdrs = &clone_xprt->xp_xdrout;
361 	int stat = FALSE;
362 	mblk_t *mp;
363 	int msgsz;
364 	struct T_unitdata_req *udreq;
365 	xdrproc_t xdr_results;
366 	caddr_t xdr_location;
367 	bool_t has_args;
368 
369 	TRACE_0(TR_FAC_KRPC, TR_SVC_CLTS_KSEND_START,
370 	    "svc_clts_ksend_start:");
371 
372 	ASSERT(ud->ud_resp != NULL);
373 
374 	/*
375 	 * If there is a result procedure specified in the reply message,
376 	 * it will be processed in the xdr_replymsg and SVCAUTH_WRAP.
377 	 * We need to make sure it won't be processed twice, so we null
378 	 * it for xdr_replymsg here.
379 	 */
380 	has_args = FALSE;
381 	if (msg->rm_reply.rp_stat == MSG_ACCEPTED &&
382 		msg->rm_reply.rp_acpt.ar_stat == SUCCESS) {
383 		if ((xdr_results = msg->acpted_rply.ar_results.proc) != NULL) {
384 			has_args = TRUE;
385 			xdr_location = msg->acpted_rply.ar_results.where;
386 			msg->acpted_rply.ar_results.proc = xdr_void;
387 			msg->acpted_rply.ar_results.where = NULL;
388 		}
389 	}
390 
391 	if (ud->ud_resp->b_cont == NULL) {
392 		/*
393 		 * Allocate an initial mblk for the response data.
394 		 */
395 		while ((mp = allocb(UD_INITSIZE, BPRI_LO)) == NULL) {
396 			if (strwaitbuf(UD_INITSIZE, BPRI_LO)) {
397 				TRACE_1(TR_FAC_KRPC, TR_SVC_CLTS_KSEND_END,
398 				    "svc_clts_ksend_end:(%S)", "strwaitbuf");
399 				return (FALSE);
400 			}
401 		}
402 
403 		/*
404 		 * Initialize the XDR decode stream.  Additional mblks
405 		 * will be allocated if necessary.  They will be UD_MAXSIZE
406 		 * sized.
407 		 */
408 		xdrmblk_init(xdrs, mp, XDR_ENCODE, UD_MAXSIZE);
409 
410 		/*
411 		 * Leave some space for protocol headers.
412 		 */
413 		(void) XDR_SETPOS(xdrs, 512);
414 		mp->b_rptr += 512;
415 
416 		msg->rm_xid = clone_xprt->xp_xid;
417 
418 		ud->ud_resp->b_cont = mp;
419 
420 		TRACE_0(TR_FAC_KRPC, TR_XDR_REPLYMSG_START,
421 		    "xdr_replymsg_start:");
422 		if (!(xdr_replymsg(xdrs, msg) &&
423 			(!has_args || SVCAUTH_WRAP(&clone_xprt->xp_auth, xdrs,
424 				xdr_results, xdr_location)))) {
425 			TRACE_1(TR_FAC_KRPC, TR_XDR_REPLYMSG_END,
426 			    "xdr_replymsg_end:(%S)", "bad");
427 			RPCLOG0(1, "xdr_replymsg/SVCAUTH_WRAP failed\n");
428 			goto out;
429 		}
430 		TRACE_1(TR_FAC_KRPC, TR_XDR_REPLYMSG_END,
431 		    "xdr_replymsg_end:(%S)", "good");
432 
433 	} else if (!(xdr_replymsg_body(xdrs, msg) &&
434 		    (!has_args || SVCAUTH_WRAP(&clone_xprt->xp_auth, xdrs,
435 				xdr_results, xdr_location)))) {
436 		RPCLOG0(1, "xdr_replymsg_body/SVCAUTH_WRAP failed\n");
437 		goto out;
438 	}
439 
440 	msgsz = (int)xmsgsize(ud->ud_resp->b_cont);
441 
442 	if (msgsz <= 0 || (clone_xprt->xp_msg_size != -1 &&
443 	    msgsz > clone_xprt->xp_msg_size)) {
444 #ifdef	DEBUG
445 		cmn_err(CE_NOTE,
446 "KRPC: server response message of %d bytes; transport limits are [0, %d]",
447 			msgsz, clone_xprt->xp_msg_size);
448 #endif
449 		goto out;
450 	}
451 
452 	/*
453 	 * Construct the T_unitdata_req.  We take advantage
454 	 * of the fact that T_unitdata_ind looks just like
455 	 * T_unitdata_req, except for the primitive type.
456 	 */
457 	udreq = (struct T_unitdata_req *)ud->ud_resp->b_rptr;
458 	udreq->PRIM_type = T_UNITDATA_REQ;
459 
460 	put(clone_xprt->xp_wq, ud->ud_resp);
461 	stat = TRUE;
462 	ud->ud_resp = NULL;
463 
464 out:
465 	if (stat == FALSE) {
466 		freemsg(ud->ud_resp);
467 		ud->ud_resp = NULL;
468 	}
469 
470 	/*
471 	 * This is completely disgusting.  If public is set it is
472 	 * a pointer to a structure whose first field is the address
473 	 * of the function to free that structure and any related
474 	 * stuff.  (see rrokfree in nfs_xdr.c).
475 	 */
476 	if (xdrs->x_public) {
477 		/* LINTED pointer alignment */
478 		(**((int (**)())xdrs->x_public))(xdrs->x_public);
479 	}
480 
481 	TRACE_1(TR_FAC_KRPC, TR_SVC_CLTS_KSEND_END,
482 	    "svc_clts_ksend_end:(%S)", "done");
483 	return (stat);
484 }
485 
486 /*
487  * Deserialize arguments.
488  */
489 static bool_t
490 svc_clts_kgetargs(SVCXPRT *clone_xprt, xdrproc_t xdr_args,
491     caddr_t args_ptr)
492 {
493 
494 	/* LINTED pointer alignment */
495 	return (SVCAUTH_UNWRAP(&clone_xprt->xp_auth, &clone_xprt->xp_xdrin,
496 				xdr_args, args_ptr));
497 
498 }
499 
500 static bool_t
501 svc_clts_kfreeargs(SVCXPRT *clone_xprt, xdrproc_t xdr_args,
502     caddr_t args_ptr)
503 {
504 	/* LINTED pointer alignment */
505 	struct udp_data *ud = (struct udp_data *)clone_xprt->xp_p2buf;
506 	XDR *xdrs = &clone_xprt->xp_xdrin;
507 	bool_t retval;
508 
509 	if (args_ptr) {
510 		xdrs->x_op = XDR_FREE;
511 		retval = (*xdr_args)(xdrs, args_ptr);
512 	} else
513 		retval = TRUE;
514 
515 	if (ud->ud_inmp) {
516 		freemsg(ud->ud_inmp);
517 		ud->ud_inmp = NULL;
518 	}
519 
520 	return (retval);
521 }
522 
523 static int32_t *
524 svc_clts_kgetres(SVCXPRT *clone_xprt, int size)
525 {
526 	/* LINTED pointer alignment */
527 	struct udp_data *ud = (struct udp_data *)clone_xprt->xp_p2buf;
528 	XDR *xdrs = &clone_xprt->xp_xdrout;
529 	mblk_t *mp;
530 	int32_t *buf;
531 	struct rpc_msg rply;
532 
533 	/*
534 	 * Allocate an initial mblk for the response data.
535 	 */
536 	while ((mp = allocb(UD_INITSIZE, BPRI_LO)) == NULL) {
537 		if (strwaitbuf(UD_INITSIZE, BPRI_LO)) {
538 			return (FALSE);
539 		}
540 	}
541 
542 	mp->b_cont = NULL;
543 
544 	/*
545 	 * Initialize the XDR decode stream.  Additional mblks
546 	 * will be allocated if necessary.  They will be UD_MAXSIZE
547 	 * sized.
548 	 */
549 	xdrmblk_init(xdrs, mp, XDR_ENCODE, UD_MAXSIZE);
550 
551 	/*
552 	 * Leave some space for protocol headers.
553 	 */
554 	(void) XDR_SETPOS(xdrs, 512);
555 	mp->b_rptr += 512;
556 
557 	/*
558 	 * Assume a successful RPC since most of them are.
559 	 */
560 	rply.rm_xid = clone_xprt->xp_xid;
561 	rply.rm_direction = REPLY;
562 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
563 	rply.acpted_rply.ar_verf = clone_xprt->xp_verf;
564 	rply.acpted_rply.ar_stat = SUCCESS;
565 
566 	if (!xdr_replymsg_hdr(xdrs, &rply)) {
567 		freeb(mp);
568 		return (NULL);
569 	}
570 
571 	buf = XDR_INLINE(xdrs, size);
572 
573 	if (buf == NULL)
574 		freeb(mp);
575 	else
576 		ud->ud_resp->b_cont = mp;
577 
578 	return (buf);
579 }
580 
581 static void
582 svc_clts_kfreeres(SVCXPRT *clone_xprt)
583 {
584 	/* LINTED pointer alignment */
585 	struct udp_data *ud = (struct udp_data *)clone_xprt->xp_p2buf;
586 
587 	if (ud->ud_resp == NULL || ud->ud_resp->b_cont == NULL)
588 		return;
589 
590 	/*
591 	 * SVC_FREERES() is called whenever the server decides not to
592 	 * send normal reply. Thus, we expect only one mblk to be allocated,
593 	 * because we have not attempted any XDR encoding.
594 	 * If we do any XDR encoding and we get an error, then SVC_REPLY()
595 	 * will freemsg(ud->ud_resp);
596 	 */
597 	ASSERT(ud->ud_resp->b_cont->b_cont == NULL);
598 	freeb(ud->ud_resp->b_cont);
599 	ud->ud_resp->b_cont = NULL;
600 }
601 
602 /*
603  * the dup cacheing routines below provide a cache of non-failure
604  * transaction id's.  rpc service routines can use this to detect
605  * retransmissions and re-send a non-failure response.
606  */
607 
608 /*
609  * MAXDUPREQS is the number of cached items.  It should be adjusted
610  * to the service load so that there is likely to be a response entry
611  * when the first retransmission comes in.
612  */
613 #define	MAXDUPREQS	1024
614 
615 /*
616  * This should be appropriately scaled to MAXDUPREQS.
617  */
618 #define	DRHASHSZ	257
619 
620 #if ((DRHASHSZ & (DRHASHSZ - 1)) == 0)
621 #define	XIDHASH(xid)	((xid) & (DRHASHSZ - 1))
622 #else
623 #define	XIDHASH(xid)	((xid) % DRHASHSZ)
624 #endif
625 #define	DRHASH(dr)	XIDHASH((dr)->dr_xid)
626 #define	REQTOXID(req)	((req)->rq_xprt->xp_xid)
627 
628 static int	ndupreqs = 0;
629 static int	maxdupreqs = MAXDUPREQS;
630 static kmutex_t dupreq_lock;
631 static struct dupreq *drhashtbl[DRHASHSZ];
632 static int	drhashstat[DRHASHSZ];
633 
634 static void unhash(struct dupreq *);
635 
636 /*
637  * drmru points to the head of a circular linked list in lru order.
638  * drmru->dr_next == drlru
639  */
640 struct dupreq *drmru;
641 
642 /*
643  * PSARC 2003/523 Contract Private Interface
644  * svc_clts_kdup
645  * Changes must be reviewed by Solaris File Sharing
646  * Changes must be communicated to contract-2003-523@sun.com
647  *
648  * svc_clts_kdup searches the request cache and returns 0 if the
649  * request is not found in the cache.  If it is found, then it
650  * returns the state of the request (in progress or done) and
651  * the status or attributes that were part of the original reply.
652  *
653  * If DUP_DONE (there is a duplicate) svc_clts_kdup copies over the
654  * value of the response. In that case, also return in *dupcachedp
655  * whether the response free routine is cached in the dupreq - in which case
656  * the caller should not be freeing it, because it will be done later
657  * in the svc_clts_kdup code when the dupreq is reused.
658  */
659 static int
660 svc_clts_kdup(struct svc_req *req, caddr_t res, int size, struct dupreq **drpp,
661 	bool_t *dupcachedp)
662 {
663 	struct rpc_clts_server *stats = CLONE2STATS(req->rq_xprt);
664 	struct dupreq *dr;
665 	uint32_t xid;
666 	uint32_t drhash;
667 	int status;
668 
669 	xid = REQTOXID(req);
670 	mutex_enter(&dupreq_lock);
671 	RSSTAT_INCR(stats, rsdupchecks);
672 	/*
673 	 * Check to see whether an entry already exists in the cache.
674 	 */
675 	dr = drhashtbl[XIDHASH(xid)];
676 	while (dr != NULL) {
677 		if (dr->dr_xid == xid &&
678 		    dr->dr_proc == req->rq_proc &&
679 		    dr->dr_prog == req->rq_prog &&
680 		    dr->dr_vers == req->rq_vers &&
681 		    dr->dr_addr.len == req->rq_xprt->xp_rtaddr.len &&
682 		    bcmp(dr->dr_addr.buf, req->rq_xprt->xp_rtaddr.buf,
683 		    dr->dr_addr.len) == 0) {
684 			status = dr->dr_status;
685 			if (status == DUP_DONE) {
686 				bcopy(dr->dr_resp.buf, res, size);
687 				if (dupcachedp != NULL)
688 					*dupcachedp = (dr->dr_resfree != NULL);
689 			} else {
690 				dr->dr_status = DUP_INPROGRESS;
691 				*drpp = dr;
692 			}
693 			RSSTAT_INCR(stats, rsdupreqs);
694 			mutex_exit(&dupreq_lock);
695 			return (status);
696 		}
697 		dr = dr->dr_chain;
698 	}
699 
700 	/*
701 	 * There wasn't an entry, either allocate a new one or recycle
702 	 * an old one.
703 	 */
704 	if (ndupreqs < maxdupreqs) {
705 		dr = kmem_alloc(sizeof (*dr), KM_NOSLEEP);
706 		if (dr == NULL) {
707 			mutex_exit(&dupreq_lock);
708 			return (DUP_ERROR);
709 		}
710 		dr->dr_resp.buf = NULL;
711 		dr->dr_resp.maxlen = 0;
712 		dr->dr_addr.buf = NULL;
713 		dr->dr_addr.maxlen = 0;
714 		if (drmru) {
715 			dr->dr_next = drmru->dr_next;
716 			drmru->dr_next = dr;
717 		} else {
718 			dr->dr_next = dr;
719 		}
720 		ndupreqs++;
721 	} else {
722 		dr = drmru->dr_next;
723 		while (dr->dr_status == DUP_INPROGRESS) {
724 			dr = dr->dr_next;
725 			if (dr == drmru->dr_next) {
726 				cmn_err(CE_WARN, "svc_clts_kdup no slots free");
727 				mutex_exit(&dupreq_lock);
728 				return (DUP_ERROR);
729 			}
730 		}
731 		unhash(dr);
732 		if (dr->dr_resfree) {
733 			(*dr->dr_resfree)(dr->dr_resp.buf);
734 		}
735 	}
736 	dr->dr_resfree = NULL;
737 	drmru = dr;
738 
739 	dr->dr_xid = REQTOXID(req);
740 	dr->dr_prog = req->rq_prog;
741 	dr->dr_vers = req->rq_vers;
742 	dr->dr_proc = req->rq_proc;
743 	if (dr->dr_addr.maxlen < req->rq_xprt->xp_rtaddr.len) {
744 		if (dr->dr_addr.buf != NULL)
745 			kmem_free(dr->dr_addr.buf, dr->dr_addr.maxlen);
746 		dr->dr_addr.maxlen = req->rq_xprt->xp_rtaddr.len;
747 		dr->dr_addr.buf = kmem_alloc(dr->dr_addr.maxlen,
748 		    KM_NOSLEEP);
749 		if (dr->dr_addr.buf == NULL) {
750 			dr->dr_addr.maxlen = 0;
751 			dr->dr_status = DUP_DROP;
752 			mutex_exit(&dupreq_lock);
753 			return (DUP_ERROR);
754 		}
755 	}
756 	dr->dr_addr.len = req->rq_xprt->xp_rtaddr.len;
757 	bcopy(req->rq_xprt->xp_rtaddr.buf, dr->dr_addr.buf, dr->dr_addr.len);
758 	if (dr->dr_resp.maxlen < size) {
759 		if (dr->dr_resp.buf != NULL)
760 			kmem_free(dr->dr_resp.buf, dr->dr_resp.maxlen);
761 		dr->dr_resp.maxlen = (unsigned int)size;
762 		dr->dr_resp.buf = kmem_alloc(size, KM_NOSLEEP);
763 		if (dr->dr_resp.buf == NULL) {
764 			dr->dr_resp.maxlen = 0;
765 			dr->dr_status = DUP_DROP;
766 			mutex_exit(&dupreq_lock);
767 			return (DUP_ERROR);
768 		}
769 	}
770 	dr->dr_status = DUP_INPROGRESS;
771 
772 	drhash = (uint32_t)DRHASH(dr);
773 	dr->dr_chain = drhashtbl[drhash];
774 	drhashtbl[drhash] = dr;
775 	drhashstat[drhash]++;
776 	mutex_exit(&dupreq_lock);
777 	*drpp = dr;
778 	return (DUP_NEW);
779 }
780 
781 /*
782  * PSARC 2003/523 Contract Private Interface
783  * svc_clts_kdupdone
784  * Changes must be reviewed by Solaris File Sharing
785  * Changes must be communicated to contract-2003-523@sun.com
786  *
787  * svc_clts_kdupdone marks the request done (DUP_DONE or DUP_DROP)
788  * and stores the response.
789  */
790 static void
791 svc_clts_kdupdone(struct dupreq *dr, caddr_t res, void (*dis_resfree)(),
792 	int size, int status)
793 {
794 
795 	ASSERT(dr->dr_resfree == NULL);
796 	if (status == DUP_DONE) {
797 		bcopy(res, dr->dr_resp.buf, size);
798 		dr->dr_resfree = dis_resfree;
799 	}
800 	dr->dr_status = status;
801 }
802 
803 /*
804  * This routine expects that the mutex, dupreq_lock, is already held.
805  */
806 static void
807 unhash(struct dupreq *dr)
808 {
809 	struct dupreq *drt;
810 	struct dupreq *drtprev = NULL;
811 	uint32_t drhash;
812 
813 	ASSERT(MUTEX_HELD(&dupreq_lock));
814 
815 	drhash = (uint32_t)DRHASH(dr);
816 	drt = drhashtbl[drhash];
817 	while (drt != NULL) {
818 		if (drt == dr) {
819 			drhashstat[drhash]--;
820 			if (drtprev == NULL) {
821 				drhashtbl[drhash] = drt->dr_chain;
822 			} else {
823 				drtprev->dr_chain = drt->dr_chain;
824 			}
825 			return;
826 		}
827 		drtprev = drt;
828 		drt = drt->dr_chain;
829 	}
830 }
831 
832 void
833 svc_clts_stats_init(zoneid_t zoneid, struct rpc_clts_server **statsp)
834 {
835 	kstat_t *ksp;
836 	kstat_named_t *knp;
837 
838 	knp = rpcstat_zone_init_common(zoneid, "unix", "rpc_clts_server",
839 	    (const kstat_named_t *)&clts_rsstat_tmpl,
840 	    sizeof (clts_rsstat_tmpl));
841 	/*
842 	 * Backwards compatibility for old kstat clients
843 	 */
844 	ksp = kstat_create_zone("unix", 0, "rpc_server", "rpc",
845 	    KSTAT_TYPE_NAMED, clts_rsstat_ndata,
846 	    KSTAT_FLAG_VIRTUAL | KSTAT_FLAG_WRITABLE, zoneid);
847 	if (ksp) {
848 		ksp->ks_data = knp;
849 		kstat_install(ksp);
850 	}
851 	*statsp = (struct rpc_clts_server *)knp;
852 }
853 
854 void
855 svc_clts_stats_fini(zoneid_t zoneid, struct rpc_clts_server **statsp)
856 {
857 	rpcstat_zone_fini_common(zoneid, "unix", "rpc_clts_server");
858 	kstat_delete_byname_zone("unix", 0, "rpc_server", zoneid);
859 	kmem_free(*statsp, sizeof (clts_rsstat_tmpl));
860 }
861 
862 void
863 svc_clts_init()
864 {
865 	/*
866 	 * Check to make sure that the clts private data will fit into
867 	 * the stack buffer allocated by svc_run.  The compiler should
868 	 * remove this check, but it's a safety net if the udp_data
869 	 * structure ever changes.
870 	 */
871 	/*CONSTANTCONDITION*/
872 	ASSERT(sizeof (struct udp_data) <= SVC_P2LEN);
873 
874 	mutex_init(&dupreq_lock, NULL, MUTEX_DEFAULT, NULL);
875 }
876