xref: /titanic_44/usr/src/lib/libnsl/rpc/svc_raw.c (revision 8eea8e29cc4374d1ee24c25a07f45af132db3499)
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  * Copyright 2003 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
34 
35 /*
36  * svc_raw.c,   This a toy for simple testing and timing.
37  * Interface to create an rpc client and server in the same UNIX process.
38  * This lets us similate rpc and get rpc (round trip) overhead, without
39  * any interference from the kernal.
40  *
41  */
42 
43 #include "mt.h"
44 #include "rpc_mt.h"
45 #include <rpc/rpc.h>
46 #include <sys/types.h>
47 #include <rpc/trace.h>
48 #include <rpc/raw.h>
49 #include <syslog.h>
50 
51 #ifndef UDPMSGSIZE
52 #define	UDPMSGSIZE 8800
53 #endif
54 
55 /*
56  * This is the "network" that we will be moving data over
57  */
58 static struct svc_raw_private {
59 	char	*raw_buf;	/* should be shared with the cl handle */
60 	SVCXPRT	*server;
61 	XDR	xdr_stream;
62 	char	verf_body[MAX_AUTH_BYTES];
63 } *svc_raw_private;
64 
65 static struct xp_ops *svc_raw_ops();
66 extern char *calloc();
67 extern void free();
68 extern mutex_t	svcraw_lock;
69 
70 
71 
72 SVCXPRT *
73 svc_raw_create()
74 {
75 	struct svc_raw_private *srp;
76 	bool_t flag1 = FALSE, flag2 = FALSE;
77 
78 /* VARIABLES PROTECTED BY svcraw_lock: svc_raw_private, srp */
79 	trace1(TR_svc_raw_create, 0);
80 	mutex_lock(&svcraw_lock);
81 	srp = svc_raw_private;
82 	if (srp == NULL) {
83 /* LINTED pointer alignment */
84 		srp = (struct svc_raw_private *)calloc(1, sizeof (*srp));
85 		if (srp == NULL) {
86 			syslog(LOG_ERR, "svc_raw_create: out of memory");
87 			mutex_unlock(&svcraw_lock);
88 			trace1(TR_svc_raw_create, 1);
89 			return ((SVCXPRT *)NULL);
90 		}
91 		flag1 = TRUE;
92 		if (_rawcombuf == NULL) {
93 			_rawcombuf = (char *)calloc(UDPMSGSIZE, sizeof (char));
94 			if (_rawcombuf == NULL) {
95 				free((char *)srp);
96 				syslog(LOG_ERR, "svc_raw_create: "
97 					"out of memory");
98 				mutex_unlock(&svcraw_lock);
99 				trace1(TR_svc_raw_create, 1);
100 				return ((SVCXPRT *)NULL);
101 			}
102 			flag2 = TRUE;
103 		}
104 		srp->raw_buf = _rawcombuf; /* Share it with the client */
105 		svc_raw_private = srp;
106 	}
107 	if ((srp->server = svc_xprt_alloc()) == NULL) {
108 		if (flag2)
109 			free(svc_raw_private->raw_buf);
110 		if (flag1)
111 			free(svc_raw_private);
112 		mutex_unlock(&svcraw_lock);
113 		trace1(TR_svc_raw_create, 1);
114 		return ((SVCXPRT *)NULL);
115 	}
116 	/*
117 	 * By convention, using FD_SETSIZE as the psuedo file descriptor
118 	 */
119 	srp->server->xp_fd = FD_SETSIZE;
120 	srp->server->xp_port = 0;
121 	srp->server->xp_ops = svc_raw_ops();
122 	srp->server->xp_verf.oa_base = srp->verf_body;
123 	xdrmem_create(&srp->xdr_stream, srp->raw_buf, UDPMSGSIZE, XDR_DECODE);
124 	xprt_register(srp->server);
125 	mutex_unlock(&svcraw_lock);
126 	trace1(TR_svc_raw_create, 1);
127 	return (srp->server);
128 }
129 
130 /*ARGSUSED*/
131 static enum xprt_stat
132 svc_raw_stat(xprt)
133 SVCXPRT *xprt; /* args needed to satisfy ANSI-C typechecking */
134 {
135 	trace1(TR_svc_raw_stat, 0);
136 	trace1(TR_svc_raw_stat, 1);
137 	return (XPRT_IDLE);
138 }
139 
140 /*ARGSUSED*/
141 static bool_t
142 svc_raw_recv(xprt, msg)
143 	SVCXPRT *xprt;
144 	struct rpc_msg *msg;
145 {
146 	struct svc_raw_private *srp;
147 	XDR *xdrs;
148 
149 	trace1(TR_svc_raw_recv, 0);
150 	mutex_lock(&svcraw_lock);
151 	srp = svc_raw_private;
152 	if (srp == NULL) {
153 		mutex_unlock(&svcraw_lock);
154 		trace1(TR_svc_raw_recv, 1);
155 		return (FALSE);
156 	}
157 	mutex_unlock(&svcraw_lock);
158 
159 	xdrs = &srp->xdr_stream;
160 	xdrs->x_op = XDR_DECODE;
161 	(void) XDR_SETPOS(xdrs, 0);
162 	if (! xdr_callmsg(xdrs, msg)) {
163 		trace1(TR_svc_raw_recv, 1);
164 		return (FALSE);
165 	}
166 	trace1(TR_svc_raw_recv, 1);
167 	return (TRUE);
168 }
169 
170 /*ARGSUSED*/
171 static bool_t
172 svc_raw_reply(xprt, msg)
173 	SVCXPRT *xprt;
174 	struct rpc_msg *msg;
175 {
176 	struct svc_raw_private *srp;
177 	XDR *xdrs;
178 
179 	trace1(TR_svc_raw_reply, 0);
180 	mutex_lock(&svcraw_lock);
181 	srp = svc_raw_private;
182 	if (srp == NULL) {
183 		mutex_unlock(&svcraw_lock);
184 		trace1(TR_svc_raw_reply, 1);
185 		return (FALSE);
186 	}
187 	mutex_unlock(&svcraw_lock);
188 
189 	xdrs = &srp->xdr_stream;
190 	xdrs->x_op = XDR_ENCODE;
191 	(void) XDR_SETPOS(xdrs, 0);
192 	if (! xdr_replymsg(xdrs, msg)) {
193 		trace1(TR_svc_raw_reply, 1);
194 		return (FALSE);
195 	}
196 	(void) XDR_GETPOS(xdrs);  /* called just for overhead */
197 	trace1(TR_svc_raw_reply, 1);
198 	return (TRUE);
199 }
200 
201 /*ARGSUSED*/
202 static bool_t
203 svc_raw_getargs(xprt, xdr_args, args_ptr)
204 	SVCXPRT *xprt;
205 	xdrproc_t xdr_args;
206 	caddr_t args_ptr;
207 {
208 	struct svc_raw_private *srp;
209 	bool_t dummy1;
210 
211 	trace1(TR_svc_raw_getargs, 0);
212 	mutex_lock(&svcraw_lock);
213 	srp = svc_raw_private;
214 	if (srp == NULL) {
215 		mutex_unlock(&svcraw_lock);
216 		trace1(TR_svc_raw_getargs, 1);
217 		return (FALSE);
218 	}
219 	mutex_unlock(&svcraw_lock);
220 	dummy1 = (*xdr_args)(&srp->xdr_stream, args_ptr);
221 	trace1(TR_svc_raw_getargs, 1);
222 	return (dummy1);
223 }
224 
225 /*ARGSUSED*/
226 static bool_t
227 svc_raw_freeargs(xprt, xdr_args, args_ptr)
228 	SVCXPRT *xprt;
229 	xdrproc_t xdr_args;
230 	caddr_t args_ptr;
231 {
232 	struct svc_raw_private *srp;
233 	XDR *xdrs;
234 	bool_t dummy2;
235 
236 	trace1(TR_svc_raw_freeargs, 0);
237 	mutex_lock(&svcraw_lock);
238 	srp = svc_raw_private;
239 	if (srp == NULL) {
240 		mutex_unlock(&svcraw_lock);
241 		trace1(TR_svc_raw_freeargs, 1);
242 		return (FALSE);
243 	}
244 	mutex_unlock(&svcraw_lock);
245 
246 	xdrs = &srp->xdr_stream;
247 	xdrs->x_op = XDR_FREE;
248 	dummy2 = (*xdr_args)(xdrs, args_ptr);
249 	trace1(TR_svc_raw_freeargs, 1);
250 	return (dummy2);
251 }
252 
253 /*ARGSUSED*/
254 static void
255 svc_raw_destroy(xprt)
256 SVCXPRT *xprt;
257 {
258 	trace1(TR_svc_raw_destroy, 0);
259 	trace1(TR_svc_raw_destroy, 1);
260 }
261 
262 /*ARGSUSED*/
263 static bool_t
264 svc_raw_control(xprt, rq, in)
265 	SVCXPRT *xprt;
266 	const uint_t	rq;
267 	void		*in;
268 {
269 	trace3(TR_svc_raw_control, 0, xprt, rq);
270 	switch (rq) {
271 	case SVCGET_XID: /* fall through for now */
272 	default:
273 		trace1(TR_svc_raw_control, 1);
274 		return (FALSE);
275 	}
276 }
277 
278 static struct xp_ops *
279 svc_raw_ops()
280 {
281 	static struct xp_ops ops;
282 	extern mutex_t ops_lock;
283 
284 /* VARIABLES PROTECTED BY ops_lock: ops */
285 
286 	trace1(TR_svc_raw_ops, 0);
287 	mutex_lock(&ops_lock);
288 	if (ops.xp_recv == NULL) {
289 		ops.xp_recv = svc_raw_recv;
290 		ops.xp_stat = svc_raw_stat;
291 		ops.xp_getargs = svc_raw_getargs;
292 		ops.xp_reply = svc_raw_reply;
293 		ops.xp_freeargs = svc_raw_freeargs;
294 		ops.xp_destroy = svc_raw_destroy;
295 		ops.xp_control = svc_raw_control;
296 	}
297 	mutex_unlock(&ops_lock);
298 	trace1(TR_svc_raw_ops, 1);
299 	return (&ops);
300 }
301