usched: Implement LWP lazy migration support.
[dragonfly.git] / lib / libc / rpc / svc_raw.c
blob271a168508510bdaf70c17c8cc065aa1f5829a67
1 /*-
2 * Copyright (c) 2009, Sun Microsystems, Inc.
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * - Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * - Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 * - Neither the name of Sun Microsystems, Inc. nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
28 * @(#)svc_raw.c 1.25 89/01/31 Copyr 1984 Sun Micro
29 * @(#)svc_raw.c 1.16 94/04/24 SMI
30 * $NetBSD: svc_raw.c,v 1.14 2000/07/06 03:10:35 christos Exp $
31 * $FreeBSD: src/lib/libc/rpc/svc_raw.c,v 1.15 2006/02/27 22:10:59 deischen Exp $
34 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
38 * svc_raw.c, This a toy for simple testing and timing.
39 * Interface to create an rpc client and server in the same UNIX process.
40 * This lets us similate rpc and get rpc (round trip) overhead, without
41 * any interference from the kernel.
45 #include "namespace.h"
46 #include "reentrant.h"
47 #include <rpc/rpc.h>
48 #include <sys/types.h>
49 #include <rpc/raw.h>
50 #include <stdlib.h>
51 #include "un-namespace.h"
52 #include "mt_misc.h"
54 #ifndef UDPMSGSIZE
55 #define UDPMSGSIZE 8800
56 #endif
59 * This is the "network" that we will be moving data over
61 static struct svc_raw_private {
62 char *raw_buf; /* should be shared with the cl handle */
63 SVCXPRT server;
64 XDR xdr_stream;
65 char verf_body[MAX_AUTH_BYTES];
66 } *svc_raw_private;
68 static enum xprt_stat svc_raw_stat(SVCXPRT *);
69 static bool_t svc_raw_recv(SVCXPRT *, struct rpc_msg *);
70 static bool_t svc_raw_reply(SVCXPRT *, struct rpc_msg *);
71 static bool_t svc_raw_getargs(SVCXPRT *, xdrproc_t, void *);
72 static bool_t svc_raw_freeargs(SVCXPRT *, xdrproc_t, void *);
73 static void svc_raw_destroy(SVCXPRT *);
74 static void svc_raw_ops(SVCXPRT *);
75 static bool_t svc_raw_control(SVCXPRT *, const u_int, void *);
77 char *__rpc_rawcombuf = NULL;
79 SVCXPRT *
80 svc_raw_create(void)
82 struct svc_raw_private *srp;
83 /* VARIABLES PROTECTED BY svcraw_lock: svc_raw_private, srp */
85 mutex_lock(&svcraw_lock);
86 srp = svc_raw_private;
87 if (srp == NULL) {
88 srp = (struct svc_raw_private *)calloc(1, sizeof (*srp));
89 if (srp == NULL) {
90 mutex_unlock(&svcraw_lock);
91 return (NULL);
93 if (__rpc_rawcombuf == NULL)
94 __rpc_rawcombuf = calloc(UDPMSGSIZE, sizeof (char));
95 srp->raw_buf = __rpc_rawcombuf; /* Share it with the client */
96 svc_raw_private = srp;
98 srp->server.xp_fd = FD_SETSIZE;
99 srp->server.xp_port = 0;
100 srp->server.xp_p3 = NULL;
101 svc_raw_ops(&srp->server);
102 srp->server.xp_verf.oa_base = srp->verf_body;
103 xdrmem_create(&srp->xdr_stream, srp->raw_buf, UDPMSGSIZE, XDR_DECODE);
104 xprt_register(&srp->server);
105 mutex_unlock(&svcraw_lock);
106 return (&srp->server);
109 /*ARGSUSED*/
110 static enum xprt_stat
111 svc_raw_stat(SVCXPRT *xprt __unused)
113 return (XPRT_IDLE);
116 /*ARGSUSED*/
117 static bool_t
118 svc_raw_recv(SVCXPRT *xprt __unused, struct rpc_msg *msg)
120 struct svc_raw_private *srp;
121 XDR *xdrs;
123 mutex_lock(&svcraw_lock);
124 srp = svc_raw_private;
125 if (srp == NULL) {
126 mutex_unlock(&svcraw_lock);
127 return (FALSE);
129 mutex_unlock(&svcraw_lock);
131 xdrs = &srp->xdr_stream;
132 xdrs->x_op = XDR_DECODE;
133 XDR_SETPOS(xdrs, 0);
134 if (! xdr_callmsg(xdrs, msg)) {
135 return (FALSE);
137 return (TRUE);
140 /*ARGSUSED*/
141 static bool_t
142 svc_raw_reply(SVCXPRT *xprt __unused, struct rpc_msg *msg)
144 struct svc_raw_private *srp;
145 XDR *xdrs;
147 mutex_lock(&svcraw_lock);
148 srp = svc_raw_private;
149 if (srp == NULL) {
150 mutex_unlock(&svcraw_lock);
151 return (FALSE);
153 mutex_unlock(&svcraw_lock);
155 xdrs = &srp->xdr_stream;
156 xdrs->x_op = XDR_ENCODE;
157 XDR_SETPOS(xdrs, 0);
158 if (! xdr_replymsg(xdrs, msg)) {
159 return (FALSE);
161 XDR_GETPOS(xdrs); /* called just for overhead */
162 return (TRUE);
165 /*ARGSUSED*/
166 static bool_t
167 svc_raw_getargs(SVCXPRT *xprt __unused, xdrproc_t xdr_args, void *args_ptr)
169 struct svc_raw_private *srp;
171 mutex_lock(&svcraw_lock);
172 srp = svc_raw_private;
173 if (srp == NULL) {
174 mutex_unlock(&svcraw_lock);
175 return (FALSE);
177 mutex_unlock(&svcraw_lock);
178 return (*xdr_args)(&srp->xdr_stream, args_ptr);
181 /*ARGSUSED*/
182 static bool_t
183 svc_raw_freeargs(SVCXPRT *xprt __unused, xdrproc_t xdr_args, void *args_ptr)
185 struct svc_raw_private *srp;
186 XDR *xdrs;
188 mutex_lock(&svcraw_lock);
189 srp = svc_raw_private;
190 if (srp == NULL) {
191 mutex_unlock(&svcraw_lock);
192 return (FALSE);
194 mutex_unlock(&svcraw_lock);
196 xdrs = &srp->xdr_stream;
197 xdrs->x_op = XDR_FREE;
198 return (*xdr_args)(xdrs, args_ptr);
201 /*ARGSUSED*/
202 static void
203 svc_raw_destroy(SVCXPRT *xprt __unused)
207 /*ARGSUSED*/
208 static bool_t
209 svc_raw_control(SVCXPRT *xprt __unused, const u_int rq __unused,
210 void *in __unused)
212 return (FALSE);
215 static void
216 svc_raw_ops(SVCXPRT *xprt)
218 static struct xp_ops ops;
219 static struct xp_ops2 ops2;
221 /* VARIABLES PROTECTED BY ops_lock: ops */
223 mutex_lock(&ops_lock);
224 if (ops.xp_recv == NULL) {
225 ops.xp_recv = svc_raw_recv;
226 ops.xp_stat = svc_raw_stat;
227 ops.xp_getargs = svc_raw_getargs;
228 ops.xp_reply = svc_raw_reply;
229 ops.xp_freeargs = svc_raw_freeargs;
230 ops.xp_destroy = svc_raw_destroy;
231 ops2.xp_control = svc_raw_control;
233 xprt->xp_ops = &ops;
234 xprt->xp_ops2 = &ops2;
235 mutex_unlock(&ops_lock);