svc.c revision 261046
1177633Sdfr/* $NetBSD: svc.c,v 1.21 2000/07/06 03:10:35 christos Exp $ */ 2177633Sdfr 3261046Smav/*- 4261046Smav * Copyright (c) 2009, Sun Microsystems, Inc. 5261046Smav * All rights reserved. 6177633Sdfr * 7261046Smav * Redistribution and use in source and binary forms, with or without 8261046Smav * modification, are permitted provided that the following conditions are met: 9261046Smav * - Redistributions of source code must retain the above copyright notice, 10261046Smav * this list of conditions and the following disclaimer. 11261046Smav * - Redistributions in binary form must reproduce the above copyright notice, 12261046Smav * this list of conditions and the following disclaimer in the documentation 13261046Smav * and/or other materials provided with the distribution. 14261046Smav * - Neither the name of Sun Microsystems, Inc. nor the names of its 15261046Smav * contributors may be used to endorse or promote products derived 16261046Smav * from this software without specific prior written permission. 17261046Smav * 18261046Smav * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19261046Smav * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20261046Smav * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21261046Smav * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22261046Smav * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23261046Smav * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24261046Smav * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25261046Smav * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26261046Smav * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27261046Smav * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28261046Smav * POSSIBILITY OF SUCH DAMAGE. 29177633Sdfr */ 30177633Sdfr 31177633Sdfr#if defined(LIBC_SCCS) && !defined(lint) 32177633Sdfrstatic char *sccsid2 = "@(#)svc.c 1.44 88/02/08 Copyr 1984 Sun Micro"; 33177633Sdfrstatic char *sccsid = "@(#)svc.c 2.4 88/08/11 4.0 RPCSRC"; 34177633Sdfr#endif 35177633Sdfr#include <sys/cdefs.h> 36177633Sdfr__FBSDID("$FreeBSD: stable/10/sys/rpc/svc.c 261046 2014-01-22 23:45:27Z mav $"); 37177633Sdfr 38177633Sdfr/* 39177633Sdfr * svc.c, Server-side remote procedure call interface. 40177633Sdfr * 41177633Sdfr * There are two sets of procedures here. The xprt routines are 42177633Sdfr * for handling transport handles. The svc routines handle the 43177633Sdfr * list of service routines. 44177633Sdfr * 45177633Sdfr * Copyright (C) 1984, Sun Microsystems, Inc. 46177633Sdfr */ 47177633Sdfr 48177633Sdfr#include <sys/param.h> 49177633Sdfr#include <sys/lock.h> 50177633Sdfr#include <sys/kernel.h> 51184588Sdfr#include <sys/kthread.h> 52177633Sdfr#include <sys/malloc.h> 53184588Sdfr#include <sys/mbuf.h> 54177633Sdfr#include <sys/mutex.h> 55184588Sdfr#include <sys/proc.h> 56177633Sdfr#include <sys/queue.h> 57184588Sdfr#include <sys/socketvar.h> 58177633Sdfr#include <sys/systm.h> 59177633Sdfr#include <sys/ucred.h> 60177633Sdfr 61177633Sdfr#include <rpc/rpc.h> 62177633Sdfr#include <rpc/rpcb_clnt.h> 63184588Sdfr#include <rpc/replay.h> 64177633Sdfr 65177685Sdfr#include <rpc/rpc_com.h> 66177633Sdfr 67177633Sdfr#define SVC_VERSQUIET 0x0001 /* keep quiet about vers mismatch */ 68184588Sdfr#define version_keepquiet(xp) (SVC_EXT(xp)->xp_flags & SVC_VERSQUIET) 69177633Sdfr 70177633Sdfrstatic struct svc_callout *svc_find(SVCPOOL *pool, rpcprog_t, rpcvers_t, 71177633Sdfr char *); 72184588Sdfrstatic void svc_new_thread(SVCPOOL *pool); 73184588Sdfrstatic void xprt_unregister_locked(SVCXPRT *xprt); 74177633Sdfr 75177633Sdfr/* *************** SVCXPRT related stuff **************** */ 76177633Sdfr 77184588Sdfrstatic int svcpool_minthread_sysctl(SYSCTL_HANDLER_ARGS); 78184588Sdfrstatic int svcpool_maxthread_sysctl(SYSCTL_HANDLER_ARGS); 79184588Sdfr 80177633SdfrSVCPOOL* 81184588Sdfrsvcpool_create(const char *name, struct sysctl_oid_list *sysctl_base) 82177633Sdfr{ 83177633Sdfr SVCPOOL *pool; 84177633Sdfr 85177633Sdfr pool = malloc(sizeof(SVCPOOL), M_RPC, M_WAITOK|M_ZERO); 86177633Sdfr 87177633Sdfr mtx_init(&pool->sp_lock, "sp_lock", NULL, MTX_DEF); 88184588Sdfr pool->sp_name = name; 89184588Sdfr pool->sp_state = SVCPOOL_INIT; 90184588Sdfr pool->sp_proc = NULL; 91177633Sdfr TAILQ_INIT(&pool->sp_xlist); 92177633Sdfr TAILQ_INIT(&pool->sp_active); 93177633Sdfr TAILQ_INIT(&pool->sp_callouts); 94184588Sdfr LIST_INIT(&pool->sp_threads); 95184588Sdfr LIST_INIT(&pool->sp_idlethreads); 96184588Sdfr pool->sp_minthreads = 1; 97184588Sdfr pool->sp_maxthreads = 1; 98184588Sdfr pool->sp_threadcount = 0; 99177633Sdfr 100184588Sdfr /* 101184588Sdfr * Don't use more than a quarter of mbuf clusters or more than 102184588Sdfr * 45Mb buffering requests. 103184588Sdfr */ 104184588Sdfr pool->sp_space_high = nmbclusters * MCLBYTES / 4; 105184588Sdfr if (pool->sp_space_high > 45 << 20) 106184588Sdfr pool->sp_space_high = 45 << 20; 107184588Sdfr pool->sp_space_low = 2 * pool->sp_space_high / 3; 108184588Sdfr 109184588Sdfr sysctl_ctx_init(&pool->sp_sysctl); 110184588Sdfr if (sysctl_base) { 111184588Sdfr SYSCTL_ADD_PROC(&pool->sp_sysctl, sysctl_base, OID_AUTO, 112184588Sdfr "minthreads", CTLTYPE_INT | CTLFLAG_RW, 113184588Sdfr pool, 0, svcpool_minthread_sysctl, "I", ""); 114184588Sdfr SYSCTL_ADD_PROC(&pool->sp_sysctl, sysctl_base, OID_AUTO, 115184588Sdfr "maxthreads", CTLTYPE_INT | CTLFLAG_RW, 116184588Sdfr pool, 0, svcpool_maxthread_sysctl, "I", ""); 117184588Sdfr SYSCTL_ADD_INT(&pool->sp_sysctl, sysctl_base, OID_AUTO, 118184588Sdfr "threads", CTLFLAG_RD, &pool->sp_threadcount, 0, ""); 119184588Sdfr 120184588Sdfr SYSCTL_ADD_UINT(&pool->sp_sysctl, sysctl_base, OID_AUTO, 121184588Sdfr "request_space_used", CTLFLAG_RD, 122184588Sdfr &pool->sp_space_used, 0, 123184588Sdfr "Space in parsed but not handled requests."); 124184588Sdfr 125184588Sdfr SYSCTL_ADD_UINT(&pool->sp_sysctl, sysctl_base, OID_AUTO, 126184588Sdfr "request_space_used_highest", CTLFLAG_RD, 127184588Sdfr &pool->sp_space_used_highest, 0, 128184588Sdfr "Highest space used since reboot."); 129184588Sdfr 130184588Sdfr SYSCTL_ADD_UINT(&pool->sp_sysctl, sysctl_base, OID_AUTO, 131184588Sdfr "request_space_high", CTLFLAG_RW, 132184588Sdfr &pool->sp_space_high, 0, 133184588Sdfr "Maximum space in parsed but not handled requests."); 134184588Sdfr 135184588Sdfr SYSCTL_ADD_UINT(&pool->sp_sysctl, sysctl_base, OID_AUTO, 136184588Sdfr "request_space_low", CTLFLAG_RW, 137184588Sdfr &pool->sp_space_low, 0, 138184588Sdfr "Low water mark for request space."); 139184588Sdfr 140217326Smdf SYSCTL_ADD_INT(&pool->sp_sysctl, sysctl_base, OID_AUTO, 141184588Sdfr "request_space_throttled", CTLFLAG_RD, 142184588Sdfr &pool->sp_space_throttled, 0, 143184588Sdfr "Whether nfs requests are currently throttled"); 144184588Sdfr 145217326Smdf SYSCTL_ADD_INT(&pool->sp_sysctl, sysctl_base, OID_AUTO, 146184588Sdfr "request_space_throttle_count", CTLFLAG_RD, 147184588Sdfr &pool->sp_space_throttle_count, 0, 148184588Sdfr "Count of times throttling based on request space has occurred"); 149184588Sdfr } 150184588Sdfr 151177633Sdfr return pool; 152177633Sdfr} 153177633Sdfr 154177633Sdfrvoid 155177633Sdfrsvcpool_destroy(SVCPOOL *pool) 156177633Sdfr{ 157184588Sdfr SVCXPRT *xprt, *nxprt; 158177633Sdfr struct svc_callout *s; 159184588Sdfr struct svcxprt_list cleanup; 160177633Sdfr 161184588Sdfr TAILQ_INIT(&cleanup); 162177633Sdfr mtx_lock(&pool->sp_lock); 163177633Sdfr 164177633Sdfr while (TAILQ_FIRST(&pool->sp_xlist)) { 165177633Sdfr xprt = TAILQ_FIRST(&pool->sp_xlist); 166184588Sdfr xprt_unregister_locked(xprt); 167184588Sdfr TAILQ_INSERT_TAIL(&cleanup, xprt, xp_link); 168177633Sdfr } 169177633Sdfr 170177633Sdfr while (TAILQ_FIRST(&pool->sp_callouts)) { 171177633Sdfr s = TAILQ_FIRST(&pool->sp_callouts); 172177633Sdfr mtx_unlock(&pool->sp_lock); 173177633Sdfr svc_unreg(pool, s->sc_prog, s->sc_vers); 174177633Sdfr mtx_lock(&pool->sp_lock); 175177633Sdfr } 176193603Srmacklem mtx_unlock(&pool->sp_lock); 177177633Sdfr 178184588Sdfr TAILQ_FOREACH_SAFE(xprt, &cleanup, xp_link, nxprt) { 179184588Sdfr SVC_RELEASE(xprt); 180184588Sdfr } 181184588Sdfr 182193436Srmacklem mtx_destroy(&pool->sp_lock); 183193436Srmacklem 184184588Sdfr if (pool->sp_rcache) 185184588Sdfr replay_freecache(pool->sp_rcache); 186184588Sdfr 187184588Sdfr sysctl_ctx_free(&pool->sp_sysctl); 188177633Sdfr free(pool, M_RPC); 189177633Sdfr} 190177633Sdfr 191184588Sdfrstatic bool_t 192184588Sdfrsvcpool_active(SVCPOOL *pool) 193184588Sdfr{ 194184588Sdfr enum svcpool_state state = pool->sp_state; 195184588Sdfr 196184588Sdfr if (state == SVCPOOL_INIT || state == SVCPOOL_CLOSING) 197184588Sdfr return (FALSE); 198184588Sdfr return (TRUE); 199184588Sdfr} 200184588Sdfr 201177633Sdfr/* 202184588Sdfr * Sysctl handler to set the minimum thread count on a pool 203184588Sdfr */ 204184588Sdfrstatic int 205184588Sdfrsvcpool_minthread_sysctl(SYSCTL_HANDLER_ARGS) 206184588Sdfr{ 207184588Sdfr SVCPOOL *pool; 208184588Sdfr int newminthreads, error, n; 209184588Sdfr 210184588Sdfr pool = oidp->oid_arg1; 211184588Sdfr newminthreads = pool->sp_minthreads; 212184588Sdfr error = sysctl_handle_int(oidp, &newminthreads, 0, req); 213184588Sdfr if (error == 0 && newminthreads != pool->sp_minthreads) { 214184588Sdfr if (newminthreads > pool->sp_maxthreads) 215184588Sdfr return (EINVAL); 216184588Sdfr mtx_lock(&pool->sp_lock); 217184588Sdfr if (newminthreads > pool->sp_minthreads 218184588Sdfr && svcpool_active(pool)) { 219184588Sdfr /* 220184588Sdfr * If the pool is running and we are 221184588Sdfr * increasing, create some more threads now. 222184588Sdfr */ 223184588Sdfr n = newminthreads - pool->sp_threadcount; 224184588Sdfr if (n > 0) { 225184588Sdfr mtx_unlock(&pool->sp_lock); 226184588Sdfr while (n--) 227184588Sdfr svc_new_thread(pool); 228184588Sdfr mtx_lock(&pool->sp_lock); 229184588Sdfr } 230184588Sdfr } 231184588Sdfr pool->sp_minthreads = newminthreads; 232184588Sdfr mtx_unlock(&pool->sp_lock); 233184588Sdfr } 234184588Sdfr return (error); 235184588Sdfr} 236184588Sdfr 237184588Sdfr/* 238184588Sdfr * Sysctl handler to set the maximum thread count on a pool 239184588Sdfr */ 240184588Sdfrstatic int 241184588Sdfrsvcpool_maxthread_sysctl(SYSCTL_HANDLER_ARGS) 242184588Sdfr{ 243184588Sdfr SVCPOOL *pool; 244184588Sdfr SVCTHREAD *st; 245184588Sdfr int newmaxthreads, error; 246184588Sdfr 247184588Sdfr pool = oidp->oid_arg1; 248184588Sdfr newmaxthreads = pool->sp_maxthreads; 249184588Sdfr error = sysctl_handle_int(oidp, &newmaxthreads, 0, req); 250184588Sdfr if (error == 0 && newmaxthreads != pool->sp_maxthreads) { 251184588Sdfr if (newmaxthreads < pool->sp_minthreads) 252184588Sdfr return (EINVAL); 253184588Sdfr mtx_lock(&pool->sp_lock); 254184588Sdfr if (newmaxthreads < pool->sp_maxthreads 255184588Sdfr && svcpool_active(pool)) { 256184588Sdfr /* 257184588Sdfr * If the pool is running and we are 258184588Sdfr * decreasing, wake up some idle threads to 259184588Sdfr * encourage them to exit. 260184588Sdfr */ 261184588Sdfr LIST_FOREACH(st, &pool->sp_idlethreads, st_ilink) 262184588Sdfr cv_signal(&st->st_cond); 263184588Sdfr } 264184588Sdfr pool->sp_maxthreads = newmaxthreads; 265184588Sdfr mtx_unlock(&pool->sp_lock); 266184588Sdfr } 267184588Sdfr return (error); 268184588Sdfr} 269184588Sdfr 270184588Sdfr/* 271177633Sdfr * Activate a transport handle. 272177633Sdfr */ 273177633Sdfrvoid 274177633Sdfrxprt_register(SVCXPRT *xprt) 275177633Sdfr{ 276177633Sdfr SVCPOOL *pool = xprt->xp_pool; 277177633Sdfr 278194407Srmacklem SVC_ACQUIRE(xprt); 279177633Sdfr mtx_lock(&pool->sp_lock); 280177633Sdfr xprt->xp_registered = TRUE; 281177633Sdfr xprt->xp_active = FALSE; 282177633Sdfr TAILQ_INSERT_TAIL(&pool->sp_xlist, xprt, xp_link); 283177633Sdfr mtx_unlock(&pool->sp_lock); 284177633Sdfr} 285177633Sdfr 286177633Sdfr/* 287184588Sdfr * De-activate a transport handle. Note: the locked version doesn't 288184588Sdfr * release the transport - caller must do that after dropping the pool 289184588Sdfr * lock. 290177633Sdfr */ 291177633Sdfrstatic void 292184588Sdfrxprt_unregister_locked(SVCXPRT *xprt) 293177633Sdfr{ 294177633Sdfr SVCPOOL *pool = xprt->xp_pool; 295177633Sdfr 296193649Srmacklem KASSERT(xprt->xp_registered == TRUE, 297193649Srmacklem ("xprt_unregister_locked: not registered")); 298177633Sdfr if (xprt->xp_active) { 299177633Sdfr TAILQ_REMOVE(&pool->sp_active, xprt, xp_alink); 300177633Sdfr xprt->xp_active = FALSE; 301177633Sdfr } 302177633Sdfr TAILQ_REMOVE(&pool->sp_xlist, xprt, xp_link); 303177633Sdfr xprt->xp_registered = FALSE; 304184588Sdfr} 305177633Sdfr 306184588Sdfrvoid 307184588Sdfrxprt_unregister(SVCXPRT *xprt) 308184588Sdfr{ 309184588Sdfr SVCPOOL *pool = xprt->xp_pool; 310184588Sdfr 311184588Sdfr mtx_lock(&pool->sp_lock); 312193649Srmacklem if (xprt->xp_registered == FALSE) { 313193649Srmacklem /* Already unregistered by another thread */ 314193649Srmacklem mtx_unlock(&pool->sp_lock); 315193649Srmacklem return; 316193649Srmacklem } 317184588Sdfr xprt_unregister_locked(xprt); 318184588Sdfr mtx_unlock(&pool->sp_lock); 319184588Sdfr 320184588Sdfr SVC_RELEASE(xprt); 321177633Sdfr} 322177633Sdfr 323184588Sdfrstatic void 324184588Sdfrxprt_assignthread(SVCXPRT *xprt) 325184588Sdfr{ 326184588Sdfr SVCPOOL *pool = xprt->xp_pool; 327184588Sdfr SVCTHREAD *st; 328184588Sdfr 329184588Sdfr /* 330184588Sdfr * Attempt to assign a service thread to this 331184588Sdfr * transport. 332184588Sdfr */ 333184588Sdfr LIST_FOREACH(st, &pool->sp_idlethreads, st_ilink) { 334184588Sdfr if (st->st_xprt == NULL && STAILQ_EMPTY(&st->st_reqs)) 335184588Sdfr break; 336184588Sdfr } 337184588Sdfr if (st) { 338184588Sdfr SVC_ACQUIRE(xprt); 339184588Sdfr xprt->xp_thread = st; 340184588Sdfr st->st_xprt = xprt; 341184588Sdfr cv_signal(&st->st_cond); 342184588Sdfr } else { 343184588Sdfr /* 344184588Sdfr * See if we can create a new thread. The 345184588Sdfr * actual thread creation happens in 346184588Sdfr * svc_run_internal because our locking state 347184588Sdfr * is poorly defined (we are typically called 348184588Sdfr * from a socket upcall). Don't create more 349184588Sdfr * than one thread per second. 350184588Sdfr */ 351184588Sdfr if (pool->sp_state == SVCPOOL_ACTIVE 352184588Sdfr && pool->sp_lastcreatetime < time_uptime 353184588Sdfr && pool->sp_threadcount < pool->sp_maxthreads) { 354184588Sdfr pool->sp_state = SVCPOOL_THREADWANTED; 355184588Sdfr } 356184588Sdfr } 357184588Sdfr} 358184588Sdfr 359177633Sdfrvoid 360177633Sdfrxprt_active(SVCXPRT *xprt) 361177633Sdfr{ 362177633Sdfr SVCPOOL *pool = xprt->xp_pool; 363177633Sdfr 364193436Srmacklem mtx_lock(&pool->sp_lock); 365193436Srmacklem 366184588Sdfr if (!xprt->xp_registered) { 367184588Sdfr /* 368184588Sdfr * Race with xprt_unregister - we lose. 369184588Sdfr */ 370193436Srmacklem mtx_unlock(&pool->sp_lock); 371184588Sdfr return; 372184588Sdfr } 373184588Sdfr 374177633Sdfr if (!xprt->xp_active) { 375177633Sdfr TAILQ_INSERT_TAIL(&pool->sp_active, xprt, xp_alink); 376177633Sdfr xprt->xp_active = TRUE; 377184588Sdfr xprt_assignthread(xprt); 378177633Sdfr } 379177633Sdfr 380177633Sdfr mtx_unlock(&pool->sp_lock); 381177633Sdfr} 382177633Sdfr 383177633Sdfrvoid 384184588Sdfrxprt_inactive_locked(SVCXPRT *xprt) 385177633Sdfr{ 386177633Sdfr SVCPOOL *pool = xprt->xp_pool; 387177633Sdfr 388177633Sdfr if (xprt->xp_active) { 389177633Sdfr TAILQ_REMOVE(&pool->sp_active, xprt, xp_alink); 390177633Sdfr xprt->xp_active = FALSE; 391177633Sdfr } 392184588Sdfr} 393177633Sdfr 394184588Sdfrvoid 395184588Sdfrxprt_inactive(SVCXPRT *xprt) 396184588Sdfr{ 397184588Sdfr SVCPOOL *pool = xprt->xp_pool; 398184588Sdfr 399184588Sdfr mtx_lock(&pool->sp_lock); 400184588Sdfr xprt_inactive_locked(xprt); 401177633Sdfr mtx_unlock(&pool->sp_lock); 402177633Sdfr} 403177633Sdfr 404177633Sdfr/* 405177633Sdfr * Add a service program to the callout list. 406177633Sdfr * The dispatch routine will be called when a rpc request for this 407177633Sdfr * program number comes in. 408177633Sdfr */ 409177633Sdfrbool_t 410177633Sdfrsvc_reg(SVCXPRT *xprt, const rpcprog_t prog, const rpcvers_t vers, 411177633Sdfr void (*dispatch)(struct svc_req *, SVCXPRT *), 412177633Sdfr const struct netconfig *nconf) 413177633Sdfr{ 414177633Sdfr SVCPOOL *pool = xprt->xp_pool; 415177633Sdfr struct svc_callout *s; 416177633Sdfr char *netid = NULL; 417177633Sdfr int flag = 0; 418177633Sdfr 419177633Sdfr/* VARIABLES PROTECTED BY svc_lock: s, svc_head */ 420177633Sdfr 421177633Sdfr if (xprt->xp_netid) { 422177633Sdfr netid = strdup(xprt->xp_netid, M_RPC); 423177633Sdfr flag = 1; 424177633Sdfr } else if (nconf && nconf->nc_netid) { 425177633Sdfr netid = strdup(nconf->nc_netid, M_RPC); 426177633Sdfr flag = 1; 427177633Sdfr } /* must have been created with svc_raw_create */ 428177633Sdfr if ((netid == NULL) && (flag == 1)) { 429177633Sdfr return (FALSE); 430177633Sdfr } 431177633Sdfr 432177633Sdfr mtx_lock(&pool->sp_lock); 433177633Sdfr if ((s = svc_find(pool, prog, vers, netid)) != NULL) { 434177633Sdfr if (netid) 435177633Sdfr free(netid, M_RPC); 436177633Sdfr if (s->sc_dispatch == dispatch) 437177633Sdfr goto rpcb_it; /* he is registering another xptr */ 438177633Sdfr mtx_unlock(&pool->sp_lock); 439177633Sdfr return (FALSE); 440177633Sdfr } 441177633Sdfr s = malloc(sizeof (struct svc_callout), M_RPC, M_NOWAIT); 442177633Sdfr if (s == NULL) { 443177633Sdfr if (netid) 444177633Sdfr free(netid, M_RPC); 445177633Sdfr mtx_unlock(&pool->sp_lock); 446177633Sdfr return (FALSE); 447177633Sdfr } 448177633Sdfr 449177633Sdfr s->sc_prog = prog; 450177633Sdfr s->sc_vers = vers; 451177633Sdfr s->sc_dispatch = dispatch; 452177633Sdfr s->sc_netid = netid; 453177633Sdfr TAILQ_INSERT_TAIL(&pool->sp_callouts, s, sc_link); 454177633Sdfr 455177633Sdfr if ((xprt->xp_netid == NULL) && (flag == 1) && netid) 456177633Sdfr ((SVCXPRT *) xprt)->xp_netid = strdup(netid, M_RPC); 457177633Sdfr 458177633Sdfrrpcb_it: 459177633Sdfr mtx_unlock(&pool->sp_lock); 460177633Sdfr /* now register the information with the local binder service */ 461177633Sdfr if (nconf) { 462177633Sdfr bool_t dummy; 463177633Sdfr struct netconfig tnc; 464184588Sdfr struct netbuf nb; 465177633Sdfr tnc = *nconf; 466184588Sdfr nb.buf = &xprt->xp_ltaddr; 467184588Sdfr nb.len = xprt->xp_ltaddr.ss_len; 468184588Sdfr dummy = rpcb_set(prog, vers, &tnc, &nb); 469177633Sdfr return (dummy); 470177633Sdfr } 471177633Sdfr return (TRUE); 472177633Sdfr} 473177633Sdfr 474177633Sdfr/* 475177633Sdfr * Remove a service program from the callout list. 476177633Sdfr */ 477177633Sdfrvoid 478177633Sdfrsvc_unreg(SVCPOOL *pool, const rpcprog_t prog, const rpcvers_t vers) 479177633Sdfr{ 480177633Sdfr struct svc_callout *s; 481177633Sdfr 482177633Sdfr /* unregister the information anyway */ 483177633Sdfr (void) rpcb_unset(prog, vers, NULL); 484177633Sdfr mtx_lock(&pool->sp_lock); 485177633Sdfr while ((s = svc_find(pool, prog, vers, NULL)) != NULL) { 486177633Sdfr TAILQ_REMOVE(&pool->sp_callouts, s, sc_link); 487177633Sdfr if (s->sc_netid) 488177633Sdfr mem_free(s->sc_netid, sizeof (s->sc_netid) + 1); 489177633Sdfr mem_free(s, sizeof (struct svc_callout)); 490177633Sdfr } 491177633Sdfr mtx_unlock(&pool->sp_lock); 492177633Sdfr} 493177633Sdfr 494177633Sdfr/* ********************** CALLOUT list related stuff ************* */ 495177633Sdfr 496177633Sdfr/* 497177633Sdfr * Search the callout list for a program number, return the callout 498177633Sdfr * struct. 499177633Sdfr */ 500177633Sdfrstatic struct svc_callout * 501177633Sdfrsvc_find(SVCPOOL *pool, rpcprog_t prog, rpcvers_t vers, char *netid) 502177633Sdfr{ 503177633Sdfr struct svc_callout *s; 504177633Sdfr 505177633Sdfr mtx_assert(&pool->sp_lock, MA_OWNED); 506177633Sdfr TAILQ_FOREACH(s, &pool->sp_callouts, sc_link) { 507177633Sdfr if (s->sc_prog == prog && s->sc_vers == vers 508177633Sdfr && (netid == NULL || s->sc_netid == NULL || 509177633Sdfr strcmp(netid, s->sc_netid) == 0)) 510177633Sdfr break; 511177633Sdfr } 512177633Sdfr 513177633Sdfr return (s); 514177633Sdfr} 515177633Sdfr 516177633Sdfr/* ******************* REPLY GENERATION ROUTINES ************ */ 517177633Sdfr 518184588Sdfrstatic bool_t 519184588Sdfrsvc_sendreply_common(struct svc_req *rqstp, struct rpc_msg *rply, 520184588Sdfr struct mbuf *body) 521184588Sdfr{ 522184588Sdfr SVCXPRT *xprt = rqstp->rq_xprt; 523184588Sdfr bool_t ok; 524184588Sdfr 525184588Sdfr if (rqstp->rq_args) { 526184588Sdfr m_freem(rqstp->rq_args); 527184588Sdfr rqstp->rq_args = NULL; 528184588Sdfr } 529184588Sdfr 530184588Sdfr if (xprt->xp_pool->sp_rcache) 531184588Sdfr replay_setreply(xprt->xp_pool->sp_rcache, 532184588Sdfr rply, svc_getrpccaller(rqstp), body); 533184588Sdfr 534184588Sdfr if (!SVCAUTH_WRAP(&rqstp->rq_auth, &body)) 535184588Sdfr return (FALSE); 536184588Sdfr 537184588Sdfr ok = SVC_REPLY(xprt, rply, rqstp->rq_addr, body); 538184588Sdfr if (rqstp->rq_addr) { 539184588Sdfr free(rqstp->rq_addr, M_SONAME); 540184588Sdfr rqstp->rq_addr = NULL; 541184588Sdfr } 542184588Sdfr 543184588Sdfr return (ok); 544184588Sdfr} 545184588Sdfr 546177633Sdfr/* 547177633Sdfr * Send a reply to an rpc request 548177633Sdfr */ 549177633Sdfrbool_t 550184588Sdfrsvc_sendreply(struct svc_req *rqstp, xdrproc_t xdr_results, void * xdr_location) 551177633Sdfr{ 552177633Sdfr struct rpc_msg rply; 553184588Sdfr struct mbuf *m; 554184588Sdfr XDR xdrs; 555184588Sdfr bool_t ok; 556177633Sdfr 557184588Sdfr rply.rm_xid = rqstp->rq_xid; 558177633Sdfr rply.rm_direction = REPLY; 559177633Sdfr rply.rm_reply.rp_stat = MSG_ACCEPTED; 560184588Sdfr rply.acpted_rply.ar_verf = rqstp->rq_verf; 561177633Sdfr rply.acpted_rply.ar_stat = SUCCESS; 562184588Sdfr rply.acpted_rply.ar_results.where = NULL; 563184588Sdfr rply.acpted_rply.ar_results.proc = (xdrproc_t) xdr_void; 564177633Sdfr 565248195Sglebius m = m_getcl(M_WAITOK, MT_DATA, 0); 566184588Sdfr xdrmbuf_create(&xdrs, m, XDR_ENCODE); 567184588Sdfr ok = xdr_results(&xdrs, xdr_location); 568184588Sdfr XDR_DESTROY(&xdrs); 569184588Sdfr 570184588Sdfr if (ok) { 571184588Sdfr return (svc_sendreply_common(rqstp, &rply, m)); 572184588Sdfr } else { 573184588Sdfr m_freem(m); 574184588Sdfr return (FALSE); 575184588Sdfr } 576177633Sdfr} 577177633Sdfr 578184588Sdfrbool_t 579184588Sdfrsvc_sendreply_mbuf(struct svc_req *rqstp, struct mbuf *m) 580184588Sdfr{ 581184588Sdfr struct rpc_msg rply; 582184588Sdfr 583184588Sdfr rply.rm_xid = rqstp->rq_xid; 584184588Sdfr rply.rm_direction = REPLY; 585184588Sdfr rply.rm_reply.rp_stat = MSG_ACCEPTED; 586184588Sdfr rply.acpted_rply.ar_verf = rqstp->rq_verf; 587184588Sdfr rply.acpted_rply.ar_stat = SUCCESS; 588184588Sdfr rply.acpted_rply.ar_results.where = NULL; 589184588Sdfr rply.acpted_rply.ar_results.proc = (xdrproc_t) xdr_void; 590184588Sdfr 591184588Sdfr return (svc_sendreply_common(rqstp, &rply, m)); 592184588Sdfr} 593184588Sdfr 594177633Sdfr/* 595177633Sdfr * No procedure error reply 596177633Sdfr */ 597177633Sdfrvoid 598184588Sdfrsvcerr_noproc(struct svc_req *rqstp) 599177633Sdfr{ 600184588Sdfr SVCXPRT *xprt = rqstp->rq_xprt; 601177633Sdfr struct rpc_msg rply; 602177633Sdfr 603184588Sdfr rply.rm_xid = rqstp->rq_xid; 604177633Sdfr rply.rm_direction = REPLY; 605177633Sdfr rply.rm_reply.rp_stat = MSG_ACCEPTED; 606184588Sdfr rply.acpted_rply.ar_verf = rqstp->rq_verf; 607177633Sdfr rply.acpted_rply.ar_stat = PROC_UNAVAIL; 608177633Sdfr 609184588Sdfr if (xprt->xp_pool->sp_rcache) 610184588Sdfr replay_setreply(xprt->xp_pool->sp_rcache, 611184588Sdfr &rply, svc_getrpccaller(rqstp), NULL); 612184588Sdfr 613184588Sdfr svc_sendreply_common(rqstp, &rply, NULL); 614177633Sdfr} 615177633Sdfr 616177633Sdfr/* 617177633Sdfr * Can't decode args error reply 618177633Sdfr */ 619177633Sdfrvoid 620184588Sdfrsvcerr_decode(struct svc_req *rqstp) 621177633Sdfr{ 622184588Sdfr SVCXPRT *xprt = rqstp->rq_xprt; 623177633Sdfr struct rpc_msg rply; 624177633Sdfr 625184588Sdfr rply.rm_xid = rqstp->rq_xid; 626177633Sdfr rply.rm_direction = REPLY; 627177633Sdfr rply.rm_reply.rp_stat = MSG_ACCEPTED; 628184588Sdfr rply.acpted_rply.ar_verf = rqstp->rq_verf; 629177633Sdfr rply.acpted_rply.ar_stat = GARBAGE_ARGS; 630177633Sdfr 631184588Sdfr if (xprt->xp_pool->sp_rcache) 632184588Sdfr replay_setreply(xprt->xp_pool->sp_rcache, 633184588Sdfr &rply, (struct sockaddr *) &xprt->xp_rtaddr, NULL); 634184588Sdfr 635184588Sdfr svc_sendreply_common(rqstp, &rply, NULL); 636177633Sdfr} 637177633Sdfr 638177633Sdfr/* 639177633Sdfr * Some system error 640177633Sdfr */ 641177633Sdfrvoid 642184588Sdfrsvcerr_systemerr(struct svc_req *rqstp) 643177633Sdfr{ 644184588Sdfr SVCXPRT *xprt = rqstp->rq_xprt; 645177633Sdfr struct rpc_msg rply; 646177633Sdfr 647184588Sdfr rply.rm_xid = rqstp->rq_xid; 648177633Sdfr rply.rm_direction = REPLY; 649177633Sdfr rply.rm_reply.rp_stat = MSG_ACCEPTED; 650184588Sdfr rply.acpted_rply.ar_verf = rqstp->rq_verf; 651177633Sdfr rply.acpted_rply.ar_stat = SYSTEM_ERR; 652177633Sdfr 653184588Sdfr if (xprt->xp_pool->sp_rcache) 654184588Sdfr replay_setreply(xprt->xp_pool->sp_rcache, 655184588Sdfr &rply, svc_getrpccaller(rqstp), NULL); 656184588Sdfr 657184588Sdfr svc_sendreply_common(rqstp, &rply, NULL); 658177633Sdfr} 659177633Sdfr 660177633Sdfr/* 661177633Sdfr * Authentication error reply 662177633Sdfr */ 663177633Sdfrvoid 664184588Sdfrsvcerr_auth(struct svc_req *rqstp, enum auth_stat why) 665177633Sdfr{ 666184588Sdfr SVCXPRT *xprt = rqstp->rq_xprt; 667177633Sdfr struct rpc_msg rply; 668177633Sdfr 669184588Sdfr rply.rm_xid = rqstp->rq_xid; 670177633Sdfr rply.rm_direction = REPLY; 671177633Sdfr rply.rm_reply.rp_stat = MSG_DENIED; 672177633Sdfr rply.rjcted_rply.rj_stat = AUTH_ERROR; 673177633Sdfr rply.rjcted_rply.rj_why = why; 674177633Sdfr 675184588Sdfr if (xprt->xp_pool->sp_rcache) 676184588Sdfr replay_setreply(xprt->xp_pool->sp_rcache, 677184588Sdfr &rply, svc_getrpccaller(rqstp), NULL); 678184588Sdfr 679184588Sdfr svc_sendreply_common(rqstp, &rply, NULL); 680177633Sdfr} 681177633Sdfr 682177633Sdfr/* 683177633Sdfr * Auth too weak error reply 684177633Sdfr */ 685177633Sdfrvoid 686184588Sdfrsvcerr_weakauth(struct svc_req *rqstp) 687177633Sdfr{ 688177633Sdfr 689184588Sdfr svcerr_auth(rqstp, AUTH_TOOWEAK); 690177633Sdfr} 691177633Sdfr 692177633Sdfr/* 693177633Sdfr * Program unavailable error reply 694177633Sdfr */ 695177633Sdfrvoid 696184588Sdfrsvcerr_noprog(struct svc_req *rqstp) 697177633Sdfr{ 698184588Sdfr SVCXPRT *xprt = rqstp->rq_xprt; 699177633Sdfr struct rpc_msg rply; 700177633Sdfr 701184588Sdfr rply.rm_xid = rqstp->rq_xid; 702177633Sdfr rply.rm_direction = REPLY; 703177633Sdfr rply.rm_reply.rp_stat = MSG_ACCEPTED; 704184588Sdfr rply.acpted_rply.ar_verf = rqstp->rq_verf; 705177633Sdfr rply.acpted_rply.ar_stat = PROG_UNAVAIL; 706177633Sdfr 707184588Sdfr if (xprt->xp_pool->sp_rcache) 708184588Sdfr replay_setreply(xprt->xp_pool->sp_rcache, 709184588Sdfr &rply, svc_getrpccaller(rqstp), NULL); 710184588Sdfr 711184588Sdfr svc_sendreply_common(rqstp, &rply, NULL); 712177633Sdfr} 713177633Sdfr 714177633Sdfr/* 715177633Sdfr * Program version mismatch error reply 716177633Sdfr */ 717177633Sdfrvoid 718184588Sdfrsvcerr_progvers(struct svc_req *rqstp, rpcvers_t low_vers, rpcvers_t high_vers) 719177633Sdfr{ 720184588Sdfr SVCXPRT *xprt = rqstp->rq_xprt; 721177633Sdfr struct rpc_msg rply; 722177633Sdfr 723184588Sdfr rply.rm_xid = rqstp->rq_xid; 724177633Sdfr rply.rm_direction = REPLY; 725177633Sdfr rply.rm_reply.rp_stat = MSG_ACCEPTED; 726184588Sdfr rply.acpted_rply.ar_verf = rqstp->rq_verf; 727177633Sdfr rply.acpted_rply.ar_stat = PROG_MISMATCH; 728177633Sdfr rply.acpted_rply.ar_vers.low = (uint32_t)low_vers; 729177633Sdfr rply.acpted_rply.ar_vers.high = (uint32_t)high_vers; 730177633Sdfr 731184588Sdfr if (xprt->xp_pool->sp_rcache) 732184588Sdfr replay_setreply(xprt->xp_pool->sp_rcache, 733184588Sdfr &rply, svc_getrpccaller(rqstp), NULL); 734184588Sdfr 735184588Sdfr svc_sendreply_common(rqstp, &rply, NULL); 736177633Sdfr} 737177633Sdfr 738184588Sdfr/* 739184588Sdfr * Allocate a new server transport structure. All fields are 740184588Sdfr * initialized to zero and xp_p3 is initialized to point at an 741184588Sdfr * extension structure to hold various flags and authentication 742184588Sdfr * parameters. 743184588Sdfr */ 744184588SdfrSVCXPRT * 745184588Sdfrsvc_xprt_alloc() 746184588Sdfr{ 747184588Sdfr SVCXPRT *xprt; 748184588Sdfr SVCXPRT_EXT *ext; 749184588Sdfr 750184588Sdfr xprt = mem_alloc(sizeof(SVCXPRT)); 751184588Sdfr memset(xprt, 0, sizeof(SVCXPRT)); 752184588Sdfr ext = mem_alloc(sizeof(SVCXPRT_EXT)); 753184588Sdfr memset(ext, 0, sizeof(SVCXPRT_EXT)); 754184588Sdfr xprt->xp_p3 = ext; 755184588Sdfr refcount_init(&xprt->xp_refs, 1); 756184588Sdfr 757184588Sdfr return (xprt); 758184588Sdfr} 759184588Sdfr 760184588Sdfr/* 761184588Sdfr * Free a server transport structure. 762184588Sdfr */ 763184588Sdfrvoid 764184588Sdfrsvc_xprt_free(xprt) 765184588Sdfr SVCXPRT *xprt; 766184588Sdfr{ 767184588Sdfr 768184588Sdfr mem_free(xprt->xp_p3, sizeof(SVCXPRT_EXT)); 769184588Sdfr mem_free(xprt, sizeof(SVCXPRT)); 770184588Sdfr} 771184588Sdfr 772177633Sdfr/* ******************* SERVER INPUT STUFF ******************* */ 773177633Sdfr 774177633Sdfr/* 775184588Sdfr * Read RPC requests from a transport and queue them to be 776184588Sdfr * executed. We handle authentication and replay cache replies here. 777184588Sdfr * Actually dispatching the RPC is deferred till svc_executereq. 778177633Sdfr */ 779184588Sdfrstatic enum xprt_stat 780184588Sdfrsvc_getreq(SVCXPRT *xprt, struct svc_req **rqstp_ret) 781177633Sdfr{ 782177633Sdfr SVCPOOL *pool = xprt->xp_pool; 783184588Sdfr struct svc_req *r; 784177633Sdfr struct rpc_msg msg; 785184588Sdfr struct mbuf *args; 786177633Sdfr enum xprt_stat stat; 787177633Sdfr 788177633Sdfr /* now receive msgs from xprtprt (support batch calls) */ 789184588Sdfr r = malloc(sizeof(*r), M_RPC, M_WAITOK|M_ZERO); 790177633Sdfr 791184588Sdfr msg.rm_call.cb_cred.oa_base = r->rq_credarea; 792184588Sdfr msg.rm_call.cb_verf.oa_base = &r->rq_credarea[MAX_AUTH_BYTES]; 793184588Sdfr r->rq_clntcred = &r->rq_credarea[2*MAX_AUTH_BYTES]; 794184588Sdfr if (SVC_RECV(xprt, &msg, &r->rq_addr, &args)) { 795184588Sdfr enum auth_stat why; 796177633Sdfr 797184588Sdfr /* 798184588Sdfr * Handle replays and authenticate before queuing the 799184588Sdfr * request to be executed. 800184588Sdfr */ 801184588Sdfr SVC_ACQUIRE(xprt); 802184588Sdfr r->rq_xprt = xprt; 803184588Sdfr if (pool->sp_rcache) { 804184588Sdfr struct rpc_msg repmsg; 805184588Sdfr struct mbuf *repbody; 806184588Sdfr enum replay_state rs; 807184588Sdfr rs = replay_find(pool->sp_rcache, &msg, 808184588Sdfr svc_getrpccaller(r), &repmsg, &repbody); 809184588Sdfr switch (rs) { 810184588Sdfr case RS_NEW: 811184588Sdfr break; 812184588Sdfr case RS_DONE: 813184588Sdfr SVC_REPLY(xprt, &repmsg, r->rq_addr, 814184588Sdfr repbody); 815184588Sdfr if (r->rq_addr) { 816184588Sdfr free(r->rq_addr, M_SONAME); 817184588Sdfr r->rq_addr = NULL; 818184588Sdfr } 819205562Srmacklem m_freem(args); 820177633Sdfr goto call_done; 821184588Sdfr 822184588Sdfr default: 823205562Srmacklem m_freem(args); 824184588Sdfr goto call_done; 825177633Sdfr } 826184588Sdfr } 827184588Sdfr 828184588Sdfr r->rq_xid = msg.rm_xid; 829184588Sdfr r->rq_prog = msg.rm_call.cb_prog; 830184588Sdfr r->rq_vers = msg.rm_call.cb_vers; 831184588Sdfr r->rq_proc = msg.rm_call.cb_proc; 832184588Sdfr r->rq_size = sizeof(*r) + m_length(args, NULL); 833184588Sdfr r->rq_args = args; 834184588Sdfr if ((why = _authenticate(r, &msg)) != AUTH_OK) { 835177633Sdfr /* 836184588Sdfr * RPCSEC_GSS uses this return code 837184588Sdfr * for requests that form part of its 838184588Sdfr * context establishment protocol and 839184588Sdfr * should not be dispatched to the 840184588Sdfr * application. 841177633Sdfr */ 842184588Sdfr if (why != RPCSEC_GSS_NODISPATCH) 843184588Sdfr svcerr_auth(r, why); 844184588Sdfr goto call_done; 845177633Sdfr } 846184588Sdfr 847184588Sdfr if (!SVCAUTH_UNWRAP(&r->rq_auth, &r->rq_args)) { 848184588Sdfr svcerr_decode(r); 849184588Sdfr goto call_done; 850184588Sdfr } 851184588Sdfr 852177633Sdfr /* 853184588Sdfr * Everything checks out, return request to caller. 854177633Sdfr */ 855184588Sdfr *rqstp_ret = r; 856184588Sdfr r = NULL; 857184588Sdfr } 858177633Sdfrcall_done: 859184588Sdfr if (r) { 860184588Sdfr svc_freereq(r); 861184588Sdfr r = NULL; 862184588Sdfr } 863184588Sdfr if ((stat = SVC_STAT(xprt)) == XPRT_DIED) { 864184588Sdfr xprt_unregister(xprt); 865184588Sdfr } 866184588Sdfr 867184588Sdfr return (stat); 868184588Sdfr} 869184588Sdfr 870184588Sdfrstatic void 871184588Sdfrsvc_executereq(struct svc_req *rqstp) 872184588Sdfr{ 873184588Sdfr SVCXPRT *xprt = rqstp->rq_xprt; 874184588Sdfr SVCPOOL *pool = xprt->xp_pool; 875184588Sdfr int prog_found; 876184588Sdfr rpcvers_t low_vers; 877184588Sdfr rpcvers_t high_vers; 878184588Sdfr struct svc_callout *s; 879184588Sdfr 880184588Sdfr /* now match message with a registered service*/ 881184588Sdfr prog_found = FALSE; 882184588Sdfr low_vers = (rpcvers_t) -1L; 883184588Sdfr high_vers = (rpcvers_t) 0L; 884184588Sdfr TAILQ_FOREACH(s, &pool->sp_callouts, sc_link) { 885184588Sdfr if (s->sc_prog == rqstp->rq_prog) { 886184588Sdfr if (s->sc_vers == rqstp->rq_vers) { 887184588Sdfr /* 888184588Sdfr * We hand ownership of r to the 889184588Sdfr * dispatch method - they must call 890184588Sdfr * svc_freereq. 891184588Sdfr */ 892184588Sdfr (*s->sc_dispatch)(rqstp, xprt); 893184588Sdfr return; 894184588Sdfr } /* found correct version */ 895184588Sdfr prog_found = TRUE; 896184588Sdfr if (s->sc_vers < low_vers) 897184588Sdfr low_vers = s->sc_vers; 898184588Sdfr if (s->sc_vers > high_vers) 899184588Sdfr high_vers = s->sc_vers; 900184588Sdfr } /* found correct program */ 901184588Sdfr } 902184588Sdfr 903184588Sdfr /* 904184588Sdfr * if we got here, the program or version 905184588Sdfr * is not served ... 906184588Sdfr */ 907184588Sdfr if (prog_found) 908184588Sdfr svcerr_progvers(rqstp, low_vers, high_vers); 909184588Sdfr else 910184588Sdfr svcerr_noprog(rqstp); 911184588Sdfr 912184588Sdfr svc_freereq(rqstp); 913184588Sdfr} 914184588Sdfr 915184588Sdfrstatic void 916184588Sdfrsvc_checkidle(SVCPOOL *pool) 917184588Sdfr{ 918184588Sdfr SVCXPRT *xprt, *nxprt; 919184588Sdfr time_t timo; 920184588Sdfr struct svcxprt_list cleanup; 921184588Sdfr 922184588Sdfr TAILQ_INIT(&cleanup); 923184588Sdfr TAILQ_FOREACH_SAFE(xprt, &pool->sp_xlist, xp_link, nxprt) { 924184588Sdfr /* 925184588Sdfr * Only some transports have idle timers. Don't time 926184588Sdfr * something out which is just waking up. 927184588Sdfr */ 928184588Sdfr if (!xprt->xp_idletimeout || xprt->xp_thread) 929184588Sdfr continue; 930184588Sdfr 931184588Sdfr timo = xprt->xp_lastactive + xprt->xp_idletimeout; 932184588Sdfr if (time_uptime > timo) { 933184588Sdfr xprt_unregister_locked(xprt); 934184588Sdfr TAILQ_INSERT_TAIL(&cleanup, xprt, xp_link); 935177633Sdfr } 936184588Sdfr } 937184588Sdfr 938184588Sdfr mtx_unlock(&pool->sp_lock); 939184588Sdfr TAILQ_FOREACH_SAFE(xprt, &cleanup, xp_link, nxprt) { 940184588Sdfr SVC_RELEASE(xprt); 941184588Sdfr } 942184588Sdfr mtx_lock(&pool->sp_lock); 943184588Sdfr 944177633Sdfr} 945177633Sdfr 946184588Sdfrstatic void 947184588Sdfrsvc_assign_waiting_sockets(SVCPOOL *pool) 948177633Sdfr{ 949177633Sdfr SVCXPRT *xprt; 950184588Sdfr 951184588Sdfr TAILQ_FOREACH(xprt, &pool->sp_active, xp_alink) { 952184588Sdfr if (!xprt->xp_thread) { 953184588Sdfr xprt_assignthread(xprt); 954184588Sdfr } 955184588Sdfr } 956184588Sdfr} 957184588Sdfr 958184588Sdfrstatic bool_t 959184588Sdfrsvc_request_space_available(SVCPOOL *pool) 960184588Sdfr{ 961184588Sdfr 962184588Sdfr mtx_assert(&pool->sp_lock, MA_OWNED); 963184588Sdfr 964184588Sdfr if (pool->sp_space_throttled) { 965184588Sdfr /* 966184588Sdfr * Below the low-water yet? If so, assign any waiting sockets. 967184588Sdfr */ 968184588Sdfr if (pool->sp_space_used < pool->sp_space_low) { 969184588Sdfr pool->sp_space_throttled = FALSE; 970184588Sdfr svc_assign_waiting_sockets(pool); 971184588Sdfr return TRUE; 972184588Sdfr } 973184588Sdfr 974184588Sdfr return FALSE; 975184588Sdfr } else { 976184588Sdfr if (pool->sp_space_used 977184588Sdfr >= pool->sp_space_high) { 978184588Sdfr pool->sp_space_throttled = TRUE; 979184588Sdfr pool->sp_space_throttle_count++; 980184588Sdfr return FALSE; 981184588Sdfr } 982184588Sdfr 983184588Sdfr return TRUE; 984184588Sdfr } 985184588Sdfr} 986184588Sdfr 987184588Sdfrstatic void 988184588Sdfrsvc_run_internal(SVCPOOL *pool, bool_t ismaster) 989184588Sdfr{ 990184588Sdfr SVCTHREAD *st, *stpref; 991184588Sdfr SVCXPRT *xprt; 992184588Sdfr enum xprt_stat stat; 993184588Sdfr struct svc_req *rqstp; 994177633Sdfr int error; 995177633Sdfr 996184588Sdfr st = mem_alloc(sizeof(*st)); 997184588Sdfr st->st_xprt = NULL; 998184588Sdfr STAILQ_INIT(&st->st_reqs); 999184588Sdfr cv_init(&st->st_cond, "rpcsvc"); 1000184588Sdfr 1001177633Sdfr mtx_lock(&pool->sp_lock); 1002184588Sdfr LIST_INSERT_HEAD(&pool->sp_threads, st, st_link); 1003177633Sdfr 1004184588Sdfr /* 1005184588Sdfr * If we are a new thread which was spawned to cope with 1006184588Sdfr * increased load, set the state back to SVCPOOL_ACTIVE. 1007184588Sdfr */ 1008184588Sdfr if (pool->sp_state == SVCPOOL_THREADSTARTING) 1009184588Sdfr pool->sp_state = SVCPOOL_ACTIVE; 1010177633Sdfr 1011184588Sdfr while (pool->sp_state != SVCPOOL_CLOSING) { 1012184588Sdfr /* 1013261045Smav * Create new thread if requested. 1014261045Smav */ 1015261045Smav if (pool->sp_state == SVCPOOL_THREADWANTED) { 1016261045Smav pool->sp_state = SVCPOOL_THREADSTARTING; 1017261045Smav pool->sp_lastcreatetime = time_uptime; 1018261045Smav mtx_unlock(&pool->sp_lock); 1019261045Smav svc_new_thread(pool); 1020261045Smav mtx_lock(&pool->sp_lock); 1021261045Smav continue; 1022261045Smav } 1023261045Smav 1024261045Smav /* 1025184588Sdfr * Check for idle transports once per second. 1026184588Sdfr */ 1027184588Sdfr if (time_uptime > pool->sp_lastidlecheck) { 1028184588Sdfr pool->sp_lastidlecheck = time_uptime; 1029184588Sdfr svc_checkidle(pool); 1030184588Sdfr } 1031184588Sdfr 1032184588Sdfr xprt = st->st_xprt; 1033184588Sdfr if (!xprt && STAILQ_EMPTY(&st->st_reqs)) { 1034184588Sdfr /* 1035184588Sdfr * Enforce maxthreads count. 1036184588Sdfr */ 1037184588Sdfr if (pool->sp_threadcount > pool->sp_maxthreads) 1038177633Sdfr break; 1039184588Sdfr 1040184588Sdfr /* 1041184588Sdfr * Before sleeping, see if we can find an 1042184588Sdfr * active transport which isn't being serviced 1043184588Sdfr * by a thread. 1044184588Sdfr */ 1045184588Sdfr if (svc_request_space_available(pool)) { 1046184588Sdfr TAILQ_FOREACH(xprt, &pool->sp_active, 1047184588Sdfr xp_alink) { 1048184588Sdfr if (!xprt->xp_thread) { 1049184588Sdfr SVC_ACQUIRE(xprt); 1050184588Sdfr xprt->xp_thread = st; 1051184588Sdfr st->st_xprt = xprt; 1052184588Sdfr break; 1053184588Sdfr } 1054184588Sdfr } 1055184588Sdfr } 1056184588Sdfr if (st->st_xprt) 1057184588Sdfr continue; 1058184588Sdfr 1059184588Sdfr LIST_INSERT_HEAD(&pool->sp_idlethreads, st, st_ilink); 1060261045Smav if (ismaster || (!ismaster && 1061261045Smav pool->sp_threadcount > pool->sp_minthreads)) 1062261045Smav error = cv_timedwait_sig(&st->st_cond, 1063261045Smav &pool->sp_lock, 5 * hz); 1064261045Smav else 1065261045Smav error = cv_wait_sig(&st->st_cond, 1066261045Smav &pool->sp_lock); 1067184588Sdfr LIST_REMOVE(st, st_ilink); 1068184588Sdfr 1069184588Sdfr /* 1070184588Sdfr * Reduce worker thread count when idle. 1071184588Sdfr */ 1072184588Sdfr if (error == EWOULDBLOCK) { 1073184588Sdfr if (!ismaster 1074184588Sdfr && (pool->sp_threadcount 1075184588Sdfr > pool->sp_minthreads) 1076184588Sdfr && !st->st_xprt 1077184588Sdfr && STAILQ_EMPTY(&st->st_reqs)) 1078184588Sdfr break; 1079261045Smav } else if (error) { 1080184588Sdfr mtx_unlock(&pool->sp_lock); 1081261045Smav svc_exit(pool); 1082184588Sdfr mtx_lock(&pool->sp_lock); 1083261045Smav break; 1084184588Sdfr } 1085177633Sdfr continue; 1086177633Sdfr } 1087177633Sdfr 1088184588Sdfr if (xprt) { 1089184588Sdfr /* 1090184588Sdfr * Drain the transport socket and queue up any 1091184588Sdfr * RPCs. 1092184588Sdfr */ 1093184588Sdfr xprt->xp_lastactive = time_uptime; 1094184588Sdfr stat = XPRT_IDLE; 1095184588Sdfr do { 1096184588Sdfr if (!svc_request_space_available(pool)) 1097184588Sdfr break; 1098184588Sdfr rqstp = NULL; 1099184588Sdfr mtx_unlock(&pool->sp_lock); 1100184588Sdfr stat = svc_getreq(xprt, &rqstp); 1101184588Sdfr mtx_lock(&pool->sp_lock); 1102184588Sdfr if (rqstp) { 1103184588Sdfr /* 1104184588Sdfr * See if the application has 1105184588Sdfr * a preference for some other 1106184588Sdfr * thread. 1107184588Sdfr */ 1108184588Sdfr stpref = st; 1109184588Sdfr if (pool->sp_assign) 1110184588Sdfr stpref = pool->sp_assign(st, 1111184588Sdfr rqstp); 1112184588Sdfr 1113184588Sdfr pool->sp_space_used += 1114184588Sdfr rqstp->rq_size; 1115184588Sdfr if (pool->sp_space_used 1116184588Sdfr > pool->sp_space_used_highest) 1117184588Sdfr pool->sp_space_used_highest = 1118184588Sdfr pool->sp_space_used; 1119184588Sdfr rqstp->rq_thread = stpref; 1120184588Sdfr STAILQ_INSERT_TAIL(&stpref->st_reqs, 1121184588Sdfr rqstp, rq_link); 1122184588Sdfr stpref->st_reqcount++; 1123184588Sdfr 1124184588Sdfr /* 1125184588Sdfr * If we assigned the request 1126184588Sdfr * to another thread, make 1127184588Sdfr * sure its awake and continue 1128184588Sdfr * reading from the 1129184588Sdfr * socket. Otherwise, try to 1130184588Sdfr * find some other thread to 1131184588Sdfr * read from the socket and 1132184588Sdfr * execute the request 1133184588Sdfr * immediately. 1134184588Sdfr */ 1135184588Sdfr if (stpref != st) { 1136184588Sdfr cv_signal(&stpref->st_cond); 1137184588Sdfr continue; 1138184588Sdfr } else { 1139184588Sdfr break; 1140184588Sdfr } 1141184588Sdfr } 1142184588Sdfr } while (stat == XPRT_MOREREQS 1143184588Sdfr && pool->sp_state != SVCPOOL_CLOSING); 1144184588Sdfr 1145184588Sdfr /* 1146184588Sdfr * Move this transport to the end of the 1147184588Sdfr * active list to ensure fairness when 1148184588Sdfr * multiple transports are active. If this was 1149184588Sdfr * the last queued request, svc_getreq will 1150184588Sdfr * end up calling xprt_inactive to remove from 1151184588Sdfr * the active list. 1152184588Sdfr */ 1153184588Sdfr xprt->xp_thread = NULL; 1154184588Sdfr st->st_xprt = NULL; 1155184588Sdfr if (xprt->xp_active) { 1156184588Sdfr xprt_assignthread(xprt); 1157184588Sdfr TAILQ_REMOVE(&pool->sp_active, xprt, xp_alink); 1158184588Sdfr TAILQ_INSERT_TAIL(&pool->sp_active, xprt, 1159184588Sdfr xp_alink); 1160184588Sdfr } 1161184588Sdfr mtx_unlock(&pool->sp_lock); 1162184588Sdfr SVC_RELEASE(xprt); 1163184588Sdfr mtx_lock(&pool->sp_lock); 1164184588Sdfr } 1165184588Sdfr 1166177633Sdfr /* 1167184588Sdfr * Execute what we have queued. 1168177633Sdfr */ 1169184588Sdfr while ((rqstp = STAILQ_FIRST(&st->st_reqs)) != NULL) { 1170184588Sdfr size_t sz = rqstp->rq_size; 1171184588Sdfr mtx_unlock(&pool->sp_lock); 1172184588Sdfr svc_executereq(rqstp); 1173184588Sdfr mtx_lock(&pool->sp_lock); 1174184588Sdfr pool->sp_space_used -= sz; 1175184588Sdfr } 1176184588Sdfr } 1177177633Sdfr 1178184588Sdfr if (st->st_xprt) { 1179184588Sdfr xprt = st->st_xprt; 1180184588Sdfr st->st_xprt = NULL; 1181184588Sdfr SVC_RELEASE(xprt); 1182177633Sdfr } 1183177633Sdfr 1184184588Sdfr KASSERT(STAILQ_EMPTY(&st->st_reqs), ("stray reqs on exit")); 1185184588Sdfr LIST_REMOVE(st, st_link); 1186184588Sdfr pool->sp_threadcount--; 1187184588Sdfr 1188177633Sdfr mtx_unlock(&pool->sp_lock); 1189184588Sdfr 1190184588Sdfr cv_destroy(&st->st_cond); 1191184588Sdfr mem_free(st, sizeof(*st)); 1192184588Sdfr 1193184588Sdfr if (!ismaster) 1194184588Sdfr wakeup(pool); 1195177633Sdfr} 1196177633Sdfr 1197184588Sdfrstatic void 1198184588Sdfrsvc_thread_start(void *arg) 1199184588Sdfr{ 1200184588Sdfr 1201184588Sdfr svc_run_internal((SVCPOOL *) arg, FALSE); 1202184588Sdfr kthread_exit(); 1203184588Sdfr} 1204184588Sdfr 1205184588Sdfrstatic void 1206184588Sdfrsvc_new_thread(SVCPOOL *pool) 1207184588Sdfr{ 1208184588Sdfr struct thread *td; 1209184588Sdfr 1210184588Sdfr pool->sp_threadcount++; 1211184588Sdfr kthread_add(svc_thread_start, pool, 1212184588Sdfr pool->sp_proc, &td, 0, 0, 1213184588Sdfr "%s: service", pool->sp_name); 1214184588Sdfr} 1215184588Sdfr 1216177633Sdfrvoid 1217184588Sdfrsvc_run(SVCPOOL *pool) 1218184588Sdfr{ 1219184588Sdfr int i; 1220184588Sdfr struct proc *p; 1221184588Sdfr struct thread *td; 1222184588Sdfr 1223184588Sdfr p = curproc; 1224184588Sdfr td = curthread; 1225184588Sdfr snprintf(td->td_name, sizeof(td->td_name), 1226184588Sdfr "%s: master", pool->sp_name); 1227184588Sdfr pool->sp_state = SVCPOOL_ACTIVE; 1228184588Sdfr pool->sp_proc = p; 1229184588Sdfr pool->sp_lastcreatetime = time_uptime; 1230184588Sdfr pool->sp_threadcount = 1; 1231184588Sdfr 1232184588Sdfr for (i = 1; i < pool->sp_minthreads; i++) { 1233184588Sdfr svc_new_thread(pool); 1234184588Sdfr } 1235184588Sdfr 1236184588Sdfr svc_run_internal(pool, TRUE); 1237184588Sdfr 1238184588Sdfr mtx_lock(&pool->sp_lock); 1239184588Sdfr while (pool->sp_threadcount > 0) 1240184588Sdfr msleep(pool, &pool->sp_lock, 0, "svcexit", 0); 1241184588Sdfr mtx_unlock(&pool->sp_lock); 1242184588Sdfr} 1243184588Sdfr 1244184588Sdfrvoid 1245177633Sdfrsvc_exit(SVCPOOL *pool) 1246177633Sdfr{ 1247184588Sdfr SVCTHREAD *st; 1248184588Sdfr 1249177633Sdfr mtx_lock(&pool->sp_lock); 1250184588Sdfr 1251261045Smav if (pool->sp_state != SVCPOOL_CLOSING) { 1252261045Smav pool->sp_state = SVCPOOL_CLOSING; 1253261045Smav LIST_FOREACH(st, &pool->sp_idlethreads, st_ilink) 1254261045Smav cv_signal(&st->st_cond); 1255261045Smav } 1256184588Sdfr 1257177633Sdfr mtx_unlock(&pool->sp_lock); 1258177633Sdfr} 1259184588Sdfr 1260184588Sdfrbool_t 1261184588Sdfrsvc_getargs(struct svc_req *rqstp, xdrproc_t xargs, void *args) 1262184588Sdfr{ 1263184588Sdfr struct mbuf *m; 1264184588Sdfr XDR xdrs; 1265184588Sdfr bool_t stat; 1266184588Sdfr 1267184588Sdfr m = rqstp->rq_args; 1268184588Sdfr rqstp->rq_args = NULL; 1269184588Sdfr 1270184588Sdfr xdrmbuf_create(&xdrs, m, XDR_DECODE); 1271184588Sdfr stat = xargs(&xdrs, args); 1272184588Sdfr XDR_DESTROY(&xdrs); 1273184588Sdfr 1274184588Sdfr return (stat); 1275184588Sdfr} 1276184588Sdfr 1277184588Sdfrbool_t 1278184588Sdfrsvc_freeargs(struct svc_req *rqstp, xdrproc_t xargs, void *args) 1279184588Sdfr{ 1280184588Sdfr XDR xdrs; 1281184588Sdfr 1282184588Sdfr if (rqstp->rq_addr) { 1283184588Sdfr free(rqstp->rq_addr, M_SONAME); 1284184588Sdfr rqstp->rq_addr = NULL; 1285184588Sdfr } 1286184588Sdfr 1287184588Sdfr xdrs.x_op = XDR_FREE; 1288184588Sdfr return (xargs(&xdrs, args)); 1289184588Sdfr} 1290184588Sdfr 1291184588Sdfrvoid 1292184588Sdfrsvc_freereq(struct svc_req *rqstp) 1293184588Sdfr{ 1294184588Sdfr SVCTHREAD *st; 1295184588Sdfr SVCXPRT *xprt; 1296184588Sdfr SVCPOOL *pool; 1297184588Sdfr 1298184588Sdfr st = rqstp->rq_thread; 1299184588Sdfr xprt = rqstp->rq_xprt; 1300184588Sdfr if (xprt) 1301184588Sdfr pool = xprt->xp_pool; 1302184588Sdfr else 1303184588Sdfr pool = NULL; 1304184588Sdfr if (st) { 1305184588Sdfr mtx_lock(&pool->sp_lock); 1306184588Sdfr KASSERT(rqstp == STAILQ_FIRST(&st->st_reqs), 1307184588Sdfr ("Freeing request out of order")); 1308184588Sdfr STAILQ_REMOVE_HEAD(&st->st_reqs, rq_link); 1309184588Sdfr st->st_reqcount--; 1310184588Sdfr if (pool->sp_done) 1311184588Sdfr pool->sp_done(st, rqstp); 1312184588Sdfr mtx_unlock(&pool->sp_lock); 1313184588Sdfr } 1314184588Sdfr 1315184588Sdfr if (rqstp->rq_auth.svc_ah_ops) 1316184588Sdfr SVCAUTH_RELEASE(&rqstp->rq_auth); 1317184588Sdfr 1318184588Sdfr if (rqstp->rq_xprt) { 1319184588Sdfr SVC_RELEASE(rqstp->rq_xprt); 1320184588Sdfr } 1321184588Sdfr 1322184588Sdfr if (rqstp->rq_addr) 1323184588Sdfr free(rqstp->rq_addr, M_SONAME); 1324184588Sdfr 1325184588Sdfr if (rqstp->rq_args) 1326184588Sdfr m_freem(rqstp->rq_args); 1327184588Sdfr 1328184588Sdfr free(rqstp, M_RPC); 1329184588Sdfr} 1330