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>
61 #include <sys/ucred.h>
64 #include <rpc/rpcb_clnt.h>
65 #include <rpc/replay.h>
67 #include <rpc/rpc_com.h>
69 #define SVC_VERSQUIET 0x0001 /* keep quiet about vers mismatch */
70 #define version_keepquiet(xp) (SVC_EXT(xp)->xp_flags & SVC_VERSQUIET)
72 static struct svc_callout *svc_find(SVCPOOL *pool, rpcprog_t, rpcvers_t,
74 static void svc_new_thread(SVCGROUP *grp);
75 static void xprt_unregister_locked(SVCXPRT *xprt);
76 static void svc_change_space_used(SVCPOOL *pool, int delta);
77 static bool_t svc_request_space_available(SVCPOOL *pool);
79 /* *************** SVCXPRT related stuff **************** */
81 static int svcpool_minthread_sysctl(SYSCTL_HANDLER_ARGS);
82 static int svcpool_maxthread_sysctl(SYSCTL_HANDLER_ARGS);
83 static int svcpool_threads_sysctl(SYSCTL_HANDLER_ARGS);
86 svcpool_create(const char *name, struct sysctl_oid_list *sysctl_base)
92 pool = malloc(sizeof(SVCPOOL), M_RPC, M_WAITOK|M_ZERO);
94 mtx_init(&pool->sp_lock, "sp_lock", NULL, MTX_DEF);
96 pool->sp_state = SVCPOOL_INIT;
98 TAILQ_INIT(&pool->sp_callouts);
99 TAILQ_INIT(&pool->sp_lcallouts);
100 pool->sp_minthreads = 1;
101 pool->sp_maxthreads = 1;
102 pool->sp_groupcount = 1;
103 for (g = 0; g < SVC_MAXGROUPS; g++) {
104 grp = &pool->sp_groups[g];
105 mtx_init(&grp->sg_lock, "sg_lock", NULL, MTX_DEF);
107 grp->sg_state = SVCPOOL_ACTIVE;
108 TAILQ_INIT(&grp->sg_xlist);
109 TAILQ_INIT(&grp->sg_active);
110 LIST_INIT(&grp->sg_idlethreads);
111 grp->sg_minthreads = 1;
112 grp->sg_maxthreads = 1;
116 * Don't use more than a quarter of mbuf clusters or more than
117 * 45Mb buffering requests.
119 pool->sp_space_high = nmbclusters * MCLBYTES / 4;
120 if (pool->sp_space_high > 45 << 20)
121 pool->sp_space_high = 45 << 20;
122 pool->sp_space_low = 2 * pool->sp_space_high / 3;
124 sysctl_ctx_init(&pool->sp_sysctl);
126 SYSCTL_ADD_PROC(&pool->sp_sysctl, sysctl_base, OID_AUTO,
127 "minthreads", CTLTYPE_INT | CTLFLAG_RW,
128 pool, 0, svcpool_minthread_sysctl, "I",
129 "Minimal number of threads");
130 SYSCTL_ADD_PROC(&pool->sp_sysctl, sysctl_base, OID_AUTO,
131 "maxthreads", CTLTYPE_INT | CTLFLAG_RW,
132 pool, 0, svcpool_maxthread_sysctl, "I",
133 "Maximal number of threads");
134 SYSCTL_ADD_PROC(&pool->sp_sysctl, sysctl_base, OID_AUTO,
135 "threads", CTLTYPE_INT | CTLFLAG_RD,
136 pool, 0, svcpool_threads_sysctl, "I",
137 "Current number of threads");
138 SYSCTL_ADD_INT(&pool->sp_sysctl, sysctl_base, OID_AUTO,
139 "groups", CTLFLAG_RD, &pool->sp_groupcount, 0,
140 "Number of thread groups");
142 SYSCTL_ADD_UINT(&pool->sp_sysctl, sysctl_base, OID_AUTO,
143 "request_space_used", CTLFLAG_RD,
144 &pool->sp_space_used, 0,
145 "Space in parsed but not handled requests.");
147 SYSCTL_ADD_UINT(&pool->sp_sysctl, sysctl_base, OID_AUTO,
148 "request_space_used_highest", CTLFLAG_RD,
149 &pool->sp_space_used_highest, 0,
150 "Highest space used since reboot.");
152 SYSCTL_ADD_UINT(&pool->sp_sysctl, sysctl_base, OID_AUTO,
153 "request_space_high", CTLFLAG_RW,
154 &pool->sp_space_high, 0,
155 "Maximum space in parsed but not handled requests.");
157 SYSCTL_ADD_UINT(&pool->sp_sysctl, sysctl_base, OID_AUTO,
158 "request_space_low", CTLFLAG_RW,
159 &pool->sp_space_low, 0,
160 "Low water mark for request space.");
162 SYSCTL_ADD_INT(&pool->sp_sysctl, sysctl_base, OID_AUTO,
163 "request_space_throttled", CTLFLAG_RD,
164 &pool->sp_space_throttled, 0,
165 "Whether nfs requests are currently throttled");
167 SYSCTL_ADD_INT(&pool->sp_sysctl, sysctl_base, OID_AUTO,
168 "request_space_throttle_count", CTLFLAG_RD,
169 &pool->sp_space_throttle_count, 0,
170 "Count of times throttling based on request space has occurred");
177 svcpool_destroy(SVCPOOL *pool)
180 SVCXPRT *xprt, *nxprt;
181 struct svc_callout *s;
182 struct svc_loss_callout *sl;
183 struct svcxprt_list cleanup;
186 TAILQ_INIT(&cleanup);
188 for (g = 0; g < SVC_MAXGROUPS; g++) {
189 grp = &pool->sp_groups[g];
190 mtx_lock(&grp->sg_lock);
191 while ((xprt = TAILQ_FIRST(&grp->sg_xlist)) != NULL) {
192 xprt_unregister_locked(xprt);
193 TAILQ_INSERT_TAIL(&cleanup, xprt, xp_link);
195 mtx_unlock(&grp->sg_lock);
197 TAILQ_FOREACH_SAFE(xprt, &cleanup, xp_link, nxprt) {
201 mtx_lock(&pool->sp_lock);
202 while ((s = TAILQ_FIRST(&pool->sp_callouts)) != NULL) {
203 mtx_unlock(&pool->sp_lock);
204 svc_unreg(pool, s->sc_prog, s->sc_vers);
205 mtx_lock(&pool->sp_lock);
207 while ((sl = TAILQ_FIRST(&pool->sp_lcallouts)) != NULL) {
208 mtx_unlock(&pool->sp_lock);
209 svc_loss_unreg(pool, sl->slc_dispatch);
210 mtx_lock(&pool->sp_lock);
212 mtx_unlock(&pool->sp_lock);
214 for (g = 0; g < SVC_MAXGROUPS; g++) {
215 grp = &pool->sp_groups[g];
216 mtx_destroy(&grp->sg_lock);
218 mtx_destroy(&pool->sp_lock);
221 replay_freecache(pool->sp_rcache);
223 sysctl_ctx_free(&pool->sp_sysctl);
228 * Sysctl handler to get the present thread count on a pool
231 svcpool_threads_sysctl(SYSCTL_HANDLER_ARGS)
234 int threads, error, g;
236 pool = oidp->oid_arg1;
238 mtx_lock(&pool->sp_lock);
239 for (g = 0; g < pool->sp_groupcount; g++)
240 threads += pool->sp_groups[g].sg_threadcount;
241 mtx_unlock(&pool->sp_lock);
242 error = sysctl_handle_int(oidp, &threads, 0, req);
247 * Sysctl handler to set the minimum thread count on a pool
250 svcpool_minthread_sysctl(SYSCTL_HANDLER_ARGS)
253 int newminthreads, error, g;
255 pool = oidp->oid_arg1;
256 newminthreads = pool->sp_minthreads;
257 error = sysctl_handle_int(oidp, &newminthreads, 0, req);
258 if (error == 0 && newminthreads != pool->sp_minthreads) {
259 if (newminthreads > pool->sp_maxthreads)
261 mtx_lock(&pool->sp_lock);
262 pool->sp_minthreads = newminthreads;
263 for (g = 0; g < pool->sp_groupcount; g++) {
264 pool->sp_groups[g].sg_minthreads = max(1,
265 pool->sp_minthreads / pool->sp_groupcount);
267 mtx_unlock(&pool->sp_lock);
273 * Sysctl handler to set the maximum thread count on a pool
276 svcpool_maxthread_sysctl(SYSCTL_HANDLER_ARGS)
279 int newmaxthreads, error, g;
281 pool = oidp->oid_arg1;
282 newmaxthreads = pool->sp_maxthreads;
283 error = sysctl_handle_int(oidp, &newmaxthreads, 0, req);
284 if (error == 0 && newmaxthreads != pool->sp_maxthreads) {
285 if (newmaxthreads < pool->sp_minthreads)
287 mtx_lock(&pool->sp_lock);
288 pool->sp_maxthreads = newmaxthreads;
289 for (g = 0; g < pool->sp_groupcount; g++) {
290 pool->sp_groups[g].sg_maxthreads = max(1,
291 pool->sp_maxthreads / pool->sp_groupcount);
293 mtx_unlock(&pool->sp_lock);
299 * Activate a transport handle.
302 xprt_register(SVCXPRT *xprt)
304 SVCPOOL *pool = xprt->xp_pool;
309 g = atomic_fetchadd_int(&pool->sp_nextgroup, 1) % pool->sp_groupcount;
310 xprt->xp_group = grp = &pool->sp_groups[g];
311 mtx_lock(&grp->sg_lock);
312 xprt->xp_registered = TRUE;
313 xprt->xp_active = FALSE;
314 TAILQ_INSERT_TAIL(&grp->sg_xlist, xprt, xp_link);
315 mtx_unlock(&grp->sg_lock);
319 * De-activate a transport handle. Note: the locked version doesn't
320 * release the transport - caller must do that after dropping the pool
324 xprt_unregister_locked(SVCXPRT *xprt)
326 SVCGROUP *grp = xprt->xp_group;
328 mtx_assert(&grp->sg_lock, MA_OWNED);
329 KASSERT(xprt->xp_registered == TRUE,
330 ("xprt_unregister_locked: not registered"));
331 xprt_inactive_locked(xprt);
332 TAILQ_REMOVE(&grp->sg_xlist, xprt, xp_link);
333 xprt->xp_registered = FALSE;
337 xprt_unregister(SVCXPRT *xprt)
339 SVCGROUP *grp = xprt->xp_group;
341 mtx_lock(&grp->sg_lock);
342 if (xprt->xp_registered == FALSE) {
343 /* Already unregistered by another thread */
344 mtx_unlock(&grp->sg_lock);
347 xprt_unregister_locked(xprt);
348 mtx_unlock(&grp->sg_lock);
354 * Attempt to assign a service thread to this transport.
357 xprt_assignthread(SVCXPRT *xprt)
359 SVCGROUP *grp = xprt->xp_group;
362 mtx_assert(&grp->sg_lock, MA_OWNED);
363 st = LIST_FIRST(&grp->sg_idlethreads);
365 LIST_REMOVE(st, st_ilink);
367 xprt->xp_thread = st;
369 cv_signal(&st->st_cond);
373 * See if we can create a new thread. The
374 * actual thread creation happens in
375 * svc_run_internal because our locking state
376 * is poorly defined (we are typically called
377 * from a socket upcall). Don't create more
378 * than one thread per second.
380 if (grp->sg_state == SVCPOOL_ACTIVE
381 && grp->sg_lastcreatetime < time_uptime
382 && grp->sg_threadcount < grp->sg_maxthreads) {
383 grp->sg_state = SVCPOOL_THREADWANTED;
390 xprt_active(SVCXPRT *xprt)
392 SVCGROUP *grp = xprt->xp_group;
394 mtx_lock(&grp->sg_lock);
396 if (!xprt->xp_registered) {
398 * Race with xprt_unregister - we lose.
400 mtx_unlock(&grp->sg_lock);
404 if (!xprt->xp_active) {
405 xprt->xp_active = TRUE;
406 if (xprt->xp_thread == NULL) {
407 if (!svc_request_space_available(xprt->xp_pool) ||
408 !xprt_assignthread(xprt))
409 TAILQ_INSERT_TAIL(&grp->sg_active, xprt,
414 mtx_unlock(&grp->sg_lock);
418 xprt_inactive_locked(SVCXPRT *xprt)
420 SVCGROUP *grp = xprt->xp_group;
422 mtx_assert(&grp->sg_lock, MA_OWNED);
423 if (xprt->xp_active) {
424 if (xprt->xp_thread == NULL)
425 TAILQ_REMOVE(&grp->sg_active, xprt, xp_alink);
426 xprt->xp_active = FALSE;
431 xprt_inactive(SVCXPRT *xprt)
433 SVCGROUP *grp = xprt->xp_group;
435 mtx_lock(&grp->sg_lock);
436 xprt_inactive_locked(xprt);
437 mtx_unlock(&grp->sg_lock);
441 * Variant of xprt_inactive() for use only when sure that port is
442 * assigned to thread. For example, withing receive handlers.
445 xprt_inactive_self(SVCXPRT *xprt)
448 KASSERT(xprt->xp_thread != NULL,
449 ("xprt_inactive_self(%p) with NULL xp_thread", xprt));
450 xprt->xp_active = FALSE;
454 * Add a service program to the callout list.
455 * The dispatch routine will be called when a rpc request for this
456 * program number comes in.
459 svc_reg(SVCXPRT *xprt, const rpcprog_t prog, const rpcvers_t vers,
460 void (*dispatch)(struct svc_req *, SVCXPRT *),
461 const struct netconfig *nconf)
463 SVCPOOL *pool = xprt->xp_pool;
464 struct svc_callout *s;
468 /* VARIABLES PROTECTED BY svc_lock: s, svc_head */
470 if (xprt->xp_netid) {
471 netid = strdup(xprt->xp_netid, M_RPC);
473 } else if (nconf && nconf->nc_netid) {
474 netid = strdup(nconf->nc_netid, M_RPC);
476 } /* must have been created with svc_raw_create */
477 if ((netid == NULL) && (flag == 1)) {
481 mtx_lock(&pool->sp_lock);
482 if ((s = svc_find(pool, prog, vers, netid)) != NULL) {
485 if (s->sc_dispatch == dispatch)
486 goto rpcb_it; /* he is registering another xptr */
487 mtx_unlock(&pool->sp_lock);
490 s = malloc(sizeof (struct svc_callout), M_RPC, M_NOWAIT);
494 mtx_unlock(&pool->sp_lock);
500 s->sc_dispatch = dispatch;
502 TAILQ_INSERT_TAIL(&pool->sp_callouts, s, sc_link);
504 if ((xprt->xp_netid == NULL) && (flag == 1) && netid)
505 ((SVCXPRT *) xprt)->xp_netid = strdup(netid, M_RPC);
508 mtx_unlock(&pool->sp_lock);
509 /* now register the information with the local binder service */
512 struct netconfig tnc;
515 nb.buf = &xprt->xp_ltaddr;
516 nb.len = xprt->xp_ltaddr.ss_len;
517 dummy = rpcb_set(prog, vers, &tnc, &nb);
524 * Remove a service program from the callout list.
527 svc_unreg(SVCPOOL *pool, const rpcprog_t prog, const rpcvers_t vers)
529 struct svc_callout *s;
531 /* unregister the information anyway */
532 (void) rpcb_unset(prog, vers, NULL);
533 mtx_lock(&pool->sp_lock);
534 while ((s = svc_find(pool, prog, vers, NULL)) != NULL) {
535 TAILQ_REMOVE(&pool->sp_callouts, s, sc_link);
537 mem_free(s->sc_netid, sizeof (s->sc_netid) + 1);
538 mem_free(s, sizeof (struct svc_callout));
540 mtx_unlock(&pool->sp_lock);
544 * Add a service connection loss program to the callout list.
545 * The dispatch routine will be called when some port in ths pool die.
548 svc_loss_reg(SVCXPRT *xprt, void (*dispatch)(SVCXPRT *))
550 SVCPOOL *pool = xprt->xp_pool;
551 struct svc_loss_callout *s;
553 mtx_lock(&pool->sp_lock);
554 TAILQ_FOREACH(s, &pool->sp_lcallouts, slc_link) {
555 if (s->slc_dispatch == dispatch)
559 mtx_unlock(&pool->sp_lock);
562 s = malloc(sizeof(struct svc_loss_callout), M_RPC, M_NOWAIT);
564 mtx_unlock(&pool->sp_lock);
567 s->slc_dispatch = dispatch;
568 TAILQ_INSERT_TAIL(&pool->sp_lcallouts, s, slc_link);
569 mtx_unlock(&pool->sp_lock);
574 * Remove a service connection loss program from the callout list.
577 svc_loss_unreg(SVCPOOL *pool, void (*dispatch)(SVCXPRT *))
579 struct svc_loss_callout *s;
581 mtx_lock(&pool->sp_lock);
582 TAILQ_FOREACH(s, &pool->sp_lcallouts, slc_link) {
583 if (s->slc_dispatch == dispatch) {
584 TAILQ_REMOVE(&pool->sp_lcallouts, s, slc_link);
589 mtx_unlock(&pool->sp_lock);
592 /* ********************** CALLOUT list related stuff ************* */
595 * Search the callout list for a program number, return the callout
598 static struct svc_callout *
599 svc_find(SVCPOOL *pool, rpcprog_t prog, rpcvers_t vers, char *netid)
601 struct svc_callout *s;
603 mtx_assert(&pool->sp_lock, MA_OWNED);
604 TAILQ_FOREACH(s, &pool->sp_callouts, sc_link) {
605 if (s->sc_prog == prog && s->sc_vers == vers
606 && (netid == NULL || s->sc_netid == NULL ||
607 strcmp(netid, s->sc_netid) == 0))
614 /* ******************* REPLY GENERATION ROUTINES ************ */
617 svc_sendreply_common(struct svc_req *rqstp, struct rpc_msg *rply,
620 SVCXPRT *xprt = rqstp->rq_xprt;
623 if (rqstp->rq_args) {
624 m_freem(rqstp->rq_args);
625 rqstp->rq_args = NULL;
628 if (xprt->xp_pool->sp_rcache)
629 replay_setreply(xprt->xp_pool->sp_rcache,
630 rply, svc_getrpccaller(rqstp), body);
632 if (!SVCAUTH_WRAP(&rqstp->rq_auth, &body))
635 ok = SVC_REPLY(xprt, rply, rqstp->rq_addr, body, &rqstp->rq_reply_seq);
636 if (rqstp->rq_addr) {
637 free(rqstp->rq_addr, M_SONAME);
638 rqstp->rq_addr = NULL;
645 * Send a reply to an rpc request
648 svc_sendreply(struct svc_req *rqstp, xdrproc_t xdr_results, void * xdr_location)
655 rply.rm_xid = rqstp->rq_xid;
656 rply.rm_direction = REPLY;
657 rply.rm_reply.rp_stat = MSG_ACCEPTED;
658 rply.acpted_rply.ar_verf = rqstp->rq_verf;
659 rply.acpted_rply.ar_stat = SUCCESS;
660 rply.acpted_rply.ar_results.where = NULL;
661 rply.acpted_rply.ar_results.proc = (xdrproc_t) xdr_void;
663 MGET(m, M_WAIT, MT_DATA);
666 xdrmbuf_create(&xdrs, m, XDR_ENCODE);
667 ok = xdr_results(&xdrs, xdr_location);
671 return (svc_sendreply_common(rqstp, &rply, m));
679 svc_sendreply_mbuf(struct svc_req *rqstp, struct mbuf *m)
683 rply.rm_xid = rqstp->rq_xid;
684 rply.rm_direction = REPLY;
685 rply.rm_reply.rp_stat = MSG_ACCEPTED;
686 rply.acpted_rply.ar_verf = rqstp->rq_verf;
687 rply.acpted_rply.ar_stat = SUCCESS;
688 rply.acpted_rply.ar_results.where = NULL;
689 rply.acpted_rply.ar_results.proc = (xdrproc_t) xdr_void;
691 return (svc_sendreply_common(rqstp, &rply, m));
695 * No procedure error reply
698 svcerr_noproc(struct svc_req *rqstp)
700 SVCXPRT *xprt = rqstp->rq_xprt;
703 rply.rm_xid = rqstp->rq_xid;
704 rply.rm_direction = REPLY;
705 rply.rm_reply.rp_stat = MSG_ACCEPTED;
706 rply.acpted_rply.ar_verf = rqstp->rq_verf;
707 rply.acpted_rply.ar_stat = PROC_UNAVAIL;
709 if (xprt->xp_pool->sp_rcache)
710 replay_setreply(xprt->xp_pool->sp_rcache,
711 &rply, svc_getrpccaller(rqstp), NULL);
713 svc_sendreply_common(rqstp, &rply, NULL);
717 * Can't decode args error reply
720 svcerr_decode(struct svc_req *rqstp)
722 SVCXPRT *xprt = rqstp->rq_xprt;
725 rply.rm_xid = rqstp->rq_xid;
726 rply.rm_direction = REPLY;
727 rply.rm_reply.rp_stat = MSG_ACCEPTED;
728 rply.acpted_rply.ar_verf = rqstp->rq_verf;
729 rply.acpted_rply.ar_stat = GARBAGE_ARGS;
731 if (xprt->xp_pool->sp_rcache)
732 replay_setreply(xprt->xp_pool->sp_rcache,
733 &rply, (struct sockaddr *) &xprt->xp_rtaddr, NULL);
735 svc_sendreply_common(rqstp, &rply, NULL);
742 svcerr_systemerr(struct svc_req *rqstp)
744 SVCXPRT *xprt = rqstp->rq_xprt;
747 rply.rm_xid = rqstp->rq_xid;
748 rply.rm_direction = REPLY;
749 rply.rm_reply.rp_stat = MSG_ACCEPTED;
750 rply.acpted_rply.ar_verf = rqstp->rq_verf;
751 rply.acpted_rply.ar_stat = SYSTEM_ERR;
753 if (xprt->xp_pool->sp_rcache)
754 replay_setreply(xprt->xp_pool->sp_rcache,
755 &rply, svc_getrpccaller(rqstp), NULL);
757 svc_sendreply_common(rqstp, &rply, NULL);
761 * Authentication error reply
764 svcerr_auth(struct svc_req *rqstp, enum auth_stat why)
766 SVCXPRT *xprt = rqstp->rq_xprt;
769 rply.rm_xid = rqstp->rq_xid;
770 rply.rm_direction = REPLY;
771 rply.rm_reply.rp_stat = MSG_DENIED;
772 rply.rjcted_rply.rj_stat = AUTH_ERROR;
773 rply.rjcted_rply.rj_why = why;
775 if (xprt->xp_pool->sp_rcache)
776 replay_setreply(xprt->xp_pool->sp_rcache,
777 &rply, svc_getrpccaller(rqstp), NULL);
779 svc_sendreply_common(rqstp, &rply, NULL);
783 * Auth too weak error reply
786 svcerr_weakauth(struct svc_req *rqstp)
789 svcerr_auth(rqstp, AUTH_TOOWEAK);
793 * Program unavailable error reply
796 svcerr_noprog(struct svc_req *rqstp)
798 SVCXPRT *xprt = rqstp->rq_xprt;
801 rply.rm_xid = rqstp->rq_xid;
802 rply.rm_direction = REPLY;
803 rply.rm_reply.rp_stat = MSG_ACCEPTED;
804 rply.acpted_rply.ar_verf = rqstp->rq_verf;
805 rply.acpted_rply.ar_stat = PROG_UNAVAIL;
807 if (xprt->xp_pool->sp_rcache)
808 replay_setreply(xprt->xp_pool->sp_rcache,
809 &rply, svc_getrpccaller(rqstp), NULL);
811 svc_sendreply_common(rqstp, &rply, NULL);
815 * Program version mismatch error reply
818 svcerr_progvers(struct svc_req *rqstp, rpcvers_t low_vers, rpcvers_t high_vers)
820 SVCXPRT *xprt = rqstp->rq_xprt;
823 rply.rm_xid = rqstp->rq_xid;
824 rply.rm_direction = REPLY;
825 rply.rm_reply.rp_stat = MSG_ACCEPTED;
826 rply.acpted_rply.ar_verf = rqstp->rq_verf;
827 rply.acpted_rply.ar_stat = PROG_MISMATCH;
828 rply.acpted_rply.ar_vers.low = (uint32_t)low_vers;
829 rply.acpted_rply.ar_vers.high = (uint32_t)high_vers;
831 if (xprt->xp_pool->sp_rcache)
832 replay_setreply(xprt->xp_pool->sp_rcache,
833 &rply, svc_getrpccaller(rqstp), NULL);
835 svc_sendreply_common(rqstp, &rply, NULL);
839 * Allocate a new server transport structure. All fields are
840 * initialized to zero and xp_p3 is initialized to point at an
841 * extension structure to hold various flags and authentication
850 xprt = mem_alloc(sizeof(SVCXPRT));
851 ext = mem_alloc(sizeof(SVCXPRT_EXT));
853 refcount_init(&xprt->xp_refs, 1);
859 * Free a server transport structure.
862 svc_xprt_free(SVCXPRT *xprt)
865 mem_free(xprt->xp_p3, sizeof(SVCXPRT_EXT));
866 mem_free(xprt, sizeof(SVCXPRT));
869 /* ******************* SERVER INPUT STUFF ******************* */
872 * Read RPC requests from a transport and queue them to be
873 * executed. We handle authentication and replay cache replies here.
874 * Actually dispatching the RPC is deferred till svc_executereq.
876 static enum xprt_stat
877 svc_getreq(SVCXPRT *xprt, struct svc_req **rqstp_ret)
879 SVCPOOL *pool = xprt->xp_pool;
883 struct svc_loss_callout *s;
886 /* now receive msgs from xprtprt (support batch calls) */
887 r = malloc(sizeof(*r), M_RPC, M_WAITOK|M_ZERO);
889 msg.rm_call.cb_cred.oa_base = r->rq_credarea;
890 msg.rm_call.cb_verf.oa_base = &r->rq_credarea[MAX_AUTH_BYTES];
891 r->rq_clntcred = &r->rq_credarea[2*MAX_AUTH_BYTES];
892 if (SVC_RECV(xprt, &msg, &r->rq_addr, &args)) {
896 * Handle replays and authenticate before queuing the
897 * request to be executed.
901 if (pool->sp_rcache) {
902 struct rpc_msg repmsg;
903 struct mbuf *repbody;
904 enum replay_state rs;
905 rs = replay_find(pool->sp_rcache, &msg,
906 svc_getrpccaller(r), &repmsg, &repbody);
911 SVC_REPLY(xprt, &repmsg, r->rq_addr,
912 repbody, &r->rq_reply_seq);
914 free(r->rq_addr, M_SONAME);
926 r->rq_xid = msg.rm_xid;
927 r->rq_prog = msg.rm_call.cb_prog;
928 r->rq_vers = msg.rm_call.cb_vers;
929 r->rq_proc = msg.rm_call.cb_proc;
930 r->rq_size = sizeof(*r) + m_length(args, NULL);
932 if ((why = _authenticate(r, &msg)) != AUTH_OK) {
934 * RPCSEC_GSS uses this return code
935 * for requests that form part of its
936 * context establishment protocol and
937 * should not be dispatched to the
940 if (why != RPCSEC_GSS_NODISPATCH)
945 if (!SVCAUTH_UNWRAP(&r->rq_auth, &r->rq_args)) {
951 * Everything checks out, return request to caller.
961 if ((stat = SVC_STAT(xprt)) == XPRT_DIED) {
962 TAILQ_FOREACH(s, &pool->sp_lcallouts, slc_link)
963 (*s->slc_dispatch)(xprt);
964 xprt_unregister(xprt);
971 svc_executereq(struct svc_req *rqstp)
973 SVCXPRT *xprt = rqstp->rq_xprt;
974 SVCPOOL *pool = xprt->xp_pool;
978 struct svc_callout *s;
980 /* now match message with a registered service*/
982 low_vers = (rpcvers_t) -1L;
983 high_vers = (rpcvers_t) 0L;
984 TAILQ_FOREACH(s, &pool->sp_callouts, sc_link) {
985 if (s->sc_prog == rqstp->rq_prog) {
986 if (s->sc_vers == rqstp->rq_vers) {
988 * We hand ownership of r to the
989 * dispatch method - they must call
992 (*s->sc_dispatch)(rqstp, xprt);
994 } /* found correct version */
996 if (s->sc_vers < low_vers)
997 low_vers = s->sc_vers;
998 if (s->sc_vers > high_vers)
999 high_vers = s->sc_vers;
1000 } /* found correct program */
1004 * if we got here, the program or version
1008 svcerr_progvers(rqstp, low_vers, high_vers);
1010 svcerr_noprog(rqstp);
1016 svc_checkidle(SVCGROUP *grp)
1018 SVCXPRT *xprt, *nxprt;
1020 struct svcxprt_list cleanup;
1022 TAILQ_INIT(&cleanup);
1023 TAILQ_FOREACH_SAFE(xprt, &grp->sg_xlist, xp_link, nxprt) {
1025 * Only some transports have idle timers. Don't time
1026 * something out which is just waking up.
1028 if (!xprt->xp_idletimeout || xprt->xp_thread)
1031 timo = xprt->xp_lastactive + xprt->xp_idletimeout;
1032 if (time_uptime > timo) {
1033 xprt_unregister_locked(xprt);
1034 TAILQ_INSERT_TAIL(&cleanup, xprt, xp_link);
1038 mtx_unlock(&grp->sg_lock);
1039 TAILQ_FOREACH_SAFE(xprt, &cleanup, xp_link, nxprt) {
1042 mtx_lock(&grp->sg_lock);
1046 svc_assign_waiting_sockets(SVCPOOL *pool)
1052 for (g = 0; g < pool->sp_groupcount; g++) {
1053 grp = &pool->sp_groups[g];
1054 mtx_lock(&grp->sg_lock);
1055 while ((xprt = TAILQ_FIRST(&grp->sg_active)) != NULL) {
1056 if (xprt_assignthread(xprt))
1057 TAILQ_REMOVE(&grp->sg_active, xprt, xp_alink);
1061 mtx_unlock(&grp->sg_lock);
1066 svc_change_space_used(SVCPOOL *pool, int delta)
1070 value = atomic_fetchadd_int(&pool->sp_space_used, delta) + delta;
1072 if (value >= pool->sp_space_high && !pool->sp_space_throttled) {
1073 pool->sp_space_throttled = TRUE;
1074 pool->sp_space_throttle_count++;
1076 if (value > pool->sp_space_used_highest)
1077 pool->sp_space_used_highest = value;
1079 if (value < pool->sp_space_low && pool->sp_space_throttled) {
1080 pool->sp_space_throttled = FALSE;
1081 svc_assign_waiting_sockets(pool);
1087 svc_request_space_available(SVCPOOL *pool)
1090 if (pool->sp_space_throttled)
1096 svc_run_internal(SVCGROUP *grp, bool_t ismaster)
1098 SVCPOOL *pool = grp->sg_pool;
1099 SVCTHREAD *st, *stpref;
1101 enum xprt_stat stat;
1102 struct svc_req *rqstp;
1106 st = mem_alloc(sizeof(*st));
1107 mtx_init(&st->st_lock, "st_lock", NULL, MTX_DEF);
1110 STAILQ_INIT(&st->st_reqs);
1111 cv_init(&st->st_cond, "rpcsvc");
1113 mtx_lock(&grp->sg_lock);
1116 * If we are a new thread which was spawned to cope with
1117 * increased load, set the state back to SVCPOOL_ACTIVE.
1119 if (grp->sg_state == SVCPOOL_THREADSTARTING)
1120 grp->sg_state = SVCPOOL_ACTIVE;
1122 while (grp->sg_state != SVCPOOL_CLOSING) {
1124 * Create new thread if requested.
1126 if (grp->sg_state == SVCPOOL_THREADWANTED) {
1127 grp->sg_state = SVCPOOL_THREADSTARTING;
1128 grp->sg_lastcreatetime = time_uptime;
1129 mtx_unlock(&grp->sg_lock);
1130 svc_new_thread(grp);
1131 mtx_lock(&grp->sg_lock);
1136 * Check for idle transports once per second.
1138 if (time_uptime > grp->sg_lastidlecheck) {
1139 grp->sg_lastidlecheck = time_uptime;
1146 * Enforce maxthreads count.
1148 if (grp->sg_threadcount > grp->sg_maxthreads)
1152 * Before sleeping, see if we can find an
1153 * active transport which isn't being serviced
1156 if (svc_request_space_available(pool) &&
1157 (xprt = TAILQ_FIRST(&grp->sg_active)) != NULL) {
1158 TAILQ_REMOVE(&grp->sg_active, xprt, xp_alink);
1160 xprt->xp_thread = st;
1165 LIST_INSERT_HEAD(&grp->sg_idlethreads, st, st_ilink);
1166 if (ismaster || (!ismaster &&
1167 grp->sg_threadcount > grp->sg_minthreads))
1168 error = cv_timedwait_sig(&st->st_cond,
1169 &grp->sg_lock, 5 * hz);
1171 error = cv_wait_sig(&st->st_cond,
1173 if (st->st_xprt == NULL)
1174 LIST_REMOVE(st, st_ilink);
1177 * Reduce worker thread count when idle.
1179 if (error == EWOULDBLOCK) {
1181 && (grp->sg_threadcount
1182 > grp->sg_minthreads)
1186 mtx_unlock(&grp->sg_lock);
1188 mtx_lock(&grp->sg_lock);
1193 mtx_unlock(&grp->sg_lock);
1196 * Drain the transport socket and queue up any RPCs.
1198 xprt->xp_lastactive = time_uptime;
1200 if (!svc_request_space_available(pool))
1203 stat = svc_getreq(xprt, &rqstp);
1205 svc_change_space_used(pool, rqstp->rq_size);
1207 * See if the application has a preference
1208 * for some other thread.
1210 if (pool->sp_assign) {
1211 stpref = pool->sp_assign(st, rqstp);
1212 rqstp->rq_thread = stpref;
1213 STAILQ_INSERT_TAIL(&stpref->st_reqs,
1215 mtx_unlock(&stpref->st_lock);
1219 rqstp->rq_thread = st;
1220 STAILQ_INSERT_TAIL(&st->st_reqs,
1224 } while (rqstp == NULL && stat == XPRT_MOREREQS
1225 && grp->sg_state != SVCPOOL_CLOSING);
1228 * Move this transport to the end of the active list to
1229 * ensure fairness when multiple transports are active.
1230 * If this was the last queued request, svc_getreq will end
1231 * up calling xprt_inactive to remove from the active list.
1233 mtx_lock(&grp->sg_lock);
1234 xprt->xp_thread = NULL;
1236 if (xprt->xp_active) {
1237 if (!svc_request_space_available(pool) ||
1238 !xprt_assignthread(xprt))
1239 TAILQ_INSERT_TAIL(&grp->sg_active,
1242 mtx_unlock(&grp->sg_lock);
1246 * Execute what we have queued.
1249 mtx_lock(&st->st_lock);
1250 while ((rqstp = STAILQ_FIRST(&st->st_reqs)) != NULL) {
1251 STAILQ_REMOVE_HEAD(&st->st_reqs, rq_link);
1252 mtx_unlock(&st->st_lock);
1253 sz += rqstp->rq_size;
1254 svc_executereq(rqstp);
1255 mtx_lock(&st->st_lock);
1257 mtx_unlock(&st->st_lock);
1258 svc_change_space_used(pool, -sz);
1259 mtx_lock(&grp->sg_lock);
1267 KASSERT(STAILQ_EMPTY(&st->st_reqs), ("stray reqs on exit"));
1268 mtx_destroy(&st->st_lock);
1269 cv_destroy(&st->st_cond);
1270 mem_free(st, sizeof(*st));
1272 grp->sg_threadcount--;
1275 mtx_unlock(&grp->sg_lock);
1279 svc_thread_start(void *arg)
1282 svc_run_internal((SVCGROUP *) arg, FALSE);
1287 svc_new_thread(SVCGROUP *grp)
1289 SVCPOOL *pool = grp->sg_pool;
1292 mtx_lock(&grp->sg_lock);
1293 grp->sg_threadcount++;
1294 mtx_unlock(&grp->sg_lock);
1295 kthread_add(svc_thread_start, grp, pool->sp_proc, &td, 0, 0,
1296 "%s: service", pool->sp_name);
1300 svc_run(SVCPOOL *pool)
1309 snprintf(td->td_name, sizeof(td->td_name),
1310 "%s: master", pool->sp_name);
1311 pool->sp_state = SVCPOOL_ACTIVE;
1314 /* Choose group count based on number of threads and CPUs. */
1315 pool->sp_groupcount = max(1, min(SVC_MAXGROUPS,
1316 min(pool->sp_maxthreads / 2, mp_ncpus) / 6));
1317 for (g = 0; g < pool->sp_groupcount; g++) {
1318 grp = &pool->sp_groups[g];
1319 grp->sg_minthreads = max(1,
1320 pool->sp_minthreads / pool->sp_groupcount);
1321 grp->sg_maxthreads = max(1,
1322 pool->sp_maxthreads / pool->sp_groupcount);
1323 grp->sg_lastcreatetime = time_uptime;
1326 /* Starting threads */
1327 pool->sp_groups[0].sg_threadcount++;
1328 for (g = 0; g < pool->sp_groupcount; g++) {
1329 grp = &pool->sp_groups[g];
1330 for (i = ((g == 0) ? 1 : 0); i < grp->sg_minthreads; i++)
1331 svc_new_thread(grp);
1333 svc_run_internal(&pool->sp_groups[0], TRUE);
1335 /* Waiting for threads to stop. */
1336 for (g = 0; g < pool->sp_groupcount; g++) {
1337 grp = &pool->sp_groups[g];
1338 mtx_lock(&grp->sg_lock);
1339 while (grp->sg_threadcount > 0)
1340 msleep(grp, &grp->sg_lock, 0, "svcexit", 0);
1341 mtx_unlock(&grp->sg_lock);
1346 svc_exit(SVCPOOL *pool)
1352 pool->sp_state = SVCPOOL_CLOSING;
1353 for (g = 0; g < pool->sp_groupcount; g++) {
1354 grp = &pool->sp_groups[g];
1355 mtx_lock(&grp->sg_lock);
1356 if (grp->sg_state != SVCPOOL_CLOSING) {
1357 grp->sg_state = SVCPOOL_CLOSING;
1358 LIST_FOREACH(st, &grp->sg_idlethreads, st_ilink)
1359 cv_signal(&st->st_cond);
1361 mtx_unlock(&grp->sg_lock);
1366 svc_getargs(struct svc_req *rqstp, xdrproc_t xargs, void *args)
1373 rqstp->rq_args = NULL;
1375 xdrmbuf_create(&xdrs, m, XDR_DECODE);
1376 stat = xargs(&xdrs, args);
1383 svc_freeargs(struct svc_req *rqstp, xdrproc_t xargs, void *args)
1387 if (rqstp->rq_addr) {
1388 free(rqstp->rq_addr, M_SONAME);
1389 rqstp->rq_addr = NULL;
1392 xdrs.x_op = XDR_FREE;
1393 return (xargs(&xdrs, args));
1397 svc_freereq(struct svc_req *rqstp)
1402 st = rqstp->rq_thread;
1406 pool->sp_done(st, rqstp);
1409 if (rqstp->rq_auth.svc_ah_ops)
1410 SVCAUTH_RELEASE(&rqstp->rq_auth);
1412 if (rqstp->rq_xprt) {
1413 SVC_RELEASE(rqstp->rq_xprt);
1417 free(rqstp->rq_addr, M_SONAME);
1420 m_freem(rqstp->rq_args);