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