xref: /titanic_51/usr/src/uts/common/rpc/svc.h (revision 36fe4a92b52649b0979d6a13212f4cea730d19c7)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
27 /* All Rights Reserved */
28 /*
29  * Portions of this source code were derived from Berkeley
30  * 4.3 BSD under license from the Regents of the University of
31  * California.
32  */
33 
34 /*
35  * svc.h, Server-side remote procedure call interface.
36  */
37 
38 #ifndef	_RPC_SVC_H
39 #define	_RPC_SVC_H
40 
41 #pragma ident	"%Z%%M%	%I%	%E% SMI"
42 
43 #include <rpc/rpc_com.h>
44 #include <rpc/rpc_msg.h>
45 #include <sys/tihdr.h>
46 #include <sys/poll.h>
47 
48 #ifdef	_KERNEL
49 #include <rpc/svc_auth.h>
50 #include <sys/callb.h>
51 #endif	/* _KERNEL */
52 
53 /*
54  * This interface must manage two items concerning remote procedure calling:
55  *
56  * 1) An arbitrary number of transport connections upon which rpc requests
57  * are received. They are created and registered by routines in svc_generic.c,
58  * svc_vc.c and svc_dg.c; they in turn call xprt_register and
59  * xprt_unregister.
60  *
61  * 2) An arbitrary number of locally registered services.  Services are
62  * described by the following four data: program number, version number,
63  * "service dispatch" function, a transport handle, and a boolean that
64  * indicates whether or not the exported program should be registered with a
65  * local binder service;  if true the program's number and version and the
66  * address from the transport handle are registered with the binder.
67  * These data are registered with rpcbind via svc_reg().
68  *
69  * A service's dispatch function is called whenever an rpc request comes in
70  * on a transport.  The request's program and version numbers must match
71  * those of the registered service.  The dispatch function is passed two
72  * parameters, struct svc_req * and SVCXPRT *, defined below.
73  */
74 
75 #ifdef __cplusplus
76 extern "C" {
77 #endif
78 
79 /*
80  * Server-side transport handles.
81  * The actual type definitions are below.
82  */
83 #ifdef	_KERNEL
84 typedef struct __svcmasterxprt	SVCMASTERXPRT;	/* Master transport handle */
85 typedef struct __svcxprt	SVCXPRT;	/* Per-thread clone handle */
86 typedef	struct __svcpool	SVCPOOL;	/* Kernel thread pool	   */
87 #else	/* _KERNEL */
88 typedef struct __svcxprt	SVCXPRT;	/* Server transport handle */
89 #endif	/* _KERNEL */
90 
91 /*
92  *  Prototype of error handler callback
93  */
94 #ifndef _KERNEL
95 typedef void (*svc_errorhandler_t)(const SVCXPRT* svc, const bool_t isAConn);
96 #endif
97 
98 /*
99  * Service request.
100  *
101  * PSARC 2003/523 Contract Private Interface
102  * svc_req
103  * Changes must be reviewed by Solaris File Sharing
104  * Changes must be communicated to contract-2003-523@sun.com
105  */
106 struct svc_req {
107 	rpcprog_t	rq_prog;	/* service program number */
108 	rpcvers_t	rq_vers;	/* service protocol version */
109 	rpcproc_t	rq_proc;	/* the desired procedure */
110 	struct opaque_auth rq_cred;	/* raw creds from the wire */
111 	caddr_t		rq_clntcred;	/* read only cooked cred */
112 	SVCXPRT		*rq_xprt;	/* associated transport */
113 };
114 
115 #ifdef _KERNEL
116 struct dupreq {
117 	uint32_t	dr_xid;
118 	rpcproc_t	dr_proc;
119 	rpcvers_t	dr_vers;
120 	rpcprog_t	dr_prog;
121 	struct netbuf	dr_addr;
122 	struct netbuf	dr_resp;
123 	void		(*dr_resfree)();
124 	int		dr_status;
125 	struct dupreq	*dr_next;
126 	struct dupreq	*dr_chain;
127 };
128 
129 /*
130  * States of requests for duplicate request caching.
131  */
132 #define	DUP_NEW			0x00	/* new entry */
133 #define	DUP_INPROGRESS		0x01	/* request already going */
134 #define	DUP_DONE		0x02	/* request done */
135 #define	DUP_DROP		0x03	/* request dropped */
136 #define	DUP_ERROR		0x04	/* error in dup req cache */
137 
138 /*
139  * Prototype for a service dispatch routine.
140  */
141 typedef void (SVC_DISPATCH)(struct svc_req *, SVCXPRT *);
142 
143 /*
144  * The service provider callout.
145  * Each entry identifies a dispatch routine to be called
146  * for a given RPC program number and a version fitting
147  * into the registered range.
148  */
149 typedef struct {
150 	rpcprog_t	sc_prog;	/* RPC Program number */
151 	rpcvers_t	sc_versmin;	/* Min version number */
152 	rpcvers_t	sc_versmax;	/* Max version number */
153 	SVC_DISPATCH	*sc_dispatch;	/* Dispatch routine   */
154 } SVC_CALLOUT;
155 
156 /*
157  * Table of service provider `callouts' for an RPC
158  * transport handle. If sct_free is TRUE then transport
159  * destructor is supposed to deallocate this table.
160  */
161 typedef struct {
162 	size_t		sct_size;	/* Number of entries  */
163 	bool_t		sct_free;	/* Deallocate if true */
164 	SVC_CALLOUT	*sct_sc;	/* Callout entries    */
165 } SVC_CALLOUT_TABLE;
166 
167 struct svc_ops {
168 	bool_t	(*xp_recv)(SVCXPRT *, mblk_t *, struct rpc_msg *);
169 		/* receive incoming requests */
170 	bool_t	(*xp_getargs)(SVCXPRT *, xdrproc_t, caddr_t);
171 		/* get arguments */
172 	bool_t	(*xp_reply)(SVCXPRT *, struct rpc_msg *);
173 		/* send reply */
174 	bool_t	(*xp_freeargs)(SVCXPRT *, xdrproc_t, caddr_t);
175 		/* free mem allocated for args */
176 	void	(*xp_destroy)(SVCMASTERXPRT *);
177 		/* destroy this struct */
178 	int	(*xp_dup)(struct svc_req *, caddr_t, int,
179 				struct dupreq **, bool_t *);
180 		/* check for dup */
181 	void	(*xp_dupdone)(struct dupreq *, caddr_t, void (*)(), int, int);
182 		/* mark dup entry as completed */
183 	int32_t *(*xp_getres)(SVCXPRT *, int);
184 		/* get pointer to response buffer */
185 	void	(*xp_freeres)(SVCXPRT *);
186 		/* destroy pre-serialized response */
187 	void	(*xp_clone_destroy)(SVCXPRT *);
188 		/* destroy a clone xprt */
189 	void	(*xp_start)(SVCMASTERXPRT *);
190 		/* `ready-to-receive' */
191 };
192 #else	/* _KERNEL */
193 /*
194  *	Service control requests
195  */
196 #define	SVCGET_VERSQUIET	1
197 #define	SVCSET_VERSQUIET	2
198 #define	SVCGET_XID		4
199 #define	SVCSET_KEEPALIVE	5
200 #define	SVCSET_CONNMAXREC	6
201 #define	SVCGET_CONNMAXREC	7
202 #define	SVCGET_RECVERRHANDLER	8
203 #define	SVCSET_RECVERRHANDLER	9
204 
205 enum xprt_stat {
206 	XPRT_DIED,
207 	XPRT_MOREREQS,
208 	XPRT_IDLE
209 };
210 
211 struct xp_ops {
212 #ifdef	__STDC__
213 	bool_t	(*xp_recv)(SVCXPRT *, struct rpc_msg *);
214 		/* receive incoming requests */
215 	enum xprt_stat (*xp_stat)(SVCXPRT *);
216 		/* get transport status */
217 	bool_t	(*xp_getargs)(SVCXPRT *, xdrproc_t, caddr_t);
218 		/* get arguments */
219 	bool_t	(*xp_reply)(SVCXPRT *,	struct rpc_msg *);
220 		/* send reply */
221 	bool_t	(*xp_freeargs)(SVCXPRT *, xdrproc_t, caddr_t);
222 		/* free mem allocated for args */
223 	void	(*xp_destroy)(SVCXPRT *);
224 		/* destroy this struct */
225 	bool_t	(*xp_control)(SVCXPRT *, const uint_t,	void *);
226 		/* catch-all control function */
227 #else	/* __STDC__ */
228 	bool_t	(*xp_recv)(); /* receive incoming requests */
229 	enum xprt_stat (*xp_stat)(); /* get transport status */
230 	bool_t	(*xp_getargs)(); /* get arguments */
231 	bool_t	(*xp_reply)(); /* send reply */
232 	bool_t	(*xp_freeargs)(); /* free mem allocated for args */
233 	void	(*xp_destroy)(); /* destroy this struct */
234 	bool_t	(*xp_control)(); /* catch-all control function */
235 #endif	/* __STDC__ */
236 };
237 #endif	/* _KERNEL */
238 
239 #ifdef	_KERNEL
240 /*
241  * SVCPOOL
242  * Kernel RPC server-side thread pool structure.
243  */
244 typedef struct __svcxprt_qnode __SVCXPRT_QNODE;	/* Defined in svc.c */
245 
246 struct __svcpool {
247 	/*
248 	 * Thread pool variables.
249 	 *
250 	 * The pool's thread lock p_thread_lock protects:
251 	 * - p_threads, p_detached_threads, p_reserved_threads and p_closing
252 	 * The pool's request lock protects:
253 	 * - p_asleep, p_drowsy, p_reqs, p_walkers, p_req_cv.
254 	 * The following fields are `initialized constants':
255 	 * - p_id, p_stksize, p_timeout.
256 	 * Access to p_next and p_prev is protected by the pool
257 	 * list lock.
258 	 */
259 	SVCPOOL		*p_next;		/* Next pool in the list  */
260 	SVCPOOL		*p_prev;		/* Prev pool in the list  */
261 	int		p_id;			/* Pool id		  */
262 	int		p_threads;		/* Non-detached threads	  */
263 	int		p_detached_threads;	/* Detached threads	  */
264 	int		p_maxthreads;		/* Max threads in the pool */
265 	int		p_redline;		/* `Redline' for the pool */
266 	int		p_reserved_threads;	/* Reserved threads	  */
267 	kmutex_t	p_thread_lock;		/* Thread lock		  */
268 	int		p_asleep;		/* Asleep threads	  */
269 	int		p_drowsy;		/* Drowsy flag		  */
270 	kcondvar_t 	p_req_cv;		/* svc_poll() sleep var.  */
271 	clock_t		p_timeout;		/* svc_poll() timeout	  */
272 	kmutex_t	p_req_lock;		/* Request lock		  */
273 	int		p_reqs;			/* Pending requests	  */
274 	int		p_walkers;		/* Walking threads	  */
275 	int		p_max_same_xprt;	/* Max reqs from the xprt */
276 	int		p_stksize;		/* Stack size for svc_run */
277 	bool_t		p_closing : 1;		/* Pool is closing	  */
278 
279 	/*
280 	 * Thread creator variables.
281 	 * The `creator signaled' flag is turned on when a signal is send
282 	 * to the creator thread (to create a new service thread). The
283 	 * creator clears when the thread is created. The protocol is not
284 	 * to signal the creator thread when the flag is on. However,
285 	 * a new thread should signal the creator if there are more
286 	 * requests in the queue.
287 	 *
288 	 * When the pool is closing (ie it has been already unregistered from
289 	 * the pool list) the last thread on the last transport should turn
290 	 * the p_creator_exit flag on. This tells the creator thread to
291 	 * free the pool structure and exit.
292 	 */
293 	bool_t		p_creator_signaled : 1;	/* Create requested flag  */
294 	bool_t		p_creator_exit : 1;	/* If true creator exits  */
295 	kcondvar_t	p_creator_cv;		/* Creator cond. variable */
296 	kmutex_t	p_creator_lock;		/* Creator lock		  */
297 
298 	/*
299 	 * Doubly linked list containing `registered' master transport handles.
300 	 * There is no special structure for a list node. Instead the
301 	 * SVCMASTERXPRT structure has the xp_next and xp_prev fields.
302 	 *
303 	 * The p_lrwlock protects access to xprt->xp_next and xprt->xp_prev.
304 	 * A service thread should also acquire a reader lock before accessing
305 	 * any transports it is no longer linked to (to prevent them from
306 	 * being destroyed).
307 	 *
308 	 * The list lock governs also the `pool is closing' flag.
309 	 */
310 	size_t		p_lcount;		/* Current count	  */
311 	SVCMASTERXPRT	*p_lhead;		/* List head		  */
312 	krwlock_t	p_lrwlock;		/* R/W lock		  */
313 
314 	/*
315 	 * Circular linked list for the `xprt-ready' queue (FIFO).
316 	 * Must be initialized with svc_xprt_qinit() before it is used.
317 	 *
318 	 * The writer's end is protected by the pool's request lock
319 	 * (pool->p_req_lock). The reader's end is protected by q_end_lock.
320 	 *
321 	 * When the queue is full the p_qoverflow flag is raised. It stays
322 	 * on until all the pending request are drained.
323 	 */
324 	size_t		p_qsize;		/* Number of queue nodes  */
325 	int		p_qoverflow : 1;	/* Overflow flag	  */
326 	__SVCXPRT_QNODE *p_qbody;		/* Queue body (array)	  */
327 	__SVCXPRT_QNODE *p_qtop;		/* Writer's end of FIFO   */
328 	__SVCXPRT_QNODE *p_qend;		/* Reader's end of FIFO	  */
329 	kmutex_t	p_qend_lock;		/* Reader's end lock	  */
330 
331 	/*
332 	 * Userspace thread creator variables.
333 	 * Thread creation is actually done in userland, via a thread
334 	 * that is parked in the kernel. When that thread is signaled,
335 	 * it returns back down to the daemon from whence it came and
336 	 * does the lwp create.
337 	 *
338 	 * A parallel "creator" thread runs in the kernel. That is the
339 	 * thread that will signal for the user thread to return to
340 	 * userland and do its work.
341 	 *
342 	 * Since the thread doesn't always exist (there could be a race
343 	 * if two threads are created in rapid succession), we set
344 	 * p_signal_create_thread to FALSE when we're ready to accept work.
345 	 *
346 	 * p_user_exit is set to true when the service pool is about
347 	 * to close. This is done so that the user creation thread
348 	 * can be informed and cleanup any userland state.
349 	 */
350 
351 	bool_t		p_signal_create_thread : 1; /* Create requested flag  */
352 	bool_t		p_user_exit : 1;	/* If true creator exits  */
353 	bool_t		p_user_waiting : 1;	/* Thread waiting for work */
354 	kcondvar_t	p_user_cv;		/* Creator cond. variable */
355 	kmutex_t	p_user_lock;		/* Creator lock		  */
356 	void		(*p_offline)();		/* callout for unregister */
357 	void		(*p_shutdown)();	/* callout for shutdown */
358 };
359 
360 /*
361  * Server side transport handle (SVCMASTERXPRT).
362  * xprt->xp_req_lock governs the following fields in xprt:
363  *		xp_req_head, xp_req_tail.
364  * xprt->xp_thread_lock governs the following fields in xprt:
365  *		xp_threads, xp_detached_threads.
366  *
367  * xp_req_tail is only valid if xp_req_head is non-NULL
368  *
369  * The xp_threads count is the number of attached threads.  These threads
370  * are able to handle new requests, and it is expected that they will not
371  * block for a very long time handling a given request. The
372  * xp_detached_threads count is the number of threads that have detached
373  * themselves from the transport. These threads can block indefinitely
374  * while handling a request.  Once they complete the request, they exit.
375  *
376  * A kernel service provider may register a callback function "closeproc"
377  * for a transport.  When the transport is closing the last exiting attached
378  * thread - xp_threads goes to zero - it calls the callback function, passing
379  * it a reference to the transport.  This call is made with xp_thread_lock
380  * held, so any cleanup bookkeeping it does should be done quickly.
381  *
382  * When the transport is closing the last exiting thread is supposed
383  * to destroy/free the data structure.
384  */
385 typedef struct __svcxprt_common {
386 	struct file	*xpc_fp;
387 	struct svc_ops	*xpc_ops;
388 	queue_t		*xpc_wq;	/* queue to write onto		*/
389 	cred_t		*xpc_cred;	/* cached cred for server to use */
390 	int32_t		xpc_type;	/* transport type		*/
391 	int		xpc_msg_size;	/* TSDU or TIDU size		*/
392 	struct netbuf	xpc_rtaddr;	/* remote transport address	*/
393 	SVC_CALLOUT_TABLE *xpc_sct;
394 } __SVCXPRT_COMMON;
395 
396 #define	xp_fp		xp_xpc.xpc_fp
397 #define	xp_ops		xp_xpc.xpc_ops
398 #define	xp_wq		xp_xpc.xpc_wq
399 #define	xp_cred		xp_xpc.xpc_cred
400 #define	xp_type		xp_xpc.xpc_type
401 #define	xp_msg_size	xp_xpc.xpc_msg_size
402 #define	xp_rtaddr	xp_xpc.xpc_rtaddr
403 #define	xp_sct		xp_xpc.xpc_sct
404 
405 struct __svcmasterxprt {
406 	SVCMASTERXPRT 	*xp_next;	/* Next transport in the list	*/
407 	SVCMASTERXPRT 	*xp_prev;	/* Prev transport in the list	*/
408 	__SVCXPRT_COMMON xp_xpc;	/* Fields common with the clone	*/
409 	SVCPOOL		*xp_pool;	/* Pointer to the pool		*/
410 	mblk_t		*xp_req_head;	/* Request queue head		*/
411 	mblk_t		*xp_req_tail;	/* Request queue tail		*/
412 	kmutex_t	xp_req_lock;	/* Request lock			*/
413 	int		xp_threads;	/* Current num. of attached threads */
414 	int		xp_detached_threads; /* num. of detached threads */
415 	kmutex_t	xp_thread_lock;	/* Thread count lock		*/
416 	void		(*xp_closeproc)(const SVCMASTERXPRT *);
417 					/* optional; see comments above	*/
418 	char		*xp_netid;	/* network token		*/
419 	struct netbuf	xp_addrmask;	/* address mask			*/
420 
421 	caddr_t		xp_p2;		/* private: for use by svc ops  */
422 };
423 
424 /*
425  * Service thread `clone' transport handle (SVCXPRT)
426  *
427  * PSARC 2003/523 Contract Private Interface
428  * SVCXPRT
429  * Changes must be reviewed by Solaris File Sharing
430  * Changes must be communicated to contract-2003-523@sun.com
431  *
432  * The xp_p2buf buffer is used as the storage for a transport type
433  * specific structure. It is private for the svc ops for a given
434  * transport type.
435  */
436 
437 #define	SVC_P2LEN   64
438 
439 struct __svcxprt {
440 	__SVCXPRT_COMMON xp_xpc;
441 	SVCMASTERXPRT	*xp_master;	/* back ptr to master		*/
442 
443 	/* The following fileds are on a per-thread basis */
444 	callb_cpr_t	*xp_cprp;	/* unused padding for Contract	*/
445 	bool_t		xp_reserved : 1; /* is thread reserved?		*/
446 	bool_t		xp_detached : 1; /* is thread detached?		*/
447 	int		xp_same_xprt;	/* Reqs from the same xprt	*/
448 
449 	/* The following fields are used on a per-request basis */
450 	struct opaque_auth xp_verf;	/* raw response verifier	*/
451 	SVCAUTH		xp_auth;	/* auth flavor of current req	*/
452 	void		*xp_cookie;	/* a cookie			*/
453 	uint32_t	xp_xid;		/* id				*/
454 	XDR		xp_xdrin;	/* input xdr stream		*/
455 	XDR		xp_xdrout;	/* output xdr stream		*/
456 
457 	/* Private for svc ops */
458 	char		xp_p2buf[SVC_P2LEN]; /* udp_data or cots_data_t */
459 						/* or clone_rdma_data_t */
460 };
461 #else	/* _KERNEL */
462 struct __svcxprt {
463 	int		xp_fd;
464 #define	xp_sock		xp_fd
465 	ushort_t	xp_port;
466 	/*
467 	 * associated port number.
468 	 * Obsolete, but still used to
469 	 * specify whether rendezvouser
470 	 * or normal connection
471 	 */
472 	struct	xp_ops	*xp_ops;
473 	int		xp_addrlen;	/* length of remote addr. Obsoleted */
474 	char		*xp_tp;		/* transport provider device name */
475 	char		*xp_netid;	/* network token */
476 	struct netbuf	xp_ltaddr;	/* local transport address */
477 	struct netbuf	xp_rtaddr;	/* remote transport address */
478 	char		xp_raddr[16];	/* remote address. Now obsoleted */
479 	struct opaque_auth xp_verf;	/* raw response verifier */
480 	caddr_t		xp_p1;		/* private: for use by svc ops */
481 	caddr_t		xp_p2;		/* private: for use by svc ops */
482 	caddr_t		xp_p3;		/* private: for use by svc lib */
483 	int		xp_type;	/* transport type */
484 	/*
485 	 * callback on client death
486 	 * First parameter is the current structure,
487 	 * Second parameter :
488 	 *	- FALSE for the service listener
489 	 *	- TRUE for a real connected socket
490 	 */
491 	svc_errorhandler_t xp_closeclnt;
492 };
493 #endif	/* _KERNEL */
494 
495 /*
496  *  Approved way of getting address of caller,
497  *  address mask, and netid of transport.
498  */
499 #define	svc_getrpccaller(x) (&(x)->xp_rtaddr)
500 #ifdef _KERNEL
501 #define	svc_getcaller(x) (&(x)->xp_rtaddr.buf)
502 #define	svc_getaddrmask(x) (&(x)->xp_master->xp_addrmask)
503 #define	svc_getnetid(x) ((x)->xp_master->xp_netid)
504 #endif	/* _KERNEL */
505 
506 /*
507  * Operations defined on an SVCXPRT handle
508  */
509 
510 #ifdef	_KERNEL
511 #define	SVC_RECV(clone_xprt, mp, msg) \
512 	(*(clone_xprt)->xp_ops->xp_recv)((clone_xprt), (mp), (msg))
513 
514 /*
515  * PSARC 2003/523 Contract Private Interface
516  * SVC_GETARGS
517  * Changes must be reviewed by Solaris File Sharing
518  * Changes must be communicated to contract-2003-523@sun.com
519  */
520 #define	SVC_GETARGS(clone_xprt, xargs, argsp) \
521 	(*(clone_xprt)->xp_ops->xp_getargs)((clone_xprt), (xargs), (argsp))
522 
523 #define	SVC_REPLY(clone_xprt, msg) \
524 	(*(clone_xprt)->xp_ops->xp_reply) ((clone_xprt), (msg))
525 
526 #define	SVC_FREEARGS(clone_xprt, xargs, argsp) \
527 	(*(clone_xprt)->xp_ops->xp_freeargs)((clone_xprt), (xargs), (argsp))
528 
529 #define	SVC_GETRES(clone_xprt, size) \
530 	(*(clone_xprt)->xp_ops->xp_getres)((clone_xprt), (size))
531 
532 #define	SVC_FREERES(clone_xprt)	\
533 	(*(clone_xprt)->xp_ops->xp_freeres)(clone_xprt)
534 
535 #define	SVC_DESTROY(xprt) \
536 	(*(xprt)->xp_ops->xp_destroy)(xprt)
537 
538 /*
539  * PSARC 2003/523 Contract Private Interfaces
540  * SVC_DUP, SVC_DUPDONE, SVC_DUP_EXT, SVC_DUPDONE_EXT
541  * Changes must be reviewed by Solaris File Sharing
542  * Changes must be communicated to contract-2003-523@sun.com
543  *
544  * SVC_DUP and SVC_DUPDONE are defined here for backward compatibility.
545  */
546 #define	SVC_DUP_EXT(clone_xprt, req, res, size, drpp, dupcachedp) \
547 	(*(clone_xprt)->xp_ops->xp_dup)(req, res, size, drpp, dupcachedp)
548 
549 #define	SVC_DUPDONE_EXT(clone_xprt, dr, res, resfree, size, status) \
550 	(*(clone_xprt)->xp_ops->xp_dupdone)(dr, res, resfree, size, status)
551 
552 #define	SVC_DUP(clone_xprt, req, res, size, drpp) \
553 	(*(clone_xprt)->xp_ops->xp_dup)(req, res, size, drpp, NULL)
554 
555 #define	SVC_DUPDONE(clone_xprt, dr, res, size, status) \
556 	(*(clone_xprt)->xp_ops->xp_dupdone)(dr, res, NULL, size, status)
557 
558 #define	SVC_CLONE_DESTROY(clone_xprt) \
559 	(*(clone_xprt)->xp_ops->xp_clone_destroy)(clone_xprt)
560 
561 
562 #define	SVC_START(xprt) \
563 	(*(xprt)->xp_ops->xp_start)(xprt)
564 
565 #else	/* _KERNEL */
566 
567 #define	SVC_RECV(xprt, msg) \
568 	(*(xprt)->xp_ops->xp_recv)((xprt), (msg))
569 #define	svc_recv(xprt, msg) \
570 	(*(xprt)->xp_ops->xp_recv)((xprt), (msg))
571 
572 #define	SVC_STAT(xprt) \
573 	(*(xprt)->xp_ops->xp_stat)(xprt)
574 #define	svc_stat(xprt) \
575 	(*(xprt)->xp_ops->xp_stat)(xprt)
576 
577 #define	SVC_GETARGS(xprt, xargs, argsp) \
578 	(*(xprt)->xp_ops->xp_getargs)((xprt), (xargs), (argsp))
579 #define	svc_getargs(xprt, xargs, argsp)	\
580 	(*(xprt)->xp_ops->xp_getargs)((xprt), (xargs), (argsp))
581 
582 #define	SVC_REPLY(xprt, msg) \
583 	(*(xprt)->xp_ops->xp_reply) ((xprt), (msg))
584 #define	svc_reply(xprt, msg) \
585 	(*(xprt)->xp_ops->xp_reply) ((xprt), (msg))
586 
587 #define	SVC_FREEARGS(xprt, xargs, argsp) \
588 	(*(xprt)->xp_ops->xp_freeargs)((xprt), (xargs), (argsp))
589 #define	svc_freeargs(xprt, xargs, argsp) \
590 	(*(xprt)->xp_ops->xp_freeargs)((xprt), (xargs), (argsp))
591 
592 #define	SVC_GETRES(xprt, size) \
593 	(*(xprt)->xp_ops->xp_getres)((xprt), (size))
594 #define	svc_getres(xprt, size) \
595 	(*(xprt)->xp_ops->xp_getres)((xprt), (size))
596 
597 #define	SVC_FREERES(xprt) \
598 	(*(xprt)->xp_ops->xp_freeres)(xprt)
599 #define	svc_freeres(xprt) \
600 	(*(xprt)->xp_ops->xp_freeres)(xprt)
601 
602 #define	SVC_DESTROY(xprt) \
603 	(*(xprt)->xp_ops->xp_destroy)(xprt)
604 #define	svc_destroy(xprt) \
605 	(*(xprt)->xp_ops->xp_destroy)(xprt)
606 
607 /*
608  * PSARC 2003/523 Contract Private Interface
609  * SVC_CONTROL
610  * Changes must be reviewed by Solaris File Sharing
611  * Changes must be communicated to contract-2003-523@sun.com
612  */
613 #define	SVC_CONTROL(xprt, rq, in) \
614 	(*(xprt)->xp_ops->xp_control)((xprt), (rq), (in))
615 #endif	/* _KERNEL */
616 
617 /*
618  * Pool id's reserved for NFS, NLM, and the NFSv4 callback program.
619  */
620 #define	NFS_SVCPOOL_ID		0x01
621 #define	NLM_SVCPOOL_ID		0x02
622 #define	NFS_CB_SVCPOOL_ID	0x03
623 #define	RDC_SVCPOOL_ID		0x05	/* SNDR, PSARC 2001/699 */
624 
625 struct svcpool_args {
626 	uint32_t	id;		/* Pool id */
627 	uint32_t	maxthreads;	/* Max threads in the pool */
628 	uint32_t	redline;	/* `Redline' for the pool */
629 	uint32_t	qsize;		/* `xprt-ready' queue size */
630 	uint32_t	timeout;	/* svc_poll() timeout */
631 	uint32_t	stksize;	/* svc_run() stack size */
632 	uint32_t	max_same_xprt;	/* Max reqs from the same xprt */
633 };
634 
635 
636 #ifdef	_KERNEL
637 /*
638  * Transport registration and thread pool creation.
639  */
640 extern int	svc_xprt_register(SVCMASTERXPRT *, int);
641 extern void	svc_xprt_unregister(SVCMASTERXPRT *);
642 extern int	svc_pool_create(struct svcpool_args *);
643 extern int	svc_wait(int);
644 extern int	svc_do_run(int);
645 #define	SVCPSET_SHUTDOWN_PROC	1
646 #define	SVCPSET_UNREGISTER_PROC	2
647 extern int	svc_pool_control(int, int, void *);
648 #else	/* _KERNEL */
649 #ifdef	__STDC__
650 extern bool_t	rpc_reg(const rpcprog_t, const rpcvers_t, const rpcproc_t,
651 			char *(*)(char *), const xdrproc_t, const xdrproc_t,
652 			const char *);
653 
654 /*
655  * Service registration
656  *
657  * svc_reg(xprt, prog, vers, dispatch, nconf)
658  *	const SVCXPRT *xprt;
659  *	const rpcprog_t prog;
660  *	const rpcvers_t vers;
661  *	const void (*dispatch)();
662  *	const struct netconfig *nconf;
663  */
664 extern bool_t	svc_reg(const SVCXPRT *, const rpcprog_t, const rpcvers_t,
665 			void (*)(struct svc_req *, SVCXPRT *),
666 			const struct netconfig *);
667 
668 /*
669  * Service authentication registration
670  *
671  * svc_auth_reg(cred_flavor, handler)
672  *    int cred_flavor;
673  *    enum auth_stat (*handler)();
674  */
675 extern int	svc_auth_reg(int, enum auth_stat (*)());
676 
677 /*
678  * Service un-registration
679  *
680  * svc_unreg(prog, vers)
681  *	const rpcprog_t prog;
682  *	const rpcvers_t vers;
683  */
684 extern void	svc_unreg(const rpcprog_t, const rpcvers_t);
685 
686 /*
687  * Transport registration/unregistration.
688  *
689  * xprt_register(xprt)
690  *	const SVCXPRT *xprt;
691  *
692  * xprt_unregister(xprt)
693  *	const SVCXPRT *xprt;
694  */
695 extern void	xprt_register(const SVCXPRT *);
696 extern void	xprt_unregister(const SVCXPRT *);
697 #else	/* __STDC__ */
698 extern bool_t	rpc_reg();
699 extern bool_t	svc_reg();
700 extern bool_t	svc_auth_reg();
701 extern void	svc_unreg();
702 extern void	xprt_register();
703 extern void	xprt_unregister();
704 #endif /* __STDC__ */
705 #endif	/* _KERNEL */
706 
707 
708 /*
709  * When the service routine is called, it must first check to see if it
710  * knows about the procedure;  if not, it should call svcerr_noproc
711  * and return.  If so, it should deserialize its arguments via
712  * SVC_GETARGS (defined above).  If the deserialization does not work,
713  * svcerr_decode should be called followed by a return.  Successful
714  * decoding of the arguments should be followed the execution of the
715  * procedure's code and a call to svc_sendreply.
716  *
717  * Also, if the service refuses to execute the procedure due to too-
718  * weak authentication parameters, svcerr_weakauth should be called.
719  * Note: do not confuse access-control failure with weak authentication!
720  *
721  * NB: In pure implementations of rpc, the caller always waits for a reply
722  * msg.  This message is sent when svc_sendreply is called.
723  * Therefore pure service implementations should always call
724  * svc_sendreply even if the function logically returns void;  use
725  * xdr.h - xdr_void for the xdr routine.  HOWEVER, connectionful rpc allows
726  * for the abuse of pure rpc via batched calling or pipelining.  In the
727  * case of a batched call, svc_sendreply should NOT be called since
728  * this would send a return message, which is what batching tries to avoid.
729  * It is the service/protocol writer's responsibility to know which calls are
730  * batched and which are not.  Warning: responding to batch calls may
731  * deadlock the caller and server processes!
732  */
733 #ifdef	__STDC__
734 extern bool_t	svc_sendreply(const SVCXPRT *, const xdrproc_t,	const caddr_t);
735 extern void	svcerr_decode(const SVCXPRT *);
736 extern void	svcerr_weakauth(const SVCXPRT *);
737 extern void	svcerr_noproc(const SVCXPRT *);
738 extern void	svcerr_progvers(const SVCXPRT *, const rpcvers_t,
739     const rpcvers_t);
740 extern void	svcerr_auth(const SVCXPRT *, const enum auth_stat);
741 extern void	svcerr_noprog(const SVCXPRT *);
742 extern void	svcerr_systemerr(const SVCXPRT *);
743 #else	/* __STDC__ */
744 extern bool_t	svc_sendreply();
745 extern void	svcerr_decode();
746 extern void	svcerr_weakauth();
747 extern void	svcerr_noproc();
748 extern void	svcerr_progvers();
749 extern void	svcerr_auth();
750 extern void	svcerr_noprog();
751 extern void	svcerr_systemerr();
752 #endif	/* __STDC__ */
753 
754 #ifdef	_KERNEL
755 /*
756  * Kernel RPC functions.
757  */
758 extern void	svc_init(void);
759 extern void	svc_cots_init(void);
760 extern void	svc_clts_init(void);
761 extern void	mt_kstat_init(void);
762 extern void	mt_kstat_fini(void);
763 extern int	svc_tli_kcreate(struct file *, uint_t, char *,
764 				struct netbuf *, SVCMASTERXPRT **,
765 				SVC_CALLOUT_TABLE *,
766 				void (*closeproc)(const SVCMASTERXPRT *),
767 				int, bool_t);
768 extern int	svc_clts_kcreate(struct file *, uint_t, struct T_info_ack *,
769 				SVCMASTERXPRT **);
770 extern int	svc_cots_kcreate(struct file *, uint_t, struct T_info_ack *,
771 				SVCMASTERXPRT **);
772 extern void	svc_queuereq(queue_t *, mblk_t *);
773 extern void	svc_queueclean(queue_t *);
774 extern void	svc_queueclose(queue_t *);
775 extern int	svc_reserve_thread(SVCXPRT *);
776 extern void	svc_unreserve_thread(SVCXPRT *);
777 extern callb_cpr_t *svc_detach_thread(SVCXPRT *);
778 
779 /*
780  * For RDMA based kRPC.
781  * "rdma_xprt_record" is a reference to master transport handles
782  * in kRPC thread pools. This is an easy way of tracking and shuting
783  * down rdma based kRPC transports on demand.
784  * "rdma_xprt_group" is a list of RDMA based mster transport handles
785  * or records in a kRPC thread pool.
786  */
787 typedef struct rdma_xprt_record		rdma_xprt_record_t;
788 struct rdma_xprt_record {
789 	int			rtr_type;	/* Type of rdma; IB/VI/RDDP */
790 	SVCMASTERXPRT		*rtr_xprt_ptr;	/* Ptr to master xprt handle */
791 	rdma_xprt_record_t	*rtr_next;	/* Ptr to next record */
792 };
793 
794 typedef struct {
795 	int			rtg_count;	/* Number transport records */
796 	int			rtg_poolid;	/* Pool Id for this group */
797 	rdma_xprt_record_t	*rtg_listhead;	/* Head of the records list */
798 } rdma_xprt_group_t;
799 
800 extern int	svc_rdma_kcreate(char *, SVC_CALLOUT_TABLE *, int,
801 			rdma_xprt_group_t *);
802 extern void	svc_rdma_kstop(SVCMASTERXPRT *);
803 extern void	svc_rdma_kdestroy(SVCMASTERXPRT *);
804 extern void	rdma_stop(rdma_xprt_group_t);
805 
806 /*
807  * GSS cleanup method.
808  */
809 extern void	rpc_gss_cleanup(SVCXPRT *);
810 #else	/* _KERNEL */
811 /*
812  * Lowest level dispatching -OR- who owns this process anyway.
813  * Somebody has to wait for incoming requests and then call the correct
814  * service routine.  The routine svc_run does infinite waiting; i.e.,
815  * svc_run never returns.
816  * Since another (co-existant) package may wish to selectively wait for
817  * incoming calls or other events outside of the rpc architecture, the
818  * routine svc_getreq_poll is provided.  It must be passed pollfds, the
819  * "in-place" results of a poll call (see poll, section 2).
820  */
821 
822 /*
823  * Global keeper of rpc service descriptors in use
824  * dynamic; must be inspected before each call to select or poll
825  */
826 extern pollfd_t	*svc_pollfd;
827 extern int	svc_max_pollfd;
828 extern fd_set	svc_fdset;
829 #if !defined(_LP64) && FD_SETSIZE > 1024
830 extern fd_set	_new_svc_fdset;
831 #ifdef __PRAGMA_REDEFINE_EXTNAME
832 #pragma redefine_extname	svc_fdset	_new_svc_fdset
833 #else   /* __PRAGMA_REDEFINE_EXTNAME */
834 #define	svc_fdset	_new_svc_fdset
835 #endif  /* __PRAGMA_REDEFINE_EXTNAME */
836 #endif	/* LP64 && FD_SETSIZE > 1024 */
837 #define	svc_fds svc_fdset.fds_bits[0]	/* compatibility */
838 
839 /*
840  * A small program implemented by the svc_rpc implementation itself.
841  * Also see clnt.h for protocol numbers.
842  */
843 #ifdef __STDC__
844 extern void	svc_getreq(int);
845 extern void	svc_getreq_common(const int);
846 extern void	svc_getreqset(fd_set *); /* takes fdset instead of int */
847 extern void	svc_getreq_poll(struct pollfd *, const int);
848 extern void	svc_run(void);
849 extern void	svc_exit(void);
850 #else	/* __STDC__ */
851 extern void	rpctest_service();
852 extern void	svc_getreqset();
853 extern void	svc_getreq();
854 extern void	svc_getreq_common();
855 extern void	svc_getreqset();	 /* takes fdset instead of int */
856 extern void	svc_getreq_poll();
857 extern void	svc_run();
858 extern void	svc_exit();
859 #endif	/* __STDC__ */
860 
861 /*
862  *  Functions used to manage user file descriptors
863  */
864 typedef int svc_input_id_t;
865 typedef void (*svc_callback_t)(svc_input_id_t id, int fd,
866 				unsigned int events, void* cookie);
867 
868 #ifdef __STDC__
869 extern svc_input_id_t svc_add_input(int fd, unsigned int events,
870 				svc_callback_t user_callback,
871 				void* cookie);
872 extern int svc_remove_input(svc_input_id_t id);
873 #else	/* __STDC__ */
874 extern svc_input_id_t svc_add_input();
875 extern int	svc_remove_input();
876 #endif
877 
878 /*
879  * These are the existing service side transport implementations.
880  *
881  * Transport independent svc_create routine.
882  */
883 #ifdef __STDC__
884 extern int	svc_create(void (*)(struct svc_req *, SVCXPRT *),
885 				const rpcprog_t, const rpcvers_t,
886 				const char *);
887 	/*
888 	 * 	void (*dispatch)();		-- dispatch routine
889 	 *	const rpcprog_t prognum;	-- program number
890 	 *	const rpcvers_t versnum;	-- version number
891 	 *	const char *nettype;		-- network type
892 	 */
893 
894 /*
895  * Generic server creation routine. It takes a netconfig structure
896  * instead of a nettype.
897  */
898 extern SVCXPRT	*svc_tp_create(void (*)(struct svc_req *, SVCXPRT *),
899 				const rpcprog_t, const rpcvers_t,
900 				const struct netconfig *);
901 	/*
902 	 * void (*dispatch)();			-- dispatch routine
903 	 * const rpcprog_t prognum;		-- program number
904 	 * const rpcvers_t versnum;		-- version number
905 	 * const struct netconfig *nconf;	-- netconfig structure
906 	 */
907 
908 /*
909  * Generic TLI create routine
910  */
911 extern  SVCXPRT	*svc_tli_create(const int, const struct netconfig *,
912 				const struct t_bind *, const uint_t,
913 				const uint_t);
914 	/*
915 	 *	const int fd;			-- connection end point
916 	 *	const struct netconfig *nconf;	-- netconfig structure
917 	 *	const struct t_bind *bindaddr;	-- local bind address
918 	 *	const uint_t sendsz;		-- max sendsize
919 	 *	const uint_t recvsz;		-- max recvsize
920 	 */
921 
922 /*
923  * Connectionless and connectionful create routines.
924  */
925 extern SVCXPRT	*svc_vc_create(const int, const uint_t, const uint_t);
926 	/*
927 	 *	const int fd;			-- open connection end point
928 	 *	const uint_t sendsize;		-- max send size
929 	 *	const uint_t recvsize;		-- max recv size
930 	 */
931 
932 extern SVCXPRT	*svc_dg_create(const int, const uint_t, const uint_t);
933 	/*
934 	 * const int fd;			-- open connection
935 	 * const uint_t sendsize;		-- max send size
936 	 * const uint_t recvsize;		-- max recv size
937 	 */
938 
939 /*
940  * the routine takes any *open* TLI file
941  * descriptor as its first input and is used for open connections.
942  */
943 extern  SVCXPRT	*svc_fd_create(const int, const uint_t, const uint_t);
944 	/*
945 	 * 	const int fd;			-- open connection end point
946 	 * 	const uint_t sendsize;		-- max send size
947 	 * 	const uint_t recvsize;		-- max recv size
948 	 */
949 
950 /*
951  * Memory based rpc (for speed check and testing)
952  */
953 extern SVCXPRT	*svc_raw_create(void);
954 
955 /*
956  * Creation of service over doors transport.
957  */
958 extern SVCXPRT	*svc_door_create(void (*)(struct svc_req *, SVCXPRT *),
959 				const rpcprog_t, const rpcvers_t,
960 				const uint_t);
961 	/*
962 	 * 	void (*dispatch)();		-- dispatch routine
963 	 *	const rpcprog_t prognum;	-- program number
964 	 *	const rpcvers_t versnum;	-- version number
965 	 *	const uint_t sendsize;		-- send buffer size
966 	 */
967 
968 /*
969  * Service control interface
970  */
971 extern	bool_t	svc_control(SVCXPRT *, const uint_t, void *);
972 	/*
973 	 *	SVCXPRT *svc;			-- service to manipulate
974 	 *	const uint_t req;		-- request
975 	 *	void *info;			-- argument to request
976 	 */
977 
978 /*
979  * svc_dg_enable_cache() enables the cache on dg transports.
980  */
981 extern int svc_dg_enablecache(SVCXPRT *, const uint_t);
982 #else	/* __STDC__ */
983 extern int	svc_create();
984 extern SVCXPRT	*svc_tp_create();
985 extern SVCXPRT	*svc_tli_create();
986 extern SVCXPRT	*svc_vc_create();
987 extern SVCXPRT	*svc_dg_create();
988 extern SVCXPRT	*svc_fd_create();
989 extern SVCXPRT	*svc_raw_create();
990 extern SVCXPRT	*svc_door_create();
991 extern int svc_dg_enablecache();
992 #endif	/* __STDC__ */
993 
994 #ifdef	PORTMAP
995 /* For backward compatibility */
996 #include <rpc/svc_soc.h>
997 #endif	/* PORTMAP */
998 
999 /*
1000  * For user level MT hot server functions
1001  */
1002 
1003 /*
1004  * Different MT modes
1005  */
1006 #define	RPC_SVC_MT_NONE		0	/* default, single-threaded */
1007 #define	RPC_SVC_MT_AUTO		1	/* automatic MT mode */
1008 #define	RPC_SVC_MT_USER		2	/* user MT mode */
1009 
1010 #ifdef	__STDC__
1011 extern void	svc_done(SVCXPRT *);
1012 #else
1013 extern void	svc_done();
1014 #endif	/* __STDC__ */
1015 
1016 /*
1017  * Obtaining local credentials.
1018  */
1019 typedef struct __svc_local_cred_t {
1020 	uid_t	euid;	/* effective uid */
1021 	gid_t	egid;	/* effective gid */
1022 	uid_t	ruid;	/* real uid */
1023 	gid_t	rgid;	/* real gid */
1024 	pid_t	pid;	/* caller's pid, or -1 if not available */
1025 } svc_local_cred_t;
1026 
1027 #ifdef __STDC__
1028 struct ucred_s;
1029 extern void	svc_fd_negotiate_ucred(int);
1030 extern int	svc_getcallerucred(const SVCXPRT *, struct ucred_s **);
1031 extern bool_t	svc_get_local_cred(SVCXPRT *, svc_local_cred_t *);
1032 #else
1033 extern void	svc_fd_negotiate_ucred();
1034 extern int	svc_getcallerucred();
1035 extern bool_t	svc_get_local_cred();
1036 #endif	/* __STDC__ */
1037 
1038 /*
1039  * Private interfaces and structures for user level duplicate request caching.
1040  * The interfaces and data structures are not committed and subject to
1041  * change in future releases. Currently only intended for use by automountd.
1042  */
1043 struct dupreq {
1044 	uint32_t	dr_xid;
1045 	rpcproc_t	dr_proc;
1046 	rpcvers_t	dr_vers;
1047 	rpcprog_t	dr_prog;
1048 	struct netbuf	dr_addr;
1049 	struct netbuf	dr_resp;
1050 	int		dr_status;
1051 	time_t		dr_time;
1052 	uint_t		dr_hash;
1053 	struct dupreq	*dr_next;
1054 	struct dupreq	*dr_prev;
1055 	struct dupreq	*dr_chain;
1056 	struct dupreq	*dr_prevchain;
1057 };
1058 
1059 /*
1060  * The fixedtime state is defined if we want to expand the routines to
1061  * handle and encompass fixed size caches.
1062  */
1063 #define	DUPCACHE_FIXEDTIME	0
1064 
1065 /*
1066  * States of requests for duplicate request caching.
1067  * These are the same as defined for the kernel.
1068  */
1069 #define	DUP_NEW			0x00	/* new entry */
1070 #define	DUP_INPROGRESS		0x01	/* request already going */
1071 #define	DUP_DONE		0x02	/* request done */
1072 #define	DUP_DROP		0x03	/* request dropped */
1073 #define	DUP_ERROR		0x04	/* error in dup req cache */
1074 
1075 #ifdef __STDC__
1076 extern bool_t	__svc_dupcache_init(void *, int, char **);
1077 extern int	__svc_dup(struct svc_req *, caddr_t *, uint_t *, char *);
1078 extern int	__svc_dupdone(struct svc_req *, caddr_t, uint_t, int, char *);
1079 extern bool_t	__svc_vc_dupcache_init(SVCXPRT *, void *, int);
1080 extern int	__svc_vc_dup(struct svc_req *, caddr_t *, uint_t *);
1081 extern int	__svc_vc_dupdone(struct svc_req *, caddr_t, uint_t, int);
1082 #else
1083 extern bool_t	__svc_dupcache_init();
1084 extern int	__svc_dup();
1085 extern int	__svc_dupdone();
1086 extern bool_t	__svc_vc_dupcache_init();
1087 extern int	__svc_vc_dup();
1088 extern int	__svc_vc_dupdone();
1089 #endif	/* __STDC__ */
1090 #endif	/* _KERNEL */
1091 
1092 #ifdef	__cplusplus
1093 }
1094 #endif
1095 
1096 #endif	/* !_RPC_SVC_H */
1097