svc_dg.c revision 1.12.8.2 1 1.12.8.2 christos /* $NetBSD: svc_dg.c,v 1.12.8.2 2008/04/25 17:44:45 christos Exp $ */
2 1.12.8.2 christos
3 1.12.8.2 christos /*
4 1.12.8.2 christos * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5 1.12.8.2 christos * unrestricted use provided that this legend is included on all tape
6 1.12.8.2 christos * media and as a part of the software program in whole or part. Users
7 1.12.8.2 christos * may copy or modify Sun RPC without charge, but are not authorized
8 1.12.8.2 christos * to license or distribute it to anyone else except as part of a product or
9 1.12.8.2 christos * program developed by the user.
10 1.12.8.2 christos *
11 1.12.8.2 christos * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 1.12.8.2 christos * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 1.12.8.2 christos * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 1.12.8.2 christos *
15 1.12.8.2 christos * Sun RPC is provided with no support and without any obligation on the
16 1.12.8.2 christos * part of Sun Microsystems, Inc. to assist in its use, correction,
17 1.12.8.2 christos * modification or enhancement.
18 1.12.8.2 christos *
19 1.12.8.2 christos * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 1.12.8.2 christos * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 1.12.8.2 christos * OR ANY PART THEREOF.
22 1.12.8.2 christos *
23 1.12.8.2 christos * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 1.12.8.2 christos * or profits or other special, indirect and consequential damages, even if
25 1.12.8.2 christos * Sun has been advised of the possibility of such damages.
26 1.12.8.2 christos *
27 1.12.8.2 christos * Sun Microsystems, Inc.
28 1.12.8.2 christos * 2550 Garcia Avenue
29 1.12.8.2 christos * Mountain View, California 94043
30 1.12.8.2 christos */
31 1.12.8.2 christos
32 1.12.8.2 christos /*
33 1.12.8.2 christos * Copyright (c) 1986-1991 by Sun Microsystems Inc.
34 1.12.8.2 christos */
35 1.12.8.2 christos
36 1.12.8.2 christos /* #ident "@(#)svc_dg.c 1.17 94/04/24 SMI" */
37 1.12.8.2 christos
38 1.12.8.2 christos
39 1.12.8.2 christos /*
40 1.12.8.2 christos * svc_dg.c, Server side for connectionless RPC.
41 1.12.8.2 christos *
42 1.12.8.2 christos * Does some caching in the hopes of achieving execute-at-most-once semantics.
43 1.12.8.2 christos */
44 1.12.8.2 christos
45 1.12.8.2 christos #include <sys/cdefs.h>
46 1.12.8.2 christos #if defined(LIBC_SCCS) && !defined(lint)
47 1.12.8.2 christos __RCSID("$NetBSD: svc_dg.c,v 1.12.8.2 2008/04/25 17:44:45 christos Exp $");
48 1.12.8.2 christos #endif
49 1.12.8.2 christos
50 1.12.8.2 christos #include "namespace.h"
51 1.12.8.2 christos #include "reentrant.h"
52 1.12.8.2 christos #include <sys/types.h>
53 1.12.8.2 christos #include <sys/socket.h>
54 1.12.8.2 christos #include <rpc/rpc.h>
55 1.12.8.2 christos #include <assert.h>
56 1.12.8.2 christos #include <errno.h>
57 1.12.8.2 christos #include <unistd.h>
58 1.12.8.2 christos #include <stdio.h>
59 1.12.8.2 christos #include <stdlib.h>
60 1.12.8.2 christos #include <string.h>
61 1.12.8.2 christos #ifdef RPC_CACHE_DEBUG
62 1.12.8.2 christos #include <netconfig.h>
63 1.12.8.2 christos #include <netdir.h>
64 1.12.8.2 christos #endif
65 1.12.8.2 christos #include <err.h>
66 1.12.8.2 christos
67 1.12.8.2 christos #include "rpc_internal.h"
68 1.12.8.2 christos #include "svc_dg.h"
69 1.12.8.2 christos
70 1.12.8.2 christos #define su_data(xprt) ((struct svc_dg_data *)(xprt->xp_p2))
71 1.12.8.2 christos #define rpc_buffer(xprt) ((xprt)->xp_p1)
72 1.12.8.2 christos
73 1.12.8.2 christos #ifdef __weak_alias
74 1.12.8.2 christos __weak_alias(svc_dg_create,_svc_dg_create)
75 1.12.8.2 christos #endif
76 1.12.8.2 christos
77 1.12.8.2 christos #ifndef MAX
78 1.12.8.2 christos #define MAX(a, b) (((a) > (b)) ? (a) : (b))
79 1.12.8.2 christos #endif
80 1.12.8.2 christos
81 1.12.8.2 christos static void svc_dg_ops __P((SVCXPRT *));
82 1.12.8.2 christos static enum xprt_stat svc_dg_stat __P((SVCXPRT *));
83 1.12.8.2 christos static bool_t svc_dg_recv __P((SVCXPRT *, struct rpc_msg *));
84 1.12.8.2 christos static bool_t svc_dg_reply __P((SVCXPRT *, struct rpc_msg *));
85 1.12.8.2 christos static bool_t svc_dg_getargs __P((SVCXPRT *, xdrproc_t, caddr_t));
86 1.12.8.2 christos static bool_t svc_dg_freeargs __P((SVCXPRT *, xdrproc_t, caddr_t));
87 1.12.8.2 christos static void svc_dg_destroy __P((SVCXPRT *));
88 1.12.8.2 christos static bool_t svc_dg_control __P((SVCXPRT *, const u_int, void *));
89 1.12.8.2 christos static int cache_get __P((SVCXPRT *, struct rpc_msg *, char **, size_t *));
90 1.12.8.2 christos static void cache_set __P((SVCXPRT *, size_t));
91 1.12.8.2 christos
92 1.12.8.2 christos /*
93 1.12.8.2 christos * Usage:
94 1.12.8.2 christos * xprt = svc_dg_create(sock, sendsize, recvsize);
95 1.12.8.2 christos * Does other connectionless specific initializations.
96 1.12.8.2 christos * Once *xprt is initialized, it is registered.
97 1.12.8.2 christos * see (svc.h, xprt_register). If recvsize or sendsize are 0 suitable
98 1.12.8.2 christos * system defaults are chosen.
99 1.12.8.2 christos * The routines returns NULL if a problem occurred.
100 1.12.8.2 christos */
101 1.12.8.2 christos static const char svc_dg_str[] = "svc_dg_create: %s";
102 1.12.8.2 christos static const char svc_dg_err1[] = "could not get transport information";
103 1.12.8.2 christos static const char svc_dg_err2[] = " transport does not support data transfer";
104 1.12.8.2 christos static const char __no_mem_str[] = "out of memory";
105 1.12.8.2 christos
106 1.12.8.2 christos SVCXPRT *
107 1.12.8.2 christos svc_dg_create(fd, sendsize, recvsize)
108 1.12.8.2 christos int fd;
109 1.12.8.2 christos u_int sendsize;
110 1.12.8.2 christos u_int recvsize;
111 1.12.8.2 christos {
112 1.12.8.2 christos SVCXPRT *xprt;
113 1.12.8.2 christos struct svc_dg_data *su = NULL;
114 1.12.8.2 christos struct __rpc_sockinfo si;
115 1.12.8.2 christos struct sockaddr_storage ss;
116 1.12.8.2 christos socklen_t slen;
117 1.12.8.2 christos
118 1.12.8.2 christos if (!__rpc_fd2sockinfo(fd, &si)) {
119 1.12.8.2 christos warnx(svc_dg_str, svc_dg_err1);
120 1.12.8.2 christos return (NULL);
121 1.12.8.2 christos }
122 1.12.8.2 christos /*
123 1.12.8.2 christos * Find the receive and the send size
124 1.12.8.2 christos */
125 1.12.8.2 christos sendsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsize);
126 1.12.8.2 christos recvsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsize);
127 1.12.8.2 christos if ((sendsize == 0) || (recvsize == 0)) {
128 1.12.8.2 christos warnx(svc_dg_str, svc_dg_err2);
129 1.12.8.2 christos return (NULL);
130 1.12.8.2 christos }
131 1.12.8.2 christos
132 1.12.8.2 christos xprt = mem_alloc(sizeof (SVCXPRT));
133 1.12.8.2 christos if (xprt == NULL)
134 1.12.8.2 christos goto freedata;
135 1.12.8.2 christos memset(xprt, 0, sizeof (SVCXPRT));
136 1.12.8.2 christos
137 1.12.8.2 christos su = mem_alloc(sizeof (*su));
138 1.12.8.2 christos if (su == NULL)
139 1.12.8.2 christos goto freedata;
140 1.12.8.2 christos su->su_iosz = ((MAX(sendsize, recvsize) + 3) / 4) * 4;
141 1.12.8.2 christos if ((rpc_buffer(xprt) = malloc(su->su_iosz)) == NULL)
142 1.12.8.2 christos goto freedata;
143 1.12.8.2 christos xdrmem_create(&(su->su_xdrs), rpc_buffer(xprt), su->su_iosz,
144 1.12.8.2 christos XDR_DECODE);
145 1.12.8.2 christos su->su_cache = NULL;
146 1.12.8.2 christos xprt->xp_fd = fd;
147 1.12.8.2 christos xprt->xp_p2 = (caddr_t)(void *)su;
148 1.12.8.2 christos xprt->xp_verf.oa_base = su->su_verfbody;
149 1.12.8.2 christos svc_dg_ops(xprt);
150 1.12.8.2 christos xprt->xp_rtaddr.maxlen = sizeof (struct sockaddr_storage);
151 1.12.8.2 christos
152 1.12.8.2 christos slen = sizeof ss;
153 1.12.8.2 christos if (getsockname(fd, (struct sockaddr *)(void *)&ss, &slen) < 0)
154 1.12.8.2 christos goto freedata;
155 1.12.8.2 christos xprt->xp_ltaddr.buf = mem_alloc(sizeof (struct sockaddr_storage));
156 1.12.8.2 christos xprt->xp_ltaddr.maxlen = sizeof (struct sockaddr_storage);
157 1.12.8.2 christos xprt->xp_ltaddr.len = slen;
158 1.12.8.2 christos memcpy(xprt->xp_ltaddr.buf, &ss, slen);
159 1.12.8.2 christos
160 1.12.8.2 christos xprt_register(xprt);
161 1.12.8.2 christos return (xprt);
162 1.12.8.2 christos freedata:
163 1.12.8.2 christos (void) warnx(svc_dg_str, __no_mem_str);
164 1.12.8.2 christos if (xprt) {
165 1.12.8.2 christos if (su)
166 1.12.8.2 christos (void) mem_free(su, sizeof (*su));
167 1.12.8.2 christos (void) mem_free(xprt, sizeof (SVCXPRT));
168 1.12.8.2 christos }
169 1.12.8.2 christos return (NULL);
170 1.12.8.2 christos }
171 1.12.8.2 christos
172 1.12.8.2 christos /*ARGSUSED*/
173 1.12.8.2 christos static enum xprt_stat
174 1.12.8.2 christos svc_dg_stat(xprt)
175 1.12.8.2 christos SVCXPRT *xprt;
176 1.12.8.2 christos {
177 1.12.8.2 christos return (XPRT_IDLE);
178 1.12.8.2 christos }
179 1.12.8.2 christos
180 1.12.8.2 christos static bool_t
181 1.12.8.2 christos svc_dg_recv(xprt, msg)
182 1.12.8.2 christos SVCXPRT *xprt;
183 1.12.8.2 christos struct rpc_msg *msg;
184 1.12.8.2 christos {
185 1.12.8.2 christos struct svc_dg_data *su;
186 1.12.8.2 christos XDR *xdrs;
187 1.12.8.2 christos char *reply;
188 1.12.8.2 christos struct sockaddr_storage ss;
189 1.12.8.2 christos socklen_t alen;
190 1.12.8.2 christos size_t replylen;
191 1.12.8.2 christos ssize_t rlen;
192 1.12.8.2 christos
193 1.12.8.2 christos _DIAGASSERT(xprt != NULL);
194 1.12.8.2 christos _DIAGASSERT(msg != NULL);
195 1.12.8.2 christos
196 1.12.8.2 christos su = su_data(xprt);
197 1.12.8.2 christos xdrs = &(su->su_xdrs);
198 1.12.8.2 christos
199 1.12.8.2 christos again:
200 1.12.8.2 christos alen = sizeof (struct sockaddr_storage);
201 1.12.8.2 christos rlen = recvfrom(xprt->xp_fd, rpc_buffer(xprt), su->su_iosz, 0,
202 1.12.8.2 christos (struct sockaddr *)(void *)&ss, &alen);
203 1.12.8.2 christos if (rlen == -1 && errno == EINTR)
204 1.12.8.2 christos goto again;
205 1.12.8.2 christos if (rlen == -1 || (rlen < (ssize_t)(4 * sizeof (u_int32_t))))
206 1.12.8.2 christos return (FALSE);
207 1.12.8.2 christos if (xprt->xp_rtaddr.len < alen) {
208 1.12.8.2 christos if (xprt->xp_rtaddr.len != 0)
209 1.12.8.2 christos mem_free(xprt->xp_rtaddr.buf, xprt->xp_rtaddr.len);
210 1.12.8.2 christos xprt->xp_rtaddr.buf = mem_alloc(alen);
211 1.12.8.2 christos xprt->xp_rtaddr.len = alen;
212 1.12.8.2 christos }
213 1.12.8.2 christos memcpy(xprt->xp_rtaddr.buf, &ss, alen);
214 1.12.8.2 christos #ifdef PORTMAP
215 1.12.8.2 christos if (ss.ss_family == AF_INET) {
216 1.12.8.2 christos xprt->xp_raddr = *(struct sockaddr_in *)xprt->xp_rtaddr.buf;
217 1.12.8.2 christos xprt->xp_addrlen = sizeof (struct sockaddr_in);
218 1.12.8.2 christos }
219 1.12.8.2 christos #endif
220 1.12.8.2 christos xdrs->x_op = XDR_DECODE;
221 1.12.8.2 christos XDR_SETPOS(xdrs, 0);
222 1.12.8.2 christos if (! xdr_callmsg(xdrs, msg)) {
223 1.12.8.2 christos return (FALSE);
224 1.12.8.2 christos }
225 1.12.8.2 christos su->su_xid = msg->rm_xid;
226 1.12.8.2 christos if (su->su_cache != NULL) {
227 1.12.8.2 christos if (cache_get(xprt, msg, &reply, &replylen)) {
228 1.12.8.2 christos (void)sendto(xprt->xp_fd, reply, replylen, 0,
229 1.12.8.2 christos (struct sockaddr *)(void *)&ss, alen);
230 1.12.8.2 christos return (FALSE);
231 1.12.8.2 christos }
232 1.12.8.2 christos }
233 1.12.8.2 christos return (TRUE);
234 1.12.8.2 christos }
235 1.12.8.2 christos
236 1.12.8.2 christos static bool_t
237 1.12.8.2 christos svc_dg_reply(xprt, msg)
238 1.12.8.2 christos SVCXPRT *xprt;
239 1.12.8.2 christos struct rpc_msg *msg;
240 1.12.8.2 christos {
241 1.12.8.2 christos struct svc_dg_data *su;
242 1.12.8.2 christos XDR *xdrs;
243 1.12.8.2 christos bool_t stat = FALSE;
244 1.12.8.2 christos size_t slen;
245 1.12.8.2 christos
246 1.12.8.2 christos _DIAGASSERT(xprt != NULL);
247 1.12.8.2 christos _DIAGASSERT(msg != NULL);
248 1.12.8.2 christos
249 1.12.8.2 christos su = su_data(xprt);
250 1.12.8.2 christos xdrs = &(su->su_xdrs);
251 1.12.8.2 christos
252 1.12.8.2 christos xdrs->x_op = XDR_ENCODE;
253 1.12.8.2 christos XDR_SETPOS(xdrs, 0);
254 1.12.8.2 christos msg->rm_xid = su->su_xid;
255 1.12.8.2 christos if (xdr_replymsg(xdrs, msg)) {
256 1.12.8.2 christos slen = XDR_GETPOS(xdrs);
257 1.12.8.2 christos if (sendto(xprt->xp_fd, rpc_buffer(xprt), slen, 0,
258 1.12.8.2 christos (struct sockaddr *)xprt->xp_rtaddr.buf,
259 1.12.8.2 christos (socklen_t)xprt->xp_rtaddr.len) == (ssize_t) slen) {
260 1.12.8.2 christos stat = TRUE;
261 1.12.8.2 christos if (su->su_cache)
262 1.12.8.2 christos cache_set(xprt, slen);
263 1.12.8.2 christos }
264 1.12.8.2 christos }
265 1.12.8.2 christos return (stat);
266 1.12.8.2 christos }
267 1.12.8.2 christos
268 1.12.8.2 christos static bool_t
269 1.12.8.2 christos svc_dg_getargs(xprt, xdr_args, args_ptr)
270 1.12.8.2 christos SVCXPRT *xprt;
271 1.12.8.2 christos xdrproc_t xdr_args;
272 1.12.8.2 christos caddr_t args_ptr;
273 1.12.8.2 christos {
274 1.12.8.2 christos return (*xdr_args)(&(su_data(xprt)->su_xdrs), args_ptr);
275 1.12.8.2 christos }
276 1.12.8.2 christos
277 1.12.8.2 christos static bool_t
278 1.12.8.2 christos svc_dg_freeargs(xprt, xdr_args, args_ptr)
279 1.12.8.2 christos SVCXPRT *xprt;
280 1.12.8.2 christos xdrproc_t xdr_args;
281 1.12.8.2 christos caddr_t args_ptr;
282 1.12.8.2 christos {
283 1.12.8.2 christos XDR *xdrs;
284 1.12.8.2 christos
285 1.12.8.2 christos _DIAGASSERT(xprt != NULL);
286 1.12.8.2 christos
287 1.12.8.2 christos xdrs = &(su_data(xprt)->su_xdrs);
288 1.12.8.2 christos xdrs->x_op = XDR_FREE;
289 1.12.8.2 christos return (*xdr_args)(xdrs, args_ptr);
290 1.12.8.2 christos }
291 1.12.8.2 christos
292 1.12.8.2 christos static void
293 1.12.8.2 christos svc_dg_destroy(xprt)
294 1.12.8.2 christos SVCXPRT *xprt;
295 1.12.8.2 christos {
296 1.12.8.2 christos struct svc_dg_data *su;
297 1.12.8.2 christos
298 1.12.8.2 christos _DIAGASSERT(xprt != NULL);
299 1.12.8.2 christos
300 1.12.8.2 christos su = su_data(xprt);
301 1.12.8.2 christos
302 1.12.8.2 christos xprt_unregister(xprt);
303 1.12.8.2 christos if (xprt->xp_fd != -1)
304 1.12.8.2 christos (void)close(xprt->xp_fd);
305 1.12.8.2 christos XDR_DESTROY(&(su->su_xdrs));
306 1.12.8.2 christos (void) mem_free(rpc_buffer(xprt), su->su_iosz);
307 1.12.8.2 christos (void) mem_free(su, sizeof (*su));
308 1.12.8.2 christos if (xprt->xp_rtaddr.buf)
309 1.12.8.2 christos (void) mem_free(xprt->xp_rtaddr.buf, xprt->xp_rtaddr.maxlen);
310 1.12.8.2 christos if (xprt->xp_ltaddr.buf)
311 1.12.8.2 christos (void) mem_free(xprt->xp_ltaddr.buf, xprt->xp_ltaddr.maxlen);
312 1.12.8.2 christos if (xprt->xp_tp)
313 1.12.8.2 christos (void) free(xprt->xp_tp);
314 1.12.8.2 christos (void) mem_free(xprt, sizeof (SVCXPRT));
315 1.12.8.2 christos }
316 1.12.8.2 christos
317 1.12.8.2 christos static bool_t
318 1.12.8.2 christos /*ARGSUSED*/
319 1.12.8.2 christos svc_dg_control(xprt, rq, in)
320 1.12.8.2 christos SVCXPRT *xprt;
321 1.12.8.2 christos const u_int rq;
322 1.12.8.2 christos void *in;
323 1.12.8.2 christos {
324 1.12.8.2 christos return (FALSE);
325 1.12.8.2 christos }
326 1.12.8.2 christos
327 1.12.8.2 christos static void
328 1.12.8.2 christos svc_dg_ops(xprt)
329 1.12.8.2 christos SVCXPRT *xprt;
330 1.12.8.2 christos {
331 1.12.8.2 christos static struct xp_ops ops;
332 1.12.8.2 christos static struct xp_ops2 ops2;
333 1.12.8.2 christos #ifdef _REENTRANT
334 1.12.8.2 christos extern mutex_t ops_lock;
335 1.12.8.2 christos #endif
336 1.12.8.2 christos
337 1.12.8.2 christos _DIAGASSERT(xprt != NULL);
338 1.12.8.2 christos
339 1.12.8.2 christos /* VARIABLES PROTECTED BY ops_lock: ops */
340 1.12.8.2 christos
341 1.12.8.2 christos mutex_lock(&ops_lock);
342 1.12.8.2 christos if (ops.xp_recv == NULL) {
343 1.12.8.2 christos ops.xp_recv = svc_dg_recv;
344 1.12.8.2 christos ops.xp_stat = svc_dg_stat;
345 1.12.8.2 christos ops.xp_getargs = svc_dg_getargs;
346 1.12.8.2 christos ops.xp_reply = svc_dg_reply;
347 1.12.8.2 christos ops.xp_freeargs = svc_dg_freeargs;
348 1.12.8.2 christos ops.xp_destroy = svc_dg_destroy;
349 1.12.8.2 christos ops2.xp_control = svc_dg_control;
350 1.12.8.2 christos }
351 1.12.8.2 christos xprt->xp_ops = &ops;
352 1.12.8.2 christos xprt->xp_ops2 = &ops2;
353 1.12.8.2 christos mutex_unlock(&ops_lock);
354 1.12.8.2 christos }
355 1.12.8.2 christos
356 1.12.8.2 christos /* The CACHING COMPONENT */
357 1.12.8.2 christos
358 1.12.8.2 christos /*
359 1.12.8.2 christos * Could have been a separate file, but some part of it depends upon the
360 1.12.8.2 christos * private structure of the client handle.
361 1.12.8.2 christos *
362 1.12.8.2 christos * Fifo cache for cl server
363 1.12.8.2 christos * Copies pointers to reply buffers into fifo cache
364 1.12.8.2 christos * Buffers are sent again if retransmissions are detected.
365 1.12.8.2 christos */
366 1.12.8.2 christos
367 1.12.8.2 christos #define SPARSENESS 4 /* 75% sparse */
368 1.12.8.2 christos
369 1.12.8.2 christos #define ALLOC(type, size) \
370 1.12.8.2 christos mem_alloc((sizeof (type) * (size)))
371 1.12.8.2 christos
372 1.12.8.2 christos #define MEMZERO(addr, type, size) \
373 1.12.8.2 christos (void) memset((void *) (addr), 0, sizeof (type) * (int) (size))
374 1.12.8.2 christos
375 1.12.8.2 christos #define FREE(addr, type, size) \
376 1.12.8.2 christos mem_free((addr), (sizeof (type) * (size)))
377 1.12.8.2 christos
378 1.12.8.2 christos /*
379 1.12.8.2 christos * An entry in the cache
380 1.12.8.2 christos */
381 1.12.8.2 christos typedef struct cache_node *cache_ptr;
382 1.12.8.2 christos struct cache_node {
383 1.12.8.2 christos /*
384 1.12.8.2 christos * Index into cache is xid, proc, vers, prog and address
385 1.12.8.2 christos */
386 1.12.8.2 christos u_int32_t cache_xid;
387 1.12.8.2 christos rpcproc_t cache_proc;
388 1.12.8.2 christos rpcvers_t cache_vers;
389 1.12.8.2 christos rpcprog_t cache_prog;
390 1.12.8.2 christos struct netbuf cache_addr;
391 1.12.8.2 christos /*
392 1.12.8.2 christos * The cached reply and length
393 1.12.8.2 christos */
394 1.12.8.2 christos char *cache_reply;
395 1.12.8.2 christos size_t cache_replylen;
396 1.12.8.2 christos /*
397 1.12.8.2 christos * Next node on the list, if there is a collision
398 1.12.8.2 christos */
399 1.12.8.2 christos cache_ptr cache_next;
400 1.12.8.2 christos };
401 1.12.8.2 christos
402 1.12.8.2 christos /*
403 1.12.8.2 christos * The entire cache
404 1.12.8.2 christos */
405 1.12.8.2 christos struct cl_cache {
406 1.12.8.2 christos u_int uc_size; /* size of cache */
407 1.12.8.2 christos cache_ptr *uc_entries; /* hash table of entries in cache */
408 1.12.8.2 christos cache_ptr *uc_fifo; /* fifo list of entries in cache */
409 1.12.8.2 christos u_int uc_nextvictim; /* points to next victim in fifo list */
410 1.12.8.2 christos rpcprog_t uc_prog; /* saved program number */
411 1.12.8.2 christos rpcvers_t uc_vers; /* saved version number */
412 1.12.8.2 christos rpcproc_t uc_proc; /* saved procedure number */
413 1.12.8.2 christos };
414 1.12.8.2 christos
415 1.12.8.2 christos
416 1.12.8.2 christos /*
417 1.12.8.2 christos * the hashing function
418 1.12.8.2 christos */
419 1.12.8.2 christos #define CACHE_LOC(transp, xid) \
420 1.12.8.2 christos (xid % (SPARSENESS * ((struct cl_cache *) \
421 1.12.8.2 christos su_data(transp)->su_cache)->uc_size))
422 1.12.8.2 christos
423 1.12.8.2 christos #ifdef _REENTRANT
424 1.12.8.2 christos extern mutex_t dupreq_lock;
425 1.12.8.2 christos #endif
426 1.12.8.2 christos
427 1.12.8.2 christos /*
428 1.12.8.2 christos * Enable use of the cache. Returns 1 on success, 0 on failure.
429 1.12.8.2 christos * Note: there is no disable.
430 1.12.8.2 christos */
431 1.12.8.2 christos static const char cache_enable_str[] = "svc_enablecache: %s %s";
432 1.12.8.2 christos static const char alloc_err[] = "could not allocate cache ";
433 1.12.8.2 christos static const char enable_err[] = "cache already enabled";
434 1.12.8.2 christos
435 1.12.8.2 christos int
436 1.12.8.2 christos svc_dg_enablecache(transp, size)
437 1.12.8.2 christos SVCXPRT *transp;
438 1.12.8.2 christos u_int size;
439 1.12.8.2 christos {
440 1.12.8.2 christos struct svc_dg_data *su;
441 1.12.8.2 christos struct cl_cache *uc;
442 1.12.8.2 christos
443 1.12.8.2 christos _DIAGASSERT(transp != NULL);
444 1.12.8.2 christos
445 1.12.8.2 christos su = su_data(transp);
446 1.12.8.2 christos
447 1.12.8.2 christos mutex_lock(&dupreq_lock);
448 1.12.8.2 christos if (su->su_cache != NULL) {
449 1.12.8.2 christos (void) warnx(cache_enable_str, enable_err, " ");
450 1.12.8.2 christos mutex_unlock(&dupreq_lock);
451 1.12.8.2 christos return (0);
452 1.12.8.2 christos }
453 1.12.8.2 christos uc = ALLOC(struct cl_cache, 1);
454 1.12.8.2 christos if (uc == NULL) {
455 1.12.8.2 christos warnx(cache_enable_str, alloc_err, " ");
456 1.12.8.2 christos mutex_unlock(&dupreq_lock);
457 1.12.8.2 christos return (0);
458 1.12.8.2 christos }
459 1.12.8.2 christos uc->uc_size = size;
460 1.12.8.2 christos uc->uc_nextvictim = 0;
461 1.12.8.2 christos uc->uc_entries = ALLOC(cache_ptr, size * SPARSENESS);
462 1.12.8.2 christos if (uc->uc_entries == NULL) {
463 1.12.8.2 christos warnx(cache_enable_str, alloc_err, "data");
464 1.12.8.2 christos FREE(uc, struct cl_cache, 1);
465 1.12.8.2 christos mutex_unlock(&dupreq_lock);
466 1.12.8.2 christos return (0);
467 1.12.8.2 christos }
468 1.12.8.2 christos MEMZERO(uc->uc_entries, cache_ptr, size * SPARSENESS);
469 1.12.8.2 christos uc->uc_fifo = ALLOC(cache_ptr, size);
470 1.12.8.2 christos if (uc->uc_fifo == NULL) {
471 1.12.8.2 christos warnx(cache_enable_str, alloc_err, "fifo");
472 1.12.8.2 christos FREE(uc->uc_entries, cache_ptr, size * SPARSENESS);
473 1.12.8.2 christos FREE(uc, struct cl_cache, 1);
474 1.12.8.2 christos mutex_unlock(&dupreq_lock);
475 1.12.8.2 christos return (0);
476 1.12.8.2 christos }
477 1.12.8.2 christos MEMZERO(uc->uc_fifo, cache_ptr, size);
478 1.12.8.2 christos su->su_cache = (char *)(void *)uc;
479 1.12.8.2 christos mutex_unlock(&dupreq_lock);
480 1.12.8.2 christos return (1);
481 1.12.8.2 christos }
482 1.12.8.2 christos
483 1.12.8.2 christos /*
484 1.12.8.2 christos * Set an entry in the cache. It assumes that the uc entry is set from
485 1.12.8.2 christos * the earlier call to cache_get() for the same procedure. This will always
486 1.12.8.2 christos * happen because cache_get() is calle by svc_dg_recv and cache_set() is called
487 1.12.8.2 christos * by svc_dg_reply(). All this hoopla because the right RPC parameters are
488 1.12.8.2 christos * not available at svc_dg_reply time.
489 1.12.8.2 christos */
490 1.12.8.2 christos
491 1.12.8.2 christos static const char cache_set_str[] = "cache_set: %s";
492 1.12.8.2 christos static const char cache_set_err1[] = "victim not found";
493 1.12.8.2 christos static const char cache_set_err2[] = "victim alloc failed";
494 1.12.8.2 christos static const char cache_set_err3[] = "could not allocate new rpc buffer";
495 1.12.8.2 christos
496 1.12.8.2 christos static void
497 1.12.8.2 christos cache_set(xprt, replylen)
498 1.12.8.2 christos SVCXPRT *xprt;
499 1.12.8.2 christos size_t replylen;
500 1.12.8.2 christos {
501 1.12.8.2 christos cache_ptr victim;
502 1.12.8.2 christos cache_ptr *vicp;
503 1.12.8.2 christos struct svc_dg_data *su;
504 1.12.8.2 christos struct cl_cache *uc;
505 1.12.8.2 christos u_int loc;
506 1.12.8.2 christos char *newbuf;
507 1.12.8.2 christos #ifdef RPC_CACHE_DEBUG
508 1.12.8.2 christos struct netconfig *nconf;
509 1.12.8.2 christos char *uaddr;
510 1.12.8.2 christos #endif
511 1.12.8.2 christos
512 1.12.8.2 christos _DIAGASSERT(xprt != NULL);
513 1.12.8.2 christos
514 1.12.8.2 christos su = su_data(xprt);
515 1.12.8.2 christos uc = (struct cl_cache *) su->su_cache;
516 1.12.8.2 christos
517 1.12.8.2 christos mutex_lock(&dupreq_lock);
518 1.12.8.2 christos /*
519 1.12.8.2 christos * Find space for the new entry, either by
520 1.12.8.2 christos * reusing an old entry, or by mallocing a new one
521 1.12.8.2 christos */
522 1.12.8.2 christos victim = uc->uc_fifo[uc->uc_nextvictim];
523 1.12.8.2 christos if (victim != NULL) {
524 1.12.8.2 christos loc = CACHE_LOC(xprt, victim->cache_xid);
525 1.12.8.2 christos for (vicp = &uc->uc_entries[loc];
526 1.12.8.2 christos *vicp != NULL && *vicp != victim;
527 1.12.8.2 christos vicp = &(*vicp)->cache_next)
528 1.12.8.2 christos ;
529 1.12.8.2 christos if (*vicp == NULL) {
530 1.12.8.2 christos warnx(cache_set_str, cache_set_err1);
531 1.12.8.2 christos mutex_unlock(&dupreq_lock);
532 1.12.8.2 christos return;
533 1.12.8.2 christos }
534 1.12.8.2 christos *vicp = victim->cache_next; /* remove from cache */
535 1.12.8.2 christos newbuf = victim->cache_reply;
536 1.12.8.2 christos } else {
537 1.12.8.2 christos victim = ALLOC(struct cache_node, 1);
538 1.12.8.2 christos if (victim == NULL) {
539 1.12.8.2 christos warnx(cache_set_str, cache_set_err2);
540 1.12.8.2 christos mutex_unlock(&dupreq_lock);
541 1.12.8.2 christos return;
542 1.12.8.2 christos }
543 1.12.8.2 christos newbuf = mem_alloc(su->su_iosz);
544 1.12.8.2 christos if (newbuf == NULL) {
545 1.12.8.2 christos warnx(cache_set_str, cache_set_err3);
546 1.12.8.2 christos FREE(victim, struct cache_node, 1);
547 1.12.8.2 christos mutex_unlock(&dupreq_lock);
548 1.12.8.2 christos return;
549 1.12.8.2 christos }
550 1.12.8.2 christos }
551 1.12.8.2 christos
552 1.12.8.2 christos /*
553 1.12.8.2 christos * Store it away
554 1.12.8.2 christos */
555 1.12.8.2 christos #ifdef RPC_CACHE_DEBUG
556 1.12.8.2 christos if (nconf = getnetconfigent(xprt->xp_netid)) {
557 1.12.8.2 christos uaddr = taddr2uaddr(nconf, &xprt->xp_rtaddr);
558 1.12.8.2 christos freenetconfigent(nconf);
559 1.12.8.2 christos printf(
560 1.12.8.2 christos "cache set for xid= %x prog=%d vers=%d proc=%d for rmtaddr=%s\n",
561 1.12.8.2 christos su->su_xid, uc->uc_prog, uc->uc_vers,
562 1.12.8.2 christos uc->uc_proc, uaddr);
563 1.12.8.2 christos free(uaddr);
564 1.12.8.2 christos }
565 1.12.8.2 christos #endif
566 1.12.8.2 christos victim->cache_replylen = replylen;
567 1.12.8.2 christos victim->cache_reply = rpc_buffer(xprt);
568 1.12.8.2 christos rpc_buffer(xprt) = newbuf;
569 1.12.8.2 christos xdrmem_create(&(su->su_xdrs), rpc_buffer(xprt),
570 1.12.8.2 christos su->su_iosz, XDR_ENCODE);
571 1.12.8.2 christos victim->cache_xid = su->su_xid;
572 1.12.8.2 christos victim->cache_proc = uc->uc_proc;
573 1.12.8.2 christos victim->cache_vers = uc->uc_vers;
574 1.12.8.2 christos victim->cache_prog = uc->uc_prog;
575 1.12.8.2 christos victim->cache_addr = xprt->xp_rtaddr;
576 1.12.8.2 christos victim->cache_addr.buf = ALLOC(char, xprt->xp_rtaddr.len);
577 1.12.8.2 christos (void) memcpy(victim->cache_addr.buf, xprt->xp_rtaddr.buf,
578 1.12.8.2 christos (size_t)xprt->xp_rtaddr.len);
579 1.12.8.2 christos loc = CACHE_LOC(xprt, victim->cache_xid);
580 1.12.8.2 christos victim->cache_next = uc->uc_entries[loc];
581 1.12.8.2 christos uc->uc_entries[loc] = victim;
582 1.12.8.2 christos uc->uc_fifo[uc->uc_nextvictim++] = victim;
583 1.12.8.2 christos uc->uc_nextvictim %= uc->uc_size;
584 1.12.8.2 christos mutex_unlock(&dupreq_lock);
585 1.12.8.2 christos }
586 1.12.8.2 christos
587 1.12.8.2 christos /*
588 1.12.8.2 christos * Try to get an entry from the cache
589 1.12.8.2 christos * return 1 if found, 0 if not found and set the stage for cache_set()
590 1.12.8.2 christos */
591 1.12.8.2 christos static int
592 1.12.8.2 christos cache_get(xprt, msg, replyp, replylenp)
593 1.12.8.2 christos SVCXPRT *xprt;
594 1.12.8.2 christos struct rpc_msg *msg;
595 1.12.8.2 christos char **replyp;
596 1.12.8.2 christos size_t *replylenp;
597 1.12.8.2 christos {
598 1.12.8.2 christos u_int loc;
599 1.12.8.2 christos cache_ptr ent;
600 1.12.8.2 christos struct svc_dg_data *su;
601 1.12.8.2 christos struct cl_cache *uc;
602 1.12.8.2 christos #ifdef RPC_CACHE_DEBUG
603 1.12.8.2 christos struct netconfig *nconf;
604 1.12.8.2 christos char *uaddr;
605 1.12.8.2 christos #endif
606 1.12.8.2 christos
607 1.12.8.2 christos _DIAGASSERT(xprt != NULL);
608 1.12.8.2 christos _DIAGASSERT(msg != NULL);
609 1.12.8.2 christos _DIAGASSERT(replyp != NULL);
610 1.12.8.2 christos _DIAGASSERT(replylenp != NULL);
611 1.12.8.2 christos
612 1.12.8.2 christos su = su_data(xprt);
613 1.12.8.2 christos uc = (struct cl_cache *) su->su_cache;
614 1.12.8.2 christos
615 1.12.8.2 christos mutex_lock(&dupreq_lock);
616 1.12.8.2 christos loc = CACHE_LOC(xprt, su->su_xid);
617 1.12.8.2 christos for (ent = uc->uc_entries[loc]; ent != NULL; ent = ent->cache_next) {
618 1.12.8.2 christos if (ent->cache_xid == su->su_xid &&
619 1.12.8.2 christos ent->cache_proc == msg->rm_call.cb_proc &&
620 1.12.8.2 christos ent->cache_vers == msg->rm_call.cb_vers &&
621 1.12.8.2 christos ent->cache_prog == msg->rm_call.cb_prog &&
622 1.12.8.2 christos ent->cache_addr.len == xprt->xp_rtaddr.len &&
623 1.12.8.2 christos (memcmp(ent->cache_addr.buf, xprt->xp_rtaddr.buf,
624 1.12.8.2 christos xprt->xp_rtaddr.len) == 0)) {
625 1.12.8.2 christos #ifdef RPC_CACHE_DEBUG
626 1.12.8.2 christos if (nconf = getnetconfigent(xprt->xp_netid)) {
627 1.12.8.2 christos uaddr = taddr2uaddr(nconf, &xprt->xp_rtaddr);
628 1.12.8.2 christos freenetconfigent(nconf);
629 1.12.8.2 christos printf(
630 1.12.8.2 christos "cache entry found for xid=%x prog=%d vers=%d proc=%d for rmtaddr=%s\n",
631 1.12.8.2 christos su->su_xid, msg->rm_call.cb_prog,
632 1.12.8.2 christos msg->rm_call.cb_vers,
633 1.12.8.2 christos msg->rm_call.cb_proc, uaddr);
634 1.12.8.2 christos free(uaddr);
635 1.12.8.2 christos }
636 1.12.8.2 christos #endif
637 1.12.8.2 christos *replyp = ent->cache_reply;
638 1.12.8.2 christos *replylenp = ent->cache_replylen;
639 1.12.8.2 christos mutex_unlock(&dupreq_lock);
640 1.12.8.2 christos return (1);
641 1.12.8.2 christos }
642 1.12.8.2 christos }
643 1.12.8.2 christos /*
644 1.12.8.2 christos * Failed to find entry
645 1.12.8.2 christos * Remember a few things so we can do a set later
646 1.12.8.2 christos */
647 1.12.8.2 christos uc->uc_proc = msg->rm_call.cb_proc;
648 1.12.8.2 christos uc->uc_vers = msg->rm_call.cb_vers;
649 1.12.8.2 christos uc->uc_prog = msg->rm_call.cb_prog;
650 1.12.8.2 christos mutex_unlock(&dupreq_lock);
651 1.12.8.2 christos return (0);
652 1.12.8.2 christos }
653