1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate /* 28*7c478bd9Sstevel@tonic-gate * Copyright 1993 OpenVision Technologies, Inc., All Rights Reserved. 29*7c478bd9Sstevel@tonic-gate */ 30*7c478bd9Sstevel@tonic-gate 31*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 32*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 33*7c478bd9Sstevel@tonic-gate 34*7c478bd9Sstevel@tonic-gate /* 35*7c478bd9Sstevel@tonic-gate * Portions of this source code were derived from Berkeley 4.3 BSD 36*7c478bd9Sstevel@tonic-gate * under license from the Regents of the University of California. 37*7c478bd9Sstevel@tonic-gate */ 38*7c478bd9Sstevel@tonic-gate 39*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 40*7c478bd9Sstevel@tonic-gate 41*7c478bd9Sstevel@tonic-gate /* 42*7c478bd9Sstevel@tonic-gate * Server-side remote procedure call interface. 43*7c478bd9Sstevel@tonic-gate * 44*7c478bd9Sstevel@tonic-gate * Master transport handle (SVCMASTERXPRT). 45*7c478bd9Sstevel@tonic-gate * The master transport handle structure is shared among service 46*7c478bd9Sstevel@tonic-gate * threads processing events on the transport. Some fields in the 47*7c478bd9Sstevel@tonic-gate * master structure are protected by locks 48*7c478bd9Sstevel@tonic-gate * - xp_req_lock protects the request queue: 49*7c478bd9Sstevel@tonic-gate * xp_req_head, xp_req_tail 50*7c478bd9Sstevel@tonic-gate * - xp_thread_lock protects the thread (clone) counts 51*7c478bd9Sstevel@tonic-gate * xp_threads, xp_detached_threads, xp_wq 52*7c478bd9Sstevel@tonic-gate * Each master transport is registered to exactly one thread pool. 53*7c478bd9Sstevel@tonic-gate * 54*7c478bd9Sstevel@tonic-gate * Clone transport handle (SVCXPRT) 55*7c478bd9Sstevel@tonic-gate * The clone transport handle structure is a per-service-thread handle 56*7c478bd9Sstevel@tonic-gate * to the transport. The structure carries all the fields/buffers used 57*7c478bd9Sstevel@tonic-gate * for request processing. A service thread or, in other words, a clone 58*7c478bd9Sstevel@tonic-gate * structure, can be linked to an arbitrary master structure to process 59*7c478bd9Sstevel@tonic-gate * requests on this transport. The master handle keeps track of reference 60*7c478bd9Sstevel@tonic-gate * counts of threads (clones) linked to it. A service thread can switch 61*7c478bd9Sstevel@tonic-gate * to another transport by unlinking its clone handle from the current 62*7c478bd9Sstevel@tonic-gate * transport and linking to a new one. Switching is relatively inexpensive 63*7c478bd9Sstevel@tonic-gate * but it involves locking (master's xprt->xp_thread_lock). 64*7c478bd9Sstevel@tonic-gate * 65*7c478bd9Sstevel@tonic-gate * Pools. 66*7c478bd9Sstevel@tonic-gate * A pool represents a kernel RPC service (NFS, Lock Manager, etc.). 67*7c478bd9Sstevel@tonic-gate * Transports related to the service are registered to the service pool. 68*7c478bd9Sstevel@tonic-gate * Service threads can switch between different transports in the pool. 69*7c478bd9Sstevel@tonic-gate * Thus, each service has its own pool of service threads. The maximum 70*7c478bd9Sstevel@tonic-gate * number of threads in a pool is pool->p_maxthreads. This limit allows 71*7c478bd9Sstevel@tonic-gate * to restrict resource usage by the service. Some fields are protected 72*7c478bd9Sstevel@tonic-gate * by locks: 73*7c478bd9Sstevel@tonic-gate * - p_req_lock protects several counts and flags: 74*7c478bd9Sstevel@tonic-gate * p_reqs, p_walkers, p_asleep, p_drowsy, p_req_cv 75*7c478bd9Sstevel@tonic-gate * - p_thread_lock governs other thread counts: 76*7c478bd9Sstevel@tonic-gate * p_threads, p_detached_threads, p_reserved_threads, p_closing 77*7c478bd9Sstevel@tonic-gate * 78*7c478bd9Sstevel@tonic-gate * In addition, each pool contains a doubly-linked list of transports, 79*7c478bd9Sstevel@tonic-gate * an `xprt-ready' queue and a creator thread (see below). Threads in 80*7c478bd9Sstevel@tonic-gate * the pool share some other parameters such as stack size and 81*7c478bd9Sstevel@tonic-gate * polling timeout. 82*7c478bd9Sstevel@tonic-gate * 83*7c478bd9Sstevel@tonic-gate * Pools are initialized through the svc_pool_create() function called from 84*7c478bd9Sstevel@tonic-gate * the nfssys() system call. However, thread creation must be done by 85*7c478bd9Sstevel@tonic-gate * the userland agent. This is done by using SVCPOOL_WAIT and 86*7c478bd9Sstevel@tonic-gate * SVCPOOL_RUN arguments to nfssys(), which call svc_wait() and 87*7c478bd9Sstevel@tonic-gate * svc_do_run(), respectively. Once the pool has been initialized, 88*7c478bd9Sstevel@tonic-gate * the userland process must set up a 'creator' thread. This thread 89*7c478bd9Sstevel@tonic-gate * should park itself in the kernel by calling svc_wait(). If 90*7c478bd9Sstevel@tonic-gate * svc_wait() returns successfully, it should fork off a new worker 91*7c478bd9Sstevel@tonic-gate * thread, which then calls svc_do_run() in order to get work. When 92*7c478bd9Sstevel@tonic-gate * that thread is complete, svc_do_run() will return, and the user 93*7c478bd9Sstevel@tonic-gate * program should call thr_exit(). 94*7c478bd9Sstevel@tonic-gate * 95*7c478bd9Sstevel@tonic-gate * When we try to register a new pool and there is an old pool with 96*7c478bd9Sstevel@tonic-gate * the same id in the doubly linked pool list (this happens when we kill 97*7c478bd9Sstevel@tonic-gate * and restart nfsd or lockd), then we unlink the old pool from the list 98*7c478bd9Sstevel@tonic-gate * and mark its state as `closing'. After that the transports can still 99*7c478bd9Sstevel@tonic-gate * process requests but new transports won't be registered. When all the 100*7c478bd9Sstevel@tonic-gate * transports and service threads associated with the pool are gone the 101*7c478bd9Sstevel@tonic-gate * creator thread (see below) will clean up the pool structure and exit. 102*7c478bd9Sstevel@tonic-gate * 103*7c478bd9Sstevel@tonic-gate * svc_queuereq() and svc_run(). 104*7c478bd9Sstevel@tonic-gate * The kernel RPC server is interrupt driven. The svc_queuereq() interrupt 105*7c478bd9Sstevel@tonic-gate * routine is called to deliver an RPC request. The service threads 106*7c478bd9Sstevel@tonic-gate * loop in svc_run(). The interrupt function queues a request on the 107*7c478bd9Sstevel@tonic-gate * transport's queue and it makes sure that the request is serviced. 108*7c478bd9Sstevel@tonic-gate * It may either wake up one of sleeping threads, or ask for a new thread 109*7c478bd9Sstevel@tonic-gate * to be created, or, if the previous request is just being picked up, do 110*7c478bd9Sstevel@tonic-gate * nothing. In the last case the service thread that is picking up the 111*7c478bd9Sstevel@tonic-gate * previous request will wake up or create the next thread. After a service 112*7c478bd9Sstevel@tonic-gate * thread processes a request and sends a reply it returns to svc_run() 113*7c478bd9Sstevel@tonic-gate * and svc_run() calls svc_poll() to find new input. 114*7c478bd9Sstevel@tonic-gate * 115*7c478bd9Sstevel@tonic-gate * There is an "inconsistent" but "safe" optimization in the 116*7c478bd9Sstevel@tonic-gate * svc_queuereq() code. The request is queued under the transport's 117*7c478bd9Sstevel@tonic-gate * request lock, while the `pending-requests' count is incremented 118*7c478bd9Sstevel@tonic-gate * independently under the pool request lock. Thus, a request can be picked 119*7c478bd9Sstevel@tonic-gate * up by a service thread before the counter is incremented. It may also 120*7c478bd9Sstevel@tonic-gate * happen that the service thread will win the race condition on the pool 121*7c478bd9Sstevel@tonic-gate * lock and it will decrement the count even before the interrupt thread 122*7c478bd9Sstevel@tonic-gate * increments it (so the count can be temporarily negative). 123*7c478bd9Sstevel@tonic-gate * 124*7c478bd9Sstevel@tonic-gate * svc_poll(). 125*7c478bd9Sstevel@tonic-gate * In order to avoid unnecessary locking, which causes performance 126*7c478bd9Sstevel@tonic-gate * problems, we always look for a pending request on the current transport. 127*7c478bd9Sstevel@tonic-gate * If there is none we take a hint from the pool's `xprt-ready' queue. 128*7c478bd9Sstevel@tonic-gate * If the queue had an overflow we switch to the `drain' mode checking 129*7c478bd9Sstevel@tonic-gate * each transport in the pool's transport list. Once we find a 130*7c478bd9Sstevel@tonic-gate * master transport handle with a pending request we latch the request 131*7c478bd9Sstevel@tonic-gate * lock on this transport and return to svc_run(). If the request 132*7c478bd9Sstevel@tonic-gate * belongs to a transport different than the one the service thread is 133*7c478bd9Sstevel@tonic-gate * linked to we need to unlink and link again. 134*7c478bd9Sstevel@tonic-gate * 135*7c478bd9Sstevel@tonic-gate * A service thread goes asleep when there are no pending 136*7c478bd9Sstevel@tonic-gate * requests on the transports registered on the pool's transports. 137*7c478bd9Sstevel@tonic-gate * All the pool's threads sleep on the same condition variable. 138*7c478bd9Sstevel@tonic-gate * If a thread has been sleeping for too long period of time 139*7c478bd9Sstevel@tonic-gate * (by default 5 seconds) it wakes up and exits. Also when a transport 140*7c478bd9Sstevel@tonic-gate * is closing sleeping threads wake up to unlink from this transport. 141*7c478bd9Sstevel@tonic-gate * 142*7c478bd9Sstevel@tonic-gate * The `xprt-ready' queue. 143*7c478bd9Sstevel@tonic-gate * If a service thread finds no request on a transport it is currently linked 144*7c478bd9Sstevel@tonic-gate * to it will find another transport with a pending request. To make 145*7c478bd9Sstevel@tonic-gate * this search more efficient each pool has an `xprt-ready' queue. 146*7c478bd9Sstevel@tonic-gate * The queue is a FIFO. When the interrupt routine queues a request it also 147*7c478bd9Sstevel@tonic-gate * inserts a pointer to the transport into the `xprt-ready' queue. A 148*7c478bd9Sstevel@tonic-gate * thread looking for a transport with a pending request can pop up a 149*7c478bd9Sstevel@tonic-gate * transport and check for a request. The request can be already gone 150*7c478bd9Sstevel@tonic-gate * since it could be taken by a thread linked to that transport. In such a 151*7c478bd9Sstevel@tonic-gate * case we try the next hint. The `xprt-ready' queue has fixed size (by 152*7c478bd9Sstevel@tonic-gate * default 256 nodes). If it overflows svc_poll() has to switch to the 153*7c478bd9Sstevel@tonic-gate * less efficient but safe `drain' mode and walk through the pool's 154*7c478bd9Sstevel@tonic-gate * transport list. 155*7c478bd9Sstevel@tonic-gate * 156*7c478bd9Sstevel@tonic-gate * Both the svc_poll() loop and the `xprt-ready' queue are optimized 157*7c478bd9Sstevel@tonic-gate * for the peak load case that is for the situation when the queue is not 158*7c478bd9Sstevel@tonic-gate * empty, there are all the time few pending requests, and a service 159*7c478bd9Sstevel@tonic-gate * thread which has just processed a request does not go asleep but picks 160*7c478bd9Sstevel@tonic-gate * up immediately the next request. 161*7c478bd9Sstevel@tonic-gate * 162*7c478bd9Sstevel@tonic-gate * Thread creator. 163*7c478bd9Sstevel@tonic-gate * Each pool has a thread creator associated with it. The creator thread 164*7c478bd9Sstevel@tonic-gate * sleeps on a condition variable and waits for a signal to create a 165*7c478bd9Sstevel@tonic-gate * service thread. The actual thread creation is done in userland by 166*7c478bd9Sstevel@tonic-gate * the method described in "Pools" above. 167*7c478bd9Sstevel@tonic-gate * 168*7c478bd9Sstevel@tonic-gate * Signaling threads should turn on the `creator signaled' flag, and 169*7c478bd9Sstevel@tonic-gate * can avoid sending signals when the flag is on. The flag is cleared 170*7c478bd9Sstevel@tonic-gate * when the thread is created. 171*7c478bd9Sstevel@tonic-gate * 172*7c478bd9Sstevel@tonic-gate * When the pool is in closing state (ie it has been already unregistered 173*7c478bd9Sstevel@tonic-gate * from the pool list) the last thread on the last transport in the pool 174*7c478bd9Sstevel@tonic-gate * should turn the p_creator_exit flag on. The creator thread will 175*7c478bd9Sstevel@tonic-gate * clean up the pool structure and exit. 176*7c478bd9Sstevel@tonic-gate * 177*7c478bd9Sstevel@tonic-gate * Thread reservation; Detaching service threads. 178*7c478bd9Sstevel@tonic-gate * A service thread can detach itself to block for an extended amount 179*7c478bd9Sstevel@tonic-gate * of time. However, to keep the service active we need to guarantee 180*7c478bd9Sstevel@tonic-gate * at least pool->p_redline non-detached threads that can process incoming 181*7c478bd9Sstevel@tonic-gate * requests. This, the maximum number of detached and reserved threads is 182*7c478bd9Sstevel@tonic-gate * p->p_maxthreads - p->p_redline. A service thread should first acquire 183*7c478bd9Sstevel@tonic-gate * a reservation, and if the reservation was granted it can detach itself. 184*7c478bd9Sstevel@tonic-gate * If a reservation was granted but the thread does not detach itself 185*7c478bd9Sstevel@tonic-gate * it should cancel the reservation before it returns to svc_run(). 186*7c478bd9Sstevel@tonic-gate */ 187*7c478bd9Sstevel@tonic-gate 188*7c478bd9Sstevel@tonic-gate #include <sys/param.h> 189*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 190*7c478bd9Sstevel@tonic-gate #include <rpc/types.h> 191*7c478bd9Sstevel@tonic-gate #include <sys/socket.h> 192*7c478bd9Sstevel@tonic-gate #include <sys/time.h> 193*7c478bd9Sstevel@tonic-gate #include <sys/tiuser.h> 194*7c478bd9Sstevel@tonic-gate #include <sys/t_kuser.h> 195*7c478bd9Sstevel@tonic-gate #include <netinet/in.h> 196*7c478bd9Sstevel@tonic-gate #include <rpc/xdr.h> 197*7c478bd9Sstevel@tonic-gate #include <rpc/auth.h> 198*7c478bd9Sstevel@tonic-gate #include <rpc/clnt.h> 199*7c478bd9Sstevel@tonic-gate #include <rpc/rpc_msg.h> 200*7c478bd9Sstevel@tonic-gate #include <rpc/svc.h> 201*7c478bd9Sstevel@tonic-gate #include <sys/proc.h> 202*7c478bd9Sstevel@tonic-gate #include <sys/user.h> 203*7c478bd9Sstevel@tonic-gate #include <sys/stream.h> 204*7c478bd9Sstevel@tonic-gate #include <sys/strsubr.h> 205*7c478bd9Sstevel@tonic-gate #include <sys/tihdr.h> 206*7c478bd9Sstevel@tonic-gate #include <sys/debug.h> 207*7c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h> 208*7c478bd9Sstevel@tonic-gate #include <sys/file.h> 209*7c478bd9Sstevel@tonic-gate #include <sys/systm.h> 210*7c478bd9Sstevel@tonic-gate #include <sys/callb.h> 211*7c478bd9Sstevel@tonic-gate #include <sys/vtrace.h> 212*7c478bd9Sstevel@tonic-gate #include <sys/zone.h> 213*7c478bd9Sstevel@tonic-gate #include <nfs/nfs.h> 214*7c478bd9Sstevel@tonic-gate 215*7c478bd9Sstevel@tonic-gate #define RQCRED_SIZE 400 /* this size is excessive */ 216*7c478bd9Sstevel@tonic-gate 217*7c478bd9Sstevel@tonic-gate /* 218*7c478bd9Sstevel@tonic-gate * Defines for svc_poll() 219*7c478bd9Sstevel@tonic-gate */ 220*7c478bd9Sstevel@tonic-gate #define SVC_EXPRTGONE ((SVCMASTERXPRT *)1) /* Transport is closing */ 221*7c478bd9Sstevel@tonic-gate #define SVC_ETIMEDOUT ((SVCMASTERXPRT *)2) /* Timeout */ 222*7c478bd9Sstevel@tonic-gate #define SVC_EINTR ((SVCMASTERXPRT *)3) /* Interrupted by signal */ 223*7c478bd9Sstevel@tonic-gate 224*7c478bd9Sstevel@tonic-gate /* 225*7c478bd9Sstevel@tonic-gate * Default stack size for service threads. 226*7c478bd9Sstevel@tonic-gate */ 227*7c478bd9Sstevel@tonic-gate #define DEFAULT_SVC_RUN_STKSIZE (0) /* default kernel stack */ 228*7c478bd9Sstevel@tonic-gate 229*7c478bd9Sstevel@tonic-gate int svc_default_stksize = DEFAULT_SVC_RUN_STKSIZE; 230*7c478bd9Sstevel@tonic-gate 231*7c478bd9Sstevel@tonic-gate /* 232*7c478bd9Sstevel@tonic-gate * Default polling timeout for service threads. 233*7c478bd9Sstevel@tonic-gate * Multiplied by hz when used. 234*7c478bd9Sstevel@tonic-gate */ 235*7c478bd9Sstevel@tonic-gate #define DEFAULT_SVC_POLL_TIMEOUT (5) /* seconds */ 236*7c478bd9Sstevel@tonic-gate 237*7c478bd9Sstevel@tonic-gate clock_t svc_default_timeout = DEFAULT_SVC_POLL_TIMEOUT; 238*7c478bd9Sstevel@tonic-gate 239*7c478bd9Sstevel@tonic-gate /* 240*7c478bd9Sstevel@tonic-gate * Size of the `xprt-ready' queue. 241*7c478bd9Sstevel@tonic-gate */ 242*7c478bd9Sstevel@tonic-gate #define DEFAULT_SVC_QSIZE (256) /* qnodes */ 243*7c478bd9Sstevel@tonic-gate 244*7c478bd9Sstevel@tonic-gate size_t svc_default_qsize = DEFAULT_SVC_QSIZE; 245*7c478bd9Sstevel@tonic-gate 246*7c478bd9Sstevel@tonic-gate /* 247*7c478bd9Sstevel@tonic-gate * Default limit for the number of service threads. 248*7c478bd9Sstevel@tonic-gate */ 249*7c478bd9Sstevel@tonic-gate #define DEFAULT_SVC_MAXTHREADS (INT16_MAX) 250*7c478bd9Sstevel@tonic-gate 251*7c478bd9Sstevel@tonic-gate int svc_default_maxthreads = DEFAULT_SVC_MAXTHREADS; 252*7c478bd9Sstevel@tonic-gate 253*7c478bd9Sstevel@tonic-gate /* 254*7c478bd9Sstevel@tonic-gate * Maximum number of requests from the same transport (in `drain' mode). 255*7c478bd9Sstevel@tonic-gate */ 256*7c478bd9Sstevel@tonic-gate #define DEFAULT_SVC_MAX_SAME_XPRT (8) 257*7c478bd9Sstevel@tonic-gate 258*7c478bd9Sstevel@tonic-gate int svc_default_max_same_xprt = DEFAULT_SVC_MAX_SAME_XPRT; 259*7c478bd9Sstevel@tonic-gate 260*7c478bd9Sstevel@tonic-gate 261*7c478bd9Sstevel@tonic-gate /* 262*7c478bd9Sstevel@tonic-gate * Default `Redline' of non-detached threads. 263*7c478bd9Sstevel@tonic-gate * Total number of detached and reserved threads in an RPC server 264*7c478bd9Sstevel@tonic-gate * thread pool is limited to pool->p_maxthreads - svc_redline. 265*7c478bd9Sstevel@tonic-gate */ 266*7c478bd9Sstevel@tonic-gate #define DEFAULT_SVC_REDLINE (1) 267*7c478bd9Sstevel@tonic-gate 268*7c478bd9Sstevel@tonic-gate int svc_default_redline = DEFAULT_SVC_REDLINE; 269*7c478bd9Sstevel@tonic-gate 270*7c478bd9Sstevel@tonic-gate /* 271*7c478bd9Sstevel@tonic-gate * A node for the `xprt-ready' queue. 272*7c478bd9Sstevel@tonic-gate * See below. 273*7c478bd9Sstevel@tonic-gate */ 274*7c478bd9Sstevel@tonic-gate struct __svcxprt_qnode { 275*7c478bd9Sstevel@tonic-gate __SVCXPRT_QNODE *q_next; 276*7c478bd9Sstevel@tonic-gate SVCMASTERXPRT *q_xprt; 277*7c478bd9Sstevel@tonic-gate }; 278*7c478bd9Sstevel@tonic-gate 279*7c478bd9Sstevel@tonic-gate /* 280*7c478bd9Sstevel@tonic-gate * Global SVC variables (private). 281*7c478bd9Sstevel@tonic-gate */ 282*7c478bd9Sstevel@tonic-gate struct svc_globals { 283*7c478bd9Sstevel@tonic-gate SVCPOOL *svc_pools; 284*7c478bd9Sstevel@tonic-gate kmutex_t svc_plock; 285*7c478bd9Sstevel@tonic-gate }; 286*7c478bd9Sstevel@tonic-gate 287*7c478bd9Sstevel@tonic-gate /* 288*7c478bd9Sstevel@tonic-gate * Debug variable to check for rdma based 289*7c478bd9Sstevel@tonic-gate * transport startup and cleanup. Contorlled 290*7c478bd9Sstevel@tonic-gate * through /etc/system. Off by default. 291*7c478bd9Sstevel@tonic-gate */ 292*7c478bd9Sstevel@tonic-gate int rdma_check = 0; 293*7c478bd9Sstevel@tonic-gate 294*7c478bd9Sstevel@tonic-gate /* 295*7c478bd9Sstevel@tonic-gate * Authentication parameters list. 296*7c478bd9Sstevel@tonic-gate */ 297*7c478bd9Sstevel@tonic-gate static caddr_t rqcred_head; 298*7c478bd9Sstevel@tonic-gate static kmutex_t rqcred_lock; 299*7c478bd9Sstevel@tonic-gate 300*7c478bd9Sstevel@tonic-gate /* 301*7c478bd9Sstevel@tonic-gate * Pointers to transport specific `rele' routines in rpcmod (set from rpcmod). 302*7c478bd9Sstevel@tonic-gate */ 303*7c478bd9Sstevel@tonic-gate void (*rpc_rele)(queue_t *, mblk_t *) = NULL; 304*7c478bd9Sstevel@tonic-gate void (*mir_rele)(queue_t *, mblk_t *) = NULL; 305*7c478bd9Sstevel@tonic-gate 306*7c478bd9Sstevel@tonic-gate /* ARGSUSED */ 307*7c478bd9Sstevel@tonic-gate void 308*7c478bd9Sstevel@tonic-gate rpc_rdma_rele(queue_t *q, mblk_t *mp) 309*7c478bd9Sstevel@tonic-gate { 310*7c478bd9Sstevel@tonic-gate } 311*7c478bd9Sstevel@tonic-gate void (*rdma_rele)(queue_t *, mblk_t *) = rpc_rdma_rele; 312*7c478bd9Sstevel@tonic-gate 313*7c478bd9Sstevel@tonic-gate 314*7c478bd9Sstevel@tonic-gate /* 315*7c478bd9Sstevel@tonic-gate * This macro picks which `rele' routine to use, based on the transport type. 316*7c478bd9Sstevel@tonic-gate */ 317*7c478bd9Sstevel@tonic-gate #define RELE_PROC(xprt) \ 318*7c478bd9Sstevel@tonic-gate ((xprt)->xp_type == T_RDMA ? rdma_rele : \ 319*7c478bd9Sstevel@tonic-gate (((xprt)->xp_type == T_CLTS) ? rpc_rele : mir_rele)) 320*7c478bd9Sstevel@tonic-gate 321*7c478bd9Sstevel@tonic-gate /* 322*7c478bd9Sstevel@tonic-gate * If true, then keep quiet about version mismatch. 323*7c478bd9Sstevel@tonic-gate * This macro is for broadcast RPC only. We have no broadcast RPC in 324*7c478bd9Sstevel@tonic-gate * kernel now but one may define a flag in the transport structure 325*7c478bd9Sstevel@tonic-gate * and redefine this macro. 326*7c478bd9Sstevel@tonic-gate */ 327*7c478bd9Sstevel@tonic-gate #define version_keepquiet(xprt) (FALSE) 328*7c478bd9Sstevel@tonic-gate 329*7c478bd9Sstevel@tonic-gate /* 330*7c478bd9Sstevel@tonic-gate * ZSD key used to retrieve zone-specific svc globals 331*7c478bd9Sstevel@tonic-gate */ 332*7c478bd9Sstevel@tonic-gate static zone_key_t svc_zone_key; 333*7c478bd9Sstevel@tonic-gate 334*7c478bd9Sstevel@tonic-gate static void svc_callout_free(SVCMASTERXPRT *); 335*7c478bd9Sstevel@tonic-gate static void svc_xprt_qinit(SVCPOOL *, size_t); 336*7c478bd9Sstevel@tonic-gate static void svc_xprt_qdestroy(SVCPOOL *); 337*7c478bd9Sstevel@tonic-gate static void svc_thread_creator(SVCPOOL *); 338*7c478bd9Sstevel@tonic-gate static void svc_creator_signal(SVCPOOL *); 339*7c478bd9Sstevel@tonic-gate static void svc_creator_signalexit(SVCPOOL *); 340*7c478bd9Sstevel@tonic-gate static void svc_pool_unregister(struct svc_globals *, SVCPOOL *); 341*7c478bd9Sstevel@tonic-gate static int svc_run(SVCPOOL *); 342*7c478bd9Sstevel@tonic-gate 343*7c478bd9Sstevel@tonic-gate /* ARGSUSED */ 344*7c478bd9Sstevel@tonic-gate static void * 345*7c478bd9Sstevel@tonic-gate svc_zoneinit(zoneid_t zoneid) 346*7c478bd9Sstevel@tonic-gate { 347*7c478bd9Sstevel@tonic-gate struct svc_globals *svc; 348*7c478bd9Sstevel@tonic-gate 349*7c478bd9Sstevel@tonic-gate svc = kmem_alloc(sizeof (*svc), KM_SLEEP); 350*7c478bd9Sstevel@tonic-gate mutex_init(&svc->svc_plock, NULL, MUTEX_DEFAULT, NULL); 351*7c478bd9Sstevel@tonic-gate svc->svc_pools = NULL; 352*7c478bd9Sstevel@tonic-gate return (svc); 353*7c478bd9Sstevel@tonic-gate } 354*7c478bd9Sstevel@tonic-gate 355*7c478bd9Sstevel@tonic-gate /* ARGSUSED */ 356*7c478bd9Sstevel@tonic-gate static void 357*7c478bd9Sstevel@tonic-gate svc_zoneshutdown(zoneid_t zoneid, void *arg) 358*7c478bd9Sstevel@tonic-gate { 359*7c478bd9Sstevel@tonic-gate struct svc_globals *svc = arg; 360*7c478bd9Sstevel@tonic-gate SVCPOOL *pool; 361*7c478bd9Sstevel@tonic-gate 362*7c478bd9Sstevel@tonic-gate mutex_enter(&svc->svc_plock); 363*7c478bd9Sstevel@tonic-gate while ((pool = svc->svc_pools) != NULL) { 364*7c478bd9Sstevel@tonic-gate svc_pool_unregister(svc, pool); 365*7c478bd9Sstevel@tonic-gate } 366*7c478bd9Sstevel@tonic-gate mutex_exit(&svc->svc_plock); 367*7c478bd9Sstevel@tonic-gate } 368*7c478bd9Sstevel@tonic-gate 369*7c478bd9Sstevel@tonic-gate /* ARGSUSED */ 370*7c478bd9Sstevel@tonic-gate static void 371*7c478bd9Sstevel@tonic-gate svc_zonefini(zoneid_t zoneid, void *arg) 372*7c478bd9Sstevel@tonic-gate { 373*7c478bd9Sstevel@tonic-gate struct svc_globals *svc = arg; 374*7c478bd9Sstevel@tonic-gate 375*7c478bd9Sstevel@tonic-gate ASSERT(svc->svc_pools == NULL); 376*7c478bd9Sstevel@tonic-gate mutex_destroy(&svc->svc_plock); 377*7c478bd9Sstevel@tonic-gate kmem_free(svc, sizeof (*svc)); 378*7c478bd9Sstevel@tonic-gate } 379*7c478bd9Sstevel@tonic-gate 380*7c478bd9Sstevel@tonic-gate /* 381*7c478bd9Sstevel@tonic-gate * Global SVC init routine. 382*7c478bd9Sstevel@tonic-gate * Initialize global generic and transport type specific structures 383*7c478bd9Sstevel@tonic-gate * used by the kernel RPC server side. This routine is called only 384*7c478bd9Sstevel@tonic-gate * once when the module is being loaded. 385*7c478bd9Sstevel@tonic-gate */ 386*7c478bd9Sstevel@tonic-gate void 387*7c478bd9Sstevel@tonic-gate svc_init() 388*7c478bd9Sstevel@tonic-gate { 389*7c478bd9Sstevel@tonic-gate zone_key_create(&svc_zone_key, svc_zoneinit, svc_zoneshutdown, 390*7c478bd9Sstevel@tonic-gate svc_zonefini); 391*7c478bd9Sstevel@tonic-gate svc_cots_init(); 392*7c478bd9Sstevel@tonic-gate svc_clts_init(); 393*7c478bd9Sstevel@tonic-gate } 394*7c478bd9Sstevel@tonic-gate 395*7c478bd9Sstevel@tonic-gate /* 396*7c478bd9Sstevel@tonic-gate * Destroy the SVCPOOL structure. 397*7c478bd9Sstevel@tonic-gate */ 398*7c478bd9Sstevel@tonic-gate static void 399*7c478bd9Sstevel@tonic-gate svc_pool_cleanup(SVCPOOL *pool) 400*7c478bd9Sstevel@tonic-gate { 401*7c478bd9Sstevel@tonic-gate ASSERT(pool->p_threads + pool->p_detached_threads == 0); 402*7c478bd9Sstevel@tonic-gate ASSERT(pool->p_lcount == 0); 403*7c478bd9Sstevel@tonic-gate ASSERT(pool->p_closing); 404*7c478bd9Sstevel@tonic-gate 405*7c478bd9Sstevel@tonic-gate /* 406*7c478bd9Sstevel@tonic-gate * Call the user supplied shutdown function. This is done 407*7c478bd9Sstevel@tonic-gate * here so the user of the pool will be able to cleanup 408*7c478bd9Sstevel@tonic-gate * service related resources. 409*7c478bd9Sstevel@tonic-gate */ 410*7c478bd9Sstevel@tonic-gate if (pool->p_shutdown != NULL) 411*7c478bd9Sstevel@tonic-gate (pool->p_shutdown)(); 412*7c478bd9Sstevel@tonic-gate 413*7c478bd9Sstevel@tonic-gate /* Destroy `xprt-ready' queue */ 414*7c478bd9Sstevel@tonic-gate svc_xprt_qdestroy(pool); 415*7c478bd9Sstevel@tonic-gate 416*7c478bd9Sstevel@tonic-gate /* Destroy transport list */ 417*7c478bd9Sstevel@tonic-gate rw_destroy(&pool->p_lrwlock); 418*7c478bd9Sstevel@tonic-gate 419*7c478bd9Sstevel@tonic-gate /* Destroy locks and condition variables */ 420*7c478bd9Sstevel@tonic-gate mutex_destroy(&pool->p_thread_lock); 421*7c478bd9Sstevel@tonic-gate mutex_destroy(&pool->p_req_lock); 422*7c478bd9Sstevel@tonic-gate cv_destroy(&pool->p_req_cv); 423*7c478bd9Sstevel@tonic-gate 424*7c478bd9Sstevel@tonic-gate /* Destroy creator's locks and condition variables */ 425*7c478bd9Sstevel@tonic-gate mutex_destroy(&pool->p_creator_lock); 426*7c478bd9Sstevel@tonic-gate cv_destroy(&pool->p_creator_cv); 427*7c478bd9Sstevel@tonic-gate mutex_destroy(&pool->p_user_lock); 428*7c478bd9Sstevel@tonic-gate cv_destroy(&pool->p_user_cv); 429*7c478bd9Sstevel@tonic-gate 430*7c478bd9Sstevel@tonic-gate /* Free pool structure */ 431*7c478bd9Sstevel@tonic-gate kmem_free(pool, sizeof (SVCPOOL)); 432*7c478bd9Sstevel@tonic-gate } 433*7c478bd9Sstevel@tonic-gate 434*7c478bd9Sstevel@tonic-gate /* 435*7c478bd9Sstevel@tonic-gate * If all the transports and service threads are already gone 436*7c478bd9Sstevel@tonic-gate * signal the creator thread to clean up and exit. 437*7c478bd9Sstevel@tonic-gate */ 438*7c478bd9Sstevel@tonic-gate static bool_t 439*7c478bd9Sstevel@tonic-gate svc_pool_tryexit(SVCPOOL *pool) 440*7c478bd9Sstevel@tonic-gate { 441*7c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&pool->p_thread_lock)); 442*7c478bd9Sstevel@tonic-gate ASSERT(pool->p_closing); 443*7c478bd9Sstevel@tonic-gate 444*7c478bd9Sstevel@tonic-gate if (pool->p_threads + pool->p_detached_threads == 0) { 445*7c478bd9Sstevel@tonic-gate rw_enter(&pool->p_lrwlock, RW_READER); 446*7c478bd9Sstevel@tonic-gate if (pool->p_lcount == 0) { 447*7c478bd9Sstevel@tonic-gate /* 448*7c478bd9Sstevel@tonic-gate * Release the locks before sending a signal. 449*7c478bd9Sstevel@tonic-gate */ 450*7c478bd9Sstevel@tonic-gate rw_exit(&pool->p_lrwlock); 451*7c478bd9Sstevel@tonic-gate mutex_exit(&pool->p_thread_lock); 452*7c478bd9Sstevel@tonic-gate 453*7c478bd9Sstevel@tonic-gate /* 454*7c478bd9Sstevel@tonic-gate * Notify the creator thread to clean up and exit 455*7c478bd9Sstevel@tonic-gate * 456*7c478bd9Sstevel@tonic-gate * NOTICE: No references to the pool beyond this point! 457*7c478bd9Sstevel@tonic-gate * The pool is being destroyed. 458*7c478bd9Sstevel@tonic-gate */ 459*7c478bd9Sstevel@tonic-gate ASSERT(!MUTEX_HELD(&pool->p_thread_lock)); 460*7c478bd9Sstevel@tonic-gate svc_creator_signalexit(pool); 461*7c478bd9Sstevel@tonic-gate 462*7c478bd9Sstevel@tonic-gate return (TRUE); 463*7c478bd9Sstevel@tonic-gate } 464*7c478bd9Sstevel@tonic-gate rw_exit(&pool->p_lrwlock); 465*7c478bd9Sstevel@tonic-gate } 466*7c478bd9Sstevel@tonic-gate 467*7c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&pool->p_thread_lock)); 468*7c478bd9Sstevel@tonic-gate return (FALSE); 469*7c478bd9Sstevel@tonic-gate } 470*7c478bd9Sstevel@tonic-gate 471*7c478bd9Sstevel@tonic-gate /* 472*7c478bd9Sstevel@tonic-gate * Find a pool with a given id. 473*7c478bd9Sstevel@tonic-gate */ 474*7c478bd9Sstevel@tonic-gate static SVCPOOL * 475*7c478bd9Sstevel@tonic-gate svc_pool_find(struct svc_globals *svc, int id) 476*7c478bd9Sstevel@tonic-gate { 477*7c478bd9Sstevel@tonic-gate SVCPOOL *pool; 478*7c478bd9Sstevel@tonic-gate 479*7c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&svc->svc_plock)); 480*7c478bd9Sstevel@tonic-gate 481*7c478bd9Sstevel@tonic-gate /* 482*7c478bd9Sstevel@tonic-gate * Search the list for a pool with a matching id 483*7c478bd9Sstevel@tonic-gate * and register the transport handle with that pool. 484*7c478bd9Sstevel@tonic-gate */ 485*7c478bd9Sstevel@tonic-gate for (pool = svc->svc_pools; pool; pool = pool->p_next) 486*7c478bd9Sstevel@tonic-gate if (pool->p_id == id) 487*7c478bd9Sstevel@tonic-gate return (pool); 488*7c478bd9Sstevel@tonic-gate 489*7c478bd9Sstevel@tonic-gate return (NULL); 490*7c478bd9Sstevel@tonic-gate } 491*7c478bd9Sstevel@tonic-gate 492*7c478bd9Sstevel@tonic-gate /* 493*7c478bd9Sstevel@tonic-gate * PSARC 2003/523 Contract Private Interface 494*7c478bd9Sstevel@tonic-gate * svc_do_run 495*7c478bd9Sstevel@tonic-gate * Changes must be reviewed by Solaris File Sharing 496*7c478bd9Sstevel@tonic-gate * Changes must be communicated to contract-2003-523@sun.com 497*7c478bd9Sstevel@tonic-gate */ 498*7c478bd9Sstevel@tonic-gate int 499*7c478bd9Sstevel@tonic-gate svc_do_run(int id) 500*7c478bd9Sstevel@tonic-gate { 501*7c478bd9Sstevel@tonic-gate SVCPOOL *pool; 502*7c478bd9Sstevel@tonic-gate int err = 0; 503*7c478bd9Sstevel@tonic-gate struct svc_globals *svc; 504*7c478bd9Sstevel@tonic-gate 505*7c478bd9Sstevel@tonic-gate svc = zone_getspecific(svc_zone_key, curproc->p_zone); 506*7c478bd9Sstevel@tonic-gate mutex_enter(&svc->svc_plock); 507*7c478bd9Sstevel@tonic-gate 508*7c478bd9Sstevel@tonic-gate pool = svc_pool_find(svc, id); 509*7c478bd9Sstevel@tonic-gate 510*7c478bd9Sstevel@tonic-gate mutex_exit(&svc->svc_plock); 511*7c478bd9Sstevel@tonic-gate 512*7c478bd9Sstevel@tonic-gate if (pool == NULL) 513*7c478bd9Sstevel@tonic-gate return (ENOENT); 514*7c478bd9Sstevel@tonic-gate 515*7c478bd9Sstevel@tonic-gate /* 516*7c478bd9Sstevel@tonic-gate * Increment counter of pool threads now 517*7c478bd9Sstevel@tonic-gate * that a thread has been created. 518*7c478bd9Sstevel@tonic-gate */ 519*7c478bd9Sstevel@tonic-gate mutex_enter(&pool->p_thread_lock); 520*7c478bd9Sstevel@tonic-gate pool->p_threads++; 521*7c478bd9Sstevel@tonic-gate mutex_exit(&pool->p_thread_lock); 522*7c478bd9Sstevel@tonic-gate 523*7c478bd9Sstevel@tonic-gate /* Give work to the new thread. */ 524*7c478bd9Sstevel@tonic-gate err = svc_run(pool); 525*7c478bd9Sstevel@tonic-gate 526*7c478bd9Sstevel@tonic-gate return (err); 527*7c478bd9Sstevel@tonic-gate } 528*7c478bd9Sstevel@tonic-gate 529*7c478bd9Sstevel@tonic-gate /* 530*7c478bd9Sstevel@tonic-gate * Unregister a pool from the pool list. 531*7c478bd9Sstevel@tonic-gate * Set the closing state. If all the transports and service threads 532*7c478bd9Sstevel@tonic-gate * are already gone signal the creator thread to clean up and exit. 533*7c478bd9Sstevel@tonic-gate */ 534*7c478bd9Sstevel@tonic-gate static void 535*7c478bd9Sstevel@tonic-gate svc_pool_unregister(struct svc_globals *svc, SVCPOOL *pool) 536*7c478bd9Sstevel@tonic-gate { 537*7c478bd9Sstevel@tonic-gate SVCPOOL *next = pool->p_next; 538*7c478bd9Sstevel@tonic-gate SVCPOOL *prev = pool->p_prev; 539*7c478bd9Sstevel@tonic-gate 540*7c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&svc->svc_plock)); 541*7c478bd9Sstevel@tonic-gate 542*7c478bd9Sstevel@tonic-gate /* Remove from the list */ 543*7c478bd9Sstevel@tonic-gate if (pool == svc->svc_pools) 544*7c478bd9Sstevel@tonic-gate svc->svc_pools = next; 545*7c478bd9Sstevel@tonic-gate if (next) 546*7c478bd9Sstevel@tonic-gate next->p_prev = prev; 547*7c478bd9Sstevel@tonic-gate if (prev) 548*7c478bd9Sstevel@tonic-gate prev->p_next = next; 549*7c478bd9Sstevel@tonic-gate pool->p_next = pool->p_prev = NULL; 550*7c478bd9Sstevel@tonic-gate 551*7c478bd9Sstevel@tonic-gate /* 552*7c478bd9Sstevel@tonic-gate * Offline the pool. Mark the pool as closing. 553*7c478bd9Sstevel@tonic-gate * If there are no transports in this pool notify 554*7c478bd9Sstevel@tonic-gate * the creator thread to clean it up and exit. 555*7c478bd9Sstevel@tonic-gate */ 556*7c478bd9Sstevel@tonic-gate mutex_enter(&pool->p_thread_lock); 557*7c478bd9Sstevel@tonic-gate if (pool->p_offline != NULL) 558*7c478bd9Sstevel@tonic-gate (pool->p_offline)(); 559*7c478bd9Sstevel@tonic-gate pool->p_closing = TRUE; 560*7c478bd9Sstevel@tonic-gate if (svc_pool_tryexit(pool)) 561*7c478bd9Sstevel@tonic-gate return; 562*7c478bd9Sstevel@tonic-gate mutex_exit(&pool->p_thread_lock); 563*7c478bd9Sstevel@tonic-gate } 564*7c478bd9Sstevel@tonic-gate 565*7c478bd9Sstevel@tonic-gate /* 566*7c478bd9Sstevel@tonic-gate * Register a pool with a given id in the global doubly linked pool list. 567*7c478bd9Sstevel@tonic-gate * - if there is a pool with the same id in the list then unregister it 568*7c478bd9Sstevel@tonic-gate * - insert the new pool into the list. 569*7c478bd9Sstevel@tonic-gate */ 570*7c478bd9Sstevel@tonic-gate static void 571*7c478bd9Sstevel@tonic-gate svc_pool_register(struct svc_globals *svc, SVCPOOL *pool, int id) 572*7c478bd9Sstevel@tonic-gate { 573*7c478bd9Sstevel@tonic-gate SVCPOOL *old_pool; 574*7c478bd9Sstevel@tonic-gate 575*7c478bd9Sstevel@tonic-gate /* 576*7c478bd9Sstevel@tonic-gate * If there is a pool with the same id then remove it from 577*7c478bd9Sstevel@tonic-gate * the list and mark the pool as closing. 578*7c478bd9Sstevel@tonic-gate */ 579*7c478bd9Sstevel@tonic-gate mutex_enter(&svc->svc_plock); 580*7c478bd9Sstevel@tonic-gate 581*7c478bd9Sstevel@tonic-gate if (old_pool = svc_pool_find(svc, id)) 582*7c478bd9Sstevel@tonic-gate svc_pool_unregister(svc, old_pool); 583*7c478bd9Sstevel@tonic-gate 584*7c478bd9Sstevel@tonic-gate /* Insert into the doubly linked list */ 585*7c478bd9Sstevel@tonic-gate pool->p_id = id; 586*7c478bd9Sstevel@tonic-gate pool->p_next = svc->svc_pools; 587*7c478bd9Sstevel@tonic-gate pool->p_prev = NULL; 588*7c478bd9Sstevel@tonic-gate if (svc->svc_pools) 589*7c478bd9Sstevel@tonic-gate svc->svc_pools->p_prev = pool; 590*7c478bd9Sstevel@tonic-gate svc->svc_pools = pool; 591*7c478bd9Sstevel@tonic-gate 592*7c478bd9Sstevel@tonic-gate mutex_exit(&svc->svc_plock); 593*7c478bd9Sstevel@tonic-gate } 594*7c478bd9Sstevel@tonic-gate 595*7c478bd9Sstevel@tonic-gate /* 596*7c478bd9Sstevel@tonic-gate * Initialize a newly created pool structure 597*7c478bd9Sstevel@tonic-gate */ 598*7c478bd9Sstevel@tonic-gate static int 599*7c478bd9Sstevel@tonic-gate svc_pool_init(SVCPOOL *pool, uint_t maxthreads, uint_t redline, 600*7c478bd9Sstevel@tonic-gate uint_t qsize, uint_t timeout, uint_t stksize, uint_t max_same_xprt) 601*7c478bd9Sstevel@tonic-gate { 602*7c478bd9Sstevel@tonic-gate klwp_t *lwp = ttolwp(curthread); 603*7c478bd9Sstevel@tonic-gate 604*7c478bd9Sstevel@tonic-gate ASSERT(pool); 605*7c478bd9Sstevel@tonic-gate 606*7c478bd9Sstevel@tonic-gate if (maxthreads == 0) 607*7c478bd9Sstevel@tonic-gate maxthreads = svc_default_maxthreads; 608*7c478bd9Sstevel@tonic-gate if (redline == 0) 609*7c478bd9Sstevel@tonic-gate redline = svc_default_redline; 610*7c478bd9Sstevel@tonic-gate if (qsize == 0) 611*7c478bd9Sstevel@tonic-gate qsize = svc_default_qsize; 612*7c478bd9Sstevel@tonic-gate if (timeout == 0) 613*7c478bd9Sstevel@tonic-gate timeout = svc_default_timeout; 614*7c478bd9Sstevel@tonic-gate if (stksize == 0) 615*7c478bd9Sstevel@tonic-gate stksize = svc_default_stksize; 616*7c478bd9Sstevel@tonic-gate if (max_same_xprt == 0) 617*7c478bd9Sstevel@tonic-gate max_same_xprt = svc_default_max_same_xprt; 618*7c478bd9Sstevel@tonic-gate 619*7c478bd9Sstevel@tonic-gate if (maxthreads < redline) 620*7c478bd9Sstevel@tonic-gate return (EINVAL); 621*7c478bd9Sstevel@tonic-gate 622*7c478bd9Sstevel@tonic-gate /* Allocate and initialize the `xprt-ready' queue */ 623*7c478bd9Sstevel@tonic-gate svc_xprt_qinit(pool, qsize); 624*7c478bd9Sstevel@tonic-gate 625*7c478bd9Sstevel@tonic-gate /* Initialize doubly-linked xprt list */ 626*7c478bd9Sstevel@tonic-gate rw_init(&pool->p_lrwlock, NULL, RW_DEFAULT, NULL); 627*7c478bd9Sstevel@tonic-gate 628*7c478bd9Sstevel@tonic-gate /* 629*7c478bd9Sstevel@tonic-gate * Setting lwp_childstksz on the current lwp so that 630*7c478bd9Sstevel@tonic-gate * descendants of this lwp get the modified stacksize, if 631*7c478bd9Sstevel@tonic-gate * it is defined. It is important that either this lwp or 632*7c478bd9Sstevel@tonic-gate * one of its descendants do the actual servicepool thread 633*7c478bd9Sstevel@tonic-gate * creation to maintain the stacksize inheritance. 634*7c478bd9Sstevel@tonic-gate */ 635*7c478bd9Sstevel@tonic-gate if (lwp != NULL) 636*7c478bd9Sstevel@tonic-gate lwp->lwp_childstksz = stksize; 637*7c478bd9Sstevel@tonic-gate 638*7c478bd9Sstevel@tonic-gate /* Initialize thread limits, locks and condition variables */ 639*7c478bd9Sstevel@tonic-gate pool->p_maxthreads = maxthreads; 640*7c478bd9Sstevel@tonic-gate pool->p_redline = redline; 641*7c478bd9Sstevel@tonic-gate pool->p_timeout = timeout * hz; 642*7c478bd9Sstevel@tonic-gate pool->p_stksize = stksize; 643*7c478bd9Sstevel@tonic-gate pool->p_max_same_xprt = max_same_xprt; 644*7c478bd9Sstevel@tonic-gate mutex_init(&pool->p_thread_lock, NULL, MUTEX_DEFAULT, NULL); 645*7c478bd9Sstevel@tonic-gate mutex_init(&pool->p_req_lock, NULL, MUTEX_DEFAULT, NULL); 646*7c478bd9Sstevel@tonic-gate cv_init(&pool->p_req_cv, NULL, CV_DEFAULT, NULL); 647*7c478bd9Sstevel@tonic-gate 648*7c478bd9Sstevel@tonic-gate /* Initialize userland creator */ 649*7c478bd9Sstevel@tonic-gate pool->p_user_exit = FALSE; 650*7c478bd9Sstevel@tonic-gate pool->p_signal_create_thread = FALSE; 651*7c478bd9Sstevel@tonic-gate pool->p_user_waiting = FALSE; 652*7c478bd9Sstevel@tonic-gate mutex_init(&pool->p_user_lock, NULL, MUTEX_DEFAULT, NULL); 653*7c478bd9Sstevel@tonic-gate cv_init(&pool->p_user_cv, NULL, CV_DEFAULT, NULL); 654*7c478bd9Sstevel@tonic-gate 655*7c478bd9Sstevel@tonic-gate /* Initialize the creator and start the creator thread */ 656*7c478bd9Sstevel@tonic-gate pool->p_creator_exit = FALSE; 657*7c478bd9Sstevel@tonic-gate mutex_init(&pool->p_creator_lock, NULL, MUTEX_DEFAULT, NULL); 658*7c478bd9Sstevel@tonic-gate cv_init(&pool->p_creator_cv, NULL, CV_DEFAULT, NULL); 659*7c478bd9Sstevel@tonic-gate 660*7c478bd9Sstevel@tonic-gate (void) zthread_create(NULL, pool->p_stksize, svc_thread_creator, 661*7c478bd9Sstevel@tonic-gate pool, 0, minclsyspri); 662*7c478bd9Sstevel@tonic-gate 663*7c478bd9Sstevel@tonic-gate return (0); 664*7c478bd9Sstevel@tonic-gate } 665*7c478bd9Sstevel@tonic-gate 666*7c478bd9Sstevel@tonic-gate /* 667*7c478bd9Sstevel@tonic-gate * PSARC 2003/523 Contract Private Interface 668*7c478bd9Sstevel@tonic-gate * svc_pool_create 669*7c478bd9Sstevel@tonic-gate * Changes must be reviewed by Solaris File Sharing 670*7c478bd9Sstevel@tonic-gate * Changes must be communicated to contract-2003-523@sun.com 671*7c478bd9Sstevel@tonic-gate * 672*7c478bd9Sstevel@tonic-gate * Create an kernel RPC server-side thread/transport pool. 673*7c478bd9Sstevel@tonic-gate * 674*7c478bd9Sstevel@tonic-gate * This is public interface for creation of a server RPC thread pool 675*7c478bd9Sstevel@tonic-gate * for a given service provider. Transports registered with the pool's id 676*7c478bd9Sstevel@tonic-gate * will be served by a pool's threads. This function is called from the 677*7c478bd9Sstevel@tonic-gate * nfssys() system call. 678*7c478bd9Sstevel@tonic-gate */ 679*7c478bd9Sstevel@tonic-gate int 680*7c478bd9Sstevel@tonic-gate svc_pool_create(struct svcpool_args *args) 681*7c478bd9Sstevel@tonic-gate { 682*7c478bd9Sstevel@tonic-gate SVCPOOL *pool; 683*7c478bd9Sstevel@tonic-gate int error; 684*7c478bd9Sstevel@tonic-gate struct svc_globals *svc; 685*7c478bd9Sstevel@tonic-gate 686*7c478bd9Sstevel@tonic-gate /* 687*7c478bd9Sstevel@tonic-gate * Caller should check credentials in a way appropriate 688*7c478bd9Sstevel@tonic-gate * in the context of the call. 689*7c478bd9Sstevel@tonic-gate */ 690*7c478bd9Sstevel@tonic-gate 691*7c478bd9Sstevel@tonic-gate svc = zone_getspecific(svc_zone_key, curproc->p_zone); 692*7c478bd9Sstevel@tonic-gate /* Allocate a new pool */ 693*7c478bd9Sstevel@tonic-gate pool = kmem_zalloc(sizeof (SVCPOOL), KM_SLEEP); 694*7c478bd9Sstevel@tonic-gate 695*7c478bd9Sstevel@tonic-gate /* 696*7c478bd9Sstevel@tonic-gate * Initialize the pool structure and create a creator thread. 697*7c478bd9Sstevel@tonic-gate */ 698*7c478bd9Sstevel@tonic-gate error = svc_pool_init(pool, args->maxthreads, args->redline, 699*7c478bd9Sstevel@tonic-gate args->qsize, args->timeout, args->stksize, args->max_same_xprt); 700*7c478bd9Sstevel@tonic-gate 701*7c478bd9Sstevel@tonic-gate if (error) { 702*7c478bd9Sstevel@tonic-gate kmem_free(pool, sizeof (SVCPOOL)); 703*7c478bd9Sstevel@tonic-gate return (error); 704*7c478bd9Sstevel@tonic-gate } 705*7c478bd9Sstevel@tonic-gate 706*7c478bd9Sstevel@tonic-gate /* Register the pool with the global pool list */ 707*7c478bd9Sstevel@tonic-gate svc_pool_register(svc, pool, args->id); 708*7c478bd9Sstevel@tonic-gate 709*7c478bd9Sstevel@tonic-gate return (0); 710*7c478bd9Sstevel@tonic-gate } 711*7c478bd9Sstevel@tonic-gate 712*7c478bd9Sstevel@tonic-gate int 713*7c478bd9Sstevel@tonic-gate svc_pool_control(int id, int cmd, void *arg) 714*7c478bd9Sstevel@tonic-gate { 715*7c478bd9Sstevel@tonic-gate SVCPOOL *pool; 716*7c478bd9Sstevel@tonic-gate struct svc_globals *svc; 717*7c478bd9Sstevel@tonic-gate 718*7c478bd9Sstevel@tonic-gate svc = zone_getspecific(svc_zone_key, curproc->p_zone); 719*7c478bd9Sstevel@tonic-gate 720*7c478bd9Sstevel@tonic-gate switch (cmd) { 721*7c478bd9Sstevel@tonic-gate case SVCPSET_SHUTDOWN_PROC: 722*7c478bd9Sstevel@tonic-gate /* 723*7c478bd9Sstevel@tonic-gate * Search the list for a pool with a matching id 724*7c478bd9Sstevel@tonic-gate * and register the transport handle with that pool. 725*7c478bd9Sstevel@tonic-gate */ 726*7c478bd9Sstevel@tonic-gate mutex_enter(&svc->svc_plock); 727*7c478bd9Sstevel@tonic-gate 728*7c478bd9Sstevel@tonic-gate if ((pool = svc_pool_find(svc, id)) == NULL) { 729*7c478bd9Sstevel@tonic-gate mutex_exit(&svc->svc_plock); 730*7c478bd9Sstevel@tonic-gate return (ENOENT); 731*7c478bd9Sstevel@tonic-gate } 732*7c478bd9Sstevel@tonic-gate /* 733*7c478bd9Sstevel@tonic-gate * Grab the transport list lock before releasing the 734*7c478bd9Sstevel@tonic-gate * pool list lock 735*7c478bd9Sstevel@tonic-gate */ 736*7c478bd9Sstevel@tonic-gate rw_enter(&pool->p_lrwlock, RW_WRITER); 737*7c478bd9Sstevel@tonic-gate mutex_exit(&svc->svc_plock); 738*7c478bd9Sstevel@tonic-gate 739*7c478bd9Sstevel@tonic-gate pool->p_shutdown = *((void (*)())arg); 740*7c478bd9Sstevel@tonic-gate 741*7c478bd9Sstevel@tonic-gate rw_exit(&pool->p_lrwlock); 742*7c478bd9Sstevel@tonic-gate 743*7c478bd9Sstevel@tonic-gate return (0); 744*7c478bd9Sstevel@tonic-gate case SVCPSET_UNREGISTER_PROC: 745*7c478bd9Sstevel@tonic-gate /* 746*7c478bd9Sstevel@tonic-gate * Search the list for a pool with a matching id 747*7c478bd9Sstevel@tonic-gate * and register the unregister callback handle with that pool. 748*7c478bd9Sstevel@tonic-gate */ 749*7c478bd9Sstevel@tonic-gate mutex_enter(&svc->svc_plock); 750*7c478bd9Sstevel@tonic-gate 751*7c478bd9Sstevel@tonic-gate if ((pool = svc_pool_find(svc, id)) == NULL) { 752*7c478bd9Sstevel@tonic-gate mutex_exit(&svc->svc_plock); 753*7c478bd9Sstevel@tonic-gate return (ENOENT); 754*7c478bd9Sstevel@tonic-gate } 755*7c478bd9Sstevel@tonic-gate /* 756*7c478bd9Sstevel@tonic-gate * Grab the transport list lock before releasing the 757*7c478bd9Sstevel@tonic-gate * pool list lock 758*7c478bd9Sstevel@tonic-gate */ 759*7c478bd9Sstevel@tonic-gate rw_enter(&pool->p_lrwlock, RW_WRITER); 760*7c478bd9Sstevel@tonic-gate mutex_exit(&svc->svc_plock); 761*7c478bd9Sstevel@tonic-gate 762*7c478bd9Sstevel@tonic-gate pool->p_offline = *((void (*)())arg); 763*7c478bd9Sstevel@tonic-gate 764*7c478bd9Sstevel@tonic-gate rw_exit(&pool->p_lrwlock); 765*7c478bd9Sstevel@tonic-gate 766*7c478bd9Sstevel@tonic-gate return (0); 767*7c478bd9Sstevel@tonic-gate default: 768*7c478bd9Sstevel@tonic-gate return (EINVAL); 769*7c478bd9Sstevel@tonic-gate } 770*7c478bd9Sstevel@tonic-gate } 771*7c478bd9Sstevel@tonic-gate 772*7c478bd9Sstevel@tonic-gate /* 773*7c478bd9Sstevel@tonic-gate * Pool's transport list manipulation routines. 774*7c478bd9Sstevel@tonic-gate * - svc_xprt_register() 775*7c478bd9Sstevel@tonic-gate * - svc_xprt_unregister() 776*7c478bd9Sstevel@tonic-gate * 777*7c478bd9Sstevel@tonic-gate * svc_xprt_register() is called from svc_tli_kcreate() to 778*7c478bd9Sstevel@tonic-gate * insert a new master transport handle into the doubly linked 779*7c478bd9Sstevel@tonic-gate * list of server transport handles (one list per pool). 780*7c478bd9Sstevel@tonic-gate * 781*7c478bd9Sstevel@tonic-gate * The list is used by svc_poll(), when it operates in `drain' 782*7c478bd9Sstevel@tonic-gate * mode, to search for a next transport with a pending request. 783*7c478bd9Sstevel@tonic-gate */ 784*7c478bd9Sstevel@tonic-gate 785*7c478bd9Sstevel@tonic-gate int 786*7c478bd9Sstevel@tonic-gate svc_xprt_register(SVCMASTERXPRT *xprt, int id) 787*7c478bd9Sstevel@tonic-gate { 788*7c478bd9Sstevel@tonic-gate SVCMASTERXPRT *prev, *next; 789*7c478bd9Sstevel@tonic-gate SVCPOOL *pool; 790*7c478bd9Sstevel@tonic-gate struct svc_globals *svc; 791*7c478bd9Sstevel@tonic-gate 792*7c478bd9Sstevel@tonic-gate svc = zone_getspecific(svc_zone_key, curproc->p_zone); 793*7c478bd9Sstevel@tonic-gate /* 794*7c478bd9Sstevel@tonic-gate * Search the list for a pool with a matching id 795*7c478bd9Sstevel@tonic-gate * and register the transport handle with that pool. 796*7c478bd9Sstevel@tonic-gate */ 797*7c478bd9Sstevel@tonic-gate mutex_enter(&svc->svc_plock); 798*7c478bd9Sstevel@tonic-gate 799*7c478bd9Sstevel@tonic-gate if ((pool = svc_pool_find(svc, id)) == NULL) { 800*7c478bd9Sstevel@tonic-gate mutex_exit(&svc->svc_plock); 801*7c478bd9Sstevel@tonic-gate return (ENOENT); 802*7c478bd9Sstevel@tonic-gate } 803*7c478bd9Sstevel@tonic-gate 804*7c478bd9Sstevel@tonic-gate /* Grab the transport list lock before releasing the pool list lock */ 805*7c478bd9Sstevel@tonic-gate rw_enter(&pool->p_lrwlock, RW_WRITER); 806*7c478bd9Sstevel@tonic-gate mutex_exit(&svc->svc_plock); 807*7c478bd9Sstevel@tonic-gate 808*7c478bd9Sstevel@tonic-gate /* Don't register new transports when the pool is in closing state */ 809*7c478bd9Sstevel@tonic-gate if (pool->p_closing) { 810*7c478bd9Sstevel@tonic-gate rw_exit(&pool->p_lrwlock); 811*7c478bd9Sstevel@tonic-gate return (EBUSY); 812*7c478bd9Sstevel@tonic-gate } 813*7c478bd9Sstevel@tonic-gate 814*7c478bd9Sstevel@tonic-gate /* 815*7c478bd9Sstevel@tonic-gate * Initialize xp_pool to point to the pool. 816*7c478bd9Sstevel@tonic-gate * We don't want to go through the pool list every time. 817*7c478bd9Sstevel@tonic-gate */ 818*7c478bd9Sstevel@tonic-gate xprt->xp_pool = pool; 819*7c478bd9Sstevel@tonic-gate 820*7c478bd9Sstevel@tonic-gate /* 821*7c478bd9Sstevel@tonic-gate * Insert a transport handle into the list. 822*7c478bd9Sstevel@tonic-gate * The list head points to the most recently inserted transport. 823*7c478bd9Sstevel@tonic-gate */ 824*7c478bd9Sstevel@tonic-gate if (pool->p_lhead == NULL) 825*7c478bd9Sstevel@tonic-gate pool->p_lhead = xprt->xp_prev = xprt->xp_next = xprt; 826*7c478bd9Sstevel@tonic-gate else { 827*7c478bd9Sstevel@tonic-gate next = pool->p_lhead; 828*7c478bd9Sstevel@tonic-gate prev = pool->p_lhead->xp_prev; 829*7c478bd9Sstevel@tonic-gate 830*7c478bd9Sstevel@tonic-gate xprt->xp_next = next; 831*7c478bd9Sstevel@tonic-gate xprt->xp_prev = prev; 832*7c478bd9Sstevel@tonic-gate 833*7c478bd9Sstevel@tonic-gate pool->p_lhead = prev->xp_next = next->xp_prev = xprt; 834*7c478bd9Sstevel@tonic-gate } 835*7c478bd9Sstevel@tonic-gate 836*7c478bd9Sstevel@tonic-gate /* Increment the transports count */ 837*7c478bd9Sstevel@tonic-gate pool->p_lcount++; 838*7c478bd9Sstevel@tonic-gate 839*7c478bd9Sstevel@tonic-gate rw_exit(&pool->p_lrwlock); 840*7c478bd9Sstevel@tonic-gate return (0); 841*7c478bd9Sstevel@tonic-gate } 842*7c478bd9Sstevel@tonic-gate 843*7c478bd9Sstevel@tonic-gate /* 844*7c478bd9Sstevel@tonic-gate * Called from svc_xprt_cleanup() to remove a master transport handle 845*7c478bd9Sstevel@tonic-gate * from the pool's list of server transports (when a transport is 846*7c478bd9Sstevel@tonic-gate * being destroyed). 847*7c478bd9Sstevel@tonic-gate */ 848*7c478bd9Sstevel@tonic-gate void 849*7c478bd9Sstevel@tonic-gate svc_xprt_unregister(SVCMASTERXPRT *xprt) 850*7c478bd9Sstevel@tonic-gate { 851*7c478bd9Sstevel@tonic-gate SVCPOOL *pool = xprt->xp_pool; 852*7c478bd9Sstevel@tonic-gate 853*7c478bd9Sstevel@tonic-gate /* 854*7c478bd9Sstevel@tonic-gate * Unlink xprt from the list. 855*7c478bd9Sstevel@tonic-gate * If the list head points to this xprt then move it 856*7c478bd9Sstevel@tonic-gate * to the next xprt or reset to NULL if this is the last 857*7c478bd9Sstevel@tonic-gate * xprt in the list. 858*7c478bd9Sstevel@tonic-gate */ 859*7c478bd9Sstevel@tonic-gate rw_enter(&pool->p_lrwlock, RW_WRITER); 860*7c478bd9Sstevel@tonic-gate 861*7c478bd9Sstevel@tonic-gate if (xprt == xprt->xp_next) 862*7c478bd9Sstevel@tonic-gate pool->p_lhead = NULL; 863*7c478bd9Sstevel@tonic-gate else { 864*7c478bd9Sstevel@tonic-gate SVCMASTERXPRT *next = xprt->xp_next; 865*7c478bd9Sstevel@tonic-gate SVCMASTERXPRT *prev = xprt->xp_prev; 866*7c478bd9Sstevel@tonic-gate 867*7c478bd9Sstevel@tonic-gate next->xp_prev = prev; 868*7c478bd9Sstevel@tonic-gate prev->xp_next = next; 869*7c478bd9Sstevel@tonic-gate 870*7c478bd9Sstevel@tonic-gate if (pool->p_lhead == xprt) 871*7c478bd9Sstevel@tonic-gate pool->p_lhead = next; 872*7c478bd9Sstevel@tonic-gate } 873*7c478bd9Sstevel@tonic-gate 874*7c478bd9Sstevel@tonic-gate xprt->xp_next = xprt->xp_prev = NULL; 875*7c478bd9Sstevel@tonic-gate 876*7c478bd9Sstevel@tonic-gate /* Decrement list count */ 877*7c478bd9Sstevel@tonic-gate pool->p_lcount--; 878*7c478bd9Sstevel@tonic-gate 879*7c478bd9Sstevel@tonic-gate rw_exit(&pool->p_lrwlock); 880*7c478bd9Sstevel@tonic-gate } 881*7c478bd9Sstevel@tonic-gate 882*7c478bd9Sstevel@tonic-gate static void 883*7c478bd9Sstevel@tonic-gate svc_xprt_qdestroy(SVCPOOL *pool) 884*7c478bd9Sstevel@tonic-gate { 885*7c478bd9Sstevel@tonic-gate mutex_destroy(&pool->p_qend_lock); 886*7c478bd9Sstevel@tonic-gate kmem_free(pool->p_qbody, pool->p_qsize * sizeof (__SVCXPRT_QNODE)); 887*7c478bd9Sstevel@tonic-gate } 888*7c478bd9Sstevel@tonic-gate 889*7c478bd9Sstevel@tonic-gate /* 890*7c478bd9Sstevel@tonic-gate * Initialize an `xprt-ready' queue for a given pool. 891*7c478bd9Sstevel@tonic-gate */ 892*7c478bd9Sstevel@tonic-gate static void 893*7c478bd9Sstevel@tonic-gate svc_xprt_qinit(SVCPOOL *pool, size_t qsize) 894*7c478bd9Sstevel@tonic-gate { 895*7c478bd9Sstevel@tonic-gate int i; 896*7c478bd9Sstevel@tonic-gate 897*7c478bd9Sstevel@tonic-gate pool->p_qsize = qsize; 898*7c478bd9Sstevel@tonic-gate pool->p_qbody = kmem_zalloc(pool->p_qsize * sizeof (__SVCXPRT_QNODE), 899*7c478bd9Sstevel@tonic-gate KM_SLEEP); 900*7c478bd9Sstevel@tonic-gate 901*7c478bd9Sstevel@tonic-gate for (i = 0; i < pool->p_qsize - 1; i++) 902*7c478bd9Sstevel@tonic-gate pool->p_qbody[i].q_next = &(pool->p_qbody[i+1]); 903*7c478bd9Sstevel@tonic-gate 904*7c478bd9Sstevel@tonic-gate pool->p_qbody[pool->p_qsize-1].q_next = &(pool->p_qbody[0]); 905*7c478bd9Sstevel@tonic-gate pool->p_qtop = &(pool->p_qbody[0]); 906*7c478bd9Sstevel@tonic-gate pool->p_qend = &(pool->p_qbody[0]); 907*7c478bd9Sstevel@tonic-gate 908*7c478bd9Sstevel@tonic-gate mutex_init(&pool->p_qend_lock, NULL, MUTEX_DEFAULT, NULL); 909*7c478bd9Sstevel@tonic-gate } 910*7c478bd9Sstevel@tonic-gate 911*7c478bd9Sstevel@tonic-gate /* 912*7c478bd9Sstevel@tonic-gate * Called from the svc_queuereq() interrupt routine to queue 913*7c478bd9Sstevel@tonic-gate * a hint for svc_poll() which transport has a pending request. 914*7c478bd9Sstevel@tonic-gate * - insert a pointer to xprt into the xprt-ready queue (FIFO) 915*7c478bd9Sstevel@tonic-gate * - if the xprt-ready queue is full turn the overflow flag on. 916*7c478bd9Sstevel@tonic-gate * 917*7c478bd9Sstevel@tonic-gate * NOTICE: pool->p_qtop is protected by the the pool's request lock 918*7c478bd9Sstevel@tonic-gate * and the caller (svc_queuereq()) must hold the lock. 919*7c478bd9Sstevel@tonic-gate */ 920*7c478bd9Sstevel@tonic-gate static void 921*7c478bd9Sstevel@tonic-gate svc_xprt_qput(SVCPOOL *pool, SVCMASTERXPRT *xprt) 922*7c478bd9Sstevel@tonic-gate { 923*7c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&pool->p_req_lock)); 924*7c478bd9Sstevel@tonic-gate 925*7c478bd9Sstevel@tonic-gate /* If the overflow flag is there is nothing we can do */ 926*7c478bd9Sstevel@tonic-gate if (pool->p_qoverflow) 927*7c478bd9Sstevel@tonic-gate return; 928*7c478bd9Sstevel@tonic-gate 929*7c478bd9Sstevel@tonic-gate /* If the queue is full turn the overflow flag on and exit */ 930*7c478bd9Sstevel@tonic-gate if (pool->p_qtop->q_next == pool->p_qend) { 931*7c478bd9Sstevel@tonic-gate mutex_enter(&pool->p_qend_lock); 932*7c478bd9Sstevel@tonic-gate if (pool->p_qtop->q_next == pool->p_qend) { 933*7c478bd9Sstevel@tonic-gate pool->p_qoverflow = TRUE; 934*7c478bd9Sstevel@tonic-gate mutex_exit(&pool->p_qend_lock); 935*7c478bd9Sstevel@tonic-gate return; 936*7c478bd9Sstevel@tonic-gate } 937*7c478bd9Sstevel@tonic-gate mutex_exit(&pool->p_qend_lock); 938*7c478bd9Sstevel@tonic-gate } 939*7c478bd9Sstevel@tonic-gate 940*7c478bd9Sstevel@tonic-gate /* Insert a hint and move pool->p_qtop */ 941*7c478bd9Sstevel@tonic-gate pool->p_qtop->q_xprt = xprt; 942*7c478bd9Sstevel@tonic-gate pool->p_qtop = pool->p_qtop->q_next; 943*7c478bd9Sstevel@tonic-gate } 944*7c478bd9Sstevel@tonic-gate 945*7c478bd9Sstevel@tonic-gate /* 946*7c478bd9Sstevel@tonic-gate * Called from svc_poll() to get a hint which transport has a 947*7c478bd9Sstevel@tonic-gate * pending request. Returns a pointer to a transport or NULL if the 948*7c478bd9Sstevel@tonic-gate * `xprt-ready' queue is empty. 949*7c478bd9Sstevel@tonic-gate * 950*7c478bd9Sstevel@tonic-gate * Since we do not acquire the pool's request lock while checking if 951*7c478bd9Sstevel@tonic-gate * the queue is empty we may miss a request that is just being delivered. 952*7c478bd9Sstevel@tonic-gate * However this is ok since svc_poll() will retry again until the 953*7c478bd9Sstevel@tonic-gate * count indicates that there are pending requests for this pool. 954*7c478bd9Sstevel@tonic-gate */ 955*7c478bd9Sstevel@tonic-gate static SVCMASTERXPRT * 956*7c478bd9Sstevel@tonic-gate svc_xprt_qget(SVCPOOL *pool) 957*7c478bd9Sstevel@tonic-gate { 958*7c478bd9Sstevel@tonic-gate SVCMASTERXPRT *xprt; 959*7c478bd9Sstevel@tonic-gate 960*7c478bd9Sstevel@tonic-gate mutex_enter(&pool->p_qend_lock); 961*7c478bd9Sstevel@tonic-gate do { 962*7c478bd9Sstevel@tonic-gate /* 963*7c478bd9Sstevel@tonic-gate * If the queue is empty return NULL. 964*7c478bd9Sstevel@tonic-gate * Since we do not acquire the pool's request lock which 965*7c478bd9Sstevel@tonic-gate * protects pool->p_qtop this is not exact check. However, 966*7c478bd9Sstevel@tonic-gate * this is safe - if we miss a request here svc_poll() 967*7c478bd9Sstevel@tonic-gate * will retry again. 968*7c478bd9Sstevel@tonic-gate */ 969*7c478bd9Sstevel@tonic-gate if (pool->p_qend == pool->p_qtop) { 970*7c478bd9Sstevel@tonic-gate mutex_exit(&pool->p_qend_lock); 971*7c478bd9Sstevel@tonic-gate return (NULL); 972*7c478bd9Sstevel@tonic-gate } 973*7c478bd9Sstevel@tonic-gate 974*7c478bd9Sstevel@tonic-gate /* Get a hint and move pool->p_qend */ 975*7c478bd9Sstevel@tonic-gate xprt = pool->p_qend->q_xprt; 976*7c478bd9Sstevel@tonic-gate pool->p_qend = pool->p_qend->q_next; 977*7c478bd9Sstevel@tonic-gate 978*7c478bd9Sstevel@tonic-gate /* Skip fields deleted by svc_xprt_qdelete() */ 979*7c478bd9Sstevel@tonic-gate } while (xprt == NULL); 980*7c478bd9Sstevel@tonic-gate mutex_exit(&pool->p_qend_lock); 981*7c478bd9Sstevel@tonic-gate 982*7c478bd9Sstevel@tonic-gate return (xprt); 983*7c478bd9Sstevel@tonic-gate } 984*7c478bd9Sstevel@tonic-gate 985*7c478bd9Sstevel@tonic-gate /* 986*7c478bd9Sstevel@tonic-gate * Reset an overflow in the xprt-ready queue after 987*7c478bd9Sstevel@tonic-gate * all the pending requests has been drained. 988*7c478bd9Sstevel@tonic-gate * This switches svc_poll back to getting hints from the 989*7c478bd9Sstevel@tonic-gate * xprt-ready queue. 990*7c478bd9Sstevel@tonic-gate * 991*7c478bd9Sstevel@tonic-gate * NOTICE: pool->p_qtop is protected by the the pool's request lock 992*7c478bd9Sstevel@tonic-gate * and the caller (svc_poll()) must hold the lock. 993*7c478bd9Sstevel@tonic-gate */ 994*7c478bd9Sstevel@tonic-gate static void 995*7c478bd9Sstevel@tonic-gate svc_xprt_qreset(SVCPOOL *pool) 996*7c478bd9Sstevel@tonic-gate { 997*7c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&pool->p_req_lock)); 998*7c478bd9Sstevel@tonic-gate 999*7c478bd9Sstevel@tonic-gate pool->p_qend = pool->p_qtop; 1000*7c478bd9Sstevel@tonic-gate pool->p_qoverflow = FALSE; 1001*7c478bd9Sstevel@tonic-gate } 1002*7c478bd9Sstevel@tonic-gate 1003*7c478bd9Sstevel@tonic-gate /* 1004*7c478bd9Sstevel@tonic-gate * Delete all the references to a transport handle that 1005*7c478bd9Sstevel@tonic-gate * is being destroyed from the xprt-ready queue. 1006*7c478bd9Sstevel@tonic-gate * Deleted pointers are replaced with NULLs. 1007*7c478bd9Sstevel@tonic-gate */ 1008*7c478bd9Sstevel@tonic-gate static void 1009*7c478bd9Sstevel@tonic-gate svc_xprt_qdelete(SVCPOOL *pool, SVCMASTERXPRT *xprt) 1010*7c478bd9Sstevel@tonic-gate { 1011*7c478bd9Sstevel@tonic-gate __SVCXPRT_QNODE *q = pool->p_qend; 1012*7c478bd9Sstevel@tonic-gate __SVCXPRT_QNODE *qtop = pool->p_qtop; 1013*7c478bd9Sstevel@tonic-gate 1014*7c478bd9Sstevel@tonic-gate /* 1015*7c478bd9Sstevel@tonic-gate * Delete all the references to xprt between the current 1016*7c478bd9Sstevel@tonic-gate * position of pool->p_qend and current pool->p_qtop. 1017*7c478bd9Sstevel@tonic-gate */ 1018*7c478bd9Sstevel@tonic-gate for (;;) { 1019*7c478bd9Sstevel@tonic-gate if (q->q_xprt == xprt) 1020*7c478bd9Sstevel@tonic-gate q->q_xprt = NULL; 1021*7c478bd9Sstevel@tonic-gate if (q == qtop) 1022*7c478bd9Sstevel@tonic-gate return; 1023*7c478bd9Sstevel@tonic-gate q = q->q_next; 1024*7c478bd9Sstevel@tonic-gate } 1025*7c478bd9Sstevel@tonic-gate } 1026*7c478bd9Sstevel@tonic-gate 1027*7c478bd9Sstevel@tonic-gate /* 1028*7c478bd9Sstevel@tonic-gate * Destructor for a master server transport handle. 1029*7c478bd9Sstevel@tonic-gate * - if there are no more non-detached threads linked to this transport 1030*7c478bd9Sstevel@tonic-gate * then, if requested, call xp_closeproc (we don't wait for detached 1031*7c478bd9Sstevel@tonic-gate * threads linked to this transport to complete). 1032*7c478bd9Sstevel@tonic-gate * - if there are no more threads linked to this 1033*7c478bd9Sstevel@tonic-gate * transport then 1034*7c478bd9Sstevel@tonic-gate * a) remove references to this transport from the xprt-ready queue 1035*7c478bd9Sstevel@tonic-gate * b) remove a reference to this transport from the pool's transport list 1036*7c478bd9Sstevel@tonic-gate * c) call a transport specific `destroy' function 1037*7c478bd9Sstevel@tonic-gate * d) cancel remaining thread reservations. 1038*7c478bd9Sstevel@tonic-gate * 1039*7c478bd9Sstevel@tonic-gate * NOTICE: Caller must hold the transport's thread lock. 1040*7c478bd9Sstevel@tonic-gate */ 1041*7c478bd9Sstevel@tonic-gate static void 1042*7c478bd9Sstevel@tonic-gate svc_xprt_cleanup(SVCMASTERXPRT *xprt, bool_t detached) 1043*7c478bd9Sstevel@tonic-gate { 1044*7c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&xprt->xp_thread_lock)); 1045*7c478bd9Sstevel@tonic-gate ASSERT(xprt->xp_wq == NULL); 1046*7c478bd9Sstevel@tonic-gate 1047*7c478bd9Sstevel@tonic-gate /* 1048*7c478bd9Sstevel@tonic-gate * If called from the last non-detached thread 1049*7c478bd9Sstevel@tonic-gate * it should call the closeproc on this transport. 1050*7c478bd9Sstevel@tonic-gate */ 1051*7c478bd9Sstevel@tonic-gate if (!detached && xprt->xp_threads == 0 && xprt->xp_closeproc) { 1052*7c478bd9Sstevel@tonic-gate (*(xprt->xp_closeproc)) (xprt); 1053*7c478bd9Sstevel@tonic-gate } 1054*7c478bd9Sstevel@tonic-gate 1055*7c478bd9Sstevel@tonic-gate if (xprt->xp_threads + xprt->xp_detached_threads > 0) 1056*7c478bd9Sstevel@tonic-gate mutex_exit(&xprt->xp_thread_lock); 1057*7c478bd9Sstevel@tonic-gate else { 1058*7c478bd9Sstevel@tonic-gate /* Remove references to xprt from the `xprt-ready' queue */ 1059*7c478bd9Sstevel@tonic-gate svc_xprt_qdelete(xprt->xp_pool, xprt); 1060*7c478bd9Sstevel@tonic-gate 1061*7c478bd9Sstevel@tonic-gate /* Unregister xprt from the pool's transport list */ 1062*7c478bd9Sstevel@tonic-gate svc_xprt_unregister(xprt); 1063*7c478bd9Sstevel@tonic-gate svc_callout_free(xprt); 1064*7c478bd9Sstevel@tonic-gate SVC_DESTROY(xprt); 1065*7c478bd9Sstevel@tonic-gate } 1066*7c478bd9Sstevel@tonic-gate } 1067*7c478bd9Sstevel@tonic-gate 1068*7c478bd9Sstevel@tonic-gate /* 1069*7c478bd9Sstevel@tonic-gate * Find a dispatch routine for a given prog/vers pair. 1070*7c478bd9Sstevel@tonic-gate * This function is called from svc_getreq() to search the callout 1071*7c478bd9Sstevel@tonic-gate * table for an entry with a matching RPC program number `prog' 1072*7c478bd9Sstevel@tonic-gate * and a version range that covers `vers'. 1073*7c478bd9Sstevel@tonic-gate * - if it finds a matching entry it returns pointer to the dispatch routine 1074*7c478bd9Sstevel@tonic-gate * - otherwise it returns NULL and, if `minp' or `maxp' are not NULL, 1075*7c478bd9Sstevel@tonic-gate * fills them with, respectively, lowest version and highest version 1076*7c478bd9Sstevel@tonic-gate * supported for the program `prog' 1077*7c478bd9Sstevel@tonic-gate */ 1078*7c478bd9Sstevel@tonic-gate static SVC_DISPATCH * 1079*7c478bd9Sstevel@tonic-gate svc_callout_find(SVCXPRT *xprt, rpcprog_t prog, rpcvers_t vers, 1080*7c478bd9Sstevel@tonic-gate rpcvers_t *vers_min, rpcvers_t *vers_max) 1081*7c478bd9Sstevel@tonic-gate { 1082*7c478bd9Sstevel@tonic-gate SVC_CALLOUT_TABLE *sct = xprt->xp_sct; 1083*7c478bd9Sstevel@tonic-gate int i; 1084*7c478bd9Sstevel@tonic-gate 1085*7c478bd9Sstevel@tonic-gate *vers_min = ~(rpcvers_t)0; 1086*7c478bd9Sstevel@tonic-gate *vers_max = 0; 1087*7c478bd9Sstevel@tonic-gate 1088*7c478bd9Sstevel@tonic-gate for (i = 0; i < sct->sct_size; i++) { 1089*7c478bd9Sstevel@tonic-gate SVC_CALLOUT *sc = &sct->sct_sc[i]; 1090*7c478bd9Sstevel@tonic-gate 1091*7c478bd9Sstevel@tonic-gate if (prog == sc->sc_prog) { 1092*7c478bd9Sstevel@tonic-gate if (vers >= sc->sc_versmin && vers <= sc->sc_versmax) 1093*7c478bd9Sstevel@tonic-gate return (sc->sc_dispatch); 1094*7c478bd9Sstevel@tonic-gate 1095*7c478bd9Sstevel@tonic-gate if (*vers_max < sc->sc_versmax) 1096*7c478bd9Sstevel@tonic-gate *vers_max = sc->sc_versmax; 1097*7c478bd9Sstevel@tonic-gate if (*vers_min > sc->sc_versmin) 1098*7c478bd9Sstevel@tonic-gate *vers_min = sc->sc_versmin; 1099*7c478bd9Sstevel@tonic-gate } 1100*7c478bd9Sstevel@tonic-gate } 1101*7c478bd9Sstevel@tonic-gate 1102*7c478bd9Sstevel@tonic-gate return (NULL); 1103*7c478bd9Sstevel@tonic-gate } 1104*7c478bd9Sstevel@tonic-gate 1105*7c478bd9Sstevel@tonic-gate /* 1106*7c478bd9Sstevel@tonic-gate * Optionally free callout table allocated for this transport by 1107*7c478bd9Sstevel@tonic-gate * the service provider. 1108*7c478bd9Sstevel@tonic-gate */ 1109*7c478bd9Sstevel@tonic-gate static void 1110*7c478bd9Sstevel@tonic-gate svc_callout_free(SVCMASTERXPRT *xprt) 1111*7c478bd9Sstevel@tonic-gate { 1112*7c478bd9Sstevel@tonic-gate SVC_CALLOUT_TABLE *sct = xprt->xp_sct; 1113*7c478bd9Sstevel@tonic-gate 1114*7c478bd9Sstevel@tonic-gate if (sct->sct_free) { 1115*7c478bd9Sstevel@tonic-gate kmem_free(sct->sct_sc, sct->sct_size * sizeof (SVC_CALLOUT)); 1116*7c478bd9Sstevel@tonic-gate kmem_free(sct, sizeof (SVC_CALLOUT_TABLE)); 1117*7c478bd9Sstevel@tonic-gate } 1118*7c478bd9Sstevel@tonic-gate } 1119*7c478bd9Sstevel@tonic-gate 1120*7c478bd9Sstevel@tonic-gate /* 1121*7c478bd9Sstevel@tonic-gate * Send a reply to an RPC request 1122*7c478bd9Sstevel@tonic-gate * 1123*7c478bd9Sstevel@tonic-gate * PSARC 2003/523 Contract Private Interface 1124*7c478bd9Sstevel@tonic-gate * svc_sendreply 1125*7c478bd9Sstevel@tonic-gate * Changes must be reviewed by Solaris File Sharing 1126*7c478bd9Sstevel@tonic-gate * Changes must be communicated to contract-2003-523@sun.com 1127*7c478bd9Sstevel@tonic-gate */ 1128*7c478bd9Sstevel@tonic-gate bool_t 1129*7c478bd9Sstevel@tonic-gate svc_sendreply(const SVCXPRT *clone_xprt, const xdrproc_t xdr_results, 1130*7c478bd9Sstevel@tonic-gate const caddr_t xdr_location) 1131*7c478bd9Sstevel@tonic-gate { 1132*7c478bd9Sstevel@tonic-gate struct rpc_msg rply; 1133*7c478bd9Sstevel@tonic-gate 1134*7c478bd9Sstevel@tonic-gate rply.rm_direction = REPLY; 1135*7c478bd9Sstevel@tonic-gate rply.rm_reply.rp_stat = MSG_ACCEPTED; 1136*7c478bd9Sstevel@tonic-gate rply.acpted_rply.ar_verf = clone_xprt->xp_verf; 1137*7c478bd9Sstevel@tonic-gate rply.acpted_rply.ar_stat = SUCCESS; 1138*7c478bd9Sstevel@tonic-gate rply.acpted_rply.ar_results.where = xdr_location; 1139*7c478bd9Sstevel@tonic-gate rply.acpted_rply.ar_results.proc = xdr_results; 1140*7c478bd9Sstevel@tonic-gate 1141*7c478bd9Sstevel@tonic-gate return (SVC_REPLY((SVCXPRT *)clone_xprt, &rply)); 1142*7c478bd9Sstevel@tonic-gate } 1143*7c478bd9Sstevel@tonic-gate 1144*7c478bd9Sstevel@tonic-gate /* 1145*7c478bd9Sstevel@tonic-gate * No procedure error reply 1146*7c478bd9Sstevel@tonic-gate * 1147*7c478bd9Sstevel@tonic-gate * PSARC 2003/523 Contract Private Interface 1148*7c478bd9Sstevel@tonic-gate * svcerr_noproc 1149*7c478bd9Sstevel@tonic-gate * Changes must be reviewed by Solaris File Sharing 1150*7c478bd9Sstevel@tonic-gate * Changes must be communicated to contract-2003-523@sun.com 1151*7c478bd9Sstevel@tonic-gate */ 1152*7c478bd9Sstevel@tonic-gate void 1153*7c478bd9Sstevel@tonic-gate svcerr_noproc(const SVCXPRT *clone_xprt) 1154*7c478bd9Sstevel@tonic-gate { 1155*7c478bd9Sstevel@tonic-gate struct rpc_msg rply; 1156*7c478bd9Sstevel@tonic-gate 1157*7c478bd9Sstevel@tonic-gate rply.rm_direction = REPLY; 1158*7c478bd9Sstevel@tonic-gate rply.rm_reply.rp_stat = MSG_ACCEPTED; 1159*7c478bd9Sstevel@tonic-gate rply.acpted_rply.ar_verf = clone_xprt->xp_verf; 1160*7c478bd9Sstevel@tonic-gate rply.acpted_rply.ar_stat = PROC_UNAVAIL; 1161*7c478bd9Sstevel@tonic-gate SVC_FREERES((SVCXPRT *)clone_xprt); 1162*7c478bd9Sstevel@tonic-gate SVC_REPLY((SVCXPRT *)clone_xprt, &rply); 1163*7c478bd9Sstevel@tonic-gate } 1164*7c478bd9Sstevel@tonic-gate 1165*7c478bd9Sstevel@tonic-gate /* 1166*7c478bd9Sstevel@tonic-gate * Can't decode arguments error reply 1167*7c478bd9Sstevel@tonic-gate * 1168*7c478bd9Sstevel@tonic-gate * PSARC 2003/523 Contract Private Interface 1169*7c478bd9Sstevel@tonic-gate * svcerr_decode 1170*7c478bd9Sstevel@tonic-gate * Changes must be reviewed by Solaris File Sharing 1171*7c478bd9Sstevel@tonic-gate * Changes must be communicated to contract-2003-523@sun.com 1172*7c478bd9Sstevel@tonic-gate */ 1173*7c478bd9Sstevel@tonic-gate void 1174*7c478bd9Sstevel@tonic-gate svcerr_decode(const SVCXPRT *clone_xprt) 1175*7c478bd9Sstevel@tonic-gate { 1176*7c478bd9Sstevel@tonic-gate struct rpc_msg rply; 1177*7c478bd9Sstevel@tonic-gate 1178*7c478bd9Sstevel@tonic-gate rply.rm_direction = REPLY; 1179*7c478bd9Sstevel@tonic-gate rply.rm_reply.rp_stat = MSG_ACCEPTED; 1180*7c478bd9Sstevel@tonic-gate rply.acpted_rply.ar_verf = clone_xprt->xp_verf; 1181*7c478bd9Sstevel@tonic-gate rply.acpted_rply.ar_stat = GARBAGE_ARGS; 1182*7c478bd9Sstevel@tonic-gate SVC_FREERES((SVCXPRT *)clone_xprt); 1183*7c478bd9Sstevel@tonic-gate SVC_REPLY((SVCXPRT *)clone_xprt, &rply); 1184*7c478bd9Sstevel@tonic-gate } 1185*7c478bd9Sstevel@tonic-gate 1186*7c478bd9Sstevel@tonic-gate /* 1187*7c478bd9Sstevel@tonic-gate * Some system error 1188*7c478bd9Sstevel@tonic-gate */ 1189*7c478bd9Sstevel@tonic-gate void 1190*7c478bd9Sstevel@tonic-gate svcerr_systemerr(const SVCXPRT *clone_xprt) 1191*7c478bd9Sstevel@tonic-gate { 1192*7c478bd9Sstevel@tonic-gate struct rpc_msg rply; 1193*7c478bd9Sstevel@tonic-gate 1194*7c478bd9Sstevel@tonic-gate rply.rm_direction = REPLY; 1195*7c478bd9Sstevel@tonic-gate rply.rm_reply.rp_stat = MSG_ACCEPTED; 1196*7c478bd9Sstevel@tonic-gate rply.acpted_rply.ar_verf = clone_xprt->xp_verf; 1197*7c478bd9Sstevel@tonic-gate rply.acpted_rply.ar_stat = SYSTEM_ERR; 1198*7c478bd9Sstevel@tonic-gate SVC_FREERES((SVCXPRT *)clone_xprt); 1199*7c478bd9Sstevel@tonic-gate SVC_REPLY((SVCXPRT *)clone_xprt, &rply); 1200*7c478bd9Sstevel@tonic-gate } 1201*7c478bd9Sstevel@tonic-gate 1202*7c478bd9Sstevel@tonic-gate /* 1203*7c478bd9Sstevel@tonic-gate * Authentication error reply 1204*7c478bd9Sstevel@tonic-gate */ 1205*7c478bd9Sstevel@tonic-gate void 1206*7c478bd9Sstevel@tonic-gate svcerr_auth(const SVCXPRT *clone_xprt, const enum auth_stat why) 1207*7c478bd9Sstevel@tonic-gate { 1208*7c478bd9Sstevel@tonic-gate struct rpc_msg rply; 1209*7c478bd9Sstevel@tonic-gate 1210*7c478bd9Sstevel@tonic-gate rply.rm_direction = REPLY; 1211*7c478bd9Sstevel@tonic-gate rply.rm_reply.rp_stat = MSG_DENIED; 1212*7c478bd9Sstevel@tonic-gate rply.rjcted_rply.rj_stat = AUTH_ERROR; 1213*7c478bd9Sstevel@tonic-gate rply.rjcted_rply.rj_why = why; 1214*7c478bd9Sstevel@tonic-gate SVC_FREERES((SVCXPRT *)clone_xprt); 1215*7c478bd9Sstevel@tonic-gate SVC_REPLY((SVCXPRT *)clone_xprt, &rply); 1216*7c478bd9Sstevel@tonic-gate } 1217*7c478bd9Sstevel@tonic-gate 1218*7c478bd9Sstevel@tonic-gate /* 1219*7c478bd9Sstevel@tonic-gate * Authentication too weak error reply 1220*7c478bd9Sstevel@tonic-gate */ 1221*7c478bd9Sstevel@tonic-gate void 1222*7c478bd9Sstevel@tonic-gate svcerr_weakauth(const SVCXPRT *clone_xprt) 1223*7c478bd9Sstevel@tonic-gate { 1224*7c478bd9Sstevel@tonic-gate svcerr_auth((SVCXPRT *)clone_xprt, AUTH_TOOWEAK); 1225*7c478bd9Sstevel@tonic-gate } 1226*7c478bd9Sstevel@tonic-gate 1227*7c478bd9Sstevel@tonic-gate /* 1228*7c478bd9Sstevel@tonic-gate * Program unavailable error reply 1229*7c478bd9Sstevel@tonic-gate * 1230*7c478bd9Sstevel@tonic-gate * PSARC 2003/523 Contract Private Interface 1231*7c478bd9Sstevel@tonic-gate * svcerr_noprog 1232*7c478bd9Sstevel@tonic-gate * Changes must be reviewed by Solaris File Sharing 1233*7c478bd9Sstevel@tonic-gate * Changes must be communicated to contract-2003-523@sun.com 1234*7c478bd9Sstevel@tonic-gate */ 1235*7c478bd9Sstevel@tonic-gate void 1236*7c478bd9Sstevel@tonic-gate svcerr_noprog(const SVCXPRT *clone_xprt) 1237*7c478bd9Sstevel@tonic-gate { 1238*7c478bd9Sstevel@tonic-gate struct rpc_msg rply; 1239*7c478bd9Sstevel@tonic-gate 1240*7c478bd9Sstevel@tonic-gate rply.rm_direction = REPLY; 1241*7c478bd9Sstevel@tonic-gate rply.rm_reply.rp_stat = MSG_ACCEPTED; 1242*7c478bd9Sstevel@tonic-gate rply.acpted_rply.ar_verf = clone_xprt->xp_verf; 1243*7c478bd9Sstevel@tonic-gate rply.acpted_rply.ar_stat = PROG_UNAVAIL; 1244*7c478bd9Sstevel@tonic-gate SVC_FREERES((SVCXPRT *)clone_xprt); 1245*7c478bd9Sstevel@tonic-gate SVC_REPLY((SVCXPRT *)clone_xprt, &rply); 1246*7c478bd9Sstevel@tonic-gate } 1247*7c478bd9Sstevel@tonic-gate 1248*7c478bd9Sstevel@tonic-gate /* 1249*7c478bd9Sstevel@tonic-gate * Program version mismatch error reply 1250*7c478bd9Sstevel@tonic-gate * 1251*7c478bd9Sstevel@tonic-gate * PSARC 2003/523 Contract Private Interface 1252*7c478bd9Sstevel@tonic-gate * svcerr_progvers 1253*7c478bd9Sstevel@tonic-gate * Changes must be reviewed by Solaris File Sharing 1254*7c478bd9Sstevel@tonic-gate * Changes must be communicated to contract-2003-523@sun.com 1255*7c478bd9Sstevel@tonic-gate */ 1256*7c478bd9Sstevel@tonic-gate void 1257*7c478bd9Sstevel@tonic-gate svcerr_progvers(const SVCXPRT *clone_xprt, 1258*7c478bd9Sstevel@tonic-gate const rpcvers_t low_vers, const rpcvers_t high_vers) 1259*7c478bd9Sstevel@tonic-gate { 1260*7c478bd9Sstevel@tonic-gate struct rpc_msg rply; 1261*7c478bd9Sstevel@tonic-gate 1262*7c478bd9Sstevel@tonic-gate rply.rm_direction = REPLY; 1263*7c478bd9Sstevel@tonic-gate rply.rm_reply.rp_stat = MSG_ACCEPTED; 1264*7c478bd9Sstevel@tonic-gate rply.acpted_rply.ar_verf = clone_xprt->xp_verf; 1265*7c478bd9Sstevel@tonic-gate rply.acpted_rply.ar_stat = PROG_MISMATCH; 1266*7c478bd9Sstevel@tonic-gate rply.acpted_rply.ar_vers.low = low_vers; 1267*7c478bd9Sstevel@tonic-gate rply.acpted_rply.ar_vers.high = high_vers; 1268*7c478bd9Sstevel@tonic-gate SVC_FREERES((SVCXPRT *)clone_xprt); 1269*7c478bd9Sstevel@tonic-gate SVC_REPLY((SVCXPRT *)clone_xprt, &rply); 1270*7c478bd9Sstevel@tonic-gate } 1271*7c478bd9Sstevel@tonic-gate 1272*7c478bd9Sstevel@tonic-gate /* 1273*7c478bd9Sstevel@tonic-gate * Get server side input from some transport. 1274*7c478bd9Sstevel@tonic-gate * 1275*7c478bd9Sstevel@tonic-gate * Statement of authentication parameters management: 1276*7c478bd9Sstevel@tonic-gate * This function owns and manages all authentication parameters, specifically 1277*7c478bd9Sstevel@tonic-gate * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and 1278*7c478bd9Sstevel@tonic-gate * the "cooked" credentials (rqst->rq_clntcred). 1279*7c478bd9Sstevel@tonic-gate * However, this function does not know the structure of the cooked 1280*7c478bd9Sstevel@tonic-gate * credentials, so it make the following assumptions: 1281*7c478bd9Sstevel@tonic-gate * a) the structure is contiguous (no pointers), and 1282*7c478bd9Sstevel@tonic-gate * b) the cred structure size does not exceed RQCRED_SIZE bytes. 1283*7c478bd9Sstevel@tonic-gate * In all events, all three parameters are freed upon exit from this routine. 1284*7c478bd9Sstevel@tonic-gate * The storage is trivially managed on the call stack in user land, but 1285*7c478bd9Sstevel@tonic-gate * is malloced in kernel land. 1286*7c478bd9Sstevel@tonic-gate * 1287*7c478bd9Sstevel@tonic-gate * Note: the xprt's xp_svc_lock is not held while the service's dispatch 1288*7c478bd9Sstevel@tonic-gate * routine is running. If we decide to implement svc_unregister(), we'll 1289*7c478bd9Sstevel@tonic-gate * need to decide whether it's okay for a thread to unregister a service 1290*7c478bd9Sstevel@tonic-gate * while a request is being processed. If we decide that this is a 1291*7c478bd9Sstevel@tonic-gate * problem, we can probably use some sort of reference counting scheme to 1292*7c478bd9Sstevel@tonic-gate * keep the callout entry from going away until the request has completed. 1293*7c478bd9Sstevel@tonic-gate */ 1294*7c478bd9Sstevel@tonic-gate static void 1295*7c478bd9Sstevel@tonic-gate svc_getreq( 1296*7c478bd9Sstevel@tonic-gate SVCXPRT *clone_xprt, /* clone transport handle */ 1297*7c478bd9Sstevel@tonic-gate mblk_t *mp) 1298*7c478bd9Sstevel@tonic-gate { 1299*7c478bd9Sstevel@tonic-gate struct rpc_msg msg; 1300*7c478bd9Sstevel@tonic-gate struct svc_req r; 1301*7c478bd9Sstevel@tonic-gate char *cred_area; /* too big to allocate on call stack */ 1302*7c478bd9Sstevel@tonic-gate 1303*7c478bd9Sstevel@tonic-gate TRACE_0(TR_FAC_KRPC, TR_SVC_GETREQ_START, 1304*7c478bd9Sstevel@tonic-gate "svc_getreq_start:"); 1305*7c478bd9Sstevel@tonic-gate 1306*7c478bd9Sstevel@tonic-gate ASSERT(clone_xprt->xp_master != NULL); 1307*7c478bd9Sstevel@tonic-gate 1308*7c478bd9Sstevel@tonic-gate /* 1309*7c478bd9Sstevel@tonic-gate * Firstly, allocate the authentication parameters' storage 1310*7c478bd9Sstevel@tonic-gate */ 1311*7c478bd9Sstevel@tonic-gate mutex_enter(&rqcred_lock); 1312*7c478bd9Sstevel@tonic-gate if (rqcred_head) { 1313*7c478bd9Sstevel@tonic-gate cred_area = rqcred_head; 1314*7c478bd9Sstevel@tonic-gate 1315*7c478bd9Sstevel@tonic-gate /* LINTED pointer alignment */ 1316*7c478bd9Sstevel@tonic-gate rqcred_head = *(caddr_t *)rqcred_head; 1317*7c478bd9Sstevel@tonic-gate mutex_exit(&rqcred_lock); 1318*7c478bd9Sstevel@tonic-gate } else { 1319*7c478bd9Sstevel@tonic-gate mutex_exit(&rqcred_lock); 1320*7c478bd9Sstevel@tonic-gate cred_area = kmem_alloc(2 * MAX_AUTH_BYTES + RQCRED_SIZE, 1321*7c478bd9Sstevel@tonic-gate KM_SLEEP); 1322*7c478bd9Sstevel@tonic-gate } 1323*7c478bd9Sstevel@tonic-gate msg.rm_call.cb_cred.oa_base = cred_area; 1324*7c478bd9Sstevel@tonic-gate msg.rm_call.cb_verf.oa_base = &(cred_area[MAX_AUTH_BYTES]); 1325*7c478bd9Sstevel@tonic-gate r.rq_clntcred = &(cred_area[2 * MAX_AUTH_BYTES]); 1326*7c478bd9Sstevel@tonic-gate 1327*7c478bd9Sstevel@tonic-gate /* 1328*7c478bd9Sstevel@tonic-gate * Now receive a message from the transport. 1329*7c478bd9Sstevel@tonic-gate */ 1330*7c478bd9Sstevel@tonic-gate if (SVC_RECV(clone_xprt, mp, &msg)) { 1331*7c478bd9Sstevel@tonic-gate void (*dispatchroutine) (struct svc_req *, SVCXPRT *); 1332*7c478bd9Sstevel@tonic-gate rpcvers_t vers_min; 1333*7c478bd9Sstevel@tonic-gate rpcvers_t vers_max; 1334*7c478bd9Sstevel@tonic-gate bool_t no_dispatch; 1335*7c478bd9Sstevel@tonic-gate enum auth_stat why; 1336*7c478bd9Sstevel@tonic-gate 1337*7c478bd9Sstevel@tonic-gate /* 1338*7c478bd9Sstevel@tonic-gate * Find the registered program and call its 1339*7c478bd9Sstevel@tonic-gate * dispatch routine. 1340*7c478bd9Sstevel@tonic-gate */ 1341*7c478bd9Sstevel@tonic-gate r.rq_xprt = clone_xprt; 1342*7c478bd9Sstevel@tonic-gate r.rq_prog = msg.rm_call.cb_prog; 1343*7c478bd9Sstevel@tonic-gate r.rq_vers = msg.rm_call.cb_vers; 1344*7c478bd9Sstevel@tonic-gate r.rq_proc = msg.rm_call.cb_proc; 1345*7c478bd9Sstevel@tonic-gate r.rq_cred = msg.rm_call.cb_cred; 1346*7c478bd9Sstevel@tonic-gate 1347*7c478bd9Sstevel@tonic-gate /* 1348*7c478bd9Sstevel@tonic-gate * First authenticate the message. 1349*7c478bd9Sstevel@tonic-gate */ 1350*7c478bd9Sstevel@tonic-gate TRACE_0(TR_FAC_KRPC, TR_SVC_GETREQ_AUTH_START, 1351*7c478bd9Sstevel@tonic-gate "svc_getreq_auth_start:"); 1352*7c478bd9Sstevel@tonic-gate if ((why = sec_svc_msg(&r, &msg, &no_dispatch)) != AUTH_OK) { 1353*7c478bd9Sstevel@tonic-gate TRACE_1(TR_FAC_KRPC, TR_SVC_GETREQ_AUTH_END, 1354*7c478bd9Sstevel@tonic-gate "svc_getreq_auth_end:(%S)", "failed"); 1355*7c478bd9Sstevel@tonic-gate svcerr_auth(clone_xprt, why); 1356*7c478bd9Sstevel@tonic-gate /* 1357*7c478bd9Sstevel@tonic-gate * Free the arguments. 1358*7c478bd9Sstevel@tonic-gate */ 1359*7c478bd9Sstevel@tonic-gate (void) SVC_FREEARGS(clone_xprt, NULL, NULL); 1360*7c478bd9Sstevel@tonic-gate } else if (no_dispatch) { 1361*7c478bd9Sstevel@tonic-gate /* 1362*7c478bd9Sstevel@tonic-gate * XXX - when bug id 4053736 is done, remove 1363*7c478bd9Sstevel@tonic-gate * the SVC_FREEARGS() call. 1364*7c478bd9Sstevel@tonic-gate */ 1365*7c478bd9Sstevel@tonic-gate (void) SVC_FREEARGS(clone_xprt, NULL, NULL); 1366*7c478bd9Sstevel@tonic-gate } else { 1367*7c478bd9Sstevel@tonic-gate TRACE_1(TR_FAC_KRPC, TR_SVC_GETREQ_AUTH_END, 1368*7c478bd9Sstevel@tonic-gate "svc_getreq_auth_end:(%S)", "good"); 1369*7c478bd9Sstevel@tonic-gate 1370*7c478bd9Sstevel@tonic-gate dispatchroutine = svc_callout_find(clone_xprt, 1371*7c478bd9Sstevel@tonic-gate r.rq_prog, r.rq_vers, &vers_min, &vers_max); 1372*7c478bd9Sstevel@tonic-gate 1373*7c478bd9Sstevel@tonic-gate if (dispatchroutine) { 1374*7c478bd9Sstevel@tonic-gate (*dispatchroutine) (&r, clone_xprt); 1375*7c478bd9Sstevel@tonic-gate } else { 1376*7c478bd9Sstevel@tonic-gate /* 1377*7c478bd9Sstevel@tonic-gate * If we got here, the program or version 1378*7c478bd9Sstevel@tonic-gate * is not served ... 1379*7c478bd9Sstevel@tonic-gate */ 1380*7c478bd9Sstevel@tonic-gate if (vers_max == 0 || 1381*7c478bd9Sstevel@tonic-gate version_keepquiet(clone_xprt)) 1382*7c478bd9Sstevel@tonic-gate svcerr_noprog(clone_xprt); 1383*7c478bd9Sstevel@tonic-gate else 1384*7c478bd9Sstevel@tonic-gate svcerr_progvers(clone_xprt, vers_min, 1385*7c478bd9Sstevel@tonic-gate vers_max); 1386*7c478bd9Sstevel@tonic-gate 1387*7c478bd9Sstevel@tonic-gate /* 1388*7c478bd9Sstevel@tonic-gate * Free the arguments. For successful calls 1389*7c478bd9Sstevel@tonic-gate * this is done by the dispatch routine. 1390*7c478bd9Sstevel@tonic-gate */ 1391*7c478bd9Sstevel@tonic-gate (void) SVC_FREEARGS(clone_xprt, NULL, NULL); 1392*7c478bd9Sstevel@tonic-gate /* Fall through to ... */ 1393*7c478bd9Sstevel@tonic-gate } 1394*7c478bd9Sstevel@tonic-gate /* 1395*7c478bd9Sstevel@tonic-gate * Call cleanup procedure for RPCSEC_GSS. 1396*7c478bd9Sstevel@tonic-gate * This is a hack since there is currently no 1397*7c478bd9Sstevel@tonic-gate * op, such as SVC_CLEANAUTH. rpc_gss_cleanup 1398*7c478bd9Sstevel@tonic-gate * should only be called for a non null proc. 1399*7c478bd9Sstevel@tonic-gate * Null procs in RPC GSS are overloaded to 1400*7c478bd9Sstevel@tonic-gate * provide context setup and control. The main 1401*7c478bd9Sstevel@tonic-gate * purpose of rpc_gss_cleanup is to decrement the 1402*7c478bd9Sstevel@tonic-gate * reference count associated with the cached 1403*7c478bd9Sstevel@tonic-gate * GSS security context. We should never get here 1404*7c478bd9Sstevel@tonic-gate * for an RPCSEC_GSS null proc since *no_dispatch 1405*7c478bd9Sstevel@tonic-gate * would have been set to true from sec_svc_msg above. 1406*7c478bd9Sstevel@tonic-gate */ 1407*7c478bd9Sstevel@tonic-gate if (r.rq_cred.oa_flavor == RPCSEC_GSS) 1408*7c478bd9Sstevel@tonic-gate rpc_gss_cleanup(clone_xprt); 1409*7c478bd9Sstevel@tonic-gate } 1410*7c478bd9Sstevel@tonic-gate } 1411*7c478bd9Sstevel@tonic-gate 1412*7c478bd9Sstevel@tonic-gate /* 1413*7c478bd9Sstevel@tonic-gate * Free authentication parameters' storage 1414*7c478bd9Sstevel@tonic-gate */ 1415*7c478bd9Sstevel@tonic-gate mutex_enter(&rqcred_lock); 1416*7c478bd9Sstevel@tonic-gate /* LINTED pointer alignment */ 1417*7c478bd9Sstevel@tonic-gate *(caddr_t *)cred_area = rqcred_head; 1418*7c478bd9Sstevel@tonic-gate rqcred_head = cred_area; 1419*7c478bd9Sstevel@tonic-gate mutex_exit(&rqcred_lock); 1420*7c478bd9Sstevel@tonic-gate } 1421*7c478bd9Sstevel@tonic-gate 1422*7c478bd9Sstevel@tonic-gate /* 1423*7c478bd9Sstevel@tonic-gate * Allocate new clone transport handle. 1424*7c478bd9Sstevel@tonic-gate */ 1425*7c478bd9Sstevel@tonic-gate static SVCXPRT * 1426*7c478bd9Sstevel@tonic-gate svc_clone_init(void) 1427*7c478bd9Sstevel@tonic-gate { 1428*7c478bd9Sstevel@tonic-gate SVCXPRT *clone_xprt; 1429*7c478bd9Sstevel@tonic-gate 1430*7c478bd9Sstevel@tonic-gate clone_xprt = kmem_zalloc(sizeof (SVCXPRT), KM_SLEEP); 1431*7c478bd9Sstevel@tonic-gate clone_xprt->xp_cred = crget(); 1432*7c478bd9Sstevel@tonic-gate return (clone_xprt); 1433*7c478bd9Sstevel@tonic-gate } 1434*7c478bd9Sstevel@tonic-gate 1435*7c478bd9Sstevel@tonic-gate /* 1436*7c478bd9Sstevel@tonic-gate * Free memory allocated by svc_clone_init. 1437*7c478bd9Sstevel@tonic-gate */ 1438*7c478bd9Sstevel@tonic-gate static void 1439*7c478bd9Sstevel@tonic-gate svc_clone_free(SVCXPRT *clone_xprt) 1440*7c478bd9Sstevel@tonic-gate { 1441*7c478bd9Sstevel@tonic-gate /* Fre credentials from crget() */ 1442*7c478bd9Sstevel@tonic-gate if (clone_xprt->xp_cred) 1443*7c478bd9Sstevel@tonic-gate crfree(clone_xprt->xp_cred); 1444*7c478bd9Sstevel@tonic-gate 1445*7c478bd9Sstevel@tonic-gate kmem_free(clone_xprt, sizeof (SVCXPRT)); 1446*7c478bd9Sstevel@tonic-gate } 1447*7c478bd9Sstevel@tonic-gate 1448*7c478bd9Sstevel@tonic-gate /* 1449*7c478bd9Sstevel@tonic-gate * Link a per-thread clone transport handle to a master 1450*7c478bd9Sstevel@tonic-gate * - increment a thread reference count on the master 1451*7c478bd9Sstevel@tonic-gate * - copy some of the master's fields to the clone 1452*7c478bd9Sstevel@tonic-gate * - call a transport specific clone routine. 1453*7c478bd9Sstevel@tonic-gate */ 1454*7c478bd9Sstevel@tonic-gate static void 1455*7c478bd9Sstevel@tonic-gate svc_clone_link(SVCMASTERXPRT *xprt, SVCXPRT *clone_xprt) 1456*7c478bd9Sstevel@tonic-gate { 1457*7c478bd9Sstevel@tonic-gate cred_t *cred = clone_xprt->xp_cred; 1458*7c478bd9Sstevel@tonic-gate 1459*7c478bd9Sstevel@tonic-gate ASSERT(cred); 1460*7c478bd9Sstevel@tonic-gate 1461*7c478bd9Sstevel@tonic-gate /* 1462*7c478bd9Sstevel@tonic-gate * Bump up master's thread count. 1463*7c478bd9Sstevel@tonic-gate * Linking a per-thread clone transport handle to a master 1464*7c478bd9Sstevel@tonic-gate * associates a service thread with the master. 1465*7c478bd9Sstevel@tonic-gate */ 1466*7c478bd9Sstevel@tonic-gate mutex_enter(&xprt->xp_thread_lock); 1467*7c478bd9Sstevel@tonic-gate xprt->xp_threads++; 1468*7c478bd9Sstevel@tonic-gate mutex_exit(&xprt->xp_thread_lock); 1469*7c478bd9Sstevel@tonic-gate 1470*7c478bd9Sstevel@tonic-gate /* Clear everything */ 1471*7c478bd9Sstevel@tonic-gate bzero(clone_xprt, sizeof (SVCXPRT)); 1472*7c478bd9Sstevel@tonic-gate 1473*7c478bd9Sstevel@tonic-gate /* Set pointer to the master transport stucture */ 1474*7c478bd9Sstevel@tonic-gate clone_xprt->xp_master = xprt; 1475*7c478bd9Sstevel@tonic-gate 1476*7c478bd9Sstevel@tonic-gate /* Structure copy of all the common fields */ 1477*7c478bd9Sstevel@tonic-gate clone_xprt->xp_xpc = xprt->xp_xpc; 1478*7c478bd9Sstevel@tonic-gate 1479*7c478bd9Sstevel@tonic-gate /* Restore per-thread fields (xp_cred) */ 1480*7c478bd9Sstevel@tonic-gate clone_xprt->xp_cred = cred; 1481*7c478bd9Sstevel@tonic-gate 1482*7c478bd9Sstevel@tonic-gate /* 1483*7c478bd9Sstevel@tonic-gate * NOTICE: There is no transport-type specific code now. 1484*7c478bd9Sstevel@tonic-gate * If you want to add a transport-type specific cloning code 1485*7c478bd9Sstevel@tonic-gate * add one more operation (e.g. xp_clone()) to svc_ops, 1486*7c478bd9Sstevel@tonic-gate * implement it for each transport type, and call it here 1487*7c478bd9Sstevel@tonic-gate * through an appropriate macro (e.g. SVC_CLONE()). 1488*7c478bd9Sstevel@tonic-gate */ 1489*7c478bd9Sstevel@tonic-gate } 1490*7c478bd9Sstevel@tonic-gate 1491*7c478bd9Sstevel@tonic-gate /* 1492*7c478bd9Sstevel@tonic-gate * Unlink a non-detached clone transport handle from a master 1493*7c478bd9Sstevel@tonic-gate * - decrement a thread reference count on the master 1494*7c478bd9Sstevel@tonic-gate * - if the transport is closing (xp_wq is NULL) call svc_xprt_cleanup(); 1495*7c478bd9Sstevel@tonic-gate * if this is the last non-detached/absolute thread on this transport 1496*7c478bd9Sstevel@tonic-gate * then it will close/destroy the transport 1497*7c478bd9Sstevel@tonic-gate * - call transport specific function to destroy the clone handle 1498*7c478bd9Sstevel@tonic-gate * - clear xp_master to avoid recursion. 1499*7c478bd9Sstevel@tonic-gate */ 1500*7c478bd9Sstevel@tonic-gate static void 1501*7c478bd9Sstevel@tonic-gate svc_clone_unlink(SVCXPRT *clone_xprt) 1502*7c478bd9Sstevel@tonic-gate { 1503*7c478bd9Sstevel@tonic-gate SVCMASTERXPRT *xprt = clone_xprt->xp_master; 1504*7c478bd9Sstevel@tonic-gate 1505*7c478bd9Sstevel@tonic-gate /* This cannot be a detached thread */ 1506*7c478bd9Sstevel@tonic-gate ASSERT(!clone_xprt->xp_detached); 1507*7c478bd9Sstevel@tonic-gate ASSERT(xprt->xp_threads > 0); 1508*7c478bd9Sstevel@tonic-gate 1509*7c478bd9Sstevel@tonic-gate /* Decrement a reference count on the transport */ 1510*7c478bd9Sstevel@tonic-gate mutex_enter(&xprt->xp_thread_lock); 1511*7c478bd9Sstevel@tonic-gate xprt->xp_threads--; 1512*7c478bd9Sstevel@tonic-gate 1513*7c478bd9Sstevel@tonic-gate /* svc_xprt_cleanup() unlocks xp_thread_lock or destroys xprt */ 1514*7c478bd9Sstevel@tonic-gate if (xprt->xp_wq) 1515*7c478bd9Sstevel@tonic-gate mutex_exit(&xprt->xp_thread_lock); 1516*7c478bd9Sstevel@tonic-gate else 1517*7c478bd9Sstevel@tonic-gate svc_xprt_cleanup(xprt, FALSE); 1518*7c478bd9Sstevel@tonic-gate 1519*7c478bd9Sstevel@tonic-gate /* Call a transport specific clone `destroy' function */ 1520*7c478bd9Sstevel@tonic-gate SVC_CLONE_DESTROY(clone_xprt); 1521*7c478bd9Sstevel@tonic-gate 1522*7c478bd9Sstevel@tonic-gate /* Clear xp_master */ 1523*7c478bd9Sstevel@tonic-gate clone_xprt->xp_master = NULL; 1524*7c478bd9Sstevel@tonic-gate } 1525*7c478bd9Sstevel@tonic-gate 1526*7c478bd9Sstevel@tonic-gate /* 1527*7c478bd9Sstevel@tonic-gate * Unlink a detached clone transport handle from a master 1528*7c478bd9Sstevel@tonic-gate * - decrement the thread count on the master 1529*7c478bd9Sstevel@tonic-gate * - if the transport is closing (xp_wq is NULL) call svc_xprt_cleanup(); 1530*7c478bd9Sstevel@tonic-gate * if this is the last thread on this transport then it will destroy 1531*7c478bd9Sstevel@tonic-gate * the transport. 1532*7c478bd9Sstevel@tonic-gate * - call a transport specific function to destroy the clone handle 1533*7c478bd9Sstevel@tonic-gate * - clear xp_master to avoid recursion. 1534*7c478bd9Sstevel@tonic-gate */ 1535*7c478bd9Sstevel@tonic-gate static void 1536*7c478bd9Sstevel@tonic-gate svc_clone_unlinkdetached(SVCXPRT *clone_xprt) 1537*7c478bd9Sstevel@tonic-gate { 1538*7c478bd9Sstevel@tonic-gate SVCMASTERXPRT *xprt = clone_xprt->xp_master; 1539*7c478bd9Sstevel@tonic-gate 1540*7c478bd9Sstevel@tonic-gate /* This must be a detached thread */ 1541*7c478bd9Sstevel@tonic-gate ASSERT(clone_xprt->xp_detached); 1542*7c478bd9Sstevel@tonic-gate ASSERT(xprt->xp_detached_threads > 0); 1543*7c478bd9Sstevel@tonic-gate ASSERT(xprt->xp_threads + xprt->xp_detached_threads > 0); 1544*7c478bd9Sstevel@tonic-gate 1545*7c478bd9Sstevel@tonic-gate /* Grab xprt->xp_thread_lock and decrement link counts */ 1546*7c478bd9Sstevel@tonic-gate mutex_enter(&xprt->xp_thread_lock); 1547*7c478bd9Sstevel@tonic-gate xprt->xp_detached_threads--; 1548*7c478bd9Sstevel@tonic-gate 1549*7c478bd9Sstevel@tonic-gate /* svc_xprt_cleanup() unlocks xp_thread_lock or destroys xprt */ 1550*7c478bd9Sstevel@tonic-gate if (xprt->xp_wq) 1551*7c478bd9Sstevel@tonic-gate mutex_exit(&xprt->xp_thread_lock); 1552*7c478bd9Sstevel@tonic-gate else 1553*7c478bd9Sstevel@tonic-gate svc_xprt_cleanup(xprt, TRUE); 1554*7c478bd9Sstevel@tonic-gate 1555*7c478bd9Sstevel@tonic-gate /* Call transport specific clone `destroy' function */ 1556*7c478bd9Sstevel@tonic-gate SVC_CLONE_DESTROY(clone_xprt); 1557*7c478bd9Sstevel@tonic-gate 1558*7c478bd9Sstevel@tonic-gate /* Clear xp_master */ 1559*7c478bd9Sstevel@tonic-gate clone_xprt->xp_master = NULL; 1560*7c478bd9Sstevel@tonic-gate } 1561*7c478bd9Sstevel@tonic-gate 1562*7c478bd9Sstevel@tonic-gate /* 1563*7c478bd9Sstevel@tonic-gate * Try to exit a non-detached service thread 1564*7c478bd9Sstevel@tonic-gate * - check if there are enough threads left 1565*7c478bd9Sstevel@tonic-gate * - if this thread (ie its clone transport handle) are linked 1566*7c478bd9Sstevel@tonic-gate * to a master transport then unlink it 1567*7c478bd9Sstevel@tonic-gate * - free the clone structure 1568*7c478bd9Sstevel@tonic-gate * - return to userland for thread exit 1569*7c478bd9Sstevel@tonic-gate * 1570*7c478bd9Sstevel@tonic-gate * If this is the last non-detached or the last thread on this 1571*7c478bd9Sstevel@tonic-gate * transport then the call to svc_clone_unlink() will, respectively, 1572*7c478bd9Sstevel@tonic-gate * close and/or destroy the transport. 1573*7c478bd9Sstevel@tonic-gate */ 1574*7c478bd9Sstevel@tonic-gate static void 1575*7c478bd9Sstevel@tonic-gate svc_thread_exit(SVCPOOL *pool, SVCXPRT *clone_xprt) 1576*7c478bd9Sstevel@tonic-gate { 1577*7c478bd9Sstevel@tonic-gate if (clone_xprt->xp_master) 1578*7c478bd9Sstevel@tonic-gate svc_clone_unlink(clone_xprt); 1579*7c478bd9Sstevel@tonic-gate svc_clone_free(clone_xprt); 1580*7c478bd9Sstevel@tonic-gate 1581*7c478bd9Sstevel@tonic-gate mutex_enter(&pool->p_thread_lock); 1582*7c478bd9Sstevel@tonic-gate pool->p_threads--; 1583*7c478bd9Sstevel@tonic-gate if (pool->p_closing && svc_pool_tryexit(pool)) 1584*7c478bd9Sstevel@tonic-gate /* return - thread exit will be handled at user level */ 1585*7c478bd9Sstevel@tonic-gate return; 1586*7c478bd9Sstevel@tonic-gate mutex_exit(&pool->p_thread_lock); 1587*7c478bd9Sstevel@tonic-gate 1588*7c478bd9Sstevel@tonic-gate /* return - thread exit will be handled at user level */ 1589*7c478bd9Sstevel@tonic-gate } 1590*7c478bd9Sstevel@tonic-gate 1591*7c478bd9Sstevel@tonic-gate /* 1592*7c478bd9Sstevel@tonic-gate * Exit a detached service thread that returned to svc_run 1593*7c478bd9Sstevel@tonic-gate * - decrement the `detached thread' count for the pool 1594*7c478bd9Sstevel@tonic-gate * - unlink the detached clone transport handle from the master 1595*7c478bd9Sstevel@tonic-gate * - free the clone structure 1596*7c478bd9Sstevel@tonic-gate * - return to userland for thread exit 1597*7c478bd9Sstevel@tonic-gate * 1598*7c478bd9Sstevel@tonic-gate * If this is the last thread on this transport then the call 1599*7c478bd9Sstevel@tonic-gate * to svc_clone_unlinkdetached() will destroy the transport. 1600*7c478bd9Sstevel@tonic-gate */ 1601*7c478bd9Sstevel@tonic-gate static void 1602*7c478bd9Sstevel@tonic-gate svc_thread_exitdetached(SVCPOOL *pool, SVCXPRT *clone_xprt) 1603*7c478bd9Sstevel@tonic-gate { 1604*7c478bd9Sstevel@tonic-gate /* This must be a detached thread */ 1605*7c478bd9Sstevel@tonic-gate ASSERT(clone_xprt->xp_master); 1606*7c478bd9Sstevel@tonic-gate ASSERT(clone_xprt->xp_detached); 1607*7c478bd9Sstevel@tonic-gate ASSERT(!MUTEX_HELD(&pool->p_thread_lock)); 1608*7c478bd9Sstevel@tonic-gate 1609*7c478bd9Sstevel@tonic-gate svc_clone_unlinkdetached(clone_xprt); 1610*7c478bd9Sstevel@tonic-gate svc_clone_free(clone_xprt); 1611*7c478bd9Sstevel@tonic-gate 1612*7c478bd9Sstevel@tonic-gate mutex_enter(&pool->p_thread_lock); 1613*7c478bd9Sstevel@tonic-gate 1614*7c478bd9Sstevel@tonic-gate ASSERT(pool->p_reserved_threads >= 0); 1615*7c478bd9Sstevel@tonic-gate ASSERT(pool->p_detached_threads > 0); 1616*7c478bd9Sstevel@tonic-gate 1617*7c478bd9Sstevel@tonic-gate pool->p_detached_threads--; 1618*7c478bd9Sstevel@tonic-gate if (pool->p_closing && svc_pool_tryexit(pool)) 1619*7c478bd9Sstevel@tonic-gate /* return - thread exit will be handled at user level */ 1620*7c478bd9Sstevel@tonic-gate return; 1621*7c478bd9Sstevel@tonic-gate mutex_exit(&pool->p_thread_lock); 1622*7c478bd9Sstevel@tonic-gate 1623*7c478bd9Sstevel@tonic-gate /* return - thread exit will be handled at user level */ 1624*7c478bd9Sstevel@tonic-gate } 1625*7c478bd9Sstevel@tonic-gate 1626*7c478bd9Sstevel@tonic-gate /* 1627*7c478bd9Sstevel@tonic-gate * PSARC 2003/523 Contract Private Interface 1628*7c478bd9Sstevel@tonic-gate * svc_wait 1629*7c478bd9Sstevel@tonic-gate * Changes must be reviewed by Solaris File Sharing 1630*7c478bd9Sstevel@tonic-gate * Changes must be communicated to contract-2003-523@sun.com 1631*7c478bd9Sstevel@tonic-gate */ 1632*7c478bd9Sstevel@tonic-gate int 1633*7c478bd9Sstevel@tonic-gate svc_wait(int id) 1634*7c478bd9Sstevel@tonic-gate { 1635*7c478bd9Sstevel@tonic-gate SVCPOOL *pool; 1636*7c478bd9Sstevel@tonic-gate int err = 0; 1637*7c478bd9Sstevel@tonic-gate struct svc_globals *svc; 1638*7c478bd9Sstevel@tonic-gate 1639*7c478bd9Sstevel@tonic-gate svc = zone_getspecific(svc_zone_key, curproc->p_zone); 1640*7c478bd9Sstevel@tonic-gate mutex_enter(&svc->svc_plock); 1641*7c478bd9Sstevel@tonic-gate pool = svc_pool_find(svc, id); 1642*7c478bd9Sstevel@tonic-gate mutex_exit(&svc->svc_plock); 1643*7c478bd9Sstevel@tonic-gate 1644*7c478bd9Sstevel@tonic-gate if (pool == NULL) 1645*7c478bd9Sstevel@tonic-gate return (ENOENT); 1646*7c478bd9Sstevel@tonic-gate 1647*7c478bd9Sstevel@tonic-gate mutex_enter(&pool->p_user_lock); 1648*7c478bd9Sstevel@tonic-gate 1649*7c478bd9Sstevel@tonic-gate /* Check if there's already a user thread waiting on this pool */ 1650*7c478bd9Sstevel@tonic-gate if (pool->p_user_waiting) { 1651*7c478bd9Sstevel@tonic-gate mutex_exit(&pool->p_user_lock); 1652*7c478bd9Sstevel@tonic-gate return (EBUSY); 1653*7c478bd9Sstevel@tonic-gate } 1654*7c478bd9Sstevel@tonic-gate 1655*7c478bd9Sstevel@tonic-gate pool->p_user_waiting = TRUE; 1656*7c478bd9Sstevel@tonic-gate 1657*7c478bd9Sstevel@tonic-gate /* Go to sleep, waiting for the signaled flag. */ 1658*7c478bd9Sstevel@tonic-gate while (!pool->p_signal_create_thread && !pool->p_user_exit) { 1659*7c478bd9Sstevel@tonic-gate if (cv_wait_sig(&pool->p_user_cv, &pool->p_user_lock) == 0) { 1660*7c478bd9Sstevel@tonic-gate /* Interrupted, return to handle exit or signal */ 1661*7c478bd9Sstevel@tonic-gate pool->p_user_waiting = FALSE; 1662*7c478bd9Sstevel@tonic-gate pool->p_signal_create_thread = FALSE; 1663*7c478bd9Sstevel@tonic-gate mutex_exit(&pool->p_user_lock); 1664*7c478bd9Sstevel@tonic-gate 1665*7c478bd9Sstevel@tonic-gate /* 1666*7c478bd9Sstevel@tonic-gate * Thread has been interrupted and therefore 1667*7c478bd9Sstevel@tonic-gate * the service daemon is leaving as well so 1668*7c478bd9Sstevel@tonic-gate * let's go ahead and remove the service 1669*7c478bd9Sstevel@tonic-gate * pool at this time. 1670*7c478bd9Sstevel@tonic-gate */ 1671*7c478bd9Sstevel@tonic-gate mutex_enter(&svc->svc_plock); 1672*7c478bd9Sstevel@tonic-gate svc_pool_unregister(svc, pool); 1673*7c478bd9Sstevel@tonic-gate mutex_exit(&svc->svc_plock); 1674*7c478bd9Sstevel@tonic-gate 1675*7c478bd9Sstevel@tonic-gate return (EINTR); 1676*7c478bd9Sstevel@tonic-gate } 1677*7c478bd9Sstevel@tonic-gate } 1678*7c478bd9Sstevel@tonic-gate 1679*7c478bd9Sstevel@tonic-gate pool->p_signal_create_thread = FALSE; 1680*7c478bd9Sstevel@tonic-gate pool->p_user_waiting = FALSE; 1681*7c478bd9Sstevel@tonic-gate 1682*7c478bd9Sstevel@tonic-gate /* 1683*7c478bd9Sstevel@tonic-gate * About to exit the service pool. Set return value 1684*7c478bd9Sstevel@tonic-gate * to let the userland code know our intent. Signal 1685*7c478bd9Sstevel@tonic-gate * svc_thread_creator() so that it can clean up the 1686*7c478bd9Sstevel@tonic-gate * pool structure. 1687*7c478bd9Sstevel@tonic-gate */ 1688*7c478bd9Sstevel@tonic-gate if (pool->p_user_exit) { 1689*7c478bd9Sstevel@tonic-gate err = ECANCELED; 1690*7c478bd9Sstevel@tonic-gate cv_signal(&pool->p_user_cv); 1691*7c478bd9Sstevel@tonic-gate } 1692*7c478bd9Sstevel@tonic-gate 1693*7c478bd9Sstevel@tonic-gate mutex_exit(&pool->p_user_lock); 1694*7c478bd9Sstevel@tonic-gate 1695*7c478bd9Sstevel@tonic-gate /* Return to userland with error code, for possible thread creation. */ 1696*7c478bd9Sstevel@tonic-gate return (err); 1697*7c478bd9Sstevel@tonic-gate } 1698*7c478bd9Sstevel@tonic-gate 1699*7c478bd9Sstevel@tonic-gate /* 1700*7c478bd9Sstevel@tonic-gate * `Service threads' creator thread. 1701*7c478bd9Sstevel@tonic-gate * The creator thread waits for a signal to create new thread. 1702*7c478bd9Sstevel@tonic-gate */ 1703*7c478bd9Sstevel@tonic-gate static void 1704*7c478bd9Sstevel@tonic-gate svc_thread_creator(SVCPOOL *pool) 1705*7c478bd9Sstevel@tonic-gate { 1706*7c478bd9Sstevel@tonic-gate callb_cpr_t cpr_info; /* CPR info for the creator thread */ 1707*7c478bd9Sstevel@tonic-gate 1708*7c478bd9Sstevel@tonic-gate CALLB_CPR_INIT(&cpr_info, &pool->p_creator_lock, callb_generic_cpr, 1709*7c478bd9Sstevel@tonic-gate "svc_thread_creator"); 1710*7c478bd9Sstevel@tonic-gate 1711*7c478bd9Sstevel@tonic-gate for (;;) { 1712*7c478bd9Sstevel@tonic-gate mutex_enter(&pool->p_creator_lock); 1713*7c478bd9Sstevel@tonic-gate 1714*7c478bd9Sstevel@tonic-gate /* Check if someone set the exit flag */ 1715*7c478bd9Sstevel@tonic-gate if (pool->p_creator_exit) 1716*7c478bd9Sstevel@tonic-gate break; 1717*7c478bd9Sstevel@tonic-gate 1718*7c478bd9Sstevel@tonic-gate /* Clear the `signaled' flag and go asleep */ 1719*7c478bd9Sstevel@tonic-gate pool->p_creator_signaled = FALSE; 1720*7c478bd9Sstevel@tonic-gate 1721*7c478bd9Sstevel@tonic-gate CALLB_CPR_SAFE_BEGIN(&cpr_info); 1722*7c478bd9Sstevel@tonic-gate cv_wait(&pool->p_creator_cv, &pool->p_creator_lock); 1723*7c478bd9Sstevel@tonic-gate CALLB_CPR_SAFE_END(&cpr_info, &pool->p_creator_lock); 1724*7c478bd9Sstevel@tonic-gate 1725*7c478bd9Sstevel@tonic-gate /* Check if someone signaled to exit */ 1726*7c478bd9Sstevel@tonic-gate if (pool->p_creator_exit) 1727*7c478bd9Sstevel@tonic-gate break; 1728*7c478bd9Sstevel@tonic-gate 1729*7c478bd9Sstevel@tonic-gate mutex_exit(&pool->p_creator_lock); 1730*7c478bd9Sstevel@tonic-gate 1731*7c478bd9Sstevel@tonic-gate mutex_enter(&pool->p_thread_lock); 1732*7c478bd9Sstevel@tonic-gate 1733*7c478bd9Sstevel@tonic-gate /* 1734*7c478bd9Sstevel@tonic-gate * When the pool is in closing state and all the transports 1735*7c478bd9Sstevel@tonic-gate * are gone the creator should not create any new threads. 1736*7c478bd9Sstevel@tonic-gate */ 1737*7c478bd9Sstevel@tonic-gate if (pool->p_closing) { 1738*7c478bd9Sstevel@tonic-gate rw_enter(&pool->p_lrwlock, RW_READER); 1739*7c478bd9Sstevel@tonic-gate if (pool->p_lcount == 0) { 1740*7c478bd9Sstevel@tonic-gate rw_exit(&pool->p_lrwlock); 1741*7c478bd9Sstevel@tonic-gate mutex_exit(&pool->p_thread_lock); 1742*7c478bd9Sstevel@tonic-gate continue; 1743*7c478bd9Sstevel@tonic-gate } 1744*7c478bd9Sstevel@tonic-gate rw_exit(&pool->p_lrwlock); 1745*7c478bd9Sstevel@tonic-gate } 1746*7c478bd9Sstevel@tonic-gate 1747*7c478bd9Sstevel@tonic-gate /* 1748*7c478bd9Sstevel@tonic-gate * Create a new service thread now. 1749*7c478bd9Sstevel@tonic-gate */ 1750*7c478bd9Sstevel@tonic-gate ASSERT(pool->p_reserved_threads >= 0); 1751*7c478bd9Sstevel@tonic-gate ASSERT(pool->p_detached_threads >= 0); 1752*7c478bd9Sstevel@tonic-gate 1753*7c478bd9Sstevel@tonic-gate if (pool->p_threads + pool->p_detached_threads < 1754*7c478bd9Sstevel@tonic-gate pool->p_maxthreads) { 1755*7c478bd9Sstevel@tonic-gate /* 1756*7c478bd9Sstevel@tonic-gate * Signal the service pool wait thread 1757*7c478bd9Sstevel@tonic-gate * only if it hasn't already been signaled. 1758*7c478bd9Sstevel@tonic-gate */ 1759*7c478bd9Sstevel@tonic-gate mutex_enter(&pool->p_user_lock); 1760*7c478bd9Sstevel@tonic-gate if (pool->p_signal_create_thread == FALSE) { 1761*7c478bd9Sstevel@tonic-gate pool->p_signal_create_thread = TRUE; 1762*7c478bd9Sstevel@tonic-gate cv_signal(&pool->p_user_cv); 1763*7c478bd9Sstevel@tonic-gate } 1764*7c478bd9Sstevel@tonic-gate mutex_exit(&pool->p_user_lock); 1765*7c478bd9Sstevel@tonic-gate 1766*7c478bd9Sstevel@tonic-gate } 1767*7c478bd9Sstevel@tonic-gate 1768*7c478bd9Sstevel@tonic-gate mutex_exit(&pool->p_thread_lock); 1769*7c478bd9Sstevel@tonic-gate } 1770*7c478bd9Sstevel@tonic-gate 1771*7c478bd9Sstevel@tonic-gate /* 1772*7c478bd9Sstevel@tonic-gate * Pool is closed. Cleanup and exit. 1773*7c478bd9Sstevel@tonic-gate */ 1774*7c478bd9Sstevel@tonic-gate 1775*7c478bd9Sstevel@tonic-gate /* Signal userland creator thread that it can stop now. */ 1776*7c478bd9Sstevel@tonic-gate mutex_enter(&pool->p_user_lock); 1777*7c478bd9Sstevel@tonic-gate pool->p_user_exit = TRUE; 1778*7c478bd9Sstevel@tonic-gate cv_broadcast(&pool->p_user_cv); 1779*7c478bd9Sstevel@tonic-gate mutex_exit(&pool->p_user_lock); 1780*7c478bd9Sstevel@tonic-gate 1781*7c478bd9Sstevel@tonic-gate /* Wait for svc_wait() to be done with the pool */ 1782*7c478bd9Sstevel@tonic-gate mutex_enter(&pool->p_user_lock); 1783*7c478bd9Sstevel@tonic-gate while (pool->p_user_waiting) { 1784*7c478bd9Sstevel@tonic-gate CALLB_CPR_SAFE_BEGIN(&cpr_info); 1785*7c478bd9Sstevel@tonic-gate cv_wait(&pool->p_user_cv, &pool->p_user_lock); 1786*7c478bd9Sstevel@tonic-gate CALLB_CPR_SAFE_END(&cpr_info, &pool->p_creator_lock); 1787*7c478bd9Sstevel@tonic-gate } 1788*7c478bd9Sstevel@tonic-gate mutex_exit(&pool->p_user_lock); 1789*7c478bd9Sstevel@tonic-gate 1790*7c478bd9Sstevel@tonic-gate CALLB_CPR_EXIT(&cpr_info); 1791*7c478bd9Sstevel@tonic-gate svc_pool_cleanup(pool); 1792*7c478bd9Sstevel@tonic-gate zthread_exit(); 1793*7c478bd9Sstevel@tonic-gate } 1794*7c478bd9Sstevel@tonic-gate 1795*7c478bd9Sstevel@tonic-gate /* 1796*7c478bd9Sstevel@tonic-gate * If the creator thread is idle signal it to create 1797*7c478bd9Sstevel@tonic-gate * a new service thread. 1798*7c478bd9Sstevel@tonic-gate */ 1799*7c478bd9Sstevel@tonic-gate static void 1800*7c478bd9Sstevel@tonic-gate svc_creator_signal(SVCPOOL *pool) 1801*7c478bd9Sstevel@tonic-gate { 1802*7c478bd9Sstevel@tonic-gate mutex_enter(&pool->p_creator_lock); 1803*7c478bd9Sstevel@tonic-gate if (pool->p_creator_signaled == FALSE) { 1804*7c478bd9Sstevel@tonic-gate pool->p_creator_signaled = TRUE; 1805*7c478bd9Sstevel@tonic-gate cv_signal(&pool->p_creator_cv); 1806*7c478bd9Sstevel@tonic-gate } 1807*7c478bd9Sstevel@tonic-gate mutex_exit(&pool->p_creator_lock); 1808*7c478bd9Sstevel@tonic-gate } 1809*7c478bd9Sstevel@tonic-gate 1810*7c478bd9Sstevel@tonic-gate /* 1811*7c478bd9Sstevel@tonic-gate * Notify the creator thread to clean up and exit. 1812*7c478bd9Sstevel@tonic-gate */ 1813*7c478bd9Sstevel@tonic-gate static void 1814*7c478bd9Sstevel@tonic-gate svc_creator_signalexit(SVCPOOL *pool) 1815*7c478bd9Sstevel@tonic-gate { 1816*7c478bd9Sstevel@tonic-gate mutex_enter(&pool->p_creator_lock); 1817*7c478bd9Sstevel@tonic-gate pool->p_creator_exit = TRUE; 1818*7c478bd9Sstevel@tonic-gate cv_signal(&pool->p_creator_cv); 1819*7c478bd9Sstevel@tonic-gate mutex_exit(&pool->p_creator_lock); 1820*7c478bd9Sstevel@tonic-gate } 1821*7c478bd9Sstevel@tonic-gate 1822*7c478bd9Sstevel@tonic-gate /* 1823*7c478bd9Sstevel@tonic-gate * Polling part of the svc_run(). 1824*7c478bd9Sstevel@tonic-gate * - search for a transport with a pending request 1825*7c478bd9Sstevel@tonic-gate * - when one is found then latch the request lock and return to svc_run() 1826*7c478bd9Sstevel@tonic-gate * - if there is no request go asleep and wait for a signal 1827*7c478bd9Sstevel@tonic-gate * - handle two exceptions: 1828*7c478bd9Sstevel@tonic-gate * a) current transport is closing 1829*7c478bd9Sstevel@tonic-gate * b) timeout waiting for a new request 1830*7c478bd9Sstevel@tonic-gate * in both cases return to svc_run() 1831*7c478bd9Sstevel@tonic-gate */ 1832*7c478bd9Sstevel@tonic-gate static SVCMASTERXPRT * 1833*7c478bd9Sstevel@tonic-gate svc_poll(SVCPOOL *pool, SVCMASTERXPRT *xprt, SVCXPRT *clone_xprt) 1834*7c478bd9Sstevel@tonic-gate { 1835*7c478bd9Sstevel@tonic-gate /* 1836*7c478bd9Sstevel@tonic-gate * Main loop iterates until 1837*7c478bd9Sstevel@tonic-gate * a) we find a pending request, 1838*7c478bd9Sstevel@tonic-gate * b) detect that the current transport is closing 1839*7c478bd9Sstevel@tonic-gate * c) time out waiting for a new request. 1840*7c478bd9Sstevel@tonic-gate */ 1841*7c478bd9Sstevel@tonic-gate for (;;) { 1842*7c478bd9Sstevel@tonic-gate SVCMASTERXPRT *next; 1843*7c478bd9Sstevel@tonic-gate clock_t timeleft; 1844*7c478bd9Sstevel@tonic-gate 1845*7c478bd9Sstevel@tonic-gate /* 1846*7c478bd9Sstevel@tonic-gate * Step 1. 1847*7c478bd9Sstevel@tonic-gate * Check if there is a pending request on the current 1848*7c478bd9Sstevel@tonic-gate * transport handle so that we can avoid cloning. 1849*7c478bd9Sstevel@tonic-gate * If so then decrement the `pending-request' count for 1850*7c478bd9Sstevel@tonic-gate * the pool and return to svc_run(). 1851*7c478bd9Sstevel@tonic-gate * 1852*7c478bd9Sstevel@tonic-gate * We need to prevent a potential starvation. When 1853*7c478bd9Sstevel@tonic-gate * a selected transport has all pending requests coming in 1854*7c478bd9Sstevel@tonic-gate * all the time then the service threads will never switch to 1855*7c478bd9Sstevel@tonic-gate * another transport. With a limited number of service 1856*7c478bd9Sstevel@tonic-gate * threads some transports may be never serviced. 1857*7c478bd9Sstevel@tonic-gate * To prevent such a scenario we pick up at most 1858*7c478bd9Sstevel@tonic-gate * pool->p_max_same_xprt requests from the same transport 1859*7c478bd9Sstevel@tonic-gate * and then take a hint from the xprt-ready queue or walk 1860*7c478bd9Sstevel@tonic-gate * the transport list. 1861*7c478bd9Sstevel@tonic-gate */ 1862*7c478bd9Sstevel@tonic-gate if (xprt && xprt->xp_req_head && (!pool->p_qoverflow || 1863*7c478bd9Sstevel@tonic-gate clone_xprt->xp_same_xprt++ < pool->p_max_same_xprt)) { 1864*7c478bd9Sstevel@tonic-gate mutex_enter(&xprt->xp_req_lock); 1865*7c478bd9Sstevel@tonic-gate if (xprt->xp_req_head) { 1866*7c478bd9Sstevel@tonic-gate mutex_enter(&pool->p_req_lock); 1867*7c478bd9Sstevel@tonic-gate pool->p_reqs--; 1868*7c478bd9Sstevel@tonic-gate mutex_exit(&pool->p_req_lock); 1869*7c478bd9Sstevel@tonic-gate 1870*7c478bd9Sstevel@tonic-gate return (xprt); 1871*7c478bd9Sstevel@tonic-gate } 1872*7c478bd9Sstevel@tonic-gate mutex_exit(&xprt->xp_req_lock); 1873*7c478bd9Sstevel@tonic-gate } 1874*7c478bd9Sstevel@tonic-gate clone_xprt->xp_same_xprt = 0; 1875*7c478bd9Sstevel@tonic-gate 1876*7c478bd9Sstevel@tonic-gate /* 1877*7c478bd9Sstevel@tonic-gate * Step 2. 1878*7c478bd9Sstevel@tonic-gate * If there is no request on the current transport try to 1879*7c478bd9Sstevel@tonic-gate * find another transport with a pending request. 1880*7c478bd9Sstevel@tonic-gate */ 1881*7c478bd9Sstevel@tonic-gate mutex_enter(&pool->p_req_lock); 1882*7c478bd9Sstevel@tonic-gate pool->p_walkers++; 1883*7c478bd9Sstevel@tonic-gate mutex_exit(&pool->p_req_lock); 1884*7c478bd9Sstevel@tonic-gate 1885*7c478bd9Sstevel@tonic-gate /* 1886*7c478bd9Sstevel@tonic-gate * Make sure that transports will not be destroyed just 1887*7c478bd9Sstevel@tonic-gate * while we are checking them. 1888*7c478bd9Sstevel@tonic-gate */ 1889*7c478bd9Sstevel@tonic-gate rw_enter(&pool->p_lrwlock, RW_READER); 1890*7c478bd9Sstevel@tonic-gate 1891*7c478bd9Sstevel@tonic-gate for (;;) { 1892*7c478bd9Sstevel@tonic-gate SVCMASTERXPRT *hint; 1893*7c478bd9Sstevel@tonic-gate 1894*7c478bd9Sstevel@tonic-gate /* 1895*7c478bd9Sstevel@tonic-gate * Get the next transport from the xprt-ready queue. 1896*7c478bd9Sstevel@tonic-gate * This is a hint. There is no guarantee that the 1897*7c478bd9Sstevel@tonic-gate * transport still has a pending request since it 1898*7c478bd9Sstevel@tonic-gate * could be picked up by another thread in step 1. 1899*7c478bd9Sstevel@tonic-gate * 1900*7c478bd9Sstevel@tonic-gate * If the transport has a pending request then keep 1901*7c478bd9Sstevel@tonic-gate * it locked. Decrement the `pending-requests' for 1902*7c478bd9Sstevel@tonic-gate * the pool and `walking-threads' counts, and return 1903*7c478bd9Sstevel@tonic-gate * to svc_run(). 1904*7c478bd9Sstevel@tonic-gate */ 1905*7c478bd9Sstevel@tonic-gate hint = svc_xprt_qget(pool); 1906*7c478bd9Sstevel@tonic-gate 1907*7c478bd9Sstevel@tonic-gate if (hint && hint->xp_req_head) { 1908*7c478bd9Sstevel@tonic-gate mutex_enter(&hint->xp_req_lock); 1909*7c478bd9Sstevel@tonic-gate if (hint->xp_req_head) { 1910*7c478bd9Sstevel@tonic-gate rw_exit(&pool->p_lrwlock); 1911*7c478bd9Sstevel@tonic-gate 1912*7c478bd9Sstevel@tonic-gate mutex_enter(&pool->p_req_lock); 1913*7c478bd9Sstevel@tonic-gate pool->p_reqs--; 1914*7c478bd9Sstevel@tonic-gate pool->p_walkers--; 1915*7c478bd9Sstevel@tonic-gate mutex_exit(&pool->p_req_lock); 1916*7c478bd9Sstevel@tonic-gate 1917*7c478bd9Sstevel@tonic-gate return (hint); 1918*7c478bd9Sstevel@tonic-gate } 1919*7c478bd9Sstevel@tonic-gate mutex_exit(&hint->xp_req_lock); 1920*7c478bd9Sstevel@tonic-gate } 1921*7c478bd9Sstevel@tonic-gate 1922*7c478bd9Sstevel@tonic-gate /* 1923*7c478bd9Sstevel@tonic-gate * If there was no hint in the xprt-ready queue then 1924*7c478bd9Sstevel@tonic-gate * - if there is less pending requests than polling 1925*7c478bd9Sstevel@tonic-gate * threads go asleep 1926*7c478bd9Sstevel@tonic-gate * - otherwise check if there was an overflow in the 1927*7c478bd9Sstevel@tonic-gate * xprt-ready queue; if so, then we need to break 1928*7c478bd9Sstevel@tonic-gate * the `drain' mode 1929*7c478bd9Sstevel@tonic-gate */ 1930*7c478bd9Sstevel@tonic-gate if (hint == NULL) { 1931*7c478bd9Sstevel@tonic-gate if (pool->p_reqs < pool->p_walkers) { 1932*7c478bd9Sstevel@tonic-gate mutex_enter(&pool->p_req_lock); 1933*7c478bd9Sstevel@tonic-gate if (pool->p_reqs < pool->p_walkers) 1934*7c478bd9Sstevel@tonic-gate goto sleep; 1935*7c478bd9Sstevel@tonic-gate mutex_exit(&pool->p_req_lock); 1936*7c478bd9Sstevel@tonic-gate } 1937*7c478bd9Sstevel@tonic-gate if (pool->p_qoverflow) { 1938*7c478bd9Sstevel@tonic-gate break; 1939*7c478bd9Sstevel@tonic-gate } 1940*7c478bd9Sstevel@tonic-gate } 1941*7c478bd9Sstevel@tonic-gate } 1942*7c478bd9Sstevel@tonic-gate 1943*7c478bd9Sstevel@tonic-gate /* 1944*7c478bd9Sstevel@tonic-gate * If there was an overflow in the xprt-ready queue then we 1945*7c478bd9Sstevel@tonic-gate * need to switch to the `drain' mode, i.e. walk through the 1946*7c478bd9Sstevel@tonic-gate * pool's transport list and search for a transport with a 1947*7c478bd9Sstevel@tonic-gate * pending request. If we manage to drain all the pending 1948*7c478bd9Sstevel@tonic-gate * requests then we can clear the overflow flag. This will 1949*7c478bd9Sstevel@tonic-gate * switch svc_poll() back to taking hints from the xprt-ready 1950*7c478bd9Sstevel@tonic-gate * queue (which is generally more efficient). 1951*7c478bd9Sstevel@tonic-gate * 1952*7c478bd9Sstevel@tonic-gate * If there are no registered transports simply go asleep. 1953*7c478bd9Sstevel@tonic-gate */ 1954*7c478bd9Sstevel@tonic-gate if (xprt == NULL && pool->p_lhead == NULL) { 1955*7c478bd9Sstevel@tonic-gate mutex_enter(&pool->p_req_lock); 1956*7c478bd9Sstevel@tonic-gate goto sleep; 1957*7c478bd9Sstevel@tonic-gate } 1958*7c478bd9Sstevel@tonic-gate 1959*7c478bd9Sstevel@tonic-gate /* 1960*7c478bd9Sstevel@tonic-gate * `Walk' through the pool's list of master server 1961*7c478bd9Sstevel@tonic-gate * transport handles. Continue to loop until there are less 1962*7c478bd9Sstevel@tonic-gate * looping threads then pending requests. 1963*7c478bd9Sstevel@tonic-gate */ 1964*7c478bd9Sstevel@tonic-gate next = xprt ? xprt->xp_next : pool->p_lhead; 1965*7c478bd9Sstevel@tonic-gate 1966*7c478bd9Sstevel@tonic-gate for (;;) { 1967*7c478bd9Sstevel@tonic-gate /* 1968*7c478bd9Sstevel@tonic-gate * Check if there is a request on this transport. 1969*7c478bd9Sstevel@tonic-gate * 1970*7c478bd9Sstevel@tonic-gate * Since blocking on a locked mutex is very expensive 1971*7c478bd9Sstevel@tonic-gate * check for a request without a lock first. If we miss 1972*7c478bd9Sstevel@tonic-gate * a request that is just being delivered but this will 1973*7c478bd9Sstevel@tonic-gate * cost at most one full walk through the list. 1974*7c478bd9Sstevel@tonic-gate */ 1975*7c478bd9Sstevel@tonic-gate if (next->xp_req_head) { 1976*7c478bd9Sstevel@tonic-gate /* 1977*7c478bd9Sstevel@tonic-gate * Check again, now with a lock. 1978*7c478bd9Sstevel@tonic-gate */ 1979*7c478bd9Sstevel@tonic-gate mutex_enter(&next->xp_req_lock); 1980*7c478bd9Sstevel@tonic-gate if (next->xp_req_head) { 1981*7c478bd9Sstevel@tonic-gate rw_exit(&pool->p_lrwlock); 1982*7c478bd9Sstevel@tonic-gate 1983*7c478bd9Sstevel@tonic-gate mutex_enter(&pool->p_req_lock); 1984*7c478bd9Sstevel@tonic-gate pool->p_reqs--; 1985*7c478bd9Sstevel@tonic-gate pool->p_walkers--; 1986*7c478bd9Sstevel@tonic-gate mutex_exit(&pool->p_req_lock); 1987*7c478bd9Sstevel@tonic-gate 1988*7c478bd9Sstevel@tonic-gate return (next); 1989*7c478bd9Sstevel@tonic-gate } 1990*7c478bd9Sstevel@tonic-gate mutex_exit(&next->xp_req_lock); 1991*7c478bd9Sstevel@tonic-gate } 1992*7c478bd9Sstevel@tonic-gate 1993*7c478bd9Sstevel@tonic-gate /* 1994*7c478bd9Sstevel@tonic-gate * Continue to `walk' through the pool's 1995*7c478bd9Sstevel@tonic-gate * transport list until there is less requests 1996*7c478bd9Sstevel@tonic-gate * than walkers. Check this condition without 1997*7c478bd9Sstevel@tonic-gate * a lock first to avoid contention on a mutex. 1998*7c478bd9Sstevel@tonic-gate */ 1999*7c478bd9Sstevel@tonic-gate if (pool->p_reqs < pool->p_walkers) { 2000*7c478bd9Sstevel@tonic-gate /* 2001*7c478bd9Sstevel@tonic-gate * Check again, now with the lock. 2002*7c478bd9Sstevel@tonic-gate * If all the pending requests have been 2003*7c478bd9Sstevel@tonic-gate * picked up than clear the overflow flag. 2004*7c478bd9Sstevel@tonic-gate */ 2005*7c478bd9Sstevel@tonic-gate mutex_enter(&pool->p_req_lock); 2006*7c478bd9Sstevel@tonic-gate if (pool->p_reqs <= 0) 2007*7c478bd9Sstevel@tonic-gate svc_xprt_qreset(pool); 2008*7c478bd9Sstevel@tonic-gate if (pool->p_reqs < pool->p_walkers) 2009*7c478bd9Sstevel@tonic-gate break; /* goto sleep */ 2010*7c478bd9Sstevel@tonic-gate mutex_exit(&pool->p_req_lock); 2011*7c478bd9Sstevel@tonic-gate } 2012*7c478bd9Sstevel@tonic-gate 2013*7c478bd9Sstevel@tonic-gate next = next->xp_next; 2014*7c478bd9Sstevel@tonic-gate } 2015*7c478bd9Sstevel@tonic-gate 2016*7c478bd9Sstevel@tonic-gate sleep: 2017*7c478bd9Sstevel@tonic-gate /* 2018*7c478bd9Sstevel@tonic-gate * No work to do. Stop the `walk' and go asleep. 2019*7c478bd9Sstevel@tonic-gate * Decrement the `walking-threads' count for the pool. 2020*7c478bd9Sstevel@tonic-gate */ 2021*7c478bd9Sstevel@tonic-gate pool->p_walkers--; 2022*7c478bd9Sstevel@tonic-gate rw_exit(&pool->p_lrwlock); 2023*7c478bd9Sstevel@tonic-gate 2024*7c478bd9Sstevel@tonic-gate /* 2025*7c478bd9Sstevel@tonic-gate * Count us as asleep, mark this thread as safe 2026*7c478bd9Sstevel@tonic-gate * for suspend and wait for a request. 2027*7c478bd9Sstevel@tonic-gate */ 2028*7c478bd9Sstevel@tonic-gate pool->p_asleep++; 2029*7c478bd9Sstevel@tonic-gate timeleft = cv_timedwait_sig(&pool->p_req_cv, &pool->p_req_lock, 2030*7c478bd9Sstevel@tonic-gate pool->p_timeout + lbolt); 2031*7c478bd9Sstevel@tonic-gate 2032*7c478bd9Sstevel@tonic-gate /* 2033*7c478bd9Sstevel@tonic-gate * If the drowsy flag is on this means that 2034*7c478bd9Sstevel@tonic-gate * someone has signaled a wakeup. In such a case 2035*7c478bd9Sstevel@tonic-gate * the `asleep-threads' count has already updated 2036*7c478bd9Sstevel@tonic-gate * so just clear the flag. 2037*7c478bd9Sstevel@tonic-gate * 2038*7c478bd9Sstevel@tonic-gate * If the drowsy flag is off then we need to update 2039*7c478bd9Sstevel@tonic-gate * the `asleep-threads' count. 2040*7c478bd9Sstevel@tonic-gate */ 2041*7c478bd9Sstevel@tonic-gate if (pool->p_drowsy) { 2042*7c478bd9Sstevel@tonic-gate pool->p_drowsy = FALSE; 2043*7c478bd9Sstevel@tonic-gate /* 2044*7c478bd9Sstevel@tonic-gate * If the thread is here because it timedout, 2045*7c478bd9Sstevel@tonic-gate * instead of returning SVC_ETIMEDOUT, it is 2046*7c478bd9Sstevel@tonic-gate * time to do some more work. 2047*7c478bd9Sstevel@tonic-gate */ 2048*7c478bd9Sstevel@tonic-gate if (timeleft == -1) 2049*7c478bd9Sstevel@tonic-gate timeleft = 1; 2050*7c478bd9Sstevel@tonic-gate } else { 2051*7c478bd9Sstevel@tonic-gate pool->p_asleep--; 2052*7c478bd9Sstevel@tonic-gate } 2053*7c478bd9Sstevel@tonic-gate mutex_exit(&pool->p_req_lock); 2054*7c478bd9Sstevel@tonic-gate 2055*7c478bd9Sstevel@tonic-gate /* 2056*7c478bd9Sstevel@tonic-gate * If we received a signal while waiting for a 2057*7c478bd9Sstevel@tonic-gate * request, inform svc_run(), so that we can return 2058*7c478bd9Sstevel@tonic-gate * to user level and restart the call. 2059*7c478bd9Sstevel@tonic-gate */ 2060*7c478bd9Sstevel@tonic-gate if (timeleft == 0) 2061*7c478bd9Sstevel@tonic-gate return (SVC_EINTR); 2062*7c478bd9Sstevel@tonic-gate 2063*7c478bd9Sstevel@tonic-gate /* 2064*7c478bd9Sstevel@tonic-gate * If the current transport is gone then notify 2065*7c478bd9Sstevel@tonic-gate * svc_run() to unlink from it. 2066*7c478bd9Sstevel@tonic-gate */ 2067*7c478bd9Sstevel@tonic-gate if (xprt && xprt->xp_wq == NULL) 2068*7c478bd9Sstevel@tonic-gate return (SVC_EXPRTGONE); 2069*7c478bd9Sstevel@tonic-gate 2070*7c478bd9Sstevel@tonic-gate /* 2071*7c478bd9Sstevel@tonic-gate * If we have timed out waiting for a request inform 2072*7c478bd9Sstevel@tonic-gate * svc_run() that we probably don't need this thread. 2073*7c478bd9Sstevel@tonic-gate */ 2074*7c478bd9Sstevel@tonic-gate if (timeleft == -1) 2075*7c478bd9Sstevel@tonic-gate return (SVC_ETIMEDOUT); 2076*7c478bd9Sstevel@tonic-gate } 2077*7c478bd9Sstevel@tonic-gate } 2078*7c478bd9Sstevel@tonic-gate 2079*7c478bd9Sstevel@tonic-gate /* 2080*7c478bd9Sstevel@tonic-gate * Main loop of the kernel RPC server 2081*7c478bd9Sstevel@tonic-gate * - wait for input (find a transport with a pending request). 2082*7c478bd9Sstevel@tonic-gate * - dequeue the request 2083*7c478bd9Sstevel@tonic-gate * - call a registered server routine to process the requests 2084*7c478bd9Sstevel@tonic-gate * 2085*7c478bd9Sstevel@tonic-gate * There can many threads running concurrently in this loop 2086*7c478bd9Sstevel@tonic-gate * on the same or on different transports. 2087*7c478bd9Sstevel@tonic-gate */ 2088*7c478bd9Sstevel@tonic-gate static int 2089*7c478bd9Sstevel@tonic-gate svc_run(SVCPOOL *pool) 2090*7c478bd9Sstevel@tonic-gate { 2091*7c478bd9Sstevel@tonic-gate SVCMASTERXPRT *xprt = NULL; /* master transport handle */ 2092*7c478bd9Sstevel@tonic-gate SVCXPRT *clone_xprt; /* clone for this thread */ 2093*7c478bd9Sstevel@tonic-gate struct svc_globals *svc; 2094*7c478bd9Sstevel@tonic-gate proc_t *p = ttoproc(curthread); 2095*7c478bd9Sstevel@tonic-gate 2096*7c478bd9Sstevel@tonic-gate /* Allocate a clone transport handle for this thread */ 2097*7c478bd9Sstevel@tonic-gate clone_xprt = svc_clone_init(); 2098*7c478bd9Sstevel@tonic-gate 2099*7c478bd9Sstevel@tonic-gate /* 2100*7c478bd9Sstevel@tonic-gate * The loop iterates until the thread becomes 2101*7c478bd9Sstevel@tonic-gate * idle too long or the transport is gone. 2102*7c478bd9Sstevel@tonic-gate */ 2103*7c478bd9Sstevel@tonic-gate for (;;) { 2104*7c478bd9Sstevel@tonic-gate SVCMASTERXPRT *next; 2105*7c478bd9Sstevel@tonic-gate mblk_t *mp; 2106*7c478bd9Sstevel@tonic-gate 2107*7c478bd9Sstevel@tonic-gate TRACE_0(TR_FAC_KRPC, TR_SVC_RUN, "svc_run"); 2108*7c478bd9Sstevel@tonic-gate 2109*7c478bd9Sstevel@tonic-gate /* 2110*7c478bd9Sstevel@tonic-gate * If the process is exiting/killed, return 2111*7c478bd9Sstevel@tonic-gate * immediately without processing any more 2112*7c478bd9Sstevel@tonic-gate * requests. 2113*7c478bd9Sstevel@tonic-gate */ 2114*7c478bd9Sstevel@tonic-gate if (p->p_flag & (SEXITLWPS|SKILLED)) { 2115*7c478bd9Sstevel@tonic-gate svc_thread_exit(pool, clone_xprt); 2116*7c478bd9Sstevel@tonic-gate 2117*7c478bd9Sstevel@tonic-gate /* 2118*7c478bd9Sstevel@tonic-gate * Thread has been interrupted and therefore 2119*7c478bd9Sstevel@tonic-gate * the service daemon is leaving as well so 2120*7c478bd9Sstevel@tonic-gate * let's go ahead and remove the service 2121*7c478bd9Sstevel@tonic-gate * pool at this time. 2122*7c478bd9Sstevel@tonic-gate */ 2123*7c478bd9Sstevel@tonic-gate svc = zone_getspecific(svc_zone_key, curproc->p_zone); 2124*7c478bd9Sstevel@tonic-gate mutex_enter(&svc->svc_plock); 2125*7c478bd9Sstevel@tonic-gate svc_pool_unregister(svc, pool); 2126*7c478bd9Sstevel@tonic-gate mutex_exit(&svc->svc_plock); 2127*7c478bd9Sstevel@tonic-gate 2128*7c478bd9Sstevel@tonic-gate return (0); 2129*7c478bd9Sstevel@tonic-gate } 2130*7c478bd9Sstevel@tonic-gate 2131*7c478bd9Sstevel@tonic-gate /* Find a transport with a pending request */ 2132*7c478bd9Sstevel@tonic-gate next = svc_poll(pool, xprt, clone_xprt); 2133*7c478bd9Sstevel@tonic-gate 2134*7c478bd9Sstevel@tonic-gate /* 2135*7c478bd9Sstevel@tonic-gate * If svc_poll() finds a transport with a request 2136*7c478bd9Sstevel@tonic-gate * it latches xp_req_lock on it. Therefore we need 2137*7c478bd9Sstevel@tonic-gate * to dequeue the request and release the lock as 2138*7c478bd9Sstevel@tonic-gate * soon as possible. 2139*7c478bd9Sstevel@tonic-gate */ 2140*7c478bd9Sstevel@tonic-gate ASSERT(next != NULL && 2141*7c478bd9Sstevel@tonic-gate (next == SVC_EXPRTGONE || 2142*7c478bd9Sstevel@tonic-gate next == SVC_ETIMEDOUT || 2143*7c478bd9Sstevel@tonic-gate next == SVC_EINTR || 2144*7c478bd9Sstevel@tonic-gate MUTEX_HELD(&next->xp_req_lock))); 2145*7c478bd9Sstevel@tonic-gate 2146*7c478bd9Sstevel@tonic-gate /* Ooops! Current transport is closing. Unlink now */ 2147*7c478bd9Sstevel@tonic-gate if (next == SVC_EXPRTGONE) { 2148*7c478bd9Sstevel@tonic-gate svc_clone_unlink(clone_xprt); 2149*7c478bd9Sstevel@tonic-gate xprt = NULL; 2150*7c478bd9Sstevel@tonic-gate continue; 2151*7c478bd9Sstevel@tonic-gate } 2152*7c478bd9Sstevel@tonic-gate 2153*7c478bd9Sstevel@tonic-gate /* Ooops! Timeout while waiting for a request. Exit */ 2154*7c478bd9Sstevel@tonic-gate if (next == SVC_ETIMEDOUT) { 2155*7c478bd9Sstevel@tonic-gate svc_thread_exit(pool, clone_xprt); 2156*7c478bd9Sstevel@tonic-gate return (0); 2157*7c478bd9Sstevel@tonic-gate } 2158*7c478bd9Sstevel@tonic-gate 2159*7c478bd9Sstevel@tonic-gate /* 2160*7c478bd9Sstevel@tonic-gate * Interrupted by a signal while waiting for a 2161*7c478bd9Sstevel@tonic-gate * request. Return to userspace and restart. 2162*7c478bd9Sstevel@tonic-gate */ 2163*7c478bd9Sstevel@tonic-gate if (next == SVC_EINTR) { 2164*7c478bd9Sstevel@tonic-gate svc_thread_exit(pool, clone_xprt); 2165*7c478bd9Sstevel@tonic-gate 2166*7c478bd9Sstevel@tonic-gate /* 2167*7c478bd9Sstevel@tonic-gate * Thread has been interrupted and therefore 2168*7c478bd9Sstevel@tonic-gate * the service daemon is leaving as well so 2169*7c478bd9Sstevel@tonic-gate * let's go ahead and remove the service 2170*7c478bd9Sstevel@tonic-gate * pool at this time. 2171*7c478bd9Sstevel@tonic-gate */ 2172*7c478bd9Sstevel@tonic-gate svc = zone_getspecific(svc_zone_key, curproc->p_zone); 2173*7c478bd9Sstevel@tonic-gate mutex_enter(&svc->svc_plock); 2174*7c478bd9Sstevel@tonic-gate svc_pool_unregister(svc, pool); 2175*7c478bd9Sstevel@tonic-gate mutex_exit(&svc->svc_plock); 2176*7c478bd9Sstevel@tonic-gate 2177*7c478bd9Sstevel@tonic-gate return (EINTR); 2178*7c478bd9Sstevel@tonic-gate } 2179*7c478bd9Sstevel@tonic-gate 2180*7c478bd9Sstevel@tonic-gate /* 2181*7c478bd9Sstevel@tonic-gate * De-queue the request and release the request lock 2182*7c478bd9Sstevel@tonic-gate * on this transport (latched by svc_poll()). 2183*7c478bd9Sstevel@tonic-gate */ 2184*7c478bd9Sstevel@tonic-gate mp = next->xp_req_head; 2185*7c478bd9Sstevel@tonic-gate next->xp_req_head = mp->b_next; 2186*7c478bd9Sstevel@tonic-gate mp->b_next = (mblk_t *)0; 2187*7c478bd9Sstevel@tonic-gate 2188*7c478bd9Sstevel@tonic-gate TRACE_2(TR_FAC_KRPC, TR_NFSFP_QUE_REQ_DEQ, 2189*7c478bd9Sstevel@tonic-gate "rpc_que_req_deq:pool %p mp %p", pool, mp); 2190*7c478bd9Sstevel@tonic-gate mutex_exit(&next->xp_req_lock); 2191*7c478bd9Sstevel@tonic-gate 2192*7c478bd9Sstevel@tonic-gate /* 2193*7c478bd9Sstevel@tonic-gate * If this is a new request on a current transport then 2194*7c478bd9Sstevel@tonic-gate * the clone structure is already properly initialized. 2195*7c478bd9Sstevel@tonic-gate * Otherwise, if the request is on a different transport, 2196*7c478bd9Sstevel@tonic-gate * unlink from the current master and link to 2197*7c478bd9Sstevel@tonic-gate * the one we got a request on. 2198*7c478bd9Sstevel@tonic-gate */ 2199*7c478bd9Sstevel@tonic-gate if (next != xprt) { 2200*7c478bd9Sstevel@tonic-gate if (xprt) 2201*7c478bd9Sstevel@tonic-gate svc_clone_unlink(clone_xprt); 2202*7c478bd9Sstevel@tonic-gate svc_clone_link(next, clone_xprt); 2203*7c478bd9Sstevel@tonic-gate xprt = next; 2204*7c478bd9Sstevel@tonic-gate } 2205*7c478bd9Sstevel@tonic-gate 2206*7c478bd9Sstevel@tonic-gate /* 2207*7c478bd9Sstevel@tonic-gate * If there are more requests and req_cv hasn't 2208*7c478bd9Sstevel@tonic-gate * been signaled yet then wake up one more thread now. 2209*7c478bd9Sstevel@tonic-gate * 2210*7c478bd9Sstevel@tonic-gate * We avoid signaling req_cv until the most recently 2211*7c478bd9Sstevel@tonic-gate * signaled thread wakes up and gets CPU to clear 2212*7c478bd9Sstevel@tonic-gate * the `drowsy' flag. 2213*7c478bd9Sstevel@tonic-gate */ 2214*7c478bd9Sstevel@tonic-gate if (!(pool->p_drowsy || pool->p_reqs <= pool->p_walkers || 2215*7c478bd9Sstevel@tonic-gate pool->p_asleep == 0)) { 2216*7c478bd9Sstevel@tonic-gate mutex_enter(&pool->p_req_lock); 2217*7c478bd9Sstevel@tonic-gate 2218*7c478bd9Sstevel@tonic-gate if (pool->p_drowsy || pool->p_reqs <= pool->p_walkers || 2219*7c478bd9Sstevel@tonic-gate pool->p_asleep == 0) 2220*7c478bd9Sstevel@tonic-gate mutex_exit(&pool->p_req_lock); 2221*7c478bd9Sstevel@tonic-gate else { 2222*7c478bd9Sstevel@tonic-gate pool->p_asleep--; 2223*7c478bd9Sstevel@tonic-gate pool->p_drowsy = TRUE; 2224*7c478bd9Sstevel@tonic-gate 2225*7c478bd9Sstevel@tonic-gate cv_signal(&pool->p_req_cv); 2226*7c478bd9Sstevel@tonic-gate mutex_exit(&pool->p_req_lock); 2227*7c478bd9Sstevel@tonic-gate } 2228*7c478bd9Sstevel@tonic-gate } 2229*7c478bd9Sstevel@tonic-gate 2230*7c478bd9Sstevel@tonic-gate /* 2231*7c478bd9Sstevel@tonic-gate * If there are no asleep/signaled threads, we are 2232*7c478bd9Sstevel@tonic-gate * still below pool->p_maxthreads limit, and no thread is 2233*7c478bd9Sstevel@tonic-gate * currently being created then signal the creator 2234*7c478bd9Sstevel@tonic-gate * for one more service thread. 2235*7c478bd9Sstevel@tonic-gate * 2236*7c478bd9Sstevel@tonic-gate * The asleep and drowsy checks are not protected 2237*7c478bd9Sstevel@tonic-gate * by a lock since it hurts performance and a wrong 2238*7c478bd9Sstevel@tonic-gate * decision is not essential. 2239*7c478bd9Sstevel@tonic-gate */ 2240*7c478bd9Sstevel@tonic-gate if (pool->p_asleep == 0 && !pool->p_drowsy && 2241*7c478bd9Sstevel@tonic-gate pool->p_threads + pool->p_detached_threads < 2242*7c478bd9Sstevel@tonic-gate pool->p_maxthreads) 2243*7c478bd9Sstevel@tonic-gate svc_creator_signal(pool); 2244*7c478bd9Sstevel@tonic-gate 2245*7c478bd9Sstevel@tonic-gate /* 2246*7c478bd9Sstevel@tonic-gate * Process the request. 2247*7c478bd9Sstevel@tonic-gate */ 2248*7c478bd9Sstevel@tonic-gate svc_getreq(clone_xprt, mp); 2249*7c478bd9Sstevel@tonic-gate 2250*7c478bd9Sstevel@tonic-gate /* If thread had a reservation it should have been canceled */ 2251*7c478bd9Sstevel@tonic-gate ASSERT(!clone_xprt->xp_reserved); 2252*7c478bd9Sstevel@tonic-gate 2253*7c478bd9Sstevel@tonic-gate /* 2254*7c478bd9Sstevel@tonic-gate * If the clone is marked detached then exit. 2255*7c478bd9Sstevel@tonic-gate * The rpcmod slot has already been released 2256*7c478bd9Sstevel@tonic-gate * when we detached this thread. 2257*7c478bd9Sstevel@tonic-gate */ 2258*7c478bd9Sstevel@tonic-gate if (clone_xprt->xp_detached) { 2259*7c478bd9Sstevel@tonic-gate svc_thread_exitdetached(pool, clone_xprt); 2260*7c478bd9Sstevel@tonic-gate return (0); 2261*7c478bd9Sstevel@tonic-gate } 2262*7c478bd9Sstevel@tonic-gate 2263*7c478bd9Sstevel@tonic-gate /* 2264*7c478bd9Sstevel@tonic-gate * Release our reference on the rpcmod 2265*7c478bd9Sstevel@tonic-gate * slot attached to xp_wq->q_ptr. 2266*7c478bd9Sstevel@tonic-gate */ 2267*7c478bd9Sstevel@tonic-gate (*RELE_PROC(xprt)) (clone_xprt->xp_wq, NULL); 2268*7c478bd9Sstevel@tonic-gate } 2269*7c478bd9Sstevel@tonic-gate /* NOTREACHED */ 2270*7c478bd9Sstevel@tonic-gate } 2271*7c478bd9Sstevel@tonic-gate 2272*7c478bd9Sstevel@tonic-gate /* 2273*7c478bd9Sstevel@tonic-gate * Flush any pending requests for the queue and 2274*7c478bd9Sstevel@tonic-gate * and free the associated mblks. 2275*7c478bd9Sstevel@tonic-gate */ 2276*7c478bd9Sstevel@tonic-gate void 2277*7c478bd9Sstevel@tonic-gate svc_queueclean(queue_t *q) 2278*7c478bd9Sstevel@tonic-gate { 2279*7c478bd9Sstevel@tonic-gate SVCMASTERXPRT *xprt = ((void **) q->q_ptr)[0]; 2280*7c478bd9Sstevel@tonic-gate mblk_t *mp; 2281*7c478bd9Sstevel@tonic-gate 2282*7c478bd9Sstevel@tonic-gate /* 2283*7c478bd9Sstevel@tonic-gate * clean up the requests 2284*7c478bd9Sstevel@tonic-gate */ 2285*7c478bd9Sstevel@tonic-gate mutex_enter(&xprt->xp_req_lock); 2286*7c478bd9Sstevel@tonic-gate while ((mp = xprt->xp_req_head) != NULL) { 2287*7c478bd9Sstevel@tonic-gate xprt->xp_req_head = mp->b_next; 2288*7c478bd9Sstevel@tonic-gate mp->b_next = (mblk_t *)0; 2289*7c478bd9Sstevel@tonic-gate (*RELE_PROC(xprt)) (xprt->xp_wq, mp); 2290*7c478bd9Sstevel@tonic-gate } 2291*7c478bd9Sstevel@tonic-gate mutex_exit(&xprt->xp_req_lock); 2292*7c478bd9Sstevel@tonic-gate } 2293*7c478bd9Sstevel@tonic-gate 2294*7c478bd9Sstevel@tonic-gate /* 2295*7c478bd9Sstevel@tonic-gate * This routine is called by rpcmod to inform kernel RPC that a 2296*7c478bd9Sstevel@tonic-gate * queue is closing. It is called after all the requests have been 2297*7c478bd9Sstevel@tonic-gate * picked up (that is after all the slots on the queue have 2298*7c478bd9Sstevel@tonic-gate * been released by kernel RPC). It is also guaranteed that no more 2299*7c478bd9Sstevel@tonic-gate * request will be delivered on this transport. 2300*7c478bd9Sstevel@tonic-gate * 2301*7c478bd9Sstevel@tonic-gate * - clear xp_wq to mark the master server transport handle as closing 2302*7c478bd9Sstevel@tonic-gate * - if there are no more threads on this transport close/destroy it 2303*7c478bd9Sstevel@tonic-gate * - otherwise, broadcast threads sleeping in svc_poll(); the last 2304*7c478bd9Sstevel@tonic-gate * thread will close/destroy the transport. 2305*7c478bd9Sstevel@tonic-gate */ 2306*7c478bd9Sstevel@tonic-gate void 2307*7c478bd9Sstevel@tonic-gate svc_queueclose(queue_t *q) 2308*7c478bd9Sstevel@tonic-gate { 2309*7c478bd9Sstevel@tonic-gate SVCMASTERXPRT *xprt = ((void **) q->q_ptr)[0]; 2310*7c478bd9Sstevel@tonic-gate 2311*7c478bd9Sstevel@tonic-gate if (xprt == NULL) { 2312*7c478bd9Sstevel@tonic-gate /* 2313*7c478bd9Sstevel@tonic-gate * If there is no master xprt associated with this stream, 2314*7c478bd9Sstevel@tonic-gate * then there is nothing to do. This happens regularly 2315*7c478bd9Sstevel@tonic-gate * with connection-oriented listening streams created by 2316*7c478bd9Sstevel@tonic-gate * nfsd. 2317*7c478bd9Sstevel@tonic-gate */ 2318*7c478bd9Sstevel@tonic-gate return; 2319*7c478bd9Sstevel@tonic-gate } 2320*7c478bd9Sstevel@tonic-gate 2321*7c478bd9Sstevel@tonic-gate mutex_enter(&xprt->xp_thread_lock); 2322*7c478bd9Sstevel@tonic-gate 2323*7c478bd9Sstevel@tonic-gate ASSERT(xprt->xp_req_head == NULL); 2324*7c478bd9Sstevel@tonic-gate ASSERT(xprt->xp_wq != NULL); 2325*7c478bd9Sstevel@tonic-gate 2326*7c478bd9Sstevel@tonic-gate xprt->xp_wq = NULL; 2327*7c478bd9Sstevel@tonic-gate 2328*7c478bd9Sstevel@tonic-gate if (xprt->xp_threads == 0) { 2329*7c478bd9Sstevel@tonic-gate SVCPOOL *pool = xprt->xp_pool; 2330*7c478bd9Sstevel@tonic-gate 2331*7c478bd9Sstevel@tonic-gate /* 2332*7c478bd9Sstevel@tonic-gate * svc_xprt_cleanup() destroys the transport 2333*7c478bd9Sstevel@tonic-gate * or releases the transport thread lock 2334*7c478bd9Sstevel@tonic-gate */ 2335*7c478bd9Sstevel@tonic-gate svc_xprt_cleanup(xprt, FALSE); 2336*7c478bd9Sstevel@tonic-gate 2337*7c478bd9Sstevel@tonic-gate mutex_enter(&pool->p_thread_lock); 2338*7c478bd9Sstevel@tonic-gate 2339*7c478bd9Sstevel@tonic-gate /* 2340*7c478bd9Sstevel@tonic-gate * If the pool is in closing state and this was 2341*7c478bd9Sstevel@tonic-gate * the last transport in the pool then signal the creator 2342*7c478bd9Sstevel@tonic-gate * thread to clean up and exit. 2343*7c478bd9Sstevel@tonic-gate */ 2344*7c478bd9Sstevel@tonic-gate if (pool->p_closing && svc_pool_tryexit(pool)) { 2345*7c478bd9Sstevel@tonic-gate return; 2346*7c478bd9Sstevel@tonic-gate } 2347*7c478bd9Sstevel@tonic-gate mutex_exit(&pool->p_thread_lock); 2348*7c478bd9Sstevel@tonic-gate } else { 2349*7c478bd9Sstevel@tonic-gate /* 2350*7c478bd9Sstevel@tonic-gate * Wakeup threads sleeping in svc_poll() so that they 2351*7c478bd9Sstevel@tonic-gate * unlink from the transport 2352*7c478bd9Sstevel@tonic-gate */ 2353*7c478bd9Sstevel@tonic-gate mutex_enter(&xprt->xp_pool->p_req_lock); 2354*7c478bd9Sstevel@tonic-gate cv_broadcast(&xprt->xp_pool->p_req_cv); 2355*7c478bd9Sstevel@tonic-gate mutex_exit(&xprt->xp_pool->p_req_lock); 2356*7c478bd9Sstevel@tonic-gate 2357*7c478bd9Sstevel@tonic-gate /* 2358*7c478bd9Sstevel@tonic-gate * NOTICE: No references to the master transport structure 2359*7c478bd9Sstevel@tonic-gate * beyond this point! 2360*7c478bd9Sstevel@tonic-gate */ 2361*7c478bd9Sstevel@tonic-gate mutex_exit(&xprt->xp_thread_lock); 2362*7c478bd9Sstevel@tonic-gate } 2363*7c478bd9Sstevel@tonic-gate } 2364*7c478bd9Sstevel@tonic-gate 2365*7c478bd9Sstevel@tonic-gate /* 2366*7c478bd9Sstevel@tonic-gate * Interrupt `request delivery' routine called from rpcmod 2367*7c478bd9Sstevel@tonic-gate * - put a request at the tail of the transport request queue 2368*7c478bd9Sstevel@tonic-gate * - insert a hint for svc_poll() into the xprt-ready queue 2369*7c478bd9Sstevel@tonic-gate * - increment the `pending-requests' count for the pool 2370*7c478bd9Sstevel@tonic-gate * - wake up a thread sleeping in svc_poll() if necessary 2371*7c478bd9Sstevel@tonic-gate * - if all the threads are running ask the creator for a new one. 2372*7c478bd9Sstevel@tonic-gate */ 2373*7c478bd9Sstevel@tonic-gate void 2374*7c478bd9Sstevel@tonic-gate svc_queuereq(queue_t *q, mblk_t *mp) 2375*7c478bd9Sstevel@tonic-gate { 2376*7c478bd9Sstevel@tonic-gate SVCMASTERXPRT *xprt = ((void **) q->q_ptr)[0]; 2377*7c478bd9Sstevel@tonic-gate SVCPOOL *pool = xprt->xp_pool; 2378*7c478bd9Sstevel@tonic-gate 2379*7c478bd9Sstevel@tonic-gate TRACE_0(TR_FAC_KRPC, TR_SVC_QUEUEREQ_START, "svc_queuereq_start"); 2380*7c478bd9Sstevel@tonic-gate 2381*7c478bd9Sstevel@tonic-gate /* 2382*7c478bd9Sstevel@tonic-gate * Step 1. 2383*7c478bd9Sstevel@tonic-gate * Grab the transport's request lock and put 2384*7c478bd9Sstevel@tonic-gate * the request at the tail of the transport's 2385*7c478bd9Sstevel@tonic-gate * request queue. 2386*7c478bd9Sstevel@tonic-gate */ 2387*7c478bd9Sstevel@tonic-gate mutex_enter(&xprt->xp_req_lock); 2388*7c478bd9Sstevel@tonic-gate if (xprt->xp_req_head == NULL) 2389*7c478bd9Sstevel@tonic-gate xprt->xp_req_head = mp; 2390*7c478bd9Sstevel@tonic-gate else 2391*7c478bd9Sstevel@tonic-gate xprt->xp_req_tail->b_next = mp; 2392*7c478bd9Sstevel@tonic-gate xprt->xp_req_tail = mp; 2393*7c478bd9Sstevel@tonic-gate 2394*7c478bd9Sstevel@tonic-gate mutex_exit(&xprt->xp_req_lock); 2395*7c478bd9Sstevel@tonic-gate 2396*7c478bd9Sstevel@tonic-gate /* 2397*7c478bd9Sstevel@tonic-gate * Step 2. 2398*7c478bd9Sstevel@tonic-gate * Grab the pool request lock, insert a hint into 2399*7c478bd9Sstevel@tonic-gate * the xprt-ready queue, increment `pending-requests' 2400*7c478bd9Sstevel@tonic-gate * count for the pool, and wake up a thread sleeping 2401*7c478bd9Sstevel@tonic-gate * in svc_poll() if necessary. 2402*7c478bd9Sstevel@tonic-gate */ 2403*7c478bd9Sstevel@tonic-gate mutex_enter(&pool->p_req_lock); 2404*7c478bd9Sstevel@tonic-gate 2405*7c478bd9Sstevel@tonic-gate /* Insert pointer to this transport into the xprt-ready queue */ 2406*7c478bd9Sstevel@tonic-gate svc_xprt_qput(pool, xprt); 2407*7c478bd9Sstevel@tonic-gate 2408*7c478bd9Sstevel@tonic-gate /* Increment the `pending-requests' count for the pool */ 2409*7c478bd9Sstevel@tonic-gate pool->p_reqs++; 2410*7c478bd9Sstevel@tonic-gate 2411*7c478bd9Sstevel@tonic-gate TRACE_2(TR_FAC_KRPC, TR_NFSFP_QUE_REQ_ENQ, 2412*7c478bd9Sstevel@tonic-gate "rpc_que_req_enq:pool %p mp %p", pool, mp); 2413*7c478bd9Sstevel@tonic-gate 2414*7c478bd9Sstevel@tonic-gate /* 2415*7c478bd9Sstevel@tonic-gate * If there are more requests and req_cv hasn't 2416*7c478bd9Sstevel@tonic-gate * been signaled yet then wake up one more thread now. 2417*7c478bd9Sstevel@tonic-gate * 2418*7c478bd9Sstevel@tonic-gate * We avoid signaling req_cv until the most recently 2419*7c478bd9Sstevel@tonic-gate * signaled thread wakes up and gets CPU to clear 2420*7c478bd9Sstevel@tonic-gate * the `drowsy' flag. 2421*7c478bd9Sstevel@tonic-gate */ 2422*7c478bd9Sstevel@tonic-gate if (pool->p_drowsy || pool->p_reqs <= pool->p_walkers || 2423*7c478bd9Sstevel@tonic-gate pool->p_asleep == 0) { 2424*7c478bd9Sstevel@tonic-gate mutex_exit(&pool->p_req_lock); 2425*7c478bd9Sstevel@tonic-gate } else { 2426*7c478bd9Sstevel@tonic-gate pool->p_drowsy = TRUE; 2427*7c478bd9Sstevel@tonic-gate pool->p_asleep--; 2428*7c478bd9Sstevel@tonic-gate 2429*7c478bd9Sstevel@tonic-gate /* 2430*7c478bd9Sstevel@tonic-gate * Signal wakeup and drop the request lock. 2431*7c478bd9Sstevel@tonic-gate */ 2432*7c478bd9Sstevel@tonic-gate cv_signal(&pool->p_req_cv); 2433*7c478bd9Sstevel@tonic-gate mutex_exit(&pool->p_req_lock); 2434*7c478bd9Sstevel@tonic-gate } 2435*7c478bd9Sstevel@tonic-gate 2436*7c478bd9Sstevel@tonic-gate /* 2437*7c478bd9Sstevel@tonic-gate * Step 3. 2438*7c478bd9Sstevel@tonic-gate * If there are no asleep/signaled threads, we are 2439*7c478bd9Sstevel@tonic-gate * still below pool->p_maxthreads limit, and no thread is 2440*7c478bd9Sstevel@tonic-gate * currently being created then signal the creator 2441*7c478bd9Sstevel@tonic-gate * for one more service thread. 2442*7c478bd9Sstevel@tonic-gate * 2443*7c478bd9Sstevel@tonic-gate * The asleep and drowsy checks are not not protected 2444*7c478bd9Sstevel@tonic-gate * by a lock since it hurts performance and a wrong 2445*7c478bd9Sstevel@tonic-gate * decision is not essential. 2446*7c478bd9Sstevel@tonic-gate */ 2447*7c478bd9Sstevel@tonic-gate if (pool->p_asleep == 0 && !pool->p_drowsy && 2448*7c478bd9Sstevel@tonic-gate pool->p_threads + pool->p_detached_threads < pool->p_maxthreads) 2449*7c478bd9Sstevel@tonic-gate svc_creator_signal(pool); 2450*7c478bd9Sstevel@tonic-gate 2451*7c478bd9Sstevel@tonic-gate TRACE_1(TR_FAC_KRPC, TR_SVC_QUEUEREQ_END, 2452*7c478bd9Sstevel@tonic-gate "svc_queuereq_end:(%S)", "end"); 2453*7c478bd9Sstevel@tonic-gate } 2454*7c478bd9Sstevel@tonic-gate 2455*7c478bd9Sstevel@tonic-gate /* 2456*7c478bd9Sstevel@tonic-gate * Reserve a service thread so that it can be detached later. 2457*7c478bd9Sstevel@tonic-gate * This reservation is required to make sure that when it tries to 2458*7c478bd9Sstevel@tonic-gate * detach itself the total number of detached threads does not exceed 2459*7c478bd9Sstevel@tonic-gate * pool->p_maxthreads - pool->p_redline (i.e. that we can have 2460*7c478bd9Sstevel@tonic-gate * up to pool->p_redline non-detached threads). 2461*7c478bd9Sstevel@tonic-gate * 2462*7c478bd9Sstevel@tonic-gate * If the thread does not detach itself later, it should cancel the 2463*7c478bd9Sstevel@tonic-gate * reservation before returning to svc_run(). 2464*7c478bd9Sstevel@tonic-gate * 2465*7c478bd9Sstevel@tonic-gate * - check if there is room for more reserved/detached threads 2466*7c478bd9Sstevel@tonic-gate * - if so, then increment the `reserved threads' count for the pool 2467*7c478bd9Sstevel@tonic-gate * - mark the thread as reserved (setting the flag in the clone transport 2468*7c478bd9Sstevel@tonic-gate * handle for this thread 2469*7c478bd9Sstevel@tonic-gate * - returns 1 if the reservation succeeded, 0 if it failed. 2470*7c478bd9Sstevel@tonic-gate */ 2471*7c478bd9Sstevel@tonic-gate int 2472*7c478bd9Sstevel@tonic-gate svc_reserve_thread(SVCXPRT *clone_xprt) 2473*7c478bd9Sstevel@tonic-gate { 2474*7c478bd9Sstevel@tonic-gate SVCPOOL *pool = clone_xprt->xp_master->xp_pool; 2475*7c478bd9Sstevel@tonic-gate 2476*7c478bd9Sstevel@tonic-gate /* Recursive reservations are not allowed */ 2477*7c478bd9Sstevel@tonic-gate ASSERT(!clone_xprt->xp_reserved); 2478*7c478bd9Sstevel@tonic-gate ASSERT(!clone_xprt->xp_detached); 2479*7c478bd9Sstevel@tonic-gate 2480*7c478bd9Sstevel@tonic-gate /* Check pool counts if there is room for reservation */ 2481*7c478bd9Sstevel@tonic-gate mutex_enter(&pool->p_thread_lock); 2482*7c478bd9Sstevel@tonic-gate if (pool->p_reserved_threads + pool->p_detached_threads >= 2483*7c478bd9Sstevel@tonic-gate pool->p_maxthreads - pool->p_redline) { 2484*7c478bd9Sstevel@tonic-gate mutex_exit(&pool->p_thread_lock); 2485*7c478bd9Sstevel@tonic-gate return (0); 2486*7c478bd9Sstevel@tonic-gate } 2487*7c478bd9Sstevel@tonic-gate pool->p_reserved_threads++; 2488*7c478bd9Sstevel@tonic-gate mutex_exit(&pool->p_thread_lock); 2489*7c478bd9Sstevel@tonic-gate 2490*7c478bd9Sstevel@tonic-gate /* Mark the thread (clone handle) as reserved */ 2491*7c478bd9Sstevel@tonic-gate clone_xprt->xp_reserved = TRUE; 2492*7c478bd9Sstevel@tonic-gate 2493*7c478bd9Sstevel@tonic-gate return (1); 2494*7c478bd9Sstevel@tonic-gate } 2495*7c478bd9Sstevel@tonic-gate 2496*7c478bd9Sstevel@tonic-gate /* 2497*7c478bd9Sstevel@tonic-gate * Cancel a reservation for a thread. 2498*7c478bd9Sstevel@tonic-gate * - decrement the `reserved threads' count for the pool 2499*7c478bd9Sstevel@tonic-gate * - clear the flag in the clone transport handle for this thread. 2500*7c478bd9Sstevel@tonic-gate */ 2501*7c478bd9Sstevel@tonic-gate void 2502*7c478bd9Sstevel@tonic-gate svc_unreserve_thread(SVCXPRT *clone_xprt) 2503*7c478bd9Sstevel@tonic-gate { 2504*7c478bd9Sstevel@tonic-gate SVCPOOL *pool = clone_xprt->xp_master->xp_pool; 2505*7c478bd9Sstevel@tonic-gate 2506*7c478bd9Sstevel@tonic-gate /* Thread must have a reservation */ 2507*7c478bd9Sstevel@tonic-gate ASSERT(clone_xprt->xp_reserved); 2508*7c478bd9Sstevel@tonic-gate ASSERT(!clone_xprt->xp_detached); 2509*7c478bd9Sstevel@tonic-gate 2510*7c478bd9Sstevel@tonic-gate /* Decrement global count */ 2511*7c478bd9Sstevel@tonic-gate mutex_enter(&pool->p_thread_lock); 2512*7c478bd9Sstevel@tonic-gate pool->p_reserved_threads--; 2513*7c478bd9Sstevel@tonic-gate mutex_exit(&pool->p_thread_lock); 2514*7c478bd9Sstevel@tonic-gate 2515*7c478bd9Sstevel@tonic-gate /* Clear reservation flag */ 2516*7c478bd9Sstevel@tonic-gate clone_xprt->xp_reserved = FALSE; 2517*7c478bd9Sstevel@tonic-gate } 2518*7c478bd9Sstevel@tonic-gate 2519*7c478bd9Sstevel@tonic-gate /* 2520*7c478bd9Sstevel@tonic-gate * Detach a thread from its transport, so that it can block for an 2521*7c478bd9Sstevel@tonic-gate * extended time. Because the transport can be closed after the thread is 2522*7c478bd9Sstevel@tonic-gate * detached, the thread should have already sent off a reply if it was 2523*7c478bd9Sstevel@tonic-gate * going to send one. 2524*7c478bd9Sstevel@tonic-gate * 2525*7c478bd9Sstevel@tonic-gate * - decrement `non-detached threads' count and increment `detached threads' 2526*7c478bd9Sstevel@tonic-gate * counts for the transport 2527*7c478bd9Sstevel@tonic-gate * - decrement the `non-detached threads' and `reserved threads' 2528*7c478bd9Sstevel@tonic-gate * counts and increment the `detached threads' count for the pool 2529*7c478bd9Sstevel@tonic-gate * - release the rpcmod slot 2530*7c478bd9Sstevel@tonic-gate * - mark the clone (thread) as detached. 2531*7c478bd9Sstevel@tonic-gate * 2532*7c478bd9Sstevel@tonic-gate * No need to return a pointer to the thread's CPR information, since 2533*7c478bd9Sstevel@tonic-gate * the thread has a userland identity. 2534*7c478bd9Sstevel@tonic-gate * 2535*7c478bd9Sstevel@tonic-gate * NOTICE: a thread must not detach itself without making a prior reservation 2536*7c478bd9Sstevel@tonic-gate * through svc_thread_reserve(). 2537*7c478bd9Sstevel@tonic-gate */ 2538*7c478bd9Sstevel@tonic-gate callb_cpr_t * 2539*7c478bd9Sstevel@tonic-gate svc_detach_thread(SVCXPRT *clone_xprt) 2540*7c478bd9Sstevel@tonic-gate { 2541*7c478bd9Sstevel@tonic-gate SVCMASTERXPRT *xprt = clone_xprt->xp_master; 2542*7c478bd9Sstevel@tonic-gate SVCPOOL *pool = xprt->xp_pool; 2543*7c478bd9Sstevel@tonic-gate 2544*7c478bd9Sstevel@tonic-gate /* Thread must have a reservation */ 2545*7c478bd9Sstevel@tonic-gate ASSERT(clone_xprt->xp_reserved); 2546*7c478bd9Sstevel@tonic-gate ASSERT(!clone_xprt->xp_detached); 2547*7c478bd9Sstevel@tonic-gate 2548*7c478bd9Sstevel@tonic-gate /* Bookkeeping for this transport */ 2549*7c478bd9Sstevel@tonic-gate mutex_enter(&xprt->xp_thread_lock); 2550*7c478bd9Sstevel@tonic-gate xprt->xp_threads--; 2551*7c478bd9Sstevel@tonic-gate xprt->xp_detached_threads++; 2552*7c478bd9Sstevel@tonic-gate mutex_exit(&xprt->xp_thread_lock); 2553*7c478bd9Sstevel@tonic-gate 2554*7c478bd9Sstevel@tonic-gate /* Bookkeeping for the pool */ 2555*7c478bd9Sstevel@tonic-gate mutex_enter(&pool->p_thread_lock); 2556*7c478bd9Sstevel@tonic-gate pool->p_threads--; 2557*7c478bd9Sstevel@tonic-gate pool->p_reserved_threads--; 2558*7c478bd9Sstevel@tonic-gate pool->p_detached_threads++; 2559*7c478bd9Sstevel@tonic-gate mutex_exit(&pool->p_thread_lock); 2560*7c478bd9Sstevel@tonic-gate 2561*7c478bd9Sstevel@tonic-gate /* Release an rpcmod slot for this request */ 2562*7c478bd9Sstevel@tonic-gate (*RELE_PROC(xprt)) (clone_xprt->xp_wq, NULL); 2563*7c478bd9Sstevel@tonic-gate 2564*7c478bd9Sstevel@tonic-gate /* Mark the clone (thread) as detached */ 2565*7c478bd9Sstevel@tonic-gate clone_xprt->xp_reserved = FALSE; 2566*7c478bd9Sstevel@tonic-gate clone_xprt->xp_detached = TRUE; 2567*7c478bd9Sstevel@tonic-gate 2568*7c478bd9Sstevel@tonic-gate return (NULL); 2569*7c478bd9Sstevel@tonic-gate } 2570*7c478bd9Sstevel@tonic-gate 2571*7c478bd9Sstevel@tonic-gate /* 2572*7c478bd9Sstevel@tonic-gate * This routine is responsible for extracting RDMA plugin master XPRT, 2573*7c478bd9Sstevel@tonic-gate * unregister from the SVCPOOL and initiate plugin specific cleanup. 2574*7c478bd9Sstevel@tonic-gate * It is passed a list/group of rdma transports as records which are 2575*7c478bd9Sstevel@tonic-gate * active in a given registered or unregistered kRPC thread pool. Its shuts 2576*7c478bd9Sstevel@tonic-gate * all active rdma transports in that pool. If the thread active on the trasport 2577*7c478bd9Sstevel@tonic-gate * happens to be last thread for that pool, it will signal the creater thread 2578*7c478bd9Sstevel@tonic-gate * to cleanup the pool and destroy the xprt in svc_queueclose() 2579*7c478bd9Sstevel@tonic-gate */ 2580*7c478bd9Sstevel@tonic-gate void 2581*7c478bd9Sstevel@tonic-gate rdma_stop(rdma_xprt_group_t rdma_xprts) 2582*7c478bd9Sstevel@tonic-gate { 2583*7c478bd9Sstevel@tonic-gate SVCMASTERXPRT *xprt; 2584*7c478bd9Sstevel@tonic-gate rdma_xprt_record_t *curr_rec; 2585*7c478bd9Sstevel@tonic-gate queue_t *q; 2586*7c478bd9Sstevel@tonic-gate mblk_t *mp; 2587*7c478bd9Sstevel@tonic-gate int i; 2588*7c478bd9Sstevel@tonic-gate 2589*7c478bd9Sstevel@tonic-gate if (rdma_xprts.rtg_count == 0) 2590*7c478bd9Sstevel@tonic-gate return; 2591*7c478bd9Sstevel@tonic-gate 2592*7c478bd9Sstevel@tonic-gate for (i = 0; i < rdma_xprts.rtg_count; i++) { 2593*7c478bd9Sstevel@tonic-gate curr_rec = rdma_xprts.rtg_listhead; 2594*7c478bd9Sstevel@tonic-gate rdma_xprts.rtg_listhead = curr_rec->rtr_next; 2595*7c478bd9Sstevel@tonic-gate curr_rec->rtr_next = NULL; 2596*7c478bd9Sstevel@tonic-gate xprt = curr_rec->rtr_xprt_ptr; 2597*7c478bd9Sstevel@tonic-gate q = xprt->xp_wq; 2598*7c478bd9Sstevel@tonic-gate svc_rdma_kstop(xprt); 2599*7c478bd9Sstevel@tonic-gate 2600*7c478bd9Sstevel@tonic-gate mutex_enter(&xprt->xp_req_lock); 2601*7c478bd9Sstevel@tonic-gate while ((mp = xprt->xp_req_head) != NULL) { 2602*7c478bd9Sstevel@tonic-gate xprt->xp_req_head = mp->b_next; 2603*7c478bd9Sstevel@tonic-gate mp->b_next = (mblk_t *)0; 2604*7c478bd9Sstevel@tonic-gate if (mp) 2605*7c478bd9Sstevel@tonic-gate freemsg(mp); 2606*7c478bd9Sstevel@tonic-gate } 2607*7c478bd9Sstevel@tonic-gate mutex_exit(&xprt->xp_req_lock); 2608*7c478bd9Sstevel@tonic-gate svc_queueclose(q); 2609*7c478bd9Sstevel@tonic-gate #ifdef DEBUG 2610*7c478bd9Sstevel@tonic-gate if (rdma_check) 2611*7c478bd9Sstevel@tonic-gate cmn_err(CE_NOTE, "rdma_stop: Exited svc_queueclose\n"); 2612*7c478bd9Sstevel@tonic-gate #endif 2613*7c478bd9Sstevel@tonic-gate /* 2614*7c478bd9Sstevel@tonic-gate * Free the rdma transport record for the expunged rdma 2615*7c478bd9Sstevel@tonic-gate * based master transport handle. 2616*7c478bd9Sstevel@tonic-gate */ 2617*7c478bd9Sstevel@tonic-gate kmem_free(curr_rec, sizeof (rdma_xprt_record_t)); 2618*7c478bd9Sstevel@tonic-gate if (!rdma_xprts.rtg_listhead) 2619*7c478bd9Sstevel@tonic-gate break; 2620*7c478bd9Sstevel@tonic-gate } 2621*7c478bd9Sstevel@tonic-gate } 2622