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.
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.
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]
23 * Copyright 2012 Marcel Telka <marcel@telka.sk>
24 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
25 * Copyright 2018 OmniOS Community Edition (OmniOSce) Association.
29 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
30 * Use is subject to license terms.
34 * Copyright 1993 OpenVision Technologies, Inc., All Rights Reserved.
37 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
38 /* All Rights Reserved */
41 * Portions of this source code were derived from Berkeley 4.3 BSD
42 * under license from the Regents of the University of California.
46 * Server-side remote procedure call interface.
48 * Master transport handle (SVCMASTERXPRT).
49 * The master transport handle structure is shared among service
50 * threads processing events on the transport. Some fields in the
51 * master structure are protected by locks
52 * - xp_req_lock protects the request queue:
53 * xp_req_head, xp_req_tail, xp_reqs, xp_size, xp_full, xp_enable
54 * - xp_thread_lock protects the thread (clone) counts
55 * xp_threads, xp_detached_threads, xp_wq
56 * Each master transport is registered to exactly one thread pool.
58 * Clone transport handle (SVCXPRT)
59 * The clone transport handle structure is a per-service-thread handle
60 * to the transport. The structure carries all the fields/buffers used
61 * for request processing. A service thread or, in other words, a clone
62 * structure, can be linked to an arbitrary master structure to process
63 * requests on this transport. The master handle keeps track of reference
64 * counts of threads (clones) linked to it. A service thread can switch
65 * to another transport by unlinking its clone handle from the current
66 * transport and linking to a new one. Switching is relatively inexpensive
67 * but it involves locking (master's xprt->xp_thread_lock).
70 * A pool represents a kernel RPC service (NFS, Lock Manager, etc.).
71 * Transports related to the service are registered to the service pool.
72 * Service threads can switch between different transports in the pool.
73 * Thus, each service has its own pool of service threads. The maximum
74 * number of threads in a pool is pool->p_maxthreads. This limit allows
75 * to restrict resource usage by the service. Some fields are protected
77 * - p_req_lock protects several counts and flags:
78 * p_reqs, p_size, p_walkers, p_asleep, p_drowsy, p_req_cv
79 * - p_thread_lock governs other thread counts:
80 * p_threads, p_detached_threads, p_reserved_threads, p_closing
82 * In addition, each pool contains a doubly-linked list of transports,
83 * an `xprt-ready' queue and a creator thread (see below). Threads in
84 * the pool share some other parameters such as stack size and
87 * Pools are initialized through the svc_pool_create() function called from
88 * the nfssys() system call. However, thread creation must be done by
89 * the userland agent. This is done by using SVCPOOL_WAIT and
90 * SVCPOOL_RUN arguments to nfssys(), which call svc_wait() and
91 * svc_do_run(), respectively. Once the pool has been initialized,
92 * the userland process must set up a 'creator' thread. This thread
93 * should park itself in the kernel by calling svc_wait(). If
94 * svc_wait() returns successfully, it should fork off a new worker
95 * thread, which then calls svc_do_run() in order to get work. When
96 * that thread is complete, svc_do_run() will return, and the user
97 * program should call thr_exit().
99 * When we try to register a new pool and there is an old pool with
100 * the same id in the doubly linked pool list (this happens when we kill
101 * and restart nfsd or lockd), then we unlink the old pool from the list
102 * and mark its state as `closing'. After that the transports can still
103 * process requests but new transports won't be registered. When all the
104 * transports and service threads associated with the pool are gone the
105 * creator thread (see below) will clean up the pool structure and exit.
107 * svc_queuereq() and svc_run().
108 * The kernel RPC server is interrupt driven. The svc_queuereq() interrupt
109 * routine is called to deliver an RPC request. The service threads
110 * loop in svc_run(). The interrupt function queues a request on the
111 * transport's queue and it makes sure that the request is serviced.
112 * It may either wake up one of sleeping threads, or ask for a new thread
113 * to be created, or, if the previous request is just being picked up, do
114 * nothing. In the last case the service thread that is picking up the
115 * previous request will wake up or create the next thread. After a service
116 * thread processes a request and sends a reply it returns to svc_run()
117 * and svc_run() calls svc_poll() to find new input.
120 * In order to avoid unnecessary locking, which causes performance
121 * problems, we always look for a pending request on the current transport.
122 * If there is none we take a hint from the pool's `xprt-ready' queue.
123 * If the queue had an overflow we switch to the `drain' mode checking
124 * each transport in the pool's transport list. Once we find a
125 * master transport handle with a pending request we latch the request
126 * lock on this transport and return to svc_run(). If the request
127 * belongs to a transport different than the one the service thread is
128 * linked to we need to unlink and link again.
130 * A service thread goes asleep when there are no pending
131 * requests on the transports registered on the pool's transports.
132 * All the pool's threads sleep on the same condition variable.
133 * If a thread has been sleeping for too long period of time
134 * (by default 5 seconds) it wakes up and exits. Also when a transport
135 * is closing sleeping threads wake up to unlink from this transport.
137 * The `xprt-ready' queue.
138 * If a service thread finds no request on a transport it is currently linked
139 * to it will find another transport with a pending request. To make
140 * this search more efficient each pool has an `xprt-ready' queue.
141 * The queue is a FIFO. When the interrupt routine queues a request it also
142 * inserts a pointer to the transport into the `xprt-ready' queue. A
143 * thread looking for a transport with a pending request can pop up a
144 * transport and check for a request. The request can be already gone
145 * since it could be taken by a thread linked to that transport. In such a
146 * case we try the next hint. The `xprt-ready' queue has fixed size (by
147 * default 256 nodes). If it overflows svc_poll() has to switch to the
148 * less efficient but safe `drain' mode and walk through the pool's
151 * Both the svc_poll() loop and the `xprt-ready' queue are optimized
152 * for the peak load case that is for the situation when the queue is not
153 * empty, there are all the time few pending requests, and a service
154 * thread which has just processed a request does not go asleep but picks
155 * up immediately the next request.
158 * Each pool has a thread creator associated with it. The creator thread
159 * sleeps on a condition variable and waits for a signal to create a
160 * service thread. The actual thread creation is done in userland by
161 * the method described in "Pools" above.
163 * Signaling threads should turn on the `creator signaled' flag, and
164 * can avoid sending signals when the flag is on. The flag is cleared
165 * when the thread is created.
167 * When the pool is in closing state (ie it has been already unregistered
168 * from the pool list) the last thread on the last transport in the pool
169 * should turn the p_creator_exit flag on. The creator thread will
170 * clean up the pool structure and exit.
172 * Thread reservation; Detaching service threads.
173 * A service thread can detach itself to block for an extended amount
174 * of time. However, to keep the service active we need to guarantee
175 * at least pool->p_redline non-detached threads that can process incoming
176 * requests. This, the maximum number of detached and reserved threads is
177 * p->p_maxthreads - p->p_redline. A service thread should first acquire
178 * a reservation, and if the reservation was granted it can detach itself.
179 * If a reservation was granted but the thread does not detach itself
180 * it should cancel the reservation before it returns to svc_run().
183 #include <sys/param.h>
184 #include <sys/types.h>
185 #include <rpc/types.h>
186 #include <sys/socket.h>
187 #include <sys/time.h>
188 #include <sys/tiuser.h>
189 #include <sys/t_kuser.h>
190 #include <netinet/in.h>
192 #include <rpc/auth.h>
193 #include <rpc/clnt.h>
194 #include <rpc/rpc_msg.h>
196 #include <sys/proc.h>
197 #include <sys/user.h>
198 #include <sys/stream.h>
199 #include <sys/strsubr.h>
200 #include <sys/strsun.h>
201 #include <sys/tihdr.h>
202 #include <sys/debug.h>
203 #include <sys/cmn_err.h>
204 #include <sys/file.h>
205 #include <sys/systm.h>
206 #include <sys/callb.h>
207 #include <sys/vtrace.h>
208 #include <sys/zone.h>
212 * Defines for svc_poll()
214 #define SVC_EXPRTGONE ((SVCMASTERXPRT *)1) /* Transport is closing */
215 #define SVC_ETIMEDOUT ((SVCMASTERXPRT *)2) /* Timeout */
216 #define SVC_EINTR ((SVCMASTERXPRT *)3) /* Interrupted by signal */
219 * Default stack size for service threads.
221 #define DEFAULT_SVC_RUN_STKSIZE (0) /* default kernel stack */
223 int svc_default_stksize
= DEFAULT_SVC_RUN_STKSIZE
;
226 * Default polling timeout for service threads.
227 * Multiplied by hz when used.
229 #define DEFAULT_SVC_POLL_TIMEOUT (5) /* seconds */
231 clock_t svc_default_timeout
= DEFAULT_SVC_POLL_TIMEOUT
;
234 * Size of the `xprt-ready' queue.
236 #define DEFAULT_SVC_QSIZE (256) /* qnodes */
238 size_t svc_default_qsize
= DEFAULT_SVC_QSIZE
;
241 * Default limit for the number of service threads.
243 #define DEFAULT_SVC_MAXTHREADS (INT16_MAX)
245 int svc_default_maxthreads
= DEFAULT_SVC_MAXTHREADS
;
248 * Maximum number of requests from the same transport (in `drain' mode).
250 #define DEFAULT_SVC_MAX_SAME_XPRT (8)
252 int svc_default_max_same_xprt
= DEFAULT_SVC_MAX_SAME_XPRT
;
256 * Default `Redline' of non-detached threads.
257 * Total number of detached and reserved threads in an RPC server
258 * thread pool is limited to pool->p_maxthreads - svc_redline.
260 #define DEFAULT_SVC_REDLINE (1)
262 int svc_default_redline
= DEFAULT_SVC_REDLINE
;
265 * A node for the `xprt-ready' queue.
268 struct __svcxprt_qnode
{
269 __SVCXPRT_QNODE
*q_next
;
270 SVCMASTERXPRT
*q_xprt
;
274 * Global SVC variables (private).
282 * Debug variable to check for rdma based
283 * transport startup and cleanup. Contorlled
284 * through /etc/system. Off by default.
289 * This allows disabling flow control in svc_queuereq().
291 volatile int svc_flowcontrol_disable
= 0;
294 * Authentication parameters list.
296 static caddr_t rqcred_head
;
297 static kmutex_t rqcred_lock
;
300 * If true, then keep quiet about version mismatch.
301 * This macro is for broadcast RPC only. We have no broadcast RPC in
302 * kernel now but one may define a flag in the transport structure
303 * and redefine this macro.
305 #define version_keepquiet(xprt) (FALSE)
308 * ZSD key used to retrieve zone-specific svc globals
310 static zone_key_t svc_zone_key
;
312 static void svc_callout_free(SVCMASTERXPRT
*);
313 static void svc_xprt_qinit(SVCPOOL
*, size_t);
314 static void svc_xprt_qdestroy(SVCPOOL
*);
315 static void svc_thread_creator(SVCPOOL
*);
316 static void svc_creator_signal(SVCPOOL
*);
317 static void svc_creator_signalexit(SVCPOOL
*);
318 static void svc_pool_unregister(struct svc_globals
*, SVCPOOL
*);
319 static int svc_run(SVCPOOL
*);
323 svc_zoneinit(zoneid_t zoneid
)
325 struct svc_globals
*svc
;
327 svc
= kmem_alloc(sizeof (*svc
), KM_SLEEP
);
328 mutex_init(&svc
->svc_plock
, NULL
, MUTEX_DEFAULT
, NULL
);
329 svc
->svc_pools
= NULL
;
335 svc_zoneshutdown(zoneid_t zoneid
, void *arg
)
337 struct svc_globals
*svc
= arg
;
340 mutex_enter(&svc
->svc_plock
);
341 while ((pool
= svc
->svc_pools
) != NULL
) {
342 svc_pool_unregister(svc
, pool
);
344 mutex_exit(&svc
->svc_plock
);
349 svc_zonefini(zoneid_t zoneid
, void *arg
)
351 struct svc_globals
*svc
= arg
;
353 ASSERT(svc
->svc_pools
== NULL
);
354 mutex_destroy(&svc
->svc_plock
);
355 kmem_free(svc
, sizeof (*svc
));
359 * Global SVC init routine.
360 * Initialize global generic and transport type specific structures
361 * used by the kernel RPC server side. This routine is called only
362 * once when the module is being loaded.
367 zone_key_create(&svc_zone_key
, svc_zoneinit
, svc_zoneshutdown
,
374 * Destroy the SVCPOOL structure.
377 svc_pool_cleanup(SVCPOOL
*pool
)
379 ASSERT(pool
->p_threads
+ pool
->p_detached_threads
== 0);
380 ASSERT(pool
->p_lcount
== 0);
381 ASSERT(pool
->p_closing
);
384 * Call the user supplied shutdown function. This is done
385 * here so the user of the pool will be able to cleanup
386 * service related resources.
388 if (pool
->p_shutdown
!= NULL
)
389 (pool
->p_shutdown
)();
391 /* Destroy `xprt-ready' queue */
392 svc_xprt_qdestroy(pool
);
394 /* Destroy transport list */
395 rw_destroy(&pool
->p_lrwlock
);
397 /* Destroy locks and condition variables */
398 mutex_destroy(&pool
->p_thread_lock
);
399 mutex_destroy(&pool
->p_req_lock
);
400 cv_destroy(&pool
->p_req_cv
);
402 /* Destroy creator's locks and condition variables */
403 mutex_destroy(&pool
->p_creator_lock
);
404 cv_destroy(&pool
->p_creator_cv
);
405 mutex_destroy(&pool
->p_user_lock
);
406 cv_destroy(&pool
->p_user_cv
);
408 /* Free pool structure */
409 kmem_free(pool
, sizeof (SVCPOOL
));
413 * If all the transports and service threads are already gone
414 * signal the creator thread to clean up and exit.
417 svc_pool_tryexit(SVCPOOL
*pool
)
419 ASSERT(MUTEX_HELD(&pool
->p_thread_lock
));
420 ASSERT(pool
->p_closing
);
422 if (pool
->p_threads
+ pool
->p_detached_threads
== 0) {
423 rw_enter(&pool
->p_lrwlock
, RW_READER
);
424 if (pool
->p_lcount
== 0) {
426 * Release the locks before sending a signal.
428 rw_exit(&pool
->p_lrwlock
);
429 mutex_exit(&pool
->p_thread_lock
);
432 * Notify the creator thread to clean up and exit
434 * NOTICE: No references to the pool beyond this point!
435 * The pool is being destroyed.
437 ASSERT(!MUTEX_HELD(&pool
->p_thread_lock
));
438 svc_creator_signalexit(pool
);
442 rw_exit(&pool
->p_lrwlock
);
445 ASSERT(MUTEX_HELD(&pool
->p_thread_lock
));
450 * Find a pool with a given id.
453 svc_pool_find(struct svc_globals
*svc
, int id
)
457 ASSERT(MUTEX_HELD(&svc
->svc_plock
));
460 * Search the list for a pool with a matching id
461 * and register the transport handle with that pool.
463 for (pool
= svc
->svc_pools
; pool
; pool
= pool
->p_next
)
464 if (pool
->p_id
== id
)
471 * PSARC 2003/523 Contract Private Interface
473 * Changes must be reviewed by Solaris File Sharing
474 * Changes must be communicated to contract-2003-523@sun.com
481 struct svc_globals
*svc
;
483 svc
= zone_getspecific(svc_zone_key
, curproc
->p_zone
);
484 mutex_enter(&svc
->svc_plock
);
486 pool
= svc_pool_find(svc
, id
);
488 mutex_exit(&svc
->svc_plock
);
494 * Increment counter of pool threads now
495 * that a thread has been created.
497 mutex_enter(&pool
->p_thread_lock
);
499 mutex_exit(&pool
->p_thread_lock
);
501 /* Give work to the new thread. */
508 * Unregister a pool from the pool list.
509 * Set the closing state. If all the transports and service threads
510 * are already gone signal the creator thread to clean up and exit.
513 svc_pool_unregister(struct svc_globals
*svc
, SVCPOOL
*pool
)
515 SVCPOOL
*next
= pool
->p_next
;
516 SVCPOOL
*prev
= pool
->p_prev
;
518 ASSERT(MUTEX_HELD(&svc
->svc_plock
));
520 /* Remove from the list */
521 if (pool
== svc
->svc_pools
)
522 svc
->svc_pools
= next
;
527 pool
->p_next
= pool
->p_prev
= NULL
;
530 * Offline the pool. Mark the pool as closing.
531 * If there are no transports in this pool notify
532 * the creator thread to clean it up and exit.
534 mutex_enter(&pool
->p_thread_lock
);
535 if (pool
->p_offline
!= NULL
)
537 pool
->p_closing
= TRUE
;
538 if (svc_pool_tryexit(pool
))
540 mutex_exit(&pool
->p_thread_lock
);
544 * Register a pool with a given id in the global doubly linked pool list.
545 * - if there is a pool with the same id in the list then unregister it
546 * - insert the new pool into the list.
549 svc_pool_register(struct svc_globals
*svc
, SVCPOOL
*pool
, int id
)
554 * If there is a pool with the same id then remove it from
555 * the list and mark the pool as closing.
557 mutex_enter(&svc
->svc_plock
);
559 if (old_pool
= svc_pool_find(svc
, id
))
560 svc_pool_unregister(svc
, old_pool
);
562 /* Insert into the doubly linked list */
564 pool
->p_next
= svc
->svc_pools
;
567 svc
->svc_pools
->p_prev
= pool
;
568 svc
->svc_pools
= pool
;
570 mutex_exit(&svc
->svc_plock
);
574 * Initialize a newly created pool structure
577 svc_pool_init(SVCPOOL
*pool
, uint_t maxthreads
, uint_t redline
,
578 uint_t qsize
, uint_t timeout
, uint_t stksize
, uint_t max_same_xprt
)
580 klwp_t
*lwp
= ttolwp(curthread
);
585 maxthreads
= svc_default_maxthreads
;
587 redline
= svc_default_redline
;
589 qsize
= svc_default_qsize
;
591 timeout
= svc_default_timeout
;
593 stksize
= svc_default_stksize
;
594 if (max_same_xprt
== 0)
595 max_same_xprt
= svc_default_max_same_xprt
;
597 if (maxthreads
< redline
)
600 /* Allocate and initialize the `xprt-ready' queue */
601 svc_xprt_qinit(pool
, qsize
);
603 /* Initialize doubly-linked xprt list */
604 rw_init(&pool
->p_lrwlock
, NULL
, RW_DEFAULT
, NULL
);
607 * Setting lwp_childstksz on the current lwp so that
608 * descendants of this lwp get the modified stacksize, if
609 * it is defined. It is important that either this lwp or
610 * one of its descendants do the actual servicepool thread
611 * creation to maintain the stacksize inheritance.
614 lwp
->lwp_childstksz
= stksize
;
616 /* Initialize thread limits, locks and condition variables */
617 pool
->p_maxthreads
= maxthreads
;
618 pool
->p_redline
= redline
;
619 pool
->p_timeout
= timeout
* hz
;
620 pool
->p_stksize
= stksize
;
621 pool
->p_max_same_xprt
= max_same_xprt
;
622 mutex_init(&pool
->p_thread_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
623 mutex_init(&pool
->p_req_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
624 cv_init(&pool
->p_req_cv
, NULL
, CV_DEFAULT
, NULL
);
626 /* Initialize userland creator */
627 pool
->p_user_exit
= FALSE
;
628 pool
->p_signal_create_thread
= FALSE
;
629 pool
->p_user_waiting
= FALSE
;
630 mutex_init(&pool
->p_user_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
631 cv_init(&pool
->p_user_cv
, NULL
, CV_DEFAULT
, NULL
);
633 /* Initialize the creator and start the creator thread */
634 pool
->p_creator_exit
= FALSE
;
635 mutex_init(&pool
->p_creator_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
636 cv_init(&pool
->p_creator_cv
, NULL
, CV_DEFAULT
, NULL
);
638 (void) zthread_create(NULL
, pool
->p_stksize
, svc_thread_creator
,
639 pool
, 0, minclsyspri
);
645 * PSARC 2003/523 Contract Private Interface
647 * Changes must be reviewed by Solaris File Sharing
648 * Changes must be communicated to contract-2003-523@sun.com
650 * Create an kernel RPC server-side thread/transport pool.
652 * This is public interface for creation of a server RPC thread pool
653 * for a given service provider. Transports registered with the pool's id
654 * will be served by a pool's threads. This function is called from the
655 * nfssys() system call.
658 svc_pool_create(struct svcpool_args
*args
)
662 struct svc_globals
*svc
;
665 * Caller should check credentials in a way appropriate
666 * in the context of the call.
669 svc
= zone_getspecific(svc_zone_key
, curproc
->p_zone
);
670 /* Allocate a new pool */
671 pool
= kmem_zalloc(sizeof (SVCPOOL
), KM_SLEEP
);
674 * Initialize the pool structure and create a creator thread.
676 error
= svc_pool_init(pool
, args
->maxthreads
, args
->redline
,
677 args
->qsize
, args
->timeout
, args
->stksize
, args
->max_same_xprt
);
680 kmem_free(pool
, sizeof (SVCPOOL
));
684 /* Register the pool with the global pool list */
685 svc_pool_register(svc
, pool
, args
->id
);
691 svc_pool_control(int id
, int cmd
, void *arg
)
694 struct svc_globals
*svc
;
696 svc
= zone_getspecific(svc_zone_key
, curproc
->p_zone
);
699 case SVCPSET_SHUTDOWN_PROC
:
701 * Search the list for a pool with a matching id
702 * and register the transport handle with that pool.
704 mutex_enter(&svc
->svc_plock
);
706 if ((pool
= svc_pool_find(svc
, id
)) == NULL
) {
707 mutex_exit(&svc
->svc_plock
);
711 * Grab the transport list lock before releasing the
714 rw_enter(&pool
->p_lrwlock
, RW_WRITER
);
715 mutex_exit(&svc
->svc_plock
);
717 pool
->p_shutdown
= *((void (*)())arg
);
719 rw_exit(&pool
->p_lrwlock
);
722 case SVCPSET_UNREGISTER_PROC
:
724 * Search the list for a pool with a matching id
725 * and register the unregister callback handle with that pool.
727 mutex_enter(&svc
->svc_plock
);
729 if ((pool
= svc_pool_find(svc
, id
)) == NULL
) {
730 mutex_exit(&svc
->svc_plock
);
734 * Grab the transport list lock before releasing the
737 rw_enter(&pool
->p_lrwlock
, RW_WRITER
);
738 mutex_exit(&svc
->svc_plock
);
740 pool
->p_offline
= *((void (*)())arg
);
742 rw_exit(&pool
->p_lrwlock
);
751 * Pool's transport list manipulation routines.
752 * - svc_xprt_register()
753 * - svc_xprt_unregister()
755 * svc_xprt_register() is called from svc_tli_kcreate() to
756 * insert a new master transport handle into the doubly linked
757 * list of server transport handles (one list per pool).
759 * The list is used by svc_poll(), when it operates in `drain'
760 * mode, to search for a next transport with a pending request.
764 svc_xprt_register(SVCMASTERXPRT
*xprt
, int id
)
766 SVCMASTERXPRT
*prev
, *next
;
768 struct svc_globals
*svc
;
770 svc
= zone_getspecific(svc_zone_key
, curproc
->p_zone
);
772 * Search the list for a pool with a matching id
773 * and register the transport handle with that pool.
775 mutex_enter(&svc
->svc_plock
);
777 if ((pool
= svc_pool_find(svc
, id
)) == NULL
) {
778 mutex_exit(&svc
->svc_plock
);
782 /* Grab the transport list lock before releasing the pool list lock */
783 rw_enter(&pool
->p_lrwlock
, RW_WRITER
);
784 mutex_exit(&svc
->svc_plock
);
786 /* Don't register new transports when the pool is in closing state */
787 if (pool
->p_closing
) {
788 rw_exit(&pool
->p_lrwlock
);
793 * Initialize xp_pool to point to the pool.
794 * We don't want to go through the pool list every time.
796 xprt
->xp_pool
= pool
;
799 * Insert a transport handle into the list.
800 * The list head points to the most recently inserted transport.
802 if (pool
->p_lhead
== NULL
)
803 pool
->p_lhead
= xprt
->xp_prev
= xprt
->xp_next
= xprt
;
805 next
= pool
->p_lhead
;
806 prev
= pool
->p_lhead
->xp_prev
;
808 xprt
->xp_next
= next
;
809 xprt
->xp_prev
= prev
;
811 pool
->p_lhead
= prev
->xp_next
= next
->xp_prev
= xprt
;
814 /* Increment the transports count */
817 rw_exit(&pool
->p_lrwlock
);
822 * Called from svc_xprt_cleanup() to remove a master transport handle
823 * from the pool's list of server transports (when a transport is
827 svc_xprt_unregister(SVCMASTERXPRT
*xprt
)
829 SVCPOOL
*pool
= xprt
->xp_pool
;
832 * Unlink xprt from the list.
833 * If the list head points to this xprt then move it
834 * to the next xprt or reset to NULL if this is the last
837 rw_enter(&pool
->p_lrwlock
, RW_WRITER
);
839 if (xprt
== xprt
->xp_next
)
840 pool
->p_lhead
= NULL
;
842 SVCMASTERXPRT
*next
= xprt
->xp_next
;
843 SVCMASTERXPRT
*prev
= xprt
->xp_prev
;
845 next
->xp_prev
= prev
;
846 prev
->xp_next
= next
;
848 if (pool
->p_lhead
== xprt
)
849 pool
->p_lhead
= next
;
852 xprt
->xp_next
= xprt
->xp_prev
= NULL
;
854 /* Decrement list count */
857 rw_exit(&pool
->p_lrwlock
);
861 svc_xprt_qdestroy(SVCPOOL
*pool
)
863 mutex_destroy(&pool
->p_qend_lock
);
864 kmem_free(pool
->p_qbody
, pool
->p_qsize
* sizeof (__SVCXPRT_QNODE
));
868 * Initialize an `xprt-ready' queue for a given pool.
871 svc_xprt_qinit(SVCPOOL
*pool
, size_t qsize
)
875 pool
->p_qsize
= qsize
;
876 pool
->p_qbody
= kmem_zalloc(pool
->p_qsize
* sizeof (__SVCXPRT_QNODE
),
879 for (i
= 0; i
< pool
->p_qsize
- 1; i
++)
880 pool
->p_qbody
[i
].q_next
= &(pool
->p_qbody
[i
+1]);
882 pool
->p_qbody
[pool
->p_qsize
-1].q_next
= &(pool
->p_qbody
[0]);
883 pool
->p_qtop
= &(pool
->p_qbody
[0]);
884 pool
->p_qend
= &(pool
->p_qbody
[0]);
886 mutex_init(&pool
->p_qend_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
890 * Called from the svc_queuereq() interrupt routine to queue
891 * a hint for svc_poll() which transport has a pending request.
892 * - insert a pointer to xprt into the xprt-ready queue (FIFO)
893 * - if the xprt-ready queue is full turn the overflow flag on.
895 * NOTICE: pool->p_qtop is protected by the pool's request lock
896 * and the caller (svc_queuereq()) must hold the lock.
899 svc_xprt_qput(SVCPOOL
*pool
, SVCMASTERXPRT
*xprt
)
901 ASSERT(MUTEX_HELD(&pool
->p_req_lock
));
903 /* If the overflow flag is on there is nothing we can do */
904 if (pool
->p_qoverflow
)
907 /* If the queue is full turn the overflow flag on and exit */
908 if (pool
->p_qtop
->q_next
== pool
->p_qend
) {
909 mutex_enter(&pool
->p_qend_lock
);
910 if (pool
->p_qtop
->q_next
== pool
->p_qend
) {
911 pool
->p_qoverflow
= TRUE
;
912 mutex_exit(&pool
->p_qend_lock
);
915 mutex_exit(&pool
->p_qend_lock
);
918 /* Insert a hint and move pool->p_qtop */
919 pool
->p_qtop
->q_xprt
= xprt
;
920 pool
->p_qtop
= pool
->p_qtop
->q_next
;
924 * Called from svc_poll() to get a hint which transport has a
925 * pending request. Returns a pointer to a transport or NULL if the
926 * `xprt-ready' queue is empty.
928 * Since we do not acquire the pool's request lock while checking if
929 * the queue is empty we may miss a request that is just being delivered.
930 * However this is ok since svc_poll() will retry again until the
931 * count indicates that there are pending requests for this pool.
933 static SVCMASTERXPRT
*
934 svc_xprt_qget(SVCPOOL
*pool
)
938 mutex_enter(&pool
->p_qend_lock
);
941 * If the queue is empty return NULL.
942 * Since we do not acquire the pool's request lock which
943 * protects pool->p_qtop this is not exact check. However,
944 * this is safe - if we miss a request here svc_poll()
947 if (pool
->p_qend
== pool
->p_qtop
) {
948 mutex_exit(&pool
->p_qend_lock
);
952 /* Get a hint and move pool->p_qend */
953 xprt
= pool
->p_qend
->q_xprt
;
954 pool
->p_qend
= pool
->p_qend
->q_next
;
956 /* Skip fields deleted by svc_xprt_qdelete() */
957 } while (xprt
== NULL
);
958 mutex_exit(&pool
->p_qend_lock
);
964 * Delete all the references to a transport handle that
965 * is being destroyed from the xprt-ready queue.
966 * Deleted pointers are replaced with NULLs.
969 svc_xprt_qdelete(SVCPOOL
*pool
, SVCMASTERXPRT
*xprt
)
973 mutex_enter(&pool
->p_req_lock
);
974 for (q
= pool
->p_qend
; q
!= pool
->p_qtop
; q
= q
->q_next
) {
975 if (q
->q_xprt
== xprt
)
978 mutex_exit(&pool
->p_req_lock
);
982 * Destructor for a master server transport handle.
983 * - if there are no more non-detached threads linked to this transport
984 * then, if requested, call xp_closeproc (we don't wait for detached
985 * threads linked to this transport to complete).
986 * - if there are no more threads linked to this
988 * a) remove references to this transport from the xprt-ready queue
989 * b) remove a reference to this transport from the pool's transport list
990 * c) call a transport specific `destroy' function
991 * d) cancel remaining thread reservations.
993 * NOTICE: Caller must hold the transport's thread lock.
996 svc_xprt_cleanup(SVCMASTERXPRT
*xprt
, bool_t detached
)
998 ASSERT(MUTEX_HELD(&xprt
->xp_thread_lock
));
999 ASSERT(xprt
->xp_wq
== NULL
);
1002 * If called from the last non-detached thread
1003 * it should call the closeproc on this transport.
1005 if (!detached
&& xprt
->xp_threads
== 0 && xprt
->xp_closeproc
) {
1006 (*(xprt
->xp_closeproc
)) (xprt
);
1009 if (xprt
->xp_threads
+ xprt
->xp_detached_threads
> 0)
1010 mutex_exit(&xprt
->xp_thread_lock
);
1012 /* Remove references to xprt from the `xprt-ready' queue */
1013 svc_xprt_qdelete(xprt
->xp_pool
, xprt
);
1015 /* Unregister xprt from the pool's transport list */
1016 svc_xprt_unregister(xprt
);
1017 svc_callout_free(xprt
);
1023 * Find a dispatch routine for a given prog/vers pair.
1024 * This function is called from svc_getreq() to search the callout
1025 * table for an entry with a matching RPC program number `prog'
1026 * and a version range that covers `vers'.
1027 * - if it finds a matching entry it returns pointer to the dispatch routine
1028 * - otherwise it returns NULL and, if `minp' or `maxp' are not NULL,
1029 * fills them with, respectively, lowest version and highest version
1030 * supported for the program `prog'
1032 static SVC_DISPATCH
*
1033 svc_callout_find(SVCXPRT
*xprt
, rpcprog_t prog
, rpcvers_t vers
,
1034 rpcvers_t
*vers_min
, rpcvers_t
*vers_max
)
1036 SVC_CALLOUT_TABLE
*sct
= xprt
->xp_sct
;
1039 *vers_min
= ~(rpcvers_t
)0;
1042 for (i
= 0; i
< sct
->sct_size
; i
++) {
1043 SVC_CALLOUT
*sc
= &sct
->sct_sc
[i
];
1045 if (prog
== sc
->sc_prog
) {
1046 if (vers
>= sc
->sc_versmin
&& vers
<= sc
->sc_versmax
)
1047 return (sc
->sc_dispatch
);
1049 if (*vers_max
< sc
->sc_versmax
)
1050 *vers_max
= sc
->sc_versmax
;
1051 if (*vers_min
> sc
->sc_versmin
)
1052 *vers_min
= sc
->sc_versmin
;
1060 * Optionally free callout table allocated for this transport by
1061 * the service provider.
1064 svc_callout_free(SVCMASTERXPRT
*xprt
)
1066 SVC_CALLOUT_TABLE
*sct
= xprt
->xp_sct
;
1068 if (sct
->sct_free
) {
1069 kmem_free(sct
->sct_sc
, sct
->sct_size
* sizeof (SVC_CALLOUT
));
1070 kmem_free(sct
, sizeof (SVC_CALLOUT_TABLE
));
1075 * Send a reply to an RPC request
1077 * PSARC 2003/523 Contract Private Interface
1079 * Changes must be reviewed by Solaris File Sharing
1080 * Changes must be communicated to contract-2003-523@sun.com
1083 svc_sendreply(const SVCXPRT
*clone_xprt
, const xdrproc_t xdr_results
,
1084 const caddr_t xdr_location
)
1086 struct rpc_msg rply
;
1088 rply
.rm_direction
= REPLY
;
1089 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
1090 rply
.acpted_rply
.ar_verf
= clone_xprt
->xp_verf
;
1091 rply
.acpted_rply
.ar_stat
= SUCCESS
;
1092 rply
.acpted_rply
.ar_results
.where
= xdr_location
;
1093 rply
.acpted_rply
.ar_results
.proc
= xdr_results
;
1095 return (SVC_REPLY((SVCXPRT
*)clone_xprt
, &rply
));
1099 * No procedure error reply
1101 * PSARC 2003/523 Contract Private Interface
1103 * Changes must be reviewed by Solaris File Sharing
1104 * Changes must be communicated to contract-2003-523@sun.com
1107 svcerr_noproc(const SVCXPRT
*clone_xprt
)
1109 struct rpc_msg rply
;
1111 rply
.rm_direction
= REPLY
;
1112 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
1113 rply
.acpted_rply
.ar_verf
= clone_xprt
->xp_verf
;
1114 rply
.acpted_rply
.ar_stat
= PROC_UNAVAIL
;
1115 SVC_FREERES((SVCXPRT
*)clone_xprt
);
1116 SVC_REPLY((SVCXPRT
*)clone_xprt
, &rply
);
1120 * Can't decode arguments error reply
1122 * PSARC 2003/523 Contract Private Interface
1124 * Changes must be reviewed by Solaris File Sharing
1125 * Changes must be communicated to contract-2003-523@sun.com
1128 svcerr_decode(const SVCXPRT
*clone_xprt
)
1130 struct rpc_msg rply
;
1132 rply
.rm_direction
= REPLY
;
1133 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
1134 rply
.acpted_rply
.ar_verf
= clone_xprt
->xp_verf
;
1135 rply
.acpted_rply
.ar_stat
= GARBAGE_ARGS
;
1136 SVC_FREERES((SVCXPRT
*)clone_xprt
);
1137 SVC_REPLY((SVCXPRT
*)clone_xprt
, &rply
);
1144 svcerr_systemerr(const SVCXPRT
*clone_xprt
)
1146 struct rpc_msg rply
;
1148 rply
.rm_direction
= REPLY
;
1149 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
1150 rply
.acpted_rply
.ar_verf
= clone_xprt
->xp_verf
;
1151 rply
.acpted_rply
.ar_stat
= SYSTEM_ERR
;
1152 SVC_FREERES((SVCXPRT
*)clone_xprt
);
1153 SVC_REPLY((SVCXPRT
*)clone_xprt
, &rply
);
1157 * Authentication error reply
1160 svcerr_auth(const SVCXPRT
*clone_xprt
, const enum auth_stat why
)
1162 struct rpc_msg rply
;
1164 rply
.rm_direction
= REPLY
;
1165 rply
.rm_reply
.rp_stat
= MSG_DENIED
;
1166 rply
.rjcted_rply
.rj_stat
= AUTH_ERROR
;
1167 rply
.rjcted_rply
.rj_why
= why
;
1168 SVC_FREERES((SVCXPRT
*)clone_xprt
);
1169 SVC_REPLY((SVCXPRT
*)clone_xprt
, &rply
);
1173 * Authentication too weak error reply
1176 svcerr_weakauth(const SVCXPRT
*clone_xprt
)
1178 svcerr_auth((SVCXPRT
*)clone_xprt
, AUTH_TOOWEAK
);
1182 * Authentication error; bad credentials
1185 svcerr_badcred(const SVCXPRT
*clone_xprt
)
1187 struct rpc_msg rply
;
1189 rply
.rm_direction
= REPLY
;
1190 rply
.rm_reply
.rp_stat
= MSG_DENIED
;
1191 rply
.rjcted_rply
.rj_stat
= AUTH_ERROR
;
1192 rply
.rjcted_rply
.rj_why
= AUTH_BADCRED
;
1193 SVC_FREERES((SVCXPRT
*)clone_xprt
);
1194 SVC_REPLY((SVCXPRT
*)clone_xprt
, &rply
);
1198 * Program unavailable error reply
1200 * PSARC 2003/523 Contract Private Interface
1202 * Changes must be reviewed by Solaris File Sharing
1203 * Changes must be communicated to contract-2003-523@sun.com
1206 svcerr_noprog(const SVCXPRT
*clone_xprt
)
1208 struct rpc_msg rply
;
1210 rply
.rm_direction
= REPLY
;
1211 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
1212 rply
.acpted_rply
.ar_verf
= clone_xprt
->xp_verf
;
1213 rply
.acpted_rply
.ar_stat
= PROG_UNAVAIL
;
1214 SVC_FREERES((SVCXPRT
*)clone_xprt
);
1215 SVC_REPLY((SVCXPRT
*)clone_xprt
, &rply
);
1219 * Program version mismatch error reply
1221 * PSARC 2003/523 Contract Private Interface
1223 * Changes must be reviewed by Solaris File Sharing
1224 * Changes must be communicated to contract-2003-523@sun.com
1227 svcerr_progvers(const SVCXPRT
*clone_xprt
,
1228 const rpcvers_t low_vers
, const rpcvers_t high_vers
)
1230 struct rpc_msg rply
;
1232 rply
.rm_direction
= REPLY
;
1233 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
1234 rply
.acpted_rply
.ar_verf
= clone_xprt
->xp_verf
;
1235 rply
.acpted_rply
.ar_stat
= PROG_MISMATCH
;
1236 rply
.acpted_rply
.ar_vers
.low
= low_vers
;
1237 rply
.acpted_rply
.ar_vers
.high
= high_vers
;
1238 SVC_FREERES((SVCXPRT
*)clone_xprt
);
1239 SVC_REPLY((SVCXPRT
*)clone_xprt
, &rply
);
1243 * Get server side input from some transport.
1245 * Statement of authentication parameters management:
1246 * This function owns and manages all authentication parameters, specifically
1247 * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and
1248 * the "cooked" credentials (rqst->rq_clntcred).
1249 * However, this function does not know the structure of the cooked
1250 * credentials, so it make the following assumptions:
1251 * a) the structure is contiguous (no pointers), and
1252 * b) the cred structure size does not exceed RQCRED_SIZE bytes.
1253 * In all events, all three parameters are freed upon exit from this routine.
1254 * The storage is trivially managed on the call stack in user land, but
1255 * is malloced in kernel land.
1257 * Note: the xprt's xp_svc_lock is not held while the service's dispatch
1258 * routine is running. If we decide to implement svc_unregister(), we'll
1259 * need to decide whether it's okay for a thread to unregister a service
1260 * while a request is being processed. If we decide that this is a
1261 * problem, we can probably use some sort of reference counting scheme to
1262 * keep the callout entry from going away until the request has completed.
1266 SVCXPRT
*clone_xprt
, /* clone transport handle */
1271 char *cred_area
; /* too big to allocate on call stack */
1273 TRACE_0(TR_FAC_KRPC
, TR_SVC_GETREQ_START
,
1274 "svc_getreq_start:");
1276 ASSERT(clone_xprt
->xp_master
!= NULL
);
1278 * Firstly, allocate the authentication parameters' storage
1280 mutex_enter(&rqcred_lock
);
1282 cred_area
= rqcred_head
;
1284 /* LINTED pointer alignment */
1285 rqcred_head
= *(caddr_t
*)rqcred_head
;
1286 mutex_exit(&rqcred_lock
);
1288 mutex_exit(&rqcred_lock
);
1289 cred_area
= kmem_alloc(2 * MAX_AUTH_BYTES
+ RQCRED_SIZE
,
1292 msg
.rm_call
.cb_cred
.oa_base
= cred_area
;
1293 msg
.rm_call
.cb_verf
.oa_base
= &(cred_area
[MAX_AUTH_BYTES
]);
1294 r
.rq_clntcred
= &(cred_area
[2 * MAX_AUTH_BYTES
]);
1297 * Now receive a message from the transport.
1299 if (SVC_RECV(clone_xprt
, mp
, &msg
)) {
1300 void (*dispatchroutine
) (struct svc_req
*, SVCXPRT
*);
1307 * Find the registered program and call its
1310 r
.rq_xprt
= clone_xprt
;
1311 r
.rq_prog
= msg
.rm_call
.cb_prog
;
1312 r
.rq_vers
= msg
.rm_call
.cb_vers
;
1313 r
.rq_proc
= msg
.rm_call
.cb_proc
;
1314 r
.rq_cred
= msg
.rm_call
.cb_cred
;
1317 * First authenticate the message.
1319 TRACE_0(TR_FAC_KRPC
, TR_SVC_GETREQ_AUTH_START
,
1320 "svc_getreq_auth_start:");
1321 if ((why
= sec_svc_msg(&r
, &msg
, &no_dispatch
)) != AUTH_OK
) {
1322 TRACE_1(TR_FAC_KRPC
, TR_SVC_GETREQ_AUTH_END
,
1323 "svc_getreq_auth_end:(%S)", "failed");
1324 svcerr_auth(clone_xprt
, why
);
1326 * Free the arguments.
1328 (void) SVC_FREEARGS(clone_xprt
, NULL
, NULL
);
1329 } else if (no_dispatch
) {
1331 * XXX - when bug id 4053736 is done, remove
1332 * the SVC_FREEARGS() call.
1334 (void) SVC_FREEARGS(clone_xprt
, NULL
, NULL
);
1336 TRACE_1(TR_FAC_KRPC
, TR_SVC_GETREQ_AUTH_END
,
1337 "svc_getreq_auth_end:(%S)", "good");
1339 dispatchroutine
= svc_callout_find(clone_xprt
,
1340 r
.rq_prog
, r
.rq_vers
, &vers_min
, &vers_max
);
1342 if (dispatchroutine
) {
1343 (*dispatchroutine
) (&r
, clone_xprt
);
1346 * If we got here, the program or version
1349 if (vers_max
== 0 ||
1350 version_keepquiet(clone_xprt
))
1351 svcerr_noprog(clone_xprt
);
1353 svcerr_progvers(clone_xprt
, vers_min
,
1357 * Free the arguments. For successful calls
1358 * this is done by the dispatch routine.
1360 (void) SVC_FREEARGS(clone_xprt
, NULL
, NULL
);
1361 /* Fall through to ... */
1364 * Call cleanup procedure for RPCSEC_GSS.
1365 * This is a hack since there is currently no
1366 * op, such as SVC_CLEANAUTH. rpc_gss_cleanup
1367 * should only be called for a non null proc.
1368 * Null procs in RPC GSS are overloaded to
1369 * provide context setup and control. The main
1370 * purpose of rpc_gss_cleanup is to decrement the
1371 * reference count associated with the cached
1372 * GSS security context. We should never get here
1373 * for an RPCSEC_GSS null proc since *no_dispatch
1374 * would have been set to true from sec_svc_msg above.
1376 if (r
.rq_cred
.oa_flavor
== RPCSEC_GSS
)
1377 rpc_gss_cleanup(clone_xprt
);
1382 * Free authentication parameters' storage
1384 mutex_enter(&rqcred_lock
);
1385 /* LINTED pointer alignment */
1386 *(caddr_t
*)cred_area
= rqcred_head
;
1387 rqcred_head
= cred_area
;
1388 mutex_exit(&rqcred_lock
);
1392 * Allocate new clone transport handle.
1395 svc_clone_init(void)
1397 SVCXPRT
*clone_xprt
;
1399 clone_xprt
= kmem_zalloc(sizeof (SVCXPRT
), KM_SLEEP
);
1400 clone_xprt
->xp_cred
= crget();
1401 return (clone_xprt
);
1405 * Free memory allocated by svc_clone_init.
1408 svc_clone_free(SVCXPRT
*clone_xprt
)
1410 /* Fre credentials from crget() */
1411 if (clone_xprt
->xp_cred
)
1412 crfree(clone_xprt
->xp_cred
);
1413 kmem_free(clone_xprt
, sizeof (SVCXPRT
));
1417 * Link a per-thread clone transport handle to a master
1418 * - increment a thread reference count on the master
1419 * - copy some of the master's fields to the clone
1420 * - call a transport specific clone routine.
1423 svc_clone_link(SVCMASTERXPRT
*xprt
, SVCXPRT
*clone_xprt
, SVCXPRT
*clone_xprt2
)
1425 cred_t
*cred
= clone_xprt
->xp_cred
;
1430 * Bump up master's thread count.
1431 * Linking a per-thread clone transport handle to a master
1432 * associates a service thread with the master.
1434 mutex_enter(&xprt
->xp_thread_lock
);
1436 mutex_exit(&xprt
->xp_thread_lock
);
1438 /* Clear everything */
1439 bzero(clone_xprt
, sizeof (SVCXPRT
));
1441 /* Set pointer to the master transport stucture */
1442 clone_xprt
->xp_master
= xprt
;
1444 /* Structure copy of all the common fields */
1445 clone_xprt
->xp_xpc
= xprt
->xp_xpc
;
1447 /* Restore per-thread fields (xp_cred) */
1448 clone_xprt
->xp_cred
= cred
;
1451 SVC_CLONE_XPRT(clone_xprt2
, clone_xprt
);
1455 * Unlink a non-detached clone transport handle from a master
1456 * - decrement a thread reference count on the master
1457 * - if the transport is closing (xp_wq is NULL) call svc_xprt_cleanup();
1458 * if this is the last non-detached/absolute thread on this transport
1459 * then it will close/destroy the transport
1460 * - call transport specific function to destroy the clone handle
1461 * - clear xp_master to avoid recursion.
1464 svc_clone_unlink(SVCXPRT
*clone_xprt
)
1466 SVCMASTERXPRT
*xprt
= clone_xprt
->xp_master
;
1468 /* This cannot be a detached thread */
1469 ASSERT(!clone_xprt
->xp_detached
);
1470 ASSERT(xprt
->xp_threads
> 0);
1472 /* Decrement a reference count on the transport */
1473 mutex_enter(&xprt
->xp_thread_lock
);
1476 /* svc_xprt_cleanup() unlocks xp_thread_lock or destroys xprt */
1478 mutex_exit(&xprt
->xp_thread_lock
);
1480 svc_xprt_cleanup(xprt
, FALSE
);
1482 /* Call a transport specific clone `destroy' function */
1483 SVC_CLONE_DESTROY(clone_xprt
);
1485 /* Clear xp_master */
1486 clone_xprt
->xp_master
= NULL
;
1490 * Unlink a detached clone transport handle from a master
1491 * - decrement the thread count on the master
1492 * - if the transport is closing (xp_wq is NULL) call svc_xprt_cleanup();
1493 * if this is the last thread on this transport then it will destroy
1495 * - call a transport specific function to destroy the clone handle
1496 * - clear xp_master to avoid recursion.
1499 svc_clone_unlinkdetached(SVCXPRT
*clone_xprt
)
1501 SVCMASTERXPRT
*xprt
= clone_xprt
->xp_master
;
1503 /* This must be a detached thread */
1504 ASSERT(clone_xprt
->xp_detached
);
1505 ASSERT(xprt
->xp_detached_threads
> 0);
1506 ASSERT(xprt
->xp_threads
+ xprt
->xp_detached_threads
> 0);
1508 /* Grab xprt->xp_thread_lock and decrement link counts */
1509 mutex_enter(&xprt
->xp_thread_lock
);
1510 xprt
->xp_detached_threads
--;
1512 /* svc_xprt_cleanup() unlocks xp_thread_lock or destroys xprt */
1514 mutex_exit(&xprt
->xp_thread_lock
);
1516 svc_xprt_cleanup(xprt
, TRUE
);
1518 /* Call transport specific clone `destroy' function */
1519 SVC_CLONE_DESTROY(clone_xprt
);
1521 /* Clear xp_master */
1522 clone_xprt
->xp_master
= NULL
;
1526 * Try to exit a non-detached service thread
1527 * - check if there are enough threads left
1528 * - if this thread (ie its clone transport handle) are linked
1529 * to a master transport then unlink it
1530 * - free the clone structure
1531 * - return to userland for thread exit
1533 * If this is the last non-detached or the last thread on this
1534 * transport then the call to svc_clone_unlink() will, respectively,
1535 * close and/or destroy the transport.
1538 svc_thread_exit(SVCPOOL
*pool
, SVCXPRT
*clone_xprt
)
1540 if (clone_xprt
->xp_master
)
1541 svc_clone_unlink(clone_xprt
);
1542 svc_clone_free(clone_xprt
);
1544 mutex_enter(&pool
->p_thread_lock
);
1546 if (pool
->p_closing
&& svc_pool_tryexit(pool
))
1547 /* return - thread exit will be handled at user level */
1549 mutex_exit(&pool
->p_thread_lock
);
1551 /* return - thread exit will be handled at user level */
1555 * Exit a detached service thread that returned to svc_run
1556 * - decrement the `detached thread' count for the pool
1557 * - unlink the detached clone transport handle from the master
1558 * - free the clone structure
1559 * - return to userland for thread exit
1561 * If this is the last thread on this transport then the call
1562 * to svc_clone_unlinkdetached() will destroy the transport.
1565 svc_thread_exitdetached(SVCPOOL
*pool
, SVCXPRT
*clone_xprt
)
1567 /* This must be a detached thread */
1568 ASSERT(clone_xprt
->xp_master
);
1569 ASSERT(clone_xprt
->xp_detached
);
1570 ASSERT(!MUTEX_HELD(&pool
->p_thread_lock
));
1572 svc_clone_unlinkdetached(clone_xprt
);
1573 svc_clone_free(clone_xprt
);
1575 mutex_enter(&pool
->p_thread_lock
);
1577 ASSERT(pool
->p_reserved_threads
>= 0);
1578 ASSERT(pool
->p_detached_threads
> 0);
1580 pool
->p_detached_threads
--;
1581 if (pool
->p_closing
&& svc_pool_tryexit(pool
))
1582 /* return - thread exit will be handled at user level */
1584 mutex_exit(&pool
->p_thread_lock
);
1586 /* return - thread exit will be handled at user level */
1590 * PSARC 2003/523 Contract Private Interface
1592 * Changes must be reviewed by Solaris File Sharing
1593 * Changes must be communicated to contract-2003-523@sun.com
1600 struct svc_globals
*svc
;
1602 svc
= zone_getspecific(svc_zone_key
, curproc
->p_zone
);
1603 mutex_enter(&svc
->svc_plock
);
1604 pool
= svc_pool_find(svc
, id
);
1605 mutex_exit(&svc
->svc_plock
);
1610 mutex_enter(&pool
->p_user_lock
);
1612 /* Check if there's already a user thread waiting on this pool */
1613 if (pool
->p_user_waiting
) {
1614 mutex_exit(&pool
->p_user_lock
);
1618 pool
->p_user_waiting
= TRUE
;
1620 /* Go to sleep, waiting for the signaled flag. */
1621 while (!pool
->p_signal_create_thread
&& !pool
->p_user_exit
) {
1622 if (cv_wait_sig(&pool
->p_user_cv
, &pool
->p_user_lock
) == 0) {
1623 /* Interrupted, return to handle exit or signal */
1624 pool
->p_user_waiting
= FALSE
;
1625 pool
->p_signal_create_thread
= FALSE
;
1626 mutex_exit(&pool
->p_user_lock
);
1629 * Thread has been interrupted and therefore
1630 * the service daemon is leaving as well so
1631 * let's go ahead and remove the service
1632 * pool at this time.
1634 mutex_enter(&svc
->svc_plock
);
1635 svc_pool_unregister(svc
, pool
);
1636 mutex_exit(&svc
->svc_plock
);
1642 pool
->p_signal_create_thread
= FALSE
;
1643 pool
->p_user_waiting
= FALSE
;
1646 * About to exit the service pool. Set return value
1647 * to let the userland code know our intent. Signal
1648 * svc_thread_creator() so that it can clean up the
1651 if (pool
->p_user_exit
) {
1653 cv_signal(&pool
->p_user_cv
);
1656 mutex_exit(&pool
->p_user_lock
);
1658 /* Return to userland with error code, for possible thread creation. */
1663 * `Service threads' creator thread.
1664 * The creator thread waits for a signal to create new thread.
1667 svc_thread_creator(SVCPOOL
*pool
)
1669 callb_cpr_t cpr_info
; /* CPR info for the creator thread */
1671 CALLB_CPR_INIT(&cpr_info
, &pool
->p_creator_lock
, callb_generic_cpr
,
1672 "svc_thread_creator");
1675 mutex_enter(&pool
->p_creator_lock
);
1677 /* Check if someone set the exit flag */
1678 if (pool
->p_creator_exit
)
1681 /* Clear the `signaled' flag and go asleep */
1682 pool
->p_creator_signaled
= FALSE
;
1684 CALLB_CPR_SAFE_BEGIN(&cpr_info
);
1685 cv_wait(&pool
->p_creator_cv
, &pool
->p_creator_lock
);
1686 CALLB_CPR_SAFE_END(&cpr_info
, &pool
->p_creator_lock
);
1688 /* Check if someone signaled to exit */
1689 if (pool
->p_creator_exit
)
1692 mutex_exit(&pool
->p_creator_lock
);
1694 mutex_enter(&pool
->p_thread_lock
);
1697 * When the pool is in closing state and all the transports
1698 * are gone the creator should not create any new threads.
1700 if (pool
->p_closing
) {
1701 rw_enter(&pool
->p_lrwlock
, RW_READER
);
1702 if (pool
->p_lcount
== 0) {
1703 rw_exit(&pool
->p_lrwlock
);
1704 mutex_exit(&pool
->p_thread_lock
);
1707 rw_exit(&pool
->p_lrwlock
);
1711 * Create a new service thread now.
1713 ASSERT(pool
->p_reserved_threads
>= 0);
1714 ASSERT(pool
->p_detached_threads
>= 0);
1716 if (pool
->p_threads
+ pool
->p_detached_threads
<
1717 pool
->p_maxthreads
) {
1719 * Signal the service pool wait thread
1720 * only if it hasn't already been signaled.
1722 mutex_enter(&pool
->p_user_lock
);
1723 if (pool
->p_signal_create_thread
== FALSE
) {
1724 pool
->p_signal_create_thread
= TRUE
;
1725 cv_signal(&pool
->p_user_cv
);
1727 mutex_exit(&pool
->p_user_lock
);
1731 mutex_exit(&pool
->p_thread_lock
);
1735 * Pool is closed. Cleanup and exit.
1738 /* Signal userland creator thread that it can stop now. */
1739 mutex_enter(&pool
->p_user_lock
);
1740 pool
->p_user_exit
= TRUE
;
1741 cv_broadcast(&pool
->p_user_cv
);
1742 mutex_exit(&pool
->p_user_lock
);
1744 /* Wait for svc_wait() to be done with the pool */
1745 mutex_enter(&pool
->p_user_lock
);
1746 while (pool
->p_user_waiting
) {
1747 CALLB_CPR_SAFE_BEGIN(&cpr_info
);
1748 cv_wait(&pool
->p_user_cv
, &pool
->p_user_lock
);
1749 CALLB_CPR_SAFE_END(&cpr_info
, &pool
->p_creator_lock
);
1751 mutex_exit(&pool
->p_user_lock
);
1753 CALLB_CPR_EXIT(&cpr_info
);
1754 svc_pool_cleanup(pool
);
1759 * If the creator thread is idle signal it to create
1760 * a new service thread.
1763 svc_creator_signal(SVCPOOL
*pool
)
1765 mutex_enter(&pool
->p_creator_lock
);
1766 if (pool
->p_creator_signaled
== FALSE
) {
1767 pool
->p_creator_signaled
= TRUE
;
1768 cv_signal(&pool
->p_creator_cv
);
1770 mutex_exit(&pool
->p_creator_lock
);
1774 * Notify the creator thread to clean up and exit.
1777 svc_creator_signalexit(SVCPOOL
*pool
)
1779 mutex_enter(&pool
->p_creator_lock
);
1780 pool
->p_creator_exit
= TRUE
;
1781 cv_signal(&pool
->p_creator_cv
);
1782 mutex_exit(&pool
->p_creator_lock
);
1786 * Polling part of the svc_run().
1787 * - search for a transport with a pending request
1788 * - when one is found then latch the request lock and return to svc_run()
1789 * - if there is no request go asleep and wait for a signal
1790 * - handle two exceptions:
1791 * a) current transport is closing
1792 * b) timeout waiting for a new request
1793 * in both cases return to svc_run()
1795 static SVCMASTERXPRT
*
1796 svc_poll(SVCPOOL
*pool
, SVCMASTERXPRT
*xprt
, SVCXPRT
*clone_xprt
)
1799 * Main loop iterates until
1800 * a) we find a pending request,
1801 * b) detect that the current transport is closing
1802 * c) time out waiting for a new request.
1805 SVCMASTERXPRT
*next
;
1810 * Check if there is a pending request on the current
1811 * transport handle so that we can avoid cloning.
1812 * If so then decrement the `pending-request' count for
1813 * the pool and return to svc_run().
1815 * We need to prevent a potential starvation. When
1816 * a selected transport has all pending requests coming in
1817 * all the time then the service threads will never switch to
1818 * another transport. With a limited number of service
1819 * threads some transports may be never serviced.
1820 * To prevent such a scenario we pick up at most
1821 * pool->p_max_same_xprt requests from the same transport
1822 * and then take a hint from the xprt-ready queue or walk
1823 * the transport list.
1825 if (xprt
&& xprt
->xp_req_head
&& (!pool
->p_qoverflow
||
1826 clone_xprt
->xp_same_xprt
++ < pool
->p_max_same_xprt
)) {
1827 mutex_enter(&xprt
->xp_req_lock
);
1828 if (xprt
->xp_req_head
)
1830 mutex_exit(&xprt
->xp_req_lock
);
1832 clone_xprt
->xp_same_xprt
= 0;
1836 * If there is no request on the current transport try to
1837 * find another transport with a pending request.
1839 mutex_enter(&pool
->p_req_lock
);
1841 mutex_exit(&pool
->p_req_lock
);
1844 * Make sure that transports will not be destroyed just
1845 * while we are checking them.
1847 rw_enter(&pool
->p_lrwlock
, RW_READER
);
1850 SVCMASTERXPRT
*hint
;
1853 * Get the next transport from the xprt-ready queue.
1854 * This is a hint. There is no guarantee that the
1855 * transport still has a pending request since it
1856 * could be picked up by another thread in step 1.
1858 * If the transport has a pending request then keep
1859 * it locked. Decrement the `pending-requests' for
1860 * the pool and `walking-threads' counts, and return
1863 hint
= svc_xprt_qget(pool
);
1865 if (hint
&& hint
->xp_req_head
) {
1866 mutex_enter(&hint
->xp_req_lock
);
1867 if (hint
->xp_req_head
) {
1868 rw_exit(&pool
->p_lrwlock
);
1870 mutex_enter(&pool
->p_req_lock
);
1872 mutex_exit(&pool
->p_req_lock
);
1876 mutex_exit(&hint
->xp_req_lock
);
1880 * If there was no hint in the xprt-ready queue then
1881 * - if there is less pending requests than polling
1883 * - otherwise check if there was an overflow in the
1884 * xprt-ready queue; if so, then we need to break
1888 if (pool
->p_reqs
< pool
->p_walkers
) {
1889 mutex_enter(&pool
->p_req_lock
);
1890 if (pool
->p_reqs
< pool
->p_walkers
)
1892 mutex_exit(&pool
->p_req_lock
);
1894 if (pool
->p_qoverflow
) {
1901 * If there was an overflow in the xprt-ready queue then we
1902 * need to switch to the `drain' mode, i.e. walk through the
1903 * pool's transport list and search for a transport with a
1904 * pending request. If we manage to drain all the pending
1905 * requests then we can clear the overflow flag. This will
1906 * switch svc_poll() back to taking hints from the xprt-ready
1907 * queue (which is generally more efficient).
1909 * If there are no registered transports simply go asleep.
1911 if (xprt
== NULL
&& pool
->p_lhead
== NULL
) {
1912 mutex_enter(&pool
->p_req_lock
);
1917 * `Walk' through the pool's list of master server
1918 * transport handles. Continue to loop until there are less
1919 * looping threads then pending requests.
1921 next
= xprt
? xprt
->xp_next
: pool
->p_lhead
;
1925 * Check if there is a request on this transport.
1927 * Since blocking on a locked mutex is very expensive
1928 * check for a request without a lock first. If we miss
1929 * a request that is just being delivered but this will
1930 * cost at most one full walk through the list.
1932 if (next
->xp_req_head
) {
1934 * Check again, now with a lock.
1936 mutex_enter(&next
->xp_req_lock
);
1937 if (next
->xp_req_head
) {
1938 rw_exit(&pool
->p_lrwlock
);
1940 mutex_enter(&pool
->p_req_lock
);
1942 mutex_exit(&pool
->p_req_lock
);
1946 mutex_exit(&next
->xp_req_lock
);
1950 * Continue to `walk' through the pool's
1951 * transport list until there is less requests
1952 * than walkers. Check this condition without
1953 * a lock first to avoid contention on a mutex.
1955 if (pool
->p_reqs
< pool
->p_walkers
) {
1956 /* Check again, now with the lock. */
1957 mutex_enter(&pool
->p_req_lock
);
1958 if (pool
->p_reqs
< pool
->p_walkers
)
1959 break; /* goto sleep */
1960 mutex_exit(&pool
->p_req_lock
);
1963 next
= next
->xp_next
;
1968 * No work to do. Stop the `walk' and go asleep.
1969 * Decrement the `walking-threads' count for the pool.
1972 rw_exit(&pool
->p_lrwlock
);
1975 * Count us as asleep, mark this thread as safe
1976 * for suspend and wait for a request.
1979 timeleft
= cv_reltimedwait_sig(&pool
->p_req_cv
,
1980 &pool
->p_req_lock
, pool
->p_timeout
, TR_CLOCK_TICK
);
1983 * If the drowsy flag is on this means that
1984 * someone has signaled a wakeup. In such a case
1985 * the `asleep-threads' count has already updated
1986 * so just clear the flag.
1988 * If the drowsy flag is off then we need to update
1989 * the `asleep-threads' count.
1991 if (pool
->p_drowsy
) {
1992 pool
->p_drowsy
= FALSE
;
1994 * If the thread is here because it timedout,
1995 * instead of returning SVC_ETIMEDOUT, it is
1996 * time to do some more work.
2003 mutex_exit(&pool
->p_req_lock
);
2006 * If we received a signal while waiting for a
2007 * request, inform svc_run(), so that we can return
2008 * to user level and exit.
2014 * If the current transport is gone then notify
2015 * svc_run() to unlink from it.
2017 if (xprt
&& xprt
->xp_wq
== NULL
)
2018 return (SVC_EXPRTGONE
);
2021 * If we have timed out waiting for a request inform
2022 * svc_run() that we probably don't need this thread.
2025 return (SVC_ETIMEDOUT
);
2030 * calculate memory space used by message
2033 svc_msgsize(mblk_t
*mp
)
2037 for (; mp
; mp
= mp
->b_cont
)
2038 count
+= MBLKSIZE(mp
);
2044 * svc_flowcontrol() attempts to turn the flow control on or off for the
2047 * On input the xprt->xp_full determines whether the flow control is currently
2048 * off (FALSE) or on (TRUE). If it is off we do tests to see whether we should
2049 * turn it on, and vice versa.
2051 * There are two conditions considered for the flow control. Both conditions
2052 * have the low and the high watermark. Once the high watermark is reached in
2053 * EITHER condition the flow control is turned on. For turning the flow
2054 * control off BOTH conditions must be below the low watermark.
2056 * Condition #1 - Number of requests queued:
2058 * The max number of threads working on the pool is roughly pool->p_maxthreads.
2059 * Every thread could handle up to pool->p_max_same_xprt requests from one
2060 * transport before it moves to another transport. See svc_poll() for details.
2061 * In case all threads in the pool are working on a transport they will handle
2062 * no more than enough_reqs (pool->p_maxthreads * pool->p_max_same_xprt)
2063 * requests in one shot from that transport. We are turning the flow control
2064 * on once the high watermark is reached for a transport so that the underlying
2065 * queue knows the rate of incoming requests is higher than we are able to
2068 * The high watermark: 2 * enough_reqs
2069 * The low watermark: enough_reqs
2071 * Condition #2 - Length of the data payload for the queued messages/requests:
2073 * We want to prevent a particular pool exhausting the memory, so once the
2074 * total length of queued requests for the whole pool reaches the high
2075 * watermark we start to turn on the flow control for significant memory
2076 * consumers (individual transports). To keep the implementation simple
2077 * enough, this condition is not exact, because we count only the data part of
2078 * the queued requests and we ignore the overhead. For our purposes this
2079 * should be enough. We should also consider that up to pool->p_maxthreads
2080 * threads for the pool might work on large requests (this is not counted for
2081 * this condition). We need to leave some space for rest of the system and for
2082 * other big memory consumers (like ZFS). Also, after the flow control is
2083 * turned on (on cots transports) we can start to accumulate a few megabytes in
2084 * queues for each transport.
2086 * Usually, the big memory consumers are NFS WRITE requests, so we do not
2087 * expect to see this condition met for other than NFS pools.
2089 * The high watermark: 1/5 of available memory
2090 * The low watermark: 1/6 of available memory
2092 * Once the high watermark is reached we turn the flow control on only for
2093 * transports exceeding a per-transport memory limit. The per-transport
2094 * fraction of memory is calculated as:
2096 * the high watermark / number of transports
2098 * For transports with less than the per-transport fraction of memory consumed,
2099 * the flow control is not turned on, so they are not blocked by a few "hungry"
2100 * transports. Because of this, the total memory consumption for the
2101 * particular pool might grow up to 2 * the high watermark.
2103 * The individual transports are unblocked once their consumption is below:
2105 * per-transport fraction of memory / 2
2107 * or once the total memory consumption for the whole pool falls below the low
2112 svc_flowcontrol(SVCMASTERXPRT
*xprt
)
2114 SVCPOOL
*pool
= xprt
->xp_pool
;
2115 size_t totalmem
= ptob(physmem
);
2116 int enough_reqs
= pool
->p_maxthreads
* pool
->p_max_same_xprt
;
2118 ASSERT(MUTEX_HELD(&xprt
->xp_req_lock
));
2120 /* Should we turn the flow control on? */
2121 if (xprt
->xp_full
== FALSE
) {
2122 /* Is flow control disabled? */
2123 if (svc_flowcontrol_disable
!= 0)
2126 /* Is there enough requests queued? */
2127 if (xprt
->xp_reqs
>= enough_reqs
* 2) {
2128 xprt
->xp_full
= TRUE
;
2133 * If this pool uses over 20% of memory and this transport is
2134 * significant memory consumer then we are full
2136 if (pool
->p_size
>= totalmem
/ 5 &&
2137 xprt
->xp_size
>= totalmem
/ 5 / pool
->p_lcount
)
2138 xprt
->xp_full
= TRUE
;
2143 /* We might want to turn the flow control off */
2145 /* Do we still have enough requests? */
2146 if (xprt
->xp_reqs
> enough_reqs
)
2150 * If this pool still uses over 16% of memory and this transport is
2151 * still significant memory consumer then we are still full
2153 if (pool
->p_size
>= totalmem
/ 6 &&
2154 xprt
->xp_size
>= totalmem
/ 5 / pool
->p_lcount
/ 2)
2157 /* Turn the flow control off and make sure rpcmod is notified */
2158 xprt
->xp_full
= FALSE
;
2159 xprt
->xp_enable
= TRUE
;
2163 * Main loop of the kernel RPC server
2164 * - wait for input (find a transport with a pending request).
2165 * - dequeue the request
2166 * - call a registered server routine to process the requests
2168 * There can many threads running concurrently in this loop
2169 * on the same or on different transports.
2172 svc_run(SVCPOOL
*pool
)
2174 SVCMASTERXPRT
*xprt
= NULL
; /* master transport handle */
2175 SVCXPRT
*clone_xprt
; /* clone for this thread */
2176 proc_t
*p
= ttoproc(curthread
);
2178 /* Allocate a clone transport handle for this thread */
2179 clone_xprt
= svc_clone_init();
2182 * The loop iterates until the thread becomes
2183 * idle too long or the transport is gone.
2186 SVCMASTERXPRT
*next
;
2191 TRACE_0(TR_FAC_KRPC
, TR_SVC_RUN
, "svc_run");
2194 * If the process is exiting/killed, return
2195 * immediately without processing any more
2198 if (p
->p_flag
& (SEXITING
| SKILLED
)) {
2199 svc_thread_exit(pool
, clone_xprt
);
2203 /* Find a transport with a pending request */
2204 next
= svc_poll(pool
, xprt
, clone_xprt
);
2207 * If svc_poll() finds a transport with a request
2208 * it latches xp_req_lock on it. Therefore we need
2209 * to dequeue the request and release the lock as
2212 ASSERT(next
!= NULL
&&
2213 (next
== SVC_EXPRTGONE
||
2214 next
== SVC_ETIMEDOUT
||
2215 next
== SVC_EINTR
||
2216 MUTEX_HELD(&next
->xp_req_lock
)));
2218 /* Ooops! Current transport is closing. Unlink now */
2219 if (next
== SVC_EXPRTGONE
) {
2220 svc_clone_unlink(clone_xprt
);
2225 /* Ooops! Timeout while waiting for a request. Exit */
2226 if (next
== SVC_ETIMEDOUT
) {
2227 svc_thread_exit(pool
, clone_xprt
);
2232 * Interrupted by a signal while waiting for a
2233 * request. Return to userspace and exit.
2235 if (next
== SVC_EINTR
) {
2236 svc_thread_exit(pool
, clone_xprt
);
2241 * De-queue the request and release the request lock
2242 * on this transport (latched by svc_poll()).
2244 mp
= next
->xp_req_head
;
2245 next
->xp_req_head
= mp
->b_next
;
2246 mp
->b_next
= (mblk_t
*)0;
2247 size
= svc_msgsize(mp
);
2249 mutex_enter(&pool
->p_req_lock
);
2251 if (pool
->p_reqs
== 0)
2252 pool
->p_qoverflow
= FALSE
;
2253 pool
->p_size
-= size
;
2254 mutex_exit(&pool
->p_req_lock
);
2257 next
->xp_size
-= size
;
2260 svc_flowcontrol(next
);
2262 TRACE_2(TR_FAC_KRPC
, TR_NFSFP_QUE_REQ_DEQ
,
2263 "rpc_que_req_deq:pool %p mp %p", pool
, mp
);
2264 mutex_exit(&next
->xp_req_lock
);
2267 * If this is a new request on a current transport then
2268 * the clone structure is already properly initialized.
2269 * Otherwise, if the request is on a different transport,
2270 * unlink from the current master and link to
2271 * the one we got a request on.
2275 svc_clone_unlink(clone_xprt
);
2276 svc_clone_link(next
, clone_xprt
, NULL
);
2281 * If there are more requests and req_cv hasn't
2282 * been signaled yet then wake up one more thread now.
2284 * We avoid signaling req_cv until the most recently
2285 * signaled thread wakes up and gets CPU to clear
2286 * the `drowsy' flag.
2288 if (!(pool
->p_drowsy
|| pool
->p_reqs
<= pool
->p_walkers
||
2289 pool
->p_asleep
== 0)) {
2290 mutex_enter(&pool
->p_req_lock
);
2292 if (pool
->p_drowsy
|| pool
->p_reqs
<= pool
->p_walkers
||
2293 pool
->p_asleep
== 0)
2294 mutex_exit(&pool
->p_req_lock
);
2297 pool
->p_drowsy
= TRUE
;
2299 cv_signal(&pool
->p_req_cv
);
2300 mutex_exit(&pool
->p_req_lock
);
2305 * If there are no asleep/signaled threads, we are
2306 * still below pool->p_maxthreads limit, and no thread is
2307 * currently being created then signal the creator
2308 * for one more service thread.
2310 * The asleep and drowsy checks are not protected
2311 * by a lock since it hurts performance and a wrong
2312 * decision is not essential.
2314 if (pool
->p_asleep
== 0 && !pool
->p_drowsy
&&
2315 pool
->p_threads
+ pool
->p_detached_threads
<
2317 svc_creator_signal(pool
);
2320 * Process the request.
2322 svc_getreq(clone_xprt
, mp
);
2324 /* If thread had a reservation it should have been canceled */
2325 ASSERT(!clone_xprt
->xp_reserved
);
2328 * If the clone is marked detached then exit.
2329 * The rpcmod slot has already been released
2330 * when we detached this thread.
2332 if (clone_xprt
->xp_detached
) {
2333 svc_thread_exitdetached(pool
, clone_xprt
);
2338 * Release our reference on the rpcmod
2339 * slot attached to xp_wq->q_ptr.
2341 mutex_enter(&xprt
->xp_req_lock
);
2342 enable
= xprt
->xp_enable
;
2344 xprt
->xp_enable
= FALSE
;
2345 mutex_exit(&xprt
->xp_req_lock
);
2346 SVC_RELE(clone_xprt
, NULL
, enable
);
2352 * Flush any pending requests for the queue and
2353 * free the associated mblks.
2356 svc_queueclean(queue_t
*q
)
2358 SVCMASTERXPRT
*xprt
= ((void **) q
->q_ptr
)[0];
2363 * clean up the requests
2365 mutex_enter(&xprt
->xp_req_lock
);
2366 pool
= xprt
->xp_pool
;
2367 while ((mp
= xprt
->xp_req_head
) != NULL
) {
2368 /* remove the request from the list */
2369 xprt
->xp_req_head
= mp
->b_next
;
2370 mp
->b_next
= (mblk_t
*)0;
2371 SVC_RELE(xprt
, mp
, FALSE
);
2374 mutex_enter(&pool
->p_req_lock
);
2375 pool
->p_reqs
-= xprt
->xp_reqs
;
2376 pool
->p_size
-= xprt
->xp_size
;
2377 mutex_exit(&pool
->p_req_lock
);
2381 xprt
->xp_full
= FALSE
;
2382 xprt
->xp_enable
= FALSE
;
2383 mutex_exit(&xprt
->xp_req_lock
);
2387 * This routine is called by rpcmod to inform kernel RPC that a
2388 * queue is closing. It is called after all the requests have been
2389 * picked up (that is after all the slots on the queue have
2390 * been released by kernel RPC). It is also guaranteed that no more
2391 * request will be delivered on this transport.
2393 * - clear xp_wq to mark the master server transport handle as closing
2394 * - if there are no more threads on this transport close/destroy it
2395 * - otherwise, leave the linked threads to close/destroy the transport
2399 svc_queueclose(queue_t
*q
)
2401 SVCMASTERXPRT
*xprt
= ((void **) q
->q_ptr
)[0];
2405 * If there is no master xprt associated with this stream,
2406 * then there is nothing to do. This happens regularly
2407 * with connection-oriented listening streams created by
2413 mutex_enter(&xprt
->xp_thread_lock
);
2415 ASSERT(xprt
->xp_req_head
== NULL
);
2416 ASSERT(xprt
->xp_wq
!= NULL
);
2420 if (xprt
->xp_threads
== 0) {
2421 SVCPOOL
*pool
= xprt
->xp_pool
;
2424 * svc_xprt_cleanup() destroys the transport
2425 * or releases the transport thread lock
2427 svc_xprt_cleanup(xprt
, FALSE
);
2429 mutex_enter(&pool
->p_thread_lock
);
2432 * If the pool is in closing state and this was
2433 * the last transport in the pool then signal the creator
2434 * thread to clean up and exit.
2436 if (pool
->p_closing
&& svc_pool_tryexit(pool
)) {
2439 mutex_exit(&pool
->p_thread_lock
);
2442 * There are still some threads linked to the transport. They
2443 * are very likely sleeping in svc_poll(). We could wake up
2444 * them by broadcasting on the p_req_cv condition variable, but
2445 * that might give us a performance penalty if there are too
2446 * many sleeping threads.
2448 * Instead, we do nothing here. The linked threads will unlink
2449 * themselves and destroy the transport once they are woken up
2450 * on timeout, or by new request. There is no reason to hurry
2451 * up now with the thread wake up.
2455 * NOTICE: No references to the master transport structure
2456 * beyond this point!
2458 mutex_exit(&xprt
->xp_thread_lock
);
2463 * Interrupt `request delivery' routine called from rpcmod
2464 * - put a request at the tail of the transport request queue
2465 * - insert a hint for svc_poll() into the xprt-ready queue
2466 * - increment the `pending-requests' count for the pool
2467 * - handle flow control
2468 * - wake up a thread sleeping in svc_poll() if necessary
2469 * - if all the threads are running ask the creator for a new one.
2472 svc_queuereq(queue_t
*q
, mblk_t
*mp
, bool_t flowcontrol
)
2474 SVCMASTERXPRT
*xprt
= ((void **) q
->q_ptr
)[0];
2475 SVCPOOL
*pool
= xprt
->xp_pool
;
2478 TRACE_0(TR_FAC_KRPC
, TR_SVC_QUEUEREQ_START
, "svc_queuereq_start");
2482 * Grab the transport's request lock and the
2483 * pool's request lock so that when we put
2484 * the request at the tail of the transport's
2485 * request queue, possibly put the request on
2486 * the xprt ready queue and increment the
2487 * pending request count it looks atomic.
2489 mutex_enter(&xprt
->xp_req_lock
);
2490 if (flowcontrol
&& xprt
->xp_full
) {
2491 mutex_exit(&xprt
->xp_req_lock
);
2495 ASSERT(xprt
->xp_full
== FALSE
);
2496 mutex_enter(&pool
->p_req_lock
);
2497 if (xprt
->xp_req_head
== NULL
)
2498 xprt
->xp_req_head
= mp
;
2500 xprt
->xp_req_tail
->b_next
= mp
;
2501 xprt
->xp_req_tail
= mp
;
2505 * Insert a hint into the xprt-ready queue, increment
2506 * counters, handle flow control, and wake up
2507 * a thread sleeping in svc_poll() if necessary.
2510 /* Insert pointer to this transport into the xprt-ready queue */
2511 svc_xprt_qput(pool
, xprt
);
2513 /* Increment counters */
2517 size
= svc_msgsize(mp
);
2518 xprt
->xp_size
+= size
;
2519 pool
->p_size
+= size
;
2521 /* Handle flow control */
2523 svc_flowcontrol(xprt
);
2525 TRACE_2(TR_FAC_KRPC
, TR_NFSFP_QUE_REQ_ENQ
,
2526 "rpc_que_req_enq:pool %p mp %p", pool
, mp
);
2529 * If there are more requests and req_cv hasn't
2530 * been signaled yet then wake up one more thread now.
2532 * We avoid signaling req_cv until the most recently
2533 * signaled thread wakes up and gets CPU to clear
2534 * the `drowsy' flag.
2536 if (pool
->p_drowsy
|| pool
->p_reqs
<= pool
->p_walkers
||
2537 pool
->p_asleep
== 0) {
2538 mutex_exit(&pool
->p_req_lock
);
2540 pool
->p_drowsy
= TRUE
;
2544 * Signal wakeup and drop the request lock.
2546 cv_signal(&pool
->p_req_cv
);
2547 mutex_exit(&pool
->p_req_lock
);
2549 mutex_exit(&xprt
->xp_req_lock
);
2553 * If there are no asleep/signaled threads, we are
2554 * still below pool->p_maxthreads limit, and no thread is
2555 * currently being created then signal the creator
2556 * for one more service thread.
2558 * The asleep and drowsy checks are not not protected
2559 * by a lock since it hurts performance and a wrong
2560 * decision is not essential.
2562 if (pool
->p_asleep
== 0 && !pool
->p_drowsy
&&
2563 pool
->p_threads
+ pool
->p_detached_threads
< pool
->p_maxthreads
)
2564 svc_creator_signal(pool
);
2566 TRACE_1(TR_FAC_KRPC
, TR_SVC_QUEUEREQ_END
,
2567 "svc_queuereq_end:(%S)", "end");
2573 * Reserve a service thread so that it can be detached later.
2574 * This reservation is required to make sure that when it tries to
2575 * detach itself the total number of detached threads does not exceed
2576 * pool->p_maxthreads - pool->p_redline (i.e. that we can have
2577 * up to pool->p_redline non-detached threads).
2579 * If the thread does not detach itself later, it should cancel the
2580 * reservation before returning to svc_run().
2582 * - check if there is room for more reserved/detached threads
2583 * - if so, then increment the `reserved threads' count for the pool
2584 * - mark the thread as reserved (setting the flag in the clone transport
2585 * handle for this thread
2586 * - returns 1 if the reservation succeeded, 0 if it failed.
2589 svc_reserve_thread(SVCXPRT
*clone_xprt
)
2591 SVCPOOL
*pool
= clone_xprt
->xp_master
->xp_pool
;
2593 /* Recursive reservations are not allowed */
2594 ASSERT(!clone_xprt
->xp_reserved
);
2595 ASSERT(!clone_xprt
->xp_detached
);
2597 /* Check pool counts if there is room for reservation */
2598 mutex_enter(&pool
->p_thread_lock
);
2599 if (pool
->p_reserved_threads
+ pool
->p_detached_threads
>=
2600 pool
->p_maxthreads
- pool
->p_redline
) {
2601 mutex_exit(&pool
->p_thread_lock
);
2604 pool
->p_reserved_threads
++;
2605 mutex_exit(&pool
->p_thread_lock
);
2607 /* Mark the thread (clone handle) as reserved */
2608 clone_xprt
->xp_reserved
= TRUE
;
2614 * Cancel a reservation for a thread.
2615 * - decrement the `reserved threads' count for the pool
2616 * - clear the flag in the clone transport handle for this thread.
2619 svc_unreserve_thread(SVCXPRT
*clone_xprt
)
2621 SVCPOOL
*pool
= clone_xprt
->xp_master
->xp_pool
;
2623 /* Thread must have a reservation */
2624 ASSERT(clone_xprt
->xp_reserved
);
2625 ASSERT(!clone_xprt
->xp_detached
);
2627 /* Decrement global count */
2628 mutex_enter(&pool
->p_thread_lock
);
2629 pool
->p_reserved_threads
--;
2630 mutex_exit(&pool
->p_thread_lock
);
2632 /* Clear reservation flag */
2633 clone_xprt
->xp_reserved
= FALSE
;
2637 * Detach a thread from its transport, so that it can block for an
2638 * extended time. Because the transport can be closed after the thread is
2639 * detached, the thread should have already sent off a reply if it was
2640 * going to send one.
2642 * - decrement `non-detached threads' count and increment `detached threads'
2643 * counts for the transport
2644 * - decrement the `non-detached threads' and `reserved threads'
2645 * counts and increment the `detached threads' count for the pool
2646 * - release the rpcmod slot
2647 * - mark the clone (thread) as detached.
2649 * No need to return a pointer to the thread's CPR information, since
2650 * the thread has a userland identity.
2652 * NOTICE: a thread must not detach itself without making a prior reservation
2653 * through svc_thread_reserve().
2656 svc_detach_thread(SVCXPRT
*clone_xprt
)
2658 SVCMASTERXPRT
*xprt
= clone_xprt
->xp_master
;
2659 SVCPOOL
*pool
= xprt
->xp_pool
;
2662 /* Thread must have a reservation */
2663 ASSERT(clone_xprt
->xp_reserved
);
2664 ASSERT(!clone_xprt
->xp_detached
);
2666 /* Bookkeeping for this transport */
2667 mutex_enter(&xprt
->xp_thread_lock
);
2669 xprt
->xp_detached_threads
++;
2670 mutex_exit(&xprt
->xp_thread_lock
);
2672 /* Bookkeeping for the pool */
2673 mutex_enter(&pool
->p_thread_lock
);
2675 pool
->p_reserved_threads
--;
2676 pool
->p_detached_threads
++;
2677 mutex_exit(&pool
->p_thread_lock
);
2679 /* Release an rpcmod slot for this request */
2680 mutex_enter(&xprt
->xp_req_lock
);
2681 enable
= xprt
->xp_enable
;
2683 xprt
->xp_enable
= FALSE
;
2684 mutex_exit(&xprt
->xp_req_lock
);
2685 SVC_RELE(clone_xprt
, NULL
, enable
);
2687 /* Mark the clone (thread) as detached */
2688 clone_xprt
->xp_reserved
= FALSE
;
2689 clone_xprt
->xp_detached
= TRUE
;
2695 * This routine is responsible for extracting RDMA plugin master XPRT,
2696 * unregister from the SVCPOOL and initiate plugin specific cleanup.
2697 * It is passed a list/group of rdma transports as records which are
2698 * active in a given registered or unregistered kRPC thread pool. Its shuts
2699 * all active rdma transports in that pool. If the thread active on the trasport
2700 * happens to be last thread for that pool, it will signal the creater thread
2701 * to cleanup the pool and destroy the xprt in svc_queueclose()
2704 rdma_stop(rdma_xprt_group_t
*rdma_xprts
)
2706 SVCMASTERXPRT
*xprt
;
2707 rdma_xprt_record_t
*curr_rec
;
2713 if (rdma_xprts
->rtg_count
== 0)
2716 rtg_count
= rdma_xprts
->rtg_count
;
2718 for (i
= 0; i
< rtg_count
; i
++) {
2719 curr_rec
= rdma_xprts
->rtg_listhead
;
2720 rdma_xprts
->rtg_listhead
= curr_rec
->rtr_next
;
2721 rdma_xprts
->rtg_count
--;
2722 curr_rec
->rtr_next
= NULL
;
2723 xprt
= curr_rec
->rtr_xprt_ptr
;
2725 svc_rdma_kstop(xprt
);
2727 mutex_enter(&xprt
->xp_req_lock
);
2728 pool
= xprt
->xp_pool
;
2729 while ((mp
= xprt
->xp_req_head
) != NULL
) {
2730 rdma_recv_data_t
*rdp
= (rdma_recv_data_t
*)mp
->b_rptr
;
2732 /* remove the request from the list */
2733 xprt
->xp_req_head
= mp
->b_next
;
2734 mp
->b_next
= (mblk_t
*)0;
2736 RDMA_BUF_FREE(rdp
->conn
, &rdp
->rpcmsg
);
2737 RDMA_REL_CONN(rdp
->conn
);
2740 mutex_enter(&pool
->p_req_lock
);
2741 pool
->p_reqs
-= xprt
->xp_reqs
;
2742 pool
->p_size
-= xprt
->xp_size
;
2743 mutex_exit(&pool
->p_req_lock
);
2746 xprt
->xp_full
= FALSE
;
2747 xprt
->xp_enable
= FALSE
;
2748 mutex_exit(&xprt
->xp_req_lock
);
2752 cmn_err(CE_NOTE
, "rdma_stop: Exited svc_queueclose\n");
2755 * Free the rdma transport record for the expunged rdma
2756 * based master transport handle.
2758 kmem_free(curr_rec
, sizeof (rdma_xprt_record_t
));
2759 if (!rdma_xprts
->rtg_listhead
)
2766 * rpc_msg_dup/rpc_msg_free
2767 * Currently only used by svc_rpcsec_gss.c but put in this file as it
2768 * may be useful to others in the future.
2769 * But future consumers should be careful cuz so far
2770 * - only tested/used for call msgs (not reply)
2771 * - only tested/used with call verf oa_length==0
2774 rpc_msg_dup(struct rpc_msg
*src
)
2776 struct rpc_msg
*dst
;
2777 struct opaque_auth oa_src
, oa_dst
;
2779 dst
= kmem_alloc(sizeof (*dst
), KM_SLEEP
);
2781 dst
->rm_xid
= src
->rm_xid
;
2782 dst
->rm_direction
= src
->rm_direction
;
2784 dst
->rm_call
.cb_rpcvers
= src
->rm_call
.cb_rpcvers
;
2785 dst
->rm_call
.cb_prog
= src
->rm_call
.cb_prog
;
2786 dst
->rm_call
.cb_vers
= src
->rm_call
.cb_vers
;
2787 dst
->rm_call
.cb_proc
= src
->rm_call
.cb_proc
;
2789 /* dup opaque auth call body cred */
2790 oa_src
= src
->rm_call
.cb_cred
;
2792 oa_dst
.oa_flavor
= oa_src
.oa_flavor
;
2793 oa_dst
.oa_base
= kmem_alloc(oa_src
.oa_length
, KM_SLEEP
);
2795 bcopy(oa_src
.oa_base
, oa_dst
.oa_base
, oa_src
.oa_length
);
2796 oa_dst
.oa_length
= oa_src
.oa_length
;
2798 dst
->rm_call
.cb_cred
= oa_dst
;
2800 /* dup or just alloc opaque auth call body verifier */
2801 if (src
->rm_call
.cb_verf
.oa_length
> 0) {
2802 oa_src
= src
->rm_call
.cb_verf
;
2804 oa_dst
.oa_flavor
= oa_src
.oa_flavor
;
2805 oa_dst
.oa_base
= kmem_alloc(oa_src
.oa_length
, KM_SLEEP
);
2807 bcopy(oa_src
.oa_base
, oa_dst
.oa_base
, oa_src
.oa_length
);
2808 oa_dst
.oa_length
= oa_src
.oa_length
;
2810 dst
->rm_call
.cb_verf
= oa_dst
;
2812 oa_dst
.oa_flavor
= -1; /* will be set later */
2813 oa_dst
.oa_base
= kmem_alloc(MAX_AUTH_BYTES
, KM_SLEEP
);
2815 oa_dst
.oa_length
= 0; /* will be set later */
2817 dst
->rm_call
.cb_verf
= oa_dst
;
2822 kmem_free(dst
->rm_call
.cb_cred
.oa_base
, dst
->rm_call
.cb_cred
.oa_length
);
2823 kmem_free(dst
, sizeof (*dst
));
2828 rpc_msg_free(struct rpc_msg
**msg
, int cb_verf_oa_length
)
2830 struct rpc_msg
*m
= *msg
;
2832 kmem_free(m
->rm_call
.cb_cred
.oa_base
, m
->rm_call
.cb_cred
.oa_length
);
2833 m
->rm_call
.cb_cred
.oa_base
= NULL
;
2834 m
->rm_call
.cb_cred
.oa_length
= 0;
2836 kmem_free(m
->rm_call
.cb_verf
.oa_base
, cb_verf_oa_length
);
2837 m
->rm_call
.cb_verf
.oa_base
= NULL
;
2838 m
->rm_call
.cb_verf
.oa_length
= 0;
2840 kmem_free(m
, sizeof (*m
));