1 | /* |
2 | * svc_raw.c, This a toy for simple testing and timing. |
3 | * Interface to create an rpc client and server in the same UNIX process. |
4 | * This lets us simulate rpc and get rpc (round trip) overhead, without |
5 | * any interference from the kernel. |
6 | * |
7 | * Copyright (c) 2010, Oracle America, Inc. |
8 | * |
9 | * Redistribution and use in source and binary forms, with or without |
10 | * modification, are permitted provided that the following conditions are |
11 | * met: |
12 | * |
13 | * * Redistributions of source code must retain the above copyright |
14 | * notice, this list of conditions and the following disclaimer. |
15 | * * Redistributions in binary form must reproduce the above |
16 | * copyright notice, this list of conditions and the following |
17 | * disclaimer in the documentation and/or other materials |
18 | * provided with the distribution. |
19 | * * Neither the name of the "Oracle America, Inc." nor the names of its |
20 | * contributors may be used to endorse or promote products derived |
21 | * from this software without specific prior written permission. |
22 | * |
23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
26 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
27 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
28 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
30 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
31 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
32 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
33 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
34 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
35 | */ |
36 | |
37 | #include <rpc/rpc.h> |
38 | #include <rpc/svc.h> |
39 | #include <shlib-compat.h> |
40 | |
41 | /* |
42 | * This is the "network" that we will be moving data over |
43 | */ |
44 | struct svcraw_private_s |
45 | { |
46 | char _raw_buf[UDPMSGSIZE]; |
47 | SVCXPRT server; |
48 | XDR xdr_stream; |
49 | char verf_body[MAX_AUTH_BYTES]; |
50 | }; |
51 | #define svcraw_private RPC_THREAD_VARIABLE(svcraw_private_s) |
52 | |
53 | static bool_t svcraw_recv (SVCXPRT *, struct rpc_msg *); |
54 | static enum xprt_stat svcraw_stat (SVCXPRT *); |
55 | static bool_t svcraw_getargs (SVCXPRT *, xdrproc_t, caddr_t); |
56 | static bool_t svcraw_reply (SVCXPRT *, struct rpc_msg *); |
57 | static bool_t svcraw_freeargs (SVCXPRT *, xdrproc_t, caddr_t); |
58 | static void svcraw_destroy (SVCXPRT *); |
59 | |
60 | static const struct xp_ops server_ops = |
61 | { |
62 | svcraw_recv, |
63 | svcraw_stat, |
64 | svcraw_getargs, |
65 | svcraw_reply, |
66 | svcraw_freeargs, |
67 | svcraw_destroy |
68 | }; |
69 | |
70 | SVCXPRT * |
71 | svcraw_create (void) |
72 | { |
73 | struct svcraw_private_s *srp = svcraw_private; |
74 | |
75 | if (srp == 0) |
76 | { |
77 | srp = (struct svcraw_private_s *) calloc (nmemb: 1, size: sizeof (*srp)); |
78 | if (srp == 0) |
79 | return NULL; |
80 | } |
81 | srp->server.xp_sock = 0; |
82 | srp->server.xp_port = 0; |
83 | srp->server.xp_ops = (struct xp_ops *) &server_ops; |
84 | srp->server.xp_verf.oa_base = srp->verf_body; |
85 | xdrmem_create (&srp->xdr_stream, srp->_raw_buf, UDPMSGSIZE, XDR_FREE); |
86 | return &srp->server; |
87 | } |
88 | libc_hidden_nolink_sunrpc (svcraw_create, GLIBC_2_0) |
89 | |
90 | static enum xprt_stat |
91 | svcraw_stat (SVCXPRT *xprt) |
92 | { |
93 | return XPRT_IDLE; |
94 | } |
95 | |
96 | static bool_t |
97 | svcraw_recv (SVCXPRT *xprt, struct rpc_msg *msg) |
98 | { |
99 | struct svcraw_private_s *srp = svcraw_private; |
100 | XDR *xdrs; |
101 | |
102 | if (srp == 0) |
103 | return FALSE; |
104 | xdrs = &srp->xdr_stream; |
105 | xdrs->x_op = XDR_DECODE; |
106 | XDR_SETPOS (xdrs, 0); |
107 | if (!xdr_callmsg (xdrs, msg)) |
108 | return FALSE; |
109 | return TRUE; |
110 | } |
111 | |
112 | static bool_t |
113 | svcraw_reply (SVCXPRT *xprt, struct rpc_msg *msg) |
114 | { |
115 | struct svcraw_private_s *srp = svcraw_private; |
116 | XDR *xdrs; |
117 | |
118 | if (srp == 0) |
119 | return FALSE; |
120 | xdrs = &srp->xdr_stream; |
121 | xdrs->x_op = XDR_ENCODE; |
122 | XDR_SETPOS (xdrs, 0); |
123 | if (!xdr_replymsg (xdrs, msg)) |
124 | return FALSE; |
125 | (void) XDR_GETPOS (xdrs); /* called just for overhead */ |
126 | return TRUE; |
127 | } |
128 | |
129 | static bool_t |
130 | svcraw_getargs (SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr) |
131 | { |
132 | struct svcraw_private_s *srp = svcraw_private; |
133 | |
134 | if (srp == 0) |
135 | return FALSE; |
136 | return (*xdr_args) (&srp->xdr_stream, args_ptr); |
137 | } |
138 | |
139 | static bool_t |
140 | svcraw_freeargs (SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr) |
141 | { |
142 | struct svcraw_private_s *srp = svcraw_private; |
143 | XDR *xdrs; |
144 | |
145 | if (srp == 0) |
146 | return FALSE; |
147 | xdrs = &srp->xdr_stream; |
148 | xdrs->x_op = XDR_FREE; |
149 | return (*xdr_args) (xdrs, args_ptr); |
150 | } |
151 | |
152 | static void |
153 | svcraw_destroy (SVCXPRT *xprt) |
154 | { |
155 | } |
156 | |