1 /* $NetBSD: svc.c,v 1.21 2000/07/06 03:10:35 christos Exp $ */
4 * SPDX-License-Identifier: BSD-3-Clause
6 * Copyright (c) 2009, Sun Microsystems, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 * - Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following disclaimer in the documentation
15 * and/or other materials provided with the distribution.
16 * - Neither the name of Sun Microsystems, Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
33 #if defined(LIBC_SCCS) && !defined(lint)
34 static char *sccsid2 = "@(#)svc.c 1.44 88/02/08 Copyr 1984 Sun Micro";
35 static char *sccsid = "@(#)svc.c 2.4 88/08/11 4.0 RPCSRC";
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
41 * svc.c, Server-side remote procedure call interface.
43 * There are two sets of procedures here. The xprt routines are
44 * for handling transport handles. The svc routines handle the
45 * list of service routines.
47 * Copyright (C) 1984, Sun Microsystems, Inc.
50 #include <sys/param.h>
52 #include <sys/kernel.h>
53 #include <sys/kthread.h>
54 #include <sys/malloc.h>
56 #include <sys/mutex.h>
58 #include <sys/queue.h>
59 #include <sys/socketvar.h>
60 #include <sys/systm.h>
63 #include <sys/ucred.h>
66 #include <rpc/rpcb_clnt.h>
67 #include <rpc/replay.h>
69 #include <rpc/rpc_com.h>
71 #define SVC_VERSQUIET 0x0001 /* keep quiet about vers mismatch */
72 #define version_keepquiet(xp) (SVC_EXT(xp)->xp_flags & SVC_VERSQUIET)
74 static struct svc_callout *svc_find(SVCPOOL *pool, rpcprog_t, rpcvers_t,
76 static void svc_new_thread(SVCGROUP *grp);
77 static void xprt_unregister_locked(SVCXPRT *xprt);
78 static void svc_change_space_used(SVCPOOL *pool, long delta);
79 static bool_t svc_request_space_available(SVCPOOL *pool);
80 static void svcpool_cleanup(SVCPOOL *pool);
82 /* *************** SVCXPRT related stuff **************** */
84 static int svcpool_minthread_sysctl(SYSCTL_HANDLER_ARGS);
85 static int svcpool_maxthread_sysctl(SYSCTL_HANDLER_ARGS);
86 static int svcpool_threads_sysctl(SYSCTL_HANDLER_ARGS);
89 svcpool_create(const char *name, struct sysctl_oid_list *sysctl_base)
95 pool = malloc(sizeof(SVCPOOL), M_RPC, M_WAITOK|M_ZERO);
97 mtx_init(&pool->sp_lock, "sp_lock", NULL, MTX_DEF);
99 pool->sp_state = SVCPOOL_INIT;
100 pool->sp_proc = NULL;
101 TAILQ_INIT(&pool->sp_callouts);
102 TAILQ_INIT(&pool->sp_lcallouts);
103 pool->sp_minthreads = 1;
104 pool->sp_maxthreads = 1;
105 pool->sp_groupcount = 1;
106 for (g = 0; g < SVC_MAXGROUPS; g++) {
107 grp = &pool->sp_groups[g];
108 mtx_init(&grp->sg_lock, "sg_lock", NULL, MTX_DEF);
110 grp->sg_state = SVCPOOL_ACTIVE;
111 TAILQ_INIT(&grp->sg_xlist);
112 TAILQ_INIT(&grp->sg_active);
113 LIST_INIT(&grp->sg_idlethreads);
114 grp->sg_minthreads = 1;
115 grp->sg_maxthreads = 1;
119 * Don't use more than a quarter of mbuf clusters. Nota bene:
120 * nmbclusters is an int, but nmbclusters*MCLBYTES may overflow
121 * on LP64 architectures, so cast to u_long to avoid undefined
122 * behavior. (ILP32 architectures cannot have nmbclusters
123 * large enough to overflow for other reasons.)
125 pool->sp_space_high = (u_long)nmbclusters * MCLBYTES / 4;
126 pool->sp_space_low = (pool->sp_space_high / 3) * 2;
128 sysctl_ctx_init(&pool->sp_sysctl);
130 SYSCTL_ADD_PROC(&pool->sp_sysctl, sysctl_base, OID_AUTO,
131 "minthreads", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
132 pool, 0, svcpool_minthread_sysctl, "I",
133 "Minimal number of threads");
134 SYSCTL_ADD_PROC(&pool->sp_sysctl, sysctl_base, OID_AUTO,
135 "maxthreads", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
136 pool, 0, svcpool_maxthread_sysctl, "I",
137 "Maximal number of threads");
138 SYSCTL_ADD_PROC(&pool->sp_sysctl, sysctl_base, OID_AUTO,
139 "threads", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
140 pool, 0, svcpool_threads_sysctl, "I",
141 "Current number of threads");
142 SYSCTL_ADD_INT(&pool->sp_sysctl, sysctl_base, OID_AUTO,
143 "groups", CTLFLAG_RD, &pool->sp_groupcount, 0,
144 "Number of thread groups");
146 SYSCTL_ADD_ULONG(&pool->sp_sysctl, sysctl_base, OID_AUTO,
147 "request_space_used", CTLFLAG_RD,
148 &pool->sp_space_used,
149 "Space in parsed but not handled requests.");
151 SYSCTL_ADD_ULONG(&pool->sp_sysctl, sysctl_base, OID_AUTO,
152 "request_space_used_highest", CTLFLAG_RD,
153 &pool->sp_space_used_highest,
154 "Highest space used since reboot.");
156 SYSCTL_ADD_ULONG(&pool->sp_sysctl, sysctl_base, OID_AUTO,
157 "request_space_high", CTLFLAG_RW,
158 &pool->sp_space_high,
159 "Maximum space in parsed but not handled requests.");
161 SYSCTL_ADD_ULONG(&pool->sp_sysctl, sysctl_base, OID_AUTO,
162 "request_space_low", CTLFLAG_RW,
164 "Low water mark for request space.");
166 SYSCTL_ADD_INT(&pool->sp_sysctl, sysctl_base, OID_AUTO,
167 "request_space_throttled", CTLFLAG_RD,
168 &pool->sp_space_throttled, 0,
169 "Whether nfs requests are currently throttled");
171 SYSCTL_ADD_INT(&pool->sp_sysctl, sysctl_base, OID_AUTO,
172 "request_space_throttle_count", CTLFLAG_RD,
173 &pool->sp_space_throttle_count, 0,
174 "Count of times throttling based on request space has occurred");
181 * Code common to svcpool_destroy() and svcpool_close(), which cleans up
182 * the pool data structures.
185 svcpool_cleanup(SVCPOOL *pool)
188 SVCXPRT *xprt, *nxprt;
189 struct svc_callout *s;
190 struct svc_loss_callout *sl;
191 struct svcxprt_list cleanup;
194 TAILQ_INIT(&cleanup);
196 for (g = 0; g < SVC_MAXGROUPS; g++) {
197 grp = &pool->sp_groups[g];
198 mtx_lock(&grp->sg_lock);
199 while ((xprt = TAILQ_FIRST(&grp->sg_xlist)) != NULL) {
200 xprt_unregister_locked(xprt);
201 TAILQ_INSERT_TAIL(&cleanup, xprt, xp_link);
203 mtx_unlock(&grp->sg_lock);
205 TAILQ_FOREACH_SAFE(xprt, &cleanup, xp_link, nxprt) {
206 if (xprt->xp_socket != NULL)
207 soshutdown(xprt->xp_socket, SHUT_WR);
211 mtx_lock(&pool->sp_lock);
212 while ((s = TAILQ_FIRST(&pool->sp_callouts)) != NULL) {
213 mtx_unlock(&pool->sp_lock);
214 svc_unreg(pool, s->sc_prog, s->sc_vers);
215 mtx_lock(&pool->sp_lock);
217 while ((sl = TAILQ_FIRST(&pool->sp_lcallouts)) != NULL) {
218 mtx_unlock(&pool->sp_lock);
219 svc_loss_unreg(pool, sl->slc_dispatch);
220 mtx_lock(&pool->sp_lock);
222 mtx_unlock(&pool->sp_lock);
226 svcpool_destroy(SVCPOOL *pool)
231 svcpool_cleanup(pool);
233 for (g = 0; g < SVC_MAXGROUPS; g++) {
234 grp = &pool->sp_groups[g];
235 mtx_destroy(&grp->sg_lock);
237 mtx_destroy(&pool->sp_lock);
240 replay_freecache(pool->sp_rcache);
242 sysctl_ctx_free(&pool->sp_sysctl);
247 * Similar to svcpool_destroy(), except that it does not destroy the actual
248 * data structures. As such, "pool" may be used again.
251 svcpool_close(SVCPOOL *pool)
256 svcpool_cleanup(pool);
258 /* Now, initialize the pool's state for a fresh svc_run() call. */
259 mtx_lock(&pool->sp_lock);
260 pool->sp_state = SVCPOOL_INIT;
261 mtx_unlock(&pool->sp_lock);
262 for (g = 0; g < SVC_MAXGROUPS; g++) {
263 grp = &pool->sp_groups[g];
264 mtx_lock(&grp->sg_lock);
265 grp->sg_state = SVCPOOL_ACTIVE;
266 mtx_unlock(&grp->sg_lock);
271 * Sysctl handler to get the present thread count on a pool
274 svcpool_threads_sysctl(SYSCTL_HANDLER_ARGS)
277 int threads, error, g;
279 pool = oidp->oid_arg1;
281 mtx_lock(&pool->sp_lock);
282 for (g = 0; g < pool->sp_groupcount; g++)
283 threads += pool->sp_groups[g].sg_threadcount;
284 mtx_unlock(&pool->sp_lock);
285 error = sysctl_handle_int(oidp, &threads, 0, req);
290 * Sysctl handler to set the minimum thread count on a pool
293 svcpool_minthread_sysctl(SYSCTL_HANDLER_ARGS)
296 int newminthreads, error, g;
298 pool = oidp->oid_arg1;
299 newminthreads = pool->sp_minthreads;
300 error = sysctl_handle_int(oidp, &newminthreads, 0, req);
301 if (error == 0 && newminthreads != pool->sp_minthreads) {
302 if (newminthreads > pool->sp_maxthreads)
304 mtx_lock(&pool->sp_lock);
305 pool->sp_minthreads = newminthreads;
306 for (g = 0; g < pool->sp_groupcount; g++) {
307 pool->sp_groups[g].sg_minthreads = max(1,
308 pool->sp_minthreads / pool->sp_groupcount);
310 mtx_unlock(&pool->sp_lock);
316 * Sysctl handler to set the maximum thread count on a pool
319 svcpool_maxthread_sysctl(SYSCTL_HANDLER_ARGS)
322 int newmaxthreads, error, g;
324 pool = oidp->oid_arg1;
325 newmaxthreads = pool->sp_maxthreads;
326 error = sysctl_handle_int(oidp, &newmaxthreads, 0, req);
327 if (error == 0 && newmaxthreads != pool->sp_maxthreads) {
328 if (newmaxthreads < pool->sp_minthreads)
330 mtx_lock(&pool->sp_lock);
331 pool->sp_maxthreads = newmaxthreads;
332 for (g = 0; g < pool->sp_groupcount; g++) {
333 pool->sp_groups[g].sg_maxthreads = max(1,
334 pool->sp_maxthreads / pool->sp_groupcount);
336 mtx_unlock(&pool->sp_lock);
342 * Activate a transport handle.
345 xprt_register(SVCXPRT *xprt)
347 SVCPOOL *pool = xprt->xp_pool;
352 g = atomic_fetchadd_int(&pool->sp_nextgroup, 1) % pool->sp_groupcount;
353 xprt->xp_group = grp = &pool->sp_groups[g];
354 mtx_lock(&grp->sg_lock);
355 xprt->xp_registered = TRUE;
356 xprt->xp_active = FALSE;
357 TAILQ_INSERT_TAIL(&grp->sg_xlist, xprt, xp_link);
358 mtx_unlock(&grp->sg_lock);
362 * De-activate a transport handle. Note: the locked version doesn't
363 * release the transport - caller must do that after dropping the pool
367 xprt_unregister_locked(SVCXPRT *xprt)
369 SVCGROUP *grp = xprt->xp_group;
371 mtx_assert(&grp->sg_lock, MA_OWNED);
372 KASSERT(xprt->xp_registered == TRUE,
373 ("xprt_unregister_locked: not registered"));
374 xprt_inactive_locked(xprt);
375 TAILQ_REMOVE(&grp->sg_xlist, xprt, xp_link);
376 xprt->xp_registered = FALSE;
380 xprt_unregister(SVCXPRT *xprt)
382 SVCGROUP *grp = xprt->xp_group;
384 mtx_lock(&grp->sg_lock);
385 if (xprt->xp_registered == FALSE) {
386 /* Already unregistered by another thread */
387 mtx_unlock(&grp->sg_lock);
390 xprt_unregister_locked(xprt);
391 mtx_unlock(&grp->sg_lock);
393 if (xprt->xp_socket != NULL)
394 soshutdown(xprt->xp_socket, SHUT_WR);
399 * Attempt to assign a service thread to this transport.
402 xprt_assignthread(SVCXPRT *xprt)
404 SVCGROUP *grp = xprt->xp_group;
407 mtx_assert(&grp->sg_lock, MA_OWNED);
408 st = LIST_FIRST(&grp->sg_idlethreads);
410 LIST_REMOVE(st, st_ilink);
412 xprt->xp_thread = st;
414 cv_signal(&st->st_cond);
418 * See if we can create a new thread. The
419 * actual thread creation happens in
420 * svc_run_internal because our locking state
421 * is poorly defined (we are typically called
422 * from a socket upcall). Don't create more
423 * than one thread per second.
425 if (grp->sg_state == SVCPOOL_ACTIVE
426 && grp->sg_lastcreatetime < time_uptime
427 && grp->sg_threadcount < grp->sg_maxthreads) {
428 grp->sg_state = SVCPOOL_THREADWANTED;
435 xprt_active(SVCXPRT *xprt)
437 SVCGROUP *grp = xprt->xp_group;
439 mtx_lock(&grp->sg_lock);
441 if (!xprt->xp_registered) {
443 * Race with xprt_unregister - we lose.
445 mtx_unlock(&grp->sg_lock);
449 if (!xprt->xp_active) {
450 xprt->xp_active = TRUE;
451 if (xprt->xp_thread == NULL) {
452 if (!svc_request_space_available(xprt->xp_pool) ||
453 !xprt_assignthread(xprt))
454 TAILQ_INSERT_TAIL(&grp->sg_active, xprt,
459 mtx_unlock(&grp->sg_lock);
463 xprt_inactive_locked(SVCXPRT *xprt)
465 SVCGROUP *grp = xprt->xp_group;
467 mtx_assert(&grp->sg_lock, MA_OWNED);
468 if (xprt->xp_active) {
469 if (xprt->xp_thread == NULL)
470 TAILQ_REMOVE(&grp->sg_active, xprt, xp_alink);
471 xprt->xp_active = FALSE;
476 xprt_inactive(SVCXPRT *xprt)
478 SVCGROUP *grp = xprt->xp_group;
480 mtx_lock(&grp->sg_lock);
481 xprt_inactive_locked(xprt);
482 mtx_unlock(&grp->sg_lock);
486 * Variant of xprt_inactive() for use only when sure that port is
487 * assigned to thread. For example, within receive handlers.
490 xprt_inactive_self(SVCXPRT *xprt)
493 KASSERT(xprt->xp_thread != NULL,
494 ("xprt_inactive_self(%p) with NULL xp_thread", xprt));
495 xprt->xp_active = FALSE;
499 * Add a service program to the callout list.
500 * The dispatch routine will be called when a rpc request for this
501 * program number comes in.
504 svc_reg(SVCXPRT *xprt, const rpcprog_t prog, const rpcvers_t vers,
505 void (*dispatch)(struct svc_req *, SVCXPRT *),
506 const struct netconfig *nconf)
508 SVCPOOL *pool = xprt->xp_pool;
509 struct svc_callout *s;
513 /* VARIABLES PROTECTED BY svc_lock: s, svc_head */
515 if (xprt->xp_netid) {
516 netid = strdup(xprt->xp_netid, M_RPC);
518 } else if (nconf && nconf->nc_netid) {
519 netid = strdup(nconf->nc_netid, M_RPC);
521 } /* must have been created with svc_raw_create */
522 if ((netid == NULL) && (flag == 1)) {
526 mtx_lock(&pool->sp_lock);
527 if ((s = svc_find(pool, prog, vers, netid)) != NULL) {
530 if (s->sc_dispatch == dispatch)
531 goto rpcb_it; /* he is registering another xptr */
532 mtx_unlock(&pool->sp_lock);
535 s = malloc(sizeof (struct svc_callout), M_RPC, M_NOWAIT);
539 mtx_unlock(&pool->sp_lock);
545 s->sc_dispatch = dispatch;
547 TAILQ_INSERT_TAIL(&pool->sp_callouts, s, sc_link);
549 if ((xprt->xp_netid == NULL) && (flag == 1) && netid)
550 ((SVCXPRT *) xprt)->xp_netid = strdup(netid, M_RPC);
553 mtx_unlock(&pool->sp_lock);
554 /* now register the information with the local binder service */
557 struct netconfig tnc;
560 nb.buf = &xprt->xp_ltaddr;
561 nb.len = xprt->xp_ltaddr.ss_len;
562 dummy = rpcb_set(prog, vers, &tnc, &nb);
569 * Remove a service program from the callout list.
572 svc_unreg(SVCPOOL *pool, const rpcprog_t prog, const rpcvers_t vers)
574 struct svc_callout *s;
576 /* unregister the information anyway */
577 (void) rpcb_unset(prog, vers, NULL);
578 mtx_lock(&pool->sp_lock);
579 while ((s = svc_find(pool, prog, vers, NULL)) != NULL) {
580 TAILQ_REMOVE(&pool->sp_callouts, s, sc_link);
582 mem_free(s->sc_netid, sizeof (s->sc_netid) + 1);
583 mem_free(s, sizeof (struct svc_callout));
585 mtx_unlock(&pool->sp_lock);
589 * Add a service connection loss program to the callout list.
590 * The dispatch routine will be called when some port in ths pool die.
593 svc_loss_reg(SVCXPRT *xprt, void (*dispatch)(SVCXPRT *))
595 SVCPOOL *pool = xprt->xp_pool;
596 struct svc_loss_callout *s;
598 mtx_lock(&pool->sp_lock);
599 TAILQ_FOREACH(s, &pool->sp_lcallouts, slc_link) {
600 if (s->slc_dispatch == dispatch)
604 mtx_unlock(&pool->sp_lock);
607 s = malloc(sizeof(struct svc_loss_callout), M_RPC, M_NOWAIT);
609 mtx_unlock(&pool->sp_lock);
612 s->slc_dispatch = dispatch;
613 TAILQ_INSERT_TAIL(&pool->sp_lcallouts, s, slc_link);
614 mtx_unlock(&pool->sp_lock);
619 * Remove a service connection loss program from the callout list.
622 svc_loss_unreg(SVCPOOL *pool, void (*dispatch)(SVCXPRT *))
624 struct svc_loss_callout *s;
626 mtx_lock(&pool->sp_lock);
627 TAILQ_FOREACH(s, &pool->sp_lcallouts, slc_link) {
628 if (s->slc_dispatch == dispatch) {
629 TAILQ_REMOVE(&pool->sp_lcallouts, s, slc_link);
634 mtx_unlock(&pool->sp_lock);
637 /* ********************** CALLOUT list related stuff ************* */
640 * Search the callout list for a program number, return the callout
643 static struct svc_callout *
644 svc_find(SVCPOOL *pool, rpcprog_t prog, rpcvers_t vers, char *netid)
646 struct svc_callout *s;
648 mtx_assert(&pool->sp_lock, MA_OWNED);
649 TAILQ_FOREACH(s, &pool->sp_callouts, sc_link) {
650 if (s->sc_prog == prog && s->sc_vers == vers
651 && (netid == NULL || s->sc_netid == NULL ||
652 strcmp(netid, s->sc_netid) == 0))
659 /* ******************* REPLY GENERATION ROUTINES ************ */
662 svc_sendreply_common(struct svc_req *rqstp, struct rpc_msg *rply,
665 SVCXPRT *xprt = rqstp->rq_xprt;
668 if (rqstp->rq_args) {
669 m_freem(rqstp->rq_args);
670 rqstp->rq_args = NULL;
673 if (xprt->xp_pool->sp_rcache)
674 replay_setreply(xprt->xp_pool->sp_rcache,
675 rply, svc_getrpccaller(rqstp), body);
677 if (!SVCAUTH_WRAP(&rqstp->rq_auth, &body))
680 ok = SVC_REPLY(xprt, rply, rqstp->rq_addr, body, &rqstp->rq_reply_seq);
681 if (rqstp->rq_addr) {
682 free(rqstp->rq_addr, M_SONAME);
683 rqstp->rq_addr = NULL;
690 * Send a reply to an rpc request
693 svc_sendreply(struct svc_req *rqstp, xdrproc_t xdr_results, void * xdr_location)
700 rply.rm_xid = rqstp->rq_xid;
701 rply.rm_direction = REPLY;
702 rply.rm_reply.rp_stat = MSG_ACCEPTED;
703 rply.acpted_rply.ar_verf = rqstp->rq_verf;
704 rply.acpted_rply.ar_stat = SUCCESS;
705 rply.acpted_rply.ar_results.where = NULL;
706 rply.acpted_rply.ar_results.proc = (xdrproc_t) xdr_void;
708 m = m_getcl(M_WAITOK, MT_DATA, 0);
709 xdrmbuf_create(&xdrs, m, XDR_ENCODE);
710 ok = xdr_results(&xdrs, xdr_location);
714 return (svc_sendreply_common(rqstp, &rply, m));
722 svc_sendreply_mbuf(struct svc_req *rqstp, struct mbuf *m)
726 rply.rm_xid = rqstp->rq_xid;
727 rply.rm_direction = REPLY;
728 rply.rm_reply.rp_stat = MSG_ACCEPTED;
729 rply.acpted_rply.ar_verf = rqstp->rq_verf;
730 rply.acpted_rply.ar_stat = SUCCESS;
731 rply.acpted_rply.ar_results.where = NULL;
732 rply.acpted_rply.ar_results.proc = (xdrproc_t) xdr_void;
734 return (svc_sendreply_common(rqstp, &rply, m));
738 * No procedure error reply
741 svcerr_noproc(struct svc_req *rqstp)
743 SVCXPRT *xprt = rqstp->rq_xprt;
746 rply.rm_xid = rqstp->rq_xid;
747 rply.rm_direction = REPLY;
748 rply.rm_reply.rp_stat = MSG_ACCEPTED;
749 rply.acpted_rply.ar_verf = rqstp->rq_verf;
750 rply.acpted_rply.ar_stat = PROC_UNAVAIL;
752 if (xprt->xp_pool->sp_rcache)
753 replay_setreply(xprt->xp_pool->sp_rcache,
754 &rply, svc_getrpccaller(rqstp), NULL);
756 svc_sendreply_common(rqstp, &rply, NULL);
760 * Can't decode args error reply
763 svcerr_decode(struct svc_req *rqstp)
765 SVCXPRT *xprt = rqstp->rq_xprt;
768 rply.rm_xid = rqstp->rq_xid;
769 rply.rm_direction = REPLY;
770 rply.rm_reply.rp_stat = MSG_ACCEPTED;
771 rply.acpted_rply.ar_verf = rqstp->rq_verf;
772 rply.acpted_rply.ar_stat = GARBAGE_ARGS;
774 if (xprt->xp_pool->sp_rcache)
775 replay_setreply(xprt->xp_pool->sp_rcache,
776 &rply, (struct sockaddr *) &xprt->xp_rtaddr, NULL);
778 svc_sendreply_common(rqstp, &rply, NULL);
785 svcerr_systemerr(struct svc_req *rqstp)
787 SVCXPRT *xprt = rqstp->rq_xprt;
790 rply.rm_xid = rqstp->rq_xid;
791 rply.rm_direction = REPLY;
792 rply.rm_reply.rp_stat = MSG_ACCEPTED;
793 rply.acpted_rply.ar_verf = rqstp->rq_verf;
794 rply.acpted_rply.ar_stat = SYSTEM_ERR;
796 if (xprt->xp_pool->sp_rcache)
797 replay_setreply(xprt->xp_pool->sp_rcache,
798 &rply, svc_getrpccaller(rqstp), NULL);
800 svc_sendreply_common(rqstp, &rply, NULL);
804 * Authentication error reply
807 svcerr_auth(struct svc_req *rqstp, enum auth_stat why)
809 SVCXPRT *xprt = rqstp->rq_xprt;
812 rply.rm_xid = rqstp->rq_xid;
813 rply.rm_direction = REPLY;
814 rply.rm_reply.rp_stat = MSG_DENIED;
815 rply.rjcted_rply.rj_stat = AUTH_ERROR;
816 rply.rjcted_rply.rj_why = why;
818 if (xprt->xp_pool->sp_rcache)
819 replay_setreply(xprt->xp_pool->sp_rcache,
820 &rply, svc_getrpccaller(rqstp), NULL);
822 svc_sendreply_common(rqstp, &rply, NULL);
826 * Auth too weak error reply
829 svcerr_weakauth(struct svc_req *rqstp)
832 svcerr_auth(rqstp, AUTH_TOOWEAK);
836 * Program unavailable error reply
839 svcerr_noprog(struct svc_req *rqstp)
841 SVCXPRT *xprt = rqstp->rq_xprt;
844 rply.rm_xid = rqstp->rq_xid;
845 rply.rm_direction = REPLY;
846 rply.rm_reply.rp_stat = MSG_ACCEPTED;
847 rply.acpted_rply.ar_verf = rqstp->rq_verf;
848 rply.acpted_rply.ar_stat = PROG_UNAVAIL;
850 if (xprt->xp_pool->sp_rcache)
851 replay_setreply(xprt->xp_pool->sp_rcache,
852 &rply, svc_getrpccaller(rqstp), NULL);
854 svc_sendreply_common(rqstp, &rply, NULL);
858 * Program version mismatch error reply
861 svcerr_progvers(struct svc_req *rqstp, rpcvers_t low_vers, rpcvers_t high_vers)
863 SVCXPRT *xprt = rqstp->rq_xprt;
866 rply.rm_xid = rqstp->rq_xid;
867 rply.rm_direction = REPLY;
868 rply.rm_reply.rp_stat = MSG_ACCEPTED;
869 rply.acpted_rply.ar_verf = rqstp->rq_verf;
870 rply.acpted_rply.ar_stat = PROG_MISMATCH;
871 rply.acpted_rply.ar_vers.low = (uint32_t)low_vers;
872 rply.acpted_rply.ar_vers.high = (uint32_t)high_vers;
874 if (xprt->xp_pool->sp_rcache)
875 replay_setreply(xprt->xp_pool->sp_rcache,
876 &rply, svc_getrpccaller(rqstp), NULL);
878 svc_sendreply_common(rqstp, &rply, NULL);
882 * Allocate a new server transport structure. All fields are
883 * initialized to zero and xp_p3 is initialized to point at an
884 * extension structure to hold various flags and authentication
893 xprt = mem_alloc(sizeof(SVCXPRT));
894 ext = mem_alloc(sizeof(SVCXPRT_EXT));
896 refcount_init(&xprt->xp_refs, 1);
902 * Free a server transport structure.
905 svc_xprt_free(SVCXPRT *xprt)
908 mem_free(xprt->xp_p3, sizeof(SVCXPRT_EXT));
909 /* The size argument is ignored, so 0 is ok. */
910 mem_free(xprt->xp_gidp, 0);
911 mem_free(xprt, sizeof(SVCXPRT));
914 /* ******************* SERVER INPUT STUFF ******************* */
917 * Read RPC requests from a transport and queue them to be
918 * executed. We handle authentication and replay cache replies here.
919 * Actually dispatching the RPC is deferred till svc_executereq.
921 static enum xprt_stat
922 svc_getreq(SVCXPRT *xprt, struct svc_req **rqstp_ret)
924 SVCPOOL *pool = xprt->xp_pool;
928 struct svc_loss_callout *s;
931 /* now receive msgs from xprtprt (support batch calls) */
932 r = malloc(sizeof(*r), M_RPC, M_WAITOK|M_ZERO);
934 msg.rm_call.cb_cred.oa_base = r->rq_credarea;
935 msg.rm_call.cb_verf.oa_base = &r->rq_credarea[MAX_AUTH_BYTES];
936 r->rq_clntcred = &r->rq_credarea[2*MAX_AUTH_BYTES];
937 if (SVC_RECV(xprt, &msg, &r->rq_addr, &args)) {
941 * Handle replays and authenticate before queuing the
942 * request to be executed.
946 if (pool->sp_rcache) {
947 struct rpc_msg repmsg;
948 struct mbuf *repbody;
949 enum replay_state rs;
950 rs = replay_find(pool->sp_rcache, &msg,
951 svc_getrpccaller(r), &repmsg, &repbody);
956 SVC_REPLY(xprt, &repmsg, r->rq_addr,
957 repbody, &r->rq_reply_seq);
959 free(r->rq_addr, M_SONAME);
971 r->rq_xid = msg.rm_xid;
972 r->rq_prog = msg.rm_call.cb_prog;
973 r->rq_vers = msg.rm_call.cb_vers;
974 r->rq_proc = msg.rm_call.cb_proc;
975 r->rq_size = sizeof(*r) + m_length(args, NULL);
977 if ((why = _authenticate(r, &msg)) != AUTH_OK) {
979 * RPCSEC_GSS uses this return code
980 * for requests that form part of its
981 * context establishment protocol and
982 * should not be dispatched to the
985 if (why != RPCSEC_GSS_NODISPATCH)
990 if (!SVCAUTH_UNWRAP(&r->rq_auth, &r->rq_args)) {
996 * Everything checks out, return request to caller.
1006 if ((stat = SVC_STAT(xprt)) == XPRT_DIED) {
1007 TAILQ_FOREACH(s, &pool->sp_lcallouts, slc_link)
1008 (*s->slc_dispatch)(xprt);
1009 xprt_unregister(xprt);
1016 svc_executereq(struct svc_req *rqstp)
1018 SVCXPRT *xprt = rqstp->rq_xprt;
1019 SVCPOOL *pool = xprt->xp_pool;
1022 rpcvers_t high_vers;
1023 struct svc_callout *s;
1025 /* now match message with a registered service*/
1027 low_vers = (rpcvers_t) -1L;
1028 high_vers = (rpcvers_t) 0L;
1029 TAILQ_FOREACH(s, &pool->sp_callouts, sc_link) {
1030 if (s->sc_prog == rqstp->rq_prog) {
1031 if (s->sc_vers == rqstp->rq_vers) {
1033 * We hand ownership of r to the
1034 * dispatch method - they must call
1037 (*s->sc_dispatch)(rqstp, xprt);
1039 } /* found correct version */
1041 if (s->sc_vers < low_vers)
1042 low_vers = s->sc_vers;
1043 if (s->sc_vers > high_vers)
1044 high_vers = s->sc_vers;
1045 } /* found correct program */
1049 * if we got here, the program or version
1053 svcerr_progvers(rqstp, low_vers, high_vers);
1055 svcerr_noprog(rqstp);
1061 svc_checkidle(SVCGROUP *grp)
1063 SVCXPRT *xprt, *nxprt;
1065 struct svcxprt_list cleanup;
1067 TAILQ_INIT(&cleanup);
1068 TAILQ_FOREACH_SAFE(xprt, &grp->sg_xlist, xp_link, nxprt) {
1070 * Only some transports have idle timers. Don't time
1071 * something out which is just waking up.
1073 if (!xprt->xp_idletimeout || xprt->xp_thread)
1076 timo = xprt->xp_lastactive + xprt->xp_idletimeout;
1077 if (time_uptime > timo) {
1078 xprt_unregister_locked(xprt);
1079 TAILQ_INSERT_TAIL(&cleanup, xprt, xp_link);
1083 mtx_unlock(&grp->sg_lock);
1084 TAILQ_FOREACH_SAFE(xprt, &cleanup, xp_link, nxprt) {
1085 soshutdown(xprt->xp_socket, SHUT_WR);
1088 mtx_lock(&grp->sg_lock);
1092 svc_assign_waiting_sockets(SVCPOOL *pool)
1098 for (g = 0; g < pool->sp_groupcount; g++) {
1099 grp = &pool->sp_groups[g];
1100 mtx_lock(&grp->sg_lock);
1101 while ((xprt = TAILQ_FIRST(&grp->sg_active)) != NULL) {
1102 if (xprt_assignthread(xprt))
1103 TAILQ_REMOVE(&grp->sg_active, xprt, xp_alink);
1107 mtx_unlock(&grp->sg_lock);
1112 svc_change_space_used(SVCPOOL *pool, long delta)
1114 unsigned long value;
1116 value = atomic_fetchadd_long(&pool->sp_space_used, delta) + delta;
1118 if (value >= pool->sp_space_high && !pool->sp_space_throttled) {
1119 pool->sp_space_throttled = TRUE;
1120 pool->sp_space_throttle_count++;
1122 if (value > pool->sp_space_used_highest)
1123 pool->sp_space_used_highest = value;
1125 if (value < pool->sp_space_low && pool->sp_space_throttled) {
1126 pool->sp_space_throttled = FALSE;
1127 svc_assign_waiting_sockets(pool);
1133 svc_request_space_available(SVCPOOL *pool)
1136 if (pool->sp_space_throttled)
1142 svc_run_internal(SVCGROUP *grp, bool_t ismaster)
1144 SVCPOOL *pool = grp->sg_pool;
1145 SVCTHREAD *st, *stpref;
1147 enum xprt_stat stat;
1148 struct svc_req *rqstp;
1153 st = mem_alloc(sizeof(*st));
1154 mtx_init(&st->st_lock, "st_lock", NULL, MTX_DEF);
1157 STAILQ_INIT(&st->st_reqs);
1158 cv_init(&st->st_cond, "rpcsvc");
1160 mtx_lock(&grp->sg_lock);
1163 * If we are a new thread which was spawned to cope with
1164 * increased load, set the state back to SVCPOOL_ACTIVE.
1166 if (grp->sg_state == SVCPOOL_THREADSTARTING)
1167 grp->sg_state = SVCPOOL_ACTIVE;
1169 while (grp->sg_state != SVCPOOL_CLOSING) {
1171 * Create new thread if requested.
1173 if (grp->sg_state == SVCPOOL_THREADWANTED) {
1174 grp->sg_state = SVCPOOL_THREADSTARTING;
1175 grp->sg_lastcreatetime = time_uptime;
1176 mtx_unlock(&grp->sg_lock);
1177 svc_new_thread(grp);
1178 mtx_lock(&grp->sg_lock);
1183 * Check for idle transports once per second.
1185 if (time_uptime > grp->sg_lastidlecheck) {
1186 grp->sg_lastidlecheck = time_uptime;
1193 * Enforce maxthreads count.
1195 if (!ismaster && grp->sg_threadcount >
1200 * Before sleeping, see if we can find an
1201 * active transport which isn't being serviced
1204 if (svc_request_space_available(pool) &&
1205 (xprt = TAILQ_FIRST(&grp->sg_active)) != NULL) {
1206 TAILQ_REMOVE(&grp->sg_active, xprt, xp_alink);
1208 xprt->xp_thread = st;
1213 LIST_INSERT_HEAD(&grp->sg_idlethreads, st, st_ilink);
1214 if (ismaster || (!ismaster &&
1215 grp->sg_threadcount > grp->sg_minthreads))
1216 error = cv_timedwait_sig(&st->st_cond,
1217 &grp->sg_lock, 5 * hz);
1219 error = cv_wait_sig(&st->st_cond,
1221 if (st->st_xprt == NULL)
1222 LIST_REMOVE(st, st_ilink);
1225 * Reduce worker thread count when idle.
1227 if (error == EWOULDBLOCK) {
1229 && (grp->sg_threadcount
1230 > grp->sg_minthreads)
1233 } else if (error != 0) {
1234 KASSERT(error == EINTR || error == ERESTART,
1235 ("non-signal error %d", error));
1236 mtx_unlock(&grp->sg_lock);
1239 if (P_SHOULDSTOP(p) ||
1240 (p->p_flag & P_TOTAL_STOP) != 0) {
1241 thread_suspend_check(0);
1243 mtx_lock(&grp->sg_lock);
1247 mtx_lock(&grp->sg_lock);
1253 mtx_unlock(&grp->sg_lock);
1256 * Drain the transport socket and queue up any RPCs.
1258 xprt->xp_lastactive = time_uptime;
1260 if (!svc_request_space_available(pool))
1263 stat = svc_getreq(xprt, &rqstp);
1265 svc_change_space_used(pool, rqstp->rq_size);
1267 * See if the application has a preference
1268 * for some other thread.
1270 if (pool->sp_assign) {
1271 stpref = pool->sp_assign(st, rqstp);
1272 rqstp->rq_thread = stpref;
1273 STAILQ_INSERT_TAIL(&stpref->st_reqs,
1275 mtx_unlock(&stpref->st_lock);
1279 rqstp->rq_thread = st;
1280 STAILQ_INSERT_TAIL(&st->st_reqs,
1284 } while (rqstp == NULL && stat == XPRT_MOREREQS
1285 && grp->sg_state != SVCPOOL_CLOSING);
1288 * Move this transport to the end of the active list to
1289 * ensure fairness when multiple transports are active.
1290 * If this was the last queued request, svc_getreq will end
1291 * up calling xprt_inactive to remove from the active list.
1293 mtx_lock(&grp->sg_lock);
1294 xprt->xp_thread = NULL;
1296 if (xprt->xp_active) {
1297 if (!svc_request_space_available(pool) ||
1298 !xprt_assignthread(xprt))
1299 TAILQ_INSERT_TAIL(&grp->sg_active,
1302 mtx_unlock(&grp->sg_lock);
1306 * Execute what we have queued.
1308 mtx_lock(&st->st_lock);
1309 while ((rqstp = STAILQ_FIRST(&st->st_reqs)) != NULL) {
1310 STAILQ_REMOVE_HEAD(&st->st_reqs, rq_link);
1311 mtx_unlock(&st->st_lock);
1312 sz = (long)rqstp->rq_size;
1313 svc_executereq(rqstp);
1314 svc_change_space_used(pool, -sz);
1315 mtx_lock(&st->st_lock);
1317 mtx_unlock(&st->st_lock);
1318 mtx_lock(&grp->sg_lock);
1326 KASSERT(STAILQ_EMPTY(&st->st_reqs), ("stray reqs on exit"));
1327 mtx_destroy(&st->st_lock);
1328 cv_destroy(&st->st_cond);
1329 mem_free(st, sizeof(*st));
1331 grp->sg_threadcount--;
1334 mtx_unlock(&grp->sg_lock);
1338 svc_thread_start(void *arg)
1341 svc_run_internal((SVCGROUP *) arg, FALSE);
1346 svc_new_thread(SVCGROUP *grp)
1348 SVCPOOL *pool = grp->sg_pool;
1351 mtx_lock(&grp->sg_lock);
1352 grp->sg_threadcount++;
1353 mtx_unlock(&grp->sg_lock);
1354 kthread_add(svc_thread_start, grp, pool->sp_proc, &td, 0, 0,
1355 "%s: service", pool->sp_name);
1359 svc_run(SVCPOOL *pool)
1368 snprintf(td->td_name, sizeof(td->td_name),
1369 "%s: master", pool->sp_name);
1370 pool->sp_state = SVCPOOL_ACTIVE;
1373 /* Choose group count based on number of threads and CPUs. */
1374 pool->sp_groupcount = max(1, min(SVC_MAXGROUPS,
1375 min(pool->sp_maxthreads / 2, mp_ncpus) / 6));
1376 for (g = 0; g < pool->sp_groupcount; g++) {
1377 grp = &pool->sp_groups[g];
1378 grp->sg_minthreads = max(1,
1379 pool->sp_minthreads / pool->sp_groupcount);
1380 grp->sg_maxthreads = max(1,
1381 pool->sp_maxthreads / pool->sp_groupcount);
1382 grp->sg_lastcreatetime = time_uptime;
1385 /* Starting threads */
1386 pool->sp_groups[0].sg_threadcount++;
1387 for (g = 0; g < pool->sp_groupcount; g++) {
1388 grp = &pool->sp_groups[g];
1389 for (i = ((g == 0) ? 1 : 0); i < grp->sg_minthreads; i++)
1390 svc_new_thread(grp);
1392 svc_run_internal(&pool->sp_groups[0], TRUE);
1394 /* Waiting for threads to stop. */
1395 for (g = 0; g < pool->sp_groupcount; g++) {
1396 grp = &pool->sp_groups[g];
1397 mtx_lock(&grp->sg_lock);
1398 while (grp->sg_threadcount > 0)
1399 msleep(grp, &grp->sg_lock, 0, "svcexit", 0);
1400 mtx_unlock(&grp->sg_lock);
1405 svc_exit(SVCPOOL *pool)
1411 pool->sp_state = SVCPOOL_CLOSING;
1412 for (g = 0; g < pool->sp_groupcount; g++) {
1413 grp = &pool->sp_groups[g];
1414 mtx_lock(&grp->sg_lock);
1415 if (grp->sg_state != SVCPOOL_CLOSING) {
1416 grp->sg_state = SVCPOOL_CLOSING;
1417 LIST_FOREACH(st, &grp->sg_idlethreads, st_ilink)
1418 cv_signal(&st->st_cond);
1420 mtx_unlock(&grp->sg_lock);
1425 svc_getargs(struct svc_req *rqstp, xdrproc_t xargs, void *args)
1432 rqstp->rq_args = NULL;
1434 xdrmbuf_create(&xdrs, m, XDR_DECODE);
1435 stat = xargs(&xdrs, args);
1442 svc_freeargs(struct svc_req *rqstp, xdrproc_t xargs, void *args)
1446 if (rqstp->rq_addr) {
1447 free(rqstp->rq_addr, M_SONAME);
1448 rqstp->rq_addr = NULL;
1451 xdrs.x_op = XDR_FREE;
1452 return (xargs(&xdrs, args));
1456 svc_freereq(struct svc_req *rqstp)
1461 st = rqstp->rq_thread;
1465 pool->sp_done(st, rqstp);
1468 if (rqstp->rq_auth.svc_ah_ops)
1469 SVCAUTH_RELEASE(&rqstp->rq_auth);
1471 if (rqstp->rq_xprt) {
1472 SVC_RELEASE(rqstp->rq_xprt);
1476 free(rqstp->rq_addr, M_SONAME);
1479 m_freem(rqstp->rq_args);