svc.c revision 184588
1177633Sdfr/*	$NetBSD: svc.c,v 1.21 2000/07/06 03:10:35 christos Exp $	*/
2177633Sdfr
3177633Sdfr/*
4177633Sdfr * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5177633Sdfr * unrestricted use provided that this legend is included on all tape
6177633Sdfr * media and as a part of the software program in whole or part.  Users
7177633Sdfr * may copy or modify Sun RPC without charge, but are not authorized
8177633Sdfr * to license or distribute it to anyone else except as part of a product or
9177633Sdfr * program developed by the user.
10177633Sdfr *
11177633Sdfr * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12177633Sdfr * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13177633Sdfr * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14177633Sdfr *
15177633Sdfr * Sun RPC is provided with no support and without any obligation on the
16177633Sdfr * part of Sun Microsystems, Inc. to assist in its use, correction,
17177633Sdfr * modification or enhancement.
18177633Sdfr *
19177633Sdfr * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20177633Sdfr * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21177633Sdfr * OR ANY PART THEREOF.
22177633Sdfr *
23177633Sdfr * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24177633Sdfr * or profits or other special, indirect and consequential damages, even if
25177633Sdfr * Sun has been advised of the possibility of such damages.
26177633Sdfr *
27177633Sdfr * Sun Microsystems, Inc.
28177633Sdfr * 2550 Garcia Avenue
29177633Sdfr * Mountain View, California  94043
30177633Sdfr */
31177633Sdfr
32177633Sdfr#if defined(LIBC_SCCS) && !defined(lint)
33177633Sdfrstatic char *sccsid2 = "@(#)svc.c 1.44 88/02/08 Copyr 1984 Sun Micro";
34177633Sdfrstatic char *sccsid = "@(#)svc.c	2.4 88/08/11 4.0 RPCSRC";
35177633Sdfr#endif
36177633Sdfr#include <sys/cdefs.h>
37177633Sdfr__FBSDID("$FreeBSD: head/sys/rpc/svc.c 184588 2008-11-03 10:38:00Z dfr $");
38177633Sdfr
39177633Sdfr/*
40177633Sdfr * svc.c, Server-side remote procedure call interface.
41177633Sdfr *
42177633Sdfr * There are two sets of procedures here.  The xprt routines are
43177633Sdfr * for handling transport handles.  The svc routines handle the
44177633Sdfr * list of service routines.
45177633Sdfr *
46177633Sdfr * Copyright (C) 1984, Sun Microsystems, Inc.
47177633Sdfr */
48177633Sdfr
49177633Sdfr#include <sys/param.h>
50177633Sdfr#include <sys/lock.h>
51177633Sdfr#include <sys/kernel.h>
52184588Sdfr#include <sys/kthread.h>
53177633Sdfr#include <sys/malloc.h>
54184588Sdfr#include <sys/mbuf.h>
55177633Sdfr#include <sys/mutex.h>
56184588Sdfr#include <sys/proc.h>
57177633Sdfr#include <sys/queue.h>
58184588Sdfr#include <sys/socketvar.h>
59177633Sdfr#include <sys/systm.h>
60177633Sdfr#include <sys/ucred.h>
61177633Sdfr
62177633Sdfr#include <rpc/rpc.h>
63177633Sdfr#include <rpc/rpcb_clnt.h>
64184588Sdfr#include <rpc/replay.h>
65177633Sdfr
66177685Sdfr#include <rpc/rpc_com.h>
67177633Sdfr
68177633Sdfr#define SVC_VERSQUIET 0x0001		/* keep quiet about vers mismatch */
69184588Sdfr#define version_keepquiet(xp) (SVC_EXT(xp)->xp_flags & SVC_VERSQUIET)
70177633Sdfr
71177633Sdfrstatic struct svc_callout *svc_find(SVCPOOL *pool, rpcprog_t, rpcvers_t,
72177633Sdfr    char *);
73184588Sdfrstatic void svc_new_thread(SVCPOOL *pool);
74184588Sdfrstatic void xprt_unregister_locked(SVCXPRT *xprt);
75177633Sdfr
76177633Sdfr/* ***************  SVCXPRT related stuff **************** */
77177633Sdfr
78184588Sdfrstatic int svcpool_minthread_sysctl(SYSCTL_HANDLER_ARGS);
79184588Sdfrstatic int svcpool_maxthread_sysctl(SYSCTL_HANDLER_ARGS);
80184588Sdfr
81177633SdfrSVCPOOL*
82184588Sdfrsvcpool_create(const char *name, struct sysctl_oid_list *sysctl_base)
83177633Sdfr{
84177633Sdfr	SVCPOOL *pool;
85177633Sdfr
86177633Sdfr	pool = malloc(sizeof(SVCPOOL), M_RPC, M_WAITOK|M_ZERO);
87177633Sdfr
88177633Sdfr	mtx_init(&pool->sp_lock, "sp_lock", NULL, MTX_DEF);
89184588Sdfr	pool->sp_name = name;
90184588Sdfr	pool->sp_state = SVCPOOL_INIT;
91184588Sdfr	pool->sp_proc = NULL;
92177633Sdfr	TAILQ_INIT(&pool->sp_xlist);
93177633Sdfr	TAILQ_INIT(&pool->sp_active);
94177633Sdfr	TAILQ_INIT(&pool->sp_callouts);
95184588Sdfr	LIST_INIT(&pool->sp_threads);
96184588Sdfr	LIST_INIT(&pool->sp_idlethreads);
97184588Sdfr	pool->sp_minthreads = 1;
98184588Sdfr	pool->sp_maxthreads = 1;
99184588Sdfr	pool->sp_threadcount = 0;
100177633Sdfr
101184588Sdfr	/*
102184588Sdfr	 * Don't use more than a quarter of mbuf clusters or more than
103184588Sdfr	 * 45Mb buffering requests.
104184588Sdfr	 */
105184588Sdfr	pool->sp_space_high = nmbclusters * MCLBYTES / 4;
106184588Sdfr	if (pool->sp_space_high > 45 << 20)
107184588Sdfr		pool->sp_space_high = 45 << 20;
108184588Sdfr	pool->sp_space_low = 2 * pool->sp_space_high / 3;
109184588Sdfr
110184588Sdfr	sysctl_ctx_init(&pool->sp_sysctl);
111184588Sdfr	if (sysctl_base) {
112184588Sdfr		SYSCTL_ADD_PROC(&pool->sp_sysctl, sysctl_base, OID_AUTO,
113184588Sdfr		    "minthreads", CTLTYPE_INT | CTLFLAG_RW,
114184588Sdfr		    pool, 0, svcpool_minthread_sysctl, "I", "");
115184588Sdfr		SYSCTL_ADD_PROC(&pool->sp_sysctl, sysctl_base, OID_AUTO,
116184588Sdfr		    "maxthreads", CTLTYPE_INT | CTLFLAG_RW,
117184588Sdfr		    pool, 0, svcpool_maxthread_sysctl, "I", "");
118184588Sdfr		SYSCTL_ADD_INT(&pool->sp_sysctl, sysctl_base, OID_AUTO,
119184588Sdfr		    "threads", CTLFLAG_RD, &pool->sp_threadcount, 0, "");
120184588Sdfr
121184588Sdfr		SYSCTL_ADD_UINT(&pool->sp_sysctl, sysctl_base, OID_AUTO,
122184588Sdfr		    "request_space_used", CTLFLAG_RD,
123184588Sdfr		    &pool->sp_space_used, 0,
124184588Sdfr		    "Space in parsed but not handled requests.");
125184588Sdfr
126184588Sdfr		SYSCTL_ADD_UINT(&pool->sp_sysctl, sysctl_base, OID_AUTO,
127184588Sdfr		    "request_space_used_highest", CTLFLAG_RD,
128184588Sdfr		    &pool->sp_space_used_highest, 0,
129184588Sdfr		    "Highest space used since reboot.");
130184588Sdfr
131184588Sdfr		SYSCTL_ADD_UINT(&pool->sp_sysctl, sysctl_base, OID_AUTO,
132184588Sdfr		    "request_space_high", CTLFLAG_RW,
133184588Sdfr		    &pool->sp_space_high, 0,
134184588Sdfr		    "Maximum space in parsed but not handled requests.");
135184588Sdfr
136184588Sdfr		SYSCTL_ADD_UINT(&pool->sp_sysctl, sysctl_base, OID_AUTO,
137184588Sdfr		    "request_space_low", CTLFLAG_RW,
138184588Sdfr		    &pool->sp_space_low, 0,
139184588Sdfr		    "Low water mark for request space.");
140184588Sdfr
141184588Sdfr		SYSCTL_ADD_UINT(&pool->sp_sysctl, sysctl_base, OID_AUTO,
142184588Sdfr		    "request_space_throttled", CTLFLAG_RD,
143184588Sdfr		    &pool->sp_space_throttled, 0,
144184588Sdfr		    "Whether nfs requests are currently throttled");
145184588Sdfr
146184588Sdfr		SYSCTL_ADD_UINT(&pool->sp_sysctl, sysctl_base, OID_AUTO,
147184588Sdfr		    "request_space_throttle_count", CTLFLAG_RD,
148184588Sdfr		    &pool->sp_space_throttle_count, 0,
149184588Sdfr		    "Count of times throttling based on request space has occurred");
150184588Sdfr	}
151184588Sdfr
152177633Sdfr	return pool;
153177633Sdfr}
154177633Sdfr
155177633Sdfrvoid
156177633Sdfrsvcpool_destroy(SVCPOOL *pool)
157177633Sdfr{
158184588Sdfr	SVCXPRT *xprt, *nxprt;
159177633Sdfr	struct svc_callout *s;
160184588Sdfr	struct svcxprt_list cleanup;
161177633Sdfr
162184588Sdfr	TAILQ_INIT(&cleanup);
163177633Sdfr	mtx_lock(&pool->sp_lock);
164177633Sdfr
165177633Sdfr	while (TAILQ_FIRST(&pool->sp_xlist)) {
166177633Sdfr		xprt = TAILQ_FIRST(&pool->sp_xlist);
167184588Sdfr		xprt_unregister_locked(xprt);
168184588Sdfr		TAILQ_INSERT_TAIL(&cleanup, xprt, xp_link);
169177633Sdfr	}
170177633Sdfr
171177633Sdfr	while (TAILQ_FIRST(&pool->sp_callouts)) {
172177633Sdfr		s = TAILQ_FIRST(&pool->sp_callouts);
173177633Sdfr		mtx_unlock(&pool->sp_lock);
174177633Sdfr		svc_unreg(pool, s->sc_prog, s->sc_vers);
175177633Sdfr		mtx_lock(&pool->sp_lock);
176177633Sdfr	}
177177633Sdfr
178177633Sdfr	mtx_destroy(&pool->sp_lock);
179184588Sdfr
180184588Sdfr	TAILQ_FOREACH_SAFE(xprt, &cleanup, xp_link, nxprt) {
181184588Sdfr		SVC_RELEASE(xprt);
182184588Sdfr	}
183184588Sdfr
184184588Sdfr	if (pool->sp_rcache)
185184588Sdfr		replay_freecache(pool->sp_rcache);
186184588Sdfr
187184588Sdfr	sysctl_ctx_free(&pool->sp_sysctl);
188177633Sdfr	free(pool, M_RPC);
189177633Sdfr}
190177633Sdfr
191184588Sdfrstatic bool_t
192184588Sdfrsvcpool_active(SVCPOOL *pool)
193184588Sdfr{
194184588Sdfr	enum svcpool_state state = pool->sp_state;
195184588Sdfr
196184588Sdfr	if (state == SVCPOOL_INIT || state == SVCPOOL_CLOSING)
197184588Sdfr		return (FALSE);
198184588Sdfr	return (TRUE);
199184588Sdfr}
200184588Sdfr
201177633Sdfr/*
202184588Sdfr * Sysctl handler to set the minimum thread count on a pool
203184588Sdfr */
204184588Sdfrstatic int
205184588Sdfrsvcpool_minthread_sysctl(SYSCTL_HANDLER_ARGS)
206184588Sdfr{
207184588Sdfr	SVCPOOL *pool;
208184588Sdfr	int newminthreads, error, n;
209184588Sdfr
210184588Sdfr	pool = oidp->oid_arg1;
211184588Sdfr	newminthreads = pool->sp_minthreads;
212184588Sdfr	error = sysctl_handle_int(oidp, &newminthreads, 0, req);
213184588Sdfr	if (error == 0 && newminthreads != pool->sp_minthreads) {
214184588Sdfr		if (newminthreads > pool->sp_maxthreads)
215184588Sdfr			return (EINVAL);
216184588Sdfr		mtx_lock(&pool->sp_lock);
217184588Sdfr		if (newminthreads > pool->sp_minthreads
218184588Sdfr		    && svcpool_active(pool)) {
219184588Sdfr			/*
220184588Sdfr			 * If the pool is running and we are
221184588Sdfr			 * increasing, create some more threads now.
222184588Sdfr			 */
223184588Sdfr			n = newminthreads - pool->sp_threadcount;
224184588Sdfr			if (n > 0) {
225184588Sdfr				mtx_unlock(&pool->sp_lock);
226184588Sdfr				while (n--)
227184588Sdfr					svc_new_thread(pool);
228184588Sdfr				mtx_lock(&pool->sp_lock);
229184588Sdfr			}
230184588Sdfr		}
231184588Sdfr		pool->sp_minthreads = newminthreads;
232184588Sdfr		mtx_unlock(&pool->sp_lock);
233184588Sdfr	}
234184588Sdfr	return (error);
235184588Sdfr}
236184588Sdfr
237184588Sdfr/*
238184588Sdfr * Sysctl handler to set the maximum thread count on a pool
239184588Sdfr */
240184588Sdfrstatic int
241184588Sdfrsvcpool_maxthread_sysctl(SYSCTL_HANDLER_ARGS)
242184588Sdfr{
243184588Sdfr	SVCPOOL *pool;
244184588Sdfr	SVCTHREAD *st;
245184588Sdfr	int newmaxthreads, error;
246184588Sdfr
247184588Sdfr	pool = oidp->oid_arg1;
248184588Sdfr	newmaxthreads = pool->sp_maxthreads;
249184588Sdfr	error = sysctl_handle_int(oidp, &newmaxthreads, 0, req);
250184588Sdfr	if (error == 0 && newmaxthreads != pool->sp_maxthreads) {
251184588Sdfr		if (newmaxthreads < pool->sp_minthreads)
252184588Sdfr			return (EINVAL);
253184588Sdfr		mtx_lock(&pool->sp_lock);
254184588Sdfr		if (newmaxthreads < pool->sp_maxthreads
255184588Sdfr		    && svcpool_active(pool)) {
256184588Sdfr			/*
257184588Sdfr			 * If the pool is running and we are
258184588Sdfr			 * decreasing, wake up some idle threads to
259184588Sdfr			 * encourage them to exit.
260184588Sdfr			 */
261184588Sdfr			LIST_FOREACH(st, &pool->sp_idlethreads, st_ilink)
262184588Sdfr				cv_signal(&st->st_cond);
263184588Sdfr		}
264184588Sdfr		pool->sp_maxthreads = newmaxthreads;
265184588Sdfr		mtx_unlock(&pool->sp_lock);
266184588Sdfr	}
267184588Sdfr	return (error);
268184588Sdfr}
269184588Sdfr
270184588Sdfr/*
271177633Sdfr * Activate a transport handle.
272177633Sdfr */
273177633Sdfrvoid
274177633Sdfrxprt_register(SVCXPRT *xprt)
275177633Sdfr{
276177633Sdfr	SVCPOOL *pool = xprt->xp_pool;
277177633Sdfr
278177633Sdfr	mtx_lock(&pool->sp_lock);
279177633Sdfr	xprt->xp_registered = TRUE;
280177633Sdfr	xprt->xp_active = FALSE;
281177633Sdfr	TAILQ_INSERT_TAIL(&pool->sp_xlist, xprt, xp_link);
282177633Sdfr	mtx_unlock(&pool->sp_lock);
283177633Sdfr}
284177633Sdfr
285177633Sdfr/*
286184588Sdfr * De-activate a transport handle. Note: the locked version doesn't
287184588Sdfr * release the transport - caller must do that after dropping the pool
288184588Sdfr * lock.
289177633Sdfr */
290177633Sdfrstatic void
291184588Sdfrxprt_unregister_locked(SVCXPRT *xprt)
292177633Sdfr{
293177633Sdfr	SVCPOOL *pool = xprt->xp_pool;
294177633Sdfr
295177633Sdfr	if (xprt->xp_active) {
296177633Sdfr		TAILQ_REMOVE(&pool->sp_active, xprt, xp_alink);
297177633Sdfr		xprt->xp_active = FALSE;
298177633Sdfr	}
299177633Sdfr	TAILQ_REMOVE(&pool->sp_xlist, xprt, xp_link);
300177633Sdfr	xprt->xp_registered = FALSE;
301184588Sdfr}
302177633Sdfr
303184588Sdfrvoid
304184588Sdfrxprt_unregister(SVCXPRT *xprt)
305184588Sdfr{
306184588Sdfr	SVCPOOL *pool = xprt->xp_pool;
307184588Sdfr
308184588Sdfr	mtx_lock(&pool->sp_lock);
309184588Sdfr	xprt_unregister_locked(xprt);
310184588Sdfr	mtx_unlock(&pool->sp_lock);
311184588Sdfr
312184588Sdfr	SVC_RELEASE(xprt);
313177633Sdfr}
314177633Sdfr
315184588Sdfrstatic void
316184588Sdfrxprt_assignthread(SVCXPRT *xprt)
317184588Sdfr{
318184588Sdfr	SVCPOOL *pool = xprt->xp_pool;
319184588Sdfr	SVCTHREAD *st;
320184588Sdfr
321184588Sdfr	/*
322184588Sdfr	 * Attempt to assign a service thread to this
323184588Sdfr	 * transport.
324184588Sdfr	 */
325184588Sdfr	LIST_FOREACH(st, &pool->sp_idlethreads, st_ilink) {
326184588Sdfr		if (st->st_xprt == NULL && STAILQ_EMPTY(&st->st_reqs))
327184588Sdfr			break;
328184588Sdfr	}
329184588Sdfr	if (st) {
330184588Sdfr		SVC_ACQUIRE(xprt);
331184588Sdfr		xprt->xp_thread = st;
332184588Sdfr		st->st_xprt = xprt;
333184588Sdfr		cv_signal(&st->st_cond);
334184588Sdfr	} else {
335184588Sdfr		/*
336184588Sdfr		 * See if we can create a new thread. The
337184588Sdfr		 * actual thread creation happens in
338184588Sdfr		 * svc_run_internal because our locking state
339184588Sdfr		 * is poorly defined (we are typically called
340184588Sdfr		 * from a socket upcall). Don't create more
341184588Sdfr		 * than one thread per second.
342184588Sdfr		 */
343184588Sdfr		if (pool->sp_state == SVCPOOL_ACTIVE
344184588Sdfr		    && pool->sp_lastcreatetime < time_uptime
345184588Sdfr		    && pool->sp_threadcount < pool->sp_maxthreads) {
346184588Sdfr			pool->sp_state = SVCPOOL_THREADWANTED;
347184588Sdfr		}
348184588Sdfr	}
349184588Sdfr}
350184588Sdfr
351177633Sdfrvoid
352177633Sdfrxprt_active(SVCXPRT *xprt)
353177633Sdfr{
354177633Sdfr	SVCPOOL *pool = xprt->xp_pool;
355177633Sdfr
356184588Sdfr	if (!xprt->xp_registered) {
357184588Sdfr		/*
358184588Sdfr		 * Race with xprt_unregister - we lose.
359184588Sdfr		 */
360184588Sdfr		return;
361184588Sdfr	}
362184588Sdfr
363177633Sdfr	mtx_lock(&pool->sp_lock);
364177633Sdfr
365177633Sdfr	if (!xprt->xp_active) {
366177633Sdfr		TAILQ_INSERT_TAIL(&pool->sp_active, xprt, xp_alink);
367177633Sdfr		xprt->xp_active = TRUE;
368184588Sdfr		xprt_assignthread(xprt);
369177633Sdfr	}
370177633Sdfr
371177633Sdfr	mtx_unlock(&pool->sp_lock);
372177633Sdfr}
373177633Sdfr
374177633Sdfrvoid
375184588Sdfrxprt_inactive_locked(SVCXPRT *xprt)
376177633Sdfr{
377177633Sdfr	SVCPOOL *pool = xprt->xp_pool;
378177633Sdfr
379177633Sdfr	if (xprt->xp_active) {
380177633Sdfr		TAILQ_REMOVE(&pool->sp_active, xprt, xp_alink);
381177633Sdfr		xprt->xp_active = FALSE;
382177633Sdfr	}
383184588Sdfr}
384177633Sdfr
385184588Sdfrvoid
386184588Sdfrxprt_inactive(SVCXPRT *xprt)
387184588Sdfr{
388184588Sdfr	SVCPOOL *pool = xprt->xp_pool;
389184588Sdfr
390184588Sdfr	mtx_lock(&pool->sp_lock);
391184588Sdfr	xprt_inactive_locked(xprt);
392177633Sdfr	mtx_unlock(&pool->sp_lock);
393177633Sdfr}
394177633Sdfr
395177633Sdfr/*
396177633Sdfr * Add a service program to the callout list.
397177633Sdfr * The dispatch routine will be called when a rpc request for this
398177633Sdfr * program number comes in.
399177633Sdfr */
400177633Sdfrbool_t
401177633Sdfrsvc_reg(SVCXPRT *xprt, const rpcprog_t prog, const rpcvers_t vers,
402177633Sdfr    void (*dispatch)(struct svc_req *, SVCXPRT *),
403177633Sdfr    const struct netconfig *nconf)
404177633Sdfr{
405177633Sdfr	SVCPOOL *pool = xprt->xp_pool;
406177633Sdfr	struct svc_callout *s;
407177633Sdfr	char *netid = NULL;
408177633Sdfr	int flag = 0;
409177633Sdfr
410177633Sdfr/* VARIABLES PROTECTED BY svc_lock: s, svc_head */
411177633Sdfr
412177633Sdfr	if (xprt->xp_netid) {
413177633Sdfr		netid = strdup(xprt->xp_netid, M_RPC);
414177633Sdfr		flag = 1;
415177633Sdfr	} else if (nconf && nconf->nc_netid) {
416177633Sdfr		netid = strdup(nconf->nc_netid, M_RPC);
417177633Sdfr		flag = 1;
418177633Sdfr	} /* must have been created with svc_raw_create */
419177633Sdfr	if ((netid == NULL) && (flag == 1)) {
420177633Sdfr		return (FALSE);
421177633Sdfr	}
422177633Sdfr
423177633Sdfr	mtx_lock(&pool->sp_lock);
424177633Sdfr	if ((s = svc_find(pool, prog, vers, netid)) != NULL) {
425177633Sdfr		if (netid)
426177633Sdfr			free(netid, M_RPC);
427177633Sdfr		if (s->sc_dispatch == dispatch)
428177633Sdfr			goto rpcb_it; /* he is registering another xptr */
429177633Sdfr		mtx_unlock(&pool->sp_lock);
430177633Sdfr		return (FALSE);
431177633Sdfr	}
432177633Sdfr	s = malloc(sizeof (struct svc_callout), M_RPC, M_NOWAIT);
433177633Sdfr	if (s == NULL) {
434177633Sdfr		if (netid)
435177633Sdfr			free(netid, M_RPC);
436177633Sdfr		mtx_unlock(&pool->sp_lock);
437177633Sdfr		return (FALSE);
438177633Sdfr	}
439177633Sdfr
440177633Sdfr	s->sc_prog = prog;
441177633Sdfr	s->sc_vers = vers;
442177633Sdfr	s->sc_dispatch = dispatch;
443177633Sdfr	s->sc_netid = netid;
444177633Sdfr	TAILQ_INSERT_TAIL(&pool->sp_callouts, s, sc_link);
445177633Sdfr
446177633Sdfr	if ((xprt->xp_netid == NULL) && (flag == 1) && netid)
447177633Sdfr		((SVCXPRT *) xprt)->xp_netid = strdup(netid, M_RPC);
448177633Sdfr
449177633Sdfrrpcb_it:
450177633Sdfr	mtx_unlock(&pool->sp_lock);
451177633Sdfr	/* now register the information with the local binder service */
452177633Sdfr	if (nconf) {
453177633Sdfr		bool_t dummy;
454177633Sdfr		struct netconfig tnc;
455184588Sdfr		struct netbuf nb;
456177633Sdfr		tnc = *nconf;
457184588Sdfr		nb.buf = &xprt->xp_ltaddr;
458184588Sdfr		nb.len = xprt->xp_ltaddr.ss_len;
459184588Sdfr		dummy = rpcb_set(prog, vers, &tnc, &nb);
460177633Sdfr		return (dummy);
461177633Sdfr	}
462177633Sdfr	return (TRUE);
463177633Sdfr}
464177633Sdfr
465177633Sdfr/*
466177633Sdfr * Remove a service program from the callout list.
467177633Sdfr */
468177633Sdfrvoid
469177633Sdfrsvc_unreg(SVCPOOL *pool, const rpcprog_t prog, const rpcvers_t vers)
470177633Sdfr{
471177633Sdfr	struct svc_callout *s;
472177633Sdfr
473177633Sdfr	/* unregister the information anyway */
474177633Sdfr	(void) rpcb_unset(prog, vers, NULL);
475177633Sdfr	mtx_lock(&pool->sp_lock);
476177633Sdfr	while ((s = svc_find(pool, prog, vers, NULL)) != NULL) {
477177633Sdfr		TAILQ_REMOVE(&pool->sp_callouts, s, sc_link);
478177633Sdfr		if (s->sc_netid)
479177633Sdfr			mem_free(s->sc_netid, sizeof (s->sc_netid) + 1);
480177633Sdfr		mem_free(s, sizeof (struct svc_callout));
481177633Sdfr	}
482177633Sdfr	mtx_unlock(&pool->sp_lock);
483177633Sdfr}
484177633Sdfr
485177633Sdfr/* ********************** CALLOUT list related stuff ************* */
486177633Sdfr
487177633Sdfr/*
488177633Sdfr * Search the callout list for a program number, return the callout
489177633Sdfr * struct.
490177633Sdfr */
491177633Sdfrstatic struct svc_callout *
492177633Sdfrsvc_find(SVCPOOL *pool, rpcprog_t prog, rpcvers_t vers, char *netid)
493177633Sdfr{
494177633Sdfr	struct svc_callout *s;
495177633Sdfr
496177633Sdfr	mtx_assert(&pool->sp_lock, MA_OWNED);
497177633Sdfr	TAILQ_FOREACH(s, &pool->sp_callouts, sc_link) {
498177633Sdfr		if (s->sc_prog == prog && s->sc_vers == vers
499177633Sdfr		    && (netid == NULL || s->sc_netid == NULL ||
500177633Sdfr			strcmp(netid, s->sc_netid) == 0))
501177633Sdfr			break;
502177633Sdfr	}
503177633Sdfr
504177633Sdfr	return (s);
505177633Sdfr}
506177633Sdfr
507177633Sdfr/* ******************* REPLY GENERATION ROUTINES  ************ */
508177633Sdfr
509184588Sdfrstatic bool_t
510184588Sdfrsvc_sendreply_common(struct svc_req *rqstp, struct rpc_msg *rply,
511184588Sdfr    struct mbuf *body)
512184588Sdfr{
513184588Sdfr	SVCXPRT *xprt = rqstp->rq_xprt;
514184588Sdfr	bool_t ok;
515184588Sdfr
516184588Sdfr	if (rqstp->rq_args) {
517184588Sdfr		m_freem(rqstp->rq_args);
518184588Sdfr		rqstp->rq_args = NULL;
519184588Sdfr	}
520184588Sdfr
521184588Sdfr	if (xprt->xp_pool->sp_rcache)
522184588Sdfr		replay_setreply(xprt->xp_pool->sp_rcache,
523184588Sdfr		    rply, svc_getrpccaller(rqstp), body);
524184588Sdfr
525184588Sdfr	if (!SVCAUTH_WRAP(&rqstp->rq_auth, &body))
526184588Sdfr		return (FALSE);
527184588Sdfr
528184588Sdfr	ok = SVC_REPLY(xprt, rply, rqstp->rq_addr, body);
529184588Sdfr	if (rqstp->rq_addr) {
530184588Sdfr		free(rqstp->rq_addr, M_SONAME);
531184588Sdfr		rqstp->rq_addr = NULL;
532184588Sdfr	}
533184588Sdfr
534184588Sdfr	return (ok);
535184588Sdfr}
536184588Sdfr
537177633Sdfr/*
538177633Sdfr * Send a reply to an rpc request
539177633Sdfr */
540177633Sdfrbool_t
541184588Sdfrsvc_sendreply(struct svc_req *rqstp, xdrproc_t xdr_results, void * xdr_location)
542177633Sdfr{
543177633Sdfr	struct rpc_msg rply;
544184588Sdfr	struct mbuf *m;
545184588Sdfr	XDR xdrs;
546184588Sdfr	bool_t ok;
547177633Sdfr
548184588Sdfr	rply.rm_xid = rqstp->rq_xid;
549177633Sdfr	rply.rm_direction = REPLY;
550177633Sdfr	rply.rm_reply.rp_stat = MSG_ACCEPTED;
551184588Sdfr	rply.acpted_rply.ar_verf = rqstp->rq_verf;
552177633Sdfr	rply.acpted_rply.ar_stat = SUCCESS;
553184588Sdfr	rply.acpted_rply.ar_results.where = NULL;
554184588Sdfr	rply.acpted_rply.ar_results.proc = (xdrproc_t) xdr_void;
555177633Sdfr
556184588Sdfr	MGET(m, M_WAIT, MT_DATA);
557184588Sdfr	MCLGET(m, M_WAIT);
558184588Sdfr	m->m_len = 0;
559184588Sdfr	xdrmbuf_create(&xdrs, m, XDR_ENCODE);
560184588Sdfr	ok = xdr_results(&xdrs, xdr_location);
561184588Sdfr	XDR_DESTROY(&xdrs);
562184588Sdfr
563184588Sdfr	if (ok) {
564184588Sdfr		return (svc_sendreply_common(rqstp, &rply, m));
565184588Sdfr	} else {
566184588Sdfr		m_freem(m);
567184588Sdfr		return (FALSE);
568184588Sdfr	}
569177633Sdfr}
570177633Sdfr
571184588Sdfrbool_t
572184588Sdfrsvc_sendreply_mbuf(struct svc_req *rqstp, struct mbuf *m)
573184588Sdfr{
574184588Sdfr	struct rpc_msg rply;
575184588Sdfr
576184588Sdfr	rply.rm_xid = rqstp->rq_xid;
577184588Sdfr	rply.rm_direction = REPLY;
578184588Sdfr	rply.rm_reply.rp_stat = MSG_ACCEPTED;
579184588Sdfr	rply.acpted_rply.ar_verf = rqstp->rq_verf;
580184588Sdfr	rply.acpted_rply.ar_stat = SUCCESS;
581184588Sdfr	rply.acpted_rply.ar_results.where = NULL;
582184588Sdfr	rply.acpted_rply.ar_results.proc = (xdrproc_t) xdr_void;
583184588Sdfr
584184588Sdfr	return (svc_sendreply_common(rqstp, &rply, m));
585184588Sdfr}
586184588Sdfr
587177633Sdfr/*
588177633Sdfr * No procedure error reply
589177633Sdfr */
590177633Sdfrvoid
591184588Sdfrsvcerr_noproc(struct svc_req *rqstp)
592177633Sdfr{
593184588Sdfr	SVCXPRT *xprt = rqstp->rq_xprt;
594177633Sdfr	struct rpc_msg rply;
595177633Sdfr
596184588Sdfr	rply.rm_xid = rqstp->rq_xid;
597177633Sdfr	rply.rm_direction = REPLY;
598177633Sdfr	rply.rm_reply.rp_stat = MSG_ACCEPTED;
599184588Sdfr	rply.acpted_rply.ar_verf = rqstp->rq_verf;
600177633Sdfr	rply.acpted_rply.ar_stat = PROC_UNAVAIL;
601177633Sdfr
602184588Sdfr	if (xprt->xp_pool->sp_rcache)
603184588Sdfr		replay_setreply(xprt->xp_pool->sp_rcache,
604184588Sdfr		    &rply, svc_getrpccaller(rqstp), NULL);
605184588Sdfr
606184588Sdfr	svc_sendreply_common(rqstp, &rply, NULL);
607177633Sdfr}
608177633Sdfr
609177633Sdfr/*
610177633Sdfr * Can't decode args error reply
611177633Sdfr */
612177633Sdfrvoid
613184588Sdfrsvcerr_decode(struct svc_req *rqstp)
614177633Sdfr{
615184588Sdfr	SVCXPRT *xprt = rqstp->rq_xprt;
616177633Sdfr	struct rpc_msg rply;
617177633Sdfr
618184588Sdfr	rply.rm_xid = rqstp->rq_xid;
619177633Sdfr	rply.rm_direction = REPLY;
620177633Sdfr	rply.rm_reply.rp_stat = MSG_ACCEPTED;
621184588Sdfr	rply.acpted_rply.ar_verf = rqstp->rq_verf;
622177633Sdfr	rply.acpted_rply.ar_stat = GARBAGE_ARGS;
623177633Sdfr
624184588Sdfr	if (xprt->xp_pool->sp_rcache)
625184588Sdfr		replay_setreply(xprt->xp_pool->sp_rcache,
626184588Sdfr		    &rply, (struct sockaddr *) &xprt->xp_rtaddr, NULL);
627184588Sdfr
628184588Sdfr	svc_sendreply_common(rqstp, &rply, NULL);
629177633Sdfr}
630177633Sdfr
631177633Sdfr/*
632177633Sdfr * Some system error
633177633Sdfr */
634177633Sdfrvoid
635184588Sdfrsvcerr_systemerr(struct svc_req *rqstp)
636177633Sdfr{
637184588Sdfr	SVCXPRT *xprt = rqstp->rq_xprt;
638177633Sdfr	struct rpc_msg rply;
639177633Sdfr
640184588Sdfr	rply.rm_xid = rqstp->rq_xid;
641177633Sdfr	rply.rm_direction = REPLY;
642177633Sdfr	rply.rm_reply.rp_stat = MSG_ACCEPTED;
643184588Sdfr	rply.acpted_rply.ar_verf = rqstp->rq_verf;
644177633Sdfr	rply.acpted_rply.ar_stat = SYSTEM_ERR;
645177633Sdfr
646184588Sdfr	if (xprt->xp_pool->sp_rcache)
647184588Sdfr		replay_setreply(xprt->xp_pool->sp_rcache,
648184588Sdfr		    &rply, svc_getrpccaller(rqstp), NULL);
649184588Sdfr
650184588Sdfr	svc_sendreply_common(rqstp, &rply, NULL);
651177633Sdfr}
652177633Sdfr
653177633Sdfr/*
654177633Sdfr * Authentication error reply
655177633Sdfr */
656177633Sdfrvoid
657184588Sdfrsvcerr_auth(struct svc_req *rqstp, enum auth_stat why)
658177633Sdfr{
659184588Sdfr	SVCXPRT *xprt = rqstp->rq_xprt;
660177633Sdfr	struct rpc_msg rply;
661177633Sdfr
662184588Sdfr	rply.rm_xid = rqstp->rq_xid;
663177633Sdfr	rply.rm_direction = REPLY;
664177633Sdfr	rply.rm_reply.rp_stat = MSG_DENIED;
665177633Sdfr	rply.rjcted_rply.rj_stat = AUTH_ERROR;
666177633Sdfr	rply.rjcted_rply.rj_why = why;
667177633Sdfr
668184588Sdfr	if (xprt->xp_pool->sp_rcache)
669184588Sdfr		replay_setreply(xprt->xp_pool->sp_rcache,
670184588Sdfr		    &rply, svc_getrpccaller(rqstp), NULL);
671184588Sdfr
672184588Sdfr	svc_sendreply_common(rqstp, &rply, NULL);
673177633Sdfr}
674177633Sdfr
675177633Sdfr/*
676177633Sdfr * Auth too weak error reply
677177633Sdfr */
678177633Sdfrvoid
679184588Sdfrsvcerr_weakauth(struct svc_req *rqstp)
680177633Sdfr{
681177633Sdfr
682184588Sdfr	svcerr_auth(rqstp, AUTH_TOOWEAK);
683177633Sdfr}
684177633Sdfr
685177633Sdfr/*
686177633Sdfr * Program unavailable error reply
687177633Sdfr */
688177633Sdfrvoid
689184588Sdfrsvcerr_noprog(struct svc_req *rqstp)
690177633Sdfr{
691184588Sdfr	SVCXPRT *xprt = rqstp->rq_xprt;
692177633Sdfr	struct rpc_msg rply;
693177633Sdfr
694184588Sdfr	rply.rm_xid = rqstp->rq_xid;
695177633Sdfr	rply.rm_direction = REPLY;
696177633Sdfr	rply.rm_reply.rp_stat = MSG_ACCEPTED;
697184588Sdfr	rply.acpted_rply.ar_verf = rqstp->rq_verf;
698177633Sdfr	rply.acpted_rply.ar_stat = PROG_UNAVAIL;
699177633Sdfr
700184588Sdfr	if (xprt->xp_pool->sp_rcache)
701184588Sdfr		replay_setreply(xprt->xp_pool->sp_rcache,
702184588Sdfr		    &rply, svc_getrpccaller(rqstp), NULL);
703184588Sdfr
704184588Sdfr	svc_sendreply_common(rqstp, &rply, NULL);
705177633Sdfr}
706177633Sdfr
707177633Sdfr/*
708177633Sdfr * Program version mismatch error reply
709177633Sdfr */
710177633Sdfrvoid
711184588Sdfrsvcerr_progvers(struct svc_req *rqstp, rpcvers_t low_vers, rpcvers_t high_vers)
712177633Sdfr{
713184588Sdfr	SVCXPRT *xprt = rqstp->rq_xprt;
714177633Sdfr	struct rpc_msg rply;
715177633Sdfr
716184588Sdfr	rply.rm_xid = rqstp->rq_xid;
717177633Sdfr	rply.rm_direction = REPLY;
718177633Sdfr	rply.rm_reply.rp_stat = MSG_ACCEPTED;
719184588Sdfr	rply.acpted_rply.ar_verf = rqstp->rq_verf;
720177633Sdfr	rply.acpted_rply.ar_stat = PROG_MISMATCH;
721177633Sdfr	rply.acpted_rply.ar_vers.low = (uint32_t)low_vers;
722177633Sdfr	rply.acpted_rply.ar_vers.high = (uint32_t)high_vers;
723177633Sdfr
724184588Sdfr	if (xprt->xp_pool->sp_rcache)
725184588Sdfr		replay_setreply(xprt->xp_pool->sp_rcache,
726184588Sdfr		    &rply, svc_getrpccaller(rqstp), NULL);
727184588Sdfr
728184588Sdfr	svc_sendreply_common(rqstp, &rply, NULL);
729177633Sdfr}
730177633Sdfr
731184588Sdfr/*
732184588Sdfr * Allocate a new server transport structure. All fields are
733184588Sdfr * initialized to zero and xp_p3 is initialized to point at an
734184588Sdfr * extension structure to hold various flags and authentication
735184588Sdfr * parameters.
736184588Sdfr */
737184588SdfrSVCXPRT *
738184588Sdfrsvc_xprt_alloc()
739184588Sdfr{
740184588Sdfr	SVCXPRT *xprt;
741184588Sdfr	SVCXPRT_EXT *ext;
742184588Sdfr
743184588Sdfr	xprt = mem_alloc(sizeof(SVCXPRT));
744184588Sdfr	memset(xprt, 0, sizeof(SVCXPRT));
745184588Sdfr	ext = mem_alloc(sizeof(SVCXPRT_EXT));
746184588Sdfr	memset(ext, 0, sizeof(SVCXPRT_EXT));
747184588Sdfr	xprt->xp_p3 = ext;
748184588Sdfr	refcount_init(&xprt->xp_refs, 1);
749184588Sdfr
750184588Sdfr	return (xprt);
751184588Sdfr}
752184588Sdfr
753184588Sdfr/*
754184588Sdfr * Free a server transport structure.
755184588Sdfr */
756184588Sdfrvoid
757184588Sdfrsvc_xprt_free(xprt)
758184588Sdfr	SVCXPRT *xprt;
759184588Sdfr{
760184588Sdfr
761184588Sdfr	mem_free(xprt->xp_p3, sizeof(SVCXPRT_EXT));
762184588Sdfr	mem_free(xprt, sizeof(SVCXPRT));
763184588Sdfr}
764184588Sdfr
765177633Sdfr/* ******************* SERVER INPUT STUFF ******************* */
766177633Sdfr
767177633Sdfr/*
768184588Sdfr * Read RPC requests from a transport and queue them to be
769184588Sdfr * executed. We handle authentication and replay cache replies here.
770184588Sdfr * Actually dispatching the RPC is deferred till svc_executereq.
771177633Sdfr */
772184588Sdfrstatic enum xprt_stat
773184588Sdfrsvc_getreq(SVCXPRT *xprt, struct svc_req **rqstp_ret)
774177633Sdfr{
775177633Sdfr	SVCPOOL *pool = xprt->xp_pool;
776184588Sdfr	struct svc_req *r;
777177633Sdfr	struct rpc_msg msg;
778184588Sdfr	struct mbuf *args;
779177633Sdfr	enum xprt_stat stat;
780177633Sdfr
781177633Sdfr	/* now receive msgs from xprtprt (support batch calls) */
782184588Sdfr	r = malloc(sizeof(*r), M_RPC, M_WAITOK|M_ZERO);
783177633Sdfr
784184588Sdfr	msg.rm_call.cb_cred.oa_base = r->rq_credarea;
785184588Sdfr	msg.rm_call.cb_verf.oa_base = &r->rq_credarea[MAX_AUTH_BYTES];
786184588Sdfr	r->rq_clntcred = &r->rq_credarea[2*MAX_AUTH_BYTES];
787184588Sdfr	if (SVC_RECV(xprt, &msg, &r->rq_addr, &args)) {
788184588Sdfr		enum auth_stat why;
789177633Sdfr
790184588Sdfr		/*
791184588Sdfr		 * Handle replays and authenticate before queuing the
792184588Sdfr		 * request to be executed.
793184588Sdfr		 */
794184588Sdfr		SVC_ACQUIRE(xprt);
795184588Sdfr		r->rq_xprt = xprt;
796184588Sdfr		if (pool->sp_rcache) {
797184588Sdfr			struct rpc_msg repmsg;
798184588Sdfr			struct mbuf *repbody;
799184588Sdfr			enum replay_state rs;
800184588Sdfr			rs = replay_find(pool->sp_rcache, &msg,
801184588Sdfr			    svc_getrpccaller(r), &repmsg, &repbody);
802184588Sdfr			switch (rs) {
803184588Sdfr			case RS_NEW:
804184588Sdfr				break;
805184588Sdfr			case RS_DONE:
806184588Sdfr				SVC_REPLY(xprt, &repmsg, r->rq_addr,
807184588Sdfr				    repbody);
808184588Sdfr				if (r->rq_addr) {
809184588Sdfr					free(r->rq_addr, M_SONAME);
810184588Sdfr					r->rq_addr = NULL;
811184588Sdfr				}
812177633Sdfr				goto call_done;
813184588Sdfr
814184588Sdfr			default:
815184588Sdfr				goto call_done;
816177633Sdfr			}
817184588Sdfr		}
818184588Sdfr
819184588Sdfr		r->rq_xid = msg.rm_xid;
820184588Sdfr		r->rq_prog = msg.rm_call.cb_prog;
821184588Sdfr		r->rq_vers = msg.rm_call.cb_vers;
822184588Sdfr		r->rq_proc = msg.rm_call.cb_proc;
823184588Sdfr		r->rq_size = sizeof(*r) + m_length(args, NULL);
824184588Sdfr		r->rq_args = args;
825184588Sdfr		if ((why = _authenticate(r, &msg)) != AUTH_OK) {
826177633Sdfr			/*
827184588Sdfr			 * RPCSEC_GSS uses this return code
828184588Sdfr			 * for requests that form part of its
829184588Sdfr			 * context establishment protocol and
830184588Sdfr			 * should not be dispatched to the
831184588Sdfr			 * application.
832177633Sdfr			 */
833184588Sdfr			if (why != RPCSEC_GSS_NODISPATCH)
834184588Sdfr				svcerr_auth(r, why);
835184588Sdfr			goto call_done;
836177633Sdfr		}
837184588Sdfr
838184588Sdfr		if (!SVCAUTH_UNWRAP(&r->rq_auth, &r->rq_args)) {
839184588Sdfr			svcerr_decode(r);
840184588Sdfr			goto call_done;
841184588Sdfr		}
842184588Sdfr
843177633Sdfr		/*
844184588Sdfr		 * Everything checks out, return request to caller.
845177633Sdfr		 */
846184588Sdfr		*rqstp_ret = r;
847184588Sdfr		r = NULL;
848184588Sdfr	}
849177633Sdfrcall_done:
850184588Sdfr	if (r) {
851184588Sdfr		svc_freereq(r);
852184588Sdfr		r = NULL;
853184588Sdfr	}
854184588Sdfr	if ((stat = SVC_STAT(xprt)) == XPRT_DIED) {
855184588Sdfr		xprt_unregister(xprt);
856184588Sdfr	}
857184588Sdfr
858184588Sdfr	return (stat);
859184588Sdfr}
860184588Sdfr
861184588Sdfrstatic void
862184588Sdfrsvc_executereq(struct svc_req *rqstp)
863184588Sdfr{
864184588Sdfr	SVCXPRT *xprt = rqstp->rq_xprt;
865184588Sdfr	SVCPOOL *pool = xprt->xp_pool;
866184588Sdfr	int prog_found;
867184588Sdfr	rpcvers_t low_vers;
868184588Sdfr	rpcvers_t high_vers;
869184588Sdfr	struct svc_callout *s;
870184588Sdfr
871184588Sdfr	/* now match message with a registered service*/
872184588Sdfr	prog_found = FALSE;
873184588Sdfr	low_vers = (rpcvers_t) -1L;
874184588Sdfr	high_vers = (rpcvers_t) 0L;
875184588Sdfr	TAILQ_FOREACH(s, &pool->sp_callouts, sc_link) {
876184588Sdfr		if (s->sc_prog == rqstp->rq_prog) {
877184588Sdfr			if (s->sc_vers == rqstp->rq_vers) {
878184588Sdfr				/*
879184588Sdfr				 * We hand ownership of r to the
880184588Sdfr				 * dispatch method - they must call
881184588Sdfr				 * svc_freereq.
882184588Sdfr				 */
883184588Sdfr				(*s->sc_dispatch)(rqstp, xprt);
884184588Sdfr				return;
885184588Sdfr			}  /* found correct version */
886184588Sdfr			prog_found = TRUE;
887184588Sdfr			if (s->sc_vers < low_vers)
888184588Sdfr				low_vers = s->sc_vers;
889184588Sdfr			if (s->sc_vers > high_vers)
890184588Sdfr				high_vers = s->sc_vers;
891184588Sdfr		}   /* found correct program */
892184588Sdfr	}
893184588Sdfr
894184588Sdfr	/*
895184588Sdfr	 * if we got here, the program or version
896184588Sdfr	 * is not served ...
897184588Sdfr	 */
898184588Sdfr	if (prog_found)
899184588Sdfr		svcerr_progvers(rqstp, low_vers, high_vers);
900184588Sdfr	else
901184588Sdfr		svcerr_noprog(rqstp);
902184588Sdfr
903184588Sdfr	svc_freereq(rqstp);
904184588Sdfr}
905184588Sdfr
906184588Sdfrstatic void
907184588Sdfrsvc_checkidle(SVCPOOL *pool)
908184588Sdfr{
909184588Sdfr	SVCXPRT *xprt, *nxprt;
910184588Sdfr	time_t timo;
911184588Sdfr	struct svcxprt_list cleanup;
912184588Sdfr
913184588Sdfr	TAILQ_INIT(&cleanup);
914184588Sdfr	TAILQ_FOREACH_SAFE(xprt, &pool->sp_xlist, xp_link, nxprt) {
915184588Sdfr		/*
916184588Sdfr		 * Only some transports have idle timers. Don't time
917184588Sdfr		 * something out which is just waking up.
918184588Sdfr		 */
919184588Sdfr		if (!xprt->xp_idletimeout || xprt->xp_thread)
920184588Sdfr			continue;
921184588Sdfr
922184588Sdfr		timo = xprt->xp_lastactive + xprt->xp_idletimeout;
923184588Sdfr		if (time_uptime > timo) {
924184588Sdfr			xprt_unregister_locked(xprt);
925184588Sdfr			TAILQ_INSERT_TAIL(&cleanup, xprt, xp_link);
926177633Sdfr		}
927184588Sdfr	}
928184588Sdfr
929184588Sdfr	mtx_unlock(&pool->sp_lock);
930184588Sdfr	TAILQ_FOREACH_SAFE(xprt, &cleanup, xp_link, nxprt) {
931184588Sdfr		SVC_RELEASE(xprt);
932184588Sdfr	}
933184588Sdfr	mtx_lock(&pool->sp_lock);
934184588Sdfr
935177633Sdfr}
936177633Sdfr
937184588Sdfrstatic void
938184588Sdfrsvc_assign_waiting_sockets(SVCPOOL *pool)
939177633Sdfr{
940177633Sdfr	SVCXPRT *xprt;
941184588Sdfr
942184588Sdfr	TAILQ_FOREACH(xprt, &pool->sp_active, xp_alink) {
943184588Sdfr		if (!xprt->xp_thread) {
944184588Sdfr			xprt_assignthread(xprt);
945184588Sdfr		}
946184588Sdfr	}
947184588Sdfr}
948184588Sdfr
949184588Sdfrstatic bool_t
950184588Sdfrsvc_request_space_available(SVCPOOL *pool)
951184588Sdfr{
952184588Sdfr
953184588Sdfr	mtx_assert(&pool->sp_lock, MA_OWNED);
954184588Sdfr
955184588Sdfr	if (pool->sp_space_throttled) {
956184588Sdfr		/*
957184588Sdfr		 * Below the low-water yet? If so, assign any waiting sockets.
958184588Sdfr		 */
959184588Sdfr		if (pool->sp_space_used < pool->sp_space_low) {
960184588Sdfr			pool->sp_space_throttled = FALSE;
961184588Sdfr			svc_assign_waiting_sockets(pool);
962184588Sdfr			return TRUE;
963184588Sdfr		}
964184588Sdfr
965184588Sdfr		return FALSE;
966184588Sdfr	} else {
967184588Sdfr		if (pool->sp_space_used
968184588Sdfr		    >= pool->sp_space_high) {
969184588Sdfr			pool->sp_space_throttled = TRUE;
970184588Sdfr			pool->sp_space_throttle_count++;
971184588Sdfr			return FALSE;
972184588Sdfr		}
973184588Sdfr
974184588Sdfr		return TRUE;
975184588Sdfr	}
976184588Sdfr}
977184588Sdfr
978184588Sdfrstatic void
979184588Sdfrsvc_run_internal(SVCPOOL *pool, bool_t ismaster)
980184588Sdfr{
981184588Sdfr	SVCTHREAD *st, *stpref;
982184588Sdfr	SVCXPRT *xprt;
983184588Sdfr	enum xprt_stat stat;
984184588Sdfr	struct svc_req *rqstp;
985177633Sdfr	int error;
986177633Sdfr
987184588Sdfr	st = mem_alloc(sizeof(*st));
988184588Sdfr	st->st_xprt = NULL;
989184588Sdfr	STAILQ_INIT(&st->st_reqs);
990184588Sdfr	cv_init(&st->st_cond, "rpcsvc");
991184588Sdfr
992177633Sdfr	mtx_lock(&pool->sp_lock);
993184588Sdfr	LIST_INSERT_HEAD(&pool->sp_threads, st, st_link);
994177633Sdfr
995184588Sdfr	/*
996184588Sdfr	 * If we are a new thread which was spawned to cope with
997184588Sdfr	 * increased load, set the state back to SVCPOOL_ACTIVE.
998184588Sdfr	 */
999184588Sdfr	if (pool->sp_state == SVCPOOL_THREADSTARTING)
1000184588Sdfr		pool->sp_state = SVCPOOL_ACTIVE;
1001177633Sdfr
1002184588Sdfr	while (pool->sp_state != SVCPOOL_CLOSING) {
1003184588Sdfr		/*
1004184588Sdfr		 * Check for idle transports once per second.
1005184588Sdfr		 */
1006184588Sdfr		if (time_uptime > pool->sp_lastidlecheck) {
1007184588Sdfr			pool->sp_lastidlecheck = time_uptime;
1008184588Sdfr			svc_checkidle(pool);
1009184588Sdfr		}
1010184588Sdfr
1011184588Sdfr		xprt = st->st_xprt;
1012184588Sdfr		if (!xprt && STAILQ_EMPTY(&st->st_reqs)) {
1013184588Sdfr			/*
1014184588Sdfr			 * Enforce maxthreads count.
1015184588Sdfr			 */
1016184588Sdfr			if (pool->sp_threadcount > pool->sp_maxthreads)
1017177633Sdfr				break;
1018184588Sdfr
1019184588Sdfr			/*
1020184588Sdfr			 * Before sleeping, see if we can find an
1021184588Sdfr			 * active transport which isn't being serviced
1022184588Sdfr			 * by a thread.
1023184588Sdfr			 */
1024184588Sdfr			if (svc_request_space_available(pool)) {
1025184588Sdfr				TAILQ_FOREACH(xprt, &pool->sp_active,
1026184588Sdfr				    xp_alink) {
1027184588Sdfr					if (!xprt->xp_thread) {
1028184588Sdfr						SVC_ACQUIRE(xprt);
1029184588Sdfr						xprt->xp_thread = st;
1030184588Sdfr						st->st_xprt = xprt;
1031184588Sdfr						break;
1032184588Sdfr					}
1033184588Sdfr				}
1034184588Sdfr			}
1035184588Sdfr			if (st->st_xprt)
1036184588Sdfr				continue;
1037184588Sdfr
1038184588Sdfr			LIST_INSERT_HEAD(&pool->sp_idlethreads, st, st_ilink);
1039184588Sdfr			error = cv_timedwait_sig(&st->st_cond, &pool->sp_lock,
1040184588Sdfr				5 * hz);
1041184588Sdfr			LIST_REMOVE(st, st_ilink);
1042184588Sdfr
1043184588Sdfr			/*
1044184588Sdfr			 * Reduce worker thread count when idle.
1045184588Sdfr			 */
1046184588Sdfr			if (error == EWOULDBLOCK) {
1047184588Sdfr				if (!ismaster
1048184588Sdfr				    && (pool->sp_threadcount
1049184588Sdfr					> pool->sp_minthreads)
1050184588Sdfr					&& !st->st_xprt
1051184588Sdfr					&& STAILQ_EMPTY(&st->st_reqs))
1052184588Sdfr					break;
1053184588Sdfr			}
1054184588Sdfr			if (error == EWOULDBLOCK)
1055184588Sdfr				continue;
1056184588Sdfr			if (error) {
1057184588Sdfr				if (pool->sp_state != SVCPOOL_CLOSING) {
1058184588Sdfr					mtx_unlock(&pool->sp_lock);
1059184588Sdfr					svc_exit(pool);
1060184588Sdfr					mtx_lock(&pool->sp_lock);
1061184588Sdfr				}
1062184588Sdfr				break;
1063184588Sdfr			}
1064184588Sdfr
1065184588Sdfr			if (pool->sp_state == SVCPOOL_THREADWANTED) {
1066184588Sdfr				pool->sp_state = SVCPOOL_THREADSTARTING;
1067184588Sdfr				pool->sp_lastcreatetime = time_uptime;
1068184588Sdfr				mtx_unlock(&pool->sp_lock);
1069184588Sdfr				svc_new_thread(pool);
1070184588Sdfr				mtx_lock(&pool->sp_lock);
1071184588Sdfr			}
1072177633Sdfr			continue;
1073177633Sdfr		}
1074177633Sdfr
1075184588Sdfr		if (xprt) {
1076184588Sdfr			/*
1077184588Sdfr			 * Drain the transport socket and queue up any
1078184588Sdfr			 * RPCs.
1079184588Sdfr			 */
1080184588Sdfr			xprt->xp_lastactive = time_uptime;
1081184588Sdfr			stat = XPRT_IDLE;
1082184588Sdfr			do {
1083184588Sdfr				if (!svc_request_space_available(pool))
1084184588Sdfr					break;
1085184588Sdfr				rqstp = NULL;
1086184588Sdfr				mtx_unlock(&pool->sp_lock);
1087184588Sdfr				stat = svc_getreq(xprt, &rqstp);
1088184588Sdfr				mtx_lock(&pool->sp_lock);
1089184588Sdfr				if (rqstp) {
1090184588Sdfr					/*
1091184588Sdfr					 * See if the application has
1092184588Sdfr					 * a preference for some other
1093184588Sdfr					 * thread.
1094184588Sdfr					 */
1095184588Sdfr					stpref = st;
1096184588Sdfr					if (pool->sp_assign)
1097184588Sdfr						stpref = pool->sp_assign(st,
1098184588Sdfr						    rqstp);
1099184588Sdfr
1100184588Sdfr					pool->sp_space_used +=
1101184588Sdfr						rqstp->rq_size;
1102184588Sdfr					if (pool->sp_space_used
1103184588Sdfr					    > pool->sp_space_used_highest)
1104184588Sdfr						pool->sp_space_used_highest =
1105184588Sdfr							pool->sp_space_used;
1106184588Sdfr					rqstp->rq_thread = stpref;
1107184588Sdfr					STAILQ_INSERT_TAIL(&stpref->st_reqs,
1108184588Sdfr					    rqstp, rq_link);
1109184588Sdfr					stpref->st_reqcount++;
1110184588Sdfr
1111184588Sdfr					/*
1112184588Sdfr					 * If we assigned the request
1113184588Sdfr					 * to another thread, make
1114184588Sdfr					 * sure its awake and continue
1115184588Sdfr					 * reading from the
1116184588Sdfr					 * socket. Otherwise, try to
1117184588Sdfr					 * find some other thread to
1118184588Sdfr					 * read from the socket and
1119184588Sdfr					 * execute the request
1120184588Sdfr					 * immediately.
1121184588Sdfr					 */
1122184588Sdfr					if (stpref != st) {
1123184588Sdfr						cv_signal(&stpref->st_cond);
1124184588Sdfr						continue;
1125184588Sdfr					} else {
1126184588Sdfr						break;
1127184588Sdfr					}
1128184588Sdfr				}
1129184588Sdfr			} while (stat == XPRT_MOREREQS
1130184588Sdfr			    && pool->sp_state != SVCPOOL_CLOSING);
1131184588Sdfr
1132184588Sdfr			/*
1133184588Sdfr			 * Move this transport to the end of the
1134184588Sdfr			 * active list to ensure fairness when
1135184588Sdfr			 * multiple transports are active. If this was
1136184588Sdfr			 * the last queued request, svc_getreq will
1137184588Sdfr			 * end up calling xprt_inactive to remove from
1138184588Sdfr			 * the active list.
1139184588Sdfr			 */
1140184588Sdfr			xprt->xp_thread = NULL;
1141184588Sdfr			st->st_xprt = NULL;
1142184588Sdfr			if (xprt->xp_active) {
1143184588Sdfr				xprt_assignthread(xprt);
1144184588Sdfr				TAILQ_REMOVE(&pool->sp_active, xprt, xp_alink);
1145184588Sdfr				TAILQ_INSERT_TAIL(&pool->sp_active, xprt,
1146184588Sdfr				    xp_alink);
1147184588Sdfr			}
1148184588Sdfr			mtx_unlock(&pool->sp_lock);
1149184588Sdfr			SVC_RELEASE(xprt);
1150184588Sdfr			mtx_lock(&pool->sp_lock);
1151184588Sdfr		}
1152184588Sdfr
1153177633Sdfr		/*
1154184588Sdfr		 * Execute what we have queued.
1155177633Sdfr		 */
1156184588Sdfr		while ((rqstp = STAILQ_FIRST(&st->st_reqs)) != NULL) {
1157184588Sdfr			size_t sz = rqstp->rq_size;
1158184588Sdfr			mtx_unlock(&pool->sp_lock);
1159184588Sdfr			svc_executereq(rqstp);
1160184588Sdfr			mtx_lock(&pool->sp_lock);
1161184588Sdfr			pool->sp_space_used -= sz;
1162184588Sdfr		}
1163184588Sdfr	}
1164177633Sdfr
1165184588Sdfr	if (st->st_xprt) {
1166184588Sdfr		xprt = st->st_xprt;
1167184588Sdfr		st->st_xprt = NULL;
1168184588Sdfr		SVC_RELEASE(xprt);
1169177633Sdfr	}
1170177633Sdfr
1171184588Sdfr	KASSERT(STAILQ_EMPTY(&st->st_reqs), ("stray reqs on exit"));
1172184588Sdfr	LIST_REMOVE(st, st_link);
1173184588Sdfr	pool->sp_threadcount--;
1174184588Sdfr
1175177633Sdfr	mtx_unlock(&pool->sp_lock);
1176184588Sdfr
1177184588Sdfr	cv_destroy(&st->st_cond);
1178184588Sdfr	mem_free(st, sizeof(*st));
1179184588Sdfr
1180184588Sdfr	if (!ismaster)
1181184588Sdfr		wakeup(pool);
1182177633Sdfr}
1183177633Sdfr
1184184588Sdfrstatic void
1185184588Sdfrsvc_thread_start(void *arg)
1186184588Sdfr{
1187184588Sdfr
1188184588Sdfr	svc_run_internal((SVCPOOL *) arg, FALSE);
1189184588Sdfr	kthread_exit();
1190184588Sdfr}
1191184588Sdfr
1192184588Sdfrstatic void
1193184588Sdfrsvc_new_thread(SVCPOOL *pool)
1194184588Sdfr{
1195184588Sdfr	struct thread *td;
1196184588Sdfr
1197184588Sdfr	pool->sp_threadcount++;
1198184588Sdfr	kthread_add(svc_thread_start, pool,
1199184588Sdfr	    pool->sp_proc, &td, 0, 0,
1200184588Sdfr	    "%s: service", pool->sp_name);
1201184588Sdfr}
1202184588Sdfr
1203177633Sdfrvoid
1204184588Sdfrsvc_run(SVCPOOL *pool)
1205184588Sdfr{
1206184588Sdfr	int i;
1207184588Sdfr	struct proc *p;
1208184588Sdfr	struct thread *td;
1209184588Sdfr
1210184588Sdfr	p = curproc;
1211184588Sdfr	td = curthread;
1212184588Sdfr	snprintf(td->td_name, sizeof(td->td_name),
1213184588Sdfr	    "%s: master", pool->sp_name);
1214184588Sdfr	pool->sp_state = SVCPOOL_ACTIVE;
1215184588Sdfr	pool->sp_proc = p;
1216184588Sdfr	pool->sp_lastcreatetime = time_uptime;
1217184588Sdfr	pool->sp_threadcount = 1;
1218184588Sdfr
1219184588Sdfr	for (i = 1; i < pool->sp_minthreads; i++) {
1220184588Sdfr		svc_new_thread(pool);
1221184588Sdfr	}
1222184588Sdfr
1223184588Sdfr	svc_run_internal(pool, TRUE);
1224184588Sdfr
1225184588Sdfr	mtx_lock(&pool->sp_lock);
1226184588Sdfr	while (pool->sp_threadcount > 0)
1227184588Sdfr		msleep(pool, &pool->sp_lock, 0, "svcexit", 0);
1228184588Sdfr	mtx_unlock(&pool->sp_lock);
1229184588Sdfr}
1230184588Sdfr
1231184588Sdfrvoid
1232177633Sdfrsvc_exit(SVCPOOL *pool)
1233177633Sdfr{
1234184588Sdfr	SVCTHREAD *st;
1235184588Sdfr
1236177633Sdfr	mtx_lock(&pool->sp_lock);
1237184588Sdfr
1238184588Sdfr	pool->sp_state = SVCPOOL_CLOSING;
1239184588Sdfr	LIST_FOREACH(st, &pool->sp_idlethreads, st_ilink)
1240184588Sdfr		cv_signal(&st->st_cond);
1241184588Sdfr
1242177633Sdfr	mtx_unlock(&pool->sp_lock);
1243177633Sdfr}
1244184588Sdfr
1245184588Sdfrbool_t
1246184588Sdfrsvc_getargs(struct svc_req *rqstp, xdrproc_t xargs, void *args)
1247184588Sdfr{
1248184588Sdfr	struct mbuf *m;
1249184588Sdfr	XDR xdrs;
1250184588Sdfr	bool_t stat;
1251184588Sdfr
1252184588Sdfr	m = rqstp->rq_args;
1253184588Sdfr	rqstp->rq_args = NULL;
1254184588Sdfr
1255184588Sdfr	xdrmbuf_create(&xdrs, m, XDR_DECODE);
1256184588Sdfr	stat = xargs(&xdrs, args);
1257184588Sdfr	XDR_DESTROY(&xdrs);
1258184588Sdfr
1259184588Sdfr	return (stat);
1260184588Sdfr}
1261184588Sdfr
1262184588Sdfrbool_t
1263184588Sdfrsvc_freeargs(struct svc_req *rqstp, xdrproc_t xargs, void *args)
1264184588Sdfr{
1265184588Sdfr	XDR xdrs;
1266184588Sdfr
1267184588Sdfr	if (rqstp->rq_addr) {
1268184588Sdfr		free(rqstp->rq_addr, M_SONAME);
1269184588Sdfr		rqstp->rq_addr = NULL;
1270184588Sdfr	}
1271184588Sdfr
1272184588Sdfr	xdrs.x_op = XDR_FREE;
1273184588Sdfr	return (xargs(&xdrs, args));
1274184588Sdfr}
1275184588Sdfr
1276184588Sdfrvoid
1277184588Sdfrsvc_freereq(struct svc_req *rqstp)
1278184588Sdfr{
1279184588Sdfr	SVCTHREAD *st;
1280184588Sdfr	SVCXPRT *xprt;
1281184588Sdfr	SVCPOOL *pool;
1282184588Sdfr
1283184588Sdfr	st = rqstp->rq_thread;
1284184588Sdfr	xprt = rqstp->rq_xprt;
1285184588Sdfr	if (xprt)
1286184588Sdfr		pool = xprt->xp_pool;
1287184588Sdfr	else
1288184588Sdfr		pool = NULL;
1289184588Sdfr	if (st) {
1290184588Sdfr		mtx_lock(&pool->sp_lock);
1291184588Sdfr		KASSERT(rqstp == STAILQ_FIRST(&st->st_reqs),
1292184588Sdfr		    ("Freeing request out of order"));
1293184588Sdfr		STAILQ_REMOVE_HEAD(&st->st_reqs, rq_link);
1294184588Sdfr		st->st_reqcount--;
1295184588Sdfr		if (pool->sp_done)
1296184588Sdfr			pool->sp_done(st, rqstp);
1297184588Sdfr		mtx_unlock(&pool->sp_lock);
1298184588Sdfr	}
1299184588Sdfr
1300184588Sdfr	if (rqstp->rq_auth.svc_ah_ops)
1301184588Sdfr		SVCAUTH_RELEASE(&rqstp->rq_auth);
1302184588Sdfr
1303184588Sdfr	if (rqstp->rq_xprt) {
1304184588Sdfr		SVC_RELEASE(rqstp->rq_xprt);
1305184588Sdfr	}
1306184588Sdfr
1307184588Sdfr	if (rqstp->rq_addr)
1308184588Sdfr		free(rqstp->rq_addr, M_SONAME);
1309184588Sdfr
1310184588Sdfr	if (rqstp->rq_args)
1311184588Sdfr		m_freem(rqstp->rq_args);
1312184588Sdfr
1313184588Sdfr	free(rqstp, M_RPC);
1314184588Sdfr}
1315