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