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 21*61961e0fSrobinson */ 22*61961e0fSrobinson 23*61961e0fSrobinson /* 24*61961e0fSrobinson * 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 #pragma ident "%Z%%M% %I% %E% SMI" 367c478bd9Sstevel@tonic-gate 377c478bd9Sstevel@tonic-gate /* 387c478bd9Sstevel@tonic-gate * svc_raw.c, This a toy for simple testing and timing. 397c478bd9Sstevel@tonic-gate * Interface to create an rpc client and server in the same UNIX process. 407c478bd9Sstevel@tonic-gate * This lets us similate rpc and get rpc (round trip) overhead, without 417c478bd9Sstevel@tonic-gate * any interference from the kernal. 427c478bd9Sstevel@tonic-gate * 437c478bd9Sstevel@tonic-gate */ 447c478bd9Sstevel@tonic-gate 457c478bd9Sstevel@tonic-gate #include "mt.h" 467c478bd9Sstevel@tonic-gate #include "rpc_mt.h" 47*61961e0fSrobinson #include <stdlib.h> 487c478bd9Sstevel@tonic-gate #include <rpc/rpc.h> 497c478bd9Sstevel@tonic-gate #include <sys/types.h> 507c478bd9Sstevel@tonic-gate #include <rpc/raw.h> 517c478bd9Sstevel@tonic-gate #include <syslog.h> 527c478bd9Sstevel@tonic-gate 537c478bd9Sstevel@tonic-gate #ifndef UDPMSGSIZE 547c478bd9Sstevel@tonic-gate #define UDPMSGSIZE 8800 557c478bd9Sstevel@tonic-gate #endif 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate /* 587c478bd9Sstevel@tonic-gate * This is the "network" that we will be moving data over 597c478bd9Sstevel@tonic-gate */ 607c478bd9Sstevel@tonic-gate static struct svc_raw_private { 617c478bd9Sstevel@tonic-gate char *raw_buf; /* should be shared with the cl handle */ 627c478bd9Sstevel@tonic-gate SVCXPRT *server; 637c478bd9Sstevel@tonic-gate XDR xdr_stream; 647c478bd9Sstevel@tonic-gate char verf_body[MAX_AUTH_BYTES]; 657c478bd9Sstevel@tonic-gate } *svc_raw_private; 667c478bd9Sstevel@tonic-gate 677c478bd9Sstevel@tonic-gate static struct xp_ops *svc_raw_ops(); 687c478bd9Sstevel@tonic-gate extern mutex_t svcraw_lock; 697c478bd9Sstevel@tonic-gate 707c478bd9Sstevel@tonic-gate 717c478bd9Sstevel@tonic-gate 727c478bd9Sstevel@tonic-gate SVCXPRT * 73*61961e0fSrobinson svc_raw_create(void) 747c478bd9Sstevel@tonic-gate { 757c478bd9Sstevel@tonic-gate struct svc_raw_private *srp; 767c478bd9Sstevel@tonic-gate bool_t flag1 = FALSE, flag2 = FALSE; 777c478bd9Sstevel@tonic-gate 787c478bd9Sstevel@tonic-gate /* VARIABLES PROTECTED BY svcraw_lock: svc_raw_private, srp */ 79*61961e0fSrobinson (void) mutex_lock(&svcraw_lock); 807c478bd9Sstevel@tonic-gate srp = svc_raw_private; 817c478bd9Sstevel@tonic-gate if (srp == NULL) { 82*61961e0fSrobinson srp = calloc(1, sizeof (*srp)); 837c478bd9Sstevel@tonic-gate if (srp == NULL) { 847c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "svc_raw_create: out of memory"); 85*61961e0fSrobinson (void) mutex_unlock(&svcraw_lock); 86*61961e0fSrobinson return (NULL); 877c478bd9Sstevel@tonic-gate } 887c478bd9Sstevel@tonic-gate flag1 = TRUE; 897c478bd9Sstevel@tonic-gate if (_rawcombuf == NULL) { 90*61961e0fSrobinson _rawcombuf = calloc(UDPMSGSIZE, sizeof (char)); 917c478bd9Sstevel@tonic-gate if (_rawcombuf == NULL) { 92*61961e0fSrobinson free(srp); 937c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "svc_raw_create: " 947c478bd9Sstevel@tonic-gate "out of memory"); 95*61961e0fSrobinson (void) mutex_unlock(&svcraw_lock); 96*61961e0fSrobinson return (NULL); 977c478bd9Sstevel@tonic-gate } 987c478bd9Sstevel@tonic-gate flag2 = TRUE; 997c478bd9Sstevel@tonic-gate } 1007c478bd9Sstevel@tonic-gate srp->raw_buf = _rawcombuf; /* Share it with the client */ 1017c478bd9Sstevel@tonic-gate svc_raw_private = srp; 1027c478bd9Sstevel@tonic-gate } 1037c478bd9Sstevel@tonic-gate if ((srp->server = svc_xprt_alloc()) == NULL) { 1047c478bd9Sstevel@tonic-gate if (flag2) 1057c478bd9Sstevel@tonic-gate free(svc_raw_private->raw_buf); 1067c478bd9Sstevel@tonic-gate if (flag1) 1077c478bd9Sstevel@tonic-gate free(svc_raw_private); 108*61961e0fSrobinson (void) mutex_unlock(&svcraw_lock); 109*61961e0fSrobinson return (NULL); 1107c478bd9Sstevel@tonic-gate } 1117c478bd9Sstevel@tonic-gate /* 1127c478bd9Sstevel@tonic-gate * By convention, using FD_SETSIZE as the psuedo file descriptor 1137c478bd9Sstevel@tonic-gate */ 1147c478bd9Sstevel@tonic-gate srp->server->xp_fd = FD_SETSIZE; 1157c478bd9Sstevel@tonic-gate srp->server->xp_port = 0; 1167c478bd9Sstevel@tonic-gate srp->server->xp_ops = svc_raw_ops(); 1177c478bd9Sstevel@tonic-gate srp->server->xp_verf.oa_base = srp->verf_body; 1187c478bd9Sstevel@tonic-gate xdrmem_create(&srp->xdr_stream, srp->raw_buf, UDPMSGSIZE, XDR_DECODE); 1197c478bd9Sstevel@tonic-gate xprt_register(srp->server); 120*61961e0fSrobinson (void) mutex_unlock(&svcraw_lock); 1217c478bd9Sstevel@tonic-gate return (srp->server); 1227c478bd9Sstevel@tonic-gate } 1237c478bd9Sstevel@tonic-gate 1247c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 1257c478bd9Sstevel@tonic-gate static enum xprt_stat 126*61961e0fSrobinson svc_raw_stat(SVCXPRT *xprt) 1277c478bd9Sstevel@tonic-gate { 1287c478bd9Sstevel@tonic-gate return (XPRT_IDLE); 1297c478bd9Sstevel@tonic-gate } 1307c478bd9Sstevel@tonic-gate 1317c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 1327c478bd9Sstevel@tonic-gate static bool_t 133*61961e0fSrobinson svc_raw_recv(SVCXPRT *xprt, struct rpc_msg *msg) 1347c478bd9Sstevel@tonic-gate { 1357c478bd9Sstevel@tonic-gate struct svc_raw_private *srp; 1367c478bd9Sstevel@tonic-gate XDR *xdrs; 1377c478bd9Sstevel@tonic-gate 138*61961e0fSrobinson (void) mutex_lock(&svcraw_lock); 1397c478bd9Sstevel@tonic-gate srp = svc_raw_private; 1407c478bd9Sstevel@tonic-gate if (srp == NULL) { 141*61961e0fSrobinson (void) mutex_unlock(&svcraw_lock); 1427c478bd9Sstevel@tonic-gate return (FALSE); 1437c478bd9Sstevel@tonic-gate } 144*61961e0fSrobinson (void) mutex_unlock(&svcraw_lock); 1457c478bd9Sstevel@tonic-gate 1467c478bd9Sstevel@tonic-gate xdrs = &srp->xdr_stream; 1477c478bd9Sstevel@tonic-gate xdrs->x_op = XDR_DECODE; 1487c478bd9Sstevel@tonic-gate (void) XDR_SETPOS(xdrs, 0); 149*61961e0fSrobinson return (xdr_callmsg(xdrs, msg)); 1507c478bd9Sstevel@tonic-gate } 1517c478bd9Sstevel@tonic-gate 1527c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 1537c478bd9Sstevel@tonic-gate static bool_t 154*61961e0fSrobinson svc_raw_reply(SVCXPRT *xprt, struct rpc_msg *msg) 1557c478bd9Sstevel@tonic-gate { 1567c478bd9Sstevel@tonic-gate struct svc_raw_private *srp; 1577c478bd9Sstevel@tonic-gate XDR *xdrs; 1587c478bd9Sstevel@tonic-gate 159*61961e0fSrobinson (void) mutex_lock(&svcraw_lock); 1607c478bd9Sstevel@tonic-gate srp = svc_raw_private; 1617c478bd9Sstevel@tonic-gate if (srp == NULL) { 162*61961e0fSrobinson (void) mutex_unlock(&svcraw_lock); 1637c478bd9Sstevel@tonic-gate return (FALSE); 1647c478bd9Sstevel@tonic-gate } 165*61961e0fSrobinson (void) mutex_unlock(&svcraw_lock); 1667c478bd9Sstevel@tonic-gate 1677c478bd9Sstevel@tonic-gate xdrs = &srp->xdr_stream; 1687c478bd9Sstevel@tonic-gate xdrs->x_op = XDR_ENCODE; 1697c478bd9Sstevel@tonic-gate (void) XDR_SETPOS(xdrs, 0); 170*61961e0fSrobinson return (xdr_replymsg(xdrs, msg)); 1717c478bd9Sstevel@tonic-gate } 1727c478bd9Sstevel@tonic-gate 1737c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 1747c478bd9Sstevel@tonic-gate static bool_t 175*61961e0fSrobinson svc_raw_getargs(SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr) 1767c478bd9Sstevel@tonic-gate { 1777c478bd9Sstevel@tonic-gate struct svc_raw_private *srp; 1787c478bd9Sstevel@tonic-gate 179*61961e0fSrobinson (void) mutex_lock(&svcraw_lock); 1807c478bd9Sstevel@tonic-gate srp = svc_raw_private; 1817c478bd9Sstevel@tonic-gate if (srp == NULL) { 182*61961e0fSrobinson (void) mutex_unlock(&svcraw_lock); 1837c478bd9Sstevel@tonic-gate return (FALSE); 1847c478bd9Sstevel@tonic-gate } 185*61961e0fSrobinson (void) mutex_unlock(&svcraw_lock); 186*61961e0fSrobinson return ((*xdr_args)(&srp->xdr_stream, args_ptr)); 1877c478bd9Sstevel@tonic-gate } 1887c478bd9Sstevel@tonic-gate 1897c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 1907c478bd9Sstevel@tonic-gate static bool_t 191*61961e0fSrobinson svc_raw_freeargs(SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr) 1927c478bd9Sstevel@tonic-gate { 1937c478bd9Sstevel@tonic-gate struct svc_raw_private *srp; 1947c478bd9Sstevel@tonic-gate XDR *xdrs; 1957c478bd9Sstevel@tonic-gate 196*61961e0fSrobinson (void) mutex_lock(&svcraw_lock); 1977c478bd9Sstevel@tonic-gate srp = svc_raw_private; 1987c478bd9Sstevel@tonic-gate if (srp == NULL) { 199*61961e0fSrobinson (void) mutex_unlock(&svcraw_lock); 2007c478bd9Sstevel@tonic-gate return (FALSE); 2017c478bd9Sstevel@tonic-gate } 202*61961e0fSrobinson (void) mutex_unlock(&svcraw_lock); 2037c478bd9Sstevel@tonic-gate 2047c478bd9Sstevel@tonic-gate xdrs = &srp->xdr_stream; 2057c478bd9Sstevel@tonic-gate xdrs->x_op = XDR_FREE; 206*61961e0fSrobinson return ((*xdr_args)(xdrs, args_ptr)); 2077c478bd9Sstevel@tonic-gate } 2087c478bd9Sstevel@tonic-gate 2097c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 2107c478bd9Sstevel@tonic-gate static void 211*61961e0fSrobinson svc_raw_destroy(SVCXPRT *xprt) 2127c478bd9Sstevel@tonic-gate { 2137c478bd9Sstevel@tonic-gate } 2147c478bd9Sstevel@tonic-gate 2157c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 2167c478bd9Sstevel@tonic-gate static bool_t 217*61961e0fSrobinson svc_raw_control(SVCXPRT *xprt, const uint_t rq, void *in) 2187c478bd9Sstevel@tonic-gate { 2197c478bd9Sstevel@tonic-gate switch (rq) { 2207c478bd9Sstevel@tonic-gate case SVCGET_XID: /* fall through for now */ 2217c478bd9Sstevel@tonic-gate default: 2227c478bd9Sstevel@tonic-gate return (FALSE); 2237c478bd9Sstevel@tonic-gate } 2247c478bd9Sstevel@tonic-gate } 2257c478bd9Sstevel@tonic-gate 2267c478bd9Sstevel@tonic-gate static struct xp_ops * 227*61961e0fSrobinson svc_raw_ops(void) 2287c478bd9Sstevel@tonic-gate { 2297c478bd9Sstevel@tonic-gate static struct xp_ops ops; 2307c478bd9Sstevel@tonic-gate extern mutex_t ops_lock; 2317c478bd9Sstevel@tonic-gate 2327c478bd9Sstevel@tonic-gate /* VARIABLES PROTECTED BY ops_lock: ops */ 2337c478bd9Sstevel@tonic-gate 234*61961e0fSrobinson (void) mutex_lock(&ops_lock); 2357c478bd9Sstevel@tonic-gate if (ops.xp_recv == NULL) { 2367c478bd9Sstevel@tonic-gate ops.xp_recv = svc_raw_recv; 2377c478bd9Sstevel@tonic-gate ops.xp_stat = svc_raw_stat; 2387c478bd9Sstevel@tonic-gate ops.xp_getargs = svc_raw_getargs; 2397c478bd9Sstevel@tonic-gate ops.xp_reply = svc_raw_reply; 2407c478bd9Sstevel@tonic-gate ops.xp_freeargs = svc_raw_freeargs; 2417c478bd9Sstevel@tonic-gate ops.xp_destroy = svc_raw_destroy; 2427c478bd9Sstevel@tonic-gate ops.xp_control = svc_raw_control; 2437c478bd9Sstevel@tonic-gate } 244*61961e0fSrobinson (void) mutex_unlock(&ops_lock); 2457c478bd9Sstevel@tonic-gate return (&ops); 2467c478bd9Sstevel@tonic-gate } 247