svc.c revision 261048
1/* $NetBSD: svc.c,v 1.21 2000/07/06 03:10:35 christos Exp $ */ 2 3/*- 4 * Copyright (c) 2009, Sun Microsystems, Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions are met: 9 * - Redistributions of source code must retain the above copyright notice, 10 * this list of conditions and the following disclaimer. 11 * - Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * - Neither the name of Sun Microsystems, Inc. nor the names of its 15 * contributors may be used to endorse or promote products derived 16 * from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31#if defined(LIBC_SCCS) && !defined(lint) 32static char *sccsid2 = "@(#)svc.c 1.44 88/02/08 Copyr 1984 Sun Micro"; 33static char *sccsid = "@(#)svc.c 2.4 88/08/11 4.0 RPCSRC"; 34#endif 35#include <sys/cdefs.h> 36__FBSDID("$FreeBSD: stable/10/sys/rpc/svc.c 261048 2014-01-22 23:47:29Z mav $"); 37 38/* 39 * svc.c, Server-side remote procedure call interface. 40 * 41 * There are two sets of procedures here. The xprt routines are 42 * for handling transport handles. The svc routines handle the 43 * list of service routines. 44 * 45 * Copyright (C) 1984, Sun Microsystems, Inc. 46 */ 47 48#include <sys/param.h> 49#include <sys/lock.h> 50#include <sys/kernel.h> 51#include <sys/kthread.h> 52#include <sys/malloc.h> 53#include <sys/mbuf.h> 54#include <sys/mutex.h> 55#include <sys/proc.h> 56#include <sys/queue.h> 57#include <sys/socketvar.h> 58#include <sys/systm.h> 59#include <sys/ucred.h> 60 61#include <rpc/rpc.h> 62#include <rpc/rpcb_clnt.h> 63#include <rpc/replay.h> 64 65#include <rpc/rpc_com.h> 66 67#define SVC_VERSQUIET 0x0001 /* keep quiet about vers mismatch */ 68#define version_keepquiet(xp) (SVC_EXT(xp)->xp_flags & SVC_VERSQUIET) 69 70static struct svc_callout *svc_find(SVCPOOL *pool, rpcprog_t, rpcvers_t, 71 char *); 72static void svc_new_thread(SVCPOOL *pool); 73static void xprt_unregister_locked(SVCXPRT *xprt); 74 75/* *************** SVCXPRT related stuff **************** */ 76 77static int svcpool_minthread_sysctl(SYSCTL_HANDLER_ARGS); 78static int svcpool_maxthread_sysctl(SYSCTL_HANDLER_ARGS); 79 80SVCPOOL* 81svcpool_create(const char *name, struct sysctl_oid_list *sysctl_base) 82{ 83 SVCPOOL *pool; 84 85 pool = malloc(sizeof(SVCPOOL), M_RPC, M_WAITOK|M_ZERO); 86 87 mtx_init(&pool->sp_lock, "sp_lock", NULL, MTX_DEF); 88 pool->sp_name = name; 89 pool->sp_state = SVCPOOL_INIT; 90 pool->sp_proc = NULL; 91 TAILQ_INIT(&pool->sp_xlist); 92 TAILQ_INIT(&pool->sp_active); 93 TAILQ_INIT(&pool->sp_callouts); 94 LIST_INIT(&pool->sp_threads); 95 LIST_INIT(&pool->sp_idlethreads); 96 pool->sp_minthreads = 1; 97 pool->sp_maxthreads = 1; 98 pool->sp_threadcount = 0; 99 100 /* 101 * Don't use more than a quarter of mbuf clusters or more than 102 * 45Mb buffering requests. 103 */ 104 pool->sp_space_high = nmbclusters * MCLBYTES / 4; 105 if (pool->sp_space_high > 45 << 20) 106 pool->sp_space_high = 45 << 20; 107 pool->sp_space_low = 2 * pool->sp_space_high / 3; 108 109 sysctl_ctx_init(&pool->sp_sysctl); 110 if (sysctl_base) { 111 SYSCTL_ADD_PROC(&pool->sp_sysctl, sysctl_base, OID_AUTO, 112 "minthreads", CTLTYPE_INT | CTLFLAG_RW, 113 pool, 0, svcpool_minthread_sysctl, "I", ""); 114 SYSCTL_ADD_PROC(&pool->sp_sysctl, sysctl_base, OID_AUTO, 115 "maxthreads", CTLTYPE_INT | CTLFLAG_RW, 116 pool, 0, svcpool_maxthread_sysctl, "I", ""); 117 SYSCTL_ADD_INT(&pool->sp_sysctl, sysctl_base, OID_AUTO, 118 "threads", CTLFLAG_RD, &pool->sp_threadcount, 0, ""); 119 120 SYSCTL_ADD_UINT(&pool->sp_sysctl, sysctl_base, OID_AUTO, 121 "request_space_used", CTLFLAG_RD, 122 &pool->sp_space_used, 0, 123 "Space in parsed but not handled requests."); 124 125 SYSCTL_ADD_UINT(&pool->sp_sysctl, sysctl_base, OID_AUTO, 126 "request_space_used_highest", CTLFLAG_RD, 127 &pool->sp_space_used_highest, 0, 128 "Highest space used since reboot."); 129 130 SYSCTL_ADD_UINT(&pool->sp_sysctl, sysctl_base, OID_AUTO, 131 "request_space_high", CTLFLAG_RW, 132 &pool->sp_space_high, 0, 133 "Maximum space in parsed but not handled requests."); 134 135 SYSCTL_ADD_UINT(&pool->sp_sysctl, sysctl_base, OID_AUTO, 136 "request_space_low", CTLFLAG_RW, 137 &pool->sp_space_low, 0, 138 "Low water mark for request space."); 139 140 SYSCTL_ADD_INT(&pool->sp_sysctl, sysctl_base, OID_AUTO, 141 "request_space_throttled", CTLFLAG_RD, 142 &pool->sp_space_throttled, 0, 143 "Whether nfs requests are currently throttled"); 144 145 SYSCTL_ADD_INT(&pool->sp_sysctl, sysctl_base, OID_AUTO, 146 "request_space_throttle_count", CTLFLAG_RD, 147 &pool->sp_space_throttle_count, 0, 148 "Count of times throttling based on request space has occurred"); 149 } 150 151 return pool; 152} 153 154void 155svcpool_destroy(SVCPOOL *pool) 156{ 157 SVCXPRT *xprt, *nxprt; 158 struct svc_callout *s; 159 struct svcxprt_list cleanup; 160 161 TAILQ_INIT(&cleanup); 162 mtx_lock(&pool->sp_lock); 163 164 while (TAILQ_FIRST(&pool->sp_xlist)) { 165 xprt = TAILQ_FIRST(&pool->sp_xlist); 166 xprt_unregister_locked(xprt); 167 TAILQ_INSERT_TAIL(&cleanup, xprt, xp_link); 168 } 169 170 while (TAILQ_FIRST(&pool->sp_callouts)) { 171 s = TAILQ_FIRST(&pool->sp_callouts); 172 mtx_unlock(&pool->sp_lock); 173 svc_unreg(pool, s->sc_prog, s->sc_vers); 174 mtx_lock(&pool->sp_lock); 175 } 176 mtx_unlock(&pool->sp_lock); 177 178 TAILQ_FOREACH_SAFE(xprt, &cleanup, xp_link, nxprt) { 179 SVC_RELEASE(xprt); 180 } 181 182 mtx_destroy(&pool->sp_lock); 183 184 if (pool->sp_rcache) 185 replay_freecache(pool->sp_rcache); 186 187 sysctl_ctx_free(&pool->sp_sysctl); 188 free(pool, M_RPC); 189} 190 191static bool_t 192svcpool_active(SVCPOOL *pool) 193{ 194 enum svcpool_state state = pool->sp_state; 195 196 if (state == SVCPOOL_INIT || state == SVCPOOL_CLOSING) 197 return (FALSE); 198 return (TRUE); 199} 200 201/* 202 * Sysctl handler to set the minimum thread count on a pool 203 */ 204static int 205svcpool_minthread_sysctl(SYSCTL_HANDLER_ARGS) 206{ 207 SVCPOOL *pool; 208 int newminthreads, error, n; 209 210 pool = oidp->oid_arg1; 211 newminthreads = pool->sp_minthreads; 212 error = sysctl_handle_int(oidp, &newminthreads, 0, req); 213 if (error == 0 && newminthreads != pool->sp_minthreads) { 214 if (newminthreads > pool->sp_maxthreads) 215 return (EINVAL); 216 mtx_lock(&pool->sp_lock); 217 if (newminthreads > pool->sp_minthreads 218 && svcpool_active(pool)) { 219 /* 220 * If the pool is running and we are 221 * increasing, create some more threads now. 222 */ 223 n = newminthreads - pool->sp_threadcount; 224 if (n > 0) { 225 mtx_unlock(&pool->sp_lock); 226 while (n--) 227 svc_new_thread(pool); 228 mtx_lock(&pool->sp_lock); 229 } 230 } 231 pool->sp_minthreads = newminthreads; 232 mtx_unlock(&pool->sp_lock); 233 } 234 return (error); 235} 236 237/* 238 * Sysctl handler to set the maximum thread count on a pool 239 */ 240static int 241svcpool_maxthread_sysctl(SYSCTL_HANDLER_ARGS) 242{ 243 SVCPOOL *pool; 244 SVCTHREAD *st; 245 int newmaxthreads, error; 246 247 pool = oidp->oid_arg1; 248 newmaxthreads = pool->sp_maxthreads; 249 error = sysctl_handle_int(oidp, &newmaxthreads, 0, req); 250 if (error == 0 && newmaxthreads != pool->sp_maxthreads) { 251 if (newmaxthreads < pool->sp_minthreads) 252 return (EINVAL); 253 mtx_lock(&pool->sp_lock); 254 if (newmaxthreads < pool->sp_maxthreads 255 && svcpool_active(pool)) { 256 /* 257 * If the pool is running and we are 258 * decreasing, wake up some idle threads to 259 * encourage them to exit. 260 */ 261 LIST_FOREACH(st, &pool->sp_idlethreads, st_ilink) 262 cv_signal(&st->st_cond); 263 } 264 pool->sp_maxthreads = newmaxthreads; 265 mtx_unlock(&pool->sp_lock); 266 } 267 return (error); 268} 269 270/* 271 * Activate a transport handle. 272 */ 273void 274xprt_register(SVCXPRT *xprt) 275{ 276 SVCPOOL *pool = xprt->xp_pool; 277 278 SVC_ACQUIRE(xprt); 279 mtx_lock(&pool->sp_lock); 280 xprt->xp_registered = TRUE; 281 xprt->xp_active = FALSE; 282 TAILQ_INSERT_TAIL(&pool->sp_xlist, xprt, xp_link); 283 mtx_unlock(&pool->sp_lock); 284} 285 286/* 287 * De-activate a transport handle. Note: the locked version doesn't 288 * release the transport - caller must do that after dropping the pool 289 * lock. 290 */ 291static void 292xprt_unregister_locked(SVCXPRT *xprt) 293{ 294 SVCPOOL *pool = xprt->xp_pool; 295 296 mtx_assert(&pool->sp_lock, MA_OWNED); 297 KASSERT(xprt->xp_registered == TRUE, 298 ("xprt_unregister_locked: not registered")); 299 xprt_inactive_locked(xprt); 300 TAILQ_REMOVE(&pool->sp_xlist, xprt, xp_link); 301 xprt->xp_registered = FALSE; 302} 303 304void 305xprt_unregister(SVCXPRT *xprt) 306{ 307 SVCPOOL *pool = xprt->xp_pool; 308 309 mtx_lock(&pool->sp_lock); 310 if (xprt->xp_registered == FALSE) { 311 /* Already unregistered by another thread */ 312 mtx_unlock(&pool->sp_lock); 313 return; 314 } 315 xprt_unregister_locked(xprt); 316 mtx_unlock(&pool->sp_lock); 317 318 SVC_RELEASE(xprt); 319} 320 321/* 322 * Attempt to assign a service thread to this transport. 323 */ 324static int 325xprt_assignthread(SVCXPRT *xprt) 326{ 327 SVCPOOL *pool = xprt->xp_pool; 328 SVCTHREAD *st; 329 330 mtx_assert(&pool->sp_lock, MA_OWNED); 331 st = LIST_FIRST(&pool->sp_idlethreads); 332 if (st) { 333 LIST_REMOVE(st, st_ilink); 334 st->st_idle = FALSE; 335 SVC_ACQUIRE(xprt); 336 xprt->xp_thread = st; 337 st->st_xprt = xprt; 338 cv_signal(&st->st_cond); 339 return (TRUE); 340 } else { 341 /* 342 * See if we can create a new thread. The 343 * actual thread creation happens in 344 * svc_run_internal because our locking state 345 * is poorly defined (we are typically called 346 * from a socket upcall). Don't create more 347 * than one thread per second. 348 */ 349 if (pool->sp_state == SVCPOOL_ACTIVE 350 && pool->sp_lastcreatetime < time_uptime 351 && pool->sp_threadcount < pool->sp_maxthreads) { 352 pool->sp_state = SVCPOOL_THREADWANTED; 353 } 354 } 355 return (FALSE); 356} 357 358void 359xprt_active(SVCXPRT *xprt) 360{ 361 SVCPOOL *pool = xprt->xp_pool; 362 363 mtx_lock(&pool->sp_lock); 364 365 if (!xprt->xp_registered) { 366 /* 367 * Race with xprt_unregister - we lose. 368 */ 369 mtx_unlock(&pool->sp_lock); 370 return; 371 } 372 373 if (!xprt->xp_active) { 374 xprt->xp_active = TRUE; 375 if (xprt->xp_thread == NULL) { 376 if (!xprt_assignthread(xprt)) 377 TAILQ_INSERT_TAIL(&pool->sp_active, xprt, 378 xp_alink); 379 } 380 } 381 382 mtx_unlock(&pool->sp_lock); 383} 384 385void 386xprt_inactive_locked(SVCXPRT *xprt) 387{ 388 SVCPOOL *pool = xprt->xp_pool; 389 390 mtx_assert(&pool->sp_lock, MA_OWNED); 391 if (xprt->xp_active) { 392 if (xprt->xp_thread == NULL) 393 TAILQ_REMOVE(&pool->sp_active, xprt, xp_alink); 394 xprt->xp_active = FALSE; 395 } 396} 397 398void 399xprt_inactive(SVCXPRT *xprt) 400{ 401 SVCPOOL *pool = xprt->xp_pool; 402 403 mtx_lock(&pool->sp_lock); 404 xprt_inactive_locked(xprt); 405 mtx_unlock(&pool->sp_lock); 406} 407 408/* 409 * Add a service program to the callout list. 410 * The dispatch routine will be called when a rpc request for this 411 * program number comes in. 412 */ 413bool_t 414svc_reg(SVCXPRT *xprt, const rpcprog_t prog, const rpcvers_t vers, 415 void (*dispatch)(struct svc_req *, SVCXPRT *), 416 const struct netconfig *nconf) 417{ 418 SVCPOOL *pool = xprt->xp_pool; 419 struct svc_callout *s; 420 char *netid = NULL; 421 int flag = 0; 422 423/* VARIABLES PROTECTED BY svc_lock: s, svc_head */ 424 425 if (xprt->xp_netid) { 426 netid = strdup(xprt->xp_netid, M_RPC); 427 flag = 1; 428 } else if (nconf && nconf->nc_netid) { 429 netid = strdup(nconf->nc_netid, M_RPC); 430 flag = 1; 431 } /* must have been created with svc_raw_create */ 432 if ((netid == NULL) && (flag == 1)) { 433 return (FALSE); 434 } 435 436 mtx_lock(&pool->sp_lock); 437 if ((s = svc_find(pool, prog, vers, netid)) != NULL) { 438 if (netid) 439 free(netid, M_RPC); 440 if (s->sc_dispatch == dispatch) 441 goto rpcb_it; /* he is registering another xptr */ 442 mtx_unlock(&pool->sp_lock); 443 return (FALSE); 444 } 445 s = malloc(sizeof (struct svc_callout), M_RPC, M_NOWAIT); 446 if (s == NULL) { 447 if (netid) 448 free(netid, M_RPC); 449 mtx_unlock(&pool->sp_lock); 450 return (FALSE); 451 } 452 453 s->sc_prog = prog; 454 s->sc_vers = vers; 455 s->sc_dispatch = dispatch; 456 s->sc_netid = netid; 457 TAILQ_INSERT_TAIL(&pool->sp_callouts, s, sc_link); 458 459 if ((xprt->xp_netid == NULL) && (flag == 1) && netid) 460 ((SVCXPRT *) xprt)->xp_netid = strdup(netid, M_RPC); 461 462rpcb_it: 463 mtx_unlock(&pool->sp_lock); 464 /* now register the information with the local binder service */ 465 if (nconf) { 466 bool_t dummy; 467 struct netconfig tnc; 468 struct netbuf nb; 469 tnc = *nconf; 470 nb.buf = &xprt->xp_ltaddr; 471 nb.len = xprt->xp_ltaddr.ss_len; 472 dummy = rpcb_set(prog, vers, &tnc, &nb); 473 return (dummy); 474 } 475 return (TRUE); 476} 477 478/* 479 * Remove a service program from the callout list. 480 */ 481void 482svc_unreg(SVCPOOL *pool, const rpcprog_t prog, const rpcvers_t vers) 483{ 484 struct svc_callout *s; 485 486 /* unregister the information anyway */ 487 (void) rpcb_unset(prog, vers, NULL); 488 mtx_lock(&pool->sp_lock); 489 while ((s = svc_find(pool, prog, vers, NULL)) != NULL) { 490 TAILQ_REMOVE(&pool->sp_callouts, s, sc_link); 491 if (s->sc_netid) 492 mem_free(s->sc_netid, sizeof (s->sc_netid) + 1); 493 mem_free(s, sizeof (struct svc_callout)); 494 } 495 mtx_unlock(&pool->sp_lock); 496} 497 498/* ********************** CALLOUT list related stuff ************* */ 499 500/* 501 * Search the callout list for a program number, return the callout 502 * struct. 503 */ 504static struct svc_callout * 505svc_find(SVCPOOL *pool, rpcprog_t prog, rpcvers_t vers, char *netid) 506{ 507 struct svc_callout *s; 508 509 mtx_assert(&pool->sp_lock, MA_OWNED); 510 TAILQ_FOREACH(s, &pool->sp_callouts, sc_link) { 511 if (s->sc_prog == prog && s->sc_vers == vers 512 && (netid == NULL || s->sc_netid == NULL || 513 strcmp(netid, s->sc_netid) == 0)) 514 break; 515 } 516 517 return (s); 518} 519 520/* ******************* REPLY GENERATION ROUTINES ************ */ 521 522static bool_t 523svc_sendreply_common(struct svc_req *rqstp, struct rpc_msg *rply, 524 struct mbuf *body) 525{ 526 SVCXPRT *xprt = rqstp->rq_xprt; 527 bool_t ok; 528 529 if (rqstp->rq_args) { 530 m_freem(rqstp->rq_args); 531 rqstp->rq_args = NULL; 532 } 533 534 if (xprt->xp_pool->sp_rcache) 535 replay_setreply(xprt->xp_pool->sp_rcache, 536 rply, svc_getrpccaller(rqstp), body); 537 538 if (!SVCAUTH_WRAP(&rqstp->rq_auth, &body)) 539 return (FALSE); 540 541 ok = SVC_REPLY(xprt, rply, rqstp->rq_addr, body); 542 if (rqstp->rq_addr) { 543 free(rqstp->rq_addr, M_SONAME); 544 rqstp->rq_addr = NULL; 545 } 546 547 return (ok); 548} 549 550/* 551 * Send a reply to an rpc request 552 */ 553bool_t 554svc_sendreply(struct svc_req *rqstp, xdrproc_t xdr_results, void * xdr_location) 555{ 556 struct rpc_msg rply; 557 struct mbuf *m; 558 XDR xdrs; 559 bool_t ok; 560 561 rply.rm_xid = rqstp->rq_xid; 562 rply.rm_direction = REPLY; 563 rply.rm_reply.rp_stat = MSG_ACCEPTED; 564 rply.acpted_rply.ar_verf = rqstp->rq_verf; 565 rply.acpted_rply.ar_stat = SUCCESS; 566 rply.acpted_rply.ar_results.where = NULL; 567 rply.acpted_rply.ar_results.proc = (xdrproc_t) xdr_void; 568 569 m = m_getcl(M_WAITOK, MT_DATA, 0); 570 xdrmbuf_create(&xdrs, m, XDR_ENCODE); 571 ok = xdr_results(&xdrs, xdr_location); 572 XDR_DESTROY(&xdrs); 573 574 if (ok) { 575 return (svc_sendreply_common(rqstp, &rply, m)); 576 } else { 577 m_freem(m); 578 return (FALSE); 579 } 580} 581 582bool_t 583svc_sendreply_mbuf(struct svc_req *rqstp, struct mbuf *m) 584{ 585 struct rpc_msg rply; 586 587 rply.rm_xid = rqstp->rq_xid; 588 rply.rm_direction = REPLY; 589 rply.rm_reply.rp_stat = MSG_ACCEPTED; 590 rply.acpted_rply.ar_verf = rqstp->rq_verf; 591 rply.acpted_rply.ar_stat = SUCCESS; 592 rply.acpted_rply.ar_results.where = NULL; 593 rply.acpted_rply.ar_results.proc = (xdrproc_t) xdr_void; 594 595 return (svc_sendreply_common(rqstp, &rply, m)); 596} 597 598/* 599 * No procedure error reply 600 */ 601void 602svcerr_noproc(struct svc_req *rqstp) 603{ 604 SVCXPRT *xprt = rqstp->rq_xprt; 605 struct rpc_msg rply; 606 607 rply.rm_xid = rqstp->rq_xid; 608 rply.rm_direction = REPLY; 609 rply.rm_reply.rp_stat = MSG_ACCEPTED; 610 rply.acpted_rply.ar_verf = rqstp->rq_verf; 611 rply.acpted_rply.ar_stat = PROC_UNAVAIL; 612 613 if (xprt->xp_pool->sp_rcache) 614 replay_setreply(xprt->xp_pool->sp_rcache, 615 &rply, svc_getrpccaller(rqstp), NULL); 616 617 svc_sendreply_common(rqstp, &rply, NULL); 618} 619 620/* 621 * Can't decode args error reply 622 */ 623void 624svcerr_decode(struct svc_req *rqstp) 625{ 626 SVCXPRT *xprt = rqstp->rq_xprt; 627 struct rpc_msg rply; 628 629 rply.rm_xid = rqstp->rq_xid; 630 rply.rm_direction = REPLY; 631 rply.rm_reply.rp_stat = MSG_ACCEPTED; 632 rply.acpted_rply.ar_verf = rqstp->rq_verf; 633 rply.acpted_rply.ar_stat = GARBAGE_ARGS; 634 635 if (xprt->xp_pool->sp_rcache) 636 replay_setreply(xprt->xp_pool->sp_rcache, 637 &rply, (struct sockaddr *) &xprt->xp_rtaddr, NULL); 638 639 svc_sendreply_common(rqstp, &rply, NULL); 640} 641 642/* 643 * Some system error 644 */ 645void 646svcerr_systemerr(struct svc_req *rqstp) 647{ 648 SVCXPRT *xprt = rqstp->rq_xprt; 649 struct rpc_msg rply; 650 651 rply.rm_xid = rqstp->rq_xid; 652 rply.rm_direction = REPLY; 653 rply.rm_reply.rp_stat = MSG_ACCEPTED; 654 rply.acpted_rply.ar_verf = rqstp->rq_verf; 655 rply.acpted_rply.ar_stat = SYSTEM_ERR; 656 657 if (xprt->xp_pool->sp_rcache) 658 replay_setreply(xprt->xp_pool->sp_rcache, 659 &rply, svc_getrpccaller(rqstp), NULL); 660 661 svc_sendreply_common(rqstp, &rply, NULL); 662} 663 664/* 665 * Authentication error reply 666 */ 667void 668svcerr_auth(struct svc_req *rqstp, enum auth_stat why) 669{ 670 SVCXPRT *xprt = rqstp->rq_xprt; 671 struct rpc_msg rply; 672 673 rply.rm_xid = rqstp->rq_xid; 674 rply.rm_direction = REPLY; 675 rply.rm_reply.rp_stat = MSG_DENIED; 676 rply.rjcted_rply.rj_stat = AUTH_ERROR; 677 rply.rjcted_rply.rj_why = why; 678 679 if (xprt->xp_pool->sp_rcache) 680 replay_setreply(xprt->xp_pool->sp_rcache, 681 &rply, svc_getrpccaller(rqstp), NULL); 682 683 svc_sendreply_common(rqstp, &rply, NULL); 684} 685 686/* 687 * Auth too weak error reply 688 */ 689void 690svcerr_weakauth(struct svc_req *rqstp) 691{ 692 693 svcerr_auth(rqstp, AUTH_TOOWEAK); 694} 695 696/* 697 * Program unavailable error reply 698 */ 699void 700svcerr_noprog(struct svc_req *rqstp) 701{ 702 SVCXPRT *xprt = rqstp->rq_xprt; 703 struct rpc_msg rply; 704 705 rply.rm_xid = rqstp->rq_xid; 706 rply.rm_direction = REPLY; 707 rply.rm_reply.rp_stat = MSG_ACCEPTED; 708 rply.acpted_rply.ar_verf = rqstp->rq_verf; 709 rply.acpted_rply.ar_stat = PROG_UNAVAIL; 710 711 if (xprt->xp_pool->sp_rcache) 712 replay_setreply(xprt->xp_pool->sp_rcache, 713 &rply, svc_getrpccaller(rqstp), NULL); 714 715 svc_sendreply_common(rqstp, &rply, NULL); 716} 717 718/* 719 * Program version mismatch error reply 720 */ 721void 722svcerr_progvers(struct svc_req *rqstp, rpcvers_t low_vers, rpcvers_t high_vers) 723{ 724 SVCXPRT *xprt = rqstp->rq_xprt; 725 struct rpc_msg rply; 726 727 rply.rm_xid = rqstp->rq_xid; 728 rply.rm_direction = REPLY; 729 rply.rm_reply.rp_stat = MSG_ACCEPTED; 730 rply.acpted_rply.ar_verf = rqstp->rq_verf; 731 rply.acpted_rply.ar_stat = PROG_MISMATCH; 732 rply.acpted_rply.ar_vers.low = (uint32_t)low_vers; 733 rply.acpted_rply.ar_vers.high = (uint32_t)high_vers; 734 735 if (xprt->xp_pool->sp_rcache) 736 replay_setreply(xprt->xp_pool->sp_rcache, 737 &rply, svc_getrpccaller(rqstp), NULL); 738 739 svc_sendreply_common(rqstp, &rply, NULL); 740} 741 742/* 743 * Allocate a new server transport structure. All fields are 744 * initialized to zero and xp_p3 is initialized to point at an 745 * extension structure to hold various flags and authentication 746 * parameters. 747 */ 748SVCXPRT * 749svc_xprt_alloc() 750{ 751 SVCXPRT *xprt; 752 SVCXPRT_EXT *ext; 753 754 xprt = mem_alloc(sizeof(SVCXPRT)); 755 memset(xprt, 0, sizeof(SVCXPRT)); 756 ext = mem_alloc(sizeof(SVCXPRT_EXT)); 757 memset(ext, 0, sizeof(SVCXPRT_EXT)); 758 xprt->xp_p3 = ext; 759 refcount_init(&xprt->xp_refs, 1); 760 761 return (xprt); 762} 763 764/* 765 * Free a server transport structure. 766 */ 767void 768svc_xprt_free(xprt) 769 SVCXPRT *xprt; 770{ 771 772 mem_free(xprt->xp_p3, sizeof(SVCXPRT_EXT)); 773 mem_free(xprt, sizeof(SVCXPRT)); 774} 775 776/* ******************* SERVER INPUT STUFF ******************* */ 777 778/* 779 * Read RPC requests from a transport and queue them to be 780 * executed. We handle authentication and replay cache replies here. 781 * Actually dispatching the RPC is deferred till svc_executereq. 782 */ 783static enum xprt_stat 784svc_getreq(SVCXPRT *xprt, struct svc_req **rqstp_ret) 785{ 786 SVCPOOL *pool = xprt->xp_pool; 787 struct svc_req *r; 788 struct rpc_msg msg; 789 struct mbuf *args; 790 enum xprt_stat stat; 791 792 /* now receive msgs from xprtprt (support batch calls) */ 793 r = malloc(sizeof(*r), M_RPC, M_WAITOK|M_ZERO); 794 795 msg.rm_call.cb_cred.oa_base = r->rq_credarea; 796 msg.rm_call.cb_verf.oa_base = &r->rq_credarea[MAX_AUTH_BYTES]; 797 r->rq_clntcred = &r->rq_credarea[2*MAX_AUTH_BYTES]; 798 if (SVC_RECV(xprt, &msg, &r->rq_addr, &args)) { 799 enum auth_stat why; 800 801 /* 802 * Handle replays and authenticate before queuing the 803 * request to be executed. 804 */ 805 SVC_ACQUIRE(xprt); 806 r->rq_xprt = xprt; 807 if (pool->sp_rcache) { 808 struct rpc_msg repmsg; 809 struct mbuf *repbody; 810 enum replay_state rs; 811 rs = replay_find(pool->sp_rcache, &msg, 812 svc_getrpccaller(r), &repmsg, &repbody); 813 switch (rs) { 814 case RS_NEW: 815 break; 816 case RS_DONE: 817 SVC_REPLY(xprt, &repmsg, r->rq_addr, 818 repbody); 819 if (r->rq_addr) { 820 free(r->rq_addr, M_SONAME); 821 r->rq_addr = NULL; 822 } 823 m_freem(args); 824 goto call_done; 825 826 default: 827 m_freem(args); 828 goto call_done; 829 } 830 } 831 832 r->rq_xid = msg.rm_xid; 833 r->rq_prog = msg.rm_call.cb_prog; 834 r->rq_vers = msg.rm_call.cb_vers; 835 r->rq_proc = msg.rm_call.cb_proc; 836 r->rq_size = sizeof(*r) + m_length(args, NULL); 837 r->rq_args = args; 838 if ((why = _authenticate(r, &msg)) != AUTH_OK) { 839 /* 840 * RPCSEC_GSS uses this return code 841 * for requests that form part of its 842 * context establishment protocol and 843 * should not be dispatched to the 844 * application. 845 */ 846 if (why != RPCSEC_GSS_NODISPATCH) 847 svcerr_auth(r, why); 848 goto call_done; 849 } 850 851 if (!SVCAUTH_UNWRAP(&r->rq_auth, &r->rq_args)) { 852 svcerr_decode(r); 853 goto call_done; 854 } 855 856 /* 857 * Everything checks out, return request to caller. 858 */ 859 *rqstp_ret = r; 860 r = NULL; 861 } 862call_done: 863 if (r) { 864 svc_freereq(r); 865 r = NULL; 866 } 867 if ((stat = SVC_STAT(xprt)) == XPRT_DIED) { 868 xprt_unregister(xprt); 869 } 870 871 return (stat); 872} 873 874static void 875svc_executereq(struct svc_req *rqstp) 876{ 877 SVCXPRT *xprt = rqstp->rq_xprt; 878 SVCPOOL *pool = xprt->xp_pool; 879 int prog_found; 880 rpcvers_t low_vers; 881 rpcvers_t high_vers; 882 struct svc_callout *s; 883 884 /* now match message with a registered service*/ 885 prog_found = FALSE; 886 low_vers = (rpcvers_t) -1L; 887 high_vers = (rpcvers_t) 0L; 888 TAILQ_FOREACH(s, &pool->sp_callouts, sc_link) { 889 if (s->sc_prog == rqstp->rq_prog) { 890 if (s->sc_vers == rqstp->rq_vers) { 891 /* 892 * We hand ownership of r to the 893 * dispatch method - they must call 894 * svc_freereq. 895 */ 896 (*s->sc_dispatch)(rqstp, xprt); 897 return; 898 } /* found correct version */ 899 prog_found = TRUE; 900 if (s->sc_vers < low_vers) 901 low_vers = s->sc_vers; 902 if (s->sc_vers > high_vers) 903 high_vers = s->sc_vers; 904 } /* found correct program */ 905 } 906 907 /* 908 * if we got here, the program or version 909 * is not served ... 910 */ 911 if (prog_found) 912 svcerr_progvers(rqstp, low_vers, high_vers); 913 else 914 svcerr_noprog(rqstp); 915 916 svc_freereq(rqstp); 917} 918 919static void 920svc_checkidle(SVCPOOL *pool) 921{ 922 SVCXPRT *xprt, *nxprt; 923 time_t timo; 924 struct svcxprt_list cleanup; 925 926 TAILQ_INIT(&cleanup); 927 TAILQ_FOREACH_SAFE(xprt, &pool->sp_xlist, xp_link, nxprt) { 928 /* 929 * Only some transports have idle timers. Don't time 930 * something out which is just waking up. 931 */ 932 if (!xprt->xp_idletimeout || xprt->xp_thread) 933 continue; 934 935 timo = xprt->xp_lastactive + xprt->xp_idletimeout; 936 if (time_uptime > timo) { 937 xprt_unregister_locked(xprt); 938 TAILQ_INSERT_TAIL(&cleanup, xprt, xp_link); 939 } 940 } 941 942 mtx_unlock(&pool->sp_lock); 943 TAILQ_FOREACH_SAFE(xprt, &cleanup, xp_link, nxprt) { 944 SVC_RELEASE(xprt); 945 } 946 mtx_lock(&pool->sp_lock); 947 948} 949 950static void 951svc_assign_waiting_sockets(SVCPOOL *pool) 952{ 953 SVCXPRT *xprt; 954 955 while ((xprt = TAILQ_FIRST(&pool->sp_active)) != NULL) { 956 if (xprt_assignthread(xprt)) 957 TAILQ_REMOVE(&pool->sp_active, xprt, xp_alink); 958 else 959 break; 960 } 961} 962 963static bool_t 964svc_request_space_available(SVCPOOL *pool) 965{ 966 967 mtx_assert(&pool->sp_lock, MA_OWNED); 968 969 if (pool->sp_space_throttled) { 970 /* 971 * Below the low-water yet? If so, assign any waiting sockets. 972 */ 973 if (pool->sp_space_used < pool->sp_space_low) { 974 pool->sp_space_throttled = FALSE; 975 svc_assign_waiting_sockets(pool); 976 return TRUE; 977 } 978 979 return FALSE; 980 } else { 981 if (pool->sp_space_used 982 >= pool->sp_space_high) { 983 pool->sp_space_throttled = TRUE; 984 pool->sp_space_throttle_count++; 985 return FALSE; 986 } 987 988 return TRUE; 989 } 990} 991 992static void 993svc_run_internal(SVCPOOL *pool, bool_t ismaster) 994{ 995 SVCTHREAD *st, *stpref; 996 SVCXPRT *xprt; 997 enum xprt_stat stat; 998 struct svc_req *rqstp; 999 int error; 1000 1001 st = mem_alloc(sizeof(*st)); 1002 st->st_xprt = NULL; 1003 STAILQ_INIT(&st->st_reqs); 1004 cv_init(&st->st_cond, "rpcsvc"); 1005 1006 mtx_lock(&pool->sp_lock); 1007 LIST_INSERT_HEAD(&pool->sp_threads, st, st_link); 1008 1009 /* 1010 * If we are a new thread which was spawned to cope with 1011 * increased load, set the state back to SVCPOOL_ACTIVE. 1012 */ 1013 if (pool->sp_state == SVCPOOL_THREADSTARTING) 1014 pool->sp_state = SVCPOOL_ACTIVE; 1015 1016 while (pool->sp_state != SVCPOOL_CLOSING) { 1017 /* 1018 * Create new thread if requested. 1019 */ 1020 if (pool->sp_state == SVCPOOL_THREADWANTED) { 1021 pool->sp_state = SVCPOOL_THREADSTARTING; 1022 pool->sp_lastcreatetime = time_uptime; 1023 mtx_unlock(&pool->sp_lock); 1024 svc_new_thread(pool); 1025 mtx_lock(&pool->sp_lock); 1026 continue; 1027 } 1028 1029 /* 1030 * Check for idle transports once per second. 1031 */ 1032 if (time_uptime > pool->sp_lastidlecheck) { 1033 pool->sp_lastidlecheck = time_uptime; 1034 svc_checkidle(pool); 1035 } 1036 1037 xprt = st->st_xprt; 1038 if (!xprt && STAILQ_EMPTY(&st->st_reqs)) { 1039 /* 1040 * Enforce maxthreads count. 1041 */ 1042 if (pool->sp_threadcount > pool->sp_maxthreads) 1043 break; 1044 1045 /* 1046 * Before sleeping, see if we can find an 1047 * active transport which isn't being serviced 1048 * by a thread. 1049 */ 1050 if (svc_request_space_available(pool) && 1051 (xprt = TAILQ_FIRST(&pool->sp_active)) != NULL) { 1052 TAILQ_REMOVE(&pool->sp_active, xprt, xp_alink); 1053 SVC_ACQUIRE(xprt); 1054 xprt->xp_thread = st; 1055 st->st_xprt = xprt; 1056 continue; 1057 } 1058 1059 LIST_INSERT_HEAD(&pool->sp_idlethreads, st, st_ilink); 1060 st->st_idle = TRUE; 1061 if (ismaster || (!ismaster && 1062 pool->sp_threadcount > pool->sp_minthreads)) 1063 error = cv_timedwait_sig(&st->st_cond, 1064 &pool->sp_lock, 5 * hz); 1065 else 1066 error = cv_wait_sig(&st->st_cond, 1067 &pool->sp_lock); 1068 if (st->st_idle) { 1069 LIST_REMOVE(st, st_ilink); 1070 st->st_idle = FALSE; 1071 } 1072 1073 /* 1074 * Reduce worker thread count when idle. 1075 */ 1076 if (error == EWOULDBLOCK) { 1077 if (!ismaster 1078 && (pool->sp_threadcount 1079 > pool->sp_minthreads) 1080 && !st->st_xprt 1081 && STAILQ_EMPTY(&st->st_reqs)) 1082 break; 1083 } else if (error) { 1084 mtx_unlock(&pool->sp_lock); 1085 svc_exit(pool); 1086 mtx_lock(&pool->sp_lock); 1087 break; 1088 } 1089 continue; 1090 } 1091 1092 if (xprt) { 1093 /* 1094 * Drain the transport socket and queue up any 1095 * RPCs. 1096 */ 1097 xprt->xp_lastactive = time_uptime; 1098 stat = XPRT_IDLE; 1099 do { 1100 if (!svc_request_space_available(pool)) 1101 break; 1102 rqstp = NULL; 1103 mtx_unlock(&pool->sp_lock); 1104 stat = svc_getreq(xprt, &rqstp); 1105 mtx_lock(&pool->sp_lock); 1106 if (rqstp) { 1107 /* 1108 * See if the application has 1109 * a preference for some other 1110 * thread. 1111 */ 1112 stpref = st; 1113 if (pool->sp_assign) 1114 stpref = pool->sp_assign(st, 1115 rqstp); 1116 1117 pool->sp_space_used += 1118 rqstp->rq_size; 1119 if (pool->sp_space_used 1120 > pool->sp_space_used_highest) 1121 pool->sp_space_used_highest = 1122 pool->sp_space_used; 1123 rqstp->rq_thread = stpref; 1124 STAILQ_INSERT_TAIL(&stpref->st_reqs, 1125 rqstp, rq_link); 1126 stpref->st_reqcount++; 1127 1128 /* 1129 * If we assigned the request 1130 * to another thread, make 1131 * sure its awake and continue 1132 * reading from the 1133 * socket. Otherwise, try to 1134 * find some other thread to 1135 * read from the socket and 1136 * execute the request 1137 * immediately. 1138 */ 1139 if (stpref == st) 1140 break; 1141 if (stpref->st_idle) { 1142 LIST_REMOVE(stpref, st_ilink); 1143 stpref->st_idle = FALSE; 1144 cv_signal(&stpref->st_cond); 1145 } 1146 } 1147 } while (stat == XPRT_MOREREQS 1148 && pool->sp_state != SVCPOOL_CLOSING); 1149 1150 /* 1151 * Move this transport to the end of the 1152 * active list to ensure fairness when 1153 * multiple transports are active. If this was 1154 * the last queued request, svc_getreq will 1155 * end up calling xprt_inactive to remove from 1156 * the active list. 1157 */ 1158 xprt->xp_thread = NULL; 1159 st->st_xprt = NULL; 1160 if (xprt->xp_active) { 1161 if (!xprt_assignthread(xprt)) 1162 TAILQ_INSERT_TAIL(&pool->sp_active, 1163 xprt, xp_alink); 1164 } 1165 mtx_unlock(&pool->sp_lock); 1166 SVC_RELEASE(xprt); 1167 mtx_lock(&pool->sp_lock); 1168 } 1169 1170 /* 1171 * Execute what we have queued. 1172 */ 1173 while ((rqstp = STAILQ_FIRST(&st->st_reqs)) != NULL) { 1174 size_t sz = rqstp->rq_size; 1175 mtx_unlock(&pool->sp_lock); 1176 svc_executereq(rqstp); 1177 mtx_lock(&pool->sp_lock); 1178 pool->sp_space_used -= sz; 1179 } 1180 } 1181 1182 if (st->st_xprt) { 1183 xprt = st->st_xprt; 1184 st->st_xprt = NULL; 1185 SVC_RELEASE(xprt); 1186 } 1187 1188 KASSERT(STAILQ_EMPTY(&st->st_reqs), ("stray reqs on exit")); 1189 LIST_REMOVE(st, st_link); 1190 pool->sp_threadcount--; 1191 1192 mtx_unlock(&pool->sp_lock); 1193 1194 cv_destroy(&st->st_cond); 1195 mem_free(st, sizeof(*st)); 1196 1197 if (!ismaster) 1198 wakeup(pool); 1199} 1200 1201static void 1202svc_thread_start(void *arg) 1203{ 1204 1205 svc_run_internal((SVCPOOL *) arg, FALSE); 1206 kthread_exit(); 1207} 1208 1209static void 1210svc_new_thread(SVCPOOL *pool) 1211{ 1212 struct thread *td; 1213 1214 pool->sp_threadcount++; 1215 kthread_add(svc_thread_start, pool, 1216 pool->sp_proc, &td, 0, 0, 1217 "%s: service", pool->sp_name); 1218} 1219 1220void 1221svc_run(SVCPOOL *pool) 1222{ 1223 int i; 1224 struct proc *p; 1225 struct thread *td; 1226 1227 p = curproc; 1228 td = curthread; 1229 snprintf(td->td_name, sizeof(td->td_name), 1230 "%s: master", pool->sp_name); 1231 pool->sp_state = SVCPOOL_ACTIVE; 1232 pool->sp_proc = p; 1233 pool->sp_lastcreatetime = time_uptime; 1234 pool->sp_threadcount = 1; 1235 1236 for (i = 1; i < pool->sp_minthreads; i++) { 1237 svc_new_thread(pool); 1238 } 1239 1240 svc_run_internal(pool, TRUE); 1241 1242 mtx_lock(&pool->sp_lock); 1243 while (pool->sp_threadcount > 0) 1244 msleep(pool, &pool->sp_lock, 0, "svcexit", 0); 1245 mtx_unlock(&pool->sp_lock); 1246} 1247 1248void 1249svc_exit(SVCPOOL *pool) 1250{ 1251 SVCTHREAD *st; 1252 1253 mtx_lock(&pool->sp_lock); 1254 1255 if (pool->sp_state != SVCPOOL_CLOSING) { 1256 pool->sp_state = SVCPOOL_CLOSING; 1257 LIST_FOREACH(st, &pool->sp_idlethreads, st_ilink) 1258 cv_signal(&st->st_cond); 1259 } 1260 1261 mtx_unlock(&pool->sp_lock); 1262} 1263 1264bool_t 1265svc_getargs(struct svc_req *rqstp, xdrproc_t xargs, void *args) 1266{ 1267 struct mbuf *m; 1268 XDR xdrs; 1269 bool_t stat; 1270 1271 m = rqstp->rq_args; 1272 rqstp->rq_args = NULL; 1273 1274 xdrmbuf_create(&xdrs, m, XDR_DECODE); 1275 stat = xargs(&xdrs, args); 1276 XDR_DESTROY(&xdrs); 1277 1278 return (stat); 1279} 1280 1281bool_t 1282svc_freeargs(struct svc_req *rqstp, xdrproc_t xargs, void *args) 1283{ 1284 XDR xdrs; 1285 1286 if (rqstp->rq_addr) { 1287 free(rqstp->rq_addr, M_SONAME); 1288 rqstp->rq_addr = NULL; 1289 } 1290 1291 xdrs.x_op = XDR_FREE; 1292 return (xargs(&xdrs, args)); 1293} 1294 1295void 1296svc_freereq(struct svc_req *rqstp) 1297{ 1298 SVCTHREAD *st; 1299 SVCXPRT *xprt; 1300 SVCPOOL *pool; 1301 1302 st = rqstp->rq_thread; 1303 xprt = rqstp->rq_xprt; 1304 if (xprt) 1305 pool = xprt->xp_pool; 1306 else 1307 pool = NULL; 1308 if (st) { 1309 mtx_lock(&pool->sp_lock); 1310 KASSERT(rqstp == STAILQ_FIRST(&st->st_reqs), 1311 ("Freeing request out of order")); 1312 STAILQ_REMOVE_HEAD(&st->st_reqs, rq_link); 1313 st->st_reqcount--; 1314 if (pool->sp_done) 1315 pool->sp_done(st, rqstp); 1316 mtx_unlock(&pool->sp_lock); 1317 } 1318 1319 if (rqstp->rq_auth.svc_ah_ops) 1320 SVCAUTH_RELEASE(&rqstp->rq_auth); 1321 1322 if (rqstp->rq_xprt) { 1323 SVC_RELEASE(rqstp->rq_xprt); 1324 } 1325 1326 if (rqstp->rq_addr) 1327 free(rqstp->rq_addr, M_SONAME); 1328 1329 if (rqstp->rq_args) 1330 m_freem(rqstp->rq_args); 1331 1332 free(rqstp, M_RPC); 1333} 1334