xref: /onnv-gate/usr/src/lib/libnsl/rpc/svc_raw.c (revision 132:e3f7eaf7dde4)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
21*132Srobinson  */
22*132Srobinson 
23*132Srobinson /*
24*132Srobinson  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
250Sstevel@tonic-gate  * Use is subject to license terms.
260Sstevel@tonic-gate  */
270Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
280Sstevel@tonic-gate /* All Rights Reserved */
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley
310Sstevel@tonic-gate  * 4.3 BSD under license from the Regents of the University of
320Sstevel@tonic-gate  * California.
330Sstevel@tonic-gate  */
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
360Sstevel@tonic-gate 
370Sstevel@tonic-gate /*
380Sstevel@tonic-gate  * svc_raw.c,   This a toy for simple testing and timing.
390Sstevel@tonic-gate  * Interface to create an rpc client and server in the same UNIX process.
400Sstevel@tonic-gate  * This lets us similate rpc and get rpc (round trip) overhead, without
410Sstevel@tonic-gate  * any interference from the kernal.
420Sstevel@tonic-gate  *
430Sstevel@tonic-gate  */
440Sstevel@tonic-gate 
450Sstevel@tonic-gate #include "mt.h"
460Sstevel@tonic-gate #include "rpc_mt.h"
47*132Srobinson #include <stdlib.h>
480Sstevel@tonic-gate #include <rpc/rpc.h>
490Sstevel@tonic-gate #include <sys/types.h>
500Sstevel@tonic-gate #include <rpc/raw.h>
510Sstevel@tonic-gate #include <syslog.h>
520Sstevel@tonic-gate 
530Sstevel@tonic-gate #ifndef UDPMSGSIZE
540Sstevel@tonic-gate #define	UDPMSGSIZE 8800
550Sstevel@tonic-gate #endif
560Sstevel@tonic-gate 
570Sstevel@tonic-gate /*
580Sstevel@tonic-gate  * This is the "network" that we will be moving data over
590Sstevel@tonic-gate  */
600Sstevel@tonic-gate static struct svc_raw_private {
610Sstevel@tonic-gate 	char	*raw_buf;	/* should be shared with the cl handle */
620Sstevel@tonic-gate 	SVCXPRT	*server;
630Sstevel@tonic-gate 	XDR	xdr_stream;
640Sstevel@tonic-gate 	char	verf_body[MAX_AUTH_BYTES];
650Sstevel@tonic-gate } *svc_raw_private;
660Sstevel@tonic-gate 
670Sstevel@tonic-gate static struct xp_ops *svc_raw_ops();
680Sstevel@tonic-gate extern mutex_t	svcraw_lock;
690Sstevel@tonic-gate 
700Sstevel@tonic-gate 
710Sstevel@tonic-gate 
720Sstevel@tonic-gate SVCXPRT *
svc_raw_create(void)73*132Srobinson svc_raw_create(void)
740Sstevel@tonic-gate {
750Sstevel@tonic-gate 	struct svc_raw_private *srp;
760Sstevel@tonic-gate 	bool_t flag1 = FALSE, flag2 = FALSE;
770Sstevel@tonic-gate 
780Sstevel@tonic-gate /* VARIABLES PROTECTED BY svcraw_lock: svc_raw_private, srp */
79*132Srobinson 	(void) mutex_lock(&svcraw_lock);
800Sstevel@tonic-gate 	srp = svc_raw_private;
810Sstevel@tonic-gate 	if (srp == NULL) {
82*132Srobinson 		srp = calloc(1, sizeof (*srp));
830Sstevel@tonic-gate 		if (srp == NULL) {
840Sstevel@tonic-gate 			syslog(LOG_ERR, "svc_raw_create: out of memory");
85*132Srobinson 			(void) mutex_unlock(&svcraw_lock);
86*132Srobinson 			return (NULL);
870Sstevel@tonic-gate 		}
880Sstevel@tonic-gate 		flag1 = TRUE;
890Sstevel@tonic-gate 		if (_rawcombuf == NULL) {
90*132Srobinson 			_rawcombuf = calloc(UDPMSGSIZE, sizeof (char));
910Sstevel@tonic-gate 			if (_rawcombuf == NULL) {
92*132Srobinson 				free(srp);
930Sstevel@tonic-gate 				syslog(LOG_ERR, "svc_raw_create: "
940Sstevel@tonic-gate 					"out of memory");
95*132Srobinson 				(void) mutex_unlock(&svcraw_lock);
96*132Srobinson 				return (NULL);
970Sstevel@tonic-gate 			}
980Sstevel@tonic-gate 			flag2 = TRUE;
990Sstevel@tonic-gate 		}
1000Sstevel@tonic-gate 		srp->raw_buf = _rawcombuf; /* Share it with the client */
1010Sstevel@tonic-gate 		svc_raw_private = srp;
1020Sstevel@tonic-gate 	}
1030Sstevel@tonic-gate 	if ((srp->server = svc_xprt_alloc()) == NULL) {
1040Sstevel@tonic-gate 		if (flag2)
1050Sstevel@tonic-gate 			free(svc_raw_private->raw_buf);
1060Sstevel@tonic-gate 		if (flag1)
1070Sstevel@tonic-gate 			free(svc_raw_private);
108*132Srobinson 		(void) mutex_unlock(&svcraw_lock);
109*132Srobinson 		return (NULL);
1100Sstevel@tonic-gate 	}
1110Sstevel@tonic-gate 	/*
1120Sstevel@tonic-gate 	 * By convention, using FD_SETSIZE as the psuedo file descriptor
1130Sstevel@tonic-gate 	 */
1140Sstevel@tonic-gate 	srp->server->xp_fd = FD_SETSIZE;
1150Sstevel@tonic-gate 	srp->server->xp_port = 0;
1160Sstevel@tonic-gate 	srp->server->xp_ops = svc_raw_ops();
1170Sstevel@tonic-gate 	srp->server->xp_verf.oa_base = srp->verf_body;
1180Sstevel@tonic-gate 	xdrmem_create(&srp->xdr_stream, srp->raw_buf, UDPMSGSIZE, XDR_DECODE);
1190Sstevel@tonic-gate 	xprt_register(srp->server);
120*132Srobinson 	(void) mutex_unlock(&svcraw_lock);
1210Sstevel@tonic-gate 	return (srp->server);
1220Sstevel@tonic-gate }
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate /*ARGSUSED*/
1250Sstevel@tonic-gate static enum xprt_stat
svc_raw_stat(SVCXPRT * xprt)126*132Srobinson svc_raw_stat(SVCXPRT *xprt)
1270Sstevel@tonic-gate {
1280Sstevel@tonic-gate 	return (XPRT_IDLE);
1290Sstevel@tonic-gate }
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate /*ARGSUSED*/
1320Sstevel@tonic-gate static bool_t
svc_raw_recv(SVCXPRT * xprt,struct rpc_msg * msg)133*132Srobinson svc_raw_recv(SVCXPRT *xprt, struct rpc_msg *msg)
1340Sstevel@tonic-gate {
1350Sstevel@tonic-gate 	struct svc_raw_private *srp;
1360Sstevel@tonic-gate 	XDR *xdrs;
1370Sstevel@tonic-gate 
138*132Srobinson 	(void) mutex_lock(&svcraw_lock);
1390Sstevel@tonic-gate 	srp = svc_raw_private;
1400Sstevel@tonic-gate 	if (srp == NULL) {
141*132Srobinson 		(void) mutex_unlock(&svcraw_lock);
1420Sstevel@tonic-gate 		return (FALSE);
1430Sstevel@tonic-gate 	}
144*132Srobinson 	(void) mutex_unlock(&svcraw_lock);
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 	xdrs = &srp->xdr_stream;
1470Sstevel@tonic-gate 	xdrs->x_op = XDR_DECODE;
1480Sstevel@tonic-gate 	(void) XDR_SETPOS(xdrs, 0);
149*132Srobinson 	return (xdr_callmsg(xdrs, msg));
1500Sstevel@tonic-gate }
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate /*ARGSUSED*/
1530Sstevel@tonic-gate static bool_t
svc_raw_reply(SVCXPRT * xprt,struct rpc_msg * msg)154*132Srobinson svc_raw_reply(SVCXPRT *xprt, struct rpc_msg *msg)
1550Sstevel@tonic-gate {
1560Sstevel@tonic-gate 	struct svc_raw_private *srp;
1570Sstevel@tonic-gate 	XDR *xdrs;
1580Sstevel@tonic-gate 
159*132Srobinson 	(void) mutex_lock(&svcraw_lock);
1600Sstevel@tonic-gate 	srp = svc_raw_private;
1610Sstevel@tonic-gate 	if (srp == NULL) {
162*132Srobinson 		(void) mutex_unlock(&svcraw_lock);
1630Sstevel@tonic-gate 		return (FALSE);
1640Sstevel@tonic-gate 	}
165*132Srobinson 	(void) mutex_unlock(&svcraw_lock);
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	xdrs = &srp->xdr_stream;
1680Sstevel@tonic-gate 	xdrs->x_op = XDR_ENCODE;
1690Sstevel@tonic-gate 	(void) XDR_SETPOS(xdrs, 0);
170*132Srobinson 	return (xdr_replymsg(xdrs, msg));
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate /*ARGSUSED*/
1740Sstevel@tonic-gate static bool_t
svc_raw_getargs(SVCXPRT * xprt,xdrproc_t xdr_args,caddr_t args_ptr)175*132Srobinson svc_raw_getargs(SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr)
1760Sstevel@tonic-gate {
1770Sstevel@tonic-gate 	struct svc_raw_private *srp;
1780Sstevel@tonic-gate 
179*132Srobinson 	(void) mutex_lock(&svcraw_lock);
1800Sstevel@tonic-gate 	srp = svc_raw_private;
1810Sstevel@tonic-gate 	if (srp == NULL) {
182*132Srobinson 		(void) mutex_unlock(&svcraw_lock);
1830Sstevel@tonic-gate 		return (FALSE);
1840Sstevel@tonic-gate 	}
185*132Srobinson 	(void) mutex_unlock(&svcraw_lock);
186*132Srobinson 	return ((*xdr_args)(&srp->xdr_stream, args_ptr));
1870Sstevel@tonic-gate }
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate /*ARGSUSED*/
1900Sstevel@tonic-gate static bool_t
svc_raw_freeargs(SVCXPRT * xprt,xdrproc_t xdr_args,caddr_t args_ptr)191*132Srobinson svc_raw_freeargs(SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr)
1920Sstevel@tonic-gate {
1930Sstevel@tonic-gate 	struct svc_raw_private *srp;
1940Sstevel@tonic-gate 	XDR *xdrs;
1950Sstevel@tonic-gate 
196*132Srobinson 	(void) mutex_lock(&svcraw_lock);
1970Sstevel@tonic-gate 	srp = svc_raw_private;
1980Sstevel@tonic-gate 	if (srp == NULL) {
199*132Srobinson 		(void) mutex_unlock(&svcraw_lock);
2000Sstevel@tonic-gate 		return (FALSE);
2010Sstevel@tonic-gate 	}
202*132Srobinson 	(void) mutex_unlock(&svcraw_lock);
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 	xdrs = &srp->xdr_stream;
2050Sstevel@tonic-gate 	xdrs->x_op = XDR_FREE;
206*132Srobinson 	return ((*xdr_args)(xdrs, args_ptr));
2070Sstevel@tonic-gate }
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate /*ARGSUSED*/
2100Sstevel@tonic-gate static void
svc_raw_destroy(SVCXPRT * xprt)211*132Srobinson svc_raw_destroy(SVCXPRT *xprt)
2120Sstevel@tonic-gate {
2130Sstevel@tonic-gate }
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate /*ARGSUSED*/
2160Sstevel@tonic-gate static bool_t
svc_raw_control(SVCXPRT * xprt,const uint_t rq,void * in)217*132Srobinson svc_raw_control(SVCXPRT *xprt, const uint_t rq, void *in)
2180Sstevel@tonic-gate {
2190Sstevel@tonic-gate 	switch (rq) {
2200Sstevel@tonic-gate 	case SVCGET_XID: /* fall through for now */
2210Sstevel@tonic-gate 	default:
2220Sstevel@tonic-gate 		return (FALSE);
2230Sstevel@tonic-gate 	}
2240Sstevel@tonic-gate }
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate static struct xp_ops *
svc_raw_ops(void)227*132Srobinson svc_raw_ops(void)
2280Sstevel@tonic-gate {
2290Sstevel@tonic-gate 	static struct xp_ops ops;
2300Sstevel@tonic-gate 	extern mutex_t ops_lock;
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate /* VARIABLES PROTECTED BY ops_lock: ops */
2330Sstevel@tonic-gate 
234*132Srobinson 	(void) mutex_lock(&ops_lock);
2350Sstevel@tonic-gate 	if (ops.xp_recv == NULL) {
2360Sstevel@tonic-gate 		ops.xp_recv = svc_raw_recv;
2370Sstevel@tonic-gate 		ops.xp_stat = svc_raw_stat;
2380Sstevel@tonic-gate 		ops.xp_getargs = svc_raw_getargs;
2390Sstevel@tonic-gate 		ops.xp_reply = svc_raw_reply;
2400Sstevel@tonic-gate 		ops.xp_freeargs = svc_raw_freeargs;
2410Sstevel@tonic-gate 		ops.xp_destroy = svc_raw_destroy;
2420Sstevel@tonic-gate 		ops.xp_control = svc_raw_control;
2430Sstevel@tonic-gate 	}
244*132Srobinson 	(void) mutex_unlock(&ops_lock);
2450Sstevel@tonic-gate 	return (&ops);
2460Sstevel@tonic-gate }
247