import less(1)
[unleashed/tickless.git] / usr / src / lib / libc / port / nsl / svc_raw.c
blob1fe2ad7c6ab818b3092e8157213ae980db2f5fd9
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
24 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
27 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
30 * Portions of this source code were derived from Berkeley
31 * 4.3 BSD under license from the Regents of the University of
32 * California.
36 * svc_raw.c, This is 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 simulate rpc and get rpc (round trip) overhead, without
39 * any interference from the kernal.
42 #include "mt.h"
43 #include "rpc_mt.h"
44 #include <stdlib.h>
45 #include <rpc/rpc.h>
46 #include <sys/types.h>
47 #include <syslog.h>
49 #ifndef UDPMSGSIZE
50 #define UDPMSGSIZE 8800
51 #endif
54 * This is the "network" that we will be moving data over
56 static struct svc_raw_private {
57 struct netbuf *raw_netbuf;
58 SVCXPRT *server;
59 XDR xdr_stream;
60 char verf_body[MAX_AUTH_BYTES];
61 } *svc_raw_private;
63 static struct xp_ops *svc_raw_ops();
64 extern mutex_t svcraw_lock;
67 * This netbuf is shared with the raw client.
69 struct netbuf _rawcomnetbuf;
71 SVCXPRT *
72 svc_raw_create(void)
74 struct svc_raw_private *srp;
76 /* VARIABLES PROTECTED BY svcraw_lock: svc_raw_private, srp */
77 (void) mutex_lock(&svcraw_lock);
78 srp = svc_raw_private;
79 if (srp != NULL) {
80 (void) mutex_unlock(&svcraw_lock);
81 return (srp->server);
84 srp = calloc(1, sizeof (*srp));
85 if (srp == NULL) {
86 syslog(LOG_ERR, "svc_raw_create: out of memory");
88 (void) mutex_unlock(&svcraw_lock);
89 return (NULL);
92 srp->raw_netbuf = &_rawcomnetbuf;
93 srp->raw_netbuf->buf = malloc(UDPMSGSIZE);
94 if (srp->raw_netbuf->buf == NULL) {
95 free(srp);
96 syslog(LOG_ERR, "svc_raw_create: out of memory");
98 (void) mutex_unlock(&svcraw_lock);
99 return (NULL);
101 srp->raw_netbuf->maxlen = UDPMSGSIZE;
102 srp->raw_netbuf->len = 0;
104 if ((srp->server = svc_xprt_alloc()) == NULL) {
105 free(srp->raw_netbuf->buf);
106 srp->raw_netbuf->buf = NULL;
107 srp->raw_netbuf->maxlen = 0;
109 free(srp);
111 (void) mutex_unlock(&svcraw_lock);
112 return (NULL);
116 * By convention, using FD_SETSIZE as the pseudo file descriptor
118 srp->server->xp_fd = FD_SETSIZE;
119 srp->server->xp_port = 0;
120 srp->server->xp_ops = svc_raw_ops();
121 srp->server->xp_verf.oa_base = srp->verf_body;
122 xprt_register(srp->server);
124 svc_raw_private = srp;
126 (void) mutex_unlock(&svcraw_lock);
127 return (srp->server);
130 /*ARGSUSED*/
131 static enum xprt_stat
132 svc_raw_stat(SVCXPRT *xprt)
134 return (XPRT_IDLE);
137 /*ARGSUSED*/
138 static bool_t
139 svc_raw_recv(SVCXPRT *xprt, struct rpc_msg *msg)
141 struct svc_raw_private *srp;
142 XDR *xdrs;
144 (void) mutex_lock(&svcraw_lock);
145 srp = svc_raw_private;
146 if (srp == NULL) {
147 (void) mutex_unlock(&svcraw_lock);
148 return (FALSE);
150 (void) mutex_unlock(&svcraw_lock);
152 xdrs = &srp->xdr_stream;
154 xdrmem_create(xdrs, srp->raw_netbuf->buf, srp->raw_netbuf->len,
155 XDR_DECODE);
157 if (!xdr_callmsg(xdrs, msg)) {
158 XDR_DESTROY(xdrs);
159 return (FALSE);
162 return (TRUE);
165 /*ARGSUSED*/
166 static bool_t
167 svc_raw_reply(SVCXPRT *xprt, struct rpc_msg *msg)
169 struct svc_raw_private *srp;
170 XDR *xdrs;
171 uint_t start;
173 (void) mutex_lock(&svcraw_lock);
174 srp = svc_raw_private;
175 if (srp == NULL) {
176 (void) mutex_unlock(&svcraw_lock);
177 return (FALSE);
179 (void) mutex_unlock(&svcraw_lock);
181 xdrs = &srp->xdr_stream;
183 XDR_DESTROY(xdrs);
184 xdrmem_create(xdrs, srp->raw_netbuf->buf, srp->raw_netbuf->maxlen,
185 XDR_ENCODE);
187 start = XDR_GETPOS(xdrs);
188 if (!xdr_replymsg(xdrs, msg)) {
189 XDR_DESTROY(xdrs);
190 return (FALSE);
192 srp->raw_netbuf->len = XDR_GETPOS(xdrs) - start;
194 return (TRUE);
197 /*ARGSUSED*/
198 static bool_t
199 svc_raw_getargs(SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr)
201 struct svc_raw_private *srp;
203 (void) mutex_lock(&svcraw_lock);
204 srp = svc_raw_private;
205 if (srp == NULL) {
206 (void) mutex_unlock(&svcraw_lock);
207 return (FALSE);
209 (void) mutex_unlock(&svcraw_lock);
211 return ((*xdr_args)(&srp->xdr_stream, args_ptr));
214 /*ARGSUSED*/
215 static bool_t
216 svc_raw_freeargs(SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr)
218 struct svc_raw_private *srp;
220 (void) mutex_lock(&svcraw_lock);
221 srp = svc_raw_private;
222 if (srp == NULL) {
223 (void) mutex_unlock(&svcraw_lock);
224 return (FALSE);
226 (void) mutex_unlock(&svcraw_lock);
228 XDR_DESTROY(&srp->xdr_stream);
230 xdr_free(xdr_args, args_ptr);
232 return (TRUE);
235 /*ARGSUSED*/
236 static void
237 svc_raw_destroy(SVCXPRT *xprt)
241 /*ARGSUSED*/
242 static bool_t
243 svc_raw_control(SVCXPRT *xprt, const uint_t rq, void *in)
245 switch (rq) {
246 case SVCGET_XID: /* fall through for now */
247 default:
248 return (FALSE);
252 static struct xp_ops *
253 svc_raw_ops(void)
255 static struct xp_ops ops;
256 extern mutex_t ops_lock;
258 /* VARIABLES PROTECTED BY ops_lock: ops */
260 (void) mutex_lock(&ops_lock);
261 if (ops.xp_recv == NULL) {
262 ops.xp_recv = svc_raw_recv;
263 ops.xp_stat = svc_raw_stat;
264 ops.xp_getargs = svc_raw_getargs;
265 ops.xp_reply = svc_raw_reply;
266 ops.xp_freeargs = svc_raw_freeargs;
267 ops.xp_destroy = svc_raw_destroy;
268 ops.xp_control = svc_raw_control;
270 (void) mutex_unlock(&ops_lock);
271 return (&ops);