Home | History | Annotate | Line # | Download | only in rpc
svc_raw.c revision 1.25
      1  1.25  christos /*	$NetBSD: svc_raw.c,v 1.25 2015/11/06 19:32:08 christos Exp $	*/
      2   1.3       cgd 
      3   1.1       cgd /*
      4  1.24      tron  * Copyright (c) 2010, Oracle America, Inc.
      5  1.24      tron  *
      6  1.24      tron  * Redistribution and use in source and binary forms, with or without
      7  1.24      tron  * modification, are permitted provided that the following conditions are
      8  1.24      tron  * met:
      9  1.24      tron  *
     10  1.24      tron  *     * Redistributions of source code must retain the above copyright
     11  1.24      tron  *       notice, this list of conditions and the following disclaimer.
     12  1.24      tron  *     * Redistributions in binary form must reproduce the above
     13  1.24      tron  *       copyright notice, this list of conditions and the following
     14  1.24      tron  *       disclaimer in the documentation and/or other materials
     15  1.24      tron  *       provided with the distribution.
     16  1.24      tron  *     * Neither the name of the "Oracle America, Inc." nor the names of its
     17  1.24      tron  *       contributors may be used to endorse or promote products derived
     18  1.24      tron  *       from this software without specific prior written permission.
     19  1.24      tron  *
     20  1.24      tron  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     21  1.24      tron  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     22  1.24      tron  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     23  1.24      tron  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
     24  1.24      tron  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     25  1.24      tron  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.24      tron  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     27  1.24      tron  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  1.24      tron  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     29  1.24      tron  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     30  1.24      tron  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     31  1.24      tron  *   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.25  christos __RCSID("$NetBSD: svc_raw.c,v 1.25 2015/11/06 19:32:08 christos 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.22      matt static enum xprt_stat svc_raw_stat(SVCXPRT *);
     87  1.22      matt static bool_t svc_raw_recv(SVCXPRT *, struct rpc_msg *);
     88  1.22      matt static bool_t svc_raw_reply(SVCXPRT *, struct rpc_msg *);
     89  1.22      matt static bool_t svc_raw_getargs(SVCXPRT *, xdrproc_t, caddr_t);
     90  1.22      matt static bool_t svc_raw_freeargs(SVCXPRT *, xdrproc_t, caddr_t);
     91  1.22      matt static void svc_raw_destroy(SVCXPRT *);
     92  1.22      matt static void svc_raw_ops(SVCXPRT *);
     93  1.22      matt static bool_t svc_raw_control(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.22      matt svc_raw_create(void)
     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.25  christos 	srp->server.xp_fd = -1;
    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.23  christos 	if (!xprt_register(&srp->server))
    123  1.23  christos 		goto out;
    124  1.13      fvdl 	mutex_unlock(&svcraw_lock);
    125   1.1       cgd 	return (&srp->server);
    126  1.20  christos out:
    127  1.21  christos 	if (srp != NULL)
    128  1.21  christos 		free(srp);
    129  1.20  christos 	mutex_unlock(&svcraw_lock);
    130  1.20  christos 	return (NULL);
    131   1.1       cgd }
    132   1.1       cgd 
    133  1.14  christos /*ARGSUSED*/
    134   1.1       cgd static enum xprt_stat
    135  1.22      matt svc_raw_stat(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.22      matt svc_raw_recv(SVCXPRT *xprt, struct rpc_msg *msg)
    143   1.1       cgd {
    144  1.13      fvdl 	struct svc_raw_private *srp;
    145   1.8     lukem 	XDR *xdrs;
    146   1.1       cgd 
    147  1.13      fvdl 	mutex_lock(&svcraw_lock);
    148  1.13      fvdl 	srp = svc_raw_private;
    149  1.13      fvdl 	if (srp == NULL) {
    150  1.13      fvdl 		mutex_unlock(&svcraw_lock);
    151  1.13      fvdl 		return (FALSE);
    152  1.13      fvdl 	}
    153  1.13      fvdl 	mutex_unlock(&svcraw_lock);
    154  1.11     lukem 
    155   1.1       cgd 	xdrs = &srp->xdr_stream;
    156   1.1       cgd 	xdrs->x_op = XDR_DECODE;
    157  1.13      fvdl 	(void) XDR_SETPOS(xdrs, 0);
    158  1.13      fvdl 	if (! xdr_callmsg(xdrs, msg)) {
    159  1.13      fvdl 		return (FALSE);
    160  1.13      fvdl 	}
    161   1.1       cgd 	return (TRUE);
    162   1.1       cgd }
    163   1.1       cgd 
    164  1.14  christos /*ARGSUSED*/
    165   1.1       cgd static bool_t
    166  1.22      matt svc_raw_reply(SVCXPRT *xprt, struct rpc_msg *msg)
    167   1.1       cgd {
    168  1.13      fvdl 	struct svc_raw_private *srp;
    169   1.8     lukem 	XDR *xdrs;
    170   1.1       cgd 
    171  1.13      fvdl 	mutex_lock(&svcraw_lock);
    172  1.13      fvdl 	srp = svc_raw_private;
    173  1.13      fvdl 	if (srp == NULL) {
    174  1.13      fvdl 		mutex_unlock(&svcraw_lock);
    175  1.13      fvdl 		return (FALSE);
    176  1.13      fvdl 	}
    177  1.13      fvdl 	mutex_unlock(&svcraw_lock);
    178  1.11     lukem 
    179   1.1       cgd 	xdrs = &srp->xdr_stream;
    180   1.1       cgd 	xdrs->x_op = XDR_ENCODE;
    181  1.13      fvdl 	(void) XDR_SETPOS(xdrs, 0);
    182  1.13      fvdl 	if (! xdr_replymsg(xdrs, msg)) {
    183  1.13      fvdl 		return (FALSE);
    184  1.13      fvdl 	}
    185  1.13      fvdl 	(void) XDR_GETPOS(xdrs);  /* called just for overhead */
    186   1.1       cgd 	return (TRUE);
    187   1.1       cgd }
    188   1.1       cgd 
    189  1.14  christos /*ARGSUSED*/
    190   1.1       cgd static bool_t
    191  1.22      matt svc_raw_getargs(SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr)
    192   1.1       cgd {
    193  1.14  christos 	struct svc_raw_private *srp;
    194  1.11     lukem 
    195  1.13      fvdl 	mutex_lock(&svcraw_lock);
    196  1.13      fvdl 	srp = svc_raw_private;
    197  1.13      fvdl 	if (srp == NULL) {
    198  1.13      fvdl 		mutex_unlock(&svcraw_lock);
    199   1.1       cgd 		return (FALSE);
    200  1.13      fvdl 	}
    201  1.13      fvdl 	mutex_unlock(&svcraw_lock);
    202  1.13      fvdl 	return (*xdr_args)(&srp->xdr_stream, args_ptr);
    203   1.1       cgd }
    204   1.1       cgd 
    205  1.14  christos /*ARGSUSED*/
    206   1.1       cgd static bool_t
    207  1.22      matt svc_raw_freeargs(SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr)
    208  1.13      fvdl {
    209  1.13      fvdl 	struct svc_raw_private *srp;
    210   1.8     lukem 	XDR *xdrs;
    211  1.11     lukem 
    212  1.13      fvdl 	mutex_lock(&svcraw_lock);
    213  1.13      fvdl 	srp = svc_raw_private;
    214  1.13      fvdl 	if (srp == NULL) {
    215  1.13      fvdl 		mutex_unlock(&svcraw_lock);
    216  1.13      fvdl 		return (FALSE);
    217  1.13      fvdl 	}
    218  1.13      fvdl 	mutex_unlock(&svcraw_lock);
    219   1.1       cgd 
    220   1.1       cgd 	xdrs = &srp->xdr_stream;
    221   1.1       cgd 	xdrs->x_op = XDR_FREE;
    222  1.13      fvdl 	return (*xdr_args)(xdrs, args_ptr);
    223  1.13      fvdl }
    224   1.1       cgd 
    225  1.14  christos /*ARGSUSED*/
    226   1.1       cgd static void
    227  1.22      matt svc_raw_destroy(SVCXPRT *xprt)
    228  1.13      fvdl {
    229  1.13      fvdl }
    230  1.13      fvdl 
    231  1.14  christos /*ARGSUSED*/
    232  1.13      fvdl static bool_t
    233  1.22      matt svc_raw_control(SVCXPRT *xprt, const u_int rq, void *in)
    234  1.13      fvdl {
    235  1.13      fvdl 	return (FALSE);
    236  1.13      fvdl }
    237  1.13      fvdl 
    238  1.13      fvdl static void
    239  1.22      matt svc_raw_ops(SVCXPRT *xprt)
    240   1.1       cgd {
    241  1.13      fvdl 	static struct xp_ops ops;
    242  1.13      fvdl 	static struct xp_ops2 ops2;
    243  1.16   thorpej #ifdef _REENTRANT
    244  1.13      fvdl 	extern mutex_t ops_lock;
    245  1.13      fvdl #endif
    246  1.15     lukem 
    247  1.15     lukem 	_DIAGASSERT(xprt != NULL);
    248  1.13      fvdl 
    249  1.13      fvdl /* VARIABLES PROTECTED BY ops_lock: ops */
    250  1.13      fvdl 
    251  1.13      fvdl 	mutex_lock(&ops_lock);
    252  1.13      fvdl 	if (ops.xp_recv == NULL) {
    253  1.13      fvdl 		ops.xp_recv = svc_raw_recv;
    254  1.13      fvdl 		ops.xp_stat = svc_raw_stat;
    255  1.13      fvdl 		ops.xp_getargs = svc_raw_getargs;
    256  1.13      fvdl 		ops.xp_reply = svc_raw_reply;
    257  1.13      fvdl 		ops.xp_freeargs = svc_raw_freeargs;
    258  1.13      fvdl 		ops.xp_destroy = svc_raw_destroy;
    259  1.13      fvdl 		ops2.xp_control = svc_raw_control;
    260  1.13      fvdl 	}
    261  1.13      fvdl 	xprt->xp_ops = &ops;
    262  1.13      fvdl 	xprt->xp_ops2 = &ops2;
    263  1.13      fvdl 	mutex_unlock(&ops_lock);
    264   1.1       cgd }
    265