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