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