1*84d9c625SLionel Sambuc /* $NetBSD: svc_raw.c,v 1.24 2013/03/11 20:19:29 tron Exp $ */
22fe8fb19SBen Gras
32fe8fb19SBen Gras /*
4*84d9c625SLionel Sambuc * Copyright (c) 2010, Oracle America, Inc.
52fe8fb19SBen Gras *
6*84d9c625SLionel Sambuc * Redistribution and use in source and binary forms, with or without
7*84d9c625SLionel Sambuc * modification, are permitted provided that the following conditions are
8*84d9c625SLionel Sambuc * met:
92fe8fb19SBen Gras *
10*84d9c625SLionel Sambuc * * Redistributions of source code must retain the above copyright
11*84d9c625SLionel Sambuc * notice, this list of conditions and the following disclaimer.
12*84d9c625SLionel Sambuc * * Redistributions in binary form must reproduce the above
13*84d9c625SLionel Sambuc * copyright notice, this list of conditions and the following
14*84d9c625SLionel Sambuc * disclaimer in the documentation and/or other materials
15*84d9c625SLionel Sambuc * provided with the distribution.
16*84d9c625SLionel Sambuc * * Neither the name of the "Oracle America, Inc." nor the names of its
17*84d9c625SLionel Sambuc * contributors may be used to endorse or promote products derived
18*84d9c625SLionel Sambuc * from this software without specific prior written permission.
192fe8fb19SBen Gras *
20*84d9c625SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21*84d9c625SLionel Sambuc * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*84d9c625SLionel Sambuc * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*84d9c625SLionel Sambuc * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24*84d9c625SLionel Sambuc * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25*84d9c625SLionel Sambuc * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26*84d9c625SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27*84d9c625SLionel Sambuc * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28*84d9c625SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29*84d9c625SLionel Sambuc * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30*84d9c625SLionel Sambuc * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31*84d9c625SLionel Sambuc * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
322fe8fb19SBen Gras */
332fe8fb19SBen Gras /*
342fe8fb19SBen Gras * Copyright (c) 1986-1991 by Sun Microsystems Inc.
352fe8fb19SBen Gras */
362fe8fb19SBen Gras
372fe8fb19SBen Gras /* #ident "@(#)svc_raw.c 1.16 94/04/24 SMI" */
382fe8fb19SBen Gras
392fe8fb19SBen Gras #include <sys/cdefs.h>
402fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
412fe8fb19SBen Gras #if 0
422fe8fb19SBen Gras static char sccsid[] = "@(#)svc_raw.c 1.25 89/01/31 Copyr 1984 Sun Micro";
432fe8fb19SBen Gras #else
44*84d9c625SLionel Sambuc __RCSID("$NetBSD: svc_raw.c,v 1.24 2013/03/11 20:19:29 tron Exp $");
452fe8fb19SBen Gras #endif
462fe8fb19SBen Gras #endif
472fe8fb19SBen Gras
482fe8fb19SBen Gras /*
492fe8fb19SBen Gras * svc_raw.c, This a toy for simple testing and timing.
502fe8fb19SBen Gras * Interface to create an rpc client and server in the same UNIX process.
512fe8fb19SBen Gras * This lets us similate rpc and get rpc (round trip) overhead, without
522fe8fb19SBen Gras * any interference from the kernel.
532fe8fb19SBen Gras *
542fe8fb19SBen Gras */
552fe8fb19SBen Gras
562fe8fb19SBen Gras #include "namespace.h"
572fe8fb19SBen Gras #include "reentrant.h"
582fe8fb19SBen Gras #include <rpc/rpc.h>
592fe8fb19SBen Gras #include <sys/types.h>
602fe8fb19SBen Gras #include <rpc/raw.h>
612fe8fb19SBen Gras #include <assert.h>
622fe8fb19SBen Gras #include <stdlib.h>
632fe8fb19SBen Gras
642fe8fb19SBen Gras #ifdef __weak_alias
652fe8fb19SBen Gras __weak_alias(svc_raw_create,_svc_raw_create)
662fe8fb19SBen Gras #endif
672fe8fb19SBen Gras
682fe8fb19SBen Gras #ifndef UDPMSGSIZE
692fe8fb19SBen Gras #define UDPMSGSIZE 8800
702fe8fb19SBen Gras #endif
712fe8fb19SBen Gras
722fe8fb19SBen Gras /*
732fe8fb19SBen Gras * This is the "network" that we will be moving data over
742fe8fb19SBen Gras */
752fe8fb19SBen Gras static struct svc_raw_private {
762fe8fb19SBen Gras char *raw_buf; /* should be shared with the cl handle */
772fe8fb19SBen Gras SVCXPRT server;
782fe8fb19SBen Gras XDR xdr_stream;
792fe8fb19SBen Gras char verf_body[MAX_AUTH_BYTES];
802fe8fb19SBen Gras } *svc_raw_private;
812fe8fb19SBen Gras
822fe8fb19SBen Gras #ifdef _REENTRANT
832fe8fb19SBen Gras extern mutex_t svcraw_lock;
842fe8fb19SBen Gras #endif
852fe8fb19SBen Gras
86f14fb602SLionel Sambuc static enum xprt_stat svc_raw_stat(SVCXPRT *);
87f14fb602SLionel Sambuc static bool_t svc_raw_recv(SVCXPRT *, struct rpc_msg *);
88f14fb602SLionel Sambuc static bool_t svc_raw_reply(SVCXPRT *, struct rpc_msg *);
89f14fb602SLionel Sambuc static bool_t svc_raw_getargs(SVCXPRT *, xdrproc_t, caddr_t);
90f14fb602SLionel Sambuc static bool_t svc_raw_freeargs(SVCXPRT *, xdrproc_t, caddr_t);
91f14fb602SLionel Sambuc static void svc_raw_destroy(SVCXPRT *);
92f14fb602SLionel Sambuc static void svc_raw_ops(SVCXPRT *);
93f14fb602SLionel Sambuc static bool_t svc_raw_control(SVCXPRT *, const u_int, void *);
942fe8fb19SBen Gras
952fe8fb19SBen Gras char *__rpc_rawcombuf = NULL;
962fe8fb19SBen Gras
972fe8fb19SBen Gras SVCXPRT *
svc_raw_create(void)98f14fb602SLionel Sambuc svc_raw_create(void)
992fe8fb19SBen Gras {
1002fe8fb19SBen Gras struct svc_raw_private *srp;
1012fe8fb19SBen Gras /* VARIABLES PROTECTED BY svcraw_lock: svc_raw_private, srp */
1022fe8fb19SBen Gras
1032fe8fb19SBen Gras mutex_lock(&svcraw_lock);
1042fe8fb19SBen Gras srp = svc_raw_private;
1052fe8fb19SBen Gras if (srp == NULL) {
1062fe8fb19SBen Gras srp = calloc(1, sizeof(*srp));
1072fe8fb19SBen Gras if (srp == NULL)
1082fe8fb19SBen Gras goto out;
1092fe8fb19SBen Gras if (__rpc_rawcombuf == NULL)
1102fe8fb19SBen Gras __rpc_rawcombuf = malloc(UDPMSGSIZE);
1112fe8fb19SBen Gras if (__rpc_rawcombuf == NULL)
1122fe8fb19SBen Gras goto out;
1132fe8fb19SBen Gras srp->raw_buf = __rpc_rawcombuf; /* Share it with the client */
1142fe8fb19SBen Gras svc_raw_private = srp;
1152fe8fb19SBen Gras }
1162fe8fb19SBen Gras srp->server.xp_fd = FD_SETSIZE;
1172fe8fb19SBen Gras srp->server.xp_port = 0;
1182fe8fb19SBen Gras srp->server.xp_p3 = NULL;
1192fe8fb19SBen Gras svc_raw_ops(&srp->server);
1202fe8fb19SBen Gras srp->server.xp_verf.oa_base = srp->verf_body;
1212fe8fb19SBen Gras xdrmem_create(&srp->xdr_stream, srp->raw_buf, UDPMSGSIZE, XDR_DECODE);
122*84d9c625SLionel Sambuc if (!xprt_register(&srp->server))
123*84d9c625SLionel Sambuc goto out;
1242fe8fb19SBen Gras mutex_unlock(&svcraw_lock);
1252fe8fb19SBen Gras return (&srp->server);
1262fe8fb19SBen Gras out:
1272fe8fb19SBen Gras if (srp != NULL)
1282fe8fb19SBen Gras free(srp);
1292fe8fb19SBen Gras mutex_unlock(&svcraw_lock);
1302fe8fb19SBen Gras return (NULL);
1312fe8fb19SBen Gras }
1322fe8fb19SBen Gras
1332fe8fb19SBen Gras /*ARGSUSED*/
1342fe8fb19SBen Gras static enum xprt_stat
svc_raw_stat(SVCXPRT * xprt)135f14fb602SLionel Sambuc svc_raw_stat(SVCXPRT *xprt) /* args needed to satisfy ANSI-C typechecking */
1362fe8fb19SBen Gras {
1372fe8fb19SBen Gras return (XPRT_IDLE);
1382fe8fb19SBen Gras }
1392fe8fb19SBen Gras
1402fe8fb19SBen Gras /*ARGSUSED*/
1412fe8fb19SBen Gras static bool_t
svc_raw_recv(SVCXPRT * xprt,struct rpc_msg * msg)142f14fb602SLionel Sambuc svc_raw_recv(SVCXPRT *xprt, struct rpc_msg *msg)
1432fe8fb19SBen Gras {
1442fe8fb19SBen Gras struct svc_raw_private *srp;
1452fe8fb19SBen Gras XDR *xdrs;
1462fe8fb19SBen Gras
1472fe8fb19SBen Gras mutex_lock(&svcraw_lock);
1482fe8fb19SBen Gras srp = svc_raw_private;
1492fe8fb19SBen Gras if (srp == NULL) {
1502fe8fb19SBen Gras mutex_unlock(&svcraw_lock);
1512fe8fb19SBen Gras return (FALSE);
1522fe8fb19SBen Gras }
1532fe8fb19SBen Gras mutex_unlock(&svcraw_lock);
1542fe8fb19SBen Gras
1552fe8fb19SBen Gras xdrs = &srp->xdr_stream;
1562fe8fb19SBen Gras xdrs->x_op = XDR_DECODE;
1572fe8fb19SBen Gras (void) XDR_SETPOS(xdrs, 0);
1582fe8fb19SBen Gras if (! xdr_callmsg(xdrs, msg)) {
1592fe8fb19SBen Gras return (FALSE);
1602fe8fb19SBen Gras }
1612fe8fb19SBen Gras return (TRUE);
1622fe8fb19SBen Gras }
1632fe8fb19SBen Gras
1642fe8fb19SBen Gras /*ARGSUSED*/
1652fe8fb19SBen Gras static bool_t
svc_raw_reply(SVCXPRT * xprt,struct rpc_msg * msg)166f14fb602SLionel Sambuc svc_raw_reply(SVCXPRT *xprt, struct rpc_msg *msg)
1672fe8fb19SBen Gras {
1682fe8fb19SBen Gras struct svc_raw_private *srp;
1692fe8fb19SBen Gras XDR *xdrs;
1702fe8fb19SBen Gras
1712fe8fb19SBen Gras mutex_lock(&svcraw_lock);
1722fe8fb19SBen Gras srp = svc_raw_private;
1732fe8fb19SBen Gras if (srp == NULL) {
1742fe8fb19SBen Gras mutex_unlock(&svcraw_lock);
1752fe8fb19SBen Gras return (FALSE);
1762fe8fb19SBen Gras }
1772fe8fb19SBen Gras mutex_unlock(&svcraw_lock);
1782fe8fb19SBen Gras
1792fe8fb19SBen Gras xdrs = &srp->xdr_stream;
1802fe8fb19SBen Gras xdrs->x_op = XDR_ENCODE;
1812fe8fb19SBen Gras (void) XDR_SETPOS(xdrs, 0);
1822fe8fb19SBen Gras if (! xdr_replymsg(xdrs, msg)) {
1832fe8fb19SBen Gras return (FALSE);
1842fe8fb19SBen Gras }
1852fe8fb19SBen Gras (void) XDR_GETPOS(xdrs); /* called just for overhead */
1862fe8fb19SBen Gras return (TRUE);
1872fe8fb19SBen Gras }
1882fe8fb19SBen Gras
1892fe8fb19SBen Gras /*ARGSUSED*/
1902fe8fb19SBen Gras static bool_t
svc_raw_getargs(SVCXPRT * xprt,xdrproc_t xdr_args,caddr_t args_ptr)191f14fb602SLionel Sambuc svc_raw_getargs(SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr)
1922fe8fb19SBen Gras {
1932fe8fb19SBen Gras struct svc_raw_private *srp;
1942fe8fb19SBen Gras
1952fe8fb19SBen Gras mutex_lock(&svcraw_lock);
1962fe8fb19SBen Gras srp = svc_raw_private;
1972fe8fb19SBen Gras if (srp == NULL) {
1982fe8fb19SBen Gras mutex_unlock(&svcraw_lock);
1992fe8fb19SBen Gras return (FALSE);
2002fe8fb19SBen Gras }
2012fe8fb19SBen Gras mutex_unlock(&svcraw_lock);
2022fe8fb19SBen Gras return (*xdr_args)(&srp->xdr_stream, args_ptr);
2032fe8fb19SBen Gras }
2042fe8fb19SBen Gras
2052fe8fb19SBen Gras /*ARGSUSED*/
2062fe8fb19SBen Gras static bool_t
svc_raw_freeargs(SVCXPRT * xprt,xdrproc_t xdr_args,caddr_t args_ptr)207f14fb602SLionel Sambuc svc_raw_freeargs(SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr)
2082fe8fb19SBen Gras {
2092fe8fb19SBen Gras struct svc_raw_private *srp;
2102fe8fb19SBen Gras XDR *xdrs;
2112fe8fb19SBen Gras
2122fe8fb19SBen Gras mutex_lock(&svcraw_lock);
2132fe8fb19SBen Gras srp = svc_raw_private;
2142fe8fb19SBen Gras if (srp == NULL) {
2152fe8fb19SBen Gras mutex_unlock(&svcraw_lock);
2162fe8fb19SBen Gras return (FALSE);
2172fe8fb19SBen Gras }
2182fe8fb19SBen Gras mutex_unlock(&svcraw_lock);
2192fe8fb19SBen Gras
2202fe8fb19SBen Gras xdrs = &srp->xdr_stream;
2212fe8fb19SBen Gras xdrs->x_op = XDR_FREE;
2222fe8fb19SBen Gras return (*xdr_args)(xdrs, args_ptr);
2232fe8fb19SBen Gras }
2242fe8fb19SBen Gras
2252fe8fb19SBen Gras /*ARGSUSED*/
2262fe8fb19SBen Gras static void
svc_raw_destroy(SVCXPRT * xprt)227f14fb602SLionel Sambuc svc_raw_destroy(SVCXPRT *xprt)
2282fe8fb19SBen Gras {
2292fe8fb19SBen Gras }
2302fe8fb19SBen Gras
2312fe8fb19SBen Gras /*ARGSUSED*/
2322fe8fb19SBen Gras static bool_t
svc_raw_control(SVCXPRT * xprt,const u_int rq,void * in)233f14fb602SLionel Sambuc svc_raw_control(SVCXPRT *xprt, const u_int rq, void *in)
2342fe8fb19SBen Gras {
2352fe8fb19SBen Gras return (FALSE);
2362fe8fb19SBen Gras }
2372fe8fb19SBen Gras
2382fe8fb19SBen Gras static void
svc_raw_ops(SVCXPRT * xprt)239f14fb602SLionel Sambuc svc_raw_ops(SVCXPRT *xprt)
2402fe8fb19SBen Gras {
2412fe8fb19SBen Gras static struct xp_ops ops;
2422fe8fb19SBen Gras static struct xp_ops2 ops2;
2432fe8fb19SBen Gras #ifdef _REENTRANT
2442fe8fb19SBen Gras extern mutex_t ops_lock;
2452fe8fb19SBen Gras #endif
2462fe8fb19SBen Gras
2472fe8fb19SBen Gras _DIAGASSERT(xprt != NULL);
2482fe8fb19SBen Gras
2492fe8fb19SBen Gras /* VARIABLES PROTECTED BY ops_lock: ops */
2502fe8fb19SBen Gras
2512fe8fb19SBen Gras mutex_lock(&ops_lock);
2522fe8fb19SBen Gras if (ops.xp_recv == NULL) {
2532fe8fb19SBen Gras ops.xp_recv = svc_raw_recv;
2542fe8fb19SBen Gras ops.xp_stat = svc_raw_stat;
2552fe8fb19SBen Gras ops.xp_getargs = svc_raw_getargs;
2562fe8fb19SBen Gras ops.xp_reply = svc_raw_reply;
2572fe8fb19SBen Gras ops.xp_freeargs = svc_raw_freeargs;
2582fe8fb19SBen Gras ops.xp_destroy = svc_raw_destroy;
2592fe8fb19SBen Gras ops2.xp_control = svc_raw_control;
2602fe8fb19SBen Gras }
2612fe8fb19SBen Gras xprt->xp_ops = &ops;
2622fe8fb19SBen Gras xprt->xp_ops2 = &ops2;
2632fe8fb19SBen Gras mutex_unlock(&ops_lock);
2642fe8fb19SBen Gras }
265