xref: /freebsd-14.2/sys/rpc/svc.c (revision 4c136aad)
1 /*	$NetBSD: svc.c,v 1.21 2000/07/06 03:10:35 christos Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  * Copyright (c) 2009, Sun Microsystems, Inc.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  * - Redistributions of source code must retain the above copyright notice,
12  *   this list of conditions and the following disclaimer.
13  * - Redistributions in binary form must reproduce the above copyright notice,
14  *   this list of conditions and the following disclaimer in the documentation
15  *   and/or other materials provided with the distribution.
16  * - Neither the name of Sun Microsystems, Inc. nor the names of its
17  *   contributors may be used to endorse or promote products derived
18  *   from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #if defined(LIBC_SCCS) && !defined(lint)
34 static char *sccsid2 = "@(#)svc.c 1.44 88/02/08 Copyr 1984 Sun Micro";
35 static char *sccsid = "@(#)svc.c	2.4 88/08/11 4.0 RPCSRC";
36 #endif
37 #include <sys/cdefs.h>
38 /*
39  * svc.c, Server-side remote procedure call interface.
40  *
41  * There are two sets of procedures here.  The xprt routines are
42  * for handling transport handles.  The svc routines handle the
43  * list of service routines.
44  *
45  * Copyright (C) 1984, Sun Microsystems, Inc.
46  */
47 
48 #include <sys/param.h>
49 #include <sys/jail.h>
50 #include <sys/lock.h>
51 #include <sys/kernel.h>
52 #include <sys/kthread.h>
53 #include <sys/malloc.h>
54 #include <sys/mbuf.h>
55 #include <sys/mutex.h>
56 #include <sys/proc.h>
57 #include <sys/protosw.h>
58 #include <sys/queue.h>
59 #include <sys/socketvar.h>
60 #include <sys/systm.h>
61 #include <sys/smp.h>
62 #include <sys/sx.h>
63 #include <sys/ucred.h>
64 
65 #include <netinet/tcp.h>
66 
67 #include <rpc/rpc.h>
68 #include <rpc/rpcb_clnt.h>
69 #include <rpc/replay.h>
70 
71 #include <rpc/rpc_com.h>
72 
73 #define SVC_VERSQUIET 0x0001		/* keep quiet about vers mismatch */
74 #define version_keepquiet(xp) (SVC_EXT(xp)->xp_flags & SVC_VERSQUIET)
75 
76 static struct svc_callout *svc_find(SVCPOOL *pool, rpcprog_t, rpcvers_t,
77     char *);
78 static void svc_new_thread(SVCGROUP *grp);
79 static void xprt_unregister_locked(SVCXPRT *xprt);
80 static void svc_change_space_used(SVCPOOL *pool, long delta);
81 static bool_t svc_request_space_available(SVCPOOL *pool);
82 static void svcpool_cleanup(SVCPOOL *pool);
83 
84 /* ***************  SVCXPRT related stuff **************** */
85 
86 static int svcpool_minthread_sysctl(SYSCTL_HANDLER_ARGS);
87 static int svcpool_maxthread_sysctl(SYSCTL_HANDLER_ARGS);
88 static int svcpool_threads_sysctl(SYSCTL_HANDLER_ARGS);
89 
90 SVCPOOL*
svcpool_create(const char * name,struct sysctl_oid_list * sysctl_base)91 svcpool_create(const char *name, struct sysctl_oid_list *sysctl_base)
92 {
93 	SVCPOOL *pool;
94 	SVCGROUP *grp;
95 	int g;
96 
97 	pool = malloc(sizeof(SVCPOOL), M_RPC, M_WAITOK|M_ZERO);
98 
99 	mtx_init(&pool->sp_lock, "sp_lock", NULL, MTX_DEF);
100 	pool->sp_name = name;
101 	pool->sp_state = SVCPOOL_INIT;
102 	pool->sp_proc = NULL;
103 	TAILQ_INIT(&pool->sp_callouts);
104 	TAILQ_INIT(&pool->sp_lcallouts);
105 	pool->sp_minthreads = 1;
106 	pool->sp_maxthreads = 1;
107 	pool->sp_groupcount = 1;
108 	for (g = 0; g < SVC_MAXGROUPS; g++) {
109 		grp = &pool->sp_groups[g];
110 		mtx_init(&grp->sg_lock, "sg_lock", NULL, MTX_DEF);
111 		grp->sg_pool = pool;
112 		grp->sg_state = SVCPOOL_ACTIVE;
113 		TAILQ_INIT(&grp->sg_xlist);
114 		TAILQ_INIT(&grp->sg_active);
115 		LIST_INIT(&grp->sg_idlethreads);
116 		grp->sg_minthreads = 1;
117 		grp->sg_maxthreads = 1;
118 	}
119 
120 	/*
121 	 * Don't use more than a quarter of mbuf clusters.  Nota bene:
122 	 * nmbclusters is an int, but nmbclusters*MCLBYTES may overflow
123 	 * on LP64 architectures, so cast to u_long to avoid undefined
124 	 * behavior.  (ILP32 architectures cannot have nmbclusters
125 	 * large enough to overflow for other reasons.)
126 	 */
127 	pool->sp_space_high = (u_long)nmbclusters * MCLBYTES / 4;
128 	pool->sp_space_low = (pool->sp_space_high / 3) * 2;
129 
130 	sysctl_ctx_init(&pool->sp_sysctl);
131 	if (IS_DEFAULT_VNET(curvnet) && sysctl_base) {
132 		SYSCTL_ADD_PROC(&pool->sp_sysctl, sysctl_base, OID_AUTO,
133 		    "minthreads", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
134 		    pool, 0, svcpool_minthread_sysctl, "I",
135 		    "Minimal number of threads");
136 		SYSCTL_ADD_PROC(&pool->sp_sysctl, sysctl_base, OID_AUTO,
137 		    "maxthreads", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
138 		    pool, 0, svcpool_maxthread_sysctl, "I",
139 		    "Maximal number of threads");
140 		SYSCTL_ADD_PROC(&pool->sp_sysctl, sysctl_base, OID_AUTO,
141 		    "threads", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
142 		    pool, 0, svcpool_threads_sysctl, "I",
143 		    "Current number of threads");
144 		SYSCTL_ADD_INT(&pool->sp_sysctl, sysctl_base, OID_AUTO,
145 		    "groups", CTLFLAG_RD, &pool->sp_groupcount, 0,
146 		    "Number of thread groups");
147 
148 		SYSCTL_ADD_ULONG(&pool->sp_sysctl, sysctl_base, OID_AUTO,
149 		    "request_space_used", CTLFLAG_RD,
150 		    &pool->sp_space_used,
151 		    "Space in parsed but not handled requests.");
152 
153 		SYSCTL_ADD_ULONG(&pool->sp_sysctl, sysctl_base, OID_AUTO,
154 		    "request_space_used_highest", CTLFLAG_RD,
155 		    &pool->sp_space_used_highest,
156 		    "Highest space used since reboot.");
157 
158 		SYSCTL_ADD_ULONG(&pool->sp_sysctl, sysctl_base, OID_AUTO,
159 		    "request_space_high", CTLFLAG_RW,
160 		    &pool->sp_space_high,
161 		    "Maximum space in parsed but not handled requests.");
162 
163 		SYSCTL_ADD_ULONG(&pool->sp_sysctl, sysctl_base, OID_AUTO,
164 		    "request_space_low", CTLFLAG_RW,
165 		    &pool->sp_space_low,
166 		    "Low water mark for request space.");
167 
168 		SYSCTL_ADD_INT(&pool->sp_sysctl, sysctl_base, OID_AUTO,
169 		    "request_space_throttled", CTLFLAG_RD,
170 		    &pool->sp_space_throttled, 0,
171 		    "Whether nfs requests are currently throttled");
172 
173 		SYSCTL_ADD_INT(&pool->sp_sysctl, sysctl_base, OID_AUTO,
174 		    "request_space_throttle_count", CTLFLAG_RD,
175 		    &pool->sp_space_throttle_count, 0,
176 		    "Count of times throttling based on request space has occurred");
177 	}
178 
179 	return pool;
180 }
181 
182 /*
183  * Code common to svcpool_destroy() and svcpool_close(), which cleans up
184  * the pool data structures.
185  */
186 static void
svcpool_cleanup(SVCPOOL * pool)187 svcpool_cleanup(SVCPOOL *pool)
188 {
189 	SVCGROUP *grp;
190 	SVCXPRT *xprt, *nxprt;
191 	struct svc_callout *s;
192 	struct svc_loss_callout *sl;
193 	struct svcxprt_list cleanup;
194 	int g;
195 
196 	TAILQ_INIT(&cleanup);
197 
198 	for (g = 0; g < SVC_MAXGROUPS; g++) {
199 		grp = &pool->sp_groups[g];
200 		mtx_lock(&grp->sg_lock);
201 		while ((xprt = TAILQ_FIRST(&grp->sg_xlist)) != NULL) {
202 			xprt_unregister_locked(xprt);
203 			TAILQ_INSERT_TAIL(&cleanup, xprt, xp_link);
204 		}
205 		mtx_unlock(&grp->sg_lock);
206 	}
207 	TAILQ_FOREACH_SAFE(xprt, &cleanup, xp_link, nxprt) {
208 		if (xprt->xp_socket != NULL)
209 			soshutdown(xprt->xp_socket, SHUT_WR);
210 		SVC_RELEASE(xprt);
211 	}
212 
213 	mtx_lock(&pool->sp_lock);
214 	while ((s = TAILQ_FIRST(&pool->sp_callouts)) != NULL) {
215 		mtx_unlock(&pool->sp_lock);
216 		svc_unreg(pool, s->sc_prog, s->sc_vers);
217 		mtx_lock(&pool->sp_lock);
218 	}
219 	while ((sl = TAILQ_FIRST(&pool->sp_lcallouts)) != NULL) {
220 		mtx_unlock(&pool->sp_lock);
221 		svc_loss_unreg(pool, sl->slc_dispatch);
222 		mtx_lock(&pool->sp_lock);
223 	}
224 	mtx_unlock(&pool->sp_lock);
225 }
226 
227 void
svcpool_destroy(SVCPOOL * pool)228 svcpool_destroy(SVCPOOL *pool)
229 {
230 	SVCGROUP *grp;
231 	int g;
232 
233 	svcpool_cleanup(pool);
234 
235 	for (g = 0; g < SVC_MAXGROUPS; g++) {
236 		grp = &pool->sp_groups[g];
237 		mtx_destroy(&grp->sg_lock);
238 	}
239 	mtx_destroy(&pool->sp_lock);
240 
241 	if (pool->sp_rcache)
242 		replay_freecache(pool->sp_rcache);
243 
244 	sysctl_ctx_free(&pool->sp_sysctl);
245 	free(pool, M_RPC);
246 }
247 
248 /*
249  * Similar to svcpool_destroy(), except that it does not destroy the actual
250  * data structures.  As such, "pool" may be used again.
251  */
252 void
svcpool_close(SVCPOOL * pool)253 svcpool_close(SVCPOOL *pool)
254 {
255 	SVCGROUP *grp;
256 	int g;
257 
258 	svcpool_cleanup(pool);
259 
260 	/* Now, initialize the pool's state for a fresh svc_run() call. */
261 	mtx_lock(&pool->sp_lock);
262 	pool->sp_state = SVCPOOL_INIT;
263 	mtx_unlock(&pool->sp_lock);
264 	for (g = 0; g < SVC_MAXGROUPS; g++) {
265 		grp = &pool->sp_groups[g];
266 		mtx_lock(&grp->sg_lock);
267 		grp->sg_state = SVCPOOL_ACTIVE;
268 		mtx_unlock(&grp->sg_lock);
269 	}
270 }
271 
272 /*
273  * Sysctl handler to get the present thread count on a pool
274  */
275 static int
svcpool_threads_sysctl(SYSCTL_HANDLER_ARGS)276 svcpool_threads_sysctl(SYSCTL_HANDLER_ARGS)
277 {
278 	SVCPOOL *pool;
279 	int threads, error, g;
280 
281 	pool = oidp->oid_arg1;
282 	threads = 0;
283 	mtx_lock(&pool->sp_lock);
284 	for (g = 0; g < pool->sp_groupcount; g++)
285 		threads += pool->sp_groups[g].sg_threadcount;
286 	mtx_unlock(&pool->sp_lock);
287 	error = sysctl_handle_int(oidp, &threads, 0, req);
288 	return (error);
289 }
290 
291 /*
292  * Sysctl handler to set the minimum thread count on a pool
293  */
294 static int
svcpool_minthread_sysctl(SYSCTL_HANDLER_ARGS)295 svcpool_minthread_sysctl(SYSCTL_HANDLER_ARGS)
296 {
297 	SVCPOOL *pool;
298 	int newminthreads, error, g;
299 
300 	pool = oidp->oid_arg1;
301 	newminthreads = pool->sp_minthreads;
302 	error = sysctl_handle_int(oidp, &newminthreads, 0, req);
303 	if (error == 0 && newminthreads != pool->sp_minthreads) {
304 		if (newminthreads > pool->sp_maxthreads)
305 			return (EINVAL);
306 		mtx_lock(&pool->sp_lock);
307 		pool->sp_minthreads = newminthreads;
308 		for (g = 0; g < pool->sp_groupcount; g++) {
309 			pool->sp_groups[g].sg_minthreads = max(1,
310 			    pool->sp_minthreads / pool->sp_groupcount);
311 		}
312 		mtx_unlock(&pool->sp_lock);
313 	}
314 	return (error);
315 }
316 
317 /*
318  * Sysctl handler to set the maximum thread count on a pool
319  */
320 static int
svcpool_maxthread_sysctl(SYSCTL_HANDLER_ARGS)321 svcpool_maxthread_sysctl(SYSCTL_HANDLER_ARGS)
322 {
323 	SVCPOOL *pool;
324 	int newmaxthreads, error, g;
325 
326 	pool = oidp->oid_arg1;
327 	newmaxthreads = pool->sp_maxthreads;
328 	error = sysctl_handle_int(oidp, &newmaxthreads, 0, req);
329 	if (error == 0 && newmaxthreads != pool->sp_maxthreads) {
330 		if (newmaxthreads < pool->sp_minthreads)
331 			return (EINVAL);
332 		mtx_lock(&pool->sp_lock);
333 		pool->sp_maxthreads = newmaxthreads;
334 		for (g = 0; g < pool->sp_groupcount; g++) {
335 			pool->sp_groups[g].sg_maxthreads = max(1,
336 			    pool->sp_maxthreads / pool->sp_groupcount);
337 		}
338 		mtx_unlock(&pool->sp_lock);
339 	}
340 	return (error);
341 }
342 
343 /*
344  * Activate a transport handle.
345  */
346 void
xprt_register(SVCXPRT * xprt)347 xprt_register(SVCXPRT *xprt)
348 {
349 	SVCPOOL *pool = xprt->xp_pool;
350 	SVCGROUP *grp;
351 	int g;
352 
353 	SVC_ACQUIRE(xprt);
354 	g = atomic_fetchadd_int(&pool->sp_nextgroup, 1) % pool->sp_groupcount;
355 	xprt->xp_group = grp = &pool->sp_groups[g];
356 	mtx_lock(&grp->sg_lock);
357 	xprt->xp_registered = TRUE;
358 	xprt->xp_active = FALSE;
359 	TAILQ_INSERT_TAIL(&grp->sg_xlist, xprt, xp_link);
360 	mtx_unlock(&grp->sg_lock);
361 }
362 
363 /*
364  * De-activate a transport handle. Note: the locked version doesn't
365  * release the transport - caller must do that after dropping the pool
366  * lock.
367  */
368 static void
xprt_unregister_locked(SVCXPRT * xprt)369 xprt_unregister_locked(SVCXPRT *xprt)
370 {
371 	SVCGROUP *grp = xprt->xp_group;
372 
373 	mtx_assert(&grp->sg_lock, MA_OWNED);
374 	KASSERT(xprt->xp_registered == TRUE,
375 	    ("xprt_unregister_locked: not registered"));
376 	xprt_inactive_locked(xprt);
377 	TAILQ_REMOVE(&grp->sg_xlist, xprt, xp_link);
378 	xprt->xp_registered = FALSE;
379 }
380 
381 void
xprt_unregister(SVCXPRT * xprt)382 xprt_unregister(SVCXPRT *xprt)
383 {
384 	SVCGROUP *grp = xprt->xp_group;
385 
386 	mtx_lock(&grp->sg_lock);
387 	if (xprt->xp_registered == FALSE) {
388 		/* Already unregistered by another thread */
389 		mtx_unlock(&grp->sg_lock);
390 		return;
391 	}
392 	xprt_unregister_locked(xprt);
393 	mtx_unlock(&grp->sg_lock);
394 
395 	if (xprt->xp_socket != NULL)
396 		soshutdown(xprt->xp_socket, SHUT_WR);
397 	SVC_RELEASE(xprt);
398 }
399 
400 /*
401  * Attempt to assign a service thread to this transport.
402  */
403 static int
xprt_assignthread(SVCXPRT * xprt)404 xprt_assignthread(SVCXPRT *xprt)
405 {
406 	SVCGROUP *grp = xprt->xp_group;
407 	SVCTHREAD *st;
408 
409 	mtx_assert(&grp->sg_lock, MA_OWNED);
410 	st = LIST_FIRST(&grp->sg_idlethreads);
411 	if (st) {
412 		LIST_REMOVE(st, st_ilink);
413 		SVC_ACQUIRE(xprt);
414 		xprt->xp_thread = st;
415 		st->st_xprt = xprt;
416 		cv_signal(&st->st_cond);
417 		return (TRUE);
418 	} else {
419 		/*
420 		 * See if we can create a new thread. The
421 		 * actual thread creation happens in
422 		 * svc_run_internal because our locking state
423 		 * is poorly defined (we are typically called
424 		 * from a socket upcall). Don't create more
425 		 * than one thread per second.
426 		 */
427 		if (grp->sg_state == SVCPOOL_ACTIVE
428 		    && grp->sg_lastcreatetime < time_uptime
429 		    && grp->sg_threadcount < grp->sg_maxthreads) {
430 			grp->sg_state = SVCPOOL_THREADWANTED;
431 		}
432 	}
433 	return (FALSE);
434 }
435 
436 void
xprt_active(SVCXPRT * xprt)437 xprt_active(SVCXPRT *xprt)
438 {
439 	SVCGROUP *grp = xprt->xp_group;
440 
441 	mtx_lock(&grp->sg_lock);
442 
443 	if (!xprt->xp_registered) {
444 		/*
445 		 * Race with xprt_unregister - we lose.
446 		 */
447 		mtx_unlock(&grp->sg_lock);
448 		return;
449 	}
450 
451 	if (!xprt->xp_active) {
452 		xprt->xp_active = TRUE;
453 		if (xprt->xp_thread == NULL) {
454 			if (!svc_request_space_available(xprt->xp_pool) ||
455 			    !xprt_assignthread(xprt))
456 				TAILQ_INSERT_TAIL(&grp->sg_active, xprt,
457 				    xp_alink);
458 		}
459 	}
460 
461 	mtx_unlock(&grp->sg_lock);
462 }
463 
464 void
xprt_inactive_locked(SVCXPRT * xprt)465 xprt_inactive_locked(SVCXPRT *xprt)
466 {
467 	SVCGROUP *grp = xprt->xp_group;
468 
469 	mtx_assert(&grp->sg_lock, MA_OWNED);
470 	if (xprt->xp_active) {
471 		if (xprt->xp_thread == NULL)
472 			TAILQ_REMOVE(&grp->sg_active, xprt, xp_alink);
473 		xprt->xp_active = FALSE;
474 	}
475 }
476 
477 void
xprt_inactive(SVCXPRT * xprt)478 xprt_inactive(SVCXPRT *xprt)
479 {
480 	SVCGROUP *grp = xprt->xp_group;
481 
482 	mtx_lock(&grp->sg_lock);
483 	xprt_inactive_locked(xprt);
484 	mtx_unlock(&grp->sg_lock);
485 }
486 
487 /*
488  * Variant of xprt_inactive() for use only when sure that port is
489  * assigned to thread. For example, within receive handlers.
490  */
491 void
xprt_inactive_self(SVCXPRT * xprt)492 xprt_inactive_self(SVCXPRT *xprt)
493 {
494 
495 	KASSERT(xprt->xp_thread != NULL,
496 	    ("xprt_inactive_self(%p) with NULL xp_thread", xprt));
497 	xprt->xp_active = FALSE;
498 }
499 
500 /*
501  * Add a service program to the callout list.
502  * The dispatch routine will be called when a rpc request for this
503  * program number comes in.
504  */
505 bool_t
svc_reg(SVCXPRT * xprt,const rpcprog_t prog,const rpcvers_t vers,void (* dispatch)(struct svc_req *,SVCXPRT *),const struct netconfig * nconf)506 svc_reg(SVCXPRT *xprt, const rpcprog_t prog, const rpcvers_t vers,
507     void (*dispatch)(struct svc_req *, SVCXPRT *),
508     const struct netconfig *nconf)
509 {
510 	SVCPOOL *pool = xprt->xp_pool;
511 	struct svc_callout *s;
512 	char *netid = NULL;
513 	int flag = 0;
514 
515 /* VARIABLES PROTECTED BY svc_lock: s, svc_head */
516 
517 	if (xprt->xp_netid) {
518 		netid = strdup(xprt->xp_netid, M_RPC);
519 		flag = 1;
520 	} else if (nconf && nconf->nc_netid) {
521 		netid = strdup(nconf->nc_netid, M_RPC);
522 		flag = 1;
523 	} /* must have been created with svc_raw_create */
524 	if ((netid == NULL) && (flag == 1)) {
525 		return (FALSE);
526 	}
527 
528 	mtx_lock(&pool->sp_lock);
529 	if ((s = svc_find(pool, prog, vers, netid)) != NULL) {
530 		if (netid)
531 			free(netid, M_RPC);
532 		if (s->sc_dispatch == dispatch)
533 			goto rpcb_it; /* he is registering another xptr */
534 		mtx_unlock(&pool->sp_lock);
535 		return (FALSE);
536 	}
537 	s = malloc(sizeof (struct svc_callout), M_RPC, M_NOWAIT);
538 	if (s == NULL) {
539 		if (netid)
540 			free(netid, M_RPC);
541 		mtx_unlock(&pool->sp_lock);
542 		return (FALSE);
543 	}
544 
545 	s->sc_prog = prog;
546 	s->sc_vers = vers;
547 	s->sc_dispatch = dispatch;
548 	s->sc_netid = netid;
549 	TAILQ_INSERT_TAIL(&pool->sp_callouts, s, sc_link);
550 
551 	if ((xprt->xp_netid == NULL) && (flag == 1) && netid)
552 		((SVCXPRT *) xprt)->xp_netid = strdup(netid, M_RPC);
553 
554 rpcb_it:
555 	mtx_unlock(&pool->sp_lock);
556 	/* now register the information with the local binder service */
557 	if (nconf) {
558 		bool_t dummy;
559 		struct netconfig tnc;
560 		struct netbuf nb;
561 		tnc = *nconf;
562 		nb.buf = &xprt->xp_ltaddr;
563 		nb.len = xprt->xp_ltaddr.ss_len;
564 		dummy = rpcb_set(prog, vers, &tnc, &nb);
565 		return (dummy);
566 	}
567 	return (TRUE);
568 }
569 
570 /*
571  * Remove a service program from the callout list.
572  */
573 void
svc_unreg(SVCPOOL * pool,const rpcprog_t prog,const rpcvers_t vers)574 svc_unreg(SVCPOOL *pool, const rpcprog_t prog, const rpcvers_t vers)
575 {
576 	struct svc_callout *s;
577 
578 	/* unregister the information anyway */
579 	(void) rpcb_unset(prog, vers, NULL);
580 	mtx_lock(&pool->sp_lock);
581 	while ((s = svc_find(pool, prog, vers, NULL)) != NULL) {
582 		TAILQ_REMOVE(&pool->sp_callouts, s, sc_link);
583 		if (s->sc_netid)
584 			mem_free(s->sc_netid, sizeof (s->sc_netid) + 1);
585 		mem_free(s, sizeof (struct svc_callout));
586 	}
587 	mtx_unlock(&pool->sp_lock);
588 }
589 
590 /*
591  * Add a service connection loss program to the callout list.
592  * The dispatch routine will be called when some port in ths pool die.
593  */
594 bool_t
svc_loss_reg(SVCXPRT * xprt,void (* dispatch)(SVCXPRT *))595 svc_loss_reg(SVCXPRT *xprt, void (*dispatch)(SVCXPRT *))
596 {
597 	SVCPOOL *pool = xprt->xp_pool;
598 	struct svc_loss_callout *s;
599 
600 	mtx_lock(&pool->sp_lock);
601 	TAILQ_FOREACH(s, &pool->sp_lcallouts, slc_link) {
602 		if (s->slc_dispatch == dispatch)
603 			break;
604 	}
605 	if (s != NULL) {
606 		mtx_unlock(&pool->sp_lock);
607 		return (TRUE);
608 	}
609 	s = malloc(sizeof(struct svc_loss_callout), M_RPC, M_NOWAIT);
610 	if (s == NULL) {
611 		mtx_unlock(&pool->sp_lock);
612 		return (FALSE);
613 	}
614 	s->slc_dispatch = dispatch;
615 	TAILQ_INSERT_TAIL(&pool->sp_lcallouts, s, slc_link);
616 	mtx_unlock(&pool->sp_lock);
617 	return (TRUE);
618 }
619 
620 /*
621  * Remove a service connection loss program from the callout list.
622  */
623 void
svc_loss_unreg(SVCPOOL * pool,void (* dispatch)(SVCXPRT *))624 svc_loss_unreg(SVCPOOL *pool, void (*dispatch)(SVCXPRT *))
625 {
626 	struct svc_loss_callout *s;
627 
628 	mtx_lock(&pool->sp_lock);
629 	TAILQ_FOREACH(s, &pool->sp_lcallouts, slc_link) {
630 		if (s->slc_dispatch == dispatch) {
631 			TAILQ_REMOVE(&pool->sp_lcallouts, s, slc_link);
632 			free(s, M_RPC);
633 			break;
634 		}
635 	}
636 	mtx_unlock(&pool->sp_lock);
637 }
638 
639 /* ********************** CALLOUT list related stuff ************* */
640 
641 /*
642  * Search the callout list for a program number, return the callout
643  * struct.
644  */
645 static struct svc_callout *
svc_find(SVCPOOL * pool,rpcprog_t prog,rpcvers_t vers,char * netid)646 svc_find(SVCPOOL *pool, rpcprog_t prog, rpcvers_t vers, char *netid)
647 {
648 	struct svc_callout *s;
649 
650 	mtx_assert(&pool->sp_lock, MA_OWNED);
651 	TAILQ_FOREACH(s, &pool->sp_callouts, sc_link) {
652 		if (s->sc_prog == prog && s->sc_vers == vers
653 		    && (netid == NULL || s->sc_netid == NULL ||
654 			strcmp(netid, s->sc_netid) == 0))
655 			break;
656 	}
657 
658 	return (s);
659 }
660 
661 /* ******************* REPLY GENERATION ROUTINES  ************ */
662 
663 static bool_t
svc_sendreply_common(struct svc_req * rqstp,struct rpc_msg * rply,struct mbuf * body)664 svc_sendreply_common(struct svc_req *rqstp, struct rpc_msg *rply,
665     struct mbuf *body)
666 {
667 	SVCXPRT *xprt = rqstp->rq_xprt;
668 	bool_t ok;
669 
670 	if (rqstp->rq_args) {
671 		m_freem(rqstp->rq_args);
672 		rqstp->rq_args = NULL;
673 	}
674 
675 	if (xprt->xp_pool->sp_rcache)
676 		replay_setreply(xprt->xp_pool->sp_rcache,
677 		    rply, svc_getrpccaller(rqstp), body);
678 
679 	if (!SVCAUTH_WRAP(&rqstp->rq_auth, &body))
680 		return (FALSE);
681 
682 	ok = SVC_REPLY(xprt, rply, rqstp->rq_addr, body, &rqstp->rq_reply_seq);
683 	if (rqstp->rq_addr) {
684 		free(rqstp->rq_addr, M_SONAME);
685 		rqstp->rq_addr = NULL;
686 	}
687 
688 	return (ok);
689 }
690 
691 /*
692  * Send a reply to an rpc request
693  */
694 bool_t
svc_sendreply(struct svc_req * rqstp,xdrproc_t xdr_results,void * xdr_location)695 svc_sendreply(struct svc_req *rqstp, xdrproc_t xdr_results, void * xdr_location)
696 {
697 	struct rpc_msg rply;
698 	struct mbuf *m;
699 	XDR xdrs;
700 	bool_t ok;
701 
702 	rply.rm_xid = rqstp->rq_xid;
703 	rply.rm_direction = REPLY;
704 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
705 	rply.acpted_rply.ar_verf = rqstp->rq_verf;
706 	rply.acpted_rply.ar_stat = SUCCESS;
707 	rply.acpted_rply.ar_results.where = NULL;
708 	rply.acpted_rply.ar_results.proc = (xdrproc_t) xdr_void;
709 
710 	m = m_getcl(M_WAITOK, MT_DATA, 0);
711 	xdrmbuf_create(&xdrs, m, XDR_ENCODE);
712 	ok = xdr_results(&xdrs, xdr_location);
713 	XDR_DESTROY(&xdrs);
714 
715 	if (ok) {
716 		return (svc_sendreply_common(rqstp, &rply, m));
717 	} else {
718 		m_freem(m);
719 		return (FALSE);
720 	}
721 }
722 
723 bool_t
svc_sendreply_mbuf(struct svc_req * rqstp,struct mbuf * m)724 svc_sendreply_mbuf(struct svc_req *rqstp, struct mbuf *m)
725 {
726 	struct rpc_msg rply;
727 
728 	rply.rm_xid = rqstp->rq_xid;
729 	rply.rm_direction = REPLY;
730 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
731 	rply.acpted_rply.ar_verf = rqstp->rq_verf;
732 	rply.acpted_rply.ar_stat = SUCCESS;
733 	rply.acpted_rply.ar_results.where = NULL;
734 	rply.acpted_rply.ar_results.proc = (xdrproc_t) xdr_void;
735 
736 	return (svc_sendreply_common(rqstp, &rply, m));
737 }
738 
739 /*
740  * No procedure error reply
741  */
742 void
svcerr_noproc(struct svc_req * rqstp)743 svcerr_noproc(struct svc_req *rqstp)
744 {
745 	SVCXPRT *xprt = rqstp->rq_xprt;
746 	struct rpc_msg rply;
747 
748 	rply.rm_xid = rqstp->rq_xid;
749 	rply.rm_direction = REPLY;
750 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
751 	rply.acpted_rply.ar_verf = rqstp->rq_verf;
752 	rply.acpted_rply.ar_stat = PROC_UNAVAIL;
753 
754 	if (xprt->xp_pool->sp_rcache)
755 		replay_setreply(xprt->xp_pool->sp_rcache,
756 		    &rply, svc_getrpccaller(rqstp), NULL);
757 
758 	svc_sendreply_common(rqstp, &rply, NULL);
759 }
760 
761 /*
762  * Can't decode args error reply
763  */
764 void
svcerr_decode(struct svc_req * rqstp)765 svcerr_decode(struct svc_req *rqstp)
766 {
767 	SVCXPRT *xprt = rqstp->rq_xprt;
768 	struct rpc_msg rply;
769 
770 	rply.rm_xid = rqstp->rq_xid;
771 	rply.rm_direction = REPLY;
772 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
773 	rply.acpted_rply.ar_verf = rqstp->rq_verf;
774 	rply.acpted_rply.ar_stat = GARBAGE_ARGS;
775 
776 	if (xprt->xp_pool->sp_rcache)
777 		replay_setreply(xprt->xp_pool->sp_rcache,
778 		    &rply, (struct sockaddr *) &xprt->xp_rtaddr, NULL);
779 
780 	svc_sendreply_common(rqstp, &rply, NULL);
781 }
782 
783 /*
784  * Some system error
785  */
786 void
svcerr_systemerr(struct svc_req * rqstp)787 svcerr_systemerr(struct svc_req *rqstp)
788 {
789 	SVCXPRT *xprt = rqstp->rq_xprt;
790 	struct rpc_msg rply;
791 
792 	rply.rm_xid = rqstp->rq_xid;
793 	rply.rm_direction = REPLY;
794 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
795 	rply.acpted_rply.ar_verf = rqstp->rq_verf;
796 	rply.acpted_rply.ar_stat = SYSTEM_ERR;
797 
798 	if (xprt->xp_pool->sp_rcache)
799 		replay_setreply(xprt->xp_pool->sp_rcache,
800 		    &rply, svc_getrpccaller(rqstp), NULL);
801 
802 	svc_sendreply_common(rqstp, &rply, NULL);
803 }
804 
805 /*
806  * Authentication error reply
807  */
808 void
svcerr_auth(struct svc_req * rqstp,enum auth_stat why)809 svcerr_auth(struct svc_req *rqstp, enum auth_stat why)
810 {
811 	SVCXPRT *xprt = rqstp->rq_xprt;
812 	struct rpc_msg rply;
813 
814 	rply.rm_xid = rqstp->rq_xid;
815 	rply.rm_direction = REPLY;
816 	rply.rm_reply.rp_stat = MSG_DENIED;
817 	rply.rjcted_rply.rj_stat = AUTH_ERROR;
818 	rply.rjcted_rply.rj_why = why;
819 
820 	if (xprt->xp_pool->sp_rcache)
821 		replay_setreply(xprt->xp_pool->sp_rcache,
822 		    &rply, svc_getrpccaller(rqstp), NULL);
823 
824 	svc_sendreply_common(rqstp, &rply, NULL);
825 }
826 
827 /*
828  * Auth too weak error reply
829  */
830 void
svcerr_weakauth(struct svc_req * rqstp)831 svcerr_weakauth(struct svc_req *rqstp)
832 {
833 
834 	svcerr_auth(rqstp, AUTH_TOOWEAK);
835 }
836 
837 /*
838  * Program unavailable error reply
839  */
840 void
svcerr_noprog(struct svc_req * rqstp)841 svcerr_noprog(struct svc_req *rqstp)
842 {
843 	SVCXPRT *xprt = rqstp->rq_xprt;
844 	struct rpc_msg rply;
845 
846 	rply.rm_xid = rqstp->rq_xid;
847 	rply.rm_direction = REPLY;
848 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
849 	rply.acpted_rply.ar_verf = rqstp->rq_verf;
850 	rply.acpted_rply.ar_stat = PROG_UNAVAIL;
851 
852 	if (xprt->xp_pool->sp_rcache)
853 		replay_setreply(xprt->xp_pool->sp_rcache,
854 		    &rply, svc_getrpccaller(rqstp), NULL);
855 
856 	svc_sendreply_common(rqstp, &rply, NULL);
857 }
858 
859 /*
860  * Program version mismatch error reply
861  */
862 void
svcerr_progvers(struct svc_req * rqstp,rpcvers_t low_vers,rpcvers_t high_vers)863 svcerr_progvers(struct svc_req *rqstp, rpcvers_t low_vers, rpcvers_t high_vers)
864 {
865 	SVCXPRT *xprt = rqstp->rq_xprt;
866 	struct rpc_msg rply;
867 
868 	rply.rm_xid = rqstp->rq_xid;
869 	rply.rm_direction = REPLY;
870 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
871 	rply.acpted_rply.ar_verf = rqstp->rq_verf;
872 	rply.acpted_rply.ar_stat = PROG_MISMATCH;
873 	rply.acpted_rply.ar_vers.low = (uint32_t)low_vers;
874 	rply.acpted_rply.ar_vers.high = (uint32_t)high_vers;
875 
876 	if (xprt->xp_pool->sp_rcache)
877 		replay_setreply(xprt->xp_pool->sp_rcache,
878 		    &rply, svc_getrpccaller(rqstp), NULL);
879 
880 	svc_sendreply_common(rqstp, &rply, NULL);
881 }
882 
883 /*
884  * Allocate a new server transport structure. All fields are
885  * initialized to zero and xp_p3 is initialized to point at an
886  * extension structure to hold various flags and authentication
887  * parameters.
888  */
889 SVCXPRT *
svc_xprt_alloc(void)890 svc_xprt_alloc(void)
891 {
892 	SVCXPRT *xprt;
893 	SVCXPRT_EXT *ext;
894 
895 	xprt = mem_alloc(sizeof(SVCXPRT));
896 	ext = mem_alloc(sizeof(SVCXPRT_EXT));
897 	xprt->xp_p3 = ext;
898 	refcount_init(&xprt->xp_refs, 1);
899 
900 	return (xprt);
901 }
902 
903 /*
904  * Free a server transport structure.
905  */
906 void
svc_xprt_free(SVCXPRT * xprt)907 svc_xprt_free(SVCXPRT *xprt)
908 {
909 
910 	mem_free(xprt->xp_p3, sizeof(SVCXPRT_EXT));
911 	/* The size argument is ignored, so 0 is ok. */
912 	mem_free(xprt->xp_gidp, 0);
913 	mem_free(xprt, sizeof(SVCXPRT));
914 }
915 
916 /* ******************* SERVER INPUT STUFF ******************* */
917 
918 /*
919  * Read RPC requests from a transport and queue them to be
920  * executed. We handle authentication and replay cache replies here.
921  * Actually dispatching the RPC is deferred till svc_executereq.
922  */
923 static enum xprt_stat
svc_getreq(SVCXPRT * xprt,struct svc_req ** rqstp_ret)924 svc_getreq(SVCXPRT *xprt, struct svc_req **rqstp_ret)
925 {
926 	SVCPOOL *pool = xprt->xp_pool;
927 	struct svc_req *r;
928 	struct rpc_msg msg;
929 	struct mbuf *args;
930 	struct svc_loss_callout *s;
931 	enum xprt_stat stat;
932 
933 	/* now receive msgs from xprtprt (support batch calls) */
934 	r = malloc(sizeof(*r), M_RPC, M_WAITOK|M_ZERO);
935 
936 	msg.rm_call.cb_cred.oa_base = r->rq_credarea;
937 	msg.rm_call.cb_verf.oa_base = &r->rq_credarea[MAX_AUTH_BYTES];
938 	r->rq_clntcred = &r->rq_credarea[2*MAX_AUTH_BYTES];
939 	if (SVC_RECV(xprt, &msg, &r->rq_addr, &args)) {
940 		enum auth_stat why;
941 
942 		/*
943 		 * Handle replays and authenticate before queuing the
944 		 * request to be executed.
945 		 */
946 		SVC_ACQUIRE(xprt);
947 		r->rq_xprt = xprt;
948 		if (pool->sp_rcache) {
949 			struct rpc_msg repmsg;
950 			struct mbuf *repbody;
951 			enum replay_state rs;
952 			rs = replay_find(pool->sp_rcache, &msg,
953 			    svc_getrpccaller(r), &repmsg, &repbody);
954 			switch (rs) {
955 			case RS_NEW:
956 				break;
957 			case RS_DONE:
958 				SVC_REPLY(xprt, &repmsg, r->rq_addr,
959 				    repbody, &r->rq_reply_seq);
960 				if (r->rq_addr) {
961 					free(r->rq_addr, M_SONAME);
962 					r->rq_addr = NULL;
963 				}
964 				m_freem(args);
965 				goto call_done;
966 
967 			default:
968 				m_freem(args);
969 				goto call_done;
970 			}
971 		}
972 
973 		r->rq_xid = msg.rm_xid;
974 		r->rq_prog = msg.rm_call.cb_prog;
975 		r->rq_vers = msg.rm_call.cb_vers;
976 		r->rq_proc = msg.rm_call.cb_proc;
977 		r->rq_size = sizeof(*r) + m_length(args, NULL);
978 		r->rq_args = args;
979 		if ((why = _authenticate(r, &msg)) != AUTH_OK) {
980 			/*
981 			 * RPCSEC_GSS uses this return code
982 			 * for requests that form part of its
983 			 * context establishment protocol and
984 			 * should not be dispatched to the
985 			 * application.
986 			 */
987 			if (why != RPCSEC_GSS_NODISPATCH)
988 				svcerr_auth(r, why);
989 			goto call_done;
990 		}
991 
992 		if (!SVCAUTH_UNWRAP(&r->rq_auth, &r->rq_args)) {
993 			svcerr_decode(r);
994 			goto call_done;
995 		}
996 
997 		/*
998 		 * Defer enabling DDP until the first non-NULLPROC RPC
999 		 * is received to allow STARTTLS authentication to
1000 		 * enable TLS offload first.
1001 		 */
1002 		if (xprt->xp_doneddp == 0 && r->rq_proc != NULLPROC &&
1003 		    xprt->xp_socket != NULL &&
1004 		    atomic_cmpset_int(&xprt->xp_doneddp, 0, 1)) {
1005 			if (xprt->xp_socket->so_proto->pr_protocol ==
1006 			    IPPROTO_TCP) {
1007 				int optval = 1;
1008 
1009 				(void)so_setsockopt(xprt->xp_socket,
1010 				    IPPROTO_TCP, TCP_USE_DDP, &optval,
1011 				    sizeof(optval));
1012 			}
1013 		}
1014 
1015 		/*
1016 		 * Everything checks out, return request to caller.
1017 		 */
1018 		*rqstp_ret = r;
1019 		r = NULL;
1020 	}
1021 call_done:
1022 	if (r) {
1023 		svc_freereq(r);
1024 		r = NULL;
1025 	}
1026 	if ((stat = SVC_STAT(xprt)) == XPRT_DIED) {
1027 		TAILQ_FOREACH(s, &pool->sp_lcallouts, slc_link)
1028 			(*s->slc_dispatch)(xprt);
1029 		xprt_unregister(xprt);
1030 	}
1031 
1032 	return (stat);
1033 }
1034 
1035 static void
svc_executereq(struct svc_req * rqstp)1036 svc_executereq(struct svc_req *rqstp)
1037 {
1038 	SVCXPRT *xprt = rqstp->rq_xprt;
1039 	SVCPOOL *pool = xprt->xp_pool;
1040 	int prog_found;
1041 	rpcvers_t low_vers;
1042 	rpcvers_t high_vers;
1043 	struct svc_callout *s;
1044 
1045 	/* now match message with a registered service*/
1046 	prog_found = FALSE;
1047 	low_vers = (rpcvers_t) -1L;
1048 	high_vers = (rpcvers_t) 0L;
1049 	TAILQ_FOREACH(s, &pool->sp_callouts, sc_link) {
1050 		if (s->sc_prog == rqstp->rq_prog) {
1051 			if (s->sc_vers == rqstp->rq_vers) {
1052 				/*
1053 				 * We hand ownership of r to the
1054 				 * dispatch method - they must call
1055 				 * svc_freereq.
1056 				 */
1057 				(*s->sc_dispatch)(rqstp, xprt);
1058 				return;
1059 			}  /* found correct version */
1060 			prog_found = TRUE;
1061 			if (s->sc_vers < low_vers)
1062 				low_vers = s->sc_vers;
1063 			if (s->sc_vers > high_vers)
1064 				high_vers = s->sc_vers;
1065 		}   /* found correct program */
1066 	}
1067 
1068 	/*
1069 	 * if we got here, the program or version
1070 	 * is not served ...
1071 	 */
1072 	if (prog_found)
1073 		svcerr_progvers(rqstp, low_vers, high_vers);
1074 	else
1075 		svcerr_noprog(rqstp);
1076 
1077 	svc_freereq(rqstp);
1078 }
1079 
1080 static void
svc_checkidle(SVCGROUP * grp)1081 svc_checkidle(SVCGROUP *grp)
1082 {
1083 	SVCXPRT *xprt, *nxprt;
1084 	time_t timo;
1085 	struct svcxprt_list cleanup;
1086 
1087 	TAILQ_INIT(&cleanup);
1088 	TAILQ_FOREACH_SAFE(xprt, &grp->sg_xlist, xp_link, nxprt) {
1089 		/*
1090 		 * Only some transports have idle timers. Don't time
1091 		 * something out which is just waking up.
1092 		 */
1093 		if (!xprt->xp_idletimeout || xprt->xp_thread)
1094 			continue;
1095 
1096 		timo = xprt->xp_lastactive + xprt->xp_idletimeout;
1097 		if (time_uptime > timo) {
1098 			xprt_unregister_locked(xprt);
1099 			TAILQ_INSERT_TAIL(&cleanup, xprt, xp_link);
1100 		}
1101 	}
1102 
1103 	mtx_unlock(&grp->sg_lock);
1104 	TAILQ_FOREACH_SAFE(xprt, &cleanup, xp_link, nxprt) {
1105 		soshutdown(xprt->xp_socket, SHUT_WR);
1106 		SVC_RELEASE(xprt);
1107 	}
1108 	mtx_lock(&grp->sg_lock);
1109 }
1110 
1111 static void
svc_assign_waiting_sockets(SVCPOOL * pool)1112 svc_assign_waiting_sockets(SVCPOOL *pool)
1113 {
1114 	SVCGROUP *grp;
1115 	SVCXPRT *xprt;
1116 	int g;
1117 
1118 	for (g = 0; g < pool->sp_groupcount; g++) {
1119 		grp = &pool->sp_groups[g];
1120 		mtx_lock(&grp->sg_lock);
1121 		while ((xprt = TAILQ_FIRST(&grp->sg_active)) != NULL) {
1122 			if (xprt_assignthread(xprt))
1123 				TAILQ_REMOVE(&grp->sg_active, xprt, xp_alink);
1124 			else
1125 				break;
1126 		}
1127 		mtx_unlock(&grp->sg_lock);
1128 	}
1129 }
1130 
1131 static void
svc_change_space_used(SVCPOOL * pool,long delta)1132 svc_change_space_used(SVCPOOL *pool, long delta)
1133 {
1134 	unsigned long value;
1135 
1136 	value = atomic_fetchadd_long(&pool->sp_space_used, delta) + delta;
1137 	if (delta > 0) {
1138 		if (value >= pool->sp_space_high && !pool->sp_space_throttled) {
1139 			pool->sp_space_throttled = TRUE;
1140 			pool->sp_space_throttle_count++;
1141 		}
1142 		if (value > pool->sp_space_used_highest)
1143 			pool->sp_space_used_highest = value;
1144 	} else {
1145 		if (value < pool->sp_space_low && pool->sp_space_throttled) {
1146 			pool->sp_space_throttled = FALSE;
1147 			svc_assign_waiting_sockets(pool);
1148 		}
1149 	}
1150 }
1151 
1152 static bool_t
svc_request_space_available(SVCPOOL * pool)1153 svc_request_space_available(SVCPOOL *pool)
1154 {
1155 
1156 	if (pool->sp_space_throttled)
1157 		return (FALSE);
1158 	return (TRUE);
1159 }
1160 
1161 static void
svc_run_internal(SVCGROUP * grp,bool_t ismaster)1162 svc_run_internal(SVCGROUP *grp, bool_t ismaster)
1163 {
1164 	SVCPOOL *pool = grp->sg_pool;
1165 	SVCTHREAD *st, *stpref;
1166 	SVCXPRT *xprt;
1167 	enum xprt_stat stat;
1168 	struct svc_req *rqstp;
1169 	struct proc *p;
1170 	long sz;
1171 	int error;
1172 
1173 	st = mem_alloc(sizeof(*st));
1174 	mtx_init(&st->st_lock, "st_lock", NULL, MTX_DEF);
1175 	st->st_pool = pool;
1176 	st->st_xprt = NULL;
1177 	STAILQ_INIT(&st->st_reqs);
1178 	cv_init(&st->st_cond, "rpcsvc");
1179 
1180 	mtx_lock(&grp->sg_lock);
1181 
1182 	/*
1183 	 * If we are a new thread which was spawned to cope with
1184 	 * increased load, set the state back to SVCPOOL_ACTIVE.
1185 	 */
1186 	if (grp->sg_state == SVCPOOL_THREADSTARTING)
1187 		grp->sg_state = SVCPOOL_ACTIVE;
1188 
1189 	while (grp->sg_state != SVCPOOL_CLOSING) {
1190 		/*
1191 		 * Create new thread if requested.
1192 		 */
1193 		if (grp->sg_state == SVCPOOL_THREADWANTED) {
1194 			grp->sg_state = SVCPOOL_THREADSTARTING;
1195 			grp->sg_lastcreatetime = time_uptime;
1196 			mtx_unlock(&grp->sg_lock);
1197 			svc_new_thread(grp);
1198 			mtx_lock(&grp->sg_lock);
1199 			continue;
1200 		}
1201 
1202 		/*
1203 		 * Check for idle transports once per second.
1204 		 */
1205 		if (time_uptime > grp->sg_lastidlecheck) {
1206 			grp->sg_lastidlecheck = time_uptime;
1207 			svc_checkidle(grp);
1208 		}
1209 
1210 		xprt = st->st_xprt;
1211 		if (!xprt) {
1212 			/*
1213 			 * Enforce maxthreads count.
1214 			 */
1215 			if (!ismaster && grp->sg_threadcount >
1216 			    grp->sg_maxthreads)
1217 				break;
1218 
1219 			/*
1220 			 * Before sleeping, see if we can find an
1221 			 * active transport which isn't being serviced
1222 			 * by a thread.
1223 			 */
1224 			if (svc_request_space_available(pool) &&
1225 			    (xprt = TAILQ_FIRST(&grp->sg_active)) != NULL) {
1226 				TAILQ_REMOVE(&grp->sg_active, xprt, xp_alink);
1227 				SVC_ACQUIRE(xprt);
1228 				xprt->xp_thread = st;
1229 				st->st_xprt = xprt;
1230 				continue;
1231 			}
1232 
1233 			LIST_INSERT_HEAD(&grp->sg_idlethreads, st, st_ilink);
1234 			if (ismaster || (!ismaster &&
1235 			    grp->sg_threadcount > grp->sg_minthreads))
1236 				error = cv_timedwait_sig(&st->st_cond,
1237 				    &grp->sg_lock, 5 * hz);
1238 			else
1239 				error = cv_wait_sig(&st->st_cond,
1240 				    &grp->sg_lock);
1241 			if (st->st_xprt == NULL)
1242 				LIST_REMOVE(st, st_ilink);
1243 
1244 			/*
1245 			 * Reduce worker thread count when idle.
1246 			 */
1247 			if (error == EWOULDBLOCK) {
1248 				if (!ismaster
1249 				    && (grp->sg_threadcount
1250 					> grp->sg_minthreads)
1251 					&& !st->st_xprt)
1252 					break;
1253 			} else if (error != 0) {
1254 				KASSERT(error == EINTR || error == ERESTART,
1255 				    ("non-signal error %d", error));
1256 				mtx_unlock(&grp->sg_lock);
1257 				p = curproc;
1258 				PROC_LOCK(p);
1259 				if (P_SHOULDSTOP(p) ||
1260 				    (p->p_flag & P_TOTAL_STOP) != 0) {
1261 					thread_suspend_check(0);
1262 					PROC_UNLOCK(p);
1263 					mtx_lock(&grp->sg_lock);
1264 				} else {
1265 					PROC_UNLOCK(p);
1266 					svc_exit(pool);
1267 					mtx_lock(&grp->sg_lock);
1268 					break;
1269 				}
1270 			}
1271 			continue;
1272 		}
1273 		mtx_unlock(&grp->sg_lock);
1274 
1275 		/*
1276 		 * Drain the transport socket and queue up any RPCs.
1277 		 */
1278 		xprt->xp_lastactive = time_uptime;
1279 		do {
1280 			if (!svc_request_space_available(pool))
1281 				break;
1282 			rqstp = NULL;
1283 			stat = svc_getreq(xprt, &rqstp);
1284 			if (rqstp) {
1285 				svc_change_space_used(pool, rqstp->rq_size);
1286 				/*
1287 				 * See if the application has a preference
1288 				 * for some other thread.
1289 				 */
1290 				if (pool->sp_assign) {
1291 					stpref = pool->sp_assign(st, rqstp);
1292 					rqstp->rq_thread = stpref;
1293 					STAILQ_INSERT_TAIL(&stpref->st_reqs,
1294 					    rqstp, rq_link);
1295 					mtx_unlock(&stpref->st_lock);
1296 					if (stpref != st)
1297 						rqstp = NULL;
1298 				} else {
1299 					rqstp->rq_thread = st;
1300 					STAILQ_INSERT_TAIL(&st->st_reqs,
1301 					    rqstp, rq_link);
1302 				}
1303 			}
1304 		} while (rqstp == NULL && stat == XPRT_MOREREQS
1305 		    && grp->sg_state != SVCPOOL_CLOSING);
1306 
1307 		/*
1308 		 * Move this transport to the end of the active list to
1309 		 * ensure fairness when multiple transports are active.
1310 		 * If this was the last queued request, svc_getreq will end
1311 		 * up calling xprt_inactive to remove from the active list.
1312 		 */
1313 		mtx_lock(&grp->sg_lock);
1314 		xprt->xp_thread = NULL;
1315 		st->st_xprt = NULL;
1316 		if (xprt->xp_active) {
1317 			if (!svc_request_space_available(pool) ||
1318 			    !xprt_assignthread(xprt))
1319 				TAILQ_INSERT_TAIL(&grp->sg_active,
1320 				    xprt, xp_alink);
1321 		}
1322 		mtx_unlock(&grp->sg_lock);
1323 		SVC_RELEASE(xprt);
1324 
1325 		/*
1326 		 * Execute what we have queued.
1327 		 */
1328 		mtx_lock(&st->st_lock);
1329 		while ((rqstp = STAILQ_FIRST(&st->st_reqs)) != NULL) {
1330 			STAILQ_REMOVE_HEAD(&st->st_reqs, rq_link);
1331 			mtx_unlock(&st->st_lock);
1332 			sz = (long)rqstp->rq_size;
1333 			svc_executereq(rqstp);
1334 			svc_change_space_used(pool, -sz);
1335 			mtx_lock(&st->st_lock);
1336 		}
1337 		mtx_unlock(&st->st_lock);
1338 		mtx_lock(&grp->sg_lock);
1339 	}
1340 
1341 	if (st->st_xprt) {
1342 		xprt = st->st_xprt;
1343 		st->st_xprt = NULL;
1344 		SVC_RELEASE(xprt);
1345 	}
1346 	KASSERT(STAILQ_EMPTY(&st->st_reqs), ("stray reqs on exit"));
1347 	mtx_destroy(&st->st_lock);
1348 	cv_destroy(&st->st_cond);
1349 	mem_free(st, sizeof(*st));
1350 
1351 	grp->sg_threadcount--;
1352 	if (!ismaster)
1353 		wakeup(grp);
1354 	mtx_unlock(&grp->sg_lock);
1355 }
1356 
1357 static void
svc_thread_start(void * arg)1358 svc_thread_start(void *arg)
1359 {
1360 
1361 	svc_run_internal((SVCGROUP *) arg, FALSE);
1362 	kthread_exit();
1363 }
1364 
1365 static void
svc_new_thread(SVCGROUP * grp)1366 svc_new_thread(SVCGROUP *grp)
1367 {
1368 	SVCPOOL *pool = grp->sg_pool;
1369 	struct thread *td;
1370 
1371 	mtx_lock(&grp->sg_lock);
1372 	grp->sg_threadcount++;
1373 	mtx_unlock(&grp->sg_lock);
1374 	kthread_add(svc_thread_start, grp, pool->sp_proc, &td, 0, 0,
1375 	    "%s: service", pool->sp_name);
1376 }
1377 
1378 void
svc_run(SVCPOOL * pool)1379 svc_run(SVCPOOL *pool)
1380 {
1381 	int g, i;
1382 	struct proc *p;
1383 	struct thread *td;
1384 	SVCGROUP *grp;
1385 
1386 	p = curproc;
1387 	td = curthread;
1388 	snprintf(td->td_name, sizeof(td->td_name),
1389 	    "%s: master", pool->sp_name);
1390 	pool->sp_state = SVCPOOL_ACTIVE;
1391 	pool->sp_proc = p;
1392 
1393 	/* Choose group count based on number of threads and CPUs. */
1394 	pool->sp_groupcount = max(1, min(SVC_MAXGROUPS,
1395 	    min(pool->sp_maxthreads / 2, mp_ncpus) / 6));
1396 	for (g = 0; g < pool->sp_groupcount; g++) {
1397 		grp = &pool->sp_groups[g];
1398 		grp->sg_minthreads = max(1,
1399 		    pool->sp_minthreads / pool->sp_groupcount);
1400 		grp->sg_maxthreads = max(1,
1401 		    pool->sp_maxthreads / pool->sp_groupcount);
1402 		grp->sg_lastcreatetime = time_uptime;
1403 	}
1404 
1405 	/* Starting threads */
1406 	pool->sp_groups[0].sg_threadcount++;
1407 	for (g = 0; g < pool->sp_groupcount; g++) {
1408 		grp = &pool->sp_groups[g];
1409 		for (i = ((g == 0) ? 1 : 0); i < grp->sg_minthreads; i++)
1410 			svc_new_thread(grp);
1411 	}
1412 	svc_run_internal(&pool->sp_groups[0], TRUE);
1413 
1414 	/* Waiting for threads to stop. */
1415 	for (g = 0; g < pool->sp_groupcount; g++) {
1416 		grp = &pool->sp_groups[g];
1417 		mtx_lock(&grp->sg_lock);
1418 		while (grp->sg_threadcount > 0)
1419 			msleep(grp, &grp->sg_lock, 0, "svcexit", 0);
1420 		mtx_unlock(&grp->sg_lock);
1421 	}
1422 }
1423 
1424 void
svc_exit(SVCPOOL * pool)1425 svc_exit(SVCPOOL *pool)
1426 {
1427 	SVCGROUP *grp;
1428 	SVCTHREAD *st;
1429 	int g;
1430 
1431 	pool->sp_state = SVCPOOL_CLOSING;
1432 	for (g = 0; g < pool->sp_groupcount; g++) {
1433 		grp = &pool->sp_groups[g];
1434 		mtx_lock(&grp->sg_lock);
1435 		if (grp->sg_state != SVCPOOL_CLOSING) {
1436 			grp->sg_state = SVCPOOL_CLOSING;
1437 			LIST_FOREACH(st, &grp->sg_idlethreads, st_ilink)
1438 				cv_signal(&st->st_cond);
1439 		}
1440 		mtx_unlock(&grp->sg_lock);
1441 	}
1442 }
1443 
1444 bool_t
svc_getargs(struct svc_req * rqstp,xdrproc_t xargs,void * args)1445 svc_getargs(struct svc_req *rqstp, xdrproc_t xargs, void *args)
1446 {
1447 	struct mbuf *m;
1448 	XDR xdrs;
1449 	bool_t stat;
1450 
1451 	m = rqstp->rq_args;
1452 	rqstp->rq_args = NULL;
1453 
1454 	xdrmbuf_create(&xdrs, m, XDR_DECODE);
1455 	stat = xargs(&xdrs, args);
1456 	XDR_DESTROY(&xdrs);
1457 
1458 	return (stat);
1459 }
1460 
1461 bool_t
svc_freeargs(struct svc_req * rqstp,xdrproc_t xargs,void * args)1462 svc_freeargs(struct svc_req *rqstp, xdrproc_t xargs, void *args)
1463 {
1464 	XDR xdrs;
1465 
1466 	if (rqstp->rq_addr) {
1467 		free(rqstp->rq_addr, M_SONAME);
1468 		rqstp->rq_addr = NULL;
1469 	}
1470 
1471 	xdrs.x_op = XDR_FREE;
1472 	return (xargs(&xdrs, args));
1473 }
1474 
1475 void
svc_freereq(struct svc_req * rqstp)1476 svc_freereq(struct svc_req *rqstp)
1477 {
1478 	SVCTHREAD *st;
1479 	SVCPOOL *pool;
1480 
1481 	st = rqstp->rq_thread;
1482 	if (st) {
1483 		pool = st->st_pool;
1484 		if (pool->sp_done)
1485 			pool->sp_done(st, rqstp);
1486 	}
1487 
1488 	if (rqstp->rq_auth.svc_ah_ops)
1489 		SVCAUTH_RELEASE(&rqstp->rq_auth);
1490 
1491 	if (rqstp->rq_xprt) {
1492 		SVC_RELEASE(rqstp->rq_xprt);
1493 	}
1494 
1495 	if (rqstp->rq_addr)
1496 		free(rqstp->rq_addr, M_SONAME);
1497 
1498 	if (rqstp->rq_args)
1499 		m_freem(rqstp->rq_args);
1500 
1501 	free(rqstp, M_RPC);
1502 }
1503