1ba96d07fShrs /*-
2ba96d07fShrs * Copyright (c) 2009, Sun Microsystems, Inc.
3ba96d07fShrs * All rights reserved.
4ce0e08e2SPeter Avalos *
5ba96d07fShrs * Redistribution and use in source and binary forms, with or without
6ba96d07fShrs * modification, are permitted provided that the following conditions are met:
7ba96d07fShrs * - Redistributions of source code must retain the above copyright notice,
8ba96d07fShrs * this list of conditions and the following disclaimer.
9ba96d07fShrs * - Redistributions in binary form must reproduce the above copyright notice,
10ba96d07fShrs * this list of conditions and the following disclaimer in the documentation
11ba96d07fShrs * and/or other materials provided with the distribution.
12ba96d07fShrs * - Neither the name of Sun Microsystems, Inc. nor the names of its
13ba96d07fShrs * contributors may be used to endorse or promote products derived
14ba96d07fShrs * from this software without specific prior written permission.
15ce0e08e2SPeter Avalos *
16ba96d07fShrs * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17ba96d07fShrs * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18ba96d07fShrs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19ba96d07fShrs * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20ba96d07fShrs * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21ba96d07fShrs * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22ba96d07fShrs * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23ba96d07fShrs * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24ba96d07fShrs * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25ba96d07fShrs * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26ba96d07fShrs * POSSIBILITY OF SUCH DAMAGE.
27ce0e08e2SPeter Avalos *
28ce0e08e2SPeter Avalos * @(#)svc_dg.c 1.17 94/04/24 SMI
29ce0e08e2SPeter Avalos * $NetBSD: svc_dg.c,v 1.4 2000/07/06 03:10:35 christos Exp $
30ce0e08e2SPeter Avalos * $FreeBSD: src/lib/libc/rpc/svc_dg.c,v 1.8 2006/02/27 22:10:59 deischen Exp $
31ce0e08e2SPeter Avalos */
32ce0e08e2SPeter Avalos
33ce0e08e2SPeter Avalos /*
34ce0e08e2SPeter Avalos * Copyright (c) 1986-1991 by Sun Microsystems Inc.
35ce0e08e2SPeter Avalos */
36ce0e08e2SPeter Avalos
37ce0e08e2SPeter Avalos /*
38ce0e08e2SPeter Avalos * svc_dg.c, Server side for connectionless RPC.
39ce0e08e2SPeter Avalos *
40ce0e08e2SPeter Avalos * Does some caching in the hopes of achieving execute-at-most-once semantics.
41ce0e08e2SPeter Avalos */
42ce0e08e2SPeter Avalos
43ce0e08e2SPeter Avalos #include "namespace.h"
44ce0e08e2SPeter Avalos #include "reentrant.h"
45*538de33eSSascha Wildner #include <sys/param.h>
46ce0e08e2SPeter Avalos #include <sys/socket.h>
47ce0e08e2SPeter Avalos #include <rpc/rpc.h>
48ce0e08e2SPeter Avalos #include <rpc/svc_dg.h>
49ce0e08e2SPeter Avalos #include <errno.h>
50ce0e08e2SPeter Avalos #include <unistd.h>
51ce0e08e2SPeter Avalos #include <stdio.h>
52ce0e08e2SPeter Avalos #include <stdlib.h>
53ce0e08e2SPeter Avalos #include <string.h>
54ce0e08e2SPeter Avalos #ifdef RPC_CACHE_DEBUG
55ce0e08e2SPeter Avalos #include <netconfig.h>
56ce0e08e2SPeter Avalos #include <netdir.h>
57ce0e08e2SPeter Avalos #endif
58ce0e08e2SPeter Avalos #include <err.h>
59ce0e08e2SPeter Avalos #include "un-namespace.h"
60ce0e08e2SPeter Avalos
61ce0e08e2SPeter Avalos #include "rpc_com.h"
62ce0e08e2SPeter Avalos #include "mt_misc.h"
63ce0e08e2SPeter Avalos
64ce0e08e2SPeter Avalos #define su_data(xprt) ((struct svc_dg_data *)(xprt->xp_p2))
65ce0e08e2SPeter Avalos #define rpc_buffer(xprt) ((xprt)->xp_p1)
66ce0e08e2SPeter Avalos
67ce0e08e2SPeter Avalos #ifndef MAX
68ce0e08e2SPeter Avalos #define MAX(a, b) (((a) > (b)) ? (a) : (b))
69ce0e08e2SPeter Avalos #endif
70ce0e08e2SPeter Avalos
71ce0e08e2SPeter Avalos static void svc_dg_ops(SVCXPRT *);
72ce0e08e2SPeter Avalos static enum xprt_stat svc_dg_stat(SVCXPRT *);
73ce0e08e2SPeter Avalos static bool_t svc_dg_recv(SVCXPRT *, struct rpc_msg *);
74ce0e08e2SPeter Avalos static bool_t svc_dg_reply(SVCXPRT *, struct rpc_msg *);
75ce0e08e2SPeter Avalos static bool_t svc_dg_getargs(SVCXPRT *, xdrproc_t, void *);
76ce0e08e2SPeter Avalos static bool_t svc_dg_freeargs(SVCXPRT *, xdrproc_t, void *);
77ce0e08e2SPeter Avalos static void svc_dg_destroy(SVCXPRT *);
78ce0e08e2SPeter Avalos static bool_t svc_dg_control(SVCXPRT *, const u_int, void *);
79ce0e08e2SPeter Avalos static int cache_get(SVCXPRT *, struct rpc_msg *, char **, size_t *);
80ce0e08e2SPeter Avalos static void cache_set(SVCXPRT *, size_t);
81ce0e08e2SPeter Avalos int svc_dg_enablecache(SVCXPRT *, u_int);
82ce0e08e2SPeter Avalos
83ce0e08e2SPeter Avalos /*
84ce0e08e2SPeter Avalos * Usage:
85ce0e08e2SPeter Avalos * xprt = svc_dg_create(sock, sendsize, recvsize);
86ce0e08e2SPeter Avalos * Does other connectionless specific initializations.
87ce0e08e2SPeter Avalos * Once *xprt is initialized, it is registered.
88ce0e08e2SPeter Avalos * see (svc.h, xprt_register). If recvsize or sendsize are 0 suitable
89ce0e08e2SPeter Avalos * system defaults are chosen.
90ce0e08e2SPeter Avalos * The routines returns NULL if a problem occurred.
91ce0e08e2SPeter Avalos */
92ce0e08e2SPeter Avalos static const char svc_dg_str[] = "svc_dg_create: %s";
93ce0e08e2SPeter Avalos static const char svc_dg_err1[] = "could not get transport information";
94ce0e08e2SPeter Avalos static const char svc_dg_err2[] = " transport does not support data transfer";
95ce0e08e2SPeter Avalos static const char __no_mem_str[] = "out of memory";
96ce0e08e2SPeter Avalos
97ce0e08e2SPeter Avalos SVCXPRT *
svc_dg_create(int fd,u_int sendsize,u_int recvsize)98ce0e08e2SPeter Avalos svc_dg_create(int fd, u_int sendsize, u_int recvsize)
99ce0e08e2SPeter Avalos {
100ce0e08e2SPeter Avalos SVCXPRT *xprt;
101ce0e08e2SPeter Avalos struct svc_dg_data *su = NULL;
102ce0e08e2SPeter Avalos struct __rpc_sockinfo si;
103ce0e08e2SPeter Avalos struct sockaddr_storage ss;
104ce0e08e2SPeter Avalos socklen_t slen;
105ce0e08e2SPeter Avalos
106ce0e08e2SPeter Avalos if (!__rpc_fd2sockinfo(fd, &si)) {
107ce0e08e2SPeter Avalos warnx(svc_dg_str, svc_dg_err1);
108ce0e08e2SPeter Avalos return (NULL);
109ce0e08e2SPeter Avalos }
110ce0e08e2SPeter Avalos /*
111ce0e08e2SPeter Avalos * Find the receive and the send size
112ce0e08e2SPeter Avalos */
113ce0e08e2SPeter Avalos sendsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsize);
114ce0e08e2SPeter Avalos recvsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsize);
115ce0e08e2SPeter Avalos if ((sendsize == 0) || (recvsize == 0)) {
116ce0e08e2SPeter Avalos warnx(svc_dg_str, svc_dg_err2);
117ce0e08e2SPeter Avalos return (NULL);
118ce0e08e2SPeter Avalos }
119ce0e08e2SPeter Avalos
120ce0e08e2SPeter Avalos xprt = mem_alloc(sizeof (SVCXPRT));
121ce0e08e2SPeter Avalos if (xprt == NULL)
122ce0e08e2SPeter Avalos goto freedata;
123ce0e08e2SPeter Avalos memset(xprt, 0, sizeof (SVCXPRT));
124ce0e08e2SPeter Avalos
125ce0e08e2SPeter Avalos su = mem_alloc(sizeof (*su));
126ce0e08e2SPeter Avalos if (su == NULL)
127ce0e08e2SPeter Avalos goto freedata;
128ed183f8cSSascha Wildner su->su_iosz = rounddown(MAX(sendsize, recvsize) + 3, 4);
129ce0e08e2SPeter Avalos if ((rpc_buffer(xprt) = mem_alloc(su->su_iosz)) == NULL)
130ce0e08e2SPeter Avalos goto freedata;
131ce0e08e2SPeter Avalos xdrmem_create(&(su->su_xdrs), rpc_buffer(xprt), su->su_iosz,
132ce0e08e2SPeter Avalos XDR_DECODE);
133ce0e08e2SPeter Avalos su->su_cache = NULL;
134ce0e08e2SPeter Avalos xprt->xp_fd = fd;
135ce0e08e2SPeter Avalos xprt->xp_p2 = su;
136ce0e08e2SPeter Avalos xprt->xp_verf.oa_base = su->su_verfbody;
137ce0e08e2SPeter Avalos svc_dg_ops(xprt);
138ce0e08e2SPeter Avalos xprt->xp_rtaddr.maxlen = sizeof (struct sockaddr_storage);
139ce0e08e2SPeter Avalos
140ce0e08e2SPeter Avalos slen = sizeof ss;
141ce0e08e2SPeter Avalos if (_getsockname(fd, (struct sockaddr *)(void *)&ss, &slen) < 0)
142ce0e08e2SPeter Avalos goto freedata;
143ce0e08e2SPeter Avalos xprt->xp_ltaddr.buf = mem_alloc(sizeof (struct sockaddr_storage));
144ce0e08e2SPeter Avalos xprt->xp_ltaddr.maxlen = sizeof (struct sockaddr_storage);
145ce0e08e2SPeter Avalos xprt->xp_ltaddr.len = slen;
146ce0e08e2SPeter Avalos memcpy(xprt->xp_ltaddr.buf, &ss, slen);
147ce0e08e2SPeter Avalos
148ce0e08e2SPeter Avalos xprt_register(xprt);
149ce0e08e2SPeter Avalos return (xprt);
150ce0e08e2SPeter Avalos freedata:
151ce0e08e2SPeter Avalos warnx(svc_dg_str, __no_mem_str);
152ce0e08e2SPeter Avalos if (xprt) {
153ce0e08e2SPeter Avalos if (su)
154ce0e08e2SPeter Avalos mem_free(su, sizeof (*su));
155ce0e08e2SPeter Avalos mem_free(xprt, sizeof (SVCXPRT));
156ce0e08e2SPeter Avalos }
157ce0e08e2SPeter Avalos return (NULL);
158ce0e08e2SPeter Avalos }
159ce0e08e2SPeter Avalos
160ce0e08e2SPeter Avalos /*ARGSUSED*/
161ce0e08e2SPeter Avalos static enum xprt_stat
svc_dg_stat(SVCXPRT * xprt __unused)1626d7019e6SSascha Wildner svc_dg_stat(SVCXPRT *xprt __unused)
163ce0e08e2SPeter Avalos {
164ce0e08e2SPeter Avalos return (XPRT_IDLE);
165ce0e08e2SPeter Avalos }
166ce0e08e2SPeter Avalos
167ce0e08e2SPeter Avalos static bool_t
svc_dg_recv(SVCXPRT * xprt,struct rpc_msg * msg)168ce0e08e2SPeter Avalos svc_dg_recv(SVCXPRT *xprt, struct rpc_msg *msg)
169ce0e08e2SPeter Avalos {
170ce0e08e2SPeter Avalos struct svc_dg_data *su = su_data(xprt);
171ce0e08e2SPeter Avalos XDR *xdrs = &(su->su_xdrs);
172ce0e08e2SPeter Avalos char *reply;
173ce0e08e2SPeter Avalos struct sockaddr_storage ss;
174ce0e08e2SPeter Avalos socklen_t alen;
175ce0e08e2SPeter Avalos size_t replylen;
176ce0e08e2SPeter Avalos ssize_t rlen;
177ce0e08e2SPeter Avalos
178ce0e08e2SPeter Avalos again:
179ce0e08e2SPeter Avalos alen = sizeof (struct sockaddr_storage);
180ce0e08e2SPeter Avalos rlen = _recvfrom(xprt->xp_fd, rpc_buffer(xprt), su->su_iosz, 0,
181ce0e08e2SPeter Avalos (struct sockaddr *)(void *)&ss, &alen);
182ce0e08e2SPeter Avalos if (rlen == -1 && errno == EINTR)
183ce0e08e2SPeter Avalos goto again;
184ce0e08e2SPeter Avalos if (rlen == -1 || (rlen < (ssize_t)(4 * sizeof (u_int32_t))))
185ce0e08e2SPeter Avalos return (FALSE);
186ce0e08e2SPeter Avalos if (xprt->xp_rtaddr.len < alen) {
187ce0e08e2SPeter Avalos if (xprt->xp_rtaddr.len != 0)
188ce0e08e2SPeter Avalos mem_free(xprt->xp_rtaddr.buf, xprt->xp_rtaddr.len);
189ce0e08e2SPeter Avalos xprt->xp_rtaddr.buf = mem_alloc(alen);
190ce0e08e2SPeter Avalos xprt->xp_rtaddr.len = alen;
191ce0e08e2SPeter Avalos }
192ce0e08e2SPeter Avalos memcpy(xprt->xp_rtaddr.buf, &ss, alen);
193ce0e08e2SPeter Avalos #ifdef PORTMAP
194ce0e08e2SPeter Avalos if (ss.ss_family == AF_INET) {
195ce0e08e2SPeter Avalos xprt->xp_raddr = *(struct sockaddr_in *)xprt->xp_rtaddr.buf;
196ce0e08e2SPeter Avalos xprt->xp_addrlen = sizeof (struct sockaddr_in);
197ce0e08e2SPeter Avalos }
198ce0e08e2SPeter Avalos #endif /* PORTMAP */
199ce0e08e2SPeter Avalos xdrs->x_op = XDR_DECODE;
200ce0e08e2SPeter Avalos XDR_SETPOS(xdrs, 0);
201ce0e08e2SPeter Avalos if (! xdr_callmsg(xdrs, msg)) {
202ce0e08e2SPeter Avalos return (FALSE);
203ce0e08e2SPeter Avalos }
204ce0e08e2SPeter Avalos su->su_xid = msg->rm_xid;
205ce0e08e2SPeter Avalos if (su->su_cache != NULL) {
206ce0e08e2SPeter Avalos if (cache_get(xprt, msg, &reply, &replylen)) {
207ce0e08e2SPeter Avalos _sendto(xprt->xp_fd, reply, replylen, 0,
208ce0e08e2SPeter Avalos (struct sockaddr *)(void *)&ss, alen);
209ce0e08e2SPeter Avalos return (FALSE);
210ce0e08e2SPeter Avalos }
211ce0e08e2SPeter Avalos }
212ce0e08e2SPeter Avalos return (TRUE);
213ce0e08e2SPeter Avalos }
214ce0e08e2SPeter Avalos
215ce0e08e2SPeter Avalos static bool_t
svc_dg_reply(SVCXPRT * xprt,struct rpc_msg * msg)216ce0e08e2SPeter Avalos svc_dg_reply(SVCXPRT *xprt, struct rpc_msg *msg)
217ce0e08e2SPeter Avalos {
218ce0e08e2SPeter Avalos struct svc_dg_data *su = su_data(xprt);
219ce0e08e2SPeter Avalos XDR *xdrs = &(su->su_xdrs);
220ce0e08e2SPeter Avalos bool_t stat = FALSE;
221ce0e08e2SPeter Avalos size_t slen;
222ce0e08e2SPeter Avalos
223ce0e08e2SPeter Avalos xdrs->x_op = XDR_ENCODE;
224ce0e08e2SPeter Avalos XDR_SETPOS(xdrs, 0);
225ce0e08e2SPeter Avalos msg->rm_xid = su->su_xid;
226ce0e08e2SPeter Avalos if (xdr_replymsg(xdrs, msg)) {
227ce0e08e2SPeter Avalos slen = XDR_GETPOS(xdrs);
228ce0e08e2SPeter Avalos if (_sendto(xprt->xp_fd, rpc_buffer(xprt), slen, 0,
229ce0e08e2SPeter Avalos (struct sockaddr *)xprt->xp_rtaddr.buf,
230ce0e08e2SPeter Avalos (socklen_t)xprt->xp_rtaddr.len) == (ssize_t) slen) {
231ce0e08e2SPeter Avalos stat = TRUE;
232ce0e08e2SPeter Avalos if (su->su_cache)
233ce0e08e2SPeter Avalos cache_set(xprt, slen);
234ce0e08e2SPeter Avalos }
235ce0e08e2SPeter Avalos }
236ce0e08e2SPeter Avalos return (stat);
237ce0e08e2SPeter Avalos }
238ce0e08e2SPeter Avalos
239ce0e08e2SPeter Avalos static bool_t
svc_dg_getargs(SVCXPRT * xprt,xdrproc_t xdr_args,void * args_ptr)240ce0e08e2SPeter Avalos svc_dg_getargs(SVCXPRT *xprt, xdrproc_t xdr_args, void *args_ptr)
241ce0e08e2SPeter Avalos {
242ce0e08e2SPeter Avalos return (*xdr_args)(&(su_data(xprt)->su_xdrs), args_ptr);
243ce0e08e2SPeter Avalos }
244ce0e08e2SPeter Avalos
245ce0e08e2SPeter Avalos static bool_t
svc_dg_freeargs(SVCXPRT * xprt,xdrproc_t xdr_args,void * args_ptr)246ce0e08e2SPeter Avalos svc_dg_freeargs(SVCXPRT *xprt, xdrproc_t xdr_args, void *args_ptr)
247ce0e08e2SPeter Avalos {
248ce0e08e2SPeter Avalos XDR *xdrs = &(su_data(xprt)->su_xdrs);
249ce0e08e2SPeter Avalos
250ce0e08e2SPeter Avalos xdrs->x_op = XDR_FREE;
251ce0e08e2SPeter Avalos return (*xdr_args)(xdrs, args_ptr);
252ce0e08e2SPeter Avalos }
253ce0e08e2SPeter Avalos
254ce0e08e2SPeter Avalos static void
svc_dg_destroy(SVCXPRT * xprt)255ce0e08e2SPeter Avalos svc_dg_destroy(SVCXPRT *xprt)
256ce0e08e2SPeter Avalos {
257ce0e08e2SPeter Avalos struct svc_dg_data *su = su_data(xprt);
258ce0e08e2SPeter Avalos
259ce0e08e2SPeter Avalos xprt_unregister(xprt);
260ce0e08e2SPeter Avalos if (xprt->xp_fd != -1)
261ce0e08e2SPeter Avalos _close(xprt->xp_fd);
262ce0e08e2SPeter Avalos XDR_DESTROY(&(su->su_xdrs));
263ce0e08e2SPeter Avalos mem_free(rpc_buffer(xprt), su->su_iosz);
264ce0e08e2SPeter Avalos mem_free(su, sizeof (*su));
265ce0e08e2SPeter Avalos if (xprt->xp_rtaddr.buf)
266ce0e08e2SPeter Avalos mem_free(xprt->xp_rtaddr.buf, xprt->xp_rtaddr.maxlen);
267ce0e08e2SPeter Avalos if (xprt->xp_ltaddr.buf)
268ce0e08e2SPeter Avalos mem_free(xprt->xp_ltaddr.buf, xprt->xp_ltaddr.maxlen);
269ce0e08e2SPeter Avalos if (xprt->xp_tp)
270ce0e08e2SPeter Avalos free(xprt->xp_tp);
271ce0e08e2SPeter Avalos mem_free(xprt, sizeof (SVCXPRT));
272ce0e08e2SPeter Avalos }
273ce0e08e2SPeter Avalos
274ce0e08e2SPeter Avalos static bool_t
275ce0e08e2SPeter Avalos /*ARGSUSED*/
svc_dg_control(SVCXPRT * xprt __unused,const u_int rq __unused,void * in __unused)2766d7019e6SSascha Wildner svc_dg_control(SVCXPRT *xprt __unused, const u_int rq __unused,
2776d7019e6SSascha Wildner void *in __unused)
278ce0e08e2SPeter Avalos {
279ce0e08e2SPeter Avalos return (FALSE);
280ce0e08e2SPeter Avalos }
281ce0e08e2SPeter Avalos
282ce0e08e2SPeter Avalos static void
svc_dg_ops(SVCXPRT * xprt)283ce0e08e2SPeter Avalos svc_dg_ops(SVCXPRT *xprt)
284ce0e08e2SPeter Avalos {
285ce0e08e2SPeter Avalos static struct xp_ops ops;
286ce0e08e2SPeter Avalos static struct xp_ops2 ops2;
287ce0e08e2SPeter Avalos
288ce0e08e2SPeter Avalos /* VARIABLES PROTECTED BY ops_lock: ops */
289ce0e08e2SPeter Avalos
290ce0e08e2SPeter Avalos mutex_lock(&ops_lock);
291ce0e08e2SPeter Avalos if (ops.xp_recv == NULL) {
292ce0e08e2SPeter Avalos ops.xp_recv = svc_dg_recv;
293ce0e08e2SPeter Avalos ops.xp_stat = svc_dg_stat;
294ce0e08e2SPeter Avalos ops.xp_getargs = svc_dg_getargs;
295ce0e08e2SPeter Avalos ops.xp_reply = svc_dg_reply;
296ce0e08e2SPeter Avalos ops.xp_freeargs = svc_dg_freeargs;
297ce0e08e2SPeter Avalos ops.xp_destroy = svc_dg_destroy;
298ce0e08e2SPeter Avalos ops2.xp_control = svc_dg_control;
299ce0e08e2SPeter Avalos }
300ce0e08e2SPeter Avalos xprt->xp_ops = &ops;
301ce0e08e2SPeter Avalos xprt->xp_ops2 = &ops2;
302ce0e08e2SPeter Avalos mutex_unlock(&ops_lock);
303ce0e08e2SPeter Avalos }
304ce0e08e2SPeter Avalos
305ce0e08e2SPeter Avalos /* The CACHING COMPONENT */
306ce0e08e2SPeter Avalos
307ce0e08e2SPeter Avalos /*
308ce0e08e2SPeter Avalos * Could have been a separate file, but some part of it depends upon the
309ce0e08e2SPeter Avalos * private structure of the client handle.
310ce0e08e2SPeter Avalos *
311ce0e08e2SPeter Avalos * Fifo cache for cl server
312ce0e08e2SPeter Avalos * Copies pointers to reply buffers into fifo cache
313ce0e08e2SPeter Avalos * Buffers are sent again if retransmissions are detected.
314ce0e08e2SPeter Avalos */
315ce0e08e2SPeter Avalos
316ce0e08e2SPeter Avalos #define SPARSENESS 4 /* 75% sparse */
317ce0e08e2SPeter Avalos
318ce0e08e2SPeter Avalos #define ALLOC(type, size) \
319ce0e08e2SPeter Avalos (type *) mem_alloc((sizeof (type) * (size)))
320ce0e08e2SPeter Avalos
321ce0e08e2SPeter Avalos #define MEMZERO(addr, type, size) \
322ce0e08e2SPeter Avalos memset((void *) (addr), 0, sizeof (type) * (int) (size))
323ce0e08e2SPeter Avalos
324ce0e08e2SPeter Avalos #define FREE(addr, type, size) \
325ce0e08e2SPeter Avalos mem_free((addr), (sizeof (type) * (size)))
326ce0e08e2SPeter Avalos
327ce0e08e2SPeter Avalos /*
328ce0e08e2SPeter Avalos * An entry in the cache
329ce0e08e2SPeter Avalos */
330ce0e08e2SPeter Avalos typedef struct cache_node *cache_ptr;
331ce0e08e2SPeter Avalos struct cache_node {
332ce0e08e2SPeter Avalos /*
333ce0e08e2SPeter Avalos * Index into cache is xid, proc, vers, prog and address
334ce0e08e2SPeter Avalos */
335ce0e08e2SPeter Avalos u_int32_t cache_xid;
336ce0e08e2SPeter Avalos rpcproc_t cache_proc;
337ce0e08e2SPeter Avalos rpcvers_t cache_vers;
338ce0e08e2SPeter Avalos rpcprog_t cache_prog;
339ce0e08e2SPeter Avalos struct netbuf cache_addr;
340ce0e08e2SPeter Avalos /*
341ce0e08e2SPeter Avalos * The cached reply and length
342ce0e08e2SPeter Avalos */
343ce0e08e2SPeter Avalos char *cache_reply;
344ce0e08e2SPeter Avalos size_t cache_replylen;
345ce0e08e2SPeter Avalos /*
346ce0e08e2SPeter Avalos * Next node on the list, if there is a collision
347ce0e08e2SPeter Avalos */
348ce0e08e2SPeter Avalos cache_ptr cache_next;
349ce0e08e2SPeter Avalos };
350ce0e08e2SPeter Avalos
351ce0e08e2SPeter Avalos /*
352ce0e08e2SPeter Avalos * The entire cache
353ce0e08e2SPeter Avalos */
354ce0e08e2SPeter Avalos struct cl_cache {
355ce0e08e2SPeter Avalos u_int uc_size; /* size of cache */
356ce0e08e2SPeter Avalos cache_ptr *uc_entries; /* hash table of entries in cache */
357ce0e08e2SPeter Avalos cache_ptr *uc_fifo; /* fifo list of entries in cache */
358ce0e08e2SPeter Avalos u_int uc_nextvictim; /* points to next victim in fifo list */
359ce0e08e2SPeter Avalos rpcprog_t uc_prog; /* saved program number */
360ce0e08e2SPeter Avalos rpcvers_t uc_vers; /* saved version number */
361ce0e08e2SPeter Avalos rpcproc_t uc_proc; /* saved procedure number */
362ce0e08e2SPeter Avalos };
363ce0e08e2SPeter Avalos
364ce0e08e2SPeter Avalos
365ce0e08e2SPeter Avalos /*
366ce0e08e2SPeter Avalos * the hashing function
367ce0e08e2SPeter Avalos */
368ce0e08e2SPeter Avalos #define CACHE_LOC(transp, xid) \
369ce0e08e2SPeter Avalos (xid % (SPARSENESS * ((struct cl_cache *) \
370ce0e08e2SPeter Avalos su_data(transp)->su_cache)->uc_size))
371ce0e08e2SPeter Avalos
372ce0e08e2SPeter Avalos /*
373ce0e08e2SPeter Avalos * Enable use of the cache. Returns 1 on success, 0 on failure.
374ce0e08e2SPeter Avalos * Note: there is no disable.
375ce0e08e2SPeter Avalos */
376ce0e08e2SPeter Avalos static const char cache_enable_str[] = "svc_enablecache: %s %s";
377ce0e08e2SPeter Avalos static const char alloc_err[] = "could not allocate cache ";
378ce0e08e2SPeter Avalos static const char enable_err[] = "cache already enabled";
379ce0e08e2SPeter Avalos
380ce0e08e2SPeter Avalos int
svc_dg_enablecache(SVCXPRT * transp,u_int size)381ce0e08e2SPeter Avalos svc_dg_enablecache(SVCXPRT *transp, u_int size)
382ce0e08e2SPeter Avalos {
383ce0e08e2SPeter Avalos struct svc_dg_data *su = su_data(transp);
384ce0e08e2SPeter Avalos struct cl_cache *uc;
385ce0e08e2SPeter Avalos
386ce0e08e2SPeter Avalos mutex_lock(&dupreq_lock);
387ce0e08e2SPeter Avalos if (su->su_cache != NULL) {
388ce0e08e2SPeter Avalos warnx(cache_enable_str, enable_err, " ");
389ce0e08e2SPeter Avalos mutex_unlock(&dupreq_lock);
390ce0e08e2SPeter Avalos return (0);
391ce0e08e2SPeter Avalos }
392ce0e08e2SPeter Avalos uc = ALLOC(struct cl_cache, 1);
393ce0e08e2SPeter Avalos if (uc == NULL) {
394ce0e08e2SPeter Avalos warnx(cache_enable_str, alloc_err, " ");
395ce0e08e2SPeter Avalos mutex_unlock(&dupreq_lock);
396ce0e08e2SPeter Avalos return (0);
397ce0e08e2SPeter Avalos }
398ce0e08e2SPeter Avalos uc->uc_size = size;
399ce0e08e2SPeter Avalos uc->uc_nextvictim = 0;
400ce0e08e2SPeter Avalos uc->uc_entries = ALLOC(cache_ptr, size * SPARSENESS);
401ce0e08e2SPeter Avalos if (uc->uc_entries == NULL) {
402ce0e08e2SPeter Avalos warnx(cache_enable_str, alloc_err, "data");
403ce0e08e2SPeter Avalos FREE(uc, struct cl_cache, 1);
404ce0e08e2SPeter Avalos mutex_unlock(&dupreq_lock);
405ce0e08e2SPeter Avalos return (0);
406ce0e08e2SPeter Avalos }
407ce0e08e2SPeter Avalos MEMZERO(uc->uc_entries, cache_ptr, size * SPARSENESS);
408ce0e08e2SPeter Avalos uc->uc_fifo = ALLOC(cache_ptr, size);
409ce0e08e2SPeter Avalos if (uc->uc_fifo == NULL) {
410ce0e08e2SPeter Avalos warnx(cache_enable_str, alloc_err, "fifo");
411ce0e08e2SPeter Avalos FREE(uc->uc_entries, cache_ptr, size * SPARSENESS);
412ce0e08e2SPeter Avalos FREE(uc, struct cl_cache, 1);
413ce0e08e2SPeter Avalos mutex_unlock(&dupreq_lock);
414ce0e08e2SPeter Avalos return (0);
415ce0e08e2SPeter Avalos }
416ce0e08e2SPeter Avalos MEMZERO(uc->uc_fifo, cache_ptr, size);
417ce0e08e2SPeter Avalos su->su_cache = (char *)(void *)uc;
418ce0e08e2SPeter Avalos mutex_unlock(&dupreq_lock);
419ce0e08e2SPeter Avalos return (1);
420ce0e08e2SPeter Avalos }
421ce0e08e2SPeter Avalos
422ce0e08e2SPeter Avalos /*
423ce0e08e2SPeter Avalos * Set an entry in the cache. It assumes that the uc entry is set from
424ce0e08e2SPeter Avalos * the earlier call to cache_get() for the same procedure. This will always
425ce0e08e2SPeter Avalos * happen because cache_get() is calle by svc_dg_recv and cache_set() is called
426ce0e08e2SPeter Avalos * by svc_dg_reply(). All this hoopla because the right RPC parameters are
427ce0e08e2SPeter Avalos * not available at svc_dg_reply time.
428ce0e08e2SPeter Avalos */
429ce0e08e2SPeter Avalos
430ce0e08e2SPeter Avalos static const char cache_set_str[] = "cache_set: %s";
431ce0e08e2SPeter Avalos static const char cache_set_err1[] = "victim not found";
432ce0e08e2SPeter Avalos static const char cache_set_err2[] = "victim alloc failed";
433ce0e08e2SPeter Avalos static const char cache_set_err3[] = "could not allocate new rpc buffer";
434ce0e08e2SPeter Avalos
435ce0e08e2SPeter Avalos static void
cache_set(SVCXPRT * xprt,size_t replylen)436ce0e08e2SPeter Avalos cache_set(SVCXPRT *xprt, size_t replylen)
437ce0e08e2SPeter Avalos {
438ce0e08e2SPeter Avalos cache_ptr victim;
439ce0e08e2SPeter Avalos cache_ptr *vicp;
440ce0e08e2SPeter Avalos struct svc_dg_data *su = su_data(xprt);
441ce0e08e2SPeter Avalos struct cl_cache *uc = (struct cl_cache *) su->su_cache;
442ce0e08e2SPeter Avalos u_int loc;
443ce0e08e2SPeter Avalos char *newbuf;
444ce0e08e2SPeter Avalos #ifdef RPC_CACHE_DEBUG
445ce0e08e2SPeter Avalos struct netconfig *nconf;
446ce0e08e2SPeter Avalos char *uaddr;
447ce0e08e2SPeter Avalos #endif
448ce0e08e2SPeter Avalos
449ce0e08e2SPeter Avalos mutex_lock(&dupreq_lock);
450ce0e08e2SPeter Avalos /*
451ce0e08e2SPeter Avalos * Find space for the new entry, either by
452ce0e08e2SPeter Avalos * reusing an old entry, or by mallocing a new one
453ce0e08e2SPeter Avalos */
454ce0e08e2SPeter Avalos victim = uc->uc_fifo[uc->uc_nextvictim];
455ce0e08e2SPeter Avalos if (victim != NULL) {
456ce0e08e2SPeter Avalos loc = CACHE_LOC(xprt, victim->cache_xid);
457ce0e08e2SPeter Avalos for (vicp = &uc->uc_entries[loc];
458ce0e08e2SPeter Avalos *vicp != NULL && *vicp != victim;
459ce0e08e2SPeter Avalos vicp = &(*vicp)->cache_next)
460ce0e08e2SPeter Avalos ;
461ce0e08e2SPeter Avalos if (*vicp == NULL) {
462ce0e08e2SPeter Avalos warnx(cache_set_str, cache_set_err1);
463ce0e08e2SPeter Avalos mutex_unlock(&dupreq_lock);
464ce0e08e2SPeter Avalos return;
465ce0e08e2SPeter Avalos }
466ce0e08e2SPeter Avalos *vicp = victim->cache_next; /* remove from cache */
467ce0e08e2SPeter Avalos newbuf = victim->cache_reply;
468ce0e08e2SPeter Avalos } else {
469ce0e08e2SPeter Avalos victim = ALLOC(struct cache_node, 1);
470ce0e08e2SPeter Avalos if (victim == NULL) {
471ce0e08e2SPeter Avalos warnx(cache_set_str, cache_set_err2);
472ce0e08e2SPeter Avalos mutex_unlock(&dupreq_lock);
473ce0e08e2SPeter Avalos return;
474ce0e08e2SPeter Avalos }
475ce0e08e2SPeter Avalos newbuf = mem_alloc(su->su_iosz);
476ce0e08e2SPeter Avalos if (newbuf == NULL) {
477ce0e08e2SPeter Avalos warnx(cache_set_str, cache_set_err3);
478ce0e08e2SPeter Avalos FREE(victim, struct cache_node, 1);
479ce0e08e2SPeter Avalos mutex_unlock(&dupreq_lock);
480ce0e08e2SPeter Avalos return;
481ce0e08e2SPeter Avalos }
482ce0e08e2SPeter Avalos }
483ce0e08e2SPeter Avalos
484ce0e08e2SPeter Avalos /*
485ce0e08e2SPeter Avalos * Store it away
486ce0e08e2SPeter Avalos */
487ce0e08e2SPeter Avalos #ifdef RPC_CACHE_DEBUG
488ce0e08e2SPeter Avalos if (nconf = getnetconfigent(xprt->xp_netid)) {
489ce0e08e2SPeter Avalos uaddr = taddr2uaddr(nconf, &xprt->xp_rtaddr);
490ce0e08e2SPeter Avalos freenetconfigent(nconf);
491ce0e08e2SPeter Avalos printf(
492ce0e08e2SPeter Avalos "cache set for xid= %x prog=%d vers=%d proc=%d for rmtaddr=%s\n",
493ce0e08e2SPeter Avalos su->su_xid, uc->uc_prog, uc->uc_vers,
494ce0e08e2SPeter Avalos uc->uc_proc, uaddr);
495ce0e08e2SPeter Avalos free(uaddr);
496ce0e08e2SPeter Avalos }
497ce0e08e2SPeter Avalos #endif
498ce0e08e2SPeter Avalos victim->cache_replylen = replylen;
499ce0e08e2SPeter Avalos victim->cache_reply = rpc_buffer(xprt);
500ce0e08e2SPeter Avalos rpc_buffer(xprt) = newbuf;
501ce0e08e2SPeter Avalos xdrmem_create(&(su->su_xdrs), rpc_buffer(xprt),
502ce0e08e2SPeter Avalos su->su_iosz, XDR_ENCODE);
503ce0e08e2SPeter Avalos victim->cache_xid = su->su_xid;
504ce0e08e2SPeter Avalos victim->cache_proc = uc->uc_proc;
505ce0e08e2SPeter Avalos victim->cache_vers = uc->uc_vers;
506ce0e08e2SPeter Avalos victim->cache_prog = uc->uc_prog;
507ce0e08e2SPeter Avalos victim->cache_addr = xprt->xp_rtaddr;
508ce0e08e2SPeter Avalos victim->cache_addr.buf = ALLOC(char, xprt->xp_rtaddr.len);
509ce0e08e2SPeter Avalos memcpy(victim->cache_addr.buf, xprt->xp_rtaddr.buf,
510ce0e08e2SPeter Avalos (size_t)xprt->xp_rtaddr.len);
511ce0e08e2SPeter Avalos loc = CACHE_LOC(xprt, victim->cache_xid);
512ce0e08e2SPeter Avalos victim->cache_next = uc->uc_entries[loc];
513ce0e08e2SPeter Avalos uc->uc_entries[loc] = victim;
514ce0e08e2SPeter Avalos uc->uc_fifo[uc->uc_nextvictim++] = victim;
515ce0e08e2SPeter Avalos uc->uc_nextvictim %= uc->uc_size;
516ce0e08e2SPeter Avalos mutex_unlock(&dupreq_lock);
517ce0e08e2SPeter Avalos }
518ce0e08e2SPeter Avalos
519ce0e08e2SPeter Avalos /*
520ce0e08e2SPeter Avalos * Try to get an entry from the cache
521ce0e08e2SPeter Avalos * return 1 if found, 0 if not found and set the stage for cache_set()
522ce0e08e2SPeter Avalos */
523ce0e08e2SPeter Avalos static int
cache_get(SVCXPRT * xprt,struct rpc_msg * msg,char ** replyp,size_t * replylenp)524ce0e08e2SPeter Avalos cache_get(SVCXPRT *xprt, struct rpc_msg *msg, char **replyp, size_t *replylenp)
525ce0e08e2SPeter Avalos {
526ce0e08e2SPeter Avalos u_int loc;
527ce0e08e2SPeter Avalos cache_ptr ent;
528ce0e08e2SPeter Avalos struct svc_dg_data *su = su_data(xprt);
529ce0e08e2SPeter Avalos struct cl_cache *uc = (struct cl_cache *) su->su_cache;
530ce0e08e2SPeter Avalos #ifdef RPC_CACHE_DEBUG
531ce0e08e2SPeter Avalos struct netconfig *nconf;
532ce0e08e2SPeter Avalos char *uaddr;
533ce0e08e2SPeter Avalos #endif
534ce0e08e2SPeter Avalos
535ce0e08e2SPeter Avalos mutex_lock(&dupreq_lock);
536ce0e08e2SPeter Avalos loc = CACHE_LOC(xprt, su->su_xid);
537ce0e08e2SPeter Avalos for (ent = uc->uc_entries[loc]; ent != NULL; ent = ent->cache_next) {
538ce0e08e2SPeter Avalos if (ent->cache_xid == su->su_xid &&
539ce0e08e2SPeter Avalos ent->cache_proc == msg->rm_call.cb_proc &&
540ce0e08e2SPeter Avalos ent->cache_vers == msg->rm_call.cb_vers &&
541ce0e08e2SPeter Avalos ent->cache_prog == msg->rm_call.cb_prog &&
542ce0e08e2SPeter Avalos ent->cache_addr.len == xprt->xp_rtaddr.len &&
543ce0e08e2SPeter Avalos (memcmp(ent->cache_addr.buf, xprt->xp_rtaddr.buf,
544ce0e08e2SPeter Avalos xprt->xp_rtaddr.len) == 0)) {
545ce0e08e2SPeter Avalos #ifdef RPC_CACHE_DEBUG
546ce0e08e2SPeter Avalos if (nconf = getnetconfigent(xprt->xp_netid)) {
547ce0e08e2SPeter Avalos uaddr = taddr2uaddr(nconf, &xprt->xp_rtaddr);
548ce0e08e2SPeter Avalos freenetconfigent(nconf);
549ce0e08e2SPeter Avalos printf(
550ce0e08e2SPeter Avalos "cache entry found for xid=%x prog=%d vers=%d proc=%d for rmtaddr=%s\n",
551ce0e08e2SPeter Avalos su->su_xid, msg->rm_call.cb_prog,
552ce0e08e2SPeter Avalos msg->rm_call.cb_vers,
553ce0e08e2SPeter Avalos msg->rm_call.cb_proc, uaddr);
554ce0e08e2SPeter Avalos free(uaddr);
555ce0e08e2SPeter Avalos }
556ce0e08e2SPeter Avalos #endif
557ce0e08e2SPeter Avalos *replyp = ent->cache_reply;
558ce0e08e2SPeter Avalos *replylenp = ent->cache_replylen;
559ce0e08e2SPeter Avalos mutex_unlock(&dupreq_lock);
560ce0e08e2SPeter Avalos return (1);
561ce0e08e2SPeter Avalos }
562ce0e08e2SPeter Avalos }
563ce0e08e2SPeter Avalos /*
564ce0e08e2SPeter Avalos * Failed to find entry
565ce0e08e2SPeter Avalos * Remember a few things so we can do a set later
566ce0e08e2SPeter Avalos */
567ce0e08e2SPeter Avalos uc->uc_proc = msg->rm_call.cb_proc;
568ce0e08e2SPeter Avalos uc->uc_vers = msg->rm_call.cb_vers;
569ce0e08e2SPeter Avalos uc->uc_prog = msg->rm_call.cb_prog;
570ce0e08e2SPeter Avalos mutex_unlock(&dupreq_lock);
571ce0e08e2SPeter Avalos return (0);
572ce0e08e2SPeter Avalos }
573