Home | History | Annotate | Line # | Download | only in rpc
svc_raw.c revision 1.21.22.1
      1  1.21.22.1       riz /*	$NetBSD: svc_raw.c,v 1.21.22.1 2013/03/14 22:03:08 riz Exp $	*/
      2        1.3       cgd 
      3        1.1       cgd /*
      4  1.21.22.1       riz  * Copyright (c) 2010, Oracle America, Inc.
      5  1.21.22.1       riz  *
      6  1.21.22.1       riz  * Redistribution and use in source and binary forms, with or without
      7  1.21.22.1       riz  * modification, are permitted provided that the following conditions are
      8  1.21.22.1       riz  * met:
      9  1.21.22.1       riz  *
     10  1.21.22.1       riz  *     * Redistributions of source code must retain the above copyright
     11  1.21.22.1       riz  *       notice, this list of conditions and the following disclaimer.
     12  1.21.22.1       riz  *     * Redistributions in binary form must reproduce the above
     13  1.21.22.1       riz  *       copyright notice, this list of conditions and the following
     14  1.21.22.1       riz  *       disclaimer in the documentation and/or other materials
     15  1.21.22.1       riz  *       provided with the distribution.
     16  1.21.22.1       riz  *     * Neither the name of the "Oracle America, Inc." nor the names of its
     17  1.21.22.1       riz  *       contributors may be used to endorse or promote products derived
     18  1.21.22.1       riz  *       from this software without specific prior written permission.
     19  1.21.22.1       riz  *
     20  1.21.22.1       riz  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     21  1.21.22.1       riz  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     22  1.21.22.1       riz  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     23  1.21.22.1       riz  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
     24  1.21.22.1       riz  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     25  1.21.22.1       riz  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.21.22.1       riz  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     27  1.21.22.1       riz  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  1.21.22.1       riz  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     29  1.21.22.1       riz  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     30  1.21.22.1       riz  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     31  1.21.22.1       riz  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32        1.1       cgd  */
     33       1.13      fvdl /*
     34       1.13      fvdl  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
     35       1.13      fvdl  */
     36       1.13      fvdl 
     37       1.13      fvdl /* #ident	"@(#)svc_raw.c	1.16	94/04/24 SMI" */
     38        1.1       cgd 
     39       1.17    itojun #include <sys/cdefs.h>
     40       1.17    itojun #if defined(LIBC_SCCS) && !defined(lint)
     41        1.4  christos #if 0
     42       1.13      fvdl static char sccsid[] = "@(#)svc_raw.c 1.25 89/01/31 Copyr 1984 Sun Micro";
     43       1.17    itojun #else
     44  1.21.22.1       riz __RCSID("$NetBSD: svc_raw.c,v 1.21.22.1 2013/03/14 22:03:08 riz Exp $");
     45        1.4  christos #endif
     46        1.1       cgd #endif
     47        1.1       cgd 
     48        1.1       cgd /*
     49        1.1       cgd  * svc_raw.c,   This a toy for simple testing and timing.
     50        1.1       cgd  * Interface to create an rpc client and server in the same UNIX process.
     51        1.1       cgd  * This lets us similate rpc and get rpc (round trip) overhead, without
     52       1.19       wiz  * any interference from the kernel.
     53        1.1       cgd  *
     54        1.1       cgd  */
     55        1.1       cgd 
     56        1.5       jtc #include "namespace.h"
     57       1.13      fvdl #include "reentrant.h"
     58       1.13      fvdl #include <rpc/rpc.h>
     59       1.13      fvdl #include <sys/types.h>
     60       1.13      fvdl #include <rpc/raw.h>
     61       1.15     lukem #include <assert.h>
     62        1.2       cgd #include <stdlib.h>
     63        1.8     lukem 
     64       1.13      fvdl #ifdef __weak_alias
     65       1.13      fvdl __weak_alias(svc_raw_create,_svc_raw_create)
     66       1.13      fvdl #endif
     67        1.1       cgd 
     68       1.13      fvdl #ifndef UDPMSGSIZE
     69       1.13      fvdl #define	UDPMSGSIZE 8800
     70        1.5       jtc #endif
     71        1.1       cgd 
     72        1.1       cgd /*
     73        1.1       cgd  * This is the "network" that we will be moving data over
     74        1.1       cgd  */
     75       1.13      fvdl static struct svc_raw_private {
     76       1.13      fvdl 	char	*raw_buf;	/* should be shared with the cl handle */
     77        1.1       cgd 	SVCXPRT	server;
     78        1.1       cgd 	XDR	xdr_stream;
     79        1.1       cgd 	char	verf_body[MAX_AUTH_BYTES];
     80       1.13      fvdl } *svc_raw_private;
     81       1.13      fvdl 
     82       1.16   thorpej #ifdef _REENTRANT
     83       1.13      fvdl extern mutex_t	svcraw_lock;
     84       1.13      fvdl #endif
     85        1.1       cgd 
     86       1.13      fvdl static enum xprt_stat svc_raw_stat __P((SVCXPRT *));
     87       1.13      fvdl static bool_t svc_raw_recv __P((SVCXPRT *, struct rpc_msg *));
     88       1.13      fvdl static bool_t svc_raw_reply __P((SVCXPRT *, struct rpc_msg *));
     89       1.13      fvdl static bool_t svc_raw_getargs __P((SVCXPRT *, xdrproc_t, caddr_t));
     90       1.13      fvdl static bool_t svc_raw_freeargs __P((SVCXPRT *, xdrproc_t, caddr_t));
     91       1.13      fvdl static void svc_raw_destroy __P((SVCXPRT *));
     92       1.13      fvdl static void svc_raw_ops __P((SVCXPRT *));
     93       1.13      fvdl static bool_t svc_raw_control __P((SVCXPRT *, const u_int, void *));
     94       1.13      fvdl 
     95       1.13      fvdl char *__rpc_rawcombuf = NULL;
     96        1.1       cgd 
     97        1.1       cgd SVCXPRT *
     98       1.13      fvdl svc_raw_create()
     99        1.1       cgd {
    100       1.13      fvdl 	struct svc_raw_private *srp;
    101       1.13      fvdl /* VARIABLES PROTECTED BY svcraw_lock: svc_raw_private, srp */
    102        1.1       cgd 
    103       1.13      fvdl 	mutex_lock(&svcraw_lock);
    104       1.13      fvdl 	srp = svc_raw_private;
    105       1.13      fvdl 	if (srp == NULL) {
    106       1.20  christos 		srp = calloc(1, sizeof(*srp));
    107       1.20  christos 		if (srp == NULL)
    108       1.20  christos 			goto out;
    109       1.13      fvdl 		if (__rpc_rawcombuf == NULL)
    110       1.18      yamt 			__rpc_rawcombuf = malloc(UDPMSGSIZE);
    111       1.20  christos 		if (__rpc_rawcombuf == NULL)
    112       1.20  christos 			goto out;
    113       1.13      fvdl 		srp->raw_buf = __rpc_rawcombuf; /* Share it with the client */
    114       1.13      fvdl 		svc_raw_private = srp;
    115        1.1       cgd 	}
    116       1.13      fvdl 	srp->server.xp_fd = FD_SETSIZE;
    117        1.1       cgd 	srp->server.xp_port = 0;
    118       1.13      fvdl 	srp->server.xp_p3 = NULL;
    119       1.13      fvdl 	svc_raw_ops(&srp->server);
    120        1.1       cgd 	srp->server.xp_verf.oa_base = srp->verf_body;
    121       1.13      fvdl 	xdrmem_create(&srp->xdr_stream, srp->raw_buf, UDPMSGSIZE, XDR_DECODE);
    122       1.13      fvdl 	xprt_register(&srp->server);
    123       1.13      fvdl 	mutex_unlock(&svcraw_lock);
    124        1.1       cgd 	return (&srp->server);
    125       1.20  christos out:
    126       1.21  christos 	if (srp != NULL)
    127       1.21  christos 		free(srp);
    128       1.20  christos 	mutex_unlock(&svcraw_lock);
    129       1.20  christos 	return (NULL);
    130        1.1       cgd }
    131        1.1       cgd 
    132       1.14  christos /*ARGSUSED*/
    133        1.1       cgd static enum xprt_stat
    134       1.13      fvdl svc_raw_stat(xprt)
    135       1.13      fvdl SVCXPRT *xprt; /* args needed to satisfy ANSI-C typechecking */
    136        1.1       cgd {
    137        1.1       cgd 	return (XPRT_IDLE);
    138        1.1       cgd }
    139        1.1       cgd 
    140       1.14  christos /*ARGSUSED*/
    141        1.1       cgd static bool_t
    142       1.13      fvdl svc_raw_recv(xprt, msg)
    143        1.1       cgd 	SVCXPRT *xprt;
    144        1.1       cgd 	struct rpc_msg *msg;
    145        1.1       cgd {
    146       1.13      fvdl 	struct svc_raw_private *srp;
    147        1.8     lukem 	XDR *xdrs;
    148        1.1       cgd 
    149       1.13      fvdl 	mutex_lock(&svcraw_lock);
    150       1.13      fvdl 	srp = svc_raw_private;
    151       1.13      fvdl 	if (srp == NULL) {
    152       1.13      fvdl 		mutex_unlock(&svcraw_lock);
    153       1.13      fvdl 		return (FALSE);
    154       1.13      fvdl 	}
    155       1.13      fvdl 	mutex_unlock(&svcraw_lock);
    156       1.11     lukem 
    157        1.1       cgd 	xdrs = &srp->xdr_stream;
    158        1.1       cgd 	xdrs->x_op = XDR_DECODE;
    159       1.13      fvdl 	(void) XDR_SETPOS(xdrs, 0);
    160       1.13      fvdl 	if (! xdr_callmsg(xdrs, msg)) {
    161       1.13      fvdl 		return (FALSE);
    162       1.13      fvdl 	}
    163        1.1       cgd 	return (TRUE);
    164        1.1       cgd }
    165        1.1       cgd 
    166       1.14  christos /*ARGSUSED*/
    167        1.1       cgd static bool_t
    168       1.13      fvdl svc_raw_reply(xprt, msg)
    169        1.1       cgd 	SVCXPRT *xprt;
    170        1.1       cgd 	struct rpc_msg *msg;
    171        1.1       cgd {
    172       1.13      fvdl 	struct svc_raw_private *srp;
    173        1.8     lukem 	XDR *xdrs;
    174        1.1       cgd 
    175       1.13      fvdl 	mutex_lock(&svcraw_lock);
    176       1.13      fvdl 	srp = svc_raw_private;
    177       1.13      fvdl 	if (srp == NULL) {
    178       1.13      fvdl 		mutex_unlock(&svcraw_lock);
    179       1.13      fvdl 		return (FALSE);
    180       1.13      fvdl 	}
    181       1.13      fvdl 	mutex_unlock(&svcraw_lock);
    182       1.11     lukem 
    183        1.1       cgd 	xdrs = &srp->xdr_stream;
    184        1.1       cgd 	xdrs->x_op = XDR_ENCODE;
    185       1.13      fvdl 	(void) XDR_SETPOS(xdrs, 0);
    186       1.13      fvdl 	if (! xdr_replymsg(xdrs, msg)) {
    187       1.13      fvdl 		return (FALSE);
    188       1.13      fvdl 	}
    189       1.13      fvdl 	(void) XDR_GETPOS(xdrs);  /* called just for overhead */
    190        1.1       cgd 	return (TRUE);
    191        1.1       cgd }
    192        1.1       cgd 
    193       1.14  christos /*ARGSUSED*/
    194        1.1       cgd static bool_t
    195       1.13      fvdl svc_raw_getargs(xprt, xdr_args, args_ptr)
    196        1.1       cgd 	SVCXPRT *xprt;
    197        1.1       cgd 	xdrproc_t xdr_args;
    198        1.1       cgd 	caddr_t args_ptr;
    199        1.1       cgd {
    200       1.14  christos 	struct svc_raw_private *srp;
    201       1.11     lukem 
    202       1.13      fvdl 	mutex_lock(&svcraw_lock);
    203       1.13      fvdl 	srp = svc_raw_private;
    204       1.13      fvdl 	if (srp == NULL) {
    205       1.13      fvdl 		mutex_unlock(&svcraw_lock);
    206        1.1       cgd 		return (FALSE);
    207       1.13      fvdl 	}
    208       1.13      fvdl 	mutex_unlock(&svcraw_lock);
    209       1.13      fvdl 	return (*xdr_args)(&srp->xdr_stream, args_ptr);
    210        1.1       cgd }
    211        1.1       cgd 
    212       1.14  christos /*ARGSUSED*/
    213        1.1       cgd static bool_t
    214       1.13      fvdl svc_raw_freeargs(xprt, xdr_args, args_ptr)
    215        1.1       cgd 	SVCXPRT *xprt;
    216        1.1       cgd 	xdrproc_t xdr_args;
    217        1.1       cgd 	caddr_t args_ptr;
    218       1.13      fvdl {
    219       1.13      fvdl 	struct svc_raw_private *srp;
    220        1.8     lukem 	XDR *xdrs;
    221       1.11     lukem 
    222       1.13      fvdl 	mutex_lock(&svcraw_lock);
    223       1.13      fvdl 	srp = svc_raw_private;
    224       1.13      fvdl 	if (srp == NULL) {
    225       1.13      fvdl 		mutex_unlock(&svcraw_lock);
    226       1.13      fvdl 		return (FALSE);
    227       1.13      fvdl 	}
    228       1.13      fvdl 	mutex_unlock(&svcraw_lock);
    229        1.1       cgd 
    230        1.1       cgd 	xdrs = &srp->xdr_stream;
    231        1.1       cgd 	xdrs->x_op = XDR_FREE;
    232       1.13      fvdl 	return (*xdr_args)(xdrs, args_ptr);
    233       1.13      fvdl }
    234        1.1       cgd 
    235       1.14  christos /*ARGSUSED*/
    236        1.1       cgd static void
    237       1.13      fvdl svc_raw_destroy(xprt)
    238       1.13      fvdl SVCXPRT *xprt;
    239       1.13      fvdl {
    240       1.13      fvdl }
    241       1.13      fvdl 
    242       1.14  christos /*ARGSUSED*/
    243       1.13      fvdl static bool_t
    244       1.13      fvdl svc_raw_control(xprt, rq, in)
    245       1.13      fvdl 	SVCXPRT *xprt;
    246       1.13      fvdl 	const u_int	rq;
    247       1.13      fvdl 	void		*in;
    248       1.13      fvdl {
    249       1.13      fvdl 	return (FALSE);
    250       1.13      fvdl }
    251       1.13      fvdl 
    252       1.13      fvdl static void
    253       1.13      fvdl svc_raw_ops(xprt)
    254        1.4  christos 	SVCXPRT *xprt;
    255        1.1       cgd {
    256       1.13      fvdl 	static struct xp_ops ops;
    257       1.13      fvdl 	static struct xp_ops2 ops2;
    258       1.16   thorpej #ifdef _REENTRANT
    259       1.13      fvdl 	extern mutex_t ops_lock;
    260       1.13      fvdl #endif
    261       1.15     lukem 
    262       1.15     lukem 	_DIAGASSERT(xprt != NULL);
    263       1.13      fvdl 
    264       1.13      fvdl /* VARIABLES PROTECTED BY ops_lock: ops */
    265       1.13      fvdl 
    266       1.13      fvdl 	mutex_lock(&ops_lock);
    267       1.13      fvdl 	if (ops.xp_recv == NULL) {
    268       1.13      fvdl 		ops.xp_recv = svc_raw_recv;
    269       1.13      fvdl 		ops.xp_stat = svc_raw_stat;
    270       1.13      fvdl 		ops.xp_getargs = svc_raw_getargs;
    271       1.13      fvdl 		ops.xp_reply = svc_raw_reply;
    272       1.13      fvdl 		ops.xp_freeargs = svc_raw_freeargs;
    273       1.13      fvdl 		ops.xp_destroy = svc_raw_destroy;
    274       1.13      fvdl 		ops2.xp_control = svc_raw_control;
    275       1.13      fvdl 	}
    276       1.13      fvdl 	xprt->xp_ops = &ops;
    277       1.13      fvdl 	xprt->xp_ops2 = &ops2;
    278       1.13      fvdl 	mutex_unlock(&ops_lock);
    279        1.1       cgd }
    280