xref: /illumos-gate/usr/src/lib/libnsl/rpc/svc_raw.c (revision 2a6fb28d)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
2161961e0fSrobinson  */
2261961e0fSrobinson 
2361961e0fSrobinson /*
2461961e0fSrobinson  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
287c478bd9Sstevel@tonic-gate /* All Rights Reserved */
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley
317c478bd9Sstevel@tonic-gate  * 4.3 BSD under license from the Regents of the University of
327c478bd9Sstevel@tonic-gate  * California.
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate /*
36*2a6fb28dSMarcel Telka  * svc_raw.c,   This is a toy for simple testing and timing.
377c478bd9Sstevel@tonic-gate  * Interface to create an rpc client and server in the same UNIX process.
38*2a6fb28dSMarcel Telka  * This lets us simulate rpc and get rpc (round trip) overhead, without
397c478bd9Sstevel@tonic-gate  * any interference from the kernal.
407c478bd9Sstevel@tonic-gate  */
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include "mt.h"
437c478bd9Sstevel@tonic-gate #include "rpc_mt.h"
4461961e0fSrobinson #include <stdlib.h>
457c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
467c478bd9Sstevel@tonic-gate #include <sys/types.h>
477c478bd9Sstevel@tonic-gate #include <syslog.h>
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate #ifndef UDPMSGSIZE
507c478bd9Sstevel@tonic-gate #define	UDPMSGSIZE 8800
517c478bd9Sstevel@tonic-gate #endif
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate /*
547c478bd9Sstevel@tonic-gate  * This is the "network" that we will be moving data over
557c478bd9Sstevel@tonic-gate  */
567c478bd9Sstevel@tonic-gate static struct svc_raw_private {
57*2a6fb28dSMarcel Telka 	struct netbuf	*raw_netbuf;
587c478bd9Sstevel@tonic-gate 	SVCXPRT	*server;
597c478bd9Sstevel@tonic-gate 	XDR	xdr_stream;
607c478bd9Sstevel@tonic-gate 	char	verf_body[MAX_AUTH_BYTES];
617c478bd9Sstevel@tonic-gate } *svc_raw_private;
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate static struct xp_ops *svc_raw_ops();
647c478bd9Sstevel@tonic-gate extern mutex_t	svcraw_lock;
657c478bd9Sstevel@tonic-gate 
66*2a6fb28dSMarcel Telka /*
67*2a6fb28dSMarcel Telka  * This netbuf is shared with the raw client.
68*2a6fb28dSMarcel Telka  */
69*2a6fb28dSMarcel Telka struct netbuf _rawcomnetbuf;
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate SVCXPRT *
svc_raw_create(void)7261961e0fSrobinson svc_raw_create(void)
737c478bd9Sstevel@tonic-gate {
747c478bd9Sstevel@tonic-gate 	struct svc_raw_private *srp;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate /* VARIABLES PROTECTED BY svcraw_lock: svc_raw_private, srp */
7761961e0fSrobinson 	(void) mutex_lock(&svcraw_lock);
787c478bd9Sstevel@tonic-gate 	srp = svc_raw_private;
79*2a6fb28dSMarcel Telka 	if (srp != NULL) {
80*2a6fb28dSMarcel Telka 		(void) mutex_unlock(&svcraw_lock);
81*2a6fb28dSMarcel Telka 		return (srp->server);
82*2a6fb28dSMarcel Telka 	}
83*2a6fb28dSMarcel Telka 
84*2a6fb28dSMarcel Telka 	srp = calloc(1, sizeof (*srp));
857c478bd9Sstevel@tonic-gate 	if (srp == NULL) {
86*2a6fb28dSMarcel Telka 		syslog(LOG_ERR, "svc_raw_create: out of memory");
87*2a6fb28dSMarcel Telka 
88*2a6fb28dSMarcel Telka 		(void) mutex_unlock(&svcraw_lock);
89*2a6fb28dSMarcel Telka 		return (NULL);
907c478bd9Sstevel@tonic-gate 	}
91*2a6fb28dSMarcel Telka 
92*2a6fb28dSMarcel Telka 	srp->raw_netbuf = &_rawcomnetbuf;
93*2a6fb28dSMarcel Telka 	srp->raw_netbuf->buf = malloc(UDPMSGSIZE);
94*2a6fb28dSMarcel Telka 	if (srp->raw_netbuf->buf == NULL) {
95*2a6fb28dSMarcel Telka 		free(srp);
96*2a6fb28dSMarcel Telka 		syslog(LOG_ERR, "svc_raw_create: out of memory");
97*2a6fb28dSMarcel Telka 
98*2a6fb28dSMarcel Telka 		(void) mutex_unlock(&svcraw_lock);
99*2a6fb28dSMarcel Telka 		return (NULL);
100*2a6fb28dSMarcel Telka 	}
101*2a6fb28dSMarcel Telka 	srp->raw_netbuf->maxlen = UDPMSGSIZE;
102*2a6fb28dSMarcel Telka 	srp->raw_netbuf->len = 0;
103*2a6fb28dSMarcel Telka 
1047c478bd9Sstevel@tonic-gate 	if ((srp->server = svc_xprt_alloc()) == NULL) {
105*2a6fb28dSMarcel Telka 		free(srp->raw_netbuf->buf);
106*2a6fb28dSMarcel Telka 		srp->raw_netbuf->buf = NULL;
107*2a6fb28dSMarcel Telka 		srp->raw_netbuf->maxlen = 0;
108*2a6fb28dSMarcel Telka 
109*2a6fb28dSMarcel Telka 		free(srp);
110*2a6fb28dSMarcel Telka 
11161961e0fSrobinson 		(void) mutex_unlock(&svcraw_lock);
11261961e0fSrobinson 		return (NULL);
1137c478bd9Sstevel@tonic-gate 	}
114*2a6fb28dSMarcel Telka 
1157c478bd9Sstevel@tonic-gate 	/*
116*2a6fb28dSMarcel Telka 	 * By convention, using FD_SETSIZE as the pseudo file descriptor
1177c478bd9Sstevel@tonic-gate 	 */
1187c478bd9Sstevel@tonic-gate 	srp->server->xp_fd = FD_SETSIZE;
1197c478bd9Sstevel@tonic-gate 	srp->server->xp_port = 0;
1207c478bd9Sstevel@tonic-gate 	srp->server->xp_ops = svc_raw_ops();
1217c478bd9Sstevel@tonic-gate 	srp->server->xp_verf.oa_base = srp->verf_body;
1227c478bd9Sstevel@tonic-gate 	xprt_register(srp->server);
123*2a6fb28dSMarcel Telka 
124*2a6fb28dSMarcel Telka 	svc_raw_private = srp;
125*2a6fb28dSMarcel Telka 
12661961e0fSrobinson 	(void) mutex_unlock(&svcraw_lock);
1277c478bd9Sstevel@tonic-gate 	return (srp->server);
1287c478bd9Sstevel@tonic-gate }
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1317c478bd9Sstevel@tonic-gate static enum xprt_stat
svc_raw_stat(SVCXPRT * xprt)13261961e0fSrobinson svc_raw_stat(SVCXPRT *xprt)
1337c478bd9Sstevel@tonic-gate {
1347c478bd9Sstevel@tonic-gate 	return (XPRT_IDLE);
1357c478bd9Sstevel@tonic-gate }
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1387c478bd9Sstevel@tonic-gate static bool_t
svc_raw_recv(SVCXPRT * xprt,struct rpc_msg * msg)13961961e0fSrobinson svc_raw_recv(SVCXPRT *xprt, struct rpc_msg *msg)
1407c478bd9Sstevel@tonic-gate {
1417c478bd9Sstevel@tonic-gate 	struct svc_raw_private *srp;
1427c478bd9Sstevel@tonic-gate 	XDR *xdrs;
1437c478bd9Sstevel@tonic-gate 
14461961e0fSrobinson 	(void) mutex_lock(&svcraw_lock);
1457c478bd9Sstevel@tonic-gate 	srp = svc_raw_private;
1467c478bd9Sstevel@tonic-gate 	if (srp == NULL) {
14761961e0fSrobinson 		(void) mutex_unlock(&svcraw_lock);
1487c478bd9Sstevel@tonic-gate 		return (FALSE);
1497c478bd9Sstevel@tonic-gate 	}
15061961e0fSrobinson 	(void) mutex_unlock(&svcraw_lock);
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	xdrs = &srp->xdr_stream;
153*2a6fb28dSMarcel Telka 
154*2a6fb28dSMarcel Telka 	xdrmem_create(xdrs, srp->raw_netbuf->buf, srp->raw_netbuf->len,
155*2a6fb28dSMarcel Telka 	    XDR_DECODE);
156*2a6fb28dSMarcel Telka 
157*2a6fb28dSMarcel Telka 	if (!xdr_callmsg(xdrs, msg)) {
158*2a6fb28dSMarcel Telka 		XDR_DESTROY(xdrs);
159*2a6fb28dSMarcel Telka 		return (FALSE);
160*2a6fb28dSMarcel Telka 	}
161*2a6fb28dSMarcel Telka 
162*2a6fb28dSMarcel Telka 	return (TRUE);
1637c478bd9Sstevel@tonic-gate }
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1667c478bd9Sstevel@tonic-gate static bool_t
svc_raw_reply(SVCXPRT * xprt,struct rpc_msg * msg)16761961e0fSrobinson svc_raw_reply(SVCXPRT *xprt, struct rpc_msg *msg)
1687c478bd9Sstevel@tonic-gate {
1697c478bd9Sstevel@tonic-gate 	struct svc_raw_private *srp;
1707c478bd9Sstevel@tonic-gate 	XDR *xdrs;
171*2a6fb28dSMarcel Telka 	uint_t start;
1727c478bd9Sstevel@tonic-gate 
17361961e0fSrobinson 	(void) mutex_lock(&svcraw_lock);
1747c478bd9Sstevel@tonic-gate 	srp = svc_raw_private;
1757c478bd9Sstevel@tonic-gate 	if (srp == NULL) {
17661961e0fSrobinson 		(void) mutex_unlock(&svcraw_lock);
1777c478bd9Sstevel@tonic-gate 		return (FALSE);
1787c478bd9Sstevel@tonic-gate 	}
17961961e0fSrobinson 	(void) mutex_unlock(&svcraw_lock);
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	xdrs = &srp->xdr_stream;
182*2a6fb28dSMarcel Telka 
183*2a6fb28dSMarcel Telka 	XDR_DESTROY(xdrs);
184*2a6fb28dSMarcel Telka 	xdrmem_create(xdrs, srp->raw_netbuf->buf, srp->raw_netbuf->maxlen,
185*2a6fb28dSMarcel Telka 	    XDR_ENCODE);
186*2a6fb28dSMarcel Telka 
187*2a6fb28dSMarcel Telka 	start = XDR_GETPOS(xdrs);
188*2a6fb28dSMarcel Telka 	if (!xdr_replymsg(xdrs, msg)) {
189*2a6fb28dSMarcel Telka 		XDR_DESTROY(xdrs);
190*2a6fb28dSMarcel Telka 		return (FALSE);
191*2a6fb28dSMarcel Telka 	}
192*2a6fb28dSMarcel Telka 	srp->raw_netbuf->len = XDR_GETPOS(xdrs) - start;
193*2a6fb28dSMarcel Telka 
194*2a6fb28dSMarcel Telka 	return (TRUE);
1957c478bd9Sstevel@tonic-gate }
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1987c478bd9Sstevel@tonic-gate static bool_t
svc_raw_getargs(SVCXPRT * xprt,xdrproc_t xdr_args,caddr_t args_ptr)19961961e0fSrobinson svc_raw_getargs(SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr)
2007c478bd9Sstevel@tonic-gate {
2017c478bd9Sstevel@tonic-gate 	struct svc_raw_private *srp;
2027c478bd9Sstevel@tonic-gate 
20361961e0fSrobinson 	(void) mutex_lock(&svcraw_lock);
2047c478bd9Sstevel@tonic-gate 	srp = svc_raw_private;
2057c478bd9Sstevel@tonic-gate 	if (srp == NULL) {
20661961e0fSrobinson 		(void) mutex_unlock(&svcraw_lock);
2077c478bd9Sstevel@tonic-gate 		return (FALSE);
2087c478bd9Sstevel@tonic-gate 	}
20961961e0fSrobinson 	(void) mutex_unlock(&svcraw_lock);
210*2a6fb28dSMarcel Telka 
21161961e0fSrobinson 	return ((*xdr_args)(&srp->xdr_stream, args_ptr));
2127c478bd9Sstevel@tonic-gate }
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2157c478bd9Sstevel@tonic-gate static bool_t
svc_raw_freeargs(SVCXPRT * xprt,xdrproc_t xdr_args,caddr_t args_ptr)21661961e0fSrobinson svc_raw_freeargs(SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr)
2177c478bd9Sstevel@tonic-gate {
2187c478bd9Sstevel@tonic-gate 	struct svc_raw_private *srp;
2197c478bd9Sstevel@tonic-gate 
22061961e0fSrobinson 	(void) mutex_lock(&svcraw_lock);
2217c478bd9Sstevel@tonic-gate 	srp = svc_raw_private;
2227c478bd9Sstevel@tonic-gate 	if (srp == NULL) {
22361961e0fSrobinson 		(void) mutex_unlock(&svcraw_lock);
2247c478bd9Sstevel@tonic-gate 		return (FALSE);
2257c478bd9Sstevel@tonic-gate 	}
22661961e0fSrobinson 	(void) mutex_unlock(&svcraw_lock);
2277c478bd9Sstevel@tonic-gate 
228*2a6fb28dSMarcel Telka 	XDR_DESTROY(&srp->xdr_stream);
229*2a6fb28dSMarcel Telka 
230*2a6fb28dSMarcel Telka 	xdr_free(xdr_args, args_ptr);
231*2a6fb28dSMarcel Telka 
232*2a6fb28dSMarcel Telka 	return (TRUE);
2337c478bd9Sstevel@tonic-gate }
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2367c478bd9Sstevel@tonic-gate static void
svc_raw_destroy(SVCXPRT * xprt)23761961e0fSrobinson svc_raw_destroy(SVCXPRT *xprt)
2387c478bd9Sstevel@tonic-gate {
2397c478bd9Sstevel@tonic-gate }
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2427c478bd9Sstevel@tonic-gate static bool_t
svc_raw_control(SVCXPRT * xprt,const uint_t rq,void * in)24361961e0fSrobinson svc_raw_control(SVCXPRT *xprt, const uint_t rq, void *in)
2447c478bd9Sstevel@tonic-gate {
2457c478bd9Sstevel@tonic-gate 	switch (rq) {
2467c478bd9Sstevel@tonic-gate 	case SVCGET_XID: /* fall through for now */
2477c478bd9Sstevel@tonic-gate 	default:
2487c478bd9Sstevel@tonic-gate 		return (FALSE);
2497c478bd9Sstevel@tonic-gate 	}
2507c478bd9Sstevel@tonic-gate }
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate static struct xp_ops *
svc_raw_ops(void)25361961e0fSrobinson svc_raw_ops(void)
2547c478bd9Sstevel@tonic-gate {
2557c478bd9Sstevel@tonic-gate 	static struct xp_ops ops;
2567c478bd9Sstevel@tonic-gate 	extern mutex_t ops_lock;
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate /* VARIABLES PROTECTED BY ops_lock: ops */
2597c478bd9Sstevel@tonic-gate 
26061961e0fSrobinson 	(void) mutex_lock(&ops_lock);
2617c478bd9Sstevel@tonic-gate 	if (ops.xp_recv == NULL) {
2627c478bd9Sstevel@tonic-gate 		ops.xp_recv = svc_raw_recv;
2637c478bd9Sstevel@tonic-gate 		ops.xp_stat = svc_raw_stat;
2647c478bd9Sstevel@tonic-gate 		ops.xp_getargs = svc_raw_getargs;
2657c478bd9Sstevel@tonic-gate 		ops.xp_reply = svc_raw_reply;
2667c478bd9Sstevel@tonic-gate 		ops.xp_freeargs = svc_raw_freeargs;
2677c478bd9Sstevel@tonic-gate 		ops.xp_destroy = svc_raw_destroy;
2687c478bd9Sstevel@tonic-gate 		ops.xp_control = svc_raw_control;
2697c478bd9Sstevel@tonic-gate 	}
27061961e0fSrobinson 	(void) mutex_unlock(&ops_lock);
2717c478bd9Sstevel@tonic-gate 	return (&ops);
2727c478bd9Sstevel@tonic-gate }
273