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