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);
345 xprt->xp_thread = st;
347 cv_signal(&st->st_cond);
351 * See if we can create a new thread. The
352 * actual thread creation happens in
353 * svc_run_internal because our locking state
354 * is poorly defined (we are typically called
355 * from a socket upcall). Don't create more
356 * than one thread per second.
358 if (pool->sp_state == SVCPOOL_ACTIVE
359 && pool->sp_lastcreatetime < time_uptime
360 && pool->sp_threadcount < pool->sp_maxthreads) {
361 pool->sp_state = SVCPOOL_THREADWANTED;
368 xprt_active(SVCXPRT *xprt)
370 SVCPOOL *pool = xprt->xp_pool;
372 mtx_lock(&pool->sp_lock);
374 if (!xprt->xp_registered) {
376 * Race with xprt_unregister - we lose.
378 mtx_unlock(&pool->sp_lock);
382 if (!xprt->xp_active) {
383 xprt->xp_active = TRUE;
384 if (xprt->xp_thread == NULL) {
385 if (!svc_request_space_available(pool) ||
386 !xprt_assignthread(xprt))
387 TAILQ_INSERT_TAIL(&pool->sp_active, xprt,
392 mtx_unlock(&pool->sp_lock);
396 xprt_inactive_locked(SVCXPRT *xprt)
398 SVCPOOL *pool = xprt->xp_pool;
400 mtx_assert(&pool->sp_lock, MA_OWNED);
401 if (xprt->xp_active) {
402 if (xprt->xp_thread == NULL)
403 TAILQ_REMOVE(&pool->sp_active, xprt, xp_alink);
404 xprt->xp_active = FALSE;
409 xprt_inactive(SVCXPRT *xprt)
411 SVCPOOL *pool = xprt->xp_pool;
413 mtx_lock(&pool->sp_lock);
414 xprt_inactive_locked(xprt);
415 mtx_unlock(&pool->sp_lock);
419 * Variant of xprt_inactive() for use only when sure that port is
420 * assigned to thread. For example, withing receive handlers.
423 xprt_inactive_self(SVCXPRT *xprt)
426 KASSERT(xprt->xp_thread != NULL,
427 ("xprt_inactive_self(%p) with NULL xp_thread", xprt));
428 xprt->xp_active = FALSE;
432 * Add a service program to the callout list.
433 * The dispatch routine will be called when a rpc request for this
434 * program number comes in.
437 svc_reg(SVCXPRT *xprt, const rpcprog_t prog, const rpcvers_t vers,
438 void (*dispatch)(struct svc_req *, SVCXPRT *),
439 const struct netconfig *nconf)
441 SVCPOOL *pool = xprt->xp_pool;
442 struct svc_callout *s;
446 /* VARIABLES PROTECTED BY svc_lock: s, svc_head */
448 if (xprt->xp_netid) {
449 netid = strdup(xprt->xp_netid, M_RPC);
451 } else if (nconf && nconf->nc_netid) {
452 netid = strdup(nconf->nc_netid, M_RPC);
454 } /* must have been created with svc_raw_create */
455 if ((netid == NULL) && (flag == 1)) {
459 mtx_lock(&pool->sp_lock);
460 if ((s = svc_find(pool, prog, vers, netid)) != NULL) {
463 if (s->sc_dispatch == dispatch)
464 goto rpcb_it; /* he is registering another xptr */
465 mtx_unlock(&pool->sp_lock);
468 s = malloc(sizeof (struct svc_callout), M_RPC, M_NOWAIT);
472 mtx_unlock(&pool->sp_lock);
478 s->sc_dispatch = dispatch;
480 TAILQ_INSERT_TAIL(&pool->sp_callouts, s, sc_link);
482 if ((xprt->xp_netid == NULL) && (flag == 1) && netid)
483 ((SVCXPRT *) xprt)->xp_netid = strdup(netid, M_RPC);
486 mtx_unlock(&pool->sp_lock);
487 /* now register the information with the local binder service */
490 struct netconfig tnc;
493 nb.buf = &xprt->xp_ltaddr;
494 nb.len = xprt->xp_ltaddr.ss_len;
495 dummy = rpcb_set(prog, vers, &tnc, &nb);
502 * Remove a service program from the callout list.
505 svc_unreg(SVCPOOL *pool, const rpcprog_t prog, const rpcvers_t vers)
507 struct svc_callout *s;
509 /* unregister the information anyway */
510 (void) rpcb_unset(prog, vers, NULL);
511 mtx_lock(&pool->sp_lock);
512 while ((s = svc_find(pool, prog, vers, NULL)) != NULL) {
513 TAILQ_REMOVE(&pool->sp_callouts, s, sc_link);
515 mem_free(s->sc_netid, sizeof (s->sc_netid) + 1);
516 mem_free(s, sizeof (struct svc_callout));
518 mtx_unlock(&pool->sp_lock);
522 * Add a service connection loss program to the callout list.
523 * The dispatch routine will be called when some port in ths pool die.
526 svc_loss_reg(SVCXPRT *xprt, void (*dispatch)(SVCXPRT *))
528 SVCPOOL *pool = xprt->xp_pool;
529 struct svc_loss_callout *s;
531 mtx_lock(&pool->sp_lock);
532 TAILQ_FOREACH(s, &pool->sp_lcallouts, slc_link) {
533 if (s->slc_dispatch == dispatch)
537 mtx_unlock(&pool->sp_lock);
540 s = malloc(sizeof (struct svc_callout), M_RPC, M_NOWAIT);
542 mtx_unlock(&pool->sp_lock);
545 s->slc_dispatch = dispatch;
546 TAILQ_INSERT_TAIL(&pool->sp_lcallouts, s, slc_link);
547 mtx_unlock(&pool->sp_lock);
552 * Remove a service connection loss program from the callout list.
555 svc_loss_unreg(SVCPOOL *pool, void (*dispatch)(SVCXPRT *))
557 struct svc_loss_callout *s;
559 mtx_lock(&pool->sp_lock);
560 TAILQ_FOREACH(s, &pool->sp_lcallouts, slc_link) {
561 if (s->slc_dispatch == dispatch) {
562 TAILQ_REMOVE(&pool->sp_lcallouts, s, slc_link);
567 mtx_unlock(&pool->sp_lock);
570 /* ********************** CALLOUT list related stuff ************* */
573 * Search the callout list for a program number, return the callout
576 static struct svc_callout *
577 svc_find(SVCPOOL *pool, rpcprog_t prog, rpcvers_t vers, char *netid)
579 struct svc_callout *s;
581 mtx_assert(&pool->sp_lock, MA_OWNED);
582 TAILQ_FOREACH(s, &pool->sp_callouts, sc_link) {
583 if (s->sc_prog == prog && s->sc_vers == vers
584 && (netid == NULL || s->sc_netid == NULL ||
585 strcmp(netid, s->sc_netid) == 0))
592 /* ******************* REPLY GENERATION ROUTINES ************ */
595 svc_sendreply_common(struct svc_req *rqstp, struct rpc_msg *rply,
598 SVCXPRT *xprt = rqstp->rq_xprt;
601 if (rqstp->rq_args) {
602 m_freem(rqstp->rq_args);
603 rqstp->rq_args = NULL;
606 if (xprt->xp_pool->sp_rcache)
607 replay_setreply(xprt->xp_pool->sp_rcache,
608 rply, svc_getrpccaller(rqstp), body);
610 if (!SVCAUTH_WRAP(&rqstp->rq_auth, &body))
613 ok = SVC_REPLY(xprt, rply, rqstp->rq_addr, body, &rqstp->rq_reply_seq);
614 if (rqstp->rq_addr) {
615 free(rqstp->rq_addr, M_SONAME);
616 rqstp->rq_addr = NULL;
623 * Send a reply to an rpc request
626 svc_sendreply(struct svc_req *rqstp, xdrproc_t xdr_results, void * xdr_location)
633 rply.rm_xid = rqstp->rq_xid;
634 rply.rm_direction = REPLY;
635 rply.rm_reply.rp_stat = MSG_ACCEPTED;
636 rply.acpted_rply.ar_verf = rqstp->rq_verf;
637 rply.acpted_rply.ar_stat = SUCCESS;
638 rply.acpted_rply.ar_results.where = NULL;
639 rply.acpted_rply.ar_results.proc = (xdrproc_t) xdr_void;
641 m = m_getcl(M_WAITOK, MT_DATA, 0);
642 xdrmbuf_create(&xdrs, m, XDR_ENCODE);
643 ok = xdr_results(&xdrs, xdr_location);
647 return (svc_sendreply_common(rqstp, &rply, m));
655 svc_sendreply_mbuf(struct svc_req *rqstp, struct mbuf *m)
659 rply.rm_xid = rqstp->rq_xid;
660 rply.rm_direction = REPLY;
661 rply.rm_reply.rp_stat = MSG_ACCEPTED;
662 rply.acpted_rply.ar_verf = rqstp->rq_verf;
663 rply.acpted_rply.ar_stat = SUCCESS;
664 rply.acpted_rply.ar_results.where = NULL;
665 rply.acpted_rply.ar_results.proc = (xdrproc_t) xdr_void;
667 return (svc_sendreply_common(rqstp, &rply, m));
671 * No procedure error reply
674 svcerr_noproc(struct svc_req *rqstp)
676 SVCXPRT *xprt = rqstp->rq_xprt;
679 rply.rm_xid = rqstp->rq_xid;
680 rply.rm_direction = REPLY;
681 rply.rm_reply.rp_stat = MSG_ACCEPTED;
682 rply.acpted_rply.ar_verf = rqstp->rq_verf;
683 rply.acpted_rply.ar_stat = PROC_UNAVAIL;
685 if (xprt->xp_pool->sp_rcache)
686 replay_setreply(xprt->xp_pool->sp_rcache,
687 &rply, svc_getrpccaller(rqstp), NULL);
689 svc_sendreply_common(rqstp, &rply, NULL);
693 * Can't decode args error reply
696 svcerr_decode(struct svc_req *rqstp)
698 SVCXPRT *xprt = rqstp->rq_xprt;
701 rply.rm_xid = rqstp->rq_xid;
702 rply.rm_direction = REPLY;
703 rply.rm_reply.rp_stat = MSG_ACCEPTED;
704 rply.acpted_rply.ar_verf = rqstp->rq_verf;
705 rply.acpted_rply.ar_stat = GARBAGE_ARGS;
707 if (xprt->xp_pool->sp_rcache)
708 replay_setreply(xprt->xp_pool->sp_rcache,
709 &rply, (struct sockaddr *) &xprt->xp_rtaddr, NULL);
711 svc_sendreply_common(rqstp, &rply, NULL);
718 svcerr_systemerr(struct svc_req *rqstp)
720 SVCXPRT *xprt = rqstp->rq_xprt;
723 rply.rm_xid = rqstp->rq_xid;
724 rply.rm_direction = REPLY;
725 rply.rm_reply.rp_stat = MSG_ACCEPTED;
726 rply.acpted_rply.ar_verf = rqstp->rq_verf;
727 rply.acpted_rply.ar_stat = SYSTEM_ERR;
729 if (xprt->xp_pool->sp_rcache)
730 replay_setreply(xprt->xp_pool->sp_rcache,
731 &rply, svc_getrpccaller(rqstp), NULL);
733 svc_sendreply_common(rqstp, &rply, NULL);
737 * Authentication error reply
740 svcerr_auth(struct svc_req *rqstp, enum auth_stat why)
742 SVCXPRT *xprt = rqstp->rq_xprt;
745 rply.rm_xid = rqstp->rq_xid;
746 rply.rm_direction = REPLY;
747 rply.rm_reply.rp_stat = MSG_DENIED;
748 rply.rjcted_rply.rj_stat = AUTH_ERROR;
749 rply.rjcted_rply.rj_why = why;
751 if (xprt->xp_pool->sp_rcache)
752 replay_setreply(xprt->xp_pool->sp_rcache,
753 &rply, svc_getrpccaller(rqstp), NULL);
755 svc_sendreply_common(rqstp, &rply, NULL);
759 * Auth too weak error reply
762 svcerr_weakauth(struct svc_req *rqstp)
765 svcerr_auth(rqstp, AUTH_TOOWEAK);
769 * Program unavailable error reply
772 svcerr_noprog(struct svc_req *rqstp)
774 SVCXPRT *xprt = rqstp->rq_xprt;
777 rply.rm_xid = rqstp->rq_xid;
778 rply.rm_direction = REPLY;
779 rply.rm_reply.rp_stat = MSG_ACCEPTED;
780 rply.acpted_rply.ar_verf = rqstp->rq_verf;
781 rply.acpted_rply.ar_stat = PROG_UNAVAIL;
783 if (xprt->xp_pool->sp_rcache)
784 replay_setreply(xprt->xp_pool->sp_rcache,
785 &rply, svc_getrpccaller(rqstp), NULL);
787 svc_sendreply_common(rqstp, &rply, NULL);
791 * Program version mismatch error reply
794 svcerr_progvers(struct svc_req *rqstp, rpcvers_t low_vers, rpcvers_t high_vers)
796 SVCXPRT *xprt = rqstp->rq_xprt;
799 rply.rm_xid = rqstp->rq_xid;
800 rply.rm_direction = REPLY;
801 rply.rm_reply.rp_stat = MSG_ACCEPTED;
802 rply.acpted_rply.ar_verf = rqstp->rq_verf;
803 rply.acpted_rply.ar_stat = PROG_MISMATCH;
804 rply.acpted_rply.ar_vers.low = (uint32_t)low_vers;
805 rply.acpted_rply.ar_vers.high = (uint32_t)high_vers;
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 * Allocate a new server transport structure. All fields are
816 * initialized to zero and xp_p3 is initialized to point at an
817 * extension structure to hold various flags and authentication
826 xprt = mem_alloc(sizeof(SVCXPRT));
827 memset(xprt, 0, sizeof(SVCXPRT));
828 ext = mem_alloc(sizeof(SVCXPRT_EXT));
829 memset(ext, 0, sizeof(SVCXPRT_EXT));
831 refcount_init(&xprt->xp_refs, 1);
837 * Free a server transport structure.
844 mem_free(xprt->xp_p3, sizeof(SVCXPRT_EXT));
845 mem_free(xprt, sizeof(SVCXPRT));
848 /* ******************* SERVER INPUT STUFF ******************* */
851 * Read RPC requests from a transport and queue them to be
852 * executed. We handle authentication and replay cache replies here.
853 * Actually dispatching the RPC is deferred till svc_executereq.
855 static enum xprt_stat
856 svc_getreq(SVCXPRT *xprt, struct svc_req **rqstp_ret)
858 SVCPOOL *pool = xprt->xp_pool;
862 struct svc_loss_callout *s;
865 /* now receive msgs from xprtprt (support batch calls) */
866 r = malloc(sizeof(*r), M_RPC, M_WAITOK|M_ZERO);
868 msg.rm_call.cb_cred.oa_base = r->rq_credarea;
869 msg.rm_call.cb_verf.oa_base = &r->rq_credarea[MAX_AUTH_BYTES];
870 r->rq_clntcred = &r->rq_credarea[2*MAX_AUTH_BYTES];
871 if (SVC_RECV(xprt, &msg, &r->rq_addr, &args)) {
875 * Handle replays and authenticate before queuing the
876 * request to be executed.
880 if (pool->sp_rcache) {
881 struct rpc_msg repmsg;
882 struct mbuf *repbody;
883 enum replay_state rs;
884 rs = replay_find(pool->sp_rcache, &msg,
885 svc_getrpccaller(r), &repmsg, &repbody);
890 SVC_REPLY(xprt, &repmsg, r->rq_addr,
891 repbody, &r->rq_reply_seq);
893 free(r->rq_addr, M_SONAME);
905 r->rq_xid = msg.rm_xid;
906 r->rq_prog = msg.rm_call.cb_prog;
907 r->rq_vers = msg.rm_call.cb_vers;
908 r->rq_proc = msg.rm_call.cb_proc;
909 r->rq_size = sizeof(*r) + m_length(args, NULL);
911 if ((why = _authenticate(r, &msg)) != AUTH_OK) {
913 * RPCSEC_GSS uses this return code
914 * for requests that form part of its
915 * context establishment protocol and
916 * should not be dispatched to the
919 if (why != RPCSEC_GSS_NODISPATCH)
924 if (!SVCAUTH_UNWRAP(&r->rq_auth, &r->rq_args)) {
930 * Everything checks out, return request to caller.
940 if ((stat = SVC_STAT(xprt)) == XPRT_DIED) {
941 TAILQ_FOREACH(s, &pool->sp_lcallouts, slc_link)
942 (*s->slc_dispatch)(xprt);
943 xprt_unregister(xprt);
950 svc_executereq(struct svc_req *rqstp)
952 SVCXPRT *xprt = rqstp->rq_xprt;
953 SVCPOOL *pool = xprt->xp_pool;
957 struct svc_callout *s;
959 /* now match message with a registered service*/
961 low_vers = (rpcvers_t) -1L;
962 high_vers = (rpcvers_t) 0L;
963 TAILQ_FOREACH(s, &pool->sp_callouts, sc_link) {
964 if (s->sc_prog == rqstp->rq_prog) {
965 if (s->sc_vers == rqstp->rq_vers) {
967 * We hand ownership of r to the
968 * dispatch method - they must call
971 (*s->sc_dispatch)(rqstp, xprt);
973 } /* found correct version */
975 if (s->sc_vers < low_vers)
976 low_vers = s->sc_vers;
977 if (s->sc_vers > high_vers)
978 high_vers = s->sc_vers;
979 } /* found correct program */
983 * if we got here, the program or version
987 svcerr_progvers(rqstp, low_vers, high_vers);
989 svcerr_noprog(rqstp);
995 svc_checkidle(SVCPOOL *pool)
997 SVCXPRT *xprt, *nxprt;
999 struct svcxprt_list cleanup;
1001 TAILQ_INIT(&cleanup);
1002 TAILQ_FOREACH_SAFE(xprt, &pool->sp_xlist, xp_link, nxprt) {
1004 * Only some transports have idle timers. Don't time
1005 * something out which is just waking up.
1007 if (!xprt->xp_idletimeout || xprt->xp_thread)
1010 timo = xprt->xp_lastactive + xprt->xp_idletimeout;
1011 if (time_uptime > timo) {
1012 xprt_unregister_locked(xprt);
1013 TAILQ_INSERT_TAIL(&cleanup, xprt, xp_link);
1017 mtx_unlock(&pool->sp_lock);
1018 TAILQ_FOREACH_SAFE(xprt, &cleanup, xp_link, nxprt) {
1021 mtx_lock(&pool->sp_lock);
1026 svc_assign_waiting_sockets(SVCPOOL *pool)
1030 mtx_lock(&pool->sp_lock);
1031 while ((xprt = TAILQ_FIRST(&pool->sp_active)) != NULL) {
1032 if (xprt_assignthread(xprt))
1033 TAILQ_REMOVE(&pool->sp_active, xprt, xp_alink);
1037 mtx_unlock(&pool->sp_lock);
1041 svc_change_space_used(SVCPOOL *pool, int delta)
1045 value = atomic_fetchadd_int(&pool->sp_space_used, delta) + delta;
1047 if (value >= pool->sp_space_high && !pool->sp_space_throttled) {
1048 pool->sp_space_throttled = TRUE;
1049 pool->sp_space_throttle_count++;
1051 if (value > pool->sp_space_used_highest)
1052 pool->sp_space_used_highest = value;
1054 if (value < pool->sp_space_low && pool->sp_space_throttled) {
1055 pool->sp_space_throttled = FALSE;
1056 svc_assign_waiting_sockets(pool);
1062 svc_request_space_available(SVCPOOL *pool)
1065 if (pool->sp_space_throttled)
1071 svc_run_internal(SVCPOOL *pool, bool_t ismaster)
1073 struct svc_reqlist reqs;
1074 SVCTHREAD *st, *stpref;
1076 enum xprt_stat stat;
1077 struct svc_req *rqstp;
1081 st = mem_alloc(sizeof(*st));
1084 STAILQ_INIT(&st->st_reqs);
1085 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);
1120 if (!xprt && STAILQ_EMPTY(&st->st_reqs)) {
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);
1143 if (ismaster || (!ismaster &&
1144 pool->sp_threadcount > pool->sp_minthreads))
1145 error = cv_timedwait_sig(&st->st_cond,
1146 &pool->sp_lock, 5 * hz);
1148 error = cv_wait_sig(&st->st_cond,
1151 LIST_REMOVE(st, st_ilink);
1152 st->st_idle = FALSE;
1156 * Reduce worker thread count when idle.
1158 if (error == EWOULDBLOCK) {
1160 && (pool->sp_threadcount
1161 > pool->sp_minthreads)
1163 && STAILQ_EMPTY(&st->st_reqs))
1166 mtx_unlock(&pool->sp_lock);
1168 mtx_lock(&pool->sp_lock);
1176 * Drain the transport socket and queue up any
1179 xprt->xp_lastactive = time_uptime;
1181 if (!svc_request_space_available(pool))
1183 mtx_unlock(&pool->sp_lock);
1185 stat = svc_getreq(xprt, &rqstp);
1187 svc_change_space_used(pool, rqstp->rq_size);
1189 * See if the application has
1190 * a preference for some other
1194 if (pool->sp_assign)
1195 stpref = pool->sp_assign(st,
1198 mtx_lock(&pool->sp_lock);
1200 rqstp->rq_thread = stpref;
1201 STAILQ_INSERT_TAIL(&stpref->st_reqs,
1205 * If we assigned the request
1206 * to another thread, make
1207 * sure its awake and continue
1209 * socket. Otherwise, try to
1210 * find some other thread to
1211 * read from the socket and
1212 * execute the request
1217 if (stpref->st_idle) {
1218 LIST_REMOVE(stpref, st_ilink);
1219 stpref->st_idle = FALSE;
1220 cv_signal(&stpref->st_cond);
1223 mtx_lock(&pool->sp_lock);
1224 } while (stat == XPRT_MOREREQS
1225 && pool->sp_state != SVCPOOL_CLOSING);
1228 * Move this transport to the end of the
1229 * active list to ensure fairness when
1230 * multiple transports are active. If this was
1231 * the last queued request, svc_getreq will
1232 * end up calling xprt_inactive to remove from
1235 xprt->xp_thread = NULL;
1237 if (xprt->xp_active) {
1238 if (!svc_request_space_available(pool) ||
1239 !xprt_assignthread(xprt))
1240 TAILQ_INSERT_TAIL(&pool->sp_active,
1243 STAILQ_CONCAT(&reqs, &st->st_reqs);
1244 mtx_unlock(&pool->sp_lock);
1247 STAILQ_CONCAT(&reqs, &st->st_reqs);
1248 mtx_unlock(&pool->sp_lock);
1252 * Execute what we have queued.
1255 while ((rqstp = STAILQ_FIRST(&reqs)) != NULL) {
1256 STAILQ_REMOVE_HEAD(&reqs, rq_link);
1257 sz += rqstp->rq_size;
1258 svc_executereq(rqstp);
1260 svc_change_space_used(pool, -sz);
1261 mtx_lock(&pool->sp_lock);
1270 KASSERT(STAILQ_EMPTY(&st->st_reqs), ("stray reqs on exit"));
1271 LIST_REMOVE(st, st_link);
1272 pool->sp_threadcount--;
1274 mtx_unlock(&pool->sp_lock);
1276 cv_destroy(&st->st_cond);
1277 mem_free(st, sizeof(*st));
1284 svc_thread_start(void *arg)
1287 svc_run_internal((SVCPOOL *) arg, FALSE);
1292 svc_new_thread(SVCPOOL *pool)
1296 pool->sp_threadcount++;
1297 kthread_add(svc_thread_start, pool,
1298 pool->sp_proc, &td, 0, 0,
1299 "%s: service", pool->sp_name);
1303 svc_run(SVCPOOL *pool)
1311 snprintf(td->td_name, sizeof(td->td_name),
1312 "%s: master", pool->sp_name);
1313 pool->sp_state = SVCPOOL_ACTIVE;
1315 pool->sp_lastcreatetime = time_uptime;
1316 pool->sp_threadcount = 1;
1318 for (i = 1; i < pool->sp_minthreads; i++) {
1319 svc_new_thread(pool);
1322 svc_run_internal(pool, TRUE);
1324 mtx_lock(&pool->sp_lock);
1325 while (pool->sp_threadcount > 0)
1326 msleep(pool, &pool->sp_lock, 0, "svcexit", 0);
1327 mtx_unlock(&pool->sp_lock);
1331 svc_exit(SVCPOOL *pool)
1335 mtx_lock(&pool->sp_lock);
1337 if (pool->sp_state != SVCPOOL_CLOSING) {
1338 pool->sp_state = SVCPOOL_CLOSING;
1339 LIST_FOREACH(st, &pool->sp_idlethreads, st_ilink)
1340 cv_signal(&st->st_cond);
1343 mtx_unlock(&pool->sp_lock);
1347 svc_getargs(struct svc_req *rqstp, xdrproc_t xargs, void *args)
1354 rqstp->rq_args = NULL;
1356 xdrmbuf_create(&xdrs, m, XDR_DECODE);
1357 stat = xargs(&xdrs, args);
1364 svc_freeargs(struct svc_req *rqstp, xdrproc_t xargs, void *args)
1368 if (rqstp->rq_addr) {
1369 free(rqstp->rq_addr, M_SONAME);
1370 rqstp->rq_addr = NULL;
1373 xdrs.x_op = XDR_FREE;
1374 return (xargs(&xdrs, args));
1378 svc_freereq(struct svc_req *rqstp)
1383 st = rqstp->rq_thread;
1387 pool->sp_done(st, rqstp);
1390 if (rqstp->rq_auth.svc_ah_ops)
1391 SVCAUTH_RELEASE(&rqstp->rq_auth);
1393 if (rqstp->rq_xprt) {
1394 SVC_RELEASE(rqstp->rq_xprt);
1398 free(rqstp->rq_addr, M_SONAME);
1401 m_freem(rqstp->rq_args);