summaryrefslogtreecommitdiff
path: root/usr/src/lib/libnsl/rpc/svc_raw.c
blob: 1fe2ad7c6ab818b3092e8157213ae980db2f5fd9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
 * Portions of this source code were derived from Berkeley
 * 4.3 BSD under license from the Regents of the University of
 * California.
 */

/*
 * svc_raw.c,   This is a toy for simple testing and timing.
 * Interface to create an rpc client and server in the same UNIX process.
 * This lets us simulate rpc and get rpc (round trip) overhead, without
 * any interference from the kernal.
 */

#include "mt.h"
#include "rpc_mt.h"
#include <stdlib.h>
#include <rpc/rpc.h>
#include <sys/types.h>
#include <syslog.h>

#ifndef UDPMSGSIZE
#define	UDPMSGSIZE 8800
#endif

/*
 * This is the "network" that we will be moving data over
 */
static struct svc_raw_private {
	struct netbuf	*raw_netbuf;
	SVCXPRT	*server;
	XDR	xdr_stream;
	char	verf_body[MAX_AUTH_BYTES];
} *svc_raw_private;

static struct xp_ops *svc_raw_ops();
extern mutex_t	svcraw_lock;

/*
 * This netbuf is shared with the raw client.
 */
struct netbuf _rawcomnetbuf;

SVCXPRT *
svc_raw_create(void)
{
	struct svc_raw_private *srp;

/* VARIABLES PROTECTED BY svcraw_lock: svc_raw_private, srp */
	(void) mutex_lock(&svcraw_lock);
	srp = svc_raw_private;
	if (srp != NULL) {
		(void) mutex_unlock(&svcraw_lock);
		return (srp->server);
	}

	srp = calloc(1, sizeof (*srp));
	if (srp == NULL) {
		syslog(LOG_ERR, "svc_raw_create: out of memory");

		(void) mutex_unlock(&svcraw_lock);
		return (NULL);
	}

	srp->raw_netbuf = &_rawcomnetbuf;
	srp->raw_netbuf->buf = malloc(UDPMSGSIZE);
	if (srp->raw_netbuf->buf == NULL) {
		free(srp);
		syslog(LOG_ERR, "svc_raw_create: out of memory");

		(void) mutex_unlock(&svcraw_lock);
		return (NULL);
	}
	srp->raw_netbuf->maxlen = UDPMSGSIZE;
	srp->raw_netbuf->len = 0;

	if ((srp->server = svc_xprt_alloc()) == NULL) {
		free(srp->raw_netbuf->buf);
		srp->raw_netbuf->buf = NULL;
		srp->raw_netbuf->maxlen = 0;

		free(srp);

		(void) mutex_unlock(&svcraw_lock);
		return (NULL);
	}

	/*
	 * By convention, using FD_SETSIZE as the pseudo file descriptor
	 */
	srp->server->xp_fd = FD_SETSIZE;
	srp->server->xp_port = 0;
	srp->server->xp_ops = svc_raw_ops();
	srp->server->xp_verf.oa_base = srp->verf_body;
	xprt_register(srp->server);

	svc_raw_private = srp;

	(void) mutex_unlock(&svcraw_lock);
	return (srp->server);
}

/*ARGSUSED*/
static enum xprt_stat
svc_raw_stat(SVCXPRT *xprt)
{
	return (XPRT_IDLE);
}

/*ARGSUSED*/
static bool_t
svc_raw_recv(SVCXPRT *xprt, struct rpc_msg *msg)
{
	struct svc_raw_private *srp;
	XDR *xdrs;

	(void) mutex_lock(&svcraw_lock);
	srp = svc_raw_private;
	if (srp == NULL) {
		(void) mutex_unlock(&svcraw_lock);
		return (FALSE);
	}
	(void) mutex_unlock(&svcraw_lock);

	xdrs = &srp->xdr_stream;

	xdrmem_create(xdrs, srp->raw_netbuf->buf, srp->raw_netbuf->len,
	    XDR_DECODE);

	if (!xdr_callmsg(xdrs, msg)) {
		XDR_DESTROY(xdrs);
		return (FALSE);
	}

	return (TRUE);
}

/*ARGSUSED*/
static bool_t
svc_raw_reply(SVCXPRT *xprt, struct rpc_msg *msg)
{
	struct svc_raw_private *srp;
	XDR *xdrs;
	uint_t start;

	(void) mutex_lock(&svcraw_lock);
	srp = svc_raw_private;
	if (srp == NULL) {
		(void) mutex_unlock(&svcraw_lock);
		return (FALSE);
	}
	(void) mutex_unlock(&svcraw_lock);

	xdrs = &srp->xdr_stream;

	XDR_DESTROY(xdrs);
	xdrmem_create(xdrs, srp->raw_netbuf->buf, srp->raw_netbuf->maxlen,
	    XDR_ENCODE);

	start = XDR_GETPOS(xdrs);
	if (!xdr_replymsg(xdrs, msg)) {
		XDR_DESTROY(xdrs);
		return (FALSE);
	}
	srp->raw_netbuf->len = XDR_GETPOS(xdrs) - start;

	return (TRUE);
}

/*ARGSUSED*/
static bool_t
svc_raw_getargs(SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr)
{
	struct svc_raw_private *srp;

	(void) mutex_lock(&svcraw_lock);
	srp = svc_raw_private;
	if (srp == NULL) {
		(void) mutex_unlock(&svcraw_lock);
		return (FALSE);
	}
	(void) mutex_unlock(&svcraw_lock);

	return ((*xdr_args)(&srp->xdr_stream, args_ptr));
}

/*ARGSUSED*/
static bool_t
svc_raw_freeargs(SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr)
{
	struct svc_raw_private *srp;

	(void) mutex_lock(&svcraw_lock);
	srp = svc_raw_private;
	if (srp == NULL) {
		(void) mutex_unlock(&svcraw_lock);
		return (FALSE);
	}
	(void) mutex_unlock(&svcraw_lock);

	XDR_DESTROY(&srp->xdr_stream);

	xdr_free(xdr_args, args_ptr);

	return (TRUE);
}

/*ARGSUSED*/
static void
svc_raw_destroy(SVCXPRT *xprt)
{
}

/*ARGSUSED*/
static bool_t
svc_raw_control(SVCXPRT *xprt, const uint_t rq, void *in)
{
	switch (rq) {
	case SVCGET_XID: /* fall through for now */
	default:
		return (FALSE);
	}
}

static struct xp_ops *
svc_raw_ops(void)
{
	static struct xp_ops ops;
	extern mutex_t ops_lock;

/* VARIABLES PROTECTED BY ops_lock: ops */

	(void) mutex_lock(&ops_lock);
	if (ops.xp_recv == NULL) {
		ops.xp_recv = svc_raw_recv;
		ops.xp_stat = svc_raw_stat;
		ops.xp_getargs = svc_raw_getargs;
		ops.xp_reply = svc_raw_reply;
		ops.xp_freeargs = svc_raw_freeargs;
		ops.xp_destroy = svc_raw_destroy;
		ops.xp_control = svc_raw_control;
	}
	(void) mutex_unlock(&ops_lock);
	return (&ops);
}