xref: /freebsd/sys/rpc/svc.h (revision 6bfca4dcab07dad45a805879d954876b353c0810)
1 /*	$NetBSD: svc.h,v 1.17 2000/06/02 22:57:56 fvdl 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 /*
34  * svc.h, Server-side remote procedure call interface.
35  *
36  * Copyright (C) 1986-1993 by Sun Microsystems, Inc.
37  */
38 
39 #ifndef _RPC_SVC_H
40 #define _RPC_SVC_H
41 #include <sys/cdefs.h>
42 
43 #ifdef _KERNEL
44 #include <sys/queue.h>
45 #include <sys/_lock.h>
46 #include <sys/_mutex.h>
47 #include <sys/_sx.h>
48 #include <sys/condvar.h>
49 #include <sys/sysctl.h>
50 #endif
51 
52 /*
53  * This interface must manage two items concerning remote procedure calling:
54  *
55  * 1) An arbitrary number of transport connections upon which rpc requests
56  * are received.  The two most notable transports are TCP and UDP;  they are
57  * created and registered by routines in svc_tcp.c and svc_udp.c, respectively;
58  * they in turn call xprt_register and xprt_unregister.
59  *
60  * 2) An arbitrary number of locally registered services.  Services are
61  * described by the following four data: program number, version number,
62  * "service dispatch" function, a transport handle, and a boolean that
63  * indicates whether or not the exported program should be registered with a
64  * local binder service;  if true the program's number and version and the
65  * port number from the transport handle are registered with the binder.
66  * These data are registered with the rpc svc system via svc_register.
67  *
68  * A service's dispatch function is called whenever an rpc request comes in
69  * on a transport.  The request's program and version numbers must match
70  * those of the registered service.  The dispatch function is passed two
71  * parameters, struct svc_req * and SVCXPRT *, defined below.
72  */
73 
74 /*
75  *      Service control requests
76  */
77 #define SVCGET_VERSQUIET	1
78 #define SVCSET_VERSQUIET	2
79 #define SVCGET_CONNMAXREC	3
80 #define SVCSET_CONNMAXREC	4
81 
82 /*
83  * Operations for rpc_control().
84  */
85 #define RPC_SVC_CONNMAXREC_SET  0	/* set max rec size, enable nonblock */
86 #define RPC_SVC_CONNMAXREC_GET  1
87 
88 enum xprt_stat {
89 	XPRT_DIED,
90 	XPRT_MOREREQS,
91 	XPRT_IDLE
92 };
93 
94 struct __rpc_svcxprt;
95 struct mbuf;
96 
97 struct xp_ops {
98 #ifdef _KERNEL
99 	/* receive incoming requests */
100 	bool_t	(*xp_recv)(struct __rpc_svcxprt *, struct rpc_msg *,
101 	    struct sockaddr **, struct mbuf **);
102 	/* get transport status */
103 	enum xprt_stat (*xp_stat)(struct __rpc_svcxprt *);
104 	/* get transport acknowledge sequence */
105 	bool_t (*xp_ack)(struct __rpc_svcxprt *, uint32_t *);
106 	/* send reply */
107 	bool_t	(*xp_reply)(struct __rpc_svcxprt *, struct rpc_msg *,
108 	    struct sockaddr *, struct mbuf *, uint32_t *);
109 	/* destroy this struct */
110 	void	(*xp_destroy)(struct __rpc_svcxprt *);
111 	/* catch-all function */
112 	bool_t  (*xp_control)(struct __rpc_svcxprt *, const u_int, void *);
113 #else
114 	/* receive incoming requests */
115 	bool_t	(*xp_recv)(struct __rpc_svcxprt *, struct rpc_msg *);
116 	/* get transport status */
117 	enum xprt_stat (*xp_stat)(struct __rpc_svcxprt *);
118 	/* get arguments */
119 	bool_t	(*xp_getargs)(struct __rpc_svcxprt *, xdrproc_t, void *);
120 	/* send reply */
121 	bool_t	(*xp_reply)(struct __rpc_svcxprt *, struct rpc_msg *);
122 	/* free mem allocated for args */
123 	bool_t	(*xp_freeargs)(struct __rpc_svcxprt *, xdrproc_t, void *);
124 	/* destroy this struct */
125 	void	(*xp_destroy)(struct __rpc_svcxprt *);
126 #endif
127 };
128 
129 #ifndef _KERNEL
130 struct xp_ops2 {
131 	/* catch-all function */
132 	bool_t  (*xp_control)(struct __rpc_svcxprt *, const u_int, void *);
133 };
134 #endif
135 
136 #ifdef _KERNEL
137 struct __rpc_svcpool;
138 struct __rpc_svcgroup;
139 struct __rpc_svcthread;
140 #endif
141 
142 /*
143  * Server side transport handle. In the kernel, transports have a
144  * reference count which tracks the number of currently assigned
145  * worker threads plus one for the service pool's reference.
146  * For NFSv4.1 sessions, a reference is also held for a backchannel.
147  * xp_p2 - Points to the CLIENT structure for the RPC server end
148  *         (the client end for callbacks).
149  *         Points to the private structure (cl_private) for the
150  *         CLIENT structure for the RPC client end (the server
151  *         end for callbacks).
152  */
153 typedef struct __rpc_svcxprt {
154 #ifdef _KERNEL
155 	volatile u_int	xp_refs;
156 	struct sx	xp_lock;
157 	struct __rpc_svcpool *xp_pool;  /* owning pool (see below) */
158 	struct __rpc_svcgroup *xp_group; /* owning group (see below) */
159 	TAILQ_ENTRY(__rpc_svcxprt) xp_link;
160 	TAILQ_ENTRY(__rpc_svcxprt) xp_alink;
161 	bool_t		xp_registered;	/* xprt_register has been called */
162 	bool_t		xp_active;	/* xprt_active has been called */
163 	struct __rpc_svcthread *xp_thread; /* assigned service thread */
164 	struct socket*	xp_socket;
165 	const struct xp_ops *xp_ops;
166 	char		*xp_netid;	/* network token */
167 	struct sockaddr_storage xp_ltaddr; /* local transport address */
168 	struct sockaddr_storage	xp_rtaddr; /* remote transport address */
169 	void		*xp_p1;		/* private: for use by svc ops */
170 	void		*xp_p2;		/* private: for use by svc ops */
171 	void		*xp_p3;		/* private: for use by svc lib */
172 	int		xp_type;	/* transport type */
173 	int		xp_idletimeout; /* idle time before closing */
174 	time_t		xp_lastactive;	/* time of last RPC */
175 	uint64_t	xp_sockref;	/* set by nfsv4 to identify socket */
176 	int		xp_upcallset;	/* socket upcall is set up */
177 	uint32_t	xp_snd_cnt;	/* # of bytes to send to socket */
178 	uint32_t	xp_snt_cnt;	/* # of bytes sent to socket */
179 	bool_t		xp_dontrcv;	/* Do not receive on the socket */
180 	uint32_t	xp_tls;		/* RPC-over-TLS on socket */
181 	uint64_t	xp_sslsec;	/* Userland SSL * */
182 	uint64_t	xp_sslusec;
183 	uint64_t	xp_sslrefno;
184 	int		xp_sslproc;	/* Which upcall daemon being used */
185 	int		xp_ngrps;	/* Cred. from TLS cert. */
186 	uid_t		xp_uid;
187 	gid_t		*xp_gidp;
188 #else
189 	int		xp_fd;
190 	u_short		xp_port;	 /* associated port number */
191 	const struct xp_ops *xp_ops;
192 	int		xp_addrlen;	 /* length of remote address */
193 	struct sockaddr_in xp_raddr;	 /* remote addr. (backward ABI compat) */
194 	/* XXX - fvdl stick this here for ABI backward compat reasons */
195 	const struct xp_ops2 *xp_ops2;
196 	char		*xp_tp;		 /* transport provider device name */
197 	char		*xp_netid;	 /* network token */
198 	struct netbuf	xp_ltaddr;	 /* local transport address */
199 	struct netbuf	xp_rtaddr;	 /* remote transport address */
200 	struct opaque_auth xp_verf;	 /* raw response verifier */
201 	void		*xp_p1;		 /* private: for use by svc ops */
202 	void		*xp_p2;		 /* private: for use by svc ops */
203 	void		*xp_p3;		 /* private: for use by svc lib */
204 	int		xp_type;	 /* transport type */
205 #endif
206 } SVCXPRT;
207 
208 /*
209  * Interface to server-side authentication flavors.
210  */
211 typedef struct __rpc_svcauth {
212 	const struct svc_auth_ops {
213 #ifdef _KERNEL
214 		int   (*svc_ah_wrap)(struct __rpc_svcauth *,  struct mbuf **);
215 		int   (*svc_ah_unwrap)(struct __rpc_svcauth *, struct mbuf **);
216 		void  (*svc_ah_release)(struct __rpc_svcauth *);
217 #else
218 		int   (*svc_ah_wrap)(struct __rpc_svcauth *, XDR *,
219 		    xdrproc_t, caddr_t);
220 		int   (*svc_ah_unwrap)(struct __rpc_svcauth *, XDR *,
221 		    xdrproc_t, caddr_t);
222 #endif
223 	} *svc_ah_ops;
224 	void *svc_ah_private;
225 } SVCAUTH;
226 
227 /*
228  * Server transport extensions (accessed via xp_p3).
229  */
230 typedef struct __rpc_svcxprt_ext {
231 	int		xp_flags;	/* versquiet */
232 	SVCAUTH		xp_auth;	/* interface to auth methods */
233 } SVCXPRT_EXT;
234 
235 #ifdef _KERNEL
236 
237 /*
238  * The services list
239  * Each entry represents a set of procedures (an rpc program).
240  * The dispatch routine takes request structs and runs the
241  * appropriate procedure.
242  */
243 struct svc_callout {
244 	TAILQ_ENTRY(svc_callout) sc_link;
245 	rpcprog_t	    sc_prog;
246 	rpcvers_t	    sc_vers;
247 	char		   *sc_netid;
248 	void		    (*sc_dispatch)(struct svc_req *, SVCXPRT *);
249 };
250 TAILQ_HEAD(svc_callout_list, svc_callout);
251 
252 /*
253  * The services connection loss list
254  * The dispatch routine takes request structs and runs the
255  * appropriate procedure.
256  */
257 struct svc_loss_callout {
258 	TAILQ_ENTRY(svc_loss_callout) slc_link;
259 	void		    (*slc_dispatch)(SVCXPRT *);
260 };
261 TAILQ_HEAD(svc_loss_callout_list, svc_loss_callout);
262 
263 /*
264  * Service request
265  */
266 struct svc_req {
267 	STAILQ_ENTRY(svc_req) rq_link;	/* list of requests for a thread */
268 	struct __rpc_svcthread *rq_thread; /* thread which is to execute this */
269 	uint32_t	rq_xid;		/* RPC transaction ID */
270 	uint32_t	rq_prog;	/* service program number */
271 	uint32_t	rq_vers;	/* service protocol version */
272 	uint32_t	rq_proc;	/* the desired procedure */
273 	size_t		rq_size;	/* space used by request */
274 	struct mbuf	*rq_args;	/* XDR-encoded procedure arguments */
275 	struct opaque_auth rq_cred;	/* raw creds from the wire */
276 	struct opaque_auth rq_verf;	/* verifier for the reply */
277 	void		*rq_clntcred;	/* read only cooked cred */
278 	SVCAUTH		rq_auth;	/* interface to auth methods */
279 	SVCXPRT		*rq_xprt;	/* associated transport */
280 	struct sockaddr	*rq_addr;	/* reply address or NULL if connected */
281 	void		*rq_p1;		/* application workspace */
282 	int		rq_p2;		/* application workspace */
283 	uint64_t	rq_p3;		/* application workspace */
284 	uint32_t	rq_reply_seq;	/* reply socket sequence # */
285 	char		rq_credarea[3*MAX_AUTH_BYTES];
286 };
287 STAILQ_HEAD(svc_reqlist, svc_req);
288 
289 #define svc_getrpccaller(rq)					\
290 	((rq)->rq_addr ? (rq)->rq_addr :			\
291 	    (struct sockaddr *) &(rq)->rq_xprt->xp_rtaddr)
292 
293 /*
294  * This structure is used to manage a thread which is executing
295  * requests from a service pool. A service thread is in one of three
296  * states:
297  *
298  *	SVCTHREAD_SLEEPING	waiting for a request to process
299  *	SVCTHREAD_ACTIVE	processing a request
300  *	SVCTHREAD_EXITING	exiting after finishing current request
301  *
302  * Threads which have no work to process sleep on the pool's sp_active
303  * list. When a transport becomes active, it is assigned a service
304  * thread to read and execute pending RPCs.
305  */
306 typedef struct __rpc_svcthread {
307 	struct mtx_padalign	st_lock; /* protects st_reqs field */
308 	struct __rpc_svcpool	*st_pool;
309 	SVCXPRT			*st_xprt; /* transport we are processing */
310 	struct svc_reqlist	st_reqs;  /* RPC requests to execute */
311 	struct cv		st_cond; /* sleeping for work */
312 	LIST_ENTRY(__rpc_svcthread) st_ilink; /* idle threads list */
313 	LIST_ENTRY(__rpc_svcthread) st_alink; /* application thread list */
314 	int		st_p2;		/* application workspace */
315 	uint64_t	st_p3;		/* application workspace */
316 } SVCTHREAD;
317 LIST_HEAD(svcthread_list, __rpc_svcthread);
318 
319 /*
320  * A thread group contain all information needed to assign subset of
321  * transports to subset of threads.  On systems with many CPUs and many
322  * threads that allows to reduce lock congestion and improve performance.
323  * Hundreds of threads on dozens of CPUs sharing the single pool lock do
324  * not scale well otherwise.
325  */
326 TAILQ_HEAD(svcxprt_list, __rpc_svcxprt);
327 enum svcpool_state {
328 	SVCPOOL_INIT,		/* svc_run not called yet */
329 	SVCPOOL_ACTIVE,		/* normal running state */
330 	SVCPOOL_THREADWANTED,	/* new service thread requested */
331 	SVCPOOL_THREADSTARTING,	/* new service thread started */
332 	SVCPOOL_CLOSING		/* svc_exit called */
333 };
334 typedef struct __rpc_svcgroup {
335 	struct mtx_padalign sg_lock;	/* protect the thread/req lists */
336 	struct __rpc_svcpool	*sg_pool;
337 	enum svcpool_state sg_state;	/* current pool state */
338 	struct svcxprt_list sg_xlist;	/* all transports in the group */
339 	struct svcxprt_list sg_active;	/* transports needing service */
340 	struct svcthread_list sg_idlethreads; /* idle service threads */
341 
342 	int		sg_minthreads;	/* minimum service thread count */
343 	int		sg_maxthreads;	/* maximum service thread count */
344 	int		sg_threadcount; /* current service thread count */
345 	time_t		sg_lastcreatetime; /* when we last started a thread */
346 	time_t		sg_lastidlecheck;  /* when we last checked idle transports */
347 } SVCGROUP;
348 
349 /*
350  * In the kernel, we can't use global variables to store lists of
351  * transports etc. since otherwise we could not have two unrelated RPC
352  * services running, each on its own thread. We solve this by
353  * importing a tiny part of a Solaris kernel concept, SVCPOOL.
354  *
355  * A service pool contains a set of transports and service callbacks
356  * for a set of related RPC services. The pool handle should be passed
357  * when creating new transports etc. Future work may include extending
358  * this to support something similar to the Solaris multi-threaded RPC
359  * server.
360  */
361 typedef SVCTHREAD *pool_assign_fn(SVCTHREAD *, struct svc_req *);
362 typedef void pool_done_fn(SVCTHREAD *, struct svc_req *);
363 #define	SVC_MAXGROUPS	16
364 typedef struct __rpc_svcpool {
365 	struct mtx_padalign sp_lock;	/* protect the transport lists */
366 	const char	*sp_name;	/* pool name (e.g. "nfsd", "NLM" */
367 	enum svcpool_state sp_state;	/* current pool state */
368 	struct proc	*sp_proc;	/* process which is in svc_run */
369 	struct svc_callout_list sp_callouts; /* (prog,vers)->dispatch list */
370 	struct svc_loss_callout_list sp_lcallouts; /* loss->dispatch list */
371 	int		sp_minthreads;	/* minimum service thread count */
372 	int		sp_maxthreads;	/* maximum service thread count */
373 
374 	/*
375 	 * Hooks to allow an application to control request to thread
376 	 * placement.
377 	 */
378 	pool_assign_fn	*sp_assign;
379 	pool_done_fn	*sp_done;
380 
381 	/*
382 	 * These variables are used to put an upper bound on the
383 	 * amount of memory used by RPC requests which are queued
384 	 * waiting for execution.
385 	 */
386 	unsigned long	sp_space_low;
387 	unsigned long	sp_space_high;
388 	unsigned long	sp_space_used;
389 	unsigned long	sp_space_used_highest;
390 	bool_t		sp_space_throttled;
391 	int		sp_space_throttle_count;
392 
393 	struct replay_cache *sp_rcache; /* optional replay cache */
394 	struct sysctl_ctx_list sp_sysctl;
395 
396 	int		sp_groupcount;	/* Number of groups in the pool. */
397 	int		sp_nextgroup;	/* Next group to assign port. */
398 	SVCGROUP	sp_groups[SVC_MAXGROUPS]; /* Thread/port groups. */
399 } SVCPOOL;
400 
401 #else
402 
403 /*
404  * Service request
405  */
406 struct svc_req {
407 	uint32_t	rq_prog;	/* service program number */
408 	uint32_t	rq_vers;	/* service protocol version */
409 	uint32_t	rq_proc;	/* the desired procedure */
410 	struct opaque_auth rq_cred;	/* raw creds from the wire */
411 	void		*rq_clntcred;	/* read only cooked cred */
412 	SVCXPRT		*rq_xprt;	/* associated transport */
413 };
414 
415 /*
416  *  Approved way of getting address of caller
417  */
418 #define svc_getrpccaller(x) (&(x)->xp_rtaddr)
419 
420 #endif
421 
422 /*
423  * Operations defined on an SVCXPRT handle
424  *
425  * SVCXPRT		*xprt;
426  * struct rpc_msg	*msg;
427  * xdrproc_t		 xargs;
428  * void *		 argsp;
429  */
430 #ifdef _KERNEL
431 
432 #define SVC_ACQUIRE(xprt)			\
433 	refcount_acquire(&(xprt)->xp_refs)
434 
435 #define SVC_RELEASE(xprt)			\
436 	if (refcount_release(&(xprt)->xp_refs))	\
437 		SVC_DESTROY(xprt)
438 
439 #define SVC_RECV(xprt, msg, addr, args)			\
440 	(*(xprt)->xp_ops->xp_recv)((xprt), (msg), (addr), (args))
441 
442 #define SVC_STAT(xprt)					\
443 	(*(xprt)->xp_ops->xp_stat)(xprt)
444 
445 #define SVC_ACK(xprt, ack)				\
446 	((xprt)->xp_ops->xp_ack == NULL ? FALSE :	\
447 	    ((ack) == NULL ? TRUE : (*(xprt)->xp_ops->xp_ack)((xprt), (ack))))
448 
449 #define SVC_REPLY(xprt, msg, addr, m, seq)			\
450 	(*(xprt)->xp_ops->xp_reply) ((xprt), (msg), (addr), (m), (seq))
451 
452 #define SVC_DESTROY(xprt)				\
453 	(*(xprt)->xp_ops->xp_destroy)(xprt)
454 
455 #define SVC_CONTROL(xprt, rq, in)			\
456 	(*(xprt)->xp_ops->xp_control)((xprt), (rq), (in))
457 
458 #else
459 
460 #define SVC_RECV(xprt, msg)				\
461 	(*(xprt)->xp_ops->xp_recv)((xprt), (msg))
462 #define svc_recv(xprt, msg)				\
463 	(*(xprt)->xp_ops->xp_recv)((xprt), (msg))
464 
465 #define SVC_STAT(xprt)					\
466 	(*(xprt)->xp_ops->xp_stat)(xprt)
467 #define svc_stat(xprt)					\
468 	(*(xprt)->xp_ops->xp_stat)(xprt)
469 
470 #define SVC_GETARGS(xprt, xargs, argsp)			\
471 	(*(xprt)->xp_ops->xp_getargs)((xprt), (xargs), (argsp))
472 #define svc_getargs(xprt, xargs, argsp)			\
473 	(*(xprt)->xp_ops->xp_getargs)((xprt), (xargs), (argsp))
474 
475 #define SVC_REPLY(xprt, msg)				\
476 	(*(xprt)->xp_ops->xp_reply) ((xprt), (msg))
477 #define svc_reply(xprt, msg)				\
478 	(*(xprt)->xp_ops->xp_reply) ((xprt), (msg))
479 
480 #define SVC_FREEARGS(xprt, xargs, argsp)		\
481 	(*(xprt)->xp_ops->xp_freeargs)((xprt), (xargs), (argsp))
482 #define svc_freeargs(xprt, xargs, argsp)		\
483 	(*(xprt)->xp_ops->xp_freeargs)((xprt), (xargs), (argsp))
484 
485 #define SVC_DESTROY(xprt)				\
486 	(*(xprt)->xp_ops->xp_destroy)(xprt)
487 #define svc_destroy(xprt)				\
488 	(*(xprt)->xp_ops->xp_destroy)(xprt)
489 
490 #define SVC_CONTROL(xprt, rq, in)			\
491 	(*(xprt)->xp_ops2->xp_control)((xprt), (rq), (in))
492 
493 #endif
494 
495 #define SVC_EXT(xprt)					\
496 	((SVCXPRT_EXT *) xprt->xp_p3)
497 
498 #define SVC_AUTH(xprt)					\
499 	(SVC_EXT(xprt)->xp_auth)
500 
501 /*
502  * Operations defined on an SVCAUTH handle
503  */
504 #ifdef _KERNEL
505 #define SVCAUTH_WRAP(auth, mp)		\
506 	((auth)->svc_ah_ops->svc_ah_wrap(auth, mp))
507 #define SVCAUTH_UNWRAP(auth, mp)	\
508 	((auth)->svc_ah_ops->svc_ah_unwrap(auth, mp))
509 #define SVCAUTH_RELEASE(auth)	\
510 	((auth)->svc_ah_ops->svc_ah_release(auth))
511 #else
512 #define SVCAUTH_WRAP(auth, xdrs, xfunc, xwhere)		\
513 	((auth)->svc_ah_ops->svc_ah_wrap(auth, xdrs, xfunc, xwhere))
514 #define SVCAUTH_UNWRAP(auth, xdrs, xfunc, xwhere)	\
515 	((auth)->svc_ah_ops->svc_ah_unwrap(auth, xdrs, xfunc, xwhere))
516 #endif
517 
518 /*
519  * Service registration
520  *
521  * svc_reg(xprt, prog, vers, dispatch, nconf)
522  *	const SVCXPRT *xprt;
523  *	const rpcprog_t prog;
524  *	const rpcvers_t vers;
525  *	const void (*dispatch)();
526  *	const struct netconfig *nconf;
527  */
528 
529 __BEGIN_DECLS
530 extern bool_t	svc_reg(SVCXPRT *, const rpcprog_t, const rpcvers_t,
531 			void (*)(struct svc_req *, SVCXPRT *),
532 			const struct netconfig *);
533 __END_DECLS
534 
535 /*
536  * Service un-registration
537  *
538  * svc_unreg(prog, vers)
539  *	const rpcprog_t prog;
540  *	const rpcvers_t vers;
541  */
542 
543 __BEGIN_DECLS
544 #ifdef _KERNEL
545 extern void	svc_unreg(SVCPOOL *, const rpcprog_t, const rpcvers_t);
546 #else
547 extern void	svc_unreg(const rpcprog_t, const rpcvers_t);
548 #endif
549 __END_DECLS
550 
551 #ifdef _KERNEL
552 /*
553  * Service connection loss registration
554  *
555  * svc_loss_reg(xprt, dispatch)
556  *	const SVCXPRT *xprt;
557  *	const void (*dispatch)();
558  */
559 
560 __BEGIN_DECLS
561 extern bool_t	svc_loss_reg(SVCXPRT *, void (*)(SVCXPRT *));
562 __END_DECLS
563 
564 /*
565  * Service connection loss un-registration
566  *
567  * svc_loss_unreg(xprt, dispatch)
568  *	const SVCXPRT *xprt;
569  *	const void (*dispatch)();
570  */
571 
572 __BEGIN_DECLS
573 extern void	svc_loss_unreg(SVCPOOL *, void (*)(SVCXPRT *));
574 __END_DECLS
575 #endif
576 
577 /*
578  * Transport registration.
579  *
580  * xprt_register(xprt)
581  *	SVCXPRT *xprt;
582  */
583 __BEGIN_DECLS
584 extern void	xprt_register(SVCXPRT *);
585 __END_DECLS
586 
587 /*
588  * Transport un-register
589  *
590  * xprt_unregister(xprt)
591  *	SVCXPRT *xprt;
592  */
593 __BEGIN_DECLS
594 extern void	xprt_unregister(SVCXPRT *);
595 extern void	__xprt_unregister_unlocked(SVCXPRT *);
596 __END_DECLS
597 
598 #ifdef _KERNEL
599 
600 /*
601  * Called when a transport has pending requests.
602  */
603 __BEGIN_DECLS
604 extern void	xprt_active(SVCXPRT *);
605 extern void	xprt_inactive(SVCXPRT *);
606 extern void	xprt_inactive_locked(SVCXPRT *);
607 extern void	xprt_inactive_self(SVCXPRT *);
608 __END_DECLS
609 
610 #endif
611 
612 /*
613  * When the service routine is called, it must first check to see if it
614  * knows about the procedure;  if not, it should call svcerr_noproc
615  * and return.  If so, it should deserialize its arguments via
616  * SVC_GETARGS (defined above).  If the deserialization does not work,
617  * svcerr_decode should be called followed by a return.  Successful
618  * decoding of the arguments should be followed the execution of the
619  * procedure's code and a call to svc_sendreply.
620  *
621  * Also, if the service refuses to execute the procedure due to too-
622  * weak authentication parameters, svcerr_weakauth should be called.
623  * Note: do not confuse access-control failure with weak authentication!
624  *
625  * NB: In pure implementations of rpc, the caller always waits for a reply
626  * msg.  This message is sent when svc_sendreply is called.
627  * Therefore pure service implementations should always call
628  * svc_sendreply even if the function logically returns void;  use
629  * xdr.h - xdr_void for the xdr routine.  HOWEVER, tcp based rpc allows
630  * for the abuse of pure rpc via batched calling or pipelining.  In the
631  * case of a batched call, svc_sendreply should NOT be called since
632  * this would send a return message, which is what batching tries to avoid.
633  * It is the service/protocol writer's responsibility to know which calls are
634  * batched and which are not.  Warning: responding to batch calls may
635  * deadlock the caller and server processes!
636  */
637 
638 __BEGIN_DECLS
639 #ifdef _KERNEL
640 extern bool_t	svc_sendreply(struct svc_req *, xdrproc_t, void *);
641 extern bool_t	svc_sendreply_mbuf(struct svc_req *, struct mbuf *);
642 extern void	svcerr_decode(struct svc_req *);
643 extern void	svcerr_weakauth(struct svc_req *);
644 extern void	svcerr_noproc(struct svc_req *);
645 extern void	svcerr_progvers(struct svc_req *, rpcvers_t, rpcvers_t);
646 extern void	svcerr_auth(struct svc_req *, enum auth_stat);
647 extern void	svcerr_noprog(struct svc_req *);
648 extern void	svcerr_systemerr(struct svc_req *);
649 #else
650 extern bool_t	svc_sendreply(SVCXPRT *, xdrproc_t, void *);
651 extern void	svcerr_decode(SVCXPRT *);
652 extern void	svcerr_weakauth(SVCXPRT *);
653 extern void	svcerr_noproc(SVCXPRT *);
654 extern void	svcerr_progvers(SVCXPRT *, rpcvers_t, rpcvers_t);
655 extern void	svcerr_auth(SVCXPRT *, enum auth_stat);
656 extern void	svcerr_noprog(SVCXPRT *);
657 extern void	svcerr_systemerr(SVCXPRT *);
658 #endif
659 extern int	rpc_reg(rpcprog_t, rpcvers_t, rpcproc_t,
660 			char *(*)(char *), xdrproc_t, xdrproc_t,
661 			char *);
662 __END_DECLS
663 
664 /*
665  * Lowest level dispatching -OR- who owns this process anyway.
666  * Somebody has to wait for incoming requests and then call the correct
667  * service routine.  The routine svc_run does infinite waiting; i.e.,
668  * svc_run never returns.
669  * Since another (co-existent) package may wish to selectively wait for
670  * incoming calls or other events outside of the rpc architecture, the
671  * routine svc_getreq is provided.  It must be passed readfds, the
672  * "in-place" results of a select system call (see select, section 2).
673  */
674 
675 #ifndef _KERNEL
676 /*
677  * Global keeper of rpc service descriptors in use
678  * dynamic; must be inspected before each call to select
679  */
680 extern int svc_maxfd;
681 #ifdef FD_SETSIZE
682 extern fd_set svc_fdset;
683 #define svc_fds svc_fdset.fds_bits[0]	/* compatibility */
684 #else
685 extern int svc_fds;
686 #endif /* def FD_SETSIZE */
687 #endif
688 
689 /*
690  * a small program implemented by the svc_rpc implementation itself;
691  * also see clnt.h for protocol numbers.
692  */
693 __BEGIN_DECLS
694 extern void rpctest_service(void);
695 __END_DECLS
696 
697 __BEGIN_DECLS
698 extern SVCXPRT *svc_xprt_alloc(void);
699 extern void	svc_xprt_free(SVCXPRT *);
700 #ifndef _KERNEL
701 extern void	svc_getreq(int);
702 extern void	svc_getreqset(fd_set *);
703 extern void	svc_getreq_common(int);
704 struct pollfd;
705 extern void	svc_getreq_poll(struct pollfd *, int);
706 extern void	svc_run(void);
707 extern void	svc_exit(void);
708 #else
709 extern void	svc_run(SVCPOOL *);
710 extern void	svc_exit(SVCPOOL *);
711 extern bool_t	svc_getargs(struct svc_req *, xdrproc_t, void *);
712 extern bool_t	svc_freeargs(struct svc_req *, xdrproc_t, void *);
713 extern void	svc_freereq(struct svc_req *);
714 
715 #endif
716 __END_DECLS
717 
718 /*
719  * Socket to use on svcxxx_create call to get default socket
720  */
721 #define	RPC_ANYSOCK	-1
722 #define RPC_ANYFD	RPC_ANYSOCK
723 
724 /*
725  * These are the existing service side transport implementations
726  */
727 
728 __BEGIN_DECLS
729 
730 #ifdef _KERNEL
731 
732 /*
733  * Create a new service pool.
734  */
735 extern SVCPOOL* svcpool_create(const char *name,
736     struct sysctl_oid_list *sysctl_base);
737 
738 /*
739  * Destroy a service pool, including all registered transports.
740  */
741 extern void svcpool_destroy(SVCPOOL *pool);
742 
743 /*
744  * Close a service pool.  Similar to svcpool_destroy(), but it does not
745  * free the data structures.  As such, the pool can be used again.
746  */
747 extern void svcpool_close(SVCPOOL *pool);
748 
749 /*
750  * Transport independent svc_create routine.
751  */
752 extern int svc_create(SVCPOOL *, void (*)(struct svc_req *, SVCXPRT *),
753     const rpcprog_t, const rpcvers_t, const char *);
754 /*
755  *      void (*dispatch)();             -- dispatch routine
756  *      const rpcprog_t prognum;        -- program number
757  *      const rpcvers_t versnum;        -- version number
758  *      const char *nettype;            -- network type
759  */
760 
761 
762 /*
763  * Generic server creation routine. It takes a netconfig structure
764  * instead of a nettype.
765  */
766 
767 extern SVCXPRT *svc_tp_create(SVCPOOL *, void (*)(struct svc_req *, SVCXPRT *),
768     const rpcprog_t, const rpcvers_t, const char *uaddr,
769     const struct netconfig *);
770         /*
771          * void (*dispatch)();            -- dispatch routine
772          * const rpcprog_t prognum;       -- program number
773          * const rpcvers_t versnum;       -- version number
774 	 * const char *uaddr;		  -- universal address of service
775          * const struct netconfig *nconf; -- netconfig structure
776          */
777 
778 extern SVCXPRT *svc_dg_create(SVCPOOL *, struct socket *,
779     const size_t, const size_t);
780         /*
781          * struct socket *;                             -- open connection
782          * const size_t sendsize;                        -- max send size
783          * const size_t recvsize;                        -- max recv size
784          */
785 
786 extern SVCXPRT *svc_vc_create(SVCPOOL *, struct socket *,
787     const size_t, const size_t);
788         /*
789          * struct socket *;                             -- open connection
790          * const size_t sendsize;                        -- max send size
791          * const size_t recvsize;                        -- max recv size
792          */
793 
794 extern SVCXPRT *svc_vc_create_backchannel(SVCPOOL *);
795 
796 extern void *clnt_bck_create(struct socket *, const rpcprog_t, const rpcvers_t);
797 	/*
798 	 * struct socket *;			-- server transport socket
799 	 * const rpcprog_t prog;		-- RPC program number
800 	 * const rpcvers_t vers;		-- RPC program version
801 	 */
802 
803 /*
804  * Generic TLI create routine
805  */
806 extern SVCXPRT *svc_tli_create(SVCPOOL *, struct socket *,
807     const struct netconfig *, const struct t_bind *, const size_t, const size_t);
808 /*
809  *      struct socket * so;             -- connection end point
810  *      const struct netconfig *nconf;  -- netconfig structure for network
811  *      const struct t_bind *bindaddr;  -- local bind address
812  *      const size_t sendsz;             -- max sendsize
813  *      const size_t recvsz;             -- max recvsize
814  */
815 
816 #else /* !_KERNEL */
817 
818 /*
819  * Transport independent svc_create routine.
820  */
821 extern int svc_create(void (*)(struct svc_req *, SVCXPRT *),
822 			   const rpcprog_t, const rpcvers_t, const char *);
823 /*
824  *      void (*dispatch)();             -- dispatch routine
825  *      const rpcprog_t prognum;        -- program number
826  *      const rpcvers_t versnum;        -- version number
827  *      const char *nettype;            -- network type
828  */
829 
830 
831 /*
832  * Generic server creation routine. It takes a netconfig structure
833  * instead of a nettype.
834  */
835 
836 extern SVCXPRT *svc_tp_create(void (*)(struct svc_req *, SVCXPRT *),
837 				   const rpcprog_t, const rpcvers_t,
838 				   const struct netconfig *);
839         /*
840          * void (*dispatch)();            -- dispatch routine
841          * const rpcprog_t prognum;       -- program number
842          * const rpcvers_t versnum;       -- version number
843          * const struct netconfig *nconf; -- netconfig structure
844          */
845 
846 /*
847  * Generic TLI create routine
848  */
849 extern SVCXPRT *svc_tli_create(const int, const struct netconfig *,
850 			       const struct t_bind *, const u_int,
851 			       const u_int);
852 /*
853  *      const int fd;                   -- connection end point
854  *      const struct netconfig *nconf;  -- netconfig structure for network
855  *      const struct t_bind *bindaddr;  -- local bind address
856  *      const u_int sendsz;             -- max sendsize
857  *      const u_int recvsz;             -- max recvsize
858  */
859 
860 /*
861  * Connectionless and connectionful create routines
862  */
863 
864 extern SVCXPRT *svc_vc_create(const int, const u_int, const u_int);
865 /*
866  *      const int fd;                           -- open connection end point
867  *      const u_int sendsize;                   -- max send size
868  *      const u_int recvsize;                   -- max recv size
869  */
870 
871 /*
872  * Added for compatibility to old rpc 4.0. Obsoleted by svc_vc_create().
873  */
874 extern SVCXPRT *svcunix_create(int, u_int, u_int, char *);
875 
876 extern SVCXPRT *svc_dg_create(const int, const u_int, const u_int);
877         /*
878          * const int fd;                                -- open connection
879          * const u_int sendsize;                        -- max send size
880          * const u_int recvsize;                        -- max recv size
881          */
882 
883 
884 /*
885  * the routine takes any *open* connection
886  * descriptor as its first input and is used for open connections.
887  */
888 extern SVCXPRT *svc_fd_create(const int, const u_int, const u_int);
889 /*
890  *      const int fd;                           -- open connection end point
891  *      const u_int sendsize;                   -- max send size
892  *      const u_int recvsize;                   -- max recv size
893  */
894 
895 /*
896  * Added for compatibility to old rpc 4.0. Obsoleted by svc_fd_create().
897  */
898 extern SVCXPRT *svcunixfd_create(int, u_int, u_int);
899 
900 /*
901  * Memory based rpc (for speed check and testing)
902  */
903 extern SVCXPRT *svc_raw_create(void);
904 
905 /*
906  * svc_dg_enable_cache() enables the cache on dg transports.
907  */
908 int svc_dg_enablecache(SVCXPRT *, const u_int);
909 
910 int __rpc_get_local_uid(SVCXPRT *_transp, uid_t *_uid);
911 
912 #endif	/* !_KERNEL */
913 
914 __END_DECLS
915 
916 #ifndef _KERNEL
917 /* for backward compatibility */
918 #include <rpc/svc_soc.h>
919 #endif
920 
921 #endif /* !_RPC_SVC_H */
922