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) {
209 mtx_lock(&pool->sp_lock);
210 while ((s = TAILQ_FIRST(&pool->sp_callouts)) != NULL) {
211 mtx_unlock(&pool->sp_lock);
212 svc_unreg(pool, s->sc_prog, s->sc_vers);
213 mtx_lock(&pool->sp_lock);
215 while ((sl = TAILQ_FIRST(&pool->sp_lcallouts)) != NULL) {
216 mtx_unlock(&pool->sp_lock);
217 svc_loss_unreg(pool, sl->slc_dispatch);
218 mtx_lock(&pool->sp_lock);
220 mtx_unlock(&pool->sp_lock);
224 svcpool_destroy(SVCPOOL *pool)
229 svcpool_cleanup(pool);
231 for (g = 0; g < SVC_MAXGROUPS; g++) {
232 grp = &pool->sp_groups[g];
233 mtx_destroy(&grp->sg_lock);
235 mtx_destroy(&pool->sp_lock);
238 replay_freecache(pool->sp_rcache);
240 sysctl_ctx_free(&pool->sp_sysctl);
245 * Similar to svcpool_destroy(), except that it does not destroy the actual
246 * data structures. As such, "pool" may be used again.
249 svcpool_close(SVCPOOL *pool)
254 svcpool_cleanup(pool);
256 /* Now, initialize the pool's state for a fresh svc_run() call. */
257 mtx_lock(&pool->sp_lock);
258 pool->sp_state = SVCPOOL_INIT;
259 mtx_unlock(&pool->sp_lock);
260 for (g = 0; g < SVC_MAXGROUPS; g++) {
261 grp = &pool->sp_groups[g];
262 mtx_lock(&grp->sg_lock);
263 grp->sg_state = SVCPOOL_ACTIVE;
264 mtx_unlock(&grp->sg_lock);
269 * Sysctl handler to get the present thread count on a pool
272 svcpool_threads_sysctl(SYSCTL_HANDLER_ARGS)
275 int threads, error, g;
277 pool = oidp->oid_arg1;
279 mtx_lock(&pool->sp_lock);
280 for (g = 0; g < pool->sp_groupcount; g++)
281 threads += pool->sp_groups[g].sg_threadcount;
282 mtx_unlock(&pool->sp_lock);
283 error = sysctl_handle_int(oidp, &threads, 0, req);
288 * Sysctl handler to set the minimum thread count on a pool
291 svcpool_minthread_sysctl(SYSCTL_HANDLER_ARGS)
294 int newminthreads, error, g;
296 pool = oidp->oid_arg1;
297 newminthreads = pool->sp_minthreads;
298 error = sysctl_handle_int(oidp, &newminthreads, 0, req);
299 if (error == 0 && newminthreads != pool->sp_minthreads) {
300 if (newminthreads > pool->sp_maxthreads)
302 mtx_lock(&pool->sp_lock);
303 pool->sp_minthreads = newminthreads;
304 for (g = 0; g < pool->sp_groupcount; g++) {
305 pool->sp_groups[g].sg_minthreads = max(1,
306 pool->sp_minthreads / pool->sp_groupcount);
308 mtx_unlock(&pool->sp_lock);
314 * Sysctl handler to set the maximum thread count on a pool
317 svcpool_maxthread_sysctl(SYSCTL_HANDLER_ARGS)
320 int newmaxthreads, error, g;
322 pool = oidp->oid_arg1;
323 newmaxthreads = pool->sp_maxthreads;
324 error = sysctl_handle_int(oidp, &newmaxthreads, 0, req);
325 if (error == 0 && newmaxthreads != pool->sp_maxthreads) {
326 if (newmaxthreads < pool->sp_minthreads)
328 mtx_lock(&pool->sp_lock);
329 pool->sp_maxthreads = newmaxthreads;
330 for (g = 0; g < pool->sp_groupcount; g++) {
331 pool->sp_groups[g].sg_maxthreads = max(1,
332 pool->sp_maxthreads / pool->sp_groupcount);
334 mtx_unlock(&pool->sp_lock);
340 * Activate a transport handle.
343 xprt_register(SVCXPRT *xprt)
345 SVCPOOL *pool = xprt->xp_pool;
350 g = atomic_fetchadd_int(&pool->sp_nextgroup, 1) % pool->sp_groupcount;
351 xprt->xp_group = grp = &pool->sp_groups[g];
352 mtx_lock(&grp->sg_lock);
353 xprt->xp_registered = TRUE;
354 xprt->xp_active = FALSE;
355 TAILQ_INSERT_TAIL(&grp->sg_xlist, xprt, xp_link);
356 mtx_unlock(&grp->sg_lock);
360 * De-activate a transport handle. Note: the locked version doesn't
361 * release the transport - caller must do that after dropping the pool
365 xprt_unregister_locked(SVCXPRT *xprt)
367 SVCGROUP *grp = xprt->xp_group;
369 mtx_assert(&grp->sg_lock, MA_OWNED);
370 KASSERT(xprt->xp_registered == TRUE,
371 ("xprt_unregister_locked: not registered"));
372 xprt_inactive_locked(xprt);
373 TAILQ_REMOVE(&grp->sg_xlist, xprt, xp_link);
374 xprt->xp_registered = FALSE;
378 xprt_unregister(SVCXPRT *xprt)
380 SVCGROUP *grp = xprt->xp_group;
382 mtx_lock(&grp->sg_lock);
383 if (xprt->xp_registered == FALSE) {
384 /* Already unregistered by another thread */
385 mtx_unlock(&grp->sg_lock);
388 xprt_unregister_locked(xprt);
389 mtx_unlock(&grp->sg_lock);
395 * Attempt to assign a service thread to this transport.
398 xprt_assignthread(SVCXPRT *xprt)
400 SVCGROUP *grp = xprt->xp_group;
403 mtx_assert(&grp->sg_lock, MA_OWNED);
404 st = LIST_FIRST(&grp->sg_idlethreads);
406 LIST_REMOVE(st, st_ilink);
408 xprt->xp_thread = st;
410 cv_signal(&st->st_cond);
414 * See if we can create a new thread. The
415 * actual thread creation happens in
416 * svc_run_internal because our locking state
417 * is poorly defined (we are typically called
418 * from a socket upcall). Don't create more
419 * than one thread per second.
421 if (grp->sg_state == SVCPOOL_ACTIVE
422 && grp->sg_lastcreatetime < time_uptime
423 && grp->sg_threadcount < grp->sg_maxthreads) {
424 grp->sg_state = SVCPOOL_THREADWANTED;
431 xprt_active(SVCXPRT *xprt)
433 SVCGROUP *grp = xprt->xp_group;
435 mtx_lock(&grp->sg_lock);
437 if (!xprt->xp_registered) {
439 * Race with xprt_unregister - we lose.
441 mtx_unlock(&grp->sg_lock);
445 if (!xprt->xp_active) {
446 xprt->xp_active = TRUE;
447 if (xprt->xp_thread == NULL) {
448 if (!svc_request_space_available(xprt->xp_pool) ||
449 !xprt_assignthread(xprt))
450 TAILQ_INSERT_TAIL(&grp->sg_active, xprt,
455 mtx_unlock(&grp->sg_lock);
459 xprt_inactive_locked(SVCXPRT *xprt)
461 SVCGROUP *grp = xprt->xp_group;
463 mtx_assert(&grp->sg_lock, MA_OWNED);
464 if (xprt->xp_active) {
465 if (xprt->xp_thread == NULL)
466 TAILQ_REMOVE(&grp->sg_active, xprt, xp_alink);
467 xprt->xp_active = FALSE;
472 xprt_inactive(SVCXPRT *xprt)
474 SVCGROUP *grp = xprt->xp_group;
476 mtx_lock(&grp->sg_lock);
477 xprt_inactive_locked(xprt);
478 mtx_unlock(&grp->sg_lock);
482 * Variant of xprt_inactive() for use only when sure that port is
483 * assigned to thread. For example, within receive handlers.
486 xprt_inactive_self(SVCXPRT *xprt)
489 KASSERT(xprt->xp_thread != NULL,
490 ("xprt_inactive_self(%p) with NULL xp_thread", xprt));
491 xprt->xp_active = FALSE;
495 * Add a service program to the callout list.
496 * The dispatch routine will be called when a rpc request for this
497 * program number comes in.
500 svc_reg(SVCXPRT *xprt, const rpcprog_t prog, const rpcvers_t vers,
501 void (*dispatch)(struct svc_req *, SVCXPRT *),
502 const struct netconfig *nconf)
504 SVCPOOL *pool = xprt->xp_pool;
505 struct svc_callout *s;
509 /* VARIABLES PROTECTED BY svc_lock: s, svc_head */
511 if (xprt->xp_netid) {
512 netid = strdup(xprt->xp_netid, M_RPC);
514 } else if (nconf && nconf->nc_netid) {
515 netid = strdup(nconf->nc_netid, M_RPC);
517 } /* must have been created with svc_raw_create */
518 if ((netid == NULL) && (flag == 1)) {
522 mtx_lock(&pool->sp_lock);
523 if ((s = svc_find(pool, prog, vers, netid)) != NULL) {
526 if (s->sc_dispatch == dispatch)
527 goto rpcb_it; /* he is registering another xptr */
528 mtx_unlock(&pool->sp_lock);
531 s = malloc(sizeof (struct svc_callout), M_RPC, M_NOWAIT);
535 mtx_unlock(&pool->sp_lock);
541 s->sc_dispatch = dispatch;
543 TAILQ_INSERT_TAIL(&pool->sp_callouts, s, sc_link);
545 if ((xprt->xp_netid == NULL) && (flag == 1) && netid)
546 ((SVCXPRT *) xprt)->xp_netid = strdup(netid, M_RPC);
549 mtx_unlock(&pool->sp_lock);
550 /* now register the information with the local binder service */
553 struct netconfig tnc;
556 nb.buf = &xprt->xp_ltaddr;
557 nb.len = xprt->xp_ltaddr.ss_len;
558 dummy = rpcb_set(prog, vers, &tnc, &nb);
565 * Remove a service program from the callout list.
568 svc_unreg(SVCPOOL *pool, const rpcprog_t prog, const rpcvers_t vers)
570 struct svc_callout *s;
572 /* unregister the information anyway */
573 (void) rpcb_unset(prog, vers, NULL);
574 mtx_lock(&pool->sp_lock);
575 while ((s = svc_find(pool, prog, vers, NULL)) != NULL) {
576 TAILQ_REMOVE(&pool->sp_callouts, s, sc_link);
578 mem_free(s->sc_netid, sizeof (s->sc_netid) + 1);
579 mem_free(s, sizeof (struct svc_callout));
581 mtx_unlock(&pool->sp_lock);
585 * Add a service connection loss program to the callout list.
586 * The dispatch routine will be called when some port in ths pool die.
589 svc_loss_reg(SVCXPRT *xprt, void (*dispatch)(SVCXPRT *))
591 SVCPOOL *pool = xprt->xp_pool;
592 struct svc_loss_callout *s;
594 mtx_lock(&pool->sp_lock);
595 TAILQ_FOREACH(s, &pool->sp_lcallouts, slc_link) {
596 if (s->slc_dispatch == dispatch)
600 mtx_unlock(&pool->sp_lock);
603 s = malloc(sizeof(struct svc_loss_callout), M_RPC, M_NOWAIT);
605 mtx_unlock(&pool->sp_lock);
608 s->slc_dispatch = dispatch;
609 TAILQ_INSERT_TAIL(&pool->sp_lcallouts, s, slc_link);
610 mtx_unlock(&pool->sp_lock);
615 * Remove a service connection loss program from the callout list.
618 svc_loss_unreg(SVCPOOL *pool, void (*dispatch)(SVCXPRT *))
620 struct svc_loss_callout *s;
622 mtx_lock(&pool->sp_lock);
623 TAILQ_FOREACH(s, &pool->sp_lcallouts, slc_link) {
624 if (s->slc_dispatch == dispatch) {
625 TAILQ_REMOVE(&pool->sp_lcallouts, s, slc_link);
630 mtx_unlock(&pool->sp_lock);
633 /* ********************** CALLOUT list related stuff ************* */
636 * Search the callout list for a program number, return the callout
639 static struct svc_callout *
640 svc_find(SVCPOOL *pool, rpcprog_t prog, rpcvers_t vers, char *netid)
642 struct svc_callout *s;
644 mtx_assert(&pool->sp_lock, MA_OWNED);
645 TAILQ_FOREACH(s, &pool->sp_callouts, sc_link) {
646 if (s->sc_prog == prog && s->sc_vers == vers
647 && (netid == NULL || s->sc_netid == NULL ||
648 strcmp(netid, s->sc_netid) == 0))
655 /* ******************* REPLY GENERATION ROUTINES ************ */
658 svc_sendreply_common(struct svc_req *rqstp, struct rpc_msg *rply,
661 SVCXPRT *xprt = rqstp->rq_xprt;
664 if (rqstp->rq_args) {
665 m_freem(rqstp->rq_args);
666 rqstp->rq_args = NULL;
669 if (xprt->xp_pool->sp_rcache)
670 replay_setreply(xprt->xp_pool->sp_rcache,
671 rply, svc_getrpccaller(rqstp), body);
673 if (!SVCAUTH_WRAP(&rqstp->rq_auth, &body))
676 ok = SVC_REPLY(xprt, rply, rqstp->rq_addr, body, &rqstp->rq_reply_seq);
677 if (rqstp->rq_addr) {
678 free(rqstp->rq_addr, M_SONAME);
679 rqstp->rq_addr = NULL;
686 * Send a reply to an rpc request
689 svc_sendreply(struct svc_req *rqstp, xdrproc_t xdr_results, void * xdr_location)
696 rply.rm_xid = rqstp->rq_xid;
697 rply.rm_direction = REPLY;
698 rply.rm_reply.rp_stat = MSG_ACCEPTED;
699 rply.acpted_rply.ar_verf = rqstp->rq_verf;
700 rply.acpted_rply.ar_stat = SUCCESS;
701 rply.acpted_rply.ar_results.where = NULL;
702 rply.acpted_rply.ar_results.proc = (xdrproc_t) xdr_void;
704 m = m_getcl(M_WAITOK, MT_DATA, 0);
705 xdrmbuf_create(&xdrs, m, XDR_ENCODE);
706 ok = xdr_results(&xdrs, xdr_location);
710 return (svc_sendreply_common(rqstp, &rply, m));
718 svc_sendreply_mbuf(struct svc_req *rqstp, struct mbuf *m)
722 rply.rm_xid = rqstp->rq_xid;
723 rply.rm_direction = REPLY;
724 rply.rm_reply.rp_stat = MSG_ACCEPTED;
725 rply.acpted_rply.ar_verf = rqstp->rq_verf;
726 rply.acpted_rply.ar_stat = SUCCESS;
727 rply.acpted_rply.ar_results.where = NULL;
728 rply.acpted_rply.ar_results.proc = (xdrproc_t) xdr_void;
730 return (svc_sendreply_common(rqstp, &rply, m));
734 * No procedure error reply
737 svcerr_noproc(struct svc_req *rqstp)
739 SVCXPRT *xprt = rqstp->rq_xprt;
742 rply.rm_xid = rqstp->rq_xid;
743 rply.rm_direction = REPLY;
744 rply.rm_reply.rp_stat = MSG_ACCEPTED;
745 rply.acpted_rply.ar_verf = rqstp->rq_verf;
746 rply.acpted_rply.ar_stat = PROC_UNAVAIL;
748 if (xprt->xp_pool->sp_rcache)
749 replay_setreply(xprt->xp_pool->sp_rcache,
750 &rply, svc_getrpccaller(rqstp), NULL);
752 svc_sendreply_common(rqstp, &rply, NULL);
756 * Can't decode args error reply
759 svcerr_decode(struct svc_req *rqstp)
761 SVCXPRT *xprt = rqstp->rq_xprt;
764 rply.rm_xid = rqstp->rq_xid;
765 rply.rm_direction = REPLY;
766 rply.rm_reply.rp_stat = MSG_ACCEPTED;
767 rply.acpted_rply.ar_verf = rqstp->rq_verf;
768 rply.acpted_rply.ar_stat = GARBAGE_ARGS;
770 if (xprt->xp_pool->sp_rcache)
771 replay_setreply(xprt->xp_pool->sp_rcache,
772 &rply, (struct sockaddr *) &xprt->xp_rtaddr, NULL);
774 svc_sendreply_common(rqstp, &rply, NULL);
781 svcerr_systemerr(struct svc_req *rqstp)
783 SVCXPRT *xprt = rqstp->rq_xprt;
786 rply.rm_xid = rqstp->rq_xid;
787 rply.rm_direction = REPLY;
788 rply.rm_reply.rp_stat = MSG_ACCEPTED;
789 rply.acpted_rply.ar_verf = rqstp->rq_verf;
790 rply.acpted_rply.ar_stat = SYSTEM_ERR;
792 if (xprt->xp_pool->sp_rcache)
793 replay_setreply(xprt->xp_pool->sp_rcache,
794 &rply, svc_getrpccaller(rqstp), NULL);
796 svc_sendreply_common(rqstp, &rply, NULL);
800 * Authentication error reply
803 svcerr_auth(struct svc_req *rqstp, enum auth_stat why)
805 SVCXPRT *xprt = rqstp->rq_xprt;
808 rply.rm_xid = rqstp->rq_xid;
809 rply.rm_direction = REPLY;
810 rply.rm_reply.rp_stat = MSG_DENIED;
811 rply.rjcted_rply.rj_stat = AUTH_ERROR;
812 rply.rjcted_rply.rj_why = why;
814 if (xprt->xp_pool->sp_rcache)
815 replay_setreply(xprt->xp_pool->sp_rcache,
816 &rply, svc_getrpccaller(rqstp), NULL);
818 svc_sendreply_common(rqstp, &rply, NULL);
822 * Auth too weak error reply
825 svcerr_weakauth(struct svc_req *rqstp)
828 svcerr_auth(rqstp, AUTH_TOOWEAK);
832 * Program unavailable error reply
835 svcerr_noprog(struct svc_req *rqstp)
837 SVCXPRT *xprt = rqstp->rq_xprt;
840 rply.rm_xid = rqstp->rq_xid;
841 rply.rm_direction = REPLY;
842 rply.rm_reply.rp_stat = MSG_ACCEPTED;
843 rply.acpted_rply.ar_verf = rqstp->rq_verf;
844 rply.acpted_rply.ar_stat = PROG_UNAVAIL;
846 if (xprt->xp_pool->sp_rcache)
847 replay_setreply(xprt->xp_pool->sp_rcache,
848 &rply, svc_getrpccaller(rqstp), NULL);
850 svc_sendreply_common(rqstp, &rply, NULL);
854 * Program version mismatch error reply
857 svcerr_progvers(struct svc_req *rqstp, rpcvers_t low_vers, rpcvers_t high_vers)
859 SVCXPRT *xprt = rqstp->rq_xprt;
862 rply.rm_xid = rqstp->rq_xid;
863 rply.rm_direction = REPLY;
864 rply.rm_reply.rp_stat = MSG_ACCEPTED;
865 rply.acpted_rply.ar_verf = rqstp->rq_verf;
866 rply.acpted_rply.ar_stat = PROG_MISMATCH;
867 rply.acpted_rply.ar_vers.low = (uint32_t)low_vers;
868 rply.acpted_rply.ar_vers.high = (uint32_t)high_vers;
870 if (xprt->xp_pool->sp_rcache)
871 replay_setreply(xprt->xp_pool->sp_rcache,
872 &rply, svc_getrpccaller(rqstp), NULL);
874 svc_sendreply_common(rqstp, &rply, NULL);
878 * Allocate a new server transport structure. All fields are
879 * initialized to zero and xp_p3 is initialized to point at an
880 * extension structure to hold various flags and authentication
889 xprt = mem_alloc(sizeof(SVCXPRT));
890 ext = mem_alloc(sizeof(SVCXPRT_EXT));
892 refcount_init(&xprt->xp_refs, 1);
898 * Free a server transport structure.
901 svc_xprt_free(SVCXPRT *xprt)
904 mem_free(xprt->xp_p3, sizeof(SVCXPRT_EXT));
905 /* The size argument is ignored, so 0 is ok. */
906 mem_free(xprt->xp_gidp, 0);
907 mem_free(xprt, sizeof(SVCXPRT));
910 /* ******************* SERVER INPUT STUFF ******************* */
913 * Read RPC requests from a transport and queue them to be
914 * executed. We handle authentication and replay cache replies here.
915 * Actually dispatching the RPC is deferred till svc_executereq.
917 static enum xprt_stat
918 svc_getreq(SVCXPRT *xprt, struct svc_req **rqstp_ret)
920 SVCPOOL *pool = xprt->xp_pool;
924 struct svc_loss_callout *s;
927 /* now receive msgs from xprtprt (support batch calls) */
928 r = malloc(sizeof(*r), M_RPC, M_WAITOK|M_ZERO);
930 msg.rm_call.cb_cred.oa_base = r->rq_credarea;
931 msg.rm_call.cb_verf.oa_base = &r->rq_credarea[MAX_AUTH_BYTES];
932 r->rq_clntcred = &r->rq_credarea[2*MAX_AUTH_BYTES];
933 if (SVC_RECV(xprt, &msg, &r->rq_addr, &args)) {
937 * Handle replays and authenticate before queuing the
938 * request to be executed.
942 if (pool->sp_rcache) {
943 struct rpc_msg repmsg;
944 struct mbuf *repbody;
945 enum replay_state rs;
946 rs = replay_find(pool->sp_rcache, &msg,
947 svc_getrpccaller(r), &repmsg, &repbody);
952 SVC_REPLY(xprt, &repmsg, r->rq_addr,
953 repbody, &r->rq_reply_seq);
955 free(r->rq_addr, M_SONAME);
967 r->rq_xid = msg.rm_xid;
968 r->rq_prog = msg.rm_call.cb_prog;
969 r->rq_vers = msg.rm_call.cb_vers;
970 r->rq_proc = msg.rm_call.cb_proc;
971 r->rq_size = sizeof(*r) + m_length(args, NULL);
973 if ((why = _authenticate(r, &msg)) != AUTH_OK) {
975 * RPCSEC_GSS uses this return code
976 * for requests that form part of its
977 * context establishment protocol and
978 * should not be dispatched to the
981 if (why != RPCSEC_GSS_NODISPATCH)
986 if (!SVCAUTH_UNWRAP(&r->rq_auth, &r->rq_args)) {
992 * Everything checks out, return request to caller.
1002 if ((stat = SVC_STAT(xprt)) == XPRT_DIED) {
1003 TAILQ_FOREACH(s, &pool->sp_lcallouts, slc_link)
1004 (*s->slc_dispatch)(xprt);
1005 xprt_unregister(xprt);
1012 svc_executereq(struct svc_req *rqstp)
1014 SVCXPRT *xprt = rqstp->rq_xprt;
1015 SVCPOOL *pool = xprt->xp_pool;
1018 rpcvers_t high_vers;
1019 struct svc_callout *s;
1021 /* now match message with a registered service*/
1023 low_vers = (rpcvers_t) -1L;
1024 high_vers = (rpcvers_t) 0L;
1025 TAILQ_FOREACH(s, &pool->sp_callouts, sc_link) {
1026 if (s->sc_prog == rqstp->rq_prog) {
1027 if (s->sc_vers == rqstp->rq_vers) {
1029 * We hand ownership of r to the
1030 * dispatch method - they must call
1033 (*s->sc_dispatch)(rqstp, xprt);
1035 } /* found correct version */
1037 if (s->sc_vers < low_vers)
1038 low_vers = s->sc_vers;
1039 if (s->sc_vers > high_vers)
1040 high_vers = s->sc_vers;
1041 } /* found correct program */
1045 * if we got here, the program or version
1049 svcerr_progvers(rqstp, low_vers, high_vers);
1051 svcerr_noprog(rqstp);
1057 svc_checkidle(SVCGROUP *grp)
1059 SVCXPRT *xprt, *nxprt;
1061 struct svcxprt_list cleanup;
1063 TAILQ_INIT(&cleanup);
1064 TAILQ_FOREACH_SAFE(xprt, &grp->sg_xlist, xp_link, nxprt) {
1066 * Only some transports have idle timers. Don't time
1067 * something out which is just waking up.
1069 if (!xprt->xp_idletimeout || xprt->xp_thread)
1072 timo = xprt->xp_lastactive + xprt->xp_idletimeout;
1073 if (time_uptime > timo) {
1074 xprt_unregister_locked(xprt);
1075 TAILQ_INSERT_TAIL(&cleanup, xprt, xp_link);
1079 mtx_unlock(&grp->sg_lock);
1080 TAILQ_FOREACH_SAFE(xprt, &cleanup, xp_link, nxprt) {
1083 mtx_lock(&grp->sg_lock);
1087 svc_assign_waiting_sockets(SVCPOOL *pool)
1093 for (g = 0; g < pool->sp_groupcount; g++) {
1094 grp = &pool->sp_groups[g];
1095 mtx_lock(&grp->sg_lock);
1096 while ((xprt = TAILQ_FIRST(&grp->sg_active)) != NULL) {
1097 if (xprt_assignthread(xprt))
1098 TAILQ_REMOVE(&grp->sg_active, xprt, xp_alink);
1102 mtx_unlock(&grp->sg_lock);
1107 svc_change_space_used(SVCPOOL *pool, long delta)
1109 unsigned long value;
1111 value = atomic_fetchadd_long(&pool->sp_space_used, delta) + delta;
1113 if (value >= pool->sp_space_high && !pool->sp_space_throttled) {
1114 pool->sp_space_throttled = TRUE;
1115 pool->sp_space_throttle_count++;
1117 if (value > pool->sp_space_used_highest)
1118 pool->sp_space_used_highest = value;
1120 if (value < pool->sp_space_low && pool->sp_space_throttled) {
1121 pool->sp_space_throttled = FALSE;
1122 svc_assign_waiting_sockets(pool);
1128 svc_request_space_available(SVCPOOL *pool)
1131 if (pool->sp_space_throttled)
1137 svc_run_internal(SVCGROUP *grp, bool_t ismaster)
1139 SVCPOOL *pool = grp->sg_pool;
1140 SVCTHREAD *st, *stpref;
1142 enum xprt_stat stat;
1143 struct svc_req *rqstp;
1148 st = mem_alloc(sizeof(*st));
1149 mtx_init(&st->st_lock, "st_lock", NULL, MTX_DEF);
1152 STAILQ_INIT(&st->st_reqs);
1153 cv_init(&st->st_cond, "rpcsvc");
1155 mtx_lock(&grp->sg_lock);
1158 * If we are a new thread which was spawned to cope with
1159 * increased load, set the state back to SVCPOOL_ACTIVE.
1161 if (grp->sg_state == SVCPOOL_THREADSTARTING)
1162 grp->sg_state = SVCPOOL_ACTIVE;
1164 while (grp->sg_state != SVCPOOL_CLOSING) {
1166 * Create new thread if requested.
1168 if (grp->sg_state == SVCPOOL_THREADWANTED) {
1169 grp->sg_state = SVCPOOL_THREADSTARTING;
1170 grp->sg_lastcreatetime = time_uptime;
1171 mtx_unlock(&grp->sg_lock);
1172 svc_new_thread(grp);
1173 mtx_lock(&grp->sg_lock);
1178 * Check for idle transports once per second.
1180 if (time_uptime > grp->sg_lastidlecheck) {
1181 grp->sg_lastidlecheck = time_uptime;
1188 * Enforce maxthreads count.
1190 if (!ismaster && grp->sg_threadcount >
1195 * Before sleeping, see if we can find an
1196 * active transport which isn't being serviced
1199 if (svc_request_space_available(pool) &&
1200 (xprt = TAILQ_FIRST(&grp->sg_active)) != NULL) {
1201 TAILQ_REMOVE(&grp->sg_active, xprt, xp_alink);
1203 xprt->xp_thread = st;
1208 LIST_INSERT_HEAD(&grp->sg_idlethreads, st, st_ilink);
1209 if (ismaster || (!ismaster &&
1210 grp->sg_threadcount > grp->sg_minthreads))
1211 error = cv_timedwait_sig(&st->st_cond,
1212 &grp->sg_lock, 5 * hz);
1214 error = cv_wait_sig(&st->st_cond,
1216 if (st->st_xprt == NULL)
1217 LIST_REMOVE(st, st_ilink);
1220 * Reduce worker thread count when idle.
1222 if (error == EWOULDBLOCK) {
1224 && (grp->sg_threadcount
1225 > grp->sg_minthreads)
1228 } else if (error != 0) {
1229 KASSERT(error == EINTR || error == ERESTART,
1230 ("non-signal error %d", error));
1231 mtx_unlock(&grp->sg_lock);
1234 if (P_SHOULDSTOP(p) ||
1235 (p->p_flag & P_TOTAL_STOP) != 0) {
1236 thread_suspend_check(0);
1238 mtx_lock(&grp->sg_lock);
1242 mtx_lock(&grp->sg_lock);
1248 mtx_unlock(&grp->sg_lock);
1251 * Drain the transport socket and queue up any RPCs.
1253 xprt->xp_lastactive = time_uptime;
1255 if (!svc_request_space_available(pool))
1258 stat = svc_getreq(xprt, &rqstp);
1260 svc_change_space_used(pool, rqstp->rq_size);
1262 * See if the application has a preference
1263 * for some other thread.
1265 if (pool->sp_assign) {
1266 stpref = pool->sp_assign(st, rqstp);
1267 rqstp->rq_thread = stpref;
1268 STAILQ_INSERT_TAIL(&stpref->st_reqs,
1270 mtx_unlock(&stpref->st_lock);
1274 rqstp->rq_thread = st;
1275 STAILQ_INSERT_TAIL(&st->st_reqs,
1279 } while (rqstp == NULL && stat == XPRT_MOREREQS
1280 && grp->sg_state != SVCPOOL_CLOSING);
1283 * Move this transport to the end of the active list to
1284 * ensure fairness when multiple transports are active.
1285 * If this was the last queued request, svc_getreq will end
1286 * up calling xprt_inactive to remove from the active list.
1288 mtx_lock(&grp->sg_lock);
1289 xprt->xp_thread = NULL;
1291 if (xprt->xp_active) {
1292 if (!svc_request_space_available(pool) ||
1293 !xprt_assignthread(xprt))
1294 TAILQ_INSERT_TAIL(&grp->sg_active,
1297 mtx_unlock(&grp->sg_lock);
1301 * Execute what we have queued.
1303 mtx_lock(&st->st_lock);
1304 while ((rqstp = STAILQ_FIRST(&st->st_reqs)) != NULL) {
1305 STAILQ_REMOVE_HEAD(&st->st_reqs, rq_link);
1306 mtx_unlock(&st->st_lock);
1307 sz = (long)rqstp->rq_size;
1308 svc_executereq(rqstp);
1309 svc_change_space_used(pool, -sz);
1310 mtx_lock(&st->st_lock);
1312 mtx_unlock(&st->st_lock);
1313 mtx_lock(&grp->sg_lock);
1321 KASSERT(STAILQ_EMPTY(&st->st_reqs), ("stray reqs on exit"));
1322 mtx_destroy(&st->st_lock);
1323 cv_destroy(&st->st_cond);
1324 mem_free(st, sizeof(*st));
1326 grp->sg_threadcount--;
1329 mtx_unlock(&grp->sg_lock);
1333 svc_thread_start(void *arg)
1336 svc_run_internal((SVCGROUP *) arg, FALSE);
1341 svc_new_thread(SVCGROUP *grp)
1343 SVCPOOL *pool = grp->sg_pool;
1346 mtx_lock(&grp->sg_lock);
1347 grp->sg_threadcount++;
1348 mtx_unlock(&grp->sg_lock);
1349 kthread_add(svc_thread_start, grp, pool->sp_proc, &td, 0, 0,
1350 "%s: service", pool->sp_name);
1354 svc_run(SVCPOOL *pool)
1363 snprintf(td->td_name, sizeof(td->td_name),
1364 "%s: master", pool->sp_name);
1365 pool->sp_state = SVCPOOL_ACTIVE;
1368 /* Choose group count based on number of threads and CPUs. */
1369 pool->sp_groupcount = max(1, min(SVC_MAXGROUPS,
1370 min(pool->sp_maxthreads / 2, mp_ncpus) / 6));
1371 for (g = 0; g < pool->sp_groupcount; g++) {
1372 grp = &pool->sp_groups[g];
1373 grp->sg_minthreads = max(1,
1374 pool->sp_minthreads / pool->sp_groupcount);
1375 grp->sg_maxthreads = max(1,
1376 pool->sp_maxthreads / pool->sp_groupcount);
1377 grp->sg_lastcreatetime = time_uptime;
1380 /* Starting threads */
1381 pool->sp_groups[0].sg_threadcount++;
1382 for (g = 0; g < pool->sp_groupcount; g++) {
1383 grp = &pool->sp_groups[g];
1384 for (i = ((g == 0) ? 1 : 0); i < grp->sg_minthreads; i++)
1385 svc_new_thread(grp);
1387 svc_run_internal(&pool->sp_groups[0], TRUE);
1389 /* Waiting for threads to stop. */
1390 for (g = 0; g < pool->sp_groupcount; g++) {
1391 grp = &pool->sp_groups[g];
1392 mtx_lock(&grp->sg_lock);
1393 while (grp->sg_threadcount > 0)
1394 msleep(grp, &grp->sg_lock, 0, "svcexit", 0);
1395 mtx_unlock(&grp->sg_lock);
1400 svc_exit(SVCPOOL *pool)
1406 pool->sp_state = SVCPOOL_CLOSING;
1407 for (g = 0; g < pool->sp_groupcount; g++) {
1408 grp = &pool->sp_groups[g];
1409 mtx_lock(&grp->sg_lock);
1410 if (grp->sg_state != SVCPOOL_CLOSING) {
1411 grp->sg_state = SVCPOOL_CLOSING;
1412 LIST_FOREACH(st, &grp->sg_idlethreads, st_ilink)
1413 cv_signal(&st->st_cond);
1415 mtx_unlock(&grp->sg_lock);
1420 svc_getargs(struct svc_req *rqstp, xdrproc_t xargs, void *args)
1427 rqstp->rq_args = NULL;
1429 xdrmbuf_create(&xdrs, m, XDR_DECODE);
1430 stat = xargs(&xdrs, args);
1437 svc_freeargs(struct svc_req *rqstp, xdrproc_t xargs, void *args)
1441 if (rqstp->rq_addr) {
1442 free(rqstp->rq_addr, M_SONAME);
1443 rqstp->rq_addr = NULL;
1446 xdrs.x_op = XDR_FREE;
1447 return (xargs(&xdrs, args));
1451 svc_freereq(struct svc_req *rqstp)
1456 st = rqstp->rq_thread;
1460 pool->sp_done(st, rqstp);
1463 if (rqstp->rq_auth.svc_ah_ops)
1464 SVCAUTH_RELEASE(&rqstp->rq_auth);
1466 if (rqstp->rq_xprt) {
1467 SVC_RELEASE(rqstp->rq_xprt);
1471 free(rqstp->rq_addr, M_SONAME);
1474 m_freem(rqstp->rq_args);