svc.c revision 193436
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 193436 2009-06-04 14:13:06Z 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	}
177177633Sdfr
178184588Sdfr	TAILQ_FOREACH_SAFE(xprt, &cleanup, xp_link, nxprt) {
179184588Sdfr		SVC_RELEASE(xprt);
180184588Sdfr	}
181184588Sdfr
182193436Srmacklem	mtx_destroy(&pool->sp_lock);
183193436Srmacklem
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
356193436Srmacklem	mtx_lock(&pool->sp_lock);
357193436Srmacklem
358184588Sdfr	if (!xprt->xp_registered) {
359184588Sdfr		/*
360184588Sdfr		 * Race with xprt_unregister - we lose.
361184588Sdfr		 */
362193436Srmacklem		mtx_unlock(&pool->sp_lock);
363184588Sdfr		return;
364184588Sdfr	}
365184588Sdfr
366177633Sdfr	if (!xprt->xp_active) {
367177633Sdfr		TAILQ_INSERT_TAIL(&pool->sp_active, xprt, xp_alink);
368177633Sdfr		xprt->xp_active = TRUE;
369184588Sdfr		xprt_assignthread(xprt);
370177633Sdfr	}
371177633Sdfr
372177633Sdfr	mtx_unlock(&pool->sp_lock);
373177633Sdfr}
374177633Sdfr
375177633Sdfrvoid
376184588Sdfrxprt_inactive_locked(SVCXPRT *xprt)
377177633Sdfr{
378177633Sdfr	SVCPOOL *pool = xprt->xp_pool;
379177633Sdfr
380177633Sdfr	if (xprt->xp_active) {
381177633Sdfr		TAILQ_REMOVE(&pool->sp_active, xprt, xp_alink);
382177633Sdfr		xprt->xp_active = FALSE;
383177633Sdfr	}
384184588Sdfr}
385177633Sdfr
386184588Sdfrvoid
387184588Sdfrxprt_inactive(SVCXPRT *xprt)
388184588Sdfr{
389184588Sdfr	SVCPOOL *pool = xprt->xp_pool;
390184588Sdfr
391184588Sdfr	mtx_lock(&pool->sp_lock);
392184588Sdfr	xprt_inactive_locked(xprt);
393177633Sdfr	mtx_unlock(&pool->sp_lock);
394177633Sdfr}
395177633Sdfr
396177633Sdfr/*
397177633Sdfr * Add a service program to the callout list.
398177633Sdfr * The dispatch routine will be called when a rpc request for this
399177633Sdfr * program number comes in.
400177633Sdfr */
401177633Sdfrbool_t
402177633Sdfrsvc_reg(SVCXPRT *xprt, const rpcprog_t prog, const rpcvers_t vers,
403177633Sdfr    void (*dispatch)(struct svc_req *, SVCXPRT *),
404177633Sdfr    const struct netconfig *nconf)
405177633Sdfr{
406177633Sdfr	SVCPOOL *pool = xprt->xp_pool;
407177633Sdfr	struct svc_callout *s;
408177633Sdfr	char *netid = NULL;
409177633Sdfr	int flag = 0;
410177633Sdfr
411177633Sdfr/* VARIABLES PROTECTED BY svc_lock: s, svc_head */
412177633Sdfr
413177633Sdfr	if (xprt->xp_netid) {
414177633Sdfr		netid = strdup(xprt->xp_netid, M_RPC);
415177633Sdfr		flag = 1;
416177633Sdfr	} else if (nconf && nconf->nc_netid) {
417177633Sdfr		netid = strdup(nconf->nc_netid, M_RPC);
418177633Sdfr		flag = 1;
419177633Sdfr	} /* must have been created with svc_raw_create */
420177633Sdfr	if ((netid == NULL) && (flag == 1)) {
421177633Sdfr		return (FALSE);
422177633Sdfr	}
423177633Sdfr
424177633Sdfr	mtx_lock(&pool->sp_lock);
425177633Sdfr	if ((s = svc_find(pool, prog, vers, netid)) != NULL) {
426177633Sdfr		if (netid)
427177633Sdfr			free(netid, M_RPC);
428177633Sdfr		if (s->sc_dispatch == dispatch)
429177633Sdfr			goto rpcb_it; /* he is registering another xptr */
430177633Sdfr		mtx_unlock(&pool->sp_lock);
431177633Sdfr		return (FALSE);
432177633Sdfr	}
433177633Sdfr	s = malloc(sizeof (struct svc_callout), M_RPC, M_NOWAIT);
434177633Sdfr	if (s == NULL) {
435177633Sdfr		if (netid)
436177633Sdfr			free(netid, M_RPC);
437177633Sdfr		mtx_unlock(&pool->sp_lock);
438177633Sdfr		return (FALSE);
439177633Sdfr	}
440177633Sdfr
441177633Sdfr	s->sc_prog = prog;
442177633Sdfr	s->sc_vers = vers;
443177633Sdfr	s->sc_dispatch = dispatch;
444177633Sdfr	s->sc_netid = netid;
445177633Sdfr	TAILQ_INSERT_TAIL(&pool->sp_callouts, s, sc_link);
446177633Sdfr
447177633Sdfr	if ((xprt->xp_netid == NULL) && (flag == 1) && netid)
448177633Sdfr		((SVCXPRT *) xprt)->xp_netid = strdup(netid, M_RPC);
449177633Sdfr
450177633Sdfrrpcb_it:
451177633Sdfr	mtx_unlock(&pool->sp_lock);
452177633Sdfr	/* now register the information with the local binder service */
453177633Sdfr	if (nconf) {
454177633Sdfr		bool_t dummy;
455177633Sdfr		struct netconfig tnc;
456184588Sdfr		struct netbuf nb;
457177633Sdfr		tnc = *nconf;
458184588Sdfr		nb.buf = &xprt->xp_ltaddr;
459184588Sdfr		nb.len = xprt->xp_ltaddr.ss_len;
460184588Sdfr		dummy = rpcb_set(prog, vers, &tnc, &nb);
461177633Sdfr		return (dummy);
462177633Sdfr	}
463177633Sdfr	return (TRUE);
464177633Sdfr}
465177633Sdfr
466177633Sdfr/*
467177633Sdfr * Remove a service program from the callout list.
468177633Sdfr */
469177633Sdfrvoid
470177633Sdfrsvc_unreg(SVCPOOL *pool, const rpcprog_t prog, const rpcvers_t vers)
471177633Sdfr{
472177633Sdfr	struct svc_callout *s;
473177633Sdfr
474177633Sdfr	/* unregister the information anyway */
475177633Sdfr	(void) rpcb_unset(prog, vers, NULL);
476177633Sdfr	mtx_lock(&pool->sp_lock);
477177633Sdfr	while ((s = svc_find(pool, prog, vers, NULL)) != NULL) {
478177633Sdfr		TAILQ_REMOVE(&pool->sp_callouts, s, sc_link);
479177633Sdfr		if (s->sc_netid)
480177633Sdfr			mem_free(s->sc_netid, sizeof (s->sc_netid) + 1);
481177633Sdfr		mem_free(s, sizeof (struct svc_callout));
482177633Sdfr	}
483177633Sdfr	mtx_unlock(&pool->sp_lock);
484177633Sdfr}
485177633Sdfr
486177633Sdfr/* ********************** CALLOUT list related stuff ************* */
487177633Sdfr
488177633Sdfr/*
489177633Sdfr * Search the callout list for a program number, return the callout
490177633Sdfr * struct.
491177633Sdfr */
492177633Sdfrstatic struct svc_callout *
493177633Sdfrsvc_find(SVCPOOL *pool, rpcprog_t prog, rpcvers_t vers, char *netid)
494177633Sdfr{
495177633Sdfr	struct svc_callout *s;
496177633Sdfr
497177633Sdfr	mtx_assert(&pool->sp_lock, MA_OWNED);
498177633Sdfr	TAILQ_FOREACH(s, &pool->sp_callouts, sc_link) {
499177633Sdfr		if (s->sc_prog == prog && s->sc_vers == vers
500177633Sdfr		    && (netid == NULL || s->sc_netid == NULL ||
501177633Sdfr			strcmp(netid, s->sc_netid) == 0))
502177633Sdfr			break;
503177633Sdfr	}
504177633Sdfr
505177633Sdfr	return (s);
506177633Sdfr}
507177633Sdfr
508177633Sdfr/* ******************* REPLY GENERATION ROUTINES  ************ */
509177633Sdfr
510184588Sdfrstatic bool_t
511184588Sdfrsvc_sendreply_common(struct svc_req *rqstp, struct rpc_msg *rply,
512184588Sdfr    struct mbuf *body)
513184588Sdfr{
514184588Sdfr	SVCXPRT *xprt = rqstp->rq_xprt;
515184588Sdfr	bool_t ok;
516184588Sdfr
517184588Sdfr	if (rqstp->rq_args) {
518184588Sdfr		m_freem(rqstp->rq_args);
519184588Sdfr		rqstp->rq_args = NULL;
520184588Sdfr	}
521184588Sdfr
522184588Sdfr	if (xprt->xp_pool->sp_rcache)
523184588Sdfr		replay_setreply(xprt->xp_pool->sp_rcache,
524184588Sdfr		    rply, svc_getrpccaller(rqstp), body);
525184588Sdfr
526184588Sdfr	if (!SVCAUTH_WRAP(&rqstp->rq_auth, &body))
527184588Sdfr		return (FALSE);
528184588Sdfr
529184588Sdfr	ok = SVC_REPLY(xprt, rply, rqstp->rq_addr, body);
530184588Sdfr	if (rqstp->rq_addr) {
531184588Sdfr		free(rqstp->rq_addr, M_SONAME);
532184588Sdfr		rqstp->rq_addr = NULL;
533184588Sdfr	}
534184588Sdfr
535184588Sdfr	return (ok);
536184588Sdfr}
537184588Sdfr
538177633Sdfr/*
539177633Sdfr * Send a reply to an rpc request
540177633Sdfr */
541177633Sdfrbool_t
542184588Sdfrsvc_sendreply(struct svc_req *rqstp, xdrproc_t xdr_results, void * xdr_location)
543177633Sdfr{
544177633Sdfr	struct rpc_msg rply;
545184588Sdfr	struct mbuf *m;
546184588Sdfr	XDR xdrs;
547184588Sdfr	bool_t ok;
548177633Sdfr
549184588Sdfr	rply.rm_xid = rqstp->rq_xid;
550177633Sdfr	rply.rm_direction = REPLY;
551177633Sdfr	rply.rm_reply.rp_stat = MSG_ACCEPTED;
552184588Sdfr	rply.acpted_rply.ar_verf = rqstp->rq_verf;
553177633Sdfr	rply.acpted_rply.ar_stat = SUCCESS;
554184588Sdfr	rply.acpted_rply.ar_results.where = NULL;
555184588Sdfr	rply.acpted_rply.ar_results.proc = (xdrproc_t) xdr_void;
556177633Sdfr
557184588Sdfr	MGET(m, M_WAIT, MT_DATA);
558184588Sdfr	MCLGET(m, M_WAIT);
559184588Sdfr	m->m_len = 0;
560184588Sdfr	xdrmbuf_create(&xdrs, m, XDR_ENCODE);
561184588Sdfr	ok = xdr_results(&xdrs, xdr_location);
562184588Sdfr	XDR_DESTROY(&xdrs);
563184588Sdfr
564184588Sdfr	if (ok) {
565184588Sdfr		return (svc_sendreply_common(rqstp, &rply, m));
566184588Sdfr	} else {
567184588Sdfr		m_freem(m);
568184588Sdfr		return (FALSE);
569184588Sdfr	}
570177633Sdfr}
571177633Sdfr
572184588Sdfrbool_t
573184588Sdfrsvc_sendreply_mbuf(struct svc_req *rqstp, struct mbuf *m)
574184588Sdfr{
575184588Sdfr	struct rpc_msg rply;
576184588Sdfr
577184588Sdfr	rply.rm_xid = rqstp->rq_xid;
578184588Sdfr	rply.rm_direction = REPLY;
579184588Sdfr	rply.rm_reply.rp_stat = MSG_ACCEPTED;
580184588Sdfr	rply.acpted_rply.ar_verf = rqstp->rq_verf;
581184588Sdfr	rply.acpted_rply.ar_stat = SUCCESS;
582184588Sdfr	rply.acpted_rply.ar_results.where = NULL;
583184588Sdfr	rply.acpted_rply.ar_results.proc = (xdrproc_t) xdr_void;
584184588Sdfr
585184588Sdfr	return (svc_sendreply_common(rqstp, &rply, m));
586184588Sdfr}
587184588Sdfr
588177633Sdfr/*
589177633Sdfr * No procedure error reply
590177633Sdfr */
591177633Sdfrvoid
592184588Sdfrsvcerr_noproc(struct svc_req *rqstp)
593177633Sdfr{
594184588Sdfr	SVCXPRT *xprt = rqstp->rq_xprt;
595177633Sdfr	struct rpc_msg rply;
596177633Sdfr
597184588Sdfr	rply.rm_xid = rqstp->rq_xid;
598177633Sdfr	rply.rm_direction = REPLY;
599177633Sdfr	rply.rm_reply.rp_stat = MSG_ACCEPTED;
600184588Sdfr	rply.acpted_rply.ar_verf = rqstp->rq_verf;
601177633Sdfr	rply.acpted_rply.ar_stat = PROC_UNAVAIL;
602177633Sdfr
603184588Sdfr	if (xprt->xp_pool->sp_rcache)
604184588Sdfr		replay_setreply(xprt->xp_pool->sp_rcache,
605184588Sdfr		    &rply, svc_getrpccaller(rqstp), NULL);
606184588Sdfr
607184588Sdfr	svc_sendreply_common(rqstp, &rply, NULL);
608177633Sdfr}
609177633Sdfr
610177633Sdfr/*
611177633Sdfr * Can't decode args error reply
612177633Sdfr */
613177633Sdfrvoid
614184588Sdfrsvcerr_decode(struct svc_req *rqstp)
615177633Sdfr{
616184588Sdfr	SVCXPRT *xprt = rqstp->rq_xprt;
617177633Sdfr	struct rpc_msg rply;
618177633Sdfr
619184588Sdfr	rply.rm_xid = rqstp->rq_xid;
620177633Sdfr	rply.rm_direction = REPLY;
621177633Sdfr	rply.rm_reply.rp_stat = MSG_ACCEPTED;
622184588Sdfr	rply.acpted_rply.ar_verf = rqstp->rq_verf;
623177633Sdfr	rply.acpted_rply.ar_stat = GARBAGE_ARGS;
624177633Sdfr
625184588Sdfr	if (xprt->xp_pool->sp_rcache)
626184588Sdfr		replay_setreply(xprt->xp_pool->sp_rcache,
627184588Sdfr		    &rply, (struct sockaddr *) &xprt->xp_rtaddr, NULL);
628184588Sdfr
629184588Sdfr	svc_sendreply_common(rqstp, &rply, NULL);
630177633Sdfr}
631177633Sdfr
632177633Sdfr/*
633177633Sdfr * Some system error
634177633Sdfr */
635177633Sdfrvoid
636184588Sdfrsvcerr_systemerr(struct svc_req *rqstp)
637177633Sdfr{
638184588Sdfr	SVCXPRT *xprt = rqstp->rq_xprt;
639177633Sdfr	struct rpc_msg rply;
640177633Sdfr
641184588Sdfr	rply.rm_xid = rqstp->rq_xid;
642177633Sdfr	rply.rm_direction = REPLY;
643177633Sdfr	rply.rm_reply.rp_stat = MSG_ACCEPTED;
644184588Sdfr	rply.acpted_rply.ar_verf = rqstp->rq_verf;
645177633Sdfr	rply.acpted_rply.ar_stat = SYSTEM_ERR;
646177633Sdfr
647184588Sdfr	if (xprt->xp_pool->sp_rcache)
648184588Sdfr		replay_setreply(xprt->xp_pool->sp_rcache,
649184588Sdfr		    &rply, svc_getrpccaller(rqstp), NULL);
650184588Sdfr
651184588Sdfr	svc_sendreply_common(rqstp, &rply, NULL);
652177633Sdfr}
653177633Sdfr
654177633Sdfr/*
655177633Sdfr * Authentication error reply
656177633Sdfr */
657177633Sdfrvoid
658184588Sdfrsvcerr_auth(struct svc_req *rqstp, enum auth_stat why)
659177633Sdfr{
660184588Sdfr	SVCXPRT *xprt = rqstp->rq_xprt;
661177633Sdfr	struct rpc_msg rply;
662177633Sdfr
663184588Sdfr	rply.rm_xid = rqstp->rq_xid;
664177633Sdfr	rply.rm_direction = REPLY;
665177633Sdfr	rply.rm_reply.rp_stat = MSG_DENIED;
666177633Sdfr	rply.rjcted_rply.rj_stat = AUTH_ERROR;
667177633Sdfr	rply.rjcted_rply.rj_why = why;
668177633Sdfr
669184588Sdfr	if (xprt->xp_pool->sp_rcache)
670184588Sdfr		replay_setreply(xprt->xp_pool->sp_rcache,
671184588Sdfr		    &rply, svc_getrpccaller(rqstp), NULL);
672184588Sdfr
673184588Sdfr	svc_sendreply_common(rqstp, &rply, NULL);
674177633Sdfr}
675177633Sdfr
676177633Sdfr/*
677177633Sdfr * Auth too weak error reply
678177633Sdfr */
679177633Sdfrvoid
680184588Sdfrsvcerr_weakauth(struct svc_req *rqstp)
681177633Sdfr{
682177633Sdfr
683184588Sdfr	svcerr_auth(rqstp, AUTH_TOOWEAK);
684177633Sdfr}
685177633Sdfr
686177633Sdfr/*
687177633Sdfr * Program unavailable error reply
688177633Sdfr */
689177633Sdfrvoid
690184588Sdfrsvcerr_noprog(struct svc_req *rqstp)
691177633Sdfr{
692184588Sdfr	SVCXPRT *xprt = rqstp->rq_xprt;
693177633Sdfr	struct rpc_msg rply;
694177633Sdfr
695184588Sdfr	rply.rm_xid = rqstp->rq_xid;
696177633Sdfr	rply.rm_direction = REPLY;
697177633Sdfr	rply.rm_reply.rp_stat = MSG_ACCEPTED;
698184588Sdfr	rply.acpted_rply.ar_verf = rqstp->rq_verf;
699177633Sdfr	rply.acpted_rply.ar_stat = PROG_UNAVAIL;
700177633Sdfr
701184588Sdfr	if (xprt->xp_pool->sp_rcache)
702184588Sdfr		replay_setreply(xprt->xp_pool->sp_rcache,
703184588Sdfr		    &rply, svc_getrpccaller(rqstp), NULL);
704184588Sdfr
705184588Sdfr	svc_sendreply_common(rqstp, &rply, NULL);
706177633Sdfr}
707177633Sdfr
708177633Sdfr/*
709177633Sdfr * Program version mismatch error reply
710177633Sdfr */
711177633Sdfrvoid
712184588Sdfrsvcerr_progvers(struct svc_req *rqstp, rpcvers_t low_vers, rpcvers_t high_vers)
713177633Sdfr{
714184588Sdfr	SVCXPRT *xprt = rqstp->rq_xprt;
715177633Sdfr	struct rpc_msg rply;
716177633Sdfr
717184588Sdfr	rply.rm_xid = rqstp->rq_xid;
718177633Sdfr	rply.rm_direction = REPLY;
719177633Sdfr	rply.rm_reply.rp_stat = MSG_ACCEPTED;
720184588Sdfr	rply.acpted_rply.ar_verf = rqstp->rq_verf;
721177633Sdfr	rply.acpted_rply.ar_stat = PROG_MISMATCH;
722177633Sdfr	rply.acpted_rply.ar_vers.low = (uint32_t)low_vers;
723177633Sdfr	rply.acpted_rply.ar_vers.high = (uint32_t)high_vers;
724177633Sdfr
725184588Sdfr	if (xprt->xp_pool->sp_rcache)
726184588Sdfr		replay_setreply(xprt->xp_pool->sp_rcache,
727184588Sdfr		    &rply, svc_getrpccaller(rqstp), NULL);
728184588Sdfr
729184588Sdfr	svc_sendreply_common(rqstp, &rply, NULL);
730177633Sdfr}
731177633Sdfr
732184588Sdfr/*
733184588Sdfr * Allocate a new server transport structure. All fields are
734184588Sdfr * initialized to zero and xp_p3 is initialized to point at an
735184588Sdfr * extension structure to hold various flags and authentication
736184588Sdfr * parameters.
737184588Sdfr */
738184588SdfrSVCXPRT *
739184588Sdfrsvc_xprt_alloc()
740184588Sdfr{
741184588Sdfr	SVCXPRT *xprt;
742184588Sdfr	SVCXPRT_EXT *ext;
743184588Sdfr
744184588Sdfr	xprt = mem_alloc(sizeof(SVCXPRT));
745184588Sdfr	memset(xprt, 0, sizeof(SVCXPRT));
746184588Sdfr	ext = mem_alloc(sizeof(SVCXPRT_EXT));
747184588Sdfr	memset(ext, 0, sizeof(SVCXPRT_EXT));
748184588Sdfr	xprt->xp_p3 = ext;
749184588Sdfr	refcount_init(&xprt->xp_refs, 1);
750184588Sdfr
751184588Sdfr	return (xprt);
752184588Sdfr}
753184588Sdfr
754184588Sdfr/*
755184588Sdfr * Free a server transport structure.
756184588Sdfr */
757184588Sdfrvoid
758184588Sdfrsvc_xprt_free(xprt)
759184588Sdfr	SVCXPRT *xprt;
760184588Sdfr{
761184588Sdfr
762184588Sdfr	mem_free(xprt->xp_p3, sizeof(SVCXPRT_EXT));
763184588Sdfr	mem_free(xprt, sizeof(SVCXPRT));
764184588Sdfr}
765184588Sdfr
766177633Sdfr/* ******************* SERVER INPUT STUFF ******************* */
767177633Sdfr
768177633Sdfr/*
769184588Sdfr * Read RPC requests from a transport and queue them to be
770184588Sdfr * executed. We handle authentication and replay cache replies here.
771184588Sdfr * Actually dispatching the RPC is deferred till svc_executereq.
772177633Sdfr */
773184588Sdfrstatic enum xprt_stat
774184588Sdfrsvc_getreq(SVCXPRT *xprt, struct svc_req **rqstp_ret)
775177633Sdfr{
776177633Sdfr	SVCPOOL *pool = xprt->xp_pool;
777184588Sdfr	struct svc_req *r;
778177633Sdfr	struct rpc_msg msg;
779184588Sdfr	struct mbuf *args;
780177633Sdfr	enum xprt_stat stat;
781177633Sdfr
782177633Sdfr	/* now receive msgs from xprtprt (support batch calls) */
783184588Sdfr	r = malloc(sizeof(*r), M_RPC, M_WAITOK|M_ZERO);
784177633Sdfr
785184588Sdfr	msg.rm_call.cb_cred.oa_base = r->rq_credarea;
786184588Sdfr	msg.rm_call.cb_verf.oa_base = &r->rq_credarea[MAX_AUTH_BYTES];
787184588Sdfr	r->rq_clntcred = &r->rq_credarea[2*MAX_AUTH_BYTES];
788184588Sdfr	if (SVC_RECV(xprt, &msg, &r->rq_addr, &args)) {
789184588Sdfr		enum auth_stat why;
790177633Sdfr
791184588Sdfr		/*
792184588Sdfr		 * Handle replays and authenticate before queuing the
793184588Sdfr		 * request to be executed.
794184588Sdfr		 */
795184588Sdfr		SVC_ACQUIRE(xprt);
796184588Sdfr		r->rq_xprt = xprt;
797184588Sdfr		if (pool->sp_rcache) {
798184588Sdfr			struct rpc_msg repmsg;
799184588Sdfr			struct mbuf *repbody;
800184588Sdfr			enum replay_state rs;
801184588Sdfr			rs = replay_find(pool->sp_rcache, &msg,
802184588Sdfr			    svc_getrpccaller(r), &repmsg, &repbody);
803184588Sdfr			switch (rs) {
804184588Sdfr			case RS_NEW:
805184588Sdfr				break;
806184588Sdfr			case RS_DONE:
807184588Sdfr				SVC_REPLY(xprt, &repmsg, r->rq_addr,
808184588Sdfr				    repbody);
809184588Sdfr				if (r->rq_addr) {
810184588Sdfr					free(r->rq_addr, M_SONAME);
811184588Sdfr					r->rq_addr = NULL;
812184588Sdfr				}
813177633Sdfr				goto call_done;
814184588Sdfr
815184588Sdfr			default:
816184588Sdfr				goto call_done;
817177633Sdfr			}
818184588Sdfr		}
819184588Sdfr
820184588Sdfr		r->rq_xid = msg.rm_xid;
821184588Sdfr		r->rq_prog = msg.rm_call.cb_prog;
822184588Sdfr		r->rq_vers = msg.rm_call.cb_vers;
823184588Sdfr		r->rq_proc = msg.rm_call.cb_proc;
824184588Sdfr		r->rq_size = sizeof(*r) + m_length(args, NULL);
825184588Sdfr		r->rq_args = args;
826184588Sdfr		if ((why = _authenticate(r, &msg)) != AUTH_OK) {
827177633Sdfr			/*
828184588Sdfr			 * RPCSEC_GSS uses this return code
829184588Sdfr			 * for requests that form part of its
830184588Sdfr			 * context establishment protocol and
831184588Sdfr			 * should not be dispatched to the
832184588Sdfr			 * application.
833177633Sdfr			 */
834184588Sdfr			if (why != RPCSEC_GSS_NODISPATCH)
835184588Sdfr				svcerr_auth(r, why);
836184588Sdfr			goto call_done;
837177633Sdfr		}
838184588Sdfr
839184588Sdfr		if (!SVCAUTH_UNWRAP(&r->rq_auth, &r->rq_args)) {
840184588Sdfr			svcerr_decode(r);
841184588Sdfr			goto call_done;
842184588Sdfr		}
843184588Sdfr
844177633Sdfr		/*
845184588Sdfr		 * Everything checks out, return request to caller.
846177633Sdfr		 */
847184588Sdfr		*rqstp_ret = r;
848184588Sdfr		r = NULL;
849184588Sdfr	}
850177633Sdfrcall_done:
851184588Sdfr	if (r) {
852184588Sdfr		svc_freereq(r);
853184588Sdfr		r = NULL;
854184588Sdfr	}
855184588Sdfr	if ((stat = SVC_STAT(xprt)) == XPRT_DIED) {
856184588Sdfr		xprt_unregister(xprt);
857184588Sdfr	}
858184588Sdfr
859184588Sdfr	return (stat);
860184588Sdfr}
861184588Sdfr
862184588Sdfrstatic void
863184588Sdfrsvc_executereq(struct svc_req *rqstp)
864184588Sdfr{
865184588Sdfr	SVCXPRT *xprt = rqstp->rq_xprt;
866184588Sdfr	SVCPOOL *pool = xprt->xp_pool;
867184588Sdfr	int prog_found;
868184588Sdfr	rpcvers_t low_vers;
869184588Sdfr	rpcvers_t high_vers;
870184588Sdfr	struct svc_callout *s;
871184588Sdfr
872184588Sdfr	/* now match message with a registered service*/
873184588Sdfr	prog_found = FALSE;
874184588Sdfr	low_vers = (rpcvers_t) -1L;
875184588Sdfr	high_vers = (rpcvers_t) 0L;
876184588Sdfr	TAILQ_FOREACH(s, &pool->sp_callouts, sc_link) {
877184588Sdfr		if (s->sc_prog == rqstp->rq_prog) {
878184588Sdfr			if (s->sc_vers == rqstp->rq_vers) {
879184588Sdfr				/*
880184588Sdfr				 * We hand ownership of r to the
881184588Sdfr				 * dispatch method - they must call
882184588Sdfr				 * svc_freereq.
883184588Sdfr				 */
884184588Sdfr				(*s->sc_dispatch)(rqstp, xprt);
885184588Sdfr				return;
886184588Sdfr			}  /* found correct version */
887184588Sdfr			prog_found = TRUE;
888184588Sdfr			if (s->sc_vers < low_vers)
889184588Sdfr				low_vers = s->sc_vers;
890184588Sdfr			if (s->sc_vers > high_vers)
891184588Sdfr				high_vers = s->sc_vers;
892184588Sdfr		}   /* found correct program */
893184588Sdfr	}
894184588Sdfr
895184588Sdfr	/*
896184588Sdfr	 * if we got here, the program or version
897184588Sdfr	 * is not served ...
898184588Sdfr	 */
899184588Sdfr	if (prog_found)
900184588Sdfr		svcerr_progvers(rqstp, low_vers, high_vers);
901184588Sdfr	else
902184588Sdfr		svcerr_noprog(rqstp);
903184588Sdfr
904184588Sdfr	svc_freereq(rqstp);
905184588Sdfr}
906184588Sdfr
907184588Sdfrstatic void
908184588Sdfrsvc_checkidle(SVCPOOL *pool)
909184588Sdfr{
910184588Sdfr	SVCXPRT *xprt, *nxprt;
911184588Sdfr	time_t timo;
912184588Sdfr	struct svcxprt_list cleanup;
913184588Sdfr
914184588Sdfr	TAILQ_INIT(&cleanup);
915184588Sdfr	TAILQ_FOREACH_SAFE(xprt, &pool->sp_xlist, xp_link, nxprt) {
916184588Sdfr		/*
917184588Sdfr		 * Only some transports have idle timers. Don't time
918184588Sdfr		 * something out which is just waking up.
919184588Sdfr		 */
920184588Sdfr		if (!xprt->xp_idletimeout || xprt->xp_thread)
921184588Sdfr			continue;
922184588Sdfr
923184588Sdfr		timo = xprt->xp_lastactive + xprt->xp_idletimeout;
924184588Sdfr		if (time_uptime > timo) {
925184588Sdfr			xprt_unregister_locked(xprt);
926184588Sdfr			TAILQ_INSERT_TAIL(&cleanup, xprt, xp_link);
927177633Sdfr		}
928184588Sdfr	}
929184588Sdfr
930184588Sdfr	mtx_unlock(&pool->sp_lock);
931184588Sdfr	TAILQ_FOREACH_SAFE(xprt, &cleanup, xp_link, nxprt) {
932184588Sdfr		SVC_RELEASE(xprt);
933184588Sdfr	}
934184588Sdfr	mtx_lock(&pool->sp_lock);
935184588Sdfr
936177633Sdfr}
937177633Sdfr
938184588Sdfrstatic void
939184588Sdfrsvc_assign_waiting_sockets(SVCPOOL *pool)
940177633Sdfr{
941177633Sdfr	SVCXPRT *xprt;
942184588Sdfr
943184588Sdfr	TAILQ_FOREACH(xprt, &pool->sp_active, xp_alink) {
944184588Sdfr		if (!xprt->xp_thread) {
945184588Sdfr			xprt_assignthread(xprt);
946184588Sdfr		}
947184588Sdfr	}
948184588Sdfr}
949184588Sdfr
950184588Sdfrstatic bool_t
951184588Sdfrsvc_request_space_available(SVCPOOL *pool)
952184588Sdfr{
953184588Sdfr
954184588Sdfr	mtx_assert(&pool->sp_lock, MA_OWNED);
955184588Sdfr
956184588Sdfr	if (pool->sp_space_throttled) {
957184588Sdfr		/*
958184588Sdfr		 * Below the low-water yet? If so, assign any waiting sockets.
959184588Sdfr		 */
960184588Sdfr		if (pool->sp_space_used < pool->sp_space_low) {
961184588Sdfr			pool->sp_space_throttled = FALSE;
962184588Sdfr			svc_assign_waiting_sockets(pool);
963184588Sdfr			return TRUE;
964184588Sdfr		}
965184588Sdfr
966184588Sdfr		return FALSE;
967184588Sdfr	} else {
968184588Sdfr		if (pool->sp_space_used
969184588Sdfr		    >= pool->sp_space_high) {
970184588Sdfr			pool->sp_space_throttled = TRUE;
971184588Sdfr			pool->sp_space_throttle_count++;
972184588Sdfr			return FALSE;
973184588Sdfr		}
974184588Sdfr
975184588Sdfr		return TRUE;
976184588Sdfr	}
977184588Sdfr}
978184588Sdfr
979184588Sdfrstatic void
980184588Sdfrsvc_run_internal(SVCPOOL *pool, bool_t ismaster)
981184588Sdfr{
982184588Sdfr	SVCTHREAD *st, *stpref;
983184588Sdfr	SVCXPRT *xprt;
984184588Sdfr	enum xprt_stat stat;
985184588Sdfr	struct svc_req *rqstp;
986177633Sdfr	int error;
987177633Sdfr
988184588Sdfr	st = mem_alloc(sizeof(*st));
989184588Sdfr	st->st_xprt = NULL;
990184588Sdfr	STAILQ_INIT(&st->st_reqs);
991184588Sdfr	cv_init(&st->st_cond, "rpcsvc");
992184588Sdfr
993177633Sdfr	mtx_lock(&pool->sp_lock);
994184588Sdfr	LIST_INSERT_HEAD(&pool->sp_threads, st, st_link);
995177633Sdfr
996184588Sdfr	/*
997184588Sdfr	 * If we are a new thread which was spawned to cope with
998184588Sdfr	 * increased load, set the state back to SVCPOOL_ACTIVE.
999184588Sdfr	 */
1000184588Sdfr	if (pool->sp_state == SVCPOOL_THREADSTARTING)
1001184588Sdfr		pool->sp_state = SVCPOOL_ACTIVE;
1002177633Sdfr
1003184588Sdfr	while (pool->sp_state != SVCPOOL_CLOSING) {
1004184588Sdfr		/*
1005184588Sdfr		 * Check for idle transports once per second.
1006184588Sdfr		 */
1007184588Sdfr		if (time_uptime > pool->sp_lastidlecheck) {
1008184588Sdfr			pool->sp_lastidlecheck = time_uptime;
1009184588Sdfr			svc_checkidle(pool);
1010184588Sdfr		}
1011184588Sdfr
1012184588Sdfr		xprt = st->st_xprt;
1013184588Sdfr		if (!xprt && STAILQ_EMPTY(&st->st_reqs)) {
1014184588Sdfr			/*
1015184588Sdfr			 * Enforce maxthreads count.
1016184588Sdfr			 */
1017184588Sdfr			if (pool->sp_threadcount > pool->sp_maxthreads)
1018177633Sdfr				break;
1019184588Sdfr
1020184588Sdfr			/*
1021184588Sdfr			 * Before sleeping, see if we can find an
1022184588Sdfr			 * active transport which isn't being serviced
1023184588Sdfr			 * by a thread.
1024184588Sdfr			 */
1025184588Sdfr			if (svc_request_space_available(pool)) {
1026184588Sdfr				TAILQ_FOREACH(xprt, &pool->sp_active,
1027184588Sdfr				    xp_alink) {
1028184588Sdfr					if (!xprt->xp_thread) {
1029184588Sdfr						SVC_ACQUIRE(xprt);
1030184588Sdfr						xprt->xp_thread = st;
1031184588Sdfr						st->st_xprt = xprt;
1032184588Sdfr						break;
1033184588Sdfr					}
1034184588Sdfr				}
1035184588Sdfr			}
1036184588Sdfr			if (st->st_xprt)
1037184588Sdfr				continue;
1038184588Sdfr
1039184588Sdfr			LIST_INSERT_HEAD(&pool->sp_idlethreads, st, st_ilink);
1040184588Sdfr			error = cv_timedwait_sig(&st->st_cond, &pool->sp_lock,
1041184588Sdfr				5 * hz);
1042184588Sdfr			LIST_REMOVE(st, st_ilink);
1043184588Sdfr
1044184588Sdfr			/*
1045184588Sdfr			 * Reduce worker thread count when idle.
1046184588Sdfr			 */
1047184588Sdfr			if (error == EWOULDBLOCK) {
1048184588Sdfr				if (!ismaster
1049184588Sdfr				    && (pool->sp_threadcount
1050184588Sdfr					> pool->sp_minthreads)
1051184588Sdfr					&& !st->st_xprt
1052184588Sdfr					&& STAILQ_EMPTY(&st->st_reqs))
1053184588Sdfr					break;
1054184588Sdfr			}
1055184588Sdfr			if (error == EWOULDBLOCK)
1056184588Sdfr				continue;
1057184588Sdfr			if (error) {
1058184588Sdfr				if (pool->sp_state != SVCPOOL_CLOSING) {
1059184588Sdfr					mtx_unlock(&pool->sp_lock);
1060184588Sdfr					svc_exit(pool);
1061184588Sdfr					mtx_lock(&pool->sp_lock);
1062184588Sdfr				}
1063184588Sdfr				break;
1064184588Sdfr			}
1065184588Sdfr
1066184588Sdfr			if (pool->sp_state == SVCPOOL_THREADWANTED) {
1067184588Sdfr				pool->sp_state = SVCPOOL_THREADSTARTING;
1068184588Sdfr				pool->sp_lastcreatetime = time_uptime;
1069184588Sdfr				mtx_unlock(&pool->sp_lock);
1070184588Sdfr				svc_new_thread(pool);
1071184588Sdfr				mtx_lock(&pool->sp_lock);
1072184588Sdfr			}
1073177633Sdfr			continue;
1074177633Sdfr		}
1075177633Sdfr
1076184588Sdfr		if (xprt) {
1077184588Sdfr			/*
1078184588Sdfr			 * Drain the transport socket and queue up any
1079184588Sdfr			 * RPCs.
1080184588Sdfr			 */
1081184588Sdfr			xprt->xp_lastactive = time_uptime;
1082184588Sdfr			stat = XPRT_IDLE;
1083184588Sdfr			do {
1084184588Sdfr				if (!svc_request_space_available(pool))
1085184588Sdfr					break;
1086184588Sdfr				rqstp = NULL;
1087184588Sdfr				mtx_unlock(&pool->sp_lock);
1088184588Sdfr				stat = svc_getreq(xprt, &rqstp);
1089184588Sdfr				mtx_lock(&pool->sp_lock);
1090184588Sdfr				if (rqstp) {
1091184588Sdfr					/*
1092184588Sdfr					 * See if the application has
1093184588Sdfr					 * a preference for some other
1094184588Sdfr					 * thread.
1095184588Sdfr					 */
1096184588Sdfr					stpref = st;
1097184588Sdfr					if (pool->sp_assign)
1098184588Sdfr						stpref = pool->sp_assign(st,
1099184588Sdfr						    rqstp);
1100184588Sdfr
1101184588Sdfr					pool->sp_space_used +=
1102184588Sdfr						rqstp->rq_size;
1103184588Sdfr					if (pool->sp_space_used
1104184588Sdfr					    > pool->sp_space_used_highest)
1105184588Sdfr						pool->sp_space_used_highest =
1106184588Sdfr							pool->sp_space_used;
1107184588Sdfr					rqstp->rq_thread = stpref;
1108184588Sdfr					STAILQ_INSERT_TAIL(&stpref->st_reqs,
1109184588Sdfr					    rqstp, rq_link);
1110184588Sdfr					stpref->st_reqcount++;
1111184588Sdfr
1112184588Sdfr					/*
1113184588Sdfr					 * If we assigned the request
1114184588Sdfr					 * to another thread, make
1115184588Sdfr					 * sure its awake and continue
1116184588Sdfr					 * reading from the
1117184588Sdfr					 * socket. Otherwise, try to
1118184588Sdfr					 * find some other thread to
1119184588Sdfr					 * read from the socket and
1120184588Sdfr					 * execute the request
1121184588Sdfr					 * immediately.
1122184588Sdfr					 */
1123184588Sdfr					if (stpref != st) {
1124184588Sdfr						cv_signal(&stpref->st_cond);
1125184588Sdfr						continue;
1126184588Sdfr					} else {
1127184588Sdfr						break;
1128184588Sdfr					}
1129184588Sdfr				}
1130184588Sdfr			} while (stat == XPRT_MOREREQS
1131184588Sdfr			    && pool->sp_state != SVCPOOL_CLOSING);
1132184588Sdfr
1133184588Sdfr			/*
1134184588Sdfr			 * Move this transport to the end of the
1135184588Sdfr			 * active list to ensure fairness when
1136184588Sdfr			 * multiple transports are active. If this was
1137184588Sdfr			 * the last queued request, svc_getreq will
1138184588Sdfr			 * end up calling xprt_inactive to remove from
1139184588Sdfr			 * the active list.
1140184588Sdfr			 */
1141184588Sdfr			xprt->xp_thread = NULL;
1142184588Sdfr			st->st_xprt = NULL;
1143184588Sdfr			if (xprt->xp_active) {
1144184588Sdfr				xprt_assignthread(xprt);
1145184588Sdfr				TAILQ_REMOVE(&pool->sp_active, xprt, xp_alink);
1146184588Sdfr				TAILQ_INSERT_TAIL(&pool->sp_active, xprt,
1147184588Sdfr				    xp_alink);
1148184588Sdfr			}
1149184588Sdfr			mtx_unlock(&pool->sp_lock);
1150184588Sdfr			SVC_RELEASE(xprt);
1151184588Sdfr			mtx_lock(&pool->sp_lock);
1152184588Sdfr		}
1153184588Sdfr
1154177633Sdfr		/*
1155184588Sdfr		 * Execute what we have queued.
1156177633Sdfr		 */
1157184588Sdfr		while ((rqstp = STAILQ_FIRST(&st->st_reqs)) != NULL) {
1158184588Sdfr			size_t sz = rqstp->rq_size;
1159184588Sdfr			mtx_unlock(&pool->sp_lock);
1160184588Sdfr			svc_executereq(rqstp);
1161184588Sdfr			mtx_lock(&pool->sp_lock);
1162184588Sdfr			pool->sp_space_used -= sz;
1163184588Sdfr		}
1164184588Sdfr	}
1165177633Sdfr
1166184588Sdfr	if (st->st_xprt) {
1167184588Sdfr		xprt = st->st_xprt;
1168184588Sdfr		st->st_xprt = NULL;
1169184588Sdfr		SVC_RELEASE(xprt);
1170177633Sdfr	}
1171177633Sdfr
1172184588Sdfr	KASSERT(STAILQ_EMPTY(&st->st_reqs), ("stray reqs on exit"));
1173184588Sdfr	LIST_REMOVE(st, st_link);
1174184588Sdfr	pool->sp_threadcount--;
1175184588Sdfr
1176177633Sdfr	mtx_unlock(&pool->sp_lock);
1177184588Sdfr
1178184588Sdfr	cv_destroy(&st->st_cond);
1179184588Sdfr	mem_free(st, sizeof(*st));
1180184588Sdfr
1181184588Sdfr	if (!ismaster)
1182184588Sdfr		wakeup(pool);
1183177633Sdfr}
1184177633Sdfr
1185184588Sdfrstatic void
1186184588Sdfrsvc_thread_start(void *arg)
1187184588Sdfr{
1188184588Sdfr
1189184588Sdfr	svc_run_internal((SVCPOOL *) arg, FALSE);
1190184588Sdfr	kthread_exit();
1191184588Sdfr}
1192184588Sdfr
1193184588Sdfrstatic void
1194184588Sdfrsvc_new_thread(SVCPOOL *pool)
1195184588Sdfr{
1196184588Sdfr	struct thread *td;
1197184588Sdfr
1198184588Sdfr	pool->sp_threadcount++;
1199184588Sdfr	kthread_add(svc_thread_start, pool,
1200184588Sdfr	    pool->sp_proc, &td, 0, 0,
1201184588Sdfr	    "%s: service", pool->sp_name);
1202184588Sdfr}
1203184588Sdfr
1204177633Sdfrvoid
1205184588Sdfrsvc_run(SVCPOOL *pool)
1206184588Sdfr{
1207184588Sdfr	int i;
1208184588Sdfr	struct proc *p;
1209184588Sdfr	struct thread *td;
1210184588Sdfr
1211184588Sdfr	p = curproc;
1212184588Sdfr	td = curthread;
1213184588Sdfr	snprintf(td->td_name, sizeof(td->td_name),
1214184588Sdfr	    "%s: master", pool->sp_name);
1215184588Sdfr	pool->sp_state = SVCPOOL_ACTIVE;
1216184588Sdfr	pool->sp_proc = p;
1217184588Sdfr	pool->sp_lastcreatetime = time_uptime;
1218184588Sdfr	pool->sp_threadcount = 1;
1219184588Sdfr
1220184588Sdfr	for (i = 1; i < pool->sp_minthreads; i++) {
1221184588Sdfr		svc_new_thread(pool);
1222184588Sdfr	}
1223184588Sdfr
1224184588Sdfr	svc_run_internal(pool, TRUE);
1225184588Sdfr
1226184588Sdfr	mtx_lock(&pool->sp_lock);
1227184588Sdfr	while (pool->sp_threadcount > 0)
1228184588Sdfr		msleep(pool, &pool->sp_lock, 0, "svcexit", 0);
1229184588Sdfr	mtx_unlock(&pool->sp_lock);
1230184588Sdfr}
1231184588Sdfr
1232184588Sdfrvoid
1233177633Sdfrsvc_exit(SVCPOOL *pool)
1234177633Sdfr{
1235184588Sdfr	SVCTHREAD *st;
1236184588Sdfr
1237177633Sdfr	mtx_lock(&pool->sp_lock);
1238184588Sdfr
1239184588Sdfr	pool->sp_state = SVCPOOL_CLOSING;
1240184588Sdfr	LIST_FOREACH(st, &pool->sp_idlethreads, st_ilink)
1241184588Sdfr		cv_signal(&st->st_cond);
1242184588Sdfr
1243177633Sdfr	mtx_unlock(&pool->sp_lock);
1244177633Sdfr}
1245184588Sdfr
1246184588Sdfrbool_t
1247184588Sdfrsvc_getargs(struct svc_req *rqstp, xdrproc_t xargs, void *args)
1248184588Sdfr{
1249184588Sdfr	struct mbuf *m;
1250184588Sdfr	XDR xdrs;
1251184588Sdfr	bool_t stat;
1252184588Sdfr
1253184588Sdfr	m = rqstp->rq_args;
1254184588Sdfr	rqstp->rq_args = NULL;
1255184588Sdfr
1256184588Sdfr	xdrmbuf_create(&xdrs, m, XDR_DECODE);
1257184588Sdfr	stat = xargs(&xdrs, args);
1258184588Sdfr	XDR_DESTROY(&xdrs);
1259184588Sdfr
1260184588Sdfr	return (stat);
1261184588Sdfr}
1262184588Sdfr
1263184588Sdfrbool_t
1264184588Sdfrsvc_freeargs(struct svc_req *rqstp, xdrproc_t xargs, void *args)
1265184588Sdfr{
1266184588Sdfr	XDR xdrs;
1267184588Sdfr
1268184588Sdfr	if (rqstp->rq_addr) {
1269184588Sdfr		free(rqstp->rq_addr, M_SONAME);
1270184588Sdfr		rqstp->rq_addr = NULL;
1271184588Sdfr	}
1272184588Sdfr
1273184588Sdfr	xdrs.x_op = XDR_FREE;
1274184588Sdfr	return (xargs(&xdrs, args));
1275184588Sdfr}
1276184588Sdfr
1277184588Sdfrvoid
1278184588Sdfrsvc_freereq(struct svc_req *rqstp)
1279184588Sdfr{
1280184588Sdfr	SVCTHREAD *st;
1281184588Sdfr	SVCXPRT *xprt;
1282184588Sdfr	SVCPOOL *pool;
1283184588Sdfr
1284184588Sdfr	st = rqstp->rq_thread;
1285184588Sdfr	xprt = rqstp->rq_xprt;
1286184588Sdfr	if (xprt)
1287184588Sdfr		pool = xprt->xp_pool;
1288184588Sdfr	else
1289184588Sdfr		pool = NULL;
1290184588Sdfr	if (st) {
1291184588Sdfr		mtx_lock(&pool->sp_lock);
1292184588Sdfr		KASSERT(rqstp == STAILQ_FIRST(&st->st_reqs),
1293184588Sdfr		    ("Freeing request out of order"));
1294184588Sdfr		STAILQ_REMOVE_HEAD(&st->st_reqs, rq_link);
1295184588Sdfr		st->st_reqcount--;
1296184588Sdfr		if (pool->sp_done)
1297184588Sdfr			pool->sp_done(st, rqstp);
1298184588Sdfr		mtx_unlock(&pool->sp_lock);
1299184588Sdfr	}
1300184588Sdfr
1301184588Sdfr	if (rqstp->rq_auth.svc_ah_ops)
1302184588Sdfr		SVCAUTH_RELEASE(&rqstp->rq_auth);
1303184588Sdfr
1304184588Sdfr	if (rqstp->rq_xprt) {
1305184588Sdfr		SVC_RELEASE(rqstp->rq_xprt);
1306184588Sdfr	}
1307184588Sdfr
1308184588Sdfr	if (rqstp->rq_addr)
1309184588Sdfr		free(rqstp->rq_addr, M_SONAME);
1310184588Sdfr
1311184588Sdfr	if (rqstp->rq_args)
1312184588Sdfr		m_freem(rqstp->rq_args);
1313184588Sdfr
1314184588Sdfr	free(rqstp, M_RPC);
1315184588Sdfr}
1316