1 /* $NetBSD: svc.c,v 1.21 2000/07/06 03:10:35 christos Exp $ */
4 * Copyright (c) 2009, Sun Microsystems, Inc.
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.
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.
31 #if defined(LIBC_SCCS) && !defined(lint)
32 static char *sccsid2 = "@(#)svc.c 1.44 88/02/08 Copyr 1984 Sun Micro";
33 static char *sccsid = "@(#)svc.c 2.4 88/08/11 4.0 RPCSRC";
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
39 * svc.c, Server-side remote procedure call interface.
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.
45 * Copyright (C) 1984, Sun Microsystems, Inc.
48 #include <sys/param.h>
50 #include <sys/kernel.h>
51 #include <sys/kthread.h>
52 #include <sys/malloc.h>
54 #include <sys/mutex.h>
56 #include <sys/queue.h>
57 #include <sys/socketvar.h>
58 #include <sys/systm.h>
60 #include <sys/ucred.h>
63 #include <rpc/rpcb_clnt.h>
64 #include <rpc/replay.h>
66 #include <rpc/rpc_com.h>
68 #define SVC_VERSQUIET 0x0001 /* keep quiet about vers mismatch */
69 #define version_keepquiet(xp) (SVC_EXT(xp)->xp_flags & SVC_VERSQUIET)
71 static struct svc_callout *svc_find(SVCPOOL *pool, rpcprog_t, rpcvers_t,
73 static void svc_new_thread(SVCPOOL *pool);
74 static void xprt_unregister_locked(SVCXPRT *xprt);
75 static void svc_change_space_used(SVCPOOL *pool, int delta);
76 static bool_t svc_request_space_available(SVCPOOL *pool);
78 /* *************** SVCXPRT related stuff **************** */
80 static int svcpool_minthread_sysctl(SYSCTL_HANDLER_ARGS);
81 static int svcpool_maxthread_sysctl(SYSCTL_HANDLER_ARGS);
84 svcpool_create(const char *name, struct sysctl_oid_list *sysctl_base)
88 pool = malloc(sizeof(SVCPOOL), M_RPC, M_WAITOK|M_ZERO);
90 mtx_init(&pool->sp_lock, "sp_lock", NULL, MTX_DEF);
92 pool->sp_state = SVCPOOL_INIT;
94 TAILQ_INIT(&pool->sp_xlist);
95 TAILQ_INIT(&pool->sp_active);
96 TAILQ_INIT(&pool->sp_callouts);
97 TAILQ_INIT(&pool->sp_lcallouts);
98 LIST_INIT(&pool->sp_threads);
99 LIST_INIT(&pool->sp_idlethreads);
100 pool->sp_minthreads = 1;
101 pool->sp_maxthreads = 1;
102 pool->sp_threadcount = 0;
105 * Don't use more than a quarter of mbuf clusters or more than
106 * 45Mb buffering requests.
108 pool->sp_space_high = nmbclusters * MCLBYTES / 4;
109 if (pool->sp_space_high > 45 << 20)
110 pool->sp_space_high = 45 << 20;
111 pool->sp_space_low = 2 * pool->sp_space_high / 3;
113 sysctl_ctx_init(&pool->sp_sysctl);
115 SYSCTL_ADD_PROC(&pool->sp_sysctl, sysctl_base, OID_AUTO,
116 "minthreads", CTLTYPE_INT | CTLFLAG_RW,
117 pool, 0, svcpool_minthread_sysctl, "I", "");
118 SYSCTL_ADD_PROC(&pool->sp_sysctl, sysctl_base, OID_AUTO,
119 "maxthreads", CTLTYPE_INT | CTLFLAG_RW,
120 pool, 0, svcpool_maxthread_sysctl, "I", "");
121 SYSCTL_ADD_INT(&pool->sp_sysctl, sysctl_base, OID_AUTO,
122 "threads", CTLFLAG_RD, &pool->sp_threadcount, 0, "");
124 SYSCTL_ADD_UINT(&pool->sp_sysctl, sysctl_base, OID_AUTO,
125 "request_space_used", CTLFLAG_RD,
126 &pool->sp_space_used, 0,
127 "Space in parsed but not handled requests.");
129 SYSCTL_ADD_UINT(&pool->sp_sysctl, sysctl_base, OID_AUTO,
130 "request_space_used_highest", CTLFLAG_RD,
131 &pool->sp_space_used_highest, 0,
132 "Highest space used since reboot.");
134 SYSCTL_ADD_UINT(&pool->sp_sysctl, sysctl_base, OID_AUTO,
135 "request_space_high", CTLFLAG_RW,
136 &pool->sp_space_high, 0,
137 "Maximum space in parsed but not handled requests.");
139 SYSCTL_ADD_UINT(&pool->sp_sysctl, sysctl_base, OID_AUTO,
140 "request_space_low", CTLFLAG_RW,
141 &pool->sp_space_low, 0,
142 "Low water mark for request space.");
144 SYSCTL_ADD_INT(&pool->sp_sysctl, sysctl_base, OID_AUTO,
145 "request_space_throttled", CTLFLAG_RD,
146 &pool->sp_space_throttled, 0,
147 "Whether nfs requests are currently throttled");
149 SYSCTL_ADD_INT(&pool->sp_sysctl, sysctl_base, OID_AUTO,
150 "request_space_throttle_count", CTLFLAG_RD,
151 &pool->sp_space_throttle_count, 0,
152 "Count of times throttling based on request space has occurred");
159 svcpool_destroy(SVCPOOL *pool)
161 SVCXPRT *xprt, *nxprt;
162 struct svc_callout *s;
163 struct svc_loss_callout *sl;
164 struct svcxprt_list cleanup;
166 TAILQ_INIT(&cleanup);
167 mtx_lock(&pool->sp_lock);
169 while (TAILQ_FIRST(&pool->sp_xlist)) {
170 xprt = TAILQ_FIRST(&pool->sp_xlist);
171 xprt_unregister_locked(xprt);
172 TAILQ_INSERT_TAIL(&cleanup, xprt, xp_link);
175 while ((s = TAILQ_FIRST(&pool->sp_callouts)) != NULL) {
176 mtx_unlock(&pool->sp_lock);
177 svc_unreg(pool, s->sc_prog, s->sc_vers);
178 mtx_lock(&pool->sp_lock);
180 while ((sl = TAILQ_FIRST(&pool->sp_lcallouts)) != NULL) {
181 mtx_unlock(&pool->sp_lock);
182 svc_loss_unreg(pool, sl->slc_dispatch);
183 mtx_lock(&pool->sp_lock);
185 mtx_unlock(&pool->sp_lock);
187 TAILQ_FOREACH_SAFE(xprt, &cleanup, xp_link, nxprt) {
191 mtx_destroy(&pool->sp_lock);
194 replay_freecache(pool->sp_rcache);
196 sysctl_ctx_free(&pool->sp_sysctl);
201 svcpool_active(SVCPOOL *pool)
203 enum svcpool_state state = pool->sp_state;
205 if (state == SVCPOOL_INIT || state == SVCPOOL_CLOSING)
211 * Sysctl handler to set the minimum thread count on a pool
214 svcpool_minthread_sysctl(SYSCTL_HANDLER_ARGS)
217 int newminthreads, error, n;
219 pool = oidp->oid_arg1;
220 newminthreads = pool->sp_minthreads;
221 error = sysctl_handle_int(oidp, &newminthreads, 0, req);
222 if (error == 0 && newminthreads != pool->sp_minthreads) {
223 if (newminthreads > pool->sp_maxthreads)
225 mtx_lock(&pool->sp_lock);
226 if (newminthreads > pool->sp_minthreads
227 && svcpool_active(pool)) {
229 * If the pool is running and we are
230 * increasing, create some more threads now.
232 n = newminthreads - pool->sp_threadcount;
234 mtx_unlock(&pool->sp_lock);
236 svc_new_thread(pool);
237 mtx_lock(&pool->sp_lock);
240 pool->sp_minthreads = newminthreads;
241 mtx_unlock(&pool->sp_lock);
247 * Sysctl handler to set the maximum thread count on a pool
250 svcpool_maxthread_sysctl(SYSCTL_HANDLER_ARGS)
254 int newmaxthreads, error;
256 pool = oidp->oid_arg1;
257 newmaxthreads = pool->sp_maxthreads;
258 error = sysctl_handle_int(oidp, &newmaxthreads, 0, req);
259 if (error == 0 && newmaxthreads != pool->sp_maxthreads) {
260 if (newmaxthreads < pool->sp_minthreads)
262 mtx_lock(&pool->sp_lock);
263 if (newmaxthreads < pool->sp_maxthreads
264 && svcpool_active(pool)) {
266 * If the pool is running and we are
267 * decreasing, wake up some idle threads to
268 * encourage them to exit.
270 LIST_FOREACH(st, &pool->sp_idlethreads, st_ilink)
271 cv_signal(&st->st_cond);
273 pool->sp_maxthreads = newmaxthreads;
274 mtx_unlock(&pool->sp_lock);
280 * Activate a transport handle.
283 xprt_register(SVCXPRT *xprt)
285 SVCPOOL *pool = xprt->xp_pool;
288 mtx_lock(&pool->sp_lock);
289 xprt->xp_registered = TRUE;
290 xprt->xp_active = FALSE;
291 TAILQ_INSERT_TAIL(&pool->sp_xlist, xprt, xp_link);
292 mtx_unlock(&pool->sp_lock);
296 * De-activate a transport handle. Note: the locked version doesn't
297 * release the transport - caller must do that after dropping the pool
301 xprt_unregister_locked(SVCXPRT *xprt)
303 SVCPOOL *pool = xprt->xp_pool;
305 mtx_assert(&pool->sp_lock, MA_OWNED);
306 KASSERT(xprt->xp_registered == TRUE,
307 ("xprt_unregister_locked: not registered"));
308 xprt_inactive_locked(xprt);
309 TAILQ_REMOVE(&pool->sp_xlist, xprt, xp_link);
310 xprt->xp_registered = FALSE;
314 xprt_unregister(SVCXPRT *xprt)
316 SVCPOOL *pool = xprt->xp_pool;
318 mtx_lock(&pool->sp_lock);
319 if (xprt->xp_registered == FALSE) {
320 /* Already unregistered by another thread */
321 mtx_unlock(&pool->sp_lock);
324 xprt_unregister_locked(xprt);
325 mtx_unlock(&pool->sp_lock);
331 * Attempt to assign a service thread to this transport.
334 xprt_assignthread(SVCXPRT *xprt)
336 SVCPOOL *pool = xprt->xp_pool;
339 mtx_assert(&pool->sp_lock, MA_OWNED);
340 st = LIST_FIRST(&pool->sp_idlethreads);
342 LIST_REMOVE(st, st_ilink);
344 xprt->xp_thread = st;
346 cv_signal(&st->st_cond);
350 * See if we can create a new thread. The
351 * actual thread creation happens in
352 * svc_run_internal because our locking state
353 * is poorly defined (we are typically called
354 * from a socket upcall). Don't create more
355 * than one thread per second.
357 if (pool->sp_state == SVCPOOL_ACTIVE
358 && pool->sp_lastcreatetime < time_uptime
359 && pool->sp_threadcount < pool->sp_maxthreads) {
360 pool->sp_state = SVCPOOL_THREADWANTED;
367 xprt_active(SVCXPRT *xprt)
369 SVCPOOL *pool = xprt->xp_pool;
371 mtx_lock(&pool->sp_lock);
373 if (!xprt->xp_registered) {
375 * Race with xprt_unregister - we lose.
377 mtx_unlock(&pool->sp_lock);
381 if (!xprt->xp_active) {
382 xprt->xp_active = TRUE;
383 if (xprt->xp_thread == NULL) {
384 if (!svc_request_space_available(pool) ||
385 !xprt_assignthread(xprt))
386 TAILQ_INSERT_TAIL(&pool->sp_active, xprt,
391 mtx_unlock(&pool->sp_lock);
395 xprt_inactive_locked(SVCXPRT *xprt)
397 SVCPOOL *pool = xprt->xp_pool;
399 mtx_assert(&pool->sp_lock, MA_OWNED);
400 if (xprt->xp_active) {
401 if (xprt->xp_thread == NULL)
402 TAILQ_REMOVE(&pool->sp_active, xprt, xp_alink);
403 xprt->xp_active = FALSE;
408 xprt_inactive(SVCXPRT *xprt)
410 SVCPOOL *pool = xprt->xp_pool;
412 mtx_lock(&pool->sp_lock);
413 xprt_inactive_locked(xprt);
414 mtx_unlock(&pool->sp_lock);
418 * Variant of xprt_inactive() for use only when sure that port is
419 * assigned to thread. For example, withing receive handlers.
422 xprt_inactive_self(SVCXPRT *xprt)
425 KASSERT(xprt->xp_thread != NULL,
426 ("xprt_inactive_self(%p) with NULL xp_thread", xprt));
427 xprt->xp_active = FALSE;
431 * Add a service program to the callout list.
432 * The dispatch routine will be called when a rpc request for this
433 * program number comes in.
436 svc_reg(SVCXPRT *xprt, const rpcprog_t prog, const rpcvers_t vers,
437 void (*dispatch)(struct svc_req *, SVCXPRT *),
438 const struct netconfig *nconf)
440 SVCPOOL *pool = xprt->xp_pool;
441 struct svc_callout *s;
445 /* VARIABLES PROTECTED BY svc_lock: s, svc_head */
447 if (xprt->xp_netid) {
448 netid = strdup(xprt->xp_netid, M_RPC);
450 } else if (nconf && nconf->nc_netid) {
451 netid = strdup(nconf->nc_netid, M_RPC);
453 } /* must have been created with svc_raw_create */
454 if ((netid == NULL) && (flag == 1)) {
458 mtx_lock(&pool->sp_lock);
459 if ((s = svc_find(pool, prog, vers, netid)) != NULL) {
462 if (s->sc_dispatch == dispatch)
463 goto rpcb_it; /* he is registering another xptr */
464 mtx_unlock(&pool->sp_lock);
467 s = malloc(sizeof (struct svc_callout), M_RPC, M_NOWAIT);
471 mtx_unlock(&pool->sp_lock);
477 s->sc_dispatch = dispatch;
479 TAILQ_INSERT_TAIL(&pool->sp_callouts, s, sc_link);
481 if ((xprt->xp_netid == NULL) && (flag == 1) && netid)
482 ((SVCXPRT *) xprt)->xp_netid = strdup(netid, M_RPC);
485 mtx_unlock(&pool->sp_lock);
486 /* now register the information with the local binder service */
489 struct netconfig tnc;
492 nb.buf = &xprt->xp_ltaddr;
493 nb.len = xprt->xp_ltaddr.ss_len;
494 dummy = rpcb_set(prog, vers, &tnc, &nb);
501 * Remove a service program from the callout list.
504 svc_unreg(SVCPOOL *pool, const rpcprog_t prog, const rpcvers_t vers)
506 struct svc_callout *s;
508 /* unregister the information anyway */
509 (void) rpcb_unset(prog, vers, NULL);
510 mtx_lock(&pool->sp_lock);
511 while ((s = svc_find(pool, prog, vers, NULL)) != NULL) {
512 TAILQ_REMOVE(&pool->sp_callouts, s, sc_link);
514 mem_free(s->sc_netid, sizeof (s->sc_netid) + 1);
515 mem_free(s, sizeof (struct svc_callout));
517 mtx_unlock(&pool->sp_lock);
521 * Add a service connection loss program to the callout list.
522 * The dispatch routine will be called when some port in ths pool die.
525 svc_loss_reg(SVCXPRT *xprt, void (*dispatch)(SVCXPRT *))
527 SVCPOOL *pool = xprt->xp_pool;
528 struct svc_loss_callout *s;
530 mtx_lock(&pool->sp_lock);
531 TAILQ_FOREACH(s, &pool->sp_lcallouts, slc_link) {
532 if (s->slc_dispatch == dispatch)
536 mtx_unlock(&pool->sp_lock);
539 s = malloc(sizeof (struct svc_callout), M_RPC, M_NOWAIT);
541 mtx_unlock(&pool->sp_lock);
544 s->slc_dispatch = dispatch;
545 TAILQ_INSERT_TAIL(&pool->sp_lcallouts, s, slc_link);
546 mtx_unlock(&pool->sp_lock);
551 * Remove a service connection loss program from the callout list.
554 svc_loss_unreg(SVCPOOL *pool, void (*dispatch)(SVCXPRT *))
556 struct svc_loss_callout *s;
558 mtx_lock(&pool->sp_lock);
559 TAILQ_FOREACH(s, &pool->sp_lcallouts, slc_link) {
560 if (s->slc_dispatch == dispatch) {
561 TAILQ_REMOVE(&pool->sp_lcallouts, s, slc_link);
566 mtx_unlock(&pool->sp_lock);
569 /* ********************** CALLOUT list related stuff ************* */
572 * Search the callout list for a program number, return the callout
575 static struct svc_callout *
576 svc_find(SVCPOOL *pool, rpcprog_t prog, rpcvers_t vers, char *netid)
578 struct svc_callout *s;
580 mtx_assert(&pool->sp_lock, MA_OWNED);
581 TAILQ_FOREACH(s, &pool->sp_callouts, sc_link) {
582 if (s->sc_prog == prog && s->sc_vers == vers
583 && (netid == NULL || s->sc_netid == NULL ||
584 strcmp(netid, s->sc_netid) == 0))
591 /* ******************* REPLY GENERATION ROUTINES ************ */
594 svc_sendreply_common(struct svc_req *rqstp, struct rpc_msg *rply,
597 SVCXPRT *xprt = rqstp->rq_xprt;
600 if (rqstp->rq_args) {
601 m_freem(rqstp->rq_args);
602 rqstp->rq_args = NULL;
605 if (xprt->xp_pool->sp_rcache)
606 replay_setreply(xprt->xp_pool->sp_rcache,
607 rply, svc_getrpccaller(rqstp), body);
609 if (!SVCAUTH_WRAP(&rqstp->rq_auth, &body))
612 ok = SVC_REPLY(xprt, rply, rqstp->rq_addr, body, &rqstp->rq_reply_seq);
613 if (rqstp->rq_addr) {
614 free(rqstp->rq_addr, M_SONAME);
615 rqstp->rq_addr = NULL;
622 * Send a reply to an rpc request
625 svc_sendreply(struct svc_req *rqstp, xdrproc_t xdr_results, void * xdr_location)
632 rply.rm_xid = rqstp->rq_xid;
633 rply.rm_direction = REPLY;
634 rply.rm_reply.rp_stat = MSG_ACCEPTED;
635 rply.acpted_rply.ar_verf = rqstp->rq_verf;
636 rply.acpted_rply.ar_stat = SUCCESS;
637 rply.acpted_rply.ar_results.where = NULL;
638 rply.acpted_rply.ar_results.proc = (xdrproc_t) xdr_void;
640 MGET(m, M_WAIT, MT_DATA);
643 xdrmbuf_create(&xdrs, m, XDR_ENCODE);
644 ok = xdr_results(&xdrs, xdr_location);
648 return (svc_sendreply_common(rqstp, &rply, m));
656 svc_sendreply_mbuf(struct svc_req *rqstp, struct mbuf *m)
660 rply.rm_xid = rqstp->rq_xid;
661 rply.rm_direction = REPLY;
662 rply.rm_reply.rp_stat = MSG_ACCEPTED;
663 rply.acpted_rply.ar_verf = rqstp->rq_verf;
664 rply.acpted_rply.ar_stat = SUCCESS;
665 rply.acpted_rply.ar_results.where = NULL;
666 rply.acpted_rply.ar_results.proc = (xdrproc_t) xdr_void;
668 return (svc_sendreply_common(rqstp, &rply, m));
672 * No procedure error reply
675 svcerr_noproc(struct svc_req *rqstp)
677 SVCXPRT *xprt = rqstp->rq_xprt;
680 rply.rm_xid = rqstp->rq_xid;
681 rply.rm_direction = REPLY;
682 rply.rm_reply.rp_stat = MSG_ACCEPTED;
683 rply.acpted_rply.ar_verf = rqstp->rq_verf;
684 rply.acpted_rply.ar_stat = PROC_UNAVAIL;
686 if (xprt->xp_pool->sp_rcache)
687 replay_setreply(xprt->xp_pool->sp_rcache,
688 &rply, svc_getrpccaller(rqstp), NULL);
690 svc_sendreply_common(rqstp, &rply, NULL);
694 * Can't decode args error reply
697 svcerr_decode(struct svc_req *rqstp)
699 SVCXPRT *xprt = rqstp->rq_xprt;
702 rply.rm_xid = rqstp->rq_xid;
703 rply.rm_direction = REPLY;
704 rply.rm_reply.rp_stat = MSG_ACCEPTED;
705 rply.acpted_rply.ar_verf = rqstp->rq_verf;
706 rply.acpted_rply.ar_stat = GARBAGE_ARGS;
708 if (xprt->xp_pool->sp_rcache)
709 replay_setreply(xprt->xp_pool->sp_rcache,
710 &rply, (struct sockaddr *) &xprt->xp_rtaddr, NULL);
712 svc_sendreply_common(rqstp, &rply, NULL);
719 svcerr_systemerr(struct svc_req *rqstp)
721 SVCXPRT *xprt = rqstp->rq_xprt;
724 rply.rm_xid = rqstp->rq_xid;
725 rply.rm_direction = REPLY;
726 rply.rm_reply.rp_stat = MSG_ACCEPTED;
727 rply.acpted_rply.ar_verf = rqstp->rq_verf;
728 rply.acpted_rply.ar_stat = SYSTEM_ERR;
730 if (xprt->xp_pool->sp_rcache)
731 replay_setreply(xprt->xp_pool->sp_rcache,
732 &rply, svc_getrpccaller(rqstp), NULL);
734 svc_sendreply_common(rqstp, &rply, NULL);
738 * Authentication error reply
741 svcerr_auth(struct svc_req *rqstp, enum auth_stat why)
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_DENIED;
749 rply.rjcted_rply.rj_stat = AUTH_ERROR;
750 rply.rjcted_rply.rj_why = why;
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 * Auth too weak error reply
763 svcerr_weakauth(struct svc_req *rqstp)
766 svcerr_auth(rqstp, AUTH_TOOWEAK);
770 * Program unavailable error reply
773 svcerr_noprog(struct svc_req *rqstp)
775 SVCXPRT *xprt = rqstp->rq_xprt;
778 rply.rm_xid = rqstp->rq_xid;
779 rply.rm_direction = REPLY;
780 rply.rm_reply.rp_stat = MSG_ACCEPTED;
781 rply.acpted_rply.ar_verf = rqstp->rq_verf;
782 rply.acpted_rply.ar_stat = PROG_UNAVAIL;
784 if (xprt->xp_pool->sp_rcache)
785 replay_setreply(xprt->xp_pool->sp_rcache,
786 &rply, svc_getrpccaller(rqstp), NULL);
788 svc_sendreply_common(rqstp, &rply, NULL);
792 * Program version mismatch error reply
795 svcerr_progvers(struct svc_req *rqstp, rpcvers_t low_vers, rpcvers_t high_vers)
797 SVCXPRT *xprt = rqstp->rq_xprt;
800 rply.rm_xid = rqstp->rq_xid;
801 rply.rm_direction = REPLY;
802 rply.rm_reply.rp_stat = MSG_ACCEPTED;
803 rply.acpted_rply.ar_verf = rqstp->rq_verf;
804 rply.acpted_rply.ar_stat = PROG_MISMATCH;
805 rply.acpted_rply.ar_vers.low = (uint32_t)low_vers;
806 rply.acpted_rply.ar_vers.high = (uint32_t)high_vers;
808 if (xprt->xp_pool->sp_rcache)
809 replay_setreply(xprt->xp_pool->sp_rcache,
810 &rply, svc_getrpccaller(rqstp), NULL);
812 svc_sendreply_common(rqstp, &rply, NULL);
816 * Allocate a new server transport structure. All fields are
817 * initialized to zero and xp_p3 is initialized to point at an
818 * extension structure to hold various flags and authentication
827 xprt = mem_alloc(sizeof(SVCXPRT));
828 memset(xprt, 0, sizeof(SVCXPRT));
829 ext = mem_alloc(sizeof(SVCXPRT_EXT));
830 memset(ext, 0, sizeof(SVCXPRT_EXT));
832 refcount_init(&xprt->xp_refs, 1);
838 * Free a server transport structure.
845 mem_free(xprt->xp_p3, sizeof(SVCXPRT_EXT));
846 mem_free(xprt, sizeof(SVCXPRT));
849 /* ******************* SERVER INPUT STUFF ******************* */
852 * Read RPC requests from a transport and queue them to be
853 * executed. We handle authentication and replay cache replies here.
854 * Actually dispatching the RPC is deferred till svc_executereq.
856 static enum xprt_stat
857 svc_getreq(SVCXPRT *xprt, struct svc_req **rqstp_ret)
859 SVCPOOL *pool = xprt->xp_pool;
863 struct svc_loss_callout *s;
866 /* now receive msgs from xprtprt (support batch calls) */
867 r = malloc(sizeof(*r), M_RPC, M_WAITOK|M_ZERO);
869 msg.rm_call.cb_cred.oa_base = r->rq_credarea;
870 msg.rm_call.cb_verf.oa_base = &r->rq_credarea[MAX_AUTH_BYTES];
871 r->rq_clntcred = &r->rq_credarea[2*MAX_AUTH_BYTES];
872 if (SVC_RECV(xprt, &msg, &r->rq_addr, &args)) {
876 * Handle replays and authenticate before queuing the
877 * request to be executed.
881 if (pool->sp_rcache) {
882 struct rpc_msg repmsg;
883 struct mbuf *repbody;
884 enum replay_state rs;
885 rs = replay_find(pool->sp_rcache, &msg,
886 svc_getrpccaller(r), &repmsg, &repbody);
891 SVC_REPLY(xprt, &repmsg, r->rq_addr,
892 repbody, &r->rq_reply_seq);
894 free(r->rq_addr, M_SONAME);
906 r->rq_xid = msg.rm_xid;
907 r->rq_prog = msg.rm_call.cb_prog;
908 r->rq_vers = msg.rm_call.cb_vers;
909 r->rq_proc = msg.rm_call.cb_proc;
910 r->rq_size = sizeof(*r) + m_length(args, NULL);
912 if ((why = _authenticate(r, &msg)) != AUTH_OK) {
914 * RPCSEC_GSS uses this return code
915 * for requests that form part of its
916 * context establishment protocol and
917 * should not be dispatched to the
920 if (why != RPCSEC_GSS_NODISPATCH)
925 if (!SVCAUTH_UNWRAP(&r->rq_auth, &r->rq_args)) {
931 * Everything checks out, return request to caller.
941 if ((stat = SVC_STAT(xprt)) == XPRT_DIED) {
942 TAILQ_FOREACH(s, &pool->sp_lcallouts, slc_link)
943 (*s->slc_dispatch)(xprt);
944 xprt_unregister(xprt);
951 svc_executereq(struct svc_req *rqstp)
953 SVCXPRT *xprt = rqstp->rq_xprt;
954 SVCPOOL *pool = xprt->xp_pool;
958 struct svc_callout *s;
960 /* now match message with a registered service*/
962 low_vers = (rpcvers_t) -1L;
963 high_vers = (rpcvers_t) 0L;
964 TAILQ_FOREACH(s, &pool->sp_callouts, sc_link) {
965 if (s->sc_prog == rqstp->rq_prog) {
966 if (s->sc_vers == rqstp->rq_vers) {
968 * We hand ownership of r to the
969 * dispatch method - they must call
972 (*s->sc_dispatch)(rqstp, xprt);
974 } /* found correct version */
976 if (s->sc_vers < low_vers)
977 low_vers = s->sc_vers;
978 if (s->sc_vers > high_vers)
979 high_vers = s->sc_vers;
980 } /* found correct program */
984 * if we got here, the program or version
988 svcerr_progvers(rqstp, low_vers, high_vers);
990 svcerr_noprog(rqstp);
996 svc_checkidle(SVCPOOL *pool)
998 SVCXPRT *xprt, *nxprt;
1000 struct svcxprt_list cleanup;
1002 TAILQ_INIT(&cleanup);
1003 TAILQ_FOREACH_SAFE(xprt, &pool->sp_xlist, xp_link, nxprt) {
1005 * Only some transports have idle timers. Don't time
1006 * something out which is just waking up.
1008 if (!xprt->xp_idletimeout || xprt->xp_thread)
1011 timo = xprt->xp_lastactive + xprt->xp_idletimeout;
1012 if (time_uptime > timo) {
1013 xprt_unregister_locked(xprt);
1014 TAILQ_INSERT_TAIL(&cleanup, xprt, xp_link);
1018 mtx_unlock(&pool->sp_lock);
1019 TAILQ_FOREACH_SAFE(xprt, &cleanup, xp_link, nxprt) {
1022 mtx_lock(&pool->sp_lock);
1027 svc_assign_waiting_sockets(SVCPOOL *pool)
1031 mtx_lock(&pool->sp_lock);
1032 while ((xprt = TAILQ_FIRST(&pool->sp_active)) != NULL) {
1033 if (xprt_assignthread(xprt))
1034 TAILQ_REMOVE(&pool->sp_active, xprt, xp_alink);
1038 mtx_unlock(&pool->sp_lock);
1042 svc_change_space_used(SVCPOOL *pool, int delta)
1046 value = atomic_fetchadd_int(&pool->sp_space_used, delta) + delta;
1048 if (value >= pool->sp_space_high && !pool->sp_space_throttled) {
1049 pool->sp_space_throttled = TRUE;
1050 pool->sp_space_throttle_count++;
1052 if (value > pool->sp_space_used_highest)
1053 pool->sp_space_used_highest = value;
1055 if (value < pool->sp_space_low && pool->sp_space_throttled) {
1056 pool->sp_space_throttled = FALSE;
1057 svc_assign_waiting_sockets(pool);
1063 svc_request_space_available(SVCPOOL *pool)
1066 if (pool->sp_space_throttled)
1072 svc_run_internal(SVCPOOL *pool, bool_t ismaster)
1074 SVCTHREAD *st, *stpref;
1076 enum xprt_stat stat;
1077 struct svc_req *rqstp;
1081 st = mem_alloc(sizeof(*st));
1082 mtx_init(&st->st_lock, "st_lock", NULL, MTX_DEF);
1085 STAILQ_INIT(&st->st_reqs);
1086 cv_init(&st->st_cond, "rpcsvc");
1088 mtx_lock(&pool->sp_lock);
1089 LIST_INSERT_HEAD(&pool->sp_threads, st, st_link);
1092 * If we are a new thread which was spawned to cope with
1093 * increased load, set the state back to SVCPOOL_ACTIVE.
1095 if (pool->sp_state == SVCPOOL_THREADSTARTING)
1096 pool->sp_state = SVCPOOL_ACTIVE;
1098 while (pool->sp_state != SVCPOOL_CLOSING) {
1100 * Create new thread if requested.
1102 if (pool->sp_state == SVCPOOL_THREADWANTED) {
1103 pool->sp_state = SVCPOOL_THREADSTARTING;
1104 pool->sp_lastcreatetime = time_uptime;
1105 mtx_unlock(&pool->sp_lock);
1106 svc_new_thread(pool);
1107 mtx_lock(&pool->sp_lock);
1112 * Check for idle transports once per second.
1114 if (time_uptime > pool->sp_lastidlecheck) {
1115 pool->sp_lastidlecheck = time_uptime;
1116 svc_checkidle(pool);
1122 * Enforce maxthreads count.
1124 if (pool->sp_threadcount > pool->sp_maxthreads)
1128 * Before sleeping, see if we can find an
1129 * active transport which isn't being serviced
1132 if (svc_request_space_available(pool) &&
1133 (xprt = TAILQ_FIRST(&pool->sp_active)) != NULL) {
1134 TAILQ_REMOVE(&pool->sp_active, xprt, xp_alink);
1136 xprt->xp_thread = st;
1141 LIST_INSERT_HEAD(&pool->sp_idlethreads, st, st_ilink);
1142 if (ismaster || (!ismaster &&
1143 pool->sp_threadcount > pool->sp_minthreads))
1144 error = cv_timedwait_sig(&st->st_cond,
1145 &pool->sp_lock, 5 * hz);
1147 error = cv_wait_sig(&st->st_cond,
1149 if (st->st_xprt == NULL)
1150 LIST_REMOVE(st, st_ilink);
1153 * Reduce worker thread count when idle.
1155 if (error == EWOULDBLOCK) {
1157 && (pool->sp_threadcount
1158 > pool->sp_minthreads)
1162 mtx_unlock(&pool->sp_lock);
1164 mtx_lock(&pool->sp_lock);
1169 mtx_unlock(&pool->sp_lock);
1172 * Drain the transport socket and queue up any RPCs.
1174 xprt->xp_lastactive = time_uptime;
1176 if (!svc_request_space_available(pool))
1179 stat = svc_getreq(xprt, &rqstp);
1181 svc_change_space_used(pool, rqstp->rq_size);
1183 * See if the application has a preference
1184 * for some other thread.
1186 if (pool->sp_assign) {
1187 stpref = pool->sp_assign(st, rqstp);
1188 rqstp->rq_thread = stpref;
1189 STAILQ_INSERT_TAIL(&stpref->st_reqs,
1191 mtx_unlock(&stpref->st_lock);
1195 rqstp->rq_thread = st;
1196 STAILQ_INSERT_TAIL(&st->st_reqs,
1200 } while (rqstp == NULL && stat == XPRT_MOREREQS
1201 && pool->sp_state != SVCPOOL_CLOSING);
1204 * Move this transport to the end of the active list to
1205 * ensure fairness when multiple transports are active.
1206 * If this was the last queued request, svc_getreq will end
1207 * up calling xprt_inactive to remove from the active list.
1209 mtx_lock(&pool->sp_lock);
1210 xprt->xp_thread = NULL;
1212 if (xprt->xp_active) {
1213 if (!svc_request_space_available(pool) ||
1214 !xprt_assignthread(xprt))
1215 TAILQ_INSERT_TAIL(&pool->sp_active,
1218 mtx_unlock(&pool->sp_lock);
1222 * Execute what we have queued.
1225 mtx_lock(&st->st_lock);
1226 while ((rqstp = STAILQ_FIRST(&st->st_reqs)) != NULL) {
1227 STAILQ_REMOVE_HEAD(&st->st_reqs, rq_link);
1228 mtx_unlock(&st->st_lock);
1229 sz += rqstp->rq_size;
1230 svc_executereq(rqstp);
1231 mtx_lock(&st->st_lock);
1233 mtx_unlock(&st->st_lock);
1234 svc_change_space_used(pool, -sz);
1235 mtx_lock(&pool->sp_lock);
1244 KASSERT(STAILQ_EMPTY(&st->st_reqs), ("stray reqs on exit"));
1245 LIST_REMOVE(st, st_link);
1246 pool->sp_threadcount--;
1248 mtx_unlock(&pool->sp_lock);
1250 mtx_destroy(&st->st_lock);
1251 cv_destroy(&st->st_cond);
1252 mem_free(st, sizeof(*st));
1259 svc_thread_start(void *arg)
1262 svc_run_internal((SVCPOOL *) arg, FALSE);
1267 svc_new_thread(SVCPOOL *pool)
1271 pool->sp_threadcount++;
1272 kthread_add(svc_thread_start, pool,
1273 pool->sp_proc, &td, 0, 0,
1274 "%s: service", pool->sp_name);
1278 svc_run(SVCPOOL *pool)
1286 snprintf(td->td_name, sizeof(td->td_name),
1287 "%s: master", pool->sp_name);
1288 pool->sp_state = SVCPOOL_ACTIVE;
1290 pool->sp_lastcreatetime = time_uptime;
1291 pool->sp_threadcount = 1;
1293 for (i = 1; i < pool->sp_minthreads; i++) {
1294 svc_new_thread(pool);
1297 svc_run_internal(pool, TRUE);
1299 mtx_lock(&pool->sp_lock);
1300 while (pool->sp_threadcount > 0)
1301 msleep(pool, &pool->sp_lock, 0, "svcexit", 0);
1302 mtx_unlock(&pool->sp_lock);
1306 svc_exit(SVCPOOL *pool)
1310 mtx_lock(&pool->sp_lock);
1312 if (pool->sp_state != SVCPOOL_CLOSING) {
1313 pool->sp_state = SVCPOOL_CLOSING;
1314 LIST_FOREACH(st, &pool->sp_idlethreads, st_ilink)
1315 cv_signal(&st->st_cond);
1318 mtx_unlock(&pool->sp_lock);
1322 svc_getargs(struct svc_req *rqstp, xdrproc_t xargs, void *args)
1329 rqstp->rq_args = NULL;
1331 xdrmbuf_create(&xdrs, m, XDR_DECODE);
1332 stat = xargs(&xdrs, args);
1339 svc_freeargs(struct svc_req *rqstp, xdrproc_t xargs, void *args)
1343 if (rqstp->rq_addr) {
1344 free(rqstp->rq_addr, M_SONAME);
1345 rqstp->rq_addr = NULL;
1348 xdrs.x_op = XDR_FREE;
1349 return (xargs(&xdrs, args));
1353 svc_freereq(struct svc_req *rqstp)
1358 st = rqstp->rq_thread;
1362 pool->sp_done(st, rqstp);
1365 if (rqstp->rq_auth.svc_ah_ops)
1366 SVCAUTH_RELEASE(&rqstp->rq_auth);
1368 if (rqstp->rq_xprt) {
1369 SVC_RELEASE(rqstp->rq_xprt);
1373 free(rqstp->rq_addr, M_SONAME);
1376 m_freem(rqstp->rq_args);