svc_raw.c revision 1.21.22.1 1 /* $NetBSD: svc_raw.c,v 1.21.22.1 2013/03/14 22:03:08 riz Exp $ */
2
3 /*
4 * Copyright (c) 2010, Oracle America, Inc.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
16 * * Neither the name of the "Oracle America, 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
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33 /*
34 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
35 */
36
37 /* #ident "@(#)svc_raw.c 1.16 94/04/24 SMI" */
38
39 #include <sys/cdefs.h>
40 #if defined(LIBC_SCCS) && !defined(lint)
41 #if 0
42 static char sccsid[] = "@(#)svc_raw.c 1.25 89/01/31 Copyr 1984 Sun Micro";
43 #else
44 __RCSID("$NetBSD: svc_raw.c,v 1.21.22.1 2013/03/14 22:03:08 riz Exp $");
45 #endif
46 #endif
47
48 /*
49 * svc_raw.c, This a toy for simple testing and timing.
50 * Interface to create an rpc client and server in the same UNIX process.
51 * This lets us similate rpc and get rpc (round trip) overhead, without
52 * any interference from the kernel.
53 *
54 */
55
56 #include "namespace.h"
57 #include "reentrant.h"
58 #include <rpc/rpc.h>
59 #include <sys/types.h>
60 #include <rpc/raw.h>
61 #include <assert.h>
62 #include <stdlib.h>
63
64 #ifdef __weak_alias
65 __weak_alias(svc_raw_create,_svc_raw_create)
66 #endif
67
68 #ifndef UDPMSGSIZE
69 #define UDPMSGSIZE 8800
70 #endif
71
72 /*
73 * This is the "network" that we will be moving data over
74 */
75 static struct svc_raw_private {
76 char *raw_buf; /* should be shared with the cl handle */
77 SVCXPRT server;
78 XDR xdr_stream;
79 char verf_body[MAX_AUTH_BYTES];
80 } *svc_raw_private;
81
82 #ifdef _REENTRANT
83 extern mutex_t svcraw_lock;
84 #endif
85
86 static enum xprt_stat svc_raw_stat __P((SVCXPRT *));
87 static bool_t svc_raw_recv __P((SVCXPRT *, struct rpc_msg *));
88 static bool_t svc_raw_reply __P((SVCXPRT *, struct rpc_msg *));
89 static bool_t svc_raw_getargs __P((SVCXPRT *, xdrproc_t, caddr_t));
90 static bool_t svc_raw_freeargs __P((SVCXPRT *, xdrproc_t, caddr_t));
91 static void svc_raw_destroy __P((SVCXPRT *));
92 static void svc_raw_ops __P((SVCXPRT *));
93 static bool_t svc_raw_control __P((SVCXPRT *, const u_int, void *));
94
95 char *__rpc_rawcombuf = NULL;
96
97 SVCXPRT *
98 svc_raw_create()
99 {
100 struct svc_raw_private *srp;
101 /* VARIABLES PROTECTED BY svcraw_lock: svc_raw_private, srp */
102
103 mutex_lock(&svcraw_lock);
104 srp = svc_raw_private;
105 if (srp == NULL) {
106 srp = calloc(1, sizeof(*srp));
107 if (srp == NULL)
108 goto out;
109 if (__rpc_rawcombuf == NULL)
110 __rpc_rawcombuf = malloc(UDPMSGSIZE);
111 if (__rpc_rawcombuf == NULL)
112 goto out;
113 srp->raw_buf = __rpc_rawcombuf; /* Share it with the client */
114 svc_raw_private = srp;
115 }
116 srp->server.xp_fd = FD_SETSIZE;
117 srp->server.xp_port = 0;
118 srp->server.xp_p3 = NULL;
119 svc_raw_ops(&srp->server);
120 srp->server.xp_verf.oa_base = srp->verf_body;
121 xdrmem_create(&srp->xdr_stream, srp->raw_buf, UDPMSGSIZE, XDR_DECODE);
122 xprt_register(&srp->server);
123 mutex_unlock(&svcraw_lock);
124 return (&srp->server);
125 out:
126 if (srp != NULL)
127 free(srp);
128 mutex_unlock(&svcraw_lock);
129 return (NULL);
130 }
131
132 /*ARGSUSED*/
133 static enum xprt_stat
134 svc_raw_stat(xprt)
135 SVCXPRT *xprt; /* args needed to satisfy ANSI-C typechecking */
136 {
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 mutex_lock(&svcraw_lock);
150 srp = svc_raw_private;
151 if (srp == NULL) {
152 mutex_unlock(&svcraw_lock);
153 return (FALSE);
154 }
155 mutex_unlock(&svcraw_lock);
156
157 xdrs = &srp->xdr_stream;
158 xdrs->x_op = XDR_DECODE;
159 (void) XDR_SETPOS(xdrs, 0);
160 if (! xdr_callmsg(xdrs, msg)) {
161 return (FALSE);
162 }
163 return (TRUE);
164 }
165
166 /*ARGSUSED*/
167 static bool_t
168 svc_raw_reply(xprt, msg)
169 SVCXPRT *xprt;
170 struct rpc_msg *msg;
171 {
172 struct svc_raw_private *srp;
173 XDR *xdrs;
174
175 mutex_lock(&svcraw_lock);
176 srp = svc_raw_private;
177 if (srp == NULL) {
178 mutex_unlock(&svcraw_lock);
179 return (FALSE);
180 }
181 mutex_unlock(&svcraw_lock);
182
183 xdrs = &srp->xdr_stream;
184 xdrs->x_op = XDR_ENCODE;
185 (void) XDR_SETPOS(xdrs, 0);
186 if (! xdr_replymsg(xdrs, msg)) {
187 return (FALSE);
188 }
189 (void) XDR_GETPOS(xdrs); /* called just for overhead */
190 return (TRUE);
191 }
192
193 /*ARGSUSED*/
194 static bool_t
195 svc_raw_getargs(xprt, xdr_args, args_ptr)
196 SVCXPRT *xprt;
197 xdrproc_t xdr_args;
198 caddr_t args_ptr;
199 {
200 struct svc_raw_private *srp;
201
202 mutex_lock(&svcraw_lock);
203 srp = svc_raw_private;
204 if (srp == NULL) {
205 mutex_unlock(&svcraw_lock);
206 return (FALSE);
207 }
208 mutex_unlock(&svcraw_lock);
209 return (*xdr_args)(&srp->xdr_stream, args_ptr);
210 }
211
212 /*ARGSUSED*/
213 static bool_t
214 svc_raw_freeargs(xprt, xdr_args, args_ptr)
215 SVCXPRT *xprt;
216 xdrproc_t xdr_args;
217 caddr_t args_ptr;
218 {
219 struct svc_raw_private *srp;
220 XDR *xdrs;
221
222 mutex_lock(&svcraw_lock);
223 srp = svc_raw_private;
224 if (srp == NULL) {
225 mutex_unlock(&svcraw_lock);
226 return (FALSE);
227 }
228 mutex_unlock(&svcraw_lock);
229
230 xdrs = &srp->xdr_stream;
231 xdrs->x_op = XDR_FREE;
232 return (*xdr_args)(xdrs, args_ptr);
233 }
234
235 /*ARGSUSED*/
236 static void
237 svc_raw_destroy(xprt)
238 SVCXPRT *xprt;
239 {
240 }
241
242 /*ARGSUSED*/
243 static bool_t
244 svc_raw_control(xprt, rq, in)
245 SVCXPRT *xprt;
246 const u_int rq;
247 void *in;
248 {
249 return (FALSE);
250 }
251
252 static void
253 svc_raw_ops(xprt)
254 SVCXPRT *xprt;
255 {
256 static struct xp_ops ops;
257 static struct xp_ops2 ops2;
258 #ifdef _REENTRANT
259 extern mutex_t ops_lock;
260 #endif
261
262 _DIAGASSERT(xprt != NULL);
263
264 /* VARIABLES PROTECTED BY ops_lock: ops */
265
266 mutex_lock(&ops_lock);
267 if (ops.xp_recv == NULL) {
268 ops.xp_recv = svc_raw_recv;
269 ops.xp_stat = svc_raw_stat;
270 ops.xp_getargs = svc_raw_getargs;
271 ops.xp_reply = svc_raw_reply;
272 ops.xp_freeargs = svc_raw_freeargs;
273 ops.xp_destroy = svc_raw_destroy;
274 ops2.xp_control = svc_raw_control;
275 }
276 xprt->xp_ops = &ops;
277 xprt->xp_ops2 = &ops2;
278 mutex_unlock(&ops_lock);
279 }
280