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