xref: /titanic_52/usr/src/lib/libnsl/rpc/svc_raw.c (revision bdfc6d18da790deeec2e0eb09c625902defe2498)
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 
23 /*
24  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
29 /*
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.
33  */
34 
35 #pragma ident	"%Z%%M%	%I%	%E% SMI"
36 
37 /*
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 kernal.
42  *
43  */
44 
45 #include "mt.h"
46 #include "rpc_mt.h"
47 #include <stdlib.h>
48 #include <rpc/rpc.h>
49 #include <sys/types.h>
50 #include <rpc/raw.h>
51 #include <syslog.h>
52 
53 #ifndef UDPMSGSIZE
54 #define	UDPMSGSIZE 8800
55 #endif
56 
57 /*
58  * This is the "network" that we will be moving data over
59  */
60 static struct svc_raw_private {
61 	char	*raw_buf;	/* should be shared with the cl handle */
62 	SVCXPRT	*server;
63 	XDR	xdr_stream;
64 	char	verf_body[MAX_AUTH_BYTES];
65 } *svc_raw_private;
66 
67 static struct xp_ops *svc_raw_ops();
68 extern mutex_t	svcraw_lock;
69 
70 
71 
72 SVCXPRT *
73 svc_raw_create(void)
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 	(void) mutex_lock(&svcraw_lock);
80 	srp = svc_raw_private;
81 	if (srp == NULL) {
82 		srp = calloc(1, sizeof (*srp));
83 		if (srp == NULL) {
84 			syslog(LOG_ERR, "svc_raw_create: out of memory");
85 			(void) mutex_unlock(&svcraw_lock);
86 			return (NULL);
87 		}
88 		flag1 = TRUE;
89 		if (_rawcombuf == NULL) {
90 			_rawcombuf = calloc(UDPMSGSIZE, sizeof (char));
91 			if (_rawcombuf == NULL) {
92 				free(srp);
93 				syslog(LOG_ERR, "svc_raw_create: "
94 					"out of memory");
95 				(void) mutex_unlock(&svcraw_lock);
96 				return (NULL);
97 			}
98 			flag2 = TRUE;
99 		}
100 		srp->raw_buf = _rawcombuf; /* Share it with the client */
101 		svc_raw_private = srp;
102 	}
103 	if ((srp->server = svc_xprt_alloc()) == NULL) {
104 		if (flag2)
105 			free(svc_raw_private->raw_buf);
106 		if (flag1)
107 			free(svc_raw_private);
108 		(void) mutex_unlock(&svcraw_lock);
109 		return (NULL);
110 	}
111 	/*
112 	 * By convention, using FD_SETSIZE as the psuedo file descriptor
113 	 */
114 	srp->server->xp_fd = FD_SETSIZE;
115 	srp->server->xp_port = 0;
116 	srp->server->xp_ops = svc_raw_ops();
117 	srp->server->xp_verf.oa_base = srp->verf_body;
118 	xdrmem_create(&srp->xdr_stream, srp->raw_buf, UDPMSGSIZE, XDR_DECODE);
119 	xprt_register(srp->server);
120 	(void) mutex_unlock(&svcraw_lock);
121 	return (srp->server);
122 }
123 
124 /*ARGSUSED*/
125 static enum xprt_stat
126 svc_raw_stat(SVCXPRT *xprt)
127 {
128 	return (XPRT_IDLE);
129 }
130 
131 /*ARGSUSED*/
132 static bool_t
133 svc_raw_recv(SVCXPRT *xprt, struct rpc_msg *msg)
134 {
135 	struct svc_raw_private *srp;
136 	XDR *xdrs;
137 
138 	(void) mutex_lock(&svcraw_lock);
139 	srp = svc_raw_private;
140 	if (srp == NULL) {
141 		(void) mutex_unlock(&svcraw_lock);
142 		return (FALSE);
143 	}
144 	(void) mutex_unlock(&svcraw_lock);
145 
146 	xdrs = &srp->xdr_stream;
147 	xdrs->x_op = XDR_DECODE;
148 	(void) XDR_SETPOS(xdrs, 0);
149 	return (xdr_callmsg(xdrs, msg));
150 }
151 
152 /*ARGSUSED*/
153 static bool_t
154 svc_raw_reply(SVCXPRT *xprt, struct rpc_msg *msg)
155 {
156 	struct svc_raw_private *srp;
157 	XDR *xdrs;
158 
159 	(void) mutex_lock(&svcraw_lock);
160 	srp = svc_raw_private;
161 	if (srp == NULL) {
162 		(void) mutex_unlock(&svcraw_lock);
163 		return (FALSE);
164 	}
165 	(void) mutex_unlock(&svcraw_lock);
166 
167 	xdrs = &srp->xdr_stream;
168 	xdrs->x_op = XDR_ENCODE;
169 	(void) XDR_SETPOS(xdrs, 0);
170 	return (xdr_replymsg(xdrs, msg));
171 }
172 
173 /*ARGSUSED*/
174 static bool_t
175 svc_raw_getargs(SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr)
176 {
177 	struct svc_raw_private *srp;
178 
179 	(void) mutex_lock(&svcraw_lock);
180 	srp = svc_raw_private;
181 	if (srp == NULL) {
182 		(void) mutex_unlock(&svcraw_lock);
183 		return (FALSE);
184 	}
185 	(void) mutex_unlock(&svcraw_lock);
186 	return ((*xdr_args)(&srp->xdr_stream, args_ptr));
187 }
188 
189 /*ARGSUSED*/
190 static bool_t
191 svc_raw_freeargs(SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr)
192 {
193 	struct svc_raw_private *srp;
194 	XDR *xdrs;
195 
196 	(void) mutex_lock(&svcraw_lock);
197 	srp = svc_raw_private;
198 	if (srp == NULL) {
199 		(void) mutex_unlock(&svcraw_lock);
200 		return (FALSE);
201 	}
202 	(void) mutex_unlock(&svcraw_lock);
203 
204 	xdrs = &srp->xdr_stream;
205 	xdrs->x_op = XDR_FREE;
206 	return ((*xdr_args)(xdrs, args_ptr));
207 }
208 
209 /*ARGSUSED*/
210 static void
211 svc_raw_destroy(SVCXPRT *xprt)
212 {
213 }
214 
215 /*ARGSUSED*/
216 static bool_t
217 svc_raw_control(SVCXPRT *xprt, const uint_t rq, void *in)
218 {
219 	switch (rq) {
220 	case SVCGET_XID: /* fall through for now */
221 	default:
222 		return (FALSE);
223 	}
224 }
225 
226 static struct xp_ops *
227 svc_raw_ops(void)
228 {
229 	static struct xp_ops ops;
230 	extern mutex_t ops_lock;
231 
232 /* VARIABLES PROTECTED BY ops_lock: ops */
233 
234 	(void) mutex_lock(&ops_lock);
235 	if (ops.xp_recv == NULL) {
236 		ops.xp_recv = svc_raw_recv;
237 		ops.xp_stat = svc_raw_stat;
238 		ops.xp_getargs = svc_raw_getargs;
239 		ops.xp_reply = svc_raw_reply;
240 		ops.xp_freeargs = svc_raw_freeargs;
241 		ops.xp_destroy = svc_raw_destroy;
242 		ops.xp_control = svc_raw_control;
243 	}
244 	(void) mutex_unlock(&ops_lock);
245 	return (&ops);
246 }
247