xref: /illumos-gate/usr/src/uts/common/rpc/rpcmod.c (revision ac2250cb76bb32944fd2c8a3ba2cd3f79747748d)
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 2010 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /*
26  * Copyright 2012 Milan Jurik. All rights reserved.
27  * Copyright 2012 Marcel Telka <marcel@telka.sk>
28  * Copyright 2018 OmniOS Community Edition (OmniOSce) Association.
29  * Copyright 2020 Tintri by DDN. All rights reserved.
30  */
31 /* Copyright (c) 1990 Mentat Inc. */
32 
33 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
34 /*	All Rights Reserved	*/
35 
36 /*
37  * Kernel RPC filtering module
38  */
39 
40 #include <sys/param.h>
41 #include <sys/types.h>
42 #include <sys/stream.h>
43 #include <sys/stropts.h>
44 #include <sys/strsubr.h>
45 #include <sys/tihdr.h>
46 #include <sys/timod.h>
47 #include <sys/tiuser.h>
48 #include <sys/debug.h>
49 #include <sys/signal.h>
50 #include <sys/pcb.h>
51 #include <sys/user.h>
52 #include <sys/errno.h>
53 #include <sys/cred.h>
54 #include <sys/policy.h>
55 #include <sys/inline.h>
56 #include <sys/cmn_err.h>
57 #include <sys/kmem.h>
58 #include <sys/file.h>
59 #include <sys/sysmacros.h>
60 #include <sys/systm.h>
61 #include <sys/t_lock.h>
62 #include <sys/ddi.h>
63 #include <sys/vtrace.h>
64 #include <sys/callb.h>
65 #include <sys/strsun.h>
66 
67 #include <sys/strlog.h>
68 #include <rpc/rpc_com.h>
69 #include <inet/common.h>
70 #include <rpc/types.h>
71 #include <sys/time.h>
72 #include <rpc/xdr.h>
73 #include <rpc/auth.h>
74 #include <rpc/clnt.h>
75 #include <rpc/rpc_msg.h>
76 #include <rpc/clnt.h>
77 #include <rpc/svc.h>
78 #include <rpc/rpcsys.h>
79 #include <rpc/rpc_rdma.h>
80 
81 /*
82  * This is the loadable module wrapper.
83  */
84 #include <sys/conf.h>
85 #include <sys/modctl.h>
86 #include <sys/syscall.h>
87 
88 extern struct streamtab rpcinfo;
89 
90 static struct fmodsw fsw = {
91 	"rpcmod",
92 	&rpcinfo,
93 	D_NEW|D_MP,
94 };
95 
96 /*
97  * Module linkage information for the kernel.
98  */
99 
100 static struct modlstrmod modlstrmod = {
101 	&mod_strmodops, "rpc interface str mod", &fsw
102 };
103 
104 /*
105  * For the RPC system call.
106  */
107 static struct sysent rpcsysent = {
108 	2,
109 	SE_32RVAL1 | SE_ARGC | SE_NOUNLOAD,
110 	rpcsys
111 };
112 
113 static struct modlsys modlsys = {
114 	&mod_syscallops,
115 	"RPC syscall",
116 	&rpcsysent
117 };
118 
119 #ifdef _SYSCALL32_IMPL
120 static struct modlsys modlsys32 = {
121 	&mod_syscallops32,
122 	"32-bit RPC syscall",
123 	&rpcsysent
124 };
125 #endif /* _SYSCALL32_IMPL */
126 
127 static struct modlinkage modlinkage = {
128 	MODREV_1,
129 	{
130 		&modlsys,
131 #ifdef _SYSCALL32_IMPL
132 		&modlsys32,
133 #endif
134 		&modlstrmod,
135 		NULL
136 	}
137 };
138 
139 int
140 _init(void)
141 {
142 	int error = 0;
143 	callb_id_t cid;
144 	int status;
145 
146 	svc_init();
147 	clnt_init();
148 	cid = callb_add(connmgr_cpr_reset, 0, CB_CL_CPR_RPC, "rpc");
149 
150 	if (error = mod_install(&modlinkage)) {
151 		/*
152 		 * Could not install module, cleanup previous
153 		 * initialization work.
154 		 */
155 		clnt_fini();
156 		if (cid != NULL)
157 			(void) callb_delete(cid);
158 
159 		return (error);
160 	}
161 
162 	/*
163 	 * Load up the RDMA plugins and initialize the stats. Even if the
164 	 * plugins loadup fails, but rpcmod was successfully installed the
165 	 * counters still get initialized.
166 	 */
167 	rw_init(&rdma_lock, NULL, RW_DEFAULT, NULL);
168 	mutex_init(&rdma_modload_lock, NULL, MUTEX_DEFAULT, NULL);
169 
170 	cv_init(&rdma_wait.svc_cv, NULL, CV_DEFAULT, NULL);
171 	mutex_init(&rdma_wait.svc_lock, NULL, MUTEX_DEFAULT, NULL);
172 
173 	mt_kstat_init();
174 
175 	/*
176 	 * Get our identification into ldi.  This is used for loading
177 	 * other modules, e.g. rpcib.
178 	 */
179 	status = ldi_ident_from_mod(&modlinkage, &rpcmod_li);
180 	if (status != 0) {
181 		cmn_err(CE_WARN, "ldi_ident_from_mod fails with %d", status);
182 		rpcmod_li = NULL;
183 	}
184 
185 	return (error);
186 }
187 
188 /*
189  * The unload entry point fails, because we advertise entry points into
190  * rpcmod from the rest of kRPC: rpcmod_release().
191  */
192 int
193 _fini(void)
194 {
195 	return (EBUSY);
196 }
197 
198 int
199 _info(struct modinfo *modinfop)
200 {
201 	return (mod_info(&modlinkage, modinfop));
202 }
203 
204 extern int nulldev();
205 
206 #define	RPCMOD_ID	2049
207 
208 int rmm_open(queue_t *, dev_t *, int, int, cred_t *);
209 int rmm_close(queue_t *, int, cred_t *);
210 
211 /*
212  * To save instructions, since STREAMS ignores the return value
213  * from these functions, they are defined as void here. Kind of icky, but...
214  */
215 int rmm_rput(queue_t *, mblk_t *);
216 int rmm_wput(queue_t *, mblk_t *);
217 int rmm_rsrv(queue_t *);
218 int rmm_wsrv(queue_t *);
219 
220 int rpcmodopen(queue_t *, dev_t *, int, int, cred_t *);
221 int rpcmodclose(queue_t *, int, cred_t *);
222 void rpcmodrput(queue_t *, mblk_t *);
223 void rpcmodwput(queue_t *, mblk_t *);
224 void rpcmodrsrv();
225 void rpcmodwsrv(queue_t *);
226 
227 static	void	rpcmodwput_other(queue_t *, mblk_t *);
228 static	int	mir_close(queue_t *q);
229 static	int	mir_open(queue_t *q, dev_t *devp, int flag, int sflag,
230 		    cred_t *credp);
231 static	void	mir_rput(queue_t *q, mblk_t *mp);
232 static	void	mir_rsrv(queue_t *q);
233 static	void	mir_wput(queue_t *q, mblk_t *mp);
234 static	void	mir_wsrv(queue_t *q);
235 
236 static struct module_info rpcmod_info =
237 	{RPCMOD_ID, "rpcmod", 0, INFPSZ, 256*1024, 1024};
238 
239 static struct qinit rpcmodrinit = {
240 	rmm_rput,
241 	rmm_rsrv,
242 	rmm_open,
243 	rmm_close,
244 	nulldev,
245 	&rpcmod_info,
246 	NULL
247 };
248 
249 /*
250  * The write put procedure is simply putnext to conserve stack space.
251  * The write service procedure is not used to queue data, but instead to
252  * synchronize with flow control.
253  */
254 static struct qinit rpcmodwinit = {
255 	rmm_wput,
256 	rmm_wsrv,
257 	rmm_open,
258 	rmm_close,
259 	nulldev,
260 	&rpcmod_info,
261 	NULL
262 };
263 struct streamtab rpcinfo = { &rpcmodrinit, &rpcmodwinit, NULL, NULL };
264 
265 struct xprt_style_ops {
266 	int (*xo_open)();
267 	int (*xo_close)();
268 	void (*xo_wput)();
269 	void (*xo_wsrv)();
270 	void (*xo_rput)();
271 	void (*xo_rsrv)();
272 };
273 
274 /*
275  * Read side has no service procedure.
276  */
277 static struct xprt_style_ops xprt_clts_ops = {
278 	rpcmodopen,
279 	rpcmodclose,
280 	rpcmodwput,
281 	rpcmodwsrv,
282 	rpcmodrput,
283 	NULL
284 };
285 
286 static struct xprt_style_ops xprt_cots_ops = {
287 	mir_open,
288 	mir_close,
289 	mir_wput,
290 	mir_wsrv,
291 	mir_rput,
292 	mir_rsrv
293 };
294 
295 /*
296  * Per rpcmod "slot" data structure. q->q_ptr points to one of these.
297  */
298 struct rpcm {
299 	void		*rm_krpc_cell;	/* Reserved for use by kRPC */
300 	struct		xprt_style_ops	*rm_ops;
301 	int		rm_type;	/* Client or server side stream */
302 #define	RM_CLOSING	0x1		/* somebody is trying to close slot */
303 	uint_t		rm_state;	/* state of the slot. see above */
304 	uint_t		rm_ref;		/* cnt of external references to slot */
305 	kmutex_t	rm_lock;	/* mutex protecting above fields */
306 	kcondvar_t	rm_cwait;	/* condition for closing */
307 	zoneid_t	rm_zoneid;	/* zone which pushed rpcmod */
308 };
309 
310 struct temp_slot {
311 	void *cell;
312 	struct xprt_style_ops *ops;
313 	int type;
314 	mblk_t *info_ack;
315 	kmutex_t lock;
316 	kcondvar_t wait;
317 };
318 
319 typedef struct mir_s {
320 	void	*mir_krpc_cell;	/* Reserved for kRPC use. This field */
321 					/* must be first in the structure. */
322 	struct xprt_style_ops	*rm_ops;
323 	int	mir_type;		/* Client or server side stream */
324 
325 	mblk_t	*mir_head_mp;		/* RPC msg in progress */
326 		/*
327 		 * mir_head_mp points the first mblk being collected in
328 		 * the current RPC message.  Record headers are removed
329 		 * before data is linked into mir_head_mp.
330 		 */
331 	mblk_t	*mir_tail_mp;		/* Last mblk in mir_head_mp */
332 		/*
333 		 * mir_tail_mp points to the last mblk in the message
334 		 * chain starting at mir_head_mp.  It is only valid
335 		 * if mir_head_mp is non-NULL and is used to add new
336 		 * data blocks to the end of chain quickly.
337 		 */
338 
339 	int32_t	mir_frag_len;		/* Bytes seen in the current frag */
340 		/*
341 		 * mir_frag_len starts at -4 for beginning of each fragment.
342 		 * When this length is negative, it indicates the number of
343 		 * bytes that rpcmod needs to complete the record marker
344 		 * header.  When it is positive or zero, it holds the number
345 		 * of bytes that have arrived for the current fragment and
346 		 * are held in mir_header_mp.
347 		 */
348 
349 	int32_t	mir_frag_header;
350 		/*
351 		 * Fragment header as collected for the current fragment.
352 		 * It holds the last-fragment indicator and the number
353 		 * of bytes in the fragment.
354 		 */
355 
356 	unsigned int
357 		mir_ordrel_pending : 1,	/* Sent T_ORDREL_REQ */
358 		mir_hold_inbound : 1,	/* Hold inbound messages on server */
359 					/* side until outbound flow control */
360 					/* is relieved. */
361 		mir_closing : 1,	/* The stream is being closed */
362 		mir_inrservice : 1,	/* data queued or rd srv proc running */
363 		mir_inwservice : 1,	/* data queued or wr srv proc running */
364 		mir_inwflushdata : 1,	/* flush M_DATAs when srv runs */
365 		/*
366 		 * On client streams, mir_clntreq is 0 or 1; it is set
367 		 * to 1 whenever a new request is sent out (mir_wput)
368 		 * and cleared when the timer fires (mir_timer).  If
369 		 * the timer fires with this value equal to 0, then the
370 		 * stream is considered idle and kRPC is notified.
371 		 */
372 		mir_clntreq : 1,
373 		/*
374 		 * On server streams, stop accepting messages
375 		 */
376 		mir_svc_no_more_msgs : 1,
377 		mir_listen_stream : 1,	/* listen end point */
378 		mir_unused : 1,	/* no longer used */
379 		mir_timer_call : 1,
380 		mir_junk_fill_thru_bit_31 : 21;
381 
382 	int	mir_setup_complete;	/* server has initialized everything */
383 	timeout_id_t mir_timer_id;	/* Timer for idle checks */
384 	clock_t	mir_idle_timeout;	/* Allowed idle time before shutdown */
385 		/*
386 		 * This value is copied from clnt_idle_timeout or
387 		 * svc_idle_timeout during the appropriate ioctl.
388 		 * Kept in milliseconds
389 		 */
390 	clock_t	mir_use_timestamp;	/* updated on client with each use */
391 		/*
392 		 * This value is set to lbolt
393 		 * every time a client stream sends or receives data.
394 		 * Even if the timer message arrives, we don't shutdown
395 		 * client unless:
396 		 *    lbolt >= MSEC_TO_TICK(mir_idle_timeout)+mir_use_timestamp.
397 		 * This value is kept in HZ.
398 		 */
399 
400 	uint_t	*mir_max_msg_sizep;	/* Reference to sanity check size */
401 		/*
402 		 * This pointer is set to &clnt_max_msg_size or
403 		 * &svc_max_msg_size during the appropriate ioctl.
404 		 */
405 	zoneid_t mir_zoneid;	/* zone which pushed rpcmod */
406 	/* Server-side fields. */
407 	int	mir_ref_cnt;		/* Reference count: server side only */
408 					/* counts the number of references */
409 					/* that a kernel RPC server thread */
410 					/* (see svc_run()) has on this rpcmod */
411 					/* slot. Effectively, it is the */
412 					/* number of unprocessed messages */
413 					/* that have been passed up to the */
414 					/* kRPC layer */
415 
416 	mblk_t	*mir_svc_pend_mp;	/* Pending T_ORDREL_IND or */
417 					/* T_DISCON_IND */
418 
419 	/*
420 	 * these fields are for both client and server, but for debugging,
421 	 * it is easier to have these last in the structure.
422 	 */
423 	kmutex_t	mir_mutex;	/* Mutex and condvar for close */
424 	kcondvar_t	mir_condvar;	/* synchronization. */
425 	kcondvar_t	mir_timer_cv;	/* Timer routine sync. */
426 } mir_t;
427 
428 void tmp_rput(queue_t *q, mblk_t *mp);
429 
430 struct xprt_style_ops tmpops = {
431 	NULL,
432 	NULL,
433 	putnext,
434 	NULL,
435 	tmp_rput,
436 	NULL
437 };
438 
439 void
440 tmp_rput(queue_t *q, mblk_t *mp)
441 {
442 	struct temp_slot *t = (struct temp_slot *)(q->q_ptr);
443 	struct T_info_ack *pptr;
444 
445 	switch (mp->b_datap->db_type) {
446 	case M_PCPROTO:
447 		pptr = (struct T_info_ack *)mp->b_rptr;
448 		switch (pptr->PRIM_type) {
449 		case T_INFO_ACK:
450 			mutex_enter(&t->lock);
451 			t->info_ack = mp;
452 			cv_signal(&t->wait);
453 			mutex_exit(&t->lock);
454 			return;
455 		default:
456 			break;
457 		}
458 	default:
459 		break;
460 	}
461 
462 	/*
463 	 * Not an info-ack, so free it. This is ok because we should
464 	 * not be receiving data until the open finishes: rpcmod
465 	 * is pushed well before the end-point is bound to an address.
466 	 */
467 	freemsg(mp);
468 }
469 
470 int
471 rmm_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *crp)
472 {
473 	mblk_t *bp;
474 	struct temp_slot ts, *t;
475 	struct T_info_ack *pptr;
476 	int error = 0;
477 
478 	ASSERT(q != NULL);
479 	/*
480 	 * Check for re-opens.
481 	 */
482 	if (q->q_ptr) {
483 		TRACE_1(TR_FAC_KRPC, TR_RPCMODOPEN_END,
484 		    "rpcmodopen_end:(%s)", "q->qptr");
485 		return (0);
486 	}
487 
488 	t = &ts;
489 	bzero(t, sizeof (*t));
490 	q->q_ptr = (void *)t;
491 	WR(q)->q_ptr = (void *)t;
492 
493 	/*
494 	 * Allocate the required messages upfront.
495 	 */
496 	if ((bp = allocb_cred(sizeof (struct T_info_req) +
497 	    sizeof (struct T_info_ack), crp, curproc->p_pid)) == NULL) {
498 		return (ENOBUFS);
499 	}
500 
501 	mutex_init(&t->lock, NULL, MUTEX_DEFAULT, NULL);
502 	cv_init(&t->wait, NULL, CV_DEFAULT, NULL);
503 
504 	t->ops = &tmpops;
505 
506 	qprocson(q);
507 	bp->b_datap->db_type = M_PCPROTO;
508 	*(int32_t *)bp->b_wptr = (int32_t)T_INFO_REQ;
509 	bp->b_wptr += sizeof (struct T_info_req);
510 	putnext(WR(q), bp);
511 
512 	mutex_enter(&t->lock);
513 	while (t->info_ack == NULL) {
514 		if (cv_wait_sig(&t->wait, &t->lock) == 0) {
515 			error = EINTR;
516 			break;
517 		}
518 	}
519 	mutex_exit(&t->lock);
520 
521 	if (error)
522 		goto out;
523 
524 	pptr = (struct T_info_ack *)t->info_ack->b_rptr;
525 
526 	if (pptr->SERV_type == T_CLTS) {
527 		if ((error = rpcmodopen(q, devp, flag, sflag, crp)) == 0)
528 			((struct rpcm *)q->q_ptr)->rm_ops = &xprt_clts_ops;
529 	} else {
530 		if ((error = mir_open(q, devp, flag, sflag, crp)) == 0)
531 			((mir_t *)q->q_ptr)->rm_ops = &xprt_cots_ops;
532 	}
533 
534 out:
535 	if (error)
536 		qprocsoff(q);
537 
538 	freemsg(t->info_ack);
539 	mutex_destroy(&t->lock);
540 	cv_destroy(&t->wait);
541 
542 	return (error);
543 }
544 
545 int
546 rmm_rput(queue_t *q, mblk_t  *mp)
547 {
548 	(*((struct temp_slot *)q->q_ptr)->ops->xo_rput)(q, mp);
549 	return (0);
550 }
551 
552 int
553 rmm_rsrv(queue_t *q)
554 {
555 	(*((struct temp_slot *)q->q_ptr)->ops->xo_rsrv)(q);
556 	return (0);
557 }
558 
559 int
560 rmm_wput(queue_t *q, mblk_t *mp)
561 {
562 	(*((struct temp_slot *)q->q_ptr)->ops->xo_wput)(q, mp);
563 	return (0);
564 }
565 
566 int
567 rmm_wsrv(queue_t *q)
568 {
569 	(*((struct temp_slot *)q->q_ptr)->ops->xo_wsrv)(q);
570 	return (0);
571 }
572 
573 int
574 rmm_close(queue_t *q, int flag, cred_t *crp)
575 {
576 	return ((*((struct temp_slot *)q->q_ptr)->ops->xo_close)(q, flag, crp));
577 }
578 
579 /*
580  * rpcmodopen -	open routine gets called when the module gets pushed
581  *		onto the stream.
582  */
583 /*ARGSUSED*/
584 int
585 rpcmodopen(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *crp)
586 {
587 	struct rpcm *rmp;
588 
589 	TRACE_0(TR_FAC_KRPC, TR_RPCMODOPEN_START, "rpcmodopen_start:");
590 
591 	/*
592 	 * Only sufficiently privileged users can use this module, and it
593 	 * is assumed that they will use this module properly, and NOT send
594 	 * bulk data from downstream.
595 	 */
596 	if (secpolicy_rpcmod_open(crp) != 0)
597 		return (EPERM);
598 
599 	/*
600 	 * Allocate slot data structure.
601 	 */
602 	rmp = kmem_zalloc(sizeof (*rmp), KM_SLEEP);
603 
604 	mutex_init(&rmp->rm_lock, NULL, MUTEX_DEFAULT, NULL);
605 	cv_init(&rmp->rm_cwait, NULL, CV_DEFAULT, NULL);
606 	rmp->rm_zoneid = rpc_zoneid();
607 	/*
608 	 * slot type will be set by kRPC client and server ioctl's
609 	 */
610 	rmp->rm_type = 0;
611 
612 	q->q_ptr = (void *)rmp;
613 	WR(q)->q_ptr = (void *)rmp;
614 
615 	TRACE_1(TR_FAC_KRPC, TR_RPCMODOPEN_END, "rpcmodopen_end:(%s)", "end");
616 	return (0);
617 }
618 
619 /*
620  * rpcmodclose - This routine gets called when the module gets popped
621  * off of the stream.
622  */
623 /*ARGSUSED*/
624 int
625 rpcmodclose(queue_t *q, int flag, cred_t *crp)
626 {
627 	struct rpcm *rmp;
628 
629 	ASSERT(q != NULL);
630 	rmp = (struct rpcm *)q->q_ptr;
631 
632 	/*
633 	 * Mark our state as closing.
634 	 */
635 	mutex_enter(&rmp->rm_lock);
636 	rmp->rm_state |= RM_CLOSING;
637 
638 	/*
639 	 * Check and see if there are any messages on the queue.  If so, send
640 	 * the messages, regardless whether the downstream module is ready to
641 	 * accept data.
642 	 */
643 	if (rmp->rm_type == RPC_SERVER) {
644 		flushq(q, FLUSHDATA);
645 
646 		qenable(WR(q));
647 
648 		if (rmp->rm_ref) {
649 			mutex_exit(&rmp->rm_lock);
650 			/*
651 			 * call into SVC to clean the queue
652 			 */
653 			svc_queueclean(q);
654 			mutex_enter(&rmp->rm_lock);
655 
656 			/*
657 			 * Block while there are kRPC threads with a reference
658 			 * to this message.
659 			 */
660 			while (rmp->rm_ref)
661 				cv_wait(&rmp->rm_cwait, &rmp->rm_lock);
662 		}
663 
664 		mutex_exit(&rmp->rm_lock);
665 
666 		/*
667 		 * It is now safe to remove this queue from the stream. No kRPC
668 		 * threads have a reference to the stream, and none ever will,
669 		 * because RM_CLOSING is set.
670 		 */
671 		qprocsoff(q);
672 
673 		/* Notify kRPC that this stream is going away. */
674 		svc_queueclose(q);
675 	} else {
676 		mutex_exit(&rmp->rm_lock);
677 		qprocsoff(q);
678 	}
679 
680 	q->q_ptr = NULL;
681 	WR(q)->q_ptr = NULL;
682 	mutex_destroy(&rmp->rm_lock);
683 	cv_destroy(&rmp->rm_cwait);
684 	kmem_free(rmp, sizeof (*rmp));
685 	return (0);
686 }
687 
688 /*
689  * rpcmodrput -	Module read put procedure.  This is called from
690  *		the module, driver, or stream head downstream.
691  */
692 void
693 rpcmodrput(queue_t *q, mblk_t *mp)
694 {
695 	struct rpcm *rmp;
696 	union T_primitives *pptr;
697 	int hdrsz;
698 
699 	TRACE_0(TR_FAC_KRPC, TR_RPCMODRPUT_START, "rpcmodrput_start:");
700 
701 	ASSERT(q != NULL);
702 	rmp = (struct rpcm *)q->q_ptr;
703 
704 	if (rmp->rm_type == 0) {
705 		freemsg(mp);
706 		return;
707 	}
708 
709 	switch (mp->b_datap->db_type) {
710 	default:
711 		putnext(q, mp);
712 		break;
713 
714 	case M_PROTO:
715 	case M_PCPROTO:
716 		ASSERT((mp->b_wptr - mp->b_rptr) >= sizeof (int32_t));
717 		pptr = (union T_primitives *)mp->b_rptr;
718 
719 		/*
720 		 * Forward this message to kRPC if it is data.
721 		 */
722 		if (pptr->type == T_UNITDATA_IND) {
723 			/*
724 			 * Check if the module is being popped.
725 			 */
726 			mutex_enter(&rmp->rm_lock);
727 			if (rmp->rm_state & RM_CLOSING) {
728 				mutex_exit(&rmp->rm_lock);
729 				putnext(q, mp);
730 				break;
731 			}
732 
733 			switch (rmp->rm_type) {
734 			case RPC_CLIENT:
735 				mutex_exit(&rmp->rm_lock);
736 				hdrsz = mp->b_wptr - mp->b_rptr;
737 
738 				/*
739 				 * Make sure the header is sane.
740 				 */
741 				if (hdrsz < TUNITDATAINDSZ ||
742 				    hdrsz < (pptr->unitdata_ind.OPT_length +
743 				    pptr->unitdata_ind.OPT_offset) ||
744 				    hdrsz < (pptr->unitdata_ind.SRC_length +
745 				    pptr->unitdata_ind.SRC_offset)) {
746 					freemsg(mp);
747 					return;
748 				}
749 
750 				/*
751 				 * Call clnt_clts_dispatch_notify, so that it
752 				 * can pass the message to the proper caller.
753 				 * Don't discard the header just yet since the
754 				 * client may need the sender's address.
755 				 */
756 				clnt_clts_dispatch_notify(mp, hdrsz,
757 				    rmp->rm_zoneid);
758 				return;
759 			case RPC_SERVER:
760 				/*
761 				 * rm_krpc_cell is exclusively used by the kRPC
762 				 * CLTS server. Try to submit the message to
763 				 * kRPC. Since this is an unreliable channel, we
764 				 * can just free the message in case the kRPC
765 				 * does not accept new messages.
766 				 */
767 				if (rmp->rm_krpc_cell &&
768 				    svc_queuereq(q, mp, TRUE)) {
769 					/*
770 					 * Raise the reference count on this
771 					 * module to prevent it from being
772 					 * popped before kRPC generates the
773 					 * reply.
774 					 */
775 					rmp->rm_ref++;
776 					mutex_exit(&rmp->rm_lock);
777 				} else {
778 					mutex_exit(&rmp->rm_lock);
779 					freemsg(mp);
780 				}
781 				return;
782 			default:
783 				mutex_exit(&rmp->rm_lock);
784 				freemsg(mp);
785 				return;
786 			} /* end switch(rmp->rm_type) */
787 		} else if (pptr->type == T_UDERROR_IND) {
788 			mutex_enter(&rmp->rm_lock);
789 			hdrsz = mp->b_wptr - mp->b_rptr;
790 
791 			/*
792 			 * Make sure the header is sane
793 			 */
794 			if (hdrsz < TUDERRORINDSZ ||
795 			    hdrsz < (pptr->uderror_ind.OPT_length +
796 			    pptr->uderror_ind.OPT_offset) ||
797 			    hdrsz < (pptr->uderror_ind.DEST_length +
798 			    pptr->uderror_ind.DEST_offset)) {
799 				mutex_exit(&rmp->rm_lock);
800 				freemsg(mp);
801 				return;
802 			}
803 
804 			/*
805 			 * In the case where a unit data error has been
806 			 * received, all we need to do is clear the message from
807 			 * the queue.
808 			 */
809 			mutex_exit(&rmp->rm_lock);
810 			freemsg(mp);
811 			RPCLOG(32, "rpcmodrput: unitdata error received at "
812 			    "%ld\n", gethrestime_sec());
813 			return;
814 		} /* end else if (pptr->type == T_UDERROR_IND) */
815 
816 		putnext(q, mp);
817 		break;
818 	} /* end switch (mp->b_datap->db_type) */
819 
820 	TRACE_0(TR_FAC_KRPC, TR_RPCMODRPUT_END,
821 	    "rpcmodrput_end:");
822 	/*
823 	 * Return codes are not looked at by the STREAMS framework.
824 	 */
825 }
826 
827 /*
828  * write put procedure
829  */
830 void
831 rpcmodwput(queue_t *q, mblk_t *mp)
832 {
833 	struct rpcm	*rmp;
834 
835 	ASSERT(q != NULL);
836 
837 	switch (mp->b_datap->db_type) {
838 		case M_PROTO:
839 		case M_PCPROTO:
840 			break;
841 		default:
842 			rpcmodwput_other(q, mp);
843 			return;
844 	}
845 
846 	/*
847 	 * Check to see if we can send the message downstream.
848 	 */
849 	if (canputnext(q)) {
850 		putnext(q, mp);
851 		return;
852 	}
853 
854 	rmp = (struct rpcm *)q->q_ptr;
855 	ASSERT(rmp != NULL);
856 
857 	/*
858 	 * The first canputnext failed.  Try again except this time with the
859 	 * lock held, so that we can check the state of the stream to see if
860 	 * it is closing.  If either of these conditions evaluate to true
861 	 * then send the meesage.
862 	 */
863 	mutex_enter(&rmp->rm_lock);
864 	if (canputnext(q) || (rmp->rm_state & RM_CLOSING)) {
865 		mutex_exit(&rmp->rm_lock);
866 		putnext(q, mp);
867 	} else {
868 		/*
869 		 * canputnext failed again and the stream is not closing.
870 		 * Place the message on the queue and let the service
871 		 * procedure handle the message.
872 		 */
873 		mutex_exit(&rmp->rm_lock);
874 		(void) putq(q, mp);
875 	}
876 }
877 
878 static void
879 rpcmodwput_other(queue_t *q, mblk_t *mp)
880 {
881 	struct rpcm	*rmp;
882 	struct iocblk	*iocp;
883 
884 	rmp = (struct rpcm *)q->q_ptr;
885 	ASSERT(rmp != NULL);
886 
887 	switch (mp->b_datap->db_type) {
888 		case M_IOCTL:
889 			iocp = (struct iocblk *)mp->b_rptr;
890 			ASSERT(iocp != NULL);
891 			switch (iocp->ioc_cmd) {
892 				case RPC_CLIENT:
893 				case RPC_SERVER:
894 					mutex_enter(&rmp->rm_lock);
895 					rmp->rm_type = iocp->ioc_cmd;
896 					mutex_exit(&rmp->rm_lock);
897 					mp->b_datap->db_type = M_IOCACK;
898 					qreply(q, mp);
899 					return;
900 				default:
901 				/*
902 				 * pass the ioctl downstream and hope someone
903 				 * down there knows how to handle it.
904 				 */
905 					putnext(q, mp);
906 					return;
907 			}
908 		default:
909 			break;
910 	}
911 	/*
912 	 * This is something we definitely do not know how to handle, just
913 	 * pass the message downstream
914 	 */
915 	putnext(q, mp);
916 }
917 
918 /*
919  * Module write service procedure. This is called by downstream modules
920  * for back enabling during flow control.
921  */
922 void
923 rpcmodwsrv(queue_t *q)
924 {
925 	struct rpcm	*rmp;
926 	mblk_t		*mp = NULL;
927 
928 	rmp = (struct rpcm *)q->q_ptr;
929 	ASSERT(rmp != NULL);
930 
931 	/*
932 	 * Get messages that may be queued and send them down stream
933 	 */
934 	while ((mp = getq(q)) != NULL) {
935 		/*
936 		 * Optimize the service procedure for the server-side, by
937 		 * avoiding a call to canputnext().
938 		 */
939 		if (rmp->rm_type == RPC_SERVER || canputnext(q)) {
940 			putnext(q, mp);
941 			continue;
942 		}
943 		(void) putbq(q, mp);
944 		return;
945 	}
946 }
947 
948 void
949 rpcmod_hold(queue_t *q)
950 {
951 	struct rpcm *rmp = (struct rpcm *)q->q_ptr;
952 
953 	mutex_enter(&rmp->rm_lock);
954 	rmp->rm_ref++;
955 	mutex_exit(&rmp->rm_lock);
956 }
957 
958 void
959 rpcmod_release(queue_t *q, mblk_t *bp, bool_t enable __unused)
960 {
961 	struct rpcm *rmp;
962 
963 	/*
964 	 * For now, just free the message.
965 	 */
966 	if (bp)
967 		freemsg(bp);
968 	rmp = (struct rpcm *)q->q_ptr;
969 
970 	mutex_enter(&rmp->rm_lock);
971 	rmp->rm_ref--;
972 
973 	if (rmp->rm_ref == 0 && (rmp->rm_state & RM_CLOSING)) {
974 		cv_broadcast(&rmp->rm_cwait);
975 	}
976 
977 	mutex_exit(&rmp->rm_lock);
978 }
979 
980 /*
981  * This part of rpcmod is pushed on a connection-oriented transport for use
982  * by RPC.  It serves to bypass the Stream head, implements
983  * the record marking protocol, and dispatches incoming RPC messages.
984  */
985 
986 /* Default idle timer values */
987 #define	MIR_CLNT_IDLE_TIMEOUT	(5 * (60 * 1000L))	/* 5 minutes */
988 #define	MIR_SVC_IDLE_TIMEOUT	(6 * (60 * 1000L))	/* 6 minutes */
989 #define	MIR_SVC_ORDREL_TIMEOUT	(10 * (60 * 1000L))	/* 10 minutes */
990 #define	MIR_LASTFRAG	0x80000000	/* Record marker */
991 
992 #define	MIR_SVC_QUIESCED(mir)	\
993 	(mir->mir_ref_cnt == 0 && mir->mir_inrservice == 0)
994 
995 #define	MIR_CLEAR_INRSRV(mir_ptr)	{	\
996 	(mir_ptr)->mir_inrservice = 0;	\
997 	if ((mir_ptr)->mir_type == RPC_SERVER &&	\
998 		(mir_ptr)->mir_closing)	\
999 		cv_signal(&(mir_ptr)->mir_condvar);	\
1000 }
1001 
1002 /*
1003  * Don't block service procedure (and mir_close) if
1004  * we are in the process of closing.
1005  */
1006 #define	MIR_WCANPUTNEXT(mir_ptr, write_q)	\
1007 	(canputnext(write_q) || ((mir_ptr)->mir_svc_no_more_msgs == 1))
1008 
1009 static int	mir_clnt_dup_request(queue_t *q, mblk_t *mp);
1010 static void	mir_rput_proto(queue_t *q, mblk_t *mp);
1011 static int	mir_svc_policy_notify(queue_t *q, int event);
1012 static void	mir_svc_start(queue_t *wq);
1013 static void	mir_svc_idle_start(queue_t *, mir_t *);
1014 static void	mir_svc_idle_stop(queue_t *, mir_t *);
1015 static void	mir_svc_start_close(queue_t *, mir_t *);
1016 static void	mir_clnt_idle_do_stop(queue_t *);
1017 static void	mir_clnt_idle_stop(queue_t *, mir_t *);
1018 static void	mir_clnt_idle_start(queue_t *, mir_t *);
1019 static void	mir_wput(queue_t *q, mblk_t *mp);
1020 static void	mir_wput_other(queue_t *q, mblk_t *mp);
1021 static void	mir_wsrv(queue_t *q);
1022 static	void	mir_disconnect(queue_t *, mir_t *ir);
1023 static	int	mir_check_len(queue_t *, mblk_t *);
1024 static	void	mir_timer(void *);
1025 
1026 extern void	(*mir_start)(queue_t *);
1027 extern void	(*clnt_stop_idle)(queue_t *);
1028 
1029 clock_t	clnt_idle_timeout = MIR_CLNT_IDLE_TIMEOUT;
1030 clock_t	svc_idle_timeout = MIR_SVC_IDLE_TIMEOUT;
1031 
1032 /*
1033  * Timeout for subsequent notifications of idle connection.  This is
1034  * typically used to clean up after a wedged orderly release.
1035  */
1036 clock_t	svc_ordrel_timeout = MIR_SVC_ORDREL_TIMEOUT; /* milliseconds */
1037 
1038 extern	uint_t	*clnt_max_msg_sizep;
1039 extern	uint_t	*svc_max_msg_sizep;
1040 uint_t	clnt_max_msg_size = RPC_MAXDATASIZE;
1041 uint_t	svc_max_msg_size = RPC_MAXDATASIZE;
1042 uint_t	mir_krpc_cell_null;
1043 
1044 static void
1045 mir_timer_stop(mir_t *mir)
1046 {
1047 	timeout_id_t tid;
1048 
1049 	ASSERT(MUTEX_HELD(&mir->mir_mutex));
1050 
1051 	/*
1052 	 * Since the mir_mutex lock needs to be released to call
1053 	 * untimeout(), we need to make sure that no other thread
1054 	 * can start/stop the timer (changing mir_timer_id) during
1055 	 * that time.  The mir_timer_call bit and the mir_timer_cv
1056 	 * condition variable are used to synchronize this.  Setting
1057 	 * mir_timer_call also tells mir_timer() (refer to the comments
1058 	 * in mir_timer()) that it does not need to do anything.
1059 	 */
1060 	while (mir->mir_timer_call)
1061 		cv_wait(&mir->mir_timer_cv, &mir->mir_mutex);
1062 	mir->mir_timer_call = B_TRUE;
1063 
1064 	if ((tid = mir->mir_timer_id) != 0) {
1065 		mir->mir_timer_id = 0;
1066 		mutex_exit(&mir->mir_mutex);
1067 		(void) untimeout(tid);
1068 		mutex_enter(&mir->mir_mutex);
1069 	}
1070 	mir->mir_timer_call = B_FALSE;
1071 	cv_broadcast(&mir->mir_timer_cv);
1072 }
1073 
1074 static void
1075 mir_timer_start(queue_t *q, mir_t *mir, clock_t intrvl)
1076 {
1077 	timeout_id_t tid;
1078 
1079 	ASSERT(MUTEX_HELD(&mir->mir_mutex));
1080 
1081 	while (mir->mir_timer_call)
1082 		cv_wait(&mir->mir_timer_cv, &mir->mir_mutex);
1083 	mir->mir_timer_call = B_TRUE;
1084 
1085 	if ((tid = mir->mir_timer_id) != 0) {
1086 		mutex_exit(&mir->mir_mutex);
1087 		(void) untimeout(tid);
1088 		mutex_enter(&mir->mir_mutex);
1089 	}
1090 	/* Only start the timer when it is not closing. */
1091 	if (!mir->mir_closing) {
1092 		mir->mir_timer_id = timeout(mir_timer, q,
1093 		    MSEC_TO_TICK(intrvl));
1094 	}
1095 	mir->mir_timer_call = B_FALSE;
1096 	cv_broadcast(&mir->mir_timer_cv);
1097 }
1098 
1099 static int
1100 mir_clnt_dup_request(queue_t *q, mblk_t *mp)
1101 {
1102 	mblk_t  *mp1;
1103 	uint32_t  new_xid;
1104 	uint32_t  old_xid;
1105 
1106 	ASSERT(MUTEX_HELD(&((mir_t *)q->q_ptr)->mir_mutex));
1107 	new_xid = BE32_TO_U32(&mp->b_rptr[4]);
1108 	/*
1109 	 * This loop is a bit tacky -- it walks the STREAMS list of
1110 	 * flow-controlled messages.
1111 	 */
1112 	if ((mp1 = q->q_first) != NULL) {
1113 		do {
1114 			old_xid = BE32_TO_U32(&mp1->b_rptr[4]);
1115 			if (new_xid == old_xid)
1116 				return (1);
1117 		} while ((mp1 = mp1->b_next) != NULL);
1118 	}
1119 	return (0);
1120 }
1121 
1122 static int
1123 mir_close(queue_t *q)
1124 {
1125 	mir_t	*mir = q->q_ptr;
1126 	mblk_t	*mp;
1127 	bool_t queue_cleaned = FALSE;
1128 
1129 	RPCLOG(32, "rpcmod: mir_close of q 0x%p\n", (void *)q);
1130 	ASSERT(MUTEX_NOT_HELD(&mir->mir_mutex));
1131 	mutex_enter(&mir->mir_mutex);
1132 	if ((mp = mir->mir_head_mp) != NULL) {
1133 		mir->mir_head_mp = NULL;
1134 		mir->mir_tail_mp = NULL;
1135 		freemsg(mp);
1136 	}
1137 	/*
1138 	 * Set mir_closing so we get notified when MIR_SVC_QUIESCED()
1139 	 * is TRUE.  And mir_timer_start() won't start the timer again.
1140 	 */
1141 	mir->mir_closing = B_TRUE;
1142 	mir_timer_stop(mir);
1143 
1144 	if (mir->mir_type == RPC_SERVER) {
1145 		flushq(q, FLUSHDATA);	/* Ditch anything waiting on read q */
1146 
1147 		/*
1148 		 * This will prevent more requests from arriving and
1149 		 * will force rpcmod to ignore flow control.
1150 		 */
1151 		mir_svc_start_close(WR(q), mir);
1152 
1153 		while ((!MIR_SVC_QUIESCED(mir)) || mir->mir_inwservice == 1) {
1154 
1155 			if (mir->mir_ref_cnt && !mir->mir_inrservice &&
1156 			    (queue_cleaned == FALSE)) {
1157 				/*
1158 				 * call into SVC to clean the queue
1159 				 */
1160 				mutex_exit(&mir->mir_mutex);
1161 				svc_queueclean(q);
1162 				queue_cleaned = TRUE;
1163 				mutex_enter(&mir->mir_mutex);
1164 				continue;
1165 			}
1166 
1167 			/*
1168 			 * Bugid 1253810 - Force the write service
1169 			 * procedure to send its messages, regardless
1170 			 * whether the downstream  module is ready
1171 			 * to accept data.
1172 			 */
1173 			if (mir->mir_inwservice == 1)
1174 				qenable(WR(q));
1175 
1176 			cv_wait(&mir->mir_condvar, &mir->mir_mutex);
1177 		}
1178 
1179 		mutex_exit(&mir->mir_mutex);
1180 		/*
1181 		 * Destroy the cm_entry
1182 		 */
1183 		connmgr_cb_destroy(WR(q));
1184 		connmgr_destroy(WR(q));
1185 		qprocsoff(q);
1186 
1187 		/* Notify kRPC that this stream is going away. */
1188 		svc_queueclose(q);
1189 	} else {
1190 		mutex_exit(&mir->mir_mutex);
1191 		qprocsoff(q);
1192 	}
1193 
1194 	mutex_destroy(&mir->mir_mutex);
1195 	cv_destroy(&mir->mir_condvar);
1196 	cv_destroy(&mir->mir_timer_cv);
1197 	kmem_free(mir, sizeof (mir_t));
1198 	return (0);
1199 }
1200 
1201 /*
1202  * This is server side only (RPC_SERVER).
1203  *
1204  * Exit idle mode.
1205  */
1206 static void
1207 mir_svc_idle_stop(queue_t *q, mir_t *mir)
1208 {
1209 	ASSERT(MUTEX_HELD(&mir->mir_mutex));
1210 	ASSERT((q->q_flag & QREADR) == 0);
1211 	ASSERT(mir->mir_type == RPC_SERVER);
1212 	RPCLOG(16, "rpcmod: mir_svc_idle_stop of q 0x%p\n", (void *)q);
1213 
1214 	mir_timer_stop(mir);
1215 }
1216 
1217 /*
1218  * This is server side only (RPC_SERVER).
1219  *
1220  * Start idle processing, which will include setting idle timer if the
1221  * stream is not being closed.
1222  */
1223 static void
1224 mir_svc_idle_start(queue_t *q, mir_t *mir)
1225 {
1226 	ASSERT(MUTEX_HELD(&mir->mir_mutex));
1227 	ASSERT((q->q_flag & QREADR) == 0);
1228 	ASSERT(mir->mir_type == RPC_SERVER);
1229 	RPCLOG(16, "rpcmod: mir_svc_idle_start q 0x%p\n", (void *)q);
1230 
1231 	/*
1232 	 * Don't re-start idle timer if we are closing queues.
1233 	 */
1234 	if (mir->mir_closing) {
1235 		RPCLOG(16, "mir_svc_idle_start - closing: 0x%p\n",
1236 		    (void *)q);
1237 
1238 		/*
1239 		 * We will call mir_svc_idle_start() whenever MIR_SVC_QUIESCED()
1240 		 * is true.  When it is true, and we are in the process of
1241 		 * closing the stream, signal any thread waiting in
1242 		 * mir_close().
1243 		 */
1244 		if (mir->mir_inwservice == 0)
1245 			cv_signal(&mir->mir_condvar);
1246 
1247 	} else {
1248 		RPCLOG(16, "mir_svc_idle_start - reset %s timer\n",
1249 		    mir->mir_ordrel_pending ? "ordrel" : "normal");
1250 		/*
1251 		 * Normal condition, start the idle timer.  If an orderly
1252 		 * release has been sent, set the timeout to wait for the
1253 		 * client to close its side of the connection.  Otherwise,
1254 		 * use the normal idle timeout.
1255 		 */
1256 		mir_timer_start(q, mir, mir->mir_ordrel_pending ?
1257 		    svc_ordrel_timeout : mir->mir_idle_timeout);
1258 	}
1259 }
1260 
1261 /*
1262  * Copy out the RPC transaction id and RPC Direction
1263  * from the mblk chain. Leave the mblk intact.
1264  */
1265 bool_t
1266 mir_dir_xid(mblk_t *mp, uint32_t *dir, uint32_t *xid)
1267 {
1268 	unsigned char	*p;
1269 	unsigned char	*rptr;
1270 	mblk_t		*tmp;
1271 	int		i, get_rpcdir;
1272 	uint32_t	d_tmp = 0;
1273 
1274 	/*
1275 	 * If we can just grab the XID and RPC direction flag great.
1276 	 */
1277 	if ((IS_P2ALIGNED(mp->b_rptr, (sizeof (uint64_t)))) &&
1278 	    (mp->b_wptr - mp->b_rptr) >= (sizeof (uint64_t))) {
1279 		*xid = *((uint32_t *)mp->b_rptr);
1280 		*dir = ntohl(*((uint32_t *)(mp->b_rptr + sizeof (uint32_t))));
1281 		return (TRUE);
1282 	}
1283 
1284 	/*
1285 	 * Otherwise we need to copy byte-by-byte
1286 	 */
1287 	DTRACE_PROBE(krpc__i__bytecopy);
1288 
1289 	i = get_rpcdir = 0;
1290 	p = (unsigned char *)xid;
1291 	tmp = mp;
1292 
1293 	/*
1294 	 * While we have not exhausted the entire mblk chain:
1295 	 * copy the first sizeof uint32_t value into xid, and
1296 	 * then the second sizeof uint32_t value into a temporary
1297 	 * so that we can convert from network byte order.
1298 	 *
1299 	 * Should we exhaust the entire mblk chain in attempting
1300 	 * to do this, return FALSE.
1301 	 */
1302 	while (tmp) {
1303 		rptr = tmp->b_rptr;
1304 		while (rptr < tmp->b_wptr) {
1305 			*p++ = *rptr++;
1306 			/*
1307 			 * Have we collected enough bytes for
1308 			 * a uint32_t ?
1309 			 */
1310 			if (++i == sizeof (uint32_t)) {
1311 				/*
1312 				 * If yes, do we need to switch to
1313 				 * RPC Direction or are we all done ?
1314 				 */
1315 				if (get_rpcdir) {
1316 					/* Got it all */
1317 					*dir = ntohl(d_tmp);
1318 					return (TRUE);
1319 				}
1320 				/* start to collect RPC Direction */
1321 				get_rpcdir++;
1322 				i = 0;
1323 				p = (unsigned char *)&d_tmp;
1324 			}
1325 		}
1326 		tmp = tmp->b_cont;
1327 	}
1328 
1329 	/* We didn't get both of them.. */
1330 	DTRACE_PROBE(krpc__e__mblk_exhausted);
1331 	return (FALSE);
1332 }
1333 
1334 static int
1335 mir_open(queue_t *q, dev_t *devp __unused, int flag __unused,
1336     int sflag __unused, cred_t *credp __unused)
1337 {
1338 	mir_t	*mir;
1339 
1340 	RPCLOG(32, "rpcmod: mir_open of q 0x%p\n", (void *)q);
1341 	/* Set variables used directly by kRPC. */
1342 	if (!mir_start)
1343 		mir_start = mir_svc_start;
1344 	if (!clnt_stop_idle)
1345 		clnt_stop_idle = mir_clnt_idle_do_stop;
1346 	if (!clnt_max_msg_sizep)
1347 		clnt_max_msg_sizep = &clnt_max_msg_size;
1348 	if (!svc_max_msg_sizep)
1349 		svc_max_msg_sizep = &svc_max_msg_size;
1350 
1351 	/* Allocate a zero'ed out mir structure for this stream. */
1352 	mir = kmem_zalloc(sizeof (mir_t), KM_SLEEP);
1353 
1354 	/*
1355 	 * We set hold inbound here so that incoming messages will
1356 	 * be held on the read-side queue until the stream is completely
1357 	 * initialized with a RPC_CLIENT or RPC_SERVER ioctl.  During
1358 	 * the ioctl processing, the flag is cleared and any messages that
1359 	 * arrived between the open and the ioctl are delivered to kRPC.
1360 	 *
1361 	 * Early data should never arrive on a client stream since
1362 	 * servers only respond to our requests and we do not send any.
1363 	 * until after the stream is initialized.  Early data is
1364 	 * very common on a server stream where the client will start
1365 	 * sending data as soon as the connection is made (and this
1366 	 * is especially true with TCP where the protocol accepts the
1367 	 * connection before nfsd or kRPC is notified about it).
1368 	 */
1369 
1370 	mir->mir_hold_inbound = 1;
1371 
1372 	/*
1373 	 * Start the record marker looking for a 4-byte header.  When
1374 	 * this length is negative, it indicates that rpcmod is looking
1375 	 * for bytes to consume for the record marker header.  When it
1376 	 * is positive, it holds the number of bytes that have arrived
1377 	 * for the current fragment and are being held in mir_header_mp.
1378 	 */
1379 
1380 	mir->mir_frag_len = -(int32_t)sizeof (uint32_t);
1381 
1382 	mir->mir_zoneid = rpc_zoneid();
1383 	mutex_init(&mir->mir_mutex, NULL, MUTEX_DEFAULT, NULL);
1384 	cv_init(&mir->mir_condvar, NULL, CV_DRIVER, NULL);
1385 	cv_init(&mir->mir_timer_cv, NULL, CV_DRIVER, NULL);
1386 
1387 	q->q_ptr = (char *)mir;
1388 	WR(q)->q_ptr = (char *)mir;
1389 
1390 	/*
1391 	 * We noenable the read-side queue because we don't want it
1392 	 * automatically enabled by putq.  We enable it explicitly
1393 	 * in mir_wsrv when appropriate. (See additional comments on
1394 	 * flow control at the beginning of mir_rsrv.)
1395 	 */
1396 	noenable(q);
1397 
1398 	qprocson(q);
1399 	return (0);
1400 }
1401 
1402 /*
1403  * Read-side put routine for both the client and server side.  Does the
1404  * record marking for incoming RPC messages, and when complete, dispatches
1405  * the message to either the client or server.
1406  */
1407 static void
1408 mir_rput(queue_t *q, mblk_t *mp)
1409 {
1410 	int	excess;
1411 	int32_t	frag_len, frag_header;
1412 	mblk_t	*cont_mp, *head_mp, *tail_mp, *mp1;
1413 	mir_t	*mir = q->q_ptr;
1414 	boolean_t stop_timer = B_FALSE;
1415 	uint32_t	xid;
1416 	uint32_t	dir;
1417 
1418 	ASSERT(mir != NULL);
1419 
1420 	/*
1421 	 * If the stream has not been set up as a RPC_CLIENT or RPC_SERVER
1422 	 * with the corresponding ioctl, then don't accept
1423 	 * any inbound data.  This should never happen for streams
1424 	 * created by nfsd or client-side kRPC because they are careful
1425 	 * to set the mode of the stream before doing anything else.
1426 	 */
1427 	if (mir->mir_type == 0) {
1428 		freemsg(mp);
1429 		return;
1430 	}
1431 
1432 	ASSERT(MUTEX_NOT_HELD(&mir->mir_mutex));
1433 
1434 	switch (mp->b_datap->db_type) {
1435 	case M_DATA:
1436 		break;
1437 	case M_PROTO:
1438 	case M_PCPROTO:
1439 		if (MBLKL(mp) < sizeof (t_scalar_t)) {
1440 			RPCLOG(1, "mir_rput: runt TPI message (%d bytes)\n",
1441 			    (int)MBLKL(mp));
1442 			freemsg(mp);
1443 			return;
1444 		}
1445 		if (((union T_primitives *)mp->b_rptr)->type != T_DATA_IND) {
1446 			mir_rput_proto(q, mp);
1447 			return;
1448 		}
1449 
1450 		/* Throw away the T_DATA_IND block and continue with data. */
1451 		mp1 = mp;
1452 		mp = mp->b_cont;
1453 		freeb(mp1);
1454 		break;
1455 	case M_SETOPTS:
1456 		/*
1457 		 * If a module on the stream is trying set the Stream head's
1458 		 * high water mark, then set our hiwater to the requested
1459 		 * value.  We are the "stream head" for all inbound
1460 		 * data messages since messages are passed directly to kRPC.
1461 		 */
1462 		if (MBLKL(mp) >= sizeof (struct stroptions)) {
1463 			struct stroptions	*stropts;
1464 
1465 			stropts = (struct stroptions *)mp->b_rptr;
1466 			if ((stropts->so_flags & SO_HIWAT) &&
1467 			    !(stropts->so_flags & SO_BAND)) {
1468 				(void) strqset(q, QHIWAT, 0, stropts->so_hiwat);
1469 			}
1470 		}
1471 		putnext(q, mp);
1472 		return;
1473 	case M_FLUSH:
1474 		RPCLOG(32, "mir_rput: ignoring M_FLUSH %x ", *mp->b_rptr);
1475 		RPCLOG(32, "on q 0x%p\n", (void *)q);
1476 		putnext(q, mp);
1477 		return;
1478 	default:
1479 		putnext(q, mp);
1480 		return;
1481 	}
1482 
1483 	mutex_enter(&mir->mir_mutex);
1484 
1485 	/*
1486 	 * If this connection is closing, don't accept any new messages.
1487 	 */
1488 	if (mir->mir_svc_no_more_msgs) {
1489 		ASSERT(mir->mir_type == RPC_SERVER);
1490 		mutex_exit(&mir->mir_mutex);
1491 		freemsg(mp);
1492 		return;
1493 	}
1494 
1495 	/* Get local copies for quicker access. */
1496 	frag_len = mir->mir_frag_len;
1497 	frag_header = mir->mir_frag_header;
1498 	head_mp = mir->mir_head_mp;
1499 	tail_mp = mir->mir_tail_mp;
1500 
1501 	/* Loop, processing each message block in the mp chain separately. */
1502 	do {
1503 		cont_mp = mp->b_cont;
1504 		mp->b_cont = NULL;
1505 
1506 		/*
1507 		 * Drop zero-length mblks to prevent unbounded kernel memory
1508 		 * consumption.
1509 		 */
1510 		if (MBLKL(mp) == 0) {
1511 			freeb(mp);
1512 			continue;
1513 		}
1514 
1515 		/*
1516 		 * If frag_len is negative, we're still in the process of
1517 		 * building frag_header -- try to complete it with this mblk.
1518 		 */
1519 		while (frag_len < 0 && mp->b_rptr < mp->b_wptr) {
1520 			frag_len++;
1521 			frag_header <<= 8;
1522 			frag_header += *mp->b_rptr++;
1523 		}
1524 
1525 		if (MBLKL(mp) == 0 && frag_len < 0) {
1526 			/*
1527 			 * We consumed this mblk while trying to complete the
1528 			 * fragment header.  Free it and move on.
1529 			 */
1530 			freeb(mp);
1531 			continue;
1532 		}
1533 
1534 		ASSERT(frag_len >= 0);
1535 
1536 		/*
1537 		 * Now frag_header has the number of bytes in this fragment
1538 		 * and we're just waiting to collect them all.  Chain our
1539 		 * latest mblk onto the list and see if we now have enough
1540 		 * bytes to complete the fragment.
1541 		 */
1542 		if (head_mp == NULL) {
1543 			ASSERT(tail_mp == NULL);
1544 			head_mp = tail_mp = mp;
1545 		} else {
1546 			tail_mp->b_cont = mp;
1547 			tail_mp = mp;
1548 		}
1549 
1550 		frag_len += MBLKL(mp);
1551 		excess = frag_len - (frag_header & ~MIR_LASTFRAG);
1552 		if (excess < 0) {
1553 			/*
1554 			 * We still haven't received enough data to complete
1555 			 * the fragment, so continue on to the next mblk.
1556 			 */
1557 			continue;
1558 		}
1559 
1560 		/*
1561 		 * We've got a complete fragment.  If there are excess bytes,
1562 		 * then they're part of the next fragment's header (of either
1563 		 * this RPC message or the next RPC message).  Split that part
1564 		 * into its own mblk so that we can safely freeb() it when
1565 		 * building frag_header above.
1566 		 */
1567 		if (excess > 0) {
1568 			if ((mp1 = dupb(mp)) == NULL &&
1569 			    (mp1 = copyb(mp)) == NULL) {
1570 				freemsg(head_mp);
1571 				freemsg(cont_mp);
1572 				RPCLOG0(1, "mir_rput: dupb/copyb failed\n");
1573 				mir->mir_frag_header = 0;
1574 				mir->mir_frag_len = -(int32_t)sizeof (uint32_t);
1575 				mir->mir_head_mp = NULL;
1576 				mir->mir_tail_mp = NULL;
1577 				mir_disconnect(q, mir);	/* drops mir_mutex */
1578 				return;
1579 			}
1580 
1581 			/*
1582 			 * Relink the message chain so that the next mblk is
1583 			 * the next fragment header, followed by the rest of
1584 			 * the message chain.
1585 			 */
1586 			mp1->b_cont = cont_mp;
1587 			cont_mp = mp1;
1588 
1589 			/*
1590 			 * Data in the new mblk begins at the next fragment,
1591 			 * and data in the old mblk ends at the next fragment.
1592 			 */
1593 			mp1->b_rptr = mp1->b_wptr - excess;
1594 			mp->b_wptr -= excess;
1595 		}
1596 
1597 		/*
1598 		 * Reset frag_len and frag_header for the next fragment.
1599 		 */
1600 		frag_len = -(int32_t)sizeof (uint32_t);
1601 		if (!(frag_header & MIR_LASTFRAG)) {
1602 			/*
1603 			 * The current fragment is complete, but more
1604 			 * fragments need to be processed before we can
1605 			 * pass along the RPC message headed at head_mp.
1606 			 */
1607 			frag_header = 0;
1608 			continue;
1609 		}
1610 		frag_header = 0;
1611 
1612 		/*
1613 		 * Get msg direction and handle to the appropriate ctxt
1614 		 */
1615 		if (!mir_dir_xid(head_mp, &dir, &xid)) {
1616 			/* XXX - if we can't get the dir, we're hosed */
1617 			mutex_exit(&mir->mir_mutex);
1618 			freemsg(head_mp);
1619 			return;
1620 		}
1621 
1622 		/*
1623 		 * We've got a complete RPC message; pass it to the
1624 		 * appropriate consumer.
1625 		 */
1626 		switch (mir->mir_type) {
1627 		case RPC_CLIENT:
1628 			switch (dir) {
1629 			case REPLY:
1630 				if (clnt_dispatch_notify(head_mp,
1631 				    mir->mir_zoneid, xid)) {
1632 					/*
1633 					 * Mark this stream as active.
1634 					 * This marker is used in mir_timer().
1635 					 */
1636 					mir->mir_clntreq = 1;
1637 					mir->mir_use_timestamp =
1638 					    ddi_get_lbolt();
1639 				} else {
1640 					freemsg(head_mp);
1641 				}
1642 				break;
1643 			case CALL:
1644 				/* TBD: client is now a callback server */
1645 			default:
1646 				RPCLOG(1, "mir_rput: TBD callback server %d\n",
1647 				    dir);
1648 				break;
1649 			}
1650 			break;
1651 		case RPC_SERVER:
1652 			switch (dir) {
1653 			case REPLY:
1654 				/*
1655 				 * RPC Server initiated a Callback RPC and
1656 				 * is receiving a reply from the RPC Client.
1657 				 */
1658 				if (clnt_dispatch_notify(head_mp,
1659 				    mir->mir_zoneid, xid)) {
1660 					mir->mir_clntreq = 1;
1661 					mir->mir_use_timestamp =
1662 					    ddi_get_lbolt();
1663 				} else {
1664 					freemsg(head_mp);
1665 				}
1666 				break;
1667 
1668 			case CALL:
1669 			default:
1670 				/*
1671 				 * Check for flow control before passing the
1672 				 * message to kRPC.
1673 				 */
1674 				if (!mir->mir_hold_inbound) {
1675 					if (!mir->mir_krpc_cell) {
1676 						/*
1677 						 * Count # of times this
1678 						 * happens. Should be never,
1679 						 * but experience shows
1680 						 * otherwise.
1681 						 * break;
1682 						 */
1683 						mir_krpc_cell_null++;
1684 						freemsg(head_mp);
1685 						break;
1686 					}
1687 					if (mir_check_len(q, head_mp))
1688 						return;
1689 
1690 					if (q->q_first == NULL &&
1691 					    svc_queuereq(q, head_mp, TRUE)) {
1692 						/*
1693 						 * If the reference count is 0
1694 						 * (not including this
1695 						 * request), then the stream is
1696 						 * transitioning from idle to
1697 						 * non-idle.  In this case, we
1698 						 * cancel the idle timer.
1699 						 */
1700 						if (mir->mir_ref_cnt++ == 0)
1701 							stop_timer = B_TRUE;
1702 					} else {
1703 						(void) putq(q, head_mp);
1704 						mir->mir_inrservice = B_TRUE;
1705 					}
1706 				} else {
1707 					/*
1708 					 * If the outbound side of the stream
1709 					 * is flow controlled, then hold this
1710 					 * message until client catches up.
1711 					 * mir_hold_inbound is set in mir_wput
1712 					 * and cleared in mir_wsrv.
1713 					 */
1714 					(void) putq(q, head_mp);
1715 					mir->mir_inrservice = B_TRUE;
1716 				}
1717 			}
1718 			break;
1719 		default:
1720 			RPCLOG(1, "mir_rput: unknown mir_type %d\n",
1721 			    mir->mir_type);
1722 			freemsg(head_mp);
1723 			break;
1724 		}
1725 
1726 		/*
1727 		 * Reset the chain since we're starting on a new RPC message.
1728 		 */
1729 		head_mp = tail_mp = NULL;
1730 	} while ((mp = cont_mp) != NULL);
1731 
1732 	/*
1733 	 * Sanity check the message length; if it's too large mir_check_len()
1734 	 * will shutdown the connection, drop mir_mutex, and return non-zero.
1735 	 */
1736 	if (head_mp != NULL && mir->mir_setup_complete &&
1737 	    mir_check_len(q, head_mp))
1738 		return;
1739 
1740 	/* Save our local copies back in the mir structure. */
1741 	mir->mir_frag_header = frag_header;
1742 	mir->mir_frag_len = frag_len;
1743 	mir->mir_head_mp = head_mp;
1744 	mir->mir_tail_mp = tail_mp;
1745 
1746 	/*
1747 	 * The timer is stopped after the whole message chain is processed.
1748 	 * The reason is that stopping the timer releases the mir_mutex
1749 	 * lock temporarily.  This means that the request can be serviced
1750 	 * while we are still processing the message chain.  This is not
1751 	 * good.  So we stop the timer here instead.
1752 	 *
1753 	 * Note that if the timer fires before we stop it, it will not
1754 	 * do any harm as MIR_SVC_QUIESCED() is false and mir_timer()
1755 	 * will just return.
1756 	 */
1757 	if (stop_timer) {
1758 		RPCLOG(16, "mir_rput: stopping idle timer on 0x%p because "
1759 		    "ref cnt going to non zero\n", (void *)WR(q));
1760 		mir_svc_idle_stop(WR(q), mir);
1761 	}
1762 	mutex_exit(&mir->mir_mutex);
1763 }
1764 
1765 static void
1766 mir_rput_proto(queue_t *q, mblk_t *mp)
1767 {
1768 	mir_t	*mir = (mir_t *)q->q_ptr;
1769 	uint32_t	type;
1770 	uint32_t reason = 0;
1771 
1772 	ASSERT(MUTEX_NOT_HELD(&mir->mir_mutex));
1773 
1774 	type = ((union T_primitives *)mp->b_rptr)->type;
1775 	switch (mir->mir_type) {
1776 	case RPC_CLIENT:
1777 		switch (type) {
1778 		case T_DISCON_IND:
1779 			reason = ((struct T_discon_ind *)
1780 			    (mp->b_rptr))->DISCON_reason;
1781 			/*FALLTHROUGH*/
1782 		case T_ORDREL_IND:
1783 			mutex_enter(&mir->mir_mutex);
1784 			if (mir->mir_head_mp) {
1785 				freemsg(mir->mir_head_mp);
1786 				mir->mir_head_mp = (mblk_t *)0;
1787 				mir->mir_tail_mp = (mblk_t *)0;
1788 			}
1789 			/*
1790 			 * We are disconnecting, but not necessarily
1791 			 * closing. By not closing, we will fail to
1792 			 * pick up a possibly changed global timeout value,
1793 			 * unless we store it now.
1794 			 */
1795 			mir->mir_idle_timeout = clnt_idle_timeout;
1796 			mir_clnt_idle_stop(WR(q), mir);
1797 
1798 			/*
1799 			 * Even though we are unconnected, we still
1800 			 * leave the idle timer going on the client. The
1801 			 * reason for is that if we've disconnected due
1802 			 * to a server-side disconnect, reset, or connection
1803 			 * timeout, there is a possibility the client may
1804 			 * retry the RPC request. This retry needs to done on
1805 			 * the same bound address for the server to interpret
1806 			 * it as such. However, we don't want
1807 			 * to wait forever for that possibility. If the
1808 			 * end-point stays unconnected for mir_idle_timeout
1809 			 * units of time, then that is a signal to the
1810 			 * connection manager to give up waiting for the
1811 			 * application (eg. NFS) to send a retry.
1812 			 */
1813 			mir_clnt_idle_start(WR(q), mir);
1814 			mutex_exit(&mir->mir_mutex);
1815 			clnt_dispatch_notifyall(WR(q), type, reason);
1816 			freemsg(mp);
1817 			return;
1818 		case T_ERROR_ACK:
1819 		{
1820 			struct T_error_ack	*terror;
1821 
1822 			terror = (struct T_error_ack *)mp->b_rptr;
1823 			RPCLOG(1, "mir_rput_proto T_ERROR_ACK for queue 0x%p",
1824 			    (void *)q);
1825 			RPCLOG(1, " ERROR_prim: %s,",
1826 			    rpc_tpiprim2name(terror->ERROR_prim));
1827 			RPCLOG(1, " TLI_error: %s,",
1828 			    rpc_tpierr2name(terror->TLI_error));
1829 			RPCLOG(1, " UNIX_error: %d\n", terror->UNIX_error);
1830 			if (terror->ERROR_prim == T_DISCON_REQ)  {
1831 				clnt_dispatch_notifyall(WR(q), type, reason);
1832 				freemsg(mp);
1833 				return;
1834 			} else {
1835 				if (clnt_dispatch_notifyconn(WR(q), mp))
1836 					return;
1837 			}
1838 			break;
1839 		}
1840 		case T_OK_ACK:
1841 		{
1842 			struct T_ok_ack	*tok = (struct T_ok_ack *)mp->b_rptr;
1843 
1844 			if (tok->CORRECT_prim == T_DISCON_REQ) {
1845 				clnt_dispatch_notifyall(WR(q), type, reason);
1846 				freemsg(mp);
1847 				return;
1848 			} else {
1849 				if (clnt_dispatch_notifyconn(WR(q), mp))
1850 					return;
1851 			}
1852 			break;
1853 		}
1854 		case T_CONN_CON:
1855 		case T_INFO_ACK:
1856 		case T_OPTMGMT_ACK:
1857 			if (clnt_dispatch_notifyconn(WR(q), mp))
1858 				return;
1859 			break;
1860 		case T_BIND_ACK:
1861 			break;
1862 		default:
1863 			RPCLOG(1, "mir_rput: unexpected message %d "
1864 			    "for kRPC client\n",
1865 			    ((union T_primitives *)mp->b_rptr)->type);
1866 			break;
1867 		}
1868 		break;
1869 
1870 	case RPC_SERVER:
1871 		switch (type) {
1872 		case T_BIND_ACK:
1873 		{
1874 			struct T_bind_ack	*tbind;
1875 
1876 			/*
1877 			 * If this is a listening stream, then shut
1878 			 * off the idle timer.
1879 			 */
1880 			tbind = (struct T_bind_ack *)mp->b_rptr;
1881 			if (tbind->CONIND_number > 0) {
1882 				mutex_enter(&mir->mir_mutex);
1883 				mir_svc_idle_stop(WR(q), mir);
1884 
1885 				/*
1886 				 * mark this as a listen endpoint
1887 				 * for special handling.
1888 				 */
1889 
1890 				mir->mir_listen_stream = 1;
1891 				mutex_exit(&mir->mir_mutex);
1892 			}
1893 			break;
1894 		}
1895 		case T_DISCON_IND:
1896 		case T_ORDREL_IND:
1897 			RPCLOG(16, "mir_rput_proto: got %s indication\n",
1898 			    type == T_DISCON_IND ? "disconnect"
1899 			    : "orderly release");
1900 
1901 			/*
1902 			 * For listen endpoint just pass
1903 			 * on the message.
1904 			 */
1905 
1906 			if (mir->mir_listen_stream)
1907 				break;
1908 
1909 			mutex_enter(&mir->mir_mutex);
1910 
1911 			/*
1912 			 * If client wants to break off connection, record
1913 			 * that fact.
1914 			 */
1915 			mir_svc_start_close(WR(q), mir);
1916 
1917 			/*
1918 			 * If we are idle, then send the orderly release
1919 			 * or disconnect indication to nfsd.
1920 			 */
1921 			if (MIR_SVC_QUIESCED(mir)) {
1922 				mutex_exit(&mir->mir_mutex);
1923 				break;
1924 			}
1925 
1926 			RPCLOG(16, "mir_rput_proto: not idle, so "
1927 			    "disconnect/ord rel indication not passed "
1928 			    "upstream on 0x%p\n", (void *)q);
1929 
1930 			/*
1931 			 * Hold the indication until we get idle
1932 			 * If there already is an indication stored,
1933 			 * replace it if the new one is a disconnect. The
1934 			 * reasoning is that disconnection takes less time
1935 			 * to process, and once a client decides to
1936 			 * disconnect, we should do that.
1937 			 */
1938 			if (mir->mir_svc_pend_mp) {
1939 				if (type == T_DISCON_IND) {
1940 					RPCLOG(16, "mir_rput_proto: replacing"
1941 					    " held disconnect/ord rel"
1942 					    " indication with disconnect on"
1943 					    " 0x%p\n", (void *)q);
1944 
1945 					freemsg(mir->mir_svc_pend_mp);
1946 					mir->mir_svc_pend_mp = mp;
1947 				} else {
1948 					RPCLOG(16, "mir_rput_proto: already "
1949 					    "held a disconnect/ord rel "
1950 					    "indication. freeing ord rel "
1951 					    "ind on 0x%p\n", (void *)q);
1952 					freemsg(mp);
1953 				}
1954 			} else
1955 				mir->mir_svc_pend_mp = mp;
1956 
1957 			mutex_exit(&mir->mir_mutex);
1958 			return;
1959 
1960 		default:
1961 			/* nfsd handles server-side non-data messages. */
1962 			break;
1963 		}
1964 		break;
1965 
1966 	default:
1967 		break;
1968 	}
1969 
1970 	putnext(q, mp);
1971 }
1972 
1973 /*
1974  * The server-side read queues are used to hold inbound messages while
1975  * outbound flow control is exerted.  When outbound flow control is
1976  * relieved, mir_wsrv qenables the read-side queue.  Read-side queues
1977  * are not enabled by STREAMS and are explicitly noenable'ed in mir_open.
1978  */
1979 static void
1980 mir_rsrv(queue_t *q)
1981 {
1982 	mir_t	*mir;
1983 	mblk_t	*mp;
1984 	boolean_t stop_timer = B_FALSE;
1985 
1986 	mir = (mir_t *)q->q_ptr;
1987 	mutex_enter(&mir->mir_mutex);
1988 
1989 	mp = NULL;
1990 	switch (mir->mir_type) {
1991 	case RPC_SERVER:
1992 		if (mir->mir_ref_cnt == 0)
1993 			mir->mir_hold_inbound = 0;
1994 		if (mir->mir_hold_inbound)
1995 			break;
1996 
1997 		while (mp = getq(q)) {
1998 			if (mir->mir_krpc_cell &&
1999 			    (mir->mir_svc_no_more_msgs == 0)) {
2000 
2001 				if (mir_check_len(q, mp))
2002 					return;
2003 
2004 				if (svc_queuereq(q, mp, TRUE)) {
2005 					/*
2006 					 * If we were idle, turn off idle timer
2007 					 * since we aren't idle any more.
2008 					 */
2009 					if (mir->mir_ref_cnt++ == 0)
2010 						stop_timer = B_TRUE;
2011 				} else {
2012 					(void) putbq(q, mp);
2013 					break;
2014 				}
2015 			} else {
2016 				/*
2017 				 * Count # of times this happens. Should be
2018 				 * never, but experience shows otherwise.
2019 				 */
2020 				if (mir->mir_krpc_cell == NULL)
2021 					mir_krpc_cell_null++;
2022 				freemsg(mp);
2023 			}
2024 		}
2025 		break;
2026 	case RPC_CLIENT:
2027 		break;
2028 	default:
2029 		RPCLOG(1, "mir_rsrv: unexpected mir_type %d\n", mir->mir_type);
2030 
2031 		if (q->q_first == NULL)
2032 			MIR_CLEAR_INRSRV(mir);
2033 
2034 		mutex_exit(&mir->mir_mutex);
2035 
2036 		return;
2037 	}
2038 
2039 	/*
2040 	 * The timer is stopped after all the messages are processed.
2041 	 * The reason is that stopping the timer releases the mir_mutex
2042 	 * lock temporarily.  This means that the request can be serviced
2043 	 * while we are still processing the message queue.  This is not
2044 	 * good.  So we stop the timer here instead.
2045 	 */
2046 	if (stop_timer)  {
2047 		RPCLOG(16, "mir_rsrv stopping idle timer on 0x%p because ref "
2048 		    "cnt going to non zero\n", (void *)WR(q));
2049 		mir_svc_idle_stop(WR(q), mir);
2050 	}
2051 
2052 	if (q->q_first == NULL) {
2053 		mblk_t	*cmp = NULL;
2054 
2055 		MIR_CLEAR_INRSRV(mir);
2056 
2057 		if (mir->mir_type == RPC_SERVER && MIR_SVC_QUIESCED(mir)) {
2058 			cmp = mir->mir_svc_pend_mp;
2059 			mir->mir_svc_pend_mp = NULL;
2060 		}
2061 
2062 		mutex_exit(&mir->mir_mutex);
2063 
2064 		if (cmp != NULL) {
2065 			RPCLOG(16, "mir_rsrv: line %d: sending a held "
2066 			    "disconnect/ord rel indication upstream\n",
2067 			    __LINE__);
2068 			putnext(q, cmp);
2069 		}
2070 
2071 		return;
2072 	}
2073 	mutex_exit(&mir->mir_mutex);
2074 }
2075 
2076 static int mir_svc_policy_fails;
2077 
2078 /*
2079  * Called to send an event code to nfsd/lockd so that it initiates
2080  * connection close.
2081  */
2082 static int
2083 mir_svc_policy_notify(queue_t *q, int event)
2084 {
2085 	mblk_t	*mp;
2086 #ifdef DEBUG
2087 	mir_t *mir = (mir_t *)q->q_ptr;
2088 	ASSERT(MUTEX_NOT_HELD(&mir->mir_mutex));
2089 #endif
2090 	ASSERT(q->q_flag & QREADR);
2091 
2092 	/*
2093 	 * Create an M_DATA message with the event code and pass it to the
2094 	 * Stream head (nfsd or whoever created the stream will consume it).
2095 	 */
2096 	mp = allocb(sizeof (int), BPRI_HI);
2097 
2098 	if (!mp) {
2099 
2100 		mir_svc_policy_fails++;
2101 		RPCLOG(16, "mir_svc_policy_notify: could not allocate event "
2102 		    "%d\n", event);
2103 		return (ENOMEM);
2104 	}
2105 
2106 	U32_TO_BE32(event, mp->b_rptr);
2107 	mp->b_wptr = mp->b_rptr + sizeof (int);
2108 	putnext(q, mp);
2109 	return (0);
2110 }
2111 
2112 /*
2113  * Server side: start the close phase. We want to get this rpcmod slot in an
2114  * idle state before mir_close() is called.
2115  */
2116 static void
2117 mir_svc_start_close(queue_t *wq, mir_t *mir)
2118 {
2119 	ASSERT(MUTEX_HELD(&mir->mir_mutex));
2120 	ASSERT((wq->q_flag & QREADR) == 0);
2121 	ASSERT(mir->mir_type == RPC_SERVER);
2122 
2123 	/*
2124 	 * Do not accept any more messages.
2125 	 */
2126 	mir->mir_svc_no_more_msgs = 1;
2127 
2128 	/*
2129 	 * Next two statements will make the read service procedure
2130 	 * free everything stuck in the streams read queue.
2131 	 * It's not necessary because enabling the write queue will
2132 	 * have the same effect, but why not speed the process along?
2133 	 */
2134 	mir->mir_hold_inbound = 0;
2135 	qenable(RD(wq));
2136 
2137 	/*
2138 	 * Meanwhile force the write service procedure to send the
2139 	 * responses downstream, regardless of flow control.
2140 	 */
2141 	qenable(wq);
2142 }
2143 
2144 void
2145 mir_svc_hold(queue_t *wq)
2146 {
2147 	mir_t *mir = (mir_t *)wq->q_ptr;
2148 
2149 	mutex_enter(&mir->mir_mutex);
2150 	mir->mir_ref_cnt++;
2151 	mutex_exit(&mir->mir_mutex);
2152 }
2153 
2154 /*
2155  * This routine is called directly by kRPC after a request is completed,
2156  * whether a reply was sent or the request was dropped.
2157  */
2158 void
2159 mir_svc_release(queue_t *wq, mblk_t *mp, bool_t enable)
2160 {
2161 	mir_t   *mir = (mir_t *)wq->q_ptr;
2162 	mblk_t	*cmp = NULL;
2163 
2164 	ASSERT((wq->q_flag & QREADR) == 0);
2165 	if (mp)
2166 		freemsg(mp);
2167 
2168 	if (enable)
2169 		qenable(RD(wq));
2170 
2171 	mutex_enter(&mir->mir_mutex);
2172 
2173 	/*
2174 	 * Start idle processing if this is the last reference.
2175 	 */
2176 	if ((mir->mir_ref_cnt == 1) && (mir->mir_inrservice == 0)) {
2177 		cmp = mir->mir_svc_pend_mp;
2178 		mir->mir_svc_pend_mp = NULL;
2179 	}
2180 
2181 	if (cmp) {
2182 		RPCLOG(16, "mir_svc_release: sending a held "
2183 		    "disconnect/ord rel indication upstream on queue 0x%p\n",
2184 		    (void *)RD(wq));
2185 
2186 		mutex_exit(&mir->mir_mutex);
2187 
2188 		putnext(RD(wq), cmp);
2189 
2190 		mutex_enter(&mir->mir_mutex);
2191 	}
2192 
2193 	/*
2194 	 * Start idle processing if this is the last reference.
2195 	 */
2196 	if (mir->mir_ref_cnt == 1 && mir->mir_inrservice == 0) {
2197 
2198 		RPCLOG(16, "mir_svc_release starting idle timer on 0x%p "
2199 		    "because ref cnt is zero\n", (void *) wq);
2200 
2201 		mir_svc_idle_start(wq, mir);
2202 	}
2203 
2204 	mir->mir_ref_cnt--;
2205 	ASSERT(mir->mir_ref_cnt >= 0);
2206 
2207 	/*
2208 	 * Wake up the thread waiting to close.
2209 	 */
2210 
2211 	if ((mir->mir_ref_cnt == 0) && mir->mir_closing)
2212 		cv_signal(&mir->mir_condvar);
2213 
2214 	mutex_exit(&mir->mir_mutex);
2215 }
2216 
2217 /*
2218  * This routine is called by server-side kRPC when it is ready to
2219  * handle inbound messages on the stream.
2220  */
2221 static void
2222 mir_svc_start(queue_t *wq)
2223 {
2224 	mir_t   *mir = (mir_t *)wq->q_ptr;
2225 
2226 	/*
2227 	 * no longer need to take the mir_mutex because the
2228 	 * mir_setup_complete field has been moved out of
2229 	 * the binary field protected by the mir_mutex.
2230 	 */
2231 
2232 	mir->mir_setup_complete = 1;
2233 	qenable(RD(wq));
2234 }
2235 
2236 /*
2237  * client side wrapper for stopping timer with normal idle timeout.
2238  */
2239 static void
2240 mir_clnt_idle_stop(queue_t *wq, mir_t *mir)
2241 {
2242 	ASSERT(MUTEX_HELD(&mir->mir_mutex));
2243 	ASSERT((wq->q_flag & QREADR) == 0);
2244 	ASSERT(mir->mir_type == RPC_CLIENT);
2245 
2246 	mir_timer_stop(mir);
2247 }
2248 
2249 /*
2250  * client side wrapper for stopping timer with normal idle timeout.
2251  */
2252 static void
2253 mir_clnt_idle_start(queue_t *wq, mir_t *mir)
2254 {
2255 	ASSERT(MUTEX_HELD(&mir->mir_mutex));
2256 	ASSERT((wq->q_flag & QREADR) == 0);
2257 	ASSERT(mir->mir_type == RPC_CLIENT);
2258 
2259 	mir_timer_start(wq, mir, mir->mir_idle_timeout);
2260 }
2261 
2262 /*
2263  * client side only. Forces rpcmod to stop sending T_ORDREL_REQs on
2264  * end-points that aren't connected.
2265  */
2266 static void
2267 mir_clnt_idle_do_stop(queue_t *wq)
2268 {
2269 	mir_t   *mir = (mir_t *)wq->q_ptr;
2270 
2271 	RPCLOG(1, "mir_clnt_idle_do_stop: wq 0x%p\n", (void *)wq);
2272 	ASSERT(MUTEX_NOT_HELD(&mir->mir_mutex));
2273 	mutex_enter(&mir->mir_mutex);
2274 	mir_clnt_idle_stop(wq, mir);
2275 	mutex_exit(&mir->mir_mutex);
2276 }
2277 
2278 /*
2279  * Timer handler.  It handles idle timeout and memory shortage problem.
2280  */
2281 static void
2282 mir_timer(void *arg)
2283 {
2284 	queue_t *wq = (queue_t *)arg;
2285 	mir_t *mir = (mir_t *)wq->q_ptr;
2286 	boolean_t notify;
2287 	clock_t now;
2288 
2289 	mutex_enter(&mir->mir_mutex);
2290 
2291 	/*
2292 	 * mir_timer_call is set only when either mir_timer_[start|stop]
2293 	 * is progressing.  And mir_timer() can only be run while they
2294 	 * are progressing if the timer is being stopped.  So just
2295 	 * return.
2296 	 */
2297 	if (mir->mir_timer_call) {
2298 		mutex_exit(&mir->mir_mutex);
2299 		return;
2300 	}
2301 	mir->mir_timer_id = 0;
2302 
2303 	switch (mir->mir_type) {
2304 	case RPC_CLIENT:
2305 
2306 		/*
2307 		 * For clients, the timer fires at clnt_idle_timeout
2308 		 * intervals.  If the activity marker (mir_clntreq) is
2309 		 * zero, then the stream has been idle since the last
2310 		 * timer event and we notify kRPC.  If mir_clntreq is
2311 		 * non-zero, then the stream is active and we just
2312 		 * restart the timer for another interval.  mir_clntreq
2313 		 * is set to 1 in mir_wput for every request passed
2314 		 * downstream.
2315 		 *
2316 		 * If this was a memory shortage timer reset the idle
2317 		 * timeout regardless; the mir_clntreq will not be a
2318 		 * valid indicator.
2319 		 *
2320 		 * The timer is initially started in mir_wput during
2321 		 * RPC_CLIENT ioctl processing.
2322 		 *
2323 		 * The timer interval can be changed for individual
2324 		 * streams with the ND variable "mir_idle_timeout".
2325 		 */
2326 		now = ddi_get_lbolt();
2327 		if (mir->mir_clntreq > 0 && mir->mir_use_timestamp +
2328 		    MSEC_TO_TICK(mir->mir_idle_timeout) - now >= 0) {
2329 			clock_t tout;
2330 
2331 			tout = mir->mir_idle_timeout -
2332 			    TICK_TO_MSEC(now - mir->mir_use_timestamp);
2333 			if (tout < 0)
2334 				tout = 1000;
2335 #if 0
2336 			printf("mir_timer[%d < %d + %d]: reset client timer "
2337 			    "to %d (ms)\n", TICK_TO_MSEC(now),
2338 			    TICK_TO_MSEC(mir->mir_use_timestamp),
2339 			    mir->mir_idle_timeout, tout);
2340 #endif
2341 			mir->mir_clntreq = 0;
2342 			mir_timer_start(wq, mir, tout);
2343 			mutex_exit(&mir->mir_mutex);
2344 			return;
2345 		}
2346 #if 0
2347 printf("mir_timer[%d]: doing client timeout\n", now / hz);
2348 #endif
2349 		/*
2350 		 * We are disconnecting, but not necessarily
2351 		 * closing. By not closing, we will fail to
2352 		 * pick up a possibly changed global timeout value,
2353 		 * unless we store it now.
2354 		 */
2355 		mir->mir_idle_timeout = clnt_idle_timeout;
2356 		mir_clnt_idle_start(wq, mir);
2357 
2358 		mutex_exit(&mir->mir_mutex);
2359 		/*
2360 		 * We pass T_ORDREL_REQ as an integer value
2361 		 * to kRPC as the indication that the stream
2362 		 * is idle.  This is not a T_ORDREL_REQ message,
2363 		 * it is just a convenient value since we call
2364 		 * the same kRPC routine for T_ORDREL_INDs and
2365 		 * T_DISCON_INDs.
2366 		 */
2367 		clnt_dispatch_notifyall(wq, T_ORDREL_REQ, 0);
2368 		return;
2369 
2370 	case RPC_SERVER:
2371 
2372 		/*
2373 		 * For servers, the timer is only running when the stream
2374 		 * is really idle or memory is short.  The timer is started
2375 		 * by mir_wput when mir_type is set to RPC_SERVER and
2376 		 * by mir_svc_idle_start whenever the stream goes idle
2377 		 * (mir_ref_cnt == 0).  The timer is cancelled in
2378 		 * mir_rput whenever a new inbound request is passed to kRPC
2379 		 * and the stream was previously idle.
2380 		 *
2381 		 * The timer interval can be changed for individual
2382 		 * streams with the ND variable "mir_idle_timeout".
2383 		 *
2384 		 * If the stream is not idle do nothing.
2385 		 */
2386 		if (!MIR_SVC_QUIESCED(mir)) {
2387 			mutex_exit(&mir->mir_mutex);
2388 			return;
2389 		}
2390 
2391 		notify = !mir->mir_inrservice;
2392 		mutex_exit(&mir->mir_mutex);
2393 
2394 		/*
2395 		 * If there is no packet queued up in read queue, the stream
2396 		 * is really idle so notify nfsd to close it.
2397 		 */
2398 		if (notify) {
2399 			RPCLOG(16, "mir_timer: telling stream head listener "
2400 			    "to close stream (0x%p)\n", (void *) RD(wq));
2401 			(void) mir_svc_policy_notify(RD(wq), 1);
2402 		}
2403 		return;
2404 	default:
2405 		RPCLOG(1, "mir_timer: unexpected mir_type %d\n",
2406 		    mir->mir_type);
2407 		mutex_exit(&mir->mir_mutex);
2408 		return;
2409 	}
2410 }
2411 
2412 /*
2413  * Called by the RPC package to send either a call or a return, or a
2414  * transport connection request.  Adds the record marking header.
2415  */
2416 static void
2417 mir_wput(queue_t *q, mblk_t *mp)
2418 {
2419 	uint_t	frag_header;
2420 	mir_t	*mir = (mir_t *)q->q_ptr;
2421 	uchar_t	*rptr = mp->b_rptr;
2422 
2423 	if (!mir) {
2424 		freemsg(mp);
2425 		return;
2426 	}
2427 
2428 	if (mp->b_datap->db_type != M_DATA) {
2429 		mir_wput_other(q, mp);
2430 		return;
2431 	}
2432 
2433 	if (mir->mir_ordrel_pending == 1) {
2434 		freemsg(mp);
2435 		RPCLOG(16, "mir_wput wq 0x%p: got data after T_ORDREL_REQ\n",
2436 		    (void *)q);
2437 		return;
2438 	}
2439 
2440 	frag_header = (uint_t)DLEN(mp);
2441 	frag_header |= MIR_LASTFRAG;
2442 
2443 	/* Stick in the 4 byte record marking header. */
2444 	if ((rptr - mp->b_datap->db_base) < sizeof (uint32_t) ||
2445 	    !IS_P2ALIGNED(mp->b_rptr, sizeof (uint32_t))) {
2446 		/*
2447 		 * Since we know that M_DATA messages are created exclusively
2448 		 * by kRPC, we expect that kRPC will leave room for our header
2449 		 * and 4 byte align which is normal for XDR.
2450 		 * If kRPC (or someone else) does not cooperate, then we
2451 		 * just throw away the message.
2452 		 */
2453 		RPCLOG(1, "mir_wput: kRPC did not leave space for record "
2454 		    "fragment header (%d bytes left)\n",
2455 		    (int)(rptr - mp->b_datap->db_base));
2456 		freemsg(mp);
2457 		return;
2458 	}
2459 	rptr -= sizeof (uint32_t);
2460 	*(uint32_t *)rptr = htonl(frag_header);
2461 	mp->b_rptr = rptr;
2462 
2463 	mutex_enter(&mir->mir_mutex);
2464 	if (mir->mir_type == RPC_CLIENT) {
2465 		/*
2466 		 * For the client, set mir_clntreq to indicate that the
2467 		 * connection is active.
2468 		 */
2469 		mir->mir_clntreq = 1;
2470 		mir->mir_use_timestamp = ddi_get_lbolt();
2471 	}
2472 
2473 	/*
2474 	 * If we haven't already queued some data and the downstream module
2475 	 * can accept more data, send it on, otherwise we queue the message
2476 	 * and take other actions depending on mir_type.
2477 	 */
2478 	if (!mir->mir_inwservice && MIR_WCANPUTNEXT(mir, q)) {
2479 		mutex_exit(&mir->mir_mutex);
2480 
2481 		/*
2482 		 * Now we pass the RPC message downstream.
2483 		 */
2484 		putnext(q, mp);
2485 		return;
2486 	}
2487 
2488 	switch (mir->mir_type) {
2489 	case RPC_CLIENT:
2490 		/*
2491 		 * Check for a previous duplicate request on the
2492 		 * queue.  If there is one, then we throw away
2493 		 * the current message and let the previous one
2494 		 * go through.  If we can't find a duplicate, then
2495 		 * send this one.  This tap dance is an effort
2496 		 * to reduce traffic and processing requirements
2497 		 * under load conditions.
2498 		 */
2499 		if (mir_clnt_dup_request(q, mp)) {
2500 			mutex_exit(&mir->mir_mutex);
2501 			freemsg(mp);
2502 			return;
2503 		}
2504 		break;
2505 	case RPC_SERVER:
2506 		/*
2507 		 * Set mir_hold_inbound so that new inbound RPC
2508 		 * messages will be held until the client catches
2509 		 * up on the earlier replies.  This flag is cleared
2510 		 * in mir_wsrv after flow control is relieved;
2511 		 * the read-side queue is also enabled at that time.
2512 		 */
2513 		mir->mir_hold_inbound = 1;
2514 		break;
2515 	default:
2516 		RPCLOG(1, "mir_wput: unexpected mir_type %d\n", mir->mir_type);
2517 		break;
2518 	}
2519 	mir->mir_inwservice = 1;
2520 	(void) putq(q, mp);
2521 	mutex_exit(&mir->mir_mutex);
2522 }
2523 
2524 static void
2525 mir_wput_other(queue_t *q, mblk_t *mp)
2526 {
2527 	mir_t	*mir = (mir_t *)q->q_ptr;
2528 	struct iocblk	*iocp;
2529 	uchar_t	*rptr = mp->b_rptr;
2530 	bool_t	flush_in_svc = FALSE;
2531 
2532 	ASSERT(MUTEX_NOT_HELD(&mir->mir_mutex));
2533 	switch (mp->b_datap->db_type) {
2534 	case M_IOCTL:
2535 		iocp = (struct iocblk *)rptr;
2536 		switch (iocp->ioc_cmd) {
2537 		case RPC_CLIENT:
2538 			mutex_enter(&mir->mir_mutex);
2539 			if (mir->mir_type != 0 &&
2540 			    mir->mir_type != iocp->ioc_cmd) {
2541 ioc_eperm:
2542 				mutex_exit(&mir->mir_mutex);
2543 				iocp->ioc_error = EPERM;
2544 				iocp->ioc_count = 0;
2545 				mp->b_datap->db_type = M_IOCACK;
2546 				qreply(q, mp);
2547 				return;
2548 			}
2549 
2550 			mir->mir_type = iocp->ioc_cmd;
2551 
2552 			/*
2553 			 * Clear mir_hold_inbound which was set to 1 by
2554 			 * mir_open.  This flag is not used on client
2555 			 * streams.
2556 			 */
2557 			mir->mir_hold_inbound = 0;
2558 			mir->mir_max_msg_sizep = &clnt_max_msg_size;
2559 
2560 			/*
2561 			 * Start the idle timer.  See mir_timer() for more
2562 			 * information on how client timers work.
2563 			 */
2564 			mir->mir_idle_timeout = clnt_idle_timeout;
2565 			mir_clnt_idle_start(q, mir);
2566 			mutex_exit(&mir->mir_mutex);
2567 
2568 			mp->b_datap->db_type = M_IOCACK;
2569 			qreply(q, mp);
2570 			return;
2571 		case RPC_SERVER:
2572 			mutex_enter(&mir->mir_mutex);
2573 			if (mir->mir_type != 0 &&
2574 			    mir->mir_type != iocp->ioc_cmd)
2575 				goto ioc_eperm;
2576 
2577 			/*
2578 			 * We don't clear mir_hold_inbound here because
2579 			 * mir_hold_inbound is used in the flow control
2580 			 * model. If we cleared it here, then we'd commit
2581 			 * a small violation to the model where the transport
2582 			 * might immediately block downstream flow.
2583 			 */
2584 
2585 			mir->mir_type = iocp->ioc_cmd;
2586 			mir->mir_max_msg_sizep = &svc_max_msg_size;
2587 
2588 			/*
2589 			 * Start the idle timer.  See mir_timer() for more
2590 			 * information on how server timers work.
2591 			 *
2592 			 * Note that it is important to start the idle timer
2593 			 * here so that connections time out even if we
2594 			 * never receive any data on them.
2595 			 */
2596 			mir->mir_idle_timeout = svc_idle_timeout;
2597 			RPCLOG(16, "mir_wput_other starting idle timer on 0x%p "
2598 			    "because we got RPC_SERVER ioctl\n", (void *)q);
2599 			mir_svc_idle_start(q, mir);
2600 			mutex_exit(&mir->mir_mutex);
2601 
2602 			mp->b_datap->db_type = M_IOCACK;
2603 			qreply(q, mp);
2604 			return;
2605 		default:
2606 			break;
2607 		}
2608 		break;
2609 
2610 	case M_PROTO:
2611 		if (mir->mir_type == RPC_CLIENT) {
2612 			/*
2613 			 * We are likely being called from the context of a
2614 			 * service procedure. So we need to enqueue. However
2615 			 * enqueing may put our message behind data messages.
2616 			 * So flush the data first.
2617 			 */
2618 			flush_in_svc = TRUE;
2619 		}
2620 		if ((mp->b_wptr - rptr) < sizeof (uint32_t) ||
2621 		    !IS_P2ALIGNED(rptr, sizeof (uint32_t)))
2622 			break;
2623 
2624 		switch (((union T_primitives *)rptr)->type) {
2625 		case T_DATA_REQ:
2626 			/* Don't pass T_DATA_REQ messages downstream. */
2627 			freemsg(mp);
2628 			return;
2629 		case T_ORDREL_REQ:
2630 			RPCLOG(8, "mir_wput_other wq 0x%p: got T_ORDREL_REQ\n",
2631 			    (void *)q);
2632 			mutex_enter(&mir->mir_mutex);
2633 			if (mir->mir_type != RPC_SERVER) {
2634 				/*
2635 				 * We are likely being called from
2636 				 * clnt_dispatch_notifyall(). Sending
2637 				 * a T_ORDREL_REQ will result in
2638 				 * a some kind of _IND message being sent,
2639 				 * will be another call to
2640 				 * clnt_dispatch_notifyall(). To keep the stack
2641 				 * lean, queue this message.
2642 				 */
2643 				mir->mir_inwservice = 1;
2644 				(void) putq(q, mp);
2645 				mutex_exit(&mir->mir_mutex);
2646 				return;
2647 			}
2648 
2649 			/*
2650 			 * Mark the structure such that we don't accept any
2651 			 * more requests from client. We could defer this
2652 			 * until we actually send the orderly release
2653 			 * request downstream, but all that does is delay
2654 			 * the closing of this stream.
2655 			 */
2656 			RPCLOG(16, "mir_wput_other wq 0x%p: got T_ORDREL_REQ "
2657 			    " so calling mir_svc_start_close\n", (void *)q);
2658 
2659 			mir_svc_start_close(q, mir);
2660 
2661 			/*
2662 			 * If we have sent down a T_ORDREL_REQ, don't send
2663 			 * any more.
2664 			 */
2665 			if (mir->mir_ordrel_pending) {
2666 				freemsg(mp);
2667 				mutex_exit(&mir->mir_mutex);
2668 				return;
2669 			}
2670 
2671 			/*
2672 			 * If the stream is not idle, then we hold the
2673 			 * orderly release until it becomes idle.  This
2674 			 * ensures that kRPC will be able to reply to
2675 			 * all requests that we have passed to it.
2676 			 *
2677 			 * We also queue the request if there is data already
2678 			 * queued, because we cannot allow the T_ORDREL_REQ
2679 			 * to go before data. When we had a separate reply
2680 			 * count, this was not a problem, because the
2681 			 * reply count was reconciled when mir_wsrv()
2682 			 * completed.
2683 			 */
2684 			if (!MIR_SVC_QUIESCED(mir) ||
2685 			    mir->mir_inwservice == 1) {
2686 				mir->mir_inwservice = 1;
2687 				(void) putq(q, mp);
2688 
2689 				RPCLOG(16, "mir_wput_other: queuing "
2690 				    "T_ORDREL_REQ on 0x%p\n", (void *)q);
2691 
2692 				mutex_exit(&mir->mir_mutex);
2693 				return;
2694 			}
2695 
2696 			/*
2697 			 * Mark the structure so that we know we sent
2698 			 * an orderly release request, and reset the idle timer.
2699 			 */
2700 			mir->mir_ordrel_pending = 1;
2701 
2702 			RPCLOG(16, "mir_wput_other: calling mir_svc_idle_start"
2703 			    " on 0x%p because we got T_ORDREL_REQ\n",
2704 			    (void *)q);
2705 
2706 			mir_svc_idle_start(q, mir);
2707 			mutex_exit(&mir->mir_mutex);
2708 
2709 			/*
2710 			 * When we break, we will putnext the T_ORDREL_REQ.
2711 			 */
2712 			break;
2713 
2714 		case T_CONN_REQ:
2715 			mutex_enter(&mir->mir_mutex);
2716 			if (mir->mir_head_mp != NULL) {
2717 				freemsg(mir->mir_head_mp);
2718 				mir->mir_head_mp = NULL;
2719 				mir->mir_tail_mp = NULL;
2720 			}
2721 			mir->mir_frag_len = -(int32_t)sizeof (uint32_t);
2722 			/*
2723 			 * Restart timer in case mir_clnt_idle_do_stop() was
2724 			 * called.
2725 			 */
2726 			mir->mir_idle_timeout = clnt_idle_timeout;
2727 			mir_clnt_idle_stop(q, mir);
2728 			mir_clnt_idle_start(q, mir);
2729 			mutex_exit(&mir->mir_mutex);
2730 			break;
2731 
2732 		default:
2733 			/*
2734 			 * T_DISCON_REQ is one of the interesting default
2735 			 * cases here. Ideally, an M_FLUSH is done before
2736 			 * T_DISCON_REQ is done. However, that is somewhat
2737 			 * cumbersome for clnt_cots.c to do. So we queue
2738 			 * T_DISCON_REQ, and let the service procedure
2739 			 * flush all M_DATA.
2740 			 */
2741 			break;
2742 		}
2743 		/* FALLTHROUGH */
2744 	default:
2745 		if (mp->b_datap->db_type >= QPCTL) {
2746 			if (mp->b_datap->db_type == M_FLUSH) {
2747 				if (mir->mir_type == RPC_CLIENT &&
2748 				    *mp->b_rptr & FLUSHW) {
2749 					RPCLOG(32, "mir_wput_other: flushing "
2750 					    "wq 0x%p\n", (void *)q);
2751 					if (*mp->b_rptr & FLUSHBAND) {
2752 						flushband(q, *(mp->b_rptr + 1),
2753 						    FLUSHDATA);
2754 					} else {
2755 						flushq(q, FLUSHDATA);
2756 					}
2757 				} else {
2758 					RPCLOG(32, "mir_wput_other: ignoring "
2759 					    "M_FLUSH on wq 0x%p\n", (void *)q);
2760 				}
2761 			}
2762 			break;
2763 		}
2764 
2765 		mutex_enter(&mir->mir_mutex);
2766 		if (mir->mir_inwservice == 0 && MIR_WCANPUTNEXT(mir, q)) {
2767 			mutex_exit(&mir->mir_mutex);
2768 			break;
2769 		}
2770 		mir->mir_inwservice = 1;
2771 		mir->mir_inwflushdata = flush_in_svc;
2772 		(void) putq(q, mp);
2773 		mutex_exit(&mir->mir_mutex);
2774 		qenable(q);
2775 
2776 		return;
2777 	}
2778 	putnext(q, mp);
2779 }
2780 
2781 static void
2782 mir_wsrv(queue_t *q)
2783 {
2784 	mblk_t	*mp;
2785 	mir_t	*mir;
2786 	bool_t flushdata;
2787 
2788 	mir = (mir_t *)q->q_ptr;
2789 	mutex_enter(&mir->mir_mutex);
2790 
2791 	flushdata = mir->mir_inwflushdata;
2792 	mir->mir_inwflushdata = 0;
2793 
2794 	while (mp = getq(q)) {
2795 		if (mp->b_datap->db_type == M_DATA) {
2796 			/*
2797 			 * Do not send any more data if we have sent
2798 			 * a T_ORDREL_REQ.
2799 			 */
2800 			if (flushdata || mir->mir_ordrel_pending == 1) {
2801 				freemsg(mp);
2802 				continue;
2803 			}
2804 
2805 			/*
2806 			 * Make sure that the stream can really handle more
2807 			 * data.
2808 			 */
2809 			if (!MIR_WCANPUTNEXT(mir, q)) {
2810 				(void) putbq(q, mp);
2811 				mutex_exit(&mir->mir_mutex);
2812 				return;
2813 			}
2814 
2815 			/*
2816 			 * Now we pass the RPC message downstream.
2817 			 */
2818 			mutex_exit(&mir->mir_mutex);
2819 			putnext(q, mp);
2820 			mutex_enter(&mir->mir_mutex);
2821 			continue;
2822 		}
2823 
2824 		/*
2825 		 * This is not an RPC message, pass it downstream
2826 		 * (ignoring flow control) if the server side is not sending a
2827 		 * T_ORDREL_REQ downstream.
2828 		 */
2829 		if (mir->mir_type != RPC_SERVER ||
2830 		    ((union T_primitives *)mp->b_rptr)->type !=
2831 		    T_ORDREL_REQ) {
2832 			mutex_exit(&mir->mir_mutex);
2833 			putnext(q, mp);
2834 			mutex_enter(&mir->mir_mutex);
2835 			continue;
2836 		}
2837 
2838 		if (mir->mir_ordrel_pending == 1) {
2839 			/*
2840 			 * Don't send two T_ORDRELs
2841 			 */
2842 			freemsg(mp);
2843 			continue;
2844 		}
2845 
2846 		/*
2847 		 * Mark the structure so that we know we sent an orderly
2848 		 * release request.  We will check to see slot is idle at the
2849 		 * end of this routine, and if so, reset the idle timer to
2850 		 * handle orderly release timeouts.
2851 		 */
2852 		mir->mir_ordrel_pending = 1;
2853 		RPCLOG(16, "mir_wsrv: sending ordrel req on q 0x%p\n",
2854 		    (void *)q);
2855 		/*
2856 		 * Send the orderly release downstream. If there are other
2857 		 * pending replies we won't be able to send them.  However,
2858 		 * the only reason we should send the orderly release is if
2859 		 * we were idle, or if an unusual event occurred.
2860 		 */
2861 		mutex_exit(&mir->mir_mutex);
2862 		putnext(q, mp);
2863 		mutex_enter(&mir->mir_mutex);
2864 	}
2865 
2866 	if (q->q_first == NULL)
2867 		/*
2868 		 * If we call mir_svc_idle_start() below, then
2869 		 * clearing mir_inwservice here will also result in
2870 		 * any thread waiting in mir_close() to be signaled.
2871 		 */
2872 		mir->mir_inwservice = 0;
2873 
2874 	if (mir->mir_type != RPC_SERVER) {
2875 		mutex_exit(&mir->mir_mutex);
2876 		return;
2877 	}
2878 
2879 	/*
2880 	 * If idle we call mir_svc_idle_start to start the timer (or wakeup
2881 	 * a close). Also make sure not to start the idle timer on the
2882 	 * listener stream. This can cause nfsd to send an orderly release
2883 	 * command on the listener stream.
2884 	 */
2885 	if (MIR_SVC_QUIESCED(mir) && !(mir->mir_listen_stream)) {
2886 		RPCLOG(16, "mir_wsrv: calling mir_svc_idle_start on 0x%p "
2887 		    "because mir slot is idle\n", (void *)q);
2888 		mir_svc_idle_start(q, mir);
2889 	}
2890 
2891 	/*
2892 	 * If outbound flow control has been relieved, then allow new
2893 	 * inbound requests to be processed.
2894 	 */
2895 	if (mir->mir_hold_inbound) {
2896 		mir->mir_hold_inbound = 0;
2897 		qenable(RD(q));
2898 	}
2899 	mutex_exit(&mir->mir_mutex);
2900 }
2901 
2902 static void
2903 mir_disconnect(queue_t *q, mir_t *mir)
2904 {
2905 	ASSERT(MUTEX_HELD(&mir->mir_mutex));
2906 
2907 	switch (mir->mir_type) {
2908 	case RPC_CLIENT:
2909 		/*
2910 		 * We are disconnecting, but not necessarily
2911 		 * closing. By not closing, we will fail to
2912 		 * pick up a possibly changed global timeout value,
2913 		 * unless we store it now.
2914 		 */
2915 		mir->mir_idle_timeout = clnt_idle_timeout;
2916 		mir_clnt_idle_start(WR(q), mir);
2917 		mutex_exit(&mir->mir_mutex);
2918 
2919 		/*
2920 		 * T_DISCON_REQ is passed to kRPC as an integer value
2921 		 * (this is not a TPI message).  It is used as a
2922 		 * convenient value to indicate a sanity check
2923 		 * failure -- the same kRPC routine is also called
2924 		 * for T_DISCON_INDs and T_ORDREL_INDs.
2925 		 */
2926 		clnt_dispatch_notifyall(WR(q), T_DISCON_REQ, 0);
2927 		break;
2928 
2929 	case RPC_SERVER:
2930 		mir->mir_svc_no_more_msgs = 1;
2931 		mir_svc_idle_stop(WR(q), mir);
2932 		mutex_exit(&mir->mir_mutex);
2933 		RPCLOG(16, "mir_disconnect: telling "
2934 		    "stream head listener to disconnect stream "
2935 		    "(0x%p)\n", (void *) q);
2936 		(void) mir_svc_policy_notify(q, 2);
2937 		break;
2938 
2939 	default:
2940 		mutex_exit(&mir->mir_mutex);
2941 		break;
2942 	}
2943 }
2944 
2945 /*
2946  * Sanity check the message length, and if it's too large, shutdown the
2947  * connection.  Returns 1 if the connection is shutdown; 0 otherwise.
2948  */
2949 static int
2950 mir_check_len(queue_t *q, mblk_t *head_mp)
2951 {
2952 	mir_t *mir = q->q_ptr;
2953 	uint_t maxsize = 0;
2954 	size_t msg_len = msgdsize(head_mp);
2955 
2956 	if (mir->mir_max_msg_sizep != NULL)
2957 		maxsize = *mir->mir_max_msg_sizep;
2958 
2959 	if (maxsize == 0 || msg_len <= maxsize)
2960 		return (0);
2961 
2962 	freemsg(head_mp);
2963 	mir->mir_head_mp = NULL;
2964 	mir->mir_tail_mp = NULL;
2965 	mir->mir_frag_header = 0;
2966 	mir->mir_frag_len = -(int32_t)sizeof (uint32_t);
2967 	if (mir->mir_type != RPC_SERVER || mir->mir_setup_complete) {
2968 		cmn_err(CE_NOTE,
2969 		    "kRPC: record fragment from %s of size(%lu) exceeds "
2970 		    "maximum (%u). Disconnecting",
2971 		    (mir->mir_type == RPC_CLIENT) ? "server" :
2972 		    (mir->mir_type == RPC_SERVER) ? "client" :
2973 		    "test tool", msg_len, maxsize);
2974 	}
2975 
2976 	mir_disconnect(q, mir);
2977 	return (1);
2978 }
2979