svc.c revision 193603
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 193603 2009-06-07 01:06:56Z rmacklem $");
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	}
177193603Srmacklem	mtx_unlock(&pool->sp_lock);
178177633Sdfr
179184588Sdfr	TAILQ_FOREACH_SAFE(xprt, &cleanup, xp_link, nxprt) {
180184588Sdfr		SVC_RELEASE(xprt);
181184588Sdfr	}
182184588Sdfr
183193436Srmacklem	mtx_destroy(&pool->sp_lock);
184193436Srmacklem
185184588Sdfr	if (pool->sp_rcache)
186184588Sdfr		replay_freecache(pool->sp_rcache);
187184588Sdfr
188184588Sdfr	sysctl_ctx_free(&pool->sp_sysctl);
189177633Sdfr	free(pool, M_RPC);
190177633Sdfr}
191177633Sdfr
192184588Sdfrstatic bool_t
193184588Sdfrsvcpool_active(SVCPOOL *pool)
194184588Sdfr{
195184588Sdfr	enum svcpool_state state = pool->sp_state;
196184588Sdfr
197184588Sdfr	if (state == SVCPOOL_INIT || state == SVCPOOL_CLOSING)
198184588Sdfr		return (FALSE);
199184588Sdfr	return (TRUE);
200184588Sdfr}
201184588Sdfr
202177633Sdfr/*
203184588Sdfr * Sysctl handler to set the minimum thread count on a pool
204184588Sdfr */
205184588Sdfrstatic int
206184588Sdfrsvcpool_minthread_sysctl(SYSCTL_HANDLER_ARGS)
207184588Sdfr{
208184588Sdfr	SVCPOOL *pool;
209184588Sdfr	int newminthreads, error, n;
210184588Sdfr
211184588Sdfr	pool = oidp->oid_arg1;
212184588Sdfr	newminthreads = pool->sp_minthreads;
213184588Sdfr	error = sysctl_handle_int(oidp, &newminthreads, 0, req);
214184588Sdfr	if (error == 0 && newminthreads != pool->sp_minthreads) {
215184588Sdfr		if (newminthreads > pool->sp_maxthreads)
216184588Sdfr			return (EINVAL);
217184588Sdfr		mtx_lock(&pool->sp_lock);
218184588Sdfr		if (newminthreads > pool->sp_minthreads
219184588Sdfr		    && svcpool_active(pool)) {
220184588Sdfr			/*
221184588Sdfr			 * If the pool is running and we are
222184588Sdfr			 * increasing, create some more threads now.
223184588Sdfr			 */
224184588Sdfr			n = newminthreads - pool->sp_threadcount;
225184588Sdfr			if (n > 0) {
226184588Sdfr				mtx_unlock(&pool->sp_lock);
227184588Sdfr				while (n--)
228184588Sdfr					svc_new_thread(pool);
229184588Sdfr				mtx_lock(&pool->sp_lock);
230184588Sdfr			}
231184588Sdfr		}
232184588Sdfr		pool->sp_minthreads = newminthreads;
233184588Sdfr		mtx_unlock(&pool->sp_lock);
234184588Sdfr	}
235184588Sdfr	return (error);
236184588Sdfr}
237184588Sdfr
238184588Sdfr/*
239184588Sdfr * Sysctl handler to set the maximum thread count on a pool
240184588Sdfr */
241184588Sdfrstatic int
242184588Sdfrsvcpool_maxthread_sysctl(SYSCTL_HANDLER_ARGS)
243184588Sdfr{
244184588Sdfr	SVCPOOL *pool;
245184588Sdfr	SVCTHREAD *st;
246184588Sdfr	int newmaxthreads, error;
247184588Sdfr
248184588Sdfr	pool = oidp->oid_arg1;
249184588Sdfr	newmaxthreads = pool->sp_maxthreads;
250184588Sdfr	error = sysctl_handle_int(oidp, &newmaxthreads, 0, req);
251184588Sdfr	if (error == 0 && newmaxthreads != pool->sp_maxthreads) {
252184588Sdfr		if (newmaxthreads < pool->sp_minthreads)
253184588Sdfr			return (EINVAL);
254184588Sdfr		mtx_lock(&pool->sp_lock);
255184588Sdfr		if (newmaxthreads < pool->sp_maxthreads
256184588Sdfr		    && svcpool_active(pool)) {
257184588Sdfr			/*
258184588Sdfr			 * If the pool is running and we are
259184588Sdfr			 * decreasing, wake up some idle threads to
260184588Sdfr			 * encourage them to exit.
261184588Sdfr			 */
262184588Sdfr			LIST_FOREACH(st, &pool->sp_idlethreads, st_ilink)
263184588Sdfr				cv_signal(&st->st_cond);
264184588Sdfr		}
265184588Sdfr		pool->sp_maxthreads = newmaxthreads;
266184588Sdfr		mtx_unlock(&pool->sp_lock);
267184588Sdfr	}
268184588Sdfr	return (error);
269184588Sdfr}
270184588Sdfr
271184588Sdfr/*
272177633Sdfr * Activate a transport handle.
273177633Sdfr */
274177633Sdfrvoid
275177633Sdfrxprt_register(SVCXPRT *xprt)
276177633Sdfr{
277177633Sdfr	SVCPOOL *pool = xprt->xp_pool;
278177633Sdfr
279177633Sdfr	mtx_lock(&pool->sp_lock);
280177633Sdfr	xprt->xp_registered = TRUE;
281177633Sdfr	xprt->xp_active = FALSE;
282177633Sdfr	TAILQ_INSERT_TAIL(&pool->sp_xlist, xprt, xp_link);
283177633Sdfr	mtx_unlock(&pool->sp_lock);
284177633Sdfr}
285177633Sdfr
286177633Sdfr/*
287184588Sdfr * De-activate a transport handle. Note: the locked version doesn't
288184588Sdfr * release the transport - caller must do that after dropping the pool
289184588Sdfr * lock.
290177633Sdfr */
291177633Sdfrstatic void
292184588Sdfrxprt_unregister_locked(SVCXPRT *xprt)
293177633Sdfr{
294177633Sdfr	SVCPOOL *pool = xprt->xp_pool;
295177633Sdfr
296177633Sdfr	if (xprt->xp_active) {
297177633Sdfr		TAILQ_REMOVE(&pool->sp_active, xprt, xp_alink);
298177633Sdfr		xprt->xp_active = FALSE;
299177633Sdfr	}
300177633Sdfr	TAILQ_REMOVE(&pool->sp_xlist, xprt, xp_link);
301177633Sdfr	xprt->xp_registered = FALSE;
302184588Sdfr}
303177633Sdfr
304184588Sdfrvoid
305184588Sdfrxprt_unregister(SVCXPRT *xprt)
306184588Sdfr{
307184588Sdfr	SVCPOOL *pool = xprt->xp_pool;
308184588Sdfr
309184588Sdfr	mtx_lock(&pool->sp_lock);
310184588Sdfr	xprt_unregister_locked(xprt);
311184588Sdfr	mtx_unlock(&pool->sp_lock);
312184588Sdfr
313184588Sdfr	SVC_RELEASE(xprt);
314177633Sdfr}
315177633Sdfr
316184588Sdfrstatic void
317184588Sdfrxprt_assignthread(SVCXPRT *xprt)
318184588Sdfr{
319184588Sdfr	SVCPOOL *pool = xprt->xp_pool;
320184588Sdfr	SVCTHREAD *st;
321184588Sdfr
322184588Sdfr	/*
323184588Sdfr	 * Attempt to assign a service thread to this
324184588Sdfr	 * transport.
325184588Sdfr	 */
326184588Sdfr	LIST_FOREACH(st, &pool->sp_idlethreads, st_ilink) {
327184588Sdfr		if (st->st_xprt == NULL && STAILQ_EMPTY(&st->st_reqs))
328184588Sdfr			break;
329184588Sdfr	}
330184588Sdfr	if (st) {
331184588Sdfr		SVC_ACQUIRE(xprt);
332184588Sdfr		xprt->xp_thread = st;
333184588Sdfr		st->st_xprt = xprt;
334184588Sdfr		cv_signal(&st->st_cond);
335184588Sdfr	} else {
336184588Sdfr		/*
337184588Sdfr		 * See if we can create a new thread. The
338184588Sdfr		 * actual thread creation happens in
339184588Sdfr		 * svc_run_internal because our locking state
340184588Sdfr		 * is poorly defined (we are typically called
341184588Sdfr		 * from a socket upcall). Don't create more
342184588Sdfr		 * than one thread per second.
343184588Sdfr		 */
344184588Sdfr		if (pool->sp_state == SVCPOOL_ACTIVE
345184588Sdfr		    && pool->sp_lastcreatetime < time_uptime
346184588Sdfr		    && pool->sp_threadcount < pool->sp_maxthreads) {
347184588Sdfr			pool->sp_state = SVCPOOL_THREADWANTED;
348184588Sdfr		}
349184588Sdfr	}
350184588Sdfr}
351184588Sdfr
352177633Sdfrvoid
353177633Sdfrxprt_active(SVCXPRT *xprt)
354177633Sdfr{
355177633Sdfr	SVCPOOL *pool = xprt->xp_pool;
356177633Sdfr
357193436Srmacklem	mtx_lock(&pool->sp_lock);
358193436Srmacklem
359184588Sdfr	if (!xprt->xp_registered) {
360184588Sdfr		/*
361184588Sdfr		 * Race with xprt_unregister - we lose.
362184588Sdfr		 */
363193436Srmacklem		mtx_unlock(&pool->sp_lock);
364184588Sdfr		return;
365184588Sdfr	}
366184588Sdfr
367177633Sdfr	if (!xprt->xp_active) {
368177633Sdfr		TAILQ_INSERT_TAIL(&pool->sp_active, xprt, xp_alink);
369177633Sdfr		xprt->xp_active = TRUE;
370184588Sdfr		xprt_assignthread(xprt);
371177633Sdfr	}
372177633Sdfr
373177633Sdfr	mtx_unlock(&pool->sp_lock);
374177633Sdfr}
375177633Sdfr
376177633Sdfrvoid
377184588Sdfrxprt_inactive_locked(SVCXPRT *xprt)
378177633Sdfr{
379177633Sdfr	SVCPOOL *pool = xprt->xp_pool;
380177633Sdfr
381177633Sdfr	if (xprt->xp_active) {
382177633Sdfr		TAILQ_REMOVE(&pool->sp_active, xprt, xp_alink);
383177633Sdfr		xprt->xp_active = FALSE;
384177633Sdfr	}
385184588Sdfr}
386177633Sdfr
387184588Sdfrvoid
388184588Sdfrxprt_inactive(SVCXPRT *xprt)
389184588Sdfr{
390184588Sdfr	SVCPOOL *pool = xprt->xp_pool;
391184588Sdfr
392184588Sdfr	mtx_lock(&pool->sp_lock);
393184588Sdfr	xprt_inactive_locked(xprt);
394177633Sdfr	mtx_unlock(&pool->sp_lock);
395177633Sdfr}
396177633Sdfr
397177633Sdfr/*
398177633Sdfr * Add a service program to the callout list.
399177633Sdfr * The dispatch routine will be called when a rpc request for this
400177633Sdfr * program number comes in.
401177633Sdfr */
402177633Sdfrbool_t
403177633Sdfrsvc_reg(SVCXPRT *xprt, const rpcprog_t prog, const rpcvers_t vers,
404177633Sdfr    void (*dispatch)(struct svc_req *, SVCXPRT *),
405177633Sdfr    const struct netconfig *nconf)
406177633Sdfr{
407177633Sdfr	SVCPOOL *pool = xprt->xp_pool;
408177633Sdfr	struct svc_callout *s;
409177633Sdfr	char *netid = NULL;
410177633Sdfr	int flag = 0;
411177633Sdfr
412177633Sdfr/* VARIABLES PROTECTED BY svc_lock: s, svc_head */
413177633Sdfr
414177633Sdfr	if (xprt->xp_netid) {
415177633Sdfr		netid = strdup(xprt->xp_netid, M_RPC);
416177633Sdfr		flag = 1;
417177633Sdfr	} else if (nconf && nconf->nc_netid) {
418177633Sdfr		netid = strdup(nconf->nc_netid, M_RPC);
419177633Sdfr		flag = 1;
420177633Sdfr	} /* must have been created with svc_raw_create */
421177633Sdfr	if ((netid == NULL) && (flag == 1)) {
422177633Sdfr		return (FALSE);
423177633Sdfr	}
424177633Sdfr
425177633Sdfr	mtx_lock(&pool->sp_lock);
426177633Sdfr	if ((s = svc_find(pool, prog, vers, netid)) != NULL) {
427177633Sdfr		if (netid)
428177633Sdfr			free(netid, M_RPC);
429177633Sdfr		if (s->sc_dispatch == dispatch)
430177633Sdfr			goto rpcb_it; /* he is registering another xptr */
431177633Sdfr		mtx_unlock(&pool->sp_lock);
432177633Sdfr		return (FALSE);
433177633Sdfr	}
434177633Sdfr	s = malloc(sizeof (struct svc_callout), M_RPC, M_NOWAIT);
435177633Sdfr	if (s == NULL) {
436177633Sdfr		if (netid)
437177633Sdfr			free(netid, M_RPC);
438177633Sdfr		mtx_unlock(&pool->sp_lock);
439177633Sdfr		return (FALSE);
440177633Sdfr	}
441177633Sdfr
442177633Sdfr	s->sc_prog = prog;
443177633Sdfr	s->sc_vers = vers;
444177633Sdfr	s->sc_dispatch = dispatch;
445177633Sdfr	s->sc_netid = netid;
446177633Sdfr	TAILQ_INSERT_TAIL(&pool->sp_callouts, s, sc_link);
447177633Sdfr
448177633Sdfr	if ((xprt->xp_netid == NULL) && (flag == 1) && netid)
449177633Sdfr		((SVCXPRT *) xprt)->xp_netid = strdup(netid, M_RPC);
450177633Sdfr
451177633Sdfrrpcb_it:
452177633Sdfr	mtx_unlock(&pool->sp_lock);
453177633Sdfr	/* now register the information with the local binder service */
454177633Sdfr	if (nconf) {
455177633Sdfr		bool_t dummy;
456177633Sdfr		struct netconfig tnc;
457184588Sdfr		struct netbuf nb;
458177633Sdfr		tnc = *nconf;
459184588Sdfr		nb.buf = &xprt->xp_ltaddr;
460184588Sdfr		nb.len = xprt->xp_ltaddr.ss_len;
461184588Sdfr		dummy = rpcb_set(prog, vers, &tnc, &nb);
462177633Sdfr		return (dummy);
463177633Sdfr	}
464177633Sdfr	return (TRUE);
465177633Sdfr}
466177633Sdfr
467177633Sdfr/*
468177633Sdfr * Remove a service program from the callout list.
469177633Sdfr */
470177633Sdfrvoid
471177633Sdfrsvc_unreg(SVCPOOL *pool, const rpcprog_t prog, const rpcvers_t vers)
472177633Sdfr{
473177633Sdfr	struct svc_callout *s;
474177633Sdfr
475177633Sdfr	/* unregister the information anyway */
476177633Sdfr	(void) rpcb_unset(prog, vers, NULL);
477177633Sdfr	mtx_lock(&pool->sp_lock);
478177633Sdfr	while ((s = svc_find(pool, prog, vers, NULL)) != NULL) {
479177633Sdfr		TAILQ_REMOVE(&pool->sp_callouts, s, sc_link);
480177633Sdfr		if (s->sc_netid)
481177633Sdfr			mem_free(s->sc_netid, sizeof (s->sc_netid) + 1);
482177633Sdfr		mem_free(s, sizeof (struct svc_callout));
483177633Sdfr	}
484177633Sdfr	mtx_unlock(&pool->sp_lock);
485177633Sdfr}
486177633Sdfr
487177633Sdfr/* ********************** CALLOUT list related stuff ************* */
488177633Sdfr
489177633Sdfr/*
490177633Sdfr * Search the callout list for a program number, return the callout
491177633Sdfr * struct.
492177633Sdfr */
493177633Sdfrstatic struct svc_callout *
494177633Sdfrsvc_find(SVCPOOL *pool, rpcprog_t prog, rpcvers_t vers, char *netid)
495177633Sdfr{
496177633Sdfr	struct svc_callout *s;
497177633Sdfr
498177633Sdfr	mtx_assert(&pool->sp_lock, MA_OWNED);
499177633Sdfr	TAILQ_FOREACH(s, &pool->sp_callouts, sc_link) {
500177633Sdfr		if (s->sc_prog == prog && s->sc_vers == vers
501177633Sdfr		    && (netid == NULL || s->sc_netid == NULL ||
502177633Sdfr			strcmp(netid, s->sc_netid) == 0))
503177633Sdfr			break;
504177633Sdfr	}
505177633Sdfr
506177633Sdfr	return (s);
507177633Sdfr}
508177633Sdfr
509177633Sdfr/* ******************* REPLY GENERATION ROUTINES  ************ */
510177633Sdfr
511184588Sdfrstatic bool_t
512184588Sdfrsvc_sendreply_common(struct svc_req *rqstp, struct rpc_msg *rply,
513184588Sdfr    struct mbuf *body)
514184588Sdfr{
515184588Sdfr	SVCXPRT *xprt = rqstp->rq_xprt;
516184588Sdfr	bool_t ok;
517184588Sdfr
518184588Sdfr	if (rqstp->rq_args) {
519184588Sdfr		m_freem(rqstp->rq_args);
520184588Sdfr		rqstp->rq_args = NULL;
521184588Sdfr	}
522184588Sdfr
523184588Sdfr	if (xprt->xp_pool->sp_rcache)
524184588Sdfr		replay_setreply(xprt->xp_pool->sp_rcache,
525184588Sdfr		    rply, svc_getrpccaller(rqstp), body);
526184588Sdfr
527184588Sdfr	if (!SVCAUTH_WRAP(&rqstp->rq_auth, &body))
528184588Sdfr		return (FALSE);
529184588Sdfr
530184588Sdfr	ok = SVC_REPLY(xprt, rply, rqstp->rq_addr, body);
531184588Sdfr	if (rqstp->rq_addr) {
532184588Sdfr		free(rqstp->rq_addr, M_SONAME);
533184588Sdfr		rqstp->rq_addr = NULL;
534184588Sdfr	}
535184588Sdfr
536184588Sdfr	return (ok);
537184588Sdfr}
538184588Sdfr
539177633Sdfr/*
540177633Sdfr * Send a reply to an rpc request
541177633Sdfr */
542177633Sdfrbool_t
543184588Sdfrsvc_sendreply(struct svc_req *rqstp, xdrproc_t xdr_results, void * xdr_location)
544177633Sdfr{
545177633Sdfr	struct rpc_msg rply;
546184588Sdfr	struct mbuf *m;
547184588Sdfr	XDR xdrs;
548184588Sdfr	bool_t ok;
549177633Sdfr
550184588Sdfr	rply.rm_xid = rqstp->rq_xid;
551177633Sdfr	rply.rm_direction = REPLY;
552177633Sdfr	rply.rm_reply.rp_stat = MSG_ACCEPTED;
553184588Sdfr	rply.acpted_rply.ar_verf = rqstp->rq_verf;
554177633Sdfr	rply.acpted_rply.ar_stat = SUCCESS;
555184588Sdfr	rply.acpted_rply.ar_results.where = NULL;
556184588Sdfr	rply.acpted_rply.ar_results.proc = (xdrproc_t) xdr_void;
557177633Sdfr
558184588Sdfr	MGET(m, M_WAIT, MT_DATA);
559184588Sdfr	MCLGET(m, M_WAIT);
560184588Sdfr	m->m_len = 0;
561184588Sdfr	xdrmbuf_create(&xdrs, m, XDR_ENCODE);
562184588Sdfr	ok = xdr_results(&xdrs, xdr_location);
563184588Sdfr	XDR_DESTROY(&xdrs);
564184588Sdfr
565184588Sdfr	if (ok) {
566184588Sdfr		return (svc_sendreply_common(rqstp, &rply, m));
567184588Sdfr	} else {
568184588Sdfr		m_freem(m);
569184588Sdfr		return (FALSE);
570184588Sdfr	}
571177633Sdfr}
572177633Sdfr
573184588Sdfrbool_t
574184588Sdfrsvc_sendreply_mbuf(struct svc_req *rqstp, struct mbuf *m)
575184588Sdfr{
576184588Sdfr	struct rpc_msg rply;
577184588Sdfr
578184588Sdfr	rply.rm_xid = rqstp->rq_xid;
579184588Sdfr	rply.rm_direction = REPLY;
580184588Sdfr	rply.rm_reply.rp_stat = MSG_ACCEPTED;
581184588Sdfr	rply.acpted_rply.ar_verf = rqstp->rq_verf;
582184588Sdfr	rply.acpted_rply.ar_stat = SUCCESS;
583184588Sdfr	rply.acpted_rply.ar_results.where = NULL;
584184588Sdfr	rply.acpted_rply.ar_results.proc = (xdrproc_t) xdr_void;
585184588Sdfr
586184588Sdfr	return (svc_sendreply_common(rqstp, &rply, m));
587184588Sdfr}
588184588Sdfr
589177633Sdfr/*
590177633Sdfr * No procedure error reply
591177633Sdfr */
592177633Sdfrvoid
593184588Sdfrsvcerr_noproc(struct svc_req *rqstp)
594177633Sdfr{
595184588Sdfr	SVCXPRT *xprt = rqstp->rq_xprt;
596177633Sdfr	struct rpc_msg rply;
597177633Sdfr
598184588Sdfr	rply.rm_xid = rqstp->rq_xid;
599177633Sdfr	rply.rm_direction = REPLY;
600177633Sdfr	rply.rm_reply.rp_stat = MSG_ACCEPTED;
601184588Sdfr	rply.acpted_rply.ar_verf = rqstp->rq_verf;
602177633Sdfr	rply.acpted_rply.ar_stat = PROC_UNAVAIL;
603177633Sdfr
604184588Sdfr	if (xprt->xp_pool->sp_rcache)
605184588Sdfr		replay_setreply(xprt->xp_pool->sp_rcache,
606184588Sdfr		    &rply, svc_getrpccaller(rqstp), NULL);
607184588Sdfr
608184588Sdfr	svc_sendreply_common(rqstp, &rply, NULL);
609177633Sdfr}
610177633Sdfr
611177633Sdfr/*
612177633Sdfr * Can't decode args error reply
613177633Sdfr */
614177633Sdfrvoid
615184588Sdfrsvcerr_decode(struct svc_req *rqstp)
616177633Sdfr{
617184588Sdfr	SVCXPRT *xprt = rqstp->rq_xprt;
618177633Sdfr	struct rpc_msg rply;
619177633Sdfr
620184588Sdfr	rply.rm_xid = rqstp->rq_xid;
621177633Sdfr	rply.rm_direction = REPLY;
622177633Sdfr	rply.rm_reply.rp_stat = MSG_ACCEPTED;
623184588Sdfr	rply.acpted_rply.ar_verf = rqstp->rq_verf;
624177633Sdfr	rply.acpted_rply.ar_stat = GARBAGE_ARGS;
625177633Sdfr
626184588Sdfr	if (xprt->xp_pool->sp_rcache)
627184588Sdfr		replay_setreply(xprt->xp_pool->sp_rcache,
628184588Sdfr		    &rply, (struct sockaddr *) &xprt->xp_rtaddr, NULL);
629184588Sdfr
630184588Sdfr	svc_sendreply_common(rqstp, &rply, NULL);
631177633Sdfr}
632177633Sdfr
633177633Sdfr/*
634177633Sdfr * Some system error
635177633Sdfr */
636177633Sdfrvoid
637184588Sdfrsvcerr_systemerr(struct svc_req *rqstp)
638177633Sdfr{
639184588Sdfr	SVCXPRT *xprt = rqstp->rq_xprt;
640177633Sdfr	struct rpc_msg rply;
641177633Sdfr
642184588Sdfr	rply.rm_xid = rqstp->rq_xid;
643177633Sdfr	rply.rm_direction = REPLY;
644177633Sdfr	rply.rm_reply.rp_stat = MSG_ACCEPTED;
645184588Sdfr	rply.acpted_rply.ar_verf = rqstp->rq_verf;
646177633Sdfr	rply.acpted_rply.ar_stat = SYSTEM_ERR;
647177633Sdfr
648184588Sdfr	if (xprt->xp_pool->sp_rcache)
649184588Sdfr		replay_setreply(xprt->xp_pool->sp_rcache,
650184588Sdfr		    &rply, svc_getrpccaller(rqstp), NULL);
651184588Sdfr
652184588Sdfr	svc_sendreply_common(rqstp, &rply, NULL);
653177633Sdfr}
654177633Sdfr
655177633Sdfr/*
656177633Sdfr * Authentication error reply
657177633Sdfr */
658177633Sdfrvoid
659184588Sdfrsvcerr_auth(struct svc_req *rqstp, enum auth_stat why)
660177633Sdfr{
661184588Sdfr	SVCXPRT *xprt = rqstp->rq_xprt;
662177633Sdfr	struct rpc_msg rply;
663177633Sdfr
664184588Sdfr	rply.rm_xid = rqstp->rq_xid;
665177633Sdfr	rply.rm_direction = REPLY;
666177633Sdfr	rply.rm_reply.rp_stat = MSG_DENIED;
667177633Sdfr	rply.rjcted_rply.rj_stat = AUTH_ERROR;
668177633Sdfr	rply.rjcted_rply.rj_why = why;
669177633Sdfr
670184588Sdfr	if (xprt->xp_pool->sp_rcache)
671184588Sdfr		replay_setreply(xprt->xp_pool->sp_rcache,
672184588Sdfr		    &rply, svc_getrpccaller(rqstp), NULL);
673184588Sdfr
674184588Sdfr	svc_sendreply_common(rqstp, &rply, NULL);
675177633Sdfr}
676177633Sdfr
677177633Sdfr/*
678177633Sdfr * Auth too weak error reply
679177633Sdfr */
680177633Sdfrvoid
681184588Sdfrsvcerr_weakauth(struct svc_req *rqstp)
682177633Sdfr{
683177633Sdfr
684184588Sdfr	svcerr_auth(rqstp, AUTH_TOOWEAK);
685177633Sdfr}
686177633Sdfr
687177633Sdfr/*
688177633Sdfr * Program unavailable error reply
689177633Sdfr */
690177633Sdfrvoid
691184588Sdfrsvcerr_noprog(struct svc_req *rqstp)
692177633Sdfr{
693184588Sdfr	SVCXPRT *xprt = rqstp->rq_xprt;
694177633Sdfr	struct rpc_msg rply;
695177633Sdfr
696184588Sdfr	rply.rm_xid = rqstp->rq_xid;
697177633Sdfr	rply.rm_direction = REPLY;
698177633Sdfr	rply.rm_reply.rp_stat = MSG_ACCEPTED;
699184588Sdfr	rply.acpted_rply.ar_verf = rqstp->rq_verf;
700177633Sdfr	rply.acpted_rply.ar_stat = PROG_UNAVAIL;
701177633Sdfr
702184588Sdfr	if (xprt->xp_pool->sp_rcache)
703184588Sdfr		replay_setreply(xprt->xp_pool->sp_rcache,
704184588Sdfr		    &rply, svc_getrpccaller(rqstp), NULL);
705184588Sdfr
706184588Sdfr	svc_sendreply_common(rqstp, &rply, NULL);
707177633Sdfr}
708177633Sdfr
709177633Sdfr/*
710177633Sdfr * Program version mismatch error reply
711177633Sdfr */
712177633Sdfrvoid
713184588Sdfrsvcerr_progvers(struct svc_req *rqstp, rpcvers_t low_vers, rpcvers_t high_vers)
714177633Sdfr{
715184588Sdfr	SVCXPRT *xprt = rqstp->rq_xprt;
716177633Sdfr	struct rpc_msg rply;
717177633Sdfr
718184588Sdfr	rply.rm_xid = rqstp->rq_xid;
719177633Sdfr	rply.rm_direction = REPLY;
720177633Sdfr	rply.rm_reply.rp_stat = MSG_ACCEPTED;
721184588Sdfr	rply.acpted_rply.ar_verf = rqstp->rq_verf;
722177633Sdfr	rply.acpted_rply.ar_stat = PROG_MISMATCH;
723177633Sdfr	rply.acpted_rply.ar_vers.low = (uint32_t)low_vers;
724177633Sdfr	rply.acpted_rply.ar_vers.high = (uint32_t)high_vers;
725177633Sdfr
726184588Sdfr	if (xprt->xp_pool->sp_rcache)
727184588Sdfr		replay_setreply(xprt->xp_pool->sp_rcache,
728184588Sdfr		    &rply, svc_getrpccaller(rqstp), NULL);
729184588Sdfr
730184588Sdfr	svc_sendreply_common(rqstp, &rply, NULL);
731177633Sdfr}
732177633Sdfr
733184588Sdfr/*
734184588Sdfr * Allocate a new server transport structure. All fields are
735184588Sdfr * initialized to zero and xp_p3 is initialized to point at an
736184588Sdfr * extension structure to hold various flags and authentication
737184588Sdfr * parameters.
738184588Sdfr */
739184588SdfrSVCXPRT *
740184588Sdfrsvc_xprt_alloc()
741184588Sdfr{
742184588Sdfr	SVCXPRT *xprt;
743184588Sdfr	SVCXPRT_EXT *ext;
744184588Sdfr
745184588Sdfr	xprt = mem_alloc(sizeof(SVCXPRT));
746184588Sdfr	memset(xprt, 0, sizeof(SVCXPRT));
747184588Sdfr	ext = mem_alloc(sizeof(SVCXPRT_EXT));
748184588Sdfr	memset(ext, 0, sizeof(SVCXPRT_EXT));
749184588Sdfr	xprt->xp_p3 = ext;
750184588Sdfr	refcount_init(&xprt->xp_refs, 1);
751184588Sdfr
752184588Sdfr	return (xprt);
753184588Sdfr}
754184588Sdfr
755184588Sdfr/*
756184588Sdfr * Free a server transport structure.
757184588Sdfr */
758184588Sdfrvoid
759184588Sdfrsvc_xprt_free(xprt)
760184588Sdfr	SVCXPRT *xprt;
761184588Sdfr{
762184588Sdfr
763184588Sdfr	mem_free(xprt->xp_p3, sizeof(SVCXPRT_EXT));
764184588Sdfr	mem_free(xprt, sizeof(SVCXPRT));
765184588Sdfr}
766184588Sdfr
767177633Sdfr/* ******************* SERVER INPUT STUFF ******************* */
768177633Sdfr
769177633Sdfr/*
770184588Sdfr * Read RPC requests from a transport and queue them to be
771184588Sdfr * executed. We handle authentication and replay cache replies here.
772184588Sdfr * Actually dispatching the RPC is deferred till svc_executereq.
773177633Sdfr */
774184588Sdfrstatic enum xprt_stat
775184588Sdfrsvc_getreq(SVCXPRT *xprt, struct svc_req **rqstp_ret)
776177633Sdfr{
777177633Sdfr	SVCPOOL *pool = xprt->xp_pool;
778184588Sdfr	struct svc_req *r;
779177633Sdfr	struct rpc_msg msg;
780184588Sdfr	struct mbuf *args;
781177633Sdfr	enum xprt_stat stat;
782177633Sdfr
783177633Sdfr	/* now receive msgs from xprtprt (support batch calls) */
784184588Sdfr	r = malloc(sizeof(*r), M_RPC, M_WAITOK|M_ZERO);
785177633Sdfr
786184588Sdfr	msg.rm_call.cb_cred.oa_base = r->rq_credarea;
787184588Sdfr	msg.rm_call.cb_verf.oa_base = &r->rq_credarea[MAX_AUTH_BYTES];
788184588Sdfr	r->rq_clntcred = &r->rq_credarea[2*MAX_AUTH_BYTES];
789184588Sdfr	if (SVC_RECV(xprt, &msg, &r->rq_addr, &args)) {
790184588Sdfr		enum auth_stat why;
791177633Sdfr
792184588Sdfr		/*
793184588Sdfr		 * Handle replays and authenticate before queuing the
794184588Sdfr		 * request to be executed.
795184588Sdfr		 */
796184588Sdfr		SVC_ACQUIRE(xprt);
797184588Sdfr		r->rq_xprt = xprt;
798184588Sdfr		if (pool->sp_rcache) {
799184588Sdfr			struct rpc_msg repmsg;
800184588Sdfr			struct mbuf *repbody;
801184588Sdfr			enum replay_state rs;
802184588Sdfr			rs = replay_find(pool->sp_rcache, &msg,
803184588Sdfr			    svc_getrpccaller(r), &repmsg, &repbody);
804184588Sdfr			switch (rs) {
805184588Sdfr			case RS_NEW:
806184588Sdfr				break;
807184588Sdfr			case RS_DONE:
808184588Sdfr				SVC_REPLY(xprt, &repmsg, r->rq_addr,
809184588Sdfr				    repbody);
810184588Sdfr				if (r->rq_addr) {
811184588Sdfr					free(r->rq_addr, M_SONAME);
812184588Sdfr					r->rq_addr = NULL;
813184588Sdfr				}
814177633Sdfr				goto call_done;
815184588Sdfr
816184588Sdfr			default:
817184588Sdfr				goto call_done;
818177633Sdfr			}
819184588Sdfr		}
820184588Sdfr
821184588Sdfr		r->rq_xid = msg.rm_xid;
822184588Sdfr		r->rq_prog = msg.rm_call.cb_prog;
823184588Sdfr		r->rq_vers = msg.rm_call.cb_vers;
824184588Sdfr		r->rq_proc = msg.rm_call.cb_proc;
825184588Sdfr		r->rq_size = sizeof(*r) + m_length(args, NULL);
826184588Sdfr		r->rq_args = args;
827184588Sdfr		if ((why = _authenticate(r, &msg)) != AUTH_OK) {
828177633Sdfr			/*
829184588Sdfr			 * RPCSEC_GSS uses this return code
830184588Sdfr			 * for requests that form part of its
831184588Sdfr			 * context establishment protocol and
832184588Sdfr			 * should not be dispatched to the
833184588Sdfr			 * application.
834177633Sdfr			 */
835184588Sdfr			if (why != RPCSEC_GSS_NODISPATCH)
836184588Sdfr				svcerr_auth(r, why);
837184588Sdfr			goto call_done;
838177633Sdfr		}
839184588Sdfr
840184588Sdfr		if (!SVCAUTH_UNWRAP(&r->rq_auth, &r->rq_args)) {
841184588Sdfr			svcerr_decode(r);
842184588Sdfr			goto call_done;
843184588Sdfr		}
844184588Sdfr
845177633Sdfr		/*
846184588Sdfr		 * Everything checks out, return request to caller.
847177633Sdfr		 */
848184588Sdfr		*rqstp_ret = r;
849184588Sdfr		r = NULL;
850184588Sdfr	}
851177633Sdfrcall_done:
852184588Sdfr	if (r) {
853184588Sdfr		svc_freereq(r);
854184588Sdfr		r = NULL;
855184588Sdfr	}
856184588Sdfr	if ((stat = SVC_STAT(xprt)) == XPRT_DIED) {
857184588Sdfr		xprt_unregister(xprt);
858184588Sdfr	}
859184588Sdfr
860184588Sdfr	return (stat);
861184588Sdfr}
862184588Sdfr
863184588Sdfrstatic void
864184588Sdfrsvc_executereq(struct svc_req *rqstp)
865184588Sdfr{
866184588Sdfr	SVCXPRT *xprt = rqstp->rq_xprt;
867184588Sdfr	SVCPOOL *pool = xprt->xp_pool;
868184588Sdfr	int prog_found;
869184588Sdfr	rpcvers_t low_vers;
870184588Sdfr	rpcvers_t high_vers;
871184588Sdfr	struct svc_callout *s;
872184588Sdfr
873184588Sdfr	/* now match message with a registered service*/
874184588Sdfr	prog_found = FALSE;
875184588Sdfr	low_vers = (rpcvers_t) -1L;
876184588Sdfr	high_vers = (rpcvers_t) 0L;
877184588Sdfr	TAILQ_FOREACH(s, &pool->sp_callouts, sc_link) {
878184588Sdfr		if (s->sc_prog == rqstp->rq_prog) {
879184588Sdfr			if (s->sc_vers == rqstp->rq_vers) {
880184588Sdfr				/*
881184588Sdfr				 * We hand ownership of r to the
882184588Sdfr				 * dispatch method - they must call
883184588Sdfr				 * svc_freereq.
884184588Sdfr				 */
885184588Sdfr				(*s->sc_dispatch)(rqstp, xprt);
886184588Sdfr				return;
887184588Sdfr			}  /* found correct version */
888184588Sdfr			prog_found = TRUE;
889184588Sdfr			if (s->sc_vers < low_vers)
890184588Sdfr				low_vers = s->sc_vers;
891184588Sdfr			if (s->sc_vers > high_vers)
892184588Sdfr				high_vers = s->sc_vers;
893184588Sdfr		}   /* found correct program */
894184588Sdfr	}
895184588Sdfr
896184588Sdfr	/*
897184588Sdfr	 * if we got here, the program or version
898184588Sdfr	 * is not served ...
899184588Sdfr	 */
900184588Sdfr	if (prog_found)
901184588Sdfr		svcerr_progvers(rqstp, low_vers, high_vers);
902184588Sdfr	else
903184588Sdfr		svcerr_noprog(rqstp);
904184588Sdfr
905184588Sdfr	svc_freereq(rqstp);
906184588Sdfr}
907184588Sdfr
908184588Sdfrstatic void
909184588Sdfrsvc_checkidle(SVCPOOL *pool)
910184588Sdfr{
911184588Sdfr	SVCXPRT *xprt, *nxprt;
912184588Sdfr	time_t timo;
913184588Sdfr	struct svcxprt_list cleanup;
914184588Sdfr
915184588Sdfr	TAILQ_INIT(&cleanup);
916184588Sdfr	TAILQ_FOREACH_SAFE(xprt, &pool->sp_xlist, xp_link, nxprt) {
917184588Sdfr		/*
918184588Sdfr		 * Only some transports have idle timers. Don't time
919184588Sdfr		 * something out which is just waking up.
920184588Sdfr		 */
921184588Sdfr		if (!xprt->xp_idletimeout || xprt->xp_thread)
922184588Sdfr			continue;
923184588Sdfr
924184588Sdfr		timo = xprt->xp_lastactive + xprt->xp_idletimeout;
925184588Sdfr		if (time_uptime > timo) {
926184588Sdfr			xprt_unregister_locked(xprt);
927184588Sdfr			TAILQ_INSERT_TAIL(&cleanup, xprt, xp_link);
928177633Sdfr		}
929184588Sdfr	}
930184588Sdfr
931184588Sdfr	mtx_unlock(&pool->sp_lock);
932184588Sdfr	TAILQ_FOREACH_SAFE(xprt, &cleanup, xp_link, nxprt) {
933184588Sdfr		SVC_RELEASE(xprt);
934184588Sdfr	}
935184588Sdfr	mtx_lock(&pool->sp_lock);
936184588Sdfr
937177633Sdfr}
938177633Sdfr
939184588Sdfrstatic void
940184588Sdfrsvc_assign_waiting_sockets(SVCPOOL *pool)
941177633Sdfr{
942177633Sdfr	SVCXPRT *xprt;
943184588Sdfr
944184588Sdfr	TAILQ_FOREACH(xprt, &pool->sp_active, xp_alink) {
945184588Sdfr		if (!xprt->xp_thread) {
946184588Sdfr			xprt_assignthread(xprt);
947184588Sdfr		}
948184588Sdfr	}
949184588Sdfr}
950184588Sdfr
951184588Sdfrstatic bool_t
952184588Sdfrsvc_request_space_available(SVCPOOL *pool)
953184588Sdfr{
954184588Sdfr
955184588Sdfr	mtx_assert(&pool->sp_lock, MA_OWNED);
956184588Sdfr
957184588Sdfr	if (pool->sp_space_throttled) {
958184588Sdfr		/*
959184588Sdfr		 * Below the low-water yet? If so, assign any waiting sockets.
960184588Sdfr		 */
961184588Sdfr		if (pool->sp_space_used < pool->sp_space_low) {
962184588Sdfr			pool->sp_space_throttled = FALSE;
963184588Sdfr			svc_assign_waiting_sockets(pool);
964184588Sdfr			return TRUE;
965184588Sdfr		}
966184588Sdfr
967184588Sdfr		return FALSE;
968184588Sdfr	} else {
969184588Sdfr		if (pool->sp_space_used
970184588Sdfr		    >= pool->sp_space_high) {
971184588Sdfr			pool->sp_space_throttled = TRUE;
972184588Sdfr			pool->sp_space_throttle_count++;
973184588Sdfr			return FALSE;
974184588Sdfr		}
975184588Sdfr
976184588Sdfr		return TRUE;
977184588Sdfr	}
978184588Sdfr}
979184588Sdfr
980184588Sdfrstatic void
981184588Sdfrsvc_run_internal(SVCPOOL *pool, bool_t ismaster)
982184588Sdfr{
983184588Sdfr	SVCTHREAD *st, *stpref;
984184588Sdfr	SVCXPRT *xprt;
985184588Sdfr	enum xprt_stat stat;
986184588Sdfr	struct svc_req *rqstp;
987177633Sdfr	int error;
988177633Sdfr
989184588Sdfr	st = mem_alloc(sizeof(*st));
990184588Sdfr	st->st_xprt = NULL;
991184588Sdfr	STAILQ_INIT(&st->st_reqs);
992184588Sdfr	cv_init(&st->st_cond, "rpcsvc");
993184588Sdfr
994177633Sdfr	mtx_lock(&pool->sp_lock);
995184588Sdfr	LIST_INSERT_HEAD(&pool->sp_threads, st, st_link);
996177633Sdfr
997184588Sdfr	/*
998184588Sdfr	 * If we are a new thread which was spawned to cope with
999184588Sdfr	 * increased load, set the state back to SVCPOOL_ACTIVE.
1000184588Sdfr	 */
1001184588Sdfr	if (pool->sp_state == SVCPOOL_THREADSTARTING)
1002184588Sdfr		pool->sp_state = SVCPOOL_ACTIVE;
1003177633Sdfr
1004184588Sdfr	while (pool->sp_state != SVCPOOL_CLOSING) {
1005184588Sdfr		/*
1006184588Sdfr		 * Check for idle transports once per second.
1007184588Sdfr		 */
1008184588Sdfr		if (time_uptime > pool->sp_lastidlecheck) {
1009184588Sdfr			pool->sp_lastidlecheck = time_uptime;
1010184588Sdfr			svc_checkidle(pool);
1011184588Sdfr		}
1012184588Sdfr
1013184588Sdfr		xprt = st->st_xprt;
1014184588Sdfr		if (!xprt && STAILQ_EMPTY(&st->st_reqs)) {
1015184588Sdfr			/*
1016184588Sdfr			 * Enforce maxthreads count.
1017184588Sdfr			 */
1018184588Sdfr			if (pool->sp_threadcount > pool->sp_maxthreads)
1019177633Sdfr				break;
1020184588Sdfr
1021184588Sdfr			/*
1022184588Sdfr			 * Before sleeping, see if we can find an
1023184588Sdfr			 * active transport which isn't being serviced
1024184588Sdfr			 * by a thread.
1025184588Sdfr			 */
1026184588Sdfr			if (svc_request_space_available(pool)) {
1027184588Sdfr				TAILQ_FOREACH(xprt, &pool->sp_active,
1028184588Sdfr				    xp_alink) {
1029184588Sdfr					if (!xprt->xp_thread) {
1030184588Sdfr						SVC_ACQUIRE(xprt);
1031184588Sdfr						xprt->xp_thread = st;
1032184588Sdfr						st->st_xprt = xprt;
1033184588Sdfr						break;
1034184588Sdfr					}
1035184588Sdfr				}
1036184588Sdfr			}
1037184588Sdfr			if (st->st_xprt)
1038184588Sdfr				continue;
1039184588Sdfr
1040184588Sdfr			LIST_INSERT_HEAD(&pool->sp_idlethreads, st, st_ilink);
1041184588Sdfr			error = cv_timedwait_sig(&st->st_cond, &pool->sp_lock,
1042184588Sdfr				5 * hz);
1043184588Sdfr			LIST_REMOVE(st, st_ilink);
1044184588Sdfr
1045184588Sdfr			/*
1046184588Sdfr			 * Reduce worker thread count when idle.
1047184588Sdfr			 */
1048184588Sdfr			if (error == EWOULDBLOCK) {
1049184588Sdfr				if (!ismaster
1050184588Sdfr				    && (pool->sp_threadcount
1051184588Sdfr					> pool->sp_minthreads)
1052184588Sdfr					&& !st->st_xprt
1053184588Sdfr					&& STAILQ_EMPTY(&st->st_reqs))
1054184588Sdfr					break;
1055184588Sdfr			}
1056184588Sdfr			if (error == EWOULDBLOCK)
1057184588Sdfr				continue;
1058184588Sdfr			if (error) {
1059184588Sdfr				if (pool->sp_state != SVCPOOL_CLOSING) {
1060184588Sdfr					mtx_unlock(&pool->sp_lock);
1061184588Sdfr					svc_exit(pool);
1062184588Sdfr					mtx_lock(&pool->sp_lock);
1063184588Sdfr				}
1064184588Sdfr				break;
1065184588Sdfr			}
1066184588Sdfr
1067184588Sdfr			if (pool->sp_state == SVCPOOL_THREADWANTED) {
1068184588Sdfr				pool->sp_state = SVCPOOL_THREADSTARTING;
1069184588Sdfr				pool->sp_lastcreatetime = time_uptime;
1070184588Sdfr				mtx_unlock(&pool->sp_lock);
1071184588Sdfr				svc_new_thread(pool);
1072184588Sdfr				mtx_lock(&pool->sp_lock);
1073184588Sdfr			}
1074177633Sdfr			continue;
1075177633Sdfr		}
1076177633Sdfr
1077184588Sdfr		if (xprt) {
1078184588Sdfr			/*
1079184588Sdfr			 * Drain the transport socket and queue up any
1080184588Sdfr			 * RPCs.
1081184588Sdfr			 */
1082184588Sdfr			xprt->xp_lastactive = time_uptime;
1083184588Sdfr			stat = XPRT_IDLE;
1084184588Sdfr			do {
1085184588Sdfr				if (!svc_request_space_available(pool))
1086184588Sdfr					break;
1087184588Sdfr				rqstp = NULL;
1088184588Sdfr				mtx_unlock(&pool->sp_lock);
1089184588Sdfr				stat = svc_getreq(xprt, &rqstp);
1090184588Sdfr				mtx_lock(&pool->sp_lock);
1091184588Sdfr				if (rqstp) {
1092184588Sdfr					/*
1093184588Sdfr					 * See if the application has
1094184588Sdfr					 * a preference for some other
1095184588Sdfr					 * thread.
1096184588Sdfr					 */
1097184588Sdfr					stpref = st;
1098184588Sdfr					if (pool->sp_assign)
1099184588Sdfr						stpref = pool->sp_assign(st,
1100184588Sdfr						    rqstp);
1101184588Sdfr
1102184588Sdfr					pool->sp_space_used +=
1103184588Sdfr						rqstp->rq_size;
1104184588Sdfr					if (pool->sp_space_used
1105184588Sdfr					    > pool->sp_space_used_highest)
1106184588Sdfr						pool->sp_space_used_highest =
1107184588Sdfr							pool->sp_space_used;
1108184588Sdfr					rqstp->rq_thread = stpref;
1109184588Sdfr					STAILQ_INSERT_TAIL(&stpref->st_reqs,
1110184588Sdfr					    rqstp, rq_link);
1111184588Sdfr					stpref->st_reqcount++;
1112184588Sdfr
1113184588Sdfr					/*
1114184588Sdfr					 * If we assigned the request
1115184588Sdfr					 * to another thread, make
1116184588Sdfr					 * sure its awake and continue
1117184588Sdfr					 * reading from the
1118184588Sdfr					 * socket. Otherwise, try to
1119184588Sdfr					 * find some other thread to
1120184588Sdfr					 * read from the socket and
1121184588Sdfr					 * execute the request
1122184588Sdfr					 * immediately.
1123184588Sdfr					 */
1124184588Sdfr					if (stpref != st) {
1125184588Sdfr						cv_signal(&stpref->st_cond);
1126184588Sdfr						continue;
1127184588Sdfr					} else {
1128184588Sdfr						break;
1129184588Sdfr					}
1130184588Sdfr				}
1131184588Sdfr			} while (stat == XPRT_MOREREQS
1132184588Sdfr			    && pool->sp_state != SVCPOOL_CLOSING);
1133184588Sdfr
1134184588Sdfr			/*
1135184588Sdfr			 * Move this transport to the end of the
1136184588Sdfr			 * active list to ensure fairness when
1137184588Sdfr			 * multiple transports are active. If this was
1138184588Sdfr			 * the last queued request, svc_getreq will
1139184588Sdfr			 * end up calling xprt_inactive to remove from
1140184588Sdfr			 * the active list.
1141184588Sdfr			 */
1142184588Sdfr			xprt->xp_thread = NULL;
1143184588Sdfr			st->st_xprt = NULL;
1144184588Sdfr			if (xprt->xp_active) {
1145184588Sdfr				xprt_assignthread(xprt);
1146184588Sdfr				TAILQ_REMOVE(&pool->sp_active, xprt, xp_alink);
1147184588Sdfr				TAILQ_INSERT_TAIL(&pool->sp_active, xprt,
1148184588Sdfr				    xp_alink);
1149184588Sdfr			}
1150184588Sdfr			mtx_unlock(&pool->sp_lock);
1151184588Sdfr			SVC_RELEASE(xprt);
1152184588Sdfr			mtx_lock(&pool->sp_lock);
1153184588Sdfr		}
1154184588Sdfr
1155177633Sdfr		/*
1156184588Sdfr		 * Execute what we have queued.
1157177633Sdfr		 */
1158184588Sdfr		while ((rqstp = STAILQ_FIRST(&st->st_reqs)) != NULL) {
1159184588Sdfr			size_t sz = rqstp->rq_size;
1160184588Sdfr			mtx_unlock(&pool->sp_lock);
1161184588Sdfr			svc_executereq(rqstp);
1162184588Sdfr			mtx_lock(&pool->sp_lock);
1163184588Sdfr			pool->sp_space_used -= sz;
1164184588Sdfr		}
1165184588Sdfr	}
1166177633Sdfr
1167184588Sdfr	if (st->st_xprt) {
1168184588Sdfr		xprt = st->st_xprt;
1169184588Sdfr		st->st_xprt = NULL;
1170184588Sdfr		SVC_RELEASE(xprt);
1171177633Sdfr	}
1172177633Sdfr
1173184588Sdfr	KASSERT(STAILQ_EMPTY(&st->st_reqs), ("stray reqs on exit"));
1174184588Sdfr	LIST_REMOVE(st, st_link);
1175184588Sdfr	pool->sp_threadcount--;
1176184588Sdfr
1177177633Sdfr	mtx_unlock(&pool->sp_lock);
1178184588Sdfr
1179184588Sdfr	cv_destroy(&st->st_cond);
1180184588Sdfr	mem_free(st, sizeof(*st));
1181184588Sdfr
1182184588Sdfr	if (!ismaster)
1183184588Sdfr		wakeup(pool);
1184177633Sdfr}
1185177633Sdfr
1186184588Sdfrstatic void
1187184588Sdfrsvc_thread_start(void *arg)
1188184588Sdfr{
1189184588Sdfr
1190184588Sdfr	svc_run_internal((SVCPOOL *) arg, FALSE);
1191184588Sdfr	kthread_exit();
1192184588Sdfr}
1193184588Sdfr
1194184588Sdfrstatic void
1195184588Sdfrsvc_new_thread(SVCPOOL *pool)
1196184588Sdfr{
1197184588Sdfr	struct thread *td;
1198184588Sdfr
1199184588Sdfr	pool->sp_threadcount++;
1200184588Sdfr	kthread_add(svc_thread_start, pool,
1201184588Sdfr	    pool->sp_proc, &td, 0, 0,
1202184588Sdfr	    "%s: service", pool->sp_name);
1203184588Sdfr}
1204184588Sdfr
1205177633Sdfrvoid
1206184588Sdfrsvc_run(SVCPOOL *pool)
1207184588Sdfr{
1208184588Sdfr	int i;
1209184588Sdfr	struct proc *p;
1210184588Sdfr	struct thread *td;
1211184588Sdfr
1212184588Sdfr	p = curproc;
1213184588Sdfr	td = curthread;
1214184588Sdfr	snprintf(td->td_name, sizeof(td->td_name),
1215184588Sdfr	    "%s: master", pool->sp_name);
1216184588Sdfr	pool->sp_state = SVCPOOL_ACTIVE;
1217184588Sdfr	pool->sp_proc = p;
1218184588Sdfr	pool->sp_lastcreatetime = time_uptime;
1219184588Sdfr	pool->sp_threadcount = 1;
1220184588Sdfr
1221184588Sdfr	for (i = 1; i < pool->sp_minthreads; i++) {
1222184588Sdfr		svc_new_thread(pool);
1223184588Sdfr	}
1224184588Sdfr
1225184588Sdfr	svc_run_internal(pool, TRUE);
1226184588Sdfr
1227184588Sdfr	mtx_lock(&pool->sp_lock);
1228184588Sdfr	while (pool->sp_threadcount > 0)
1229184588Sdfr		msleep(pool, &pool->sp_lock, 0, "svcexit", 0);
1230184588Sdfr	mtx_unlock(&pool->sp_lock);
1231184588Sdfr}
1232184588Sdfr
1233184588Sdfrvoid
1234177633Sdfrsvc_exit(SVCPOOL *pool)
1235177633Sdfr{
1236184588Sdfr	SVCTHREAD *st;
1237184588Sdfr
1238177633Sdfr	mtx_lock(&pool->sp_lock);
1239184588Sdfr
1240184588Sdfr	pool->sp_state = SVCPOOL_CLOSING;
1241184588Sdfr	LIST_FOREACH(st, &pool->sp_idlethreads, st_ilink)
1242184588Sdfr		cv_signal(&st->st_cond);
1243184588Sdfr
1244177633Sdfr	mtx_unlock(&pool->sp_lock);
1245177633Sdfr}
1246184588Sdfr
1247184588Sdfrbool_t
1248184588Sdfrsvc_getargs(struct svc_req *rqstp, xdrproc_t xargs, void *args)
1249184588Sdfr{
1250184588Sdfr	struct mbuf *m;
1251184588Sdfr	XDR xdrs;
1252184588Sdfr	bool_t stat;
1253184588Sdfr
1254184588Sdfr	m = rqstp->rq_args;
1255184588Sdfr	rqstp->rq_args = NULL;
1256184588Sdfr
1257184588Sdfr	xdrmbuf_create(&xdrs, m, XDR_DECODE);
1258184588Sdfr	stat = xargs(&xdrs, args);
1259184588Sdfr	XDR_DESTROY(&xdrs);
1260184588Sdfr
1261184588Sdfr	return (stat);
1262184588Sdfr}
1263184588Sdfr
1264184588Sdfrbool_t
1265184588Sdfrsvc_freeargs(struct svc_req *rqstp, xdrproc_t xargs, void *args)
1266184588Sdfr{
1267184588Sdfr	XDR xdrs;
1268184588Sdfr
1269184588Sdfr	if (rqstp->rq_addr) {
1270184588Sdfr		free(rqstp->rq_addr, M_SONAME);
1271184588Sdfr		rqstp->rq_addr = NULL;
1272184588Sdfr	}
1273184588Sdfr
1274184588Sdfr	xdrs.x_op = XDR_FREE;
1275184588Sdfr	return (xargs(&xdrs, args));
1276184588Sdfr}
1277184588Sdfr
1278184588Sdfrvoid
1279184588Sdfrsvc_freereq(struct svc_req *rqstp)
1280184588Sdfr{
1281184588Sdfr	SVCTHREAD *st;
1282184588Sdfr	SVCXPRT *xprt;
1283184588Sdfr	SVCPOOL *pool;
1284184588Sdfr
1285184588Sdfr	st = rqstp->rq_thread;
1286184588Sdfr	xprt = rqstp->rq_xprt;
1287184588Sdfr	if (xprt)
1288184588Sdfr		pool = xprt->xp_pool;
1289184588Sdfr	else
1290184588Sdfr		pool = NULL;
1291184588Sdfr	if (st) {
1292184588Sdfr		mtx_lock(&pool->sp_lock);
1293184588Sdfr		KASSERT(rqstp == STAILQ_FIRST(&st->st_reqs),
1294184588Sdfr		    ("Freeing request out of order"));
1295184588Sdfr		STAILQ_REMOVE_HEAD(&st->st_reqs, rq_link);
1296184588Sdfr		st->st_reqcount--;
1297184588Sdfr		if (pool->sp_done)
1298184588Sdfr			pool->sp_done(st, rqstp);
1299184588Sdfr		mtx_unlock(&pool->sp_lock);
1300184588Sdfr	}
1301184588Sdfr
1302184588Sdfr	if (rqstp->rq_auth.svc_ah_ops)
1303184588Sdfr		SVCAUTH_RELEASE(&rqstp->rq_auth);
1304184588Sdfr
1305184588Sdfr	if (rqstp->rq_xprt) {
1306184588Sdfr		SVC_RELEASE(rqstp->rq_xprt);
1307184588Sdfr	}
1308184588Sdfr
1309184588Sdfr	if (rqstp->rq_addr)
1310184588Sdfr		free(rqstp->rq_addr, M_SONAME);
1311184588Sdfr
1312184588Sdfr	if (rqstp->rq_args)
1313184588Sdfr		m_freem(rqstp->rq_args);
1314184588Sdfr
1315184588Sdfr	free(rqstp, M_RPC);
1316184588Sdfr}
1317